OSDN Git Service

* parser.c (cp_parser_primary_expression): See through explicitly
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "ggc.h"
36 #include "toplev.h"
37 #include "output.h"
38
39 \f
40 /* The lexer.  */
41
42 /* Overview
43    --------
44
45    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
46    look-ahead.
47
48    Methodology
49    -----------
50
51    We use a circular buffer to store incoming tokens.
52
53    Some artifacts of the C++ language (such as the
54    expression/declaration ambiguity) require arbitrary look-ahead.
55    The strategy we adopt for dealing with these problems is to attempt
56    to parse one construct (e.g., the declaration) and fall back to the
57    other (e.g., the expression) if that attempt does not succeed.
58    Therefore, we must sometimes store an arbitrary number of tokens.
59
60    The parser routinely peeks at the next token, and then consumes it
61    later.  That also requires a buffer in which to store the tokens.
62      
63    In order to easily permit adding tokens to the end of the buffer,
64    while removing them from the beginning of the buffer, we use a
65    circular buffer.  */
66
67 /* A C++ token.  */
68
69 typedef struct cp_token GTY (())
70 {
71   /* The kind of token.  */
72   enum cpp_ttype type;
73   /* The value associated with this token, if any.  */
74   tree value;
75   /* If this token is a keyword, this value indicates which keyword.
76      Otherwise, this value is RID_MAX.  */
77   enum rid keyword;
78   /* The file in which this token was found.  */
79   const char *file_name;
80   /* The line at which this token was found.  */
81   int line_number;
82 } cp_token;
83
84 /* The number of tokens in a single token block.  */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
87
88 /* A group of tokens.  These groups are chained together to store
89    large numbers of tokens.  (For example, a token block is created
90    when the body of an inline member function is first encountered;
91    the tokens are processed later after the class definition is
92    complete.)  
93
94    This somewhat ungainly data structure (as opposed to, say, a
95    variable-length array), is used due to contraints imposed by the
96    current garbage-collection methodology.  If it is made more
97    flexible, we could perhaps simplify the data structures involved.  */
98
99 typedef struct cp_token_block GTY (())
100 {
101   /* The tokens.  */
102   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103   /* The number of tokens in this block.  */
104   size_t num_tokens;
105   /* The next token block in the chain.  */
106   struct cp_token_block *next;
107   /* The previous block in the chain.  */
108   struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113   /* The first block in the cache.  NULL if there are no tokens in the
114      cache.  */
115   cp_token_block *first;
116   /* The last block in the cache.  NULL If there are no tokens in the
117      cache.  */
118   cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes. */
122
123 static cp_token_cache *cp_token_cache_new 
124   (void);
125 static void cp_token_cache_push_token
126   (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache.  */
129
130 static cp_token_cache *
131 cp_token_cache_new ()
132 {
133   return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE.  */
137
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140                            cp_token *token)
141 {
142   cp_token_block *b = cache->last;
143
144   /* See if we need to allocate a new token block.  */
145   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146     {
147       b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
148       b->prev = cache->last;
149       if (cache->last)
150         {
151           cache->last->next = b;
152           cache->last = b;
153         }
154       else
155         cache->first = cache->last = b;
156     }
157   /* Add this token to the current token block.  */
158   b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer.  It is responsible
162    for managing the token stream from the preprocessor and supplying
163    it to the parser.  */
164
165 typedef struct cp_lexer GTY (())
166 {
167   /* The memory allocated for the buffer.  Never NULL.  */
168   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169   /* A pointer just past the end of the memory allocated for the buffer.  */
170   cp_token * GTY ((skip (""))) buffer_end;
171   /* The first valid token in the buffer, or NULL if none.  */
172   cp_token * GTY ((skip (""))) first_token;
173   /* The next available token.  If NEXT_TOKEN is NULL, then there are
174      no more available tokens.  */
175   cp_token * GTY ((skip (""))) next_token;
176   /* A pointer just past the last available token.  If FIRST_TOKEN is
177      NULL, however, there are no available tokens, and then this
178      location is simply the place in which the next token read will be
179      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180      When the LAST_TOKEN == BUFFER, then the last token is at the
181      highest memory address in the BUFFER.  */
182   cp_token * GTY ((skip (""))) last_token;
183
184   /* A stack indicating positions at which cp_lexer_save_tokens was
185      called.  The top entry is the most recent position at which we
186      began saving tokens.  The entries are differences in token
187      position between FIRST_TOKEN and the first saved token.
188
189      If the stack is non-empty, we are saving tokens.  When a token is
190      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191      pointer will not.  The token stream will be preserved so that it
192      can be reexamined later.
193
194      If the stack is empty, then we are not saving tokens.  Whenever a
195      token is consumed, the FIRST_TOKEN pointer will be moved, and the
196      consumed token will be gone forever.  */
197   varray_type saved_tokens;
198
199   /* The STRING_CST tokens encountered while processing the current
200      string literal.  */
201   varray_type string_tokens;
202
203   /* True if we should obtain more tokens from the preprocessor; false
204      if we are processing a saved token cache.  */
205   bool main_lexer_p;
206
207   /* True if we should output debugging information.  */
208   bool debugging_p;
209
210   /* The next lexer in a linked list of lexers.  */
211   struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes.  */
215
216 static cp_lexer *cp_lexer_new
217   PARAMS ((bool));
218 static cp_lexer *cp_lexer_new_from_tokens
219   PARAMS ((struct cp_token_cache *));
220 static int cp_lexer_saving_tokens
221   PARAMS ((const cp_lexer *));
222 static cp_token *cp_lexer_next_token
223   PARAMS ((cp_lexer *, cp_token *));
224 static ptrdiff_t cp_lexer_token_difference
225   PARAMS ((cp_lexer *, cp_token *, cp_token *));
226 static cp_token *cp_lexer_read_token
227   PARAMS ((cp_lexer *));
228 static void cp_lexer_maybe_grow_buffer
229   PARAMS ((cp_lexer *));
230 static void cp_lexer_get_preprocessor_token
231   PARAMS ((cp_lexer *, cp_token *));
232 static cp_token *cp_lexer_peek_token
233   PARAMS ((cp_lexer *));
234 static cp_token *cp_lexer_peek_nth_token
235   PARAMS ((cp_lexer *, size_t));
236 static inline bool cp_lexer_next_token_is
237   PARAMS ((cp_lexer *, enum cpp_ttype));
238 static bool cp_lexer_next_token_is_not
239   PARAMS ((cp_lexer *, enum cpp_ttype));
240 static bool cp_lexer_next_token_is_keyword
241   PARAMS ((cp_lexer *, enum rid));
242 static cp_token *cp_lexer_consume_token
243   PARAMS ((cp_lexer *));
244 static void cp_lexer_purge_token
245   (cp_lexer *);
246 static void cp_lexer_purge_tokens_after
247   (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249   PARAMS ((cp_lexer *));
250 static void cp_lexer_commit_tokens
251   PARAMS ((cp_lexer *));
252 static void cp_lexer_rollback_tokens
253   PARAMS ((cp_lexer *));
254 static inline void cp_lexer_set_source_position_from_token 
255   PARAMS ((cp_lexer *, const cp_token *));
256 static void cp_lexer_print_token
257   PARAMS ((FILE *, cp_token *));
258 static inline bool cp_lexer_debugging_p 
259   PARAMS ((cp_lexer *));
260 static void cp_lexer_start_debugging
261   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
264
265 /* Manifest constants.  */
266
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
269
270 /* A token type for keywords, as opposed to ordinary identifiers.  */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273 /* A token type for template-ids.  If a template-id is processed while
274    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275    the value of the CPP_TEMPLATE_ID is whatever was returned by
276    cp_parser_template_id.  */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279 /* A token type for nested-name-specifiers.  If a
280    nested-name-specifier is processed while parsing tentatively, it is
281    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283    cp_parser_nested_name_specifier_opt.  */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286 /* A token type for tokens that are not tokens at all; these are used
287    to mark the end of a token block.  */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290 /* Variables.  */
291
292 /* The stream to which debugging output should be written.  */
293 static FILE *cp_lexer_debug_stream;
294
295 /* Create a new C++ lexer.  If MAIN_LEXER_P is true the new lexer is
296    the main lexer -- i.e, the lexer that gets tokens from the
297    preprocessor.  Otherwise, it is a lexer that uses a cache of stored
298    tokens.  */
299
300 static cp_lexer *
301 cp_lexer_new (bool main_lexer_p)
302 {
303   cp_lexer *lexer;
304
305   /* Allocate the memory.  */
306   lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
307
308   /* Create the circular buffer.  */
309   lexer->buffer = ((cp_token *) 
310                    ggc_alloc (CP_TOKEN_BUFFER_SIZE * sizeof (cp_token)));
311   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
312
313   /* There are no tokens in the buffer.  */
314   lexer->last_token = lexer->buffer;
315
316   /* This lexer obtains more tokens by calling c_lex.  */
317   lexer->main_lexer_p = main_lexer_p;
318
319   /* Create the SAVED_TOKENS stack.  */
320   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
321   
322   /* Create the STRINGS array.  */
323   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
324
325   /* Assume we are not debugging.  */
326   lexer->debugging_p = false;
327
328   return lexer;
329 }
330
331 /* Create a new lexer whose token stream is primed with the TOKENS.
332    When these tokens are exhausted, no new tokens will be read.  */
333
334 static cp_lexer *
335 cp_lexer_new_from_tokens (cp_token_cache *tokens)
336 {
337   cp_lexer *lexer;
338   cp_token *token;
339   cp_token_block *block;
340   ptrdiff_t num_tokens;
341
342   /* Create the lexer.  */
343   lexer = cp_lexer_new (/*main_lexer_p=*/false);
344
345   /* Create a new buffer, appropriately sized.  */
346   num_tokens = 0;
347   for (block = tokens->first; block != NULL; block = block->next)
348     num_tokens += block->num_tokens;
349   lexer->buffer = ((cp_token *) 
350                    ggc_alloc (num_tokens * sizeof (cp_token)));
351   lexer->buffer_end = lexer->buffer + num_tokens;
352   
353   /* Install the tokens.  */
354   token = lexer->buffer;
355   for (block = tokens->first; block != NULL; block = block->next)
356     {
357       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
358       token += block->num_tokens;
359     }
360
361   /* The FIRST_TOKEN is the beginning of the buffer.  */
362   lexer->first_token = lexer->buffer;
363   /* The next available token is also at the beginning of the buffer.  */
364   lexer->next_token = lexer->buffer;
365   /* The buffer is full.  */
366   lexer->last_token = lexer->first_token;
367
368   return lexer;
369 }
370
371 /* Returns non-zero if debugging information should be output.  */
372
373 static inline bool
374 cp_lexer_debugging_p (cp_lexer *lexer)
375 {
376   return lexer->debugging_p;
377 }
378
379 /* Set the current source position from the information stored in
380    TOKEN.  */
381
382 static inline void
383 cp_lexer_set_source_position_from_token (lexer, token)
384      cp_lexer *lexer ATTRIBUTE_UNUSED;
385      const cp_token *token;
386 {
387   /* Ideally, the source position information would not be a global
388      variable, but it is.  */
389
390   /* Update the line number.  */
391   if (token->type != CPP_EOF)
392     {
393       lineno = token->line_number;
394       input_filename = token->file_name;
395     }
396 }
397
398 /* TOKEN points into the circular token buffer.  Return a pointer to
399    the next token in the buffer.  */
400
401 static inline cp_token *
402 cp_lexer_next_token (lexer, token)
403      cp_lexer *lexer;
404      cp_token *token;
405 {
406   token++;
407   if (token == lexer->buffer_end)
408     token = lexer->buffer;
409   return token;
410 }
411
412 /* Non-zero if we are presently saving tokens.  */
413
414 static int
415 cp_lexer_saving_tokens (lexer)
416      const cp_lexer *lexer;
417 {
418   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
419 }
420
421 /* Return a pointer to the token that is N tokens beyond TOKEN in the
422    buffer.  */
423
424 static cp_token *
425 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
426 {
427   token += n;
428   if (token >= lexer->buffer_end)
429     token = lexer->buffer + (token - lexer->buffer_end);
430   return token;
431 }
432
433 /* Returns the number of times that START would have to be incremented
434    to reach FINISH.  If START and FINISH are the same, returns zero.  */
435
436 static ptrdiff_t
437 cp_lexer_token_difference (lexer, start, finish)
438      cp_lexer *lexer;
439      cp_token *start;
440      cp_token *finish;
441 {
442   if (finish >= start)
443     return finish - start;
444   else
445     return ((lexer->buffer_end - lexer->buffer)
446             - (start - finish));
447 }
448
449 /* Obtain another token from the C preprocessor and add it to the
450    token buffer.  Returns the newly read token.  */
451
452 static cp_token *
453 cp_lexer_read_token (lexer)
454      cp_lexer *lexer;
455 {
456   cp_token *token;
457
458   /* Make sure there is room in the buffer.  */
459   cp_lexer_maybe_grow_buffer (lexer);
460
461   /* If there weren't any tokens, then this one will be the first.  */
462   if (!lexer->first_token)
463     lexer->first_token = lexer->last_token;
464   /* Similarly, if there were no available tokens, there is one now.  */
465   if (!lexer->next_token)
466     lexer->next_token = lexer->last_token;
467
468   /* Figure out where we're going to store the new token.  */
469   token = lexer->last_token;
470
471   /* Get a new token from the preprocessor.  */
472   cp_lexer_get_preprocessor_token (lexer, token);
473
474   /* Increment LAST_TOKEN.  */
475   lexer->last_token = cp_lexer_next_token (lexer, token);
476
477   /* The preprocessor does not yet do translation phase six, i.e., the
478      combination of adjacent string literals.  Therefore, we do it
479      here.  */
480   if (token->type == CPP_STRING || token->type == CPP_WSTRING)
481     {
482       ptrdiff_t delta;
483       int i;
484
485       /* When we grow the buffer, we may invalidate TOKEN.  So, save
486          the distance from the beginning of the BUFFER so that we can
487          recaulate it.  */
488       delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
489       /* Make sure there is room in the buffer for another token.  */
490       cp_lexer_maybe_grow_buffer (lexer);
491       /* Restore TOKEN.  */
492       token = lexer->buffer;
493       for (i = 0; i < delta; ++i)
494         token = cp_lexer_next_token (lexer, token);
495
496       VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
497       while (true)
498         {
499           /* Read the token after TOKEN.  */
500           cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
501           /* See whether it's another string constant.  */
502           if (lexer->last_token->type != token->type)
503             {
504               /* If not, then it will be the next real token.  */
505               lexer->last_token = cp_lexer_next_token (lexer, 
506                                                        lexer->last_token);
507               break;
508             }
509
510           /* Chain the strings together.  */
511           VARRAY_PUSH_TREE (lexer->string_tokens, 
512                             lexer->last_token->value);
513         }
514
515       /* Create a single STRING_CST.  Curiously we have to call
516          combine_strings even if there is only a single string in
517          order to get the type set correctly.  */
518       token->value = combine_strings (lexer->string_tokens);
519       VARRAY_CLEAR (lexer->string_tokens);
520       token->value = fix_string_type (token->value);
521       /* Strings should have type `const char []'.  Right now, we will
522          have an ARRAY_TYPE that is constant rather than an array of
523          constant elements.  */
524       if (flag_const_strings)
525         {
526           tree type;
527
528           /* Get the current type.  It will be an ARRAY_TYPE.  */
529           type = TREE_TYPE (token->value);
530           /* Use build_cplus_array_type to rebuild the array, thereby
531              getting the right type.  */
532           type = build_cplus_array_type (TREE_TYPE (type),
533                                          TYPE_DOMAIN (type));
534           /* Reset the type of the token.  */
535           TREE_TYPE (token->value) = type;
536         }
537     }
538
539   return token;
540 }
541
542 /* If the circular buffer is full, make it bigger.  */
543
544 static void
545 cp_lexer_maybe_grow_buffer (lexer)
546      cp_lexer *lexer;
547 {
548   /* If the buffer is full, enlarge it.  */
549   if (lexer->last_token == lexer->first_token)
550     {
551       cp_token *new_buffer;
552       cp_token *old_buffer;
553       cp_token *new_first_token;
554       ptrdiff_t buffer_length;
555       size_t num_tokens_to_copy;
556
557       /* Remember the current buffer pointer.  It will become invalid,
558          but we will need to do pointer arithmetic involving this
559          value.  */
560       old_buffer = lexer->buffer;
561       /* Compute the current buffer size.  */
562       buffer_length = lexer->buffer_end - lexer->buffer;
563       /* Allocate a buffer twice as big.  */
564       new_buffer = ((cp_token *)
565                     ggc_realloc (lexer->buffer, 
566                                  2 * buffer_length * sizeof (cp_token)));
567       
568       /* Because the buffer is circular, logically consecutive tokens
569          are not necessarily placed consecutively in memory.
570          Therefore, we must keep move the tokens that were before
571          FIRST_TOKEN to the second half of the newly allocated
572          buffer.  */
573       num_tokens_to_copy = (lexer->first_token - old_buffer);
574       memcpy (new_buffer + buffer_length,
575               new_buffer,
576               num_tokens_to_copy * sizeof (cp_token));
577       /* Clear the rest of the buffer.  We never look at this storage,
578          but the garbage collector may.  */
579       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
580               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
581
582       /* Now recompute all of the buffer pointers.  */
583       new_first_token 
584         = new_buffer + (lexer->first_token - old_buffer);
585       if (lexer->next_token != NULL)
586         {
587           ptrdiff_t next_token_delta;
588
589           if (lexer->next_token > lexer->first_token)
590             next_token_delta = lexer->next_token - lexer->first_token;
591           else
592             next_token_delta = 
593               buffer_length - (lexer->first_token - lexer->next_token);
594           lexer->next_token = new_first_token + next_token_delta;
595         }
596       lexer->last_token = new_first_token + buffer_length;
597       lexer->buffer = new_buffer;
598       lexer->buffer_end = new_buffer + buffer_length * 2;
599       lexer->first_token = new_first_token;
600     }
601 }
602
603 /* Store the next token from the preprocessor in *TOKEN.  */
604
605 static void 
606 cp_lexer_get_preprocessor_token (lexer, token)
607      cp_lexer *lexer ATTRIBUTE_UNUSED;
608      cp_token *token;
609 {
610   bool done;
611
612   /* If this not the main lexer, return a terminating CPP_EOF token.  */
613   if (!lexer->main_lexer_p)
614     {
615       token->type = CPP_EOF;
616       token->line_number = 0;
617       token->file_name = NULL;
618       token->value = NULL_TREE;
619       token->keyword = RID_MAX;
620
621       return;
622     }
623
624   done = false;
625   /* Keep going until we get a token we like.  */
626   while (!done)
627     {
628       /* Get a new token from the preprocessor.  */
629       token->type = c_lex (&token->value);
630       /* Issue messages about tokens we cannot process.  */
631       switch (token->type)
632         {
633         case CPP_ATSIGN:
634         case CPP_HASH:
635         case CPP_PASTE:
636           error ("invalid token");
637           break;
638
639         case CPP_OTHER:
640           /* These tokens are already warned about by c_lex.  */
641           break;
642
643         default:
644           /* This is a good token, so we exit the loop.  */
645           done = true;
646           break;
647         }
648     }
649   /* Now we've got our token.  */
650   token->line_number = lineno;
651   token->file_name = input_filename;
652
653   /* Check to see if this token is a keyword.  */
654   if (token->type == CPP_NAME 
655       && C_IS_RESERVED_WORD (token->value))
656     {
657       /* Mark this token as a keyword.  */
658       token->type = CPP_KEYWORD;
659       /* Record which keyword.  */
660       token->keyword = C_RID_CODE (token->value);
661       /* Update the value.  Some keywords are mapped to particular
662          entities, rather than simply having the value of the
663          corresponding IDENTIFIER_NODE.  For example, `__const' is
664          mapped to `const'.  */
665       token->value = ridpointers[token->keyword];
666     }
667   else
668     token->keyword = RID_MAX;
669 }
670
671 /* Return a pointer to the next token in the token stream, but do not
672    consume it.  */
673
674 static cp_token *
675 cp_lexer_peek_token (lexer)
676      cp_lexer *lexer;
677 {
678   cp_token *token;
679
680   /* If there are no tokens, read one now.  */
681   if (!lexer->next_token)
682     cp_lexer_read_token (lexer);
683
684   /* Provide debugging output.  */
685   if (cp_lexer_debugging_p (lexer))
686     {
687       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
688       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
689       fprintf (cp_lexer_debug_stream, "\n");
690     }
691
692   token = lexer->next_token;
693   cp_lexer_set_source_position_from_token (lexer, token);
694   return token;
695 }
696
697 /* Return true if the next token has the indicated TYPE.  */
698
699 static bool
700 cp_lexer_next_token_is (lexer, type)
701      cp_lexer *lexer;
702      enum cpp_ttype type;
703 {
704   cp_token *token;
705
706   /* Peek at the next token.  */
707   token = cp_lexer_peek_token (lexer);
708   /* Check to see if it has the indicated TYPE.  */
709   return token->type == type;
710 }
711
712 /* Return true if the next token does not have the indicated TYPE.  */
713
714 static bool
715 cp_lexer_next_token_is_not (lexer, type)
716      cp_lexer *lexer;
717      enum cpp_ttype type;
718 {
719   return !cp_lexer_next_token_is (lexer, type);
720 }
721
722 /* Return true if the next token is the indicated KEYWORD.  */
723
724 static bool
725 cp_lexer_next_token_is_keyword (lexer, keyword)
726      cp_lexer *lexer;
727      enum rid keyword;
728 {
729   cp_token *token;
730
731   /* Peek at the next token.  */
732   token = cp_lexer_peek_token (lexer);
733   /* Check to see if it is the indicated keyword.  */
734   return token->keyword == keyword;
735 }
736
737 /* Return a pointer to the Nth token in the token stream.  If N is 1,
738    then this is precisely equivalent to cp_lexer_peek_token.  */
739
740 static cp_token *
741 cp_lexer_peek_nth_token (lexer, n)
742      cp_lexer *lexer;
743      size_t n;
744 {
745   cp_token *token;
746
747   /* N is 1-based, not zero-based.  */
748   my_friendly_assert (n > 0, 20000224);
749
750   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
751   token = lexer->next_token;
752   /* If there are no tokens in the buffer, get one now.  */
753   if (!token)
754     {
755       cp_lexer_read_token (lexer);
756       token = lexer->next_token;
757     }
758
759   /* Now, read tokens until we have enough.  */
760   while (--n > 0)
761     {
762       /* Advance to the next token.  */
763       token = cp_lexer_next_token (lexer, token);
764       /* If that's all the tokens we have, read a new one.  */
765       if (token == lexer->last_token)
766         token = cp_lexer_read_token (lexer);
767     }
768
769   return token;
770 }
771
772 /* Consume the next token.  The pointer returned is valid only until
773    another token is read.  Callers should preserve copy the token
774    explicitly if they will need its value for a longer period of
775    time.  */
776
777 static cp_token *
778 cp_lexer_consume_token (lexer)
779      cp_lexer *lexer;
780 {
781   cp_token *token;
782
783   /* If there are no tokens, read one now.  */
784   if (!lexer->next_token)
785     cp_lexer_read_token (lexer);
786
787   /* Remember the token we'll be returning.  */
788   token = lexer->next_token;
789
790   /* Increment NEXT_TOKEN.  */
791   lexer->next_token = cp_lexer_next_token (lexer, 
792                                            lexer->next_token);
793   /* Check to see if we're all out of tokens.  */
794   if (lexer->next_token == lexer->last_token)
795     lexer->next_token = NULL;
796
797   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
798   if (!cp_lexer_saving_tokens (lexer))
799     {
800       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
801       if (!lexer->next_token)
802         lexer->first_token = NULL;
803       else
804         lexer->first_token = lexer->next_token;
805     }
806
807   /* Provide debugging output.  */
808   if (cp_lexer_debugging_p (lexer))
809     {
810       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
811       cp_lexer_print_token (cp_lexer_debug_stream, token);
812       fprintf (cp_lexer_debug_stream, "\n");
813     }
814
815   return token;
816 }
817
818 /* Permanently remove the next token from the token stream.  There
819    must be a valid next token already; this token never reads
820    additional tokens from the preprocessor.  */
821
822 static void
823 cp_lexer_purge_token (cp_lexer *lexer)
824 {
825   cp_token *token;
826   cp_token *next_token;
827
828   token = lexer->next_token;
829   while (true) 
830     {
831       next_token = cp_lexer_next_token (lexer, token);
832       if (next_token == lexer->last_token)
833         break;
834       *token = *next_token;
835       token = next_token;
836     }
837
838   lexer->last_token = token;
839   /* The token purged may have been the only token remaining; if so,
840      clear NEXT_TOKEN.  */
841   if (lexer->next_token == token)
842     lexer->next_token = NULL;
843 }
844
845 /* Permanently remove all tokens after TOKEN, up to, but not
846    including, the token that will be returned next by
847    cp_lexer_peek_token.  */
848
849 static void
850 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
851 {
852   cp_token *peek;
853   cp_token *t1;
854   cp_token *t2;
855
856   if (lexer->next_token)
857     {
858       /* Copy the tokens that have not yet been read to the location
859          immediately following TOKEN.  */
860       t1 = cp_lexer_next_token (lexer, token);
861       t2 = peek = cp_lexer_peek_token (lexer);
862       /* Move tokens into the vacant area between TOKEN and PEEK.  */
863       while (t2 != lexer->last_token)
864         {
865           *t1 = *t2;
866           t1 = cp_lexer_next_token (lexer, t1);
867           t2 = cp_lexer_next_token (lexer, t2);
868         }
869       /* Now, the next available token is right after TOKEN.  */
870       lexer->next_token = cp_lexer_next_token (lexer, token);
871       /* And the last token is wherever we ended up.  */
872       lexer->last_token = t1;
873     }
874   else
875     {
876       /* There are no tokens in the buffer, so there is nothing to
877          copy.  The last token in the buffer is TOKEN itself.  */
878       lexer->last_token = cp_lexer_next_token (lexer, token);
879     }
880 }
881
882 /* Begin saving tokens.  All tokens consumed after this point will be
883    preserved.  */
884
885 static void
886 cp_lexer_save_tokens (lexer)
887      cp_lexer *lexer;
888 {
889   /* Provide debugging output.  */
890   if (cp_lexer_debugging_p (lexer))
891     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
892
893   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
894      restore the tokens if required.  */
895   if (!lexer->next_token)
896     cp_lexer_read_token (lexer);
897
898   VARRAY_PUSH_INT (lexer->saved_tokens,
899                    cp_lexer_token_difference (lexer,
900                                               lexer->first_token,
901                                               lexer->next_token));
902 }
903
904 /* Commit to the portion of the token stream most recently saved.  */
905
906 static void
907 cp_lexer_commit_tokens (lexer)
908      cp_lexer *lexer;
909 {
910   /* Provide debugging output.  */
911   if (cp_lexer_debugging_p (lexer))
912     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
913
914   VARRAY_POP (lexer->saved_tokens);
915 }
916
917 /* Return all tokens saved since the last call to cp_lexer_save_tokens
918    to the token stream.  Stop saving tokens.  */
919
920 static void
921 cp_lexer_rollback_tokens (lexer)
922      cp_lexer *lexer;
923 {
924   size_t delta;
925
926   /* Provide debugging output.  */
927   if (cp_lexer_debugging_p (lexer))
928     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
929
930   /* Find the token that was the NEXT_TOKEN when we started saving
931      tokens.  */
932   delta = VARRAY_TOP_INT(lexer->saved_tokens);
933   /* Make it the next token again now.  */
934   lexer->next_token = cp_lexer_advance_token (lexer,
935                                               lexer->first_token, 
936                                               delta);
937   /* It might be the case that there wer no tokens when we started
938      saving tokens, but that there are some tokens now.  */
939   if (!lexer->next_token && lexer->first_token)
940     lexer->next_token = lexer->first_token;
941
942   /* Stop saving tokens.  */
943   VARRAY_POP (lexer->saved_tokens);
944 }
945
946 /* Print a representation of the TOKEN on the STREAM.  */
947
948 static void
949 cp_lexer_print_token (stream, token)
950      FILE *stream;
951      cp_token *token;
952 {
953   const char *token_type = NULL;
954
955   /* Figure out what kind of token this is.  */
956   switch (token->type)
957     {
958     case CPP_EQ:
959       token_type = "EQ";
960       break;
961
962     case CPP_COMMA:
963       token_type = "COMMA";
964       break;
965
966     case CPP_OPEN_PAREN:
967       token_type = "OPEN_PAREN";
968       break;
969
970     case CPP_CLOSE_PAREN:
971       token_type = "CLOSE_PAREN";
972       break;
973
974     case CPP_OPEN_BRACE:
975       token_type = "OPEN_BRACE";
976       break;
977
978     case CPP_CLOSE_BRACE:
979       token_type = "CLOSE_BRACE";
980       break;
981
982     case CPP_SEMICOLON:
983       token_type = "SEMICOLON";
984       break;
985
986     case CPP_NAME:
987       token_type = "NAME";
988       break;
989
990     case CPP_EOF:
991       token_type = "EOF";
992       break;
993
994     case CPP_KEYWORD:
995       token_type = "keyword";
996       break;
997
998       /* This is not a token that we know how to handle yet.  */
999     default:
1000       break;
1001     }
1002
1003   /* If we have a name for the token, print it out.  Otherwise, we
1004      simply give the numeric code.  */
1005   if (token_type)
1006     fprintf (stream, "%s", token_type);
1007   else
1008     fprintf (stream, "%d", token->type);
1009   /* And, for an identifier, print the identifier name.  */
1010   if (token->type == CPP_NAME 
1011       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1012          For example, `struct' is mapped to an INTEGER_CST.  */
1013       || (token->type == CPP_KEYWORD 
1014           && TREE_CODE (token->value) == IDENTIFIER_NODE))
1015     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1016 }
1017
1018 /* Start emitting debugging information.  */
1019
1020 static void
1021 cp_lexer_start_debugging (lexer)
1022      cp_lexer *lexer;
1023 {
1024   ++lexer->debugging_p;
1025 }
1026   
1027 /* Stop emitting debugging information.  */
1028
1029 static void
1030 cp_lexer_stop_debugging (lexer)
1031      cp_lexer *lexer;
1032 {
1033   --lexer->debugging_p;
1034 }
1035
1036 \f
1037 /* The parser.  */
1038
1039 /* Overview
1040    --------
1041
1042    A cp_parser parses the token stream as specified by the C++
1043    grammar.  Its job is purely parsing, not semantic analysis.  For
1044    example, the parser breaks the token stream into declarators,
1045    expressions, statements, and other similar syntactic constructs.
1046    It does not check that the types of the expressions on either side
1047    of an assignment-statement are compatible, or that a function is
1048    not declared with a parameter of type `void'.
1049
1050    The parser invokes routines elsewhere in the compiler to perform
1051    semantic analysis and to build up the abstract syntax tree for the
1052    code processed.  
1053
1054    The parser (and the template instantiation code, which is, in a
1055    way, a close relative of parsing) are the only parts of the
1056    compiler that should be calling push_scope and pop_scope, or
1057    related functions.  The parser (and template instantiation code)
1058    keeps track of what scope is presently active; everything else
1059    should simply honor that.  (The code that generates static
1060    initializers may also need to set the scope, in order to check
1061    access control correctly when emitting the initializers.)
1062
1063    Methodology
1064    -----------
1065    
1066    The parser is of the standard recursive-descent variety.  Upcoming
1067    tokens in the token stream are examined in order to determine which
1068    production to use when parsing a non-terminal.  Some C++ constructs
1069    require arbitrary look ahead to disambiguate.  For example, it is
1070    impossible, in the general case, to tell whether a statement is an
1071    expression or declaration without scanning the entire statement.
1072    Therefore, the parser is capable of "parsing tentatively."  When the
1073    parser is not sure what construct comes next, it enters this mode.
1074    Then, while we attempt to parse the construct, the parser queues up
1075    error messages, rather than issuing them immediately, and saves the
1076    tokens it consumes.  If the construct is parsed successfully, the
1077    parser "commits", i.e., it issues any queued error messages and
1078    the tokens that were being preserved are permanently discarded.
1079    If, however, the construct is not parsed successfully, the parser
1080    rolls back its state completely so that it can resume parsing using
1081    a different alternative.
1082
1083    Future Improvements
1084    -------------------
1085    
1086    The performance of the parser could probably be improved
1087    substantially.  Some possible improvements include:
1088
1089      - The expression parser recurses through the various levels of
1090        precedence as specified in the grammar, rather than using an
1091        operator-precedence technique.  Therefore, parsing a simple
1092        identifier requires multiple recursive calls.
1093
1094      - We could often eliminate the need to parse tentatively by
1095        looking ahead a little bit.  In some places, this approach
1096        might not entirely eliminate the need to parse tentatively, but
1097        it might still speed up the average case.  */
1098
1099 /* Flags that are passed to some parsing functions.  These values can
1100    be bitwise-ored together.  */
1101
1102 typedef enum cp_parser_flags
1103 {
1104   /* No flags.  */
1105   CP_PARSER_FLAGS_NONE = 0x0,
1106   /* The construct is optional.  If it is not present, then no error
1107      should be issued.  */
1108   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1109   /* When parsing a type-specifier, do not allow user-defined types.  */
1110   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1111 } cp_parser_flags;
1112
1113 /* The different kinds of ids that we ecounter.  */
1114
1115 typedef enum cp_parser_id_kind
1116 {
1117   /* Not an id at all.  */
1118   CP_PARSER_ID_KIND_NONE,
1119   /* An unqualified-id that is not a template-id.  */
1120   CP_PARSER_ID_KIND_UNQUALIFIED,
1121   /* An unqualified template-id.  */
1122   CP_PARSER_ID_KIND_TEMPLATE_ID,
1123   /* A qualified-id.  */
1124   CP_PARSER_ID_KIND_QUALIFIED
1125 } cp_parser_id_kind;
1126
1127 /* The different kinds of declarators we want to parse.  */
1128
1129 typedef enum cp_parser_declarator_kind
1130 {
1131   /* We want an abstract declartor. */
1132   CP_PARSER_DECLARATOR_ABSTRACT,
1133   /* We want a named declarator.  */
1134   CP_PARSER_DECLARATOR_NAMED,
1135   /* We don't mind.  */
1136   CP_PARSER_DECLARATOR_EITHER
1137 } cp_parser_declarator_kind;
1138
1139 /* A mapping from a token type to a corresponding tree node type.  */
1140
1141 typedef struct cp_parser_token_tree_map_node
1142 {
1143   /* The token type.  */
1144   enum cpp_ttype token_type;
1145   /* The corresponding tree code.  */
1146   enum tree_code tree_type;
1147 } cp_parser_token_tree_map_node;
1148
1149 /* A complete map consists of several ordinary entries, followed by a
1150    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1151
1152 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1153
1154 /* The status of a tentative parse.  */
1155
1156 typedef enum cp_parser_status_kind
1157 {
1158   /* No errors have occurred.  */
1159   CP_PARSER_STATUS_KIND_NO_ERROR,
1160   /* An error has occurred.  */
1161   CP_PARSER_STATUS_KIND_ERROR,
1162   /* We are committed to this tentative parse, whether or not an error
1163      has occurred.  */
1164   CP_PARSER_STATUS_KIND_COMMITTED
1165 } cp_parser_status_kind;
1166
1167 /* Context that is saved and restored when parsing tentatively.  */
1168
1169 typedef struct cp_parser_context GTY (())
1170 {
1171   /* If this is a tentative parsing context, the status of the
1172      tentative parse.  */
1173   enum cp_parser_status_kind status;
1174   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1175      that are looked up in this context must be looked up both in the
1176      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1177      the context of the containing expression.  */
1178   tree object_type;
1179   /* A TREE_LIST representing name-lookups for which we have deferred
1180      checking access controls.  We cannot check the accessibility of
1181      names used in a decl-specifier-seq until we know what is being
1182      declared because code like:
1183
1184        class A { 
1185          class B {};
1186          B* f();
1187        }
1188
1189        A::B* A::f() { return 0; }
1190
1191      is valid, even though `A::B' is not generally accessible.  
1192
1193      The TREE_PURPOSE of each node is the scope used to qualify the
1194      name being looked up; the TREE_VALUE is the DECL to which the
1195      name was resolved.  */
1196   tree deferred_access_checks;
1197   /* TRUE iff we are deferring access checks.  */
1198   bool deferring_access_checks_p;
1199   /* The next parsing context in the stack.  */
1200   struct cp_parser_context *next;
1201 } cp_parser_context;
1202
1203 /* Prototypes.  */
1204
1205 /* Constructors and destructors.  */
1206
1207 static cp_parser_context *cp_parser_context_new
1208   PARAMS ((cp_parser_context *));
1209
1210 /* Class variables.  */
1211
1212 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1213
1214 /* Constructors and destructors.  */
1215
1216 /* Construct a new context.  The context below this one on the stack
1217    is given by NEXT.  */
1218
1219 static cp_parser_context *
1220 cp_parser_context_new (next)
1221      cp_parser_context *next;
1222 {
1223   cp_parser_context *context;
1224
1225   /* Allocate the storage.  */
1226   if (cp_parser_context_free_list != NULL)
1227     {
1228       /* Pull the first entry from the free list.  */
1229       context = cp_parser_context_free_list;
1230       cp_parser_context_free_list = context->next;
1231       memset ((char *)context, 0, sizeof (*context));
1232     }
1233   else
1234     context = ((cp_parser_context *) 
1235                ggc_alloc_cleared (sizeof (cp_parser_context)));
1236   /* No errors have occurred yet in this context.  */
1237   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1238   /* If this is not the bottomost context, copy information that we
1239      need from the previous context.  */
1240   if (next)
1241     {
1242       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1243          expression, then we are parsing one in this context, too.  */
1244       context->object_type = next->object_type;
1245       /* We are deferring access checks here if we were in the NEXT
1246          context.  */
1247       context->deferring_access_checks_p 
1248         = next->deferring_access_checks_p;
1249       /* Thread the stack.  */
1250       context->next = next;
1251     }
1252
1253   return context;
1254 }
1255
1256 /* The cp_parser structure represents the C++ parser.  */
1257
1258 typedef struct cp_parser GTY(())
1259 {
1260   /* The lexer from which we are obtaining tokens.  */
1261   cp_lexer *lexer;
1262
1263   /* The scope in which names should be looked up.  If NULL_TREE, then
1264      we look up names in the scope that is currently open in the
1265      source program.  If non-NULL, this is either a TYPE or
1266      NAMESPACE_DECL for the scope in which we should look.  
1267
1268      This value is not cleared automatically after a name is looked
1269      up, so we must be careful to clear it before starting a new look
1270      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1271      will look up `Z' in the scope of `X', rather than the current
1272      scope.)  Unfortunately, it is difficult to tell when name lookup
1273      is complete, because we sometimes peek at a token, look it up,
1274      and then decide not to consume it.  */
1275   tree scope;
1276
1277   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1278      last lookup took place.  OBJECT_SCOPE is used if an expression
1279      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1280      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1281      form "X::Y"; it refers to X.  */
1282   tree object_scope;
1283   tree qualifying_scope;
1284
1285   /* A stack of parsing contexts.  All but the bottom entry on the
1286      stack will be tentative contexts.
1287
1288      We parse tentatively in order to determine which construct is in
1289      use in some situations.  For example, in order to determine
1290      whether a statement is an expression-statement or a
1291      declaration-statement we parse it tentatively as a
1292      declaration-statement.  If that fails, we then reparse the same
1293      token stream as an expression-statement.  */
1294   cp_parser_context *context;
1295
1296   /* True if we are parsing GNU C++.  If this flag is not set, then
1297      GNU extensions are not recognized.  */
1298   bool allow_gnu_extensions_p;
1299
1300   /* TRUE if the `>' token should be interpreted as the greater-than
1301      operator.  FALSE if it is the end of a template-id or
1302      template-parameter-list.  */
1303   bool greater_than_is_operator_p;
1304
1305   /* TRUE if default arguments are allowed within a parameter list
1306      that starts at this point. FALSE if only a gnu extension makes
1307      them permissable.  */
1308   bool default_arg_ok_p;
1309   
1310   /* TRUE if we are parsing an integral constant-expression.  See
1311      [expr.const] for a precise definition.  */
1312   /* FIXME: Need to implement code that checks this flag.  */
1313   bool constant_expression_p;
1314
1315   /* TRUE if local variable names and `this' are forbidden in the
1316      current context.  */
1317   bool local_variables_forbidden_p;
1318
1319   /* TRUE if the declaration we are parsing is part of a
1320      linkage-specification of the form `extern string-literal
1321      declaration'.  */
1322   bool in_unbraced_linkage_specification_p;
1323
1324   /* TRUE if we are presently parsing a declarator, after the
1325      direct-declarator.  */
1326   bool in_declarator_p;
1327
1328   /* If non-NULL, then we are parsing a construct where new type
1329      definitions are not permitted.  The string stored here will be
1330      issued as an error message if a type is defined.  */
1331   const char *type_definition_forbidden_message;
1332
1333   /* A TREE_LIST of queues of functions whose bodies have been lexed,
1334      but may not have been parsed.  These functions are friends of
1335      members defined within a class-specification; they are not
1336      procssed until the class is complete.  The active queue is at the
1337      front of the list.
1338
1339      Within each queue, functions appear in the reverse order that
1340      they appeared in the source.  Each TREE_VALUE is a
1341      FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1342      function.  */
1343   tree unparsed_functions_queues;
1344
1345   /* The number of classes whose definitions are currently in
1346      progress.  */
1347   unsigned num_classes_being_defined;
1348
1349   /* The number of template parameter lists that apply directly to the
1350      current declaration.  */
1351   unsigned num_template_parameter_lists;
1352
1353   /* List of access checks lists, used to prevent GC collection while
1354      they are in use.  */
1355   tree access_checks_lists;
1356 } cp_parser;
1357
1358 /* The type of a function that parses some kind of expression  */
1359 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1360
1361 /* Prototypes.  */
1362
1363 /* Constructors and destructors.  */
1364
1365 static cp_parser *cp_parser_new
1366   PARAMS ((void));
1367
1368 /* Routines to parse various constructs.  
1369
1370    Those that return `tree' will return the error_mark_node (rather
1371    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1372    Sometimes, they will return an ordinary node if error-recovery was
1373    attempted, even though a parse error occurrred.  So, to check
1374    whether or not a parse error occurred, you should always use
1375    cp_parser_error_occurred.  If the construct is optional (indicated
1376    either by an `_opt' in the name of the function that does the
1377    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1378    the construct is not present.  */
1379
1380 /* Lexical conventions [gram.lex]  */
1381
1382 static tree cp_parser_identifier
1383   PARAMS ((cp_parser *));
1384
1385 /* Basic concepts [gram.basic]  */
1386
1387 static bool cp_parser_translation_unit
1388   PARAMS ((cp_parser *));
1389
1390 /* Expressions [gram.expr]  */
1391
1392 static tree cp_parser_primary_expression
1393   (cp_parser *, cp_parser_id_kind *, tree *);
1394 static tree cp_parser_id_expression
1395   PARAMS ((cp_parser *, bool, bool, bool *));
1396 static tree cp_parser_unqualified_id
1397   PARAMS ((cp_parser *, bool, bool));
1398 static tree cp_parser_nested_name_specifier_opt
1399   (cp_parser *, bool, bool, bool);
1400 static tree cp_parser_nested_name_specifier
1401   (cp_parser *, bool, bool, bool);
1402 static tree cp_parser_class_or_namespace_name
1403   (cp_parser *, bool, bool, bool, bool);
1404 static tree cp_parser_postfix_expression
1405   (cp_parser *, bool);
1406 static tree cp_parser_expression_list
1407   PARAMS ((cp_parser *));
1408 static void cp_parser_pseudo_destructor_name
1409   PARAMS ((cp_parser *, tree *, tree *));
1410 static tree cp_parser_unary_expression
1411   (cp_parser *, bool);
1412 static enum tree_code cp_parser_unary_operator
1413   PARAMS ((cp_token *));
1414 static tree cp_parser_new_expression
1415   PARAMS ((cp_parser *));
1416 static tree cp_parser_new_placement
1417   PARAMS ((cp_parser *));
1418 static tree cp_parser_new_type_id
1419   PARAMS ((cp_parser *));
1420 static tree cp_parser_new_declarator_opt
1421   PARAMS ((cp_parser *));
1422 static tree cp_parser_direct_new_declarator
1423   PARAMS ((cp_parser *));
1424 static tree cp_parser_new_initializer
1425   PARAMS ((cp_parser *));
1426 static tree cp_parser_delete_expression
1427   PARAMS ((cp_parser *));
1428 static tree cp_parser_cast_expression 
1429   (cp_parser *, bool);
1430 static tree cp_parser_pm_expression
1431   PARAMS ((cp_parser *));
1432 static tree cp_parser_multiplicative_expression
1433   PARAMS ((cp_parser *));
1434 static tree cp_parser_additive_expression
1435   PARAMS ((cp_parser *));
1436 static tree cp_parser_shift_expression
1437   PARAMS ((cp_parser *));
1438 static tree cp_parser_relational_expression
1439   PARAMS ((cp_parser *));
1440 static tree cp_parser_equality_expression
1441   PARAMS ((cp_parser *));
1442 static tree cp_parser_and_expression
1443   PARAMS ((cp_parser *));
1444 static tree cp_parser_exclusive_or_expression
1445   PARAMS ((cp_parser *));
1446 static tree cp_parser_inclusive_or_expression
1447   PARAMS ((cp_parser *));
1448 static tree cp_parser_logical_and_expression
1449   PARAMS ((cp_parser *));
1450 static tree cp_parser_logical_or_expression 
1451   PARAMS ((cp_parser *));
1452 static tree cp_parser_conditional_expression
1453   PARAMS ((cp_parser *));
1454 static tree cp_parser_question_colon_clause
1455   PARAMS ((cp_parser *, tree));
1456 static tree cp_parser_assignment_expression
1457   PARAMS ((cp_parser *));
1458 static enum tree_code cp_parser_assignment_operator_opt
1459   PARAMS ((cp_parser *));
1460 static tree cp_parser_expression
1461   PARAMS ((cp_parser *));
1462 static tree cp_parser_constant_expression
1463   PARAMS ((cp_parser *));
1464
1465 /* Statements [gram.stmt.stmt]  */
1466
1467 static void cp_parser_statement
1468   PARAMS ((cp_parser *));
1469 static tree cp_parser_labeled_statement
1470   PARAMS ((cp_parser *));
1471 static tree cp_parser_expression_statement
1472   PARAMS ((cp_parser *));
1473 static tree cp_parser_compound_statement
1474   (cp_parser *);
1475 static void cp_parser_statement_seq_opt
1476   PARAMS ((cp_parser *));
1477 static tree cp_parser_selection_statement
1478   PARAMS ((cp_parser *));
1479 static tree cp_parser_condition
1480   PARAMS ((cp_parser *));
1481 static tree cp_parser_iteration_statement
1482   PARAMS ((cp_parser *));
1483 static void cp_parser_for_init_statement
1484   PARAMS ((cp_parser *));
1485 static tree cp_parser_jump_statement
1486   PARAMS ((cp_parser *));
1487 static void cp_parser_declaration_statement
1488   PARAMS ((cp_parser *));
1489
1490 static tree cp_parser_implicitly_scoped_statement
1491   PARAMS ((cp_parser *));
1492 static void cp_parser_already_scoped_statement
1493   PARAMS ((cp_parser *));
1494
1495 /* Declarations [gram.dcl.dcl] */
1496
1497 static void cp_parser_declaration_seq_opt
1498   PARAMS ((cp_parser *));
1499 static void cp_parser_declaration
1500   PARAMS ((cp_parser *));
1501 static void cp_parser_block_declaration
1502   PARAMS ((cp_parser *, bool));
1503 static void cp_parser_simple_declaration
1504   PARAMS ((cp_parser *, bool));
1505 static tree cp_parser_decl_specifier_seq 
1506   PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1507 static tree cp_parser_storage_class_specifier_opt
1508   PARAMS ((cp_parser *));
1509 static tree cp_parser_function_specifier_opt
1510   PARAMS ((cp_parser *));
1511 static tree cp_parser_type_specifier
1512  (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1513 static tree cp_parser_simple_type_specifier
1514   PARAMS ((cp_parser *, cp_parser_flags));
1515 static tree cp_parser_type_name
1516   PARAMS ((cp_parser *));
1517 static tree cp_parser_elaborated_type_specifier
1518   PARAMS ((cp_parser *, bool, bool));
1519 static tree cp_parser_enum_specifier
1520   PARAMS ((cp_parser *));
1521 static void cp_parser_enumerator_list
1522   PARAMS ((cp_parser *, tree));
1523 static void cp_parser_enumerator_definition 
1524   PARAMS ((cp_parser *, tree));
1525 static tree cp_parser_namespace_name
1526   PARAMS ((cp_parser *));
1527 static void cp_parser_namespace_definition
1528   PARAMS ((cp_parser *));
1529 static void cp_parser_namespace_body
1530   PARAMS ((cp_parser *));
1531 static tree cp_parser_qualified_namespace_specifier
1532   PARAMS ((cp_parser *));
1533 static void cp_parser_namespace_alias_definition
1534   PARAMS ((cp_parser *));
1535 static void cp_parser_using_declaration
1536   PARAMS ((cp_parser *));
1537 static void cp_parser_using_directive
1538   PARAMS ((cp_parser *));
1539 static void cp_parser_asm_definition
1540   PARAMS ((cp_parser *));
1541 static void cp_parser_linkage_specification
1542   PARAMS ((cp_parser *));
1543
1544 /* Declarators [gram.dcl.decl] */
1545
1546 static tree cp_parser_init_declarator
1547   PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1548 static tree cp_parser_declarator
1549   PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1550 static tree cp_parser_direct_declarator
1551   PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1552 static enum tree_code cp_parser_ptr_operator
1553   PARAMS ((cp_parser *, tree *, tree *));
1554 static tree cp_parser_cv_qualifier_seq_opt
1555   PARAMS ((cp_parser *));
1556 static tree cp_parser_cv_qualifier_opt
1557   PARAMS ((cp_parser *));
1558 static tree cp_parser_declarator_id
1559   PARAMS ((cp_parser *));
1560 static tree cp_parser_type_id
1561   PARAMS ((cp_parser *));
1562 static tree cp_parser_type_specifier_seq
1563   PARAMS ((cp_parser *));
1564 static tree cp_parser_parameter_declaration_clause
1565   PARAMS ((cp_parser *));
1566 static tree cp_parser_parameter_declaration_list
1567   PARAMS ((cp_parser *));
1568 static tree cp_parser_parameter_declaration
1569   PARAMS ((cp_parser *, bool));
1570 static tree cp_parser_function_definition
1571   PARAMS ((cp_parser *, bool *));
1572 static void cp_parser_function_body
1573   (cp_parser *);
1574 static tree cp_parser_initializer
1575   PARAMS ((cp_parser *, bool *));
1576 static tree cp_parser_initializer_clause
1577   PARAMS ((cp_parser *));
1578 static tree cp_parser_initializer_list
1579   PARAMS ((cp_parser *));
1580
1581 static bool cp_parser_ctor_initializer_opt_and_function_body
1582   (cp_parser *);
1583
1584 /* Classes [gram.class] */
1585
1586 static tree cp_parser_class_name
1587   (cp_parser *, bool, bool, bool, bool, bool, bool);
1588 static tree cp_parser_class_specifier
1589   PARAMS ((cp_parser *));
1590 static tree cp_parser_class_head
1591   PARAMS ((cp_parser *, bool *, bool *, tree *));
1592 static enum tag_types cp_parser_class_key
1593   PARAMS ((cp_parser *));
1594 static void cp_parser_member_specification_opt
1595   PARAMS ((cp_parser *));
1596 static void cp_parser_member_declaration
1597   PARAMS ((cp_parser *));
1598 static tree cp_parser_pure_specifier
1599   PARAMS ((cp_parser *));
1600 static tree cp_parser_constant_initializer
1601   PARAMS ((cp_parser *));
1602
1603 /* Derived classes [gram.class.derived] */
1604
1605 static tree cp_parser_base_clause
1606   PARAMS ((cp_parser *));
1607 static tree cp_parser_base_specifier
1608   PARAMS ((cp_parser *));
1609
1610 /* Special member functions [gram.special] */
1611
1612 static tree cp_parser_conversion_function_id
1613   PARAMS ((cp_parser *));
1614 static tree cp_parser_conversion_type_id
1615   PARAMS ((cp_parser *));
1616 static tree cp_parser_conversion_declarator_opt
1617   PARAMS ((cp_parser *));
1618 static bool cp_parser_ctor_initializer_opt
1619   PARAMS ((cp_parser *));
1620 static void cp_parser_mem_initializer_list
1621   PARAMS ((cp_parser *));
1622 static tree cp_parser_mem_initializer
1623   PARAMS ((cp_parser *));
1624 static tree cp_parser_mem_initializer_id
1625   PARAMS ((cp_parser *));
1626
1627 /* Overloading [gram.over] */
1628
1629 static tree cp_parser_operator_function_id
1630   PARAMS ((cp_parser *));
1631 static tree cp_parser_operator
1632   PARAMS ((cp_parser *));
1633
1634 /* Templates [gram.temp] */
1635
1636 static void cp_parser_template_declaration
1637   PARAMS ((cp_parser *, bool));
1638 static tree cp_parser_template_parameter_list
1639   PARAMS ((cp_parser *));
1640 static tree cp_parser_template_parameter
1641   PARAMS ((cp_parser *));
1642 static tree cp_parser_type_parameter
1643   PARAMS ((cp_parser *));
1644 static tree cp_parser_template_id
1645   PARAMS ((cp_parser *, bool, bool));
1646 static tree cp_parser_template_name
1647   PARAMS ((cp_parser *, bool, bool));
1648 static tree cp_parser_template_argument_list
1649   PARAMS ((cp_parser *));
1650 static tree cp_parser_template_argument
1651   PARAMS ((cp_parser *));
1652 static void cp_parser_explicit_instantiation
1653   PARAMS ((cp_parser *));
1654 static void cp_parser_explicit_specialization
1655   PARAMS ((cp_parser *));
1656
1657 /* Exception handling [gram.exception] */
1658
1659 static tree cp_parser_try_block 
1660   PARAMS ((cp_parser *));
1661 static bool cp_parser_function_try_block
1662   PARAMS ((cp_parser *));
1663 static void cp_parser_handler_seq
1664   PARAMS ((cp_parser *));
1665 static void cp_parser_handler
1666   PARAMS ((cp_parser *));
1667 static tree cp_parser_exception_declaration
1668   PARAMS ((cp_parser *));
1669 static tree cp_parser_throw_expression
1670   PARAMS ((cp_parser *));
1671 static tree cp_parser_exception_specification_opt
1672   PARAMS ((cp_parser *));
1673 static tree cp_parser_type_id_list
1674   PARAMS ((cp_parser *));
1675
1676 /* GNU Extensions */
1677
1678 static tree cp_parser_asm_specification_opt
1679   PARAMS ((cp_parser *));
1680 static tree cp_parser_asm_operand_list
1681   PARAMS ((cp_parser *));
1682 static tree cp_parser_asm_clobber_list
1683   PARAMS ((cp_parser *));
1684 static tree cp_parser_attributes_opt
1685   PARAMS ((cp_parser *));
1686 static tree cp_parser_attribute_list
1687   PARAMS ((cp_parser *));
1688 static bool cp_parser_extension_opt
1689   PARAMS ((cp_parser *, int *));
1690 static void cp_parser_label_declaration
1691   PARAMS ((cp_parser *));
1692
1693 /* Utility Routines */
1694
1695 static tree cp_parser_lookup_name
1696   PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1697 static tree cp_parser_lookup_name_simple
1698   PARAMS ((cp_parser *, tree));
1699 static tree cp_parser_resolve_typename_type
1700   PARAMS ((cp_parser *, tree));
1701 static tree cp_parser_maybe_treat_template_as_class
1702   (tree, bool);
1703 static bool cp_parser_check_declarator_template_parameters
1704   PARAMS ((cp_parser *, tree));
1705 static bool cp_parser_check_template_parameters
1706   PARAMS ((cp_parser *, unsigned));
1707 static tree cp_parser_binary_expression
1708   PARAMS ((cp_parser *, 
1709            const cp_parser_token_tree_map,
1710            cp_parser_expression_fn));
1711 static tree cp_parser_global_scope_opt
1712   PARAMS ((cp_parser *, bool));
1713 static bool cp_parser_constructor_declarator_p
1714   (cp_parser *, bool);
1715 static tree cp_parser_function_definition_from_specifiers_and_declarator
1716   PARAMS ((cp_parser *, tree, tree, tree, tree));
1717 static tree cp_parser_function_definition_after_declarator
1718   PARAMS ((cp_parser *, bool));
1719 static void cp_parser_template_declaration_after_export
1720   PARAMS ((cp_parser *, bool));
1721 static tree cp_parser_single_declaration
1722   PARAMS ((cp_parser *, bool, bool *));
1723 static tree cp_parser_functional_cast
1724   PARAMS ((cp_parser *, tree));
1725 static void cp_parser_late_parsing_for_member
1726   PARAMS ((cp_parser *, tree));
1727 static void cp_parser_late_parsing_default_args
1728   (cp_parser *, tree);
1729 static tree cp_parser_sizeof_operand
1730   PARAMS ((cp_parser *, enum rid));
1731 static bool cp_parser_declares_only_class_p
1732   PARAMS ((cp_parser *));
1733 static bool cp_parser_friend_p
1734   PARAMS ((tree));
1735 static cp_token *cp_parser_require
1736   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1737 static cp_token *cp_parser_require_keyword
1738   PARAMS ((cp_parser *, enum rid, const char *));
1739 static bool cp_parser_token_starts_function_definition_p 
1740   PARAMS ((cp_token *));
1741 static bool cp_parser_next_token_starts_class_definition_p
1742   (cp_parser *);
1743 static enum tag_types cp_parser_token_is_class_key
1744   PARAMS ((cp_token *));
1745 static void cp_parser_check_class_key
1746   (enum tag_types, tree type);
1747 static bool cp_parser_optional_template_keyword
1748   (cp_parser *);
1749 static void cp_parser_cache_group
1750   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1751 static void cp_parser_parse_tentatively 
1752   PARAMS ((cp_parser *));
1753 static void cp_parser_commit_to_tentative_parse
1754   PARAMS ((cp_parser *));
1755 static void cp_parser_abort_tentative_parse
1756   PARAMS ((cp_parser *));
1757 static bool cp_parser_parse_definitely
1758   PARAMS ((cp_parser *));
1759 static inline bool cp_parser_parsing_tentatively
1760   PARAMS ((cp_parser *));
1761 static bool cp_parser_committed_to_tentative_parse
1762   PARAMS ((cp_parser *));
1763 static void cp_parser_error
1764   PARAMS ((cp_parser *, const char *));
1765 static bool cp_parser_simulate_error
1766   PARAMS ((cp_parser *));
1767 static void cp_parser_check_type_definition
1768   PARAMS ((cp_parser *));
1769 static bool cp_parser_skip_to_closing_parenthesis
1770   PARAMS ((cp_parser *));
1771 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1772   (cp_parser *);
1773 static void cp_parser_skip_to_end_of_statement
1774   PARAMS ((cp_parser *));
1775 static void cp_parser_skip_to_end_of_block_or_statement
1776   PARAMS ((cp_parser *));
1777 static void cp_parser_skip_to_closing_brace
1778   (cp_parser *);
1779 static void cp_parser_skip_until_found
1780   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1781 static bool cp_parser_error_occurred
1782   PARAMS ((cp_parser *));
1783 static bool cp_parser_allow_gnu_extensions_p
1784   PARAMS ((cp_parser *));
1785 static bool cp_parser_is_string_literal
1786   PARAMS ((cp_token *));
1787 static bool cp_parser_is_keyword 
1788   PARAMS ((cp_token *, enum rid));
1789 static bool cp_parser_dependent_type_p
1790   (tree);
1791 static bool cp_parser_value_dependent_expression_p
1792   (tree);
1793 static bool cp_parser_type_dependent_expression_p
1794   (tree);
1795 static bool cp_parser_dependent_template_arg_p
1796   (tree);
1797 static bool cp_parser_dependent_template_id_p
1798   (tree, tree);
1799 static bool cp_parser_dependent_template_p
1800   (tree);
1801 static void cp_parser_defer_access_check
1802   (cp_parser *, tree, tree);
1803 static void cp_parser_start_deferring_access_checks
1804   (cp_parser *);
1805 static tree cp_parser_stop_deferring_access_checks
1806   PARAMS ((cp_parser *));
1807 static void cp_parser_perform_deferred_access_checks
1808   PARAMS ((tree));
1809 static tree cp_parser_scope_through_which_access_occurs
1810   (tree, tree, tree);
1811
1812 /* Returns non-zero if we are parsing tentatively.  */
1813
1814 static inline bool
1815 cp_parser_parsing_tentatively (parser)
1816      cp_parser *parser;
1817 {
1818   return parser->context->next != NULL;
1819 }
1820
1821 /* Returns non-zero if TOKEN is a string literal.  */
1822
1823 static bool
1824 cp_parser_is_string_literal (token)
1825      cp_token *token;
1826 {
1827   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1828 }
1829
1830 /* Returns non-zero if TOKEN is the indicated KEYWORD.  */
1831
1832 static bool
1833 cp_parser_is_keyword (token, keyword)
1834      cp_token *token;
1835      enum rid keyword;
1836 {
1837   return token->keyword == keyword;
1838 }
1839
1840 /* Returns TRUE if TYPE is dependent, in the sense of
1841    [temp.dep.type].  */
1842
1843 static bool
1844 cp_parser_dependent_type_p (type)
1845      tree type;
1846 {
1847   tree scope;
1848
1849   if (!processing_template_decl)
1850     return false;
1851
1852   /* If the type is NULL, we have not computed a type for the entity
1853      in question; in that case, the type is dependent.  */
1854   if (!type)
1855     return true;
1856
1857   /* Erroneous types can be considered non-dependent.  */
1858   if (type == error_mark_node)
1859     return false;
1860
1861   /* [temp.dep.type]
1862
1863      A type is dependent if it is:
1864
1865      -- a template parameter.  */
1866   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1867     return true;
1868   /* -- a qualified-id with a nested-name-specifier which contains a
1869         class-name that names a dependent type or whose unqualified-id
1870         names a dependent type.  */
1871   if (TREE_CODE (type) == TYPENAME_TYPE)
1872     return true;
1873   /* -- a cv-qualified type where the cv-unqualified type is
1874         dependent.  */
1875   type = TYPE_MAIN_VARIANT (type);
1876   /* -- a compound type constructed from any dependent type.  */
1877   if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1878     return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1879             || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE 
1880                                            (type)));
1881   else if (TREE_CODE (type) == POINTER_TYPE
1882            || TREE_CODE (type) == REFERENCE_TYPE)
1883     return cp_parser_dependent_type_p (TREE_TYPE (type));
1884   else if (TREE_CODE (type) == FUNCTION_TYPE
1885            || TREE_CODE (type) == METHOD_TYPE)
1886     {
1887       tree arg_type;
1888
1889       if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1890         return true;
1891       for (arg_type = TYPE_ARG_TYPES (type); 
1892            arg_type; 
1893            arg_type = TREE_CHAIN (arg_type))
1894         if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1895           return true;
1896       return false;
1897     }
1898   /* -- an array type constructed from any dependent type or whose
1899         size is specified by a constant expression that is
1900         value-dependent.  */
1901   if (TREE_CODE (type) == ARRAY_TYPE)
1902     {
1903       if (TYPE_DOMAIN (type)
1904           && ((cp_parser_value_dependent_expression_p 
1905                (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1906               || (cp_parser_type_dependent_expression_p
1907                   (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1908         return true;
1909       return cp_parser_dependent_type_p (TREE_TYPE (type));
1910     }
1911   /* -- a template-id in which either the template name is a template
1912         parameter or any of the template arguments is a dependent type or
1913         an expression that is type-dependent or value-dependent.  
1914
1915      This language seems somewhat confused; for example, it does not
1916      discuss template template arguments.  Therefore, we use the
1917      definition for dependent template arguments in [temp.dep.temp].  */
1918   if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1919       && (cp_parser_dependent_template_id_p
1920           (CLASSTYPE_TI_TEMPLATE (type),
1921            CLASSTYPE_TI_ARGS (type))))
1922     return true;
1923   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1924     return true;
1925   /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1926      expression is not type-dependent, then it should already been
1927      have resolved.  */
1928   if (TREE_CODE (type) == TYPEOF_TYPE)
1929     return true;
1930   /* The standard does not specifically mention types that are local
1931      to template functions or local classes, but they should be
1932      considered dependent too.  For example:
1933
1934        template <int I> void f() { 
1935          enum E { a = I }; 
1936          S<sizeof (E)> s;
1937        }
1938
1939      The size of `E' cannot be known until the value of `I' has been
1940      determined.  Therefore, `E' must be considered dependent.  */
1941   scope = TYPE_CONTEXT (type);
1942   if (scope && TYPE_P (scope))
1943     return cp_parser_dependent_type_p (scope);
1944   else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1945     return cp_parser_type_dependent_expression_p (scope);
1946
1947   /* Other types are non-dependent.  */
1948   return false;
1949 }
1950
1951 /* Returns TRUE if the EXPRESSION is value-dependent.  */
1952
1953 static bool
1954 cp_parser_value_dependent_expression_p (tree expression)
1955 {
1956   if (!processing_template_decl)
1957     return false;
1958
1959   /* A name declared with a dependent type.  */
1960   if (DECL_P (expression)
1961       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1962     return true;
1963   /* A non-type template parameter.  */
1964   if ((TREE_CODE (expression) == CONST_DECL
1965        && DECL_TEMPLATE_PARM_P (expression))
1966       || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1967     return true;
1968   /* A constant with integral or enumeration type and is initialized 
1969      with an expression that is value-dependent.  */
1970   if (TREE_CODE (expression) == VAR_DECL
1971       && DECL_INITIAL (expression)
1972       && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1973           || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1974       && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1975     return true;
1976   /* These expressions are value-dependent if the type to which the
1977      cast occurs is dependent.  */
1978   if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1979        || TREE_CODE (expression) == STATIC_CAST_EXPR
1980        || TREE_CODE (expression) == CONST_CAST_EXPR
1981        || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1982        || TREE_CODE (expression) == CAST_EXPR)
1983       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1984     return true;
1985   /* A `sizeof' expression where the sizeof operand is a type is
1986      value-dependent if the type is dependent.  If the type was not
1987      dependent, we would no longer have a SIZEOF_EXPR, so any
1988      SIZEOF_EXPR is dependent.  */
1989   if (TREE_CODE (expression) == SIZEOF_EXPR)
1990     return true;
1991   /* A constant expression is value-dependent if any subexpression is
1992      value-dependent.  */
1993   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1994     {
1995       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1996         {
1997         case '1':
1998           return (cp_parser_value_dependent_expression_p 
1999                   (TREE_OPERAND (expression, 0)));
2000         case '<':
2001         case '2':
2002           return ((cp_parser_value_dependent_expression_p 
2003                    (TREE_OPERAND (expression, 0)))
2004                   || (cp_parser_value_dependent_expression_p 
2005                       (TREE_OPERAND (expression, 1))));
2006         case 'e':
2007           {
2008             int i;
2009             for (i = 0; 
2010                  i < TREE_CODE_LENGTH (TREE_CODE (expression));
2011                  ++i)
2012               if (cp_parser_value_dependent_expression_p
2013                   (TREE_OPERAND (expression, i)))
2014                 return true;
2015             return false;
2016           }
2017         }
2018     }
2019
2020   /* The expression is not value-dependent.  */
2021   return false;
2022 }
2023
2024 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2025    [temp.dep.expr].  */
2026
2027 static bool
2028 cp_parser_type_dependent_expression_p (expression)
2029      tree expression;
2030 {
2031   if (!processing_template_decl)
2032     return false;
2033
2034   /* Some expression forms are never type-dependent.  */
2035   if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2036       || TREE_CODE (expression) == SIZEOF_EXPR
2037       || TREE_CODE (expression) == ALIGNOF_EXPR
2038       || TREE_CODE (expression) == TYPEID_EXPR
2039       || TREE_CODE (expression) == DELETE_EXPR
2040       || TREE_CODE (expression) == VEC_DELETE_EXPR
2041       || TREE_CODE (expression) == THROW_EXPR)
2042     return false;
2043
2044   /* The types of these expressions depends only on the type to which
2045      the cast occurs.  */
2046   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2047       || TREE_CODE (expression) == STATIC_CAST_EXPR
2048       || TREE_CODE (expression) == CONST_CAST_EXPR
2049       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2050       || TREE_CODE (expression) == CAST_EXPR)
2051     return cp_parser_dependent_type_p (TREE_TYPE (expression));
2052   /* The types of these expressions depends only on the type created
2053      by the expression.  */
2054   else if (TREE_CODE (expression) == NEW_EXPR
2055            || TREE_CODE (expression) == VEC_NEW_EXPR)
2056     return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2057
2058   if (TREE_CODE (expression) == FUNCTION_DECL
2059       && DECL_LANG_SPECIFIC (expression)
2060       && DECL_TEMPLATE_INFO (expression)
2061       && (cp_parser_dependent_template_id_p
2062           (DECL_TI_TEMPLATE (expression),
2063            INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2064     return true;
2065
2066   return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2067 }
2068
2069 /* Returns TRUE if the ARG (a template argument) is dependent.  */
2070
2071 static bool
2072 cp_parser_dependent_template_arg_p (tree arg)
2073 {
2074   if (!processing_template_decl)
2075     return false;
2076
2077   if (TREE_CODE (arg) == TEMPLATE_DECL
2078       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2079     return cp_parser_dependent_template_p (arg);
2080   else if (TYPE_P (arg))
2081     return cp_parser_dependent_type_p (arg);
2082   else
2083     return (cp_parser_type_dependent_expression_p (arg)
2084             || cp_parser_value_dependent_expression_p (arg));
2085 }
2086
2087 /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
2088
2089 static bool
2090 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2091 {
2092   int i;
2093
2094   if (cp_parser_dependent_template_p (tmpl))
2095     return true;
2096   for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2097     if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2098       return true;
2099   return false;
2100 }
2101
2102 /* Returns TRUE if the template TMPL is dependent.  */
2103
2104 static bool
2105 cp_parser_dependent_template_p (tree tmpl)
2106 {
2107   /* Template template parameters are dependent.  */
2108   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2109       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2110     return true;
2111   /* So are member templates of dependent classes.  */
2112   if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2113     return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2114   return false;
2115 }
2116
2117 /* Defer checking the accessibility of DECL, when looked up in
2118    CLASS_TYPE.  */
2119
2120 static void
2121 cp_parser_defer_access_check (cp_parser *parser, 
2122                               tree class_type,
2123                               tree decl)
2124 {
2125   tree check;
2126
2127   /* If we are not supposed to defer access checks, just check now.  */
2128   if (!parser->context->deferring_access_checks_p)
2129     {
2130       enforce_access (class_type, decl);
2131       return;
2132     }
2133
2134   /* See if we are already going to perform this check.  */
2135   for (check = parser->context->deferred_access_checks;
2136        check;
2137        check = TREE_CHAIN (check))
2138     if (TREE_VALUE (check) == decl
2139         && same_type_p (TREE_PURPOSE (check), class_type))
2140       return;
2141   /* If not, record the check.  */
2142   parser->context->deferred_access_checks
2143     = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2144 }
2145
2146 /* Start deferring access control checks.  */
2147
2148 static void
2149 cp_parser_start_deferring_access_checks (cp_parser *parser)
2150 {
2151   parser->context->deferring_access_checks_p = true;
2152 }
2153
2154 /* Stop deferring access control checks.  Returns a TREE_LIST
2155    representing the deferred checks.  The TREE_PURPOSE of each node is
2156    the type through which the access occurred; the TREE_VALUE is the
2157    declaration named.  */
2158
2159 static tree
2160 cp_parser_stop_deferring_access_checks (parser)
2161      cp_parser *parser;
2162 {
2163   tree access_checks;
2164
2165   parser->context->deferring_access_checks_p = false;
2166   access_checks = parser->context->deferred_access_checks;
2167   parser->context->deferred_access_checks = NULL_TREE;
2168
2169   return access_checks;
2170 }
2171
2172 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2173    documented with cp_parser_stop_deferrring_access_checks.  */
2174
2175 static void
2176 cp_parser_perform_deferred_access_checks (access_checks)
2177      tree access_checks;
2178 {
2179   tree deferred_check;
2180
2181   /* Look through all the deferred checks.  */
2182   for (deferred_check = access_checks;
2183        deferred_check;
2184        deferred_check = TREE_CHAIN (deferred_check))
2185     /* Check access.  */
2186     enforce_access (TREE_PURPOSE (deferred_check), 
2187                     TREE_VALUE (deferred_check));
2188 }
2189
2190 /* Returns the scope through which DECL is being accessed, or
2191    NULL_TREE if DECL is not a member.  If OBJECT_TYPE is non-NULL, we
2192    have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2193    or `x', respectively.  If the DECL was named as `A::B' then
2194    NESTED_NAME_SPECIFIER is `A'.  */
2195
2196 tree
2197 cp_parser_scope_through_which_access_occurs (decl, 
2198                                              object_type,
2199                                              nested_name_specifier)
2200      tree decl;
2201      tree object_type;
2202      tree nested_name_specifier;
2203 {
2204   tree scope;
2205   tree qualifying_type = NULL_TREE;
2206   
2207   /* Determine the SCOPE of DECL.  */
2208   scope = context_for_name_lookup (decl);
2209   /* If the SCOPE is not a type, then DECL is not a member.  */
2210   if (!TYPE_P (scope))
2211     return NULL_TREE;
2212   /* Figure out the type through which DECL is being accessed.  */
2213   if (object_type && DERIVED_FROM_P (scope, object_type))
2214     /* If we are processing a `->' or `.' expression, use the type of the
2215        left-hand side.  */
2216     qualifying_type = object_type;
2217   else if (nested_name_specifier)
2218     {
2219       /* If the reference is to a non-static member of the
2220          current class, treat it as if it were referenced through
2221          `this'.  */
2222       if (DECL_NONSTATIC_MEMBER_P (decl)
2223           && current_class_ptr
2224           && DERIVED_FROM_P (scope, current_class_type))
2225         qualifying_type = current_class_type;
2226       /* Otherwise, use the type indicated by the
2227          nested-name-specifier.  */
2228       else
2229         qualifying_type = nested_name_specifier;
2230     }
2231   else
2232     /* Otherwise, the name must be from the current class or one of
2233        its bases.  */
2234     qualifying_type = currently_open_derived_class (scope);
2235
2236   return qualifying_type;
2237 }
2238
2239 /* Issue the indicated error MESSAGE.  */
2240
2241 static void
2242 cp_parser_error (parser, message)
2243      cp_parser *parser;
2244      const char *message;
2245 {
2246   /* Output the MESSAGE -- unless we're parsing tentatively.  */
2247   if (!cp_parser_simulate_error (parser))
2248     error (message);
2249 }
2250
2251 /* If we are parsing tentatively, remember that an error has occurred
2252    during this tentative parse.  Returns true if the error was
2253    simulated; false if a messgae should be issued by the caller.  */
2254
2255 static bool
2256 cp_parser_simulate_error (parser)
2257      cp_parser *parser;
2258 {
2259   if (cp_parser_parsing_tentatively (parser)
2260       && !cp_parser_committed_to_tentative_parse (parser))
2261     {
2262       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2263       return true;
2264     }
2265   return false;
2266 }
2267
2268 /* This function is called when a type is defined.  If type
2269    definitions are forbidden at this point, an error message is
2270    issued.  */
2271
2272 static void
2273 cp_parser_check_type_definition (parser)
2274      cp_parser *parser;
2275 {
2276   /* If types are forbidden here, issue a message.  */
2277   if (parser->type_definition_forbidden_message)
2278     /* Use `%s' to print the string in case there are any escape
2279        characters in the message.  */
2280     error ("%s", parser->type_definition_forbidden_message);
2281 }
2282
2283 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2284    Returns TRUE iff we found a closing `)'.  */
2285
2286 static bool
2287 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2288 {
2289   unsigned nesting_depth = 0;
2290
2291   while (true)
2292     {
2293       cp_token *token;
2294
2295       /* If we've run out of tokens, then there is no closing `)'.  */
2296       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2297         return false;
2298       /* Consume the token.  */
2299       token = cp_lexer_consume_token (parser->lexer);
2300       /* If it is an `(', we have entered another level of nesting.  */
2301       if (token->type == CPP_OPEN_PAREN)
2302         ++nesting_depth;
2303       /* If it is a `)', then we might be done.  */
2304       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2305         return true;
2306     }
2307 }
2308
2309 /* Consume tokens until the next token is a `)', or a `,'.  Returns
2310    TRUE if the next token is a `,'.  */
2311
2312 static bool
2313 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2314 {
2315   unsigned nesting_depth = 0;
2316
2317   while (true)
2318     {
2319       cp_token *token = cp_lexer_peek_token (parser->lexer);
2320
2321       /* If we've run out of tokens, then there is no closing `)'.  */
2322       if (token->type == CPP_EOF)
2323         return false;
2324       /* If it is a `,' stop.  */
2325       else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2326         return true;
2327       /* If it is a `)', stop.  */
2328       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2329         return false;
2330       /* If it is an `(', we have entered another level of nesting.  */
2331       else if (token->type == CPP_OPEN_PAREN)
2332         ++nesting_depth;
2333       /* Consume the token.  */
2334       token = cp_lexer_consume_token (parser->lexer);
2335     }
2336 }
2337
2338 /* Consume tokens until we reach the end of the current statement.
2339    Normally, that will be just before consuming a `;'.  However, if a
2340    non-nested `}' comes first, then we stop before consuming that.  */
2341
2342 static void
2343 cp_parser_skip_to_end_of_statement (parser)
2344      cp_parser *parser;
2345 {
2346   unsigned nesting_depth = 0;
2347
2348   while (true)
2349     {
2350       cp_token *token;
2351
2352       /* Peek at the next token.  */
2353       token = cp_lexer_peek_token (parser->lexer);
2354       /* If we've run out of tokens, stop.  */
2355       if (token->type == CPP_EOF)
2356         break;
2357       /* If the next token is a `;', we have reached the end of the
2358          statement.  */
2359       if (token->type == CPP_SEMICOLON && !nesting_depth)
2360         break;
2361       /* If the next token is a non-nested `}', then we have reached
2362          the end of the current block.  */
2363       if (token->type == CPP_CLOSE_BRACE)
2364         {
2365           /* If this is a non-nested `}', stop before consuming it.
2366              That way, when confronted with something like:
2367
2368                { 3 + } 
2369
2370              we stop before consuming the closing `}', even though we
2371              have not yet reached a `;'.  */
2372           if (nesting_depth == 0)
2373             break;
2374           /* If it is the closing `}' for a block that we have
2375              scanned, stop -- but only after consuming the token.
2376              That way given:
2377
2378                 void f g () { ... }
2379                 typedef int I;
2380
2381              we will stop after the body of the erroneously declared
2382              function, but before consuming the following `typedef'
2383              declaration.  */
2384           if (--nesting_depth == 0)
2385             {
2386               cp_lexer_consume_token (parser->lexer);
2387               break;
2388             }
2389         }
2390       /* If it the next token is a `{', then we are entering a new
2391          block.  Consume the entire block.  */
2392       else if (token->type == CPP_OPEN_BRACE)
2393         ++nesting_depth;
2394       /* Consume the token.  */
2395       cp_lexer_consume_token (parser->lexer);
2396     }
2397 }
2398
2399 /* Skip tokens until we have consumed an entire block, or until we
2400    have consumed a non-nested `;'.  */
2401
2402 static void
2403 cp_parser_skip_to_end_of_block_or_statement (parser)
2404      cp_parser *parser;
2405 {
2406   unsigned nesting_depth = 0;
2407
2408   while (true)
2409     {
2410       cp_token *token;
2411
2412       /* Peek at the next token.  */
2413       token = cp_lexer_peek_token (parser->lexer);
2414       /* If we've run out of tokens, stop.  */
2415       if (token->type == CPP_EOF)
2416         break;
2417       /* If the next token is a `;', we have reached the end of the
2418          statement.  */
2419       if (token->type == CPP_SEMICOLON && !nesting_depth)
2420         {
2421           /* Consume the `;'.  */
2422           cp_lexer_consume_token (parser->lexer);
2423           break;
2424         }
2425       /* Consume the token.  */
2426       token = cp_lexer_consume_token (parser->lexer);
2427       /* If the next token is a non-nested `}', then we have reached
2428          the end of the current block.  */
2429       if (token->type == CPP_CLOSE_BRACE 
2430           && (nesting_depth == 0 || --nesting_depth == 0))
2431         break;
2432       /* If it the next token is a `{', then we are entering a new
2433          block.  Consume the entire block.  */
2434       if (token->type == CPP_OPEN_BRACE)
2435         ++nesting_depth;
2436     }
2437 }
2438
2439 /* Skip tokens until a non-nested closing curly brace is the next
2440    token.  */
2441
2442 static void
2443 cp_parser_skip_to_closing_brace (cp_parser *parser)
2444 {
2445   unsigned nesting_depth = 0;
2446
2447   while (true)
2448     {
2449       cp_token *token;
2450
2451       /* Peek at the next token.  */
2452       token = cp_lexer_peek_token (parser->lexer);
2453       /* If we've run out of tokens, stop.  */
2454       if (token->type == CPP_EOF)
2455         break;
2456       /* If the next token is a non-nested `}', then we have reached
2457          the end of the current block.  */
2458       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2459         break;
2460       /* If it the next token is a `{', then we are entering a new
2461          block.  Consume the entire block.  */
2462       else if (token->type == CPP_OPEN_BRACE)
2463         ++nesting_depth;
2464       /* Consume the token.  */
2465       cp_lexer_consume_token (parser->lexer);
2466     }
2467 }
2468
2469 /* Create a new C++ parser.  */
2470
2471 static cp_parser *
2472 cp_parser_new ()
2473 {
2474   cp_parser *parser;
2475
2476   parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2477   parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2478   parser->context = cp_parser_context_new (NULL);
2479
2480   /* For now, we always accept GNU extensions.  */
2481   parser->allow_gnu_extensions_p = 1;
2482
2483   /* The `>' token is a greater-than operator, not the end of a
2484      template-id.  */
2485   parser->greater_than_is_operator_p = true;
2486
2487   parser->default_arg_ok_p = true;
2488   
2489   /* We are not parsing a constant-expression.  */
2490   parser->constant_expression_p = false;
2491
2492   /* Local variable names are not forbidden.  */
2493   parser->local_variables_forbidden_p = false;
2494
2495   /* We are not procesing an `extern "C"' declaration.  */
2496   parser->in_unbraced_linkage_specification_p = false;
2497
2498   /* We are not processing a declarator.  */
2499   parser->in_declarator_p = false;
2500
2501   /* The unparsed function queue is empty.  */
2502   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2503
2504   /* There are no classes being defined.  */
2505   parser->num_classes_being_defined = 0;
2506
2507   /* No template parameters apply.  */
2508   parser->num_template_parameter_lists = 0;
2509
2510   return parser;
2511 }
2512
2513 /* Lexical conventions [gram.lex]  */
2514
2515 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2516    identifier.  */
2517
2518 static tree 
2519 cp_parser_identifier (parser)
2520      cp_parser *parser;
2521 {
2522   cp_token *token;
2523
2524   /* Look for the identifier.  */
2525   token = cp_parser_require (parser, CPP_NAME, "identifier");
2526   /* Return the value.  */
2527   return token ? token->value : error_mark_node;
2528 }
2529
2530 /* Basic concepts [gram.basic]  */
2531
2532 /* Parse a translation-unit.
2533
2534    translation-unit:
2535      declaration-seq [opt]  
2536
2537    Returns TRUE if all went well.  */
2538
2539 static bool
2540 cp_parser_translation_unit (parser)
2541      cp_parser *parser;
2542 {
2543   while (true)
2544     {
2545       cp_parser_declaration_seq_opt (parser);
2546
2547       /* If there are no tokens left then all went well.  */
2548       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2549         break;
2550       
2551       /* Otherwise, issue an error message.  */
2552       cp_parser_error (parser, "expected declaration");
2553       return false;
2554     }
2555
2556   /* Consume the EOF token.  */
2557   cp_parser_require (parser, CPP_EOF, "end-of-file");
2558   
2559   /* Finish up.  */
2560   finish_translation_unit ();
2561
2562   /* All went well.  */
2563   return true;
2564 }
2565
2566 /* Expressions [gram.expr] */
2567
2568 /* Parse a primary-expression.
2569
2570    primary-expression:
2571      literal
2572      this
2573      ( expression )
2574      id-expression
2575
2576    GNU Extensions:
2577
2578    primary-expression:
2579      ( compound-statement )
2580      __builtin_va_arg ( assignment-expression , type-id )
2581
2582    literal:
2583      __null
2584
2585    Returns a representation of the expression.  
2586
2587    *IDK indicates what kind of id-expression (if any) was present.  
2588
2589    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2590    used as the operand of a pointer-to-member.  In that case,
2591    *QUALIFYING_CLASS gives the class that is used as the qualifying
2592    class in the pointer-to-member.  */
2593
2594 static tree
2595 cp_parser_primary_expression (cp_parser *parser, 
2596                               cp_parser_id_kind *idk,
2597                               tree *qualifying_class)
2598 {
2599   cp_token *token;
2600
2601   /* Assume the primary expression is not an id-expression.  */
2602   *idk = CP_PARSER_ID_KIND_NONE;
2603   /* And that it cannot be used as pointer-to-member.  */
2604   *qualifying_class = NULL_TREE;
2605
2606   /* Peek at the next token.  */
2607   token = cp_lexer_peek_token (parser->lexer);
2608   switch (token->type)
2609     {
2610       /* literal:
2611            integer-literal
2612            character-literal
2613            floating-literal
2614            string-literal
2615            boolean-literal  */
2616     case CPP_CHAR:
2617     case CPP_WCHAR:
2618     case CPP_STRING:
2619     case CPP_WSTRING:
2620     case CPP_NUMBER:
2621       token = cp_lexer_consume_token (parser->lexer);
2622       return token->value;
2623
2624     case CPP_OPEN_PAREN:
2625       {
2626         tree expr;
2627         bool saved_greater_than_is_operator_p;
2628
2629         /* Consume the `('.  */
2630         cp_lexer_consume_token (parser->lexer);
2631         /* Within a parenthesized expression, a `>' token is always
2632            the greater-than operator.  */
2633         saved_greater_than_is_operator_p 
2634           = parser->greater_than_is_operator_p;
2635         parser->greater_than_is_operator_p = true;
2636         /* If we see `( { ' then we are looking at the beginning of
2637            a GNU statement-expression.  */
2638         if (cp_parser_allow_gnu_extensions_p (parser)
2639             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2640           {
2641             /* Statement-expressions are not allowed by the standard.  */
2642             if (pedantic)
2643               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2644             
2645             /* And they're not allowed outside of a function-body; you
2646                cannot, for example, write:
2647                
2648                  int i = ({ int j = 3; j + 1; });
2649                
2650                at class or namespace scope.  */
2651             if (!at_function_scope_p ())
2652               error ("statement-expressions are allowed only inside functions");
2653             /* Start the statement-expression.  */
2654             expr = begin_stmt_expr ();
2655             /* Parse the compound-statement.  */
2656             cp_parser_compound_statement (parser);
2657             /* Finish up.  */
2658             expr = finish_stmt_expr (expr);
2659           }
2660         else
2661           {
2662             /* Parse the parenthesized expression.  */
2663             expr = cp_parser_expression (parser);
2664             /* Let the front end know that this expression was
2665                enclosed in parentheses. This matters in case, for
2666                example, the expression is of the form `A::B', since
2667                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2668                not.  */
2669             finish_parenthesized_expr (expr);
2670           }
2671         /* The `>' token might be the end of a template-id or
2672            template-parameter-list now.  */
2673         parser->greater_than_is_operator_p 
2674           = saved_greater_than_is_operator_p;
2675         /* Consume the `)'.  */
2676         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2677           cp_parser_skip_to_end_of_statement (parser);
2678
2679         return expr;
2680       }
2681
2682     case CPP_KEYWORD:
2683       switch (token->keyword)
2684         {
2685           /* These two are the boolean literals.  */
2686         case RID_TRUE:
2687           cp_lexer_consume_token (parser->lexer);
2688           return boolean_true_node;
2689         case RID_FALSE:
2690           cp_lexer_consume_token (parser->lexer);
2691           return boolean_false_node;
2692           
2693           /* The `__null' literal.  */
2694         case RID_NULL:
2695           cp_lexer_consume_token (parser->lexer);
2696           return null_node;
2697
2698           /* Recognize the `this' keyword.  */
2699         case RID_THIS:
2700           cp_lexer_consume_token (parser->lexer);
2701           if (parser->local_variables_forbidden_p)
2702             {
2703               error ("`this' may not be used in this context");
2704               return error_mark_node;
2705             }
2706           return finish_this_expr ();
2707
2708           /* The `operator' keyword can be the beginning of an
2709              id-expression.  */
2710         case RID_OPERATOR:
2711           goto id_expression;
2712
2713         case RID_FUNCTION_NAME:
2714         case RID_PRETTY_FUNCTION_NAME:
2715         case RID_C99_FUNCTION_NAME:
2716           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2717              __func__ are the names of variables -- but they are
2718              treated specially.  Therefore, they are handled here,
2719              rather than relying on the generic id-expression logic
2720              below.  Gramatically, these names are id-expressions.  
2721
2722              Consume the token.  */
2723           token = cp_lexer_consume_token (parser->lexer);
2724           /* Look up the name.  */
2725           return finish_fname (token->value);
2726
2727         case RID_VA_ARG:
2728           {
2729             tree expression;
2730             tree type;
2731
2732             /* The `__builtin_va_arg' construct is used to handle
2733                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2734             cp_lexer_consume_token (parser->lexer);
2735             /* Look for the opening `('.  */
2736             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2737             /* Now, parse the assignment-expression.  */
2738             expression = cp_parser_assignment_expression (parser);
2739             /* Look for the `,'.  */
2740             cp_parser_require (parser, CPP_COMMA, "`,'");
2741             /* Parse the type-id.  */
2742             type = cp_parser_type_id (parser);
2743             /* Look for the closing `)'.  */
2744             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2745
2746             return build_x_va_arg (expression, type);
2747           }
2748
2749         default:
2750           cp_parser_error (parser, "expected primary-expression");
2751           return error_mark_node;
2752         }
2753       /* Fall through. */
2754
2755       /* An id-expression can start with either an identifier, a
2756          `::' as the beginning of a qualified-id, or the "operator"
2757          keyword.  */
2758     case CPP_NAME:
2759     case CPP_SCOPE:
2760     case CPP_TEMPLATE_ID:
2761     case CPP_NESTED_NAME_SPECIFIER:
2762       {
2763         tree id_expression;
2764         tree decl;
2765
2766       id_expression:
2767         /* Parse the id-expression.  */
2768         id_expression 
2769           = cp_parser_id_expression (parser, 
2770                                      /*template_keyword_p=*/false,
2771                                      /*check_dependency_p=*/true,
2772                                      /*template_p=*/NULL);
2773         if (id_expression == error_mark_node)
2774           return error_mark_node;
2775         /* If we have a template-id, then no further lookup is
2776            required.  If the template-id was for a template-class, we
2777            will sometimes have a TYPE_DECL at this point.  */
2778         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2779             || TREE_CODE (id_expression) == TYPE_DECL)
2780           decl = id_expression;
2781         /* Look up the name.  */
2782         else 
2783           {
2784             decl = cp_parser_lookup_name_simple (parser, id_expression);
2785             /* If name lookup gives us a SCOPE_REF, then the
2786                qualifying scope was dependent.  Just propagate the
2787                name.  */
2788             if (TREE_CODE (decl) == SCOPE_REF)
2789               {
2790                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2791                   *qualifying_class = TREE_OPERAND (decl, 0);
2792                 return decl;
2793               }
2794             /* Check to see if DECL is a local variable in a context
2795                where that is forbidden.  */
2796             if (parser->local_variables_forbidden_p
2797                 && local_variable_p (decl))
2798               {
2799                 /* It might be that we only found DECL because we are
2800                    trying to be generous with pre-ISO scoping rules.
2801                    For example, consider:
2802
2803                      int i;
2804                      void g() {
2805                        for (int i = 0; i < 10; ++i) {}
2806                        extern void f(int j = i);
2807                      }
2808
2809                    Here, name look up will originally find the out 
2810                    of scope `i'.  We need to issue a warning message,
2811                    but then use the global `i'.  */
2812                 decl = check_for_out_of_scope_variable (decl);
2813                 if (local_variable_p (decl))
2814                   {
2815                     error ("local variable `%D' may not appear in this context",
2816                            decl);
2817                     return error_mark_node;
2818                   }
2819               }
2820
2821             /* If unqualified name lookup fails while processing a
2822                template, that just means that we need to do name
2823                lookup again when the template is instantiated.  */
2824             if (!parser->scope 
2825                 && decl == error_mark_node
2826                 && processing_template_decl)
2827               {
2828                 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2829                 return build_min_nt (LOOKUP_EXPR, id_expression);
2830               }
2831             else if (decl == error_mark_node
2832                      && !processing_template_decl)
2833               {
2834                 if (!parser->scope)
2835                   {
2836                     /* It may be resolvable as a koenig lookup function
2837                        call.  */
2838                     *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2839                     return id_expression;
2840                   }
2841                 else if (TYPE_P (parser->scope)
2842                          && !COMPLETE_TYPE_P (parser->scope))
2843                   error ("incomplete type `%T' used in nested name specifier",
2844                          parser->scope);
2845                 else if (parser->scope != global_namespace)
2846                   error ("`%D' is not a member of `%D'",
2847                          id_expression, parser->scope);
2848                 else
2849                   error ("`::%D' has not been declared", id_expression);
2850               }
2851             /* If DECL is a variable would be out of scope under
2852                ANSI/ISO rules, but in scope in the ARM, name lookup
2853                will succeed.  Issue a diagnostic here.  */
2854             else
2855               decl = check_for_out_of_scope_variable (decl);
2856
2857             /* Remember that the name was used in the definition of
2858                the current class so that we can check later to see if
2859                the meaning would have been different after the class
2860                was entirely defined.  */
2861             if (!parser->scope && decl != error_mark_node)
2862               maybe_note_name_used_in_class (id_expression, decl);
2863           }
2864
2865         /* If we didn't find anything, or what we found was a type,
2866            then this wasn't really an id-expression.  */
2867         if (TREE_CODE (decl) == TYPE_DECL
2868             || TREE_CODE (decl) == NAMESPACE_DECL
2869             || (TREE_CODE (decl) == TEMPLATE_DECL
2870                 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2871           {
2872             cp_parser_error (parser, 
2873                              "expected primary-expression");
2874             return error_mark_node;
2875           }
2876
2877         /* If the name resolved to a template parameter, there is no
2878            need to look it up again later.  Similarly, we resolve
2879            enumeration constants to their underlying values.  */
2880         if (TREE_CODE (decl) == CONST_DECL)
2881           {
2882             *idk = CP_PARSER_ID_KIND_NONE;
2883             if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2884               return DECL_INITIAL (decl);
2885             return decl;
2886           }
2887         else
2888           {
2889             bool dependent_p;
2890             
2891             /* If the declaration was explicitly qualified indicate
2892                that.  The semantics of `A::f(3)' are different than
2893                `f(3)' if `f' is virtual.  */
2894             *idk = (parser->scope 
2895                     ? CP_PARSER_ID_KIND_QUALIFIED
2896                     : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2897                        ? CP_PARSER_ID_KIND_TEMPLATE_ID
2898                        : CP_PARSER_ID_KIND_UNQUALIFIED));
2899
2900
2901             /* [temp.dep.expr]
2902                
2903                An id-expression is type-dependent if it contains an
2904                identifier that was declared with a dependent type.
2905                
2906                As an optimization, we could choose not to create a
2907                LOOKUP_EXPR for a name that resolved to a local
2908                variable in the template function that we are currently
2909                declaring; such a name cannot ever resolve to anything
2910                else.  If we did that we would not have to look up
2911                these names at instantiation time.
2912                
2913                The standard is not very specific about an
2914                id-expression that names a set of overloaded functions.
2915                What if some of them have dependent types and some of
2916                them do not?  Presumably, such a name should be treated
2917                as a dependent name.  */
2918             /* Assume the name is not dependent.  */
2919             dependent_p = false;
2920             if (!processing_template_decl)
2921               /* No names are dependent outside a template.  */
2922               ;
2923             /* A template-id where the name of the template was not
2924                resolved is definitely dependent.  */
2925             else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2926                      && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2927                          == IDENTIFIER_NODE))
2928               dependent_p = true;
2929             /* For anything except an overloaded function, just check
2930                its type.  */
2931             else if (!is_overloaded_fn (decl))
2932               dependent_p 
2933                 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2934             /* For a set of overloaded functions, check each of the
2935                functions.  */
2936             else
2937               {
2938                 tree fns = decl;
2939
2940                 if (BASELINK_P (fns))
2941                   fns = BASELINK_FUNCTIONS (fns);
2942                   
2943                 /* For a template-id, check to see if the template
2944                    arguments are dependent.  */
2945                 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2946                   {
2947                     tree args = TREE_OPERAND (fns, 1);
2948
2949                     if (args && TREE_CODE (args) == TREE_LIST)
2950                       {
2951                         while (args)
2952                           {
2953                             if (cp_parser_dependent_template_arg_p
2954                                 (TREE_VALUE (args)))
2955                               {
2956                                 dependent_p = true;
2957                                 break;
2958                               }
2959                             args = TREE_CHAIN (args);
2960                           }
2961                       }
2962                     else if (args && TREE_CODE (args) == TREE_VEC)
2963                       {
2964                         int i; 
2965                         for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2966                           if (cp_parser_dependent_template_arg_p
2967                               (TREE_VEC_ELT (args, i)))
2968                             {
2969                               dependent_p = true;
2970                               break;
2971                             }
2972                       }
2973
2974                     /* The functions are those referred to by the
2975                        template-id.  */
2976                     fns = TREE_OPERAND (fns, 0);
2977                   }
2978
2979                 /* If there are no dependent template arguments, go
2980                    through the overlaoded functions.  */
2981                 while (fns && !dependent_p)
2982                   {
2983                     tree fn = OVL_CURRENT (fns);
2984                     
2985                     /* Member functions of dependent classes are
2986                        dependent.  */
2987                     if (TREE_CODE (fn) == FUNCTION_DECL
2988                         && cp_parser_type_dependent_expression_p (fn))
2989                       dependent_p = true;
2990                     else if (TREE_CODE (fn) == TEMPLATE_DECL
2991                              && cp_parser_dependent_template_p (fn))
2992                       dependent_p = true;
2993                     
2994                     fns = OVL_NEXT (fns);
2995                   }
2996               }
2997
2998             /* If the name was dependent on a template parameter,
2999                we will resolve the name at instantiation time.  */
3000             if (dependent_p)
3001               {
3002                 /* Create a SCOPE_REF for qualified names.  */
3003                 if (parser->scope)
3004                   {
3005                     if (TYPE_P (parser->scope))
3006                       *qualifying_class = parser->scope;
3007                     return build_nt (SCOPE_REF, 
3008                                      parser->scope, 
3009                                      id_expression);
3010                   }
3011                 /* A TEMPLATE_ID already contains all the information
3012                    we need.  */
3013                 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3014                   return id_expression;
3015                 /* Create a LOOKUP_EXPR for other unqualified names.  */
3016                 return build_min_nt (LOOKUP_EXPR, id_expression);
3017               }
3018
3019             if (parser->scope)
3020               {
3021                 decl = (adjust_result_of_qualified_name_lookup 
3022                         (decl, parser->scope, current_class_type));
3023                 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3024                   *qualifying_class = parser->scope;
3025               }
3026             else
3027               /* Transform references to non-static data members into
3028                  COMPONENT_REFs.  */
3029               decl = hack_identifier (decl, id_expression);
3030
3031             /* Resolve references to variables of anonymous unions
3032                into COMPONENT_REFs.  */
3033             if (TREE_CODE (decl) == ALIAS_DECL)
3034               decl = DECL_INITIAL (decl);
3035           }
3036
3037         if (TREE_DEPRECATED (decl))
3038           warn_deprecated_use (decl);
3039
3040         return decl;
3041       }
3042
3043       /* Anything else is an error.  */
3044     default:
3045       cp_parser_error (parser, "expected primary-expression");
3046       return error_mark_node;
3047     }
3048 }
3049
3050 /* Parse an id-expression.
3051
3052    id-expression:
3053      unqualified-id
3054      qualified-id
3055
3056    qualified-id:
3057      :: [opt] nested-name-specifier template [opt] unqualified-id
3058      :: identifier
3059      :: operator-function-id
3060      :: template-id
3061
3062    Return a representation of the unqualified portion of the
3063    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3064    a `::' or nested-name-specifier.
3065
3066    Often, if the id-expression was a qualified-id, the caller will
3067    want to make a SCOPE_REF to represent the qualified-id.  This
3068    function does not do this in order to avoid wastefully creating
3069    SCOPE_REFs when they are not required.
3070
3071    If ASSUME_TYPENAME_P is true then we assume that qualified names
3072    are typenames.  This flag is set when parsing a declarator-id;
3073    for something like:
3074
3075      template <class T>
3076      int S<T>::R::i = 3;
3077
3078    we are supposed to assume that `S<T>::R' is a class.
3079
3080    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3081    `template' keyword.
3082
3083    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3084    uninstantiated templates.  
3085
3086    If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3087    `template' keyword is used to explicitly indicate that the entity
3088    named is a template.  */
3089
3090 static tree
3091 cp_parser_id_expression (cp_parser *parser,
3092                          bool template_keyword_p,
3093                          bool check_dependency_p,
3094                          bool *template_p)
3095 {
3096   bool global_scope_p;
3097   bool nested_name_specifier_p;
3098
3099   /* Assume the `template' keyword was not used.  */
3100   if (template_p)
3101     *template_p = false;
3102
3103   /* Look for the optional `::' operator.  */
3104   global_scope_p 
3105     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
3106        != NULL_TREE);
3107   /* Look for the optional nested-name-specifier.  */
3108   nested_name_specifier_p 
3109     = (cp_parser_nested_name_specifier_opt (parser,
3110                                             /*typename_keyword_p=*/false,
3111                                             check_dependency_p,
3112                                             /*type_p=*/false)
3113        != NULL_TREE);
3114   /* If there is a nested-name-specifier, then we are looking at
3115      the first qualified-id production.  */
3116   if (nested_name_specifier_p)
3117     {
3118       tree saved_scope;
3119       tree saved_object_scope;
3120       tree saved_qualifying_scope;
3121       tree unqualified_id;
3122       bool is_template;
3123
3124       /* See if the next token is the `template' keyword.  */
3125       if (!template_p)
3126         template_p = &is_template;
3127       *template_p = cp_parser_optional_template_keyword (parser);
3128       /* Name lookup we do during the processing of the
3129          unqualified-id might obliterate SCOPE.  */
3130       saved_scope = parser->scope;
3131       saved_object_scope = parser->object_scope;
3132       saved_qualifying_scope = parser->qualifying_scope;
3133       /* Process the final unqualified-id.  */
3134       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3135                                                  check_dependency_p);
3136       /* Restore the SAVED_SCOPE for our caller.  */
3137       parser->scope = saved_scope;
3138       parser->object_scope = saved_object_scope;
3139       parser->qualifying_scope = saved_qualifying_scope;
3140
3141       return unqualified_id;
3142     }
3143   /* Otherwise, if we are in global scope, then we are looking at one
3144      of the other qualified-id productions.  */
3145   else if (global_scope_p)
3146     {
3147       cp_token *token;
3148       tree id;
3149
3150       /* Peek at the next token.  */
3151       token = cp_lexer_peek_token (parser->lexer);
3152
3153       /* If it's an identifier, and the next token is not a "<", then
3154          we can avoid the template-id case.  This is an optimization
3155          for this common case.  */
3156       if (token->type == CPP_NAME 
3157           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3158         return cp_parser_identifier (parser);
3159
3160       cp_parser_parse_tentatively (parser);
3161       /* Try a template-id.  */
3162       id = cp_parser_template_id (parser, 
3163                                   /*template_keyword_p=*/false,
3164                                   /*check_dependency_p=*/true);
3165       /* If that worked, we're done.  */
3166       if (cp_parser_parse_definitely (parser))
3167         return id;
3168
3169       /* Peek at the next token.  (Changes in the token buffer may
3170          have invalidated the pointer obtained above.)  */
3171       token = cp_lexer_peek_token (parser->lexer);
3172
3173       switch (token->type)
3174         {
3175         case CPP_NAME:
3176           return cp_parser_identifier (parser);
3177
3178         case CPP_KEYWORD:
3179           if (token->keyword == RID_OPERATOR)
3180             return cp_parser_operator_function_id (parser);
3181           /* Fall through.  */
3182           
3183         default:
3184           cp_parser_error (parser, "expected id-expression");
3185           return error_mark_node;
3186         }
3187     }
3188   else
3189     return cp_parser_unqualified_id (parser, template_keyword_p,
3190                                      /*check_dependency_p=*/true);
3191 }
3192
3193 /* Parse an unqualified-id.
3194
3195    unqualified-id:
3196      identifier
3197      operator-function-id
3198      conversion-function-id
3199      ~ class-name
3200      template-id
3201
3202    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3203    keyword, in a construct like `A::template ...'.
3204
3205    Returns a representation of unqualified-id.  For the `identifier'
3206    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3207    production a BIT_NOT_EXPR is returned; the operand of the
3208    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3209    other productions, see the documentation accompanying the
3210    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3211    names are looked up in uninstantiated templates.  */
3212
3213 static tree
3214 cp_parser_unqualified_id (parser, template_keyword_p,
3215                           check_dependency_p)
3216      cp_parser *parser;
3217      bool template_keyword_p;
3218      bool check_dependency_p;
3219 {
3220   cp_token *token;
3221
3222   /* Peek at the next token.  */
3223   token = cp_lexer_peek_token (parser->lexer);
3224   
3225   switch (token->type)
3226     {
3227     case CPP_NAME:
3228       {
3229         tree id;
3230
3231         /* We don't know yet whether or not this will be a
3232            template-id.  */
3233         cp_parser_parse_tentatively (parser);
3234         /* Try a template-id.  */
3235         id = cp_parser_template_id (parser, template_keyword_p,
3236                                     check_dependency_p);
3237         /* If it worked, we're done.  */
3238         if (cp_parser_parse_definitely (parser))
3239           return id;
3240         /* Otherwise, it's an ordinary identifier.  */
3241         return cp_parser_identifier (parser);
3242       }
3243
3244     case CPP_TEMPLATE_ID:
3245       return cp_parser_template_id (parser, template_keyword_p,
3246                                     check_dependency_p);
3247
3248     case CPP_COMPL:
3249       {
3250         tree type_decl;
3251         tree qualifying_scope;
3252         tree object_scope;
3253         tree scope;
3254
3255         /* Consume the `~' token.  */
3256         cp_lexer_consume_token (parser->lexer);
3257         /* Parse the class-name.  The standard, as written, seems to
3258            say that:
3259
3260              template <typename T> struct S { ~S (); };
3261              template <typename T> S<T>::~S() {}
3262
3263            is invalid, since `~' must be followed by a class-name, but
3264            `S<T>' is dependent, and so not known to be a class.
3265            That's not right; we need to look in uninstantiated
3266            templates.  A further complication arises from:
3267
3268              template <typename T> void f(T t) {
3269                t.T::~T();
3270              } 
3271
3272            Here, it is not possible to look up `T' in the scope of `T'
3273            itself.  We must look in both the current scope, and the
3274            scope of the containing complete expression.  
3275
3276            Yet another issue is:
3277
3278              struct S {
3279                int S;
3280                ~S();
3281              };
3282
3283              S::~S() {}
3284
3285            The standard does not seem to say that the `S' in `~S'
3286            should refer to the type `S' and not the data member
3287            `S::S'.  */
3288
3289         /* DR 244 says that we look up the name after the "~" in the
3290            same scope as we looked up the qualifying name.  That idea
3291            isn't fully worked out; it's more complicated than that.  */
3292         scope = parser->scope;
3293         object_scope = parser->object_scope;
3294         qualifying_scope = parser->qualifying_scope;
3295
3296         /* If the name is of the form "X::~X" it's OK.  */
3297         if (scope && TYPE_P (scope)
3298             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3299             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3300                 == CPP_OPEN_PAREN)
3301             && (cp_lexer_peek_token (parser->lexer)->value 
3302                 == TYPE_IDENTIFIER (scope)))
3303           {
3304             cp_lexer_consume_token (parser->lexer);
3305             return build_nt (BIT_NOT_EXPR, scope);
3306           }
3307
3308         /* If there was an explicit qualification (S::~T), first look
3309            in the scope given by the qualification (i.e., S).  */
3310         if (scope)
3311           {
3312             cp_parser_parse_tentatively (parser);
3313             type_decl = cp_parser_class_name (parser, 
3314                                               /*typename_keyword_p=*/false,
3315                                               /*template_keyword_p=*/false,
3316                                               /*type_p=*/false,
3317                                               /*check_access_p=*/true,
3318                                               /*check_dependency=*/false,
3319                                               /*class_head_p=*/false);
3320             if (cp_parser_parse_definitely (parser))
3321               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3322           }
3323         /* In "N::S::~S", look in "N" as well.  */
3324         if (scope && qualifying_scope)
3325           {
3326             cp_parser_parse_tentatively (parser);
3327             parser->scope = qualifying_scope;
3328             parser->object_scope = NULL_TREE;
3329             parser->qualifying_scope = NULL_TREE;
3330             type_decl 
3331               = cp_parser_class_name (parser, 
3332                                       /*typename_keyword_p=*/false,
3333                                       /*template_keyword_p=*/false,
3334                                       /*type_p=*/false,
3335                                       /*check_access_p=*/true,
3336                                       /*check_dependency=*/false,
3337                                       /*class_head_p=*/false);
3338             if (cp_parser_parse_definitely (parser))
3339               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3340           }
3341         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3342         else if (object_scope)
3343           {
3344             cp_parser_parse_tentatively (parser);
3345             parser->scope = object_scope;
3346             parser->object_scope = NULL_TREE;
3347             parser->qualifying_scope = NULL_TREE;
3348             type_decl 
3349               = cp_parser_class_name (parser, 
3350                                       /*typename_keyword_p=*/false,
3351                                       /*template_keyword_p=*/false,
3352                                       /*type_p=*/false,
3353                                       /*check_access_p=*/true,
3354                                       /*check_dependency=*/false,
3355                                       /*class_head_p=*/false);
3356             if (cp_parser_parse_definitely (parser))
3357               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3358           }
3359         /* Look in the surrounding context.  */
3360         parser->scope = NULL_TREE;
3361         parser->object_scope = NULL_TREE;
3362         parser->qualifying_scope = NULL_TREE;
3363         type_decl 
3364           = cp_parser_class_name (parser, 
3365                                   /*typename_keyword_p=*/false,
3366                                   /*template_keyword_p=*/false,
3367                                   /*type_p=*/false,
3368                                   /*check_access_p=*/true,
3369                                   /*check_dependency=*/false,
3370                                   /*class_head_p=*/false);
3371         /* If an error occurred, assume that the name of the
3372            destructor is the same as the name of the qualifying
3373            class.  That allows us to keep parsing after running
3374            into ill-formed destructor names.  */
3375         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3376           return build_nt (BIT_NOT_EXPR, scope);
3377         else if (type_decl == error_mark_node)
3378           return error_mark_node;
3379
3380         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3381       }
3382
3383     case CPP_KEYWORD:
3384       if (token->keyword == RID_OPERATOR)
3385         {
3386           tree id;
3387
3388           /* This could be a template-id, so we try that first.  */
3389           cp_parser_parse_tentatively (parser);
3390           /* Try a template-id.  */
3391           id = cp_parser_template_id (parser, template_keyword_p,
3392                                       /*check_dependency_p=*/true);
3393           /* If that worked, we're done.  */
3394           if (cp_parser_parse_definitely (parser))
3395             return id;
3396           /* We still don't know whether we're looking at an
3397              operator-function-id or a conversion-function-id.  */
3398           cp_parser_parse_tentatively (parser);
3399           /* Try an operator-function-id.  */
3400           id = cp_parser_operator_function_id (parser);
3401           /* If that didn't work, try a conversion-function-id.  */
3402           if (!cp_parser_parse_definitely (parser))
3403             id = cp_parser_conversion_function_id (parser);
3404
3405           return id;
3406         }
3407       /* Fall through.  */
3408
3409     default:
3410       cp_parser_error (parser, "expected unqualified-id");
3411       return error_mark_node;
3412     }
3413 }
3414
3415 /* Parse an (optional) nested-name-specifier.
3416
3417    nested-name-specifier:
3418      class-or-namespace-name :: nested-name-specifier [opt]
3419      class-or-namespace-name :: template nested-name-specifier [opt]
3420
3421    PARSER->SCOPE should be set appropriately before this function is
3422    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3423    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3424    in name lookups.
3425
3426    Sets PARSER->SCOPE to the class (TYPE) or namespace
3427    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3428    it unchanged if there is no nested-name-specifier.  Returns the new
3429    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  */
3430
3431 static tree
3432 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3433                                      bool typename_keyword_p, 
3434                                      bool check_dependency_p,
3435                                      bool type_p)
3436 {
3437   bool success = false;
3438   tree access_check = NULL_TREE;
3439   ptrdiff_t start;
3440
3441   /* If the next token corresponds to a nested name specifier, there
3442      is no need to reparse it.  */
3443   if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3444     {
3445       tree value;
3446       tree check;
3447
3448       /* Get the stored value.  */
3449       value = cp_lexer_consume_token (parser->lexer)->value;
3450       /* Perform any access checks that were deferred.  */
3451       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3452         cp_parser_defer_access_check (parser, 
3453                                       TREE_PURPOSE (check),
3454                                       TREE_VALUE (check));
3455       /* Set the scope from the stored value.  */
3456       parser->scope = TREE_VALUE (value);
3457       parser->qualifying_scope = TREE_TYPE (value);
3458       parser->object_scope = NULL_TREE;
3459       return parser->scope;
3460     }
3461
3462   /* Remember where the nested-name-specifier starts.  */
3463   if (cp_parser_parsing_tentatively (parser)
3464       && !cp_parser_committed_to_tentative_parse (parser))
3465     {
3466       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3467       start = cp_lexer_token_difference (parser->lexer,
3468                                          parser->lexer->first_token,
3469                                          next_token);
3470       access_check = parser->context->deferred_access_checks;
3471     }
3472   else
3473     start = -1;
3474
3475   while (true)
3476     {
3477       tree new_scope;
3478       tree old_scope;
3479       tree saved_qualifying_scope;
3480       cp_token *token;
3481       bool template_keyword_p;
3482
3483       /* Spot cases that cannot be the beginning of a
3484          nested-name-specifier.  On the second and subsequent times
3485          through the loop, we look for the `template' keyword.  */
3486       token = cp_lexer_peek_token (parser->lexer);
3487       if (success && token->keyword == RID_TEMPLATE)
3488         ;
3489       /* A template-id can start a nested-name-specifier.  */
3490       else if (token->type == CPP_TEMPLATE_ID)
3491         ;
3492       else
3493         {
3494           /* If the next token is not an identifier, then it is
3495              definitely not a class-or-namespace-name.  */
3496           if (token->type != CPP_NAME)
3497             break;
3498           /* If the following token is neither a `<' (to begin a
3499              template-id), nor a `::', then we are not looking at a
3500              nested-name-specifier.  */
3501           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3502           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3503             break;
3504         }
3505
3506       /* The nested-name-specifier is optional, so we parse
3507          tentatively.  */
3508       cp_parser_parse_tentatively (parser);
3509
3510       /* Look for the optional `template' keyword, if this isn't the
3511          first time through the loop.  */
3512       if (success)
3513         template_keyword_p = cp_parser_optional_template_keyword (parser);
3514       else
3515         template_keyword_p = false;
3516
3517       /* Save the old scope since the name lookup we are about to do
3518          might destroy it.  */
3519       old_scope = parser->scope;
3520       saved_qualifying_scope = parser->qualifying_scope;
3521       /* Parse the qualifying entity.  */
3522       new_scope 
3523         = cp_parser_class_or_namespace_name (parser,
3524                                              typename_keyword_p,
3525                                              template_keyword_p,
3526                                              check_dependency_p,
3527                                              type_p);
3528       /* Look for the `::' token.  */
3529       cp_parser_require (parser, CPP_SCOPE, "`::'");
3530
3531       /* If we found what we wanted, we keep going; otherwise, we're
3532          done.  */
3533       if (!cp_parser_parse_definitely (parser))
3534         {
3535           bool error_p = false;
3536
3537           /* Restore the OLD_SCOPE since it was valid before the
3538              failed attempt at finding the last
3539              class-or-namespace-name.  */
3540           parser->scope = old_scope;
3541           parser->qualifying_scope = saved_qualifying_scope;
3542           /* If the next token is an identifier, and the one after
3543              that is a `::', then any valid interpretation would have
3544              found a class-or-namespace-name.  */
3545           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3546                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3547                      == CPP_SCOPE)
3548                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3549                      != CPP_COMPL))
3550             {
3551               token = cp_lexer_consume_token (parser->lexer);
3552               if (!error_p) 
3553                 {
3554                   tree decl;
3555
3556                   decl = cp_parser_lookup_name_simple (parser, token->value);
3557                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3558                     error ("`%D' used without template parameters",
3559                            decl);
3560                   else if (parser->scope)
3561                     {
3562                       if (TYPE_P (parser->scope))
3563                         error ("`%T::%D' is not a class-name or "
3564                                "namespace-name",
3565                                parser->scope, token->value);
3566                       else
3567                         error ("`%D::%D' is not a class-name or "
3568                                "namespace-name",
3569                                parser->scope, token->value);
3570                     }
3571                   else
3572                     error ("`%D' is not a class-name or namespace-name",
3573                            token->value);
3574                   parser->scope = NULL_TREE;
3575                   error_p = true;
3576                   /* Treat this as a successful nested-name-specifier
3577                      due to:
3578
3579                      [basic.lookup.qual]
3580
3581                      If the name found is not a class-name (clause
3582                      _class_) or namespace-name (_namespace.def_), the
3583                      program is ill-formed.  */
3584                   success = true;
3585                 }
3586               cp_lexer_consume_token (parser->lexer);
3587             }
3588           break;
3589         }
3590
3591       /* We've found one valid nested-name-specifier.  */
3592       success = true;
3593       /* Make sure we look in the right scope the next time through
3594          the loop.  */
3595       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3596                        ? TREE_TYPE (new_scope)
3597                        : new_scope);
3598       /* If it is a class scope, try to complete it; we are about to
3599          be looking up names inside the class.  */
3600       if (TYPE_P (parser->scope))
3601         complete_type (parser->scope);
3602     }
3603
3604   /* If parsing tentatively, replace the sequence of tokens that makes
3605      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3606      token.  That way, should we re-parse the token stream, we will
3607      not have to repeat the effort required to do the parse, nor will
3608      we issue duplicate error messages.  */
3609   if (success && start >= 0)
3610     {
3611       cp_token *token;
3612       tree c;
3613
3614       /* Find the token that corresponds to the start of the
3615          template-id.  */
3616       token = cp_lexer_advance_token (parser->lexer, 
3617                                       parser->lexer->first_token,
3618                                       start);
3619
3620       /* Remember the access checks associated with this
3621          nested-name-specifier.  */
3622       c = parser->context->deferred_access_checks;
3623       if (c == access_check)
3624         access_check = NULL_TREE;
3625       else
3626         {
3627           while (TREE_CHAIN (c) != access_check)
3628             c = TREE_CHAIN (c);
3629           access_check = parser->context->deferred_access_checks;
3630           parser->context->deferred_access_checks = TREE_CHAIN (c);
3631           TREE_CHAIN (c) = NULL_TREE;
3632         }
3633
3634       /* Reset the contents of the START token.  */
3635       token->type = CPP_NESTED_NAME_SPECIFIER;
3636       token->value = build_tree_list (access_check, parser->scope);
3637       TREE_TYPE (token->value) = parser->qualifying_scope;
3638       token->keyword = RID_MAX;
3639       /* Purge all subsequent tokens.  */
3640       cp_lexer_purge_tokens_after (parser->lexer, token);
3641     }
3642
3643   return success ? parser->scope : NULL_TREE;
3644 }
3645
3646 /* Parse a nested-name-specifier.  See
3647    cp_parser_nested_name_specifier_opt for details.  This function
3648    behaves identically, except that it will an issue an error if no
3649    nested-name-specifier is present, and it will return
3650    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3651    is present.  */
3652
3653 static tree
3654 cp_parser_nested_name_specifier (cp_parser *parser, 
3655                                  bool typename_keyword_p, 
3656                                  bool check_dependency_p,
3657                                  bool type_p)
3658 {
3659   tree scope;
3660
3661   /* Look for the nested-name-specifier.  */
3662   scope = cp_parser_nested_name_specifier_opt (parser,
3663                                                typename_keyword_p,
3664                                                check_dependency_p,
3665                                                type_p);
3666   /* If it was not present, issue an error message.  */
3667   if (!scope)
3668     {
3669       cp_parser_error (parser, "expected nested-name-specifier");
3670       return error_mark_node;
3671     }
3672
3673   return scope;
3674 }
3675
3676 /* Parse a class-or-namespace-name.
3677
3678    class-or-namespace-name:
3679      class-name
3680      namespace-name
3681
3682    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3683    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3684    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3685    TYPE_P is TRUE iff the next name should be taken as a class-name,
3686    even the same name is declared to be another entity in the same
3687    scope.
3688
3689    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3690    specified by the class-or-namespace-name.  If neither is found the
3691    ERROR_MARK_NODE is returned.  */
3692
3693 static tree
3694 cp_parser_class_or_namespace_name (cp_parser *parser, 
3695                                    bool typename_keyword_p,
3696                                    bool template_keyword_p,
3697                                    bool check_dependency_p,
3698                                    bool type_p)
3699 {
3700   tree saved_scope;
3701   tree saved_qualifying_scope;
3702   tree saved_object_scope;
3703   tree scope;
3704   bool only_class_p;
3705
3706   /* If the next token is the `template' keyword, we know that we are
3707      looking at a class-name.  */
3708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3709     return cp_parser_class_name (parser, 
3710                                  typename_keyword_p,
3711                                  template_keyword_p,
3712                                  type_p,
3713                                  /*check_access_p=*/true,
3714                                  check_dependency_p,
3715                                  /*class_head_p=*/false);
3716   /* Before we try to parse the class-name, we must save away the
3717      current PARSER->SCOPE since cp_parser_class_name will destroy
3718      it.  */
3719   saved_scope = parser->scope;
3720   saved_qualifying_scope = parser->qualifying_scope;
3721   saved_object_scope = parser->object_scope;
3722   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3723      there is no need to look for a namespace-name.  */
3724   only_class_p = saved_scope && TYPE_P (saved_scope);
3725   if (!only_class_p)
3726     cp_parser_parse_tentatively (parser);
3727   scope = cp_parser_class_name (parser, 
3728                                 typename_keyword_p,
3729                                 template_keyword_p,
3730                                 type_p,
3731                                 /*check_access_p=*/true,
3732                                 check_dependency_p,
3733                                 /*class_head_p=*/false);
3734   /* If that didn't work, try for a namespace-name.  */
3735   if (!only_class_p && !cp_parser_parse_definitely (parser))
3736     {
3737       /* Restore the saved scope.  */
3738       parser->scope = saved_scope;
3739       parser->qualifying_scope = saved_qualifying_scope;
3740       parser->object_scope = saved_object_scope;
3741       /* If we are not looking at an identifier followed by the scope
3742          resolution operator, then this is not part of a
3743          nested-name-specifier.  (Note that this function is only used
3744          to parse the components of a nested-name-specifier.)  */
3745       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3746           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3747         return error_mark_node;
3748       scope = cp_parser_namespace_name (parser);
3749     }
3750
3751   return scope;
3752 }
3753
3754 /* Parse a postfix-expression.
3755
3756    postfix-expression:
3757      primary-expression
3758      postfix-expression [ expression ]
3759      postfix-expression ( expression-list [opt] )
3760      simple-type-specifier ( expression-list [opt] )
3761      typename :: [opt] nested-name-specifier identifier 
3762        ( expression-list [opt] )
3763      typename :: [opt] nested-name-specifier template [opt] template-id
3764        ( expression-list [opt] )
3765      postfix-expression . template [opt] id-expression
3766      postfix-expression -> template [opt] id-expression
3767      postfix-expression . pseudo-destructor-name
3768      postfix-expression -> pseudo-destructor-name
3769      postfix-expression ++
3770      postfix-expression --
3771      dynamic_cast < type-id > ( expression )
3772      static_cast < type-id > ( expression )
3773      reinterpret_cast < type-id > ( expression )
3774      const_cast < type-id > ( expression )
3775      typeid ( expression )
3776      typeid ( type-id )
3777
3778    GNU Extension:
3779      
3780    postfix-expression:
3781      ( type-id ) { initializer-list , [opt] }
3782
3783    This extension is a GNU version of the C99 compound-literal
3784    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3785    but they are essentially the same concept.)
3786
3787    If ADDRESS_P is true, the postfix expression is the operand of the
3788    `&' operator.
3789
3790    Returns a representation of the expression.  */
3791
3792 static tree
3793 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3794 {
3795   cp_token *token;
3796   enum rid keyword;
3797   cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3798   tree postfix_expression = NULL_TREE;
3799   /* Non-NULL only if the current postfix-expression can be used to
3800      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3801      class used to qualify the member.  */
3802   tree qualifying_class = NULL_TREE;
3803   bool done;
3804
3805   /* Peek at the next token.  */
3806   token = cp_lexer_peek_token (parser->lexer);
3807   /* Some of the productions are determined by keywords.  */
3808   keyword = token->keyword;
3809   switch (keyword)
3810     {
3811     case RID_DYNCAST:
3812     case RID_STATCAST:
3813     case RID_REINTCAST:
3814     case RID_CONSTCAST:
3815       {
3816         tree type;
3817         tree expression;
3818         const char *saved_message;
3819
3820         /* All of these can be handled in the same way from the point
3821            of view of parsing.  Begin by consuming the token
3822            identifying the cast.  */
3823         cp_lexer_consume_token (parser->lexer);
3824         
3825         /* New types cannot be defined in the cast.  */
3826         saved_message = parser->type_definition_forbidden_message;
3827         parser->type_definition_forbidden_message
3828           = "types may not be defined in casts";
3829
3830         /* Look for the opening `<'.  */
3831         cp_parser_require (parser, CPP_LESS, "`<'");
3832         /* Parse the type to which we are casting.  */
3833         type = cp_parser_type_id (parser);
3834         /* Look for the closing `>'.  */
3835         cp_parser_require (parser, CPP_GREATER, "`>'");
3836         /* Restore the old message.  */
3837         parser->type_definition_forbidden_message = saved_message;
3838
3839         /* And the expression which is being cast.  */
3840         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3841         expression = cp_parser_expression (parser);
3842         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3843
3844         switch (keyword)
3845           {
3846           case RID_DYNCAST:
3847             postfix_expression
3848               = build_dynamic_cast (type, expression);
3849             break;
3850           case RID_STATCAST:
3851             postfix_expression
3852               = build_static_cast (type, expression);
3853             break;
3854           case RID_REINTCAST:
3855             postfix_expression
3856               = build_reinterpret_cast (type, expression);
3857             break;
3858           case RID_CONSTCAST:
3859             postfix_expression
3860               = build_const_cast (type, expression);
3861             break;
3862           default:
3863             abort ();
3864           }
3865       }
3866       break;
3867
3868     case RID_TYPEID:
3869       {
3870         tree type;
3871         const char *saved_message;
3872
3873         /* Consume the `typeid' token.  */
3874         cp_lexer_consume_token (parser->lexer);
3875         /* Look for the `(' token.  */
3876         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3877         /* Types cannot be defined in a `typeid' expression.  */
3878         saved_message = parser->type_definition_forbidden_message;
3879         parser->type_definition_forbidden_message
3880           = "types may not be defined in a `typeid\' expression";
3881         /* We can't be sure yet whether we're looking at a type-id or an
3882            expression.  */
3883         cp_parser_parse_tentatively (parser);
3884         /* Try a type-id first.  */
3885         type = cp_parser_type_id (parser);
3886         /* Look for the `)' token.  Otherwise, we can't be sure that
3887            we're not looking at an expression: consider `typeid (int
3888            (3))', for example.  */
3889         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3890         /* If all went well, simply lookup the type-id.  */
3891         if (cp_parser_parse_definitely (parser))
3892           postfix_expression = get_typeid (type);
3893         /* Otherwise, fall back to the expression variant.  */
3894         else
3895           {
3896             tree expression;
3897
3898             /* Look for an expression.  */
3899             expression = cp_parser_expression (parser);
3900             /* Compute its typeid.  */
3901             postfix_expression = build_typeid (expression);
3902             /* Look for the `)' token.  */
3903             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3904           }
3905
3906         /* Restore the saved message.  */
3907         parser->type_definition_forbidden_message = saved_message;
3908       }
3909       break;
3910       
3911     case RID_TYPENAME:
3912       {
3913         bool template_p = false;
3914         tree id;
3915         tree type;
3916
3917         /* Consume the `typename' token.  */
3918         cp_lexer_consume_token (parser->lexer);
3919         /* Look for the optional `::' operator.  */
3920         cp_parser_global_scope_opt (parser, 
3921                                     /*current_scope_valid_p=*/false);
3922         /* Look for the nested-name-specifier.  */
3923         cp_parser_nested_name_specifier (parser,
3924                                          /*typename_keyword_p=*/true,
3925                                          /*check_dependency_p=*/true,
3926                                          /*type_p=*/true);
3927         /* Look for the optional `template' keyword.  */
3928         template_p = cp_parser_optional_template_keyword (parser);
3929         /* We don't know whether we're looking at a template-id or an
3930            identifier.  */
3931         cp_parser_parse_tentatively (parser);
3932         /* Try a template-id.  */
3933         id = cp_parser_template_id (parser, template_p,
3934                                     /*check_dependency_p=*/true);
3935         /* If that didn't work, try an identifier.  */
3936         if (!cp_parser_parse_definitely (parser))
3937           id = cp_parser_identifier (parser);
3938         /* Create a TYPENAME_TYPE to represent the type to which the
3939            functional cast is being performed.  */
3940         type = make_typename_type (parser->scope, id, 
3941                                    /*complain=*/1);
3942
3943         postfix_expression = cp_parser_functional_cast (parser, type);
3944       }
3945       break;
3946
3947     default:
3948       {
3949         tree type;
3950
3951         /* If the next thing is a simple-type-specifier, we may be
3952            looking at a functional cast.  We could also be looking at
3953            an id-expression.  So, we try the functional cast, and if
3954            that doesn't work we fall back to the primary-expression.  */
3955         cp_parser_parse_tentatively (parser);
3956         /* Look for the simple-type-specifier.  */
3957         type = cp_parser_simple_type_specifier (parser, 
3958                                                 CP_PARSER_FLAGS_NONE);
3959         /* Parse the cast itself.  */
3960         if (!cp_parser_error_occurred (parser))
3961           postfix_expression 
3962             = cp_parser_functional_cast (parser, type);
3963         /* If that worked, we're done.  */
3964         if (cp_parser_parse_definitely (parser))
3965           break;
3966
3967         /* If the functional-cast didn't work out, try a
3968            compound-literal.  */
3969         if (cp_parser_allow_gnu_extensions_p (parser))
3970           {
3971             tree initializer_list = NULL_TREE;
3972
3973             cp_parser_parse_tentatively (parser);
3974             /* Look for the `('.  */
3975             if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3976               {
3977                 type = cp_parser_type_id (parser);
3978                 /* Look for the `)'.  */
3979                 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3980                 /* Look for the `{'.  */
3981                 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3982                 /* If things aren't going well, there's no need to
3983                    keep going.  */
3984                 if (!cp_parser_error_occurred (parser))
3985                   {
3986                     /* Parse the initializer-list.  */
3987                     initializer_list 
3988                       = cp_parser_initializer_list (parser);
3989                     /* Allow a trailing `,'.  */
3990                     if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3991                       cp_lexer_consume_token (parser->lexer);
3992                     /* Look for the final `}'.  */
3993                     cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3994                   }
3995               }
3996             /* If that worked, we're definitely looking at a
3997                compound-literal expression.  */
3998             if (cp_parser_parse_definitely (parser))
3999               {
4000                 /* Warn the user that a compound literal is not
4001                    allowed in standard C++.  */
4002                 if (pedantic)
4003                   pedwarn ("ISO C++ forbids compound-literals");
4004                 /* Form the representation of the compound-literal.  */
4005                 postfix_expression 
4006                   = finish_compound_literal (type, initializer_list);
4007                 break;
4008               }
4009           }
4010
4011         /* It must be a primary-expression.  */
4012         postfix_expression = cp_parser_primary_expression (parser, 
4013                                                            &idk,
4014                                                            &qualifying_class);
4015       }
4016       break;
4017     }
4018
4019   /* Peek at the next token.  */
4020   token = cp_lexer_peek_token (parser->lexer);
4021   done = (token->type != CPP_OPEN_SQUARE
4022           && token->type != CPP_OPEN_PAREN
4023           && token->type != CPP_DOT
4024           && token->type != CPP_DEREF
4025           && token->type != CPP_PLUS_PLUS
4026           && token->type != CPP_MINUS_MINUS);
4027
4028   /* If the postfix expression is complete, finish up.  */
4029   if (address_p && qualifying_class && done)
4030     {
4031       if (TREE_CODE (postfix_expression) == SCOPE_REF)
4032         postfix_expression = TREE_OPERAND (postfix_expression, 1);
4033       postfix_expression 
4034         = build_offset_ref (qualifying_class, postfix_expression);
4035       return postfix_expression;
4036     }
4037
4038   /* Otherwise, if we were avoiding committing until we knew
4039      whether or not we had a pointer-to-member, we now know that
4040      the expression is an ordinary reference to a qualified name.  */
4041   if (qualifying_class && !processing_template_decl)
4042     {
4043       if (TREE_CODE (postfix_expression) == FIELD_DECL)
4044         postfix_expression 
4045           = finish_non_static_data_member (postfix_expression,
4046                                            qualifying_class);
4047       else if (BASELINK_P (postfix_expression))
4048         {
4049           tree fn;
4050           tree fns;
4051
4052           /* See if any of the functions are non-static members.  */
4053           fns = BASELINK_FUNCTIONS (postfix_expression);
4054           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4055             fns = TREE_OPERAND (fns, 0);
4056           for (fn = fns; fn; fn = OVL_NEXT (fn))
4057             if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4058               break;
4059           /* If so, the expression may be relative to the current
4060              class.  */
4061           if (fn && current_class_type 
4062               && DERIVED_FROM_P (qualifying_class, current_class_type))
4063             postfix_expression 
4064               = (build_class_member_access_expr 
4065                  (maybe_dummy_object (qualifying_class, NULL),
4066                   postfix_expression,
4067                   BASELINK_ACCESS_BINFO (postfix_expression),
4068                   /*preserve_reference=*/false));
4069           else if (done)
4070             return build_offset_ref (qualifying_class,
4071                                      postfix_expression);
4072         }
4073     }
4074
4075   /* Remember that there was a reference to this entity.  */
4076   if (DECL_P (postfix_expression))
4077     mark_used (postfix_expression);
4078
4079   /* Keep looping until the postfix-expression is complete.  */
4080   while (true)
4081     {
4082       if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4083           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4084         {
4085           /* It is not a Koenig lookup function call.  */
4086           unqualified_name_lookup_error (postfix_expression);
4087           postfix_expression = error_mark_node;
4088         }
4089       
4090       /* Peek at the next token.  */
4091       token = cp_lexer_peek_token (parser->lexer);
4092
4093       switch (token->type)
4094         {
4095         case CPP_OPEN_SQUARE:
4096           /* postfix-expression [ expression ] */
4097           {
4098             tree index;
4099
4100             /* Consume the `[' token.  */
4101             cp_lexer_consume_token (parser->lexer);
4102             /* Parse the index expression.  */
4103             index = cp_parser_expression (parser);
4104             /* Look for the closing `]'.  */
4105             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4106
4107             /* Build the ARRAY_REF.  */
4108             postfix_expression 
4109               = grok_array_decl (postfix_expression, index);
4110             idk = CP_PARSER_ID_KIND_NONE;
4111           }
4112           break;
4113
4114         case CPP_OPEN_PAREN:
4115           /* postfix-expression ( expression-list [opt] ) */
4116           {
4117             tree args;
4118
4119             /* Consume the `(' token.  */
4120             cp_lexer_consume_token (parser->lexer);
4121             /* If the next token is not a `)', then there are some
4122                arguments.  */
4123             if (cp_lexer_next_token_is_not (parser->lexer, 
4124                                             CPP_CLOSE_PAREN))
4125               args = cp_parser_expression_list (parser);
4126             else
4127               args = NULL_TREE;
4128             /* Look for the closing `)'.  */
4129             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4130
4131             if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4132                 && (is_overloaded_fn (postfix_expression)
4133                     || DECL_P (postfix_expression)
4134                     || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4135                 && args)
4136               {
4137                 tree arg;
4138                 tree identifier = NULL_TREE;
4139                 tree functions = NULL_TREE;
4140
4141                 /* Find the name of the overloaded function.  */
4142                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4143                   identifier = postfix_expression;
4144                 else if (is_overloaded_fn (postfix_expression))
4145                   {
4146                     functions = postfix_expression;
4147                     identifier = DECL_NAME (get_first_fn (functions));
4148                   }
4149                 else if (DECL_P (postfix_expression))
4150                   {
4151                     functions = postfix_expression;
4152                     identifier = DECL_NAME (postfix_expression);
4153                   }
4154
4155                 /* A call to a namespace-scope function using an
4156                    unqualified name.
4157
4158                    Do Koenig lookup -- unless any of the arguments are
4159                    type-dependent.  */
4160                 for (arg = args; arg; arg = TREE_CHAIN (arg))
4161                   if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4162                       break;
4163                 if (!arg)
4164                   {
4165                     postfix_expression 
4166                       = lookup_arg_dependent(identifier, functions, args);
4167                     if (!postfix_expression)
4168                       {
4169                         /* The unqualified name could not be resolved.  */
4170                         unqualified_name_lookup_error (identifier);
4171                         postfix_expression = error_mark_node;
4172                       }
4173                     postfix_expression
4174                       = build_call_from_tree (postfix_expression, args, 
4175                                               /*diallow_virtual=*/false);
4176                     break;
4177                   }
4178                 postfix_expression = build_min_nt (LOOKUP_EXPR,
4179                                                    identifier);
4180               }
4181             else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED 
4182                      && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4183               {
4184                 /* The unqualified name could not be resolved.  */
4185                 unqualified_name_lookup_error (postfix_expression);
4186                 postfix_expression = error_mark_node;
4187                 break;
4188               }
4189
4190             /* In the body of a template, no further processing is
4191                required.  */
4192             if (processing_template_decl)
4193               {
4194                 postfix_expression = build_nt (CALL_EXPR,
4195                                                postfix_expression, 
4196                                                args);
4197                 break;
4198               }
4199
4200             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4201               postfix_expression
4202                 = (build_new_method_call 
4203                    (TREE_OPERAND (postfix_expression, 0),
4204                     TREE_OPERAND (postfix_expression, 1),
4205                     args, NULL_TREE, 
4206                     (idk == CP_PARSER_ID_KIND_QUALIFIED 
4207                      ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4208             else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4209               postfix_expression = (build_offset_ref_call_from_tree
4210                                     (postfix_expression, args));
4211             else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4212               {
4213                 /* A call to a static class member, or a
4214                    namespace-scope function.  */
4215                 postfix_expression
4216                   = finish_call_expr (postfix_expression, args,
4217                                       /*disallow_virtual=*/true);
4218               }
4219             else
4220               {
4221                 /* All other function calls.  */
4222                 postfix_expression 
4223                   = finish_call_expr (postfix_expression, args, 
4224                                       /*disallow_virtual=*/false);
4225               }
4226
4227             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4228             idk = CP_PARSER_ID_KIND_NONE;
4229           }
4230           break;
4231           
4232         case CPP_DOT:
4233         case CPP_DEREF:
4234           /* postfix-expression . template [opt] id-expression  
4235              postfix-expression . pseudo-destructor-name 
4236              postfix-expression -> template [opt] id-expression
4237              postfix-expression -> pseudo-destructor-name */
4238           {
4239             tree name;
4240             bool dependent_p;
4241             bool template_p;
4242             tree scope = NULL_TREE;
4243
4244             /* If this is a `->' operator, dereference the pointer.  */
4245             if (token->type == CPP_DEREF)
4246               postfix_expression = build_x_arrow (postfix_expression);
4247             /* Check to see whether or not the expression is
4248                type-dependent.  */
4249             dependent_p = (cp_parser_type_dependent_expression_p 
4250                            (postfix_expression));
4251             /* The identifier following the `->' or `.' is not
4252                qualified.  */
4253             parser->scope = NULL_TREE;
4254             parser->qualifying_scope = NULL_TREE;
4255             parser->object_scope = NULL_TREE;
4256             /* Enter the scope corresponding to the type of the object
4257                given by the POSTFIX_EXPRESSION.  */
4258             if (!dependent_p 
4259                 && TREE_TYPE (postfix_expression) != NULL_TREE)
4260               {
4261                 scope = TREE_TYPE (postfix_expression);
4262                 /* According to the standard, no expression should
4263                    ever have reference type.  Unfortunately, we do not
4264                    currently match the standard in this respect in
4265                    that our internal representation of an expression
4266                    may have reference type even when the standard says
4267                    it does not.  Therefore, we have to manually obtain
4268                    the underlying type here.  */
4269                 if (TREE_CODE (scope) == REFERENCE_TYPE)
4270                   scope = TREE_TYPE (scope);
4271                 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4272                    type of the field.  We get an OFFSET_TYPE for
4273                    something like:
4274
4275                      S::T.a ...
4276
4277                    Probably, we should not get an OFFSET_TYPE here;
4278                    that transformation should be made only if `&S::T'
4279                    is written.  */
4280                 if (TREE_CODE (scope) == OFFSET_TYPE)
4281                   scope = TREE_TYPE (scope);
4282                 /* The type of the POSTFIX_EXPRESSION must be
4283                    complete.  */
4284                 scope = complete_type_or_else (scope, NULL_TREE);
4285                 /* Let the name lookup machinery know that we are
4286                    processing a class member access expression.  */
4287                 parser->context->object_type = scope;
4288                 /* If something went wrong, we want to be able to
4289                    discern that case, as opposed to the case where
4290                    there was no SCOPE due to the type of expression
4291                    being dependent.  */
4292                 if (!scope)
4293                   scope = error_mark_node;
4294               }
4295
4296             /* Consume the `.' or `->' operator.  */
4297             cp_lexer_consume_token (parser->lexer);
4298             /* If the SCOPE is not a scalar type, we are looking at an
4299                ordinary class member access expression, rather than a
4300                pseudo-destructor-name.  */
4301             if (!scope || !SCALAR_TYPE_P (scope))
4302               {
4303                 template_p = cp_parser_optional_template_keyword (parser);
4304                 /* Parse the id-expression.  */
4305                 name = cp_parser_id_expression (parser,
4306                                                 template_p,
4307                                                 /*check_dependency_p=*/true,
4308                                                 /*template_p=*/NULL);
4309                 /* In general, build a SCOPE_REF if the member name is
4310                    qualified.  However, if the name was not dependent
4311                    and has already been resolved; there is no need to
4312                    build the SCOPE_REF.  For example;
4313
4314                      struct X { void f(); };
4315                      template <typename T> void f(T* t) { t->X::f(); }
4316  
4317                    Even though "t" is dependent, "X::f" is not and has 
4318                    except that for a BASELINK there is no need to
4319                    include scope information.  */
4320                 if (name != error_mark_node 
4321                     && !BASELINK_P (name)
4322                     && parser->scope)
4323                   {
4324                     name = build_nt (SCOPE_REF, parser->scope, name);
4325                     parser->scope = NULL_TREE;
4326                     parser->qualifying_scope = NULL_TREE;
4327                     parser->object_scope = NULL_TREE;
4328                   }
4329                 postfix_expression 
4330                   = finish_class_member_access_expr (postfix_expression, name);
4331               }
4332             /* Otherwise, try the pseudo-destructor-name production.  */
4333             else
4334               {
4335                 tree s;
4336                 tree type;
4337
4338                 /* Parse the pseudo-destructor-name.  */
4339                 cp_parser_pseudo_destructor_name (parser, &s, &type);
4340                 /* Form the call.  */
4341                 postfix_expression 
4342                   = finish_pseudo_destructor_expr (postfix_expression,
4343                                                    s, TREE_TYPE (type));
4344               }
4345
4346             /* We no longer need to look up names in the scope of the
4347                object on the left-hand side of the `.' or `->'
4348                operator.  */
4349             parser->context->object_type = NULL_TREE;
4350             idk = CP_PARSER_ID_KIND_NONE;
4351           }
4352           break;
4353
4354         case CPP_PLUS_PLUS:
4355           /* postfix-expression ++  */
4356           /* Consume the `++' token.  */
4357           cp_lexer_consume_token (parser->lexer);
4358           /* Generate a reprsentation for the complete expression.  */
4359           postfix_expression 
4360             = finish_increment_expr (postfix_expression, 
4361                                      POSTINCREMENT_EXPR);
4362           idk = CP_PARSER_ID_KIND_NONE;
4363           break;
4364
4365         case CPP_MINUS_MINUS:
4366           /* postfix-expression -- */
4367           /* Consume the `--' token.  */
4368           cp_lexer_consume_token (parser->lexer);
4369           /* Generate a reprsentation for the complete expression.  */
4370           postfix_expression 
4371             = finish_increment_expr (postfix_expression, 
4372                                      POSTDECREMENT_EXPR);
4373           idk = CP_PARSER_ID_KIND_NONE;
4374           break;
4375
4376         default:
4377           return postfix_expression;
4378         }
4379     }
4380
4381   /* We should never get here.  */
4382   abort ();
4383   return error_mark_node;
4384 }
4385
4386 /* Parse an expression-list.
4387
4388    expression-list:
4389      assignment-expression
4390      expression-list, assignment-expression
4391
4392    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4393    representation of an assignment-expression.  Note that a TREE_LIST
4394    is returned even if there is only a single expression in the list.  */
4395
4396 static tree
4397 cp_parser_expression_list (parser)
4398      cp_parser *parser;
4399 {
4400   tree expression_list = NULL_TREE;
4401
4402   /* Consume expressions until there are no more.  */
4403   while (true)
4404     {
4405       tree expr;
4406
4407       /* Parse the next assignment-expression.  */
4408       expr = cp_parser_assignment_expression (parser);
4409       /* Add it to the list.  */
4410       expression_list = tree_cons (NULL_TREE, expr, expression_list);
4411
4412       /* If the next token isn't a `,', then we are done.  */
4413       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4414         {
4415           /* All uses of expression-list in the grammar are followed
4416              by a `)'.  Therefore, if the next token is not a `)' an
4417              error will be issued, unless we are parsing tentatively.
4418              Skip ahead to see if there is another `,' before the `)';
4419              if so, we can go there and recover.  */
4420           if (cp_parser_parsing_tentatively (parser)
4421               || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4422               || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4423             break;
4424         }
4425
4426       /* Otherwise, consume the `,' and keep going.  */
4427       cp_lexer_consume_token (parser->lexer);
4428     }
4429
4430   /* We built up the list in reverse order so we must reverse it now.  */
4431   return nreverse (expression_list);
4432 }
4433
4434 /* Parse a pseudo-destructor-name.
4435
4436    pseudo-destructor-name:
4437      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4438      :: [opt] nested-name-specifier template template-id :: ~ type-name
4439      :: [opt] nested-name-specifier [opt] ~ type-name
4440
4441    If either of the first two productions is used, sets *SCOPE to the
4442    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4443    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4444    or ERROR_MARK_NODE if no type-name is present.  */
4445
4446 static void
4447 cp_parser_pseudo_destructor_name (parser, scope, type)
4448      cp_parser *parser;
4449      tree *scope;
4450      tree *type;
4451 {
4452   bool nested_name_specifier_p;
4453
4454   /* Look for the optional `::' operator.  */
4455   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4456   /* Look for the optional nested-name-specifier.  */
4457   nested_name_specifier_p 
4458     = (cp_parser_nested_name_specifier_opt (parser,
4459                                             /*typename_keyword_p=*/false,
4460                                             /*check_dependency_p=*/true,
4461                                             /*type_p=*/false) 
4462        != NULL_TREE);
4463   /* Now, if we saw a nested-name-specifier, we might be doing the
4464      second production.  */
4465   if (nested_name_specifier_p 
4466       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4467     {
4468       /* Consume the `template' keyword.  */
4469       cp_lexer_consume_token (parser->lexer);
4470       /* Parse the template-id.  */
4471       cp_parser_template_id (parser, 
4472                              /*template_keyword_p=*/true,
4473                              /*check_dependency_p=*/false);
4474       /* Look for the `::' token.  */
4475       cp_parser_require (parser, CPP_SCOPE, "`::'");
4476     }
4477   /* If the next token is not a `~', then there might be some
4478      additional qualification. */
4479   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4480     {
4481       /* Look for the type-name.  */
4482       *scope = TREE_TYPE (cp_parser_type_name (parser));
4483       /* Look for the `::' token.  */
4484       cp_parser_require (parser, CPP_SCOPE, "`::'");
4485     }
4486   else
4487     *scope = NULL_TREE;
4488
4489   /* Look for the `~'.  */
4490   cp_parser_require (parser, CPP_COMPL, "`~'");
4491   /* Look for the type-name again.  We are not responsible for
4492      checking that it matches the first type-name.  */
4493   *type = cp_parser_type_name (parser);
4494 }
4495
4496 /* Parse a unary-expression.
4497
4498    unary-expression:
4499      postfix-expression
4500      ++ cast-expression
4501      -- cast-expression
4502      unary-operator cast-expression
4503      sizeof unary-expression
4504      sizeof ( type-id )
4505      new-expression
4506      delete-expression
4507
4508    GNU Extensions:
4509
4510    unary-expression:
4511      __extension__ cast-expression
4512      __alignof__ unary-expression
4513      __alignof__ ( type-id )
4514      __real__ cast-expression
4515      __imag__ cast-expression
4516      && identifier
4517
4518    ADDRESS_P is true iff the unary-expression is appearing as the
4519    operand of the `&' operator.
4520
4521    Returns a representation of the expresion.  */
4522
4523 static tree
4524 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4525 {
4526   cp_token *token;
4527   enum tree_code unary_operator;
4528
4529   /* Peek at the next token.  */
4530   token = cp_lexer_peek_token (parser->lexer);
4531   /* Some keywords give away the kind of expression.  */
4532   if (token->type == CPP_KEYWORD)
4533     {
4534       enum rid keyword = token->keyword;
4535
4536       switch (keyword)
4537         {
4538         case RID_ALIGNOF:
4539           {
4540             /* Consume the `alignof' token.  */
4541             cp_lexer_consume_token (parser->lexer);
4542             /* Parse the operand.  */
4543             return finish_alignof (cp_parser_sizeof_operand 
4544                                    (parser, keyword));
4545           }
4546
4547         case RID_SIZEOF:
4548           {
4549             tree operand;
4550             
4551             /* Consume the `sizeof' token.   */
4552             cp_lexer_consume_token (parser->lexer);
4553             /* Parse the operand.  */
4554             operand = cp_parser_sizeof_operand (parser, keyword);
4555
4556             /* If the type of the operand cannot be determined build a
4557                SIZEOF_EXPR.  */
4558             if (TYPE_P (operand)
4559                 ? cp_parser_dependent_type_p (operand)
4560                 : cp_parser_type_dependent_expression_p (operand))
4561               return build_min (SIZEOF_EXPR, size_type_node, operand);
4562             /* Otherwise, compute the constant value.  */
4563             else
4564               return finish_sizeof (operand);
4565           }
4566
4567         case RID_NEW:
4568           return cp_parser_new_expression (parser);
4569
4570         case RID_DELETE:
4571           return cp_parser_delete_expression (parser);
4572           
4573         case RID_EXTENSION:
4574           {
4575             /* The saved value of the PEDANTIC flag.  */
4576             int saved_pedantic;
4577             tree expr;
4578
4579             /* Save away the PEDANTIC flag.  */
4580             cp_parser_extension_opt (parser, &saved_pedantic);
4581             /* Parse the cast-expression.  */
4582             expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4583             /* Restore the PEDANTIC flag.  */
4584             pedantic = saved_pedantic;
4585
4586             return expr;
4587           }
4588
4589         case RID_REALPART:
4590         case RID_IMAGPART:
4591           {
4592             tree expression;
4593
4594             /* Consume the `__real__' or `__imag__' token.  */
4595             cp_lexer_consume_token (parser->lexer);
4596             /* Parse the cast-expression.  */
4597             expression = cp_parser_cast_expression (parser,
4598                                                     /*address_p=*/false);
4599             /* Create the complete representation.  */
4600             return build_x_unary_op ((keyword == RID_REALPART
4601                                       ? REALPART_EXPR : IMAGPART_EXPR),
4602                                      expression);
4603           }
4604           break;
4605
4606         default:
4607           break;
4608         }
4609     }
4610
4611   /* Look for the `:: new' and `:: delete', which also signal the
4612      beginning of a new-expression, or delete-expression,
4613      respectively.  If the next token is `::', then it might be one of
4614      these.  */
4615   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4616     {
4617       enum rid keyword;
4618
4619       /* See if the token after the `::' is one of the keywords in
4620          which we're interested.  */
4621       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4622       /* If it's `new', we have a new-expression.  */
4623       if (keyword == RID_NEW)
4624         return cp_parser_new_expression (parser);
4625       /* Similarly, for `delete'.  */
4626       else if (keyword == RID_DELETE)
4627         return cp_parser_delete_expression (parser);
4628     }
4629
4630   /* Look for a unary operator.  */
4631   unary_operator = cp_parser_unary_operator (token);
4632   /* The `++' and `--' operators can be handled similarly, even though
4633      they are not technically unary-operators in the grammar.  */
4634   if (unary_operator == ERROR_MARK)
4635     {
4636       if (token->type == CPP_PLUS_PLUS)
4637         unary_operator = PREINCREMENT_EXPR;
4638       else if (token->type == CPP_MINUS_MINUS)
4639         unary_operator = PREDECREMENT_EXPR;
4640       /* Handle the GNU address-of-label extension.  */
4641       else if (cp_parser_allow_gnu_extensions_p (parser)
4642                && token->type == CPP_AND_AND)
4643         {
4644           tree identifier;
4645
4646           /* Consume the '&&' token.  */
4647           cp_lexer_consume_token (parser->lexer);
4648           /* Look for the identifier.  */
4649           identifier = cp_parser_identifier (parser);
4650           /* Create an expression representing the address.  */
4651           return finish_label_address_expr (identifier);
4652         }
4653     }
4654   if (unary_operator != ERROR_MARK)
4655     {
4656       tree cast_expression;
4657
4658       /* Consume the operator token.  */
4659       token = cp_lexer_consume_token (parser->lexer);
4660       /* Parse the cast-expression.  */
4661       cast_expression 
4662         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4663       /* Now, build an appropriate representation.  */
4664       switch (unary_operator)
4665         {
4666         case INDIRECT_REF:
4667           return build_x_indirect_ref (cast_expression, "unary *");
4668           
4669         case ADDR_EXPR:
4670           return build_x_unary_op (ADDR_EXPR, cast_expression);
4671           
4672         case CONVERT_EXPR:
4673         case NEGATE_EXPR:
4674         case TRUTH_NOT_EXPR:
4675         case PREINCREMENT_EXPR:
4676         case PREDECREMENT_EXPR:
4677           return finish_unary_op_expr (unary_operator, cast_expression);
4678
4679         case BIT_NOT_EXPR:
4680           return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4681
4682         default:
4683           abort ();
4684           return error_mark_node;
4685         }
4686     }
4687
4688   return cp_parser_postfix_expression (parser, address_p);
4689 }
4690
4691 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4692    unary-operator, the corresponding tree code is returned.  */
4693
4694 static enum tree_code
4695 cp_parser_unary_operator (token)
4696      cp_token *token;
4697 {
4698   switch (token->type)
4699     {
4700     case CPP_MULT:
4701       return INDIRECT_REF;
4702
4703     case CPP_AND:
4704       return ADDR_EXPR;
4705
4706     case CPP_PLUS:
4707       return CONVERT_EXPR;
4708
4709     case CPP_MINUS:
4710       return NEGATE_EXPR;
4711
4712     case CPP_NOT:
4713       return TRUTH_NOT_EXPR;
4714       
4715     case CPP_COMPL:
4716       return BIT_NOT_EXPR;
4717
4718     default:
4719       return ERROR_MARK;
4720     }
4721 }
4722
4723 /* Parse a new-expression.
4724
4725      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4726      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4727
4728    Returns a representation of the expression.  */
4729
4730 static tree
4731 cp_parser_new_expression (parser)
4732      cp_parser *parser;
4733 {
4734   bool global_scope_p;
4735   tree placement;
4736   tree type;
4737   tree initializer;
4738
4739   /* Look for the optional `::' operator.  */
4740   global_scope_p 
4741     = (cp_parser_global_scope_opt (parser,
4742                                    /*current_scope_valid_p=*/false)
4743        != NULL_TREE);
4744   /* Look for the `new' operator.  */
4745   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4746   /* There's no easy way to tell a new-placement from the
4747      `( type-id )' construct.  */
4748   cp_parser_parse_tentatively (parser);
4749   /* Look for a new-placement.  */
4750   placement = cp_parser_new_placement (parser);
4751   /* If that didn't work out, there's no new-placement.  */
4752   if (!cp_parser_parse_definitely (parser))
4753     placement = NULL_TREE;
4754
4755   /* If the next token is a `(', then we have a parenthesized
4756      type-id.  */
4757   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4758     {
4759       /* Consume the `('.  */
4760       cp_lexer_consume_token (parser->lexer);
4761       /* Parse the type-id.  */
4762       type = cp_parser_type_id (parser);
4763       /* Look for the closing `)'.  */
4764       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4765     }
4766   /* Otherwise, there must be a new-type-id.  */
4767   else
4768     type = cp_parser_new_type_id (parser);
4769
4770   /* If the next token is a `(', then we have a new-initializer.  */
4771   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4772     initializer = cp_parser_new_initializer (parser);
4773   else
4774     initializer = NULL_TREE;
4775
4776   /* Create a representation of the new-expression.  */
4777   return build_new (placement, type, initializer, global_scope_p);
4778 }
4779
4780 /* Parse a new-placement.
4781
4782    new-placement:
4783      ( expression-list )
4784
4785    Returns the same representation as for an expression-list.  */
4786
4787 static tree
4788 cp_parser_new_placement (parser)
4789      cp_parser *parser;
4790 {
4791   tree expression_list;
4792
4793   /* Look for the opening `('.  */
4794   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4795     return error_mark_node;
4796   /* Parse the expression-list.  */
4797   expression_list = cp_parser_expression_list (parser);
4798   /* Look for the closing `)'.  */
4799   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4800
4801   return expression_list;
4802 }
4803
4804 /* Parse a new-type-id.
4805
4806    new-type-id:
4807      type-specifier-seq new-declarator [opt]
4808
4809    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4810    and whose TREE_VALUE is the new-declarator.  */
4811
4812 static tree
4813 cp_parser_new_type_id (parser)
4814      cp_parser *parser;
4815 {
4816   tree type_specifier_seq;
4817   tree declarator;
4818   const char *saved_message;
4819
4820   /* The type-specifier sequence must not contain type definitions.
4821      (It cannot contain declarations of new types either, but if they
4822      are not definitions we will catch that because they are not
4823      complete.)  */
4824   saved_message = parser->type_definition_forbidden_message;
4825   parser->type_definition_forbidden_message
4826     = "types may not be defined in a new-type-id";
4827   /* Parse the type-specifier-seq.  */
4828   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4829   /* Restore the old message.  */
4830   parser->type_definition_forbidden_message = saved_message;
4831   /* Parse the new-declarator.  */
4832   declarator = cp_parser_new_declarator_opt (parser);
4833
4834   return build_tree_list (type_specifier_seq, declarator);
4835 }
4836
4837 /* Parse an (optional) new-declarator.
4838
4839    new-declarator:
4840      ptr-operator new-declarator [opt]
4841      direct-new-declarator
4842
4843    Returns a representation of the declarator.  See
4844    cp_parser_declarator for the representations used.  */
4845
4846 static tree
4847 cp_parser_new_declarator_opt (parser)
4848      cp_parser *parser;
4849 {
4850   enum tree_code code;
4851   tree type;
4852   tree cv_qualifier_seq;
4853
4854   /* We don't know if there's a ptr-operator next, or not.  */
4855   cp_parser_parse_tentatively (parser);
4856   /* Look for a ptr-operator.  */
4857   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4858   /* If that worked, look for more new-declarators.  */
4859   if (cp_parser_parse_definitely (parser))
4860     {
4861       tree declarator;
4862
4863       /* Parse another optional declarator.  */
4864       declarator = cp_parser_new_declarator_opt (parser);
4865
4866       /* Create the representation of the declarator.  */
4867       if (code == INDIRECT_REF)
4868         declarator = make_pointer_declarator (cv_qualifier_seq,
4869                                               declarator);
4870       else
4871         declarator = make_reference_declarator (cv_qualifier_seq,
4872                                                 declarator);
4873
4874      /* Handle the pointer-to-member case.  */
4875      if (type)
4876        declarator = build_nt (SCOPE_REF, type, declarator);
4877
4878       return declarator;
4879     }
4880
4881   /* If the next token is a `[', there is a direct-new-declarator.  */
4882   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4883     return cp_parser_direct_new_declarator (parser);
4884
4885   return NULL_TREE;
4886 }
4887
4888 /* Parse a direct-new-declarator.
4889
4890    direct-new-declarator:
4891      [ expression ]
4892      direct-new-declarator [constant-expression]  
4893
4894    Returns an ARRAY_REF, following the same conventions as are
4895    documented for cp_parser_direct_declarator.  */
4896
4897 static tree
4898 cp_parser_direct_new_declarator (parser)
4899      cp_parser *parser;
4900 {
4901   tree declarator = NULL_TREE;
4902
4903   while (true)
4904     {
4905       tree expression;
4906
4907       /* Look for the opening `['.  */
4908       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4909       /* The first expression is not required to be constant.  */
4910       if (!declarator)
4911         {
4912           expression = cp_parser_expression (parser);
4913           /* The standard requires that the expression have integral
4914              type.  DR 74 adds enumeration types.  We believe that the
4915              real intent is that these expressions be handled like the
4916              expression in a `switch' condition, which also allows
4917              classes with a single conversion to integral or
4918              enumeration type.  */
4919           if (!processing_template_decl)
4920             {
4921               expression 
4922                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4923                                               expression,
4924                                               /*complain=*/true);
4925               if (!expression)
4926                 {
4927                   error ("expression in new-declarator must have integral or enumeration type");
4928                   expression = error_mark_node;
4929                 }
4930             }
4931         }
4932       /* But all the other expressions must be.  */
4933       else
4934         expression = cp_parser_constant_expression (parser);
4935       /* Look for the closing `]'.  */
4936       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4937
4938       /* Add this bound to the declarator.  */
4939       declarator = build_nt (ARRAY_REF, declarator, expression);
4940
4941       /* If the next token is not a `[', then there are no more
4942          bounds.  */
4943       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4944         break;
4945     }
4946
4947   return declarator;
4948 }
4949
4950 /* Parse a new-initializer.
4951
4952    new-initializer:
4953      ( expression-list [opt] )
4954
4955    Returns a reprsentation of the expression-list.  If there is no
4956    expression-list, VOID_ZERO_NODE is returned.  */
4957
4958 static tree
4959 cp_parser_new_initializer (parser)
4960      cp_parser *parser;
4961 {
4962   tree expression_list;
4963
4964   /* Look for the opening parenthesis.  */
4965   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4966   /* If the next token is not a `)', then there is an
4967      expression-list.  */
4968   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4969     expression_list = cp_parser_expression_list (parser);
4970   else
4971     expression_list = void_zero_node;
4972   /* Look for the closing parenthesis.  */
4973   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4974
4975   return expression_list;
4976 }
4977
4978 /* Parse a delete-expression.
4979
4980    delete-expression:
4981      :: [opt] delete cast-expression
4982      :: [opt] delete [ ] cast-expression
4983
4984    Returns a representation of the expression.  */
4985
4986 static tree
4987 cp_parser_delete_expression (parser)
4988      cp_parser *parser;
4989 {
4990   bool global_scope_p;
4991   bool array_p;
4992   tree expression;
4993
4994   /* Look for the optional `::' operator.  */
4995   global_scope_p
4996     = (cp_parser_global_scope_opt (parser,
4997                                    /*current_scope_valid_p=*/false)
4998        != NULL_TREE);
4999   /* Look for the `delete' keyword.  */
5000   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5001   /* See if the array syntax is in use.  */
5002   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5003     {
5004       /* Consume the `[' token.  */
5005       cp_lexer_consume_token (parser->lexer);
5006       /* Look for the `]' token.  */
5007       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5008       /* Remember that this is the `[]' construct.  */
5009       array_p = true;
5010     }
5011   else
5012     array_p = false;
5013
5014   /* Parse the cast-expression.  */
5015   expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5016
5017   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5018 }
5019
5020 /* Parse a cast-expression.
5021
5022    cast-expression:
5023      unary-expression
5024      ( type-id ) cast-expression
5025
5026    Returns a representation of the expression.  */
5027
5028 static tree
5029 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5030 {
5031   /* If it's a `(', then we might be looking at a cast.  */
5032   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5033     {
5034       tree type = NULL_TREE;
5035       tree expr = NULL_TREE;
5036       bool compound_literal_p;
5037       const char *saved_message;
5038
5039       /* There's no way to know yet whether or not this is a cast.
5040          For example, `(int (3))' is a unary-expression, while `(int)
5041          3' is a cast.  So, we resort to parsing tentatively.  */
5042       cp_parser_parse_tentatively (parser);
5043       /* Types may not be defined in a cast.  */
5044       saved_message = parser->type_definition_forbidden_message;
5045       parser->type_definition_forbidden_message
5046         = "types may not be defined in casts";
5047       /* Consume the `('.  */
5048       cp_lexer_consume_token (parser->lexer);
5049       /* A very tricky bit is that `(struct S) { 3 }' is a
5050          compound-literal (which we permit in C++ as an extension).
5051          But, that construct is not a cast-expression -- it is a
5052          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5053          is legal; if the compound-literal were a cast-expression,
5054          you'd need an extra set of parentheses.)  But, if we parse
5055          the type-id, and it happens to be a class-specifier, then we
5056          will commit to the parse at that point, because we cannot
5057          undo the action that is done when creating a new class.  So,
5058          then we cannot back up and do a postfix-expression.  
5059
5060          Therefore, we scan ahead to the closing `)', and check to see
5061          if the token after the `)' is a `{'.  If so, we are not
5062          looking at a cast-expression.  
5063
5064          Save tokens so that we can put them back.  */
5065       cp_lexer_save_tokens (parser->lexer);
5066       /* Skip tokens until the next token is a closing parenthesis.
5067          If we find the closing `)', and the next token is a `{', then
5068          we are looking at a compound-literal.  */
5069       compound_literal_p 
5070         = (cp_parser_skip_to_closing_parenthesis (parser)
5071            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5072       /* Roll back the tokens we skipped.  */
5073       cp_lexer_rollback_tokens (parser->lexer);
5074       /* If we were looking at a compound-literal, simulate an error
5075          so that the call to cp_parser_parse_definitely below will
5076          fail.  */
5077       if (compound_literal_p)
5078         cp_parser_simulate_error (parser);
5079       else
5080         {
5081           /* Look for the type-id.  */
5082           type = cp_parser_type_id (parser);
5083           /* Look for the closing `)'.  */
5084           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5085         }
5086
5087       /* Restore the saved message.  */
5088       parser->type_definition_forbidden_message = saved_message;
5089
5090       /* If all went well, this is a cast.  */
5091       if (cp_parser_parse_definitely (parser))
5092         {
5093           /* Parse the dependent expression.  */
5094           expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5095           /* Warn about old-style casts, if so requested.  */
5096           if (warn_old_style_cast 
5097               && !in_system_header 
5098               && !VOID_TYPE_P (type) 
5099               && current_lang_name != lang_name_c)
5100             warning ("use of old-style cast");
5101           /* Perform the cast.  */
5102           expr = build_c_cast (type, expr);
5103         }
5104
5105       if (expr)
5106         return expr;
5107     }
5108
5109   /* If we get here, then it's not a cast, so it must be a
5110      unary-expression.  */
5111   return cp_parser_unary_expression (parser, address_p);
5112 }
5113
5114 /* Parse a pm-expression.
5115
5116    pm-expression:
5117      cast-expression
5118      pm-expression .* cast-expression
5119      pm-expression ->* cast-expression
5120
5121      Returns a representation of the expression.  */
5122
5123 static tree
5124 cp_parser_pm_expression (parser)
5125      cp_parser *parser;
5126 {
5127   tree cast_expr;
5128   tree pm_expr;
5129
5130   /* Parse the cast-expresion.  */
5131   cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5132   pm_expr = cast_expr;
5133   /* Now look for pointer-to-member operators.  */
5134   while (true)
5135     {
5136       cp_token *token;
5137       enum cpp_ttype token_type;
5138
5139       /* Peek at the next token.  */
5140       token = cp_lexer_peek_token (parser->lexer);
5141       token_type = token->type;
5142       /* If it's not `.*' or `->*' there's no pointer-to-member
5143          operation.  */
5144       if (token_type != CPP_DOT_STAR 
5145           && token_type != CPP_DEREF_STAR)
5146         break;
5147
5148       /* Consume the token.  */
5149       cp_lexer_consume_token (parser->lexer);
5150
5151       /* Parse another cast-expression.  */
5152       cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5153
5154       /* Build the representation of the pointer-to-member 
5155          operation.  */
5156       if (token_type == CPP_DEREF_STAR)
5157         pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5158       else
5159         pm_expr = build_m_component_ref (pm_expr, cast_expr);
5160     }
5161
5162   return pm_expr;
5163 }
5164
5165 /* Parse a multiplicative-expression.
5166
5167    mulitplicative-expression:
5168      pm-expression
5169      multiplicative-expression * pm-expression
5170      multiplicative-expression / pm-expression
5171      multiplicative-expression % pm-expression
5172
5173    Returns a representation of the expression.  */
5174
5175 static tree
5176 cp_parser_multiplicative_expression (parser)
5177      cp_parser *parser;
5178 {
5179   static const cp_parser_token_tree_map map = {
5180     { CPP_MULT, MULT_EXPR },
5181     { CPP_DIV, TRUNC_DIV_EXPR },
5182     { CPP_MOD, TRUNC_MOD_EXPR },
5183     { CPP_EOF, ERROR_MARK }
5184   };
5185
5186   return cp_parser_binary_expression (parser,
5187                                       map,
5188                                       cp_parser_pm_expression);
5189 }
5190
5191 /* Parse an additive-expression.
5192
5193    additive-expression:
5194      multiplicative-expression
5195      additive-expression + multiplicative-expression
5196      additive-expression - multiplicative-expression
5197
5198    Returns a representation of the expression.  */
5199
5200 static tree
5201 cp_parser_additive_expression (parser)
5202      cp_parser *parser;
5203 {
5204   static const cp_parser_token_tree_map map = {
5205     { CPP_PLUS, PLUS_EXPR },
5206     { CPP_MINUS, MINUS_EXPR },
5207     { CPP_EOF, ERROR_MARK }
5208   };
5209
5210   return cp_parser_binary_expression (parser,
5211                                       map,
5212                                       cp_parser_multiplicative_expression);
5213 }
5214
5215 /* Parse a shift-expression.
5216
5217    shift-expression:
5218      additive-expression
5219      shift-expression << additive-expression
5220      shift-expression >> additive-expression
5221
5222    Returns a representation of the expression.  */
5223
5224 static tree
5225 cp_parser_shift_expression (parser)
5226      cp_parser *parser;
5227 {
5228   static const cp_parser_token_tree_map map = {
5229     { CPP_LSHIFT, LSHIFT_EXPR },
5230     { CPP_RSHIFT, RSHIFT_EXPR },
5231     { CPP_EOF, ERROR_MARK }
5232   };
5233
5234   return cp_parser_binary_expression (parser,
5235                                       map,
5236                                       cp_parser_additive_expression);
5237 }
5238
5239 /* Parse a relational-expression.
5240
5241    relational-expression:
5242      shift-expression
5243      relational-expression < shift-expression
5244      relational-expression > shift-expression
5245      relational-expression <= shift-expression
5246      relational-expression >= shift-expression
5247
5248    GNU Extension:
5249
5250    relational-expression:
5251      relational-expression <? shift-expression
5252      relational-expression >? shift-expression
5253
5254    Returns a representation of the expression.  */
5255
5256 static tree
5257 cp_parser_relational_expression (parser)
5258      cp_parser *parser;
5259 {
5260   static const cp_parser_token_tree_map map = {
5261     { CPP_LESS, LT_EXPR },
5262     { CPP_GREATER, GT_EXPR },
5263     { CPP_LESS_EQ, LE_EXPR },
5264     { CPP_GREATER_EQ, GE_EXPR },
5265     { CPP_MIN, MIN_EXPR },
5266     { CPP_MAX, MAX_EXPR },
5267     { CPP_EOF, ERROR_MARK }
5268   };
5269
5270   return cp_parser_binary_expression (parser,
5271                                       map,
5272                                       cp_parser_shift_expression);
5273 }
5274
5275 /* Parse an equality-expression.
5276
5277    equality-expression:
5278      relational-expression
5279      equality-expression == relational-expression
5280      equality-expression != relational-expression
5281
5282    Returns a representation of the expression.  */
5283
5284 static tree
5285 cp_parser_equality_expression (parser)
5286      cp_parser *parser;
5287 {
5288   static const cp_parser_token_tree_map map = {
5289     { CPP_EQ_EQ, EQ_EXPR },
5290     { CPP_NOT_EQ, NE_EXPR },
5291     { CPP_EOF, ERROR_MARK }
5292   };
5293
5294   return cp_parser_binary_expression (parser,
5295                                       map,
5296                                       cp_parser_relational_expression);
5297 }
5298
5299 /* Parse an and-expression.
5300
5301    and-expression:
5302      equality-expression
5303      and-expression & equality-expression
5304
5305    Returns a representation of the expression.  */
5306
5307 static tree
5308 cp_parser_and_expression (parser)
5309      cp_parser *parser;
5310 {
5311   static const cp_parser_token_tree_map map = {
5312     { CPP_AND, BIT_AND_EXPR },
5313     { CPP_EOF, ERROR_MARK }
5314   };
5315
5316   return cp_parser_binary_expression (parser,
5317                                       map,
5318                                       cp_parser_equality_expression);
5319 }
5320
5321 /* Parse an exclusive-or-expression.
5322
5323    exclusive-or-expression:
5324      and-expression
5325      exclusive-or-expression ^ and-expression
5326
5327    Returns a representation of the expression.  */
5328
5329 static tree
5330 cp_parser_exclusive_or_expression (parser)
5331      cp_parser *parser;
5332 {
5333   static const cp_parser_token_tree_map map = {
5334     { CPP_XOR, BIT_XOR_EXPR },
5335     { CPP_EOF, ERROR_MARK }
5336   };
5337
5338   return cp_parser_binary_expression (parser,
5339                                       map,
5340                                       cp_parser_and_expression);
5341 }
5342
5343
5344 /* Parse an inclusive-or-expression.
5345
5346    inclusive-or-expression:
5347      exclusive-or-expression
5348      inclusive-or-expression | exclusive-or-expression
5349
5350    Returns a representation of the expression.  */
5351
5352 static tree
5353 cp_parser_inclusive_or_expression (parser)
5354      cp_parser *parser;
5355 {
5356   static const cp_parser_token_tree_map map = {
5357     { CPP_OR, BIT_IOR_EXPR },
5358     { CPP_EOF, ERROR_MARK }
5359   };
5360
5361   return cp_parser_binary_expression (parser,
5362                                       map,
5363                                       cp_parser_exclusive_or_expression);
5364 }
5365
5366 /* Parse a logical-and-expression.
5367
5368    logical-and-expression:
5369      inclusive-or-expression
5370      logical-and-expression && inclusive-or-expression
5371
5372    Returns a representation of the expression.  */
5373
5374 static tree
5375 cp_parser_logical_and_expression (parser)
5376      cp_parser *parser;
5377 {
5378   static const cp_parser_token_tree_map map = {
5379     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5380     { CPP_EOF, ERROR_MARK }
5381   };
5382
5383   return cp_parser_binary_expression (parser,
5384                                       map,
5385                                       cp_parser_inclusive_or_expression);
5386 }
5387
5388 /* Parse a logical-or-expression.
5389
5390    logical-or-expression:
5391      logical-and-expresion
5392      logical-or-expression || logical-and-expression
5393
5394    Returns a representation of the expression.  */
5395
5396 static tree
5397 cp_parser_logical_or_expression (parser)
5398      cp_parser *parser;
5399 {
5400   static const cp_parser_token_tree_map map = {
5401     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5402     { CPP_EOF, ERROR_MARK }
5403   };
5404
5405   return cp_parser_binary_expression (parser,
5406                                       map,
5407                                       cp_parser_logical_and_expression);
5408 }
5409
5410 /* Parse a conditional-expression.
5411
5412    conditional-expression:
5413      logical-or-expression
5414      logical-or-expression ? expression : assignment-expression
5415      
5416    GNU Extensions:
5417    
5418    conditional-expression:
5419      logical-or-expression ?  : assignment-expression
5420
5421    Returns a representation of the expression.  */
5422
5423 static tree
5424 cp_parser_conditional_expression (parser)
5425      cp_parser *parser;
5426 {
5427   tree logical_or_expr;
5428
5429   /* Parse the logical-or-expression.  */
5430   logical_or_expr = cp_parser_logical_or_expression (parser);
5431   /* If the next token is a `?', then we have a real conditional
5432      expression.  */
5433   if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5434     return cp_parser_question_colon_clause (parser, logical_or_expr);
5435   /* Otherwise, the value is simply the logical-or-expression.  */
5436   else
5437     return logical_or_expr;
5438 }
5439
5440 /* Parse the `? expression : assignment-expression' part of a
5441    conditional-expression.  The LOGICAL_OR_EXPR is the
5442    logical-or-expression that started the conditional-expression.
5443    Returns a representation of the entire conditional-expression.
5444
5445    This routine exists only so that it can be shared between
5446    cp_parser_conditional_expression and
5447    cp_parser_assignment_expression.
5448
5449      ? expression : assignment-expression
5450    
5451    GNU Extensions:
5452    
5453      ? : assignment-expression */
5454
5455 static tree
5456 cp_parser_question_colon_clause (parser, logical_or_expr)
5457      cp_parser *parser;
5458      tree logical_or_expr;
5459 {
5460   tree expr;
5461   tree assignment_expr;
5462
5463   /* Consume the `?' token.  */
5464   cp_lexer_consume_token (parser->lexer);
5465   if (cp_parser_allow_gnu_extensions_p (parser)
5466       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5467     /* Implicit true clause.  */
5468     expr = NULL_TREE;
5469   else
5470     /* Parse the expression.  */
5471     expr = cp_parser_expression (parser);
5472   
5473   /* The next token should be a `:'.  */
5474   cp_parser_require (parser, CPP_COLON, "`:'");
5475   /* Parse the assignment-expression.  */
5476   assignment_expr = cp_parser_assignment_expression (parser);
5477
5478   /* Build the conditional-expression.  */
5479   return build_x_conditional_expr (logical_or_expr,
5480                                    expr,
5481                                    assignment_expr);
5482 }
5483
5484 /* Parse an assignment-expression.
5485
5486    assignment-expression:
5487      conditional-expression
5488      logical-or-expression assignment-operator assignment_expression
5489      throw-expression
5490
5491    Returns a representation for the expression.  */
5492
5493 static tree
5494 cp_parser_assignment_expression (parser)
5495      cp_parser *parser;
5496 {
5497   tree expr;
5498
5499   /* If the next token is the `throw' keyword, then we're looking at
5500      a throw-expression.  */
5501   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5502     expr = cp_parser_throw_expression (parser);
5503   /* Otherwise, it must be that we are looking at a
5504      logical-or-expression.  */
5505   else
5506     {
5507       /* Parse the logical-or-expression.  */
5508       expr = cp_parser_logical_or_expression (parser);
5509       /* If the next token is a `?' then we're actually looking at a
5510          conditional-expression.  */
5511       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5512         return cp_parser_question_colon_clause (parser, expr);
5513       else 
5514         {
5515           enum tree_code assignment_operator;
5516
5517           /* If it's an assignment-operator, we're using the second
5518              production.  */
5519           assignment_operator 
5520             = cp_parser_assignment_operator_opt (parser);
5521           if (assignment_operator != ERROR_MARK)
5522             {
5523               tree rhs;
5524
5525               /* Parse the right-hand side of the assignment.  */
5526               rhs = cp_parser_assignment_expression (parser);
5527               /* Build the asignment expression.  */
5528               expr = build_x_modify_expr (expr, 
5529                                           assignment_operator, 
5530                                           rhs);
5531             }
5532         }
5533     }
5534
5535   return expr;
5536 }
5537
5538 /* Parse an (optional) assignment-operator.
5539
5540    assignment-operator: one of 
5541      = *= /= %= += -= >>= <<= &= ^= |=  
5542
5543    GNU Extension:
5544    
5545    assignment-operator: one of
5546      <?= >?=
5547
5548    If the next token is an assignment operator, the corresponding tree
5549    code is returned, and the token is consumed.  For example, for
5550    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5551    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5552    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5553    operator, ERROR_MARK is returned.  */
5554
5555 static enum tree_code
5556 cp_parser_assignment_operator_opt (parser)
5557      cp_parser *parser;
5558 {
5559   enum tree_code op;
5560   cp_token *token;
5561
5562   /* Peek at the next toen.  */
5563   token = cp_lexer_peek_token (parser->lexer);
5564
5565   switch (token->type)
5566     {
5567     case CPP_EQ:
5568       op = NOP_EXPR;
5569       break;
5570
5571     case CPP_MULT_EQ:
5572       op = MULT_EXPR;
5573       break;
5574
5575     case CPP_DIV_EQ:
5576       op = TRUNC_DIV_EXPR;
5577       break;
5578
5579     case CPP_MOD_EQ:
5580       op = TRUNC_MOD_EXPR;
5581       break;
5582
5583     case CPP_PLUS_EQ:
5584       op = PLUS_EXPR;
5585       break;
5586
5587     case CPP_MINUS_EQ:
5588       op = MINUS_EXPR;
5589       break;
5590
5591     case CPP_RSHIFT_EQ:
5592       op = RSHIFT_EXPR;
5593       break;
5594
5595     case CPP_LSHIFT_EQ:
5596       op = LSHIFT_EXPR;
5597       break;
5598
5599     case CPP_AND_EQ:
5600       op = BIT_AND_EXPR;
5601       break;
5602
5603     case CPP_XOR_EQ:
5604       op = BIT_XOR_EXPR;
5605       break;
5606
5607     case CPP_OR_EQ:
5608       op = BIT_IOR_EXPR;
5609       break;
5610
5611     case CPP_MIN_EQ:
5612       op = MIN_EXPR;
5613       break;
5614
5615     case CPP_MAX_EQ:
5616       op = MAX_EXPR;
5617       break;
5618
5619     default: 
5620       /* Nothing else is an assignment operator.  */
5621       op = ERROR_MARK;
5622     }
5623
5624   /* If it was an assignment operator, consume it.  */
5625   if (op != ERROR_MARK)
5626     cp_lexer_consume_token (parser->lexer);
5627
5628   return op;
5629 }
5630
5631 /* Parse an expression.
5632
5633    expression:
5634      assignment-expression
5635      expression , assignment-expression
5636
5637    Returns a representation of the expression.  */
5638
5639 static tree
5640 cp_parser_expression (parser)
5641      cp_parser *parser;
5642 {
5643   tree expression = NULL_TREE;
5644   bool saw_comma_p = false;
5645
5646   while (true)
5647     {
5648       tree assignment_expression;
5649
5650       /* Parse the next assignment-expression.  */
5651       assignment_expression 
5652         = cp_parser_assignment_expression (parser);
5653       /* If this is the first assignment-expression, we can just
5654          save it away.  */
5655       if (!expression)
5656         expression = assignment_expression;
5657       /* Otherwise, chain the expressions together.  It is unclear why
5658          we do not simply build COMPOUND_EXPRs as we go.  */
5659       else
5660         expression = tree_cons (NULL_TREE, 
5661                                 assignment_expression,
5662                                 expression);
5663       /* If the next token is not a comma, then we are done with the
5664          expression.  */
5665       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5666         break;
5667       /* Consume the `,'.  */
5668       cp_lexer_consume_token (parser->lexer);
5669       /* The first time we see a `,', we must take special action
5670          because the representation used for a single expression is
5671          different from that used for a list containing the single
5672          expression.  */
5673       if (!saw_comma_p)
5674         {
5675           /* Remember that this expression has a `,' in it.  */
5676           saw_comma_p = true;
5677           /* Turn the EXPRESSION into a TREE_LIST so that we can link
5678              additional expressions to it.  */
5679           expression = build_tree_list (NULL_TREE, expression);
5680         }
5681     }
5682
5683   /* Build a COMPOUND_EXPR to represent the entire expression, if
5684      necessary.  We built up the list in reverse order, so we must
5685      straighten it out here.  */
5686   if (saw_comma_p)
5687     expression = build_x_compound_expr (nreverse (expression));
5688
5689   return expression;
5690 }
5691
5692 /* Parse a constant-expression. 
5693
5694    constant-expression:
5695      conditional-expression  */
5696
5697 static tree
5698 cp_parser_constant_expression (parser)
5699      cp_parser *parser;
5700 {
5701   bool saved_constant_expression_p;
5702   tree expression;
5703
5704   /* It might seem that we could simply parse the
5705      conditional-expression, and then check to see if it were
5706      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5707      one that the compiler can figure out is constant, possibly after
5708      doing some simplifications or optimizations.  The standard has a
5709      precise definition of constant-expression, and we must honor
5710      that, even though it is somewhat more restrictive.
5711
5712      For example:
5713
5714        int i[(2, 3)];
5715
5716      is not a legal declaration, because `(2, 3)' is not a
5717      constant-expression.  The `,' operator is forbidden in a
5718      constant-expression.  However, GCC's constant-folding machinery
5719      will fold this operation to an INTEGER_CST for `3'.  */
5720
5721   /* Save the old setting of CONSTANT_EXPRESSION_P.  */
5722   saved_constant_expression_p = parser->constant_expression_p;
5723   /* We are now parsing a constant-expression.  */
5724   parser->constant_expression_p = true;
5725   /* Parse the conditional-expression.  */
5726   expression = cp_parser_conditional_expression (parser);
5727   /* Restore the old setting of CONSTANT_EXPRESSION_P.  */
5728   parser->constant_expression_p = saved_constant_expression_p;
5729
5730   return expression;
5731 }
5732
5733 /* Statements [gram.stmt.stmt]  */
5734
5735 /* Parse a statement.  
5736
5737    statement:
5738      labeled-statement
5739      expression-statement
5740      compound-statement
5741      selection-statement
5742      iteration-statement
5743      jump-statement
5744      declaration-statement
5745      try-block  */
5746
5747 static void
5748 cp_parser_statement (parser)
5749      cp_parser *parser;
5750 {
5751   tree statement;
5752   cp_token *token;
5753   int statement_line_number;
5754
5755   /* There is no statement yet.  */
5756   statement = NULL_TREE;
5757   /* Peek at the next token.  */
5758   token = cp_lexer_peek_token (parser->lexer);
5759   /* Remember the line number of the first token in the statement.  */
5760   statement_line_number = token->line_number;
5761   /* If this is a keyword, then that will often determine what kind of
5762      statement we have.  */
5763   if (token->type == CPP_KEYWORD)
5764     {
5765       enum rid keyword = token->keyword;
5766
5767       switch (keyword)
5768         {
5769         case RID_CASE:
5770         case RID_DEFAULT:
5771           statement = cp_parser_labeled_statement (parser);
5772           break;
5773
5774         case RID_IF:
5775         case RID_SWITCH:
5776           statement = cp_parser_selection_statement (parser);
5777           break;
5778
5779         case RID_WHILE:
5780         case RID_DO:
5781         case RID_FOR:
5782           statement = cp_parser_iteration_statement (parser);
5783           break;
5784
5785         case RID_BREAK:
5786         case RID_CONTINUE:
5787         case RID_RETURN:
5788         case RID_GOTO:
5789           statement = cp_parser_jump_statement (parser);
5790           break;
5791
5792         case RID_TRY:
5793           statement = cp_parser_try_block (parser);
5794           break;
5795
5796         default:
5797           /* It might be a keyword like `int' that can start a
5798              declaration-statement.  */
5799           break;
5800         }
5801     }
5802   else if (token->type == CPP_NAME)
5803     {
5804       /* If the next token is a `:', then we are looking at a
5805          labeled-statement.  */
5806       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5807       if (token->type == CPP_COLON)
5808         statement = cp_parser_labeled_statement (parser);
5809     }
5810   /* Anything that starts with a `{' must be a compound-statement.  */
5811   else if (token->type == CPP_OPEN_BRACE)
5812     statement = cp_parser_compound_statement (parser);
5813
5814   /* Everything else must be a declaration-statement or an
5815      expression-statement.  Try for the declaration-statement 
5816      first, unless we are looking at a `;', in which case we know that
5817      we have an expression-statement.  */
5818   if (!statement)
5819     {
5820       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5821         {
5822           cp_parser_parse_tentatively (parser);
5823           /* Try to parse the declaration-statement.  */
5824           cp_parser_declaration_statement (parser);
5825           /* If that worked, we're done.  */
5826           if (cp_parser_parse_definitely (parser))
5827             return;
5828         }
5829       /* Look for an expression-statement instead.  */
5830       statement = cp_parser_expression_statement (parser);
5831     }
5832
5833   /* Set the line number for the statement.  */
5834   if (statement && statement_code_p (TREE_CODE (statement)))
5835     STMT_LINENO (statement) = statement_line_number;
5836 }
5837
5838 /* Parse a labeled-statement.
5839
5840    labeled-statement:
5841      identifier : statement
5842      case constant-expression : statement
5843      default : statement  
5844
5845    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5846    an ordinary label, returns a LABEL_STMT.  */
5847
5848 static tree
5849 cp_parser_labeled_statement (parser)
5850      cp_parser *parser;
5851 {
5852   cp_token *token;
5853   tree statement = NULL_TREE;
5854
5855   /* The next token should be an identifier.  */
5856   token = cp_lexer_peek_token (parser->lexer);
5857   if (token->type != CPP_NAME
5858       && token->type != CPP_KEYWORD)
5859     {
5860       cp_parser_error (parser, "expected labeled-statement");
5861       return error_mark_node;
5862     }
5863
5864   switch (token->keyword)
5865     {
5866     case RID_CASE:
5867       {
5868         tree expr;
5869
5870         /* Consume the `case' token.  */
5871         cp_lexer_consume_token (parser->lexer);
5872         /* Parse the constant-expression.  */
5873         expr = cp_parser_constant_expression (parser);
5874         /* Create the label.  */
5875         statement = finish_case_label (expr, NULL_TREE);
5876       }
5877       break;
5878
5879     case RID_DEFAULT:
5880       /* Consume the `default' token.  */
5881       cp_lexer_consume_token (parser->lexer);
5882       /* Create the label.  */
5883       statement = finish_case_label (NULL_TREE, NULL_TREE);
5884       break;
5885
5886     default:
5887       /* Anything else must be an ordinary label.  */
5888       statement = finish_label_stmt (cp_parser_identifier (parser));
5889       break;
5890     }
5891
5892   /* Require the `:' token.  */
5893   cp_parser_require (parser, CPP_COLON, "`:'");
5894   /* Parse the labeled statement.  */
5895   cp_parser_statement (parser);
5896
5897   /* Return the label, in the case of a `case' or `default' label.  */
5898   return statement;
5899 }
5900
5901 /* Parse an expression-statement.
5902
5903    expression-statement:
5904      expression [opt] ;
5905
5906    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5907    statement consists of nothing more than an `;'.  */
5908
5909 static tree
5910 cp_parser_expression_statement (parser)
5911      cp_parser *parser;
5912 {
5913   tree statement;
5914
5915   /* If the next token is not a `;', then there is an expression to parse.  */
5916   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5917     statement = finish_expr_stmt (cp_parser_expression (parser));
5918   /* Otherwise, we do not even bother to build an EXPR_STMT.  */
5919   else
5920     {
5921       finish_stmt ();
5922       statement = NULL_TREE;
5923     }
5924   /* Consume the final `;'.  */
5925   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5926     {
5927       /* If there is additional (erroneous) input, skip to the end of
5928          the statement.  */
5929       cp_parser_skip_to_end_of_statement (parser);
5930       /* If the next token is now a `;', consume it.  */
5931       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5932         cp_lexer_consume_token (parser->lexer);
5933     }
5934
5935   return statement;
5936 }
5937
5938 /* Parse a compound-statement.
5939
5940    compound-statement:
5941      { statement-seq [opt] }
5942      
5943    Returns a COMPOUND_STMT representing the statement.  */
5944
5945 static tree
5946 cp_parser_compound_statement (cp_parser *parser)
5947 {
5948   tree compound_stmt;
5949
5950   /* Consume the `{'.  */
5951   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5952     return error_mark_node;
5953   /* Begin the compound-statement.  */
5954   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5955   /* Parse an (optional) statement-seq.  */
5956   cp_parser_statement_seq_opt (parser);
5957   /* Finish the compound-statement.  */
5958   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5959   /* Consume the `}'.  */
5960   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5961
5962   return compound_stmt;
5963 }
5964
5965 /* Parse an (optional) statement-seq.
5966
5967    statement-seq:
5968      statement
5969      statement-seq [opt] statement  */
5970
5971 static void
5972 cp_parser_statement_seq_opt (parser)
5973      cp_parser *parser;
5974 {
5975   /* Scan statements until there aren't any more.  */
5976   while (true)
5977     {
5978       /* If we're looking at a `}', then we've run out of statements.  */
5979       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5980           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5981         break;
5982
5983       /* Parse the statement.  */
5984       cp_parser_statement (parser);
5985     }
5986 }
5987
5988 /* Parse a selection-statement.
5989
5990    selection-statement:
5991      if ( condition ) statement
5992      if ( condition ) statement else statement
5993      switch ( condition ) statement  
5994
5995    Returns the new IF_STMT or SWITCH_STMT.  */
5996
5997 static tree
5998 cp_parser_selection_statement (parser)
5999      cp_parser *parser;
6000 {
6001   cp_token *token;
6002   enum rid keyword;
6003
6004   /* Peek at the next token.  */
6005   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6006
6007   /* See what kind of keyword it is.  */
6008   keyword = token->keyword;
6009   switch (keyword)
6010     {
6011     case RID_IF:
6012     case RID_SWITCH:
6013       {
6014         tree statement;
6015         tree condition;
6016
6017         /* Look for the `('.  */
6018         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6019           {
6020             cp_parser_skip_to_end_of_statement (parser);
6021             return error_mark_node;
6022           }
6023
6024         /* Begin the selection-statement.  */
6025         if (keyword == RID_IF)
6026           statement = begin_if_stmt ();
6027         else
6028           statement = begin_switch_stmt ();
6029
6030         /* Parse the condition.  */
6031         condition = cp_parser_condition (parser);
6032         /* Look for the `)'.  */
6033         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6034           cp_parser_skip_to_closing_parenthesis (parser);
6035
6036         if (keyword == RID_IF)
6037           {
6038             tree then_stmt;
6039
6040             /* Add the condition.  */
6041             finish_if_stmt_cond (condition, statement);
6042
6043             /* Parse the then-clause.  */
6044             then_stmt = cp_parser_implicitly_scoped_statement (parser);
6045             finish_then_clause (statement);
6046
6047             /* If the next token is `else', parse the else-clause.  */
6048             if (cp_lexer_next_token_is_keyword (parser->lexer,
6049                                                 RID_ELSE))
6050               {
6051                 tree else_stmt;
6052
6053                 /* Consume the `else' keyword.  */
6054                 cp_lexer_consume_token (parser->lexer);
6055                 /* Parse the else-clause.  */
6056                 else_stmt 
6057                   = cp_parser_implicitly_scoped_statement (parser);
6058                 finish_else_clause (statement);
6059               }
6060
6061             /* Now we're all done with the if-statement.  */
6062             finish_if_stmt ();
6063           }
6064         else
6065           {
6066             tree body;
6067
6068             /* Add the condition.  */
6069             finish_switch_cond (condition, statement);
6070
6071             /* Parse the body of the switch-statement.  */
6072             body = cp_parser_implicitly_scoped_statement (parser);
6073
6074             /* Now we're all done with the switch-statement.  */
6075             finish_switch_stmt (statement);
6076           }
6077
6078         return statement;
6079       }
6080       break;
6081
6082     default:
6083       cp_parser_error (parser, "expected selection-statement");
6084       return error_mark_node;
6085     }
6086 }
6087
6088 /* Parse a condition. 
6089
6090    condition:
6091      expression
6092      type-specifier-seq declarator = assignment-expression  
6093
6094    GNU Extension:
6095    
6096    condition:
6097      type-specifier-seq declarator asm-specification [opt] 
6098        attributes [opt] = assignment-expression
6099  
6100    Returns the expression that should be tested.  */
6101
6102 static tree
6103 cp_parser_condition (parser)
6104      cp_parser *parser;
6105 {
6106   tree type_specifiers;
6107   const char *saved_message;
6108
6109   /* Try the declaration first.  */
6110   cp_parser_parse_tentatively (parser);
6111   /* New types are not allowed in the type-specifier-seq for a
6112      condition.  */
6113   saved_message = parser->type_definition_forbidden_message;
6114   parser->type_definition_forbidden_message
6115     = "types may not be defined in conditions";
6116   /* Parse the type-specifier-seq.  */
6117   type_specifiers = cp_parser_type_specifier_seq (parser);
6118   /* Restore the saved message.  */
6119   parser->type_definition_forbidden_message = saved_message;
6120   /* If all is well, we might be looking at a declaration.  */
6121   if (!cp_parser_error_occurred (parser))
6122     {
6123       tree decl;
6124       tree asm_specification;
6125       tree attributes;
6126       tree declarator;
6127       tree initializer = NULL_TREE;
6128       
6129       /* Parse the declarator.  */
6130       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6131                                          /*ctor_dtor_or_conv_p=*/NULL);
6132       /* Parse the attributes.  */
6133       attributes = cp_parser_attributes_opt (parser);
6134       /* Parse the asm-specification.  */
6135       asm_specification = cp_parser_asm_specification_opt (parser);
6136       /* If the next token is not an `=', then we might still be
6137          looking at an expression.  For example:
6138          
6139            if (A(a).x)
6140           
6141          looks like a decl-specifier-seq and a declarator -- but then
6142          there is no `=', so this is an expression.  */
6143       cp_parser_require (parser, CPP_EQ, "`='");
6144       /* If we did see an `=', then we are looking at a declaration
6145          for sure.  */
6146       if (cp_parser_parse_definitely (parser))
6147         {
6148           /* Create the declaration.  */
6149           decl = start_decl (declarator, type_specifiers, 
6150                              /*initialized_p=*/true,
6151                              attributes, /*prefix_attributes=*/NULL_TREE);
6152           /* Parse the assignment-expression.  */
6153           initializer = cp_parser_assignment_expression (parser);
6154           
6155           /* Process the initializer.  */
6156           cp_finish_decl (decl, 
6157                           initializer, 
6158                           asm_specification, 
6159                           LOOKUP_ONLYCONVERTING);
6160           
6161           return convert_from_reference (decl);
6162         }
6163     }
6164   /* If we didn't even get past the declarator successfully, we are
6165      definitely not looking at a declaration.  */
6166   else
6167     cp_parser_abort_tentative_parse (parser);
6168
6169   /* Otherwise, we are looking at an expression.  */
6170   return cp_parser_expression (parser);
6171 }
6172
6173 /* Parse an iteration-statement.
6174
6175    iteration-statement:
6176      while ( condition ) statement
6177      do statement while ( expression ) ;
6178      for ( for-init-statement condition [opt] ; expression [opt] )
6179        statement
6180
6181    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6182
6183 static tree
6184 cp_parser_iteration_statement (parser)
6185      cp_parser *parser;
6186 {
6187   cp_token *token;
6188   enum rid keyword;
6189   tree statement;
6190
6191   /* Peek at the next token.  */
6192   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6193   if (!token)
6194     return error_mark_node;
6195
6196   /* See what kind of keyword it is.  */
6197   keyword = token->keyword;
6198   switch (keyword)
6199     {
6200     case RID_WHILE:
6201       {
6202         tree condition;
6203
6204         /* Begin the while-statement.  */
6205         statement = begin_while_stmt ();
6206         /* Look for the `('.  */
6207         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6208         /* Parse the condition.  */
6209         condition = cp_parser_condition (parser);
6210         finish_while_stmt_cond (condition, statement);
6211         /* Look for the `)'.  */
6212         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6213         /* Parse the dependent statement.  */
6214         cp_parser_already_scoped_statement (parser);
6215         /* We're done with the while-statement.  */
6216         finish_while_stmt (statement);
6217       }
6218       break;
6219
6220     case RID_DO:
6221       {
6222         tree expression;
6223
6224         /* Begin the do-statement.  */
6225         statement = begin_do_stmt ();
6226         /* Parse the body of the do-statement.  */
6227         cp_parser_implicitly_scoped_statement (parser);
6228         finish_do_body (statement);
6229         /* Look for the `while' keyword.  */
6230         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6231         /* Look for the `('.  */
6232         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6233         /* Parse the expression.  */
6234         expression = cp_parser_expression (parser);
6235         /* We're done with the do-statement.  */
6236         finish_do_stmt (expression, statement);
6237         /* Look for the `)'.  */
6238         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6239         /* Look for the `;'.  */
6240         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6241       }
6242       break;
6243
6244     case RID_FOR:
6245       {
6246         tree condition = NULL_TREE;
6247         tree expression = NULL_TREE;
6248
6249         /* Begin the for-statement.  */
6250         statement = begin_for_stmt ();
6251         /* Look for the `('.  */
6252         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6253         /* Parse the initialization.  */
6254         cp_parser_for_init_statement (parser);
6255         finish_for_init_stmt (statement);
6256
6257         /* If there's a condition, process it.  */
6258         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6259           condition = cp_parser_condition (parser);
6260         finish_for_cond (condition, statement);
6261         /* Look for the `;'.  */
6262         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6263
6264         /* If there's an expression, process it.  */
6265         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6266           expression = cp_parser_expression (parser);
6267         finish_for_expr (expression, statement);
6268         /* Look for the `)'.  */
6269         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6270
6271         /* Parse the body of the for-statement.  */
6272         cp_parser_already_scoped_statement (parser);
6273
6274         /* We're done with the for-statement.  */
6275         finish_for_stmt (statement);
6276       }
6277       break;
6278
6279     default:
6280       cp_parser_error (parser, "expected iteration-statement");
6281       statement = error_mark_node;
6282       break;
6283     }
6284
6285   return statement;
6286 }
6287
6288 /* Parse a for-init-statement.
6289
6290    for-init-statement:
6291      expression-statement
6292      simple-declaration  */
6293
6294 static void
6295 cp_parser_for_init_statement (parser)
6296      cp_parser *parser;
6297 {
6298   /* If the next token is a `;', then we have an empty
6299      expression-statement.  Gramatically, this is also a
6300      simple-declaration, but an invalid one, because it does not
6301      declare anything.  Therefore, if we did not handle this case
6302      specially, we would issue an error message about an invalid
6303      declaration.  */
6304   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6305     {
6306       /* We're going to speculatively look for a declaration, falling back
6307          to an expression, if necessary.  */
6308       cp_parser_parse_tentatively (parser);
6309       /* Parse the declaration.  */
6310       cp_parser_simple_declaration (parser,
6311                                     /*function_definition_allowed_p=*/false);
6312       /* If the tentative parse failed, then we shall need to look for an
6313          expression-statement.  */
6314       if (cp_parser_parse_definitely (parser))
6315         return;
6316     }
6317
6318   cp_parser_expression_statement (parser);
6319 }
6320
6321 /* Parse a jump-statement.
6322
6323    jump-statement:
6324      break ;
6325      continue ;
6326      return expression [opt] ;
6327      goto identifier ;  
6328
6329    GNU extension:
6330
6331    jump-statement:
6332      goto * expression ;
6333
6334    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6335    GOTO_STMT.  */
6336
6337 static tree
6338 cp_parser_jump_statement (parser)
6339      cp_parser *parser;
6340 {
6341   tree statement = error_mark_node;
6342   cp_token *token;
6343   enum rid keyword;
6344
6345   /* Peek at the next token.  */
6346   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6347   if (!token)
6348     return error_mark_node;
6349
6350   /* See what kind of keyword it is.  */
6351   keyword = token->keyword;
6352   switch (keyword)
6353     {
6354     case RID_BREAK:
6355       statement = finish_break_stmt ();
6356       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6357       break;
6358
6359     case RID_CONTINUE:
6360       statement = finish_continue_stmt ();
6361       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6362       break;
6363
6364     case RID_RETURN:
6365       {
6366         tree expr;
6367
6368         /* If the next token is a `;', then there is no 
6369            expression.  */
6370         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6371           expr = cp_parser_expression (parser);
6372         else
6373           expr = NULL_TREE;
6374         /* Build the return-statement.  */
6375         statement = finish_return_stmt (expr);
6376         /* Look for the final `;'.  */
6377         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6378       }
6379       break;
6380
6381     case RID_GOTO:
6382       /* Create the goto-statement.  */
6383       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6384         {
6385           /* Issue a warning about this use of a GNU extension.  */
6386           if (pedantic)
6387             pedwarn ("ISO C++ forbids computed gotos");
6388           /* Consume the '*' token.  */
6389           cp_lexer_consume_token (parser->lexer);
6390           /* Parse the dependent expression.  */
6391           finish_goto_stmt (cp_parser_expression (parser));
6392         }
6393       else
6394         finish_goto_stmt (cp_parser_identifier (parser));
6395       /* Look for the final `;'.  */
6396       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6397       break;
6398
6399     default:
6400       cp_parser_error (parser, "expected jump-statement");
6401       break;
6402     }
6403
6404   return statement;
6405 }
6406
6407 /* Parse a declaration-statement.
6408
6409    declaration-statement:
6410      block-declaration  */
6411
6412 static void
6413 cp_parser_declaration_statement (parser)
6414      cp_parser *parser;
6415 {
6416   /* Parse the block-declaration.  */
6417   cp_parser_block_declaration (parser, /*statement_p=*/true);
6418
6419   /* Finish off the statement.  */
6420   finish_stmt ();
6421 }
6422
6423 /* Some dependent statements (like `if (cond) statement'), are
6424    implicitly in their own scope.  In other words, if the statement is
6425    a single statement (as opposed to a compound-statement), it is
6426    none-the-less treated as if it were enclosed in braces.  Any
6427    declarations appearing in the dependent statement are out of scope
6428    after control passes that point.  This function parses a statement,
6429    but ensures that is in its own scope, even if it is not a
6430    compound-statement.  
6431
6432    Returns the new statement.  */
6433
6434 static tree
6435 cp_parser_implicitly_scoped_statement (parser)
6436      cp_parser *parser;
6437 {
6438   tree statement;
6439
6440   /* If the token is not a `{', then we must take special action.  */
6441   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6442     {
6443       /* Create a compound-statement.  */
6444       statement = begin_compound_stmt (/*has_no_scope=*/0);
6445       /* Parse the dependent-statement.  */
6446       cp_parser_statement (parser);
6447       /* Finish the dummy compound-statement.  */
6448       finish_compound_stmt (/*has_no_scope=*/0, statement);
6449     }
6450   /* Otherwise, we simply parse the statement directly.  */
6451   else
6452     statement = cp_parser_compound_statement (parser);
6453
6454   /* Return the statement.  */
6455   return statement;
6456 }
6457
6458 /* For some dependent statements (like `while (cond) statement'), we
6459    have already created a scope.  Therefore, even if the dependent
6460    statement is a compound-statement, we do not want to create another
6461    scope.  */
6462
6463 static void
6464 cp_parser_already_scoped_statement (parser)
6465      cp_parser *parser;
6466 {
6467   /* If the token is not a `{', then we must take special action.  */
6468   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6469     {
6470       tree statement;
6471
6472       /* Create a compound-statement.  */
6473       statement = begin_compound_stmt (/*has_no_scope=*/1);
6474       /* Parse the dependent-statement.  */
6475       cp_parser_statement (parser);
6476       /* Finish the dummy compound-statement.  */
6477       finish_compound_stmt (/*has_no_scope=*/1, statement);
6478     }
6479   /* Otherwise, we simply parse the statement directly.  */
6480   else
6481     cp_parser_statement (parser);
6482 }
6483
6484 /* Declarations [gram.dcl.dcl] */
6485
6486 /* Parse an optional declaration-sequence.
6487
6488    declaration-seq:
6489      declaration
6490      declaration-seq declaration  */
6491
6492 static void
6493 cp_parser_declaration_seq_opt (parser)
6494      cp_parser *parser;
6495 {
6496   while (true)
6497     {
6498       cp_token *token;
6499
6500       token = cp_lexer_peek_token (parser->lexer);
6501
6502       if (token->type == CPP_CLOSE_BRACE
6503           || token->type == CPP_EOF)
6504         break;
6505
6506       if (token->type == CPP_SEMICOLON) 
6507         {
6508           /* A declaration consisting of a single semicolon is
6509              invalid.  Allow it unless we're being pedantic.  */
6510           if (pedantic)
6511             pedwarn ("extra `;'");
6512           cp_lexer_consume_token (parser->lexer);
6513           continue;
6514         }
6515
6516       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6517          parser to enter or exit implict `extern "C"' blocks.  */
6518       while (pending_lang_change > 0)
6519         {
6520           push_lang_context (lang_name_c);
6521           --pending_lang_change;
6522         }
6523       while (pending_lang_change < 0)
6524         {
6525           pop_lang_context ();
6526           ++pending_lang_change;
6527         }
6528
6529       /* Parse the declaration itself.  */
6530       cp_parser_declaration (parser);
6531     }
6532 }
6533
6534 /* Parse a declaration.
6535
6536    declaration:
6537      block-declaration
6538      function-definition
6539      template-declaration
6540      explicit-instantiation
6541      explicit-specialization
6542      linkage-specification
6543      namespace-definition    
6544
6545    GNU extension:
6546
6547    declaration:
6548       __extension__ declaration */
6549
6550 static void
6551 cp_parser_declaration (parser)
6552      cp_parser *parser;
6553 {
6554   cp_token token1;
6555   cp_token token2;
6556   int saved_pedantic;
6557
6558   /* Check for the `__extension__' keyword.  */
6559   if (cp_parser_extension_opt (parser, &saved_pedantic))
6560     {
6561       /* Parse the qualified declaration.  */
6562       cp_parser_declaration (parser);
6563       /* Restore the PEDANTIC flag.  */
6564       pedantic = saved_pedantic;
6565
6566       return;
6567     }
6568
6569   /* Try to figure out what kind of declaration is present.  */
6570   token1 = *cp_lexer_peek_token (parser->lexer);
6571   if (token1.type != CPP_EOF)
6572     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6573
6574   /* If the next token is `extern' and the following token is a string
6575      literal, then we have a linkage specification.  */
6576   if (token1.keyword == RID_EXTERN
6577       && cp_parser_is_string_literal (&token2))
6578     cp_parser_linkage_specification (parser);
6579   /* If the next token is `template', then we have either a template
6580      declaration, an explicit instantiation, or an explicit
6581      specialization.  */
6582   else if (token1.keyword == RID_TEMPLATE)
6583     {
6584       /* `template <>' indicates a template specialization.  */
6585       if (token2.type == CPP_LESS
6586           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6587         cp_parser_explicit_specialization (parser);
6588       /* `template <' indicates a template declaration.  */
6589       else if (token2.type == CPP_LESS)
6590         cp_parser_template_declaration (parser, /*member_p=*/false);
6591       /* Anything else must be an explicit instantiation.  */
6592       else
6593         cp_parser_explicit_instantiation (parser);
6594     }
6595   /* If the next token is `export', then we have a template
6596      declaration.  */
6597   else if (token1.keyword == RID_EXPORT)
6598     cp_parser_template_declaration (parser, /*member_p=*/false);
6599   /* If the next token is `extern', 'static' or 'inline' and the one
6600      after that is `template', we have a GNU extended explicit
6601      instantiation directive.  */
6602   else if (cp_parser_allow_gnu_extensions_p (parser)
6603            && (token1.keyword == RID_EXTERN
6604                || token1.keyword == RID_STATIC
6605                || token1.keyword == RID_INLINE)
6606            && token2.keyword == RID_TEMPLATE)
6607     cp_parser_explicit_instantiation (parser);
6608   /* If the next token is `namespace', check for a named or unnamed
6609      namespace definition.  */
6610   else if (token1.keyword == RID_NAMESPACE
6611            && (/* A named namespace definition.  */
6612                (token2.type == CPP_NAME
6613                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6614                     == CPP_OPEN_BRACE))
6615                /* An unnamed namespace definition.  */
6616                || token2.type == CPP_OPEN_BRACE))
6617     cp_parser_namespace_definition (parser);
6618   /* We must have either a block declaration or a function
6619      definition.  */
6620   else
6621     /* Try to parse a block-declaration, or a function-definition.  */
6622     cp_parser_block_declaration (parser, /*statement_p=*/false);
6623 }
6624
6625 /* Parse a block-declaration.  
6626
6627    block-declaration:
6628      simple-declaration
6629      asm-definition
6630      namespace-alias-definition
6631      using-declaration
6632      using-directive  
6633
6634    GNU Extension:
6635
6636    block-declaration:
6637      __extension__ block-declaration 
6638      label-declaration
6639
6640    If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6641    part of a declaration-statement.  */
6642
6643 static void
6644 cp_parser_block_declaration (cp_parser *parser, 
6645                              bool      statement_p)
6646 {
6647   cp_token *token1;
6648   int saved_pedantic;
6649
6650   /* Check for the `__extension__' keyword.  */
6651   if (cp_parser_extension_opt (parser, &saved_pedantic))
6652     {
6653       /* Parse the qualified declaration.  */
6654       cp_parser_block_declaration (parser, statement_p);
6655       /* Restore the PEDANTIC flag.  */
6656       pedantic = saved_pedantic;
6657
6658       return;
6659     }
6660
6661   /* Peek at the next token to figure out which kind of declaration is
6662      present.  */
6663   token1 = cp_lexer_peek_token (parser->lexer);
6664
6665   /* If the next keyword is `asm', we have an asm-definition.  */
6666   if (token1->keyword == RID_ASM)
6667     {
6668       if (statement_p)
6669         cp_parser_commit_to_tentative_parse (parser);
6670       cp_parser_asm_definition (parser);
6671     }
6672   /* If the next keyword is `namespace', we have a
6673      namespace-alias-definition.  */
6674   else if (token1->keyword == RID_NAMESPACE)
6675     cp_parser_namespace_alias_definition (parser);
6676   /* If the next keyword is `using', we have either a
6677      using-declaration or a using-directive.  */
6678   else if (token1->keyword == RID_USING)
6679     {
6680       cp_token *token2;
6681
6682       if (statement_p)
6683         cp_parser_commit_to_tentative_parse (parser);
6684       /* If the token after `using' is `namespace', then we have a
6685          using-directive.  */
6686       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6687       if (token2->keyword == RID_NAMESPACE)
6688         cp_parser_using_directive (parser);
6689       /* Otherwise, it's a using-declaration.  */
6690       else
6691         cp_parser_using_declaration (parser);
6692     }
6693   /* If the next keyword is `__label__' we have a label declaration.  */
6694   else if (token1->keyword == RID_LABEL)
6695     {
6696       if (statement_p)
6697         cp_parser_commit_to_tentative_parse (parser);
6698       cp_parser_label_declaration (parser);
6699     }
6700   /* Anything else must be a simple-declaration.  */
6701   else
6702     cp_parser_simple_declaration (parser, !statement_p);
6703 }
6704
6705 /* Parse a simple-declaration.
6706
6707    simple-declaration:
6708      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6709
6710    init-declarator-list:
6711      init-declarator
6712      init-declarator-list , init-declarator 
6713
6714    If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6715    function-definition as a simple-declaration.   */
6716
6717 static void
6718 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6719      cp_parser *parser;
6720      bool function_definition_allowed_p;
6721 {
6722   tree decl_specifiers;
6723   tree attributes;
6724   tree access_checks;
6725   bool declares_class_or_enum;
6726   bool saw_declarator;
6727
6728   /* Defer access checks until we know what is being declared; the
6729      checks for names appearing in the decl-specifier-seq should be
6730      done as if we were in the scope of the thing being declared.  */
6731   cp_parser_start_deferring_access_checks (parser);
6732   /* Parse the decl-specifier-seq.  We have to keep track of whether
6733      or not the decl-specifier-seq declares a named class or
6734      enumeration type, since that is the only case in which the
6735      init-declarator-list is allowed to be empty.  
6736
6737      [dcl.dcl]
6738
6739      In a simple-declaration, the optional init-declarator-list can be
6740      omitted only when declaring a class or enumeration, that is when
6741      the decl-specifier-seq contains either a class-specifier, an
6742      elaborated-type-specifier, or an enum-specifier.  */
6743   decl_specifiers
6744     = cp_parser_decl_specifier_seq (parser, 
6745                                     CP_PARSER_FLAGS_OPTIONAL,
6746                                     &attributes,
6747                                     &declares_class_or_enum);
6748   /* We no longer need to defer access checks.  */
6749   access_checks = cp_parser_stop_deferring_access_checks (parser);
6750
6751   /* Prevent access checks from being reclaimed by GC.  */
6752   parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6753                                            parser->access_checks_lists);
6754
6755   /* Keep going until we hit the `;' at the end of the simple
6756      declaration.  */
6757   saw_declarator = false;
6758   while (cp_lexer_next_token_is_not (parser->lexer, 
6759                                      CPP_SEMICOLON))
6760     {
6761       cp_token *token;
6762       bool function_definition_p;
6763
6764       saw_declarator = true;
6765       /* Parse the init-declarator.  */
6766       cp_parser_init_declarator (parser, decl_specifiers, attributes,
6767                                  access_checks,
6768                                  function_definition_allowed_p,
6769                                  /*member_p=*/false,
6770                                  &function_definition_p);
6771       /* Handle function definitions specially.  */
6772       if (function_definition_p)
6773         {
6774           /* If the next token is a `,', then we are probably
6775              processing something like:
6776
6777                void f() {}, *p;
6778
6779              which is erroneous.  */
6780           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6781             error ("mixing declarations and function-definitions is forbidden");
6782           /* Otherwise, we're done with the list of declarators.  */
6783           else
6784             {
6785               /* Discard access checks no longer in use. */
6786               parser->access_checks_lists
6787                 = TREE_CHAIN (parser->access_checks_lists);
6788               return;
6789             }
6790         }
6791       /* The next token should be either a `,' or a `;'.  */
6792       token = cp_lexer_peek_token (parser->lexer);
6793       /* If it's a `,', there are more declarators to come.  */
6794       if (token->type == CPP_COMMA)
6795         cp_lexer_consume_token (parser->lexer);
6796       /* If it's a `;', we are done.  */
6797       else if (token->type == CPP_SEMICOLON)
6798         break;
6799       /* Anything else is an error.  */
6800       else
6801         {
6802           cp_parser_error (parser, "expected `,' or `;'");
6803           /* Skip tokens until we reach the end of the statement.  */
6804           cp_parser_skip_to_end_of_statement (parser);
6805           /* Discard access checks no longer in use.  */
6806           parser->access_checks_lists
6807             = TREE_CHAIN (parser->access_checks_lists);
6808           return;
6809         }
6810       /* After the first time around, a function-definition is not
6811          allowed -- even if it was OK at first.  For example:
6812
6813            int i, f() {}
6814
6815          is not valid.  */
6816       function_definition_allowed_p = false;
6817     }
6818
6819   /* Issue an error message if no declarators are present, and the
6820      decl-specifier-seq does not itself declare a class or
6821      enumeration.  */
6822   if (!saw_declarator)
6823     {
6824       if (cp_parser_declares_only_class_p (parser))
6825         shadow_tag (decl_specifiers);
6826       /* Perform any deferred access checks.  */
6827       cp_parser_perform_deferred_access_checks (access_checks);
6828     }
6829
6830   /* Consume the `;'.  */
6831   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6832
6833   /* Mark all the classes that appeared in the decl-specifier-seq as
6834      having received a `;'.  */
6835   note_list_got_semicolon (decl_specifiers);
6836
6837   /* Discard access checks no longer in use.  */
6838   parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6839 }
6840
6841 /* Parse a decl-specifier-seq.
6842
6843    decl-specifier-seq:
6844      decl-specifier-seq [opt] decl-specifier
6845
6846    decl-specifier:
6847      storage-class-specifier
6848      type-specifier
6849      function-specifier
6850      friend
6851      typedef  
6852
6853    GNU Extension:
6854
6855    decl-specifier-seq:
6856      decl-specifier-seq [opt] attributes
6857
6858    Returns a TREE_LIST, giving the decl-specifiers in the order they
6859    appear in the source code.  The TREE_VALUE of each node is the
6860    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6861    TREE_VALUE is simply the correspoding TREE_IDENTIFIER.  For the
6862    representation of a type-specifier, see cp_parser_type_specifier.  
6863
6864    If there are attributes, they will be stored in *ATTRIBUTES,
6865    represented as described above cp_parser_attributes.  
6866
6867    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6868    appears, and the entity that will be a friend is not going to be a
6869    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6870    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6871    friendship is granted might not be a class.  */
6872
6873 static tree
6874 cp_parser_decl_specifier_seq (parser, flags, attributes,
6875                               declares_class_or_enum)
6876      cp_parser *parser;
6877      cp_parser_flags flags;
6878      tree *attributes;
6879      bool *declares_class_or_enum;
6880 {
6881   tree decl_specs = NULL_TREE;
6882   bool friend_p = false;
6883
6884   /* Assume no class or enumeration type is declared.  */
6885   *declares_class_or_enum = false;
6886
6887   /* Assume there are no attributes.  */
6888   *attributes = NULL_TREE;
6889
6890   /* Keep reading specifiers until there are no more to read.  */
6891   while (true)
6892     {
6893       tree decl_spec = NULL_TREE;
6894       bool constructor_p;
6895       cp_token *token;
6896
6897       /* Peek at the next token.  */
6898       token = cp_lexer_peek_token (parser->lexer);
6899       /* Handle attributes.  */
6900       if (token->keyword == RID_ATTRIBUTE)
6901         {
6902           /* Parse the attributes.  */
6903           decl_spec = cp_parser_attributes_opt (parser);
6904           /* Add them to the list.  */
6905           *attributes = chainon (*attributes, decl_spec);
6906           continue;
6907         }
6908       /* If the next token is an appropriate keyword, we can simply
6909          add it to the list.  */
6910       switch (token->keyword)
6911         {
6912         case RID_FRIEND:
6913           /* decl-specifier:
6914                friend  */
6915           friend_p = true;
6916           /* The representation of the specifier is simply the
6917              appropriate TREE_IDENTIFIER node.  */
6918           decl_spec = token->value;
6919           /* Consume the token.  */
6920           cp_lexer_consume_token (parser->lexer);
6921           break;
6922
6923           /* function-specifier:
6924                inline
6925                virtual
6926                explicit  */
6927         case RID_INLINE:
6928         case RID_VIRTUAL:
6929         case RID_EXPLICIT:
6930           decl_spec = cp_parser_function_specifier_opt (parser);
6931           break;
6932           
6933           /* decl-specifier:
6934                typedef  */
6935         case RID_TYPEDEF:
6936           /* The representation of the specifier is simply the
6937              appropriate TREE_IDENTIFIER node.  */
6938           decl_spec = token->value;
6939           /* Consume the token.  */
6940           cp_lexer_consume_token (parser->lexer);
6941           break;
6942
6943           /* storage-class-specifier:
6944                auto
6945                register
6946                static
6947                extern
6948                mutable  
6949
6950              GNU Extension:
6951                thread  */
6952         case RID_AUTO:
6953         case RID_REGISTER:
6954         case RID_STATIC:
6955         case RID_EXTERN:
6956         case RID_MUTABLE:
6957         case RID_THREAD:
6958           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6959           break;
6960           
6961         default:
6962           break;
6963         }
6964
6965       /* Constructors are a special case.  The `S' in `S()' is not a
6966          decl-specifier; it is the beginning of the declarator.  */
6967       constructor_p = (!decl_spec 
6968                        && cp_parser_constructor_declarator_p (parser,
6969                                                               friend_p));
6970
6971       /* If we don't have a DECL_SPEC yet, then we must be looking at
6972          a type-specifier.  */
6973       if (!decl_spec && !constructor_p)
6974         {
6975           bool decl_spec_declares_class_or_enum;
6976           bool is_cv_qualifier;
6977
6978           decl_spec
6979             = cp_parser_type_specifier (parser, flags,
6980                                         friend_p,
6981                                         /*is_declaration=*/true,
6982                                         &decl_spec_declares_class_or_enum,
6983                                         &is_cv_qualifier);
6984
6985           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6986
6987           /* If this type-specifier referenced a user-defined type
6988              (a typedef, class-name, etc.), then we can't allow any
6989              more such type-specifiers henceforth.
6990
6991              [dcl.spec]
6992
6993              The longest sequence of decl-specifiers that could
6994              possibly be a type name is taken as the
6995              decl-specifier-seq of a declaration.  The sequence shall
6996              be self-consistent as described below.
6997
6998              [dcl.type]
6999
7000              As a general rule, at most one type-specifier is allowed
7001              in the complete decl-specifier-seq of a declaration.  The
7002              only exceptions are the following:
7003
7004              -- const or volatile can be combined with any other
7005                 type-specifier. 
7006
7007              -- signed or unsigned can be combined with char, long,
7008                 short, or int.
7009
7010              -- ..
7011
7012              Example:
7013
7014                typedef char* Pc;
7015                void g (const int Pc);
7016
7017              Here, Pc is *not* part of the decl-specifier seq; it's
7018              the declarator.  Therefore, once we see a type-specifier
7019              (other than a cv-qualifier), we forbid any additional
7020              user-defined types.  We *do* still allow things like `int
7021              int' to be considered a decl-specifier-seq, and issue the
7022              error message later.  */
7023           if (decl_spec && !is_cv_qualifier)
7024             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7025         }
7026
7027       /* If we still do not have a DECL_SPEC, then there are no more
7028          decl-specifiers.  */
7029       if (!decl_spec)
7030         {
7031           /* Issue an error message, unless the entire construct was
7032              optional.  */
7033           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7034             {
7035               cp_parser_error (parser, "expected decl specifier");
7036               return error_mark_node;
7037             }
7038
7039           break;
7040         }
7041
7042       /* Add the DECL_SPEC to the list of specifiers.  */
7043       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7044
7045       /* After we see one decl-specifier, further decl-specifiers are
7046          always optional.  */
7047       flags |= CP_PARSER_FLAGS_OPTIONAL;
7048     }
7049
7050   /* We have built up the DECL_SPECS in reverse order.  Return them in
7051      the correct order.  */
7052   return nreverse (decl_specs);
7053 }
7054
7055 /* Parse an (optional) storage-class-specifier. 
7056
7057    storage-class-specifier:
7058      auto
7059      register
7060      static
7061      extern
7062      mutable  
7063
7064    GNU Extension:
7065
7066    storage-class-specifier:
7067      thread
7068
7069    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7070    
7071 static tree
7072 cp_parser_storage_class_specifier_opt (parser)
7073      cp_parser *parser;
7074 {
7075   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7076     {
7077     case RID_AUTO:
7078     case RID_REGISTER:
7079     case RID_STATIC:
7080     case RID_EXTERN:
7081     case RID_MUTABLE:
7082     case RID_THREAD:
7083       /* Consume the token.  */
7084       return cp_lexer_consume_token (parser->lexer)->value;
7085
7086     default:
7087       return NULL_TREE;
7088     }
7089 }
7090
7091 /* Parse an (optional) function-specifier. 
7092
7093    function-specifier:
7094      inline
7095      virtual
7096      explicit
7097
7098    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7099    
7100 static tree
7101 cp_parser_function_specifier_opt (parser)
7102      cp_parser *parser;
7103 {
7104   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7105     {
7106     case RID_INLINE:
7107     case RID_VIRTUAL:
7108     case RID_EXPLICIT:
7109       /* Consume the token.  */
7110       return cp_lexer_consume_token (parser->lexer)->value;
7111
7112     default:
7113       return NULL_TREE;
7114     }
7115 }
7116
7117 /* Parse a linkage-specification.
7118
7119    linkage-specification:
7120      extern string-literal { declaration-seq [opt] }
7121      extern string-literal declaration  */
7122
7123 static void
7124 cp_parser_linkage_specification (parser)
7125      cp_parser *parser;
7126 {
7127   cp_token *token;
7128   tree linkage;
7129
7130   /* Look for the `extern' keyword.  */
7131   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7132
7133   /* Peek at the next token.  */
7134   token = cp_lexer_peek_token (parser->lexer);
7135   /* If it's not a string-literal, then there's a problem.  */
7136   if (!cp_parser_is_string_literal (token))
7137     {
7138       cp_parser_error (parser, "expected language-name");
7139       return;
7140     }
7141   /* Consume the token.  */
7142   cp_lexer_consume_token (parser->lexer);
7143
7144   /* Transform the literal into an identifier.  If the literal is a
7145      wide-character string, or contains embedded NULs, then we can't
7146      handle it as the user wants.  */
7147   if (token->type == CPP_WSTRING
7148       || (strlen (TREE_STRING_POINTER (token->value))
7149           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7150     {
7151       cp_parser_error (parser, "invalid linkage-specification");
7152       /* Assume C++ linkage.  */
7153       linkage = get_identifier ("c++");
7154     }
7155   /* If it's a simple string constant, things are easier.  */
7156   else
7157     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7158
7159   /* We're now using the new linkage.  */
7160   push_lang_context (linkage);
7161
7162   /* If the next token is a `{', then we're using the first
7163      production.  */
7164   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7165     {
7166       /* Consume the `{' token.  */
7167       cp_lexer_consume_token (parser->lexer);
7168       /* Parse the declarations.  */
7169       cp_parser_declaration_seq_opt (parser);
7170       /* Look for the closing `}'.  */
7171       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7172     }
7173   /* Otherwise, there's just one declaration.  */
7174   else
7175     {
7176       bool saved_in_unbraced_linkage_specification_p;
7177
7178       saved_in_unbraced_linkage_specification_p 
7179         = parser->in_unbraced_linkage_specification_p;
7180       parser->in_unbraced_linkage_specification_p = true;
7181       have_extern_spec = true;
7182       cp_parser_declaration (parser);
7183       have_extern_spec = false;
7184       parser->in_unbraced_linkage_specification_p 
7185         = saved_in_unbraced_linkage_specification_p;
7186     }
7187
7188   /* We're done with the linkage-specification.  */
7189   pop_lang_context ();
7190 }
7191
7192 /* Special member functions [gram.special] */
7193
7194 /* Parse a conversion-function-id.
7195
7196    conversion-function-id:
7197      operator conversion-type-id  
7198
7199    Returns an IDENTIFIER_NODE representing the operator.  */
7200
7201 static tree 
7202 cp_parser_conversion_function_id (parser)
7203      cp_parser *parser;
7204 {
7205   tree type;
7206   tree saved_scope;
7207   tree saved_qualifying_scope;
7208   tree saved_object_scope;
7209
7210   /* Look for the `operator' token.  */
7211   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7212     return error_mark_node;
7213   /* When we parse the conversion-type-id, the current scope will be
7214      reset.  However, we need that information in able to look up the
7215      conversion function later, so we save it here.  */
7216   saved_scope = parser->scope;
7217   saved_qualifying_scope = parser->qualifying_scope;
7218   saved_object_scope = parser->object_scope;
7219   /* We must enter the scope of the class so that the names of
7220      entities declared within the class are available in the
7221      conversion-type-id.  For example, consider:
7222
7223        struct S { 
7224          typedef int I;
7225          operator I();
7226        };
7227
7228        S::operator I() { ... }
7229
7230      In order to see that `I' is a type-name in the definition, we
7231      must be in the scope of `S'.  */
7232   if (saved_scope)
7233     push_scope (saved_scope);
7234   /* Parse the conversion-type-id.  */
7235   type = cp_parser_conversion_type_id (parser);
7236   /* Leave the scope of the class, if any.  */
7237   if (saved_scope)
7238     pop_scope (saved_scope);
7239   /* Restore the saved scope.  */
7240   parser->scope = saved_scope;
7241   parser->qualifying_scope = saved_qualifying_scope;
7242   parser->object_scope = saved_object_scope;
7243   /* If the TYPE is invalid, indicate failure.  */
7244   if (type == error_mark_node)
7245     return error_mark_node;
7246   return mangle_conv_op_name_for_type (type);
7247 }
7248
7249 /* Parse a conversion-type-id:
7250
7251    conversion-type-id:
7252      type-specifier-seq conversion-declarator [opt]
7253
7254    Returns the TYPE specified.  */
7255
7256 static tree
7257 cp_parser_conversion_type_id (parser)
7258      cp_parser *parser;
7259 {
7260   tree attributes;
7261   tree type_specifiers;
7262   tree declarator;
7263
7264   /* Parse the attributes.  */
7265   attributes = cp_parser_attributes_opt (parser);
7266   /* Parse the type-specifiers.  */
7267   type_specifiers = cp_parser_type_specifier_seq (parser);
7268   /* If that didn't work, stop.  */
7269   if (type_specifiers == error_mark_node)
7270     return error_mark_node;
7271   /* Parse the conversion-declarator.  */
7272   declarator = cp_parser_conversion_declarator_opt (parser);
7273
7274   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7275                          /*initialized=*/0, &attributes);
7276 }
7277
7278 /* Parse an (optional) conversion-declarator.
7279
7280    conversion-declarator:
7281      ptr-operator conversion-declarator [opt]  
7282
7283    Returns a representation of the declarator.  See
7284    cp_parser_declarator for details.  */
7285
7286 static tree
7287 cp_parser_conversion_declarator_opt (parser)
7288      cp_parser *parser;
7289 {
7290   enum tree_code code;
7291   tree class_type;
7292   tree cv_qualifier_seq;
7293
7294   /* We don't know if there's a ptr-operator next, or not.  */
7295   cp_parser_parse_tentatively (parser);
7296   /* Try the ptr-operator.  */
7297   code = cp_parser_ptr_operator (parser, &class_type, 
7298                                  &cv_qualifier_seq);
7299   /* If it worked, look for more conversion-declarators.  */
7300   if (cp_parser_parse_definitely (parser))
7301     {
7302      tree declarator;
7303
7304      /* Parse another optional declarator.  */
7305      declarator = cp_parser_conversion_declarator_opt (parser);
7306
7307      /* Create the representation of the declarator.  */
7308      if (code == INDIRECT_REF)
7309        declarator = make_pointer_declarator (cv_qualifier_seq,
7310                                              declarator);
7311      else
7312        declarator =  make_reference_declarator (cv_qualifier_seq,
7313                                                 declarator);
7314
7315      /* Handle the pointer-to-member case.  */
7316      if (class_type)
7317        declarator = build_nt (SCOPE_REF, class_type, declarator);
7318
7319      return declarator;
7320    }
7321
7322   return NULL_TREE;
7323 }
7324
7325 /* Parse an (optional) ctor-initializer.
7326
7327    ctor-initializer:
7328      : mem-initializer-list  
7329
7330    Returns TRUE iff the ctor-initializer was actually present.  */
7331
7332 static bool
7333 cp_parser_ctor_initializer_opt (parser)
7334      cp_parser *parser;
7335 {
7336   /* If the next token is not a `:', then there is no
7337      ctor-initializer.  */
7338   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7339     {
7340       /* Do default initialization of any bases and members.  */
7341       if (DECL_CONSTRUCTOR_P (current_function_decl))
7342         finish_mem_initializers (NULL_TREE);
7343
7344       return false;
7345     }
7346
7347   /* Consume the `:' token.  */
7348   cp_lexer_consume_token (parser->lexer);
7349   /* And the mem-initializer-list.  */
7350   cp_parser_mem_initializer_list (parser);
7351
7352   return true;
7353 }
7354
7355 /* Parse a mem-initializer-list.
7356
7357    mem-initializer-list:
7358      mem-initializer
7359      mem-initializer , mem-initializer-list  */
7360
7361 static void
7362 cp_parser_mem_initializer_list (parser)
7363      cp_parser *parser;
7364 {
7365   tree mem_initializer_list = NULL_TREE;
7366
7367   /* Let the semantic analysis code know that we are starting the
7368      mem-initializer-list.  */
7369   begin_mem_initializers ();
7370
7371   /* Loop through the list.  */
7372   while (true)
7373     {
7374       tree mem_initializer;
7375
7376       /* Parse the mem-initializer.  */
7377       mem_initializer = cp_parser_mem_initializer (parser);
7378       /* Add it to the list, unless it was erroneous.  */
7379       if (mem_initializer)
7380         {
7381           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7382           mem_initializer_list = mem_initializer;
7383         }
7384       /* If the next token is not a `,', we're done.  */
7385       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7386         break;
7387       /* Consume the `,' token.  */
7388       cp_lexer_consume_token (parser->lexer);
7389     }
7390
7391   /* Perform semantic analysis.  */
7392   finish_mem_initializers (mem_initializer_list);
7393 }
7394
7395 /* Parse a mem-initializer.
7396
7397    mem-initializer:
7398      mem-initializer-id ( expression-list [opt] )  
7399
7400    GNU extension:
7401   
7402    mem-initializer:
7403      ( expresion-list [opt] )
7404
7405    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7406    class) or FIELD_DECL (for a non-static data member) to initialize;
7407    the TREE_VALUE is the expression-list.  */
7408
7409 static tree
7410 cp_parser_mem_initializer (parser)
7411      cp_parser *parser;
7412 {
7413   tree mem_initializer_id;
7414   tree expression_list;
7415
7416   /* Find out what is being initialized.  */
7417   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7418     {
7419       pedwarn ("anachronistic old-style base class initializer");
7420       mem_initializer_id = NULL_TREE;
7421     }
7422   else
7423     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7424   /* Look for the opening `('.  */
7425   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7426   /* Parse the expression-list.  */
7427   if (cp_lexer_next_token_is_not (parser->lexer,
7428                                   CPP_CLOSE_PAREN))
7429     expression_list = cp_parser_expression_list (parser);
7430   else
7431     expression_list = void_type_node;
7432   /* Look for the closing `)'.  */
7433   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7434
7435   return expand_member_init (mem_initializer_id,
7436                              expression_list);
7437 }
7438
7439 /* Parse a mem-initializer-id.
7440
7441    mem-initializer-id:
7442      :: [opt] nested-name-specifier [opt] class-name
7443      identifier  
7444
7445    Returns a TYPE indicating the class to be initializer for the first
7446    production.  Returns an IDENTIFIER_NODE indicating the data member
7447    to be initialized for the second production.  */
7448
7449 static tree
7450 cp_parser_mem_initializer_id (parser)
7451      cp_parser *parser;
7452 {
7453   bool global_scope_p;
7454   bool nested_name_specifier_p;
7455   tree id;
7456
7457   /* Look for the optional `::' operator.  */
7458   global_scope_p 
7459     = (cp_parser_global_scope_opt (parser, 
7460                                    /*current_scope_valid_p=*/false) 
7461        != NULL_TREE);
7462   /* Look for the optional nested-name-specifier.  The simplest way to
7463      implement:
7464
7465        [temp.res]
7466
7467        The keyword `typename' is not permitted in a base-specifier or
7468        mem-initializer; in these contexts a qualified name that
7469        depends on a template-parameter is implicitly assumed to be a
7470        type name.
7471
7472      is to assume that we have seen the `typename' keyword at this
7473      point.  */
7474   nested_name_specifier_p 
7475     = (cp_parser_nested_name_specifier_opt (parser,
7476                                             /*typename_keyword_p=*/true,
7477                                             /*check_dependency_p=*/true,
7478                                             /*type_p=*/true)
7479        != NULL_TREE);
7480   /* If there is a `::' operator or a nested-name-specifier, then we
7481      are definitely looking for a class-name.  */
7482   if (global_scope_p || nested_name_specifier_p)
7483     return cp_parser_class_name (parser,
7484                                  /*typename_keyword_p=*/true,
7485                                  /*template_keyword_p=*/false,
7486                                  /*type_p=*/false,
7487                                  /*check_access_p=*/true,
7488                                  /*check_dependency_p=*/true,
7489                                  /*class_head_p=*/false);
7490   /* Otherwise, we could also be looking for an ordinary identifier.  */
7491   cp_parser_parse_tentatively (parser);
7492   /* Try a class-name.  */
7493   id = cp_parser_class_name (parser, 
7494                              /*typename_keyword_p=*/true,
7495                              /*template_keyword_p=*/false,
7496                              /*type_p=*/false,
7497                              /*check_access_p=*/true,
7498                              /*check_dependency_p=*/true,
7499                              /*class_head_p=*/false);
7500   /* If we found one, we're done.  */
7501   if (cp_parser_parse_definitely (parser))
7502     return id;
7503   /* Otherwise, look for an ordinary identifier.  */
7504   return cp_parser_identifier (parser);
7505 }
7506
7507 /* Overloading [gram.over] */
7508
7509 /* Parse an operator-function-id.
7510
7511    operator-function-id:
7512      operator operator  
7513
7514    Returns an IDENTIFIER_NODE for the operator which is a
7515    human-readable spelling of the identifier, e.g., `operator +'.  */
7516
7517 static tree 
7518 cp_parser_operator_function_id (parser)
7519      cp_parser *parser;
7520 {
7521   /* Look for the `operator' keyword.  */
7522   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7523     return error_mark_node;
7524   /* And then the name of the operator itself.  */
7525   return cp_parser_operator (parser);
7526 }
7527
7528 /* Parse an operator.
7529
7530    operator:
7531      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7532      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7533      || ++ -- , ->* -> () []
7534
7535    GNU Extensions:
7536    
7537    operator:
7538      <? >? <?= >?=
7539
7540    Returns an IDENTIFIER_NODE for the operator which is a
7541    human-readable spelling of the identifier, e.g., `operator +'.  */
7542    
7543 static tree
7544 cp_parser_operator (parser)
7545      cp_parser *parser;
7546 {
7547   tree id = NULL_TREE;
7548   cp_token *token;
7549
7550   /* Peek at the next token.  */
7551   token = cp_lexer_peek_token (parser->lexer);
7552   /* Figure out which operator we have.  */
7553   switch (token->type)
7554     {
7555     case CPP_KEYWORD:
7556       {
7557         enum tree_code op;
7558
7559         /* The keyword should be either `new' or `delete'.  */
7560         if (token->keyword == RID_NEW)
7561           op = NEW_EXPR;
7562         else if (token->keyword == RID_DELETE)
7563           op = DELETE_EXPR;
7564         else
7565           break;
7566
7567         /* Consume the `new' or `delete' token.  */
7568         cp_lexer_consume_token (parser->lexer);
7569
7570         /* Peek at the next token.  */
7571         token = cp_lexer_peek_token (parser->lexer);
7572         /* If it's a `[' token then this is the array variant of the
7573            operator.  */
7574         if (token->type == CPP_OPEN_SQUARE)
7575           {
7576             /* Consume the `[' token.  */
7577             cp_lexer_consume_token (parser->lexer);
7578             /* Look for the `]' token.  */
7579             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7580             id = ansi_opname (op == NEW_EXPR 
7581                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7582           }
7583         /* Otherwise, we have the non-array variant.  */
7584         else
7585           id = ansi_opname (op);
7586
7587         return id;
7588       }
7589
7590     case CPP_PLUS:
7591       id = ansi_opname (PLUS_EXPR);
7592       break;
7593
7594     case CPP_MINUS:
7595       id = ansi_opname (MINUS_EXPR);
7596       break;
7597
7598     case CPP_MULT:
7599       id = ansi_opname (MULT_EXPR);
7600       break;
7601
7602     case CPP_DIV:
7603       id = ansi_opname (TRUNC_DIV_EXPR);
7604       break;
7605
7606     case CPP_MOD:
7607       id = ansi_opname (TRUNC_MOD_EXPR);
7608       break;
7609
7610     case CPP_XOR:
7611       id = ansi_opname (BIT_XOR_EXPR);
7612       break;
7613
7614     case CPP_AND:
7615       id = ansi_opname (BIT_AND_EXPR);
7616       break;
7617
7618     case CPP_OR:
7619       id = ansi_opname (BIT_IOR_EXPR);
7620       break;
7621
7622     case CPP_COMPL:
7623       id = ansi_opname (BIT_NOT_EXPR);
7624       break;
7625       
7626     case CPP_NOT:
7627       id = ansi_opname (TRUTH_NOT_EXPR);
7628       break;
7629
7630     case CPP_EQ:
7631       id = ansi_assopname (NOP_EXPR);
7632       break;
7633
7634     case CPP_LESS:
7635       id = ansi_opname (LT_EXPR);
7636       break;
7637
7638     case CPP_GREATER:
7639       id = ansi_opname (GT_EXPR);
7640       break;
7641
7642     case CPP_PLUS_EQ:
7643       id = ansi_assopname (PLUS_EXPR);
7644       break;
7645
7646     case CPP_MINUS_EQ:
7647       id = ansi_assopname (MINUS_EXPR);
7648       break;
7649
7650     case CPP_MULT_EQ:
7651       id = ansi_assopname (MULT_EXPR);
7652       break;
7653
7654     case CPP_DIV_EQ:
7655       id = ansi_assopname (TRUNC_DIV_EXPR);
7656       break;
7657
7658     case CPP_MOD_EQ:
7659       id = ansi_assopname (TRUNC_MOD_EXPR);
7660       break;
7661
7662     case CPP_XOR_EQ:
7663       id = ansi_assopname (BIT_XOR_EXPR);
7664       break;
7665
7666     case CPP_AND_EQ:
7667       id = ansi_assopname (BIT_AND_EXPR);
7668       break;
7669
7670     case CPP_OR_EQ:
7671       id = ansi_assopname (BIT_IOR_EXPR);
7672       break;
7673
7674     case CPP_LSHIFT:
7675       id = ansi_opname (LSHIFT_EXPR);
7676       break;
7677
7678     case CPP_RSHIFT:
7679       id = ansi_opname (RSHIFT_EXPR);
7680       break;
7681
7682     case CPP_LSHIFT_EQ:
7683       id = ansi_assopname (LSHIFT_EXPR);
7684       break;
7685
7686     case CPP_RSHIFT_EQ:
7687       id = ansi_assopname (RSHIFT_EXPR);
7688       break;
7689
7690     case CPP_EQ_EQ:
7691       id = ansi_opname (EQ_EXPR);
7692       break;
7693
7694     case CPP_NOT_EQ:
7695       id = ansi_opname (NE_EXPR);
7696       break;
7697
7698     case CPP_LESS_EQ:
7699       id = ansi_opname (LE_EXPR);
7700       break;
7701
7702     case CPP_GREATER_EQ:
7703       id = ansi_opname (GE_EXPR);
7704       break;
7705
7706     case CPP_AND_AND:
7707       id = ansi_opname (TRUTH_ANDIF_EXPR);
7708       break;
7709
7710     case CPP_OR_OR:
7711       id = ansi_opname (TRUTH_ORIF_EXPR);
7712       break;
7713       
7714     case CPP_PLUS_PLUS:
7715       id = ansi_opname (POSTINCREMENT_EXPR);
7716       break;
7717
7718     case CPP_MINUS_MINUS:
7719       id = ansi_opname (PREDECREMENT_EXPR);
7720       break;
7721
7722     case CPP_COMMA:
7723       id = ansi_opname (COMPOUND_EXPR);
7724       break;
7725
7726     case CPP_DEREF_STAR:
7727       id = ansi_opname (MEMBER_REF);
7728       break;
7729
7730     case CPP_DEREF:
7731       id = ansi_opname (COMPONENT_REF);
7732       break;
7733
7734     case CPP_OPEN_PAREN:
7735       /* Consume the `('.  */
7736       cp_lexer_consume_token (parser->lexer);
7737       /* Look for the matching `)'.  */
7738       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7739       return ansi_opname (CALL_EXPR);
7740
7741     case CPP_OPEN_SQUARE:
7742       /* Consume the `['.  */
7743       cp_lexer_consume_token (parser->lexer);
7744       /* Look for the matching `]'.  */
7745       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7746       return ansi_opname (ARRAY_REF);
7747
7748       /* Extensions.  */
7749     case CPP_MIN:
7750       id = ansi_opname (MIN_EXPR);
7751       break;
7752
7753     case CPP_MAX:
7754       id = ansi_opname (MAX_EXPR);
7755       break;
7756
7757     case CPP_MIN_EQ:
7758       id = ansi_assopname (MIN_EXPR);
7759       break;
7760
7761     case CPP_MAX_EQ:
7762       id = ansi_assopname (MAX_EXPR);
7763       break;
7764
7765     default:
7766       /* Anything else is an error.  */
7767       break;
7768     }
7769
7770   /* If we have selected an identifier, we need to consume the
7771      operator token.  */
7772   if (id)
7773     cp_lexer_consume_token (parser->lexer);
7774   /* Otherwise, no valid operator name was present.  */
7775   else
7776     {
7777       cp_parser_error (parser, "expected operator");
7778       id = error_mark_node;
7779     }
7780
7781   return id;
7782 }
7783
7784 /* Parse a template-declaration.
7785
7786    template-declaration:
7787      export [opt] template < template-parameter-list > declaration  
7788
7789    If MEMBER_P is TRUE, this template-declaration occurs within a
7790    class-specifier.  
7791
7792    The grammar rule given by the standard isn't correct.  What
7793    is really meant is:
7794
7795    template-declaration:
7796      export [opt] template-parameter-list-seq 
7797        decl-specifier-seq [opt] init-declarator [opt] ;
7798      export [opt] template-parameter-list-seq 
7799        function-definition
7800
7801    template-parameter-list-seq:
7802      template-parameter-list-seq [opt]
7803      template < template-parameter-list >  */
7804
7805 static void
7806 cp_parser_template_declaration (parser, member_p)
7807      cp_parser *parser;
7808      bool member_p;
7809 {
7810   /* Check for `export'.  */
7811   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7812     {
7813       /* Consume the `export' token.  */
7814       cp_lexer_consume_token (parser->lexer);
7815       /* Warn that we do not support `export'.  */
7816       warning ("keyword `export' not implemented, and will be ignored");
7817     }
7818
7819   cp_parser_template_declaration_after_export (parser, member_p);
7820 }
7821
7822 /* Parse a template-parameter-list.
7823
7824    template-parameter-list:
7825      template-parameter
7826      template-parameter-list , template-parameter
7827
7828    Returns a TREE_LIST.  Each node represents a template parameter.
7829    The nodes are connected via their TREE_CHAINs.  */
7830
7831 static tree
7832 cp_parser_template_parameter_list (parser)
7833      cp_parser *parser;
7834 {
7835   tree parameter_list = NULL_TREE;
7836
7837   while (true)
7838     {
7839       tree parameter;
7840       cp_token *token;
7841
7842       /* Parse the template-parameter.  */
7843       parameter = cp_parser_template_parameter (parser);
7844       /* Add it to the list.  */
7845       parameter_list = process_template_parm (parameter_list,
7846                                               parameter);
7847
7848       /* Peek at the next token.  */
7849       token = cp_lexer_peek_token (parser->lexer);
7850       /* If it's not a `,', we're done.  */
7851       if (token->type != CPP_COMMA)
7852         break;
7853       /* Otherwise, consume the `,' token.  */
7854       cp_lexer_consume_token (parser->lexer);
7855     }
7856
7857   return parameter_list;
7858 }
7859
7860 /* Parse a template-parameter.
7861
7862    template-parameter:
7863      type-parameter
7864      parameter-declaration
7865
7866    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7867    TREE_PURPOSE is the default value, if any.  */
7868
7869 static tree
7870 cp_parser_template_parameter (parser)
7871      cp_parser *parser;
7872 {
7873   cp_token *token;
7874
7875   /* Peek at the next token.  */
7876   token = cp_lexer_peek_token (parser->lexer);
7877   /* If it is `class' or `template', we have a type-parameter.  */
7878   if (token->keyword == RID_TEMPLATE)
7879     return cp_parser_type_parameter (parser);
7880   /* If it is `class' or `typename' we do not know yet whether it is a
7881      type parameter or a non-type parameter.  Consider:
7882
7883        template <typename T, typename T::X X> ...
7884
7885      or:
7886      
7887        template <class C, class D*> ...
7888
7889      Here, the first parameter is a type parameter, and the second is
7890      a non-type parameter.  We can tell by looking at the token after
7891      the identifier -- if it is a `,', `=', or `>' then we have a type
7892      parameter.  */
7893   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7894     {
7895       /* Peek at the token after `class' or `typename'.  */
7896       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7897       /* If it's an identifier, skip it.  */
7898       if (token->type == CPP_NAME)
7899         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7900       /* Now, see if the token looks like the end of a template
7901          parameter.  */
7902       if (token->type == CPP_COMMA 
7903           || token->type == CPP_EQ
7904           || token->type == CPP_GREATER)
7905         return cp_parser_type_parameter (parser);
7906     }
7907
7908   /* Otherwise, it is a non-type parameter.  
7909
7910      [temp.param]
7911
7912      When parsing a default template-argument for a non-type
7913      template-parameter, the first non-nested `>' is taken as the end
7914      of the template parameter-list rather than a greater-than
7915      operator.  */
7916   return 
7917     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7918 }
7919
7920 /* Parse a type-parameter.
7921
7922    type-parameter:
7923      class identifier [opt]
7924      class identifier [opt] = type-id
7925      typename identifier [opt]
7926      typename identifier [opt] = type-id
7927      template < template-parameter-list > class identifier [opt]
7928      template < template-parameter-list > class identifier [opt] 
7929        = id-expression  
7930
7931    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7932    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7933    the declaration of the parameter.  */
7934
7935 static tree
7936 cp_parser_type_parameter (parser)
7937      cp_parser *parser;
7938 {
7939   cp_token *token;
7940   tree parameter;
7941
7942   /* Look for a keyword to tell us what kind of parameter this is.  */
7943   token = cp_parser_require (parser, CPP_KEYWORD, 
7944                              "expected `class', `typename', or `template'");
7945   if (!token)
7946     return error_mark_node;
7947
7948   switch (token->keyword)
7949     {
7950     case RID_CLASS:
7951     case RID_TYPENAME:
7952       {
7953         tree identifier;
7954         tree default_argument;
7955
7956         /* If the next token is an identifier, then it names the
7957            parameter.  */
7958         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7959           identifier = cp_parser_identifier (parser);
7960         else
7961           identifier = NULL_TREE;
7962
7963         /* Create the parameter.  */
7964         parameter = finish_template_type_parm (class_type_node, identifier);
7965
7966         /* If the next token is an `=', we have a default argument.  */
7967         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7968           {
7969             /* Consume the `=' token.  */
7970             cp_lexer_consume_token (parser->lexer);
7971             /* Parse the default-argumen.  */
7972             default_argument = cp_parser_type_id (parser);
7973           }
7974         else
7975           default_argument = NULL_TREE;
7976
7977         /* Create the combined representation of the parameter and the
7978            default argument.  */
7979         parameter = build_tree_list (default_argument, 
7980                                      parameter);
7981       }
7982       break;
7983
7984     case RID_TEMPLATE:
7985       {
7986         tree parameter_list;
7987         tree identifier;
7988         tree default_argument;
7989
7990         /* Look for the `<'.  */
7991         cp_parser_require (parser, CPP_LESS, "`<'");
7992         /* Parse the template-parameter-list.  */
7993         begin_template_parm_list ();
7994         parameter_list 
7995           = cp_parser_template_parameter_list (parser);
7996         parameter_list = end_template_parm_list (parameter_list);
7997         /* Look for the `>'.  */
7998         cp_parser_require (parser, CPP_GREATER, "`>'");
7999         /* Look for the `class' keyword.  */
8000         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8001         /* If the next token is an `=', then there is a
8002            default-argument.  If the next token is a `>', we are at
8003            the end of the parameter-list.  If the next token is a `,',
8004            then we are at the end of this parameter.  */
8005         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8006             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8007             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8008           identifier = cp_parser_identifier (parser);
8009         else
8010           identifier = NULL_TREE;
8011         /* Create the template parameter.  */
8012         parameter = finish_template_template_parm (class_type_node,
8013                                                    identifier);
8014                                                    
8015         /* If the next token is an `=', then there is a
8016            default-argument.  */
8017         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8018           {
8019             /* Consume the `='.  */
8020             cp_lexer_consume_token (parser->lexer);
8021             /* Parse the id-expression.  */
8022             default_argument 
8023               = cp_parser_id_expression (parser,
8024                                          /*template_keyword_p=*/false,
8025                                          /*check_dependency_p=*/true,
8026                                          /*template_p=*/NULL);
8027             /* Look up the name.  */
8028             default_argument 
8029               = cp_parser_lookup_name_simple (parser, default_argument);
8030             /* See if the default argument is valid.  */
8031             default_argument
8032               = check_template_template_default_arg (default_argument);
8033           }
8034         else
8035           default_argument = NULL_TREE;
8036
8037         /* Create the combined representation of the parameter and the
8038            default argument.  */
8039         parameter =  build_tree_list (default_argument, 
8040                                       parameter);
8041       }
8042       break;
8043
8044     default:
8045       /* Anything else is an error.  */
8046       cp_parser_error (parser,
8047                        "expected `class', `typename', or `template'");
8048       parameter = error_mark_node;
8049     }
8050   
8051   return parameter;
8052 }
8053
8054 /* Parse a template-id.
8055
8056    template-id:
8057      template-name < template-argument-list [opt] >
8058
8059    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8060    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8061    returned.  Otherwise, if the template-name names a function, or set
8062    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8063    names a class, returns a TYPE_DECL for the specialization.  
8064
8065    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8066    uninstantiated templates.  */
8067
8068 static tree
8069 cp_parser_template_id (cp_parser *parser, 
8070                        bool template_keyword_p, 
8071                        bool check_dependency_p)
8072 {
8073   tree template;
8074   tree arguments;
8075   tree saved_scope;
8076   tree saved_qualifying_scope;
8077   tree saved_object_scope;
8078   tree template_id;
8079   bool saved_greater_than_is_operator_p;
8080   ptrdiff_t start_of_id;
8081   tree access_check = NULL_TREE;
8082
8083   /* If the next token corresponds to a template-id, there is no need
8084      to reparse it.  */
8085   if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8086     {
8087       tree value;
8088       tree check;
8089
8090       /* Get the stored value.  */
8091       value = cp_lexer_consume_token (parser->lexer)->value;
8092       /* Perform any access checks that were deferred.  */
8093       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8094         cp_parser_defer_access_check (parser, 
8095                                       TREE_PURPOSE (check),
8096                                       TREE_VALUE (check));
8097       /* Return the stored value.  */
8098       return TREE_VALUE (value);
8099     }
8100
8101   /* Remember where the template-id starts.  */
8102   if (cp_parser_parsing_tentatively (parser)
8103       && !cp_parser_committed_to_tentative_parse (parser))
8104     {
8105       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8106       start_of_id = cp_lexer_token_difference (parser->lexer,
8107                                                parser->lexer->first_token,
8108                                                next_token);
8109       access_check = parser->context->deferred_access_checks;
8110     }
8111   else
8112     start_of_id = -1;
8113
8114   /* Parse the template-name.  */
8115   template = cp_parser_template_name (parser, template_keyword_p,
8116                                       check_dependency_p);
8117   if (template == error_mark_node)
8118     return error_mark_node;
8119
8120   /* Look for the `<' that starts the template-argument-list.  */
8121   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8122     return error_mark_node;
8123
8124   /* [temp.names]
8125
8126      When parsing a template-id, the first non-nested `>' is taken as
8127      the end of the template-argument-list rather than a greater-than
8128      operator.  */
8129   saved_greater_than_is_operator_p 
8130     = parser->greater_than_is_operator_p;
8131   parser->greater_than_is_operator_p = false;
8132   /* Parsing the argument list may modify SCOPE, so we save it
8133      here.  */
8134   saved_scope = parser->scope;
8135   saved_qualifying_scope = parser->qualifying_scope;
8136   saved_object_scope = parser->object_scope;
8137   /* Parse the template-argument-list itself.  */
8138   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8139     arguments = NULL_TREE;
8140   else
8141     arguments = cp_parser_template_argument_list (parser);
8142   /* Look for the `>' that ends the template-argument-list.  */
8143   cp_parser_require (parser, CPP_GREATER, "`>'");
8144   /* The `>' token might be a greater-than operator again now.  */
8145   parser->greater_than_is_operator_p 
8146     = saved_greater_than_is_operator_p;
8147   /* Restore the SAVED_SCOPE.  */
8148   parser->scope = saved_scope;
8149   parser->qualifying_scope = saved_qualifying_scope;
8150   parser->object_scope = saved_object_scope;
8151
8152   /* Build a representation of the specialization.  */
8153   if (TREE_CODE (template) == IDENTIFIER_NODE)
8154     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8155   else if (DECL_CLASS_TEMPLATE_P (template)
8156            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8157     template_id 
8158       = finish_template_type (template, arguments, 
8159                               cp_lexer_next_token_is (parser->lexer, 
8160                                                       CPP_SCOPE));
8161   else
8162     {
8163       /* If it's not a class-template or a template-template, it should be
8164          a function-template.  */
8165       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8166                            || TREE_CODE (template) == OVERLOAD
8167                            || BASELINK_P (template)),
8168                           20010716);
8169       
8170       template_id = lookup_template_function (template, arguments);
8171     }
8172   
8173   /* If parsing tentatively, replace the sequence of tokens that makes
8174      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8175      should we re-parse the token stream, we will not have to repeat
8176      the effort required to do the parse, nor will we issue duplicate
8177      error messages about problems during instantiation of the
8178      template.  */
8179   if (start_of_id >= 0)
8180     {
8181       cp_token *token;
8182       tree c;
8183
8184       /* Find the token that corresponds to the start of the
8185          template-id.  */
8186       token = cp_lexer_advance_token (parser->lexer, 
8187                                       parser->lexer->first_token,
8188                                       start_of_id);
8189
8190       /* Remember the access checks associated with this
8191          nested-name-specifier.  */
8192       c = parser->context->deferred_access_checks;
8193       if (c == access_check)
8194         access_check = NULL_TREE;
8195       else
8196         {
8197           while (TREE_CHAIN (c) != access_check)
8198             c = TREE_CHAIN (c);
8199           access_check = parser->context->deferred_access_checks;
8200           parser->context->deferred_access_checks = TREE_CHAIN (c);
8201           TREE_CHAIN (c) = NULL_TREE;
8202         }
8203
8204       /* Reset the contents of the START_OF_ID token.  */
8205       token->type = CPP_TEMPLATE_ID;
8206       token->value = build_tree_list (access_check, template_id);
8207       token->keyword = RID_MAX;
8208       /* Purge all subsequent tokens.  */
8209       cp_lexer_purge_tokens_after (parser->lexer, token);
8210     }
8211
8212   return template_id;
8213 }
8214
8215 /* Parse a template-name.
8216
8217    template-name:
8218      identifier
8219  
8220    The standard should actually say:
8221
8222    template-name:
8223      identifier
8224      operator-function-id
8225      conversion-function-id
8226
8227    A defect report has been filed about this issue.
8228
8229    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8230    `template' keyword, in a construction like:
8231
8232      T::template f<3>()
8233
8234    In that case `f' is taken to be a template-name, even though there
8235    is no way of knowing for sure.
8236
8237    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8238    name refers to a set of overloaded functions, at least one of which
8239    is a template, or an IDENTIFIER_NODE with the name of the template,
8240    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8241    names are looked up inside uninstantiated templates.  */
8242
8243 static tree
8244 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8245      cp_parser *parser;
8246      bool template_keyword_p;
8247      bool check_dependency_p;
8248 {
8249   tree identifier;
8250   tree decl;
8251   tree fns;
8252
8253   /* If the next token is `operator', then we have either an
8254      operator-function-id or a conversion-function-id.  */
8255   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8256     {
8257       /* We don't know whether we're looking at an
8258          operator-function-id or a conversion-function-id.  */
8259       cp_parser_parse_tentatively (parser);
8260       /* Try an operator-function-id.  */
8261       identifier = cp_parser_operator_function_id (parser);
8262       /* If that didn't work, try a conversion-function-id.  */
8263       if (!cp_parser_parse_definitely (parser))
8264         identifier = cp_parser_conversion_function_id (parser);
8265     }
8266   /* Look for the identifier.  */
8267   else
8268     identifier = cp_parser_identifier (parser);
8269   
8270   /* If we didn't find an identifier, we don't have a template-id.  */
8271   if (identifier == error_mark_node)
8272     return error_mark_node;
8273
8274   /* If the name immediately followed the `template' keyword, then it
8275      is a template-name.  However, if the next token is not `<', then
8276      we do not treat it as a template-name, since it is not being used
8277      as part of a template-id.  This enables us to handle constructs
8278      like:
8279
8280        template <typename T> struct S { S(); };
8281        template <typename T> S<T>::S();
8282
8283      correctly.  We would treat `S' as a template -- if it were `S<T>'
8284      -- but we do not if there is no `<'.  */
8285   if (template_keyword_p && processing_template_decl
8286       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8287     return identifier;
8288
8289   /* Look up the name.  */
8290   decl = cp_parser_lookup_name (parser, identifier,
8291                                 /*check_access=*/true,
8292                                 /*is_type=*/false,
8293                                 /*is_namespace=*/false,
8294                                 check_dependency_p);
8295   decl = maybe_get_template_decl_from_type_decl (decl);
8296
8297   /* If DECL is a template, then the name was a template-name.  */
8298   if (TREE_CODE (decl) == TEMPLATE_DECL)
8299     ;
8300   else 
8301     {
8302       /* The standard does not explicitly indicate whether a name that
8303          names a set of overloaded declarations, some of which are
8304          templates, is a template-name.  However, such a name should
8305          be a template-name; otherwise, there is no way to form a
8306          template-id for the overloaded templates.  */
8307       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8308       if (TREE_CODE (fns) == OVERLOAD)
8309         {
8310           tree fn;
8311           
8312           for (fn = fns; fn; fn = OVL_NEXT (fn))
8313             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8314               break;
8315         }
8316       else
8317         {
8318           /* Otherwise, the name does not name a template.  */
8319           cp_parser_error (parser, "expected template-name");
8320           return error_mark_node;
8321         }
8322     }
8323
8324   /* If DECL is dependent, and refers to a function, then just return
8325      its name; we will look it up again during template instantiation.  */
8326   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8327     {
8328       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8329       if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8330         return identifier;
8331     }
8332
8333   return decl;
8334 }
8335
8336 /* Parse a template-argument-list.
8337
8338    template-argument-list:
8339      template-argument
8340      template-argument-list , template-argument
8341
8342    Returns a TREE_LIST representing the arguments, in the order they
8343    appeared.  The TREE_VALUE of each node is a representation of the
8344    argument.  */
8345
8346 static tree
8347 cp_parser_template_argument_list (parser)
8348      cp_parser *parser;
8349 {
8350   tree arguments = NULL_TREE;
8351
8352   while (true)
8353     {
8354       tree argument;
8355
8356       /* Parse the template-argument.  */
8357       argument = cp_parser_template_argument (parser);
8358       /* Add it to the list.  */
8359       arguments = tree_cons (NULL_TREE, argument, arguments);
8360       /* If it is not a `,', then there are no more arguments.  */
8361       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8362         break;
8363       /* Otherwise, consume the ','.  */
8364       cp_lexer_consume_token (parser->lexer);
8365     }
8366
8367   /* We built up the arguments in reverse order.  */
8368   return nreverse (arguments);
8369 }
8370
8371 /* Parse a template-argument.
8372
8373    template-argument:
8374      assignment-expression
8375      type-id
8376      id-expression
8377
8378    The representation is that of an assignment-expression, type-id, or
8379    id-expression -- except that the qualified id-expression is
8380    evaluated, so that the value returned is either a DECL or an
8381    OVERLOAD.  */
8382
8383 static tree
8384 cp_parser_template_argument (parser)
8385      cp_parser *parser;
8386 {
8387   tree argument;
8388   bool template_p;
8389
8390   /* There's really no way to know what we're looking at, so we just
8391      try each alternative in order.  
8392
8393        [temp.arg]
8394
8395        In a template-argument, an ambiguity between a type-id and an
8396        expression is resolved to a type-id, regardless of the form of
8397        the corresponding template-parameter.  
8398
8399      Therefore, we try a type-id first.  */
8400   cp_parser_parse_tentatively (parser);
8401   argument = cp_parser_type_id (parser);
8402   /* If the next token isn't a `,' or a `>', then this argument wasn't
8403      really finished.  */
8404   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8405       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8406     cp_parser_error (parser, "expected template-argument");
8407   /* If that worked, we're done.  */
8408   if (cp_parser_parse_definitely (parser))
8409     return argument;
8410   /* We're still not sure what the argument will be.  */
8411   cp_parser_parse_tentatively (parser);
8412   /* Try a template.  */
8413   argument = cp_parser_id_expression (parser, 
8414                                       /*template_keyword_p=*/false,
8415                                       /*check_dependency_p=*/true,
8416                                       &template_p);
8417   /* If the next token isn't a `,' or a `>', then this argument wasn't
8418      really finished.  */
8419   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8420       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8421     cp_parser_error (parser, "expected template-argument");
8422   if (!cp_parser_error_occurred (parser))
8423     {
8424       /* Figure out what is being referred to.  */
8425       argument = cp_parser_lookup_name_simple (parser, argument);
8426       if (template_p)
8427         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8428                                                 TREE_OPERAND (argument, 1),
8429                                                 tf_error | tf_parsing);
8430       else if (TREE_CODE (argument) != TEMPLATE_DECL)
8431         cp_parser_error (parser, "expected template-name");
8432     }
8433   if (cp_parser_parse_definitely (parser))
8434     return argument;
8435   /* It must be an assignment-expression.  */
8436   return cp_parser_assignment_expression (parser);
8437 }
8438
8439 /* Parse an explicit-instantiation.
8440
8441    explicit-instantiation:
8442      template declaration  
8443
8444    Although the standard says `declaration', what it really means is:
8445
8446    explicit-instantiation:
8447      template decl-specifier-seq [opt] declarator [opt] ; 
8448
8449    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8450    supposed to be allowed.  A defect report has been filed about this
8451    issue.  
8452
8453    GNU Extension:
8454   
8455    explicit-instantiation:
8456      storage-class-specifier template 
8457        decl-specifier-seq [opt] declarator [opt] ;
8458      function-specifier template 
8459        decl-specifier-seq [opt] declarator [opt] ;  */
8460
8461 static void
8462 cp_parser_explicit_instantiation (parser)
8463      cp_parser *parser;
8464 {
8465   bool declares_class_or_enum;
8466   tree decl_specifiers;
8467   tree attributes;
8468   tree extension_specifier = NULL_TREE;
8469
8470   /* Look for an (optional) storage-class-specifier or
8471      function-specifier.  */
8472   if (cp_parser_allow_gnu_extensions_p (parser))
8473     {
8474       extension_specifier 
8475         = cp_parser_storage_class_specifier_opt (parser);
8476       if (!extension_specifier)
8477         extension_specifier = cp_parser_function_specifier_opt (parser);
8478     }
8479
8480   /* Look for the `template' keyword.  */
8481   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8482   /* Let the front end know that we are processing an explicit
8483      instantiation.  */
8484   begin_explicit_instantiation ();
8485   /* [temp.explicit] says that we are supposed to ignore access
8486      control while processing explicit instantiation directives.  */
8487   scope_chain->check_access = 0;
8488   /* Parse a decl-specifier-seq.  */
8489   decl_specifiers 
8490     = cp_parser_decl_specifier_seq (parser,
8491                                     CP_PARSER_FLAGS_OPTIONAL,
8492                                     &attributes,
8493                                     &declares_class_or_enum);
8494   /* If there was exactly one decl-specifier, and it declared a class,
8495      and there's no declarator, then we have an explicit type
8496      instantiation.  */
8497   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8498     {
8499       tree type;
8500
8501       type = check_tag_decl (decl_specifiers);
8502       if (type)
8503         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8504     }
8505   else
8506     {
8507       tree declarator;
8508       tree decl;
8509
8510       /* Parse the declarator.  */
8511       declarator 
8512         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8513                                 /*ctor_dtor_or_conv_p=*/NULL);
8514       decl = grokdeclarator (declarator, decl_specifiers, 
8515                              NORMAL, 0, NULL);
8516       /* Do the explicit instantiation.  */
8517       do_decl_instantiation (decl, extension_specifier);
8518     }
8519   /* We're done with the instantiation.  */
8520   end_explicit_instantiation ();
8521   /* Trun access control back on.  */
8522   scope_chain->check_access = flag_access_control;
8523
8524   /* Look for the trailing `;'.  */
8525   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8526 }
8527
8528 /* Parse an explicit-specialization.
8529
8530    explicit-specialization:
8531      template < > declaration  
8532
8533    Although the standard says `declaration', what it really means is:
8534
8535    explicit-specialization:
8536      template <> decl-specifier [opt] init-declarator [opt] ;
8537      template <> function-definition 
8538      template <> explicit-specialization
8539      template <> template-declaration  */
8540
8541 static void
8542 cp_parser_explicit_specialization (parser)
8543      cp_parser *parser;
8544 {
8545   /* Look for the `template' keyword.  */
8546   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8547   /* Look for the `<'.  */
8548   cp_parser_require (parser, CPP_LESS, "`<'");
8549   /* Look for the `>'.  */
8550   cp_parser_require (parser, CPP_GREATER, "`>'");
8551   /* We have processed another parameter list.  */
8552   ++parser->num_template_parameter_lists;
8553   /* Let the front end know that we are beginning a specialization.  */
8554   begin_specialization ();
8555
8556   /* If the next keyword is `template', we need to figure out whether
8557      or not we're looking a template-declaration.  */
8558   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8559     {
8560       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8561           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8562         cp_parser_template_declaration_after_export (parser,
8563                                                      /*member_p=*/false);
8564       else
8565         cp_parser_explicit_specialization (parser);
8566     }
8567   else
8568     /* Parse the dependent declaration.  */
8569     cp_parser_single_declaration (parser, 
8570                                   /*member_p=*/false,
8571                                   /*friend_p=*/NULL);
8572
8573   /* We're done with the specialization.  */
8574   end_specialization ();
8575   /* We're done with this parameter list.  */
8576   --parser->num_template_parameter_lists;
8577 }
8578
8579 /* Parse a type-specifier.
8580
8581    type-specifier:
8582      simple-type-specifier
8583      class-specifier
8584      enum-specifier
8585      elaborated-type-specifier
8586      cv-qualifier
8587
8588    GNU Extension:
8589
8590    type-specifier:
8591      __complex__
8592
8593    Returns a representation of the type-specifier.  If the
8594    type-specifier is a keyword (like `int' or `const', or
8595    `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8596    For a class-specifier, enum-specifier, or elaborated-type-specifier
8597    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8598
8599    If IS_FRIEND is TRUE then this type-specifier is being declared a
8600    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8601    appearing in a decl-specifier-seq.
8602
8603    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8604    class-specifier, enum-specifier, or elaborated-type-specifier, then
8605    *DECLARES_CLASS_OR_ENUM is set to TRUE.  Otherwise, it is set to
8606    FALSE.
8607
8608    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8609    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8610    is set to FALSE.  */
8611
8612 static tree
8613 cp_parser_type_specifier (parser, 
8614                           flags, 
8615                           is_friend,
8616                           is_declaration,
8617                           declares_class_or_enum,
8618                           is_cv_qualifier)
8619      cp_parser *parser;
8620      cp_parser_flags flags;
8621      bool is_friend;
8622      bool is_declaration;
8623      bool *declares_class_or_enum;
8624      bool *is_cv_qualifier;
8625 {
8626   tree type_spec = NULL_TREE;
8627   cp_token *token;
8628   enum rid keyword;
8629
8630   /* Assume this type-specifier does not declare a new type.  */
8631   if (declares_class_or_enum)
8632     *declares_class_or_enum = false;
8633   /* And that it does not specify a cv-qualifier.  */
8634   if (is_cv_qualifier)
8635     *is_cv_qualifier = false;
8636   /* Peek at the next token.  */
8637   token = cp_lexer_peek_token (parser->lexer);
8638
8639   /* If we're looking at a keyword, we can use that to guide the
8640      production we choose.  */
8641   keyword = token->keyword;
8642   switch (keyword)
8643     {
8644       /* Any of these indicate either a class-specifier, or an
8645          elaborated-type-specifier.  */
8646     case RID_CLASS:
8647     case RID_STRUCT:
8648     case RID_UNION:
8649     case RID_ENUM:
8650       /* Parse tentatively so that we can back up if we don't find a
8651          class-specifier or enum-specifier.  */
8652       cp_parser_parse_tentatively (parser);
8653       /* Look for the class-specifier or enum-specifier.  */
8654       if (keyword == RID_ENUM)
8655         type_spec = cp_parser_enum_specifier (parser);
8656       else
8657         type_spec = cp_parser_class_specifier (parser);
8658
8659       /* If that worked, we're done.  */
8660       if (cp_parser_parse_definitely (parser))
8661         {
8662           if (declares_class_or_enum)
8663             *declares_class_or_enum = true;
8664           return type_spec;
8665         }
8666
8667       /* Fall through.  */
8668
8669     case RID_TYPENAME:
8670       /* Look for an elaborated-type-specifier.  */
8671       type_spec = cp_parser_elaborated_type_specifier (parser,
8672                                                        is_friend,
8673                                                        is_declaration);
8674       /* We're declaring a class or enum -- unless we're using
8675          `typename'.  */
8676       if (declares_class_or_enum && keyword != RID_TYPENAME)
8677         *declares_class_or_enum = true;
8678       return type_spec;
8679
8680     case RID_CONST:
8681     case RID_VOLATILE:
8682     case RID_RESTRICT:
8683       type_spec = cp_parser_cv_qualifier_opt (parser);
8684       /* Even though we call a routine that looks for an optional
8685          qualifier, we know that there should be one.  */
8686       my_friendly_assert (type_spec != NULL, 20000328);
8687       /* This type-specifier was a cv-qualified.  */
8688       if (is_cv_qualifier)
8689         *is_cv_qualifier = true;
8690
8691       return type_spec;
8692
8693     case RID_COMPLEX:
8694       /* The `__complex__' keyword is a GNU extension.  */
8695       return cp_lexer_consume_token (parser->lexer)->value;
8696
8697     default:
8698       break;
8699     }
8700
8701   /* If we do not already have a type-specifier, assume we are looking
8702      at a simple-type-specifier.  */
8703   type_spec = cp_parser_simple_type_specifier (parser, flags);
8704
8705   /* If we didn't find a type-specifier, and a type-specifier was not
8706      optional in this context, issue an error message.  */
8707   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8708     {
8709       cp_parser_error (parser, "expected type specifier");
8710       return error_mark_node;
8711     }
8712
8713   return type_spec;
8714 }
8715
8716 /* Parse a simple-type-specifier.
8717
8718    simple-type-specifier:
8719      :: [opt] nested-name-specifier [opt] type-name
8720      :: [opt] nested-name-specifier template template-id
8721      char
8722      wchar_t
8723      bool
8724      short
8725      int
8726      long
8727      signed
8728      unsigned
8729      float
8730      double
8731      void  
8732
8733    GNU Extension:
8734
8735    simple-type-specifier:
8736      __typeof__ unary-expression
8737      __typeof__ ( type-id )
8738
8739    For the various keywords, the value returned is simply the
8740    TREE_IDENTIFIER representing the keyword.  For the first two
8741    productions, the value returned is the indicated TYPE_DECL.  */
8742
8743 static tree
8744 cp_parser_simple_type_specifier (parser, flags)
8745      cp_parser *parser;
8746      cp_parser_flags flags;
8747 {
8748   tree type = NULL_TREE;
8749   cp_token *token;
8750
8751   /* Peek at the next token.  */
8752   token = cp_lexer_peek_token (parser->lexer);
8753
8754   /* If we're looking at a keyword, things are easy.  */
8755   switch (token->keyword)
8756     {
8757     case RID_CHAR:
8758     case RID_WCHAR:
8759     case RID_BOOL:
8760     case RID_SHORT:
8761     case RID_INT:
8762     case RID_LONG:
8763     case RID_SIGNED:
8764     case RID_UNSIGNED:
8765     case RID_FLOAT:
8766     case RID_DOUBLE:
8767     case RID_VOID:
8768       /* Consume the token.  */
8769       return cp_lexer_consume_token (parser->lexer)->value;
8770
8771     case RID_TYPEOF:
8772       {
8773         tree operand;
8774
8775         /* Consume the `typeof' token.  */
8776         cp_lexer_consume_token (parser->lexer);
8777         /* Parse the operand to `typeof'  */
8778         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8779         /* If it is not already a TYPE, take its type.  */
8780         if (!TYPE_P (operand))
8781           operand = finish_typeof (operand);
8782
8783         return operand;
8784       }
8785
8786     default:
8787       break;
8788     }
8789
8790   /* The type-specifier must be a user-defined type.  */
8791   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8792     {
8793       /* Don't gobble tokens or issue error messages if this is an
8794          optional type-specifier.  */
8795       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8796         cp_parser_parse_tentatively (parser);
8797
8798       /* Look for the optional `::' operator.  */
8799       cp_parser_global_scope_opt (parser,
8800                                   /*current_scope_valid_p=*/false);
8801       /* Look for the nested-name specifier.  */
8802       cp_parser_nested_name_specifier_opt (parser,
8803                                            /*typename_keyword_p=*/false,
8804                                            /*check_dependency_p=*/true,
8805                                            /*type_p=*/false);
8806       /* If we have seen a nested-name-specifier, and the next token
8807          is `template', then we are using the template-id production.  */
8808       if (parser->scope 
8809           && cp_parser_optional_template_keyword (parser))
8810         {
8811           /* Look for the template-id.  */
8812           type = cp_parser_template_id (parser, 
8813                                         /*template_keyword_p=*/true,
8814                                         /*check_dependency_p=*/true);
8815           /* If the template-id did not name a type, we are out of
8816              luck.  */
8817           if (TREE_CODE (type) != TYPE_DECL)
8818             {
8819               cp_parser_error (parser, "expected template-id for type");
8820               type = NULL_TREE;
8821             }
8822         }
8823       /* Otherwise, look for a type-name.  */
8824       else
8825         {
8826           type = cp_parser_type_name (parser);
8827           if (type == error_mark_node)
8828             type = NULL_TREE;
8829         }
8830
8831       /* If it didn't work out, we don't have a TYPE.  */
8832       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8833           && !cp_parser_parse_definitely (parser))
8834         type = NULL_TREE;
8835     }
8836
8837   /* If we didn't get a type-name, issue an error message.  */
8838   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8839     {
8840       cp_parser_error (parser, "expected type-name");
8841       return error_mark_node;
8842     }
8843
8844   return type;
8845 }
8846
8847 /* Parse a type-name.
8848
8849    type-name:
8850      class-name
8851      enum-name
8852      typedef-name  
8853
8854    enum-name:
8855      identifier
8856
8857    typedef-name:
8858      identifier 
8859
8860    Returns a TYPE_DECL for the the type.  */
8861
8862 static tree
8863 cp_parser_type_name (parser)
8864      cp_parser *parser;
8865 {
8866   tree type_decl;
8867   tree identifier;
8868
8869   /* We can't know yet whether it is a class-name or not.  */
8870   cp_parser_parse_tentatively (parser);
8871   /* Try a class-name.  */
8872   type_decl = cp_parser_class_name (parser, 
8873                                     /*typename_keyword_p=*/false,
8874                                     /*template_keyword_p=*/false,
8875                                     /*type_p=*/false,
8876                                     /*check_access_p=*/true,
8877                                     /*check_dependency_p=*/true,
8878                                     /*class_head_p=*/false);
8879   /* If it's not a class-name, keep looking.  */
8880   if (!cp_parser_parse_definitely (parser))
8881     {
8882       /* It must be a typedef-name or an enum-name.  */
8883       identifier = cp_parser_identifier (parser);
8884       if (identifier == error_mark_node)
8885         return error_mark_node;
8886       
8887       /* Look up the type-name.  */
8888       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8889       /* Issue an error if we did not find a type-name.  */
8890       if (TREE_CODE (type_decl) != TYPE_DECL)
8891         {
8892           cp_parser_error (parser, "expected type-name");
8893           type_decl = error_mark_node;
8894         }
8895       /* Remember that the name was used in the definition of the
8896          current class so that we can check later to see if the
8897          meaning would have been different after the class was
8898          entirely defined.  */
8899       else if (type_decl != error_mark_node
8900                && !parser->scope)
8901         maybe_note_name_used_in_class (identifier, type_decl);
8902     }
8903   
8904   return type_decl;
8905 }
8906
8907
8908 /* Parse an elaborated-type-specifier.  Note that the grammar given
8909    here incorporates the resolution to DR68.
8910
8911    elaborated-type-specifier:
8912      class-key :: [opt] nested-name-specifier [opt] identifier
8913      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8914      enum :: [opt] nested-name-specifier [opt] identifier
8915      typename :: [opt] nested-name-specifier identifier
8916      typename :: [opt] nested-name-specifier template [opt] 
8917        template-id 
8918
8919    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8920    declared `friend'.  If IS_DECLARATION is TRUE, then this
8921    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8922    something is being declared.
8923
8924    Returns the TYPE specified.  */
8925
8926 static tree
8927 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8928      cp_parser *parser;
8929      bool is_friend;
8930      bool is_declaration;
8931 {
8932   enum tag_types tag_type;
8933   tree identifier;
8934   tree type = NULL_TREE;
8935
8936   /* See if we're looking at the `enum' keyword.  */
8937   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8938     {
8939       /* Consume the `enum' token.  */
8940       cp_lexer_consume_token (parser->lexer);
8941       /* Remember that it's an enumeration type.  */
8942       tag_type = enum_type;
8943     }
8944   /* Or, it might be `typename'.  */
8945   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8946                                            RID_TYPENAME))
8947     {
8948       /* Consume the `typename' token.  */
8949       cp_lexer_consume_token (parser->lexer);
8950       /* Remember that it's a `typename' type.  */
8951       tag_type = typename_type;
8952       /* The `typename' keyword is only allowed in templates.  */
8953       if (!processing_template_decl)
8954         pedwarn ("using `typename' outside of template");
8955     }
8956   /* Otherwise it must be a class-key.  */
8957   else
8958     {
8959       tag_type = cp_parser_class_key (parser);
8960       if (tag_type == none_type)
8961         return error_mark_node;
8962     }
8963
8964   /* Look for the `::' operator.  */
8965   cp_parser_global_scope_opt (parser, 
8966                               /*current_scope_valid_p=*/false);
8967   /* Look for the nested-name-specifier.  */
8968   if (tag_type == typename_type)
8969     cp_parser_nested_name_specifier (parser,
8970                                      /*typename_keyword_p=*/true,
8971                                      /*check_dependency_p=*/true,
8972                                      /*type_p=*/true);
8973   else
8974     /* Even though `typename' is not present, the proposed resolution
8975        to Core Issue 180 says that in `class A<T>::B', `B' should be
8976        considered a type-name, even if `A<T>' is dependent.  */
8977     cp_parser_nested_name_specifier_opt (parser,
8978                                          /*typename_keyword_p=*/true,
8979                                          /*check_dependency_p=*/true,
8980                                          /*type_p=*/true);
8981   /* For everything but enumeration types, consider a template-id.  */
8982   if (tag_type != enum_type)
8983     {
8984       bool template_p = false;
8985       tree decl;
8986
8987       /* Allow the `template' keyword.  */
8988       template_p = cp_parser_optional_template_keyword (parser);
8989       /* If we didn't see `template', we don't know if there's a
8990          template-id or not.  */
8991       if (!template_p)
8992         cp_parser_parse_tentatively (parser);
8993       /* Parse the template-id.  */
8994       decl = cp_parser_template_id (parser, template_p,
8995                                     /*check_dependency_p=*/true);
8996       /* If we didn't find a template-id, look for an ordinary
8997          identifier.  */
8998       if (!template_p && !cp_parser_parse_definitely (parser))
8999         ;
9000       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9001          in effect, then we must assume that, upon instantiation, the
9002          template will correspond to a class.  */
9003       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9004                && tag_type == typename_type)
9005         type = make_typename_type (parser->scope, decl,
9006                                    /*complain=*/1);
9007       else 
9008         type = TREE_TYPE (decl);
9009     }
9010
9011   /* For an enumeration type, consider only a plain identifier.  */
9012   if (!type)
9013     {
9014       identifier = cp_parser_identifier (parser);
9015
9016       if (identifier == error_mark_node)
9017         return error_mark_node;
9018
9019       /* For a `typename', we needn't call xref_tag.  */
9020       if (tag_type == typename_type)
9021         return make_typename_type (parser->scope, identifier, 
9022                                    /*complain=*/1);
9023       /* Look up a qualified name in the usual way.  */
9024       if (parser->scope)
9025         {
9026           tree decl;
9027
9028           /* In an elaborated-type-specifier, names are assumed to name
9029              types, so we set IS_TYPE to TRUE when calling
9030              cp_parser_lookup_name.  */
9031           decl = cp_parser_lookup_name (parser, identifier, 
9032                                         /*check_access=*/true,
9033                                         /*is_type=*/true,
9034                                         /*is_namespace=*/false,
9035                                         /*check_dependency=*/true);
9036           decl = (cp_parser_maybe_treat_template_as_class 
9037                   (decl, /*tag_name_p=*/is_friend));
9038
9039           if (TREE_CODE (decl) != TYPE_DECL)
9040             {
9041               error ("expected type-name");
9042               return error_mark_node;
9043             }
9044           else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9045                    && tag_type != enum_type)
9046             error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9047                    tag_type == record_type ? "struct" : "class");
9048           else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9049                    && tag_type == enum_type)
9050             error ("`%T' referred to as enum", TREE_TYPE (decl));
9051
9052           type = TREE_TYPE (decl);
9053         }
9054       else 
9055         {
9056           /* An elaborated-type-specifier sometimes introduces a new type and
9057              sometimes names an existing type.  Normally, the rule is that it
9058              introduces a new type only if there is not an existing type of
9059              the same name already in scope.  For example, given:
9060
9061                struct S {};
9062                void f() { struct S s; }
9063
9064              the `struct S' in the body of `f' is the same `struct S' as in
9065              the global scope; the existing definition is used.  However, if
9066              there were no global declaration, this would introduce a new 
9067              local class named `S'.
9068
9069              An exception to this rule applies to the following code:
9070
9071                namespace N { struct S; }
9072
9073              Here, the elaborated-type-specifier names a new type
9074              unconditionally; even if there is already an `S' in the
9075              containing scope this declaration names a new type.
9076              This exception only applies if the elaborated-type-specifier
9077              forms the complete declaration:
9078
9079                [class.name] 
9080
9081                A declaration consisting solely of `class-key identifier ;' is
9082                either a redeclaration of the name in the current scope or a
9083                forward declaration of the identifier as a class name.  It
9084                introduces the name into the current scope.
9085
9086              We are in this situation precisely when the next token is a `;'.
9087
9088              An exception to the exception is that a `friend' declaration does
9089              *not* name a new type; i.e., given:
9090
9091                struct S { friend struct T; };
9092
9093              `T' is not a new type in the scope of `S'.  
9094
9095              Also, `new struct S' or `sizeof (struct S)' never results in the
9096              definition of a new type; a new type can only be declared in a
9097              declaration context.   */
9098
9099           type = xref_tag (tag_type, identifier, 
9100                            /*attributes=*/NULL_TREE,
9101                            (is_friend 
9102                             || !is_declaration
9103                             || cp_lexer_next_token_is_not (parser->lexer, 
9104                                                            CPP_SEMICOLON)));
9105         }
9106     }
9107   if (tag_type != enum_type)
9108     cp_parser_check_class_key (tag_type, type);
9109   return type;
9110 }
9111
9112 /* Parse an enum-specifier.
9113
9114    enum-specifier:
9115      enum identifier [opt] { enumerator-list [opt] }
9116
9117    Returns an ENUM_TYPE representing the enumeration.  */
9118
9119 static tree
9120 cp_parser_enum_specifier (parser)
9121      cp_parser *parser;
9122 {
9123   cp_token *token;
9124   tree identifier = NULL_TREE;
9125   tree type;
9126
9127   /* Look for the `enum' keyword.  */
9128   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9129     return error_mark_node;
9130   /* Peek at the next token.  */
9131   token = cp_lexer_peek_token (parser->lexer);
9132
9133   /* See if it is an identifier.  */
9134   if (token->type == CPP_NAME)
9135     identifier = cp_parser_identifier (parser);
9136
9137   /* Look for the `{'.  */
9138   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9139     return error_mark_node;
9140
9141   /* At this point, we're going ahead with the enum-specifier, even
9142      if some other problem occurs.  */
9143   cp_parser_commit_to_tentative_parse (parser);
9144
9145   /* Issue an error message if type-definitions are forbidden here.  */
9146   cp_parser_check_type_definition (parser);
9147
9148   /* Create the new type.  */
9149   type = start_enum (identifier ? identifier : make_anon_name ());
9150
9151   /* Peek at the next token.  */
9152   token = cp_lexer_peek_token (parser->lexer);
9153   /* If it's not a `}', then there are some enumerators.  */
9154   if (token->type != CPP_CLOSE_BRACE)
9155     cp_parser_enumerator_list (parser, type);
9156   /* Look for the `}'.  */
9157   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9158
9159   /* Finish up the enumeration.  */
9160   finish_enum (type);
9161
9162   return type;
9163 }
9164
9165 /* Parse an enumerator-list.  The enumerators all have the indicated
9166    TYPE.  
9167
9168    enumerator-list:
9169      enumerator-definition
9170      enumerator-list , enumerator-definition  */
9171
9172 static void
9173 cp_parser_enumerator_list (parser, type)
9174      cp_parser *parser;
9175      tree type;
9176 {
9177   while (true)
9178     {
9179       cp_token *token;
9180
9181       /* Parse an enumerator-definition.  */
9182       cp_parser_enumerator_definition (parser, type);
9183       /* Peek at the next token.  */
9184       token = cp_lexer_peek_token (parser->lexer);
9185       /* If it's not a `,', then we've reached the end of the 
9186          list.  */
9187       if (token->type != CPP_COMMA)
9188         break;
9189       /* Otherwise, consume the `,' and keep going.  */
9190       cp_lexer_consume_token (parser->lexer);
9191       /* If the next token is a `}', there is a trailing comma.  */
9192       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9193         {
9194           if (pedantic && !in_system_header)
9195             pedwarn ("comma at end of enumerator list");
9196           break;
9197         }
9198     }
9199 }
9200
9201 /* Parse an enumerator-definition.  The enumerator has the indicated
9202    TYPE.
9203
9204    enumerator-definition:
9205      enumerator
9206      enumerator = constant-expression
9207     
9208    enumerator:
9209      identifier  */
9210
9211 static void
9212 cp_parser_enumerator_definition (parser, type)
9213      cp_parser *parser;
9214      tree type;
9215 {
9216   cp_token *token;
9217   tree identifier;
9218   tree value;
9219
9220   /* Look for the identifier.  */
9221   identifier = cp_parser_identifier (parser);
9222   if (identifier == error_mark_node)
9223     return;
9224   
9225   /* Peek at the next token.  */
9226   token = cp_lexer_peek_token (parser->lexer);
9227   /* If it's an `=', then there's an explicit value.  */
9228   if (token->type == CPP_EQ)
9229     {
9230       /* Consume the `=' token.  */
9231       cp_lexer_consume_token (parser->lexer);
9232       /* Parse the value.  */
9233       value = cp_parser_constant_expression (parser);
9234     }
9235   else
9236     value = NULL_TREE;
9237
9238   /* Create the enumerator.  */
9239   build_enumerator (identifier, value, type);
9240 }
9241
9242 /* Parse a namespace-name.
9243
9244    namespace-name:
9245      original-namespace-name
9246      namespace-alias
9247
9248    Returns the NAMESPACE_DECL for the namespace.  */
9249
9250 static tree
9251 cp_parser_namespace_name (parser)
9252      cp_parser *parser;
9253 {
9254   tree identifier;
9255   tree namespace_decl;
9256
9257   /* Get the name of the namespace.  */
9258   identifier = cp_parser_identifier (parser);
9259   if (identifier == error_mark_node)
9260     return error_mark_node;
9261
9262   /* Look up the identifier in the currently active scope.  Look only
9263      for namespaces, due to:
9264
9265        [basic.lookup.udir]
9266
9267        When looking up a namespace-name in a using-directive or alias
9268        definition, only namespace names are considered.  
9269
9270      And:
9271
9272        [basic.lookup.qual]
9273
9274        During the lookup of a name preceding the :: scope resolution
9275        operator, object, function, and enumerator names are ignored.  
9276
9277      (Note that cp_parser_class_or_namespace_name only calls this
9278      function if the token after the name is the scope resolution
9279      operator.)  */
9280   namespace_decl = cp_parser_lookup_name (parser, identifier,
9281                                           /*check_access=*/true,
9282                                           /*is_type=*/false,
9283                                           /*is_namespace=*/true,
9284                                           /*check_dependency=*/true);
9285   /* If it's not a namespace, issue an error.  */
9286   if (namespace_decl == error_mark_node
9287       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9288     {
9289       cp_parser_error (parser, "expected namespace-name");
9290       namespace_decl = error_mark_node;
9291     }
9292   
9293   return namespace_decl;
9294 }
9295
9296 /* Parse a namespace-definition.
9297
9298    namespace-definition:
9299      named-namespace-definition
9300      unnamed-namespace-definition  
9301
9302    named-namespace-definition:
9303      original-namespace-definition
9304      extension-namespace-definition
9305
9306    original-namespace-definition:
9307      namespace identifier { namespace-body }
9308    
9309    extension-namespace-definition:
9310      namespace original-namespace-name { namespace-body }
9311  
9312    unnamed-namespace-definition:
9313      namespace { namespace-body } */
9314
9315 static void
9316 cp_parser_namespace_definition (parser)
9317      cp_parser *parser;
9318 {
9319   tree identifier;
9320
9321   /* Look for the `namespace' keyword.  */
9322   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9323
9324   /* Get the name of the namespace.  We do not attempt to distinguish
9325      between an original-namespace-definition and an
9326      extension-namespace-definition at this point.  The semantic
9327      analysis routines are responsible for that.  */
9328   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9329     identifier = cp_parser_identifier (parser);
9330   else
9331     identifier = NULL_TREE;
9332
9333   /* Look for the `{' to start the namespace.  */
9334   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9335   /* Start the namespace.  */
9336   push_namespace (identifier);
9337   /* Parse the body of the namespace.  */
9338   cp_parser_namespace_body (parser);
9339   /* Finish the namespace.  */
9340   pop_namespace ();
9341   /* Look for the final `}'.  */
9342   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9343 }
9344
9345 /* Parse a namespace-body.
9346
9347    namespace-body:
9348      declaration-seq [opt]  */
9349
9350 static void
9351 cp_parser_namespace_body (parser)
9352      cp_parser *parser;
9353 {
9354   cp_parser_declaration_seq_opt (parser);
9355 }
9356
9357 /* Parse a namespace-alias-definition.
9358
9359    namespace-alias-definition:
9360      namespace identifier = qualified-namespace-specifier ;  */
9361
9362 static void
9363 cp_parser_namespace_alias_definition (parser)
9364      cp_parser *parser;
9365 {
9366   tree identifier;
9367   tree namespace_specifier;
9368
9369   /* Look for the `namespace' keyword.  */
9370   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9371   /* Look for the identifier.  */
9372   identifier = cp_parser_identifier (parser);
9373   if (identifier == error_mark_node)
9374     return;
9375   /* Look for the `=' token.  */
9376   cp_parser_require (parser, CPP_EQ, "`='");
9377   /* Look for the qualified-namespace-specifier.  */
9378   namespace_specifier 
9379     = cp_parser_qualified_namespace_specifier (parser);
9380   /* Look for the `;' token.  */
9381   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9382
9383   /* Register the alias in the symbol table.  */
9384   do_namespace_alias (identifier, namespace_specifier);
9385 }
9386
9387 /* Parse a qualified-namespace-specifier.
9388
9389    qualified-namespace-specifier:
9390      :: [opt] nested-name-specifier [opt] namespace-name
9391
9392    Returns a NAMESPACE_DECL corresponding to the specified
9393    namespace.  */
9394
9395 static tree
9396 cp_parser_qualified_namespace_specifier (parser)
9397      cp_parser *parser;
9398 {
9399   /* Look for the optional `::'.  */
9400   cp_parser_global_scope_opt (parser, 
9401                               /*current_scope_valid_p=*/false);
9402
9403   /* Look for the optional nested-name-specifier.  */
9404   cp_parser_nested_name_specifier_opt (parser,
9405                                        /*typename_keyword_p=*/false,
9406                                        /*check_dependency_p=*/true,
9407                                        /*type_p=*/false);
9408
9409   return cp_parser_namespace_name (parser);
9410 }
9411
9412 /* Parse a using-declaration.
9413
9414    using-declaration:
9415      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9416      using :: unqualified-id ;  */
9417
9418 static void
9419 cp_parser_using_declaration (parser)
9420      cp_parser *parser;
9421 {
9422   cp_token *token;
9423   bool typename_p = false;
9424   bool global_scope_p;
9425   tree decl;
9426   tree identifier;
9427   tree scope;
9428
9429   /* Look for the `using' keyword.  */
9430   cp_parser_require_keyword (parser, RID_USING, "`using'");
9431   
9432   /* Peek at the next token.  */
9433   token = cp_lexer_peek_token (parser->lexer);
9434   /* See if it's `typename'.  */
9435   if (token->keyword == RID_TYPENAME)
9436     {
9437       /* Remember that we've seen it.  */
9438       typename_p = true;
9439       /* Consume the `typename' token.  */
9440       cp_lexer_consume_token (parser->lexer);
9441     }
9442
9443   /* Look for the optional global scope qualification.  */
9444   global_scope_p 
9445     = (cp_parser_global_scope_opt (parser,
9446                                    /*current_scope_valid_p=*/false) 
9447        != NULL_TREE);
9448
9449   /* If we saw `typename', or didn't see `::', then there must be a
9450      nested-name-specifier present.  */
9451   if (typename_p || !global_scope_p)
9452     cp_parser_nested_name_specifier (parser, typename_p, 
9453                                      /*check_dependency_p=*/true,
9454                                      /*type_p=*/false);
9455   /* Otherwise, we could be in either of the two productions.  In that
9456      case, treat the nested-name-specifier as optional.  */
9457   else
9458     cp_parser_nested_name_specifier_opt (parser,
9459                                          /*typename_keyword_p=*/false,
9460                                          /*check_dependency_p=*/true,
9461                                          /*type_p=*/false);
9462
9463   /* Parse the unqualified-id.  */
9464   identifier = cp_parser_unqualified_id (parser, 
9465                                          /*template_keyword_p=*/false,
9466                                          /*check_dependency_p=*/true);
9467
9468   /* The function we call to handle a using-declaration is different
9469      depending on what scope we are in.  */
9470   scope = current_scope ();
9471   if (scope && TYPE_P (scope))
9472     {
9473       /* Create the USING_DECL.  */
9474       decl = do_class_using_decl (build_nt (SCOPE_REF,
9475                                             parser->scope,
9476                                             identifier));
9477       /* Add it to the list of members in this class.  */
9478       finish_member_declaration (decl);
9479     }
9480   else
9481     {
9482       decl = cp_parser_lookup_name_simple (parser, identifier);
9483       if (scope)
9484         do_local_using_decl (decl);
9485       else
9486         do_toplevel_using_decl (decl);
9487     }
9488
9489   /* Look for the final `;'.  */
9490   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9491 }
9492
9493 /* Parse a using-directive.  
9494  
9495    using-directive:
9496      using namespace :: [opt] nested-name-specifier [opt]
9497        namespace-name ;  */
9498
9499 static void
9500 cp_parser_using_directive (parser)
9501      cp_parser *parser;
9502 {
9503   tree namespace_decl;
9504
9505   /* Look for the `using' keyword.  */
9506   cp_parser_require_keyword (parser, RID_USING, "`using'");
9507   /* And the `namespace' keyword.  */
9508   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9509   /* Look for the optional `::' operator.  */
9510   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9511   /* And the optional nested-name-sepcifier.  */
9512   cp_parser_nested_name_specifier_opt (parser,
9513                                        /*typename_keyword_p=*/false,
9514                                        /*check_dependency_p=*/true,
9515                                        /*type_p=*/false);
9516   /* Get the namespace being used.  */
9517   namespace_decl = cp_parser_namespace_name (parser);
9518   /* Update the symbol table.  */
9519   do_using_directive (namespace_decl);
9520   /* Look for the final `;'.  */
9521   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9522 }
9523
9524 /* Parse an asm-definition.
9525
9526    asm-definition:
9527      asm ( string-literal ) ;  
9528
9529    GNU Extension:
9530
9531    asm-definition:
9532      asm volatile [opt] ( string-literal ) ;
9533      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9534      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9535                           : asm-operand-list [opt] ) ;
9536      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9537                           : asm-operand-list [opt] 
9538                           : asm-operand-list [opt] ) ;  */
9539
9540 static void
9541 cp_parser_asm_definition (parser)
9542      cp_parser *parser;
9543 {
9544   cp_token *token;
9545   tree string;
9546   tree outputs = NULL_TREE;
9547   tree inputs = NULL_TREE;
9548   tree clobbers = NULL_TREE;
9549   tree asm_stmt;
9550   bool volatile_p = false;
9551   bool extended_p = false;
9552
9553   /* Look for the `asm' keyword.  */
9554   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9555   /* See if the next token is `volatile'.  */
9556   if (cp_parser_allow_gnu_extensions_p (parser)
9557       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9558     {
9559       /* Remember that we saw the `volatile' keyword.  */
9560       volatile_p = true;
9561       /* Consume the token.  */
9562       cp_lexer_consume_token (parser->lexer);
9563     }
9564   /* Look for the opening `('.  */
9565   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9566   /* Look for the string.  */
9567   token = cp_parser_require (parser, CPP_STRING, "asm body");
9568   if (!token)
9569     return;
9570   string = token->value;
9571   /* If we're allowing GNU extensions, check for the extended assembly
9572      syntax.  Unfortunately, the `:' tokens need not be separated by 
9573      a space in C, and so, for compatibility, we tolerate that here
9574      too.  Doing that means that we have to treat the `::' operator as
9575      two `:' tokens.  */
9576   if (cp_parser_allow_gnu_extensions_p (parser)
9577       && at_function_scope_p ()
9578       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9579           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9580     {
9581       bool inputs_p = false;
9582       bool clobbers_p = false;
9583
9584       /* The extended syntax was used.  */
9585       extended_p = true;
9586
9587       /* Look for outputs.  */
9588       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9589         {
9590           /* Consume the `:'.  */
9591           cp_lexer_consume_token (parser->lexer);
9592           /* Parse the output-operands.  */
9593           if (cp_lexer_next_token_is_not (parser->lexer, 
9594                                           CPP_COLON)
9595               && cp_lexer_next_token_is_not (parser->lexer,
9596                                              CPP_SCOPE)
9597               && cp_lexer_next_token_is_not (parser->lexer,
9598                                              CPP_CLOSE_PAREN))
9599             outputs = cp_parser_asm_operand_list (parser);
9600         }
9601       /* If the next token is `::', there are no outputs, and the
9602          next token is the beginning of the inputs.  */
9603       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9604         {
9605           /* Consume the `::' token.  */
9606           cp_lexer_consume_token (parser->lexer);
9607           /* The inputs are coming next.  */
9608           inputs_p = true;
9609         }
9610
9611       /* Look for inputs.  */
9612       if (inputs_p
9613           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9614         {
9615           if (!inputs_p)
9616             /* Consume the `:'.  */
9617             cp_lexer_consume_token (parser->lexer);
9618           /* Parse the output-operands.  */
9619           if (cp_lexer_next_token_is_not (parser->lexer, 
9620                                           CPP_COLON)
9621               && cp_lexer_next_token_is_not (parser->lexer,
9622                                              CPP_SCOPE)
9623               && cp_lexer_next_token_is_not (parser->lexer,
9624                                              CPP_CLOSE_PAREN))
9625             inputs = cp_parser_asm_operand_list (parser);
9626         }
9627       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9628         /* The clobbers are coming next.  */
9629         clobbers_p = true;
9630
9631       /* Look for clobbers.  */
9632       if (clobbers_p 
9633           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9634         {
9635           if (!clobbers_p)
9636             /* Consume the `:'.  */
9637             cp_lexer_consume_token (parser->lexer);
9638           /* Parse the clobbers.  */
9639           if (cp_lexer_next_token_is_not (parser->lexer,
9640                                           CPP_CLOSE_PAREN))
9641             clobbers = cp_parser_asm_clobber_list (parser);
9642         }
9643     }
9644   /* Look for the closing `)'.  */
9645   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9646     cp_parser_skip_to_closing_parenthesis (parser);
9647   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9648
9649   /* Create the ASM_STMT.  */
9650   if (at_function_scope_p ())
9651     {
9652       asm_stmt = 
9653         finish_asm_stmt (volatile_p 
9654                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9655                          string, outputs, inputs, clobbers);
9656       /* If the extended syntax was not used, mark the ASM_STMT.  */
9657       if (!extended_p)
9658         ASM_INPUT_P (asm_stmt) = 1;
9659     }
9660   else
9661     assemble_asm (string);
9662 }
9663
9664 /* Declarators [gram.dcl.decl] */
9665
9666 /* Parse an init-declarator.
9667
9668    init-declarator:
9669      declarator initializer [opt]
9670
9671    GNU Extension:
9672
9673    init-declarator:
9674      declarator asm-specification [opt] attributes [opt] initializer [opt]
9675
9676    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9677    Returns a reprsentation of the entity declared.  The ACCESS_CHECKS
9678    represent deferred access checks from the decl-specifier-seq.  If
9679    MEMBER_P is TRUE, then this declarator appears in a class scope.
9680    The new DECL created by this declarator is returned.
9681
9682    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9683    for a function-definition here as well.  If the declarator is a
9684    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9685    be TRUE upon return.  By that point, the function-definition will
9686    have been completely parsed.
9687
9688    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9689    is FALSE.  */
9690
9691 static tree
9692 cp_parser_init_declarator (parser, 
9693                            decl_specifiers, 
9694                            prefix_attributes,
9695                            access_checks,
9696                            function_definition_allowed_p,
9697                            member_p,
9698                            function_definition_p)
9699      cp_parser *parser;
9700      tree decl_specifiers;
9701      tree prefix_attributes;
9702      tree access_checks;
9703      bool function_definition_allowed_p;
9704      bool member_p;
9705      bool *function_definition_p;
9706 {
9707   cp_token *token;
9708   tree declarator;
9709   tree attributes;
9710   tree asm_specification;
9711   tree initializer;
9712   tree decl = NULL_TREE;
9713   tree scope;
9714   tree declarator_access_checks;
9715   bool is_initialized;
9716   bool is_parenthesized_init;
9717   bool ctor_dtor_or_conv_p;
9718   bool friend_p;
9719
9720   /* Assume that this is not the declarator for a function
9721      definition.  */
9722   if (function_definition_p)
9723     *function_definition_p = false;
9724
9725   /* Defer access checks while parsing the declarator; we cannot know
9726      what names are accessible until we know what is being 
9727      declared.  */
9728   cp_parser_start_deferring_access_checks (parser);
9729   /* Parse the declarator.  */
9730   declarator 
9731     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9732                             &ctor_dtor_or_conv_p);
9733   /* Gather up the deferred checks.  */
9734   declarator_access_checks 
9735     = cp_parser_stop_deferring_access_checks (parser);
9736
9737   /* Prevent the access checks from being reclaimed by GC.  */
9738   parser->access_checks_lists
9739     = tree_cons (NULL_TREE, declarator_access_checks,
9740                  parser->access_checks_lists);
9741
9742   /* If the DECLARATOR was erroneous, there's no need to go
9743      further.  */
9744   if (declarator == error_mark_node)
9745     {
9746       /* Discard access checks no longer in use.  */
9747       parser->access_checks_lists
9748         = TREE_CHAIN (parser->access_checks_lists);
9749       return error_mark_node;
9750     }
9751
9752   /* Figure out what scope the entity declared by the DECLARATOR is
9753      located in.  `grokdeclarator' sometimes changes the scope, so
9754      we compute it now.  */
9755   scope = get_scope_of_declarator (declarator);
9756
9757   /* If we're allowing GNU extensions, look for an asm-specification
9758      and attributes.  */
9759   if (cp_parser_allow_gnu_extensions_p (parser))
9760     {
9761       /* Look for an asm-specification.  */
9762       asm_specification = cp_parser_asm_specification_opt (parser);
9763       /* And attributes.  */
9764       attributes = cp_parser_attributes_opt (parser);
9765     }
9766   else
9767     {
9768       asm_specification = NULL_TREE;
9769       attributes = NULL_TREE;
9770     }
9771
9772   /* Peek at the next token.  */
9773   token = cp_lexer_peek_token (parser->lexer);
9774   /* Check to see if the token indicates the start of a
9775      function-definition.  */
9776   if (cp_parser_token_starts_function_definition_p (token))
9777     {
9778       if (!function_definition_allowed_p)
9779         {
9780           /* If a function-definition should not appear here, issue an
9781              error message.  */
9782           cp_parser_error (parser,
9783                            "a function-definition is not allowed here");
9784           /* Discard access checks no longer in use.  */
9785           parser->access_checks_lists
9786             = TREE_CHAIN (parser->access_checks_lists);
9787           return error_mark_node;
9788         }
9789       else
9790         {
9791           tree *ac;
9792
9793           /* Neither attributes nor an asm-specification are allowed
9794              on a function-definition.  */
9795           if (asm_specification)
9796             error ("an asm-specification is not allowed on a function-definition");
9797           if (attributes)
9798             error ("attributes are not allowed on a function-definition");
9799           /* This is a function-definition.  */
9800           *function_definition_p = true;
9801
9802           /* Thread the access checks together.  */
9803           ac = &access_checks;
9804           while (*ac)
9805             ac = &TREE_CHAIN (*ac);
9806           *ac = declarator_access_checks;
9807
9808           /* Parse the function definition.  */
9809           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9810                   (parser, decl_specifiers, prefix_attributes, declarator,
9811                    access_checks));
9812
9813           /* Pull the access-checks apart again.  */
9814           *ac = NULL_TREE;
9815
9816           /* Discard access checks no longer in use.  */
9817           parser->access_checks_lists
9818              = TREE_CHAIN (parser->access_checks_lists);
9819
9820           return decl;
9821         }
9822     }
9823
9824   /* [dcl.dcl]
9825
9826      Only in function declarations for constructors, destructors, and
9827      type conversions can the decl-specifier-seq be omitted.  
9828
9829      We explicitly postpone this check past the point where we handle
9830      function-definitions because we tolerate function-definitions
9831      that are missing their return types in some modes.  */
9832   if (!decl_specifiers && !ctor_dtor_or_conv_p)
9833     {
9834       cp_parser_error (parser, 
9835                        "expected constructor, destructor, or type conversion");
9836       /* Discard access checks no longer in use.  */
9837       parser->access_checks_lists
9838         = TREE_CHAIN (parser->access_checks_lists);
9839       return error_mark_node;
9840     }
9841
9842   /* An `=' or an `(' indicates an initializer.  */
9843   is_initialized = (token->type == CPP_EQ 
9844                      || token->type == CPP_OPEN_PAREN);
9845   /* If the init-declarator isn't initialized and isn't followed by a
9846      `,' or `;', it's not a valid init-declarator.  */
9847   if (!is_initialized 
9848       && token->type != CPP_COMMA
9849       && token->type != CPP_SEMICOLON)
9850     {
9851       cp_parser_error (parser, "expected init-declarator");
9852       /* Discard access checks no longer in use.  */
9853       parser->access_checks_lists
9854          = TREE_CHAIN (parser->access_checks_lists);
9855       return error_mark_node;
9856     }
9857
9858   /* Because start_decl has side-effects, we should only call it if we
9859      know we're going ahead.  By this point, we know that we cannot
9860      possibly be looking at any other construct.  */
9861   cp_parser_commit_to_tentative_parse (parser);
9862
9863   /* Check to see whether or not this declaration is a friend.  */
9864   friend_p = cp_parser_friend_p (decl_specifiers);
9865
9866   /* Check that the number of template-parameter-lists is OK.  */
9867   if (!cp_parser_check_declarator_template_parameters (parser, 
9868                                                        declarator))
9869     {
9870       /* Discard access checks no longer in use.  */
9871       parser->access_checks_lists
9872          = TREE_CHAIN (parser->access_checks_lists);
9873       return error_mark_node;
9874     }
9875
9876   /* Enter the newly declared entry in the symbol table.  If we're
9877      processing a declaration in a class-specifier, we wait until
9878      after processing the initializer.  */
9879   if (!member_p)
9880     {
9881       if (parser->in_unbraced_linkage_specification_p)
9882         {
9883           decl_specifiers = tree_cons (error_mark_node,
9884                                        get_identifier ("extern"),
9885                                        decl_specifiers);
9886           have_extern_spec = false;
9887         }
9888       decl = start_decl (declarator,
9889                          decl_specifiers,
9890                          is_initialized,
9891                          attributes,
9892                          prefix_attributes);
9893     }
9894
9895   /* Enter the SCOPE.  That way unqualified names appearing in the
9896      initializer will be looked up in SCOPE.  */
9897   if (scope)
9898     push_scope (scope);
9899
9900   /* Perform deferred access control checks, now that we know in which
9901      SCOPE the declared entity resides.  */
9902   if (!member_p && decl) 
9903     {
9904       tree saved_current_function_decl = NULL_TREE;
9905
9906       /* If the entity being declared is a function, pretend that we
9907          are in its scope.  If it is a `friend', it may have access to
9908          things that would not otherwise be accessible. */
9909       if (TREE_CODE (decl) == FUNCTION_DECL)
9910         {
9911           saved_current_function_decl = current_function_decl;
9912           current_function_decl = decl;
9913         }
9914         
9915       /* Perform the access control checks for the decl-specifiers.  */
9916       cp_parser_perform_deferred_access_checks (access_checks);
9917       /* And for the declarator.  */
9918       cp_parser_perform_deferred_access_checks (declarator_access_checks);
9919
9920       /* Restore the saved value.  */
9921       if (TREE_CODE (decl) == FUNCTION_DECL)
9922         current_function_decl = saved_current_function_decl;
9923     }
9924
9925   /* Parse the initializer.  */
9926   if (is_initialized)
9927     initializer = cp_parser_initializer (parser, 
9928                                          &is_parenthesized_init);
9929   else
9930     {
9931       initializer = NULL_TREE;
9932       is_parenthesized_init = false;
9933     }
9934
9935   /* The old parser allows attributes to appear after a parenthesized
9936      initializer.  Mark Mitchell proposed removing this functionality
9937      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9938      attributes -- but ignores them.  */
9939   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9940     if (cp_parser_attributes_opt (parser))
9941       warning ("attributes after parenthesized initializer ignored");
9942
9943   /* Leave the SCOPE, now that we have processed the initializer.  It
9944      is important to do this before calling cp_finish_decl because it
9945      makes decisions about whether to create DECL_STMTs or not based
9946      on the current scope.  */
9947   if (scope)
9948     pop_scope (scope);
9949
9950   /* For an in-class declaration, use `grokfield' to create the
9951      declaration.  */
9952   if (member_p)
9953     decl = grokfield (declarator, decl_specifiers,
9954                       initializer, /*asmspec=*/NULL_TREE,
9955                         /*attributes=*/NULL_TREE);
9956
9957   /* Finish processing the declaration.  But, skip friend
9958      declarations.  */
9959   if (!friend_p && decl)
9960     cp_finish_decl (decl, 
9961                     initializer, 
9962                     asm_specification,
9963                     /* If the initializer is in parentheses, then this is
9964                        a direct-initialization, which means that an
9965                        `explicit' constructor is OK.  Otherwise, an
9966                        `explicit' constructor cannot be used.  */
9967                     ((is_parenthesized_init || !is_initialized)
9968                      ? 0 : LOOKUP_ONLYCONVERTING));
9969
9970   /* Discard access checks no longer in use.  */
9971   parser->access_checks_lists
9972     = TREE_CHAIN (parser->access_checks_lists);
9973
9974   return decl;
9975 }
9976
9977 /* Parse a declarator.
9978    
9979    declarator:
9980      direct-declarator
9981      ptr-operator declarator  
9982
9983    abstract-declarator:
9984      ptr-operator abstract-declarator [opt]
9985      direct-abstract-declarator
9986
9987    GNU Extensions:
9988
9989    declarator:
9990      attributes [opt] direct-declarator
9991      attributes [opt] ptr-operator declarator  
9992
9993    abstract-declarator:
9994      attributes [opt] ptr-operator abstract-declarator [opt]
9995      attributes [opt] direct-abstract-declarator
9996      
9997    Returns a representation of the declarator.  If the declarator has
9998    the form `* declarator', then an INDIRECT_REF is returned, whose
9999    only operand is the sub-declarator.  Analagously, `& declarator' is
10000    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10001    used.  The first operand is the TYPE for `X'.  The second operand
10002    is an INDIRECT_REF whose operand is the sub-declarator.
10003
10004    Otherwise, the reprsentation is as for a direct-declarator.
10005
10006    (It would be better to define a structure type to represent
10007    declarators, rather than abusing `tree' nodes to represent
10008    declarators.  That would be much clearer and save some memory.
10009    There is no reason for declarators to be garbage-collected, for
10010    example; they are created during parser and no longer needed after
10011    `grokdeclarator' has been called.)
10012
10013    For a ptr-operator that has the optional cv-qualifier-seq,
10014    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10015    node.
10016
10017    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10018    true if this declarator represents a constructor, destructor, or
10019    type conversion operator.  Otherwise, it is set to false.  
10020
10021    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10022    a decl-specifier-seq unless it declares a constructor, destructor,
10023    or conversion.  It might seem that we could check this condition in
10024    semantic analysis, rather than parsing, but that makes it difficult
10025    to handle something like `f()'.  We want to notice that there are
10026    no decl-specifiers, and therefore realize that this is an
10027    expression, not a declaration.)  */
10028
10029 static tree
10030 cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10031      cp_parser *parser;
10032      cp_parser_declarator_kind dcl_kind;
10033      bool *ctor_dtor_or_conv_p;
10034 {
10035   cp_token *token;
10036   tree declarator;
10037   enum tree_code code;
10038   tree cv_qualifier_seq;
10039   tree class_type;
10040   tree attributes = NULL_TREE;
10041
10042   /* Assume this is not a constructor, destructor, or type-conversion
10043      operator.  */
10044   if (ctor_dtor_or_conv_p)
10045     *ctor_dtor_or_conv_p = false;
10046
10047   if (cp_parser_allow_gnu_extensions_p (parser))
10048     attributes = cp_parser_attributes_opt (parser);
10049   
10050   /* Peek at the next token.  */
10051   token = cp_lexer_peek_token (parser->lexer);
10052   
10053   /* Check for the ptr-operator production.  */
10054   cp_parser_parse_tentatively (parser);
10055   /* Parse the ptr-operator.  */
10056   code = cp_parser_ptr_operator (parser, 
10057                                  &class_type, 
10058                                  &cv_qualifier_seq);
10059   /* If that worked, then we have a ptr-operator.  */
10060   if (cp_parser_parse_definitely (parser))
10061     {
10062       /* The dependent declarator is optional if we are parsing an
10063          abstract-declarator.  */
10064       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10065         cp_parser_parse_tentatively (parser);
10066
10067       /* Parse the dependent declarator.  */
10068       declarator = cp_parser_declarator (parser, dcl_kind,
10069                                          /*ctor_dtor_or_conv_p=*/NULL);
10070
10071       /* If we are parsing an abstract-declarator, we must handle the
10072          case where the dependent declarator is absent.  */
10073       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10074           && !cp_parser_parse_definitely (parser))
10075         declarator = NULL_TREE;
10076         
10077       /* Build the representation of the ptr-operator.  */
10078       if (code == INDIRECT_REF)
10079         declarator = make_pointer_declarator (cv_qualifier_seq, 
10080                                               declarator);
10081       else
10082         declarator = make_reference_declarator (cv_qualifier_seq,
10083                                                 declarator);
10084       /* Handle the pointer-to-member case.  */
10085       if (class_type)
10086         declarator = build_nt (SCOPE_REF, class_type, declarator);
10087     }
10088   /* Everything else is a direct-declarator.  */
10089   else
10090     declarator = cp_parser_direct_declarator (parser, 
10091                                               dcl_kind,
10092                                               ctor_dtor_or_conv_p);
10093
10094   if (attributes && declarator != error_mark_node)
10095     declarator = tree_cons (attributes, declarator, NULL_TREE);
10096   
10097   return declarator;
10098 }
10099
10100 /* Parse a direct-declarator or direct-abstract-declarator.
10101
10102    direct-declarator:
10103      declarator-id
10104      direct-declarator ( parameter-declaration-clause )
10105        cv-qualifier-seq [opt] 
10106        exception-specification [opt]
10107      direct-declarator [ constant-expression [opt] ]
10108      ( declarator )  
10109
10110    direct-abstract-declarator:
10111      direct-abstract-declarator [opt]
10112        ( parameter-declaration-clause ) 
10113        cv-qualifier-seq [opt]
10114        exception-specification [opt]
10115      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10116      ( abstract-declarator )
10117
10118    Returns a representation of the declarator.  DCL_KIND is
10119    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10120    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10121    we are parsing a direct-declarator.  It is
10122    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10123    of ambiguity we prefer an abstract declarator, as per
10124    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10125    cp_parser_declarator.
10126
10127    For the declarator-id production, the representation is as for an
10128    id-expression, except that a qualified name is represented as a
10129    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10130    see the documentation of the FUNCTION_DECLARATOR_* macros for
10131    information about how to find the various declarator components.
10132    An array-declarator is represented as an ARRAY_REF.  The
10133    direct-declarator is the first operand; the constant-expression
10134    indicating the size of the array is the second operand.  */
10135
10136 static tree
10137 cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10138      cp_parser *parser;
10139      cp_parser_declarator_kind dcl_kind;
10140      bool *ctor_dtor_or_conv_p;
10141 {
10142   cp_token *token;
10143   tree declarator = NULL_TREE;
10144   tree scope = NULL_TREE;
10145   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10146   bool saved_in_declarator_p = parser->in_declarator_p;
10147   bool first = true;
10148   
10149   while (true)
10150     {
10151       /* Peek at the next token.  */
10152       token = cp_lexer_peek_token (parser->lexer);
10153       if (token->type == CPP_OPEN_PAREN)
10154         {
10155           /* This is either a parameter-declaration-clause, or a
10156              parenthesized declarator. When we know we are parsing a
10157              named declaratory, it must be a paranthesized declarator
10158              if FIRST is true. For instance, `(int)' is a
10159              parameter-declaration-clause, with an omitted
10160              direct-abstract-declarator. But `((*))', is a
10161              parenthesized abstract declarator. Finally, when T is a
10162              template parameter `(T)' is a
10163              paremeter-declaration-clause, and not a parenthesized
10164              named declarator.
10165              
10166              We first try and parse a parameter-declaration-clause,
10167              and then try a nested declarator (if FIRST is true).
10168
10169              It is not an error for it not to be a
10170              parameter-declaration-clause, even when FIRST is
10171              false. Consider,
10172
10173                int i (int);
10174                int i (3);
10175
10176              The first is the declaration of a function while the
10177              second is a the definition of a variable, including its
10178              initializer.
10179
10180              Having seen only the parenthesis, we cannot know which of
10181              these two alternatives should be selected.  Even more
10182              complex are examples like:
10183
10184                int i (int (a));
10185                int i (int (3));
10186
10187              The former is a function-declaration; the latter is a
10188              variable initialization.  
10189
10190              Thus again, we try a parameter-declation-clause, and if
10191              that fails, we back out and return.  */
10192
10193           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10194             {
10195               tree params;
10196               
10197               cp_parser_parse_tentatively (parser);
10198
10199               /* Consume the `('.  */
10200               cp_lexer_consume_token (parser->lexer);
10201               if (first)
10202                 {
10203                   /* If this is going to be an abstract declarator, we're
10204                      in a declarator and we can't have default args.  */
10205                   parser->default_arg_ok_p = false;
10206                   parser->in_declarator_p = true;
10207                 }
10208           
10209               /* Parse the parameter-declaration-clause.  */
10210               params = cp_parser_parameter_declaration_clause (parser);
10211
10212               /* If all went well, parse the cv-qualifier-seq and the
10213                  exception-specfication.  */
10214               if (cp_parser_parse_definitely (parser))
10215                 {
10216                   tree cv_qualifiers;
10217                   tree exception_specification;
10218                   
10219                   first = false;
10220                   /* Consume the `)'.  */
10221                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10222
10223                   /* Parse the cv-qualifier-seq.  */
10224                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10225                   /* And the exception-specification.  */
10226                   exception_specification 
10227                     = cp_parser_exception_specification_opt (parser);
10228
10229                   /* Create the function-declarator.  */
10230                   declarator = make_call_declarator (declarator,
10231                                                      params,
10232                                                      cv_qualifiers,
10233                                                      exception_specification);
10234                   /* Any subsequent parameter lists are to do with
10235                      return type, so are not those of the declared
10236                      function.  */
10237                   parser->default_arg_ok_p = false;
10238                   
10239                   /* Repeat the main loop.  */
10240                   continue;
10241                 }
10242             }
10243           
10244           /* If this is the first, we can try a parenthesized
10245              declarator.  */
10246           if (first)
10247             {
10248               parser->default_arg_ok_p = saved_default_arg_ok_p;
10249               parser->in_declarator_p = saved_in_declarator_p;
10250               
10251               /* Consume the `('.  */
10252               cp_lexer_consume_token (parser->lexer);
10253               /* Parse the nested declarator.  */
10254               declarator 
10255                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10256               first = false;
10257               /* Expect a `)'.  */
10258               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10259                 declarator = error_mark_node;
10260               if (declarator == error_mark_node)
10261                 break;
10262               
10263               goto handle_declarator;
10264             }
10265           /* Otherwise, we must be done. */
10266           else
10267             break;
10268         }
10269       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10270                && token->type == CPP_OPEN_SQUARE)
10271         {
10272           /* Parse an array-declarator.  */
10273           tree bounds;
10274
10275           first = false;
10276           parser->default_arg_ok_p = false;
10277           parser->in_declarator_p = true;
10278           /* Consume the `['.  */
10279           cp_lexer_consume_token (parser->lexer);
10280           /* Peek at the next token.  */
10281           token = cp_lexer_peek_token (parser->lexer);
10282           /* If the next token is `]', then there is no
10283              constant-expression.  */
10284           if (token->type != CPP_CLOSE_SQUARE)
10285             bounds = cp_parser_constant_expression (parser);
10286           else
10287             bounds = NULL_TREE;
10288           /* Look for the closing `]'.  */
10289           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10290             {
10291               declarator = error_mark_node;
10292               break;
10293             }
10294
10295           declarator = build_nt (ARRAY_REF, declarator, bounds);
10296         }
10297       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10298         {
10299           /* Parse a declarator_id */
10300           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10301             cp_parser_parse_tentatively (parser);
10302           declarator = cp_parser_declarator_id (parser);
10303           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10304               && !cp_parser_parse_definitely (parser))
10305             declarator = error_mark_node;
10306           if (declarator == error_mark_node)
10307             break;
10308           
10309           if (TREE_CODE (declarator) == SCOPE_REF)
10310             {
10311               tree scope = TREE_OPERAND (declarator, 0);
10312           
10313               /* In the declaration of a member of a template class
10314                  outside of the class itself, the SCOPE will sometimes
10315                  be a TYPENAME_TYPE.  For example, given:
10316                   
10317                  template <typename T>
10318                  int S<T>::R::i = 3;
10319                   
10320                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10321                  this context, we must resolve S<T>::R to an ordinary
10322                  type, rather than a typename type.
10323                   
10324                  The reason we normally avoid resolving TYPENAME_TYPEs
10325                  is that a specialization of `S' might render
10326                  `S<T>::R' not a type.  However, if `S' is
10327                  specialized, then this `i' will not be used, so there
10328                  is no harm in resolving the types here.  */
10329               if (TREE_CODE (scope) == TYPENAME_TYPE)
10330                 {
10331                   /* Resolve the TYPENAME_TYPE.  */
10332                   scope = cp_parser_resolve_typename_type (parser, scope);
10333                   /* If that failed, the declarator is invalid.  */
10334                   if (scope == error_mark_node)
10335                     return error_mark_node;
10336                   /* Build a new DECLARATOR.  */
10337                   declarator = build_nt (SCOPE_REF, 
10338                                          scope,
10339                                          TREE_OPERAND (declarator, 1));
10340                 }
10341             }
10342       
10343           /* Check to see whether the declarator-id names a constructor, 
10344              destructor, or conversion.  */
10345           if (declarator && ctor_dtor_or_conv_p 
10346               && ((TREE_CODE (declarator) == SCOPE_REF 
10347                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10348                   || (TREE_CODE (declarator) != SCOPE_REF
10349                       && at_class_scope_p ())))
10350             {
10351               tree unqualified_name;
10352               tree class_type;
10353
10354               /* Get the unqualified part of the name.  */
10355               if (TREE_CODE (declarator) == SCOPE_REF)
10356                 {
10357                   class_type = TREE_OPERAND (declarator, 0);
10358                   unqualified_name = TREE_OPERAND (declarator, 1);
10359                 }
10360               else
10361                 {
10362                   class_type = current_class_type;
10363                   unqualified_name = declarator;
10364                 }
10365
10366               /* See if it names ctor, dtor or conv.  */
10367               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10368                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10369                   || constructor_name_p (unqualified_name, class_type))
10370                 *ctor_dtor_or_conv_p = true;
10371             }
10372
10373         handle_declarator:;
10374           scope = get_scope_of_declarator (declarator);
10375           if (scope)
10376             /* Any names that appear after the declarator-id for a member
10377                are looked up in the containing scope.  */
10378             push_scope (scope);
10379           parser->in_declarator_p = true;
10380           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10381               || (declarator
10382                   && (TREE_CODE (declarator) == SCOPE_REF
10383                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10384             /* Default args are only allowed on function
10385                declarations.  */
10386             parser->default_arg_ok_p = saved_default_arg_ok_p;
10387           else
10388             parser->default_arg_ok_p = false;
10389
10390           first = false;
10391         }
10392       /* We're done.  */
10393       else
10394         break;
10395     }
10396
10397   /* For an abstract declarator, we might wind up with nothing at this
10398      point.  That's an error; the declarator is not optional.  */
10399   if (!declarator)
10400     cp_parser_error (parser, "expected declarator");
10401
10402   /* If we entered a scope, we must exit it now.  */
10403   if (scope)
10404     pop_scope (scope);
10405
10406   parser->default_arg_ok_p = saved_default_arg_ok_p;
10407   parser->in_declarator_p = saved_in_declarator_p;
10408   
10409   return declarator;
10410 }
10411
10412 /* Parse a ptr-operator.  
10413
10414    ptr-operator:
10415      * cv-qualifier-seq [opt]
10416      &
10417      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10418
10419    GNU Extension:
10420
10421    ptr-operator:
10422      & cv-qualifier-seq [opt]
10423
10424    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10425    used.  Returns ADDR_EXPR if a reference was used.  In the
10426    case of a pointer-to-member, *TYPE is filled in with the 
10427    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10428    with the cv-qualifier-seq, or NULL_TREE, if there are no
10429    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10430    
10431 static enum tree_code
10432 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10433      cp_parser *parser;
10434      tree *type;
10435      tree *cv_qualifier_seq;
10436 {
10437   enum tree_code code = ERROR_MARK;
10438   cp_token *token;
10439
10440   /* Assume that it's not a pointer-to-member.  */
10441   *type = NULL_TREE;
10442   /* And that there are no cv-qualifiers.  */
10443   *cv_qualifier_seq = NULL_TREE;
10444
10445   /* Peek at the next token.  */
10446   token = cp_lexer_peek_token (parser->lexer);
10447   /* If it's a `*' or `&' we have a pointer or reference.  */
10448   if (token->type == CPP_MULT || token->type == CPP_AND)
10449     {
10450       /* Remember which ptr-operator we were processing.  */
10451       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10452
10453       /* Consume the `*' or `&'.  */
10454       cp_lexer_consume_token (parser->lexer);
10455
10456       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10457          `&', if we are allowing GNU extensions.  (The only qualifier
10458          that can legally appear after `&' is `restrict', but that is
10459          enforced during semantic analysis.  */
10460       if (code == INDIRECT_REF 
10461           || cp_parser_allow_gnu_extensions_p (parser))
10462         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10463     }
10464   else
10465     {
10466       /* Try the pointer-to-member case.  */
10467       cp_parser_parse_tentatively (parser);
10468       /* Look for the optional `::' operator.  */
10469       cp_parser_global_scope_opt (parser,
10470                                   /*current_scope_valid_p=*/false);
10471       /* Look for the nested-name specifier.  */
10472       cp_parser_nested_name_specifier (parser,
10473                                        /*typename_keyword_p=*/false,
10474                                        /*check_dependency_p=*/true,
10475                                        /*type_p=*/false);
10476       /* If we found it, and the next token is a `*', then we are
10477          indeed looking at a pointer-to-member operator.  */
10478       if (!cp_parser_error_occurred (parser)
10479           && cp_parser_require (parser, CPP_MULT, "`*'"))
10480         {
10481           /* The type of which the member is a member is given by the
10482              current SCOPE.  */
10483           *type = parser->scope;
10484           /* The next name will not be qualified.  */
10485           parser->scope = NULL_TREE;
10486           parser->qualifying_scope = NULL_TREE;
10487           parser->object_scope = NULL_TREE;
10488           /* Indicate that the `*' operator was used.  */
10489           code = INDIRECT_REF;
10490           /* Look for the optional cv-qualifier-seq.  */
10491           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10492         }
10493       /* If that didn't work we don't have a ptr-operator.  */
10494       if (!cp_parser_parse_definitely (parser))
10495         cp_parser_error (parser, "expected ptr-operator");
10496     }
10497
10498   return code;
10499 }
10500
10501 /* Parse an (optional) cv-qualifier-seq.
10502
10503    cv-qualifier-seq:
10504      cv-qualifier cv-qualifier-seq [opt]  
10505
10506    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10507    representation of a cv-qualifier.  */
10508
10509 static tree
10510 cp_parser_cv_qualifier_seq_opt (parser)
10511      cp_parser *parser;
10512 {
10513   tree cv_qualifiers = NULL_TREE;
10514   
10515   while (true)
10516     {
10517       tree cv_qualifier;
10518
10519       /* Look for the next cv-qualifier.  */
10520       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10521       /* If we didn't find one, we're done.  */
10522       if (!cv_qualifier)
10523         break;
10524
10525       /* Add this cv-qualifier to the list.  */
10526       cv_qualifiers 
10527         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10528     }
10529
10530   /* We built up the list in reverse order.  */
10531   return nreverse (cv_qualifiers);
10532 }
10533
10534 /* Parse an (optional) cv-qualifier.
10535
10536    cv-qualifier:
10537      const
10538      volatile  
10539
10540    GNU Extension:
10541
10542    cv-qualifier:
10543      __restrict__ */
10544
10545 static tree
10546 cp_parser_cv_qualifier_opt (parser)
10547      cp_parser *parser;
10548 {
10549   cp_token *token;
10550   tree cv_qualifier = NULL_TREE;
10551
10552   /* Peek at the next token.  */
10553   token = cp_lexer_peek_token (parser->lexer);
10554   /* See if it's a cv-qualifier.  */
10555   switch (token->keyword)
10556     {
10557     case RID_CONST:
10558     case RID_VOLATILE:
10559     case RID_RESTRICT:
10560       /* Save the value of the token.  */
10561       cv_qualifier = token->value;
10562       /* Consume the token.  */
10563       cp_lexer_consume_token (parser->lexer);
10564       break;
10565
10566     default:
10567       break;
10568     }
10569
10570   return cv_qualifier;
10571 }
10572
10573 /* Parse a declarator-id.
10574
10575    declarator-id:
10576      id-expression
10577      :: [opt] nested-name-specifier [opt] type-name  
10578
10579    In the `id-expression' case, the value returned is as for
10580    cp_parser_id_expression if the id-expression was an unqualified-id.
10581    If the id-expression was a qualified-id, then a SCOPE_REF is
10582    returned.  The first operand is the scope (either a NAMESPACE_DECL
10583    or TREE_TYPE), but the second is still just a representation of an
10584    unqualified-id.  */
10585
10586 static tree
10587 cp_parser_declarator_id (parser)
10588      cp_parser *parser;
10589 {
10590   tree id_expression;
10591
10592   /* The expression must be an id-expression.  Assume that qualified
10593      names are the names of types so that:
10594
10595        template <class T>
10596        int S<T>::R::i = 3;
10597
10598      will work; we must treat `S<T>::R' as the name of a type.
10599      Similarly, assume that qualified names are templates, where
10600      required, so that:
10601
10602        template <class T>
10603        int S<T>::R<T>::i = 3;
10604
10605      will work, too.  */
10606   id_expression = cp_parser_id_expression (parser,
10607                                            /*template_keyword_p=*/false,
10608                                            /*check_dependency_p=*/false,
10609                                            /*template_p=*/NULL);
10610   /* If the name was qualified, create a SCOPE_REF to represent 
10611      that.  */
10612   if (parser->scope)
10613     id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10614
10615   return id_expression;
10616 }
10617
10618 /* Parse a type-id.
10619
10620    type-id:
10621      type-specifier-seq abstract-declarator [opt]
10622
10623    Returns the TYPE specified.  */
10624
10625 static tree
10626 cp_parser_type_id (parser)
10627      cp_parser *parser;
10628 {
10629   tree type_specifier_seq;
10630   tree abstract_declarator;
10631
10632   /* Parse the type-specifier-seq.  */
10633   type_specifier_seq 
10634     = cp_parser_type_specifier_seq (parser);
10635   if (type_specifier_seq == error_mark_node)
10636     return error_mark_node;
10637
10638   /* There might or might not be an abstract declarator.  */
10639   cp_parser_parse_tentatively (parser);
10640   /* Look for the declarator.  */
10641   abstract_declarator 
10642     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10643   /* Check to see if there really was a declarator.  */
10644   if (!cp_parser_parse_definitely (parser))
10645     abstract_declarator = NULL_TREE;
10646
10647   return groktypename (build_tree_list (type_specifier_seq,
10648                                         abstract_declarator));
10649 }
10650
10651 /* Parse a type-specifier-seq.
10652
10653    type-specifier-seq:
10654      type-specifier type-specifier-seq [opt]
10655
10656    GNU extension:
10657
10658    type-specifier-seq:
10659      attributes type-specifier-seq [opt]
10660
10661    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10662    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10663
10664 static tree
10665 cp_parser_type_specifier_seq (parser)
10666      cp_parser *parser;
10667 {
10668   bool seen_type_specifier = false;
10669   tree type_specifier_seq = NULL_TREE;
10670
10671   /* Parse the type-specifiers and attributes.  */
10672   while (true)
10673     {
10674       tree type_specifier;
10675
10676       /* Check for attributes first.  */
10677       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10678         {
10679           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10680                                           NULL_TREE,
10681                                           type_specifier_seq);
10682           continue;
10683         }
10684
10685       /* After the first type-specifier, others are optional.  */
10686       if (seen_type_specifier)
10687         cp_parser_parse_tentatively (parser);
10688       /* Look for the type-specifier.  */
10689       type_specifier = cp_parser_type_specifier (parser, 
10690                                                  CP_PARSER_FLAGS_NONE,
10691                                                  /*is_friend=*/false,
10692                                                  /*is_declaration=*/false,
10693                                                  NULL,
10694                                                  NULL);
10695       /* If the first type-specifier could not be found, this is not a
10696          type-specifier-seq at all.  */
10697       if (!seen_type_specifier && type_specifier == error_mark_node)
10698         return error_mark_node;
10699       /* If subsequent type-specifiers could not be found, the
10700          type-specifier-seq is complete.  */
10701       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10702         break;
10703
10704       /* Add the new type-specifier to the list.  */
10705       type_specifier_seq 
10706         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10707       seen_type_specifier = true;
10708     }
10709
10710   /* We built up the list in reverse order.  */
10711   return nreverse (type_specifier_seq);
10712 }
10713
10714 /* Parse a parameter-declaration-clause.
10715
10716    parameter-declaration-clause:
10717      parameter-declaration-list [opt] ... [opt]
10718      parameter-declaration-list , ...
10719
10720    Returns a representation for the parameter declarations.  Each node
10721    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10722    representation.)  If the parameter-declaration-clause ends with an
10723    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10724    list.  A return value of NULL_TREE indicates a
10725    parameter-declaration-clause consisting only of an ellipsis.  */
10726
10727 static tree
10728 cp_parser_parameter_declaration_clause (parser)
10729      cp_parser *parser;
10730 {
10731   tree parameters;
10732   cp_token *token;
10733   bool ellipsis_p;
10734
10735   /* Peek at the next token.  */
10736   token = cp_lexer_peek_token (parser->lexer);
10737   /* Check for trivial parameter-declaration-clauses.  */
10738   if (token->type == CPP_ELLIPSIS)
10739     {
10740       /* Consume the `...' token.  */
10741       cp_lexer_consume_token (parser->lexer);
10742       return NULL_TREE;
10743     }
10744   else if (token->type == CPP_CLOSE_PAREN)
10745     /* There are no parameters.  */
10746     {
10747 #ifndef NO_IMPLICIT_EXTERN_C
10748       if (in_system_header && current_class_type == NULL
10749           && current_lang_name == lang_name_c)
10750         return NULL_TREE;
10751       else
10752 #endif
10753         return void_list_node;
10754     }
10755   /* Check for `(void)', too, which is a special case.  */
10756   else if (token->keyword == RID_VOID
10757            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10758                == CPP_CLOSE_PAREN))
10759     {
10760       /* Consume the `void' token.  */
10761       cp_lexer_consume_token (parser->lexer);
10762       /* There are no parameters.  */
10763       return void_list_node;
10764     }
10765   
10766   /* Parse the parameter-declaration-list.  */
10767   parameters = cp_parser_parameter_declaration_list (parser);
10768   /* If a parse error occurred while parsing the
10769      parameter-declaration-list, then the entire
10770      parameter-declaration-clause is erroneous.  */
10771   if (parameters == error_mark_node)
10772     return error_mark_node;
10773
10774   /* Peek at the next token.  */
10775   token = cp_lexer_peek_token (parser->lexer);
10776   /* If it's a `,', the clause should terminate with an ellipsis.  */
10777   if (token->type == CPP_COMMA)
10778     {
10779       /* Consume the `,'.  */
10780       cp_lexer_consume_token (parser->lexer);
10781       /* Expect an ellipsis.  */
10782       ellipsis_p 
10783         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10784     }
10785   /* It might also be `...' if the optional trailing `,' was 
10786      omitted.  */
10787   else if (token->type == CPP_ELLIPSIS)
10788     {
10789       /* Consume the `...' token.  */
10790       cp_lexer_consume_token (parser->lexer);
10791       /* And remember that we saw it.  */
10792       ellipsis_p = true;
10793     }
10794   else
10795     ellipsis_p = false;
10796
10797   /* Finish the parameter list.  */
10798   return finish_parmlist (parameters, ellipsis_p);
10799 }
10800
10801 /* Parse a parameter-declaration-list.
10802
10803    parameter-declaration-list:
10804      parameter-declaration
10805      parameter-declaration-list , parameter-declaration
10806
10807    Returns a representation of the parameter-declaration-list, as for
10808    cp_parser_parameter_declaration_clause.  However, the
10809    `void_list_node' is never appended to the list.  */
10810
10811 static tree
10812 cp_parser_parameter_declaration_list (parser)
10813      cp_parser *parser;
10814 {
10815   tree parameters = NULL_TREE;
10816
10817   /* Look for more parameters.  */
10818   while (true)
10819     {
10820       tree parameter;
10821       /* Parse the parameter.  */
10822       parameter 
10823         = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10824
10825       /* If a parse error ocurred parsing the parameter declaration,
10826          then the entire parameter-declaration-list is erroneous.  */
10827       if (parameter == error_mark_node)
10828         {
10829           parameters = error_mark_node;
10830           break;
10831         }
10832       /* Add the new parameter to the list.  */
10833       TREE_CHAIN (parameter) = parameters;
10834       parameters = parameter;
10835
10836       /* Peek at the next token.  */
10837       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10838           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10839         /* The parameter-declaration-list is complete.  */
10840         break;
10841       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10842         {
10843           cp_token *token;
10844
10845           /* Peek at the next token.  */
10846           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10847           /* If it's an ellipsis, then the list is complete.  */
10848           if (token->type == CPP_ELLIPSIS)
10849             break;
10850           /* Otherwise, there must be more parameters.  Consume the
10851              `,'.  */
10852           cp_lexer_consume_token (parser->lexer);
10853         }
10854       else
10855         {
10856           cp_parser_error (parser, "expected `,' or `...'");
10857           break;
10858         }
10859     }
10860
10861   /* We built up the list in reverse order; straighten it out now.  */
10862   return nreverse (parameters);
10863 }
10864
10865 /* Parse a parameter declaration.
10866
10867    parameter-declaration:
10868      decl-specifier-seq declarator
10869      decl-specifier-seq declarator = assignment-expression
10870      decl-specifier-seq abstract-declarator [opt]
10871      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10872
10873    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10874    declares a template parameter.  (In that case, a non-nested `>'
10875    token encountered during the parsing of the assignment-expression
10876    is not interpreted as a greater-than operator.)
10877
10878    Returns a TREE_LIST representing the parameter-declaration.  The
10879    TREE_VALUE is a representation of the decl-specifier-seq and
10880    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10881    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10882    TREE_VALUE represents the declarator.  */
10883
10884 static tree
10885 cp_parser_parameter_declaration (cp_parser *parser, 
10886                                  bool template_parm_p)
10887 {
10888   bool declares_class_or_enum;
10889   bool greater_than_is_operator_p;
10890   tree decl_specifiers;
10891   tree attributes;
10892   tree declarator;
10893   tree default_argument;
10894   tree parameter;
10895   cp_token *token;
10896   const char *saved_message;
10897
10898   /* In a template parameter, `>' is not an operator.
10899
10900      [temp.param]
10901
10902      When parsing a default template-argument for a non-type
10903      template-parameter, the first non-nested `>' is taken as the end
10904      of the template parameter-list rather than a greater-than
10905      operator.  */
10906   greater_than_is_operator_p = !template_parm_p;
10907
10908   /* Type definitions may not appear in parameter types.  */
10909   saved_message = parser->type_definition_forbidden_message;
10910   parser->type_definition_forbidden_message 
10911     = "types may not be defined in parameter types";
10912
10913   /* Parse the declaration-specifiers.  */
10914   decl_specifiers 
10915     = cp_parser_decl_specifier_seq (parser,
10916                                     CP_PARSER_FLAGS_NONE,
10917                                     &attributes,
10918                                     &declares_class_or_enum);
10919   /* If an error occurred, there's no reason to attempt to parse the
10920      rest of the declaration.  */
10921   if (cp_parser_error_occurred (parser))
10922     {
10923       parser->type_definition_forbidden_message = saved_message;
10924       return error_mark_node;
10925     }
10926
10927   /* Peek at the next token.  */
10928   token = cp_lexer_peek_token (parser->lexer);
10929   /* If the next token is a `)', `,', `=', `>', or `...', then there
10930      is no declarator.  */
10931   if (token->type == CPP_CLOSE_PAREN 
10932       || token->type == CPP_COMMA
10933       || token->type == CPP_EQ
10934       || token->type == CPP_ELLIPSIS
10935       || token->type == CPP_GREATER)
10936     declarator = NULL_TREE;
10937   /* Otherwise, there should be a declarator.  */
10938   else
10939     {
10940       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10941       parser->default_arg_ok_p = false;
10942   
10943       declarator = cp_parser_declarator (parser,
10944                                          CP_PARSER_DECLARATOR_EITHER,
10945                                          /*ctor_dtor_or_conv_p=*/NULL);
10946       parser->default_arg_ok_p = saved_default_arg_ok_p;
10947       /* After the declarator, allow more attributes.  */
10948       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10949     }
10950
10951   /* The restriction on defining new types applies only to the type
10952      of the parameter, not to the default argument.  */
10953   parser->type_definition_forbidden_message = saved_message;
10954
10955   /* If the next token is `=', then process a default argument.  */
10956   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10957     {
10958       bool saved_greater_than_is_operator_p;
10959       /* Consume the `='.  */
10960       cp_lexer_consume_token (parser->lexer);
10961
10962       /* If we are defining a class, then the tokens that make up the
10963          default argument must be saved and processed later.  */
10964       if (!template_parm_p && at_class_scope_p () 
10965           && TYPE_BEING_DEFINED (current_class_type))
10966         {
10967           unsigned depth = 0;
10968
10969           /* Create a DEFAULT_ARG to represented the unparsed default
10970              argument.  */
10971           default_argument = make_node (DEFAULT_ARG);
10972           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10973
10974           /* Add tokens until we have processed the entire default
10975              argument.  */
10976           while (true)
10977             {
10978               bool done = false;
10979               cp_token *token;
10980
10981               /* Peek at the next token.  */
10982               token = cp_lexer_peek_token (parser->lexer);
10983               /* What we do depends on what token we have.  */
10984               switch (token->type)
10985                 {
10986                   /* In valid code, a default argument must be
10987                      immediately followed by a `,' `)', or `...'.  */
10988                 case CPP_COMMA:
10989                 case CPP_CLOSE_PAREN:
10990                 case CPP_ELLIPSIS:
10991                   /* If we run into a non-nested `;', `}', or `]',
10992                      then the code is invalid -- but the default
10993                      argument is certainly over.  */
10994                 case CPP_SEMICOLON:
10995                 case CPP_CLOSE_BRACE:
10996                 case CPP_CLOSE_SQUARE:
10997                   if (depth == 0)
10998                     done = true;
10999                   /* Update DEPTH, if necessary.  */
11000                   else if (token->type == CPP_CLOSE_PAREN
11001                            || token->type == CPP_CLOSE_BRACE
11002                            || token->type == CPP_CLOSE_SQUARE)
11003                     --depth;
11004                   break;
11005
11006                 case CPP_OPEN_PAREN:
11007                 case CPP_OPEN_SQUARE:
11008                 case CPP_OPEN_BRACE:
11009                   ++depth;
11010                   break;
11011
11012                 case CPP_GREATER:
11013                   /* If we see a non-nested `>', and `>' is not an
11014                      operator, then it marks the end of the default
11015                      argument.  */
11016                   if (!depth && !greater_than_is_operator_p)
11017                     done = true;
11018                   break;
11019
11020                   /* If we run out of tokens, issue an error message.  */
11021                 case CPP_EOF:
11022                   error ("file ends in default argument");
11023                   done = true;
11024                   break;
11025
11026                 case CPP_NAME:
11027                 case CPP_SCOPE:
11028                   /* In these cases, we should look for template-ids.
11029                      For example, if the default argument is 
11030                      `X<int, double>()', we need to do name lookup to
11031                      figure out whether or not `X' is a template; if
11032                      so, the `,' does not end the deault argument.
11033
11034                      That is not yet done.  */
11035                   break;
11036
11037                 default:
11038                   break;
11039                 }
11040
11041               /* If we've reached the end, stop.  */
11042               if (done)
11043                 break;
11044               
11045               /* Add the token to the token block.  */
11046               token = cp_lexer_consume_token (parser->lexer);
11047               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11048                                          token);
11049             }
11050         }
11051       /* Outside of a class definition, we can just parse the
11052          assignment-expression.  */
11053       else
11054         {
11055           bool saved_local_variables_forbidden_p;
11056
11057           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11058              set correctly.  */
11059           saved_greater_than_is_operator_p 
11060             = parser->greater_than_is_operator_p;
11061           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11062           /* Local variable names (and the `this' keyword) may not
11063              appear in a default argument.  */
11064           saved_local_variables_forbidden_p 
11065             = parser->local_variables_forbidden_p;
11066           parser->local_variables_forbidden_p = true;
11067           /* Parse the assignment-expression.  */
11068           default_argument = cp_parser_assignment_expression (parser);
11069           /* Restore saved state.  */
11070           parser->greater_than_is_operator_p 
11071             = saved_greater_than_is_operator_p;
11072           parser->local_variables_forbidden_p 
11073             = saved_local_variables_forbidden_p; 
11074         }
11075       if (!parser->default_arg_ok_p)
11076         {
11077           pedwarn ("default arguments are only permitted on functions");
11078           if (flag_pedantic_errors)
11079             default_argument = NULL_TREE;
11080         }
11081     }
11082   else
11083     default_argument = NULL_TREE;
11084   
11085   /* Create the representation of the parameter.  */
11086   if (attributes)
11087     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11088   parameter = build_tree_list (default_argument, 
11089                                build_tree_list (decl_specifiers,
11090                                                 declarator));
11091
11092   return parameter;
11093 }
11094
11095 /* Parse a function-definition.  
11096
11097    function-definition:
11098      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11099        function-body 
11100      decl-specifier-seq [opt] declarator function-try-block  
11101
11102    GNU Extension:
11103
11104    function-definition:
11105      __extension__ function-definition 
11106
11107    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
11108    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11109    be a `friend'.  */
11110
11111 static tree
11112 cp_parser_function_definition (parser, friend_p)
11113      cp_parser *parser;
11114      bool *friend_p;
11115 {
11116   tree decl_specifiers;
11117   tree attributes;
11118   tree declarator;
11119   tree fn;
11120   tree access_checks;
11121   cp_token *token;
11122   bool declares_class_or_enum;
11123   bool member_p;
11124   /* The saved value of the PEDANTIC flag.  */
11125   int saved_pedantic;
11126
11127   /* Any pending qualification must be cleared by our caller.  It is
11128      more robust to force the callers to clear PARSER->SCOPE than to
11129      do it here since if the qualification is in effect here, it might
11130      also end up in effect elsewhere that it is not intended.  */
11131   my_friendly_assert (!parser->scope, 20010821);
11132
11133   /* Handle `__extension__'.  */
11134   if (cp_parser_extension_opt (parser, &saved_pedantic))
11135     {
11136       /* Parse the function-definition.  */
11137       fn = cp_parser_function_definition (parser, friend_p);
11138       /* Restore the PEDANTIC flag.  */
11139       pedantic = saved_pedantic;
11140
11141       return fn;
11142     }
11143
11144   /* Check to see if this definition appears in a class-specifier.  */
11145   member_p = (at_class_scope_p () 
11146               && TYPE_BEING_DEFINED (current_class_type));
11147   /* Defer access checks in the decl-specifier-seq until we know what
11148      function is being defined.  There is no need to do this for the
11149      definition of member functions; we cannot be defining a member
11150      from another class.  */
11151   if (!member_p)
11152     cp_parser_start_deferring_access_checks (parser);
11153   /* Parse the decl-specifier-seq.  */
11154   decl_specifiers 
11155     = cp_parser_decl_specifier_seq (parser,
11156                                     CP_PARSER_FLAGS_OPTIONAL,
11157                                     &attributes,
11158                                     &declares_class_or_enum);
11159   /* Figure out whether this declaration is a `friend'.  */
11160   if (friend_p)
11161     *friend_p = cp_parser_friend_p (decl_specifiers);
11162
11163   /* Parse the declarator.  */
11164   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11165                                      /*ctor_dtor_or_conv_p=*/NULL);
11166
11167   /* Gather up any access checks that occurred.  */
11168   if (!member_p)
11169     access_checks = cp_parser_stop_deferring_access_checks (parser);
11170   else
11171     access_checks = NULL_TREE;
11172
11173   /* If something has already gone wrong, we may as well stop now.  */
11174   if (declarator == error_mark_node)
11175     {
11176       /* Skip to the end of the function, or if this wasn't anything
11177          like a function-definition, to a `;' in the hopes of finding
11178          a sensible place from which to continue parsing.  */
11179       cp_parser_skip_to_end_of_block_or_statement (parser);
11180       return error_mark_node;
11181     }
11182
11183   /* The next character should be a `{' (for a simple function
11184      definition), a `:' (for a ctor-initializer), or `try' (for a
11185      function-try block).  */
11186   token = cp_lexer_peek_token (parser->lexer);
11187   if (!cp_parser_token_starts_function_definition_p (token))
11188     {
11189       /* Issue the error-message.  */
11190       cp_parser_error (parser, "expected function-definition");
11191       /* Skip to the next `;'.  */
11192       cp_parser_skip_to_end_of_block_or_statement (parser);
11193
11194       return error_mark_node;
11195     }
11196
11197   /* If we are in a class scope, then we must handle
11198      function-definitions specially.  In particular, we save away the
11199      tokens that make up the function body, and parse them again
11200      later, in order to handle code like:
11201
11202        struct S {
11203          int f () { return i; }
11204          int i;
11205        }; 
11206  
11207      Here, we cannot parse the body of `f' until after we have seen
11208      the declaration of `i'.  */
11209   if (member_p)
11210     {
11211       cp_token_cache *cache;
11212
11213       /* Create the function-declaration.  */
11214       fn = start_method (decl_specifiers, declarator, attributes);
11215       /* If something went badly wrong, bail out now.  */
11216       if (fn == error_mark_node)
11217         {
11218           /* If there's a function-body, skip it.  */
11219           if (cp_parser_token_starts_function_definition_p 
11220               (cp_lexer_peek_token (parser->lexer)))
11221             cp_parser_skip_to_end_of_block_or_statement (parser);
11222           return error_mark_node;
11223         }
11224
11225       /* Create a token cache.  */
11226       cache = cp_token_cache_new ();
11227       /* Save away the tokens that make up the body of the 
11228          function.  */
11229       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11230       /* Handle function try blocks.  */
11231       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11232         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11233
11234       /* Save away the inline definition; we will process it when the
11235          class is complete.  */
11236       DECL_PENDING_INLINE_INFO (fn) = cache;
11237       DECL_PENDING_INLINE_P (fn) = 1;
11238
11239       /* We're done with the inline definition.  */
11240       finish_method (fn);
11241
11242       /* Add FN to the queue of functions to be parsed later.  */
11243       TREE_VALUE (parser->unparsed_functions_queues)
11244         = tree_cons (NULL_TREE, fn, 
11245                      TREE_VALUE (parser->unparsed_functions_queues));
11246
11247       return fn;
11248     }
11249
11250   /* Check that the number of template-parameter-lists is OK.  */
11251   if (!cp_parser_check_declarator_template_parameters (parser, 
11252                                                        declarator))
11253     {
11254       cp_parser_skip_to_end_of_block_or_statement (parser);
11255       return error_mark_node;
11256     }
11257
11258   return (cp_parser_function_definition_from_specifiers_and_declarator
11259           (parser, decl_specifiers, attributes, declarator, access_checks));
11260 }
11261
11262 /* Parse a function-body.
11263
11264    function-body:
11265      compound_statement  */
11266
11267 static void
11268 cp_parser_function_body (cp_parser *parser)
11269 {
11270   cp_parser_compound_statement (parser);
11271 }
11272
11273 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11274    true if a ctor-initializer was present.  */
11275
11276 static bool
11277 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11278 {
11279   tree body;
11280   bool ctor_initializer_p;
11281
11282   /* Begin the function body.  */
11283   body = begin_function_body ();
11284   /* Parse the optional ctor-initializer.  */
11285   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11286   /* Parse the function-body.  */
11287   cp_parser_function_body (parser);
11288   /* Finish the function body.  */
11289   finish_function_body (body);
11290
11291   return ctor_initializer_p;
11292 }
11293
11294 /* Parse an initializer.
11295
11296    initializer:
11297      = initializer-clause
11298      ( expression-list )  
11299
11300    Returns a expression representing the initializer.  If no
11301    initializer is present, NULL_TREE is returned.  
11302
11303    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11304    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11305    set to FALSE if there is no initializer present.  */
11306
11307 static tree
11308 cp_parser_initializer (parser, is_parenthesized_init)
11309      cp_parser *parser;
11310      bool *is_parenthesized_init;
11311 {
11312   cp_token *token;
11313   tree init;
11314
11315   /* Peek at the next token.  */
11316   token = cp_lexer_peek_token (parser->lexer);
11317
11318   /* Let our caller know whether or not this initializer was
11319      parenthesized.  */
11320   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11321
11322   if (token->type == CPP_EQ)
11323     {
11324       /* Consume the `='.  */
11325       cp_lexer_consume_token (parser->lexer);
11326       /* Parse the initializer-clause.  */
11327       init = cp_parser_initializer_clause (parser);
11328     }
11329   else if (token->type == CPP_OPEN_PAREN)
11330     {
11331       /* Consume the `('.  */
11332       cp_lexer_consume_token (parser->lexer);
11333       /* Parse the expression-list.  */
11334       init = cp_parser_expression_list (parser);
11335       /* Consume the `)' token.  */
11336       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11337         cp_parser_skip_to_closing_parenthesis (parser);
11338     }
11339   else
11340     {
11341       /* Anything else is an error.  */
11342       cp_parser_error (parser, "expected initializer");
11343       init = error_mark_node;
11344     }
11345
11346   return init;
11347 }
11348
11349 /* Parse an initializer-clause.  
11350
11351    initializer-clause:
11352      assignment-expression
11353      { initializer-list , [opt] }
11354      { }
11355
11356    Returns an expression representing the initializer.  
11357
11358    If the `assignment-expression' production is used the value
11359    returned is simply a reprsentation for the expression.  
11360
11361    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11362    the elements of the initializer-list (or NULL_TREE, if the last
11363    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11364    NULL_TREE.  There is no way to detect whether or not the optional
11365    trailing `,' was provided.  */
11366
11367 static tree
11368 cp_parser_initializer_clause (parser)
11369      cp_parser *parser;
11370 {
11371   tree initializer;
11372
11373   /* If it is not a `{', then we are looking at an
11374      assignment-expression.  */
11375   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11376     initializer = cp_parser_assignment_expression (parser);
11377   else
11378     {
11379       /* Consume the `{' token.  */
11380       cp_lexer_consume_token (parser->lexer);
11381       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11382       initializer = make_node (CONSTRUCTOR);
11383       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11384          necessary, but check_initializer depends upon it, for 
11385          now.  */
11386       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11387       /* If it's not a `}', then there is a non-trivial initializer.  */
11388       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11389         {
11390           /* Parse the initializer list.  */
11391           CONSTRUCTOR_ELTS (initializer)
11392             = cp_parser_initializer_list (parser);
11393           /* A trailing `,' token is allowed.  */
11394           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11395             cp_lexer_consume_token (parser->lexer);
11396         }
11397
11398       /* Now, there should be a trailing `}'.  */
11399       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11400     }
11401
11402   return initializer;
11403 }
11404
11405 /* Parse an initializer-list.
11406
11407    initializer-list:
11408      initializer-clause
11409      initializer-list , initializer-clause
11410
11411    GNU Extension:
11412    
11413    initializer-list:
11414      identifier : initializer-clause
11415      initializer-list, identifier : initializer-clause
11416
11417    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11418    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11419    IDENTIFIER_NODE naming the field to initialize.   */
11420
11421 static tree
11422 cp_parser_initializer_list (parser)
11423      cp_parser *parser;
11424 {
11425   tree initializers = NULL_TREE;
11426
11427   /* Parse the rest of the list.  */
11428   while (true)
11429     {
11430       cp_token *token;
11431       tree identifier;
11432       tree initializer;
11433       
11434       /* If the next token is an identifier and the following one is a
11435          colon, we are looking at the GNU designated-initializer
11436          syntax.  */
11437       if (cp_parser_allow_gnu_extensions_p (parser)
11438           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11439           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11440         {
11441           /* Consume the identifier.  */
11442           identifier = cp_lexer_consume_token (parser->lexer)->value;
11443           /* Consume the `:'.  */
11444           cp_lexer_consume_token (parser->lexer);
11445         }
11446       else
11447         identifier = NULL_TREE;
11448
11449       /* Parse the initializer.  */
11450       initializer = cp_parser_initializer_clause (parser);
11451
11452       /* Add it to the list.  */
11453       initializers = tree_cons (identifier, initializer, initializers);
11454
11455       /* If the next token is not a comma, we have reached the end of
11456          the list.  */
11457       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11458         break;
11459
11460       /* Peek at the next token.  */
11461       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11462       /* If the next token is a `}', then we're still done.  An
11463          initializer-clause can have a trailing `,' after the
11464          initializer-list and before the closing `}'.  */
11465       if (token->type == CPP_CLOSE_BRACE)
11466         break;
11467
11468       /* Consume the `,' token.  */
11469       cp_lexer_consume_token (parser->lexer);
11470     }
11471
11472   /* The initializers were built up in reverse order, so we need to
11473      reverse them now.  */
11474   return nreverse (initializers);
11475 }
11476
11477 /* Classes [gram.class] */
11478
11479 /* Parse a class-name.
11480
11481    class-name:
11482      identifier
11483      template-id
11484
11485    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11486    to indicate that names looked up in dependent types should be
11487    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11488    keyword has been used to indicate that the name that appears next
11489    is a template.  TYPE_P is true iff the next name should be treated
11490    as class-name, even if it is declared to be some other kind of name
11491    as well.  The accessibility of the class-name is checked iff
11492    CHECK_ACCESS_P is true.  If CHECK_DEPENDENCY_P is FALSE, names are
11493    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
11494    is the class being defined in a class-head.
11495
11496    Returns the TYPE_DECL representing the class.  */
11497
11498 static tree
11499 cp_parser_class_name (cp_parser *parser, 
11500                       bool typename_keyword_p, 
11501                       bool template_keyword_p, 
11502                       bool type_p,
11503                       bool check_access_p,
11504                       bool check_dependency_p,
11505                       bool class_head_p)
11506 {
11507   tree decl;
11508   tree scope;
11509   bool typename_p;
11510   cp_token *token;
11511
11512   /* All class-names start with an identifier.  */
11513   token = cp_lexer_peek_token (parser->lexer);
11514   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11515     {
11516       cp_parser_error (parser, "expected class-name");
11517       return error_mark_node;
11518     }
11519     
11520   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11521      to a template-id, so we save it here.  */
11522   scope = parser->scope;
11523   /* Any name names a type if we're following the `typename' keyword
11524      in a qualified name where the enclosing scope is type-dependent.  */
11525   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11526                 && cp_parser_dependent_type_p (scope));
11527   /* Handle the common case (an identifier, but not a template-id)
11528      efficiently.  */
11529   if (token->type == CPP_NAME 
11530       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11531     {
11532       tree identifier;
11533
11534       /* Look for the identifier.  */
11535       identifier = cp_parser_identifier (parser);
11536       /* If the next token isn't an identifier, we are certainly not
11537          looking at a class-name.  */
11538       if (identifier == error_mark_node)
11539         decl = error_mark_node;
11540       /* If we know this is a type-name, there's no need to look it
11541          up.  */
11542       else if (typename_p)
11543         decl = identifier;
11544       else
11545         {
11546           /* If the next token is a `::', then the name must be a type
11547              name.
11548
11549              [basic.lookup.qual]
11550
11551              During the lookup for a name preceding the :: scope
11552              resolution operator, object, function, and enumerator
11553              names are ignored.  */
11554           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11555             type_p = true;
11556           /* Look up the name.  */
11557           decl = cp_parser_lookup_name (parser, identifier, 
11558                                         check_access_p,
11559                                         type_p,
11560                                         /*is_namespace=*/false,
11561                                         check_dependency_p);
11562         }
11563     }
11564   else
11565     {
11566       /* Try a template-id.  */
11567       decl = cp_parser_template_id (parser, template_keyword_p,
11568                                     check_dependency_p);
11569       if (decl == error_mark_node)
11570         return error_mark_node;
11571     }
11572
11573   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11574
11575   /* If this is a typename, create a TYPENAME_TYPE.  */
11576   if (typename_p && decl != error_mark_node)
11577     decl = TYPE_NAME (make_typename_type (scope, decl,
11578                                           /*complain=*/1));
11579
11580   /* Check to see that it is really the name of a class.  */
11581   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11582       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11583       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11584     /* Situations like this:
11585
11586          template <typename T> struct A {
11587            typename T::template X<int>::I i; 
11588          };
11589
11590        are problematic.  Is `T::template X<int>' a class-name?  The
11591        standard does not seem to be definitive, but there is no other
11592        valid interpretation of the following `::'.  Therefore, those
11593        names are considered class-names.  */
11594     decl = TYPE_NAME (make_typename_type (scope, decl, 
11595                                           tf_error | tf_parsing));
11596   else if (decl == error_mark_node
11597            || TREE_CODE (decl) != TYPE_DECL
11598            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11599     {
11600       cp_parser_error (parser, "expected class-name");
11601       return error_mark_node;
11602     }
11603
11604   return decl;
11605 }
11606
11607 /* Parse a class-specifier.
11608
11609    class-specifier:
11610      class-head { member-specification [opt] }
11611
11612    Returns the TREE_TYPE representing the class.  */
11613
11614 static tree
11615 cp_parser_class_specifier (parser)
11616      cp_parser *parser;
11617 {
11618   cp_token *token;
11619   tree type;
11620   tree attributes = NULL_TREE;
11621   int has_trailing_semicolon;
11622   bool nested_name_specifier_p;
11623   bool deferring_access_checks_p;
11624   tree saved_access_checks;
11625   unsigned saved_num_template_parameter_lists;
11626
11627   /* Parse the class-head.  */
11628   type = cp_parser_class_head (parser,
11629                                &nested_name_specifier_p,
11630                                &deferring_access_checks_p,
11631                                &saved_access_checks);
11632   /* If the class-head was a semantic disaster, skip the entire body
11633      of the class.  */
11634   if (!type)
11635     {
11636       cp_parser_skip_to_end_of_block_or_statement (parser);
11637       return error_mark_node;
11638     }
11639   /* Look for the `{'.  */
11640   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11641     return error_mark_node;
11642   /* Issue an error message if type-definitions are forbidden here.  */
11643   cp_parser_check_type_definition (parser);
11644   /* Remember that we are defining one more class.  */
11645   ++parser->num_classes_being_defined;
11646   /* Inside the class, surrounding template-parameter-lists do not
11647      apply.  */
11648   saved_num_template_parameter_lists 
11649     = parser->num_template_parameter_lists; 
11650   parser->num_template_parameter_lists = 0;
11651   /* Start the class.  */
11652   type = begin_class_definition (type);
11653   if (type == error_mark_node)
11654     /* If the type is erroneous, skip the entire body of the class. */
11655     cp_parser_skip_to_closing_brace (parser);
11656   else
11657     /* Parse the member-specification.  */
11658     cp_parser_member_specification_opt (parser);
11659   /* Look for the trailing `}'.  */
11660   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11661   /* We get better error messages by noticing a common problem: a
11662      missing trailing `;'.  */
11663   token = cp_lexer_peek_token (parser->lexer);
11664   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11665   /* Look for attributes to apply to this class.  */
11666   if (cp_parser_allow_gnu_extensions_p (parser))
11667     attributes = cp_parser_attributes_opt (parser);
11668   /* Finish the class definition.  */
11669   type = finish_class_definition (type, 
11670                                   attributes,
11671                                   has_trailing_semicolon,
11672                                   nested_name_specifier_p);
11673   /* If this class is not itself within the scope of another class,
11674      then we need to parse the bodies of all of the queued function
11675      definitions.  Note that the queued functions defined in a class
11676      are not always processed immediately following the
11677      class-specifier for that class.  Consider:
11678
11679        struct A {
11680          struct B { void f() { sizeof (A); } };
11681        };
11682
11683      If `f' were processed before the processing of `A' were
11684      completed, there would be no way to compute the size of `A'.
11685      Note that the nesting we are interested in here is lexical --
11686      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11687      for:
11688
11689        struct A { struct B; };
11690        struct A::B { void f() { } };
11691
11692      there is no need to delay the parsing of `A::B::f'.  */
11693   if (--parser->num_classes_being_defined == 0) 
11694     {
11695       tree last_scope = NULL_TREE;
11696       tree queue_entry;
11697       tree fn;
11698
11699       /* Reverse the queue, so that we process it in the order the
11700          functions were declared.  */
11701       TREE_VALUE (parser->unparsed_functions_queues)
11702         = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11703       /* In a first pass, parse default arguments to the functions.
11704          Then, in a second pass, parse the bodies of the functions.
11705          This two-phased approach handles cases like:
11706          
11707             struct S { 
11708               void f() { g(); } 
11709               void g(int i = 3);
11710             };
11711
11712          */
11713       for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11714            queue_entry;
11715            queue_entry = TREE_CHAIN (queue_entry))
11716         {
11717           fn = TREE_VALUE (queue_entry);
11718           if (DECL_FUNCTION_TEMPLATE_P (fn))
11719             fn = DECL_TEMPLATE_RESULT (fn);
11720           /* Make sure that any template parameters are in scope.  */
11721           maybe_begin_member_template_processing (fn);
11722           /* If there are default arguments that have not yet been processed,
11723              take care of them now.  */
11724           cp_parser_late_parsing_default_args (parser, fn);
11725           /* Remove any template parameters from the symbol table.  */
11726           maybe_end_member_template_processing ();
11727         }
11728       /* Now parse the body of the functions.  */
11729       while (TREE_VALUE (parser->unparsed_functions_queues))
11730
11731         {
11732           /* Figure out which function we need to process.  */
11733           queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11734           fn = TREE_VALUE (queue_entry);
11735
11736           /* Parse the function.  */
11737           cp_parser_late_parsing_for_member (parser, fn);
11738
11739           TREE_VALUE (parser->unparsed_functions_queues)
11740             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11741         }
11742
11743       /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11744          more time than we have popped, so me must pop here.  */
11745       if (last_scope)
11746         pop_scope (last_scope);
11747     }
11748
11749   /* Put back any saved access checks.  */
11750   if (deferring_access_checks_p)
11751     {
11752       cp_parser_start_deferring_access_checks (parser);
11753       parser->context->deferred_access_checks = saved_access_checks;
11754     }
11755
11756   /* Restore the count of active template-parameter-lists.  */
11757   parser->num_template_parameter_lists
11758     = saved_num_template_parameter_lists;
11759
11760   return type;
11761 }
11762
11763 /* Parse a class-head.
11764
11765    class-head:
11766      class-key identifier [opt] base-clause [opt]
11767      class-key nested-name-specifier identifier base-clause [opt]
11768      class-key nested-name-specifier [opt] template-id 
11769        base-clause [opt]  
11770
11771    GNU Extensions:
11772      class-key attributes identifier [opt] base-clause [opt]
11773      class-key attributes nested-name-specifier identifier base-clause [opt]
11774      class-key attributes nested-name-specifier [opt] template-id 
11775        base-clause [opt]  
11776
11777    Returns the TYPE of the indicated class.  Sets
11778    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11779    involving a nested-name-specifier was used, and FALSE otherwise.
11780    Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11781    access checks before this class-head.  In that case,
11782    *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11783    checks.  
11784
11785    Returns NULL_TREE if the class-head is syntactically valid, but
11786    semantically invalid in a way that means we should skip the entire
11787    body of the class.  */
11788
11789 static tree
11790 cp_parser_class_head (parser, 
11791                       nested_name_specifier_p,
11792                       deferring_access_checks_p,
11793                       saved_access_checks)
11794      cp_parser *parser;
11795      bool *nested_name_specifier_p;
11796      bool *deferring_access_checks_p;
11797      tree *saved_access_checks;
11798 {
11799   cp_token *token;
11800   tree nested_name_specifier;
11801   enum tag_types class_key;
11802   tree id = NULL_TREE;
11803   tree type = NULL_TREE;
11804   tree attributes;
11805   bool template_id_p = false;
11806   bool qualified_p = false;
11807   bool invalid_nested_name_p = false;
11808   unsigned num_templates;
11809
11810   /* Assume no nested-name-specifier will be present.  */
11811   *nested_name_specifier_p = false;
11812   /* Assume no template parameter lists will be used in defining the
11813      type.  */
11814   num_templates = 0;
11815
11816   /* Look for the class-key.  */
11817   class_key = cp_parser_class_key (parser);
11818   if (class_key == none_type)
11819     return error_mark_node;
11820
11821   /* Parse the attributes.  */
11822   attributes = cp_parser_attributes_opt (parser);
11823
11824   /* If the next token is `::', that is invalid -- but sometimes
11825      people do try to write:
11826
11827        struct ::S {};  
11828
11829      Handle this gracefully by accepting the extra qualifier, and then
11830      issuing an error about it later if this really is a
11831      class-header.  If it turns out just to be an elaborated type
11832      specifier, remain silent.  */
11833   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11834     qualified_p = true;
11835
11836   /* Determine the name of the class.  Begin by looking for an
11837      optional nested-name-specifier.  */
11838   nested_name_specifier 
11839     = cp_parser_nested_name_specifier_opt (parser,
11840                                            /*typename_keyword_p=*/false,
11841                                            /*check_dependency_p=*/true,
11842                                            /*type_p=*/false);
11843   /* If there was a nested-name-specifier, then there *must* be an
11844      identifier.  */
11845   if (nested_name_specifier)
11846     {
11847       /* Although the grammar says `identifier', it really means
11848          `class-name' or `template-name'.  You are only allowed to
11849          define a class that has already been declared with this
11850          syntax.  
11851
11852          The proposed resolution for Core Issue 180 says that whever
11853          you see `class T::X' you should treat `X' as a type-name.
11854          
11855          It is OK to define an inaccessible class; for example:
11856          
11857            class A { class B; };
11858            class A::B {};
11859          
11860          So, we ask cp_parser_class_name not to check accessibility.  
11861
11862          We do not know if we will see a class-name, or a
11863          template-name.  We look for a class-name first, in case the
11864          class-name is a template-id; if we looked for the
11865          template-name first we would stop after the template-name.  */
11866       cp_parser_parse_tentatively (parser);
11867       type = cp_parser_class_name (parser,
11868                                    /*typename_keyword_p=*/false,
11869                                    /*template_keyword_p=*/false,
11870                                    /*type_p=*/true,
11871                                    /*check_access_p=*/false,
11872                                    /*check_dependency_p=*/false,
11873                                    /*class_head_p=*/true);
11874       /* If that didn't work, ignore the nested-name-specifier.  */
11875       if (!cp_parser_parse_definitely (parser))
11876         {
11877           invalid_nested_name_p = true;
11878           id = cp_parser_identifier (parser);
11879           if (id == error_mark_node)
11880             id = NULL_TREE;
11881         }
11882       /* If we could not find a corresponding TYPE, treat this
11883          declaration like an unqualified declaration.  */
11884       if (type == error_mark_node)
11885         nested_name_specifier = NULL_TREE;
11886       /* Otherwise, count the number of templates used in TYPE and its
11887          containing scopes.  */
11888       else 
11889         {
11890           tree scope;
11891
11892           for (scope = TREE_TYPE (type); 
11893                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11894                scope = (TYPE_P (scope) 
11895                         ? TYPE_CONTEXT (scope)
11896                         : DECL_CONTEXT (scope))) 
11897             if (TYPE_P (scope) 
11898                 && CLASS_TYPE_P (scope)
11899                 && CLASSTYPE_TEMPLATE_INFO (scope)
11900                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11901               ++num_templates;
11902         }
11903     }
11904   /* Otherwise, the identifier is optional.  */
11905   else
11906     {
11907       /* We don't know whether what comes next is a template-id,
11908          an identifier, or nothing at all.  */
11909       cp_parser_parse_tentatively (parser);
11910       /* Check for a template-id.  */
11911       id = cp_parser_template_id (parser, 
11912                                   /*template_keyword_p=*/false,
11913                                   /*check_dependency_p=*/true);
11914       /* If that didn't work, it could still be an identifier.  */
11915       if (!cp_parser_parse_definitely (parser))
11916         {
11917           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11918             id = cp_parser_identifier (parser);
11919           else
11920             id = NULL_TREE;
11921         }
11922       else
11923         {
11924           template_id_p = true;
11925           ++num_templates;
11926         }
11927     }
11928
11929   /* If it's not a `:' or a `{' then we can't really be looking at a
11930      class-head, since a class-head only appears as part of a
11931      class-specifier.  We have to detect this situation before calling
11932      xref_tag, since that has irreversible side-effects.  */
11933   if (!cp_parser_next_token_starts_class_definition_p (parser))
11934     {
11935       cp_parser_error (parser, "expected `{' or `:'");
11936       return error_mark_node;
11937     }
11938
11939   /* At this point, we're going ahead with the class-specifier, even
11940      if some other problem occurs.  */
11941   cp_parser_commit_to_tentative_parse (parser);
11942   /* Issue the error about the overly-qualified name now.  */
11943   if (qualified_p)
11944     cp_parser_error (parser,
11945                      "global qualification of class name is invalid");
11946   else if (invalid_nested_name_p)
11947     cp_parser_error (parser,
11948                      "qualified name does not name a class");
11949   /* Make sure that the right number of template parameters were
11950      present.  */
11951   if (!cp_parser_check_template_parameters (parser, num_templates))
11952     /* If something went wrong, there is no point in even trying to
11953        process the class-definition.  */
11954     return NULL_TREE;
11955
11956   /* We do not need to defer access checks for entities declared
11957      within the class.  But, we do need to save any access checks that
11958      are currently deferred and restore them later, in case we are in
11959      the middle of something else.  */
11960   *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11961   if (*deferring_access_checks_p)
11962     *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11963
11964   /* Look up the type.  */
11965   if (template_id_p)
11966     {
11967       type = TREE_TYPE (id);
11968       maybe_process_partial_specialization (type);
11969     }
11970   else if (!nested_name_specifier)
11971     {
11972       /* If the class was unnamed, create a dummy name.  */
11973       if (!id)
11974         id = make_anon_name ();
11975       type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11976     }
11977   else
11978     {
11979       bool new_type_p;
11980       tree class_type;
11981
11982       /* Given:
11983
11984             template <typename T> struct S { struct T };
11985             template <typename T> struct S::T { };
11986
11987          we will get a TYPENAME_TYPE when processing the definition of
11988          `S::T'.  We need to resolve it to the actual type before we
11989          try to define it.  */
11990       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11991         {
11992           type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11993           if (type != error_mark_node)
11994             type = TYPE_NAME (type);
11995         }
11996
11997       maybe_process_partial_specialization (TREE_TYPE (type));
11998       class_type = current_class_type;
11999       type = TREE_TYPE (handle_class_head (class_key, 
12000                                            nested_name_specifier,
12001                                            type,
12002                                            attributes,
12003                                            /*defn_p=*/true,
12004                                            &new_type_p));
12005       if (type != error_mark_node)
12006         {
12007           if (!class_type && TYPE_CONTEXT (type))
12008             *nested_name_specifier_p = true;
12009           else if (class_type && !same_type_p (TYPE_CONTEXT (type),
12010                                                class_type))
12011             *nested_name_specifier_p = true;
12012         }
12013     }
12014   /* Indicate whether this class was declared as a `class' or as a
12015      `struct'.  */
12016   if (TREE_CODE (type) == RECORD_TYPE)
12017     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12018   cp_parser_check_class_key (class_key, type);
12019
12020   /* Enter the scope containing the class; the names of base classes
12021      should be looked up in that context.  For example, given:
12022
12023        struct A { struct B {}; struct C; };
12024        struct A::C : B {};
12025
12026      is valid.  */
12027   if (nested_name_specifier)
12028     push_scope (nested_name_specifier);
12029   /* Now, look for the base-clause.  */
12030   token = cp_lexer_peek_token (parser->lexer);
12031   if (token->type == CPP_COLON)
12032     {
12033       tree bases;
12034
12035       /* Get the list of base-classes.  */
12036       bases = cp_parser_base_clause (parser);
12037       /* Process them.  */
12038       xref_basetypes (type, bases);
12039     }
12040   /* Leave the scope given by the nested-name-specifier.  We will
12041      enter the class scope itself while processing the members.  */
12042   if (nested_name_specifier)
12043     pop_scope (nested_name_specifier);
12044
12045   return type;
12046 }
12047
12048 /* Parse a class-key.
12049
12050    class-key:
12051      class
12052      struct
12053      union
12054
12055    Returns the kind of class-key specified, or none_type to indicate
12056    error.  */
12057
12058 static enum tag_types
12059 cp_parser_class_key (parser)
12060      cp_parser *parser;
12061 {
12062   cp_token *token;
12063   enum tag_types tag_type;
12064
12065   /* Look for the class-key.  */
12066   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12067   if (!token)
12068     return none_type;
12069
12070   /* Check to see if the TOKEN is a class-key.  */
12071   tag_type = cp_parser_token_is_class_key (token);
12072   if (!tag_type)
12073     cp_parser_error (parser, "expected class-key");
12074   return tag_type;
12075 }
12076
12077 /* Parse an (optional) member-specification.
12078
12079    member-specification:
12080      member-declaration member-specification [opt]
12081      access-specifier : member-specification [opt]  */
12082
12083 static void
12084 cp_parser_member_specification_opt (parser)
12085      cp_parser *parser;
12086 {
12087   while (true)
12088     {
12089       cp_token *token;
12090       enum rid keyword;
12091
12092       /* Peek at the next token.  */
12093       token = cp_lexer_peek_token (parser->lexer);
12094       /* If it's a `}', or EOF then we've seen all the members.  */
12095       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12096         break;
12097
12098       /* See if this token is a keyword.  */
12099       keyword = token->keyword;
12100       switch (keyword)
12101         {
12102         case RID_PUBLIC:
12103         case RID_PROTECTED:
12104         case RID_PRIVATE:
12105           /* Consume the access-specifier.  */
12106           cp_lexer_consume_token (parser->lexer);
12107           /* Remember which access-specifier is active.  */
12108           current_access_specifier = token->value;
12109           /* Look for the `:'.  */
12110           cp_parser_require (parser, CPP_COLON, "`:'");
12111           break;
12112
12113         default:
12114           /* Otherwise, the next construction must be a
12115              member-declaration.  */
12116           cp_parser_member_declaration (parser);
12117           reset_type_access_control ();
12118         }
12119     }
12120 }
12121
12122 /* Parse a member-declaration.  
12123
12124    member-declaration:
12125      decl-specifier-seq [opt] member-declarator-list [opt] ;
12126      function-definition ; [opt]
12127      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12128      using-declaration
12129      template-declaration 
12130
12131    member-declarator-list:
12132      member-declarator
12133      member-declarator-list , member-declarator
12134
12135    member-declarator:
12136      declarator pure-specifier [opt] 
12137      declarator constant-initializer [opt]
12138      identifier [opt] : constant-expression 
12139
12140    GNU Extensions:
12141
12142    member-declaration:
12143      __extension__ member-declaration
12144
12145    member-declarator:
12146      declarator attributes [opt] pure-specifier [opt]
12147      declarator attributes [opt] constant-initializer [opt]
12148      identifier [opt] attributes [opt] : constant-expression  */
12149
12150 static void
12151 cp_parser_member_declaration (parser)
12152      cp_parser *parser;
12153 {
12154   tree decl_specifiers;
12155   tree prefix_attributes;
12156   tree decl;
12157   bool declares_class_or_enum;
12158   bool friend_p;
12159   cp_token *token;
12160   int saved_pedantic;
12161
12162   /* Check for the `__extension__' keyword.  */
12163   if (cp_parser_extension_opt (parser, &saved_pedantic))
12164     {
12165       /* Recurse.  */
12166       cp_parser_member_declaration (parser);
12167       /* Restore the old value of the PEDANTIC flag.  */
12168       pedantic = saved_pedantic;
12169
12170       return;
12171     }
12172
12173   /* Check for a template-declaration.  */
12174   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12175     {
12176       /* Parse the template-declaration.  */
12177       cp_parser_template_declaration (parser, /*member_p=*/true);
12178
12179       return;
12180     }
12181
12182   /* Check for a using-declaration.  */
12183   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12184     {
12185       /* Parse the using-declaration.  */
12186       cp_parser_using_declaration (parser);
12187
12188       return;
12189     }
12190   
12191   /* We can't tell whether we're looking at a declaration or a
12192      function-definition.  */
12193   cp_parser_parse_tentatively (parser);
12194
12195   /* Parse the decl-specifier-seq.  */
12196   decl_specifiers 
12197     = cp_parser_decl_specifier_seq (parser,
12198                                     CP_PARSER_FLAGS_OPTIONAL,
12199                                     &prefix_attributes,
12200                                     &declares_class_or_enum);
12201   /* If there is no declarator, then the decl-specifier-seq should
12202      specify a type.  */
12203   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12204     {
12205       /* If there was no decl-specifier-seq, and the next token is a
12206          `;', then we have something like:
12207
12208            struct S { ; };
12209
12210          [class.mem]
12211
12212          Each member-declaration shall declare at least one member
12213          name of the class.  */
12214       if (!decl_specifiers)
12215         {
12216           if (pedantic)
12217             pedwarn ("extra semicolon");
12218         }
12219       else 
12220         {
12221           tree type;
12222           
12223           /* See if this declaration is a friend.  */
12224           friend_p = cp_parser_friend_p (decl_specifiers);
12225           /* If there were decl-specifiers, check to see if there was
12226              a class-declaration.  */
12227           type = check_tag_decl (decl_specifiers);
12228           /* Nested classes have already been added to the class, but
12229              a `friend' needs to be explicitly registered.  */
12230           if (friend_p)
12231             {
12232               /* If the `friend' keyword was present, the friend must
12233                  be introduced with a class-key.  */
12234                if (!declares_class_or_enum)
12235                  error ("a class-key must be used when declaring a friend");
12236                /* In this case:
12237
12238                     template <typename T> struct A { 
12239                       friend struct A<T>::B; 
12240                     };
12241  
12242                   A<T>::B will be represented by a TYPENAME_TYPE, and
12243                   therefore not recognized by check_tag_decl.  */
12244                if (!type)
12245                  {
12246                    tree specifier;
12247
12248                    for (specifier = decl_specifiers; 
12249                         specifier;
12250                         specifier = TREE_CHAIN (specifier))
12251                      {
12252                        tree s = TREE_VALUE (specifier);
12253
12254                        if (TREE_CODE (s) == IDENTIFIER_NODE
12255                            && IDENTIFIER_GLOBAL_VALUE (s))
12256                          type = IDENTIFIER_GLOBAL_VALUE (s);
12257                        if (TREE_CODE (s) == TYPE_DECL)
12258                          s = TREE_TYPE (s);
12259                        if (TYPE_P (s))
12260                          {
12261                            type = s;
12262                            break;
12263                          }
12264                      }
12265                  }
12266                if (!type)
12267                  error ("friend declaration does not name a class or "
12268                         "function");
12269                else
12270                  make_friend_class (current_class_type, type);
12271             }
12272           /* If there is no TYPE, an error message will already have
12273              been issued.  */
12274           else if (!type)
12275             ;
12276           /* An anonymous aggregate has to be handled specially; such
12277              a declaration really declares a data member (with a
12278              particular type), as opposed to a nested class.  */
12279           else if (ANON_AGGR_TYPE_P (type))
12280             {
12281               /* Remove constructors and such from TYPE, now that we
12282                  know it is an anoymous aggregate.  */
12283               fixup_anonymous_aggr (type);
12284               /* And make the corresponding data member.  */
12285               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12286               /* Add it to the class.  */
12287               finish_member_declaration (decl);
12288             }
12289         }
12290     }
12291   else
12292     {
12293       /* See if these declarations will be friends.  */
12294       friend_p = cp_parser_friend_p (decl_specifiers);
12295
12296       /* Keep going until we hit the `;' at the end of the 
12297          declaration.  */
12298       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12299         {
12300           tree attributes = NULL_TREE;
12301           tree first_attribute;
12302
12303           /* Peek at the next token.  */
12304           token = cp_lexer_peek_token (parser->lexer);
12305
12306           /* Check for a bitfield declaration.  */
12307           if (token->type == CPP_COLON
12308               || (token->type == CPP_NAME
12309                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12310                   == CPP_COLON))
12311             {
12312               tree identifier;
12313               tree width;
12314
12315               /* Get the name of the bitfield.  Note that we cannot just
12316                  check TOKEN here because it may have been invalidated by
12317                  the call to cp_lexer_peek_nth_token above.  */
12318               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12319                 identifier = cp_parser_identifier (parser);
12320               else
12321                 identifier = NULL_TREE;
12322
12323               /* Consume the `:' token.  */
12324               cp_lexer_consume_token (parser->lexer);
12325               /* Get the width of the bitfield.  */
12326               width = cp_parser_constant_expression (parser);
12327
12328               /* Look for attributes that apply to the bitfield.  */
12329               attributes = cp_parser_attributes_opt (parser);
12330               /* Remember which attributes are prefix attributes and
12331                  which are not.  */
12332               first_attribute = attributes;
12333               /* Combine the attributes.  */
12334               attributes = chainon (prefix_attributes, attributes);
12335
12336               /* Create the bitfield declaration.  */
12337               decl = grokbitfield (identifier, 
12338                                    decl_specifiers,
12339                                    width);
12340               /* Apply the attributes.  */
12341               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12342             }
12343           else
12344             {
12345               tree declarator;
12346               tree initializer;
12347               tree asm_specification;
12348               bool ctor_dtor_or_conv_p;
12349
12350               /* Parse the declarator.  */
12351               declarator 
12352                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12353                                         &ctor_dtor_or_conv_p);
12354
12355               /* If something went wrong parsing the declarator, make sure
12356                  that we at least consume some tokens.  */
12357               if (declarator == error_mark_node)
12358                 {
12359                   /* Skip to the end of the statement.  */
12360                   cp_parser_skip_to_end_of_statement (parser);
12361                   break;
12362                 }
12363
12364               /* Look for an asm-specification.  */
12365               asm_specification = cp_parser_asm_specification_opt (parser);
12366               /* Look for attributes that apply to the declaration.  */
12367               attributes = cp_parser_attributes_opt (parser);
12368               /* Remember which attributes are prefix attributes and
12369                  which are not.  */
12370               first_attribute = attributes;
12371               /* Combine the attributes.  */
12372               attributes = chainon (prefix_attributes, attributes);
12373
12374               /* If it's an `=', then we have a constant-initializer or a
12375                  pure-specifier.  It is not correct to parse the
12376                  initializer before registering the member declaration
12377                  since the member declaration should be in scope while
12378                  its initializer is processed.  However, the rest of the
12379                  front end does not yet provide an interface that allows
12380                  us to handle this correctly.  */
12381               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12382                 {
12383                   /* In [class.mem]:
12384
12385                      A pure-specifier shall be used only in the declaration of
12386                      a virtual function.  
12387
12388                      A member-declarator can contain a constant-initializer
12389                      only if it declares a static member of integral or
12390                      enumeration type.  
12391
12392                      Therefore, if the DECLARATOR is for a function, we look
12393                      for a pure-specifier; otherwise, we look for a
12394                      constant-initializer.  When we call `grokfield', it will
12395                      perform more stringent semantics checks.  */
12396                   if (TREE_CODE (declarator) == CALL_EXPR)
12397                     initializer = cp_parser_pure_specifier (parser);
12398                   else
12399                     {
12400                       /* This declaration cannot be a function
12401                          definition.  */
12402                       cp_parser_commit_to_tentative_parse (parser);
12403                       /* Parse the initializer.  */
12404                       initializer = cp_parser_constant_initializer (parser);
12405                     }
12406                 }
12407               /* Otherwise, there is no initializer.  */
12408               else
12409                 initializer = NULL_TREE;
12410
12411               /* See if we are probably looking at a function
12412                  definition.  We are certainly not looking at at a
12413                  member-declarator.  Calling `grokfield' has
12414                  side-effects, so we must not do it unless we are sure
12415                  that we are looking at a member-declarator.  */
12416               if (cp_parser_token_starts_function_definition_p 
12417                   (cp_lexer_peek_token (parser->lexer)))
12418                 decl = error_mark_node;
12419               else
12420                 /* Create the declaration.  */
12421                 decl = grokfield (declarator, 
12422                                   decl_specifiers, 
12423                                   initializer,
12424                                   asm_specification,
12425                                   attributes);
12426             }
12427
12428           /* Reset PREFIX_ATTRIBUTES.  */
12429           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12430             attributes = TREE_CHAIN (attributes);
12431           if (attributes)
12432             TREE_CHAIN (attributes) = NULL_TREE;
12433
12434           /* If there is any qualification still in effect, clear it
12435              now; we will be starting fresh with the next declarator.  */
12436           parser->scope = NULL_TREE;
12437           parser->qualifying_scope = NULL_TREE;
12438           parser->object_scope = NULL_TREE;
12439           /* If it's a `,', then there are more declarators.  */
12440           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12441             cp_lexer_consume_token (parser->lexer);
12442           /* If the next token isn't a `;', then we have a parse error.  */
12443           else if (cp_lexer_next_token_is_not (parser->lexer,
12444                                                CPP_SEMICOLON))
12445             {
12446               cp_parser_error (parser, "expected `;'");
12447               /* Skip tokens until we find a `;'  */
12448               cp_parser_skip_to_end_of_statement (parser);
12449
12450               break;
12451             }
12452
12453           if (decl)
12454             {
12455               /* Add DECL to the list of members.  */
12456               if (!friend_p)
12457                 finish_member_declaration (decl);
12458
12459               /* If DECL is a function, we must return
12460                  to parse it later.  (Even though there is no definition,
12461                  there might be default arguments that need handling.)  */
12462               if (TREE_CODE (decl) == FUNCTION_DECL)
12463                 TREE_VALUE (parser->unparsed_functions_queues)
12464                   = tree_cons (NULL_TREE, decl, 
12465                                TREE_VALUE (parser->unparsed_functions_queues));
12466             }
12467         }
12468     }
12469
12470   /* If everything went well, look for the `;'.  */
12471   if (cp_parser_parse_definitely (parser))
12472     {
12473       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12474       return;
12475     }
12476
12477   /* Parse the function-definition.  */
12478   decl = cp_parser_function_definition (parser, &friend_p);
12479   /* If the member was not a friend, declare it here.  */
12480   if (!friend_p)
12481     finish_member_declaration (decl);
12482   /* Peek at the next token.  */
12483   token = cp_lexer_peek_token (parser->lexer);
12484   /* If the next token is a semicolon, consume it.  */
12485   if (token->type == CPP_SEMICOLON)
12486     cp_lexer_consume_token (parser->lexer);
12487 }
12488
12489 /* Parse a pure-specifier.
12490
12491    pure-specifier:
12492      = 0
12493
12494    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12495    Otherwiser, ERROR_MARK_NODE is returned.  */
12496
12497 static tree
12498 cp_parser_pure_specifier (parser)
12499      cp_parser *parser;
12500 {
12501   cp_token *token;
12502
12503   /* Look for the `=' token.  */
12504   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12505     return error_mark_node;
12506   /* Look for the `0' token.  */
12507   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12508   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12509      to get information from the lexer about how the number was
12510      spelled in order to fix this problem.  */
12511   if (!token || !integer_zerop (token->value))
12512     return error_mark_node;
12513
12514   return integer_zero_node;
12515 }
12516
12517 /* Parse a constant-initializer.
12518
12519    constant-initializer:
12520      = constant-expression
12521
12522    Returns a representation of the constant-expression.  */
12523
12524 static tree
12525 cp_parser_constant_initializer (parser)
12526      cp_parser *parser;
12527 {
12528   /* Look for the `=' token.  */
12529   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12530     return error_mark_node;
12531
12532   /* It is invalid to write:
12533
12534        struct S { static const int i = { 7 }; };
12535
12536      */
12537   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12538     {
12539       cp_parser_error (parser,
12540                        "a brace-enclosed initializer is not allowed here");
12541       /* Consume the opening brace.  */
12542       cp_lexer_consume_token (parser->lexer);
12543       /* Skip the initializer.  */
12544       cp_parser_skip_to_closing_brace (parser);
12545       /* Look for the trailing `}'.  */
12546       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12547       
12548       return error_mark_node;
12549     }
12550
12551   return cp_parser_constant_expression (parser);
12552 }
12553
12554 /* Derived classes [gram.class.derived] */
12555
12556 /* Parse a base-clause.
12557
12558    base-clause:
12559      : base-specifier-list  
12560
12561    base-specifier-list:
12562      base-specifier
12563      base-specifier-list , base-specifier
12564
12565    Returns a TREE_LIST representing the base-classes, in the order in
12566    which they were declared.  The representation of each node is as
12567    described by cp_parser_base_specifier.  
12568
12569    In the case that no bases are specified, this function will return
12570    NULL_TREE, not ERROR_MARK_NODE.  */
12571
12572 static tree
12573 cp_parser_base_clause (parser)
12574      cp_parser *parser;
12575 {
12576   tree bases = NULL_TREE;
12577
12578   /* Look for the `:' that begins the list.  */
12579   cp_parser_require (parser, CPP_COLON, "`:'");
12580
12581   /* Scan the base-specifier-list.  */
12582   while (true)
12583     {
12584       cp_token *token;
12585       tree base;
12586
12587       /* Look for the base-specifier.  */
12588       base = cp_parser_base_specifier (parser);
12589       /* Add BASE to the front of the list.  */
12590       if (base != error_mark_node)
12591         {
12592           TREE_CHAIN (base) = bases;
12593           bases = base;
12594         }
12595       /* Peek at the next token.  */
12596       token = cp_lexer_peek_token (parser->lexer);
12597       /* If it's not a comma, then the list is complete.  */
12598       if (token->type != CPP_COMMA)
12599         break;
12600       /* Consume the `,'.  */
12601       cp_lexer_consume_token (parser->lexer);
12602     }
12603
12604   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12605      base class had a qualified name.  However, the next name that
12606      appears is certainly not qualified.  */
12607   parser->scope = NULL_TREE;
12608   parser->qualifying_scope = NULL_TREE;
12609   parser->object_scope = NULL_TREE;
12610
12611   return nreverse (bases);
12612 }
12613
12614 /* Parse a base-specifier.
12615
12616    base-specifier:
12617      :: [opt] nested-name-specifier [opt] class-name
12618      virtual access-specifier [opt] :: [opt] nested-name-specifier
12619        [opt] class-name
12620      access-specifier virtual [opt] :: [opt] nested-name-specifier
12621        [opt] class-name
12622
12623    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12624    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12625    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12626    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12627        
12628 static tree
12629 cp_parser_base_specifier (parser)
12630      cp_parser *parser;
12631 {
12632   cp_token *token;
12633   bool done = false;
12634   bool virtual_p = false;
12635   bool duplicate_virtual_error_issued_p = false;
12636   bool duplicate_access_error_issued_p = false;
12637   bool class_scope_p;
12638   access_kind access = ak_none;
12639   tree access_node;
12640   tree type;
12641
12642   /* Process the optional `virtual' and `access-specifier'.  */
12643   while (!done)
12644     {
12645       /* Peek at the next token.  */
12646       token = cp_lexer_peek_token (parser->lexer);
12647       /* Process `virtual'.  */
12648       switch (token->keyword)
12649         {
12650         case RID_VIRTUAL:
12651           /* If `virtual' appears more than once, issue an error.  */
12652           if (virtual_p && !duplicate_virtual_error_issued_p)
12653             {
12654               cp_parser_error (parser,
12655                                "`virtual' specified more than once in base-specified");
12656               duplicate_virtual_error_issued_p = true;
12657             }
12658
12659           virtual_p = true;
12660
12661           /* Consume the `virtual' token.  */
12662           cp_lexer_consume_token (parser->lexer);
12663
12664           break;
12665
12666         case RID_PUBLIC:
12667         case RID_PROTECTED:
12668         case RID_PRIVATE:
12669           /* If more than one access specifier appears, issue an
12670              error.  */
12671           if (access != ak_none && !duplicate_access_error_issued_p)
12672             {
12673               cp_parser_error (parser,
12674                                "more than one access specifier in base-specified");
12675               duplicate_access_error_issued_p = true;
12676             }
12677
12678           access = ((access_kind) 
12679                     tree_low_cst (ridpointers[(int) token->keyword],
12680                                   /*pos=*/1));
12681
12682           /* Consume the access-specifier.  */
12683           cp_lexer_consume_token (parser->lexer);
12684
12685           break;
12686
12687         default:
12688           done = true;
12689           break;
12690         }
12691     }
12692
12693   /* Map `virtual_p' and `access' onto one of the access 
12694      tree-nodes.  */
12695   if (!virtual_p)
12696     switch (access)
12697       {
12698       case ak_none:
12699         access_node = access_default_node;
12700         break;
12701       case ak_public:
12702         access_node = access_public_node;
12703         break;
12704       case ak_protected:
12705         access_node = access_protected_node;
12706         break;
12707       case ak_private:
12708         access_node = access_private_node;
12709         break;
12710       default:
12711         abort ();
12712       }
12713   else
12714     switch (access)
12715       {
12716       case ak_none:
12717         access_node = access_default_virtual_node;
12718         break;
12719       case ak_public:
12720         access_node = access_public_virtual_node;
12721         break;
12722       case ak_protected:
12723         access_node = access_protected_virtual_node;
12724         break;
12725       case ak_private:
12726         access_node = access_private_virtual_node;
12727         break;
12728       default:
12729         abort ();
12730       }
12731
12732   /* Look for the optional `::' operator.  */
12733   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12734   /* Look for the nested-name-specifier.  The simplest way to
12735      implement:
12736
12737        [temp.res]
12738
12739        The keyword `typename' is not permitted in a base-specifier or
12740        mem-initializer; in these contexts a qualified name that
12741        depends on a template-parameter is implicitly assumed to be a
12742        type name.
12743
12744      is to pretend that we have seen the `typename' keyword at this
12745      point.  */ 
12746   cp_parser_nested_name_specifier_opt (parser,
12747                                        /*typename_keyword_p=*/true,
12748                                        /*check_dependency_p=*/true,
12749                                        /*type_p=*/true);
12750   /* If the base class is given by a qualified name, assume that names
12751      we see are type names or templates, as appropriate.  */
12752   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12753   /* Finally, look for the class-name.  */
12754   type = cp_parser_class_name (parser, 
12755                                class_scope_p,
12756                                class_scope_p,
12757                                /*type_p=*/true,
12758                                /*check_access=*/true,
12759                                /*check_dependency_p=*/true,
12760                                /*class_head_p=*/false);
12761
12762   if (type == error_mark_node)
12763     return error_mark_node;
12764
12765   return finish_base_specifier (access_node, TREE_TYPE (type));
12766 }
12767
12768 /* Exception handling [gram.exception] */
12769
12770 /* Parse an (optional) exception-specification.
12771
12772    exception-specification:
12773      throw ( type-id-list [opt] )
12774
12775    Returns a TREE_LIST representing the exception-specification.  The
12776    TREE_VALUE of each node is a type.  */
12777
12778 static tree
12779 cp_parser_exception_specification_opt (parser)
12780      cp_parser *parser;
12781 {
12782   cp_token *token;
12783   tree type_id_list;
12784
12785   /* Peek at the next token.  */
12786   token = cp_lexer_peek_token (parser->lexer);
12787   /* If it's not `throw', then there's no exception-specification.  */
12788   if (!cp_parser_is_keyword (token, RID_THROW))
12789     return NULL_TREE;
12790
12791   /* Consume the `throw'.  */
12792   cp_lexer_consume_token (parser->lexer);
12793
12794   /* Look for the `('.  */
12795   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12796
12797   /* Peek at the next token.  */
12798   token = cp_lexer_peek_token (parser->lexer);
12799   /* If it's not a `)', then there is a type-id-list.  */
12800   if (token->type != CPP_CLOSE_PAREN)
12801     {
12802       const char *saved_message;
12803
12804       /* Types may not be defined in an exception-specification.  */
12805       saved_message = parser->type_definition_forbidden_message;
12806       parser->type_definition_forbidden_message
12807         = "types may not be defined in an exception-specification";
12808       /* Parse the type-id-list.  */
12809       type_id_list = cp_parser_type_id_list (parser);
12810       /* Restore the saved message.  */
12811       parser->type_definition_forbidden_message = saved_message;
12812     }
12813   else
12814     type_id_list = empty_except_spec;
12815
12816   /* Look for the `)'.  */
12817   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12818
12819   return type_id_list;
12820 }
12821
12822 /* Parse an (optional) type-id-list.
12823
12824    type-id-list:
12825      type-id
12826      type-id-list , type-id
12827
12828    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12829    in the order that the types were presented.  */
12830
12831 static tree
12832 cp_parser_type_id_list (parser)
12833      cp_parser *parser;
12834 {
12835   tree types = NULL_TREE;
12836
12837   while (true)
12838     {
12839       cp_token *token;
12840       tree type;
12841
12842       /* Get the next type-id.  */
12843       type = cp_parser_type_id (parser);
12844       /* Add it to the list.  */
12845       types = add_exception_specifier (types, type, /*complain=*/1);
12846       /* Peek at the next token.  */
12847       token = cp_lexer_peek_token (parser->lexer);
12848       /* If it is not a `,', we are done.  */
12849       if (token->type != CPP_COMMA)
12850         break;
12851       /* Consume the `,'.  */
12852       cp_lexer_consume_token (parser->lexer);
12853     }
12854
12855   return nreverse (types);
12856 }
12857
12858 /* Parse a try-block.
12859
12860    try-block:
12861      try compound-statement handler-seq  */
12862
12863 static tree
12864 cp_parser_try_block (parser)
12865      cp_parser *parser;
12866 {
12867   tree try_block;
12868
12869   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12870   try_block = begin_try_block ();
12871   cp_parser_compound_statement (parser);
12872   finish_try_block (try_block);
12873   cp_parser_handler_seq (parser);
12874   finish_handler_sequence (try_block);
12875
12876   return try_block;
12877 }
12878
12879 /* Parse a function-try-block.
12880
12881    function-try-block:
12882      try ctor-initializer [opt] function-body handler-seq  */
12883
12884 static bool
12885 cp_parser_function_try_block (parser)
12886      cp_parser *parser;
12887 {
12888   tree try_block;
12889   bool ctor_initializer_p;
12890
12891   /* Look for the `try' keyword.  */
12892   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12893     return false;
12894   /* Let the rest of the front-end know where we are.  */
12895   try_block = begin_function_try_block ();
12896   /* Parse the function-body.  */
12897   ctor_initializer_p 
12898     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12899   /* We're done with the `try' part.  */
12900   finish_function_try_block (try_block);
12901   /* Parse the handlers.  */
12902   cp_parser_handler_seq (parser);
12903   /* We're done with the handlers.  */
12904   finish_function_handler_sequence (try_block);
12905
12906   return ctor_initializer_p;
12907 }
12908
12909 /* Parse a handler-seq.
12910
12911    handler-seq:
12912      handler handler-seq [opt]  */
12913
12914 static void
12915 cp_parser_handler_seq (parser)
12916      cp_parser *parser;
12917 {
12918   while (true)
12919     {
12920       cp_token *token;
12921
12922       /* Parse the handler.  */
12923       cp_parser_handler (parser);
12924       /* Peek at the next token.  */
12925       token = cp_lexer_peek_token (parser->lexer);
12926       /* If it's not `catch' then there are no more handlers.  */
12927       if (!cp_parser_is_keyword (token, RID_CATCH))
12928         break;
12929     }
12930 }
12931
12932 /* Parse a handler.
12933
12934    handler:
12935      catch ( exception-declaration ) compound-statement  */
12936
12937 static void
12938 cp_parser_handler (parser)
12939      cp_parser *parser;
12940 {
12941   tree handler;
12942   tree declaration;
12943
12944   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12945   handler = begin_handler ();
12946   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12947   declaration = cp_parser_exception_declaration (parser);
12948   finish_handler_parms (declaration, handler);
12949   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12950   cp_parser_compound_statement (parser);
12951   finish_handler (handler);
12952 }
12953
12954 /* Parse an exception-declaration.
12955
12956    exception-declaration:
12957      type-specifier-seq declarator
12958      type-specifier-seq abstract-declarator
12959      type-specifier-seq
12960      ...  
12961
12962    Returns a VAR_DECL for the declaration, or NULL_TREE if the
12963    ellipsis variant is used.  */
12964
12965 static tree
12966 cp_parser_exception_declaration (parser)
12967      cp_parser *parser;
12968 {
12969   tree type_specifiers;
12970   tree declarator;
12971   const char *saved_message;
12972
12973   /* If it's an ellipsis, it's easy to handle.  */
12974   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12975     {
12976       /* Consume the `...' token.  */
12977       cp_lexer_consume_token (parser->lexer);
12978       return NULL_TREE;
12979     }
12980
12981   /* Types may not be defined in exception-declarations.  */
12982   saved_message = parser->type_definition_forbidden_message;
12983   parser->type_definition_forbidden_message
12984     = "types may not be defined in exception-declarations";
12985
12986   /* Parse the type-specifier-seq.  */
12987   type_specifiers = cp_parser_type_specifier_seq (parser);
12988   /* If it's a `)', then there is no declarator.  */
12989   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12990     declarator = NULL_TREE;
12991   else
12992     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12993                                        /*ctor_dtor_or_conv_p=*/NULL);
12994
12995   /* Restore the saved message.  */
12996   parser->type_definition_forbidden_message = saved_message;
12997
12998   return start_handler_parms (type_specifiers, declarator);
12999 }
13000
13001 /* Parse a throw-expression. 
13002
13003    throw-expression:
13004      throw assignment-expresion [opt]
13005
13006    Returns a THROW_EXPR representing the throw-expression.  */
13007
13008 static tree
13009 cp_parser_throw_expression (parser)
13010      cp_parser *parser;
13011 {
13012   tree expression;
13013
13014   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13015   /* We can't be sure if there is an assignment-expression or not.  */
13016   cp_parser_parse_tentatively (parser);
13017   /* Try it.  */
13018   expression = cp_parser_assignment_expression (parser);
13019   /* If it didn't work, this is just a rethrow.  */
13020   if (!cp_parser_parse_definitely (parser))
13021     expression = NULL_TREE;
13022
13023   return build_throw (expression);
13024 }
13025
13026 /* GNU Extensions */
13027
13028 /* Parse an (optional) asm-specification.
13029
13030    asm-specification:
13031      asm ( string-literal )
13032
13033    If the asm-specification is present, returns a STRING_CST
13034    corresponding to the string-literal.  Otherwise, returns
13035    NULL_TREE.  */
13036
13037 static tree
13038 cp_parser_asm_specification_opt (parser)
13039      cp_parser *parser;
13040 {
13041   cp_token *token;
13042   tree asm_specification;
13043
13044   /* Peek at the next token.  */
13045   token = cp_lexer_peek_token (parser->lexer);
13046   /* If the next token isn't the `asm' keyword, then there's no 
13047      asm-specification.  */
13048   if (!cp_parser_is_keyword (token, RID_ASM))
13049     return NULL_TREE;
13050
13051   /* Consume the `asm' token.  */
13052   cp_lexer_consume_token (parser->lexer);
13053   /* Look for the `('.  */
13054   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13055
13056   /* Look for the string-literal.  */
13057   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13058   if (token)
13059     asm_specification = token->value;
13060   else
13061     asm_specification = NULL_TREE;
13062
13063   /* Look for the `)'.  */
13064   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13065
13066   return asm_specification;
13067 }
13068
13069 /* Parse an asm-operand-list.  
13070
13071    asm-operand-list:
13072      asm-operand
13073      asm-operand-list , asm-operand
13074      
13075    asm-operand:
13076      string-literal ( expression )  
13077      [ string-literal ] string-literal ( expression )
13078
13079    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13080    each node is the expression.  The TREE_PURPOSE is itself a
13081    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13082    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13083    is a STRING_CST for the string literal before the parenthesis.  */
13084
13085 static tree
13086 cp_parser_asm_operand_list (parser)
13087      cp_parser *parser;
13088 {
13089   tree asm_operands = NULL_TREE;
13090
13091   while (true)
13092     {
13093       tree string_literal;
13094       tree expression;
13095       tree name;
13096       cp_token *token;
13097       
13098       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13099         {
13100           /* Consume the `[' token.  */
13101           cp_lexer_consume_token (parser->lexer);
13102           /* Read the operand name.  */
13103           name = cp_parser_identifier (parser);
13104           if (name != error_mark_node) 
13105             name = build_string (IDENTIFIER_LENGTH (name),
13106                                  IDENTIFIER_POINTER (name));
13107           /* Look for the closing `]'.  */
13108           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13109         }
13110       else
13111         name = NULL_TREE;
13112       /* Look for the string-literal.  */
13113       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13114       string_literal = token ? token->value : error_mark_node;
13115       /* Look for the `('.  */
13116       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13117       /* Parse the expression.  */
13118       expression = cp_parser_expression (parser);
13119       /* Look for the `)'.  */
13120       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13121       /* Add this operand to the list.  */
13122       asm_operands = tree_cons (build_tree_list (name, string_literal),
13123                                 expression, 
13124                                 asm_operands);
13125       /* If the next token is not a `,', there are no more 
13126          operands.  */
13127       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13128         break;
13129       /* Consume the `,'.  */
13130       cp_lexer_consume_token (parser->lexer);
13131     }
13132
13133   return nreverse (asm_operands);
13134 }
13135
13136 /* Parse an asm-clobber-list.  
13137
13138    asm-clobber-list:
13139      string-literal
13140      asm-clobber-list , string-literal  
13141
13142    Returns a TREE_LIST, indicating the clobbers in the order that they
13143    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13144
13145 static tree
13146 cp_parser_asm_clobber_list (parser)
13147      cp_parser *parser;
13148 {
13149   tree clobbers = NULL_TREE;
13150
13151   while (true)
13152     {
13153       cp_token *token;
13154       tree string_literal;
13155
13156       /* Look for the string literal.  */
13157       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13158       string_literal = token ? token->value : error_mark_node;
13159       /* Add it to the list.  */
13160       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13161       /* If the next token is not a `,', then the list is 
13162          complete.  */
13163       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13164         break;
13165       /* Consume the `,' token.  */
13166       cp_lexer_consume_token (parser->lexer);
13167     }
13168
13169   return clobbers;
13170 }
13171
13172 /* Parse an (optional) series of attributes.
13173
13174    attributes:
13175      attributes attribute
13176
13177    attribute:
13178      __attribute__ (( attribute-list [opt] ))  
13179
13180    The return value is as for cp_parser_attribute_list.  */
13181      
13182 static tree
13183 cp_parser_attributes_opt (parser)
13184      cp_parser *parser;
13185 {
13186   tree attributes = NULL_TREE;
13187
13188   while (true)
13189     {
13190       cp_token *token;
13191       tree attribute_list;
13192
13193       /* Peek at the next token.  */
13194       token = cp_lexer_peek_token (parser->lexer);
13195       /* If it's not `__attribute__', then we're done.  */
13196       if (token->keyword != RID_ATTRIBUTE)
13197         break;
13198
13199       /* Consume the `__attribute__' keyword.  */
13200       cp_lexer_consume_token (parser->lexer);
13201       /* Look for the two `(' tokens.  */
13202       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13203       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13204
13205       /* Peek at the next token.  */
13206       token = cp_lexer_peek_token (parser->lexer);
13207       if (token->type != CPP_CLOSE_PAREN)
13208         /* Parse the attribute-list.  */
13209         attribute_list = cp_parser_attribute_list (parser);
13210       else
13211         /* If the next token is a `)', then there is no attribute
13212            list.  */
13213         attribute_list = NULL;
13214
13215       /* Look for the two `)' tokens.  */
13216       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13217       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13218
13219       /* Add these new attributes to the list.  */
13220       attributes = chainon (attributes, attribute_list);
13221     }
13222
13223   return attributes;
13224 }
13225
13226 /* Parse an attribute-list.  
13227
13228    attribute-list:  
13229      attribute 
13230      attribute-list , attribute
13231
13232    attribute:
13233      identifier     
13234      identifier ( identifier )
13235      identifier ( identifier , expression-list )
13236      identifier ( expression-list ) 
13237
13238    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13239    TREE_PURPOSE of each node is the identifier indicating which
13240    attribute is in use.  The TREE_VALUE represents the arguments, if
13241    any.  */
13242
13243 static tree
13244 cp_parser_attribute_list (parser)
13245      cp_parser *parser;
13246 {
13247   tree attribute_list = NULL_TREE;
13248
13249   while (true)
13250     {
13251       cp_token *token;
13252       tree identifier;
13253       tree attribute;
13254
13255       /* Look for the identifier.  We also allow keywords here; for
13256          example `__attribute__ ((const))' is legal.  */
13257       token = cp_lexer_peek_token (parser->lexer);
13258       if (token->type != CPP_NAME 
13259           && token->type != CPP_KEYWORD)
13260         return error_mark_node;
13261       /* Consume the token.  */
13262       token = cp_lexer_consume_token (parser->lexer);
13263       
13264       /* Save away the identifier that indicates which attribute this is.  */
13265       identifier = token->value;
13266       attribute = build_tree_list (identifier, NULL_TREE);
13267
13268       /* Peek at the next token.  */
13269       token = cp_lexer_peek_token (parser->lexer);
13270       /* If it's an `(', then parse the attribute arguments.  */
13271       if (token->type == CPP_OPEN_PAREN)
13272         {
13273           tree arguments;
13274           int arguments_allowed_p = 1;
13275
13276           /* Consume the `('.  */
13277           cp_lexer_consume_token (parser->lexer);
13278           /* Peek at the next token.  */
13279           token = cp_lexer_peek_token (parser->lexer);
13280           /* Check to see if the next token is an identifier.  */
13281           if (token->type == CPP_NAME)
13282             {
13283               /* Save the identifier.  */
13284               identifier = token->value;
13285               /* Consume the identifier.  */
13286               cp_lexer_consume_token (parser->lexer);
13287               /* Peek at the next token.  */
13288               token = cp_lexer_peek_token (parser->lexer);
13289               /* If the next token is a `,', then there are some other
13290                  expressions as well.  */
13291               if (token->type == CPP_COMMA)
13292                 /* Consume the comma.  */
13293                 cp_lexer_consume_token (parser->lexer);
13294               else
13295                 arguments_allowed_p = 0;
13296             }
13297           else
13298             identifier = NULL_TREE;
13299
13300           /* If there are arguments, parse them too.  */
13301           if (arguments_allowed_p)
13302             arguments = cp_parser_expression_list (parser);
13303           else
13304             arguments = NULL_TREE;
13305
13306           /* Combine the identifier and the arguments.  */
13307           if (identifier)
13308             arguments = tree_cons (NULL_TREE, identifier, arguments);
13309
13310           /* Save the identifier and arguments away.  */
13311           TREE_VALUE (attribute) = arguments;
13312
13313           /* Look for the closing `)'.  */
13314           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13315         }
13316
13317       /* Add this attribute to the list.  */
13318       TREE_CHAIN (attribute) = attribute_list;
13319       attribute_list = attribute;
13320
13321       /* Now, look for more attributes.  */
13322       token = cp_lexer_peek_token (parser->lexer);
13323       /* If the next token isn't a `,', we're done.  */
13324       if (token->type != CPP_COMMA)
13325         break;
13326
13327       /* Consume the commma and keep going.  */
13328       cp_lexer_consume_token (parser->lexer);
13329     }
13330
13331   /* We built up the list in reverse order.  */
13332   return nreverse (attribute_list);
13333 }
13334
13335 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13336    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13337    current value of the PEDANTIC flag, regardless of whether or not
13338    the `__extension__' keyword is present.  The caller is responsible
13339    for restoring the value of the PEDANTIC flag.  */
13340
13341 static bool
13342 cp_parser_extension_opt (parser, saved_pedantic)
13343      cp_parser *parser;
13344      int *saved_pedantic;
13345 {
13346   /* Save the old value of the PEDANTIC flag.  */
13347   *saved_pedantic = pedantic;
13348
13349   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13350     {
13351       /* Consume the `__extension__' token.  */
13352       cp_lexer_consume_token (parser->lexer);
13353       /* We're not being pedantic while the `__extension__' keyword is
13354          in effect.  */
13355       pedantic = 0;
13356
13357       return true;
13358     }
13359
13360   return false;
13361 }
13362
13363 /* Parse a label declaration.
13364
13365    label-declaration:
13366      __label__ label-declarator-seq ;
13367
13368    label-declarator-seq:
13369      identifier , label-declarator-seq
13370      identifier  */
13371
13372 static void
13373 cp_parser_label_declaration (parser)
13374      cp_parser *parser;
13375 {
13376   /* Look for the `__label__' keyword.  */
13377   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13378
13379   while (true)
13380     {
13381       tree identifier;
13382
13383       /* Look for an identifier.  */
13384       identifier = cp_parser_identifier (parser);
13385       /* Declare it as a lobel.  */
13386       finish_label_decl (identifier);
13387       /* If the next token is a `;', stop.  */
13388       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13389         break;
13390       /* Look for the `,' separating the label declarations.  */
13391       cp_parser_require (parser, CPP_COMMA, "`,'");
13392     }
13393
13394   /* Look for the final `;'.  */
13395   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13396 }
13397
13398 /* Support Functions */
13399
13400 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13401    NAME should have one of the representations used for an
13402    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13403    is returned.  If PARSER->SCOPE is a dependent type, then a
13404    SCOPE_REF is returned.
13405
13406    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13407    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13408    was formed.  Abstractly, such entities should not be passed to this
13409    function, because they do not need to be looked up, but it is
13410    simpler to check for this special case here, rather than at the
13411    call-sites.
13412
13413    In cases not explicitly covered above, this function returns a
13414    DECL, OVERLOAD, or baselink representing the result of the lookup.
13415    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13416    is returned.
13417
13418    If CHECK_ACCESS is TRUE, then access control is performed on the
13419    declaration to which the name resolves, and an error message is
13420    issued if the declaration is inaccessible.
13421
13422    If IS_TYPE is TRUE, bindings that do not refer to types are
13423    ignored.
13424
13425    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13426    are ignored.
13427
13428    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13429    types.  */
13430
13431 static tree
13432 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access, 
13433                        bool is_type, bool is_namespace, bool check_dependency)
13434 {
13435   tree decl;
13436   tree object_type = parser->context->object_type;
13437
13438   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13439      no longer valid.  Note that if we are parsing tentatively, and
13440      the parse fails, OBJECT_TYPE will be automatically restored.  */
13441   parser->context->object_type = NULL_TREE;
13442
13443   if (name == error_mark_node)
13444     return error_mark_node;
13445
13446   /* A template-id has already been resolved; there is no lookup to
13447      do.  */
13448   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13449     return name;
13450   if (BASELINK_P (name))
13451     {
13452       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13453                            == TEMPLATE_ID_EXPR),
13454                           20020909);
13455       return name;
13456     }
13457
13458   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13459      it should already have been checked to make sure that the name
13460      used matches the type being destroyed.  */
13461   if (TREE_CODE (name) == BIT_NOT_EXPR)
13462     {
13463       tree type;
13464
13465       /* Figure out to which type this destructor applies.  */
13466       if (parser->scope)
13467         type = parser->scope;
13468       else if (object_type)
13469         type = object_type;
13470       else
13471         type = current_class_type;
13472       /* If that's not a class type, there is no destructor.  */
13473       if (!type || !CLASS_TYPE_P (type))
13474         return error_mark_node;
13475       /* If it was a class type, return the destructor.  */
13476       return CLASSTYPE_DESTRUCTORS (type);
13477     }
13478
13479   /* By this point, the NAME should be an ordinary identifier.  If
13480      the id-expression was a qualified name, the qualifying scope is
13481      stored in PARSER->SCOPE at this point.  */
13482   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13483                       20000619);
13484   
13485   /* Perform the lookup.  */
13486   if (parser->scope)
13487     { 
13488       bool dependent_type_p;
13489
13490       if (parser->scope == error_mark_node)
13491         return error_mark_node;
13492
13493       /* If the SCOPE is dependent, the lookup must be deferred until
13494          the template is instantiated -- unless we are explicitly
13495          looking up names in uninstantiated templates.  Even then, we
13496          cannot look up the name if the scope is not a class type; it
13497          might, for example, be a template type parameter.  */
13498       dependent_type_p = (TYPE_P (parser->scope)
13499                           && !(parser->in_declarator_p
13500                                && currently_open_class (parser->scope))
13501                           && cp_parser_dependent_type_p (parser->scope));
13502       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13503            && dependent_type_p)
13504         {
13505           if (!is_type)
13506             decl = build_nt (SCOPE_REF, parser->scope, name);
13507           else
13508             /* The resolution to Core Issue 180 says that `struct A::B'
13509                should be considered a type-name, even if `A' is
13510                dependent.  */
13511             decl = TYPE_NAME (make_typename_type (parser->scope,
13512                                                   name,
13513                                                   /*complain=*/1));
13514         }
13515       else
13516         {
13517           /* If PARSER->SCOPE is a dependent type, then it must be a
13518              class type, and we must not be checking dependencies;
13519              otherwise, we would have processed this lookup above.  So
13520              that PARSER->SCOPE is not considered a dependent base by
13521              lookup_member, we must enter the scope here.  */
13522           if (dependent_type_p)
13523             push_scope (parser->scope);
13524           /* If the PARSER->SCOPE is a a template specialization, it
13525              may be instantiated during name lookup.  In that case,
13526              errors may be issued.  Even if we rollback the current
13527              tentative parse, those errors are valid.  */
13528           decl = lookup_qualified_name (parser->scope, name, is_type,
13529                                         /*flags=*/0);
13530           if (dependent_type_p)
13531             pop_scope (parser->scope);
13532         }
13533       parser->qualifying_scope = parser->scope;
13534       parser->object_scope = NULL_TREE;
13535     }
13536   else if (object_type)
13537     {
13538       tree object_decl = NULL_TREE;
13539       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13540          OBJECT_TYPE is not a class.  */
13541       if (CLASS_TYPE_P (object_type))
13542         /* If the OBJECT_TYPE is a template specialization, it may
13543            be instantiated during name lookup.  In that case, errors
13544            may be issued.  Even if we rollback the current tentative
13545            parse, those errors are valid.  */
13546         object_decl = lookup_member (object_type,
13547                                      name,
13548                                      /*protect=*/0, is_type);
13549       /* Look it up in the enclosing context, too.  */
13550       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13551                                is_namespace,
13552                                /*flags=*/0);
13553       parser->object_scope = object_type;
13554       parser->qualifying_scope = NULL_TREE;
13555       if (object_decl)
13556         decl = object_decl;
13557     }
13558   else
13559     {
13560       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13561                                is_namespace,
13562                                /*flags=*/0);
13563       parser->qualifying_scope = NULL_TREE;
13564       parser->object_scope = NULL_TREE;
13565     }
13566
13567   /* If the lookup failed, let our caller know.  */
13568   if (!decl 
13569       || decl == error_mark_node
13570       || (TREE_CODE (decl) == FUNCTION_DECL 
13571           && DECL_ANTICIPATED (decl)))
13572     return error_mark_node;
13573
13574   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13575   if (TREE_CODE (decl) == TREE_LIST)
13576     {
13577       /* The error message we have to print is too complicated for
13578          cp_parser_error, so we incorporate its actions directly.  */
13579       if (!cp_parser_simulate_error (parser))
13580         {
13581           error ("reference to `%D' is ambiguous", name);
13582           print_candidates (decl);
13583         }
13584       return error_mark_node;
13585     }
13586
13587   my_friendly_assert (DECL_P (decl) 
13588                       || TREE_CODE (decl) == OVERLOAD
13589                       || TREE_CODE (decl) == SCOPE_REF
13590                       || BASELINK_P (decl),
13591                       20000619);
13592
13593   /* If we have resolved the name of a member declaration, check to
13594      see if the declaration is accessible.  When the name resolves to
13595      set of overloaded functions, accesibility is checked when
13596      overload resolution is done.  
13597
13598      During an explicit instantiation, access is not checked at all,
13599      as per [temp.explicit].  */
13600   if (check_access && scope_chain->check_access && DECL_P (decl))
13601     {
13602       tree qualifying_type;
13603       
13604       /* Figure out the type through which DECL is being
13605          accessed.  */
13606       qualifying_type 
13607         = cp_parser_scope_through_which_access_occurs (decl,
13608                                                        object_type,
13609                                                        parser->scope);
13610       if (qualifying_type)
13611         {
13612           /* If we are supposed to defer access checks, just record
13613              the information for later.  */
13614           if (parser->context->deferring_access_checks_p)
13615             cp_parser_defer_access_check (parser, qualifying_type, decl);
13616           /* Otherwise, check accessibility now.  */
13617           else
13618             enforce_access (qualifying_type, decl);
13619         }
13620     }
13621
13622   return decl;
13623 }
13624
13625 /* Like cp_parser_lookup_name, but for use in the typical case where
13626    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13627    TRUE.  */
13628
13629 static tree
13630 cp_parser_lookup_name_simple (parser, name)
13631      cp_parser *parser;
13632      tree name;
13633 {
13634   return cp_parser_lookup_name (parser, name, 
13635                                 /*check_access=*/true,
13636                                 /*is_type=*/false,
13637                                 /*is_namespace=*/false,
13638                                 /*check_dependency=*/true);
13639 }
13640
13641 /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
13642    TYPENAME_TYPE corresponds.  Note that this function peers inside
13643    uninstantiated templates and therefore should be used only in
13644    extremely limited situations.  */
13645
13646 static tree
13647 cp_parser_resolve_typename_type (parser, type)
13648      cp_parser *parser;
13649      tree type;
13650 {
13651   tree scope;
13652   tree name;
13653   tree decl;
13654
13655   my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13656                       20010702);
13657
13658   scope = TYPE_CONTEXT (type);
13659   name = DECL_NAME (TYPE_NAME (type));
13660
13661   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13662      it first before we can figure out what NAME refers to.  */
13663   if (TREE_CODE (scope) == TYPENAME_TYPE)
13664     scope = cp_parser_resolve_typename_type (parser, scope);
13665   /* If we don't know what SCOPE refers to, then we cannot resolve the
13666      TYPENAME_TYPE.  */
13667   if (scope == error_mark_node)
13668     return error_mark_node;
13669   /* If the SCOPE is a template type parameter, we have no way of
13670      resolving the name.  */
13671   if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13672     return type;
13673   /* Enter the SCOPE so that name lookup will be resolved as if we
13674      were in the class definition.  In particular, SCOPE will no
13675      longer be considered a dependent type.  */
13676   push_scope (scope);
13677   /* Look up the declaration.  */
13678   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13679   /* If all went well, we got a TYPE_DECL for a non-typename.  */
13680   if (!decl 
13681       || TREE_CODE (decl) != TYPE_DECL 
13682       || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13683     {
13684       cp_parser_error (parser, "could not resolve typename type");
13685       type = error_mark_node;
13686     }
13687   else
13688     type = TREE_TYPE (decl);
13689   /* Leave the SCOPE.  */
13690   pop_scope (scope);
13691
13692   return type;
13693 }
13694
13695 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13696    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13697    true, the DECL indicates the class being defined in a class-head,
13698    or declared in an elaborated-type-specifier.
13699
13700    Otherwise, return DECL.  */
13701
13702 static tree
13703 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13704 {
13705   /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13706      the scope of the class, then treat the TEMPLATE_DECL as a
13707      class-name.  For example, in:
13708
13709        template <class T> struct S {
13710          S s;
13711        };
13712
13713      is OK.  
13714
13715      If the TEMPLATE_DECL is being declared as part of a class-head,
13716      the same translation occurs:
13717
13718        struct A { 
13719          template <typename T> struct B;
13720        };
13721
13722        template <typename T> struct A::B {}; 
13723    
13724      Similarly, in a elaborated-type-specifier:
13725
13726        namespace N { struct X{}; }
13727
13728        struct A {
13729          template <typename T> friend struct N::X;
13730        };
13731
13732      */
13733   if (DECL_CLASS_TEMPLATE_P (decl)
13734       && (tag_name_p
13735           || (current_class_type
13736               && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13737                               current_class_type))))
13738     return DECL_TEMPLATE_RESULT (decl);
13739
13740   return decl;
13741 }
13742
13743 /* If too many, or too few, template-parameter lists apply to the
13744    declarator, issue an error message.  Returns TRUE if all went well,
13745    and FALSE otherwise.  */
13746
13747 static bool
13748 cp_parser_check_declarator_template_parameters (parser, declarator)
13749      cp_parser *parser;
13750      tree declarator;
13751 {
13752   unsigned num_templates;
13753
13754   /* We haven't seen any classes that involve template parameters yet.  */
13755   num_templates = 0;
13756
13757   switch (TREE_CODE (declarator))
13758     {
13759     case CALL_EXPR:
13760     case ARRAY_REF:
13761     case INDIRECT_REF:
13762     case ADDR_EXPR:
13763       {
13764         tree main_declarator = TREE_OPERAND (declarator, 0);
13765         return
13766           cp_parser_check_declarator_template_parameters (parser, 
13767                                                           main_declarator);
13768       }
13769
13770     case SCOPE_REF:
13771       {
13772         tree scope;
13773         tree member;
13774
13775         scope = TREE_OPERAND (declarator, 0);
13776         member = TREE_OPERAND (declarator, 1);
13777
13778         /* If this is a pointer-to-member, then we are not interested
13779            in the SCOPE, because it does not qualify the thing that is
13780            being declared.  */
13781         if (TREE_CODE (member) == INDIRECT_REF)
13782           return (cp_parser_check_declarator_template_parameters
13783                   (parser, member));
13784
13785         while (scope && CLASS_TYPE_P (scope))
13786           {
13787             /* You're supposed to have one `template <...>'
13788                for every template class, but you don't need one
13789                for a full specialization.  For example:
13790                
13791                template <class T> struct S{};
13792                template <> struct S<int> { void f(); };
13793                void S<int>::f () {}
13794                
13795                is correct; there shouldn't be a `template <>' for
13796                the definition of `S<int>::f'.  */
13797             if (CLASSTYPE_TEMPLATE_INFO (scope)
13798                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13799                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13800                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13801               ++num_templates;
13802
13803             scope = TYPE_CONTEXT (scope);
13804           }
13805       }
13806
13807       /* Fall through.  */
13808
13809     default:
13810       /* If the DECLARATOR has the form `X<y>' then it uses one
13811          additional level of template parameters.  */
13812       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13813         ++num_templates;
13814
13815       return cp_parser_check_template_parameters (parser, 
13816                                                   num_templates);
13817     }
13818 }
13819
13820 /* NUM_TEMPLATES were used in the current declaration.  If that is
13821    invalid, return FALSE and issue an error messages.  Otherwise,
13822    return TRUE.  */
13823
13824 static bool
13825 cp_parser_check_template_parameters (parser, num_templates)
13826      cp_parser *parser;
13827      unsigned num_templates;
13828 {
13829   /* If there are more template classes than parameter lists, we have
13830      something like:
13831      
13832        template <class T> void S<T>::R<T>::f ();  */
13833   if (parser->num_template_parameter_lists < num_templates)
13834     {
13835       error ("too few template-parameter-lists");
13836       return false;
13837     }
13838   /* If there are the same number of template classes and parameter
13839      lists, that's OK.  */
13840   if (parser->num_template_parameter_lists == num_templates)
13841     return true;
13842   /* If there are more, but only one more, then we are referring to a
13843      member template.  That's OK too.  */
13844   if (parser->num_template_parameter_lists == num_templates + 1)
13845       return true;
13846   /* Otherwise, there are too many template parameter lists.  We have
13847      something like:
13848
13849      template <class T> template <class U> void S::f();  */
13850   error ("too many template-parameter-lists");
13851   return false;
13852 }
13853
13854 /* Parse a binary-expression of the general form:
13855
13856    binary-expression:
13857      <expr>
13858      binary-expression <token> <expr>
13859
13860    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13861    to parser the <expr>s.  If the first production is used, then the
13862    value returned by FN is returned directly.  Otherwise, a node with
13863    the indicated EXPR_TYPE is returned, with operands corresponding to
13864    the two sub-expressions.  */
13865
13866 static tree
13867 cp_parser_binary_expression (parser, token_tree_map, fn)
13868      cp_parser *parser;
13869      const cp_parser_token_tree_map token_tree_map;
13870      cp_parser_expression_fn fn;
13871 {
13872   tree lhs;
13873
13874   /* Parse the first expression.  */
13875   lhs = (*fn) (parser);
13876   /* Now, look for more expressions.  */
13877   while (true)
13878     {
13879       cp_token *token;
13880       const cp_parser_token_tree_map_node *map_node;
13881       tree rhs;
13882
13883       /* Peek at the next token.  */
13884       token = cp_lexer_peek_token (parser->lexer);
13885       /* If the token is `>', and that's not an operator at the
13886          moment, then we're done.  */
13887       if (token->type == CPP_GREATER
13888           && !parser->greater_than_is_operator_p)
13889         break;
13890       /* If we find one of the tokens we want, build the correspoding
13891          tree representation.  */
13892       for (map_node = token_tree_map; 
13893            map_node->token_type != CPP_EOF;
13894            ++map_node)
13895         if (map_node->token_type == token->type)
13896           {
13897             /* Consume the operator token.  */
13898             cp_lexer_consume_token (parser->lexer);
13899             /* Parse the right-hand side of the expression.  */
13900             rhs = (*fn) (parser);
13901             /* Build the binary tree node.  */
13902             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13903             break;
13904           }
13905
13906       /* If the token wasn't one of the ones we want, we're done.  */
13907       if (map_node->token_type == CPP_EOF)
13908         break;
13909     }
13910
13911   return lhs;
13912 }
13913
13914 /* Parse an optional `::' token indicating that the following name is
13915    from the global namespace.  If so, PARSER->SCOPE is set to the
13916    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13917    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13918    Returns the new value of PARSER->SCOPE, if the `::' token is
13919    present, and NULL_TREE otherwise.  */
13920
13921 static tree
13922 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13923      cp_parser *parser;
13924      bool current_scope_valid_p;
13925 {
13926   cp_token *token;
13927
13928   /* Peek at the next token.  */
13929   token = cp_lexer_peek_token (parser->lexer);
13930   /* If we're looking at a `::' token then we're starting from the
13931      global namespace, not our current location.  */
13932   if (token->type == CPP_SCOPE)
13933     {
13934       /* Consume the `::' token.  */
13935       cp_lexer_consume_token (parser->lexer);
13936       /* Set the SCOPE so that we know where to start the lookup.  */
13937       parser->scope = global_namespace;
13938       parser->qualifying_scope = global_namespace;
13939       parser->object_scope = NULL_TREE;
13940
13941       return parser->scope;
13942     }
13943   else if (!current_scope_valid_p)
13944     {
13945       parser->scope = NULL_TREE;
13946       parser->qualifying_scope = NULL_TREE;
13947       parser->object_scope = NULL_TREE;
13948     }
13949
13950   return NULL_TREE;
13951 }
13952
13953 /* Returns TRUE if the upcoming token sequence is the start of a
13954    constructor declarator.  If FRIEND_P is true, the declarator is
13955    preceded by the `friend' specifier.  */
13956
13957 static bool
13958 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13959 {
13960   bool constructor_p;
13961   tree type_decl = NULL_TREE;
13962   bool nested_name_p;
13963
13964   /* Parse tentatively; we are going to roll back all of the tokens
13965      consumed here.  */
13966   cp_parser_parse_tentatively (parser);
13967   /* Assume that we are looking at a constructor declarator.  */
13968   constructor_p = true;
13969   /* Look for the optional `::' operator.  */
13970   cp_parser_global_scope_opt (parser,
13971                               /*current_scope_valid_p=*/false);
13972   /* Look for the nested-name-specifier.  */
13973   nested_name_p 
13974     = (cp_parser_nested_name_specifier_opt (parser,
13975                                             /*typename_keyword_p=*/false,
13976                                             /*check_dependency_p=*/false,
13977                                             /*type_p=*/false)
13978        != NULL_TREE);
13979   /* Outside of a class-specifier, there must be a
13980      nested-name-specifier.  */
13981   if (!nested_name_p && 
13982       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13983        || friend_p))
13984     constructor_p = false;
13985   /* If we still think that this might be a constructor-declarator,
13986      look for a class-name.  */
13987   if (constructor_p)
13988     {
13989       /* If we have:
13990
13991            template <typename T> struct S { S(); }
13992            template <typename T> S<T>::S ();
13993
13994          we must recognize that the nested `S' names a class.
13995          Similarly, for:
13996
13997            template <typename T> S<T>::S<T> ();
13998
13999          we must recognize that the nested `S' names a template.  */
14000       type_decl = cp_parser_class_name (parser,
14001                                         /*typename_keyword_p=*/false,
14002                                         /*template_keyword_p=*/false,
14003                                         /*type_p=*/false,
14004                                         /*check_access_p=*/false,
14005                                         /*check_dependency_p=*/false,
14006                                         /*class_head_p=*/false);
14007       /* If there was no class-name, then this is not a constructor.  */
14008       constructor_p = !cp_parser_error_occurred (parser);
14009     }
14010   /* If we're still considering a constructor, we have to see a `(',
14011      to begin the parameter-declaration-clause, followed by either a
14012      `)', an `...', or a decl-specifier.  We need to check for a
14013      type-specifier to avoid being fooled into thinking that:
14014
14015        S::S (f) (int);
14016
14017      is a constructor.  (It is actually a function named `f' that
14018      takes one parameter (of type `int') and returns a value of type
14019      `S::S'.  */
14020   if (constructor_p 
14021       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14022     {
14023       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14024           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14025           && !cp_parser_storage_class_specifier_opt (parser))
14026         {
14027           if (current_class_type 
14028               && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
14029             /* The constructor for one class cannot be declared inside
14030                another.  */
14031             constructor_p = false;
14032           else
14033             {
14034               tree type;
14035
14036               /* Names appearing in the type-specifier should be looked up
14037                  in the scope of the class.  */
14038               if (current_class_type)
14039                 type = NULL_TREE;
14040               else
14041                 {
14042                   type = TREE_TYPE (type_decl);
14043                   if (TREE_CODE (type) == TYPENAME_TYPE)
14044                     type = cp_parser_resolve_typename_type (parser, type);
14045                   push_scope (type);
14046                 }
14047               /* Look for the type-specifier.  */
14048               cp_parser_type_specifier (parser,
14049                                         CP_PARSER_FLAGS_NONE,
14050                                         /*is_friend=*/false,
14051                                         /*is_declarator=*/true,
14052                                         /*declares_class_or_enum=*/NULL,
14053                                         /*is_cv_qualifier=*/NULL);
14054               /* Leave the scope of the class.  */
14055               if (type)
14056                 pop_scope (type);
14057
14058               constructor_p = !cp_parser_error_occurred (parser);
14059             }
14060         }
14061     }
14062   else
14063     constructor_p = false;
14064   /* We did not really want to consume any tokens.  */
14065   cp_parser_abort_tentative_parse (parser);
14066
14067   return constructor_p;
14068 }
14069
14070 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14071    ATTRIBUTES, and DECLARATOR.  The ACCESS_CHECKS have been deferred;
14072    they must be performed once we are in the scope of the function.
14073
14074    Returns the function defined.  */
14075
14076 static tree
14077 cp_parser_function_definition_from_specifiers_and_declarator
14078   (parser, decl_specifiers, attributes, declarator, access_checks)
14079      cp_parser *parser;
14080      tree decl_specifiers;
14081      tree attributes;
14082      tree declarator;
14083      tree access_checks;
14084 {
14085   tree fn;
14086   bool success_p;
14087
14088   /* Begin the function-definition.  */
14089   success_p = begin_function_definition (decl_specifiers, 
14090                                          attributes, 
14091                                          declarator);
14092
14093   /* If there were names looked up in the decl-specifier-seq that we
14094      did not check, check them now.  We must wait until we are in the
14095      scope of the function to perform the checks, since the function
14096      might be a friend.  */
14097   cp_parser_perform_deferred_access_checks (access_checks);
14098
14099   if (!success_p)
14100     {
14101       /* If begin_function_definition didn't like the definition, skip
14102          the entire function.  */
14103       error ("invalid function declaration");
14104       cp_parser_skip_to_end_of_block_or_statement (parser);
14105       fn = error_mark_node;
14106     }
14107   else
14108     fn = cp_parser_function_definition_after_declarator (parser,
14109                                                          /*inline_p=*/false);
14110
14111   return fn;
14112 }
14113
14114 /* Parse the part of a function-definition that follows the
14115    declarator.  INLINE_P is TRUE iff this function is an inline
14116    function defined with a class-specifier.
14117
14118    Returns the function defined.  */
14119
14120 static tree 
14121 cp_parser_function_definition_after_declarator (parser, 
14122                                                 inline_p)
14123      cp_parser *parser;
14124      bool inline_p;
14125 {
14126   tree fn;
14127   bool ctor_initializer_p = false;
14128   bool saved_in_unbraced_linkage_specification_p;
14129   unsigned saved_num_template_parameter_lists;
14130
14131   /* If the next token is `return', then the code may be trying to
14132      make use of the "named return value" extension that G++ used to
14133      support.  */
14134   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14135     {
14136       /* Consume the `return' keyword.  */
14137       cp_lexer_consume_token (parser->lexer);
14138       /* Look for the identifier that indicates what value is to be
14139          returned.  */
14140       cp_parser_identifier (parser);
14141       /* Issue an error message.  */
14142       error ("named return values are no longer supported");
14143       /* Skip tokens until we reach the start of the function body.  */
14144       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14145         cp_lexer_consume_token (parser->lexer);
14146     }
14147   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14148      anything declared inside `f'.  */
14149   saved_in_unbraced_linkage_specification_p 
14150     = parser->in_unbraced_linkage_specification_p;
14151   parser->in_unbraced_linkage_specification_p = false;
14152   /* Inside the function, surrounding template-parameter-lists do not
14153      apply.  */
14154   saved_num_template_parameter_lists 
14155     = parser->num_template_parameter_lists; 
14156   parser->num_template_parameter_lists = 0;
14157   /* If the next token is `try', then we are looking at a
14158      function-try-block.  */
14159   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14160     ctor_initializer_p = cp_parser_function_try_block (parser);
14161   /* A function-try-block includes the function-body, so we only do
14162      this next part if we're not processing a function-try-block.  */
14163   else
14164     ctor_initializer_p 
14165       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14166
14167   /* Finish the function.  */
14168   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14169                         (inline_p ? 2 : 0));
14170   /* Generate code for it, if necessary.  */
14171   expand_body (fn);
14172   /* Restore the saved values.  */
14173   parser->in_unbraced_linkage_specification_p 
14174     = saved_in_unbraced_linkage_specification_p;
14175   parser->num_template_parameter_lists 
14176     = saved_num_template_parameter_lists;
14177
14178   return fn;
14179 }
14180
14181 /* Parse a template-declaration, assuming that the `export' (and
14182    `extern') keywords, if present, has already been scanned.  MEMBER_P
14183    is as for cp_parser_template_declaration.  */
14184
14185 static void
14186 cp_parser_template_declaration_after_export (parser, member_p)
14187      cp_parser *parser;
14188      bool member_p;
14189 {
14190   tree decl = NULL_TREE;
14191   tree parameter_list;
14192   bool friend_p = false;
14193
14194   /* Look for the `template' keyword.  */
14195   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14196     return;
14197       
14198   /* And the `<'.  */
14199   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14200     return;
14201       
14202   /* Parse the template parameters.  */
14203   begin_template_parm_list ();
14204   /* If the next token is `>', then we have an invalid
14205      specialization.  Rather than complain about an invalid template
14206      parameter, issue an error message here.  */
14207   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14208     {
14209       cp_parser_error (parser, "invalid explicit specialization");
14210       parameter_list = NULL_TREE;
14211     }
14212   else
14213     parameter_list = cp_parser_template_parameter_list (parser);
14214   parameter_list = end_template_parm_list (parameter_list);
14215   /* Look for the `>'.  */
14216   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14217   /* We just processed one more parameter list.  */
14218   ++parser->num_template_parameter_lists;
14219   /* If the next token is `template', there are more template
14220      parameters.  */
14221   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14222                                       RID_TEMPLATE))
14223     cp_parser_template_declaration_after_export (parser, member_p);
14224   else
14225     {
14226       decl = cp_parser_single_declaration (parser,
14227                                            member_p,
14228                                            &friend_p);
14229
14230       /* If this is a member template declaration, let the front
14231          end know.  */
14232       if (member_p && !friend_p && decl)
14233         decl = finish_member_template_decl (decl);
14234       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14235         make_friend_class (current_class_type, TREE_TYPE (decl));
14236     }
14237   /* We are done with the current parameter list.  */
14238   --parser->num_template_parameter_lists;
14239
14240   /* Finish up.  */
14241   finish_template_decl (parameter_list);
14242
14243   /* Register member declarations.  */
14244   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14245     finish_member_declaration (decl);
14246
14247   /* If DECL is a function template, we must return to parse it later.
14248      (Even though there is no definition, there might be default
14249      arguments that need handling.)  */
14250   if (member_p && decl 
14251       && (TREE_CODE (decl) == FUNCTION_DECL
14252           || DECL_FUNCTION_TEMPLATE_P (decl)))
14253     TREE_VALUE (parser->unparsed_functions_queues)
14254       = tree_cons (NULL_TREE, decl, 
14255                    TREE_VALUE (parser->unparsed_functions_queues));
14256 }
14257
14258 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14259    `function-definition' sequence.  MEMBER_P is true, this declaration
14260    appears in a class scope.
14261
14262    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14263    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14264
14265 static tree
14266 cp_parser_single_declaration (parser, 
14267                               member_p,
14268                               friend_p)
14269      cp_parser *parser;
14270      bool member_p;
14271      bool *friend_p;
14272 {
14273   bool declares_class_or_enum;
14274   tree decl = NULL_TREE;
14275   tree decl_specifiers;
14276   tree attributes;
14277   tree access_checks;
14278
14279   /* Parse the dependent declaration.  We don't know yet
14280      whether it will be a function-definition.  */
14281   cp_parser_parse_tentatively (parser);
14282   /* Defer access checks until we know what is being declared.  */
14283   cp_parser_start_deferring_access_checks (parser);
14284   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14285      alternative.  */
14286   decl_specifiers 
14287     = cp_parser_decl_specifier_seq (parser,
14288                                     CP_PARSER_FLAGS_OPTIONAL,
14289                                     &attributes,
14290                                     &declares_class_or_enum);
14291   /* Gather up the access checks that occurred the
14292      decl-specifier-seq.  */
14293   access_checks = cp_parser_stop_deferring_access_checks (parser);
14294   /* Check for the declaration of a template class.  */
14295   if (declares_class_or_enum)
14296     {
14297       if (cp_parser_declares_only_class_p (parser))
14298         {
14299           decl = shadow_tag (decl_specifiers);
14300           if (decl)
14301             decl = TYPE_NAME (decl);
14302           else
14303             decl = error_mark_node;
14304         }
14305     }
14306   else
14307     decl = NULL_TREE;
14308   /* If it's not a template class, try for a template function.  If
14309      the next token is a `;', then this declaration does not declare
14310      anything.  But, if there were errors in the decl-specifiers, then
14311      the error might well have come from an attempted class-specifier.
14312      In that case, there's no need to warn about a missing declarator.  */
14313   if (!decl
14314       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14315           || !value_member (error_mark_node, decl_specifiers)))
14316     decl = cp_parser_init_declarator (parser, 
14317                                       decl_specifiers,
14318                                       attributes,
14319                                       access_checks,
14320                                       /*function_definition_allowed_p=*/false,
14321                                       member_p,
14322                                       /*function_definition_p=*/NULL);
14323   /* Clear any current qualification; whatever comes next is the start
14324      of something new.  */
14325   parser->scope = NULL_TREE;
14326   parser->qualifying_scope = NULL_TREE;
14327   parser->object_scope = NULL_TREE;
14328   /* Look for a trailing `;' after the declaration.  */
14329   if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14330       && cp_parser_committed_to_tentative_parse (parser))
14331     cp_parser_skip_to_end_of_block_or_statement (parser);
14332   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
14333   if (cp_parser_parse_definitely (parser))
14334     {
14335       if (friend_p)
14336         *friend_p = cp_parser_friend_p (decl_specifiers);
14337     }
14338   /* Otherwise, try a function-definition.  */
14339   else
14340     decl = cp_parser_function_definition (parser, friend_p);
14341
14342   return decl;
14343 }
14344
14345 /* Parse a functional cast to TYPE.  Returns an expression
14346    representing the cast.  */
14347
14348 static tree
14349 cp_parser_functional_cast (parser, type)
14350      cp_parser *parser;
14351      tree type;
14352 {
14353   tree expression_list;
14354
14355   /* Look for the opening `('.  */
14356   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14357     return error_mark_node;
14358   /* If the next token is not an `)', there are arguments to the
14359      cast.  */
14360   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14361     expression_list = cp_parser_expression_list (parser);
14362   else
14363     expression_list = NULL_TREE;
14364   /* Look for the closing `)'.  */
14365   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14366
14367   return build_functional_cast (type, expression_list);
14368 }
14369
14370 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14371    arguments, or the body of the function have not yet been parsed,
14372    parse them now.  */
14373
14374 static void
14375 cp_parser_late_parsing_for_member (parser, member_function)
14376      cp_parser *parser;
14377      tree member_function;
14378 {
14379   cp_lexer *saved_lexer;
14380
14381   /* If this member is a template, get the underlying
14382      FUNCTION_DECL.  */
14383   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14384     member_function = DECL_TEMPLATE_RESULT (member_function);
14385
14386   /* There should not be any class definitions in progress at this
14387      point; the bodies of members are only parsed outside of all class
14388      definitions.  */
14389   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14390   /* While we're parsing the member functions we might encounter more
14391      classes.  We want to handle them right away, but we don't want
14392      them getting mixed up with functions that are currently in the
14393      queue.  */
14394   parser->unparsed_functions_queues
14395     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14396
14397   /* Make sure that any template parameters are in scope.  */
14398   maybe_begin_member_template_processing (member_function);
14399
14400   /* If the body of the function has not yet been parsed, parse it
14401      now.  */
14402   if (DECL_PENDING_INLINE_P (member_function))
14403     {
14404       tree function_scope;
14405       cp_token_cache *tokens;
14406
14407       /* The function is no longer pending; we are processing it.  */
14408       tokens = DECL_PENDING_INLINE_INFO (member_function);
14409       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14410       DECL_PENDING_INLINE_P (member_function) = 0;
14411       /* If this was an inline function in a local class, enter the scope
14412          of the containing function.  */
14413       function_scope = decl_function_context (member_function);
14414       if (function_scope)
14415         push_function_context_to (function_scope);
14416       
14417       /* Save away the current lexer.  */
14418       saved_lexer = parser->lexer;
14419       /* Make a new lexer to feed us the tokens saved for this function.  */
14420       parser->lexer = cp_lexer_new_from_tokens (tokens);
14421       parser->lexer->next = saved_lexer;
14422       
14423       /* Set the current source position to be the location of the first
14424          token in the saved inline body.  */
14425       cp_lexer_set_source_position_from_token 
14426         (parser->lexer,
14427          cp_lexer_peek_token (parser->lexer));
14428       
14429       /* Let the front end know that we going to be defining this
14430          function.  */
14431       start_function (NULL_TREE, member_function, NULL_TREE,
14432                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14433       
14434       /* Now, parse the body of the function.  */
14435       cp_parser_function_definition_after_declarator (parser,
14436                                                       /*inline_p=*/true);
14437       
14438       /* Leave the scope of the containing function.  */
14439       if (function_scope)
14440         pop_function_context_from (function_scope);
14441       /* Restore the lexer.  */
14442       parser->lexer = saved_lexer;
14443     }
14444
14445   /* Remove any template parameters from the symbol table.  */
14446   maybe_end_member_template_processing ();
14447
14448   /* Restore the queue.  */
14449   parser->unparsed_functions_queues 
14450     = TREE_CHAIN (parser->unparsed_functions_queues);
14451 }
14452
14453 /* FN is a FUNCTION_DECL which may contains a parameter with an
14454    unparsed DEFAULT_ARG.  Parse the default args now.  */
14455
14456 static void
14457 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14458 {
14459   cp_lexer *saved_lexer;
14460   cp_token_cache *tokens;
14461   bool saved_local_variables_forbidden_p;
14462   tree parameters;
14463
14464   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14465        parameters;
14466        parameters = TREE_CHAIN (parameters))
14467     {
14468       if (!TREE_PURPOSE (parameters)
14469           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14470         continue;
14471   
14472        /* Save away the current lexer.  */
14473       saved_lexer = parser->lexer;
14474        /* Create a new one, using the tokens we have saved.  */
14475       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14476       parser->lexer = cp_lexer_new_from_tokens (tokens);
14477
14478        /* Set the current source position to be the location of the
14479           first token in the default argument.  */
14480       cp_lexer_set_source_position_from_token 
14481         (parser->lexer, cp_lexer_peek_token (parser->lexer));
14482
14483        /* Local variable names (and the `this' keyword) may not appear
14484           in a default argument.  */
14485       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14486       parser->local_variables_forbidden_p = true;
14487        /* Parse the assignment-expression.  */
14488       if (DECL_CONTEXT (fn))
14489         push_nested_class (DECL_CONTEXT (fn), 1);
14490       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14491       if (DECL_CONTEXT (fn))
14492         pop_nested_class ();
14493
14494        /* Restore saved state.  */
14495       parser->lexer = saved_lexer;
14496       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14497     }
14498 }
14499
14500 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14501    either a TYPE or an expression, depending on the form of the
14502    input.  The KEYWORD indicates which kind of expression we have
14503    encountered.  */
14504
14505 static tree
14506 cp_parser_sizeof_operand (parser, keyword)
14507      cp_parser *parser;
14508      enum rid keyword;
14509 {
14510   static const char *format;
14511   tree expr = NULL_TREE;
14512   const char *saved_message;
14513   bool saved_constant_expression_p;
14514
14515   /* Initialize FORMAT the first time we get here.  */
14516   if (!format)
14517     format = "types may not be defined in `%s' expressions";
14518
14519   /* Types cannot be defined in a `sizeof' expression.  Save away the
14520      old message.  */
14521   saved_message = parser->type_definition_forbidden_message;
14522   /* And create the new one.  */
14523   parser->type_definition_forbidden_message 
14524     = ((const char *) 
14525        xmalloc (strlen (format) 
14526                 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14527                 + 1 /* `\0' */));
14528   sprintf ((char *) parser->type_definition_forbidden_message,
14529            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14530
14531   /* The restrictions on constant-expressions do not apply inside
14532      sizeof expressions.  */
14533   saved_constant_expression_p = parser->constant_expression_p;
14534   parser->constant_expression_p = false;
14535
14536   /* Do not actually evaluate the expression.  */
14537   ++skip_evaluation;
14538   /* If it's a `(', then we might be looking at the type-id
14539      construction.  */
14540   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14541     {
14542       tree type;
14543
14544       /* We can't be sure yet whether we're looking at a type-id or an
14545          expression.  */
14546       cp_parser_parse_tentatively (parser);
14547       /* Consume the `('.  */
14548       cp_lexer_consume_token (parser->lexer);
14549       /* Parse the type-id.  */
14550       type = cp_parser_type_id (parser);
14551       /* Now, look for the trailing `)'.  */
14552       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14553       /* If all went well, then we're done.  */
14554       if (cp_parser_parse_definitely (parser))
14555         {
14556           /* Build a list of decl-specifiers; right now, we have only
14557              a single type-specifier.  */
14558           type = build_tree_list (NULL_TREE,
14559                                   type);
14560
14561           /* Call grokdeclarator to figure out what type this is.  */
14562           expr = grokdeclarator (NULL_TREE,
14563                                  type,
14564                                  TYPENAME,
14565                                  /*initialized=*/0,
14566                                  /*attrlist=*/NULL);
14567         }
14568     }
14569
14570   /* If the type-id production did not work out, then we must be
14571      looking at the unary-expression production.  */
14572   if (!expr)
14573     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14574   /* Go back to evaluating expressions.  */
14575   --skip_evaluation;
14576
14577   /* Free the message we created.  */
14578   free ((char *) parser->type_definition_forbidden_message);
14579   /* And restore the old one.  */
14580   parser->type_definition_forbidden_message = saved_message;
14581   parser->constant_expression_p = saved_constant_expression_p;
14582
14583   return expr;
14584 }
14585
14586 /* If the current declaration has no declarator, return true.  */
14587
14588 static bool
14589 cp_parser_declares_only_class_p (cp_parser *parser)
14590 {
14591   /* If the next token is a `;' or a `,' then there is no 
14592      declarator.  */
14593   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14594           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14595 }
14596
14597 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14598    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14599
14600 static bool
14601 cp_parser_friend_p (decl_specifiers)
14602      tree decl_specifiers;
14603 {
14604   while (decl_specifiers)
14605     {
14606       /* See if this decl-specifier is `friend'.  */
14607       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14608           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14609         return true;
14610
14611       /* Go on to the next decl-specifier.  */
14612       decl_specifiers = TREE_CHAIN (decl_specifiers);
14613     }
14614
14615   return false;
14616 }
14617
14618 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14619    issue an error message indicating that TOKEN_DESC was expected.
14620    
14621    Returns the token consumed, if the token had the appropriate type.
14622    Otherwise, returns NULL.  */
14623
14624 static cp_token *
14625 cp_parser_require (parser, type, token_desc)
14626      cp_parser *parser;
14627      enum cpp_ttype type;
14628      const char *token_desc;
14629 {
14630   if (cp_lexer_next_token_is (parser->lexer, type))
14631     return cp_lexer_consume_token (parser->lexer);
14632   else
14633     {
14634       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14635       if (!cp_parser_simulate_error (parser))
14636         error ("expected %s", token_desc);
14637       return NULL;
14638     }
14639 }
14640
14641 /* Like cp_parser_require, except that tokens will be skipped until
14642    the desired token is found.  An error message is still produced if
14643    the next token is not as expected.  */
14644
14645 static void
14646 cp_parser_skip_until_found (parser, type, token_desc)
14647      cp_parser *parser;
14648      enum cpp_ttype type;
14649      const char *token_desc;
14650 {
14651   cp_token *token;
14652   unsigned nesting_depth = 0;
14653
14654   if (cp_parser_require (parser, type, token_desc))
14655     return;
14656
14657   /* Skip tokens until the desired token is found.  */
14658   while (true)
14659     {
14660       /* Peek at the next token.  */
14661       token = cp_lexer_peek_token (parser->lexer);
14662       /* If we've reached the token we want, consume it and 
14663          stop.  */
14664       if (token->type == type && !nesting_depth)
14665         {
14666           cp_lexer_consume_token (parser->lexer);
14667           return;
14668         }
14669       /* If we've run out of tokens, stop.  */
14670       if (token->type == CPP_EOF)
14671         return;
14672       if (token->type == CPP_OPEN_BRACE 
14673           || token->type == CPP_OPEN_PAREN
14674           || token->type == CPP_OPEN_SQUARE)
14675         ++nesting_depth;
14676       else if (token->type == CPP_CLOSE_BRACE 
14677                || token->type == CPP_CLOSE_PAREN
14678                || token->type == CPP_CLOSE_SQUARE)
14679         {
14680           if (nesting_depth-- == 0)
14681             return;
14682         }
14683       /* Consume this token.  */
14684       cp_lexer_consume_token (parser->lexer);
14685     }
14686 }
14687
14688 /* If the next token is the indicated keyword, consume it.  Otherwise,
14689    issue an error message indicating that TOKEN_DESC was expected.
14690    
14691    Returns the token consumed, if the token had the appropriate type.
14692    Otherwise, returns NULL.  */
14693
14694 static cp_token *
14695 cp_parser_require_keyword (parser, keyword, token_desc)
14696      cp_parser *parser;
14697      enum rid keyword;
14698      const char *token_desc;
14699 {
14700   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14701
14702   if (token && token->keyword != keyword)
14703     {
14704       dyn_string_t error_msg;
14705
14706       /* Format the error message.  */
14707       error_msg = dyn_string_new (0);
14708       dyn_string_append_cstr (error_msg, "expected ");
14709       dyn_string_append_cstr (error_msg, token_desc);
14710       cp_parser_error (parser, error_msg->s);
14711       dyn_string_delete (error_msg);
14712       return NULL;
14713     }
14714
14715   return token;
14716 }
14717
14718 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14719    function-definition.  */
14720
14721 static bool 
14722 cp_parser_token_starts_function_definition_p (token)
14723      cp_token *token;
14724 {
14725   return (/* An ordinary function-body begins with an `{'.  */
14726           token->type == CPP_OPEN_BRACE
14727           /* A ctor-initializer begins with a `:'.  */
14728           || token->type == CPP_COLON
14729           /* A function-try-block begins with `try'.  */
14730           || token->keyword == RID_TRY
14731           /* The named return value extension begins with `return'.  */
14732           || token->keyword == RID_RETURN);
14733 }
14734
14735 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14736    definition.  */
14737
14738 static bool
14739 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14740 {
14741   cp_token *token;
14742
14743   token = cp_lexer_peek_token (parser->lexer);
14744   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14745 }
14746
14747 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14748    or none_type otherwise.  */
14749
14750 static enum tag_types
14751 cp_parser_token_is_class_key (token)
14752      cp_token *token;
14753 {
14754   switch (token->keyword)
14755     {
14756     case RID_CLASS:
14757       return class_type;
14758     case RID_STRUCT:
14759       return record_type;
14760     case RID_UNION:
14761       return union_type;
14762       
14763     default:
14764       return none_type;
14765     }
14766 }
14767
14768 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14769
14770 static void
14771 cp_parser_check_class_key (enum tag_types class_key, tree type)
14772 {
14773   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14774     pedwarn ("`%s' tag used in naming `%#T'",
14775             class_key == union_type ? "union"
14776              : class_key == record_type ? "struct" : "class", 
14777              type);
14778 }
14779                            
14780 /* Look for the `template' keyword, as a syntactic disambiguator.
14781    Return TRUE iff it is present, in which case it will be 
14782    consumed.  */
14783
14784 static bool
14785 cp_parser_optional_template_keyword (cp_parser *parser)
14786 {
14787   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14788     {
14789       /* The `template' keyword can only be used within templates;
14790          outside templates the parser can always figure out what is a
14791          template and what is not.  */
14792       if (!processing_template_decl)
14793         {
14794           error ("`template' (as a disambiguator) is only allowed "
14795                  "within templates");
14796           /* If this part of the token stream is rescanned, the same
14797              error message would be generated.  So, we purge the token
14798              from the stream.  */
14799           cp_lexer_purge_token (parser->lexer);
14800           return false;
14801         }
14802       else
14803         {
14804           /* Consume the `template' keyword.  */
14805           cp_lexer_consume_token (parser->lexer);
14806           return true;
14807         }
14808     }
14809
14810   return false;
14811 }
14812
14813 /* Add tokens to CACHE until an non-nested END token appears.  */
14814
14815 static void
14816 cp_parser_cache_group (cp_parser *parser, 
14817                        cp_token_cache *cache,
14818                        enum cpp_ttype end,
14819                        unsigned depth)
14820 {
14821   while (true)
14822     {
14823       cp_token *token;
14824
14825       /* Abort a parenthesized expression if we encounter a brace.  */
14826       if ((end == CPP_CLOSE_PAREN || depth == 0)
14827           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14828         return;
14829       /* Consume the next token.  */
14830       token = cp_lexer_consume_token (parser->lexer);
14831       /* If we've reached the end of the file, stop.  */
14832       if (token->type == CPP_EOF)
14833         return;
14834       /* Add this token to the tokens we are saving.  */
14835       cp_token_cache_push_token (cache, token);
14836       /* See if it starts a new group.  */
14837       if (token->type == CPP_OPEN_BRACE)
14838         {
14839           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14840           if (depth == 0)
14841             return;
14842         }
14843       else if (token->type == CPP_OPEN_PAREN)
14844         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14845       else if (token->type == end)
14846         return;
14847     }
14848 }
14849
14850 /* Begin parsing tentatively.  We always save tokens while parsing
14851    tentatively so that if the tentative parsing fails we can restore the
14852    tokens.  */
14853
14854 static void
14855 cp_parser_parse_tentatively (parser)
14856      cp_parser *parser;
14857 {
14858   /* Enter a new parsing context.  */
14859   parser->context = cp_parser_context_new (parser->context);
14860   /* Begin saving tokens.  */
14861   cp_lexer_save_tokens (parser->lexer);
14862   /* In order to avoid repetitive access control error messages,
14863      access checks are queued up until we are no longer parsing
14864      tentatively.  */
14865   cp_parser_start_deferring_access_checks (parser);
14866 }
14867
14868 /* Commit to the currently active tentative parse.  */
14869
14870 static void
14871 cp_parser_commit_to_tentative_parse (parser)
14872      cp_parser *parser;
14873 {
14874   cp_parser_context *context;
14875   cp_lexer *lexer;
14876
14877   /* Mark all of the levels as committed.  */
14878   lexer = parser->lexer;
14879   for (context = parser->context; context->next; context = context->next)
14880     {
14881       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14882         break;
14883       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14884       while (!cp_lexer_saving_tokens (lexer))
14885         lexer = lexer->next;
14886       cp_lexer_commit_tokens (lexer);
14887     }
14888 }
14889
14890 /* Abort the currently active tentative parse.  All consumed tokens
14891    will be rolled back, and no diagnostics will be issued.  */
14892
14893 static void
14894 cp_parser_abort_tentative_parse (parser)
14895      cp_parser *parser;
14896 {
14897   cp_parser_simulate_error (parser);
14898   /* Now, pretend that we want to see if the construct was
14899      successfully parsed.  */
14900   cp_parser_parse_definitely (parser);
14901 }
14902
14903 /* Stop parsing tentatively.  If a parse error has ocurred, restore the
14904    token stream.  Otherwise, commit to the tokens we have consumed.
14905    Returns true if no error occurred; false otherwise.  */
14906
14907 static bool
14908 cp_parser_parse_definitely (parser)
14909      cp_parser *parser;
14910 {
14911   bool error_occurred;
14912   cp_parser_context *context;
14913
14914   /* Remember whether or not an error ocurred, since we are about to
14915      destroy that information.  */
14916   error_occurred = cp_parser_error_occurred (parser);
14917   /* Remove the topmost context from the stack.  */
14918   context = parser->context;
14919   parser->context = context->next;
14920   /* If no parse errors occurred, commit to the tentative parse.  */
14921   if (!error_occurred)
14922     {
14923       /* Commit to the tokens read tentatively, unless that was
14924          already done.  */
14925       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14926         cp_lexer_commit_tokens (parser->lexer);
14927       if (!parser->context->deferring_access_checks_p)
14928         /* If in the parent context we are not deferring checks, then
14929            these perform these checks now.  */
14930         (cp_parser_perform_deferred_access_checks 
14931          (context->deferred_access_checks));
14932       else
14933         /* Any lookups that were deferred during the tentative parse are
14934            still deferred.  */
14935         parser->context->deferred_access_checks 
14936           = chainon (parser->context->deferred_access_checks,
14937                      context->deferred_access_checks);
14938     }
14939   /* Otherwise, if errors occurred, roll back our state so that things
14940      are just as they were before we began the tentative parse.  */
14941   else
14942     cp_lexer_rollback_tokens (parser->lexer);
14943   /* Add the context to the front of the free list.  */
14944   context->next = cp_parser_context_free_list;
14945   cp_parser_context_free_list = context;
14946
14947   return !error_occurred;
14948 }
14949
14950 /* Returns true if we are parsing tentatively -- but have decided that
14951    we will stick with this tentative parse, even if errors occur.  */
14952
14953 static bool
14954 cp_parser_committed_to_tentative_parse (parser)
14955      cp_parser *parser;
14956 {
14957   return (cp_parser_parsing_tentatively (parser)
14958           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14959 }
14960
14961 /* Returns non-zero iff an error has occurred during the most recent
14962    tentative parse.  */
14963    
14964 static bool
14965 cp_parser_error_occurred (parser)
14966      cp_parser *parser;
14967 {
14968   return (cp_parser_parsing_tentatively (parser)
14969           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14970 }
14971
14972 /* Returns non-zero if GNU extensions are allowed.  */
14973
14974 static bool
14975 cp_parser_allow_gnu_extensions_p (parser)
14976      cp_parser *parser;
14977 {
14978   return parser->allow_gnu_extensions_p;
14979 }
14980
14981 \f
14982
14983 /* The parser.  */
14984
14985 static GTY (()) cp_parser *the_parser;
14986
14987 /* External interface.  */
14988
14989 /* Parse the entire translation unit.  */
14990
14991 int
14992 yyparse ()
14993 {
14994   bool error_occurred;
14995
14996   the_parser = cp_parser_new ();
14997   error_occurred = cp_parser_translation_unit (the_parser);
14998   the_parser = NULL;
14999
15000   return error_occurred;
15001 }
15002
15003 /* Clean up after parsing the entire translation unit.  */
15004
15005 void
15006 free_parser_stacks ()
15007 {
15008   /* Nothing to do.  */
15009 }
15010
15011 /* This variable must be provided by every front end.  */
15012
15013 int yydebug;
15014
15015 #include "gt-cp-parser.h"