OSDN Git Service

2004-06-26 Richard Kenner <kenner@vlsi1.ultra.nyu.edu>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 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 #include "target.h"
38
39 \f
40 /* The lexer.  */
41
42 /* Overview
43    --------
44
45    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
46    look-ahead.
47
48    Methodology
49    -----------
50
51    We use a circular buffer to store incoming tokens.
52
53    Some artifacts of the C++ language (such as the
54    expression/declaration ambiguity) require arbitrary look-ahead.
55    The strategy we adopt for dealing with these problems is to attempt
56    to parse one construct (e.g., the declaration) and fall back to the
57    other (e.g., the expression) if that attempt does not succeed.
58    Therefore, we must sometimes store an arbitrary number of tokens.
59
60    The parser routinely peeks at the next token, and then consumes it
61    later.  That also requires a buffer in which to store the tokens.
62
63    In order to easily permit adding tokens to the end of the buffer,
64    while removing them from the beginning of the buffer, we use a
65    circular buffer.  */
66
67 /* A C++ token.  */
68
69 typedef struct cp_token GTY (())
70 {
71   /* The kind of token.  */
72   ENUM_BITFIELD (cpp_ttype) type : 8;
73   /* If this token is a keyword, this value indicates which keyword.
74      Otherwise, this value is RID_MAX.  */
75   ENUM_BITFIELD (rid) keyword : 8;
76   /* Token flags.  */
77   unsigned char flags;
78   /* The value associated with this token, if any.  */
79   tree value;
80   /* The location at which this token was found.  */
81   location_t location;
82 } cp_token;
83
84 /* The number of tokens in a single token block.
85    Computed so that cp_token_block fits in a 512B allocation unit.  */
86
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88
89 /* A group of tokens.  These groups are chained together to store
90    large numbers of tokens.  (For example, a token block is created
91    when the body of an inline member function is first encountered;
92    the tokens are processed later after the class definition is
93    complete.)
94
95    This somewhat ungainly data structure (as opposed to, say, a
96    variable-length array), is used due to constraints imposed by the
97    current garbage-collection methodology.  If it is made more
98    flexible, we could perhaps simplify the data structures involved.  */
99
100 typedef struct cp_token_block GTY (())
101 {
102   /* The tokens.  */
103   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104   /* The number of tokens in this block.  */
105   size_t num_tokens;
106   /* The next token block in the chain.  */
107   struct cp_token_block *next;
108   /* The previous block in the chain.  */
109   struct cp_token_block *prev;
110 } cp_token_block;
111
112 typedef struct cp_token_cache GTY (())
113 {
114   /* The first block in the cache.  NULL if there are no tokens in the
115      cache.  */
116   cp_token_block *first;
117   /* The last block in the cache.  NULL If there are no tokens in the
118      cache.  */
119   cp_token_block *last;
120 } cp_token_cache;
121
122 /* Prototypes.  */
123
124 static cp_token_cache *cp_token_cache_new
125   (void);
126 static void cp_token_cache_push_token
127   (cp_token_cache *, cp_token *);
128
129 /* Create a new cp_token_cache.  */
130
131 static cp_token_cache *
132 cp_token_cache_new (void)
133 {
134   return ggc_alloc_cleared (sizeof (cp_token_cache));
135 }
136
137 /* Add *TOKEN to *CACHE.  */
138
139 static void
140 cp_token_cache_push_token (cp_token_cache *cache,
141                            cp_token *token)
142 {
143   cp_token_block *b = cache->last;
144
145   /* See if we need to allocate a new token block.  */
146   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147     {
148       b = ggc_alloc_cleared (sizeof (cp_token_block));
149       b->prev = cache->last;
150       if (cache->last)
151         {
152           cache->last->next = b;
153           cache->last = b;
154         }
155       else
156         cache->first = cache->last = b;
157     }
158   /* Add this token to the current token block.  */
159   b->tokens[b->num_tokens++] = *token;
160 }
161
162 /* The cp_lexer structure represents the C++ lexer.  It is responsible
163    for managing the token stream from the preprocessor and supplying
164    it to the parser.  */
165
166 typedef struct cp_lexer GTY (())
167 {
168   /* The memory allocated for the buffer.  Never NULL.  */
169   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170   /* A pointer just past the end of the memory allocated for the buffer.  */
171   cp_token * GTY ((skip)) buffer_end;
172   /* The first valid token in the buffer, or NULL if none.  */
173   cp_token * GTY ((skip)) first_token;
174   /* The next available token.  If NEXT_TOKEN is NULL, then there are
175      no more available tokens.  */
176   cp_token * GTY ((skip)) next_token;
177   /* A pointer just past the last available token.  If FIRST_TOKEN is
178      NULL, however, there are no available tokens, and then this
179      location is simply the place in which the next token read will be
180      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181      When the LAST_TOKEN == BUFFER, then the last token is at the
182      highest memory address in the BUFFER.  */
183   cp_token * GTY ((skip)) last_token;
184
185   /* A stack indicating positions at which cp_lexer_save_tokens was
186      called.  The top entry is the most recent position at which we
187      began saving tokens.  The entries are differences in token
188      position between FIRST_TOKEN and the first saved token.
189
190      If the stack is non-empty, we are saving tokens.  When a token is
191      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192      pointer will not.  The token stream will be preserved so that it
193      can be reexamined later.
194
195      If the stack is empty, then we are not saving tokens.  Whenever a
196      token is consumed, the FIRST_TOKEN pointer will be moved, and the
197      consumed token will be gone forever.  */
198   varray_type saved_tokens;
199
200   /* The STRING_CST tokens encountered while processing the current
201      string literal.  */
202   varray_type string_tokens;
203
204   /* True if we should obtain more tokens from the preprocessor; false
205      if we are processing a saved token cache.  */
206   bool main_lexer_p;
207
208   /* True if we should output debugging information.  */
209   bool debugging_p;
210
211   /* The next lexer in a linked list of lexers.  */
212   struct cp_lexer *next;
213 } cp_lexer;
214
215 /* Prototypes.  */
216
217 static cp_lexer *cp_lexer_new_main
218   (void);
219 static cp_lexer *cp_lexer_new_from_tokens
220   (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
222   (const cp_lexer *);
223 static cp_token *cp_lexer_next_token
224   (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226   (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference
228   (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
230   (cp_lexer *);
231 static void cp_lexer_maybe_grow_buffer
232   (cp_lexer *);
233 static void cp_lexer_get_preprocessor_token
234   (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
236   (cp_lexer *);
237 static cp_token *cp_lexer_peek_nth_token
238   (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240   (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242   (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244   (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token
246   (cp_lexer *);
247 static void cp_lexer_purge_token
248   (cp_lexer *);
249 static void cp_lexer_purge_tokens_after
250   (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
252   (cp_lexer *);
253 static void cp_lexer_commit_tokens
254   (cp_lexer *);
255 static void cp_lexer_rollback_tokens
256   (cp_lexer *);
257 static inline void cp_lexer_set_source_position_from_token
258   (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260   (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p
262   (cp_lexer *);
263 static void cp_lexer_start_debugging
264   (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266   (cp_lexer *) ATTRIBUTE_UNUSED;
267
268 /* Manifest constants.  */
269
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
272
273 /* A token type for keywords, as opposed to ordinary identifiers.  */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275
276 /* A token type for template-ids.  If a template-id is processed while
277    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278    the value of the CPP_TEMPLATE_ID is whatever was returned by
279    cp_parser_template_id.  */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281
282 /* A token type for nested-name-specifiers.  If a
283    nested-name-specifier is processed while parsing tentatively, it is
284    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286    cp_parser_nested_name_specifier_opt.  */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288
289 /* A token type for tokens that are not tokens at all; these are used
290    to mark the end of a token block.  */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292
293 /* Variables.  */
294
295 /* The stream to which debugging output should be written.  */
296 static FILE *cp_lexer_debug_stream;
297
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
299    preprocessor.  */
300
301 static cp_lexer *
302 cp_lexer_new_main (void)
303 {
304   cp_lexer *lexer;
305   cp_token first_token;
306
307   /* It's possible that lexing the first token will load a PCH file,
308      which is a GC collection point.  So we have to grab the first
309      token before allocating any memory.  */
310   cp_lexer_get_preprocessor_token (NULL, &first_token);
311   c_common_no_more_pch ();
312
313   /* Allocate the memory.  */
314   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315
316   /* Create the circular buffer.  */
317   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319
320   /* There is one token in the buffer.  */
321   lexer->last_token = lexer->buffer + 1;
322   lexer->first_token = lexer->buffer;
323   lexer->next_token = lexer->buffer;
324   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325
326   /* This lexer obtains more tokens by calling c_lex.  */
327   lexer->main_lexer_p = true;
328
329   /* Create the SAVED_TOKENS stack.  */
330   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331
332   /* Create the STRINGS array.  */
333   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334
335   /* Assume we are not debugging.  */
336   lexer->debugging_p = false;
337
338   return lexer;
339 }
340
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342    When these tokens are exhausted, no new tokens will be read.  */
343
344 static cp_lexer *
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
346 {
347   cp_lexer *lexer;
348   cp_token *token;
349   cp_token_block *block;
350   ptrdiff_t num_tokens;
351
352   /* Allocate the memory.  */
353   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354
355   /* Create a new buffer, appropriately sized.  */
356   num_tokens = 0;
357   for (block = tokens->first; block != NULL; block = block->next)
358     num_tokens += block->num_tokens;
359   lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360   lexer->buffer_end = lexer->buffer + num_tokens;
361
362   /* Install the tokens.  */
363   token = lexer->buffer;
364   for (block = tokens->first; block != NULL; block = block->next)
365     {
366       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367       token += block->num_tokens;
368     }
369
370   /* The FIRST_TOKEN is the beginning of the buffer.  */
371   lexer->first_token = lexer->buffer;
372   /* The next available token is also at the beginning of the buffer.  */
373   lexer->next_token = lexer->buffer;
374   /* The buffer is full.  */
375   lexer->last_token = lexer->first_token;
376
377   /* This lexer doesn't obtain more tokens.  */
378   lexer->main_lexer_p = false;
379
380   /* Create the SAVED_TOKENS stack.  */
381   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382
383   /* Create the STRINGS array.  */
384   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385
386   /* Assume we are not debugging.  */
387   lexer->debugging_p = false;
388
389   return lexer;
390 }
391
392 /* Returns nonzero if debugging information should be output.  */
393
394 static inline bool
395 cp_lexer_debugging_p (cp_lexer *lexer)
396 {
397   return lexer->debugging_p;
398 }
399
400 /* Set the current source position from the information stored in
401    TOKEN.  */
402
403 static inline void
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405                                          const cp_token *token)
406 {
407   /* Ideally, the source position information would not be a global
408      variable, but it is.  */
409
410   /* Update the line number.  */
411   if (token->type != CPP_EOF)
412     input_location = token->location;
413 }
414
415 /* TOKEN points into the circular token buffer.  Return a pointer to
416    the next token in the buffer.  */
417
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
420 {
421   token++;
422   if (token == lexer->buffer_end)
423     token = lexer->buffer;
424   return token;
425 }
426
427 /* TOKEN points into the circular token buffer.  Return a pointer to
428    the previous token in the buffer.  */
429
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432 {
433   if (token == lexer->buffer)
434     token = lexer->buffer_end;
435   return token - 1;
436 }
437
438 /* nonzero if we are presently saving tokens.  */
439
440 static int
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
442 {
443   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
444 }
445
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
447    buffer.  */
448
449 static cp_token *
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
451 {
452   token += n;
453   if (token >= lexer->buffer_end)
454     token = lexer->buffer + (token - lexer->buffer_end);
455   return token;
456 }
457
458 /* Returns the number of times that START would have to be incremented
459    to reach FINISH.  If START and FINISH are the same, returns zero.  */
460
461 static ptrdiff_t
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
463 {
464   if (finish >= start)
465     return finish - start;
466   else
467     return ((lexer->buffer_end - lexer->buffer)
468             - (start - finish));
469 }
470
471 /* Obtain another token from the C preprocessor and add it to the
472    token buffer.  Returns the newly read token.  */
473
474 static cp_token *
475 cp_lexer_read_token (cp_lexer* lexer)
476 {
477   cp_token *token;
478
479   /* Make sure there is room in the buffer.  */
480   cp_lexer_maybe_grow_buffer (lexer);
481
482   /* If there weren't any tokens, then this one will be the first.  */
483   if (!lexer->first_token)
484     lexer->first_token = lexer->last_token;
485   /* Similarly, if there were no available tokens, there is one now.  */
486   if (!lexer->next_token)
487     lexer->next_token = lexer->last_token;
488
489   /* Figure out where we're going to store the new token.  */
490   token = lexer->last_token;
491
492   /* Get a new token from the preprocessor.  */
493   cp_lexer_get_preprocessor_token (lexer, token);
494
495   /* Increment LAST_TOKEN.  */
496   lexer->last_token = cp_lexer_next_token (lexer, token);
497
498   /* Strings should have type `const char []'.  Right now, we will
499      have an ARRAY_TYPE that is constant rather than an array of
500      constant elements.
501      FIXME: Make fix_string_type get this right in the first place.  */
502   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503       && flag_const_strings)
504     {
505       if (c_lex_string_translate)
506         {
507           tree value = token->value;
508           tree type;
509
510           /* We might as well go ahead and release the chained
511              translated string such that we can reuse its memory.  */
512           if (TREE_CHAIN (value))
513             value = TREE_CHAIN (token->value);
514
515           /* Get the current type.  It will be an ARRAY_TYPE.  */
516           type = TREE_TYPE (value);
517           /* Use build_cplus_array_type to rebuild the array, thereby
518              getting the right type.  */
519           type = build_cplus_array_type (TREE_TYPE (type),
520                                          TYPE_DOMAIN (type));
521           /* Reset the type of the token.  */
522           TREE_TYPE (value) = type;
523         }
524     }
525
526   return token;
527 }
528
529 /* If the circular buffer is full, make it bigger.  */
530
531 static void
532 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
533 {
534   /* If the buffer is full, enlarge it.  */
535   if (lexer->last_token == lexer->first_token)
536     {
537       cp_token *new_buffer;
538       cp_token *old_buffer;
539       cp_token *new_first_token;
540       ptrdiff_t buffer_length;
541       size_t num_tokens_to_copy;
542
543       /* Remember the current buffer pointer.  It will become invalid,
544          but we will need to do pointer arithmetic involving this
545          value.  */
546       old_buffer = lexer->buffer;
547       /* Compute the current buffer size.  */
548       buffer_length = lexer->buffer_end - lexer->buffer;
549       /* Allocate a buffer twice as big.  */
550       new_buffer = ggc_realloc (lexer->buffer,
551                                 2 * buffer_length * sizeof (cp_token));
552
553       /* Because the buffer is circular, logically consecutive tokens
554          are not necessarily placed consecutively in memory.
555          Therefore, we must keep move the tokens that were before
556          FIRST_TOKEN to the second half of the newly allocated
557          buffer.  */
558       num_tokens_to_copy = (lexer->first_token - old_buffer);
559       memcpy (new_buffer + buffer_length,
560               new_buffer,
561               num_tokens_to_copy * sizeof (cp_token));
562       /* Clear the rest of the buffer.  We never look at this storage,
563          but the garbage collector may.  */
564       memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
565               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
566
567       /* Now recompute all of the buffer pointers.  */
568       new_first_token
569         = new_buffer + (lexer->first_token - old_buffer);
570       if (lexer->next_token != NULL)
571         {
572           ptrdiff_t next_token_delta;
573
574           if (lexer->next_token > lexer->first_token)
575             next_token_delta = lexer->next_token - lexer->first_token;
576           else
577             next_token_delta =
578               buffer_length - (lexer->first_token - lexer->next_token);
579           lexer->next_token = new_first_token + next_token_delta;
580         }
581       lexer->last_token = new_first_token + buffer_length;
582       lexer->buffer = new_buffer;
583       lexer->buffer_end = new_buffer + buffer_length * 2;
584       lexer->first_token = new_first_token;
585     }
586 }
587
588 /* Store the next token from the preprocessor in *TOKEN.  */
589
590 static void
591 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
592                                  cp_token *token)
593 {
594   bool done;
595
596   /* If this not the main lexer, return a terminating CPP_EOF token.  */
597   if (lexer != NULL && !lexer->main_lexer_p)
598     {
599       token->type = CPP_EOF;
600       token->location.line = 0;
601       token->location.file = NULL;
602       token->value = NULL_TREE;
603       token->keyword = RID_MAX;
604
605       return;
606     }
607
608   done = false;
609   /* Keep going until we get a token we like.  */
610   while (!done)
611     {
612       /* Get a new token from the preprocessor.  */
613       token->type = c_lex_with_flags (&token->value, &token->flags);
614       /* Issue messages about tokens we cannot process.  */
615       switch (token->type)
616         {
617         case CPP_ATSIGN:
618         case CPP_HASH:
619         case CPP_PASTE:
620           error ("invalid token");
621           break;
622
623         default:
624           /* This is a good token, so we exit the loop.  */
625           done = true;
626           break;
627         }
628     }
629   /* Now we've got our token.  */
630   token->location = input_location;
631
632   /* Check to see if this token is a keyword.  */
633   if (token->type == CPP_NAME
634       && C_IS_RESERVED_WORD (token->value))
635     {
636       /* Mark this token as a keyword.  */
637       token->type = CPP_KEYWORD;
638       /* Record which keyword.  */
639       token->keyword = C_RID_CODE (token->value);
640       /* Update the value.  Some keywords are mapped to particular
641          entities, rather than simply having the value of the
642          corresponding IDENTIFIER_NODE.  For example, `__const' is
643          mapped to `const'.  */
644       token->value = ridpointers[token->keyword];
645     }
646   else
647     token->keyword = RID_MAX;
648 }
649
650 /* Return a pointer to the next token in the token stream, but do not
651    consume it.  */
652
653 static cp_token *
654 cp_lexer_peek_token (cp_lexer* lexer)
655 {
656   cp_token *token;
657
658   /* If there are no tokens, read one now.  */
659   if (!lexer->next_token)
660     cp_lexer_read_token (lexer);
661
662   /* Provide debugging output.  */
663   if (cp_lexer_debugging_p (lexer))
664     {
665       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
666       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
667       fprintf (cp_lexer_debug_stream, "\n");
668     }
669
670   token = lexer->next_token;
671   cp_lexer_set_source_position_from_token (lexer, token);
672   return token;
673 }
674
675 /* Return true if the next token has the indicated TYPE.  */
676
677 static bool
678 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
679 {
680   cp_token *token;
681
682   /* Peek at the next token.  */
683   token = cp_lexer_peek_token (lexer);
684   /* Check to see if it has the indicated TYPE.  */
685   return token->type == type;
686 }
687
688 /* Return true if the next token does not have the indicated TYPE.  */
689
690 static bool
691 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
692 {
693   return !cp_lexer_next_token_is (lexer, type);
694 }
695
696 /* Return true if the next token is the indicated KEYWORD.  */
697
698 static bool
699 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
700 {
701   cp_token *token;
702
703   /* Peek at the next token.  */
704   token = cp_lexer_peek_token (lexer);
705   /* Check to see if it is the indicated keyword.  */
706   return token->keyword == keyword;
707 }
708
709 /* Return a pointer to the Nth token in the token stream.  If N is 1,
710    then this is precisely equivalent to cp_lexer_peek_token.  */
711
712 static cp_token *
713 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
714 {
715   cp_token *token;
716
717   /* N is 1-based, not zero-based.  */
718   my_friendly_assert (n > 0, 20000224);
719
720   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
721   token = lexer->next_token;
722   /* If there are no tokens in the buffer, get one now.  */
723   if (!token)
724     {
725       cp_lexer_read_token (lexer);
726       token = lexer->next_token;
727     }
728
729   /* Now, read tokens until we have enough.  */
730   while (--n > 0)
731     {
732       /* Advance to the next token.  */
733       token = cp_lexer_next_token (lexer, token);
734       /* If that's all the tokens we have, read a new one.  */
735       if (token == lexer->last_token)
736         token = cp_lexer_read_token (lexer);
737     }
738
739   return token;
740 }
741
742 /* Consume the next token.  The pointer returned is valid only until
743    another token is read.  Callers should preserve copy the token
744    explicitly if they will need its value for a longer period of
745    time.  */
746
747 static cp_token *
748 cp_lexer_consume_token (cp_lexer* lexer)
749 {
750   cp_token *token;
751
752   /* If there are no tokens, read one now.  */
753   if (!lexer->next_token)
754     cp_lexer_read_token (lexer);
755
756   /* Remember the token we'll be returning.  */
757   token = lexer->next_token;
758
759   /* Increment NEXT_TOKEN.  */
760   lexer->next_token = cp_lexer_next_token (lexer,
761                                            lexer->next_token);
762   /* Check to see if we're all out of tokens.  */
763   if (lexer->next_token == lexer->last_token)
764     lexer->next_token = NULL;
765
766   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
767   if (!cp_lexer_saving_tokens (lexer))
768     {
769       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
770       if (!lexer->next_token)
771         lexer->first_token = NULL;
772       else
773         lexer->first_token = lexer->next_token;
774     }
775
776   /* Provide debugging output.  */
777   if (cp_lexer_debugging_p (lexer))
778     {
779       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
780       cp_lexer_print_token (cp_lexer_debug_stream, token);
781       fprintf (cp_lexer_debug_stream, "\n");
782     }
783
784   return token;
785 }
786
787 /* Permanently remove the next token from the token stream.  There
788    must be a valid next token already; this token never reads
789    additional tokens from the preprocessor.  */
790
791 static void
792 cp_lexer_purge_token (cp_lexer *lexer)
793 {
794   cp_token *token;
795   cp_token *next_token;
796
797   token = lexer->next_token;
798   while (true)
799     {
800       next_token = cp_lexer_next_token (lexer, token);
801       if (next_token == lexer->last_token)
802         break;
803       *token = *next_token;
804       token = next_token;
805     }
806
807   lexer->last_token = token;
808   /* The token purged may have been the only token remaining; if so,
809      clear NEXT_TOKEN.  */
810   if (lexer->next_token == token)
811     lexer->next_token = NULL;
812 }
813
814 /* Permanently remove all tokens after TOKEN, up to, but not
815    including, the token that will be returned next by
816    cp_lexer_peek_token.  */
817
818 static void
819 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
820 {
821   cp_token *peek;
822   cp_token *t1;
823   cp_token *t2;
824
825   if (lexer->next_token)
826     {
827       /* Copy the tokens that have not yet been read to the location
828          immediately following TOKEN.  */
829       t1 = cp_lexer_next_token (lexer, token);
830       t2 = peek = cp_lexer_peek_token (lexer);
831       /* Move tokens into the vacant area between TOKEN and PEEK.  */
832       while (t2 != lexer->last_token)
833         {
834           *t1 = *t2;
835           t1 = cp_lexer_next_token (lexer, t1);
836           t2 = cp_lexer_next_token (lexer, t2);
837         }
838       /* Now, the next available token is right after TOKEN.  */
839       lexer->next_token = cp_lexer_next_token (lexer, token);
840       /* And the last token is wherever we ended up.  */
841       lexer->last_token = t1;
842     }
843   else
844     {
845       /* There are no tokens in the buffer, so there is nothing to
846          copy.  The last token in the buffer is TOKEN itself.  */
847       lexer->last_token = cp_lexer_next_token (lexer, token);
848     }
849 }
850
851 /* Begin saving tokens.  All tokens consumed after this point will be
852    preserved.  */
853
854 static void
855 cp_lexer_save_tokens (cp_lexer* lexer)
856 {
857   /* Provide debugging output.  */
858   if (cp_lexer_debugging_p (lexer))
859     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
860
861   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
862      restore the tokens if required.  */
863   if (!lexer->next_token)
864     cp_lexer_read_token (lexer);
865
866   VARRAY_PUSH_INT (lexer->saved_tokens,
867                    cp_lexer_token_difference (lexer,
868                                               lexer->first_token,
869                                               lexer->next_token));
870 }
871
872 /* Commit to the portion of the token stream most recently saved.  */
873
874 static void
875 cp_lexer_commit_tokens (cp_lexer* lexer)
876 {
877   /* Provide debugging output.  */
878   if (cp_lexer_debugging_p (lexer))
879     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
880
881   VARRAY_POP (lexer->saved_tokens);
882 }
883
884 /* Return all tokens saved since the last call to cp_lexer_save_tokens
885    to the token stream.  Stop saving tokens.  */
886
887 static void
888 cp_lexer_rollback_tokens (cp_lexer* lexer)
889 {
890   size_t delta;
891
892   /* Provide debugging output.  */
893   if (cp_lexer_debugging_p (lexer))
894     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
895
896   /* Find the token that was the NEXT_TOKEN when we started saving
897      tokens.  */
898   delta = VARRAY_TOP_INT(lexer->saved_tokens);
899   /* Make it the next token again now.  */
900   lexer->next_token = cp_lexer_advance_token (lexer,
901                                               lexer->first_token,
902                                               delta);
903   /* It might be the case that there were no tokens when we started
904      saving tokens, but that there are some tokens now.  */
905   if (!lexer->next_token && lexer->first_token)
906     lexer->next_token = lexer->first_token;
907
908   /* Stop saving tokens.  */
909   VARRAY_POP (lexer->saved_tokens);
910 }
911
912 /* Print a representation of the TOKEN on the STREAM.  */
913
914 static void
915 cp_lexer_print_token (FILE * stream, cp_token* token)
916 {
917   const char *token_type = NULL;
918
919   /* Figure out what kind of token this is.  */
920   switch (token->type)
921     {
922     case CPP_EQ:
923       token_type = "EQ";
924       break;
925
926     case CPP_COMMA:
927       token_type = "COMMA";
928       break;
929
930     case CPP_OPEN_PAREN:
931       token_type = "OPEN_PAREN";
932       break;
933
934     case CPP_CLOSE_PAREN:
935       token_type = "CLOSE_PAREN";
936       break;
937
938     case CPP_OPEN_BRACE:
939       token_type = "OPEN_BRACE";
940       break;
941
942     case CPP_CLOSE_BRACE:
943       token_type = "CLOSE_BRACE";
944       break;
945
946     case CPP_SEMICOLON:
947       token_type = "SEMICOLON";
948       break;
949
950     case CPP_NAME:
951       token_type = "NAME";
952       break;
953
954     case CPP_EOF:
955       token_type = "EOF";
956       break;
957
958     case CPP_KEYWORD:
959       token_type = "keyword";
960       break;
961
962       /* This is not a token that we know how to handle yet.  */
963     default:
964       break;
965     }
966
967   /* If we have a name for the token, print it out.  Otherwise, we
968      simply give the numeric code.  */
969   if (token_type)
970     fprintf (stream, "%s", token_type);
971   else
972     fprintf (stream, "%d", token->type);
973   /* And, for an identifier, print the identifier name.  */
974   if (token->type == CPP_NAME
975       /* Some keywords have a value that is not an IDENTIFIER_NODE.
976          For example, `struct' is mapped to an INTEGER_CST.  */
977       || (token->type == CPP_KEYWORD
978           && TREE_CODE (token->value) == IDENTIFIER_NODE))
979     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
980 }
981
982 /* Start emitting debugging information.  */
983
984 static void
985 cp_lexer_start_debugging (cp_lexer* lexer)
986 {
987   ++lexer->debugging_p;
988 }
989
990 /* Stop emitting debugging information.  */
991
992 static void
993 cp_lexer_stop_debugging (cp_lexer* lexer)
994 {
995   --lexer->debugging_p;
996 }
997
998 \f
999 /* Decl-specifiers.  */
1000
1001 static void clear_decl_specs
1002   (cp_decl_specifier_seq *);
1003
1004 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1005
1006 static void
1007 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1008 {
1009   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1010 }
1011
1012 /* Declarators.  */
1013
1014 /* Nothing other than the parser should be creating declarators;
1015    declarators are a semi-syntactic representation of C++ entities.
1016    Other parts of the front end that need to create entities (like
1017    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1018
1019 static cp_declarator *make_id_declarator 
1020   (tree);
1021 static cp_declarator *make_call_declarator      
1022   (cp_declarator *, cp_parameter_declarator *, tree, tree);
1023 static cp_declarator *make_array_declarator     
1024   (cp_declarator *, tree);
1025 static cp_declarator *make_pointer_declarator   
1026   (tree, cp_declarator *);
1027 static cp_declarator *make_reference_declarator 
1028   (tree, cp_declarator *);
1029 static cp_parameter_declarator *make_parameter_declarator 
1030   (cp_decl_specifier_seq *, cp_declarator *, tree);
1031 static cp_declarator *make_ptrmem_declarator    
1032   (tree, tree, cp_declarator *);
1033
1034 cp_declarator *cp_error_declarator;
1035
1036 /* The obstack on which declarators and related data structures are
1037    allocated.  */
1038 static struct obstack declarator_obstack;
1039
1040 /* Alloc BYTES from the declarator memory pool.  */
1041
1042 static inline void *
1043 alloc_declarator (size_t bytes)
1044 {
1045   return obstack_alloc (&declarator_obstack, bytes);
1046 }
1047
1048 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1049    common to all declarators.  */
1050
1051 static cp_declarator *
1052 make_declarator (cp_declarator_kind kind)
1053 {
1054   cp_declarator *declarator;
1055
1056   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1057   declarator->kind = kind;
1058   declarator->attributes = NULL_TREE;
1059   declarator->declarator = NULL;
1060
1061   return declarator;
1062 }
1063
1064 /* Make a declarator for a generalized identifier.  */
1065
1066 cp_declarator *
1067 make_id_declarator (tree id)
1068 {
1069   cp_declarator *declarator;
1070   
1071   declarator = make_declarator (cdk_id);
1072   declarator->u.id.name = id;
1073   declarator->u.id.sfk = sfk_none;
1074
1075   return declarator;
1076 }
1077
1078 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1079    of modifiers such as const or volatile to apply to the pointer
1080    type, represented as identifiers.  */
1081
1082 cp_declarator *
1083 make_pointer_declarator (tree cv_qualifiers, cp_declarator *target)
1084 {
1085   cp_declarator *declarator;
1086
1087   declarator = make_declarator (cdk_pointer);
1088   declarator->declarator = target;
1089   declarator->u.pointer.qualifiers = cv_qualifiers;
1090   declarator->u.pointer.class_type = NULL_TREE;
1091
1092   return declarator;
1093 }
1094
1095 /* Like make_pointer_declarator -- but for references.  */
1096
1097 cp_declarator *
1098 make_reference_declarator (tree cv_qualifiers, cp_declarator *target)
1099 {
1100   cp_declarator *declarator;
1101
1102   declarator = make_declarator (cdk_reference);
1103   declarator->declarator = target;
1104   declarator->u.pointer.qualifiers = cv_qualifiers;
1105   declarator->u.pointer.class_type = NULL_TREE;
1106
1107   return declarator;
1108 }
1109
1110 /* Like make_pointer_declarator -- but for a pointer to a non-static
1111    member of CLASS_TYPE.  */
1112
1113 cp_declarator *
1114 make_ptrmem_declarator (tree cv_qualifiers, tree class_type,
1115                         cp_declarator *pointee)
1116 {
1117   cp_declarator *declarator;
1118
1119   declarator = make_declarator (cdk_ptrmem);
1120   declarator->declarator = pointee;
1121   declarator->u.pointer.qualifiers = cv_qualifiers;
1122   declarator->u.pointer.class_type = class_type;
1123
1124   return declarator;
1125 }
1126
1127 /* Make a declarator for the function given by TARGET, with the
1128    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1129    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1130    indicates what exceptions can be thrown.  */
1131
1132 cp_declarator *
1133 make_call_declarator (cp_declarator *target, 
1134                       cp_parameter_declarator *parms,
1135                       tree cv_qualifiers, 
1136                       tree exception_specification)
1137 {
1138   cp_declarator *declarator;
1139
1140   declarator = make_declarator (cdk_function);
1141   declarator->declarator = target;
1142   declarator->u.function.parameters = parms;
1143   declarator->u.function.qualifiers = cv_qualifiers;
1144   declarator->u.function.exception_specification = exception_specification;
1145
1146   return declarator;
1147 }
1148
1149 /* Make a declarator for an array of BOUNDS elements, each of which is
1150    defined by ELEMENT.  */
1151
1152 cp_declarator *
1153 make_array_declarator (cp_declarator *element, tree bounds)
1154 {
1155   cp_declarator *declarator;
1156
1157   declarator = make_declarator (cdk_array);
1158   declarator->declarator = element;
1159   declarator->u.array.bounds = bounds;
1160
1161   return declarator;
1162 }
1163
1164 cp_parameter_declarator *no_parameters;
1165
1166 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1167    DECLARATOR and DEFAULT_ARGUMENT.  */
1168
1169 cp_parameter_declarator *
1170 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers, 
1171                            cp_declarator *declarator,
1172                            tree default_argument)
1173 {
1174   cp_parameter_declarator *parameter;
1175
1176   parameter = ((cp_parameter_declarator *) 
1177                alloc_declarator (sizeof (cp_parameter_declarator)));
1178   parameter->next = NULL;
1179   if (decl_specifiers)
1180     parameter->decl_specifiers = *decl_specifiers;
1181   else
1182     clear_decl_specs (&parameter->decl_specifiers);
1183   parameter->declarator = declarator;
1184   parameter->default_argument = default_argument;
1185   parameter->ellipsis_p = false;
1186
1187   return parameter;
1188 }
1189
1190 /* The parser.  */
1191
1192 /* Overview
1193    --------
1194
1195    A cp_parser parses the token stream as specified by the C++
1196    grammar.  Its job is purely parsing, not semantic analysis.  For
1197    example, the parser breaks the token stream into declarators,
1198    expressions, statements, and other similar syntactic constructs.
1199    It does not check that the types of the expressions on either side
1200    of an assignment-statement are compatible, or that a function is
1201    not declared with a parameter of type `void'.
1202
1203    The parser invokes routines elsewhere in the compiler to perform
1204    semantic analysis and to build up the abstract syntax tree for the
1205    code processed.
1206
1207    The parser (and the template instantiation code, which is, in a
1208    way, a close relative of parsing) are the only parts of the
1209    compiler that should be calling push_scope and pop_scope, or
1210    related functions.  The parser (and template instantiation code)
1211    keeps track of what scope is presently active; everything else
1212    should simply honor that.  (The code that generates static
1213    initializers may also need to set the scope, in order to check
1214    access control correctly when emitting the initializers.)
1215
1216    Methodology
1217    -----------
1218
1219    The parser is of the standard recursive-descent variety.  Upcoming
1220    tokens in the token stream are examined in order to determine which
1221    production to use when parsing a non-terminal.  Some C++ constructs
1222    require arbitrary look ahead to disambiguate.  For example, it is
1223    impossible, in the general case, to tell whether a statement is an
1224    expression or declaration without scanning the entire statement.
1225    Therefore, the parser is capable of "parsing tentatively."  When the
1226    parser is not sure what construct comes next, it enters this mode.
1227    Then, while we attempt to parse the construct, the parser queues up
1228    error messages, rather than issuing them immediately, and saves the
1229    tokens it consumes.  If the construct is parsed successfully, the
1230    parser "commits", i.e., it issues any queued error messages and
1231    the tokens that were being preserved are permanently discarded.
1232    If, however, the construct is not parsed successfully, the parser
1233    rolls back its state completely so that it can resume parsing using
1234    a different alternative.
1235
1236    Future Improvements
1237    -------------------
1238
1239    The performance of the parser could probably be improved
1240    substantially.  Some possible improvements include:
1241
1242      - The expression parser recurses through the various levels of
1243        precedence as specified in the grammar, rather than using an
1244        operator-precedence technique.  Therefore, parsing a simple
1245        identifier requires multiple recursive calls.
1246
1247      - We could often eliminate the need to parse tentatively by
1248        looking ahead a little bit.  In some places, this approach
1249        might not entirely eliminate the need to parse tentatively, but
1250        it might still speed up the average case.  */
1251
1252 /* Flags that are passed to some parsing functions.  These values can
1253    be bitwise-ored together.  */
1254
1255 typedef enum cp_parser_flags
1256 {
1257   /* No flags.  */
1258   CP_PARSER_FLAGS_NONE = 0x0,
1259   /* The construct is optional.  If it is not present, then no error
1260      should be issued.  */
1261   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1262   /* When parsing a type-specifier, do not allow user-defined types.  */
1263   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1264 } cp_parser_flags;
1265
1266 /* The different kinds of declarators we want to parse.  */
1267
1268 typedef enum cp_parser_declarator_kind
1269 {
1270   /* We want an abstract declarator.  */
1271   CP_PARSER_DECLARATOR_ABSTRACT,
1272   /* We want a named declarator.  */
1273   CP_PARSER_DECLARATOR_NAMED,
1274   /* We don't mind, but the name must be an unqualified-id.  */
1275   CP_PARSER_DECLARATOR_EITHER
1276 } cp_parser_declarator_kind;
1277
1278 /* A mapping from a token type to a corresponding tree node type.  */
1279
1280 typedef struct cp_parser_token_tree_map_node
1281 {
1282   /* The token type.  */
1283   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1284   /* The corresponding tree code.  */
1285   ENUM_BITFIELD (tree_code) tree_type : 8;
1286 } cp_parser_token_tree_map_node;
1287
1288 /* A complete map consists of several ordinary entries, followed by a
1289    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1290
1291 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1292
1293 /* The status of a tentative parse.  */
1294
1295 typedef enum cp_parser_status_kind
1296 {
1297   /* No errors have occurred.  */
1298   CP_PARSER_STATUS_KIND_NO_ERROR,
1299   /* An error has occurred.  */
1300   CP_PARSER_STATUS_KIND_ERROR,
1301   /* We are committed to this tentative parse, whether or not an error
1302      has occurred.  */
1303   CP_PARSER_STATUS_KIND_COMMITTED
1304 } cp_parser_status_kind;
1305
1306 /* Context that is saved and restored when parsing tentatively.  */
1307
1308 typedef struct cp_parser_context GTY (())
1309 {
1310   /* If this is a tentative parsing context, the status of the
1311      tentative parse.  */
1312   enum cp_parser_status_kind status;
1313   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1314      that are looked up in this context must be looked up both in the
1315      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1316      the context of the containing expression.  */
1317   tree object_type;
1318   /* The next parsing context in the stack.  */
1319   struct cp_parser_context *next;
1320 } cp_parser_context;
1321
1322 /* Prototypes.  */
1323
1324 /* Constructors and destructors.  */
1325
1326 static cp_parser_context *cp_parser_context_new
1327   (cp_parser_context *);
1328
1329 /* Class variables.  */
1330
1331 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1332
1333 /* Constructors and destructors.  */
1334
1335 /* Construct a new context.  The context below this one on the stack
1336    is given by NEXT.  */
1337
1338 static cp_parser_context *
1339 cp_parser_context_new (cp_parser_context* next)
1340 {
1341   cp_parser_context *context;
1342
1343   /* Allocate the storage.  */
1344   if (cp_parser_context_free_list != NULL)
1345     {
1346       /* Pull the first entry from the free list.  */
1347       context = cp_parser_context_free_list;
1348       cp_parser_context_free_list = context->next;
1349       memset (context, 0, sizeof (*context));
1350     }
1351   else
1352     context = ggc_alloc_cleared (sizeof (cp_parser_context));
1353   /* No errors have occurred yet in this context.  */
1354   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1355   /* If this is not the bottomost context, copy information that we
1356      need from the previous context.  */
1357   if (next)
1358     {
1359       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1360          expression, then we are parsing one in this context, too.  */
1361       context->object_type = next->object_type;
1362       /* Thread the stack.  */
1363       context->next = next;
1364     }
1365
1366   return context;
1367 }
1368
1369 /* The cp_parser structure represents the C++ parser.  */
1370
1371 typedef struct cp_parser GTY(())
1372 {
1373   /* The lexer from which we are obtaining tokens.  */
1374   cp_lexer *lexer;
1375
1376   /* The scope in which names should be looked up.  If NULL_TREE, then
1377      we look up names in the scope that is currently open in the
1378      source program.  If non-NULL, this is either a TYPE or
1379      NAMESPACE_DECL for the scope in which we should look.
1380
1381      This value is not cleared automatically after a name is looked
1382      up, so we must be careful to clear it before starting a new look
1383      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1384      will look up `Z' in the scope of `X', rather than the current
1385      scope.)  Unfortunately, it is difficult to tell when name lookup
1386      is complete, because we sometimes peek at a token, look it up,
1387      and then decide not to consume it.  */
1388   tree scope;
1389
1390   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1391      last lookup took place.  OBJECT_SCOPE is used if an expression
1392      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1393      respectively.  QUALIFYING_SCOPE is used for an expression of the
1394      form "X::Y"; it refers to X.  */
1395   tree object_scope;
1396   tree qualifying_scope;
1397
1398   /* A stack of parsing contexts.  All but the bottom entry on the
1399      stack will be tentative contexts.
1400
1401      We parse tentatively in order to determine which construct is in
1402      use in some situations.  For example, in order to determine
1403      whether a statement is an expression-statement or a
1404      declaration-statement we parse it tentatively as a
1405      declaration-statement.  If that fails, we then reparse the same
1406      token stream as an expression-statement.  */
1407   cp_parser_context *context;
1408
1409   /* True if we are parsing GNU C++.  If this flag is not set, then
1410      GNU extensions are not recognized.  */
1411   bool allow_gnu_extensions_p;
1412
1413   /* TRUE if the `>' token should be interpreted as the greater-than
1414      operator.  FALSE if it is the end of a template-id or
1415      template-parameter-list.  */
1416   bool greater_than_is_operator_p;
1417
1418   /* TRUE if default arguments are allowed within a parameter list
1419      that starts at this point. FALSE if only a gnu extension makes
1420      them permissible.  */
1421   bool default_arg_ok_p;
1422
1423   /* TRUE if we are parsing an integral constant-expression.  See
1424      [expr.const] for a precise definition.  */
1425   bool integral_constant_expression_p;
1426
1427   /* TRUE if we are parsing an integral constant-expression -- but a
1428      non-constant expression should be permitted as well.  This flag
1429      is used when parsing an array bound so that GNU variable-length
1430      arrays are tolerated.  */
1431   bool allow_non_integral_constant_expression_p;
1432
1433   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1434      been seen that makes the expression non-constant.  */
1435   bool non_integral_constant_expression_p;
1436
1437   /* TRUE if local variable names and `this' are forbidden in the
1438      current context.  */
1439   bool local_variables_forbidden_p;
1440
1441   /* TRUE if the declaration we are parsing is part of a
1442      linkage-specification of the form `extern string-literal
1443      declaration'.  */
1444   bool in_unbraced_linkage_specification_p;
1445
1446   /* TRUE if we are presently parsing a declarator, after the
1447      direct-declarator.  */
1448   bool in_declarator_p;
1449
1450   /* TRUE if we are presently parsing a template-argument-list.  */
1451   bool in_template_argument_list_p;
1452
1453   /* TRUE if we are presently parsing the body of an
1454      iteration-statement.  */
1455   bool in_iteration_statement_p;
1456
1457   /* TRUE if we are presently parsing the body of a switch
1458      statement.  */
1459   bool in_switch_statement_p;
1460
1461   /* TRUE if we are parsing a type-id in an expression context.  In
1462      such a situation, both "type (expr)" and "type (type)" are valid
1463      alternatives.  */
1464   bool in_type_id_in_expr_p;
1465
1466   /* If non-NULL, then we are parsing a construct where new type
1467      definitions are not permitted.  The string stored here will be
1468      issued as an error message if a type is defined.  */
1469   const char *type_definition_forbidden_message;
1470
1471   /* A list of lists. The outer list is a stack, used for member
1472      functions of local classes. At each level there are two sub-list,
1473      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1474      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1475      TREE_VALUE's. The functions are chained in reverse declaration
1476      order.
1477
1478      The TREE_PURPOSE sublist contains those functions with default
1479      arguments that need post processing, and the TREE_VALUE sublist
1480      contains those functions with definitions that need post
1481      processing.
1482
1483      These lists can only be processed once the outermost class being
1484      defined is complete.  */
1485   tree unparsed_functions_queues;
1486
1487   /* The number of classes whose definitions are currently in
1488      progress.  */
1489   unsigned num_classes_being_defined;
1490
1491   /* The number of template parameter lists that apply directly to the
1492      current declaration.  */
1493   unsigned num_template_parameter_lists;
1494 } cp_parser;
1495
1496 /* The type of a function that parses some kind of expression.  */
1497 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1498
1499 /* Prototypes.  */
1500
1501 /* Constructors and destructors.  */
1502
1503 static cp_parser *cp_parser_new
1504   (void);
1505
1506 /* Routines to parse various constructs.
1507
1508    Those that return `tree' will return the error_mark_node (rather
1509    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1510    Sometimes, they will return an ordinary node if error-recovery was
1511    attempted, even though a parse error occurred.  So, to check
1512    whether or not a parse error occurred, you should always use
1513    cp_parser_error_occurred.  If the construct is optional (indicated
1514    either by an `_opt' in the name of the function that does the
1515    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1516    the construct is not present.  */
1517
1518 /* Lexical conventions [gram.lex]  */
1519
1520 static tree cp_parser_identifier
1521   (cp_parser *);
1522
1523 /* Basic concepts [gram.basic]  */
1524
1525 static bool cp_parser_translation_unit
1526   (cp_parser *);
1527
1528 /* Expressions [gram.expr]  */
1529
1530 static tree cp_parser_primary_expression
1531   (cp_parser *, cp_id_kind *, tree *);
1532 static tree cp_parser_id_expression
1533   (cp_parser *, bool, bool, bool *, bool);
1534 static tree cp_parser_unqualified_id
1535   (cp_parser *, bool, bool, bool);
1536 static tree cp_parser_nested_name_specifier_opt
1537   (cp_parser *, bool, bool, bool, bool);
1538 static tree cp_parser_nested_name_specifier
1539   (cp_parser *, bool, bool, bool, bool);
1540 static tree cp_parser_class_or_namespace_name
1541   (cp_parser *, bool, bool, bool, bool, bool);
1542 static tree cp_parser_postfix_expression
1543   (cp_parser *, bool);
1544 static tree cp_parser_postfix_open_square_expression
1545   (cp_parser *, tree, bool);
1546 static tree cp_parser_postfix_dot_deref_expression
1547   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1548 static tree cp_parser_parenthesized_expression_list
1549   (cp_parser *, bool, bool *);
1550 static void cp_parser_pseudo_destructor_name
1551   (cp_parser *, tree *, tree *);
1552 static tree cp_parser_unary_expression
1553   (cp_parser *, bool);
1554 static enum tree_code cp_parser_unary_operator
1555   (cp_token *);
1556 static tree cp_parser_new_expression
1557   (cp_parser *);
1558 static tree cp_parser_new_placement
1559   (cp_parser *);
1560 static tree cp_parser_new_type_id
1561   (cp_parser *, tree *);
1562 static cp_declarator *cp_parser_new_declarator_opt
1563   (cp_parser *);
1564 static cp_declarator *cp_parser_direct_new_declarator
1565   (cp_parser *);
1566 static tree cp_parser_new_initializer
1567   (cp_parser *);
1568 static tree cp_parser_delete_expression
1569   (cp_parser *);
1570 static tree cp_parser_cast_expression
1571   (cp_parser *, bool);
1572 static tree cp_parser_pm_expression
1573   (cp_parser *);
1574 static tree cp_parser_multiplicative_expression
1575   (cp_parser *);
1576 static tree cp_parser_additive_expression
1577   (cp_parser *);
1578 static tree cp_parser_shift_expression
1579   (cp_parser *);
1580 static tree cp_parser_relational_expression
1581   (cp_parser *);
1582 static tree cp_parser_equality_expression
1583   (cp_parser *);
1584 static tree cp_parser_and_expression
1585   (cp_parser *);
1586 static tree cp_parser_exclusive_or_expression
1587   (cp_parser *);
1588 static tree cp_parser_inclusive_or_expression
1589   (cp_parser *);
1590 static tree cp_parser_logical_and_expression
1591   (cp_parser *);
1592 static tree cp_parser_logical_or_expression
1593   (cp_parser *);
1594 static tree cp_parser_question_colon_clause
1595   (cp_parser *, tree);
1596 static tree cp_parser_assignment_expression
1597   (cp_parser *);
1598 static enum tree_code cp_parser_assignment_operator_opt
1599   (cp_parser *);
1600 static tree cp_parser_expression
1601   (cp_parser *);
1602 static tree cp_parser_constant_expression
1603   (cp_parser *, bool, bool *);
1604 static tree cp_parser_builtin_offsetof
1605   (cp_parser *);
1606
1607 /* Statements [gram.stmt.stmt]  */
1608
1609 static void cp_parser_statement
1610   (cp_parser *, tree);
1611 static tree cp_parser_labeled_statement
1612   (cp_parser *, tree);
1613 static tree cp_parser_expression_statement
1614   (cp_parser *, tree);
1615 static tree cp_parser_compound_statement
1616   (cp_parser *, tree, bool);
1617 static void cp_parser_statement_seq_opt
1618   (cp_parser *, tree);
1619 static tree cp_parser_selection_statement
1620   (cp_parser *);
1621 static tree cp_parser_condition
1622   (cp_parser *);
1623 static tree cp_parser_iteration_statement
1624   (cp_parser *);
1625 static void cp_parser_for_init_statement
1626   (cp_parser *);
1627 static tree cp_parser_jump_statement
1628   (cp_parser *);
1629 static void cp_parser_declaration_statement
1630   (cp_parser *);
1631
1632 static tree cp_parser_implicitly_scoped_statement
1633   (cp_parser *);
1634 static void cp_parser_already_scoped_statement
1635   (cp_parser *);
1636
1637 /* Declarations [gram.dcl.dcl] */
1638
1639 static void cp_parser_declaration_seq_opt
1640   (cp_parser *);
1641 static void cp_parser_declaration
1642   (cp_parser *);
1643 static void cp_parser_block_declaration
1644   (cp_parser *, bool);
1645 static void cp_parser_simple_declaration
1646   (cp_parser *, bool);
1647 static void cp_parser_decl_specifier_seq
1648   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1649 static tree cp_parser_storage_class_specifier_opt
1650   (cp_parser *);
1651 static tree cp_parser_function_specifier_opt
1652   (cp_parser *, cp_decl_specifier_seq *);
1653 static tree cp_parser_type_specifier
1654   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool, 
1655    int *, bool *);
1656 static tree cp_parser_simple_type_specifier
1657   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1658 static tree cp_parser_type_name
1659   (cp_parser *);
1660 static tree cp_parser_elaborated_type_specifier
1661   (cp_parser *, bool, bool);
1662 static tree cp_parser_enum_specifier
1663   (cp_parser *);
1664 static void cp_parser_enumerator_list
1665   (cp_parser *, tree);
1666 static void cp_parser_enumerator_definition
1667   (cp_parser *, tree);
1668 static tree cp_parser_namespace_name
1669   (cp_parser *);
1670 static void cp_parser_namespace_definition
1671   (cp_parser *);
1672 static void cp_parser_namespace_body
1673   (cp_parser *);
1674 static tree cp_parser_qualified_namespace_specifier
1675   (cp_parser *);
1676 static void cp_parser_namespace_alias_definition
1677   (cp_parser *);
1678 static void cp_parser_using_declaration
1679   (cp_parser *);
1680 static void cp_parser_using_directive
1681   (cp_parser *);
1682 static void cp_parser_asm_definition
1683   (cp_parser *);
1684 static void cp_parser_linkage_specification
1685   (cp_parser *);
1686
1687 /* Declarators [gram.dcl.decl] */
1688
1689 static tree cp_parser_init_declarator
1690   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1691 static cp_declarator *cp_parser_declarator
1692   (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1693 static cp_declarator *cp_parser_direct_declarator
1694   (cp_parser *, cp_parser_declarator_kind, int *);
1695 static enum tree_code cp_parser_ptr_operator
1696   (cp_parser *, tree *, tree *);
1697 static tree cp_parser_cv_qualifier_seq_opt
1698   (cp_parser *);
1699 static tree cp_parser_cv_qualifier_opt
1700   (cp_parser *);
1701 static tree cp_parser_declarator_id
1702   (cp_parser *);
1703 static tree cp_parser_type_id
1704   (cp_parser *);
1705 static void cp_parser_type_specifier_seq
1706   (cp_parser *, cp_decl_specifier_seq *);
1707 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1708   (cp_parser *);
1709 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1710   (cp_parser *, bool *);
1711 static cp_parameter_declarator *cp_parser_parameter_declaration
1712   (cp_parser *, bool, bool *);
1713 static void cp_parser_function_body
1714   (cp_parser *);
1715 static tree cp_parser_initializer
1716   (cp_parser *, bool *, bool *);
1717 static tree cp_parser_initializer_clause
1718   (cp_parser *, bool *);
1719 static tree cp_parser_initializer_list
1720   (cp_parser *, bool *);
1721
1722 static bool cp_parser_ctor_initializer_opt_and_function_body
1723   (cp_parser *);
1724
1725 /* Classes [gram.class] */
1726
1727 static tree cp_parser_class_name
1728   (cp_parser *, bool, bool, bool, bool, bool, bool);
1729 static tree cp_parser_class_specifier
1730   (cp_parser *);
1731 static tree cp_parser_class_head
1732   (cp_parser *, bool *, tree *);
1733 static enum tag_types cp_parser_class_key
1734   (cp_parser *);
1735 static void cp_parser_member_specification_opt
1736   (cp_parser *);
1737 static void cp_parser_member_declaration
1738   (cp_parser *);
1739 static tree cp_parser_pure_specifier
1740   (cp_parser *);
1741 static tree cp_parser_constant_initializer
1742   (cp_parser *);
1743
1744 /* Derived classes [gram.class.derived] */
1745
1746 static tree cp_parser_base_clause
1747   (cp_parser *);
1748 static tree cp_parser_base_specifier
1749   (cp_parser *);
1750
1751 /* Special member functions [gram.special] */
1752
1753 static tree cp_parser_conversion_function_id
1754   (cp_parser *);
1755 static tree cp_parser_conversion_type_id
1756   (cp_parser *);
1757 static cp_declarator *cp_parser_conversion_declarator_opt
1758   (cp_parser *);
1759 static bool cp_parser_ctor_initializer_opt
1760   (cp_parser *);
1761 static void cp_parser_mem_initializer_list
1762   (cp_parser *);
1763 static tree cp_parser_mem_initializer
1764   (cp_parser *);
1765 static tree cp_parser_mem_initializer_id
1766   (cp_parser *);
1767
1768 /* Overloading [gram.over] */
1769
1770 static tree cp_parser_operator_function_id
1771   (cp_parser *);
1772 static tree cp_parser_operator
1773   (cp_parser *);
1774
1775 /* Templates [gram.temp] */
1776
1777 static void cp_parser_template_declaration
1778   (cp_parser *, bool);
1779 static tree cp_parser_template_parameter_list
1780   (cp_parser *);
1781 static tree cp_parser_template_parameter
1782   (cp_parser *, bool *);
1783 static tree cp_parser_type_parameter
1784   (cp_parser *);
1785 static tree cp_parser_template_id
1786   (cp_parser *, bool, bool, bool);
1787 static tree cp_parser_template_name
1788   (cp_parser *, bool, bool, bool, bool *);
1789 static tree cp_parser_template_argument_list
1790   (cp_parser *);
1791 static tree cp_parser_template_argument
1792   (cp_parser *);
1793 static void cp_parser_explicit_instantiation
1794   (cp_parser *);
1795 static void cp_parser_explicit_specialization
1796   (cp_parser *);
1797
1798 /* Exception handling [gram.exception] */
1799
1800 static tree cp_parser_try_block
1801   (cp_parser *);
1802 static bool cp_parser_function_try_block
1803   (cp_parser *);
1804 static void cp_parser_handler_seq
1805   (cp_parser *);
1806 static void cp_parser_handler
1807   (cp_parser *);
1808 static tree cp_parser_exception_declaration
1809   (cp_parser *);
1810 static tree cp_parser_throw_expression
1811   (cp_parser *);
1812 static tree cp_parser_exception_specification_opt
1813   (cp_parser *);
1814 static tree cp_parser_type_id_list
1815   (cp_parser *);
1816
1817 /* GNU Extensions */
1818
1819 static tree cp_parser_asm_specification_opt
1820   (cp_parser *);
1821 static tree cp_parser_asm_operand_list
1822   (cp_parser *);
1823 static tree cp_parser_asm_clobber_list
1824   (cp_parser *);
1825 static tree cp_parser_attributes_opt
1826   (cp_parser *);
1827 static tree cp_parser_attribute_list
1828   (cp_parser *);
1829 static bool cp_parser_extension_opt
1830   (cp_parser *, int *);
1831 static void cp_parser_label_declaration
1832   (cp_parser *);
1833
1834 /* Utility Routines */
1835
1836 static tree cp_parser_lookup_name
1837   (cp_parser *, tree, bool, bool, bool, bool);
1838 static tree cp_parser_lookup_name_simple
1839   (cp_parser *, tree);
1840 static tree cp_parser_maybe_treat_template_as_class
1841   (tree, bool);
1842 static bool cp_parser_check_declarator_template_parameters
1843   (cp_parser *, cp_declarator *);
1844 static bool cp_parser_check_template_parameters
1845   (cp_parser *, unsigned);
1846 static tree cp_parser_simple_cast_expression
1847   (cp_parser *);
1848 static tree cp_parser_binary_expression
1849   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1850 static tree cp_parser_global_scope_opt
1851   (cp_parser *, bool);
1852 static bool cp_parser_constructor_declarator_p
1853   (cp_parser *, bool);
1854 static tree cp_parser_function_definition_from_specifiers_and_declarator
1855   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1856 static tree cp_parser_function_definition_after_declarator
1857   (cp_parser *, bool);
1858 static void cp_parser_template_declaration_after_export
1859   (cp_parser *, bool);
1860 static tree cp_parser_single_declaration
1861   (cp_parser *, bool, bool *);
1862 static tree cp_parser_functional_cast
1863   (cp_parser *, tree);
1864 static tree cp_parser_save_member_function_body
1865   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1866 static tree cp_parser_enclosed_template_argument_list
1867   (cp_parser *);
1868 static void cp_parser_save_default_args
1869   (cp_parser *, tree);
1870 static void cp_parser_late_parsing_for_member
1871   (cp_parser *, tree);
1872 static void cp_parser_late_parsing_default_args
1873   (cp_parser *, tree);
1874 static tree cp_parser_sizeof_operand
1875   (cp_parser *, enum rid);
1876 static bool cp_parser_declares_only_class_p
1877   (cp_parser *);
1878 static void cp_parser_set_storage_class
1879   (cp_decl_specifier_seq *, cp_storage_class);
1880 static void cp_parser_set_decl_spec_type 
1881   (cp_decl_specifier_seq *, tree, bool);
1882 static bool cp_parser_friend_p
1883   (const cp_decl_specifier_seq *);
1884 static cp_token *cp_parser_require
1885   (cp_parser *, enum cpp_ttype, const char *);
1886 static cp_token *cp_parser_require_keyword
1887   (cp_parser *, enum rid, const char *);
1888 static bool cp_parser_token_starts_function_definition_p
1889   (cp_token *);
1890 static bool cp_parser_next_token_starts_class_definition_p
1891   (cp_parser *);
1892 static bool cp_parser_next_token_ends_template_argument_p
1893   (cp_parser *);
1894 static bool cp_parser_nth_token_starts_template_argument_list_p
1895   (cp_parser *, size_t);
1896 static enum tag_types cp_parser_token_is_class_key
1897   (cp_token *);
1898 static void cp_parser_check_class_key
1899   (enum tag_types, tree type);
1900 static void cp_parser_check_access_in_redeclaration
1901   (tree type);
1902 static bool cp_parser_optional_template_keyword
1903   (cp_parser *);
1904 static void cp_parser_pre_parsed_nested_name_specifier
1905   (cp_parser *);
1906 static void cp_parser_cache_group
1907   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1908 static void cp_parser_parse_tentatively
1909   (cp_parser *);
1910 static void cp_parser_commit_to_tentative_parse
1911   (cp_parser *);
1912 static void cp_parser_abort_tentative_parse
1913   (cp_parser *);
1914 static bool cp_parser_parse_definitely
1915   (cp_parser *);
1916 static inline bool cp_parser_parsing_tentatively
1917   (cp_parser *);
1918 static bool cp_parser_committed_to_tentative_parse
1919   (cp_parser *);
1920 static void cp_parser_error
1921   (cp_parser *, const char *);
1922 static void cp_parser_name_lookup_error
1923   (cp_parser *, tree, tree, const char *);
1924 static bool cp_parser_simulate_error
1925   (cp_parser *);
1926 static void cp_parser_check_type_definition
1927   (cp_parser *);
1928 static void cp_parser_check_for_definition_in_return_type
1929   (cp_declarator *, int);
1930 static void cp_parser_check_for_invalid_template_id
1931   (cp_parser *, tree);
1932 static bool cp_parser_non_integral_constant_expression
1933   (cp_parser *, const char *);
1934 static void cp_parser_diagnose_invalid_type_name
1935   (cp_parser *, tree, tree);
1936 static bool cp_parser_parse_and_diagnose_invalid_type_name
1937   (cp_parser *);
1938 static int cp_parser_skip_to_closing_parenthesis
1939   (cp_parser *, bool, bool, bool);
1940 static void cp_parser_skip_to_end_of_statement
1941   (cp_parser *);
1942 static void cp_parser_consume_semicolon_at_end_of_statement
1943   (cp_parser *);
1944 static void cp_parser_skip_to_end_of_block_or_statement
1945   (cp_parser *);
1946 static void cp_parser_skip_to_closing_brace
1947   (cp_parser *);
1948 static void cp_parser_skip_until_found
1949   (cp_parser *, enum cpp_ttype, const char *);
1950 static bool cp_parser_error_occurred
1951   (cp_parser *);
1952 static bool cp_parser_allow_gnu_extensions_p
1953   (cp_parser *);
1954 static bool cp_parser_is_string_literal
1955   (cp_token *);
1956 static bool cp_parser_is_keyword
1957   (cp_token *, enum rid);
1958 static tree cp_parser_make_typename_type
1959   (cp_parser *, tree, tree);
1960
1961 /* Returns nonzero if we are parsing tentatively.  */
1962
1963 static inline bool
1964 cp_parser_parsing_tentatively (cp_parser* parser)
1965 {
1966   return parser->context->next != NULL;
1967 }
1968
1969 /* Returns nonzero if TOKEN is a string literal.  */
1970
1971 static bool
1972 cp_parser_is_string_literal (cp_token* token)
1973 {
1974   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1975 }
1976
1977 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1978
1979 static bool
1980 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1981 {
1982   return token->keyword == keyword;
1983 }
1984
1985 /* Issue the indicated error MESSAGE.  */
1986
1987 static void
1988 cp_parser_error (cp_parser* parser, const char* message)
1989 {
1990   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1991   if (!cp_parser_simulate_error (parser))
1992     {
1993       cp_token *token;
1994       token = cp_lexer_peek_token (parser->lexer);
1995       c_parse_error (message,
1996                      /* Because c_parser_error does not understand
1997                         CPP_KEYWORD, keywords are treated like
1998                         identifiers.  */
1999                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2000                      token->value);
2001     }
2002 }
2003
2004 /* Issue an error about name-lookup failing.  NAME is the
2005    IDENTIFIER_NODE DECL is the result of
2006    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2007    the thing that we hoped to find.  */
2008
2009 static void
2010 cp_parser_name_lookup_error (cp_parser* parser,
2011                              tree name,
2012                              tree decl,
2013                              const char* desired)
2014 {
2015   /* If name lookup completely failed, tell the user that NAME was not
2016      declared.  */
2017   if (decl == error_mark_node)
2018     {
2019       if (parser->scope && parser->scope != global_namespace)
2020         error ("`%D::%D' has not been declared",
2021                parser->scope, name);
2022       else if (parser->scope == global_namespace)
2023         error ("`::%D' has not been declared", name);
2024       else
2025         error ("`%D' has not been declared", name);
2026     }
2027   else if (parser->scope && parser->scope != global_namespace)
2028     error ("`%D::%D' %s", parser->scope, name, desired);
2029   else if (parser->scope == global_namespace)
2030     error ("`::%D' %s", name, desired);
2031   else
2032     error ("`%D' %s", name, desired);
2033 }
2034
2035 /* If we are parsing tentatively, remember that an error has occurred
2036    during this tentative parse.  Returns true if the error was
2037    simulated; false if a message should be issued by the caller.  */
2038
2039 static bool
2040 cp_parser_simulate_error (cp_parser* parser)
2041 {
2042   if (cp_parser_parsing_tentatively (parser)
2043       && !cp_parser_committed_to_tentative_parse (parser))
2044     {
2045       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2046       return true;
2047     }
2048   return false;
2049 }
2050
2051 /* This function is called when a type is defined.  If type
2052    definitions are forbidden at this point, an error message is
2053    issued.  */
2054
2055 static void
2056 cp_parser_check_type_definition (cp_parser* parser)
2057 {
2058   /* If types are forbidden here, issue a message.  */
2059   if (parser->type_definition_forbidden_message)
2060     /* Use `%s' to print the string in case there are any escape
2061        characters in the message.  */
2062     error ("%s", parser->type_definition_forbidden_message);
2063 }
2064
2065 /* This function is called when a declaration is parsed.  If
2066    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2067    indicates that a type was defined in the decl-specifiers for DECL,
2068    then an error is issued.  */
2069
2070 static void
2071 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2072                                                int declares_class_or_enum)
2073 {
2074   /* [dcl.fct] forbids type definitions in return types.
2075      Unfortunately, it's not easy to know whether or not we are
2076      processing a return type until after the fact.  */
2077   while (declarator
2078          && (declarator->kind == cdk_pointer
2079              || declarator->kind == cdk_reference
2080              || declarator->kind == cdk_ptrmem))
2081     declarator = declarator->declarator;
2082   if (declarator
2083       && declarator->kind == cdk_function
2084       && declares_class_or_enum & 2)
2085     error ("new types may not be defined in a return type");
2086 }
2087
2088 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2089    "<" in any valid C++ program.  If the next token is indeed "<",
2090    issue a message warning the user about what appears to be an
2091    invalid attempt to form a template-id.  */
2092
2093 static void
2094 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2095                                          tree type)
2096 {
2097   ptrdiff_t start;
2098   cp_token *token;
2099
2100   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2101     {
2102       if (TYPE_P (type))
2103         error ("`%T' is not a template", type);
2104       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2105         error ("`%E' is not a template", type);
2106       else
2107         error ("invalid template-id");
2108       /* Remember the location of the invalid "<".  */
2109       if (cp_parser_parsing_tentatively (parser)
2110           && !cp_parser_committed_to_tentative_parse (parser))
2111         {
2112           token = cp_lexer_peek_token (parser->lexer);
2113           token = cp_lexer_prev_token (parser->lexer, token);
2114           start = cp_lexer_token_difference (parser->lexer,
2115                                              parser->lexer->first_token,
2116                                              token);
2117         }
2118       else
2119         start = -1;
2120       /* Consume the "<".  */
2121       cp_lexer_consume_token (parser->lexer);
2122       /* Parse the template arguments.  */
2123       cp_parser_enclosed_template_argument_list (parser);
2124       /* Permanently remove the invalid template arguments so that
2125          this error message is not issued again.  */
2126       if (start >= 0)
2127         {
2128           token = cp_lexer_advance_token (parser->lexer,
2129                                           parser->lexer->first_token,
2130                                           start);
2131           cp_lexer_purge_tokens_after (parser->lexer, token);
2132         }
2133     }
2134 }
2135
2136 /* If parsing an integral constant-expression, issue an error message
2137    about the fact that THING appeared and return true.  Otherwise,
2138    return false, marking the current expression as non-constant.  */
2139
2140 static bool
2141 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2142                                             const char *thing)
2143 {
2144   if (parser->integral_constant_expression_p)
2145     {
2146       if (!parser->allow_non_integral_constant_expression_p)
2147         {
2148           error ("%s cannot appear in a constant-expression", thing);
2149           return true;
2150         }
2151       parser->non_integral_constant_expression_p = true;
2152     }
2153   return false;
2154 }
2155
2156 /* Emit a diagnostic for an invalid type name. Consider also if it is
2157    qualified or not and the result of a lookup, to provide a better
2158    message.  */
2159
2160 static void
2161 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2162 {
2163   tree decl, old_scope;
2164   /* Try to lookup the identifier.  */
2165   old_scope = parser->scope;
2166   parser->scope = scope;
2167   decl = cp_parser_lookup_name_simple (parser, id);
2168   parser->scope = old_scope;
2169   /* If the lookup found a template-name, it means that the user forgot
2170   to specify an argument list. Emit an useful error message.  */
2171   if (TREE_CODE (decl) == TEMPLATE_DECL)
2172     error ("invalid use of template-name `%E' without an argument list",
2173       decl);
2174   else if (!parser->scope)
2175     {
2176       /* Issue an error message.  */
2177       error ("`%E' does not name a type", id);
2178       /* If we're in a template class, it's possible that the user was
2179          referring to a type from a base class.  For example:
2180
2181            template <typename T> struct A { typedef T X; };
2182            template <typename T> struct B : public A<T> { X x; };
2183
2184          The user should have said "typename A<T>::X".  */
2185       if (processing_template_decl && current_class_type)
2186         {
2187           tree b;
2188
2189           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2190                b;
2191                b = TREE_CHAIN (b))
2192             {
2193               tree base_type = BINFO_TYPE (b);
2194               if (CLASS_TYPE_P (base_type)
2195                   && dependent_type_p (base_type))
2196                 {
2197                   tree field;
2198                   /* Go from a particular instantiation of the
2199                      template (which will have an empty TYPE_FIELDs),
2200                      to the main version.  */
2201                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2202                   for (field = TYPE_FIELDS (base_type);
2203                        field;
2204                        field = TREE_CHAIN (field))
2205                     if (TREE_CODE (field) == TYPE_DECL
2206                         && DECL_NAME (field) == id)
2207                       {
2208                         inform ("(perhaps `typename %T::%E' was intended)",
2209                                 BINFO_TYPE (b), id);
2210                         break;
2211                       }
2212                   if (field)
2213                     break;
2214                 }
2215             }
2216         }
2217     }
2218   /* Here we diagnose qualified-ids where the scope is actually correct,
2219      but the identifier does not resolve to a valid type name.  */
2220   else
2221     {
2222       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2223         error ("`%E' in namespace `%E' does not name a type",
2224                id, parser->scope);
2225       else if (TYPE_P (parser->scope))
2226         error ("`%E' in class `%T' does not name a type",
2227                id, parser->scope);
2228       else
2229         abort();
2230     }
2231 }
2232
2233 /* Check for a common situation where a type-name should be present,
2234    but is not, and issue a sensible error message.  Returns true if an
2235    invalid type-name was detected.
2236
2237    The situation handled by this function are variable declarations of the
2238    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2239    Usually, `ID' should name a type, but if we got here it means that it
2240    does not. We try to emit the best possible error message depending on
2241    how exactly the id-expression looks like.
2242 */
2243
2244 static bool
2245 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2246 {
2247   tree id;
2248
2249   cp_parser_parse_tentatively (parser);
2250   id = cp_parser_id_expression (parser,
2251                                 /*template_keyword_p=*/false,
2252                                 /*check_dependency_p=*/true,
2253                                 /*template_p=*/NULL,
2254                                 /*declarator_p=*/true);
2255   /* After the id-expression, there should be a plain identifier,
2256      otherwise this is not a simple variable declaration. Also, if
2257      the scope is dependent, we cannot do much.  */
2258   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2259       || (parser->scope && TYPE_P (parser->scope)
2260           && dependent_type_p (parser->scope)))
2261     {
2262       cp_parser_abort_tentative_parse (parser);
2263       return false;
2264     }
2265   if (!cp_parser_parse_definitely (parser))
2266     return false;
2267
2268   /* If we got here, this cannot be a valid variable declaration, thus
2269      the cp_parser_id_expression must have resolved to a plain identifier
2270      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
2271   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2272   /* Emit a diagnostic for the invalid type.  */
2273   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2274   /* Skip to the end of the declaration; there's no point in
2275      trying to process it.  */
2276   cp_parser_skip_to_end_of_block_or_statement (parser);
2277   return true;
2278 }
2279
2280 /* Consume tokens up to, and including, the next non-nested closing `)'.
2281    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2282    are doing error recovery. Returns -1 if OR_COMMA is true and we
2283    found an unnested comma.  */
2284
2285 static int
2286 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2287                                        bool recovering,
2288                                        bool or_comma,
2289                                        bool consume_paren)
2290 {
2291   unsigned paren_depth = 0;
2292   unsigned brace_depth = 0;
2293   int saved_c_lex_string_translate = c_lex_string_translate;
2294   int result;
2295
2296   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2297       && !cp_parser_committed_to_tentative_parse (parser))
2298     return 0;
2299
2300   if (! recovering)
2301     /* If we're looking ahead, keep both translated and untranslated
2302        strings.  */
2303     c_lex_string_translate = -1;
2304
2305   while (true)
2306     {
2307       cp_token *token;
2308
2309       /* If we've run out of tokens, then there is no closing `)'.  */
2310       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2311         {
2312           result = 0;
2313           break;
2314         }
2315
2316       token = cp_lexer_peek_token (parser->lexer);
2317
2318       /* This matches the processing in skip_to_end_of_statement.  */
2319       if (token->type == CPP_SEMICOLON && !brace_depth)
2320         {
2321           result = 0;
2322           break;
2323         }
2324       if (token->type == CPP_OPEN_BRACE)
2325         ++brace_depth;
2326       if (token->type == CPP_CLOSE_BRACE)
2327         {
2328           if (!brace_depth--)
2329             {
2330               result = 0;
2331               break;
2332             }
2333         }
2334       if (recovering && or_comma && token->type == CPP_COMMA
2335           && !brace_depth && !paren_depth)
2336         {
2337           result = -1;
2338           break;
2339         }
2340
2341       if (!brace_depth)
2342         {
2343           /* If it is an `(', we have entered another level of nesting.  */
2344           if (token->type == CPP_OPEN_PAREN)
2345             ++paren_depth;
2346           /* If it is a `)', then we might be done.  */
2347           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2348             {
2349               if (consume_paren)
2350                 cp_lexer_consume_token (parser->lexer);
2351               {
2352                 result = 1;
2353                 break;
2354               }
2355             }
2356         }
2357
2358       /* Consume the token.  */
2359       cp_lexer_consume_token (parser->lexer);
2360     }
2361
2362   c_lex_string_translate = saved_c_lex_string_translate;
2363   return result;
2364 }
2365
2366 /* Consume tokens until we reach the end of the current statement.
2367    Normally, that will be just before consuming a `;'.  However, if a
2368    non-nested `}' comes first, then we stop before consuming that.  */
2369
2370 static void
2371 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2372 {
2373   unsigned nesting_depth = 0;
2374
2375   while (true)
2376     {
2377       cp_token *token;
2378
2379       /* Peek at the next token.  */
2380       token = cp_lexer_peek_token (parser->lexer);
2381       /* If we've run out of tokens, stop.  */
2382       if (token->type == CPP_EOF)
2383         break;
2384       /* If the next token is a `;', we have reached the end of the
2385          statement.  */
2386       if (token->type == CPP_SEMICOLON && !nesting_depth)
2387         break;
2388       /* If the next token is a non-nested `}', then we have reached
2389          the end of the current block.  */
2390       if (token->type == CPP_CLOSE_BRACE)
2391         {
2392           /* If this is a non-nested `}', stop before consuming it.
2393              That way, when confronted with something like:
2394
2395                { 3 + }
2396
2397              we stop before consuming the closing `}', even though we
2398              have not yet reached a `;'.  */
2399           if (nesting_depth == 0)
2400             break;
2401           /* If it is the closing `}' for a block that we have
2402              scanned, stop -- but only after consuming the token.
2403              That way given:
2404
2405                 void f g () { ... }
2406                 typedef int I;
2407
2408              we will stop after the body of the erroneously declared
2409              function, but before consuming the following `typedef'
2410              declaration.  */
2411           if (--nesting_depth == 0)
2412             {
2413               cp_lexer_consume_token (parser->lexer);
2414               break;
2415             }
2416         }
2417       /* If it the next token is a `{', then we are entering a new
2418          block.  Consume the entire block.  */
2419       else if (token->type == CPP_OPEN_BRACE)
2420         ++nesting_depth;
2421       /* Consume the token.  */
2422       cp_lexer_consume_token (parser->lexer);
2423     }
2424 }
2425
2426 /* This function is called at the end of a statement or declaration.
2427    If the next token is a semicolon, it is consumed; otherwise, error
2428    recovery is attempted.  */
2429
2430 static void
2431 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2432 {
2433   /* Look for the trailing `;'.  */
2434   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2435     {
2436       /* If there is additional (erroneous) input, skip to the end of
2437          the statement.  */
2438       cp_parser_skip_to_end_of_statement (parser);
2439       /* If the next token is now a `;', consume it.  */
2440       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2441         cp_lexer_consume_token (parser->lexer);
2442     }
2443 }
2444
2445 /* Skip tokens until we have consumed an entire block, or until we
2446    have consumed a non-nested `;'.  */
2447
2448 static void
2449 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2450 {
2451   unsigned nesting_depth = 0;
2452
2453   while (true)
2454     {
2455       cp_token *token;
2456
2457       /* Peek at the next token.  */
2458       token = cp_lexer_peek_token (parser->lexer);
2459       /* If we've run out of tokens, stop.  */
2460       if (token->type == CPP_EOF)
2461         break;
2462       /* If the next token is a `;', we have reached the end of the
2463          statement.  */
2464       if (token->type == CPP_SEMICOLON && !nesting_depth)
2465         {
2466           /* Consume the `;'.  */
2467           cp_lexer_consume_token (parser->lexer);
2468           break;
2469         }
2470       /* Consume the token.  */
2471       token = cp_lexer_consume_token (parser->lexer);
2472       /* If the next token is a non-nested `}', then we have reached
2473          the end of the current block.  */
2474       if (token->type == CPP_CLOSE_BRACE
2475           && (nesting_depth == 0 || --nesting_depth == 0))
2476         break;
2477       /* If it the next token is a `{', then we are entering a new
2478          block.  Consume the entire block.  */
2479       if (token->type == CPP_OPEN_BRACE)
2480         ++nesting_depth;
2481     }
2482 }
2483
2484 /* Skip tokens until a non-nested closing curly brace is the next
2485    token.  */
2486
2487 static void
2488 cp_parser_skip_to_closing_brace (cp_parser *parser)
2489 {
2490   unsigned nesting_depth = 0;
2491
2492   while (true)
2493     {
2494       cp_token *token;
2495
2496       /* Peek at the next token.  */
2497       token = cp_lexer_peek_token (parser->lexer);
2498       /* If we've run out of tokens, stop.  */
2499       if (token->type == CPP_EOF)
2500         break;
2501       /* If the next token is a non-nested `}', then we have reached
2502          the end of the current block.  */
2503       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2504         break;
2505       /* If it the next token is a `{', then we are entering a new
2506          block.  Consume the entire block.  */
2507       else if (token->type == CPP_OPEN_BRACE)
2508         ++nesting_depth;
2509       /* Consume the token.  */
2510       cp_lexer_consume_token (parser->lexer);
2511     }
2512 }
2513
2514 /* This is a simple wrapper around make_typename_type. When the id is
2515    an unresolved identifier node, we can provide a superior diagnostic
2516    using cp_parser_diagnose_invalid_type_name.  */
2517
2518 static tree
2519 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2520 {
2521   tree result;
2522   if (TREE_CODE (id) == IDENTIFIER_NODE)
2523     {
2524       result = make_typename_type (scope, id, /*complain=*/0);
2525       if (result == error_mark_node)
2526         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2527       return result;
2528     }
2529   return make_typename_type (scope, id, tf_error);
2530 }
2531
2532
2533 /* Create a new C++ parser.  */
2534
2535 static cp_parser *
2536 cp_parser_new (void)
2537 {
2538   cp_parser *parser;
2539   cp_lexer *lexer;
2540
2541   /* cp_lexer_new_main is called before calling ggc_alloc because
2542      cp_lexer_new_main might load a PCH file.  */
2543   lexer = cp_lexer_new_main ();
2544
2545   parser = ggc_alloc_cleared (sizeof (cp_parser));
2546   parser->lexer = lexer;
2547   parser->context = cp_parser_context_new (NULL);
2548
2549   /* For now, we always accept GNU extensions.  */
2550   parser->allow_gnu_extensions_p = 1;
2551
2552   /* The `>' token is a greater-than operator, not the end of a
2553      template-id.  */
2554   parser->greater_than_is_operator_p = true;
2555
2556   parser->default_arg_ok_p = true;
2557
2558   /* We are not parsing a constant-expression.  */
2559   parser->integral_constant_expression_p = false;
2560   parser->allow_non_integral_constant_expression_p = false;
2561   parser->non_integral_constant_expression_p = false;
2562
2563   /* Local variable names are not forbidden.  */
2564   parser->local_variables_forbidden_p = false;
2565
2566   /* We are not processing an `extern "C"' declaration.  */
2567   parser->in_unbraced_linkage_specification_p = false;
2568
2569   /* We are not processing a declarator.  */
2570   parser->in_declarator_p = false;
2571
2572   /* We are not processing a template-argument-list.  */
2573   parser->in_template_argument_list_p = false;
2574
2575   /* We are not in an iteration statement.  */
2576   parser->in_iteration_statement_p = false;
2577
2578   /* We are not in a switch statement.  */
2579   parser->in_switch_statement_p = false;
2580
2581   /* We are not parsing a type-id inside an expression.  */
2582   parser->in_type_id_in_expr_p = false;
2583
2584   /* The unparsed function queue is empty.  */
2585   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2586
2587   /* There are no classes being defined.  */
2588   parser->num_classes_being_defined = 0;
2589
2590   /* No template parameters apply.  */
2591   parser->num_template_parameter_lists = 0;
2592
2593   return parser;
2594 }
2595
2596 /* Lexical conventions [gram.lex]  */
2597
2598 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2599    identifier.  */
2600
2601 static tree
2602 cp_parser_identifier (cp_parser* parser)
2603 {
2604   cp_token *token;
2605
2606   /* Look for the identifier.  */
2607   token = cp_parser_require (parser, CPP_NAME, "identifier");
2608   /* Return the value.  */
2609   return token ? token->value : error_mark_node;
2610 }
2611
2612 /* Basic concepts [gram.basic]  */
2613
2614 /* Parse a translation-unit.
2615
2616    translation-unit:
2617      declaration-seq [opt]
2618
2619    Returns TRUE if all went well.  */
2620
2621 static bool
2622 cp_parser_translation_unit (cp_parser* parser)
2623 {
2624   /* The address of the first non-permanent object on the declarator
2625      obstack.  */
2626   static void *declarator_obstack_base;
2627
2628   bool success;
2629   
2630   /* Create the declarator obstack, if necessary.  */
2631   if (!cp_error_declarator)
2632     {
2633       gcc_obstack_init (&declarator_obstack);
2634       /* Create the error declarator.  */
2635       cp_error_declarator = make_declarator (cdk_error);
2636       /* Create the empty parameter list.  */
2637       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2638       /* Remember where the base of the declarator obstack lies.  */
2639       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2640     }
2641
2642   while (true)
2643     {
2644       cp_parser_declaration_seq_opt (parser);
2645
2646       /* If there are no tokens left then all went well.  */
2647       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2648         {
2649           /* Consume the EOF token.  */
2650           cp_parser_require (parser, CPP_EOF, "end-of-file");
2651           
2652           /* Finish up.  */
2653           finish_translation_unit ();
2654
2655           success = true;
2656           break;
2657         }
2658       else
2659         {
2660           cp_parser_error (parser, "expected declaration");
2661           success = false;
2662           break;
2663         }
2664     }
2665
2666   /* Make sure the declarator obstack was fully cleaned up.  */
2667   my_friendly_assert (obstack_next_free (&declarator_obstack) ==
2668                       declarator_obstack_base,
2669                       20040621);
2670
2671   /* All went well.  */
2672   return success;
2673 }
2674
2675 /* Expressions [gram.expr] */
2676
2677 /* Parse a primary-expression.
2678
2679    primary-expression:
2680      literal
2681      this
2682      ( expression )
2683      id-expression
2684
2685    GNU Extensions:
2686
2687    primary-expression:
2688      ( compound-statement )
2689      __builtin_va_arg ( assignment-expression , type-id )
2690
2691    literal:
2692      __null
2693
2694    Returns a representation of the expression.
2695
2696    *IDK indicates what kind of id-expression (if any) was present.
2697
2698    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2699    used as the operand of a pointer-to-member.  In that case,
2700    *QUALIFYING_CLASS gives the class that is used as the qualifying
2701    class in the pointer-to-member.  */
2702
2703 static tree
2704 cp_parser_primary_expression (cp_parser *parser,
2705                               cp_id_kind *idk,
2706                               tree *qualifying_class)
2707 {
2708   cp_token *token;
2709
2710   /* Assume the primary expression is not an id-expression.  */
2711   *idk = CP_ID_KIND_NONE;
2712   /* And that it cannot be used as pointer-to-member.  */
2713   *qualifying_class = NULL_TREE;
2714
2715   /* Peek at the next token.  */
2716   token = cp_lexer_peek_token (parser->lexer);
2717   switch (token->type)
2718     {
2719       /* literal:
2720            integer-literal
2721            character-literal
2722            floating-literal
2723            string-literal
2724            boolean-literal  */
2725     case CPP_CHAR:
2726     case CPP_WCHAR:
2727     case CPP_NUMBER:
2728       token = cp_lexer_consume_token (parser->lexer);
2729       return token->value;
2730
2731     case CPP_STRING:
2732     case CPP_WSTRING:
2733       token = cp_lexer_consume_token (parser->lexer);
2734       if (TREE_CHAIN (token->value))
2735         return TREE_CHAIN (token->value);
2736       else
2737         return token->value;
2738
2739     case CPP_OPEN_PAREN:
2740       {
2741         tree expr;
2742         bool saved_greater_than_is_operator_p;
2743
2744         /* Consume the `('.  */
2745         cp_lexer_consume_token (parser->lexer);
2746         /* Within a parenthesized expression, a `>' token is always
2747            the greater-than operator.  */
2748         saved_greater_than_is_operator_p
2749           = parser->greater_than_is_operator_p;
2750         parser->greater_than_is_operator_p = true;
2751         /* If we see `( { ' then we are looking at the beginning of
2752            a GNU statement-expression.  */
2753         if (cp_parser_allow_gnu_extensions_p (parser)
2754             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2755           {
2756             /* Statement-expressions are not allowed by the standard.  */
2757             if (pedantic)
2758               pedwarn ("ISO C++ forbids braced-groups within expressions");
2759
2760             /* And they're not allowed outside of a function-body; you
2761                cannot, for example, write:
2762
2763                  int i = ({ int j = 3; j + 1; });
2764
2765                at class or namespace scope.  */
2766             if (!at_function_scope_p ())
2767               error ("statement-expressions are allowed only inside functions");
2768             /* Start the statement-expression.  */
2769             expr = begin_stmt_expr ();
2770             /* Parse the compound-statement.  */
2771             cp_parser_compound_statement (parser, expr, false);
2772             /* Finish up.  */
2773             expr = finish_stmt_expr (expr, false);
2774           }
2775         else
2776           {
2777             /* Parse the parenthesized expression.  */
2778             expr = cp_parser_expression (parser);
2779             /* Let the front end know that this expression was
2780                enclosed in parentheses. This matters in case, for
2781                example, the expression is of the form `A::B', since
2782                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2783                not.  */
2784             finish_parenthesized_expr (expr);
2785           }
2786         /* The `>' token might be the end of a template-id or
2787            template-parameter-list now.  */
2788         parser->greater_than_is_operator_p
2789           = saved_greater_than_is_operator_p;
2790         /* Consume the `)'.  */
2791         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2792           cp_parser_skip_to_end_of_statement (parser);
2793
2794         return expr;
2795       }
2796
2797     case CPP_KEYWORD:
2798       switch (token->keyword)
2799         {
2800           /* These two are the boolean literals.  */
2801         case RID_TRUE:
2802           cp_lexer_consume_token (parser->lexer);
2803           return boolean_true_node;
2804         case RID_FALSE:
2805           cp_lexer_consume_token (parser->lexer);
2806           return boolean_false_node;
2807
2808           /* The `__null' literal.  */
2809         case RID_NULL:
2810           cp_lexer_consume_token (parser->lexer);
2811           return null_node;
2812
2813           /* Recognize the `this' keyword.  */
2814         case RID_THIS:
2815           cp_lexer_consume_token (parser->lexer);
2816           if (parser->local_variables_forbidden_p)
2817             {
2818               error ("`this' may not be used in this context");
2819               return error_mark_node;
2820             }
2821           /* Pointers cannot appear in constant-expressions.  */
2822           if (cp_parser_non_integral_constant_expression (parser,
2823                                                           "`this'"))
2824             return error_mark_node;
2825           return finish_this_expr ();
2826
2827           /* The `operator' keyword can be the beginning of an
2828              id-expression.  */
2829         case RID_OPERATOR:
2830           goto id_expression;
2831
2832         case RID_FUNCTION_NAME:
2833         case RID_PRETTY_FUNCTION_NAME:
2834         case RID_C99_FUNCTION_NAME:
2835           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836              __func__ are the names of variables -- but they are
2837              treated specially.  Therefore, they are handled here,
2838              rather than relying on the generic id-expression logic
2839              below.  Grammatically, these names are id-expressions.
2840
2841              Consume the token.  */
2842           token = cp_lexer_consume_token (parser->lexer);
2843           /* Look up the name.  */
2844           return finish_fname (token->value);
2845
2846         case RID_VA_ARG:
2847           {
2848             tree expression;
2849             tree type;
2850
2851             /* The `__builtin_va_arg' construct is used to handle
2852                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2853             cp_lexer_consume_token (parser->lexer);
2854             /* Look for the opening `('.  */
2855             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2856             /* Now, parse the assignment-expression.  */
2857             expression = cp_parser_assignment_expression (parser);
2858             /* Look for the `,'.  */
2859             cp_parser_require (parser, CPP_COMMA, "`,'");
2860             /* Parse the type-id.  */
2861             type = cp_parser_type_id (parser);
2862             /* Look for the closing `)'.  */
2863             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2864             /* Using `va_arg' in a constant-expression is not
2865                allowed.  */
2866             if (cp_parser_non_integral_constant_expression (parser,
2867                                                             "`va_arg'"))
2868               return error_mark_node;
2869             return build_x_va_arg (expression, type);
2870           }
2871
2872         case RID_OFFSETOF:
2873           return cp_parser_builtin_offsetof (parser);
2874
2875         default:
2876           cp_parser_error (parser, "expected primary-expression");
2877           return error_mark_node;
2878         }
2879
2880       /* An id-expression can start with either an identifier, a
2881          `::' as the beginning of a qualified-id, or the "operator"
2882          keyword.  */
2883     case CPP_NAME:
2884     case CPP_SCOPE:
2885     case CPP_TEMPLATE_ID:
2886     case CPP_NESTED_NAME_SPECIFIER:
2887       {
2888         tree id_expression;
2889         tree decl;
2890         const char *error_msg;
2891
2892       id_expression:
2893         /* Parse the id-expression.  */
2894         id_expression
2895           = cp_parser_id_expression (parser,
2896                                      /*template_keyword_p=*/false,
2897                                      /*check_dependency_p=*/true,
2898                                      /*template_p=*/NULL,
2899                                      /*declarator_p=*/false);
2900         if (id_expression == error_mark_node)
2901           return error_mark_node;
2902         /* If we have a template-id, then no further lookup is
2903            required.  If the template-id was for a template-class, we
2904            will sometimes have a TYPE_DECL at this point.  */
2905         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2906             || TREE_CODE (id_expression) == TYPE_DECL)
2907           decl = id_expression;
2908         /* Look up the name.  */
2909         else
2910           {
2911             decl = cp_parser_lookup_name_simple (parser, id_expression);
2912             /* If name lookup gives us a SCOPE_REF, then the
2913                qualifying scope was dependent.  Just propagate the
2914                name.  */
2915             if (TREE_CODE (decl) == SCOPE_REF)
2916               {
2917                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2918                   *qualifying_class = TREE_OPERAND (decl, 0);
2919                 return decl;
2920               }
2921             /* Check to see if DECL is a local variable in a context
2922                where that is forbidden.  */
2923             if (parser->local_variables_forbidden_p
2924                 && local_variable_p (decl))
2925               {
2926                 /* It might be that we only found DECL because we are
2927                    trying to be generous with pre-ISO scoping rules.
2928                    For example, consider:
2929
2930                      int i;
2931                      void g() {
2932                        for (int i = 0; i < 10; ++i) {}
2933                        extern void f(int j = i);
2934                      }
2935
2936                    Here, name look up will originally find the out
2937                    of scope `i'.  We need to issue a warning message,
2938                    but then use the global `i'.  */
2939                 decl = check_for_out_of_scope_variable (decl);
2940                 if (local_variable_p (decl))
2941                   {
2942                     error ("local variable `%D' may not appear in this context",
2943                            decl);
2944                     return error_mark_node;
2945                   }
2946               }
2947           }
2948
2949         decl = finish_id_expression (id_expression, decl, parser->scope,
2950                                      idk, qualifying_class,
2951                                      parser->integral_constant_expression_p,
2952                                      parser->allow_non_integral_constant_expression_p,
2953                                      &parser->non_integral_constant_expression_p,
2954                                      &error_msg);
2955         if (error_msg)
2956           cp_parser_error (parser, error_msg);
2957         return decl;
2958       }
2959
2960       /* Anything else is an error.  */
2961     default:
2962       cp_parser_error (parser, "expected primary-expression");
2963       return error_mark_node;
2964     }
2965 }
2966
2967 /* Parse an id-expression.
2968
2969    id-expression:
2970      unqualified-id
2971      qualified-id
2972
2973    qualified-id:
2974      :: [opt] nested-name-specifier template [opt] unqualified-id
2975      :: identifier
2976      :: operator-function-id
2977      :: template-id
2978
2979    Return a representation of the unqualified portion of the
2980    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2981    a `::' or nested-name-specifier.
2982
2983    Often, if the id-expression was a qualified-id, the caller will
2984    want to make a SCOPE_REF to represent the qualified-id.  This
2985    function does not do this in order to avoid wastefully creating
2986    SCOPE_REFs when they are not required.
2987
2988    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2989    `template' keyword.
2990
2991    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2992    uninstantiated templates.
2993
2994    If *TEMPLATE_P is non-NULL, it is set to true iff the
2995    `template' keyword is used to explicitly indicate that the entity
2996    named is a template.
2997
2998    If DECLARATOR_P is true, the id-expression is appearing as part of
2999    a declarator, rather than as part of an expression.  */
3000
3001 static tree
3002 cp_parser_id_expression (cp_parser *parser,
3003                          bool template_keyword_p,
3004                          bool check_dependency_p,
3005                          bool *template_p,
3006                          bool declarator_p)
3007 {
3008   bool global_scope_p;
3009   bool nested_name_specifier_p;
3010
3011   /* Assume the `template' keyword was not used.  */
3012   if (template_p)
3013     *template_p = false;
3014
3015   /* Look for the optional `::' operator.  */
3016   global_scope_p
3017     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3018        != NULL_TREE);
3019   /* Look for the optional nested-name-specifier.  */
3020   nested_name_specifier_p
3021     = (cp_parser_nested_name_specifier_opt (parser,
3022                                             /*typename_keyword_p=*/false,
3023                                             check_dependency_p,
3024                                             /*type_p=*/false,
3025                                             /*is_declarator=*/false)
3026        != NULL_TREE);
3027   /* If there is a nested-name-specifier, then we are looking at
3028      the first qualified-id production.  */
3029   if (nested_name_specifier_p)
3030     {
3031       tree saved_scope;
3032       tree saved_object_scope;
3033       tree saved_qualifying_scope;
3034       tree unqualified_id;
3035       bool is_template;
3036
3037       /* See if the next token is the `template' keyword.  */
3038       if (!template_p)
3039         template_p = &is_template;
3040       *template_p = cp_parser_optional_template_keyword (parser);
3041       /* Name lookup we do during the processing of the
3042          unqualified-id might obliterate SCOPE.  */
3043       saved_scope = parser->scope;
3044       saved_object_scope = parser->object_scope;
3045       saved_qualifying_scope = parser->qualifying_scope;
3046       /* Process the final unqualified-id.  */
3047       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3048                                                  check_dependency_p,
3049                                                  declarator_p);
3050       /* Restore the SAVED_SCOPE for our caller.  */
3051       parser->scope = saved_scope;
3052       parser->object_scope = saved_object_scope;
3053       parser->qualifying_scope = saved_qualifying_scope;
3054
3055       return unqualified_id;
3056     }
3057   /* Otherwise, if we are in global scope, then we are looking at one
3058      of the other qualified-id productions.  */
3059   else if (global_scope_p)
3060     {
3061       cp_token *token;
3062       tree id;
3063
3064       /* Peek at the next token.  */
3065       token = cp_lexer_peek_token (parser->lexer);
3066
3067       /* If it's an identifier, and the next token is not a "<", then
3068          we can avoid the template-id case.  This is an optimization
3069          for this common case.  */
3070       if (token->type == CPP_NAME
3071           && !cp_parser_nth_token_starts_template_argument_list_p
3072                (parser, 2))
3073         return cp_parser_identifier (parser);
3074
3075       cp_parser_parse_tentatively (parser);
3076       /* Try a template-id.  */
3077       id = cp_parser_template_id (parser,
3078                                   /*template_keyword_p=*/false,
3079                                   /*check_dependency_p=*/true,
3080                                   declarator_p);
3081       /* If that worked, we're done.  */
3082       if (cp_parser_parse_definitely (parser))
3083         return id;
3084
3085       /* Peek at the next token.  (Changes in the token buffer may
3086          have invalidated the pointer obtained above.)  */
3087       token = cp_lexer_peek_token (parser->lexer);
3088
3089       switch (token->type)
3090         {
3091         case CPP_NAME:
3092           return cp_parser_identifier (parser);
3093
3094         case CPP_KEYWORD:
3095           if (token->keyword == RID_OPERATOR)
3096             return cp_parser_operator_function_id (parser);
3097           /* Fall through.  */
3098
3099         default:
3100           cp_parser_error (parser, "expected id-expression");
3101           return error_mark_node;
3102         }
3103     }
3104   else
3105     return cp_parser_unqualified_id (parser, template_keyword_p,
3106                                      /*check_dependency_p=*/true,
3107                                      declarator_p);
3108 }
3109
3110 /* Parse an unqualified-id.
3111
3112    unqualified-id:
3113      identifier
3114      operator-function-id
3115      conversion-function-id
3116      ~ class-name
3117      template-id
3118
3119    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3120    keyword, in a construct like `A::template ...'.
3121
3122    Returns a representation of unqualified-id.  For the `identifier'
3123    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3124    production a BIT_NOT_EXPR is returned; the operand of the
3125    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3126    other productions, see the documentation accompanying the
3127    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3128    names are looked up in uninstantiated templates.  If DECLARATOR_P
3129    is true, the unqualified-id is appearing as part of a declarator,
3130    rather than as part of an expression.  */
3131
3132 static tree
3133 cp_parser_unqualified_id (cp_parser* parser,
3134                           bool template_keyword_p,
3135                           bool check_dependency_p,
3136                           bool declarator_p)
3137 {
3138   cp_token *token;
3139
3140   /* Peek at the next token.  */
3141   token = cp_lexer_peek_token (parser->lexer);
3142
3143   switch (token->type)
3144     {
3145     case CPP_NAME:
3146       {
3147         tree id;
3148
3149         /* We don't know yet whether or not this will be a
3150            template-id.  */
3151         cp_parser_parse_tentatively (parser);
3152         /* Try a template-id.  */
3153         id = cp_parser_template_id (parser, template_keyword_p,
3154                                     check_dependency_p,
3155                                     declarator_p);
3156         /* If it worked, we're done.  */
3157         if (cp_parser_parse_definitely (parser))
3158           return id;
3159         /* Otherwise, it's an ordinary identifier.  */
3160         return cp_parser_identifier (parser);
3161       }
3162
3163     case CPP_TEMPLATE_ID:
3164       return cp_parser_template_id (parser, template_keyword_p,
3165                                     check_dependency_p,
3166                                     declarator_p);
3167
3168     case CPP_COMPL:
3169       {
3170         tree type_decl;
3171         tree qualifying_scope;
3172         tree object_scope;
3173         tree scope;
3174
3175         /* Consume the `~' token.  */
3176         cp_lexer_consume_token (parser->lexer);
3177         /* Parse the class-name.  The standard, as written, seems to
3178            say that:
3179
3180              template <typename T> struct S { ~S (); };
3181              template <typename T> S<T>::~S() {}
3182
3183            is invalid, since `~' must be followed by a class-name, but
3184            `S<T>' is dependent, and so not known to be a class.
3185            That's not right; we need to look in uninstantiated
3186            templates.  A further complication arises from:
3187
3188              template <typename T> void f(T t) {
3189                t.T::~T();
3190              }
3191
3192            Here, it is not possible to look up `T' in the scope of `T'
3193            itself.  We must look in both the current scope, and the
3194            scope of the containing complete expression.
3195
3196            Yet another issue is:
3197
3198              struct S {
3199                int S;
3200                ~S();
3201              };
3202
3203              S::~S() {}
3204
3205            The standard does not seem to say that the `S' in `~S'
3206            should refer to the type `S' and not the data member
3207            `S::S'.  */
3208
3209         /* DR 244 says that we look up the name after the "~" in the
3210            same scope as we looked up the qualifying name.  That idea
3211            isn't fully worked out; it's more complicated than that.  */
3212         scope = parser->scope;
3213         object_scope = parser->object_scope;
3214         qualifying_scope = parser->qualifying_scope;
3215
3216         /* If the name is of the form "X::~X" it's OK.  */
3217         if (scope && TYPE_P (scope)
3218             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3219             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3220                 == CPP_OPEN_PAREN)
3221             && (cp_lexer_peek_token (parser->lexer)->value
3222                 == TYPE_IDENTIFIER (scope)))
3223           {
3224             cp_lexer_consume_token (parser->lexer);
3225             return build_nt (BIT_NOT_EXPR, scope);
3226           }
3227
3228         /* If there was an explicit qualification (S::~T), first look
3229            in the scope given by the qualification (i.e., S).  */
3230         if (scope)
3231           {
3232             cp_parser_parse_tentatively (parser);
3233             type_decl = cp_parser_class_name (parser,
3234                                               /*typename_keyword_p=*/false,
3235                                               /*template_keyword_p=*/false,
3236                                               /*type_p=*/false,
3237                                               /*check_dependency=*/false,
3238                                               /*class_head_p=*/false,
3239                                               declarator_p);
3240             if (cp_parser_parse_definitely (parser))
3241               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3242           }
3243         /* In "N::S::~S", look in "N" as well.  */
3244         if (scope && qualifying_scope)
3245           {
3246             cp_parser_parse_tentatively (parser);
3247             parser->scope = qualifying_scope;
3248             parser->object_scope = NULL_TREE;
3249             parser->qualifying_scope = NULL_TREE;
3250             type_decl
3251               = cp_parser_class_name (parser,
3252                                       /*typename_keyword_p=*/false,
3253                                       /*template_keyword_p=*/false,
3254                                       /*type_p=*/false,
3255                                       /*check_dependency=*/false,
3256                                       /*class_head_p=*/false,
3257                                       declarator_p);
3258             if (cp_parser_parse_definitely (parser))
3259               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3260           }
3261         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3262         else if (object_scope)
3263           {
3264             cp_parser_parse_tentatively (parser);
3265             parser->scope = object_scope;
3266             parser->object_scope = NULL_TREE;
3267             parser->qualifying_scope = NULL_TREE;
3268             type_decl
3269               = cp_parser_class_name (parser,
3270                                       /*typename_keyword_p=*/false,
3271                                       /*template_keyword_p=*/false,
3272                                       /*type_p=*/false,
3273                                       /*check_dependency=*/false,
3274                                       /*class_head_p=*/false,
3275                                       declarator_p);
3276             if (cp_parser_parse_definitely (parser))
3277               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3278           }
3279         /* Look in the surrounding context.  */
3280         parser->scope = NULL_TREE;
3281         parser->object_scope = NULL_TREE;
3282         parser->qualifying_scope = NULL_TREE;
3283         type_decl
3284           = cp_parser_class_name (parser,
3285                                   /*typename_keyword_p=*/false,
3286                                   /*template_keyword_p=*/false,
3287                                   /*type_p=*/false,
3288                                   /*check_dependency=*/false,
3289                                   /*class_head_p=*/false,
3290                                   declarator_p);
3291         /* If an error occurred, assume that the name of the
3292            destructor is the same as the name of the qualifying
3293            class.  That allows us to keep parsing after running
3294            into ill-formed destructor names.  */
3295         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3296           return build_nt (BIT_NOT_EXPR, scope);
3297         else if (type_decl == error_mark_node)
3298           return error_mark_node;
3299
3300         /* [class.dtor]
3301
3302            A typedef-name that names a class shall not be used as the
3303            identifier in the declarator for a destructor declaration.  */
3304         if (declarator_p
3305             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3306             && !DECL_SELF_REFERENCE_P (type_decl))
3307           error ("typedef-name `%D' used as destructor declarator",
3308                  type_decl);
3309
3310         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3311       }
3312
3313     case CPP_KEYWORD:
3314       if (token->keyword == RID_OPERATOR)
3315         {
3316           tree id;
3317
3318           /* This could be a template-id, so we try that first.  */
3319           cp_parser_parse_tentatively (parser);
3320           /* Try a template-id.  */
3321           id = cp_parser_template_id (parser, template_keyword_p,
3322                                       /*check_dependency_p=*/true,
3323                                       declarator_p);
3324           /* If that worked, we're done.  */
3325           if (cp_parser_parse_definitely (parser))
3326             return id;
3327           /* We still don't know whether we're looking at an
3328              operator-function-id or a conversion-function-id.  */
3329           cp_parser_parse_tentatively (parser);
3330           /* Try an operator-function-id.  */
3331           id = cp_parser_operator_function_id (parser);
3332           /* If that didn't work, try a conversion-function-id.  */
3333           if (!cp_parser_parse_definitely (parser))
3334             id = cp_parser_conversion_function_id (parser);
3335
3336           return id;
3337         }
3338       /* Fall through.  */
3339
3340     default:
3341       cp_parser_error (parser, "expected unqualified-id");
3342       return error_mark_node;
3343     }
3344 }
3345
3346 /* Parse an (optional) nested-name-specifier.
3347
3348    nested-name-specifier:
3349      class-or-namespace-name :: nested-name-specifier [opt]
3350      class-or-namespace-name :: template nested-name-specifier [opt]
3351
3352    PARSER->SCOPE should be set appropriately before this function is
3353    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3354    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3355    in name lookups.
3356
3357    Sets PARSER->SCOPE to the class (TYPE) or namespace
3358    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3359    it unchanged if there is no nested-name-specifier.  Returns the new
3360    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3361
3362    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3363    part of a declaration and/or decl-specifier.  */
3364
3365 static tree
3366 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3367                                      bool typename_keyword_p,
3368                                      bool check_dependency_p,
3369                                      bool type_p,
3370                                      bool is_declaration)
3371 {
3372   bool success = false;
3373   tree access_check = NULL_TREE;
3374   ptrdiff_t start;
3375   cp_token* token;
3376
3377   /* If the next token corresponds to a nested name specifier, there
3378      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3379      false, it may have been true before, in which case something
3380      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3381      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3382      CHECK_DEPENDENCY_P is false, we have to fall through into the
3383      main loop.  */
3384   if (check_dependency_p
3385       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3386     {
3387       cp_parser_pre_parsed_nested_name_specifier (parser);
3388       return parser->scope;
3389     }
3390
3391   /* Remember where the nested-name-specifier starts.  */
3392   if (cp_parser_parsing_tentatively (parser)
3393       && !cp_parser_committed_to_tentative_parse (parser))
3394     {
3395       token = cp_lexer_peek_token (parser->lexer);
3396       start = cp_lexer_token_difference (parser->lexer,
3397                                          parser->lexer->first_token,
3398                                          token);
3399     }
3400   else
3401     start = -1;
3402
3403   push_deferring_access_checks (dk_deferred);
3404
3405   while (true)
3406     {
3407       tree new_scope;
3408       tree old_scope;
3409       tree saved_qualifying_scope;
3410       bool template_keyword_p;
3411
3412       /* Spot cases that cannot be the beginning of a
3413          nested-name-specifier.  */
3414       token = cp_lexer_peek_token (parser->lexer);
3415
3416       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3417          the already parsed nested-name-specifier.  */
3418       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3419         {
3420           /* Grab the nested-name-specifier and continue the loop.  */
3421           cp_parser_pre_parsed_nested_name_specifier (parser);
3422           success = true;
3423           continue;
3424         }
3425
3426       /* Spot cases that cannot be the beginning of a
3427          nested-name-specifier.  On the second and subsequent times
3428          through the loop, we look for the `template' keyword.  */
3429       if (success && token->keyword == RID_TEMPLATE)
3430         ;
3431       /* A template-id can start a nested-name-specifier.  */
3432       else if (token->type == CPP_TEMPLATE_ID)
3433         ;
3434       else
3435         {
3436           /* If the next token is not an identifier, then it is
3437              definitely not a class-or-namespace-name.  */
3438           if (token->type != CPP_NAME)
3439             break;
3440           /* If the following token is neither a `<' (to begin a
3441              template-id), nor a `::', then we are not looking at a
3442              nested-name-specifier.  */
3443           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3444           if (token->type != CPP_SCOPE
3445               && !cp_parser_nth_token_starts_template_argument_list_p
3446                   (parser, 2))
3447             break;
3448         }
3449
3450       /* The nested-name-specifier is optional, so we parse
3451          tentatively.  */
3452       cp_parser_parse_tentatively (parser);
3453
3454       /* Look for the optional `template' keyword, if this isn't the
3455          first time through the loop.  */
3456       if (success)
3457         template_keyword_p = cp_parser_optional_template_keyword (parser);
3458       else
3459         template_keyword_p = false;
3460
3461       /* Save the old scope since the name lookup we are about to do
3462          might destroy it.  */
3463       old_scope = parser->scope;
3464       saved_qualifying_scope = parser->qualifying_scope;
3465       /* Parse the qualifying entity.  */
3466       new_scope
3467         = cp_parser_class_or_namespace_name (parser,
3468                                              typename_keyword_p,
3469                                              template_keyword_p,
3470                                              check_dependency_p,
3471                                              type_p,
3472                                              is_declaration);
3473       /* Look for the `::' token.  */
3474       cp_parser_require (parser, CPP_SCOPE, "`::'");
3475
3476       /* If we found what we wanted, we keep going; otherwise, we're
3477          done.  */
3478       if (!cp_parser_parse_definitely (parser))
3479         {
3480           bool error_p = false;
3481
3482           /* Restore the OLD_SCOPE since it was valid before the
3483              failed attempt at finding the last
3484              class-or-namespace-name.  */
3485           parser->scope = old_scope;
3486           parser->qualifying_scope = saved_qualifying_scope;
3487           /* If the next token is an identifier, and the one after
3488              that is a `::', then any valid interpretation would have
3489              found a class-or-namespace-name.  */
3490           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3491                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3492                      == CPP_SCOPE)
3493                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3494                      != CPP_COMPL))
3495             {
3496               token = cp_lexer_consume_token (parser->lexer);
3497               if (!error_p)
3498                 {
3499                   tree decl;
3500
3501                   decl = cp_parser_lookup_name_simple (parser, token->value);
3502                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3503                     error ("`%D' used without template parameters",
3504                            decl);
3505                   else
3506                     cp_parser_name_lookup_error
3507                       (parser, token->value, decl,
3508                        "is not a class or namespace");
3509                   parser->scope = NULL_TREE;
3510                   error_p = true;
3511                   /* Treat this as a successful nested-name-specifier
3512                      due to:
3513
3514                      [basic.lookup.qual]
3515
3516                      If the name found is not a class-name (clause
3517                      _class_) or namespace-name (_namespace.def_), the
3518                      program is ill-formed.  */
3519                   success = true;
3520                 }
3521               cp_lexer_consume_token (parser->lexer);
3522             }
3523           break;
3524         }
3525
3526       /* We've found one valid nested-name-specifier.  */
3527       success = true;
3528       /* Make sure we look in the right scope the next time through
3529          the loop.  */
3530       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3531                        ? TREE_TYPE (new_scope)
3532                        : new_scope);
3533       /* If it is a class scope, try to complete it; we are about to
3534          be looking up names inside the class.  */
3535       if (TYPE_P (parser->scope)
3536           /* Since checking types for dependency can be expensive,
3537              avoid doing it if the type is already complete.  */
3538           && !COMPLETE_TYPE_P (parser->scope)
3539           /* Do not try to complete dependent types.  */
3540           && !dependent_type_p (parser->scope))
3541         complete_type (parser->scope);
3542     }
3543
3544   /* Retrieve any deferred checks.  Do not pop this access checks yet
3545      so the memory will not be reclaimed during token replacing below.  */
3546   access_check = get_deferred_access_checks ();
3547
3548   /* If parsing tentatively, replace the sequence of tokens that makes
3549      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3550      token.  That way, should we re-parse the token stream, we will
3551      not have to repeat the effort required to do the parse, nor will
3552      we issue duplicate error messages.  */
3553   if (success && start >= 0)
3554     {
3555       /* Find the token that corresponds to the start of the
3556          template-id.  */
3557       token = cp_lexer_advance_token (parser->lexer,
3558                                       parser->lexer->first_token,
3559                                       start);
3560
3561       /* Reset the contents of the START token.  */
3562       token->type = CPP_NESTED_NAME_SPECIFIER;
3563       token->value = build_tree_list (access_check, parser->scope);
3564       TREE_TYPE (token->value) = parser->qualifying_scope;
3565       token->keyword = RID_MAX;
3566       /* Purge all subsequent tokens.  */
3567       cp_lexer_purge_tokens_after (parser->lexer, token);
3568     }
3569
3570   pop_deferring_access_checks ();
3571   return success ? parser->scope : NULL_TREE;
3572 }
3573
3574 /* Parse a nested-name-specifier.  See
3575    cp_parser_nested_name_specifier_opt for details.  This function
3576    behaves identically, except that it will an issue an error if no
3577    nested-name-specifier is present, and it will return
3578    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3579    is present.  */
3580
3581 static tree
3582 cp_parser_nested_name_specifier (cp_parser *parser,
3583                                  bool typename_keyword_p,
3584                                  bool check_dependency_p,
3585                                  bool type_p,
3586                                  bool is_declaration)
3587 {
3588   tree scope;
3589
3590   /* Look for the nested-name-specifier.  */
3591   scope = cp_parser_nested_name_specifier_opt (parser,
3592                                                typename_keyword_p,
3593                                                check_dependency_p,
3594                                                type_p,
3595                                                is_declaration);
3596   /* If it was not present, issue an error message.  */
3597   if (!scope)
3598     {
3599       cp_parser_error (parser, "expected nested-name-specifier");
3600       parser->scope = NULL_TREE;
3601       return error_mark_node;
3602     }
3603
3604   return scope;
3605 }
3606
3607 /* Parse a class-or-namespace-name.
3608
3609    class-or-namespace-name:
3610      class-name
3611      namespace-name
3612
3613    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3614    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3615    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3616    TYPE_P is TRUE iff the next name should be taken as a class-name,
3617    even the same name is declared to be another entity in the same
3618    scope.
3619
3620    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3621    specified by the class-or-namespace-name.  If neither is found the
3622    ERROR_MARK_NODE is returned.  */
3623
3624 static tree
3625 cp_parser_class_or_namespace_name (cp_parser *parser,
3626                                    bool typename_keyword_p,
3627                                    bool template_keyword_p,
3628                                    bool check_dependency_p,
3629                                    bool type_p,
3630                                    bool is_declaration)
3631 {
3632   tree saved_scope;
3633   tree saved_qualifying_scope;
3634   tree saved_object_scope;
3635   tree scope;
3636   bool only_class_p;
3637
3638   /* Before we try to parse the class-name, we must save away the
3639      current PARSER->SCOPE since cp_parser_class_name will destroy
3640      it.  */
3641   saved_scope = parser->scope;
3642   saved_qualifying_scope = parser->qualifying_scope;
3643   saved_object_scope = parser->object_scope;
3644   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3645      there is no need to look for a namespace-name.  */
3646   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3647   if (!only_class_p)
3648     cp_parser_parse_tentatively (parser);
3649   scope = cp_parser_class_name (parser,
3650                                 typename_keyword_p,
3651                                 template_keyword_p,
3652                                 type_p,
3653                                 check_dependency_p,
3654                                 /*class_head_p=*/false,
3655                                 is_declaration);
3656   /* If that didn't work, try for a namespace-name.  */
3657   if (!only_class_p && !cp_parser_parse_definitely (parser))
3658     {
3659       /* Restore the saved scope.  */
3660       parser->scope = saved_scope;
3661       parser->qualifying_scope = saved_qualifying_scope;
3662       parser->object_scope = saved_object_scope;
3663       /* If we are not looking at an identifier followed by the scope
3664          resolution operator, then this is not part of a
3665          nested-name-specifier.  (Note that this function is only used
3666          to parse the components of a nested-name-specifier.)  */
3667       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3668           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3669         return error_mark_node;
3670       scope = cp_parser_namespace_name (parser);
3671     }
3672
3673   return scope;
3674 }
3675
3676 /* Parse a postfix-expression.
3677
3678    postfix-expression:
3679      primary-expression
3680      postfix-expression [ expression ]
3681      postfix-expression ( expression-list [opt] )
3682      simple-type-specifier ( expression-list [opt] )
3683      typename :: [opt] nested-name-specifier identifier
3684        ( expression-list [opt] )
3685      typename :: [opt] nested-name-specifier template [opt] template-id
3686        ( expression-list [opt] )
3687      postfix-expression . template [opt] id-expression
3688      postfix-expression -> template [opt] id-expression
3689      postfix-expression . pseudo-destructor-name
3690      postfix-expression -> pseudo-destructor-name
3691      postfix-expression ++
3692      postfix-expression --
3693      dynamic_cast < type-id > ( expression )
3694      static_cast < type-id > ( expression )
3695      reinterpret_cast < type-id > ( expression )
3696      const_cast < type-id > ( expression )
3697      typeid ( expression )
3698      typeid ( type-id )
3699
3700    GNU Extension:
3701
3702    postfix-expression:
3703      ( type-id ) { initializer-list , [opt] }
3704
3705    This extension is a GNU version of the C99 compound-literal
3706    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3707    but they are essentially the same concept.)
3708
3709    If ADDRESS_P is true, the postfix expression is the operand of the
3710    `&' operator.
3711
3712    Returns a representation of the expression.  */
3713
3714 static tree
3715 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3716 {
3717   cp_token *token;
3718   enum rid keyword;
3719   cp_id_kind idk = CP_ID_KIND_NONE;
3720   tree postfix_expression = NULL_TREE;
3721   /* Non-NULL only if the current postfix-expression can be used to
3722      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3723      class used to qualify the member.  */
3724   tree qualifying_class = NULL_TREE;
3725
3726   /* Peek at the next token.  */
3727   token = cp_lexer_peek_token (parser->lexer);
3728   /* Some of the productions are determined by keywords.  */
3729   keyword = token->keyword;
3730   switch (keyword)
3731     {
3732     case RID_DYNCAST:
3733     case RID_STATCAST:
3734     case RID_REINTCAST:
3735     case RID_CONSTCAST:
3736       {
3737         tree type;
3738         tree expression;
3739         const char *saved_message;
3740
3741         /* All of these can be handled in the same way from the point
3742            of view of parsing.  Begin by consuming the token
3743            identifying the cast.  */
3744         cp_lexer_consume_token (parser->lexer);
3745
3746         /* New types cannot be defined in the cast.  */
3747         saved_message = parser->type_definition_forbidden_message;
3748         parser->type_definition_forbidden_message
3749           = "types may not be defined in casts";
3750
3751         /* Look for the opening `<'.  */
3752         cp_parser_require (parser, CPP_LESS, "`<'");
3753         /* Parse the type to which we are casting.  */
3754         type = cp_parser_type_id (parser);
3755         /* Look for the closing `>'.  */
3756         cp_parser_require (parser, CPP_GREATER, "`>'");
3757         /* Restore the old message.  */
3758         parser->type_definition_forbidden_message = saved_message;
3759
3760         /* And the expression which is being cast.  */
3761         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3762         expression = cp_parser_expression (parser);
3763         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3764
3765         /* Only type conversions to integral or enumeration types
3766            can be used in constant-expressions.  */
3767         if (parser->integral_constant_expression_p
3768             && !dependent_type_p (type)
3769             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3770             && (cp_parser_non_integral_constant_expression 
3771                 (parser,
3772                  "a cast to a type other than an integral or "
3773                  "enumeration type")))
3774           return error_mark_node;
3775
3776         switch (keyword)
3777           {
3778           case RID_DYNCAST:
3779             postfix_expression
3780               = build_dynamic_cast (type, expression);
3781             break;
3782           case RID_STATCAST:
3783             postfix_expression
3784               = build_static_cast (type, expression);
3785             break;
3786           case RID_REINTCAST:
3787             postfix_expression
3788               = build_reinterpret_cast (type, expression);
3789             break;
3790           case RID_CONSTCAST:
3791             postfix_expression
3792               = build_const_cast (type, expression);
3793             break;
3794           default:
3795             abort ();
3796           }
3797       }
3798       break;
3799
3800     case RID_TYPEID:
3801       {
3802         tree type;
3803         const char *saved_message;
3804         bool saved_in_type_id_in_expr_p;
3805
3806         /* Consume the `typeid' token.  */
3807         cp_lexer_consume_token (parser->lexer);
3808         /* Look for the `(' token.  */
3809         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3810         /* Types cannot be defined in a `typeid' expression.  */
3811         saved_message = parser->type_definition_forbidden_message;
3812         parser->type_definition_forbidden_message
3813           = "types may not be defined in a `typeid\' expression";
3814         /* We can't be sure yet whether we're looking at a type-id or an
3815            expression.  */
3816         cp_parser_parse_tentatively (parser);
3817         /* Try a type-id first.  */
3818         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3819         parser->in_type_id_in_expr_p = true;
3820         type = cp_parser_type_id (parser);
3821         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3822         /* Look for the `)' token.  Otherwise, we can't be sure that
3823            we're not looking at an expression: consider `typeid (int
3824            (3))', for example.  */
3825         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3826         /* If all went well, simply lookup the type-id.  */
3827         if (cp_parser_parse_definitely (parser))
3828           postfix_expression = get_typeid (type);
3829         /* Otherwise, fall back to the expression variant.  */
3830         else
3831           {
3832             tree expression;
3833
3834             /* Look for an expression.  */
3835             expression = cp_parser_expression (parser);
3836             /* Compute its typeid.  */
3837             postfix_expression = build_typeid (expression);
3838             /* Look for the `)' token.  */
3839             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3840           }
3841         /* `typeid' may not appear in an integral constant expression.  */
3842         if (cp_parser_non_integral_constant_expression(parser, 
3843                                                        "`typeid' operator"))
3844           return error_mark_node;
3845         /* Restore the saved message.  */
3846         parser->type_definition_forbidden_message = saved_message;
3847       }
3848       break;
3849
3850     case RID_TYPENAME:
3851       {
3852         bool template_p = false;
3853         tree id;
3854         tree type;
3855
3856         /* Consume the `typename' token.  */
3857         cp_lexer_consume_token (parser->lexer);
3858         /* Look for the optional `::' operator.  */
3859         cp_parser_global_scope_opt (parser,
3860                                     /*current_scope_valid_p=*/false);
3861         /* Look for the nested-name-specifier.  */
3862         cp_parser_nested_name_specifier (parser,
3863                                          /*typename_keyword_p=*/true,
3864                                          /*check_dependency_p=*/true,
3865                                          /*type_p=*/true,
3866                                          /*is_declaration=*/true);
3867         /* Look for the optional `template' keyword.  */
3868         template_p = cp_parser_optional_template_keyword (parser);
3869         /* We don't know whether we're looking at a template-id or an
3870            identifier.  */
3871         cp_parser_parse_tentatively (parser);
3872         /* Try a template-id.  */
3873         id = cp_parser_template_id (parser, template_p,
3874                                     /*check_dependency_p=*/true,
3875                                     /*is_declaration=*/true);
3876         /* If that didn't work, try an identifier.  */
3877         if (!cp_parser_parse_definitely (parser))
3878           id = cp_parser_identifier (parser);
3879         /* If we look up a template-id in a non-dependent qualifying
3880            scope, there's no need to create a dependent type.  */
3881         if (TREE_CODE (id) == TYPE_DECL
3882             && !dependent_type_p (parser->scope))
3883           type = TREE_TYPE (id);
3884         /* Create a TYPENAME_TYPE to represent the type to which the
3885            functional cast is being performed.  */
3886         else
3887           type = make_typename_type (parser->scope, id, 
3888                                      /*complain=*/1);
3889
3890         postfix_expression = cp_parser_functional_cast (parser, type);
3891       }
3892       break;
3893
3894     default:
3895       {
3896         tree type;
3897
3898         /* If the next thing is a simple-type-specifier, we may be
3899            looking at a functional cast.  We could also be looking at
3900            an id-expression.  So, we try the functional cast, and if
3901            that doesn't work we fall back to the primary-expression.  */
3902         cp_parser_parse_tentatively (parser);
3903         /* Look for the simple-type-specifier.  */
3904         type = cp_parser_simple_type_specifier (parser,
3905                                                 /*decl_specs=*/NULL,
3906                                                 CP_PARSER_FLAGS_NONE);
3907         /* Parse the cast itself.  */
3908         if (!cp_parser_error_occurred (parser))
3909           postfix_expression
3910             = cp_parser_functional_cast (parser, type);
3911         /* If that worked, we're done.  */
3912         if (cp_parser_parse_definitely (parser))
3913           break;
3914
3915         /* If the functional-cast didn't work out, try a
3916            compound-literal.  */
3917         if (cp_parser_allow_gnu_extensions_p (parser)
3918             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3919           {
3920             tree initializer_list = NULL_TREE;
3921             bool saved_in_type_id_in_expr_p;
3922
3923             cp_parser_parse_tentatively (parser);
3924             /* Consume the `('.  */
3925             cp_lexer_consume_token (parser->lexer);
3926             /* Parse the type.  */
3927             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3928             parser->in_type_id_in_expr_p = true;
3929             type = cp_parser_type_id (parser);
3930             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3931             /* Look for the `)'.  */
3932             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3933             /* Look for the `{'.  */
3934             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3935             /* If things aren't going well, there's no need to
3936                keep going.  */
3937             if (!cp_parser_error_occurred (parser))
3938               {
3939                 bool non_constant_p;
3940                 /* Parse the initializer-list.  */
3941                 initializer_list
3942                   = cp_parser_initializer_list (parser, &non_constant_p);
3943                 /* Allow a trailing `,'.  */
3944                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3945                   cp_lexer_consume_token (parser->lexer);
3946                 /* Look for the final `}'.  */
3947                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3948               }
3949             /* If that worked, we're definitely looking at a
3950                compound-literal expression.  */
3951             if (cp_parser_parse_definitely (parser))
3952               {
3953                 /* Warn the user that a compound literal is not
3954                    allowed in standard C++.  */
3955                 if (pedantic)
3956                   pedwarn ("ISO C++ forbids compound-literals");
3957                 /* Form the representation of the compound-literal.  */
3958                 postfix_expression
3959                   = finish_compound_literal (type, initializer_list);
3960                 break;
3961               }
3962           }
3963
3964         /* It must be a primary-expression.  */
3965         postfix_expression = cp_parser_primary_expression (parser,
3966                                                            &idk,
3967                                                            &qualifying_class);
3968       }
3969       break;
3970     }
3971
3972   /* If we were avoiding committing to the processing of a
3973      qualified-id until we knew whether or not we had a
3974      pointer-to-member, we now know.  */
3975   if (qualifying_class)
3976     {
3977       bool done;
3978
3979       /* Peek at the next token.  */
3980       token = cp_lexer_peek_token (parser->lexer);
3981       done = (token->type != CPP_OPEN_SQUARE
3982               && token->type != CPP_OPEN_PAREN
3983               && token->type != CPP_DOT
3984               && token->type != CPP_DEREF
3985               && token->type != CPP_PLUS_PLUS
3986               && token->type != CPP_MINUS_MINUS);
3987
3988       postfix_expression = finish_qualified_id_expr (qualifying_class,
3989                                                      postfix_expression,
3990                                                      done,
3991                                                      address_p);
3992       if (done)
3993         return postfix_expression;
3994     }
3995
3996   /* Keep looping until the postfix-expression is complete.  */
3997   while (true)
3998     {
3999       if (idk == CP_ID_KIND_UNQUALIFIED
4000           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4001           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4002         /* It is not a Koenig lookup function call.  */
4003         postfix_expression
4004           = unqualified_name_lookup_error (postfix_expression);
4005
4006       /* Peek at the next token.  */
4007       token = cp_lexer_peek_token (parser->lexer);
4008
4009       switch (token->type)
4010         {
4011         case CPP_OPEN_SQUARE:
4012           postfix_expression
4013             = cp_parser_postfix_open_square_expression (parser,
4014                                                         postfix_expression,
4015                                                         false);
4016           idk = CP_ID_KIND_NONE;
4017           break;
4018
4019         case CPP_OPEN_PAREN:
4020           /* postfix-expression ( expression-list [opt] ) */
4021           {
4022             bool koenig_p;
4023             tree args = (cp_parser_parenthesized_expression_list
4024                          (parser, false, /*non_constant_p=*/NULL));
4025
4026             if (args == error_mark_node)
4027               {
4028                 postfix_expression = error_mark_node;
4029                 break;
4030               }
4031
4032             /* Function calls are not permitted in
4033                constant-expressions.  */
4034             if (cp_parser_non_integral_constant_expression (parser,
4035                                                             "a function call"))
4036               {
4037                 postfix_expression = error_mark_node;
4038                 break;
4039               }
4040
4041             koenig_p = false;
4042             if (idk == CP_ID_KIND_UNQUALIFIED)
4043               {
4044                 /* We do not perform argument-dependent lookup if
4045                    normal lookup finds a non-function, in accordance
4046                    with the expected resolution of DR 218.  */
4047                 if (args
4048                     && (is_overloaded_fn (postfix_expression)
4049                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4050                   {
4051                     koenig_p = true;
4052                     postfix_expression
4053                       = perform_koenig_lookup (postfix_expression, args);
4054                   }
4055                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4056                   postfix_expression
4057                     = unqualified_fn_lookup_error (postfix_expression);
4058               }
4059
4060             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4061               {
4062                 tree instance = TREE_OPERAND (postfix_expression, 0);
4063                 tree fn = TREE_OPERAND (postfix_expression, 1);
4064
4065                 if (processing_template_decl
4066                     && (type_dependent_expression_p (instance)
4067                         || (!BASELINK_P (fn)
4068                             && TREE_CODE (fn) != FIELD_DECL)
4069                         || type_dependent_expression_p (fn)
4070                         || any_type_dependent_arguments_p (args)))
4071                   {
4072                     postfix_expression
4073                       = build_min_nt (CALL_EXPR, postfix_expression,
4074                                       args, NULL_TREE);
4075                     break;
4076                   }
4077
4078                 if (BASELINK_P (fn))
4079                   postfix_expression
4080                     = (build_new_method_call
4081                        (instance, fn, args, NULL_TREE,
4082                         (idk == CP_ID_KIND_QUALIFIED
4083                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4084                 else
4085                   postfix_expression
4086                     = finish_call_expr (postfix_expression, args,
4087                                         /*disallow_virtual=*/false,
4088                                         /*koenig_p=*/false);
4089               }
4090             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4091                      || TREE_CODE (postfix_expression) == MEMBER_REF
4092                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4093               postfix_expression = (build_offset_ref_call_from_tree
4094                                     (postfix_expression, args));
4095             else if (idk == CP_ID_KIND_QUALIFIED)
4096               /* A call to a static class member, or a namespace-scope
4097                  function.  */
4098               postfix_expression
4099                 = finish_call_expr (postfix_expression, args,
4100                                     /*disallow_virtual=*/true,
4101                                     koenig_p);
4102             else
4103               /* All other function calls.  */
4104               postfix_expression
4105                 = finish_call_expr (postfix_expression, args,
4106                                     /*disallow_virtual=*/false,
4107                                     koenig_p);
4108
4109             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4110             idk = CP_ID_KIND_NONE;
4111           }
4112           break;
4113
4114         case CPP_DOT:
4115         case CPP_DEREF:
4116           /* postfix-expression . template [opt] id-expression
4117              postfix-expression . pseudo-destructor-name
4118              postfix-expression -> template [opt] id-expression
4119              postfix-expression -> pseudo-destructor-name */
4120         
4121           /* Consume the `.' or `->' operator.  */
4122           cp_lexer_consume_token (parser->lexer);
4123
4124           postfix_expression
4125             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4126                                                       postfix_expression,
4127                                                       false, &idk);
4128           break;
4129
4130         case CPP_PLUS_PLUS:
4131           /* postfix-expression ++  */
4132           /* Consume the `++' token.  */
4133           cp_lexer_consume_token (parser->lexer);
4134           /* Generate a representation for the complete expression.  */
4135           postfix_expression
4136             = finish_increment_expr (postfix_expression,
4137                                      POSTINCREMENT_EXPR);
4138           /* Increments may not appear in constant-expressions.  */
4139           if (cp_parser_non_integral_constant_expression (parser,
4140                                                           "an increment"))
4141             postfix_expression = error_mark_node;
4142           idk = CP_ID_KIND_NONE;
4143           break;
4144
4145         case CPP_MINUS_MINUS:
4146           /* postfix-expression -- */
4147           /* Consume the `--' token.  */
4148           cp_lexer_consume_token (parser->lexer);
4149           /* Generate a representation for the complete expression.  */
4150           postfix_expression
4151             = finish_increment_expr (postfix_expression,
4152                                      POSTDECREMENT_EXPR);
4153           /* Decrements may not appear in constant-expressions.  */
4154           if (cp_parser_non_integral_constant_expression (parser,
4155                                                           "a decrement"))
4156             postfix_expression = error_mark_node;
4157           idk = CP_ID_KIND_NONE;
4158           break;
4159
4160         default:
4161           return postfix_expression;
4162         }
4163     }
4164
4165   /* We should never get here.  */
4166   abort ();
4167   return error_mark_node;
4168 }
4169
4170 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4171    by cp_parser_builtin_offsetof.  We're looking for
4172
4173      postfix-expression [ expression ]
4174
4175    FOR_OFFSETOF is set if we're being called in that context, which
4176    changes how we deal with integer constant expressions.  */
4177
4178 static tree
4179 cp_parser_postfix_open_square_expression (cp_parser *parser,
4180                                           tree postfix_expression,
4181                                           bool for_offsetof)
4182 {
4183   tree index;
4184
4185   /* Consume the `[' token.  */
4186   cp_lexer_consume_token (parser->lexer);
4187
4188   /* Parse the index expression.  */
4189   /* ??? For offsetof, there is a question of what to allow here.  If
4190      offsetof is not being used in an integral constant expression context,
4191      then we *could* get the right answer by computing the value at runtime.
4192      If we are in an integral constant expression context, then we might
4193      could accept any constant expression; hard to say without analysis.
4194      Rather than open the barn door too wide right away, allow only integer
4195      constant expresions here.  */
4196   if (for_offsetof)
4197     index = cp_parser_constant_expression (parser, false, NULL);
4198   else
4199     index = cp_parser_expression (parser);
4200
4201   /* Look for the closing `]'.  */
4202   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4203
4204   /* Build the ARRAY_REF.  */
4205   postfix_expression = grok_array_decl (postfix_expression, index);
4206
4207   /* When not doing offsetof, array references are not permitted in
4208      constant-expressions.  */
4209   if (!for_offsetof
4210       && (cp_parser_non_integral_constant_expression
4211           (parser, "an array reference")))
4212     postfix_expression = error_mark_node;
4213
4214   return postfix_expression;
4215 }
4216
4217 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4218    by cp_parser_builtin_offsetof.  We're looking for
4219
4220      postfix-expression . template [opt] id-expression
4221      postfix-expression . pseudo-destructor-name
4222      postfix-expression -> template [opt] id-expression
4223      postfix-expression -> pseudo-destructor-name
4224
4225    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4226    limits what of the above we'll actually accept, but nevermind.
4227    TOKEN_TYPE is the "." or "->" token, which will already have been
4228    removed from the stream.  */
4229
4230 static tree
4231 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4232                                         enum cpp_ttype token_type,
4233                                         tree postfix_expression,
4234                                         bool for_offsetof, cp_id_kind *idk)
4235 {
4236   tree name;
4237   bool dependent_p;
4238   bool template_p;
4239   tree scope = NULL_TREE;
4240
4241   /* If this is a `->' operator, dereference the pointer.  */
4242   if (token_type == CPP_DEREF)
4243     postfix_expression = build_x_arrow (postfix_expression);
4244   /* Check to see whether or not the expression is type-dependent.  */
4245   dependent_p = type_dependent_expression_p (postfix_expression);
4246   /* The identifier following the `->' or `.' is not qualified.  */
4247   parser->scope = NULL_TREE;
4248   parser->qualifying_scope = NULL_TREE;
4249   parser->object_scope = NULL_TREE;
4250   *idk = CP_ID_KIND_NONE;
4251   /* Enter the scope corresponding to the type of the object
4252      given by the POSTFIX_EXPRESSION.  */
4253   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4254     {
4255       scope = TREE_TYPE (postfix_expression);
4256       /* According to the standard, no expression should ever have
4257          reference type.  Unfortunately, we do not currently match
4258          the standard in this respect in that our internal representation
4259          of an expression may have reference type even when the standard
4260          says it does not.  Therefore, we have to manually obtain the
4261          underlying type here.  */
4262       scope = non_reference (scope);
4263       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4264       scope = complete_type_or_else (scope, NULL_TREE);
4265       /* Let the name lookup machinery know that we are processing a
4266          class member access expression.  */
4267       parser->context->object_type = scope;
4268       /* If something went wrong, we want to be able to discern that case,
4269          as opposed to the case where there was no SCOPE due to the type
4270          of expression being dependent.  */
4271       if (!scope)
4272         scope = error_mark_node;
4273       /* If the SCOPE was erroneous, make the various semantic analysis
4274          functions exit quickly -- and without issuing additional error
4275          messages.  */
4276       if (scope == error_mark_node)
4277         postfix_expression = error_mark_node;
4278     }
4279
4280   /* If the SCOPE is not a scalar type, we are looking at an
4281      ordinary class member access expression, rather than a
4282      pseudo-destructor-name.  */
4283   if (!scope || !SCALAR_TYPE_P (scope))
4284     {
4285       template_p = cp_parser_optional_template_keyword (parser);
4286       /* Parse the id-expression.  */
4287       name = cp_parser_id_expression (parser, template_p,
4288                                       /*check_dependency_p=*/true,
4289                                       /*template_p=*/NULL,
4290                                       /*declarator_p=*/false);
4291       /* In general, build a SCOPE_REF if the member name is qualified.
4292          However, if the name was not dependent and has already been
4293          resolved; there is no need to build the SCOPE_REF.  For example;
4294
4295              struct X { void f(); };
4296              template <typename T> void f(T* t) { t->X::f(); }
4297
4298          Even though "t" is dependent, "X::f" is not and has been resolved
4299          to a BASELINK; there is no need to include scope information.  */
4300
4301       /* But we do need to remember that there was an explicit scope for
4302          virtual function calls.  */
4303       if (parser->scope)
4304         *idk = CP_ID_KIND_QUALIFIED;
4305
4306       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4307         {
4308           name = build_nt (SCOPE_REF, parser->scope, name);
4309           parser->scope = NULL_TREE;
4310           parser->qualifying_scope = NULL_TREE;
4311           parser->object_scope = NULL_TREE;
4312         }
4313       if (scope && name && BASELINK_P (name))
4314         adjust_result_of_qualified_name_lookup 
4315           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4316       postfix_expression
4317         = finish_class_member_access_expr (postfix_expression, name);
4318     }
4319   /* Otherwise, try the pseudo-destructor-name production.  */
4320   else
4321     {
4322       tree s = NULL_TREE;
4323       tree type;
4324
4325       /* Parse the pseudo-destructor-name.  */
4326       cp_parser_pseudo_destructor_name (parser, &s, &type);
4327       /* Form the call.  */
4328       postfix_expression
4329         = finish_pseudo_destructor_expr (postfix_expression,
4330                                          s, TREE_TYPE (type));
4331     }
4332
4333   /* We no longer need to look up names in the scope of the object on
4334      the left-hand side of the `.' or `->' operator.  */
4335   parser->context->object_type = NULL_TREE;
4336
4337   /* Outside of offsetof, these operators may not appear in
4338      constant-expressions.  */
4339   if (!for_offsetof
4340       && (cp_parser_non_integral_constant_expression 
4341           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4342     postfix_expression = error_mark_node;
4343
4344   return postfix_expression;
4345 }
4346
4347 /* Parse a parenthesized expression-list.
4348
4349    expression-list:
4350      assignment-expression
4351      expression-list, assignment-expression
4352
4353    attribute-list:
4354      expression-list
4355      identifier
4356      identifier, expression-list
4357
4358    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4359    representation of an assignment-expression.  Note that a TREE_LIST
4360    is returned even if there is only a single expression in the list.
4361    error_mark_node is returned if the ( and or ) are
4362    missing. NULL_TREE is returned on no expressions. The parentheses
4363    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4364    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4365    indicates whether or not all of the expressions in the list were
4366    constant.  */
4367
4368 static tree
4369 cp_parser_parenthesized_expression_list (cp_parser* parser,
4370                                          bool is_attribute_list,
4371                                          bool *non_constant_p)
4372 {
4373   tree expression_list = NULL_TREE;
4374   tree identifier = NULL_TREE;
4375
4376   /* Assume all the expressions will be constant.  */
4377   if (non_constant_p)
4378     *non_constant_p = false;
4379
4380   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4381     return error_mark_node;
4382
4383   /* Consume expressions until there are no more.  */
4384   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4385     while (true)
4386       {
4387         tree expr;
4388
4389         /* At the beginning of attribute lists, check to see if the
4390            next token is an identifier.  */
4391         if (is_attribute_list
4392             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4393           {
4394             cp_token *token;
4395
4396             /* Consume the identifier.  */
4397             token = cp_lexer_consume_token (parser->lexer);
4398             /* Save the identifier.  */
4399             identifier = token->value;
4400           }
4401         else
4402           {
4403             /* Parse the next assignment-expression.  */
4404             if (non_constant_p)
4405               {
4406                 bool expr_non_constant_p;
4407                 expr = (cp_parser_constant_expression
4408                         (parser, /*allow_non_constant_p=*/true,
4409                          &expr_non_constant_p));
4410                 if (expr_non_constant_p)
4411                   *non_constant_p = true;
4412               }
4413             else
4414               expr = cp_parser_assignment_expression (parser);
4415
4416              /* Add it to the list.  We add error_mark_node
4417                 expressions to the list, so that we can still tell if
4418                 the correct form for a parenthesized expression-list
4419                 is found. That gives better errors.  */
4420             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4421
4422             if (expr == error_mark_node)
4423               goto skip_comma;
4424           }
4425
4426         /* After the first item, attribute lists look the same as
4427            expression lists.  */
4428         is_attribute_list = false;
4429
4430       get_comma:;
4431         /* If the next token isn't a `,', then we are done.  */
4432         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4433           break;
4434
4435         /* Otherwise, consume the `,' and keep going.  */
4436         cp_lexer_consume_token (parser->lexer);
4437       }
4438
4439   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4440     {
4441       int ending;
4442
4443     skip_comma:;
4444       /* We try and resync to an unnested comma, as that will give the
4445          user better diagnostics.  */
4446       ending = cp_parser_skip_to_closing_parenthesis (parser,
4447                                                       /*recovering=*/true,
4448                                                       /*or_comma=*/true,
4449                                                       /*consume_paren=*/true);
4450       if (ending < 0)
4451         goto get_comma;
4452       if (!ending)
4453         return error_mark_node;
4454     }
4455
4456   /* We built up the list in reverse order so we must reverse it now.  */
4457   expression_list = nreverse (expression_list);
4458   if (identifier)
4459     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4460
4461   return expression_list;
4462 }
4463
4464 /* Parse a pseudo-destructor-name.
4465
4466    pseudo-destructor-name:
4467      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4468      :: [opt] nested-name-specifier template template-id :: ~ type-name
4469      :: [opt] nested-name-specifier [opt] ~ type-name
4470
4471    If either of the first two productions is used, sets *SCOPE to the
4472    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4473    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4474    or ERROR_MARK_NODE if the parse fails.  */
4475
4476 static void
4477 cp_parser_pseudo_destructor_name (cp_parser* parser,
4478                                   tree* scope,
4479                                   tree* type)
4480 {
4481   bool nested_name_specifier_p;
4482
4483   /* Look for the optional `::' operator.  */
4484   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4485   /* Look for the optional nested-name-specifier.  */
4486   nested_name_specifier_p
4487     = (cp_parser_nested_name_specifier_opt (parser,
4488                                             /*typename_keyword_p=*/false,
4489                                             /*check_dependency_p=*/true,
4490                                             /*type_p=*/false,
4491                                             /*is_declaration=*/true)
4492        != NULL_TREE);
4493   /* Now, if we saw a nested-name-specifier, we might be doing the
4494      second production.  */
4495   if (nested_name_specifier_p
4496       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4497     {
4498       /* Consume the `template' keyword.  */
4499       cp_lexer_consume_token (parser->lexer);
4500       /* Parse the template-id.  */
4501       cp_parser_template_id (parser,
4502                              /*template_keyword_p=*/true,
4503                              /*check_dependency_p=*/false,
4504                              /*is_declaration=*/true);
4505       /* Look for the `::' token.  */
4506       cp_parser_require (parser, CPP_SCOPE, "`::'");
4507     }
4508   /* If the next token is not a `~', then there might be some
4509      additional qualification.  */
4510   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4511     {
4512       /* Look for the type-name.  */
4513       *scope = TREE_TYPE (cp_parser_type_name (parser));
4514
4515       /* If we didn't get an aggregate type, or we don't have ::~,
4516          then something has gone wrong.  Since the only caller of this
4517          function is looking for something after `.' or `->' after a
4518          scalar type, most likely the program is trying to get a
4519          member of a non-aggregate type.  */
4520       if (*scope == error_mark_node
4521           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4522           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4523         {
4524           cp_parser_error (parser, "request for member of non-aggregate type");
4525           *type = error_mark_node;
4526           return;
4527         }
4528
4529       /* Look for the `::' token.  */
4530       cp_parser_require (parser, CPP_SCOPE, "`::'");
4531     }
4532   else
4533     *scope = NULL_TREE;
4534
4535   /* Look for the `~'.  */
4536   cp_parser_require (parser, CPP_COMPL, "`~'");
4537   /* Look for the type-name again.  We are not responsible for
4538      checking that it matches the first type-name.  */
4539   *type = cp_parser_type_name (parser);
4540 }
4541
4542 /* Parse a unary-expression.
4543
4544    unary-expression:
4545      postfix-expression
4546      ++ cast-expression
4547      -- cast-expression
4548      unary-operator cast-expression
4549      sizeof unary-expression
4550      sizeof ( type-id )
4551      new-expression
4552      delete-expression
4553
4554    GNU Extensions:
4555
4556    unary-expression:
4557      __extension__ cast-expression
4558      __alignof__ unary-expression
4559      __alignof__ ( type-id )
4560      __real__ cast-expression
4561      __imag__ cast-expression
4562      && identifier
4563
4564    ADDRESS_P is true iff the unary-expression is appearing as the
4565    operand of the `&' operator.
4566
4567    Returns a representation of the expression.  */
4568
4569 static tree
4570 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4571 {
4572   cp_token *token;
4573   enum tree_code unary_operator;
4574
4575   /* Peek at the next token.  */
4576   token = cp_lexer_peek_token (parser->lexer);
4577   /* Some keywords give away the kind of expression.  */
4578   if (token->type == CPP_KEYWORD)
4579     {
4580       enum rid keyword = token->keyword;
4581
4582       switch (keyword)
4583         {
4584         case RID_ALIGNOF:
4585         case RID_SIZEOF:
4586           {
4587             tree operand;
4588             enum tree_code op;
4589
4590             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4591             /* Consume the token.  */
4592             cp_lexer_consume_token (parser->lexer);
4593             /* Parse the operand.  */
4594             operand = cp_parser_sizeof_operand (parser, keyword);
4595
4596             if (TYPE_P (operand))
4597               return cxx_sizeof_or_alignof_type (operand, op, true);
4598             else
4599               return cxx_sizeof_or_alignof_expr (operand, op);
4600           }
4601
4602         case RID_NEW:
4603           return cp_parser_new_expression (parser);
4604
4605         case RID_DELETE:
4606           return cp_parser_delete_expression (parser);
4607
4608         case RID_EXTENSION:
4609           {
4610             /* The saved value of the PEDANTIC flag.  */
4611             int saved_pedantic;
4612             tree expr;
4613
4614             /* Save away the PEDANTIC flag.  */
4615             cp_parser_extension_opt (parser, &saved_pedantic);
4616             /* Parse the cast-expression.  */
4617             expr = cp_parser_simple_cast_expression (parser);
4618             /* Restore the PEDANTIC flag.  */
4619             pedantic = saved_pedantic;
4620
4621             return expr;
4622           }
4623
4624         case RID_REALPART:
4625         case RID_IMAGPART:
4626           {
4627             tree expression;
4628
4629             /* Consume the `__real__' or `__imag__' token.  */
4630             cp_lexer_consume_token (parser->lexer);
4631             /* Parse the cast-expression.  */
4632             expression = cp_parser_simple_cast_expression (parser);
4633             /* Create the complete representation.  */
4634             return build_x_unary_op ((keyword == RID_REALPART
4635                                       ? REALPART_EXPR : IMAGPART_EXPR),
4636                                      expression);
4637           }
4638           break;
4639
4640         default:
4641           break;
4642         }
4643     }
4644
4645   /* Look for the `:: new' and `:: delete', which also signal the
4646      beginning of a new-expression, or delete-expression,
4647      respectively.  If the next token is `::', then it might be one of
4648      these.  */
4649   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4650     {
4651       enum rid keyword;
4652
4653       /* See if the token after the `::' is one of the keywords in
4654          which we're interested.  */
4655       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4656       /* If it's `new', we have a new-expression.  */
4657       if (keyword == RID_NEW)
4658         return cp_parser_new_expression (parser);
4659       /* Similarly, for `delete'.  */
4660       else if (keyword == RID_DELETE)
4661         return cp_parser_delete_expression (parser);
4662     }
4663
4664   /* Look for a unary operator.  */
4665   unary_operator = cp_parser_unary_operator (token);
4666   /* The `++' and `--' operators can be handled similarly, even though
4667      they are not technically unary-operators in the grammar.  */
4668   if (unary_operator == ERROR_MARK)
4669     {
4670       if (token->type == CPP_PLUS_PLUS)
4671         unary_operator = PREINCREMENT_EXPR;
4672       else if (token->type == CPP_MINUS_MINUS)
4673         unary_operator = PREDECREMENT_EXPR;
4674       /* Handle the GNU address-of-label extension.  */
4675       else if (cp_parser_allow_gnu_extensions_p (parser)
4676                && token->type == CPP_AND_AND)
4677         {
4678           tree identifier;
4679
4680           /* Consume the '&&' token.  */
4681           cp_lexer_consume_token (parser->lexer);
4682           /* Look for the identifier.  */
4683           identifier = cp_parser_identifier (parser);
4684           /* Create an expression representing the address.  */
4685           return finish_label_address_expr (identifier);
4686         }
4687     }
4688   if (unary_operator != ERROR_MARK)
4689     {
4690       tree cast_expression;
4691       tree expression = error_mark_node;
4692       const char *non_constant_p = NULL;
4693
4694       /* Consume the operator token.  */
4695       token = cp_lexer_consume_token (parser->lexer);
4696       /* Parse the cast-expression.  */
4697       cast_expression
4698         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4699       /* Now, build an appropriate representation.  */
4700       switch (unary_operator)
4701         {
4702         case INDIRECT_REF:
4703           non_constant_p = "`*'";
4704           expression = build_x_indirect_ref (cast_expression, "unary *");
4705           break;
4706
4707         case ADDR_EXPR:
4708           non_constant_p = "`&'";
4709           /* Fall through.  */
4710         case BIT_NOT_EXPR:
4711           expression = build_x_unary_op (unary_operator, cast_expression);
4712           break;
4713
4714         case PREINCREMENT_EXPR:
4715         case PREDECREMENT_EXPR:
4716           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4717                             ? "`++'" : "`--'");
4718           /* Fall through.  */
4719         case CONVERT_EXPR:
4720         case NEGATE_EXPR:
4721         case TRUTH_NOT_EXPR:
4722           expression = finish_unary_op_expr (unary_operator, cast_expression);
4723           break;
4724
4725         default:
4726           abort ();
4727         }
4728
4729       if (non_constant_p 
4730           && cp_parser_non_integral_constant_expression (parser,
4731                                                          non_constant_p))
4732         expression = error_mark_node;
4733
4734       return expression;
4735     }
4736
4737   return cp_parser_postfix_expression (parser, address_p);
4738 }
4739
4740 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4741    unary-operator, the corresponding tree code is returned.  */
4742
4743 static enum tree_code
4744 cp_parser_unary_operator (cp_token* token)
4745 {
4746   switch (token->type)
4747     {
4748     case CPP_MULT:
4749       return INDIRECT_REF;
4750
4751     case CPP_AND:
4752       return ADDR_EXPR;
4753
4754     case CPP_PLUS:
4755       return CONVERT_EXPR;
4756
4757     case CPP_MINUS:
4758       return NEGATE_EXPR;
4759
4760     case CPP_NOT:
4761       return TRUTH_NOT_EXPR;
4762
4763     case CPP_COMPL:
4764       return BIT_NOT_EXPR;
4765
4766     default:
4767       return ERROR_MARK;
4768     }
4769 }
4770
4771 /* Parse a new-expression.
4772
4773    new-expression:
4774      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4775      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4776
4777    Returns a representation of the expression.  */
4778
4779 static tree
4780 cp_parser_new_expression (cp_parser* parser)
4781 {
4782   bool global_scope_p;
4783   tree placement;
4784   tree type;
4785   tree initializer;
4786   tree nelts;
4787
4788   /* Look for the optional `::' operator.  */
4789   global_scope_p
4790     = (cp_parser_global_scope_opt (parser,
4791                                    /*current_scope_valid_p=*/false)
4792        != NULL_TREE);
4793   /* Look for the `new' operator.  */
4794   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4795   /* There's no easy way to tell a new-placement from the
4796      `( type-id )' construct.  */
4797   cp_parser_parse_tentatively (parser);
4798   /* Look for a new-placement.  */
4799   placement = cp_parser_new_placement (parser);
4800   /* If that didn't work out, there's no new-placement.  */
4801   if (!cp_parser_parse_definitely (parser))
4802     placement = NULL_TREE;
4803
4804   /* If the next token is a `(', then we have a parenthesized
4805      type-id.  */
4806   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4807     {
4808       /* Consume the `('.  */
4809       cp_lexer_consume_token (parser->lexer);
4810       /* Parse the type-id.  */
4811       type = cp_parser_type_id (parser);
4812       /* Look for the closing `)'.  */
4813       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4814       /* There should not be a direct-new-declarator in this production, 
4815          but GCC used to allowed this, so we check and emit a sensible error
4816          message for this case.  */
4817       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4818         {
4819           error ("array bound forbidden after parenthesized type-id");
4820           inform ("try removing the parentheses around the type-id");
4821           cp_parser_direct_new_declarator (parser);
4822         }
4823       nelts = integer_one_node;
4824     }
4825   /* Otherwise, there must be a new-type-id.  */
4826   else
4827     type = cp_parser_new_type_id (parser, &nelts);
4828
4829   /* If the next token is a `(', then we have a new-initializer.  */
4830   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4831     initializer = cp_parser_new_initializer (parser);
4832   else
4833     initializer = NULL_TREE;
4834
4835   /* A new-expression may not appear in an integral constant
4836      expression.  */
4837   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4838     return error_mark_node;
4839
4840   /* Create a representation of the new-expression.  */
4841   return build_new (placement, type, nelts, initializer, global_scope_p);
4842 }
4843
4844 /* Parse a new-placement.
4845
4846    new-placement:
4847      ( expression-list )
4848
4849    Returns the same representation as for an expression-list.  */
4850
4851 static tree
4852 cp_parser_new_placement (cp_parser* parser)
4853 {
4854   tree expression_list;
4855
4856   /* Parse the expression-list.  */
4857   expression_list = (cp_parser_parenthesized_expression_list
4858                      (parser, false, /*non_constant_p=*/NULL));
4859
4860   return expression_list;
4861 }
4862
4863 /* Parse a new-type-id.
4864
4865    new-type-id:
4866      type-specifier-seq new-declarator [opt]
4867
4868    Returns the TYPE allocated.  If the new-type-id indicates an array
4869    type, *NELTS is set to the number of elements in the last array
4870    bound; the TYPE will not include the last array bound.  */
4871
4872 static tree
4873 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4874 {
4875   cp_decl_specifier_seq type_specifier_seq;
4876   cp_declarator *new_declarator;
4877   cp_declarator *declarator;
4878   cp_declarator *outer_declarator;
4879   const char *saved_message;
4880   tree type;
4881
4882   /* The type-specifier sequence must not contain type definitions.
4883      (It cannot contain declarations of new types either, but if they
4884      are not definitions we will catch that because they are not
4885      complete.)  */
4886   saved_message = parser->type_definition_forbidden_message;
4887   parser->type_definition_forbidden_message
4888     = "types may not be defined in a new-type-id";
4889   /* Parse the type-specifier-seq.  */
4890   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4891   /* Restore the old message.  */
4892   parser->type_definition_forbidden_message = saved_message;
4893   /* Parse the new-declarator.  */
4894   new_declarator = cp_parser_new_declarator_opt (parser);
4895
4896   /* Determine the number of elements in the last array dimension, if
4897      any.  */
4898   *nelts = NULL_TREE;
4899   /* Skip down to the last array dimension.  */
4900   declarator = new_declarator;
4901   outer_declarator = NULL;
4902   while (declarator && (declarator->kind == cdk_pointer
4903                         || declarator->kind == cdk_ptrmem))
4904     {
4905       outer_declarator = declarator;
4906       declarator = declarator->declarator;
4907     }
4908   while (declarator 
4909          && declarator->kind == cdk_array
4910          && declarator->declarator
4911          && declarator->declarator->kind == cdk_array)
4912     {
4913       outer_declarator = declarator;
4914       declarator = declarator->declarator;
4915     }
4916   
4917   if (declarator && declarator->kind == cdk_array)
4918     {
4919       *nelts = declarator->u.array.bounds;
4920       if (*nelts == error_mark_node)
4921         *nelts = integer_one_node;
4922       else if (!processing_template_decl)
4923         {
4924           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts, 
4925                                            false))
4926             pedwarn ("size in array new must have integral type");
4927           *nelts = save_expr (cp_convert (sizetype, *nelts));
4928           if (*nelts == integer_zero_node)
4929             warning ("zero size array reserves no space");
4930         }
4931       if (outer_declarator)
4932         outer_declarator->declarator = declarator->declarator;
4933       else
4934         new_declarator = NULL;
4935     }
4936
4937   type = groktypename (&type_specifier_seq, new_declarator);
4938   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4939     {
4940       *nelts = array_type_nelts_top (type);
4941       type = TREE_TYPE (type);
4942     }
4943   return type;
4944 }
4945
4946 /* Parse an (optional) new-declarator.
4947
4948    new-declarator:
4949      ptr-operator new-declarator [opt]
4950      direct-new-declarator
4951
4952    Returns the declarator.  */
4953
4954 static cp_declarator *
4955 cp_parser_new_declarator_opt (cp_parser* parser)
4956 {
4957   enum tree_code code;
4958   tree type;
4959   tree cv_qualifier_seq;
4960
4961   /* We don't know if there's a ptr-operator next, or not.  */
4962   cp_parser_parse_tentatively (parser);
4963   /* Look for a ptr-operator.  */
4964   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4965   /* If that worked, look for more new-declarators.  */
4966   if (cp_parser_parse_definitely (parser))
4967     {
4968       cp_declarator *declarator;
4969
4970       /* Parse another optional declarator.  */
4971       declarator = cp_parser_new_declarator_opt (parser);
4972
4973       /* Create the representation of the declarator.  */
4974       if (type)
4975         declarator = make_ptrmem_declarator (cv_qualifier_seq,
4976                                              type,
4977                                              declarator);
4978       else if (code == INDIRECT_REF)
4979         declarator = make_pointer_declarator (cv_qualifier_seq,
4980                                               declarator);
4981       else
4982         declarator = make_reference_declarator (cv_qualifier_seq,
4983                                                 declarator);
4984
4985       return declarator;
4986     }
4987
4988   /* If the next token is a `[', there is a direct-new-declarator.  */
4989   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4990     return cp_parser_direct_new_declarator (parser);
4991
4992   return NULL;
4993 }
4994
4995 /* Parse a direct-new-declarator.
4996
4997    direct-new-declarator:
4998      [ expression ]
4999      direct-new-declarator [constant-expression]
5000
5001    */
5002
5003 static cp_declarator *
5004 cp_parser_direct_new_declarator (cp_parser* parser)
5005 {
5006   cp_declarator *declarator = NULL;
5007
5008   while (true)
5009     {
5010       tree expression;
5011
5012       /* Look for the opening `['.  */
5013       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5014       /* The first expression is not required to be constant.  */
5015       if (!declarator)
5016         {
5017           expression = cp_parser_expression (parser);
5018           /* The standard requires that the expression have integral
5019              type.  DR 74 adds enumeration types.  We believe that the
5020              real intent is that these expressions be handled like the
5021              expression in a `switch' condition, which also allows
5022              classes with a single conversion to integral or
5023              enumeration type.  */
5024           if (!processing_template_decl)
5025             {
5026               expression
5027                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5028                                               expression,
5029                                               /*complain=*/true);
5030               if (!expression)
5031                 {
5032                   error ("expression in new-declarator must have integral or enumeration type");
5033                   expression = error_mark_node;
5034                 }
5035             }
5036         }
5037       /* But all the other expressions must be.  */
5038       else
5039         expression
5040           = cp_parser_constant_expression (parser,
5041                                            /*allow_non_constant=*/false,
5042                                            NULL);
5043       /* Look for the closing `]'.  */
5044       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5045
5046       /* Add this bound to the declarator.  */
5047       declarator = make_array_declarator (declarator, expression);
5048
5049       /* If the next token is not a `[', then there are no more
5050          bounds.  */
5051       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5052         break;
5053     }
5054
5055   return declarator;
5056 }
5057
5058 /* Parse a new-initializer.
5059
5060    new-initializer:
5061      ( expression-list [opt] )
5062
5063    Returns a representation of the expression-list.  If there is no
5064    expression-list, VOID_ZERO_NODE is returned.  */
5065
5066 static tree
5067 cp_parser_new_initializer (cp_parser* parser)
5068 {
5069   tree expression_list;
5070
5071   expression_list = (cp_parser_parenthesized_expression_list
5072                      (parser, false, /*non_constant_p=*/NULL));
5073   if (!expression_list)
5074     expression_list = void_zero_node;
5075
5076   return expression_list;
5077 }
5078
5079 /* Parse a delete-expression.
5080
5081    delete-expression:
5082      :: [opt] delete cast-expression
5083      :: [opt] delete [ ] cast-expression
5084
5085    Returns a representation of the expression.  */
5086
5087 static tree
5088 cp_parser_delete_expression (cp_parser* parser)
5089 {
5090   bool global_scope_p;
5091   bool array_p;
5092   tree expression;
5093
5094   /* Look for the optional `::' operator.  */
5095   global_scope_p
5096     = (cp_parser_global_scope_opt (parser,
5097                                    /*current_scope_valid_p=*/false)
5098        != NULL_TREE);
5099   /* Look for the `delete' keyword.  */
5100   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5101   /* See if the array syntax is in use.  */
5102   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5103     {
5104       /* Consume the `[' token.  */
5105       cp_lexer_consume_token (parser->lexer);
5106       /* Look for the `]' token.  */
5107       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5108       /* Remember that this is the `[]' construct.  */
5109       array_p = true;
5110     }
5111   else
5112     array_p = false;
5113
5114   /* Parse the cast-expression.  */
5115   expression = cp_parser_simple_cast_expression (parser);
5116
5117   /* A delete-expression may not appear in an integral constant
5118      expression.  */
5119   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5120     return error_mark_node;
5121
5122   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5123 }
5124
5125 /* Parse a cast-expression.
5126
5127    cast-expression:
5128      unary-expression
5129      ( type-id ) cast-expression
5130
5131    Returns a representation of the expression.  */
5132
5133 static tree
5134 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5135 {
5136   /* If it's a `(', then we might be looking at a cast.  */
5137   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5138     {
5139       tree type = NULL_TREE;
5140       tree expr = NULL_TREE;
5141       bool compound_literal_p;
5142       const char *saved_message;
5143
5144       /* There's no way to know yet whether or not this is a cast.
5145          For example, `(int (3))' is a unary-expression, while `(int)
5146          3' is a cast.  So, we resort to parsing tentatively.  */
5147       cp_parser_parse_tentatively (parser);
5148       /* Types may not be defined in a cast.  */
5149       saved_message = parser->type_definition_forbidden_message;
5150       parser->type_definition_forbidden_message
5151         = "types may not be defined in casts";
5152       /* Consume the `('.  */
5153       cp_lexer_consume_token (parser->lexer);
5154       /* A very tricky bit is that `(struct S) { 3 }' is a
5155          compound-literal (which we permit in C++ as an extension).
5156          But, that construct is not a cast-expression -- it is a
5157          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5158          is legal; if the compound-literal were a cast-expression,
5159          you'd need an extra set of parentheses.)  But, if we parse
5160          the type-id, and it happens to be a class-specifier, then we
5161          will commit to the parse at that point, because we cannot
5162          undo the action that is done when creating a new class.  So,
5163          then we cannot back up and do a postfix-expression.
5164
5165          Therefore, we scan ahead to the closing `)', and check to see
5166          if the token after the `)' is a `{'.  If so, we are not
5167          looking at a cast-expression.
5168
5169          Save tokens so that we can put them back.  */
5170       cp_lexer_save_tokens (parser->lexer);
5171       /* Skip tokens until the next token is a closing parenthesis.
5172          If we find the closing `)', and the next token is a `{', then
5173          we are looking at a compound-literal.  */
5174       compound_literal_p
5175         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5176                                                   /*consume_paren=*/true)
5177            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5178       /* Roll back the tokens we skipped.  */
5179       cp_lexer_rollback_tokens (parser->lexer);
5180       /* If we were looking at a compound-literal, simulate an error
5181          so that the call to cp_parser_parse_definitely below will
5182          fail.  */
5183       if (compound_literal_p)
5184         cp_parser_simulate_error (parser);
5185       else
5186         {
5187           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5188           parser->in_type_id_in_expr_p = true;
5189           /* Look for the type-id.  */
5190           type = cp_parser_type_id (parser);
5191           /* Look for the closing `)'.  */
5192           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5193           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5194         }
5195
5196       /* Restore the saved message.  */
5197       parser->type_definition_forbidden_message = saved_message;
5198
5199       /* If ok so far, parse the dependent expression. We cannot be
5200          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5201          ctor of T, but looks like a cast to function returning T
5202          without a dependent expression.  */
5203       if (!cp_parser_error_occurred (parser))
5204         expr = cp_parser_simple_cast_expression (parser);
5205
5206       if (cp_parser_parse_definitely (parser))
5207         {
5208           /* Warn about old-style casts, if so requested.  */
5209           if (warn_old_style_cast
5210               && !in_system_header
5211               && !VOID_TYPE_P (type)
5212               && current_lang_name != lang_name_c)
5213             warning ("use of old-style cast");
5214
5215           /* Only type conversions to integral or enumeration types
5216              can be used in constant-expressions.  */
5217           if (parser->integral_constant_expression_p
5218               && !dependent_type_p (type)
5219               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5220               && (cp_parser_non_integral_constant_expression 
5221                   (parser,
5222                    "a cast to a type other than an integral or "
5223                    "enumeration type")))
5224             return error_mark_node;
5225
5226           /* Perform the cast.  */
5227           expr = build_c_cast (type, expr);
5228           return expr;
5229         }
5230     }
5231
5232   /* If we get here, then it's not a cast, so it must be a
5233      unary-expression.  */
5234   return cp_parser_unary_expression (parser, address_p);
5235 }
5236
5237 /* Parse a pm-expression.
5238
5239    pm-expression:
5240      cast-expression
5241      pm-expression .* cast-expression
5242      pm-expression ->* cast-expression
5243
5244      Returns a representation of the expression.  */
5245
5246 static tree
5247 cp_parser_pm_expression (cp_parser* parser)
5248 {
5249   static const cp_parser_token_tree_map map = {
5250     { CPP_DEREF_STAR, MEMBER_REF },
5251     { CPP_DOT_STAR, DOTSTAR_EXPR },
5252     { CPP_EOF, ERROR_MARK }
5253   };
5254
5255   return cp_parser_binary_expression (parser, map,
5256                                       cp_parser_simple_cast_expression);
5257 }
5258
5259 /* Parse a multiplicative-expression.
5260
5261    multiplicative-expression:
5262      pm-expression
5263      multiplicative-expression * pm-expression
5264      multiplicative-expression / pm-expression
5265      multiplicative-expression % pm-expression
5266
5267    Returns a representation of the expression.  */
5268
5269 static tree
5270 cp_parser_multiplicative_expression (cp_parser* parser)
5271 {
5272   static const cp_parser_token_tree_map map = {
5273     { CPP_MULT, MULT_EXPR },
5274     { CPP_DIV, TRUNC_DIV_EXPR },
5275     { CPP_MOD, TRUNC_MOD_EXPR },
5276     { CPP_EOF, ERROR_MARK }
5277   };
5278
5279   return cp_parser_binary_expression (parser,
5280                                       map,
5281                                       cp_parser_pm_expression);
5282 }
5283
5284 /* Parse an additive-expression.
5285
5286    additive-expression:
5287      multiplicative-expression
5288      additive-expression + multiplicative-expression
5289      additive-expression - multiplicative-expression
5290
5291    Returns a representation of the expression.  */
5292
5293 static tree
5294 cp_parser_additive_expression (cp_parser* parser)
5295 {
5296   static const cp_parser_token_tree_map map = {
5297     { CPP_PLUS, PLUS_EXPR },
5298     { CPP_MINUS, MINUS_EXPR },
5299     { CPP_EOF, ERROR_MARK }
5300   };
5301
5302   return cp_parser_binary_expression (parser,
5303                                       map,
5304                                       cp_parser_multiplicative_expression);
5305 }
5306
5307 /* Parse a shift-expression.
5308
5309    shift-expression:
5310      additive-expression
5311      shift-expression << additive-expression
5312      shift-expression >> additive-expression
5313
5314    Returns a representation of the expression.  */
5315
5316 static tree
5317 cp_parser_shift_expression (cp_parser* parser)
5318 {
5319   static const cp_parser_token_tree_map map = {
5320     { CPP_LSHIFT, LSHIFT_EXPR },
5321     { CPP_RSHIFT, RSHIFT_EXPR },
5322     { CPP_EOF, ERROR_MARK }
5323   };
5324
5325   return cp_parser_binary_expression (parser,
5326                                       map,
5327                                       cp_parser_additive_expression);
5328 }
5329
5330 /* Parse a relational-expression.
5331
5332    relational-expression:
5333      shift-expression
5334      relational-expression < shift-expression
5335      relational-expression > shift-expression
5336      relational-expression <= shift-expression
5337      relational-expression >= shift-expression
5338
5339    GNU Extension:
5340
5341    relational-expression:
5342      relational-expression <? shift-expression
5343      relational-expression >? shift-expression
5344
5345    Returns a representation of the expression.  */
5346
5347 static tree
5348 cp_parser_relational_expression (cp_parser* parser)
5349 {
5350   static const cp_parser_token_tree_map map = {
5351     { CPP_LESS, LT_EXPR },
5352     { CPP_GREATER, GT_EXPR },
5353     { CPP_LESS_EQ, LE_EXPR },
5354     { CPP_GREATER_EQ, GE_EXPR },
5355     { CPP_MIN, MIN_EXPR },
5356     { CPP_MAX, MAX_EXPR },
5357     { CPP_EOF, ERROR_MARK }
5358   };
5359
5360   return cp_parser_binary_expression (parser,
5361                                       map,
5362                                       cp_parser_shift_expression);
5363 }
5364
5365 /* Parse an equality-expression.
5366
5367    equality-expression:
5368      relational-expression
5369      equality-expression == relational-expression
5370      equality-expression != relational-expression
5371
5372    Returns a representation of the expression.  */
5373
5374 static tree
5375 cp_parser_equality_expression (cp_parser* parser)
5376 {
5377   static const cp_parser_token_tree_map map = {
5378     { CPP_EQ_EQ, EQ_EXPR },
5379     { CPP_NOT_EQ, NE_EXPR },
5380     { CPP_EOF, ERROR_MARK }
5381   };
5382
5383   return cp_parser_binary_expression (parser,
5384                                       map,
5385                                       cp_parser_relational_expression);
5386 }
5387
5388 /* Parse an and-expression.
5389
5390    and-expression:
5391      equality-expression
5392      and-expression & equality-expression
5393
5394    Returns a representation of the expression.  */
5395
5396 static tree
5397 cp_parser_and_expression (cp_parser* parser)
5398 {
5399   static const cp_parser_token_tree_map map = {
5400     { CPP_AND, BIT_AND_EXPR },
5401     { CPP_EOF, ERROR_MARK }
5402   };
5403
5404   return cp_parser_binary_expression (parser,
5405                                       map,
5406                                       cp_parser_equality_expression);
5407 }
5408
5409 /* Parse an exclusive-or-expression.
5410
5411    exclusive-or-expression:
5412      and-expression
5413      exclusive-or-expression ^ and-expression
5414
5415    Returns a representation of the expression.  */
5416
5417 static tree
5418 cp_parser_exclusive_or_expression (cp_parser* parser)
5419 {
5420   static const cp_parser_token_tree_map map = {
5421     { CPP_XOR, BIT_XOR_EXPR },
5422     { CPP_EOF, ERROR_MARK }
5423   };
5424
5425   return cp_parser_binary_expression (parser,
5426                                       map,
5427                                       cp_parser_and_expression);
5428 }
5429
5430
5431 /* Parse an inclusive-or-expression.
5432
5433    inclusive-or-expression:
5434      exclusive-or-expression
5435      inclusive-or-expression | exclusive-or-expression
5436
5437    Returns a representation of the expression.  */
5438
5439 static tree
5440 cp_parser_inclusive_or_expression (cp_parser* parser)
5441 {
5442   static const cp_parser_token_tree_map map = {
5443     { CPP_OR, BIT_IOR_EXPR },
5444     { CPP_EOF, ERROR_MARK }
5445   };
5446
5447   return cp_parser_binary_expression (parser,
5448                                       map,
5449                                       cp_parser_exclusive_or_expression);
5450 }
5451
5452 /* Parse a logical-and-expression.
5453
5454    logical-and-expression:
5455      inclusive-or-expression
5456      logical-and-expression && inclusive-or-expression
5457
5458    Returns a representation of the expression.  */
5459
5460 static tree
5461 cp_parser_logical_and_expression (cp_parser* parser)
5462 {
5463   static const cp_parser_token_tree_map map = {
5464     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5465     { CPP_EOF, ERROR_MARK }
5466   };
5467
5468   return cp_parser_binary_expression (parser,
5469                                       map,
5470                                       cp_parser_inclusive_or_expression);
5471 }
5472
5473 /* Parse a logical-or-expression.
5474
5475    logical-or-expression:
5476      logical-and-expression
5477      logical-or-expression || logical-and-expression
5478
5479    Returns a representation of the expression.  */
5480
5481 static tree
5482 cp_parser_logical_or_expression (cp_parser* parser)
5483 {
5484   static const cp_parser_token_tree_map map = {
5485     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5486     { CPP_EOF, ERROR_MARK }
5487   };
5488
5489   return cp_parser_binary_expression (parser,
5490                                       map,
5491                                       cp_parser_logical_and_expression);
5492 }
5493
5494 /* Parse the `? expression : assignment-expression' part of a
5495    conditional-expression.  The LOGICAL_OR_EXPR is the
5496    logical-or-expression that started the conditional-expression.
5497    Returns a representation of the entire conditional-expression.
5498
5499    This routine is used by cp_parser_assignment_expression.
5500
5501      ? expression : assignment-expression
5502
5503    GNU Extensions:
5504
5505      ? : assignment-expression */
5506
5507 static tree
5508 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5509 {
5510   tree expr;
5511   tree assignment_expr;
5512
5513   /* Consume the `?' token.  */
5514   cp_lexer_consume_token (parser->lexer);
5515   if (cp_parser_allow_gnu_extensions_p (parser)
5516       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5517     /* Implicit true clause.  */
5518     expr = NULL_TREE;
5519   else
5520     /* Parse the expression.  */
5521     expr = cp_parser_expression (parser);
5522
5523   /* The next token should be a `:'.  */
5524   cp_parser_require (parser, CPP_COLON, "`:'");
5525   /* Parse the assignment-expression.  */
5526   assignment_expr = cp_parser_assignment_expression (parser);
5527
5528   /* Build the conditional-expression.  */
5529   return build_x_conditional_expr (logical_or_expr,
5530                                    expr,
5531                                    assignment_expr);
5532 }
5533
5534 /* Parse an assignment-expression.
5535
5536    assignment-expression:
5537      conditional-expression
5538      logical-or-expression assignment-operator assignment_expression
5539      throw-expression
5540
5541    Returns a representation for the expression.  */
5542
5543 static tree
5544 cp_parser_assignment_expression (cp_parser* parser)
5545 {
5546   tree expr;
5547
5548   /* If the next token is the `throw' keyword, then we're looking at
5549      a throw-expression.  */
5550   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5551     expr = cp_parser_throw_expression (parser);
5552   /* Otherwise, it must be that we are looking at a
5553      logical-or-expression.  */
5554   else
5555     {
5556       /* Parse the logical-or-expression.  */
5557       expr = cp_parser_logical_or_expression (parser);
5558       /* If the next token is a `?' then we're actually looking at a
5559          conditional-expression.  */
5560       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5561         return cp_parser_question_colon_clause (parser, expr);
5562       else
5563         {
5564           enum tree_code assignment_operator;
5565
5566           /* If it's an assignment-operator, we're using the second
5567              production.  */
5568           assignment_operator
5569             = cp_parser_assignment_operator_opt (parser);
5570           if (assignment_operator != ERROR_MARK)
5571             {
5572               tree rhs;
5573
5574               /* Parse the right-hand side of the assignment.  */
5575               rhs = cp_parser_assignment_expression (parser);
5576               /* An assignment may not appear in a
5577                  constant-expression.  */
5578               if (cp_parser_non_integral_constant_expression (parser,
5579                                                               "an assignment"))
5580                 return error_mark_node;
5581               /* Build the assignment expression.  */
5582               expr = build_x_modify_expr (expr,
5583                                           assignment_operator,
5584                                           rhs);
5585             }
5586         }
5587     }
5588
5589   return expr;
5590 }
5591
5592 /* Parse an (optional) assignment-operator.
5593
5594    assignment-operator: one of
5595      = *= /= %= += -= >>= <<= &= ^= |=
5596
5597    GNU Extension:
5598
5599    assignment-operator: one of
5600      <?= >?=
5601
5602    If the next token is an assignment operator, the corresponding tree
5603    code is returned, and the token is consumed.  For example, for
5604    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5605    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5606    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5607    operator, ERROR_MARK is returned.  */
5608
5609 static enum tree_code
5610 cp_parser_assignment_operator_opt (cp_parser* parser)
5611 {
5612   enum tree_code op;
5613   cp_token *token;
5614
5615   /* Peek at the next toen.  */
5616   token = cp_lexer_peek_token (parser->lexer);
5617
5618   switch (token->type)
5619     {
5620     case CPP_EQ:
5621       op = NOP_EXPR;
5622       break;
5623
5624     case CPP_MULT_EQ:
5625       op = MULT_EXPR;
5626       break;
5627
5628     case CPP_DIV_EQ:
5629       op = TRUNC_DIV_EXPR;
5630       break;
5631
5632     case CPP_MOD_EQ:
5633       op = TRUNC_MOD_EXPR;
5634       break;
5635
5636     case CPP_PLUS_EQ:
5637       op = PLUS_EXPR;
5638       break;
5639
5640     case CPP_MINUS_EQ:
5641       op = MINUS_EXPR;
5642       break;
5643
5644     case CPP_RSHIFT_EQ:
5645       op = RSHIFT_EXPR;
5646       break;
5647
5648     case CPP_LSHIFT_EQ:
5649       op = LSHIFT_EXPR;
5650       break;
5651
5652     case CPP_AND_EQ:
5653       op = BIT_AND_EXPR;
5654       break;
5655
5656     case CPP_XOR_EQ:
5657       op = BIT_XOR_EXPR;
5658       break;
5659
5660     case CPP_OR_EQ:
5661       op = BIT_IOR_EXPR;
5662       break;
5663
5664     case CPP_MIN_EQ:
5665       op = MIN_EXPR;
5666       break;
5667
5668     case CPP_MAX_EQ:
5669       op = MAX_EXPR;
5670       break;
5671
5672     default:
5673       /* Nothing else is an assignment operator.  */
5674       op = ERROR_MARK;
5675     }
5676
5677   /* If it was an assignment operator, consume it.  */
5678   if (op != ERROR_MARK)
5679     cp_lexer_consume_token (parser->lexer);
5680
5681   return op;
5682 }
5683
5684 /* Parse an expression.
5685
5686    expression:
5687      assignment-expression
5688      expression , assignment-expression
5689
5690    Returns a representation of the expression.  */
5691
5692 static tree
5693 cp_parser_expression (cp_parser* parser)
5694 {
5695   tree expression = NULL_TREE;
5696
5697   while (true)
5698     {
5699       tree assignment_expression;
5700
5701       /* Parse the next assignment-expression.  */
5702       assignment_expression
5703         = cp_parser_assignment_expression (parser);
5704       /* If this is the first assignment-expression, we can just
5705          save it away.  */
5706       if (!expression)
5707         expression = assignment_expression;
5708       else
5709         expression = build_x_compound_expr (expression,
5710                                             assignment_expression);
5711       /* If the next token is not a comma, then we are done with the
5712          expression.  */
5713       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5714         break;
5715       /* Consume the `,'.  */
5716       cp_lexer_consume_token (parser->lexer);
5717       /* A comma operator cannot appear in a constant-expression.  */
5718       if (cp_parser_non_integral_constant_expression (parser,
5719                                                       "a comma operator"))
5720         expression = error_mark_node;
5721     }
5722
5723   return expression;
5724 }
5725
5726 /* Parse a constant-expression.
5727
5728    constant-expression:
5729      conditional-expression
5730
5731   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5732   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5733   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5734   is false, NON_CONSTANT_P should be NULL.  */
5735
5736 static tree
5737 cp_parser_constant_expression (cp_parser* parser,
5738                                bool allow_non_constant_p,
5739                                bool *non_constant_p)
5740 {
5741   bool saved_integral_constant_expression_p;
5742   bool saved_allow_non_integral_constant_expression_p;
5743   bool saved_non_integral_constant_expression_p;
5744   tree expression;
5745
5746   /* It might seem that we could simply parse the
5747      conditional-expression, and then check to see if it were
5748      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5749      one that the compiler can figure out is constant, possibly after
5750      doing some simplifications or optimizations.  The standard has a
5751      precise definition of constant-expression, and we must honor
5752      that, even though it is somewhat more restrictive.
5753
5754      For example:
5755
5756        int i[(2, 3)];
5757
5758      is not a legal declaration, because `(2, 3)' is not a
5759      constant-expression.  The `,' operator is forbidden in a
5760      constant-expression.  However, GCC's constant-folding machinery
5761      will fold this operation to an INTEGER_CST for `3'.  */
5762
5763   /* Save the old settings.  */
5764   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5765   saved_allow_non_integral_constant_expression_p
5766     = parser->allow_non_integral_constant_expression_p;
5767   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5768   /* We are now parsing a constant-expression.  */
5769   parser->integral_constant_expression_p = true;
5770   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5771   parser->non_integral_constant_expression_p = false;
5772   /* Although the grammar says "conditional-expression", we parse an
5773      "assignment-expression", which also permits "throw-expression"
5774      and the use of assignment operators.  In the case that
5775      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5776      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5777      actually essential that we look for an assignment-expression.
5778      For example, cp_parser_initializer_clauses uses this function to
5779      determine whether a particular assignment-expression is in fact
5780      constant.  */
5781   expression = cp_parser_assignment_expression (parser);
5782   /* Restore the old settings.  */
5783   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5784   parser->allow_non_integral_constant_expression_p
5785     = saved_allow_non_integral_constant_expression_p;
5786   if (allow_non_constant_p)
5787     *non_constant_p = parser->non_integral_constant_expression_p;
5788   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5789
5790   return expression;
5791 }
5792
5793 /* Parse __builtin_offsetof.
5794
5795    offsetof-expression:
5796      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5797
5798    offsetof-member-designator:
5799      id-expression
5800      | offsetof-member-designator "." id-expression
5801      | offsetof-member-designator "[" expression "]"
5802 */
5803
5804 static tree
5805 cp_parser_builtin_offsetof (cp_parser *parser)
5806 {
5807   int save_ice_p, save_non_ice_p;
5808   tree type, expr;
5809   cp_id_kind dummy;
5810
5811   /* We're about to accept non-integral-constant things, but will
5812      definitely yield an integral constant expression.  Save and
5813      restore these values around our local parsing.  */
5814   save_ice_p = parser->integral_constant_expression_p;
5815   save_non_ice_p = parser->non_integral_constant_expression_p;
5816
5817   /* Consume the "__builtin_offsetof" token.  */
5818   cp_lexer_consume_token (parser->lexer);
5819   /* Consume the opening `('.  */
5820   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5821   /* Parse the type-id.  */
5822   type = cp_parser_type_id (parser);
5823   /* Look for the `,'.  */
5824   cp_parser_require (parser, CPP_COMMA, "`,'");
5825
5826   /* Build the (type *)null that begins the traditional offsetof macro.  */
5827   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5828
5829   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5830   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5831                                                  true, &dummy);
5832   while (true)
5833     {
5834       cp_token *token = cp_lexer_peek_token (parser->lexer);
5835       switch (token->type)
5836         {
5837         case CPP_OPEN_SQUARE:
5838           /* offsetof-member-designator "[" expression "]" */
5839           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5840           break;
5841
5842         case CPP_DOT:
5843           /* offsetof-member-designator "." identifier */
5844           cp_lexer_consume_token (parser->lexer);
5845           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5846                                                          true, &dummy);
5847           break;
5848
5849         case CPP_CLOSE_PAREN:
5850           /* Consume the ")" token.  */
5851           cp_lexer_consume_token (parser->lexer);
5852           goto success;
5853
5854         default:
5855           /* Error.  We know the following require will fail, but
5856              that gives the proper error message.  */
5857           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5858           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5859           expr = error_mark_node;
5860           goto failure;
5861         }
5862     }
5863
5864  success:
5865   /* We've finished the parsing, now finish with the semantics.  At present
5866      we're just mirroring the traditional macro implementation.  Better
5867      would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5868      here rather than in build_x_unary_op.  */
5869   expr = build_reinterpret_cast (build_reference_type (char_type_node), expr);
5870   expr = build_x_unary_op (ADDR_EXPR, expr);
5871   expr = build_reinterpret_cast (size_type_node, expr);
5872
5873  failure:
5874   parser->integral_constant_expression_p = save_ice_p;
5875   parser->non_integral_constant_expression_p = save_non_ice_p;
5876
5877   return expr;
5878 }
5879
5880 /* Statements [gram.stmt.stmt]  */
5881
5882 /* Parse a statement.
5883
5884    statement:
5885      labeled-statement
5886      expression-statement
5887      compound-statement
5888      selection-statement
5889      iteration-statement
5890      jump-statement
5891      declaration-statement
5892      try-block  */
5893
5894 static void
5895 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5896 {
5897   tree statement;
5898   cp_token *token;
5899   location_t statement_locus;
5900
5901   /* There is no statement yet.  */
5902   statement = NULL_TREE;
5903   /* Peek at the next token.  */
5904   token = cp_lexer_peek_token (parser->lexer);
5905   /* Remember the location of the first token in the statement.  */
5906   statement_locus = token->location;
5907   /* If this is a keyword, then that will often determine what kind of
5908      statement we have.  */
5909   if (token->type == CPP_KEYWORD)
5910     {
5911       enum rid keyword = token->keyword;
5912
5913       switch (keyword)
5914         {
5915         case RID_CASE:
5916         case RID_DEFAULT:
5917           statement = cp_parser_labeled_statement (parser,
5918                                                    in_statement_expr);
5919           break;
5920
5921         case RID_IF:
5922         case RID_SWITCH:
5923           statement = cp_parser_selection_statement (parser);
5924           break;
5925
5926         case RID_WHILE:
5927         case RID_DO:
5928         case RID_FOR:
5929           statement = cp_parser_iteration_statement (parser);
5930           break;
5931
5932         case RID_BREAK:
5933         case RID_CONTINUE:
5934         case RID_RETURN:
5935         case RID_GOTO:
5936           statement = cp_parser_jump_statement (parser);
5937           break;
5938
5939         case RID_TRY:
5940           statement = cp_parser_try_block (parser);
5941           break;
5942
5943         default:
5944           /* It might be a keyword like `int' that can start a
5945              declaration-statement.  */
5946           break;
5947         }
5948     }
5949   else if (token->type == CPP_NAME)
5950     {
5951       /* If the next token is a `:', then we are looking at a
5952          labeled-statement.  */
5953       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5954       if (token->type == CPP_COLON)
5955         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5956     }
5957   /* Anything that starts with a `{' must be a compound-statement.  */
5958   else if (token->type == CPP_OPEN_BRACE)
5959     statement = cp_parser_compound_statement (parser, NULL, false);
5960
5961   /* Everything else must be a declaration-statement or an
5962      expression-statement.  Try for the declaration-statement
5963      first, unless we are looking at a `;', in which case we know that
5964      we have an expression-statement.  */
5965   if (!statement)
5966     {
5967       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5968         {
5969           cp_parser_parse_tentatively (parser);
5970           /* Try to parse the declaration-statement.  */
5971           cp_parser_declaration_statement (parser);
5972           /* If that worked, we're done.  */
5973           if (cp_parser_parse_definitely (parser))
5974             return;
5975         }
5976       /* Look for an expression-statement instead.  */
5977       statement = cp_parser_expression_statement (parser, in_statement_expr);
5978     }
5979
5980   /* Set the line number for the statement.  */
5981   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5982     {
5983       SET_EXPR_LOCUS (statement, NULL);
5984       annotate_with_locus (statement, statement_locus);
5985     }
5986 }
5987
5988 /* Parse a labeled-statement.
5989
5990    labeled-statement:
5991      identifier : statement
5992      case constant-expression : statement
5993      default : statement
5994
5995    GNU Extension:
5996
5997    labeled-statement:
5998      case constant-expression ... constant-expression : statement
5999
6000    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6001    For an ordinary label, returns a LABEL_EXPR.  */
6002
6003 static tree
6004 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6005 {
6006   cp_token *token;
6007   tree statement = error_mark_node;
6008
6009   /* The next token should be an identifier.  */
6010   token = cp_lexer_peek_token (parser->lexer);
6011   if (token->type != CPP_NAME
6012       && token->type != CPP_KEYWORD)
6013     {
6014       cp_parser_error (parser, "expected labeled-statement");
6015       return error_mark_node;
6016     }
6017
6018   switch (token->keyword)
6019     {
6020     case RID_CASE:
6021       {
6022         tree expr, expr_hi;
6023         cp_token *ellipsis;
6024
6025         /* Consume the `case' token.  */
6026         cp_lexer_consume_token (parser->lexer);
6027         /* Parse the constant-expression.  */
6028         expr = cp_parser_constant_expression (parser,
6029                                               /*allow_non_constant_p=*/false,
6030                                               NULL);
6031
6032         ellipsis = cp_lexer_peek_token (parser->lexer);
6033         if (ellipsis->type == CPP_ELLIPSIS)
6034           {
6035             /* Consume the `...' token.  */
6036             cp_lexer_consume_token (parser->lexer);
6037             expr_hi =
6038               cp_parser_constant_expression (parser,
6039                                              /*allow_non_constant_p=*/false,
6040                                              NULL);
6041             /* We don't need to emit warnings here, as the common code
6042                will do this for us.  */
6043           }
6044         else
6045           expr_hi = NULL_TREE;
6046
6047         if (!parser->in_switch_statement_p)
6048           error ("case label `%E' not within a switch statement", expr);
6049         else
6050           statement = finish_case_label (expr, expr_hi);
6051       }
6052       break;
6053
6054     case RID_DEFAULT:
6055       /* Consume the `default' token.  */
6056       cp_lexer_consume_token (parser->lexer);
6057       if (!parser->in_switch_statement_p)
6058         error ("case label not within a switch statement");
6059       else
6060         statement = finish_case_label (NULL_TREE, NULL_TREE);
6061       break;
6062
6063     default:
6064       /* Anything else must be an ordinary label.  */
6065       statement = finish_label_stmt (cp_parser_identifier (parser));
6066       break;
6067     }
6068
6069   /* Require the `:' token.  */
6070   cp_parser_require (parser, CPP_COLON, "`:'");
6071   /* Parse the labeled statement.  */
6072   cp_parser_statement (parser, in_statement_expr);
6073
6074   /* Return the label, in the case of a `case' or `default' label.  */
6075   return statement;
6076 }
6077
6078 /* Parse an expression-statement.
6079
6080    expression-statement:
6081      expression [opt] ;
6082
6083    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6084    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6085    indicates whether this expression-statement is part of an
6086    expression statement.  */
6087
6088 static tree
6089 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6090 {
6091   tree statement = NULL_TREE;
6092
6093   /* If the next token is a ';', then there is no expression
6094      statement.  */
6095   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6096     statement = cp_parser_expression (parser);
6097
6098   /* Consume the final `;'.  */
6099   cp_parser_consume_semicolon_at_end_of_statement (parser);
6100
6101   if (in_statement_expr
6102       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6103     {
6104       /* This is the final expression statement of a statement
6105          expression.  */
6106       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6107     }
6108   else if (statement)
6109     statement = finish_expr_stmt (statement);
6110   else
6111     finish_stmt ();
6112
6113   return statement;
6114 }
6115
6116 /* Parse a compound-statement.
6117
6118    compound-statement:
6119      { statement-seq [opt] }
6120
6121    Returns a tree representing the statement.  */
6122
6123 static tree
6124 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6125                               bool in_try)
6126 {
6127   tree compound_stmt;
6128
6129   /* Consume the `{'.  */
6130   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6131     return error_mark_node;
6132   /* Begin the compound-statement.  */
6133   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6134   /* Parse an (optional) statement-seq.  */
6135   cp_parser_statement_seq_opt (parser, in_statement_expr);
6136   /* Finish the compound-statement.  */
6137   finish_compound_stmt (compound_stmt);
6138   /* Consume the `}'.  */
6139   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6140
6141   return compound_stmt;
6142 }
6143
6144 /* Parse an (optional) statement-seq.
6145
6146    statement-seq:
6147      statement
6148      statement-seq [opt] statement  */
6149
6150 static void
6151 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6152 {
6153   /* Scan statements until there aren't any more.  */
6154   while (true)
6155     {
6156       /* If we're looking at a `}', then we've run out of statements.  */
6157       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6158           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6159         break;
6160
6161       /* Parse the statement.  */
6162       cp_parser_statement (parser, in_statement_expr);
6163     }
6164 }
6165
6166 /* Parse a selection-statement.
6167
6168    selection-statement:
6169      if ( condition ) statement
6170      if ( condition ) statement else statement
6171      switch ( condition ) statement
6172
6173    Returns the new IF_STMT or SWITCH_STMT.  */
6174
6175 static tree
6176 cp_parser_selection_statement (cp_parser* parser)
6177 {
6178   cp_token *token;
6179   enum rid keyword;
6180
6181   /* Peek at the next token.  */
6182   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6183
6184   /* See what kind of keyword it is.  */
6185   keyword = token->keyword;
6186   switch (keyword)
6187     {
6188     case RID_IF:
6189     case RID_SWITCH:
6190       {
6191         tree statement;
6192         tree condition;
6193
6194         /* Look for the `('.  */
6195         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6196           {
6197             cp_parser_skip_to_end_of_statement (parser);
6198             return error_mark_node;
6199           }
6200
6201         /* Begin the selection-statement.  */
6202         if (keyword == RID_IF)
6203           statement = begin_if_stmt ();
6204         else
6205           statement = begin_switch_stmt ();
6206
6207         /* Parse the condition.  */
6208         condition = cp_parser_condition (parser);
6209         /* Look for the `)'.  */
6210         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6211           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6212                                                  /*consume_paren=*/true);
6213
6214         if (keyword == RID_IF)
6215           {
6216             /* Add the condition.  */
6217             finish_if_stmt_cond (condition, statement);
6218
6219             /* Parse the then-clause.  */
6220             cp_parser_implicitly_scoped_statement (parser);
6221             finish_then_clause (statement);
6222
6223             /* If the next token is `else', parse the else-clause.  */
6224             if (cp_lexer_next_token_is_keyword (parser->lexer,
6225                                                 RID_ELSE))
6226               {
6227                 /* Consume the `else' keyword.  */
6228                 cp_lexer_consume_token (parser->lexer);
6229                 begin_else_clause (statement);
6230                 /* Parse the else-clause.  */
6231                 cp_parser_implicitly_scoped_statement (parser);
6232                 finish_else_clause (statement);
6233               }
6234
6235             /* Now we're all done with the if-statement.  */
6236             finish_if_stmt (statement);
6237           }
6238         else
6239           {
6240             bool in_switch_statement_p;
6241
6242             /* Add the condition.  */
6243             finish_switch_cond (condition, statement);
6244
6245             /* Parse the body of the switch-statement.  */
6246             in_switch_statement_p = parser->in_switch_statement_p;
6247             parser->in_switch_statement_p = true;
6248             cp_parser_implicitly_scoped_statement (parser);
6249             parser->in_switch_statement_p = in_switch_statement_p;
6250
6251             /* Now we're all done with the switch-statement.  */
6252             finish_switch_stmt (statement);
6253           }
6254
6255         return statement;
6256       }
6257       break;
6258
6259     default:
6260       cp_parser_error (parser, "expected selection-statement");
6261       return error_mark_node;
6262     }
6263 }
6264
6265 /* Parse a condition.
6266
6267    condition:
6268      expression
6269      type-specifier-seq declarator = assignment-expression
6270
6271    GNU Extension:
6272
6273    condition:
6274      type-specifier-seq declarator asm-specification [opt]
6275        attributes [opt] = assignment-expression
6276
6277    Returns the expression that should be tested.  */
6278
6279 static tree
6280 cp_parser_condition (cp_parser* parser)
6281 {
6282   cp_decl_specifier_seq type_specifiers;
6283   const char *saved_message;
6284
6285   /* Try the declaration first.  */
6286   cp_parser_parse_tentatively (parser);
6287   /* New types are not allowed in the type-specifier-seq for a
6288      condition.  */
6289   saved_message = parser->type_definition_forbidden_message;
6290   parser->type_definition_forbidden_message
6291     = "types may not be defined in conditions";
6292   /* Parse the type-specifier-seq.  */
6293   cp_parser_type_specifier_seq (parser, &type_specifiers);
6294   /* Restore the saved message.  */
6295   parser->type_definition_forbidden_message = saved_message;
6296   /* If all is well, we might be looking at a declaration.  */
6297   if (!cp_parser_error_occurred (parser))
6298     {
6299       tree decl;
6300       tree asm_specification;
6301       tree attributes;
6302       cp_declarator *declarator;
6303       tree initializer = NULL_TREE;
6304
6305       /* Parse the declarator.  */
6306       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6307                                          /*ctor_dtor_or_conv_p=*/NULL,
6308                                          /*parenthesized_p=*/NULL);
6309       /* Parse the attributes.  */
6310       attributes = cp_parser_attributes_opt (parser);
6311       /* Parse the asm-specification.  */
6312       asm_specification = cp_parser_asm_specification_opt (parser);
6313       /* If the next token is not an `=', then we might still be
6314          looking at an expression.  For example:
6315
6316            if (A(a).x)
6317
6318          looks like a decl-specifier-seq and a declarator -- but then
6319          there is no `=', so this is an expression.  */
6320       cp_parser_require (parser, CPP_EQ, "`='");
6321       /* If we did see an `=', then we are looking at a declaration
6322          for sure.  */
6323       if (cp_parser_parse_definitely (parser))
6324         {
6325           /* Create the declaration.  */
6326           decl = start_decl (declarator, &type_specifiers,
6327                              /*initialized_p=*/true,
6328                              attributes, /*prefix_attributes=*/NULL_TREE);
6329           /* Parse the assignment-expression.  */
6330           initializer = cp_parser_assignment_expression (parser);
6331
6332           /* Process the initializer.  */
6333           cp_finish_decl (decl,
6334                           initializer,
6335                           asm_specification,
6336                           LOOKUP_ONLYCONVERTING);
6337
6338           return convert_from_reference (decl);
6339         }
6340     }
6341   /* If we didn't even get past the declarator successfully, we are
6342      definitely not looking at a declaration.  */
6343   else
6344     cp_parser_abort_tentative_parse (parser);
6345
6346   /* Otherwise, we are looking at an expression.  */
6347   return cp_parser_expression (parser);
6348 }
6349
6350 /* Parse an iteration-statement.
6351
6352    iteration-statement:
6353      while ( condition ) statement
6354      do statement while ( expression ) ;
6355      for ( for-init-statement condition [opt] ; expression [opt] )
6356        statement
6357
6358    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6359
6360 static tree
6361 cp_parser_iteration_statement (cp_parser* parser)
6362 {
6363   cp_token *token;
6364   enum rid keyword;
6365   tree statement;
6366   bool in_iteration_statement_p;
6367
6368
6369   /* Peek at the next token.  */
6370   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6371   if (!token)
6372     return error_mark_node;
6373
6374   /* Remember whether or not we are already within an iteration
6375      statement.  */
6376   in_iteration_statement_p = parser->in_iteration_statement_p;
6377
6378   /* See what kind of keyword it is.  */
6379   keyword = token->keyword;
6380   switch (keyword)
6381     {
6382     case RID_WHILE:
6383       {
6384         tree condition;
6385
6386         /* Begin the while-statement.  */
6387         statement = begin_while_stmt ();
6388         /* Look for the `('.  */
6389         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6390         /* Parse the condition.  */
6391         condition = cp_parser_condition (parser);
6392         finish_while_stmt_cond (condition, statement);
6393         /* Look for the `)'.  */
6394         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6395         /* Parse the dependent statement.  */
6396         parser->in_iteration_statement_p = true;
6397         cp_parser_already_scoped_statement (parser);
6398         parser->in_iteration_statement_p = in_iteration_statement_p;
6399         /* We're done with the while-statement.  */
6400         finish_while_stmt (statement);
6401       }
6402       break;
6403
6404     case RID_DO:
6405       {
6406         tree expression;
6407
6408         /* Begin the do-statement.  */
6409         statement = begin_do_stmt ();
6410         /* Parse the body of the do-statement.  */
6411         parser->in_iteration_statement_p = true;
6412         cp_parser_implicitly_scoped_statement (parser);
6413         parser->in_iteration_statement_p = in_iteration_statement_p;
6414         finish_do_body (statement);
6415         /* Look for the `while' keyword.  */
6416         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6417         /* Look for the `('.  */
6418         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6419         /* Parse the expression.  */
6420         expression = cp_parser_expression (parser);
6421         /* We're done with the do-statement.  */
6422         finish_do_stmt (expression, statement);
6423         /* Look for the `)'.  */
6424         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6425         /* Look for the `;'.  */
6426         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6427       }
6428       break;
6429
6430     case RID_FOR:
6431       {
6432         tree condition = NULL_TREE;
6433         tree expression = NULL_TREE;
6434
6435         /* Begin the for-statement.  */
6436         statement = begin_for_stmt ();
6437         /* Look for the `('.  */
6438         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6439         /* Parse the initialization.  */
6440         cp_parser_for_init_statement (parser);
6441         finish_for_init_stmt (statement);
6442
6443         /* If there's a condition, process it.  */
6444         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6445           condition = cp_parser_condition (parser);
6446         finish_for_cond (condition, statement);
6447         /* Look for the `;'.  */
6448         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6449
6450         /* If there's an expression, process it.  */
6451         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6452           expression = cp_parser_expression (parser);
6453         finish_for_expr (expression, statement);
6454         /* Look for the `)'.  */
6455         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6456         
6457         /* Parse the body of the for-statement.  */
6458         parser->in_iteration_statement_p = true;
6459         cp_parser_already_scoped_statement (parser);
6460         parser->in_iteration_statement_p = in_iteration_statement_p;
6461
6462         /* We're done with the for-statement.  */
6463         finish_for_stmt (statement);
6464       }
6465       break;
6466
6467     default:
6468       cp_parser_error (parser, "expected iteration-statement");
6469       statement = error_mark_node;
6470       break;
6471     }
6472
6473   return statement;
6474 }
6475
6476 /* Parse a for-init-statement.
6477
6478    for-init-statement:
6479      expression-statement
6480      simple-declaration  */
6481
6482 static void
6483 cp_parser_for_init_statement (cp_parser* parser)
6484 {
6485   /* If the next token is a `;', then we have an empty
6486      expression-statement.  Grammatically, this is also a
6487      simple-declaration, but an invalid one, because it does not
6488      declare anything.  Therefore, if we did not handle this case
6489      specially, we would issue an error message about an invalid
6490      declaration.  */
6491   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6492     {
6493       /* We're going to speculatively look for a declaration, falling back
6494          to an expression, if necessary.  */
6495       cp_parser_parse_tentatively (parser);
6496       /* Parse the declaration.  */
6497       cp_parser_simple_declaration (parser,
6498                                     /*function_definition_allowed_p=*/false);
6499       /* If the tentative parse failed, then we shall need to look for an
6500          expression-statement.  */
6501       if (cp_parser_parse_definitely (parser))
6502         return;
6503     }
6504
6505   cp_parser_expression_statement (parser, false);
6506 }
6507
6508 /* Parse a jump-statement.
6509
6510    jump-statement:
6511      break ;
6512      continue ;
6513      return expression [opt] ;
6514      goto identifier ;
6515
6516    GNU extension:
6517
6518    jump-statement:
6519      goto * expression ;
6520
6521    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6522
6523 static tree
6524 cp_parser_jump_statement (cp_parser* parser)
6525 {
6526   tree statement = error_mark_node;
6527   cp_token *token;
6528   enum rid keyword;
6529
6530   /* Peek at the next token.  */
6531   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6532   if (!token)
6533     return error_mark_node;
6534
6535   /* See what kind of keyword it is.  */
6536   keyword = token->keyword;
6537   switch (keyword)
6538     {
6539     case RID_BREAK:
6540       if (!parser->in_switch_statement_p
6541           && !parser->in_iteration_statement_p)
6542         {
6543           error ("break statement not within loop or switch");
6544           statement = error_mark_node;
6545         }
6546       else
6547         statement = finish_break_stmt ();
6548       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6549       break;
6550
6551     case RID_CONTINUE:
6552       if (!parser->in_iteration_statement_p)
6553         {
6554           error ("continue statement not within a loop");
6555           statement = error_mark_node;
6556         }
6557       else
6558         statement = finish_continue_stmt ();
6559       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6560       break;
6561
6562     case RID_RETURN:
6563       {
6564         tree expr;
6565
6566         /* If the next token is a `;', then there is no
6567            expression.  */
6568         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6569           expr = cp_parser_expression (parser);
6570         else
6571           expr = NULL_TREE;
6572         /* Build the return-statement.  */
6573         statement = finish_return_stmt (expr);
6574         /* Look for the final `;'.  */
6575         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6576       }
6577       break;
6578
6579     case RID_GOTO:
6580       /* Create the goto-statement.  */
6581       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6582         {
6583           /* Issue a warning about this use of a GNU extension.  */
6584           if (pedantic)
6585             pedwarn ("ISO C++ forbids computed gotos");
6586           /* Consume the '*' token.  */
6587           cp_lexer_consume_token (parser->lexer);
6588           /* Parse the dependent expression.  */
6589           finish_goto_stmt (cp_parser_expression (parser));
6590         }
6591       else
6592         finish_goto_stmt (cp_parser_identifier (parser));
6593       /* Look for the final `;'.  */
6594       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6595       break;
6596
6597     default:
6598       cp_parser_error (parser, "expected jump-statement");
6599       break;
6600     }
6601
6602   return statement;
6603 }
6604
6605 /* Parse a declaration-statement.
6606
6607    declaration-statement:
6608      block-declaration  */
6609
6610 static void
6611 cp_parser_declaration_statement (cp_parser* parser)
6612 {
6613   void *p;
6614
6615   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6616   p = obstack_alloc (&declarator_obstack, 0);
6617
6618  /* Parse the block-declaration.  */
6619   cp_parser_block_declaration (parser, /*statement_p=*/true);
6620
6621   /* Free any declarators allocated.  */
6622   obstack_free (&declarator_obstack, p);
6623
6624   /* Finish off the statement.  */
6625   finish_stmt ();
6626 }
6627
6628 /* Some dependent statements (like `if (cond) statement'), are
6629    implicitly in their own scope.  In other words, if the statement is
6630    a single statement (as opposed to a compound-statement), it is
6631    none-the-less treated as if it were enclosed in braces.  Any
6632    declarations appearing in the dependent statement are out of scope
6633    after control passes that point.  This function parses a statement,
6634    but ensures that is in its own scope, even if it is not a
6635    compound-statement.
6636
6637    Returns the new statement.  */
6638
6639 static tree
6640 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6641 {
6642   tree statement;
6643
6644   /* If the token is not a `{', then we must take special action.  */
6645   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6646     {
6647       /* Create a compound-statement.  */
6648       statement = begin_compound_stmt (0);
6649       /* Parse the dependent-statement.  */
6650       cp_parser_statement (parser, false);
6651       /* Finish the dummy compound-statement.  */
6652       finish_compound_stmt (statement);
6653     }
6654   /* Otherwise, we simply parse the statement directly.  */
6655   else
6656     statement = cp_parser_compound_statement (parser, NULL, false);
6657
6658   /* Return the statement.  */
6659   return statement;
6660 }
6661
6662 /* For some dependent statements (like `while (cond) statement'), we
6663    have already created a scope.  Therefore, even if the dependent
6664    statement is a compound-statement, we do not want to create another
6665    scope.  */
6666
6667 static void
6668 cp_parser_already_scoped_statement (cp_parser* parser)
6669 {
6670   /* If the token is a `{', then we must take special action.  */
6671   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6672     cp_parser_statement (parser, false);
6673   else
6674     {
6675       /* Avoid calling cp_parser_compound_statement, so that we
6676          don't create a new scope.  Do everything else by hand.  */
6677       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6678       cp_parser_statement_seq_opt (parser, false);
6679       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6680     }
6681 }
6682
6683 /* Declarations [gram.dcl.dcl] */
6684
6685 /* Parse an optional declaration-sequence.
6686
6687    declaration-seq:
6688      declaration
6689      declaration-seq declaration  */
6690
6691 static void
6692 cp_parser_declaration_seq_opt (cp_parser* parser)
6693 {
6694   while (true)
6695     {
6696       cp_token *token;
6697
6698       token = cp_lexer_peek_token (parser->lexer);
6699
6700       if (token->type == CPP_CLOSE_BRACE
6701           || token->type == CPP_EOF)
6702         break;
6703
6704       if (token->type == CPP_SEMICOLON)
6705         {
6706           /* A declaration consisting of a single semicolon is
6707              invalid.  Allow it unless we're being pedantic.  */
6708           if (pedantic && !in_system_header)
6709             pedwarn ("extra `;'");
6710           cp_lexer_consume_token (parser->lexer);
6711           continue;
6712         }
6713
6714       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6715          parser to enter or exit implicit `extern "C"' blocks.  */
6716       while (pending_lang_change > 0)
6717         {
6718           push_lang_context (lang_name_c);
6719           --pending_lang_change;
6720         }
6721       while (pending_lang_change < 0)
6722         {
6723           pop_lang_context ();
6724           ++pending_lang_change;
6725         }
6726
6727       /* Parse the declaration itself.  */
6728       cp_parser_declaration (parser);
6729     }
6730 }
6731
6732 /* Parse a declaration.
6733
6734    declaration:
6735      block-declaration
6736      function-definition
6737      template-declaration
6738      explicit-instantiation
6739      explicit-specialization
6740      linkage-specification
6741      namespace-definition
6742
6743    GNU extension:
6744
6745    declaration:
6746       __extension__ declaration */
6747
6748 static void
6749 cp_parser_declaration (cp_parser* parser)
6750 {
6751   cp_token token1;
6752   cp_token token2;
6753   int saved_pedantic;
6754   void *p;
6755
6756   /* Set this here since we can be called after
6757      pushing the linkage specification.  */
6758   c_lex_string_translate = 1;
6759
6760   /* Check for the `__extension__' keyword.  */
6761   if (cp_parser_extension_opt (parser, &saved_pedantic))
6762     {
6763       /* Parse the qualified declaration.  */
6764       cp_parser_declaration (parser);
6765       /* Restore the PEDANTIC flag.  */
6766       pedantic = saved_pedantic;
6767
6768       return;
6769     }
6770
6771   /* Try to figure out what kind of declaration is present.  */
6772   token1 = *cp_lexer_peek_token (parser->lexer);
6773
6774   /* Don't translate the CPP_STRING in extern "C".  */
6775   if (token1.keyword == RID_EXTERN)
6776     c_lex_string_translate = 0;
6777
6778   if (token1.type != CPP_EOF)
6779     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6780
6781   c_lex_string_translate = 1;
6782
6783   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6784   p = obstack_alloc (&declarator_obstack, 0);
6785
6786   /* If the next token is `extern' and the following token is a string
6787      literal, then we have a linkage specification.  */
6788   if (token1.keyword == RID_EXTERN
6789       && cp_parser_is_string_literal (&token2))
6790     cp_parser_linkage_specification (parser);
6791   /* If the next token is `template', then we have either a template
6792      declaration, an explicit instantiation, or an explicit
6793      specialization.  */
6794   else if (token1.keyword == RID_TEMPLATE)
6795     {
6796       /* `template <>' indicates a template specialization.  */
6797       if (token2.type == CPP_LESS
6798           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6799         cp_parser_explicit_specialization (parser);
6800       /* `template <' indicates a template declaration.  */
6801       else if (token2.type == CPP_LESS)
6802         cp_parser_template_declaration (parser, /*member_p=*/false);
6803       /* Anything else must be an explicit instantiation.  */
6804       else
6805         cp_parser_explicit_instantiation (parser);
6806     }
6807   /* If the next token is `export', then we have a template
6808      declaration.  */
6809   else if (token1.keyword == RID_EXPORT)
6810     cp_parser_template_declaration (parser, /*member_p=*/false);
6811   /* If the next token is `extern', 'static' or 'inline' and the one
6812      after that is `template', we have a GNU extended explicit
6813      instantiation directive.  */
6814   else if (cp_parser_allow_gnu_extensions_p (parser)
6815            && (token1.keyword == RID_EXTERN
6816                || token1.keyword == RID_STATIC
6817                || token1.keyword == RID_INLINE)
6818            && token2.keyword == RID_TEMPLATE)
6819     cp_parser_explicit_instantiation (parser);
6820   /* If the next token is `namespace', check for a named or unnamed
6821      namespace definition.  */
6822   else if (token1.keyword == RID_NAMESPACE
6823            && (/* A named namespace definition.  */
6824                (token2.type == CPP_NAME
6825                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6826                     == CPP_OPEN_BRACE))
6827                /* An unnamed namespace definition.  */
6828                || token2.type == CPP_OPEN_BRACE))
6829     cp_parser_namespace_definition (parser);
6830   /* We must have either a block declaration or a function
6831      definition.  */
6832   else
6833     /* Try to parse a block-declaration, or a function-definition.  */
6834     cp_parser_block_declaration (parser, /*statement_p=*/false);
6835
6836   /* Free any declarators allocated.  */
6837   obstack_free (&declarator_obstack, p);
6838 }
6839
6840 /* Parse a block-declaration.
6841
6842    block-declaration:
6843      simple-declaration
6844      asm-definition
6845      namespace-alias-definition
6846      using-declaration
6847      using-directive
6848
6849    GNU Extension:
6850
6851    block-declaration:
6852      __extension__ block-declaration
6853      label-declaration
6854
6855    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6856    part of a declaration-statement.  */
6857
6858 static void
6859 cp_parser_block_declaration (cp_parser *parser,
6860                              bool      statement_p)
6861 {
6862   cp_token *token1;
6863   int saved_pedantic;
6864
6865   /* Check for the `__extension__' keyword.  */
6866   if (cp_parser_extension_opt (parser, &saved_pedantic))
6867     {
6868       /* Parse the qualified declaration.  */
6869       cp_parser_block_declaration (parser, statement_p);
6870       /* Restore the PEDANTIC flag.  */
6871       pedantic = saved_pedantic;
6872
6873       return;
6874     }
6875
6876   /* Peek at the next token to figure out which kind of declaration is
6877      present.  */
6878   token1 = cp_lexer_peek_token (parser->lexer);
6879
6880   /* If the next keyword is `asm', we have an asm-definition.  */
6881   if (token1->keyword == RID_ASM)
6882     {
6883       if (statement_p)
6884         cp_parser_commit_to_tentative_parse (parser);
6885       cp_parser_asm_definition (parser);
6886     }
6887   /* If the next keyword is `namespace', we have a
6888      namespace-alias-definition.  */
6889   else if (token1->keyword == RID_NAMESPACE)
6890     cp_parser_namespace_alias_definition (parser);
6891   /* If the next keyword is `using', we have either a
6892      using-declaration or a using-directive.  */
6893   else if (token1->keyword == RID_USING)
6894     {
6895       cp_token *token2;
6896
6897       if (statement_p)
6898         cp_parser_commit_to_tentative_parse (parser);
6899       /* If the token after `using' is `namespace', then we have a
6900          using-directive.  */
6901       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6902       if (token2->keyword == RID_NAMESPACE)
6903         cp_parser_using_directive (parser);
6904       /* Otherwise, it's a using-declaration.  */
6905       else
6906         cp_parser_using_declaration (parser);
6907     }
6908   /* If the next keyword is `__label__' we have a label declaration.  */
6909   else if (token1->keyword == RID_LABEL)
6910     {
6911       if (statement_p)
6912         cp_parser_commit_to_tentative_parse (parser);
6913       cp_parser_label_declaration (parser);
6914     }
6915   /* Anything else must be a simple-declaration.  */
6916   else
6917     cp_parser_simple_declaration (parser, !statement_p);
6918 }
6919
6920 /* Parse a simple-declaration.
6921
6922    simple-declaration:
6923      decl-specifier-seq [opt] init-declarator-list [opt] ;
6924
6925    init-declarator-list:
6926      init-declarator
6927      init-declarator-list , init-declarator
6928
6929    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6930    function-definition as a simple-declaration.  */
6931
6932 static void
6933 cp_parser_simple_declaration (cp_parser* parser,
6934                               bool function_definition_allowed_p)
6935 {
6936   cp_decl_specifier_seq decl_specifiers;
6937   int declares_class_or_enum;
6938   bool saw_declarator;
6939
6940   /* Defer access checks until we know what is being declared; the
6941      checks for names appearing in the decl-specifier-seq should be
6942      done as if we were in the scope of the thing being declared.  */
6943   push_deferring_access_checks (dk_deferred);
6944
6945   /* Parse the decl-specifier-seq.  We have to keep track of whether
6946      or not the decl-specifier-seq declares a named class or
6947      enumeration type, since that is the only case in which the
6948      init-declarator-list is allowed to be empty.
6949
6950      [dcl.dcl]
6951
6952      In a simple-declaration, the optional init-declarator-list can be
6953      omitted only when declaring a class or enumeration, that is when
6954      the decl-specifier-seq contains either a class-specifier, an
6955      elaborated-type-specifier, or an enum-specifier.  */
6956   cp_parser_decl_specifier_seq (parser,
6957                                 CP_PARSER_FLAGS_OPTIONAL,
6958                                 &decl_specifiers,
6959                                 &declares_class_or_enum);
6960   /* We no longer need to defer access checks.  */
6961   stop_deferring_access_checks ();
6962
6963   /* In a block scope, a valid declaration must always have a
6964      decl-specifier-seq.  By not trying to parse declarators, we can
6965      resolve the declaration/expression ambiguity more quickly.  */
6966   if (!function_definition_allowed_p 
6967       && !decl_specifiers.any_specifiers_p)
6968     {
6969       cp_parser_error (parser, "expected declaration");
6970       goto done;
6971     }
6972
6973   /* If the next two tokens are both identifiers, the code is
6974      erroneous. The usual cause of this situation is code like:
6975
6976        T t;
6977
6978      where "T" should name a type -- but does not.  */
6979   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6980     {
6981       /* If parsing tentatively, we should commit; we really are
6982          looking at a declaration.  */
6983       cp_parser_commit_to_tentative_parse (parser);
6984       /* Give up.  */
6985       goto done;
6986     }
6987
6988   /* Keep going until we hit the `;' at the end of the simple
6989      declaration.  */
6990   saw_declarator = false;
6991   while (cp_lexer_next_token_is_not (parser->lexer,
6992                                      CPP_SEMICOLON))
6993     {
6994       cp_token *token;
6995       bool function_definition_p;
6996       tree decl;
6997
6998       saw_declarator = true;
6999       /* Parse the init-declarator.  */
7000       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7001                                         function_definition_allowed_p,
7002                                         /*member_p=*/false,
7003                                         declares_class_or_enum,
7004                                         &function_definition_p);
7005       /* If an error occurred while parsing tentatively, exit quickly.
7006          (That usually happens when in the body of a function; each
7007          statement is treated as a declaration-statement until proven
7008          otherwise.)  */
7009       if (cp_parser_error_occurred (parser))
7010         goto done;
7011       /* Handle function definitions specially.  */
7012       if (function_definition_p)
7013         {
7014           /* If the next token is a `,', then we are probably
7015              processing something like:
7016
7017                void f() {}, *p;
7018
7019              which is erroneous.  */
7020           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7021             error ("mixing declarations and function-definitions is forbidden");
7022           /* Otherwise, we're done with the list of declarators.  */
7023           else
7024             {
7025               pop_deferring_access_checks ();
7026               return;
7027             }
7028         }
7029       /* The next token should be either a `,' or a `;'.  */
7030       token = cp_lexer_peek_token (parser->lexer);
7031       /* If it's a `,', there are more declarators to come.  */
7032       if (token->type == CPP_COMMA)
7033         cp_lexer_consume_token (parser->lexer);
7034       /* If it's a `;', we are done.  */
7035       else if (token->type == CPP_SEMICOLON)
7036         break;
7037       /* Anything else is an error.  */
7038       else
7039         {
7040           cp_parser_error (parser, "expected `,' or `;'");
7041           /* Skip tokens until we reach the end of the statement.  */
7042           cp_parser_skip_to_end_of_statement (parser);
7043           /* If the next token is now a `;', consume it.  */
7044           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7045             cp_lexer_consume_token (parser->lexer);
7046           goto done;
7047         }
7048       /* After the first time around, a function-definition is not
7049          allowed -- even if it was OK at first.  For example:
7050
7051            int i, f() {}
7052
7053          is not valid.  */
7054       function_definition_allowed_p = false;
7055     }
7056
7057   /* Issue an error message if no declarators are present, and the
7058      decl-specifier-seq does not itself declare a class or
7059      enumeration.  */
7060   if (!saw_declarator)
7061     {
7062       if (cp_parser_declares_only_class_p (parser))
7063         shadow_tag (&decl_specifiers);
7064       /* Perform any deferred access checks.  */
7065       perform_deferred_access_checks ();
7066     }
7067
7068   /* Consume the `;'.  */
7069   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7070
7071  done:
7072   pop_deferring_access_checks ();
7073 }
7074
7075 /* Parse a decl-specifier-seq.
7076
7077    decl-specifier-seq:
7078      decl-specifier-seq [opt] decl-specifier
7079
7080    decl-specifier:
7081      storage-class-specifier
7082      type-specifier
7083      function-specifier
7084      friend
7085      typedef
7086
7087    GNU Extension:
7088
7089    decl-specifier:
7090      attributes
7091
7092    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7093
7094    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7095    appears, and the entity that will be a friend is not going to be a
7096    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
7097    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
7098    friendship is granted might not be a class.
7099
7100    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7101    flags:
7102
7103      1: one of the decl-specifiers is an elaborated-type-specifier
7104         (i.e., a type declaration)
7105      2: one of the decl-specifiers is an enum-specifier or a
7106         class-specifier (i.e., a type definition)
7107    
7108    */
7109
7110 static void
7111 cp_parser_decl_specifier_seq (cp_parser* parser,
7112                               cp_parser_flags flags,
7113                               cp_decl_specifier_seq *decl_specs,
7114                               int* declares_class_or_enum)
7115 {
7116   bool constructor_possible_p = !parser->in_declarator_p;
7117
7118   /* Clear DECL_SPECS.  */
7119   clear_decl_specs (decl_specs);
7120
7121   /* Assume no class or enumeration type is declared.  */
7122   *declares_class_or_enum = 0;
7123
7124   /* Keep reading specifiers until there are no more to read.  */
7125   while (true)
7126     {
7127       bool constructor_p;
7128       bool found_decl_spec;
7129       cp_token *token;
7130
7131       /* Peek at the next token.  */
7132       token = cp_lexer_peek_token (parser->lexer);
7133       /* Handle attributes.  */
7134       if (token->keyword == RID_ATTRIBUTE)
7135         {
7136           /* Parse the attributes.  */
7137           decl_specs->attributes 
7138             = chainon (decl_specs->attributes,
7139                        cp_parser_attributes_opt (parser));
7140           continue;
7141         }
7142       /* Assume we will find a decl-specifier keyword.  */
7143       found_decl_spec = true;
7144       /* If the next token is an appropriate keyword, we can simply
7145          add it to the list.  */
7146       switch (token->keyword)
7147         {
7148           /* decl-specifier:
7149                friend  */
7150         case RID_FRIEND:
7151           if (decl_specs->specs[(int) ds_friend]++)
7152             error ("duplicate `friend'");
7153           /* Consume the token.  */
7154           cp_lexer_consume_token (parser->lexer);
7155           break;
7156
7157           /* function-specifier:
7158                inline
7159                virtual
7160                explicit  */
7161         case RID_INLINE:
7162         case RID_VIRTUAL:
7163         case RID_EXPLICIT:
7164           cp_parser_function_specifier_opt (parser, decl_specs);
7165           break;
7166
7167           /* decl-specifier:
7168                typedef  */
7169         case RID_TYPEDEF:
7170           ++decl_specs->specs[(int) ds_typedef];
7171           /* Consume the token.  */
7172           cp_lexer_consume_token (parser->lexer);
7173           /* A constructor declarator cannot appear in a typedef.  */
7174           constructor_possible_p = false;
7175           /* The "typedef" keyword can only occur in a declaration; we
7176              may as well commit at this point.  */
7177           cp_parser_commit_to_tentative_parse (parser);
7178           break;
7179
7180           /* storage-class-specifier:
7181                auto
7182                register
7183                static
7184                extern
7185                mutable
7186
7187              GNU Extension:
7188                thread  */
7189         case RID_AUTO:
7190           /* Consume the token.  */
7191           cp_lexer_consume_token (parser->lexer);
7192           cp_parser_set_storage_class (decl_specs, sc_auto);
7193           break;
7194         case RID_REGISTER:
7195           /* Consume the token.  */
7196           cp_lexer_consume_token (parser->lexer);
7197           cp_parser_set_storage_class (decl_specs, sc_register);
7198           break;
7199         case RID_STATIC:
7200           /* Consume the token.  */
7201           cp_lexer_consume_token (parser->lexer);
7202           if (decl_specs->specs[(int) ds_thread])
7203             {
7204               error ("`__thread' before `static'");
7205               decl_specs->specs[(int) ds_thread] = 0;
7206             }
7207           cp_parser_set_storage_class (decl_specs, sc_static);
7208           break;
7209         case RID_EXTERN:
7210           /* Consume the token.  */
7211           cp_lexer_consume_token (parser->lexer);
7212           if (decl_specs->specs[(int) ds_thread])
7213             {
7214               error ("`__thread' before `extern'");
7215               decl_specs->specs[(int) ds_thread] = 0;
7216             }
7217           cp_parser_set_storage_class (decl_specs, sc_extern);
7218           break;
7219         case RID_MUTABLE:
7220           /* Consume the token.  */
7221           cp_lexer_consume_token (parser->lexer);
7222           cp_parser_set_storage_class (decl_specs, sc_mutable);
7223           break;
7224         case RID_THREAD:
7225           /* Consume the token.  */
7226           cp_lexer_consume_token (parser->lexer);
7227           ++decl_specs->specs[(int) ds_thread];
7228           break;
7229
7230         default:
7231           /* We did not yet find a decl-specifier yet.  */
7232           found_decl_spec = false;
7233           break;
7234         }
7235
7236       /* Constructors are a special case.  The `S' in `S()' is not a
7237          decl-specifier; it is the beginning of the declarator.  */
7238       constructor_p 
7239         = (!found_decl_spec
7240            && constructor_possible_p
7241            && (cp_parser_constructor_declarator_p 
7242                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7243
7244       /* If we don't have a DECL_SPEC yet, then we must be looking at
7245          a type-specifier.  */
7246       if (!found_decl_spec && !constructor_p)
7247         {
7248           int decl_spec_declares_class_or_enum;
7249           bool is_cv_qualifier;
7250           tree type_spec;
7251
7252           type_spec
7253             = cp_parser_type_specifier (parser, flags,
7254                                         decl_specs,
7255                                         /*is_declaration=*/true,
7256                                         &decl_spec_declares_class_or_enum,
7257                                         &is_cv_qualifier);
7258
7259           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7260
7261           /* If this type-specifier referenced a user-defined type
7262              (a typedef, class-name, etc.), then we can't allow any
7263              more such type-specifiers henceforth.
7264
7265              [dcl.spec]
7266
7267              The longest sequence of decl-specifiers that could
7268              possibly be a type name is taken as the
7269              decl-specifier-seq of a declaration.  The sequence shall
7270              be self-consistent as described below.
7271
7272              [dcl.type]
7273
7274              As a general rule, at most one type-specifier is allowed
7275              in the complete decl-specifier-seq of a declaration.  The
7276              only exceptions are the following:
7277
7278              -- const or volatile can be combined with any other
7279                 type-specifier.
7280
7281              -- signed or unsigned can be combined with char, long,
7282                 short, or int.
7283
7284              -- ..
7285
7286              Example:
7287
7288                typedef char* Pc;
7289                void g (const int Pc);
7290
7291              Here, Pc is *not* part of the decl-specifier seq; it's
7292              the declarator.  Therefore, once we see a type-specifier
7293              (other than a cv-qualifier), we forbid any additional
7294              user-defined types.  We *do* still allow things like `int
7295              int' to be considered a decl-specifier-seq, and issue the
7296              error message later.  */
7297           if (type_spec && !is_cv_qualifier)
7298             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7299           /* A constructor declarator cannot follow a type-specifier.  */
7300           if (type_spec)
7301             {
7302               constructor_possible_p = false;
7303               found_decl_spec = true;
7304             }
7305         }
7306
7307       /* If we still do not have a DECL_SPEC, then there are no more
7308          decl-specifiers.  */
7309       if (!found_decl_spec)
7310         break;
7311
7312       decl_specs->any_specifiers_p = true;
7313       /* After we see one decl-specifier, further decl-specifiers are
7314          always optional.  */
7315       flags |= CP_PARSER_FLAGS_OPTIONAL;
7316     }
7317
7318   /* Don't allow a friend specifier with a class definition.  */
7319   if (decl_specs->specs[(int) ds_friend] != 0
7320       && (*declares_class_or_enum & 2))
7321     error ("class definition may not be declared a friend");
7322 }
7323
7324 /* Parse an (optional) storage-class-specifier.
7325
7326    storage-class-specifier:
7327      auto
7328      register
7329      static
7330      extern
7331      mutable
7332
7333    GNU Extension:
7334
7335    storage-class-specifier:
7336      thread
7337
7338    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7339
7340 static tree
7341 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7342 {
7343   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7344     {
7345     case RID_AUTO:
7346     case RID_REGISTER:
7347     case RID_STATIC:
7348     case RID_EXTERN:
7349     case RID_MUTABLE:
7350     case RID_THREAD:
7351       /* Consume the token.  */
7352       return cp_lexer_consume_token (parser->lexer)->value;
7353
7354     default:
7355       return NULL_TREE;
7356     }
7357 }
7358
7359 /* Parse an (optional) function-specifier.
7360
7361    function-specifier:
7362      inline
7363      virtual
7364      explicit
7365
7366    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7367    Updates DECL_SPECS, if it is non-NULL.  */
7368
7369 static tree
7370 cp_parser_function_specifier_opt (cp_parser* parser,
7371                                   cp_decl_specifier_seq *decl_specs)
7372 {
7373   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7374     {
7375     case RID_INLINE:
7376       if (decl_specs)
7377         ++decl_specs->specs[(int) ds_inline];
7378       break;
7379
7380     case RID_VIRTUAL:
7381       if (decl_specs)
7382         ++decl_specs->specs[(int) ds_virtual];
7383       break;
7384
7385     case RID_EXPLICIT:
7386       if (decl_specs)
7387         ++decl_specs->specs[(int) ds_explicit];
7388       break;
7389
7390     default:
7391       return NULL_TREE;
7392     }
7393
7394   /* Consume the token.  */
7395   return cp_lexer_consume_token (parser->lexer)->value;
7396 }
7397
7398 /* Parse a linkage-specification.
7399
7400    linkage-specification:
7401      extern string-literal { declaration-seq [opt] }
7402      extern string-literal declaration  */
7403
7404 static void
7405 cp_parser_linkage_specification (cp_parser* parser)
7406 {
7407   cp_token *token;
7408   tree linkage;
7409
7410   /* Look for the `extern' keyword.  */
7411   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7412
7413   /* Peek at the next token.  */
7414   token = cp_lexer_peek_token (parser->lexer);
7415   /* If it's not a string-literal, then there's a problem.  */
7416   if (!cp_parser_is_string_literal (token))
7417     {
7418       cp_parser_error (parser, "expected language-name");
7419       return;
7420     }
7421   /* Consume the token.  */
7422   cp_lexer_consume_token (parser->lexer);
7423
7424   /* Transform the literal into an identifier.  If the literal is a
7425      wide-character string, or contains embedded NULs, then we can't
7426      handle it as the user wants.  */
7427   if (token->type == CPP_WSTRING
7428       || (strlen (TREE_STRING_POINTER (token->value))
7429           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7430     {
7431       cp_parser_error (parser, "invalid linkage-specification");
7432       /* Assume C++ linkage.  */
7433       linkage = get_identifier ("c++");
7434     }
7435   /* If the string is chained to another string, take the latter,
7436      that's the untranslated string.  */
7437   else if (TREE_CHAIN (token->value))
7438     linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7439   /* If it's a simple string constant, things are easier.  */
7440   else
7441     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7442
7443   /* We're now using the new linkage.  */
7444   push_lang_context (linkage);
7445
7446   /* If the next token is a `{', then we're using the first
7447      production.  */
7448   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7449     {
7450       /* Consume the `{' token.  */
7451       cp_lexer_consume_token (parser->lexer);
7452       /* Parse the declarations.  */
7453       cp_parser_declaration_seq_opt (parser);
7454       /* Look for the closing `}'.  */
7455       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7456     }
7457   /* Otherwise, there's just one declaration.  */
7458   else
7459     {
7460       bool saved_in_unbraced_linkage_specification_p;
7461
7462       saved_in_unbraced_linkage_specification_p
7463         = parser->in_unbraced_linkage_specification_p;
7464       parser->in_unbraced_linkage_specification_p = true;
7465       have_extern_spec = true;
7466       cp_parser_declaration (parser);
7467       have_extern_spec = false;
7468       parser->in_unbraced_linkage_specification_p
7469         = saved_in_unbraced_linkage_specification_p;
7470     }
7471
7472   /* We're done with the linkage-specification.  */
7473   pop_lang_context ();
7474 }
7475
7476 /* Special member functions [gram.special] */
7477
7478 /* Parse a conversion-function-id.
7479
7480    conversion-function-id:
7481      operator conversion-type-id
7482
7483    Returns an IDENTIFIER_NODE representing the operator.  */
7484
7485 static tree
7486 cp_parser_conversion_function_id (cp_parser* parser)
7487 {
7488   tree type;
7489   tree saved_scope;
7490   tree saved_qualifying_scope;
7491   tree saved_object_scope;
7492   bool pop_p = false;
7493
7494   /* Look for the `operator' token.  */
7495   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7496     return error_mark_node;
7497   /* When we parse the conversion-type-id, the current scope will be
7498      reset.  However, we need that information in able to look up the
7499      conversion function later, so we save it here.  */
7500   saved_scope = parser->scope;
7501   saved_qualifying_scope = parser->qualifying_scope;
7502   saved_object_scope = parser->object_scope;
7503   /* We must enter the scope of the class so that the names of
7504      entities declared within the class are available in the
7505      conversion-type-id.  For example, consider:
7506
7507        struct S {
7508          typedef int I;
7509          operator I();
7510        };
7511
7512        S::operator I() { ... }
7513
7514      In order to see that `I' is a type-name in the definition, we
7515      must be in the scope of `S'.  */
7516   if (saved_scope)
7517     pop_p = push_scope (saved_scope);
7518   /* Parse the conversion-type-id.  */
7519   type = cp_parser_conversion_type_id (parser);
7520   /* Leave the scope of the class, if any.  */
7521   if (pop_p)
7522     pop_scope (saved_scope);
7523   /* Restore the saved scope.  */
7524   parser->scope = saved_scope;
7525   parser->qualifying_scope = saved_qualifying_scope;
7526   parser->object_scope = saved_object_scope;
7527   /* If the TYPE is invalid, indicate failure.  */
7528   if (type == error_mark_node)
7529     return error_mark_node;
7530   return mangle_conv_op_name_for_type (type);
7531 }
7532
7533 /* Parse a conversion-type-id:
7534
7535    conversion-type-id:
7536      type-specifier-seq conversion-declarator [opt]
7537
7538    Returns the TYPE specified.  */
7539
7540 static tree
7541 cp_parser_conversion_type_id (cp_parser* parser)
7542 {
7543   tree attributes;
7544   cp_decl_specifier_seq type_specifiers;
7545   cp_declarator *declarator;
7546
7547   /* Parse the attributes.  */
7548   attributes = cp_parser_attributes_opt (parser);
7549   /* Parse the type-specifiers.  */
7550   cp_parser_type_specifier_seq (parser, &type_specifiers);
7551   /* If that didn't work, stop.  */
7552   if (type_specifiers.type == error_mark_node)
7553     return error_mark_node;
7554   /* Parse the conversion-declarator.  */
7555   declarator = cp_parser_conversion_declarator_opt (parser);
7556
7557   return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7558                          /*initialized=*/0, &attributes);
7559 }
7560
7561 /* Parse an (optional) conversion-declarator.
7562
7563    conversion-declarator:
7564      ptr-operator conversion-declarator [opt]
7565
7566    */
7567
7568 static cp_declarator *
7569 cp_parser_conversion_declarator_opt (cp_parser* parser)
7570 {
7571   enum tree_code code;
7572   tree class_type;
7573   tree cv_qualifier_seq;
7574
7575   /* We don't know if there's a ptr-operator next, or not.  */
7576   cp_parser_parse_tentatively (parser);
7577   /* Try the ptr-operator.  */
7578   code = cp_parser_ptr_operator (parser, &class_type,
7579                                  &cv_qualifier_seq);
7580   /* If it worked, look for more conversion-declarators.  */
7581   if (cp_parser_parse_definitely (parser))
7582     {
7583       cp_declarator *declarator;
7584       
7585       /* Parse another optional declarator.  */
7586       declarator = cp_parser_conversion_declarator_opt (parser);
7587       
7588       /* Create the representation of the declarator.  */
7589       if (class_type)
7590         declarator = make_ptrmem_declarator (cv_qualifier_seq,
7591                                              class_type,
7592                                              declarator);
7593       else if (code == INDIRECT_REF)
7594         declarator = make_pointer_declarator (cv_qualifier_seq,
7595                                               declarator);
7596       else
7597         declarator = make_reference_declarator (cv_qualifier_seq,
7598                                                 declarator);
7599       
7600       return declarator;
7601    }
7602
7603   return NULL;
7604 }
7605
7606 /* Parse an (optional) ctor-initializer.
7607
7608    ctor-initializer:
7609      : mem-initializer-list
7610
7611    Returns TRUE iff the ctor-initializer was actually present.  */
7612
7613 static bool
7614 cp_parser_ctor_initializer_opt (cp_parser* parser)
7615 {
7616   /* If the next token is not a `:', then there is no
7617      ctor-initializer.  */
7618   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7619     {
7620       /* Do default initialization of any bases and members.  */
7621       if (DECL_CONSTRUCTOR_P (current_function_decl))
7622         finish_mem_initializers (NULL_TREE);
7623
7624       return false;
7625     }
7626
7627   /* Consume the `:' token.  */
7628   cp_lexer_consume_token (parser->lexer);
7629   /* And the mem-initializer-list.  */
7630   cp_parser_mem_initializer_list (parser);
7631
7632   return true;
7633 }
7634
7635 /* Parse a mem-initializer-list.
7636
7637    mem-initializer-list:
7638      mem-initializer
7639      mem-initializer , mem-initializer-list  */
7640
7641 static void
7642 cp_parser_mem_initializer_list (cp_parser* parser)
7643 {
7644   tree mem_initializer_list = NULL_TREE;
7645
7646   /* Let the semantic analysis code know that we are starting the
7647      mem-initializer-list.  */
7648   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7649     error ("only constructors take base initializers");
7650
7651   /* Loop through the list.  */
7652   while (true)
7653     {
7654       tree mem_initializer;
7655
7656       /* Parse the mem-initializer.  */
7657       mem_initializer = cp_parser_mem_initializer (parser);
7658       /* Add it to the list, unless it was erroneous.  */
7659       if (mem_initializer)
7660         {
7661           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7662           mem_initializer_list = mem_initializer;
7663         }
7664       /* If the next token is not a `,', we're done.  */
7665       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7666         break;
7667       /* Consume the `,' token.  */
7668       cp_lexer_consume_token (parser->lexer);
7669     }
7670
7671   /* Perform semantic analysis.  */
7672   if (DECL_CONSTRUCTOR_P (current_function_decl))
7673     finish_mem_initializers (mem_initializer_list);
7674 }
7675
7676 /* Parse a mem-initializer.
7677
7678    mem-initializer:
7679      mem-initializer-id ( expression-list [opt] )
7680
7681    GNU extension:
7682
7683    mem-initializer:
7684      ( expression-list [opt] )
7685
7686    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7687    class) or FIELD_DECL (for a non-static data member) to initialize;
7688    the TREE_VALUE is the expression-list.  */
7689
7690 static tree
7691 cp_parser_mem_initializer (cp_parser* parser)
7692 {
7693   tree mem_initializer_id;
7694   tree expression_list;
7695   tree member;
7696
7697   /* Find out what is being initialized.  */
7698   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7699     {
7700       pedwarn ("anachronistic old-style base class initializer");
7701       mem_initializer_id = NULL_TREE;
7702     }
7703   else
7704     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7705   member = expand_member_init (mem_initializer_id);
7706   if (member && !DECL_P (member))
7707     in_base_initializer = 1;
7708
7709   expression_list
7710     = cp_parser_parenthesized_expression_list (parser, false,
7711                                                /*non_constant_p=*/NULL);
7712   if (!expression_list)
7713     expression_list = void_type_node;
7714
7715   in_base_initializer = 0;
7716
7717   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7718 }
7719
7720 /* Parse a mem-initializer-id.
7721
7722    mem-initializer-id:
7723      :: [opt] nested-name-specifier [opt] class-name
7724      identifier
7725
7726    Returns a TYPE indicating the class to be initializer for the first
7727    production.  Returns an IDENTIFIER_NODE indicating the data member
7728    to be initialized for the second production.  */
7729
7730 static tree
7731 cp_parser_mem_initializer_id (cp_parser* parser)
7732 {
7733   bool global_scope_p;
7734   bool nested_name_specifier_p;
7735   bool template_p = false;
7736   tree id;
7737
7738   /* `typename' is not allowed in this context ([temp.res]).  */
7739   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7740     {
7741       error ("keyword `typename' not allowed in this context (a qualified "
7742              "member initializer is implicitly a type)");
7743       cp_lexer_consume_token (parser->lexer);
7744     }
7745   /* Look for the optional `::' operator.  */
7746   global_scope_p
7747     = (cp_parser_global_scope_opt (parser,
7748                                    /*current_scope_valid_p=*/false)
7749        != NULL_TREE);
7750   /* Look for the optional nested-name-specifier.  The simplest way to
7751      implement:
7752
7753        [temp.res]
7754
7755        The keyword `typename' is not permitted in a base-specifier or
7756        mem-initializer; in these contexts a qualified name that
7757        depends on a template-parameter is implicitly assumed to be a
7758        type name.
7759
7760      is to assume that we have seen the `typename' keyword at this
7761      point.  */
7762   nested_name_specifier_p
7763     = (cp_parser_nested_name_specifier_opt (parser,
7764                                             /*typename_keyword_p=*/true,
7765                                             /*check_dependency_p=*/true,
7766                                             /*type_p=*/true,
7767                                             /*is_declaration=*/true)
7768        != NULL_TREE);
7769   if (nested_name_specifier_p)
7770     template_p = cp_parser_optional_template_keyword (parser);
7771   /* If there is a `::' operator or a nested-name-specifier, then we
7772      are definitely looking for a class-name.  */
7773   if (global_scope_p || nested_name_specifier_p)
7774     return cp_parser_class_name (parser,
7775                                  /*typename_keyword_p=*/true,
7776                                  /*template_keyword_p=*/template_p,
7777                                  /*type_p=*/false,
7778                                  /*check_dependency_p=*/true,
7779                                  /*class_head_p=*/false,
7780                                  /*is_declaration=*/true);
7781   /* Otherwise, we could also be looking for an ordinary identifier.  */
7782   cp_parser_parse_tentatively (parser);
7783   /* Try a class-name.  */
7784   id = cp_parser_class_name (parser,
7785                              /*typename_keyword_p=*/true,
7786                              /*template_keyword_p=*/false,
7787                              /*type_p=*/false,
7788                              /*check_dependency_p=*/true,
7789                              /*class_head_p=*/false,
7790                              /*is_declaration=*/true);
7791   /* If we found one, we're done.  */
7792   if (cp_parser_parse_definitely (parser))
7793     return id;
7794   /* Otherwise, look for an ordinary identifier.  */
7795   return cp_parser_identifier (parser);
7796 }
7797
7798 /* Overloading [gram.over] */
7799
7800 /* Parse an operator-function-id.
7801
7802    operator-function-id:
7803      operator operator
7804
7805    Returns an IDENTIFIER_NODE for the operator which is a
7806    human-readable spelling of the identifier, e.g., `operator +'.  */
7807
7808 static tree
7809 cp_parser_operator_function_id (cp_parser* parser)
7810 {
7811   /* Look for the `operator' keyword.  */
7812   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7813     return error_mark_node;
7814   /* And then the name of the operator itself.  */
7815   return cp_parser_operator (parser);
7816 }
7817
7818 /* Parse an operator.
7819
7820    operator:
7821      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7822      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7823      || ++ -- , ->* -> () []
7824
7825    GNU Extensions:
7826
7827    operator:
7828      <? >? <?= >?=
7829
7830    Returns an IDENTIFIER_NODE for the operator which is a
7831    human-readable spelling of the identifier, e.g., `operator +'.  */
7832
7833 static tree
7834 cp_parser_operator (cp_parser* parser)
7835 {
7836   tree id = NULL_TREE;
7837   cp_token *token;
7838
7839   /* Peek at the next token.  */
7840   token = cp_lexer_peek_token (parser->lexer);
7841   /* Figure out which operator we have.  */
7842   switch (token->type)
7843     {
7844     case CPP_KEYWORD:
7845       {
7846         enum tree_code op;
7847
7848         /* The keyword should be either `new' or `delete'.  */
7849         if (token->keyword == RID_NEW)
7850           op = NEW_EXPR;
7851         else if (token->keyword == RID_DELETE)
7852           op = DELETE_EXPR;
7853         else
7854           break;
7855
7856         /* Consume the `new' or `delete' token.  */
7857         cp_lexer_consume_token (parser->lexer);
7858
7859         /* Peek at the next token.  */
7860         token = cp_lexer_peek_token (parser->lexer);
7861         /* If it's a `[' token then this is the array variant of the
7862            operator.  */
7863         if (token->type == CPP_OPEN_SQUARE)
7864           {
7865             /* Consume the `[' token.  */
7866             cp_lexer_consume_token (parser->lexer);
7867             /* Look for the `]' token.  */
7868             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7869             id = ansi_opname (op == NEW_EXPR
7870                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7871           }
7872         /* Otherwise, we have the non-array variant.  */
7873         else
7874           id = ansi_opname (op);
7875
7876         return id;
7877       }
7878
7879     case CPP_PLUS:
7880       id = ansi_opname (PLUS_EXPR);
7881       break;
7882
7883     case CPP_MINUS:
7884       id = ansi_opname (MINUS_EXPR);
7885       break;
7886
7887     case CPP_MULT:
7888       id = ansi_opname (MULT_EXPR);
7889       break;
7890
7891     case CPP_DIV:
7892       id = ansi_opname (TRUNC_DIV_EXPR);
7893       break;
7894
7895     case CPP_MOD:
7896       id = ansi_opname (TRUNC_MOD_EXPR);
7897       break;
7898
7899     case CPP_XOR:
7900       id = ansi_opname (BIT_XOR_EXPR);
7901       break;
7902
7903     case CPP_AND:
7904       id = ansi_opname (BIT_AND_EXPR);
7905       break;
7906
7907     case CPP_OR:
7908       id = ansi_opname (BIT_IOR_EXPR);
7909       break;
7910
7911     case CPP_COMPL:
7912       id = ansi_opname (BIT_NOT_EXPR);
7913       break;
7914
7915     case CPP_NOT:
7916       id = ansi_opname (TRUTH_NOT_EXPR);
7917       break;
7918
7919     case CPP_EQ:
7920       id = ansi_assopname (NOP_EXPR);
7921       break;
7922
7923     case CPP_LESS:
7924       id = ansi_opname (LT_EXPR);
7925       break;
7926
7927     case CPP_GREATER:
7928       id = ansi_opname (GT_EXPR);
7929       break;
7930
7931     case CPP_PLUS_EQ:
7932       id = ansi_assopname (PLUS_EXPR);
7933       break;
7934
7935     case CPP_MINUS_EQ:
7936       id = ansi_assopname (MINUS_EXPR);
7937       break;
7938
7939     case CPP_MULT_EQ:
7940       id = ansi_assopname (MULT_EXPR);
7941       break;
7942
7943     case CPP_DIV_EQ:
7944       id = ansi_assopname (TRUNC_DIV_EXPR);
7945       break;
7946
7947     case CPP_MOD_EQ:
7948       id = ansi_assopname (TRUNC_MOD_EXPR);
7949       break;
7950
7951     case CPP_XOR_EQ:
7952       id = ansi_assopname (BIT_XOR_EXPR);
7953       break;
7954
7955     case CPP_AND_EQ:
7956       id = ansi_assopname (BIT_AND_EXPR);
7957       break;
7958
7959     case CPP_OR_EQ:
7960       id = ansi_assopname (BIT_IOR_EXPR);
7961       break;
7962
7963     case CPP_LSHIFT:
7964       id = ansi_opname (LSHIFT_EXPR);
7965       break;
7966
7967     case CPP_RSHIFT:
7968       id = ansi_opname (RSHIFT_EXPR);
7969       break;
7970
7971     case CPP_LSHIFT_EQ:
7972       id = ansi_assopname (LSHIFT_EXPR);
7973       break;
7974
7975     case CPP_RSHIFT_EQ:
7976       id = ansi_assopname (RSHIFT_EXPR);
7977       break;
7978
7979     case CPP_EQ_EQ:
7980       id = ansi_opname (EQ_EXPR);
7981       break;
7982
7983     case CPP_NOT_EQ:
7984       id = ansi_opname (NE_EXPR);
7985       break;
7986
7987     case CPP_LESS_EQ:
7988       id = ansi_opname (LE_EXPR);
7989       break;
7990
7991     case CPP_GREATER_EQ:
7992       id = ansi_opname (GE_EXPR);
7993       break;
7994
7995     case CPP_AND_AND:
7996       id = ansi_opname (TRUTH_ANDIF_EXPR);
7997       break;
7998
7999     case CPP_OR_OR:
8000       id = ansi_opname (TRUTH_ORIF_EXPR);
8001       break;
8002
8003     case CPP_PLUS_PLUS:
8004       id = ansi_opname (POSTINCREMENT_EXPR);
8005       break;
8006
8007     case CPP_MINUS_MINUS:
8008       id = ansi_opname (PREDECREMENT_EXPR);
8009       break;
8010
8011     case CPP_COMMA:
8012       id = ansi_opname (COMPOUND_EXPR);
8013       break;
8014
8015     case CPP_DEREF_STAR:
8016       id = ansi_opname (MEMBER_REF);
8017       break;
8018
8019     case CPP_DEREF:
8020       id = ansi_opname (COMPONENT_REF);
8021       break;
8022
8023     case CPP_OPEN_PAREN:
8024       /* Consume the `('.  */
8025       cp_lexer_consume_token (parser->lexer);
8026       /* Look for the matching `)'.  */
8027       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8028       return ansi_opname (CALL_EXPR);
8029
8030     case CPP_OPEN_SQUARE:
8031       /* Consume the `['.  */
8032       cp_lexer_consume_token (parser->lexer);
8033       /* Look for the matching `]'.  */
8034       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8035       return ansi_opname (ARRAY_REF);
8036
8037       /* Extensions.  */
8038     case CPP_MIN:
8039       id = ansi_opname (MIN_EXPR);
8040       break;
8041
8042     case CPP_MAX:
8043       id = ansi_opname (MAX_EXPR);
8044       break;
8045
8046     case CPP_MIN_EQ:
8047       id = ansi_assopname (MIN_EXPR);
8048       break;
8049
8050     case CPP_MAX_EQ:
8051       id = ansi_assopname (MAX_EXPR);
8052       break;
8053
8054     default:
8055       /* Anything else is an error.  */
8056       break;
8057     }
8058
8059   /* If we have selected an identifier, we need to consume the
8060      operator token.  */
8061   if (id)
8062     cp_lexer_consume_token (parser->lexer);
8063   /* Otherwise, no valid operator name was present.  */
8064   else
8065     {
8066       cp_parser_error (parser, "expected operator");
8067       id = error_mark_node;
8068     }
8069
8070   return id;
8071 }
8072
8073 /* Parse a template-declaration.
8074
8075    template-declaration:
8076      export [opt] template < template-parameter-list > declaration
8077
8078    If MEMBER_P is TRUE, this template-declaration occurs within a
8079    class-specifier.
8080
8081    The grammar rule given by the standard isn't correct.  What
8082    is really meant is:
8083
8084    template-declaration:
8085      export [opt] template-parameter-list-seq
8086        decl-specifier-seq [opt] init-declarator [opt] ;
8087      export [opt] template-parameter-list-seq
8088        function-definition
8089
8090    template-parameter-list-seq:
8091      template-parameter-list-seq [opt]
8092      template < template-parameter-list >  */
8093
8094 static void
8095 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8096 {
8097   /* Check for `export'.  */
8098   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8099     {
8100       /* Consume the `export' token.  */
8101       cp_lexer_consume_token (parser->lexer);
8102       /* Warn that we do not support `export'.  */
8103       warning ("keyword `export' not implemented, and will be ignored");
8104     }
8105
8106   cp_parser_template_declaration_after_export (parser, member_p);
8107 }
8108
8109 /* Parse a template-parameter-list.
8110
8111    template-parameter-list:
8112      template-parameter
8113      template-parameter-list , template-parameter
8114
8115    Returns a TREE_LIST.  Each node represents a template parameter.
8116    The nodes are connected via their TREE_CHAINs.  */
8117
8118 static tree
8119 cp_parser_template_parameter_list (cp_parser* parser)
8120 {
8121   tree parameter_list = NULL_TREE;
8122
8123   while (true)
8124     {
8125       tree parameter;
8126       cp_token *token;
8127       bool is_non_type;
8128
8129       /* Parse the template-parameter.  */
8130       parameter = cp_parser_template_parameter (parser, &is_non_type);
8131       /* Add it to the list.  */
8132       parameter_list = process_template_parm (parameter_list,
8133                                               parameter,
8134                                               is_non_type);
8135       /* Peek at the next token.  */
8136       token = cp_lexer_peek_token (parser->lexer);
8137       /* If it's not a `,', we're done.  */
8138       if (token->type != CPP_COMMA)
8139         break;
8140       /* Otherwise, consume the `,' token.  */
8141       cp_lexer_consume_token (parser->lexer);
8142     }
8143
8144   return parameter_list;
8145 }
8146
8147 /* Parse a template-parameter.
8148
8149    template-parameter:
8150      type-parameter
8151      parameter-declaration
8152
8153    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8154    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8155    true iff this parameter is a non-type parameter.  */
8156
8157 static tree
8158 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8159 {
8160   cp_token *token;
8161   cp_parameter_declarator *parameter_declarator;
8162
8163   /* Assume it is a type parameter or a template parameter.  */
8164   *is_non_type = false;
8165   /* Peek at the next token.  */
8166   token = cp_lexer_peek_token (parser->lexer);
8167   /* If it is `class' or `template', we have a type-parameter.  */
8168   if (token->keyword == RID_TEMPLATE)
8169     return cp_parser_type_parameter (parser);
8170   /* If it is `class' or `typename' we do not know yet whether it is a
8171      type parameter or a non-type parameter.  Consider:
8172
8173        template <typename T, typename T::X X> ...
8174
8175      or:
8176
8177        template <class C, class D*> ...
8178
8179      Here, the first parameter is a type parameter, and the second is
8180      a non-type parameter.  We can tell by looking at the token after
8181      the identifier -- if it is a `,', `=', or `>' then we have a type
8182      parameter.  */
8183   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8184     {
8185       /* Peek at the token after `class' or `typename'.  */
8186       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8187       /* If it's an identifier, skip it.  */
8188       if (token->type == CPP_NAME)
8189         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8190       /* Now, see if the token looks like the end of a template
8191          parameter.  */
8192       if (token->type == CPP_COMMA
8193           || token->type == CPP_EQ
8194           || token->type == CPP_GREATER)
8195         return cp_parser_type_parameter (parser);
8196     }
8197
8198   /* Otherwise, it is a non-type parameter.
8199
8200      [temp.param]
8201
8202      When parsing a default template-argument for a non-type
8203      template-parameter, the first non-nested `>' is taken as the end
8204      of the template parameter-list rather than a greater-than
8205      operator.  */
8206   *is_non_type = true;
8207   parameter_declarator
8208      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8209                                         /*parenthesized_p=*/NULL);
8210   return (build_tree_list 
8211           (parameter_declarator->default_argument,
8212            grokdeclarator (parameter_declarator->declarator,
8213                            &parameter_declarator->decl_specifiers,
8214                            PARM, /*initialized=*/0, 
8215                            /*attrlist=*/NULL)));
8216 }
8217
8218 /* Parse a type-parameter.
8219
8220    type-parameter:
8221      class identifier [opt]
8222      class identifier [opt] = type-id
8223      typename identifier [opt]
8224      typename identifier [opt] = type-id
8225      template < template-parameter-list > class identifier [opt]
8226      template < template-parameter-list > class identifier [opt]
8227        = id-expression
8228
8229    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8230    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8231    the declaration of the parameter.  */
8232
8233 static tree
8234 cp_parser_type_parameter (cp_parser* parser)
8235 {
8236   cp_token *token;
8237   tree parameter;
8238
8239   /* Look for a keyword to tell us what kind of parameter this is.  */
8240   token = cp_parser_require (parser, CPP_KEYWORD,
8241                              "`class', `typename', or `template'");
8242   if (!token)
8243     return error_mark_node;
8244
8245   switch (token->keyword)
8246     {
8247     case RID_CLASS:
8248     case RID_TYPENAME:
8249       {
8250         tree identifier;
8251         tree default_argument;
8252
8253         /* If the next token is an identifier, then it names the
8254            parameter.  */
8255         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8256           identifier = cp_parser_identifier (parser);
8257         else
8258           identifier = NULL_TREE;
8259
8260         /* Create the parameter.  */
8261         parameter = finish_template_type_parm (class_type_node, identifier);
8262
8263         /* If the next token is an `=', we have a default argument.  */
8264         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8265           {
8266             /* Consume the `=' token.  */
8267             cp_lexer_consume_token (parser->lexer);
8268             /* Parse the default-argument.  */
8269             default_argument = cp_parser_type_id (parser);
8270           }
8271         else
8272           default_argument = NULL_TREE;
8273
8274         /* Create the combined representation of the parameter and the
8275            default argument.  */
8276         parameter = build_tree_list (default_argument, parameter);
8277       }
8278       break;
8279
8280     case RID_TEMPLATE:
8281       {
8282         tree parameter_list;
8283         tree identifier;
8284         tree default_argument;
8285
8286         /* Look for the `<'.  */
8287         cp_parser_require (parser, CPP_LESS, "`<'");
8288         /* Parse the template-parameter-list.  */
8289         begin_template_parm_list ();
8290         parameter_list
8291           = cp_parser_template_parameter_list (parser);
8292         parameter_list = end_template_parm_list (parameter_list);
8293         /* Look for the `>'.  */
8294         cp_parser_require (parser, CPP_GREATER, "`>'");
8295         /* Look for the `class' keyword.  */
8296         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8297         /* If the next token is an `=', then there is a
8298            default-argument.  If the next token is a `>', we are at
8299            the end of the parameter-list.  If the next token is a `,',
8300            then we are at the end of this parameter.  */
8301         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8302             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8303             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8304           identifier = cp_parser_identifier (parser);
8305         else
8306           identifier = NULL_TREE;
8307         /* Create the template parameter.  */
8308         parameter = finish_template_template_parm (class_type_node,
8309                                                    identifier);
8310
8311         /* If the next token is an `=', then there is a
8312            default-argument.  */
8313         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8314           {
8315             bool is_template;
8316
8317             /* Consume the `='.  */
8318             cp_lexer_consume_token (parser->lexer);
8319             /* Parse the id-expression.  */
8320             default_argument
8321               = cp_parser_id_expression (parser,
8322                                          /*template_keyword_p=*/false,
8323                                          /*check_dependency_p=*/true,
8324                                          /*template_p=*/&is_template,
8325                                          /*declarator_p=*/false);
8326             if (TREE_CODE (default_argument) == TYPE_DECL)
8327               /* If the id-expression was a template-id that refers to
8328                  a template-class, we already have the declaration here,
8329                  so no further lookup is needed.  */
8330                  ;
8331             else
8332               /* Look up the name.  */
8333               default_argument
8334                 = cp_parser_lookup_name (parser, default_argument,
8335                                         /*is_type=*/false,
8336                                         /*is_template=*/is_template,
8337                                         /*is_namespace=*/false,
8338                                         /*check_dependency=*/true);
8339             /* See if the default argument is valid.  */
8340             default_argument
8341               = check_template_template_default_arg (default_argument);
8342           }
8343         else
8344           default_argument = NULL_TREE;
8345
8346         /* Create the combined representation of the parameter and the
8347            default argument.  */
8348         parameter =  build_tree_list (default_argument, parameter);
8349       }
8350       break;
8351
8352     default:
8353       /* Anything else is an error.  */
8354       cp_parser_error (parser,
8355                        "expected `class', `typename', or `template'");
8356       parameter = error_mark_node;
8357     }
8358
8359   return parameter;
8360 }
8361
8362 /* Parse a template-id.
8363
8364    template-id:
8365      template-name < template-argument-list [opt] >
8366
8367    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8368    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8369    returned.  Otherwise, if the template-name names a function, or set
8370    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8371    names a class, returns a TYPE_DECL for the specialization.
8372
8373    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8374    uninstantiated templates.  */
8375
8376 static tree
8377 cp_parser_template_id (cp_parser *parser,
8378                        bool template_keyword_p,
8379                        bool check_dependency_p,
8380                        bool is_declaration)
8381 {
8382   tree template;
8383   tree arguments;
8384   tree template_id;
8385   ptrdiff_t start_of_id;
8386   tree access_check = NULL_TREE;
8387   cp_token *next_token, *next_token_2;
8388   bool is_identifier;
8389
8390   /* If the next token corresponds to a template-id, there is no need
8391      to reparse it.  */
8392   next_token = cp_lexer_peek_token (parser->lexer);
8393   if (next_token->type == CPP_TEMPLATE_ID)
8394     {
8395       tree value;
8396       tree check;
8397
8398       /* Get the stored value.  */
8399       value = cp_lexer_consume_token (parser->lexer)->value;
8400       /* Perform any access checks that were deferred.  */
8401       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8402         perform_or_defer_access_check (TREE_PURPOSE (check),
8403                                        TREE_VALUE (check));
8404       /* Return the stored value.  */
8405       return TREE_VALUE (value);
8406     }
8407
8408   /* Avoid performing name lookup if there is no possibility of
8409      finding a template-id.  */
8410   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8411       || (next_token->type == CPP_NAME
8412           && !cp_parser_nth_token_starts_template_argument_list_p
8413                (parser, 2)))
8414     {
8415       cp_parser_error (parser, "expected template-id");
8416       return error_mark_node;
8417     }
8418
8419   /* Remember where the template-id starts.  */
8420   if (cp_parser_parsing_tentatively (parser)
8421       && !cp_parser_committed_to_tentative_parse (parser))
8422     {
8423       next_token = cp_lexer_peek_token (parser->lexer);
8424       start_of_id = cp_lexer_token_difference (parser->lexer,
8425                                                parser->lexer->first_token,
8426                                                next_token);
8427     }
8428   else
8429     start_of_id = -1;
8430
8431   push_deferring_access_checks (dk_deferred);
8432
8433   /* Parse the template-name.  */
8434   is_identifier = false;
8435   template = cp_parser_template_name (parser, template_keyword_p,
8436                                       check_dependency_p,
8437                                       is_declaration,
8438                                       &is_identifier);
8439   if (template == error_mark_node || is_identifier)
8440     {
8441       pop_deferring_access_checks ();
8442       return template;
8443     }
8444
8445   /* If we find the sequence `[:' after a template-name, it's probably
8446      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8447      parse correctly the argument list.  */
8448   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8449   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8450   if (next_token->type == CPP_OPEN_SQUARE
8451       && next_token->flags & DIGRAPH
8452       && next_token_2->type == CPP_COLON
8453       && !(next_token_2->flags & PREV_WHITE))
8454     {
8455       cp_parser_parse_tentatively (parser);
8456       /* Change `:' into `::'.  */
8457       next_token_2->type = CPP_SCOPE;
8458       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8459          CPP_LESS.  */
8460       cp_lexer_consume_token (parser->lexer);
8461       /* Parse the arguments.  */
8462       arguments = cp_parser_enclosed_template_argument_list (parser);
8463       if (!cp_parser_parse_definitely (parser))
8464         {
8465           /* If we couldn't parse an argument list, then we revert our changes
8466              and return simply an error. Maybe this is not a template-id
8467              after all.  */
8468           next_token_2->type = CPP_COLON;
8469           cp_parser_error (parser, "expected `<'");
8470           pop_deferring_access_checks ();
8471           return error_mark_node;
8472         }
8473       /* Otherwise, emit an error about the invalid digraph, but continue
8474          parsing because we got our argument list.  */
8475       pedwarn ("`<::' cannot begin a template-argument list");
8476       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8477               "between `<' and `::'");
8478       if (!flag_permissive)
8479         {
8480           static bool hint;
8481           if (!hint)
8482             {
8483               inform ("(if you use `-fpermissive' G++ will accept your code)");
8484               hint = true;
8485             }
8486         }
8487     }
8488   else
8489     {
8490       /* Look for the `<' that starts the template-argument-list.  */
8491       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8492         {
8493           pop_deferring_access_checks ();
8494           return error_mark_node;
8495         }
8496       /* Parse the arguments.  */
8497       arguments = cp_parser_enclosed_template_argument_list (parser);
8498     }
8499
8500   /* Build a representation of the specialization.  */
8501   if (TREE_CODE (template) == IDENTIFIER_NODE)
8502     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8503   else if (DECL_CLASS_TEMPLATE_P (template)
8504            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8505     template_id
8506       = finish_template_type (template, arguments,
8507                               cp_lexer_next_token_is (parser->lexer,
8508                                                       CPP_SCOPE));
8509   else
8510     {
8511       /* If it's not a class-template or a template-template, it should be
8512          a function-template.  */
8513       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8514                            || TREE_CODE (template) == OVERLOAD
8515                            || BASELINK_P (template)),
8516                           20010716);
8517
8518       template_id = lookup_template_function (template, arguments);
8519     }
8520
8521   /* Retrieve any deferred checks.  Do not pop this access checks yet
8522      so the memory will not be reclaimed during token replacing below.  */
8523   access_check = get_deferred_access_checks ();
8524
8525   /* If parsing tentatively, replace the sequence of tokens that makes
8526      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8527      should we re-parse the token stream, we will not have to repeat
8528      the effort required to do the parse, nor will we issue duplicate
8529      error messages about problems during instantiation of the
8530      template.  */
8531   if (start_of_id >= 0)
8532     {
8533       cp_token *token;
8534
8535       /* Find the token that corresponds to the start of the
8536          template-id.  */
8537       token = cp_lexer_advance_token (parser->lexer,
8538                                       parser->lexer->first_token,
8539                                       start_of_id);
8540
8541       /* Reset the contents of the START_OF_ID token.  */
8542       token->type = CPP_TEMPLATE_ID;
8543       token->value = build_tree_list (access_check, template_id);
8544       token->keyword = RID_MAX;
8545       /* Purge all subsequent tokens.  */
8546       cp_lexer_purge_tokens_after (parser->lexer, token);
8547     }
8548
8549   pop_deferring_access_checks ();
8550   return template_id;
8551 }
8552
8553 /* Parse a template-name.
8554
8555    template-name:
8556      identifier
8557
8558    The standard should actually say:
8559
8560    template-name:
8561      identifier
8562      operator-function-id
8563
8564    A defect report has been filed about this issue.
8565
8566    A conversion-function-id cannot be a template name because they cannot
8567    be part of a template-id. In fact, looking at this code:
8568
8569    a.operator K<int>()
8570
8571    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8572    It is impossible to call a templated conversion-function-id with an
8573    explicit argument list, since the only allowed template parameter is
8574    the type to which it is converting.
8575
8576    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8577    `template' keyword, in a construction like:
8578
8579      T::template f<3>()
8580
8581    In that case `f' is taken to be a template-name, even though there
8582    is no way of knowing for sure.
8583
8584    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8585    name refers to a set of overloaded functions, at least one of which
8586    is a template, or an IDENTIFIER_NODE with the name of the template,
8587    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8588    names are looked up inside uninstantiated templates.  */
8589
8590 static tree
8591 cp_parser_template_name (cp_parser* parser,
8592                          bool template_keyword_p,
8593                          bool check_dependency_p,
8594                          bool is_declaration,
8595                          bool *is_identifier)
8596 {
8597   tree identifier;
8598   tree decl;
8599   tree fns;
8600
8601   /* If the next token is `operator', then we have either an
8602      operator-function-id or a conversion-function-id.  */
8603   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8604     {
8605       /* We don't know whether we're looking at an
8606          operator-function-id or a conversion-function-id.  */
8607       cp_parser_parse_tentatively (parser);
8608       /* Try an operator-function-id.  */
8609       identifier = cp_parser_operator_function_id (parser);
8610       /* If that didn't work, try a conversion-function-id.  */
8611       if (!cp_parser_parse_definitely (parser))
8612         {
8613           cp_parser_error (parser, "expected template-name");
8614           return error_mark_node;
8615         }
8616     }
8617   /* Look for the identifier.  */
8618   else
8619     identifier = cp_parser_identifier (parser);
8620
8621   /* If we didn't find an identifier, we don't have a template-id.  */
8622   if (identifier == error_mark_node)
8623     return error_mark_node;
8624
8625   /* If the name immediately followed the `template' keyword, then it
8626      is a template-name.  However, if the next token is not `<', then
8627      we do not treat it as a template-name, since it is not being used
8628      as part of a template-id.  This enables us to handle constructs
8629      like:
8630
8631        template <typename T> struct S { S(); };
8632        template <typename T> S<T>::S();
8633
8634      correctly.  We would treat `S' as a template -- if it were `S<T>'
8635      -- but we do not if there is no `<'.  */
8636
8637   if (processing_template_decl
8638       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8639     {
8640       /* In a declaration, in a dependent context, we pretend that the
8641          "template" keyword was present in order to improve error
8642          recovery.  For example, given:
8643
8644            template <typename T> void f(T::X<int>);
8645
8646          we want to treat "X<int>" as a template-id.  */
8647       if (is_declaration
8648           && !template_keyword_p
8649           && parser->scope && TYPE_P (parser->scope)
8650           && dependent_type_p (parser->scope)
8651           /* Do not do this for dtors (or ctors), since they never
8652              need the template keyword before their name.  */
8653           && !constructor_name_p (identifier, parser->scope))
8654         {
8655           ptrdiff_t start;
8656           cp_token* token;
8657           /* Explain what went wrong.  */
8658           error ("non-template `%D' used as template", identifier);
8659           inform ("use `%T::template %D' to indicate that it is a template",
8660                   parser->scope, identifier);
8661           /* If parsing tentatively, find the location of the "<"
8662              token.  */
8663           if (cp_parser_parsing_tentatively (parser)
8664               && !cp_parser_committed_to_tentative_parse (parser))
8665             {
8666               cp_parser_simulate_error (parser);
8667               token = cp_lexer_peek_token (parser->lexer);
8668               token = cp_lexer_prev_token (parser->lexer, token);
8669               start = cp_lexer_token_difference (parser->lexer,
8670                                                  parser->lexer->first_token,
8671                                                  token);
8672             }
8673           else
8674             start = -1;
8675           /* Parse the template arguments so that we can issue error
8676              messages about them.  */
8677           cp_lexer_consume_token (parser->lexer);
8678           cp_parser_enclosed_template_argument_list (parser);
8679           /* Skip tokens until we find a good place from which to
8680              continue parsing.  */
8681           cp_parser_skip_to_closing_parenthesis (parser,
8682                                                  /*recovering=*/true,
8683                                                  /*or_comma=*/true,
8684                                                  /*consume_paren=*/false);
8685           /* If parsing tentatively, permanently remove the
8686              template argument list.  That will prevent duplicate
8687              error messages from being issued about the missing
8688              "template" keyword.  */
8689           if (start >= 0)
8690             {
8691               token = cp_lexer_advance_token (parser->lexer,
8692                                               parser->lexer->first_token,
8693                                               start);
8694               cp_lexer_purge_tokens_after (parser->lexer, token);
8695             }
8696           if (is_identifier)
8697             *is_identifier = true;
8698           return identifier;
8699         }
8700
8701       /* If the "template" keyword is present, then there is generally
8702          no point in doing name-lookup, so we just return IDENTIFIER.
8703          But, if the qualifying scope is non-dependent then we can
8704          (and must) do name-lookup normally.  */
8705       if (template_keyword_p
8706           && (!parser->scope
8707               || (TYPE_P (parser->scope) 
8708                   && dependent_type_p (parser->scope))))
8709         return identifier;
8710     }
8711
8712   /* Look up the name.  */
8713   decl = cp_parser_lookup_name (parser, identifier,
8714                                 /*is_type=*/false,
8715                                 /*is_template=*/false,
8716                                 /*is_namespace=*/false,
8717                                 check_dependency_p);
8718   decl = maybe_get_template_decl_from_type_decl (decl);
8719
8720   /* If DECL is a template, then the name was a template-name.  */
8721   if (TREE_CODE (decl) == TEMPLATE_DECL)
8722     ;
8723   else
8724     {
8725       /* The standard does not explicitly indicate whether a name that
8726          names a set of overloaded declarations, some of which are
8727          templates, is a template-name.  However, such a name should
8728          be a template-name; otherwise, there is no way to form a
8729          template-id for the overloaded templates.  */
8730       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8731       if (TREE_CODE (fns) == OVERLOAD)
8732         {
8733           tree fn;
8734
8735           for (fn = fns; fn; fn = OVL_NEXT (fn))
8736             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8737               break;
8738         }
8739       else
8740         {
8741           /* Otherwise, the name does not name a template.  */
8742           cp_parser_error (parser, "expected template-name");
8743           return error_mark_node;
8744         }
8745     }
8746
8747   /* If DECL is dependent, and refers to a function, then just return
8748      its name; we will look it up again during template instantiation.  */
8749   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8750     {
8751       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8752       if (TYPE_P (scope) && dependent_type_p (scope))
8753         return identifier;
8754     }
8755
8756   return decl;
8757 }
8758
8759 /* Parse a template-argument-list.
8760
8761    template-argument-list:
8762      template-argument
8763      template-argument-list , template-argument
8764
8765    Returns a TREE_VEC containing the arguments.  */
8766
8767 static tree
8768 cp_parser_template_argument_list (cp_parser* parser)
8769 {
8770   tree fixed_args[10];
8771   unsigned n_args = 0;
8772   unsigned alloced = 10;
8773   tree *arg_ary = fixed_args;
8774   tree vec;
8775   bool saved_in_template_argument_list_p;
8776
8777   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8778   parser->in_template_argument_list_p = true;
8779   do
8780     {
8781       tree argument;
8782
8783       if (n_args)
8784         /* Consume the comma.  */
8785         cp_lexer_consume_token (parser->lexer);
8786
8787       /* Parse the template-argument.  */
8788       argument = cp_parser_template_argument (parser);
8789       if (n_args == alloced)
8790         {
8791           alloced *= 2;
8792
8793           if (arg_ary == fixed_args)
8794             {
8795               arg_ary = xmalloc (sizeof (tree) * alloced);
8796               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8797             }
8798           else
8799             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8800         }
8801       arg_ary[n_args++] = argument;
8802     }
8803   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8804
8805   vec = make_tree_vec (n_args);
8806
8807   while (n_args--)
8808     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8809
8810   if (arg_ary != fixed_args)
8811     free (arg_ary);
8812   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8813   return vec;
8814 }
8815
8816 /* Parse a template-argument.
8817
8818    template-argument:
8819      assignment-expression
8820      type-id
8821      id-expression
8822
8823    The representation is that of an assignment-expression, type-id, or
8824    id-expression -- except that the qualified id-expression is
8825    evaluated, so that the value returned is either a DECL or an
8826    OVERLOAD.
8827
8828    Although the standard says "assignment-expression", it forbids
8829    throw-expressions or assignments in the template argument.
8830    Therefore, we use "conditional-expression" instead.  */
8831
8832 static tree
8833 cp_parser_template_argument (cp_parser* parser)
8834 {
8835   tree argument;
8836   bool template_p;
8837   bool address_p;
8838   bool maybe_type_id = false;
8839   cp_token *token;
8840   cp_id_kind idk;
8841   tree qualifying_class;
8842
8843   /* There's really no way to know what we're looking at, so we just
8844      try each alternative in order.
8845
8846        [temp.arg]
8847
8848        In a template-argument, an ambiguity between a type-id and an
8849        expression is resolved to a type-id, regardless of the form of
8850        the corresponding template-parameter.
8851
8852      Therefore, we try a type-id first.  */
8853   cp_parser_parse_tentatively (parser);
8854   argument = cp_parser_type_id (parser);
8855   /* If there was no error parsing the type-id but the next token is a '>>',
8856      we probably found a typo for '> >'. But there are type-id which are
8857      also valid expressions. For instance:
8858
8859      struct X { int operator >> (int); };
8860      template <int V> struct Foo {};
8861      Foo<X () >> 5> r;
8862
8863      Here 'X()' is a valid type-id of a function type, but the user just
8864      wanted to write the expression "X() >> 5". Thus, we remember that we
8865      found a valid type-id, but we still try to parse the argument as an
8866      expression to see what happens.  */
8867   if (!cp_parser_error_occurred (parser)
8868       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8869     {
8870       maybe_type_id = true;
8871       cp_parser_abort_tentative_parse (parser);
8872     }
8873   else
8874     {
8875       /* If the next token isn't a `,' or a `>', then this argument wasn't
8876       really finished. This means that the argument is not a valid
8877       type-id.  */
8878       if (!cp_parser_next_token_ends_template_argument_p (parser))
8879         cp_parser_error (parser, "expected template-argument");
8880       /* If that worked, we're done.  */
8881       if (cp_parser_parse_definitely (parser))
8882         return argument;
8883     }
8884   /* We're still not sure what the argument will be.  */
8885   cp_parser_parse_tentatively (parser);
8886   /* Try a template.  */
8887   argument = cp_parser_id_expression (parser,
8888                                       /*template_keyword_p=*/false,
8889                                       /*check_dependency_p=*/true,
8890                                       &template_p,
8891                                       /*declarator_p=*/false);
8892   /* If the next token isn't a `,' or a `>', then this argument wasn't
8893      really finished.  */
8894   if (!cp_parser_next_token_ends_template_argument_p (parser))
8895     cp_parser_error (parser, "expected template-argument");
8896   if (!cp_parser_error_occurred (parser))
8897     {
8898       /* Figure out what is being referred to.  If the id-expression
8899          was for a class template specialization, then we will have a
8900          TYPE_DECL at this point.  There is no need to do name lookup
8901          at this point in that case.  */
8902       if (TREE_CODE (argument) != TYPE_DECL)
8903         argument = cp_parser_lookup_name (parser, argument,
8904                                           /*is_type=*/false,
8905                                           /*is_template=*/template_p,
8906                                           /*is_namespace=*/false,
8907                                           /*check_dependency=*/true);
8908       if (TREE_CODE (argument) != TEMPLATE_DECL
8909           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8910         cp_parser_error (parser, "expected template-name");
8911     }
8912   if (cp_parser_parse_definitely (parser))
8913     return argument;
8914   /* It must be a non-type argument.  There permitted cases are given
8915      in [temp.arg.nontype]:
8916
8917      -- an integral constant-expression of integral or enumeration
8918         type; or
8919
8920      -- the name of a non-type template-parameter; or
8921
8922      -- the name of an object or function with external linkage...
8923
8924      -- the address of an object or function with external linkage...
8925
8926      -- a pointer to member...  */
8927   /* Look for a non-type template parameter.  */
8928   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8929     {
8930       cp_parser_parse_tentatively (parser);
8931       argument = cp_parser_primary_expression (parser,
8932                                                &idk,
8933                                                &qualifying_class);
8934       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8935           || !cp_parser_next_token_ends_template_argument_p (parser))
8936         cp_parser_simulate_error (parser);
8937       if (cp_parser_parse_definitely (parser))
8938         return argument;
8939     }
8940   /* If the next token is "&", the argument must be the address of an
8941      object or function with external linkage.  */
8942   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8943   if (address_p)
8944     cp_lexer_consume_token (parser->lexer);
8945   /* See if we might have an id-expression.  */
8946   token = cp_lexer_peek_token (parser->lexer);
8947   if (token->type == CPP_NAME
8948       || token->keyword == RID_OPERATOR
8949       || token->type == CPP_SCOPE
8950       || token->type == CPP_TEMPLATE_ID
8951       || token->type == CPP_NESTED_NAME_SPECIFIER)
8952     {
8953       cp_parser_parse_tentatively (parser);
8954       argument = cp_parser_primary_expression (parser,
8955                                                &idk,
8956                                                &qualifying_class);
8957       if (cp_parser_error_occurred (parser)
8958           || !cp_parser_next_token_ends_template_argument_p (parser))
8959         cp_parser_abort_tentative_parse (parser);
8960       else
8961         {
8962           if (qualifying_class)
8963             argument = finish_qualified_id_expr (qualifying_class,
8964                                                  argument,
8965                                                  /*done=*/true,
8966                                                  address_p);
8967           if (TREE_CODE (argument) == VAR_DECL)
8968             {
8969               /* A variable without external linkage might still be a
8970                  valid constant-expression, so no error is issued here
8971                  if the external-linkage check fails.  */
8972               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8973                 cp_parser_simulate_error (parser);
8974             }
8975           else if (is_overloaded_fn (argument))
8976             /* All overloaded functions are allowed; if the external
8977                linkage test does not pass, an error will be issued
8978                later.  */
8979             ;
8980           else if (address_p
8981                    && (TREE_CODE (argument) == OFFSET_REF
8982                        || TREE_CODE (argument) == SCOPE_REF))
8983             /* A pointer-to-member.  */
8984             ;
8985           else
8986             cp_parser_simulate_error (parser);
8987
8988           if (cp_parser_parse_definitely (parser))
8989             {
8990               if (address_p)
8991                 argument = build_x_unary_op (ADDR_EXPR, argument);
8992               return argument;
8993             }
8994         }
8995     }
8996   /* If the argument started with "&", there are no other valid
8997      alternatives at this point.  */
8998   if (address_p)
8999     {
9000       cp_parser_error (parser, "invalid non-type template argument");
9001       return error_mark_node;
9002     }
9003   /* If the argument wasn't successfully parsed as a type-id followed
9004      by '>>', the argument can only be a constant expression now.
9005      Otherwise, we try parsing the constant-expression tentatively,
9006      because the argument could really be a type-id.  */
9007   if (maybe_type_id)
9008     cp_parser_parse_tentatively (parser);
9009   argument = cp_parser_constant_expression (parser,
9010                                             /*allow_non_constant_p=*/false,
9011                                             /*non_constant_p=*/NULL);
9012   argument = fold_non_dependent_expr (argument);
9013   if (!maybe_type_id)
9014     return argument;
9015   if (!cp_parser_next_token_ends_template_argument_p (parser))
9016     cp_parser_error (parser, "expected template-argument");
9017   if (cp_parser_parse_definitely (parser))
9018     return argument;
9019   /* We did our best to parse the argument as a non type-id, but that
9020      was the only alternative that matched (albeit with a '>' after
9021      it). We can assume it's just a typo from the user, and a
9022      diagnostic will then be issued.  */
9023   return cp_parser_type_id (parser);
9024 }
9025
9026 /* Parse an explicit-instantiation.
9027
9028    explicit-instantiation:
9029      template declaration
9030
9031    Although the standard says `declaration', what it really means is:
9032
9033    explicit-instantiation:
9034      template decl-specifier-seq [opt] declarator [opt] ;
9035
9036    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9037    supposed to be allowed.  A defect report has been filed about this
9038    issue.
9039
9040    GNU Extension:
9041
9042    explicit-instantiation:
9043      storage-class-specifier template
9044        decl-specifier-seq [opt] declarator [opt] ;
9045      function-specifier template
9046        decl-specifier-seq [opt] declarator [opt] ;  */
9047
9048 static void
9049 cp_parser_explicit_instantiation (cp_parser* parser)
9050 {
9051   int declares_class_or_enum;
9052   cp_decl_specifier_seq decl_specifiers;
9053   tree extension_specifier = NULL_TREE;
9054
9055   /* Look for an (optional) storage-class-specifier or
9056      function-specifier.  */
9057   if (cp_parser_allow_gnu_extensions_p (parser))
9058     {
9059       extension_specifier
9060         = cp_parser_storage_class_specifier_opt (parser);
9061       if (!extension_specifier)
9062         extension_specifier 
9063           = cp_parser_function_specifier_opt (parser,
9064                                               /*decl_specs=*/NULL);
9065     }
9066
9067   /* Look for the `template' keyword.  */
9068   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9069   /* Let the front end know that we are processing an explicit
9070      instantiation.  */
9071   begin_explicit_instantiation ();
9072   /* [temp.explicit] says that we are supposed to ignore access
9073      control while processing explicit instantiation directives.  */
9074   push_deferring_access_checks (dk_no_check);
9075   /* Parse a decl-specifier-seq.  */
9076   cp_parser_decl_specifier_seq (parser,
9077                                 CP_PARSER_FLAGS_OPTIONAL,
9078                                 &decl_specifiers,
9079                                 &declares_class_or_enum);
9080   /* If there was exactly one decl-specifier, and it declared a class,
9081      and there's no declarator, then we have an explicit type
9082      instantiation.  */
9083   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9084     {
9085       tree type;
9086
9087       type = check_tag_decl (&decl_specifiers);
9088       /* Turn access control back on for names used during
9089          template instantiation.  */
9090       pop_deferring_access_checks ();
9091       if (type)
9092         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9093     }
9094   else
9095     {
9096       cp_declarator *declarator;
9097       tree decl;
9098
9099       /* Parse the declarator.  */
9100       declarator
9101         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9102                                 /*ctor_dtor_or_conv_p=*/NULL,
9103                                 /*parenthesized_p=*/NULL);
9104       cp_parser_check_for_definition_in_return_type (declarator,
9105                                                      declares_class_or_enum);
9106       if (declarator != cp_error_declarator)
9107         {
9108           decl = grokdeclarator (declarator, &decl_specifiers,
9109                                  NORMAL, 0, NULL);
9110           /* Turn access control back on for names used during
9111              template instantiation.  */
9112           pop_deferring_access_checks ();
9113           /* Do the explicit instantiation.  */
9114           do_decl_instantiation (decl, extension_specifier);
9115         }
9116       else
9117         {
9118           pop_deferring_access_checks ();
9119           /* Skip the body of the explicit instantiation.  */
9120           cp_parser_skip_to_end_of_statement (parser);
9121         }
9122     }
9123   /* We're done with the instantiation.  */
9124   end_explicit_instantiation ();
9125
9126   cp_parser_consume_semicolon_at_end_of_statement (parser);
9127 }
9128
9129 /* Parse an explicit-specialization.
9130
9131    explicit-specialization:
9132      template < > declaration
9133
9134    Although the standard says `declaration', what it really means is:
9135
9136    explicit-specialization:
9137      template <> decl-specifier [opt] init-declarator [opt] ;
9138      template <> function-definition
9139      template <> explicit-specialization
9140      template <> template-declaration  */
9141
9142 static void
9143 cp_parser_explicit_specialization (cp_parser* parser)
9144 {
9145   /* Look for the `template' keyword.  */
9146   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9147   /* Look for the `<'.  */
9148   cp_parser_require (parser, CPP_LESS, "`<'");
9149   /* Look for the `>'.  */
9150   cp_parser_require (parser, CPP_GREATER, "`>'");
9151   /* We have processed another parameter list.  */
9152   ++parser->num_template_parameter_lists;
9153   /* Let the front end know that we are beginning a specialization.  */
9154   begin_specialization ();
9155
9156   /* If the next keyword is `template', we need to figure out whether
9157      or not we're looking a template-declaration.  */
9158   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9159     {
9160       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9161           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9162         cp_parser_template_declaration_after_export (parser,
9163                                                      /*member_p=*/false);
9164       else
9165         cp_parser_explicit_specialization (parser);
9166     }
9167   else
9168     /* Parse the dependent declaration.  */
9169     cp_parser_single_declaration (parser,
9170                                   /*member_p=*/false,
9171                                   /*friend_p=*/NULL);
9172
9173   /* We're done with the specialization.  */
9174   end_specialization ();
9175   /* We're done with this parameter list.  */
9176   --parser->num_template_parameter_lists;
9177 }
9178
9179 /* Parse a type-specifier.
9180
9181    type-specifier:
9182      simple-type-specifier
9183      class-specifier
9184      enum-specifier
9185      elaborated-type-specifier
9186      cv-qualifier
9187
9188    GNU Extension:
9189
9190    type-specifier:
9191      __complex__
9192
9193    Returns a representation of the type-specifier.  For a
9194    class-specifier, enum-specifier, or elaborated-type-specifier, a
9195    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9196
9197    If IS_FRIEND is TRUE then this type-specifier is being declared a
9198    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
9199    appearing in a decl-specifier-seq.
9200
9201    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9202    class-specifier, enum-specifier, or elaborated-type-specifier, then
9203    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9204    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9205    zero.
9206
9207    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9208    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9209    is set to FALSE.  */
9210
9211 static tree
9212 cp_parser_type_specifier (cp_parser* parser,
9213                           cp_parser_flags flags,
9214                           cp_decl_specifier_seq *decl_specs,
9215                           bool is_declaration,
9216                           int* declares_class_or_enum,
9217                           bool* is_cv_qualifier)
9218 {
9219   tree type_spec = NULL_TREE;
9220   cp_token *token;
9221   enum rid keyword;
9222   cp_decl_spec ds = ds_last;
9223
9224   /* Assume this type-specifier does not declare a new type.  */
9225   if (declares_class_or_enum)
9226     *declares_class_or_enum = 0;
9227   /* And that it does not specify a cv-qualifier.  */
9228   if (is_cv_qualifier)
9229     *is_cv_qualifier = false;
9230   /* Peek at the next token.  */
9231   token = cp_lexer_peek_token (parser->lexer);
9232
9233   /* If we're looking at a keyword, we can use that to guide the
9234      production we choose.  */
9235   keyword = token->keyword;
9236   switch (keyword)
9237     {
9238       /* Any of these indicate either a class-specifier, or an
9239          elaborated-type-specifier.  */
9240     case RID_CLASS:
9241     case RID_STRUCT:
9242     case RID_UNION:
9243     case RID_ENUM:
9244       /* Parse tentatively so that we can back up if we don't find a
9245          class-specifier or enum-specifier.  */
9246       cp_parser_parse_tentatively (parser);
9247       /* Look for the class-specifier or enum-specifier.  */
9248       if (keyword == RID_ENUM)
9249         type_spec = cp_parser_enum_specifier (parser);
9250       else
9251         type_spec = cp_parser_class_specifier (parser);
9252
9253       /* If that worked, we're done.  */
9254       if (cp_parser_parse_definitely (parser))
9255         {
9256           if (declares_class_or_enum)
9257             *declares_class_or_enum = 2;
9258           if (decl_specs)
9259             cp_parser_set_decl_spec_type (decl_specs,
9260                                           type_spec,
9261                                           /*user_defined_p=*/true);
9262           return type_spec;
9263         }
9264
9265       /* Fall through.  */
9266
9267     case RID_TYPENAME:
9268       /* Look for an elaborated-type-specifier.  */
9269       type_spec 
9270         = (cp_parser_elaborated_type_specifier 
9271            (parser,
9272             decl_specs && decl_specs->specs[(int) ds_friend],
9273             is_declaration));
9274       /* We're declaring a class or enum -- unless we're using
9275          `typename'.  */
9276       if (declares_class_or_enum && keyword != RID_TYPENAME)
9277         *declares_class_or_enum = 1;
9278       if (decl_specs)
9279         cp_parser_set_decl_spec_type (decl_specs,
9280                                       type_spec,
9281                                       /*user_defined_p=*/true);
9282       return type_spec;
9283
9284     case RID_CONST:
9285       ds = ds_const;
9286       if (is_cv_qualifier)
9287         *is_cv_qualifier = true;
9288       break;
9289       
9290     case RID_VOLATILE:
9291       ds = ds_volatile;
9292       if (is_cv_qualifier)
9293         *is_cv_qualifier = true;
9294       break;
9295
9296     case RID_RESTRICT:
9297       ds = ds_restrict;
9298       if (is_cv_qualifier)
9299         *is_cv_qualifier = true;
9300       break;
9301
9302     case RID_COMPLEX:
9303       /* The `__complex__' keyword is a GNU extension.  */
9304       ds = ds_complex;
9305       break;
9306
9307     default:
9308       break;
9309     }
9310
9311   /* Handle simple keywords.  */
9312   if (ds != ds_last)
9313     {
9314       if (decl_specs)
9315         {
9316           ++decl_specs->specs[(int)ds];
9317           decl_specs->any_specifiers_p = true;
9318         }
9319       return cp_lexer_consume_token (parser->lexer)->value;
9320     }
9321
9322   /* If we do not already have a type-specifier, assume we are looking
9323      at a simple-type-specifier.  */
9324   type_spec = cp_parser_simple_type_specifier (parser, 
9325                                                decl_specs,
9326                                                flags);
9327
9328   /* If we didn't find a type-specifier, and a type-specifier was not
9329      optional in this context, issue an error message.  */
9330   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9331     {
9332       cp_parser_error (parser, "expected type specifier");
9333       return error_mark_node;
9334     }
9335
9336   return type_spec;
9337 }
9338
9339 /* Parse a simple-type-specifier.
9340
9341    simple-type-specifier:
9342      :: [opt] nested-name-specifier [opt] type-name
9343      :: [opt] nested-name-specifier template template-id
9344      char
9345      wchar_t
9346      bool
9347      short
9348      int
9349      long
9350      signed
9351      unsigned
9352      float
9353      double
9354      void
9355
9356    GNU Extension:
9357
9358    simple-type-specifier:
9359      __typeof__ unary-expression
9360      __typeof__ ( type-id )
9361
9362    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9363    appropriately updated.  */
9364
9365 static tree
9366 cp_parser_simple_type_specifier (cp_parser* parser, 
9367                                  cp_decl_specifier_seq *decl_specs,
9368                                  cp_parser_flags flags)
9369 {
9370   tree type = NULL_TREE;
9371   cp_token *token;
9372
9373   /* Peek at the next token.  */
9374   token = cp_lexer_peek_token (parser->lexer);
9375
9376   /* If we're looking at a keyword, things are easy.  */
9377   switch (token->keyword)
9378     {
9379     case RID_CHAR:
9380       if (decl_specs)
9381         decl_specs->explicit_char_p = true;
9382       type = char_type_node;
9383       break;
9384     case RID_WCHAR:
9385       type = wchar_type_node;
9386       break;
9387     case RID_BOOL:
9388       type = boolean_type_node;
9389       break;
9390     case RID_SHORT:
9391       if (decl_specs)
9392         ++decl_specs->specs[(int) ds_short];
9393       type = short_integer_type_node;
9394       break;
9395     case RID_INT:
9396       if (decl_specs)
9397         decl_specs->explicit_int_p = true;
9398       type = integer_type_node;
9399       break;
9400     case RID_LONG:
9401       if (decl_specs)
9402         ++decl_specs->specs[(int) ds_long];
9403       type = long_integer_type_node;
9404       break;
9405     case RID_SIGNED:
9406       if (decl_specs)
9407         ++decl_specs->specs[(int) ds_signed];
9408       type = integer_type_node;
9409       break;
9410     case RID_UNSIGNED:
9411       if (decl_specs)
9412         ++decl_specs->specs[(int) ds_unsigned];
9413       type = unsigned_type_node;
9414       break;
9415     case RID_FLOAT:
9416       type = float_type_node;
9417       break;
9418     case RID_DOUBLE:
9419       type = double_type_node;
9420       break;
9421     case RID_VOID:
9422       type = void_type_node;
9423       break;
9424
9425     case RID_TYPEOF:
9426       /* Consume the `typeof' token.  */
9427       cp_lexer_consume_token (parser->lexer);
9428       /* Parse the operand to `typeof'.  */
9429       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9430       /* If it is not already a TYPE, take its type.  */
9431       if (!TYPE_P (type))
9432         type = finish_typeof (type);
9433
9434       if (decl_specs)
9435         cp_parser_set_decl_spec_type (decl_specs, type,
9436                                       /*user_defined_p=*/true);
9437         
9438       return type;
9439
9440     default:
9441       break;
9442     }
9443
9444   /* If the type-specifier was for a built-in type, we're done.  */
9445   if (type)
9446     {
9447       tree id;
9448
9449       /* Record the type.  */
9450       if (decl_specs
9451           && (token->keyword != RID_SIGNED
9452               && token->keyword != RID_UNSIGNED
9453               && token->keyword != RID_SHORT
9454               && token->keyword != RID_LONG))
9455         cp_parser_set_decl_spec_type (decl_specs, 
9456                                       type,
9457                                       /*user_defined=*/false);
9458       if (decl_specs)
9459         decl_specs->any_specifiers_p = true;
9460
9461       /* Consume the token.  */
9462       id = cp_lexer_consume_token (parser->lexer)->value;
9463
9464       /* There is no valid C++ program where a non-template type is
9465          followed by a "<".  That usually indicates that the user thought
9466          that the type was a template.  */
9467       cp_parser_check_for_invalid_template_id (parser, type);
9468
9469       return TYPE_NAME (type);
9470     }
9471
9472   /* The type-specifier must be a user-defined type.  */
9473   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9474     {
9475       bool qualified_p;
9476
9477       /* Don't gobble tokens or issue error messages if this is an
9478          optional type-specifier.  */
9479       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9480         cp_parser_parse_tentatively (parser);
9481
9482       /* Look for the optional `::' operator.  */
9483       cp_parser_global_scope_opt (parser,
9484                                   /*current_scope_valid_p=*/false);
9485       /* Look for the nested-name specifier.  */
9486       qualified_p
9487         = (cp_parser_nested_name_specifier_opt (parser,
9488                                                 /*typename_keyword_p=*/false,
9489                                                 /*check_dependency_p=*/true,
9490                                                 /*type_p=*/false,
9491                                                 /*is_declaration=*/false)
9492            != NULL_TREE);
9493       /* If we have seen a nested-name-specifier, and the next token
9494          is `template', then we are using the template-id production.  */
9495       if (parser->scope
9496           && cp_parser_optional_template_keyword (parser))
9497         {
9498           /* Look for the template-id.  */
9499           type = cp_parser_template_id (parser,
9500                                         /*template_keyword_p=*/true,
9501                                         /*check_dependency_p=*/true,
9502                                         /*is_declaration=*/false);
9503           /* If the template-id did not name a type, we are out of
9504              luck.  */
9505           if (TREE_CODE (type) != TYPE_DECL)
9506             {
9507               cp_parser_error (parser, "expected template-id for type");
9508               type = NULL_TREE;
9509             }
9510         }
9511       /* Otherwise, look for a type-name.  */
9512       else
9513         type = cp_parser_type_name (parser);
9514       /* Keep track of all name-lookups performed in class scopes.  */
9515       if (type  
9516           && !qualified_p
9517           && TREE_CODE (type) == TYPE_DECL 
9518           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9519         maybe_note_name_used_in_class (DECL_NAME (type), type);
9520       /* If it didn't work out, we don't have a TYPE.  */
9521       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9522           && !cp_parser_parse_definitely (parser))
9523         type = NULL_TREE;
9524       if (type && decl_specs)
9525         cp_parser_set_decl_spec_type (decl_specs, type,
9526                                       /*user_defined=*/true);
9527     }
9528
9529   /* If we didn't get a type-name, issue an error message.  */
9530   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9531     {
9532       cp_parser_error (parser, "expected type-name");
9533       return error_mark_node;
9534     }
9535
9536   /* There is no valid C++ program where a non-template type is
9537      followed by a "<".  That usually indicates that the user thought
9538      that the type was a template.  */
9539   if (type && type != error_mark_node)
9540     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9541
9542   return type;
9543 }
9544
9545 /* Parse a type-name.
9546
9547    type-name:
9548      class-name
9549      enum-name
9550      typedef-name
9551
9552    enum-name:
9553      identifier
9554
9555    typedef-name:
9556      identifier
9557
9558    Returns a TYPE_DECL for the the type.  */
9559
9560 static tree
9561 cp_parser_type_name (cp_parser* parser)
9562 {
9563   tree type_decl;
9564   tree identifier;
9565
9566   /* We can't know yet whether it is a class-name or not.  */
9567   cp_parser_parse_tentatively (parser);
9568   /* Try a class-name.  */
9569   type_decl = cp_parser_class_name (parser,
9570                                     /*typename_keyword_p=*/false,
9571                                     /*template_keyword_p=*/false,
9572                                     /*type_p=*/false,
9573                                     /*check_dependency_p=*/true,
9574                                     /*class_head_p=*/false,
9575                                     /*is_declaration=*/false);
9576   /* If it's not a class-name, keep looking.  */
9577   if (!cp_parser_parse_definitely (parser))
9578     {
9579       /* It must be a typedef-name or an enum-name.  */
9580       identifier = cp_parser_identifier (parser);
9581       if (identifier == error_mark_node)
9582         return error_mark_node;
9583
9584       /* Look up the type-name.  */
9585       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9586       /* Issue an error if we did not find a type-name.  */
9587       if (TREE_CODE (type_decl) != TYPE_DECL)
9588         {
9589           if (!cp_parser_simulate_error (parser))
9590             cp_parser_name_lookup_error (parser, identifier, type_decl,
9591                                          "is not a type");
9592           type_decl = error_mark_node;
9593         }
9594       /* Remember that the name was used in the definition of the
9595          current class so that we can check later to see if the
9596          meaning would have been different after the class was
9597          entirely defined.  */
9598       else if (type_decl != error_mark_node
9599                && !parser->scope)
9600         maybe_note_name_used_in_class (identifier, type_decl);
9601     }
9602
9603   return type_decl;
9604 }
9605
9606
9607 /* Parse an elaborated-type-specifier.  Note that the grammar given
9608    here incorporates the resolution to DR68.
9609
9610    elaborated-type-specifier:
9611      class-key :: [opt] nested-name-specifier [opt] identifier
9612      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9613      enum :: [opt] nested-name-specifier [opt] identifier
9614      typename :: [opt] nested-name-specifier identifier
9615      typename :: [opt] nested-name-specifier template [opt]
9616        template-id
9617
9618    GNU extension:
9619
9620    elaborated-type-specifier:
9621      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9622      class-key attributes :: [opt] nested-name-specifier [opt]
9623                template [opt] template-id
9624      enum attributes :: [opt] nested-name-specifier [opt] identifier
9625
9626    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9627    declared `friend'.  If IS_DECLARATION is TRUE, then this
9628    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9629    something is being declared.
9630
9631    Returns the TYPE specified.  */
9632
9633 static tree
9634 cp_parser_elaborated_type_specifier (cp_parser* parser,
9635                                      bool is_friend,
9636                                      bool is_declaration)
9637 {
9638   enum tag_types tag_type;
9639   tree identifier;
9640   tree type = NULL_TREE;
9641   tree attributes = NULL_TREE;
9642
9643   /* See if we're looking at the `enum' keyword.  */
9644   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9645     {
9646       /* Consume the `enum' token.  */
9647       cp_lexer_consume_token (parser->lexer);
9648       /* Remember that it's an enumeration type.  */
9649       tag_type = enum_type;
9650       /* Parse the attributes.  */
9651       attributes = cp_parser_attributes_opt (parser);
9652     }
9653   /* Or, it might be `typename'.  */
9654   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9655                                            RID_TYPENAME))
9656     {
9657       /* Consume the `typename' token.  */
9658       cp_lexer_consume_token (parser->lexer);
9659       /* Remember that it's a `typename' type.  */
9660       tag_type = typename_type;
9661       /* The `typename' keyword is only allowed in templates.  */
9662       if (!processing_template_decl)
9663         pedwarn ("using `typename' outside of template");
9664     }
9665   /* Otherwise it must be a class-key.  */
9666   else
9667     {
9668       tag_type = cp_parser_class_key (parser);
9669       if (tag_type == none_type)
9670         return error_mark_node;
9671       /* Parse the attributes.  */
9672       attributes = cp_parser_attributes_opt (parser);
9673     }
9674
9675   /* Look for the `::' operator.  */
9676   cp_parser_global_scope_opt (parser,
9677                               /*current_scope_valid_p=*/false);
9678   /* Look for the nested-name-specifier.  */
9679   if (tag_type == typename_type)
9680     {
9681       if (cp_parser_nested_name_specifier (parser,
9682                                            /*typename_keyword_p=*/true,
9683                                            /*check_dependency_p=*/true,
9684                                            /*type_p=*/true,
9685                                            is_declaration)
9686           == error_mark_node)
9687         return error_mark_node;
9688     }
9689   else
9690     /* Even though `typename' is not present, the proposed resolution
9691        to Core Issue 180 says that in `class A<T>::B', `B' should be
9692        considered a type-name, even if `A<T>' is dependent.  */
9693     cp_parser_nested_name_specifier_opt (parser,
9694                                          /*typename_keyword_p=*/true,
9695                                          /*check_dependency_p=*/true,
9696                                          /*type_p=*/true,
9697                                          is_declaration);
9698   /* For everything but enumeration types, consider a template-id.  */
9699   if (tag_type != enum_type)
9700     {
9701       bool template_p = false;
9702       tree decl;
9703
9704       /* Allow the `template' keyword.  */
9705       template_p = cp_parser_optional_template_keyword (parser);
9706       /* If we didn't see `template', we don't know if there's a
9707          template-id or not.  */
9708       if (!template_p)
9709         cp_parser_parse_tentatively (parser);
9710       /* Parse the template-id.  */
9711       decl = cp_parser_template_id (parser, template_p,
9712                                     /*check_dependency_p=*/true,
9713                                     is_declaration);
9714       /* If we didn't find a template-id, look for an ordinary
9715          identifier.  */
9716       if (!template_p && !cp_parser_parse_definitely (parser))
9717         ;
9718       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9719          in effect, then we must assume that, upon instantiation, the
9720          template will correspond to a class.  */
9721       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9722                && tag_type == typename_type)
9723         type = make_typename_type (parser->scope, decl,
9724                                    /*complain=*/1);
9725       else
9726         type = TREE_TYPE (decl);
9727     }
9728
9729   /* For an enumeration type, consider only a plain identifier.  */
9730   if (!type)
9731     {
9732       identifier = cp_parser_identifier (parser);
9733
9734       if (identifier == error_mark_node)
9735         {
9736           parser->scope = NULL_TREE;
9737           return error_mark_node;
9738         }
9739
9740       /* For a `typename', we needn't call xref_tag.  */
9741       if (tag_type == typename_type)
9742         return cp_parser_make_typename_type (parser, parser->scope,
9743                                              identifier);
9744       /* Look up a qualified name in the usual way.  */
9745       if (parser->scope)
9746         {
9747           tree decl;
9748
9749           /* In an elaborated-type-specifier, names are assumed to name
9750              types, so we set IS_TYPE to TRUE when calling
9751              cp_parser_lookup_name.  */
9752           decl = cp_parser_lookup_name (parser, identifier,
9753                                         /*is_type=*/true,
9754                                         /*is_template=*/false,
9755                                         /*is_namespace=*/false,
9756                                         /*check_dependency=*/true);
9757
9758           /* If we are parsing friend declaration, DECL may be a
9759              TEMPLATE_DECL tree node here.  However, we need to check
9760              whether this TEMPLATE_DECL results in valid code.  Consider
9761              the following example:
9762
9763                namespace N {
9764                  template <class T> class C {};
9765                }
9766                class X {
9767                  template <class T> friend class N::C; // #1, valid code
9768                };
9769                template <class T> class Y {
9770                  friend class N::C;                    // #2, invalid code
9771                };
9772
9773              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9774              name lookup of `N::C'.  We see that friend declaration must
9775              be template for the code to be valid.  Note that
9776              processing_template_decl does not work here since it is
9777              always 1 for the above two cases.  */
9778
9779           decl = (cp_parser_maybe_treat_template_as_class
9780                   (decl, /*tag_name_p=*/is_friend
9781                          && parser->num_template_parameter_lists));
9782
9783           if (TREE_CODE (decl) != TYPE_DECL)
9784             {
9785               error ("expected type-name");
9786               return error_mark_node;
9787             }
9788
9789           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9790             check_elaborated_type_specifier
9791               (tag_type, decl,
9792                (parser->num_template_parameter_lists
9793                 || DECL_SELF_REFERENCE_P (decl)));
9794
9795           type = TREE_TYPE (decl);
9796         }
9797       else
9798         {
9799           /* An elaborated-type-specifier sometimes introduces a new type and
9800              sometimes names an existing type.  Normally, the rule is that it
9801              introduces a new type only if there is not an existing type of
9802              the same name already in scope.  For example, given:
9803
9804                struct S {};
9805                void f() { struct S s; }
9806
9807              the `struct S' in the body of `f' is the same `struct S' as in
9808              the global scope; the existing definition is used.  However, if
9809              there were no global declaration, this would introduce a new
9810              local class named `S'.
9811
9812              An exception to this rule applies to the following code:
9813
9814                namespace N { struct S; }
9815
9816              Here, the elaborated-type-specifier names a new type
9817              unconditionally; even if there is already an `S' in the
9818              containing scope this declaration names a new type.
9819              This exception only applies if the elaborated-type-specifier
9820              forms the complete declaration:
9821
9822                [class.name]
9823
9824                A declaration consisting solely of `class-key identifier ;' is
9825                either a redeclaration of the name in the current scope or a
9826                forward declaration of the identifier as a class name.  It
9827                introduces the name into the current scope.
9828
9829              We are in this situation precisely when the next token is a `;'.
9830
9831              An exception to the exception is that a `friend' declaration does
9832              *not* name a new type; i.e., given:
9833
9834                struct S { friend struct T; };
9835
9836              `T' is not a new type in the scope of `S'.
9837
9838              Also, `new struct S' or `sizeof (struct S)' never results in the
9839              definition of a new type; a new type can only be declared in a
9840              declaration context.  */
9841
9842           /* Warn about attributes. They are ignored.  */
9843           if (attributes)
9844             warning ("type attributes are honored only at type definition");
9845
9846           type = xref_tag (tag_type, identifier,
9847                            (is_friend
9848                             || !is_declaration
9849                             || cp_lexer_next_token_is_not (parser->lexer,
9850                                                            CPP_SEMICOLON)),
9851                            parser->num_template_parameter_lists);
9852         }
9853     }
9854   if (tag_type != enum_type)
9855     cp_parser_check_class_key (tag_type, type);
9856
9857   /* A "<" cannot follow an elaborated type specifier.  If that
9858      happens, the user was probably trying to form a template-id.  */
9859   cp_parser_check_for_invalid_template_id (parser, type);
9860
9861   return type;
9862 }
9863
9864 /* Parse an enum-specifier.
9865
9866    enum-specifier:
9867      enum identifier [opt] { enumerator-list [opt] }
9868
9869    Returns an ENUM_TYPE representing the enumeration.  */
9870
9871 static tree
9872 cp_parser_enum_specifier (cp_parser* parser)
9873 {
9874   cp_token *token;
9875   tree identifier = NULL_TREE;
9876   tree type;
9877
9878   /* Look for the `enum' keyword.  */
9879   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9880     return error_mark_node;
9881   /* Peek at the next token.  */
9882   token = cp_lexer_peek_token (parser->lexer);
9883
9884   /* See if it is an identifier.  */
9885   if (token->type == CPP_NAME)
9886     identifier = cp_parser_identifier (parser);
9887
9888   /* Look for the `{'.  */
9889   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9890     return error_mark_node;
9891
9892   /* At this point, we're going ahead with the enum-specifier, even
9893      if some other problem occurs.  */
9894   cp_parser_commit_to_tentative_parse (parser);
9895
9896   /* Issue an error message if type-definitions are forbidden here.  */
9897   cp_parser_check_type_definition (parser);
9898
9899   /* Create the new type.  */
9900   type = start_enum (identifier ? identifier : make_anon_name ());
9901
9902   /* Peek at the next token.  */
9903   token = cp_lexer_peek_token (parser->lexer);
9904   /* If it's not a `}', then there are some enumerators.  */
9905   if (token->type != CPP_CLOSE_BRACE)
9906     cp_parser_enumerator_list (parser, type);
9907   /* Look for the `}'.  */
9908   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9909
9910   /* Finish up the enumeration.  */
9911   finish_enum (type);
9912
9913   return type;
9914 }
9915
9916 /* Parse an enumerator-list.  The enumerators all have the indicated
9917    TYPE.
9918
9919    enumerator-list:
9920      enumerator-definition
9921      enumerator-list , enumerator-definition  */
9922
9923 static void
9924 cp_parser_enumerator_list (cp_parser* parser, tree type)
9925 {
9926   while (true)
9927     {
9928       cp_token *token;
9929
9930       /* Parse an enumerator-definition.  */
9931       cp_parser_enumerator_definition (parser, type);
9932       /* Peek at the next token.  */
9933       token = cp_lexer_peek_token (parser->lexer);
9934       /* If it's not a `,', then we've reached the end of the
9935          list.  */
9936       if (token->type != CPP_COMMA)
9937         break;
9938       /* Otherwise, consume the `,' and keep going.  */
9939       cp_lexer_consume_token (parser->lexer);
9940       /* If the next token is a `}', there is a trailing comma.  */
9941       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9942         {
9943           if (pedantic && !in_system_header)
9944             pedwarn ("comma at end of enumerator list");
9945           break;
9946         }
9947     }
9948 }
9949
9950 /* Parse an enumerator-definition.  The enumerator has the indicated
9951    TYPE.
9952
9953    enumerator-definition:
9954      enumerator
9955      enumerator = constant-expression
9956
9957    enumerator:
9958      identifier  */
9959
9960 static void
9961 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9962 {
9963   cp_token *token;
9964   tree identifier;
9965   tree value;
9966
9967   /* Look for the identifier.  */
9968   identifier = cp_parser_identifier (parser);
9969   if (identifier == error_mark_node)
9970     return;
9971
9972   /* Peek at the next token.  */
9973   token = cp_lexer_peek_token (parser->lexer);
9974   /* If it's an `=', then there's an explicit value.  */
9975   if (token->type == CPP_EQ)
9976     {
9977       /* Consume the `=' token.  */
9978       cp_lexer_consume_token (parser->lexer);
9979       /* Parse the value.  */
9980       value = cp_parser_constant_expression (parser,
9981                                              /*allow_non_constant_p=*/false,
9982                                              NULL);
9983     }
9984   else
9985     value = NULL_TREE;
9986
9987   /* Create the enumerator.  */
9988   build_enumerator (identifier, value, type);
9989 }
9990
9991 /* Parse a namespace-name.
9992
9993    namespace-name:
9994      original-namespace-name
9995      namespace-alias
9996
9997    Returns the NAMESPACE_DECL for the namespace.  */
9998
9999 static tree
10000 cp_parser_namespace_name (cp_parser* parser)
10001 {
10002   tree identifier;
10003   tree namespace_decl;
10004
10005   /* Get the name of the namespace.  */
10006   identifier = cp_parser_identifier (parser);
10007   if (identifier == error_mark_node)
10008     return error_mark_node;
10009
10010   /* Look up the identifier in the currently active scope.  Look only
10011      for namespaces, due to:
10012
10013        [basic.lookup.udir]
10014
10015        When looking up a namespace-name in a using-directive or alias
10016        definition, only namespace names are considered.
10017
10018      And:
10019
10020        [basic.lookup.qual]
10021
10022        During the lookup of a name preceding the :: scope resolution
10023        operator, object, function, and enumerator names are ignored.
10024
10025      (Note that cp_parser_class_or_namespace_name only calls this
10026      function if the token after the name is the scope resolution
10027      operator.)  */
10028   namespace_decl = cp_parser_lookup_name (parser, identifier,
10029                                           /*is_type=*/false,
10030                                           /*is_template=*/false,
10031                                           /*is_namespace=*/true,
10032                                           /*check_dependency=*/true);
10033   /* If it's not a namespace, issue an error.  */
10034   if (namespace_decl == error_mark_node
10035       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10036     {
10037       cp_parser_error (parser, "expected namespace-name");
10038       namespace_decl = error_mark_node;
10039     }
10040
10041   return namespace_decl;
10042 }
10043
10044 /* Parse a namespace-definition.
10045
10046    namespace-definition:
10047      named-namespace-definition
10048      unnamed-namespace-definition
10049
10050    named-namespace-definition:
10051      original-namespace-definition
10052      extension-namespace-definition
10053
10054    original-namespace-definition:
10055      namespace identifier { namespace-body }
10056
10057    extension-namespace-definition:
10058      namespace original-namespace-name { namespace-body }
10059
10060    unnamed-namespace-definition:
10061      namespace { namespace-body } */
10062
10063 static void
10064 cp_parser_namespace_definition (cp_parser* parser)
10065 {
10066   tree identifier;
10067
10068   /* Look for the `namespace' keyword.  */
10069   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10070
10071   /* Get the name of the namespace.  We do not attempt to distinguish
10072      between an original-namespace-definition and an
10073      extension-namespace-definition at this point.  The semantic
10074      analysis routines are responsible for that.  */
10075   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10076     identifier = cp_parser_identifier (parser);
10077   else
10078     identifier = NULL_TREE;
10079
10080   /* Look for the `{' to start the namespace.  */
10081   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10082   /* Start the namespace.  */
10083   push_namespace (identifier);
10084   /* Parse the body of the namespace.  */
10085   cp_parser_namespace_body (parser);
10086   /* Finish the namespace.  */
10087   pop_namespace ();
10088   /* Look for the final `}'.  */
10089   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10090 }
10091
10092 /* Parse a namespace-body.
10093
10094    namespace-body:
10095      declaration-seq [opt]  */
10096
10097 static void
10098 cp_parser_namespace_body (cp_parser* parser)
10099 {
10100   cp_parser_declaration_seq_opt (parser);
10101 }
10102
10103 /* Parse a namespace-alias-definition.
10104
10105    namespace-alias-definition:
10106      namespace identifier = qualified-namespace-specifier ;  */
10107
10108 static void
10109 cp_parser_namespace_alias_definition (cp_parser* parser)
10110 {
10111   tree identifier;
10112   tree namespace_specifier;
10113
10114   /* Look for the `namespace' keyword.  */
10115   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10116   /* Look for the identifier.  */
10117   identifier = cp_parser_identifier (parser);
10118   if (identifier == error_mark_node)
10119     return;
10120   /* Look for the `=' token.  */
10121   cp_parser_require (parser, CPP_EQ, "`='");
10122   /* Look for the qualified-namespace-specifier.  */
10123   namespace_specifier
10124     = cp_parser_qualified_namespace_specifier (parser);
10125   /* Look for the `;' token.  */
10126   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10127
10128   /* Register the alias in the symbol table.  */
10129   do_namespace_alias (identifier, namespace_specifier);
10130 }
10131
10132 /* Parse a qualified-namespace-specifier.
10133
10134    qualified-namespace-specifier:
10135      :: [opt] nested-name-specifier [opt] namespace-name
10136
10137    Returns a NAMESPACE_DECL corresponding to the specified
10138    namespace.  */
10139
10140 static tree
10141 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10142 {
10143   /* Look for the optional `::'.  */
10144   cp_parser_global_scope_opt (parser,
10145                               /*current_scope_valid_p=*/false);
10146
10147   /* Look for the optional nested-name-specifier.  */
10148   cp_parser_nested_name_specifier_opt (parser,
10149                                        /*typename_keyword_p=*/false,
10150                                        /*check_dependency_p=*/true,
10151                                        /*type_p=*/false,
10152                                        /*is_declaration=*/true);
10153
10154   return cp_parser_namespace_name (parser);
10155 }
10156
10157 /* Parse a using-declaration.
10158
10159    using-declaration:
10160      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10161      using :: unqualified-id ;  */
10162
10163 static void
10164 cp_parser_using_declaration (cp_parser* parser)
10165 {
10166   cp_token *token;
10167   bool typename_p = false;
10168   bool global_scope_p;
10169   tree decl;
10170   tree identifier;
10171   tree scope;
10172   tree qscope;
10173
10174   /* Look for the `using' keyword.  */
10175   cp_parser_require_keyword (parser, RID_USING, "`using'");
10176
10177   /* Peek at the next token.  */
10178   token = cp_lexer_peek_token (parser->lexer);
10179   /* See if it's `typename'.  */
10180   if (token->keyword == RID_TYPENAME)
10181     {
10182       /* Remember that we've seen it.  */
10183       typename_p = true;
10184       /* Consume the `typename' token.  */
10185       cp_lexer_consume_token (parser->lexer);
10186     }
10187
10188   /* Look for the optional global scope qualification.  */
10189   global_scope_p
10190     = (cp_parser_global_scope_opt (parser,
10191                                    /*current_scope_valid_p=*/false)
10192        != NULL_TREE);
10193
10194   /* If we saw `typename', or didn't see `::', then there must be a
10195      nested-name-specifier present.  */
10196   if (typename_p || !global_scope_p)
10197     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10198                                               /*check_dependency_p=*/true,
10199                                               /*type_p=*/false,
10200                                               /*is_declaration=*/true);
10201   /* Otherwise, we could be in either of the two productions.  In that
10202      case, treat the nested-name-specifier as optional.  */
10203   else
10204     qscope = cp_parser_nested_name_specifier_opt (parser,
10205                                                   /*typename_keyword_p=*/false,
10206                                                   /*check_dependency_p=*/true,
10207                                                   /*type_p=*/false,
10208                                                   /*is_declaration=*/true);
10209   if (!qscope)
10210     qscope = global_namespace;
10211
10212   /* Parse the unqualified-id.  */
10213   identifier = cp_parser_unqualified_id (parser,
10214                                          /*template_keyword_p=*/false,
10215                                          /*check_dependency_p=*/true,
10216                                          /*declarator_p=*/true);
10217
10218   /* The function we call to handle a using-declaration is different
10219      depending on what scope we are in.  */
10220   if (identifier == error_mark_node)
10221     ;
10222   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10223            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10224     /* [namespace.udecl]
10225
10226        A using declaration shall not name a template-id.  */
10227     error ("a template-id may not appear in a using-declaration");
10228   else
10229     {
10230       scope = current_scope ();
10231       if (scope && TYPE_P (scope))
10232         {
10233           /* Create the USING_DECL.  */
10234           decl = do_class_using_decl (build_nt (SCOPE_REF,
10235                                                 parser->scope,
10236                                                 identifier));
10237           /* Add it to the list of members in this class.  */
10238           finish_member_declaration (decl);
10239         }
10240       else
10241         {
10242           decl = cp_parser_lookup_name_simple (parser, identifier);
10243           if (decl == error_mark_node)
10244             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10245           else if (scope)
10246             do_local_using_decl (decl, qscope, identifier);
10247           else
10248             do_toplevel_using_decl (decl, qscope, identifier);
10249         }
10250     }
10251
10252   /* Look for the final `;'.  */
10253   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10254 }
10255
10256 /* Parse a using-directive.
10257
10258    using-directive:
10259      using namespace :: [opt] nested-name-specifier [opt]
10260        namespace-name ;  */
10261
10262 static void
10263 cp_parser_using_directive (cp_parser* parser)
10264 {
10265   tree namespace_decl;
10266   tree attribs;
10267
10268   /* Look for the `using' keyword.  */
10269   cp_parser_require_keyword (parser, RID_USING, "`using'");
10270   /* And the `namespace' keyword.  */
10271   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10272   /* Look for the optional `::' operator.  */
10273   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10274   /* And the optional nested-name-specifier.  */
10275   cp_parser_nested_name_specifier_opt (parser,
10276                                        /*typename_keyword_p=*/false,
10277                                        /*check_dependency_p=*/true,
10278                                        /*type_p=*/false,
10279                                        /*is_declaration=*/true);
10280   /* Get the namespace being used.  */
10281   namespace_decl = cp_parser_namespace_name (parser);
10282   /* And any specified attributes.  */
10283   attribs = cp_parser_attributes_opt (parser);
10284   /* Update the symbol table.  */
10285   parse_using_directive (namespace_decl, attribs);
10286   /* Look for the final `;'.  */
10287   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10288 }
10289
10290 /* Parse an asm-definition.
10291
10292    asm-definition:
10293      asm ( string-literal ) ;
10294
10295    GNU Extension:
10296
10297    asm-definition:
10298      asm volatile [opt] ( string-literal ) ;
10299      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10300      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10301                           : asm-operand-list [opt] ) ;
10302      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10303                           : asm-operand-list [opt]
10304                           : asm-operand-list [opt] ) ;  */
10305
10306 static void
10307 cp_parser_asm_definition (cp_parser* parser)
10308 {
10309   cp_token *token;
10310   tree string;
10311   tree outputs = NULL_TREE;
10312   tree inputs = NULL_TREE;
10313   tree clobbers = NULL_TREE;
10314   tree asm_stmt;
10315   bool volatile_p = false;
10316   bool extended_p = false;
10317
10318   /* Look for the `asm' keyword.  */
10319   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10320   /* See if the next token is `volatile'.  */
10321   if (cp_parser_allow_gnu_extensions_p (parser)
10322       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10323     {
10324       /* Remember that we saw the `volatile' keyword.  */
10325       volatile_p = true;
10326       /* Consume the token.  */
10327       cp_lexer_consume_token (parser->lexer);
10328     }
10329   /* Look for the opening `('.  */
10330   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10331   /* Look for the string.  */
10332   c_lex_string_translate = 0;
10333   token = cp_parser_require (parser, CPP_STRING, "asm body");
10334   if (!token)
10335     goto finish;
10336   string = token->value;
10337   /* If we're allowing GNU extensions, check for the extended assembly
10338      syntax.  Unfortunately, the `:' tokens need not be separated by
10339      a space in C, and so, for compatibility, we tolerate that here
10340      too.  Doing that means that we have to treat the `::' operator as
10341      two `:' tokens.  */
10342   if (cp_parser_allow_gnu_extensions_p (parser)
10343       && at_function_scope_p ()
10344       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10345           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10346     {
10347       bool inputs_p = false;
10348       bool clobbers_p = false;
10349
10350       /* The extended syntax was used.  */
10351       extended_p = true;
10352
10353       /* Look for outputs.  */
10354       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10355         {
10356           /* Consume the `:'.  */
10357           cp_lexer_consume_token (parser->lexer);
10358           /* Parse the output-operands.  */
10359           if (cp_lexer_next_token_is_not (parser->lexer,
10360                                           CPP_COLON)
10361               && cp_lexer_next_token_is_not (parser->lexer,
10362                                              CPP_SCOPE)
10363               && cp_lexer_next_token_is_not (parser->lexer,
10364                                              CPP_CLOSE_PAREN))
10365             outputs = cp_parser_asm_operand_list (parser);
10366         }
10367       /* If the next token is `::', there are no outputs, and the
10368          next token is the beginning of the inputs.  */
10369       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10370         {
10371           /* Consume the `::' token.  */
10372           cp_lexer_consume_token (parser->lexer);
10373           /* The inputs are coming next.  */
10374           inputs_p = true;
10375         }
10376
10377       /* Look for inputs.  */
10378       if (inputs_p
10379           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10380         {
10381           if (!inputs_p)
10382             /* Consume the `:'.  */
10383             cp_lexer_consume_token (parser->lexer);
10384           /* Parse the output-operands.  */
10385           if (cp_lexer_next_token_is_not (parser->lexer,
10386                                           CPP_COLON)
10387               && cp_lexer_next_token_is_not (parser->lexer,
10388                                              CPP_SCOPE)
10389               && cp_lexer_next_token_is_not (parser->lexer,
10390                                              CPP_CLOSE_PAREN))
10391             inputs = cp_parser_asm_operand_list (parser);
10392         }
10393       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10394         /* The clobbers are coming next.  */
10395         clobbers_p = true;
10396
10397       /* Look for clobbers.  */
10398       if (clobbers_p
10399           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10400         {
10401           if (!clobbers_p)
10402             /* Consume the `:'.  */
10403             cp_lexer_consume_token (parser->lexer);
10404           /* Parse the clobbers.  */
10405           if (cp_lexer_next_token_is_not (parser->lexer,
10406                                           CPP_CLOSE_PAREN))
10407             clobbers = cp_parser_asm_clobber_list (parser);
10408         }
10409     }
10410   /* Look for the closing `)'.  */
10411   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10412     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10413                                            /*consume_paren=*/true);
10414   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10415
10416   /* Create the ASM_EXPR.  */
10417   if (at_function_scope_p ())
10418     {
10419       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10420                                   inputs, clobbers);
10421       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10422       if (!extended_p)
10423         ASM_INPUT_P (asm_stmt) = 1;
10424     }
10425   else
10426     assemble_asm (string);
10427
10428  finish:
10429   c_lex_string_translate = 1;
10430 }
10431
10432 /* Declarators [gram.dcl.decl] */
10433
10434 /* Parse an init-declarator.
10435
10436    init-declarator:
10437      declarator initializer [opt]
10438
10439    GNU Extension:
10440
10441    init-declarator:
10442      declarator asm-specification [opt] attributes [opt] initializer [opt]
10443
10444    function-definition:
10445      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10446        function-body
10447      decl-specifier-seq [opt] declarator function-try-block
10448
10449    GNU Extension:
10450
10451    function-definition:
10452      __extension__ function-definition
10453
10454    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10455    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10456    then this declarator appears in a class scope.  The new DECL created
10457    by this declarator is returned.
10458
10459    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10460    for a function-definition here as well.  If the declarator is a
10461    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10462    be TRUE upon return.  By that point, the function-definition will
10463    have been completely parsed.
10464
10465    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10466    is FALSE.  */
10467
10468 static tree
10469 cp_parser_init_declarator (cp_parser* parser,
10470                            cp_decl_specifier_seq *decl_specifiers,
10471                            bool function_definition_allowed_p,
10472                            bool member_p,
10473                            int declares_class_or_enum,
10474                            bool* function_definition_p)
10475 {
10476   cp_token *token;
10477   cp_declarator *declarator;
10478   tree prefix_attributes;
10479   tree attributes;
10480   tree asm_specification;
10481   tree initializer;
10482   tree decl = NULL_TREE;
10483   tree scope;
10484   bool is_initialized;
10485   bool is_parenthesized_init;
10486   bool is_non_constant_init;
10487   int ctor_dtor_or_conv_p;
10488   bool friend_p;
10489   bool pop_p = false;
10490
10491   /* Gather the attributes that were provided with the
10492      decl-specifiers.  */
10493   prefix_attributes = decl_specifiers->attributes;
10494   decl_specifiers->attributes = NULL_TREE;
10495
10496   /* Assume that this is not the declarator for a function
10497      definition.  */
10498   if (function_definition_p)
10499     *function_definition_p = false;
10500
10501   /* Defer access checks while parsing the declarator; we cannot know
10502      what names are accessible until we know what is being
10503      declared.  */
10504   resume_deferring_access_checks ();
10505
10506   /* Parse the declarator.  */
10507   declarator
10508     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10509                             &ctor_dtor_or_conv_p,
10510                             /*parenthesized_p=*/NULL);
10511   /* Gather up the deferred checks.  */
10512   stop_deferring_access_checks ();
10513
10514   /* If the DECLARATOR was erroneous, there's no need to go
10515      further.  */
10516   if (declarator == cp_error_declarator)
10517     return error_mark_node;
10518
10519   cp_parser_check_for_definition_in_return_type (declarator,
10520                                                  declares_class_or_enum);
10521
10522   /* Figure out what scope the entity declared by the DECLARATOR is
10523      located in.  `grokdeclarator' sometimes changes the scope, so
10524      we compute it now.  */
10525   scope = get_scope_of_declarator (declarator);
10526
10527   /* If we're allowing GNU extensions, look for an asm-specification
10528      and attributes.  */
10529   if (cp_parser_allow_gnu_extensions_p (parser))
10530     {
10531       /* Look for an asm-specification.  */
10532       asm_specification = cp_parser_asm_specification_opt (parser);
10533       /* And attributes.  */
10534       attributes = cp_parser_attributes_opt (parser);
10535     }
10536   else
10537     {
10538       asm_specification = NULL_TREE;
10539       attributes = NULL_TREE;
10540     }
10541
10542   /* Peek at the next token.  */
10543   token = cp_lexer_peek_token (parser->lexer);
10544   /* Check to see if the token indicates the start of a
10545      function-definition.  */
10546   if (cp_parser_token_starts_function_definition_p (token))
10547     {
10548       if (!function_definition_allowed_p)
10549         {
10550           /* If a function-definition should not appear here, issue an
10551              error message.  */
10552           cp_parser_error (parser,
10553                            "a function-definition is not allowed here");
10554           return error_mark_node;
10555         }
10556       else
10557         {
10558           /* Neither attributes nor an asm-specification are allowed
10559              on a function-definition.  */
10560           if (asm_specification)
10561             error ("an asm-specification is not allowed on a function-definition");
10562           if (attributes)
10563             error ("attributes are not allowed on a function-definition");
10564           /* This is a function-definition.  */
10565           *function_definition_p = true;
10566
10567           /* Parse the function definition.  */
10568           if (member_p)
10569             decl = cp_parser_save_member_function_body (parser,
10570                                                         decl_specifiers,
10571                                                         declarator,
10572                                                         prefix_attributes);
10573           else
10574             decl
10575               = (cp_parser_function_definition_from_specifiers_and_declarator
10576                  (parser, decl_specifiers, prefix_attributes, declarator));
10577
10578           return decl;
10579         }
10580     }
10581
10582   /* [dcl.dcl]
10583
10584      Only in function declarations for constructors, destructors, and
10585      type conversions can the decl-specifier-seq be omitted.
10586
10587      We explicitly postpone this check past the point where we handle
10588      function-definitions because we tolerate function-definitions
10589      that are missing their return types in some modes.  */
10590   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10591     {
10592       cp_parser_error (parser,
10593                        "expected constructor, destructor, or type conversion");
10594       return error_mark_node;
10595     }
10596
10597   /* An `=' or an `(' indicates an initializer.  */
10598   is_initialized = (token->type == CPP_EQ
10599                      || token->type == CPP_OPEN_PAREN);
10600   /* If the init-declarator isn't initialized and isn't followed by a
10601      `,' or `;', it's not a valid init-declarator.  */
10602   if (!is_initialized
10603       && token->type != CPP_COMMA
10604       && token->type != CPP_SEMICOLON)
10605     {
10606       cp_parser_error (parser, "expected init-declarator");
10607       return error_mark_node;
10608     }
10609
10610   /* Because start_decl has side-effects, we should only call it if we
10611      know we're going ahead.  By this point, we know that we cannot
10612      possibly be looking at any other construct.  */
10613   cp_parser_commit_to_tentative_parse (parser);
10614
10615   /* If the decl specifiers were bad, issue an error now that we're
10616      sure this was intended to be a declarator.  Then continue
10617      declaring the variable(s), as int, to try to cut down on further
10618      errors.  */
10619   if (decl_specifiers->any_specifiers_p
10620       && decl_specifiers->type == error_mark_node)
10621     {
10622       cp_parser_error (parser, "invalid type in declaration");
10623       decl_specifiers->type = integer_type_node;
10624     }
10625
10626   /* Check to see whether or not this declaration is a friend.  */
10627   friend_p = cp_parser_friend_p (decl_specifiers);
10628
10629   /* Check that the number of template-parameter-lists is OK.  */
10630   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10631     return error_mark_node;
10632
10633   /* Enter the newly declared entry in the symbol table.  If we're
10634      processing a declaration in a class-specifier, we wait until
10635      after processing the initializer.  */
10636   if (!member_p)
10637     {
10638       if (parser->in_unbraced_linkage_specification_p)
10639         {
10640           decl_specifiers->storage_class = sc_extern;
10641           have_extern_spec = false;
10642         }
10643       decl = start_decl (declarator, decl_specifiers,
10644                          is_initialized, attributes, prefix_attributes);
10645     }
10646
10647   /* Enter the SCOPE.  That way unqualified names appearing in the
10648      initializer will be looked up in SCOPE.  */
10649   if (scope)
10650     pop_p = push_scope (scope);
10651
10652   /* Perform deferred access control checks, now that we know in which
10653      SCOPE the declared entity resides.  */
10654   if (!member_p && decl)
10655     {
10656       tree saved_current_function_decl = NULL_TREE;
10657
10658       /* If the entity being declared is a function, pretend that we
10659          are in its scope.  If it is a `friend', it may have access to
10660          things that would not otherwise be accessible.  */
10661       if (TREE_CODE (decl) == FUNCTION_DECL)
10662         {
10663           saved_current_function_decl = current_function_decl;
10664           current_function_decl = decl;
10665         }
10666
10667       /* Perform the access control checks for the declarator and the
10668          the decl-specifiers.  */
10669       perform_deferred_access_checks ();
10670
10671       /* Restore the saved value.  */
10672       if (TREE_CODE (decl) == FUNCTION_DECL)
10673         current_function_decl = saved_current_function_decl;
10674     }
10675
10676   /* Parse the initializer.  */
10677   if (is_initialized)
10678     initializer = cp_parser_initializer (parser,
10679                                          &is_parenthesized_init,
10680                                          &is_non_constant_init);
10681   else
10682     {
10683       initializer = NULL_TREE;
10684       is_parenthesized_init = false;
10685       is_non_constant_init = true;
10686     }
10687
10688   /* The old parser allows attributes to appear after a parenthesized
10689      initializer.  Mark Mitchell proposed removing this functionality
10690      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10691      attributes -- but ignores them.  */
10692   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10693     if (cp_parser_attributes_opt (parser))
10694       warning ("attributes after parenthesized initializer ignored");
10695
10696   /* Leave the SCOPE, now that we have processed the initializer.  It
10697      is important to do this before calling cp_finish_decl because it
10698      makes decisions about whether to create DECL_EXPRs or not based
10699      on the current scope.  */
10700   if (pop_p)
10701     pop_scope (scope);
10702
10703   /* For an in-class declaration, use `grokfield' to create the
10704      declaration.  */
10705   if (member_p)
10706     {
10707       decl = grokfield (declarator, decl_specifiers,
10708                         initializer, /*asmspec=*/NULL_TREE,
10709                         /*attributes=*/NULL_TREE);
10710       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10711         cp_parser_save_default_args (parser, decl);
10712     }
10713
10714   /* Finish processing the declaration.  But, skip friend
10715      declarations.  */
10716   if (!friend_p && decl)
10717     cp_finish_decl (decl,
10718                     initializer,
10719                     asm_specification,
10720                     /* If the initializer is in parentheses, then this is
10721                        a direct-initialization, which means that an
10722                        `explicit' constructor is OK.  Otherwise, an
10723                        `explicit' constructor cannot be used.  */
10724                     ((is_parenthesized_init || !is_initialized)
10725                      ? 0 : LOOKUP_ONLYCONVERTING));
10726
10727   /* Remember whether or not variables were initialized by
10728      constant-expressions.  */
10729   if (decl && TREE_CODE (decl) == VAR_DECL
10730       && is_initialized && !is_non_constant_init)
10731     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10732
10733   return decl;
10734 }
10735
10736 /* Parse a declarator.
10737
10738    declarator:
10739      direct-declarator
10740      ptr-operator declarator
10741
10742    abstract-declarator:
10743      ptr-operator abstract-declarator [opt]
10744      direct-abstract-declarator
10745
10746    GNU Extensions:
10747
10748    declarator:
10749      attributes [opt] direct-declarator
10750      attributes [opt] ptr-operator declarator
10751
10752    abstract-declarator:
10753      attributes [opt] ptr-operator abstract-declarator [opt]
10754      attributes [opt] direct-abstract-declarator
10755
10756    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10757    detect constructor, destructor or conversion operators. It is set
10758    to -1 if the declarator is a name, and +1 if it is a
10759    function. Otherwise it is set to zero. Usually you just want to
10760    test for >0, but internally the negative value is used.
10761
10762    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10763    a decl-specifier-seq unless it declares a constructor, destructor,
10764    or conversion.  It might seem that we could check this condition in
10765    semantic analysis, rather than parsing, but that makes it difficult
10766    to handle something like `f()'.  We want to notice that there are
10767    no decl-specifiers, and therefore realize that this is an
10768    expression, not a declaration.)
10769
10770    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10771    the declarator is a direct-declarator of the form "(...)".  */
10772
10773 static cp_declarator *
10774 cp_parser_declarator (cp_parser* parser,
10775                       cp_parser_declarator_kind dcl_kind,
10776                       int* ctor_dtor_or_conv_p,
10777                       bool* parenthesized_p)
10778 {
10779   cp_token *token;
10780   cp_declarator *declarator;
10781   enum tree_code code;
10782   tree cv_qualifier_seq;
10783   tree class_type;
10784   tree attributes = NULL_TREE;
10785
10786   /* Assume this is not a constructor, destructor, or type-conversion
10787      operator.  */
10788   if (ctor_dtor_or_conv_p)
10789     *ctor_dtor_or_conv_p = 0;
10790
10791   if (cp_parser_allow_gnu_extensions_p (parser))
10792     attributes = cp_parser_attributes_opt (parser);
10793
10794   /* Peek at the next token.  */
10795   token = cp_lexer_peek_token (parser->lexer);
10796
10797   /* Check for the ptr-operator production.  */
10798   cp_parser_parse_tentatively (parser);
10799   /* Parse the ptr-operator.  */
10800   code = cp_parser_ptr_operator (parser,
10801                                  &class_type,
10802                                  &cv_qualifier_seq);
10803   /* If that worked, then we have a ptr-operator.  */
10804   if (cp_parser_parse_definitely (parser))
10805     {
10806       /* If a ptr-operator was found, then this declarator was not
10807          parenthesized.  */
10808       if (parenthesized_p)
10809         *parenthesized_p = true;
10810       /* The dependent declarator is optional if we are parsing an
10811          abstract-declarator.  */
10812       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10813         cp_parser_parse_tentatively (parser);
10814
10815       /* Parse the dependent declarator.  */
10816       declarator = cp_parser_declarator (parser, dcl_kind,
10817                                          /*ctor_dtor_or_conv_p=*/NULL,
10818                                          /*parenthesized_p=*/NULL);
10819
10820       /* If we are parsing an abstract-declarator, we must handle the
10821          case where the dependent declarator is absent.  */
10822       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10823           && !cp_parser_parse_definitely (parser))
10824         declarator = NULL;
10825
10826       /* Build the representation of the ptr-operator.  */
10827       if (class_type)
10828         declarator = make_ptrmem_declarator (cv_qualifier_seq,
10829                                              class_type,
10830                                              declarator);
10831       else if (code == INDIRECT_REF)
10832         declarator = make_pointer_declarator (cv_qualifier_seq,
10833                                               declarator);
10834       else
10835         declarator = make_reference_declarator (cv_qualifier_seq,
10836                                                 declarator);
10837     }
10838   /* Everything else is a direct-declarator.  */
10839   else
10840     {
10841       if (parenthesized_p)
10842         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10843                                                    CPP_OPEN_PAREN);
10844       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10845                                                 ctor_dtor_or_conv_p);
10846     }
10847
10848   if (attributes && declarator != cp_error_declarator)
10849     declarator->attributes = attributes;
10850
10851   return declarator;
10852 }
10853
10854 /* Parse a direct-declarator or direct-abstract-declarator.
10855
10856    direct-declarator:
10857      declarator-id
10858      direct-declarator ( parameter-declaration-clause )
10859        cv-qualifier-seq [opt]
10860        exception-specification [opt]
10861      direct-declarator [ constant-expression [opt] ]
10862      ( declarator )
10863
10864    direct-abstract-declarator:
10865      direct-abstract-declarator [opt]
10866        ( parameter-declaration-clause )
10867        cv-qualifier-seq [opt]
10868        exception-specification [opt]
10869      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10870      ( abstract-declarator )
10871
10872    Returns a representation of the declarator.  DCL_KIND is
10873    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10874    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10875    we are parsing a direct-declarator.  It is
10876    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10877    of ambiguity we prefer an abstract declarator, as per
10878    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10879    cp_parser_declarator.  */
10880
10881 static cp_declarator *
10882 cp_parser_direct_declarator (cp_parser* parser,
10883                              cp_parser_declarator_kind dcl_kind,
10884                              int* ctor_dtor_or_conv_p)
10885 {
10886   cp_token *token;
10887   cp_declarator *declarator = NULL;
10888   tree scope = NULL_TREE;
10889   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10890   bool saved_in_declarator_p = parser->in_declarator_p;
10891   bool first = true;
10892   bool pop_p = false;
10893
10894   while (true)
10895     {
10896       /* Peek at the next token.  */
10897       token = cp_lexer_peek_token (parser->lexer);
10898       if (token->type == CPP_OPEN_PAREN)
10899         {
10900           /* This is either a parameter-declaration-clause, or a
10901              parenthesized declarator. When we know we are parsing a
10902              named declarator, it must be a parenthesized declarator
10903              if FIRST is true. For instance, `(int)' is a
10904              parameter-declaration-clause, with an omitted
10905              direct-abstract-declarator. But `((*))', is a
10906              parenthesized abstract declarator. Finally, when T is a
10907              template parameter `(T)' is a
10908              parameter-declaration-clause, and not a parenthesized
10909              named declarator.
10910
10911              We first try and parse a parameter-declaration-clause,
10912              and then try a nested declarator (if FIRST is true).
10913
10914              It is not an error for it not to be a
10915              parameter-declaration-clause, even when FIRST is
10916              false. Consider,
10917
10918                int i (int);
10919                int i (3);
10920
10921              The first is the declaration of a function while the
10922              second is a the definition of a variable, including its
10923              initializer.
10924
10925              Having seen only the parenthesis, we cannot know which of
10926              these two alternatives should be selected.  Even more
10927              complex are examples like:
10928
10929                int i (int (a));
10930                int i (int (3));
10931
10932              The former is a function-declaration; the latter is a
10933              variable initialization.
10934
10935              Thus again, we try a parameter-declaration-clause, and if
10936              that fails, we back out and return.  */
10937
10938           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10939             {
10940               cp_parameter_declarator *params;
10941               unsigned saved_num_template_parameter_lists;
10942
10943               cp_parser_parse_tentatively (parser);
10944
10945               /* Consume the `('.  */
10946               cp_lexer_consume_token (parser->lexer);
10947               if (first)
10948                 {
10949                   /* If this is going to be an abstract declarator, we're
10950                      in a declarator and we can't have default args.  */
10951                   parser->default_arg_ok_p = false;
10952                   parser->in_declarator_p = true;
10953                 }
10954
10955               /* Inside the function parameter list, surrounding
10956                  template-parameter-lists do not apply.  */
10957               saved_num_template_parameter_lists
10958                 = parser->num_template_parameter_lists;
10959               parser->num_template_parameter_lists = 0;
10960
10961               /* Parse the parameter-declaration-clause.  */
10962               params = cp_parser_parameter_declaration_clause (parser);
10963
10964               parser->num_template_parameter_lists
10965                 = saved_num_template_parameter_lists;
10966
10967               /* If all went well, parse the cv-qualifier-seq and the
10968                  exception-specification.  */
10969               if (cp_parser_parse_definitely (parser))
10970                 {
10971                   tree cv_qualifiers;
10972                   tree exception_specification;
10973
10974                   if (ctor_dtor_or_conv_p)
10975                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10976                   first = false;
10977                   /* Consume the `)'.  */
10978                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10979
10980                   /* Parse the cv-qualifier-seq.  */
10981                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10982                   /* And the exception-specification.  */
10983                   exception_specification
10984                     = cp_parser_exception_specification_opt (parser);
10985
10986                   /* Create the function-declarator.  */
10987                   declarator = make_call_declarator (declarator,
10988                                                      params,
10989                                                      cv_qualifiers,
10990                                                      exception_specification);
10991                   /* Any subsequent parameter lists are to do with
10992                      return type, so are not those of the declared
10993                      function.  */
10994                   parser->default_arg_ok_p = false;
10995
10996                   /* Repeat the main loop.  */
10997                   continue;
10998                 }
10999             }
11000
11001           /* If this is the first, we can try a parenthesized
11002              declarator.  */
11003           if (first)
11004             {
11005               bool saved_in_type_id_in_expr_p;
11006
11007               parser->default_arg_ok_p = saved_default_arg_ok_p;
11008               parser->in_declarator_p = saved_in_declarator_p;
11009
11010               /* Consume the `('.  */
11011               cp_lexer_consume_token (parser->lexer);
11012               /* Parse the nested declarator.  */
11013               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11014               parser->in_type_id_in_expr_p = true;
11015               declarator
11016                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11017                                         /*parenthesized_p=*/NULL);
11018               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11019               first = false;
11020               /* Expect a `)'.  */
11021               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11022                 declarator = cp_error_declarator;
11023               if (declarator == cp_error_declarator)
11024                 break;
11025
11026               goto handle_declarator;
11027             }
11028           /* Otherwise, we must be done.  */
11029           else
11030             break;
11031         }
11032       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11033                && token->type == CPP_OPEN_SQUARE)
11034         {
11035           /* Parse an array-declarator.  */
11036           tree bounds;
11037
11038           if (ctor_dtor_or_conv_p)
11039             *ctor_dtor_or_conv_p = 0;
11040
11041           first = false;
11042           parser->default_arg_ok_p = false;
11043           parser->in_declarator_p = true;
11044           /* Consume the `['.  */
11045           cp_lexer_consume_token (parser->lexer);
11046           /* Peek at the next token.  */
11047           token = cp_lexer_peek_token (parser->lexer);
11048           /* If the next token is `]', then there is no
11049              constant-expression.  */
11050           if (token->type != CPP_CLOSE_SQUARE)
11051             {
11052               bool non_constant_p;
11053
11054               bounds
11055                 = cp_parser_constant_expression (parser,
11056                                                  /*allow_non_constant=*/true,
11057                                                  &non_constant_p);
11058               if (!non_constant_p)
11059                 bounds = fold_non_dependent_expr (bounds);
11060             }
11061           else
11062             bounds = NULL_TREE;
11063           /* Look for the closing `]'.  */
11064           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11065             {
11066               declarator = cp_error_declarator;
11067               break;
11068             }
11069
11070           declarator = make_array_declarator (declarator, bounds);
11071         }
11072       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11073         {
11074           tree id;
11075
11076           /* Parse a declarator-id */
11077           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11078             cp_parser_parse_tentatively (parser);
11079           id = cp_parser_declarator_id (parser);
11080           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11081             {
11082               if (!cp_parser_parse_definitely (parser))
11083                 id = error_mark_node;
11084               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11085                 {
11086                   cp_parser_error (parser, "expected unqualified-id");
11087                   id = error_mark_node;
11088                 }
11089             }
11090
11091           if (id == error_mark_node)
11092             {
11093               declarator = cp_error_declarator;
11094               break;
11095             }
11096
11097           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11098             {
11099               tree scope = TREE_OPERAND (id, 0);
11100
11101               /* In the declaration of a member of a template class
11102                  outside of the class itself, the SCOPE will sometimes
11103                  be a TYPENAME_TYPE.  For example, given:
11104
11105                  template <typename T>
11106                  int S<T>::R::i = 3;
11107
11108                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11109                  this context, we must resolve S<T>::R to an ordinary
11110                  type, rather than a typename type.
11111
11112                  The reason we normally avoid resolving TYPENAME_TYPEs
11113                  is that a specialization of `S' might render
11114                  `S<T>::R' not a type.  However, if `S' is
11115                  specialized, then this `i' will not be used, so there
11116                  is no harm in resolving the types here.  */
11117               if (TREE_CODE (scope) == TYPENAME_TYPE)
11118                 {
11119                   tree type;
11120
11121                   /* Resolve the TYPENAME_TYPE.  */
11122                   type = resolve_typename_type (scope,
11123                                                  /*only_current_p=*/false);
11124                   /* If that failed, the declarator is invalid.  */
11125                   if (type == error_mark_node)
11126                     error ("`%T::%D' is not a type",
11127                            TYPE_CONTEXT (scope),
11128                            TYPE_IDENTIFIER (scope));
11129                   /* Build a new DECLARATOR.  */
11130                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11131                 }
11132             }
11133
11134           declarator = make_id_declarator (id);
11135           if (id)
11136             {
11137               tree class_type;
11138               tree unqualified_name;
11139
11140               if (TREE_CODE (id) == SCOPE_REF
11141                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11142                 {
11143                   class_type = TREE_OPERAND (id, 0);
11144                   unqualified_name = TREE_OPERAND (id, 1);
11145                 }
11146               else
11147                 {
11148                   class_type = current_class_type;
11149                   unqualified_name = id;
11150                 }
11151
11152               if (class_type)
11153                 {
11154                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11155                     declarator->u.id.sfk = sfk_destructor;
11156                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11157                     declarator->u.id.sfk = sfk_conversion;
11158                   else if (constructor_name_p (unqualified_name,
11159                                                class_type)
11160                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11161                                && same_type_p (TREE_TYPE (unqualified_name),
11162                                                class_type)))
11163                     declarator->u.id.sfk = sfk_constructor;
11164
11165                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11166                     *ctor_dtor_or_conv_p = -1;
11167                   if (TREE_CODE (id) == SCOPE_REF
11168                       && TREE_CODE (unqualified_name) == TYPE_DECL 
11169                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11170                     {
11171                       error ("invalid use of constructor as a template");
11172                       inform ("use `%T::%D' instead of `%T::%T' to name the "
11173                               "constructor in a qualified name", class_type, 
11174                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11175                               class_type, class_type);
11176                     }
11177                 }
11178             }
11179
11180         handle_declarator:;
11181           scope = get_scope_of_declarator (declarator);
11182           if (scope)
11183             /* Any names that appear after the declarator-id for a
11184                member are looked up in the containing scope.  */
11185             pop_p = push_scope (scope);
11186           parser->in_declarator_p = true;
11187           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11188               || (declarator && declarator->kind == cdk_id))
11189             /* Default args are only allowed on function
11190                declarations.  */
11191             parser->default_arg_ok_p = saved_default_arg_ok_p;
11192           else
11193             parser->default_arg_ok_p = false;
11194
11195           first = false;
11196         }
11197       /* We're done.  */
11198       else
11199         break;
11200     }
11201
11202   /* For an abstract declarator, we might wind up with nothing at this
11203      point.  That's an error; the declarator is not optional.  */
11204   if (!declarator)
11205     cp_parser_error (parser, "expected declarator");
11206
11207   /* If we entered a scope, we must exit it now.  */
11208   if (pop_p)
11209     pop_scope (scope);
11210
11211   parser->default_arg_ok_p = saved_default_arg_ok_p;
11212   parser->in_declarator_p = saved_in_declarator_p;
11213
11214   return declarator;
11215 }
11216
11217 /* Parse a ptr-operator.
11218
11219    ptr-operator:
11220      * cv-qualifier-seq [opt]
11221      &
11222      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11223
11224    GNU Extension:
11225
11226    ptr-operator:
11227      & cv-qualifier-seq [opt]
11228
11229    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
11230    used.  Returns ADDR_EXPR if a reference was used.  In the
11231    case of a pointer-to-member, *TYPE is filled in with the
11232    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
11233    with the cv-qualifier-seq, or NULL_TREE, if there are no
11234    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
11235
11236 static enum tree_code
11237 cp_parser_ptr_operator (cp_parser* parser,
11238                         tree* type,
11239                         tree* cv_qualifier_seq)
11240 {
11241   enum tree_code code = ERROR_MARK;
11242   cp_token *token;
11243
11244   /* Assume that it's not a pointer-to-member.  */
11245   *type = NULL_TREE;
11246   /* And that there are no cv-qualifiers.  */
11247   *cv_qualifier_seq = NULL_TREE;
11248
11249   /* Peek at the next token.  */
11250   token = cp_lexer_peek_token (parser->lexer);
11251   /* If it's a `*' or `&' we have a pointer or reference.  */
11252   if (token->type == CPP_MULT || token->type == CPP_AND)
11253     {
11254       /* Remember which ptr-operator we were processing.  */
11255       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11256
11257       /* Consume the `*' or `&'.  */
11258       cp_lexer_consume_token (parser->lexer);
11259
11260       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11261          `&', if we are allowing GNU extensions.  (The only qualifier
11262          that can legally appear after `&' is `restrict', but that is
11263          enforced during semantic analysis.  */
11264       if (code == INDIRECT_REF
11265           || cp_parser_allow_gnu_extensions_p (parser))
11266         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
11267     }
11268   else
11269     {
11270       /* Try the pointer-to-member case.  */
11271       cp_parser_parse_tentatively (parser);
11272       /* Look for the optional `::' operator.  */
11273       cp_parser_global_scope_opt (parser,
11274                                   /*current_scope_valid_p=*/false);
11275       /* Look for the nested-name specifier.  */
11276       cp_parser_nested_name_specifier (parser,
11277                                        /*typename_keyword_p=*/false,
11278                                        /*check_dependency_p=*/true,
11279                                        /*type_p=*/false,
11280                                        /*is_declaration=*/false);
11281       /* If we found it, and the next token is a `*', then we are
11282          indeed looking at a pointer-to-member operator.  */
11283       if (!cp_parser_error_occurred (parser)
11284           && cp_parser_require (parser, CPP_MULT, "`*'"))
11285         {
11286           /* The type of which the member is a member is given by the
11287              current SCOPE.  */
11288           *type = parser->scope;
11289           /* The next name will not be qualified.  */
11290           parser->scope = NULL_TREE;
11291           parser->qualifying_scope = NULL_TREE;
11292           parser->object_scope = NULL_TREE;
11293           /* Indicate that the `*' operator was used.  */
11294           code = INDIRECT_REF;
11295           /* Look for the optional cv-qualifier-seq.  */
11296           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
11297         }
11298       /* If that didn't work we don't have a ptr-operator.  */
11299       if (!cp_parser_parse_definitely (parser))
11300         cp_parser_error (parser, "expected ptr-operator");
11301     }
11302
11303   return code;
11304 }
11305
11306 /* Parse an (optional) cv-qualifier-seq.
11307
11308    cv-qualifier-seq:
11309      cv-qualifier cv-qualifier-seq [opt]
11310
11311    Returns a TREE_LIST.  The TREE_VALUE of each node is the
11312    representation of a cv-qualifier.  */
11313
11314 static tree
11315 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11316 {
11317   tree cv_qualifiers = NULL_TREE;
11318
11319   while (true)
11320     {
11321       tree cv_qualifier;
11322
11323       /* Look for the next cv-qualifier.  */
11324       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
11325       /* If we didn't find one, we're done.  */
11326       if (!cv_qualifier)
11327         break;
11328
11329       /* Add this cv-qualifier to the list.  */
11330       cv_qualifiers
11331         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
11332     }
11333
11334   /* We built up the list in reverse order.  */
11335   return nreverse (cv_qualifiers);
11336 }
11337
11338 /* Parse an (optional) cv-qualifier.
11339
11340    cv-qualifier:
11341      const
11342      volatile
11343
11344    GNU Extension:
11345
11346    cv-qualifier:
11347      __restrict__ */
11348
11349 static tree
11350 cp_parser_cv_qualifier_opt (cp_parser* parser)
11351 {
11352   cp_token *token;
11353   tree cv_qualifier = NULL_TREE;
11354
11355   /* Peek at the next token.  */
11356   token = cp_lexer_peek_token (parser->lexer);
11357   /* See if it's a cv-qualifier.  */
11358   switch (token->keyword)
11359     {
11360     case RID_CONST:
11361     case RID_VOLATILE:
11362     case RID_RESTRICT:
11363       /* Save the value of the token.  */
11364       cv_qualifier = token->value;
11365       /* Consume the token.  */
11366       cp_lexer_consume_token (parser->lexer);
11367       break;
11368
11369     default:
11370       break;
11371     }
11372
11373   return cv_qualifier;
11374 }
11375
11376 /* Parse a declarator-id.
11377
11378    declarator-id:
11379      id-expression
11380      :: [opt] nested-name-specifier [opt] type-name
11381
11382    In the `id-expression' case, the value returned is as for
11383    cp_parser_id_expression if the id-expression was an unqualified-id.
11384    If the id-expression was a qualified-id, then a SCOPE_REF is
11385    returned.  The first operand is the scope (either a NAMESPACE_DECL
11386    or TREE_TYPE), but the second is still just a representation of an
11387    unqualified-id.  */
11388
11389 static tree
11390 cp_parser_declarator_id (cp_parser* parser)
11391 {
11392   tree id_expression;
11393
11394   /* The expression must be an id-expression.  Assume that qualified
11395      names are the names of types so that:
11396
11397        template <class T>
11398        int S<T>::R::i = 3;
11399
11400      will work; we must treat `S<T>::R' as the name of a type.
11401      Similarly, assume that qualified names are templates, where
11402      required, so that:
11403
11404        template <class T>
11405        int S<T>::R<T>::i = 3;
11406
11407      will work, too.  */
11408   id_expression = cp_parser_id_expression (parser,
11409                                            /*template_keyword_p=*/false,
11410                                            /*check_dependency_p=*/false,
11411                                            /*template_p=*/NULL,
11412                                            /*declarator_p=*/true);
11413   /* If the name was qualified, create a SCOPE_REF to represent
11414      that.  */
11415   if (parser->scope)
11416     {
11417       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11418       parser->scope = NULL_TREE;
11419     }
11420
11421   return id_expression;
11422 }
11423
11424 /* Parse a type-id.
11425
11426    type-id:
11427      type-specifier-seq abstract-declarator [opt]
11428
11429    Returns the TYPE specified.  */
11430
11431 static tree
11432 cp_parser_type_id (cp_parser* parser)
11433 {
11434   cp_decl_specifier_seq type_specifier_seq;
11435   cp_declarator *abstract_declarator;
11436
11437   /* Parse the type-specifier-seq.  */
11438   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11439   if (type_specifier_seq.type == error_mark_node)
11440     return error_mark_node;
11441
11442   /* There might or might not be an abstract declarator.  */
11443   cp_parser_parse_tentatively (parser);
11444   /* Look for the declarator.  */
11445   abstract_declarator
11446     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11447                             /*parenthesized_p=*/NULL);
11448   /* Check to see if there really was a declarator.  */
11449   if (!cp_parser_parse_definitely (parser))
11450     abstract_declarator = NULL;
11451
11452   return groktypename (&type_specifier_seq, abstract_declarator);
11453 }
11454
11455 /* Parse a type-specifier-seq.
11456
11457    type-specifier-seq:
11458      type-specifier type-specifier-seq [opt]
11459
11460    GNU extension:
11461
11462    type-specifier-seq:
11463      attributes type-specifier-seq [opt]
11464
11465    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11466
11467 static void
11468 cp_parser_type_specifier_seq (cp_parser* parser,
11469                               cp_decl_specifier_seq *type_specifier_seq)
11470 {
11471   bool seen_type_specifier = false;
11472
11473   /* Clear the TYPE_SPECIFIER_SEQ.  */
11474   clear_decl_specs (type_specifier_seq);
11475
11476   /* Parse the type-specifiers and attributes.  */
11477   while (true)
11478     {
11479       tree type_specifier;
11480
11481       /* Check for attributes first.  */
11482       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11483         {
11484           type_specifier_seq->attributes = 
11485             chainon (type_specifier_seq->attributes,
11486                      cp_parser_attributes_opt (parser));
11487           continue;
11488         }
11489
11490       /* Look for the type-specifier.  */
11491       type_specifier = cp_parser_type_specifier (parser,
11492                                                  CP_PARSER_FLAGS_OPTIONAL,
11493                                                  type_specifier_seq,
11494                                                  /*is_declaration=*/false,
11495                                                  NULL,
11496                                                  NULL);
11497       /* If the first type-specifier could not be found, this is not a
11498          type-specifier-seq at all.  */
11499       if (!seen_type_specifier && !type_specifier)
11500         {
11501           cp_parser_error (parser, "expected type-specifier");
11502           type_specifier_seq->type = error_mark_node;
11503           return;
11504         }
11505       /* If subsequent type-specifiers could not be found, the
11506          type-specifier-seq is complete.  */
11507       else if (seen_type_specifier && !type_specifier)
11508         break;
11509
11510       seen_type_specifier = true;
11511     }
11512
11513   return;
11514 }
11515
11516 /* Parse a parameter-declaration-clause.
11517
11518    parameter-declaration-clause:
11519      parameter-declaration-list [opt] ... [opt]
11520      parameter-declaration-list , ...
11521
11522    Returns a representation for the parameter declarations.  A return
11523    value of NULL indicates a parameter-declaration-clause consisting
11524    only of an ellipsis.  */
11525
11526 static cp_parameter_declarator *
11527 cp_parser_parameter_declaration_clause (cp_parser* parser)
11528 {
11529   cp_parameter_declarator *parameters;
11530   cp_token *token;
11531   bool ellipsis_p;
11532   bool is_error;
11533
11534   /* Peek at the next token.  */
11535   token = cp_lexer_peek_token (parser->lexer);
11536   /* Check for trivial parameter-declaration-clauses.  */
11537   if (token->type == CPP_ELLIPSIS)
11538     {
11539       /* Consume the `...' token.  */
11540       cp_lexer_consume_token (parser->lexer);
11541       return NULL;
11542     }
11543   else if (token->type == CPP_CLOSE_PAREN)
11544     /* There are no parameters.  */
11545     {
11546 #ifndef NO_IMPLICIT_EXTERN_C
11547       if (in_system_header && current_class_type == NULL
11548           && current_lang_name == lang_name_c)
11549         return NULL;
11550       else
11551 #endif
11552         return no_parameters;
11553     }
11554   /* Check for `(void)', too, which is a special case.  */
11555   else if (token->keyword == RID_VOID
11556            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11557                == CPP_CLOSE_PAREN))
11558     {
11559       /* Consume the `void' token.  */
11560       cp_lexer_consume_token (parser->lexer);
11561       /* There are no parameters.  */
11562       return no_parameters;
11563     }
11564
11565   /* Parse the parameter-declaration-list.  */
11566   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11567   /* If a parse error occurred while parsing the
11568      parameter-declaration-list, then the entire
11569      parameter-declaration-clause is erroneous.  */
11570   if (is_error)
11571     return NULL;
11572
11573   /* Peek at the next token.  */
11574   token = cp_lexer_peek_token (parser->lexer);
11575   /* If it's a `,', the clause should terminate with an ellipsis.  */
11576   if (token->type == CPP_COMMA)
11577     {
11578       /* Consume the `,'.  */
11579       cp_lexer_consume_token (parser->lexer);
11580       /* Expect an ellipsis.  */
11581       ellipsis_p
11582         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11583     }
11584   /* It might also be `...' if the optional trailing `,' was
11585      omitted.  */
11586   else if (token->type == CPP_ELLIPSIS)
11587     {
11588       /* Consume the `...' token.  */
11589       cp_lexer_consume_token (parser->lexer);
11590       /* And remember that we saw it.  */
11591       ellipsis_p = true;
11592     }
11593   else
11594     ellipsis_p = false;
11595
11596   /* Finish the parameter list.  */
11597   if (parameters && ellipsis_p)
11598     parameters->ellipsis_p = true;
11599   
11600   return parameters;
11601 }
11602
11603 /* Parse a parameter-declaration-list.
11604
11605    parameter-declaration-list:
11606      parameter-declaration
11607      parameter-declaration-list , parameter-declaration
11608
11609    Returns a representation of the parameter-declaration-list, as for
11610    cp_parser_parameter_declaration_clause.  However, the
11611    `void_list_node' is never appended to the list.  Upon return,
11612    *IS_ERROR will be true iff an error occurred.  */
11613
11614 static cp_parameter_declarator *
11615 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11616 {
11617   cp_parameter_declarator *parameters = NULL;
11618   cp_parameter_declarator **tail = &parameters;
11619
11620   /* Assume all will go well.  */
11621   *is_error = false;
11622
11623   /* Look for more parameters.  */
11624   while (true)
11625     {
11626       cp_parameter_declarator *parameter;
11627       bool parenthesized_p;
11628       /* Parse the parameter.  */
11629       parameter
11630         = cp_parser_parameter_declaration (parser,
11631                                            /*template_parm_p=*/false,
11632                                            &parenthesized_p);
11633
11634       /* If a parse error occurred parsing the parameter declaration,
11635          then the entire parameter-declaration-list is erroneous.  */
11636       if (!parameter)
11637         {
11638           *is_error = true;
11639           parameters = NULL;
11640           break;
11641         }
11642       /* Add the new parameter to the list.  */
11643       *tail = parameter;
11644       tail = &parameter->next;
11645
11646       /* Peek at the next token.  */
11647       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11648           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11649         /* The parameter-declaration-list is complete.  */
11650         break;
11651       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11652         {
11653           cp_token *token;
11654
11655           /* Peek at the next token.  */
11656           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11657           /* If it's an ellipsis, then the list is complete.  */
11658           if (token->type == CPP_ELLIPSIS)
11659             break;
11660           /* Otherwise, there must be more parameters.  Consume the
11661              `,'.  */
11662           cp_lexer_consume_token (parser->lexer);
11663           /* When parsing something like:
11664
11665                 int i(float f, double d)
11666
11667              we can tell after seeing the declaration for "f" that we
11668              are not looking at an initialization of a variable "i",
11669              but rather at the declaration of a function "i".
11670
11671              Due to the fact that the parsing of template arguments
11672              (as specified to a template-id) requires backtracking we
11673              cannot use this technique when inside a template argument
11674              list.  */
11675           if (!parser->in_template_argument_list_p
11676               && !parser->in_type_id_in_expr_p
11677               && cp_parser_parsing_tentatively (parser)
11678               && !cp_parser_committed_to_tentative_parse (parser)
11679               /* However, a parameter-declaration of the form
11680                  "foat(f)" (which is a valid declaration of a
11681                  parameter "f") can also be interpreted as an
11682                  expression (the conversion of "f" to "float").  */
11683               && !parenthesized_p)
11684             cp_parser_commit_to_tentative_parse (parser);
11685         }
11686       else
11687         {
11688           cp_parser_error (parser, "expected `,' or `...'");
11689           if (!cp_parser_parsing_tentatively (parser)
11690               || cp_parser_committed_to_tentative_parse (parser))
11691             cp_parser_skip_to_closing_parenthesis (parser,
11692                                                    /*recovering=*/true,
11693                                                    /*or_comma=*/false,
11694                                                    /*consume_paren=*/false);
11695           break;
11696         }
11697     }
11698
11699   return parameters;
11700 }
11701
11702 /* Parse a parameter declaration.
11703
11704    parameter-declaration:
11705      decl-specifier-seq declarator
11706      decl-specifier-seq declarator = assignment-expression
11707      decl-specifier-seq abstract-declarator [opt]
11708      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11709
11710    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11711    declares a template parameter.  (In that case, a non-nested `>'
11712    token encountered during the parsing of the assignment-expression
11713    is not interpreted as a greater-than operator.)
11714
11715    Returns a representation of the parameter, or NULL if an error
11716    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11717    true iff the declarator is of the form "(p)".  */
11718
11719 static cp_parameter_declarator *
11720 cp_parser_parameter_declaration (cp_parser *parser,
11721                                  bool template_parm_p,
11722                                  bool *parenthesized_p)
11723 {
11724   int declares_class_or_enum;
11725   bool greater_than_is_operator_p;
11726   cp_decl_specifier_seq decl_specifiers;
11727   cp_declarator *declarator;
11728   tree default_argument;
11729   cp_token *token;
11730   const char *saved_message;
11731
11732   /* In a template parameter, `>' is not an operator.
11733
11734      [temp.param]
11735
11736      When parsing a default template-argument for a non-type
11737      template-parameter, the first non-nested `>' is taken as the end
11738      of the template parameter-list rather than a greater-than
11739      operator.  */
11740   greater_than_is_operator_p = !template_parm_p;
11741
11742   /* Type definitions may not appear in parameter types.  */
11743   saved_message = parser->type_definition_forbidden_message;
11744   parser->type_definition_forbidden_message
11745     = "types may not be defined in parameter types";
11746
11747   /* Parse the declaration-specifiers.  */
11748   cp_parser_decl_specifier_seq (parser,
11749                                 CP_PARSER_FLAGS_NONE,
11750                                 &decl_specifiers,
11751                                 &declares_class_or_enum);
11752   /* If an error occurred, there's no reason to attempt to parse the
11753      rest of the declaration.  */
11754   if (cp_parser_error_occurred (parser))
11755     {
11756       parser->type_definition_forbidden_message = saved_message;
11757       return NULL;
11758     }
11759
11760   /* Peek at the next token.  */
11761   token = cp_lexer_peek_token (parser->lexer);
11762   /* If the next token is a `)', `,', `=', `>', or `...', then there
11763      is no declarator.  */
11764   if (token->type == CPP_CLOSE_PAREN
11765       || token->type == CPP_COMMA
11766       || token->type == CPP_EQ
11767       || token->type == CPP_ELLIPSIS
11768       || token->type == CPP_GREATER)
11769     {
11770       declarator = NULL;
11771       if (parenthesized_p)
11772         *parenthesized_p = false;
11773     }
11774   /* Otherwise, there should be a declarator.  */
11775   else
11776     {
11777       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11778       parser->default_arg_ok_p = false;
11779
11780       /* After seeing a decl-specifier-seq, if the next token is not a
11781          "(", there is no possibility that the code is a valid
11782          expression.  Therefore, if parsing tentatively, we commit at
11783          this point.  */
11784       if (!parser->in_template_argument_list_p
11785           /* In an expression context, having seen:
11786
11787                (int((char ...
11788
11789              we cannot be sure whether we are looking at a
11790              function-type (taking a "char" as a parameter) or a cast
11791              of some object of type "char" to "int".  */
11792           && !parser->in_type_id_in_expr_p
11793           && cp_parser_parsing_tentatively (parser)
11794           && !cp_parser_committed_to_tentative_parse (parser)
11795           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11796         cp_parser_commit_to_tentative_parse (parser);
11797       /* Parse the declarator.  */
11798       declarator = cp_parser_declarator (parser,
11799                                          CP_PARSER_DECLARATOR_EITHER,
11800                                          /*ctor_dtor_or_conv_p=*/NULL,
11801                                          parenthesized_p);
11802       parser->default_arg_ok_p = saved_default_arg_ok_p;
11803       /* After the declarator, allow more attributes.  */
11804       decl_specifiers.attributes
11805         = chainon (decl_specifiers.attributes, 
11806                    cp_parser_attributes_opt (parser));
11807     }
11808
11809   /* The restriction on defining new types applies only to the type
11810      of the parameter, not to the default argument.  */
11811   parser->type_definition_forbidden_message = saved_message;
11812
11813   /* If the next token is `=', then process a default argument.  */
11814   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11815     {
11816       bool saved_greater_than_is_operator_p;
11817       /* Consume the `='.  */
11818       cp_lexer_consume_token (parser->lexer);
11819
11820       /* If we are defining a class, then the tokens that make up the
11821          default argument must be saved and processed later.  */
11822       if (!template_parm_p && at_class_scope_p ()
11823           && TYPE_BEING_DEFINED (current_class_type))
11824         {
11825           unsigned depth = 0;
11826
11827           /* Create a DEFAULT_ARG to represented the unparsed default
11828              argument.  */
11829           default_argument = make_node (DEFAULT_ARG);
11830           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11831
11832           /* Add tokens until we have processed the entire default
11833              argument.  */
11834           while (true)
11835             {
11836               bool done = false;
11837               cp_token *token;
11838
11839               /* Peek at the next token.  */
11840               token = cp_lexer_peek_token (parser->lexer);
11841               /* What we do depends on what token we have.  */
11842               switch (token->type)
11843                 {
11844                   /* In valid code, a default argument must be
11845                      immediately followed by a `,' `)', or `...'.  */
11846                 case CPP_COMMA:
11847                 case CPP_CLOSE_PAREN:
11848                 case CPP_ELLIPSIS:
11849                   /* If we run into a non-nested `;', `}', or `]',
11850                      then the code is invalid -- but the default
11851                      argument is certainly over.  */
11852                 case CPP_SEMICOLON:
11853                 case CPP_CLOSE_BRACE:
11854                 case CPP_CLOSE_SQUARE:
11855                   if (depth == 0)
11856                     done = true;
11857                   /* Update DEPTH, if necessary.  */
11858                   else if (token->type == CPP_CLOSE_PAREN
11859                            || token->type == CPP_CLOSE_BRACE
11860                            || token->type == CPP_CLOSE_SQUARE)
11861                     --depth;
11862                   break;
11863
11864                 case CPP_OPEN_PAREN:
11865                 case CPP_OPEN_SQUARE:
11866                 case CPP_OPEN_BRACE:
11867                   ++depth;
11868                   break;
11869
11870                 case CPP_GREATER:
11871                   /* If we see a non-nested `>', and `>' is not an
11872                      operator, then it marks the end of the default
11873                      argument.  */
11874                   if (!depth && !greater_than_is_operator_p)
11875                     done = true;
11876                   break;
11877
11878                   /* If we run out of tokens, issue an error message.  */
11879                 case CPP_EOF:
11880                   error ("file ends in default argument");
11881                   done = true;
11882                   break;
11883
11884                 case CPP_NAME:
11885                 case CPP_SCOPE:
11886                   /* In these cases, we should look for template-ids.
11887                      For example, if the default argument is
11888                      `X<int, double>()', we need to do name lookup to
11889                      figure out whether or not `X' is a template; if
11890                      so, the `,' does not end the default argument.
11891
11892                      That is not yet done.  */
11893                   break;
11894
11895                 default:
11896                   break;
11897                 }
11898
11899               /* If we've reached the end, stop.  */
11900               if (done)
11901                 break;
11902
11903               /* Add the token to the token block.  */
11904               token = cp_lexer_consume_token (parser->lexer);
11905               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11906                                          token);
11907             }
11908         }
11909       /* Outside of a class definition, we can just parse the
11910          assignment-expression.  */
11911       else
11912         {
11913           bool saved_local_variables_forbidden_p;
11914
11915           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11916              set correctly.  */
11917           saved_greater_than_is_operator_p
11918             = parser->greater_than_is_operator_p;
11919           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11920           /* Local variable names (and the `this' keyword) may not
11921              appear in a default argument.  */
11922           saved_local_variables_forbidden_p
11923             = parser->local_variables_forbidden_p;
11924           parser->local_variables_forbidden_p = true;
11925           /* Parse the assignment-expression.  */
11926           default_argument = cp_parser_assignment_expression (parser);
11927           /* Restore saved state.  */
11928           parser->greater_than_is_operator_p
11929             = saved_greater_than_is_operator_p;
11930           parser->local_variables_forbidden_p
11931             = saved_local_variables_forbidden_p;
11932         }
11933       if (!parser->default_arg_ok_p)
11934         {
11935           if (!flag_pedantic_errors)
11936             warning ("deprecated use of default argument for parameter of non-function");
11937           else
11938             {
11939               error ("default arguments are only permitted for function parameters");
11940               default_argument = NULL_TREE;
11941             }
11942         }
11943     }
11944   else
11945     default_argument = NULL_TREE;
11946
11947   return make_parameter_declarator (&decl_specifiers,
11948                                     declarator,
11949                                     default_argument);
11950 }
11951
11952 /* Parse a function-body.
11953
11954    function-body:
11955      compound_statement  */
11956
11957 static void
11958 cp_parser_function_body (cp_parser *parser)
11959 {
11960   cp_parser_compound_statement (parser, NULL, false);
11961 }
11962
11963 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11964    true if a ctor-initializer was present.  */
11965
11966 static bool
11967 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11968 {
11969   tree body;
11970   bool ctor_initializer_p;
11971
11972   /* Begin the function body.  */
11973   body = begin_function_body ();
11974   /* Parse the optional ctor-initializer.  */
11975   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11976   /* Parse the function-body.  */
11977   cp_parser_function_body (parser);
11978   /* Finish the function body.  */
11979   finish_function_body (body);
11980
11981   return ctor_initializer_p;
11982 }
11983
11984 /* Parse an initializer.
11985
11986    initializer:
11987      = initializer-clause
11988      ( expression-list )
11989
11990    Returns a expression representing the initializer.  If no
11991    initializer is present, NULL_TREE is returned.
11992
11993    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11994    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11995    set to FALSE if there is no initializer present.  If there is an
11996    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11997    is set to true; otherwise it is set to false.  */
11998
11999 static tree
12000 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12001                        bool* non_constant_p)
12002 {
12003   cp_token *token;
12004   tree init;
12005
12006   /* Peek at the next token.  */
12007   token = cp_lexer_peek_token (parser->lexer);
12008
12009   /* Let our caller know whether or not this initializer was
12010      parenthesized.  */
12011   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12012   /* Assume that the initializer is constant.  */
12013   *non_constant_p = false;
12014
12015   if (token->type == CPP_EQ)
12016     {
12017       /* Consume the `='.  */
12018       cp_lexer_consume_token (parser->lexer);
12019       /* Parse the initializer-clause.  */
12020       init = cp_parser_initializer_clause (parser, non_constant_p);
12021     }
12022   else if (token->type == CPP_OPEN_PAREN)
12023     init = cp_parser_parenthesized_expression_list (parser, false,
12024                                                     non_constant_p);
12025   else
12026     {
12027       /* Anything else is an error.  */
12028       cp_parser_error (parser, "expected initializer");
12029       init = error_mark_node;
12030     }
12031
12032   return init;
12033 }
12034
12035 /* Parse an initializer-clause.
12036
12037    initializer-clause:
12038      assignment-expression
12039      { initializer-list , [opt] }
12040      { }
12041
12042    Returns an expression representing the initializer.
12043
12044    If the `assignment-expression' production is used the value
12045    returned is simply a representation for the expression.
12046
12047    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12048    the elements of the initializer-list (or NULL_TREE, if the last
12049    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12050    NULL_TREE.  There is no way to detect whether or not the optional
12051    trailing `,' was provided.  NON_CONSTANT_P is as for
12052    cp_parser_initializer.  */
12053
12054 static tree
12055 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12056 {
12057   tree initializer;
12058
12059   /* If it is not a `{', then we are looking at an
12060      assignment-expression.  */
12061   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12062     {
12063       initializer 
12064         = cp_parser_constant_expression (parser,
12065                                         /*allow_non_constant_p=*/true,
12066                                         non_constant_p);
12067       if (!*non_constant_p)
12068         initializer = fold_non_dependent_expr (initializer);
12069     }
12070   else
12071     {
12072       /* Consume the `{' token.  */
12073       cp_lexer_consume_token (parser->lexer);
12074       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12075       initializer = make_node (CONSTRUCTOR);
12076       /* If it's not a `}', then there is a non-trivial initializer.  */
12077       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12078         {
12079           /* Parse the initializer list.  */
12080           CONSTRUCTOR_ELTS (initializer)
12081             = cp_parser_initializer_list (parser, non_constant_p);
12082           /* A trailing `,' token is allowed.  */
12083           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12084             cp_lexer_consume_token (parser->lexer);
12085         }
12086       /* Now, there should be a trailing `}'.  */
12087       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12088     }
12089
12090   return initializer;
12091 }
12092
12093 /* Parse an initializer-list.
12094
12095    initializer-list:
12096      initializer-clause
12097      initializer-list , initializer-clause
12098
12099    GNU Extension:
12100
12101    initializer-list:
12102      identifier : initializer-clause
12103      initializer-list, identifier : initializer-clause
12104
12105    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12106    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12107    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12108    as for cp_parser_initializer.  */
12109
12110 static tree
12111 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12112 {
12113   tree initializers = NULL_TREE;
12114
12115   /* Assume all of the expressions are constant.  */
12116   *non_constant_p = false;
12117
12118   /* Parse the rest of the list.  */
12119   while (true)
12120     {
12121       cp_token *token;
12122       tree identifier;
12123       tree initializer;
12124       bool clause_non_constant_p;
12125
12126       /* If the next token is an identifier and the following one is a
12127          colon, we are looking at the GNU designated-initializer
12128          syntax.  */
12129       if (cp_parser_allow_gnu_extensions_p (parser)
12130           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12131           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12132         {
12133           /* Consume the identifier.  */
12134           identifier = cp_lexer_consume_token (parser->lexer)->value;
12135           /* Consume the `:'.  */
12136           cp_lexer_consume_token (parser->lexer);
12137         }
12138       else
12139         identifier = NULL_TREE;
12140
12141       /* Parse the initializer.  */
12142       initializer = cp_parser_initializer_clause (parser,
12143                                                   &clause_non_constant_p);
12144       /* If any clause is non-constant, so is the entire initializer.  */
12145       if (clause_non_constant_p)
12146         *non_constant_p = true;
12147       /* Add it to the list.  */
12148       initializers = tree_cons (identifier, initializer, initializers);
12149
12150       /* If the next token is not a comma, we have reached the end of
12151          the list.  */
12152       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12153         break;
12154
12155       /* Peek at the next token.  */
12156       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12157       /* If the next token is a `}', then we're still done.  An
12158          initializer-clause can have a trailing `,' after the
12159          initializer-list and before the closing `}'.  */
12160       if (token->type == CPP_CLOSE_BRACE)
12161         break;
12162
12163       /* Consume the `,' token.  */
12164       cp_lexer_consume_token (parser->lexer);
12165     }
12166
12167   /* The initializers were built up in reverse order, so we need to
12168      reverse them now.  */
12169   return nreverse (initializers);
12170 }
12171
12172 /* Classes [gram.class] */
12173
12174 /* Parse a class-name.
12175
12176    class-name:
12177      identifier
12178      template-id
12179
12180    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12181    to indicate that names looked up in dependent types should be
12182    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12183    keyword has been used to indicate that the name that appears next
12184    is a template.  TYPE_P is true iff the next name should be treated
12185    as class-name, even if it is declared to be some other kind of name
12186    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12187    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12188    being defined in a class-head.
12189
12190    Returns the TYPE_DECL representing the class.  */
12191
12192 static tree
12193 cp_parser_class_name (cp_parser *parser,
12194                       bool typename_keyword_p,
12195                       bool template_keyword_p,
12196                       bool type_p,
12197                       bool check_dependency_p,
12198                       bool class_head_p,
12199                       bool is_declaration)
12200 {
12201   tree decl;
12202   tree scope;
12203   bool typename_p;
12204   cp_token *token;
12205
12206   /* All class-names start with an identifier.  */
12207   token = cp_lexer_peek_token (parser->lexer);
12208   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12209     {
12210       cp_parser_error (parser, "expected class-name");
12211       return error_mark_node;
12212     }
12213
12214   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12215      to a template-id, so we save it here.  */
12216   scope = parser->scope;
12217   if (scope == error_mark_node)
12218     return error_mark_node;
12219
12220   /* Any name names a type if we're following the `typename' keyword
12221      in a qualified name where the enclosing scope is type-dependent.  */
12222   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12223                 && dependent_type_p (scope));
12224   /* Handle the common case (an identifier, but not a template-id)
12225      efficiently.  */
12226   if (token->type == CPP_NAME
12227       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12228     {
12229       tree identifier;
12230
12231       /* Look for the identifier.  */
12232       identifier = cp_parser_identifier (parser);
12233       /* If the next token isn't an identifier, we are certainly not
12234          looking at a class-name.  */
12235       if (identifier == error_mark_node)
12236         decl = error_mark_node;
12237       /* If we know this is a type-name, there's no need to look it
12238          up.  */
12239       else if (typename_p)
12240         decl = identifier;
12241       else
12242         {
12243           /* If the next token is a `::', then the name must be a type
12244              name.
12245
12246              [basic.lookup.qual]
12247
12248              During the lookup for a name preceding the :: scope
12249              resolution operator, object, function, and enumerator
12250              names are ignored.  */
12251           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12252             type_p = true;
12253           /* Look up the name.  */
12254           decl = cp_parser_lookup_name (parser, identifier,
12255                                         type_p,
12256                                         /*is_template=*/false,
12257                                         /*is_namespace=*/false,
12258                                         check_dependency_p);
12259         }
12260     }
12261   else
12262     {
12263       /* Try a template-id.  */
12264       decl = cp_parser_template_id (parser, template_keyword_p,
12265                                     check_dependency_p,
12266                                     is_declaration);
12267       if (decl == error_mark_node)
12268         return error_mark_node;
12269     }
12270
12271   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12272
12273   /* If this is a typename, create a TYPENAME_TYPE.  */
12274   if (typename_p && decl != error_mark_node)
12275     {
12276       decl = make_typename_type (scope, decl, /*complain=*/1);
12277       if (decl != error_mark_node)
12278         decl = TYPE_NAME (decl);
12279     }
12280
12281   /* Check to see that it is really the name of a class.  */
12282   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12283       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12284       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12285     /* Situations like this:
12286
12287          template <typename T> struct A {
12288            typename T::template X<int>::I i;
12289          };
12290
12291        are problematic.  Is `T::template X<int>' a class-name?  The
12292        standard does not seem to be definitive, but there is no other
12293        valid interpretation of the following `::'.  Therefore, those
12294        names are considered class-names.  */
12295     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12296   else if (decl == error_mark_node
12297            || TREE_CODE (decl) != TYPE_DECL
12298            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12299     {
12300       cp_parser_error (parser, "expected class-name");
12301       return error_mark_node;
12302     }
12303
12304   return decl;
12305 }
12306
12307 /* Parse a class-specifier.
12308
12309    class-specifier:
12310      class-head { member-specification [opt] }
12311
12312    Returns the TREE_TYPE representing the class.  */
12313
12314 static tree
12315 cp_parser_class_specifier (cp_parser* parser)
12316 {
12317   cp_token *token;
12318   tree type;
12319   tree attributes = NULL_TREE;
12320   int has_trailing_semicolon;
12321   bool nested_name_specifier_p;
12322   unsigned saved_num_template_parameter_lists;
12323   bool pop_p = false;
12324
12325   push_deferring_access_checks (dk_no_deferred);
12326
12327   /* Parse the class-head.  */
12328   type = cp_parser_class_head (parser,
12329                                &nested_name_specifier_p,
12330                                &attributes);
12331   /* If the class-head was a semantic disaster, skip the entire body
12332      of the class.  */
12333   if (!type)
12334     {
12335       cp_parser_skip_to_end_of_block_or_statement (parser);
12336       pop_deferring_access_checks ();
12337       return error_mark_node;
12338     }
12339
12340   /* Look for the `{'.  */
12341   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12342     {
12343       pop_deferring_access_checks ();
12344       return error_mark_node;
12345     }
12346
12347   /* Issue an error message if type-definitions are forbidden here.  */
12348   cp_parser_check_type_definition (parser);
12349   /* Remember that we are defining one more class.  */
12350   ++parser->num_classes_being_defined;
12351   /* Inside the class, surrounding template-parameter-lists do not
12352      apply.  */
12353   saved_num_template_parameter_lists
12354     = parser->num_template_parameter_lists;
12355   parser->num_template_parameter_lists = 0;
12356
12357   /* Start the class.  */
12358   if (nested_name_specifier_p)
12359     pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12360   type = begin_class_definition (type);
12361   if (type == error_mark_node)
12362     /* If the type is erroneous, skip the entire body of the class.  */
12363     cp_parser_skip_to_closing_brace (parser);
12364   else
12365     /* Parse the member-specification.  */
12366     cp_parser_member_specification_opt (parser);
12367   /* Look for the trailing `}'.  */
12368   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12369   /* We get better error messages by noticing a common problem: a
12370      missing trailing `;'.  */
12371   token = cp_lexer_peek_token (parser->lexer);
12372   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12373   /* Look for trailing attributes to apply to this class.  */
12374   if (cp_parser_allow_gnu_extensions_p (parser))
12375     {
12376       tree sub_attr = cp_parser_attributes_opt (parser);
12377       attributes = chainon (attributes, sub_attr);
12378     }
12379   if (type != error_mark_node)
12380     type = finish_struct (type, attributes);
12381   if (pop_p)
12382     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12383   /* If this class is not itself within the scope of another class,
12384      then we need to parse the bodies of all of the queued function
12385      definitions.  Note that the queued functions defined in a class
12386      are not always processed immediately following the
12387      class-specifier for that class.  Consider:
12388
12389        struct A {
12390          struct B { void f() { sizeof (A); } };
12391        };
12392
12393      If `f' were processed before the processing of `A' were
12394      completed, there would be no way to compute the size of `A'.
12395      Note that the nesting we are interested in here is lexical --
12396      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12397      for:
12398
12399        struct A { struct B; };
12400        struct A::B { void f() { } };
12401
12402      there is no need to delay the parsing of `A::B::f'.  */
12403   if (--parser->num_classes_being_defined == 0)
12404     {
12405       tree queue_entry;
12406       tree fn;
12407
12408       /* In a first pass, parse default arguments to the functions.
12409          Then, in a second pass, parse the bodies of the functions.
12410          This two-phased approach handles cases like:
12411
12412             struct S {
12413               void f() { g(); }
12414               void g(int i = 3);
12415             };
12416
12417          */
12418       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12419              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12420            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12421            TREE_PURPOSE (parser->unparsed_functions_queues)
12422              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12423         {
12424           fn = TREE_VALUE (queue_entry);
12425           /* Make sure that any template parameters are in scope.  */
12426           maybe_begin_member_template_processing (fn);
12427           /* If there are default arguments that have not yet been processed,
12428              take care of them now.  */
12429           cp_parser_late_parsing_default_args (parser, fn);
12430           /* Remove any template parameters from the symbol table.  */
12431           maybe_end_member_template_processing ();
12432         }
12433       /* Now parse the body of the functions.  */
12434       for (TREE_VALUE (parser->unparsed_functions_queues)
12435              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12436            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12437            TREE_VALUE (parser->unparsed_functions_queues)
12438              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12439         {
12440           /* Figure out which function we need to process.  */
12441           fn = TREE_VALUE (queue_entry);
12442
12443           /* A hack to prevent garbage collection.  */
12444           function_depth++;
12445
12446           /* Parse the function.  */
12447           cp_parser_late_parsing_for_member (parser, fn);
12448           function_depth--;
12449         }
12450
12451     }
12452
12453   /* Put back any saved access checks.  */
12454   pop_deferring_access_checks ();
12455
12456   /* Restore the count of active template-parameter-lists.  */
12457   parser->num_template_parameter_lists
12458     = saved_num_template_parameter_lists;
12459
12460   return type;
12461 }
12462
12463 /* Parse a class-head.
12464
12465    class-head:
12466      class-key identifier [opt] base-clause [opt]
12467      class-key nested-name-specifier identifier base-clause [opt]
12468      class-key nested-name-specifier [opt] template-id
12469        base-clause [opt]
12470
12471    GNU Extensions:
12472      class-key attributes identifier [opt] base-clause [opt]
12473      class-key attributes nested-name-specifier identifier base-clause [opt]
12474      class-key attributes nested-name-specifier [opt] template-id
12475        base-clause [opt]
12476
12477    Returns the TYPE of the indicated class.  Sets
12478    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12479    involving a nested-name-specifier was used, and FALSE otherwise.
12480
12481    Returns NULL_TREE if the class-head is syntactically valid, but
12482    semantically invalid in a way that means we should skip the entire
12483    body of the class.  */
12484
12485 static tree
12486 cp_parser_class_head (cp_parser* parser,
12487                       bool* nested_name_specifier_p,
12488                       tree *attributes_p)
12489 {
12490   cp_token *token;
12491   tree nested_name_specifier;
12492   enum tag_types class_key;
12493   tree id = NULL_TREE;
12494   tree type = NULL_TREE;
12495   tree attributes;
12496   bool template_id_p = false;
12497   bool qualified_p = false;
12498   bool invalid_nested_name_p = false;
12499   bool invalid_explicit_specialization_p = false;
12500   bool pop_p = false;
12501   unsigned num_templates;
12502
12503   /* Assume no nested-name-specifier will be present.  */
12504   *nested_name_specifier_p = false;
12505   /* Assume no template parameter lists will be used in defining the
12506      type.  */
12507   num_templates = 0;
12508
12509   /* Look for the class-key.  */
12510   class_key = cp_parser_class_key (parser);
12511   if (class_key == none_type)
12512     return error_mark_node;
12513
12514   /* Parse the attributes.  */
12515   attributes = cp_parser_attributes_opt (parser);
12516
12517   /* If the next token is `::', that is invalid -- but sometimes
12518      people do try to write:
12519
12520        struct ::S {};
12521
12522      Handle this gracefully by accepting the extra qualifier, and then
12523      issuing an error about it later if this really is a
12524      class-head.  If it turns out just to be an elaborated type
12525      specifier, remain silent.  */
12526   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12527     qualified_p = true;
12528
12529   push_deferring_access_checks (dk_no_check);
12530
12531   /* Determine the name of the class.  Begin by looking for an
12532      optional nested-name-specifier.  */
12533   nested_name_specifier
12534     = cp_parser_nested_name_specifier_opt (parser,
12535                                            /*typename_keyword_p=*/false,
12536                                            /*check_dependency_p=*/false,
12537                                            /*type_p=*/false,
12538                                            /*is_declaration=*/false);
12539   /* If there was a nested-name-specifier, then there *must* be an
12540      identifier.  */
12541   if (nested_name_specifier)
12542     {
12543       /* Although the grammar says `identifier', it really means
12544          `class-name' or `template-name'.  You are only allowed to
12545          define a class that has already been declared with this
12546          syntax.
12547
12548          The proposed resolution for Core Issue 180 says that whever
12549          you see `class T::X' you should treat `X' as a type-name.
12550
12551          It is OK to define an inaccessible class; for example:
12552
12553            class A { class B; };
12554            class A::B {};
12555
12556          We do not know if we will see a class-name, or a
12557          template-name.  We look for a class-name first, in case the
12558          class-name is a template-id; if we looked for the
12559          template-name first we would stop after the template-name.  */
12560       cp_parser_parse_tentatively (parser);
12561       type = cp_parser_class_name (parser,
12562                                    /*typename_keyword_p=*/false,
12563                                    /*template_keyword_p=*/false,
12564                                    /*type_p=*/true,
12565                                    /*check_dependency_p=*/false,
12566                                    /*class_head_p=*/true,
12567                                    /*is_declaration=*/false);
12568       /* If that didn't work, ignore the nested-name-specifier.  */
12569       if (!cp_parser_parse_definitely (parser))
12570         {
12571           invalid_nested_name_p = true;
12572           id = cp_parser_identifier (parser);
12573           if (id == error_mark_node)
12574             id = NULL_TREE;
12575         }
12576       /* If we could not find a corresponding TYPE, treat this
12577          declaration like an unqualified declaration.  */
12578       if (type == error_mark_node)
12579         nested_name_specifier = NULL_TREE;
12580       /* Otherwise, count the number of templates used in TYPE and its
12581          containing scopes.  */
12582       else
12583         {
12584           tree scope;
12585
12586           for (scope = TREE_TYPE (type);
12587                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12588                scope = (TYPE_P (scope)
12589                         ? TYPE_CONTEXT (scope)
12590                         : DECL_CONTEXT (scope)))
12591             if (TYPE_P (scope)
12592                 && CLASS_TYPE_P (scope)
12593                 && CLASSTYPE_TEMPLATE_INFO (scope)
12594                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12595                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12596               ++num_templates;
12597         }
12598     }
12599   /* Otherwise, the identifier is optional.  */
12600   else
12601     {
12602       /* We don't know whether what comes next is a template-id,
12603          an identifier, or nothing at all.  */
12604       cp_parser_parse_tentatively (parser);
12605       /* Check for a template-id.  */
12606       id = cp_parser_template_id (parser,
12607                                   /*template_keyword_p=*/false,
12608                                   /*check_dependency_p=*/true,
12609                                   /*is_declaration=*/true);
12610       /* If that didn't work, it could still be an identifier.  */
12611       if (!cp_parser_parse_definitely (parser))
12612         {
12613           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12614             id = cp_parser_identifier (parser);
12615           else
12616             id = NULL_TREE;
12617         }
12618       else
12619         {
12620           template_id_p = true;
12621           ++num_templates;
12622         }
12623     }
12624
12625   pop_deferring_access_checks ();
12626
12627   if (id)
12628     cp_parser_check_for_invalid_template_id (parser, id);
12629
12630   /* If it's not a `:' or a `{' then we can't really be looking at a
12631      class-head, since a class-head only appears as part of a
12632      class-specifier.  We have to detect this situation before calling
12633      xref_tag, since that has irreversible side-effects.  */
12634   if (!cp_parser_next_token_starts_class_definition_p (parser))
12635     {
12636       cp_parser_error (parser, "expected `{' or `:'");
12637       return error_mark_node;
12638     }
12639
12640   /* At this point, we're going ahead with the class-specifier, even
12641      if some other problem occurs.  */
12642   cp_parser_commit_to_tentative_parse (parser);
12643   /* Issue the error about the overly-qualified name now.  */
12644   if (qualified_p)
12645     cp_parser_error (parser,
12646                      "global qualification of class name is invalid");
12647   else if (invalid_nested_name_p)
12648     cp_parser_error (parser,
12649                      "qualified name does not name a class");
12650   else if (nested_name_specifier)
12651     {
12652       tree scope;
12653       /* Figure out in what scope the declaration is being placed.  */
12654       scope = current_scope ();
12655       if (!scope)
12656         scope = current_namespace;
12657       /* If that scope does not contain the scope in which the
12658          class was originally declared, the program is invalid.  */
12659       if (scope && !is_ancestor (scope, nested_name_specifier))
12660         {
12661           error ("declaration of `%D' in `%D' which does not "
12662                  "enclose `%D'", type, scope, nested_name_specifier);
12663           type = NULL_TREE;
12664           goto done;
12665         }
12666       /* [dcl.meaning]
12667
12668          A declarator-id shall not be qualified exception of the
12669          definition of a ... nested class outside of its class
12670          ... [or] a the definition or explicit instantiation of a
12671          class member of a namespace outside of its namespace.  */
12672       if (scope == nested_name_specifier)
12673         {
12674           pedwarn ("extra qualification ignored");
12675           nested_name_specifier = NULL_TREE;
12676           num_templates = 0;
12677         }
12678     }
12679   /* An explicit-specialization must be preceded by "template <>".  If
12680      it is not, try to recover gracefully.  */
12681   if (at_namespace_scope_p ()
12682       && parser->num_template_parameter_lists == 0
12683       && template_id_p)
12684     {
12685       error ("an explicit specialization must be preceded by 'template <>'");
12686       invalid_explicit_specialization_p = true;
12687       /* Take the same action that would have been taken by
12688          cp_parser_explicit_specialization.  */
12689       ++parser->num_template_parameter_lists;
12690       begin_specialization ();
12691     }
12692   /* There must be no "return" statements between this point and the
12693      end of this function; set "type "to the correct return value and
12694      use "goto done;" to return.  */
12695   /* Make sure that the right number of template parameters were
12696      present.  */
12697   if (!cp_parser_check_template_parameters (parser, num_templates))
12698     {
12699       /* If something went wrong, there is no point in even trying to
12700          process the class-definition.  */
12701       type = NULL_TREE;
12702       goto done;
12703     }
12704
12705   /* Look up the type.  */
12706   if (template_id_p)
12707     {
12708       type = TREE_TYPE (id);
12709       maybe_process_partial_specialization (type);
12710     }
12711   else if (!nested_name_specifier)
12712     {
12713       /* If the class was unnamed, create a dummy name.  */
12714       if (!id)
12715         id = make_anon_name ();
12716       type = xref_tag (class_key, id, /*globalize=*/false,
12717                        parser->num_template_parameter_lists);
12718     }
12719   else
12720     {
12721       tree class_type;
12722       bool pop_p = false;
12723
12724       /* Given:
12725
12726             template <typename T> struct S { struct T };
12727             template <typename T> struct S<T>::T { };
12728
12729          we will get a TYPENAME_TYPE when processing the definition of
12730          `S::T'.  We need to resolve it to the actual type before we
12731          try to define it.  */
12732       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12733         {
12734           class_type = resolve_typename_type (TREE_TYPE (type),
12735                                               /*only_current_p=*/false);
12736           if (class_type != error_mark_node)
12737             type = TYPE_NAME (class_type);
12738           else
12739             {
12740               cp_parser_error (parser, "could not resolve typename type");
12741               type = error_mark_node;
12742             }
12743         }
12744
12745       maybe_process_partial_specialization (TREE_TYPE (type));
12746       class_type = current_class_type;
12747       /* Enter the scope indicated by the nested-name-specifier.  */
12748       if (nested_name_specifier)
12749         pop_p = push_scope (nested_name_specifier);
12750       /* Get the canonical version of this type.  */
12751       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12752       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12753           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12754         type = push_template_decl (type);
12755       type = TREE_TYPE (type);
12756       if (nested_name_specifier)
12757         {
12758           *nested_name_specifier_p = true;
12759           if (pop_p)
12760             pop_scope (nested_name_specifier);
12761         }
12762     }
12763   /* Indicate whether this class was declared as a `class' or as a
12764      `struct'.  */
12765   if (TREE_CODE (type) == RECORD_TYPE)
12766     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12767   cp_parser_check_class_key (class_key, type);
12768
12769   /* Enter the scope containing the class; the names of base classes
12770      should be looked up in that context.  For example, given:
12771
12772        struct A { struct B {}; struct C; };
12773        struct A::C : B {};
12774
12775      is valid.  */
12776   if (nested_name_specifier)
12777     pop_p = push_scope (nested_name_specifier);
12778   /* Now, look for the base-clause.  */
12779   token = cp_lexer_peek_token (parser->lexer);
12780   if (token->type == CPP_COLON)
12781     {
12782       tree bases;
12783
12784       /* Get the list of base-classes.  */
12785       bases = cp_parser_base_clause (parser);
12786       /* Process them.  */
12787       xref_basetypes (type, bases);
12788     }
12789   /* Leave the scope given by the nested-name-specifier.  We will
12790      enter the class scope itself while processing the members.  */
12791   if (pop_p)
12792     pop_scope (nested_name_specifier);
12793
12794  done:
12795   if (invalid_explicit_specialization_p)
12796     {
12797       end_specialization ();
12798       --parser->num_template_parameter_lists;
12799     }
12800   *attributes_p = attributes;
12801   return type;
12802 }
12803
12804 /* Parse a class-key.
12805
12806    class-key:
12807      class
12808      struct
12809      union
12810
12811    Returns the kind of class-key specified, or none_type to indicate
12812    error.  */
12813
12814 static enum tag_types
12815 cp_parser_class_key (cp_parser* parser)
12816 {
12817   cp_token *token;
12818   enum tag_types tag_type;
12819
12820   /* Look for the class-key.  */
12821   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12822   if (!token)
12823     return none_type;
12824
12825   /* Check to see if the TOKEN is a class-key.  */
12826   tag_type = cp_parser_token_is_class_key (token);
12827   if (!tag_type)
12828     cp_parser_error (parser, "expected class-key");
12829   return tag_type;
12830 }
12831
12832 /* Parse an (optional) member-specification.
12833
12834    member-specification:
12835      member-declaration member-specification [opt]
12836      access-specifier : member-specification [opt]  */
12837
12838 static void
12839 cp_parser_member_specification_opt (cp_parser* parser)
12840 {
12841   while (true)
12842     {
12843       cp_token *token;
12844       enum rid keyword;
12845
12846       /* Peek at the next token.  */
12847       token = cp_lexer_peek_token (parser->lexer);
12848       /* If it's a `}', or EOF then we've seen all the members.  */
12849       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12850         break;
12851
12852       /* See if this token is a keyword.  */
12853       keyword = token->keyword;
12854       switch (keyword)
12855         {
12856         case RID_PUBLIC:
12857         case RID_PROTECTED:
12858         case RID_PRIVATE:
12859           /* Consume the access-specifier.  */
12860           cp_lexer_consume_token (parser->lexer);
12861           /* Remember which access-specifier is active.  */
12862           current_access_specifier = token->value;
12863           /* Look for the `:'.  */
12864           cp_parser_require (parser, CPP_COLON, "`:'");
12865           break;
12866
12867         default:
12868           /* Otherwise, the next construction must be a
12869              member-declaration.  */
12870           cp_parser_member_declaration (parser);
12871         }
12872     }
12873 }
12874
12875 /* Parse a member-declaration.
12876
12877    member-declaration:
12878      decl-specifier-seq [opt] member-declarator-list [opt] ;
12879      function-definition ; [opt]
12880      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12881      using-declaration
12882      template-declaration
12883
12884    member-declarator-list:
12885      member-declarator
12886      member-declarator-list , member-declarator
12887
12888    member-declarator:
12889      declarator pure-specifier [opt]
12890      declarator constant-initializer [opt]
12891      identifier [opt] : constant-expression
12892
12893    GNU Extensions:
12894
12895    member-declaration:
12896      __extension__ member-declaration
12897
12898    member-declarator:
12899      declarator attributes [opt] pure-specifier [opt]
12900      declarator attributes [opt] constant-initializer [opt]
12901      identifier [opt] attributes [opt] : constant-expression  */
12902
12903 static void
12904 cp_parser_member_declaration (cp_parser* parser)
12905 {
12906   cp_decl_specifier_seq decl_specifiers;
12907   tree prefix_attributes;
12908   tree decl;
12909   int declares_class_or_enum;
12910   bool friend_p;
12911   cp_token *token;
12912   int saved_pedantic;
12913
12914   /* Check for the `__extension__' keyword.  */
12915   if (cp_parser_extension_opt (parser, &saved_pedantic))
12916     {
12917       /* Recurse.  */
12918       cp_parser_member_declaration (parser);
12919       /* Restore the old value of the PEDANTIC flag.  */
12920       pedantic = saved_pedantic;
12921
12922       return;
12923     }
12924
12925   /* Check for a template-declaration.  */
12926   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12927     {
12928       /* Parse the template-declaration.  */
12929       cp_parser_template_declaration (parser, /*member_p=*/true);
12930
12931       return;
12932     }
12933
12934   /* Check for a using-declaration.  */
12935   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12936     {
12937       /* Parse the using-declaration.  */
12938       cp_parser_using_declaration (parser);
12939
12940       return;
12941     }
12942
12943   /* Parse the decl-specifier-seq.  */
12944   cp_parser_decl_specifier_seq (parser,
12945                                 CP_PARSER_FLAGS_OPTIONAL,
12946                                 &decl_specifiers,
12947                                 &declares_class_or_enum);
12948   prefix_attributes = decl_specifiers.attributes;
12949   decl_specifiers.attributes = NULL_TREE;
12950   /* Check for an invalid type-name.  */
12951   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12952     return;
12953   /* If there is no declarator, then the decl-specifier-seq should
12954      specify a type.  */
12955   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12956     {
12957       /* If there was no decl-specifier-seq, and the next token is a
12958          `;', then we have something like:
12959
12960            struct S { ; };
12961
12962          [class.mem]
12963
12964          Each member-declaration shall declare at least one member
12965          name of the class.  */
12966       if (!decl_specifiers.any_specifiers_p)
12967         {
12968           if (pedantic)
12969             pedwarn ("extra semicolon");
12970         }
12971       else
12972         {
12973           tree type;
12974
12975           /* See if this declaration is a friend.  */
12976           friend_p = cp_parser_friend_p (&decl_specifiers);
12977           /* If there were decl-specifiers, check to see if there was
12978              a class-declaration.  */
12979           type = check_tag_decl (&decl_specifiers);
12980           /* Nested classes have already been added to the class, but
12981              a `friend' needs to be explicitly registered.  */
12982           if (friend_p)
12983             {
12984               /* If the `friend' keyword was present, the friend must
12985                  be introduced with a class-key.  */
12986                if (!declares_class_or_enum)
12987                  error ("a class-key must be used when declaring a friend");
12988                /* In this case:
12989
12990                     template <typename T> struct A {
12991                       friend struct A<T>::B;
12992                     };
12993
12994                   A<T>::B will be represented by a TYPENAME_TYPE, and
12995                   therefore not recognized by check_tag_decl.  */
12996                if (!type 
12997                    && decl_specifiers.type
12998                    && TYPE_P (decl_specifiers.type))
12999                  type = decl_specifiers.type;
13000                if (!type || !TYPE_P (type))
13001                  error ("friend declaration does not name a class or "
13002                         "function");
13003                else
13004                  make_friend_class (current_class_type, type,
13005                                     /*complain=*/true);
13006             }
13007           /* If there is no TYPE, an error message will already have
13008              been issued.  */
13009           else if (!type || type == error_mark_node)
13010             ;
13011           /* An anonymous aggregate has to be handled specially; such
13012              a declaration really declares a data member (with a
13013              particular type), as opposed to a nested class.  */
13014           else if (ANON_AGGR_TYPE_P (type))
13015             {
13016               /* Remove constructors and such from TYPE, now that we
13017                  know it is an anonymous aggregate.  */
13018               fixup_anonymous_aggr (type);
13019               /* And make the corresponding data member.  */
13020               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13021               /* Add it to the class.  */
13022               finish_member_declaration (decl);
13023             }
13024           else
13025             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13026         }
13027     }
13028   else
13029     {
13030       /* See if these declarations will be friends.  */
13031       friend_p = cp_parser_friend_p (&decl_specifiers);
13032
13033       /* Keep going until we hit the `;' at the end of the
13034          declaration.  */
13035       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13036         {
13037           tree attributes = NULL_TREE;
13038           tree first_attribute;
13039
13040           /* Peek at the next token.  */
13041           token = cp_lexer_peek_token (parser->lexer);
13042
13043           /* Check for a bitfield declaration.  */
13044           if (token->type == CPP_COLON
13045               || (token->type == CPP_NAME
13046                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13047                   == CPP_COLON))
13048             {
13049               tree identifier;
13050               tree width;
13051
13052               /* Get the name of the bitfield.  Note that we cannot just
13053                  check TOKEN here because it may have been invalidated by
13054                  the call to cp_lexer_peek_nth_token above.  */
13055               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13056                 identifier = cp_parser_identifier (parser);
13057               else
13058                 identifier = NULL_TREE;
13059
13060               /* Consume the `:' token.  */
13061               cp_lexer_consume_token (parser->lexer);
13062               /* Get the width of the bitfield.  */
13063               width
13064                 = cp_parser_constant_expression (parser,
13065                                                  /*allow_non_constant=*/false,
13066                                                  NULL);
13067
13068               /* Look for attributes that apply to the bitfield.  */
13069               attributes = cp_parser_attributes_opt (parser);
13070               /* Remember which attributes are prefix attributes and
13071                  which are not.  */
13072               first_attribute = attributes;
13073               /* Combine the attributes.  */
13074               attributes = chainon (prefix_attributes, attributes);
13075
13076               /* Create the bitfield declaration.  */
13077               decl = grokbitfield (identifier 
13078                                    ? make_id_declarator (identifier)
13079                                    : NULL,
13080                                    &decl_specifiers,
13081                                    width);
13082               /* Apply the attributes.  */
13083               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13084             }
13085           else
13086             {
13087               cp_declarator *declarator;
13088               tree initializer;
13089               tree asm_specification;
13090               int ctor_dtor_or_conv_p;
13091
13092               /* Parse the declarator.  */
13093               declarator
13094                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13095                                         &ctor_dtor_or_conv_p,
13096                                         /*parenthesized_p=*/NULL);
13097
13098               /* If something went wrong parsing the declarator, make sure
13099                  that we at least consume some tokens.  */
13100               if (declarator == cp_error_declarator)
13101                 {
13102                   /* Skip to the end of the statement.  */
13103                   cp_parser_skip_to_end_of_statement (parser);
13104                   /* If the next token is not a semicolon, that is
13105                      probably because we just skipped over the body of
13106                      a function.  So, we consume a semicolon if
13107                      present, but do not issue an error message if it
13108                      is not present.  */
13109                   if (cp_lexer_next_token_is (parser->lexer,
13110                                               CPP_SEMICOLON))
13111                     cp_lexer_consume_token (parser->lexer);
13112                   return;
13113                 }
13114
13115               cp_parser_check_for_definition_in_return_type
13116                 (declarator, declares_class_or_enum);
13117
13118               /* Look for an asm-specification.  */
13119               asm_specification = cp_parser_asm_specification_opt (parser);
13120               /* Look for attributes that apply to the declaration.  */
13121               attributes = cp_parser_attributes_opt (parser);
13122               /* Remember which attributes are prefix attributes and
13123                  which are not.  */
13124               first_attribute = attributes;
13125               /* Combine the attributes.  */
13126               attributes = chainon (prefix_attributes, attributes);
13127
13128               /* If it's an `=', then we have a constant-initializer or a
13129                  pure-specifier.  It is not correct to parse the
13130                  initializer before registering the member declaration
13131                  since the member declaration should be in scope while
13132                  its initializer is processed.  However, the rest of the
13133                  front end does not yet provide an interface that allows
13134                  us to handle this correctly.  */
13135               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13136                 {
13137                   /* In [class.mem]:
13138
13139                      A pure-specifier shall be used only in the declaration of
13140                      a virtual function.
13141
13142                      A member-declarator can contain a constant-initializer
13143                      only if it declares a static member of integral or
13144                      enumeration type.
13145
13146                      Therefore, if the DECLARATOR is for a function, we look
13147                      for a pure-specifier; otherwise, we look for a
13148                      constant-initializer.  When we call `grokfield', it will
13149                      perform more stringent semantics checks.  */
13150                   if (declarator->kind == cdk_function)
13151                     initializer = cp_parser_pure_specifier (parser);
13152                   else
13153                     /* Parse the initializer.  */
13154                     initializer = cp_parser_constant_initializer (parser);
13155                 }
13156               /* Otherwise, there is no initializer.  */
13157               else
13158                 initializer = NULL_TREE;
13159
13160               /* See if we are probably looking at a function
13161                  definition.  We are certainly not looking at at a
13162                  member-declarator.  Calling `grokfield' has
13163                  side-effects, so we must not do it unless we are sure
13164                  that we are looking at a member-declarator.  */
13165               if (cp_parser_token_starts_function_definition_p
13166                   (cp_lexer_peek_token (parser->lexer)))
13167                 {
13168                   /* The grammar does not allow a pure-specifier to be
13169                      used when a member function is defined.  (It is
13170                      possible that this fact is an oversight in the
13171                      standard, since a pure function may be defined
13172                      outside of the class-specifier.  */
13173                   if (initializer)
13174                     error ("pure-specifier on function-definition");
13175                   decl = cp_parser_save_member_function_body (parser,
13176                                                               &decl_specifiers,
13177                                                               declarator,
13178                                                               attributes);
13179                   /* If the member was not a friend, declare it here.  */
13180                   if (!friend_p)
13181                     finish_member_declaration (decl);
13182                   /* Peek at the next token.  */
13183                   token = cp_lexer_peek_token (parser->lexer);
13184                   /* If the next token is a semicolon, consume it.  */
13185                   if (token->type == CPP_SEMICOLON)
13186                     cp_lexer_consume_token (parser->lexer);
13187                   return;
13188                 }
13189               else
13190                 {
13191                   /* Create the declaration.  */
13192                   decl = grokfield (declarator, &decl_specifiers,
13193                                     initializer, asm_specification,
13194                                     attributes);
13195                   /* Any initialization must have been from a
13196                      constant-expression.  */
13197                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13198                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13199                 }
13200             }
13201
13202           /* Reset PREFIX_ATTRIBUTES.  */
13203           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13204             attributes = TREE_CHAIN (attributes);
13205           if (attributes)
13206             TREE_CHAIN (attributes) = NULL_TREE;
13207
13208           /* If there is any qualification still in effect, clear it
13209              now; we will be starting fresh with the next declarator.  */
13210           parser->scope = NULL_TREE;
13211           parser->qualifying_scope = NULL_TREE;
13212           parser->object_scope = NULL_TREE;
13213           /* If it's a `,', then there are more declarators.  */
13214           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13215             cp_lexer_consume_token (parser->lexer);
13216           /* If the next token isn't a `;', then we have a parse error.  */
13217           else if (cp_lexer_next_token_is_not (parser->lexer,
13218                                                CPP_SEMICOLON))
13219             {
13220               cp_parser_error (parser, "expected `;'");
13221               /* Skip tokens until we find a `;'.  */
13222               cp_parser_skip_to_end_of_statement (parser);
13223
13224               break;
13225             }
13226
13227           if (decl)
13228             {
13229               /* Add DECL to the list of members.  */
13230               if (!friend_p)
13231                 finish_member_declaration (decl);
13232
13233               if (TREE_CODE (decl) == FUNCTION_DECL)
13234                 cp_parser_save_default_args (parser, decl);
13235             }
13236         }
13237     }
13238
13239   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13240 }
13241
13242 /* Parse a pure-specifier.
13243
13244    pure-specifier:
13245      = 0
13246
13247    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13248    Otherwise, ERROR_MARK_NODE is returned.  */
13249
13250 static tree
13251 cp_parser_pure_specifier (cp_parser* parser)
13252 {
13253   cp_token *token;
13254
13255   /* Look for the `=' token.  */
13256   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13257     return error_mark_node;
13258   /* Look for the `0' token.  */
13259   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13260   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13261      to get information from the lexer about how the number was
13262      spelled in order to fix this problem.  */
13263   if (!token || !integer_zerop (token->value))
13264     return error_mark_node;
13265
13266   return integer_zero_node;
13267 }
13268
13269 /* Parse a constant-initializer.
13270
13271    constant-initializer:
13272      = constant-expression
13273
13274    Returns a representation of the constant-expression.  */
13275
13276 static tree
13277 cp_parser_constant_initializer (cp_parser* parser)
13278 {
13279   /* Look for the `=' token.  */
13280   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13281     return error_mark_node;
13282
13283   /* It is invalid to write:
13284
13285        struct S { static const int i = { 7 }; };
13286
13287      */
13288   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13289     {
13290       cp_parser_error (parser,
13291                        "a brace-enclosed initializer is not allowed here");
13292       /* Consume the opening brace.  */
13293       cp_lexer_consume_token (parser->lexer);
13294       /* Skip the initializer.  */
13295       cp_parser_skip_to_closing_brace (parser);
13296       /* Look for the trailing `}'.  */
13297       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13298
13299       return error_mark_node;
13300     }
13301
13302   return cp_parser_constant_expression (parser,
13303                                         /*allow_non_constant=*/false,
13304                                         NULL);
13305 }
13306
13307 /* Derived classes [gram.class.derived] */
13308
13309 /* Parse a base-clause.
13310
13311    base-clause:
13312      : base-specifier-list
13313
13314    base-specifier-list:
13315      base-specifier
13316      base-specifier-list , base-specifier
13317
13318    Returns a TREE_LIST representing the base-classes, in the order in
13319    which they were declared.  The representation of each node is as
13320    described by cp_parser_base_specifier.
13321
13322    In the case that no bases are specified, this function will return
13323    NULL_TREE, not ERROR_MARK_NODE.  */
13324
13325 static tree
13326 cp_parser_base_clause (cp_parser* parser)
13327 {
13328   tree bases = NULL_TREE;
13329
13330   /* Look for the `:' that begins the list.  */
13331   cp_parser_require (parser, CPP_COLON, "`:'");
13332
13333   /* Scan the base-specifier-list.  */
13334   while (true)
13335     {
13336       cp_token *token;
13337       tree base;
13338
13339       /* Look for the base-specifier.  */
13340       base = cp_parser_base_specifier (parser);
13341       /* Add BASE to the front of the list.  */
13342       if (base != error_mark_node)
13343         {
13344           TREE_CHAIN (base) = bases;
13345           bases = base;
13346         }
13347       /* Peek at the next token.  */
13348       token = cp_lexer_peek_token (parser->lexer);
13349       /* If it's not a comma, then the list is complete.  */
13350       if (token->type != CPP_COMMA)
13351         break;
13352       /* Consume the `,'.  */
13353       cp_lexer_consume_token (parser->lexer);
13354     }
13355
13356   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13357      base class had a qualified name.  However, the next name that
13358      appears is certainly not qualified.  */
13359   parser->scope = NULL_TREE;
13360   parser->qualifying_scope = NULL_TREE;
13361   parser->object_scope = NULL_TREE;
13362
13363   return nreverse (bases);
13364 }
13365
13366 /* Parse a base-specifier.
13367
13368    base-specifier:
13369      :: [opt] nested-name-specifier [opt] class-name
13370      virtual access-specifier [opt] :: [opt] nested-name-specifier
13371        [opt] class-name
13372      access-specifier virtual [opt] :: [opt] nested-name-specifier
13373        [opt] class-name
13374
13375    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13376    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13377    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13378    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13379
13380 static tree
13381 cp_parser_base_specifier (cp_parser* parser)
13382 {
13383   cp_token *token;
13384   bool done = false;
13385   bool virtual_p = false;
13386   bool duplicate_virtual_error_issued_p = false;
13387   bool duplicate_access_error_issued_p = false;
13388   bool class_scope_p, template_p;
13389   tree access = access_default_node;
13390   tree type;
13391
13392   /* Process the optional `virtual' and `access-specifier'.  */
13393   while (!done)
13394     {
13395       /* Peek at the next token.  */
13396       token = cp_lexer_peek_token (parser->lexer);
13397       /* Process `virtual'.  */
13398       switch (token->keyword)
13399         {
13400         case RID_VIRTUAL:
13401           /* If `virtual' appears more than once, issue an error.  */
13402           if (virtual_p && !duplicate_virtual_error_issued_p)
13403             {
13404               cp_parser_error (parser,
13405                                "`virtual' specified more than once in base-specified");
13406               duplicate_virtual_error_issued_p = true;
13407             }
13408
13409           virtual_p = true;
13410
13411           /* Consume the `virtual' token.  */
13412           cp_lexer_consume_token (parser->lexer);
13413
13414           break;
13415
13416         case RID_PUBLIC:
13417         case RID_PROTECTED:
13418         case RID_PRIVATE:
13419           /* If more than one access specifier appears, issue an
13420              error.  */
13421           if (access != access_default_node
13422               && !duplicate_access_error_issued_p)
13423             {
13424               cp_parser_error (parser,
13425                                "more than one access specifier in base-specified");
13426               duplicate_access_error_issued_p = true;
13427             }
13428
13429           access = ridpointers[(int) token->keyword];
13430
13431           /* Consume the access-specifier.  */
13432           cp_lexer_consume_token (parser->lexer);
13433
13434           break;
13435
13436         default:
13437           done = true;
13438           break;
13439         }
13440     }
13441   /* It is not uncommon to see programs mechanically, erroneously, use
13442      the 'typename' keyword to denote (dependent) qualified types
13443      as base classes.  */
13444   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13445     {
13446       if (!processing_template_decl)
13447         error ("keyword `typename' not allowed outside of templates");
13448       else
13449         error ("keyword `typename' not allowed in this context "
13450                "(the base class is implicitly a type)");
13451       cp_lexer_consume_token (parser->lexer);
13452     }
13453
13454   /* Look for the optional `::' operator.  */
13455   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13456   /* Look for the nested-name-specifier.  The simplest way to
13457      implement:
13458
13459        [temp.res]
13460
13461        The keyword `typename' is not permitted in a base-specifier or
13462        mem-initializer; in these contexts a qualified name that
13463        depends on a template-parameter is implicitly assumed to be a
13464        type name.
13465
13466      is to pretend that we have seen the `typename' keyword at this
13467      point.  */
13468   cp_parser_nested_name_specifier_opt (parser,
13469                                        /*typename_keyword_p=*/true,
13470                                        /*check_dependency_p=*/true,
13471                                        /*type_p=*/true,
13472                                        /*is_declaration=*/true);
13473   /* If the base class is given by a qualified name, assume that names
13474      we see are type names or templates, as appropriate.  */
13475   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13476   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13477
13478   /* Finally, look for the class-name.  */
13479   type = cp_parser_class_name (parser,
13480                                class_scope_p,
13481                                template_p,
13482                                /*type_p=*/true,
13483                                /*check_dependency_p=*/true,
13484                                /*class_head_p=*/false,
13485                                /*is_declaration=*/true);
13486
13487   if (type == error_mark_node)
13488     return error_mark_node;
13489
13490   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13491 }
13492
13493 /* Exception handling [gram.exception] */
13494
13495 /* Parse an (optional) exception-specification.
13496
13497    exception-specification:
13498      throw ( type-id-list [opt] )
13499
13500    Returns a TREE_LIST representing the exception-specification.  The
13501    TREE_VALUE of each node is a type.  */
13502
13503 static tree
13504 cp_parser_exception_specification_opt (cp_parser* parser)
13505 {
13506   cp_token *token;
13507   tree type_id_list;
13508
13509   /* Peek at the next token.  */
13510   token = cp_lexer_peek_token (parser->lexer);
13511   /* If it's not `throw', then there's no exception-specification.  */
13512   if (!cp_parser_is_keyword (token, RID_THROW))
13513     return NULL_TREE;
13514
13515   /* Consume the `throw'.  */
13516   cp_lexer_consume_token (parser->lexer);
13517
13518   /* Look for the `('.  */
13519   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13520
13521   /* Peek at the next token.  */
13522   token = cp_lexer_peek_token (parser->lexer);
13523   /* If it's not a `)', then there is a type-id-list.  */
13524   if (token->type != CPP_CLOSE_PAREN)
13525     {
13526       const char *saved_message;
13527
13528       /* Types may not be defined in an exception-specification.  */
13529       saved_message = parser->type_definition_forbidden_message;
13530       parser->type_definition_forbidden_message
13531         = "types may not be defined in an exception-specification";
13532       /* Parse the type-id-list.  */
13533       type_id_list = cp_parser_type_id_list (parser);
13534       /* Restore the saved message.  */
13535       parser->type_definition_forbidden_message = saved_message;
13536     }
13537   else
13538     type_id_list = empty_except_spec;
13539
13540   /* Look for the `)'.  */
13541   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13542
13543   return type_id_list;
13544 }
13545
13546 /* Parse an (optional) type-id-list.
13547
13548    type-id-list:
13549      type-id
13550      type-id-list , type-id
13551
13552    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13553    in the order that the types were presented.  */
13554
13555 static tree
13556 cp_parser_type_id_list (cp_parser* parser)
13557 {
13558   tree types = NULL_TREE;
13559
13560   while (true)
13561     {
13562       cp_token *token;
13563       tree type;
13564
13565       /* Get the next type-id.  */
13566       type = cp_parser_type_id (parser);
13567       /* Add it to the list.  */
13568       types = add_exception_specifier (types, type, /*complain=*/1);
13569       /* Peek at the next token.  */
13570       token = cp_lexer_peek_token (parser->lexer);
13571       /* If it is not a `,', we are done.  */
13572       if (token->type != CPP_COMMA)
13573         break;
13574       /* Consume the `,'.  */
13575       cp_lexer_consume_token (parser->lexer);
13576     }
13577
13578   return nreverse (types);
13579 }
13580
13581 /* Parse a try-block.
13582
13583    try-block:
13584      try compound-statement handler-seq  */
13585
13586 static tree
13587 cp_parser_try_block (cp_parser* parser)
13588 {
13589   tree try_block;
13590
13591   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13592   try_block = begin_try_block ();
13593   cp_parser_compound_statement (parser, NULL, true);
13594   finish_try_block (try_block);
13595   cp_parser_handler_seq (parser);
13596   finish_handler_sequence (try_block);
13597
13598   return try_block;
13599 }
13600
13601 /* Parse a function-try-block.
13602
13603    function-try-block:
13604      try ctor-initializer [opt] function-body handler-seq  */
13605
13606 static bool
13607 cp_parser_function_try_block (cp_parser* parser)
13608 {
13609   tree try_block;
13610   bool ctor_initializer_p;
13611
13612   /* Look for the `try' keyword.  */
13613   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13614     return false;
13615   /* Let the rest of the front-end know where we are.  */
13616   try_block = begin_function_try_block ();
13617   /* Parse the function-body.  */
13618   ctor_initializer_p
13619     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13620   /* We're done with the `try' part.  */
13621   finish_function_try_block (try_block);
13622   /* Parse the handlers.  */
13623   cp_parser_handler_seq (parser);
13624   /* We're done with the handlers.  */
13625   finish_function_handler_sequence (try_block);
13626
13627   return ctor_initializer_p;
13628 }
13629
13630 /* Parse a handler-seq.
13631
13632    handler-seq:
13633      handler handler-seq [opt]  */
13634
13635 static void
13636 cp_parser_handler_seq (cp_parser* parser)
13637 {
13638   while (true)
13639     {
13640       cp_token *token;
13641
13642       /* Parse the handler.  */
13643       cp_parser_handler (parser);
13644       /* Peek at the next token.  */
13645       token = cp_lexer_peek_token (parser->lexer);
13646       /* If it's not `catch' then there are no more handlers.  */
13647       if (!cp_parser_is_keyword (token, RID_CATCH))
13648         break;
13649     }
13650 }
13651
13652 /* Parse a handler.
13653
13654    handler:
13655      catch ( exception-declaration ) compound-statement  */
13656
13657 static void
13658 cp_parser_handler (cp_parser* parser)
13659 {
13660   tree handler;
13661   tree declaration;
13662
13663   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13664   handler = begin_handler ();
13665   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13666   declaration = cp_parser_exception_declaration (parser);
13667   finish_handler_parms (declaration, handler);
13668   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13669   cp_parser_compound_statement (parser, NULL, false);
13670   finish_handler (handler);
13671 }
13672
13673 /* Parse an exception-declaration.
13674
13675    exception-declaration:
13676      type-specifier-seq declarator
13677      type-specifier-seq abstract-declarator
13678      type-specifier-seq
13679      ...
13680
13681    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13682    ellipsis variant is used.  */
13683
13684 static tree
13685 cp_parser_exception_declaration (cp_parser* parser)
13686 {
13687   tree decl;
13688   cp_decl_specifier_seq type_specifiers;
13689   cp_declarator *declarator;
13690   const char *saved_message;
13691
13692   /* If it's an ellipsis, it's easy to handle.  */
13693   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13694     {
13695       /* Consume the `...' token.  */
13696       cp_lexer_consume_token (parser->lexer);
13697       return NULL_TREE;
13698     }
13699
13700   /* Types may not be defined in exception-declarations.  */
13701   saved_message = parser->type_definition_forbidden_message;
13702   parser->type_definition_forbidden_message
13703     = "types may not be defined in exception-declarations";
13704
13705   /* Parse the type-specifier-seq.  */
13706   cp_parser_type_specifier_seq (parser, &type_specifiers);
13707   /* If it's a `)', then there is no declarator.  */
13708   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13709     declarator = NULL;
13710   else
13711     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13712                                        /*ctor_dtor_or_conv_p=*/NULL,
13713                                        /*parenthesized_p=*/NULL);
13714
13715   /* Restore the saved message.  */
13716   parser->type_definition_forbidden_message = saved_message;
13717
13718   if (type_specifiers.any_specifiers_p)
13719     {
13720       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13721       if (decl == NULL_TREE)
13722         error ("invalid catch parameter");
13723     }
13724   else
13725     decl = NULL_TREE;
13726
13727   return decl;
13728 }
13729
13730 /* Parse a throw-expression.
13731
13732    throw-expression:
13733      throw assignment-expression [opt]
13734
13735    Returns a THROW_EXPR representing the throw-expression.  */
13736
13737 static tree
13738 cp_parser_throw_expression (cp_parser* parser)
13739 {
13740   tree expression;
13741   cp_token* token;
13742
13743   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13744   token = cp_lexer_peek_token (parser->lexer);
13745   /* Figure out whether or not there is an assignment-expression
13746      following the "throw" keyword.  */
13747   if (token->type == CPP_COMMA
13748       || token->type == CPP_SEMICOLON
13749       || token->type == CPP_CLOSE_PAREN
13750       || token->type == CPP_CLOSE_SQUARE
13751       || token->type == CPP_CLOSE_BRACE
13752       || token->type == CPP_COLON)
13753     expression = NULL_TREE;
13754   else
13755     expression = cp_parser_assignment_expression (parser);
13756
13757   return build_throw (expression);
13758 }
13759
13760 /* GNU Extensions */
13761
13762 /* Parse an (optional) asm-specification.
13763
13764    asm-specification:
13765      asm ( string-literal )
13766
13767    If the asm-specification is present, returns a STRING_CST
13768    corresponding to the string-literal.  Otherwise, returns
13769    NULL_TREE.  */
13770
13771 static tree
13772 cp_parser_asm_specification_opt (cp_parser* parser)
13773 {
13774   cp_token *token;
13775   tree asm_specification;
13776
13777   /* Peek at the next token.  */
13778   token = cp_lexer_peek_token (parser->lexer);
13779   /* If the next token isn't the `asm' keyword, then there's no
13780      asm-specification.  */
13781   if (!cp_parser_is_keyword (token, RID_ASM))
13782     return NULL_TREE;
13783
13784   /* Consume the `asm' token.  */
13785   cp_lexer_consume_token (parser->lexer);
13786   /* Look for the `('.  */
13787   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13788
13789   /* Look for the string-literal.  */
13790   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13791   if (token)
13792     asm_specification = token->value;
13793   else
13794     asm_specification = NULL_TREE;
13795
13796   /* Look for the `)'.  */
13797   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13798
13799   return asm_specification;
13800 }
13801
13802 /* Parse an asm-operand-list.
13803
13804    asm-operand-list:
13805      asm-operand
13806      asm-operand-list , asm-operand
13807
13808    asm-operand:
13809      string-literal ( expression )
13810      [ string-literal ] string-literal ( expression )
13811
13812    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13813    each node is the expression.  The TREE_PURPOSE is itself a
13814    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13815    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13816    is a STRING_CST for the string literal before the parenthesis.  */
13817
13818 static tree
13819 cp_parser_asm_operand_list (cp_parser* parser)
13820 {
13821   tree asm_operands = NULL_TREE;
13822
13823   while (true)
13824     {
13825       tree string_literal;
13826       tree expression;
13827       tree name;
13828       cp_token *token;
13829
13830       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13831         {
13832           /* Consume the `[' token.  */
13833           cp_lexer_consume_token (parser->lexer);
13834           /* Read the operand name.  */
13835           name = cp_parser_identifier (parser);
13836           if (name != error_mark_node)
13837             name = build_string (IDENTIFIER_LENGTH (name),
13838                                  IDENTIFIER_POINTER (name));
13839           /* Look for the closing `]'.  */
13840           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13841         }
13842       else
13843         name = NULL_TREE;
13844       /* Look for the string-literal.  */
13845       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13846       string_literal = token ? token->value : error_mark_node;
13847       c_lex_string_translate = 1;
13848       /* Look for the `('.  */
13849       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13850       /* Parse the expression.  */
13851       expression = cp_parser_expression (parser);
13852       /* Look for the `)'.  */
13853       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13854       c_lex_string_translate = 0;
13855       /* Add this operand to the list.  */
13856       asm_operands = tree_cons (build_tree_list (name, string_literal),
13857                                 expression,
13858                                 asm_operands);
13859       /* If the next token is not a `,', there are no more
13860          operands.  */
13861       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13862         break;
13863       /* Consume the `,'.  */
13864       cp_lexer_consume_token (parser->lexer);
13865     }
13866
13867   return nreverse (asm_operands);
13868 }
13869
13870 /* Parse an asm-clobber-list.
13871
13872    asm-clobber-list:
13873      string-literal
13874      asm-clobber-list , string-literal
13875
13876    Returns a TREE_LIST, indicating the clobbers in the order that they
13877    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13878
13879 static tree
13880 cp_parser_asm_clobber_list (cp_parser* parser)
13881 {
13882   tree clobbers = NULL_TREE;
13883
13884   while (true)
13885     {
13886       cp_token *token;
13887       tree string_literal;
13888
13889       /* Look for the string literal.  */
13890       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13891       string_literal = token ? token->value : error_mark_node;
13892       /* Add it to the list.  */
13893       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13894       /* If the next token is not a `,', then the list is
13895          complete.  */
13896       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13897         break;
13898       /* Consume the `,' token.  */
13899       cp_lexer_consume_token (parser->lexer);
13900     }
13901
13902   return clobbers;
13903 }
13904
13905 /* Parse an (optional) series of attributes.
13906
13907    attributes:
13908      attributes attribute
13909
13910    attribute:
13911      __attribute__ (( attribute-list [opt] ))
13912
13913    The return value is as for cp_parser_attribute_list.  */
13914
13915 static tree
13916 cp_parser_attributes_opt (cp_parser* parser)
13917 {
13918   tree attributes = NULL_TREE;
13919
13920   while (true)
13921     {
13922       cp_token *token;
13923       tree attribute_list;
13924
13925       /* Peek at the next token.  */
13926       token = cp_lexer_peek_token (parser->lexer);
13927       /* If it's not `__attribute__', then we're done.  */
13928       if (token->keyword != RID_ATTRIBUTE)
13929         break;
13930
13931       /* Consume the `__attribute__' keyword.  */
13932       cp_lexer_consume_token (parser->lexer);
13933       /* Look for the two `(' tokens.  */
13934       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13935       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13936
13937       /* Peek at the next token.  */
13938       token = cp_lexer_peek_token (parser->lexer);
13939       if (token->type != CPP_CLOSE_PAREN)
13940         /* Parse the attribute-list.  */
13941         attribute_list = cp_parser_attribute_list (parser);
13942       else
13943         /* If the next token is a `)', then there is no attribute
13944            list.  */
13945         attribute_list = NULL;
13946
13947       /* Look for the two `)' tokens.  */
13948       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13949       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13950
13951       /* Add these new attributes to the list.  */
13952       attributes = chainon (attributes, attribute_list);
13953     }
13954
13955   return attributes;
13956 }
13957
13958 /* Parse an attribute-list.
13959
13960    attribute-list:
13961      attribute
13962      attribute-list , attribute
13963
13964    attribute:
13965      identifier
13966      identifier ( identifier )
13967      identifier ( identifier , expression-list )
13968      identifier ( expression-list )
13969
13970    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13971    TREE_PURPOSE of each node is the identifier indicating which
13972    attribute is in use.  The TREE_VALUE represents the arguments, if
13973    any.  */
13974
13975 static tree
13976 cp_parser_attribute_list (cp_parser* parser)
13977 {
13978   tree attribute_list = NULL_TREE;
13979
13980   c_lex_string_translate = 0;
13981   while (true)
13982     {
13983       cp_token *token;
13984       tree identifier;
13985       tree attribute;
13986
13987       /* Look for the identifier.  We also allow keywords here; for
13988          example `__attribute__ ((const))' is legal.  */
13989       token = cp_lexer_peek_token (parser->lexer);
13990       if (token->type != CPP_NAME
13991           && token->type != CPP_KEYWORD)
13992         return error_mark_node;
13993       /* Consume the token.  */
13994       token = cp_lexer_consume_token (parser->lexer);
13995
13996       /* Save away the identifier that indicates which attribute this is.  */
13997       identifier = token->value;
13998       attribute = build_tree_list (identifier, NULL_TREE);
13999
14000       /* Peek at the next token.  */
14001       token = cp_lexer_peek_token (parser->lexer);
14002       /* If it's an `(', then parse the attribute arguments.  */
14003       if (token->type == CPP_OPEN_PAREN)
14004         {
14005           tree arguments;
14006
14007           arguments = (cp_parser_parenthesized_expression_list
14008                        (parser, true, /*non_constant_p=*/NULL));
14009           /* Save the identifier and arguments away.  */
14010           TREE_VALUE (attribute) = arguments;
14011         }
14012
14013       /* Add this attribute to the list.  */
14014       TREE_CHAIN (attribute) = attribute_list;
14015       attribute_list = attribute;
14016
14017       /* Now, look for more attributes.  */
14018       token = cp_lexer_peek_token (parser->lexer);
14019       /* If the next token isn't a `,', we're done.  */
14020       if (token->type != CPP_COMMA)
14021         break;
14022
14023       /* Consume the comma and keep going.  */
14024       cp_lexer_consume_token (parser->lexer);
14025     }
14026   c_lex_string_translate = 1;
14027
14028   /* We built up the list in reverse order.  */
14029   return nreverse (attribute_list);
14030 }
14031
14032 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14033    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14034    current value of the PEDANTIC flag, regardless of whether or not
14035    the `__extension__' keyword is present.  The caller is responsible
14036    for restoring the value of the PEDANTIC flag.  */
14037
14038 static bool
14039 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14040 {
14041   /* Save the old value of the PEDANTIC flag.  */
14042   *saved_pedantic = pedantic;
14043
14044   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14045     {
14046       /* Consume the `__extension__' token.  */
14047       cp_lexer_consume_token (parser->lexer);
14048       /* We're not being pedantic while the `__extension__' keyword is
14049          in effect.  */
14050       pedantic = 0;
14051
14052       return true;
14053     }
14054
14055   return false;
14056 }
14057
14058 /* Parse a label declaration.
14059
14060    label-declaration:
14061      __label__ label-declarator-seq ;
14062
14063    label-declarator-seq:
14064      identifier , label-declarator-seq
14065      identifier  */
14066
14067 static void
14068 cp_parser_label_declaration (cp_parser* parser)
14069 {
14070   /* Look for the `__label__' keyword.  */
14071   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14072
14073   while (true)
14074     {
14075       tree identifier;
14076
14077       /* Look for an identifier.  */
14078       identifier = cp_parser_identifier (parser);
14079       /* Declare it as a lobel.  */
14080       finish_label_decl (identifier);
14081       /* If the next token is a `;', stop.  */
14082       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14083         break;
14084       /* Look for the `,' separating the label declarations.  */
14085       cp_parser_require (parser, CPP_COMMA, "`,'");
14086     }
14087
14088   /* Look for the final `;'.  */
14089   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14090 }
14091
14092 /* Support Functions */
14093
14094 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14095    NAME should have one of the representations used for an
14096    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14097    is returned.  If PARSER->SCOPE is a dependent type, then a
14098    SCOPE_REF is returned.
14099
14100    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14101    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14102    was formed.  Abstractly, such entities should not be passed to this
14103    function, because they do not need to be looked up, but it is
14104    simpler to check for this special case here, rather than at the
14105    call-sites.
14106
14107    In cases not explicitly covered above, this function returns a
14108    DECL, OVERLOAD, or baselink representing the result of the lookup.
14109    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14110    is returned.
14111
14112    If IS_TYPE is TRUE, bindings that do not refer to types are
14113    ignored.
14114
14115    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14116    ignored.
14117
14118    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14119    are ignored.
14120
14121    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14122    types.  */
14123
14124 static tree
14125 cp_parser_lookup_name (cp_parser *parser, tree name,
14126                        bool is_type, bool is_template, bool is_namespace,
14127                        bool check_dependency)
14128 {
14129   tree decl;
14130   tree object_type = parser->context->object_type;
14131
14132   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14133      no longer valid.  Note that if we are parsing tentatively, and
14134      the parse fails, OBJECT_TYPE will be automatically restored.  */
14135   parser->context->object_type = NULL_TREE;
14136
14137   if (name == error_mark_node)
14138     return error_mark_node;
14139
14140   /* A template-id has already been resolved; there is no lookup to
14141      do.  */
14142   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14143     return name;
14144   if (BASELINK_P (name))
14145     {
14146       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
14147                            == TEMPLATE_ID_EXPR),
14148                           20020909);
14149       return name;
14150     }
14151
14152   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14153      it should already have been checked to make sure that the name
14154      used matches the type being destroyed.  */
14155   if (TREE_CODE (name) == BIT_NOT_EXPR)
14156     {
14157       tree type;
14158
14159       /* Figure out to which type this destructor applies.  */
14160       if (parser->scope)
14161         type = parser->scope;
14162       else if (object_type)
14163         type = object_type;
14164       else
14165         type = current_class_type;
14166       /* If that's not a class type, there is no destructor.  */
14167       if (!type || !CLASS_TYPE_P (type))
14168         return error_mark_node;
14169       if (!CLASSTYPE_DESTRUCTORS (type))
14170           return error_mark_node;
14171       /* If it was a class type, return the destructor.  */
14172       return CLASSTYPE_DESTRUCTORS (type);
14173     }
14174
14175   /* By this point, the NAME should be an ordinary identifier.  If
14176      the id-expression was a qualified name, the qualifying scope is
14177      stored in PARSER->SCOPE at this point.  */
14178   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
14179                       20000619);
14180
14181   /* Perform the lookup.  */
14182   if (parser->scope)
14183     {
14184       bool dependent_p;
14185
14186       if (parser->scope == error_mark_node)
14187         return error_mark_node;
14188
14189       /* If the SCOPE is dependent, the lookup must be deferred until
14190          the template is instantiated -- unless we are explicitly
14191          looking up names in uninstantiated templates.  Even then, we
14192          cannot look up the name if the scope is not a class type; it
14193          might, for example, be a template type parameter.  */
14194       dependent_p = (TYPE_P (parser->scope)
14195                      && !(parser->in_declarator_p
14196                           && currently_open_class (parser->scope))
14197                      && dependent_type_p (parser->scope));
14198       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14199            && dependent_p)
14200         {
14201           if (is_type)
14202             /* The resolution to Core Issue 180 says that `struct A::B'
14203                should be considered a type-name, even if `A' is
14204                dependent.  */
14205             decl = TYPE_NAME (make_typename_type (parser->scope,
14206                                                   name,
14207                                                   /*complain=*/1));
14208           else if (is_template)
14209             decl = make_unbound_class_template (parser->scope,
14210                                                 name,
14211                                                 /*complain=*/1);
14212           else
14213             decl = build_nt (SCOPE_REF, parser->scope, name);
14214         }
14215       else
14216         {
14217           bool pop_p = false;
14218
14219           /* If PARSER->SCOPE is a dependent type, then it must be a
14220              class type, and we must not be checking dependencies;
14221              otherwise, we would have processed this lookup above.  So
14222              that PARSER->SCOPE is not considered a dependent base by
14223              lookup_member, we must enter the scope here.  */
14224           if (dependent_p)
14225             pop_p = push_scope (parser->scope);
14226           /* If the PARSER->SCOPE is a a template specialization, it
14227              may be instantiated during name lookup.  In that case,
14228              errors may be issued.  Even if we rollback the current
14229              tentative parse, those errors are valid.  */
14230           decl = lookup_qualified_name (parser->scope, name, is_type,
14231                                         /*complain=*/true);
14232           if (pop_p)
14233             pop_scope (parser->scope);
14234         }
14235       parser->qualifying_scope = parser->scope;
14236       parser->object_scope = NULL_TREE;
14237     }
14238   else if (object_type)
14239     {
14240       tree object_decl = NULL_TREE;
14241       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14242          OBJECT_TYPE is not a class.  */
14243       if (CLASS_TYPE_P (object_type))
14244         /* If the OBJECT_TYPE is a template specialization, it may
14245            be instantiated during name lookup.  In that case, errors
14246            may be issued.  Even if we rollback the current tentative
14247            parse, those errors are valid.  */
14248         object_decl = lookup_member (object_type,
14249                                      name,
14250                                      /*protect=*/0, is_type);
14251       /* Look it up in the enclosing context, too.  */
14252       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14253                                is_namespace,
14254                                /*flags=*/0);
14255       parser->object_scope = object_type;
14256       parser->qualifying_scope = NULL_TREE;
14257       if (object_decl)
14258         decl = object_decl;
14259     }
14260   else
14261     {
14262       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14263                                is_namespace,
14264                                /*flags=*/0);
14265       parser->qualifying_scope = NULL_TREE;
14266       parser->object_scope = NULL_TREE;
14267     }
14268
14269   /* If the lookup failed, let our caller know.  */
14270   if (!decl
14271       || decl == error_mark_node
14272       || (TREE_CODE (decl) == FUNCTION_DECL
14273           && DECL_ANTICIPATED (decl)))
14274     return error_mark_node;
14275
14276   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14277   if (TREE_CODE (decl) == TREE_LIST)
14278     {
14279       /* The error message we have to print is too complicated for
14280          cp_parser_error, so we incorporate its actions directly.  */
14281       if (!cp_parser_simulate_error (parser))
14282         {
14283           error ("reference to `%D' is ambiguous", name);
14284           print_candidates (decl);
14285         }
14286       return error_mark_node;
14287     }
14288
14289   my_friendly_assert (DECL_P (decl)
14290                       || TREE_CODE (decl) == OVERLOAD
14291                       || TREE_CODE (decl) == SCOPE_REF
14292                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14293                       || BASELINK_P (decl),
14294                       20000619);
14295
14296   /* If we have resolved the name of a member declaration, check to
14297      see if the declaration is accessible.  When the name resolves to
14298      set of overloaded functions, accessibility is checked when
14299      overload resolution is done.
14300
14301      During an explicit instantiation, access is not checked at all,
14302      as per [temp.explicit].  */
14303   if (DECL_P (decl))
14304     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14305
14306   return decl;
14307 }
14308
14309 /* Like cp_parser_lookup_name, but for use in the typical case where
14310    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14311    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14312
14313 static tree
14314 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14315 {
14316   return cp_parser_lookup_name (parser, name,
14317                                 /*is_type=*/false,
14318                                 /*is_template=*/false,
14319                                 /*is_namespace=*/false,
14320                                 /*check_dependency=*/true);
14321 }
14322
14323 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14324    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14325    true, the DECL indicates the class being defined in a class-head,
14326    or declared in an elaborated-type-specifier.
14327
14328    Otherwise, return DECL.  */
14329
14330 static tree
14331 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14332 {
14333   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14334      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14335
14336        struct A {
14337          template <typename T> struct B;
14338        };
14339
14340        template <typename T> struct A::B {};
14341
14342      Similarly, in a elaborated-type-specifier:
14343
14344        namespace N { struct X{}; }
14345
14346        struct A {
14347          template <typename T> friend struct N::X;
14348        };
14349
14350      However, if the DECL refers to a class type, and we are in
14351      the scope of the class, then the name lookup automatically
14352      finds the TYPE_DECL created by build_self_reference rather
14353      than a TEMPLATE_DECL.  For example, in:
14354
14355        template <class T> struct S {
14356          S s;
14357        };
14358
14359      there is no need to handle such case.  */
14360
14361   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14362     return DECL_TEMPLATE_RESULT (decl);
14363
14364   return decl;
14365 }
14366
14367 /* If too many, or too few, template-parameter lists apply to the
14368    declarator, issue an error message.  Returns TRUE if all went well,
14369    and FALSE otherwise.  */
14370
14371 static bool
14372 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14373                                                 cp_declarator *declarator)
14374 {
14375   unsigned num_templates;
14376
14377   /* We haven't seen any classes that involve template parameters yet.  */
14378   num_templates = 0;
14379
14380   switch (declarator->kind)
14381     {
14382     case cdk_id:
14383       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14384         {
14385           tree scope;
14386           tree member;
14387
14388           scope = TREE_OPERAND (declarator->u.id.name, 0);
14389           member = TREE_OPERAND (declarator->u.id.name, 1);
14390
14391           while (scope && CLASS_TYPE_P (scope))
14392             {
14393               /* You're supposed to have one `template <...>'
14394                  for every template class, but you don't need one
14395                  for a full specialization.  For example:
14396
14397                  template <class T> struct S{};
14398                  template <> struct S<int> { void f(); };
14399                  void S<int>::f () {}
14400
14401                  is correct; there shouldn't be a `template <>' for
14402                  the definition of `S<int>::f'.  */
14403               if (CLASSTYPE_TEMPLATE_INFO (scope)
14404                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14405                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14406                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14407                 ++num_templates;
14408
14409               scope = TYPE_CONTEXT (scope);
14410             }
14411         }
14412
14413       /* If the DECLARATOR has the form `X<y>' then it uses one
14414          additional level of template parameters.  */
14415       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14416         ++num_templates;
14417
14418       return cp_parser_check_template_parameters (parser,
14419                                                   num_templates);
14420
14421     case cdk_function:
14422     case cdk_array:
14423     case cdk_pointer:
14424     case cdk_reference:
14425     case cdk_ptrmem:
14426       return (cp_parser_check_declarator_template_parameters 
14427               (parser, declarator->declarator));
14428
14429     case cdk_error:
14430       return true;
14431
14432     default:
14433       abort ();
14434       return false;
14435     }
14436 }
14437
14438 /* NUM_TEMPLATES were used in the current declaration.  If that is
14439    invalid, return FALSE and issue an error messages.  Otherwise,
14440    return TRUE.  */
14441
14442 static bool
14443 cp_parser_check_template_parameters (cp_parser* parser,
14444                                      unsigned num_templates)
14445 {
14446   /* If there are more template classes than parameter lists, we have
14447      something like:
14448
14449        template <class T> void S<T>::R<T>::f ();  */
14450   if (parser->num_template_parameter_lists < num_templates)
14451     {
14452       error ("too few template-parameter-lists");
14453       return false;
14454     }
14455   /* If there are the same number of template classes and parameter
14456      lists, that's OK.  */
14457   if (parser->num_template_parameter_lists == num_templates)
14458     return true;
14459   /* If there are more, but only one more, then we are referring to a
14460      member template.  That's OK too.  */
14461   if (parser->num_template_parameter_lists == num_templates + 1)
14462       return true;
14463   /* Otherwise, there are too many template parameter lists.  We have
14464      something like:
14465
14466      template <class T> template <class U> void S::f();  */
14467   error ("too many template-parameter-lists");
14468   return false;
14469 }
14470
14471 /* Parse a binary-expression of the general form:
14472
14473    binary-expression:
14474      <expr>
14475      binary-expression <token> <expr>
14476
14477    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14478    to parser the <expr>s.  If the first production is used, then the
14479    value returned by FN is returned directly.  Otherwise, a node with
14480    the indicated EXPR_TYPE is returned, with operands corresponding to
14481    the two sub-expressions.  */
14482
14483 static tree
14484 cp_parser_binary_expression (cp_parser* parser,
14485                              const cp_parser_token_tree_map token_tree_map,
14486                              cp_parser_expression_fn fn)
14487 {
14488   tree lhs;
14489
14490   /* Parse the first expression.  */
14491   lhs = (*fn) (parser);
14492   /* Now, look for more expressions.  */
14493   while (true)
14494     {
14495       cp_token *token;
14496       const cp_parser_token_tree_map_node *map_node;
14497       tree rhs;
14498
14499       /* Peek at the next token.  */
14500       token = cp_lexer_peek_token (parser->lexer);
14501       /* If the token is `>', and that's not an operator at the
14502          moment, then we're done.  */
14503       if (token->type == CPP_GREATER
14504           && !parser->greater_than_is_operator_p)
14505         break;
14506       /* If we find one of the tokens we want, build the corresponding
14507          tree representation.  */
14508       for (map_node = token_tree_map;
14509            map_node->token_type != CPP_EOF;
14510            ++map_node)
14511         if (map_node->token_type == token->type)
14512           {
14513             /* Assume that an overloaded operator will not be used.  */
14514             bool overloaded_p = false;
14515
14516             /* Consume the operator token.  */
14517             cp_lexer_consume_token (parser->lexer);
14518             /* Parse the right-hand side of the expression.  */
14519             rhs = (*fn) (parser);
14520             /* Build the binary tree node.  */
14521             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs, 
14522                                      &overloaded_p);
14523             /* If the binary operator required the use of an
14524                overloaded operator, then this expression cannot be an
14525                integral constant-expression.  An overloaded operator
14526                can be used even if both operands are otherwise
14527                permissible in an integral constant-expression if at
14528                least one of the operands is of enumeration type.  */
14529             if (overloaded_p
14530                 && (cp_parser_non_integral_constant_expression 
14531                     (parser, "calls to overloaded operators")))
14532               lhs = error_mark_node;
14533             break;
14534           }
14535
14536       /* If the token wasn't one of the ones we want, we're done.  */
14537       if (map_node->token_type == CPP_EOF)
14538         break;
14539     }
14540
14541   return lhs;
14542 }
14543
14544 /* Parse an optional `::' token indicating that the following name is
14545    from the global namespace.  If so, PARSER->SCOPE is set to the
14546    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14547    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14548    Returns the new value of PARSER->SCOPE, if the `::' token is
14549    present, and NULL_TREE otherwise.  */
14550
14551 static tree
14552 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14553 {
14554   cp_token *token;
14555
14556   /* Peek at the next token.  */
14557   token = cp_lexer_peek_token (parser->lexer);
14558   /* If we're looking at a `::' token then we're starting from the
14559      global namespace, not our current location.  */
14560   if (token->type == CPP_SCOPE)
14561     {
14562       /* Consume the `::' token.  */
14563       cp_lexer_consume_token (parser->lexer);
14564       /* Set the SCOPE so that we know where to start the lookup.  */
14565       parser->scope = global_namespace;
14566       parser->qualifying_scope = global_namespace;
14567       parser->object_scope = NULL_TREE;
14568
14569       return parser->scope;
14570     }
14571   else if (!current_scope_valid_p)
14572     {
14573       parser->scope = NULL_TREE;
14574       parser->qualifying_scope = NULL_TREE;
14575       parser->object_scope = NULL_TREE;
14576     }
14577
14578   return NULL_TREE;
14579 }
14580
14581 /* Returns TRUE if the upcoming token sequence is the start of a
14582    constructor declarator.  If FRIEND_P is true, the declarator is
14583    preceded by the `friend' specifier.  */
14584
14585 static bool
14586 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14587 {
14588   bool constructor_p;
14589   tree type_decl = NULL_TREE;
14590   bool nested_name_p;
14591   cp_token *next_token;
14592
14593   /* The common case is that this is not a constructor declarator, so
14594      try to avoid doing lots of work if at all possible.  It's not
14595      valid declare a constructor at function scope.  */
14596   if (at_function_scope_p ())
14597     return false;
14598   /* And only certain tokens can begin a constructor declarator.  */
14599   next_token = cp_lexer_peek_token (parser->lexer);
14600   if (next_token->type != CPP_NAME
14601       && next_token->type != CPP_SCOPE
14602       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14603       && next_token->type != CPP_TEMPLATE_ID)
14604     return false;
14605
14606   /* Parse tentatively; we are going to roll back all of the tokens
14607      consumed here.  */
14608   cp_parser_parse_tentatively (parser);
14609   /* Assume that we are looking at a constructor declarator.  */
14610   constructor_p = true;
14611
14612   /* Look for the optional `::' operator.  */
14613   cp_parser_global_scope_opt (parser,
14614                               /*current_scope_valid_p=*/false);
14615   /* Look for the nested-name-specifier.  */
14616   nested_name_p
14617     = (cp_parser_nested_name_specifier_opt (parser,
14618                                             /*typename_keyword_p=*/false,
14619                                             /*check_dependency_p=*/false,
14620                                             /*type_p=*/false,
14621                                             /*is_declaration=*/false)
14622        != NULL_TREE);
14623   /* Outside of a class-specifier, there must be a
14624      nested-name-specifier.  */
14625   if (!nested_name_p &&
14626       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14627        || friend_p))
14628     constructor_p = false;
14629   /* If we still think that this might be a constructor-declarator,
14630      look for a class-name.  */
14631   if (constructor_p)
14632     {
14633       /* If we have:
14634
14635            template <typename T> struct S { S(); };
14636            template <typename T> S<T>::S ();
14637
14638          we must recognize that the nested `S' names a class.
14639          Similarly, for:
14640
14641            template <typename T> S<T>::S<T> ();
14642
14643          we must recognize that the nested `S' names a template.  */
14644       type_decl = cp_parser_class_name (parser,
14645                                         /*typename_keyword_p=*/false,
14646                                         /*template_keyword_p=*/false,
14647                                         /*type_p=*/false,
14648                                         /*check_dependency_p=*/false,
14649                                         /*class_head_p=*/false,
14650                                         /*is_declaration=*/false);
14651       /* If there was no class-name, then this is not a constructor.  */
14652       constructor_p = !cp_parser_error_occurred (parser);
14653     }
14654
14655   /* If we're still considering a constructor, we have to see a `(',
14656      to begin the parameter-declaration-clause, followed by either a
14657      `)', an `...', or a decl-specifier.  We need to check for a
14658      type-specifier to avoid being fooled into thinking that:
14659
14660        S::S (f) (int);
14661
14662      is a constructor.  (It is actually a function named `f' that
14663      takes one parameter (of type `int') and returns a value of type
14664      `S::S'.  */
14665   if (constructor_p
14666       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14667     {
14668       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14669           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14670           /* A parameter declaration begins with a decl-specifier,
14671              which is either the "attribute" keyword, a storage class
14672              specifier, or (usually) a type-specifier.  */
14673           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14674           && !cp_parser_storage_class_specifier_opt (parser))
14675         {
14676           tree type;
14677           bool pop_p = false;
14678           unsigned saved_num_template_parameter_lists;
14679
14680           /* Names appearing in the type-specifier should be looked up
14681              in the scope of the class.  */
14682           if (current_class_type)
14683             type = NULL_TREE;
14684           else
14685             {
14686               type = TREE_TYPE (type_decl);
14687               if (TREE_CODE (type) == TYPENAME_TYPE)
14688                 {
14689                   type = resolve_typename_type (type,
14690                                                 /*only_current_p=*/false);
14691                   if (type == error_mark_node)
14692                     {
14693                       cp_parser_abort_tentative_parse (parser);
14694                       return false;
14695                     }
14696                 }
14697               pop_p = push_scope (type);
14698             }
14699
14700           /* Inside the constructor parameter list, surrounding
14701              template-parameter-lists do not apply.  */
14702           saved_num_template_parameter_lists
14703             = parser->num_template_parameter_lists;
14704           parser->num_template_parameter_lists = 0;
14705
14706           /* Look for the type-specifier.  */
14707           cp_parser_type_specifier (parser,
14708                                     CP_PARSER_FLAGS_NONE,
14709                                     /*decl_specs=*/NULL,
14710                                     /*is_declarator=*/true,
14711                                     /*declares_class_or_enum=*/NULL,
14712                                     /*is_cv_qualifier=*/NULL);
14713
14714           parser->num_template_parameter_lists
14715             = saved_num_template_parameter_lists;
14716
14717           /* Leave the scope of the class.  */
14718           if (pop_p)
14719             pop_scope (type);
14720
14721           constructor_p = !cp_parser_error_occurred (parser);
14722         }
14723     }
14724   else
14725     constructor_p = false;
14726   /* We did not really want to consume any tokens.  */
14727   cp_parser_abort_tentative_parse (parser);
14728
14729   return constructor_p;
14730 }
14731
14732 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14733    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14734    they must be performed once we are in the scope of the function.
14735
14736    Returns the function defined.  */
14737
14738 static tree
14739 cp_parser_function_definition_from_specifiers_and_declarator
14740   (cp_parser* parser,
14741    cp_decl_specifier_seq *decl_specifiers,
14742    tree attributes,
14743    const cp_declarator *declarator)
14744 {
14745   tree fn;
14746   bool success_p;
14747
14748   /* Begin the function-definition.  */
14749   success_p = start_function (decl_specifiers, declarator, attributes);
14750
14751   /* The things we're about to see are not directly qualified by any
14752      template headers we've seen thus far.  */
14753   reset_specialization ();
14754
14755   /* If there were names looked up in the decl-specifier-seq that we
14756      did not check, check them now.  We must wait until we are in the
14757      scope of the function to perform the checks, since the function
14758      might be a friend.  */
14759   perform_deferred_access_checks ();
14760
14761   if (!success_p)
14762     {
14763       /* Skip the entire function.  */
14764       error ("invalid function declaration");
14765       cp_parser_skip_to_end_of_block_or_statement (parser);
14766       fn = error_mark_node;
14767     }
14768   else
14769     fn = cp_parser_function_definition_after_declarator (parser,
14770                                                          /*inline_p=*/false);
14771
14772   return fn;
14773 }
14774
14775 /* Parse the part of a function-definition that follows the
14776    declarator.  INLINE_P is TRUE iff this function is an inline
14777    function defined with a class-specifier.
14778
14779    Returns the function defined.  */
14780
14781 static tree
14782 cp_parser_function_definition_after_declarator (cp_parser* parser,
14783                                                 bool inline_p)
14784 {
14785   tree fn;
14786   bool ctor_initializer_p = false;
14787   bool saved_in_unbraced_linkage_specification_p;
14788   unsigned saved_num_template_parameter_lists;
14789
14790   /* If the next token is `return', then the code may be trying to
14791      make use of the "named return value" extension that G++ used to
14792      support.  */
14793   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14794     {
14795       /* Consume the `return' keyword.  */
14796       cp_lexer_consume_token (parser->lexer);
14797       /* Look for the identifier that indicates what value is to be
14798          returned.  */
14799       cp_parser_identifier (parser);
14800       /* Issue an error message.  */
14801       error ("named return values are no longer supported");
14802       /* Skip tokens until we reach the start of the function body.  */
14803       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14804              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14805         cp_lexer_consume_token (parser->lexer);
14806     }
14807   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14808      anything declared inside `f'.  */
14809   saved_in_unbraced_linkage_specification_p
14810     = parser->in_unbraced_linkage_specification_p;
14811   parser->in_unbraced_linkage_specification_p = false;
14812   /* Inside the function, surrounding template-parameter-lists do not
14813      apply.  */
14814   saved_num_template_parameter_lists
14815     = parser->num_template_parameter_lists;
14816   parser->num_template_parameter_lists = 0;
14817   /* If the next token is `try', then we are looking at a
14818      function-try-block.  */
14819   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14820     ctor_initializer_p = cp_parser_function_try_block (parser);
14821   /* A function-try-block includes the function-body, so we only do
14822      this next part if we're not processing a function-try-block.  */
14823   else
14824     ctor_initializer_p
14825       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14826
14827   /* Finish the function.  */
14828   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14829                         (inline_p ? 2 : 0));
14830   /* Generate code for it, if necessary.  */
14831   expand_or_defer_fn (fn);
14832   /* Restore the saved values.  */
14833   parser->in_unbraced_linkage_specification_p
14834     = saved_in_unbraced_linkage_specification_p;
14835   parser->num_template_parameter_lists
14836     = saved_num_template_parameter_lists;
14837
14838   return fn;
14839 }
14840
14841 /* Parse a template-declaration, assuming that the `export' (and
14842    `extern') keywords, if present, has already been scanned.  MEMBER_P
14843    is as for cp_parser_template_declaration.  */
14844
14845 static void
14846 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14847 {
14848   tree decl = NULL_TREE;
14849   tree parameter_list;
14850   bool friend_p = false;
14851
14852   /* Look for the `template' keyword.  */
14853   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14854     return;
14855
14856   /* And the `<'.  */
14857   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14858     return;
14859
14860   /* If the next token is `>', then we have an invalid
14861      specialization.  Rather than complain about an invalid template
14862      parameter, issue an error message here.  */
14863   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14864     {
14865       cp_parser_error (parser, "invalid explicit specialization");
14866       begin_specialization ();
14867       parameter_list = NULL_TREE;
14868     }
14869   else
14870     {
14871       /* Parse the template parameters.  */
14872       begin_template_parm_list ();
14873       parameter_list = cp_parser_template_parameter_list (parser);
14874       parameter_list = end_template_parm_list (parameter_list);
14875     }
14876
14877   /* Look for the `>'.  */
14878   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14879   /* We just processed one more parameter list.  */
14880   ++parser->num_template_parameter_lists;
14881   /* If the next token is `template', there are more template
14882      parameters.  */
14883   if (cp_lexer_next_token_is_keyword (parser->lexer,
14884                                       RID_TEMPLATE))
14885     cp_parser_template_declaration_after_export (parser, member_p);
14886   else
14887     {
14888       decl = cp_parser_single_declaration (parser,
14889                                            member_p,
14890                                            &friend_p);
14891
14892       /* If this is a member template declaration, let the front
14893          end know.  */
14894       if (member_p && !friend_p && decl)
14895         {
14896           if (TREE_CODE (decl) == TYPE_DECL)
14897             cp_parser_check_access_in_redeclaration (decl);
14898
14899           decl = finish_member_template_decl (decl);
14900         }
14901       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14902         make_friend_class (current_class_type, TREE_TYPE (decl),
14903                            /*complain=*/true);
14904     }
14905   /* We are done with the current parameter list.  */
14906   --parser->num_template_parameter_lists;
14907
14908   /* Finish up.  */
14909   finish_template_decl (parameter_list);
14910
14911   /* Register member declarations.  */
14912   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14913     finish_member_declaration (decl);
14914
14915   /* If DECL is a function template, we must return to parse it later.
14916      (Even though there is no definition, there might be default
14917      arguments that need handling.)  */
14918   if (member_p && decl
14919       && (TREE_CODE (decl) == FUNCTION_DECL
14920           || DECL_FUNCTION_TEMPLATE_P (decl)))
14921     TREE_VALUE (parser->unparsed_functions_queues)
14922       = tree_cons (NULL_TREE, decl,
14923                    TREE_VALUE (parser->unparsed_functions_queues));
14924 }
14925
14926 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14927    `function-definition' sequence.  MEMBER_P is true, this declaration
14928    appears in a class scope.
14929
14930    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14931    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14932
14933 static tree
14934 cp_parser_single_declaration (cp_parser* parser,
14935                               bool member_p,
14936                               bool* friend_p)
14937 {
14938   int declares_class_or_enum;
14939   tree decl = NULL_TREE;
14940   cp_decl_specifier_seq decl_specifiers;
14941   bool function_definition_p = false;
14942
14943   /* Defer access checks until we know what is being declared.  */
14944   push_deferring_access_checks (dk_deferred);
14945
14946   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14947      alternative.  */
14948   cp_parser_decl_specifier_seq (parser,
14949                                 CP_PARSER_FLAGS_OPTIONAL,
14950                                 &decl_specifiers,
14951                                 &declares_class_or_enum);
14952   if (friend_p)
14953     *friend_p = cp_parser_friend_p (&decl_specifiers);
14954   /* Gather up the access checks that occurred the
14955      decl-specifier-seq.  */
14956   stop_deferring_access_checks ();
14957
14958   /* Check for the declaration of a template class.  */
14959   if (declares_class_or_enum)
14960     {
14961       if (cp_parser_declares_only_class_p (parser))
14962         {
14963           decl = shadow_tag (&decl_specifiers);
14964           if (decl && decl != error_mark_node)
14965             decl = TYPE_NAME (decl);
14966           else
14967             decl = error_mark_node;
14968         }
14969     }
14970   else
14971     decl = NULL_TREE;
14972   /* If it's not a template class, try for a template function.  If
14973      the next token is a `;', then this declaration does not declare
14974      anything.  But, if there were errors in the decl-specifiers, then
14975      the error might well have come from an attempted class-specifier.
14976      In that case, there's no need to warn about a missing declarator.  */
14977   if (!decl
14978       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14979           || decl_specifiers.type != error_mark_node))
14980     decl = cp_parser_init_declarator (parser,
14981                                       &decl_specifiers,
14982                                       /*function_definition_allowed_p=*/true,
14983                                       member_p,
14984                                       declares_class_or_enum,
14985                                       &function_definition_p);
14986
14987   pop_deferring_access_checks ();
14988
14989   /* Clear any current qualification; whatever comes next is the start
14990      of something new.  */
14991   parser->scope = NULL_TREE;
14992   parser->qualifying_scope = NULL_TREE;
14993   parser->object_scope = NULL_TREE;
14994   /* Look for a trailing `;' after the declaration.  */
14995   if (!function_definition_p
14996       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14997     cp_parser_skip_to_end_of_block_or_statement (parser);
14998
14999   return decl;
15000 }
15001
15002 /* Parse a cast-expression that is not the operand of a unary "&".  */
15003
15004 static tree
15005 cp_parser_simple_cast_expression (cp_parser *parser)
15006 {
15007   return cp_parser_cast_expression (parser, /*address_p=*/false);
15008 }
15009
15010 /* Parse a functional cast to TYPE.  Returns an expression
15011    representing the cast.  */
15012
15013 static tree
15014 cp_parser_functional_cast (cp_parser* parser, tree type)
15015 {
15016   tree expression_list;
15017   tree cast;
15018
15019   expression_list
15020     = cp_parser_parenthesized_expression_list (parser, false,
15021                                                /*non_constant_p=*/NULL);
15022
15023   cast = build_functional_cast (type, expression_list);
15024   /* [expr.const]/1: In an integral constant expression "only type
15025      conversions to integral or enumeration type can be used".  */
15026   if (cast != error_mark_node && !type_dependent_expression_p (type) 
15027       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15028     {
15029       if (cp_parser_non_integral_constant_expression 
15030           (parser, "a call to a constructor"))
15031         return error_mark_node;
15032     }
15033   return cast;
15034 }
15035
15036 /* Save the tokens that make up the body of a member function defined
15037    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15038    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15039    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15040    for the member function.  */
15041
15042 static tree
15043 cp_parser_save_member_function_body (cp_parser* parser,
15044                                      cp_decl_specifier_seq *decl_specifiers,
15045                                      cp_declarator *declarator,
15046                                      tree attributes)
15047 {
15048   cp_token_cache *cache;
15049   tree fn;
15050
15051   /* Create the function-declaration.  */
15052   fn = start_method (decl_specifiers, declarator, attributes);
15053   /* If something went badly wrong, bail out now.  */
15054   if (fn == error_mark_node)
15055     {
15056       /* If there's a function-body, skip it.  */
15057       if (cp_parser_token_starts_function_definition_p
15058           (cp_lexer_peek_token (parser->lexer)))
15059         cp_parser_skip_to_end_of_block_or_statement (parser);
15060       return error_mark_node;
15061     }
15062
15063   /* Remember it, if there default args to post process.  */
15064   cp_parser_save_default_args (parser, fn);
15065
15066   /* Create a token cache.  */
15067   cache = cp_token_cache_new ();
15068   /* Save away the tokens that make up the body of the
15069      function.  */
15070   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15071   /* Handle function try blocks.  */
15072   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15073     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15074
15075   /* Save away the inline definition; we will process it when the
15076      class is complete.  */
15077   DECL_PENDING_INLINE_INFO (fn) = cache;
15078   DECL_PENDING_INLINE_P (fn) = 1;
15079
15080   /* We need to know that this was defined in the class, so that
15081      friend templates are handled correctly.  */
15082   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15083
15084   /* We're done with the inline definition.  */
15085   finish_method (fn);
15086
15087   /* Add FN to the queue of functions to be parsed later.  */
15088   TREE_VALUE (parser->unparsed_functions_queues)
15089     = tree_cons (NULL_TREE, fn,
15090                  TREE_VALUE (parser->unparsed_functions_queues));
15091
15092   return fn;
15093 }
15094
15095 /* Parse a template-argument-list, as well as the trailing ">" (but
15096    not the opening ">").  See cp_parser_template_argument_list for the
15097    return value.  */
15098
15099 static tree
15100 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15101 {
15102   tree arguments;
15103   tree saved_scope;
15104   tree saved_qualifying_scope;
15105   tree saved_object_scope;
15106   bool saved_greater_than_is_operator_p;
15107
15108   /* [temp.names]
15109
15110      When parsing a template-id, the first non-nested `>' is taken as
15111      the end of the template-argument-list rather than a greater-than
15112      operator.  */
15113   saved_greater_than_is_operator_p
15114     = parser->greater_than_is_operator_p;
15115   parser->greater_than_is_operator_p = false;
15116   /* Parsing the argument list may modify SCOPE, so we save it
15117      here.  */
15118   saved_scope = parser->scope;
15119   saved_qualifying_scope = parser->qualifying_scope;
15120   saved_object_scope = parser->object_scope;
15121   /* Parse the template-argument-list itself.  */
15122   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15123     arguments = NULL_TREE;
15124   else
15125     arguments = cp_parser_template_argument_list (parser);
15126   /* Look for the `>' that ends the template-argument-list. If we find
15127      a '>>' instead, it's probably just a typo.  */
15128   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15129     {
15130       if (!saved_greater_than_is_operator_p)
15131         {
15132           /* If we're in a nested template argument list, the '>>' has to be
15133             a typo for '> >'. We emit the error message, but we continue
15134             parsing and we push a '>' as next token, so that the argument
15135             list will be parsed correctly..  */
15136           cp_token* token;
15137           error ("`>>' should be `> >' within a nested template argument list");
15138           token = cp_lexer_peek_token (parser->lexer);
15139           token->type = CPP_GREATER;
15140         }
15141       else
15142         {
15143           /* If this is not a nested template argument list, the '>>' is
15144             a typo for '>'. Emit an error message and continue.  */
15145           error ("spurious `>>', use `>' to terminate a template argument list");
15146           cp_lexer_consume_token (parser->lexer);
15147         }
15148     }
15149   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15150     error ("missing `>' to terminate the template argument list");
15151   /* The `>' token might be a greater-than operator again now.  */
15152   parser->greater_than_is_operator_p
15153     = saved_greater_than_is_operator_p;
15154   /* Restore the SAVED_SCOPE.  */
15155   parser->scope = saved_scope;
15156   parser->qualifying_scope = saved_qualifying_scope;
15157   parser->object_scope = saved_object_scope;
15158
15159   return arguments;
15160 }
15161
15162 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15163    arguments, or the body of the function have not yet been parsed,
15164    parse them now.  */
15165
15166 static void
15167 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15168 {
15169   cp_lexer *saved_lexer;
15170
15171   /* If this member is a template, get the underlying
15172      FUNCTION_DECL.  */
15173   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15174     member_function = DECL_TEMPLATE_RESULT (member_function);
15175
15176   /* There should not be any class definitions in progress at this
15177      point; the bodies of members are only parsed outside of all class
15178      definitions.  */
15179   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
15180   /* While we're parsing the member functions we might encounter more
15181      classes.  We want to handle them right away, but we don't want
15182      them getting mixed up with functions that are currently in the
15183      queue.  */
15184   parser->unparsed_functions_queues
15185     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15186
15187   /* Make sure that any template parameters are in scope.  */
15188   maybe_begin_member_template_processing (member_function);
15189
15190   /* If the body of the function has not yet been parsed, parse it
15191      now.  */
15192   if (DECL_PENDING_INLINE_P (member_function))
15193     {
15194       tree function_scope;
15195       cp_token_cache *tokens;
15196
15197       /* The function is no longer pending; we are processing it.  */
15198       tokens = DECL_PENDING_INLINE_INFO (member_function);
15199       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15200       DECL_PENDING_INLINE_P (member_function) = 0;
15201       /* If this was an inline function in a local class, enter the scope
15202          of the containing function.  */
15203       function_scope = decl_function_context (member_function);
15204       if (function_scope)
15205         push_function_context_to (function_scope);
15206
15207       /* Save away the current lexer.  */
15208       saved_lexer = parser->lexer;
15209       /* Make a new lexer to feed us the tokens saved for this function.  */
15210       parser->lexer = cp_lexer_new_from_tokens (tokens);
15211       parser->lexer->next = saved_lexer;
15212
15213       /* Set the current source position to be the location of the first
15214          token in the saved inline body.  */
15215       cp_lexer_peek_token (parser->lexer);
15216
15217       /* Let the front end know that we going to be defining this
15218          function.  */
15219       start_preparsed_function (member_function, NULL_TREE,
15220                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15221
15222       /* Now, parse the body of the function.  */
15223       cp_parser_function_definition_after_declarator (parser,
15224                                                       /*inline_p=*/true);
15225
15226       /* Leave the scope of the containing function.  */
15227       if (function_scope)
15228         pop_function_context_from (function_scope);
15229       /* Restore the lexer.  */
15230       parser->lexer = saved_lexer;
15231     }
15232
15233   /* Remove any template parameters from the symbol table.  */
15234   maybe_end_member_template_processing ();
15235
15236   /* Restore the queue.  */
15237   parser->unparsed_functions_queues
15238     = TREE_CHAIN (parser->unparsed_functions_queues);
15239 }
15240
15241 /* If DECL contains any default args, remember it on the unparsed
15242    functions queue.  */
15243
15244 static void
15245 cp_parser_save_default_args (cp_parser* parser, tree decl)
15246 {
15247   tree probe;
15248
15249   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15250        probe;
15251        probe = TREE_CHAIN (probe))
15252     if (TREE_PURPOSE (probe))
15253       {
15254         TREE_PURPOSE (parser->unparsed_functions_queues)
15255           = tree_cons (NULL_TREE, decl,
15256                        TREE_PURPOSE (parser->unparsed_functions_queues));
15257         break;
15258       }
15259   return;
15260 }
15261
15262 /* FN is a FUNCTION_DECL which may contains a parameter with an
15263    unparsed DEFAULT_ARG.  Parse the default args now.  */
15264
15265 static void
15266 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15267 {
15268   cp_lexer *saved_lexer;
15269   cp_token_cache *tokens;
15270   bool saved_local_variables_forbidden_p;
15271   tree parameters;
15272
15273   /* While we're parsing the default args, we might (due to the
15274      statement expression extension) encounter more classes.  We want
15275      to handle them right away, but we don't want them getting mixed
15276      up with default args that are currently in the queue.  */
15277   parser->unparsed_functions_queues
15278     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15279
15280   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15281        parameters;
15282        parameters = TREE_CHAIN (parameters))
15283     {
15284       if (!TREE_PURPOSE (parameters)
15285           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15286         continue;
15287
15288        /* Save away the current lexer.  */
15289       saved_lexer = parser->lexer;
15290        /* Create a new one, using the tokens we have saved.  */
15291       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
15292       parser->lexer = cp_lexer_new_from_tokens (tokens);
15293
15294        /* Set the current source position to be the location of the
15295           first token in the default argument.  */
15296       cp_lexer_peek_token (parser->lexer);
15297
15298        /* Local variable names (and the `this' keyword) may not appear
15299           in a default argument.  */
15300       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15301       parser->local_variables_forbidden_p = true;
15302        /* Parse the assignment-expression.  */
15303       if (DECL_CLASS_SCOPE_P (fn))
15304         push_nested_class (DECL_CONTEXT (fn));
15305       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15306       if (DECL_CLASS_SCOPE_P (fn))
15307         pop_nested_class ();
15308
15309       /* If the token stream has not been completely used up, then
15310          there was extra junk after the end of the default
15311          argument.  */
15312       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15313         cp_parser_error (parser, "expected `,'");
15314
15315        /* Restore saved state.  */
15316       parser->lexer = saved_lexer;
15317       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15318     }
15319
15320   /* Restore the queue.  */
15321   parser->unparsed_functions_queues
15322     = TREE_CHAIN (parser->unparsed_functions_queues);
15323 }
15324
15325 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15326    either a TYPE or an expression, depending on the form of the
15327    input.  The KEYWORD indicates which kind of expression we have
15328    encountered.  */
15329
15330 static tree
15331 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15332 {
15333   static const char *format;
15334   tree expr = NULL_TREE;
15335   const char *saved_message;
15336   bool saved_integral_constant_expression_p;
15337
15338   /* Initialize FORMAT the first time we get here.  */
15339   if (!format)
15340     format = "types may not be defined in `%s' expressions";
15341
15342   /* Types cannot be defined in a `sizeof' expression.  Save away the
15343      old message.  */
15344   saved_message = parser->type_definition_forbidden_message;
15345   /* And create the new one.  */
15346   parser->type_definition_forbidden_message
15347     = xmalloc (strlen (format)
15348                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15349                + 1 /* `\0' */);
15350   sprintf ((char *) parser->type_definition_forbidden_message,
15351            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15352
15353   /* The restrictions on constant-expressions do not apply inside
15354      sizeof expressions.  */
15355   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15356   parser->integral_constant_expression_p = false;
15357
15358   /* Do not actually evaluate the expression.  */
15359   ++skip_evaluation;
15360   /* If it's a `(', then we might be looking at the type-id
15361      construction.  */
15362   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15363     {
15364       tree type;
15365       bool saved_in_type_id_in_expr_p;
15366
15367       /* We can't be sure yet whether we're looking at a type-id or an
15368          expression.  */
15369       cp_parser_parse_tentatively (parser);
15370       /* Consume the `('.  */
15371       cp_lexer_consume_token (parser->lexer);
15372       /* Parse the type-id.  */
15373       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15374       parser->in_type_id_in_expr_p = true;
15375       type = cp_parser_type_id (parser);
15376       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15377       /* Now, look for the trailing `)'.  */
15378       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15379       /* If all went well, then we're done.  */
15380       if (cp_parser_parse_definitely (parser))
15381         {
15382           cp_decl_specifier_seq decl_specs;
15383
15384           /* Build a trivial decl-specifier-seq.  */
15385           clear_decl_specs (&decl_specs);
15386           decl_specs.type = type;
15387
15388           /* Call grokdeclarator to figure out what type this is.  */
15389           expr = grokdeclarator (NULL,
15390                                  &decl_specs,
15391                                  TYPENAME,
15392                                  /*initialized=*/0,
15393                                  /*attrlist=*/NULL);
15394         }
15395     }
15396
15397   /* If the type-id production did not work out, then we must be
15398      looking at the unary-expression production.  */
15399   if (!expr)
15400     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15401   /* Go back to evaluating expressions.  */
15402   --skip_evaluation;
15403
15404   /* Free the message we created.  */
15405   free ((char *) parser->type_definition_forbidden_message);
15406   /* And restore the old one.  */
15407   parser->type_definition_forbidden_message = saved_message;
15408   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15409
15410   return expr;
15411 }
15412
15413 /* If the current declaration has no declarator, return true.  */
15414
15415 static bool
15416 cp_parser_declares_only_class_p (cp_parser *parser)
15417 {
15418   /* If the next token is a `;' or a `,' then there is no
15419      declarator.  */
15420   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15421           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15422 }
15423
15424 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15425
15426 static void
15427 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15428                              cp_storage_class storage_class)
15429 {
15430   if (decl_specs->storage_class != sc_none)
15431     decl_specs->multiple_storage_classes_p = true;
15432   else
15433     decl_specs->storage_class = storage_class;
15434 }
15435
15436 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15437    is true, the type is a user-defined type; otherwise it is a
15438    built-in type specified by a keyword.  */
15439
15440 static void
15441 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15442                               tree type_spec,
15443                               bool user_defined_p)
15444 {
15445   decl_specs->any_specifiers_p = true;
15446   if (decl_specs->type)
15447     {
15448       if (decl_specs->specs[(int)ds_typedef] && !user_defined_p)
15449         decl_specs->redefined_builtin_type = type_spec;
15450       else
15451         decl_specs->multiple_types_p = true;
15452     }
15453   else
15454     {
15455       decl_specs->type = type_spec;
15456       decl_specs->user_defined_type_p = user_defined_p;
15457     }
15458 }
15459
15460 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15461    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15462
15463 static bool
15464 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15465 {
15466   return decl_specifiers->specs[(int) ds_friend] != 0;
15467 }
15468
15469 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15470    issue an error message indicating that TOKEN_DESC was expected.
15471
15472    Returns the token consumed, if the token had the appropriate type.
15473    Otherwise, returns NULL.  */
15474
15475 static cp_token *
15476 cp_parser_require (cp_parser* parser,
15477                    enum cpp_ttype type,
15478                    const char* token_desc)
15479 {
15480   if (cp_lexer_next_token_is (parser->lexer, type))
15481     return cp_lexer_consume_token (parser->lexer);
15482   else
15483     {
15484       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15485       if (!cp_parser_simulate_error (parser))
15486         {
15487           char *message = concat ("expected ", token_desc, NULL);
15488           cp_parser_error (parser, message);
15489           free (message);
15490         }
15491       return NULL;
15492     }
15493 }
15494
15495 /* Like cp_parser_require, except that tokens will be skipped until
15496    the desired token is found.  An error message is still produced if
15497    the next token is not as expected.  */
15498
15499 static void
15500 cp_parser_skip_until_found (cp_parser* parser,
15501                             enum cpp_ttype type,
15502                             const char* token_desc)
15503 {
15504   cp_token *token;
15505   unsigned nesting_depth = 0;
15506
15507   if (cp_parser_require (parser, type, token_desc))
15508     return;
15509
15510   /* Skip tokens until the desired token is found.  */
15511   while (true)
15512     {
15513       /* Peek at the next token.  */
15514       token = cp_lexer_peek_token (parser->lexer);
15515       /* If we've reached the token we want, consume it and
15516          stop.  */
15517       if (token->type == type && !nesting_depth)
15518         {
15519           cp_lexer_consume_token (parser->lexer);
15520           return;
15521         }
15522       /* If we've run out of tokens, stop.  */
15523       if (token->type == CPP_EOF)
15524         return;
15525       if (token->type == CPP_OPEN_BRACE
15526           || token->type == CPP_OPEN_PAREN
15527           || token->type == CPP_OPEN_SQUARE)
15528         ++nesting_depth;
15529       else if (token->type == CPP_CLOSE_BRACE
15530                || token->type == CPP_CLOSE_PAREN
15531                || token->type == CPP_CLOSE_SQUARE)
15532         {
15533           if (nesting_depth-- == 0)
15534             return;
15535         }
15536       /* Consume this token.  */
15537       cp_lexer_consume_token (parser->lexer);
15538     }
15539 }
15540
15541 /* If the next token is the indicated keyword, consume it.  Otherwise,
15542    issue an error message indicating that TOKEN_DESC was expected.
15543
15544    Returns the token consumed, if the token had the appropriate type.
15545    Otherwise, returns NULL.  */
15546
15547 static cp_token *
15548 cp_parser_require_keyword (cp_parser* parser,
15549                            enum rid keyword,
15550                            const char* token_desc)
15551 {
15552   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15553
15554   if (token && token->keyword != keyword)
15555     {
15556       dyn_string_t error_msg;
15557
15558       /* Format the error message.  */
15559       error_msg = dyn_string_new (0);
15560       dyn_string_append_cstr (error_msg, "expected ");
15561       dyn_string_append_cstr (error_msg, token_desc);
15562       cp_parser_error (parser, error_msg->s);
15563       dyn_string_delete (error_msg);
15564       return NULL;
15565     }
15566
15567   return token;
15568 }
15569
15570 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15571    function-definition.  */
15572
15573 static bool
15574 cp_parser_token_starts_function_definition_p (cp_token* token)
15575 {
15576   return (/* An ordinary function-body begins with an `{'.  */
15577           token->type == CPP_OPEN_BRACE
15578           /* A ctor-initializer begins with a `:'.  */
15579           || token->type == CPP_COLON
15580           /* A function-try-block begins with `try'.  */
15581           || token->keyword == RID_TRY
15582           /* The named return value extension begins with `return'.  */
15583           || token->keyword == RID_RETURN);
15584 }
15585
15586 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15587    definition.  */
15588
15589 static bool
15590 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15591 {
15592   cp_token *token;
15593
15594   token = cp_lexer_peek_token (parser->lexer);
15595   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15596 }
15597
15598 /* Returns TRUE iff the next token is the "," or ">" ending a
15599    template-argument. ">>" is also accepted (after the full
15600    argument was parsed) because it's probably a typo for "> >",
15601    and there is a specific diagnostic for this.  */
15602
15603 static bool
15604 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15605 {
15606   cp_token *token;
15607
15608   token = cp_lexer_peek_token (parser->lexer);
15609   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15610           || token->type == CPP_RSHIFT);
15611 }
15612
15613 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15614    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15615
15616 static bool
15617 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15618                                                      size_t n)
15619 {
15620   cp_token *token;
15621
15622   token = cp_lexer_peek_nth_token (parser->lexer, n);
15623   if (token->type == CPP_LESS)
15624     return true;
15625   /* Check for the sequence `<::' in the original code. It would be lexed as
15626      `[:', where `[' is a digraph, and there is no whitespace before
15627      `:'.  */
15628   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15629     {
15630       cp_token *token2;
15631       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15632       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15633         return true;
15634     }
15635   return false;
15636 }
15637
15638 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15639    or none_type otherwise.  */
15640
15641 static enum tag_types
15642 cp_parser_token_is_class_key (cp_token* token)
15643 {
15644   switch (token->keyword)
15645     {
15646     case RID_CLASS:
15647       return class_type;
15648     case RID_STRUCT:
15649       return record_type;
15650     case RID_UNION:
15651       return union_type;
15652
15653     default:
15654       return none_type;
15655     }
15656 }
15657
15658 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15659
15660 static void
15661 cp_parser_check_class_key (enum tag_types class_key, tree type)
15662 {
15663   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15664     pedwarn ("`%s' tag used in naming `%#T'",
15665             class_key == union_type ? "union"
15666              : class_key == record_type ? "struct" : "class",
15667              type);
15668 }
15669
15670 /* Issue an error message if DECL is redeclared with different
15671    access than its original declaration [class.access.spec/3].
15672    This applies to nested classes and nested class templates.
15673    [class.mem/1].  */
15674
15675 static void cp_parser_check_access_in_redeclaration (tree decl)
15676 {
15677   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15678     return;
15679
15680   if ((TREE_PRIVATE (decl)
15681        != (current_access_specifier == access_private_node))
15682       || (TREE_PROTECTED (decl)
15683           != (current_access_specifier == access_protected_node)))
15684     error ("%D redeclared with different access", decl);
15685 }
15686
15687 /* Look for the `template' keyword, as a syntactic disambiguator.
15688    Return TRUE iff it is present, in which case it will be
15689    consumed.  */
15690
15691 static bool
15692 cp_parser_optional_template_keyword (cp_parser *parser)
15693 {
15694   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15695     {
15696       /* The `template' keyword can only be used within templates;
15697          outside templates the parser can always figure out what is a
15698          template and what is not.  */
15699       if (!processing_template_decl)
15700         {
15701           error ("`template' (as a disambiguator) is only allowed "
15702                  "within templates");
15703           /* If this part of the token stream is rescanned, the same
15704              error message would be generated.  So, we purge the token
15705              from the stream.  */
15706           cp_lexer_purge_token (parser->lexer);
15707           return false;
15708         }
15709       else
15710         {
15711           /* Consume the `template' keyword.  */
15712           cp_lexer_consume_token (parser->lexer);
15713           return true;
15714         }
15715     }
15716
15717   return false;
15718 }
15719
15720 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15721    set PARSER->SCOPE, and perform other related actions.  */
15722
15723 static void
15724 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15725 {
15726   tree value;
15727   tree check;
15728
15729   /* Get the stored value.  */
15730   value = cp_lexer_consume_token (parser->lexer)->value;
15731   /* Perform any access checks that were deferred.  */
15732   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15733     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15734   /* Set the scope from the stored value.  */
15735   parser->scope = TREE_VALUE (value);
15736   parser->qualifying_scope = TREE_TYPE (value);
15737   parser->object_scope = NULL_TREE;
15738 }
15739
15740 /* Add tokens to CACHE until a non-nested END token appears.  */
15741
15742 static void
15743 cp_parser_cache_group_1 (cp_parser *parser,
15744                          cp_token_cache *cache,
15745                          enum cpp_ttype end,
15746                          unsigned depth)
15747 {
15748   while (true)
15749     {
15750       cp_token *token;
15751
15752       /* Abort a parenthesized expression if we encounter a brace.  */
15753       if ((end == CPP_CLOSE_PAREN || depth == 0)
15754           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15755         return;
15756       /* If we've reached the end of the file, stop.  */
15757       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15758         return;
15759       /* Consume the next token.  */
15760       token = cp_lexer_consume_token (parser->lexer);
15761       /* Add this token to the tokens we are saving.  */
15762       cp_token_cache_push_token (cache, token);
15763       /* See if it starts a new group.  */
15764       if (token->type == CPP_OPEN_BRACE)
15765         {
15766           cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15767           if (depth == 0)
15768             return;
15769         }
15770       else if (token->type == CPP_OPEN_PAREN)
15771         cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15772       else if (token->type == end)
15773         return;
15774     }
15775 }
15776
15777 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15778    preserve string tokens in both translated and untranslated
15779    forms.  */
15780
15781 static void
15782 cp_parser_cache_group (cp_parser *parser,
15783                          cp_token_cache *cache,
15784                          enum cpp_ttype end,
15785                          unsigned depth)
15786 {
15787   int saved_c_lex_string_translate;
15788
15789   saved_c_lex_string_translate = c_lex_string_translate;
15790   c_lex_string_translate = -1;
15791
15792   cp_parser_cache_group_1 (parser, cache, end, depth);
15793   
15794   c_lex_string_translate = saved_c_lex_string_translate;
15795 }
15796
15797
15798 /* Begin parsing tentatively.  We always save tokens while parsing
15799    tentatively so that if the tentative parsing fails we can restore the
15800    tokens.  */
15801
15802 static void
15803 cp_parser_parse_tentatively (cp_parser* parser)
15804 {
15805   /* Enter a new parsing context.  */
15806   parser->context = cp_parser_context_new (parser->context);
15807   /* Begin saving tokens.  */
15808   cp_lexer_save_tokens (parser->lexer);
15809   /* In order to avoid repetitive access control error messages,
15810      access checks are queued up until we are no longer parsing
15811      tentatively.  */
15812   push_deferring_access_checks (dk_deferred);
15813 }
15814
15815 /* Commit to the currently active tentative parse.  */
15816
15817 static void
15818 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15819 {
15820   cp_parser_context *context;
15821   cp_lexer *lexer;
15822
15823   /* Mark all of the levels as committed.  */
15824   lexer = parser->lexer;
15825   for (context = parser->context; context->next; context = context->next)
15826     {
15827       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15828         break;
15829       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15830       while (!cp_lexer_saving_tokens (lexer))
15831         lexer = lexer->next;
15832       cp_lexer_commit_tokens (lexer);
15833     }
15834 }
15835
15836 /* Abort the currently active tentative parse.  All consumed tokens
15837    will be rolled back, and no diagnostics will be issued.  */
15838
15839 static void
15840 cp_parser_abort_tentative_parse (cp_parser* parser)
15841 {
15842   cp_parser_simulate_error (parser);
15843   /* Now, pretend that we want to see if the construct was
15844      successfully parsed.  */
15845   cp_parser_parse_definitely (parser);
15846 }
15847
15848 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15849    token stream.  Otherwise, commit to the tokens we have consumed.
15850    Returns true if no error occurred; false otherwise.  */
15851
15852 static bool
15853 cp_parser_parse_definitely (cp_parser* parser)
15854 {
15855   bool error_occurred;
15856   cp_parser_context *context;
15857
15858   /* Remember whether or not an error occurred, since we are about to
15859      destroy that information.  */
15860   error_occurred = cp_parser_error_occurred (parser);
15861   /* Remove the topmost context from the stack.  */
15862   context = parser->context;
15863   parser->context = context->next;
15864   /* If no parse errors occurred, commit to the tentative parse.  */
15865   if (!error_occurred)
15866     {
15867       /* Commit to the tokens read tentatively, unless that was
15868          already done.  */
15869       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15870         cp_lexer_commit_tokens (parser->lexer);
15871
15872       pop_to_parent_deferring_access_checks ();
15873     }
15874   /* Otherwise, if errors occurred, roll back our state so that things
15875      are just as they were before we began the tentative parse.  */
15876   else
15877     {
15878       cp_lexer_rollback_tokens (parser->lexer);
15879       pop_deferring_access_checks ();
15880     }
15881   /* Add the context to the front of the free list.  */
15882   context->next = cp_parser_context_free_list;
15883   cp_parser_context_free_list = context;
15884
15885   return !error_occurred;
15886 }
15887
15888 /* Returns true if we are parsing tentatively -- but have decided that
15889    we will stick with this tentative parse, even if errors occur.  */
15890
15891 static bool
15892 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15893 {
15894   return (cp_parser_parsing_tentatively (parser)
15895           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15896 }
15897
15898 /* Returns nonzero iff an error has occurred during the most recent
15899    tentative parse.  */
15900
15901 static bool
15902 cp_parser_error_occurred (cp_parser* parser)
15903 {
15904   return (cp_parser_parsing_tentatively (parser)
15905           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15906 }
15907
15908 /* Returns nonzero if GNU extensions are allowed.  */
15909
15910 static bool
15911 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15912 {
15913   return parser->allow_gnu_extensions_p;
15914 }
15915
15916 \f
15917 /* The parser.  */
15918
15919 static GTY (()) cp_parser *the_parser;
15920
15921 /* External interface.  */
15922
15923 /* Parse one entire translation unit.  */
15924
15925 void
15926 c_parse_file (void)
15927 {
15928   bool error_occurred;
15929   static bool already_called = false;
15930
15931   if (already_called)
15932     {
15933       sorry ("inter-module optimizations not implemented for C++");
15934       return;
15935     }
15936   already_called = true;
15937
15938   the_parser = cp_parser_new ();
15939   push_deferring_access_checks (flag_access_control
15940                                 ? dk_no_deferred : dk_no_check);
15941   error_occurred = cp_parser_translation_unit (the_parser);
15942   the_parser = NULL;
15943 }
15944
15945 /* This variable must be provided by every front end.  */
15946
15947 int yydebug;
15948
15949 #include "gt-cp-parser.h"