parser.c (struct cp_parser): Add access_checks_lists field
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "ggc.h"
36 #include "toplev.h"
37 #include "output.h"
38
39 \f
40 /* The lexer.  */
41
42 /* Overview
43    --------
44
45    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
46    look-ahead.
47
48    Methodology
49    -----------
50
51    We use a circular buffer to store incoming tokens.
52
53    Some artifacts of the C++ language (such as the
54    expression/declaration ambiguity) require arbitrary look-ahead.
55    The strategy we adopt for dealing with these problems is to attempt
56    to parse one construct (e.g., the declaration) and fall back to the
57    other (e.g., the expression) if that attempt does not succeed.
58    Therefore, we must sometimes store an arbitrary number of tokens.
59
60    The parser routinely peeks at the next token, and then consumes it
61    later.  That also requires a buffer in which to store the tokens.
62      
63    In order to easily permit adding tokens to the end of the buffer,
64    while removing them from the beginning of the buffer, we use a
65    circular buffer.  */
66
67 /* A C++ token.  */
68
69 typedef struct cp_token GTY (())
70 {
71   /* The kind of token.  */
72   enum cpp_ttype type;
73   /* The value associated with this token, if any.  */
74   tree value;
75   /* If this token is a keyword, this value indicates which keyword.
76      Otherwise, this value is RID_MAX.  */
77   enum rid keyword;
78   /* The file in which this token was found.  */
79   const char *file_name;
80   /* The line at which this token was found.  */
81   int line_number;
82 } cp_token;
83
84 /* The number of tokens in a single token block.  */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
87
88 /* A group of tokens.  These groups are chained together to store
89    large numbers of tokens.  (For example, a token block is created
90    when the body of an inline member function is first encountered;
91    the tokens are processed later after the class definition is
92    complete.)  
93
94    This somewhat ungainly data structure (as opposed to, say, a
95    variable-length array), is used due to contraints imposed by the
96    current garbage-collection methodology.  If it is made more
97    flexible, we could perhaps simplify the data structures involved.  */
98
99 typedef struct cp_token_block GTY (())
100 {
101   /* The tokens.  */
102   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103   /* The number of tokens in this block.  */
104   size_t num_tokens;
105   /* The next token block in the chain.  */
106   struct cp_token_block *next;
107   /* The previous block in the chain.  */
108   struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113   /* The first block in the cache.  NULL if there are no tokens in the
114      cache.  */
115   cp_token_block *first;
116   /* The last block in the cache.  NULL If there are no tokens in the
117      cache.  */
118   cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes. */
122
123 static cp_token_cache *cp_token_cache_new 
124   (void);
125 static void cp_token_cache_push_token
126   (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache.  */
129
130 static cp_token_cache *
131 cp_token_cache_new ()
132 {
133   return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE.  */
137
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140                            cp_token *token)
141 {
142   cp_token_block *b = cache->last;
143
144   /* See if we need to allocate a new token block.  */
145   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146     {
147       b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
148       b->prev = cache->last;
149       if (cache->last)
150         {
151           cache->last->next = b;
152           cache->last = b;
153         }
154       else
155         cache->first = cache->last = b;
156     }
157   /* Add this token to the current token block.  */
158   b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer.  It is responsible
162    for managing the token stream from the preprocessor and supplying
163    it to the parser.  */
164
165 typedef struct cp_lexer GTY (())
166 {
167   /* The memory allocated for the buffer.  Never NULL.  */
168   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169   /* A pointer just past the end of the memory allocated for the buffer.  */
170   cp_token * GTY ((skip (""))) buffer_end;
171   /* The first valid token in the buffer, or NULL if none.  */
172   cp_token * GTY ((skip (""))) first_token;
173   /* The next available token.  If NEXT_TOKEN is NULL, then there are
174      no more available tokens.  */
175   cp_token * GTY ((skip (""))) next_token;
176   /* A pointer just past the last available token.  If FIRST_TOKEN is
177      NULL, however, there are no available tokens, and then this
178      location is simply the place in which the next token read will be
179      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180      When the LAST_TOKEN == BUFFER, then the last token is at the
181      highest memory address in the BUFFER.  */
182   cp_token * GTY ((skip (""))) last_token;
183
184   /* A stack indicating positions at which cp_lexer_save_tokens was
185      called.  The top entry is the most recent position at which we
186      began saving tokens.  The entries are differences in token
187      position between FIRST_TOKEN and the first saved token.
188
189      If the stack is non-empty, we are saving tokens.  When a token is
190      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191      pointer will not.  The token stream will be preserved so that it
192      can be reexamined later.
193
194      If the stack is empty, then we are not saving tokens.  Whenever a
195      token is consumed, the FIRST_TOKEN pointer will be moved, and the
196      consumed token will be gone forever.  */
197   varray_type saved_tokens;
198
199   /* The STRING_CST tokens encountered while processing the current
200      string literal.  */
201   varray_type string_tokens;
202
203   /* True if we should obtain more tokens from the preprocessor; false
204      if we are processing a saved token cache.  */
205   bool main_lexer_p;
206
207   /* True if we should output debugging information.  */
208   bool debugging_p;
209
210   /* The next lexer in a linked list of lexers.  */
211   struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes.  */
215
216 static cp_lexer *cp_lexer_new
217   PARAMS ((bool));
218 static cp_lexer *cp_lexer_new_from_tokens
219   PARAMS ((struct cp_token_cache *));
220 static int cp_lexer_saving_tokens
221   PARAMS ((const cp_lexer *));
222 static cp_token *cp_lexer_next_token
223   PARAMS ((cp_lexer *, cp_token *));
224 static ptrdiff_t cp_lexer_token_difference
225   PARAMS ((cp_lexer *, cp_token *, cp_token *));
226 static cp_token *cp_lexer_read_token
227   PARAMS ((cp_lexer *));
228 static void cp_lexer_maybe_grow_buffer
229   PARAMS ((cp_lexer *));
230 static void cp_lexer_get_preprocessor_token
231   PARAMS ((cp_lexer *, cp_token *));
232 static cp_token *cp_lexer_peek_token
233   PARAMS ((cp_lexer *));
234 static cp_token *cp_lexer_peek_nth_token
235   PARAMS ((cp_lexer *, size_t));
236 static inline bool cp_lexer_next_token_is
237   PARAMS ((cp_lexer *, enum cpp_ttype));
238 static bool cp_lexer_next_token_is_not
239   PARAMS ((cp_lexer *, enum cpp_ttype));
240 static bool cp_lexer_next_token_is_keyword
241   PARAMS ((cp_lexer *, enum rid));
242 static cp_token *cp_lexer_consume_token
243   PARAMS ((cp_lexer *));
244 static void cp_lexer_purge_token
245   (cp_lexer *);
246 static void cp_lexer_purge_tokens_after
247   (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249   PARAMS ((cp_lexer *));
250 static void cp_lexer_commit_tokens
251   PARAMS ((cp_lexer *));
252 static void cp_lexer_rollback_tokens
253   PARAMS ((cp_lexer *));
254 static inline void cp_lexer_set_source_position_from_token 
255   PARAMS ((cp_lexer *, const cp_token *));
256 static void cp_lexer_print_token
257   PARAMS ((FILE *, cp_token *));
258 static inline bool cp_lexer_debugging_p 
259   PARAMS ((cp_lexer *));
260 static void cp_lexer_start_debugging
261   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
264
265 /* Manifest constants.  */
266
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
269
270 /* A token type for keywords, as opposed to ordinary identifiers.  */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273 /* A token type for template-ids.  If a template-id is processed while
274    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275    the value of the CPP_TEMPLATE_ID is whatever was returned by
276    cp_parser_template_id.  */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279 /* A token type for nested-name-specifiers.  If a
280    nested-name-specifier is processed while parsing tentatively, it is
281    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283    cp_parser_nested_name_specifier_opt.  */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286 /* A token type for tokens that are not tokens at all; these are used
287    to mark the end of a token block.  */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290 /* Variables.  */
291
292 /* The stream to which debugging output should be written.  */
293 static FILE *cp_lexer_debug_stream;
294
295 /* Create a new C++ lexer.  If MAIN_LEXER_P is true the new lexer is
296    the main lexer -- i.e, the lexer that gets tokens from the
297    preprocessor.  Otherwise, it is a lexer that uses a cache of stored
298    tokens.  */
299
300 static cp_lexer *
301 cp_lexer_new (bool main_lexer_p)
302 {
303   cp_lexer *lexer;
304
305   /* Allocate the memory.  */
306   lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
307
308   /* Create the circular buffer.  */
309   lexer->buffer = ((cp_token *) 
310                    ggc_alloc (CP_TOKEN_BUFFER_SIZE * sizeof (cp_token)));
311   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
312
313   /* There are no tokens in the buffer.  */
314   lexer->last_token = lexer->buffer;
315
316   /* This lexer obtains more tokens by calling c_lex.  */
317   lexer->main_lexer_p = main_lexer_p;
318
319   /* Create the SAVED_TOKENS stack.  */
320   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
321   
322   /* Create the STRINGS array.  */
323   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
324
325   /* Assume we are not debugging.  */
326   lexer->debugging_p = false;
327
328   return lexer;
329 }
330
331 /* Create a new lexer whose token stream is primed with the TOKENS.
332    When these tokens are exhausted, no new tokens will be read.  */
333
334 static cp_lexer *
335 cp_lexer_new_from_tokens (cp_token_cache *tokens)
336 {
337   cp_lexer *lexer;
338   cp_token *token;
339   cp_token_block *block;
340   ptrdiff_t num_tokens;
341
342   /* Create the lexer.  */
343   lexer = cp_lexer_new (/*main_lexer_p=*/false);
344
345   /* Create a new buffer, appropriately sized.  */
346   num_tokens = 0;
347   for (block = tokens->first; block != NULL; block = block->next)
348     num_tokens += block->num_tokens;
349   lexer->buffer = ((cp_token *) 
350                    ggc_alloc (num_tokens * sizeof (cp_token)));
351   lexer->buffer_end = lexer->buffer + num_tokens;
352   
353   /* Install the tokens.  */
354   token = lexer->buffer;
355   for (block = tokens->first; block != NULL; block = block->next)
356     {
357       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
358       token += block->num_tokens;
359     }
360
361   /* The FIRST_TOKEN is the beginning of the buffer.  */
362   lexer->first_token = lexer->buffer;
363   /* The next available token is also at the beginning of the buffer.  */
364   lexer->next_token = lexer->buffer;
365   /* The buffer is full.  */
366   lexer->last_token = lexer->first_token;
367
368   return lexer;
369 }
370
371 /* Returns non-zero if debugging information should be output.  */
372
373 static inline bool
374 cp_lexer_debugging_p (cp_lexer *lexer)
375 {
376   return lexer->debugging_p;
377 }
378
379 /* Set the current source position from the information stored in
380    TOKEN.  */
381
382 static inline void
383 cp_lexer_set_source_position_from_token (lexer, token)
384      cp_lexer *lexer ATTRIBUTE_UNUSED;
385      const cp_token *token;
386 {
387   /* Ideally, the source position information would not be a global
388      variable, but it is.  */
389
390   /* Update the line number.  */
391   if (token->type != CPP_EOF)
392     {
393       lineno = token->line_number;
394       input_filename = token->file_name;
395     }
396 }
397
398 /* TOKEN points into the circular token buffer.  Return a pointer to
399    the next token in the buffer.  */
400
401 static inline cp_token *
402 cp_lexer_next_token (lexer, token)
403      cp_lexer *lexer;
404      cp_token *token;
405 {
406   token++;
407   if (token == lexer->buffer_end)
408     token = lexer->buffer;
409   return token;
410 }
411
412 /* Non-zero if we are presently saving tokens.  */
413
414 static int
415 cp_lexer_saving_tokens (lexer)
416      const cp_lexer *lexer;
417 {
418   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
419 }
420
421 /* Return a pointer to the token that is N tokens beyond TOKEN in the
422    buffer.  */
423
424 static cp_token *
425 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
426 {
427   token += n;
428   if (token >= lexer->buffer_end)
429     token = lexer->buffer + (token - lexer->buffer_end);
430   return token;
431 }
432
433 /* Returns the number of times that START would have to be incremented
434    to reach FINISH.  If START and FINISH are the same, returns zero.  */
435
436 static ptrdiff_t
437 cp_lexer_token_difference (lexer, start, finish)
438      cp_lexer *lexer;
439      cp_token *start;
440      cp_token *finish;
441 {
442   if (finish >= start)
443     return finish - start;
444   else
445     return ((lexer->buffer_end - lexer->buffer)
446             - (start - finish));
447 }
448
449 /* Obtain another token from the C preprocessor and add it to the
450    token buffer.  Returns the newly read token.  */
451
452 static cp_token *
453 cp_lexer_read_token (lexer)
454      cp_lexer *lexer;
455 {
456   cp_token *token;
457
458   /* Make sure there is room in the buffer.  */
459   cp_lexer_maybe_grow_buffer (lexer);
460
461   /* If there weren't any tokens, then this one will be the first.  */
462   if (!lexer->first_token)
463     lexer->first_token = lexer->last_token;
464   /* Similarly, if there were no available tokens, there is one now.  */
465   if (!lexer->next_token)
466     lexer->next_token = lexer->last_token;
467
468   /* Figure out where we're going to store the new token.  */
469   token = lexer->last_token;
470
471   /* Get a new token from the preprocessor.  */
472   cp_lexer_get_preprocessor_token (lexer, token);
473
474   /* Increment LAST_TOKEN.  */
475   lexer->last_token = cp_lexer_next_token (lexer, token);
476
477   /* The preprocessor does not yet do translation phase six, i.e., the
478      combination of adjacent string literals.  Therefore, we do it
479      here.  */
480   if (token->type == CPP_STRING || token->type == CPP_WSTRING)
481     {
482       ptrdiff_t delta;
483       int i;
484
485       /* When we grow the buffer, we may invalidate TOKEN.  So, save
486          the distance from the beginning of the BUFFER so that we can
487          recaulate it.  */
488       delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
489       /* Make sure there is room in the buffer for another token.  */
490       cp_lexer_maybe_grow_buffer (lexer);
491       /* Restore TOKEN.  */
492       token = lexer->buffer;
493       for (i = 0; i < delta; ++i)
494         token = cp_lexer_next_token (lexer, token);
495
496       VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
497       while (true)
498         {
499           /* Read the token after TOKEN.  */
500           cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
501           /* See whether it's another string constant.  */
502           if (lexer->last_token->type != token->type)
503             {
504               /* If not, then it will be the next real token.  */
505               lexer->last_token = cp_lexer_next_token (lexer, 
506                                                        lexer->last_token);
507               break;
508             }
509
510           /* Chain the strings together.  */
511           VARRAY_PUSH_TREE (lexer->string_tokens, 
512                             lexer->last_token->value);
513         }
514
515       /* Create a single STRING_CST.  Curiously we have to call
516          combine_strings even if there is only a single string in
517          order to get the type set correctly.  */
518       token->value = combine_strings (lexer->string_tokens);
519       VARRAY_CLEAR (lexer->string_tokens);
520       token->value = fix_string_type (token->value);
521       /* Strings should have type `const char []'.  Right now, we will
522          have an ARRAY_TYPE that is constant rather than an array of
523          constant elements.  */
524       if (flag_const_strings)
525         {
526           tree type;
527
528           /* Get the current type.  It will be an ARRAY_TYPE.  */
529           type = TREE_TYPE (token->value);
530           /* Use build_cplus_array_type to rebuild the array, thereby
531              getting the right type.  */
532           type = build_cplus_array_type (TREE_TYPE (type),
533                                          TYPE_DOMAIN (type));
534           /* Reset the type of the token.  */
535           TREE_TYPE (token->value) = type;
536         }
537     }
538
539   return token;
540 }
541
542 /* If the circular buffer is full, make it bigger.  */
543
544 static void
545 cp_lexer_maybe_grow_buffer (lexer)
546      cp_lexer *lexer;
547 {
548   /* If the buffer is full, enlarge it.  */
549   if (lexer->last_token == lexer->first_token)
550     {
551       cp_token *new_buffer;
552       cp_token *old_buffer;
553       cp_token *new_first_token;
554       ptrdiff_t buffer_length;
555       size_t num_tokens_to_copy;
556
557       /* Remember the current buffer pointer.  It will become invalid,
558          but we will need to do pointer arithmetic involving this
559          value.  */
560       old_buffer = lexer->buffer;
561       /* Compute the current buffer size.  */
562       buffer_length = lexer->buffer_end - lexer->buffer;
563       /* Allocate a buffer twice as big.  */
564       new_buffer = ((cp_token *)
565                     ggc_realloc (lexer->buffer, 
566                                  2 * buffer_length * sizeof (cp_token)));
567       
568       /* Because the buffer is circular, logically consecutive tokens
569          are not necessarily placed consecutively in memory.
570          Therefore, we must keep move the tokens that were before
571          FIRST_TOKEN to the second half of the newly allocated
572          buffer.  */
573       num_tokens_to_copy = (lexer->first_token - old_buffer);
574       memcpy (new_buffer + buffer_length,
575               new_buffer,
576               num_tokens_to_copy * sizeof (cp_token));
577       /* Clear the rest of the buffer.  We never look at this storage,
578          but the garbage collector may.  */
579       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
580               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
581
582       /* Now recompute all of the buffer pointers.  */
583       new_first_token 
584         = new_buffer + (lexer->first_token - old_buffer);
585       if (lexer->next_token != NULL)
586         {
587           ptrdiff_t next_token_delta;
588
589           if (lexer->next_token > lexer->first_token)
590             next_token_delta = lexer->next_token - lexer->first_token;
591           else
592             next_token_delta = 
593               buffer_length - (lexer->first_token - lexer->next_token);
594           lexer->next_token = new_first_token + next_token_delta;
595         }
596       lexer->last_token = new_first_token + buffer_length;
597       lexer->buffer = new_buffer;
598       lexer->buffer_end = new_buffer + buffer_length * 2;
599       lexer->first_token = new_first_token;
600     }
601 }
602
603 /* Store the next token from the preprocessor in *TOKEN.  */
604
605 static void 
606 cp_lexer_get_preprocessor_token (lexer, token)
607      cp_lexer *lexer ATTRIBUTE_UNUSED;
608      cp_token *token;
609 {
610   bool done;
611
612   /* If this not the main lexer, return a terminating CPP_EOF token.  */
613   if (!lexer->main_lexer_p)
614     {
615       token->type = CPP_EOF;
616       token->line_number = 0;
617       token->file_name = NULL;
618       token->value = NULL_TREE;
619       token->keyword = RID_MAX;
620
621       return;
622     }
623
624   done = false;
625   /* Keep going until we get a token we like.  */
626   while (!done)
627     {
628       /* Get a new token from the preprocessor.  */
629       token->type = c_lex (&token->value);
630       /* Issue messages about tokens we cannot process.  */
631       switch (token->type)
632         {
633         case CPP_ATSIGN:
634         case CPP_HASH:
635         case CPP_PASTE:
636           error ("invalid token");
637           break;
638
639         case CPP_OTHER:
640           /* These tokens are already warned about by c_lex.  */
641           break;
642
643         default:
644           /* This is a good token, so we exit the loop.  */
645           done = true;
646           break;
647         }
648     }
649   /* Now we've got our token.  */
650   token->line_number = lineno;
651   token->file_name = input_filename;
652
653   /* Check to see if this token is a keyword.  */
654   if (token->type == CPP_NAME 
655       && C_IS_RESERVED_WORD (token->value))
656     {
657       /* Mark this token as a keyword.  */
658       token->type = CPP_KEYWORD;
659       /* Record which keyword.  */
660       token->keyword = C_RID_CODE (token->value);
661       /* Update the value.  Some keywords are mapped to particular
662          entities, rather than simply having the value of the
663          corresponding IDENTIFIER_NODE.  For example, `__const' is
664          mapped to `const'.  */
665       token->value = ridpointers[token->keyword];
666     }
667   else
668     token->keyword = RID_MAX;
669 }
670
671 /* Return a pointer to the next token in the token stream, but do not
672    consume it.  */
673
674 static cp_token *
675 cp_lexer_peek_token (lexer)
676      cp_lexer *lexer;
677 {
678   cp_token *token;
679
680   /* If there are no tokens, read one now.  */
681   if (!lexer->next_token)
682     cp_lexer_read_token (lexer);
683
684   /* Provide debugging output.  */
685   if (cp_lexer_debugging_p (lexer))
686     {
687       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
688       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
689       fprintf (cp_lexer_debug_stream, "\n");
690     }
691
692   token = lexer->next_token;
693   cp_lexer_set_source_position_from_token (lexer, token);
694   return token;
695 }
696
697 /* Return true if the next token has the indicated TYPE.  */
698
699 static bool
700 cp_lexer_next_token_is (lexer, type)
701      cp_lexer *lexer;
702      enum cpp_ttype type;
703 {
704   cp_token *token;
705
706   /* Peek at the next token.  */
707   token = cp_lexer_peek_token (lexer);
708   /* Check to see if it has the indicated TYPE.  */
709   return token->type == type;
710 }
711
712 /* Return true if the next token does not have the indicated TYPE.  */
713
714 static bool
715 cp_lexer_next_token_is_not (lexer, type)
716      cp_lexer *lexer;
717      enum cpp_ttype type;
718 {
719   return !cp_lexer_next_token_is (lexer, type);
720 }
721
722 /* Return true if the next token is the indicated KEYWORD.  */
723
724 static bool
725 cp_lexer_next_token_is_keyword (lexer, keyword)
726      cp_lexer *lexer;
727      enum rid keyword;
728 {
729   cp_token *token;
730
731   /* Peek at the next token.  */
732   token = cp_lexer_peek_token (lexer);
733   /* Check to see if it is the indicated keyword.  */
734   return token->keyword == keyword;
735 }
736
737 /* Return a pointer to the Nth token in the token stream.  If N is 1,
738    then this is precisely equivalent to cp_lexer_peek_token.  */
739
740 static cp_token *
741 cp_lexer_peek_nth_token (lexer, n)
742      cp_lexer *lexer;
743      size_t n;
744 {
745   cp_token *token;
746
747   /* N is 1-based, not zero-based.  */
748   my_friendly_assert (n > 0, 20000224);
749
750   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
751   token = lexer->next_token;
752   /* If there are no tokens in the buffer, get one now.  */
753   if (!token)
754     {
755       cp_lexer_read_token (lexer);
756       token = lexer->next_token;
757     }
758
759   /* Now, read tokens until we have enough.  */
760   while (--n > 0)
761     {
762       /* Advance to the next token.  */
763       token = cp_lexer_next_token (lexer, token);
764       /* If that's all the tokens we have, read a new one.  */
765       if (token == lexer->last_token)
766         token = cp_lexer_read_token (lexer);
767     }
768
769   return token;
770 }
771
772 /* Consume the next token.  The pointer returned is valid only until
773    another token is read.  Callers should preserve copy the token
774    explicitly if they will need its value for a longer period of
775    time.  */
776
777 static cp_token *
778 cp_lexer_consume_token (lexer)
779      cp_lexer *lexer;
780 {
781   cp_token *token;
782
783   /* If there are no tokens, read one now.  */
784   if (!lexer->next_token)
785     cp_lexer_read_token (lexer);
786
787   /* Remember the token we'll be returning.  */
788   token = lexer->next_token;
789
790   /* Increment NEXT_TOKEN.  */
791   lexer->next_token = cp_lexer_next_token (lexer, 
792                                            lexer->next_token);
793   /* Check to see if we're all out of tokens.  */
794   if (lexer->next_token == lexer->last_token)
795     lexer->next_token = NULL;
796
797   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
798   if (!cp_lexer_saving_tokens (lexer))
799     {
800       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
801       if (!lexer->next_token)
802         lexer->first_token = NULL;
803       else
804         lexer->first_token = lexer->next_token;
805     }
806
807   /* Provide debugging output.  */
808   if (cp_lexer_debugging_p (lexer))
809     {
810       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
811       cp_lexer_print_token (cp_lexer_debug_stream, token);
812       fprintf (cp_lexer_debug_stream, "\n");
813     }
814
815   return token;
816 }
817
818 /* Permanently remove the next token from the token stream.  There
819    must be a valid next token already; this token never reads
820    additional tokens from the preprocessor.  */
821
822 static void
823 cp_lexer_purge_token (cp_lexer *lexer)
824 {
825   cp_token *token;
826   cp_token *next_token;
827
828   token = lexer->next_token;
829   while (true) 
830     {
831       next_token = cp_lexer_next_token (lexer, token);
832       if (next_token == lexer->last_token)
833         break;
834       *token = *next_token;
835       token = next_token;
836     }
837
838   lexer->last_token = token;
839   /* The token purged may have been the only token remaining; if so,
840      clear NEXT_TOKEN.  */
841   if (lexer->next_token == token)
842     lexer->next_token = NULL;
843 }
844
845 /* Permanently remove all tokens after TOKEN, up to, but not
846    including, the token that will be returned next by
847    cp_lexer_peek_token.  */
848
849 static void
850 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
851 {
852   cp_token *peek;
853   cp_token *t1;
854   cp_token *t2;
855
856   if (lexer->next_token)
857     {
858       /* Copy the tokens that have not yet been read to the location
859          immediately following TOKEN.  */
860       t1 = cp_lexer_next_token (lexer, token);
861       t2 = peek = cp_lexer_peek_token (lexer);
862       /* Move tokens into the vacant area between TOKEN and PEEK.  */
863       while (t2 != lexer->last_token)
864         {
865           *t1 = *t2;
866           t1 = cp_lexer_next_token (lexer, t1);
867           t2 = cp_lexer_next_token (lexer, t2);
868         }
869       /* Now, the next available token is right after TOKEN.  */
870       lexer->next_token = cp_lexer_next_token (lexer, token);
871       /* And the last token is wherever we ended up.  */
872       lexer->last_token = t1;
873     }
874   else
875     {
876       /* There are no tokens in the buffer, so there is nothing to
877          copy.  The last token in the buffer is TOKEN itself.  */
878       lexer->last_token = cp_lexer_next_token (lexer, token);
879     }
880 }
881
882 /* Begin saving tokens.  All tokens consumed after this point will be
883    preserved.  */
884
885 static void
886 cp_lexer_save_tokens (lexer)
887      cp_lexer *lexer;
888 {
889   /* Provide debugging output.  */
890   if (cp_lexer_debugging_p (lexer))
891     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
892
893   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
894      restore the tokens if required.  */
895   if (!lexer->next_token)
896     cp_lexer_read_token (lexer);
897
898   VARRAY_PUSH_INT (lexer->saved_tokens,
899                    cp_lexer_token_difference (lexer,
900                                               lexer->first_token,
901                                               lexer->next_token));
902 }
903
904 /* Commit to the portion of the token stream most recently saved.  */
905
906 static void
907 cp_lexer_commit_tokens (lexer)
908      cp_lexer *lexer;
909 {
910   /* Provide debugging output.  */
911   if (cp_lexer_debugging_p (lexer))
912     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
913
914   VARRAY_POP (lexer->saved_tokens);
915 }
916
917 /* Return all tokens saved since the last call to cp_lexer_save_tokens
918    to the token stream.  Stop saving tokens.  */
919
920 static void
921 cp_lexer_rollback_tokens (lexer)
922      cp_lexer *lexer;
923 {
924   size_t delta;
925
926   /* Provide debugging output.  */
927   if (cp_lexer_debugging_p (lexer))
928     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
929
930   /* Find the token that was the NEXT_TOKEN when we started saving
931      tokens.  */
932   delta = VARRAY_TOP_INT(lexer->saved_tokens);
933   /* Make it the next token again now.  */
934   lexer->next_token = cp_lexer_advance_token (lexer,
935                                               lexer->first_token, 
936                                               delta);
937   /* It might be the case that there wer no tokens when we started
938      saving tokens, but that there are some tokens now.  */
939   if (!lexer->next_token && lexer->first_token)
940     lexer->next_token = lexer->first_token;
941
942   /* Stop saving tokens.  */
943   VARRAY_POP (lexer->saved_tokens);
944 }
945
946 /* Print a representation of the TOKEN on the STREAM.  */
947
948 static void
949 cp_lexer_print_token (stream, token)
950      FILE *stream;
951      cp_token *token;
952 {
953   const char *token_type = NULL;
954
955   /* Figure out what kind of token this is.  */
956   switch (token->type)
957     {
958     case CPP_EQ:
959       token_type = "EQ";
960       break;
961
962     case CPP_COMMA:
963       token_type = "COMMA";
964       break;
965
966     case CPP_OPEN_PAREN:
967       token_type = "OPEN_PAREN";
968       break;
969
970     case CPP_CLOSE_PAREN:
971       token_type = "CLOSE_PAREN";
972       break;
973
974     case CPP_OPEN_BRACE:
975       token_type = "OPEN_BRACE";
976       break;
977
978     case CPP_CLOSE_BRACE:
979       token_type = "CLOSE_BRACE";
980       break;
981
982     case CPP_SEMICOLON:
983       token_type = "SEMICOLON";
984       break;
985
986     case CPP_NAME:
987       token_type = "NAME";
988       break;
989
990     case CPP_EOF:
991       token_type = "EOF";
992       break;
993
994     case CPP_KEYWORD:
995       token_type = "keyword";
996       break;
997
998       /* This is not a token that we know how to handle yet.  */
999     default:
1000       break;
1001     }
1002
1003   /* If we have a name for the token, print it out.  Otherwise, we
1004      simply give the numeric code.  */
1005   if (token_type)
1006     fprintf (stream, "%s", token_type);
1007   else
1008     fprintf (stream, "%d", token->type);
1009   /* And, for an identifier, print the identifier name.  */
1010   if (token->type == CPP_NAME 
1011       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1012          For example, `struct' is mapped to an INTEGER_CST.  */
1013       || (token->type == CPP_KEYWORD 
1014           && TREE_CODE (token->value) == IDENTIFIER_NODE))
1015     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1016 }
1017
1018 /* Start emitting debugging information.  */
1019
1020 static void
1021 cp_lexer_start_debugging (lexer)
1022      cp_lexer *lexer;
1023 {
1024   ++lexer->debugging_p;
1025 }
1026   
1027 /* Stop emitting debugging information.  */
1028
1029 static void
1030 cp_lexer_stop_debugging (lexer)
1031      cp_lexer *lexer;
1032 {
1033   --lexer->debugging_p;
1034 }
1035
1036 \f
1037 /* The parser.  */
1038
1039 /* Overview
1040    --------
1041
1042    A cp_parser parses the token stream as specified by the C++
1043    grammar.  Its job is purely parsing, not semantic analysis.  For
1044    example, the parser breaks the token stream into declarators,
1045    expressions, statements, and other similar syntactic constructs.
1046    It does not check that the types of the expressions on either side
1047    of an assignment-statement are compatible, or that a function is
1048    not declared with a parameter of type `void'.
1049
1050    The parser invokes routines elsewhere in the compiler to perform
1051    semantic analysis and to build up the abstract syntax tree for the
1052    code processed.  
1053
1054    The parser (and the template instantiation code, which is, in a
1055    way, a close relative of parsing) are the only parts of the
1056    compiler that should be calling push_scope and pop_scope, or
1057    related functions.  The parser (and template instantiation code)
1058    keeps track of what scope is presently active; everything else
1059    should simply honor that.  (The code that generates static
1060    initializers may also need to set the scope, in order to check
1061    access control correctly when emitting the initializers.)
1062
1063    Methodology
1064    -----------
1065    
1066    The parser is of the standard recursive-descent variety.  Upcoming
1067    tokens in the token stream are examined in order to determine which
1068    production to use when parsing a non-terminal.  Some C++ constructs
1069    require arbitrary look ahead to disambiguate.  For example, it is
1070    impossible, in the general case, to tell whether a statement is an
1071    expression or declaration without scanning the entire statement.
1072    Therefore, the parser is capable of "parsing tentatively."  When the
1073    parser is not sure what construct comes next, it enters this mode.
1074    Then, while we attempt to parse the construct, the parser queues up
1075    error messages, rather than issuing them immediately, and saves the
1076    tokens it consumes.  If the construct is parsed successfully, the
1077    parser "commits", i.e., it issues any queued error messages and
1078    the tokens that were being preserved are permanently discarded.
1079    If, however, the construct is not parsed successfully, the parser
1080    rolls back its state completely so that it can resume parsing using
1081    a different alternative.
1082
1083    Future Improvements
1084    -------------------
1085    
1086    The performance of the parser could probably be improved
1087    substantially.  Some possible improvements include:
1088
1089      - The expression parser recurses through the various levels of
1090        precedence as specified in the grammar, rather than using an
1091        operator-precedence technique.  Therefore, parsing a simple
1092        identifier requires multiple recursive calls.
1093
1094      - We could often eliminate the need to parse tentatively by
1095        looking ahead a little bit.  In some places, this approach
1096        might not entirely eliminate the need to parse tentatively, but
1097        it might still speed up the average case.  */
1098
1099 /* Flags that are passed to some parsing functions.  These values can
1100    be bitwise-ored together.  */
1101
1102 typedef enum cp_parser_flags
1103 {
1104   /* No flags.  */
1105   CP_PARSER_FLAGS_NONE = 0x0,
1106   /* The construct is optional.  If it is not present, then no error
1107      should be issued.  */
1108   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1109   /* When parsing a type-specifier, do not allow user-defined types.  */
1110   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1111 } cp_parser_flags;
1112
1113 /* The different kinds of ids that we ecounter.  */
1114
1115 typedef enum cp_parser_id_kind
1116 {
1117   /* Not an id at all.  */
1118   CP_PARSER_ID_KIND_NONE,
1119   /* An unqualified-id that is not a template-id.  */
1120   CP_PARSER_ID_KIND_UNQUALIFIED,
1121   /* An unqualified template-id.  */
1122   CP_PARSER_ID_KIND_TEMPLATE_ID,
1123   /* A qualified-id.  */
1124   CP_PARSER_ID_KIND_QUALIFIED
1125 } cp_parser_id_kind;
1126
1127 /* A mapping from a token type to a corresponding tree node type.  */
1128
1129 typedef struct cp_parser_token_tree_map_node
1130 {
1131   /* The token type.  */
1132   enum cpp_ttype token_type;
1133   /* The corresponding tree code.  */
1134   enum tree_code tree_type;
1135 } cp_parser_token_tree_map_node;
1136
1137 /* A complete map consists of several ordinary entries, followed by a
1138    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1139
1140 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1141
1142 /* The status of a tentative parse.  */
1143
1144 typedef enum cp_parser_status_kind
1145 {
1146   /* No errors have occurred.  */
1147   CP_PARSER_STATUS_KIND_NO_ERROR,
1148   /* An error has occurred.  */
1149   CP_PARSER_STATUS_KIND_ERROR,
1150   /* We are committed to this tentative parse, whether or not an error
1151      has occurred.  */
1152   CP_PARSER_STATUS_KIND_COMMITTED
1153 } cp_parser_status_kind;
1154
1155 /* Context that is saved and restored when parsing tentatively.  */
1156
1157 typedef struct cp_parser_context GTY (())
1158 {
1159   /* If this is a tentative parsing context, the status of the
1160      tentative parse.  */
1161   enum cp_parser_status_kind status;
1162   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1163      that are looked up in this context must be looked up both in the
1164      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1165      the context of the containing expression.  */
1166   tree object_type;
1167   /* A TREE_LIST representing name-lookups for which we have deferred
1168      checking access controls.  We cannot check the accessibility of
1169      names used in a decl-specifier-seq until we know what is being
1170      declared because code like:
1171
1172        class A { 
1173          class B {};
1174          B* f();
1175        }
1176
1177        A::B* A::f() { return 0; }
1178
1179      is valid, even though `A::B' is not generally accessible.  
1180
1181      The TREE_PURPOSE of each node is the scope used to qualify the
1182      name being looked up; the TREE_VALUE is the DECL to which the
1183      name was resolved.  */
1184   tree deferred_access_checks;
1185   /* TRUE iff we are deferring access checks.  */
1186   bool deferring_access_checks_p;
1187   /* The next parsing context in the stack.  */
1188   struct cp_parser_context *next;
1189 } cp_parser_context;
1190
1191 /* Prototypes.  */
1192
1193 /* Constructors and destructors.  */
1194
1195 static cp_parser_context *cp_parser_context_new
1196   PARAMS ((cp_parser_context *));
1197
1198 /* Class variables.  */
1199
1200 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1201
1202 /* Constructors and destructors.  */
1203
1204 /* Construct a new context.  The context below this one on the stack
1205    is given by NEXT.  */
1206
1207 static cp_parser_context *
1208 cp_parser_context_new (next)
1209      cp_parser_context *next;
1210 {
1211   cp_parser_context *context;
1212
1213   /* Allocate the storage.  */
1214   if (cp_parser_context_free_list != NULL)
1215     {
1216       /* Pull the first entry from the free list.  */
1217       context = cp_parser_context_free_list;
1218       cp_parser_context_free_list = context->next;
1219       memset ((char *)context, 0, sizeof (*context));
1220     }
1221   else
1222     context = ((cp_parser_context *) 
1223                ggc_alloc_cleared (sizeof (cp_parser_context)));
1224   /* No errors have occurred yet in this context.  */
1225   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1226   /* If this is not the bottomost context, copy information that we
1227      need from the previous context.  */
1228   if (next)
1229     {
1230       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1231          expression, then we are parsing one in this context, too.  */
1232       context->object_type = next->object_type;
1233       /* We are deferring access checks here if we were in the NEXT
1234          context.  */
1235       context->deferring_access_checks_p 
1236         = next->deferring_access_checks_p;
1237       /* Thread the stack.  */
1238       context->next = next;
1239     }
1240
1241   return context;
1242 }
1243
1244 /* The cp_parser structure represents the C++ parser.  */
1245
1246 typedef struct cp_parser GTY(())
1247 {
1248   /* The lexer from which we are obtaining tokens.  */
1249   cp_lexer *lexer;
1250
1251   /* The scope in which names should be looked up.  If NULL_TREE, then
1252      we look up names in the scope that is currently open in the
1253      source program.  If non-NULL, this is either a TYPE or
1254      NAMESPACE_DECL for the scope in which we should look.  
1255
1256      This value is not cleared automatically after a name is looked
1257      up, so we must be careful to clear it before starting a new look
1258      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1259      will look up `Z' in the scope of `X', rather than the current
1260      scope.)  Unfortunately, it is difficult to tell when name lookup
1261      is complete, because we sometimes peek at a token, look it up,
1262      and then decide not to consume it.  */
1263   tree scope;
1264
1265   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1266      last lookup took place.  OBJECT_SCOPE is used if an expression
1267      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1268      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1269      form "X::Y"; it refers to X.  */
1270   tree object_scope;
1271   tree qualifying_scope;
1272
1273   /* A stack of parsing contexts.  All but the bottom entry on the
1274      stack will be tentative contexts.
1275
1276      We parse tentatively in order to determine which construct is in
1277      use in some situations.  For example, in order to determine
1278      whether a statement is an expression-statement or a
1279      declaration-statement we parse it tentatively as a
1280      declaration-statement.  If that fails, we then reparse the same
1281      token stream as an expression-statement.  */
1282   cp_parser_context *context;
1283
1284   /* True if we are parsing GNU C++.  If this flag is not set, then
1285      GNU extensions are not recognized.  */
1286   bool allow_gnu_extensions_p;
1287
1288   /* TRUE if the `>' token should be interpreted as the greater-than
1289      operator.  FALSE if it is the end of a template-id or
1290      template-parameter-list.  */
1291   bool greater_than_is_operator_p;
1292
1293   /* TRUE if default arguments are allowed within a parameter list
1294      that starts at this point. FALSE if only a gnu extension makes
1295      them permissable.  */
1296   bool default_arg_ok_p;
1297   
1298   /* TRUE if we are parsing an integral constant-expression.  See
1299      [expr.const] for a precise definition.  */
1300   /* FIXME: Need to implement code that checks this flag.  */
1301   bool constant_expression_p;
1302
1303   /* TRUE if local variable names and `this' are forbidden in the
1304      current context.  */
1305   bool local_variables_forbidden_p;
1306
1307   /* TRUE if the declaration we are parsing is part of a
1308      linkage-specification of the form `extern string-literal
1309      declaration'.  */
1310   bool in_unbraced_linkage_specification_p;
1311
1312   /* TRUE if we are presently parsing a declarator, after the
1313      direct-declarator.  */
1314   bool in_declarator_p;
1315
1316   /* If non-NULL, then we are parsing a construct where new type
1317      definitions are not permitted.  The string stored here will be
1318      issued as an error message if a type is defined.  */
1319   const char *type_definition_forbidden_message;
1320
1321   /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1322      during class parsing, and are not FUNCTION_DECLs.  G++ has an
1323      awkward extension allowing default args on pointers to functions
1324      etc.  */
1325   tree default_arg_types;
1326
1327   /* A TREE_LIST of queues of functions whose bodies have been lexed,
1328      but may not have been parsed.  These functions are friends of
1329      members defined within a class-specification; they are not
1330      procssed until the class is complete.  The active queue is at the
1331      front of the list.
1332
1333      Within each queue, functions appear in the reverse order that
1334      they appeared in the source.  The TREE_PURPOSE of each node is
1335      the class in which the function was defined or declared; the
1336      TREE_VALUE is the FUNCTION_DECL itself.  */
1337   tree unparsed_functions_queues;
1338
1339   /* The number of classes whose definitions are currently in
1340      progress.  */
1341   unsigned num_classes_being_defined;
1342
1343   /* The number of template parameter lists that apply directly to the
1344      current declaration.  */
1345   unsigned num_template_parameter_lists;
1346
1347   /* List of access checks lists, used to prevent GC collection while
1348      they are in use.  */
1349   tree access_checks_lists;
1350 } cp_parser;
1351
1352 /* The type of a function that parses some kind of expression  */
1353 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1354
1355 /* Prototypes.  */
1356
1357 /* Constructors and destructors.  */
1358
1359 static cp_parser *cp_parser_new
1360   PARAMS ((void));
1361
1362 /* Routines to parse various constructs.  
1363
1364    Those that return `tree' will return the error_mark_node (rather
1365    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1366    Sometimes, they will return an ordinary node if error-recovery was
1367    attempted, even though a parse error occurrred.  So, to check
1368    whether or not a parse error occurred, you should always use
1369    cp_parser_error_occurred.  If the construct is optional (indicated
1370    either by an `_opt' in the name of the function that does the
1371    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1372    the construct is not present.  */
1373
1374 /* Lexical conventions [gram.lex]  */
1375
1376 static tree cp_parser_identifier
1377   PARAMS ((cp_parser *));
1378
1379 /* Basic concepts [gram.basic]  */
1380
1381 static bool cp_parser_translation_unit
1382   PARAMS ((cp_parser *));
1383
1384 /* Expressions [gram.expr]  */
1385
1386 static tree cp_parser_primary_expression
1387   (cp_parser *, cp_parser_id_kind *, tree *);
1388 static tree cp_parser_id_expression
1389   PARAMS ((cp_parser *, bool, bool, bool *));
1390 static tree cp_parser_unqualified_id
1391   PARAMS ((cp_parser *, bool, bool));
1392 static tree cp_parser_nested_name_specifier_opt
1393   (cp_parser *, bool, bool, bool);
1394 static tree cp_parser_nested_name_specifier
1395   (cp_parser *, bool, bool, bool);
1396 static tree cp_parser_class_or_namespace_name
1397   (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_postfix_expression
1399   (cp_parser *, bool);
1400 static tree cp_parser_expression_list
1401   PARAMS ((cp_parser *));
1402 static void cp_parser_pseudo_destructor_name
1403   PARAMS ((cp_parser *, tree *, tree *));
1404 static tree cp_parser_unary_expression
1405   (cp_parser *, bool);
1406 static enum tree_code cp_parser_unary_operator
1407   PARAMS ((cp_token *));
1408 static tree cp_parser_new_expression
1409   PARAMS ((cp_parser *));
1410 static tree cp_parser_new_placement
1411   PARAMS ((cp_parser *));
1412 static tree cp_parser_new_type_id
1413   PARAMS ((cp_parser *));
1414 static tree cp_parser_new_declarator_opt
1415   PARAMS ((cp_parser *));
1416 static tree cp_parser_direct_new_declarator
1417   PARAMS ((cp_parser *));
1418 static tree cp_parser_new_initializer
1419   PARAMS ((cp_parser *));
1420 static tree cp_parser_delete_expression
1421   PARAMS ((cp_parser *));
1422 static tree cp_parser_cast_expression 
1423   (cp_parser *, bool);
1424 static tree cp_parser_pm_expression
1425   PARAMS ((cp_parser *));
1426 static tree cp_parser_multiplicative_expression
1427   PARAMS ((cp_parser *));
1428 static tree cp_parser_additive_expression
1429   PARAMS ((cp_parser *));
1430 static tree cp_parser_shift_expression
1431   PARAMS ((cp_parser *));
1432 static tree cp_parser_relational_expression
1433   PARAMS ((cp_parser *));
1434 static tree cp_parser_equality_expression
1435   PARAMS ((cp_parser *));
1436 static tree cp_parser_and_expression
1437   PARAMS ((cp_parser *));
1438 static tree cp_parser_exclusive_or_expression
1439   PARAMS ((cp_parser *));
1440 static tree cp_parser_inclusive_or_expression
1441   PARAMS ((cp_parser *));
1442 static tree cp_parser_logical_and_expression
1443   PARAMS ((cp_parser *));
1444 static tree cp_parser_logical_or_expression 
1445   PARAMS ((cp_parser *));
1446 static tree cp_parser_conditional_expression
1447   PARAMS ((cp_parser *));
1448 static tree cp_parser_question_colon_clause
1449   PARAMS ((cp_parser *, tree));
1450 static tree cp_parser_assignment_expression
1451   PARAMS ((cp_parser *));
1452 static enum tree_code cp_parser_assignment_operator_opt
1453   PARAMS ((cp_parser *));
1454 static tree cp_parser_expression
1455   PARAMS ((cp_parser *));
1456 static tree cp_parser_constant_expression
1457   PARAMS ((cp_parser *));
1458
1459 /* Statements [gram.stmt.stmt]  */
1460
1461 static void cp_parser_statement
1462   PARAMS ((cp_parser *));
1463 static tree cp_parser_labeled_statement
1464   PARAMS ((cp_parser *));
1465 static tree cp_parser_expression_statement
1466   PARAMS ((cp_parser *));
1467 static tree cp_parser_compound_statement
1468   (cp_parser *);
1469 static void cp_parser_statement_seq_opt
1470   PARAMS ((cp_parser *));
1471 static tree cp_parser_selection_statement
1472   PARAMS ((cp_parser *));
1473 static tree cp_parser_condition
1474   PARAMS ((cp_parser *));
1475 static tree cp_parser_iteration_statement
1476   PARAMS ((cp_parser *));
1477 static void cp_parser_for_init_statement
1478   PARAMS ((cp_parser *));
1479 static tree cp_parser_jump_statement
1480   PARAMS ((cp_parser *));
1481 static void cp_parser_declaration_statement
1482   PARAMS ((cp_parser *));
1483
1484 static tree cp_parser_implicitly_scoped_statement
1485   PARAMS ((cp_parser *));
1486 static void cp_parser_already_scoped_statement
1487   PARAMS ((cp_parser *));
1488
1489 /* Declarations [gram.dcl.dcl] */
1490
1491 static void cp_parser_declaration_seq_opt
1492   PARAMS ((cp_parser *));
1493 static void cp_parser_declaration
1494   PARAMS ((cp_parser *));
1495 static void cp_parser_block_declaration
1496   PARAMS ((cp_parser *, bool));
1497 static void cp_parser_simple_declaration
1498   PARAMS ((cp_parser *, bool));
1499 static tree cp_parser_decl_specifier_seq 
1500   PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1501 static tree cp_parser_storage_class_specifier_opt
1502   PARAMS ((cp_parser *));
1503 static tree cp_parser_function_specifier_opt
1504   PARAMS ((cp_parser *));
1505 static tree cp_parser_type_specifier
1506  (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1507 static tree cp_parser_simple_type_specifier
1508   PARAMS ((cp_parser *, cp_parser_flags));
1509 static tree cp_parser_type_name
1510   PARAMS ((cp_parser *));
1511 static tree cp_parser_elaborated_type_specifier
1512   PARAMS ((cp_parser *, bool, bool));
1513 static tree cp_parser_enum_specifier
1514   PARAMS ((cp_parser *));
1515 static void cp_parser_enumerator_list
1516   PARAMS ((cp_parser *, tree));
1517 static void cp_parser_enumerator_definition 
1518   PARAMS ((cp_parser *, tree));
1519 static tree cp_parser_namespace_name
1520   PARAMS ((cp_parser *));
1521 static void cp_parser_namespace_definition
1522   PARAMS ((cp_parser *));
1523 static void cp_parser_namespace_body
1524   PARAMS ((cp_parser *));
1525 static tree cp_parser_qualified_namespace_specifier
1526   PARAMS ((cp_parser *));
1527 static void cp_parser_namespace_alias_definition
1528   PARAMS ((cp_parser *));
1529 static void cp_parser_using_declaration
1530   PARAMS ((cp_parser *));
1531 static void cp_parser_using_directive
1532   PARAMS ((cp_parser *));
1533 static void cp_parser_asm_definition
1534   PARAMS ((cp_parser *));
1535 static void cp_parser_linkage_specification
1536   PARAMS ((cp_parser *));
1537
1538 /* Declarators [gram.dcl.decl] */
1539
1540 static tree cp_parser_init_declarator
1541   PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1542 static tree cp_parser_declarator
1543   PARAMS ((cp_parser *, bool, bool *));
1544 static tree cp_parser_direct_declarator
1545   PARAMS ((cp_parser *, bool, bool *));
1546 static enum tree_code cp_parser_ptr_operator
1547   PARAMS ((cp_parser *, tree *, tree *));
1548 static tree cp_parser_cv_qualifier_seq_opt
1549   PARAMS ((cp_parser *));
1550 static tree cp_parser_cv_qualifier_opt
1551   PARAMS ((cp_parser *));
1552 static tree cp_parser_declarator_id
1553   PARAMS ((cp_parser *));
1554 static tree cp_parser_type_id
1555   PARAMS ((cp_parser *));
1556 static tree cp_parser_type_specifier_seq
1557   PARAMS ((cp_parser *));
1558 static tree cp_parser_parameter_declaration_clause
1559   PARAMS ((cp_parser *));
1560 static tree cp_parser_parameter_declaration_list
1561   PARAMS ((cp_parser *));
1562 static tree cp_parser_parameter_declaration
1563   PARAMS ((cp_parser *, bool));
1564 static tree cp_parser_function_definition
1565   PARAMS ((cp_parser *, bool *));
1566 static void cp_parser_function_body
1567   (cp_parser *);
1568 static tree cp_parser_initializer
1569   PARAMS ((cp_parser *, bool *));
1570 static tree cp_parser_initializer_clause
1571   PARAMS ((cp_parser *));
1572 static tree cp_parser_initializer_list
1573   PARAMS ((cp_parser *));
1574
1575 static bool cp_parser_ctor_initializer_opt_and_function_body
1576   (cp_parser *);
1577
1578 /* Classes [gram.class] */
1579
1580 static tree cp_parser_class_name
1581   (cp_parser *, bool, bool, bool, bool, bool, bool);
1582 static tree cp_parser_class_specifier
1583   PARAMS ((cp_parser *));
1584 static tree cp_parser_class_head
1585   PARAMS ((cp_parser *, bool *, bool *, tree *));
1586 static enum tag_types cp_parser_class_key
1587   PARAMS ((cp_parser *));
1588 static void cp_parser_member_specification_opt
1589   PARAMS ((cp_parser *));
1590 static void cp_parser_member_declaration
1591   PARAMS ((cp_parser *));
1592 static tree cp_parser_pure_specifier
1593   PARAMS ((cp_parser *));
1594 static tree cp_parser_constant_initializer
1595   PARAMS ((cp_parser *));
1596
1597 /* Derived classes [gram.class.derived] */
1598
1599 static tree cp_parser_base_clause
1600   PARAMS ((cp_parser *));
1601 static tree cp_parser_base_specifier
1602   PARAMS ((cp_parser *));
1603
1604 /* Special member functions [gram.special] */
1605
1606 static tree cp_parser_conversion_function_id
1607   PARAMS ((cp_parser *));
1608 static tree cp_parser_conversion_type_id
1609   PARAMS ((cp_parser *));
1610 static tree cp_parser_conversion_declarator_opt
1611   PARAMS ((cp_parser *));
1612 static bool cp_parser_ctor_initializer_opt
1613   PARAMS ((cp_parser *));
1614 static void cp_parser_mem_initializer_list
1615   PARAMS ((cp_parser *));
1616 static tree cp_parser_mem_initializer
1617   PARAMS ((cp_parser *));
1618 static tree cp_parser_mem_initializer_id
1619   PARAMS ((cp_parser *));
1620
1621 /* Overloading [gram.over] */
1622
1623 static tree cp_parser_operator_function_id
1624   PARAMS ((cp_parser *));
1625 static tree cp_parser_operator
1626   PARAMS ((cp_parser *));
1627
1628 /* Templates [gram.temp] */
1629
1630 static void cp_parser_template_declaration
1631   PARAMS ((cp_parser *, bool));
1632 static tree cp_parser_template_parameter_list
1633   PARAMS ((cp_parser *));
1634 static tree cp_parser_template_parameter
1635   PARAMS ((cp_parser *));
1636 static tree cp_parser_type_parameter
1637   PARAMS ((cp_parser *));
1638 static tree cp_parser_template_id
1639   PARAMS ((cp_parser *, bool, bool));
1640 static tree cp_parser_template_name
1641   PARAMS ((cp_parser *, bool, bool));
1642 static tree cp_parser_template_argument_list
1643   PARAMS ((cp_parser *));
1644 static tree cp_parser_template_argument
1645   PARAMS ((cp_parser *));
1646 static void cp_parser_explicit_instantiation
1647   PARAMS ((cp_parser *));
1648 static void cp_parser_explicit_specialization
1649   PARAMS ((cp_parser *));
1650
1651 /* Exception handling [gram.exception] */
1652
1653 static tree cp_parser_try_block 
1654   PARAMS ((cp_parser *));
1655 static bool cp_parser_function_try_block
1656   PARAMS ((cp_parser *));
1657 static void cp_parser_handler_seq
1658   PARAMS ((cp_parser *));
1659 static void cp_parser_handler
1660   PARAMS ((cp_parser *));
1661 static tree cp_parser_exception_declaration
1662   PARAMS ((cp_parser *));
1663 static tree cp_parser_throw_expression
1664   PARAMS ((cp_parser *));
1665 static tree cp_parser_exception_specification_opt
1666   PARAMS ((cp_parser *));
1667 static tree cp_parser_type_id_list
1668   PARAMS ((cp_parser *));
1669
1670 /* GNU Extensions */
1671
1672 static tree cp_parser_asm_specification_opt
1673   PARAMS ((cp_parser *));
1674 static tree cp_parser_asm_operand_list
1675   PARAMS ((cp_parser *));
1676 static tree cp_parser_asm_clobber_list
1677   PARAMS ((cp_parser *));
1678 static tree cp_parser_attributes_opt
1679   PARAMS ((cp_parser *));
1680 static tree cp_parser_attribute_list
1681   PARAMS ((cp_parser *));
1682 static bool cp_parser_extension_opt
1683   PARAMS ((cp_parser *, int *));
1684 static void cp_parser_label_declaration
1685   PARAMS ((cp_parser *));
1686
1687 /* Utility Routines */
1688
1689 static tree cp_parser_lookup_name
1690   PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1691 static tree cp_parser_lookup_name_simple
1692   PARAMS ((cp_parser *, tree));
1693 static tree cp_parser_resolve_typename_type
1694   PARAMS ((cp_parser *, tree));
1695 static tree cp_parser_maybe_treat_template_as_class
1696   (tree, bool);
1697 static bool cp_parser_check_declarator_template_parameters
1698   PARAMS ((cp_parser *, tree));
1699 static bool cp_parser_check_template_parameters
1700   PARAMS ((cp_parser *, unsigned));
1701 static tree cp_parser_binary_expression
1702   PARAMS ((cp_parser *, 
1703            cp_parser_token_tree_map,
1704            cp_parser_expression_fn));
1705 static tree cp_parser_global_scope_opt
1706   PARAMS ((cp_parser *, bool));
1707 static bool cp_parser_constructor_declarator_p
1708   (cp_parser *, bool);
1709 static tree cp_parser_function_definition_from_specifiers_and_declarator
1710   PARAMS ((cp_parser *, tree, tree, tree, tree));
1711 static tree cp_parser_function_definition_after_declarator
1712   PARAMS ((cp_parser *, bool));
1713 static void cp_parser_template_declaration_after_export
1714   PARAMS ((cp_parser *, bool));
1715 static tree cp_parser_single_declaration
1716   PARAMS ((cp_parser *, bool, bool *));
1717 static tree cp_parser_functional_cast
1718   PARAMS ((cp_parser *, tree));
1719 static void cp_parser_late_parsing_for_member
1720   PARAMS ((cp_parser *, tree));
1721 static void cp_parser_late_parsing_default_args
1722   (cp_parser *, tree, tree);
1723 static tree cp_parser_sizeof_operand
1724   PARAMS ((cp_parser *, enum rid));
1725 static bool cp_parser_declares_only_class_p
1726   PARAMS ((cp_parser *));
1727 static bool cp_parser_friend_p
1728   PARAMS ((tree));
1729 static cp_token *cp_parser_require
1730   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1731 static cp_token *cp_parser_require_keyword
1732   PARAMS ((cp_parser *, enum rid, const char *));
1733 static bool cp_parser_token_starts_function_definition_p 
1734   PARAMS ((cp_token *));
1735 static bool cp_parser_next_token_starts_class_definition_p
1736   (cp_parser *);
1737 static enum tag_types cp_parser_token_is_class_key
1738   PARAMS ((cp_token *));
1739 static void cp_parser_check_class_key
1740   (enum tag_types, tree type);
1741 static bool cp_parser_optional_template_keyword
1742   (cp_parser *);
1743 static void cp_parser_cache_group
1744   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1745 static void cp_parser_parse_tentatively 
1746   PARAMS ((cp_parser *));
1747 static void cp_parser_commit_to_tentative_parse
1748   PARAMS ((cp_parser *));
1749 static void cp_parser_abort_tentative_parse
1750   PARAMS ((cp_parser *));
1751 static bool cp_parser_parse_definitely
1752   PARAMS ((cp_parser *));
1753 static inline bool cp_parser_parsing_tentatively
1754   PARAMS ((cp_parser *));
1755 static bool cp_parser_committed_to_tentative_parse
1756   PARAMS ((cp_parser *));
1757 static void cp_parser_error
1758   PARAMS ((cp_parser *, const char *));
1759 static bool cp_parser_simulate_error
1760   PARAMS ((cp_parser *));
1761 static void cp_parser_check_type_definition
1762   PARAMS ((cp_parser *));
1763 static bool cp_parser_skip_to_closing_parenthesis
1764   PARAMS ((cp_parser *));
1765 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1766   (cp_parser *);
1767 static void cp_parser_skip_to_end_of_statement
1768   PARAMS ((cp_parser *));
1769 static void cp_parser_skip_to_end_of_block_or_statement
1770   PARAMS ((cp_parser *));
1771 static void cp_parser_skip_to_closing_brace
1772   (cp_parser *);
1773 static void cp_parser_skip_until_found
1774   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1775 static bool cp_parser_error_occurred
1776   PARAMS ((cp_parser *));
1777 static bool cp_parser_allow_gnu_extensions_p
1778   PARAMS ((cp_parser *));
1779 static bool cp_parser_is_string_literal
1780   PARAMS ((cp_token *));
1781 static bool cp_parser_is_keyword 
1782   PARAMS ((cp_token *, enum rid));
1783 static bool cp_parser_dependent_type_p
1784   (tree);
1785 static bool cp_parser_value_dependent_expression_p
1786   (tree);
1787 static bool cp_parser_type_dependent_expression_p
1788   (tree);
1789 static bool cp_parser_dependent_template_arg_p
1790   (tree);
1791 static bool cp_parser_dependent_template_id_p
1792   (tree, tree);
1793 static bool cp_parser_dependent_template_p
1794   (tree);
1795 static void cp_parser_defer_access_check
1796   (cp_parser *, tree, tree);
1797 static void cp_parser_start_deferring_access_checks
1798   (cp_parser *);
1799 static tree cp_parser_stop_deferring_access_checks
1800   PARAMS ((cp_parser *));
1801 static void cp_parser_perform_deferred_access_checks
1802   PARAMS ((tree));
1803 static tree cp_parser_scope_through_which_access_occurs
1804   (tree, tree, tree);
1805
1806 /* Returns non-zero if we are parsing tentatively.  */
1807
1808 static inline bool
1809 cp_parser_parsing_tentatively (parser)
1810      cp_parser *parser;
1811 {
1812   return parser->context->next != NULL;
1813 }
1814
1815 /* Returns non-zero if TOKEN is a string literal.  */
1816
1817 static bool
1818 cp_parser_is_string_literal (token)
1819      cp_token *token;
1820 {
1821   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1822 }
1823
1824 /* Returns non-zero if TOKEN is the indicated KEYWORD.  */
1825
1826 static bool
1827 cp_parser_is_keyword (token, keyword)
1828      cp_token *token;
1829      enum rid keyword;
1830 {
1831   return token->keyword == keyword;
1832 }
1833
1834 /* Returns TRUE if TYPE is dependent, in the sense of
1835    [temp.dep.type].  */
1836
1837 static bool
1838 cp_parser_dependent_type_p (type)
1839      tree type;
1840 {
1841   tree scope;
1842
1843   if (!processing_template_decl)
1844     return false;
1845
1846   /* If the type is NULL, we have not computed a type for the entity
1847      in question; in that case, the type is dependent.  */
1848   if (!type)
1849     return true;
1850
1851   /* Erroneous types can be considered non-dependent.  */
1852   if (type == error_mark_node)
1853     return false;
1854
1855   /* [temp.dep.type]
1856
1857      A type is dependent if it is:
1858
1859      -- a template parameter.  */
1860   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1861     return true;
1862   /* -- a qualified-id with a nested-name-specifier which contains a
1863         class-name that names a dependent type or whose unqualified-id
1864         names a dependent type.  */
1865   if (TREE_CODE (type) == TYPENAME_TYPE)
1866     return true;
1867   /* -- a cv-qualified type where the cv-unqualified type is
1868         dependent.  */
1869   type = TYPE_MAIN_VARIANT (type);
1870   /* -- a compound type constructed from any dependent type.  */
1871   if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1872     return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1873             || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE 
1874                                            (type)));
1875   else if (TREE_CODE (type) == POINTER_TYPE
1876            || TREE_CODE (type) == REFERENCE_TYPE)
1877     return cp_parser_dependent_type_p (TREE_TYPE (type));
1878   else if (TREE_CODE (type) == FUNCTION_TYPE
1879            || TREE_CODE (type) == METHOD_TYPE)
1880     {
1881       tree arg_type;
1882
1883       if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1884         return true;
1885       for (arg_type = TYPE_ARG_TYPES (type); 
1886            arg_type; 
1887            arg_type = TREE_CHAIN (arg_type))
1888         if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1889           return true;
1890       return false;
1891     }
1892   /* -- an array type constructed from any dependent type or whose
1893         size is specified by a constant expression that is
1894         value-dependent.  */
1895   if (TREE_CODE (type) == ARRAY_TYPE)
1896     {
1897       if (TYPE_DOMAIN (type)
1898           && ((cp_parser_value_dependent_expression_p 
1899                (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1900               || (cp_parser_type_dependent_expression_p
1901                   (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1902         return true;
1903       return cp_parser_dependent_type_p (TREE_TYPE (type));
1904     }
1905   /* -- a template-id in which either the template name is a template
1906         parameter or any of the template arguments is a dependent type or
1907         an expression that is type-dependent or value-dependent.  
1908
1909      This language seems somewhat confused; for example, it does not
1910      discuss template template arguments.  Therefore, we use the
1911      definition for dependent template arguments in [temp.dep.temp].  */
1912   if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1913       && (cp_parser_dependent_template_id_p
1914           (CLASSTYPE_TI_TEMPLATE (type),
1915            CLASSTYPE_TI_ARGS (type))))
1916     return true;
1917   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1918     return true;
1919   /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1920      expression is not type-dependent, then it should already been
1921      have resolved.  */
1922   if (TREE_CODE (type) == TYPEOF_TYPE)
1923     return true;
1924   /* The standard does not specifically mention types that are local
1925      to template functions or local classes, but they should be
1926      considered dependent too.  For example:
1927
1928        template <int I> void f() { 
1929          enum E { a = I }; 
1930          S<sizeof (E)> s;
1931        }
1932
1933      The size of `E' cannot be known until the value of `I' has been
1934      determined.  Therefore, `E' must be considered dependent.  */
1935   scope = TYPE_CONTEXT (type);
1936   if (scope && TYPE_P (scope))
1937     return cp_parser_dependent_type_p (scope);
1938   else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1939     return cp_parser_type_dependent_expression_p (scope);
1940
1941   /* Other types are non-dependent.  */
1942   return false;
1943 }
1944
1945 /* Returns TRUE if the EXPRESSION is value-dependent.  */
1946
1947 static bool
1948 cp_parser_value_dependent_expression_p (tree expression)
1949 {
1950   if (!processing_template_decl)
1951     return false;
1952
1953   /* A name declared with a dependent type.  */
1954   if (DECL_P (expression)
1955       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1956     return true;
1957   /* A non-type template parameter.  */
1958   if ((TREE_CODE (expression) == CONST_DECL
1959        && DECL_TEMPLATE_PARM_P (expression))
1960       || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1961     return true;
1962   /* A constant with integral or enumeration type and is initialized 
1963      with an expression that is value-dependent.  */
1964   if (TREE_CODE (expression) == VAR_DECL
1965       && DECL_INITIAL (expression)
1966       && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1967           || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1968       && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1969     return true;
1970   /* These expressions are value-dependent if the type to which the
1971      cast occurs is dependent.  */
1972   if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1973        || TREE_CODE (expression) == STATIC_CAST_EXPR
1974        || TREE_CODE (expression) == CONST_CAST_EXPR
1975        || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1976        || TREE_CODE (expression) == CAST_EXPR)
1977       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1978     return true;
1979   /* A `sizeof' expression where the sizeof operand is a type is
1980      value-dependent if the type is dependent.  If the type was not
1981      dependent, we would no longer have a SIZEOF_EXPR, so any
1982      SIZEOF_EXPR is dependent.  */
1983   if (TREE_CODE (expression) == SIZEOF_EXPR)
1984     return true;
1985   /* A constant expression is value-dependent if any subexpression is
1986      value-dependent.  */
1987   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1988     {
1989       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1990         {
1991         case '1':
1992           return (cp_parser_value_dependent_expression_p 
1993                   (TREE_OPERAND (expression, 0)));
1994         case '<':
1995         case '2':
1996           return ((cp_parser_value_dependent_expression_p 
1997                    (TREE_OPERAND (expression, 0)))
1998                   || (cp_parser_value_dependent_expression_p 
1999                       (TREE_OPERAND (expression, 1))));
2000         case 'e':
2001           {
2002             int i;
2003             for (i = 0; 
2004                  i < TREE_CODE_LENGTH (TREE_CODE (expression));
2005                  ++i)
2006               if (cp_parser_value_dependent_expression_p
2007                   (TREE_OPERAND (expression, i)))
2008                 return true;
2009             return false;
2010           }
2011         }
2012     }
2013
2014   /* The expression is not value-dependent.  */
2015   return false;
2016 }
2017
2018 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2019    [temp.dep.expr].  */
2020
2021 static bool
2022 cp_parser_type_dependent_expression_p (expression)
2023      tree expression;
2024 {
2025   if (!processing_template_decl)
2026     return false;
2027
2028   /* Some expression forms are never type-dependent.  */
2029   if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2030       || TREE_CODE (expression) == SIZEOF_EXPR
2031       || TREE_CODE (expression) == ALIGNOF_EXPR
2032       || TREE_CODE (expression) == TYPEID_EXPR
2033       || TREE_CODE (expression) == DELETE_EXPR
2034       || TREE_CODE (expression) == VEC_DELETE_EXPR
2035       || TREE_CODE (expression) == THROW_EXPR)
2036     return false;
2037
2038   /* The types of these expressions depends only on the type to which
2039      the cast occurs.  */
2040   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2041       || TREE_CODE (expression) == STATIC_CAST_EXPR
2042       || TREE_CODE (expression) == CONST_CAST_EXPR
2043       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2044       || TREE_CODE (expression) == CAST_EXPR)
2045     return cp_parser_dependent_type_p (TREE_TYPE (expression));
2046   /* The types of these expressions depends only on the type created
2047      by the expression.  */
2048   else if (TREE_CODE (expression) == NEW_EXPR
2049            || TREE_CODE (expression) == VEC_NEW_EXPR)
2050     return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2051
2052   if (TREE_CODE (expression) == FUNCTION_DECL
2053       && DECL_LANG_SPECIFIC (expression)
2054       && DECL_TEMPLATE_INFO (expression)
2055       && (cp_parser_dependent_template_id_p
2056           (DECL_TI_TEMPLATE (expression),
2057            INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2058     return true;
2059
2060   return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2061 }
2062
2063 /* Returns TRUE if the ARG (a template argument) is dependent.  */
2064
2065 static bool
2066 cp_parser_dependent_template_arg_p (tree arg)
2067 {
2068   if (!processing_template_decl)
2069     return false;
2070
2071   if (TREE_CODE (arg) == TEMPLATE_DECL
2072       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2073     return cp_parser_dependent_template_p (arg);
2074   else if (TYPE_P (arg))
2075     return cp_parser_dependent_type_p (arg);
2076   else
2077     return (cp_parser_type_dependent_expression_p (arg)
2078             || cp_parser_value_dependent_expression_p (arg));
2079 }
2080
2081 /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
2082
2083 static bool
2084 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2085 {
2086   int i;
2087
2088   if (cp_parser_dependent_template_p (tmpl))
2089     return true;
2090   for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2091     if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2092       return true;
2093   return false;
2094 }
2095
2096 /* Returns TRUE if the template TMPL is dependent.  */
2097
2098 static bool
2099 cp_parser_dependent_template_p (tree tmpl)
2100 {
2101   /* Template template parameters are dependent.  */
2102   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2103       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2104     return true;
2105   /* So are member templates of dependent classes.  */
2106   if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2107     return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2108   return false;
2109 }
2110
2111 /* Defer checking the accessibility of DECL, when looked up in
2112    CLASS_TYPE.  */
2113
2114 static void
2115 cp_parser_defer_access_check (cp_parser *parser, 
2116                               tree class_type,
2117                               tree decl)
2118 {
2119   tree check;
2120
2121   /* If we are not supposed to defer access checks, just check now.  */
2122   if (!parser->context->deferring_access_checks_p)
2123     {
2124       enforce_access (class_type, decl);
2125       return;
2126     }
2127
2128   /* See if we are already going to perform this check.  */
2129   for (check = parser->context->deferred_access_checks;
2130        check;
2131        check = TREE_CHAIN (check))
2132     if (TREE_VALUE (check) == decl
2133         && same_type_p (TREE_PURPOSE (check), class_type))
2134       return;
2135   /* If not, record the check.  */
2136   parser->context->deferred_access_checks
2137     = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2138 }
2139
2140 /* Start deferring access control checks.  */
2141
2142 static void
2143 cp_parser_start_deferring_access_checks (cp_parser *parser)
2144 {
2145   parser->context->deferring_access_checks_p = true;
2146 }
2147
2148 /* Stop deferring access control checks.  Returns a TREE_LIST
2149    representing the deferred checks.  The TREE_PURPOSE of each node is
2150    the type through which the access occurred; the TREE_VALUE is the
2151    declaration named.  */
2152
2153 static tree
2154 cp_parser_stop_deferring_access_checks (parser)
2155      cp_parser *parser;
2156 {
2157   tree access_checks;
2158
2159   parser->context->deferring_access_checks_p = false;
2160   access_checks = parser->context->deferred_access_checks;
2161   parser->context->deferred_access_checks = NULL_TREE;
2162
2163   return access_checks;
2164 }
2165
2166 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2167    documented with cp_parser_stop_deferrring_access_checks.  */
2168
2169 static void
2170 cp_parser_perform_deferred_access_checks (access_checks)
2171      tree access_checks;
2172 {
2173   tree deferred_check;
2174
2175   /* Look through all the deferred checks.  */
2176   for (deferred_check = access_checks;
2177        deferred_check;
2178        deferred_check = TREE_CHAIN (deferred_check))
2179     /* Check access.  */
2180     enforce_access (TREE_PURPOSE (deferred_check), 
2181                     TREE_VALUE (deferred_check));
2182 }
2183
2184 /* Returns the scope through which DECL is being accessed, or
2185    NULL_TREE if DECL is not a member.  If OBJECT_TYPE is non-NULL, we
2186    have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2187    or `x', respectively.  If the DECL was named as `A::B' then
2188    NESTED_NAME_SPECIFIER is `A'.  */
2189
2190 tree
2191 cp_parser_scope_through_which_access_occurs (decl, 
2192                                              object_type,
2193                                              nested_name_specifier)
2194      tree decl;
2195      tree object_type;
2196      tree nested_name_specifier;
2197 {
2198   tree scope;
2199   tree qualifying_type = NULL_TREE;
2200   
2201   /* Determine the SCOPE of DECL.  */
2202   scope = context_for_name_lookup (decl);
2203   /* If the SCOPE is not a type, then DECL is not a member.  */
2204   if (!TYPE_P (scope))
2205     return NULL_TREE;
2206   /* Figure out the type through which DECL is being accessed.  */
2207   if (object_type && DERIVED_FROM_P (scope, object_type))
2208     /* If we are processing a `->' or `.' expression, use the type of the
2209        left-hand side.  */
2210     qualifying_type = object_type;
2211   else if (nested_name_specifier)
2212     {
2213       /* If the reference is to a non-static member of the
2214          current class, treat it as if it were referenced through
2215          `this'.  */
2216       if (DECL_NONSTATIC_MEMBER_P (decl)
2217           && current_class_ptr
2218           && DERIVED_FROM_P (scope, current_class_type))
2219         qualifying_type = current_class_type;
2220       /* Otherwise, use the type indicated by the
2221          nested-name-specifier.  */
2222       else
2223         qualifying_type = nested_name_specifier;
2224     }
2225   else
2226     /* Otherwise, the name must be from the current class or one of
2227        its bases.  */
2228     qualifying_type = currently_open_derived_class (scope);
2229
2230   return qualifying_type;
2231 }
2232
2233 /* Issue the indicated error MESSAGE.  */
2234
2235 static void
2236 cp_parser_error (parser, message)
2237      cp_parser *parser;
2238      const char *message;
2239 {
2240   /* Output the MESSAGE -- unless we're parsing tentatively.  */
2241   if (!cp_parser_simulate_error (parser))
2242     error (message);
2243 }
2244
2245 /* If we are parsing tentatively, remember that an error has occurred
2246    during this tentative parse.  Returns true if the error was
2247    simulated; false if a messgae should be issued by the caller.  */
2248
2249 static bool
2250 cp_parser_simulate_error (parser)
2251      cp_parser *parser;
2252 {
2253   if (cp_parser_parsing_tentatively (parser)
2254       && !cp_parser_committed_to_tentative_parse (parser))
2255     {
2256       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2257       return true;
2258     }
2259   return false;
2260 }
2261
2262 /* This function is called when a type is defined.  If type
2263    definitions are forbidden at this point, an error message is
2264    issued.  */
2265
2266 static void
2267 cp_parser_check_type_definition (parser)
2268      cp_parser *parser;
2269 {
2270   /* If types are forbidden here, issue a message.  */
2271   if (parser->type_definition_forbidden_message)
2272     /* Use `%s' to print the string in case there are any escape
2273        characters in the message.  */
2274     error ("%s", parser->type_definition_forbidden_message);
2275 }
2276
2277 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2278    Returns TRUE iff we found a closing `)'.  */
2279
2280 static bool
2281 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2282 {
2283   unsigned nesting_depth = 0;
2284
2285   while (true)
2286     {
2287       cp_token *token;
2288
2289       /* If we've run out of tokens, then there is no closing `)'.  */
2290       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2291         return false;
2292       /* Consume the token.  */
2293       token = cp_lexer_consume_token (parser->lexer);
2294       /* If it is an `(', we have entered another level of nesting.  */
2295       if (token->type == CPP_OPEN_PAREN)
2296         ++nesting_depth;
2297       /* If it is a `)', then we might be done.  */
2298       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2299         return true;
2300     }
2301 }
2302
2303 /* Consume tokens until the next token is a `)', or a `,'.  Returns
2304    TRUE if the next token is a `,'.  */
2305
2306 static bool
2307 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2308 {
2309   unsigned nesting_depth = 0;
2310
2311   while (true)
2312     {
2313       cp_token *token = cp_lexer_peek_token (parser->lexer);
2314
2315       /* If we've run out of tokens, then there is no closing `)'.  */
2316       if (token->type == CPP_EOF)
2317         return false;
2318       /* If it is a `,' stop.  */
2319       else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2320         return true;
2321       /* If it is a `)', stop.  */
2322       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2323         return false;
2324       /* If it is an `(', we have entered another level of nesting.  */
2325       else if (token->type == CPP_OPEN_PAREN)
2326         ++nesting_depth;
2327       /* Consume the token.  */
2328       token = cp_lexer_consume_token (parser->lexer);
2329     }
2330 }
2331
2332 /* Consume tokens until we reach the end of the current statement.
2333    Normally, that will be just before consuming a `;'.  However, if a
2334    non-nested `}' comes first, then we stop before consuming that.  */
2335
2336 static void
2337 cp_parser_skip_to_end_of_statement (parser)
2338      cp_parser *parser;
2339 {
2340   unsigned nesting_depth = 0;
2341
2342   while (true)
2343     {
2344       cp_token *token;
2345
2346       /* Peek at the next token.  */
2347       token = cp_lexer_peek_token (parser->lexer);
2348       /* If we've run out of tokens, stop.  */
2349       if (token->type == CPP_EOF)
2350         break;
2351       /* If the next token is a `;', we have reached the end of the
2352          statement.  */
2353       if (token->type == CPP_SEMICOLON && !nesting_depth)
2354         break;
2355       /* If the next token is a non-nested `}', then we have reached
2356          the end of the current block.  */
2357       if (token->type == CPP_CLOSE_BRACE)
2358         {
2359           /* If this is a non-nested `}', stop before consuming it.
2360              That way, when confronted with something like:
2361
2362                { 3 + } 
2363
2364              we stop before consuming the closing `}', even though we
2365              have not yet reached a `;'.  */
2366           if (nesting_depth == 0)
2367             break;
2368           /* If it is the closing `}' for a block that we have
2369              scanned, stop -- but only after consuming the token.
2370              That way given:
2371
2372                 void f g () { ... }
2373                 typedef int I;
2374
2375              we will stop after the body of the erroneously declared
2376              function, but before consuming the following `typedef'
2377              declaration.  */
2378           if (--nesting_depth == 0)
2379             {
2380               cp_lexer_consume_token (parser->lexer);
2381               break;
2382             }
2383         }
2384       /* If it the next token is a `{', then we are entering a new
2385          block.  Consume the entire block.  */
2386       else if (token->type == CPP_OPEN_BRACE)
2387         ++nesting_depth;
2388       /* Consume the token.  */
2389       cp_lexer_consume_token (parser->lexer);
2390     }
2391 }
2392
2393 /* Skip tokens until we have consumed an entire block, or until we
2394    have consumed a non-nested `;'.  */
2395
2396 static void
2397 cp_parser_skip_to_end_of_block_or_statement (parser)
2398      cp_parser *parser;
2399 {
2400   unsigned nesting_depth = 0;
2401
2402   while (true)
2403     {
2404       cp_token *token;
2405
2406       /* Peek at the next token.  */
2407       token = cp_lexer_peek_token (parser->lexer);
2408       /* If we've run out of tokens, stop.  */
2409       if (token->type == CPP_EOF)
2410         break;
2411       /* If the next token is a `;', we have reached the end of the
2412          statement.  */
2413       if (token->type == CPP_SEMICOLON && !nesting_depth)
2414         {
2415           /* Consume the `;'.  */
2416           cp_lexer_consume_token (parser->lexer);
2417           break;
2418         }
2419       /* Consume the token.  */
2420       token = cp_lexer_consume_token (parser->lexer);
2421       /* If the next token is a non-nested `}', then we have reached
2422          the end of the current block.  */
2423       if (token->type == CPP_CLOSE_BRACE 
2424           && (nesting_depth == 0 || --nesting_depth == 0))
2425         break;
2426       /* If it the next token is a `{', then we are entering a new
2427          block.  Consume the entire block.  */
2428       if (token->type == CPP_OPEN_BRACE)
2429         ++nesting_depth;
2430     }
2431 }
2432
2433 /* Skip tokens until a non-nested closing curly brace is the next
2434    token.  */
2435
2436 static void
2437 cp_parser_skip_to_closing_brace (cp_parser *parser)
2438 {
2439   unsigned nesting_depth = 0;
2440
2441   while (true)
2442     {
2443       cp_token *token;
2444
2445       /* Peek at the next token.  */
2446       token = cp_lexer_peek_token (parser->lexer);
2447       /* If we've run out of tokens, stop.  */
2448       if (token->type == CPP_EOF)
2449         break;
2450       /* If the next token is a non-nested `}', then we have reached
2451          the end of the current block.  */
2452       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2453         break;
2454       /* If it the next token is a `{', then we are entering a new
2455          block.  Consume the entire block.  */
2456       else if (token->type == CPP_OPEN_BRACE)
2457         ++nesting_depth;
2458       /* Consume the token.  */
2459       cp_lexer_consume_token (parser->lexer);
2460     }
2461 }
2462
2463 /* Create a new C++ parser.  */
2464
2465 static cp_parser *
2466 cp_parser_new ()
2467 {
2468   cp_parser *parser;
2469
2470   parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2471   parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2472   parser->context = cp_parser_context_new (NULL);
2473
2474   /* For now, we always accept GNU extensions.  */
2475   parser->allow_gnu_extensions_p = 1;
2476
2477   /* The `>' token is a greater-than operator, not the end of a
2478      template-id.  */
2479   parser->greater_than_is_operator_p = true;
2480
2481   parser->default_arg_ok_p = true;
2482   
2483   /* We are not parsing a constant-expression.  */
2484   parser->constant_expression_p = false;
2485
2486   /* Local variable names are not forbidden.  */
2487   parser->local_variables_forbidden_p = false;
2488
2489   /* We are not procesing an `extern "C"' declaration.  */
2490   parser->in_unbraced_linkage_specification_p = false;
2491
2492   /* We are not processing a declarator.  */
2493   parser->in_declarator_p = false;
2494
2495   /* There are no default args to process.  */
2496   parser->default_arg_types = NULL;
2497
2498   /* The unparsed function queue is empty.  */
2499   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2500
2501   /* There are no classes being defined.  */
2502   parser->num_classes_being_defined = 0;
2503
2504   /* No template parameters apply.  */
2505   parser->num_template_parameter_lists = 0;
2506
2507   return parser;
2508 }
2509
2510 /* Lexical conventions [gram.lex]  */
2511
2512 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2513    identifier.  */
2514
2515 static tree 
2516 cp_parser_identifier (parser)
2517      cp_parser *parser;
2518 {
2519   cp_token *token;
2520
2521   /* Look for the identifier.  */
2522   token = cp_parser_require (parser, CPP_NAME, "identifier");
2523   /* Return the value.  */
2524   return token ? token->value : error_mark_node;
2525 }
2526
2527 /* Basic concepts [gram.basic]  */
2528
2529 /* Parse a translation-unit.
2530
2531    translation-unit:
2532      declaration-seq [opt]  
2533
2534    Returns TRUE if all went well.  */
2535
2536 static bool
2537 cp_parser_translation_unit (parser)
2538      cp_parser *parser;
2539 {
2540   while (true)
2541     {
2542       cp_parser_declaration_seq_opt (parser);
2543
2544       /* If there are no tokens left then all went well.  */
2545       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2546         break;
2547       
2548       /* Otherwise, issue an error message.  */
2549       cp_parser_error (parser, "expected declaration");
2550       return false;
2551     }
2552
2553   /* Consume the EOF token.  */
2554   cp_parser_require (parser, CPP_EOF, "end-of-file");
2555   
2556   /* Finish up.  */
2557   finish_translation_unit ();
2558
2559   /* All went well.  */
2560   return true;
2561 }
2562
2563 /* Expressions [gram.expr] */
2564
2565 /* Parse a primary-expression.
2566
2567    primary-expression:
2568      literal
2569      this
2570      ( expression )
2571      id-expression
2572
2573    GNU Extensions:
2574
2575    primary-expression:
2576      ( compound-statement )
2577      __builtin_va_arg ( assignment-expression , type-id )
2578
2579    literal:
2580      __null
2581
2582    Returns a representation of the expression.  
2583
2584    *IDK indicates what kind of id-expression (if any) was present.  
2585
2586    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2587    used as the operand of a pointer-to-member.  In that case,
2588    *QUALIFYING_CLASS gives the class that is used as the qualifying
2589    class in the pointer-to-member.  */
2590
2591 static tree
2592 cp_parser_primary_expression (cp_parser *parser, 
2593                               cp_parser_id_kind *idk,
2594                               tree *qualifying_class)
2595 {
2596   cp_token *token;
2597
2598   /* Assume the primary expression is not an id-expression.  */
2599   *idk = CP_PARSER_ID_KIND_NONE;
2600   /* And that it cannot be used as pointer-to-member.  */
2601   *qualifying_class = NULL_TREE;
2602
2603   /* Peek at the next token.  */
2604   token = cp_lexer_peek_token (parser->lexer);
2605   switch (token->type)
2606     {
2607       /* literal:
2608            integer-literal
2609            character-literal
2610            floating-literal
2611            string-literal
2612            boolean-literal  */
2613     case CPP_CHAR:
2614     case CPP_WCHAR:
2615     case CPP_STRING:
2616     case CPP_WSTRING:
2617     case CPP_NUMBER:
2618       token = cp_lexer_consume_token (parser->lexer);
2619       return token->value;
2620
2621     case CPP_OPEN_PAREN:
2622       {
2623         tree expr;
2624         bool saved_greater_than_is_operator_p;
2625
2626         /* Consume the `('.  */
2627         cp_lexer_consume_token (parser->lexer);
2628         /* Within a parenthesized expression, a `>' token is always
2629            the greater-than operator.  */
2630         saved_greater_than_is_operator_p 
2631           = parser->greater_than_is_operator_p;
2632         parser->greater_than_is_operator_p = true;
2633         /* If we see `( { ' then we are looking at the beginning of
2634            a GNU statement-expression.  */
2635         if (cp_parser_allow_gnu_extensions_p (parser)
2636             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2637           {
2638             /* Statement-expressions are not allowed by the standard.  */
2639             if (pedantic)
2640               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2641             
2642             /* And they're not allowed outside of a function-body; you
2643                cannot, for example, write:
2644                
2645                  int i = ({ int j = 3; j + 1; });
2646                
2647                at class or namespace scope.  */
2648             if (!at_function_scope_p ())
2649               error ("statement-expressions are allowed only inside functions");
2650             /* Start the statement-expression.  */
2651             expr = begin_stmt_expr ();
2652             /* Parse the compound-statement.  */
2653             cp_parser_compound_statement (parser);
2654             /* Finish up.  */
2655             expr = finish_stmt_expr (expr);
2656           }
2657         else
2658           {
2659             /* Parse the parenthesized expression.  */
2660             expr = cp_parser_expression (parser);
2661             /* Let the front end know that this expression was
2662                enclosed in parentheses. This matters in case, for
2663                example, the expression is of the form `A::B', since
2664                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2665                not.  */
2666             finish_parenthesized_expr (expr);
2667           }
2668         /* The `>' token might be the end of a template-id or
2669            template-parameter-list now.  */
2670         parser->greater_than_is_operator_p 
2671           = saved_greater_than_is_operator_p;
2672         /* Consume the `)'.  */
2673         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2674           cp_parser_skip_to_end_of_statement (parser);
2675
2676         return expr;
2677       }
2678
2679     case CPP_KEYWORD:
2680       switch (token->keyword)
2681         {
2682           /* These two are the boolean literals.  */
2683         case RID_TRUE:
2684           cp_lexer_consume_token (parser->lexer);
2685           return boolean_true_node;
2686         case RID_FALSE:
2687           cp_lexer_consume_token (parser->lexer);
2688           return boolean_false_node;
2689           
2690           /* The `__null' literal.  */
2691         case RID_NULL:
2692           cp_lexer_consume_token (parser->lexer);
2693           return null_node;
2694
2695           /* Recognize the `this' keyword.  */
2696         case RID_THIS:
2697           cp_lexer_consume_token (parser->lexer);
2698           if (parser->local_variables_forbidden_p)
2699             {
2700               error ("`this' may not be used in this context");
2701               return error_mark_node;
2702             }
2703           return finish_this_expr ();
2704
2705           /* The `operator' keyword can be the beginning of an
2706              id-expression.  */
2707         case RID_OPERATOR:
2708           goto id_expression;
2709
2710         case RID_FUNCTION_NAME:
2711         case RID_PRETTY_FUNCTION_NAME:
2712         case RID_C99_FUNCTION_NAME:
2713           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2714              __func__ are the names of variables -- but they are
2715              treated specially.  Therefore, they are handled here,
2716              rather than relying on the generic id-expression logic
2717              below.  Gramatically, these names are id-expressions.  
2718
2719              Consume the token.  */
2720           token = cp_lexer_consume_token (parser->lexer);
2721           /* Look up the name.  */
2722           return finish_fname (token->value);
2723
2724         case RID_VA_ARG:
2725           {
2726             tree expression;
2727             tree type;
2728
2729             /* The `__builtin_va_arg' construct is used to handle
2730                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2731             cp_lexer_consume_token (parser->lexer);
2732             /* Look for the opening `('.  */
2733             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2734             /* Now, parse the assignment-expression.  */
2735             expression = cp_parser_assignment_expression (parser);
2736             /* Look for the `,'.  */
2737             cp_parser_require (parser, CPP_COMMA, "`,'");
2738             /* Parse the type-id.  */
2739             type = cp_parser_type_id (parser);
2740             /* Look for the closing `)'.  */
2741             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2742
2743             return build_x_va_arg (expression, type);
2744           }
2745
2746         default:
2747           cp_parser_error (parser, "expected primary-expression");
2748           return error_mark_node;
2749         }
2750       /* Fall through. */
2751
2752       /* An id-expression can start with either an identifier, a
2753          `::' as the beginning of a qualified-id, or the "operator"
2754          keyword.  */
2755     case CPP_NAME:
2756     case CPP_SCOPE:
2757     case CPP_TEMPLATE_ID:
2758     case CPP_NESTED_NAME_SPECIFIER:
2759       {
2760         tree id_expression;
2761         tree decl;
2762
2763       id_expression:
2764         /* Parse the id-expression.  */
2765         id_expression 
2766           = cp_parser_id_expression (parser, 
2767                                      /*template_keyword_p=*/false,
2768                                      /*check_dependency_p=*/true,
2769                                      /*template_p=*/NULL);
2770         if (id_expression == error_mark_node)
2771           return error_mark_node;
2772         /* If we have a template-id, then no further lookup is
2773            required.  If the template-id was for a template-class, we
2774            will sometimes have a TYPE_DECL at this point.  */
2775         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2776             || TREE_CODE (id_expression) == TYPE_DECL)
2777           decl = id_expression;
2778         /* Look up the name.  */
2779         else 
2780           {
2781             decl = cp_parser_lookup_name_simple (parser, id_expression);
2782             /* If name lookup gives us a SCOPE_REF, then the
2783                qualifying scope was dependent.  Just propagate the
2784                name.  */
2785             if (TREE_CODE (decl) == SCOPE_REF)
2786               {
2787                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2788                   *qualifying_class = TREE_OPERAND (decl, 0);
2789                 return decl;
2790               }
2791             /* Check to see if DECL is a local variable in a context
2792                where that is forbidden.  */
2793             if (parser->local_variables_forbidden_p
2794                 && local_variable_p (decl))
2795               {
2796                 /* It might be that we only found DECL because we are
2797                    trying to be generous with pre-ISO scoping rules.
2798                    For example, consider:
2799
2800                      int i;
2801                      void g() {
2802                        for (int i = 0; i < 10; ++i) {}
2803                        extern void f(int j = i);
2804                      }
2805
2806                    Here, name look up will originally find the out 
2807                    of scope `i'.  We need to issue a warning message,
2808                    but then use the global `i'.  */
2809                 decl = check_for_out_of_scope_variable (decl);
2810                 if (local_variable_p (decl))
2811                   {
2812                     error ("local variable `%D' may not appear in this context",
2813                            decl);
2814                     return error_mark_node;
2815                   }
2816               }
2817
2818             /* If unqualified name lookup fails while processing a
2819                template, that just means that we need to do name
2820                lookup again when the template is instantiated.  */
2821             if (!parser->scope 
2822                 && decl == error_mark_node
2823                 && processing_template_decl)
2824               {
2825                 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2826                 return build_min_nt (LOOKUP_EXPR, id_expression);
2827               }
2828             else if (decl == error_mark_node
2829                      && !processing_template_decl)
2830               {
2831                 if (!parser->scope)
2832                   {
2833                     /* It may be resolvable as a koenig lookup function
2834                        call.  */
2835                     *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2836                     return id_expression;
2837                   }
2838                 else if (TYPE_P (parser->scope)
2839                          && !COMPLETE_TYPE_P (parser->scope))
2840                   error ("incomplete type `%T' used in nested name specifier",
2841                          parser->scope);
2842                 else if (parser->scope != global_namespace)
2843                   error ("`%D' is not a member of `%D'",
2844                          id_expression, parser->scope);
2845                 else
2846                   error ("`::%D' has not been declared", id_expression);
2847               }
2848             /* If DECL is a variable would be out of scope under
2849                ANSI/ISO rules, but in scope in the ARM, name lookup
2850                will succeed.  Issue a diagnostic here.  */
2851             else
2852               decl = check_for_out_of_scope_variable (decl);
2853
2854             /* Remember that the name was used in the definition of
2855                the current class so that we can check later to see if
2856                the meaning would have been different after the class
2857                was entirely defined.  */
2858             if (!parser->scope && decl != error_mark_node)
2859               maybe_note_name_used_in_class (id_expression, decl);
2860           }
2861
2862         /* If we didn't find anything, or what we found was a type,
2863            then this wasn't really an id-expression.  */
2864         if (TREE_CODE (decl) == TYPE_DECL
2865             || TREE_CODE (decl) == NAMESPACE_DECL
2866             || (TREE_CODE (decl) == TEMPLATE_DECL
2867                 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2868           {
2869             cp_parser_error (parser, 
2870                              "expected primary-expression");
2871             return error_mark_node;
2872           }
2873
2874         /* If the name resolved to a template parameter, there is no
2875            need to look it up again later.  Similarly, we resolve
2876            enumeration constants to their underlying values.  */
2877         if (TREE_CODE (decl) == CONST_DECL)
2878           {
2879             *idk = CP_PARSER_ID_KIND_NONE;
2880             if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2881               return DECL_INITIAL (decl);
2882             return decl;
2883           }
2884         else
2885           {
2886             bool dependent_p;
2887             
2888             /* If the declaration was explicitly qualified indicate
2889                that.  The semantics of `A::f(3)' are different than
2890                `f(3)' if `f' is virtual.  */
2891             *idk = (parser->scope 
2892                     ? CP_PARSER_ID_KIND_QUALIFIED
2893                     : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2894                        ? CP_PARSER_ID_KIND_TEMPLATE_ID
2895                        : CP_PARSER_ID_KIND_UNQUALIFIED));
2896
2897
2898             /* [temp.dep.expr]
2899                
2900                An id-expression is type-dependent if it contains an
2901                identifier that was declared with a dependent type.
2902                
2903                As an optimization, we could choose not to create a
2904                LOOKUP_EXPR for a name that resolved to a local
2905                variable in the template function that we are currently
2906                declaring; such a name cannot ever resolve to anything
2907                else.  If we did that we would not have to look up
2908                these names at instantiation time.
2909                
2910                The standard is not very specific about an
2911                id-expression that names a set of overloaded functions.
2912                What if some of them have dependent types and some of
2913                them do not?  Presumably, such a name should be treated
2914                as a dependent name.  */
2915             /* Assume the name is not dependent.  */
2916             dependent_p = false;
2917             if (!processing_template_decl)
2918               /* No names are dependent outside a template.  */
2919               ;
2920             /* A template-id where the name of the template was not
2921                resolved is definitely dependent.  */
2922             else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2923                      && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2924                          == IDENTIFIER_NODE))
2925               dependent_p = true;
2926             /* For anything except an overloaded function, just check
2927                its type.  */
2928             else if (!is_overloaded_fn (decl))
2929               dependent_p 
2930                 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2931             /* For a set of overloaded functions, check each of the
2932                functions.  */
2933             else
2934               {
2935                 tree fns = decl;
2936
2937                 if (BASELINK_P (fns))
2938                   fns = BASELINK_FUNCTIONS (fns);
2939                   
2940                 /* For a template-id, check to see if the template
2941                    arguments are dependent.  */
2942                 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2943                   {
2944                     tree args = TREE_OPERAND (fns, 1);
2945
2946                     if (args && TREE_CODE (args) == TREE_LIST)
2947                       {
2948                         while (args)
2949                           {
2950                             if (cp_parser_dependent_template_arg_p
2951                                 (TREE_VALUE (args)))
2952                               {
2953                                 dependent_p = true;
2954                                 break;
2955                               }
2956                             args = TREE_CHAIN (args);
2957                           }
2958                       }
2959                     else if (args && TREE_CODE (args) == TREE_VEC)
2960                       {
2961                         int i; 
2962                         for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2963                           if (cp_parser_dependent_template_arg_p
2964                               (TREE_VEC_ELT (args, i)))
2965                             {
2966                               dependent_p = true;
2967                               break;
2968                             }
2969                       }
2970
2971                     /* The functions are those referred to by the
2972                        template-id.  */
2973                     fns = TREE_OPERAND (fns, 0);
2974                   }
2975
2976                 /* If there are no dependent template arguments, go
2977                    through the overlaoded functions.  */
2978                 while (fns && !dependent_p)
2979                   {
2980                     tree fn = OVL_CURRENT (fns);
2981                     
2982                     /* Member functions of dependent classes are
2983                        dependent.  */
2984                     if (TREE_CODE (fn) == FUNCTION_DECL
2985                         && cp_parser_type_dependent_expression_p (fn))
2986                       dependent_p = true;
2987                     else if (TREE_CODE (fn) == TEMPLATE_DECL
2988                              && cp_parser_dependent_template_p (fn))
2989                       dependent_p = true;
2990                     
2991                     fns = OVL_NEXT (fns);
2992                   }
2993               }
2994
2995             /* If the name was dependent on a template parameter,
2996                we will resolve the name at instantiation time.  */
2997             if (dependent_p)
2998               {
2999                 /* Create a SCOPE_REF for qualified names.  */
3000                 if (parser->scope)
3001                   {
3002                     if (TYPE_P (parser->scope))
3003                       *qualifying_class = parser->scope;
3004                     return build_nt (SCOPE_REF, 
3005                                      parser->scope, 
3006                                      id_expression);
3007                   }
3008                 /* A TEMPLATE_ID already contains all the information
3009                    we need.  */
3010                 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3011                   return id_expression;
3012                 /* Create a LOOKUP_EXPR for other unqualified names.  */
3013                 return build_min_nt (LOOKUP_EXPR, id_expression);
3014               }
3015
3016             if (parser->scope)
3017               {
3018                 decl = (adjust_result_of_qualified_name_lookup 
3019                         (decl, parser->scope, current_class_type));
3020                 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3021                   *qualifying_class = parser->scope;
3022               }
3023             /* Resolve references to variables of anonymous unions
3024                into COMPONENT_REFs.  */
3025             else if (TREE_CODE (decl) == ALIAS_DECL)
3026               decl = DECL_INITIAL (decl);
3027             else
3028               /* Transform references to non-static data members into
3029                  COMPONENT_REFs.  */
3030               decl = hack_identifier (decl, id_expression);
3031           }
3032
3033         if (TREE_DEPRECATED (decl))
3034           warn_deprecated_use (decl);
3035
3036         return decl;
3037       }
3038
3039       /* Anything else is an error.  */
3040     default:
3041       cp_parser_error (parser, "expected primary-expression");
3042       return error_mark_node;
3043     }
3044 }
3045
3046 /* Parse an id-expression.
3047
3048    id-expression:
3049      unqualified-id
3050      qualified-id
3051
3052    qualified-id:
3053      :: [opt] nested-name-specifier template [opt] unqualified-id
3054      :: identifier
3055      :: operator-function-id
3056      :: template-id
3057
3058    Return a representation of the unqualified portion of the
3059    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3060    a `::' or nested-name-specifier.
3061
3062    Often, if the id-expression was a qualified-id, the caller will
3063    want to make a SCOPE_REF to represent the qualified-id.  This
3064    function does not do this in order to avoid wastefully creating
3065    SCOPE_REFs when they are not required.
3066
3067    If ASSUME_TYPENAME_P is true then we assume that qualified names
3068    are typenames.  This flag is set when parsing a declarator-id;
3069    for something like:
3070
3071      template <class T>
3072      int S<T>::R::i = 3;
3073
3074    we are supposed to assume that `S<T>::R' is a class.
3075
3076    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3077    `template' keyword.
3078
3079    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3080    uninstantiated templates.  
3081
3082    If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3083    `template' keyword is used to explicitly indicate that the entity
3084    named is a template.  */
3085
3086 static tree
3087 cp_parser_id_expression (cp_parser *parser,
3088                          bool template_keyword_p,
3089                          bool check_dependency_p,
3090                          bool *template_p)
3091 {
3092   bool global_scope_p;
3093   bool nested_name_specifier_p;
3094
3095   /* Assume the `template' keyword was not used.  */
3096   if (template_p)
3097     *template_p = false;
3098
3099   /* Look for the optional `::' operator.  */
3100   global_scope_p 
3101     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
3102        != NULL_TREE);
3103   /* Look for the optional nested-name-specifier.  */
3104   nested_name_specifier_p 
3105     = (cp_parser_nested_name_specifier_opt (parser,
3106                                             /*typename_keyword_p=*/false,
3107                                             check_dependency_p,
3108                                             /*type_p=*/false)
3109        != NULL_TREE);
3110   /* If there is a nested-name-specifier, then we are looking at
3111      the first qualified-id production.  */
3112   if (nested_name_specifier_p)
3113     {
3114       tree saved_scope;
3115       tree saved_object_scope;
3116       tree saved_qualifying_scope;
3117       tree unqualified_id;
3118       bool is_template;
3119
3120       /* See if the next token is the `template' keyword.  */
3121       if (!template_p)
3122         template_p = &is_template;
3123       *template_p = cp_parser_optional_template_keyword (parser);
3124       /* Name lookup we do during the processing of the
3125          unqualified-id might obliterate SCOPE.  */
3126       saved_scope = parser->scope;
3127       saved_object_scope = parser->object_scope;
3128       saved_qualifying_scope = parser->qualifying_scope;
3129       /* Process the final unqualified-id.  */
3130       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3131                                                  check_dependency_p);
3132       /* Restore the SAVED_SCOPE for our caller.  */
3133       parser->scope = saved_scope;
3134       parser->object_scope = saved_object_scope;
3135       parser->qualifying_scope = saved_qualifying_scope;
3136
3137       return unqualified_id;
3138     }
3139   /* Otherwise, if we are in global scope, then we are looking at one
3140      of the other qualified-id productions.  */
3141   else if (global_scope_p)
3142     {
3143       cp_token *token;
3144       tree id;
3145
3146       /* Peek at the next token.  */
3147       token = cp_lexer_peek_token (parser->lexer);
3148
3149       /* If it's an identifier, and the next token is not a "<", then
3150          we can avoid the template-id case.  This is an optimization
3151          for this common case.  */
3152       if (token->type == CPP_NAME 
3153           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3154         return cp_parser_identifier (parser);
3155
3156       cp_parser_parse_tentatively (parser);
3157       /* Try a template-id.  */
3158       id = cp_parser_template_id (parser, 
3159                                   /*template_keyword_p=*/false,
3160                                   /*check_dependency_p=*/true);
3161       /* If that worked, we're done.  */
3162       if (cp_parser_parse_definitely (parser))
3163         return id;
3164
3165       /* Peek at the next token.  (Changes in the token buffer may
3166          have invalidated the pointer obtained above.)  */
3167       token = cp_lexer_peek_token (parser->lexer);
3168
3169       switch (token->type)
3170         {
3171         case CPP_NAME:
3172           return cp_parser_identifier (parser);
3173
3174         case CPP_KEYWORD:
3175           if (token->keyword == RID_OPERATOR)
3176             return cp_parser_operator_function_id (parser);
3177           /* Fall through.  */
3178           
3179         default:
3180           cp_parser_error (parser, "expected id-expression");
3181           return error_mark_node;
3182         }
3183     }
3184   else
3185     return cp_parser_unqualified_id (parser, template_keyword_p,
3186                                      /*check_dependency_p=*/true);
3187 }
3188
3189 /* Parse an unqualified-id.
3190
3191    unqualified-id:
3192      identifier
3193      operator-function-id
3194      conversion-function-id
3195      ~ class-name
3196      template-id
3197
3198    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3199    keyword, in a construct like `A::template ...'.
3200
3201    Returns a representation of unqualified-id.  For the `identifier'
3202    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3203    production a BIT_NOT_EXPR is returned; the operand of the
3204    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3205    other productions, see the documentation accompanying the
3206    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3207    names are looked up in uninstantiated templates.  */
3208
3209 static tree
3210 cp_parser_unqualified_id (parser, template_keyword_p,
3211                           check_dependency_p)
3212      cp_parser *parser;
3213      bool template_keyword_p;
3214      bool check_dependency_p;
3215 {
3216   cp_token *token;
3217
3218   /* Peek at the next token.  */
3219   token = cp_lexer_peek_token (parser->lexer);
3220   
3221   switch (token->type)
3222     {
3223     case CPP_NAME:
3224       {
3225         tree id;
3226
3227         /* We don't know yet whether or not this will be a
3228            template-id.  */
3229         cp_parser_parse_tentatively (parser);
3230         /* Try a template-id.  */
3231         id = cp_parser_template_id (parser, template_keyword_p,
3232                                     check_dependency_p);
3233         /* If it worked, we're done.  */
3234         if (cp_parser_parse_definitely (parser))
3235           return id;
3236         /* Otherwise, it's an ordinary identifier.  */
3237         return cp_parser_identifier (parser);
3238       }
3239
3240     case CPP_TEMPLATE_ID:
3241       return cp_parser_template_id (parser, template_keyword_p,
3242                                     check_dependency_p);
3243
3244     case CPP_COMPL:
3245       {
3246         tree type_decl;
3247         tree qualifying_scope;
3248         tree object_scope;
3249         tree scope;
3250
3251         /* Consume the `~' token.  */
3252         cp_lexer_consume_token (parser->lexer);
3253         /* Parse the class-name.  The standard, as written, seems to
3254            say that:
3255
3256              template <typename T> struct S { ~S (); };
3257              template <typename T> S<T>::~S() {}
3258
3259            is invalid, since `~' must be followed by a class-name, but
3260            `S<T>' is dependent, and so not known to be a class.
3261            That's not right; we need to look in uninstantiated
3262            templates.  A further complication arises from:
3263
3264              template <typename T> void f(T t) {
3265                t.T::~T();
3266              } 
3267
3268            Here, it is not possible to look up `T' in the scope of `T'
3269            itself.  We must look in both the current scope, and the
3270            scope of the containing complete expression.  
3271
3272            Yet another issue is:
3273
3274              struct S {
3275                int S;
3276                ~S();
3277              };
3278
3279              S::~S() {}
3280
3281            The standard does not seem to say that the `S' in `~S'
3282            should refer to the type `S' and not the data member
3283            `S::S'.  */
3284
3285         /* DR 244 says that we look up the name after the "~" in the
3286            same scope as we looked up the qualifying name.  That idea
3287            isn't fully worked out; it's more complicated than that.  */
3288         scope = parser->scope;
3289         object_scope = parser->object_scope;
3290         qualifying_scope = parser->qualifying_scope;
3291
3292         /* If the name is of the form "X::~X" it's OK.  */
3293         if (scope && TYPE_P (scope)
3294             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3295             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3296                 == CPP_OPEN_PAREN)
3297             && (cp_lexer_peek_token (parser->lexer)->value 
3298                 == TYPE_IDENTIFIER (scope)))
3299           {
3300             cp_lexer_consume_token (parser->lexer);
3301             return build_nt (BIT_NOT_EXPR, scope);
3302           }
3303
3304         /* If there was an explicit qualification (S::~T), first look
3305            in the scope given by the qualification (i.e., S).  */
3306         if (scope)
3307           {
3308             cp_parser_parse_tentatively (parser);
3309             type_decl = cp_parser_class_name (parser, 
3310                                               /*typename_keyword_p=*/false,
3311                                               /*template_keyword_p=*/false,
3312                                               /*type_p=*/false,
3313                                               /*check_access_p=*/true,
3314                                               /*check_dependency=*/false,
3315                                               /*class_head_p=*/false);
3316             if (cp_parser_parse_definitely (parser))
3317               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3318           }
3319         /* In "N::S::~S", look in "N" as well.  */
3320         if (scope && qualifying_scope)
3321           {
3322             cp_parser_parse_tentatively (parser);
3323             parser->scope = qualifying_scope;
3324             parser->object_scope = NULL_TREE;
3325             parser->qualifying_scope = NULL_TREE;
3326             type_decl 
3327               = cp_parser_class_name (parser, 
3328                                       /*typename_keyword_p=*/false,
3329                                       /*template_keyword_p=*/false,
3330                                       /*type_p=*/false,
3331                                       /*check_access_p=*/true,
3332                                       /*check_dependency=*/false,
3333                                       /*class_head_p=*/false);
3334             if (cp_parser_parse_definitely (parser))
3335               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3336           }
3337         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3338         else if (object_scope)
3339           {
3340             cp_parser_parse_tentatively (parser);
3341             parser->scope = object_scope;
3342             parser->object_scope = NULL_TREE;
3343             parser->qualifying_scope = NULL_TREE;
3344             type_decl 
3345               = cp_parser_class_name (parser, 
3346                                       /*typename_keyword_p=*/false,
3347                                       /*template_keyword_p=*/false,
3348                                       /*type_p=*/false,
3349                                       /*check_access_p=*/true,
3350                                       /*check_dependency=*/false,
3351                                       /*class_head_p=*/false);
3352             if (cp_parser_parse_definitely (parser))
3353               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3354           }
3355         /* Look in the surrounding context.  */
3356         parser->scope = NULL_TREE;
3357         parser->object_scope = NULL_TREE;
3358         parser->qualifying_scope = NULL_TREE;
3359         type_decl 
3360           = cp_parser_class_name (parser, 
3361                                   /*typename_keyword_p=*/false,
3362                                   /*template_keyword_p=*/false,
3363                                   /*type_p=*/false,
3364                                   /*check_access_p=*/true,
3365                                   /*check_dependency=*/false,
3366                                   /*class_head_p=*/false);
3367         /* If an error occurred, assume that the name of the
3368            destructor is the same as the name of the qualifying
3369            class.  That allows us to keep parsing after running
3370            into ill-formed destructor names.  */
3371         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3372           return build_nt (BIT_NOT_EXPR, scope);
3373         else if (type_decl == error_mark_node)
3374           return error_mark_node;
3375
3376         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3377       }
3378
3379     case CPP_KEYWORD:
3380       if (token->keyword == RID_OPERATOR)
3381         {
3382           tree id;
3383
3384           /* This could be a template-id, so we try that first.  */
3385           cp_parser_parse_tentatively (parser);
3386           /* Try a template-id.  */
3387           id = cp_parser_template_id (parser, template_keyword_p,
3388                                       /*check_dependency_p=*/true);
3389           /* If that worked, we're done.  */
3390           if (cp_parser_parse_definitely (parser))
3391             return id;
3392           /* We still don't know whether we're looking at an
3393              operator-function-id or a conversion-function-id.  */
3394           cp_parser_parse_tentatively (parser);
3395           /* Try an operator-function-id.  */
3396           id = cp_parser_operator_function_id (parser);
3397           /* If that didn't work, try a conversion-function-id.  */
3398           if (!cp_parser_parse_definitely (parser))
3399             id = cp_parser_conversion_function_id (parser);
3400
3401           return id;
3402         }
3403       /* Fall through.  */
3404
3405     default:
3406       cp_parser_error (parser, "expected unqualified-id");
3407       return error_mark_node;
3408     }
3409 }
3410
3411 /* Parse an (optional) nested-name-specifier.
3412
3413    nested-name-specifier:
3414      class-or-namespace-name :: nested-name-specifier [opt]
3415      class-or-namespace-name :: template nested-name-specifier [opt]
3416
3417    PARSER->SCOPE should be set appropriately before this function is
3418    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3419    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3420    in name lookups.
3421
3422    Sets PARSER->SCOPE to the class (TYPE) or namespace
3423    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3424    it unchanged if there is no nested-name-specifier.  Returns the new
3425    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  */
3426
3427 static tree
3428 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3429                                      bool typename_keyword_p, 
3430                                      bool check_dependency_p,
3431                                      bool type_p)
3432 {
3433   bool success = false;
3434   tree access_check = NULL_TREE;
3435   ptrdiff_t start;
3436
3437   /* If the next token corresponds to a nested name specifier, there
3438      is no need to reparse it.  */
3439   if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3440     {
3441       tree value;
3442       tree check;
3443
3444       /* Get the stored value.  */
3445       value = cp_lexer_consume_token (parser->lexer)->value;
3446       /* Perform any access checks that were deferred.  */
3447       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3448         cp_parser_defer_access_check (parser, 
3449                                       TREE_PURPOSE (check),
3450                                       TREE_VALUE (check));
3451       /* Set the scope from the stored value.  */
3452       parser->scope = TREE_VALUE (value);
3453       parser->qualifying_scope = TREE_TYPE (value);
3454       parser->object_scope = NULL_TREE;
3455       return parser->scope;
3456     }
3457
3458   /* Remember where the nested-name-specifier starts.  */
3459   if (cp_parser_parsing_tentatively (parser)
3460       && !cp_parser_committed_to_tentative_parse (parser))
3461     {
3462       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3463       start = cp_lexer_token_difference (parser->lexer,
3464                                          parser->lexer->first_token,
3465                                          next_token);
3466       access_check = parser->context->deferred_access_checks;
3467     }
3468   else
3469     start = -1;
3470
3471   while (true)
3472     {
3473       tree new_scope;
3474       tree old_scope;
3475       tree saved_qualifying_scope;
3476       cp_token *token;
3477       bool template_keyword_p;
3478
3479       /* Spot cases that cannot be the beginning of a
3480          nested-name-specifier.  On the second and subsequent times
3481          through the loop, we look for the `template' keyword.  */
3482       token = cp_lexer_peek_token (parser->lexer);
3483       if (success && token->keyword == RID_TEMPLATE)
3484         ;
3485       /* A template-id can start a nested-name-specifier.  */
3486       else if (token->type == CPP_TEMPLATE_ID)
3487         ;
3488       else
3489         {
3490           /* If the next token is not an identifier, then it is
3491              definitely not a class-or-namespace-name.  */
3492           if (token->type != CPP_NAME)
3493             break;
3494           /* If the following token is neither a `<' (to begin a
3495              template-id), nor a `::', then we are not looking at a
3496              nested-name-specifier.  */
3497           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3498           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3499             break;
3500         }
3501
3502       /* The nested-name-specifier is optional, so we parse
3503          tentatively.  */
3504       cp_parser_parse_tentatively (parser);
3505
3506       /* Look for the optional `template' keyword, if this isn't the
3507          first time through the loop.  */
3508       if (success)
3509         template_keyword_p = cp_parser_optional_template_keyword (parser);
3510       else
3511         template_keyword_p = false;
3512
3513       /* Save the old scope since the name lookup we are about to do
3514          might destroy it.  */
3515       old_scope = parser->scope;
3516       saved_qualifying_scope = parser->qualifying_scope;
3517       /* Parse the qualifying entity.  */
3518       new_scope 
3519         = cp_parser_class_or_namespace_name (parser,
3520                                              typename_keyword_p,
3521                                              template_keyword_p,
3522                                              check_dependency_p,
3523                                              type_p);
3524       /* Look for the `::' token.  */
3525       cp_parser_require (parser, CPP_SCOPE, "`::'");
3526
3527       /* If we found what we wanted, we keep going; otherwise, we're
3528          done.  */
3529       if (!cp_parser_parse_definitely (parser))
3530         {
3531           bool error_p = false;
3532
3533           /* Restore the OLD_SCOPE since it was valid before the
3534              failed attempt at finding the last
3535              class-or-namespace-name.  */
3536           parser->scope = old_scope;
3537           parser->qualifying_scope = saved_qualifying_scope;
3538           /* If the next token is an identifier, and the one after
3539              that is a `::', then any valid interpretation would have
3540              found a class-or-namespace-name.  */
3541           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3542                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3543                      == CPP_SCOPE)
3544                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3545                      != CPP_COMPL))
3546             {
3547               token = cp_lexer_consume_token (parser->lexer);
3548               if (!error_p) 
3549                 {
3550                   tree decl;
3551
3552                   decl = cp_parser_lookup_name_simple (parser, token->value);
3553                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3554                     error ("`%D' used without template parameters",
3555                            decl);
3556                   else if (parser->scope)
3557                     {
3558                       if (TYPE_P (parser->scope))
3559                         error ("`%T::%D' is not a class-name or "
3560                                "namespace-name",
3561                                parser->scope, token->value);
3562                       else
3563                         error ("`%D::%D' is not a class-name or "
3564                                "namespace-name",
3565                                parser->scope, token->value);
3566                     }
3567                   else
3568                     error ("`%D' is not a class-name or namespace-name",
3569                            token->value);
3570                   parser->scope = NULL_TREE;
3571                   error_p = true;
3572                   /* Treat this as a successful nested-name-specifier
3573                      due to:
3574
3575                      [basic.lookup.qual]
3576
3577                      If the name found is not a class-name (clause
3578                      _class_) or namespace-name (_namespace.def_), the
3579                      program is ill-formed.  */
3580                   success = true;
3581                 }
3582               cp_lexer_consume_token (parser->lexer);
3583             }
3584           break;
3585         }
3586
3587       /* We've found one valid nested-name-specifier.  */
3588       success = true;
3589       /* Make sure we look in the right scope the next time through
3590          the loop.  */
3591       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3592                        ? TREE_TYPE (new_scope)
3593                        : new_scope);
3594       /* If it is a class scope, try to complete it; we are about to
3595          be looking up names inside the class.  */
3596       if (TYPE_P (parser->scope))
3597         complete_type (parser->scope);
3598     }
3599
3600   /* If parsing tentatively, replace the sequence of tokens that makes
3601      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3602      token.  That way, should we re-parse the token stream, we will
3603      not have to repeat the effort required to do the parse, nor will
3604      we issue duplicate error messages.  */
3605   if (success && start >= 0)
3606     {
3607       cp_token *token;
3608       tree c;
3609
3610       /* Find the token that corresponds to the start of the
3611          template-id.  */
3612       token = cp_lexer_advance_token (parser->lexer, 
3613                                       parser->lexer->first_token,
3614                                       start);
3615
3616       /* Remember the access checks associated with this
3617          nested-name-specifier.  */
3618       c = parser->context->deferred_access_checks;
3619       if (c == access_check)
3620         access_check = NULL_TREE;
3621       else
3622         {
3623           while (TREE_CHAIN (c) != access_check)
3624             c = TREE_CHAIN (c);
3625           access_check = parser->context->deferred_access_checks;
3626           parser->context->deferred_access_checks = TREE_CHAIN (c);
3627           TREE_CHAIN (c) = NULL_TREE;
3628         }
3629
3630       /* Reset the contents of the START token.  */
3631       token->type = CPP_NESTED_NAME_SPECIFIER;
3632       token->value = build_tree_list (access_check, parser->scope);
3633       TREE_TYPE (token->value) = parser->qualifying_scope;
3634       token->keyword = RID_MAX;
3635       /* Purge all subsequent tokens.  */
3636       cp_lexer_purge_tokens_after (parser->lexer, token);
3637     }
3638
3639   return success ? parser->scope : NULL_TREE;
3640 }
3641
3642 /* Parse a nested-name-specifier.  See
3643    cp_parser_nested_name_specifier_opt for details.  This function
3644    behaves identically, except that it will an issue an error if no
3645    nested-name-specifier is present, and it will return
3646    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3647    is present.  */
3648
3649 static tree
3650 cp_parser_nested_name_specifier (cp_parser *parser, 
3651                                  bool typename_keyword_p, 
3652                                  bool check_dependency_p,
3653                                  bool type_p)
3654 {
3655   tree scope;
3656
3657   /* Look for the nested-name-specifier.  */
3658   scope = cp_parser_nested_name_specifier_opt (parser,
3659                                                typename_keyword_p,
3660                                                check_dependency_p,
3661                                                type_p);
3662   /* If it was not present, issue an error message.  */
3663   if (!scope)
3664     {
3665       cp_parser_error (parser, "expected nested-name-specifier");
3666       return error_mark_node;
3667     }
3668
3669   return scope;
3670 }
3671
3672 /* Parse a class-or-namespace-name.
3673
3674    class-or-namespace-name:
3675      class-name
3676      namespace-name
3677
3678    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3679    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3680    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3681    TYPE_P is TRUE iff the next name should be taken as a class-name,
3682    even the same name is declared to be another entity in the same
3683    scope.
3684
3685    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3686    specified by the class-or-namespace-name.  If neither is found the
3687    ERROR_MARK_NODE is returned.  */
3688
3689 static tree
3690 cp_parser_class_or_namespace_name (cp_parser *parser, 
3691                                    bool typename_keyword_p,
3692                                    bool template_keyword_p,
3693                                    bool check_dependency_p,
3694                                    bool type_p)
3695 {
3696   tree saved_scope;
3697   tree saved_qualifying_scope;
3698   tree saved_object_scope;
3699   tree scope;
3700   bool only_class_p;
3701
3702   /* If the next token is the `template' keyword, we know that we are
3703      looking at a class-name.  */
3704   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3705     return cp_parser_class_name (parser, 
3706                                  typename_keyword_p,
3707                                  template_keyword_p,
3708                                  type_p,
3709                                  /*check_access_p=*/true,
3710                                  check_dependency_p,
3711                                  /*class_head_p=*/false);
3712   /* Before we try to parse the class-name, we must save away the
3713      current PARSER->SCOPE since cp_parser_class_name will destroy
3714      it.  */
3715   saved_scope = parser->scope;
3716   saved_qualifying_scope = parser->qualifying_scope;
3717   saved_object_scope = parser->object_scope;
3718   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3719      there is no need to look for a namespace-name.  */
3720   only_class_p = saved_scope && TYPE_P (saved_scope);
3721   if (!only_class_p)
3722     cp_parser_parse_tentatively (parser);
3723   scope = cp_parser_class_name (parser, 
3724                                 typename_keyword_p,
3725                                 template_keyword_p,
3726                                 type_p,
3727                                 /*check_access_p=*/true,
3728                                 check_dependency_p,
3729                                 /*class_head_p=*/false);
3730   /* If that didn't work, try for a namespace-name.  */
3731   if (!only_class_p && !cp_parser_parse_definitely (parser))
3732     {
3733       /* Restore the saved scope.  */
3734       parser->scope = saved_scope;
3735       parser->qualifying_scope = saved_qualifying_scope;
3736       parser->object_scope = saved_object_scope;
3737       /* If we are not looking at an identifier followed by the scope
3738          resolution operator, then this is not part of a
3739          nested-name-specifier.  (Note that this function is only used
3740          to parse the components of a nested-name-specifier.)  */
3741       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3742           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3743         return error_mark_node;
3744       scope = cp_parser_namespace_name (parser);
3745     }
3746
3747   return scope;
3748 }
3749
3750 /* Parse a postfix-expression.
3751
3752    postfix-expression:
3753      primary-expression
3754      postfix-expression [ expression ]
3755      postfix-expression ( expression-list [opt] )
3756      simple-type-specifier ( expression-list [opt] )
3757      typename :: [opt] nested-name-specifier identifier 
3758        ( expression-list [opt] )
3759      typename :: [opt] nested-name-specifier template [opt] template-id
3760        ( expression-list [opt] )
3761      postfix-expression . template [opt] id-expression
3762      postfix-expression -> template [opt] id-expression
3763      postfix-expression . pseudo-destructor-name
3764      postfix-expression -> pseudo-destructor-name
3765      postfix-expression ++
3766      postfix-expression --
3767      dynamic_cast < type-id > ( expression )
3768      static_cast < type-id > ( expression )
3769      reinterpret_cast < type-id > ( expression )
3770      const_cast < type-id > ( expression )
3771      typeid ( expression )
3772      typeid ( type-id )
3773
3774    GNU Extension:
3775      
3776    postfix-expression:
3777      ( type-id ) { initializer-list , [opt] }
3778
3779    This extension is a GNU version of the C99 compound-literal
3780    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3781    but they are essentially the same concept.)
3782
3783    If ADDRESS_P is true, the postfix expression is the operand of the
3784    `&' operator.
3785
3786    Returns a representation of the expression.  */
3787
3788 static tree
3789 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3790 {
3791   cp_token *token;
3792   enum rid keyword;
3793   cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3794   tree postfix_expression = NULL_TREE;
3795   /* Non-NULL only if the current postfix-expression can be used to
3796      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3797      class used to qualify the member.  */
3798   tree qualifying_class = NULL_TREE;
3799   bool done;
3800
3801   /* Peek at the next token.  */
3802   token = cp_lexer_peek_token (parser->lexer);
3803   /* Some of the productions are determined by keywords.  */
3804   keyword = token->keyword;
3805   switch (keyword)
3806     {
3807     case RID_DYNCAST:
3808     case RID_STATCAST:
3809     case RID_REINTCAST:
3810     case RID_CONSTCAST:
3811       {
3812         tree type;
3813         tree expression;
3814         const char *saved_message;
3815
3816         /* All of these can be handled in the same way from the point
3817            of view of parsing.  Begin by consuming the token
3818            identifying the cast.  */
3819         cp_lexer_consume_token (parser->lexer);
3820         
3821         /* New types cannot be defined in the cast.  */
3822         saved_message = parser->type_definition_forbidden_message;
3823         parser->type_definition_forbidden_message
3824           = "types may not be defined in casts";
3825
3826         /* Look for the opening `<'.  */
3827         cp_parser_require (parser, CPP_LESS, "`<'");
3828         /* Parse the type to which we are casting.  */
3829         type = cp_parser_type_id (parser);
3830         /* Look for the closing `>'.  */
3831         cp_parser_require (parser, CPP_GREATER, "`>'");
3832         /* Restore the old message.  */
3833         parser->type_definition_forbidden_message = saved_message;
3834
3835         /* And the expression which is being cast.  */
3836         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3837         expression = cp_parser_expression (parser);
3838         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3839
3840         switch (keyword)
3841           {
3842           case RID_DYNCAST:
3843             postfix_expression
3844               = build_dynamic_cast (type, expression);
3845             break;
3846           case RID_STATCAST:
3847             postfix_expression
3848               = build_static_cast (type, expression);
3849             break;
3850           case RID_REINTCAST:
3851             postfix_expression
3852               = build_reinterpret_cast (type, expression);
3853             break;
3854           case RID_CONSTCAST:
3855             postfix_expression
3856               = build_const_cast (type, expression);
3857             break;
3858           default:
3859             abort ();
3860           }
3861       }
3862       break;
3863
3864     case RID_TYPEID:
3865       {
3866         tree type;
3867         const char *saved_message;
3868
3869         /* Consume the `typeid' token.  */
3870         cp_lexer_consume_token (parser->lexer);
3871         /* Look for the `(' token.  */
3872         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3873         /* Types cannot be defined in a `typeid' expression.  */
3874         saved_message = parser->type_definition_forbidden_message;
3875         parser->type_definition_forbidden_message
3876           = "types may not be defined in a `typeid\' expression";
3877         /* We can't be sure yet whether we're looking at a type-id or an
3878            expression.  */
3879         cp_parser_parse_tentatively (parser);
3880         /* Try a type-id first.  */
3881         type = cp_parser_type_id (parser);
3882         /* Look for the `)' token.  Otherwise, we can't be sure that
3883            we're not looking at an expression: consider `typeid (int
3884            (3))', for example.  */
3885         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3886         /* If all went well, simply lookup the type-id.  */
3887         if (cp_parser_parse_definitely (parser))
3888           postfix_expression = get_typeid (type);
3889         /* Otherwise, fall back to the expression variant.  */
3890         else
3891           {
3892             tree expression;
3893
3894             /* Look for an expression.  */
3895             expression = cp_parser_expression (parser);
3896             /* Compute its typeid.  */
3897             postfix_expression = build_typeid (expression);
3898             /* Look for the `)' token.  */
3899             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3900           }
3901
3902         /* Restore the saved message.  */
3903         parser->type_definition_forbidden_message = saved_message;
3904       }
3905       break;
3906       
3907     case RID_TYPENAME:
3908       {
3909         bool template_p = false;
3910         tree id;
3911         tree type;
3912
3913         /* Consume the `typename' token.  */
3914         cp_lexer_consume_token (parser->lexer);
3915         /* Look for the optional `::' operator.  */
3916         cp_parser_global_scope_opt (parser, 
3917                                     /*current_scope_valid_p=*/false);
3918         /* Look for the nested-name-specifier.  */
3919         cp_parser_nested_name_specifier (parser,
3920                                          /*typename_keyword_p=*/true,
3921                                          /*check_dependency_p=*/true,
3922                                          /*type_p=*/true);
3923         /* Look for the optional `template' keyword.  */
3924         template_p = cp_parser_optional_template_keyword (parser);
3925         /* We don't know whether we're looking at a template-id or an
3926            identifier.  */
3927         cp_parser_parse_tentatively (parser);
3928         /* Try a template-id.  */
3929         id = cp_parser_template_id (parser, template_p,
3930                                     /*check_dependency_p=*/true);
3931         /* If that didn't work, try an identifier.  */
3932         if (!cp_parser_parse_definitely (parser))
3933           id = cp_parser_identifier (parser);
3934         /* Create a TYPENAME_TYPE to represent the type to which the
3935            functional cast is being performed.  */
3936         type = make_typename_type (parser->scope, id, 
3937                                    /*complain=*/1);
3938
3939         postfix_expression = cp_parser_functional_cast (parser, type);
3940       }
3941       break;
3942
3943     default:
3944       {
3945         tree type;
3946
3947         /* If the next thing is a simple-type-specifier, we may be
3948            looking at a functional cast.  We could also be looking at
3949            an id-expression.  So, we try the functional cast, and if
3950            that doesn't work we fall back to the primary-expression.  */
3951         cp_parser_parse_tentatively (parser);
3952         /* Look for the simple-type-specifier.  */
3953         type = cp_parser_simple_type_specifier (parser, 
3954                                                 CP_PARSER_FLAGS_NONE);
3955         /* Parse the cast itself.  */
3956         if (!cp_parser_error_occurred (parser))
3957           postfix_expression 
3958             = cp_parser_functional_cast (parser, type);
3959         /* If that worked, we're done.  */
3960         if (cp_parser_parse_definitely (parser))
3961           break;
3962
3963         /* If the functional-cast didn't work out, try a
3964            compound-literal.  */
3965         if (cp_parser_allow_gnu_extensions_p (parser))
3966           {
3967             tree initializer_list = NULL_TREE;
3968
3969             cp_parser_parse_tentatively (parser);
3970             /* Look for the `('.  */
3971             if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3972               {
3973                 type = cp_parser_type_id (parser);
3974                 /* Look for the `)'.  */
3975                 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3976                 /* Look for the `{'.  */
3977                 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3978                 /* If things aren't going well, there's no need to
3979                    keep going.  */
3980                 if (!cp_parser_error_occurred (parser))
3981                   {
3982                     /* Parse the initializer-list.  */
3983                     initializer_list 
3984                       = cp_parser_initializer_list (parser);
3985                     /* Allow a trailing `,'.  */
3986                     if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3987                       cp_lexer_consume_token (parser->lexer);
3988                     /* Look for the final `}'.  */
3989                     cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3990                   }
3991               }
3992             /* If that worked, we're definitely looking at a
3993                compound-literal expression.  */
3994             if (cp_parser_parse_definitely (parser))
3995               {
3996                 /* Warn the user that a compound literal is not
3997                    allowed in standard C++.  */
3998                 if (pedantic)
3999                   pedwarn ("ISO C++ forbids compound-literals");
4000                 /* Form the representation of the compound-literal.  */
4001                 postfix_expression 
4002                   = finish_compound_literal (type, initializer_list);
4003                 break;
4004               }
4005           }
4006
4007         /* It must be a primary-expression.  */
4008         postfix_expression = cp_parser_primary_expression (parser, 
4009                                                            &idk,
4010                                                            &qualifying_class);
4011       }
4012       break;
4013     }
4014
4015   /* Peek at the next token.  */
4016   token = cp_lexer_peek_token (parser->lexer);
4017   done = (token->type != CPP_OPEN_SQUARE
4018           && token->type != CPP_OPEN_PAREN
4019           && token->type != CPP_DOT
4020           && token->type != CPP_DEREF
4021           && token->type != CPP_PLUS_PLUS
4022           && token->type != CPP_MINUS_MINUS);
4023
4024   /* If the postfix expression is complete, finish up.  */
4025   if (address_p && qualifying_class && done)
4026     {
4027       if (TREE_CODE (postfix_expression) == SCOPE_REF)
4028         postfix_expression = TREE_OPERAND (postfix_expression, 1);
4029       postfix_expression 
4030         = build_offset_ref (qualifying_class, postfix_expression);
4031       return postfix_expression;
4032     }
4033
4034   /* Otherwise, if we were avoiding committing until we knew
4035      whether or not we had a pointer-to-member, we now know that
4036      the expression is an ordinary reference to a qualified name.  */
4037   if (qualifying_class && !processing_template_decl)
4038     {
4039       if (TREE_CODE (postfix_expression) == FIELD_DECL)
4040         postfix_expression 
4041           = finish_non_static_data_member (postfix_expression,
4042                                            qualifying_class);
4043       else if (BASELINK_P (postfix_expression))
4044         {
4045           tree fn;
4046           tree fns;
4047
4048           /* See if any of the functions are non-static members.  */
4049           fns = BASELINK_FUNCTIONS (postfix_expression);
4050           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4051             fns = TREE_OPERAND (fns, 0);
4052           for (fn = fns; fn; fn = OVL_NEXT (fn))
4053             if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4054               break;
4055           /* If so, the expression may be relative to the current
4056              class.  */
4057           if (fn && current_class_type 
4058               && DERIVED_FROM_P (qualifying_class, current_class_type))
4059             postfix_expression 
4060               = (build_class_member_access_expr 
4061                  (maybe_dummy_object (qualifying_class, NULL),
4062                   postfix_expression,
4063                   BASELINK_ACCESS_BINFO (postfix_expression),
4064                   /*preserve_reference=*/false));
4065           else if (done)
4066             return build_offset_ref (qualifying_class,
4067                                      postfix_expression);
4068         }
4069     }
4070
4071   /* Remember that there was a reference to this entity.  */
4072   if (DECL_P (postfix_expression))
4073     mark_used (postfix_expression);
4074
4075   /* Keep looping until the postfix-expression is complete.  */
4076   while (true)
4077     {
4078       if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4079           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4080         {
4081           /* It is not a Koenig lookup function call.  */
4082           unqualified_name_lookup_error (postfix_expression);
4083           postfix_expression = error_mark_node;
4084         }
4085       
4086       /* Peek at the next token.  */
4087       token = cp_lexer_peek_token (parser->lexer);
4088
4089       switch (token->type)
4090         {
4091         case CPP_OPEN_SQUARE:
4092           /* postfix-expression [ expression ] */
4093           {
4094             tree index;
4095
4096             /* Consume the `[' token.  */
4097             cp_lexer_consume_token (parser->lexer);
4098             /* Parse the index expression.  */
4099             index = cp_parser_expression (parser);
4100             /* Look for the closing `]'.  */
4101             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4102
4103             /* Build the ARRAY_REF.  */
4104             postfix_expression 
4105               = grok_array_decl (postfix_expression, index);
4106             idk = CP_PARSER_ID_KIND_NONE;
4107           }
4108           break;
4109
4110         case CPP_OPEN_PAREN:
4111           /* postfix-expression ( expression-list [opt] ) */
4112           {
4113             tree args;
4114
4115             /* Consume the `(' token.  */
4116             cp_lexer_consume_token (parser->lexer);
4117             /* If the next token is not a `)', then there are some
4118                arguments.  */
4119             if (cp_lexer_next_token_is_not (parser->lexer, 
4120                                             CPP_CLOSE_PAREN))
4121               args = cp_parser_expression_list (parser);
4122             else
4123               args = NULL_TREE;
4124             /* Look for the closing `)'.  */
4125             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4126
4127             if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4128                 && (is_overloaded_fn (postfix_expression)
4129                     || DECL_P (postfix_expression)
4130                     || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4131                 && args)
4132               {
4133                 tree arg;
4134                 tree identifier = NULL_TREE;
4135                 tree functions = NULL_TREE;
4136
4137                 /* Find the name of the overloaded function.  */
4138                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4139                   identifier = postfix_expression;
4140                 else if (is_overloaded_fn (postfix_expression))
4141                   {
4142                     functions = postfix_expression;
4143                     identifier = DECL_NAME (get_first_fn (functions));
4144                   }
4145                 else if (DECL_P (postfix_expression))
4146                   {
4147                     functions = postfix_expression;
4148                     identifier = DECL_NAME (postfix_expression);
4149                   }
4150
4151                 /* A call to a namespace-scope function using an
4152                    unqualified name.
4153
4154                    Do Koenig lookup -- unless any of the arguments are
4155                    type-dependent.  */
4156                 for (arg = args; arg; arg = TREE_CHAIN (arg))
4157                   if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4158                       break;
4159                 if (!arg)
4160                   {
4161                     postfix_expression 
4162                       = lookup_arg_dependent(identifier, functions, args);
4163                     if (!postfix_expression)
4164                       {
4165                         /* The unqualified name could not be resolved.  */
4166                         unqualified_name_lookup_error (identifier);
4167                         postfix_expression = error_mark_node;
4168                       }
4169                     postfix_expression
4170                       = build_call_from_tree (postfix_expression, args, 
4171                                               /*diallow_virtual=*/false);
4172                     break;
4173                   }
4174                 postfix_expression = build_min_nt (LOOKUP_EXPR,
4175                                                    identifier);
4176               }
4177             else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED 
4178                      && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4179               {
4180                 /* The unqualified name could not be resolved.  */
4181                 unqualified_name_lookup_error (postfix_expression);
4182                 postfix_expression = error_mark_node;
4183                 break;
4184               }
4185
4186             /* In the body of a template, no further processing is
4187                required.  */
4188             if (processing_template_decl)
4189               {
4190                 postfix_expression = build_nt (CALL_EXPR,
4191                                                postfix_expression, 
4192                                                args);
4193                 break;
4194               }
4195
4196             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4197               postfix_expression
4198                 = (build_new_method_call 
4199                    (TREE_OPERAND (postfix_expression, 0),
4200                     TREE_OPERAND (postfix_expression, 1),
4201                     args, NULL_TREE, 
4202                     (idk == CP_PARSER_ID_KIND_QUALIFIED 
4203                      ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4204             else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4205               postfix_expression = (build_offset_ref_call_from_tree
4206                                     (postfix_expression, args));
4207             else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4208               {
4209                 /* A call to a static class member, or a
4210                    namespace-scope function.  */
4211                 postfix_expression
4212                   = finish_call_expr (postfix_expression, args,
4213                                       /*disallow_virtual=*/true);
4214               }
4215             else
4216               {
4217                 /* All other function calls.  */
4218                 postfix_expression 
4219                   = finish_call_expr (postfix_expression, args, 
4220                                       /*disallow_virtual=*/false);
4221               }
4222
4223             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4224             idk = CP_PARSER_ID_KIND_NONE;
4225           }
4226           break;
4227           
4228         case CPP_DOT:
4229         case CPP_DEREF:
4230           /* postfix-expression . template [opt] id-expression  
4231              postfix-expression . pseudo-destructor-name 
4232              postfix-expression -> template [opt] id-expression
4233              postfix-expression -> pseudo-destructor-name */
4234           {
4235             tree name;
4236             bool dependent_p;
4237             bool template_p;
4238             tree scope = NULL_TREE;
4239
4240             /* If this is a `->' operator, dereference the pointer.  */
4241             if (token->type == CPP_DEREF)
4242               postfix_expression = build_x_arrow (postfix_expression);
4243             /* Check to see whether or not the expression is
4244                type-dependent.  */
4245             dependent_p = (cp_parser_type_dependent_expression_p 
4246                            (postfix_expression));
4247             /* The identifier following the `->' or `.' is not
4248                qualified.  */
4249             parser->scope = NULL_TREE;
4250             parser->qualifying_scope = NULL_TREE;
4251             parser->object_scope = NULL_TREE;
4252             /* Enter the scope corresponding to the type of the object
4253                given by the POSTFIX_EXPRESSION.  */
4254             if (!dependent_p 
4255                 && TREE_TYPE (postfix_expression) != NULL_TREE)
4256               {
4257                 scope = TREE_TYPE (postfix_expression);
4258                 /* According to the standard, no expression should
4259                    ever have reference type.  Unfortunately, we do not
4260                    currently match the standard in this respect in
4261                    that our internal representation of an expression
4262                    may have reference type even when the standard says
4263                    it does not.  Therefore, we have to manually obtain
4264                    the underlying type here.  */
4265                 if (TREE_CODE (scope) == REFERENCE_TYPE)
4266                   scope = TREE_TYPE (scope);
4267                 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4268                    type of the field.  We get an OFFSET_TYPE for
4269                    something like:
4270
4271                      S::T.a ...
4272
4273                    Probably, we should not get an OFFSET_TYPE here;
4274                    that transformation should be made only if `&S::T'
4275                    is written.  */
4276                 if (TREE_CODE (scope) == OFFSET_TYPE)
4277                   scope = TREE_TYPE (scope);
4278                 /* The type of the POSTFIX_EXPRESSION must be
4279                    complete.  */
4280                 scope = complete_type_or_else (scope, NULL_TREE);
4281                 /* Let the name lookup machinery know that we are
4282                    processing a class member access expression.  */
4283                 parser->context->object_type = scope;
4284                 /* If something went wrong, we want to be able to
4285                    discern that case, as opposed to the case where
4286                    there was no SCOPE due to the type of expression
4287                    being dependent.  */
4288                 if (!scope)
4289                   scope = error_mark_node;
4290               }
4291
4292             /* Consume the `.' or `->' operator.  */
4293             cp_lexer_consume_token (parser->lexer);
4294             /* If the SCOPE is not a scalar type, we are looking at an
4295                ordinary class member access expression, rather than a
4296                pseudo-destructor-name.  */
4297             if (!scope || !SCALAR_TYPE_P (scope))
4298               {
4299                 template_p = cp_parser_optional_template_keyword (parser);
4300                 /* Parse the id-expression.  */
4301                 name = cp_parser_id_expression (parser,
4302                                                 template_p,
4303                                                 /*check_dependency_p=*/true,
4304                                                 /*template_p=*/NULL);
4305                 /* In general, build a SCOPE_REF if the member name is
4306                    qualified.  However, if the name was not dependent
4307                    and has already been resolved; there is no need to
4308                    build the SCOPE_REF.  For example;
4309
4310                      struct X { void f(); };
4311                      template <typename T> void f(T* t) { t->X::f(); }
4312  
4313                    Even though "t" is dependent, "X::f" is not and has 
4314                    except that for a BASELINK there is no need to
4315                    include scope information.  */
4316                 if (name != error_mark_node 
4317                     && !BASELINK_P (name)
4318                     && parser->scope)
4319                   {
4320                     name = build_nt (SCOPE_REF, parser->scope, name);
4321                     parser->scope = NULL_TREE;
4322                     parser->qualifying_scope = NULL_TREE;
4323                     parser->object_scope = NULL_TREE;
4324                   }
4325                 postfix_expression 
4326                   = finish_class_member_access_expr (postfix_expression, name);
4327               }
4328             /* Otherwise, try the pseudo-destructor-name production.  */
4329             else
4330               {
4331                 tree s;
4332                 tree type;
4333
4334                 /* Parse the pseudo-destructor-name.  */
4335                 cp_parser_pseudo_destructor_name (parser, &s, &type);
4336                 /* Form the call.  */
4337                 postfix_expression 
4338                   = finish_pseudo_destructor_expr (postfix_expression,
4339                                                    s, TREE_TYPE (type));
4340               }
4341
4342             /* We no longer need to look up names in the scope of the
4343                object on the left-hand side of the `.' or `->'
4344                operator.  */
4345             parser->context->object_type = NULL_TREE;
4346             idk = CP_PARSER_ID_KIND_NONE;
4347           }
4348           break;
4349
4350         case CPP_PLUS_PLUS:
4351           /* postfix-expression ++  */
4352           /* Consume the `++' token.  */
4353           cp_lexer_consume_token (parser->lexer);
4354           /* Generate a reprsentation for the complete expression.  */
4355           postfix_expression 
4356             = finish_increment_expr (postfix_expression, 
4357                                      POSTINCREMENT_EXPR);
4358           idk = CP_PARSER_ID_KIND_NONE;
4359           break;
4360
4361         case CPP_MINUS_MINUS:
4362           /* postfix-expression -- */
4363           /* Consume the `--' token.  */
4364           cp_lexer_consume_token (parser->lexer);
4365           /* Generate a reprsentation for the complete expression.  */
4366           postfix_expression 
4367             = finish_increment_expr (postfix_expression, 
4368                                      POSTDECREMENT_EXPR);
4369           idk = CP_PARSER_ID_KIND_NONE;
4370           break;
4371
4372         default:
4373           return postfix_expression;
4374         }
4375     }
4376
4377   /* We should never get here.  */
4378   abort ();
4379   return error_mark_node;
4380 }
4381
4382 /* Parse an expression-list.
4383
4384    expression-list:
4385      assignment-expression
4386      expression-list, assignment-expression
4387
4388    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4389    representation of an assignment-expression.  Note that a TREE_LIST
4390    is returned even if there is only a single expression in the list.  */
4391
4392 static tree
4393 cp_parser_expression_list (parser)
4394      cp_parser *parser;
4395 {
4396   tree expression_list = NULL_TREE;
4397
4398   /* Consume expressions until there are no more.  */
4399   while (true)
4400     {
4401       tree expr;
4402
4403       /* Parse the next assignment-expression.  */
4404       expr = cp_parser_assignment_expression (parser);
4405       /* Add it to the list.  */
4406       expression_list = tree_cons (NULL_TREE, expr, expression_list);
4407
4408       /* If the next token isn't a `,', then we are done.  */
4409       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4410         {
4411           /* All uses of expression-list in the grammar are followed
4412              by a `)'.  Therefore, if the next token is not a `)' an
4413              error will be issued, unless we are parsing tentatively.
4414              Skip ahead to see if there is another `,' before the `)';
4415              if so, we can go there and recover.  */
4416           if (cp_parser_parsing_tentatively (parser)
4417               || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4418               || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4419             break;
4420         }
4421
4422       /* Otherwise, consume the `,' and keep going.  */
4423       cp_lexer_consume_token (parser->lexer);
4424     }
4425
4426   /* We built up the list in reverse order so we must reverse it now.  */
4427   return nreverse (expression_list);
4428 }
4429
4430 /* Parse a pseudo-destructor-name.
4431
4432    pseudo-destructor-name:
4433      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4434      :: [opt] nested-name-specifier template template-id :: ~ type-name
4435      :: [opt] nested-name-specifier [opt] ~ type-name
4436
4437    If either of the first two productions is used, sets *SCOPE to the
4438    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4439    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4440    or ERROR_MARK_NODE if no type-name is present.  */
4441
4442 static void
4443 cp_parser_pseudo_destructor_name (parser, scope, type)
4444      cp_parser *parser;
4445      tree *scope;
4446      tree *type;
4447 {
4448   bool nested_name_specifier_p;
4449
4450   /* Look for the optional `::' operator.  */
4451   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4452   /* Look for the optional nested-name-specifier.  */
4453   nested_name_specifier_p 
4454     = (cp_parser_nested_name_specifier_opt (parser,
4455                                             /*typename_keyword_p=*/false,
4456                                             /*check_dependency_p=*/true,
4457                                             /*type_p=*/false) 
4458        != NULL_TREE);
4459   /* Now, if we saw a nested-name-specifier, we might be doing the
4460      second production.  */
4461   if (nested_name_specifier_p 
4462       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4463     {
4464       /* Consume the `template' keyword.  */
4465       cp_lexer_consume_token (parser->lexer);
4466       /* Parse the template-id.  */
4467       cp_parser_template_id (parser, 
4468                              /*template_keyword_p=*/true,
4469                              /*check_dependency_p=*/false);
4470       /* Look for the `::' token.  */
4471       cp_parser_require (parser, CPP_SCOPE, "`::'");
4472     }
4473   /* If the next token is not a `~', then there might be some
4474      additional qualification. */
4475   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4476     {
4477       /* Look for the type-name.  */
4478       *scope = TREE_TYPE (cp_parser_type_name (parser));
4479       /* Look for the `::' token.  */
4480       cp_parser_require (parser, CPP_SCOPE, "`::'");
4481     }
4482   else
4483     *scope = NULL_TREE;
4484
4485   /* Look for the `~'.  */
4486   cp_parser_require (parser, CPP_COMPL, "`~'");
4487   /* Look for the type-name again.  We are not responsible for
4488      checking that it matches the first type-name.  */
4489   *type = cp_parser_type_name (parser);
4490 }
4491
4492 /* Parse a unary-expression.
4493
4494    unary-expression:
4495      postfix-expression
4496      ++ cast-expression
4497      -- cast-expression
4498      unary-operator cast-expression
4499      sizeof unary-expression
4500      sizeof ( type-id )
4501      new-expression
4502      delete-expression
4503
4504    GNU Extensions:
4505
4506    unary-expression:
4507      __extension__ cast-expression
4508      __alignof__ unary-expression
4509      __alignof__ ( type-id )
4510      __real__ cast-expression
4511      __imag__ cast-expression
4512      && identifier
4513
4514    ADDRESS_P is true iff the unary-expression is appearing as the
4515    operand of the `&' operator.
4516
4517    Returns a representation of the expresion.  */
4518
4519 static tree
4520 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4521 {
4522   cp_token *token;
4523   enum tree_code unary_operator;
4524
4525   /* Peek at the next token.  */
4526   token = cp_lexer_peek_token (parser->lexer);
4527   /* Some keywords give away the kind of expression.  */
4528   if (token->type == CPP_KEYWORD)
4529     {
4530       enum rid keyword = token->keyword;
4531
4532       switch (keyword)
4533         {
4534         case RID_ALIGNOF:
4535           {
4536             /* Consume the `alignof' token.  */
4537             cp_lexer_consume_token (parser->lexer);
4538             /* Parse the operand.  */
4539             return finish_alignof (cp_parser_sizeof_operand 
4540                                    (parser, keyword));
4541           }
4542
4543         case RID_SIZEOF:
4544           {
4545             tree operand;
4546             
4547             /* Consume the `sizeof' token.   */
4548             cp_lexer_consume_token (parser->lexer);
4549             /* Parse the operand.  */
4550             operand = cp_parser_sizeof_operand (parser, keyword);
4551
4552             /* If the type of the operand cannot be determined build a
4553                SIZEOF_EXPR.  */
4554             if (TYPE_P (operand)
4555                 ? cp_parser_dependent_type_p (operand)
4556                 : cp_parser_type_dependent_expression_p (operand))
4557               return build_min (SIZEOF_EXPR, size_type_node, operand);
4558             /* Otherwise, compute the constant value.  */
4559             else
4560               return finish_sizeof (operand);
4561           }
4562
4563         case RID_NEW:
4564           return cp_parser_new_expression (parser);
4565
4566         case RID_DELETE:
4567           return cp_parser_delete_expression (parser);
4568           
4569         case RID_EXTENSION:
4570           {
4571             /* The saved value of the PEDANTIC flag.  */
4572             int saved_pedantic;
4573             tree expr;
4574
4575             /* Save away the PEDANTIC flag.  */
4576             cp_parser_extension_opt (parser, &saved_pedantic);
4577             /* Parse the cast-expression.  */
4578             expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4579             /* Restore the PEDANTIC flag.  */
4580             pedantic = saved_pedantic;
4581
4582             return expr;
4583           }
4584
4585         case RID_REALPART:
4586         case RID_IMAGPART:
4587           {
4588             tree expression;
4589
4590             /* Consume the `__real__' or `__imag__' token.  */
4591             cp_lexer_consume_token (parser->lexer);
4592             /* Parse the cast-expression.  */
4593             expression = cp_parser_cast_expression (parser,
4594                                                     /*address_p=*/false);
4595             /* Create the complete representation.  */
4596             return build_x_unary_op ((keyword == RID_REALPART
4597                                       ? REALPART_EXPR : IMAGPART_EXPR),
4598                                      expression);
4599           }
4600           break;
4601
4602         default:
4603           break;
4604         }
4605     }
4606
4607   /* Look for the `:: new' and `:: delete', which also signal the
4608      beginning of a new-expression, or delete-expression,
4609      respectively.  If the next token is `::', then it might be one of
4610      these.  */
4611   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4612     {
4613       enum rid keyword;
4614
4615       /* See if the token after the `::' is one of the keywords in
4616          which we're interested.  */
4617       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4618       /* If it's `new', we have a new-expression.  */
4619       if (keyword == RID_NEW)
4620         return cp_parser_new_expression (parser);
4621       /* Similarly, for `delete'.  */
4622       else if (keyword == RID_DELETE)
4623         return cp_parser_delete_expression (parser);
4624     }
4625
4626   /* Look for a unary operator.  */
4627   unary_operator = cp_parser_unary_operator (token);
4628   /* The `++' and `--' operators can be handled similarly, even though
4629      they are not technically unary-operators in the grammar.  */
4630   if (unary_operator == ERROR_MARK)
4631     {
4632       if (token->type == CPP_PLUS_PLUS)
4633         unary_operator = PREINCREMENT_EXPR;
4634       else if (token->type == CPP_MINUS_MINUS)
4635         unary_operator = PREDECREMENT_EXPR;
4636       /* Handle the GNU address-of-label extension.  */
4637       else if (cp_parser_allow_gnu_extensions_p (parser)
4638                && token->type == CPP_AND_AND)
4639         {
4640           tree identifier;
4641
4642           /* Consume the '&&' token.  */
4643           cp_lexer_consume_token (parser->lexer);
4644           /* Look for the identifier.  */
4645           identifier = cp_parser_identifier (parser);
4646           /* Create an expression representing the address.  */
4647           return finish_label_address_expr (identifier);
4648         }
4649     }
4650   if (unary_operator != ERROR_MARK)
4651     {
4652       tree cast_expression;
4653
4654       /* Consume the operator token.  */
4655       token = cp_lexer_consume_token (parser->lexer);
4656       /* Parse the cast-expression.  */
4657       cast_expression 
4658         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4659       /* Now, build an appropriate representation.  */
4660       switch (unary_operator)
4661         {
4662         case INDIRECT_REF:
4663           return build_x_indirect_ref (cast_expression, "unary *");
4664           
4665         case ADDR_EXPR:
4666           return build_x_unary_op (ADDR_EXPR, cast_expression);
4667           
4668         case CONVERT_EXPR:
4669         case NEGATE_EXPR:
4670         case TRUTH_NOT_EXPR:
4671         case PREINCREMENT_EXPR:
4672         case PREDECREMENT_EXPR:
4673           return finish_unary_op_expr (unary_operator, cast_expression);
4674
4675         case BIT_NOT_EXPR:
4676           return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4677
4678         default:
4679           abort ();
4680           return error_mark_node;
4681         }
4682     }
4683
4684   return cp_parser_postfix_expression (parser, address_p);
4685 }
4686
4687 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4688    unary-operator, the corresponding tree code is returned.  */
4689
4690 static enum tree_code
4691 cp_parser_unary_operator (token)
4692      cp_token *token;
4693 {
4694   switch (token->type)
4695     {
4696     case CPP_MULT:
4697       return INDIRECT_REF;
4698
4699     case CPP_AND:
4700       return ADDR_EXPR;
4701
4702     case CPP_PLUS:
4703       return CONVERT_EXPR;
4704
4705     case CPP_MINUS:
4706       return NEGATE_EXPR;
4707
4708     case CPP_NOT:
4709       return TRUTH_NOT_EXPR;
4710       
4711     case CPP_COMPL:
4712       return BIT_NOT_EXPR;
4713
4714     default:
4715       return ERROR_MARK;
4716     }
4717 }
4718
4719 /* Parse a new-expression.
4720
4721      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4722      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4723
4724    Returns a representation of the expression.  */
4725
4726 static tree
4727 cp_parser_new_expression (parser)
4728      cp_parser *parser;
4729 {
4730   bool global_scope_p;
4731   tree placement;
4732   tree type;
4733   tree initializer;
4734
4735   /* Look for the optional `::' operator.  */
4736   global_scope_p 
4737     = (cp_parser_global_scope_opt (parser,
4738                                    /*current_scope_valid_p=*/false)
4739        != NULL_TREE);
4740   /* Look for the `new' operator.  */
4741   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4742   /* There's no easy way to tell a new-placement from the
4743      `( type-id )' construct.  */
4744   cp_parser_parse_tentatively (parser);
4745   /* Look for a new-placement.  */
4746   placement = cp_parser_new_placement (parser);
4747   /* If that didn't work out, there's no new-placement.  */
4748   if (!cp_parser_parse_definitely (parser))
4749     placement = NULL_TREE;
4750
4751   /* If the next token is a `(', then we have a parenthesized
4752      type-id.  */
4753   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4754     {
4755       /* Consume the `('.  */
4756       cp_lexer_consume_token (parser->lexer);
4757       /* Parse the type-id.  */
4758       type = cp_parser_type_id (parser);
4759       /* Look for the closing `)'.  */
4760       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4761     }
4762   /* Otherwise, there must be a new-type-id.  */
4763   else
4764     type = cp_parser_new_type_id (parser);
4765
4766   /* If the next token is a `(', then we have a new-initializer.  */
4767   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4768     initializer = cp_parser_new_initializer (parser);
4769   else
4770     initializer = NULL_TREE;
4771
4772   /* Create a representation of the new-expression.  */
4773   return build_new (placement, type, initializer, global_scope_p);
4774 }
4775
4776 /* Parse a new-placement.
4777
4778    new-placement:
4779      ( expression-list )
4780
4781    Returns the same representation as for an expression-list.  */
4782
4783 static tree
4784 cp_parser_new_placement (parser)
4785      cp_parser *parser;
4786 {
4787   tree expression_list;
4788
4789   /* Look for the opening `('.  */
4790   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4791     return error_mark_node;
4792   /* Parse the expression-list.  */
4793   expression_list = cp_parser_expression_list (parser);
4794   /* Look for the closing `)'.  */
4795   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4796
4797   return expression_list;
4798 }
4799
4800 /* Parse a new-type-id.
4801
4802    new-type-id:
4803      type-specifier-seq new-declarator [opt]
4804
4805    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4806    and whose TREE_VALUE is the new-declarator.  */
4807
4808 static tree
4809 cp_parser_new_type_id (parser)
4810      cp_parser *parser;
4811 {
4812   tree type_specifier_seq;
4813   tree declarator;
4814   const char *saved_message;
4815
4816   /* The type-specifier sequence must not contain type definitions.
4817      (It cannot contain declarations of new types either, but if they
4818      are not definitions we will catch that because they are not
4819      complete.)  */
4820   saved_message = parser->type_definition_forbidden_message;
4821   parser->type_definition_forbidden_message
4822     = "types may not be defined in a new-type-id";
4823   /* Parse the type-specifier-seq.  */
4824   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4825   /* Restore the old message.  */
4826   parser->type_definition_forbidden_message = saved_message;
4827   /* Parse the new-declarator.  */
4828   declarator = cp_parser_new_declarator_opt (parser);
4829
4830   return build_tree_list (type_specifier_seq, declarator);
4831 }
4832
4833 /* Parse an (optional) new-declarator.
4834
4835    new-declarator:
4836      ptr-operator new-declarator [opt]
4837      direct-new-declarator
4838
4839    Returns a representation of the declarator.  See
4840    cp_parser_declarator for the representations used.  */
4841
4842 static tree
4843 cp_parser_new_declarator_opt (parser)
4844      cp_parser *parser;
4845 {
4846   enum tree_code code;
4847   tree type;
4848   tree cv_qualifier_seq;
4849
4850   /* We don't know if there's a ptr-operator next, or not.  */
4851   cp_parser_parse_tentatively (parser);
4852   /* Look for a ptr-operator.  */
4853   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4854   /* If that worked, look for more new-declarators.  */
4855   if (cp_parser_parse_definitely (parser))
4856     {
4857       tree declarator;
4858
4859       /* Parse another optional declarator.  */
4860       declarator = cp_parser_new_declarator_opt (parser);
4861
4862       /* Create the representation of the declarator.  */
4863       if (code == INDIRECT_REF)
4864         declarator = make_pointer_declarator (cv_qualifier_seq,
4865                                               declarator);
4866       else
4867         declarator = make_reference_declarator (cv_qualifier_seq,
4868                                                 declarator);
4869
4870      /* Handle the pointer-to-member case.  */
4871      if (type)
4872        declarator = build_nt (SCOPE_REF, type, declarator);
4873
4874       return declarator;
4875     }
4876
4877   /* If the next token is a `[', there is a direct-new-declarator.  */
4878   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4879     return cp_parser_direct_new_declarator (parser);
4880
4881   return NULL_TREE;
4882 }
4883
4884 /* Parse a direct-new-declarator.
4885
4886    direct-new-declarator:
4887      [ expression ]
4888      direct-new-declarator [constant-expression]  
4889
4890    Returns an ARRAY_REF, following the same conventions as are
4891    documented for cp_parser_direct_declarator.  */
4892
4893 static tree
4894 cp_parser_direct_new_declarator (parser)
4895      cp_parser *parser;
4896 {
4897   tree declarator = NULL_TREE;
4898
4899   while (true)
4900     {
4901       tree expression;
4902
4903       /* Look for the opening `['.  */
4904       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4905       /* The first expression is not required to be constant.  */
4906       if (!declarator)
4907         {
4908           expression = cp_parser_expression (parser);
4909           /* The standard requires that the expression have integral
4910              type.  DR 74 adds enumeration types.  We believe that the
4911              real intent is that these expressions be handled like the
4912              expression in a `switch' condition, which also allows
4913              classes with a single conversion to integral or
4914              enumeration type.  */
4915           if (!processing_template_decl)
4916             {
4917               expression 
4918                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4919                                               expression,
4920                                               /*complain=*/true);
4921               if (!expression)
4922                 {
4923                   error ("expression in new-declarator must have integral or enumeration type");
4924                   expression = error_mark_node;
4925                 }
4926             }
4927         }
4928       /* But all the other expressions must be.  */
4929       else
4930         expression = cp_parser_constant_expression (parser);
4931       /* Look for the closing `]'.  */
4932       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4933
4934       /* Add this bound to the declarator.  */
4935       declarator = build_nt (ARRAY_REF, declarator, expression);
4936
4937       /* If the next token is not a `[', then there are no more
4938          bounds.  */
4939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4940         break;
4941     }
4942
4943   return declarator;
4944 }
4945
4946 /* Parse a new-initializer.
4947
4948    new-initializer:
4949      ( expression-list [opt] )
4950
4951    Returns a reprsentation of the expression-list.  If there is no
4952    expression-list, VOID_ZERO_NODE is returned.  */
4953
4954 static tree
4955 cp_parser_new_initializer (parser)
4956      cp_parser *parser;
4957 {
4958   tree expression_list;
4959
4960   /* Look for the opening parenthesis.  */
4961   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4962   /* If the next token is not a `)', then there is an
4963      expression-list.  */
4964   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4965     expression_list = cp_parser_expression_list (parser);
4966   else
4967     expression_list = void_zero_node;
4968   /* Look for the closing parenthesis.  */
4969   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4970
4971   return expression_list;
4972 }
4973
4974 /* Parse a delete-expression.
4975
4976    delete-expression:
4977      :: [opt] delete cast-expression
4978      :: [opt] delete [ ] cast-expression
4979
4980    Returns a representation of the expression.  */
4981
4982 static tree
4983 cp_parser_delete_expression (parser)
4984      cp_parser *parser;
4985 {
4986   bool global_scope_p;
4987   bool array_p;
4988   tree expression;
4989
4990   /* Look for the optional `::' operator.  */
4991   global_scope_p
4992     = (cp_parser_global_scope_opt (parser,
4993                                    /*current_scope_valid_p=*/false)
4994        != NULL_TREE);
4995   /* Look for the `delete' keyword.  */
4996   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4997   /* See if the array syntax is in use.  */
4998   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4999     {
5000       /* Consume the `[' token.  */
5001       cp_lexer_consume_token (parser->lexer);
5002       /* Look for the `]' token.  */
5003       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5004       /* Remember that this is the `[]' construct.  */
5005       array_p = true;
5006     }
5007   else
5008     array_p = false;
5009
5010   /* Parse the cast-expression.  */
5011   expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5012
5013   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5014 }
5015
5016 /* Parse a cast-expression.
5017
5018    cast-expression:
5019      unary-expression
5020      ( type-id ) cast-expression
5021
5022    Returns a representation of the expression.  */
5023
5024 static tree
5025 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5026 {
5027   /* If it's a `(', then we might be looking at a cast.  */
5028   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5029     {
5030       tree type = NULL_TREE;
5031       tree expr = NULL_TREE;
5032       bool compound_literal_p;
5033       const char *saved_message;
5034
5035       /* There's no way to know yet whether or not this is a cast.
5036          For example, `(int (3))' is a unary-expression, while `(int)
5037          3' is a cast.  So, we resort to parsing tentatively.  */
5038       cp_parser_parse_tentatively (parser);
5039       /* Types may not be defined in a cast.  */
5040       saved_message = parser->type_definition_forbidden_message;
5041       parser->type_definition_forbidden_message
5042         = "types may not be defined in casts";
5043       /* Consume the `('.  */
5044       cp_lexer_consume_token (parser->lexer);
5045       /* A very tricky bit is that `(struct S) { 3 }' is a
5046          compound-literal (which we permit in C++ as an extension).
5047          But, that construct is not a cast-expression -- it is a
5048          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5049          is legal; if the compound-literal were a cast-expression,
5050          you'd need an extra set of parentheses.)  But, if we parse
5051          the type-id, and it happens to be a class-specifier, then we
5052          will commit to the parse at that point, because we cannot
5053          undo the action that is done when creating a new class.  So,
5054          then we cannot back up and do a postfix-expression.  
5055
5056          Therefore, we scan ahead to the closing `)', and check to see
5057          if the token after the `)' is a `{'.  If so, we are not
5058          looking at a cast-expression.  
5059
5060          Save tokens so that we can put them back.  */
5061       cp_lexer_save_tokens (parser->lexer);
5062       /* Skip tokens until the next token is a closing parenthesis.
5063          If we find the closing `)', and the next token is a `{', then
5064          we are looking at a compound-literal.  */
5065       compound_literal_p 
5066         = (cp_parser_skip_to_closing_parenthesis (parser)
5067            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5068       /* Roll back the tokens we skipped.  */
5069       cp_lexer_rollback_tokens (parser->lexer);
5070       /* If we were looking at a compound-literal, simulate an error
5071          so that the call to cp_parser_parse_definitely below will
5072          fail.  */
5073       if (compound_literal_p)
5074         cp_parser_simulate_error (parser);
5075       else
5076         {
5077           /* Look for the type-id.  */
5078           type = cp_parser_type_id (parser);
5079           /* Look for the closing `)'.  */
5080           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5081         }
5082
5083       /* Restore the saved message.  */
5084       parser->type_definition_forbidden_message = saved_message;
5085
5086       /* If all went well, this is a cast.  */
5087       if (cp_parser_parse_definitely (parser))
5088         {
5089           /* Parse the dependent expression.  */
5090           expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5091           /* Warn about old-style casts, if so requested.  */
5092           if (warn_old_style_cast 
5093               && !in_system_header 
5094               && !VOID_TYPE_P (type) 
5095               && current_lang_name != lang_name_c)
5096             warning ("use of old-style cast");
5097           /* Perform the cast.  */
5098           expr = build_c_cast (type, expr);
5099         }
5100
5101       if (expr)
5102         return expr;
5103     }
5104
5105   /* If we get here, then it's not a cast, so it must be a
5106      unary-expression.  */
5107   return cp_parser_unary_expression (parser, address_p);
5108 }
5109
5110 /* Parse a pm-expression.
5111
5112    pm-expression:
5113      cast-expression
5114      pm-expression .* cast-expression
5115      pm-expression ->* cast-expression
5116
5117      Returns a representation of the expression.  */
5118
5119 static tree
5120 cp_parser_pm_expression (parser)
5121      cp_parser *parser;
5122 {
5123   tree cast_expr;
5124   tree pm_expr;
5125
5126   /* Parse the cast-expresion.  */
5127   cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5128   pm_expr = cast_expr;
5129   /* Now look for pointer-to-member operators.  */
5130   while (true)
5131     {
5132       cp_token *token;
5133       enum cpp_ttype token_type;
5134
5135       /* Peek at the next token.  */
5136       token = cp_lexer_peek_token (parser->lexer);
5137       token_type = token->type;
5138       /* If it's not `.*' or `->*' there's no pointer-to-member
5139          operation.  */
5140       if (token_type != CPP_DOT_STAR 
5141           && token_type != CPP_DEREF_STAR)
5142         break;
5143
5144       /* Consume the token.  */
5145       cp_lexer_consume_token (parser->lexer);
5146
5147       /* Parse another cast-expression.  */
5148       cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5149
5150       /* Build the representation of the pointer-to-member 
5151          operation.  */
5152       if (token_type == CPP_DEREF_STAR)
5153         pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5154       else
5155         pm_expr = build_m_component_ref (pm_expr, cast_expr);
5156     }
5157
5158   return pm_expr;
5159 }
5160
5161 /* Parse a multiplicative-expression.
5162
5163    mulitplicative-expression:
5164      pm-expression
5165      multiplicative-expression * pm-expression
5166      multiplicative-expression / pm-expression
5167      multiplicative-expression % pm-expression
5168
5169    Returns a representation of the expression.  */
5170
5171 static tree
5172 cp_parser_multiplicative_expression (parser)
5173      cp_parser *parser;
5174 {
5175   static cp_parser_token_tree_map map = {
5176     { CPP_MULT, MULT_EXPR },
5177     { CPP_DIV, TRUNC_DIV_EXPR },
5178     { CPP_MOD, TRUNC_MOD_EXPR },
5179     { CPP_EOF, ERROR_MARK }
5180   };
5181
5182   return cp_parser_binary_expression (parser,
5183                                       map,
5184                                       cp_parser_pm_expression);
5185 }
5186
5187 /* Parse an additive-expression.
5188
5189    additive-expression:
5190      multiplicative-expression
5191      additive-expression + multiplicative-expression
5192      additive-expression - multiplicative-expression
5193
5194    Returns a representation of the expression.  */
5195
5196 static tree
5197 cp_parser_additive_expression (parser)
5198      cp_parser *parser;
5199 {
5200   static cp_parser_token_tree_map map = {
5201     { CPP_PLUS, PLUS_EXPR },
5202     { CPP_MINUS, MINUS_EXPR },
5203     { CPP_EOF, ERROR_MARK }
5204   };
5205
5206   return cp_parser_binary_expression (parser,
5207                                       map,
5208                                       cp_parser_multiplicative_expression);
5209 }
5210
5211 /* Parse a shift-expression.
5212
5213    shift-expression:
5214      additive-expression
5215      shift-expression << additive-expression
5216      shift-expression >> additive-expression
5217
5218    Returns a representation of the expression.  */
5219
5220 static tree
5221 cp_parser_shift_expression (parser)
5222      cp_parser *parser;
5223 {
5224   static cp_parser_token_tree_map map = {
5225     { CPP_LSHIFT, LSHIFT_EXPR },
5226     { CPP_RSHIFT, RSHIFT_EXPR },
5227     { CPP_EOF, ERROR_MARK }
5228   };
5229
5230   return cp_parser_binary_expression (parser,
5231                                       map,
5232                                       cp_parser_additive_expression);
5233 }
5234
5235 /* Parse a relational-expression.
5236
5237    relational-expression:
5238      shift-expression
5239      relational-expression < shift-expression
5240      relational-expression > shift-expression
5241      relational-expression <= shift-expression
5242      relational-expression >= shift-expression
5243
5244    GNU Extension:
5245
5246    relational-expression:
5247      relational-expression <? shift-expression
5248      relational-expression >? shift-expression
5249
5250    Returns a representation of the expression.  */
5251
5252 static tree
5253 cp_parser_relational_expression (parser)
5254      cp_parser *parser;
5255 {
5256   static cp_parser_token_tree_map map = {
5257     { CPP_LESS, LT_EXPR },
5258     { CPP_GREATER, GT_EXPR },
5259     { CPP_LESS_EQ, LE_EXPR },
5260     { CPP_GREATER_EQ, GE_EXPR },
5261     { CPP_MIN, MIN_EXPR },
5262     { CPP_MAX, MAX_EXPR },
5263     { CPP_EOF, ERROR_MARK }
5264   };
5265
5266   return cp_parser_binary_expression (parser,
5267                                       map,
5268                                       cp_parser_shift_expression);
5269 }
5270
5271 /* Parse an equality-expression.
5272
5273    equality-expression:
5274      relational-expression
5275      equality-expression == relational-expression
5276      equality-expression != relational-expression
5277
5278    Returns a representation of the expression.  */
5279
5280 static tree
5281 cp_parser_equality_expression (parser)
5282      cp_parser *parser;
5283 {
5284   static cp_parser_token_tree_map map = {
5285     { CPP_EQ_EQ, EQ_EXPR },
5286     { CPP_NOT_EQ, NE_EXPR },
5287     { CPP_EOF, ERROR_MARK }
5288   };
5289
5290   return cp_parser_binary_expression (parser,
5291                                       map,
5292                                       cp_parser_relational_expression);
5293 }
5294
5295 /* Parse an and-expression.
5296
5297    and-expression:
5298      equality-expression
5299      and-expression & equality-expression
5300
5301    Returns a representation of the expression.  */
5302
5303 static tree
5304 cp_parser_and_expression (parser)
5305      cp_parser *parser;
5306 {
5307   static cp_parser_token_tree_map map = {
5308     { CPP_AND, BIT_AND_EXPR },
5309     { CPP_EOF, ERROR_MARK }
5310   };
5311
5312   return cp_parser_binary_expression (parser,
5313                                       map,
5314                                       cp_parser_equality_expression);
5315 }
5316
5317 /* Parse an exclusive-or-expression.
5318
5319    exclusive-or-expression:
5320      and-expression
5321      exclusive-or-expression ^ and-expression
5322
5323    Returns a representation of the expression.  */
5324
5325 static tree
5326 cp_parser_exclusive_or_expression (parser)
5327      cp_parser *parser;
5328 {
5329   static cp_parser_token_tree_map map = {
5330     { CPP_XOR, BIT_XOR_EXPR },
5331     { CPP_EOF, ERROR_MARK }
5332   };
5333
5334   return cp_parser_binary_expression (parser,
5335                                       map,
5336                                       cp_parser_and_expression);
5337 }
5338
5339
5340 /* Parse an inclusive-or-expression.
5341
5342    inclusive-or-expression:
5343      exclusive-or-expression
5344      inclusive-or-expression | exclusive-or-expression
5345
5346    Returns a representation of the expression.  */
5347
5348 static tree
5349 cp_parser_inclusive_or_expression (parser)
5350      cp_parser *parser;
5351 {
5352   static cp_parser_token_tree_map map = {
5353     { CPP_OR, BIT_IOR_EXPR },
5354     { CPP_EOF, ERROR_MARK }
5355   };
5356
5357   return cp_parser_binary_expression (parser,
5358                                       map,
5359                                       cp_parser_exclusive_or_expression);
5360 }
5361
5362 /* Parse a logical-and-expression.
5363
5364    logical-and-expression:
5365      inclusive-or-expression
5366      logical-and-expression && inclusive-or-expression
5367
5368    Returns a representation of the expression.  */
5369
5370 static tree
5371 cp_parser_logical_and_expression (parser)
5372      cp_parser *parser;
5373 {
5374   static cp_parser_token_tree_map map = {
5375     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5376     { CPP_EOF, ERROR_MARK }
5377   };
5378
5379   return cp_parser_binary_expression (parser,
5380                                       map,
5381                                       cp_parser_inclusive_or_expression);
5382 }
5383
5384 /* Parse a logical-or-expression.
5385
5386    logical-or-expression:
5387      logical-and-expresion
5388      logical-or-expression || logical-and-expression
5389
5390    Returns a representation of the expression.  */
5391
5392 static tree
5393 cp_parser_logical_or_expression (parser)
5394      cp_parser *parser;
5395 {
5396   static cp_parser_token_tree_map map = {
5397     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5398     { CPP_EOF, ERROR_MARK }
5399   };
5400
5401   return cp_parser_binary_expression (parser,
5402                                       map,
5403                                       cp_parser_logical_and_expression);
5404 }
5405
5406 /* Parse a conditional-expression.
5407
5408    conditional-expression:
5409      logical-or-expression
5410      logical-or-expression ? expression : assignment-expression
5411      
5412    GNU Extensions:
5413    
5414    conditional-expression:
5415      logical-or-expression ?  : assignment-expression
5416
5417    Returns a representation of the expression.  */
5418
5419 static tree
5420 cp_parser_conditional_expression (parser)
5421      cp_parser *parser;
5422 {
5423   tree logical_or_expr;
5424
5425   /* Parse the logical-or-expression.  */
5426   logical_or_expr = cp_parser_logical_or_expression (parser);
5427   /* If the next token is a `?', then we have a real conditional
5428      expression.  */
5429   if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5430     return cp_parser_question_colon_clause (parser, logical_or_expr);
5431   /* Otherwise, the value is simply the logical-or-expression.  */
5432   else
5433     return logical_or_expr;
5434 }
5435
5436 /* Parse the `? expression : assignment-expression' part of a
5437    conditional-expression.  The LOGICAL_OR_EXPR is the
5438    logical-or-expression that started the conditional-expression.
5439    Returns a representation of the entire conditional-expression.
5440
5441    This routine exists only so that it can be shared between
5442    cp_parser_conditional_expression and
5443    cp_parser_assignment_expression.
5444
5445      ? expression : assignment-expression
5446    
5447    GNU Extensions:
5448    
5449      ? : assignment-expression */
5450
5451 static tree
5452 cp_parser_question_colon_clause (parser, logical_or_expr)
5453      cp_parser *parser;
5454      tree logical_or_expr;
5455 {
5456   tree expr;
5457   tree assignment_expr;
5458
5459   /* Consume the `?' token.  */
5460   cp_lexer_consume_token (parser->lexer);
5461   if (cp_parser_allow_gnu_extensions_p (parser)
5462       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5463     /* Implicit true clause.  */
5464     expr = NULL_TREE;
5465   else
5466     /* Parse the expression.  */
5467     expr = cp_parser_expression (parser);
5468   
5469   /* The next token should be a `:'.  */
5470   cp_parser_require (parser, CPP_COLON, "`:'");
5471   /* Parse the assignment-expression.  */
5472   assignment_expr = cp_parser_assignment_expression (parser);
5473
5474   /* Build the conditional-expression.  */
5475   return build_x_conditional_expr (logical_or_expr,
5476                                    expr,
5477                                    assignment_expr);
5478 }
5479
5480 /* Parse an assignment-expression.
5481
5482    assignment-expression:
5483      conditional-expression
5484      logical-or-expression assignment-operator assignment_expression
5485      throw-expression
5486
5487    Returns a representation for the expression.  */
5488
5489 static tree
5490 cp_parser_assignment_expression (parser)
5491      cp_parser *parser;
5492 {
5493   tree expr;
5494
5495   /* If the next token is the `throw' keyword, then we're looking at
5496      a throw-expression.  */
5497   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5498     expr = cp_parser_throw_expression (parser);
5499   /* Otherwise, it must be that we are looking at a
5500      logical-or-expression.  */
5501   else
5502     {
5503       /* Parse the logical-or-expression.  */
5504       expr = cp_parser_logical_or_expression (parser);
5505       /* If the next token is a `?' then we're actually looking at a
5506          conditional-expression.  */
5507       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5508         return cp_parser_question_colon_clause (parser, expr);
5509       else 
5510         {
5511           enum tree_code assignment_operator;
5512
5513           /* If it's an assignment-operator, we're using the second
5514              production.  */
5515           assignment_operator 
5516             = cp_parser_assignment_operator_opt (parser);
5517           if (assignment_operator != ERROR_MARK)
5518             {
5519               tree rhs;
5520
5521               /* Parse the right-hand side of the assignment.  */
5522               rhs = cp_parser_assignment_expression (parser);
5523               /* Build the asignment expression.  */
5524               expr = build_x_modify_expr (expr, 
5525                                           assignment_operator, 
5526                                           rhs);
5527             }
5528         }
5529     }
5530
5531   return expr;
5532 }
5533
5534 /* Parse an (optional) assignment-operator.
5535
5536    assignment-operator: one of 
5537      = *= /= %= += -= >>= <<= &= ^= |=  
5538
5539    GNU Extension:
5540    
5541    assignment-operator: one of
5542      <?= >?=
5543
5544    If the next token is an assignment operator, the corresponding tree
5545    code is returned, and the token is consumed.  For example, for
5546    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5547    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5548    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5549    operator, ERROR_MARK is returned.  */
5550
5551 static enum tree_code
5552 cp_parser_assignment_operator_opt (parser)
5553      cp_parser *parser;
5554 {
5555   enum tree_code op;
5556   cp_token *token;
5557
5558   /* Peek at the next toen.  */
5559   token = cp_lexer_peek_token (parser->lexer);
5560
5561   switch (token->type)
5562     {
5563     case CPP_EQ:
5564       op = NOP_EXPR;
5565       break;
5566
5567     case CPP_MULT_EQ:
5568       op = MULT_EXPR;
5569       break;
5570
5571     case CPP_DIV_EQ:
5572       op = TRUNC_DIV_EXPR;
5573       break;
5574
5575     case CPP_MOD_EQ:
5576       op = TRUNC_MOD_EXPR;
5577       break;
5578
5579     case CPP_PLUS_EQ:
5580       op = PLUS_EXPR;
5581       break;
5582
5583     case CPP_MINUS_EQ:
5584       op = MINUS_EXPR;
5585       break;
5586
5587     case CPP_RSHIFT_EQ:
5588       op = RSHIFT_EXPR;
5589       break;
5590
5591     case CPP_LSHIFT_EQ:
5592       op = LSHIFT_EXPR;
5593       break;
5594
5595     case CPP_AND_EQ:
5596       op = BIT_AND_EXPR;
5597       break;
5598
5599     case CPP_XOR_EQ:
5600       op = BIT_XOR_EXPR;
5601       break;
5602
5603     case CPP_OR_EQ:
5604       op = BIT_IOR_EXPR;
5605       break;
5606
5607     case CPP_MIN_EQ:
5608       op = MIN_EXPR;
5609       break;
5610
5611     case CPP_MAX_EQ:
5612       op = MAX_EXPR;
5613       break;
5614
5615     default: 
5616       /* Nothing else is an assignment operator.  */
5617       op = ERROR_MARK;
5618     }
5619
5620   /* If it was an assignment operator, consume it.  */
5621   if (op != ERROR_MARK)
5622     cp_lexer_consume_token (parser->lexer);
5623
5624   return op;
5625 }
5626
5627 /* Parse an expression.
5628
5629    expression:
5630      assignment-expression
5631      expression , assignment-expression
5632
5633    Returns a representation of the expression.  */
5634
5635 static tree
5636 cp_parser_expression (parser)
5637      cp_parser *parser;
5638 {
5639   tree expression = NULL_TREE;
5640   bool saw_comma_p = false;
5641
5642   while (true)
5643     {
5644       tree assignment_expression;
5645
5646       /* Parse the next assignment-expression.  */
5647       assignment_expression 
5648         = cp_parser_assignment_expression (parser);
5649       /* If this is the first assignment-expression, we can just
5650          save it away.  */
5651       if (!expression)
5652         expression = assignment_expression;
5653       /* Otherwise, chain the expressions together.  It is unclear why
5654          we do not simply build COMPOUND_EXPRs as we go.  */
5655       else
5656         expression = tree_cons (NULL_TREE, 
5657                                 assignment_expression,
5658                                 expression);
5659       /* If the next token is not a comma, then we are done with the
5660          expression.  */
5661       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5662         break;
5663       /* Consume the `,'.  */
5664       cp_lexer_consume_token (parser->lexer);
5665       /* The first time we see a `,', we must take special action
5666          because the representation used for a single expression is
5667          different from that used for a list containing the single
5668          expression.  */
5669       if (!saw_comma_p)
5670         {
5671           /* Remember that this expression has a `,' in it.  */
5672           saw_comma_p = true;
5673           /* Turn the EXPRESSION into a TREE_LIST so that we can link
5674              additional expressions to it.  */
5675           expression = build_tree_list (NULL_TREE, expression);
5676         }
5677     }
5678
5679   /* Build a COMPOUND_EXPR to represent the entire expression, if
5680      necessary.  We built up the list in reverse order, so we must
5681      straighten it out here.  */
5682   if (saw_comma_p)
5683     expression = build_x_compound_expr (nreverse (expression));
5684
5685   return expression;
5686 }
5687
5688 /* Parse a constant-expression. 
5689
5690    constant-expression:
5691      conditional-expression  */
5692
5693 static tree
5694 cp_parser_constant_expression (parser)
5695      cp_parser *parser;
5696 {
5697   bool saved_constant_expression_p;
5698   tree expression;
5699
5700   /* It might seem that we could simply parse the
5701      conditional-expression, and then check to see if it were
5702      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5703      one that the compiler can figure out is constant, possibly after
5704      doing some simplifications or optimizations.  The standard has a
5705      precise definition of constant-expression, and we must honor
5706      that, even though it is somewhat more restrictive.
5707
5708      For example:
5709
5710        int i[(2, 3)];
5711
5712      is not a legal declaration, because `(2, 3)' is not a
5713      constant-expression.  The `,' operator is forbidden in a
5714      constant-expression.  However, GCC's constant-folding machinery
5715      will fold this operation to an INTEGER_CST for `3'.  */
5716
5717   /* Save the old setting of CONSTANT_EXPRESSION_P.  */
5718   saved_constant_expression_p = parser->constant_expression_p;
5719   /* We are now parsing a constant-expression.  */
5720   parser->constant_expression_p = true;
5721   /* Parse the conditional-expression.  */
5722   expression = cp_parser_conditional_expression (parser);
5723   /* Restore the old setting of CONSTANT_EXPRESSION_P.  */
5724   parser->constant_expression_p = saved_constant_expression_p;
5725
5726   return expression;
5727 }
5728
5729 /* Statements [gram.stmt.stmt]  */
5730
5731 /* Parse a statement.  
5732
5733    statement:
5734      labeled-statement
5735      expression-statement
5736      compound-statement
5737      selection-statement
5738      iteration-statement
5739      jump-statement
5740      declaration-statement
5741      try-block  */
5742
5743 static void
5744 cp_parser_statement (parser)
5745      cp_parser *parser;
5746 {
5747   tree statement;
5748   cp_token *token;
5749   int statement_line_number;
5750
5751   /* There is no statement yet.  */
5752   statement = NULL_TREE;
5753   /* Peek at the next token.  */
5754   token = cp_lexer_peek_token (parser->lexer);
5755   /* Remember the line number of the first token in the statement.  */
5756   statement_line_number = token->line_number;
5757   /* If this is a keyword, then that will often determine what kind of
5758      statement we have.  */
5759   if (token->type == CPP_KEYWORD)
5760     {
5761       enum rid keyword = token->keyword;
5762
5763       switch (keyword)
5764         {
5765         case RID_CASE:
5766         case RID_DEFAULT:
5767           statement = cp_parser_labeled_statement (parser);
5768           break;
5769
5770         case RID_IF:
5771         case RID_SWITCH:
5772           statement = cp_parser_selection_statement (parser);
5773           break;
5774
5775         case RID_WHILE:
5776         case RID_DO:
5777         case RID_FOR:
5778           statement = cp_parser_iteration_statement (parser);
5779           break;
5780
5781         case RID_BREAK:
5782         case RID_CONTINUE:
5783         case RID_RETURN:
5784         case RID_GOTO:
5785           statement = cp_parser_jump_statement (parser);
5786           break;
5787
5788         case RID_TRY:
5789           statement = cp_parser_try_block (parser);
5790           break;
5791
5792         default:
5793           /* It might be a keyword like `int' that can start a
5794              declaration-statement.  */
5795           break;
5796         }
5797     }
5798   else if (token->type == CPP_NAME)
5799     {
5800       /* If the next token is a `:', then we are looking at a
5801          labeled-statement.  */
5802       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5803       if (token->type == CPP_COLON)
5804         statement = cp_parser_labeled_statement (parser);
5805     }
5806   /* Anything that starts with a `{' must be a compound-statement.  */
5807   else if (token->type == CPP_OPEN_BRACE)
5808     statement = cp_parser_compound_statement (parser);
5809
5810   /* Everything else must be a declaration-statement or an
5811      expression-statement.  Try for the declaration-statement 
5812      first, unless we are looking at a `;', in which case we know that
5813      we have an expression-statement.  */
5814   if (!statement)
5815     {
5816       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5817         {
5818           cp_parser_parse_tentatively (parser);
5819           /* Try to parse the declaration-statement.  */
5820           cp_parser_declaration_statement (parser);
5821           /* If that worked, we're done.  */
5822           if (cp_parser_parse_definitely (parser))
5823             return;
5824         }
5825       /* Look for an expression-statement instead.  */
5826       statement = cp_parser_expression_statement (parser);
5827     }
5828
5829   /* Set the line number for the statement.  */
5830   if (statement && statement_code_p (TREE_CODE (statement)))
5831     STMT_LINENO (statement) = statement_line_number;
5832 }
5833
5834 /* Parse a labeled-statement.
5835
5836    labeled-statement:
5837      identifier : statement
5838      case constant-expression : statement
5839      default : statement  
5840
5841    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5842    an ordinary label, returns a LABEL_STMT.  */
5843
5844 static tree
5845 cp_parser_labeled_statement (parser)
5846      cp_parser *parser;
5847 {
5848   cp_token *token;
5849   tree statement = NULL_TREE;
5850
5851   /* The next token should be an identifier.  */
5852   token = cp_lexer_peek_token (parser->lexer);
5853   if (token->type != CPP_NAME
5854       && token->type != CPP_KEYWORD)
5855     {
5856       cp_parser_error (parser, "expected labeled-statement");
5857       return error_mark_node;
5858     }
5859
5860   switch (token->keyword)
5861     {
5862     case RID_CASE:
5863       {
5864         tree expr;
5865
5866         /* Consume the `case' token.  */
5867         cp_lexer_consume_token (parser->lexer);
5868         /* Parse the constant-expression.  */
5869         expr = cp_parser_constant_expression (parser);
5870         /* Create the label.  */
5871         statement = finish_case_label (expr, NULL_TREE);
5872       }
5873       break;
5874
5875     case RID_DEFAULT:
5876       /* Consume the `default' token.  */
5877       cp_lexer_consume_token (parser->lexer);
5878       /* Create the label.  */
5879       statement = finish_case_label (NULL_TREE, NULL_TREE);
5880       break;
5881
5882     default:
5883       /* Anything else must be an ordinary label.  */
5884       statement = finish_label_stmt (cp_parser_identifier (parser));
5885       break;
5886     }
5887
5888   /* Require the `:' token.  */
5889   cp_parser_require (parser, CPP_COLON, "`:'");
5890   /* Parse the labeled statement.  */
5891   cp_parser_statement (parser);
5892
5893   /* Return the label, in the case of a `case' or `default' label.  */
5894   return statement;
5895 }
5896
5897 /* Parse an expression-statement.
5898
5899    expression-statement:
5900      expression [opt] ;
5901
5902    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5903    statement consists of nothing more than an `;'.  */
5904
5905 static tree
5906 cp_parser_expression_statement (parser)
5907      cp_parser *parser;
5908 {
5909   tree statement;
5910
5911   /* If the next token is not a `;', then there is an expression to parse.  */
5912   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5913     statement = finish_expr_stmt (cp_parser_expression (parser));
5914   /* Otherwise, we do not even bother to build an EXPR_STMT.  */
5915   else
5916     {
5917       finish_stmt ();
5918       statement = NULL_TREE;
5919     }
5920   /* Consume the final `;'.  */
5921   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5922     {
5923       /* If there is additional (erroneous) input, skip to the end of
5924          the statement.  */
5925       cp_parser_skip_to_end_of_statement (parser);
5926       /* If the next token is now a `;', consume it.  */
5927       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5928         cp_lexer_consume_token (parser->lexer);
5929     }
5930
5931   return statement;
5932 }
5933
5934 /* Parse a compound-statement.
5935
5936    compound-statement:
5937      { statement-seq [opt] }
5938      
5939    Returns a COMPOUND_STMT representing the statement.  */
5940
5941 static tree
5942 cp_parser_compound_statement (cp_parser *parser)
5943 {
5944   tree compound_stmt;
5945
5946   /* Consume the `{'.  */
5947   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5948     return error_mark_node;
5949   /* Begin the compound-statement.  */
5950   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5951   /* Parse an (optional) statement-seq.  */
5952   cp_parser_statement_seq_opt (parser);
5953   /* Finish the compound-statement.  */
5954   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5955   /* Consume the `}'.  */
5956   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5957
5958   return compound_stmt;
5959 }
5960
5961 /* Parse an (optional) statement-seq.
5962
5963    statement-seq:
5964      statement
5965      statement-seq [opt] statement  */
5966
5967 static void
5968 cp_parser_statement_seq_opt (parser)
5969      cp_parser *parser;
5970 {
5971   /* Scan statements until there aren't any more.  */
5972   while (true)
5973     {
5974       /* If we're looking at a `}', then we've run out of statements.  */
5975       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5976           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5977         break;
5978
5979       /* Parse the statement.  */
5980       cp_parser_statement (parser);
5981     }
5982 }
5983
5984 /* Parse a selection-statement.
5985
5986    selection-statement:
5987      if ( condition ) statement
5988      if ( condition ) statement else statement
5989      switch ( condition ) statement  
5990
5991    Returns the new IF_STMT or SWITCH_STMT.  */
5992
5993 static tree
5994 cp_parser_selection_statement (parser)
5995      cp_parser *parser;
5996 {
5997   cp_token *token;
5998   enum rid keyword;
5999
6000   /* Peek at the next token.  */
6001   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6002
6003   /* See what kind of keyword it is.  */
6004   keyword = token->keyword;
6005   switch (keyword)
6006     {
6007     case RID_IF:
6008     case RID_SWITCH:
6009       {
6010         tree statement;
6011         tree condition;
6012
6013         /* Look for the `('.  */
6014         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6015           {
6016             cp_parser_skip_to_end_of_statement (parser);
6017             return error_mark_node;
6018           }
6019
6020         /* Begin the selection-statement.  */
6021         if (keyword == RID_IF)
6022           statement = begin_if_stmt ();
6023         else
6024           statement = begin_switch_stmt ();
6025
6026         /* Parse the condition.  */
6027         condition = cp_parser_condition (parser);
6028         /* Look for the `)'.  */
6029         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6030           cp_parser_skip_to_closing_parenthesis (parser);
6031
6032         if (keyword == RID_IF)
6033           {
6034             tree then_stmt;
6035
6036             /* Add the condition.  */
6037             finish_if_stmt_cond (condition, statement);
6038
6039             /* Parse the then-clause.  */
6040             then_stmt = cp_parser_implicitly_scoped_statement (parser);
6041             finish_then_clause (statement);
6042
6043             /* If the next token is `else', parse the else-clause.  */
6044             if (cp_lexer_next_token_is_keyword (parser->lexer,
6045                                                 RID_ELSE))
6046               {
6047                 tree else_stmt;
6048
6049                 /* Consume the `else' keyword.  */
6050                 cp_lexer_consume_token (parser->lexer);
6051                 /* Parse the else-clause.  */
6052                 else_stmt 
6053                   = cp_parser_implicitly_scoped_statement (parser);
6054                 finish_else_clause (statement);
6055               }
6056
6057             /* Now we're all done with the if-statement.  */
6058             finish_if_stmt ();
6059           }
6060         else
6061           {
6062             tree body;
6063
6064             /* Add the condition.  */
6065             finish_switch_cond (condition, statement);
6066
6067             /* Parse the body of the switch-statement.  */
6068             body = cp_parser_implicitly_scoped_statement (parser);
6069
6070             /* Now we're all done with the switch-statement.  */
6071             finish_switch_stmt (statement);
6072           }
6073
6074         return statement;
6075       }
6076       break;
6077
6078     default:
6079       cp_parser_error (parser, "expected selection-statement");
6080       return error_mark_node;
6081     }
6082 }
6083
6084 /* Parse a condition. 
6085
6086    condition:
6087      expression
6088      type-specifier-seq declarator = assignment-expression  
6089
6090    GNU Extension:
6091    
6092    condition:
6093      type-specifier-seq declarator asm-specification [opt] 
6094        attributes [opt] = assignment-expression
6095  
6096    Returns the expression that should be tested.  */
6097
6098 static tree
6099 cp_parser_condition (parser)
6100      cp_parser *parser;
6101 {
6102   tree type_specifiers;
6103   const char *saved_message;
6104
6105   /* Try the declaration first.  */
6106   cp_parser_parse_tentatively (parser);
6107   /* New types are not allowed in the type-specifier-seq for a
6108      condition.  */
6109   saved_message = parser->type_definition_forbidden_message;
6110   parser->type_definition_forbidden_message
6111     = "types may not be defined in conditions";
6112   /* Parse the type-specifier-seq.  */
6113   type_specifiers = cp_parser_type_specifier_seq (parser);
6114   /* Restore the saved message.  */
6115   parser->type_definition_forbidden_message = saved_message;
6116   /* If all is well, we might be looking at a declaration.  */
6117   if (!cp_parser_error_occurred (parser))
6118     {
6119       tree decl;
6120       tree asm_specification;
6121       tree attributes;
6122       tree declarator;
6123       tree initializer = NULL_TREE;
6124       
6125       /* Parse the declarator.  */
6126       declarator = cp_parser_declarator (parser, 
6127                                          /*abstract_p=*/false,
6128                                          /*ctor_dtor_or_conv_p=*/NULL);
6129       /* Parse the attributes.  */
6130       attributes = cp_parser_attributes_opt (parser);
6131       /* Parse the asm-specification.  */
6132       asm_specification = cp_parser_asm_specification_opt (parser);
6133       /* If the next token is not an `=', then we might still be
6134          looking at an expression.  For example:
6135          
6136            if (A(a).x)
6137           
6138          looks like a decl-specifier-seq and a declarator -- but then
6139          there is no `=', so this is an expression.  */
6140       cp_parser_require (parser, CPP_EQ, "`='");
6141       /* If we did see an `=', then we are looking at a declaration
6142          for sure.  */
6143       if (cp_parser_parse_definitely (parser))
6144         {
6145           /* Create the declaration.  */
6146           decl = start_decl (declarator, type_specifiers, 
6147                              /*initialized_p=*/true,
6148                              attributes, /*prefix_attributes=*/NULL_TREE);
6149           /* Parse the assignment-expression.  */
6150           initializer = cp_parser_assignment_expression (parser);
6151           
6152           /* Process the initializer.  */
6153           cp_finish_decl (decl, 
6154                           initializer, 
6155                           asm_specification, 
6156                           LOOKUP_ONLYCONVERTING);
6157           
6158           return convert_from_reference (decl);
6159         }
6160     }
6161   /* If we didn't even get past the declarator successfully, we are
6162      definitely not looking at a declaration.  */
6163   else
6164     cp_parser_abort_tentative_parse (parser);
6165
6166   /* Otherwise, we are looking at an expression.  */
6167   return cp_parser_expression (parser);
6168 }
6169
6170 /* Parse an iteration-statement.
6171
6172    iteration-statement:
6173      while ( condition ) statement
6174      do statement while ( expression ) ;
6175      for ( for-init-statement condition [opt] ; expression [opt] )
6176        statement
6177
6178    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6179
6180 static tree
6181 cp_parser_iteration_statement (parser)
6182      cp_parser *parser;
6183 {
6184   cp_token *token;
6185   enum rid keyword;
6186   tree statement;
6187
6188   /* Peek at the next token.  */
6189   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6190   if (!token)
6191     return error_mark_node;
6192
6193   /* See what kind of keyword it is.  */
6194   keyword = token->keyword;
6195   switch (keyword)
6196     {
6197     case RID_WHILE:
6198       {
6199         tree condition;
6200
6201         /* Begin the while-statement.  */
6202         statement = begin_while_stmt ();
6203         /* Look for the `('.  */
6204         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6205         /* Parse the condition.  */
6206         condition = cp_parser_condition (parser);
6207         finish_while_stmt_cond (condition, statement);
6208         /* Look for the `)'.  */
6209         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6210         /* Parse the dependent statement.  */
6211         cp_parser_already_scoped_statement (parser);
6212         /* We're done with the while-statement.  */
6213         finish_while_stmt (statement);
6214       }
6215       break;
6216
6217     case RID_DO:
6218       {
6219         tree expression;
6220
6221         /* Begin the do-statement.  */
6222         statement = begin_do_stmt ();
6223         /* Parse the body of the do-statement.  */
6224         cp_parser_implicitly_scoped_statement (parser);
6225         finish_do_body (statement);
6226         /* Look for the `while' keyword.  */
6227         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6228         /* Look for the `('.  */
6229         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6230         /* Parse the expression.  */
6231         expression = cp_parser_expression (parser);
6232         /* We're done with the do-statement.  */
6233         finish_do_stmt (expression, statement);
6234         /* Look for the `)'.  */
6235         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6236         /* Look for the `;'.  */
6237         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6238       }
6239       break;
6240
6241     case RID_FOR:
6242       {
6243         tree condition = NULL_TREE;
6244         tree expression = NULL_TREE;
6245
6246         /* Begin the for-statement.  */
6247         statement = begin_for_stmt ();
6248         /* Look for the `('.  */
6249         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6250         /* Parse the initialization.  */
6251         cp_parser_for_init_statement (parser);
6252         finish_for_init_stmt (statement);
6253
6254         /* If there's a condition, process it.  */
6255         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6256           condition = cp_parser_condition (parser);
6257         finish_for_cond (condition, statement);
6258         /* Look for the `;'.  */
6259         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6260
6261         /* If there's an expression, process it.  */
6262         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6263           expression = cp_parser_expression (parser);
6264         finish_for_expr (expression, statement);
6265         /* Look for the `)'.  */
6266         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6267
6268         /* Parse the body of the for-statement.  */
6269         cp_parser_already_scoped_statement (parser);
6270
6271         /* We're done with the for-statement.  */
6272         finish_for_stmt (statement);
6273       }
6274       break;
6275
6276     default:
6277       cp_parser_error (parser, "expected iteration-statement");
6278       statement = error_mark_node;
6279       break;
6280     }
6281
6282   return statement;
6283 }
6284
6285 /* Parse a for-init-statement.
6286
6287    for-init-statement:
6288      expression-statement
6289      simple-declaration  */
6290
6291 static void
6292 cp_parser_for_init_statement (parser)
6293      cp_parser *parser;
6294 {
6295   /* If the next token is a `;', then we have an empty
6296      expression-statement.  Gramatically, this is also a
6297      simple-declaration, but an invalid one, because it does not
6298      declare anything.  Therefore, if we did not handle this case
6299      specially, we would issue an error message about an invalid
6300      declaration.  */
6301   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6302     {
6303       /* We're going to speculatively look for a declaration, falling back
6304          to an expression, if necessary.  */
6305       cp_parser_parse_tentatively (parser);
6306       /* Parse the declaration.  */
6307       cp_parser_simple_declaration (parser,
6308                                     /*function_definition_allowed_p=*/false);
6309       /* If the tentative parse failed, then we shall need to look for an
6310          expression-statement.  */
6311       if (cp_parser_parse_definitely (parser))
6312         return;
6313     }
6314
6315   cp_parser_expression_statement (parser);
6316 }
6317
6318 /* Parse a jump-statement.
6319
6320    jump-statement:
6321      break ;
6322      continue ;
6323      return expression [opt] ;
6324      goto identifier ;  
6325
6326    GNU extension:
6327
6328    jump-statement:
6329      goto * expression ;
6330
6331    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6332    GOTO_STMT.  */
6333
6334 static tree
6335 cp_parser_jump_statement (parser)
6336      cp_parser *parser;
6337 {
6338   tree statement = error_mark_node;
6339   cp_token *token;
6340   enum rid keyword;
6341
6342   /* Peek at the next token.  */
6343   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6344   if (!token)
6345     return error_mark_node;
6346
6347   /* See what kind of keyword it is.  */
6348   keyword = token->keyword;
6349   switch (keyword)
6350     {
6351     case RID_BREAK:
6352       statement = finish_break_stmt ();
6353       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6354       break;
6355
6356     case RID_CONTINUE:
6357       statement = finish_continue_stmt ();
6358       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6359       break;
6360
6361     case RID_RETURN:
6362       {
6363         tree expr;
6364
6365         /* If the next token is a `;', then there is no 
6366            expression.  */
6367         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6368           expr = cp_parser_expression (parser);
6369         else
6370           expr = NULL_TREE;
6371         /* Build the return-statement.  */
6372         statement = finish_return_stmt (expr);
6373         /* Look for the final `;'.  */
6374         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6375       }
6376       break;
6377
6378     case RID_GOTO:
6379       /* Create the goto-statement.  */
6380       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6381         {
6382           /* Issue a warning about this use of a GNU extension.  */
6383           if (pedantic)
6384             pedwarn ("ISO C++ forbids computed gotos");
6385           /* Consume the '*' token.  */
6386           cp_lexer_consume_token (parser->lexer);
6387           /* Parse the dependent expression.  */
6388           finish_goto_stmt (cp_parser_expression (parser));
6389         }
6390       else
6391         finish_goto_stmt (cp_parser_identifier (parser));
6392       /* Look for the final `;'.  */
6393       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6394       break;
6395
6396     default:
6397       cp_parser_error (parser, "expected jump-statement");
6398       break;
6399     }
6400
6401   return statement;
6402 }
6403
6404 /* Parse a declaration-statement.
6405
6406    declaration-statement:
6407      block-declaration  */
6408
6409 static void
6410 cp_parser_declaration_statement (parser)
6411      cp_parser *parser;
6412 {
6413   /* Parse the block-declaration.  */
6414   cp_parser_block_declaration (parser, /*statement_p=*/true);
6415
6416   /* Finish off the statement.  */
6417   finish_stmt ();
6418 }
6419
6420 /* Some dependent statements (like `if (cond) statement'), are
6421    implicitly in their own scope.  In other words, if the statement is
6422    a single statement (as opposed to a compound-statement), it is
6423    none-the-less treated as if it were enclosed in braces.  Any
6424    declarations appearing in the dependent statement are out of scope
6425    after control passes that point.  This function parses a statement,
6426    but ensures that is in its own scope, even if it is not a
6427    compound-statement.  
6428
6429    Returns the new statement.  */
6430
6431 static tree
6432 cp_parser_implicitly_scoped_statement (parser)
6433      cp_parser *parser;
6434 {
6435   tree statement;
6436
6437   /* If the token is not a `{', then we must take special action.  */
6438   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6439     {
6440       /* Create a compound-statement.  */
6441       statement = begin_compound_stmt (/*has_no_scope=*/0);
6442       /* Parse the dependent-statement.  */
6443       cp_parser_statement (parser);
6444       /* Finish the dummy compound-statement.  */
6445       finish_compound_stmt (/*has_no_scope=*/0, statement);
6446     }
6447   /* Otherwise, we simply parse the statement directly.  */
6448   else
6449     statement = cp_parser_compound_statement (parser);
6450
6451   /* Return the statement.  */
6452   return statement;
6453 }
6454
6455 /* For some dependent statements (like `while (cond) statement'), we
6456    have already created a scope.  Therefore, even if the dependent
6457    statement is a compound-statement, we do not want to create another
6458    scope.  */
6459
6460 static void
6461 cp_parser_already_scoped_statement (parser)
6462      cp_parser *parser;
6463 {
6464   /* If the token is not a `{', then we must take special action.  */
6465   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6466     {
6467       tree statement;
6468
6469       /* Create a compound-statement.  */
6470       statement = begin_compound_stmt (/*has_no_scope=*/1);
6471       /* Parse the dependent-statement.  */
6472       cp_parser_statement (parser);
6473       /* Finish the dummy compound-statement.  */
6474       finish_compound_stmt (/*has_no_scope=*/1, statement);
6475     }
6476   /* Otherwise, we simply parse the statement directly.  */
6477   else
6478     cp_parser_statement (parser);
6479 }
6480
6481 /* Declarations [gram.dcl.dcl] */
6482
6483 /* Parse an optional declaration-sequence.
6484
6485    declaration-seq:
6486      declaration
6487      declaration-seq declaration  */
6488
6489 static void
6490 cp_parser_declaration_seq_opt (parser)
6491      cp_parser *parser;
6492 {
6493   while (true)
6494     {
6495       cp_token *token;
6496
6497       token = cp_lexer_peek_token (parser->lexer);
6498
6499       if (token->type == CPP_CLOSE_BRACE
6500           || token->type == CPP_EOF)
6501         break;
6502
6503       if (token->type == CPP_SEMICOLON) 
6504         {
6505           /* A declaration consisting of a single semicolon is
6506              invalid.  Allow it unless we're being pedantic.  */
6507           if (pedantic)
6508             pedwarn ("extra `;'");
6509           cp_lexer_consume_token (parser->lexer);
6510           continue;
6511         }
6512
6513       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6514          parser to enter or exit implict `extern "C"' blocks.  */
6515       while (pending_lang_change > 0)
6516         {
6517           push_lang_context (lang_name_c);
6518           --pending_lang_change;
6519         }
6520       while (pending_lang_change < 0)
6521         {
6522           pop_lang_context ();
6523           ++pending_lang_change;
6524         }
6525
6526       /* Parse the declaration itself.  */
6527       cp_parser_declaration (parser);
6528     }
6529 }
6530
6531 /* Parse a declaration.
6532
6533    declaration:
6534      block-declaration
6535      function-definition
6536      template-declaration
6537      explicit-instantiation
6538      explicit-specialization
6539      linkage-specification
6540      namespace-definition    
6541
6542    GNU extension:
6543
6544    declaration:
6545       __extension__ declaration */
6546
6547 static void
6548 cp_parser_declaration (parser)
6549      cp_parser *parser;
6550 {
6551   cp_token token1;
6552   cp_token token2;
6553   int saved_pedantic;
6554
6555   /* Check for the `__extension__' keyword.  */
6556   if (cp_parser_extension_opt (parser, &saved_pedantic))
6557     {
6558       /* Parse the qualified declaration.  */
6559       cp_parser_declaration (parser);
6560       /* Restore the PEDANTIC flag.  */
6561       pedantic = saved_pedantic;
6562
6563       return;
6564     }
6565
6566   /* Try to figure out what kind of declaration is present.  */
6567   token1 = *cp_lexer_peek_token (parser->lexer);
6568   if (token1.type != CPP_EOF)
6569     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6570
6571   /* If the next token is `extern' and the following token is a string
6572      literal, then we have a linkage specification.  */
6573   if (token1.keyword == RID_EXTERN
6574       && cp_parser_is_string_literal (&token2))
6575     cp_parser_linkage_specification (parser);
6576   /* If the next token is `template', then we have either a template
6577      declaration, an explicit instantiation, or an explicit
6578      specialization.  */
6579   else if (token1.keyword == RID_TEMPLATE)
6580     {
6581       /* `template <>' indicates a template specialization.  */
6582       if (token2.type == CPP_LESS
6583           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6584         cp_parser_explicit_specialization (parser);
6585       /* `template <' indicates a template declaration.  */
6586       else if (token2.type == CPP_LESS)
6587         cp_parser_template_declaration (parser, /*member_p=*/false);
6588       /* Anything else must be an explicit instantiation.  */
6589       else
6590         cp_parser_explicit_instantiation (parser);
6591     }
6592   /* If the next token is `export', then we have a template
6593      declaration.  */
6594   else if (token1.keyword == RID_EXPORT)
6595     cp_parser_template_declaration (parser, /*member_p=*/false);
6596   /* If the next token is `extern', 'static' or 'inline' and the one
6597      after that is `template', we have a GNU extended explicit
6598      instantiation directive.  */
6599   else if (cp_parser_allow_gnu_extensions_p (parser)
6600            && (token1.keyword == RID_EXTERN
6601                || token1.keyword == RID_STATIC
6602                || token1.keyword == RID_INLINE)
6603            && token2.keyword == RID_TEMPLATE)
6604     cp_parser_explicit_instantiation (parser);
6605   /* If the next token is `namespace', check for a named or unnamed
6606      namespace definition.  */
6607   else if (token1.keyword == RID_NAMESPACE
6608            && (/* A named namespace definition.  */
6609                (token2.type == CPP_NAME
6610                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6611                     == CPP_OPEN_BRACE))
6612                /* An unnamed namespace definition.  */
6613                || token2.type == CPP_OPEN_BRACE))
6614     cp_parser_namespace_definition (parser);
6615   /* We must have either a block declaration or a function
6616      definition.  */
6617   else
6618     /* Try to parse a block-declaration, or a function-definition.  */
6619     cp_parser_block_declaration (parser, /*statement_p=*/false);
6620 }
6621
6622 /* Parse a block-declaration.  
6623
6624    block-declaration:
6625      simple-declaration
6626      asm-definition
6627      namespace-alias-definition
6628      using-declaration
6629      using-directive  
6630
6631    GNU Extension:
6632
6633    block-declaration:
6634      __extension__ block-declaration 
6635      label-declaration
6636
6637    If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6638    part of a declaration-statement.  */
6639
6640 static void
6641 cp_parser_block_declaration (cp_parser *parser, 
6642                              bool      statement_p)
6643 {
6644   cp_token *token1;
6645   int saved_pedantic;
6646
6647   /* Check for the `__extension__' keyword.  */
6648   if (cp_parser_extension_opt (parser, &saved_pedantic))
6649     {
6650       /* Parse the qualified declaration.  */
6651       cp_parser_block_declaration (parser, statement_p);
6652       /* Restore the PEDANTIC flag.  */
6653       pedantic = saved_pedantic;
6654
6655       return;
6656     }
6657
6658   /* Peek at the next token to figure out which kind of declaration is
6659      present.  */
6660   token1 = cp_lexer_peek_token (parser->lexer);
6661
6662   /* If the next keyword is `asm', we have an asm-definition.  */
6663   if (token1->keyword == RID_ASM)
6664     {
6665       if (statement_p)
6666         cp_parser_commit_to_tentative_parse (parser);
6667       cp_parser_asm_definition (parser);
6668     }
6669   /* If the next keyword is `namespace', we have a
6670      namespace-alias-definition.  */
6671   else if (token1->keyword == RID_NAMESPACE)
6672     cp_parser_namespace_alias_definition (parser);
6673   /* If the next keyword is `using', we have either a
6674      using-declaration or a using-directive.  */
6675   else if (token1->keyword == RID_USING)
6676     {
6677       cp_token *token2;
6678
6679       if (statement_p)
6680         cp_parser_commit_to_tentative_parse (parser);
6681       /* If the token after `using' is `namespace', then we have a
6682          using-directive.  */
6683       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6684       if (token2->keyword == RID_NAMESPACE)
6685         cp_parser_using_directive (parser);
6686       /* Otherwise, it's a using-declaration.  */
6687       else
6688         cp_parser_using_declaration (parser);
6689     }
6690   /* If the next keyword is `__label__' we have a label declaration.  */
6691   else if (token1->keyword == RID_LABEL)
6692     {
6693       if (statement_p)
6694         cp_parser_commit_to_tentative_parse (parser);
6695       cp_parser_label_declaration (parser);
6696     }
6697   /* Anything else must be a simple-declaration.  */
6698   else
6699     cp_parser_simple_declaration (parser, !statement_p);
6700 }
6701
6702 /* Parse a simple-declaration.
6703
6704    simple-declaration:
6705      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6706
6707    init-declarator-list:
6708      init-declarator
6709      init-declarator-list , init-declarator 
6710
6711    If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6712    function-definition as a simple-declaration.   */
6713
6714 static void
6715 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6716      cp_parser *parser;
6717      bool function_definition_allowed_p;
6718 {
6719   tree decl_specifiers;
6720   tree attributes;
6721   tree access_checks;
6722   bool declares_class_or_enum;
6723   bool saw_declarator;
6724
6725   /* Defer access checks until we know what is being declared; the
6726      checks for names appearing in the decl-specifier-seq should be
6727      done as if we were in the scope of the thing being declared.  */
6728   cp_parser_start_deferring_access_checks (parser);
6729   /* Parse the decl-specifier-seq.  We have to keep track of whether
6730      or not the decl-specifier-seq declares a named class or
6731      enumeration type, since that is the only case in which the
6732      init-declarator-list is allowed to be empty.  
6733
6734      [dcl.dcl]
6735
6736      In a simple-declaration, the optional init-declarator-list can be
6737      omitted only when declaring a class or enumeration, that is when
6738      the decl-specifier-seq contains either a class-specifier, an
6739      elaborated-type-specifier, or an enum-specifier.  */
6740   decl_specifiers
6741     = cp_parser_decl_specifier_seq (parser, 
6742                                     CP_PARSER_FLAGS_OPTIONAL,
6743                                     &attributes,
6744                                     &declares_class_or_enum);
6745   /* We no longer need to defer access checks.  */
6746   access_checks = cp_parser_stop_deferring_access_checks (parser);
6747
6748   /* Prevent access checks from being reclaimed by GC.  */
6749   parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6750                                            parser->access_checks_lists);
6751
6752   /* Keep going until we hit the `;' at the end of the simple
6753      declaration.  */
6754   saw_declarator = false;
6755   while (cp_lexer_next_token_is_not (parser->lexer, 
6756                                      CPP_SEMICOLON))
6757     {
6758       cp_token *token;
6759       bool function_definition_p;
6760
6761       saw_declarator = true;
6762       /* Parse the init-declarator.  */
6763       cp_parser_init_declarator (parser, decl_specifiers, attributes,
6764                                  access_checks,
6765                                  function_definition_allowed_p,
6766                                  /*member_p=*/false,
6767                                  &function_definition_p);
6768       /* Handle function definitions specially.  */
6769       if (function_definition_p)
6770         {
6771           /* If the next token is a `,', then we are probably
6772              processing something like:
6773
6774                void f() {}, *p;
6775
6776              which is erroneous.  */
6777           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6778             error ("mixing declarations and function-definitions is forbidden");
6779           /* Otherwise, we're done with the list of declarators.  */
6780           else
6781             {
6782               /* Discard access checks no longer in use. */
6783               parser->access_checks_lists
6784                 = TREE_CHAIN (parser->access_checks_lists);
6785               return;
6786             }
6787         }
6788       /* The next token should be either a `,' or a `;'.  */
6789       token = cp_lexer_peek_token (parser->lexer);
6790       /* If it's a `,', there are more declarators to come.  */
6791       if (token->type == CPP_COMMA)
6792         cp_lexer_consume_token (parser->lexer);
6793       /* If it's a `;', we are done.  */
6794       else if (token->type == CPP_SEMICOLON)
6795         break;
6796       /* Anything else is an error.  */
6797       else
6798         {
6799           cp_parser_error (parser, "expected `,' or `;'");
6800           /* Skip tokens until we reach the end of the statement.  */
6801           cp_parser_skip_to_end_of_statement (parser);
6802           /* Discard access checks no longer in use.  */
6803           parser->access_checks_lists
6804             = TREE_CHAIN (parser->access_checks_lists);
6805           return;
6806         }
6807       /* After the first time around, a function-definition is not
6808          allowed -- even if it was OK at first.  For example:
6809
6810            int i, f() {}
6811
6812          is not valid.  */
6813       function_definition_allowed_p = false;
6814     }
6815
6816   /* Issue an error message if no declarators are present, and the
6817      decl-specifier-seq does not itself declare a class or
6818      enumeration.  */
6819   if (!saw_declarator)
6820     {
6821       if (cp_parser_declares_only_class_p (parser))
6822         shadow_tag (decl_specifiers);
6823       /* Perform any deferred access checks.  */
6824       cp_parser_perform_deferred_access_checks (access_checks);
6825     }
6826
6827   /* Consume the `;'.  */
6828   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6829
6830   /* Mark all the classes that appeared in the decl-specifier-seq as
6831      having received a `;'.  */
6832   note_list_got_semicolon (decl_specifiers);
6833
6834   /* Discard access checks no longer in use.  */
6835   parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6836 }
6837
6838 /* Parse a decl-specifier-seq.
6839
6840    decl-specifier-seq:
6841      decl-specifier-seq [opt] decl-specifier
6842
6843    decl-specifier:
6844      storage-class-specifier
6845      type-specifier
6846      function-specifier
6847      friend
6848      typedef  
6849
6850    GNU Extension:
6851
6852    decl-specifier-seq:
6853      decl-specifier-seq [opt] attributes
6854
6855    Returns a TREE_LIST, giving the decl-specifiers in the order they
6856    appear in the source code.  The TREE_VALUE of each node is the
6857    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6858    TREE_VALUE is simply the correspoding TREE_IDENTIFIER.  For the
6859    representation of a type-specifier, see cp_parser_type_specifier.  
6860
6861    If there are attributes, they will be stored in *ATTRIBUTES,
6862    represented as described above cp_parser_attributes.  
6863
6864    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6865    appears, and the entity that will be a friend is not going to be a
6866    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6867    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6868    friendship is granted might not be a class.  */
6869
6870 static tree
6871 cp_parser_decl_specifier_seq (parser, flags, attributes,
6872                               declares_class_or_enum)
6873      cp_parser *parser;
6874      cp_parser_flags flags;
6875      tree *attributes;
6876      bool *declares_class_or_enum;
6877 {
6878   tree decl_specs = NULL_TREE;
6879   bool friend_p = false;
6880
6881   /* Assume no class or enumeration type is declared.  */
6882   *declares_class_or_enum = false;
6883
6884   /* Assume there are no attributes.  */
6885   *attributes = NULL_TREE;
6886
6887   /* Keep reading specifiers until there are no more to read.  */
6888   while (true)
6889     {
6890       tree decl_spec = NULL_TREE;
6891       bool constructor_p;
6892       cp_token *token;
6893
6894       /* Peek at the next token.  */
6895       token = cp_lexer_peek_token (parser->lexer);
6896       /* Handle attributes.  */
6897       if (token->keyword == RID_ATTRIBUTE)
6898         {
6899           /* Parse the attributes.  */
6900           decl_spec = cp_parser_attributes_opt (parser);
6901           /* Add them to the list.  */
6902           *attributes = chainon (*attributes, decl_spec);
6903           continue;
6904         }
6905       /* If the next token is an appropriate keyword, we can simply
6906          add it to the list.  */
6907       switch (token->keyword)
6908         {
6909         case RID_FRIEND:
6910           /* decl-specifier:
6911                friend  */
6912           friend_p = true;
6913           /* The representation of the specifier is simply the
6914              appropriate TREE_IDENTIFIER node.  */
6915           decl_spec = token->value;
6916           /* Consume the token.  */
6917           cp_lexer_consume_token (parser->lexer);
6918           break;
6919
6920           /* function-specifier:
6921                inline
6922                virtual
6923                explicit  */
6924         case RID_INLINE:
6925         case RID_VIRTUAL:
6926         case RID_EXPLICIT:
6927           decl_spec = cp_parser_function_specifier_opt (parser);
6928           break;
6929           
6930           /* decl-specifier:
6931                typedef  */
6932         case RID_TYPEDEF:
6933           /* The representation of the specifier is simply the
6934              appropriate TREE_IDENTIFIER node.  */
6935           decl_spec = token->value;
6936           /* Consume the token.  */
6937           cp_lexer_consume_token (parser->lexer);
6938           break;
6939
6940           /* storage-class-specifier:
6941                auto
6942                register
6943                static
6944                extern
6945                mutable  
6946
6947              GNU Extension:
6948                thread  */
6949         case RID_AUTO:
6950         case RID_REGISTER:
6951         case RID_STATIC:
6952         case RID_EXTERN:
6953         case RID_MUTABLE:
6954         case RID_THREAD:
6955           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6956           break;
6957           
6958         default:
6959           break;
6960         }
6961
6962       /* Constructors are a special case.  The `S' in `S()' is not a
6963          decl-specifier; it is the beginning of the declarator.  */
6964       constructor_p = (!decl_spec 
6965                        && cp_parser_constructor_declarator_p (parser,
6966                                                               friend_p));
6967
6968       /* If we don't have a DECL_SPEC yet, then we must be looking at
6969          a type-specifier.  */
6970       if (!decl_spec && !constructor_p)
6971         {
6972           bool decl_spec_declares_class_or_enum;
6973           bool is_cv_qualifier;
6974
6975           decl_spec
6976             = cp_parser_type_specifier (parser, flags,
6977                                         friend_p,
6978                                         /*is_declaration=*/true,
6979                                         &decl_spec_declares_class_or_enum,
6980                                         &is_cv_qualifier);
6981
6982           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6983
6984           /* If this type-specifier referenced a user-defined type
6985              (a typedef, class-name, etc.), then we can't allow any
6986              more such type-specifiers henceforth.
6987
6988              [dcl.spec]
6989
6990              The longest sequence of decl-specifiers that could
6991              possibly be a type name is taken as the
6992              decl-specifier-seq of a declaration.  The sequence shall
6993              be self-consistent as described below.
6994
6995              [dcl.type]
6996
6997              As a general rule, at most one type-specifier is allowed
6998              in the complete decl-specifier-seq of a declaration.  The
6999              only exceptions are the following:
7000
7001              -- const or volatile can be combined with any other
7002                 type-specifier. 
7003
7004              -- signed or unsigned can be combined with char, long,
7005                 short, or int.
7006
7007              -- ..
7008
7009              Example:
7010
7011                typedef char* Pc;
7012                void g (const int Pc);
7013
7014              Here, Pc is *not* part of the decl-specifier seq; it's
7015              the declarator.  Therefore, once we see a type-specifier
7016              (other than a cv-qualifier), we forbid any additional
7017              user-defined types.  We *do* still allow things like `int
7018              int' to be considered a decl-specifier-seq, and issue the
7019              error message later.  */
7020           if (decl_spec && !is_cv_qualifier)
7021             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7022         }
7023
7024       /* If we still do not have a DECL_SPEC, then there are no more
7025          decl-specifiers.  */
7026       if (!decl_spec)
7027         {
7028           /* Issue an error message, unless the entire construct was
7029              optional.  */
7030           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7031             {
7032               cp_parser_error (parser, "expected decl specifier");
7033               return error_mark_node;
7034             }
7035
7036           break;
7037         }
7038
7039       /* Add the DECL_SPEC to the list of specifiers.  */
7040       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7041
7042       /* After we see one decl-specifier, further decl-specifiers are
7043          always optional.  */
7044       flags |= CP_PARSER_FLAGS_OPTIONAL;
7045     }
7046
7047   /* We have built up the DECL_SPECS in reverse order.  Return them in
7048      the correct order.  */
7049   return nreverse (decl_specs);
7050 }
7051
7052 /* Parse an (optional) storage-class-specifier. 
7053
7054    storage-class-specifier:
7055      auto
7056      register
7057      static
7058      extern
7059      mutable  
7060
7061    GNU Extension:
7062
7063    storage-class-specifier:
7064      thread
7065
7066    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7067    
7068 static tree
7069 cp_parser_storage_class_specifier_opt (parser)
7070      cp_parser *parser;
7071 {
7072   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7073     {
7074     case RID_AUTO:
7075     case RID_REGISTER:
7076     case RID_STATIC:
7077     case RID_EXTERN:
7078     case RID_MUTABLE:
7079     case RID_THREAD:
7080       /* Consume the token.  */
7081       return cp_lexer_consume_token (parser->lexer)->value;
7082
7083     default:
7084       return NULL_TREE;
7085     }
7086 }
7087
7088 /* Parse an (optional) function-specifier. 
7089
7090    function-specifier:
7091      inline
7092      virtual
7093      explicit
7094
7095    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7096    
7097 static tree
7098 cp_parser_function_specifier_opt (parser)
7099      cp_parser *parser;
7100 {
7101   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7102     {
7103     case RID_INLINE:
7104     case RID_VIRTUAL:
7105     case RID_EXPLICIT:
7106       /* Consume the token.  */
7107       return cp_lexer_consume_token (parser->lexer)->value;
7108
7109     default:
7110       return NULL_TREE;
7111     }
7112 }
7113
7114 /* Parse a linkage-specification.
7115
7116    linkage-specification:
7117      extern string-literal { declaration-seq [opt] }
7118      extern string-literal declaration  */
7119
7120 static void
7121 cp_parser_linkage_specification (parser)
7122      cp_parser *parser;
7123 {
7124   cp_token *token;
7125   tree linkage;
7126
7127   /* Look for the `extern' keyword.  */
7128   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7129
7130   /* Peek at the next token.  */
7131   token = cp_lexer_peek_token (parser->lexer);
7132   /* If it's not a string-literal, then there's a problem.  */
7133   if (!cp_parser_is_string_literal (token))
7134     {
7135       cp_parser_error (parser, "expected language-name");
7136       return;
7137     }
7138   /* Consume the token.  */
7139   cp_lexer_consume_token (parser->lexer);
7140
7141   /* Transform the literal into an identifier.  If the literal is a
7142      wide-character string, or contains embedded NULs, then we can't
7143      handle it as the user wants.  */
7144   if (token->type == CPP_WSTRING
7145       || (strlen (TREE_STRING_POINTER (token->value))
7146           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7147     {
7148       cp_parser_error (parser, "invalid linkage-specification");
7149       /* Assume C++ linkage.  */
7150       linkage = get_identifier ("c++");
7151     }
7152   /* If it's a simple string constant, things are easier.  */
7153   else
7154     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7155
7156   /* We're now using the new linkage.  */
7157   push_lang_context (linkage);
7158
7159   /* If the next token is a `{', then we're using the first
7160      production.  */
7161   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7162     {
7163       /* Consume the `{' token.  */
7164       cp_lexer_consume_token (parser->lexer);
7165       /* Parse the declarations.  */
7166       cp_parser_declaration_seq_opt (parser);
7167       /* Look for the closing `}'.  */
7168       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7169     }
7170   /* Otherwise, there's just one declaration.  */
7171   else
7172     {
7173       bool saved_in_unbraced_linkage_specification_p;
7174
7175       saved_in_unbraced_linkage_specification_p 
7176         = parser->in_unbraced_linkage_specification_p;
7177       parser->in_unbraced_linkage_specification_p = true;
7178       have_extern_spec = true;
7179       cp_parser_declaration (parser);
7180       have_extern_spec = false;
7181       parser->in_unbraced_linkage_specification_p 
7182         = saved_in_unbraced_linkage_specification_p;
7183     }
7184
7185   /* We're done with the linkage-specification.  */
7186   pop_lang_context ();
7187 }
7188
7189 /* Special member functions [gram.special] */
7190
7191 /* Parse a conversion-function-id.
7192
7193    conversion-function-id:
7194      operator conversion-type-id  
7195
7196    Returns an IDENTIFIER_NODE representing the operator.  */
7197
7198 static tree 
7199 cp_parser_conversion_function_id (parser)
7200      cp_parser *parser;
7201 {
7202   tree type;
7203   tree saved_scope;
7204   tree saved_qualifying_scope;
7205   tree saved_object_scope;
7206
7207   /* Look for the `operator' token.  */
7208   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7209     return error_mark_node;
7210   /* When we parse the conversion-type-id, the current scope will be
7211      reset.  However, we need that information in able to look up the
7212      conversion function later, so we save it here.  */
7213   saved_scope = parser->scope;
7214   saved_qualifying_scope = parser->qualifying_scope;
7215   saved_object_scope = parser->object_scope;
7216   /* We must enter the scope of the class so that the names of
7217      entities declared within the class are available in the
7218      conversion-type-id.  For example, consider:
7219
7220        struct S { 
7221          typedef int I;
7222          operator I();
7223        };
7224
7225        S::operator I() { ... }
7226
7227      In order to see that `I' is a type-name in the definition, we
7228      must be in the scope of `S'.  */
7229   if (saved_scope)
7230     push_scope (saved_scope);
7231   /* Parse the conversion-type-id.  */
7232   type = cp_parser_conversion_type_id (parser);
7233   /* Leave the scope of the class, if any.  */
7234   if (saved_scope)
7235     pop_scope (saved_scope);
7236   /* Restore the saved scope.  */
7237   parser->scope = saved_scope;
7238   parser->qualifying_scope = saved_qualifying_scope;
7239   parser->object_scope = saved_object_scope;
7240   /* If the TYPE is invalid, indicate failure.  */
7241   if (type == error_mark_node)
7242     return error_mark_node;
7243   return mangle_conv_op_name_for_type (type);
7244 }
7245
7246 /* Parse a conversion-type-id:
7247
7248    conversion-type-id:
7249      type-specifier-seq conversion-declarator [opt]
7250
7251    Returns the TYPE specified.  */
7252
7253 static tree
7254 cp_parser_conversion_type_id (parser)
7255      cp_parser *parser;
7256 {
7257   tree attributes;
7258   tree type_specifiers;
7259   tree declarator;
7260
7261   /* Parse the attributes.  */
7262   attributes = cp_parser_attributes_opt (parser);
7263   /* Parse the type-specifiers.  */
7264   type_specifiers = cp_parser_type_specifier_seq (parser);
7265   /* If that didn't work, stop.  */
7266   if (type_specifiers == error_mark_node)
7267     return error_mark_node;
7268   /* Parse the conversion-declarator.  */
7269   declarator = cp_parser_conversion_declarator_opt (parser);
7270
7271   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7272                          /*initialized=*/0, &attributes);
7273 }
7274
7275 /* Parse an (optional) conversion-declarator.
7276
7277    conversion-declarator:
7278      ptr-operator conversion-declarator [opt]  
7279
7280    Returns a representation of the declarator.  See
7281    cp_parser_declarator for details.  */
7282
7283 static tree
7284 cp_parser_conversion_declarator_opt (parser)
7285      cp_parser *parser;
7286 {
7287   enum tree_code code;
7288   tree class_type;
7289   tree cv_qualifier_seq;
7290
7291   /* We don't know if there's a ptr-operator next, or not.  */
7292   cp_parser_parse_tentatively (parser);
7293   /* Try the ptr-operator.  */
7294   code = cp_parser_ptr_operator (parser, &class_type, 
7295                                  &cv_qualifier_seq);
7296   /* If it worked, look for more conversion-declarators.  */
7297   if (cp_parser_parse_definitely (parser))
7298     {
7299      tree declarator;
7300
7301      /* Parse another optional declarator.  */
7302      declarator = cp_parser_conversion_declarator_opt (parser);
7303
7304      /* Create the representation of the declarator.  */
7305      if (code == INDIRECT_REF)
7306        declarator = make_pointer_declarator (cv_qualifier_seq,
7307                                              declarator);
7308      else
7309        declarator =  make_reference_declarator (cv_qualifier_seq,
7310                                                 declarator);
7311
7312      /* Handle the pointer-to-member case.  */
7313      if (class_type)
7314        declarator = build_nt (SCOPE_REF, class_type, declarator);
7315
7316      return declarator;
7317    }
7318
7319   return NULL_TREE;
7320 }
7321
7322 /* Parse an (optional) ctor-initializer.
7323
7324    ctor-initializer:
7325      : mem-initializer-list  
7326
7327    Returns TRUE iff the ctor-initializer was actually present.  */
7328
7329 static bool
7330 cp_parser_ctor_initializer_opt (parser)
7331      cp_parser *parser;
7332 {
7333   /* If the next token is not a `:', then there is no
7334      ctor-initializer.  */
7335   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7336     {
7337       /* Do default initialization of any bases and members.  */
7338       if (DECL_CONSTRUCTOR_P (current_function_decl))
7339         finish_mem_initializers (NULL_TREE);
7340
7341       return false;
7342     }
7343
7344   /* Consume the `:' token.  */
7345   cp_lexer_consume_token (parser->lexer);
7346   /* And the mem-initializer-list.  */
7347   cp_parser_mem_initializer_list (parser);
7348
7349   return true;
7350 }
7351
7352 /* Parse a mem-initializer-list.
7353
7354    mem-initializer-list:
7355      mem-initializer
7356      mem-initializer , mem-initializer-list  */
7357
7358 static void
7359 cp_parser_mem_initializer_list (parser)
7360      cp_parser *parser;
7361 {
7362   tree mem_initializer_list = NULL_TREE;
7363
7364   /* Let the semantic analysis code know that we are starting the
7365      mem-initializer-list.  */
7366   begin_mem_initializers ();
7367
7368   /* Loop through the list.  */
7369   while (true)
7370     {
7371       tree mem_initializer;
7372
7373       /* Parse the mem-initializer.  */
7374       mem_initializer = cp_parser_mem_initializer (parser);
7375       /* Add it to the list, unless it was erroneous.  */
7376       if (mem_initializer)
7377         {
7378           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7379           mem_initializer_list = mem_initializer;
7380         }
7381       /* If the next token is not a `,', we're done.  */
7382       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7383         break;
7384       /* Consume the `,' token.  */
7385       cp_lexer_consume_token (parser->lexer);
7386     }
7387
7388   /* Perform semantic analysis.  */
7389   finish_mem_initializers (mem_initializer_list);
7390 }
7391
7392 /* Parse a mem-initializer.
7393
7394    mem-initializer:
7395      mem-initializer-id ( expression-list [opt] )  
7396
7397    GNU extension:
7398   
7399    mem-initializer:
7400      ( expresion-list [opt] )
7401
7402    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7403    class) or FIELD_DECL (for a non-static data member) to initialize;
7404    the TREE_VALUE is the expression-list.  */
7405
7406 static tree
7407 cp_parser_mem_initializer (parser)
7408      cp_parser *parser;
7409 {
7410   tree mem_initializer_id;
7411   tree expression_list;
7412
7413   /* Find out what is being initialized.  */
7414   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7415     {
7416       pedwarn ("anachronistic old-style base class initializer");
7417       mem_initializer_id = NULL_TREE;
7418     }
7419   else
7420     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7421   /* Look for the opening `('.  */
7422   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7423   /* Parse the expression-list.  */
7424   if (cp_lexer_next_token_is_not (parser->lexer,
7425                                   CPP_CLOSE_PAREN))
7426     expression_list = cp_parser_expression_list (parser);
7427   else
7428     expression_list = void_type_node;
7429   /* Look for the closing `)'.  */
7430   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7431
7432   return expand_member_init (mem_initializer_id,
7433                              expression_list);
7434 }
7435
7436 /* Parse a mem-initializer-id.
7437
7438    mem-initializer-id:
7439      :: [opt] nested-name-specifier [opt] class-name
7440      identifier  
7441
7442    Returns a TYPE indicating the class to be initializer for the first
7443    production.  Returns an IDENTIFIER_NODE indicating the data member
7444    to be initialized for the second production.  */
7445
7446 static tree
7447 cp_parser_mem_initializer_id (parser)
7448      cp_parser *parser;
7449 {
7450   bool global_scope_p;
7451   bool nested_name_specifier_p;
7452   tree id;
7453
7454   /* Look for the optional `::' operator.  */
7455   global_scope_p 
7456     = (cp_parser_global_scope_opt (parser, 
7457                                    /*current_scope_valid_p=*/false) 
7458        != NULL_TREE);
7459   /* Look for the optional nested-name-specifier.  The simplest way to
7460      implement:
7461
7462        [temp.res]
7463
7464        The keyword `typename' is not permitted in a base-specifier or
7465        mem-initializer; in these contexts a qualified name that
7466        depends on a template-parameter is implicitly assumed to be a
7467        type name.
7468
7469      is to assume that we have seen the `typename' keyword at this
7470      point.  */
7471   nested_name_specifier_p 
7472     = (cp_parser_nested_name_specifier_opt (parser,
7473                                             /*typename_keyword_p=*/true,
7474                                             /*check_dependency_p=*/true,
7475                                             /*type_p=*/true)
7476        != NULL_TREE);
7477   /* If there is a `::' operator or a nested-name-specifier, then we
7478      are definitely looking for a class-name.  */
7479   if (global_scope_p || nested_name_specifier_p)
7480     return cp_parser_class_name (parser,
7481                                  /*typename_keyword_p=*/true,
7482                                  /*template_keyword_p=*/false,
7483                                  /*type_p=*/false,
7484                                  /*check_access_p=*/true,
7485                                  /*check_dependency_p=*/true,
7486                                  /*class_head_p=*/false);
7487   /* Otherwise, we could also be looking for an ordinary identifier.  */
7488   cp_parser_parse_tentatively (parser);
7489   /* Try a class-name.  */
7490   id = cp_parser_class_name (parser, 
7491                              /*typename_keyword_p=*/true,
7492                              /*template_keyword_p=*/false,
7493                              /*type_p=*/false,
7494                              /*check_access_p=*/true,
7495                              /*check_dependency_p=*/true,
7496                              /*class_head_p=*/false);
7497   /* If we found one, we're done.  */
7498   if (cp_parser_parse_definitely (parser))
7499     return id;
7500   /* Otherwise, look for an ordinary identifier.  */
7501   return cp_parser_identifier (parser);
7502 }
7503
7504 /* Overloading [gram.over] */
7505
7506 /* Parse an operator-function-id.
7507
7508    operator-function-id:
7509      operator operator  
7510
7511    Returns an IDENTIFIER_NODE for the operator which is a
7512    human-readable spelling of the identifier, e.g., `operator +'.  */
7513
7514 static tree 
7515 cp_parser_operator_function_id (parser)
7516      cp_parser *parser;
7517 {
7518   /* Look for the `operator' keyword.  */
7519   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7520     return error_mark_node;
7521   /* And then the name of the operator itself.  */
7522   return cp_parser_operator (parser);
7523 }
7524
7525 /* Parse an operator.
7526
7527    operator:
7528      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7529      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7530      || ++ -- , ->* -> () []
7531
7532    GNU Extensions:
7533    
7534    operator:
7535      <? >? <?= >?=
7536
7537    Returns an IDENTIFIER_NODE for the operator which is a
7538    human-readable spelling of the identifier, e.g., `operator +'.  */
7539    
7540 static tree
7541 cp_parser_operator (parser)
7542      cp_parser *parser;
7543 {
7544   tree id = NULL_TREE;
7545   cp_token *token;
7546
7547   /* Peek at the next token.  */
7548   token = cp_lexer_peek_token (parser->lexer);
7549   /* Figure out which operator we have.  */
7550   switch (token->type)
7551     {
7552     case CPP_KEYWORD:
7553       {
7554         enum tree_code op;
7555
7556         /* The keyword should be either `new' or `delete'.  */
7557         if (token->keyword == RID_NEW)
7558           op = NEW_EXPR;
7559         else if (token->keyword == RID_DELETE)
7560           op = DELETE_EXPR;
7561         else
7562           break;
7563
7564         /* Consume the `new' or `delete' token.  */
7565         cp_lexer_consume_token (parser->lexer);
7566
7567         /* Peek at the next token.  */
7568         token = cp_lexer_peek_token (parser->lexer);
7569         /* If it's a `[' token then this is the array variant of the
7570            operator.  */
7571         if (token->type == CPP_OPEN_SQUARE)
7572           {
7573             /* Consume the `[' token.  */
7574             cp_lexer_consume_token (parser->lexer);
7575             /* Look for the `]' token.  */
7576             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7577             id = ansi_opname (op == NEW_EXPR 
7578                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7579           }
7580         /* Otherwise, we have the non-array variant.  */
7581         else
7582           id = ansi_opname (op);
7583
7584         return id;
7585       }
7586
7587     case CPP_PLUS:
7588       id = ansi_opname (PLUS_EXPR);
7589       break;
7590
7591     case CPP_MINUS:
7592       id = ansi_opname (MINUS_EXPR);
7593       break;
7594
7595     case CPP_MULT:
7596       id = ansi_opname (MULT_EXPR);
7597       break;
7598
7599     case CPP_DIV:
7600       id = ansi_opname (TRUNC_DIV_EXPR);
7601       break;
7602
7603     case CPP_MOD:
7604       id = ansi_opname (TRUNC_MOD_EXPR);
7605       break;
7606
7607     case CPP_XOR:
7608       id = ansi_opname (BIT_XOR_EXPR);
7609       break;
7610
7611     case CPP_AND:
7612       id = ansi_opname (BIT_AND_EXPR);
7613       break;
7614
7615     case CPP_OR:
7616       id = ansi_opname (BIT_IOR_EXPR);
7617       break;
7618
7619     case CPP_COMPL:
7620       id = ansi_opname (BIT_NOT_EXPR);
7621       break;
7622       
7623     case CPP_NOT:
7624       id = ansi_opname (TRUTH_NOT_EXPR);
7625       break;
7626
7627     case CPP_EQ:
7628       id = ansi_assopname (NOP_EXPR);
7629       break;
7630
7631     case CPP_LESS:
7632       id = ansi_opname (LT_EXPR);
7633       break;
7634
7635     case CPP_GREATER:
7636       id = ansi_opname (GT_EXPR);
7637       break;
7638
7639     case CPP_PLUS_EQ:
7640       id = ansi_assopname (PLUS_EXPR);
7641       break;
7642
7643     case CPP_MINUS_EQ:
7644       id = ansi_assopname (MINUS_EXPR);
7645       break;
7646
7647     case CPP_MULT_EQ:
7648       id = ansi_assopname (MULT_EXPR);
7649       break;
7650
7651     case CPP_DIV_EQ:
7652       id = ansi_assopname (TRUNC_DIV_EXPR);
7653       break;
7654
7655     case CPP_MOD_EQ:
7656       id = ansi_assopname (TRUNC_MOD_EXPR);
7657       break;
7658
7659     case CPP_XOR_EQ:
7660       id = ansi_assopname (BIT_XOR_EXPR);
7661       break;
7662
7663     case CPP_AND_EQ:
7664       id = ansi_assopname (BIT_AND_EXPR);
7665       break;
7666
7667     case CPP_OR_EQ:
7668       id = ansi_assopname (BIT_IOR_EXPR);
7669       break;
7670
7671     case CPP_LSHIFT:
7672       id = ansi_opname (LSHIFT_EXPR);
7673       break;
7674
7675     case CPP_RSHIFT:
7676       id = ansi_opname (RSHIFT_EXPR);
7677       break;
7678
7679     case CPP_LSHIFT_EQ:
7680       id = ansi_assopname (LSHIFT_EXPR);
7681       break;
7682
7683     case CPP_RSHIFT_EQ:
7684       id = ansi_assopname (RSHIFT_EXPR);
7685       break;
7686
7687     case CPP_EQ_EQ:
7688       id = ansi_opname (EQ_EXPR);
7689       break;
7690
7691     case CPP_NOT_EQ:
7692       id = ansi_opname (NE_EXPR);
7693       break;
7694
7695     case CPP_LESS_EQ:
7696       id = ansi_opname (LE_EXPR);
7697       break;
7698
7699     case CPP_GREATER_EQ:
7700       id = ansi_opname (GE_EXPR);
7701       break;
7702
7703     case CPP_AND_AND:
7704       id = ansi_opname (TRUTH_ANDIF_EXPR);
7705       break;
7706
7707     case CPP_OR_OR:
7708       id = ansi_opname (TRUTH_ORIF_EXPR);
7709       break;
7710       
7711     case CPP_PLUS_PLUS:
7712       id = ansi_opname (POSTINCREMENT_EXPR);
7713       break;
7714
7715     case CPP_MINUS_MINUS:
7716       id = ansi_opname (PREDECREMENT_EXPR);
7717       break;
7718
7719     case CPP_COMMA:
7720       id = ansi_opname (COMPOUND_EXPR);
7721       break;
7722
7723     case CPP_DEREF_STAR:
7724       id = ansi_opname (MEMBER_REF);
7725       break;
7726
7727     case CPP_DEREF:
7728       id = ansi_opname (COMPONENT_REF);
7729       break;
7730
7731     case CPP_OPEN_PAREN:
7732       /* Consume the `('.  */
7733       cp_lexer_consume_token (parser->lexer);
7734       /* Look for the matching `)'.  */
7735       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7736       return ansi_opname (CALL_EXPR);
7737
7738     case CPP_OPEN_SQUARE:
7739       /* Consume the `['.  */
7740       cp_lexer_consume_token (parser->lexer);
7741       /* Look for the matching `]'.  */
7742       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7743       return ansi_opname (ARRAY_REF);
7744
7745       /* Extensions.  */
7746     case CPP_MIN:
7747       id = ansi_opname (MIN_EXPR);
7748       break;
7749
7750     case CPP_MAX:
7751       id = ansi_opname (MAX_EXPR);
7752       break;
7753
7754     case CPP_MIN_EQ:
7755       id = ansi_assopname (MIN_EXPR);
7756       break;
7757
7758     case CPP_MAX_EQ:
7759       id = ansi_assopname (MAX_EXPR);
7760       break;
7761
7762     default:
7763       /* Anything else is an error.  */
7764       break;
7765     }
7766
7767   /* If we have selected an identifier, we need to consume the
7768      operator token.  */
7769   if (id)
7770     cp_lexer_consume_token (parser->lexer);
7771   /* Otherwise, no valid operator name was present.  */
7772   else
7773     {
7774       cp_parser_error (parser, "expected operator");
7775       id = error_mark_node;
7776     }
7777
7778   return id;
7779 }
7780
7781 /* Parse a template-declaration.
7782
7783    template-declaration:
7784      export [opt] template < template-parameter-list > declaration  
7785
7786    If MEMBER_P is TRUE, this template-declaration occurs within a
7787    class-specifier.  
7788
7789    The grammar rule given by the standard isn't correct.  What
7790    is really meant is:
7791
7792    template-declaration:
7793      export [opt] template-parameter-list-seq 
7794        decl-specifier-seq [opt] init-declarator [opt] ;
7795      export [opt] template-parameter-list-seq 
7796        function-definition
7797
7798    template-parameter-list-seq:
7799      template-parameter-list-seq [opt]
7800      template < template-parameter-list >  */
7801
7802 static void
7803 cp_parser_template_declaration (parser, member_p)
7804      cp_parser *parser;
7805      bool member_p;
7806 {
7807   /* Check for `export'.  */
7808   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7809     {
7810       /* Consume the `export' token.  */
7811       cp_lexer_consume_token (parser->lexer);
7812       /* Warn that we do not support `export'.  */
7813       warning ("keyword `export' not implemented, and will be ignored");
7814     }
7815
7816   cp_parser_template_declaration_after_export (parser, member_p);
7817 }
7818
7819 /* Parse a template-parameter-list.
7820
7821    template-parameter-list:
7822      template-parameter
7823      template-parameter-list , template-parameter
7824
7825    Returns a TREE_LIST.  Each node represents a template parameter.
7826    The nodes are connected via their TREE_CHAINs.  */
7827
7828 static tree
7829 cp_parser_template_parameter_list (parser)
7830      cp_parser *parser;
7831 {
7832   tree parameter_list = NULL_TREE;
7833
7834   while (true)
7835     {
7836       tree parameter;
7837       cp_token *token;
7838
7839       /* Parse the template-parameter.  */
7840       parameter = cp_parser_template_parameter (parser);
7841       /* Add it to the list.  */
7842       parameter_list = process_template_parm (parameter_list,
7843                                               parameter);
7844
7845       /* Peek at the next token.  */
7846       token = cp_lexer_peek_token (parser->lexer);
7847       /* If it's not a `,', we're done.  */
7848       if (token->type != CPP_COMMA)
7849         break;
7850       /* Otherwise, consume the `,' token.  */
7851       cp_lexer_consume_token (parser->lexer);
7852     }
7853
7854   return parameter_list;
7855 }
7856
7857 /* Parse a template-parameter.
7858
7859    template-parameter:
7860      type-parameter
7861      parameter-declaration
7862
7863    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7864    TREE_PURPOSE is the default value, if any.  */
7865
7866 static tree
7867 cp_parser_template_parameter (parser)
7868      cp_parser *parser;
7869 {
7870   cp_token *token;
7871
7872   /* Peek at the next token.  */
7873   token = cp_lexer_peek_token (parser->lexer);
7874   /* If it is `class' or `template', we have a type-parameter.  */
7875   if (token->keyword == RID_TEMPLATE)
7876     return cp_parser_type_parameter (parser);
7877   /* If it is `class' or `typename' we do not know yet whether it is a
7878      type parameter or a non-type parameter.  Consider:
7879
7880        template <typename T, typename T::X X> ...
7881
7882      or:
7883      
7884        template <class C, class D*> ...
7885
7886      Here, the first parameter is a type parameter, and the second is
7887      a non-type parameter.  We can tell by looking at the token after
7888      the identifier -- if it is a `,', `=', or `>' then we have a type
7889      parameter.  */
7890   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7891     {
7892       /* Peek at the token after `class' or `typename'.  */
7893       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7894       /* If it's an identifier, skip it.  */
7895       if (token->type == CPP_NAME)
7896         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7897       /* Now, see if the token looks like the end of a template
7898          parameter.  */
7899       if (token->type == CPP_COMMA 
7900           || token->type == CPP_EQ
7901           || token->type == CPP_GREATER)
7902         return cp_parser_type_parameter (parser);
7903     }
7904
7905   /* Otherwise, it is a non-type parameter.  
7906
7907      [temp.param]
7908
7909      When parsing a default template-argument for a non-type
7910      template-parameter, the first non-nested `>' is taken as the end
7911      of the template parameter-list rather than a greater-than
7912      operator.  */
7913   return 
7914     cp_parser_parameter_declaration (parser,
7915                                      /*greater_than_is_operator_p=*/false);
7916 }
7917
7918 /* Parse a type-parameter.
7919
7920    type-parameter:
7921      class identifier [opt]
7922      class identifier [opt] = type-id
7923      typename identifier [opt]
7924      typename identifier [opt] = type-id
7925      template < template-parameter-list > class identifier [opt]
7926      template < template-parameter-list > class identifier [opt] 
7927        = id-expression  
7928
7929    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7930    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7931    the declaration of the parameter.  */
7932
7933 static tree
7934 cp_parser_type_parameter (parser)
7935      cp_parser *parser;
7936 {
7937   cp_token *token;
7938   tree parameter;
7939
7940   /* Look for a keyword to tell us what kind of parameter this is.  */
7941   token = cp_parser_require (parser, CPP_KEYWORD, 
7942                              "expected `class', `typename', or `template'");
7943   if (!token)
7944     return error_mark_node;
7945
7946   switch (token->keyword)
7947     {
7948     case RID_CLASS:
7949     case RID_TYPENAME:
7950       {
7951         tree identifier;
7952         tree default_argument;
7953
7954         /* If the next token is an identifier, then it names the
7955            parameter.  */
7956         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7957           identifier = cp_parser_identifier (parser);
7958         else
7959           identifier = NULL_TREE;
7960
7961         /* Create the parameter.  */
7962         parameter = finish_template_type_parm (class_type_node, identifier);
7963
7964         /* If the next token is an `=', we have a default argument.  */
7965         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7966           {
7967             /* Consume the `=' token.  */
7968             cp_lexer_consume_token (parser->lexer);
7969             /* Parse the default-argumen.  */
7970             default_argument = cp_parser_type_id (parser);
7971           }
7972         else
7973           default_argument = NULL_TREE;
7974
7975         /* Create the combined representation of the parameter and the
7976            default argument.  */
7977         parameter = build_tree_list (default_argument, 
7978                                      parameter);
7979       }
7980       break;
7981
7982     case RID_TEMPLATE:
7983       {
7984         tree parameter_list;
7985         tree identifier;
7986         tree default_argument;
7987
7988         /* Look for the `<'.  */
7989         cp_parser_require (parser, CPP_LESS, "`<'");
7990         /* Parse the template-parameter-list.  */
7991         begin_template_parm_list ();
7992         parameter_list 
7993           = cp_parser_template_parameter_list (parser);
7994         parameter_list = end_template_parm_list (parameter_list);
7995         /* Look for the `>'.  */
7996         cp_parser_require (parser, CPP_GREATER, "`>'");
7997         /* Look for the `class' keyword.  */
7998         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7999         /* If the next token is an `=', then there is a
8000            default-argument.  If the next token is a `>', we are at
8001            the end of the parameter-list.  If the next token is a `,',
8002            then we are at the end of this parameter.  */
8003         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8004             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8005             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8006           identifier = cp_parser_identifier (parser);
8007         else
8008           identifier = NULL_TREE;
8009         /* Create the template parameter.  */
8010         parameter = finish_template_template_parm (class_type_node,
8011                                                    identifier);
8012                                                    
8013         /* If the next token is an `=', then there is a
8014            default-argument.  */
8015         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8016           {
8017             /* Consume the `='.  */
8018             cp_lexer_consume_token (parser->lexer);
8019             /* Parse the id-expression.  */
8020             default_argument 
8021               = cp_parser_id_expression (parser,
8022                                          /*template_keyword_p=*/false,
8023                                          /*check_dependency_p=*/true,
8024                                          /*template_p=*/NULL);
8025             /* Look up the name.  */
8026             default_argument 
8027               = cp_parser_lookup_name_simple (parser, default_argument);
8028             /* See if the default argument is valid.  */
8029             default_argument
8030               = check_template_template_default_arg (default_argument);
8031           }
8032         else
8033           default_argument = NULL_TREE;
8034
8035         /* Create the combined representation of the parameter and the
8036            default argument.  */
8037         parameter =  build_tree_list (default_argument, 
8038                                       parameter);
8039       }
8040       break;
8041
8042     default:
8043       /* Anything else is an error.  */
8044       cp_parser_error (parser,
8045                        "expected `class', `typename', or `template'");
8046       parameter = error_mark_node;
8047     }
8048   
8049   return parameter;
8050 }
8051
8052 /* Parse a template-id.
8053
8054    template-id:
8055      template-name < template-argument-list [opt] >
8056
8057    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8058    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8059    returned.  Otherwise, if the template-name names a function, or set
8060    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8061    names a class, returns a TYPE_DECL for the specialization.  
8062
8063    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8064    uninstantiated templates.  */
8065
8066 static tree
8067 cp_parser_template_id (cp_parser *parser, 
8068                        bool template_keyword_p, 
8069                        bool check_dependency_p)
8070 {
8071   tree template;
8072   tree arguments;
8073   tree saved_scope;
8074   tree saved_qualifying_scope;
8075   tree saved_object_scope;
8076   tree template_id;
8077   bool saved_greater_than_is_operator_p;
8078   ptrdiff_t start_of_id;
8079   tree access_check = NULL_TREE;
8080
8081   /* If the next token corresponds to a template-id, there is no need
8082      to reparse it.  */
8083   if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8084     {
8085       tree value;
8086       tree check;
8087
8088       /* Get the stored value.  */
8089       value = cp_lexer_consume_token (parser->lexer)->value;
8090       /* Perform any access checks that were deferred.  */
8091       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8092         cp_parser_defer_access_check (parser, 
8093                                       TREE_PURPOSE (check),
8094                                       TREE_VALUE (check));
8095       /* Return the stored value.  */
8096       return TREE_VALUE (value);
8097     }
8098
8099   /* Remember where the template-id starts.  */
8100   if (cp_parser_parsing_tentatively (parser)
8101       && !cp_parser_committed_to_tentative_parse (parser))
8102     {
8103       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8104       start_of_id = cp_lexer_token_difference (parser->lexer,
8105                                                parser->lexer->first_token,
8106                                                next_token);
8107       access_check = parser->context->deferred_access_checks;
8108     }
8109   else
8110     start_of_id = -1;
8111
8112   /* Parse the template-name.  */
8113   template = cp_parser_template_name (parser, template_keyword_p,
8114                                       check_dependency_p);
8115   if (template == error_mark_node)
8116     return error_mark_node;
8117
8118   /* Look for the `<' that starts the template-argument-list.  */
8119   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8120     return error_mark_node;
8121
8122   /* [temp.names]
8123
8124      When parsing a template-id, the first non-nested `>' is taken as
8125      the end of the template-argument-list rather than a greater-than
8126      operator.  */
8127   saved_greater_than_is_operator_p 
8128     = parser->greater_than_is_operator_p;
8129   parser->greater_than_is_operator_p = false;
8130   /* Parsing the argument list may modify SCOPE, so we save it
8131      here.  */
8132   saved_scope = parser->scope;
8133   saved_qualifying_scope = parser->qualifying_scope;
8134   saved_object_scope = parser->object_scope;
8135   /* Parse the template-argument-list itself.  */
8136   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8137     arguments = NULL_TREE;
8138   else
8139     arguments = cp_parser_template_argument_list (parser);
8140   /* Look for the `>' that ends the template-argument-list.  */
8141   cp_parser_require (parser, CPP_GREATER, "`>'");
8142   /* The `>' token might be a greater-than operator again now.  */
8143   parser->greater_than_is_operator_p 
8144     = saved_greater_than_is_operator_p;
8145   /* Restore the SAVED_SCOPE.  */
8146   parser->scope = saved_scope;
8147   parser->qualifying_scope = saved_qualifying_scope;
8148   parser->object_scope = saved_object_scope;
8149
8150   /* Build a representation of the specialization.  */
8151   if (TREE_CODE (template) == IDENTIFIER_NODE)
8152     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8153   else if (DECL_CLASS_TEMPLATE_P (template)
8154            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8155     template_id 
8156       = finish_template_type (template, arguments, 
8157                               cp_lexer_next_token_is (parser->lexer, 
8158                                                       CPP_SCOPE));
8159   else
8160     {
8161       /* If it's not a class-template or a template-template, it should be
8162          a function-template.  */
8163       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8164                            || TREE_CODE (template) == OVERLOAD
8165                            || BASELINK_P (template)),
8166                           20010716);
8167       
8168       template_id = lookup_template_function (template, arguments);
8169     }
8170   
8171   /* If parsing tentatively, replace the sequence of tokens that makes
8172      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8173      should we re-parse the token stream, we will not have to repeat
8174      the effort required to do the parse, nor will we issue duplicate
8175      error messages about problems during instantiation of the
8176      template.  */
8177   if (start_of_id >= 0)
8178     {
8179       cp_token *token;
8180       tree c;
8181
8182       /* Find the token that corresponds to the start of the
8183          template-id.  */
8184       token = cp_lexer_advance_token (parser->lexer, 
8185                                       parser->lexer->first_token,
8186                                       start_of_id);
8187
8188       /* Remember the access checks associated with this
8189          nested-name-specifier.  */
8190       c = parser->context->deferred_access_checks;
8191       if (c == access_check)
8192         access_check = NULL_TREE;
8193       else
8194         {
8195           while (TREE_CHAIN (c) != access_check)
8196             c = TREE_CHAIN (c);
8197           access_check = parser->context->deferred_access_checks;
8198           parser->context->deferred_access_checks = TREE_CHAIN (c);
8199           TREE_CHAIN (c) = NULL_TREE;
8200         }
8201
8202       /* Reset the contents of the START_OF_ID token.  */
8203       token->type = CPP_TEMPLATE_ID;
8204       token->value = build_tree_list (access_check, template_id);
8205       token->keyword = RID_MAX;
8206       /* Purge all subsequent tokens.  */
8207       cp_lexer_purge_tokens_after (parser->lexer, token);
8208     }
8209
8210   return template_id;
8211 }
8212
8213 /* Parse a template-name.
8214
8215    template-name:
8216      identifier
8217  
8218    The standard should actually say:
8219
8220    template-name:
8221      identifier
8222      operator-function-id
8223      conversion-function-id
8224
8225    A defect report has been filed about this issue.
8226
8227    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8228    `template' keyword, in a construction like:
8229
8230      T::template f<3>()
8231
8232    In that case `f' is taken to be a template-name, even though there
8233    is no way of knowing for sure.
8234
8235    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8236    name refers to a set of overloaded functions, at least one of which
8237    is a template, or an IDENTIFIER_NODE with the name of the template,
8238    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8239    names are looked up inside uninstantiated templates.  */
8240
8241 static tree
8242 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8243      cp_parser *parser;
8244      bool template_keyword_p;
8245      bool check_dependency_p;
8246 {
8247   tree identifier;
8248   tree decl;
8249   tree fns;
8250
8251   /* If the next token is `operator', then we have either an
8252      operator-function-id or a conversion-function-id.  */
8253   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8254     {
8255       /* We don't know whether we're looking at an
8256          operator-function-id or a conversion-function-id.  */
8257       cp_parser_parse_tentatively (parser);
8258       /* Try an operator-function-id.  */
8259       identifier = cp_parser_operator_function_id (parser);
8260       /* If that didn't work, try a conversion-function-id.  */
8261       if (!cp_parser_parse_definitely (parser))
8262         identifier = cp_parser_conversion_function_id (parser);
8263     }
8264   /* Look for the identifier.  */
8265   else
8266     identifier = cp_parser_identifier (parser);
8267   
8268   /* If we didn't find an identifier, we don't have a template-id.  */
8269   if (identifier == error_mark_node)
8270     return error_mark_node;
8271
8272   /* If the name immediately followed the `template' keyword, then it
8273      is a template-name.  However, if the next token is not `<', then
8274      we do not treat it as a template-name, since it is not being used
8275      as part of a template-id.  This enables us to handle constructs
8276      like:
8277
8278        template <typename T> struct S { S(); };
8279        template <typename T> S<T>::S();
8280
8281      correctly.  We would treat `S' as a template -- if it were `S<T>'
8282      -- but we do not if there is no `<'.  */
8283   if (template_keyword_p && processing_template_decl
8284       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8285     return identifier;
8286
8287   /* Look up the name.  */
8288   decl = cp_parser_lookup_name (parser, identifier,
8289                                 /*check_access=*/true,
8290                                 /*is_type=*/false,
8291                                 /*is_namespace=*/false,
8292                                 check_dependency_p);
8293   decl = maybe_get_template_decl_from_type_decl (decl);
8294
8295   /* If DECL is a template, then the name was a template-name.  */
8296   if (TREE_CODE (decl) == TEMPLATE_DECL)
8297     ;
8298   else 
8299     {
8300       /* The standard does not explicitly indicate whether a name that
8301          names a set of overloaded declarations, some of which are
8302          templates, is a template-name.  However, such a name should
8303          be a template-name; otherwise, there is no way to form a
8304          template-id for the overloaded templates.  */
8305       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8306       if (TREE_CODE (fns) == OVERLOAD)
8307         {
8308           tree fn;
8309           
8310           for (fn = fns; fn; fn = OVL_NEXT (fn))
8311             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8312               break;
8313         }
8314       else
8315         {
8316           /* Otherwise, the name does not name a template.  */
8317           cp_parser_error (parser, "expected template-name");
8318           return error_mark_node;
8319         }
8320     }
8321
8322   /* If DECL is dependent, and refers to a function, then just return
8323      its name; we will look it up again during template instantiation.  */
8324   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8325     {
8326       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8327       if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8328         return identifier;
8329     }
8330
8331   return decl;
8332 }
8333
8334 /* Parse a template-argument-list.
8335
8336    template-argument-list:
8337      template-argument
8338      template-argument-list , template-argument
8339
8340    Returns a TREE_LIST representing the arguments, in the order they
8341    appeared.  The TREE_VALUE of each node is a representation of the
8342    argument.  */
8343
8344 static tree
8345 cp_parser_template_argument_list (parser)
8346      cp_parser *parser;
8347 {
8348   tree arguments = NULL_TREE;
8349
8350   while (true)
8351     {
8352       tree argument;
8353
8354       /* Parse the template-argument.  */
8355       argument = cp_parser_template_argument (parser);
8356       /* Add it to the list.  */
8357       arguments = tree_cons (NULL_TREE, argument, arguments);
8358       /* If it is not a `,', then there are no more arguments.  */
8359       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8360         break;
8361       /* Otherwise, consume the ','.  */
8362       cp_lexer_consume_token (parser->lexer);
8363     }
8364
8365   /* We built up the arguments in reverse order.  */
8366   return nreverse (arguments);
8367 }
8368
8369 /* Parse a template-argument.
8370
8371    template-argument:
8372      assignment-expression
8373      type-id
8374      id-expression
8375
8376    The representation is that of an assignment-expression, type-id, or
8377    id-expression -- except that the qualified id-expression is
8378    evaluated, so that the value returned is either a DECL or an
8379    OVERLOAD.  */
8380
8381 static tree
8382 cp_parser_template_argument (parser)
8383      cp_parser *parser;
8384 {
8385   tree argument;
8386   bool template_p;
8387
8388   /* There's really no way to know what we're looking at, so we just
8389      try each alternative in order.  
8390
8391        [temp.arg]
8392
8393        In a template-argument, an ambiguity between a type-id and an
8394        expression is resolved to a type-id, regardless of the form of
8395        the corresponding template-parameter.  
8396
8397      Therefore, we try a type-id first.  */
8398   cp_parser_parse_tentatively (parser);
8399   argument = cp_parser_type_id (parser);
8400   /* If the next token isn't a `,' or a `>', then this argument wasn't
8401      really finished.  */
8402   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8403       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8404     cp_parser_error (parser, "expected template-argument");
8405   /* If that worked, we're done.  */
8406   if (cp_parser_parse_definitely (parser))
8407     return argument;
8408   /* We're still not sure what the argument will be.  */
8409   cp_parser_parse_tentatively (parser);
8410   /* Try a template.  */
8411   argument = cp_parser_id_expression (parser, 
8412                                       /*template_keyword_p=*/false,
8413                                       /*check_dependency_p=*/true,
8414                                       &template_p);
8415   /* If the next token isn't a `,' or a `>', then this argument wasn't
8416      really finished.  */
8417   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8418       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8419     cp_parser_error (parser, "expected template-argument");
8420   if (!cp_parser_error_occurred (parser))
8421     {
8422       /* Figure out what is being referred to.  */
8423       argument = cp_parser_lookup_name_simple (parser, argument);
8424       if (template_p)
8425         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8426                                                 TREE_OPERAND (argument, 1),
8427                                                 tf_error | tf_parsing);
8428       else if (TREE_CODE (argument) != TEMPLATE_DECL)
8429         cp_parser_error (parser, "expected template-name");
8430     }
8431   if (cp_parser_parse_definitely (parser))
8432     return argument;
8433   /* It must be an assignment-expression.  */
8434   return cp_parser_assignment_expression (parser);
8435 }
8436
8437 /* Parse an explicit-instantiation.
8438
8439    explicit-instantiation:
8440      template declaration  
8441
8442    Although the standard says `declaration', what it really means is:
8443
8444    explicit-instantiation:
8445      template decl-specifier-seq [opt] declarator [opt] ; 
8446
8447    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8448    supposed to be allowed.  A defect report has been filed about this
8449    issue.  
8450
8451    GNU Extension:
8452   
8453    explicit-instantiation:
8454      storage-class-specifier template 
8455        decl-specifier-seq [opt] declarator [opt] ;
8456      function-specifier template 
8457        decl-specifier-seq [opt] declarator [opt] ;  */
8458
8459 static void
8460 cp_parser_explicit_instantiation (parser)
8461      cp_parser *parser;
8462 {
8463   bool declares_class_or_enum;
8464   tree decl_specifiers;
8465   tree attributes;
8466   tree extension_specifier = NULL_TREE;
8467
8468   /* Look for an (optional) storage-class-specifier or
8469      function-specifier.  */
8470   if (cp_parser_allow_gnu_extensions_p (parser))
8471     {
8472       extension_specifier 
8473         = cp_parser_storage_class_specifier_opt (parser);
8474       if (!extension_specifier)
8475         extension_specifier = cp_parser_function_specifier_opt (parser);
8476     }
8477
8478   /* Look for the `template' keyword.  */
8479   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8480   /* Let the front end know that we are processing an explicit
8481      instantiation.  */
8482   begin_explicit_instantiation ();
8483   /* [temp.explicit] says that we are supposed to ignore access
8484      control while processing explicit instantiation directives.  */
8485   scope_chain->check_access = 0;
8486   /* Parse a decl-specifier-seq.  */
8487   decl_specifiers 
8488     = cp_parser_decl_specifier_seq (parser,
8489                                     CP_PARSER_FLAGS_OPTIONAL,
8490                                     &attributes,
8491                                     &declares_class_or_enum);
8492   /* If there was exactly one decl-specifier, and it declared a class,
8493      and there's no declarator, then we have an explicit type
8494      instantiation.  */
8495   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8496     {
8497       tree type;
8498
8499       type = check_tag_decl (decl_specifiers);
8500       if (type)
8501         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8502     }
8503   else
8504     {
8505       tree declarator;
8506       tree decl;
8507
8508       /* Parse the declarator.  */
8509       declarator 
8510         = cp_parser_declarator (parser, 
8511                                 /*abstract_p=*/false, 
8512                                 /*ctor_dtor_or_conv_p=*/NULL);
8513       decl = grokdeclarator (declarator, decl_specifiers, 
8514                              NORMAL, 0, NULL);
8515       /* Do the explicit instantiation.  */
8516       do_decl_instantiation (decl, extension_specifier);
8517     }
8518   /* We're done with the instantiation.  */
8519   end_explicit_instantiation ();
8520   /* Trun access control back on.  */
8521   scope_chain->check_access = flag_access_control;
8522
8523   /* Look for the trailing `;'.  */
8524   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8525 }
8526
8527 /* Parse an explicit-specialization.
8528
8529    explicit-specialization:
8530      template < > declaration  
8531
8532    Although the standard says `declaration', what it really means is:
8533
8534    explicit-specialization:
8535      template <> decl-specifier [opt] init-declarator [opt] ;
8536      template <> function-definition 
8537      template <> explicit-specialization
8538      template <> template-declaration  */
8539
8540 static void
8541 cp_parser_explicit_specialization (parser)
8542      cp_parser *parser;
8543 {
8544   /* Look for the `template' keyword.  */
8545   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8546   /* Look for the `<'.  */
8547   cp_parser_require (parser, CPP_LESS, "`<'");
8548   /* Look for the `>'.  */
8549   cp_parser_require (parser, CPP_GREATER, "`>'");
8550   /* We have processed another parameter list.  */
8551   ++parser->num_template_parameter_lists;
8552   /* Let the front end know that we are beginning a specialization.  */
8553   begin_specialization ();
8554
8555   /* If the next keyword is `template', we need to figure out whether
8556      or not we're looking a template-declaration.  */
8557   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8558     {
8559       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8560           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8561         cp_parser_template_declaration_after_export (parser,
8562                                                      /*member_p=*/false);
8563       else
8564         cp_parser_explicit_specialization (parser);
8565     }
8566   else
8567     /* Parse the dependent declaration.  */
8568     cp_parser_single_declaration (parser, 
8569                                   /*member_p=*/false,
8570                                   /*friend_p=*/NULL);
8571
8572   /* We're done with the specialization.  */
8573   end_specialization ();
8574   /* We're done with this parameter list.  */
8575   --parser->num_template_parameter_lists;
8576 }
8577
8578 /* Parse a type-specifier.
8579
8580    type-specifier:
8581      simple-type-specifier
8582      class-specifier
8583      enum-specifier
8584      elaborated-type-specifier
8585      cv-qualifier
8586
8587    GNU Extension:
8588
8589    type-specifier:
8590      __complex__
8591
8592    Returns a representation of the type-specifier.  If the
8593    type-specifier is a keyword (like `int' or `const', or
8594    `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8595    For a class-specifier, enum-specifier, or elaborated-type-specifier
8596    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8597
8598    If IS_FRIEND is TRUE then this type-specifier is being declared a
8599    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8600    appearing in a decl-specifier-seq.
8601
8602    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8603    class-specifier, enum-specifier, or elaborated-type-specifier, then
8604    *DECLARES_CLASS_OR_ENUM is set to TRUE.  Otherwise, it is set to
8605    FALSE.
8606
8607    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8608    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8609    is set to FALSE.  */
8610
8611 static tree
8612 cp_parser_type_specifier (parser, 
8613                           flags, 
8614                           is_friend,
8615                           is_declaration,
8616                           declares_class_or_enum,
8617                           is_cv_qualifier)
8618      cp_parser *parser;
8619      cp_parser_flags flags;
8620      bool is_friend;
8621      bool is_declaration;
8622      bool *declares_class_or_enum;
8623      bool *is_cv_qualifier;
8624 {
8625   tree type_spec = NULL_TREE;
8626   cp_token *token;
8627   enum rid keyword;
8628
8629   /* Assume this type-specifier does not declare a new type.  */
8630   if (declares_class_or_enum)
8631     *declares_class_or_enum = false;
8632   /* And that it does not specify a cv-qualifier.  */
8633   if (is_cv_qualifier)
8634     *is_cv_qualifier = false;
8635   /* Peek at the next token.  */
8636   token = cp_lexer_peek_token (parser->lexer);
8637
8638   /* If we're looking at a keyword, we can use that to guide the
8639      production we choose.  */
8640   keyword = token->keyword;
8641   switch (keyword)
8642     {
8643       /* Any of these indicate either a class-specifier, or an
8644          elaborated-type-specifier.  */
8645     case RID_CLASS:
8646     case RID_STRUCT:
8647     case RID_UNION:
8648     case RID_ENUM:
8649       /* Parse tentatively so that we can back up if we don't find a
8650          class-specifier or enum-specifier.  */
8651       cp_parser_parse_tentatively (parser);
8652       /* Look for the class-specifier or enum-specifier.  */
8653       if (keyword == RID_ENUM)
8654         type_spec = cp_parser_enum_specifier (parser);
8655       else
8656         type_spec = cp_parser_class_specifier (parser);
8657
8658       /* If that worked, we're done.  */
8659       if (cp_parser_parse_definitely (parser))
8660         {
8661           if (declares_class_or_enum)
8662             *declares_class_or_enum = true;
8663           return type_spec;
8664         }
8665
8666       /* Fall through.  */
8667
8668     case RID_TYPENAME:
8669       /* Look for an elaborated-type-specifier.  */
8670       type_spec = cp_parser_elaborated_type_specifier (parser,
8671                                                        is_friend,
8672                                                        is_declaration);
8673       /* We're declaring a class or enum -- unless we're using
8674          `typename'.  */
8675       if (declares_class_or_enum && keyword != RID_TYPENAME)
8676         *declares_class_or_enum = true;
8677       return type_spec;
8678
8679     case RID_CONST:
8680     case RID_VOLATILE:
8681     case RID_RESTRICT:
8682       type_spec = cp_parser_cv_qualifier_opt (parser);
8683       /* Even though we call a routine that looks for an optional
8684          qualifier, we know that there should be one.  */
8685       my_friendly_assert (type_spec != NULL, 20000328);
8686       /* This type-specifier was a cv-qualified.  */
8687       if (is_cv_qualifier)
8688         *is_cv_qualifier = true;
8689
8690       return type_spec;
8691
8692     case RID_COMPLEX:
8693       /* The `__complex__' keyword is a GNU extension.  */
8694       return cp_lexer_consume_token (parser->lexer)->value;
8695
8696     default:
8697       break;
8698     }
8699
8700   /* If we do not already have a type-specifier, assume we are looking
8701      at a simple-type-specifier.  */
8702   type_spec = cp_parser_simple_type_specifier (parser, flags);
8703
8704   /* If we didn't find a type-specifier, and a type-specifier was not
8705      optional in this context, issue an error message.  */
8706   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8707     {
8708       cp_parser_error (parser, "expected type specifier");
8709       return error_mark_node;
8710     }
8711
8712   return type_spec;
8713 }
8714
8715 /* Parse a simple-type-specifier.
8716
8717    simple-type-specifier:
8718      :: [opt] nested-name-specifier [opt] type-name
8719      :: [opt] nested-name-specifier template template-id
8720      char
8721      wchar_t
8722      bool
8723      short
8724      int
8725      long
8726      signed
8727      unsigned
8728      float
8729      double
8730      void  
8731
8732    GNU Extension:
8733
8734    simple-type-specifier:
8735      __typeof__ unary-expression
8736      __typeof__ ( type-id )
8737
8738    For the various keywords, the value returned is simply the
8739    TREE_IDENTIFIER representing the keyword.  For the first two
8740    productions, the value returned is the indicated TYPE_DECL.  */
8741
8742 static tree
8743 cp_parser_simple_type_specifier (parser, flags)
8744      cp_parser *parser;
8745      cp_parser_flags flags;
8746 {
8747   tree type = NULL_TREE;
8748   cp_token *token;
8749
8750   /* Peek at the next token.  */
8751   token = cp_lexer_peek_token (parser->lexer);
8752
8753   /* If we're looking at a keyword, things are easy.  */
8754   switch (token->keyword)
8755     {
8756     case RID_CHAR:
8757     case RID_WCHAR:
8758     case RID_BOOL:
8759     case RID_SHORT:
8760     case RID_INT:
8761     case RID_LONG:
8762     case RID_SIGNED:
8763     case RID_UNSIGNED:
8764     case RID_FLOAT:
8765     case RID_DOUBLE:
8766     case RID_VOID:
8767       /* Consume the token.  */
8768       return cp_lexer_consume_token (parser->lexer)->value;
8769
8770     case RID_TYPEOF:
8771       {
8772         tree operand;
8773
8774         /* Consume the `typeof' token.  */
8775         cp_lexer_consume_token (parser->lexer);
8776         /* Parse the operand to `typeof'  */
8777         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8778         /* If it is not already a TYPE, take its type.  */
8779         if (!TYPE_P (operand))
8780           operand = finish_typeof (operand);
8781
8782         return operand;
8783       }
8784
8785     default:
8786       break;
8787     }
8788
8789   /* The type-specifier must be a user-defined type.  */
8790   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8791     {
8792       /* Don't gobble tokens or issue error messages if this is an
8793          optional type-specifier.  */
8794       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8795         cp_parser_parse_tentatively (parser);
8796
8797       /* Look for the optional `::' operator.  */
8798       cp_parser_global_scope_opt (parser,
8799                                   /*current_scope_valid_p=*/false);
8800       /* Look for the nested-name specifier.  */
8801       cp_parser_nested_name_specifier_opt (parser,
8802                                            /*typename_keyword_p=*/false,
8803                                            /*check_dependency_p=*/true,
8804                                            /*type_p=*/false);
8805       /* If we have seen a nested-name-specifier, and the next token
8806          is `template', then we are using the template-id production.  */
8807       if (parser->scope 
8808           && cp_parser_optional_template_keyword (parser))
8809         {
8810           /* Look for the template-id.  */
8811           type = cp_parser_template_id (parser, 
8812                                         /*template_keyword_p=*/true,
8813                                         /*check_dependency_p=*/true);
8814           /* If the template-id did not name a type, we are out of
8815              luck.  */
8816           if (TREE_CODE (type) != TYPE_DECL)
8817             {
8818               cp_parser_error (parser, "expected template-id for type");
8819               type = NULL_TREE;
8820             }
8821         }
8822       /* Otherwise, look for a type-name.  */
8823       else
8824         {
8825           type = cp_parser_type_name (parser);
8826           if (type == error_mark_node)
8827             type = NULL_TREE;
8828         }
8829
8830       /* If it didn't work out, we don't have a TYPE.  */
8831       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8832           && !cp_parser_parse_definitely (parser))
8833         type = NULL_TREE;
8834     }
8835
8836   /* If we didn't get a type-name, issue an error message.  */
8837   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8838     {
8839       cp_parser_error (parser, "expected type-name");
8840       return error_mark_node;
8841     }
8842
8843   return type;
8844 }
8845
8846 /* Parse a type-name.
8847
8848    type-name:
8849      class-name
8850      enum-name
8851      typedef-name  
8852
8853    enum-name:
8854      identifier
8855
8856    typedef-name:
8857      identifier 
8858
8859    Returns a TYPE_DECL for the the type.  */
8860
8861 static tree
8862 cp_parser_type_name (parser)
8863      cp_parser *parser;
8864 {
8865   tree type_decl;
8866   tree identifier;
8867
8868   /* We can't know yet whether it is a class-name or not.  */
8869   cp_parser_parse_tentatively (parser);
8870   /* Try a class-name.  */
8871   type_decl = cp_parser_class_name (parser, 
8872                                     /*typename_keyword_p=*/false,
8873                                     /*template_keyword_p=*/false,
8874                                     /*type_p=*/false,
8875                                     /*check_access_p=*/true,
8876                                     /*check_dependency_p=*/true,
8877                                     /*class_head_p=*/false);
8878   /* If it's not a class-name, keep looking.  */
8879   if (!cp_parser_parse_definitely (parser))
8880     {
8881       /* It must be a typedef-name or an enum-name.  */
8882       identifier = cp_parser_identifier (parser);
8883       if (identifier == error_mark_node)
8884         return error_mark_node;
8885       
8886       /* Look up the type-name.  */
8887       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8888       /* Issue an error if we did not find a type-name.  */
8889       if (TREE_CODE (type_decl) != TYPE_DECL)
8890         {
8891           cp_parser_error (parser, "expected type-name");
8892           type_decl = error_mark_node;
8893         }
8894       /* Remember that the name was used in the definition of the
8895          current class so that we can check later to see if the
8896          meaning would have been different after the class was
8897          entirely defined.  */
8898       else if (type_decl != error_mark_node
8899                && !parser->scope)
8900         maybe_note_name_used_in_class (identifier, type_decl);
8901     }
8902   
8903   return type_decl;
8904 }
8905
8906
8907 /* Parse an elaborated-type-specifier.  Note that the grammar given
8908    here incorporates the resolution to DR68.
8909
8910    elaborated-type-specifier:
8911      class-key :: [opt] nested-name-specifier [opt] identifier
8912      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8913      enum :: [opt] nested-name-specifier [opt] identifier
8914      typename :: [opt] nested-name-specifier identifier
8915      typename :: [opt] nested-name-specifier template [opt] 
8916        template-id 
8917
8918    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8919    declared `friend'.  If IS_DECLARATION is TRUE, then this
8920    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8921    something is being declared.
8922
8923    Returns the TYPE specified.  */
8924
8925 static tree
8926 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8927      cp_parser *parser;
8928      bool is_friend;
8929      bool is_declaration;
8930 {
8931   enum tag_types tag_type;
8932   tree identifier;
8933   tree type = NULL_TREE;
8934
8935   /* See if we're looking at the `enum' keyword.  */
8936   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8937     {
8938       /* Consume the `enum' token.  */
8939       cp_lexer_consume_token (parser->lexer);
8940       /* Remember that it's an enumeration type.  */
8941       tag_type = enum_type;
8942     }
8943   /* Or, it might be `typename'.  */
8944   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8945                                            RID_TYPENAME))
8946     {
8947       /* Consume the `typename' token.  */
8948       cp_lexer_consume_token (parser->lexer);
8949       /* Remember that it's a `typename' type.  */
8950       tag_type = typename_type;
8951       /* The `typename' keyword is only allowed in templates.  */
8952       if (!processing_template_decl)
8953         pedwarn ("using `typename' outside of template");
8954     }
8955   /* Otherwise it must be a class-key.  */
8956   else
8957     {
8958       tag_type = cp_parser_class_key (parser);
8959       if (tag_type == none_type)
8960         return error_mark_node;
8961     }
8962
8963   /* Look for the `::' operator.  */
8964   cp_parser_global_scope_opt (parser, 
8965                               /*current_scope_valid_p=*/false);
8966   /* Look for the nested-name-specifier.  */
8967   if (tag_type == typename_type)
8968     cp_parser_nested_name_specifier (parser,
8969                                      /*typename_keyword_p=*/true,
8970                                      /*check_dependency_p=*/true,
8971                                      /*type_p=*/true);
8972   else
8973     /* Even though `typename' is not present, the proposed resolution
8974        to Core Issue 180 says that in `class A<T>::B', `B' should be
8975        considered a type-name, even if `A<T>' is dependent.  */
8976     cp_parser_nested_name_specifier_opt (parser,
8977                                          /*typename_keyword_p=*/true,
8978                                          /*check_dependency_p=*/true,
8979                                          /*type_p=*/true);
8980   /* For everything but enumeration types, consider a template-id.  */
8981   if (tag_type != enum_type)
8982     {
8983       bool template_p = false;
8984       tree decl;
8985
8986       /* Allow the `template' keyword.  */
8987       template_p = cp_parser_optional_template_keyword (parser);
8988       /* If we didn't see `template', we don't know if there's a
8989          template-id or not.  */
8990       if (!template_p)
8991         cp_parser_parse_tentatively (parser);
8992       /* Parse the template-id.  */
8993       decl = cp_parser_template_id (parser, template_p,
8994                                     /*check_dependency_p=*/true);
8995       /* If we didn't find a template-id, look for an ordinary
8996          identifier.  */
8997       if (!template_p && !cp_parser_parse_definitely (parser))
8998         ;
8999       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9000          in effect, then we must assume that, upon instantiation, the
9001          template will correspond to a class.  */
9002       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9003                && tag_type == typename_type)
9004         type = make_typename_type (parser->scope, decl,
9005                                    /*complain=*/1);
9006       else 
9007         type = TREE_TYPE (decl);
9008     }
9009
9010   /* For an enumeration type, consider only a plain identifier.  */
9011   if (!type)
9012     {
9013       identifier = cp_parser_identifier (parser);
9014
9015       if (identifier == error_mark_node)
9016         return error_mark_node;
9017
9018       /* For a `typename', we needn't call xref_tag.  */
9019       if (tag_type == typename_type)
9020         return make_typename_type (parser->scope, identifier, 
9021                                    /*complain=*/1);
9022       /* Look up a qualified name in the usual way.  */
9023       if (parser->scope)
9024         {
9025           tree decl;
9026
9027           /* In an elaborated-type-specifier, names are assumed to name
9028              types, so we set IS_TYPE to TRUE when calling
9029              cp_parser_lookup_name.  */
9030           decl = cp_parser_lookup_name (parser, identifier, 
9031                                         /*check_access=*/true,
9032                                         /*is_type=*/true,
9033                                         /*is_namespace=*/false,
9034                                         /*check_dependency=*/true);
9035           decl = (cp_parser_maybe_treat_template_as_class 
9036                   (decl, /*tag_name_p=*/is_friend));
9037
9038           if (TREE_CODE (decl) != TYPE_DECL)
9039             {
9040               error ("expected type-name");
9041               return error_mark_node;
9042             }
9043           else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9044                    && tag_type != enum_type)
9045             error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9046                    tag_type == record_type ? "struct" : "class");
9047           else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9048                    && tag_type == enum_type)
9049             error ("`%T' referred to as enum", TREE_TYPE (decl));
9050
9051           type = TREE_TYPE (decl);
9052         }
9053       else 
9054         {
9055           /* An elaborated-type-specifier sometimes introduces a new type and
9056              sometimes names an existing type.  Normally, the rule is that it
9057              introduces a new type only if there is not an existing type of
9058              the same name already in scope.  For example, given:
9059
9060                struct S {};
9061                void f() { struct S s; }
9062
9063              the `struct S' in the body of `f' is the same `struct S' as in
9064              the global scope; the existing definition is used.  However, if
9065              there were no global declaration, this would introduce a new 
9066              local class named `S'.
9067
9068              An exception to this rule applies to the following code:
9069
9070                namespace N { struct S; }
9071
9072              Here, the elaborated-type-specifier names a new type
9073              unconditionally; even if there is already an `S' in the
9074              containing scope this declaration names a new type.
9075              This exception only applies if the elaborated-type-specifier
9076              forms the complete declaration:
9077
9078                [class.name] 
9079
9080                A declaration consisting solely of `class-key identifier ;' is
9081                either a redeclaration of the name in the current scope or a
9082                forward declaration of the identifier as a class name.  It
9083                introduces the name into the current scope.
9084
9085              We are in this situation precisely when the next token is a `;'.
9086
9087              An exception to the exception is that a `friend' declaration does
9088              *not* name a new type; i.e., given:
9089
9090                struct S { friend struct T; };
9091
9092              `T' is not a new type in the scope of `S'.  
9093
9094              Also, `new struct S' or `sizeof (struct S)' never results in the
9095              definition of a new type; a new type can only be declared in a
9096              declaration context.   */
9097
9098           type = xref_tag (tag_type, identifier, 
9099                            /*attributes=*/NULL_TREE,
9100                            (is_friend 
9101                             || !is_declaration
9102                             || cp_lexer_next_token_is_not (parser->lexer, 
9103                                                            CPP_SEMICOLON)));
9104         }
9105     }
9106   if (tag_type != enum_type)
9107     cp_parser_check_class_key (tag_type, type);
9108   return type;
9109 }
9110
9111 /* Parse an enum-specifier.
9112
9113    enum-specifier:
9114      enum identifier [opt] { enumerator-list [opt] }
9115
9116    Returns an ENUM_TYPE representing the enumeration.  */
9117
9118 static tree
9119 cp_parser_enum_specifier (parser)
9120      cp_parser *parser;
9121 {
9122   cp_token *token;
9123   tree identifier = NULL_TREE;
9124   tree type;
9125
9126   /* Look for the `enum' keyword.  */
9127   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9128     return error_mark_node;
9129   /* Peek at the next token.  */
9130   token = cp_lexer_peek_token (parser->lexer);
9131
9132   /* See if it is an identifier.  */
9133   if (token->type == CPP_NAME)
9134     identifier = cp_parser_identifier (parser);
9135
9136   /* Look for the `{'.  */
9137   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9138     return error_mark_node;
9139
9140   /* At this point, we're going ahead with the enum-specifier, even
9141      if some other problem occurs.  */
9142   cp_parser_commit_to_tentative_parse (parser);
9143
9144   /* Issue an error message if type-definitions are forbidden here.  */
9145   cp_parser_check_type_definition (parser);
9146
9147   /* Create the new type.  */
9148   type = start_enum (identifier ? identifier : make_anon_name ());
9149
9150   /* Peek at the next token.  */
9151   token = cp_lexer_peek_token (parser->lexer);
9152   /* If it's not a `}', then there are some enumerators.  */
9153   if (token->type != CPP_CLOSE_BRACE)
9154     cp_parser_enumerator_list (parser, type);
9155   /* Look for the `}'.  */
9156   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9157
9158   /* Finish up the enumeration.  */
9159   finish_enum (type);
9160
9161   return type;
9162 }
9163
9164 /* Parse an enumerator-list.  The enumerators all have the indicated
9165    TYPE.  
9166
9167    enumerator-list:
9168      enumerator-definition
9169      enumerator-list , enumerator-definition  */
9170
9171 static void
9172 cp_parser_enumerator_list (parser, type)
9173      cp_parser *parser;
9174      tree type;
9175 {
9176   while (true)
9177     {
9178       cp_token *token;
9179
9180       /* Parse an enumerator-definition.  */
9181       cp_parser_enumerator_definition (parser, type);
9182       /* Peek at the next token.  */
9183       token = cp_lexer_peek_token (parser->lexer);
9184       /* If it's not a `,', then we've reached the end of the 
9185          list.  */
9186       if (token->type != CPP_COMMA)
9187         break;
9188       /* Otherwise, consume the `,' and keep going.  */
9189       cp_lexer_consume_token (parser->lexer);
9190       /* If the next token is a `}', there is a trailing comma.  */
9191       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9192         {
9193           if (pedantic && !in_system_header)
9194             pedwarn ("comma at end of enumerator list");
9195           break;
9196         }
9197     }
9198 }
9199
9200 /* Parse an enumerator-definition.  The enumerator has the indicated
9201    TYPE.
9202
9203    enumerator-definition:
9204      enumerator
9205      enumerator = constant-expression
9206     
9207    enumerator:
9208      identifier  */
9209
9210 static void
9211 cp_parser_enumerator_definition (parser, type)
9212      cp_parser *parser;
9213      tree type;
9214 {
9215   cp_token *token;
9216   tree identifier;
9217   tree value;
9218
9219   /* Look for the identifier.  */
9220   identifier = cp_parser_identifier (parser);
9221   if (identifier == error_mark_node)
9222     return;
9223   
9224   /* Peek at the next token.  */
9225   token = cp_lexer_peek_token (parser->lexer);
9226   /* If it's an `=', then there's an explicit value.  */
9227   if (token->type == CPP_EQ)
9228     {
9229       /* Consume the `=' token.  */
9230       cp_lexer_consume_token (parser->lexer);
9231       /* Parse the value.  */
9232       value = cp_parser_constant_expression (parser);
9233     }
9234   else
9235     value = NULL_TREE;
9236
9237   /* Create the enumerator.  */
9238   build_enumerator (identifier, value, type);
9239 }
9240
9241 /* Parse a namespace-name.
9242
9243    namespace-name:
9244      original-namespace-name
9245      namespace-alias
9246
9247    Returns the NAMESPACE_DECL for the namespace.  */
9248
9249 static tree
9250 cp_parser_namespace_name (parser)
9251      cp_parser *parser;
9252 {
9253   tree identifier;
9254   tree namespace_decl;
9255
9256   /* Get the name of the namespace.  */
9257   identifier = cp_parser_identifier (parser);
9258   if (identifier == error_mark_node)
9259     return error_mark_node;
9260
9261   /* Look up the identifier in the currently active scope.  Look only
9262      for namespaces, due to:
9263
9264        [basic.lookup.udir]
9265
9266        When looking up a namespace-name in a using-directive or alias
9267        definition, only namespace names are considered.  
9268
9269      And:
9270
9271        [basic.lookup.qual]
9272
9273        During the lookup of a name preceding the :: scope resolution
9274        operator, object, function, and enumerator names are ignored.  
9275
9276      (Note that cp_parser_class_or_namespace_name only calls this
9277      function if the token after the name is the scope resolution
9278      operator.)  */
9279   namespace_decl = cp_parser_lookup_name (parser, identifier,
9280                                           /*check_access=*/true,
9281                                           /*is_type=*/false,
9282                                           /*is_namespace=*/true,
9283                                           /*check_dependency=*/true);
9284   /* If it's not a namespace, issue an error.  */
9285   if (namespace_decl == error_mark_node
9286       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9287     {
9288       cp_parser_error (parser, "expected namespace-name");
9289       namespace_decl = error_mark_node;
9290     }
9291   
9292   return namespace_decl;
9293 }
9294
9295 /* Parse a namespace-definition.
9296
9297    namespace-definition:
9298      named-namespace-definition
9299      unnamed-namespace-definition  
9300
9301    named-namespace-definition:
9302      original-namespace-definition
9303      extension-namespace-definition
9304
9305    original-namespace-definition:
9306      namespace identifier { namespace-body }
9307    
9308    extension-namespace-definition:
9309      namespace original-namespace-name { namespace-body }
9310  
9311    unnamed-namespace-definition:
9312      namespace { namespace-body } */
9313
9314 static void
9315 cp_parser_namespace_definition (parser)
9316      cp_parser *parser;
9317 {
9318   tree identifier;
9319
9320   /* Look for the `namespace' keyword.  */
9321   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9322
9323   /* Get the name of the namespace.  We do not attempt to distinguish
9324      between an original-namespace-definition and an
9325      extension-namespace-definition at this point.  The semantic
9326      analysis routines are responsible for that.  */
9327   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9328     identifier = cp_parser_identifier (parser);
9329   else
9330     identifier = NULL_TREE;
9331
9332   /* Look for the `{' to start the namespace.  */
9333   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9334   /* Start the namespace.  */
9335   push_namespace (identifier);
9336   /* Parse the body of the namespace.  */
9337   cp_parser_namespace_body (parser);
9338   /* Finish the namespace.  */
9339   pop_namespace ();
9340   /* Look for the final `}'.  */
9341   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9342 }
9343
9344 /* Parse a namespace-body.
9345
9346    namespace-body:
9347      declaration-seq [opt]  */
9348
9349 static void
9350 cp_parser_namespace_body (parser)
9351      cp_parser *parser;
9352 {
9353   cp_parser_declaration_seq_opt (parser);
9354 }
9355
9356 /* Parse a namespace-alias-definition.
9357
9358    namespace-alias-definition:
9359      namespace identifier = qualified-namespace-specifier ;  */
9360
9361 static void
9362 cp_parser_namespace_alias_definition (parser)
9363      cp_parser *parser;
9364 {
9365   tree identifier;
9366   tree namespace_specifier;
9367
9368   /* Look for the `namespace' keyword.  */
9369   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9370   /* Look for the identifier.  */
9371   identifier = cp_parser_identifier (parser);
9372   if (identifier == error_mark_node)
9373     return;
9374   /* Look for the `=' token.  */
9375   cp_parser_require (parser, CPP_EQ, "`='");
9376   /* Look for the qualified-namespace-specifier.  */
9377   namespace_specifier 
9378     = cp_parser_qualified_namespace_specifier (parser);
9379   /* Look for the `;' token.  */
9380   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9381
9382   /* Register the alias in the symbol table.  */
9383   do_namespace_alias (identifier, namespace_specifier);
9384 }
9385
9386 /* Parse a qualified-namespace-specifier.
9387
9388    qualified-namespace-specifier:
9389      :: [opt] nested-name-specifier [opt] namespace-name
9390
9391    Returns a NAMESPACE_DECL corresponding to the specified
9392    namespace.  */
9393
9394 static tree
9395 cp_parser_qualified_namespace_specifier (parser)
9396      cp_parser *parser;
9397 {
9398   /* Look for the optional `::'.  */
9399   cp_parser_global_scope_opt (parser, 
9400                               /*current_scope_valid_p=*/false);
9401
9402   /* Look for the optional nested-name-specifier.  */
9403   cp_parser_nested_name_specifier_opt (parser,
9404                                        /*typename_keyword_p=*/false,
9405                                        /*check_dependency_p=*/true,
9406                                        /*type_p=*/false);
9407
9408   return cp_parser_namespace_name (parser);
9409 }
9410
9411 /* Parse a using-declaration.
9412
9413    using-declaration:
9414      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9415      using :: unqualified-id ;  */
9416
9417 static void
9418 cp_parser_using_declaration (parser)
9419      cp_parser *parser;
9420 {
9421   cp_token *token;
9422   bool typename_p = false;
9423   bool global_scope_p;
9424   tree decl;
9425   tree identifier;
9426   tree scope;
9427
9428   /* Look for the `using' keyword.  */
9429   cp_parser_require_keyword (parser, RID_USING, "`using'");
9430   
9431   /* Peek at the next token.  */
9432   token = cp_lexer_peek_token (parser->lexer);
9433   /* See if it's `typename'.  */
9434   if (token->keyword == RID_TYPENAME)
9435     {
9436       /* Remember that we've seen it.  */
9437       typename_p = true;
9438       /* Consume the `typename' token.  */
9439       cp_lexer_consume_token (parser->lexer);
9440     }
9441
9442   /* Look for the optional global scope qualification.  */
9443   global_scope_p 
9444     = (cp_parser_global_scope_opt (parser,
9445                                    /*current_scope_valid_p=*/false) 
9446        != NULL_TREE);
9447
9448   /* If we saw `typename', or didn't see `::', then there must be a
9449      nested-name-specifier present.  */
9450   if (typename_p || !global_scope_p)
9451     cp_parser_nested_name_specifier (parser, typename_p, 
9452                                      /*check_dependency_p=*/true,
9453                                      /*type_p=*/false);
9454   /* Otherwise, we could be in either of the two productions.  In that
9455      case, treat the nested-name-specifier as optional.  */
9456   else
9457     cp_parser_nested_name_specifier_opt (parser,
9458                                          /*typename_keyword_p=*/false,
9459                                          /*check_dependency_p=*/true,
9460                                          /*type_p=*/false);
9461
9462   /* Parse the unqualified-id.  */
9463   identifier = cp_parser_unqualified_id (parser, 
9464                                          /*template_keyword_p=*/false,
9465                                          /*check_dependency_p=*/true);
9466
9467   /* The function we call to handle a using-declaration is different
9468      depending on what scope we are in.  */
9469   scope = current_scope ();
9470   if (scope && TYPE_P (scope))
9471     {
9472       /* Create the USING_DECL.  */
9473       decl = do_class_using_decl (build_nt (SCOPE_REF,
9474                                             parser->scope,
9475                                             identifier));
9476       /* Add it to the list of members in this class.  */
9477       finish_member_declaration (decl);
9478     }
9479   else
9480     {
9481       decl = cp_parser_lookup_name_simple (parser, identifier);
9482       if (scope)
9483         do_local_using_decl (decl);
9484       else
9485         do_toplevel_using_decl (decl);
9486     }
9487
9488   /* Look for the final `;'.  */
9489   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9490 }
9491
9492 /* Parse a using-directive.  
9493  
9494    using-directive:
9495      using namespace :: [opt] nested-name-specifier [opt]
9496        namespace-name ;  */
9497
9498 static void
9499 cp_parser_using_directive (parser)
9500      cp_parser *parser;
9501 {
9502   tree namespace_decl;
9503
9504   /* Look for the `using' keyword.  */
9505   cp_parser_require_keyword (parser, RID_USING, "`using'");
9506   /* And the `namespace' keyword.  */
9507   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9508   /* Look for the optional `::' operator.  */
9509   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9510   /* And the optional nested-name-sepcifier.  */
9511   cp_parser_nested_name_specifier_opt (parser,
9512                                        /*typename_keyword_p=*/false,
9513                                        /*check_dependency_p=*/true,
9514                                        /*type_p=*/false);
9515   /* Get the namespace being used.  */
9516   namespace_decl = cp_parser_namespace_name (parser);
9517   /* Update the symbol table.  */
9518   do_using_directive (namespace_decl);
9519   /* Look for the final `;'.  */
9520   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9521 }
9522
9523 /* Parse an asm-definition.
9524
9525    asm-definition:
9526      asm ( string-literal ) ;  
9527
9528    GNU Extension:
9529
9530    asm-definition:
9531      asm volatile [opt] ( string-literal ) ;
9532      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9533      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9534                           : asm-operand-list [opt] ) ;
9535      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9536                           : asm-operand-list [opt] 
9537                           : asm-operand-list [opt] ) ;  */
9538
9539 static void
9540 cp_parser_asm_definition (parser)
9541      cp_parser *parser;
9542 {
9543   cp_token *token;
9544   tree string;
9545   tree outputs = NULL_TREE;
9546   tree inputs = NULL_TREE;
9547   tree clobbers = NULL_TREE;
9548   tree asm_stmt;
9549   bool volatile_p = false;
9550   bool extended_p = false;
9551
9552   /* Look for the `asm' keyword.  */
9553   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9554   /* See if the next token is `volatile'.  */
9555   if (cp_parser_allow_gnu_extensions_p (parser)
9556       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9557     {
9558       /* Remember that we saw the `volatile' keyword.  */
9559       volatile_p = true;
9560       /* Consume the token.  */
9561       cp_lexer_consume_token (parser->lexer);
9562     }
9563   /* Look for the opening `('.  */
9564   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9565   /* Look for the string.  */
9566   token = cp_parser_require (parser, CPP_STRING, "asm body");
9567   if (!token)
9568     return;
9569   string = token->value;
9570   /* If we're allowing GNU extensions, check for the extended assembly
9571      syntax.  Unfortunately, the `:' tokens need not be separated by 
9572      a space in C, and so, for compatibility, we tolerate that here
9573      too.  Doing that means that we have to treat the `::' operator as
9574      two `:' tokens.  */
9575   if (cp_parser_allow_gnu_extensions_p (parser)
9576       && at_function_scope_p ()
9577       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9578           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9579     {
9580       bool inputs_p = false;
9581       bool clobbers_p = false;
9582
9583       /* The extended syntax was used.  */
9584       extended_p = true;
9585
9586       /* Look for outputs.  */
9587       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9588         {
9589           /* Consume the `:'.  */
9590           cp_lexer_consume_token (parser->lexer);
9591           /* Parse the output-operands.  */
9592           if (cp_lexer_next_token_is_not (parser->lexer, 
9593                                           CPP_COLON)
9594               && cp_lexer_next_token_is_not (parser->lexer,
9595                                              CPP_SCOPE))
9596             outputs = cp_parser_asm_operand_list (parser);
9597         }
9598       /* If the next token is `::', there are no outputs, and the
9599          next token is the beginning of the inputs.  */
9600       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9601         {
9602           /* Consume the `::' token.  */
9603           cp_lexer_consume_token (parser->lexer);
9604           /* The inputs are coming next.  */
9605           inputs_p = true;
9606         }
9607
9608       /* Look for inputs.  */
9609       if (inputs_p
9610           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9611         {
9612           if (!inputs_p)
9613             /* Consume the `:'.  */
9614             cp_lexer_consume_token (parser->lexer);
9615           /* Parse the output-operands.  */
9616           if (cp_lexer_next_token_is_not (parser->lexer, 
9617                                           CPP_COLON)
9618               && cp_lexer_next_token_is_not (parser->lexer,
9619                                              CPP_SCOPE))
9620             inputs = cp_parser_asm_operand_list (parser);
9621         }
9622       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9623         /* The clobbers are coming next.  */
9624         clobbers_p = true;
9625
9626       /* Look for clobbers.  */
9627       if (clobbers_p 
9628           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9629         {
9630           if (!clobbers_p)
9631             /* Consume the `:'.  */
9632             cp_lexer_consume_token (parser->lexer);
9633           /* Parse the clobbers.  */
9634           clobbers = cp_parser_asm_clobber_list (parser);
9635         }
9636     }
9637   /* Look for the closing `)'.  */
9638   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9639     cp_parser_skip_to_closing_parenthesis (parser);
9640   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9641
9642   /* Create the ASM_STMT.  */
9643   if (at_function_scope_p ())
9644     {
9645       asm_stmt = 
9646         finish_asm_stmt (volatile_p 
9647                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9648                          string, outputs, inputs, clobbers);
9649       /* If the extended syntax was not used, mark the ASM_STMT.  */
9650       if (!extended_p)
9651         ASM_INPUT_P (asm_stmt) = 1;
9652     }
9653   else
9654     assemble_asm (string);
9655 }
9656
9657 /* Declarators [gram.dcl.decl] */
9658
9659 /* Parse an init-declarator.
9660
9661    init-declarator:
9662      declarator initializer [opt]
9663
9664    GNU Extension:
9665
9666    init-declarator:
9667      declarator asm-specification [opt] attributes [opt] initializer [opt]
9668
9669    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9670    Returns a reprsentation of the entity declared.  The ACCESS_CHECKS
9671    represent deferred access checks from the decl-specifier-seq.  If
9672    MEMBER_P is TRUE, then this declarator appears in a class scope.
9673    The new DECL created by this declarator is returned.
9674
9675    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9676    for a function-definition here as well.  If the declarator is a
9677    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9678    be TRUE upon return.  By that point, the function-definition will
9679    have been completely parsed.
9680
9681    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9682    is FALSE.  */
9683
9684 static tree
9685 cp_parser_init_declarator (parser, 
9686                            decl_specifiers, 
9687                            prefix_attributes,
9688                            access_checks,
9689                            function_definition_allowed_p,
9690                            member_p,
9691                            function_definition_p)
9692      cp_parser *parser;
9693      tree decl_specifiers;
9694      tree prefix_attributes;
9695      tree access_checks;
9696      bool function_definition_allowed_p;
9697      bool member_p;
9698      bool *function_definition_p;
9699 {
9700   cp_token *token;
9701   tree declarator;
9702   tree attributes;
9703   tree asm_specification;
9704   tree initializer;
9705   tree decl = NULL_TREE;
9706   tree scope;
9707   tree declarator_access_checks;
9708   bool is_initialized;
9709   bool is_parenthesized_init;
9710   bool ctor_dtor_or_conv_p;
9711   bool friend_p;
9712
9713   /* Assume that this is not the declarator for a function
9714      definition.  */
9715   if (function_definition_p)
9716     *function_definition_p = false;
9717
9718   /* Defer access checks while parsing the declarator; we cannot know
9719      what names are accessible until we know what is being 
9720      declared.  */
9721   cp_parser_start_deferring_access_checks (parser);
9722   /* Parse the declarator.  */
9723   declarator 
9724     = cp_parser_declarator (parser,
9725                             /*abstract_p=*/false,
9726                             &ctor_dtor_or_conv_p);
9727   /* Gather up the deferred checks.  */
9728   declarator_access_checks 
9729     = cp_parser_stop_deferring_access_checks (parser);
9730
9731   /* Prevent the access checks from being reclaimed by GC.  */
9732   parser->access_checks_lists
9733     = tree_cons (NULL_TREE, declarator_access_checks,
9734                  parser->access_checks_lists);
9735
9736   /* If the DECLARATOR was erroneous, there's no need to go
9737      further.  */
9738   if (declarator == error_mark_node)
9739     {
9740       /* Discard access checks no longer in use.  */
9741       parser->access_checks_lists
9742         = TREE_CHAIN (parser->access_checks_lists);
9743       return error_mark_node;
9744     }
9745
9746   /* Figure out what scope the entity declared by the DECLARATOR is
9747      located in.  `grokdeclarator' sometimes changes the scope, so
9748      we compute it now.  */
9749   scope = get_scope_of_declarator (declarator);
9750
9751   /* If we're allowing GNU extensions, look for an asm-specification
9752      and attributes.  */
9753   if (cp_parser_allow_gnu_extensions_p (parser))
9754     {
9755       /* Look for an asm-specification.  */
9756       asm_specification = cp_parser_asm_specification_opt (parser);
9757       /* And attributes.  */
9758       attributes = cp_parser_attributes_opt (parser);
9759     }
9760   else
9761     {
9762       asm_specification = NULL_TREE;
9763       attributes = NULL_TREE;
9764     }
9765
9766   /* Peek at the next token.  */
9767   token = cp_lexer_peek_token (parser->lexer);
9768   /* Check to see if the token indicates the start of a
9769      function-definition.  */
9770   if (cp_parser_token_starts_function_definition_p (token))
9771     {
9772       if (!function_definition_allowed_p)
9773         {
9774           /* If a function-definition should not appear here, issue an
9775              error message.  */
9776           cp_parser_error (parser,
9777                            "a function-definition is not allowed here");
9778           /* Discard access checks no longer in use.  */
9779           parser->access_checks_lists
9780             = TREE_CHAIN (parser->access_checks_lists);
9781           return error_mark_node;
9782         }
9783       else
9784         {
9785           tree *ac;
9786
9787           /* Neither attributes nor an asm-specification are allowed
9788              on a function-definition.  */
9789           if (asm_specification)
9790             error ("an asm-specification is not allowed on a function-definition");
9791           if (attributes)
9792             error ("attributes are not allowed on a function-definition");
9793           /* This is a function-definition.  */
9794           *function_definition_p = true;
9795
9796           /* Thread the access checks together.  */
9797           ac = &access_checks;
9798           while (*ac)
9799             ac = &TREE_CHAIN (*ac);
9800           *ac = declarator_access_checks;
9801
9802           /* Parse the function definition.  */
9803           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9804                   (parser, decl_specifiers, prefix_attributes, declarator,
9805                    access_checks));
9806
9807           /* Pull the access-checks apart again.  */
9808           *ac = NULL_TREE;
9809
9810           /* Discard access checks no longer in use.  */
9811           parser->access_checks_lists
9812              = TREE_CHAIN (parser->access_checks_lists);
9813
9814           return decl;
9815         }
9816     }
9817
9818   /* [dcl.dcl]
9819
9820      Only in function declarations for constructors, destructors, and
9821      type conversions can the decl-specifier-seq be omitted.  
9822
9823      We explicitly postpone this check past the point where we handle
9824      function-definitions because we tolerate function-definitions
9825      that are missing their return types in some modes.  */
9826   if (!decl_specifiers && !ctor_dtor_or_conv_p)
9827     {
9828       cp_parser_error (parser, 
9829                        "expected constructor, destructor, or type conversion");
9830       /* Discard access checks no longer in use.  */
9831       parser->access_checks_lists
9832         = TREE_CHAIN (parser->access_checks_lists);
9833       return error_mark_node;
9834     }
9835
9836   /* An `=' or an `(' indicates an initializer.  */
9837   is_initialized = (token->type == CPP_EQ 
9838                      || token->type == CPP_OPEN_PAREN);
9839   /* If the init-declarator isn't initialized and isn't followed by a
9840      `,' or `;', it's not a valid init-declarator.  */
9841   if (!is_initialized 
9842       && token->type != CPP_COMMA
9843       && token->type != CPP_SEMICOLON)
9844     {
9845       cp_parser_error (parser, "expected init-declarator");
9846       /* Discard access checks no longer in use.  */
9847       parser->access_checks_lists
9848          = TREE_CHAIN (parser->access_checks_lists);
9849       return error_mark_node;
9850     }
9851
9852   /* Because start_decl has side-effects, we should only call it if we
9853      know we're going ahead.  By this point, we know that we cannot
9854      possibly be looking at any other construct.  */
9855   cp_parser_commit_to_tentative_parse (parser);
9856
9857   /* Check to see whether or not this declaration is a friend.  */
9858   friend_p = cp_parser_friend_p (decl_specifiers);
9859
9860   /* Check that the number of template-parameter-lists is OK.  */
9861   if (!cp_parser_check_declarator_template_parameters (parser, 
9862                                                        declarator))
9863     {
9864       /* Discard access checks no longer in use.  */
9865       parser->access_checks_lists
9866          = TREE_CHAIN (parser->access_checks_lists);
9867       return error_mark_node;
9868     }
9869
9870   /* Enter the newly declared entry in the symbol table.  If we're
9871      processing a declaration in a class-specifier, we wait until
9872      after processing the initializer.  */
9873   if (!member_p)
9874     {
9875       if (parser->in_unbraced_linkage_specification_p)
9876         {
9877           decl_specifiers = tree_cons (error_mark_node,
9878                                        get_identifier ("extern"),
9879                                        decl_specifiers);
9880           have_extern_spec = false;
9881         }
9882       decl = start_decl (declarator,
9883                          decl_specifiers,
9884                          is_initialized,
9885                          attributes,
9886                          prefix_attributes);
9887     }
9888
9889   /* Enter the SCOPE.  That way unqualified names appearing in the
9890      initializer will be looked up in SCOPE.  */
9891   if (scope)
9892     push_scope (scope);
9893
9894   /* Perform deferred access control checks, now that we know in which
9895      SCOPE the declared entity resides.  */
9896   if (!member_p && decl) 
9897     {
9898       tree saved_current_function_decl = NULL_TREE;
9899
9900       /* If the entity being declared is a function, pretend that we
9901          are in its scope.  If it is a `friend', it may have access to
9902          things that would not otherwise be accessible. */
9903       if (TREE_CODE (decl) == FUNCTION_DECL)
9904         {
9905           saved_current_function_decl = current_function_decl;
9906           current_function_decl = decl;
9907         }
9908         
9909       /* Perform the access control checks for the decl-specifiers.  */
9910       cp_parser_perform_deferred_access_checks (access_checks);
9911       /* And for the declarator.  */
9912       cp_parser_perform_deferred_access_checks (declarator_access_checks);
9913
9914       /* Restore the saved value.  */
9915       if (TREE_CODE (decl) == FUNCTION_DECL)
9916         current_function_decl = saved_current_function_decl;
9917     }
9918
9919   /* Parse the initializer.  */
9920   if (is_initialized)
9921     initializer = cp_parser_initializer (parser, 
9922                                          &is_parenthesized_init);
9923   else
9924     {
9925       initializer = NULL_TREE;
9926       is_parenthesized_init = false;
9927     }
9928
9929   /* The old parser allows attributes to appear after a parenthesized
9930      initializer.  Mark Mitchell proposed removing this functionality
9931      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9932      attributes -- but ignores them.  */
9933   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9934     if (cp_parser_attributes_opt (parser))
9935       warning ("attributes after parenthesized initializer ignored");
9936
9937   /* Leave the SCOPE, now that we have processed the initializer.  It
9938      is important to do this before calling cp_finish_decl because it
9939      makes decisions about whether to create DECL_STMTs or not based
9940      on the current scope.  */
9941   if (scope)
9942     pop_scope (scope);
9943
9944   /* For an in-class declaration, use `grokfield' to create the
9945      declaration.  */
9946   if (member_p)
9947     decl = grokfield (declarator, decl_specifiers,
9948                       initializer, /*asmspec=*/NULL_TREE,
9949                         /*attributes=*/NULL_TREE);
9950
9951   /* Finish processing the declaration.  But, skip friend
9952      declarations.  */
9953   if (!friend_p && decl)
9954     cp_finish_decl (decl, 
9955                     initializer, 
9956                     asm_specification,
9957                     /* If the initializer is in parentheses, then this is
9958                        a direct-initialization, which means that an
9959                        `explicit' constructor is OK.  Otherwise, an
9960                        `explicit' constructor cannot be used.  */
9961                     ((is_parenthesized_init || !is_initialized)
9962                      ? 0 : LOOKUP_ONLYCONVERTING));
9963
9964   /* Discard access checks no longer in use.  */
9965   parser->access_checks_lists
9966     = TREE_CHAIN (parser->access_checks_lists);
9967
9968   return decl;
9969 }
9970
9971 /* Parse a declarator.
9972    
9973    declarator:
9974      direct-declarator
9975      ptr-operator declarator  
9976
9977    abstract-declarator:
9978      ptr-operator abstract-declarator [opt]
9979      direct-abstract-declarator
9980
9981    GNU Extensions:
9982
9983    declarator:
9984      attributes [opt] direct-declarator
9985      attributes [opt] ptr-operator declarator  
9986
9987    abstract-declarator:
9988      attributes [opt] ptr-operator abstract-declarator [opt]
9989      attributes [opt] direct-abstract-declarator
9990      
9991    Returns a representation of the declarator.  If the declarator has
9992    the form `* declarator', then an INDIRECT_REF is returned, whose
9993    only operand is the sub-declarator.  Analagously, `& declarator' is
9994    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
9995    used.  The first operand is the TYPE for `X'.  The second operand
9996    is an INDIRECT_REF whose operand is the sub-declarator.
9997
9998    Otherwise, the reprsentation is as for a direct-declarator.
9999
10000    (It would be better to define a structure type to represent
10001    declarators, rather than abusing `tree' nodes to represent
10002    declarators.  That would be much clearer and save some memory.
10003    There is no reason for declarators to be garbage-collected, for
10004    example; they are created during parser and no longer needed after
10005    `grokdeclarator' has been called.)
10006
10007    For a ptr-operator that has the optional cv-qualifier-seq,
10008    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10009    node.
10010
10011    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10012    true if this declarator represents a constructor, destructor, or
10013    type conversion operator.  Otherwise, it is set to false.  
10014
10015    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10016    a decl-specifier-seq unless it declares a constructor, destructor,
10017    or conversion.  It might seem that we could check this condition in
10018    semantic analysis, rather than parsing, but that makes it difficult
10019    to handle something like `f()'.  We want to notice that there are
10020    no decl-specifiers, and therefore realize that this is an
10021    expression, not a declaration.)  */
10022
10023 static tree
10024 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10025      cp_parser *parser;
10026      bool abstract_p;
10027      bool *ctor_dtor_or_conv_p;
10028 {
10029   cp_token *token;
10030   tree declarator;
10031   enum tree_code code;
10032   tree cv_qualifier_seq;
10033   tree class_type;
10034   tree attributes = NULL_TREE;
10035
10036   /* Assume this is not a constructor, destructor, or type-conversion
10037      operator.  */
10038   if (ctor_dtor_or_conv_p)
10039     *ctor_dtor_or_conv_p = false;
10040
10041   if (cp_parser_allow_gnu_extensions_p (parser))
10042     attributes = cp_parser_attributes_opt (parser);
10043   
10044   /* Peek at the next token.  */
10045   token = cp_lexer_peek_token (parser->lexer);
10046   
10047   /* Check for the ptr-operator production.  */
10048   cp_parser_parse_tentatively (parser);
10049   /* Parse the ptr-operator.  */
10050   code = cp_parser_ptr_operator (parser, 
10051                                  &class_type, 
10052                                  &cv_qualifier_seq);
10053   /* If that worked, then we have a ptr-operator.  */
10054   if (cp_parser_parse_definitely (parser))
10055     {
10056       /* The dependent declarator is optional if we are parsing an
10057          abstract-declarator.  */
10058       if (abstract_p)
10059         cp_parser_parse_tentatively (parser);
10060
10061       /* Parse the dependent declarator.  */
10062       declarator = cp_parser_declarator (parser, abstract_p,
10063                                          /*ctor_dtor_or_conv_p=*/NULL);
10064
10065       /* If we are parsing an abstract-declarator, we must handle the
10066          case where the dependent declarator is absent.  */
10067       if (abstract_p && !cp_parser_parse_definitely (parser))
10068         declarator = NULL_TREE;
10069         
10070       /* Build the representation of the ptr-operator.  */
10071       if (code == INDIRECT_REF)
10072         declarator = make_pointer_declarator (cv_qualifier_seq, 
10073                                               declarator);
10074       else
10075         declarator = make_reference_declarator (cv_qualifier_seq,
10076                                                 declarator);
10077       /* Handle the pointer-to-member case.  */
10078       if (class_type)
10079         declarator = build_nt (SCOPE_REF, class_type, declarator);
10080     }
10081   /* Everything else is a direct-declarator.  */
10082   else
10083     declarator = cp_parser_direct_declarator (parser, 
10084                                               abstract_p,
10085                                               ctor_dtor_or_conv_p);
10086
10087   if (attributes && declarator != error_mark_node)
10088     declarator = tree_cons (attributes, declarator, NULL_TREE);
10089   
10090   return declarator;
10091 }
10092
10093 /* Parse a direct-declarator or direct-abstract-declarator.
10094
10095    direct-declarator:
10096      declarator-id
10097      direct-declarator ( parameter-declaration-clause )
10098        cv-qualifier-seq [opt] 
10099        exception-specification [opt]
10100      direct-declarator [ constant-expression [opt] ]
10101      ( declarator )  
10102
10103    direct-abstract-declarator:
10104      direct-abstract-declarator [opt]
10105        ( parameter-declaration-clause ) 
10106        cv-qualifier-seq [opt]
10107        exception-specification [opt]
10108      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10109      ( abstract-declarator )
10110
10111    Returns a representation of the declarator.  ABSTRACT_P is TRUE if
10112    we are parsing a direct-abstract-declarator; FALSE if we are
10113    parsing a direct-declarator.  CTOR_DTOR_OR_CONV_P is as for 
10114    cp_parser_declarator.
10115
10116    For the declarator-id production, the representation is as for an
10117    id-expression, except that a qualified name is represented as a
10118    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10119    see the documentation of the FUNCTION_DECLARATOR_* macros for
10120    information about how to find the various declarator components.
10121    An array-declarator is represented as an ARRAY_REF.  The
10122    direct-declarator is the first operand; the constant-expression
10123    indicating the size of the array is the second operand.  */
10124
10125 static tree
10126 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10127      cp_parser *parser;
10128      bool abstract_p;
10129      bool *ctor_dtor_or_conv_p;
10130 {
10131   cp_token *token;
10132   tree declarator;
10133   tree scope = NULL_TREE;
10134   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10135   bool saved_in_declarator_p = parser->in_declarator_p;
10136
10137   /* Peek at the next token.  */
10138   token = cp_lexer_peek_token (parser->lexer);
10139   /* Find the initial direct-declarator.  It might be a parenthesized
10140      declarator.  */
10141   if (token->type == CPP_OPEN_PAREN)
10142     {
10143       bool error_p;
10144
10145       /* For an abstract declarator we do not know whether we are
10146          looking at the beginning of a parameter-declaration-clause,
10147          or at a parenthesized abstract declarator.  For example, if
10148          we see `(int)', we are looking at a
10149          parameter-declaration-clause, and the
10150          direct-abstract-declarator has been omitted.  If, on the
10151          other hand we are looking at `((*))' then we are looking at a
10152          parenthesized abstract-declarator.  There is no easy way to
10153          tell which situation we are in.  */
10154       if (abstract_p)
10155         cp_parser_parse_tentatively (parser);
10156
10157       /* Consume the `('.  */
10158       cp_lexer_consume_token (parser->lexer);
10159       /* Parse the nested declarator.  */
10160       declarator 
10161         = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10162       /* Expect a `)'.  */
10163       error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10164
10165       /* If parsing a parenthesized abstract declarator didn't work,
10166          try a parameter-declaration-clause.  */
10167       if (abstract_p && !cp_parser_parse_definitely (parser))
10168         declarator = NULL_TREE;
10169       /* If we were not parsing an abstract declarator, but failed to
10170          find a satisfactory nested declarator, then an error has
10171          occurred.  */
10172       else if (!abstract_p 
10173                && (declarator == error_mark_node || error_p))
10174         return error_mark_node;
10175       /* Default args cannot appear in an abstract decl.  */
10176       parser->default_arg_ok_p = false;
10177     }
10178   /* Otherwise, for a non-abstract declarator, there should be a
10179      declarator-id.  */
10180   else if (!abstract_p)
10181     {
10182       declarator = cp_parser_declarator_id (parser);
10183       
10184       if (TREE_CODE (declarator) == SCOPE_REF)
10185         {
10186           scope = TREE_OPERAND (declarator, 0);
10187           
10188           /* In the declaration of a member of a template class
10189              outside of the class itself, the SCOPE will sometimes be
10190              a TYPENAME_TYPE.  For example, given:
10191              
10192                template <typename T>
10193                int S<T>::R::i = 3;
10194
10195              the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In this
10196              context, we must resolve S<T>::R to an ordinary type,
10197              rather than a typename type.
10198
10199              The reason we normally avoid resolving TYPENAME_TYPEs is
10200              that a specialization of `S' might render `S<T>::R' not a
10201              type.  However, if `S' is specialized, then this `i' will
10202              not be used, so there is no harm in resolving the types
10203              here.  */
10204           if (TREE_CODE (scope) == TYPENAME_TYPE)
10205             {
10206               /* Resolve the TYPENAME_TYPE.  */
10207               scope = cp_parser_resolve_typename_type (parser, scope);
10208               /* If that failed, the declarator is invalid.  */
10209               if (scope == error_mark_node)
10210                 return error_mark_node;
10211               /* Build a new DECLARATOR.  */
10212               declarator = build_nt (SCOPE_REF, 
10213                                      scope,
10214                                      TREE_OPERAND (declarator, 1));
10215             }
10216         }
10217       else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10218         /* Default args can only appear for a function decl.  */
10219         parser->default_arg_ok_p = false;
10220       
10221       /* Check to see whether the declarator-id names a constructor, 
10222          destructor, or conversion.  */
10223       if (ctor_dtor_or_conv_p 
10224           && ((TREE_CODE (declarator) == SCOPE_REF 
10225                && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10226               || (TREE_CODE (declarator) != SCOPE_REF
10227                   && at_class_scope_p ())))
10228         {
10229           tree unqualified_name;
10230           tree class_type;
10231
10232           /* Get the unqualified part of the name.  */
10233           if (TREE_CODE (declarator) == SCOPE_REF)
10234             {
10235               class_type = TREE_OPERAND (declarator, 0);
10236               unqualified_name = TREE_OPERAND (declarator, 1);
10237             }
10238           else
10239             {
10240               class_type = current_class_type;
10241               unqualified_name = declarator;
10242             }
10243
10244           /* See if it names ctor, dtor or conv.  */
10245           if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10246               || IDENTIFIER_TYPENAME_P (unqualified_name)
10247               || constructor_name_p (unqualified_name, class_type))
10248             {
10249               *ctor_dtor_or_conv_p = true;
10250               /* We would have cleared the default arg flag above, but
10251                  they are ok.  */
10252               parser->default_arg_ok_p = saved_default_arg_ok_p;
10253             }
10254         }
10255     }
10256   /* But for an abstract declarator, the initial direct-declarator can
10257      be omitted.  */
10258   else
10259     {
10260       declarator = NULL_TREE;
10261       parser->default_arg_ok_p = false;
10262     }
10263
10264   scope = get_scope_of_declarator (declarator);
10265   if (scope)
10266     /* Any names that appear after the declarator-id for a member
10267        are looked up in the containing scope.  */
10268     push_scope (scope);
10269   else
10270     scope = NULL_TREE;
10271   parser->in_declarator_p = true;
10272
10273   /* Now, parse function-declarators and array-declarators until there
10274      are no more.  */
10275   while (true)
10276     {
10277       /* Peek at the next token.  */
10278       token = cp_lexer_peek_token (parser->lexer);
10279       /* If it's a `[', we're looking at an array-declarator.  */
10280       if (token->type == CPP_OPEN_SQUARE)
10281         {
10282           tree bounds;
10283
10284           /* Consume the `['.  */
10285           cp_lexer_consume_token (parser->lexer);
10286           /* Peek at the next token.  */
10287           token = cp_lexer_peek_token (parser->lexer);
10288           /* If the next token is `]', then there is no
10289              constant-expression.  */
10290           if (token->type != CPP_CLOSE_SQUARE)
10291             bounds = cp_parser_constant_expression (parser);
10292           else
10293             bounds = NULL_TREE;
10294           /* Look for the closing `]'.  */
10295           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10296
10297           declarator = build_nt (ARRAY_REF, declarator, bounds);
10298         }
10299       /* If it's a `(', we're looking at a function-declarator.  */
10300       else if (token->type == CPP_OPEN_PAREN)
10301         {
10302           /* A function-declarator.  Or maybe not.  Consider, for
10303              example:
10304
10305                int i (int);
10306                int i (3);
10307
10308              The first is the declaration of a function while the
10309              second is a the definition of a variable, including its
10310              initializer.
10311
10312              Having seen only the parenthesis, we cannot know which of
10313              these two alternatives should be selected.  Even more
10314              complex are examples like:
10315
10316                int i (int (a));
10317                int i (int (3));
10318
10319              The former is a function-declaration; the latter is a
10320              variable initialization.  
10321
10322              First, we attempt to parse a parameter-declaration
10323              clause.  If this works, then we continue; otherwise, we
10324              replace the tokens consumed in the process and continue.  */
10325           tree params;
10326
10327           /* We are now parsing tentatively.  */
10328           cp_parser_parse_tentatively (parser);
10329           
10330           /* Consume the `('.  */
10331           cp_lexer_consume_token (parser->lexer);
10332           /* Parse the parameter-declaration-clause.  */
10333           params = cp_parser_parameter_declaration_clause (parser);
10334           
10335           /* If all went well, parse the cv-qualifier-seq and the
10336              exception-specification.  */
10337           if (cp_parser_parse_definitely (parser))
10338             {
10339               tree cv_qualifiers;
10340               tree exception_specification;
10341
10342               /* Consume the `)'.  */
10343               cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10344
10345               /* Parse the cv-qualifier-seq.  */
10346               cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10347               /* And the exception-specification.  */
10348               exception_specification 
10349                 = cp_parser_exception_specification_opt (parser);
10350
10351               /* Create the function-declarator.  */
10352               declarator = make_call_declarator (declarator,
10353                                                  params,
10354                                                  cv_qualifiers,
10355                                                  exception_specification);
10356             }
10357           /* Otherwise, we must be done with the declarator.  */
10358           else
10359             break;
10360         }
10361       /* Otherwise, we're done with the declarator.  */
10362       else
10363         break;
10364       /* Any subsequent parameter lists are to do with return type, so
10365          are not those of the declared function.  */
10366       parser->default_arg_ok_p = false;
10367     }
10368
10369   /* For an abstract declarator, we might wind up with nothing at this
10370      point.  That's an error; the declarator is not optional.  */
10371   if (!declarator)
10372     cp_parser_error (parser, "expected declarator");
10373
10374   /* If we entered a scope, we must exit it now.  */
10375   if (scope)
10376     pop_scope (scope);
10377
10378   parser->default_arg_ok_p = saved_default_arg_ok_p;
10379   parser->in_declarator_p = saved_in_declarator_p;
10380   
10381   return declarator;
10382 }
10383
10384 /* Parse a ptr-operator.  
10385
10386    ptr-operator:
10387      * cv-qualifier-seq [opt]
10388      &
10389      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10390
10391    GNU Extension:
10392
10393    ptr-operator:
10394      & cv-qualifier-seq [opt]
10395
10396    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10397    used.  Returns ADDR_EXPR if a reference was used.  In the
10398    case of a pointer-to-member, *TYPE is filled in with the 
10399    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10400    with the cv-qualifier-seq, or NULL_TREE, if there are no
10401    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10402    
10403 static enum tree_code
10404 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10405      cp_parser *parser;
10406      tree *type;
10407      tree *cv_qualifier_seq;
10408 {
10409   enum tree_code code = ERROR_MARK;
10410   cp_token *token;
10411
10412   /* Assume that it's not a pointer-to-member.  */
10413   *type = NULL_TREE;
10414   /* And that there are no cv-qualifiers.  */
10415   *cv_qualifier_seq = NULL_TREE;
10416
10417   /* Peek at the next token.  */
10418   token = cp_lexer_peek_token (parser->lexer);
10419   /* If it's a `*' or `&' we have a pointer or reference.  */
10420   if (token->type == CPP_MULT || token->type == CPP_AND)
10421     {
10422       /* Remember which ptr-operator we were processing.  */
10423       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10424
10425       /* Consume the `*' or `&'.  */
10426       cp_lexer_consume_token (parser->lexer);
10427
10428       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10429          `&', if we are allowing GNU extensions.  (The only qualifier
10430          that can legally appear after `&' is `restrict', but that is
10431          enforced during semantic analysis.  */
10432       if (code == INDIRECT_REF 
10433           || cp_parser_allow_gnu_extensions_p (parser))
10434         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10435     }
10436   else
10437     {
10438       /* Try the pointer-to-member case.  */
10439       cp_parser_parse_tentatively (parser);
10440       /* Look for the optional `::' operator.  */
10441       cp_parser_global_scope_opt (parser,
10442                                   /*current_scope_valid_p=*/false);
10443       /* Look for the nested-name specifier.  */
10444       cp_parser_nested_name_specifier (parser,
10445                                        /*typename_keyword_p=*/false,
10446                                        /*check_dependency_p=*/true,
10447                                        /*type_p=*/false);
10448       /* If we found it, and the next token is a `*', then we are
10449          indeed looking at a pointer-to-member operator.  */
10450       if (!cp_parser_error_occurred (parser)
10451           && cp_parser_require (parser, CPP_MULT, "`*'"))
10452         {
10453           /* The type of which the member is a member is given by the
10454              current SCOPE.  */
10455           *type = parser->scope;
10456           /* The next name will not be qualified.  */
10457           parser->scope = NULL_TREE;
10458           parser->qualifying_scope = NULL_TREE;
10459           parser->object_scope = NULL_TREE;
10460           /* Indicate that the `*' operator was used.  */
10461           code = INDIRECT_REF;
10462           /* Look for the optional cv-qualifier-seq.  */
10463           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10464         }
10465       /* If that didn't work we don't have a ptr-operator.  */
10466       if (!cp_parser_parse_definitely (parser))
10467         cp_parser_error (parser, "expected ptr-operator");
10468     }
10469
10470   return code;
10471 }
10472
10473 /* Parse an (optional) cv-qualifier-seq.
10474
10475    cv-qualifier-seq:
10476      cv-qualifier cv-qualifier-seq [opt]  
10477
10478    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10479    representation of a cv-qualifier.  */
10480
10481 static tree
10482 cp_parser_cv_qualifier_seq_opt (parser)
10483      cp_parser *parser;
10484 {
10485   tree cv_qualifiers = NULL_TREE;
10486   
10487   while (true)
10488     {
10489       tree cv_qualifier;
10490
10491       /* Look for the next cv-qualifier.  */
10492       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10493       /* If we didn't find one, we're done.  */
10494       if (!cv_qualifier)
10495         break;
10496
10497       /* Add this cv-qualifier to the list.  */
10498       cv_qualifiers 
10499         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10500     }
10501
10502   /* We built up the list in reverse order.  */
10503   return nreverse (cv_qualifiers);
10504 }
10505
10506 /* Parse an (optional) cv-qualifier.
10507
10508    cv-qualifier:
10509      const
10510      volatile  
10511
10512    GNU Extension:
10513
10514    cv-qualifier:
10515      __restrict__ */
10516
10517 static tree
10518 cp_parser_cv_qualifier_opt (parser)
10519      cp_parser *parser;
10520 {
10521   cp_token *token;
10522   tree cv_qualifier = NULL_TREE;
10523
10524   /* Peek at the next token.  */
10525   token = cp_lexer_peek_token (parser->lexer);
10526   /* See if it's a cv-qualifier.  */
10527   switch (token->keyword)
10528     {
10529     case RID_CONST:
10530     case RID_VOLATILE:
10531     case RID_RESTRICT:
10532       /* Save the value of the token.  */
10533       cv_qualifier = token->value;
10534       /* Consume the token.  */
10535       cp_lexer_consume_token (parser->lexer);
10536       break;
10537
10538     default:
10539       break;
10540     }
10541
10542   return cv_qualifier;
10543 }
10544
10545 /* Parse a declarator-id.
10546
10547    declarator-id:
10548      id-expression
10549      :: [opt] nested-name-specifier [opt] type-name  
10550
10551    In the `id-expression' case, the value returned is as for
10552    cp_parser_id_expression if the id-expression was an unqualified-id.
10553    If the id-expression was a qualified-id, then a SCOPE_REF is
10554    returned.  The first operand is the scope (either a NAMESPACE_DECL
10555    or TREE_TYPE), but the second is still just a representation of an
10556    unqualified-id.  */
10557
10558 static tree
10559 cp_parser_declarator_id (parser)
10560      cp_parser *parser;
10561 {
10562   tree id_expression;
10563
10564   /* The expression must be an id-expression.  Assume that qualified
10565      names are the names of types so that:
10566
10567        template <class T>
10568        int S<T>::R::i = 3;
10569
10570      will work; we must treat `S<T>::R' as the name of a type.
10571      Similarly, assume that qualified names are templates, where
10572      required, so that:
10573
10574        template <class T>
10575        int S<T>::R<T>::i = 3;
10576
10577      will work, too.  */
10578   id_expression = cp_parser_id_expression (parser,
10579                                            /*template_keyword_p=*/false,
10580                                            /*check_dependency_p=*/false,
10581                                            /*template_p=*/NULL);
10582   /* If the name was qualified, create a SCOPE_REF to represent 
10583      that.  */
10584   if (parser->scope)
10585     id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10586
10587   return id_expression;
10588 }
10589
10590 /* Parse a type-id.
10591
10592    type-id:
10593      type-specifier-seq abstract-declarator [opt]
10594
10595    Returns the TYPE specified.  */
10596
10597 static tree
10598 cp_parser_type_id (parser)
10599      cp_parser *parser;
10600 {
10601   tree type_specifier_seq;
10602   tree abstract_declarator;
10603
10604   /* Parse the type-specifier-seq.  */
10605   type_specifier_seq 
10606     = cp_parser_type_specifier_seq (parser);
10607   if (type_specifier_seq == error_mark_node)
10608     return error_mark_node;
10609
10610   /* There might or might not be an abstract declarator.  */
10611   cp_parser_parse_tentatively (parser);
10612   /* Look for the declarator.  */
10613   abstract_declarator 
10614     = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10615   /* Check to see if there really was a declarator.  */
10616   if (!cp_parser_parse_definitely (parser))
10617     abstract_declarator = NULL_TREE;
10618
10619   return groktypename (build_tree_list (type_specifier_seq,
10620                                         abstract_declarator));
10621 }
10622
10623 /* Parse a type-specifier-seq.
10624
10625    type-specifier-seq:
10626      type-specifier type-specifier-seq [opt]
10627
10628    GNU extension:
10629
10630    type-specifier-seq:
10631      attributes type-specifier-seq [opt]
10632
10633    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10634    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10635
10636 static tree
10637 cp_parser_type_specifier_seq (parser)
10638      cp_parser *parser;
10639 {
10640   bool seen_type_specifier = false;
10641   tree type_specifier_seq = NULL_TREE;
10642
10643   /* Parse the type-specifiers and attributes.  */
10644   while (true)
10645     {
10646       tree type_specifier;
10647
10648       /* Check for attributes first.  */
10649       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10650         {
10651           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10652                                           NULL_TREE,
10653                                           type_specifier_seq);
10654           continue;
10655         }
10656
10657       /* After the first type-specifier, others are optional.  */
10658       if (seen_type_specifier)
10659         cp_parser_parse_tentatively (parser);
10660       /* Look for the type-specifier.  */
10661       type_specifier = cp_parser_type_specifier (parser, 
10662                                                  CP_PARSER_FLAGS_NONE,
10663                                                  /*is_friend=*/false,
10664                                                  /*is_declaration=*/false,
10665                                                  NULL,
10666                                                  NULL);
10667       /* If the first type-specifier could not be found, this is not a
10668          type-specifier-seq at all.  */
10669       if (!seen_type_specifier && type_specifier == error_mark_node)
10670         return error_mark_node;
10671       /* If subsequent type-specifiers could not be found, the
10672          type-specifier-seq is complete.  */
10673       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10674         break;
10675
10676       /* Add the new type-specifier to the list.  */
10677       type_specifier_seq 
10678         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10679       seen_type_specifier = true;
10680     }
10681
10682   /* We built up the list in reverse order.  */
10683   return nreverse (type_specifier_seq);
10684 }
10685
10686 /* Parse a parameter-declaration-clause.
10687
10688    parameter-declaration-clause:
10689      parameter-declaration-list [opt] ... [opt]
10690      parameter-declaration-list , ...
10691
10692    Returns a representation for the parameter declarations.  Each node
10693    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10694    representation.)  If the parameter-declaration-clause ends with an
10695    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10696    list.  A return value of NULL_TREE indicates a
10697    parameter-declaration-clause consisting only of an ellipsis.  */
10698
10699 static tree
10700 cp_parser_parameter_declaration_clause (parser)
10701      cp_parser *parser;
10702 {
10703   tree parameters;
10704   cp_token *token;
10705   bool ellipsis_p;
10706
10707   /* Peek at the next token.  */
10708   token = cp_lexer_peek_token (parser->lexer);
10709   /* Check for trivial parameter-declaration-clauses.  */
10710   if (token->type == CPP_ELLIPSIS)
10711     {
10712       /* Consume the `...' token.  */
10713       cp_lexer_consume_token (parser->lexer);
10714       return NULL_TREE;
10715     }
10716   else if (token->type == CPP_CLOSE_PAREN)
10717     /* There are no parameters.  */
10718     {
10719 #ifndef NO_IMPLICIT_EXTERN_C
10720       if (in_system_header && current_class_type == NULL
10721           && current_lang_name == lang_name_c)
10722         return NULL_TREE;
10723       else
10724 #endif
10725         return void_list_node;
10726     }
10727   /* Check for `(void)', too, which is a special case.  */
10728   else if (token->keyword == RID_VOID
10729            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10730                == CPP_CLOSE_PAREN))
10731     {
10732       /* Consume the `void' token.  */
10733       cp_lexer_consume_token (parser->lexer);
10734       /* There are no parameters.  */
10735       return void_list_node;
10736     }
10737   
10738   /* Parse the parameter-declaration-list.  */
10739   parameters = cp_parser_parameter_declaration_list (parser);
10740   /* If a parse error occurred while parsing the
10741      parameter-declaration-list, then the entire
10742      parameter-declaration-clause is erroneous.  */
10743   if (parameters == error_mark_node)
10744     return error_mark_node;
10745
10746   /* Peek at the next token.  */
10747   token = cp_lexer_peek_token (parser->lexer);
10748   /* If it's a `,', the clause should terminate with an ellipsis.  */
10749   if (token->type == CPP_COMMA)
10750     {
10751       /* Consume the `,'.  */
10752       cp_lexer_consume_token (parser->lexer);
10753       /* Expect an ellipsis.  */
10754       ellipsis_p 
10755         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10756     }
10757   /* It might also be `...' if the optional trailing `,' was 
10758      omitted.  */
10759   else if (token->type == CPP_ELLIPSIS)
10760     {
10761       /* Consume the `...' token.  */
10762       cp_lexer_consume_token (parser->lexer);
10763       /* And remember that we saw it.  */
10764       ellipsis_p = true;
10765     }
10766   else
10767     ellipsis_p = false;
10768
10769   /* Finish the parameter list.  */
10770   return finish_parmlist (parameters, ellipsis_p);
10771 }
10772
10773 /* Parse a parameter-declaration-list.
10774
10775    parameter-declaration-list:
10776      parameter-declaration
10777      parameter-declaration-list , parameter-declaration
10778
10779    Returns a representation of the parameter-declaration-list, as for
10780    cp_parser_parameter_declaration_clause.  However, the
10781    `void_list_node' is never appended to the list.  */
10782
10783 static tree
10784 cp_parser_parameter_declaration_list (parser)
10785      cp_parser *parser;
10786 {
10787   tree parameters = NULL_TREE;
10788
10789   /* Look for more parameters.  */
10790   while (true)
10791     {
10792       tree parameter;
10793       /* Parse the parameter.  */
10794       parameter 
10795         = cp_parser_parameter_declaration (parser,
10796                                            /*greater_than_is_operator_p=*/true);
10797       /* If a parse error ocurred parsing the parameter declaration,
10798          then the entire parameter-declaration-list is erroneous.  */
10799       if (parameter == error_mark_node)
10800         {
10801           parameters = error_mark_node;
10802           break;
10803         }
10804       /* Add the new parameter to the list.  */
10805       TREE_CHAIN (parameter) = parameters;
10806       parameters = parameter;
10807
10808       /* Peek at the next token.  */
10809       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10810           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10811         /* The parameter-declaration-list is complete.  */
10812         break;
10813       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10814         {
10815           cp_token *token;
10816
10817           /* Peek at the next token.  */
10818           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10819           /* If it's an ellipsis, then the list is complete.  */
10820           if (token->type == CPP_ELLIPSIS)
10821             break;
10822           /* Otherwise, there must be more parameters.  Consume the
10823              `,'.  */
10824           cp_lexer_consume_token (parser->lexer);
10825         }
10826       else
10827         {
10828           cp_parser_error (parser, "expected `,' or `...'");
10829           break;
10830         }
10831     }
10832
10833   /* We built up the list in reverse order; straighten it out now.  */
10834   return nreverse (parameters);
10835 }
10836
10837 /* Parse a parameter declaration.
10838
10839    parameter-declaration:
10840      decl-specifier-seq declarator
10841      decl-specifier-seq declarator = assignment-expression
10842      decl-specifier-seq abstract-declarator [opt]
10843      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10844
10845    If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10846    encountered during the parsing of the assignment-expression is not
10847    interpreted as a greater-than operator.
10848
10849    Returns a TREE_LIST representing the parameter-declaration.  The
10850    TREE_VALUE is a representation of the decl-specifier-seq and
10851    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10852    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10853    TREE_VALUE represents the declarator.  */
10854
10855 static tree
10856 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10857      cp_parser *parser;
10858      bool greater_than_is_operator_p;
10859 {
10860   bool declares_class_or_enum;
10861   tree decl_specifiers;
10862   tree attributes;
10863   tree declarator;
10864   tree default_argument;
10865   tree parameter;
10866   cp_token *token;
10867   const char *saved_message;
10868
10869   /* Type definitions may not appear in parameter types.  */
10870   saved_message = parser->type_definition_forbidden_message;
10871   parser->type_definition_forbidden_message 
10872     = "types may not be defined in parameter types";
10873
10874   /* Parse the declaration-specifiers.  */
10875   decl_specifiers 
10876     = cp_parser_decl_specifier_seq (parser,
10877                                     CP_PARSER_FLAGS_NONE,
10878                                     &attributes,
10879                                     &declares_class_or_enum);
10880   /* If an error occurred, there's no reason to attempt to parse the
10881      rest of the declaration.  */
10882   if (cp_parser_error_occurred (parser))
10883     {
10884       parser->type_definition_forbidden_message = saved_message;
10885       return error_mark_node;
10886     }
10887
10888   /* Peek at the next token.  */
10889   token = cp_lexer_peek_token (parser->lexer);
10890   /* If the next token is a `)', `,', `=', `>', or `...', then there
10891      is no declarator.  */
10892   if (token->type == CPP_CLOSE_PAREN 
10893       || token->type == CPP_COMMA
10894       || token->type == CPP_EQ
10895       || token->type == CPP_ELLIPSIS
10896       || token->type == CPP_GREATER)
10897     declarator = NULL_TREE;
10898   /* Otherwise, there should be a declarator.  */
10899   else
10900     {
10901       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10902       parser->default_arg_ok_p = false;
10903   
10904       /* We don't know whether the declarator will be abstract or
10905          not.  So, first we try an ordinary declarator.  */
10906       cp_parser_parse_tentatively (parser);
10907       declarator = cp_parser_declarator (parser,
10908                                          /*abstract_p=*/false,
10909                                          /*ctor_dtor_or_conv_p=*/NULL);
10910       /* If that didn't work, look for an abstract declarator.  */
10911       if (!cp_parser_parse_definitely (parser))
10912         declarator = cp_parser_declarator (parser,
10913                                            /*abstract_p=*/true,
10914                                            /*ctor_dtor_or_conv_p=*/NULL);
10915       parser->default_arg_ok_p = saved_default_arg_ok_p;
10916       /* After the declarator, allow more attributes.  */
10917       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10918     }
10919
10920   /* The restriction on definining new types applies only to the type
10921      of the parameter, not to the default argument.  */
10922   parser->type_definition_forbidden_message = saved_message;
10923
10924   /* If the next token is `=', then process a default argument.  */
10925   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10926     {
10927       bool saved_greater_than_is_operator_p;
10928       /* Consume the `='.  */
10929       cp_lexer_consume_token (parser->lexer);
10930
10931       /* If we are defining a class, then the tokens that make up the
10932          default argument must be saved and processed later.  */
10933       if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10934         {
10935           unsigned depth = 0;
10936
10937           /* Create a DEFAULT_ARG to represented the unparsed default
10938              argument.  */
10939           default_argument = make_node (DEFAULT_ARG);
10940           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10941
10942           /* Add tokens until we have processed the entire default
10943              argument.  */
10944           while (true)
10945             {
10946               bool done = false;
10947               cp_token *token;
10948
10949               /* Peek at the next token.  */
10950               token = cp_lexer_peek_token (parser->lexer);
10951               /* What we do depends on what token we have.  */
10952               switch (token->type)
10953                 {
10954                   /* In valid code, a default argument must be
10955                      immediately followed by a `,' `)', or `...'.  */
10956                 case CPP_COMMA:
10957                 case CPP_CLOSE_PAREN:
10958                 case CPP_ELLIPSIS:
10959                   /* If we run into a non-nested `;', `}', or `]',
10960                      then the code is invalid -- but the default
10961                      argument is certainly over.  */
10962                 case CPP_SEMICOLON:
10963                 case CPP_CLOSE_BRACE:
10964                 case CPP_CLOSE_SQUARE:
10965                   if (depth == 0)
10966                     done = true;
10967                   /* Update DEPTH, if necessary.  */
10968                   else if (token->type == CPP_CLOSE_PAREN
10969                            || token->type == CPP_CLOSE_BRACE
10970                            || token->type == CPP_CLOSE_SQUARE)
10971                     --depth;
10972                   break;
10973
10974                 case CPP_OPEN_PAREN:
10975                 case CPP_OPEN_SQUARE:
10976                 case CPP_OPEN_BRACE:
10977                   ++depth;
10978                   break;
10979
10980                 case CPP_GREATER:
10981                   /* If we see a non-nested `>', and `>' is not an
10982                      operator, then it marks the end of the default
10983                      argument.  */
10984                   if (!depth && !greater_than_is_operator_p)
10985                     done = true;
10986                   break;
10987
10988                   /* If we run out of tokens, issue an error message.  */
10989                 case CPP_EOF:
10990                   error ("file ends in default argument");
10991                   done = true;
10992                   break;
10993
10994                 case CPP_NAME:
10995                 case CPP_SCOPE:
10996                   /* In these cases, we should look for template-ids.
10997                      For example, if the default argument is 
10998                      `X<int, double>()', we need to do name lookup to
10999                      figure out whether or not `X' is a template; if
11000                      so, the `,' does not end the deault argument.
11001
11002                      That is not yet done.  */
11003                   break;
11004
11005                 default:
11006                   break;
11007                 }
11008
11009               /* If we've reached the end, stop.  */
11010               if (done)
11011                 break;
11012               
11013               /* Add the token to the token block.  */
11014               token = cp_lexer_consume_token (parser->lexer);
11015               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11016                                          token);
11017             }
11018         }
11019       /* Outside of a class definition, we can just parse the
11020          assignment-expression.  */
11021       else
11022         {
11023           bool saved_local_variables_forbidden_p;
11024
11025           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11026              set correctly.  */
11027           saved_greater_than_is_operator_p 
11028             = parser->greater_than_is_operator_p;
11029           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11030           /* Local variable names (and the `this' keyword) may not
11031              appear in a default argument.  */
11032           saved_local_variables_forbidden_p 
11033             = parser->local_variables_forbidden_p;
11034           parser->local_variables_forbidden_p = true;
11035           /* Parse the assignment-expression.  */
11036           default_argument = cp_parser_assignment_expression (parser);
11037           /* Restore saved state.  */
11038           parser->greater_than_is_operator_p 
11039             = saved_greater_than_is_operator_p;
11040           parser->local_variables_forbidden_p 
11041             = saved_local_variables_forbidden_p; 
11042         }
11043       if (!parser->default_arg_ok_p)
11044         {
11045           pedwarn ("default arguments are only permitted on functions");
11046           if (flag_pedantic_errors)
11047             default_argument = NULL_TREE;
11048         }
11049     }
11050   else
11051     default_argument = NULL_TREE;
11052   
11053   /* Create the representation of the parameter.  */
11054   if (attributes)
11055     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11056   parameter = build_tree_list (default_argument, 
11057                                build_tree_list (decl_specifiers,
11058                                                 declarator));
11059
11060   return parameter;
11061 }
11062
11063 /* Parse a function-definition.  
11064
11065    function-definition:
11066      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11067        function-body 
11068      decl-specifier-seq [opt] declarator function-try-block  
11069
11070    GNU Extension:
11071
11072    function-definition:
11073      __extension__ function-definition 
11074
11075    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
11076    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11077    be a `friend'.  */
11078
11079 static tree
11080 cp_parser_function_definition (parser, friend_p)
11081      cp_parser *parser;
11082      bool *friend_p;
11083 {
11084   tree decl_specifiers;
11085   tree attributes;
11086   tree declarator;
11087   tree fn;
11088   tree access_checks;
11089   cp_token *token;
11090   bool declares_class_or_enum;
11091   bool member_p;
11092   /* The saved value of the PEDANTIC flag.  */
11093   int saved_pedantic;
11094
11095   /* Any pending qualification must be cleared by our caller.  It is
11096      more robust to force the callers to clear PARSER->SCOPE than to
11097      do it here since if the qualification is in effect here, it might
11098      also end up in effect elsewhere that it is not intended.  */
11099   my_friendly_assert (!parser->scope, 20010821);
11100
11101   /* Handle `__extension__'.  */
11102   if (cp_parser_extension_opt (parser, &saved_pedantic))
11103     {
11104       /* Parse the function-definition.  */
11105       fn = cp_parser_function_definition (parser, friend_p);
11106       /* Restore the PEDANTIC flag.  */
11107       pedantic = saved_pedantic;
11108
11109       return fn;
11110     }
11111
11112   /* Check to see if this definition appears in a class-specifier.  */
11113   member_p = (at_class_scope_p () 
11114               && TYPE_BEING_DEFINED (current_class_type));
11115   /* Defer access checks in the decl-specifier-seq until we know what
11116      function is being defined.  There is no need to do this for the
11117      definition of member functions; we cannot be defining a member
11118      from another class.  */
11119   if (!member_p)
11120     cp_parser_start_deferring_access_checks (parser);
11121   /* Parse the decl-specifier-seq.  */
11122   decl_specifiers 
11123     = cp_parser_decl_specifier_seq (parser,
11124                                     CP_PARSER_FLAGS_OPTIONAL,
11125                                     &attributes,
11126                                     &declares_class_or_enum);
11127   /* Figure out whether this declaration is a `friend'.  */
11128   if (friend_p)
11129     *friend_p = cp_parser_friend_p (decl_specifiers);
11130
11131   /* Parse the declarator.  */
11132   declarator = cp_parser_declarator (parser, 
11133                                      /*abstract_p=*/false,
11134                                      /*ctor_dtor_or_conv_p=*/NULL);
11135
11136   /* Gather up any access checks that occurred.  */
11137   if (!member_p)
11138     access_checks = cp_parser_stop_deferring_access_checks (parser);
11139   else
11140     access_checks = NULL_TREE;
11141
11142   /* If something has already gone wrong, we may as well stop now.  */
11143   if (declarator == error_mark_node)
11144     {
11145       /* Skip to the end of the function, or if this wasn't anything
11146          like a function-definition, to a `;' in the hopes of finding
11147          a sensible place from which to continue parsing.  */
11148       cp_parser_skip_to_end_of_block_or_statement (parser);
11149       return error_mark_node;
11150     }
11151
11152   /* The next character should be a `{' (for a simple function
11153      definition), a `:' (for a ctor-initializer), or `try' (for a
11154      function-try block).  */
11155   token = cp_lexer_peek_token (parser->lexer);
11156   if (!cp_parser_token_starts_function_definition_p (token))
11157     {
11158       /* Issue the error-message.  */
11159       cp_parser_error (parser, "expected function-definition");
11160       /* Skip to the next `;'.  */
11161       cp_parser_skip_to_end_of_block_or_statement (parser);
11162
11163       return error_mark_node;
11164     }
11165
11166   /* If we are in a class scope, then we must handle
11167      function-definitions specially.  In particular, we save away the
11168      tokens that make up the function body, and parse them again
11169      later, in order to handle code like:
11170
11171        struct S {
11172          int f () { return i; }
11173          int i;
11174        }; 
11175  
11176      Here, we cannot parse the body of `f' until after we have seen
11177      the declaration of `i'.  */
11178   if (member_p)
11179     {
11180       cp_token_cache *cache;
11181
11182       /* Create the function-declaration.  */
11183       fn = start_method (decl_specifiers, declarator, attributes);
11184       /* If something went badly wrong, bail out now.  */
11185       if (fn == error_mark_node)
11186         {
11187           /* If there's a function-body, skip it.  */
11188           if (cp_parser_token_starts_function_definition_p 
11189               (cp_lexer_peek_token (parser->lexer)))
11190             cp_parser_skip_to_end_of_block_or_statement (parser);
11191           return error_mark_node;
11192         }
11193
11194       /* Create a token cache.  */
11195       cache = cp_token_cache_new ();
11196       /* Save away the tokens that make up the body of the 
11197          function.  */
11198       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11199       /* Handle function try blocks.  */
11200       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11201         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11202
11203       /* Save away the inline definition; we will process it when the
11204          class is complete.  */
11205       DECL_PENDING_INLINE_INFO (fn) = cache;
11206       DECL_PENDING_INLINE_P (fn) = 1;
11207
11208       /* We're done with the inline definition.  */
11209       finish_method (fn);
11210
11211       /* Add FN to the queue of functions to be parsed later.  */
11212       TREE_VALUE (parser->unparsed_functions_queues)
11213         = tree_cons (current_class_type, fn, 
11214                      TREE_VALUE (parser->unparsed_functions_queues));
11215
11216       return fn;
11217     }
11218
11219   /* Check that the number of template-parameter-lists is OK.  */
11220   if (!cp_parser_check_declarator_template_parameters (parser, 
11221                                                        declarator))
11222     {
11223       cp_parser_skip_to_end_of_block_or_statement (parser);
11224       return error_mark_node;
11225     }
11226
11227   return (cp_parser_function_definition_from_specifiers_and_declarator
11228           (parser, decl_specifiers, attributes, declarator, access_checks));
11229 }
11230
11231 /* Parse a function-body.
11232
11233    function-body:
11234      compound_statement  */
11235
11236 static void
11237 cp_parser_function_body (cp_parser *parser)
11238 {
11239   cp_parser_compound_statement (parser);
11240 }
11241
11242 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11243    true if a ctor-initializer was present.  */
11244
11245 static bool
11246 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11247 {
11248   tree body;
11249   bool ctor_initializer_p;
11250
11251   /* Begin the function body.  */
11252   body = begin_function_body ();
11253   /* Parse the optional ctor-initializer.  */
11254   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11255   /* Parse the function-body.  */
11256   cp_parser_function_body (parser);
11257   /* Finish the function body.  */
11258   finish_function_body (body);
11259
11260   return ctor_initializer_p;
11261 }
11262
11263 /* Parse an initializer.
11264
11265    initializer:
11266      = initializer-clause
11267      ( expression-list )  
11268
11269    Returns a expression representing the initializer.  If no
11270    initializer is present, NULL_TREE is returned.  
11271
11272    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11273    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11274    set to FALSE if there is no initializer present.  */
11275
11276 static tree
11277 cp_parser_initializer (parser, is_parenthesized_init)
11278      cp_parser *parser;
11279      bool *is_parenthesized_init;
11280 {
11281   cp_token *token;
11282   tree init;
11283
11284   /* Peek at the next token.  */
11285   token = cp_lexer_peek_token (parser->lexer);
11286
11287   /* Let our caller know whether or not this initializer was
11288      parenthesized.  */
11289   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11290
11291   if (token->type == CPP_EQ)
11292     {
11293       /* Consume the `='.  */
11294       cp_lexer_consume_token (parser->lexer);
11295       /* Parse the initializer-clause.  */
11296       init = cp_parser_initializer_clause (parser);
11297     }
11298   else if (token->type == CPP_OPEN_PAREN)
11299     {
11300       /* Consume the `('.  */
11301       cp_lexer_consume_token (parser->lexer);
11302       /* Parse the expression-list.  */
11303       init = cp_parser_expression_list (parser);
11304       /* Consume the `)' token.  */
11305       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11306         cp_parser_skip_to_closing_parenthesis (parser);
11307     }
11308   else
11309     {
11310       /* Anything else is an error.  */
11311       cp_parser_error (parser, "expected initializer");
11312       init = error_mark_node;
11313     }
11314
11315   return init;
11316 }
11317
11318 /* Parse an initializer-clause.  
11319
11320    initializer-clause:
11321      assignment-expression
11322      { initializer-list , [opt] }
11323      { }
11324
11325    Returns an expression representing the initializer.  
11326
11327    If the `assignment-expression' production is used the value
11328    returned is simply a reprsentation for the expression.  
11329
11330    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11331    the elements of the initializer-list (or NULL_TREE, if the last
11332    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11333    NULL_TREE.  There is no way to detect whether or not the optional
11334    trailing `,' was provided.  */
11335
11336 static tree
11337 cp_parser_initializer_clause (parser)
11338      cp_parser *parser;
11339 {
11340   tree initializer;
11341
11342   /* If it is not a `{', then we are looking at an
11343      assignment-expression.  */
11344   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11345     initializer = cp_parser_assignment_expression (parser);
11346   else
11347     {
11348       /* Consume the `{' token.  */
11349       cp_lexer_consume_token (parser->lexer);
11350       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11351       initializer = make_node (CONSTRUCTOR);
11352       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11353          necessary, but check_initializer depends upon it, for 
11354          now.  */
11355       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11356       /* If it's not a `}', then there is a non-trivial initializer.  */
11357       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11358         {
11359           /* Parse the initializer list.  */
11360           CONSTRUCTOR_ELTS (initializer)
11361             = cp_parser_initializer_list (parser);
11362           /* A trailing `,' token is allowed.  */
11363           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11364             cp_lexer_consume_token (parser->lexer);
11365         }
11366
11367       /* Now, there should be a trailing `}'.  */
11368       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11369     }
11370
11371   return initializer;
11372 }
11373
11374 /* Parse an initializer-list.
11375
11376    initializer-list:
11377      initializer-clause
11378      initializer-list , initializer-clause
11379
11380    GNU Extension:
11381    
11382    initializer-list:
11383      identifier : initializer-clause
11384      initializer-list, identifier : initializer-clause
11385
11386    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11387    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11388    IDENTIFIER_NODE naming the field to initialize.   */
11389
11390 static tree
11391 cp_parser_initializer_list (parser)
11392      cp_parser *parser;
11393 {
11394   tree initializers = NULL_TREE;
11395
11396   /* Parse the rest of the list.  */
11397   while (true)
11398     {
11399       cp_token *token;
11400       tree identifier;
11401       tree initializer;
11402       
11403       /* If the next token is an identifier and the following one is a
11404          colon, we are looking at the GNU designated-initializer
11405          syntax.  */
11406       if (cp_parser_allow_gnu_extensions_p (parser)
11407           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11408           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11409         {
11410           /* Consume the identifier.  */
11411           identifier = cp_lexer_consume_token (parser->lexer)->value;
11412           /* Consume the `:'.  */
11413           cp_lexer_consume_token (parser->lexer);
11414         }
11415       else
11416         identifier = NULL_TREE;
11417
11418       /* Parse the initializer.  */
11419       initializer = cp_parser_initializer_clause (parser);
11420
11421       /* Add it to the list.  */
11422       initializers = tree_cons (identifier, initializer, initializers);
11423
11424       /* If the next token is not a comma, we have reached the end of
11425          the list.  */
11426       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11427         break;
11428
11429       /* Peek at the next token.  */
11430       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11431       /* If the next token is a `}', then we're still done.  An
11432          initializer-clause can have a trailing `,' after the
11433          initializer-list and before the closing `}'.  */
11434       if (token->type == CPP_CLOSE_BRACE)
11435         break;
11436
11437       /* Consume the `,' token.  */
11438       cp_lexer_consume_token (parser->lexer);
11439     }
11440
11441   /* The initializers were built up in reverse order, so we need to
11442      reverse them now.  */
11443   return nreverse (initializers);
11444 }
11445
11446 /* Classes [gram.class] */
11447
11448 /* Parse a class-name.
11449
11450    class-name:
11451      identifier
11452      template-id
11453
11454    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11455    to indicate that names looked up in dependent types should be
11456    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11457    keyword has been used to indicate that the name that appears next
11458    is a template.  TYPE_P is true iff the next name should be treated
11459    as class-name, even if it is declared to be some other kind of name
11460    as well.  The accessibility of the class-name is checked iff
11461    CHECK_ACCESS_P is true.  If CHECK_DEPENDENCY_P is FALSE, names are
11462    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
11463    is the class being defined in a class-head.
11464
11465    Returns the TYPE_DECL representing the class.  */
11466
11467 static tree
11468 cp_parser_class_name (cp_parser *parser, 
11469                       bool typename_keyword_p, 
11470                       bool template_keyword_p, 
11471                       bool type_p,
11472                       bool check_access_p,
11473                       bool check_dependency_p,
11474                       bool class_head_p)
11475 {
11476   tree decl;
11477   tree scope;
11478   bool typename_p;
11479   cp_token *token;
11480
11481   /* All class-names start with an identifier.  */
11482   token = cp_lexer_peek_token (parser->lexer);
11483   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11484     {
11485       cp_parser_error (parser, "expected class-name");
11486       return error_mark_node;
11487     }
11488     
11489   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11490      to a template-id, so we save it here.  */
11491   scope = parser->scope;
11492   /* Any name names a type if we're following the `typename' keyword
11493      in a qualified name where the enclosing scope is type-dependent.  */
11494   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11495                 && cp_parser_dependent_type_p (scope));
11496   /* Handle the common case (an identifier, but not a template-id)
11497      efficiently.  */
11498   if (token->type == CPP_NAME 
11499       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11500     {
11501       tree identifier;
11502
11503       /* Look for the identifier.  */
11504       identifier = cp_parser_identifier (parser);
11505       /* If the next token isn't an identifier, we are certainly not
11506          looking at a class-name.  */
11507       if (identifier == error_mark_node)
11508         decl = error_mark_node;
11509       /* If we know this is a type-name, there's no need to look it
11510          up.  */
11511       else if (typename_p)
11512         decl = identifier;
11513       else
11514         {
11515           /* If the next token is a `::', then the name must be a type
11516              name.
11517
11518              [basic.lookup.qual]
11519
11520              During the lookup for a name preceding the :: scope
11521              resolution operator, object, function, and enumerator
11522              names are ignored.  */
11523           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11524             type_p = true;
11525           /* Look up the name.  */
11526           decl = cp_parser_lookup_name (parser, identifier, 
11527                                         check_access_p,
11528                                         type_p,
11529                                         /*is_namespace=*/false,
11530                                         check_dependency_p);
11531         }
11532     }
11533   else
11534     {
11535       /* Try a template-id.  */
11536       decl = cp_parser_template_id (parser, template_keyword_p,
11537                                     check_dependency_p);
11538       if (decl == error_mark_node)
11539         return error_mark_node;
11540     }
11541
11542   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11543
11544   /* If this is a typename, create a TYPENAME_TYPE.  */
11545   if (typename_p && decl != error_mark_node)
11546     decl = TYPE_NAME (make_typename_type (scope, decl,
11547                                           /*complain=*/1));
11548
11549   /* Check to see that it is really the name of a class.  */
11550   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11551       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11552       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11553     /* Situations like this:
11554
11555          template <typename T> struct A {
11556            typename T::template X<int>::I i; 
11557          };
11558
11559        are problematic.  Is `T::template X<int>' a class-name?  The
11560        standard does not seem to be definitive, but there is no other
11561        valid interpretation of the following `::'.  Therefore, those
11562        names are considered class-names.  */
11563     decl = TYPE_NAME (make_typename_type (scope, decl, 
11564                                           tf_error | tf_parsing));
11565   else if (decl == error_mark_node
11566            || TREE_CODE (decl) != TYPE_DECL
11567            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11568     {
11569       cp_parser_error (parser, "expected class-name");
11570       return error_mark_node;
11571     }
11572
11573   return decl;
11574 }
11575
11576 /* Parse a class-specifier.
11577
11578    class-specifier:
11579      class-head { member-specification [opt] }
11580
11581    Returns the TREE_TYPE representing the class.  */
11582
11583 static tree
11584 cp_parser_class_specifier (parser)
11585      cp_parser *parser;
11586 {
11587   cp_token *token;
11588   tree type;
11589   tree attributes = NULL_TREE;
11590   int has_trailing_semicolon;
11591   bool nested_name_specifier_p;
11592   bool deferring_access_checks_p;
11593   tree saved_access_checks;
11594   unsigned saved_num_template_parameter_lists;
11595
11596   /* Parse the class-head.  */
11597   type = cp_parser_class_head (parser,
11598                                &nested_name_specifier_p,
11599                                &deferring_access_checks_p,
11600                                &saved_access_checks);
11601   /* If the class-head was a semantic disaster, skip the entire body
11602      of the class.  */
11603   if (!type)
11604     {
11605       cp_parser_skip_to_end_of_block_or_statement (parser);
11606       return error_mark_node;
11607     }
11608   /* Look for the `{'.  */
11609   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11610     return error_mark_node;
11611   /* Issue an error message if type-definitions are forbidden here.  */
11612   cp_parser_check_type_definition (parser);
11613   /* Remember that we are defining one more class.  */
11614   ++parser->num_classes_being_defined;
11615   /* Inside the class, surrounding template-parameter-lists do not
11616      apply.  */
11617   saved_num_template_parameter_lists 
11618     = parser->num_template_parameter_lists; 
11619   parser->num_template_parameter_lists = 0;
11620   /* Start the class.  */
11621   type = begin_class_definition (type);
11622   if (type == error_mark_node)
11623     /* If the type is erroneous, skip the entire body of the class. */
11624     cp_parser_skip_to_closing_brace (parser);
11625   else
11626     /* Parse the member-specification.  */
11627     cp_parser_member_specification_opt (parser);
11628   /* Look for the trailing `}'.  */
11629   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11630   /* We get better error messages by noticing a common problem: a
11631      missing trailing `;'.  */
11632   token = cp_lexer_peek_token (parser->lexer);
11633   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11634   /* Look for attributes to apply to this class.  */
11635   if (cp_parser_allow_gnu_extensions_p (parser))
11636     attributes = cp_parser_attributes_opt (parser);
11637   /* Finish the class definition.  */
11638   type = finish_class_definition (type, 
11639                                   attributes,
11640                                   has_trailing_semicolon,
11641                                   nested_name_specifier_p);
11642   /* If this class is not itself within the scope of another class,
11643      then we need to parse the bodies of all of the queued function
11644      definitions.  Note that the queued functions defined in a class
11645      are not always processed immediately following the
11646      class-specifier for that class.  Consider:
11647
11648        struct A {
11649          struct B { void f() { sizeof (A); } };
11650        };
11651
11652      If `f' were processed before the processing of `A' were
11653      completed, there would be no way to compute the size of `A'.
11654      Note that the nesting we are interested in here is lexical --
11655      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11656      for:
11657
11658        struct A { struct B; };
11659        struct A::B { void f() { } };
11660
11661      there is no need to delay the parsing of `A::B::f'.  */
11662   if (--parser->num_classes_being_defined == 0) 
11663     {
11664       tree last_scope = NULL_TREE;
11665
11666       /* Process non FUNCTION_DECL related DEFAULT_ARGs.  */
11667       for (parser->default_arg_types = nreverse (parser->default_arg_types);
11668            parser->default_arg_types;
11669            parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11670         cp_parser_late_parsing_default_args
11671           (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
11672       
11673       /* Reverse the queue, so that we process it in the order the
11674          functions were declared.  */
11675       TREE_VALUE (parser->unparsed_functions_queues)
11676         = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11677       /* Loop through all of the functions.  */
11678       while (TREE_VALUE (parser->unparsed_functions_queues))
11679
11680         {
11681           tree fn;
11682           tree fn_scope;
11683           tree queue_entry;
11684
11685           /* Figure out which function we need to process.  */
11686           queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11687           fn_scope = TREE_PURPOSE (queue_entry);
11688           fn = TREE_VALUE (queue_entry);
11689
11690           /* Parse the function.  */
11691           cp_parser_late_parsing_for_member (parser, fn);
11692
11693           TREE_VALUE (parser->unparsed_functions_queues)
11694             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11695         }
11696
11697       /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11698          more time than we have popped, so me must pop here.  */
11699       if (last_scope)
11700         pop_scope (last_scope);
11701     }
11702
11703   /* Put back any saved access checks.  */
11704   if (deferring_access_checks_p)
11705     {
11706       cp_parser_start_deferring_access_checks (parser);
11707       parser->context->deferred_access_checks = saved_access_checks;
11708     }
11709
11710   /* Restore the count of active template-parameter-lists.  */
11711   parser->num_template_parameter_lists
11712     = saved_num_template_parameter_lists;
11713
11714   return type;
11715 }
11716
11717 /* Parse a class-head.
11718
11719    class-head:
11720      class-key identifier [opt] base-clause [opt]
11721      class-key nested-name-specifier identifier base-clause [opt]
11722      class-key nested-name-specifier [opt] template-id 
11723        base-clause [opt]  
11724
11725    GNU Extensions:
11726      class-key attributes identifier [opt] base-clause [opt]
11727      class-key attributes nested-name-specifier identifier base-clause [opt]
11728      class-key attributes nested-name-specifier [opt] template-id 
11729        base-clause [opt]  
11730
11731    Returns the TYPE of the indicated class.  Sets
11732    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11733    involving a nested-name-specifier was used, and FALSE otherwise.
11734    Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11735    access checks before this class-head.  In that case,
11736    *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11737    checks.  
11738
11739    Returns NULL_TREE if the class-head is syntactically valid, but
11740    semantically invalid in a way that means we should skip the entire
11741    body of the class.  */
11742
11743 static tree
11744 cp_parser_class_head (parser, 
11745                       nested_name_specifier_p,
11746                       deferring_access_checks_p,
11747                       saved_access_checks)
11748      cp_parser *parser;
11749      bool *nested_name_specifier_p;
11750      bool *deferring_access_checks_p;
11751      tree *saved_access_checks;
11752 {
11753   cp_token *token;
11754   tree nested_name_specifier;
11755   enum tag_types class_key;
11756   tree id = NULL_TREE;
11757   tree type = NULL_TREE;
11758   tree attributes;
11759   bool template_id_p = false;
11760   bool qualified_p = false;
11761   bool invalid_nested_name_p = false;
11762   unsigned num_templates;
11763
11764   /* Assume no nested-name-specifier will be present.  */
11765   *nested_name_specifier_p = false;
11766   /* Assume no template parameter lists will be used in defining the
11767      type.  */
11768   num_templates = 0;
11769
11770   /* Look for the class-key.  */
11771   class_key = cp_parser_class_key (parser);
11772   if (class_key == none_type)
11773     return error_mark_node;
11774
11775   /* Parse the attributes.  */
11776   attributes = cp_parser_attributes_opt (parser);
11777
11778   /* If the next token is `::', that is invalid -- but sometimes
11779      people do try to write:
11780
11781        struct ::S {};  
11782
11783      Handle this gracefully by accepting the extra qualifier, and then
11784      issuing an error about it later if this really is a
11785      class-header.  If it turns out just to be an elaborated type
11786      specifier, remain silent.  */
11787   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11788     qualified_p = true;
11789
11790   /* Determine the name of the class.  Begin by looking for an
11791      optional nested-name-specifier.  */
11792   nested_name_specifier 
11793     = cp_parser_nested_name_specifier_opt (parser,
11794                                            /*typename_keyword_p=*/false,
11795                                            /*check_dependency_p=*/true,
11796                                            /*type_p=*/false);
11797   /* If there was a nested-name-specifier, then there *must* be an
11798      identifier.  */
11799   if (nested_name_specifier)
11800     {
11801       /* Although the grammar says `identifier', it really means
11802          `class-name' or `template-name'.  You are only allowed to
11803          define a class that has already been declared with this
11804          syntax.  
11805
11806          The proposed resolution for Core Issue 180 says that whever
11807          you see `class T::X' you should treat `X' as a type-name.
11808          
11809          It is OK to define an inaccessible class; for example:
11810          
11811            class A { class B; };
11812            class A::B {};
11813          
11814          So, we ask cp_parser_class_name not to check accessibility.  
11815
11816          We do not know if we will see a class-name, or a
11817          template-name.  We look for a class-name first, in case the
11818          class-name is a template-id; if we looked for the
11819          template-name first we would stop after the template-name.  */
11820       cp_parser_parse_tentatively (parser);
11821       type = cp_parser_class_name (parser,
11822                                    /*typename_keyword_p=*/false,
11823                                    /*template_keyword_p=*/false,
11824                                    /*type_p=*/true,
11825                                    /*check_access_p=*/false,
11826                                    /*check_dependency_p=*/false,
11827                                    /*class_head_p=*/true);
11828       /* If that didn't work, ignore the nested-name-specifier.  */
11829       if (!cp_parser_parse_definitely (parser))
11830         {
11831           invalid_nested_name_p = true;
11832           id = cp_parser_identifier (parser);
11833           if (id == error_mark_node)
11834             id = NULL_TREE;
11835         }
11836       /* If we could not find a corresponding TYPE, treat this
11837          declaration like an unqualified declaration.  */
11838       if (type == error_mark_node)
11839         nested_name_specifier = NULL_TREE;
11840       /* Otherwise, count the number of templates used in TYPE and its
11841          containing scopes.  */
11842       else 
11843         {
11844           tree scope;
11845
11846           for (scope = TREE_TYPE (type); 
11847                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11848                scope = (TYPE_P (scope) 
11849                         ? TYPE_CONTEXT (scope)
11850                         : DECL_CONTEXT (scope))) 
11851             if (TYPE_P (scope) 
11852                 && CLASS_TYPE_P (scope)
11853                 && CLASSTYPE_TEMPLATE_INFO (scope)
11854                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11855               ++num_templates;
11856         }
11857     }
11858   /* Otherwise, the identifier is optional.  */
11859   else
11860     {
11861       /* We don't know whether what comes next is a template-id,
11862          an identifier, or nothing at all.  */
11863       cp_parser_parse_tentatively (parser);
11864       /* Check for a template-id.  */
11865       id = cp_parser_template_id (parser, 
11866                                   /*template_keyword_p=*/false,
11867                                   /*check_dependency_p=*/true);
11868       /* If that didn't work, it could still be an identifier.  */
11869       if (!cp_parser_parse_definitely (parser))
11870         {
11871           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11872             id = cp_parser_identifier (parser);
11873           else
11874             id = NULL_TREE;
11875         }
11876       else
11877         {
11878           template_id_p = true;
11879           ++num_templates;
11880         }
11881     }
11882
11883   /* If it's not a `:' or a `{' then we can't really be looking at a
11884      class-head, since a class-head only appears as part of a
11885      class-specifier.  We have to detect this situation before calling
11886      xref_tag, since that has irreversible side-effects.  */
11887   if (!cp_parser_next_token_starts_class_definition_p (parser))
11888     {
11889       cp_parser_error (parser, "expected `{' or `:'");
11890       return error_mark_node;
11891     }
11892
11893   /* At this point, we're going ahead with the class-specifier, even
11894      if some other problem occurs.  */
11895   cp_parser_commit_to_tentative_parse (parser);
11896   /* Issue the error about the overly-qualified name now.  */
11897   if (qualified_p)
11898     cp_parser_error (parser,
11899                      "global qualification of class name is invalid");
11900   else if (invalid_nested_name_p)
11901     cp_parser_error (parser,
11902                      "qualified name does not name a class");
11903   /* Make sure that the right number of template parameters were
11904      present.  */
11905   if (!cp_parser_check_template_parameters (parser, num_templates))
11906     /* If something went wrong, there is no point in even trying to
11907        process the class-definition.  */
11908     return NULL_TREE;
11909
11910   /* We do not need to defer access checks for entities declared
11911      within the class.  But, we do need to save any access checks that
11912      are currently deferred and restore them later, in case we are in
11913      the middle of something else.  */
11914   *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11915   if (*deferring_access_checks_p)
11916     *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11917
11918   /* Look up the type.  */
11919   if (template_id_p)
11920     {
11921       type = TREE_TYPE (id);
11922       maybe_process_partial_specialization (type);
11923     }
11924   else if (!nested_name_specifier)
11925     {
11926       /* If the class was unnamed, create a dummy name.  */
11927       if (!id)
11928         id = make_anon_name ();
11929       type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11930     }
11931   else
11932     {
11933       bool new_type_p;
11934       tree class_type;
11935
11936       /* Given:
11937
11938             template <typename T> struct S { struct T };
11939             template <typename T> struct S::T { };
11940
11941          we will get a TYPENAME_TYPE when processing the definition of
11942          `S::T'.  We need to resolve it to the actual type before we
11943          try to define it.  */
11944       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11945         {
11946           type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11947           if (type != error_mark_node)
11948             type = TYPE_NAME (type);
11949         }
11950
11951       maybe_process_partial_specialization (TREE_TYPE (type));
11952       class_type = current_class_type;
11953       type = TREE_TYPE (handle_class_head (class_key, 
11954                                            nested_name_specifier,
11955                                            type,
11956                                            attributes,
11957                                            /*defn_p=*/true,
11958                                            &new_type_p));
11959       if (type != error_mark_node)
11960         {
11961           if (!class_type && TYPE_CONTEXT (type))
11962             *nested_name_specifier_p = true;
11963           else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11964                                                class_type))
11965             *nested_name_specifier_p = true;
11966         }
11967     }
11968   /* Indicate whether this class was declared as a `class' or as a
11969      `struct'.  */
11970   if (TREE_CODE (type) == RECORD_TYPE)
11971     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11972   cp_parser_check_class_key (class_key, type);
11973
11974   /* Enter the scope containing the class; the names of base classes
11975      should be looked up in that context.  For example, given:
11976
11977        struct A { struct B {}; struct C; };
11978        struct A::C : B {};
11979
11980      is valid.  */
11981   if (nested_name_specifier)
11982     push_scope (nested_name_specifier);
11983   /* Now, look for the base-clause.  */
11984   token = cp_lexer_peek_token (parser->lexer);
11985   if (token->type == CPP_COLON)
11986     {
11987       tree bases;
11988
11989       /* Get the list of base-classes.  */
11990       bases = cp_parser_base_clause (parser);
11991       /* Process them.  */
11992       xref_basetypes (type, bases);
11993     }
11994   /* Leave the scope given by the nested-name-specifier.  We will
11995      enter the class scope itself while processing the members.  */
11996   if (nested_name_specifier)
11997     pop_scope (nested_name_specifier);
11998
11999   return type;
12000 }
12001
12002 /* Parse a class-key.
12003
12004    class-key:
12005      class
12006      struct
12007      union
12008
12009    Returns the kind of class-key specified, or none_type to indicate
12010    error.  */
12011
12012 static enum tag_types
12013 cp_parser_class_key (parser)
12014      cp_parser *parser;
12015 {
12016   cp_token *token;
12017   enum tag_types tag_type;
12018
12019   /* Look for the class-key.  */
12020   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12021   if (!token)
12022     return none_type;
12023
12024   /* Check to see if the TOKEN is a class-key.  */
12025   tag_type = cp_parser_token_is_class_key (token);
12026   if (!tag_type)
12027     cp_parser_error (parser, "expected class-key");
12028   return tag_type;
12029 }
12030
12031 /* Parse an (optional) member-specification.
12032
12033    member-specification:
12034      member-declaration member-specification [opt]
12035      access-specifier : member-specification [opt]  */
12036
12037 static void
12038 cp_parser_member_specification_opt (parser)
12039      cp_parser *parser;
12040 {
12041   while (true)
12042     {
12043       cp_token *token;
12044       enum rid keyword;
12045
12046       /* Peek at the next token.  */
12047       token = cp_lexer_peek_token (parser->lexer);
12048       /* If it's a `}', or EOF then we've seen all the members.  */
12049       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12050         break;
12051
12052       /* See if this token is a keyword.  */
12053       keyword = token->keyword;
12054       switch (keyword)
12055         {
12056         case RID_PUBLIC:
12057         case RID_PROTECTED:
12058         case RID_PRIVATE:
12059           /* Consume the access-specifier.  */
12060           cp_lexer_consume_token (parser->lexer);
12061           /* Remember which access-specifier is active.  */
12062           current_access_specifier = token->value;
12063           /* Look for the `:'.  */
12064           cp_parser_require (parser, CPP_COLON, "`:'");
12065           break;
12066
12067         default:
12068           /* Otherwise, the next construction must be a
12069              member-declaration.  */
12070           cp_parser_member_declaration (parser);
12071           reset_type_access_control ();
12072         }
12073     }
12074 }
12075
12076 /* Parse a member-declaration.  
12077
12078    member-declaration:
12079      decl-specifier-seq [opt] member-declarator-list [opt] ;
12080      function-definition ; [opt]
12081      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12082      using-declaration
12083      template-declaration 
12084
12085    member-declarator-list:
12086      member-declarator
12087      member-declarator-list , member-declarator
12088
12089    member-declarator:
12090      declarator pure-specifier [opt] 
12091      declarator constant-initializer [opt]
12092      identifier [opt] : constant-expression 
12093
12094    GNU Extensions:
12095
12096    member-declaration:
12097      __extension__ member-declaration
12098
12099    member-declarator:
12100      declarator attributes [opt] pure-specifier [opt]
12101      declarator attributes [opt] constant-initializer [opt]
12102      identifier [opt] attributes [opt] : constant-expression  */
12103
12104 static void
12105 cp_parser_member_declaration (parser)
12106      cp_parser *parser;
12107 {
12108   tree decl_specifiers;
12109   tree prefix_attributes;
12110   tree decl;
12111   bool declares_class_or_enum;
12112   bool friend_p;
12113   cp_token *token;
12114   int saved_pedantic;
12115
12116   /* Check for the `__extension__' keyword.  */
12117   if (cp_parser_extension_opt (parser, &saved_pedantic))
12118     {
12119       /* Recurse.  */
12120       cp_parser_member_declaration (parser);
12121       /* Restore the old value of the PEDANTIC flag.  */
12122       pedantic = saved_pedantic;
12123
12124       return;
12125     }
12126
12127   /* Check for a template-declaration.  */
12128   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12129     {
12130       /* Parse the template-declaration.  */
12131       cp_parser_template_declaration (parser, /*member_p=*/true);
12132
12133       return;
12134     }
12135
12136   /* Check for a using-declaration.  */
12137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12138     {
12139       /* Parse the using-declaration.  */
12140       cp_parser_using_declaration (parser);
12141
12142       return;
12143     }
12144   
12145   /* We can't tell whether we're looking at a declaration or a
12146      function-definition.  */
12147   cp_parser_parse_tentatively (parser);
12148
12149   /* Parse the decl-specifier-seq.  */
12150   decl_specifiers 
12151     = cp_parser_decl_specifier_seq (parser,
12152                                     CP_PARSER_FLAGS_OPTIONAL,
12153                                     &prefix_attributes,
12154                                     &declares_class_or_enum);
12155   /* If there is no declarator, then the decl-specifier-seq should
12156      specify a type.  */
12157   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12158     {
12159       /* If there was no decl-specifier-seq, and the next token is a
12160          `;', then we have something like:
12161
12162            struct S { ; };
12163
12164          [class.mem]
12165
12166          Each member-declaration shall declare at least one member
12167          name of the class.  */
12168       if (!decl_specifiers)
12169         {
12170           if (pedantic)
12171             pedwarn ("extra semicolon");
12172         }
12173       else 
12174         {
12175           tree type;
12176           
12177           /* See if this declaration is a friend.  */
12178           friend_p = cp_parser_friend_p (decl_specifiers);
12179           /* If there were decl-specifiers, check to see if there was
12180              a class-declaration.  */
12181           type = check_tag_decl (decl_specifiers);
12182           /* Nested classes have already been added to the class, but
12183              a `friend' needs to be explicitly registered.  */
12184           if (friend_p)
12185             {
12186               /* If the `friend' keyword was present, the friend must
12187                  be introduced with a class-key.  */
12188                if (!declares_class_or_enum)
12189                  error ("a class-key must be used when declaring a friend");
12190                /* In this case:
12191
12192                     template <typename T> struct A { 
12193                       friend struct A<T>::B; 
12194                     };
12195  
12196                   A<T>::B will be represented by a TYPENAME_TYPE, and
12197                   therefore not recognized by check_tag_decl.  */
12198                if (!type)
12199                  {
12200                    tree specifier;
12201
12202                    for (specifier = decl_specifiers; 
12203                         specifier;
12204                         specifier = TREE_CHAIN (specifier))
12205                      {
12206                        tree s = TREE_VALUE (specifier);
12207
12208                        if (TREE_CODE (s) == IDENTIFIER_NODE
12209                            && IDENTIFIER_GLOBAL_VALUE (s))
12210                          type = IDENTIFIER_GLOBAL_VALUE (s);
12211                        if (TREE_CODE (s) == TYPE_DECL)
12212                          s = TREE_TYPE (s);
12213                        if (TYPE_P (s))
12214                          {
12215                            type = s;
12216                            break;
12217                          }
12218                      }
12219                  }
12220                if (!type)
12221                  error ("friend declaration does not name a class or "
12222                         "function");
12223                else
12224                  make_friend_class (current_class_type, type);
12225             }
12226           /* If there is no TYPE, an error message will already have
12227              been issued.  */
12228           else if (!type)
12229             ;
12230           /* An anonymous aggregate has to be handled specially; such
12231              a declaration really declares a data member (with a
12232              particular type), as opposed to a nested class.  */
12233           else if (ANON_AGGR_TYPE_P (type))
12234             {
12235               /* Remove constructors and such from TYPE, now that we
12236                  know it is an anoymous aggregate.  */
12237               fixup_anonymous_aggr (type);
12238               /* And make the corresponding data member.  */
12239               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12240               /* Add it to the class.  */
12241               finish_member_declaration (decl);
12242             }
12243         }
12244     }
12245   else
12246     {
12247       /* See if these declarations will be friends.  */
12248       friend_p = cp_parser_friend_p (decl_specifiers);
12249
12250       /* Keep going until we hit the `;' at the end of the 
12251          declaration.  */
12252       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12253         {
12254           tree attributes = NULL_TREE;
12255           tree first_attribute;
12256
12257           /* Peek at the next token.  */
12258           token = cp_lexer_peek_token (parser->lexer);
12259
12260           /* Check for a bitfield declaration.  */
12261           if (token->type == CPP_COLON
12262               || (token->type == CPP_NAME
12263                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12264                   == CPP_COLON))
12265             {
12266               tree identifier;
12267               tree width;
12268
12269               /* Get the name of the bitfield.  Note that we cannot just
12270                  check TOKEN here because it may have been invalidated by
12271                  the call to cp_lexer_peek_nth_token above.  */
12272               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12273                 identifier = cp_parser_identifier (parser);
12274               else
12275                 identifier = NULL_TREE;
12276
12277               /* Consume the `:' token.  */
12278               cp_lexer_consume_token (parser->lexer);
12279               /* Get the width of the bitfield.  */
12280               width = cp_parser_constant_expression (parser);
12281
12282               /* Look for attributes that apply to the bitfield.  */
12283               attributes = cp_parser_attributes_opt (parser);
12284               /* Remember which attributes are prefix attributes and
12285                  which are not.  */
12286               first_attribute = attributes;
12287               /* Combine the attributes.  */
12288               attributes = chainon (prefix_attributes, attributes);
12289
12290               /* Create the bitfield declaration.  */
12291               decl = grokbitfield (identifier, 
12292                                    decl_specifiers,
12293                                    width);
12294               /* Apply the attributes.  */
12295               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12296             }
12297           else
12298             {
12299               tree declarator;
12300               tree initializer;
12301               tree asm_specification;
12302               bool ctor_dtor_or_conv_p;
12303
12304               /* Parse the declarator.  */
12305               declarator 
12306                 = cp_parser_declarator (parser,
12307                                         /*abstract_p=*/false,
12308                                         &ctor_dtor_or_conv_p);
12309
12310               /* If something went wrong parsing the declarator, make sure
12311                  that we at least consume some tokens.  */
12312               if (declarator == error_mark_node)
12313                 {
12314                   /* Skip to the end of the statement.  */
12315                   cp_parser_skip_to_end_of_statement (parser);
12316                   break;
12317                 }
12318
12319               /* Look for an asm-specification.  */
12320               asm_specification = cp_parser_asm_specification_opt (parser);
12321               /* Look for attributes that apply to the declaration.  */
12322               attributes = cp_parser_attributes_opt (parser);
12323               /* Remember which attributes are prefix attributes and
12324                  which are not.  */
12325               first_attribute = attributes;
12326               /* Combine the attributes.  */
12327               attributes = chainon (prefix_attributes, attributes);
12328
12329               /* If it's an `=', then we have a constant-initializer or a
12330                  pure-specifier.  It is not correct to parse the
12331                  initializer before registering the member declaration
12332                  since the member declaration should be in scope while
12333                  its initializer is processed.  However, the rest of the
12334                  front end does not yet provide an interface that allows
12335                  us to handle this correctly.  */
12336               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12337                 {
12338                   /* In [class.mem]:
12339
12340                      A pure-specifier shall be used only in the declaration of
12341                      a virtual function.  
12342
12343                      A member-declarator can contain a constant-initializer
12344                      only if it declares a static member of integral or
12345                      enumeration type.  
12346
12347                      Therefore, if the DECLARATOR is for a function, we look
12348                      for a pure-specifier; otherwise, we look for a
12349                      constant-initializer.  When we call `grokfield', it will
12350                      perform more stringent semantics checks.  */
12351                   if (TREE_CODE (declarator) == CALL_EXPR)
12352                     initializer = cp_parser_pure_specifier (parser);
12353                   else
12354                     {
12355                       /* This declaration cannot be a function
12356                          definition.  */
12357                       cp_parser_commit_to_tentative_parse (parser);
12358                       /* Parse the initializer.  */
12359                       initializer = cp_parser_constant_initializer (parser);
12360                     }
12361                 }
12362               /* Otherwise, there is no initializer.  */
12363               else
12364                 initializer = NULL_TREE;
12365
12366               /* See if we are probably looking at a function
12367                  definition.  We are certainly not looking at at a
12368                  member-declarator.  Calling `grokfield' has
12369                  side-effects, so we must not do it unless we are sure
12370                  that we are looking at a member-declarator.  */
12371               if (cp_parser_token_starts_function_definition_p 
12372                   (cp_lexer_peek_token (parser->lexer)))
12373                 decl = error_mark_node;
12374               else
12375                 /* Create the declaration.  */
12376                 decl = grokfield (declarator, 
12377                                   decl_specifiers, 
12378                                   initializer,
12379                                   asm_specification,
12380                                   attributes);
12381             }
12382
12383           /* Reset PREFIX_ATTRIBUTES.  */
12384           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12385             attributes = TREE_CHAIN (attributes);
12386           if (attributes)
12387             TREE_CHAIN (attributes) = NULL_TREE;
12388
12389           /* If there is any qualification still in effect, clear it
12390              now; we will be starting fresh with the next declarator.  */
12391           parser->scope = NULL_TREE;
12392           parser->qualifying_scope = NULL_TREE;
12393           parser->object_scope = NULL_TREE;
12394           /* If it's a `,', then there are more declarators.  */
12395           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12396             cp_lexer_consume_token (parser->lexer);
12397           /* If the next token isn't a `;', then we have a parse error.  */
12398           else if (cp_lexer_next_token_is_not (parser->lexer,
12399                                                CPP_SEMICOLON))
12400             {
12401               cp_parser_error (parser, "expected `;'");
12402               /* Skip tokens until we find a `;'  */
12403               cp_parser_skip_to_end_of_statement (parser);
12404
12405               break;
12406             }
12407
12408           if (decl)
12409             {
12410               /* Add DECL to the list of members.  */
12411               if (!friend_p)
12412                 finish_member_declaration (decl);
12413
12414               /* If DECL is a function, we must return
12415                  to parse it later.  (Even though there is no definition,
12416                  there might be default arguments that need handling.)  */
12417               if (TREE_CODE (decl) == FUNCTION_DECL)
12418                 TREE_VALUE (parser->unparsed_functions_queues)
12419                   = tree_cons (current_class_type, decl, 
12420                                TREE_VALUE (parser->unparsed_functions_queues));
12421             }
12422         }
12423     }
12424
12425   /* If everything went well, look for the `;'.  */
12426   if (cp_parser_parse_definitely (parser))
12427     {
12428       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12429       return;
12430     }
12431
12432   /* Parse the function-definition.  */
12433   decl = cp_parser_function_definition (parser, &friend_p);
12434   /* If the member was not a friend, declare it here.  */
12435   if (!friend_p)
12436     finish_member_declaration (decl);
12437   /* Peek at the next token.  */
12438   token = cp_lexer_peek_token (parser->lexer);
12439   /* If the next token is a semicolon, consume it.  */
12440   if (token->type == CPP_SEMICOLON)
12441     cp_lexer_consume_token (parser->lexer);
12442 }
12443
12444 /* Parse a pure-specifier.
12445
12446    pure-specifier:
12447      = 0
12448
12449    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12450    Otherwiser, ERROR_MARK_NODE is returned.  */
12451
12452 static tree
12453 cp_parser_pure_specifier (parser)
12454      cp_parser *parser;
12455 {
12456   cp_token *token;
12457
12458   /* Look for the `=' token.  */
12459   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12460     return error_mark_node;
12461   /* Look for the `0' token.  */
12462   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12463   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12464      to get information from the lexer about how the number was
12465      spelled in order to fix this problem.  */
12466   if (!token || !integer_zerop (token->value))
12467     return error_mark_node;
12468
12469   return integer_zero_node;
12470 }
12471
12472 /* Parse a constant-initializer.
12473
12474    constant-initializer:
12475      = constant-expression
12476
12477    Returns a representation of the constant-expression.  */
12478
12479 static tree
12480 cp_parser_constant_initializer (parser)
12481      cp_parser *parser;
12482 {
12483   /* Look for the `=' token.  */
12484   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12485     return error_mark_node;
12486
12487   /* It is invalid to write:
12488
12489        struct S { static const int i = { 7 }; };
12490
12491      */
12492   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12493     {
12494       cp_parser_error (parser,
12495                        "a brace-enclosed initializer is not allowed here");
12496       /* Consume the opening brace.  */
12497       cp_lexer_consume_token (parser->lexer);
12498       /* Skip the initializer.  */
12499       cp_parser_skip_to_closing_brace (parser);
12500       /* Look for the trailing `}'.  */
12501       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12502       
12503       return error_mark_node;
12504     }
12505
12506   return cp_parser_constant_expression (parser);
12507 }
12508
12509 /* Derived classes [gram.class.derived] */
12510
12511 /* Parse a base-clause.
12512
12513    base-clause:
12514      : base-specifier-list  
12515
12516    base-specifier-list:
12517      base-specifier
12518      base-specifier-list , base-specifier
12519
12520    Returns a TREE_LIST representing the base-classes, in the order in
12521    which they were declared.  The representation of each node is as
12522    described by cp_parser_base_specifier.  
12523
12524    In the case that no bases are specified, this function will return
12525    NULL_TREE, not ERROR_MARK_NODE.  */
12526
12527 static tree
12528 cp_parser_base_clause (parser)
12529      cp_parser *parser;
12530 {
12531   tree bases = NULL_TREE;
12532
12533   /* Look for the `:' that begins the list.  */
12534   cp_parser_require (parser, CPP_COLON, "`:'");
12535
12536   /* Scan the base-specifier-list.  */
12537   while (true)
12538     {
12539       cp_token *token;
12540       tree base;
12541
12542       /* Look for the base-specifier.  */
12543       base = cp_parser_base_specifier (parser);
12544       /* Add BASE to the front of the list.  */
12545       if (base != error_mark_node)
12546         {
12547           TREE_CHAIN (base) = bases;
12548           bases = base;
12549         }
12550       /* Peek at the next token.  */
12551       token = cp_lexer_peek_token (parser->lexer);
12552       /* If it's not a comma, then the list is complete.  */
12553       if (token->type != CPP_COMMA)
12554         break;
12555       /* Consume the `,'.  */
12556       cp_lexer_consume_token (parser->lexer);
12557     }
12558
12559   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12560      base class had a qualified name.  However, the next name that
12561      appears is certainly not qualified.  */
12562   parser->scope = NULL_TREE;
12563   parser->qualifying_scope = NULL_TREE;
12564   parser->object_scope = NULL_TREE;
12565
12566   return nreverse (bases);
12567 }
12568
12569 /* Parse a base-specifier.
12570
12571    base-specifier:
12572      :: [opt] nested-name-specifier [opt] class-name
12573      virtual access-specifier [opt] :: [opt] nested-name-specifier
12574        [opt] class-name
12575      access-specifier virtual [opt] :: [opt] nested-name-specifier
12576        [opt] class-name
12577
12578    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12579    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12580    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12581    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12582        
12583 static tree
12584 cp_parser_base_specifier (parser)
12585      cp_parser *parser;
12586 {
12587   cp_token *token;
12588   bool done = false;
12589   bool virtual_p = false;
12590   bool duplicate_virtual_error_issued_p = false;
12591   bool duplicate_access_error_issued_p = false;
12592   bool class_scope_p;
12593   access_kind access = ak_none;
12594   tree access_node;
12595   tree type;
12596
12597   /* Process the optional `virtual' and `access-specifier'.  */
12598   while (!done)
12599     {
12600       /* Peek at the next token.  */
12601       token = cp_lexer_peek_token (parser->lexer);
12602       /* Process `virtual'.  */
12603       switch (token->keyword)
12604         {
12605         case RID_VIRTUAL:
12606           /* If `virtual' appears more than once, issue an error.  */
12607           if (virtual_p && !duplicate_virtual_error_issued_p)
12608             {
12609               cp_parser_error (parser,
12610                                "`virtual' specified more than once in base-specified");
12611               duplicate_virtual_error_issued_p = true;
12612             }
12613
12614           virtual_p = true;
12615
12616           /* Consume the `virtual' token.  */
12617           cp_lexer_consume_token (parser->lexer);
12618
12619           break;
12620
12621         case RID_PUBLIC:
12622         case RID_PROTECTED:
12623         case RID_PRIVATE:
12624           /* If more than one access specifier appears, issue an
12625              error.  */
12626           if (access != ak_none && !duplicate_access_error_issued_p)
12627             {
12628               cp_parser_error (parser,
12629                                "more than one access specifier in base-specified");
12630               duplicate_access_error_issued_p = true;
12631             }
12632
12633           access = ((access_kind) 
12634                     tree_low_cst (ridpointers[(int) token->keyword],
12635                                   /*pos=*/1));
12636
12637           /* Consume the access-specifier.  */
12638           cp_lexer_consume_token (parser->lexer);
12639
12640           break;
12641
12642         default:
12643           done = true;
12644           break;
12645         }
12646     }
12647
12648   /* Map `virtual_p' and `access' onto one of the access 
12649      tree-nodes.  */
12650   if (!virtual_p)
12651     switch (access)
12652       {
12653       case ak_none:
12654         access_node = access_default_node;
12655         break;
12656       case ak_public:
12657         access_node = access_public_node;
12658         break;
12659       case ak_protected:
12660         access_node = access_protected_node;
12661         break;
12662       case ak_private:
12663         access_node = access_private_node;
12664         break;
12665       default:
12666         abort ();
12667       }
12668   else
12669     switch (access)
12670       {
12671       case ak_none:
12672         access_node = access_default_virtual_node;
12673         break;
12674       case ak_public:
12675         access_node = access_public_virtual_node;
12676         break;
12677       case ak_protected:
12678         access_node = access_protected_virtual_node;
12679         break;
12680       case ak_private:
12681         access_node = access_private_virtual_node;
12682         break;
12683       default:
12684         abort ();
12685       }
12686
12687   /* Look for the optional `::' operator.  */
12688   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12689   /* Look for the nested-name-specifier.  The simplest way to
12690      implement:
12691
12692        [temp.res]
12693
12694        The keyword `typename' is not permitted in a base-specifier or
12695        mem-initializer; in these contexts a qualified name that
12696        depends on a template-parameter is implicitly assumed to be a
12697        type name.
12698
12699      is to pretend that we have seen the `typename' keyword at this
12700      point.  */ 
12701   cp_parser_nested_name_specifier_opt (parser,
12702                                        /*typename_keyword_p=*/true,
12703                                        /*check_dependency_p=*/true,
12704                                        /*type_p=*/true);
12705   /* If the base class is given by a qualified name, assume that names
12706      we see are type names or templates, as appropriate.  */
12707   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12708   /* Finally, look for the class-name.  */
12709   type = cp_parser_class_name (parser, 
12710                                class_scope_p,
12711                                class_scope_p,
12712                                /*type_p=*/true,
12713                                /*check_access=*/true,
12714                                /*check_dependency_p=*/true,
12715                                /*class_head_p=*/false);
12716
12717   if (type == error_mark_node)
12718     return error_mark_node;
12719
12720   return finish_base_specifier (access_node, TREE_TYPE (type));
12721 }
12722
12723 /* Exception handling [gram.exception] */
12724
12725 /* Parse an (optional) exception-specification.
12726
12727    exception-specification:
12728      throw ( type-id-list [opt] )
12729
12730    Returns a TREE_LIST representing the exception-specification.  The
12731    TREE_VALUE of each node is a type.  */
12732
12733 static tree
12734 cp_parser_exception_specification_opt (parser)
12735      cp_parser *parser;
12736 {
12737   cp_token *token;
12738   tree type_id_list;
12739
12740   /* Peek at the next token.  */
12741   token = cp_lexer_peek_token (parser->lexer);
12742   /* If it's not `throw', then there's no exception-specification.  */
12743   if (!cp_parser_is_keyword (token, RID_THROW))
12744     return NULL_TREE;
12745
12746   /* Consume the `throw'.  */
12747   cp_lexer_consume_token (parser->lexer);
12748
12749   /* Look for the `('.  */
12750   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12751
12752   /* Peek at the next token.  */
12753   token = cp_lexer_peek_token (parser->lexer);
12754   /* If it's not a `)', then there is a type-id-list.  */
12755   if (token->type != CPP_CLOSE_PAREN)
12756     {
12757       const char *saved_message;
12758
12759       /* Types may not be defined in an exception-specification.  */
12760       saved_message = parser->type_definition_forbidden_message;
12761       parser->type_definition_forbidden_message
12762         = "types may not be defined in an exception-specification";
12763       /* Parse the type-id-list.  */
12764       type_id_list = cp_parser_type_id_list (parser);
12765       /* Restore the saved message.  */
12766       parser->type_definition_forbidden_message = saved_message;
12767     }
12768   else
12769     type_id_list = empty_except_spec;
12770
12771   /* Look for the `)'.  */
12772   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12773
12774   return type_id_list;
12775 }
12776
12777 /* Parse an (optional) type-id-list.
12778
12779    type-id-list:
12780      type-id
12781      type-id-list , type-id
12782
12783    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12784    in the order that the types were presented.  */
12785
12786 static tree
12787 cp_parser_type_id_list (parser)
12788      cp_parser *parser;
12789 {
12790   tree types = NULL_TREE;
12791
12792   while (true)
12793     {
12794       cp_token *token;
12795       tree type;
12796
12797       /* Get the next type-id.  */
12798       type = cp_parser_type_id (parser);
12799       /* Add it to the list.  */
12800       types = add_exception_specifier (types, type, /*complain=*/1);
12801       /* Peek at the next token.  */
12802       token = cp_lexer_peek_token (parser->lexer);
12803       /* If it is not a `,', we are done.  */
12804       if (token->type != CPP_COMMA)
12805         break;
12806       /* Consume the `,'.  */
12807       cp_lexer_consume_token (parser->lexer);
12808     }
12809
12810   return nreverse (types);
12811 }
12812
12813 /* Parse a try-block.
12814
12815    try-block:
12816      try compound-statement handler-seq  */
12817
12818 static tree
12819 cp_parser_try_block (parser)
12820      cp_parser *parser;
12821 {
12822   tree try_block;
12823
12824   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12825   try_block = begin_try_block ();
12826   cp_parser_compound_statement (parser);
12827   finish_try_block (try_block);
12828   cp_parser_handler_seq (parser);
12829   finish_handler_sequence (try_block);
12830
12831   return try_block;
12832 }
12833
12834 /* Parse a function-try-block.
12835
12836    function-try-block:
12837      try ctor-initializer [opt] function-body handler-seq  */
12838
12839 static bool
12840 cp_parser_function_try_block (parser)
12841      cp_parser *parser;
12842 {
12843   tree try_block;
12844   bool ctor_initializer_p;
12845
12846   /* Look for the `try' keyword.  */
12847   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12848     return false;
12849   /* Let the rest of the front-end know where we are.  */
12850   try_block = begin_function_try_block ();
12851   /* Parse the function-body.  */
12852   ctor_initializer_p 
12853     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12854   /* We're done with the `try' part.  */
12855   finish_function_try_block (try_block);
12856   /* Parse the handlers.  */
12857   cp_parser_handler_seq (parser);
12858   /* We're done with the handlers.  */
12859   finish_function_handler_sequence (try_block);
12860
12861   return ctor_initializer_p;
12862 }
12863
12864 /* Parse a handler-seq.
12865
12866    handler-seq:
12867      handler handler-seq [opt]  */
12868
12869 static void
12870 cp_parser_handler_seq (parser)
12871      cp_parser *parser;
12872 {
12873   while (true)
12874     {
12875       cp_token *token;
12876
12877       /* Parse the handler.  */
12878       cp_parser_handler (parser);
12879       /* Peek at the next token.  */
12880       token = cp_lexer_peek_token (parser->lexer);
12881       /* If it's not `catch' then there are no more handlers.  */
12882       if (!cp_parser_is_keyword (token, RID_CATCH))
12883         break;
12884     }
12885 }
12886
12887 /* Parse a handler.
12888
12889    handler:
12890      catch ( exception-declaration ) compound-statement  */
12891
12892 static void
12893 cp_parser_handler (parser)
12894      cp_parser *parser;
12895 {
12896   tree handler;
12897   tree declaration;
12898
12899   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12900   handler = begin_handler ();
12901   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12902   declaration = cp_parser_exception_declaration (parser);
12903   finish_handler_parms (declaration, handler);
12904   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12905   cp_parser_compound_statement (parser);
12906   finish_handler (handler);
12907 }
12908
12909 /* Parse an exception-declaration.
12910
12911    exception-declaration:
12912      type-specifier-seq declarator
12913      type-specifier-seq abstract-declarator
12914      type-specifier-seq
12915      ...  
12916
12917    Returns a VAR_DECL for the declaration, or NULL_TREE if the
12918    ellipsis variant is used.  */
12919
12920 static tree
12921 cp_parser_exception_declaration (parser)
12922      cp_parser *parser;
12923 {
12924   tree type_specifiers;
12925   tree declarator;
12926   const char *saved_message;
12927
12928   /* If it's an ellipsis, it's easy to handle.  */
12929   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12930     {
12931       /* Consume the `...' token.  */
12932       cp_lexer_consume_token (parser->lexer);
12933       return NULL_TREE;
12934     }
12935
12936   /* Types may not be defined in exception-declarations.  */
12937   saved_message = parser->type_definition_forbidden_message;
12938   parser->type_definition_forbidden_message
12939     = "types may not be defined in exception-declarations";
12940
12941   /* Parse the type-specifier-seq.  */
12942   type_specifiers = cp_parser_type_specifier_seq (parser);
12943   /* If it's a `)', then there is no declarator.  */
12944   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12945     declarator = NULL_TREE;
12946   else
12947     {
12948       /* Otherwise, we can't be sure whether we are looking at a
12949          direct, or an abstract, declarator.  */
12950       cp_parser_parse_tentatively (parser);
12951       /* Try an ordinary declarator.  */
12952       declarator = cp_parser_declarator (parser,
12953                                          /*abstract_p=*/false,
12954                                          /*ctor_dtor_or_conv_p=*/NULL);
12955       /* If that didn't work, try an abstract declarator.  */
12956       if (!cp_parser_parse_definitely (parser))
12957         declarator = cp_parser_declarator (parser,
12958                                            /*abstract_p=*/true,
12959                                            /*ctor_dtor_or_conv_p=*/NULL);
12960     }
12961
12962   /* Restore the saved message.  */
12963   parser->type_definition_forbidden_message = saved_message;
12964
12965   return start_handler_parms (type_specifiers, declarator);
12966 }
12967
12968 /* Parse a throw-expression. 
12969
12970    throw-expression:
12971      throw assignment-expresion [opt]
12972
12973    Returns a THROW_EXPR representing the throw-expression.  */
12974
12975 static tree
12976 cp_parser_throw_expression (parser)
12977      cp_parser *parser;
12978 {
12979   tree expression;
12980
12981   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12982   /* We can't be sure if there is an assignment-expression or not.  */
12983   cp_parser_parse_tentatively (parser);
12984   /* Try it.  */
12985   expression = cp_parser_assignment_expression (parser);
12986   /* If it didn't work, this is just a rethrow.  */
12987   if (!cp_parser_parse_definitely (parser))
12988     expression = NULL_TREE;
12989
12990   return build_throw (expression);
12991 }
12992
12993 /* GNU Extensions */
12994
12995 /* Parse an (optional) asm-specification.
12996
12997    asm-specification:
12998      asm ( string-literal )
12999
13000    If the asm-specification is present, returns a STRING_CST
13001    corresponding to the string-literal.  Otherwise, returns
13002    NULL_TREE.  */
13003
13004 static tree
13005 cp_parser_asm_specification_opt (parser)
13006      cp_parser *parser;
13007 {
13008   cp_token *token;
13009   tree asm_specification;
13010
13011   /* Peek at the next token.  */
13012   token = cp_lexer_peek_token (parser->lexer);
13013   /* If the next token isn't the `asm' keyword, then there's no 
13014      asm-specification.  */
13015   if (!cp_parser_is_keyword (token, RID_ASM))
13016     return NULL_TREE;
13017
13018   /* Consume the `asm' token.  */
13019   cp_lexer_consume_token (parser->lexer);
13020   /* Look for the `('.  */
13021   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13022
13023   /* Look for the string-literal.  */
13024   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13025   if (token)
13026     asm_specification = token->value;
13027   else
13028     asm_specification = NULL_TREE;
13029
13030   /* Look for the `)'.  */
13031   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13032
13033   return asm_specification;
13034 }
13035
13036 /* Parse an asm-operand-list.  
13037
13038    asm-operand-list:
13039      asm-operand
13040      asm-operand-list , asm-operand
13041      
13042    asm-operand:
13043      string-literal ( expression )  
13044      [ string-literal ] string-literal ( expression )
13045
13046    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13047    each node is the expression.  The TREE_PURPOSE is itself a
13048    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13049    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13050    is a STRING_CST for the string literal before the parenthesis.  */
13051
13052 static tree
13053 cp_parser_asm_operand_list (parser)
13054      cp_parser *parser;
13055 {
13056   tree asm_operands = NULL_TREE;
13057
13058   while (true)
13059     {
13060       tree string_literal;
13061       tree expression;
13062       tree name;
13063       cp_token *token;
13064       
13065       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13066         {
13067           /* Consume the `[' token.  */
13068           cp_lexer_consume_token (parser->lexer);
13069           /* Read the operand name.  */
13070           name = cp_parser_identifier (parser);
13071           if (name != error_mark_node) 
13072             name = build_string (IDENTIFIER_LENGTH (name),
13073                                  IDENTIFIER_POINTER (name));
13074           /* Look for the closing `]'.  */
13075           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13076         }
13077       else
13078         name = NULL_TREE;
13079       /* Look for the string-literal.  */
13080       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13081       string_literal = token ? token->value : error_mark_node;
13082       /* Look for the `('.  */
13083       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13084       /* Parse the expression.  */
13085       expression = cp_parser_expression (parser);
13086       /* Look for the `)'.  */
13087       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13088       /* Add this operand to the list.  */
13089       asm_operands = tree_cons (build_tree_list (name, string_literal),
13090                                 expression, 
13091                                 asm_operands);
13092       /* If the next token is not a `,', there are no more 
13093          operands.  */
13094       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13095         break;
13096       /* Consume the `,'.  */
13097       cp_lexer_consume_token (parser->lexer);
13098     }
13099
13100   return nreverse (asm_operands);
13101 }
13102
13103 /* Parse an asm-clobber-list.  
13104
13105    asm-clobber-list:
13106      string-literal
13107      asm-clobber-list , string-literal  
13108
13109    Returns a TREE_LIST, indicating the clobbers in the order that they
13110    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13111
13112 static tree
13113 cp_parser_asm_clobber_list (parser)
13114      cp_parser *parser;
13115 {
13116   tree clobbers = NULL_TREE;
13117
13118   while (true)
13119     {
13120       cp_token *token;
13121       tree string_literal;
13122
13123       /* Look for the string literal.  */
13124       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13125       string_literal = token ? token->value : error_mark_node;
13126       /* Add it to the list.  */
13127       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13128       /* If the next token is not a `,', then the list is 
13129          complete.  */
13130       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13131         break;
13132       /* Consume the `,' token.  */
13133       cp_lexer_consume_token (parser->lexer);
13134     }
13135
13136   return clobbers;
13137 }
13138
13139 /* Parse an (optional) series of attributes.
13140
13141    attributes:
13142      attributes attribute
13143
13144    attribute:
13145      __attribute__ (( attribute-list [opt] ))  
13146
13147    The return value is as for cp_parser_attribute_list.  */
13148      
13149 static tree
13150 cp_parser_attributes_opt (parser)
13151      cp_parser *parser;
13152 {
13153   tree attributes = NULL_TREE;
13154
13155   while (true)
13156     {
13157       cp_token *token;
13158       tree attribute_list;
13159
13160       /* Peek at the next token.  */
13161       token = cp_lexer_peek_token (parser->lexer);
13162       /* If it's not `__attribute__', then we're done.  */
13163       if (token->keyword != RID_ATTRIBUTE)
13164         break;
13165
13166       /* Consume the `__attribute__' keyword.  */
13167       cp_lexer_consume_token (parser->lexer);
13168       /* Look for the two `(' tokens.  */
13169       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13170       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13171
13172       /* Peek at the next token.  */
13173       token = cp_lexer_peek_token (parser->lexer);
13174       if (token->type != CPP_CLOSE_PAREN)
13175         /* Parse the attribute-list.  */
13176         attribute_list = cp_parser_attribute_list (parser);
13177       else
13178         /* If the next token is a `)', then there is no attribute
13179            list.  */
13180         attribute_list = NULL;
13181
13182       /* Look for the two `)' tokens.  */
13183       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13184       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13185
13186       /* Add these new attributes to the list.  */
13187       attributes = chainon (attributes, attribute_list);
13188     }
13189
13190   return attributes;
13191 }
13192
13193 /* Parse an attribute-list.  
13194
13195    attribute-list:  
13196      attribute 
13197      attribute-list , attribute
13198
13199    attribute:
13200      identifier     
13201      identifier ( identifier )
13202      identifier ( identifier , expression-list )
13203      identifier ( expression-list ) 
13204
13205    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13206    TREE_PURPOSE of each node is the identifier indicating which
13207    attribute is in use.  The TREE_VALUE represents the arguments, if
13208    any.  */
13209
13210 static tree
13211 cp_parser_attribute_list (parser)
13212      cp_parser *parser;
13213 {
13214   tree attribute_list = NULL_TREE;
13215
13216   while (true)
13217     {
13218       cp_token *token;
13219       tree identifier;
13220       tree attribute;
13221
13222       /* Look for the identifier.  We also allow keywords here; for
13223          example `__attribute__ ((const))' is legal.  */
13224       token = cp_lexer_peek_token (parser->lexer);
13225       if (token->type != CPP_NAME 
13226           && token->type != CPP_KEYWORD)
13227         return error_mark_node;
13228       /* Consume the token.  */
13229       token = cp_lexer_consume_token (parser->lexer);
13230       
13231       /* Save away the identifier that indicates which attribute this is.  */
13232       identifier = token->value;
13233       attribute = build_tree_list (identifier, NULL_TREE);
13234
13235       /* Peek at the next token.  */
13236       token = cp_lexer_peek_token (parser->lexer);
13237       /* If it's an `(', then parse the attribute arguments.  */
13238       if (token->type == CPP_OPEN_PAREN)
13239         {
13240           tree arguments;
13241           int arguments_allowed_p = 1;
13242
13243           /* Consume the `('.  */
13244           cp_lexer_consume_token (parser->lexer);
13245           /* Peek at the next token.  */
13246           token = cp_lexer_peek_token (parser->lexer);
13247           /* Check to see if the next token is an identifier.  */
13248           if (token->type == CPP_NAME)
13249             {
13250               /* Save the identifier.  */
13251               identifier = token->value;
13252               /* Consume the identifier.  */
13253               cp_lexer_consume_token (parser->lexer);
13254               /* Peek at the next token.  */
13255               token = cp_lexer_peek_token (parser->lexer);
13256               /* If the next token is a `,', then there are some other
13257                  expressions as well.  */
13258               if (token->type == CPP_COMMA)
13259                 /* Consume the comma.  */
13260                 cp_lexer_consume_token (parser->lexer);
13261               else
13262                 arguments_allowed_p = 0;
13263             }
13264           else
13265             identifier = NULL_TREE;
13266
13267           /* If there are arguments, parse them too.  */
13268           if (arguments_allowed_p)
13269             arguments = cp_parser_expression_list (parser);
13270           else
13271             arguments = NULL_TREE;
13272
13273           /* Combine the identifier and the arguments.  */
13274           if (identifier)
13275             arguments = tree_cons (NULL_TREE, identifier, arguments);
13276
13277           /* Save the identifier and arguments away.  */
13278           TREE_VALUE (attribute) = arguments;
13279
13280           /* Look for the closing `)'.  */
13281           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13282         }
13283
13284       /* Add this attribute to the list.  */
13285       TREE_CHAIN (attribute) = attribute_list;
13286       attribute_list = attribute;
13287
13288       /* Now, look for more attributes.  */
13289       token = cp_lexer_peek_token (parser->lexer);
13290       /* If the next token isn't a `,', we're done.  */
13291       if (token->type != CPP_COMMA)
13292         break;
13293
13294       /* Consume the commma and keep going.  */
13295       cp_lexer_consume_token (parser->lexer);
13296     }
13297
13298   /* We built up the list in reverse order.  */
13299   return nreverse (attribute_list);
13300 }
13301
13302 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13303    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13304    current value of the PEDANTIC flag, regardless of whether or not
13305    the `__extension__' keyword is present.  The caller is responsible
13306    for restoring the value of the PEDANTIC flag.  */
13307
13308 static bool
13309 cp_parser_extension_opt (parser, saved_pedantic)
13310      cp_parser *parser;
13311      int *saved_pedantic;
13312 {
13313   /* Save the old value of the PEDANTIC flag.  */
13314   *saved_pedantic = pedantic;
13315
13316   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13317     {
13318       /* Consume the `__extension__' token.  */
13319       cp_lexer_consume_token (parser->lexer);
13320       /* We're not being pedantic while the `__extension__' keyword is
13321          in effect.  */
13322       pedantic = 0;
13323
13324       return true;
13325     }
13326
13327   return false;
13328 }
13329
13330 /* Parse a label declaration.
13331
13332    label-declaration:
13333      __label__ label-declarator-seq ;
13334
13335    label-declarator-seq:
13336      identifier , label-declarator-seq
13337      identifier  */
13338
13339 static void
13340 cp_parser_label_declaration (parser)
13341      cp_parser *parser;
13342 {
13343   /* Look for the `__label__' keyword.  */
13344   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13345
13346   while (true)
13347     {
13348       tree identifier;
13349
13350       /* Look for an identifier.  */
13351       identifier = cp_parser_identifier (parser);
13352       /* Declare it as a lobel.  */
13353       finish_label_decl (identifier);
13354       /* If the next token is a `;', stop.  */
13355       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13356         break;
13357       /* Look for the `,' separating the label declarations.  */
13358       cp_parser_require (parser, CPP_COMMA, "`,'");
13359     }
13360
13361   /* Look for the final `;'.  */
13362   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13363 }
13364
13365 /* Support Functions */
13366
13367 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13368    NAME should have one of the representations used for an
13369    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13370    is returned.  If PARSER->SCOPE is a dependent type, then a
13371    SCOPE_REF is returned.
13372
13373    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13374    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13375    was formed.  Abstractly, such entities should not be passed to this
13376    function, because they do not need to be looked up, but it is
13377    simpler to check for this special case here, rather than at the
13378    call-sites.
13379
13380    In cases not explicitly covered above, this function returns a
13381    DECL, OVERLOAD, or baselink representing the result of the lookup.
13382    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13383    is returned.
13384
13385    If CHECK_ACCESS is TRUE, then access control is performed on the
13386    declaration to which the name resolves, and an error message is
13387    issued if the declaration is inaccessible.
13388
13389    If IS_TYPE is TRUE, bindings that do not refer to types are
13390    ignored.
13391
13392    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13393    are ignored.
13394
13395    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13396    types.  */
13397
13398 static tree
13399 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access, 
13400                        bool is_type, bool is_namespace, bool check_dependency)
13401 {
13402   tree decl;
13403   tree object_type = parser->context->object_type;
13404
13405   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13406      no longer valid.  Note that if we are parsing tentatively, and
13407      the parse fails, OBJECT_TYPE will be automatically restored.  */
13408   parser->context->object_type = NULL_TREE;
13409
13410   if (name == error_mark_node)
13411     return error_mark_node;
13412
13413   /* A template-id has already been resolved; there is no lookup to
13414      do.  */
13415   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13416     return name;
13417   if (BASELINK_P (name))
13418     {
13419       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13420                            == TEMPLATE_ID_EXPR),
13421                           20020909);
13422       return name;
13423     }
13424
13425   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13426      it should already have been checked to make sure that the name
13427      used matches the type being destroyed.  */
13428   if (TREE_CODE (name) == BIT_NOT_EXPR)
13429     {
13430       tree type;
13431
13432       /* Figure out to which type this destructor applies.  */
13433       if (parser->scope)
13434         type = parser->scope;
13435       else if (object_type)
13436         type = object_type;
13437       else
13438         type = current_class_type;
13439       /* If that's not a class type, there is no destructor.  */
13440       if (!type || !CLASS_TYPE_P (type))
13441         return error_mark_node;
13442       /* If it was a class type, return the destructor.  */
13443       return CLASSTYPE_DESTRUCTORS (type);
13444     }
13445
13446   /* By this point, the NAME should be an ordinary identifier.  If
13447      the id-expression was a qualified name, the qualifying scope is
13448      stored in PARSER->SCOPE at this point.  */
13449   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13450                       20000619);
13451   
13452   /* Perform the lookup.  */
13453   if (parser->scope)
13454     { 
13455       bool dependent_type_p;
13456
13457       if (parser->scope == error_mark_node)
13458         return error_mark_node;
13459
13460       /* If the SCOPE is dependent, the lookup must be deferred until
13461          the template is instantiated -- unless we are explicitly
13462          looking up names in uninstantiated templates.  Even then, we
13463          cannot look up the name if the scope is not a class type; it
13464          might, for example, be a template type parameter.  */
13465       dependent_type_p = (TYPE_P (parser->scope)
13466                           && !(parser->in_declarator_p
13467                                && currently_open_class (parser->scope))
13468                           && cp_parser_dependent_type_p (parser->scope));
13469       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13470            && dependent_type_p)
13471         {
13472           if (!is_type)
13473             decl = build_nt (SCOPE_REF, parser->scope, name);
13474           else
13475             /* The resolution to Core Issue 180 says that `struct A::B'
13476                should be considered a type-name, even if `A' is
13477                dependent.  */
13478             decl = TYPE_NAME (make_typename_type (parser->scope,
13479                                                   name,
13480                                                   /*complain=*/1));
13481         }
13482       else
13483         {
13484           /* If PARSER->SCOPE is a dependent type, then it must be a
13485              class type, and we must not be checking dependencies;
13486              otherwise, we would have processed this lookup above.  So
13487              that PARSER->SCOPE is not considered a dependent base by
13488              lookup_member, we must enter the scope here.  */
13489           if (dependent_type_p)
13490             push_scope (parser->scope);
13491           /* If the PARSER->SCOPE is a a template specialization, it
13492              may be instantiated during name lookup.  In that case,
13493              errors may be issued.  Even if we rollback the current
13494              tentative parse, those errors are valid.  */
13495           decl = lookup_qualified_name (parser->scope, name, is_type,
13496                                         /*flags=*/0);
13497           if (dependent_type_p)
13498             pop_scope (parser->scope);
13499         }
13500       parser->qualifying_scope = parser->scope;
13501       parser->object_scope = NULL_TREE;
13502     }
13503   else if (object_type)
13504     {
13505       tree object_decl = NULL_TREE;
13506       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13507          OBJECT_TYPE is not a class.  */
13508       if (CLASS_TYPE_P (object_type))
13509         /* If the OBJECT_TYPE is a template specialization, it may
13510            be instantiated during name lookup.  In that case, errors
13511            may be issued.  Even if we rollback the current tentative
13512            parse, those errors are valid.  */
13513         object_decl = lookup_member (object_type,
13514                                      name,
13515                                      /*protect=*/0, is_type);
13516       /* Look it up in the enclosing context, too.  */
13517       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13518                                is_namespace,
13519                                /*flags=*/0);
13520       parser->object_scope = object_type;
13521       parser->qualifying_scope = NULL_TREE;
13522       if (object_decl)
13523         decl = object_decl;
13524     }
13525   else
13526     {
13527       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13528                                is_namespace,
13529                                /*flags=*/0);
13530       parser->qualifying_scope = NULL_TREE;
13531       parser->object_scope = NULL_TREE;
13532     }
13533
13534   /* If the lookup failed, let our caller know.  */
13535   if (!decl 
13536       || decl == error_mark_node
13537       || (TREE_CODE (decl) == FUNCTION_DECL 
13538           && DECL_ANTICIPATED (decl)))
13539     return error_mark_node;
13540
13541   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13542   if (TREE_CODE (decl) == TREE_LIST)
13543     {
13544       /* The error message we have to print is too complicated for
13545          cp_parser_error, so we incorporate its actions directly.  */
13546       if (!cp_parser_simulate_error (parser))
13547         {
13548           error ("reference to `%D' is ambiguous", name);
13549           print_candidates (decl);
13550         }
13551       return error_mark_node;
13552     }
13553
13554   my_friendly_assert (DECL_P (decl) 
13555                       || TREE_CODE (decl) == OVERLOAD
13556                       || TREE_CODE (decl) == SCOPE_REF
13557                       || BASELINK_P (decl),
13558                       20000619);
13559
13560   /* If we have resolved the name of a member declaration, check to
13561      see if the declaration is accessible.  When the name resolves to
13562      set of overloaded functions, accesibility is checked when
13563      overload resolution is done.  
13564
13565      During an explicit instantiation, access is not checked at all,
13566      as per [temp.explicit].  */
13567   if (check_access && scope_chain->check_access && DECL_P (decl))
13568     {
13569       tree qualifying_type;
13570       
13571       /* Figure out the type through which DECL is being
13572          accessed.  */
13573       qualifying_type 
13574         = cp_parser_scope_through_which_access_occurs (decl,
13575                                                        object_type,
13576                                                        parser->scope);
13577       if (qualifying_type)
13578         {
13579           /* If we are supposed to defer access checks, just record
13580              the information for later.  */
13581           if (parser->context->deferring_access_checks_p)
13582             cp_parser_defer_access_check (parser, qualifying_type, decl);
13583           /* Otherwise, check accessibility now.  */
13584           else
13585             enforce_access (qualifying_type, decl);
13586         }
13587     }
13588
13589   return decl;
13590 }
13591
13592 /* Like cp_parser_lookup_name, but for use in the typical case where
13593    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13594    TRUE.  */
13595
13596 static tree
13597 cp_parser_lookup_name_simple (parser, name)
13598      cp_parser *parser;
13599      tree name;
13600 {
13601   return cp_parser_lookup_name (parser, name, 
13602                                 /*check_access=*/true,
13603                                 /*is_type=*/false,
13604                                 /*is_namespace=*/false,
13605                                 /*check_dependency=*/true);
13606 }
13607
13608 /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
13609    TYPENAME_TYPE corresponds.  Note that this function peers inside
13610    uninstantiated templates and therefore should be used only in
13611    extremely limited situations.  */
13612
13613 static tree
13614 cp_parser_resolve_typename_type (parser, type)
13615      cp_parser *parser;
13616      tree type;
13617 {
13618   tree scope;
13619   tree name;
13620   tree decl;
13621
13622   my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13623                       20010702);
13624
13625   scope = TYPE_CONTEXT (type);
13626   name = DECL_NAME (TYPE_NAME (type));
13627
13628   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13629      it first before we can figure out what NAME refers to.  */
13630   if (TREE_CODE (scope) == TYPENAME_TYPE)
13631     scope = cp_parser_resolve_typename_type (parser, scope);
13632   /* If we don't know what SCOPE refers to, then we cannot resolve the
13633      TYPENAME_TYPE.  */
13634   if (scope == error_mark_node)
13635     return error_mark_node;
13636   /* If the SCOPE is a template type parameter, we have no way of
13637      resolving the name.  */
13638   if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13639     return type;
13640   /* Enter the SCOPE so that name lookup will be resolved as if we
13641      were in the class definition.  In particular, SCOPE will no
13642      longer be considered a dependent type.  */
13643   push_scope (scope);
13644   /* Look up the declaration.  */
13645   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13646   /* If all went well, we got a TYPE_DECL for a non-typename.  */
13647   if (!decl 
13648       || TREE_CODE (decl) != TYPE_DECL 
13649       || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13650     {
13651       cp_parser_error (parser, "could not resolve typename type");
13652       type = error_mark_node;
13653     }
13654   else
13655     type = TREE_TYPE (decl);
13656   /* Leave the SCOPE.  */
13657   pop_scope (scope);
13658
13659   return type;
13660 }
13661
13662 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13663    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13664    true, the DECL indicates the class being defined in a class-head,
13665    or declared in an elaborated-type-specifier.
13666
13667    Otherwise, return DECL.  */
13668
13669 static tree
13670 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13671 {
13672   /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13673      the scope of the class, then treat the TEMPLATE_DECL as a
13674      class-name.  For example, in:
13675
13676        template <class T> struct S {
13677          S s;
13678        };
13679
13680      is OK.  
13681
13682      If the TEMPLATE_DECL is being declared as part of a class-head,
13683      the same translation occurs:
13684
13685        struct A { 
13686          template <typename T> struct B;
13687        };
13688
13689        template <typename T> struct A::B {}; 
13690    
13691      Similarly, in a elaborated-type-specifier:
13692
13693        namespace N { struct X{}; }
13694
13695        struct A {
13696          template <typename T> friend struct N::X;
13697        };
13698
13699      */
13700   if (DECL_CLASS_TEMPLATE_P (decl)
13701       && (tag_name_p
13702           || (current_class_type
13703               && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13704                               current_class_type))))
13705     return DECL_TEMPLATE_RESULT (decl);
13706
13707   return decl;
13708 }
13709
13710 /* If too many, or too few, template-parameter lists apply to the
13711    declarator, issue an error message.  Returns TRUE if all went well,
13712    and FALSE otherwise.  */
13713
13714 static bool
13715 cp_parser_check_declarator_template_parameters (parser, declarator)
13716      cp_parser *parser;
13717      tree declarator;
13718 {
13719   unsigned num_templates;
13720
13721   /* We haven't seen any classes that involve template parameters yet.  */
13722   num_templates = 0;
13723
13724   switch (TREE_CODE (declarator))
13725     {
13726     case CALL_EXPR:
13727     case ARRAY_REF:
13728     case INDIRECT_REF:
13729     case ADDR_EXPR:
13730       {
13731         tree main_declarator = TREE_OPERAND (declarator, 0);
13732         return
13733           cp_parser_check_declarator_template_parameters (parser, 
13734                                                           main_declarator);
13735       }
13736
13737     case SCOPE_REF:
13738       {
13739         tree scope;
13740         tree member;
13741
13742         scope = TREE_OPERAND (declarator, 0);
13743         member = TREE_OPERAND (declarator, 1);
13744
13745         /* If this is a pointer-to-member, then we are not interested
13746            in the SCOPE, because it does not qualify the thing that is
13747            being declared.  */
13748         if (TREE_CODE (member) == INDIRECT_REF)
13749           return (cp_parser_check_declarator_template_parameters
13750                   (parser, member));
13751
13752         while (scope && CLASS_TYPE_P (scope))
13753           {
13754             /* You're supposed to have one `template <...>'
13755                for every template class, but you don't need one
13756                for a full specialization.  For example:
13757                
13758                template <class T> struct S{};
13759                template <> struct S<int> { void f(); };
13760                void S<int>::f () {}
13761                
13762                is correct; there shouldn't be a `template <>' for
13763                the definition of `S<int>::f'.  */
13764             if (CLASSTYPE_TEMPLATE_INFO (scope)
13765                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13766                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13767                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13768               ++num_templates;
13769
13770             scope = TYPE_CONTEXT (scope);
13771           }
13772       }
13773
13774       /* Fall through.  */
13775
13776     default:
13777       /* If the DECLARATOR has the form `X<y>' then it uses one
13778          additional level of template parameters.  */
13779       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13780         ++num_templates;
13781
13782       return cp_parser_check_template_parameters (parser, 
13783                                                   num_templates);
13784     }
13785 }
13786
13787 /* NUM_TEMPLATES were used in the current declaration.  If that is
13788    invalid, return FALSE and issue an error messages.  Otherwise,
13789    return TRUE.  */
13790
13791 static bool
13792 cp_parser_check_template_parameters (parser, num_templates)
13793      cp_parser *parser;
13794      unsigned num_templates;
13795 {
13796   /* If there are more template classes than parameter lists, we have
13797      something like:
13798      
13799        template <class T> void S<T>::R<T>::f ();  */
13800   if (parser->num_template_parameter_lists < num_templates)
13801     {
13802       error ("too few template-parameter-lists");
13803       return false;
13804     }
13805   /* If there are the same number of template classes and parameter
13806      lists, that's OK.  */
13807   if (parser->num_template_parameter_lists == num_templates)
13808     return true;
13809   /* If there are more, but only one more, then we are referring to a
13810      member template.  That's OK too.  */
13811   if (parser->num_template_parameter_lists == num_templates + 1)
13812       return true;
13813   /* Otherwise, there are too many template parameter lists.  We have
13814      something like:
13815
13816      template <class T> template <class U> void S::f();  */
13817   error ("too many template-parameter-lists");
13818   return false;
13819 }
13820
13821 /* Parse a binary-expression of the general form:
13822
13823    binary-expression:
13824      <expr>
13825      binary-expression <token> <expr>
13826
13827    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13828    to parser the <expr>s.  If the first production is used, then the
13829    value returned by FN is returned directly.  Otherwise, a node with
13830    the indicated EXPR_TYPE is returned, with operands corresponding to
13831    the two sub-expressions.  */
13832
13833 static tree
13834 cp_parser_binary_expression (parser, token_tree_map, fn)
13835      cp_parser *parser;
13836      cp_parser_token_tree_map token_tree_map;
13837      cp_parser_expression_fn fn;
13838 {
13839   tree lhs;
13840
13841   /* Parse the first expression.  */
13842   lhs = (*fn) (parser);
13843   /* Now, look for more expressions.  */
13844   while (true)
13845     {
13846       cp_token *token;
13847       cp_parser_token_tree_map_node *map_node;
13848       tree rhs;
13849
13850       /* Peek at the next token.  */
13851       token = cp_lexer_peek_token (parser->lexer);
13852       /* If the token is `>', and that's not an operator at the
13853          moment, then we're done.  */
13854       if (token->type == CPP_GREATER
13855           && !parser->greater_than_is_operator_p)
13856         break;
13857       /* If we find one of the tokens we want, build the correspoding
13858          tree representation.  */
13859       for (map_node = token_tree_map; 
13860            map_node->token_type != CPP_EOF;
13861            ++map_node)
13862         if (map_node->token_type == token->type)
13863           {
13864             /* Consume the operator token.  */
13865             cp_lexer_consume_token (parser->lexer);
13866             /* Parse the right-hand side of the expression.  */
13867             rhs = (*fn) (parser);
13868             /* Build the binary tree node.  */
13869             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13870             break;
13871           }
13872
13873       /* If the token wasn't one of the ones we want, we're done.  */
13874       if (map_node->token_type == CPP_EOF)
13875         break;
13876     }
13877
13878   return lhs;
13879 }
13880
13881 /* Parse an optional `::' token indicating that the following name is
13882    from the global namespace.  If so, PARSER->SCOPE is set to the
13883    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13884    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13885    Returns the new value of PARSER->SCOPE, if the `::' token is
13886    present, and NULL_TREE otherwise.  */
13887
13888 static tree
13889 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13890      cp_parser *parser;
13891      bool current_scope_valid_p;
13892 {
13893   cp_token *token;
13894
13895   /* Peek at the next token.  */
13896   token = cp_lexer_peek_token (parser->lexer);
13897   /* If we're looking at a `::' token then we're starting from the
13898      global namespace, not our current location.  */
13899   if (token->type == CPP_SCOPE)
13900     {
13901       /* Consume the `::' token.  */
13902       cp_lexer_consume_token (parser->lexer);
13903       /* Set the SCOPE so that we know where to start the lookup.  */
13904       parser->scope = global_namespace;
13905       parser->qualifying_scope = global_namespace;
13906       parser->object_scope = NULL_TREE;
13907
13908       return parser->scope;
13909     }
13910   else if (!current_scope_valid_p)
13911     {
13912       parser->scope = NULL_TREE;
13913       parser->qualifying_scope = NULL_TREE;
13914       parser->object_scope = NULL_TREE;
13915     }
13916
13917   return NULL_TREE;
13918 }
13919
13920 /* Returns TRUE if the upcoming token sequence is the start of a
13921    constructor declarator.  If FRIEND_P is true, the declarator is
13922    preceded by the `friend' specifier.  */
13923
13924 static bool
13925 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13926 {
13927   bool constructor_p;
13928   tree type_decl = NULL_TREE;
13929   bool nested_name_p;
13930
13931   /* Parse tentatively; we are going to roll back all of the tokens
13932      consumed here.  */
13933   cp_parser_parse_tentatively (parser);
13934   /* Assume that we are looking at a constructor declarator.  */
13935   constructor_p = true;
13936   /* Look for the optional `::' operator.  */
13937   cp_parser_global_scope_opt (parser,
13938                               /*current_scope_valid_p=*/false);
13939   /* Look for the nested-name-specifier.  */
13940   nested_name_p 
13941     = (cp_parser_nested_name_specifier_opt (parser,
13942                                             /*typename_keyword_p=*/false,
13943                                             /*check_dependency_p=*/false,
13944                                             /*type_p=*/false)
13945        != NULL_TREE);
13946   /* Outside of a class-specifier, there must be a
13947      nested-name-specifier.  */
13948   if (!nested_name_p && 
13949       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13950        || friend_p))
13951     constructor_p = false;
13952   /* If we still think that this might be a constructor-declarator,
13953      look for a class-name.  */
13954   if (constructor_p)
13955     {
13956       /* If we have:
13957
13958            template <typename T> struct S { S(); }
13959            template <typename T> S<T>::S ();
13960
13961          we must recognize that the nested `S' names a class.
13962          Similarly, for:
13963
13964            template <typename T> S<T>::S<T> ();
13965
13966          we must recognize that the nested `S' names a template.  */
13967       type_decl = cp_parser_class_name (parser,
13968                                         /*typename_keyword_p=*/false,
13969                                         /*template_keyword_p=*/false,
13970                                         /*type_p=*/false,
13971                                         /*check_access_p=*/false,
13972                                         /*check_dependency_p=*/false,
13973                                         /*class_head_p=*/false);
13974       /* If there was no class-name, then this is not a constructor.  */
13975       constructor_p = !cp_parser_error_occurred (parser);
13976     }
13977   /* If we're still considering a constructor, we have to see a `(',
13978      to begin the parameter-declaration-clause, followed by either a
13979      `)', an `...', or a decl-specifier.  We need to check for a
13980      type-specifier to avoid being fooled into thinking that:
13981
13982        S::S (f) (int);
13983
13984      is a constructor.  (It is actually a function named `f' that
13985      takes one parameter (of type `int') and returns a value of type
13986      `S::S'.  */
13987   if (constructor_p 
13988       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13989     {
13990       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13991           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13992           && !cp_parser_storage_class_specifier_opt (parser))
13993         {
13994           if (current_class_type 
13995               && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13996             /* The constructor for one class cannot be declared inside
13997                another.  */
13998             constructor_p = false;
13999           else
14000             {
14001               tree type;
14002
14003               /* Names appearing in the type-specifier should be looked up
14004                  in the scope of the class.  */
14005               if (current_class_type)
14006                 type = NULL_TREE;
14007               else
14008                 {
14009                   type = TREE_TYPE (type_decl);
14010                   if (TREE_CODE (type) == TYPENAME_TYPE)
14011                     type = cp_parser_resolve_typename_type (parser, type);
14012                   push_scope (type);
14013                 }
14014               /* Look for the type-specifier.  */
14015               cp_parser_type_specifier (parser,
14016                                         CP_PARSER_FLAGS_NONE,
14017                                         /*is_friend=*/false,
14018                                         /*is_declarator=*/true,
14019                                         /*declares_class_or_enum=*/NULL,
14020                                         /*is_cv_qualifier=*/NULL);
14021               /* Leave the scope of the class.  */
14022               if (type)
14023                 pop_scope (type);
14024
14025               constructor_p = !cp_parser_error_occurred (parser);
14026             }
14027         }
14028     }
14029   else
14030     constructor_p = false;
14031   /* We did not really want to consume any tokens.  */
14032   cp_parser_abort_tentative_parse (parser);
14033
14034   return constructor_p;
14035 }
14036
14037 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14038    ATTRIBUTES, and DECLARATOR.  The ACCESS_CHECKS have been deferred;
14039    they must be performed once we are in the scope of the function.
14040
14041    Returns the function defined.  */
14042
14043 static tree
14044 cp_parser_function_definition_from_specifiers_and_declarator
14045   (parser, decl_specifiers, attributes, declarator, access_checks)
14046      cp_parser *parser;
14047      tree decl_specifiers;
14048      tree attributes;
14049      tree declarator;
14050      tree access_checks;
14051 {
14052   tree fn;
14053   bool success_p;
14054
14055   /* Begin the function-definition.  */
14056   success_p = begin_function_definition (decl_specifiers, 
14057                                          attributes, 
14058                                          declarator);
14059
14060   /* If there were names looked up in the decl-specifier-seq that we
14061      did not check, check them now.  We must wait until we are in the
14062      scope of the function to perform the checks, since the function
14063      might be a friend.  */
14064   cp_parser_perform_deferred_access_checks (access_checks);
14065
14066   if (!success_p)
14067     {
14068       /* If begin_function_definition didn't like the definition, skip
14069          the entire function.  */
14070       error ("invalid function declaration");
14071       cp_parser_skip_to_end_of_block_or_statement (parser);
14072       fn = error_mark_node;
14073     }
14074   else
14075     fn = cp_parser_function_definition_after_declarator (parser,
14076                                                          /*inline_p=*/false);
14077
14078   return fn;
14079 }
14080
14081 /* Parse the part of a function-definition that follows the
14082    declarator.  INLINE_P is TRUE iff this function is an inline
14083    function defined with a class-specifier.
14084
14085    Returns the function defined.  */
14086
14087 static tree 
14088 cp_parser_function_definition_after_declarator (parser, 
14089                                                 inline_p)
14090      cp_parser *parser;
14091      bool inline_p;
14092 {
14093   tree fn;
14094   bool ctor_initializer_p = false;
14095   bool saved_in_unbraced_linkage_specification_p;
14096   unsigned saved_num_template_parameter_lists;
14097
14098   /* If the next token is `return', then the code may be trying to
14099      make use of the "named return value" extension that G++ used to
14100      support.  */
14101   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14102     {
14103       /* Consume the `return' keyword.  */
14104       cp_lexer_consume_token (parser->lexer);
14105       /* Look for the identifier that indicates what value is to be
14106          returned.  */
14107       cp_parser_identifier (parser);
14108       /* Issue an error message.  */
14109       error ("named return values are no longer supported");
14110       /* Skip tokens until we reach the start of the function body.  */
14111       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14112         cp_lexer_consume_token (parser->lexer);
14113     }
14114   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14115      anything declared inside `f'.  */
14116   saved_in_unbraced_linkage_specification_p 
14117     = parser->in_unbraced_linkage_specification_p;
14118   parser->in_unbraced_linkage_specification_p = false;
14119   /* Inside the function, surrounding template-parameter-lists do not
14120      apply.  */
14121   saved_num_template_parameter_lists 
14122     = parser->num_template_parameter_lists; 
14123   parser->num_template_parameter_lists = 0;
14124   /* If the next token is `try', then we are looking at a
14125      function-try-block.  */
14126   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14127     ctor_initializer_p = cp_parser_function_try_block (parser);
14128   /* A function-try-block includes the function-body, so we only do
14129      this next part if we're not processing a function-try-block.  */
14130   else
14131     ctor_initializer_p 
14132       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14133
14134   /* Finish the function.  */
14135   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14136                         (inline_p ? 2 : 0));
14137   /* Generate code for it, if necessary.  */
14138   expand_body (fn);
14139   /* Restore the saved values.  */
14140   parser->in_unbraced_linkage_specification_p 
14141     = saved_in_unbraced_linkage_specification_p;
14142   parser->num_template_parameter_lists 
14143     = saved_num_template_parameter_lists;
14144
14145   return fn;
14146 }
14147
14148 /* Parse a template-declaration, assuming that the `export' (and
14149    `extern') keywords, if present, has already been scanned.  MEMBER_P
14150    is as for cp_parser_template_declaration.  */
14151
14152 static void
14153 cp_parser_template_declaration_after_export (parser, member_p)
14154      cp_parser *parser;
14155      bool member_p;
14156 {
14157   tree decl = NULL_TREE;
14158   tree parameter_list;
14159   bool friend_p = false;
14160
14161   /* Look for the `template' keyword.  */
14162   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14163     return;
14164       
14165   /* And the `<'.  */
14166   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14167     return;
14168       
14169   /* Parse the template parameters.  */
14170   begin_template_parm_list ();
14171   /* If the next token is `>', then we have an invalid
14172      specialization.  Rather than complain about an invalid template
14173      parameter, issue an error message here.  */
14174   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14175     {
14176       cp_parser_error (parser, "invalid explicit specialization");
14177       parameter_list = NULL_TREE;
14178     }
14179   else
14180     parameter_list = cp_parser_template_parameter_list (parser);
14181   parameter_list = end_template_parm_list (parameter_list);
14182   /* Look for the `>'.  */
14183   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14184   /* We just processed one more parameter list.  */
14185   ++parser->num_template_parameter_lists;
14186   /* If the next token is `template', there are more template
14187      parameters.  */
14188   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14189                                       RID_TEMPLATE))
14190     cp_parser_template_declaration_after_export (parser, member_p);
14191   else
14192     {
14193       decl = cp_parser_single_declaration (parser,
14194                                            member_p,
14195                                            &friend_p);
14196
14197       /* If this is a member template declaration, let the front
14198          end know.  */
14199       if (member_p && !friend_p && decl)
14200         decl = finish_member_template_decl (decl);
14201       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14202         make_friend_class (current_class_type, TREE_TYPE (decl));
14203     }
14204   /* We are done with the current parameter list.  */
14205   --parser->num_template_parameter_lists;
14206
14207   /* Finish up.  */
14208   finish_template_decl (parameter_list);
14209
14210   /* Register member declarations.  */
14211   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14212     finish_member_declaration (decl);
14213
14214   /* If DECL is a function template, we must return to parse it later.
14215      (Even though there is no definition, there might be default
14216      arguments that need handling.)  */
14217   if (member_p && decl 
14218       && (TREE_CODE (decl) == FUNCTION_DECL
14219           || DECL_FUNCTION_TEMPLATE_P (decl)))
14220     TREE_VALUE (parser->unparsed_functions_queues)
14221       = tree_cons (current_class_type, decl, 
14222                    TREE_VALUE (parser->unparsed_functions_queues));
14223 }
14224
14225 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14226    `function-definition' sequence.  MEMBER_P is true, this declaration
14227    appears in a class scope.
14228
14229    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14230    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14231
14232 static tree
14233 cp_parser_single_declaration (parser, 
14234                               member_p,
14235                               friend_p)
14236      cp_parser *parser;
14237      bool member_p;
14238      bool *friend_p;
14239 {
14240   bool declares_class_or_enum;
14241   tree decl = NULL_TREE;
14242   tree decl_specifiers;
14243   tree attributes;
14244   tree access_checks;
14245
14246   /* Parse the dependent declaration.  We don't know yet
14247      whether it will be a function-definition.  */
14248   cp_parser_parse_tentatively (parser);
14249   /* Defer access checks until we know what is being declared.  */
14250   cp_parser_start_deferring_access_checks (parser);
14251   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14252      alternative.  */
14253   decl_specifiers 
14254     = cp_parser_decl_specifier_seq (parser,
14255                                     CP_PARSER_FLAGS_OPTIONAL,
14256                                     &attributes,
14257                                     &declares_class_or_enum);
14258   /* Gather up the access checks that occurred the
14259      decl-specifier-seq.  */
14260   access_checks = cp_parser_stop_deferring_access_checks (parser);
14261   /* Check for the declaration of a template class.  */
14262   if (declares_class_or_enum)
14263     {
14264       if (cp_parser_declares_only_class_p (parser))
14265         {
14266           decl = shadow_tag (decl_specifiers);
14267           if (decl)
14268             decl = TYPE_NAME (decl);
14269           else
14270             decl = error_mark_node;
14271         }
14272     }
14273   else
14274     decl = NULL_TREE;
14275   /* If it's not a template class, try for a template function.  If
14276      the next token is a `;', then this declaration does not declare
14277      anything.  But, if there were errors in the decl-specifiers, then
14278      the error might well have come from an attempted class-specifier.
14279      In that case, there's no need to warn about a missing declarator.  */
14280   if (!decl
14281       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14282           || !value_member (error_mark_node, decl_specifiers)))
14283     decl = cp_parser_init_declarator (parser, 
14284                                       decl_specifiers,
14285                                       attributes,
14286                                       access_checks,
14287                                       /*function_definition_allowed_p=*/false,
14288                                       member_p,
14289                                       /*function_definition_p=*/NULL);
14290   /* Clear any current qualification; whatever comes next is the start
14291      of something new.  */
14292   parser->scope = NULL_TREE;
14293   parser->qualifying_scope = NULL_TREE;
14294   parser->object_scope = NULL_TREE;
14295   /* Look for a trailing `;' after the declaration.  */
14296   if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14297       && cp_parser_committed_to_tentative_parse (parser))
14298     cp_parser_skip_to_end_of_block_or_statement (parser);
14299   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
14300   if (cp_parser_parse_definitely (parser))
14301     {
14302       if (friend_p)
14303         *friend_p = cp_parser_friend_p (decl_specifiers);
14304     }
14305   /* Otherwise, try a function-definition.  */
14306   else
14307     decl = cp_parser_function_definition (parser, friend_p);
14308
14309   return decl;
14310 }
14311
14312 /* Parse a functional cast to TYPE.  Returns an expression
14313    representing the cast.  */
14314
14315 static tree
14316 cp_parser_functional_cast (parser, type)
14317      cp_parser *parser;
14318      tree type;
14319 {
14320   tree expression_list;
14321
14322   /* Look for the opening `('.  */
14323   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14324     return error_mark_node;
14325   /* If the next token is not an `)', there are arguments to the
14326      cast.  */
14327   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14328     expression_list = cp_parser_expression_list (parser);
14329   else
14330     expression_list = NULL_TREE;
14331   /* Look for the closing `)'.  */
14332   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14333
14334   return build_functional_cast (type, expression_list);
14335 }
14336
14337 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14338    arguments, or the body of the function have not yet been parsed,
14339    parse them now.  */
14340
14341 static void
14342 cp_parser_late_parsing_for_member (parser, member_function)
14343      cp_parser *parser;
14344      tree member_function;
14345 {
14346   cp_lexer *saved_lexer;
14347
14348   /* If this member is a template, get the underlying
14349      FUNCTION_DECL.  */
14350   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14351     member_function = DECL_TEMPLATE_RESULT (member_function);
14352
14353   /* There should not be any class definitions in progress at this
14354      point; the bodies of members are only parsed outside of all class
14355      definitions.  */
14356   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14357   /* While we're parsing the member functions we might encounter more
14358      classes.  We want to handle them right away, but we don't want
14359      them getting mixed up with functions that are currently in the
14360      queue.  */
14361   parser->unparsed_functions_queues
14362     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14363
14364   /* Make sure that any template parameters are in scope.  */
14365   maybe_begin_member_template_processing (member_function);
14366
14367   /* If there are default arguments that have not yet been processed,
14368      take care of them now.  */
14369   cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14370                                        DECL_FUNCTION_MEMBER_P (member_function)
14371                                        ? DECL_CONTEXT (member_function)
14372                                        : NULL_TREE);
14373
14374   /* If the body of the function has not yet been parsed, parse it
14375      now.  */
14376   if (DECL_PENDING_INLINE_P (member_function))
14377     {
14378       tree function_scope;
14379       cp_token_cache *tokens;
14380
14381       /* The function is no longer pending; we are processing it.  */
14382       tokens = DECL_PENDING_INLINE_INFO (member_function);
14383       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14384       DECL_PENDING_INLINE_P (member_function) = 0;
14385       /* If this was an inline function in a local class, enter the scope
14386          of the containing function.  */
14387       function_scope = decl_function_context (member_function);
14388       if (function_scope)
14389         push_function_context_to (function_scope);
14390       
14391       /* Save away the current lexer.  */
14392       saved_lexer = parser->lexer;
14393       /* Make a new lexer to feed us the tokens saved for this function.  */
14394       parser->lexer = cp_lexer_new_from_tokens (tokens);
14395       parser->lexer->next = saved_lexer;
14396       
14397       /* Set the current source position to be the location of the first
14398          token in the saved inline body.  */
14399       cp_lexer_set_source_position_from_token 
14400         (parser->lexer,
14401          cp_lexer_peek_token (parser->lexer));
14402       
14403       /* Let the front end know that we going to be defining this
14404          function.  */
14405       start_function (NULL_TREE, member_function, NULL_TREE,
14406                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14407       
14408       /* Now, parse the body of the function.  */
14409       cp_parser_function_definition_after_declarator (parser,
14410                                                       /*inline_p=*/true);
14411       
14412       /* Leave the scope of the containing function.  */
14413       if (function_scope)
14414         pop_function_context_from (function_scope);
14415       /* Restore the lexer.  */
14416       parser->lexer = saved_lexer;
14417     }
14418
14419   /* Remove any template parameters from the symbol table.  */
14420   maybe_end_member_template_processing ();
14421
14422   /* Restore the queue.  */
14423   parser->unparsed_functions_queues 
14424     = TREE_CHAIN (parser->unparsed_functions_queues);
14425 }
14426
14427 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14428    with an unparsed DEFAULT_ARG.  If non-NULL, SCOPE is the class in
14429    whose context name lookups in the default argument should occur.
14430    Parse the default args now.  */
14431
14432 static void
14433 cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
14434 {
14435   cp_lexer *saved_lexer;
14436   cp_token_cache *tokens;
14437   bool saved_local_variables_forbidden_p;
14438   tree parameters;
14439   
14440   for (parameters = TYPE_ARG_TYPES (type);
14441        parameters;
14442        parameters = TREE_CHAIN (parameters))
14443     {
14444       if (!TREE_PURPOSE (parameters)
14445           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14446         continue;
14447   
14448        /* Save away the current lexer.  */
14449       saved_lexer = parser->lexer;
14450        /* Create a new one, using the tokens we have saved.  */
14451       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14452       parser->lexer = cp_lexer_new_from_tokens (tokens);
14453
14454        /* Set the current source position to be the location of the
14455           first token in the default argument.  */
14456       cp_lexer_set_source_position_from_token 
14457         (parser->lexer, cp_lexer_peek_token (parser->lexer));
14458
14459        /* Local variable names (and the `this' keyword) may not appear
14460           in a default argument.  */
14461       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14462       parser->local_variables_forbidden_p = true;
14463        /* Parse the assignment-expression.  */
14464       if (scope)
14465         push_nested_class (scope, 1);
14466       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14467       if (scope)
14468         pop_nested_class ();
14469
14470        /* Restore saved state.  */
14471       parser->lexer = saved_lexer;
14472       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14473     }
14474 }
14475
14476 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14477    either a TYPE or an expression, depending on the form of the
14478    input.  The KEYWORD indicates which kind of expression we have
14479    encountered.  */
14480
14481 static tree
14482 cp_parser_sizeof_operand (parser, keyword)
14483      cp_parser *parser;
14484      enum rid keyword;
14485 {
14486   static const char *format;
14487   tree expr = NULL_TREE;
14488   const char *saved_message;
14489   bool saved_constant_expression_p;
14490
14491   /* Initialize FORMAT the first time we get here.  */
14492   if (!format)
14493     format = "types may not be defined in `%s' expressions";
14494
14495   /* Types cannot be defined in a `sizeof' expression.  Save away the
14496      old message.  */
14497   saved_message = parser->type_definition_forbidden_message;
14498   /* And create the new one.  */
14499   parser->type_definition_forbidden_message 
14500     = ((const char *) 
14501        xmalloc (strlen (format) 
14502                 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14503                 + 1 /* `\0' */));
14504   sprintf ((char *) parser->type_definition_forbidden_message,
14505            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14506
14507   /* The restrictions on constant-expressions do not apply inside
14508      sizeof expressions.  */
14509   saved_constant_expression_p = parser->constant_expression_p;
14510   parser->constant_expression_p = false;
14511
14512   /* Do not actually evaluate the expression.  */
14513   ++skip_evaluation;
14514   /* If it's a `(', then we might be looking at the type-id
14515      construction.  */
14516   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14517     {
14518       tree type;
14519
14520       /* We can't be sure yet whether we're looking at a type-id or an
14521          expression.  */
14522       cp_parser_parse_tentatively (parser);
14523       /* Consume the `('.  */
14524       cp_lexer_consume_token (parser->lexer);
14525       /* Parse the type-id.  */
14526       type = cp_parser_type_id (parser);
14527       /* Now, look for the trailing `)'.  */
14528       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14529       /* If all went well, then we're done.  */
14530       if (cp_parser_parse_definitely (parser))
14531         {
14532           /* Build a list of decl-specifiers; right now, we have only
14533              a single type-specifier.  */
14534           type = build_tree_list (NULL_TREE,
14535                                   type);
14536
14537           /* Call grokdeclarator to figure out what type this is.  */
14538           expr = grokdeclarator (NULL_TREE,
14539                                  type,
14540                                  TYPENAME,
14541                                  /*initialized=*/0,
14542                                  /*attrlist=*/NULL);
14543         }
14544     }
14545
14546   /* If the type-id production did not work out, then we must be
14547      looking at the unary-expression production.  */
14548   if (!expr)
14549     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14550   /* Go back to evaluating expressions.  */
14551   --skip_evaluation;
14552
14553   /* Free the message we created.  */
14554   free ((char *) parser->type_definition_forbidden_message);
14555   /* And restore the old one.  */
14556   parser->type_definition_forbidden_message = saved_message;
14557   parser->constant_expression_p = saved_constant_expression_p;
14558
14559   return expr;
14560 }
14561
14562 /* If the current declaration has no declarator, return true.  */
14563
14564 static bool
14565 cp_parser_declares_only_class_p (cp_parser *parser)
14566 {
14567   /* If the next token is a `;' or a `,' then there is no 
14568      declarator.  */
14569   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14570           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14571 }
14572
14573 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14574    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14575
14576 static bool
14577 cp_parser_friend_p (decl_specifiers)
14578      tree decl_specifiers;
14579 {
14580   while (decl_specifiers)
14581     {
14582       /* See if this decl-specifier is `friend'.  */
14583       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14584           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14585         return true;
14586
14587       /* Go on to the next decl-specifier.  */
14588       decl_specifiers = TREE_CHAIN (decl_specifiers);
14589     }
14590
14591   return false;
14592 }
14593
14594 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14595    issue an error message indicating that TOKEN_DESC was expected.
14596    
14597    Returns the token consumed, if the token had the appropriate type.
14598    Otherwise, returns NULL.  */
14599
14600 static cp_token *
14601 cp_parser_require (parser, type, token_desc)
14602      cp_parser *parser;
14603      enum cpp_ttype type;
14604      const char *token_desc;
14605 {
14606   if (cp_lexer_next_token_is (parser->lexer, type))
14607     return cp_lexer_consume_token (parser->lexer);
14608   else
14609     {
14610       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14611       if (!cp_parser_simulate_error (parser))
14612         error ("expected %s", token_desc);
14613       return NULL;
14614     }
14615 }
14616
14617 /* Like cp_parser_require, except that tokens will be skipped until
14618    the desired token is found.  An error message is still produced if
14619    the next token is not as expected.  */
14620
14621 static void
14622 cp_parser_skip_until_found (parser, type, token_desc)
14623      cp_parser *parser;
14624      enum cpp_ttype type;
14625      const char *token_desc;
14626 {
14627   cp_token *token;
14628   unsigned nesting_depth = 0;
14629
14630   if (cp_parser_require (parser, type, token_desc))
14631     return;
14632
14633   /* Skip tokens until the desired token is found.  */
14634   while (true)
14635     {
14636       /* Peek at the next token.  */
14637       token = cp_lexer_peek_token (parser->lexer);
14638       /* If we've reached the token we want, consume it and 
14639          stop.  */
14640       if (token->type == type && !nesting_depth)
14641         {
14642           cp_lexer_consume_token (parser->lexer);
14643           return;
14644         }
14645       /* If we've run out of tokens, stop.  */
14646       if (token->type == CPP_EOF)
14647         return;
14648       if (token->type == CPP_OPEN_BRACE 
14649           || token->type == CPP_OPEN_PAREN
14650           || token->type == CPP_OPEN_SQUARE)
14651         ++nesting_depth;
14652       else if (token->type == CPP_CLOSE_BRACE 
14653                || token->type == CPP_CLOSE_PAREN
14654                || token->type == CPP_CLOSE_SQUARE)
14655         {
14656           if (nesting_depth-- == 0)
14657             return;
14658         }
14659       /* Consume this token.  */
14660       cp_lexer_consume_token (parser->lexer);
14661     }
14662 }
14663
14664 /* If the next token is the indicated keyword, consume it.  Otherwise,
14665    issue an error message indicating that TOKEN_DESC was expected.
14666    
14667    Returns the token consumed, if the token had the appropriate type.
14668    Otherwise, returns NULL.  */
14669
14670 static cp_token *
14671 cp_parser_require_keyword (parser, keyword, token_desc)
14672      cp_parser *parser;
14673      enum rid keyword;
14674      const char *token_desc;
14675 {
14676   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14677
14678   if (token && token->keyword != keyword)
14679     {
14680       dyn_string_t error_msg;
14681
14682       /* Format the error message.  */
14683       error_msg = dyn_string_new (0);
14684       dyn_string_append_cstr (error_msg, "expected ");
14685       dyn_string_append_cstr (error_msg, token_desc);
14686       cp_parser_error (parser, error_msg->s);
14687       dyn_string_delete (error_msg);
14688       return NULL;
14689     }
14690
14691   return token;
14692 }
14693
14694 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14695    function-definition.  */
14696
14697 static bool 
14698 cp_parser_token_starts_function_definition_p (token)
14699      cp_token *token;
14700 {
14701   return (/* An ordinary function-body begins with an `{'.  */
14702           token->type == CPP_OPEN_BRACE
14703           /* A ctor-initializer begins with a `:'.  */
14704           || token->type == CPP_COLON
14705           /* A function-try-block begins with `try'.  */
14706           || token->keyword == RID_TRY
14707           /* The named return value extension begins with `return'.  */
14708           || token->keyword == RID_RETURN);
14709 }
14710
14711 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14712    definition.  */
14713
14714 static bool
14715 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14716 {
14717   cp_token *token;
14718
14719   token = cp_lexer_peek_token (parser->lexer);
14720   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14721 }
14722
14723 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14724    or none_type otherwise.  */
14725
14726 static enum tag_types
14727 cp_parser_token_is_class_key (token)
14728      cp_token *token;
14729 {
14730   switch (token->keyword)
14731     {
14732     case RID_CLASS:
14733       return class_type;
14734     case RID_STRUCT:
14735       return record_type;
14736     case RID_UNION:
14737       return union_type;
14738       
14739     default:
14740       return none_type;
14741     }
14742 }
14743
14744 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14745
14746 static void
14747 cp_parser_check_class_key (enum tag_types class_key, tree type)
14748 {
14749   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14750     pedwarn ("`%s' tag used in naming `%#T'",
14751             class_key == union_type ? "union"
14752              : class_key == record_type ? "struct" : "class", 
14753              type);
14754 }
14755                            
14756 /* Look for the `template' keyword, as a syntactic disambiguator.
14757    Return TRUE iff it is present, in which case it will be 
14758    consumed.  */
14759
14760 static bool
14761 cp_parser_optional_template_keyword (cp_parser *parser)
14762 {
14763   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14764     {
14765       /* The `template' keyword can only be used within templates;
14766          outside templates the parser can always figure out what is a
14767          template and what is not.  */
14768       if (!processing_template_decl)
14769         {
14770           error ("`template' (as a disambiguator) is only allowed "
14771                  "within templates");
14772           /* If this part of the token stream is rescanned, the same
14773              error message would be generated.  So, we purge the token
14774              from the stream.  */
14775           cp_lexer_purge_token (parser->lexer);
14776           return false;
14777         }
14778       else
14779         {
14780           /* Consume the `template' keyword.  */
14781           cp_lexer_consume_token (parser->lexer);
14782           return true;
14783         }
14784     }
14785
14786   return false;
14787 }
14788
14789 /* Add tokens to CACHE until an non-nested END token appears.  */
14790
14791 static void
14792 cp_parser_cache_group (cp_parser *parser, 
14793                        cp_token_cache *cache,
14794                        enum cpp_ttype end,
14795                        unsigned depth)
14796 {
14797   while (true)
14798     {
14799       cp_token *token;
14800
14801       /* Abort a parenthesized expression if we encounter a brace.  */
14802       if ((end == CPP_CLOSE_PAREN || depth == 0)
14803           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14804         return;
14805       /* Consume the next token.  */
14806       token = cp_lexer_consume_token (parser->lexer);
14807       /* If we've reached the end of the file, stop.  */
14808       if (token->type == CPP_EOF)
14809         return;
14810       /* Add this token to the tokens we are saving.  */
14811       cp_token_cache_push_token (cache, token);
14812       /* See if it starts a new group.  */
14813       if (token->type == CPP_OPEN_BRACE)
14814         {
14815           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14816           if (depth == 0)
14817             return;
14818         }
14819       else if (token->type == CPP_OPEN_PAREN)
14820         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14821       else if (token->type == end)
14822         return;
14823     }
14824 }
14825
14826 /* Begin parsing tentatively.  We always save tokens while parsing
14827    tentatively so that if the tentative parsing fails we can restore the
14828    tokens.  */
14829
14830 static void
14831 cp_parser_parse_tentatively (parser)
14832      cp_parser *parser;
14833 {
14834   /* Enter a new parsing context.  */
14835   parser->context = cp_parser_context_new (parser->context);
14836   /* Begin saving tokens.  */
14837   cp_lexer_save_tokens (parser->lexer);
14838   /* In order to avoid repetitive access control error messages,
14839      access checks are queued up until we are no longer parsing
14840      tentatively.  */
14841   cp_parser_start_deferring_access_checks (parser);
14842 }
14843
14844 /* Commit to the currently active tentative parse.  */
14845
14846 static void
14847 cp_parser_commit_to_tentative_parse (parser)
14848      cp_parser *parser;
14849 {
14850   cp_parser_context *context;
14851   cp_lexer *lexer;
14852
14853   /* Mark all of the levels as committed.  */
14854   lexer = parser->lexer;
14855   for (context = parser->context; context->next; context = context->next)
14856     {
14857       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14858         break;
14859       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14860       while (!cp_lexer_saving_tokens (lexer))
14861         lexer = lexer->next;
14862       cp_lexer_commit_tokens (lexer);
14863     }
14864 }
14865
14866 /* Abort the currently active tentative parse.  All consumed tokens
14867    will be rolled back, and no diagnostics will be issued.  */
14868
14869 static void
14870 cp_parser_abort_tentative_parse (parser)
14871      cp_parser *parser;
14872 {
14873   cp_parser_simulate_error (parser);
14874   /* Now, pretend that we want to see if the construct was
14875      successfully parsed.  */
14876   cp_parser_parse_definitely (parser);
14877 }
14878
14879 /* Stop parsing tentatively.  If a parse error has ocurred, restore the
14880    token stream.  Otherwise, commit to the tokens we have consumed.
14881    Returns true if no error occurred; false otherwise.  */
14882
14883 static bool
14884 cp_parser_parse_definitely (parser)
14885      cp_parser *parser;
14886 {
14887   bool error_occurred;
14888   cp_parser_context *context;
14889
14890   /* Remember whether or not an error ocurred, since we are about to
14891      destroy that information.  */
14892   error_occurred = cp_parser_error_occurred (parser);
14893   /* Remove the topmost context from the stack.  */
14894   context = parser->context;
14895   parser->context = context->next;
14896   /* If no parse errors occurred, commit to the tentative parse.  */
14897   if (!error_occurred)
14898     {
14899       /* Commit to the tokens read tentatively, unless that was
14900          already done.  */
14901       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14902         cp_lexer_commit_tokens (parser->lexer);
14903       if (!parser->context->deferring_access_checks_p)
14904         /* If in the parent context we are not deferring checks, then
14905            these perform these checks now.  */
14906         (cp_parser_perform_deferred_access_checks 
14907          (context->deferred_access_checks));
14908       else
14909         /* Any lookups that were deferred during the tentative parse are
14910            still deferred.  */
14911         parser->context->deferred_access_checks 
14912           = chainon (parser->context->deferred_access_checks,
14913                      context->deferred_access_checks);
14914     }
14915   /* Otherwise, if errors occurred, roll back our state so that things
14916      are just as they were before we began the tentative parse.  */
14917   else
14918     cp_lexer_rollback_tokens (parser->lexer);
14919   /* Add the context to the front of the free list.  */
14920   context->next = cp_parser_context_free_list;
14921   cp_parser_context_free_list = context;
14922
14923   return !error_occurred;
14924 }
14925
14926 /* Returns true if we are parsing tentatively -- but have decided that
14927    we will stick with this tentative parse, even if errors occur.  */
14928
14929 static bool
14930 cp_parser_committed_to_tentative_parse (parser)
14931      cp_parser *parser;
14932 {
14933   return (cp_parser_parsing_tentatively (parser)
14934           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14935 }
14936
14937 /* Returns non-zero iff an error has occurred during the most recent
14938    tentative parse.  */
14939    
14940 static bool
14941 cp_parser_error_occurred (parser)
14942      cp_parser *parser;
14943 {
14944   return (cp_parser_parsing_tentatively (parser)
14945           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14946 }
14947
14948 /* Returns non-zero if GNU extensions are allowed.  */
14949
14950 static bool
14951 cp_parser_allow_gnu_extensions_p (parser)
14952      cp_parser *parser;
14953 {
14954   return parser->allow_gnu_extensions_p;
14955 }
14956
14957 \f
14958
14959 /* The parser.  */
14960
14961 static GTY (()) cp_parser *the_parser;
14962
14963 /* External interface.  */
14964
14965 /* Parse the entire translation unit.  */
14966
14967 int
14968 yyparse ()
14969 {
14970   bool error_occurred;
14971
14972   the_parser = cp_parser_new ();
14973   error_occurred = cp_parser_translation_unit (the_parser);
14974   the_parser = NULL;
14975
14976   return error_occurred;
14977 }
14978
14979 /* Clean up after parsing the entire translation unit.  */
14980
14981 void
14982 free_parser_stacks ()
14983 {
14984   /* Nothing to do.  */
14985 }
14986
14987 /* This variable must be provided by every front end.  */
14988
14989 int yydebug;
14990
14991 #include "gt-cp-parser.h"