OSDN Git Service

* parser.c (cp_parser_late_parsing_for_member): Don't cast to void.
[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 "toplev.h"
36 #include "output.h"
37
38 \f
39 /* The lexer.  */
40
41 /* Overview
42    --------
43
44    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
45    look-ahead.
46
47    Methodology
48    -----------
49
50    We use a circular buffer to store incoming tokens.
51
52    Some artifacts of the C++ language (such as the
53    expression/declaration ambiguity) require arbitrary look-ahead.
54    The strategy we adopt for dealing with these problems is to attempt
55    to parse one construct (e.g., the declaration) and fall back to the
56    other (e.g., the expression) if that attempt does not succeed.
57    Therefore, we must sometimes store an arbitrary number of tokens.
58
59    The parser routinely peeks at the next token, and then consumes it
60    later.  That also requires a buffer in which to store the tokens.
61      
62    In order to easily permit adding tokens to the end of the buffer,
63    while removing them from the beginning of the buffer, we use a
64    circular buffer.  */
65
66 /* A C++ token.  */
67
68 typedef struct cp_token GTY (())
69 {
70   /* The kind of token.  */
71   enum cpp_ttype type;
72   /* The value associated with this token, if any.  */
73   tree value;
74   /* If this token is a keyword, this value indicates which keyword.
75      Otherwise, this value is RID_MAX.  */
76   enum rid keyword;
77   /* The file in which this token was found.  */
78   const char *file_name;
79   /* The line at which this token was found.  */
80   int line_number;
81 } cp_token;
82
83 /* The number of tokens in a single token block.  */
84
85 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
86
87 /* A group of tokens.  These groups are chained together to store
88    large numbers of tokens.  (For example, a token block is created
89    when the body of an inline member function is first encountered;
90    the tokens are processed later after the class definition is
91    complete.)  
92
93    This somewhat ungainly data structure (as opposed to, say, a
94    variable-length array), is used due to contraints imposed by the
95    current garbage-collection methodology.  If it is made more
96    flexible, we could perhaps simplify the data structures involved.  */
97
98 typedef struct cp_token_block GTY (())
99 {
100   /* The tokens.  */
101   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
102   /* The number of tokens in this block.  */
103   size_t num_tokens;
104   /* The next token block in the chain.  */
105   struct cp_token_block *next;
106   /* The previous block in the chain.  */
107   struct cp_token_block *prev;
108 } cp_token_block;
109
110 typedef struct cp_token_cache GTY (())
111 {
112   /* The first block in the cache.  NULL if there are no tokens in the
113      cache.  */
114   cp_token_block *first;
115   /* The last block in the cache.  NULL If there are no tokens in the
116      cache.  */
117   cp_token_block *last;
118 } cp_token_cache;
119
120 /* Prototypes. */
121
122 static cp_token_cache *cp_token_cache_new 
123   (void);
124 static void cp_token_cache_push_token
125   (cp_token_cache *, cp_token *);
126
127 /* Create a new cp_token_cache.  */
128
129 static cp_token_cache *
130 cp_token_cache_new ()
131 {
132   return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
133 }
134
135 /* Add *TOKEN to *CACHE.  */
136
137 static void
138 cp_token_cache_push_token (cp_token_cache *cache,
139                            cp_token *token)
140 {
141   cp_token_block *b = cache->last;
142
143   /* See if we need to allocate a new token block.  */
144   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145     {
146       b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
147       b->prev = cache->last;
148       if (cache->last)
149         {
150           cache->last->next = b;
151           cache->last = b;
152         }
153       else
154         cache->first = cache->last = b;
155     }
156   /* Add this token to the current token block.  */
157   b->tokens[b->num_tokens++] = *token;
158 }
159
160 /* The cp_lexer structure represents the C++ lexer.  It is responsible
161    for managing the token stream from the preprocessor and supplying
162    it to the parser.  */
163
164 typedef struct cp_lexer GTY (())
165 {
166   /* The memory allocated for the buffer.  Never NULL.  */
167   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
168   /* A pointer just past the end of the memory allocated for the buffer.  */
169   cp_token * GTY ((skip (""))) buffer_end;
170   /* The first valid token in the buffer, or NULL if none.  */
171   cp_token * GTY ((skip (""))) first_token;
172   /* The next available token.  If NEXT_TOKEN is NULL, then there are
173      no more available tokens.  */
174   cp_token * GTY ((skip (""))) next_token;
175   /* A pointer just past the last available token.  If FIRST_TOKEN is
176      NULL, however, there are no available tokens, and then this
177      location is simply the place in which the next token read will be
178      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
179      When the LAST_TOKEN == BUFFER, then the last token is at the
180      highest memory address in the BUFFER.  */
181   cp_token * GTY ((skip (""))) last_token;
182
183   /* A stack indicating positions at which cp_lexer_save_tokens was
184      called.  The top entry is the most recent position at which we
185      began saving tokens.  The entries are differences in token
186      position between FIRST_TOKEN and the first saved token.
187
188      If the stack is non-empty, we are saving tokens.  When a token is
189      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
190      pointer will not.  The token stream will be preserved so that it
191      can be reexamined later.
192
193      If the stack is empty, then we are not saving tokens.  Whenever a
194      token is consumed, the FIRST_TOKEN pointer will be moved, and the
195      consumed token will be gone forever.  */
196   varray_type saved_tokens;
197
198   /* The STRING_CST tokens encountered while processing the current
199      string literal.  */
200   varray_type string_tokens;
201
202   /* True if we should obtain more tokens from the preprocessor; false
203      if we are processing a saved token cache.  */
204   bool main_lexer_p;
205
206   /* True if we should output debugging information.  */
207   bool debugging_p;
208
209   /* The next lexer in a linked list of lexers.  */
210   struct cp_lexer *next;
211 } cp_lexer;
212
213 /* Prototypes.  */
214
215 static cp_lexer *cp_lexer_new_main
216   PARAMS ((void));
217 static cp_lexer *cp_lexer_new_from_tokens
218   PARAMS ((struct cp_token_cache *));
219 static int cp_lexer_saving_tokens
220   PARAMS ((const cp_lexer *));
221 static cp_token *cp_lexer_next_token
222   PARAMS ((cp_lexer *, cp_token *));
223 static ptrdiff_t cp_lexer_token_difference
224   PARAMS ((cp_lexer *, cp_token *, cp_token *));
225 static cp_token *cp_lexer_read_token
226   PARAMS ((cp_lexer *));
227 static void cp_lexer_maybe_grow_buffer
228   PARAMS ((cp_lexer *));
229 static void cp_lexer_get_preprocessor_token
230   PARAMS ((cp_lexer *, cp_token *));
231 static cp_token *cp_lexer_peek_token
232   PARAMS ((cp_lexer *));
233 static cp_token *cp_lexer_peek_nth_token
234   PARAMS ((cp_lexer *, size_t));
235 static inline bool cp_lexer_next_token_is
236   PARAMS ((cp_lexer *, enum cpp_ttype));
237 static bool cp_lexer_next_token_is_not
238   PARAMS ((cp_lexer *, enum cpp_ttype));
239 static bool cp_lexer_next_token_is_keyword
240   PARAMS ((cp_lexer *, enum rid));
241 static cp_token *cp_lexer_consume_token
242   PARAMS ((cp_lexer *));
243 static void cp_lexer_purge_token
244   (cp_lexer *);
245 static void cp_lexer_purge_tokens_after
246   (cp_lexer *, cp_token *);
247 static void cp_lexer_save_tokens
248   PARAMS ((cp_lexer *));
249 static void cp_lexer_commit_tokens
250   PARAMS ((cp_lexer *));
251 static void cp_lexer_rollback_tokens
252   PARAMS ((cp_lexer *));
253 static inline void cp_lexer_set_source_position_from_token 
254   PARAMS ((cp_lexer *, const cp_token *));
255 static void cp_lexer_print_token
256   PARAMS ((FILE *, cp_token *));
257 static inline bool cp_lexer_debugging_p 
258   PARAMS ((cp_lexer *));
259 static void cp_lexer_start_debugging
260   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
261 static void cp_lexer_stop_debugging
262   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
263
264 /* Manifest constants.  */
265
266 #define CP_TOKEN_BUFFER_SIZE 5
267 #define CP_SAVED_TOKENS_SIZE 5
268
269 /* A token type for keywords, as opposed to ordinary identifiers.  */
270 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
271
272 /* A token type for template-ids.  If a template-id is processed while
273    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
274    the value of the CPP_TEMPLATE_ID is whatever was returned by
275    cp_parser_template_id.  */
276 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
277
278 /* A token type for nested-name-specifiers.  If a
279    nested-name-specifier is processed while parsing tentatively, it is
280    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
281    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
282    cp_parser_nested_name_specifier_opt.  */
283 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
284
285 /* A token type for tokens that are not tokens at all; these are used
286    to mark the end of a token block.  */
287 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
288
289 /* Variables.  */
290
291 /* The stream to which debugging output should be written.  */
292 static FILE *cp_lexer_debug_stream;
293
294 /* Create a new main C++ lexer, the lexer that gets tokens from the
295    preprocessor.  */
296
297 static cp_lexer *
298 cp_lexer_new_main (void)
299 {
300   cp_lexer *lexer;
301   cp_token first_token;
302
303   /* It's possible that lexing the first token will load a PCH file,
304      which is a GC collection point.  So we have to grab the first
305      token before allocating any memory.  */
306   cp_lexer_get_preprocessor_token (NULL, &first_token);
307   cpp_get_callbacks (parse_in)->valid_pch = NULL;
308
309   /* Allocate the memory.  */
310   lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
311
312   /* Create the circular buffer.  */
313   lexer->buffer = ((cp_token *) 
314                    ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)));
315   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
317   /* There is one token in the buffer.  */
318   lexer->last_token = lexer->buffer + 1;
319   lexer->first_token = lexer->buffer;
320   lexer->next_token = lexer->buffer;
321   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
322
323   /* This lexer obtains more tokens by calling c_lex.  */
324   lexer->main_lexer_p = true;
325
326   /* Create the SAVED_TOKENS stack.  */
327   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328   
329   /* Create the STRINGS array.  */
330   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332   /* Assume we are not debugging.  */
333   lexer->debugging_p = false;
334
335   return lexer;
336 }
337
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339    When these tokens are exhausted, no new tokens will be read.  */
340
341 static cp_lexer *
342 cp_lexer_new_from_tokens (cp_token_cache *tokens)
343 {
344   cp_lexer *lexer;
345   cp_token *token;
346   cp_token_block *block;
347   ptrdiff_t num_tokens;
348
349   /* Allocate the memory.  */
350   lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
351
352   /* Create a new buffer, appropriately sized.  */
353   num_tokens = 0;
354   for (block = tokens->first; block != NULL; block = block->next)
355     num_tokens += block->num_tokens;
356   lexer->buffer = ((cp_token *) ggc_alloc (num_tokens * sizeof (cp_token)));
357   lexer->buffer_end = lexer->buffer + num_tokens;
358   
359   /* Install the tokens.  */
360   token = lexer->buffer;
361   for (block = tokens->first; block != NULL; block = block->next)
362     {
363       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364       token += block->num_tokens;
365     }
366
367   /* The FIRST_TOKEN is the beginning of the buffer.  */
368   lexer->first_token = lexer->buffer;
369   /* The next available token is also at the beginning of the buffer.  */
370   lexer->next_token = lexer->buffer;
371   /* The buffer is full.  */
372   lexer->last_token = lexer->first_token;
373
374   /* This lexer doesn't obtain more tokens.  */
375   lexer->main_lexer_p = false;
376
377   /* Create the SAVED_TOKENS stack.  */
378   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379   
380   /* Create the STRINGS array.  */
381   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383   /* Assume we are not debugging.  */
384   lexer->debugging_p = false;
385
386   return lexer;
387 }
388
389 /* Returns non-zero if debugging information should be output.  */
390
391 static inline bool
392 cp_lexer_debugging_p (cp_lexer *lexer)
393 {
394   return lexer->debugging_p;
395 }
396
397 /* Set the current source position from the information stored in
398    TOKEN.  */
399
400 static inline void
401 cp_lexer_set_source_position_from_token (lexer, token)
402      cp_lexer *lexer ATTRIBUTE_UNUSED;
403      const cp_token *token;
404 {
405   /* Ideally, the source position information would not be a global
406      variable, but it is.  */
407
408   /* Update the line number.  */
409   if (token->type != CPP_EOF)
410     {
411       lineno = token->line_number;
412       input_filename = token->file_name;
413     }
414 }
415
416 /* TOKEN points into the circular token buffer.  Return a pointer to
417    the next token in the buffer.  */
418
419 static inline cp_token *
420 cp_lexer_next_token (lexer, token)
421      cp_lexer *lexer;
422      cp_token *token;
423 {
424   token++;
425   if (token == lexer->buffer_end)
426     token = lexer->buffer;
427   return token;
428 }
429
430 /* Non-zero if we are presently saving tokens.  */
431
432 static int
433 cp_lexer_saving_tokens (lexer)
434      const cp_lexer *lexer;
435 {
436   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
437 }
438
439 /* Return a pointer to the token that is N tokens beyond TOKEN in the
440    buffer.  */
441
442 static cp_token *
443 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
444 {
445   token += n;
446   if (token >= lexer->buffer_end)
447     token = lexer->buffer + (token - lexer->buffer_end);
448   return token;
449 }
450
451 /* Returns the number of times that START would have to be incremented
452    to reach FINISH.  If START and FINISH are the same, returns zero.  */
453
454 static ptrdiff_t
455 cp_lexer_token_difference (lexer, start, finish)
456      cp_lexer *lexer;
457      cp_token *start;
458      cp_token *finish;
459 {
460   if (finish >= start)
461     return finish - start;
462   else
463     return ((lexer->buffer_end - lexer->buffer)
464             - (start - finish));
465 }
466
467 /* Obtain another token from the C preprocessor and add it to the
468    token buffer.  Returns the newly read token.  */
469
470 static cp_token *
471 cp_lexer_read_token (lexer)
472      cp_lexer *lexer;
473 {
474   cp_token *token;
475
476   /* Make sure there is room in the buffer.  */
477   cp_lexer_maybe_grow_buffer (lexer);
478
479   /* If there weren't any tokens, then this one will be the first.  */
480   if (!lexer->first_token)
481     lexer->first_token = lexer->last_token;
482   /* Similarly, if there were no available tokens, there is one now.  */
483   if (!lexer->next_token)
484     lexer->next_token = lexer->last_token;
485
486   /* Figure out where we're going to store the new token.  */
487   token = lexer->last_token;
488
489   /* Get a new token from the preprocessor.  */
490   cp_lexer_get_preprocessor_token (lexer, token);
491
492   /* Increment LAST_TOKEN.  */
493   lexer->last_token = cp_lexer_next_token (lexer, token);
494
495   /* The preprocessor does not yet do translation phase six, i.e., the
496      combination of adjacent string literals.  Therefore, we do it
497      here.  */
498   if (token->type == CPP_STRING || token->type == CPP_WSTRING)
499     {
500       ptrdiff_t delta;
501       int i;
502
503       /* When we grow the buffer, we may invalidate TOKEN.  So, save
504          the distance from the beginning of the BUFFER so that we can
505          recaulate it.  */
506       delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
507       /* Make sure there is room in the buffer for another token.  */
508       cp_lexer_maybe_grow_buffer (lexer);
509       /* Restore TOKEN.  */
510       token = lexer->buffer;
511       for (i = 0; i < delta; ++i)
512         token = cp_lexer_next_token (lexer, token);
513
514       VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
515       while (true)
516         {
517           /* Read the token after TOKEN.  */
518           cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
519           /* See whether it's another string constant.  */
520           if (lexer->last_token->type != token->type)
521             {
522               /* If not, then it will be the next real token.  */
523               lexer->last_token = cp_lexer_next_token (lexer, 
524                                                        lexer->last_token);
525               break;
526             }
527
528           /* Chain the strings together.  */
529           VARRAY_PUSH_TREE (lexer->string_tokens, 
530                             lexer->last_token->value);
531         }
532
533       /* Create a single STRING_CST.  Curiously we have to call
534          combine_strings even if there is only a single string in
535          order to get the type set correctly.  */
536       token->value = combine_strings (lexer->string_tokens);
537       VARRAY_CLEAR (lexer->string_tokens);
538       token->value = fix_string_type (token->value);
539       /* Strings should have type `const char []'.  Right now, we will
540          have an ARRAY_TYPE that is constant rather than an array of
541          constant elements.  */
542       if (flag_const_strings)
543         {
544           tree type;
545
546           /* Get the current type.  It will be an ARRAY_TYPE.  */
547           type = TREE_TYPE (token->value);
548           /* Use build_cplus_array_type to rebuild the array, thereby
549              getting the right type.  */
550           type = build_cplus_array_type (TREE_TYPE (type),
551                                          TYPE_DOMAIN (type));
552           /* Reset the type of the token.  */
553           TREE_TYPE (token->value) = type;
554         }
555     }
556
557   return token;
558 }
559
560 /* If the circular buffer is full, make it bigger.  */
561
562 static void
563 cp_lexer_maybe_grow_buffer (lexer)
564      cp_lexer *lexer;
565 {
566   /* If the buffer is full, enlarge it.  */
567   if (lexer->last_token == lexer->first_token)
568     {
569       cp_token *new_buffer;
570       cp_token *old_buffer;
571       cp_token *new_first_token;
572       ptrdiff_t buffer_length;
573       size_t num_tokens_to_copy;
574
575       /* Remember the current buffer pointer.  It will become invalid,
576          but we will need to do pointer arithmetic involving this
577          value.  */
578       old_buffer = lexer->buffer;
579       /* Compute the current buffer size.  */
580       buffer_length = lexer->buffer_end - lexer->buffer;
581       /* Allocate a buffer twice as big.  */
582       new_buffer = ((cp_token *)
583                     ggc_realloc (lexer->buffer, 
584                                  2 * buffer_length * sizeof (cp_token)));
585       
586       /* Because the buffer is circular, logically consecutive tokens
587          are not necessarily placed consecutively in memory.
588          Therefore, we must keep move the tokens that were before
589          FIRST_TOKEN to the second half of the newly allocated
590          buffer.  */
591       num_tokens_to_copy = (lexer->first_token - old_buffer);
592       memcpy (new_buffer + buffer_length,
593               new_buffer,
594               num_tokens_to_copy * sizeof (cp_token));
595       /* Clear the rest of the buffer.  We never look at this storage,
596          but the garbage collector may.  */
597       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
598               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
599
600       /* Now recompute all of the buffer pointers.  */
601       new_first_token 
602         = new_buffer + (lexer->first_token - old_buffer);
603       if (lexer->next_token != NULL)
604         {
605           ptrdiff_t next_token_delta;
606
607           if (lexer->next_token > lexer->first_token)
608             next_token_delta = lexer->next_token - lexer->first_token;
609           else
610             next_token_delta = 
611               buffer_length - (lexer->first_token - lexer->next_token);
612           lexer->next_token = new_first_token + next_token_delta;
613         }
614       lexer->last_token = new_first_token + buffer_length;
615       lexer->buffer = new_buffer;
616       lexer->buffer_end = new_buffer + buffer_length * 2;
617       lexer->first_token = new_first_token;
618     }
619 }
620
621 /* Store the next token from the preprocessor in *TOKEN.  */
622
623 static void 
624 cp_lexer_get_preprocessor_token (lexer, token)
625      cp_lexer *lexer ATTRIBUTE_UNUSED;
626      cp_token *token;
627 {
628   bool done;
629
630   /* If this not the main lexer, return a terminating CPP_EOF token.  */
631   if (lexer != NULL && !lexer->main_lexer_p)
632     {
633       token->type = CPP_EOF;
634       token->line_number = 0;
635       token->file_name = NULL;
636       token->value = NULL_TREE;
637       token->keyword = RID_MAX;
638
639       return;
640     }
641
642   done = false;
643   /* Keep going until we get a token we like.  */
644   while (!done)
645     {
646       /* Get a new token from the preprocessor.  */
647       token->type = c_lex (&token->value);
648       /* Issue messages about tokens we cannot process.  */
649       switch (token->type)
650         {
651         case CPP_ATSIGN:
652         case CPP_HASH:
653         case CPP_PASTE:
654           error ("invalid token");
655           break;
656
657         case CPP_OTHER:
658           /* These tokens are already warned about by c_lex.  */
659           break;
660
661         default:
662           /* This is a good token, so we exit the loop.  */
663           done = true;
664           break;
665         }
666     }
667   /* Now we've got our token.  */
668   token->line_number = lineno;
669   token->file_name = input_filename;
670
671   /* Check to see if this token is a keyword.  */
672   if (token->type == CPP_NAME 
673       && C_IS_RESERVED_WORD (token->value))
674     {
675       /* Mark this token as a keyword.  */
676       token->type = CPP_KEYWORD;
677       /* Record which keyword.  */
678       token->keyword = C_RID_CODE (token->value);
679       /* Update the value.  Some keywords are mapped to particular
680          entities, rather than simply having the value of the
681          corresponding IDENTIFIER_NODE.  For example, `__const' is
682          mapped to `const'.  */
683       token->value = ridpointers[token->keyword];
684     }
685   else
686     token->keyword = RID_MAX;
687 }
688
689 /* Return a pointer to the next token in the token stream, but do not
690    consume it.  */
691
692 static cp_token *
693 cp_lexer_peek_token (lexer)
694      cp_lexer *lexer;
695 {
696   cp_token *token;
697
698   /* If there are no tokens, read one now.  */
699   if (!lexer->next_token)
700     cp_lexer_read_token (lexer);
701
702   /* Provide debugging output.  */
703   if (cp_lexer_debugging_p (lexer))
704     {
705       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
706       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
707       fprintf (cp_lexer_debug_stream, "\n");
708     }
709
710   token = lexer->next_token;
711   cp_lexer_set_source_position_from_token (lexer, token);
712   return token;
713 }
714
715 /* Return true if the next token has the indicated TYPE.  */
716
717 static bool
718 cp_lexer_next_token_is (lexer, type)
719      cp_lexer *lexer;
720      enum cpp_ttype type;
721 {
722   cp_token *token;
723
724   /* Peek at the next token.  */
725   token = cp_lexer_peek_token (lexer);
726   /* Check to see if it has the indicated TYPE.  */
727   return token->type == type;
728 }
729
730 /* Return true if the next token does not have the indicated TYPE.  */
731
732 static bool
733 cp_lexer_next_token_is_not (lexer, type)
734      cp_lexer *lexer;
735      enum cpp_ttype type;
736 {
737   return !cp_lexer_next_token_is (lexer, type);
738 }
739
740 /* Return true if the next token is the indicated KEYWORD.  */
741
742 static bool
743 cp_lexer_next_token_is_keyword (lexer, keyword)
744      cp_lexer *lexer;
745      enum rid keyword;
746 {
747   cp_token *token;
748
749   /* Peek at the next token.  */
750   token = cp_lexer_peek_token (lexer);
751   /* Check to see if it is the indicated keyword.  */
752   return token->keyword == keyword;
753 }
754
755 /* Return a pointer to the Nth token in the token stream.  If N is 1,
756    then this is precisely equivalent to cp_lexer_peek_token.  */
757
758 static cp_token *
759 cp_lexer_peek_nth_token (lexer, n)
760      cp_lexer *lexer;
761      size_t n;
762 {
763   cp_token *token;
764
765   /* N is 1-based, not zero-based.  */
766   my_friendly_assert (n > 0, 20000224);
767
768   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
769   token = lexer->next_token;
770   /* If there are no tokens in the buffer, get one now.  */
771   if (!token)
772     {
773       cp_lexer_read_token (lexer);
774       token = lexer->next_token;
775     }
776
777   /* Now, read tokens until we have enough.  */
778   while (--n > 0)
779     {
780       /* Advance to the next token.  */
781       token = cp_lexer_next_token (lexer, token);
782       /* If that's all the tokens we have, read a new one.  */
783       if (token == lexer->last_token)
784         token = cp_lexer_read_token (lexer);
785     }
786
787   return token;
788 }
789
790 /* Consume the next token.  The pointer returned is valid only until
791    another token is read.  Callers should preserve copy the token
792    explicitly if they will need its value for a longer period of
793    time.  */
794
795 static cp_token *
796 cp_lexer_consume_token (lexer)
797      cp_lexer *lexer;
798 {
799   cp_token *token;
800
801   /* If there are no tokens, read one now.  */
802   if (!lexer->next_token)
803     cp_lexer_read_token (lexer);
804
805   /* Remember the token we'll be returning.  */
806   token = lexer->next_token;
807
808   /* Increment NEXT_TOKEN.  */
809   lexer->next_token = cp_lexer_next_token (lexer, 
810                                            lexer->next_token);
811   /* Check to see if we're all out of tokens.  */
812   if (lexer->next_token == lexer->last_token)
813     lexer->next_token = NULL;
814
815   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
816   if (!cp_lexer_saving_tokens (lexer))
817     {
818       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
819       if (!lexer->next_token)
820         lexer->first_token = NULL;
821       else
822         lexer->first_token = lexer->next_token;
823     }
824
825   /* Provide debugging output.  */
826   if (cp_lexer_debugging_p (lexer))
827     {
828       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
829       cp_lexer_print_token (cp_lexer_debug_stream, token);
830       fprintf (cp_lexer_debug_stream, "\n");
831     }
832
833   return token;
834 }
835
836 /* Permanently remove the next token from the token stream.  There
837    must be a valid next token already; this token never reads
838    additional tokens from the preprocessor.  */
839
840 static void
841 cp_lexer_purge_token (cp_lexer *lexer)
842 {
843   cp_token *token;
844   cp_token *next_token;
845
846   token = lexer->next_token;
847   while (true) 
848     {
849       next_token = cp_lexer_next_token (lexer, token);
850       if (next_token == lexer->last_token)
851         break;
852       *token = *next_token;
853       token = next_token;
854     }
855
856   lexer->last_token = token;
857   /* The token purged may have been the only token remaining; if so,
858      clear NEXT_TOKEN.  */
859   if (lexer->next_token == token)
860     lexer->next_token = NULL;
861 }
862
863 /* Permanently remove all tokens after TOKEN, up to, but not
864    including, the token that will be returned next by
865    cp_lexer_peek_token.  */
866
867 static void
868 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
869 {
870   cp_token *peek;
871   cp_token *t1;
872   cp_token *t2;
873
874   if (lexer->next_token)
875     {
876       /* Copy the tokens that have not yet been read to the location
877          immediately following TOKEN.  */
878       t1 = cp_lexer_next_token (lexer, token);
879       t2 = peek = cp_lexer_peek_token (lexer);
880       /* Move tokens into the vacant area between TOKEN and PEEK.  */
881       while (t2 != lexer->last_token)
882         {
883           *t1 = *t2;
884           t1 = cp_lexer_next_token (lexer, t1);
885           t2 = cp_lexer_next_token (lexer, t2);
886         }
887       /* Now, the next available token is right after TOKEN.  */
888       lexer->next_token = cp_lexer_next_token (lexer, token);
889       /* And the last token is wherever we ended up.  */
890       lexer->last_token = t1;
891     }
892   else
893     {
894       /* There are no tokens in the buffer, so there is nothing to
895          copy.  The last token in the buffer is TOKEN itself.  */
896       lexer->last_token = cp_lexer_next_token (lexer, token);
897     }
898 }
899
900 /* Begin saving tokens.  All tokens consumed after this point will be
901    preserved.  */
902
903 static void
904 cp_lexer_save_tokens (lexer)
905      cp_lexer *lexer;
906 {
907   /* Provide debugging output.  */
908   if (cp_lexer_debugging_p (lexer))
909     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
910
911   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
912      restore the tokens if required.  */
913   if (!lexer->next_token)
914     cp_lexer_read_token (lexer);
915
916   VARRAY_PUSH_INT (lexer->saved_tokens,
917                    cp_lexer_token_difference (lexer,
918                                               lexer->first_token,
919                                               lexer->next_token));
920 }
921
922 /* Commit to the portion of the token stream most recently saved.  */
923
924 static void
925 cp_lexer_commit_tokens (lexer)
926      cp_lexer *lexer;
927 {
928   /* Provide debugging output.  */
929   if (cp_lexer_debugging_p (lexer))
930     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
931
932   VARRAY_POP (lexer->saved_tokens);
933 }
934
935 /* Return all tokens saved since the last call to cp_lexer_save_tokens
936    to the token stream.  Stop saving tokens.  */
937
938 static void
939 cp_lexer_rollback_tokens (lexer)
940      cp_lexer *lexer;
941 {
942   size_t delta;
943
944   /* Provide debugging output.  */
945   if (cp_lexer_debugging_p (lexer))
946     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
947
948   /* Find the token that was the NEXT_TOKEN when we started saving
949      tokens.  */
950   delta = VARRAY_TOP_INT(lexer->saved_tokens);
951   /* Make it the next token again now.  */
952   lexer->next_token = cp_lexer_advance_token (lexer,
953                                               lexer->first_token, 
954                                               delta);
955   /* It might be the case that there wer no tokens when we started
956      saving tokens, but that there are some tokens now.  */
957   if (!lexer->next_token && lexer->first_token)
958     lexer->next_token = lexer->first_token;
959
960   /* Stop saving tokens.  */
961   VARRAY_POP (lexer->saved_tokens);
962 }
963
964 /* Print a representation of the TOKEN on the STREAM.  */
965
966 static void
967 cp_lexer_print_token (stream, token)
968      FILE *stream;
969      cp_token *token;
970 {
971   const char *token_type = NULL;
972
973   /* Figure out what kind of token this is.  */
974   switch (token->type)
975     {
976     case CPP_EQ:
977       token_type = "EQ";
978       break;
979
980     case CPP_COMMA:
981       token_type = "COMMA";
982       break;
983
984     case CPP_OPEN_PAREN:
985       token_type = "OPEN_PAREN";
986       break;
987
988     case CPP_CLOSE_PAREN:
989       token_type = "CLOSE_PAREN";
990       break;
991
992     case CPP_OPEN_BRACE:
993       token_type = "OPEN_BRACE";
994       break;
995
996     case CPP_CLOSE_BRACE:
997       token_type = "CLOSE_BRACE";
998       break;
999
1000     case CPP_SEMICOLON:
1001       token_type = "SEMICOLON";
1002       break;
1003
1004     case CPP_NAME:
1005       token_type = "NAME";
1006       break;
1007
1008     case CPP_EOF:
1009       token_type = "EOF";
1010       break;
1011
1012     case CPP_KEYWORD:
1013       token_type = "keyword";
1014       break;
1015
1016       /* This is not a token that we know how to handle yet.  */
1017     default:
1018       break;
1019     }
1020
1021   /* If we have a name for the token, print it out.  Otherwise, we
1022      simply give the numeric code.  */
1023   if (token_type)
1024     fprintf (stream, "%s", token_type);
1025   else
1026     fprintf (stream, "%d", token->type);
1027   /* And, for an identifier, print the identifier name.  */
1028   if (token->type == CPP_NAME 
1029       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1030          For example, `struct' is mapped to an INTEGER_CST.  */
1031       || (token->type == CPP_KEYWORD 
1032           && TREE_CODE (token->value) == IDENTIFIER_NODE))
1033     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1034 }
1035
1036 /* Start emitting debugging information.  */
1037
1038 static void
1039 cp_lexer_start_debugging (lexer)
1040      cp_lexer *lexer;
1041 {
1042   ++lexer->debugging_p;
1043 }
1044   
1045 /* Stop emitting debugging information.  */
1046
1047 static void
1048 cp_lexer_stop_debugging (lexer)
1049      cp_lexer *lexer;
1050 {
1051   --lexer->debugging_p;
1052 }
1053
1054 \f
1055 /* The parser.  */
1056
1057 /* Overview
1058    --------
1059
1060    A cp_parser parses the token stream as specified by the C++
1061    grammar.  Its job is purely parsing, not semantic analysis.  For
1062    example, the parser breaks the token stream into declarators,
1063    expressions, statements, and other similar syntactic constructs.
1064    It does not check that the types of the expressions on either side
1065    of an assignment-statement are compatible, or that a function is
1066    not declared with a parameter of type `void'.
1067
1068    The parser invokes routines elsewhere in the compiler to perform
1069    semantic analysis and to build up the abstract syntax tree for the
1070    code processed.  
1071
1072    The parser (and the template instantiation code, which is, in a
1073    way, a close relative of parsing) are the only parts of the
1074    compiler that should be calling push_scope and pop_scope, or
1075    related functions.  The parser (and template instantiation code)
1076    keeps track of what scope is presently active; everything else
1077    should simply honor that.  (The code that generates static
1078    initializers may also need to set the scope, in order to check
1079    access control correctly when emitting the initializers.)
1080
1081    Methodology
1082    -----------
1083    
1084    The parser is of the standard recursive-descent variety.  Upcoming
1085    tokens in the token stream are examined in order to determine which
1086    production to use when parsing a non-terminal.  Some C++ constructs
1087    require arbitrary look ahead to disambiguate.  For example, it is
1088    impossible, in the general case, to tell whether a statement is an
1089    expression or declaration without scanning the entire statement.
1090    Therefore, the parser is capable of "parsing tentatively."  When the
1091    parser is not sure what construct comes next, it enters this mode.
1092    Then, while we attempt to parse the construct, the parser queues up
1093    error messages, rather than issuing them immediately, and saves the
1094    tokens it consumes.  If the construct is parsed successfully, the
1095    parser "commits", i.e., it issues any queued error messages and
1096    the tokens that were being preserved are permanently discarded.
1097    If, however, the construct is not parsed successfully, the parser
1098    rolls back its state completely so that it can resume parsing using
1099    a different alternative.
1100
1101    Future Improvements
1102    -------------------
1103    
1104    The performance of the parser could probably be improved
1105    substantially.  Some possible improvements include:
1106
1107      - The expression parser recurses through the various levels of
1108        precedence as specified in the grammar, rather than using an
1109        operator-precedence technique.  Therefore, parsing a simple
1110        identifier requires multiple recursive calls.
1111
1112      - We could often eliminate the need to parse tentatively by
1113        looking ahead a little bit.  In some places, this approach
1114        might not entirely eliminate the need to parse tentatively, but
1115        it might still speed up the average case.  */
1116
1117 /* Flags that are passed to some parsing functions.  These values can
1118    be bitwise-ored together.  */
1119
1120 typedef enum cp_parser_flags
1121 {
1122   /* No flags.  */
1123   CP_PARSER_FLAGS_NONE = 0x0,
1124   /* The construct is optional.  If it is not present, then no error
1125      should be issued.  */
1126   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1127   /* When parsing a type-specifier, do not allow user-defined types.  */
1128   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1129 } cp_parser_flags;
1130
1131 /* The different kinds of ids that we ecounter.  */
1132
1133 typedef enum cp_parser_id_kind
1134 {
1135   /* Not an id at all.  */
1136   CP_PARSER_ID_KIND_NONE,
1137   /* An unqualified-id that is not a template-id.  */
1138   CP_PARSER_ID_KIND_UNQUALIFIED,
1139   /* An unqualified template-id.  */
1140   CP_PARSER_ID_KIND_TEMPLATE_ID,
1141   /* A qualified-id.  */
1142   CP_PARSER_ID_KIND_QUALIFIED
1143 } cp_parser_id_kind;
1144
1145 /* The different kinds of declarators we want to parse.  */
1146
1147 typedef enum cp_parser_declarator_kind
1148 {
1149   /* We want an abstract declartor. */
1150   CP_PARSER_DECLARATOR_ABSTRACT,
1151   /* We want a named declarator.  */
1152   CP_PARSER_DECLARATOR_NAMED,
1153   /* We don't mind.  */
1154   CP_PARSER_DECLARATOR_EITHER
1155 } cp_parser_declarator_kind;
1156
1157 /* A mapping from a token type to a corresponding tree node type.  */
1158
1159 typedef struct cp_parser_token_tree_map_node
1160 {
1161   /* The token type.  */
1162   enum cpp_ttype token_type;
1163   /* The corresponding tree code.  */
1164   enum tree_code tree_type;
1165 } cp_parser_token_tree_map_node;
1166
1167 /* A complete map consists of several ordinary entries, followed by a
1168    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1169
1170 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1171
1172 /* The status of a tentative parse.  */
1173
1174 typedef enum cp_parser_status_kind
1175 {
1176   /* No errors have occurred.  */
1177   CP_PARSER_STATUS_KIND_NO_ERROR,
1178   /* An error has occurred.  */
1179   CP_PARSER_STATUS_KIND_ERROR,
1180   /* We are committed to this tentative parse, whether or not an error
1181      has occurred.  */
1182   CP_PARSER_STATUS_KIND_COMMITTED
1183 } cp_parser_status_kind;
1184
1185 /* Context that is saved and restored when parsing tentatively.  */
1186
1187 typedef struct cp_parser_context GTY (())
1188 {
1189   /* If this is a tentative parsing context, the status of the
1190      tentative parse.  */
1191   enum cp_parser_status_kind status;
1192   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1193      that are looked up in this context must be looked up both in the
1194      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1195      the context of the containing expression.  */
1196   tree object_type;
1197   /* A TREE_LIST representing name-lookups for which we have deferred
1198      checking access controls.  We cannot check the accessibility of
1199      names used in a decl-specifier-seq until we know what is being
1200      declared because code like:
1201
1202        class A { 
1203          class B {};
1204          B* f();
1205        }
1206
1207        A::B* A::f() { return 0; }
1208
1209      is valid, even though `A::B' is not generally accessible.  
1210
1211      The TREE_PURPOSE of each node is the scope used to qualify the
1212      name being looked up; the TREE_VALUE is the DECL to which the
1213      name was resolved.  */
1214   tree deferred_access_checks;
1215   /* TRUE iff we are deferring access checks.  */
1216   bool deferring_access_checks_p;
1217   /* The next parsing context in the stack.  */
1218   struct cp_parser_context *next;
1219 } cp_parser_context;
1220
1221 /* Prototypes.  */
1222
1223 /* Constructors and destructors.  */
1224
1225 static cp_parser_context *cp_parser_context_new
1226   PARAMS ((cp_parser_context *));
1227
1228 /* Class variables.  */
1229
1230 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1231
1232 /* Constructors and destructors.  */
1233
1234 /* Construct a new context.  The context below this one on the stack
1235    is given by NEXT.  */
1236
1237 static cp_parser_context *
1238 cp_parser_context_new (next)
1239      cp_parser_context *next;
1240 {
1241   cp_parser_context *context;
1242
1243   /* Allocate the storage.  */
1244   if (cp_parser_context_free_list != NULL)
1245     {
1246       /* Pull the first entry from the free list.  */
1247       context = cp_parser_context_free_list;
1248       cp_parser_context_free_list = context->next;
1249       memset ((char *)context, 0, sizeof (*context));
1250     }
1251   else
1252     context = ((cp_parser_context *) 
1253                ggc_alloc_cleared (sizeof (cp_parser_context)));
1254   /* No errors have occurred yet in this context.  */
1255   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1256   /* If this is not the bottomost context, copy information that we
1257      need from the previous context.  */
1258   if (next)
1259     {
1260       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1261          expression, then we are parsing one in this context, too.  */
1262       context->object_type = next->object_type;
1263       /* We are deferring access checks here if we were in the NEXT
1264          context.  */
1265       context->deferring_access_checks_p 
1266         = next->deferring_access_checks_p;
1267       /* Thread the stack.  */
1268       context->next = next;
1269     }
1270
1271   return context;
1272 }
1273
1274 /* The cp_parser structure represents the C++ parser.  */
1275
1276 typedef struct cp_parser GTY(())
1277 {
1278   /* The lexer from which we are obtaining tokens.  */
1279   cp_lexer *lexer;
1280
1281   /* The scope in which names should be looked up.  If NULL_TREE, then
1282      we look up names in the scope that is currently open in the
1283      source program.  If non-NULL, this is either a TYPE or
1284      NAMESPACE_DECL for the scope in which we should look.  
1285
1286      This value is not cleared automatically after a name is looked
1287      up, so we must be careful to clear it before starting a new look
1288      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1289      will look up `Z' in the scope of `X', rather than the current
1290      scope.)  Unfortunately, it is difficult to tell when name lookup
1291      is complete, because we sometimes peek at a token, look it up,
1292      and then decide not to consume it.  */
1293   tree scope;
1294
1295   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1296      last lookup took place.  OBJECT_SCOPE is used if an expression
1297      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1298      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1299      form "X::Y"; it refers to X.  */
1300   tree object_scope;
1301   tree qualifying_scope;
1302
1303   /* A stack of parsing contexts.  All but the bottom entry on the
1304      stack will be tentative contexts.
1305
1306      We parse tentatively in order to determine which construct is in
1307      use in some situations.  For example, in order to determine
1308      whether a statement is an expression-statement or a
1309      declaration-statement we parse it tentatively as a
1310      declaration-statement.  If that fails, we then reparse the same
1311      token stream as an expression-statement.  */
1312   cp_parser_context *context;
1313
1314   /* True if we are parsing GNU C++.  If this flag is not set, then
1315      GNU extensions are not recognized.  */
1316   bool allow_gnu_extensions_p;
1317
1318   /* TRUE if the `>' token should be interpreted as the greater-than
1319      operator.  FALSE if it is the end of a template-id or
1320      template-parameter-list.  */
1321   bool greater_than_is_operator_p;
1322
1323   /* TRUE if default arguments are allowed within a parameter list
1324      that starts at this point. FALSE if only a gnu extension makes
1325      them permissable.  */
1326   bool default_arg_ok_p;
1327   
1328   /* TRUE if we are parsing an integral constant-expression.  See
1329      [expr.const] for a precise definition.  */
1330   /* FIXME: Need to implement code that checks this flag.  */
1331   bool constant_expression_p;
1332
1333   /* TRUE if local variable names and `this' are forbidden in the
1334      current context.  */
1335   bool local_variables_forbidden_p;
1336
1337   /* TRUE if the declaration we are parsing is part of a
1338      linkage-specification of the form `extern string-literal
1339      declaration'.  */
1340   bool in_unbraced_linkage_specification_p;
1341
1342   /* TRUE if we are presently parsing a declarator, after the
1343      direct-declarator.  */
1344   bool in_declarator_p;
1345
1346   /* If non-NULL, then we are parsing a construct where new type
1347      definitions are not permitted.  The string stored here will be
1348      issued as an error message if a type is defined.  */
1349   const char *type_definition_forbidden_message;
1350
1351   /* A TREE_LIST of queues of functions whose bodies have been lexed,
1352      but may not have been parsed.  These functions are friends of
1353      members defined within a class-specification; they are not
1354      procssed until the class is complete.  The active queue is at the
1355      front of the list.
1356
1357      Within each queue, functions appear in the reverse order that
1358      they appeared in the source.  Each TREE_VALUE is a
1359      FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1360      function.  */
1361   tree unparsed_functions_queues;
1362
1363   /* The number of classes whose definitions are currently in
1364      progress.  */
1365   unsigned num_classes_being_defined;
1366
1367   /* The number of template parameter lists that apply directly to the
1368      current declaration.  */
1369   unsigned num_template_parameter_lists;
1370
1371   /* List of access checks lists, used to prevent GC collection while
1372      they are in use.  */
1373   tree access_checks_lists;
1374 } cp_parser;
1375
1376 /* The type of a function that parses some kind of expression  */
1377 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1378
1379 /* Prototypes.  */
1380
1381 /* Constructors and destructors.  */
1382
1383 static cp_parser *cp_parser_new
1384   PARAMS ((void));
1385
1386 /* Routines to parse various constructs.  
1387
1388    Those that return `tree' will return the error_mark_node (rather
1389    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1390    Sometimes, they will return an ordinary node if error-recovery was
1391    attempted, even though a parse error occurrred.  So, to check
1392    whether or not a parse error occurred, you should always use
1393    cp_parser_error_occurred.  If the construct is optional (indicated
1394    either by an `_opt' in the name of the function that does the
1395    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1396    the construct is not present.  */
1397
1398 /* Lexical conventions [gram.lex]  */
1399
1400 static tree cp_parser_identifier
1401   PARAMS ((cp_parser *));
1402
1403 /* Basic concepts [gram.basic]  */
1404
1405 static bool cp_parser_translation_unit
1406   PARAMS ((cp_parser *));
1407
1408 /* Expressions [gram.expr]  */
1409
1410 static tree cp_parser_primary_expression
1411   (cp_parser *, cp_parser_id_kind *, tree *);
1412 static tree cp_parser_id_expression
1413   PARAMS ((cp_parser *, bool, bool, bool *));
1414 static tree cp_parser_unqualified_id
1415   PARAMS ((cp_parser *, bool, bool));
1416 static tree cp_parser_nested_name_specifier_opt
1417   (cp_parser *, bool, bool, bool);
1418 static tree cp_parser_nested_name_specifier
1419   (cp_parser *, bool, bool, bool);
1420 static tree cp_parser_class_or_namespace_name
1421   (cp_parser *, bool, bool, bool, bool);
1422 static tree cp_parser_postfix_expression
1423   (cp_parser *, bool);
1424 static tree cp_parser_expression_list
1425   PARAMS ((cp_parser *));
1426 static void cp_parser_pseudo_destructor_name
1427   PARAMS ((cp_parser *, tree *, tree *));
1428 static tree cp_parser_unary_expression
1429   (cp_parser *, bool);
1430 static enum tree_code cp_parser_unary_operator
1431   PARAMS ((cp_token *));
1432 static tree cp_parser_new_expression
1433   PARAMS ((cp_parser *));
1434 static tree cp_parser_new_placement
1435   PARAMS ((cp_parser *));
1436 static tree cp_parser_new_type_id
1437   PARAMS ((cp_parser *));
1438 static tree cp_parser_new_declarator_opt
1439   PARAMS ((cp_parser *));
1440 static tree cp_parser_direct_new_declarator
1441   PARAMS ((cp_parser *));
1442 static tree cp_parser_new_initializer
1443   PARAMS ((cp_parser *));
1444 static tree cp_parser_delete_expression
1445   PARAMS ((cp_parser *));
1446 static tree cp_parser_cast_expression 
1447   (cp_parser *, bool);
1448 static tree cp_parser_pm_expression
1449   PARAMS ((cp_parser *));
1450 static tree cp_parser_multiplicative_expression
1451   PARAMS ((cp_parser *));
1452 static tree cp_parser_additive_expression
1453   PARAMS ((cp_parser *));
1454 static tree cp_parser_shift_expression
1455   PARAMS ((cp_parser *));
1456 static tree cp_parser_relational_expression
1457   PARAMS ((cp_parser *));
1458 static tree cp_parser_equality_expression
1459   PARAMS ((cp_parser *));
1460 static tree cp_parser_and_expression
1461   PARAMS ((cp_parser *));
1462 static tree cp_parser_exclusive_or_expression
1463   PARAMS ((cp_parser *));
1464 static tree cp_parser_inclusive_or_expression
1465   PARAMS ((cp_parser *));
1466 static tree cp_parser_logical_and_expression
1467   PARAMS ((cp_parser *));
1468 static tree cp_parser_logical_or_expression 
1469   PARAMS ((cp_parser *));
1470 static tree cp_parser_conditional_expression
1471   PARAMS ((cp_parser *));
1472 static tree cp_parser_question_colon_clause
1473   PARAMS ((cp_parser *, tree));
1474 static tree cp_parser_assignment_expression
1475   PARAMS ((cp_parser *));
1476 static enum tree_code cp_parser_assignment_operator_opt
1477   PARAMS ((cp_parser *));
1478 static tree cp_parser_expression
1479   PARAMS ((cp_parser *));
1480 static tree cp_parser_constant_expression
1481   PARAMS ((cp_parser *));
1482
1483 /* Statements [gram.stmt.stmt]  */
1484
1485 static void cp_parser_statement
1486   PARAMS ((cp_parser *));
1487 static tree cp_parser_labeled_statement
1488   PARAMS ((cp_parser *));
1489 static tree cp_parser_expression_statement
1490   PARAMS ((cp_parser *));
1491 static tree cp_parser_compound_statement
1492   (cp_parser *);
1493 static void cp_parser_statement_seq_opt
1494   PARAMS ((cp_parser *));
1495 static tree cp_parser_selection_statement
1496   PARAMS ((cp_parser *));
1497 static tree cp_parser_condition
1498   PARAMS ((cp_parser *));
1499 static tree cp_parser_iteration_statement
1500   PARAMS ((cp_parser *));
1501 static void cp_parser_for_init_statement
1502   PARAMS ((cp_parser *));
1503 static tree cp_parser_jump_statement
1504   PARAMS ((cp_parser *));
1505 static void cp_parser_declaration_statement
1506   PARAMS ((cp_parser *));
1507
1508 static tree cp_parser_implicitly_scoped_statement
1509   PARAMS ((cp_parser *));
1510 static void cp_parser_already_scoped_statement
1511   PARAMS ((cp_parser *));
1512
1513 /* Declarations [gram.dcl.dcl] */
1514
1515 static void cp_parser_declaration_seq_opt
1516   PARAMS ((cp_parser *));
1517 static void cp_parser_declaration
1518   PARAMS ((cp_parser *));
1519 static void cp_parser_block_declaration
1520   PARAMS ((cp_parser *, bool));
1521 static void cp_parser_simple_declaration
1522   PARAMS ((cp_parser *, bool));
1523 static tree cp_parser_decl_specifier_seq 
1524   PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1525 static tree cp_parser_storage_class_specifier_opt
1526   PARAMS ((cp_parser *));
1527 static tree cp_parser_function_specifier_opt
1528   PARAMS ((cp_parser *));
1529 static tree cp_parser_type_specifier
1530  (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1531 static tree cp_parser_simple_type_specifier
1532   PARAMS ((cp_parser *, cp_parser_flags));
1533 static tree cp_parser_type_name
1534   PARAMS ((cp_parser *));
1535 static tree cp_parser_elaborated_type_specifier
1536   PARAMS ((cp_parser *, bool, bool));
1537 static tree cp_parser_enum_specifier
1538   PARAMS ((cp_parser *));
1539 static void cp_parser_enumerator_list
1540   PARAMS ((cp_parser *, tree));
1541 static void cp_parser_enumerator_definition 
1542   PARAMS ((cp_parser *, tree));
1543 static tree cp_parser_namespace_name
1544   PARAMS ((cp_parser *));
1545 static void cp_parser_namespace_definition
1546   PARAMS ((cp_parser *));
1547 static void cp_parser_namespace_body
1548   PARAMS ((cp_parser *));
1549 static tree cp_parser_qualified_namespace_specifier
1550   PARAMS ((cp_parser *));
1551 static void cp_parser_namespace_alias_definition
1552   PARAMS ((cp_parser *));
1553 static void cp_parser_using_declaration
1554   PARAMS ((cp_parser *));
1555 static void cp_parser_using_directive
1556   PARAMS ((cp_parser *));
1557 static void cp_parser_asm_definition
1558   PARAMS ((cp_parser *));
1559 static void cp_parser_linkage_specification
1560   PARAMS ((cp_parser *));
1561
1562 /* Declarators [gram.dcl.decl] */
1563
1564 static tree cp_parser_init_declarator
1565   PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1566 static tree cp_parser_declarator
1567   PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1568 static tree cp_parser_direct_declarator
1569   PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1570 static enum tree_code cp_parser_ptr_operator
1571   PARAMS ((cp_parser *, tree *, tree *));
1572 static tree cp_parser_cv_qualifier_seq_opt
1573   PARAMS ((cp_parser *));
1574 static tree cp_parser_cv_qualifier_opt
1575   PARAMS ((cp_parser *));
1576 static tree cp_parser_declarator_id
1577   PARAMS ((cp_parser *));
1578 static tree cp_parser_type_id
1579   PARAMS ((cp_parser *));
1580 static tree cp_parser_type_specifier_seq
1581   PARAMS ((cp_parser *));
1582 static tree cp_parser_parameter_declaration_clause
1583   PARAMS ((cp_parser *));
1584 static tree cp_parser_parameter_declaration_list
1585   PARAMS ((cp_parser *));
1586 static tree cp_parser_parameter_declaration
1587   PARAMS ((cp_parser *, bool));
1588 static tree cp_parser_function_definition
1589   PARAMS ((cp_parser *, bool *));
1590 static void cp_parser_function_body
1591   (cp_parser *);
1592 static tree cp_parser_initializer
1593   PARAMS ((cp_parser *, bool *));
1594 static tree cp_parser_initializer_clause
1595   PARAMS ((cp_parser *));
1596 static tree cp_parser_initializer_list
1597   PARAMS ((cp_parser *));
1598
1599 static bool cp_parser_ctor_initializer_opt_and_function_body
1600   (cp_parser *);
1601
1602 /* Classes [gram.class] */
1603
1604 static tree cp_parser_class_name
1605   (cp_parser *, bool, bool, bool, bool, bool, bool);
1606 static tree cp_parser_class_specifier
1607   PARAMS ((cp_parser *));
1608 static tree cp_parser_class_head
1609   PARAMS ((cp_parser *, bool *, bool *, tree *));
1610 static enum tag_types cp_parser_class_key
1611   PARAMS ((cp_parser *));
1612 static void cp_parser_member_specification_opt
1613   PARAMS ((cp_parser *));
1614 static void cp_parser_member_declaration
1615   PARAMS ((cp_parser *));
1616 static tree cp_parser_pure_specifier
1617   PARAMS ((cp_parser *));
1618 static tree cp_parser_constant_initializer
1619   PARAMS ((cp_parser *));
1620
1621 /* Derived classes [gram.class.derived] */
1622
1623 static tree cp_parser_base_clause
1624   PARAMS ((cp_parser *));
1625 static tree cp_parser_base_specifier
1626   PARAMS ((cp_parser *));
1627
1628 /* Special member functions [gram.special] */
1629
1630 static tree cp_parser_conversion_function_id
1631   PARAMS ((cp_parser *));
1632 static tree cp_parser_conversion_type_id
1633   PARAMS ((cp_parser *));
1634 static tree cp_parser_conversion_declarator_opt
1635   PARAMS ((cp_parser *));
1636 static bool cp_parser_ctor_initializer_opt
1637   PARAMS ((cp_parser *));
1638 static void cp_parser_mem_initializer_list
1639   PARAMS ((cp_parser *));
1640 static tree cp_parser_mem_initializer
1641   PARAMS ((cp_parser *));
1642 static tree cp_parser_mem_initializer_id
1643   PARAMS ((cp_parser *));
1644
1645 /* Overloading [gram.over] */
1646
1647 static tree cp_parser_operator_function_id
1648   PARAMS ((cp_parser *));
1649 static tree cp_parser_operator
1650   PARAMS ((cp_parser *));
1651
1652 /* Templates [gram.temp] */
1653
1654 static void cp_parser_template_declaration
1655   PARAMS ((cp_parser *, bool));
1656 static tree cp_parser_template_parameter_list
1657   PARAMS ((cp_parser *));
1658 static tree cp_parser_template_parameter
1659   PARAMS ((cp_parser *));
1660 static tree cp_parser_type_parameter
1661   PARAMS ((cp_parser *));
1662 static tree cp_parser_template_id
1663   PARAMS ((cp_parser *, bool, bool));
1664 static tree cp_parser_template_name
1665   PARAMS ((cp_parser *, bool, bool));
1666 static tree cp_parser_template_argument_list
1667   PARAMS ((cp_parser *));
1668 static tree cp_parser_template_argument
1669   PARAMS ((cp_parser *));
1670 static void cp_parser_explicit_instantiation
1671   PARAMS ((cp_parser *));
1672 static void cp_parser_explicit_specialization
1673   PARAMS ((cp_parser *));
1674
1675 /* Exception handling [gram.exception] */
1676
1677 static tree cp_parser_try_block 
1678   PARAMS ((cp_parser *));
1679 static bool cp_parser_function_try_block
1680   PARAMS ((cp_parser *));
1681 static void cp_parser_handler_seq
1682   PARAMS ((cp_parser *));
1683 static void cp_parser_handler
1684   PARAMS ((cp_parser *));
1685 static tree cp_parser_exception_declaration
1686   PARAMS ((cp_parser *));
1687 static tree cp_parser_throw_expression
1688   PARAMS ((cp_parser *));
1689 static tree cp_parser_exception_specification_opt
1690   PARAMS ((cp_parser *));
1691 static tree cp_parser_type_id_list
1692   PARAMS ((cp_parser *));
1693
1694 /* GNU Extensions */
1695
1696 static tree cp_parser_asm_specification_opt
1697   PARAMS ((cp_parser *));
1698 static tree cp_parser_asm_operand_list
1699   PARAMS ((cp_parser *));
1700 static tree cp_parser_asm_clobber_list
1701   PARAMS ((cp_parser *));
1702 static tree cp_parser_attributes_opt
1703   PARAMS ((cp_parser *));
1704 static tree cp_parser_attribute_list
1705   PARAMS ((cp_parser *));
1706 static bool cp_parser_extension_opt
1707   PARAMS ((cp_parser *, int *));
1708 static void cp_parser_label_declaration
1709   PARAMS ((cp_parser *));
1710
1711 /* Utility Routines */
1712
1713 static tree cp_parser_lookup_name
1714   PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1715 static tree cp_parser_lookup_name_simple
1716   PARAMS ((cp_parser *, tree));
1717 static tree cp_parser_resolve_typename_type
1718   PARAMS ((cp_parser *, tree));
1719 static tree cp_parser_maybe_treat_template_as_class
1720   (tree, bool);
1721 static bool cp_parser_check_declarator_template_parameters
1722   PARAMS ((cp_parser *, tree));
1723 static bool cp_parser_check_template_parameters
1724   PARAMS ((cp_parser *, unsigned));
1725 static tree cp_parser_binary_expression
1726   PARAMS ((cp_parser *, 
1727            const cp_parser_token_tree_map,
1728            cp_parser_expression_fn));
1729 static tree cp_parser_global_scope_opt
1730   PARAMS ((cp_parser *, bool));
1731 static bool cp_parser_constructor_declarator_p
1732   (cp_parser *, bool);
1733 static tree cp_parser_function_definition_from_specifiers_and_declarator
1734   PARAMS ((cp_parser *, tree, tree, tree, tree));
1735 static tree cp_parser_function_definition_after_declarator
1736   PARAMS ((cp_parser *, bool));
1737 static void cp_parser_template_declaration_after_export
1738   PARAMS ((cp_parser *, bool));
1739 static tree cp_parser_single_declaration
1740   PARAMS ((cp_parser *, bool, bool *));
1741 static tree cp_parser_functional_cast
1742   PARAMS ((cp_parser *, tree));
1743 static void cp_parser_late_parsing_for_member
1744   PARAMS ((cp_parser *, tree));
1745 static void cp_parser_late_parsing_default_args
1746   (cp_parser *, tree);
1747 static tree cp_parser_sizeof_operand
1748   PARAMS ((cp_parser *, enum rid));
1749 static bool cp_parser_declares_only_class_p
1750   PARAMS ((cp_parser *));
1751 static bool cp_parser_friend_p
1752   PARAMS ((tree));
1753 static cp_token *cp_parser_require
1754   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1755 static cp_token *cp_parser_require_keyword
1756   PARAMS ((cp_parser *, enum rid, const char *));
1757 static bool cp_parser_token_starts_function_definition_p 
1758   PARAMS ((cp_token *));
1759 static bool cp_parser_next_token_starts_class_definition_p
1760   (cp_parser *);
1761 static enum tag_types cp_parser_token_is_class_key
1762   PARAMS ((cp_token *));
1763 static void cp_parser_check_class_key
1764   (enum tag_types, tree type);
1765 static bool cp_parser_optional_template_keyword
1766   (cp_parser *);
1767 static void cp_parser_pre_parsed_nested_name_specifier 
1768   (cp_parser *);
1769 static void cp_parser_cache_group
1770   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1771 static void cp_parser_parse_tentatively 
1772   PARAMS ((cp_parser *));
1773 static void cp_parser_commit_to_tentative_parse
1774   PARAMS ((cp_parser *));
1775 static void cp_parser_abort_tentative_parse
1776   PARAMS ((cp_parser *));
1777 static bool cp_parser_parse_definitely
1778   PARAMS ((cp_parser *));
1779 static inline bool cp_parser_parsing_tentatively
1780   PARAMS ((cp_parser *));
1781 static bool cp_parser_committed_to_tentative_parse
1782   PARAMS ((cp_parser *));
1783 static void cp_parser_error
1784   PARAMS ((cp_parser *, const char *));
1785 static bool cp_parser_simulate_error
1786   PARAMS ((cp_parser *));
1787 static void cp_parser_check_type_definition
1788   PARAMS ((cp_parser *));
1789 static bool cp_parser_skip_to_closing_parenthesis
1790   PARAMS ((cp_parser *));
1791 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1792   (cp_parser *);
1793 static void cp_parser_skip_to_end_of_statement
1794   PARAMS ((cp_parser *));
1795 static void cp_parser_skip_to_end_of_block_or_statement
1796   PARAMS ((cp_parser *));
1797 static void cp_parser_skip_to_closing_brace
1798   (cp_parser *);
1799 static void cp_parser_skip_until_found
1800   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1801 static bool cp_parser_error_occurred
1802   PARAMS ((cp_parser *));
1803 static bool cp_parser_allow_gnu_extensions_p
1804   PARAMS ((cp_parser *));
1805 static bool cp_parser_is_string_literal
1806   PARAMS ((cp_token *));
1807 static bool cp_parser_is_keyword 
1808   PARAMS ((cp_token *, enum rid));
1809 static bool cp_parser_dependent_type_p
1810   (tree);
1811 static bool cp_parser_value_dependent_expression_p
1812   (tree);
1813 static bool cp_parser_type_dependent_expression_p
1814   (tree);
1815 static bool cp_parser_dependent_template_arg_p
1816   (tree);
1817 static bool cp_parser_dependent_template_id_p
1818   (tree, tree);
1819 static bool cp_parser_dependent_template_p
1820   (tree);
1821 static void cp_parser_defer_access_check
1822   (cp_parser *, tree, tree);
1823 static void cp_parser_start_deferring_access_checks
1824   (cp_parser *);
1825 static tree cp_parser_stop_deferring_access_checks
1826   PARAMS ((cp_parser *));
1827 static void cp_parser_perform_deferred_access_checks
1828   PARAMS ((tree));
1829 static tree cp_parser_scope_through_which_access_occurs
1830   (tree, tree, tree);
1831
1832 /* Returns non-zero if we are parsing tentatively.  */
1833
1834 static inline bool
1835 cp_parser_parsing_tentatively (parser)
1836      cp_parser *parser;
1837 {
1838   return parser->context->next != NULL;
1839 }
1840
1841 /* Returns non-zero if TOKEN is a string literal.  */
1842
1843 static bool
1844 cp_parser_is_string_literal (token)
1845      cp_token *token;
1846 {
1847   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 }
1849
1850 /* Returns non-zero if TOKEN is the indicated KEYWORD.  */
1851
1852 static bool
1853 cp_parser_is_keyword (token, keyword)
1854      cp_token *token;
1855      enum rid keyword;
1856 {
1857   return token->keyword == keyword;
1858 }
1859
1860 /* Returns TRUE if TYPE is dependent, in the sense of
1861    [temp.dep.type].  */
1862
1863 static bool
1864 cp_parser_dependent_type_p (type)
1865      tree type;
1866 {
1867   tree scope;
1868
1869   if (!processing_template_decl)
1870     return false;
1871
1872   /* If the type is NULL, we have not computed a type for the entity
1873      in question; in that case, the type is dependent.  */
1874   if (!type)
1875     return true;
1876
1877   /* Erroneous types can be considered non-dependent.  */
1878   if (type == error_mark_node)
1879     return false;
1880
1881   /* [temp.dep.type]
1882
1883      A type is dependent if it is:
1884
1885      -- a template parameter.  */
1886   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1887     return true;
1888   /* -- a qualified-id with a nested-name-specifier which contains a
1889         class-name that names a dependent type or whose unqualified-id
1890         names a dependent type.  */
1891   if (TREE_CODE (type) == TYPENAME_TYPE)
1892     return true;
1893   /* -- a cv-qualified type where the cv-unqualified type is
1894         dependent.  */
1895   type = TYPE_MAIN_VARIANT (type);
1896   /* -- a compound type constructed from any dependent type.  */
1897   if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1898     return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1899             || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE 
1900                                            (type)));
1901   else if (TREE_CODE (type) == POINTER_TYPE
1902            || TREE_CODE (type) == REFERENCE_TYPE)
1903     return cp_parser_dependent_type_p (TREE_TYPE (type));
1904   else if (TREE_CODE (type) == FUNCTION_TYPE
1905            || TREE_CODE (type) == METHOD_TYPE)
1906     {
1907       tree arg_type;
1908
1909       if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1910         return true;
1911       for (arg_type = TYPE_ARG_TYPES (type); 
1912            arg_type; 
1913            arg_type = TREE_CHAIN (arg_type))
1914         if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1915           return true;
1916       return false;
1917     }
1918   /* -- an array type constructed from any dependent type or whose
1919         size is specified by a constant expression that is
1920         value-dependent.  */
1921   if (TREE_CODE (type) == ARRAY_TYPE)
1922     {
1923       if (TYPE_DOMAIN (type)
1924           && ((cp_parser_value_dependent_expression_p 
1925                (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1926               || (cp_parser_type_dependent_expression_p
1927                   (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1928         return true;
1929       return cp_parser_dependent_type_p (TREE_TYPE (type));
1930     }
1931   /* -- a template-id in which either the template name is a template
1932         parameter or any of the template arguments is a dependent type or
1933         an expression that is type-dependent or value-dependent.  
1934
1935      This language seems somewhat confused; for example, it does not
1936      discuss template template arguments.  Therefore, we use the
1937      definition for dependent template arguments in [temp.dep.temp].  */
1938   if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1939       && (cp_parser_dependent_template_id_p
1940           (CLASSTYPE_TI_TEMPLATE (type),
1941            CLASSTYPE_TI_ARGS (type))))
1942     return true;
1943   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1944     return true;
1945   /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1946      expression is not type-dependent, then it should already been
1947      have resolved.  */
1948   if (TREE_CODE (type) == TYPEOF_TYPE)
1949     return true;
1950   /* The standard does not specifically mention types that are local
1951      to template functions or local classes, but they should be
1952      considered dependent too.  For example:
1953
1954        template <int I> void f() { 
1955          enum E { a = I }; 
1956          S<sizeof (E)> s;
1957        }
1958
1959      The size of `E' cannot be known until the value of `I' has been
1960      determined.  Therefore, `E' must be considered dependent.  */
1961   scope = TYPE_CONTEXT (type);
1962   if (scope && TYPE_P (scope))
1963     return cp_parser_dependent_type_p (scope);
1964   else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1965     return cp_parser_type_dependent_expression_p (scope);
1966
1967   /* Other types are non-dependent.  */
1968   return false;
1969 }
1970
1971 /* Returns TRUE if the EXPRESSION is value-dependent.  */
1972
1973 static bool
1974 cp_parser_value_dependent_expression_p (tree expression)
1975 {
1976   if (!processing_template_decl)
1977     return false;
1978
1979   /* A name declared with a dependent type.  */
1980   if (DECL_P (expression)
1981       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1982     return true;
1983   /* A non-type template parameter.  */
1984   if ((TREE_CODE (expression) == CONST_DECL
1985        && DECL_TEMPLATE_PARM_P (expression))
1986       || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1987     return true;
1988   /* A constant with integral or enumeration type and is initialized 
1989      with an expression that is value-dependent.  */
1990   if (TREE_CODE (expression) == VAR_DECL
1991       && DECL_INITIAL (expression)
1992       && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1993           || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1994       && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1995     return true;
1996   /* These expressions are value-dependent if the type to which the
1997      cast occurs is dependent.  */
1998   if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1999        || TREE_CODE (expression) == STATIC_CAST_EXPR
2000        || TREE_CODE (expression) == CONST_CAST_EXPR
2001        || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2002        || TREE_CODE (expression) == CAST_EXPR)
2003       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
2004     return true;
2005   /* A `sizeof' expression where the sizeof operand is a type is
2006      value-dependent if the type is dependent.  If the type was not
2007      dependent, we would no longer have a SIZEOF_EXPR, so any
2008      SIZEOF_EXPR is dependent.  */
2009   if (TREE_CODE (expression) == SIZEOF_EXPR)
2010     return true;
2011   /* A constant expression is value-dependent if any subexpression is
2012      value-dependent.  */
2013   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
2014     {
2015       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
2016         {
2017         case '1':
2018           return (cp_parser_value_dependent_expression_p 
2019                   (TREE_OPERAND (expression, 0)));
2020         case '<':
2021         case '2':
2022           return ((cp_parser_value_dependent_expression_p 
2023                    (TREE_OPERAND (expression, 0)))
2024                   || (cp_parser_value_dependent_expression_p 
2025                       (TREE_OPERAND (expression, 1))));
2026         case 'e':
2027           {
2028             int i;
2029             for (i = 0; 
2030                  i < TREE_CODE_LENGTH (TREE_CODE (expression));
2031                  ++i)
2032               if (cp_parser_value_dependent_expression_p
2033                   (TREE_OPERAND (expression, i)))
2034                 return true;
2035             return false;
2036           }
2037         }
2038     }
2039
2040   /* The expression is not value-dependent.  */
2041   return false;
2042 }
2043
2044 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2045    [temp.dep.expr].  */
2046
2047 static bool
2048 cp_parser_type_dependent_expression_p (expression)
2049      tree expression;
2050 {
2051   if (!processing_template_decl)
2052     return false;
2053
2054   /* Some expression forms are never type-dependent.  */
2055   if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2056       || TREE_CODE (expression) == SIZEOF_EXPR
2057       || TREE_CODE (expression) == ALIGNOF_EXPR
2058       || TREE_CODE (expression) == TYPEID_EXPR
2059       || TREE_CODE (expression) == DELETE_EXPR
2060       || TREE_CODE (expression) == VEC_DELETE_EXPR
2061       || TREE_CODE (expression) == THROW_EXPR)
2062     return false;
2063
2064   /* The types of these expressions depends only on the type to which
2065      the cast occurs.  */
2066   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2067       || TREE_CODE (expression) == STATIC_CAST_EXPR
2068       || TREE_CODE (expression) == CONST_CAST_EXPR
2069       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2070       || TREE_CODE (expression) == CAST_EXPR)
2071     return cp_parser_dependent_type_p (TREE_TYPE (expression));
2072   /* The types of these expressions depends only on the type created
2073      by the expression.  */
2074   else if (TREE_CODE (expression) == NEW_EXPR
2075            || TREE_CODE (expression) == VEC_NEW_EXPR)
2076     return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2077
2078   if (TREE_CODE (expression) == FUNCTION_DECL
2079       && DECL_LANG_SPECIFIC (expression)
2080       && DECL_TEMPLATE_INFO (expression)
2081       && (cp_parser_dependent_template_id_p
2082           (DECL_TI_TEMPLATE (expression),
2083            INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2084     return true;
2085
2086   return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2087 }
2088
2089 /* Returns TRUE if the ARG (a template argument) is dependent.  */
2090
2091 static bool
2092 cp_parser_dependent_template_arg_p (tree arg)
2093 {
2094   if (!processing_template_decl)
2095     return false;
2096
2097   if (TREE_CODE (arg) == TEMPLATE_DECL
2098       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2099     return cp_parser_dependent_template_p (arg);
2100   else if (TYPE_P (arg))
2101     return cp_parser_dependent_type_p (arg);
2102   else
2103     return (cp_parser_type_dependent_expression_p (arg)
2104             || cp_parser_value_dependent_expression_p (arg));
2105 }
2106
2107 /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
2108
2109 static bool
2110 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2111 {
2112   int i;
2113
2114   if (cp_parser_dependent_template_p (tmpl))
2115     return true;
2116   for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2117     if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2118       return true;
2119   return false;
2120 }
2121
2122 /* Returns TRUE if the template TMPL is dependent.  */
2123
2124 static bool
2125 cp_parser_dependent_template_p (tree tmpl)
2126 {
2127   /* Template template parameters are dependent.  */
2128   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2129       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2130     return true;
2131   /* So are member templates of dependent classes.  */
2132   if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2133     return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2134   return false;
2135 }
2136
2137 /* Defer checking the accessibility of DECL, when looked up in
2138    CLASS_TYPE.  */
2139
2140 static void
2141 cp_parser_defer_access_check (cp_parser *parser, 
2142                               tree class_type,
2143                               tree decl)
2144 {
2145   tree check;
2146
2147   /* If we are not supposed to defer access checks, just check now.  */
2148   if (!parser->context->deferring_access_checks_p)
2149     {
2150       enforce_access (class_type, decl);
2151       return;
2152     }
2153
2154   /* See if we are already going to perform this check.  */
2155   for (check = parser->context->deferred_access_checks;
2156        check;
2157        check = TREE_CHAIN (check))
2158     if (TREE_VALUE (check) == decl
2159         && same_type_p (TREE_PURPOSE (check), class_type))
2160       return;
2161   /* If not, record the check.  */
2162   parser->context->deferred_access_checks
2163     = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2164 }
2165
2166 /* Start deferring access control checks.  */
2167
2168 static void
2169 cp_parser_start_deferring_access_checks (cp_parser *parser)
2170 {
2171   parser->context->deferring_access_checks_p = true;
2172 }
2173
2174 /* Stop deferring access control checks.  Returns a TREE_LIST
2175    representing the deferred checks.  The TREE_PURPOSE of each node is
2176    the type through which the access occurred; the TREE_VALUE is the
2177    declaration named.  */
2178
2179 static tree
2180 cp_parser_stop_deferring_access_checks (parser)
2181      cp_parser *parser;
2182 {
2183   tree access_checks;
2184
2185   parser->context->deferring_access_checks_p = false;
2186   access_checks = parser->context->deferred_access_checks;
2187   parser->context->deferred_access_checks = NULL_TREE;
2188
2189   return access_checks;
2190 }
2191
2192 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2193    documented with cp_parser_stop_deferrring_access_checks.  */
2194
2195 static void
2196 cp_parser_perform_deferred_access_checks (access_checks)
2197      tree access_checks;
2198 {
2199   tree deferred_check;
2200
2201   /* Look through all the deferred checks.  */
2202   for (deferred_check = access_checks;
2203        deferred_check;
2204        deferred_check = TREE_CHAIN (deferred_check))
2205     /* Check access.  */
2206     enforce_access (TREE_PURPOSE (deferred_check), 
2207                     TREE_VALUE (deferred_check));
2208 }
2209
2210 /* Returns the scope through which DECL is being accessed, or
2211    NULL_TREE if DECL is not a member.  If OBJECT_TYPE is non-NULL, we
2212    have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2213    or `x', respectively.  If the DECL was named as `A::B' then
2214    NESTED_NAME_SPECIFIER is `A'.  */
2215
2216 tree
2217 cp_parser_scope_through_which_access_occurs (decl, 
2218                                              object_type,
2219                                              nested_name_specifier)
2220      tree decl;
2221      tree object_type;
2222      tree nested_name_specifier;
2223 {
2224   tree scope;
2225   tree qualifying_type = NULL_TREE;
2226   
2227   /* Determine the SCOPE of DECL.  */
2228   scope = context_for_name_lookup (decl);
2229   /* If the SCOPE is not a type, then DECL is not a member.  */
2230   if (!TYPE_P (scope))
2231     return NULL_TREE;
2232   /* Figure out the type through which DECL is being accessed.  */
2233   if (object_type && DERIVED_FROM_P (scope, object_type))
2234     /* If we are processing a `->' or `.' expression, use the type of the
2235        left-hand side.  */
2236     qualifying_type = object_type;
2237   else if (nested_name_specifier)
2238     {
2239       /* If the reference is to a non-static member of the
2240          current class, treat it as if it were referenced through
2241          `this'.  */
2242       if (DECL_NONSTATIC_MEMBER_P (decl)
2243           && current_class_ptr
2244           && DERIVED_FROM_P (scope, current_class_type))
2245         qualifying_type = current_class_type;
2246       /* Otherwise, use the type indicated by the
2247          nested-name-specifier.  */
2248       else
2249         qualifying_type = nested_name_specifier;
2250     }
2251   else
2252     /* Otherwise, the name must be from the current class or one of
2253        its bases.  */
2254     qualifying_type = currently_open_derived_class (scope);
2255
2256   return qualifying_type;
2257 }
2258
2259 /* Issue the indicated error MESSAGE.  */
2260
2261 static void
2262 cp_parser_error (parser, message)
2263      cp_parser *parser;
2264      const char *message;
2265 {
2266   /* Output the MESSAGE -- unless we're parsing tentatively.  */
2267   if (!cp_parser_simulate_error (parser))
2268     error (message);
2269 }
2270
2271 /* If we are parsing tentatively, remember that an error has occurred
2272    during this tentative parse.  Returns true if the error was
2273    simulated; false if a messgae should be issued by the caller.  */
2274
2275 static bool
2276 cp_parser_simulate_error (parser)
2277      cp_parser *parser;
2278 {
2279   if (cp_parser_parsing_tentatively (parser)
2280       && !cp_parser_committed_to_tentative_parse (parser))
2281     {
2282       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2283       return true;
2284     }
2285   return false;
2286 }
2287
2288 /* This function is called when a type is defined.  If type
2289    definitions are forbidden at this point, an error message is
2290    issued.  */
2291
2292 static void
2293 cp_parser_check_type_definition (parser)
2294      cp_parser *parser;
2295 {
2296   /* If types are forbidden here, issue a message.  */
2297   if (parser->type_definition_forbidden_message)
2298     /* Use `%s' to print the string in case there are any escape
2299        characters in the message.  */
2300     error ("%s", parser->type_definition_forbidden_message);
2301 }
2302
2303 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2304    Returns TRUE iff we found a closing `)'.  */
2305
2306 static bool
2307 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2308 {
2309   unsigned nesting_depth = 0;
2310
2311   while (true)
2312     {
2313       cp_token *token;
2314
2315       /* If we've run out of tokens, then there is no closing `)'.  */
2316       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2317         return false;
2318       /* Consume the token.  */
2319       token = cp_lexer_consume_token (parser->lexer);
2320       /* If it is an `(', we have entered another level of nesting.  */
2321       if (token->type == CPP_OPEN_PAREN)
2322         ++nesting_depth;
2323       /* If it is a `)', then we might be done.  */
2324       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2325         return true;
2326     }
2327 }
2328
2329 /* Consume tokens until the next token is a `)', or a `,'.  Returns
2330    TRUE if the next token is a `,'.  */
2331
2332 static bool
2333 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2334 {
2335   unsigned nesting_depth = 0;
2336
2337   while (true)
2338     {
2339       cp_token *token = cp_lexer_peek_token (parser->lexer);
2340
2341       /* If we've run out of tokens, then there is no closing `)'.  */
2342       if (token->type == CPP_EOF)
2343         return false;
2344       /* If it is a `,' stop.  */
2345       else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2346         return true;
2347       /* If it is a `)', stop.  */
2348       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2349         return false;
2350       /* If it is an `(', we have entered another level of nesting.  */
2351       else if (token->type == CPP_OPEN_PAREN)
2352         ++nesting_depth;
2353       /* Consume the token.  */
2354       token = cp_lexer_consume_token (parser->lexer);
2355     }
2356 }
2357
2358 /* Consume tokens until we reach the end of the current statement.
2359    Normally, that will be just before consuming a `;'.  However, if a
2360    non-nested `}' comes first, then we stop before consuming that.  */
2361
2362 static void
2363 cp_parser_skip_to_end_of_statement (parser)
2364      cp_parser *parser;
2365 {
2366   unsigned nesting_depth = 0;
2367
2368   while (true)
2369     {
2370       cp_token *token;
2371
2372       /* Peek at the next token.  */
2373       token = cp_lexer_peek_token (parser->lexer);
2374       /* If we've run out of tokens, stop.  */
2375       if (token->type == CPP_EOF)
2376         break;
2377       /* If the next token is a `;', we have reached the end of the
2378          statement.  */
2379       if (token->type == CPP_SEMICOLON && !nesting_depth)
2380         break;
2381       /* If the next token is a non-nested `}', then we have reached
2382          the end of the current block.  */
2383       if (token->type == CPP_CLOSE_BRACE)
2384         {
2385           /* If this is a non-nested `}', stop before consuming it.
2386              That way, when confronted with something like:
2387
2388                { 3 + } 
2389
2390              we stop before consuming the closing `}', even though we
2391              have not yet reached a `;'.  */
2392           if (nesting_depth == 0)
2393             break;
2394           /* If it is the closing `}' for a block that we have
2395              scanned, stop -- but only after consuming the token.
2396              That way given:
2397
2398                 void f g () { ... }
2399                 typedef int I;
2400
2401              we will stop after the body of the erroneously declared
2402              function, but before consuming the following `typedef'
2403              declaration.  */
2404           if (--nesting_depth == 0)
2405             {
2406               cp_lexer_consume_token (parser->lexer);
2407               break;
2408             }
2409         }
2410       /* If it the next token is a `{', then we are entering a new
2411          block.  Consume the entire block.  */
2412       else if (token->type == CPP_OPEN_BRACE)
2413         ++nesting_depth;
2414       /* Consume the token.  */
2415       cp_lexer_consume_token (parser->lexer);
2416     }
2417 }
2418
2419 /* Skip tokens until we have consumed an entire block, or until we
2420    have consumed a non-nested `;'.  */
2421
2422 static void
2423 cp_parser_skip_to_end_of_block_or_statement (parser)
2424      cp_parser *parser;
2425 {
2426   unsigned nesting_depth = 0;
2427
2428   while (true)
2429     {
2430       cp_token *token;
2431
2432       /* Peek at the next token.  */
2433       token = cp_lexer_peek_token (parser->lexer);
2434       /* If we've run out of tokens, stop.  */
2435       if (token->type == CPP_EOF)
2436         break;
2437       /* If the next token is a `;', we have reached the end of the
2438          statement.  */
2439       if (token->type == CPP_SEMICOLON && !nesting_depth)
2440         {
2441           /* Consume the `;'.  */
2442           cp_lexer_consume_token (parser->lexer);
2443           break;
2444         }
2445       /* Consume the token.  */
2446       token = cp_lexer_consume_token (parser->lexer);
2447       /* If the next token is a non-nested `}', then we have reached
2448          the end of the current block.  */
2449       if (token->type == CPP_CLOSE_BRACE 
2450           && (nesting_depth == 0 || --nesting_depth == 0))
2451         break;
2452       /* If it the next token is a `{', then we are entering a new
2453          block.  Consume the entire block.  */
2454       if (token->type == CPP_OPEN_BRACE)
2455         ++nesting_depth;
2456     }
2457 }
2458
2459 /* Skip tokens until a non-nested closing curly brace is the next
2460    token.  */
2461
2462 static void
2463 cp_parser_skip_to_closing_brace (cp_parser *parser)
2464 {
2465   unsigned nesting_depth = 0;
2466
2467   while (true)
2468     {
2469       cp_token *token;
2470
2471       /* Peek at the next token.  */
2472       token = cp_lexer_peek_token (parser->lexer);
2473       /* If we've run out of tokens, stop.  */
2474       if (token->type == CPP_EOF)
2475         break;
2476       /* If the next token is a non-nested `}', then we have reached
2477          the end of the current block.  */
2478       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2479         break;
2480       /* If it the next token is a `{', then we are entering a new
2481          block.  Consume the entire block.  */
2482       else if (token->type == CPP_OPEN_BRACE)
2483         ++nesting_depth;
2484       /* Consume the token.  */
2485       cp_lexer_consume_token (parser->lexer);
2486     }
2487 }
2488
2489 /* Create a new C++ parser.  */
2490
2491 static cp_parser *
2492 cp_parser_new ()
2493 {
2494   cp_parser *parser;
2495   cp_lexer *lexer;
2496
2497   /* cp_lexer_new_main is called before calling ggc_alloc because
2498      cp_lexer_new_main might load a PCH file.  */
2499   lexer = cp_lexer_new_main ();
2500
2501   parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2502   parser->lexer = lexer;
2503   parser->context = cp_parser_context_new (NULL);
2504
2505   /* For now, we always accept GNU extensions.  */
2506   parser->allow_gnu_extensions_p = 1;
2507
2508   /* The `>' token is a greater-than operator, not the end of a
2509      template-id.  */
2510   parser->greater_than_is_operator_p = true;
2511
2512   parser->default_arg_ok_p = true;
2513   
2514   /* We are not parsing a constant-expression.  */
2515   parser->constant_expression_p = false;
2516
2517   /* Local variable names are not forbidden.  */
2518   parser->local_variables_forbidden_p = false;
2519
2520   /* We are not procesing an `extern "C"' declaration.  */
2521   parser->in_unbraced_linkage_specification_p = false;
2522
2523   /* We are not processing a declarator.  */
2524   parser->in_declarator_p = false;
2525
2526   /* The unparsed function queue is empty.  */
2527   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2528
2529   /* There are no classes being defined.  */
2530   parser->num_classes_being_defined = 0;
2531
2532   /* No template parameters apply.  */
2533   parser->num_template_parameter_lists = 0;
2534
2535   return parser;
2536 }
2537
2538 /* Lexical conventions [gram.lex]  */
2539
2540 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2541    identifier.  */
2542
2543 static tree 
2544 cp_parser_identifier (parser)
2545      cp_parser *parser;
2546 {
2547   cp_token *token;
2548
2549   /* Look for the identifier.  */
2550   token = cp_parser_require (parser, CPP_NAME, "identifier");
2551   /* Return the value.  */
2552   return token ? token->value : error_mark_node;
2553 }
2554
2555 /* Basic concepts [gram.basic]  */
2556
2557 /* Parse a translation-unit.
2558
2559    translation-unit:
2560      declaration-seq [opt]  
2561
2562    Returns TRUE if all went well.  */
2563
2564 static bool
2565 cp_parser_translation_unit (parser)
2566      cp_parser *parser;
2567 {
2568   while (true)
2569     {
2570       cp_parser_declaration_seq_opt (parser);
2571
2572       /* If there are no tokens left then all went well.  */
2573       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2574         break;
2575       
2576       /* Otherwise, issue an error message.  */
2577       cp_parser_error (parser, "expected declaration");
2578       return false;
2579     }
2580
2581   /* Consume the EOF token.  */
2582   cp_parser_require (parser, CPP_EOF, "end-of-file");
2583   
2584   /* Finish up.  */
2585   finish_translation_unit ();
2586
2587   /* All went well.  */
2588   return true;
2589 }
2590
2591 /* Expressions [gram.expr] */
2592
2593 /* Parse a primary-expression.
2594
2595    primary-expression:
2596      literal
2597      this
2598      ( expression )
2599      id-expression
2600
2601    GNU Extensions:
2602
2603    primary-expression:
2604      ( compound-statement )
2605      __builtin_va_arg ( assignment-expression , type-id )
2606
2607    literal:
2608      __null
2609
2610    Returns a representation of the expression.  
2611
2612    *IDK indicates what kind of id-expression (if any) was present.  
2613
2614    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2615    used as the operand of a pointer-to-member.  In that case,
2616    *QUALIFYING_CLASS gives the class that is used as the qualifying
2617    class in the pointer-to-member.  */
2618
2619 static tree
2620 cp_parser_primary_expression (cp_parser *parser, 
2621                               cp_parser_id_kind *idk,
2622                               tree *qualifying_class)
2623 {
2624   cp_token *token;
2625
2626   /* Assume the primary expression is not an id-expression.  */
2627   *idk = CP_PARSER_ID_KIND_NONE;
2628   /* And that it cannot be used as pointer-to-member.  */
2629   *qualifying_class = NULL_TREE;
2630
2631   /* Peek at the next token.  */
2632   token = cp_lexer_peek_token (parser->lexer);
2633   switch (token->type)
2634     {
2635       /* literal:
2636            integer-literal
2637            character-literal
2638            floating-literal
2639            string-literal
2640            boolean-literal  */
2641     case CPP_CHAR:
2642     case CPP_WCHAR:
2643     case CPP_STRING:
2644     case CPP_WSTRING:
2645     case CPP_NUMBER:
2646       token = cp_lexer_consume_token (parser->lexer);
2647       return token->value;
2648
2649     case CPP_OPEN_PAREN:
2650       {
2651         tree expr;
2652         bool saved_greater_than_is_operator_p;
2653
2654         /* Consume the `('.  */
2655         cp_lexer_consume_token (parser->lexer);
2656         /* Within a parenthesized expression, a `>' token is always
2657            the greater-than operator.  */
2658         saved_greater_than_is_operator_p 
2659           = parser->greater_than_is_operator_p;
2660         parser->greater_than_is_operator_p = true;
2661         /* If we see `( { ' then we are looking at the beginning of
2662            a GNU statement-expression.  */
2663         if (cp_parser_allow_gnu_extensions_p (parser)
2664             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2665           {
2666             /* Statement-expressions are not allowed by the standard.  */
2667             if (pedantic)
2668               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2669             
2670             /* And they're not allowed outside of a function-body; you
2671                cannot, for example, write:
2672                
2673                  int i = ({ int j = 3; j + 1; });
2674                
2675                at class or namespace scope.  */
2676             if (!at_function_scope_p ())
2677               error ("statement-expressions are allowed only inside functions");
2678             /* Start the statement-expression.  */
2679             expr = begin_stmt_expr ();
2680             /* Parse the compound-statement.  */
2681             cp_parser_compound_statement (parser);
2682             /* Finish up.  */
2683             expr = finish_stmt_expr (expr);
2684           }
2685         else
2686           {
2687             /* Parse the parenthesized expression.  */
2688             expr = cp_parser_expression (parser);
2689             /* Let the front end know that this expression was
2690                enclosed in parentheses. This matters in case, for
2691                example, the expression is of the form `A::B', since
2692                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2693                not.  */
2694             finish_parenthesized_expr (expr);
2695           }
2696         /* The `>' token might be the end of a template-id or
2697            template-parameter-list now.  */
2698         parser->greater_than_is_operator_p 
2699           = saved_greater_than_is_operator_p;
2700         /* Consume the `)'.  */
2701         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2702           cp_parser_skip_to_end_of_statement (parser);
2703
2704         return expr;
2705       }
2706
2707     case CPP_KEYWORD:
2708       switch (token->keyword)
2709         {
2710           /* These two are the boolean literals.  */
2711         case RID_TRUE:
2712           cp_lexer_consume_token (parser->lexer);
2713           return boolean_true_node;
2714         case RID_FALSE:
2715           cp_lexer_consume_token (parser->lexer);
2716           return boolean_false_node;
2717           
2718           /* The `__null' literal.  */
2719         case RID_NULL:
2720           cp_lexer_consume_token (parser->lexer);
2721           return null_node;
2722
2723           /* Recognize the `this' keyword.  */
2724         case RID_THIS:
2725           cp_lexer_consume_token (parser->lexer);
2726           if (parser->local_variables_forbidden_p)
2727             {
2728               error ("`this' may not be used in this context");
2729               return error_mark_node;
2730             }
2731           return finish_this_expr ();
2732
2733           /* The `operator' keyword can be the beginning of an
2734              id-expression.  */
2735         case RID_OPERATOR:
2736           goto id_expression;
2737
2738         case RID_FUNCTION_NAME:
2739         case RID_PRETTY_FUNCTION_NAME:
2740         case RID_C99_FUNCTION_NAME:
2741           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2742              __func__ are the names of variables -- but they are
2743              treated specially.  Therefore, they are handled here,
2744              rather than relying on the generic id-expression logic
2745              below.  Gramatically, these names are id-expressions.  
2746
2747              Consume the token.  */
2748           token = cp_lexer_consume_token (parser->lexer);
2749           /* Look up the name.  */
2750           return finish_fname (token->value);
2751
2752         case RID_VA_ARG:
2753           {
2754             tree expression;
2755             tree type;
2756
2757             /* The `__builtin_va_arg' construct is used to handle
2758                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2759             cp_lexer_consume_token (parser->lexer);
2760             /* Look for the opening `('.  */
2761             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2762             /* Now, parse the assignment-expression.  */
2763             expression = cp_parser_assignment_expression (parser);
2764             /* Look for the `,'.  */
2765             cp_parser_require (parser, CPP_COMMA, "`,'");
2766             /* Parse the type-id.  */
2767             type = cp_parser_type_id (parser);
2768             /* Look for the closing `)'.  */
2769             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2770
2771             return build_x_va_arg (expression, type);
2772           }
2773
2774         default:
2775           cp_parser_error (parser, "expected primary-expression");
2776           return error_mark_node;
2777         }
2778       /* Fall through. */
2779
2780       /* An id-expression can start with either an identifier, a
2781          `::' as the beginning of a qualified-id, or the "operator"
2782          keyword.  */
2783     case CPP_NAME:
2784     case CPP_SCOPE:
2785     case CPP_TEMPLATE_ID:
2786     case CPP_NESTED_NAME_SPECIFIER:
2787       {
2788         tree id_expression;
2789         tree decl;
2790
2791       id_expression:
2792         /* Parse the id-expression.  */
2793         id_expression 
2794           = cp_parser_id_expression (parser, 
2795                                      /*template_keyword_p=*/false,
2796                                      /*check_dependency_p=*/true,
2797                                      /*template_p=*/NULL);
2798         if (id_expression == error_mark_node)
2799           return error_mark_node;
2800         /* If we have a template-id, then no further lookup is
2801            required.  If the template-id was for a template-class, we
2802            will sometimes have a TYPE_DECL at this point.  */
2803         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2804             || TREE_CODE (id_expression) == TYPE_DECL)
2805           decl = id_expression;
2806         /* Look up the name.  */
2807         else 
2808           {
2809             decl = cp_parser_lookup_name_simple (parser, id_expression);
2810             /* If name lookup gives us a SCOPE_REF, then the
2811                qualifying scope was dependent.  Just propagate the
2812                name.  */
2813             if (TREE_CODE (decl) == SCOPE_REF)
2814               {
2815                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2816                   *qualifying_class = TREE_OPERAND (decl, 0);
2817                 return decl;
2818               }
2819             /* Check to see if DECL is a local variable in a context
2820                where that is forbidden.  */
2821             if (parser->local_variables_forbidden_p
2822                 && local_variable_p (decl))
2823               {
2824                 /* It might be that we only found DECL because we are
2825                    trying to be generous with pre-ISO scoping rules.
2826                    For example, consider:
2827
2828                      int i;
2829                      void g() {
2830                        for (int i = 0; i < 10; ++i) {}
2831                        extern void f(int j = i);
2832                      }
2833
2834                    Here, name look up will originally find the out 
2835                    of scope `i'.  We need to issue a warning message,
2836                    but then use the global `i'.  */
2837                 decl = check_for_out_of_scope_variable (decl);
2838                 if (local_variable_p (decl))
2839                   {
2840                     error ("local variable `%D' may not appear in this context",
2841                            decl);
2842                     return error_mark_node;
2843                   }
2844               }
2845
2846             /* If unqualified name lookup fails while processing a
2847                template, that just means that we need to do name
2848                lookup again when the template is instantiated.  */
2849             if (!parser->scope 
2850                 && decl == error_mark_node
2851                 && processing_template_decl)
2852               {
2853                 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2854                 return build_min_nt (LOOKUP_EXPR, id_expression);
2855               }
2856             else if (decl == error_mark_node
2857                      && !processing_template_decl)
2858               {
2859                 if (!parser->scope)
2860                   {
2861                     /* It may be resolvable as a koenig lookup function
2862                        call.  */
2863                     *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2864                     return id_expression;
2865                   }
2866                 else if (TYPE_P (parser->scope)
2867                          && !COMPLETE_TYPE_P (parser->scope))
2868                   error ("incomplete type `%T' used in nested name specifier",
2869                          parser->scope);
2870                 else if (parser->scope != global_namespace)
2871                   error ("`%D' is not a member of `%D'",
2872                          id_expression, parser->scope);
2873                 else
2874                   error ("`::%D' has not been declared", id_expression);
2875               }
2876             /* If DECL is a variable would be out of scope under
2877                ANSI/ISO rules, but in scope in the ARM, name lookup
2878                will succeed.  Issue a diagnostic here.  */
2879             else
2880               decl = check_for_out_of_scope_variable (decl);
2881
2882             /* Remember that the name was used in the definition of
2883                the current class so that we can check later to see if
2884                the meaning would have been different after the class
2885                was entirely defined.  */
2886             if (!parser->scope && decl != error_mark_node)
2887               maybe_note_name_used_in_class (id_expression, decl);
2888           }
2889
2890         /* If we didn't find anything, or what we found was a type,
2891            then this wasn't really an id-expression.  */
2892         if (TREE_CODE (decl) == TYPE_DECL
2893             || TREE_CODE (decl) == NAMESPACE_DECL
2894             || (TREE_CODE (decl) == TEMPLATE_DECL
2895                 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2896           {
2897             cp_parser_error (parser, 
2898                              "expected primary-expression");
2899             return error_mark_node;
2900           }
2901
2902         /* If the name resolved to a template parameter, there is no
2903            need to look it up again later.  Similarly, we resolve
2904            enumeration constants to their underlying values.  */
2905         if (TREE_CODE (decl) == CONST_DECL)
2906           {
2907             *idk = CP_PARSER_ID_KIND_NONE;
2908             if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2909               return DECL_INITIAL (decl);
2910             return decl;
2911           }
2912         else
2913           {
2914             bool dependent_p;
2915             
2916             /* If the declaration was explicitly qualified indicate
2917                that.  The semantics of `A::f(3)' are different than
2918                `f(3)' if `f' is virtual.  */
2919             *idk = (parser->scope 
2920                     ? CP_PARSER_ID_KIND_QUALIFIED
2921                     : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2922                        ? CP_PARSER_ID_KIND_TEMPLATE_ID
2923                        : CP_PARSER_ID_KIND_UNQUALIFIED));
2924
2925
2926             /* [temp.dep.expr]
2927                
2928                An id-expression is type-dependent if it contains an
2929                identifier that was declared with a dependent type.
2930                
2931                As an optimization, we could choose not to create a
2932                LOOKUP_EXPR for a name that resolved to a local
2933                variable in the template function that we are currently
2934                declaring; such a name cannot ever resolve to anything
2935                else.  If we did that we would not have to look up
2936                these names at instantiation time.
2937                
2938                The standard is not very specific about an
2939                id-expression that names a set of overloaded functions.
2940                What if some of them have dependent types and some of
2941                them do not?  Presumably, such a name should be treated
2942                as a dependent name.  */
2943             /* Assume the name is not dependent.  */
2944             dependent_p = false;
2945             if (!processing_template_decl)
2946               /* No names are dependent outside a template.  */
2947               ;
2948             /* A template-id where the name of the template was not
2949                resolved is definitely dependent.  */
2950             else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2951                      && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2952                          == IDENTIFIER_NODE))
2953               dependent_p = true;
2954             /* For anything except an overloaded function, just check
2955                its type.  */
2956             else if (!is_overloaded_fn (decl))
2957               dependent_p 
2958                 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2959             /* For a set of overloaded functions, check each of the
2960                functions.  */
2961             else
2962               {
2963                 tree fns = decl;
2964
2965                 if (BASELINK_P (fns))
2966                   fns = BASELINK_FUNCTIONS (fns);
2967                   
2968                 /* For a template-id, check to see if the template
2969                    arguments are dependent.  */
2970                 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2971                   {
2972                     tree args = TREE_OPERAND (fns, 1);
2973
2974                     if (args && TREE_CODE (args) == TREE_LIST)
2975                       {
2976                         while (args)
2977                           {
2978                             if (cp_parser_dependent_template_arg_p
2979                                 (TREE_VALUE (args)))
2980                               {
2981                                 dependent_p = true;
2982                                 break;
2983                               }
2984                             args = TREE_CHAIN (args);
2985                           }
2986                       }
2987                     else if (args && TREE_CODE (args) == TREE_VEC)
2988                       {
2989                         int i; 
2990                         for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2991                           if (cp_parser_dependent_template_arg_p
2992                               (TREE_VEC_ELT (args, i)))
2993                             {
2994                               dependent_p = true;
2995                               break;
2996                             }
2997                       }
2998
2999                     /* The functions are those referred to by the
3000                        template-id.  */
3001                     fns = TREE_OPERAND (fns, 0);
3002                   }
3003
3004                 /* If there are no dependent template arguments, go
3005                    through the overlaoded functions.  */
3006                 while (fns && !dependent_p)
3007                   {
3008                     tree fn = OVL_CURRENT (fns);
3009                     
3010                     /* Member functions of dependent classes are
3011                        dependent.  */
3012                     if (TREE_CODE (fn) == FUNCTION_DECL
3013                         && cp_parser_type_dependent_expression_p (fn))
3014                       dependent_p = true;
3015                     else if (TREE_CODE (fn) == TEMPLATE_DECL
3016                              && cp_parser_dependent_template_p (fn))
3017                       dependent_p = true;
3018                     
3019                     fns = OVL_NEXT (fns);
3020                   }
3021               }
3022
3023             /* If the name was dependent on a template parameter,
3024                we will resolve the name at instantiation time.  */
3025             if (dependent_p)
3026               {
3027                 /* Create a SCOPE_REF for qualified names.  */
3028                 if (parser->scope)
3029                   {
3030                     if (TYPE_P (parser->scope))
3031                       *qualifying_class = parser->scope;
3032                     return build_nt (SCOPE_REF, 
3033                                      parser->scope, 
3034                                      id_expression);
3035                   }
3036                 /* A TEMPLATE_ID already contains all the information
3037                    we need.  */
3038                 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3039                   return id_expression;
3040                 /* Create a LOOKUP_EXPR for other unqualified names.  */
3041                 return build_min_nt (LOOKUP_EXPR, id_expression);
3042               }
3043
3044             if (parser->scope)
3045               {
3046                 decl = (adjust_result_of_qualified_name_lookup 
3047                         (decl, parser->scope, current_class_type));
3048                 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3049                   *qualifying_class = parser->scope;
3050               }
3051             else
3052               /* Transform references to non-static data members into
3053                  COMPONENT_REFs.  */
3054               decl = hack_identifier (decl, id_expression);
3055
3056             /* Resolve references to variables of anonymous unions
3057                into COMPONENT_REFs.  */
3058             if (TREE_CODE (decl) == ALIAS_DECL)
3059               decl = DECL_INITIAL (decl);
3060           }
3061
3062         if (TREE_DEPRECATED (decl))
3063           warn_deprecated_use (decl);
3064
3065         return decl;
3066       }
3067
3068       /* Anything else is an error.  */
3069     default:
3070       cp_parser_error (parser, "expected primary-expression");
3071       return error_mark_node;
3072     }
3073 }
3074
3075 /* Parse an id-expression.
3076
3077    id-expression:
3078      unqualified-id
3079      qualified-id
3080
3081    qualified-id:
3082      :: [opt] nested-name-specifier template [opt] unqualified-id
3083      :: identifier
3084      :: operator-function-id
3085      :: template-id
3086
3087    Return a representation of the unqualified portion of the
3088    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3089    a `::' or nested-name-specifier.
3090
3091    Often, if the id-expression was a qualified-id, the caller will
3092    want to make a SCOPE_REF to represent the qualified-id.  This
3093    function does not do this in order to avoid wastefully creating
3094    SCOPE_REFs when they are not required.
3095
3096    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3097    `template' keyword.
3098
3099    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3100    uninstantiated templates.  
3101
3102    If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3103    `template' keyword is used to explicitly indicate that the entity
3104    named is a template.  */
3105
3106 static tree
3107 cp_parser_id_expression (cp_parser *parser,
3108                          bool template_keyword_p,
3109                          bool check_dependency_p,
3110                          bool *template_p)
3111 {
3112   bool global_scope_p;
3113   bool nested_name_specifier_p;
3114
3115   /* Assume the `template' keyword was not used.  */
3116   if (template_p)
3117     *template_p = false;
3118
3119   /* Look for the optional `::' operator.  */
3120   global_scope_p 
3121     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
3122        != NULL_TREE);
3123   /* Look for the optional nested-name-specifier.  */
3124   nested_name_specifier_p 
3125     = (cp_parser_nested_name_specifier_opt (parser,
3126                                             /*typename_keyword_p=*/false,
3127                                             check_dependency_p,
3128                                             /*type_p=*/false)
3129        != NULL_TREE);
3130   /* If there is a nested-name-specifier, then we are looking at
3131      the first qualified-id production.  */
3132   if (nested_name_specifier_p)
3133     {
3134       tree saved_scope;
3135       tree saved_object_scope;
3136       tree saved_qualifying_scope;
3137       tree unqualified_id;
3138       bool is_template;
3139
3140       /* See if the next token is the `template' keyword.  */
3141       if (!template_p)
3142         template_p = &is_template;
3143       *template_p = cp_parser_optional_template_keyword (parser);
3144       /* Name lookup we do during the processing of the
3145          unqualified-id might obliterate SCOPE.  */
3146       saved_scope = parser->scope;
3147       saved_object_scope = parser->object_scope;
3148       saved_qualifying_scope = parser->qualifying_scope;
3149       /* Process the final unqualified-id.  */
3150       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3151                                                  check_dependency_p);
3152       /* Restore the SAVED_SCOPE for our caller.  */
3153       parser->scope = saved_scope;
3154       parser->object_scope = saved_object_scope;
3155       parser->qualifying_scope = saved_qualifying_scope;
3156
3157       return unqualified_id;
3158     }
3159   /* Otherwise, if we are in global scope, then we are looking at one
3160      of the other qualified-id productions.  */
3161   else if (global_scope_p)
3162     {
3163       cp_token *token;
3164       tree id;
3165
3166       /* Peek at the next token.  */
3167       token = cp_lexer_peek_token (parser->lexer);
3168
3169       /* If it's an identifier, and the next token is not a "<", then
3170          we can avoid the template-id case.  This is an optimization
3171          for this common case.  */
3172       if (token->type == CPP_NAME 
3173           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3174         return cp_parser_identifier (parser);
3175
3176       cp_parser_parse_tentatively (parser);
3177       /* Try a template-id.  */
3178       id = cp_parser_template_id (parser, 
3179                                   /*template_keyword_p=*/false,
3180                                   /*check_dependency_p=*/true);
3181       /* If that worked, we're done.  */
3182       if (cp_parser_parse_definitely (parser))
3183         return id;
3184
3185       /* Peek at the next token.  (Changes in the token buffer may
3186          have invalidated the pointer obtained above.)  */
3187       token = cp_lexer_peek_token (parser->lexer);
3188
3189       switch (token->type)
3190         {
3191         case CPP_NAME:
3192           return cp_parser_identifier (parser);
3193
3194         case CPP_KEYWORD:
3195           if (token->keyword == RID_OPERATOR)
3196             return cp_parser_operator_function_id (parser);
3197           /* Fall through.  */
3198           
3199         default:
3200           cp_parser_error (parser, "expected id-expression");
3201           return error_mark_node;
3202         }
3203     }
3204   else
3205     return cp_parser_unqualified_id (parser, template_keyword_p,
3206                                      /*check_dependency_p=*/true);
3207 }
3208
3209 /* Parse an unqualified-id.
3210
3211    unqualified-id:
3212      identifier
3213      operator-function-id
3214      conversion-function-id
3215      ~ class-name
3216      template-id
3217
3218    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3219    keyword, in a construct like `A::template ...'.
3220
3221    Returns a representation of unqualified-id.  For the `identifier'
3222    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3223    production a BIT_NOT_EXPR is returned; the operand of the
3224    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3225    other productions, see the documentation accompanying the
3226    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3227    names are looked up in uninstantiated templates.  */
3228
3229 static tree
3230 cp_parser_unqualified_id (parser, template_keyword_p,
3231                           check_dependency_p)
3232      cp_parser *parser;
3233      bool template_keyword_p;
3234      bool check_dependency_p;
3235 {
3236   cp_token *token;
3237
3238   /* Peek at the next token.  */
3239   token = cp_lexer_peek_token (parser->lexer);
3240   
3241   switch (token->type)
3242     {
3243     case CPP_NAME:
3244       {
3245         tree id;
3246
3247         /* We don't know yet whether or not this will be a
3248            template-id.  */
3249         cp_parser_parse_tentatively (parser);
3250         /* Try a template-id.  */
3251         id = cp_parser_template_id (parser, template_keyword_p,
3252                                     check_dependency_p);
3253         /* If it worked, we're done.  */
3254         if (cp_parser_parse_definitely (parser))
3255           return id;
3256         /* Otherwise, it's an ordinary identifier.  */
3257         return cp_parser_identifier (parser);
3258       }
3259
3260     case CPP_TEMPLATE_ID:
3261       return cp_parser_template_id (parser, template_keyword_p,
3262                                     check_dependency_p);
3263
3264     case CPP_COMPL:
3265       {
3266         tree type_decl;
3267         tree qualifying_scope;
3268         tree object_scope;
3269         tree scope;
3270
3271         /* Consume the `~' token.  */
3272         cp_lexer_consume_token (parser->lexer);
3273         /* Parse the class-name.  The standard, as written, seems to
3274            say that:
3275
3276              template <typename T> struct S { ~S (); };
3277              template <typename T> S<T>::~S() {}
3278
3279            is invalid, since `~' must be followed by a class-name, but
3280            `S<T>' is dependent, and so not known to be a class.
3281            That's not right; we need to look in uninstantiated
3282            templates.  A further complication arises from:
3283
3284              template <typename T> void f(T t) {
3285                t.T::~T();
3286              } 
3287
3288            Here, it is not possible to look up `T' in the scope of `T'
3289            itself.  We must look in both the current scope, and the
3290            scope of the containing complete expression.  
3291
3292            Yet another issue is:
3293
3294              struct S {
3295                int S;
3296                ~S();
3297              };
3298
3299              S::~S() {}
3300
3301            The standard does not seem to say that the `S' in `~S'
3302            should refer to the type `S' and not the data member
3303            `S::S'.  */
3304
3305         /* DR 244 says that we look up the name after the "~" in the
3306            same scope as we looked up the qualifying name.  That idea
3307            isn't fully worked out; it's more complicated than that.  */
3308         scope = parser->scope;
3309         object_scope = parser->object_scope;
3310         qualifying_scope = parser->qualifying_scope;
3311
3312         /* If the name is of the form "X::~X" it's OK.  */
3313         if (scope && TYPE_P (scope)
3314             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3315             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3316                 == CPP_OPEN_PAREN)
3317             && (cp_lexer_peek_token (parser->lexer)->value 
3318                 == TYPE_IDENTIFIER (scope)))
3319           {
3320             cp_lexer_consume_token (parser->lexer);
3321             return build_nt (BIT_NOT_EXPR, scope);
3322           }
3323
3324         /* If there was an explicit qualification (S::~T), first look
3325            in the scope given by the qualification (i.e., S).  */
3326         if (scope)
3327           {
3328             cp_parser_parse_tentatively (parser);
3329             type_decl = cp_parser_class_name (parser, 
3330                                               /*typename_keyword_p=*/false,
3331                                               /*template_keyword_p=*/false,
3332                                               /*type_p=*/false,
3333                                               /*check_access_p=*/true,
3334                                               /*check_dependency=*/false,
3335                                               /*class_head_p=*/false);
3336             if (cp_parser_parse_definitely (parser))
3337               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3338           }
3339         /* In "N::S::~S", look in "N" as well.  */
3340         if (scope && qualifying_scope)
3341           {
3342             cp_parser_parse_tentatively (parser);
3343             parser->scope = qualifying_scope;
3344             parser->object_scope = NULL_TREE;
3345             parser->qualifying_scope = NULL_TREE;
3346             type_decl 
3347               = cp_parser_class_name (parser, 
3348                                       /*typename_keyword_p=*/false,
3349                                       /*template_keyword_p=*/false,
3350                                       /*type_p=*/false,
3351                                       /*check_access_p=*/true,
3352                                       /*check_dependency=*/false,
3353                                       /*class_head_p=*/false);
3354             if (cp_parser_parse_definitely (parser))
3355               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3356           }
3357         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3358         else if (object_scope)
3359           {
3360             cp_parser_parse_tentatively (parser);
3361             parser->scope = object_scope;
3362             parser->object_scope = NULL_TREE;
3363             parser->qualifying_scope = NULL_TREE;
3364             type_decl 
3365               = cp_parser_class_name (parser, 
3366                                       /*typename_keyword_p=*/false,
3367                                       /*template_keyword_p=*/false,
3368                                       /*type_p=*/false,
3369                                       /*check_access_p=*/true,
3370                                       /*check_dependency=*/false,
3371                                       /*class_head_p=*/false);
3372             if (cp_parser_parse_definitely (parser))
3373               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3374           }
3375         /* Look in the surrounding context.  */
3376         parser->scope = NULL_TREE;
3377         parser->object_scope = NULL_TREE;
3378         parser->qualifying_scope = NULL_TREE;
3379         type_decl 
3380           = cp_parser_class_name (parser, 
3381                                   /*typename_keyword_p=*/false,
3382                                   /*template_keyword_p=*/false,
3383                                   /*type_p=*/false,
3384                                   /*check_access_p=*/true,
3385                                   /*check_dependency=*/false,
3386                                   /*class_head_p=*/false);
3387         /* If an error occurred, assume that the name of the
3388            destructor is the same as the name of the qualifying
3389            class.  That allows us to keep parsing after running
3390            into ill-formed destructor names.  */
3391         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3392           return build_nt (BIT_NOT_EXPR, scope);
3393         else if (type_decl == error_mark_node)
3394           return error_mark_node;
3395
3396         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3397       }
3398
3399     case CPP_KEYWORD:
3400       if (token->keyword == RID_OPERATOR)
3401         {
3402           tree id;
3403
3404           /* This could be a template-id, so we try that first.  */
3405           cp_parser_parse_tentatively (parser);
3406           /* Try a template-id.  */
3407           id = cp_parser_template_id (parser, template_keyword_p,
3408                                       /*check_dependency_p=*/true);
3409           /* If that worked, we're done.  */
3410           if (cp_parser_parse_definitely (parser))
3411             return id;
3412           /* We still don't know whether we're looking at an
3413              operator-function-id or a conversion-function-id.  */
3414           cp_parser_parse_tentatively (parser);
3415           /* Try an operator-function-id.  */
3416           id = cp_parser_operator_function_id (parser);
3417           /* If that didn't work, try a conversion-function-id.  */
3418           if (!cp_parser_parse_definitely (parser))
3419             id = cp_parser_conversion_function_id (parser);
3420
3421           return id;
3422         }
3423       /* Fall through.  */
3424
3425     default:
3426       cp_parser_error (parser, "expected unqualified-id");
3427       return error_mark_node;
3428     }
3429 }
3430
3431 /* Parse an (optional) nested-name-specifier.
3432
3433    nested-name-specifier:
3434      class-or-namespace-name :: nested-name-specifier [opt]
3435      class-or-namespace-name :: template nested-name-specifier [opt]
3436
3437    PARSER->SCOPE should be set appropriately before this function is
3438    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3439    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3440    in name lookups.
3441
3442    Sets PARSER->SCOPE to the class (TYPE) or namespace
3443    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3444    it unchanged if there is no nested-name-specifier.  Returns the new
3445    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  */
3446
3447 static tree
3448 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3449                                      bool typename_keyword_p, 
3450                                      bool check_dependency_p,
3451                                      bool type_p)
3452 {
3453   bool success = false;
3454   tree access_check = NULL_TREE;
3455   ptrdiff_t start;
3456   cp_token* token;
3457
3458   /* If the next token corresponds to a nested name specifier, there
3459      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3460      false, it may have been true before, in which case something 
3461      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3462      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3463      CHECK_DEPENDENCY_P is false, we have to fall through into the
3464      main loop.  */
3465   if (check_dependency_p
3466       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3467     {
3468       cp_parser_pre_parsed_nested_name_specifier (parser);
3469       return parser->scope;
3470     }
3471
3472   /* Remember where the nested-name-specifier starts.  */
3473   if (cp_parser_parsing_tentatively (parser)
3474       && !cp_parser_committed_to_tentative_parse (parser))
3475     {
3476       token = cp_lexer_peek_token (parser->lexer);
3477       start = cp_lexer_token_difference (parser->lexer,
3478                                          parser->lexer->first_token,
3479                                          token);
3480       access_check = parser->context->deferred_access_checks;
3481     }
3482   else
3483     start = -1;
3484
3485   while (true)
3486     {
3487       tree new_scope;
3488       tree old_scope;
3489       tree saved_qualifying_scope;
3490       bool template_keyword_p;
3491
3492       /* Spot cases that cannot be the beginning of a
3493          nested-name-specifier.  */
3494       token = cp_lexer_peek_token (parser->lexer);
3495
3496       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3497          the already parsed nested-name-specifier.  */
3498       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3499         {
3500           /* Grab the nested-name-specifier and continue the loop.  */
3501           cp_parser_pre_parsed_nested_name_specifier (parser);
3502           success = true;
3503           continue;
3504         }
3505
3506       /* Spot cases that cannot be the beginning of a
3507          nested-name-specifier.  On the second and subsequent times
3508          through the loop, we look for the `template' keyword.  */
3509       if (success && token->keyword == RID_TEMPLATE)
3510         ;
3511       /* A template-id can start a nested-name-specifier.  */
3512       else if (token->type == CPP_TEMPLATE_ID)
3513         ;
3514       else
3515         {
3516           /* If the next token is not an identifier, then it is
3517              definitely not a class-or-namespace-name.  */
3518           if (token->type != CPP_NAME)
3519             break;
3520           /* If the following token is neither a `<' (to begin a
3521              template-id), nor a `::', then we are not looking at a
3522              nested-name-specifier.  */
3523           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3524           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3525             break;
3526         }
3527
3528       /* The nested-name-specifier is optional, so we parse
3529          tentatively.  */
3530       cp_parser_parse_tentatively (parser);
3531
3532       /* Look for the optional `template' keyword, if this isn't the
3533          first time through the loop.  */
3534       if (success)
3535         template_keyword_p = cp_parser_optional_template_keyword (parser);
3536       else
3537         template_keyword_p = false;
3538
3539       /* Save the old scope since the name lookup we are about to do
3540          might destroy it.  */
3541       old_scope = parser->scope;
3542       saved_qualifying_scope = parser->qualifying_scope;
3543       /* Parse the qualifying entity.  */
3544       new_scope 
3545         = cp_parser_class_or_namespace_name (parser,
3546                                              typename_keyword_p,
3547                                              template_keyword_p,
3548                                              check_dependency_p,
3549                                              type_p);
3550       /* Look for the `::' token.  */
3551       cp_parser_require (parser, CPP_SCOPE, "`::'");
3552
3553       /* If we found what we wanted, we keep going; otherwise, we're
3554          done.  */
3555       if (!cp_parser_parse_definitely (parser))
3556         {
3557           bool error_p = false;
3558
3559           /* Restore the OLD_SCOPE since it was valid before the
3560              failed attempt at finding the last
3561              class-or-namespace-name.  */
3562           parser->scope = old_scope;
3563           parser->qualifying_scope = saved_qualifying_scope;
3564           /* If the next token is an identifier, and the one after
3565              that is a `::', then any valid interpretation would have
3566              found a class-or-namespace-name.  */
3567           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3568                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3569                      == CPP_SCOPE)
3570                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3571                      != CPP_COMPL))
3572             {
3573               token = cp_lexer_consume_token (parser->lexer);
3574               if (!error_p) 
3575                 {
3576                   tree decl;
3577
3578                   decl = cp_parser_lookup_name_simple (parser, token->value);
3579                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3580                     error ("`%D' used without template parameters",
3581                            decl);
3582                   else if (parser->scope)
3583                     {
3584                       if (TYPE_P (parser->scope))
3585                         error ("`%T::%D' is not a class-name or "
3586                                "namespace-name",
3587                                parser->scope, token->value);
3588                       else
3589                         error ("`%D::%D' is not a class-name or "
3590                                "namespace-name",
3591                                parser->scope, token->value);
3592                     }
3593                   else
3594                     error ("`%D' is not a class-name or namespace-name",
3595                            token->value);
3596                   parser->scope = NULL_TREE;
3597                   error_p = true;
3598                   /* Treat this as a successful nested-name-specifier
3599                      due to:
3600
3601                      [basic.lookup.qual]
3602
3603                      If the name found is not a class-name (clause
3604                      _class_) or namespace-name (_namespace.def_), the
3605                      program is ill-formed.  */
3606                   success = true;
3607                 }
3608               cp_lexer_consume_token (parser->lexer);
3609             }
3610           break;
3611         }
3612
3613       /* We've found one valid nested-name-specifier.  */
3614       success = true;
3615       /* Make sure we look in the right scope the next time through
3616          the loop.  */
3617       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3618                        ? TREE_TYPE (new_scope)
3619                        : new_scope);
3620       /* If it is a class scope, try to complete it; we are about to
3621          be looking up names inside the class.  */
3622       if (TYPE_P (parser->scope))
3623         complete_type (parser->scope);
3624     }
3625
3626   /* If parsing tentatively, replace the sequence of tokens that makes
3627      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3628      token.  That way, should we re-parse the token stream, we will
3629      not have to repeat the effort required to do the parse, nor will
3630      we issue duplicate error messages.  */
3631   if (success && start >= 0)
3632     {
3633       tree c;
3634
3635       /* Find the token that corresponds to the start of the
3636          template-id.  */
3637       token = cp_lexer_advance_token (parser->lexer, 
3638                                       parser->lexer->first_token,
3639                                       start);
3640
3641       /* Remember the access checks associated with this
3642          nested-name-specifier.  */
3643       c = parser->context->deferred_access_checks;
3644       if (c == access_check)
3645         access_check = NULL_TREE;
3646       else
3647         {
3648           while (TREE_CHAIN (c) != access_check)
3649             c = TREE_CHAIN (c);
3650           access_check = parser->context->deferred_access_checks;
3651           parser->context->deferred_access_checks = TREE_CHAIN (c);
3652           TREE_CHAIN (c) = NULL_TREE;
3653         }
3654
3655       /* Reset the contents of the START token.  */
3656       token->type = CPP_NESTED_NAME_SPECIFIER;
3657       token->value = build_tree_list (access_check, parser->scope);
3658       TREE_TYPE (token->value) = parser->qualifying_scope;
3659       token->keyword = RID_MAX;
3660       /* Purge all subsequent tokens.  */
3661       cp_lexer_purge_tokens_after (parser->lexer, token);
3662     }
3663
3664   return success ? parser->scope : NULL_TREE;
3665 }
3666
3667 /* Parse a nested-name-specifier.  See
3668    cp_parser_nested_name_specifier_opt for details.  This function
3669    behaves identically, except that it will an issue an error if no
3670    nested-name-specifier is present, and it will return
3671    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3672    is present.  */
3673
3674 static tree
3675 cp_parser_nested_name_specifier (cp_parser *parser, 
3676                                  bool typename_keyword_p, 
3677                                  bool check_dependency_p,
3678                                  bool type_p)
3679 {
3680   tree scope;
3681
3682   /* Look for the nested-name-specifier.  */
3683   scope = cp_parser_nested_name_specifier_opt (parser,
3684                                                typename_keyword_p,
3685                                                check_dependency_p,
3686                                                type_p);
3687   /* If it was not present, issue an error message.  */
3688   if (!scope)
3689     {
3690       cp_parser_error (parser, "expected nested-name-specifier");
3691       return error_mark_node;
3692     }
3693
3694   return scope;
3695 }
3696
3697 /* Parse a class-or-namespace-name.
3698
3699    class-or-namespace-name:
3700      class-name
3701      namespace-name
3702
3703    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3704    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3705    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3706    TYPE_P is TRUE iff the next name should be taken as a class-name,
3707    even the same name is declared to be another entity in the same
3708    scope.
3709
3710    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3711    specified by the class-or-namespace-name.  If neither is found the
3712    ERROR_MARK_NODE is returned.  */
3713
3714 static tree
3715 cp_parser_class_or_namespace_name (cp_parser *parser, 
3716                                    bool typename_keyword_p,
3717                                    bool template_keyword_p,
3718                                    bool check_dependency_p,
3719                                    bool type_p)
3720 {
3721   tree saved_scope;
3722   tree saved_qualifying_scope;
3723   tree saved_object_scope;
3724   tree scope;
3725   bool only_class_p;
3726
3727   /* If the next token is the `template' keyword, we know that we are
3728      looking at a class-name.  */
3729   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3730     return cp_parser_class_name (parser, 
3731                                  typename_keyword_p,
3732                                  template_keyword_p,
3733                                  type_p,
3734                                  /*check_access_p=*/true,
3735                                  check_dependency_p,
3736                                  /*class_head_p=*/false);
3737   /* Before we try to parse the class-name, we must save away the
3738      current PARSER->SCOPE since cp_parser_class_name will destroy
3739      it.  */
3740   saved_scope = parser->scope;
3741   saved_qualifying_scope = parser->qualifying_scope;
3742   saved_object_scope = parser->object_scope;
3743   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3744      there is no need to look for a namespace-name.  */
3745   only_class_p = saved_scope && TYPE_P (saved_scope);
3746   if (!only_class_p)
3747     cp_parser_parse_tentatively (parser);
3748   scope = cp_parser_class_name (parser, 
3749                                 typename_keyword_p,
3750                                 template_keyword_p,
3751                                 type_p,
3752                                 /*check_access_p=*/true,
3753                                 check_dependency_p,
3754                                 /*class_head_p=*/false);
3755   /* If that didn't work, try for a namespace-name.  */
3756   if (!only_class_p && !cp_parser_parse_definitely (parser))
3757     {
3758       /* Restore the saved scope.  */
3759       parser->scope = saved_scope;
3760       parser->qualifying_scope = saved_qualifying_scope;
3761       parser->object_scope = saved_object_scope;
3762       /* If we are not looking at an identifier followed by the scope
3763          resolution operator, then this is not part of a
3764          nested-name-specifier.  (Note that this function is only used
3765          to parse the components of a nested-name-specifier.)  */
3766       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3767           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3768         return error_mark_node;
3769       scope = cp_parser_namespace_name (parser);
3770     }
3771
3772   return scope;
3773 }
3774
3775 /* Parse a postfix-expression.
3776
3777    postfix-expression:
3778      primary-expression
3779      postfix-expression [ expression ]
3780      postfix-expression ( expression-list [opt] )
3781      simple-type-specifier ( expression-list [opt] )
3782      typename :: [opt] nested-name-specifier identifier 
3783        ( expression-list [opt] )
3784      typename :: [opt] nested-name-specifier template [opt] template-id
3785        ( expression-list [opt] )
3786      postfix-expression . template [opt] id-expression
3787      postfix-expression -> template [opt] id-expression
3788      postfix-expression . pseudo-destructor-name
3789      postfix-expression -> pseudo-destructor-name
3790      postfix-expression ++
3791      postfix-expression --
3792      dynamic_cast < type-id > ( expression )
3793      static_cast < type-id > ( expression )
3794      reinterpret_cast < type-id > ( expression )
3795      const_cast < type-id > ( expression )
3796      typeid ( expression )
3797      typeid ( type-id )
3798
3799    GNU Extension:
3800      
3801    postfix-expression:
3802      ( type-id ) { initializer-list , [opt] }
3803
3804    This extension is a GNU version of the C99 compound-literal
3805    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3806    but they are essentially the same concept.)
3807
3808    If ADDRESS_P is true, the postfix expression is the operand of the
3809    `&' operator.
3810
3811    Returns a representation of the expression.  */
3812
3813 static tree
3814 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3815 {
3816   cp_token *token;
3817   enum rid keyword;
3818   cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3819   tree postfix_expression = NULL_TREE;
3820   /* Non-NULL only if the current postfix-expression can be used to
3821      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3822      class used to qualify the member.  */
3823   tree qualifying_class = NULL_TREE;
3824   bool done;
3825
3826   /* Peek at the next token.  */
3827   token = cp_lexer_peek_token (parser->lexer);
3828   /* Some of the productions are determined by keywords.  */
3829   keyword = token->keyword;
3830   switch (keyword)
3831     {
3832     case RID_DYNCAST:
3833     case RID_STATCAST:
3834     case RID_REINTCAST:
3835     case RID_CONSTCAST:
3836       {
3837         tree type;
3838         tree expression;
3839         const char *saved_message;
3840
3841         /* All of these can be handled in the same way from the point
3842            of view of parsing.  Begin by consuming the token
3843            identifying the cast.  */
3844         cp_lexer_consume_token (parser->lexer);
3845         
3846         /* New types cannot be defined in the cast.  */
3847         saved_message = parser->type_definition_forbidden_message;
3848         parser->type_definition_forbidden_message
3849           = "types may not be defined in casts";
3850
3851         /* Look for the opening `<'.  */
3852         cp_parser_require (parser, CPP_LESS, "`<'");
3853         /* Parse the type to which we are casting.  */
3854         type = cp_parser_type_id (parser);
3855         /* Look for the closing `>'.  */
3856         cp_parser_require (parser, CPP_GREATER, "`>'");
3857         /* Restore the old message.  */
3858         parser->type_definition_forbidden_message = saved_message;
3859
3860         /* And the expression which is being cast.  */
3861         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862         expression = cp_parser_expression (parser);
3863         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3864
3865         switch (keyword)
3866           {
3867           case RID_DYNCAST:
3868             postfix_expression
3869               = build_dynamic_cast (type, expression);
3870             break;
3871           case RID_STATCAST:
3872             postfix_expression
3873               = build_static_cast (type, expression);
3874             break;
3875           case RID_REINTCAST:
3876             postfix_expression
3877               = build_reinterpret_cast (type, expression);
3878             break;
3879           case RID_CONSTCAST:
3880             postfix_expression
3881               = build_const_cast (type, expression);
3882             break;
3883           default:
3884             abort ();
3885           }
3886       }
3887       break;
3888
3889     case RID_TYPEID:
3890       {
3891         tree type;
3892         const char *saved_message;
3893
3894         /* Consume the `typeid' token.  */
3895         cp_lexer_consume_token (parser->lexer);
3896         /* Look for the `(' token.  */
3897         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3898         /* Types cannot be defined in a `typeid' expression.  */
3899         saved_message = parser->type_definition_forbidden_message;
3900         parser->type_definition_forbidden_message
3901           = "types may not be defined in a `typeid\' expression";
3902         /* We can't be sure yet whether we're looking at a type-id or an
3903            expression.  */
3904         cp_parser_parse_tentatively (parser);
3905         /* Try a type-id first.  */
3906         type = cp_parser_type_id (parser);
3907         /* Look for the `)' token.  Otherwise, we can't be sure that
3908            we're not looking at an expression: consider `typeid (int
3909            (3))', for example.  */
3910         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3911         /* If all went well, simply lookup the type-id.  */
3912         if (cp_parser_parse_definitely (parser))
3913           postfix_expression = get_typeid (type);
3914         /* Otherwise, fall back to the expression variant.  */
3915         else
3916           {
3917             tree expression;
3918
3919             /* Look for an expression.  */
3920             expression = cp_parser_expression (parser);
3921             /* Compute its typeid.  */
3922             postfix_expression = build_typeid (expression);
3923             /* Look for the `)' token.  */
3924             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3925           }
3926
3927         /* Restore the saved message.  */
3928         parser->type_definition_forbidden_message = saved_message;
3929       }
3930       break;
3931       
3932     case RID_TYPENAME:
3933       {
3934         bool template_p = false;
3935         tree id;
3936         tree type;
3937
3938         /* Consume the `typename' token.  */
3939         cp_lexer_consume_token (parser->lexer);
3940         /* Look for the optional `::' operator.  */
3941         cp_parser_global_scope_opt (parser, 
3942                                     /*current_scope_valid_p=*/false);
3943         /* Look for the nested-name-specifier.  */
3944         cp_parser_nested_name_specifier (parser,
3945                                          /*typename_keyword_p=*/true,
3946                                          /*check_dependency_p=*/true,
3947                                          /*type_p=*/true);
3948         /* Look for the optional `template' keyword.  */
3949         template_p = cp_parser_optional_template_keyword (parser);
3950         /* We don't know whether we're looking at a template-id or an
3951            identifier.  */
3952         cp_parser_parse_tentatively (parser);
3953         /* Try a template-id.  */
3954         id = cp_parser_template_id (parser, template_p,
3955                                     /*check_dependency_p=*/true);
3956         /* If that didn't work, try an identifier.  */
3957         if (!cp_parser_parse_definitely (parser))
3958           id = cp_parser_identifier (parser);
3959         /* Create a TYPENAME_TYPE to represent the type to which the
3960            functional cast is being performed.  */
3961         type = make_typename_type (parser->scope, id, 
3962                                    /*complain=*/1);
3963
3964         postfix_expression = cp_parser_functional_cast (parser, type);
3965       }
3966       break;
3967
3968     default:
3969       {
3970         tree type;
3971
3972         /* If the next thing is a simple-type-specifier, we may be
3973            looking at a functional cast.  We could also be looking at
3974            an id-expression.  So, we try the functional cast, and if
3975            that doesn't work we fall back to the primary-expression.  */
3976         cp_parser_parse_tentatively (parser);
3977         /* Look for the simple-type-specifier.  */
3978         type = cp_parser_simple_type_specifier (parser, 
3979                                                 CP_PARSER_FLAGS_NONE);
3980         /* Parse the cast itself.  */
3981         if (!cp_parser_error_occurred (parser))
3982           postfix_expression 
3983             = cp_parser_functional_cast (parser, type);
3984         /* If that worked, we're done.  */
3985         if (cp_parser_parse_definitely (parser))
3986           break;
3987
3988         /* If the functional-cast didn't work out, try a
3989            compound-literal.  */
3990         if (cp_parser_allow_gnu_extensions_p (parser))
3991           {
3992             tree initializer_list = NULL_TREE;
3993
3994             cp_parser_parse_tentatively (parser);
3995             /* Look for the `('.  */
3996             if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3997               {
3998                 type = cp_parser_type_id (parser);
3999                 /* Look for the `)'.  */
4000                 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4001                 /* Look for the `{'.  */
4002                 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4003                 /* If things aren't going well, there's no need to
4004                    keep going.  */
4005                 if (!cp_parser_error_occurred (parser))
4006                   {
4007                     /* Parse the initializer-list.  */
4008                     initializer_list 
4009                       = cp_parser_initializer_list (parser);
4010                     /* Allow a trailing `,'.  */
4011                     if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4012                       cp_lexer_consume_token (parser->lexer);
4013                     /* Look for the final `}'.  */
4014                     cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4015                   }
4016               }
4017             /* If that worked, we're definitely looking at a
4018                compound-literal expression.  */
4019             if (cp_parser_parse_definitely (parser))
4020               {
4021                 /* Warn the user that a compound literal is not
4022                    allowed in standard C++.  */
4023                 if (pedantic)
4024                   pedwarn ("ISO C++ forbids compound-literals");
4025                 /* Form the representation of the compound-literal.  */
4026                 postfix_expression 
4027                   = finish_compound_literal (type, initializer_list);
4028                 break;
4029               }
4030           }
4031
4032         /* It must be a primary-expression.  */
4033         postfix_expression = cp_parser_primary_expression (parser, 
4034                                                            &idk,
4035                                                            &qualifying_class);
4036       }
4037       break;
4038     }
4039
4040   /* Peek at the next token.  */
4041   token = cp_lexer_peek_token (parser->lexer);
4042   done = (token->type != CPP_OPEN_SQUARE
4043           && token->type != CPP_OPEN_PAREN
4044           && token->type != CPP_DOT
4045           && token->type != CPP_DEREF
4046           && token->type != CPP_PLUS_PLUS
4047           && token->type != CPP_MINUS_MINUS);
4048
4049   /* If the postfix expression is complete, finish up.  */
4050   if (address_p && qualifying_class && done)
4051     {
4052       if (TREE_CODE (postfix_expression) == SCOPE_REF)
4053         postfix_expression = TREE_OPERAND (postfix_expression, 1);
4054       postfix_expression 
4055         = build_offset_ref (qualifying_class, postfix_expression);
4056       return postfix_expression;
4057     }
4058
4059   /* Otherwise, if we were avoiding committing until we knew
4060      whether or not we had a pointer-to-member, we now know that
4061      the expression is an ordinary reference to a qualified name.  */
4062   if (qualifying_class && !processing_template_decl)
4063     {
4064       if (TREE_CODE (postfix_expression) == FIELD_DECL)
4065         postfix_expression 
4066           = finish_non_static_data_member (postfix_expression,
4067                                            qualifying_class);
4068       else if (BASELINK_P (postfix_expression))
4069         {
4070           tree fn;
4071           tree fns;
4072
4073           /* See if any of the functions are non-static members.  */
4074           fns = BASELINK_FUNCTIONS (postfix_expression);
4075           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4076             fns = TREE_OPERAND (fns, 0);
4077           for (fn = fns; fn; fn = OVL_NEXT (fn))
4078             if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4079               break;
4080           /* If so, the expression may be relative to the current
4081              class.  */
4082           if (fn && current_class_type 
4083               && DERIVED_FROM_P (qualifying_class, current_class_type))
4084             postfix_expression 
4085               = (build_class_member_access_expr 
4086                  (maybe_dummy_object (qualifying_class, NULL),
4087                   postfix_expression,
4088                   BASELINK_ACCESS_BINFO (postfix_expression),
4089                   /*preserve_reference=*/false));
4090           else if (done)
4091             return build_offset_ref (qualifying_class,
4092                                      postfix_expression);
4093         }
4094     }
4095
4096   /* Remember that there was a reference to this entity.  */
4097   if (DECL_P (postfix_expression))
4098     mark_used (postfix_expression);
4099
4100   /* Keep looping until the postfix-expression is complete.  */
4101   while (true)
4102     {
4103       if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4104           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4105         {
4106           /* It is not a Koenig lookup function call.  */
4107           unqualified_name_lookup_error (postfix_expression);
4108           postfix_expression = error_mark_node;
4109         }
4110       
4111       /* Peek at the next token.  */
4112       token = cp_lexer_peek_token (parser->lexer);
4113
4114       switch (token->type)
4115         {
4116         case CPP_OPEN_SQUARE:
4117           /* postfix-expression [ expression ] */
4118           {
4119             tree index;
4120
4121             /* Consume the `[' token.  */
4122             cp_lexer_consume_token (parser->lexer);
4123             /* Parse the index expression.  */
4124             index = cp_parser_expression (parser);
4125             /* Look for the closing `]'.  */
4126             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4127
4128             /* Build the ARRAY_REF.  */
4129             postfix_expression 
4130               = grok_array_decl (postfix_expression, index);
4131             idk = CP_PARSER_ID_KIND_NONE;
4132           }
4133           break;
4134
4135         case CPP_OPEN_PAREN:
4136           /* postfix-expression ( expression-list [opt] ) */
4137           {
4138             tree args;
4139
4140             /* Consume the `(' token.  */
4141             cp_lexer_consume_token (parser->lexer);
4142             /* If the next token is not a `)', then there are some
4143                arguments.  */
4144             if (cp_lexer_next_token_is_not (parser->lexer, 
4145                                             CPP_CLOSE_PAREN))
4146               args = cp_parser_expression_list (parser);
4147             else
4148               args = NULL_TREE;
4149             /* Look for the closing `)'.  */
4150             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4151
4152             if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4153                 && (is_overloaded_fn (postfix_expression)
4154                     || DECL_P (postfix_expression)
4155                     || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4156                 && args)
4157               {
4158                 tree arg;
4159                 tree identifier = NULL_TREE;
4160                 tree functions = NULL_TREE;
4161
4162                 /* Find the name of the overloaded function.  */
4163                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4164                   identifier = postfix_expression;
4165                 else if (is_overloaded_fn (postfix_expression))
4166                   {
4167                     functions = postfix_expression;
4168                     identifier = DECL_NAME (get_first_fn (functions));
4169                   }
4170                 else if (DECL_P (postfix_expression))
4171                   {
4172                     functions = postfix_expression;
4173                     identifier = DECL_NAME (postfix_expression);
4174                   }
4175
4176                 /* A call to a namespace-scope function using an
4177                    unqualified name.
4178
4179                    Do Koenig lookup -- unless any of the arguments are
4180                    type-dependent.  */
4181                 for (arg = args; arg; arg = TREE_CHAIN (arg))
4182                   if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4183                       break;
4184                 if (!arg)
4185                   {
4186                     postfix_expression 
4187                       = lookup_arg_dependent(identifier, functions, args);
4188                     if (!postfix_expression)
4189                       {
4190                         /* The unqualified name could not be resolved.  */
4191                         unqualified_name_lookup_error (identifier);
4192                         postfix_expression = error_mark_node;
4193                       }
4194                     postfix_expression
4195                       = build_call_from_tree (postfix_expression, args, 
4196                                               /*diallow_virtual=*/false);
4197                     break;
4198                   }
4199                 postfix_expression = build_min_nt (LOOKUP_EXPR,
4200                                                    identifier);
4201               }
4202             else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED 
4203                      && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4204               {
4205                 /* The unqualified name could not be resolved.  */
4206                 unqualified_name_lookup_error (postfix_expression);
4207                 postfix_expression = error_mark_node;
4208                 break;
4209               }
4210
4211             /* In the body of a template, no further processing is
4212                required.  */
4213             if (processing_template_decl)
4214               {
4215                 postfix_expression = build_nt (CALL_EXPR,
4216                                                postfix_expression, 
4217                                                args);
4218                 break;
4219               }
4220
4221             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4222               postfix_expression
4223                 = (build_new_method_call 
4224                    (TREE_OPERAND (postfix_expression, 0),
4225                     TREE_OPERAND (postfix_expression, 1),
4226                     args, NULL_TREE, 
4227                     (idk == CP_PARSER_ID_KIND_QUALIFIED 
4228                      ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4229             else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4230               postfix_expression = (build_offset_ref_call_from_tree
4231                                     (postfix_expression, args));
4232             else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4233               /* A call to a static class member, or a namespace-scope
4234                  function.  */
4235               postfix_expression
4236                 = finish_call_expr (postfix_expression, args,
4237                                     /*disallow_virtual=*/true);
4238             else
4239               /* All other function calls.  */
4240               postfix_expression 
4241                 = finish_call_expr (postfix_expression, args, 
4242                                     /*disallow_virtual=*/false);
4243
4244             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4245             idk = CP_PARSER_ID_KIND_NONE;
4246           }
4247           break;
4248           
4249         case CPP_DOT:
4250         case CPP_DEREF:
4251           /* postfix-expression . template [opt] id-expression  
4252              postfix-expression . pseudo-destructor-name 
4253              postfix-expression -> template [opt] id-expression
4254              postfix-expression -> pseudo-destructor-name */
4255           {
4256             tree name;
4257             bool dependent_p;
4258             bool template_p;
4259             tree scope = NULL_TREE;
4260
4261             /* If this is a `->' operator, dereference the pointer.  */
4262             if (token->type == CPP_DEREF)
4263               postfix_expression = build_x_arrow (postfix_expression);
4264             /* Check to see whether or not the expression is
4265                type-dependent.  */
4266             dependent_p = (cp_parser_type_dependent_expression_p 
4267                            (postfix_expression));
4268             /* The identifier following the `->' or `.' is not
4269                qualified.  */
4270             parser->scope = NULL_TREE;
4271             parser->qualifying_scope = NULL_TREE;
4272             parser->object_scope = NULL_TREE;
4273             /* Enter the scope corresponding to the type of the object
4274                given by the POSTFIX_EXPRESSION.  */
4275             if (!dependent_p 
4276                 && TREE_TYPE (postfix_expression) != NULL_TREE)
4277               {
4278                 scope = TREE_TYPE (postfix_expression);
4279                 /* According to the standard, no expression should
4280                    ever have reference type.  Unfortunately, we do not
4281                    currently match the standard in this respect in
4282                    that our internal representation of an expression
4283                    may have reference type even when the standard says
4284                    it does not.  Therefore, we have to manually obtain
4285                    the underlying type here.  */
4286                 if (TREE_CODE (scope) == REFERENCE_TYPE)
4287                   scope = TREE_TYPE (scope);
4288                 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4289                    type of the field.  We get an OFFSET_TYPE for
4290                    something like:
4291
4292                      S::T.a ...
4293
4294                    Probably, we should not get an OFFSET_TYPE here;
4295                    that transformation should be made only if `&S::T'
4296                    is written.  */
4297                 if (TREE_CODE (scope) == OFFSET_TYPE)
4298                   scope = TREE_TYPE (scope);
4299                 /* The type of the POSTFIX_EXPRESSION must be
4300                    complete.  */
4301                 scope = complete_type_or_else (scope, NULL_TREE);
4302                 /* Let the name lookup machinery know that we are
4303                    processing a class member access expression.  */
4304                 parser->context->object_type = scope;
4305                 /* If something went wrong, we want to be able to
4306                    discern that case, as opposed to the case where
4307                    there was no SCOPE due to the type of expression
4308                    being dependent.  */
4309                 if (!scope)
4310                   scope = error_mark_node;
4311               }
4312
4313             /* Consume the `.' or `->' operator.  */
4314             cp_lexer_consume_token (parser->lexer);
4315             /* If the SCOPE is not a scalar type, we are looking at an
4316                ordinary class member access expression, rather than a
4317                pseudo-destructor-name.  */
4318             if (!scope || !SCALAR_TYPE_P (scope))
4319               {
4320                 template_p = cp_parser_optional_template_keyword (parser);
4321                 /* Parse the id-expression.  */
4322                 name = cp_parser_id_expression (parser,
4323                                                 template_p,
4324                                                 /*check_dependency_p=*/true,
4325                                                 /*template_p=*/NULL);
4326                 /* In general, build a SCOPE_REF if the member name is
4327                    qualified.  However, if the name was not dependent
4328                    and has already been resolved; there is no need to
4329                    build the SCOPE_REF.  For example;
4330
4331                      struct X { void f(); };
4332                      template <typename T> void f(T* t) { t->X::f(); }
4333  
4334                    Even though "t" is dependent, "X::f" is not and has 
4335                    except that for a BASELINK there is no need to
4336                    include scope information.  */
4337                 if (name != error_mark_node 
4338                     && !BASELINK_P (name)
4339                     && parser->scope)
4340                   {
4341                     name = build_nt (SCOPE_REF, parser->scope, name);
4342                     parser->scope = NULL_TREE;
4343                     parser->qualifying_scope = NULL_TREE;
4344                     parser->object_scope = NULL_TREE;
4345                   }
4346                 postfix_expression 
4347                   = finish_class_member_access_expr (postfix_expression, name);
4348               }
4349             /* Otherwise, try the pseudo-destructor-name production.  */
4350             else
4351               {
4352                 tree s;
4353                 tree type;
4354
4355                 /* Parse the pseudo-destructor-name.  */
4356                 cp_parser_pseudo_destructor_name (parser, &s, &type);
4357                 /* Form the call.  */
4358                 postfix_expression 
4359                   = finish_pseudo_destructor_expr (postfix_expression,
4360                                                    s, TREE_TYPE (type));
4361               }
4362
4363             /* We no longer need to look up names in the scope of the
4364                object on the left-hand side of the `.' or `->'
4365                operator.  */
4366             parser->context->object_type = NULL_TREE;
4367             idk = CP_PARSER_ID_KIND_NONE;
4368           }
4369           break;
4370
4371         case CPP_PLUS_PLUS:
4372           /* postfix-expression ++  */
4373           /* Consume the `++' token.  */
4374           cp_lexer_consume_token (parser->lexer);
4375           /* Generate a reprsentation for the complete expression.  */
4376           postfix_expression 
4377             = finish_increment_expr (postfix_expression, 
4378                                      POSTINCREMENT_EXPR);
4379           idk = CP_PARSER_ID_KIND_NONE;
4380           break;
4381
4382         case CPP_MINUS_MINUS:
4383           /* postfix-expression -- */
4384           /* Consume the `--' token.  */
4385           cp_lexer_consume_token (parser->lexer);
4386           /* Generate a reprsentation for the complete expression.  */
4387           postfix_expression 
4388             = finish_increment_expr (postfix_expression, 
4389                                      POSTDECREMENT_EXPR);
4390           idk = CP_PARSER_ID_KIND_NONE;
4391           break;
4392
4393         default:
4394           return postfix_expression;
4395         }
4396     }
4397
4398   /* We should never get here.  */
4399   abort ();
4400   return error_mark_node;
4401 }
4402
4403 /* Parse an expression-list.
4404
4405    expression-list:
4406      assignment-expression
4407      expression-list, assignment-expression
4408
4409    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4410    representation of an assignment-expression.  Note that a TREE_LIST
4411    is returned even if there is only a single expression in the list.  */
4412
4413 static tree
4414 cp_parser_expression_list (parser)
4415      cp_parser *parser;
4416 {
4417   tree expression_list = NULL_TREE;
4418
4419   /* Consume expressions until there are no more.  */
4420   while (true)
4421     {
4422       tree expr;
4423
4424       /* Parse the next assignment-expression.  */
4425       expr = cp_parser_assignment_expression (parser);
4426       /* Add it to the list.  */
4427       expression_list = tree_cons (NULL_TREE, expr, expression_list);
4428
4429       /* If the next token isn't a `,', then we are done.  */
4430       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4431         {
4432           /* All uses of expression-list in the grammar are followed
4433              by a `)'.  Therefore, if the next token is not a `)' an
4434              error will be issued, unless we are parsing tentatively.
4435              Skip ahead to see if there is another `,' before the `)';
4436              if so, we can go there and recover.  */
4437           if (cp_parser_parsing_tentatively (parser)
4438               || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4439               || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4440             break;
4441         }
4442
4443       /* Otherwise, consume the `,' and keep going.  */
4444       cp_lexer_consume_token (parser->lexer);
4445     }
4446
4447   /* We built up the list in reverse order so we must reverse it now.  */
4448   return nreverse (expression_list);
4449 }
4450
4451 /* Parse a pseudo-destructor-name.
4452
4453    pseudo-destructor-name:
4454      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4455      :: [opt] nested-name-specifier template template-id :: ~ type-name
4456      :: [opt] nested-name-specifier [opt] ~ type-name
4457
4458    If either of the first two productions is used, sets *SCOPE to the
4459    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4460    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4461    or ERROR_MARK_NODE if no type-name is present.  */
4462
4463 static void
4464 cp_parser_pseudo_destructor_name (parser, scope, type)
4465      cp_parser *parser;
4466      tree *scope;
4467      tree *type;
4468 {
4469   bool nested_name_specifier_p;
4470
4471   /* Look for the optional `::' operator.  */
4472   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4473   /* Look for the optional nested-name-specifier.  */
4474   nested_name_specifier_p 
4475     = (cp_parser_nested_name_specifier_opt (parser,
4476                                             /*typename_keyword_p=*/false,
4477                                             /*check_dependency_p=*/true,
4478                                             /*type_p=*/false) 
4479        != NULL_TREE);
4480   /* Now, if we saw a nested-name-specifier, we might be doing the
4481      second production.  */
4482   if (nested_name_specifier_p 
4483       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4484     {
4485       /* Consume the `template' keyword.  */
4486       cp_lexer_consume_token (parser->lexer);
4487       /* Parse the template-id.  */
4488       cp_parser_template_id (parser, 
4489                              /*template_keyword_p=*/true,
4490                              /*check_dependency_p=*/false);
4491       /* Look for the `::' token.  */
4492       cp_parser_require (parser, CPP_SCOPE, "`::'");
4493     }
4494   /* If the next token is not a `~', then there might be some
4495      additional qualification. */
4496   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4497     {
4498       /* Look for the type-name.  */
4499       *scope = TREE_TYPE (cp_parser_type_name (parser));
4500       /* Look for the `::' token.  */
4501       cp_parser_require (parser, CPP_SCOPE, "`::'");
4502     }
4503   else
4504     *scope = NULL_TREE;
4505
4506   /* Look for the `~'.  */
4507   cp_parser_require (parser, CPP_COMPL, "`~'");
4508   /* Look for the type-name again.  We are not responsible for
4509      checking that it matches the first type-name.  */
4510   *type = cp_parser_type_name (parser);
4511 }
4512
4513 /* Parse a unary-expression.
4514
4515    unary-expression:
4516      postfix-expression
4517      ++ cast-expression
4518      -- cast-expression
4519      unary-operator cast-expression
4520      sizeof unary-expression
4521      sizeof ( type-id )
4522      new-expression
4523      delete-expression
4524
4525    GNU Extensions:
4526
4527    unary-expression:
4528      __extension__ cast-expression
4529      __alignof__ unary-expression
4530      __alignof__ ( type-id )
4531      __real__ cast-expression
4532      __imag__ cast-expression
4533      && identifier
4534
4535    ADDRESS_P is true iff the unary-expression is appearing as the
4536    operand of the `&' operator.
4537
4538    Returns a representation of the expresion.  */
4539
4540 static tree
4541 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4542 {
4543   cp_token *token;
4544   enum tree_code unary_operator;
4545
4546   /* Peek at the next token.  */
4547   token = cp_lexer_peek_token (parser->lexer);
4548   /* Some keywords give away the kind of expression.  */
4549   if (token->type == CPP_KEYWORD)
4550     {
4551       enum rid keyword = token->keyword;
4552
4553       switch (keyword)
4554         {
4555         case RID_ALIGNOF:
4556           {
4557             /* Consume the `alignof' token.  */
4558             cp_lexer_consume_token (parser->lexer);
4559             /* Parse the operand.  */
4560             return finish_alignof (cp_parser_sizeof_operand 
4561                                    (parser, keyword));
4562           }
4563
4564         case RID_SIZEOF:
4565           {
4566             tree operand;
4567             
4568             /* Consume the `sizeof' token.   */
4569             cp_lexer_consume_token (parser->lexer);
4570             /* Parse the operand.  */
4571             operand = cp_parser_sizeof_operand (parser, keyword);
4572
4573             /* If the type of the operand cannot be determined build a
4574                SIZEOF_EXPR.  */
4575             if (TYPE_P (operand)
4576                 ? cp_parser_dependent_type_p (operand)
4577                 : cp_parser_type_dependent_expression_p (operand))
4578               return build_min (SIZEOF_EXPR, size_type_node, operand);
4579             /* Otherwise, compute the constant value.  */
4580             else
4581               return finish_sizeof (operand);
4582           }
4583
4584         case RID_NEW:
4585           return cp_parser_new_expression (parser);
4586
4587         case RID_DELETE:
4588           return cp_parser_delete_expression (parser);
4589           
4590         case RID_EXTENSION:
4591           {
4592             /* The saved value of the PEDANTIC flag.  */
4593             int saved_pedantic;
4594             tree expr;
4595
4596             /* Save away the PEDANTIC flag.  */
4597             cp_parser_extension_opt (parser, &saved_pedantic);
4598             /* Parse the cast-expression.  */
4599             expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4600             /* Restore the PEDANTIC flag.  */
4601             pedantic = saved_pedantic;
4602
4603             return expr;
4604           }
4605
4606         case RID_REALPART:
4607         case RID_IMAGPART:
4608           {
4609             tree expression;
4610
4611             /* Consume the `__real__' or `__imag__' token.  */
4612             cp_lexer_consume_token (parser->lexer);
4613             /* Parse the cast-expression.  */
4614             expression = cp_parser_cast_expression (parser,
4615                                                     /*address_p=*/false);
4616             /* Create the complete representation.  */
4617             return build_x_unary_op ((keyword == RID_REALPART
4618                                       ? REALPART_EXPR : IMAGPART_EXPR),
4619                                      expression);
4620           }
4621           break;
4622
4623         default:
4624           break;
4625         }
4626     }
4627
4628   /* Look for the `:: new' and `:: delete', which also signal the
4629      beginning of a new-expression, or delete-expression,
4630      respectively.  If the next token is `::', then it might be one of
4631      these.  */
4632   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4633     {
4634       enum rid keyword;
4635
4636       /* See if the token after the `::' is one of the keywords in
4637          which we're interested.  */
4638       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4639       /* If it's `new', we have a new-expression.  */
4640       if (keyword == RID_NEW)
4641         return cp_parser_new_expression (parser);
4642       /* Similarly, for `delete'.  */
4643       else if (keyword == RID_DELETE)
4644         return cp_parser_delete_expression (parser);
4645     }
4646
4647   /* Look for a unary operator.  */
4648   unary_operator = cp_parser_unary_operator (token);
4649   /* The `++' and `--' operators can be handled similarly, even though
4650      they are not technically unary-operators in the grammar.  */
4651   if (unary_operator == ERROR_MARK)
4652     {
4653       if (token->type == CPP_PLUS_PLUS)
4654         unary_operator = PREINCREMENT_EXPR;
4655       else if (token->type == CPP_MINUS_MINUS)
4656         unary_operator = PREDECREMENT_EXPR;
4657       /* Handle the GNU address-of-label extension.  */
4658       else if (cp_parser_allow_gnu_extensions_p (parser)
4659                && token->type == CPP_AND_AND)
4660         {
4661           tree identifier;
4662
4663           /* Consume the '&&' token.  */
4664           cp_lexer_consume_token (parser->lexer);
4665           /* Look for the identifier.  */
4666           identifier = cp_parser_identifier (parser);
4667           /* Create an expression representing the address.  */
4668           return finish_label_address_expr (identifier);
4669         }
4670     }
4671   if (unary_operator != ERROR_MARK)
4672     {
4673       tree cast_expression;
4674
4675       /* Consume the operator token.  */
4676       token = cp_lexer_consume_token (parser->lexer);
4677       /* Parse the cast-expression.  */
4678       cast_expression 
4679         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4680       /* Now, build an appropriate representation.  */
4681       switch (unary_operator)
4682         {
4683         case INDIRECT_REF:
4684           return build_x_indirect_ref (cast_expression, "unary *");
4685           
4686         case ADDR_EXPR:
4687           return build_x_unary_op (ADDR_EXPR, cast_expression);
4688           
4689         case CONVERT_EXPR:
4690         case NEGATE_EXPR:
4691         case TRUTH_NOT_EXPR:
4692         case PREINCREMENT_EXPR:
4693         case PREDECREMENT_EXPR:
4694           return finish_unary_op_expr (unary_operator, cast_expression);
4695
4696         case BIT_NOT_EXPR:
4697           return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4698
4699         default:
4700           abort ();
4701           return error_mark_node;
4702         }
4703     }
4704
4705   return cp_parser_postfix_expression (parser, address_p);
4706 }
4707
4708 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4709    unary-operator, the corresponding tree code is returned.  */
4710
4711 static enum tree_code
4712 cp_parser_unary_operator (token)
4713      cp_token *token;
4714 {
4715   switch (token->type)
4716     {
4717     case CPP_MULT:
4718       return INDIRECT_REF;
4719
4720     case CPP_AND:
4721       return ADDR_EXPR;
4722
4723     case CPP_PLUS:
4724       return CONVERT_EXPR;
4725
4726     case CPP_MINUS:
4727       return NEGATE_EXPR;
4728
4729     case CPP_NOT:
4730       return TRUTH_NOT_EXPR;
4731       
4732     case CPP_COMPL:
4733       return BIT_NOT_EXPR;
4734
4735     default:
4736       return ERROR_MARK;
4737     }
4738 }
4739
4740 /* Parse a new-expression.
4741
4742      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4743      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4744
4745    Returns a representation of the expression.  */
4746
4747 static tree
4748 cp_parser_new_expression (parser)
4749      cp_parser *parser;
4750 {
4751   bool global_scope_p;
4752   tree placement;
4753   tree type;
4754   tree initializer;
4755
4756   /* Look for the optional `::' operator.  */
4757   global_scope_p 
4758     = (cp_parser_global_scope_opt (parser,
4759                                    /*current_scope_valid_p=*/false)
4760        != NULL_TREE);
4761   /* Look for the `new' operator.  */
4762   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4763   /* There's no easy way to tell a new-placement from the
4764      `( type-id )' construct.  */
4765   cp_parser_parse_tentatively (parser);
4766   /* Look for a new-placement.  */
4767   placement = cp_parser_new_placement (parser);
4768   /* If that didn't work out, there's no new-placement.  */
4769   if (!cp_parser_parse_definitely (parser))
4770     placement = NULL_TREE;
4771
4772   /* If the next token is a `(', then we have a parenthesized
4773      type-id.  */
4774   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4775     {
4776       /* Consume the `('.  */
4777       cp_lexer_consume_token (parser->lexer);
4778       /* Parse the type-id.  */
4779       type = cp_parser_type_id (parser);
4780       /* Look for the closing `)'.  */
4781       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4782     }
4783   /* Otherwise, there must be a new-type-id.  */
4784   else
4785     type = cp_parser_new_type_id (parser);
4786
4787   /* If the next token is a `(', then we have a new-initializer.  */
4788   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4789     initializer = cp_parser_new_initializer (parser);
4790   else
4791     initializer = NULL_TREE;
4792
4793   /* Create a representation of the new-expression.  */
4794   return build_new (placement, type, initializer, global_scope_p);
4795 }
4796
4797 /* Parse a new-placement.
4798
4799    new-placement:
4800      ( expression-list )
4801
4802    Returns the same representation as for an expression-list.  */
4803
4804 static tree
4805 cp_parser_new_placement (parser)
4806      cp_parser *parser;
4807 {
4808   tree expression_list;
4809
4810   /* Look for the opening `('.  */
4811   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4812     return error_mark_node;
4813   /* Parse the expression-list.  */
4814   expression_list = cp_parser_expression_list (parser);
4815   /* Look for the closing `)'.  */
4816   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4817
4818   return expression_list;
4819 }
4820
4821 /* Parse a new-type-id.
4822
4823    new-type-id:
4824      type-specifier-seq new-declarator [opt]
4825
4826    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4827    and whose TREE_VALUE is the new-declarator.  */
4828
4829 static tree
4830 cp_parser_new_type_id (parser)
4831      cp_parser *parser;
4832 {
4833   tree type_specifier_seq;
4834   tree declarator;
4835   const char *saved_message;
4836
4837   /* The type-specifier sequence must not contain type definitions.
4838      (It cannot contain declarations of new types either, but if they
4839      are not definitions we will catch that because they are not
4840      complete.)  */
4841   saved_message = parser->type_definition_forbidden_message;
4842   parser->type_definition_forbidden_message
4843     = "types may not be defined in a new-type-id";
4844   /* Parse the type-specifier-seq.  */
4845   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4846   /* Restore the old message.  */
4847   parser->type_definition_forbidden_message = saved_message;
4848   /* Parse the new-declarator.  */
4849   declarator = cp_parser_new_declarator_opt (parser);
4850
4851   return build_tree_list (type_specifier_seq, declarator);
4852 }
4853
4854 /* Parse an (optional) new-declarator.
4855
4856    new-declarator:
4857      ptr-operator new-declarator [opt]
4858      direct-new-declarator
4859
4860    Returns a representation of the declarator.  See
4861    cp_parser_declarator for the representations used.  */
4862
4863 static tree
4864 cp_parser_new_declarator_opt (parser)
4865      cp_parser *parser;
4866 {
4867   enum tree_code code;
4868   tree type;
4869   tree cv_qualifier_seq;
4870
4871   /* We don't know if there's a ptr-operator next, or not.  */
4872   cp_parser_parse_tentatively (parser);
4873   /* Look for a ptr-operator.  */
4874   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4875   /* If that worked, look for more new-declarators.  */
4876   if (cp_parser_parse_definitely (parser))
4877     {
4878       tree declarator;
4879
4880       /* Parse another optional declarator.  */
4881       declarator = cp_parser_new_declarator_opt (parser);
4882
4883       /* Create the representation of the declarator.  */
4884       if (code == INDIRECT_REF)
4885         declarator = make_pointer_declarator (cv_qualifier_seq,
4886                                               declarator);
4887       else
4888         declarator = make_reference_declarator (cv_qualifier_seq,
4889                                                 declarator);
4890
4891      /* Handle the pointer-to-member case.  */
4892      if (type)
4893        declarator = build_nt (SCOPE_REF, type, declarator);
4894
4895       return declarator;
4896     }
4897
4898   /* If the next token is a `[', there is a direct-new-declarator.  */
4899   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4900     return cp_parser_direct_new_declarator (parser);
4901
4902   return NULL_TREE;
4903 }
4904
4905 /* Parse a direct-new-declarator.
4906
4907    direct-new-declarator:
4908      [ expression ]
4909      direct-new-declarator [constant-expression]  
4910
4911    Returns an ARRAY_REF, following the same conventions as are
4912    documented for cp_parser_direct_declarator.  */
4913
4914 static tree
4915 cp_parser_direct_new_declarator (parser)
4916      cp_parser *parser;
4917 {
4918   tree declarator = NULL_TREE;
4919
4920   while (true)
4921     {
4922       tree expression;
4923
4924       /* Look for the opening `['.  */
4925       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4926       /* The first expression is not required to be constant.  */
4927       if (!declarator)
4928         {
4929           expression = cp_parser_expression (parser);
4930           /* The standard requires that the expression have integral
4931              type.  DR 74 adds enumeration types.  We believe that the
4932              real intent is that these expressions be handled like the
4933              expression in a `switch' condition, which also allows
4934              classes with a single conversion to integral or
4935              enumeration type.  */
4936           if (!processing_template_decl)
4937             {
4938               expression 
4939                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4940                                               expression,
4941                                               /*complain=*/true);
4942               if (!expression)
4943                 {
4944                   error ("expression in new-declarator must have integral or enumeration type");
4945                   expression = error_mark_node;
4946                 }
4947             }
4948         }
4949       /* But all the other expressions must be.  */
4950       else
4951         expression = cp_parser_constant_expression (parser);
4952       /* Look for the closing `]'.  */
4953       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4954
4955       /* Add this bound to the declarator.  */
4956       declarator = build_nt (ARRAY_REF, declarator, expression);
4957
4958       /* If the next token is not a `[', then there are no more
4959          bounds.  */
4960       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4961         break;
4962     }
4963
4964   return declarator;
4965 }
4966
4967 /* Parse a new-initializer.
4968
4969    new-initializer:
4970      ( expression-list [opt] )
4971
4972    Returns a reprsentation of the expression-list.  If there is no
4973    expression-list, VOID_ZERO_NODE is returned.  */
4974
4975 static tree
4976 cp_parser_new_initializer (parser)
4977      cp_parser *parser;
4978 {
4979   tree expression_list;
4980
4981   /* Look for the opening parenthesis.  */
4982   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4983   /* If the next token is not a `)', then there is an
4984      expression-list.  */
4985   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4986     expression_list = cp_parser_expression_list (parser);
4987   else
4988     expression_list = void_zero_node;
4989   /* Look for the closing parenthesis.  */
4990   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4991
4992   return expression_list;
4993 }
4994
4995 /* Parse a delete-expression.
4996
4997    delete-expression:
4998      :: [opt] delete cast-expression
4999      :: [opt] delete [ ] cast-expression
5000
5001    Returns a representation of the expression.  */
5002
5003 static tree
5004 cp_parser_delete_expression (parser)
5005      cp_parser *parser;
5006 {
5007   bool global_scope_p;
5008   bool array_p;
5009   tree expression;
5010
5011   /* Look for the optional `::' operator.  */
5012   global_scope_p
5013     = (cp_parser_global_scope_opt (parser,
5014                                    /*current_scope_valid_p=*/false)
5015        != NULL_TREE);
5016   /* Look for the `delete' keyword.  */
5017   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5018   /* See if the array syntax is in use.  */
5019   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5020     {
5021       /* Consume the `[' token.  */
5022       cp_lexer_consume_token (parser->lexer);
5023       /* Look for the `]' token.  */
5024       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5025       /* Remember that this is the `[]' construct.  */
5026       array_p = true;
5027     }
5028   else
5029     array_p = false;
5030
5031   /* Parse the cast-expression.  */
5032   expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5033
5034   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5035 }
5036
5037 /* Parse a cast-expression.
5038
5039    cast-expression:
5040      unary-expression
5041      ( type-id ) cast-expression
5042
5043    Returns a representation of the expression.  */
5044
5045 static tree
5046 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5047 {
5048   /* If it's a `(', then we might be looking at a cast.  */
5049   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5050     {
5051       tree type = NULL_TREE;
5052       tree expr = NULL_TREE;
5053       bool compound_literal_p;
5054       const char *saved_message;
5055
5056       /* There's no way to know yet whether or not this is a cast.
5057          For example, `(int (3))' is a unary-expression, while `(int)
5058          3' is a cast.  So, we resort to parsing tentatively.  */
5059       cp_parser_parse_tentatively (parser);
5060       /* Types may not be defined in a cast.  */
5061       saved_message = parser->type_definition_forbidden_message;
5062       parser->type_definition_forbidden_message
5063         = "types may not be defined in casts";
5064       /* Consume the `('.  */
5065       cp_lexer_consume_token (parser->lexer);
5066       /* A very tricky bit is that `(struct S) { 3 }' is a
5067          compound-literal (which we permit in C++ as an extension).
5068          But, that construct is not a cast-expression -- it is a
5069          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5070          is legal; if the compound-literal were a cast-expression,
5071          you'd need an extra set of parentheses.)  But, if we parse
5072          the type-id, and it happens to be a class-specifier, then we
5073          will commit to the parse at that point, because we cannot
5074          undo the action that is done when creating a new class.  So,
5075          then we cannot back up and do a postfix-expression.  
5076
5077          Therefore, we scan ahead to the closing `)', and check to see
5078          if the token after the `)' is a `{'.  If so, we are not
5079          looking at a cast-expression.  
5080
5081          Save tokens so that we can put them back.  */
5082       cp_lexer_save_tokens (parser->lexer);
5083       /* Skip tokens until the next token is a closing parenthesis.
5084          If we find the closing `)', and the next token is a `{', then
5085          we are looking at a compound-literal.  */
5086       compound_literal_p 
5087         = (cp_parser_skip_to_closing_parenthesis (parser)
5088            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5089       /* Roll back the tokens we skipped.  */
5090       cp_lexer_rollback_tokens (parser->lexer);
5091       /* If we were looking at a compound-literal, simulate an error
5092          so that the call to cp_parser_parse_definitely below will
5093          fail.  */
5094       if (compound_literal_p)
5095         cp_parser_simulate_error (parser);
5096       else
5097         {
5098           /* Look for the type-id.  */
5099           type = cp_parser_type_id (parser);
5100           /* Look for the closing `)'.  */
5101           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5102         }
5103
5104       /* Restore the saved message.  */
5105       parser->type_definition_forbidden_message = saved_message;
5106
5107       /* If all went well, this is a cast.  */
5108       if (cp_parser_parse_definitely (parser))
5109         {
5110           /* Parse the dependent expression.  */
5111           expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5112           /* Warn about old-style casts, if so requested.  */
5113           if (warn_old_style_cast 
5114               && !in_system_header 
5115               && !VOID_TYPE_P (type) 
5116               && current_lang_name != lang_name_c)
5117             warning ("use of old-style cast");
5118           /* Perform the cast.  */
5119           expr = build_c_cast (type, expr);
5120         }
5121
5122       if (expr)
5123         return expr;
5124     }
5125
5126   /* If we get here, then it's not a cast, so it must be a
5127      unary-expression.  */
5128   return cp_parser_unary_expression (parser, address_p);
5129 }
5130
5131 /* Parse a pm-expression.
5132
5133    pm-expression:
5134      cast-expression
5135      pm-expression .* cast-expression
5136      pm-expression ->* cast-expression
5137
5138      Returns a representation of the expression.  */
5139
5140 static tree
5141 cp_parser_pm_expression (parser)
5142      cp_parser *parser;
5143 {
5144   tree cast_expr;
5145   tree pm_expr;
5146
5147   /* Parse the cast-expresion.  */
5148   cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5149   pm_expr = cast_expr;
5150   /* Now look for pointer-to-member operators.  */
5151   while (true)
5152     {
5153       cp_token *token;
5154       enum cpp_ttype token_type;
5155
5156       /* Peek at the next token.  */
5157       token = cp_lexer_peek_token (parser->lexer);
5158       token_type = token->type;
5159       /* If it's not `.*' or `->*' there's no pointer-to-member
5160          operation.  */
5161       if (token_type != CPP_DOT_STAR 
5162           && token_type != CPP_DEREF_STAR)
5163         break;
5164
5165       /* Consume the token.  */
5166       cp_lexer_consume_token (parser->lexer);
5167
5168       /* Parse another cast-expression.  */
5169       cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5170
5171       /* Build the representation of the pointer-to-member 
5172          operation.  */
5173       if (token_type == CPP_DEREF_STAR)
5174         pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5175       else
5176         pm_expr = build_m_component_ref (pm_expr, cast_expr);
5177     }
5178
5179   return pm_expr;
5180 }
5181
5182 /* Parse a multiplicative-expression.
5183
5184    mulitplicative-expression:
5185      pm-expression
5186      multiplicative-expression * pm-expression
5187      multiplicative-expression / pm-expression
5188      multiplicative-expression % pm-expression
5189
5190    Returns a representation of the expression.  */
5191
5192 static tree
5193 cp_parser_multiplicative_expression (parser)
5194      cp_parser *parser;
5195 {
5196   static const cp_parser_token_tree_map map = {
5197     { CPP_MULT, MULT_EXPR },
5198     { CPP_DIV, TRUNC_DIV_EXPR },
5199     { CPP_MOD, TRUNC_MOD_EXPR },
5200     { CPP_EOF, ERROR_MARK }
5201   };
5202
5203   return cp_parser_binary_expression (parser,
5204                                       map,
5205                                       cp_parser_pm_expression);
5206 }
5207
5208 /* Parse an additive-expression.
5209
5210    additive-expression:
5211      multiplicative-expression
5212      additive-expression + multiplicative-expression
5213      additive-expression - multiplicative-expression
5214
5215    Returns a representation of the expression.  */
5216
5217 static tree
5218 cp_parser_additive_expression (parser)
5219      cp_parser *parser;
5220 {
5221   static const cp_parser_token_tree_map map = {
5222     { CPP_PLUS, PLUS_EXPR },
5223     { CPP_MINUS, MINUS_EXPR },
5224     { CPP_EOF, ERROR_MARK }
5225   };
5226
5227   return cp_parser_binary_expression (parser,
5228                                       map,
5229                                       cp_parser_multiplicative_expression);
5230 }
5231
5232 /* Parse a shift-expression.
5233
5234    shift-expression:
5235      additive-expression
5236      shift-expression << additive-expression
5237      shift-expression >> additive-expression
5238
5239    Returns a representation of the expression.  */
5240
5241 static tree
5242 cp_parser_shift_expression (parser)
5243      cp_parser *parser;
5244 {
5245   static const cp_parser_token_tree_map map = {
5246     { CPP_LSHIFT, LSHIFT_EXPR },
5247     { CPP_RSHIFT, RSHIFT_EXPR },
5248     { CPP_EOF, ERROR_MARK }
5249   };
5250
5251   return cp_parser_binary_expression (parser,
5252                                       map,
5253                                       cp_parser_additive_expression);
5254 }
5255
5256 /* Parse a relational-expression.
5257
5258    relational-expression:
5259      shift-expression
5260      relational-expression < shift-expression
5261      relational-expression > shift-expression
5262      relational-expression <= shift-expression
5263      relational-expression >= shift-expression
5264
5265    GNU Extension:
5266
5267    relational-expression:
5268      relational-expression <? shift-expression
5269      relational-expression >? shift-expression
5270
5271    Returns a representation of the expression.  */
5272
5273 static tree
5274 cp_parser_relational_expression (parser)
5275      cp_parser *parser;
5276 {
5277   static const cp_parser_token_tree_map map = {
5278     { CPP_LESS, LT_EXPR },
5279     { CPP_GREATER, GT_EXPR },
5280     { CPP_LESS_EQ, LE_EXPR },
5281     { CPP_GREATER_EQ, GE_EXPR },
5282     { CPP_MIN, MIN_EXPR },
5283     { CPP_MAX, MAX_EXPR },
5284     { CPP_EOF, ERROR_MARK }
5285   };
5286
5287   return cp_parser_binary_expression (parser,
5288                                       map,
5289                                       cp_parser_shift_expression);
5290 }
5291
5292 /* Parse an equality-expression.
5293
5294    equality-expression:
5295      relational-expression
5296      equality-expression == relational-expression
5297      equality-expression != relational-expression
5298
5299    Returns a representation of the expression.  */
5300
5301 static tree
5302 cp_parser_equality_expression (parser)
5303      cp_parser *parser;
5304 {
5305   static const cp_parser_token_tree_map map = {
5306     { CPP_EQ_EQ, EQ_EXPR },
5307     { CPP_NOT_EQ, NE_EXPR },
5308     { CPP_EOF, ERROR_MARK }
5309   };
5310
5311   return cp_parser_binary_expression (parser,
5312                                       map,
5313                                       cp_parser_relational_expression);
5314 }
5315
5316 /* Parse an and-expression.
5317
5318    and-expression:
5319      equality-expression
5320      and-expression & equality-expression
5321
5322    Returns a representation of the expression.  */
5323
5324 static tree
5325 cp_parser_and_expression (parser)
5326      cp_parser *parser;
5327 {
5328   static const cp_parser_token_tree_map map = {
5329     { CPP_AND, BIT_AND_EXPR },
5330     { CPP_EOF, ERROR_MARK }
5331   };
5332
5333   return cp_parser_binary_expression (parser,
5334                                       map,
5335                                       cp_parser_equality_expression);
5336 }
5337
5338 /* Parse an exclusive-or-expression.
5339
5340    exclusive-or-expression:
5341      and-expression
5342      exclusive-or-expression ^ and-expression
5343
5344    Returns a representation of the expression.  */
5345
5346 static tree
5347 cp_parser_exclusive_or_expression (parser)
5348      cp_parser *parser;
5349 {
5350   static const cp_parser_token_tree_map map = {
5351     { CPP_XOR, BIT_XOR_EXPR },
5352     { CPP_EOF, ERROR_MARK }
5353   };
5354
5355   return cp_parser_binary_expression (parser,
5356                                       map,
5357                                       cp_parser_and_expression);
5358 }
5359
5360
5361 /* Parse an inclusive-or-expression.
5362
5363    inclusive-or-expression:
5364      exclusive-or-expression
5365      inclusive-or-expression | exclusive-or-expression
5366
5367    Returns a representation of the expression.  */
5368
5369 static tree
5370 cp_parser_inclusive_or_expression (parser)
5371      cp_parser *parser;
5372 {
5373   static const cp_parser_token_tree_map map = {
5374     { CPP_OR, BIT_IOR_EXPR },
5375     { CPP_EOF, ERROR_MARK }
5376   };
5377
5378   return cp_parser_binary_expression (parser,
5379                                       map,
5380                                       cp_parser_exclusive_or_expression);
5381 }
5382
5383 /* Parse a logical-and-expression.
5384
5385    logical-and-expression:
5386      inclusive-or-expression
5387      logical-and-expression && inclusive-or-expression
5388
5389    Returns a representation of the expression.  */
5390
5391 static tree
5392 cp_parser_logical_and_expression (parser)
5393      cp_parser *parser;
5394 {
5395   static const cp_parser_token_tree_map map = {
5396     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5397     { CPP_EOF, ERROR_MARK }
5398   };
5399
5400   return cp_parser_binary_expression (parser,
5401                                       map,
5402                                       cp_parser_inclusive_or_expression);
5403 }
5404
5405 /* Parse a logical-or-expression.
5406
5407    logical-or-expression:
5408      logical-and-expresion
5409      logical-or-expression || logical-and-expression
5410
5411    Returns a representation of the expression.  */
5412
5413 static tree
5414 cp_parser_logical_or_expression (parser)
5415      cp_parser *parser;
5416 {
5417   static const cp_parser_token_tree_map map = {
5418     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5419     { CPP_EOF, ERROR_MARK }
5420   };
5421
5422   return cp_parser_binary_expression (parser,
5423                                       map,
5424                                       cp_parser_logical_and_expression);
5425 }
5426
5427 /* Parse a conditional-expression.
5428
5429    conditional-expression:
5430      logical-or-expression
5431      logical-or-expression ? expression : assignment-expression
5432      
5433    GNU Extensions:
5434    
5435    conditional-expression:
5436      logical-or-expression ?  : assignment-expression
5437
5438    Returns a representation of the expression.  */
5439
5440 static tree
5441 cp_parser_conditional_expression (parser)
5442      cp_parser *parser;
5443 {
5444   tree logical_or_expr;
5445
5446   /* Parse the logical-or-expression.  */
5447   logical_or_expr = cp_parser_logical_or_expression (parser);
5448   /* If the next token is a `?', then we have a real conditional
5449      expression.  */
5450   if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5451     return cp_parser_question_colon_clause (parser, logical_or_expr);
5452   /* Otherwise, the value is simply the logical-or-expression.  */
5453   else
5454     return logical_or_expr;
5455 }
5456
5457 /* Parse the `? expression : assignment-expression' part of a
5458    conditional-expression.  The LOGICAL_OR_EXPR is the
5459    logical-or-expression that started the conditional-expression.
5460    Returns a representation of the entire conditional-expression.
5461
5462    This routine exists only so that it can be shared between
5463    cp_parser_conditional_expression and
5464    cp_parser_assignment_expression.
5465
5466      ? expression : assignment-expression
5467    
5468    GNU Extensions:
5469    
5470      ? : assignment-expression */
5471
5472 static tree
5473 cp_parser_question_colon_clause (parser, logical_or_expr)
5474      cp_parser *parser;
5475      tree logical_or_expr;
5476 {
5477   tree expr;
5478   tree assignment_expr;
5479
5480   /* Consume the `?' token.  */
5481   cp_lexer_consume_token (parser->lexer);
5482   if (cp_parser_allow_gnu_extensions_p (parser)
5483       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5484     /* Implicit true clause.  */
5485     expr = NULL_TREE;
5486   else
5487     /* Parse the expression.  */
5488     expr = cp_parser_expression (parser);
5489   
5490   /* The next token should be a `:'.  */
5491   cp_parser_require (parser, CPP_COLON, "`:'");
5492   /* Parse the assignment-expression.  */
5493   assignment_expr = cp_parser_assignment_expression (parser);
5494
5495   /* Build the conditional-expression.  */
5496   return build_x_conditional_expr (logical_or_expr,
5497                                    expr,
5498                                    assignment_expr);
5499 }
5500
5501 /* Parse an assignment-expression.
5502
5503    assignment-expression:
5504      conditional-expression
5505      logical-or-expression assignment-operator assignment_expression
5506      throw-expression
5507
5508    Returns a representation for the expression.  */
5509
5510 static tree
5511 cp_parser_assignment_expression (parser)
5512      cp_parser *parser;
5513 {
5514   tree expr;
5515
5516   /* If the next token is the `throw' keyword, then we're looking at
5517      a throw-expression.  */
5518   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5519     expr = cp_parser_throw_expression (parser);
5520   /* Otherwise, it must be that we are looking at a
5521      logical-or-expression.  */
5522   else
5523     {
5524       /* Parse the logical-or-expression.  */
5525       expr = cp_parser_logical_or_expression (parser);
5526       /* If the next token is a `?' then we're actually looking at a
5527          conditional-expression.  */
5528       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5529         return cp_parser_question_colon_clause (parser, expr);
5530       else 
5531         {
5532           enum tree_code assignment_operator;
5533
5534           /* If it's an assignment-operator, we're using the second
5535              production.  */
5536           assignment_operator 
5537             = cp_parser_assignment_operator_opt (parser);
5538           if (assignment_operator != ERROR_MARK)
5539             {
5540               tree rhs;
5541
5542               /* Parse the right-hand side of the assignment.  */
5543               rhs = cp_parser_assignment_expression (parser);
5544               /* Build the asignment expression.  */
5545               expr = build_x_modify_expr (expr, 
5546                                           assignment_operator, 
5547                                           rhs);
5548             }
5549         }
5550     }
5551
5552   return expr;
5553 }
5554
5555 /* Parse an (optional) assignment-operator.
5556
5557    assignment-operator: one of 
5558      = *= /= %= += -= >>= <<= &= ^= |=  
5559
5560    GNU Extension:
5561    
5562    assignment-operator: one of
5563      <?= >?=
5564
5565    If the next token is an assignment operator, the corresponding tree
5566    code is returned, and the token is consumed.  For example, for
5567    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5568    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5569    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5570    operator, ERROR_MARK is returned.  */
5571
5572 static enum tree_code
5573 cp_parser_assignment_operator_opt (parser)
5574      cp_parser *parser;
5575 {
5576   enum tree_code op;
5577   cp_token *token;
5578
5579   /* Peek at the next toen.  */
5580   token = cp_lexer_peek_token (parser->lexer);
5581
5582   switch (token->type)
5583     {
5584     case CPP_EQ:
5585       op = NOP_EXPR;
5586       break;
5587
5588     case CPP_MULT_EQ:
5589       op = MULT_EXPR;
5590       break;
5591
5592     case CPP_DIV_EQ:
5593       op = TRUNC_DIV_EXPR;
5594       break;
5595
5596     case CPP_MOD_EQ:
5597       op = TRUNC_MOD_EXPR;
5598       break;
5599
5600     case CPP_PLUS_EQ:
5601       op = PLUS_EXPR;
5602       break;
5603
5604     case CPP_MINUS_EQ:
5605       op = MINUS_EXPR;
5606       break;
5607
5608     case CPP_RSHIFT_EQ:
5609       op = RSHIFT_EXPR;
5610       break;
5611
5612     case CPP_LSHIFT_EQ:
5613       op = LSHIFT_EXPR;
5614       break;
5615
5616     case CPP_AND_EQ:
5617       op = BIT_AND_EXPR;
5618       break;
5619
5620     case CPP_XOR_EQ:
5621       op = BIT_XOR_EXPR;
5622       break;
5623
5624     case CPP_OR_EQ:
5625       op = BIT_IOR_EXPR;
5626       break;
5627
5628     case CPP_MIN_EQ:
5629       op = MIN_EXPR;
5630       break;
5631
5632     case CPP_MAX_EQ:
5633       op = MAX_EXPR;
5634       break;
5635
5636     default: 
5637       /* Nothing else is an assignment operator.  */
5638       op = ERROR_MARK;
5639     }
5640
5641   /* If it was an assignment operator, consume it.  */
5642   if (op != ERROR_MARK)
5643     cp_lexer_consume_token (parser->lexer);
5644
5645   return op;
5646 }
5647
5648 /* Parse an expression.
5649
5650    expression:
5651      assignment-expression
5652      expression , assignment-expression
5653
5654    Returns a representation of the expression.  */
5655
5656 static tree
5657 cp_parser_expression (parser)
5658      cp_parser *parser;
5659 {
5660   tree expression = NULL_TREE;
5661   bool saw_comma_p = false;
5662
5663   while (true)
5664     {
5665       tree assignment_expression;
5666
5667       /* Parse the next assignment-expression.  */
5668       assignment_expression 
5669         = cp_parser_assignment_expression (parser);
5670       /* If this is the first assignment-expression, we can just
5671          save it away.  */
5672       if (!expression)
5673         expression = assignment_expression;
5674       /* Otherwise, chain the expressions together.  It is unclear why
5675          we do not simply build COMPOUND_EXPRs as we go.  */
5676       else
5677         expression = tree_cons (NULL_TREE, 
5678                                 assignment_expression,
5679                                 expression);
5680       /* If the next token is not a comma, then we are done with the
5681          expression.  */
5682       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5683         break;
5684       /* Consume the `,'.  */
5685       cp_lexer_consume_token (parser->lexer);
5686       /* The first time we see a `,', we must take special action
5687          because the representation used for a single expression is
5688          different from that used for a list containing the single
5689          expression.  */
5690       if (!saw_comma_p)
5691         {
5692           /* Remember that this expression has a `,' in it.  */
5693           saw_comma_p = true;
5694           /* Turn the EXPRESSION into a TREE_LIST so that we can link
5695              additional expressions to it.  */
5696           expression = build_tree_list (NULL_TREE, expression);
5697         }
5698     }
5699
5700   /* Build a COMPOUND_EXPR to represent the entire expression, if
5701      necessary.  We built up the list in reverse order, so we must
5702      straighten it out here.  */
5703   if (saw_comma_p)
5704     expression = build_x_compound_expr (nreverse (expression));
5705
5706   return expression;
5707 }
5708
5709 /* Parse a constant-expression. 
5710
5711    constant-expression:
5712      conditional-expression  */
5713
5714 static tree
5715 cp_parser_constant_expression (parser)
5716      cp_parser *parser;
5717 {
5718   bool saved_constant_expression_p;
5719   tree expression;
5720
5721   /* It might seem that we could simply parse the
5722      conditional-expression, and then check to see if it were
5723      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5724      one that the compiler can figure out is constant, possibly after
5725      doing some simplifications or optimizations.  The standard has a
5726      precise definition of constant-expression, and we must honor
5727      that, even though it is somewhat more restrictive.
5728
5729      For example:
5730
5731        int i[(2, 3)];
5732
5733      is not a legal declaration, because `(2, 3)' is not a
5734      constant-expression.  The `,' operator is forbidden in a
5735      constant-expression.  However, GCC's constant-folding machinery
5736      will fold this operation to an INTEGER_CST for `3'.  */
5737
5738   /* Save the old setting of CONSTANT_EXPRESSION_P.  */
5739   saved_constant_expression_p = parser->constant_expression_p;
5740   /* We are now parsing a constant-expression.  */
5741   parser->constant_expression_p = true;
5742   /* Parse the conditional-expression.  */
5743   expression = cp_parser_conditional_expression (parser);
5744   /* Restore the old setting of CONSTANT_EXPRESSION_P.  */
5745   parser->constant_expression_p = saved_constant_expression_p;
5746
5747   return expression;
5748 }
5749
5750 /* Statements [gram.stmt.stmt]  */
5751
5752 /* Parse a statement.  
5753
5754    statement:
5755      labeled-statement
5756      expression-statement
5757      compound-statement
5758      selection-statement
5759      iteration-statement
5760      jump-statement
5761      declaration-statement
5762      try-block  */
5763
5764 static void
5765 cp_parser_statement (parser)
5766      cp_parser *parser;
5767 {
5768   tree statement;
5769   cp_token *token;
5770   int statement_line_number;
5771
5772   /* There is no statement yet.  */
5773   statement = NULL_TREE;
5774   /* Peek at the next token.  */
5775   token = cp_lexer_peek_token (parser->lexer);
5776   /* Remember the line number of the first token in the statement.  */
5777   statement_line_number = token->line_number;
5778   /* If this is a keyword, then that will often determine what kind of
5779      statement we have.  */
5780   if (token->type == CPP_KEYWORD)
5781     {
5782       enum rid keyword = token->keyword;
5783
5784       switch (keyword)
5785         {
5786         case RID_CASE:
5787         case RID_DEFAULT:
5788           statement = cp_parser_labeled_statement (parser);
5789           break;
5790
5791         case RID_IF:
5792         case RID_SWITCH:
5793           statement = cp_parser_selection_statement (parser);
5794           break;
5795
5796         case RID_WHILE:
5797         case RID_DO:
5798         case RID_FOR:
5799           statement = cp_parser_iteration_statement (parser);
5800           break;
5801
5802         case RID_BREAK:
5803         case RID_CONTINUE:
5804         case RID_RETURN:
5805         case RID_GOTO:
5806           statement = cp_parser_jump_statement (parser);
5807           break;
5808
5809         case RID_TRY:
5810           statement = cp_parser_try_block (parser);
5811           break;
5812
5813         default:
5814           /* It might be a keyword like `int' that can start a
5815              declaration-statement.  */
5816           break;
5817         }
5818     }
5819   else if (token->type == CPP_NAME)
5820     {
5821       /* If the next token is a `:', then we are looking at a
5822          labeled-statement.  */
5823       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5824       if (token->type == CPP_COLON)
5825         statement = cp_parser_labeled_statement (parser);
5826     }
5827   /* Anything that starts with a `{' must be a compound-statement.  */
5828   else if (token->type == CPP_OPEN_BRACE)
5829     statement = cp_parser_compound_statement (parser);
5830
5831   /* Everything else must be a declaration-statement or an
5832      expression-statement.  Try for the declaration-statement 
5833      first, unless we are looking at a `;', in which case we know that
5834      we have an expression-statement.  */
5835   if (!statement)
5836     {
5837       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5838         {
5839           cp_parser_parse_tentatively (parser);
5840           /* Try to parse the declaration-statement.  */
5841           cp_parser_declaration_statement (parser);
5842           /* If that worked, we're done.  */
5843           if (cp_parser_parse_definitely (parser))
5844             return;
5845         }
5846       /* Look for an expression-statement instead.  */
5847       statement = cp_parser_expression_statement (parser);
5848     }
5849
5850   /* Set the line number for the statement.  */
5851   if (statement && statement_code_p (TREE_CODE (statement)))
5852     STMT_LINENO (statement) = statement_line_number;
5853 }
5854
5855 /* Parse a labeled-statement.
5856
5857    labeled-statement:
5858      identifier : statement
5859      case constant-expression : statement
5860      default : statement  
5861
5862    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5863    an ordinary label, returns a LABEL_STMT.  */
5864
5865 static tree
5866 cp_parser_labeled_statement (parser)
5867      cp_parser *parser;
5868 {
5869   cp_token *token;
5870   tree statement = NULL_TREE;
5871
5872   /* The next token should be an identifier.  */
5873   token = cp_lexer_peek_token (parser->lexer);
5874   if (token->type != CPP_NAME
5875       && token->type != CPP_KEYWORD)
5876     {
5877       cp_parser_error (parser, "expected labeled-statement");
5878       return error_mark_node;
5879     }
5880
5881   switch (token->keyword)
5882     {
5883     case RID_CASE:
5884       {
5885         tree expr;
5886
5887         /* Consume the `case' token.  */
5888         cp_lexer_consume_token (parser->lexer);
5889         /* Parse the constant-expression.  */
5890         expr = cp_parser_constant_expression (parser);
5891         /* Create the label.  */
5892         statement = finish_case_label (expr, NULL_TREE);
5893       }
5894       break;
5895
5896     case RID_DEFAULT:
5897       /* Consume the `default' token.  */
5898       cp_lexer_consume_token (parser->lexer);
5899       /* Create the label.  */
5900       statement = finish_case_label (NULL_TREE, NULL_TREE);
5901       break;
5902
5903     default:
5904       /* Anything else must be an ordinary label.  */
5905       statement = finish_label_stmt (cp_parser_identifier (parser));
5906       break;
5907     }
5908
5909   /* Require the `:' token.  */
5910   cp_parser_require (parser, CPP_COLON, "`:'");
5911   /* Parse the labeled statement.  */
5912   cp_parser_statement (parser);
5913
5914   /* Return the label, in the case of a `case' or `default' label.  */
5915   return statement;
5916 }
5917
5918 /* Parse an expression-statement.
5919
5920    expression-statement:
5921      expression [opt] ;
5922
5923    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5924    statement consists of nothing more than an `;'.  */
5925
5926 static tree
5927 cp_parser_expression_statement (parser)
5928      cp_parser *parser;
5929 {
5930   tree statement;
5931
5932   /* If the next token is not a `;', then there is an expression to parse.  */
5933   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5934     statement = finish_expr_stmt (cp_parser_expression (parser));
5935   /* Otherwise, we do not even bother to build an EXPR_STMT.  */
5936   else
5937     {
5938       finish_stmt ();
5939       statement = NULL_TREE;
5940     }
5941   /* Consume the final `;'.  */
5942   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5943     {
5944       /* If there is additional (erroneous) input, skip to the end of
5945          the statement.  */
5946       cp_parser_skip_to_end_of_statement (parser);
5947       /* If the next token is now a `;', consume it.  */
5948       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5949         cp_lexer_consume_token (parser->lexer);
5950     }
5951
5952   return statement;
5953 }
5954
5955 /* Parse a compound-statement.
5956
5957    compound-statement:
5958      { statement-seq [opt] }
5959      
5960    Returns a COMPOUND_STMT representing the statement.  */
5961
5962 static tree
5963 cp_parser_compound_statement (cp_parser *parser)
5964 {
5965   tree compound_stmt;
5966
5967   /* Consume the `{'.  */
5968   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5969     return error_mark_node;
5970   /* Begin the compound-statement.  */
5971   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5972   /* Parse an (optional) statement-seq.  */
5973   cp_parser_statement_seq_opt (parser);
5974   /* Finish the compound-statement.  */
5975   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5976   /* Consume the `}'.  */
5977   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5978
5979   return compound_stmt;
5980 }
5981
5982 /* Parse an (optional) statement-seq.
5983
5984    statement-seq:
5985      statement
5986      statement-seq [opt] statement  */
5987
5988 static void
5989 cp_parser_statement_seq_opt (parser)
5990      cp_parser *parser;
5991 {
5992   /* Scan statements until there aren't any more.  */
5993   while (true)
5994     {
5995       /* If we're looking at a `}', then we've run out of statements.  */
5996       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5997           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5998         break;
5999
6000       /* Parse the statement.  */
6001       cp_parser_statement (parser);
6002     }
6003 }
6004
6005 /* Parse a selection-statement.
6006
6007    selection-statement:
6008      if ( condition ) statement
6009      if ( condition ) statement else statement
6010      switch ( condition ) statement  
6011
6012    Returns the new IF_STMT or SWITCH_STMT.  */
6013
6014 static tree
6015 cp_parser_selection_statement (parser)
6016      cp_parser *parser;
6017 {
6018   cp_token *token;
6019   enum rid keyword;
6020
6021   /* Peek at the next token.  */
6022   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6023
6024   /* See what kind of keyword it is.  */
6025   keyword = token->keyword;
6026   switch (keyword)
6027     {
6028     case RID_IF:
6029     case RID_SWITCH:
6030       {
6031         tree statement;
6032         tree condition;
6033
6034         /* Look for the `('.  */
6035         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6036           {
6037             cp_parser_skip_to_end_of_statement (parser);
6038             return error_mark_node;
6039           }
6040
6041         /* Begin the selection-statement.  */
6042         if (keyword == RID_IF)
6043           statement = begin_if_stmt ();
6044         else
6045           statement = begin_switch_stmt ();
6046
6047         /* Parse the condition.  */
6048         condition = cp_parser_condition (parser);
6049         /* Look for the `)'.  */
6050         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6051           cp_parser_skip_to_closing_parenthesis (parser);
6052
6053         if (keyword == RID_IF)
6054           {
6055             tree then_stmt;
6056
6057             /* Add the condition.  */
6058             finish_if_stmt_cond (condition, statement);
6059
6060             /* Parse the then-clause.  */
6061             then_stmt = cp_parser_implicitly_scoped_statement (parser);
6062             finish_then_clause (statement);
6063
6064             /* If the next token is `else', parse the else-clause.  */
6065             if (cp_lexer_next_token_is_keyword (parser->lexer,
6066                                                 RID_ELSE))
6067               {
6068                 tree else_stmt;
6069
6070                 /* Consume the `else' keyword.  */
6071                 cp_lexer_consume_token (parser->lexer);
6072                 /* Parse the else-clause.  */
6073                 else_stmt 
6074                   = cp_parser_implicitly_scoped_statement (parser);
6075                 finish_else_clause (statement);
6076               }
6077
6078             /* Now we're all done with the if-statement.  */
6079             finish_if_stmt ();
6080           }
6081         else
6082           {
6083             tree body;
6084
6085             /* Add the condition.  */
6086             finish_switch_cond (condition, statement);
6087
6088             /* Parse the body of the switch-statement.  */
6089             body = cp_parser_implicitly_scoped_statement (parser);
6090
6091             /* Now we're all done with the switch-statement.  */
6092             finish_switch_stmt (statement);
6093           }
6094
6095         return statement;
6096       }
6097       break;
6098
6099     default:
6100       cp_parser_error (parser, "expected selection-statement");
6101       return error_mark_node;
6102     }
6103 }
6104
6105 /* Parse a condition. 
6106
6107    condition:
6108      expression
6109      type-specifier-seq declarator = assignment-expression  
6110
6111    GNU Extension:
6112    
6113    condition:
6114      type-specifier-seq declarator asm-specification [opt] 
6115        attributes [opt] = assignment-expression
6116  
6117    Returns the expression that should be tested.  */
6118
6119 static tree
6120 cp_parser_condition (parser)
6121      cp_parser *parser;
6122 {
6123   tree type_specifiers;
6124   const char *saved_message;
6125
6126   /* Try the declaration first.  */
6127   cp_parser_parse_tentatively (parser);
6128   /* New types are not allowed in the type-specifier-seq for a
6129      condition.  */
6130   saved_message = parser->type_definition_forbidden_message;
6131   parser->type_definition_forbidden_message
6132     = "types may not be defined in conditions";
6133   /* Parse the type-specifier-seq.  */
6134   type_specifiers = cp_parser_type_specifier_seq (parser);
6135   /* Restore the saved message.  */
6136   parser->type_definition_forbidden_message = saved_message;
6137   /* If all is well, we might be looking at a declaration.  */
6138   if (!cp_parser_error_occurred (parser))
6139     {
6140       tree decl;
6141       tree asm_specification;
6142       tree attributes;
6143       tree declarator;
6144       tree initializer = NULL_TREE;
6145       
6146       /* Parse the declarator.  */
6147       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6148                                          /*ctor_dtor_or_conv_p=*/NULL);
6149       /* Parse the attributes.  */
6150       attributes = cp_parser_attributes_opt (parser);
6151       /* Parse the asm-specification.  */
6152       asm_specification = cp_parser_asm_specification_opt (parser);
6153       /* If the next token is not an `=', then we might still be
6154          looking at an expression.  For example:
6155          
6156            if (A(a).x)
6157           
6158          looks like a decl-specifier-seq and a declarator -- but then
6159          there is no `=', so this is an expression.  */
6160       cp_parser_require (parser, CPP_EQ, "`='");
6161       /* If we did see an `=', then we are looking at a declaration
6162          for sure.  */
6163       if (cp_parser_parse_definitely (parser))
6164         {
6165           /* Create the declaration.  */
6166           decl = start_decl (declarator, type_specifiers, 
6167                              /*initialized_p=*/true,
6168                              attributes, /*prefix_attributes=*/NULL_TREE);
6169           /* Parse the assignment-expression.  */
6170           initializer = cp_parser_assignment_expression (parser);
6171           
6172           /* Process the initializer.  */
6173           cp_finish_decl (decl, 
6174                           initializer, 
6175                           asm_specification, 
6176                           LOOKUP_ONLYCONVERTING);
6177           
6178           return convert_from_reference (decl);
6179         }
6180     }
6181   /* If we didn't even get past the declarator successfully, we are
6182      definitely not looking at a declaration.  */
6183   else
6184     cp_parser_abort_tentative_parse (parser);
6185
6186   /* Otherwise, we are looking at an expression.  */
6187   return cp_parser_expression (parser);
6188 }
6189
6190 /* Parse an iteration-statement.
6191
6192    iteration-statement:
6193      while ( condition ) statement
6194      do statement while ( expression ) ;
6195      for ( for-init-statement condition [opt] ; expression [opt] )
6196        statement
6197
6198    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6199
6200 static tree
6201 cp_parser_iteration_statement (parser)
6202      cp_parser *parser;
6203 {
6204   cp_token *token;
6205   enum rid keyword;
6206   tree statement;
6207
6208   /* Peek at the next token.  */
6209   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6210   if (!token)
6211     return error_mark_node;
6212
6213   /* See what kind of keyword it is.  */
6214   keyword = token->keyword;
6215   switch (keyword)
6216     {
6217     case RID_WHILE:
6218       {
6219         tree condition;
6220
6221         /* Begin the while-statement.  */
6222         statement = begin_while_stmt ();
6223         /* Look for the `('.  */
6224         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6225         /* Parse the condition.  */
6226         condition = cp_parser_condition (parser);
6227         finish_while_stmt_cond (condition, statement);
6228         /* Look for the `)'.  */
6229         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6230         /* Parse the dependent statement.  */
6231         cp_parser_already_scoped_statement (parser);
6232         /* We're done with the while-statement.  */
6233         finish_while_stmt (statement);
6234       }
6235       break;
6236
6237     case RID_DO:
6238       {
6239         tree expression;
6240
6241         /* Begin the do-statement.  */
6242         statement = begin_do_stmt ();
6243         /* Parse the body of the do-statement.  */
6244         cp_parser_implicitly_scoped_statement (parser);
6245         finish_do_body (statement);
6246         /* Look for the `while' keyword.  */
6247         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6248         /* Look for the `('.  */
6249         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6250         /* Parse the expression.  */
6251         expression = cp_parser_expression (parser);
6252         /* We're done with the do-statement.  */
6253         finish_do_stmt (expression, statement);
6254         /* Look for the `)'.  */
6255         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6256         /* Look for the `;'.  */
6257         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6258       }
6259       break;
6260
6261     case RID_FOR:
6262       {
6263         tree condition = NULL_TREE;
6264         tree expression = NULL_TREE;
6265
6266         /* Begin the for-statement.  */
6267         statement = begin_for_stmt ();
6268         /* Look for the `('.  */
6269         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6270         /* Parse the initialization.  */
6271         cp_parser_for_init_statement (parser);
6272         finish_for_init_stmt (statement);
6273
6274         /* If there's a condition, process it.  */
6275         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6276           condition = cp_parser_condition (parser);
6277         finish_for_cond (condition, statement);
6278         /* Look for the `;'.  */
6279         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6280
6281         /* If there's an expression, process it.  */
6282         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6283           expression = cp_parser_expression (parser);
6284         finish_for_expr (expression, statement);
6285         /* Look for the `)'.  */
6286         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6287
6288         /* Parse the body of the for-statement.  */
6289         cp_parser_already_scoped_statement (parser);
6290
6291         /* We're done with the for-statement.  */
6292         finish_for_stmt (statement);
6293       }
6294       break;
6295
6296     default:
6297       cp_parser_error (parser, "expected iteration-statement");
6298       statement = error_mark_node;
6299       break;
6300     }
6301
6302   return statement;
6303 }
6304
6305 /* Parse a for-init-statement.
6306
6307    for-init-statement:
6308      expression-statement
6309      simple-declaration  */
6310
6311 static void
6312 cp_parser_for_init_statement (parser)
6313      cp_parser *parser;
6314 {
6315   /* If the next token is a `;', then we have an empty
6316      expression-statement.  Gramatically, this is also a
6317      simple-declaration, but an invalid one, because it does not
6318      declare anything.  Therefore, if we did not handle this case
6319      specially, we would issue an error message about an invalid
6320      declaration.  */
6321   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6322     {
6323       /* We're going to speculatively look for a declaration, falling back
6324          to an expression, if necessary.  */
6325       cp_parser_parse_tentatively (parser);
6326       /* Parse the declaration.  */
6327       cp_parser_simple_declaration (parser,
6328                                     /*function_definition_allowed_p=*/false);
6329       /* If the tentative parse failed, then we shall need to look for an
6330          expression-statement.  */
6331       if (cp_parser_parse_definitely (parser))
6332         return;
6333     }
6334
6335   cp_parser_expression_statement (parser);
6336 }
6337
6338 /* Parse a jump-statement.
6339
6340    jump-statement:
6341      break ;
6342      continue ;
6343      return expression [opt] ;
6344      goto identifier ;  
6345
6346    GNU extension:
6347
6348    jump-statement:
6349      goto * expression ;
6350
6351    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6352    GOTO_STMT.  */
6353
6354 static tree
6355 cp_parser_jump_statement (parser)
6356      cp_parser *parser;
6357 {
6358   tree statement = error_mark_node;
6359   cp_token *token;
6360   enum rid keyword;
6361
6362   /* Peek at the next token.  */
6363   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6364   if (!token)
6365     return error_mark_node;
6366
6367   /* See what kind of keyword it is.  */
6368   keyword = token->keyword;
6369   switch (keyword)
6370     {
6371     case RID_BREAK:
6372       statement = finish_break_stmt ();
6373       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6374       break;
6375
6376     case RID_CONTINUE:
6377       statement = finish_continue_stmt ();
6378       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6379       break;
6380
6381     case RID_RETURN:
6382       {
6383         tree expr;
6384
6385         /* If the next token is a `;', then there is no 
6386            expression.  */
6387         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6388           expr = cp_parser_expression (parser);
6389         else
6390           expr = NULL_TREE;
6391         /* Build the return-statement.  */
6392         statement = finish_return_stmt (expr);
6393         /* Look for the final `;'.  */
6394         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6395       }
6396       break;
6397
6398     case RID_GOTO:
6399       /* Create the goto-statement.  */
6400       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6401         {
6402           /* Issue a warning about this use of a GNU extension.  */
6403           if (pedantic)
6404             pedwarn ("ISO C++ forbids computed gotos");
6405           /* Consume the '*' token.  */
6406           cp_lexer_consume_token (parser->lexer);
6407           /* Parse the dependent expression.  */
6408           finish_goto_stmt (cp_parser_expression (parser));
6409         }
6410       else
6411         finish_goto_stmt (cp_parser_identifier (parser));
6412       /* Look for the final `;'.  */
6413       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6414       break;
6415
6416     default:
6417       cp_parser_error (parser, "expected jump-statement");
6418       break;
6419     }
6420
6421   return statement;
6422 }
6423
6424 /* Parse a declaration-statement.
6425
6426    declaration-statement:
6427      block-declaration  */
6428
6429 static void
6430 cp_parser_declaration_statement (parser)
6431      cp_parser *parser;
6432 {
6433   /* Parse the block-declaration.  */
6434   cp_parser_block_declaration (parser, /*statement_p=*/true);
6435
6436   /* Finish off the statement.  */
6437   finish_stmt ();
6438 }
6439
6440 /* Some dependent statements (like `if (cond) statement'), are
6441    implicitly in their own scope.  In other words, if the statement is
6442    a single statement (as opposed to a compound-statement), it is
6443    none-the-less treated as if it were enclosed in braces.  Any
6444    declarations appearing in the dependent statement are out of scope
6445    after control passes that point.  This function parses a statement,
6446    but ensures that is in its own scope, even if it is not a
6447    compound-statement.  
6448
6449    Returns the new statement.  */
6450
6451 static tree
6452 cp_parser_implicitly_scoped_statement (parser)
6453      cp_parser *parser;
6454 {
6455   tree statement;
6456
6457   /* If the token is not a `{', then we must take special action.  */
6458   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6459     {
6460       /* Create a compound-statement.  */
6461       statement = begin_compound_stmt (/*has_no_scope=*/0);
6462       /* Parse the dependent-statement.  */
6463       cp_parser_statement (parser);
6464       /* Finish the dummy compound-statement.  */
6465       finish_compound_stmt (/*has_no_scope=*/0, statement);
6466     }
6467   /* Otherwise, we simply parse the statement directly.  */
6468   else
6469     statement = cp_parser_compound_statement (parser);
6470
6471   /* Return the statement.  */
6472   return statement;
6473 }
6474
6475 /* For some dependent statements (like `while (cond) statement'), we
6476    have already created a scope.  Therefore, even if the dependent
6477    statement is a compound-statement, we do not want to create another
6478    scope.  */
6479
6480 static void
6481 cp_parser_already_scoped_statement (parser)
6482      cp_parser *parser;
6483 {
6484   /* If the token is not a `{', then we must take special action.  */
6485   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6486     {
6487       tree statement;
6488
6489       /* Create a compound-statement.  */
6490       statement = begin_compound_stmt (/*has_no_scope=*/1);
6491       /* Parse the dependent-statement.  */
6492       cp_parser_statement (parser);
6493       /* Finish the dummy compound-statement.  */
6494       finish_compound_stmt (/*has_no_scope=*/1, statement);
6495     }
6496   /* Otherwise, we simply parse the statement directly.  */
6497   else
6498     cp_parser_statement (parser);
6499 }
6500
6501 /* Declarations [gram.dcl.dcl] */
6502
6503 /* Parse an optional declaration-sequence.
6504
6505    declaration-seq:
6506      declaration
6507      declaration-seq declaration  */
6508
6509 static void
6510 cp_parser_declaration_seq_opt (parser)
6511      cp_parser *parser;
6512 {
6513   while (true)
6514     {
6515       cp_token *token;
6516
6517       token = cp_lexer_peek_token (parser->lexer);
6518
6519       if (token->type == CPP_CLOSE_BRACE
6520           || token->type == CPP_EOF)
6521         break;
6522
6523       if (token->type == CPP_SEMICOLON) 
6524         {
6525           /* A declaration consisting of a single semicolon is
6526              invalid.  Allow it unless we're being pedantic.  */
6527           if (pedantic)
6528             pedwarn ("extra `;'");
6529           cp_lexer_consume_token (parser->lexer);
6530           continue;
6531         }
6532
6533       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6534          parser to enter or exit implict `extern "C"' blocks.  */
6535       while (pending_lang_change > 0)
6536         {
6537           push_lang_context (lang_name_c);
6538           --pending_lang_change;
6539         }
6540       while (pending_lang_change < 0)
6541         {
6542           pop_lang_context ();
6543           ++pending_lang_change;
6544         }
6545
6546       /* Parse the declaration itself.  */
6547       cp_parser_declaration (parser);
6548     }
6549 }
6550
6551 /* Parse a declaration.
6552
6553    declaration:
6554      block-declaration
6555      function-definition
6556      template-declaration
6557      explicit-instantiation
6558      explicit-specialization
6559      linkage-specification
6560      namespace-definition    
6561
6562    GNU extension:
6563
6564    declaration:
6565       __extension__ declaration */
6566
6567 static void
6568 cp_parser_declaration (parser)
6569      cp_parser *parser;
6570 {
6571   cp_token token1;
6572   cp_token token2;
6573   int saved_pedantic;
6574
6575   /* Check for the `__extension__' keyword.  */
6576   if (cp_parser_extension_opt (parser, &saved_pedantic))
6577     {
6578       /* Parse the qualified declaration.  */
6579       cp_parser_declaration (parser);
6580       /* Restore the PEDANTIC flag.  */
6581       pedantic = saved_pedantic;
6582
6583       return;
6584     }
6585
6586   /* Try to figure out what kind of declaration is present.  */
6587   token1 = *cp_lexer_peek_token (parser->lexer);
6588   if (token1.type != CPP_EOF)
6589     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6590
6591   /* If the next token is `extern' and the following token is a string
6592      literal, then we have a linkage specification.  */
6593   if (token1.keyword == RID_EXTERN
6594       && cp_parser_is_string_literal (&token2))
6595     cp_parser_linkage_specification (parser);
6596   /* If the next token is `template', then we have either a template
6597      declaration, an explicit instantiation, or an explicit
6598      specialization.  */
6599   else if (token1.keyword == RID_TEMPLATE)
6600     {
6601       /* `template <>' indicates a template specialization.  */
6602       if (token2.type == CPP_LESS
6603           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6604         cp_parser_explicit_specialization (parser);
6605       /* `template <' indicates a template declaration.  */
6606       else if (token2.type == CPP_LESS)
6607         cp_parser_template_declaration (parser, /*member_p=*/false);
6608       /* Anything else must be an explicit instantiation.  */
6609       else
6610         cp_parser_explicit_instantiation (parser);
6611     }
6612   /* If the next token is `export', then we have a template
6613      declaration.  */
6614   else if (token1.keyword == RID_EXPORT)
6615     cp_parser_template_declaration (parser, /*member_p=*/false);
6616   /* If the next token is `extern', 'static' or 'inline' and the one
6617      after that is `template', we have a GNU extended explicit
6618      instantiation directive.  */
6619   else if (cp_parser_allow_gnu_extensions_p (parser)
6620            && (token1.keyword == RID_EXTERN
6621                || token1.keyword == RID_STATIC
6622                || token1.keyword == RID_INLINE)
6623            && token2.keyword == RID_TEMPLATE)
6624     cp_parser_explicit_instantiation (parser);
6625   /* If the next token is `namespace', check for a named or unnamed
6626      namespace definition.  */
6627   else if (token1.keyword == RID_NAMESPACE
6628            && (/* A named namespace definition.  */
6629                (token2.type == CPP_NAME
6630                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6631                     == CPP_OPEN_BRACE))
6632                /* An unnamed namespace definition.  */
6633                || token2.type == CPP_OPEN_BRACE))
6634     cp_parser_namespace_definition (parser);
6635   /* We must have either a block declaration or a function
6636      definition.  */
6637   else
6638     /* Try to parse a block-declaration, or a function-definition.  */
6639     cp_parser_block_declaration (parser, /*statement_p=*/false);
6640 }
6641
6642 /* Parse a block-declaration.  
6643
6644    block-declaration:
6645      simple-declaration
6646      asm-definition
6647      namespace-alias-definition
6648      using-declaration
6649      using-directive  
6650
6651    GNU Extension:
6652
6653    block-declaration:
6654      __extension__ block-declaration 
6655      label-declaration
6656
6657    If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6658    part of a declaration-statement.  */
6659
6660 static void
6661 cp_parser_block_declaration (cp_parser *parser, 
6662                              bool      statement_p)
6663 {
6664   cp_token *token1;
6665   int saved_pedantic;
6666
6667   /* Check for the `__extension__' keyword.  */
6668   if (cp_parser_extension_opt (parser, &saved_pedantic))
6669     {
6670       /* Parse the qualified declaration.  */
6671       cp_parser_block_declaration (parser, statement_p);
6672       /* Restore the PEDANTIC flag.  */
6673       pedantic = saved_pedantic;
6674
6675       return;
6676     }
6677
6678   /* Peek at the next token to figure out which kind of declaration is
6679      present.  */
6680   token1 = cp_lexer_peek_token (parser->lexer);
6681
6682   /* If the next keyword is `asm', we have an asm-definition.  */
6683   if (token1->keyword == RID_ASM)
6684     {
6685       if (statement_p)
6686         cp_parser_commit_to_tentative_parse (parser);
6687       cp_parser_asm_definition (parser);
6688     }
6689   /* If the next keyword is `namespace', we have a
6690      namespace-alias-definition.  */
6691   else if (token1->keyword == RID_NAMESPACE)
6692     cp_parser_namespace_alias_definition (parser);
6693   /* If the next keyword is `using', we have either a
6694      using-declaration or a using-directive.  */
6695   else if (token1->keyword == RID_USING)
6696     {
6697       cp_token *token2;
6698
6699       if (statement_p)
6700         cp_parser_commit_to_tentative_parse (parser);
6701       /* If the token after `using' is `namespace', then we have a
6702          using-directive.  */
6703       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6704       if (token2->keyword == RID_NAMESPACE)
6705         cp_parser_using_directive (parser);
6706       /* Otherwise, it's a using-declaration.  */
6707       else
6708         cp_parser_using_declaration (parser);
6709     }
6710   /* If the next keyword is `__label__' we have a label declaration.  */
6711   else if (token1->keyword == RID_LABEL)
6712     {
6713       if (statement_p)
6714         cp_parser_commit_to_tentative_parse (parser);
6715       cp_parser_label_declaration (parser);
6716     }
6717   /* Anything else must be a simple-declaration.  */
6718   else
6719     cp_parser_simple_declaration (parser, !statement_p);
6720 }
6721
6722 /* Parse a simple-declaration.
6723
6724    simple-declaration:
6725      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6726
6727    init-declarator-list:
6728      init-declarator
6729      init-declarator-list , init-declarator 
6730
6731    If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6732    function-definition as a simple-declaration.   */
6733
6734 static void
6735 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6736      cp_parser *parser;
6737      bool function_definition_allowed_p;
6738 {
6739   tree decl_specifiers;
6740   tree attributes;
6741   tree access_checks;
6742   bool declares_class_or_enum;
6743   bool saw_declarator;
6744
6745   /* Defer access checks until we know what is being declared; the
6746      checks for names appearing in the decl-specifier-seq should be
6747      done as if we were in the scope of the thing being declared.  */
6748   cp_parser_start_deferring_access_checks (parser);
6749   /* Parse the decl-specifier-seq.  We have to keep track of whether
6750      or not the decl-specifier-seq declares a named class or
6751      enumeration type, since that is the only case in which the
6752      init-declarator-list is allowed to be empty.  
6753
6754      [dcl.dcl]
6755
6756      In a simple-declaration, the optional init-declarator-list can be
6757      omitted only when declaring a class or enumeration, that is when
6758      the decl-specifier-seq contains either a class-specifier, an
6759      elaborated-type-specifier, or an enum-specifier.  */
6760   decl_specifiers
6761     = cp_parser_decl_specifier_seq (parser, 
6762                                     CP_PARSER_FLAGS_OPTIONAL,
6763                                     &attributes,
6764                                     &declares_class_or_enum);
6765   /* We no longer need to defer access checks.  */
6766   access_checks = cp_parser_stop_deferring_access_checks (parser);
6767
6768   /* Prevent access checks from being reclaimed by GC.  */
6769   parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6770                                            parser->access_checks_lists);
6771
6772   /* Keep going until we hit the `;' at the end of the simple
6773      declaration.  */
6774   saw_declarator = false;
6775   while (cp_lexer_next_token_is_not (parser->lexer, 
6776                                      CPP_SEMICOLON))
6777     {
6778       cp_token *token;
6779       bool function_definition_p;
6780
6781       saw_declarator = true;
6782       /* Parse the init-declarator.  */
6783       cp_parser_init_declarator (parser, decl_specifiers, attributes,
6784                                  access_checks,
6785                                  function_definition_allowed_p,
6786                                  /*member_p=*/false,
6787                                  &function_definition_p);
6788       /* Handle function definitions specially.  */
6789       if (function_definition_p)
6790         {
6791           /* If the next token is a `,', then we are probably
6792              processing something like:
6793
6794                void f() {}, *p;
6795
6796              which is erroneous.  */
6797           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6798             error ("mixing declarations and function-definitions is forbidden");
6799           /* Otherwise, we're done with the list of declarators.  */
6800           else
6801             {
6802               /* Discard access checks no longer in use. */
6803               parser->access_checks_lists
6804                 = TREE_CHAIN (parser->access_checks_lists);
6805               return;
6806             }
6807         }
6808       /* The next token should be either a `,' or a `;'.  */
6809       token = cp_lexer_peek_token (parser->lexer);
6810       /* If it's a `,', there are more declarators to come.  */
6811       if (token->type == CPP_COMMA)
6812         cp_lexer_consume_token (parser->lexer);
6813       /* If it's a `;', we are done.  */
6814       else if (token->type == CPP_SEMICOLON)
6815         break;
6816       /* Anything else is an error.  */
6817       else
6818         {
6819           cp_parser_error (parser, "expected `,' or `;'");
6820           /* Skip tokens until we reach the end of the statement.  */
6821           cp_parser_skip_to_end_of_statement (parser);
6822           /* Discard access checks no longer in use.  */
6823           parser->access_checks_lists
6824             = TREE_CHAIN (parser->access_checks_lists);
6825           return;
6826         }
6827       /* After the first time around, a function-definition is not
6828          allowed -- even if it was OK at first.  For example:
6829
6830            int i, f() {}
6831
6832          is not valid.  */
6833       function_definition_allowed_p = false;
6834     }
6835
6836   /* Issue an error message if no declarators are present, and the
6837      decl-specifier-seq does not itself declare a class or
6838      enumeration.  */
6839   if (!saw_declarator)
6840     {
6841       if (cp_parser_declares_only_class_p (parser))
6842         shadow_tag (decl_specifiers);
6843       /* Perform any deferred access checks.  */
6844       cp_parser_perform_deferred_access_checks (access_checks);
6845     }
6846
6847   /* Consume the `;'.  */
6848   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6849
6850   /* Mark all the classes that appeared in the decl-specifier-seq as
6851      having received a `;'.  */
6852   note_list_got_semicolon (decl_specifiers);
6853
6854   /* Discard access checks no longer in use.  */
6855   parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
6856 }
6857
6858 /* Parse a decl-specifier-seq.
6859
6860    decl-specifier-seq:
6861      decl-specifier-seq [opt] decl-specifier
6862
6863    decl-specifier:
6864      storage-class-specifier
6865      type-specifier
6866      function-specifier
6867      friend
6868      typedef  
6869
6870    GNU Extension:
6871
6872    decl-specifier-seq:
6873      decl-specifier-seq [opt] attributes
6874
6875    Returns a TREE_LIST, giving the decl-specifiers in the order they
6876    appear in the source code.  The TREE_VALUE of each node is the
6877    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6878    TREE_VALUE is simply the correspoding TREE_IDENTIFIER.  For the
6879    representation of a type-specifier, see cp_parser_type_specifier.  
6880
6881    If there are attributes, they will be stored in *ATTRIBUTES,
6882    represented as described above cp_parser_attributes.  
6883
6884    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6885    appears, and the entity that will be a friend is not going to be a
6886    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6887    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6888    friendship is granted might not be a class.  */
6889
6890 static tree
6891 cp_parser_decl_specifier_seq (parser, flags, attributes,
6892                               declares_class_or_enum)
6893      cp_parser *parser;
6894      cp_parser_flags flags;
6895      tree *attributes;
6896      bool *declares_class_or_enum;
6897 {
6898   tree decl_specs = NULL_TREE;
6899   bool friend_p = false;
6900   bool constructor_possible_p = true;
6901
6902   /* Assume no class or enumeration type is declared.  */
6903   *declares_class_or_enum = false;
6904
6905   /* Assume there are no attributes.  */
6906   *attributes = NULL_TREE;
6907
6908   /* Keep reading specifiers until there are no more to read.  */
6909   while (true)
6910     {
6911       tree decl_spec = NULL_TREE;
6912       bool constructor_p;
6913       cp_token *token;
6914
6915       /* Peek at the next token.  */
6916       token = cp_lexer_peek_token (parser->lexer);
6917       /* Handle attributes.  */
6918       if (token->keyword == RID_ATTRIBUTE)
6919         {
6920           /* Parse the attributes.  */
6921           decl_spec = cp_parser_attributes_opt (parser);
6922           /* Add them to the list.  */
6923           *attributes = chainon (*attributes, decl_spec);
6924           continue;
6925         }
6926       /* If the next token is an appropriate keyword, we can simply
6927          add it to the list.  */
6928       switch (token->keyword)
6929         {
6930         case RID_FRIEND:
6931           /* decl-specifier:
6932                friend  */
6933           friend_p = true;
6934           /* The representation of the specifier is simply the
6935              appropriate TREE_IDENTIFIER node.  */
6936           decl_spec = token->value;
6937           /* Consume the token.  */
6938           cp_lexer_consume_token (parser->lexer);
6939           break;
6940
6941           /* function-specifier:
6942                inline
6943                virtual
6944                explicit  */
6945         case RID_INLINE:
6946         case RID_VIRTUAL:
6947         case RID_EXPLICIT:
6948           decl_spec = cp_parser_function_specifier_opt (parser);
6949           break;
6950           
6951           /* decl-specifier:
6952                typedef  */
6953         case RID_TYPEDEF:
6954           /* The representation of the specifier is simply the
6955              appropriate TREE_IDENTIFIER node.  */
6956           decl_spec = token->value;
6957           /* Consume the token.  */
6958           cp_lexer_consume_token (parser->lexer);
6959           /* A constructor declarator cannot appear in a typedef.  */
6960           constructor_possible_p = false;
6961           break;
6962
6963           /* storage-class-specifier:
6964                auto
6965                register
6966                static
6967                extern
6968                mutable  
6969
6970              GNU Extension:
6971                thread  */
6972         case RID_AUTO:
6973         case RID_REGISTER:
6974         case RID_STATIC:
6975         case RID_EXTERN:
6976         case RID_MUTABLE:
6977         case RID_THREAD:
6978           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6979           break;
6980           
6981         default:
6982           break;
6983         }
6984
6985       /* Constructors are a special case.  The `S' in `S()' is not a
6986          decl-specifier; it is the beginning of the declarator.  */
6987       constructor_p = (!decl_spec 
6988                        && constructor_possible_p
6989                        && cp_parser_constructor_declarator_p (parser,
6990                                                               friend_p));
6991
6992       /* If we don't have a DECL_SPEC yet, then we must be looking at
6993          a type-specifier.  */
6994       if (!decl_spec && !constructor_p)
6995         {
6996           bool decl_spec_declares_class_or_enum;
6997           bool is_cv_qualifier;
6998
6999           decl_spec
7000             = cp_parser_type_specifier (parser, flags,
7001                                         friend_p,
7002                                         /*is_declaration=*/true,
7003                                         &decl_spec_declares_class_or_enum,
7004                                         &is_cv_qualifier);
7005
7006           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7007
7008           /* If this type-specifier referenced a user-defined type
7009              (a typedef, class-name, etc.), then we can't allow any
7010              more such type-specifiers henceforth.
7011
7012              [dcl.spec]
7013
7014              The longest sequence of decl-specifiers that could
7015              possibly be a type name is taken as the
7016              decl-specifier-seq of a declaration.  The sequence shall
7017              be self-consistent as described below.
7018
7019              [dcl.type]
7020
7021              As a general rule, at most one type-specifier is allowed
7022              in the complete decl-specifier-seq of a declaration.  The
7023              only exceptions are the following:
7024
7025              -- const or volatile can be combined with any other
7026                 type-specifier. 
7027
7028              -- signed or unsigned can be combined with char, long,
7029                 short, or int.
7030
7031              -- ..
7032
7033              Example:
7034
7035                typedef char* Pc;
7036                void g (const int Pc);
7037
7038              Here, Pc is *not* part of the decl-specifier seq; it's
7039              the declarator.  Therefore, once we see a type-specifier
7040              (other than a cv-qualifier), we forbid any additional
7041              user-defined types.  We *do* still allow things like `int
7042              int' to be considered a decl-specifier-seq, and issue the
7043              error message later.  */
7044           if (decl_spec && !is_cv_qualifier)
7045             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7046           /* A constructor declarator cannot follow a type-specifier.  */
7047           if (decl_spec)
7048             constructor_possible_p = false;
7049         }
7050
7051       /* If we still do not have a DECL_SPEC, then there are no more
7052          decl-specifiers.  */
7053       if (!decl_spec)
7054         {
7055           /* Issue an error message, unless the entire construct was
7056              optional.  */
7057           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7058             {
7059               cp_parser_error (parser, "expected decl specifier");
7060               return error_mark_node;
7061             }
7062
7063           break;
7064         }
7065
7066       /* Add the DECL_SPEC to the list of specifiers.  */
7067       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7068
7069       /* After we see one decl-specifier, further decl-specifiers are
7070          always optional.  */
7071       flags |= CP_PARSER_FLAGS_OPTIONAL;
7072     }
7073
7074   /* We have built up the DECL_SPECS in reverse order.  Return them in
7075      the correct order.  */
7076   return nreverse (decl_specs);
7077 }
7078
7079 /* Parse an (optional) storage-class-specifier. 
7080
7081    storage-class-specifier:
7082      auto
7083      register
7084      static
7085      extern
7086      mutable  
7087
7088    GNU Extension:
7089
7090    storage-class-specifier:
7091      thread
7092
7093    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7094    
7095 static tree
7096 cp_parser_storage_class_specifier_opt (parser)
7097      cp_parser *parser;
7098 {
7099   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7100     {
7101     case RID_AUTO:
7102     case RID_REGISTER:
7103     case RID_STATIC:
7104     case RID_EXTERN:
7105     case RID_MUTABLE:
7106     case RID_THREAD:
7107       /* Consume the token.  */
7108       return cp_lexer_consume_token (parser->lexer)->value;
7109
7110     default:
7111       return NULL_TREE;
7112     }
7113 }
7114
7115 /* Parse an (optional) function-specifier. 
7116
7117    function-specifier:
7118      inline
7119      virtual
7120      explicit
7121
7122    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7123    
7124 static tree
7125 cp_parser_function_specifier_opt (parser)
7126      cp_parser *parser;
7127 {
7128   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7129     {
7130     case RID_INLINE:
7131     case RID_VIRTUAL:
7132     case RID_EXPLICIT:
7133       /* Consume the token.  */
7134       return cp_lexer_consume_token (parser->lexer)->value;
7135
7136     default:
7137       return NULL_TREE;
7138     }
7139 }
7140
7141 /* Parse a linkage-specification.
7142
7143    linkage-specification:
7144      extern string-literal { declaration-seq [opt] }
7145      extern string-literal declaration  */
7146
7147 static void
7148 cp_parser_linkage_specification (parser)
7149      cp_parser *parser;
7150 {
7151   cp_token *token;
7152   tree linkage;
7153
7154   /* Look for the `extern' keyword.  */
7155   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7156
7157   /* Peek at the next token.  */
7158   token = cp_lexer_peek_token (parser->lexer);
7159   /* If it's not a string-literal, then there's a problem.  */
7160   if (!cp_parser_is_string_literal (token))
7161     {
7162       cp_parser_error (parser, "expected language-name");
7163       return;
7164     }
7165   /* Consume the token.  */
7166   cp_lexer_consume_token (parser->lexer);
7167
7168   /* Transform the literal into an identifier.  If the literal is a
7169      wide-character string, or contains embedded NULs, then we can't
7170      handle it as the user wants.  */
7171   if (token->type == CPP_WSTRING
7172       || (strlen (TREE_STRING_POINTER (token->value))
7173           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7174     {
7175       cp_parser_error (parser, "invalid linkage-specification");
7176       /* Assume C++ linkage.  */
7177       linkage = get_identifier ("c++");
7178     }
7179   /* If it's a simple string constant, things are easier.  */
7180   else
7181     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7182
7183   /* We're now using the new linkage.  */
7184   push_lang_context (linkage);
7185
7186   /* If the next token is a `{', then we're using the first
7187      production.  */
7188   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7189     {
7190       /* Consume the `{' token.  */
7191       cp_lexer_consume_token (parser->lexer);
7192       /* Parse the declarations.  */
7193       cp_parser_declaration_seq_opt (parser);
7194       /* Look for the closing `}'.  */
7195       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7196     }
7197   /* Otherwise, there's just one declaration.  */
7198   else
7199     {
7200       bool saved_in_unbraced_linkage_specification_p;
7201
7202       saved_in_unbraced_linkage_specification_p 
7203         = parser->in_unbraced_linkage_specification_p;
7204       parser->in_unbraced_linkage_specification_p = true;
7205       have_extern_spec = true;
7206       cp_parser_declaration (parser);
7207       have_extern_spec = false;
7208       parser->in_unbraced_linkage_specification_p 
7209         = saved_in_unbraced_linkage_specification_p;
7210     }
7211
7212   /* We're done with the linkage-specification.  */
7213   pop_lang_context ();
7214 }
7215
7216 /* Special member functions [gram.special] */
7217
7218 /* Parse a conversion-function-id.
7219
7220    conversion-function-id:
7221      operator conversion-type-id  
7222
7223    Returns an IDENTIFIER_NODE representing the operator.  */
7224
7225 static tree 
7226 cp_parser_conversion_function_id (parser)
7227      cp_parser *parser;
7228 {
7229   tree type;
7230   tree saved_scope;
7231   tree saved_qualifying_scope;
7232   tree saved_object_scope;
7233
7234   /* Look for the `operator' token.  */
7235   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7236     return error_mark_node;
7237   /* When we parse the conversion-type-id, the current scope will be
7238      reset.  However, we need that information in able to look up the
7239      conversion function later, so we save it here.  */
7240   saved_scope = parser->scope;
7241   saved_qualifying_scope = parser->qualifying_scope;
7242   saved_object_scope = parser->object_scope;
7243   /* We must enter the scope of the class so that the names of
7244      entities declared within the class are available in the
7245      conversion-type-id.  For example, consider:
7246
7247        struct S { 
7248          typedef int I;
7249          operator I();
7250        };
7251
7252        S::operator I() { ... }
7253
7254      In order to see that `I' is a type-name in the definition, we
7255      must be in the scope of `S'.  */
7256   if (saved_scope)
7257     push_scope (saved_scope);
7258   /* Parse the conversion-type-id.  */
7259   type = cp_parser_conversion_type_id (parser);
7260   /* Leave the scope of the class, if any.  */
7261   if (saved_scope)
7262     pop_scope (saved_scope);
7263   /* Restore the saved scope.  */
7264   parser->scope = saved_scope;
7265   parser->qualifying_scope = saved_qualifying_scope;
7266   parser->object_scope = saved_object_scope;
7267   /* If the TYPE is invalid, indicate failure.  */
7268   if (type == error_mark_node)
7269     return error_mark_node;
7270   return mangle_conv_op_name_for_type (type);
7271 }
7272
7273 /* Parse a conversion-type-id:
7274
7275    conversion-type-id:
7276      type-specifier-seq conversion-declarator [opt]
7277
7278    Returns the TYPE specified.  */
7279
7280 static tree
7281 cp_parser_conversion_type_id (parser)
7282      cp_parser *parser;
7283 {
7284   tree attributes;
7285   tree type_specifiers;
7286   tree declarator;
7287
7288   /* Parse the attributes.  */
7289   attributes = cp_parser_attributes_opt (parser);
7290   /* Parse the type-specifiers.  */
7291   type_specifiers = cp_parser_type_specifier_seq (parser);
7292   /* If that didn't work, stop.  */
7293   if (type_specifiers == error_mark_node)
7294     return error_mark_node;
7295   /* Parse the conversion-declarator.  */
7296   declarator = cp_parser_conversion_declarator_opt (parser);
7297
7298   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7299                          /*initialized=*/0, &attributes);
7300 }
7301
7302 /* Parse an (optional) conversion-declarator.
7303
7304    conversion-declarator:
7305      ptr-operator conversion-declarator [opt]  
7306
7307    Returns a representation of the declarator.  See
7308    cp_parser_declarator for details.  */
7309
7310 static tree
7311 cp_parser_conversion_declarator_opt (parser)
7312      cp_parser *parser;
7313 {
7314   enum tree_code code;
7315   tree class_type;
7316   tree cv_qualifier_seq;
7317
7318   /* We don't know if there's a ptr-operator next, or not.  */
7319   cp_parser_parse_tentatively (parser);
7320   /* Try the ptr-operator.  */
7321   code = cp_parser_ptr_operator (parser, &class_type, 
7322                                  &cv_qualifier_seq);
7323   /* If it worked, look for more conversion-declarators.  */
7324   if (cp_parser_parse_definitely (parser))
7325     {
7326      tree declarator;
7327
7328      /* Parse another optional declarator.  */
7329      declarator = cp_parser_conversion_declarator_opt (parser);
7330
7331      /* Create the representation of the declarator.  */
7332      if (code == INDIRECT_REF)
7333        declarator = make_pointer_declarator (cv_qualifier_seq,
7334                                              declarator);
7335      else
7336        declarator =  make_reference_declarator (cv_qualifier_seq,
7337                                                 declarator);
7338
7339      /* Handle the pointer-to-member case.  */
7340      if (class_type)
7341        declarator = build_nt (SCOPE_REF, class_type, declarator);
7342
7343      return declarator;
7344    }
7345
7346   return NULL_TREE;
7347 }
7348
7349 /* Parse an (optional) ctor-initializer.
7350
7351    ctor-initializer:
7352      : mem-initializer-list  
7353
7354    Returns TRUE iff the ctor-initializer was actually present.  */
7355
7356 static bool
7357 cp_parser_ctor_initializer_opt (parser)
7358      cp_parser *parser;
7359 {
7360   /* If the next token is not a `:', then there is no
7361      ctor-initializer.  */
7362   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7363     {
7364       /* Do default initialization of any bases and members.  */
7365       if (DECL_CONSTRUCTOR_P (current_function_decl))
7366         finish_mem_initializers (NULL_TREE);
7367
7368       return false;
7369     }
7370
7371   /* Consume the `:' token.  */
7372   cp_lexer_consume_token (parser->lexer);
7373   /* And the mem-initializer-list.  */
7374   cp_parser_mem_initializer_list (parser);
7375
7376   return true;
7377 }
7378
7379 /* Parse a mem-initializer-list.
7380
7381    mem-initializer-list:
7382      mem-initializer
7383      mem-initializer , mem-initializer-list  */
7384
7385 static void
7386 cp_parser_mem_initializer_list (parser)
7387      cp_parser *parser;
7388 {
7389   tree mem_initializer_list = NULL_TREE;
7390
7391   /* Let the semantic analysis code know that we are starting the
7392      mem-initializer-list.  */
7393   begin_mem_initializers ();
7394
7395   /* Loop through the list.  */
7396   while (true)
7397     {
7398       tree mem_initializer;
7399
7400       /* Parse the mem-initializer.  */
7401       mem_initializer = cp_parser_mem_initializer (parser);
7402       /* Add it to the list, unless it was erroneous.  */
7403       if (mem_initializer)
7404         {
7405           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7406           mem_initializer_list = mem_initializer;
7407         }
7408       /* If the next token is not a `,', we're done.  */
7409       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7410         break;
7411       /* Consume the `,' token.  */
7412       cp_lexer_consume_token (parser->lexer);
7413     }
7414
7415   /* Perform semantic analysis.  */
7416   finish_mem_initializers (mem_initializer_list);
7417 }
7418
7419 /* Parse a mem-initializer.
7420
7421    mem-initializer:
7422      mem-initializer-id ( expression-list [opt] )  
7423
7424    GNU extension:
7425   
7426    mem-initializer:
7427      ( expresion-list [opt] )
7428
7429    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7430    class) or FIELD_DECL (for a non-static data member) to initialize;
7431    the TREE_VALUE is the expression-list.  */
7432
7433 static tree
7434 cp_parser_mem_initializer (parser)
7435      cp_parser *parser;
7436 {
7437   tree mem_initializer_id;
7438   tree expression_list;
7439
7440   /* Find out what is being initialized.  */
7441   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7442     {
7443       pedwarn ("anachronistic old-style base class initializer");
7444       mem_initializer_id = NULL_TREE;
7445     }
7446   else
7447     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7448   /* Look for the opening `('.  */
7449   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7450   /* Parse the expression-list.  */
7451   if (cp_lexer_next_token_is_not (parser->lexer,
7452                                   CPP_CLOSE_PAREN))
7453     expression_list = cp_parser_expression_list (parser);
7454   else
7455     expression_list = void_type_node;
7456   /* Look for the closing `)'.  */
7457   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7458
7459   return expand_member_init (mem_initializer_id,
7460                              expression_list);
7461 }
7462
7463 /* Parse a mem-initializer-id.
7464
7465    mem-initializer-id:
7466      :: [opt] nested-name-specifier [opt] class-name
7467      identifier  
7468
7469    Returns a TYPE indicating the class to be initializer for the first
7470    production.  Returns an IDENTIFIER_NODE indicating the data member
7471    to be initialized for the second production.  */
7472
7473 static tree
7474 cp_parser_mem_initializer_id (parser)
7475      cp_parser *parser;
7476 {
7477   bool global_scope_p;
7478   bool nested_name_specifier_p;
7479   tree id;
7480
7481   /* Look for the optional `::' operator.  */
7482   global_scope_p 
7483     = (cp_parser_global_scope_opt (parser, 
7484                                    /*current_scope_valid_p=*/false) 
7485        != NULL_TREE);
7486   /* Look for the optional nested-name-specifier.  The simplest way to
7487      implement:
7488
7489        [temp.res]
7490
7491        The keyword `typename' is not permitted in a base-specifier or
7492        mem-initializer; in these contexts a qualified name that
7493        depends on a template-parameter is implicitly assumed to be a
7494        type name.
7495
7496      is to assume that we have seen the `typename' keyword at this
7497      point.  */
7498   nested_name_specifier_p 
7499     = (cp_parser_nested_name_specifier_opt (parser,
7500                                             /*typename_keyword_p=*/true,
7501                                             /*check_dependency_p=*/true,
7502                                             /*type_p=*/true)
7503        != NULL_TREE);
7504   /* If there is a `::' operator or a nested-name-specifier, then we
7505      are definitely looking for a class-name.  */
7506   if (global_scope_p || nested_name_specifier_p)
7507     return cp_parser_class_name (parser,
7508                                  /*typename_keyword_p=*/true,
7509                                  /*template_keyword_p=*/false,
7510                                  /*type_p=*/false,
7511                                  /*check_access_p=*/true,
7512                                  /*check_dependency_p=*/true,
7513                                  /*class_head_p=*/false);
7514   /* Otherwise, we could also be looking for an ordinary identifier.  */
7515   cp_parser_parse_tentatively (parser);
7516   /* Try a class-name.  */
7517   id = cp_parser_class_name (parser, 
7518                              /*typename_keyword_p=*/true,
7519                              /*template_keyword_p=*/false,
7520                              /*type_p=*/false,
7521                              /*check_access_p=*/true,
7522                              /*check_dependency_p=*/true,
7523                              /*class_head_p=*/false);
7524   /* If we found one, we're done.  */
7525   if (cp_parser_parse_definitely (parser))
7526     return id;
7527   /* Otherwise, look for an ordinary identifier.  */
7528   return cp_parser_identifier (parser);
7529 }
7530
7531 /* Overloading [gram.over] */
7532
7533 /* Parse an operator-function-id.
7534
7535    operator-function-id:
7536      operator operator  
7537
7538    Returns an IDENTIFIER_NODE for the operator which is a
7539    human-readable spelling of the identifier, e.g., `operator +'.  */
7540
7541 static tree 
7542 cp_parser_operator_function_id (parser)
7543      cp_parser *parser;
7544 {
7545   /* Look for the `operator' keyword.  */
7546   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7547     return error_mark_node;
7548   /* And then the name of the operator itself.  */
7549   return cp_parser_operator (parser);
7550 }
7551
7552 /* Parse an operator.
7553
7554    operator:
7555      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7556      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7557      || ++ -- , ->* -> () []
7558
7559    GNU Extensions:
7560    
7561    operator:
7562      <? >? <?= >?=
7563
7564    Returns an IDENTIFIER_NODE for the operator which is a
7565    human-readable spelling of the identifier, e.g., `operator +'.  */
7566    
7567 static tree
7568 cp_parser_operator (parser)
7569      cp_parser *parser;
7570 {
7571   tree id = NULL_TREE;
7572   cp_token *token;
7573
7574   /* Peek at the next token.  */
7575   token = cp_lexer_peek_token (parser->lexer);
7576   /* Figure out which operator we have.  */
7577   switch (token->type)
7578     {
7579     case CPP_KEYWORD:
7580       {
7581         enum tree_code op;
7582
7583         /* The keyword should be either `new' or `delete'.  */
7584         if (token->keyword == RID_NEW)
7585           op = NEW_EXPR;
7586         else if (token->keyword == RID_DELETE)
7587           op = DELETE_EXPR;
7588         else
7589           break;
7590
7591         /* Consume the `new' or `delete' token.  */
7592         cp_lexer_consume_token (parser->lexer);
7593
7594         /* Peek at the next token.  */
7595         token = cp_lexer_peek_token (parser->lexer);
7596         /* If it's a `[' token then this is the array variant of the
7597            operator.  */
7598         if (token->type == CPP_OPEN_SQUARE)
7599           {
7600             /* Consume the `[' token.  */
7601             cp_lexer_consume_token (parser->lexer);
7602             /* Look for the `]' token.  */
7603             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7604             id = ansi_opname (op == NEW_EXPR 
7605                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7606           }
7607         /* Otherwise, we have the non-array variant.  */
7608         else
7609           id = ansi_opname (op);
7610
7611         return id;
7612       }
7613
7614     case CPP_PLUS:
7615       id = ansi_opname (PLUS_EXPR);
7616       break;
7617
7618     case CPP_MINUS:
7619       id = ansi_opname (MINUS_EXPR);
7620       break;
7621
7622     case CPP_MULT:
7623       id = ansi_opname (MULT_EXPR);
7624       break;
7625
7626     case CPP_DIV:
7627       id = ansi_opname (TRUNC_DIV_EXPR);
7628       break;
7629
7630     case CPP_MOD:
7631       id = ansi_opname (TRUNC_MOD_EXPR);
7632       break;
7633
7634     case CPP_XOR:
7635       id = ansi_opname (BIT_XOR_EXPR);
7636       break;
7637
7638     case CPP_AND:
7639       id = ansi_opname (BIT_AND_EXPR);
7640       break;
7641
7642     case CPP_OR:
7643       id = ansi_opname (BIT_IOR_EXPR);
7644       break;
7645
7646     case CPP_COMPL:
7647       id = ansi_opname (BIT_NOT_EXPR);
7648       break;
7649       
7650     case CPP_NOT:
7651       id = ansi_opname (TRUTH_NOT_EXPR);
7652       break;
7653
7654     case CPP_EQ:
7655       id = ansi_assopname (NOP_EXPR);
7656       break;
7657
7658     case CPP_LESS:
7659       id = ansi_opname (LT_EXPR);
7660       break;
7661
7662     case CPP_GREATER:
7663       id = ansi_opname (GT_EXPR);
7664       break;
7665
7666     case CPP_PLUS_EQ:
7667       id = ansi_assopname (PLUS_EXPR);
7668       break;
7669
7670     case CPP_MINUS_EQ:
7671       id = ansi_assopname (MINUS_EXPR);
7672       break;
7673
7674     case CPP_MULT_EQ:
7675       id = ansi_assopname (MULT_EXPR);
7676       break;
7677
7678     case CPP_DIV_EQ:
7679       id = ansi_assopname (TRUNC_DIV_EXPR);
7680       break;
7681
7682     case CPP_MOD_EQ:
7683       id = ansi_assopname (TRUNC_MOD_EXPR);
7684       break;
7685
7686     case CPP_XOR_EQ:
7687       id = ansi_assopname (BIT_XOR_EXPR);
7688       break;
7689
7690     case CPP_AND_EQ:
7691       id = ansi_assopname (BIT_AND_EXPR);
7692       break;
7693
7694     case CPP_OR_EQ:
7695       id = ansi_assopname (BIT_IOR_EXPR);
7696       break;
7697
7698     case CPP_LSHIFT:
7699       id = ansi_opname (LSHIFT_EXPR);
7700       break;
7701
7702     case CPP_RSHIFT:
7703       id = ansi_opname (RSHIFT_EXPR);
7704       break;
7705
7706     case CPP_LSHIFT_EQ:
7707       id = ansi_assopname (LSHIFT_EXPR);
7708       break;
7709
7710     case CPP_RSHIFT_EQ:
7711       id = ansi_assopname (RSHIFT_EXPR);
7712       break;
7713
7714     case CPP_EQ_EQ:
7715       id = ansi_opname (EQ_EXPR);
7716       break;
7717
7718     case CPP_NOT_EQ:
7719       id = ansi_opname (NE_EXPR);
7720       break;
7721
7722     case CPP_LESS_EQ:
7723       id = ansi_opname (LE_EXPR);
7724       break;
7725
7726     case CPP_GREATER_EQ:
7727       id = ansi_opname (GE_EXPR);
7728       break;
7729
7730     case CPP_AND_AND:
7731       id = ansi_opname (TRUTH_ANDIF_EXPR);
7732       break;
7733
7734     case CPP_OR_OR:
7735       id = ansi_opname (TRUTH_ORIF_EXPR);
7736       break;
7737       
7738     case CPP_PLUS_PLUS:
7739       id = ansi_opname (POSTINCREMENT_EXPR);
7740       break;
7741
7742     case CPP_MINUS_MINUS:
7743       id = ansi_opname (PREDECREMENT_EXPR);
7744       break;
7745
7746     case CPP_COMMA:
7747       id = ansi_opname (COMPOUND_EXPR);
7748       break;
7749
7750     case CPP_DEREF_STAR:
7751       id = ansi_opname (MEMBER_REF);
7752       break;
7753
7754     case CPP_DEREF:
7755       id = ansi_opname (COMPONENT_REF);
7756       break;
7757
7758     case CPP_OPEN_PAREN:
7759       /* Consume the `('.  */
7760       cp_lexer_consume_token (parser->lexer);
7761       /* Look for the matching `)'.  */
7762       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7763       return ansi_opname (CALL_EXPR);
7764
7765     case CPP_OPEN_SQUARE:
7766       /* Consume the `['.  */
7767       cp_lexer_consume_token (parser->lexer);
7768       /* Look for the matching `]'.  */
7769       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7770       return ansi_opname (ARRAY_REF);
7771
7772       /* Extensions.  */
7773     case CPP_MIN:
7774       id = ansi_opname (MIN_EXPR);
7775       break;
7776
7777     case CPP_MAX:
7778       id = ansi_opname (MAX_EXPR);
7779       break;
7780
7781     case CPP_MIN_EQ:
7782       id = ansi_assopname (MIN_EXPR);
7783       break;
7784
7785     case CPP_MAX_EQ:
7786       id = ansi_assopname (MAX_EXPR);
7787       break;
7788
7789     default:
7790       /* Anything else is an error.  */
7791       break;
7792     }
7793
7794   /* If we have selected an identifier, we need to consume the
7795      operator token.  */
7796   if (id)
7797     cp_lexer_consume_token (parser->lexer);
7798   /* Otherwise, no valid operator name was present.  */
7799   else
7800     {
7801       cp_parser_error (parser, "expected operator");
7802       id = error_mark_node;
7803     }
7804
7805   return id;
7806 }
7807
7808 /* Parse a template-declaration.
7809
7810    template-declaration:
7811      export [opt] template < template-parameter-list > declaration  
7812
7813    If MEMBER_P is TRUE, this template-declaration occurs within a
7814    class-specifier.  
7815
7816    The grammar rule given by the standard isn't correct.  What
7817    is really meant is:
7818
7819    template-declaration:
7820      export [opt] template-parameter-list-seq 
7821        decl-specifier-seq [opt] init-declarator [opt] ;
7822      export [opt] template-parameter-list-seq 
7823        function-definition
7824
7825    template-parameter-list-seq:
7826      template-parameter-list-seq [opt]
7827      template < template-parameter-list >  */
7828
7829 static void
7830 cp_parser_template_declaration (parser, member_p)
7831      cp_parser *parser;
7832      bool member_p;
7833 {
7834   /* Check for `export'.  */
7835   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7836     {
7837       /* Consume the `export' token.  */
7838       cp_lexer_consume_token (parser->lexer);
7839       /* Warn that we do not support `export'.  */
7840       warning ("keyword `export' not implemented, and will be ignored");
7841     }
7842
7843   cp_parser_template_declaration_after_export (parser, member_p);
7844 }
7845
7846 /* Parse a template-parameter-list.
7847
7848    template-parameter-list:
7849      template-parameter
7850      template-parameter-list , template-parameter
7851
7852    Returns a TREE_LIST.  Each node represents a template parameter.
7853    The nodes are connected via their TREE_CHAINs.  */
7854
7855 static tree
7856 cp_parser_template_parameter_list (parser)
7857      cp_parser *parser;
7858 {
7859   tree parameter_list = NULL_TREE;
7860
7861   while (true)
7862     {
7863       tree parameter;
7864       cp_token *token;
7865
7866       /* Parse the template-parameter.  */
7867       parameter = cp_parser_template_parameter (parser);
7868       /* Add it to the list.  */
7869       parameter_list = process_template_parm (parameter_list,
7870                                               parameter);
7871
7872       /* Peek at the next token.  */
7873       token = cp_lexer_peek_token (parser->lexer);
7874       /* If it's not a `,', we're done.  */
7875       if (token->type != CPP_COMMA)
7876         break;
7877       /* Otherwise, consume the `,' token.  */
7878       cp_lexer_consume_token (parser->lexer);
7879     }
7880
7881   return parameter_list;
7882 }
7883
7884 /* Parse a template-parameter.
7885
7886    template-parameter:
7887      type-parameter
7888      parameter-declaration
7889
7890    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7891    TREE_PURPOSE is the default value, if any.  */
7892
7893 static tree
7894 cp_parser_template_parameter (parser)
7895      cp_parser *parser;
7896 {
7897   cp_token *token;
7898
7899   /* Peek at the next token.  */
7900   token = cp_lexer_peek_token (parser->lexer);
7901   /* If it is `class' or `template', we have a type-parameter.  */
7902   if (token->keyword == RID_TEMPLATE)
7903     return cp_parser_type_parameter (parser);
7904   /* If it is `class' or `typename' we do not know yet whether it is a
7905      type parameter or a non-type parameter.  Consider:
7906
7907        template <typename T, typename T::X X> ...
7908
7909      or:
7910      
7911        template <class C, class D*> ...
7912
7913      Here, the first parameter is a type parameter, and the second is
7914      a non-type parameter.  We can tell by looking at the token after
7915      the identifier -- if it is a `,', `=', or `>' then we have a type
7916      parameter.  */
7917   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7918     {
7919       /* Peek at the token after `class' or `typename'.  */
7920       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7921       /* If it's an identifier, skip it.  */
7922       if (token->type == CPP_NAME)
7923         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7924       /* Now, see if the token looks like the end of a template
7925          parameter.  */
7926       if (token->type == CPP_COMMA 
7927           || token->type == CPP_EQ
7928           || token->type == CPP_GREATER)
7929         return cp_parser_type_parameter (parser);
7930     }
7931
7932   /* Otherwise, it is a non-type parameter.  
7933
7934      [temp.param]
7935
7936      When parsing a default template-argument for a non-type
7937      template-parameter, the first non-nested `>' is taken as the end
7938      of the template parameter-list rather than a greater-than
7939      operator.  */
7940   return 
7941     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7942 }
7943
7944 /* Parse a type-parameter.
7945
7946    type-parameter:
7947      class identifier [opt]
7948      class identifier [opt] = type-id
7949      typename identifier [opt]
7950      typename identifier [opt] = type-id
7951      template < template-parameter-list > class identifier [opt]
7952      template < template-parameter-list > class identifier [opt] 
7953        = id-expression  
7954
7955    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7956    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7957    the declaration of the parameter.  */
7958
7959 static tree
7960 cp_parser_type_parameter (parser)
7961      cp_parser *parser;
7962 {
7963   cp_token *token;
7964   tree parameter;
7965
7966   /* Look for a keyword to tell us what kind of parameter this is.  */
7967   token = cp_parser_require (parser, CPP_KEYWORD, 
7968                              "expected `class', `typename', or `template'");
7969   if (!token)
7970     return error_mark_node;
7971
7972   switch (token->keyword)
7973     {
7974     case RID_CLASS:
7975     case RID_TYPENAME:
7976       {
7977         tree identifier;
7978         tree default_argument;
7979
7980         /* If the next token is an identifier, then it names the
7981            parameter.  */
7982         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7983           identifier = cp_parser_identifier (parser);
7984         else
7985           identifier = NULL_TREE;
7986
7987         /* Create the parameter.  */
7988         parameter = finish_template_type_parm (class_type_node, identifier);
7989
7990         /* If the next token is an `=', we have a default argument.  */
7991         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7992           {
7993             /* Consume the `=' token.  */
7994             cp_lexer_consume_token (parser->lexer);
7995             /* Parse the default-argumen.  */
7996             default_argument = cp_parser_type_id (parser);
7997           }
7998         else
7999           default_argument = NULL_TREE;
8000
8001         /* Create the combined representation of the parameter and the
8002            default argument.  */
8003         parameter = build_tree_list (default_argument, 
8004                                      parameter);
8005       }
8006       break;
8007
8008     case RID_TEMPLATE:
8009       {
8010         tree parameter_list;
8011         tree identifier;
8012         tree default_argument;
8013
8014         /* Look for the `<'.  */
8015         cp_parser_require (parser, CPP_LESS, "`<'");
8016         /* Parse the template-parameter-list.  */
8017         begin_template_parm_list ();
8018         parameter_list 
8019           = cp_parser_template_parameter_list (parser);
8020         parameter_list = end_template_parm_list (parameter_list);
8021         /* Look for the `>'.  */
8022         cp_parser_require (parser, CPP_GREATER, "`>'");
8023         /* Look for the `class' keyword.  */
8024         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8025         /* If the next token is an `=', then there is a
8026            default-argument.  If the next token is a `>', we are at
8027            the end of the parameter-list.  If the next token is a `,',
8028            then we are at the end of this parameter.  */
8029         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8030             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8031             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8032           identifier = cp_parser_identifier (parser);
8033         else
8034           identifier = NULL_TREE;
8035         /* Create the template parameter.  */
8036         parameter = finish_template_template_parm (class_type_node,
8037                                                    identifier);
8038                                                    
8039         /* If the next token is an `=', then there is a
8040            default-argument.  */
8041         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8042           {
8043             /* Consume the `='.  */
8044             cp_lexer_consume_token (parser->lexer);
8045             /* Parse the id-expression.  */
8046             default_argument 
8047               = cp_parser_id_expression (parser,
8048                                          /*template_keyword_p=*/false,
8049                                          /*check_dependency_p=*/true,
8050                                          /*template_p=*/NULL);
8051             /* Look up the name.  */
8052             default_argument 
8053               = cp_parser_lookup_name_simple (parser, default_argument);
8054             /* See if the default argument is valid.  */
8055             default_argument
8056               = check_template_template_default_arg (default_argument);
8057           }
8058         else
8059           default_argument = NULL_TREE;
8060
8061         /* Create the combined representation of the parameter and the
8062            default argument.  */
8063         parameter =  build_tree_list (default_argument, 
8064                                       parameter);
8065       }
8066       break;
8067
8068     default:
8069       /* Anything else is an error.  */
8070       cp_parser_error (parser,
8071                        "expected `class', `typename', or `template'");
8072       parameter = error_mark_node;
8073     }
8074   
8075   return parameter;
8076 }
8077
8078 /* Parse a template-id.
8079
8080    template-id:
8081      template-name < template-argument-list [opt] >
8082
8083    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8084    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8085    returned.  Otherwise, if the template-name names a function, or set
8086    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8087    names a class, returns a TYPE_DECL for the specialization.  
8088
8089    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8090    uninstantiated templates.  */
8091
8092 static tree
8093 cp_parser_template_id (cp_parser *parser, 
8094                        bool template_keyword_p, 
8095                        bool check_dependency_p)
8096 {
8097   tree template;
8098   tree arguments;
8099   tree saved_scope;
8100   tree saved_qualifying_scope;
8101   tree saved_object_scope;
8102   tree template_id;
8103   bool saved_greater_than_is_operator_p;
8104   ptrdiff_t start_of_id;
8105   tree access_check = NULL_TREE;
8106   cp_token *next_token;
8107
8108   /* If the next token corresponds to a template-id, there is no need
8109      to reparse it.  */
8110   next_token = cp_lexer_peek_token (parser->lexer);
8111   if (next_token->type == CPP_TEMPLATE_ID)
8112     {
8113       tree value;
8114       tree check;
8115
8116       /* Get the stored value.  */
8117       value = cp_lexer_consume_token (parser->lexer)->value;
8118       /* Perform any access checks that were deferred.  */
8119       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8120         cp_parser_defer_access_check (parser, 
8121                                       TREE_PURPOSE (check),
8122                                       TREE_VALUE (check));
8123       /* Return the stored value.  */
8124       return TREE_VALUE (value);
8125     }
8126
8127   /* Avoid performing name lookup if there is no possibility of
8128      finding a template-id.  */
8129   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8130       || (next_token->type == CPP_NAME
8131           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
8132     {
8133       cp_parser_error (parser, "expected template-id");
8134       return error_mark_node;
8135     }
8136
8137   /* Remember where the template-id starts.  */
8138   if (cp_parser_parsing_tentatively (parser)
8139       && !cp_parser_committed_to_tentative_parse (parser))
8140     {
8141       next_token = cp_lexer_peek_token (parser->lexer);
8142       start_of_id = cp_lexer_token_difference (parser->lexer,
8143                                                parser->lexer->first_token,
8144                                                next_token);
8145       access_check = parser->context->deferred_access_checks;
8146     }
8147   else
8148     start_of_id = -1;
8149
8150   /* Parse the template-name.  */
8151   template = cp_parser_template_name (parser, template_keyword_p,
8152                                       check_dependency_p);
8153   if (template == error_mark_node)
8154     return error_mark_node;
8155
8156   /* Look for the `<' that starts the template-argument-list.  */
8157   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8158     return error_mark_node;
8159
8160   /* [temp.names]
8161
8162      When parsing a template-id, the first non-nested `>' is taken as
8163      the end of the template-argument-list rather than a greater-than
8164      operator.  */
8165   saved_greater_than_is_operator_p 
8166     = parser->greater_than_is_operator_p;
8167   parser->greater_than_is_operator_p = false;
8168   /* Parsing the argument list may modify SCOPE, so we save it
8169      here.  */
8170   saved_scope = parser->scope;
8171   saved_qualifying_scope = parser->qualifying_scope;
8172   saved_object_scope = parser->object_scope;
8173   /* Parse the template-argument-list itself.  */
8174   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8175     arguments = NULL_TREE;
8176   else
8177     arguments = cp_parser_template_argument_list (parser);
8178   /* Look for the `>' that ends the template-argument-list.  */
8179   cp_parser_require (parser, CPP_GREATER, "`>'");
8180   /* The `>' token might be a greater-than operator again now.  */
8181   parser->greater_than_is_operator_p 
8182     = saved_greater_than_is_operator_p;
8183   /* Restore the SAVED_SCOPE.  */
8184   parser->scope = saved_scope;
8185   parser->qualifying_scope = saved_qualifying_scope;
8186   parser->object_scope = saved_object_scope;
8187
8188   /* Build a representation of the specialization.  */
8189   if (TREE_CODE (template) == IDENTIFIER_NODE)
8190     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8191   else if (DECL_CLASS_TEMPLATE_P (template)
8192            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8193     template_id 
8194       = finish_template_type (template, arguments, 
8195                               cp_lexer_next_token_is (parser->lexer, 
8196                                                       CPP_SCOPE));
8197   else
8198     {
8199       /* If it's not a class-template or a template-template, it should be
8200          a function-template.  */
8201       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8202                            || TREE_CODE (template) == OVERLOAD
8203                            || BASELINK_P (template)),
8204                           20010716);
8205       
8206       template_id = lookup_template_function (template, arguments);
8207     }
8208   
8209   /* If parsing tentatively, replace the sequence of tokens that makes
8210      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8211      should we re-parse the token stream, we will not have to repeat
8212      the effort required to do the parse, nor will we issue duplicate
8213      error messages about problems during instantiation of the
8214      template.  */
8215   if (start_of_id >= 0)
8216     {
8217       cp_token *token;
8218       tree c;
8219
8220       /* Find the token that corresponds to the start of the
8221          template-id.  */
8222       token = cp_lexer_advance_token (parser->lexer, 
8223                                       parser->lexer->first_token,
8224                                       start_of_id);
8225
8226       /* Remember the access checks associated with this
8227          nested-name-specifier.  */
8228       c = parser->context->deferred_access_checks;
8229       if (c == access_check)
8230         access_check = NULL_TREE;
8231       else
8232         {
8233           while (TREE_CHAIN (c) != access_check)
8234             c = TREE_CHAIN (c);
8235           access_check = parser->context->deferred_access_checks;
8236           parser->context->deferred_access_checks = TREE_CHAIN (c);
8237           TREE_CHAIN (c) = NULL_TREE;
8238         }
8239
8240       /* Reset the contents of the START_OF_ID token.  */
8241       token->type = CPP_TEMPLATE_ID;
8242       token->value = build_tree_list (access_check, template_id);
8243       token->keyword = RID_MAX;
8244       /* Purge all subsequent tokens.  */
8245       cp_lexer_purge_tokens_after (parser->lexer, token);
8246     }
8247
8248   return template_id;
8249 }
8250
8251 /* Parse a template-name.
8252
8253    template-name:
8254      identifier
8255  
8256    The standard should actually say:
8257
8258    template-name:
8259      identifier
8260      operator-function-id
8261      conversion-function-id
8262
8263    A defect report has been filed about this issue.
8264
8265    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8266    `template' keyword, in a construction like:
8267
8268      T::template f<3>()
8269
8270    In that case `f' is taken to be a template-name, even though there
8271    is no way of knowing for sure.
8272
8273    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8274    name refers to a set of overloaded functions, at least one of which
8275    is a template, or an IDENTIFIER_NODE with the name of the template,
8276    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8277    names are looked up inside uninstantiated templates.  */
8278
8279 static tree
8280 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8281      cp_parser *parser;
8282      bool template_keyword_p;
8283      bool check_dependency_p;
8284 {
8285   tree identifier;
8286   tree decl;
8287   tree fns;
8288
8289   /* If the next token is `operator', then we have either an
8290      operator-function-id or a conversion-function-id.  */
8291   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8292     {
8293       /* We don't know whether we're looking at an
8294          operator-function-id or a conversion-function-id.  */
8295       cp_parser_parse_tentatively (parser);
8296       /* Try an operator-function-id.  */
8297       identifier = cp_parser_operator_function_id (parser);
8298       /* If that didn't work, try a conversion-function-id.  */
8299       if (!cp_parser_parse_definitely (parser))
8300         identifier = cp_parser_conversion_function_id (parser);
8301     }
8302   /* Look for the identifier.  */
8303   else
8304     identifier = cp_parser_identifier (parser);
8305   
8306   /* If we didn't find an identifier, we don't have a template-id.  */
8307   if (identifier == error_mark_node)
8308     return error_mark_node;
8309
8310   /* If the name immediately followed the `template' keyword, then it
8311      is a template-name.  However, if the next token is not `<', then
8312      we do not treat it as a template-name, since it is not being used
8313      as part of a template-id.  This enables us to handle constructs
8314      like:
8315
8316        template <typename T> struct S { S(); };
8317        template <typename T> S<T>::S();
8318
8319      correctly.  We would treat `S' as a template -- if it were `S<T>'
8320      -- but we do not if there is no `<'.  */
8321   if (template_keyword_p && processing_template_decl
8322       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8323     return identifier;
8324
8325   /* Look up the name.  */
8326   decl = cp_parser_lookup_name (parser, identifier,
8327                                 /*check_access=*/true,
8328                                 /*is_type=*/false,
8329                                 /*is_namespace=*/false,
8330                                 check_dependency_p);
8331   decl = maybe_get_template_decl_from_type_decl (decl);
8332
8333   /* If DECL is a template, then the name was a template-name.  */
8334   if (TREE_CODE (decl) == TEMPLATE_DECL)
8335     ;
8336   else 
8337     {
8338       /* The standard does not explicitly indicate whether a name that
8339          names a set of overloaded declarations, some of which are
8340          templates, is a template-name.  However, such a name should
8341          be a template-name; otherwise, there is no way to form a
8342          template-id for the overloaded templates.  */
8343       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8344       if (TREE_CODE (fns) == OVERLOAD)
8345         {
8346           tree fn;
8347           
8348           for (fn = fns; fn; fn = OVL_NEXT (fn))
8349             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8350               break;
8351         }
8352       else
8353         {
8354           /* Otherwise, the name does not name a template.  */
8355           cp_parser_error (parser, "expected template-name");
8356           return error_mark_node;
8357         }
8358     }
8359
8360   /* If DECL is dependent, and refers to a function, then just return
8361      its name; we will look it up again during template instantiation.  */
8362   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8363     {
8364       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8365       if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8366         return identifier;
8367     }
8368
8369   return decl;
8370 }
8371
8372 /* Parse a template-argument-list.
8373
8374    template-argument-list:
8375      template-argument
8376      template-argument-list , template-argument
8377
8378    Returns a TREE_LIST representing the arguments, in the order they
8379    appeared.  The TREE_VALUE of each node is a representation of the
8380    argument.  */
8381
8382 static tree
8383 cp_parser_template_argument_list (parser)
8384      cp_parser *parser;
8385 {
8386   tree arguments = NULL_TREE;
8387
8388   while (true)
8389     {
8390       tree argument;
8391
8392       /* Parse the template-argument.  */
8393       argument = cp_parser_template_argument (parser);
8394       /* Add it to the list.  */
8395       arguments = tree_cons (NULL_TREE, argument, arguments);
8396       /* If it is not a `,', then there are no more arguments.  */
8397       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8398         break;
8399       /* Otherwise, consume the ','.  */
8400       cp_lexer_consume_token (parser->lexer);
8401     }
8402
8403   /* We built up the arguments in reverse order.  */
8404   return nreverse (arguments);
8405 }
8406
8407 /* Parse a template-argument.
8408
8409    template-argument:
8410      assignment-expression
8411      type-id
8412      id-expression
8413
8414    The representation is that of an assignment-expression, type-id, or
8415    id-expression -- except that the qualified id-expression is
8416    evaluated, so that the value returned is either a DECL or an
8417    OVERLOAD.  */
8418
8419 static tree
8420 cp_parser_template_argument (parser)
8421      cp_parser *parser;
8422 {
8423   tree argument;
8424   bool template_p;
8425
8426   /* There's really no way to know what we're looking at, so we just
8427      try each alternative in order.  
8428
8429        [temp.arg]
8430
8431        In a template-argument, an ambiguity between a type-id and an
8432        expression is resolved to a type-id, regardless of the form of
8433        the corresponding template-parameter.  
8434
8435      Therefore, we try a type-id first.  */
8436   cp_parser_parse_tentatively (parser);
8437   argument = cp_parser_type_id (parser);
8438   /* If the next token isn't a `,' or a `>', then this argument wasn't
8439      really finished.  */
8440   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8441       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8442     cp_parser_error (parser, "expected template-argument");
8443   /* If that worked, we're done.  */
8444   if (cp_parser_parse_definitely (parser))
8445     return argument;
8446   /* We're still not sure what the argument will be.  */
8447   cp_parser_parse_tentatively (parser);
8448   /* Try a template.  */
8449   argument = cp_parser_id_expression (parser, 
8450                                       /*template_keyword_p=*/false,
8451                                       /*check_dependency_p=*/true,
8452                                       &template_p);
8453   /* If the next token isn't a `,' or a `>', then this argument wasn't
8454      really finished.  */
8455   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8456       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8457     cp_parser_error (parser, "expected template-argument");
8458   if (!cp_parser_error_occurred (parser))
8459     {
8460       /* Figure out what is being referred to.  */
8461       argument = cp_parser_lookup_name_simple (parser, argument);
8462       if (template_p)
8463         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8464                                                 TREE_OPERAND (argument, 1),
8465                                                 tf_error | tf_parsing);
8466       else if (TREE_CODE (argument) != TEMPLATE_DECL)
8467         cp_parser_error (parser, "expected template-name");
8468     }
8469   if (cp_parser_parse_definitely (parser))
8470     return argument;
8471   /* It must be an assignment-expression.  */
8472   return cp_parser_assignment_expression (parser);
8473 }
8474
8475 /* Parse an explicit-instantiation.
8476
8477    explicit-instantiation:
8478      template declaration  
8479
8480    Although the standard says `declaration', what it really means is:
8481
8482    explicit-instantiation:
8483      template decl-specifier-seq [opt] declarator [opt] ; 
8484
8485    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8486    supposed to be allowed.  A defect report has been filed about this
8487    issue.  
8488
8489    GNU Extension:
8490   
8491    explicit-instantiation:
8492      storage-class-specifier template 
8493        decl-specifier-seq [opt] declarator [opt] ;
8494      function-specifier template 
8495        decl-specifier-seq [opt] declarator [opt] ;  */
8496
8497 static void
8498 cp_parser_explicit_instantiation (parser)
8499      cp_parser *parser;
8500 {
8501   bool declares_class_or_enum;
8502   tree decl_specifiers;
8503   tree attributes;
8504   tree extension_specifier = NULL_TREE;
8505
8506   /* Look for an (optional) storage-class-specifier or
8507      function-specifier.  */
8508   if (cp_parser_allow_gnu_extensions_p (parser))
8509     {
8510       extension_specifier 
8511         = cp_parser_storage_class_specifier_opt (parser);
8512       if (!extension_specifier)
8513         extension_specifier = cp_parser_function_specifier_opt (parser);
8514     }
8515
8516   /* Look for the `template' keyword.  */
8517   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8518   /* Let the front end know that we are processing an explicit
8519      instantiation.  */
8520   begin_explicit_instantiation ();
8521   /* [temp.explicit] says that we are supposed to ignore access
8522      control while processing explicit instantiation directives.  */
8523   scope_chain->check_access = 0;
8524   /* Parse a decl-specifier-seq.  */
8525   decl_specifiers 
8526     = cp_parser_decl_specifier_seq (parser,
8527                                     CP_PARSER_FLAGS_OPTIONAL,
8528                                     &attributes,
8529                                     &declares_class_or_enum);
8530   /* If there was exactly one decl-specifier, and it declared a class,
8531      and there's no declarator, then we have an explicit type
8532      instantiation.  */
8533   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8534     {
8535       tree type;
8536
8537       type = check_tag_decl (decl_specifiers);
8538       if (type)
8539         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8540     }
8541   else
8542     {
8543       tree declarator;
8544       tree decl;
8545
8546       /* Parse the declarator.  */
8547       declarator 
8548         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8549                                 /*ctor_dtor_or_conv_p=*/NULL);
8550       decl = grokdeclarator (declarator, decl_specifiers, 
8551                              NORMAL, 0, NULL);
8552       /* Do the explicit instantiation.  */
8553       do_decl_instantiation (decl, extension_specifier);
8554     }
8555   /* We're done with the instantiation.  */
8556   end_explicit_instantiation ();
8557   /* Trun access control back on.  */
8558   scope_chain->check_access = flag_access_control;
8559
8560   /* Look for the trailing `;'.  */
8561   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8562 }
8563
8564 /* Parse an explicit-specialization.
8565
8566    explicit-specialization:
8567      template < > declaration  
8568
8569    Although the standard says `declaration', what it really means is:
8570
8571    explicit-specialization:
8572      template <> decl-specifier [opt] init-declarator [opt] ;
8573      template <> function-definition 
8574      template <> explicit-specialization
8575      template <> template-declaration  */
8576
8577 static void
8578 cp_parser_explicit_specialization (parser)
8579      cp_parser *parser;
8580 {
8581   /* Look for the `template' keyword.  */
8582   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8583   /* Look for the `<'.  */
8584   cp_parser_require (parser, CPP_LESS, "`<'");
8585   /* Look for the `>'.  */
8586   cp_parser_require (parser, CPP_GREATER, "`>'");
8587   /* We have processed another parameter list.  */
8588   ++parser->num_template_parameter_lists;
8589   /* Let the front end know that we are beginning a specialization.  */
8590   begin_specialization ();
8591
8592   /* If the next keyword is `template', we need to figure out whether
8593      or not we're looking a template-declaration.  */
8594   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8595     {
8596       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8597           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8598         cp_parser_template_declaration_after_export (parser,
8599                                                      /*member_p=*/false);
8600       else
8601         cp_parser_explicit_specialization (parser);
8602     }
8603   else
8604     /* Parse the dependent declaration.  */
8605     cp_parser_single_declaration (parser, 
8606                                   /*member_p=*/false,
8607                                   /*friend_p=*/NULL);
8608
8609   /* We're done with the specialization.  */
8610   end_specialization ();
8611   /* We're done with this parameter list.  */
8612   --parser->num_template_parameter_lists;
8613 }
8614
8615 /* Parse a type-specifier.
8616
8617    type-specifier:
8618      simple-type-specifier
8619      class-specifier
8620      enum-specifier
8621      elaborated-type-specifier
8622      cv-qualifier
8623
8624    GNU Extension:
8625
8626    type-specifier:
8627      __complex__
8628
8629    Returns a representation of the type-specifier.  If the
8630    type-specifier is a keyword (like `int' or `const', or
8631    `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8632    For a class-specifier, enum-specifier, or elaborated-type-specifier
8633    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8634
8635    If IS_FRIEND is TRUE then this type-specifier is being declared a
8636    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8637    appearing in a decl-specifier-seq.
8638
8639    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8640    class-specifier, enum-specifier, or elaborated-type-specifier, then
8641    *DECLARES_CLASS_OR_ENUM is set to TRUE.  Otherwise, it is set to
8642    FALSE.
8643
8644    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8645    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8646    is set to FALSE.  */
8647
8648 static tree
8649 cp_parser_type_specifier (parser, 
8650                           flags, 
8651                           is_friend,
8652                           is_declaration,
8653                           declares_class_or_enum,
8654                           is_cv_qualifier)
8655      cp_parser *parser;
8656      cp_parser_flags flags;
8657      bool is_friend;
8658      bool is_declaration;
8659      bool *declares_class_or_enum;
8660      bool *is_cv_qualifier;
8661 {
8662   tree type_spec = NULL_TREE;
8663   cp_token *token;
8664   enum rid keyword;
8665
8666   /* Assume this type-specifier does not declare a new type.  */
8667   if (declares_class_or_enum)
8668     *declares_class_or_enum = false;
8669   /* And that it does not specify a cv-qualifier.  */
8670   if (is_cv_qualifier)
8671     *is_cv_qualifier = false;
8672   /* Peek at the next token.  */
8673   token = cp_lexer_peek_token (parser->lexer);
8674
8675   /* If we're looking at a keyword, we can use that to guide the
8676      production we choose.  */
8677   keyword = token->keyword;
8678   switch (keyword)
8679     {
8680       /* Any of these indicate either a class-specifier, or an
8681          elaborated-type-specifier.  */
8682     case RID_CLASS:
8683     case RID_STRUCT:
8684     case RID_UNION:
8685     case RID_ENUM:
8686       /* Parse tentatively so that we can back up if we don't find a
8687          class-specifier or enum-specifier.  */
8688       cp_parser_parse_tentatively (parser);
8689       /* Look for the class-specifier or enum-specifier.  */
8690       if (keyword == RID_ENUM)
8691         type_spec = cp_parser_enum_specifier (parser);
8692       else
8693         type_spec = cp_parser_class_specifier (parser);
8694
8695       /* If that worked, we're done.  */
8696       if (cp_parser_parse_definitely (parser))
8697         {
8698           if (declares_class_or_enum)
8699             *declares_class_or_enum = true;
8700           return type_spec;
8701         }
8702
8703       /* Fall through.  */
8704
8705     case RID_TYPENAME:
8706       /* Look for an elaborated-type-specifier.  */
8707       type_spec = cp_parser_elaborated_type_specifier (parser,
8708                                                        is_friend,
8709                                                        is_declaration);
8710       /* We're declaring a class or enum -- unless we're using
8711          `typename'.  */
8712       if (declares_class_or_enum && keyword != RID_TYPENAME)
8713         *declares_class_or_enum = true;
8714       return type_spec;
8715
8716     case RID_CONST:
8717     case RID_VOLATILE:
8718     case RID_RESTRICT:
8719       type_spec = cp_parser_cv_qualifier_opt (parser);
8720       /* Even though we call a routine that looks for an optional
8721          qualifier, we know that there should be one.  */
8722       my_friendly_assert (type_spec != NULL, 20000328);
8723       /* This type-specifier was a cv-qualified.  */
8724       if (is_cv_qualifier)
8725         *is_cv_qualifier = true;
8726
8727       return type_spec;
8728
8729     case RID_COMPLEX:
8730       /* The `__complex__' keyword is a GNU extension.  */
8731       return cp_lexer_consume_token (parser->lexer)->value;
8732
8733     default:
8734       break;
8735     }
8736
8737   /* If we do not already have a type-specifier, assume we are looking
8738      at a simple-type-specifier.  */
8739   type_spec = cp_parser_simple_type_specifier (parser, flags);
8740
8741   /* If we didn't find a type-specifier, and a type-specifier was not
8742      optional in this context, issue an error message.  */
8743   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8744     {
8745       cp_parser_error (parser, "expected type specifier");
8746       return error_mark_node;
8747     }
8748
8749   return type_spec;
8750 }
8751
8752 /* Parse a simple-type-specifier.
8753
8754    simple-type-specifier:
8755      :: [opt] nested-name-specifier [opt] type-name
8756      :: [opt] nested-name-specifier template template-id
8757      char
8758      wchar_t
8759      bool
8760      short
8761      int
8762      long
8763      signed
8764      unsigned
8765      float
8766      double
8767      void  
8768
8769    GNU Extension:
8770
8771    simple-type-specifier:
8772      __typeof__ unary-expression
8773      __typeof__ ( type-id )
8774
8775    For the various keywords, the value returned is simply the
8776    TREE_IDENTIFIER representing the keyword.  For the first two
8777    productions, the value returned is the indicated TYPE_DECL.  */
8778
8779 static tree
8780 cp_parser_simple_type_specifier (parser, flags)
8781      cp_parser *parser;
8782      cp_parser_flags flags;
8783 {
8784   tree type = NULL_TREE;
8785   cp_token *token;
8786
8787   /* Peek at the next token.  */
8788   token = cp_lexer_peek_token (parser->lexer);
8789
8790   /* If we're looking at a keyword, things are easy.  */
8791   switch (token->keyword)
8792     {
8793     case RID_CHAR:
8794     case RID_WCHAR:
8795     case RID_BOOL:
8796     case RID_SHORT:
8797     case RID_INT:
8798     case RID_LONG:
8799     case RID_SIGNED:
8800     case RID_UNSIGNED:
8801     case RID_FLOAT:
8802     case RID_DOUBLE:
8803     case RID_VOID:
8804       /* Consume the token.  */
8805       return cp_lexer_consume_token (parser->lexer)->value;
8806
8807     case RID_TYPEOF:
8808       {
8809         tree operand;
8810
8811         /* Consume the `typeof' token.  */
8812         cp_lexer_consume_token (parser->lexer);
8813         /* Parse the operand to `typeof'  */
8814         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8815         /* If it is not already a TYPE, take its type.  */
8816         if (!TYPE_P (operand))
8817           operand = finish_typeof (operand);
8818
8819         return operand;
8820       }
8821
8822     default:
8823       break;
8824     }
8825
8826   /* The type-specifier must be a user-defined type.  */
8827   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8828     {
8829       /* Don't gobble tokens or issue error messages if this is an
8830          optional type-specifier.  */
8831       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8832         cp_parser_parse_tentatively (parser);
8833
8834       /* Look for the optional `::' operator.  */
8835       cp_parser_global_scope_opt (parser,
8836                                   /*current_scope_valid_p=*/false);
8837       /* Look for the nested-name specifier.  */
8838       cp_parser_nested_name_specifier_opt (parser,
8839                                            /*typename_keyword_p=*/false,
8840                                            /*check_dependency_p=*/true,
8841                                            /*type_p=*/false);
8842       /* If we have seen a nested-name-specifier, and the next token
8843          is `template', then we are using the template-id production.  */
8844       if (parser->scope 
8845           && cp_parser_optional_template_keyword (parser))
8846         {
8847           /* Look for the template-id.  */
8848           type = cp_parser_template_id (parser, 
8849                                         /*template_keyword_p=*/true,
8850                                         /*check_dependency_p=*/true);
8851           /* If the template-id did not name a type, we are out of
8852              luck.  */
8853           if (TREE_CODE (type) != TYPE_DECL)
8854             {
8855               cp_parser_error (parser, "expected template-id for type");
8856               type = NULL_TREE;
8857             }
8858         }
8859       /* Otherwise, look for a type-name.  */
8860       else
8861         {
8862           type = cp_parser_type_name (parser);
8863           if (type == error_mark_node)
8864             type = NULL_TREE;
8865         }
8866
8867       /* If it didn't work out, we don't have a TYPE.  */
8868       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8869           && !cp_parser_parse_definitely (parser))
8870         type = NULL_TREE;
8871     }
8872
8873   /* If we didn't get a type-name, issue an error message.  */
8874   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8875     {
8876       cp_parser_error (parser, "expected type-name");
8877       return error_mark_node;
8878     }
8879
8880   return type;
8881 }
8882
8883 /* Parse a type-name.
8884
8885    type-name:
8886      class-name
8887      enum-name
8888      typedef-name  
8889
8890    enum-name:
8891      identifier
8892
8893    typedef-name:
8894      identifier 
8895
8896    Returns a TYPE_DECL for the the type.  */
8897
8898 static tree
8899 cp_parser_type_name (parser)
8900      cp_parser *parser;
8901 {
8902   tree type_decl;
8903   tree identifier;
8904
8905   /* We can't know yet whether it is a class-name or not.  */
8906   cp_parser_parse_tentatively (parser);
8907   /* Try a class-name.  */
8908   type_decl = cp_parser_class_name (parser, 
8909                                     /*typename_keyword_p=*/false,
8910                                     /*template_keyword_p=*/false,
8911                                     /*type_p=*/false,
8912                                     /*check_access_p=*/true,
8913                                     /*check_dependency_p=*/true,
8914                                     /*class_head_p=*/false);
8915   /* If it's not a class-name, keep looking.  */
8916   if (!cp_parser_parse_definitely (parser))
8917     {
8918       /* It must be a typedef-name or an enum-name.  */
8919       identifier = cp_parser_identifier (parser);
8920       if (identifier == error_mark_node)
8921         return error_mark_node;
8922       
8923       /* Look up the type-name.  */
8924       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8925       /* Issue an error if we did not find a type-name.  */
8926       if (TREE_CODE (type_decl) != TYPE_DECL)
8927         {
8928           cp_parser_error (parser, "expected type-name");
8929           type_decl = error_mark_node;
8930         }
8931       /* Remember that the name was used in the definition of the
8932          current class so that we can check later to see if the
8933          meaning would have been different after the class was
8934          entirely defined.  */
8935       else if (type_decl != error_mark_node
8936                && !parser->scope)
8937         maybe_note_name_used_in_class (identifier, type_decl);
8938     }
8939   
8940   return type_decl;
8941 }
8942
8943
8944 /* Parse an elaborated-type-specifier.  Note that the grammar given
8945    here incorporates the resolution to DR68.
8946
8947    elaborated-type-specifier:
8948      class-key :: [opt] nested-name-specifier [opt] identifier
8949      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8950      enum :: [opt] nested-name-specifier [opt] identifier
8951      typename :: [opt] nested-name-specifier identifier
8952      typename :: [opt] nested-name-specifier template [opt] 
8953        template-id 
8954
8955    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8956    declared `friend'.  If IS_DECLARATION is TRUE, then this
8957    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8958    something is being declared.
8959
8960    Returns the TYPE specified.  */
8961
8962 static tree
8963 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8964      cp_parser *parser;
8965      bool is_friend;
8966      bool is_declaration;
8967 {
8968   enum tag_types tag_type;
8969   tree identifier;
8970   tree type = NULL_TREE;
8971
8972   /* See if we're looking at the `enum' keyword.  */
8973   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8974     {
8975       /* Consume the `enum' token.  */
8976       cp_lexer_consume_token (parser->lexer);
8977       /* Remember that it's an enumeration type.  */
8978       tag_type = enum_type;
8979     }
8980   /* Or, it might be `typename'.  */
8981   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8982                                            RID_TYPENAME))
8983     {
8984       /* Consume the `typename' token.  */
8985       cp_lexer_consume_token (parser->lexer);
8986       /* Remember that it's a `typename' type.  */
8987       tag_type = typename_type;
8988       /* The `typename' keyword is only allowed in templates.  */
8989       if (!processing_template_decl)
8990         pedwarn ("using `typename' outside of template");
8991     }
8992   /* Otherwise it must be a class-key.  */
8993   else
8994     {
8995       tag_type = cp_parser_class_key (parser);
8996       if (tag_type == none_type)
8997         return error_mark_node;
8998     }
8999
9000   /* Look for the `::' operator.  */
9001   cp_parser_global_scope_opt (parser, 
9002                               /*current_scope_valid_p=*/false);
9003   /* Look for the nested-name-specifier.  */
9004   if (tag_type == typename_type)
9005     cp_parser_nested_name_specifier (parser,
9006                                      /*typename_keyword_p=*/true,
9007                                      /*check_dependency_p=*/true,
9008                                      /*type_p=*/true);
9009   else
9010     /* Even though `typename' is not present, the proposed resolution
9011        to Core Issue 180 says that in `class A<T>::B', `B' should be
9012        considered a type-name, even if `A<T>' is dependent.  */
9013     cp_parser_nested_name_specifier_opt (parser,
9014                                          /*typename_keyword_p=*/true,
9015                                          /*check_dependency_p=*/true,
9016                                          /*type_p=*/true);
9017   /* For everything but enumeration types, consider a template-id.  */
9018   if (tag_type != enum_type)
9019     {
9020       bool template_p = false;
9021       tree decl;
9022
9023       /* Allow the `template' keyword.  */
9024       template_p = cp_parser_optional_template_keyword (parser);
9025       /* If we didn't see `template', we don't know if there's a
9026          template-id or not.  */
9027       if (!template_p)
9028         cp_parser_parse_tentatively (parser);
9029       /* Parse the template-id.  */
9030       decl = cp_parser_template_id (parser, template_p,
9031                                     /*check_dependency_p=*/true);
9032       /* If we didn't find a template-id, look for an ordinary
9033          identifier.  */
9034       if (!template_p && !cp_parser_parse_definitely (parser))
9035         ;
9036       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9037          in effect, then we must assume that, upon instantiation, the
9038          template will correspond to a class.  */
9039       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9040                && tag_type == typename_type)
9041         type = make_typename_type (parser->scope, decl,
9042                                    /*complain=*/1);
9043       else 
9044         type = TREE_TYPE (decl);
9045     }
9046
9047   /* For an enumeration type, consider only a plain identifier.  */
9048   if (!type)
9049     {
9050       identifier = cp_parser_identifier (parser);
9051
9052       if (identifier == error_mark_node)
9053         return error_mark_node;
9054
9055       /* For a `typename', we needn't call xref_tag.  */
9056       if (tag_type == typename_type)
9057         return make_typename_type (parser->scope, identifier, 
9058                                    /*complain=*/1);
9059       /* Look up a qualified name in the usual way.  */
9060       if (parser->scope)
9061         {
9062           tree decl;
9063
9064           /* In an elaborated-type-specifier, names are assumed to name
9065              types, so we set IS_TYPE to TRUE when calling
9066              cp_parser_lookup_name.  */
9067           decl = cp_parser_lookup_name (parser, identifier, 
9068                                         /*check_access=*/true,
9069                                         /*is_type=*/true,
9070                                         /*is_namespace=*/false,
9071                                         /*check_dependency=*/true);
9072           decl = (cp_parser_maybe_treat_template_as_class 
9073                   (decl, /*tag_name_p=*/is_friend));
9074
9075           if (TREE_CODE (decl) != TYPE_DECL)
9076             {
9077               error ("expected type-name");
9078               return error_mark_node;
9079             }
9080           else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9081                    && tag_type != enum_type)
9082             error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9083                    tag_type == record_type ? "struct" : "class");
9084           else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9085                    && tag_type == enum_type)
9086             error ("`%T' referred to as enum", TREE_TYPE (decl));
9087
9088           type = TREE_TYPE (decl);
9089         }
9090       else 
9091         {
9092           /* An elaborated-type-specifier sometimes introduces a new type and
9093              sometimes names an existing type.  Normally, the rule is that it
9094              introduces a new type only if there is not an existing type of
9095              the same name already in scope.  For example, given:
9096
9097                struct S {};
9098                void f() { struct S s; }
9099
9100              the `struct S' in the body of `f' is the same `struct S' as in
9101              the global scope; the existing definition is used.  However, if
9102              there were no global declaration, this would introduce a new 
9103              local class named `S'.
9104
9105              An exception to this rule applies to the following code:
9106
9107                namespace N { struct S; }
9108
9109              Here, the elaborated-type-specifier names a new type
9110              unconditionally; even if there is already an `S' in the
9111              containing scope this declaration names a new type.
9112              This exception only applies if the elaborated-type-specifier
9113              forms the complete declaration:
9114
9115                [class.name] 
9116
9117                A declaration consisting solely of `class-key identifier ;' is
9118                either a redeclaration of the name in the current scope or a
9119                forward declaration of the identifier as a class name.  It
9120                introduces the name into the current scope.
9121
9122              We are in this situation precisely when the next token is a `;'.
9123
9124              An exception to the exception is that a `friend' declaration does
9125              *not* name a new type; i.e., given:
9126
9127                struct S { friend struct T; };
9128
9129              `T' is not a new type in the scope of `S'.  
9130
9131              Also, `new struct S' or `sizeof (struct S)' never results in the
9132              definition of a new type; a new type can only be declared in a
9133              declaration context.   */
9134
9135           type = xref_tag (tag_type, identifier, 
9136                            /*attributes=*/NULL_TREE,
9137                            (is_friend 
9138                             || !is_declaration
9139                             || cp_lexer_next_token_is_not (parser->lexer, 
9140                                                            CPP_SEMICOLON)));
9141         }
9142     }
9143   if (tag_type != enum_type)
9144     cp_parser_check_class_key (tag_type, type);
9145   return type;
9146 }
9147
9148 /* Parse an enum-specifier.
9149
9150    enum-specifier:
9151      enum identifier [opt] { enumerator-list [opt] }
9152
9153    Returns an ENUM_TYPE representing the enumeration.  */
9154
9155 static tree
9156 cp_parser_enum_specifier (parser)
9157      cp_parser *parser;
9158 {
9159   cp_token *token;
9160   tree identifier = NULL_TREE;
9161   tree type;
9162
9163   /* Look for the `enum' keyword.  */
9164   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9165     return error_mark_node;
9166   /* Peek at the next token.  */
9167   token = cp_lexer_peek_token (parser->lexer);
9168
9169   /* See if it is an identifier.  */
9170   if (token->type == CPP_NAME)
9171     identifier = cp_parser_identifier (parser);
9172
9173   /* Look for the `{'.  */
9174   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9175     return error_mark_node;
9176
9177   /* At this point, we're going ahead with the enum-specifier, even
9178      if some other problem occurs.  */
9179   cp_parser_commit_to_tentative_parse (parser);
9180
9181   /* Issue an error message if type-definitions are forbidden here.  */
9182   cp_parser_check_type_definition (parser);
9183
9184   /* Create the new type.  */
9185   type = start_enum (identifier ? identifier : make_anon_name ());
9186
9187   /* Peek at the next token.  */
9188   token = cp_lexer_peek_token (parser->lexer);
9189   /* If it's not a `}', then there are some enumerators.  */
9190   if (token->type != CPP_CLOSE_BRACE)
9191     cp_parser_enumerator_list (parser, type);
9192   /* Look for the `}'.  */
9193   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9194
9195   /* Finish up the enumeration.  */
9196   finish_enum (type);
9197
9198   return type;
9199 }
9200
9201 /* Parse an enumerator-list.  The enumerators all have the indicated
9202    TYPE.  
9203
9204    enumerator-list:
9205      enumerator-definition
9206      enumerator-list , enumerator-definition  */
9207
9208 static void
9209 cp_parser_enumerator_list (parser, type)
9210      cp_parser *parser;
9211      tree type;
9212 {
9213   while (true)
9214     {
9215       cp_token *token;
9216
9217       /* Parse an enumerator-definition.  */
9218       cp_parser_enumerator_definition (parser, type);
9219       /* Peek at the next token.  */
9220       token = cp_lexer_peek_token (parser->lexer);
9221       /* If it's not a `,', then we've reached the end of the 
9222          list.  */
9223       if (token->type != CPP_COMMA)
9224         break;
9225       /* Otherwise, consume the `,' and keep going.  */
9226       cp_lexer_consume_token (parser->lexer);
9227       /* If the next token is a `}', there is a trailing comma.  */
9228       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9229         {
9230           if (pedantic && !in_system_header)
9231             pedwarn ("comma at end of enumerator list");
9232           break;
9233         }
9234     }
9235 }
9236
9237 /* Parse an enumerator-definition.  The enumerator has the indicated
9238    TYPE.
9239
9240    enumerator-definition:
9241      enumerator
9242      enumerator = constant-expression
9243     
9244    enumerator:
9245      identifier  */
9246
9247 static void
9248 cp_parser_enumerator_definition (parser, type)
9249      cp_parser *parser;
9250      tree type;
9251 {
9252   cp_token *token;
9253   tree identifier;
9254   tree value;
9255
9256   /* Look for the identifier.  */
9257   identifier = cp_parser_identifier (parser);
9258   if (identifier == error_mark_node)
9259     return;
9260   
9261   /* Peek at the next token.  */
9262   token = cp_lexer_peek_token (parser->lexer);
9263   /* If it's an `=', then there's an explicit value.  */
9264   if (token->type == CPP_EQ)
9265     {
9266       /* Consume the `=' token.  */
9267       cp_lexer_consume_token (parser->lexer);
9268       /* Parse the value.  */
9269       value = cp_parser_constant_expression (parser);
9270     }
9271   else
9272     value = NULL_TREE;
9273
9274   /* Create the enumerator.  */
9275   build_enumerator (identifier, value, type);
9276 }
9277
9278 /* Parse a namespace-name.
9279
9280    namespace-name:
9281      original-namespace-name
9282      namespace-alias
9283
9284    Returns the NAMESPACE_DECL for the namespace.  */
9285
9286 static tree
9287 cp_parser_namespace_name (parser)
9288      cp_parser *parser;
9289 {
9290   tree identifier;
9291   tree namespace_decl;
9292
9293   /* Get the name of the namespace.  */
9294   identifier = cp_parser_identifier (parser);
9295   if (identifier == error_mark_node)
9296     return error_mark_node;
9297
9298   /* Look up the identifier in the currently active scope.  Look only
9299      for namespaces, due to:
9300
9301        [basic.lookup.udir]
9302
9303        When looking up a namespace-name in a using-directive or alias
9304        definition, only namespace names are considered.  
9305
9306      And:
9307
9308        [basic.lookup.qual]
9309
9310        During the lookup of a name preceding the :: scope resolution
9311        operator, object, function, and enumerator names are ignored.  
9312
9313      (Note that cp_parser_class_or_namespace_name only calls this
9314      function if the token after the name is the scope resolution
9315      operator.)  */
9316   namespace_decl = cp_parser_lookup_name (parser, identifier,
9317                                           /*check_access=*/true,
9318                                           /*is_type=*/false,
9319                                           /*is_namespace=*/true,
9320                                           /*check_dependency=*/true);
9321   /* If it's not a namespace, issue an error.  */
9322   if (namespace_decl == error_mark_node
9323       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9324     {
9325       cp_parser_error (parser, "expected namespace-name");
9326       namespace_decl = error_mark_node;
9327     }
9328   
9329   return namespace_decl;
9330 }
9331
9332 /* Parse a namespace-definition.
9333
9334    namespace-definition:
9335      named-namespace-definition
9336      unnamed-namespace-definition  
9337
9338    named-namespace-definition:
9339      original-namespace-definition
9340      extension-namespace-definition
9341
9342    original-namespace-definition:
9343      namespace identifier { namespace-body }
9344    
9345    extension-namespace-definition:
9346      namespace original-namespace-name { namespace-body }
9347  
9348    unnamed-namespace-definition:
9349      namespace { namespace-body } */
9350
9351 static void
9352 cp_parser_namespace_definition (parser)
9353      cp_parser *parser;
9354 {
9355   tree identifier;
9356
9357   /* Look for the `namespace' keyword.  */
9358   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9359
9360   /* Get the name of the namespace.  We do not attempt to distinguish
9361      between an original-namespace-definition and an
9362      extension-namespace-definition at this point.  The semantic
9363      analysis routines are responsible for that.  */
9364   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9365     identifier = cp_parser_identifier (parser);
9366   else
9367     identifier = NULL_TREE;
9368
9369   /* Look for the `{' to start the namespace.  */
9370   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9371   /* Start the namespace.  */
9372   push_namespace (identifier);
9373   /* Parse the body of the namespace.  */
9374   cp_parser_namespace_body (parser);
9375   /* Finish the namespace.  */
9376   pop_namespace ();
9377   /* Look for the final `}'.  */
9378   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9379 }
9380
9381 /* Parse a namespace-body.
9382
9383    namespace-body:
9384      declaration-seq [opt]  */
9385
9386 static void
9387 cp_parser_namespace_body (parser)
9388      cp_parser *parser;
9389 {
9390   cp_parser_declaration_seq_opt (parser);
9391 }
9392
9393 /* Parse a namespace-alias-definition.
9394
9395    namespace-alias-definition:
9396      namespace identifier = qualified-namespace-specifier ;  */
9397
9398 static void
9399 cp_parser_namespace_alias_definition (parser)
9400      cp_parser *parser;
9401 {
9402   tree identifier;
9403   tree namespace_specifier;
9404
9405   /* Look for the `namespace' keyword.  */
9406   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9407   /* Look for the identifier.  */
9408   identifier = cp_parser_identifier (parser);
9409   if (identifier == error_mark_node)
9410     return;
9411   /* Look for the `=' token.  */
9412   cp_parser_require (parser, CPP_EQ, "`='");
9413   /* Look for the qualified-namespace-specifier.  */
9414   namespace_specifier 
9415     = cp_parser_qualified_namespace_specifier (parser);
9416   /* Look for the `;' token.  */
9417   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9418
9419   /* Register the alias in the symbol table.  */
9420   do_namespace_alias (identifier, namespace_specifier);
9421 }
9422
9423 /* Parse a qualified-namespace-specifier.
9424
9425    qualified-namespace-specifier:
9426      :: [opt] nested-name-specifier [opt] namespace-name
9427
9428    Returns a NAMESPACE_DECL corresponding to the specified
9429    namespace.  */
9430
9431 static tree
9432 cp_parser_qualified_namespace_specifier (parser)
9433      cp_parser *parser;
9434 {
9435   /* Look for the optional `::'.  */
9436   cp_parser_global_scope_opt (parser, 
9437                               /*current_scope_valid_p=*/false);
9438
9439   /* Look for the optional nested-name-specifier.  */
9440   cp_parser_nested_name_specifier_opt (parser,
9441                                        /*typename_keyword_p=*/false,
9442                                        /*check_dependency_p=*/true,
9443                                        /*type_p=*/false);
9444
9445   return cp_parser_namespace_name (parser);
9446 }
9447
9448 /* Parse a using-declaration.
9449
9450    using-declaration:
9451      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9452      using :: unqualified-id ;  */
9453
9454 static void
9455 cp_parser_using_declaration (parser)
9456      cp_parser *parser;
9457 {
9458   cp_token *token;
9459   bool typename_p = false;
9460   bool global_scope_p;
9461   tree decl;
9462   tree identifier;
9463   tree scope;
9464
9465   /* Look for the `using' keyword.  */
9466   cp_parser_require_keyword (parser, RID_USING, "`using'");
9467   
9468   /* Peek at the next token.  */
9469   token = cp_lexer_peek_token (parser->lexer);
9470   /* See if it's `typename'.  */
9471   if (token->keyword == RID_TYPENAME)
9472     {
9473       /* Remember that we've seen it.  */
9474       typename_p = true;
9475       /* Consume the `typename' token.  */
9476       cp_lexer_consume_token (parser->lexer);
9477     }
9478
9479   /* Look for the optional global scope qualification.  */
9480   global_scope_p 
9481     = (cp_parser_global_scope_opt (parser,
9482                                    /*current_scope_valid_p=*/false) 
9483        != NULL_TREE);
9484
9485   /* If we saw `typename', or didn't see `::', then there must be a
9486      nested-name-specifier present.  */
9487   if (typename_p || !global_scope_p)
9488     cp_parser_nested_name_specifier (parser, typename_p, 
9489                                      /*check_dependency_p=*/true,
9490                                      /*type_p=*/false);
9491   /* Otherwise, we could be in either of the two productions.  In that
9492      case, treat the nested-name-specifier as optional.  */
9493   else
9494     cp_parser_nested_name_specifier_opt (parser,
9495                                          /*typename_keyword_p=*/false,
9496                                          /*check_dependency_p=*/true,
9497                                          /*type_p=*/false);
9498
9499   /* Parse the unqualified-id.  */
9500   identifier = cp_parser_unqualified_id (parser, 
9501                                          /*template_keyword_p=*/false,
9502                                          /*check_dependency_p=*/true);
9503
9504   /* The function we call to handle a using-declaration is different
9505      depending on what scope we are in.  */
9506   scope = current_scope ();
9507   if (scope && TYPE_P (scope))
9508     {
9509       /* Create the USING_DECL.  */
9510       decl = do_class_using_decl (build_nt (SCOPE_REF,
9511                                             parser->scope,
9512                                             identifier));
9513       /* Add it to the list of members in this class.  */
9514       finish_member_declaration (decl);
9515     }
9516   else
9517     {
9518       decl = cp_parser_lookup_name_simple (parser, identifier);
9519       if (scope)
9520         do_local_using_decl (decl);
9521       else
9522         do_toplevel_using_decl (decl);
9523     }
9524
9525   /* Look for the final `;'.  */
9526   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9527 }
9528
9529 /* Parse a using-directive.  
9530  
9531    using-directive:
9532      using namespace :: [opt] nested-name-specifier [opt]
9533        namespace-name ;  */
9534
9535 static void
9536 cp_parser_using_directive (parser)
9537      cp_parser *parser;
9538 {
9539   tree namespace_decl;
9540
9541   /* Look for the `using' keyword.  */
9542   cp_parser_require_keyword (parser, RID_USING, "`using'");
9543   /* And the `namespace' keyword.  */
9544   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9545   /* Look for the optional `::' operator.  */
9546   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9547   /* And the optional nested-name-sepcifier.  */
9548   cp_parser_nested_name_specifier_opt (parser,
9549                                        /*typename_keyword_p=*/false,
9550                                        /*check_dependency_p=*/true,
9551                                        /*type_p=*/false);
9552   /* Get the namespace being used.  */
9553   namespace_decl = cp_parser_namespace_name (parser);
9554   /* Update the symbol table.  */
9555   do_using_directive (namespace_decl);
9556   /* Look for the final `;'.  */
9557   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9558 }
9559
9560 /* Parse an asm-definition.
9561
9562    asm-definition:
9563      asm ( string-literal ) ;  
9564
9565    GNU Extension:
9566
9567    asm-definition:
9568      asm volatile [opt] ( string-literal ) ;
9569      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9570      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9571                           : asm-operand-list [opt] ) ;
9572      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9573                           : asm-operand-list [opt] 
9574                           : asm-operand-list [opt] ) ;  */
9575
9576 static void
9577 cp_parser_asm_definition (parser)
9578      cp_parser *parser;
9579 {
9580   cp_token *token;
9581   tree string;
9582   tree outputs = NULL_TREE;
9583   tree inputs = NULL_TREE;
9584   tree clobbers = NULL_TREE;
9585   tree asm_stmt;
9586   bool volatile_p = false;
9587   bool extended_p = false;
9588
9589   /* Look for the `asm' keyword.  */
9590   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9591   /* See if the next token is `volatile'.  */
9592   if (cp_parser_allow_gnu_extensions_p (parser)
9593       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9594     {
9595       /* Remember that we saw the `volatile' keyword.  */
9596       volatile_p = true;
9597       /* Consume the token.  */
9598       cp_lexer_consume_token (parser->lexer);
9599     }
9600   /* Look for the opening `('.  */
9601   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9602   /* Look for the string.  */
9603   token = cp_parser_require (parser, CPP_STRING, "asm body");
9604   if (!token)
9605     return;
9606   string = token->value;
9607   /* If we're allowing GNU extensions, check for the extended assembly
9608      syntax.  Unfortunately, the `:' tokens need not be separated by 
9609      a space in C, and so, for compatibility, we tolerate that here
9610      too.  Doing that means that we have to treat the `::' operator as
9611      two `:' tokens.  */
9612   if (cp_parser_allow_gnu_extensions_p (parser)
9613       && at_function_scope_p ()
9614       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9615           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9616     {
9617       bool inputs_p = false;
9618       bool clobbers_p = false;
9619
9620       /* The extended syntax was used.  */
9621       extended_p = true;
9622
9623       /* Look for outputs.  */
9624       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9625         {
9626           /* Consume the `:'.  */
9627           cp_lexer_consume_token (parser->lexer);
9628           /* Parse the output-operands.  */
9629           if (cp_lexer_next_token_is_not (parser->lexer, 
9630                                           CPP_COLON)
9631               && cp_lexer_next_token_is_not (parser->lexer,
9632                                              CPP_SCOPE)
9633               && cp_lexer_next_token_is_not (parser->lexer,
9634                                              CPP_CLOSE_PAREN))
9635             outputs = cp_parser_asm_operand_list (parser);
9636         }
9637       /* If the next token is `::', there are no outputs, and the
9638          next token is the beginning of the inputs.  */
9639       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9640         {
9641           /* Consume the `::' token.  */
9642           cp_lexer_consume_token (parser->lexer);
9643           /* The inputs are coming next.  */
9644           inputs_p = true;
9645         }
9646
9647       /* Look for inputs.  */
9648       if (inputs_p
9649           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9650         {
9651           if (!inputs_p)
9652             /* Consume the `:'.  */
9653             cp_lexer_consume_token (parser->lexer);
9654           /* Parse the output-operands.  */
9655           if (cp_lexer_next_token_is_not (parser->lexer, 
9656                                           CPP_COLON)
9657               && cp_lexer_next_token_is_not (parser->lexer,
9658                                              CPP_SCOPE)
9659               && cp_lexer_next_token_is_not (parser->lexer,
9660                                              CPP_CLOSE_PAREN))
9661             inputs = cp_parser_asm_operand_list (parser);
9662         }
9663       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9664         /* The clobbers are coming next.  */
9665         clobbers_p = true;
9666
9667       /* Look for clobbers.  */
9668       if (clobbers_p 
9669           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9670         {
9671           if (!clobbers_p)
9672             /* Consume the `:'.  */
9673             cp_lexer_consume_token (parser->lexer);
9674           /* Parse the clobbers.  */
9675           if (cp_lexer_next_token_is_not (parser->lexer,
9676                                           CPP_CLOSE_PAREN))
9677             clobbers = cp_parser_asm_clobber_list (parser);
9678         }
9679     }
9680   /* Look for the closing `)'.  */
9681   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9682     cp_parser_skip_to_closing_parenthesis (parser);
9683   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9684
9685   /* Create the ASM_STMT.  */
9686   if (at_function_scope_p ())
9687     {
9688       asm_stmt = 
9689         finish_asm_stmt (volatile_p 
9690                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9691                          string, outputs, inputs, clobbers);
9692       /* If the extended syntax was not used, mark the ASM_STMT.  */
9693       if (!extended_p)
9694         ASM_INPUT_P (asm_stmt) = 1;
9695     }
9696   else
9697     assemble_asm (string);
9698 }
9699
9700 /* Declarators [gram.dcl.decl] */
9701
9702 /* Parse an init-declarator.
9703
9704    init-declarator:
9705      declarator initializer [opt]
9706
9707    GNU Extension:
9708
9709    init-declarator:
9710      declarator asm-specification [opt] attributes [opt] initializer [opt]
9711
9712    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9713    Returns a reprsentation of the entity declared.  The ACCESS_CHECKS
9714    represent deferred access checks from the decl-specifier-seq.  If
9715    MEMBER_P is TRUE, then this declarator appears in a class scope.
9716    The new DECL created by this declarator is returned.
9717
9718    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9719    for a function-definition here as well.  If the declarator is a
9720    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9721    be TRUE upon return.  By that point, the function-definition will
9722    have been completely parsed.
9723
9724    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9725    is FALSE.  */
9726
9727 static tree
9728 cp_parser_init_declarator (parser, 
9729                            decl_specifiers, 
9730                            prefix_attributes,
9731                            access_checks,
9732                            function_definition_allowed_p,
9733                            member_p,
9734                            function_definition_p)
9735      cp_parser *parser;
9736      tree decl_specifiers;
9737      tree prefix_attributes;
9738      tree access_checks;
9739      bool function_definition_allowed_p;
9740      bool member_p;
9741      bool *function_definition_p;
9742 {
9743   cp_token *token;
9744   tree declarator;
9745   tree attributes;
9746   tree asm_specification;
9747   tree initializer;
9748   tree decl = NULL_TREE;
9749   tree scope;
9750   tree declarator_access_checks;
9751   bool is_initialized;
9752   bool is_parenthesized_init;
9753   bool ctor_dtor_or_conv_p;
9754   bool friend_p;
9755
9756   /* Assume that this is not the declarator for a function
9757      definition.  */
9758   if (function_definition_p)
9759     *function_definition_p = false;
9760
9761   /* Defer access checks while parsing the declarator; we cannot know
9762      what names are accessible until we know what is being 
9763      declared.  */
9764   cp_parser_start_deferring_access_checks (parser);
9765   /* Parse the declarator.  */
9766   declarator 
9767     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9768                             &ctor_dtor_or_conv_p);
9769   /* Gather up the deferred checks.  */
9770   declarator_access_checks 
9771     = cp_parser_stop_deferring_access_checks (parser);
9772
9773   /* Prevent the access checks from being reclaimed by GC.  */
9774   parser->access_checks_lists
9775     = tree_cons (NULL_TREE, declarator_access_checks,
9776                  parser->access_checks_lists);
9777
9778   /* If the DECLARATOR was erroneous, there's no need to go
9779      further.  */
9780   if (declarator == error_mark_node)
9781     {
9782       /* Discard access checks no longer in use.  */
9783       parser->access_checks_lists
9784         = TREE_CHAIN (parser->access_checks_lists);
9785       return error_mark_node;
9786     }
9787
9788   /* Figure out what scope the entity declared by the DECLARATOR is
9789      located in.  `grokdeclarator' sometimes changes the scope, so
9790      we compute it now.  */
9791   scope = get_scope_of_declarator (declarator);
9792
9793   /* If we're allowing GNU extensions, look for an asm-specification
9794      and attributes.  */
9795   if (cp_parser_allow_gnu_extensions_p (parser))
9796     {
9797       /* Look for an asm-specification.  */
9798       asm_specification = cp_parser_asm_specification_opt (parser);
9799       /* And attributes.  */
9800       attributes = cp_parser_attributes_opt (parser);
9801     }
9802   else
9803     {
9804       asm_specification = NULL_TREE;
9805       attributes = NULL_TREE;
9806     }
9807
9808   /* Peek at the next token.  */
9809   token = cp_lexer_peek_token (parser->lexer);
9810   /* Check to see if the token indicates the start of a
9811      function-definition.  */
9812   if (cp_parser_token_starts_function_definition_p (token))
9813     {
9814       if (!function_definition_allowed_p)
9815         {
9816           /* If a function-definition should not appear here, issue an
9817              error message.  */
9818           cp_parser_error (parser,
9819                            "a function-definition is not allowed here");
9820           /* Discard access checks no longer in use.  */
9821           parser->access_checks_lists
9822             = TREE_CHAIN (parser->access_checks_lists);
9823           return error_mark_node;
9824         }
9825       else
9826         {
9827           tree *ac;
9828
9829           /* Neither attributes nor an asm-specification are allowed
9830              on a function-definition.  */
9831           if (asm_specification)
9832             error ("an asm-specification is not allowed on a function-definition");
9833           if (attributes)
9834             error ("attributes are not allowed on a function-definition");
9835           /* This is a function-definition.  */
9836           *function_definition_p = true;
9837
9838           /* Thread the access checks together.  */
9839           ac = &access_checks;
9840           while (*ac)
9841             ac = &TREE_CHAIN (*ac);
9842           *ac = declarator_access_checks;
9843
9844           /* Parse the function definition.  */
9845           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9846                   (parser, decl_specifiers, prefix_attributes, declarator,
9847                    access_checks));
9848
9849           /* Pull the access-checks apart again.  */
9850           *ac = NULL_TREE;
9851
9852           /* Discard access checks no longer in use.  */
9853           parser->access_checks_lists
9854              = TREE_CHAIN (parser->access_checks_lists);
9855
9856           return decl;
9857         }
9858     }
9859
9860   /* [dcl.dcl]
9861
9862      Only in function declarations for constructors, destructors, and
9863      type conversions can the decl-specifier-seq be omitted.  
9864
9865      We explicitly postpone this check past the point where we handle
9866      function-definitions because we tolerate function-definitions
9867      that are missing their return types in some modes.  */
9868   if (!decl_specifiers && !ctor_dtor_or_conv_p)
9869     {
9870       cp_parser_error (parser, 
9871                        "expected constructor, destructor, or type conversion");
9872       /* Discard access checks no longer in use.  */
9873       parser->access_checks_lists
9874         = TREE_CHAIN (parser->access_checks_lists);
9875       return error_mark_node;
9876     }
9877
9878   /* An `=' or an `(' indicates an initializer.  */
9879   is_initialized = (token->type == CPP_EQ 
9880                      || token->type == CPP_OPEN_PAREN);
9881   /* If the init-declarator isn't initialized and isn't followed by a
9882      `,' or `;', it's not a valid init-declarator.  */
9883   if (!is_initialized 
9884       && token->type != CPP_COMMA
9885       && token->type != CPP_SEMICOLON)
9886     {
9887       cp_parser_error (parser, "expected init-declarator");
9888       /* Discard access checks no longer in use.  */
9889       parser->access_checks_lists
9890          = TREE_CHAIN (parser->access_checks_lists);
9891       return error_mark_node;
9892     }
9893
9894   /* Because start_decl has side-effects, we should only call it if we
9895      know we're going ahead.  By this point, we know that we cannot
9896      possibly be looking at any other construct.  */
9897   cp_parser_commit_to_tentative_parse (parser);
9898
9899   /* Check to see whether or not this declaration is a friend.  */
9900   friend_p = cp_parser_friend_p (decl_specifiers);
9901
9902   /* Check that the number of template-parameter-lists is OK.  */
9903   if (!cp_parser_check_declarator_template_parameters (parser, 
9904                                                        declarator))
9905     {
9906       /* Discard access checks no longer in use.  */
9907       parser->access_checks_lists
9908          = TREE_CHAIN (parser->access_checks_lists);
9909       return error_mark_node;
9910     }
9911
9912   /* Enter the newly declared entry in the symbol table.  If we're
9913      processing a declaration in a class-specifier, we wait until
9914      after processing the initializer.  */
9915   if (!member_p)
9916     {
9917       if (parser->in_unbraced_linkage_specification_p)
9918         {
9919           decl_specifiers = tree_cons (error_mark_node,
9920                                        get_identifier ("extern"),
9921                                        decl_specifiers);
9922           have_extern_spec = false;
9923         }
9924       decl = start_decl (declarator,
9925                          decl_specifiers,
9926                          is_initialized,
9927                          attributes,
9928                          prefix_attributes);
9929     }
9930
9931   /* Enter the SCOPE.  That way unqualified names appearing in the
9932      initializer will be looked up in SCOPE.  */
9933   if (scope)
9934     push_scope (scope);
9935
9936   /* Perform deferred access control checks, now that we know in which
9937      SCOPE the declared entity resides.  */
9938   if (!member_p && decl) 
9939     {
9940       tree saved_current_function_decl = NULL_TREE;
9941
9942       /* If the entity being declared is a function, pretend that we
9943          are in its scope.  If it is a `friend', it may have access to
9944          things that would not otherwise be accessible. */
9945       if (TREE_CODE (decl) == FUNCTION_DECL)
9946         {
9947           saved_current_function_decl = current_function_decl;
9948           current_function_decl = decl;
9949         }
9950         
9951       /* Perform the access control checks for the decl-specifiers.  */
9952       cp_parser_perform_deferred_access_checks (access_checks);
9953       /* And for the declarator.  */
9954       cp_parser_perform_deferred_access_checks (declarator_access_checks);
9955
9956       /* Restore the saved value.  */
9957       if (TREE_CODE (decl) == FUNCTION_DECL)
9958         current_function_decl = saved_current_function_decl;
9959     }
9960
9961   /* Parse the initializer.  */
9962   if (is_initialized)
9963     initializer = cp_parser_initializer (parser, 
9964                                          &is_parenthesized_init);
9965   else
9966     {
9967       initializer = NULL_TREE;
9968       is_parenthesized_init = false;
9969     }
9970
9971   /* The old parser allows attributes to appear after a parenthesized
9972      initializer.  Mark Mitchell proposed removing this functionality
9973      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9974      attributes -- but ignores them.  */
9975   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9976     if (cp_parser_attributes_opt (parser))
9977       warning ("attributes after parenthesized initializer ignored");
9978
9979   /* Leave the SCOPE, now that we have processed the initializer.  It
9980      is important to do this before calling cp_finish_decl because it
9981      makes decisions about whether to create DECL_STMTs or not based
9982      on the current scope.  */
9983   if (scope)
9984     pop_scope (scope);
9985
9986   /* For an in-class declaration, use `grokfield' to create the
9987      declaration.  */
9988   if (member_p)
9989     decl = grokfield (declarator, decl_specifiers,
9990                       initializer, /*asmspec=*/NULL_TREE,
9991                         /*attributes=*/NULL_TREE);
9992
9993   /* Finish processing the declaration.  But, skip friend
9994      declarations.  */
9995   if (!friend_p && decl)
9996     cp_finish_decl (decl, 
9997                     initializer, 
9998                     asm_specification,
9999                     /* If the initializer is in parentheses, then this is
10000                        a direct-initialization, which means that an
10001                        `explicit' constructor is OK.  Otherwise, an
10002                        `explicit' constructor cannot be used.  */
10003                     ((is_parenthesized_init || !is_initialized)
10004                      ? 0 : LOOKUP_ONLYCONVERTING));
10005
10006   /* Discard access checks no longer in use.  */
10007   parser->access_checks_lists
10008     = TREE_CHAIN (parser->access_checks_lists);
10009
10010   return decl;
10011 }
10012
10013 /* Parse a declarator.
10014    
10015    declarator:
10016      direct-declarator
10017      ptr-operator declarator  
10018
10019    abstract-declarator:
10020      ptr-operator abstract-declarator [opt]
10021      direct-abstract-declarator
10022
10023    GNU Extensions:
10024
10025    declarator:
10026      attributes [opt] direct-declarator
10027      attributes [opt] ptr-operator declarator  
10028
10029    abstract-declarator:
10030      attributes [opt] ptr-operator abstract-declarator [opt]
10031      attributes [opt] direct-abstract-declarator
10032      
10033    Returns a representation of the declarator.  If the declarator has
10034    the form `* declarator', then an INDIRECT_REF is returned, whose
10035    only operand is the sub-declarator.  Analagously, `& declarator' is
10036    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10037    used.  The first operand is the TYPE for `X'.  The second operand
10038    is an INDIRECT_REF whose operand is the sub-declarator.
10039
10040    Otherwise, the reprsentation is as for a direct-declarator.
10041
10042    (It would be better to define a structure type to represent
10043    declarators, rather than abusing `tree' nodes to represent
10044    declarators.  That would be much clearer and save some memory.
10045    There is no reason for declarators to be garbage-collected, for
10046    example; they are created during parser and no longer needed after
10047    `grokdeclarator' has been called.)
10048
10049    For a ptr-operator that has the optional cv-qualifier-seq,
10050    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10051    node.
10052
10053    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10054    true if this declarator represents a constructor, destructor, or
10055    type conversion operator.  Otherwise, it is set to false.  
10056
10057    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10058    a decl-specifier-seq unless it declares a constructor, destructor,
10059    or conversion.  It might seem that we could check this condition in
10060    semantic analysis, rather than parsing, but that makes it difficult
10061    to handle something like `f()'.  We want to notice that there are
10062    no decl-specifiers, and therefore realize that this is an
10063    expression, not a declaration.)  */
10064
10065 static tree
10066 cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10067      cp_parser *parser;
10068      cp_parser_declarator_kind dcl_kind;
10069      bool *ctor_dtor_or_conv_p;
10070 {
10071   cp_token *token;
10072   tree declarator;
10073   enum tree_code code;
10074   tree cv_qualifier_seq;
10075   tree class_type;
10076   tree attributes = NULL_TREE;
10077
10078   /* Assume this is not a constructor, destructor, or type-conversion
10079      operator.  */
10080   if (ctor_dtor_or_conv_p)
10081     *ctor_dtor_or_conv_p = false;
10082
10083   if (cp_parser_allow_gnu_extensions_p (parser))
10084     attributes = cp_parser_attributes_opt (parser);
10085   
10086   /* Peek at the next token.  */
10087   token = cp_lexer_peek_token (parser->lexer);
10088   
10089   /* Check for the ptr-operator production.  */
10090   cp_parser_parse_tentatively (parser);
10091   /* Parse the ptr-operator.  */
10092   code = cp_parser_ptr_operator (parser, 
10093                                  &class_type, 
10094                                  &cv_qualifier_seq);
10095   /* If that worked, then we have a ptr-operator.  */
10096   if (cp_parser_parse_definitely (parser))
10097     {
10098       /* The dependent declarator is optional if we are parsing an
10099          abstract-declarator.  */
10100       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10101         cp_parser_parse_tentatively (parser);
10102
10103       /* Parse the dependent declarator.  */
10104       declarator = cp_parser_declarator (parser, dcl_kind,
10105                                          /*ctor_dtor_or_conv_p=*/NULL);
10106
10107       /* If we are parsing an abstract-declarator, we must handle the
10108          case where the dependent declarator is absent.  */
10109       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10110           && !cp_parser_parse_definitely (parser))
10111         declarator = NULL_TREE;
10112         
10113       /* Build the representation of the ptr-operator.  */
10114       if (code == INDIRECT_REF)
10115         declarator = make_pointer_declarator (cv_qualifier_seq, 
10116                                               declarator);
10117       else
10118         declarator = make_reference_declarator (cv_qualifier_seq,
10119                                                 declarator);
10120       /* Handle the pointer-to-member case.  */
10121       if (class_type)
10122         declarator = build_nt (SCOPE_REF, class_type, declarator);
10123     }
10124   /* Everything else is a direct-declarator.  */
10125   else
10126     declarator = cp_parser_direct_declarator (parser, 
10127                                               dcl_kind,
10128                                               ctor_dtor_or_conv_p);
10129
10130   if (attributes && declarator != error_mark_node)
10131     declarator = tree_cons (attributes, declarator, NULL_TREE);
10132   
10133   return declarator;
10134 }
10135
10136 /* Parse a direct-declarator or direct-abstract-declarator.
10137
10138    direct-declarator:
10139      declarator-id
10140      direct-declarator ( parameter-declaration-clause )
10141        cv-qualifier-seq [opt] 
10142        exception-specification [opt]
10143      direct-declarator [ constant-expression [opt] ]
10144      ( declarator )  
10145
10146    direct-abstract-declarator:
10147      direct-abstract-declarator [opt]
10148        ( parameter-declaration-clause ) 
10149        cv-qualifier-seq [opt]
10150        exception-specification [opt]
10151      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10152      ( abstract-declarator )
10153
10154    Returns a representation of the declarator.  DCL_KIND is
10155    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10156    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10157    we are parsing a direct-declarator.  It is
10158    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10159    of ambiguity we prefer an abstract declarator, as per
10160    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10161    cp_parser_declarator.
10162
10163    For the declarator-id production, the representation is as for an
10164    id-expression, except that a qualified name is represented as a
10165    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10166    see the documentation of the FUNCTION_DECLARATOR_* macros for
10167    information about how to find the various declarator components.
10168    An array-declarator is represented as an ARRAY_REF.  The
10169    direct-declarator is the first operand; the constant-expression
10170    indicating the size of the array is the second operand.  */
10171
10172 static tree
10173 cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10174      cp_parser *parser;
10175      cp_parser_declarator_kind dcl_kind;
10176      bool *ctor_dtor_or_conv_p;
10177 {
10178   cp_token *token;
10179   tree declarator = NULL_TREE;
10180   tree scope = NULL_TREE;
10181   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10182   bool saved_in_declarator_p = parser->in_declarator_p;
10183   bool first = true;
10184   
10185   while (true)
10186     {
10187       /* Peek at the next token.  */
10188       token = cp_lexer_peek_token (parser->lexer);
10189       if (token->type == CPP_OPEN_PAREN)
10190         {
10191           /* This is either a parameter-declaration-clause, or a
10192              parenthesized declarator. When we know we are parsing a
10193              named declarator, it must be a paranthesized declarator
10194              if FIRST is true. For instance, `(int)' is a
10195              parameter-declaration-clause, with an omitted
10196              direct-abstract-declarator. But `((*))', is a
10197              parenthesized abstract declarator. Finally, when T is a
10198              template parameter `(T)' is a
10199              paremeter-declaration-clause, and not a parenthesized
10200              named declarator.
10201              
10202              We first try and parse a parameter-declaration-clause,
10203              and then try a nested declarator (if FIRST is true).
10204
10205              It is not an error for it not to be a
10206              parameter-declaration-clause, even when FIRST is
10207              false. Consider,
10208
10209                int i (int);
10210                int i (3);
10211
10212              The first is the declaration of a function while the
10213              second is a the definition of a variable, including its
10214              initializer.
10215
10216              Having seen only the parenthesis, we cannot know which of
10217              these two alternatives should be selected.  Even more
10218              complex are examples like:
10219
10220                int i (int (a));
10221                int i (int (3));
10222
10223              The former is a function-declaration; the latter is a
10224              variable initialization.  
10225
10226              Thus again, we try a parameter-declation-clause, and if
10227              that fails, we back out and return.  */
10228
10229           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10230             {
10231               tree params;
10232               
10233               cp_parser_parse_tentatively (parser);
10234
10235               /* Consume the `('.  */
10236               cp_lexer_consume_token (parser->lexer);
10237               if (first)
10238                 {
10239                   /* If this is going to be an abstract declarator, we're
10240                      in a declarator and we can't have default args.  */
10241                   parser->default_arg_ok_p = false;
10242                   parser->in_declarator_p = true;
10243                 }
10244           
10245               /* Parse the parameter-declaration-clause.  */
10246               params = cp_parser_parameter_declaration_clause (parser);
10247
10248               /* If all went well, parse the cv-qualifier-seq and the
10249                  exception-specfication.  */
10250               if (cp_parser_parse_definitely (parser))
10251                 {
10252                   tree cv_qualifiers;
10253                   tree exception_specification;
10254                   
10255                   first = false;
10256                   /* Consume the `)'.  */
10257                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10258
10259                   /* Parse the cv-qualifier-seq.  */
10260                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10261                   /* And the exception-specification.  */
10262                   exception_specification 
10263                     = cp_parser_exception_specification_opt (parser);
10264
10265                   /* Create the function-declarator.  */
10266                   declarator = make_call_declarator (declarator,
10267                                                      params,
10268                                                      cv_qualifiers,
10269                                                      exception_specification);
10270                   /* Any subsequent parameter lists are to do with
10271                      return type, so are not those of the declared
10272                      function.  */
10273                   parser->default_arg_ok_p = false;
10274                   
10275                   /* Repeat the main loop.  */
10276                   continue;
10277                 }
10278             }
10279           
10280           /* If this is the first, we can try a parenthesized
10281              declarator.  */
10282           if (first)
10283             {
10284               parser->default_arg_ok_p = saved_default_arg_ok_p;
10285               parser->in_declarator_p = saved_in_declarator_p;
10286               
10287               /* Consume the `('.  */
10288               cp_lexer_consume_token (parser->lexer);
10289               /* Parse the nested declarator.  */
10290               declarator 
10291                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10292               first = false;
10293               /* Expect a `)'.  */
10294               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10295                 declarator = error_mark_node;
10296               if (declarator == error_mark_node)
10297                 break;
10298               
10299               goto handle_declarator;
10300             }
10301           /* Otherwise, we must be done. */
10302           else
10303             break;
10304         }
10305       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10306                && token->type == CPP_OPEN_SQUARE)
10307         {
10308           /* Parse an array-declarator.  */
10309           tree bounds;
10310
10311           first = false;
10312           parser->default_arg_ok_p = false;
10313           parser->in_declarator_p = true;
10314           /* Consume the `['.  */
10315           cp_lexer_consume_token (parser->lexer);
10316           /* Peek at the next token.  */
10317           token = cp_lexer_peek_token (parser->lexer);
10318           /* If the next token is `]', then there is no
10319              constant-expression.  */
10320           if (token->type != CPP_CLOSE_SQUARE)
10321             bounds = cp_parser_constant_expression (parser);
10322           else
10323             bounds = NULL_TREE;
10324           /* Look for the closing `]'.  */
10325           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10326             {
10327               declarator = error_mark_node;
10328               break;
10329             }
10330
10331           declarator = build_nt (ARRAY_REF, declarator, bounds);
10332         }
10333       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10334         {
10335           /* Parse a declarator_id */
10336           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10337             cp_parser_parse_tentatively (parser);
10338           declarator = cp_parser_declarator_id (parser);
10339           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10340               && !cp_parser_parse_definitely (parser))
10341             declarator = error_mark_node;
10342           if (declarator == error_mark_node)
10343             break;
10344           
10345           if (TREE_CODE (declarator) == SCOPE_REF)
10346             {
10347               tree scope = TREE_OPERAND (declarator, 0);
10348           
10349               /* In the declaration of a member of a template class
10350                  outside of the class itself, the SCOPE will sometimes
10351                  be a TYPENAME_TYPE.  For example, given:
10352                   
10353                  template <typename T>
10354                  int S<T>::R::i = 3;
10355                   
10356                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10357                  this context, we must resolve S<T>::R to an ordinary
10358                  type, rather than a typename type.
10359                   
10360                  The reason we normally avoid resolving TYPENAME_TYPEs
10361                  is that a specialization of `S' might render
10362                  `S<T>::R' not a type.  However, if `S' is
10363                  specialized, then this `i' will not be used, so there
10364                  is no harm in resolving the types here.  */
10365               if (TREE_CODE (scope) == TYPENAME_TYPE)
10366                 {
10367                   /* Resolve the TYPENAME_TYPE.  */
10368                   scope = cp_parser_resolve_typename_type (parser, scope);
10369                   /* If that failed, the declarator is invalid.  */
10370                   if (scope == error_mark_node)
10371                     return error_mark_node;
10372                   /* Build a new DECLARATOR.  */
10373                   declarator = build_nt (SCOPE_REF, 
10374                                          scope,
10375                                          TREE_OPERAND (declarator, 1));
10376                 }
10377             }
10378       
10379           /* Check to see whether the declarator-id names a constructor, 
10380              destructor, or conversion.  */
10381           if (declarator && ctor_dtor_or_conv_p 
10382               && ((TREE_CODE (declarator) == SCOPE_REF 
10383                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10384                   || (TREE_CODE (declarator) != SCOPE_REF
10385                       && at_class_scope_p ())))
10386             {
10387               tree unqualified_name;
10388               tree class_type;
10389
10390               /* Get the unqualified part of the name.  */
10391               if (TREE_CODE (declarator) == SCOPE_REF)
10392                 {
10393                   class_type = TREE_OPERAND (declarator, 0);
10394                   unqualified_name = TREE_OPERAND (declarator, 1);
10395                 }
10396               else
10397                 {
10398                   class_type = current_class_type;
10399                   unqualified_name = declarator;
10400                 }
10401
10402               /* See if it names ctor, dtor or conv.  */
10403               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10404                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10405                   || constructor_name_p (unqualified_name, class_type))
10406                 *ctor_dtor_or_conv_p = true;
10407             }
10408
10409         handle_declarator:;
10410           scope = get_scope_of_declarator (declarator);
10411           if (scope)
10412             /* Any names that appear after the declarator-id for a member
10413                are looked up in the containing scope.  */
10414             push_scope (scope);
10415           parser->in_declarator_p = true;
10416           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10417               || (declarator
10418                   && (TREE_CODE (declarator) == SCOPE_REF
10419                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10420             /* Default args are only allowed on function
10421                declarations.  */
10422             parser->default_arg_ok_p = saved_default_arg_ok_p;
10423           else
10424             parser->default_arg_ok_p = false;
10425
10426           first = false;
10427         }
10428       /* We're done.  */
10429       else
10430         break;
10431     }
10432
10433   /* For an abstract declarator, we might wind up with nothing at this
10434      point.  That's an error; the declarator is not optional.  */
10435   if (!declarator)
10436     cp_parser_error (parser, "expected declarator");
10437
10438   /* If we entered a scope, we must exit it now.  */
10439   if (scope)
10440     pop_scope (scope);
10441
10442   parser->default_arg_ok_p = saved_default_arg_ok_p;
10443   parser->in_declarator_p = saved_in_declarator_p;
10444   
10445   return declarator;
10446 }
10447
10448 /* Parse a ptr-operator.  
10449
10450    ptr-operator:
10451      * cv-qualifier-seq [opt]
10452      &
10453      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10454
10455    GNU Extension:
10456
10457    ptr-operator:
10458      & cv-qualifier-seq [opt]
10459
10460    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10461    used.  Returns ADDR_EXPR if a reference was used.  In the
10462    case of a pointer-to-member, *TYPE is filled in with the 
10463    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10464    with the cv-qualifier-seq, or NULL_TREE, if there are no
10465    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10466    
10467 static enum tree_code
10468 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10469      cp_parser *parser;
10470      tree *type;
10471      tree *cv_qualifier_seq;
10472 {
10473   enum tree_code code = ERROR_MARK;
10474   cp_token *token;
10475
10476   /* Assume that it's not a pointer-to-member.  */
10477   *type = NULL_TREE;
10478   /* And that there are no cv-qualifiers.  */
10479   *cv_qualifier_seq = NULL_TREE;
10480
10481   /* Peek at the next token.  */
10482   token = cp_lexer_peek_token (parser->lexer);
10483   /* If it's a `*' or `&' we have a pointer or reference.  */
10484   if (token->type == CPP_MULT || token->type == CPP_AND)
10485     {
10486       /* Remember which ptr-operator we were processing.  */
10487       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10488
10489       /* Consume the `*' or `&'.  */
10490       cp_lexer_consume_token (parser->lexer);
10491
10492       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10493          `&', if we are allowing GNU extensions.  (The only qualifier
10494          that can legally appear after `&' is `restrict', but that is
10495          enforced during semantic analysis.  */
10496       if (code == INDIRECT_REF 
10497           || cp_parser_allow_gnu_extensions_p (parser))
10498         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10499     }
10500   else
10501     {
10502       /* Try the pointer-to-member case.  */
10503       cp_parser_parse_tentatively (parser);
10504       /* Look for the optional `::' operator.  */
10505       cp_parser_global_scope_opt (parser,
10506                                   /*current_scope_valid_p=*/false);
10507       /* Look for the nested-name specifier.  */
10508       cp_parser_nested_name_specifier (parser,
10509                                        /*typename_keyword_p=*/false,
10510                                        /*check_dependency_p=*/true,
10511                                        /*type_p=*/false);
10512       /* If we found it, and the next token is a `*', then we are
10513          indeed looking at a pointer-to-member operator.  */
10514       if (!cp_parser_error_occurred (parser)
10515           && cp_parser_require (parser, CPP_MULT, "`*'"))
10516         {
10517           /* The type of which the member is a member is given by the
10518              current SCOPE.  */
10519           *type = parser->scope;
10520           /* The next name will not be qualified.  */
10521           parser->scope = NULL_TREE;
10522           parser->qualifying_scope = NULL_TREE;
10523           parser->object_scope = NULL_TREE;
10524           /* Indicate that the `*' operator was used.  */
10525           code = INDIRECT_REF;
10526           /* Look for the optional cv-qualifier-seq.  */
10527           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10528         }
10529       /* If that didn't work we don't have a ptr-operator.  */
10530       if (!cp_parser_parse_definitely (parser))
10531         cp_parser_error (parser, "expected ptr-operator");
10532     }
10533
10534   return code;
10535 }
10536
10537 /* Parse an (optional) cv-qualifier-seq.
10538
10539    cv-qualifier-seq:
10540      cv-qualifier cv-qualifier-seq [opt]  
10541
10542    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10543    representation of a cv-qualifier.  */
10544
10545 static tree
10546 cp_parser_cv_qualifier_seq_opt (parser)
10547      cp_parser *parser;
10548 {
10549   tree cv_qualifiers = NULL_TREE;
10550   
10551   while (true)
10552     {
10553       tree cv_qualifier;
10554
10555       /* Look for the next cv-qualifier.  */
10556       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10557       /* If we didn't find one, we're done.  */
10558       if (!cv_qualifier)
10559         break;
10560
10561       /* Add this cv-qualifier to the list.  */
10562       cv_qualifiers 
10563         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10564     }
10565
10566   /* We built up the list in reverse order.  */
10567   return nreverse (cv_qualifiers);
10568 }
10569
10570 /* Parse an (optional) cv-qualifier.
10571
10572    cv-qualifier:
10573      const
10574      volatile  
10575
10576    GNU Extension:
10577
10578    cv-qualifier:
10579      __restrict__ */
10580
10581 static tree
10582 cp_parser_cv_qualifier_opt (parser)
10583      cp_parser *parser;
10584 {
10585   cp_token *token;
10586   tree cv_qualifier = NULL_TREE;
10587
10588   /* Peek at the next token.  */
10589   token = cp_lexer_peek_token (parser->lexer);
10590   /* See if it's a cv-qualifier.  */
10591   switch (token->keyword)
10592     {
10593     case RID_CONST:
10594     case RID_VOLATILE:
10595     case RID_RESTRICT:
10596       /* Save the value of the token.  */
10597       cv_qualifier = token->value;
10598       /* Consume the token.  */
10599       cp_lexer_consume_token (parser->lexer);
10600       break;
10601
10602     default:
10603       break;
10604     }
10605
10606   return cv_qualifier;
10607 }
10608
10609 /* Parse a declarator-id.
10610
10611    declarator-id:
10612      id-expression
10613      :: [opt] nested-name-specifier [opt] type-name  
10614
10615    In the `id-expression' case, the value returned is as for
10616    cp_parser_id_expression if the id-expression was an unqualified-id.
10617    If the id-expression was a qualified-id, then a SCOPE_REF is
10618    returned.  The first operand is the scope (either a NAMESPACE_DECL
10619    or TREE_TYPE), but the second is still just a representation of an
10620    unqualified-id.  */
10621
10622 static tree
10623 cp_parser_declarator_id (parser)
10624      cp_parser *parser;
10625 {
10626   tree id_expression;
10627
10628   /* The expression must be an id-expression.  Assume that qualified
10629      names are the names of types so that:
10630
10631        template <class T>
10632        int S<T>::R::i = 3;
10633
10634      will work; we must treat `S<T>::R' as the name of a type.
10635      Similarly, assume that qualified names are templates, where
10636      required, so that:
10637
10638        template <class T>
10639        int S<T>::R<T>::i = 3;
10640
10641      will work, too.  */
10642   id_expression = cp_parser_id_expression (parser,
10643                                            /*template_keyword_p=*/false,
10644                                            /*check_dependency_p=*/false,
10645                                            /*template_p=*/NULL);
10646   /* If the name was qualified, create a SCOPE_REF to represent 
10647      that.  */
10648   if (parser->scope)
10649     id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10650
10651   return id_expression;
10652 }
10653
10654 /* Parse a type-id.
10655
10656    type-id:
10657      type-specifier-seq abstract-declarator [opt]
10658
10659    Returns the TYPE specified.  */
10660
10661 static tree
10662 cp_parser_type_id (parser)
10663      cp_parser *parser;
10664 {
10665   tree type_specifier_seq;
10666   tree abstract_declarator;
10667
10668   /* Parse the type-specifier-seq.  */
10669   type_specifier_seq 
10670     = cp_parser_type_specifier_seq (parser);
10671   if (type_specifier_seq == error_mark_node)
10672     return error_mark_node;
10673
10674   /* There might or might not be an abstract declarator.  */
10675   cp_parser_parse_tentatively (parser);
10676   /* Look for the declarator.  */
10677   abstract_declarator 
10678     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10679   /* Check to see if there really was a declarator.  */
10680   if (!cp_parser_parse_definitely (parser))
10681     abstract_declarator = NULL_TREE;
10682
10683   return groktypename (build_tree_list (type_specifier_seq,
10684                                         abstract_declarator));
10685 }
10686
10687 /* Parse a type-specifier-seq.
10688
10689    type-specifier-seq:
10690      type-specifier type-specifier-seq [opt]
10691
10692    GNU extension:
10693
10694    type-specifier-seq:
10695      attributes type-specifier-seq [opt]
10696
10697    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10698    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10699
10700 static tree
10701 cp_parser_type_specifier_seq (parser)
10702      cp_parser *parser;
10703 {
10704   bool seen_type_specifier = false;
10705   tree type_specifier_seq = NULL_TREE;
10706
10707   /* Parse the type-specifiers and attributes.  */
10708   while (true)
10709     {
10710       tree type_specifier;
10711
10712       /* Check for attributes first.  */
10713       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10714         {
10715           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10716                                           NULL_TREE,
10717                                           type_specifier_seq);
10718           continue;
10719         }
10720
10721       /* After the first type-specifier, others are optional.  */
10722       if (seen_type_specifier)
10723         cp_parser_parse_tentatively (parser);
10724       /* Look for the type-specifier.  */
10725       type_specifier = cp_parser_type_specifier (parser, 
10726                                                  CP_PARSER_FLAGS_NONE,
10727                                                  /*is_friend=*/false,
10728                                                  /*is_declaration=*/false,
10729                                                  NULL,
10730                                                  NULL);
10731       /* If the first type-specifier could not be found, this is not a
10732          type-specifier-seq at all.  */
10733       if (!seen_type_specifier && type_specifier == error_mark_node)
10734         return error_mark_node;
10735       /* If subsequent type-specifiers could not be found, the
10736          type-specifier-seq is complete.  */
10737       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10738         break;
10739
10740       /* Add the new type-specifier to the list.  */
10741       type_specifier_seq 
10742         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10743       seen_type_specifier = true;
10744     }
10745
10746   /* We built up the list in reverse order.  */
10747   return nreverse (type_specifier_seq);
10748 }
10749
10750 /* Parse a parameter-declaration-clause.
10751
10752    parameter-declaration-clause:
10753      parameter-declaration-list [opt] ... [opt]
10754      parameter-declaration-list , ...
10755
10756    Returns a representation for the parameter declarations.  Each node
10757    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10758    representation.)  If the parameter-declaration-clause ends with an
10759    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10760    list.  A return value of NULL_TREE indicates a
10761    parameter-declaration-clause consisting only of an ellipsis.  */
10762
10763 static tree
10764 cp_parser_parameter_declaration_clause (parser)
10765      cp_parser *parser;
10766 {
10767   tree parameters;
10768   cp_token *token;
10769   bool ellipsis_p;
10770
10771   /* Peek at the next token.  */
10772   token = cp_lexer_peek_token (parser->lexer);
10773   /* Check for trivial parameter-declaration-clauses.  */
10774   if (token->type == CPP_ELLIPSIS)
10775     {
10776       /* Consume the `...' token.  */
10777       cp_lexer_consume_token (parser->lexer);
10778       return NULL_TREE;
10779     }
10780   else if (token->type == CPP_CLOSE_PAREN)
10781     /* There are no parameters.  */
10782     {
10783 #ifndef NO_IMPLICIT_EXTERN_C
10784       if (in_system_header && current_class_type == NULL
10785           && current_lang_name == lang_name_c)
10786         return NULL_TREE;
10787       else
10788 #endif
10789         return void_list_node;
10790     }
10791   /* Check for `(void)', too, which is a special case.  */
10792   else if (token->keyword == RID_VOID
10793            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10794                == CPP_CLOSE_PAREN))
10795     {
10796       /* Consume the `void' token.  */
10797       cp_lexer_consume_token (parser->lexer);
10798       /* There are no parameters.  */
10799       return void_list_node;
10800     }
10801   
10802   /* Parse the parameter-declaration-list.  */
10803   parameters = cp_parser_parameter_declaration_list (parser);
10804   /* If a parse error occurred while parsing the
10805      parameter-declaration-list, then the entire
10806      parameter-declaration-clause is erroneous.  */
10807   if (parameters == error_mark_node)
10808     return error_mark_node;
10809
10810   /* Peek at the next token.  */
10811   token = cp_lexer_peek_token (parser->lexer);
10812   /* If it's a `,', the clause should terminate with an ellipsis.  */
10813   if (token->type == CPP_COMMA)
10814     {
10815       /* Consume the `,'.  */
10816       cp_lexer_consume_token (parser->lexer);
10817       /* Expect an ellipsis.  */
10818       ellipsis_p 
10819         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10820     }
10821   /* It might also be `...' if the optional trailing `,' was 
10822      omitted.  */
10823   else if (token->type == CPP_ELLIPSIS)
10824     {
10825       /* Consume the `...' token.  */
10826       cp_lexer_consume_token (parser->lexer);
10827       /* And remember that we saw it.  */
10828       ellipsis_p = true;
10829     }
10830   else
10831     ellipsis_p = false;
10832
10833   /* Finish the parameter list.  */
10834   return finish_parmlist (parameters, ellipsis_p);
10835 }
10836
10837 /* Parse a parameter-declaration-list.
10838
10839    parameter-declaration-list:
10840      parameter-declaration
10841      parameter-declaration-list , parameter-declaration
10842
10843    Returns a representation of the parameter-declaration-list, as for
10844    cp_parser_parameter_declaration_clause.  However, the
10845    `void_list_node' is never appended to the list.  */
10846
10847 static tree
10848 cp_parser_parameter_declaration_list (parser)
10849      cp_parser *parser;
10850 {
10851   tree parameters = NULL_TREE;
10852
10853   /* Look for more parameters.  */
10854   while (true)
10855     {
10856       tree parameter;
10857       /* Parse the parameter.  */
10858       parameter 
10859         = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10860
10861       /* If a parse error ocurred parsing the parameter declaration,
10862          then the entire parameter-declaration-list is erroneous.  */
10863       if (parameter == error_mark_node)
10864         {
10865           parameters = error_mark_node;
10866           break;
10867         }
10868       /* Add the new parameter to the list.  */
10869       TREE_CHAIN (parameter) = parameters;
10870       parameters = parameter;
10871
10872       /* Peek at the next token.  */
10873       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10874           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10875         /* The parameter-declaration-list is complete.  */
10876         break;
10877       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10878         {
10879           cp_token *token;
10880
10881           /* Peek at the next token.  */
10882           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10883           /* If it's an ellipsis, then the list is complete.  */
10884           if (token->type == CPP_ELLIPSIS)
10885             break;
10886           /* Otherwise, there must be more parameters.  Consume the
10887              `,'.  */
10888           cp_lexer_consume_token (parser->lexer);
10889         }
10890       else
10891         {
10892           cp_parser_error (parser, "expected `,' or `...'");
10893           break;
10894         }
10895     }
10896
10897   /* We built up the list in reverse order; straighten it out now.  */
10898   return nreverse (parameters);
10899 }
10900
10901 /* Parse a parameter declaration.
10902
10903    parameter-declaration:
10904      decl-specifier-seq declarator
10905      decl-specifier-seq declarator = assignment-expression
10906      decl-specifier-seq abstract-declarator [opt]
10907      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10908
10909    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10910    declares a template parameter.  (In that case, a non-nested `>'
10911    token encountered during the parsing of the assignment-expression
10912    is not interpreted as a greater-than operator.)
10913
10914    Returns a TREE_LIST representing the parameter-declaration.  The
10915    TREE_VALUE is a representation of the decl-specifier-seq and
10916    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10917    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10918    TREE_VALUE represents the declarator.  */
10919
10920 static tree
10921 cp_parser_parameter_declaration (cp_parser *parser, 
10922                                  bool template_parm_p)
10923 {
10924   bool declares_class_or_enum;
10925   bool greater_than_is_operator_p;
10926   tree decl_specifiers;
10927   tree attributes;
10928   tree declarator;
10929   tree default_argument;
10930   tree parameter;
10931   cp_token *token;
10932   const char *saved_message;
10933
10934   /* In a template parameter, `>' is not an operator.
10935
10936      [temp.param]
10937
10938      When parsing a default template-argument for a non-type
10939      template-parameter, the first non-nested `>' is taken as the end
10940      of the template parameter-list rather than a greater-than
10941      operator.  */
10942   greater_than_is_operator_p = !template_parm_p;
10943
10944   /* Type definitions may not appear in parameter types.  */
10945   saved_message = parser->type_definition_forbidden_message;
10946   parser->type_definition_forbidden_message 
10947     = "types may not be defined in parameter types";
10948
10949   /* Parse the declaration-specifiers.  */
10950   decl_specifiers 
10951     = cp_parser_decl_specifier_seq (parser,
10952                                     CP_PARSER_FLAGS_NONE,
10953                                     &attributes,
10954                                     &declares_class_or_enum);
10955   /* If an error occurred, there's no reason to attempt to parse the
10956      rest of the declaration.  */
10957   if (cp_parser_error_occurred (parser))
10958     {
10959       parser->type_definition_forbidden_message = saved_message;
10960       return error_mark_node;
10961     }
10962
10963   /* Peek at the next token.  */
10964   token = cp_lexer_peek_token (parser->lexer);
10965   /* If the next token is a `)', `,', `=', `>', or `...', then there
10966      is no declarator.  */
10967   if (token->type == CPP_CLOSE_PAREN 
10968       || token->type == CPP_COMMA
10969       || token->type == CPP_EQ
10970       || token->type == CPP_ELLIPSIS
10971       || token->type == CPP_GREATER)
10972     declarator = NULL_TREE;
10973   /* Otherwise, there should be a declarator.  */
10974   else
10975     {
10976       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10977       parser->default_arg_ok_p = false;
10978   
10979       declarator = cp_parser_declarator (parser,
10980                                          CP_PARSER_DECLARATOR_EITHER,
10981                                          /*ctor_dtor_or_conv_p=*/NULL);
10982       parser->default_arg_ok_p = saved_default_arg_ok_p;
10983       /* After the declarator, allow more attributes.  */
10984       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10985     }
10986
10987   /* The restriction on defining new types applies only to the type
10988      of the parameter, not to the default argument.  */
10989   parser->type_definition_forbidden_message = saved_message;
10990
10991   /* If the next token is `=', then process a default argument.  */
10992   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10993     {
10994       bool saved_greater_than_is_operator_p;
10995       /* Consume the `='.  */
10996       cp_lexer_consume_token (parser->lexer);
10997
10998       /* If we are defining a class, then the tokens that make up the
10999          default argument must be saved and processed later.  */
11000       if (!template_parm_p && at_class_scope_p () 
11001           && TYPE_BEING_DEFINED (current_class_type))
11002         {
11003           unsigned depth = 0;
11004
11005           /* Create a DEFAULT_ARG to represented the unparsed default
11006              argument.  */
11007           default_argument = make_node (DEFAULT_ARG);
11008           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11009
11010           /* Add tokens until we have processed the entire default
11011              argument.  */
11012           while (true)
11013             {
11014               bool done = false;
11015               cp_token *token;
11016
11017               /* Peek at the next token.  */
11018               token = cp_lexer_peek_token (parser->lexer);
11019               /* What we do depends on what token we have.  */
11020               switch (token->type)
11021                 {
11022                   /* In valid code, a default argument must be
11023                      immediately followed by a `,' `)', or `...'.  */
11024                 case CPP_COMMA:
11025                 case CPP_CLOSE_PAREN:
11026                 case CPP_ELLIPSIS:
11027                   /* If we run into a non-nested `;', `}', or `]',
11028                      then the code is invalid -- but the default
11029                      argument is certainly over.  */
11030                 case CPP_SEMICOLON:
11031                 case CPP_CLOSE_BRACE:
11032                 case CPP_CLOSE_SQUARE:
11033                   if (depth == 0)
11034                     done = true;
11035                   /* Update DEPTH, if necessary.  */
11036                   else if (token->type == CPP_CLOSE_PAREN
11037                            || token->type == CPP_CLOSE_BRACE
11038                            || token->type == CPP_CLOSE_SQUARE)
11039                     --depth;
11040                   break;
11041
11042                 case CPP_OPEN_PAREN:
11043                 case CPP_OPEN_SQUARE:
11044                 case CPP_OPEN_BRACE:
11045                   ++depth;
11046                   break;
11047
11048                 case CPP_GREATER:
11049                   /* If we see a non-nested `>', and `>' is not an
11050                      operator, then it marks the end of the default
11051                      argument.  */
11052                   if (!depth && !greater_than_is_operator_p)
11053                     done = true;
11054                   break;
11055
11056                   /* If we run out of tokens, issue an error message.  */
11057                 case CPP_EOF:
11058                   error ("file ends in default argument");
11059                   done = true;
11060                   break;
11061
11062                 case CPP_NAME:
11063                 case CPP_SCOPE:
11064                   /* In these cases, we should look for template-ids.
11065                      For example, if the default argument is 
11066                      `X<int, double>()', we need to do name lookup to
11067                      figure out whether or not `X' is a template; if
11068                      so, the `,' does not end the deault argument.
11069
11070                      That is not yet done.  */
11071                   break;
11072
11073                 default:
11074                   break;
11075                 }
11076
11077               /* If we've reached the end, stop.  */
11078               if (done)
11079                 break;
11080               
11081               /* Add the token to the token block.  */
11082               token = cp_lexer_consume_token (parser->lexer);
11083               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11084                                          token);
11085             }
11086         }
11087       /* Outside of a class definition, we can just parse the
11088          assignment-expression.  */
11089       else
11090         {
11091           bool saved_local_variables_forbidden_p;
11092
11093           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11094              set correctly.  */
11095           saved_greater_than_is_operator_p 
11096             = parser->greater_than_is_operator_p;
11097           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11098           /* Local variable names (and the `this' keyword) may not
11099              appear in a default argument.  */
11100           saved_local_variables_forbidden_p 
11101             = parser->local_variables_forbidden_p;
11102           parser->local_variables_forbidden_p = true;
11103           /* Parse the assignment-expression.  */
11104           default_argument = cp_parser_assignment_expression (parser);
11105           /* Restore saved state.  */
11106           parser->greater_than_is_operator_p 
11107             = saved_greater_than_is_operator_p;
11108           parser->local_variables_forbidden_p 
11109             = saved_local_variables_forbidden_p; 
11110         }
11111       if (!parser->default_arg_ok_p)
11112         {
11113           pedwarn ("default arguments are only permitted on functions");
11114           if (flag_pedantic_errors)
11115             default_argument = NULL_TREE;
11116         }
11117     }
11118   else
11119     default_argument = NULL_TREE;
11120   
11121   /* Create the representation of the parameter.  */
11122   if (attributes)
11123     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11124   parameter = build_tree_list (default_argument, 
11125                                build_tree_list (decl_specifiers,
11126                                                 declarator));
11127
11128   return parameter;
11129 }
11130
11131 /* Parse a function-definition.  
11132
11133    function-definition:
11134      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11135        function-body 
11136      decl-specifier-seq [opt] declarator function-try-block  
11137
11138    GNU Extension:
11139
11140    function-definition:
11141      __extension__ function-definition 
11142
11143    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
11144    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11145    be a `friend'.  */
11146
11147 static tree
11148 cp_parser_function_definition (parser, friend_p)
11149      cp_parser *parser;
11150      bool *friend_p;
11151 {
11152   tree decl_specifiers;
11153   tree attributes;
11154   tree declarator;
11155   tree fn;
11156   tree access_checks;
11157   cp_token *token;
11158   bool declares_class_or_enum;
11159   bool member_p;
11160   /* The saved value of the PEDANTIC flag.  */
11161   int saved_pedantic;
11162
11163   /* Any pending qualification must be cleared by our caller.  It is
11164      more robust to force the callers to clear PARSER->SCOPE than to
11165      do it here since if the qualification is in effect here, it might
11166      also end up in effect elsewhere that it is not intended.  */
11167   my_friendly_assert (!parser->scope, 20010821);
11168
11169   /* Handle `__extension__'.  */
11170   if (cp_parser_extension_opt (parser, &saved_pedantic))
11171     {
11172       /* Parse the function-definition.  */
11173       fn = cp_parser_function_definition (parser, friend_p);
11174       /* Restore the PEDANTIC flag.  */
11175       pedantic = saved_pedantic;
11176
11177       return fn;
11178     }
11179
11180   /* Check to see if this definition appears in a class-specifier.  */
11181   member_p = (at_class_scope_p () 
11182               && TYPE_BEING_DEFINED (current_class_type));
11183   /* Defer access checks in the decl-specifier-seq until we know what
11184      function is being defined.  There is no need to do this for the
11185      definition of member functions; we cannot be defining a member
11186      from another class.  */
11187   if (!member_p)
11188     cp_parser_start_deferring_access_checks (parser);
11189   /* Parse the decl-specifier-seq.  */
11190   decl_specifiers 
11191     = cp_parser_decl_specifier_seq (parser,
11192                                     CP_PARSER_FLAGS_OPTIONAL,
11193                                     &attributes,
11194                                     &declares_class_or_enum);
11195   /* Figure out whether this declaration is a `friend'.  */
11196   if (friend_p)
11197     *friend_p = cp_parser_friend_p (decl_specifiers);
11198
11199   /* Parse the declarator.  */
11200   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11201                                      /*ctor_dtor_or_conv_p=*/NULL);
11202
11203   /* Gather up any access checks that occurred.  */
11204   if (!member_p)
11205     access_checks = cp_parser_stop_deferring_access_checks (parser);
11206   else
11207     access_checks = NULL_TREE;
11208
11209   /* If something has already gone wrong, we may as well stop now.  */
11210   if (declarator == error_mark_node)
11211     {
11212       /* Skip to the end of the function, or if this wasn't anything
11213          like a function-definition, to a `;' in the hopes of finding
11214          a sensible place from which to continue parsing.  */
11215       cp_parser_skip_to_end_of_block_or_statement (parser);
11216       return error_mark_node;
11217     }
11218
11219   /* The next character should be a `{' (for a simple function
11220      definition), a `:' (for a ctor-initializer), or `try' (for a
11221      function-try block).  */
11222   token = cp_lexer_peek_token (parser->lexer);
11223   if (!cp_parser_token_starts_function_definition_p (token))
11224     {
11225       /* Issue the error-message.  */
11226       cp_parser_error (parser, "expected function-definition");
11227       /* Skip to the next `;'.  */
11228       cp_parser_skip_to_end_of_block_or_statement (parser);
11229
11230       return error_mark_node;
11231     }
11232
11233   /* If we are in a class scope, then we must handle
11234      function-definitions specially.  In particular, we save away the
11235      tokens that make up the function body, and parse them again
11236      later, in order to handle code like:
11237
11238        struct S {
11239          int f () { return i; }
11240          int i;
11241        }; 
11242  
11243      Here, we cannot parse the body of `f' until after we have seen
11244      the declaration of `i'.  */
11245   if (member_p)
11246     {
11247       cp_token_cache *cache;
11248
11249       /* Create the function-declaration.  */
11250       fn = start_method (decl_specifiers, declarator, attributes);
11251       /* If something went badly wrong, bail out now.  */
11252       if (fn == error_mark_node)
11253         {
11254           /* If there's a function-body, skip it.  */
11255           if (cp_parser_token_starts_function_definition_p 
11256               (cp_lexer_peek_token (parser->lexer)))
11257             cp_parser_skip_to_end_of_block_or_statement (parser);
11258           return error_mark_node;
11259         }
11260
11261       /* Create a token cache.  */
11262       cache = cp_token_cache_new ();
11263       /* Save away the tokens that make up the body of the 
11264          function.  */
11265       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11266       /* Handle function try blocks.  */
11267       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11268         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11269
11270       /* Save away the inline definition; we will process it when the
11271          class is complete.  */
11272       DECL_PENDING_INLINE_INFO (fn) = cache;
11273       DECL_PENDING_INLINE_P (fn) = 1;
11274
11275       /* We're done with the inline definition.  */
11276       finish_method (fn);
11277
11278       /* Add FN to the queue of functions to be parsed later.  */
11279       TREE_VALUE (parser->unparsed_functions_queues)
11280         = tree_cons (NULL_TREE, fn, 
11281                      TREE_VALUE (parser->unparsed_functions_queues));
11282
11283       return fn;
11284     }
11285
11286   /* Check that the number of template-parameter-lists is OK.  */
11287   if (!cp_parser_check_declarator_template_parameters (parser, 
11288                                                        declarator))
11289     {
11290       cp_parser_skip_to_end_of_block_or_statement (parser);
11291       return error_mark_node;
11292     }
11293
11294   return (cp_parser_function_definition_from_specifiers_and_declarator
11295           (parser, decl_specifiers, attributes, declarator, access_checks));
11296 }
11297
11298 /* Parse a function-body.
11299
11300    function-body:
11301      compound_statement  */
11302
11303 static void
11304 cp_parser_function_body (cp_parser *parser)
11305 {
11306   cp_parser_compound_statement (parser);
11307 }
11308
11309 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11310    true if a ctor-initializer was present.  */
11311
11312 static bool
11313 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11314 {
11315   tree body;
11316   bool ctor_initializer_p;
11317
11318   /* Begin the function body.  */
11319   body = begin_function_body ();
11320   /* Parse the optional ctor-initializer.  */
11321   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11322   /* Parse the function-body.  */
11323   cp_parser_function_body (parser);
11324   /* Finish the function body.  */
11325   finish_function_body (body);
11326
11327   return ctor_initializer_p;
11328 }
11329
11330 /* Parse an initializer.
11331
11332    initializer:
11333      = initializer-clause
11334      ( expression-list )  
11335
11336    Returns a expression representing the initializer.  If no
11337    initializer is present, NULL_TREE is returned.  
11338
11339    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11340    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11341    set to FALSE if there is no initializer present.  */
11342
11343 static tree
11344 cp_parser_initializer (parser, is_parenthesized_init)
11345      cp_parser *parser;
11346      bool *is_parenthesized_init;
11347 {
11348   cp_token *token;
11349   tree init;
11350
11351   /* Peek at the next token.  */
11352   token = cp_lexer_peek_token (parser->lexer);
11353
11354   /* Let our caller know whether or not this initializer was
11355      parenthesized.  */
11356   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11357
11358   if (token->type == CPP_EQ)
11359     {
11360       /* Consume the `='.  */
11361       cp_lexer_consume_token (parser->lexer);
11362       /* Parse the initializer-clause.  */
11363       init = cp_parser_initializer_clause (parser);
11364     }
11365   else if (token->type == CPP_OPEN_PAREN)
11366     {
11367       /* Consume the `('.  */
11368       cp_lexer_consume_token (parser->lexer);
11369       /* Parse the expression-list.  */
11370       init = cp_parser_expression_list (parser);
11371       /* Consume the `)' token.  */
11372       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11373         cp_parser_skip_to_closing_parenthesis (parser);
11374     }
11375   else
11376     {
11377       /* Anything else is an error.  */
11378       cp_parser_error (parser, "expected initializer");
11379       init = error_mark_node;
11380     }
11381
11382   return init;
11383 }
11384
11385 /* Parse an initializer-clause.  
11386
11387    initializer-clause:
11388      assignment-expression
11389      { initializer-list , [opt] }
11390      { }
11391
11392    Returns an expression representing the initializer.  
11393
11394    If the `assignment-expression' production is used the value
11395    returned is simply a reprsentation for the expression.  
11396
11397    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11398    the elements of the initializer-list (or NULL_TREE, if the last
11399    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11400    NULL_TREE.  There is no way to detect whether or not the optional
11401    trailing `,' was provided.  */
11402
11403 static tree
11404 cp_parser_initializer_clause (parser)
11405      cp_parser *parser;
11406 {
11407   tree initializer;
11408
11409   /* If it is not a `{', then we are looking at an
11410      assignment-expression.  */
11411   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11412     initializer = cp_parser_assignment_expression (parser);
11413   else
11414     {
11415       /* Consume the `{' token.  */
11416       cp_lexer_consume_token (parser->lexer);
11417       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11418       initializer = make_node (CONSTRUCTOR);
11419       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11420          necessary, but check_initializer depends upon it, for 
11421          now.  */
11422       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11423       /* If it's not a `}', then there is a non-trivial initializer.  */
11424       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11425         {
11426           /* Parse the initializer list.  */
11427           CONSTRUCTOR_ELTS (initializer)
11428             = cp_parser_initializer_list (parser);
11429           /* A trailing `,' token is allowed.  */
11430           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11431             cp_lexer_consume_token (parser->lexer);
11432         }
11433
11434       /* Now, there should be a trailing `}'.  */
11435       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11436     }
11437
11438   return initializer;
11439 }
11440
11441 /* Parse an initializer-list.
11442
11443    initializer-list:
11444      initializer-clause
11445      initializer-list , initializer-clause
11446
11447    GNU Extension:
11448    
11449    initializer-list:
11450      identifier : initializer-clause
11451      initializer-list, identifier : initializer-clause
11452
11453    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11454    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11455    IDENTIFIER_NODE naming the field to initialize.   */
11456
11457 static tree
11458 cp_parser_initializer_list (parser)
11459      cp_parser *parser;
11460 {
11461   tree initializers = NULL_TREE;
11462
11463   /* Parse the rest of the list.  */
11464   while (true)
11465     {
11466       cp_token *token;
11467       tree identifier;
11468       tree initializer;
11469       
11470       /* If the next token is an identifier and the following one is a
11471          colon, we are looking at the GNU designated-initializer
11472          syntax.  */
11473       if (cp_parser_allow_gnu_extensions_p (parser)
11474           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11475           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11476         {
11477           /* Consume the identifier.  */
11478           identifier = cp_lexer_consume_token (parser->lexer)->value;
11479           /* Consume the `:'.  */
11480           cp_lexer_consume_token (parser->lexer);
11481         }
11482       else
11483         identifier = NULL_TREE;
11484
11485       /* Parse the initializer.  */
11486       initializer = cp_parser_initializer_clause (parser);
11487
11488       /* Add it to the list.  */
11489       initializers = tree_cons (identifier, initializer, initializers);
11490
11491       /* If the next token is not a comma, we have reached the end of
11492          the list.  */
11493       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11494         break;
11495
11496       /* Peek at the next token.  */
11497       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11498       /* If the next token is a `}', then we're still done.  An
11499          initializer-clause can have a trailing `,' after the
11500          initializer-list and before the closing `}'.  */
11501       if (token->type == CPP_CLOSE_BRACE)
11502         break;
11503
11504       /* Consume the `,' token.  */
11505       cp_lexer_consume_token (parser->lexer);
11506     }
11507
11508   /* The initializers were built up in reverse order, so we need to
11509      reverse them now.  */
11510   return nreverse (initializers);
11511 }
11512
11513 /* Classes [gram.class] */
11514
11515 /* Parse a class-name.
11516
11517    class-name:
11518      identifier
11519      template-id
11520
11521    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11522    to indicate that names looked up in dependent types should be
11523    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11524    keyword has been used to indicate that the name that appears next
11525    is a template.  TYPE_P is true iff the next name should be treated
11526    as class-name, even if it is declared to be some other kind of name
11527    as well.  The accessibility of the class-name is checked iff
11528    CHECK_ACCESS_P is true.  If CHECK_DEPENDENCY_P is FALSE, names are
11529    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
11530    is the class being defined in a class-head.
11531
11532    Returns the TYPE_DECL representing the class.  */
11533
11534 static tree
11535 cp_parser_class_name (cp_parser *parser, 
11536                       bool typename_keyword_p, 
11537                       bool template_keyword_p, 
11538                       bool type_p,
11539                       bool check_access_p,
11540                       bool check_dependency_p,
11541                       bool class_head_p)
11542 {
11543   tree decl;
11544   tree scope;
11545   bool typename_p;
11546   cp_token *token;
11547
11548   /* All class-names start with an identifier.  */
11549   token = cp_lexer_peek_token (parser->lexer);
11550   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11551     {
11552       cp_parser_error (parser, "expected class-name");
11553       return error_mark_node;
11554     }
11555     
11556   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11557      to a template-id, so we save it here.  */
11558   scope = parser->scope;
11559   /* Any name names a type if we're following the `typename' keyword
11560      in a qualified name where the enclosing scope is type-dependent.  */
11561   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11562                 && cp_parser_dependent_type_p (scope));
11563   /* Handle the common case (an identifier, but not a template-id)
11564      efficiently.  */
11565   if (token->type == CPP_NAME 
11566       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11567     {
11568       tree identifier;
11569
11570       /* Look for the identifier.  */
11571       identifier = cp_parser_identifier (parser);
11572       /* If the next token isn't an identifier, we are certainly not
11573          looking at a class-name.  */
11574       if (identifier == error_mark_node)
11575         decl = error_mark_node;
11576       /* If we know this is a type-name, there's no need to look it
11577          up.  */
11578       else if (typename_p)
11579         decl = identifier;
11580       else
11581         {
11582           /* If the next token is a `::', then the name must be a type
11583              name.
11584
11585              [basic.lookup.qual]
11586
11587              During the lookup for a name preceding the :: scope
11588              resolution operator, object, function, and enumerator
11589              names are ignored.  */
11590           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11591             type_p = true;
11592           /* Look up the name.  */
11593           decl = cp_parser_lookup_name (parser, identifier, 
11594                                         check_access_p,
11595                                         type_p,
11596                                         /*is_namespace=*/false,
11597                                         check_dependency_p);
11598         }
11599     }
11600   else
11601     {
11602       /* Try a template-id.  */
11603       decl = cp_parser_template_id (parser, template_keyword_p,
11604                                     check_dependency_p);
11605       if (decl == error_mark_node)
11606         return error_mark_node;
11607     }
11608
11609   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11610
11611   /* If this is a typename, create a TYPENAME_TYPE.  */
11612   if (typename_p && decl != error_mark_node)
11613     decl = TYPE_NAME (make_typename_type (scope, decl,
11614                                           /*complain=*/1));
11615
11616   /* Check to see that it is really the name of a class.  */
11617   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11618       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11619       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11620     /* Situations like this:
11621
11622          template <typename T> struct A {
11623            typename T::template X<int>::I i; 
11624          };
11625
11626        are problematic.  Is `T::template X<int>' a class-name?  The
11627        standard does not seem to be definitive, but there is no other
11628        valid interpretation of the following `::'.  Therefore, those
11629        names are considered class-names.  */
11630     decl = TYPE_NAME (make_typename_type (scope, decl, 
11631                                           tf_error | tf_parsing));
11632   else if (decl == error_mark_node
11633            || TREE_CODE (decl) != TYPE_DECL
11634            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11635     {
11636       cp_parser_error (parser, "expected class-name");
11637       return error_mark_node;
11638     }
11639
11640   return decl;
11641 }
11642
11643 /* Parse a class-specifier.
11644
11645    class-specifier:
11646      class-head { member-specification [opt] }
11647
11648    Returns the TREE_TYPE representing the class.  */
11649
11650 static tree
11651 cp_parser_class_specifier (parser)
11652      cp_parser *parser;
11653 {
11654   cp_token *token;
11655   tree type;
11656   tree attributes = NULL_TREE;
11657   int has_trailing_semicolon;
11658   bool nested_name_specifier_p;
11659   bool deferring_access_checks_p;
11660   tree saved_access_checks;
11661   unsigned saved_num_template_parameter_lists;
11662
11663   /* Parse the class-head.  */
11664   type = cp_parser_class_head (parser,
11665                                &nested_name_specifier_p,
11666                                &deferring_access_checks_p,
11667                                &saved_access_checks);
11668   /* If the class-head was a semantic disaster, skip the entire body
11669      of the class.  */
11670   if (!type)
11671     {
11672       cp_parser_skip_to_end_of_block_or_statement (parser);
11673       return error_mark_node;
11674     }
11675   /* Look for the `{'.  */
11676   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11677     return error_mark_node;
11678   /* Issue an error message if type-definitions are forbidden here.  */
11679   cp_parser_check_type_definition (parser);
11680   /* Remember that we are defining one more class.  */
11681   ++parser->num_classes_being_defined;
11682   /* Inside the class, surrounding template-parameter-lists do not
11683      apply.  */
11684   saved_num_template_parameter_lists 
11685     = parser->num_template_parameter_lists; 
11686   parser->num_template_parameter_lists = 0;
11687   /* Start the class.  */
11688   type = begin_class_definition (type);
11689   if (type == error_mark_node)
11690     /* If the type is erroneous, skip the entire body of the class. */
11691     cp_parser_skip_to_closing_brace (parser);
11692   else
11693     /* Parse the member-specification.  */
11694     cp_parser_member_specification_opt (parser);
11695   /* Look for the trailing `}'.  */
11696   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11697   /* We get better error messages by noticing a common problem: a
11698      missing trailing `;'.  */
11699   token = cp_lexer_peek_token (parser->lexer);
11700   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11701   /* Look for attributes to apply to this class.  */
11702   if (cp_parser_allow_gnu_extensions_p (parser))
11703     attributes = cp_parser_attributes_opt (parser);
11704   /* Finish the class definition.  */
11705   type = finish_class_definition (type, 
11706                                   attributes,
11707                                   has_trailing_semicolon,
11708                                   nested_name_specifier_p);
11709   /* If this class is not itself within the scope of another class,
11710      then we need to parse the bodies of all of the queued function
11711      definitions.  Note that the queued functions defined in a class
11712      are not always processed immediately following the
11713      class-specifier for that class.  Consider:
11714
11715        struct A {
11716          struct B { void f() { sizeof (A); } };
11717        };
11718
11719      If `f' were processed before the processing of `A' were
11720      completed, there would be no way to compute the size of `A'.
11721      Note that the nesting we are interested in here is lexical --
11722      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11723      for:
11724
11725        struct A { struct B; };
11726        struct A::B { void f() { } };
11727
11728      there is no need to delay the parsing of `A::B::f'.  */
11729   if (--parser->num_classes_being_defined == 0) 
11730     {
11731       tree last_scope = NULL_TREE;
11732       tree queue_entry;
11733       tree fn;
11734
11735       /* Reverse the queue, so that we process it in the order the
11736          functions were declared.  */
11737       TREE_VALUE (parser->unparsed_functions_queues)
11738         = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11739       /* In a first pass, parse default arguments to the functions.
11740          Then, in a second pass, parse the bodies of the functions.
11741          This two-phased approach handles cases like:
11742          
11743             struct S { 
11744               void f() { g(); } 
11745               void g(int i = 3);
11746             };
11747
11748          */
11749       for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11750            queue_entry;
11751            queue_entry = TREE_CHAIN (queue_entry))
11752         {
11753           fn = TREE_VALUE (queue_entry);
11754           if (DECL_FUNCTION_TEMPLATE_P (fn))
11755             fn = DECL_TEMPLATE_RESULT (fn);
11756           /* Make sure that any template parameters are in scope.  */
11757           maybe_begin_member_template_processing (fn);
11758           /* If there are default arguments that have not yet been processed,
11759              take care of them now.  */
11760           cp_parser_late_parsing_default_args (parser, fn);
11761           /* Remove any template parameters from the symbol table.  */
11762           maybe_end_member_template_processing ();
11763         }
11764       /* Now parse the body of the functions.  */
11765       while (TREE_VALUE (parser->unparsed_functions_queues))
11766
11767         {
11768           /* Figure out which function we need to process.  */
11769           queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11770           fn = TREE_VALUE (queue_entry);
11771
11772           /* Parse the function.  */
11773           cp_parser_late_parsing_for_member (parser, fn);
11774
11775           TREE_VALUE (parser->unparsed_functions_queues)
11776             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11777         }
11778
11779       /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11780          more time than we have popped, so me must pop here.  */
11781       if (last_scope)
11782         pop_scope (last_scope);
11783     }
11784
11785   /* Put back any saved access checks.  */
11786   if (deferring_access_checks_p)
11787     {
11788       cp_parser_start_deferring_access_checks (parser);
11789       parser->context->deferred_access_checks = saved_access_checks;
11790     }
11791
11792   /* Restore the count of active template-parameter-lists.  */
11793   parser->num_template_parameter_lists
11794     = saved_num_template_parameter_lists;
11795
11796   return type;
11797 }
11798
11799 /* Parse a class-head.
11800
11801    class-head:
11802      class-key identifier [opt] base-clause [opt]
11803      class-key nested-name-specifier identifier base-clause [opt]
11804      class-key nested-name-specifier [opt] template-id 
11805        base-clause [opt]  
11806
11807    GNU Extensions:
11808      class-key attributes identifier [opt] base-clause [opt]
11809      class-key attributes nested-name-specifier identifier base-clause [opt]
11810      class-key attributes nested-name-specifier [opt] template-id 
11811        base-clause [opt]  
11812
11813    Returns the TYPE of the indicated class.  Sets
11814    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11815    involving a nested-name-specifier was used, and FALSE otherwise.
11816    Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11817    access checks before this class-head.  In that case,
11818    *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11819    checks.  
11820
11821    Returns NULL_TREE if the class-head is syntactically valid, but
11822    semantically invalid in a way that means we should skip the entire
11823    body of the class.  */
11824
11825 static tree
11826 cp_parser_class_head (parser, 
11827                       nested_name_specifier_p,
11828                       deferring_access_checks_p,
11829                       saved_access_checks)
11830      cp_parser *parser;
11831      bool *nested_name_specifier_p;
11832      bool *deferring_access_checks_p;
11833      tree *saved_access_checks;
11834 {
11835   cp_token *token;
11836   tree nested_name_specifier;
11837   enum tag_types class_key;
11838   tree id = NULL_TREE;
11839   tree type = NULL_TREE;
11840   tree attributes;
11841   bool template_id_p = false;
11842   bool qualified_p = false;
11843   bool invalid_nested_name_p = false;
11844   unsigned num_templates;
11845
11846   /* Assume no nested-name-specifier will be present.  */
11847   *nested_name_specifier_p = false;
11848   /* Assume no template parameter lists will be used in defining the
11849      type.  */
11850   num_templates = 0;
11851
11852   /* Look for the class-key.  */
11853   class_key = cp_parser_class_key (parser);
11854   if (class_key == none_type)
11855     return error_mark_node;
11856
11857   /* Parse the attributes.  */
11858   attributes = cp_parser_attributes_opt (parser);
11859
11860   /* If the next token is `::', that is invalid -- but sometimes
11861      people do try to write:
11862
11863        struct ::S {};  
11864
11865      Handle this gracefully by accepting the extra qualifier, and then
11866      issuing an error about it later if this really is a
11867      class-head.  If it turns out just to be an elaborated type
11868      specifier, remain silent.  */
11869   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11870     qualified_p = true;
11871
11872   /* Determine the name of the class.  Begin by looking for an
11873      optional nested-name-specifier.  */
11874   nested_name_specifier 
11875     = cp_parser_nested_name_specifier_opt (parser,
11876                                            /*typename_keyword_p=*/false,
11877                                            /*check_dependency_p=*/true,
11878                                            /*type_p=*/false);
11879   /* If there was a nested-name-specifier, then there *must* be an
11880      identifier.  */
11881   if (nested_name_specifier)
11882     {
11883       /* Although the grammar says `identifier', it really means
11884          `class-name' or `template-name'.  You are only allowed to
11885          define a class that has already been declared with this
11886          syntax.  
11887
11888          The proposed resolution for Core Issue 180 says that whever
11889          you see `class T::X' you should treat `X' as a type-name.
11890          
11891          It is OK to define an inaccessible class; for example:
11892          
11893            class A { class B; };
11894            class A::B {};
11895          
11896          So, we ask cp_parser_class_name not to check accessibility.  
11897
11898          We do not know if we will see a class-name, or a
11899          template-name.  We look for a class-name first, in case the
11900          class-name is a template-id; if we looked for the
11901          template-name first we would stop after the template-name.  */
11902       cp_parser_parse_tentatively (parser);
11903       type = cp_parser_class_name (parser,
11904                                    /*typename_keyword_p=*/false,
11905                                    /*template_keyword_p=*/false,
11906                                    /*type_p=*/true,
11907                                    /*check_access_p=*/false,
11908                                    /*check_dependency_p=*/false,
11909                                    /*class_head_p=*/true);
11910       /* If that didn't work, ignore the nested-name-specifier.  */
11911       if (!cp_parser_parse_definitely (parser))
11912         {
11913           invalid_nested_name_p = true;
11914           id = cp_parser_identifier (parser);
11915           if (id == error_mark_node)
11916             id = NULL_TREE;
11917         }
11918       /* If we could not find a corresponding TYPE, treat this
11919          declaration like an unqualified declaration.  */
11920       if (type == error_mark_node)
11921         nested_name_specifier = NULL_TREE;
11922       /* Otherwise, count the number of templates used in TYPE and its
11923          containing scopes.  */
11924       else 
11925         {
11926           tree scope;
11927
11928           for (scope = TREE_TYPE (type); 
11929                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11930                scope = (TYPE_P (scope) 
11931                         ? TYPE_CONTEXT (scope)
11932                         : DECL_CONTEXT (scope))) 
11933             if (TYPE_P (scope) 
11934                 && CLASS_TYPE_P (scope)
11935                 && CLASSTYPE_TEMPLATE_INFO (scope)
11936                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11937                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11938               ++num_templates;
11939         }
11940     }
11941   /* Otherwise, the identifier is optional.  */
11942   else
11943     {
11944       /* We don't know whether what comes next is a template-id,
11945          an identifier, or nothing at all.  */
11946       cp_parser_parse_tentatively (parser);
11947       /* Check for a template-id.  */
11948       id = cp_parser_template_id (parser, 
11949                                   /*template_keyword_p=*/false,
11950                                   /*check_dependency_p=*/true);
11951       /* If that didn't work, it could still be an identifier.  */
11952       if (!cp_parser_parse_definitely (parser))
11953         {
11954           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11955             id = cp_parser_identifier (parser);
11956           else
11957             id = NULL_TREE;
11958         }
11959       else
11960         {
11961           template_id_p = true;
11962           ++num_templates;
11963         }
11964     }
11965
11966   /* If it's not a `:' or a `{' then we can't really be looking at a
11967      class-head, since a class-head only appears as part of a
11968      class-specifier.  We have to detect this situation before calling
11969      xref_tag, since that has irreversible side-effects.  */
11970   if (!cp_parser_next_token_starts_class_definition_p (parser))
11971     {
11972       cp_parser_error (parser, "expected `{' or `:'");
11973       return error_mark_node;
11974     }
11975
11976   /* At this point, we're going ahead with the class-specifier, even
11977      if some other problem occurs.  */
11978   cp_parser_commit_to_tentative_parse (parser);
11979   /* Issue the error about the overly-qualified name now.  */
11980   if (qualified_p)
11981     cp_parser_error (parser,
11982                      "global qualification of class name is invalid");
11983   else if (invalid_nested_name_p)
11984     cp_parser_error (parser,
11985                      "qualified name does not name a class");
11986   /* Make sure that the right number of template parameters were
11987      present.  */
11988   if (!cp_parser_check_template_parameters (parser, num_templates))
11989     /* If something went wrong, there is no point in even trying to
11990        process the class-definition.  */
11991     return NULL_TREE;
11992
11993   /* We do not need to defer access checks for entities declared
11994      within the class.  But, we do need to save any access checks that
11995      are currently deferred and restore them later, in case we are in
11996      the middle of something else.  */
11997   *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11998   if (*deferring_access_checks_p)
11999     *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
12000
12001   /* Look up the type.  */
12002   if (template_id_p)
12003     {
12004       type = TREE_TYPE (id);
12005       maybe_process_partial_specialization (type);
12006     }
12007   else if (!nested_name_specifier)
12008     {
12009       /* If the class was unnamed, create a dummy name.  */
12010       if (!id)
12011         id = make_anon_name ();
12012       type = xref_tag (class_key, id, attributes, /*globalize=*/0);
12013     }
12014   else
12015     {
12016       bool new_type_p;
12017       tree class_type;
12018
12019       /* Given:
12020
12021             template <typename T> struct S { struct T };
12022             template <typename T> struct S::T { };
12023
12024          we will get a TYPENAME_TYPE when processing the definition of
12025          `S::T'.  We need to resolve it to the actual type before we
12026          try to define it.  */
12027       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12028         {
12029           type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
12030           if (type != error_mark_node)
12031             type = TYPE_NAME (type);
12032         }
12033
12034       maybe_process_partial_specialization (TREE_TYPE (type));
12035       class_type = current_class_type;
12036       type = TREE_TYPE (handle_class_head (class_key, 
12037                                            nested_name_specifier,
12038                                            type,
12039                                            attributes,
12040                                            /*defn_p=*/true,
12041                                            &new_type_p));
12042       if (type != error_mark_node)
12043         {
12044           if (!class_type && TYPE_CONTEXT (type))
12045             *nested_name_specifier_p = true;
12046           else if (class_type && !same_type_p (TYPE_CONTEXT (type),
12047                                                class_type))
12048             *nested_name_specifier_p = true;
12049         }
12050     }
12051   /* Indicate whether this class was declared as a `class' or as a
12052      `struct'.  */
12053   if (TREE_CODE (type) == RECORD_TYPE)
12054     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12055   cp_parser_check_class_key (class_key, type);
12056
12057   /* Enter the scope containing the class; the names of base classes
12058      should be looked up in that context.  For example, given:
12059
12060        struct A { struct B {}; struct C; };
12061        struct A::C : B {};
12062
12063      is valid.  */
12064   if (nested_name_specifier)
12065     push_scope (nested_name_specifier);
12066   /* Now, look for the base-clause.  */
12067   token = cp_lexer_peek_token (parser->lexer);
12068   if (token->type == CPP_COLON)
12069     {
12070       tree bases;
12071
12072       /* Get the list of base-classes.  */
12073       bases = cp_parser_base_clause (parser);
12074       /* Process them.  */
12075       xref_basetypes (type, bases);
12076     }
12077   /* Leave the scope given by the nested-name-specifier.  We will
12078      enter the class scope itself while processing the members.  */
12079   if (nested_name_specifier)
12080     pop_scope (nested_name_specifier);
12081
12082   return type;
12083 }
12084
12085 /* Parse a class-key.
12086
12087    class-key:
12088      class
12089      struct
12090      union
12091
12092    Returns the kind of class-key specified, or none_type to indicate
12093    error.  */
12094
12095 static enum tag_types
12096 cp_parser_class_key (parser)
12097      cp_parser *parser;
12098 {
12099   cp_token *token;
12100   enum tag_types tag_type;
12101
12102   /* Look for the class-key.  */
12103   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12104   if (!token)
12105     return none_type;
12106
12107   /* Check to see if the TOKEN is a class-key.  */
12108   tag_type = cp_parser_token_is_class_key (token);
12109   if (!tag_type)
12110     cp_parser_error (parser, "expected class-key");
12111   return tag_type;
12112 }
12113
12114 /* Parse an (optional) member-specification.
12115
12116    member-specification:
12117      member-declaration member-specification [opt]
12118      access-specifier : member-specification [opt]  */
12119
12120 static void
12121 cp_parser_member_specification_opt (parser)
12122      cp_parser *parser;
12123 {
12124   while (true)
12125     {
12126       cp_token *token;
12127       enum rid keyword;
12128
12129       /* Peek at the next token.  */
12130       token = cp_lexer_peek_token (parser->lexer);
12131       /* If it's a `}', or EOF then we've seen all the members.  */
12132       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12133         break;
12134
12135       /* See if this token is a keyword.  */
12136       keyword = token->keyword;
12137       switch (keyword)
12138         {
12139         case RID_PUBLIC:
12140         case RID_PROTECTED:
12141         case RID_PRIVATE:
12142           /* Consume the access-specifier.  */
12143           cp_lexer_consume_token (parser->lexer);
12144           /* Remember which access-specifier is active.  */
12145           current_access_specifier = token->value;
12146           /* Look for the `:'.  */
12147           cp_parser_require (parser, CPP_COLON, "`:'");
12148           break;
12149
12150         default:
12151           /* Otherwise, the next construction must be a
12152              member-declaration.  */
12153           cp_parser_member_declaration (parser);
12154           reset_type_access_control ();
12155         }
12156     }
12157 }
12158
12159 /* Parse a member-declaration.  
12160
12161    member-declaration:
12162      decl-specifier-seq [opt] member-declarator-list [opt] ;
12163      function-definition ; [opt]
12164      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12165      using-declaration
12166      template-declaration 
12167
12168    member-declarator-list:
12169      member-declarator
12170      member-declarator-list , member-declarator
12171
12172    member-declarator:
12173      declarator pure-specifier [opt] 
12174      declarator constant-initializer [opt]
12175      identifier [opt] : constant-expression 
12176
12177    GNU Extensions:
12178
12179    member-declaration:
12180      __extension__ member-declaration
12181
12182    member-declarator:
12183      declarator attributes [opt] pure-specifier [opt]
12184      declarator attributes [opt] constant-initializer [opt]
12185      identifier [opt] attributes [opt] : constant-expression  */
12186
12187 static void
12188 cp_parser_member_declaration (parser)
12189      cp_parser *parser;
12190 {
12191   tree decl_specifiers;
12192   tree prefix_attributes;
12193   tree decl;
12194   bool declares_class_or_enum;
12195   bool friend_p;
12196   cp_token *token;
12197   int saved_pedantic;
12198
12199   /* Check for the `__extension__' keyword.  */
12200   if (cp_parser_extension_opt (parser, &saved_pedantic))
12201     {
12202       /* Recurse.  */
12203       cp_parser_member_declaration (parser);
12204       /* Restore the old value of the PEDANTIC flag.  */
12205       pedantic = saved_pedantic;
12206
12207       return;
12208     }
12209
12210   /* Check for a template-declaration.  */
12211   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12212     {
12213       /* Parse the template-declaration.  */
12214       cp_parser_template_declaration (parser, /*member_p=*/true);
12215
12216       return;
12217     }
12218
12219   /* Check for a using-declaration.  */
12220   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12221     {
12222       /* Parse the using-declaration.  */
12223       cp_parser_using_declaration (parser);
12224
12225       return;
12226     }
12227   
12228   /* We can't tell whether we're looking at a declaration or a
12229      function-definition.  */
12230   cp_parser_parse_tentatively (parser);
12231
12232   /* Parse the decl-specifier-seq.  */
12233   decl_specifiers 
12234     = cp_parser_decl_specifier_seq (parser,
12235                                     CP_PARSER_FLAGS_OPTIONAL,
12236                                     &prefix_attributes,
12237                                     &declares_class_or_enum);
12238   /* If there is no declarator, then the decl-specifier-seq should
12239      specify a type.  */
12240   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12241     {
12242       /* If there was no decl-specifier-seq, and the next token is a
12243          `;', then we have something like:
12244
12245            struct S { ; };
12246
12247          [class.mem]
12248
12249          Each member-declaration shall declare at least one member
12250          name of the class.  */
12251       if (!decl_specifiers)
12252         {
12253           if (pedantic)
12254             pedwarn ("extra semicolon");
12255         }
12256       else 
12257         {
12258           tree type;
12259           
12260           /* See if this declaration is a friend.  */
12261           friend_p = cp_parser_friend_p (decl_specifiers);
12262           /* If there were decl-specifiers, check to see if there was
12263              a class-declaration.  */
12264           type = check_tag_decl (decl_specifiers);
12265           /* Nested classes have already been added to the class, but
12266              a `friend' needs to be explicitly registered.  */
12267           if (friend_p)
12268             {
12269               /* If the `friend' keyword was present, the friend must
12270                  be introduced with a class-key.  */
12271                if (!declares_class_or_enum)
12272                  error ("a class-key must be used when declaring a friend");
12273                /* In this case:
12274
12275                     template <typename T> struct A { 
12276                       friend struct A<T>::B; 
12277                     };
12278  
12279                   A<T>::B will be represented by a TYPENAME_TYPE, and
12280                   therefore not recognized by check_tag_decl.  */
12281                if (!type)
12282                  {
12283                    tree specifier;
12284
12285                    for (specifier = decl_specifiers; 
12286                         specifier;
12287                         specifier = TREE_CHAIN (specifier))
12288                      {
12289                        tree s = TREE_VALUE (specifier);
12290
12291                        if (TREE_CODE (s) == IDENTIFIER_NODE
12292                            && IDENTIFIER_GLOBAL_VALUE (s))
12293                          type = IDENTIFIER_GLOBAL_VALUE (s);
12294                        if (TREE_CODE (s) == TYPE_DECL)
12295                          s = TREE_TYPE (s);
12296                        if (TYPE_P (s))
12297                          {
12298                            type = s;
12299                            break;
12300                          }
12301                      }
12302                  }
12303                if (!type)
12304                  error ("friend declaration does not name a class or "
12305                         "function");
12306                else
12307                  make_friend_class (current_class_type, type);
12308             }
12309           /* If there is no TYPE, an error message will already have
12310              been issued.  */
12311           else if (!type)
12312             ;
12313           /* An anonymous aggregate has to be handled specially; such
12314              a declaration really declares a data member (with a
12315              particular type), as opposed to a nested class.  */
12316           else if (ANON_AGGR_TYPE_P (type))
12317             {
12318               /* Remove constructors and such from TYPE, now that we
12319                  know it is an anoymous aggregate.  */
12320               fixup_anonymous_aggr (type);
12321               /* And make the corresponding data member.  */
12322               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12323               /* Add it to the class.  */
12324               finish_member_declaration (decl);
12325             }
12326         }
12327     }
12328   else
12329     {
12330       /* See if these declarations will be friends.  */
12331       friend_p = cp_parser_friend_p (decl_specifiers);
12332
12333       /* Keep going until we hit the `;' at the end of the 
12334          declaration.  */
12335       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12336         {
12337           tree attributes = NULL_TREE;
12338           tree first_attribute;
12339
12340           /* Peek at the next token.  */
12341           token = cp_lexer_peek_token (parser->lexer);
12342
12343           /* Check for a bitfield declaration.  */
12344           if (token->type == CPP_COLON
12345               || (token->type == CPP_NAME
12346                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12347                   == CPP_COLON))
12348             {
12349               tree identifier;
12350               tree width;
12351
12352               /* Get the name of the bitfield.  Note that we cannot just
12353                  check TOKEN here because it may have been invalidated by
12354                  the call to cp_lexer_peek_nth_token above.  */
12355               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12356                 identifier = cp_parser_identifier (parser);
12357               else
12358                 identifier = NULL_TREE;
12359
12360               /* Consume the `:' token.  */
12361               cp_lexer_consume_token (parser->lexer);
12362               /* Get the width of the bitfield.  */
12363               width = cp_parser_constant_expression (parser);
12364
12365               /* Look for attributes that apply to the bitfield.  */
12366               attributes = cp_parser_attributes_opt (parser);
12367               /* Remember which attributes are prefix attributes and
12368                  which are not.  */
12369               first_attribute = attributes;
12370               /* Combine the attributes.  */
12371               attributes = chainon (prefix_attributes, attributes);
12372
12373               /* Create the bitfield declaration.  */
12374               decl = grokbitfield (identifier, 
12375                                    decl_specifiers,
12376                                    width);
12377               /* Apply the attributes.  */
12378               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12379             }
12380           else
12381             {
12382               tree declarator;
12383               tree initializer;
12384               tree asm_specification;
12385               bool ctor_dtor_or_conv_p;
12386
12387               /* Parse the declarator.  */
12388               declarator 
12389                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12390                                         &ctor_dtor_or_conv_p);
12391
12392               /* If something went wrong parsing the declarator, make sure
12393                  that we at least consume some tokens.  */
12394               if (declarator == error_mark_node)
12395                 {
12396                   /* Skip to the end of the statement.  */
12397                   cp_parser_skip_to_end_of_statement (parser);
12398                   break;
12399                 }
12400
12401               /* Look for an asm-specification.  */
12402               asm_specification = cp_parser_asm_specification_opt (parser);
12403               /* Look for attributes that apply to the declaration.  */
12404               attributes = cp_parser_attributes_opt (parser);
12405               /* Remember which attributes are prefix attributes and
12406                  which are not.  */
12407               first_attribute = attributes;
12408               /* Combine the attributes.  */
12409               attributes = chainon (prefix_attributes, attributes);
12410
12411               /* If it's an `=', then we have a constant-initializer or a
12412                  pure-specifier.  It is not correct to parse the
12413                  initializer before registering the member declaration
12414                  since the member declaration should be in scope while
12415                  its initializer is processed.  However, the rest of the
12416                  front end does not yet provide an interface that allows
12417                  us to handle this correctly.  */
12418               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12419                 {
12420                   /* In [class.mem]:
12421
12422                      A pure-specifier shall be used only in the declaration of
12423                      a virtual function.  
12424
12425                      A member-declarator can contain a constant-initializer
12426                      only if it declares a static member of integral or
12427                      enumeration type.  
12428
12429                      Therefore, if the DECLARATOR is for a function, we look
12430                      for a pure-specifier; otherwise, we look for a
12431                      constant-initializer.  When we call `grokfield', it will
12432                      perform more stringent semantics checks.  */
12433                   if (TREE_CODE (declarator) == CALL_EXPR)
12434                     initializer = cp_parser_pure_specifier (parser);
12435                   else
12436                     {
12437                       /* This declaration cannot be a function
12438                          definition.  */
12439                       cp_parser_commit_to_tentative_parse (parser);
12440                       /* Parse the initializer.  */
12441                       initializer = cp_parser_constant_initializer (parser);
12442                     }
12443                 }
12444               /* Otherwise, there is no initializer.  */
12445               else
12446                 initializer = NULL_TREE;
12447
12448               /* See if we are probably looking at a function
12449                  definition.  We are certainly not looking at at a
12450                  member-declarator.  Calling `grokfield' has
12451                  side-effects, so we must not do it unless we are sure
12452                  that we are looking at a member-declarator.  */
12453               if (cp_parser_token_starts_function_definition_p 
12454                   (cp_lexer_peek_token (parser->lexer)))
12455                 decl = error_mark_node;
12456               else
12457                 /* Create the declaration.  */
12458                 decl = grokfield (declarator, 
12459                                   decl_specifiers, 
12460                                   initializer,
12461                                   asm_specification,
12462                                   attributes);
12463             }
12464
12465           /* Reset PREFIX_ATTRIBUTES.  */
12466           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12467             attributes = TREE_CHAIN (attributes);
12468           if (attributes)
12469             TREE_CHAIN (attributes) = NULL_TREE;
12470
12471           /* If there is any qualification still in effect, clear it
12472              now; we will be starting fresh with the next declarator.  */
12473           parser->scope = NULL_TREE;
12474           parser->qualifying_scope = NULL_TREE;
12475           parser->object_scope = NULL_TREE;
12476           /* If it's a `,', then there are more declarators.  */
12477           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12478             cp_lexer_consume_token (parser->lexer);
12479           /* If the next token isn't a `;', then we have a parse error.  */
12480           else if (cp_lexer_next_token_is_not (parser->lexer,
12481                                                CPP_SEMICOLON))
12482             {
12483               cp_parser_error (parser, "expected `;'");
12484               /* Skip tokens until we find a `;'  */
12485               cp_parser_skip_to_end_of_statement (parser);
12486
12487               break;
12488             }
12489
12490           if (decl)
12491             {
12492               /* Add DECL to the list of members.  */
12493               if (!friend_p)
12494                 finish_member_declaration (decl);
12495
12496               /* If DECL is a function, we must return
12497                  to parse it later.  (Even though there is no definition,
12498                  there might be default arguments that need handling.)  */
12499               if (TREE_CODE (decl) == FUNCTION_DECL)
12500                 TREE_VALUE (parser->unparsed_functions_queues)
12501                   = tree_cons (NULL_TREE, decl, 
12502                                TREE_VALUE (parser->unparsed_functions_queues));
12503             }
12504         }
12505     }
12506
12507   /* If everything went well, look for the `;'.  */
12508   if (cp_parser_parse_definitely (parser))
12509     {
12510       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12511       return;
12512     }
12513
12514   /* Parse the function-definition.  */
12515   decl = cp_parser_function_definition (parser, &friend_p);
12516   /* If the member was not a friend, declare it here.  */
12517   if (!friend_p)
12518     finish_member_declaration (decl);
12519   /* Peek at the next token.  */
12520   token = cp_lexer_peek_token (parser->lexer);
12521   /* If the next token is a semicolon, consume it.  */
12522   if (token->type == CPP_SEMICOLON)
12523     cp_lexer_consume_token (parser->lexer);
12524 }
12525
12526 /* Parse a pure-specifier.
12527
12528    pure-specifier:
12529      = 0
12530
12531    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12532    Otherwiser, ERROR_MARK_NODE is returned.  */
12533
12534 static tree
12535 cp_parser_pure_specifier (parser)
12536      cp_parser *parser;
12537 {
12538   cp_token *token;
12539
12540   /* Look for the `=' token.  */
12541   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12542     return error_mark_node;
12543   /* Look for the `0' token.  */
12544   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12545   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12546      to get information from the lexer about how the number was
12547      spelled in order to fix this problem.  */
12548   if (!token || !integer_zerop (token->value))
12549     return error_mark_node;
12550
12551   return integer_zero_node;
12552 }
12553
12554 /* Parse a constant-initializer.
12555
12556    constant-initializer:
12557      = constant-expression
12558
12559    Returns a representation of the constant-expression.  */
12560
12561 static tree
12562 cp_parser_constant_initializer (parser)
12563      cp_parser *parser;
12564 {
12565   /* Look for the `=' token.  */
12566   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12567     return error_mark_node;
12568
12569   /* It is invalid to write:
12570
12571        struct S { static const int i = { 7 }; };
12572
12573      */
12574   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12575     {
12576       cp_parser_error (parser,
12577                        "a brace-enclosed initializer is not allowed here");
12578       /* Consume the opening brace.  */
12579       cp_lexer_consume_token (parser->lexer);
12580       /* Skip the initializer.  */
12581       cp_parser_skip_to_closing_brace (parser);
12582       /* Look for the trailing `}'.  */
12583       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12584       
12585       return error_mark_node;
12586     }
12587
12588   return cp_parser_constant_expression (parser);
12589 }
12590
12591 /* Derived classes [gram.class.derived] */
12592
12593 /* Parse a base-clause.
12594
12595    base-clause:
12596      : base-specifier-list  
12597
12598    base-specifier-list:
12599      base-specifier
12600      base-specifier-list , base-specifier
12601
12602    Returns a TREE_LIST representing the base-classes, in the order in
12603    which they were declared.  The representation of each node is as
12604    described by cp_parser_base_specifier.  
12605
12606    In the case that no bases are specified, this function will return
12607    NULL_TREE, not ERROR_MARK_NODE.  */
12608
12609 static tree
12610 cp_parser_base_clause (parser)
12611      cp_parser *parser;
12612 {
12613   tree bases = NULL_TREE;
12614
12615   /* Look for the `:' that begins the list.  */
12616   cp_parser_require (parser, CPP_COLON, "`:'");
12617
12618   /* Scan the base-specifier-list.  */
12619   while (true)
12620     {
12621       cp_token *token;
12622       tree base;
12623
12624       /* Look for the base-specifier.  */
12625       base = cp_parser_base_specifier (parser);
12626       /* Add BASE to the front of the list.  */
12627       if (base != error_mark_node)
12628         {
12629           TREE_CHAIN (base) = bases;
12630           bases = base;
12631         }
12632       /* Peek at the next token.  */
12633       token = cp_lexer_peek_token (parser->lexer);
12634       /* If it's not a comma, then the list is complete.  */
12635       if (token->type != CPP_COMMA)
12636         break;
12637       /* Consume the `,'.  */
12638       cp_lexer_consume_token (parser->lexer);
12639     }
12640
12641   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12642      base class had a qualified name.  However, the next name that
12643      appears is certainly not qualified.  */
12644   parser->scope = NULL_TREE;
12645   parser->qualifying_scope = NULL_TREE;
12646   parser->object_scope = NULL_TREE;
12647
12648   return nreverse (bases);
12649 }
12650
12651 /* Parse a base-specifier.
12652
12653    base-specifier:
12654      :: [opt] nested-name-specifier [opt] class-name
12655      virtual access-specifier [opt] :: [opt] nested-name-specifier
12656        [opt] class-name
12657      access-specifier virtual [opt] :: [opt] nested-name-specifier
12658        [opt] class-name
12659
12660    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12661    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12662    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12663    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12664        
12665 static tree
12666 cp_parser_base_specifier (parser)
12667      cp_parser *parser;
12668 {
12669   cp_token *token;
12670   bool done = false;
12671   bool virtual_p = false;
12672   bool duplicate_virtual_error_issued_p = false;
12673   bool duplicate_access_error_issued_p = false;
12674   bool class_scope_p;
12675   access_kind access = ak_none;
12676   tree access_node;
12677   tree type;
12678
12679   /* Process the optional `virtual' and `access-specifier'.  */
12680   while (!done)
12681     {
12682       /* Peek at the next token.  */
12683       token = cp_lexer_peek_token (parser->lexer);
12684       /* Process `virtual'.  */
12685       switch (token->keyword)
12686         {
12687         case RID_VIRTUAL:
12688           /* If `virtual' appears more than once, issue an error.  */
12689           if (virtual_p && !duplicate_virtual_error_issued_p)
12690             {
12691               cp_parser_error (parser,
12692                                "`virtual' specified more than once in base-specified");
12693               duplicate_virtual_error_issued_p = true;
12694             }
12695
12696           virtual_p = true;
12697
12698           /* Consume the `virtual' token.  */
12699           cp_lexer_consume_token (parser->lexer);
12700
12701           break;
12702
12703         case RID_PUBLIC:
12704         case RID_PROTECTED:
12705         case RID_PRIVATE:
12706           /* If more than one access specifier appears, issue an
12707              error.  */
12708           if (access != ak_none && !duplicate_access_error_issued_p)
12709             {
12710               cp_parser_error (parser,
12711                                "more than one access specifier in base-specified");
12712               duplicate_access_error_issued_p = true;
12713             }
12714
12715           access = ((access_kind) 
12716                     tree_low_cst (ridpointers[(int) token->keyword],
12717                                   /*pos=*/1));
12718
12719           /* Consume the access-specifier.  */
12720           cp_lexer_consume_token (parser->lexer);
12721
12722           break;
12723
12724         default:
12725           done = true;
12726           break;
12727         }
12728     }
12729
12730   /* Map `virtual_p' and `access' onto one of the access 
12731      tree-nodes.  */
12732   if (!virtual_p)
12733     switch (access)
12734       {
12735       case ak_none:
12736         access_node = access_default_node;
12737         break;
12738       case ak_public:
12739         access_node = access_public_node;
12740         break;
12741       case ak_protected:
12742         access_node = access_protected_node;
12743         break;
12744       case ak_private:
12745         access_node = access_private_node;
12746         break;
12747       default:
12748         abort ();
12749       }
12750   else
12751     switch (access)
12752       {
12753       case ak_none:
12754         access_node = access_default_virtual_node;
12755         break;
12756       case ak_public:
12757         access_node = access_public_virtual_node;
12758         break;
12759       case ak_protected:
12760         access_node = access_protected_virtual_node;
12761         break;
12762       case ak_private:
12763         access_node = access_private_virtual_node;
12764         break;
12765       default:
12766         abort ();
12767       }
12768
12769   /* Look for the optional `::' operator.  */
12770   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12771   /* Look for the nested-name-specifier.  The simplest way to
12772      implement:
12773
12774        [temp.res]
12775
12776        The keyword `typename' is not permitted in a base-specifier or
12777        mem-initializer; in these contexts a qualified name that
12778        depends on a template-parameter is implicitly assumed to be a
12779        type name.
12780
12781      is to pretend that we have seen the `typename' keyword at this
12782      point.  */ 
12783   cp_parser_nested_name_specifier_opt (parser,
12784                                        /*typename_keyword_p=*/true,
12785                                        /*check_dependency_p=*/true,
12786                                        /*type_p=*/true);
12787   /* If the base class is given by a qualified name, assume that names
12788      we see are type names or templates, as appropriate.  */
12789   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12790   /* Finally, look for the class-name.  */
12791   type = cp_parser_class_name (parser, 
12792                                class_scope_p,
12793                                class_scope_p,
12794                                /*type_p=*/true,
12795                                /*check_access=*/true,
12796                                /*check_dependency_p=*/true,
12797                                /*class_head_p=*/false);
12798
12799   if (type == error_mark_node)
12800     return error_mark_node;
12801
12802   return finish_base_specifier (access_node, TREE_TYPE (type));
12803 }
12804
12805 /* Exception handling [gram.exception] */
12806
12807 /* Parse an (optional) exception-specification.
12808
12809    exception-specification:
12810      throw ( type-id-list [opt] )
12811
12812    Returns a TREE_LIST representing the exception-specification.  The
12813    TREE_VALUE of each node is a type.  */
12814
12815 static tree
12816 cp_parser_exception_specification_opt (parser)
12817      cp_parser *parser;
12818 {
12819   cp_token *token;
12820   tree type_id_list;
12821
12822   /* Peek at the next token.  */
12823   token = cp_lexer_peek_token (parser->lexer);
12824   /* If it's not `throw', then there's no exception-specification.  */
12825   if (!cp_parser_is_keyword (token, RID_THROW))
12826     return NULL_TREE;
12827
12828   /* Consume the `throw'.  */
12829   cp_lexer_consume_token (parser->lexer);
12830
12831   /* Look for the `('.  */
12832   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12833
12834   /* Peek at the next token.  */
12835   token = cp_lexer_peek_token (parser->lexer);
12836   /* If it's not a `)', then there is a type-id-list.  */
12837   if (token->type != CPP_CLOSE_PAREN)
12838     {
12839       const char *saved_message;
12840
12841       /* Types may not be defined in an exception-specification.  */
12842       saved_message = parser->type_definition_forbidden_message;
12843       parser->type_definition_forbidden_message
12844         = "types may not be defined in an exception-specification";
12845       /* Parse the type-id-list.  */
12846       type_id_list = cp_parser_type_id_list (parser);
12847       /* Restore the saved message.  */
12848       parser->type_definition_forbidden_message = saved_message;
12849     }
12850   else
12851     type_id_list = empty_except_spec;
12852
12853   /* Look for the `)'.  */
12854   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12855
12856   return type_id_list;
12857 }
12858
12859 /* Parse an (optional) type-id-list.
12860
12861    type-id-list:
12862      type-id
12863      type-id-list , type-id
12864
12865    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12866    in the order that the types were presented.  */
12867
12868 static tree
12869 cp_parser_type_id_list (parser)
12870      cp_parser *parser;
12871 {
12872   tree types = NULL_TREE;
12873
12874   while (true)
12875     {
12876       cp_token *token;
12877       tree type;
12878
12879       /* Get the next type-id.  */
12880       type = cp_parser_type_id (parser);
12881       /* Add it to the list.  */
12882       types = add_exception_specifier (types, type, /*complain=*/1);
12883       /* Peek at the next token.  */
12884       token = cp_lexer_peek_token (parser->lexer);
12885       /* If it is not a `,', we are done.  */
12886       if (token->type != CPP_COMMA)
12887         break;
12888       /* Consume the `,'.  */
12889       cp_lexer_consume_token (parser->lexer);
12890     }
12891
12892   return nreverse (types);
12893 }
12894
12895 /* Parse a try-block.
12896
12897    try-block:
12898      try compound-statement handler-seq  */
12899
12900 static tree
12901 cp_parser_try_block (parser)
12902      cp_parser *parser;
12903 {
12904   tree try_block;
12905
12906   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12907   try_block = begin_try_block ();
12908   cp_parser_compound_statement (parser);
12909   finish_try_block (try_block);
12910   cp_parser_handler_seq (parser);
12911   finish_handler_sequence (try_block);
12912
12913   return try_block;
12914 }
12915
12916 /* Parse a function-try-block.
12917
12918    function-try-block:
12919      try ctor-initializer [opt] function-body handler-seq  */
12920
12921 static bool
12922 cp_parser_function_try_block (parser)
12923      cp_parser *parser;
12924 {
12925   tree try_block;
12926   bool ctor_initializer_p;
12927
12928   /* Look for the `try' keyword.  */
12929   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12930     return false;
12931   /* Let the rest of the front-end know where we are.  */
12932   try_block = begin_function_try_block ();
12933   /* Parse the function-body.  */
12934   ctor_initializer_p 
12935     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12936   /* We're done with the `try' part.  */
12937   finish_function_try_block (try_block);
12938   /* Parse the handlers.  */
12939   cp_parser_handler_seq (parser);
12940   /* We're done with the handlers.  */
12941   finish_function_handler_sequence (try_block);
12942
12943   return ctor_initializer_p;
12944 }
12945
12946 /* Parse a handler-seq.
12947
12948    handler-seq:
12949      handler handler-seq [opt]  */
12950
12951 static void
12952 cp_parser_handler_seq (parser)
12953      cp_parser *parser;
12954 {
12955   while (true)
12956     {
12957       cp_token *token;
12958
12959       /* Parse the handler.  */
12960       cp_parser_handler (parser);
12961       /* Peek at the next token.  */
12962       token = cp_lexer_peek_token (parser->lexer);
12963       /* If it's not `catch' then there are no more handlers.  */
12964       if (!cp_parser_is_keyword (token, RID_CATCH))
12965         break;
12966     }
12967 }
12968
12969 /* Parse a handler.
12970
12971    handler:
12972      catch ( exception-declaration ) compound-statement  */
12973
12974 static void
12975 cp_parser_handler (parser)
12976      cp_parser *parser;
12977 {
12978   tree handler;
12979   tree declaration;
12980
12981   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12982   handler = begin_handler ();
12983   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12984   declaration = cp_parser_exception_declaration (parser);
12985   finish_handler_parms (declaration, handler);
12986   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12987   cp_parser_compound_statement (parser);
12988   finish_handler (handler);
12989 }
12990
12991 /* Parse an exception-declaration.
12992
12993    exception-declaration:
12994      type-specifier-seq declarator
12995      type-specifier-seq abstract-declarator
12996      type-specifier-seq
12997      ...  
12998
12999    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13000    ellipsis variant is used.  */
13001
13002 static tree
13003 cp_parser_exception_declaration (parser)
13004      cp_parser *parser;
13005 {
13006   tree type_specifiers;
13007   tree declarator;
13008   const char *saved_message;
13009
13010   /* If it's an ellipsis, it's easy to handle.  */
13011   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13012     {
13013       /* Consume the `...' token.  */
13014       cp_lexer_consume_token (parser->lexer);
13015       return NULL_TREE;
13016     }
13017
13018   /* Types may not be defined in exception-declarations.  */
13019   saved_message = parser->type_definition_forbidden_message;
13020   parser->type_definition_forbidden_message
13021     = "types may not be defined in exception-declarations";
13022
13023   /* Parse the type-specifier-seq.  */
13024   type_specifiers = cp_parser_type_specifier_seq (parser);
13025   /* If it's a `)', then there is no declarator.  */
13026   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13027     declarator = NULL_TREE;
13028   else
13029     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13030                                        /*ctor_dtor_or_conv_p=*/NULL);
13031
13032   /* Restore the saved message.  */
13033   parser->type_definition_forbidden_message = saved_message;
13034
13035   return start_handler_parms (type_specifiers, declarator);
13036 }
13037
13038 /* Parse a throw-expression. 
13039
13040    throw-expression:
13041      throw assignment-expresion [opt]
13042
13043    Returns a THROW_EXPR representing the throw-expression.  */
13044
13045 static tree
13046 cp_parser_throw_expression (parser)
13047      cp_parser *parser;
13048 {
13049   tree expression;
13050
13051   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13052   /* We can't be sure if there is an assignment-expression or not.  */
13053   cp_parser_parse_tentatively (parser);
13054   /* Try it.  */
13055   expression = cp_parser_assignment_expression (parser);
13056   /* If it didn't work, this is just a rethrow.  */
13057   if (!cp_parser_parse_definitely (parser))
13058     expression = NULL_TREE;
13059
13060   return build_throw (expression);
13061 }
13062
13063 /* GNU Extensions */
13064
13065 /* Parse an (optional) asm-specification.
13066
13067    asm-specification:
13068      asm ( string-literal )
13069
13070    If the asm-specification is present, returns a STRING_CST
13071    corresponding to the string-literal.  Otherwise, returns
13072    NULL_TREE.  */
13073
13074 static tree
13075 cp_parser_asm_specification_opt (parser)
13076      cp_parser *parser;
13077 {
13078   cp_token *token;
13079   tree asm_specification;
13080
13081   /* Peek at the next token.  */
13082   token = cp_lexer_peek_token (parser->lexer);
13083   /* If the next token isn't the `asm' keyword, then there's no 
13084      asm-specification.  */
13085   if (!cp_parser_is_keyword (token, RID_ASM))
13086     return NULL_TREE;
13087
13088   /* Consume the `asm' token.  */
13089   cp_lexer_consume_token (parser->lexer);
13090   /* Look for the `('.  */
13091   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13092
13093   /* Look for the string-literal.  */
13094   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13095   if (token)
13096     asm_specification = token->value;
13097   else
13098     asm_specification = NULL_TREE;
13099
13100   /* Look for the `)'.  */
13101   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13102
13103   return asm_specification;
13104 }
13105
13106 /* Parse an asm-operand-list.  
13107
13108    asm-operand-list:
13109      asm-operand
13110      asm-operand-list , asm-operand
13111      
13112    asm-operand:
13113      string-literal ( expression )  
13114      [ string-literal ] string-literal ( expression )
13115
13116    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13117    each node is the expression.  The TREE_PURPOSE is itself a
13118    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13119    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13120    is a STRING_CST for the string literal before the parenthesis.  */
13121
13122 static tree
13123 cp_parser_asm_operand_list (parser)
13124      cp_parser *parser;
13125 {
13126   tree asm_operands = NULL_TREE;
13127
13128   while (true)
13129     {
13130       tree string_literal;
13131       tree expression;
13132       tree name;
13133       cp_token *token;
13134       
13135       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13136         {
13137           /* Consume the `[' token.  */
13138           cp_lexer_consume_token (parser->lexer);
13139           /* Read the operand name.  */
13140           name = cp_parser_identifier (parser);
13141           if (name != error_mark_node) 
13142             name = build_string (IDENTIFIER_LENGTH (name),
13143                                  IDENTIFIER_POINTER (name));
13144           /* Look for the closing `]'.  */
13145           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13146         }
13147       else
13148         name = NULL_TREE;
13149       /* Look for the string-literal.  */
13150       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13151       string_literal = token ? token->value : error_mark_node;
13152       /* Look for the `('.  */
13153       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13154       /* Parse the expression.  */
13155       expression = cp_parser_expression (parser);
13156       /* Look for the `)'.  */
13157       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13158       /* Add this operand to the list.  */
13159       asm_operands = tree_cons (build_tree_list (name, string_literal),
13160                                 expression, 
13161                                 asm_operands);
13162       /* If the next token is not a `,', there are no more 
13163          operands.  */
13164       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13165         break;
13166       /* Consume the `,'.  */
13167       cp_lexer_consume_token (parser->lexer);
13168     }
13169
13170   return nreverse (asm_operands);
13171 }
13172
13173 /* Parse an asm-clobber-list.  
13174
13175    asm-clobber-list:
13176      string-literal
13177      asm-clobber-list , string-literal  
13178
13179    Returns a TREE_LIST, indicating the clobbers in the order that they
13180    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13181
13182 static tree
13183 cp_parser_asm_clobber_list (parser)
13184      cp_parser *parser;
13185 {
13186   tree clobbers = NULL_TREE;
13187
13188   while (true)
13189     {
13190       cp_token *token;
13191       tree string_literal;
13192
13193       /* Look for the string literal.  */
13194       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13195       string_literal = token ? token->value : error_mark_node;
13196       /* Add it to the list.  */
13197       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13198       /* If the next token is not a `,', then the list is 
13199          complete.  */
13200       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13201         break;
13202       /* Consume the `,' token.  */
13203       cp_lexer_consume_token (parser->lexer);
13204     }
13205
13206   return clobbers;
13207 }
13208
13209 /* Parse an (optional) series of attributes.
13210
13211    attributes:
13212      attributes attribute
13213
13214    attribute:
13215      __attribute__ (( attribute-list [opt] ))  
13216
13217    The return value is as for cp_parser_attribute_list.  */
13218      
13219 static tree
13220 cp_parser_attributes_opt (parser)
13221      cp_parser *parser;
13222 {
13223   tree attributes = NULL_TREE;
13224
13225   while (true)
13226     {
13227       cp_token *token;
13228       tree attribute_list;
13229
13230       /* Peek at the next token.  */
13231       token = cp_lexer_peek_token (parser->lexer);
13232       /* If it's not `__attribute__', then we're done.  */
13233       if (token->keyword != RID_ATTRIBUTE)
13234         break;
13235
13236       /* Consume the `__attribute__' keyword.  */
13237       cp_lexer_consume_token (parser->lexer);
13238       /* Look for the two `(' tokens.  */
13239       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13240       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13241
13242       /* Peek at the next token.  */
13243       token = cp_lexer_peek_token (parser->lexer);
13244       if (token->type != CPP_CLOSE_PAREN)
13245         /* Parse the attribute-list.  */
13246         attribute_list = cp_parser_attribute_list (parser);
13247       else
13248         /* If the next token is a `)', then there is no attribute
13249            list.  */
13250         attribute_list = NULL;
13251
13252       /* Look for the two `)' tokens.  */
13253       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13254       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13255
13256       /* Add these new attributes to the list.  */
13257       attributes = chainon (attributes, attribute_list);
13258     }
13259
13260   return attributes;
13261 }
13262
13263 /* Parse an attribute-list.  
13264
13265    attribute-list:  
13266      attribute 
13267      attribute-list , attribute
13268
13269    attribute:
13270      identifier     
13271      identifier ( identifier )
13272      identifier ( identifier , expression-list )
13273      identifier ( expression-list ) 
13274
13275    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13276    TREE_PURPOSE of each node is the identifier indicating which
13277    attribute is in use.  The TREE_VALUE represents the arguments, if
13278    any.  */
13279
13280 static tree
13281 cp_parser_attribute_list (parser)
13282      cp_parser *parser;
13283 {
13284   tree attribute_list = NULL_TREE;
13285
13286   while (true)
13287     {
13288       cp_token *token;
13289       tree identifier;
13290       tree attribute;
13291
13292       /* Look for the identifier.  We also allow keywords here; for
13293          example `__attribute__ ((const))' is legal.  */
13294       token = cp_lexer_peek_token (parser->lexer);
13295       if (token->type != CPP_NAME 
13296           && token->type != CPP_KEYWORD)
13297         return error_mark_node;
13298       /* Consume the token.  */
13299       token = cp_lexer_consume_token (parser->lexer);
13300       
13301       /* Save away the identifier that indicates which attribute this is.  */
13302       identifier = token->value;
13303       attribute = build_tree_list (identifier, NULL_TREE);
13304
13305       /* Peek at the next token.  */
13306       token = cp_lexer_peek_token (parser->lexer);
13307       /* If it's an `(', then parse the attribute arguments.  */
13308       if (token->type == CPP_OPEN_PAREN)
13309         {
13310           tree arguments;
13311           int arguments_allowed_p = 1;
13312
13313           /* Consume the `('.  */
13314           cp_lexer_consume_token (parser->lexer);
13315           /* Peek at the next token.  */
13316           token = cp_lexer_peek_token (parser->lexer);
13317           /* Check to see if the next token is an identifier.  */
13318           if (token->type == CPP_NAME)
13319             {
13320               /* Save the identifier.  */
13321               identifier = token->value;
13322               /* Consume the identifier.  */
13323               cp_lexer_consume_token (parser->lexer);
13324               /* Peek at the next token.  */
13325               token = cp_lexer_peek_token (parser->lexer);
13326               /* If the next token is a `,', then there are some other
13327                  expressions as well.  */
13328               if (token->type == CPP_COMMA)
13329                 /* Consume the comma.  */
13330                 cp_lexer_consume_token (parser->lexer);
13331               else
13332                 arguments_allowed_p = 0;
13333             }
13334           else
13335             identifier = NULL_TREE;
13336
13337           /* If there are arguments, parse them too.  */
13338           if (arguments_allowed_p)
13339             arguments = cp_parser_expression_list (parser);
13340           else
13341             arguments = NULL_TREE;
13342
13343           /* Combine the identifier and the arguments.  */
13344           if (identifier)
13345             arguments = tree_cons (NULL_TREE, identifier, arguments);
13346
13347           /* Save the identifier and arguments away.  */
13348           TREE_VALUE (attribute) = arguments;
13349
13350           /* Look for the closing `)'.  */
13351           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13352         }
13353
13354       /* Add this attribute to the list.  */
13355       TREE_CHAIN (attribute) = attribute_list;
13356       attribute_list = attribute;
13357
13358       /* Now, look for more attributes.  */
13359       token = cp_lexer_peek_token (parser->lexer);
13360       /* If the next token isn't a `,', we're done.  */
13361       if (token->type != CPP_COMMA)
13362         break;
13363
13364       /* Consume the commma and keep going.  */
13365       cp_lexer_consume_token (parser->lexer);
13366     }
13367
13368   /* We built up the list in reverse order.  */
13369   return nreverse (attribute_list);
13370 }
13371
13372 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13373    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13374    current value of the PEDANTIC flag, regardless of whether or not
13375    the `__extension__' keyword is present.  The caller is responsible
13376    for restoring the value of the PEDANTIC flag.  */
13377
13378 static bool
13379 cp_parser_extension_opt (parser, saved_pedantic)
13380      cp_parser *parser;
13381      int *saved_pedantic;
13382 {
13383   /* Save the old value of the PEDANTIC flag.  */
13384   *saved_pedantic = pedantic;
13385
13386   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13387     {
13388       /* Consume the `__extension__' token.  */
13389       cp_lexer_consume_token (parser->lexer);
13390       /* We're not being pedantic while the `__extension__' keyword is
13391          in effect.  */
13392       pedantic = 0;
13393
13394       return true;
13395     }
13396
13397   return false;
13398 }
13399
13400 /* Parse a label declaration.
13401
13402    label-declaration:
13403      __label__ label-declarator-seq ;
13404
13405    label-declarator-seq:
13406      identifier , label-declarator-seq
13407      identifier  */
13408
13409 static void
13410 cp_parser_label_declaration (parser)
13411      cp_parser *parser;
13412 {
13413   /* Look for the `__label__' keyword.  */
13414   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13415
13416   while (true)
13417     {
13418       tree identifier;
13419
13420       /* Look for an identifier.  */
13421       identifier = cp_parser_identifier (parser);
13422       /* Declare it as a lobel.  */
13423       finish_label_decl (identifier);
13424       /* If the next token is a `;', stop.  */
13425       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13426         break;
13427       /* Look for the `,' separating the label declarations.  */
13428       cp_parser_require (parser, CPP_COMMA, "`,'");
13429     }
13430
13431   /* Look for the final `;'.  */
13432   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13433 }
13434
13435 /* Support Functions */
13436
13437 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13438    NAME should have one of the representations used for an
13439    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13440    is returned.  If PARSER->SCOPE is a dependent type, then a
13441    SCOPE_REF is returned.
13442
13443    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13444    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13445    was formed.  Abstractly, such entities should not be passed to this
13446    function, because they do not need to be looked up, but it is
13447    simpler to check for this special case here, rather than at the
13448    call-sites.
13449
13450    In cases not explicitly covered above, this function returns a
13451    DECL, OVERLOAD, or baselink representing the result of the lookup.
13452    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13453    is returned.
13454
13455    If CHECK_ACCESS is TRUE, then access control is performed on the
13456    declaration to which the name resolves, and an error message is
13457    issued if the declaration is inaccessible.
13458
13459    If IS_TYPE is TRUE, bindings that do not refer to types are
13460    ignored.
13461
13462    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13463    are ignored.
13464
13465    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13466    types.  */
13467
13468 static tree
13469 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access, 
13470                        bool is_type, bool is_namespace, bool check_dependency)
13471 {
13472   tree decl;
13473   tree object_type = parser->context->object_type;
13474
13475   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13476      no longer valid.  Note that if we are parsing tentatively, and
13477      the parse fails, OBJECT_TYPE will be automatically restored.  */
13478   parser->context->object_type = NULL_TREE;
13479
13480   if (name == error_mark_node)
13481     return error_mark_node;
13482
13483   /* A template-id has already been resolved; there is no lookup to
13484      do.  */
13485   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13486     return name;
13487   if (BASELINK_P (name))
13488     {
13489       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13490                            == TEMPLATE_ID_EXPR),
13491                           20020909);
13492       return name;
13493     }
13494
13495   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13496      it should already have been checked to make sure that the name
13497      used matches the type being destroyed.  */
13498   if (TREE_CODE (name) == BIT_NOT_EXPR)
13499     {
13500       tree type;
13501
13502       /* Figure out to which type this destructor applies.  */
13503       if (parser->scope)
13504         type = parser->scope;
13505       else if (object_type)
13506         type = object_type;
13507       else
13508         type = current_class_type;
13509       /* If that's not a class type, there is no destructor.  */
13510       if (!type || !CLASS_TYPE_P (type))
13511         return error_mark_node;
13512       /* If it was a class type, return the destructor.  */
13513       return CLASSTYPE_DESTRUCTORS (type);
13514     }
13515
13516   /* By this point, the NAME should be an ordinary identifier.  If
13517      the id-expression was a qualified name, the qualifying scope is
13518      stored in PARSER->SCOPE at this point.  */
13519   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13520                       20000619);
13521   
13522   /* Perform the lookup.  */
13523   if (parser->scope)
13524     { 
13525       bool dependent_type_p;
13526
13527       if (parser->scope == error_mark_node)
13528         return error_mark_node;
13529
13530       /* If the SCOPE is dependent, the lookup must be deferred until
13531          the template is instantiated -- unless we are explicitly
13532          looking up names in uninstantiated templates.  Even then, we
13533          cannot look up the name if the scope is not a class type; it
13534          might, for example, be a template type parameter.  */
13535       dependent_type_p = (TYPE_P (parser->scope)
13536                           && !(parser->in_declarator_p
13537                                && currently_open_class (parser->scope))
13538                           && cp_parser_dependent_type_p (parser->scope));
13539       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13540            && dependent_type_p)
13541         {
13542           if (!is_type)
13543             decl = build_nt (SCOPE_REF, parser->scope, name);
13544           else
13545             /* The resolution to Core Issue 180 says that `struct A::B'
13546                should be considered a type-name, even if `A' is
13547                dependent.  */
13548             decl = TYPE_NAME (make_typename_type (parser->scope,
13549                                                   name,
13550                                                   /*complain=*/1));
13551         }
13552       else
13553         {
13554           /* If PARSER->SCOPE is a dependent type, then it must be a
13555              class type, and we must not be checking dependencies;
13556              otherwise, we would have processed this lookup above.  So
13557              that PARSER->SCOPE is not considered a dependent base by
13558              lookup_member, we must enter the scope here.  */
13559           if (dependent_type_p)
13560             push_scope (parser->scope);
13561           /* If the PARSER->SCOPE is a a template specialization, it
13562              may be instantiated during name lookup.  In that case,
13563              errors may be issued.  Even if we rollback the current
13564              tentative parse, those errors are valid.  */
13565           decl = lookup_qualified_name (parser->scope, name, is_type,
13566                                         /*flags=*/0);
13567           if (dependent_type_p)
13568             pop_scope (parser->scope);
13569         }
13570       parser->qualifying_scope = parser->scope;
13571       parser->object_scope = NULL_TREE;
13572     }
13573   else if (object_type)
13574     {
13575       tree object_decl = NULL_TREE;
13576       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13577          OBJECT_TYPE is not a class.  */
13578       if (CLASS_TYPE_P (object_type))
13579         /* If the OBJECT_TYPE is a template specialization, it may
13580            be instantiated during name lookup.  In that case, errors
13581            may be issued.  Even if we rollback the current tentative
13582            parse, those errors are valid.  */
13583         object_decl = lookup_member (object_type,
13584                                      name,
13585                                      /*protect=*/0, is_type);
13586       /* Look it up in the enclosing context, too.  */
13587       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13588                                is_namespace,
13589                                /*flags=*/0);
13590       parser->object_scope = object_type;
13591       parser->qualifying_scope = NULL_TREE;
13592       if (object_decl)
13593         decl = object_decl;
13594     }
13595   else
13596     {
13597       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13598                                is_namespace,
13599                                /*flags=*/0);
13600       parser->qualifying_scope = NULL_TREE;
13601       parser->object_scope = NULL_TREE;
13602     }
13603
13604   /* If the lookup failed, let our caller know.  */
13605   if (!decl 
13606       || decl == error_mark_node
13607       || (TREE_CODE (decl) == FUNCTION_DECL 
13608           && DECL_ANTICIPATED (decl)))
13609     return error_mark_node;
13610
13611   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13612   if (TREE_CODE (decl) == TREE_LIST)
13613     {
13614       /* The error message we have to print is too complicated for
13615          cp_parser_error, so we incorporate its actions directly.  */
13616       if (!cp_parser_simulate_error (parser))
13617         {
13618           error ("reference to `%D' is ambiguous", name);
13619           print_candidates (decl);
13620         }
13621       return error_mark_node;
13622     }
13623
13624   my_friendly_assert (DECL_P (decl) 
13625                       || TREE_CODE (decl) == OVERLOAD
13626                       || TREE_CODE (decl) == SCOPE_REF
13627                       || BASELINK_P (decl),
13628                       20000619);
13629
13630   /* If we have resolved the name of a member declaration, check to
13631      see if the declaration is accessible.  When the name resolves to
13632      set of overloaded functions, accesibility is checked when
13633      overload resolution is done.  
13634
13635      During an explicit instantiation, access is not checked at all,
13636      as per [temp.explicit].  */
13637   if (check_access && scope_chain->check_access && DECL_P (decl))
13638     {
13639       tree qualifying_type;
13640       
13641       /* Figure out the type through which DECL is being
13642          accessed.  */
13643       qualifying_type 
13644         = cp_parser_scope_through_which_access_occurs (decl,
13645                                                        object_type,
13646                                                        parser->scope);
13647       if (qualifying_type)
13648         {
13649           /* If we are supposed to defer access checks, just record
13650              the information for later.  */
13651           if (parser->context->deferring_access_checks_p)
13652             cp_parser_defer_access_check (parser, qualifying_type, decl);
13653           /* Otherwise, check accessibility now.  */
13654           else
13655             enforce_access (qualifying_type, decl);
13656         }
13657     }
13658
13659   return decl;
13660 }
13661
13662 /* Like cp_parser_lookup_name, but for use in the typical case where
13663    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13664    TRUE.  */
13665
13666 static tree
13667 cp_parser_lookup_name_simple (parser, name)
13668      cp_parser *parser;
13669      tree name;
13670 {
13671   return cp_parser_lookup_name (parser, name, 
13672                                 /*check_access=*/true,
13673                                 /*is_type=*/false,
13674                                 /*is_namespace=*/false,
13675                                 /*check_dependency=*/true);
13676 }
13677
13678 /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
13679    TYPENAME_TYPE corresponds.  Note that this function peers inside
13680    uninstantiated templates and therefore should be used only in
13681    extremely limited situations.  */
13682
13683 static tree
13684 cp_parser_resolve_typename_type (parser, type)
13685      cp_parser *parser;
13686      tree type;
13687 {
13688   tree scope;
13689   tree name;
13690   tree decl;
13691
13692   my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13693                       20010702);
13694
13695   scope = TYPE_CONTEXT (type);
13696   name = DECL_NAME (TYPE_NAME (type));
13697
13698   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13699      it first before we can figure out what NAME refers to.  */
13700   if (TREE_CODE (scope) == TYPENAME_TYPE)
13701     scope = cp_parser_resolve_typename_type (parser, scope);
13702   /* If we don't know what SCOPE refers to, then we cannot resolve the
13703      TYPENAME_TYPE.  */
13704   if (scope == error_mark_node)
13705     return error_mark_node;
13706   /* If the SCOPE is a template type parameter, we have no way of
13707      resolving the name.  */
13708   if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13709     return type;
13710   /* Enter the SCOPE so that name lookup will be resolved as if we
13711      were in the class definition.  In particular, SCOPE will no
13712      longer be considered a dependent type.  */
13713   push_scope (scope);
13714   /* Look up the declaration.  */
13715   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13716   /* If all went well, we got a TYPE_DECL for a non-typename.  */
13717   if (!decl 
13718       || TREE_CODE (decl) != TYPE_DECL 
13719       || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13720     {
13721       cp_parser_error (parser, "could not resolve typename type");
13722       type = error_mark_node;
13723     }
13724   else
13725     type = TREE_TYPE (decl);
13726   /* Leave the SCOPE.  */
13727   pop_scope (scope);
13728
13729   return type;
13730 }
13731
13732 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13733    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13734    true, the DECL indicates the class being defined in a class-head,
13735    or declared in an elaborated-type-specifier.
13736
13737    Otherwise, return DECL.  */
13738
13739 static tree
13740 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13741 {
13742   /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13743      the scope of the class, then treat the TEMPLATE_DECL as a
13744      class-name.  For example, in:
13745
13746        template <class T> struct S {
13747          S s;
13748        };
13749
13750      is OK.  
13751
13752      If the TEMPLATE_DECL is being declared as part of a class-head,
13753      the same translation occurs:
13754
13755        struct A { 
13756          template <typename T> struct B;
13757        };
13758
13759        template <typename T> struct A::B {}; 
13760    
13761      Similarly, in a elaborated-type-specifier:
13762
13763        namespace N { struct X{}; }
13764
13765        struct A {
13766          template <typename T> friend struct N::X;
13767        };
13768
13769      */
13770   if (DECL_CLASS_TEMPLATE_P (decl)
13771       && (tag_name_p
13772           || (current_class_type
13773               && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13774                               current_class_type))))
13775     return DECL_TEMPLATE_RESULT (decl);
13776
13777   return decl;
13778 }
13779
13780 /* If too many, or too few, template-parameter lists apply to the
13781    declarator, issue an error message.  Returns TRUE if all went well,
13782    and FALSE otherwise.  */
13783
13784 static bool
13785 cp_parser_check_declarator_template_parameters (parser, declarator)
13786      cp_parser *parser;
13787      tree declarator;
13788 {
13789   unsigned num_templates;
13790
13791   /* We haven't seen any classes that involve template parameters yet.  */
13792   num_templates = 0;
13793
13794   switch (TREE_CODE (declarator))
13795     {
13796     case CALL_EXPR:
13797     case ARRAY_REF:
13798     case INDIRECT_REF:
13799     case ADDR_EXPR:
13800       {
13801         tree main_declarator = TREE_OPERAND (declarator, 0);
13802         return
13803           cp_parser_check_declarator_template_parameters (parser, 
13804                                                           main_declarator);
13805       }
13806
13807     case SCOPE_REF:
13808       {
13809         tree scope;
13810         tree member;
13811
13812         scope = TREE_OPERAND (declarator, 0);
13813         member = TREE_OPERAND (declarator, 1);
13814
13815         /* If this is a pointer-to-member, then we are not interested
13816            in the SCOPE, because it does not qualify the thing that is
13817            being declared.  */
13818         if (TREE_CODE (member) == INDIRECT_REF)
13819           return (cp_parser_check_declarator_template_parameters
13820                   (parser, member));
13821
13822         while (scope && CLASS_TYPE_P (scope))
13823           {
13824             /* You're supposed to have one `template <...>'
13825                for every template class, but you don't need one
13826                for a full specialization.  For example:
13827                
13828                template <class T> struct S{};
13829                template <> struct S<int> { void f(); };
13830                void S<int>::f () {}
13831                
13832                is correct; there shouldn't be a `template <>' for
13833                the definition of `S<int>::f'.  */
13834             if (CLASSTYPE_TEMPLATE_INFO (scope)
13835                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13836                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13837                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13838               ++num_templates;
13839
13840             scope = TYPE_CONTEXT (scope);
13841           }
13842       }
13843
13844       /* Fall through.  */
13845
13846     default:
13847       /* If the DECLARATOR has the form `X<y>' then it uses one
13848          additional level of template parameters.  */
13849       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13850         ++num_templates;
13851
13852       return cp_parser_check_template_parameters (parser, 
13853                                                   num_templates);
13854     }
13855 }
13856
13857 /* NUM_TEMPLATES were used in the current declaration.  If that is
13858    invalid, return FALSE and issue an error messages.  Otherwise,
13859    return TRUE.  */
13860
13861 static bool
13862 cp_parser_check_template_parameters (parser, num_templates)
13863      cp_parser *parser;
13864      unsigned num_templates;
13865 {
13866   /* If there are more template classes than parameter lists, we have
13867      something like:
13868      
13869        template <class T> void S<T>::R<T>::f ();  */
13870   if (parser->num_template_parameter_lists < num_templates)
13871     {
13872       error ("too few template-parameter-lists");
13873       return false;
13874     }
13875   /* If there are the same number of template classes and parameter
13876      lists, that's OK.  */
13877   if (parser->num_template_parameter_lists == num_templates)
13878     return true;
13879   /* If there are more, but only one more, then we are referring to a
13880      member template.  That's OK too.  */
13881   if (parser->num_template_parameter_lists == num_templates + 1)
13882       return true;
13883   /* Otherwise, there are too many template parameter lists.  We have
13884      something like:
13885
13886      template <class T> template <class U> void S::f();  */
13887   error ("too many template-parameter-lists");
13888   return false;
13889 }
13890
13891 /* Parse a binary-expression of the general form:
13892
13893    binary-expression:
13894      <expr>
13895      binary-expression <token> <expr>
13896
13897    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13898    to parser the <expr>s.  If the first production is used, then the
13899    value returned by FN is returned directly.  Otherwise, a node with
13900    the indicated EXPR_TYPE is returned, with operands corresponding to
13901    the two sub-expressions.  */
13902
13903 static tree
13904 cp_parser_binary_expression (parser, token_tree_map, fn)
13905      cp_parser *parser;
13906      const cp_parser_token_tree_map token_tree_map;
13907      cp_parser_expression_fn fn;
13908 {
13909   tree lhs;
13910
13911   /* Parse the first expression.  */
13912   lhs = (*fn) (parser);
13913   /* Now, look for more expressions.  */
13914   while (true)
13915     {
13916       cp_token *token;
13917       const cp_parser_token_tree_map_node *map_node;
13918       tree rhs;
13919
13920       /* Peek at the next token.  */
13921       token = cp_lexer_peek_token (parser->lexer);
13922       /* If the token is `>', and that's not an operator at the
13923          moment, then we're done.  */
13924       if (token->type == CPP_GREATER
13925           && !parser->greater_than_is_operator_p)
13926         break;
13927       /* If we find one of the tokens we want, build the correspoding
13928          tree representation.  */
13929       for (map_node = token_tree_map; 
13930            map_node->token_type != CPP_EOF;
13931            ++map_node)
13932         if (map_node->token_type == token->type)
13933           {
13934             /* Consume the operator token.  */
13935             cp_lexer_consume_token (parser->lexer);
13936             /* Parse the right-hand side of the expression.  */
13937             rhs = (*fn) (parser);
13938             /* Build the binary tree node.  */
13939             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13940             break;
13941           }
13942
13943       /* If the token wasn't one of the ones we want, we're done.  */
13944       if (map_node->token_type == CPP_EOF)
13945         break;
13946     }
13947
13948   return lhs;
13949 }
13950
13951 /* Parse an optional `::' token indicating that the following name is
13952    from the global namespace.  If so, PARSER->SCOPE is set to the
13953    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13954    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13955    Returns the new value of PARSER->SCOPE, if the `::' token is
13956    present, and NULL_TREE otherwise.  */
13957
13958 static tree
13959 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13960      cp_parser *parser;
13961      bool current_scope_valid_p;
13962 {
13963   cp_token *token;
13964
13965   /* Peek at the next token.  */
13966   token = cp_lexer_peek_token (parser->lexer);
13967   /* If we're looking at a `::' token then we're starting from the
13968      global namespace, not our current location.  */
13969   if (token->type == CPP_SCOPE)
13970     {
13971       /* Consume the `::' token.  */
13972       cp_lexer_consume_token (parser->lexer);
13973       /* Set the SCOPE so that we know where to start the lookup.  */
13974       parser->scope = global_namespace;
13975       parser->qualifying_scope = global_namespace;
13976       parser->object_scope = NULL_TREE;
13977
13978       return parser->scope;
13979     }
13980   else if (!current_scope_valid_p)
13981     {
13982       parser->scope = NULL_TREE;
13983       parser->qualifying_scope = NULL_TREE;
13984       parser->object_scope = NULL_TREE;
13985     }
13986
13987   return NULL_TREE;
13988 }
13989
13990 /* Returns TRUE if the upcoming token sequence is the start of a
13991    constructor declarator.  If FRIEND_P is true, the declarator is
13992    preceded by the `friend' specifier.  */
13993
13994 static bool
13995 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13996 {
13997   bool constructor_p;
13998   tree type_decl = NULL_TREE;
13999   bool nested_name_p;
14000   cp_token *next_token;
14001
14002   /* The common case is that this is not a constructor declarator, so
14003      try to avoid doing lots of work if at all possible.  */
14004   next_token = cp_lexer_peek_token (parser->lexer);
14005   if (next_token->type != CPP_NAME
14006       && next_token->type != CPP_SCOPE
14007       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14008       && next_token->type != CPP_TEMPLATE_ID)
14009     return false;
14010
14011   /* Parse tentatively; we are going to roll back all of the tokens
14012      consumed here.  */
14013   cp_parser_parse_tentatively (parser);
14014   /* Assume that we are looking at a constructor declarator.  */
14015   constructor_p = true;
14016   /* Look for the optional `::' operator.  */
14017   cp_parser_global_scope_opt (parser,
14018                               /*current_scope_valid_p=*/false);
14019   /* Look for the nested-name-specifier.  */
14020   nested_name_p 
14021     = (cp_parser_nested_name_specifier_opt (parser,
14022                                             /*typename_keyword_p=*/false,
14023                                             /*check_dependency_p=*/false,
14024                                             /*type_p=*/false)
14025        != NULL_TREE);
14026   /* Outside of a class-specifier, there must be a
14027      nested-name-specifier.  */
14028   if (!nested_name_p && 
14029       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14030        || friend_p))
14031     constructor_p = false;
14032   /* If we still think that this might be a constructor-declarator,
14033      look for a class-name.  */
14034   if (constructor_p)
14035     {
14036       /* If we have:
14037
14038            template <typename T> struct S { S(); }
14039            template <typename T> S<T>::S ();
14040
14041          we must recognize that the nested `S' names a class.
14042          Similarly, for:
14043
14044            template <typename T> S<T>::S<T> ();
14045
14046          we must recognize that the nested `S' names a template.  */
14047       type_decl = cp_parser_class_name (parser,
14048                                         /*typename_keyword_p=*/false,
14049                                         /*template_keyword_p=*/false,
14050                                         /*type_p=*/false,
14051                                         /*check_access_p=*/false,
14052                                         /*check_dependency_p=*/false,
14053                                         /*class_head_p=*/false);
14054       /* If there was no class-name, then this is not a constructor.  */
14055       constructor_p = !cp_parser_error_occurred (parser);
14056     }
14057   /* If we're still considering a constructor, we have to see a `(',
14058      to begin the parameter-declaration-clause, followed by either a
14059      `)', an `...', or a decl-specifier.  We need to check for a
14060      type-specifier to avoid being fooled into thinking that:
14061
14062        S::S (f) (int);
14063
14064      is a constructor.  (It is actually a function named `f' that
14065      takes one parameter (of type `int') and returns a value of type
14066      `S::S'.  */
14067   if (constructor_p 
14068       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14069     {
14070       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14071           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14072           && !cp_parser_storage_class_specifier_opt (parser))
14073         {
14074           if (current_class_type 
14075               && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
14076             /* The constructor for one class cannot be declared inside
14077                another.  */
14078             constructor_p = false;
14079           else
14080             {
14081               tree type;
14082
14083               /* Names appearing in the type-specifier should be looked up
14084                  in the scope of the class.  */
14085               if (current_class_type)
14086                 type = NULL_TREE;
14087               else
14088                 {
14089                   type = TREE_TYPE (type_decl);
14090                   if (TREE_CODE (type) == TYPENAME_TYPE)
14091                     type = cp_parser_resolve_typename_type (parser, type);
14092                   push_scope (type);
14093                 }
14094               /* Look for the type-specifier.  */
14095               cp_parser_type_specifier (parser,
14096                                         CP_PARSER_FLAGS_NONE,
14097                                         /*is_friend=*/false,
14098                                         /*is_declarator=*/true,
14099                                         /*declares_class_or_enum=*/NULL,
14100                                         /*is_cv_qualifier=*/NULL);
14101               /* Leave the scope of the class.  */
14102               if (type)
14103                 pop_scope (type);
14104
14105               constructor_p = !cp_parser_error_occurred (parser);
14106             }
14107         }
14108     }
14109   else
14110     constructor_p = false;
14111   /* We did not really want to consume any tokens.  */
14112   cp_parser_abort_tentative_parse (parser);
14113
14114   return constructor_p;
14115 }
14116
14117 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14118    ATTRIBUTES, and DECLARATOR.  The ACCESS_CHECKS have been deferred;
14119    they must be performed once we are in the scope of the function.
14120
14121    Returns the function defined.  */
14122
14123 static tree
14124 cp_parser_function_definition_from_specifiers_and_declarator
14125   (parser, decl_specifiers, attributes, declarator, access_checks)
14126      cp_parser *parser;
14127      tree decl_specifiers;
14128      tree attributes;
14129      tree declarator;
14130      tree access_checks;
14131 {
14132   tree fn;
14133   bool success_p;
14134
14135   /* Begin the function-definition.  */
14136   success_p = begin_function_definition (decl_specifiers, 
14137                                          attributes, 
14138                                          declarator);
14139
14140   /* If there were names looked up in the decl-specifier-seq that we
14141      did not check, check them now.  We must wait until we are in the
14142      scope of the function to perform the checks, since the function
14143      might be a friend.  */
14144   cp_parser_perform_deferred_access_checks (access_checks);
14145
14146   if (!success_p)
14147     {
14148       /* If begin_function_definition didn't like the definition, skip
14149          the entire function.  */
14150       error ("invalid function declaration");
14151       cp_parser_skip_to_end_of_block_or_statement (parser);
14152       fn = error_mark_node;
14153     }
14154   else
14155     fn = cp_parser_function_definition_after_declarator (parser,
14156                                                          /*inline_p=*/false);
14157
14158   return fn;
14159 }
14160
14161 /* Parse the part of a function-definition that follows the
14162    declarator.  INLINE_P is TRUE iff this function is an inline
14163    function defined with a class-specifier.
14164
14165    Returns the function defined.  */
14166
14167 static tree 
14168 cp_parser_function_definition_after_declarator (parser, 
14169                                                 inline_p)
14170      cp_parser *parser;
14171      bool inline_p;
14172 {
14173   tree fn;
14174   bool ctor_initializer_p = false;
14175   bool saved_in_unbraced_linkage_specification_p;
14176   unsigned saved_num_template_parameter_lists;
14177
14178   /* If the next token is `return', then the code may be trying to
14179      make use of the "named return value" extension that G++ used to
14180      support.  */
14181   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14182     {
14183       /* Consume the `return' keyword.  */
14184       cp_lexer_consume_token (parser->lexer);
14185       /* Look for the identifier that indicates what value is to be
14186          returned.  */
14187       cp_parser_identifier (parser);
14188       /* Issue an error message.  */
14189       error ("named return values are no longer supported");
14190       /* Skip tokens until we reach the start of the function body.  */
14191       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14192         cp_lexer_consume_token (parser->lexer);
14193     }
14194   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14195      anything declared inside `f'.  */
14196   saved_in_unbraced_linkage_specification_p 
14197     = parser->in_unbraced_linkage_specification_p;
14198   parser->in_unbraced_linkage_specification_p = false;
14199   /* Inside the function, surrounding template-parameter-lists do not
14200      apply.  */
14201   saved_num_template_parameter_lists 
14202     = parser->num_template_parameter_lists; 
14203   parser->num_template_parameter_lists = 0;
14204   /* If the next token is `try', then we are looking at a
14205      function-try-block.  */
14206   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14207     ctor_initializer_p = cp_parser_function_try_block (parser);
14208   /* A function-try-block includes the function-body, so we only do
14209      this next part if we're not processing a function-try-block.  */
14210   else
14211     ctor_initializer_p 
14212       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14213
14214   /* Finish the function.  */
14215   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14216                         (inline_p ? 2 : 0));
14217   /* Generate code for it, if necessary.  */
14218   expand_body (fn);
14219   /* Restore the saved values.  */
14220   parser->in_unbraced_linkage_specification_p 
14221     = saved_in_unbraced_linkage_specification_p;
14222   parser->num_template_parameter_lists 
14223     = saved_num_template_parameter_lists;
14224
14225   return fn;
14226 }
14227
14228 /* Parse a template-declaration, assuming that the `export' (and
14229    `extern') keywords, if present, has already been scanned.  MEMBER_P
14230    is as for cp_parser_template_declaration.  */
14231
14232 static void
14233 cp_parser_template_declaration_after_export (parser, member_p)
14234      cp_parser *parser;
14235      bool member_p;
14236 {
14237   tree decl = NULL_TREE;
14238   tree parameter_list;
14239   bool friend_p = false;
14240
14241   /* Look for the `template' keyword.  */
14242   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14243     return;
14244       
14245   /* And the `<'.  */
14246   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14247     return;
14248       
14249   /* Parse the template parameters.  */
14250   begin_template_parm_list ();
14251   /* If the next token is `>', then we have an invalid
14252      specialization.  Rather than complain about an invalid template
14253      parameter, issue an error message here.  */
14254   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14255     {
14256       cp_parser_error (parser, "invalid explicit specialization");
14257       parameter_list = NULL_TREE;
14258     }
14259   else
14260     parameter_list = cp_parser_template_parameter_list (parser);
14261   parameter_list = end_template_parm_list (parameter_list);
14262   /* Look for the `>'.  */
14263   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14264   /* We just processed one more parameter list.  */
14265   ++parser->num_template_parameter_lists;
14266   /* If the next token is `template', there are more template
14267      parameters.  */
14268   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14269                                       RID_TEMPLATE))
14270     cp_parser_template_declaration_after_export (parser, member_p);
14271   else
14272     {
14273       decl = cp_parser_single_declaration (parser,
14274                                            member_p,
14275                                            &friend_p);
14276
14277       /* If this is a member template declaration, let the front
14278          end know.  */
14279       if (member_p && !friend_p && decl)
14280         decl = finish_member_template_decl (decl);
14281       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14282         make_friend_class (current_class_type, TREE_TYPE (decl));
14283     }
14284   /* We are done with the current parameter list.  */
14285   --parser->num_template_parameter_lists;
14286
14287   /* Finish up.  */
14288   finish_template_decl (parameter_list);
14289
14290   /* Register member declarations.  */
14291   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14292     finish_member_declaration (decl);
14293
14294   /* If DECL is a function template, we must return to parse it later.
14295      (Even though there is no definition, there might be default
14296      arguments that need handling.)  */
14297   if (member_p && decl 
14298       && (TREE_CODE (decl) == FUNCTION_DECL
14299           || DECL_FUNCTION_TEMPLATE_P (decl)))
14300     TREE_VALUE (parser->unparsed_functions_queues)
14301       = tree_cons (NULL_TREE, decl, 
14302                    TREE_VALUE (parser->unparsed_functions_queues));
14303 }
14304
14305 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14306    `function-definition' sequence.  MEMBER_P is true, this declaration
14307    appears in a class scope.
14308
14309    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14310    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14311
14312 static tree
14313 cp_parser_single_declaration (parser, 
14314                               member_p,
14315                               friend_p)
14316      cp_parser *parser;
14317      bool member_p;
14318      bool *friend_p;
14319 {
14320   bool declares_class_or_enum;
14321   tree decl = NULL_TREE;
14322   tree decl_specifiers;
14323   tree attributes;
14324   tree access_checks;
14325
14326   /* Parse the dependent declaration.  We don't know yet
14327      whether it will be a function-definition.  */
14328   cp_parser_parse_tentatively (parser);
14329   /* Defer access checks until we know what is being declared.  */
14330   cp_parser_start_deferring_access_checks (parser);
14331   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14332      alternative.  */
14333   decl_specifiers 
14334     = cp_parser_decl_specifier_seq (parser,
14335                                     CP_PARSER_FLAGS_OPTIONAL,
14336                                     &attributes,
14337                                     &declares_class_or_enum);
14338   /* Gather up the access checks that occurred the
14339      decl-specifier-seq.  */
14340   access_checks = cp_parser_stop_deferring_access_checks (parser);
14341   /* Check for the declaration of a template class.  */
14342   if (declares_class_or_enum)
14343     {
14344       if (cp_parser_declares_only_class_p (parser))
14345         {
14346           decl = shadow_tag (decl_specifiers);
14347           if (decl)
14348             decl = TYPE_NAME (decl);
14349           else
14350             decl = error_mark_node;
14351         }
14352     }
14353   else
14354     decl = NULL_TREE;
14355   /* If it's not a template class, try for a template function.  If
14356      the next token is a `;', then this declaration does not declare
14357      anything.  But, if there were errors in the decl-specifiers, then
14358      the error might well have come from an attempted class-specifier.
14359      In that case, there's no need to warn about a missing declarator.  */
14360   if (!decl
14361       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14362           || !value_member (error_mark_node, decl_specifiers)))
14363     decl = cp_parser_init_declarator (parser, 
14364                                       decl_specifiers,
14365                                       attributes,
14366                                       access_checks,
14367                                       /*function_definition_allowed_p=*/false,
14368                                       member_p,
14369                                       /*function_definition_p=*/NULL);
14370   /* Clear any current qualification; whatever comes next is the start
14371      of something new.  */
14372   parser->scope = NULL_TREE;
14373   parser->qualifying_scope = NULL_TREE;
14374   parser->object_scope = NULL_TREE;
14375   /* Look for a trailing `;' after the declaration.  */
14376   if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14377       && cp_parser_committed_to_tentative_parse (parser))
14378     cp_parser_skip_to_end_of_block_or_statement (parser);
14379   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
14380   if (cp_parser_parse_definitely (parser))
14381     {
14382       if (friend_p)
14383         *friend_p = cp_parser_friend_p (decl_specifiers);
14384     }
14385   /* Otherwise, try a function-definition.  */
14386   else
14387     decl = cp_parser_function_definition (parser, friend_p);
14388
14389   return decl;
14390 }
14391
14392 /* Parse a functional cast to TYPE.  Returns an expression
14393    representing the cast.  */
14394
14395 static tree
14396 cp_parser_functional_cast (parser, type)
14397      cp_parser *parser;
14398      tree type;
14399 {
14400   tree expression_list;
14401
14402   /* Look for the opening `('.  */
14403   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14404     return error_mark_node;
14405   /* If the next token is not an `)', there are arguments to the
14406      cast.  */
14407   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14408     expression_list = cp_parser_expression_list (parser);
14409   else
14410     expression_list = NULL_TREE;
14411   /* Look for the closing `)'.  */
14412   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14413
14414   return build_functional_cast (type, expression_list);
14415 }
14416
14417 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14418    arguments, or the body of the function have not yet been parsed,
14419    parse them now.  */
14420
14421 static void
14422 cp_parser_late_parsing_for_member (parser, member_function)
14423      cp_parser *parser;
14424      tree member_function;
14425 {
14426   cp_lexer *saved_lexer;
14427
14428   /* If this member is a template, get the underlying
14429      FUNCTION_DECL.  */
14430   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14431     member_function = DECL_TEMPLATE_RESULT (member_function);
14432
14433   /* There should not be any class definitions in progress at this
14434      point; the bodies of members are only parsed outside of all class
14435      definitions.  */
14436   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14437   /* While we're parsing the member functions we might encounter more
14438      classes.  We want to handle them right away, but we don't want
14439      them getting mixed up with functions that are currently in the
14440      queue.  */
14441   parser->unparsed_functions_queues
14442     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14443
14444   /* Make sure that any template parameters are in scope.  */
14445   maybe_begin_member_template_processing (member_function);
14446
14447   /* If the body of the function has not yet been parsed, parse it
14448      now.  */
14449   if (DECL_PENDING_INLINE_P (member_function))
14450     {
14451       tree function_scope;
14452       cp_token_cache *tokens;
14453
14454       /* The function is no longer pending; we are processing it.  */
14455       tokens = DECL_PENDING_INLINE_INFO (member_function);
14456       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14457       DECL_PENDING_INLINE_P (member_function) = 0;
14458       /* If this was an inline function in a local class, enter the scope
14459          of the containing function.  */
14460       function_scope = decl_function_context (member_function);
14461       if (function_scope)
14462         push_function_context_to (function_scope);
14463       
14464       /* Save away the current lexer.  */
14465       saved_lexer = parser->lexer;
14466       /* Make a new lexer to feed us the tokens saved for this function.  */
14467       parser->lexer = cp_lexer_new_from_tokens (tokens);
14468       parser->lexer->next = saved_lexer;
14469       
14470       /* Set the current source position to be the location of the first
14471          token in the saved inline body.  */
14472       cp_lexer_peek_token (parser->lexer);
14473       
14474       /* Let the front end know that we going to be defining this
14475          function.  */
14476       start_function (NULL_TREE, member_function, NULL_TREE,
14477                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14478       
14479       /* Now, parse the body of the function.  */
14480       cp_parser_function_definition_after_declarator (parser,
14481                                                       /*inline_p=*/true);
14482       
14483       /* Leave the scope of the containing function.  */
14484       if (function_scope)
14485         pop_function_context_from (function_scope);
14486       /* Restore the lexer.  */
14487       parser->lexer = saved_lexer;
14488     }
14489
14490   /* Remove any template parameters from the symbol table.  */
14491   maybe_end_member_template_processing ();
14492
14493   /* Restore the queue.  */
14494   parser->unparsed_functions_queues 
14495     = TREE_CHAIN (parser->unparsed_functions_queues);
14496 }
14497
14498 /* FN is a FUNCTION_DECL which may contains a parameter with an
14499    unparsed DEFAULT_ARG.  Parse the default args now.  */
14500
14501 static void
14502 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14503 {
14504   cp_lexer *saved_lexer;
14505   cp_token_cache *tokens;
14506   bool saved_local_variables_forbidden_p;
14507   tree parameters;
14508
14509   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14510        parameters;
14511        parameters = TREE_CHAIN (parameters))
14512     {
14513       if (!TREE_PURPOSE (parameters)
14514           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14515         continue;
14516   
14517        /* Save away the current lexer.  */
14518       saved_lexer = parser->lexer;
14519        /* Create a new one, using the tokens we have saved.  */
14520       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14521       parser->lexer = cp_lexer_new_from_tokens (tokens);
14522
14523        /* Set the current source position to be the location of the
14524           first token in the default argument.  */
14525       cp_lexer_peek_token (parser->lexer);
14526
14527        /* Local variable names (and the `this' keyword) may not appear
14528           in a default argument.  */
14529       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14530       parser->local_variables_forbidden_p = true;
14531        /* Parse the assignment-expression.  */
14532       if (DECL_CONTEXT (fn))
14533         push_nested_class (DECL_CONTEXT (fn), 1);
14534       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14535       if (DECL_CONTEXT (fn))
14536         pop_nested_class ();
14537
14538        /* Restore saved state.  */
14539       parser->lexer = saved_lexer;
14540       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14541     }
14542 }
14543
14544 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14545    either a TYPE or an expression, depending on the form of the
14546    input.  The KEYWORD indicates which kind of expression we have
14547    encountered.  */
14548
14549 static tree
14550 cp_parser_sizeof_operand (parser, keyword)
14551      cp_parser *parser;
14552      enum rid keyword;
14553 {
14554   static const char *format;
14555   tree expr = NULL_TREE;
14556   const char *saved_message;
14557   bool saved_constant_expression_p;
14558
14559   /* Initialize FORMAT the first time we get here.  */
14560   if (!format)
14561     format = "types may not be defined in `%s' expressions";
14562
14563   /* Types cannot be defined in a `sizeof' expression.  Save away the
14564      old message.  */
14565   saved_message = parser->type_definition_forbidden_message;
14566   /* And create the new one.  */
14567   parser->type_definition_forbidden_message 
14568     = ((const char *) 
14569        xmalloc (strlen (format) 
14570                 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14571                 + 1 /* `\0' */));
14572   sprintf ((char *) parser->type_definition_forbidden_message,
14573            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14574
14575   /* The restrictions on constant-expressions do not apply inside
14576      sizeof expressions.  */
14577   saved_constant_expression_p = parser->constant_expression_p;
14578   parser->constant_expression_p = false;
14579
14580   /* Do not actually evaluate the expression.  */
14581   ++skip_evaluation;
14582   /* If it's a `(', then we might be looking at the type-id
14583      construction.  */
14584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14585     {
14586       tree type;
14587
14588       /* We can't be sure yet whether we're looking at a type-id or an
14589          expression.  */
14590       cp_parser_parse_tentatively (parser);
14591       /* Consume the `('.  */
14592       cp_lexer_consume_token (parser->lexer);
14593       /* Parse the type-id.  */
14594       type = cp_parser_type_id (parser);
14595       /* Now, look for the trailing `)'.  */
14596       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14597       /* If all went well, then we're done.  */
14598       if (cp_parser_parse_definitely (parser))
14599         {
14600           /* Build a list of decl-specifiers; right now, we have only
14601              a single type-specifier.  */
14602           type = build_tree_list (NULL_TREE,
14603                                   type);
14604
14605           /* Call grokdeclarator to figure out what type this is.  */
14606           expr = grokdeclarator (NULL_TREE,
14607                                  type,
14608                                  TYPENAME,
14609                                  /*initialized=*/0,
14610                                  /*attrlist=*/NULL);
14611         }
14612     }
14613
14614   /* If the type-id production did not work out, then we must be
14615      looking at the unary-expression production.  */
14616   if (!expr)
14617     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14618   /* Go back to evaluating expressions.  */
14619   --skip_evaluation;
14620
14621   /* Free the message we created.  */
14622   free ((char *) parser->type_definition_forbidden_message);
14623   /* And restore the old one.  */
14624   parser->type_definition_forbidden_message = saved_message;
14625   parser->constant_expression_p = saved_constant_expression_p;
14626
14627   return expr;
14628 }
14629
14630 /* If the current declaration has no declarator, return true.  */
14631
14632 static bool
14633 cp_parser_declares_only_class_p (cp_parser *parser)
14634 {
14635   /* If the next token is a `;' or a `,' then there is no 
14636      declarator.  */
14637   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14638           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14639 }
14640
14641 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14642    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14643
14644 static bool
14645 cp_parser_friend_p (decl_specifiers)
14646      tree decl_specifiers;
14647 {
14648   while (decl_specifiers)
14649     {
14650       /* See if this decl-specifier is `friend'.  */
14651       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14652           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14653         return true;
14654
14655       /* Go on to the next decl-specifier.  */
14656       decl_specifiers = TREE_CHAIN (decl_specifiers);
14657     }
14658
14659   return false;
14660 }
14661
14662 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14663    issue an error message indicating that TOKEN_DESC was expected.
14664    
14665    Returns the token consumed, if the token had the appropriate type.
14666    Otherwise, returns NULL.  */
14667
14668 static cp_token *
14669 cp_parser_require (parser, type, token_desc)
14670      cp_parser *parser;
14671      enum cpp_ttype type;
14672      const char *token_desc;
14673 {
14674   if (cp_lexer_next_token_is (parser->lexer, type))
14675     return cp_lexer_consume_token (parser->lexer);
14676   else
14677     {
14678       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14679       if (!cp_parser_simulate_error (parser))
14680         error ("expected %s", token_desc);
14681       return NULL;
14682     }
14683 }
14684
14685 /* Like cp_parser_require, except that tokens will be skipped until
14686    the desired token is found.  An error message is still produced if
14687    the next token is not as expected.  */
14688
14689 static void
14690 cp_parser_skip_until_found (parser, type, token_desc)
14691      cp_parser *parser;
14692      enum cpp_ttype type;
14693      const char *token_desc;
14694 {
14695   cp_token *token;
14696   unsigned nesting_depth = 0;
14697
14698   if (cp_parser_require (parser, type, token_desc))
14699     return;
14700
14701   /* Skip tokens until the desired token is found.  */
14702   while (true)
14703     {
14704       /* Peek at the next token.  */
14705       token = cp_lexer_peek_token (parser->lexer);
14706       /* If we've reached the token we want, consume it and 
14707          stop.  */
14708       if (token->type == type && !nesting_depth)
14709         {
14710           cp_lexer_consume_token (parser->lexer);
14711           return;
14712         }
14713       /* If we've run out of tokens, stop.  */
14714       if (token->type == CPP_EOF)
14715         return;
14716       if (token->type == CPP_OPEN_BRACE 
14717           || token->type == CPP_OPEN_PAREN
14718           || token->type == CPP_OPEN_SQUARE)
14719         ++nesting_depth;
14720       else if (token->type == CPP_CLOSE_BRACE 
14721                || token->type == CPP_CLOSE_PAREN
14722                || token->type == CPP_CLOSE_SQUARE)
14723         {
14724           if (nesting_depth-- == 0)
14725             return;
14726         }
14727       /* Consume this token.  */
14728       cp_lexer_consume_token (parser->lexer);
14729     }
14730 }
14731
14732 /* If the next token is the indicated keyword, consume it.  Otherwise,
14733    issue an error message indicating that TOKEN_DESC was expected.
14734    
14735    Returns the token consumed, if the token had the appropriate type.
14736    Otherwise, returns NULL.  */
14737
14738 static cp_token *
14739 cp_parser_require_keyword (parser, keyword, token_desc)
14740      cp_parser *parser;
14741      enum rid keyword;
14742      const char *token_desc;
14743 {
14744   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14745
14746   if (token && token->keyword != keyword)
14747     {
14748       dyn_string_t error_msg;
14749
14750       /* Format the error message.  */
14751       error_msg = dyn_string_new (0);
14752       dyn_string_append_cstr (error_msg, "expected ");
14753       dyn_string_append_cstr (error_msg, token_desc);
14754       cp_parser_error (parser, error_msg->s);
14755       dyn_string_delete (error_msg);
14756       return NULL;
14757     }
14758
14759   return token;
14760 }
14761
14762 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14763    function-definition.  */
14764
14765 static bool 
14766 cp_parser_token_starts_function_definition_p (token)
14767      cp_token *token;
14768 {
14769   return (/* An ordinary function-body begins with an `{'.  */
14770           token->type == CPP_OPEN_BRACE
14771           /* A ctor-initializer begins with a `:'.  */
14772           || token->type == CPP_COLON
14773           /* A function-try-block begins with `try'.  */
14774           || token->keyword == RID_TRY
14775           /* The named return value extension begins with `return'.  */
14776           || token->keyword == RID_RETURN);
14777 }
14778
14779 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14780    definition.  */
14781
14782 static bool
14783 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14784 {
14785   cp_token *token;
14786
14787   token = cp_lexer_peek_token (parser->lexer);
14788   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14789 }
14790
14791 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14792    or none_type otherwise.  */
14793
14794 static enum tag_types
14795 cp_parser_token_is_class_key (token)
14796      cp_token *token;
14797 {
14798   switch (token->keyword)
14799     {
14800     case RID_CLASS:
14801       return class_type;
14802     case RID_STRUCT:
14803       return record_type;
14804     case RID_UNION:
14805       return union_type;
14806       
14807     default:
14808       return none_type;
14809     }
14810 }
14811
14812 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14813
14814 static void
14815 cp_parser_check_class_key (enum tag_types class_key, tree type)
14816 {
14817   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14818     pedwarn ("`%s' tag used in naming `%#T'",
14819             class_key == union_type ? "union"
14820              : class_key == record_type ? "struct" : "class", 
14821              type);
14822 }
14823                            
14824 /* Look for the `template' keyword, as a syntactic disambiguator.
14825    Return TRUE iff it is present, in which case it will be 
14826    consumed.  */
14827
14828 static bool
14829 cp_parser_optional_template_keyword (cp_parser *parser)
14830 {
14831   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14832     {
14833       /* The `template' keyword can only be used within templates;
14834          outside templates the parser can always figure out what is a
14835          template and what is not.  */
14836       if (!processing_template_decl)
14837         {
14838           error ("`template' (as a disambiguator) is only allowed "
14839                  "within templates");
14840           /* If this part of the token stream is rescanned, the same
14841              error message would be generated.  So, we purge the token
14842              from the stream.  */
14843           cp_lexer_purge_token (parser->lexer);
14844           return false;
14845         }
14846       else
14847         {
14848           /* Consume the `template' keyword.  */
14849           cp_lexer_consume_token (parser->lexer);
14850           return true;
14851         }
14852     }
14853
14854   return false;
14855 }
14856
14857 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
14858    set PARSER->SCOPE, and perform other related actions.  */
14859
14860 static void
14861 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14862 {
14863   tree value;
14864   tree check;
14865
14866   /* Get the stored value.  */
14867   value = cp_lexer_consume_token (parser->lexer)->value;
14868   /* Perform any access checks that were deferred.  */
14869   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14870     cp_parser_defer_access_check (parser, 
14871                                   TREE_PURPOSE (check),
14872                                   TREE_VALUE (check));
14873   /* Set the scope from the stored value.  */
14874   parser->scope = TREE_VALUE (value);
14875   parser->qualifying_scope = TREE_TYPE (value);
14876   parser->object_scope = NULL_TREE;
14877 }
14878
14879 /* Add tokens to CACHE until an non-nested END token appears.  */
14880
14881 static void
14882 cp_parser_cache_group (cp_parser *parser, 
14883                        cp_token_cache *cache,
14884                        enum cpp_ttype end,
14885                        unsigned depth)
14886 {
14887   while (true)
14888     {
14889       cp_token *token;
14890
14891       /* Abort a parenthesized expression if we encounter a brace.  */
14892       if ((end == CPP_CLOSE_PAREN || depth == 0)
14893           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14894         return;
14895       /* Consume the next token.  */
14896       token = cp_lexer_consume_token (parser->lexer);
14897       /* If we've reached the end of the file, stop.  */
14898       if (token->type == CPP_EOF)
14899         return;
14900       /* Add this token to the tokens we are saving.  */
14901       cp_token_cache_push_token (cache, token);
14902       /* See if it starts a new group.  */
14903       if (token->type == CPP_OPEN_BRACE)
14904         {
14905           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14906           if (depth == 0)
14907             return;
14908         }
14909       else if (token->type == CPP_OPEN_PAREN)
14910         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14911       else if (token->type == end)
14912         return;
14913     }
14914 }
14915
14916 /* Begin parsing tentatively.  We always save tokens while parsing
14917    tentatively so that if the tentative parsing fails we can restore the
14918    tokens.  */
14919
14920 static void
14921 cp_parser_parse_tentatively (parser)
14922      cp_parser *parser;
14923 {
14924   /* Enter a new parsing context.  */
14925   parser->context = cp_parser_context_new (parser->context);
14926   /* Begin saving tokens.  */
14927   cp_lexer_save_tokens (parser->lexer);
14928   /* In order to avoid repetitive access control error messages,
14929      access checks are queued up until we are no longer parsing
14930      tentatively.  */
14931   cp_parser_start_deferring_access_checks (parser);
14932 }
14933
14934 /* Commit to the currently active tentative parse.  */
14935
14936 static void
14937 cp_parser_commit_to_tentative_parse (parser)
14938      cp_parser *parser;
14939 {
14940   cp_parser_context *context;
14941   cp_lexer *lexer;
14942
14943   /* Mark all of the levels as committed.  */
14944   lexer = parser->lexer;
14945   for (context = parser->context; context->next; context = context->next)
14946     {
14947       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14948         break;
14949       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14950       while (!cp_lexer_saving_tokens (lexer))
14951         lexer = lexer->next;
14952       cp_lexer_commit_tokens (lexer);
14953     }
14954 }
14955
14956 /* Abort the currently active tentative parse.  All consumed tokens
14957    will be rolled back, and no diagnostics will be issued.  */
14958
14959 static void
14960 cp_parser_abort_tentative_parse (parser)
14961      cp_parser *parser;
14962 {
14963   cp_parser_simulate_error (parser);
14964   /* Now, pretend that we want to see if the construct was
14965      successfully parsed.  */
14966   cp_parser_parse_definitely (parser);
14967 }
14968
14969 /* Stop parsing tentatively.  If a parse error has ocurred, restore the
14970    token stream.  Otherwise, commit to the tokens we have consumed.
14971    Returns true if no error occurred; false otherwise.  */
14972
14973 static bool
14974 cp_parser_parse_definitely (parser)
14975      cp_parser *parser;
14976 {
14977   bool error_occurred;
14978   cp_parser_context *context;
14979
14980   /* Remember whether or not an error ocurred, since we are about to
14981      destroy that information.  */
14982   error_occurred = cp_parser_error_occurred (parser);
14983   /* Remove the topmost context from the stack.  */
14984   context = parser->context;
14985   parser->context = context->next;
14986   /* If no parse errors occurred, commit to the tentative parse.  */
14987   if (!error_occurred)
14988     {
14989       /* Commit to the tokens read tentatively, unless that was
14990          already done.  */
14991       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14992         cp_lexer_commit_tokens (parser->lexer);
14993       if (!parser->context->deferring_access_checks_p)
14994         /* If in the parent context we are not deferring checks, then
14995            these perform these checks now.  */
14996         (cp_parser_perform_deferred_access_checks 
14997          (context->deferred_access_checks));
14998       else
14999         /* Any lookups that were deferred during the tentative parse are
15000            still deferred.  */
15001         parser->context->deferred_access_checks 
15002           = chainon (parser->context->deferred_access_checks,
15003                      context->deferred_access_checks);
15004     }
15005   /* Otherwise, if errors occurred, roll back our state so that things
15006      are just as they were before we began the tentative parse.  */
15007   else
15008     cp_lexer_rollback_tokens (parser->lexer);
15009   /* Add the context to the front of the free list.  */
15010   context->next = cp_parser_context_free_list;
15011   cp_parser_context_free_list = context;
15012
15013   return !error_occurred;
15014 }
15015
15016 /* Returns true if we are parsing tentatively -- but have decided that
15017    we will stick with this tentative parse, even if errors occur.  */
15018
15019 static bool
15020 cp_parser_committed_to_tentative_parse (parser)
15021      cp_parser *parser;
15022 {
15023   return (cp_parser_parsing_tentatively (parser)
15024           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15025 }
15026
15027 /* Returns non-zero iff an error has occurred during the most recent
15028    tentative parse.  */
15029    
15030 static bool
15031 cp_parser_error_occurred (parser)
15032      cp_parser *parser;
15033 {
15034   return (cp_parser_parsing_tentatively (parser)
15035           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15036 }
15037
15038 /* Returns non-zero if GNU extensions are allowed.  */
15039
15040 static bool
15041 cp_parser_allow_gnu_extensions_p (parser)
15042      cp_parser *parser;
15043 {
15044   return parser->allow_gnu_extensions_p;
15045 }
15046
15047 \f
15048
15049 /* The parser.  */
15050
15051 static GTY (()) cp_parser *the_parser;
15052
15053 /* External interface.  */
15054
15055 /* Parse the entire translation unit.  */
15056
15057 int
15058 yyparse ()
15059 {
15060   bool error_occurred;
15061
15062   the_parser = cp_parser_new ();
15063   error_occurred = cp_parser_translation_unit (the_parser);
15064   the_parser = NULL;
15065   
15066   finish_file ();
15067
15068   return error_occurred;
15069 }
15070
15071 /* Clean up after parsing the entire translation unit.  */
15072
15073 void
15074 free_parser_stacks ()
15075 {
15076   /* Nothing to do.  */
15077 }
15078
15079 /* This variable must be provided by every front end.  */
15080
15081 int yydebug;
15082
15083 #include "gt-cp-parser.h"