OSDN Git Service

* friend.c (add_friend): Only perform access checks when context
[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 *, cp_cv_quals, tree);
1023 static cp_declarator *make_array_declarator     
1024   (cp_declarator *, tree);
1025 static cp_declarator *make_pointer_declarator   
1026   (cp_cv_quals, cp_declarator *);
1027 static cp_declarator *make_reference_declarator 
1028   (cp_cv_quals, 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   (cp_cv_quals, 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 (cp_cv_quals 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 (cp_cv_quals 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 (cp_cv_quals 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                       cp_cv_quals 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 *, cp_cv_quals *);
1697 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1698   (cp_parser *);
1699 static tree cp_parser_declarator_id
1700   (cp_parser *);
1701 static tree cp_parser_type_id
1702   (cp_parser *);
1703 static void cp_parser_type_specifier_seq
1704   (cp_parser *, cp_decl_specifier_seq *);
1705 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1706   (cp_parser *);
1707 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1708   (cp_parser *, bool *);
1709 static cp_parameter_declarator *cp_parser_parameter_declaration
1710   (cp_parser *, bool, bool *);
1711 static void cp_parser_function_body
1712   (cp_parser *);
1713 static tree cp_parser_initializer
1714   (cp_parser *, bool *, bool *);
1715 static tree cp_parser_initializer_clause
1716   (cp_parser *, bool *);
1717 static tree cp_parser_initializer_list
1718   (cp_parser *, bool *);
1719
1720 static bool cp_parser_ctor_initializer_opt_and_function_body
1721   (cp_parser *);
1722
1723 /* Classes [gram.class] */
1724
1725 static tree cp_parser_class_name
1726   (cp_parser *, bool, bool, bool, bool, bool, bool);
1727 static tree cp_parser_class_specifier
1728   (cp_parser *);
1729 static tree cp_parser_class_head
1730   (cp_parser *, bool *, tree *);
1731 static enum tag_types cp_parser_class_key
1732   (cp_parser *);
1733 static void cp_parser_member_specification_opt
1734   (cp_parser *);
1735 static void cp_parser_member_declaration
1736   (cp_parser *);
1737 static tree cp_parser_pure_specifier
1738   (cp_parser *);
1739 static tree cp_parser_constant_initializer
1740   (cp_parser *);
1741
1742 /* Derived classes [gram.class.derived] */
1743
1744 static tree cp_parser_base_clause
1745   (cp_parser *);
1746 static tree cp_parser_base_specifier
1747   (cp_parser *);
1748
1749 /* Special member functions [gram.special] */
1750
1751 static tree cp_parser_conversion_function_id
1752   (cp_parser *);
1753 static tree cp_parser_conversion_type_id
1754   (cp_parser *);
1755 static cp_declarator *cp_parser_conversion_declarator_opt
1756   (cp_parser *);
1757 static bool cp_parser_ctor_initializer_opt
1758   (cp_parser *);
1759 static void cp_parser_mem_initializer_list
1760   (cp_parser *);
1761 static tree cp_parser_mem_initializer
1762   (cp_parser *);
1763 static tree cp_parser_mem_initializer_id
1764   (cp_parser *);
1765
1766 /* Overloading [gram.over] */
1767
1768 static tree cp_parser_operator_function_id
1769   (cp_parser *);
1770 static tree cp_parser_operator
1771   (cp_parser *);
1772
1773 /* Templates [gram.temp] */
1774
1775 static void cp_parser_template_declaration
1776   (cp_parser *, bool);
1777 static tree cp_parser_template_parameter_list
1778   (cp_parser *);
1779 static tree cp_parser_template_parameter
1780   (cp_parser *, bool *);
1781 static tree cp_parser_type_parameter
1782   (cp_parser *);
1783 static tree cp_parser_template_id
1784   (cp_parser *, bool, bool, bool);
1785 static tree cp_parser_template_name
1786   (cp_parser *, bool, bool, bool, bool *);
1787 static tree cp_parser_template_argument_list
1788   (cp_parser *);
1789 static tree cp_parser_template_argument
1790   (cp_parser *);
1791 static void cp_parser_explicit_instantiation
1792   (cp_parser *);
1793 static void cp_parser_explicit_specialization
1794   (cp_parser *);
1795
1796 /* Exception handling [gram.exception] */
1797
1798 static tree cp_parser_try_block
1799   (cp_parser *);
1800 static bool cp_parser_function_try_block
1801   (cp_parser *);
1802 static void cp_parser_handler_seq
1803   (cp_parser *);
1804 static void cp_parser_handler
1805   (cp_parser *);
1806 static tree cp_parser_exception_declaration
1807   (cp_parser *);
1808 static tree cp_parser_throw_expression
1809   (cp_parser *);
1810 static tree cp_parser_exception_specification_opt
1811   (cp_parser *);
1812 static tree cp_parser_type_id_list
1813   (cp_parser *);
1814
1815 /* GNU Extensions */
1816
1817 static tree cp_parser_asm_specification_opt
1818   (cp_parser *);
1819 static tree cp_parser_asm_operand_list
1820   (cp_parser *);
1821 static tree cp_parser_asm_clobber_list
1822   (cp_parser *);
1823 static tree cp_parser_attributes_opt
1824   (cp_parser *);
1825 static tree cp_parser_attribute_list
1826   (cp_parser *);
1827 static bool cp_parser_extension_opt
1828   (cp_parser *, int *);
1829 static void cp_parser_label_declaration
1830   (cp_parser *);
1831
1832 /* Utility Routines */
1833
1834 static tree cp_parser_lookup_name
1835   (cp_parser *, tree, bool, bool, bool, bool);
1836 static tree cp_parser_lookup_name_simple
1837   (cp_parser *, tree);
1838 static tree cp_parser_maybe_treat_template_as_class
1839   (tree, bool);
1840 static bool cp_parser_check_declarator_template_parameters
1841   (cp_parser *, cp_declarator *);
1842 static bool cp_parser_check_template_parameters
1843   (cp_parser *, unsigned);
1844 static tree cp_parser_simple_cast_expression
1845   (cp_parser *);
1846 static tree cp_parser_binary_expression
1847   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1848 static tree cp_parser_global_scope_opt
1849   (cp_parser *, bool);
1850 static bool cp_parser_constructor_declarator_p
1851   (cp_parser *, bool);
1852 static tree cp_parser_function_definition_from_specifiers_and_declarator
1853   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1854 static tree cp_parser_function_definition_after_declarator
1855   (cp_parser *, bool);
1856 static void cp_parser_template_declaration_after_export
1857   (cp_parser *, bool);
1858 static tree cp_parser_single_declaration
1859   (cp_parser *, bool, bool *);
1860 static tree cp_parser_functional_cast
1861   (cp_parser *, tree);
1862 static tree cp_parser_save_member_function_body
1863   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1864 static tree cp_parser_enclosed_template_argument_list
1865   (cp_parser *);
1866 static void cp_parser_save_default_args
1867   (cp_parser *, tree);
1868 static void cp_parser_late_parsing_for_member
1869   (cp_parser *, tree);
1870 static void cp_parser_late_parsing_default_args
1871   (cp_parser *, tree);
1872 static tree cp_parser_sizeof_operand
1873   (cp_parser *, enum rid);
1874 static bool cp_parser_declares_only_class_p
1875   (cp_parser *);
1876 static void cp_parser_set_storage_class
1877   (cp_decl_specifier_seq *, cp_storage_class);
1878 static void cp_parser_set_decl_spec_type 
1879   (cp_decl_specifier_seq *, tree, bool);
1880 static bool cp_parser_friend_p
1881   (const cp_decl_specifier_seq *);
1882 static cp_token *cp_parser_require
1883   (cp_parser *, enum cpp_ttype, const char *);
1884 static cp_token *cp_parser_require_keyword
1885   (cp_parser *, enum rid, const char *);
1886 static bool cp_parser_token_starts_function_definition_p
1887   (cp_token *);
1888 static bool cp_parser_next_token_starts_class_definition_p
1889   (cp_parser *);
1890 static bool cp_parser_next_token_ends_template_argument_p
1891   (cp_parser *);
1892 static bool cp_parser_nth_token_starts_template_argument_list_p
1893   (cp_parser *, size_t);
1894 static enum tag_types cp_parser_token_is_class_key
1895   (cp_token *);
1896 static void cp_parser_check_class_key
1897   (enum tag_types, tree type);
1898 static void cp_parser_check_access_in_redeclaration
1899   (tree type);
1900 static bool cp_parser_optional_template_keyword
1901   (cp_parser *);
1902 static void cp_parser_pre_parsed_nested_name_specifier
1903   (cp_parser *);
1904 static void cp_parser_cache_group
1905   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1906 static void cp_parser_parse_tentatively
1907   (cp_parser *);
1908 static void cp_parser_commit_to_tentative_parse
1909   (cp_parser *);
1910 static void cp_parser_abort_tentative_parse
1911   (cp_parser *);
1912 static bool cp_parser_parse_definitely
1913   (cp_parser *);
1914 static inline bool cp_parser_parsing_tentatively
1915   (cp_parser *);
1916 static bool cp_parser_committed_to_tentative_parse
1917   (cp_parser *);
1918 static void cp_parser_error
1919   (cp_parser *, const char *);
1920 static void cp_parser_name_lookup_error
1921   (cp_parser *, tree, tree, const char *);
1922 static bool cp_parser_simulate_error
1923   (cp_parser *);
1924 static void cp_parser_check_type_definition
1925   (cp_parser *);
1926 static void cp_parser_check_for_definition_in_return_type
1927   (cp_declarator *, int);
1928 static void cp_parser_check_for_invalid_template_id
1929   (cp_parser *, tree);
1930 static bool cp_parser_non_integral_constant_expression
1931   (cp_parser *, const char *);
1932 static void cp_parser_diagnose_invalid_type_name
1933   (cp_parser *, tree, tree);
1934 static bool cp_parser_parse_and_diagnose_invalid_type_name
1935   (cp_parser *);
1936 static int cp_parser_skip_to_closing_parenthesis
1937   (cp_parser *, bool, bool, bool);
1938 static void cp_parser_skip_to_end_of_statement
1939   (cp_parser *);
1940 static void cp_parser_consume_semicolon_at_end_of_statement
1941   (cp_parser *);
1942 static void cp_parser_skip_to_end_of_block_or_statement
1943   (cp_parser *);
1944 static void cp_parser_skip_to_closing_brace
1945   (cp_parser *);
1946 static void cp_parser_skip_until_found
1947   (cp_parser *, enum cpp_ttype, const char *);
1948 static bool cp_parser_error_occurred
1949   (cp_parser *);
1950 static bool cp_parser_allow_gnu_extensions_p
1951   (cp_parser *);
1952 static bool cp_parser_is_string_literal
1953   (cp_token *);
1954 static bool cp_parser_is_keyword
1955   (cp_token *, enum rid);
1956 static tree cp_parser_make_typename_type
1957   (cp_parser *, tree, tree);
1958
1959 /* Returns nonzero if we are parsing tentatively.  */
1960
1961 static inline bool
1962 cp_parser_parsing_tentatively (cp_parser* parser)
1963 {
1964   return parser->context->next != NULL;
1965 }
1966
1967 /* Returns nonzero if TOKEN is a string literal.  */
1968
1969 static bool
1970 cp_parser_is_string_literal (cp_token* token)
1971 {
1972   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1973 }
1974
1975 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1976
1977 static bool
1978 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1979 {
1980   return token->keyword == keyword;
1981 }
1982
1983 /* Issue the indicated error MESSAGE.  */
1984
1985 static void
1986 cp_parser_error (cp_parser* parser, const char* message)
1987 {
1988   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1989   if (!cp_parser_simulate_error (parser))
1990     {
1991       cp_token *token;
1992       token = cp_lexer_peek_token (parser->lexer);
1993       c_parse_error (message,
1994                      /* Because c_parser_error does not understand
1995                         CPP_KEYWORD, keywords are treated like
1996                         identifiers.  */
1997                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1998                      token->value);
1999     }
2000 }
2001
2002 /* Issue an error about name-lookup failing.  NAME is the
2003    IDENTIFIER_NODE DECL is the result of
2004    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2005    the thing that we hoped to find.  */
2006
2007 static void
2008 cp_parser_name_lookup_error (cp_parser* parser,
2009                              tree name,
2010                              tree decl,
2011                              const char* desired)
2012 {
2013   /* If name lookup completely failed, tell the user that NAME was not
2014      declared.  */
2015   if (decl == error_mark_node)
2016     {
2017       if (parser->scope && parser->scope != global_namespace)
2018         error ("`%D::%D' has not been declared",
2019                parser->scope, name);
2020       else if (parser->scope == global_namespace)
2021         error ("`::%D' has not been declared", name);
2022       else
2023         error ("`%D' has not been declared", name);
2024     }
2025   else if (parser->scope && parser->scope != global_namespace)
2026     error ("`%D::%D' %s", parser->scope, name, desired);
2027   else if (parser->scope == global_namespace)
2028     error ("`::%D' %s", name, desired);
2029   else
2030     error ("`%D' %s", name, desired);
2031 }
2032
2033 /* If we are parsing tentatively, remember that an error has occurred
2034    during this tentative parse.  Returns true if the error was
2035    simulated; false if a message should be issued by the caller.  */
2036
2037 static bool
2038 cp_parser_simulate_error (cp_parser* parser)
2039 {
2040   if (cp_parser_parsing_tentatively (parser)
2041       && !cp_parser_committed_to_tentative_parse (parser))
2042     {
2043       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2044       return true;
2045     }
2046   return false;
2047 }
2048
2049 /* This function is called when a type is defined.  If type
2050    definitions are forbidden at this point, an error message is
2051    issued.  */
2052
2053 static void
2054 cp_parser_check_type_definition (cp_parser* parser)
2055 {
2056   /* If types are forbidden here, issue a message.  */
2057   if (parser->type_definition_forbidden_message)
2058     /* Use `%s' to print the string in case there are any escape
2059        characters in the message.  */
2060     error ("%s", parser->type_definition_forbidden_message);
2061 }
2062
2063 /* This function is called when a declaration is parsed.  If
2064    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2065    indicates that a type was defined in the decl-specifiers for DECL,
2066    then an error is issued.  */
2067
2068 static void
2069 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2070                                                int declares_class_or_enum)
2071 {
2072   /* [dcl.fct] forbids type definitions in return types.
2073      Unfortunately, it's not easy to know whether or not we are
2074      processing a return type until after the fact.  */
2075   while (declarator
2076          && (declarator->kind == cdk_pointer
2077              || declarator->kind == cdk_reference
2078              || declarator->kind == cdk_ptrmem))
2079     declarator = declarator->declarator;
2080   if (declarator
2081       && declarator->kind == cdk_function
2082       && declares_class_or_enum & 2)
2083     error ("new types may not be defined in a return type");
2084 }
2085
2086 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2087    "<" in any valid C++ program.  If the next token is indeed "<",
2088    issue a message warning the user about what appears to be an
2089    invalid attempt to form a template-id.  */
2090
2091 static void
2092 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2093                                          tree type)
2094 {
2095   ptrdiff_t start;
2096   cp_token *token;
2097
2098   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2099     {
2100       if (TYPE_P (type))
2101         error ("`%T' is not a template", type);
2102       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2103         error ("`%E' is not a template", type);
2104       else
2105         error ("invalid template-id");
2106       /* Remember the location of the invalid "<".  */
2107       if (cp_parser_parsing_tentatively (parser)
2108           && !cp_parser_committed_to_tentative_parse (parser))
2109         {
2110           token = cp_lexer_peek_token (parser->lexer);
2111           token = cp_lexer_prev_token (parser->lexer, token);
2112           start = cp_lexer_token_difference (parser->lexer,
2113                                              parser->lexer->first_token,
2114                                              token);
2115         }
2116       else
2117         start = -1;
2118       /* Consume the "<".  */
2119       cp_lexer_consume_token (parser->lexer);
2120       /* Parse the template arguments.  */
2121       cp_parser_enclosed_template_argument_list (parser);
2122       /* Permanently remove the invalid template arguments so that
2123          this error message is not issued again.  */
2124       if (start >= 0)
2125         {
2126           token = cp_lexer_advance_token (parser->lexer,
2127                                           parser->lexer->first_token,
2128                                           start);
2129           cp_lexer_purge_tokens_after (parser->lexer, token);
2130         }
2131     }
2132 }
2133
2134 /* If parsing an integral constant-expression, issue an error message
2135    about the fact that THING appeared and return true.  Otherwise,
2136    return false, marking the current expression as non-constant.  */
2137
2138 static bool
2139 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2140                                             const char *thing)
2141 {
2142   if (parser->integral_constant_expression_p)
2143     {
2144       if (!parser->allow_non_integral_constant_expression_p)
2145         {
2146           error ("%s cannot appear in a constant-expression", thing);
2147           return true;
2148         }
2149       parser->non_integral_constant_expression_p = true;
2150     }
2151   return false;
2152 }
2153
2154 /* Emit a diagnostic for an invalid type name. Consider also if it is
2155    qualified or not and the result of a lookup, to provide a better
2156    message.  */
2157
2158 static void
2159 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2160 {
2161   tree decl, old_scope;
2162   /* Try to lookup the identifier.  */
2163   old_scope = parser->scope;
2164   parser->scope = scope;
2165   decl = cp_parser_lookup_name_simple (parser, id);
2166   parser->scope = old_scope;
2167   /* If the lookup found a template-name, it means that the user forgot
2168   to specify an argument list. Emit an useful error message.  */
2169   if (TREE_CODE (decl) == TEMPLATE_DECL)
2170     error ("invalid use of template-name `%E' without an argument list",
2171       decl);
2172   else if (!parser->scope)
2173     {
2174       /* Issue an error message.  */
2175       error ("`%E' does not name a type", id);
2176       /* If we're in a template class, it's possible that the user was
2177          referring to a type from a base class.  For example:
2178
2179            template <typename T> struct A { typedef T X; };
2180            template <typename T> struct B : public A<T> { X x; };
2181
2182          The user should have said "typename A<T>::X".  */
2183       if (processing_template_decl && current_class_type)
2184         {
2185           tree b;
2186
2187           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2188                b;
2189                b = TREE_CHAIN (b))
2190             {
2191               tree base_type = BINFO_TYPE (b);
2192               if (CLASS_TYPE_P (base_type)
2193                   && dependent_type_p (base_type))
2194                 {
2195                   tree field;
2196                   /* Go from a particular instantiation of the
2197                      template (which will have an empty TYPE_FIELDs),
2198                      to the main version.  */
2199                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2200                   for (field = TYPE_FIELDS (base_type);
2201                        field;
2202                        field = TREE_CHAIN (field))
2203                     if (TREE_CODE (field) == TYPE_DECL
2204                         && DECL_NAME (field) == id)
2205                       {
2206                         inform ("(perhaps `typename %T::%E' was intended)",
2207                                 BINFO_TYPE (b), id);
2208                         break;
2209                       }
2210                   if (field)
2211                     break;
2212                 }
2213             }
2214         }
2215     }
2216   /* Here we diagnose qualified-ids where the scope is actually correct,
2217      but the identifier does not resolve to a valid type name.  */
2218   else
2219     {
2220       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2221         error ("`%E' in namespace `%E' does not name a type",
2222                id, parser->scope);
2223       else if (TYPE_P (parser->scope))
2224         error ("`%E' in class `%T' does not name a type",
2225                id, parser->scope);
2226       else
2227         abort();
2228     }
2229 }
2230
2231 /* Check for a common situation where a type-name should be present,
2232    but is not, and issue a sensible error message.  Returns true if an
2233    invalid type-name was detected.
2234
2235    The situation handled by this function are variable declarations of the
2236    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2237    Usually, `ID' should name a type, but if we got here it means that it
2238    does not. We try to emit the best possible error message depending on
2239    how exactly the id-expression looks like.
2240 */
2241
2242 static bool
2243 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2244 {
2245   tree id;
2246
2247   cp_parser_parse_tentatively (parser);
2248   id = cp_parser_id_expression (parser,
2249                                 /*template_keyword_p=*/false,
2250                                 /*check_dependency_p=*/true,
2251                                 /*template_p=*/NULL,
2252                                 /*declarator_p=*/true);
2253   /* After the id-expression, there should be a plain identifier,
2254      otherwise this is not a simple variable declaration. Also, if
2255      the scope is dependent, we cannot do much.  */
2256   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2257       || (parser->scope && TYPE_P (parser->scope)
2258           && dependent_type_p (parser->scope)))
2259     {
2260       cp_parser_abort_tentative_parse (parser);
2261       return false;
2262     }
2263   if (!cp_parser_parse_definitely (parser))
2264     return false;
2265
2266   /* If we got here, this cannot be a valid variable declaration, thus
2267      the cp_parser_id_expression must have resolved to a plain identifier
2268      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
2269   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2270   /* Emit a diagnostic for the invalid type.  */
2271   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2272   /* Skip to the end of the declaration; there's no point in
2273      trying to process it.  */
2274   cp_parser_skip_to_end_of_block_or_statement (parser);
2275   return true;
2276 }
2277
2278 /* Consume tokens up to, and including, the next non-nested closing `)'.
2279    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2280    are doing error recovery. Returns -1 if OR_COMMA is true and we
2281    found an unnested comma.  */
2282
2283 static int
2284 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2285                                        bool recovering,
2286                                        bool or_comma,
2287                                        bool consume_paren)
2288 {
2289   unsigned paren_depth = 0;
2290   unsigned brace_depth = 0;
2291   int saved_c_lex_string_translate = c_lex_string_translate;
2292   int result;
2293
2294   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2295       && !cp_parser_committed_to_tentative_parse (parser))
2296     return 0;
2297
2298   if (! recovering)
2299     /* If we're looking ahead, keep both translated and untranslated
2300        strings.  */
2301     c_lex_string_translate = -1;
2302
2303   while (true)
2304     {
2305       cp_token *token;
2306
2307       /* If we've run out of tokens, then there is no closing `)'.  */
2308       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2309         {
2310           result = 0;
2311           break;
2312         }
2313
2314       token = cp_lexer_peek_token (parser->lexer);
2315
2316       /* This matches the processing in skip_to_end_of_statement.  */
2317       if (token->type == CPP_SEMICOLON && !brace_depth)
2318         {
2319           result = 0;
2320           break;
2321         }
2322       if (token->type == CPP_OPEN_BRACE)
2323         ++brace_depth;
2324       if (token->type == CPP_CLOSE_BRACE)
2325         {
2326           if (!brace_depth--)
2327             {
2328               result = 0;
2329               break;
2330             }
2331         }
2332       if (recovering && or_comma && token->type == CPP_COMMA
2333           && !brace_depth && !paren_depth)
2334         {
2335           result = -1;
2336           break;
2337         }
2338
2339       if (!brace_depth)
2340         {
2341           /* If it is an `(', we have entered another level of nesting.  */
2342           if (token->type == CPP_OPEN_PAREN)
2343             ++paren_depth;
2344           /* If it is a `)', then we might be done.  */
2345           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2346             {
2347               if (consume_paren)
2348                 cp_lexer_consume_token (parser->lexer);
2349               {
2350                 result = 1;
2351                 break;
2352               }
2353             }
2354         }
2355
2356       /* Consume the token.  */
2357       cp_lexer_consume_token (parser->lexer);
2358     }
2359
2360   c_lex_string_translate = saved_c_lex_string_translate;
2361   return result;
2362 }
2363
2364 /* Consume tokens until we reach the end of the current statement.
2365    Normally, that will be just before consuming a `;'.  However, if a
2366    non-nested `}' comes first, then we stop before consuming that.  */
2367
2368 static void
2369 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2370 {
2371   unsigned nesting_depth = 0;
2372
2373   while (true)
2374     {
2375       cp_token *token;
2376
2377       /* Peek at the next token.  */
2378       token = cp_lexer_peek_token (parser->lexer);
2379       /* If we've run out of tokens, stop.  */
2380       if (token->type == CPP_EOF)
2381         break;
2382       /* If the next token is a `;', we have reached the end of the
2383          statement.  */
2384       if (token->type == CPP_SEMICOLON && !nesting_depth)
2385         break;
2386       /* If the next token is a non-nested `}', then we have reached
2387          the end of the current block.  */
2388       if (token->type == CPP_CLOSE_BRACE)
2389         {
2390           /* If this is a non-nested `}', stop before consuming it.
2391              That way, when confronted with something like:
2392
2393                { 3 + }
2394
2395              we stop before consuming the closing `}', even though we
2396              have not yet reached a `;'.  */
2397           if (nesting_depth == 0)
2398             break;
2399           /* If it is the closing `}' for a block that we have
2400              scanned, stop -- but only after consuming the token.
2401              That way given:
2402
2403                 void f g () { ... }
2404                 typedef int I;
2405
2406              we will stop after the body of the erroneously declared
2407              function, but before consuming the following `typedef'
2408              declaration.  */
2409           if (--nesting_depth == 0)
2410             {
2411               cp_lexer_consume_token (parser->lexer);
2412               break;
2413             }
2414         }
2415       /* If it the next token is a `{', then we are entering a new
2416          block.  Consume the entire block.  */
2417       else if (token->type == CPP_OPEN_BRACE)
2418         ++nesting_depth;
2419       /* Consume the token.  */
2420       cp_lexer_consume_token (parser->lexer);
2421     }
2422 }
2423
2424 /* This function is called at the end of a statement or declaration.
2425    If the next token is a semicolon, it is consumed; otherwise, error
2426    recovery is attempted.  */
2427
2428 static void
2429 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2430 {
2431   /* Look for the trailing `;'.  */
2432   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2433     {
2434       /* If there is additional (erroneous) input, skip to the end of
2435          the statement.  */
2436       cp_parser_skip_to_end_of_statement (parser);
2437       /* If the next token is now a `;', consume it.  */
2438       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2439         cp_lexer_consume_token (parser->lexer);
2440     }
2441 }
2442
2443 /* Skip tokens until we have consumed an entire block, or until we
2444    have consumed a non-nested `;'.  */
2445
2446 static void
2447 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2448 {
2449   unsigned nesting_depth = 0;
2450
2451   while (true)
2452     {
2453       cp_token *token;
2454
2455       /* Peek at the next token.  */
2456       token = cp_lexer_peek_token (parser->lexer);
2457       /* If we've run out of tokens, stop.  */
2458       if (token->type == CPP_EOF)
2459         break;
2460       /* If the next token is a `;', we have reached the end of the
2461          statement.  */
2462       if (token->type == CPP_SEMICOLON && !nesting_depth)
2463         {
2464           /* Consume the `;'.  */
2465           cp_lexer_consume_token (parser->lexer);
2466           break;
2467         }
2468       /* Consume the token.  */
2469       token = cp_lexer_consume_token (parser->lexer);
2470       /* If the next token is a non-nested `}', then we have reached
2471          the end of the current block.  */
2472       if (token->type == CPP_CLOSE_BRACE
2473           && (nesting_depth == 0 || --nesting_depth == 0))
2474         break;
2475       /* If it the next token is a `{', then we are entering a new
2476          block.  Consume the entire block.  */
2477       if (token->type == CPP_OPEN_BRACE)
2478         ++nesting_depth;
2479     }
2480 }
2481
2482 /* Skip tokens until a non-nested closing curly brace is the next
2483    token.  */
2484
2485 static void
2486 cp_parser_skip_to_closing_brace (cp_parser *parser)
2487 {
2488   unsigned nesting_depth = 0;
2489
2490   while (true)
2491     {
2492       cp_token *token;
2493
2494       /* Peek at the next token.  */
2495       token = cp_lexer_peek_token (parser->lexer);
2496       /* If we've run out of tokens, stop.  */
2497       if (token->type == CPP_EOF)
2498         break;
2499       /* If the next token is a non-nested `}', then we have reached
2500          the end of the current block.  */
2501       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2502         break;
2503       /* If it the next token is a `{', then we are entering a new
2504          block.  Consume the entire block.  */
2505       else if (token->type == CPP_OPEN_BRACE)
2506         ++nesting_depth;
2507       /* Consume the token.  */
2508       cp_lexer_consume_token (parser->lexer);
2509     }
2510 }
2511
2512 /* This is a simple wrapper around make_typename_type. When the id is
2513    an unresolved identifier node, we can provide a superior diagnostic
2514    using cp_parser_diagnose_invalid_type_name.  */
2515
2516 static tree
2517 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2518 {
2519   tree result;
2520   if (TREE_CODE (id) == IDENTIFIER_NODE)
2521     {
2522       result = make_typename_type (scope, id, /*complain=*/0);
2523       if (result == error_mark_node)
2524         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2525       return result;
2526     }
2527   return make_typename_type (scope, id, tf_error);
2528 }
2529
2530
2531 /* Create a new C++ parser.  */
2532
2533 static cp_parser *
2534 cp_parser_new (void)
2535 {
2536   cp_parser *parser;
2537   cp_lexer *lexer;
2538
2539   /* cp_lexer_new_main is called before calling ggc_alloc because
2540      cp_lexer_new_main might load a PCH file.  */
2541   lexer = cp_lexer_new_main ();
2542
2543   parser = ggc_alloc_cleared (sizeof (cp_parser));
2544   parser->lexer = lexer;
2545   parser->context = cp_parser_context_new (NULL);
2546
2547   /* For now, we always accept GNU extensions.  */
2548   parser->allow_gnu_extensions_p = 1;
2549
2550   /* The `>' token is a greater-than operator, not the end of a
2551      template-id.  */
2552   parser->greater_than_is_operator_p = true;
2553
2554   parser->default_arg_ok_p = true;
2555
2556   /* We are not parsing a constant-expression.  */
2557   parser->integral_constant_expression_p = false;
2558   parser->allow_non_integral_constant_expression_p = false;
2559   parser->non_integral_constant_expression_p = false;
2560
2561   /* Local variable names are not forbidden.  */
2562   parser->local_variables_forbidden_p = false;
2563
2564   /* We are not processing an `extern "C"' declaration.  */
2565   parser->in_unbraced_linkage_specification_p = false;
2566
2567   /* We are not processing a declarator.  */
2568   parser->in_declarator_p = false;
2569
2570   /* We are not processing a template-argument-list.  */
2571   parser->in_template_argument_list_p = false;
2572
2573   /* We are not in an iteration statement.  */
2574   parser->in_iteration_statement_p = false;
2575
2576   /* We are not in a switch statement.  */
2577   parser->in_switch_statement_p = false;
2578
2579   /* We are not parsing a type-id inside an expression.  */
2580   parser->in_type_id_in_expr_p = false;
2581
2582   /* The unparsed function queue is empty.  */
2583   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2584
2585   /* There are no classes being defined.  */
2586   parser->num_classes_being_defined = 0;
2587
2588   /* No template parameters apply.  */
2589   parser->num_template_parameter_lists = 0;
2590
2591   return parser;
2592 }
2593
2594 /* Lexical conventions [gram.lex]  */
2595
2596 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2597    identifier.  */
2598
2599 static tree
2600 cp_parser_identifier (cp_parser* parser)
2601 {
2602   cp_token *token;
2603
2604   /* Look for the identifier.  */
2605   token = cp_parser_require (parser, CPP_NAME, "identifier");
2606   /* Return the value.  */
2607   return token ? token->value : error_mark_node;
2608 }
2609
2610 /* Basic concepts [gram.basic]  */
2611
2612 /* Parse a translation-unit.
2613
2614    translation-unit:
2615      declaration-seq [opt]
2616
2617    Returns TRUE if all went well.  */
2618
2619 static bool
2620 cp_parser_translation_unit (cp_parser* parser)
2621 {
2622   /* The address of the first non-permanent object on the declarator
2623      obstack.  */
2624   static void *declarator_obstack_base;
2625
2626   bool success;
2627   
2628   /* Create the declarator obstack, if necessary.  */
2629   if (!cp_error_declarator)
2630     {
2631       gcc_obstack_init (&declarator_obstack);
2632       /* Create the error declarator.  */
2633       cp_error_declarator = make_declarator (cdk_error);
2634       /* Create the empty parameter list.  */
2635       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2636       /* Remember where the base of the declarator obstack lies.  */
2637       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2638     }
2639
2640   while (true)
2641     {
2642       cp_parser_declaration_seq_opt (parser);
2643
2644       /* If there are no tokens left then all went well.  */
2645       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2646         {
2647           /* Consume the EOF token.  */
2648           cp_parser_require (parser, CPP_EOF, "end-of-file");
2649           
2650           /* Finish up.  */
2651           finish_translation_unit ();
2652
2653           success = true;
2654           break;
2655         }
2656       else
2657         {
2658           cp_parser_error (parser, "expected declaration");
2659           success = false;
2660           break;
2661         }
2662     }
2663
2664   /* Make sure the declarator obstack was fully cleaned up.  */
2665   my_friendly_assert (obstack_next_free (&declarator_obstack) ==
2666                       declarator_obstack_base,
2667                       20040621);
2668
2669   /* All went well.  */
2670   return success;
2671 }
2672
2673 /* Expressions [gram.expr] */
2674
2675 /* Parse a primary-expression.
2676
2677    primary-expression:
2678      literal
2679      this
2680      ( expression )
2681      id-expression
2682
2683    GNU Extensions:
2684
2685    primary-expression:
2686      ( compound-statement )
2687      __builtin_va_arg ( assignment-expression , type-id )
2688
2689    literal:
2690      __null
2691
2692    Returns a representation of the expression.
2693
2694    *IDK indicates what kind of id-expression (if any) was present.
2695
2696    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2697    used as the operand of a pointer-to-member.  In that case,
2698    *QUALIFYING_CLASS gives the class that is used as the qualifying
2699    class in the pointer-to-member.  */
2700
2701 static tree
2702 cp_parser_primary_expression (cp_parser *parser,
2703                               cp_id_kind *idk,
2704                               tree *qualifying_class)
2705 {
2706   cp_token *token;
2707
2708   /* Assume the primary expression is not an id-expression.  */
2709   *idk = CP_ID_KIND_NONE;
2710   /* And that it cannot be used as pointer-to-member.  */
2711   *qualifying_class = NULL_TREE;
2712
2713   /* Peek at the next token.  */
2714   token = cp_lexer_peek_token (parser->lexer);
2715   switch (token->type)
2716     {
2717       /* literal:
2718            integer-literal
2719            character-literal
2720            floating-literal
2721            string-literal
2722            boolean-literal  */
2723     case CPP_CHAR:
2724     case CPP_WCHAR:
2725     case CPP_NUMBER:
2726       token = cp_lexer_consume_token (parser->lexer);
2727       return token->value;
2728
2729     case CPP_STRING:
2730     case CPP_WSTRING:
2731       token = cp_lexer_consume_token (parser->lexer);
2732       if (TREE_CHAIN (token->value))
2733         return TREE_CHAIN (token->value);
2734       else
2735         return token->value;
2736
2737     case CPP_OPEN_PAREN:
2738       {
2739         tree expr;
2740         bool saved_greater_than_is_operator_p;
2741
2742         /* Consume the `('.  */
2743         cp_lexer_consume_token (parser->lexer);
2744         /* Within a parenthesized expression, a `>' token is always
2745            the greater-than operator.  */
2746         saved_greater_than_is_operator_p
2747           = parser->greater_than_is_operator_p;
2748         parser->greater_than_is_operator_p = true;
2749         /* If we see `( { ' then we are looking at the beginning of
2750            a GNU statement-expression.  */
2751         if (cp_parser_allow_gnu_extensions_p (parser)
2752             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2753           {
2754             /* Statement-expressions are not allowed by the standard.  */
2755             if (pedantic)
2756               pedwarn ("ISO C++ forbids braced-groups within expressions");
2757
2758             /* And they're not allowed outside of a function-body; you
2759                cannot, for example, write:
2760
2761                  int i = ({ int j = 3; j + 1; });
2762
2763                at class or namespace scope.  */
2764             if (!at_function_scope_p ())
2765               error ("statement-expressions are allowed only inside functions");
2766             /* Start the statement-expression.  */
2767             expr = begin_stmt_expr ();
2768             /* Parse the compound-statement.  */
2769             cp_parser_compound_statement (parser, expr, false);
2770             /* Finish up.  */
2771             expr = finish_stmt_expr (expr, false);
2772           }
2773         else
2774           {
2775             /* Parse the parenthesized expression.  */
2776             expr = cp_parser_expression (parser);
2777             /* Let the front end know that this expression was
2778                enclosed in parentheses. This matters in case, for
2779                example, the expression is of the form `A::B', since
2780                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2781                not.  */
2782             finish_parenthesized_expr (expr);
2783           }
2784         /* The `>' token might be the end of a template-id or
2785            template-parameter-list now.  */
2786         parser->greater_than_is_operator_p
2787           = saved_greater_than_is_operator_p;
2788         /* Consume the `)'.  */
2789         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2790           cp_parser_skip_to_end_of_statement (parser);
2791
2792         return expr;
2793       }
2794
2795     case CPP_KEYWORD:
2796       switch (token->keyword)
2797         {
2798           /* These two are the boolean literals.  */
2799         case RID_TRUE:
2800           cp_lexer_consume_token (parser->lexer);
2801           return boolean_true_node;
2802         case RID_FALSE:
2803           cp_lexer_consume_token (parser->lexer);
2804           return boolean_false_node;
2805
2806           /* The `__null' literal.  */
2807         case RID_NULL:
2808           cp_lexer_consume_token (parser->lexer);
2809           return null_node;
2810
2811           /* Recognize the `this' keyword.  */
2812         case RID_THIS:
2813           cp_lexer_consume_token (parser->lexer);
2814           if (parser->local_variables_forbidden_p)
2815             {
2816               error ("`this' may not be used in this context");
2817               return error_mark_node;
2818             }
2819           /* Pointers cannot appear in constant-expressions.  */
2820           if (cp_parser_non_integral_constant_expression (parser,
2821                                                           "`this'"))
2822             return error_mark_node;
2823           return finish_this_expr ();
2824
2825           /* The `operator' keyword can be the beginning of an
2826              id-expression.  */
2827         case RID_OPERATOR:
2828           goto id_expression;
2829
2830         case RID_FUNCTION_NAME:
2831         case RID_PRETTY_FUNCTION_NAME:
2832         case RID_C99_FUNCTION_NAME:
2833           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2834              __func__ are the names of variables -- but they are
2835              treated specially.  Therefore, they are handled here,
2836              rather than relying on the generic id-expression logic
2837              below.  Grammatically, these names are id-expressions.
2838
2839              Consume the token.  */
2840           token = cp_lexer_consume_token (parser->lexer);
2841           /* Look up the name.  */
2842           return finish_fname (token->value);
2843
2844         case RID_VA_ARG:
2845           {
2846             tree expression;
2847             tree type;
2848
2849             /* The `__builtin_va_arg' construct is used to handle
2850                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2851             cp_lexer_consume_token (parser->lexer);
2852             /* Look for the opening `('.  */
2853             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2854             /* Now, parse the assignment-expression.  */
2855             expression = cp_parser_assignment_expression (parser);
2856             /* Look for the `,'.  */
2857             cp_parser_require (parser, CPP_COMMA, "`,'");
2858             /* Parse the type-id.  */
2859             type = cp_parser_type_id (parser);
2860             /* Look for the closing `)'.  */
2861             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2862             /* Using `va_arg' in a constant-expression is not
2863                allowed.  */
2864             if (cp_parser_non_integral_constant_expression (parser,
2865                                                             "`va_arg'"))
2866               return error_mark_node;
2867             return build_x_va_arg (expression, type);
2868           }
2869
2870         case RID_OFFSETOF:
2871           return cp_parser_builtin_offsetof (parser);
2872
2873         default:
2874           cp_parser_error (parser, "expected primary-expression");
2875           return error_mark_node;
2876         }
2877
2878       /* An id-expression can start with either an identifier, a
2879          `::' as the beginning of a qualified-id, or the "operator"
2880          keyword.  */
2881     case CPP_NAME:
2882     case CPP_SCOPE:
2883     case CPP_TEMPLATE_ID:
2884     case CPP_NESTED_NAME_SPECIFIER:
2885       {
2886         tree id_expression;
2887         tree decl;
2888         const char *error_msg;
2889
2890       id_expression:
2891         /* Parse the id-expression.  */
2892         id_expression
2893           = cp_parser_id_expression (parser,
2894                                      /*template_keyword_p=*/false,
2895                                      /*check_dependency_p=*/true,
2896                                      /*template_p=*/NULL,
2897                                      /*declarator_p=*/false);
2898         if (id_expression == error_mark_node)
2899           return error_mark_node;
2900         /* If we have a template-id, then no further lookup is
2901            required.  If the template-id was for a template-class, we
2902            will sometimes have a TYPE_DECL at this point.  */
2903         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2904             || TREE_CODE (id_expression) == TYPE_DECL)
2905           decl = id_expression;
2906         /* Look up the name.  */
2907         else
2908           {
2909             decl = cp_parser_lookup_name_simple (parser, id_expression);
2910             /* If name lookup gives us a SCOPE_REF, then the
2911                qualifying scope was dependent.  Just propagate the
2912                name.  */
2913             if (TREE_CODE (decl) == SCOPE_REF)
2914               {
2915                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2916                   *qualifying_class = TREE_OPERAND (decl, 0);
2917                 return decl;
2918               }
2919             /* Check to see if DECL is a local variable in a context
2920                where that is forbidden.  */
2921             if (parser->local_variables_forbidden_p
2922                 && local_variable_p (decl))
2923               {
2924                 /* It might be that we only found DECL because we are
2925                    trying to be generous with pre-ISO scoping rules.
2926                    For example, consider:
2927
2928                      int i;
2929                      void g() {
2930                        for (int i = 0; i < 10; ++i) {}
2931                        extern void f(int j = i);
2932                      }
2933
2934                    Here, name look up will originally find the out
2935                    of scope `i'.  We need to issue a warning message,
2936                    but then use the global `i'.  */
2937                 decl = check_for_out_of_scope_variable (decl);
2938                 if (local_variable_p (decl))
2939                   {
2940                     error ("local variable `%D' may not appear in this context",
2941                            decl);
2942                     return error_mark_node;
2943                   }
2944               }
2945           }
2946
2947         decl = finish_id_expression (id_expression, decl, parser->scope,
2948                                      idk, qualifying_class,
2949                                      parser->integral_constant_expression_p,
2950                                      parser->allow_non_integral_constant_expression_p,
2951                                      &parser->non_integral_constant_expression_p,
2952                                      &error_msg);
2953         if (error_msg)
2954           cp_parser_error (parser, error_msg);
2955         return decl;
2956       }
2957
2958       /* Anything else is an error.  */
2959     default:
2960       cp_parser_error (parser, "expected primary-expression");
2961       return error_mark_node;
2962     }
2963 }
2964
2965 /* Parse an id-expression.
2966
2967    id-expression:
2968      unqualified-id
2969      qualified-id
2970
2971    qualified-id:
2972      :: [opt] nested-name-specifier template [opt] unqualified-id
2973      :: identifier
2974      :: operator-function-id
2975      :: template-id
2976
2977    Return a representation of the unqualified portion of the
2978    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2979    a `::' or nested-name-specifier.
2980
2981    Often, if the id-expression was a qualified-id, the caller will
2982    want to make a SCOPE_REF to represent the qualified-id.  This
2983    function does not do this in order to avoid wastefully creating
2984    SCOPE_REFs when they are not required.
2985
2986    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2987    `template' keyword.
2988
2989    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2990    uninstantiated templates.
2991
2992    If *TEMPLATE_P is non-NULL, it is set to true iff the
2993    `template' keyword is used to explicitly indicate that the entity
2994    named is a template.
2995
2996    If DECLARATOR_P is true, the id-expression is appearing as part of
2997    a declarator, rather than as part of an expression.  */
2998
2999 static tree
3000 cp_parser_id_expression (cp_parser *parser,
3001                          bool template_keyword_p,
3002                          bool check_dependency_p,
3003                          bool *template_p,
3004                          bool declarator_p)
3005 {
3006   bool global_scope_p;
3007   bool nested_name_specifier_p;
3008
3009   /* Assume the `template' keyword was not used.  */
3010   if (template_p)
3011     *template_p = false;
3012
3013   /* Look for the optional `::' operator.  */
3014   global_scope_p
3015     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3016        != NULL_TREE);
3017   /* Look for the optional nested-name-specifier.  */
3018   nested_name_specifier_p
3019     = (cp_parser_nested_name_specifier_opt (parser,
3020                                             /*typename_keyword_p=*/false,
3021                                             check_dependency_p,
3022                                             /*type_p=*/false,
3023                                             /*is_declarator=*/false)
3024        != NULL_TREE);
3025   /* If there is a nested-name-specifier, then we are looking at
3026      the first qualified-id production.  */
3027   if (nested_name_specifier_p)
3028     {
3029       tree saved_scope;
3030       tree saved_object_scope;
3031       tree saved_qualifying_scope;
3032       tree unqualified_id;
3033       bool is_template;
3034
3035       /* See if the next token is the `template' keyword.  */
3036       if (!template_p)
3037         template_p = &is_template;
3038       *template_p = cp_parser_optional_template_keyword (parser);
3039       /* Name lookup we do during the processing of the
3040          unqualified-id might obliterate SCOPE.  */
3041       saved_scope = parser->scope;
3042       saved_object_scope = parser->object_scope;
3043       saved_qualifying_scope = parser->qualifying_scope;
3044       /* Process the final unqualified-id.  */
3045       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3046                                                  check_dependency_p,
3047                                                  declarator_p);
3048       /* Restore the SAVED_SCOPE for our caller.  */
3049       parser->scope = saved_scope;
3050       parser->object_scope = saved_object_scope;
3051       parser->qualifying_scope = saved_qualifying_scope;
3052
3053       return unqualified_id;
3054     }
3055   /* Otherwise, if we are in global scope, then we are looking at one
3056      of the other qualified-id productions.  */
3057   else if (global_scope_p)
3058     {
3059       cp_token *token;
3060       tree id;
3061
3062       /* Peek at the next token.  */
3063       token = cp_lexer_peek_token (parser->lexer);
3064
3065       /* If it's an identifier, and the next token is not a "<", then
3066          we can avoid the template-id case.  This is an optimization
3067          for this common case.  */
3068       if (token->type == CPP_NAME
3069           && !cp_parser_nth_token_starts_template_argument_list_p
3070                (parser, 2))
3071         return cp_parser_identifier (parser);
3072
3073       cp_parser_parse_tentatively (parser);
3074       /* Try a template-id.  */
3075       id = cp_parser_template_id (parser,
3076                                   /*template_keyword_p=*/false,
3077                                   /*check_dependency_p=*/true,
3078                                   declarator_p);
3079       /* If that worked, we're done.  */
3080       if (cp_parser_parse_definitely (parser))
3081         return id;
3082
3083       /* Peek at the next token.  (Changes in the token buffer may
3084          have invalidated the pointer obtained above.)  */
3085       token = cp_lexer_peek_token (parser->lexer);
3086
3087       switch (token->type)
3088         {
3089         case CPP_NAME:
3090           return cp_parser_identifier (parser);
3091
3092         case CPP_KEYWORD:
3093           if (token->keyword == RID_OPERATOR)
3094             return cp_parser_operator_function_id (parser);
3095           /* Fall through.  */
3096
3097         default:
3098           cp_parser_error (parser, "expected id-expression");
3099           return error_mark_node;
3100         }
3101     }
3102   else
3103     return cp_parser_unqualified_id (parser, template_keyword_p,
3104                                      /*check_dependency_p=*/true,
3105                                      declarator_p);
3106 }
3107
3108 /* Parse an unqualified-id.
3109
3110    unqualified-id:
3111      identifier
3112      operator-function-id
3113      conversion-function-id
3114      ~ class-name
3115      template-id
3116
3117    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3118    keyword, in a construct like `A::template ...'.
3119
3120    Returns a representation of unqualified-id.  For the `identifier'
3121    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3122    production a BIT_NOT_EXPR is returned; the operand of the
3123    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3124    other productions, see the documentation accompanying the
3125    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3126    names are looked up in uninstantiated templates.  If DECLARATOR_P
3127    is true, the unqualified-id is appearing as part of a declarator,
3128    rather than as part of an expression.  */
3129
3130 static tree
3131 cp_parser_unqualified_id (cp_parser* parser,
3132                           bool template_keyword_p,
3133                           bool check_dependency_p,
3134                           bool declarator_p)
3135 {
3136   cp_token *token;
3137
3138   /* Peek at the next token.  */
3139   token = cp_lexer_peek_token (parser->lexer);
3140
3141   switch (token->type)
3142     {
3143     case CPP_NAME:
3144       {
3145         tree id;
3146
3147         /* We don't know yet whether or not this will be a
3148            template-id.  */
3149         cp_parser_parse_tentatively (parser);
3150         /* Try a template-id.  */
3151         id = cp_parser_template_id (parser, template_keyword_p,
3152                                     check_dependency_p,
3153                                     declarator_p);
3154         /* If it worked, we're done.  */
3155         if (cp_parser_parse_definitely (parser))
3156           return id;
3157         /* Otherwise, it's an ordinary identifier.  */
3158         return cp_parser_identifier (parser);
3159       }
3160
3161     case CPP_TEMPLATE_ID:
3162       return cp_parser_template_id (parser, template_keyword_p,
3163                                     check_dependency_p,
3164                                     declarator_p);
3165
3166     case CPP_COMPL:
3167       {
3168         tree type_decl;
3169         tree qualifying_scope;
3170         tree object_scope;
3171         tree scope;
3172
3173         /* Consume the `~' token.  */
3174         cp_lexer_consume_token (parser->lexer);
3175         /* Parse the class-name.  The standard, as written, seems to
3176            say that:
3177
3178              template <typename T> struct S { ~S (); };
3179              template <typename T> S<T>::~S() {}
3180
3181            is invalid, since `~' must be followed by a class-name, but
3182            `S<T>' is dependent, and so not known to be a class.
3183            That's not right; we need to look in uninstantiated
3184            templates.  A further complication arises from:
3185
3186              template <typename T> void f(T t) {
3187                t.T::~T();
3188              }
3189
3190            Here, it is not possible to look up `T' in the scope of `T'
3191            itself.  We must look in both the current scope, and the
3192            scope of the containing complete expression.
3193
3194            Yet another issue is:
3195
3196              struct S {
3197                int S;
3198                ~S();
3199              };
3200
3201              S::~S() {}
3202
3203            The standard does not seem to say that the `S' in `~S'
3204            should refer to the type `S' and not the data member
3205            `S::S'.  */
3206
3207         /* DR 244 says that we look up the name after the "~" in the
3208            same scope as we looked up the qualifying name.  That idea
3209            isn't fully worked out; it's more complicated than that.  */
3210         scope = parser->scope;
3211         object_scope = parser->object_scope;
3212         qualifying_scope = parser->qualifying_scope;
3213
3214         /* If the name is of the form "X::~X" it's OK.  */
3215         if (scope && TYPE_P (scope)
3216             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3217             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3218                 == CPP_OPEN_PAREN)
3219             && (cp_lexer_peek_token (parser->lexer)->value
3220                 == TYPE_IDENTIFIER (scope)))
3221           {
3222             cp_lexer_consume_token (parser->lexer);
3223             return build_nt (BIT_NOT_EXPR, scope);
3224           }
3225
3226         /* If there was an explicit qualification (S::~T), first look
3227            in the scope given by the qualification (i.e., S).  */
3228         if (scope)
3229           {
3230             cp_parser_parse_tentatively (parser);
3231             type_decl = cp_parser_class_name (parser,
3232                                               /*typename_keyword_p=*/false,
3233                                               /*template_keyword_p=*/false,
3234                                               /*type_p=*/false,
3235                                               /*check_dependency=*/false,
3236                                               /*class_head_p=*/false,
3237                                               declarator_p);
3238             if (cp_parser_parse_definitely (parser))
3239               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3240           }
3241         /* In "N::S::~S", look in "N" as well.  */
3242         if (scope && qualifying_scope)
3243           {
3244             cp_parser_parse_tentatively (parser);
3245             parser->scope = qualifying_scope;
3246             parser->object_scope = NULL_TREE;
3247             parser->qualifying_scope = NULL_TREE;
3248             type_decl
3249               = cp_parser_class_name (parser,
3250                                       /*typename_keyword_p=*/false,
3251                                       /*template_keyword_p=*/false,
3252                                       /*type_p=*/false,
3253                                       /*check_dependency=*/false,
3254                                       /*class_head_p=*/false,
3255                                       declarator_p);
3256             if (cp_parser_parse_definitely (parser))
3257               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3258           }
3259         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3260         else if (object_scope)
3261           {
3262             cp_parser_parse_tentatively (parser);
3263             parser->scope = object_scope;
3264             parser->object_scope = NULL_TREE;
3265             parser->qualifying_scope = NULL_TREE;
3266             type_decl
3267               = cp_parser_class_name (parser,
3268                                       /*typename_keyword_p=*/false,
3269                                       /*template_keyword_p=*/false,
3270                                       /*type_p=*/false,
3271                                       /*check_dependency=*/false,
3272                                       /*class_head_p=*/false,
3273                                       declarator_p);
3274             if (cp_parser_parse_definitely (parser))
3275               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3276           }
3277         /* Look in the surrounding context.  */
3278         parser->scope = NULL_TREE;
3279         parser->object_scope = NULL_TREE;
3280         parser->qualifying_scope = NULL_TREE;
3281         type_decl
3282           = cp_parser_class_name (parser,
3283                                   /*typename_keyword_p=*/false,
3284                                   /*template_keyword_p=*/false,
3285                                   /*type_p=*/false,
3286                                   /*check_dependency=*/false,
3287                                   /*class_head_p=*/false,
3288                                   declarator_p);
3289         /* If an error occurred, assume that the name of the
3290            destructor is the same as the name of the qualifying
3291            class.  That allows us to keep parsing after running
3292            into ill-formed destructor names.  */
3293         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3294           return build_nt (BIT_NOT_EXPR, scope);
3295         else if (type_decl == error_mark_node)
3296           return error_mark_node;
3297
3298         /* [class.dtor]
3299
3300            A typedef-name that names a class shall not be used as the
3301            identifier in the declarator for a destructor declaration.  */
3302         if (declarator_p
3303             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3304             && !DECL_SELF_REFERENCE_P (type_decl))
3305           error ("typedef-name `%D' used as destructor declarator",
3306                  type_decl);
3307
3308         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3309       }
3310
3311     case CPP_KEYWORD:
3312       if (token->keyword == RID_OPERATOR)
3313         {
3314           tree id;
3315
3316           /* This could be a template-id, so we try that first.  */
3317           cp_parser_parse_tentatively (parser);
3318           /* Try a template-id.  */
3319           id = cp_parser_template_id (parser, template_keyword_p,
3320                                       /*check_dependency_p=*/true,
3321                                       declarator_p);
3322           /* If that worked, we're done.  */
3323           if (cp_parser_parse_definitely (parser))
3324             return id;
3325           /* We still don't know whether we're looking at an
3326              operator-function-id or a conversion-function-id.  */
3327           cp_parser_parse_tentatively (parser);
3328           /* Try an operator-function-id.  */
3329           id = cp_parser_operator_function_id (parser);
3330           /* If that didn't work, try a conversion-function-id.  */
3331           if (!cp_parser_parse_definitely (parser))
3332             id = cp_parser_conversion_function_id (parser);
3333
3334           return id;
3335         }
3336       /* Fall through.  */
3337
3338     default:
3339       cp_parser_error (parser, "expected unqualified-id");
3340       return error_mark_node;
3341     }
3342 }
3343
3344 /* Parse an (optional) nested-name-specifier.
3345
3346    nested-name-specifier:
3347      class-or-namespace-name :: nested-name-specifier [opt]
3348      class-or-namespace-name :: template nested-name-specifier [opt]
3349
3350    PARSER->SCOPE should be set appropriately before this function is
3351    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3352    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3353    in name lookups.
3354
3355    Sets PARSER->SCOPE to the class (TYPE) or namespace
3356    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3357    it unchanged if there is no nested-name-specifier.  Returns the new
3358    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3359
3360    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3361    part of a declaration and/or decl-specifier.  */
3362
3363 static tree
3364 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3365                                      bool typename_keyword_p,
3366                                      bool check_dependency_p,
3367                                      bool type_p,
3368                                      bool is_declaration)
3369 {
3370   bool success = false;
3371   tree access_check = NULL_TREE;
3372   ptrdiff_t start;
3373   cp_token* token;
3374
3375   /* If the next token corresponds to a nested name specifier, there
3376      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3377      false, it may have been true before, in which case something
3378      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3379      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3380      CHECK_DEPENDENCY_P is false, we have to fall through into the
3381      main loop.  */
3382   if (check_dependency_p
3383       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3384     {
3385       cp_parser_pre_parsed_nested_name_specifier (parser);
3386       return parser->scope;
3387     }
3388
3389   /* Remember where the nested-name-specifier starts.  */
3390   if (cp_parser_parsing_tentatively (parser)
3391       && !cp_parser_committed_to_tentative_parse (parser))
3392     {
3393       token = cp_lexer_peek_token (parser->lexer);
3394       start = cp_lexer_token_difference (parser->lexer,
3395                                          parser->lexer->first_token,
3396                                          token);
3397     }
3398   else
3399     start = -1;
3400
3401   push_deferring_access_checks (dk_deferred);
3402
3403   while (true)
3404     {
3405       tree new_scope;
3406       tree old_scope;
3407       tree saved_qualifying_scope;
3408       bool template_keyword_p;
3409
3410       /* Spot cases that cannot be the beginning of a
3411          nested-name-specifier.  */
3412       token = cp_lexer_peek_token (parser->lexer);
3413
3414       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3415          the already parsed nested-name-specifier.  */
3416       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3417         {
3418           /* Grab the nested-name-specifier and continue the loop.  */
3419           cp_parser_pre_parsed_nested_name_specifier (parser);
3420           success = true;
3421           continue;
3422         }
3423
3424       /* Spot cases that cannot be the beginning of a
3425          nested-name-specifier.  On the second and subsequent times
3426          through the loop, we look for the `template' keyword.  */
3427       if (success && token->keyword == RID_TEMPLATE)
3428         ;
3429       /* A template-id can start a nested-name-specifier.  */
3430       else if (token->type == CPP_TEMPLATE_ID)
3431         ;
3432       else
3433         {
3434           /* If the next token is not an identifier, then it is
3435              definitely not a class-or-namespace-name.  */
3436           if (token->type != CPP_NAME)
3437             break;
3438           /* If the following token is neither a `<' (to begin a
3439              template-id), nor a `::', then we are not looking at a
3440              nested-name-specifier.  */
3441           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3442           if (token->type != CPP_SCOPE
3443               && !cp_parser_nth_token_starts_template_argument_list_p
3444                   (parser, 2))
3445             break;
3446         }
3447
3448       /* The nested-name-specifier is optional, so we parse
3449          tentatively.  */
3450       cp_parser_parse_tentatively (parser);
3451
3452       /* Look for the optional `template' keyword, if this isn't the
3453          first time through the loop.  */
3454       if (success)
3455         template_keyword_p = cp_parser_optional_template_keyword (parser);
3456       else
3457         template_keyword_p = false;
3458
3459       /* Save the old scope since the name lookup we are about to do
3460          might destroy it.  */
3461       old_scope = parser->scope;
3462       saved_qualifying_scope = parser->qualifying_scope;
3463       /* Parse the qualifying entity.  */
3464       new_scope
3465         = cp_parser_class_or_namespace_name (parser,
3466                                              typename_keyword_p,
3467                                              template_keyword_p,
3468                                              check_dependency_p,
3469                                              type_p,
3470                                              is_declaration);
3471       /* Look for the `::' token.  */
3472       cp_parser_require (parser, CPP_SCOPE, "`::'");
3473
3474       /* If we found what we wanted, we keep going; otherwise, we're
3475          done.  */
3476       if (!cp_parser_parse_definitely (parser))
3477         {
3478           bool error_p = false;
3479
3480           /* Restore the OLD_SCOPE since it was valid before the
3481              failed attempt at finding the last
3482              class-or-namespace-name.  */
3483           parser->scope = old_scope;
3484           parser->qualifying_scope = saved_qualifying_scope;
3485           /* If the next token is an identifier, and the one after
3486              that is a `::', then any valid interpretation would have
3487              found a class-or-namespace-name.  */
3488           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3489                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3490                      == CPP_SCOPE)
3491                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3492                      != CPP_COMPL))
3493             {
3494               token = cp_lexer_consume_token (parser->lexer);
3495               if (!error_p)
3496                 {
3497                   tree decl;
3498
3499                   decl = cp_parser_lookup_name_simple (parser, token->value);
3500                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3501                     error ("`%D' used without template parameters",
3502                            decl);
3503                   else
3504                     cp_parser_name_lookup_error
3505                       (parser, token->value, decl,
3506                        "is not a class or namespace");
3507                   parser->scope = NULL_TREE;
3508                   error_p = true;
3509                   /* Treat this as a successful nested-name-specifier
3510                      due to:
3511
3512                      [basic.lookup.qual]
3513
3514                      If the name found is not a class-name (clause
3515                      _class_) or namespace-name (_namespace.def_), the
3516                      program is ill-formed.  */
3517                   success = true;
3518                 }
3519               cp_lexer_consume_token (parser->lexer);
3520             }
3521           break;
3522         }
3523
3524       /* We've found one valid nested-name-specifier.  */
3525       success = true;
3526       /* Make sure we look in the right scope the next time through
3527          the loop.  */
3528       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3529                        ? TREE_TYPE (new_scope)
3530                        : new_scope);
3531       /* If it is a class scope, try to complete it; we are about to
3532          be looking up names inside the class.  */
3533       if (TYPE_P (parser->scope)
3534           /* Since checking types for dependency can be expensive,
3535              avoid doing it if the type is already complete.  */
3536           && !COMPLETE_TYPE_P (parser->scope)
3537           /* Do not try to complete dependent types.  */
3538           && !dependent_type_p (parser->scope))
3539         complete_type (parser->scope);
3540     }
3541
3542   /* Retrieve any deferred checks.  Do not pop this access checks yet
3543      so the memory will not be reclaimed during token replacing below.  */
3544   access_check = get_deferred_access_checks ();
3545
3546   /* If parsing tentatively, replace the sequence of tokens that makes
3547      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3548      token.  That way, should we re-parse the token stream, we will
3549      not have to repeat the effort required to do the parse, nor will
3550      we issue duplicate error messages.  */
3551   if (success && start >= 0)
3552     {
3553       /* Find the token that corresponds to the start of the
3554          template-id.  */
3555       token = cp_lexer_advance_token (parser->lexer,
3556                                       parser->lexer->first_token,
3557                                       start);
3558
3559       /* Reset the contents of the START token.  */
3560       token->type = CPP_NESTED_NAME_SPECIFIER;
3561       token->value = build_tree_list (access_check, parser->scope);
3562       TREE_TYPE (token->value) = parser->qualifying_scope;
3563       token->keyword = RID_MAX;
3564       /* Purge all subsequent tokens.  */
3565       cp_lexer_purge_tokens_after (parser->lexer, token);
3566     }
3567
3568   pop_deferring_access_checks ();
3569   return success ? parser->scope : NULL_TREE;
3570 }
3571
3572 /* Parse a nested-name-specifier.  See
3573    cp_parser_nested_name_specifier_opt for details.  This function
3574    behaves identically, except that it will an issue an error if no
3575    nested-name-specifier is present, and it will return
3576    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3577    is present.  */
3578
3579 static tree
3580 cp_parser_nested_name_specifier (cp_parser *parser,
3581                                  bool typename_keyword_p,
3582                                  bool check_dependency_p,
3583                                  bool type_p,
3584                                  bool is_declaration)
3585 {
3586   tree scope;
3587
3588   /* Look for the nested-name-specifier.  */
3589   scope = cp_parser_nested_name_specifier_opt (parser,
3590                                                typename_keyword_p,
3591                                                check_dependency_p,
3592                                                type_p,
3593                                                is_declaration);
3594   /* If it was not present, issue an error message.  */
3595   if (!scope)
3596     {
3597       cp_parser_error (parser, "expected nested-name-specifier");
3598       parser->scope = NULL_TREE;
3599       return error_mark_node;
3600     }
3601
3602   return scope;
3603 }
3604
3605 /* Parse a class-or-namespace-name.
3606
3607    class-or-namespace-name:
3608      class-name
3609      namespace-name
3610
3611    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3612    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3613    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3614    TYPE_P is TRUE iff the next name should be taken as a class-name,
3615    even the same name is declared to be another entity in the same
3616    scope.
3617
3618    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3619    specified by the class-or-namespace-name.  If neither is found the
3620    ERROR_MARK_NODE is returned.  */
3621
3622 static tree
3623 cp_parser_class_or_namespace_name (cp_parser *parser,
3624                                    bool typename_keyword_p,
3625                                    bool template_keyword_p,
3626                                    bool check_dependency_p,
3627                                    bool type_p,
3628                                    bool is_declaration)
3629 {
3630   tree saved_scope;
3631   tree saved_qualifying_scope;
3632   tree saved_object_scope;
3633   tree scope;
3634   bool only_class_p;
3635
3636   /* Before we try to parse the class-name, we must save away the
3637      current PARSER->SCOPE since cp_parser_class_name will destroy
3638      it.  */
3639   saved_scope = parser->scope;
3640   saved_qualifying_scope = parser->qualifying_scope;
3641   saved_object_scope = parser->object_scope;
3642   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3643      there is no need to look for a namespace-name.  */
3644   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3645   if (!only_class_p)
3646     cp_parser_parse_tentatively (parser);
3647   scope = cp_parser_class_name (parser,
3648                                 typename_keyword_p,
3649                                 template_keyword_p,
3650                                 type_p,
3651                                 check_dependency_p,
3652                                 /*class_head_p=*/false,
3653                                 is_declaration);
3654   /* If that didn't work, try for a namespace-name.  */
3655   if (!only_class_p && !cp_parser_parse_definitely (parser))
3656     {
3657       /* Restore the saved scope.  */
3658       parser->scope = saved_scope;
3659       parser->qualifying_scope = saved_qualifying_scope;
3660       parser->object_scope = saved_object_scope;
3661       /* If we are not looking at an identifier followed by the scope
3662          resolution operator, then this is not part of a
3663          nested-name-specifier.  (Note that this function is only used
3664          to parse the components of a nested-name-specifier.)  */
3665       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3666           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3667         return error_mark_node;
3668       scope = cp_parser_namespace_name (parser);
3669     }
3670
3671   return scope;
3672 }
3673
3674 /* Parse a postfix-expression.
3675
3676    postfix-expression:
3677      primary-expression
3678      postfix-expression [ expression ]
3679      postfix-expression ( expression-list [opt] )
3680      simple-type-specifier ( expression-list [opt] )
3681      typename :: [opt] nested-name-specifier identifier
3682        ( expression-list [opt] )
3683      typename :: [opt] nested-name-specifier template [opt] template-id
3684        ( expression-list [opt] )
3685      postfix-expression . template [opt] id-expression
3686      postfix-expression -> template [opt] id-expression
3687      postfix-expression . pseudo-destructor-name
3688      postfix-expression -> pseudo-destructor-name
3689      postfix-expression ++
3690      postfix-expression --
3691      dynamic_cast < type-id > ( expression )
3692      static_cast < type-id > ( expression )
3693      reinterpret_cast < type-id > ( expression )
3694      const_cast < type-id > ( expression )
3695      typeid ( expression )
3696      typeid ( type-id )
3697
3698    GNU Extension:
3699
3700    postfix-expression:
3701      ( type-id ) { initializer-list , [opt] }
3702
3703    This extension is a GNU version of the C99 compound-literal
3704    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3705    but they are essentially the same concept.)
3706
3707    If ADDRESS_P is true, the postfix expression is the operand of the
3708    `&' operator.
3709
3710    Returns a representation of the expression.  */
3711
3712 static tree
3713 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3714 {
3715   cp_token *token;
3716   enum rid keyword;
3717   cp_id_kind idk = CP_ID_KIND_NONE;
3718   tree postfix_expression = NULL_TREE;
3719   /* Non-NULL only if the current postfix-expression can be used to
3720      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3721      class used to qualify the member.  */
3722   tree qualifying_class = NULL_TREE;
3723
3724   /* Peek at the next token.  */
3725   token = cp_lexer_peek_token (parser->lexer);
3726   /* Some of the productions are determined by keywords.  */
3727   keyword = token->keyword;
3728   switch (keyword)
3729     {
3730     case RID_DYNCAST:
3731     case RID_STATCAST:
3732     case RID_REINTCAST:
3733     case RID_CONSTCAST:
3734       {
3735         tree type;
3736         tree expression;
3737         const char *saved_message;
3738
3739         /* All of these can be handled in the same way from the point
3740            of view of parsing.  Begin by consuming the token
3741            identifying the cast.  */
3742         cp_lexer_consume_token (parser->lexer);
3743
3744         /* New types cannot be defined in the cast.  */
3745         saved_message = parser->type_definition_forbidden_message;
3746         parser->type_definition_forbidden_message
3747           = "types may not be defined in casts";
3748
3749         /* Look for the opening `<'.  */
3750         cp_parser_require (parser, CPP_LESS, "`<'");
3751         /* Parse the type to which we are casting.  */
3752         type = cp_parser_type_id (parser);
3753         /* Look for the closing `>'.  */
3754         cp_parser_require (parser, CPP_GREATER, "`>'");
3755         /* Restore the old message.  */
3756         parser->type_definition_forbidden_message = saved_message;
3757
3758         /* And the expression which is being cast.  */
3759         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3760         expression = cp_parser_expression (parser);
3761         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3762
3763         /* Only type conversions to integral or enumeration types
3764            can be used in constant-expressions.  */
3765         if (parser->integral_constant_expression_p
3766             && !dependent_type_p (type)
3767             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3768             && (cp_parser_non_integral_constant_expression 
3769                 (parser,
3770                  "a cast to a type other than an integral or "
3771                  "enumeration type")))
3772           return error_mark_node;
3773
3774         switch (keyword)
3775           {
3776           case RID_DYNCAST:
3777             postfix_expression
3778               = build_dynamic_cast (type, expression);
3779             break;
3780           case RID_STATCAST:
3781             postfix_expression
3782               = build_static_cast (type, expression);
3783             break;
3784           case RID_REINTCAST:
3785             postfix_expression
3786               = build_reinterpret_cast (type, expression);
3787             break;
3788           case RID_CONSTCAST:
3789             postfix_expression
3790               = build_const_cast (type, expression);
3791             break;
3792           default:
3793             abort ();
3794           }
3795       }
3796       break;
3797
3798     case RID_TYPEID:
3799       {
3800         tree type;
3801         const char *saved_message;
3802         bool saved_in_type_id_in_expr_p;
3803
3804         /* Consume the `typeid' token.  */
3805         cp_lexer_consume_token (parser->lexer);
3806         /* Look for the `(' token.  */
3807         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3808         /* Types cannot be defined in a `typeid' expression.  */
3809         saved_message = parser->type_definition_forbidden_message;
3810         parser->type_definition_forbidden_message
3811           = "types may not be defined in a `typeid\' expression";
3812         /* We can't be sure yet whether we're looking at a type-id or an
3813            expression.  */
3814         cp_parser_parse_tentatively (parser);
3815         /* Try a type-id first.  */
3816         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3817         parser->in_type_id_in_expr_p = true;
3818         type = cp_parser_type_id (parser);
3819         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3820         /* Look for the `)' token.  Otherwise, we can't be sure that
3821            we're not looking at an expression: consider `typeid (int
3822            (3))', for example.  */
3823         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3824         /* If all went well, simply lookup the type-id.  */
3825         if (cp_parser_parse_definitely (parser))
3826           postfix_expression = get_typeid (type);
3827         /* Otherwise, fall back to the expression variant.  */
3828         else
3829           {
3830             tree expression;
3831
3832             /* Look for an expression.  */
3833             expression = cp_parser_expression (parser);
3834             /* Compute its typeid.  */
3835             postfix_expression = build_typeid (expression);
3836             /* Look for the `)' token.  */
3837             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3838           }
3839         /* `typeid' may not appear in an integral constant expression.  */
3840         if (cp_parser_non_integral_constant_expression(parser, 
3841                                                        "`typeid' operator"))
3842           return error_mark_node;
3843         /* Restore the saved message.  */
3844         parser->type_definition_forbidden_message = saved_message;
3845       }
3846       break;
3847
3848     case RID_TYPENAME:
3849       {
3850         bool template_p = false;
3851         tree id;
3852         tree type;
3853
3854         /* Consume the `typename' token.  */
3855         cp_lexer_consume_token (parser->lexer);
3856         /* Look for the optional `::' operator.  */
3857         cp_parser_global_scope_opt (parser,
3858                                     /*current_scope_valid_p=*/false);
3859         /* Look for the nested-name-specifier.  */
3860         cp_parser_nested_name_specifier (parser,
3861                                          /*typename_keyword_p=*/true,
3862                                          /*check_dependency_p=*/true,
3863                                          /*type_p=*/true,
3864                                          /*is_declaration=*/true);
3865         /* Look for the optional `template' keyword.  */
3866         template_p = cp_parser_optional_template_keyword (parser);
3867         /* We don't know whether we're looking at a template-id or an
3868            identifier.  */
3869         cp_parser_parse_tentatively (parser);
3870         /* Try a template-id.  */
3871         id = cp_parser_template_id (parser, template_p,
3872                                     /*check_dependency_p=*/true,
3873                                     /*is_declaration=*/true);
3874         /* If that didn't work, try an identifier.  */
3875         if (!cp_parser_parse_definitely (parser))
3876           id = cp_parser_identifier (parser);
3877         /* If we look up a template-id in a non-dependent qualifying
3878            scope, there's no need to create a dependent type.  */
3879         if (TREE_CODE (id) == TYPE_DECL
3880             && !dependent_type_p (parser->scope))
3881           type = TREE_TYPE (id);
3882         /* Create a TYPENAME_TYPE to represent the type to which the
3883            functional cast is being performed.  */
3884         else
3885           type = make_typename_type (parser->scope, id, 
3886                                      /*complain=*/1);
3887
3888         postfix_expression = cp_parser_functional_cast (parser, type);
3889       }
3890       break;
3891
3892     default:
3893       {
3894         tree type;
3895
3896         /* If the next thing is a simple-type-specifier, we may be
3897            looking at a functional cast.  We could also be looking at
3898            an id-expression.  So, we try the functional cast, and if
3899            that doesn't work we fall back to the primary-expression.  */
3900         cp_parser_parse_tentatively (parser);
3901         /* Look for the simple-type-specifier.  */
3902         type = cp_parser_simple_type_specifier (parser,
3903                                                 /*decl_specs=*/NULL,
3904                                                 CP_PARSER_FLAGS_NONE);
3905         /* Parse the cast itself.  */
3906         if (!cp_parser_error_occurred (parser))
3907           postfix_expression
3908             = cp_parser_functional_cast (parser, type);
3909         /* If that worked, we're done.  */
3910         if (cp_parser_parse_definitely (parser))
3911           break;
3912
3913         /* If the functional-cast didn't work out, try a
3914            compound-literal.  */
3915         if (cp_parser_allow_gnu_extensions_p (parser)
3916             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3917           {
3918             tree initializer_list = NULL_TREE;
3919             bool saved_in_type_id_in_expr_p;
3920
3921             cp_parser_parse_tentatively (parser);
3922             /* Consume the `('.  */
3923             cp_lexer_consume_token (parser->lexer);
3924             /* Parse the type.  */
3925             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3926             parser->in_type_id_in_expr_p = true;
3927             type = cp_parser_type_id (parser);
3928             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3929             /* Look for the `)'.  */
3930             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3931             /* Look for the `{'.  */
3932             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3933             /* If things aren't going well, there's no need to
3934                keep going.  */
3935             if (!cp_parser_error_occurred (parser))
3936               {
3937                 bool non_constant_p;
3938                 /* Parse the initializer-list.  */
3939                 initializer_list
3940                   = cp_parser_initializer_list (parser, &non_constant_p);
3941                 /* Allow a trailing `,'.  */
3942                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3943                   cp_lexer_consume_token (parser->lexer);
3944                 /* Look for the final `}'.  */
3945                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3946               }
3947             /* If that worked, we're definitely looking at a
3948                compound-literal expression.  */
3949             if (cp_parser_parse_definitely (parser))
3950               {
3951                 /* Warn the user that a compound literal is not
3952                    allowed in standard C++.  */
3953                 if (pedantic)
3954                   pedwarn ("ISO C++ forbids compound-literals");
3955                 /* Form the representation of the compound-literal.  */
3956                 postfix_expression
3957                   = finish_compound_literal (type, initializer_list);
3958                 break;
3959               }
3960           }
3961
3962         /* It must be a primary-expression.  */
3963         postfix_expression = cp_parser_primary_expression (parser,
3964                                                            &idk,
3965                                                            &qualifying_class);
3966       }
3967       break;
3968     }
3969
3970   /* If we were avoiding committing to the processing of a
3971      qualified-id until we knew whether or not we had a
3972      pointer-to-member, we now know.  */
3973   if (qualifying_class)
3974     {
3975       bool done;
3976
3977       /* Peek at the next token.  */
3978       token = cp_lexer_peek_token (parser->lexer);
3979       done = (token->type != CPP_OPEN_SQUARE
3980               && token->type != CPP_OPEN_PAREN
3981               && token->type != CPP_DOT
3982               && token->type != CPP_DEREF
3983               && token->type != CPP_PLUS_PLUS
3984               && token->type != CPP_MINUS_MINUS);
3985
3986       postfix_expression = finish_qualified_id_expr (qualifying_class,
3987                                                      postfix_expression,
3988                                                      done,
3989                                                      address_p);
3990       if (done)
3991         return postfix_expression;
3992     }
3993
3994   /* Keep looping until the postfix-expression is complete.  */
3995   while (true)
3996     {
3997       if (idk == CP_ID_KIND_UNQUALIFIED
3998           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3999           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4000         /* It is not a Koenig lookup function call.  */
4001         postfix_expression
4002           = unqualified_name_lookup_error (postfix_expression);
4003
4004       /* Peek at the next token.  */
4005       token = cp_lexer_peek_token (parser->lexer);
4006
4007       switch (token->type)
4008         {
4009         case CPP_OPEN_SQUARE:
4010           postfix_expression
4011             = cp_parser_postfix_open_square_expression (parser,
4012                                                         postfix_expression,
4013                                                         false);
4014           idk = CP_ID_KIND_NONE;
4015           break;
4016
4017         case CPP_OPEN_PAREN:
4018           /* postfix-expression ( expression-list [opt] ) */
4019           {
4020             bool koenig_p;
4021             tree args = (cp_parser_parenthesized_expression_list
4022                          (parser, false, /*non_constant_p=*/NULL));
4023
4024             if (args == error_mark_node)
4025               {
4026                 postfix_expression = error_mark_node;
4027                 break;
4028               }
4029
4030             /* Function calls are not permitted in
4031                constant-expressions.  */
4032             if (cp_parser_non_integral_constant_expression (parser,
4033                                                             "a function call"))
4034               {
4035                 postfix_expression = error_mark_node;
4036                 break;
4037               }
4038
4039             koenig_p = false;
4040             if (idk == CP_ID_KIND_UNQUALIFIED)
4041               {
4042                 /* We do not perform argument-dependent lookup if
4043                    normal lookup finds a non-function, in accordance
4044                    with the expected resolution of DR 218.  */
4045                 if (args
4046                     && (is_overloaded_fn (postfix_expression)
4047                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4048                   {
4049                     koenig_p = true;
4050                     postfix_expression
4051                       = perform_koenig_lookup (postfix_expression, args);
4052                   }
4053                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4054                   postfix_expression
4055                     = unqualified_fn_lookup_error (postfix_expression);
4056               }
4057
4058             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4059               {
4060                 tree instance = TREE_OPERAND (postfix_expression, 0);
4061                 tree fn = TREE_OPERAND (postfix_expression, 1);
4062
4063                 if (processing_template_decl
4064                     && (type_dependent_expression_p (instance)
4065                         || (!BASELINK_P (fn)
4066                             && TREE_CODE (fn) != FIELD_DECL)
4067                         || type_dependent_expression_p (fn)
4068                         || any_type_dependent_arguments_p (args)))
4069                   {
4070                     postfix_expression
4071                       = build_min_nt (CALL_EXPR, postfix_expression,
4072                                       args, NULL_TREE);
4073                     break;
4074                   }
4075
4076                 if (BASELINK_P (fn))
4077                   postfix_expression
4078                     = (build_new_method_call
4079                        (instance, fn, args, NULL_TREE,
4080                         (idk == CP_ID_KIND_QUALIFIED
4081                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4082                 else
4083                   postfix_expression
4084                     = finish_call_expr (postfix_expression, args,
4085                                         /*disallow_virtual=*/false,
4086                                         /*koenig_p=*/false);
4087               }
4088             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4089                      || TREE_CODE (postfix_expression) == MEMBER_REF
4090                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4091               postfix_expression = (build_offset_ref_call_from_tree
4092                                     (postfix_expression, args));
4093             else if (idk == CP_ID_KIND_QUALIFIED)
4094               /* A call to a static class member, or a namespace-scope
4095                  function.  */
4096               postfix_expression
4097                 = finish_call_expr (postfix_expression, args,
4098                                     /*disallow_virtual=*/true,
4099                                     koenig_p);
4100             else
4101               /* All other function calls.  */
4102               postfix_expression
4103                 = finish_call_expr (postfix_expression, args,
4104                                     /*disallow_virtual=*/false,
4105                                     koenig_p);
4106
4107             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4108             idk = CP_ID_KIND_NONE;
4109           }
4110           break;
4111
4112         case CPP_DOT:
4113         case CPP_DEREF:
4114           /* postfix-expression . template [opt] id-expression
4115              postfix-expression . pseudo-destructor-name
4116              postfix-expression -> template [opt] id-expression
4117              postfix-expression -> pseudo-destructor-name */
4118         
4119           /* Consume the `.' or `->' operator.  */
4120           cp_lexer_consume_token (parser->lexer);
4121
4122           postfix_expression
4123             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4124                                                       postfix_expression,
4125                                                       false, &idk);
4126           break;
4127
4128         case CPP_PLUS_PLUS:
4129           /* postfix-expression ++  */
4130           /* Consume the `++' token.  */
4131           cp_lexer_consume_token (parser->lexer);
4132           /* Generate a representation for the complete expression.  */
4133           postfix_expression
4134             = finish_increment_expr (postfix_expression,
4135                                      POSTINCREMENT_EXPR);
4136           /* Increments may not appear in constant-expressions.  */
4137           if (cp_parser_non_integral_constant_expression (parser,
4138                                                           "an increment"))
4139             postfix_expression = error_mark_node;
4140           idk = CP_ID_KIND_NONE;
4141           break;
4142
4143         case CPP_MINUS_MINUS:
4144           /* postfix-expression -- */
4145           /* Consume the `--' token.  */
4146           cp_lexer_consume_token (parser->lexer);
4147           /* Generate a representation for the complete expression.  */
4148           postfix_expression
4149             = finish_increment_expr (postfix_expression,
4150                                      POSTDECREMENT_EXPR);
4151           /* Decrements may not appear in constant-expressions.  */
4152           if (cp_parser_non_integral_constant_expression (parser,
4153                                                           "a decrement"))
4154             postfix_expression = error_mark_node;
4155           idk = CP_ID_KIND_NONE;
4156           break;
4157
4158         default:
4159           return postfix_expression;
4160         }
4161     }
4162
4163   /* We should never get here.  */
4164   abort ();
4165   return error_mark_node;
4166 }
4167
4168 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4169    by cp_parser_builtin_offsetof.  We're looking for
4170
4171      postfix-expression [ expression ]
4172
4173    FOR_OFFSETOF is set if we're being called in that context, which
4174    changes how we deal with integer constant expressions.  */
4175
4176 static tree
4177 cp_parser_postfix_open_square_expression (cp_parser *parser,
4178                                           tree postfix_expression,
4179                                           bool for_offsetof)
4180 {
4181   tree index;
4182
4183   /* Consume the `[' token.  */
4184   cp_lexer_consume_token (parser->lexer);
4185
4186   /* Parse the index expression.  */
4187   /* ??? For offsetof, there is a question of what to allow here.  If
4188      offsetof is not being used in an integral constant expression context,
4189      then we *could* get the right answer by computing the value at runtime.
4190      If we are in an integral constant expression context, then we might
4191      could accept any constant expression; hard to say without analysis.
4192      Rather than open the barn door too wide right away, allow only integer
4193      constant expresions here.  */
4194   if (for_offsetof)
4195     index = cp_parser_constant_expression (parser, false, NULL);
4196   else
4197     index = cp_parser_expression (parser);
4198
4199   /* Look for the closing `]'.  */
4200   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4201
4202   /* Build the ARRAY_REF.  */
4203   postfix_expression = grok_array_decl (postfix_expression, index);
4204
4205   /* When not doing offsetof, array references are not permitted in
4206      constant-expressions.  */
4207   if (!for_offsetof
4208       && (cp_parser_non_integral_constant_expression
4209           (parser, "an array reference")))
4210     postfix_expression = error_mark_node;
4211
4212   return postfix_expression;
4213 }
4214
4215 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4216    by cp_parser_builtin_offsetof.  We're looking for
4217
4218      postfix-expression . template [opt] id-expression
4219      postfix-expression . pseudo-destructor-name
4220      postfix-expression -> template [opt] id-expression
4221      postfix-expression -> pseudo-destructor-name
4222
4223    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4224    limits what of the above we'll actually accept, but nevermind.
4225    TOKEN_TYPE is the "." or "->" token, which will already have been
4226    removed from the stream.  */
4227
4228 static tree
4229 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4230                                         enum cpp_ttype token_type,
4231                                         tree postfix_expression,
4232                                         bool for_offsetof, cp_id_kind *idk)
4233 {
4234   tree name;
4235   bool dependent_p;
4236   bool template_p;
4237   tree scope = NULL_TREE;
4238
4239   /* If this is a `->' operator, dereference the pointer.  */
4240   if (token_type == CPP_DEREF)
4241     postfix_expression = build_x_arrow (postfix_expression);
4242   /* Check to see whether or not the expression is type-dependent.  */
4243   dependent_p = type_dependent_expression_p (postfix_expression);
4244   /* The identifier following the `->' or `.' is not qualified.  */
4245   parser->scope = NULL_TREE;
4246   parser->qualifying_scope = NULL_TREE;
4247   parser->object_scope = NULL_TREE;
4248   *idk = CP_ID_KIND_NONE;
4249   /* Enter the scope corresponding to the type of the object
4250      given by the POSTFIX_EXPRESSION.  */
4251   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4252     {
4253       scope = TREE_TYPE (postfix_expression);
4254       /* According to the standard, no expression should ever have
4255          reference type.  Unfortunately, we do not currently match
4256          the standard in this respect in that our internal representation
4257          of an expression may have reference type even when the standard
4258          says it does not.  Therefore, we have to manually obtain the
4259          underlying type here.  */
4260       scope = non_reference (scope);
4261       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4262       scope = complete_type_or_else (scope, NULL_TREE);
4263       /* Let the name lookup machinery know that we are processing a
4264          class member access expression.  */
4265       parser->context->object_type = scope;
4266       /* If something went wrong, we want to be able to discern that case,
4267          as opposed to the case where there was no SCOPE due to the type
4268          of expression being dependent.  */
4269       if (!scope)
4270         scope = error_mark_node;
4271       /* If the SCOPE was erroneous, make the various semantic analysis
4272          functions exit quickly -- and without issuing additional error
4273          messages.  */
4274       if (scope == error_mark_node)
4275         postfix_expression = error_mark_node;
4276     }
4277
4278   /* If the SCOPE is not a scalar type, we are looking at an
4279      ordinary class member access expression, rather than a
4280      pseudo-destructor-name.  */
4281   if (!scope || !SCALAR_TYPE_P (scope))
4282     {
4283       template_p = cp_parser_optional_template_keyword (parser);
4284       /* Parse the id-expression.  */
4285       name = cp_parser_id_expression (parser, template_p,
4286                                       /*check_dependency_p=*/true,
4287                                       /*template_p=*/NULL,
4288                                       /*declarator_p=*/false);
4289       /* In general, build a SCOPE_REF if the member name is qualified.
4290          However, if the name was not dependent and has already been
4291          resolved; there is no need to build the SCOPE_REF.  For example;
4292
4293              struct X { void f(); };
4294              template <typename T> void f(T* t) { t->X::f(); }
4295
4296          Even though "t" is dependent, "X::f" is not and has been resolved
4297          to a BASELINK; there is no need to include scope information.  */
4298
4299       /* But we do need to remember that there was an explicit scope for
4300          virtual function calls.  */
4301       if (parser->scope)
4302         *idk = CP_ID_KIND_QUALIFIED;
4303
4304       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4305         {
4306           name = build_nt (SCOPE_REF, parser->scope, name);
4307           parser->scope = NULL_TREE;
4308           parser->qualifying_scope = NULL_TREE;
4309           parser->object_scope = NULL_TREE;
4310         }
4311       if (scope && name && BASELINK_P (name))
4312         adjust_result_of_qualified_name_lookup 
4313           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4314       postfix_expression
4315         = finish_class_member_access_expr (postfix_expression, name);
4316     }
4317   /* Otherwise, try the pseudo-destructor-name production.  */
4318   else
4319     {
4320       tree s = NULL_TREE;
4321       tree type;
4322
4323       /* Parse the pseudo-destructor-name.  */
4324       cp_parser_pseudo_destructor_name (parser, &s, &type);
4325       /* Form the call.  */
4326       postfix_expression
4327         = finish_pseudo_destructor_expr (postfix_expression,
4328                                          s, TREE_TYPE (type));
4329     }
4330
4331   /* We no longer need to look up names in the scope of the object on
4332      the left-hand side of the `.' or `->' operator.  */
4333   parser->context->object_type = NULL_TREE;
4334
4335   /* Outside of offsetof, these operators may not appear in
4336      constant-expressions.  */
4337   if (!for_offsetof
4338       && (cp_parser_non_integral_constant_expression 
4339           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4340     postfix_expression = error_mark_node;
4341
4342   return postfix_expression;
4343 }
4344
4345 /* Parse a parenthesized expression-list.
4346
4347    expression-list:
4348      assignment-expression
4349      expression-list, assignment-expression
4350
4351    attribute-list:
4352      expression-list
4353      identifier
4354      identifier, expression-list
4355
4356    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4357    representation of an assignment-expression.  Note that a TREE_LIST
4358    is returned even if there is only a single expression in the list.
4359    error_mark_node is returned if the ( and or ) are
4360    missing. NULL_TREE is returned on no expressions. The parentheses
4361    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4362    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4363    indicates whether or not all of the expressions in the list were
4364    constant.  */
4365
4366 static tree
4367 cp_parser_parenthesized_expression_list (cp_parser* parser,
4368                                          bool is_attribute_list,
4369                                          bool *non_constant_p)
4370 {
4371   tree expression_list = NULL_TREE;
4372   tree identifier = NULL_TREE;
4373
4374   /* Assume all the expressions will be constant.  */
4375   if (non_constant_p)
4376     *non_constant_p = false;
4377
4378   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4379     return error_mark_node;
4380
4381   /* Consume expressions until there are no more.  */
4382   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4383     while (true)
4384       {
4385         tree expr;
4386
4387         /* At the beginning of attribute lists, check to see if the
4388            next token is an identifier.  */
4389         if (is_attribute_list
4390             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4391           {
4392             cp_token *token;
4393
4394             /* Consume the identifier.  */
4395             token = cp_lexer_consume_token (parser->lexer);
4396             /* Save the identifier.  */
4397             identifier = token->value;
4398           }
4399         else
4400           {
4401             /* Parse the next assignment-expression.  */
4402             if (non_constant_p)
4403               {
4404                 bool expr_non_constant_p;
4405                 expr = (cp_parser_constant_expression
4406                         (parser, /*allow_non_constant_p=*/true,
4407                          &expr_non_constant_p));
4408                 if (expr_non_constant_p)
4409                   *non_constant_p = true;
4410               }
4411             else
4412               expr = cp_parser_assignment_expression (parser);
4413
4414              /* Add it to the list.  We add error_mark_node
4415                 expressions to the list, so that we can still tell if
4416                 the correct form for a parenthesized expression-list
4417                 is found. That gives better errors.  */
4418             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4419
4420             if (expr == error_mark_node)
4421               goto skip_comma;
4422           }
4423
4424         /* After the first item, attribute lists look the same as
4425            expression lists.  */
4426         is_attribute_list = false;
4427
4428       get_comma:;
4429         /* If the next token isn't a `,', then we are done.  */
4430         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4431           break;
4432
4433         /* Otherwise, consume the `,' and keep going.  */
4434         cp_lexer_consume_token (parser->lexer);
4435       }
4436
4437   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4438     {
4439       int ending;
4440
4441     skip_comma:;
4442       /* We try and resync to an unnested comma, as that will give the
4443          user better diagnostics.  */
4444       ending = cp_parser_skip_to_closing_parenthesis (parser,
4445                                                       /*recovering=*/true,
4446                                                       /*or_comma=*/true,
4447                                                       /*consume_paren=*/true);
4448       if (ending < 0)
4449         goto get_comma;
4450       if (!ending)
4451         return error_mark_node;
4452     }
4453
4454   /* We built up the list in reverse order so we must reverse it now.  */
4455   expression_list = nreverse (expression_list);
4456   if (identifier)
4457     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4458
4459   return expression_list;
4460 }
4461
4462 /* Parse a pseudo-destructor-name.
4463
4464    pseudo-destructor-name:
4465      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4466      :: [opt] nested-name-specifier template template-id :: ~ type-name
4467      :: [opt] nested-name-specifier [opt] ~ type-name
4468
4469    If either of the first two productions is used, sets *SCOPE to the
4470    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4471    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4472    or ERROR_MARK_NODE if the parse fails.  */
4473
4474 static void
4475 cp_parser_pseudo_destructor_name (cp_parser* parser,
4476                                   tree* scope,
4477                                   tree* type)
4478 {
4479   bool nested_name_specifier_p;
4480
4481   /* Look for the optional `::' operator.  */
4482   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4483   /* Look for the optional nested-name-specifier.  */
4484   nested_name_specifier_p
4485     = (cp_parser_nested_name_specifier_opt (parser,
4486                                             /*typename_keyword_p=*/false,
4487                                             /*check_dependency_p=*/true,
4488                                             /*type_p=*/false,
4489                                             /*is_declaration=*/true)
4490        != NULL_TREE);
4491   /* Now, if we saw a nested-name-specifier, we might be doing the
4492      second production.  */
4493   if (nested_name_specifier_p
4494       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4495     {
4496       /* Consume the `template' keyword.  */
4497       cp_lexer_consume_token (parser->lexer);
4498       /* Parse the template-id.  */
4499       cp_parser_template_id (parser,
4500                              /*template_keyword_p=*/true,
4501                              /*check_dependency_p=*/false,
4502                              /*is_declaration=*/true);
4503       /* Look for the `::' token.  */
4504       cp_parser_require (parser, CPP_SCOPE, "`::'");
4505     }
4506   /* If the next token is not a `~', then there might be some
4507      additional qualification.  */
4508   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4509     {
4510       /* Look for the type-name.  */
4511       *scope = TREE_TYPE (cp_parser_type_name (parser));
4512
4513       /* If we didn't get an aggregate type, or we don't have ::~,
4514          then something has gone wrong.  Since the only caller of this
4515          function is looking for something after `.' or `->' after a
4516          scalar type, most likely the program is trying to get a
4517          member of a non-aggregate type.  */
4518       if (*scope == error_mark_node
4519           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4520           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4521         {
4522           cp_parser_error (parser, "request for member of non-aggregate type");
4523           *type = error_mark_node;
4524           return;
4525         }
4526
4527       /* Look for the `::' token.  */
4528       cp_parser_require (parser, CPP_SCOPE, "`::'");
4529     }
4530   else
4531     *scope = NULL_TREE;
4532
4533   /* Look for the `~'.  */
4534   cp_parser_require (parser, CPP_COMPL, "`~'");
4535   /* Look for the type-name again.  We are not responsible for
4536      checking that it matches the first type-name.  */
4537   *type = cp_parser_type_name (parser);
4538 }
4539
4540 /* Parse a unary-expression.
4541
4542    unary-expression:
4543      postfix-expression
4544      ++ cast-expression
4545      -- cast-expression
4546      unary-operator cast-expression
4547      sizeof unary-expression
4548      sizeof ( type-id )
4549      new-expression
4550      delete-expression
4551
4552    GNU Extensions:
4553
4554    unary-expression:
4555      __extension__ cast-expression
4556      __alignof__ unary-expression
4557      __alignof__ ( type-id )
4558      __real__ cast-expression
4559      __imag__ cast-expression
4560      && identifier
4561
4562    ADDRESS_P is true iff the unary-expression is appearing as the
4563    operand of the `&' operator.
4564
4565    Returns a representation of the expression.  */
4566
4567 static tree
4568 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4569 {
4570   cp_token *token;
4571   enum tree_code unary_operator;
4572
4573   /* Peek at the next token.  */
4574   token = cp_lexer_peek_token (parser->lexer);
4575   /* Some keywords give away the kind of expression.  */
4576   if (token->type == CPP_KEYWORD)
4577     {
4578       enum rid keyword = token->keyword;
4579
4580       switch (keyword)
4581         {
4582         case RID_ALIGNOF:
4583         case RID_SIZEOF:
4584           {
4585             tree operand;
4586             enum tree_code op;
4587
4588             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4589             /* Consume the token.  */
4590             cp_lexer_consume_token (parser->lexer);
4591             /* Parse the operand.  */
4592             operand = cp_parser_sizeof_operand (parser, keyword);
4593
4594             if (TYPE_P (operand))
4595               return cxx_sizeof_or_alignof_type (operand, op, true);
4596             else
4597               return cxx_sizeof_or_alignof_expr (operand, op);
4598           }
4599
4600         case RID_NEW:
4601           return cp_parser_new_expression (parser);
4602
4603         case RID_DELETE:
4604           return cp_parser_delete_expression (parser);
4605
4606         case RID_EXTENSION:
4607           {
4608             /* The saved value of the PEDANTIC flag.  */
4609             int saved_pedantic;
4610             tree expr;
4611
4612             /* Save away the PEDANTIC flag.  */
4613             cp_parser_extension_opt (parser, &saved_pedantic);
4614             /* Parse the cast-expression.  */
4615             expr = cp_parser_simple_cast_expression (parser);
4616             /* Restore the PEDANTIC flag.  */
4617             pedantic = saved_pedantic;
4618
4619             return expr;
4620           }
4621
4622         case RID_REALPART:
4623         case RID_IMAGPART:
4624           {
4625             tree expression;
4626
4627             /* Consume the `__real__' or `__imag__' token.  */
4628             cp_lexer_consume_token (parser->lexer);
4629             /* Parse the cast-expression.  */
4630             expression = cp_parser_simple_cast_expression (parser);
4631             /* Create the complete representation.  */
4632             return build_x_unary_op ((keyword == RID_REALPART
4633                                       ? REALPART_EXPR : IMAGPART_EXPR),
4634                                      expression);
4635           }
4636           break;
4637
4638         default:
4639           break;
4640         }
4641     }
4642
4643   /* Look for the `:: new' and `:: delete', which also signal the
4644      beginning of a new-expression, or delete-expression,
4645      respectively.  If the next token is `::', then it might be one of
4646      these.  */
4647   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4648     {
4649       enum rid keyword;
4650
4651       /* See if the token after the `::' is one of the keywords in
4652          which we're interested.  */
4653       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4654       /* If it's `new', we have a new-expression.  */
4655       if (keyword == RID_NEW)
4656         return cp_parser_new_expression (parser);
4657       /* Similarly, for `delete'.  */
4658       else if (keyword == RID_DELETE)
4659         return cp_parser_delete_expression (parser);
4660     }
4661
4662   /* Look for a unary operator.  */
4663   unary_operator = cp_parser_unary_operator (token);
4664   /* The `++' and `--' operators can be handled similarly, even though
4665      they are not technically unary-operators in the grammar.  */
4666   if (unary_operator == ERROR_MARK)
4667     {
4668       if (token->type == CPP_PLUS_PLUS)
4669         unary_operator = PREINCREMENT_EXPR;
4670       else if (token->type == CPP_MINUS_MINUS)
4671         unary_operator = PREDECREMENT_EXPR;
4672       /* Handle the GNU address-of-label extension.  */
4673       else if (cp_parser_allow_gnu_extensions_p (parser)
4674                && token->type == CPP_AND_AND)
4675         {
4676           tree identifier;
4677
4678           /* Consume the '&&' token.  */
4679           cp_lexer_consume_token (parser->lexer);
4680           /* Look for the identifier.  */
4681           identifier = cp_parser_identifier (parser);
4682           /* Create an expression representing the address.  */
4683           return finish_label_address_expr (identifier);
4684         }
4685     }
4686   if (unary_operator != ERROR_MARK)
4687     {
4688       tree cast_expression;
4689       tree expression = error_mark_node;
4690       const char *non_constant_p = NULL;
4691
4692       /* Consume the operator token.  */
4693       token = cp_lexer_consume_token (parser->lexer);
4694       /* Parse the cast-expression.  */
4695       cast_expression
4696         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4697       /* Now, build an appropriate representation.  */
4698       switch (unary_operator)
4699         {
4700         case INDIRECT_REF:
4701           non_constant_p = "`*'";
4702           expression = build_x_indirect_ref (cast_expression, "unary *");
4703           break;
4704
4705         case ADDR_EXPR:
4706           non_constant_p = "`&'";
4707           /* Fall through.  */
4708         case BIT_NOT_EXPR:
4709           expression = build_x_unary_op (unary_operator, cast_expression);
4710           break;
4711
4712         case PREINCREMENT_EXPR:
4713         case PREDECREMENT_EXPR:
4714           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4715                             ? "`++'" : "`--'");
4716           /* Fall through.  */
4717         case CONVERT_EXPR:
4718         case NEGATE_EXPR:
4719         case TRUTH_NOT_EXPR:
4720           expression = finish_unary_op_expr (unary_operator, cast_expression);
4721           break;
4722
4723         default:
4724           abort ();
4725         }
4726
4727       if (non_constant_p 
4728           && cp_parser_non_integral_constant_expression (parser,
4729                                                          non_constant_p))
4730         expression = error_mark_node;
4731
4732       return expression;
4733     }
4734
4735   return cp_parser_postfix_expression (parser, address_p);
4736 }
4737
4738 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4739    unary-operator, the corresponding tree code is returned.  */
4740
4741 static enum tree_code
4742 cp_parser_unary_operator (cp_token* token)
4743 {
4744   switch (token->type)
4745     {
4746     case CPP_MULT:
4747       return INDIRECT_REF;
4748
4749     case CPP_AND:
4750       return ADDR_EXPR;
4751
4752     case CPP_PLUS:
4753       return CONVERT_EXPR;
4754
4755     case CPP_MINUS:
4756       return NEGATE_EXPR;
4757
4758     case CPP_NOT:
4759       return TRUTH_NOT_EXPR;
4760
4761     case CPP_COMPL:
4762       return BIT_NOT_EXPR;
4763
4764     default:
4765       return ERROR_MARK;
4766     }
4767 }
4768
4769 /* Parse a new-expression.
4770
4771    new-expression:
4772      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4773      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4774
4775    Returns a representation of the expression.  */
4776
4777 static tree
4778 cp_parser_new_expression (cp_parser* parser)
4779 {
4780   bool global_scope_p;
4781   tree placement;
4782   tree type;
4783   tree initializer;
4784   tree nelts;
4785
4786   /* Look for the optional `::' operator.  */
4787   global_scope_p
4788     = (cp_parser_global_scope_opt (parser,
4789                                    /*current_scope_valid_p=*/false)
4790        != NULL_TREE);
4791   /* Look for the `new' operator.  */
4792   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4793   /* There's no easy way to tell a new-placement from the
4794      `( type-id )' construct.  */
4795   cp_parser_parse_tentatively (parser);
4796   /* Look for a new-placement.  */
4797   placement = cp_parser_new_placement (parser);
4798   /* If that didn't work out, there's no new-placement.  */
4799   if (!cp_parser_parse_definitely (parser))
4800     placement = NULL_TREE;
4801
4802   /* If the next token is a `(', then we have a parenthesized
4803      type-id.  */
4804   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4805     {
4806       /* Consume the `('.  */
4807       cp_lexer_consume_token (parser->lexer);
4808       /* Parse the type-id.  */
4809       type = cp_parser_type_id (parser);
4810       /* Look for the closing `)'.  */
4811       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4812       /* There should not be a direct-new-declarator in this production, 
4813          but GCC used to allowed this, so we check and emit a sensible error
4814          message for this case.  */
4815       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4816         {
4817           error ("array bound forbidden after parenthesized type-id");
4818           inform ("try removing the parentheses around the type-id");
4819           cp_parser_direct_new_declarator (parser);
4820         }
4821       nelts = integer_one_node;
4822     }
4823   /* Otherwise, there must be a new-type-id.  */
4824   else
4825     type = cp_parser_new_type_id (parser, &nelts);
4826
4827   /* If the next token is a `(', then we have a new-initializer.  */
4828   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4829     initializer = cp_parser_new_initializer (parser);
4830   else
4831     initializer = NULL_TREE;
4832
4833   /* A new-expression may not appear in an integral constant
4834      expression.  */
4835   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4836     return error_mark_node;
4837
4838   /* Create a representation of the new-expression.  */
4839   return build_new (placement, type, nelts, initializer, global_scope_p);
4840 }
4841
4842 /* Parse a new-placement.
4843
4844    new-placement:
4845      ( expression-list )
4846
4847    Returns the same representation as for an expression-list.  */
4848
4849 static tree
4850 cp_parser_new_placement (cp_parser* parser)
4851 {
4852   tree expression_list;
4853
4854   /* Parse the expression-list.  */
4855   expression_list = (cp_parser_parenthesized_expression_list
4856                      (parser, false, /*non_constant_p=*/NULL));
4857
4858   return expression_list;
4859 }
4860
4861 /* Parse a new-type-id.
4862
4863    new-type-id:
4864      type-specifier-seq new-declarator [opt]
4865
4866    Returns the TYPE allocated.  If the new-type-id indicates an array
4867    type, *NELTS is set to the number of elements in the last array
4868    bound; the TYPE will not include the last array bound.  */
4869
4870 static tree
4871 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4872 {
4873   cp_decl_specifier_seq type_specifier_seq;
4874   cp_declarator *new_declarator;
4875   cp_declarator *declarator;
4876   cp_declarator *outer_declarator;
4877   const char *saved_message;
4878   tree type;
4879
4880   /* The type-specifier sequence must not contain type definitions.
4881      (It cannot contain declarations of new types either, but if they
4882      are not definitions we will catch that because they are not
4883      complete.)  */
4884   saved_message = parser->type_definition_forbidden_message;
4885   parser->type_definition_forbidden_message
4886     = "types may not be defined in a new-type-id";
4887   /* Parse the type-specifier-seq.  */
4888   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4889   /* Restore the old message.  */
4890   parser->type_definition_forbidden_message = saved_message;
4891   /* Parse the new-declarator.  */
4892   new_declarator = cp_parser_new_declarator_opt (parser);
4893
4894   /* Determine the number of elements in the last array dimension, if
4895      any.  */
4896   *nelts = NULL_TREE;
4897   /* Skip down to the last array dimension.  */
4898   declarator = new_declarator;
4899   outer_declarator = NULL;
4900   while (declarator && (declarator->kind == cdk_pointer
4901                         || declarator->kind == cdk_ptrmem))
4902     {
4903       outer_declarator = declarator;
4904       declarator = declarator->declarator;
4905     }
4906   while (declarator 
4907          && declarator->kind == cdk_array
4908          && declarator->declarator
4909          && declarator->declarator->kind == cdk_array)
4910     {
4911       outer_declarator = declarator;
4912       declarator = declarator->declarator;
4913     }
4914   
4915   if (declarator && declarator->kind == cdk_array)
4916     {
4917       *nelts = declarator->u.array.bounds;
4918       if (*nelts == error_mark_node)
4919         *nelts = integer_one_node;
4920       else if (!processing_template_decl)
4921         {
4922           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts, 
4923                                            false))
4924             pedwarn ("size in array new must have integral type");
4925           *nelts = save_expr (cp_convert (sizetype, *nelts));
4926           if (*nelts == integer_zero_node)
4927             warning ("zero size array reserves no space");
4928         }
4929       if (outer_declarator)
4930         outer_declarator->declarator = declarator->declarator;
4931       else
4932         new_declarator = NULL;
4933     }
4934
4935   type = groktypename (&type_specifier_seq, new_declarator);
4936   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4937     {
4938       *nelts = array_type_nelts_top (type);
4939       type = TREE_TYPE (type);
4940     }
4941   return type;
4942 }
4943
4944 /* Parse an (optional) new-declarator.
4945
4946    new-declarator:
4947      ptr-operator new-declarator [opt]
4948      direct-new-declarator
4949
4950    Returns the declarator.  */
4951
4952 static cp_declarator *
4953 cp_parser_new_declarator_opt (cp_parser* parser)
4954 {
4955   enum tree_code code;
4956   tree type;
4957   cp_cv_quals cv_quals;
4958
4959   /* We don't know if there's a ptr-operator next, or not.  */
4960   cp_parser_parse_tentatively (parser);
4961   /* Look for a ptr-operator.  */
4962   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4963   /* If that worked, look for more new-declarators.  */
4964   if (cp_parser_parse_definitely (parser))
4965     {
4966       cp_declarator *declarator;
4967
4968       /* Parse another optional declarator.  */
4969       declarator = cp_parser_new_declarator_opt (parser);
4970
4971       /* Create the representation of the declarator.  */
4972       if (type)
4973         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4974       else if (code == INDIRECT_REF)
4975         declarator = make_pointer_declarator (cv_quals, declarator);
4976       else
4977         declarator = make_reference_declarator (cv_quals, declarator);
4978
4979       return declarator;
4980     }
4981
4982   /* If the next token is a `[', there is a direct-new-declarator.  */
4983   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4984     return cp_parser_direct_new_declarator (parser);
4985
4986   return NULL;
4987 }
4988
4989 /* Parse a direct-new-declarator.
4990
4991    direct-new-declarator:
4992      [ expression ]
4993      direct-new-declarator [constant-expression]
4994
4995    */
4996
4997 static cp_declarator *
4998 cp_parser_direct_new_declarator (cp_parser* parser)
4999 {
5000   cp_declarator *declarator = NULL;
5001
5002   while (true)
5003     {
5004       tree expression;
5005
5006       /* Look for the opening `['.  */
5007       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5008       /* The first expression is not required to be constant.  */
5009       if (!declarator)
5010         {
5011           expression = cp_parser_expression (parser);
5012           /* The standard requires that the expression have integral
5013              type.  DR 74 adds enumeration types.  We believe that the
5014              real intent is that these expressions be handled like the
5015              expression in a `switch' condition, which also allows
5016              classes with a single conversion to integral or
5017              enumeration type.  */
5018           if (!processing_template_decl)
5019             {
5020               expression
5021                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5022                                               expression,
5023                                               /*complain=*/true);
5024               if (!expression)
5025                 {
5026                   error ("expression in new-declarator must have integral or enumeration type");
5027                   expression = error_mark_node;
5028                 }
5029             }
5030         }
5031       /* But all the other expressions must be.  */
5032       else
5033         expression
5034           = cp_parser_constant_expression (parser,
5035                                            /*allow_non_constant=*/false,
5036                                            NULL);
5037       /* Look for the closing `]'.  */
5038       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5039
5040       /* Add this bound to the declarator.  */
5041       declarator = make_array_declarator (declarator, expression);
5042
5043       /* If the next token is not a `[', then there are no more
5044          bounds.  */
5045       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5046         break;
5047     }
5048
5049   return declarator;
5050 }
5051
5052 /* Parse a new-initializer.
5053
5054    new-initializer:
5055      ( expression-list [opt] )
5056
5057    Returns a representation of the expression-list.  If there is no
5058    expression-list, VOID_ZERO_NODE is returned.  */
5059
5060 static tree
5061 cp_parser_new_initializer (cp_parser* parser)
5062 {
5063   tree expression_list;
5064
5065   expression_list = (cp_parser_parenthesized_expression_list
5066                      (parser, false, /*non_constant_p=*/NULL));
5067   if (!expression_list)
5068     expression_list = void_zero_node;
5069
5070   return expression_list;
5071 }
5072
5073 /* Parse a delete-expression.
5074
5075    delete-expression:
5076      :: [opt] delete cast-expression
5077      :: [opt] delete [ ] cast-expression
5078
5079    Returns a representation of the expression.  */
5080
5081 static tree
5082 cp_parser_delete_expression (cp_parser* parser)
5083 {
5084   bool global_scope_p;
5085   bool array_p;
5086   tree expression;
5087
5088   /* Look for the optional `::' operator.  */
5089   global_scope_p
5090     = (cp_parser_global_scope_opt (parser,
5091                                    /*current_scope_valid_p=*/false)
5092        != NULL_TREE);
5093   /* Look for the `delete' keyword.  */
5094   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5095   /* See if the array syntax is in use.  */
5096   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5097     {
5098       /* Consume the `[' token.  */
5099       cp_lexer_consume_token (parser->lexer);
5100       /* Look for the `]' token.  */
5101       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5102       /* Remember that this is the `[]' construct.  */
5103       array_p = true;
5104     }
5105   else
5106     array_p = false;
5107
5108   /* Parse the cast-expression.  */
5109   expression = cp_parser_simple_cast_expression (parser);
5110
5111   /* A delete-expression may not appear in an integral constant
5112      expression.  */
5113   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5114     return error_mark_node;
5115
5116   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5117 }
5118
5119 /* Parse a cast-expression.
5120
5121    cast-expression:
5122      unary-expression
5123      ( type-id ) cast-expression
5124
5125    Returns a representation of the expression.  */
5126
5127 static tree
5128 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5129 {
5130   /* If it's a `(', then we might be looking at a cast.  */
5131   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5132     {
5133       tree type = NULL_TREE;
5134       tree expr = NULL_TREE;
5135       bool compound_literal_p;
5136       const char *saved_message;
5137
5138       /* There's no way to know yet whether or not this is a cast.
5139          For example, `(int (3))' is a unary-expression, while `(int)
5140          3' is a cast.  So, we resort to parsing tentatively.  */
5141       cp_parser_parse_tentatively (parser);
5142       /* Types may not be defined in a cast.  */
5143       saved_message = parser->type_definition_forbidden_message;
5144       parser->type_definition_forbidden_message
5145         = "types may not be defined in casts";
5146       /* Consume the `('.  */
5147       cp_lexer_consume_token (parser->lexer);
5148       /* A very tricky bit is that `(struct S) { 3 }' is a
5149          compound-literal (which we permit in C++ as an extension).
5150          But, that construct is not a cast-expression -- it is a
5151          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5152          is legal; if the compound-literal were a cast-expression,
5153          you'd need an extra set of parentheses.)  But, if we parse
5154          the type-id, and it happens to be a class-specifier, then we
5155          will commit to the parse at that point, because we cannot
5156          undo the action that is done when creating a new class.  So,
5157          then we cannot back up and do a postfix-expression.
5158
5159          Therefore, we scan ahead to the closing `)', and check to see
5160          if the token after the `)' is a `{'.  If so, we are not
5161          looking at a cast-expression.
5162
5163          Save tokens so that we can put them back.  */
5164       cp_lexer_save_tokens (parser->lexer);
5165       /* Skip tokens until the next token is a closing parenthesis.
5166          If we find the closing `)', and the next token is a `{', then
5167          we are looking at a compound-literal.  */
5168       compound_literal_p
5169         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5170                                                   /*consume_paren=*/true)
5171            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5172       /* Roll back the tokens we skipped.  */
5173       cp_lexer_rollback_tokens (parser->lexer);
5174       /* If we were looking at a compound-literal, simulate an error
5175          so that the call to cp_parser_parse_definitely below will
5176          fail.  */
5177       if (compound_literal_p)
5178         cp_parser_simulate_error (parser);
5179       else
5180         {
5181           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5182           parser->in_type_id_in_expr_p = true;
5183           /* Look for the type-id.  */
5184           type = cp_parser_type_id (parser);
5185           /* Look for the closing `)'.  */
5186           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5187           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5188         }
5189
5190       /* Restore the saved message.  */
5191       parser->type_definition_forbidden_message = saved_message;
5192
5193       /* If ok so far, parse the dependent expression. We cannot be
5194          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5195          ctor of T, but looks like a cast to function returning T
5196          without a dependent expression.  */
5197       if (!cp_parser_error_occurred (parser))
5198         expr = cp_parser_simple_cast_expression (parser);
5199
5200       if (cp_parser_parse_definitely (parser))
5201         {
5202           /* Warn about old-style casts, if so requested.  */
5203           if (warn_old_style_cast
5204               && !in_system_header
5205               && !VOID_TYPE_P (type)
5206               && current_lang_name != lang_name_c)
5207             warning ("use of old-style cast");
5208
5209           /* Only type conversions to integral or enumeration types
5210              can be used in constant-expressions.  */
5211           if (parser->integral_constant_expression_p
5212               && !dependent_type_p (type)
5213               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5214               && (cp_parser_non_integral_constant_expression 
5215                   (parser,
5216                    "a cast to a type other than an integral or "
5217                    "enumeration type")))
5218             return error_mark_node;
5219
5220           /* Perform the cast.  */
5221           expr = build_c_cast (type, expr);
5222           return expr;
5223         }
5224     }
5225
5226   /* If we get here, then it's not a cast, so it must be a
5227      unary-expression.  */
5228   return cp_parser_unary_expression (parser, address_p);
5229 }
5230
5231 /* Parse a pm-expression.
5232
5233    pm-expression:
5234      cast-expression
5235      pm-expression .* cast-expression
5236      pm-expression ->* cast-expression
5237
5238      Returns a representation of the expression.  */
5239
5240 static tree
5241 cp_parser_pm_expression (cp_parser* parser)
5242 {
5243   static const cp_parser_token_tree_map map = {
5244     { CPP_DEREF_STAR, MEMBER_REF },
5245     { CPP_DOT_STAR, DOTSTAR_EXPR },
5246     { CPP_EOF, ERROR_MARK }
5247   };
5248
5249   return cp_parser_binary_expression (parser, map,
5250                                       cp_parser_simple_cast_expression);
5251 }
5252
5253 /* Parse a multiplicative-expression.
5254
5255    multiplicative-expression:
5256      pm-expression
5257      multiplicative-expression * pm-expression
5258      multiplicative-expression / pm-expression
5259      multiplicative-expression % pm-expression
5260
5261    Returns a representation of the expression.  */
5262
5263 static tree
5264 cp_parser_multiplicative_expression (cp_parser* parser)
5265 {
5266   static const cp_parser_token_tree_map map = {
5267     { CPP_MULT, MULT_EXPR },
5268     { CPP_DIV, TRUNC_DIV_EXPR },
5269     { CPP_MOD, TRUNC_MOD_EXPR },
5270     { CPP_EOF, ERROR_MARK }
5271   };
5272
5273   return cp_parser_binary_expression (parser,
5274                                       map,
5275                                       cp_parser_pm_expression);
5276 }
5277
5278 /* Parse an additive-expression.
5279
5280    additive-expression:
5281      multiplicative-expression
5282      additive-expression + multiplicative-expression
5283      additive-expression - multiplicative-expression
5284
5285    Returns a representation of the expression.  */
5286
5287 static tree
5288 cp_parser_additive_expression (cp_parser* parser)
5289 {
5290   static const cp_parser_token_tree_map map = {
5291     { CPP_PLUS, PLUS_EXPR },
5292     { CPP_MINUS, MINUS_EXPR },
5293     { CPP_EOF, ERROR_MARK }
5294   };
5295
5296   return cp_parser_binary_expression (parser,
5297                                       map,
5298                                       cp_parser_multiplicative_expression);
5299 }
5300
5301 /* Parse a shift-expression.
5302
5303    shift-expression:
5304      additive-expression
5305      shift-expression << additive-expression
5306      shift-expression >> additive-expression
5307
5308    Returns a representation of the expression.  */
5309
5310 static tree
5311 cp_parser_shift_expression (cp_parser* parser)
5312 {
5313   static const cp_parser_token_tree_map map = {
5314     { CPP_LSHIFT, LSHIFT_EXPR },
5315     { CPP_RSHIFT, RSHIFT_EXPR },
5316     { CPP_EOF, ERROR_MARK }
5317   };
5318
5319   return cp_parser_binary_expression (parser,
5320                                       map,
5321                                       cp_parser_additive_expression);
5322 }
5323
5324 /* Parse a relational-expression.
5325
5326    relational-expression:
5327      shift-expression
5328      relational-expression < shift-expression
5329      relational-expression > shift-expression
5330      relational-expression <= shift-expression
5331      relational-expression >= shift-expression
5332
5333    GNU Extension:
5334
5335    relational-expression:
5336      relational-expression <? shift-expression
5337      relational-expression >? shift-expression
5338
5339    Returns a representation of the expression.  */
5340
5341 static tree
5342 cp_parser_relational_expression (cp_parser* parser)
5343 {
5344   static const cp_parser_token_tree_map map = {
5345     { CPP_LESS, LT_EXPR },
5346     { CPP_GREATER, GT_EXPR },
5347     { CPP_LESS_EQ, LE_EXPR },
5348     { CPP_GREATER_EQ, GE_EXPR },
5349     { CPP_MIN, MIN_EXPR },
5350     { CPP_MAX, MAX_EXPR },
5351     { CPP_EOF, ERROR_MARK }
5352   };
5353
5354   return cp_parser_binary_expression (parser,
5355                                       map,
5356                                       cp_parser_shift_expression);
5357 }
5358
5359 /* Parse an equality-expression.
5360
5361    equality-expression:
5362      relational-expression
5363      equality-expression == relational-expression
5364      equality-expression != relational-expression
5365
5366    Returns a representation of the expression.  */
5367
5368 static tree
5369 cp_parser_equality_expression (cp_parser* parser)
5370 {
5371   static const cp_parser_token_tree_map map = {
5372     { CPP_EQ_EQ, EQ_EXPR },
5373     { CPP_NOT_EQ, NE_EXPR },
5374     { CPP_EOF, ERROR_MARK }
5375   };
5376
5377   return cp_parser_binary_expression (parser,
5378                                       map,
5379                                       cp_parser_relational_expression);
5380 }
5381
5382 /* Parse an and-expression.
5383
5384    and-expression:
5385      equality-expression
5386      and-expression & equality-expression
5387
5388    Returns a representation of the expression.  */
5389
5390 static tree
5391 cp_parser_and_expression (cp_parser* parser)
5392 {
5393   static const cp_parser_token_tree_map map = {
5394     { CPP_AND, BIT_AND_EXPR },
5395     { CPP_EOF, ERROR_MARK }
5396   };
5397
5398   return cp_parser_binary_expression (parser,
5399                                       map,
5400                                       cp_parser_equality_expression);
5401 }
5402
5403 /* Parse an exclusive-or-expression.
5404
5405    exclusive-or-expression:
5406      and-expression
5407      exclusive-or-expression ^ and-expression
5408
5409    Returns a representation of the expression.  */
5410
5411 static tree
5412 cp_parser_exclusive_or_expression (cp_parser* parser)
5413 {
5414   static const cp_parser_token_tree_map map = {
5415     { CPP_XOR, BIT_XOR_EXPR },
5416     { CPP_EOF, ERROR_MARK }
5417   };
5418
5419   return cp_parser_binary_expression (parser,
5420                                       map,
5421                                       cp_parser_and_expression);
5422 }
5423
5424
5425 /* Parse an inclusive-or-expression.
5426
5427    inclusive-or-expression:
5428      exclusive-or-expression
5429      inclusive-or-expression | exclusive-or-expression
5430
5431    Returns a representation of the expression.  */
5432
5433 static tree
5434 cp_parser_inclusive_or_expression (cp_parser* parser)
5435 {
5436   static const cp_parser_token_tree_map map = {
5437     { CPP_OR, BIT_IOR_EXPR },
5438     { CPP_EOF, ERROR_MARK }
5439   };
5440
5441   return cp_parser_binary_expression (parser,
5442                                       map,
5443                                       cp_parser_exclusive_or_expression);
5444 }
5445
5446 /* Parse a logical-and-expression.
5447
5448    logical-and-expression:
5449      inclusive-or-expression
5450      logical-and-expression && inclusive-or-expression
5451
5452    Returns a representation of the expression.  */
5453
5454 static tree
5455 cp_parser_logical_and_expression (cp_parser* parser)
5456 {
5457   static const cp_parser_token_tree_map map = {
5458     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5459     { CPP_EOF, ERROR_MARK }
5460   };
5461
5462   return cp_parser_binary_expression (parser,
5463                                       map,
5464                                       cp_parser_inclusive_or_expression);
5465 }
5466
5467 /* Parse a logical-or-expression.
5468
5469    logical-or-expression:
5470      logical-and-expression
5471      logical-or-expression || logical-and-expression
5472
5473    Returns a representation of the expression.  */
5474
5475 static tree
5476 cp_parser_logical_or_expression (cp_parser* parser)
5477 {
5478   static const cp_parser_token_tree_map map = {
5479     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5480     { CPP_EOF, ERROR_MARK }
5481   };
5482
5483   return cp_parser_binary_expression (parser,
5484                                       map,
5485                                       cp_parser_logical_and_expression);
5486 }
5487
5488 /* Parse the `? expression : assignment-expression' part of a
5489    conditional-expression.  The LOGICAL_OR_EXPR is the
5490    logical-or-expression that started the conditional-expression.
5491    Returns a representation of the entire conditional-expression.
5492
5493    This routine is used by cp_parser_assignment_expression.
5494
5495      ? expression : assignment-expression
5496
5497    GNU Extensions:
5498
5499      ? : assignment-expression */
5500
5501 static tree
5502 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5503 {
5504   tree expr;
5505   tree assignment_expr;
5506
5507   /* Consume the `?' token.  */
5508   cp_lexer_consume_token (parser->lexer);
5509   if (cp_parser_allow_gnu_extensions_p (parser)
5510       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5511     /* Implicit true clause.  */
5512     expr = NULL_TREE;
5513   else
5514     /* Parse the expression.  */
5515     expr = cp_parser_expression (parser);
5516
5517   /* The next token should be a `:'.  */
5518   cp_parser_require (parser, CPP_COLON, "`:'");
5519   /* Parse the assignment-expression.  */
5520   assignment_expr = cp_parser_assignment_expression (parser);
5521
5522   /* Build the conditional-expression.  */
5523   return build_x_conditional_expr (logical_or_expr,
5524                                    expr,
5525                                    assignment_expr);
5526 }
5527
5528 /* Parse an assignment-expression.
5529
5530    assignment-expression:
5531      conditional-expression
5532      logical-or-expression assignment-operator assignment_expression
5533      throw-expression
5534
5535    Returns a representation for the expression.  */
5536
5537 static tree
5538 cp_parser_assignment_expression (cp_parser* parser)
5539 {
5540   tree expr;
5541
5542   /* If the next token is the `throw' keyword, then we're looking at
5543      a throw-expression.  */
5544   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5545     expr = cp_parser_throw_expression (parser);
5546   /* Otherwise, it must be that we are looking at a
5547      logical-or-expression.  */
5548   else
5549     {
5550       /* Parse the logical-or-expression.  */
5551       expr = cp_parser_logical_or_expression (parser);
5552       /* If the next token is a `?' then we're actually looking at a
5553          conditional-expression.  */
5554       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5555         return cp_parser_question_colon_clause (parser, expr);
5556       else
5557         {
5558           enum tree_code assignment_operator;
5559
5560           /* If it's an assignment-operator, we're using the second
5561              production.  */
5562           assignment_operator
5563             = cp_parser_assignment_operator_opt (parser);
5564           if (assignment_operator != ERROR_MARK)
5565             {
5566               tree rhs;
5567
5568               /* Parse the right-hand side of the assignment.  */
5569               rhs = cp_parser_assignment_expression (parser);
5570               /* An assignment may not appear in a
5571                  constant-expression.  */
5572               if (cp_parser_non_integral_constant_expression (parser,
5573                                                               "an assignment"))
5574                 return error_mark_node;
5575               /* Build the assignment expression.  */
5576               expr = build_x_modify_expr (expr,
5577                                           assignment_operator,
5578                                           rhs);
5579             }
5580         }
5581     }
5582
5583   return expr;
5584 }
5585
5586 /* Parse an (optional) assignment-operator.
5587
5588    assignment-operator: one of
5589      = *= /= %= += -= >>= <<= &= ^= |=
5590
5591    GNU Extension:
5592
5593    assignment-operator: one of
5594      <?= >?=
5595
5596    If the next token is an assignment operator, the corresponding tree
5597    code is returned, and the token is consumed.  For example, for
5598    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5599    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5600    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5601    operator, ERROR_MARK is returned.  */
5602
5603 static enum tree_code
5604 cp_parser_assignment_operator_opt (cp_parser* parser)
5605 {
5606   enum tree_code op;
5607   cp_token *token;
5608
5609   /* Peek at the next toen.  */
5610   token = cp_lexer_peek_token (parser->lexer);
5611
5612   switch (token->type)
5613     {
5614     case CPP_EQ:
5615       op = NOP_EXPR;
5616       break;
5617
5618     case CPP_MULT_EQ:
5619       op = MULT_EXPR;
5620       break;
5621
5622     case CPP_DIV_EQ:
5623       op = TRUNC_DIV_EXPR;
5624       break;
5625
5626     case CPP_MOD_EQ:
5627       op = TRUNC_MOD_EXPR;
5628       break;
5629
5630     case CPP_PLUS_EQ:
5631       op = PLUS_EXPR;
5632       break;
5633
5634     case CPP_MINUS_EQ:
5635       op = MINUS_EXPR;
5636       break;
5637
5638     case CPP_RSHIFT_EQ:
5639       op = RSHIFT_EXPR;
5640       break;
5641
5642     case CPP_LSHIFT_EQ:
5643       op = LSHIFT_EXPR;
5644       break;
5645
5646     case CPP_AND_EQ:
5647       op = BIT_AND_EXPR;
5648       break;
5649
5650     case CPP_XOR_EQ:
5651       op = BIT_XOR_EXPR;
5652       break;
5653
5654     case CPP_OR_EQ:
5655       op = BIT_IOR_EXPR;
5656       break;
5657
5658     case CPP_MIN_EQ:
5659       op = MIN_EXPR;
5660       break;
5661
5662     case CPP_MAX_EQ:
5663       op = MAX_EXPR;
5664       break;
5665
5666     default:
5667       /* Nothing else is an assignment operator.  */
5668       op = ERROR_MARK;
5669     }
5670
5671   /* If it was an assignment operator, consume it.  */
5672   if (op != ERROR_MARK)
5673     cp_lexer_consume_token (parser->lexer);
5674
5675   return op;
5676 }
5677
5678 /* Parse an expression.
5679
5680    expression:
5681      assignment-expression
5682      expression , assignment-expression
5683
5684    Returns a representation of the expression.  */
5685
5686 static tree
5687 cp_parser_expression (cp_parser* parser)
5688 {
5689   tree expression = NULL_TREE;
5690
5691   while (true)
5692     {
5693       tree assignment_expression;
5694
5695       /* Parse the next assignment-expression.  */
5696       assignment_expression
5697         = cp_parser_assignment_expression (parser);
5698       /* If this is the first assignment-expression, we can just
5699          save it away.  */
5700       if (!expression)
5701         expression = assignment_expression;
5702       else
5703         expression = build_x_compound_expr (expression,
5704                                             assignment_expression);
5705       /* If the next token is not a comma, then we are done with the
5706          expression.  */
5707       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5708         break;
5709       /* Consume the `,'.  */
5710       cp_lexer_consume_token (parser->lexer);
5711       /* A comma operator cannot appear in a constant-expression.  */
5712       if (cp_parser_non_integral_constant_expression (parser,
5713                                                       "a comma operator"))
5714         expression = error_mark_node;
5715     }
5716
5717   return expression;
5718 }
5719
5720 /* Parse a constant-expression.
5721
5722    constant-expression:
5723      conditional-expression
5724
5725   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5726   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5727   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5728   is false, NON_CONSTANT_P should be NULL.  */
5729
5730 static tree
5731 cp_parser_constant_expression (cp_parser* parser,
5732                                bool allow_non_constant_p,
5733                                bool *non_constant_p)
5734 {
5735   bool saved_integral_constant_expression_p;
5736   bool saved_allow_non_integral_constant_expression_p;
5737   bool saved_non_integral_constant_expression_p;
5738   tree expression;
5739
5740   /* It might seem that we could simply parse the
5741      conditional-expression, and then check to see if it were
5742      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5743      one that the compiler can figure out is constant, possibly after
5744      doing some simplifications or optimizations.  The standard has a
5745      precise definition of constant-expression, and we must honor
5746      that, even though it is somewhat more restrictive.
5747
5748      For example:
5749
5750        int i[(2, 3)];
5751
5752      is not a legal declaration, because `(2, 3)' is not a
5753      constant-expression.  The `,' operator is forbidden in a
5754      constant-expression.  However, GCC's constant-folding machinery
5755      will fold this operation to an INTEGER_CST for `3'.  */
5756
5757   /* Save the old settings.  */
5758   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5759   saved_allow_non_integral_constant_expression_p
5760     = parser->allow_non_integral_constant_expression_p;
5761   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5762   /* We are now parsing a constant-expression.  */
5763   parser->integral_constant_expression_p = true;
5764   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5765   parser->non_integral_constant_expression_p = false;
5766   /* Although the grammar says "conditional-expression", we parse an
5767      "assignment-expression", which also permits "throw-expression"
5768      and the use of assignment operators.  In the case that
5769      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5770      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5771      actually essential that we look for an assignment-expression.
5772      For example, cp_parser_initializer_clauses uses this function to
5773      determine whether a particular assignment-expression is in fact
5774      constant.  */
5775   expression = cp_parser_assignment_expression (parser);
5776   /* Restore the old settings.  */
5777   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5778   parser->allow_non_integral_constant_expression_p
5779     = saved_allow_non_integral_constant_expression_p;
5780   if (allow_non_constant_p)
5781     *non_constant_p = parser->non_integral_constant_expression_p;
5782   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5783
5784   return expression;
5785 }
5786
5787 /* Parse __builtin_offsetof.
5788
5789    offsetof-expression:
5790      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5791
5792    offsetof-member-designator:
5793      id-expression
5794      | offsetof-member-designator "." id-expression
5795      | offsetof-member-designator "[" expression "]"
5796 */
5797
5798 static tree
5799 cp_parser_builtin_offsetof (cp_parser *parser)
5800 {
5801   int save_ice_p, save_non_ice_p;
5802   tree type, expr;
5803   cp_id_kind dummy;
5804
5805   /* We're about to accept non-integral-constant things, but will
5806      definitely yield an integral constant expression.  Save and
5807      restore these values around our local parsing.  */
5808   save_ice_p = parser->integral_constant_expression_p;
5809   save_non_ice_p = parser->non_integral_constant_expression_p;
5810
5811   /* Consume the "__builtin_offsetof" token.  */
5812   cp_lexer_consume_token (parser->lexer);
5813   /* Consume the opening `('.  */
5814   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5815   /* Parse the type-id.  */
5816   type = cp_parser_type_id (parser);
5817   /* Look for the `,'.  */
5818   cp_parser_require (parser, CPP_COMMA, "`,'");
5819
5820   /* Build the (type *)null that begins the traditional offsetof macro.  */
5821   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5822
5823   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5824   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5825                                                  true, &dummy);
5826   while (true)
5827     {
5828       cp_token *token = cp_lexer_peek_token (parser->lexer);
5829       switch (token->type)
5830         {
5831         case CPP_OPEN_SQUARE:
5832           /* offsetof-member-designator "[" expression "]" */
5833           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5834           break;
5835
5836         case CPP_DOT:
5837           /* offsetof-member-designator "." identifier */
5838           cp_lexer_consume_token (parser->lexer);
5839           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5840                                                          true, &dummy);
5841           break;
5842
5843         case CPP_CLOSE_PAREN:
5844           /* Consume the ")" token.  */
5845           cp_lexer_consume_token (parser->lexer);
5846           goto success;
5847
5848         default:
5849           /* Error.  We know the following require will fail, but
5850              that gives the proper error message.  */
5851           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5852           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5853           expr = error_mark_node;
5854           goto failure;
5855         }
5856     }
5857
5858  success:
5859   /* We've finished the parsing, now finish with the semantics.  At present
5860      we're just mirroring the traditional macro implementation.  Better
5861      would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5862      here rather than in build_x_unary_op.  */
5863   expr = build_reinterpret_cast (build_reference_type (char_type_node), expr);
5864   expr = build_x_unary_op (ADDR_EXPR, expr);
5865   expr = build_reinterpret_cast (size_type_node, expr);
5866
5867  failure:
5868   parser->integral_constant_expression_p = save_ice_p;
5869   parser->non_integral_constant_expression_p = save_non_ice_p;
5870
5871   return expr;
5872 }
5873
5874 /* Statements [gram.stmt.stmt]  */
5875
5876 /* Parse a statement.
5877
5878    statement:
5879      labeled-statement
5880      expression-statement
5881      compound-statement
5882      selection-statement
5883      iteration-statement
5884      jump-statement
5885      declaration-statement
5886      try-block  */
5887
5888 static void
5889 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5890 {
5891   tree statement;
5892   cp_token *token;
5893   location_t statement_locus;
5894
5895   /* There is no statement yet.  */
5896   statement = NULL_TREE;
5897   /* Peek at the next token.  */
5898   token = cp_lexer_peek_token (parser->lexer);
5899   /* Remember the location of the first token in the statement.  */
5900   statement_locus = token->location;
5901   /* If this is a keyword, then that will often determine what kind of
5902      statement we have.  */
5903   if (token->type == CPP_KEYWORD)
5904     {
5905       enum rid keyword = token->keyword;
5906
5907       switch (keyword)
5908         {
5909         case RID_CASE:
5910         case RID_DEFAULT:
5911           statement = cp_parser_labeled_statement (parser,
5912                                                    in_statement_expr);
5913           break;
5914
5915         case RID_IF:
5916         case RID_SWITCH:
5917           statement = cp_parser_selection_statement (parser);
5918           break;
5919
5920         case RID_WHILE:
5921         case RID_DO:
5922         case RID_FOR:
5923           statement = cp_parser_iteration_statement (parser);
5924           break;
5925
5926         case RID_BREAK:
5927         case RID_CONTINUE:
5928         case RID_RETURN:
5929         case RID_GOTO:
5930           statement = cp_parser_jump_statement (parser);
5931           break;
5932
5933         case RID_TRY:
5934           statement = cp_parser_try_block (parser);
5935           break;
5936
5937         default:
5938           /* It might be a keyword like `int' that can start a
5939              declaration-statement.  */
5940           break;
5941         }
5942     }
5943   else if (token->type == CPP_NAME)
5944     {
5945       /* If the next token is a `:', then we are looking at a
5946          labeled-statement.  */
5947       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5948       if (token->type == CPP_COLON)
5949         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5950     }
5951   /* Anything that starts with a `{' must be a compound-statement.  */
5952   else if (token->type == CPP_OPEN_BRACE)
5953     statement = cp_parser_compound_statement (parser, NULL, false);
5954
5955   /* Everything else must be a declaration-statement or an
5956      expression-statement.  Try for the declaration-statement
5957      first, unless we are looking at a `;', in which case we know that
5958      we have an expression-statement.  */
5959   if (!statement)
5960     {
5961       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5962         {
5963           cp_parser_parse_tentatively (parser);
5964           /* Try to parse the declaration-statement.  */
5965           cp_parser_declaration_statement (parser);
5966           /* If that worked, we're done.  */
5967           if (cp_parser_parse_definitely (parser))
5968             return;
5969         }
5970       /* Look for an expression-statement instead.  */
5971       statement = cp_parser_expression_statement (parser, in_statement_expr);
5972     }
5973
5974   /* Set the line number for the statement.  */
5975   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5976     {
5977       SET_EXPR_LOCUS (statement, NULL);
5978       annotate_with_locus (statement, statement_locus);
5979     }
5980 }
5981
5982 /* Parse a labeled-statement.
5983
5984    labeled-statement:
5985      identifier : statement
5986      case constant-expression : statement
5987      default : statement
5988
5989    GNU Extension:
5990
5991    labeled-statement:
5992      case constant-expression ... constant-expression : statement
5993
5994    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5995    For an ordinary label, returns a LABEL_EXPR.  */
5996
5997 static tree
5998 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5999 {
6000   cp_token *token;
6001   tree statement = error_mark_node;
6002
6003   /* The next token should be an identifier.  */
6004   token = cp_lexer_peek_token (parser->lexer);
6005   if (token->type != CPP_NAME
6006       && token->type != CPP_KEYWORD)
6007     {
6008       cp_parser_error (parser, "expected labeled-statement");
6009       return error_mark_node;
6010     }
6011
6012   switch (token->keyword)
6013     {
6014     case RID_CASE:
6015       {
6016         tree expr, expr_hi;
6017         cp_token *ellipsis;
6018
6019         /* Consume the `case' token.  */
6020         cp_lexer_consume_token (parser->lexer);
6021         /* Parse the constant-expression.  */
6022         expr = cp_parser_constant_expression (parser,
6023                                               /*allow_non_constant_p=*/false,
6024                                               NULL);
6025
6026         ellipsis = cp_lexer_peek_token (parser->lexer);
6027         if (ellipsis->type == CPP_ELLIPSIS)
6028           {
6029             /* Consume the `...' token.  */
6030             cp_lexer_consume_token (parser->lexer);
6031             expr_hi =
6032               cp_parser_constant_expression (parser,
6033                                              /*allow_non_constant_p=*/false,
6034                                              NULL);
6035             /* We don't need to emit warnings here, as the common code
6036                will do this for us.  */
6037           }
6038         else
6039           expr_hi = NULL_TREE;
6040
6041         if (!parser->in_switch_statement_p)
6042           error ("case label `%E' not within a switch statement", expr);
6043         else
6044           statement = finish_case_label (expr, expr_hi);
6045       }
6046       break;
6047
6048     case RID_DEFAULT:
6049       /* Consume the `default' token.  */
6050       cp_lexer_consume_token (parser->lexer);
6051       if (!parser->in_switch_statement_p)
6052         error ("case label not within a switch statement");
6053       else
6054         statement = finish_case_label (NULL_TREE, NULL_TREE);
6055       break;
6056
6057     default:
6058       /* Anything else must be an ordinary label.  */
6059       statement = finish_label_stmt (cp_parser_identifier (parser));
6060       break;
6061     }
6062
6063   /* Require the `:' token.  */
6064   cp_parser_require (parser, CPP_COLON, "`:'");
6065   /* Parse the labeled statement.  */
6066   cp_parser_statement (parser, in_statement_expr);
6067
6068   /* Return the label, in the case of a `case' or `default' label.  */
6069   return statement;
6070 }
6071
6072 /* Parse an expression-statement.
6073
6074    expression-statement:
6075      expression [opt] ;
6076
6077    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6078    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6079    indicates whether this expression-statement is part of an
6080    expression statement.  */
6081
6082 static tree
6083 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6084 {
6085   tree statement = NULL_TREE;
6086
6087   /* If the next token is a ';', then there is no expression
6088      statement.  */
6089   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6090     statement = cp_parser_expression (parser);
6091
6092   /* Consume the final `;'.  */
6093   cp_parser_consume_semicolon_at_end_of_statement (parser);
6094
6095   if (in_statement_expr
6096       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6097     {
6098       /* This is the final expression statement of a statement
6099          expression.  */
6100       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6101     }
6102   else if (statement)
6103     statement = finish_expr_stmt (statement);
6104   else
6105     finish_stmt ();
6106
6107   return statement;
6108 }
6109
6110 /* Parse a compound-statement.
6111
6112    compound-statement:
6113      { statement-seq [opt] }
6114
6115    Returns a tree representing the statement.  */
6116
6117 static tree
6118 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6119                               bool in_try)
6120 {
6121   tree compound_stmt;
6122
6123   /* Consume the `{'.  */
6124   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6125     return error_mark_node;
6126   /* Begin the compound-statement.  */
6127   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6128   /* Parse an (optional) statement-seq.  */
6129   cp_parser_statement_seq_opt (parser, in_statement_expr);
6130   /* Finish the compound-statement.  */
6131   finish_compound_stmt (compound_stmt);
6132   /* Consume the `}'.  */
6133   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6134
6135   return compound_stmt;
6136 }
6137
6138 /* Parse an (optional) statement-seq.
6139
6140    statement-seq:
6141      statement
6142      statement-seq [opt] statement  */
6143
6144 static void
6145 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6146 {
6147   /* Scan statements until there aren't any more.  */
6148   while (true)
6149     {
6150       /* If we're looking at a `}', then we've run out of statements.  */
6151       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6152           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6153         break;
6154
6155       /* Parse the statement.  */
6156       cp_parser_statement (parser, in_statement_expr);
6157     }
6158 }
6159
6160 /* Parse a selection-statement.
6161
6162    selection-statement:
6163      if ( condition ) statement
6164      if ( condition ) statement else statement
6165      switch ( condition ) statement
6166
6167    Returns the new IF_STMT or SWITCH_STMT.  */
6168
6169 static tree
6170 cp_parser_selection_statement (cp_parser* parser)
6171 {
6172   cp_token *token;
6173   enum rid keyword;
6174
6175   /* Peek at the next token.  */
6176   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6177
6178   /* See what kind of keyword it is.  */
6179   keyword = token->keyword;
6180   switch (keyword)
6181     {
6182     case RID_IF:
6183     case RID_SWITCH:
6184       {
6185         tree statement;
6186         tree condition;
6187
6188         /* Look for the `('.  */
6189         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6190           {
6191             cp_parser_skip_to_end_of_statement (parser);
6192             return error_mark_node;
6193           }
6194
6195         /* Begin the selection-statement.  */
6196         if (keyword == RID_IF)
6197           statement = begin_if_stmt ();
6198         else
6199           statement = begin_switch_stmt ();
6200
6201         /* Parse the condition.  */
6202         condition = cp_parser_condition (parser);
6203         /* Look for the `)'.  */
6204         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6205           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6206                                                  /*consume_paren=*/true);
6207
6208         if (keyword == RID_IF)
6209           {
6210             /* Add the condition.  */
6211             finish_if_stmt_cond (condition, statement);
6212
6213             /* Parse the then-clause.  */
6214             cp_parser_implicitly_scoped_statement (parser);
6215             finish_then_clause (statement);
6216
6217             /* If the next token is `else', parse the else-clause.  */
6218             if (cp_lexer_next_token_is_keyword (parser->lexer,
6219                                                 RID_ELSE))
6220               {
6221                 /* Consume the `else' keyword.  */
6222                 cp_lexer_consume_token (parser->lexer);
6223                 begin_else_clause (statement);
6224                 /* Parse the else-clause.  */
6225                 cp_parser_implicitly_scoped_statement (parser);
6226                 finish_else_clause (statement);
6227               }
6228
6229             /* Now we're all done with the if-statement.  */
6230             finish_if_stmt (statement);
6231           }
6232         else
6233           {
6234             bool in_switch_statement_p;
6235
6236             /* Add the condition.  */
6237             finish_switch_cond (condition, statement);
6238
6239             /* Parse the body of the switch-statement.  */
6240             in_switch_statement_p = parser->in_switch_statement_p;
6241             parser->in_switch_statement_p = true;
6242             cp_parser_implicitly_scoped_statement (parser);
6243             parser->in_switch_statement_p = in_switch_statement_p;
6244
6245             /* Now we're all done with the switch-statement.  */
6246             finish_switch_stmt (statement);
6247           }
6248
6249         return statement;
6250       }
6251       break;
6252
6253     default:
6254       cp_parser_error (parser, "expected selection-statement");
6255       return error_mark_node;
6256     }
6257 }
6258
6259 /* Parse a condition.
6260
6261    condition:
6262      expression
6263      type-specifier-seq declarator = assignment-expression
6264
6265    GNU Extension:
6266
6267    condition:
6268      type-specifier-seq declarator asm-specification [opt]
6269        attributes [opt] = assignment-expression
6270
6271    Returns the expression that should be tested.  */
6272
6273 static tree
6274 cp_parser_condition (cp_parser* parser)
6275 {
6276   cp_decl_specifier_seq type_specifiers;
6277   const char *saved_message;
6278
6279   /* Try the declaration first.  */
6280   cp_parser_parse_tentatively (parser);
6281   /* New types are not allowed in the type-specifier-seq for a
6282      condition.  */
6283   saved_message = parser->type_definition_forbidden_message;
6284   parser->type_definition_forbidden_message
6285     = "types may not be defined in conditions";
6286   /* Parse the type-specifier-seq.  */
6287   cp_parser_type_specifier_seq (parser, &type_specifiers);
6288   /* Restore the saved message.  */
6289   parser->type_definition_forbidden_message = saved_message;
6290   /* If all is well, we might be looking at a declaration.  */
6291   if (!cp_parser_error_occurred (parser))
6292     {
6293       tree decl;
6294       tree asm_specification;
6295       tree attributes;
6296       cp_declarator *declarator;
6297       tree initializer = NULL_TREE;
6298
6299       /* Parse the declarator.  */
6300       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6301                                          /*ctor_dtor_or_conv_p=*/NULL,
6302                                          /*parenthesized_p=*/NULL);
6303       /* Parse the attributes.  */
6304       attributes = cp_parser_attributes_opt (parser);
6305       /* Parse the asm-specification.  */
6306       asm_specification = cp_parser_asm_specification_opt (parser);
6307       /* If the next token is not an `=', then we might still be
6308          looking at an expression.  For example:
6309
6310            if (A(a).x)
6311
6312          looks like a decl-specifier-seq and a declarator -- but then
6313          there is no `=', so this is an expression.  */
6314       cp_parser_require (parser, CPP_EQ, "`='");
6315       /* If we did see an `=', then we are looking at a declaration
6316          for sure.  */
6317       if (cp_parser_parse_definitely (parser))
6318         {
6319           /* Create the declaration.  */
6320           decl = start_decl (declarator, &type_specifiers,
6321                              /*initialized_p=*/true,
6322                              attributes, /*prefix_attributes=*/NULL_TREE);
6323           /* Parse the assignment-expression.  */
6324           initializer = cp_parser_assignment_expression (parser);
6325
6326           /* Process the initializer.  */
6327           cp_finish_decl (decl,
6328                           initializer,
6329                           asm_specification,
6330                           LOOKUP_ONLYCONVERTING);
6331
6332           return convert_from_reference (decl);
6333         }
6334     }
6335   /* If we didn't even get past the declarator successfully, we are
6336      definitely not looking at a declaration.  */
6337   else
6338     cp_parser_abort_tentative_parse (parser);
6339
6340   /* Otherwise, we are looking at an expression.  */
6341   return cp_parser_expression (parser);
6342 }
6343
6344 /* Parse an iteration-statement.
6345
6346    iteration-statement:
6347      while ( condition ) statement
6348      do statement while ( expression ) ;
6349      for ( for-init-statement condition [opt] ; expression [opt] )
6350        statement
6351
6352    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6353
6354 static tree
6355 cp_parser_iteration_statement (cp_parser* parser)
6356 {
6357   cp_token *token;
6358   enum rid keyword;
6359   tree statement;
6360   bool in_iteration_statement_p;
6361
6362
6363   /* Peek at the next token.  */
6364   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6365   if (!token)
6366     return error_mark_node;
6367
6368   /* Remember whether or not we are already within an iteration
6369      statement.  */
6370   in_iteration_statement_p = parser->in_iteration_statement_p;
6371
6372   /* See what kind of keyword it is.  */
6373   keyword = token->keyword;
6374   switch (keyword)
6375     {
6376     case RID_WHILE:
6377       {
6378         tree condition;
6379
6380         /* Begin the while-statement.  */
6381         statement = begin_while_stmt ();
6382         /* Look for the `('.  */
6383         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6384         /* Parse the condition.  */
6385         condition = cp_parser_condition (parser);
6386         finish_while_stmt_cond (condition, statement);
6387         /* Look for the `)'.  */
6388         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6389         /* Parse the dependent statement.  */
6390         parser->in_iteration_statement_p = true;
6391         cp_parser_already_scoped_statement (parser);
6392         parser->in_iteration_statement_p = in_iteration_statement_p;
6393         /* We're done with the while-statement.  */
6394         finish_while_stmt (statement);
6395       }
6396       break;
6397
6398     case RID_DO:
6399       {
6400         tree expression;
6401
6402         /* Begin the do-statement.  */
6403         statement = begin_do_stmt ();
6404         /* Parse the body of the do-statement.  */
6405         parser->in_iteration_statement_p = true;
6406         cp_parser_implicitly_scoped_statement (parser);
6407         parser->in_iteration_statement_p = in_iteration_statement_p;
6408         finish_do_body (statement);
6409         /* Look for the `while' keyword.  */
6410         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6411         /* Look for the `('.  */
6412         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6413         /* Parse the expression.  */
6414         expression = cp_parser_expression (parser);
6415         /* We're done with the do-statement.  */
6416         finish_do_stmt (expression, statement);
6417         /* Look for the `)'.  */
6418         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6419         /* Look for the `;'.  */
6420         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6421       }
6422       break;
6423
6424     case RID_FOR:
6425       {
6426         tree condition = NULL_TREE;
6427         tree expression = NULL_TREE;
6428
6429         /* Begin the for-statement.  */
6430         statement = begin_for_stmt ();
6431         /* Look for the `('.  */
6432         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6433         /* Parse the initialization.  */
6434         cp_parser_for_init_statement (parser);
6435         finish_for_init_stmt (statement);
6436
6437         /* If there's a condition, process it.  */
6438         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6439           condition = cp_parser_condition (parser);
6440         finish_for_cond (condition, statement);
6441         /* Look for the `;'.  */
6442         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6443
6444         /* If there's an expression, process it.  */
6445         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6446           expression = cp_parser_expression (parser);
6447         finish_for_expr (expression, statement);
6448         /* Look for the `)'.  */
6449         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6450         
6451         /* Parse the body of the for-statement.  */
6452         parser->in_iteration_statement_p = true;
6453         cp_parser_already_scoped_statement (parser);
6454         parser->in_iteration_statement_p = in_iteration_statement_p;
6455
6456         /* We're done with the for-statement.  */
6457         finish_for_stmt (statement);
6458       }
6459       break;
6460
6461     default:
6462       cp_parser_error (parser, "expected iteration-statement");
6463       statement = error_mark_node;
6464       break;
6465     }
6466
6467   return statement;
6468 }
6469
6470 /* Parse a for-init-statement.
6471
6472    for-init-statement:
6473      expression-statement
6474      simple-declaration  */
6475
6476 static void
6477 cp_parser_for_init_statement (cp_parser* parser)
6478 {
6479   /* If the next token is a `;', then we have an empty
6480      expression-statement.  Grammatically, this is also a
6481      simple-declaration, but an invalid one, because it does not
6482      declare anything.  Therefore, if we did not handle this case
6483      specially, we would issue an error message about an invalid
6484      declaration.  */
6485   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6486     {
6487       /* We're going to speculatively look for a declaration, falling back
6488          to an expression, if necessary.  */
6489       cp_parser_parse_tentatively (parser);
6490       /* Parse the declaration.  */
6491       cp_parser_simple_declaration (parser,
6492                                     /*function_definition_allowed_p=*/false);
6493       /* If the tentative parse failed, then we shall need to look for an
6494          expression-statement.  */
6495       if (cp_parser_parse_definitely (parser))
6496         return;
6497     }
6498
6499   cp_parser_expression_statement (parser, false);
6500 }
6501
6502 /* Parse a jump-statement.
6503
6504    jump-statement:
6505      break ;
6506      continue ;
6507      return expression [opt] ;
6508      goto identifier ;
6509
6510    GNU extension:
6511
6512    jump-statement:
6513      goto * expression ;
6514
6515    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6516
6517 static tree
6518 cp_parser_jump_statement (cp_parser* parser)
6519 {
6520   tree statement = error_mark_node;
6521   cp_token *token;
6522   enum rid keyword;
6523
6524   /* Peek at the next token.  */
6525   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6526   if (!token)
6527     return error_mark_node;
6528
6529   /* See what kind of keyword it is.  */
6530   keyword = token->keyword;
6531   switch (keyword)
6532     {
6533     case RID_BREAK:
6534       if (!parser->in_switch_statement_p
6535           && !parser->in_iteration_statement_p)
6536         {
6537           error ("break statement not within loop or switch");
6538           statement = error_mark_node;
6539         }
6540       else
6541         statement = finish_break_stmt ();
6542       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6543       break;
6544
6545     case RID_CONTINUE:
6546       if (!parser->in_iteration_statement_p)
6547         {
6548           error ("continue statement not within a loop");
6549           statement = error_mark_node;
6550         }
6551       else
6552         statement = finish_continue_stmt ();
6553       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6554       break;
6555
6556     case RID_RETURN:
6557       {
6558         tree expr;
6559
6560         /* If the next token is a `;', then there is no
6561            expression.  */
6562         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6563           expr = cp_parser_expression (parser);
6564         else
6565           expr = NULL_TREE;
6566         /* Build the return-statement.  */
6567         statement = finish_return_stmt (expr);
6568         /* Look for the final `;'.  */
6569         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6570       }
6571       break;
6572
6573     case RID_GOTO:
6574       /* Create the goto-statement.  */
6575       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6576         {
6577           /* Issue a warning about this use of a GNU extension.  */
6578           if (pedantic)
6579             pedwarn ("ISO C++ forbids computed gotos");
6580           /* Consume the '*' token.  */
6581           cp_lexer_consume_token (parser->lexer);
6582           /* Parse the dependent expression.  */
6583           finish_goto_stmt (cp_parser_expression (parser));
6584         }
6585       else
6586         finish_goto_stmt (cp_parser_identifier (parser));
6587       /* Look for the final `;'.  */
6588       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6589       break;
6590
6591     default:
6592       cp_parser_error (parser, "expected jump-statement");
6593       break;
6594     }
6595
6596   return statement;
6597 }
6598
6599 /* Parse a declaration-statement.
6600
6601    declaration-statement:
6602      block-declaration  */
6603
6604 static void
6605 cp_parser_declaration_statement (cp_parser* parser)
6606 {
6607   void *p;
6608
6609   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6610   p = obstack_alloc (&declarator_obstack, 0);
6611
6612  /* Parse the block-declaration.  */
6613   cp_parser_block_declaration (parser, /*statement_p=*/true);
6614
6615   /* Free any declarators allocated.  */
6616   obstack_free (&declarator_obstack, p);
6617
6618   /* Finish off the statement.  */
6619   finish_stmt ();
6620 }
6621
6622 /* Some dependent statements (like `if (cond) statement'), are
6623    implicitly in their own scope.  In other words, if the statement is
6624    a single statement (as opposed to a compound-statement), it is
6625    none-the-less treated as if it were enclosed in braces.  Any
6626    declarations appearing in the dependent statement are out of scope
6627    after control passes that point.  This function parses a statement,
6628    but ensures that is in its own scope, even if it is not a
6629    compound-statement.
6630
6631    Returns the new statement.  */
6632
6633 static tree
6634 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6635 {
6636   tree statement;
6637
6638   /* If the token is not a `{', then we must take special action.  */
6639   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6640     {
6641       /* Create a compound-statement.  */
6642       statement = begin_compound_stmt (0);
6643       /* Parse the dependent-statement.  */
6644       cp_parser_statement (parser, false);
6645       /* Finish the dummy compound-statement.  */
6646       finish_compound_stmt (statement);
6647     }
6648   /* Otherwise, we simply parse the statement directly.  */
6649   else
6650     statement = cp_parser_compound_statement (parser, NULL, false);
6651
6652   /* Return the statement.  */
6653   return statement;
6654 }
6655
6656 /* For some dependent statements (like `while (cond) statement'), we
6657    have already created a scope.  Therefore, even if the dependent
6658    statement is a compound-statement, we do not want to create another
6659    scope.  */
6660
6661 static void
6662 cp_parser_already_scoped_statement (cp_parser* parser)
6663 {
6664   /* If the token is a `{', then we must take special action.  */
6665   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6666     cp_parser_statement (parser, false);
6667   else
6668     {
6669       /* Avoid calling cp_parser_compound_statement, so that we
6670          don't create a new scope.  Do everything else by hand.  */
6671       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6672       cp_parser_statement_seq_opt (parser, false);
6673       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6674     }
6675 }
6676
6677 /* Declarations [gram.dcl.dcl] */
6678
6679 /* Parse an optional declaration-sequence.
6680
6681    declaration-seq:
6682      declaration
6683      declaration-seq declaration  */
6684
6685 static void
6686 cp_parser_declaration_seq_opt (cp_parser* parser)
6687 {
6688   while (true)
6689     {
6690       cp_token *token;
6691
6692       token = cp_lexer_peek_token (parser->lexer);
6693
6694       if (token->type == CPP_CLOSE_BRACE
6695           || token->type == CPP_EOF)
6696         break;
6697
6698       if (token->type == CPP_SEMICOLON)
6699         {
6700           /* A declaration consisting of a single semicolon is
6701              invalid.  Allow it unless we're being pedantic.  */
6702           if (pedantic && !in_system_header)
6703             pedwarn ("extra `;'");
6704           cp_lexer_consume_token (parser->lexer);
6705           continue;
6706         }
6707
6708       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6709          parser to enter or exit implicit `extern "C"' blocks.  */
6710       while (pending_lang_change > 0)
6711         {
6712           push_lang_context (lang_name_c);
6713           --pending_lang_change;
6714         }
6715       while (pending_lang_change < 0)
6716         {
6717           pop_lang_context ();
6718           ++pending_lang_change;
6719         }
6720
6721       /* Parse the declaration itself.  */
6722       cp_parser_declaration (parser);
6723     }
6724 }
6725
6726 /* Parse a declaration.
6727
6728    declaration:
6729      block-declaration
6730      function-definition
6731      template-declaration
6732      explicit-instantiation
6733      explicit-specialization
6734      linkage-specification
6735      namespace-definition
6736
6737    GNU extension:
6738
6739    declaration:
6740       __extension__ declaration */
6741
6742 static void
6743 cp_parser_declaration (cp_parser* parser)
6744 {
6745   cp_token token1;
6746   cp_token token2;
6747   int saved_pedantic;
6748   void *p;
6749
6750   /* Set this here since we can be called after
6751      pushing the linkage specification.  */
6752   c_lex_string_translate = 1;
6753
6754   /* Check for the `__extension__' keyword.  */
6755   if (cp_parser_extension_opt (parser, &saved_pedantic))
6756     {
6757       /* Parse the qualified declaration.  */
6758       cp_parser_declaration (parser);
6759       /* Restore the PEDANTIC flag.  */
6760       pedantic = saved_pedantic;
6761
6762       return;
6763     }
6764
6765   /* Try to figure out what kind of declaration is present.  */
6766   token1 = *cp_lexer_peek_token (parser->lexer);
6767
6768   /* Don't translate the CPP_STRING in extern "C".  */
6769   if (token1.keyword == RID_EXTERN)
6770     c_lex_string_translate = 0;
6771
6772   if (token1.type != CPP_EOF)
6773     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6774
6775   c_lex_string_translate = 1;
6776
6777   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6778   p = obstack_alloc (&declarator_obstack, 0);
6779
6780   /* If the next token is `extern' and the following token is a string
6781      literal, then we have a linkage specification.  */
6782   if (token1.keyword == RID_EXTERN
6783       && cp_parser_is_string_literal (&token2))
6784     cp_parser_linkage_specification (parser);
6785   /* If the next token is `template', then we have either a template
6786      declaration, an explicit instantiation, or an explicit
6787      specialization.  */
6788   else if (token1.keyword == RID_TEMPLATE)
6789     {
6790       /* `template <>' indicates a template specialization.  */
6791       if (token2.type == CPP_LESS
6792           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6793         cp_parser_explicit_specialization (parser);
6794       /* `template <' indicates a template declaration.  */
6795       else if (token2.type == CPP_LESS)
6796         cp_parser_template_declaration (parser, /*member_p=*/false);
6797       /* Anything else must be an explicit instantiation.  */
6798       else
6799         cp_parser_explicit_instantiation (parser);
6800     }
6801   /* If the next token is `export', then we have a template
6802      declaration.  */
6803   else if (token1.keyword == RID_EXPORT)
6804     cp_parser_template_declaration (parser, /*member_p=*/false);
6805   /* If the next token is `extern', 'static' or 'inline' and the one
6806      after that is `template', we have a GNU extended explicit
6807      instantiation directive.  */
6808   else if (cp_parser_allow_gnu_extensions_p (parser)
6809            && (token1.keyword == RID_EXTERN
6810                || token1.keyword == RID_STATIC
6811                || token1.keyword == RID_INLINE)
6812            && token2.keyword == RID_TEMPLATE)
6813     cp_parser_explicit_instantiation (parser);
6814   /* If the next token is `namespace', check for a named or unnamed
6815      namespace definition.  */
6816   else if (token1.keyword == RID_NAMESPACE
6817            && (/* A named namespace definition.  */
6818                (token2.type == CPP_NAME
6819                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6820                     == CPP_OPEN_BRACE))
6821                /* An unnamed namespace definition.  */
6822                || token2.type == CPP_OPEN_BRACE))
6823     cp_parser_namespace_definition (parser);
6824   /* We must have either a block declaration or a function
6825      definition.  */
6826   else
6827     /* Try to parse a block-declaration, or a function-definition.  */
6828     cp_parser_block_declaration (parser, /*statement_p=*/false);
6829
6830   /* Free any declarators allocated.  */
6831   obstack_free (&declarator_obstack, p);
6832 }
6833
6834 /* Parse a block-declaration.
6835
6836    block-declaration:
6837      simple-declaration
6838      asm-definition
6839      namespace-alias-definition
6840      using-declaration
6841      using-directive
6842
6843    GNU Extension:
6844
6845    block-declaration:
6846      __extension__ block-declaration
6847      label-declaration
6848
6849    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6850    part of a declaration-statement.  */
6851
6852 static void
6853 cp_parser_block_declaration (cp_parser *parser,
6854                              bool      statement_p)
6855 {
6856   cp_token *token1;
6857   int saved_pedantic;
6858
6859   /* Check for the `__extension__' keyword.  */
6860   if (cp_parser_extension_opt (parser, &saved_pedantic))
6861     {
6862       /* Parse the qualified declaration.  */
6863       cp_parser_block_declaration (parser, statement_p);
6864       /* Restore the PEDANTIC flag.  */
6865       pedantic = saved_pedantic;
6866
6867       return;
6868     }
6869
6870   /* Peek at the next token to figure out which kind of declaration is
6871      present.  */
6872   token1 = cp_lexer_peek_token (parser->lexer);
6873
6874   /* If the next keyword is `asm', we have an asm-definition.  */
6875   if (token1->keyword == RID_ASM)
6876     {
6877       if (statement_p)
6878         cp_parser_commit_to_tentative_parse (parser);
6879       cp_parser_asm_definition (parser);
6880     }
6881   /* If the next keyword is `namespace', we have a
6882      namespace-alias-definition.  */
6883   else if (token1->keyword == RID_NAMESPACE)
6884     cp_parser_namespace_alias_definition (parser);
6885   /* If the next keyword is `using', we have either a
6886      using-declaration or a using-directive.  */
6887   else if (token1->keyword == RID_USING)
6888     {
6889       cp_token *token2;
6890
6891       if (statement_p)
6892         cp_parser_commit_to_tentative_parse (parser);
6893       /* If the token after `using' is `namespace', then we have a
6894          using-directive.  */
6895       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6896       if (token2->keyword == RID_NAMESPACE)
6897         cp_parser_using_directive (parser);
6898       /* Otherwise, it's a using-declaration.  */
6899       else
6900         cp_parser_using_declaration (parser);
6901     }
6902   /* If the next keyword is `__label__' we have a label declaration.  */
6903   else if (token1->keyword == RID_LABEL)
6904     {
6905       if (statement_p)
6906         cp_parser_commit_to_tentative_parse (parser);
6907       cp_parser_label_declaration (parser);
6908     }
6909   /* Anything else must be a simple-declaration.  */
6910   else
6911     cp_parser_simple_declaration (parser, !statement_p);
6912 }
6913
6914 /* Parse a simple-declaration.
6915
6916    simple-declaration:
6917      decl-specifier-seq [opt] init-declarator-list [opt] ;
6918
6919    init-declarator-list:
6920      init-declarator
6921      init-declarator-list , init-declarator
6922
6923    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6924    function-definition as a simple-declaration.  */
6925
6926 static void
6927 cp_parser_simple_declaration (cp_parser* parser,
6928                               bool function_definition_allowed_p)
6929 {
6930   cp_decl_specifier_seq decl_specifiers;
6931   int declares_class_or_enum;
6932   bool saw_declarator;
6933
6934   /* Defer access checks until we know what is being declared; the
6935      checks for names appearing in the decl-specifier-seq should be
6936      done as if we were in the scope of the thing being declared.  */
6937   push_deferring_access_checks (dk_deferred);
6938
6939   /* Parse the decl-specifier-seq.  We have to keep track of whether
6940      or not the decl-specifier-seq declares a named class or
6941      enumeration type, since that is the only case in which the
6942      init-declarator-list is allowed to be empty.
6943
6944      [dcl.dcl]
6945
6946      In a simple-declaration, the optional init-declarator-list can be
6947      omitted only when declaring a class or enumeration, that is when
6948      the decl-specifier-seq contains either a class-specifier, an
6949      elaborated-type-specifier, or an enum-specifier.  */
6950   cp_parser_decl_specifier_seq (parser,
6951                                 CP_PARSER_FLAGS_OPTIONAL,
6952                                 &decl_specifiers,
6953                                 &declares_class_or_enum);
6954   /* We no longer need to defer access checks.  */
6955   stop_deferring_access_checks ();
6956
6957   /* In a block scope, a valid declaration must always have a
6958      decl-specifier-seq.  By not trying to parse declarators, we can
6959      resolve the declaration/expression ambiguity more quickly.  */
6960   if (!function_definition_allowed_p 
6961       && !decl_specifiers.any_specifiers_p)
6962     {
6963       cp_parser_error (parser, "expected declaration");
6964       goto done;
6965     }
6966
6967   /* If the next two tokens are both identifiers, the code is
6968      erroneous. The usual cause of this situation is code like:
6969
6970        T t;
6971
6972      where "T" should name a type -- but does not.  */
6973   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6974     {
6975       /* If parsing tentatively, we should commit; we really are
6976          looking at a declaration.  */
6977       cp_parser_commit_to_tentative_parse (parser);
6978       /* Give up.  */
6979       goto done;
6980     }
6981
6982   /* Keep going until we hit the `;' at the end of the simple
6983      declaration.  */
6984   saw_declarator = false;
6985   while (cp_lexer_next_token_is_not (parser->lexer,
6986                                      CPP_SEMICOLON))
6987     {
6988       cp_token *token;
6989       bool function_definition_p;
6990       tree decl;
6991
6992       saw_declarator = true;
6993       /* Parse the init-declarator.  */
6994       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6995                                         function_definition_allowed_p,
6996                                         /*member_p=*/false,
6997                                         declares_class_or_enum,
6998                                         &function_definition_p);
6999       /* If an error occurred while parsing tentatively, exit quickly.
7000          (That usually happens when in the body of a function; each
7001          statement is treated as a declaration-statement until proven
7002          otherwise.)  */
7003       if (cp_parser_error_occurred (parser))
7004         goto done;
7005       /* Handle function definitions specially.  */
7006       if (function_definition_p)
7007         {
7008           /* If the next token is a `,', then we are probably
7009              processing something like:
7010
7011                void f() {}, *p;
7012
7013              which is erroneous.  */
7014           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7015             error ("mixing declarations and function-definitions is forbidden");
7016           /* Otherwise, we're done with the list of declarators.  */
7017           else
7018             {
7019               pop_deferring_access_checks ();
7020               return;
7021             }
7022         }
7023       /* The next token should be either a `,' or a `;'.  */
7024       token = cp_lexer_peek_token (parser->lexer);
7025       /* If it's a `,', there are more declarators to come.  */
7026       if (token->type == CPP_COMMA)
7027         cp_lexer_consume_token (parser->lexer);
7028       /* If it's a `;', we are done.  */
7029       else if (token->type == CPP_SEMICOLON)
7030         break;
7031       /* Anything else is an error.  */
7032       else
7033         {
7034           cp_parser_error (parser, "expected `,' or `;'");
7035           /* Skip tokens until we reach the end of the statement.  */
7036           cp_parser_skip_to_end_of_statement (parser);
7037           /* If the next token is now a `;', consume it.  */
7038           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7039             cp_lexer_consume_token (parser->lexer);
7040           goto done;
7041         }
7042       /* After the first time around, a function-definition is not
7043          allowed -- even if it was OK at first.  For example:
7044
7045            int i, f() {}
7046
7047          is not valid.  */
7048       function_definition_allowed_p = false;
7049     }
7050
7051   /* Issue an error message if no declarators are present, and the
7052      decl-specifier-seq does not itself declare a class or
7053      enumeration.  */
7054   if (!saw_declarator)
7055     {
7056       if (cp_parser_declares_only_class_p (parser))
7057         shadow_tag (&decl_specifiers);
7058       /* Perform any deferred access checks.  */
7059       perform_deferred_access_checks ();
7060     }
7061
7062   /* Consume the `;'.  */
7063   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7064
7065  done:
7066   pop_deferring_access_checks ();
7067 }
7068
7069 /* Parse a decl-specifier-seq.
7070
7071    decl-specifier-seq:
7072      decl-specifier-seq [opt] decl-specifier
7073
7074    decl-specifier:
7075      storage-class-specifier
7076      type-specifier
7077      function-specifier
7078      friend
7079      typedef
7080
7081    GNU Extension:
7082
7083    decl-specifier:
7084      attributes
7085
7086    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7087
7088    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7089    appears, and the entity that will be a friend is not going to be a
7090    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
7091    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
7092    friendship is granted might not be a class.
7093
7094    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7095    flags:
7096
7097      1: one of the decl-specifiers is an elaborated-type-specifier
7098         (i.e., a type declaration)
7099      2: one of the decl-specifiers is an enum-specifier or a
7100         class-specifier (i.e., a type definition)
7101    
7102    */
7103
7104 static void
7105 cp_parser_decl_specifier_seq (cp_parser* parser,
7106                               cp_parser_flags flags,
7107                               cp_decl_specifier_seq *decl_specs,
7108                               int* declares_class_or_enum)
7109 {
7110   bool constructor_possible_p = !parser->in_declarator_p;
7111
7112   /* Clear DECL_SPECS.  */
7113   clear_decl_specs (decl_specs);
7114
7115   /* Assume no class or enumeration type is declared.  */
7116   *declares_class_or_enum = 0;
7117
7118   /* Keep reading specifiers until there are no more to read.  */
7119   while (true)
7120     {
7121       bool constructor_p;
7122       bool found_decl_spec;
7123       cp_token *token;
7124
7125       /* Peek at the next token.  */
7126       token = cp_lexer_peek_token (parser->lexer);
7127       /* Handle attributes.  */
7128       if (token->keyword == RID_ATTRIBUTE)
7129         {
7130           /* Parse the attributes.  */
7131           decl_specs->attributes 
7132             = chainon (decl_specs->attributes,
7133                        cp_parser_attributes_opt (parser));
7134           continue;
7135         }
7136       /* Assume we will find a decl-specifier keyword.  */
7137       found_decl_spec = true;
7138       /* If the next token is an appropriate keyword, we can simply
7139          add it to the list.  */
7140       switch (token->keyword)
7141         {
7142           /* decl-specifier:
7143                friend  */
7144         case RID_FRIEND:
7145           if (decl_specs->specs[(int) ds_friend]++)
7146             error ("duplicate `friend'");
7147           /* Consume the token.  */
7148           cp_lexer_consume_token (parser->lexer);
7149           break;
7150
7151           /* function-specifier:
7152                inline
7153                virtual
7154                explicit  */
7155         case RID_INLINE:
7156         case RID_VIRTUAL:
7157         case RID_EXPLICIT:
7158           cp_parser_function_specifier_opt (parser, decl_specs);
7159           break;
7160
7161           /* decl-specifier:
7162                typedef  */
7163         case RID_TYPEDEF:
7164           ++decl_specs->specs[(int) ds_typedef];
7165           /* Consume the token.  */
7166           cp_lexer_consume_token (parser->lexer);
7167           /* A constructor declarator cannot appear in a typedef.  */
7168           constructor_possible_p = false;
7169           /* The "typedef" keyword can only occur in a declaration; we
7170              may as well commit at this point.  */
7171           cp_parser_commit_to_tentative_parse (parser);
7172           break;
7173
7174           /* storage-class-specifier:
7175                auto
7176                register
7177                static
7178                extern
7179                mutable
7180
7181              GNU Extension:
7182                thread  */
7183         case RID_AUTO:
7184           /* Consume the token.  */
7185           cp_lexer_consume_token (parser->lexer);
7186           cp_parser_set_storage_class (decl_specs, sc_auto);
7187           break;
7188         case RID_REGISTER:
7189           /* Consume the token.  */
7190           cp_lexer_consume_token (parser->lexer);
7191           cp_parser_set_storage_class (decl_specs, sc_register);
7192           break;
7193         case RID_STATIC:
7194           /* Consume the token.  */
7195           cp_lexer_consume_token (parser->lexer);
7196           if (decl_specs->specs[(int) ds_thread])
7197             {
7198               error ("`__thread' before `static'");
7199               decl_specs->specs[(int) ds_thread] = 0;
7200             }
7201           cp_parser_set_storage_class (decl_specs, sc_static);
7202           break;
7203         case RID_EXTERN:
7204           /* Consume the token.  */
7205           cp_lexer_consume_token (parser->lexer);
7206           if (decl_specs->specs[(int) ds_thread])
7207             {
7208               error ("`__thread' before `extern'");
7209               decl_specs->specs[(int) ds_thread] = 0;
7210             }
7211           cp_parser_set_storage_class (decl_specs, sc_extern);
7212           break;
7213         case RID_MUTABLE:
7214           /* Consume the token.  */
7215           cp_lexer_consume_token (parser->lexer);
7216           cp_parser_set_storage_class (decl_specs, sc_mutable);
7217           break;
7218         case RID_THREAD:
7219           /* Consume the token.  */
7220           cp_lexer_consume_token (parser->lexer);
7221           ++decl_specs->specs[(int) ds_thread];
7222           break;
7223
7224         default:
7225           /* We did not yet find a decl-specifier yet.  */
7226           found_decl_spec = false;
7227           break;
7228         }
7229
7230       /* Constructors are a special case.  The `S' in `S()' is not a
7231          decl-specifier; it is the beginning of the declarator.  */
7232       constructor_p 
7233         = (!found_decl_spec
7234            && constructor_possible_p
7235            && (cp_parser_constructor_declarator_p 
7236                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7237
7238       /* If we don't have a DECL_SPEC yet, then we must be looking at
7239          a type-specifier.  */
7240       if (!found_decl_spec && !constructor_p)
7241         {
7242           int decl_spec_declares_class_or_enum;
7243           bool is_cv_qualifier;
7244           tree type_spec;
7245
7246           type_spec
7247             = cp_parser_type_specifier (parser, flags,
7248                                         decl_specs,
7249                                         /*is_declaration=*/true,
7250                                         &decl_spec_declares_class_or_enum,
7251                                         &is_cv_qualifier);
7252
7253           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7254
7255           /* If this type-specifier referenced a user-defined type
7256              (a typedef, class-name, etc.), then we can't allow any
7257              more such type-specifiers henceforth.
7258
7259              [dcl.spec]
7260
7261              The longest sequence of decl-specifiers that could
7262              possibly be a type name is taken as the
7263              decl-specifier-seq of a declaration.  The sequence shall
7264              be self-consistent as described below.
7265
7266              [dcl.type]
7267
7268              As a general rule, at most one type-specifier is allowed
7269              in the complete decl-specifier-seq of a declaration.  The
7270              only exceptions are the following:
7271
7272              -- const or volatile can be combined with any other
7273                 type-specifier.
7274
7275              -- signed or unsigned can be combined with char, long,
7276                 short, or int.
7277
7278              -- ..
7279
7280              Example:
7281
7282                typedef char* Pc;
7283                void g (const int Pc);
7284
7285              Here, Pc is *not* part of the decl-specifier seq; it's
7286              the declarator.  Therefore, once we see a type-specifier
7287              (other than a cv-qualifier), we forbid any additional
7288              user-defined types.  We *do* still allow things like `int
7289              int' to be considered a decl-specifier-seq, and issue the
7290              error message later.  */
7291           if (type_spec && !is_cv_qualifier)
7292             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7293           /* A constructor declarator cannot follow a type-specifier.  */
7294           if (type_spec)
7295             {
7296               constructor_possible_p = false;
7297               found_decl_spec = true;
7298             }
7299         }
7300
7301       /* If we still do not have a DECL_SPEC, then there are no more
7302          decl-specifiers.  */
7303       if (!found_decl_spec)
7304         break;
7305
7306       decl_specs->any_specifiers_p = true;
7307       /* After we see one decl-specifier, further decl-specifiers are
7308          always optional.  */
7309       flags |= CP_PARSER_FLAGS_OPTIONAL;
7310     }
7311
7312   /* Don't allow a friend specifier with a class definition.  */
7313   if (decl_specs->specs[(int) ds_friend] != 0
7314       && (*declares_class_or_enum & 2))
7315     error ("class definition may not be declared a friend");
7316 }
7317
7318 /* Parse an (optional) storage-class-specifier.
7319
7320    storage-class-specifier:
7321      auto
7322      register
7323      static
7324      extern
7325      mutable
7326
7327    GNU Extension:
7328
7329    storage-class-specifier:
7330      thread
7331
7332    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7333
7334 static tree
7335 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7336 {
7337   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7338     {
7339     case RID_AUTO:
7340     case RID_REGISTER:
7341     case RID_STATIC:
7342     case RID_EXTERN:
7343     case RID_MUTABLE:
7344     case RID_THREAD:
7345       /* Consume the token.  */
7346       return cp_lexer_consume_token (parser->lexer)->value;
7347
7348     default:
7349       return NULL_TREE;
7350     }
7351 }
7352
7353 /* Parse an (optional) function-specifier.
7354
7355    function-specifier:
7356      inline
7357      virtual
7358      explicit
7359
7360    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7361    Updates DECL_SPECS, if it is non-NULL.  */
7362
7363 static tree
7364 cp_parser_function_specifier_opt (cp_parser* parser,
7365                                   cp_decl_specifier_seq *decl_specs)
7366 {
7367   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7368     {
7369     case RID_INLINE:
7370       if (decl_specs)
7371         ++decl_specs->specs[(int) ds_inline];
7372       break;
7373
7374     case RID_VIRTUAL:
7375       if (decl_specs)
7376         ++decl_specs->specs[(int) ds_virtual];
7377       break;
7378
7379     case RID_EXPLICIT:
7380       if (decl_specs)
7381         ++decl_specs->specs[(int) ds_explicit];
7382       break;
7383
7384     default:
7385       return NULL_TREE;
7386     }
7387
7388   /* Consume the token.  */
7389   return cp_lexer_consume_token (parser->lexer)->value;
7390 }
7391
7392 /* Parse a linkage-specification.
7393
7394    linkage-specification:
7395      extern string-literal { declaration-seq [opt] }
7396      extern string-literal declaration  */
7397
7398 static void
7399 cp_parser_linkage_specification (cp_parser* parser)
7400 {
7401   cp_token *token;
7402   tree linkage;
7403
7404   /* Look for the `extern' keyword.  */
7405   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7406
7407   /* Peek at the next token.  */
7408   token = cp_lexer_peek_token (parser->lexer);
7409   /* If it's not a string-literal, then there's a problem.  */
7410   if (!cp_parser_is_string_literal (token))
7411     {
7412       cp_parser_error (parser, "expected language-name");
7413       return;
7414     }
7415   /* Consume the token.  */
7416   cp_lexer_consume_token (parser->lexer);
7417
7418   /* Transform the literal into an identifier.  If the literal is a
7419      wide-character string, or contains embedded NULs, then we can't
7420      handle it as the user wants.  */
7421   if (token->type == CPP_WSTRING
7422       || (strlen (TREE_STRING_POINTER (token->value))
7423           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7424     {
7425       cp_parser_error (parser, "invalid linkage-specification");
7426       /* Assume C++ linkage.  */
7427       linkage = get_identifier ("c++");
7428     }
7429   /* If the string is chained to another string, take the latter,
7430      that's the untranslated string.  */
7431   else if (TREE_CHAIN (token->value))
7432     linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7433   /* If it's a simple string constant, things are easier.  */
7434   else
7435     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7436
7437   /* We're now using the new linkage.  */
7438   push_lang_context (linkage);
7439
7440   /* If the next token is a `{', then we're using the first
7441      production.  */
7442   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7443     {
7444       /* Consume the `{' token.  */
7445       cp_lexer_consume_token (parser->lexer);
7446       /* Parse the declarations.  */
7447       cp_parser_declaration_seq_opt (parser);
7448       /* Look for the closing `}'.  */
7449       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7450     }
7451   /* Otherwise, there's just one declaration.  */
7452   else
7453     {
7454       bool saved_in_unbraced_linkage_specification_p;
7455
7456       saved_in_unbraced_linkage_specification_p
7457         = parser->in_unbraced_linkage_specification_p;
7458       parser->in_unbraced_linkage_specification_p = true;
7459       have_extern_spec = true;
7460       cp_parser_declaration (parser);
7461       have_extern_spec = false;
7462       parser->in_unbraced_linkage_specification_p
7463         = saved_in_unbraced_linkage_specification_p;
7464     }
7465
7466   /* We're done with the linkage-specification.  */
7467   pop_lang_context ();
7468 }
7469
7470 /* Special member functions [gram.special] */
7471
7472 /* Parse a conversion-function-id.
7473
7474    conversion-function-id:
7475      operator conversion-type-id
7476
7477    Returns an IDENTIFIER_NODE representing the operator.  */
7478
7479 static tree
7480 cp_parser_conversion_function_id (cp_parser* parser)
7481 {
7482   tree type;
7483   tree saved_scope;
7484   tree saved_qualifying_scope;
7485   tree saved_object_scope;
7486   bool pop_p = false;
7487
7488   /* Look for the `operator' token.  */
7489   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7490     return error_mark_node;
7491   /* When we parse the conversion-type-id, the current scope will be
7492      reset.  However, we need that information in able to look up the
7493      conversion function later, so we save it here.  */
7494   saved_scope = parser->scope;
7495   saved_qualifying_scope = parser->qualifying_scope;
7496   saved_object_scope = parser->object_scope;
7497   /* We must enter the scope of the class so that the names of
7498      entities declared within the class are available in the
7499      conversion-type-id.  For example, consider:
7500
7501        struct S {
7502          typedef int I;
7503          operator I();
7504        };
7505
7506        S::operator I() { ... }
7507
7508      In order to see that `I' is a type-name in the definition, we
7509      must be in the scope of `S'.  */
7510   if (saved_scope)
7511     pop_p = push_scope (saved_scope);
7512   /* Parse the conversion-type-id.  */
7513   type = cp_parser_conversion_type_id (parser);
7514   /* Leave the scope of the class, if any.  */
7515   if (pop_p)
7516     pop_scope (saved_scope);
7517   /* Restore the saved scope.  */
7518   parser->scope = saved_scope;
7519   parser->qualifying_scope = saved_qualifying_scope;
7520   parser->object_scope = saved_object_scope;
7521   /* If the TYPE is invalid, indicate failure.  */
7522   if (type == error_mark_node)
7523     return error_mark_node;
7524   return mangle_conv_op_name_for_type (type);
7525 }
7526
7527 /* Parse a conversion-type-id:
7528
7529    conversion-type-id:
7530      type-specifier-seq conversion-declarator [opt]
7531
7532    Returns the TYPE specified.  */
7533
7534 static tree
7535 cp_parser_conversion_type_id (cp_parser* parser)
7536 {
7537   tree attributes;
7538   cp_decl_specifier_seq type_specifiers;
7539   cp_declarator *declarator;
7540
7541   /* Parse the attributes.  */
7542   attributes = cp_parser_attributes_opt (parser);
7543   /* Parse the type-specifiers.  */
7544   cp_parser_type_specifier_seq (parser, &type_specifiers);
7545   /* If that didn't work, stop.  */
7546   if (type_specifiers.type == error_mark_node)
7547     return error_mark_node;
7548   /* Parse the conversion-declarator.  */
7549   declarator = cp_parser_conversion_declarator_opt (parser);
7550
7551   return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7552                          /*initialized=*/0, &attributes);
7553 }
7554
7555 /* Parse an (optional) conversion-declarator.
7556
7557    conversion-declarator:
7558      ptr-operator conversion-declarator [opt]
7559
7560    */
7561
7562 static cp_declarator *
7563 cp_parser_conversion_declarator_opt (cp_parser* parser)
7564 {
7565   enum tree_code code;
7566   tree class_type;
7567   cp_cv_quals cv_quals;
7568
7569   /* We don't know if there's a ptr-operator next, or not.  */
7570   cp_parser_parse_tentatively (parser);
7571   /* Try the ptr-operator.  */
7572   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7573   /* If it worked, look for more conversion-declarators.  */
7574   if (cp_parser_parse_definitely (parser))
7575     {
7576       cp_declarator *declarator;
7577       
7578       /* Parse another optional declarator.  */
7579       declarator = cp_parser_conversion_declarator_opt (parser);
7580       
7581       /* Create the representation of the declarator.  */
7582       if (class_type)
7583         declarator = make_ptrmem_declarator (cv_quals, class_type,
7584                                              declarator);
7585       else if (code == INDIRECT_REF)
7586         declarator = make_pointer_declarator (cv_quals, declarator);
7587       else
7588         declarator = make_reference_declarator (cv_quals, declarator);
7589       
7590       return declarator;
7591    }
7592
7593   return NULL;
7594 }
7595
7596 /* Parse an (optional) ctor-initializer.
7597
7598    ctor-initializer:
7599      : mem-initializer-list
7600
7601    Returns TRUE iff the ctor-initializer was actually present.  */
7602
7603 static bool
7604 cp_parser_ctor_initializer_opt (cp_parser* parser)
7605 {
7606   /* If the next token is not a `:', then there is no
7607      ctor-initializer.  */
7608   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7609     {
7610       /* Do default initialization of any bases and members.  */
7611       if (DECL_CONSTRUCTOR_P (current_function_decl))
7612         finish_mem_initializers (NULL_TREE);
7613
7614       return false;
7615     }
7616
7617   /* Consume the `:' token.  */
7618   cp_lexer_consume_token (parser->lexer);
7619   /* And the mem-initializer-list.  */
7620   cp_parser_mem_initializer_list (parser);
7621
7622   return true;
7623 }
7624
7625 /* Parse a mem-initializer-list.
7626
7627    mem-initializer-list:
7628      mem-initializer
7629      mem-initializer , mem-initializer-list  */
7630
7631 static void
7632 cp_parser_mem_initializer_list (cp_parser* parser)
7633 {
7634   tree mem_initializer_list = NULL_TREE;
7635
7636   /* Let the semantic analysis code know that we are starting the
7637      mem-initializer-list.  */
7638   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7639     error ("only constructors take base initializers");
7640
7641   /* Loop through the list.  */
7642   while (true)
7643     {
7644       tree mem_initializer;
7645
7646       /* Parse the mem-initializer.  */
7647       mem_initializer = cp_parser_mem_initializer (parser);
7648       /* Add it to the list, unless it was erroneous.  */
7649       if (mem_initializer)
7650         {
7651           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7652           mem_initializer_list = mem_initializer;
7653         }
7654       /* If the next token is not a `,', we're done.  */
7655       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7656         break;
7657       /* Consume the `,' token.  */
7658       cp_lexer_consume_token (parser->lexer);
7659     }
7660
7661   /* Perform semantic analysis.  */
7662   if (DECL_CONSTRUCTOR_P (current_function_decl))
7663     finish_mem_initializers (mem_initializer_list);
7664 }
7665
7666 /* Parse a mem-initializer.
7667
7668    mem-initializer:
7669      mem-initializer-id ( expression-list [opt] )
7670
7671    GNU extension:
7672
7673    mem-initializer:
7674      ( expression-list [opt] )
7675
7676    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7677    class) or FIELD_DECL (for a non-static data member) to initialize;
7678    the TREE_VALUE is the expression-list.  */
7679
7680 static tree
7681 cp_parser_mem_initializer (cp_parser* parser)
7682 {
7683   tree mem_initializer_id;
7684   tree expression_list;
7685   tree member;
7686
7687   /* Find out what is being initialized.  */
7688   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7689     {
7690       pedwarn ("anachronistic old-style base class initializer");
7691       mem_initializer_id = NULL_TREE;
7692     }
7693   else
7694     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7695   member = expand_member_init (mem_initializer_id);
7696   if (member && !DECL_P (member))
7697     in_base_initializer = 1;
7698
7699   expression_list
7700     = cp_parser_parenthesized_expression_list (parser, false,
7701                                                /*non_constant_p=*/NULL);
7702   if (!expression_list)
7703     expression_list = void_type_node;
7704
7705   in_base_initializer = 0;
7706
7707   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7708 }
7709
7710 /* Parse a mem-initializer-id.
7711
7712    mem-initializer-id:
7713      :: [opt] nested-name-specifier [opt] class-name
7714      identifier
7715
7716    Returns a TYPE indicating the class to be initializer for the first
7717    production.  Returns an IDENTIFIER_NODE indicating the data member
7718    to be initialized for the second production.  */
7719
7720 static tree
7721 cp_parser_mem_initializer_id (cp_parser* parser)
7722 {
7723   bool global_scope_p;
7724   bool nested_name_specifier_p;
7725   bool template_p = false;
7726   tree id;
7727
7728   /* `typename' is not allowed in this context ([temp.res]).  */
7729   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7730     {
7731       error ("keyword `typename' not allowed in this context (a qualified "
7732              "member initializer is implicitly a type)");
7733       cp_lexer_consume_token (parser->lexer);
7734     }
7735   /* Look for the optional `::' operator.  */
7736   global_scope_p
7737     = (cp_parser_global_scope_opt (parser,
7738                                    /*current_scope_valid_p=*/false)
7739        != NULL_TREE);
7740   /* Look for the optional nested-name-specifier.  The simplest way to
7741      implement:
7742
7743        [temp.res]
7744
7745        The keyword `typename' is not permitted in a base-specifier or
7746        mem-initializer; in these contexts a qualified name that
7747        depends on a template-parameter is implicitly assumed to be a
7748        type name.
7749
7750      is to assume that we have seen the `typename' keyword at this
7751      point.  */
7752   nested_name_specifier_p
7753     = (cp_parser_nested_name_specifier_opt (parser,
7754                                             /*typename_keyword_p=*/true,
7755                                             /*check_dependency_p=*/true,
7756                                             /*type_p=*/true,
7757                                             /*is_declaration=*/true)
7758        != NULL_TREE);
7759   if (nested_name_specifier_p)
7760     template_p = cp_parser_optional_template_keyword (parser);
7761   /* If there is a `::' operator or a nested-name-specifier, then we
7762      are definitely looking for a class-name.  */
7763   if (global_scope_p || nested_name_specifier_p)
7764     return cp_parser_class_name (parser,
7765                                  /*typename_keyword_p=*/true,
7766                                  /*template_keyword_p=*/template_p,
7767                                  /*type_p=*/false,
7768                                  /*check_dependency_p=*/true,
7769                                  /*class_head_p=*/false,
7770                                  /*is_declaration=*/true);
7771   /* Otherwise, we could also be looking for an ordinary identifier.  */
7772   cp_parser_parse_tentatively (parser);
7773   /* Try a class-name.  */
7774   id = cp_parser_class_name (parser,
7775                              /*typename_keyword_p=*/true,
7776                              /*template_keyword_p=*/false,
7777                              /*type_p=*/false,
7778                              /*check_dependency_p=*/true,
7779                              /*class_head_p=*/false,
7780                              /*is_declaration=*/true);
7781   /* If we found one, we're done.  */
7782   if (cp_parser_parse_definitely (parser))
7783     return id;
7784   /* Otherwise, look for an ordinary identifier.  */
7785   return cp_parser_identifier (parser);
7786 }
7787
7788 /* Overloading [gram.over] */
7789
7790 /* Parse an operator-function-id.
7791
7792    operator-function-id:
7793      operator operator
7794
7795    Returns an IDENTIFIER_NODE for the operator which is a
7796    human-readable spelling of the identifier, e.g., `operator +'.  */
7797
7798 static tree
7799 cp_parser_operator_function_id (cp_parser* parser)
7800 {
7801   /* Look for the `operator' keyword.  */
7802   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7803     return error_mark_node;
7804   /* And then the name of the operator itself.  */
7805   return cp_parser_operator (parser);
7806 }
7807
7808 /* Parse an operator.
7809
7810    operator:
7811      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7812      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7813      || ++ -- , ->* -> () []
7814
7815    GNU Extensions:
7816
7817    operator:
7818      <? >? <?= >?=
7819
7820    Returns an IDENTIFIER_NODE for the operator which is a
7821    human-readable spelling of the identifier, e.g., `operator +'.  */
7822
7823 static tree
7824 cp_parser_operator (cp_parser* parser)
7825 {
7826   tree id = NULL_TREE;
7827   cp_token *token;
7828
7829   /* Peek at the next token.  */
7830   token = cp_lexer_peek_token (parser->lexer);
7831   /* Figure out which operator we have.  */
7832   switch (token->type)
7833     {
7834     case CPP_KEYWORD:
7835       {
7836         enum tree_code op;
7837
7838         /* The keyword should be either `new' or `delete'.  */
7839         if (token->keyword == RID_NEW)
7840           op = NEW_EXPR;
7841         else if (token->keyword == RID_DELETE)
7842           op = DELETE_EXPR;
7843         else
7844           break;
7845
7846         /* Consume the `new' or `delete' token.  */
7847         cp_lexer_consume_token (parser->lexer);
7848
7849         /* Peek at the next token.  */
7850         token = cp_lexer_peek_token (parser->lexer);
7851         /* If it's a `[' token then this is the array variant of the
7852            operator.  */
7853         if (token->type == CPP_OPEN_SQUARE)
7854           {
7855             /* Consume the `[' token.  */
7856             cp_lexer_consume_token (parser->lexer);
7857             /* Look for the `]' token.  */
7858             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7859             id = ansi_opname (op == NEW_EXPR
7860                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7861           }
7862         /* Otherwise, we have the non-array variant.  */
7863         else
7864           id = ansi_opname (op);
7865
7866         return id;
7867       }
7868
7869     case CPP_PLUS:
7870       id = ansi_opname (PLUS_EXPR);
7871       break;
7872
7873     case CPP_MINUS:
7874       id = ansi_opname (MINUS_EXPR);
7875       break;
7876
7877     case CPP_MULT:
7878       id = ansi_opname (MULT_EXPR);
7879       break;
7880
7881     case CPP_DIV:
7882       id = ansi_opname (TRUNC_DIV_EXPR);
7883       break;
7884
7885     case CPP_MOD:
7886       id = ansi_opname (TRUNC_MOD_EXPR);
7887       break;
7888
7889     case CPP_XOR:
7890       id = ansi_opname (BIT_XOR_EXPR);
7891       break;
7892
7893     case CPP_AND:
7894       id = ansi_opname (BIT_AND_EXPR);
7895       break;
7896
7897     case CPP_OR:
7898       id = ansi_opname (BIT_IOR_EXPR);
7899       break;
7900
7901     case CPP_COMPL:
7902       id = ansi_opname (BIT_NOT_EXPR);
7903       break;
7904
7905     case CPP_NOT:
7906       id = ansi_opname (TRUTH_NOT_EXPR);
7907       break;
7908
7909     case CPP_EQ:
7910       id = ansi_assopname (NOP_EXPR);
7911       break;
7912
7913     case CPP_LESS:
7914       id = ansi_opname (LT_EXPR);
7915       break;
7916
7917     case CPP_GREATER:
7918       id = ansi_opname (GT_EXPR);
7919       break;
7920
7921     case CPP_PLUS_EQ:
7922       id = ansi_assopname (PLUS_EXPR);
7923       break;
7924
7925     case CPP_MINUS_EQ:
7926       id = ansi_assopname (MINUS_EXPR);
7927       break;
7928
7929     case CPP_MULT_EQ:
7930       id = ansi_assopname (MULT_EXPR);
7931       break;
7932
7933     case CPP_DIV_EQ:
7934       id = ansi_assopname (TRUNC_DIV_EXPR);
7935       break;
7936
7937     case CPP_MOD_EQ:
7938       id = ansi_assopname (TRUNC_MOD_EXPR);
7939       break;
7940
7941     case CPP_XOR_EQ:
7942       id = ansi_assopname (BIT_XOR_EXPR);
7943       break;
7944
7945     case CPP_AND_EQ:
7946       id = ansi_assopname (BIT_AND_EXPR);
7947       break;
7948
7949     case CPP_OR_EQ:
7950       id = ansi_assopname (BIT_IOR_EXPR);
7951       break;
7952
7953     case CPP_LSHIFT:
7954       id = ansi_opname (LSHIFT_EXPR);
7955       break;
7956
7957     case CPP_RSHIFT:
7958       id = ansi_opname (RSHIFT_EXPR);
7959       break;
7960
7961     case CPP_LSHIFT_EQ:
7962       id = ansi_assopname (LSHIFT_EXPR);
7963       break;
7964
7965     case CPP_RSHIFT_EQ:
7966       id = ansi_assopname (RSHIFT_EXPR);
7967       break;
7968
7969     case CPP_EQ_EQ:
7970       id = ansi_opname (EQ_EXPR);
7971       break;
7972
7973     case CPP_NOT_EQ:
7974       id = ansi_opname (NE_EXPR);
7975       break;
7976
7977     case CPP_LESS_EQ:
7978       id = ansi_opname (LE_EXPR);
7979       break;
7980
7981     case CPP_GREATER_EQ:
7982       id = ansi_opname (GE_EXPR);
7983       break;
7984
7985     case CPP_AND_AND:
7986       id = ansi_opname (TRUTH_ANDIF_EXPR);
7987       break;
7988
7989     case CPP_OR_OR:
7990       id = ansi_opname (TRUTH_ORIF_EXPR);
7991       break;
7992
7993     case CPP_PLUS_PLUS:
7994       id = ansi_opname (POSTINCREMENT_EXPR);
7995       break;
7996
7997     case CPP_MINUS_MINUS:
7998       id = ansi_opname (PREDECREMENT_EXPR);
7999       break;
8000
8001     case CPP_COMMA:
8002       id = ansi_opname (COMPOUND_EXPR);
8003       break;
8004
8005     case CPP_DEREF_STAR:
8006       id = ansi_opname (MEMBER_REF);
8007       break;
8008
8009     case CPP_DEREF:
8010       id = ansi_opname (COMPONENT_REF);
8011       break;
8012
8013     case CPP_OPEN_PAREN:
8014       /* Consume the `('.  */
8015       cp_lexer_consume_token (parser->lexer);
8016       /* Look for the matching `)'.  */
8017       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8018       return ansi_opname (CALL_EXPR);
8019
8020     case CPP_OPEN_SQUARE:
8021       /* Consume the `['.  */
8022       cp_lexer_consume_token (parser->lexer);
8023       /* Look for the matching `]'.  */
8024       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8025       return ansi_opname (ARRAY_REF);
8026
8027       /* Extensions.  */
8028     case CPP_MIN:
8029       id = ansi_opname (MIN_EXPR);
8030       break;
8031
8032     case CPP_MAX:
8033       id = ansi_opname (MAX_EXPR);
8034       break;
8035
8036     case CPP_MIN_EQ:
8037       id = ansi_assopname (MIN_EXPR);
8038       break;
8039
8040     case CPP_MAX_EQ:
8041       id = ansi_assopname (MAX_EXPR);
8042       break;
8043
8044     default:
8045       /* Anything else is an error.  */
8046       break;
8047     }
8048
8049   /* If we have selected an identifier, we need to consume the
8050      operator token.  */
8051   if (id)
8052     cp_lexer_consume_token (parser->lexer);
8053   /* Otherwise, no valid operator name was present.  */
8054   else
8055     {
8056       cp_parser_error (parser, "expected operator");
8057       id = error_mark_node;
8058     }
8059
8060   return id;
8061 }
8062
8063 /* Parse a template-declaration.
8064
8065    template-declaration:
8066      export [opt] template < template-parameter-list > declaration
8067
8068    If MEMBER_P is TRUE, this template-declaration occurs within a
8069    class-specifier.
8070
8071    The grammar rule given by the standard isn't correct.  What
8072    is really meant is:
8073
8074    template-declaration:
8075      export [opt] template-parameter-list-seq
8076        decl-specifier-seq [opt] init-declarator [opt] ;
8077      export [opt] template-parameter-list-seq
8078        function-definition
8079
8080    template-parameter-list-seq:
8081      template-parameter-list-seq [opt]
8082      template < template-parameter-list >  */
8083
8084 static void
8085 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8086 {
8087   /* Check for `export'.  */
8088   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8089     {
8090       /* Consume the `export' token.  */
8091       cp_lexer_consume_token (parser->lexer);
8092       /* Warn that we do not support `export'.  */
8093       warning ("keyword `export' not implemented, and will be ignored");
8094     }
8095
8096   cp_parser_template_declaration_after_export (parser, member_p);
8097 }
8098
8099 /* Parse a template-parameter-list.
8100
8101    template-parameter-list:
8102      template-parameter
8103      template-parameter-list , template-parameter
8104
8105    Returns a TREE_LIST.  Each node represents a template parameter.
8106    The nodes are connected via their TREE_CHAINs.  */
8107
8108 static tree
8109 cp_parser_template_parameter_list (cp_parser* parser)
8110 {
8111   tree parameter_list = NULL_TREE;
8112
8113   while (true)
8114     {
8115       tree parameter;
8116       cp_token *token;
8117       bool is_non_type;
8118
8119       /* Parse the template-parameter.  */
8120       parameter = cp_parser_template_parameter (parser, &is_non_type);
8121       /* Add it to the list.  */
8122       parameter_list = process_template_parm (parameter_list,
8123                                               parameter,
8124                                               is_non_type);
8125       /* Peek at the next token.  */
8126       token = cp_lexer_peek_token (parser->lexer);
8127       /* If it's not a `,', we're done.  */
8128       if (token->type != CPP_COMMA)
8129         break;
8130       /* Otherwise, consume the `,' token.  */
8131       cp_lexer_consume_token (parser->lexer);
8132     }
8133
8134   return parameter_list;
8135 }
8136
8137 /* Parse a template-parameter.
8138
8139    template-parameter:
8140      type-parameter
8141      parameter-declaration
8142
8143    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8144    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8145    true iff this parameter is a non-type parameter.  */
8146
8147 static tree
8148 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8149 {
8150   cp_token *token;
8151   cp_parameter_declarator *parameter_declarator;
8152
8153   /* Assume it is a type parameter or a template parameter.  */
8154   *is_non_type = false;
8155   /* Peek at the next token.  */
8156   token = cp_lexer_peek_token (parser->lexer);
8157   /* If it is `class' or `template', we have a type-parameter.  */
8158   if (token->keyword == RID_TEMPLATE)
8159     return cp_parser_type_parameter (parser);
8160   /* If it is `class' or `typename' we do not know yet whether it is a
8161      type parameter or a non-type parameter.  Consider:
8162
8163        template <typename T, typename T::X X> ...
8164
8165      or:
8166
8167        template <class C, class D*> ...
8168
8169      Here, the first parameter is a type parameter, and the second is
8170      a non-type parameter.  We can tell by looking at the token after
8171      the identifier -- if it is a `,', `=', or `>' then we have a type
8172      parameter.  */
8173   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8174     {
8175       /* Peek at the token after `class' or `typename'.  */
8176       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8177       /* If it's an identifier, skip it.  */
8178       if (token->type == CPP_NAME)
8179         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8180       /* Now, see if the token looks like the end of a template
8181          parameter.  */
8182       if (token->type == CPP_COMMA
8183           || token->type == CPP_EQ
8184           || token->type == CPP_GREATER)
8185         return cp_parser_type_parameter (parser);
8186     }
8187
8188   /* Otherwise, it is a non-type parameter.
8189
8190      [temp.param]
8191
8192      When parsing a default template-argument for a non-type
8193      template-parameter, the first non-nested `>' is taken as the end
8194      of the template parameter-list rather than a greater-than
8195      operator.  */
8196   *is_non_type = true;
8197   parameter_declarator
8198      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8199                                         /*parenthesized_p=*/NULL);
8200   return (build_tree_list 
8201           (parameter_declarator->default_argument,
8202            grokdeclarator (parameter_declarator->declarator,
8203                            &parameter_declarator->decl_specifiers,
8204                            PARM, /*initialized=*/0, 
8205                            /*attrlist=*/NULL)));
8206 }
8207
8208 /* Parse a type-parameter.
8209
8210    type-parameter:
8211      class identifier [opt]
8212      class identifier [opt] = type-id
8213      typename identifier [opt]
8214      typename identifier [opt] = type-id
8215      template < template-parameter-list > class identifier [opt]
8216      template < template-parameter-list > class identifier [opt]
8217        = id-expression
8218
8219    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8220    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8221    the declaration of the parameter.  */
8222
8223 static tree
8224 cp_parser_type_parameter (cp_parser* parser)
8225 {
8226   cp_token *token;
8227   tree parameter;
8228
8229   /* Look for a keyword to tell us what kind of parameter this is.  */
8230   token = cp_parser_require (parser, CPP_KEYWORD,
8231                              "`class', `typename', or `template'");
8232   if (!token)
8233     return error_mark_node;
8234
8235   switch (token->keyword)
8236     {
8237     case RID_CLASS:
8238     case RID_TYPENAME:
8239       {
8240         tree identifier;
8241         tree default_argument;
8242
8243         /* If the next token is an identifier, then it names the
8244            parameter.  */
8245         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8246           identifier = cp_parser_identifier (parser);
8247         else
8248           identifier = NULL_TREE;
8249
8250         /* Create the parameter.  */
8251         parameter = finish_template_type_parm (class_type_node, identifier);
8252
8253         /* If the next token is an `=', we have a default argument.  */
8254         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8255           {
8256             /* Consume the `=' token.  */
8257             cp_lexer_consume_token (parser->lexer);
8258             /* Parse the default-argument.  */
8259             default_argument = cp_parser_type_id (parser);
8260           }
8261         else
8262           default_argument = NULL_TREE;
8263
8264         /* Create the combined representation of the parameter and the
8265            default argument.  */
8266         parameter = build_tree_list (default_argument, parameter);
8267       }
8268       break;
8269
8270     case RID_TEMPLATE:
8271       {
8272         tree parameter_list;
8273         tree identifier;
8274         tree default_argument;
8275
8276         /* Look for the `<'.  */
8277         cp_parser_require (parser, CPP_LESS, "`<'");
8278         /* Parse the template-parameter-list.  */
8279         begin_template_parm_list ();
8280         parameter_list
8281           = cp_parser_template_parameter_list (parser);
8282         parameter_list = end_template_parm_list (parameter_list);
8283         /* Look for the `>'.  */
8284         cp_parser_require (parser, CPP_GREATER, "`>'");
8285         /* Look for the `class' keyword.  */
8286         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8287         /* If the next token is an `=', then there is a
8288            default-argument.  If the next token is a `>', we are at
8289            the end of the parameter-list.  If the next token is a `,',
8290            then we are at the end of this parameter.  */
8291         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8292             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8293             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8294           identifier = cp_parser_identifier (parser);
8295         else
8296           identifier = NULL_TREE;
8297         /* Create the template parameter.  */
8298         parameter = finish_template_template_parm (class_type_node,
8299                                                    identifier);
8300
8301         /* If the next token is an `=', then there is a
8302            default-argument.  */
8303         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8304           {
8305             bool is_template;
8306
8307             /* Consume the `='.  */
8308             cp_lexer_consume_token (parser->lexer);
8309             /* Parse the id-expression.  */
8310             default_argument
8311               = cp_parser_id_expression (parser,
8312                                          /*template_keyword_p=*/false,
8313                                          /*check_dependency_p=*/true,
8314                                          /*template_p=*/&is_template,
8315                                          /*declarator_p=*/false);
8316             if (TREE_CODE (default_argument) == TYPE_DECL)
8317               /* If the id-expression was a template-id that refers to
8318                  a template-class, we already have the declaration here,
8319                  so no further lookup is needed.  */
8320                  ;
8321             else
8322               /* Look up the name.  */
8323               default_argument
8324                 = cp_parser_lookup_name (parser, default_argument,
8325                                         /*is_type=*/false,
8326                                         /*is_template=*/is_template,
8327                                         /*is_namespace=*/false,
8328                                         /*check_dependency=*/true);
8329             /* See if the default argument is valid.  */
8330             default_argument
8331               = check_template_template_default_arg (default_argument);
8332           }
8333         else
8334           default_argument = NULL_TREE;
8335
8336         /* Create the combined representation of the parameter and the
8337            default argument.  */
8338         parameter =  build_tree_list (default_argument, parameter);
8339       }
8340       break;
8341
8342     default:
8343       /* Anything else is an error.  */
8344       cp_parser_error (parser,
8345                        "expected `class', `typename', or `template'");
8346       parameter = error_mark_node;
8347     }
8348
8349   return parameter;
8350 }
8351
8352 /* Parse a template-id.
8353
8354    template-id:
8355      template-name < template-argument-list [opt] >
8356
8357    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8358    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8359    returned.  Otherwise, if the template-name names a function, or set
8360    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8361    names a class, returns a TYPE_DECL for the specialization.
8362
8363    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8364    uninstantiated templates.  */
8365
8366 static tree
8367 cp_parser_template_id (cp_parser *parser,
8368                        bool template_keyword_p,
8369                        bool check_dependency_p,
8370                        bool is_declaration)
8371 {
8372   tree template;
8373   tree arguments;
8374   tree template_id;
8375   ptrdiff_t start_of_id;
8376   tree access_check = NULL_TREE;
8377   cp_token *next_token, *next_token_2;
8378   bool is_identifier;
8379
8380   /* If the next token corresponds to a template-id, there is no need
8381      to reparse it.  */
8382   next_token = cp_lexer_peek_token (parser->lexer);
8383   if (next_token->type == CPP_TEMPLATE_ID)
8384     {
8385       tree value;
8386       tree check;
8387
8388       /* Get the stored value.  */
8389       value = cp_lexer_consume_token (parser->lexer)->value;
8390       /* Perform any access checks that were deferred.  */
8391       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8392         perform_or_defer_access_check (TREE_PURPOSE (check),
8393                                        TREE_VALUE (check));
8394       /* Return the stored value.  */
8395       return TREE_VALUE (value);
8396     }
8397
8398   /* Avoid performing name lookup if there is no possibility of
8399      finding a template-id.  */
8400   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8401       || (next_token->type == CPP_NAME
8402           && !cp_parser_nth_token_starts_template_argument_list_p
8403                (parser, 2)))
8404     {
8405       cp_parser_error (parser, "expected template-id");
8406       return error_mark_node;
8407     }
8408
8409   /* Remember where the template-id starts.  */
8410   if (cp_parser_parsing_tentatively (parser)
8411       && !cp_parser_committed_to_tentative_parse (parser))
8412     {
8413       next_token = cp_lexer_peek_token (parser->lexer);
8414       start_of_id = cp_lexer_token_difference (parser->lexer,
8415                                                parser->lexer->first_token,
8416                                                next_token);
8417     }
8418   else
8419     start_of_id = -1;
8420
8421   push_deferring_access_checks (dk_deferred);
8422
8423   /* Parse the template-name.  */
8424   is_identifier = false;
8425   template = cp_parser_template_name (parser, template_keyword_p,
8426                                       check_dependency_p,
8427                                       is_declaration,
8428                                       &is_identifier);
8429   if (template == error_mark_node || is_identifier)
8430     {
8431       pop_deferring_access_checks ();
8432       return template;
8433     }
8434
8435   /* If we find the sequence `[:' after a template-name, it's probably
8436      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8437      parse correctly the argument list.  */
8438   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8439   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8440   if (next_token->type == CPP_OPEN_SQUARE
8441       && next_token->flags & DIGRAPH
8442       && next_token_2->type == CPP_COLON
8443       && !(next_token_2->flags & PREV_WHITE))
8444     {
8445       cp_parser_parse_tentatively (parser);
8446       /* Change `:' into `::'.  */
8447       next_token_2->type = CPP_SCOPE;
8448       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8449          CPP_LESS.  */
8450       cp_lexer_consume_token (parser->lexer);
8451       /* Parse the arguments.  */
8452       arguments = cp_parser_enclosed_template_argument_list (parser);
8453       if (!cp_parser_parse_definitely (parser))
8454         {
8455           /* If we couldn't parse an argument list, then we revert our changes
8456              and return simply an error. Maybe this is not a template-id
8457              after all.  */
8458           next_token_2->type = CPP_COLON;
8459           cp_parser_error (parser, "expected `<'");
8460           pop_deferring_access_checks ();
8461           return error_mark_node;
8462         }
8463       /* Otherwise, emit an error about the invalid digraph, but continue
8464          parsing because we got our argument list.  */
8465       pedwarn ("`<::' cannot begin a template-argument list");
8466       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8467               "between `<' and `::'");
8468       if (!flag_permissive)
8469         {
8470           static bool hint;
8471           if (!hint)
8472             {
8473               inform ("(if you use `-fpermissive' G++ will accept your code)");
8474               hint = true;
8475             }
8476         }
8477     }
8478   else
8479     {
8480       /* Look for the `<' that starts the template-argument-list.  */
8481       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8482         {
8483           pop_deferring_access_checks ();
8484           return error_mark_node;
8485         }
8486       /* Parse the arguments.  */
8487       arguments = cp_parser_enclosed_template_argument_list (parser);
8488     }
8489
8490   /* Build a representation of the specialization.  */
8491   if (TREE_CODE (template) == IDENTIFIER_NODE)
8492     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8493   else if (DECL_CLASS_TEMPLATE_P (template)
8494            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8495     template_id
8496       = finish_template_type (template, arguments,
8497                               cp_lexer_next_token_is (parser->lexer,
8498                                                       CPP_SCOPE));
8499   else
8500     {
8501       /* If it's not a class-template or a template-template, it should be
8502          a function-template.  */
8503       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8504                            || TREE_CODE (template) == OVERLOAD
8505                            || BASELINK_P (template)),
8506                           20010716);
8507
8508       template_id = lookup_template_function (template, arguments);
8509     }
8510
8511   /* Retrieve any deferred checks.  Do not pop this access checks yet
8512      so the memory will not be reclaimed during token replacing below.  */
8513   access_check = get_deferred_access_checks ();
8514
8515   /* If parsing tentatively, replace the sequence of tokens that makes
8516      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8517      should we re-parse the token stream, we will not have to repeat
8518      the effort required to do the parse, nor will we issue duplicate
8519      error messages about problems during instantiation of the
8520      template.  */
8521   if (start_of_id >= 0)
8522     {
8523       cp_token *token;
8524
8525       /* Find the token that corresponds to the start of the
8526          template-id.  */
8527       token = cp_lexer_advance_token (parser->lexer,
8528                                       parser->lexer->first_token,
8529                                       start_of_id);
8530
8531       /* Reset the contents of the START_OF_ID token.  */
8532       token->type = CPP_TEMPLATE_ID;
8533       token->value = build_tree_list (access_check, template_id);
8534       token->keyword = RID_MAX;
8535       /* Purge all subsequent tokens.  */
8536       cp_lexer_purge_tokens_after (parser->lexer, token);
8537     }
8538
8539   pop_deferring_access_checks ();
8540   return template_id;
8541 }
8542
8543 /* Parse a template-name.
8544
8545    template-name:
8546      identifier
8547
8548    The standard should actually say:
8549
8550    template-name:
8551      identifier
8552      operator-function-id
8553
8554    A defect report has been filed about this issue.
8555
8556    A conversion-function-id cannot be a template name because they cannot
8557    be part of a template-id. In fact, looking at this code:
8558
8559    a.operator K<int>()
8560
8561    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8562    It is impossible to call a templated conversion-function-id with an
8563    explicit argument list, since the only allowed template parameter is
8564    the type to which it is converting.
8565
8566    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8567    `template' keyword, in a construction like:
8568
8569      T::template f<3>()
8570
8571    In that case `f' is taken to be a template-name, even though there
8572    is no way of knowing for sure.
8573
8574    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8575    name refers to a set of overloaded functions, at least one of which
8576    is a template, or an IDENTIFIER_NODE with the name of the template,
8577    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8578    names are looked up inside uninstantiated templates.  */
8579
8580 static tree
8581 cp_parser_template_name (cp_parser* parser,
8582                          bool template_keyword_p,
8583                          bool check_dependency_p,
8584                          bool is_declaration,
8585                          bool *is_identifier)
8586 {
8587   tree identifier;
8588   tree decl;
8589   tree fns;
8590
8591   /* If the next token is `operator', then we have either an
8592      operator-function-id or a conversion-function-id.  */
8593   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8594     {
8595       /* We don't know whether we're looking at an
8596          operator-function-id or a conversion-function-id.  */
8597       cp_parser_parse_tentatively (parser);
8598       /* Try an operator-function-id.  */
8599       identifier = cp_parser_operator_function_id (parser);
8600       /* If that didn't work, try a conversion-function-id.  */
8601       if (!cp_parser_parse_definitely (parser))
8602         {
8603           cp_parser_error (parser, "expected template-name");
8604           return error_mark_node;
8605         }
8606     }
8607   /* Look for the identifier.  */
8608   else
8609     identifier = cp_parser_identifier (parser);
8610
8611   /* If we didn't find an identifier, we don't have a template-id.  */
8612   if (identifier == error_mark_node)
8613     return error_mark_node;
8614
8615   /* If the name immediately followed the `template' keyword, then it
8616      is a template-name.  However, if the next token is not `<', then
8617      we do not treat it as a template-name, since it is not being used
8618      as part of a template-id.  This enables us to handle constructs
8619      like:
8620
8621        template <typename T> struct S { S(); };
8622        template <typename T> S<T>::S();
8623
8624      correctly.  We would treat `S' as a template -- if it were `S<T>'
8625      -- but we do not if there is no `<'.  */
8626
8627   if (processing_template_decl
8628       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8629     {
8630       /* In a declaration, in a dependent context, we pretend that the
8631          "template" keyword was present in order to improve error
8632          recovery.  For example, given:
8633
8634            template <typename T> void f(T::X<int>);
8635
8636          we want to treat "X<int>" as a template-id.  */
8637       if (is_declaration
8638           && !template_keyword_p
8639           && parser->scope && TYPE_P (parser->scope)
8640           && dependent_type_p (parser->scope)
8641           /* Do not do this for dtors (or ctors), since they never
8642              need the template keyword before their name.  */
8643           && !constructor_name_p (identifier, parser->scope))
8644         {
8645           ptrdiff_t start;
8646           cp_token* token;
8647           /* Explain what went wrong.  */
8648           error ("non-template `%D' used as template", identifier);
8649           inform ("use `%T::template %D' to indicate that it is a template",
8650                   parser->scope, identifier);
8651           /* If parsing tentatively, find the location of the "<"
8652              token.  */
8653           if (cp_parser_parsing_tentatively (parser)
8654               && !cp_parser_committed_to_tentative_parse (parser))
8655             {
8656               cp_parser_simulate_error (parser);
8657               token = cp_lexer_peek_token (parser->lexer);
8658               token = cp_lexer_prev_token (parser->lexer, token);
8659               start = cp_lexer_token_difference (parser->lexer,
8660                                                  parser->lexer->first_token,
8661                                                  token);
8662             }
8663           else
8664             start = -1;
8665           /* Parse the template arguments so that we can issue error
8666              messages about them.  */
8667           cp_lexer_consume_token (parser->lexer);
8668           cp_parser_enclosed_template_argument_list (parser);
8669           /* Skip tokens until we find a good place from which to
8670              continue parsing.  */
8671           cp_parser_skip_to_closing_parenthesis (parser,
8672                                                  /*recovering=*/true,
8673                                                  /*or_comma=*/true,
8674                                                  /*consume_paren=*/false);
8675           /* If parsing tentatively, permanently remove the
8676              template argument list.  That will prevent duplicate
8677              error messages from being issued about the missing
8678              "template" keyword.  */
8679           if (start >= 0)
8680             {
8681               token = cp_lexer_advance_token (parser->lexer,
8682                                               parser->lexer->first_token,
8683                                               start);
8684               cp_lexer_purge_tokens_after (parser->lexer, token);
8685             }
8686           if (is_identifier)
8687             *is_identifier = true;
8688           return identifier;
8689         }
8690
8691       /* If the "template" keyword is present, then there is generally
8692          no point in doing name-lookup, so we just return IDENTIFIER.
8693          But, if the qualifying scope is non-dependent then we can
8694          (and must) do name-lookup normally.  */
8695       if (template_keyword_p
8696           && (!parser->scope
8697               || (TYPE_P (parser->scope) 
8698                   && dependent_type_p (parser->scope))))
8699         return identifier;
8700     }
8701
8702   /* Look up the name.  */
8703   decl = cp_parser_lookup_name (parser, identifier,
8704                                 /*is_type=*/false,
8705                                 /*is_template=*/false,
8706                                 /*is_namespace=*/false,
8707                                 check_dependency_p);
8708   decl = maybe_get_template_decl_from_type_decl (decl);
8709
8710   /* If DECL is a template, then the name was a template-name.  */
8711   if (TREE_CODE (decl) == TEMPLATE_DECL)
8712     ;
8713   else
8714     {
8715       /* The standard does not explicitly indicate whether a name that
8716          names a set of overloaded declarations, some of which are
8717          templates, is a template-name.  However, such a name should
8718          be a template-name; otherwise, there is no way to form a
8719          template-id for the overloaded templates.  */
8720       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8721       if (TREE_CODE (fns) == OVERLOAD)
8722         {
8723           tree fn;
8724
8725           for (fn = fns; fn; fn = OVL_NEXT (fn))
8726             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8727               break;
8728         }
8729       else
8730         {
8731           /* Otherwise, the name does not name a template.  */
8732           cp_parser_error (parser, "expected template-name");
8733           return error_mark_node;
8734         }
8735     }
8736
8737   /* If DECL is dependent, and refers to a function, then just return
8738      its name; we will look it up again during template instantiation.  */
8739   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8740     {
8741       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8742       if (TYPE_P (scope) && dependent_type_p (scope))
8743         return identifier;
8744     }
8745
8746   return decl;
8747 }
8748
8749 /* Parse a template-argument-list.
8750
8751    template-argument-list:
8752      template-argument
8753      template-argument-list , template-argument
8754
8755    Returns a TREE_VEC containing the arguments.  */
8756
8757 static tree
8758 cp_parser_template_argument_list (cp_parser* parser)
8759 {
8760   tree fixed_args[10];
8761   unsigned n_args = 0;
8762   unsigned alloced = 10;
8763   tree *arg_ary = fixed_args;
8764   tree vec;
8765   bool saved_in_template_argument_list_p;
8766
8767   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8768   parser->in_template_argument_list_p = true;
8769   do
8770     {
8771       tree argument;
8772
8773       if (n_args)
8774         /* Consume the comma.  */
8775         cp_lexer_consume_token (parser->lexer);
8776
8777       /* Parse the template-argument.  */
8778       argument = cp_parser_template_argument (parser);
8779       if (n_args == alloced)
8780         {
8781           alloced *= 2;
8782
8783           if (arg_ary == fixed_args)
8784             {
8785               arg_ary = xmalloc (sizeof (tree) * alloced);
8786               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8787             }
8788           else
8789             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8790         }
8791       arg_ary[n_args++] = argument;
8792     }
8793   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8794
8795   vec = make_tree_vec (n_args);
8796
8797   while (n_args--)
8798     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8799
8800   if (arg_ary != fixed_args)
8801     free (arg_ary);
8802   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8803   return vec;
8804 }
8805
8806 /* Parse a template-argument.
8807
8808    template-argument:
8809      assignment-expression
8810      type-id
8811      id-expression
8812
8813    The representation is that of an assignment-expression, type-id, or
8814    id-expression -- except that the qualified id-expression is
8815    evaluated, so that the value returned is either a DECL or an
8816    OVERLOAD.
8817
8818    Although the standard says "assignment-expression", it forbids
8819    throw-expressions or assignments in the template argument.
8820    Therefore, we use "conditional-expression" instead.  */
8821
8822 static tree
8823 cp_parser_template_argument (cp_parser* parser)
8824 {
8825   tree argument;
8826   bool template_p;
8827   bool address_p;
8828   bool maybe_type_id = false;
8829   cp_token *token;
8830   cp_id_kind idk;
8831   tree qualifying_class;
8832
8833   /* There's really no way to know what we're looking at, so we just
8834      try each alternative in order.
8835
8836        [temp.arg]
8837
8838        In a template-argument, an ambiguity between a type-id and an
8839        expression is resolved to a type-id, regardless of the form of
8840        the corresponding template-parameter.
8841
8842      Therefore, we try a type-id first.  */
8843   cp_parser_parse_tentatively (parser);
8844   argument = cp_parser_type_id (parser);
8845   /* If there was no error parsing the type-id but the next token is a '>>',
8846      we probably found a typo for '> >'. But there are type-id which are
8847      also valid expressions. For instance:
8848
8849      struct X { int operator >> (int); };
8850      template <int V> struct Foo {};
8851      Foo<X () >> 5> r;
8852
8853      Here 'X()' is a valid type-id of a function type, but the user just
8854      wanted to write the expression "X() >> 5". Thus, we remember that we
8855      found a valid type-id, but we still try to parse the argument as an
8856      expression to see what happens.  */
8857   if (!cp_parser_error_occurred (parser)
8858       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8859     {
8860       maybe_type_id = true;
8861       cp_parser_abort_tentative_parse (parser);
8862     }
8863   else
8864     {
8865       /* If the next token isn't a `,' or a `>', then this argument wasn't
8866       really finished. This means that the argument is not a valid
8867       type-id.  */
8868       if (!cp_parser_next_token_ends_template_argument_p (parser))
8869         cp_parser_error (parser, "expected template-argument");
8870       /* If that worked, we're done.  */
8871       if (cp_parser_parse_definitely (parser))
8872         return argument;
8873     }
8874   /* We're still not sure what the argument will be.  */
8875   cp_parser_parse_tentatively (parser);
8876   /* Try a template.  */
8877   argument = cp_parser_id_expression (parser,
8878                                       /*template_keyword_p=*/false,
8879                                       /*check_dependency_p=*/true,
8880                                       &template_p,
8881                                       /*declarator_p=*/false);
8882   /* If the next token isn't a `,' or a `>', then this argument wasn't
8883      really finished.  */
8884   if (!cp_parser_next_token_ends_template_argument_p (parser))
8885     cp_parser_error (parser, "expected template-argument");
8886   if (!cp_parser_error_occurred (parser))
8887     {
8888       /* Figure out what is being referred to.  If the id-expression
8889          was for a class template specialization, then we will have a
8890          TYPE_DECL at this point.  There is no need to do name lookup
8891          at this point in that case.  */
8892       if (TREE_CODE (argument) != TYPE_DECL)
8893         argument = cp_parser_lookup_name (parser, argument,
8894                                           /*is_type=*/false,
8895                                           /*is_template=*/template_p,
8896                                           /*is_namespace=*/false,
8897                                           /*check_dependency=*/true);
8898       if (TREE_CODE (argument) != TEMPLATE_DECL
8899           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8900         cp_parser_error (parser, "expected template-name");
8901     }
8902   if (cp_parser_parse_definitely (parser))
8903     return argument;
8904   /* It must be a non-type argument.  There permitted cases are given
8905      in [temp.arg.nontype]:
8906
8907      -- an integral constant-expression of integral or enumeration
8908         type; or
8909
8910      -- the name of a non-type template-parameter; or
8911
8912      -- the name of an object or function with external linkage...
8913
8914      -- the address of an object or function with external linkage...
8915
8916      -- a pointer to member...  */
8917   /* Look for a non-type template parameter.  */
8918   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8919     {
8920       cp_parser_parse_tentatively (parser);
8921       argument = cp_parser_primary_expression (parser,
8922                                                &idk,
8923                                                &qualifying_class);
8924       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8925           || !cp_parser_next_token_ends_template_argument_p (parser))
8926         cp_parser_simulate_error (parser);
8927       if (cp_parser_parse_definitely (parser))
8928         return argument;
8929     }
8930   /* If the next token is "&", the argument must be the address of an
8931      object or function with external linkage.  */
8932   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8933   if (address_p)
8934     cp_lexer_consume_token (parser->lexer);
8935   /* See if we might have an id-expression.  */
8936   token = cp_lexer_peek_token (parser->lexer);
8937   if (token->type == CPP_NAME
8938       || token->keyword == RID_OPERATOR
8939       || token->type == CPP_SCOPE
8940       || token->type == CPP_TEMPLATE_ID
8941       || token->type == CPP_NESTED_NAME_SPECIFIER)
8942     {
8943       cp_parser_parse_tentatively (parser);
8944       argument = cp_parser_primary_expression (parser,
8945                                                &idk,
8946                                                &qualifying_class);
8947       if (cp_parser_error_occurred (parser)
8948           || !cp_parser_next_token_ends_template_argument_p (parser))
8949         cp_parser_abort_tentative_parse (parser);
8950       else
8951         {
8952           if (qualifying_class)
8953             argument = finish_qualified_id_expr (qualifying_class,
8954                                                  argument,
8955                                                  /*done=*/true,
8956                                                  address_p);
8957           if (TREE_CODE (argument) == VAR_DECL)
8958             {
8959               /* A variable without external linkage might still be a
8960                  valid constant-expression, so no error is issued here
8961                  if the external-linkage check fails.  */
8962               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8963                 cp_parser_simulate_error (parser);
8964             }
8965           else if (is_overloaded_fn (argument))
8966             /* All overloaded functions are allowed; if the external
8967                linkage test does not pass, an error will be issued
8968                later.  */
8969             ;
8970           else if (address_p
8971                    && (TREE_CODE (argument) == OFFSET_REF
8972                        || TREE_CODE (argument) == SCOPE_REF))
8973             /* A pointer-to-member.  */
8974             ;
8975           else
8976             cp_parser_simulate_error (parser);
8977
8978           if (cp_parser_parse_definitely (parser))
8979             {
8980               if (address_p)
8981                 argument = build_x_unary_op (ADDR_EXPR, argument);
8982               return argument;
8983             }
8984         }
8985     }
8986   /* If the argument started with "&", there are no other valid
8987      alternatives at this point.  */
8988   if (address_p)
8989     {
8990       cp_parser_error (parser, "invalid non-type template argument");
8991       return error_mark_node;
8992     }
8993   /* If the argument wasn't successfully parsed as a type-id followed
8994      by '>>', the argument can only be a constant expression now.
8995      Otherwise, we try parsing the constant-expression tentatively,
8996      because the argument could really be a type-id.  */
8997   if (maybe_type_id)
8998     cp_parser_parse_tentatively (parser);
8999   argument = cp_parser_constant_expression (parser,
9000                                             /*allow_non_constant_p=*/false,
9001                                             /*non_constant_p=*/NULL);
9002   argument = fold_non_dependent_expr (argument);
9003   if (!maybe_type_id)
9004     return argument;
9005   if (!cp_parser_next_token_ends_template_argument_p (parser))
9006     cp_parser_error (parser, "expected template-argument");
9007   if (cp_parser_parse_definitely (parser))
9008     return argument;
9009   /* We did our best to parse the argument as a non type-id, but that
9010      was the only alternative that matched (albeit with a '>' after
9011      it). We can assume it's just a typo from the user, and a
9012      diagnostic will then be issued.  */
9013   return cp_parser_type_id (parser);
9014 }
9015
9016 /* Parse an explicit-instantiation.
9017
9018    explicit-instantiation:
9019      template declaration
9020
9021    Although the standard says `declaration', what it really means is:
9022
9023    explicit-instantiation:
9024      template decl-specifier-seq [opt] declarator [opt] ;
9025
9026    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9027    supposed to be allowed.  A defect report has been filed about this
9028    issue.
9029
9030    GNU Extension:
9031
9032    explicit-instantiation:
9033      storage-class-specifier template
9034        decl-specifier-seq [opt] declarator [opt] ;
9035      function-specifier template
9036        decl-specifier-seq [opt] declarator [opt] ;  */
9037
9038 static void
9039 cp_parser_explicit_instantiation (cp_parser* parser)
9040 {
9041   int declares_class_or_enum;
9042   cp_decl_specifier_seq decl_specifiers;
9043   tree extension_specifier = NULL_TREE;
9044
9045   /* Look for an (optional) storage-class-specifier or
9046      function-specifier.  */
9047   if (cp_parser_allow_gnu_extensions_p (parser))
9048     {
9049       extension_specifier
9050         = cp_parser_storage_class_specifier_opt (parser);
9051       if (!extension_specifier)
9052         extension_specifier 
9053           = cp_parser_function_specifier_opt (parser,
9054                                               /*decl_specs=*/NULL);
9055     }
9056
9057   /* Look for the `template' keyword.  */
9058   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9059   /* Let the front end know that we are processing an explicit
9060      instantiation.  */
9061   begin_explicit_instantiation ();
9062   /* [temp.explicit] says that we are supposed to ignore access
9063      control while processing explicit instantiation directives.  */
9064   push_deferring_access_checks (dk_no_check);
9065   /* Parse a decl-specifier-seq.  */
9066   cp_parser_decl_specifier_seq (parser,
9067                                 CP_PARSER_FLAGS_OPTIONAL,
9068                                 &decl_specifiers,
9069                                 &declares_class_or_enum);
9070   /* If there was exactly one decl-specifier, and it declared a class,
9071      and there's no declarator, then we have an explicit type
9072      instantiation.  */
9073   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9074     {
9075       tree type;
9076
9077       type = check_tag_decl (&decl_specifiers);
9078       /* Turn access control back on for names used during
9079          template instantiation.  */
9080       pop_deferring_access_checks ();
9081       if (type)
9082         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9083     }
9084   else
9085     {
9086       cp_declarator *declarator;
9087       tree decl;
9088
9089       /* Parse the declarator.  */
9090       declarator
9091         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9092                                 /*ctor_dtor_or_conv_p=*/NULL,
9093                                 /*parenthesized_p=*/NULL);
9094       cp_parser_check_for_definition_in_return_type (declarator,
9095                                                      declares_class_or_enum);
9096       if (declarator != cp_error_declarator)
9097         {
9098           decl = grokdeclarator (declarator, &decl_specifiers,
9099                                  NORMAL, 0, NULL);
9100           /* Turn access control back on for names used during
9101              template instantiation.  */
9102           pop_deferring_access_checks ();
9103           /* Do the explicit instantiation.  */
9104           do_decl_instantiation (decl, extension_specifier);
9105         }
9106       else
9107         {
9108           pop_deferring_access_checks ();
9109           /* Skip the body of the explicit instantiation.  */
9110           cp_parser_skip_to_end_of_statement (parser);
9111         }
9112     }
9113   /* We're done with the instantiation.  */
9114   end_explicit_instantiation ();
9115
9116   cp_parser_consume_semicolon_at_end_of_statement (parser);
9117 }
9118
9119 /* Parse an explicit-specialization.
9120
9121    explicit-specialization:
9122      template < > declaration
9123
9124    Although the standard says `declaration', what it really means is:
9125
9126    explicit-specialization:
9127      template <> decl-specifier [opt] init-declarator [opt] ;
9128      template <> function-definition
9129      template <> explicit-specialization
9130      template <> template-declaration  */
9131
9132 static void
9133 cp_parser_explicit_specialization (cp_parser* parser)
9134 {
9135   /* Look for the `template' keyword.  */
9136   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9137   /* Look for the `<'.  */
9138   cp_parser_require (parser, CPP_LESS, "`<'");
9139   /* Look for the `>'.  */
9140   cp_parser_require (parser, CPP_GREATER, "`>'");
9141   /* We have processed another parameter list.  */
9142   ++parser->num_template_parameter_lists;
9143   /* Let the front end know that we are beginning a specialization.  */
9144   begin_specialization ();
9145
9146   /* If the next keyword is `template', we need to figure out whether
9147      or not we're looking a template-declaration.  */
9148   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9149     {
9150       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9151           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9152         cp_parser_template_declaration_after_export (parser,
9153                                                      /*member_p=*/false);
9154       else
9155         cp_parser_explicit_specialization (parser);
9156     }
9157   else
9158     /* Parse the dependent declaration.  */
9159     cp_parser_single_declaration (parser,
9160                                   /*member_p=*/false,
9161                                   /*friend_p=*/NULL);
9162
9163   /* We're done with the specialization.  */
9164   end_specialization ();
9165   /* We're done with this parameter list.  */
9166   --parser->num_template_parameter_lists;
9167 }
9168
9169 /* Parse a type-specifier.
9170
9171    type-specifier:
9172      simple-type-specifier
9173      class-specifier
9174      enum-specifier
9175      elaborated-type-specifier
9176      cv-qualifier
9177
9178    GNU Extension:
9179
9180    type-specifier:
9181      __complex__
9182
9183    Returns a representation of the type-specifier.  For a
9184    class-specifier, enum-specifier, or elaborated-type-specifier, a
9185    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9186
9187    If IS_FRIEND is TRUE then this type-specifier is being declared a
9188    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
9189    appearing in a decl-specifier-seq.
9190
9191    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9192    class-specifier, enum-specifier, or elaborated-type-specifier, then
9193    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9194    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9195    zero.
9196
9197    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9198    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9199    is set to FALSE.  */
9200
9201 static tree
9202 cp_parser_type_specifier (cp_parser* parser,
9203                           cp_parser_flags flags,
9204                           cp_decl_specifier_seq *decl_specs,
9205                           bool is_declaration,
9206                           int* declares_class_or_enum,
9207                           bool* is_cv_qualifier)
9208 {
9209   tree type_spec = NULL_TREE;
9210   cp_token *token;
9211   enum rid keyword;
9212   cp_decl_spec ds = ds_last;
9213
9214   /* Assume this type-specifier does not declare a new type.  */
9215   if (declares_class_or_enum)
9216     *declares_class_or_enum = 0;
9217   /* And that it does not specify a cv-qualifier.  */
9218   if (is_cv_qualifier)
9219     *is_cv_qualifier = false;
9220   /* Peek at the next token.  */
9221   token = cp_lexer_peek_token (parser->lexer);
9222
9223   /* If we're looking at a keyword, we can use that to guide the
9224      production we choose.  */
9225   keyword = token->keyword;
9226   switch (keyword)
9227     {
9228       /* Any of these indicate either a class-specifier, or an
9229          elaborated-type-specifier.  */
9230     case RID_CLASS:
9231     case RID_STRUCT:
9232     case RID_UNION:
9233     case RID_ENUM:
9234       /* Parse tentatively so that we can back up if we don't find a
9235          class-specifier or enum-specifier.  */
9236       cp_parser_parse_tentatively (parser);
9237       /* Look for the class-specifier or enum-specifier.  */
9238       if (keyword == RID_ENUM)
9239         type_spec = cp_parser_enum_specifier (parser);
9240       else
9241         type_spec = cp_parser_class_specifier (parser);
9242
9243       /* If that worked, we're done.  */
9244       if (cp_parser_parse_definitely (parser))
9245         {
9246           if (declares_class_or_enum)
9247             *declares_class_or_enum = 2;
9248           if (decl_specs)
9249             cp_parser_set_decl_spec_type (decl_specs,
9250                                           type_spec,
9251                                           /*user_defined_p=*/true);
9252           return type_spec;
9253         }
9254
9255       /* Fall through.  */
9256
9257     case RID_TYPENAME:
9258       /* Look for an elaborated-type-specifier.  */
9259       type_spec 
9260         = (cp_parser_elaborated_type_specifier 
9261            (parser,
9262             decl_specs && decl_specs->specs[(int) ds_friend],
9263             is_declaration));
9264       /* We're declaring a class or enum -- unless we're using
9265          `typename'.  */
9266       if (declares_class_or_enum && keyword != RID_TYPENAME)
9267         *declares_class_or_enum = 1;
9268       if (decl_specs)
9269         cp_parser_set_decl_spec_type (decl_specs,
9270                                       type_spec,
9271                                       /*user_defined_p=*/true);
9272       return type_spec;
9273
9274     case RID_CONST:
9275       ds = ds_const;
9276       if (is_cv_qualifier)
9277         *is_cv_qualifier = true;
9278       break;
9279       
9280     case RID_VOLATILE:
9281       ds = ds_volatile;
9282       if (is_cv_qualifier)
9283         *is_cv_qualifier = true;
9284       break;
9285
9286     case RID_RESTRICT:
9287       ds = ds_restrict;
9288       if (is_cv_qualifier)
9289         *is_cv_qualifier = true;
9290       break;
9291
9292     case RID_COMPLEX:
9293       /* The `__complex__' keyword is a GNU extension.  */
9294       ds = ds_complex;
9295       break;
9296
9297     default:
9298       break;
9299     }
9300
9301   /* Handle simple keywords.  */
9302   if (ds != ds_last)
9303     {
9304       if (decl_specs)
9305         {
9306           ++decl_specs->specs[(int)ds];
9307           decl_specs->any_specifiers_p = true;
9308         }
9309       return cp_lexer_consume_token (parser->lexer)->value;
9310     }
9311
9312   /* If we do not already have a type-specifier, assume we are looking
9313      at a simple-type-specifier.  */
9314   type_spec = cp_parser_simple_type_specifier (parser, 
9315                                                decl_specs,
9316                                                flags);
9317
9318   /* If we didn't find a type-specifier, and a type-specifier was not
9319      optional in this context, issue an error message.  */
9320   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9321     {
9322       cp_parser_error (parser, "expected type specifier");
9323       return error_mark_node;
9324     }
9325
9326   return type_spec;
9327 }
9328
9329 /* Parse a simple-type-specifier.
9330
9331    simple-type-specifier:
9332      :: [opt] nested-name-specifier [opt] type-name
9333      :: [opt] nested-name-specifier template template-id
9334      char
9335      wchar_t
9336      bool
9337      short
9338      int
9339      long
9340      signed
9341      unsigned
9342      float
9343      double
9344      void
9345
9346    GNU Extension:
9347
9348    simple-type-specifier:
9349      __typeof__ unary-expression
9350      __typeof__ ( type-id )
9351
9352    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9353    appropriately updated.  */
9354
9355 static tree
9356 cp_parser_simple_type_specifier (cp_parser* parser, 
9357                                  cp_decl_specifier_seq *decl_specs,
9358                                  cp_parser_flags flags)
9359 {
9360   tree type = NULL_TREE;
9361   cp_token *token;
9362
9363   /* Peek at the next token.  */
9364   token = cp_lexer_peek_token (parser->lexer);
9365
9366   /* If we're looking at a keyword, things are easy.  */
9367   switch (token->keyword)
9368     {
9369     case RID_CHAR:
9370       if (decl_specs)
9371         decl_specs->explicit_char_p = true;
9372       type = char_type_node;
9373       break;
9374     case RID_WCHAR:
9375       type = wchar_type_node;
9376       break;
9377     case RID_BOOL:
9378       type = boolean_type_node;
9379       break;
9380     case RID_SHORT:
9381       if (decl_specs)
9382         ++decl_specs->specs[(int) ds_short];
9383       type = short_integer_type_node;
9384       break;
9385     case RID_INT:
9386       if (decl_specs)
9387         decl_specs->explicit_int_p = true;
9388       type = integer_type_node;
9389       break;
9390     case RID_LONG:
9391       if (decl_specs)
9392         ++decl_specs->specs[(int) ds_long];
9393       type = long_integer_type_node;
9394       break;
9395     case RID_SIGNED:
9396       if (decl_specs)
9397         ++decl_specs->specs[(int) ds_signed];
9398       type = integer_type_node;
9399       break;
9400     case RID_UNSIGNED:
9401       if (decl_specs)
9402         ++decl_specs->specs[(int) ds_unsigned];
9403       type = unsigned_type_node;
9404       break;
9405     case RID_FLOAT:
9406       type = float_type_node;
9407       break;
9408     case RID_DOUBLE:
9409       type = double_type_node;
9410       break;
9411     case RID_VOID:
9412       type = void_type_node;
9413       break;
9414
9415     case RID_TYPEOF:
9416       /* Consume the `typeof' token.  */
9417       cp_lexer_consume_token (parser->lexer);
9418       /* Parse the operand to `typeof'.  */
9419       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9420       /* If it is not already a TYPE, take its type.  */
9421       if (!TYPE_P (type))
9422         type = finish_typeof (type);
9423
9424       if (decl_specs)
9425         cp_parser_set_decl_spec_type (decl_specs, type,
9426                                       /*user_defined_p=*/true);
9427         
9428       return type;
9429
9430     default:
9431       break;
9432     }
9433
9434   /* If the type-specifier was for a built-in type, we're done.  */
9435   if (type)
9436     {
9437       tree id;
9438
9439       /* Record the type.  */
9440       if (decl_specs
9441           && (token->keyword != RID_SIGNED
9442               && token->keyword != RID_UNSIGNED
9443               && token->keyword != RID_SHORT
9444               && token->keyword != RID_LONG))
9445         cp_parser_set_decl_spec_type (decl_specs, 
9446                                       type,
9447                                       /*user_defined=*/false);
9448       if (decl_specs)
9449         decl_specs->any_specifiers_p = true;
9450
9451       /* Consume the token.  */
9452       id = cp_lexer_consume_token (parser->lexer)->value;
9453
9454       /* There is no valid C++ program where a non-template type is
9455          followed by a "<".  That usually indicates that the user thought
9456          that the type was a template.  */
9457       cp_parser_check_for_invalid_template_id (parser, type);
9458
9459       return TYPE_NAME (type);
9460     }
9461
9462   /* The type-specifier must be a user-defined type.  */
9463   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9464     {
9465       bool qualified_p;
9466
9467       /* Don't gobble tokens or issue error messages if this is an
9468          optional type-specifier.  */
9469       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9470         cp_parser_parse_tentatively (parser);
9471
9472       /* Look for the optional `::' operator.  */
9473       cp_parser_global_scope_opt (parser,
9474                                   /*current_scope_valid_p=*/false);
9475       /* Look for the nested-name specifier.  */
9476       qualified_p
9477         = (cp_parser_nested_name_specifier_opt (parser,
9478                                                 /*typename_keyword_p=*/false,
9479                                                 /*check_dependency_p=*/true,
9480                                                 /*type_p=*/false,
9481                                                 /*is_declaration=*/false)
9482            != NULL_TREE);
9483       /* If we have seen a nested-name-specifier, and the next token
9484          is `template', then we are using the template-id production.  */
9485       if (parser->scope
9486           && cp_parser_optional_template_keyword (parser))
9487         {
9488           /* Look for the template-id.  */
9489           type = cp_parser_template_id (parser,
9490                                         /*template_keyword_p=*/true,
9491                                         /*check_dependency_p=*/true,
9492                                         /*is_declaration=*/false);
9493           /* If the template-id did not name a type, we are out of
9494              luck.  */
9495           if (TREE_CODE (type) != TYPE_DECL)
9496             {
9497               cp_parser_error (parser, "expected template-id for type");
9498               type = NULL_TREE;
9499             }
9500         }
9501       /* Otherwise, look for a type-name.  */
9502       else
9503         type = cp_parser_type_name (parser);
9504       /* Keep track of all name-lookups performed in class scopes.  */
9505       if (type  
9506           && !qualified_p
9507           && TREE_CODE (type) == TYPE_DECL 
9508           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9509         maybe_note_name_used_in_class (DECL_NAME (type), type);
9510       /* If it didn't work out, we don't have a TYPE.  */
9511       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9512           && !cp_parser_parse_definitely (parser))
9513         type = NULL_TREE;
9514       if (type && decl_specs)
9515         cp_parser_set_decl_spec_type (decl_specs, type,
9516                                       /*user_defined=*/true);
9517     }
9518
9519   /* If we didn't get a type-name, issue an error message.  */
9520   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9521     {
9522       cp_parser_error (parser, "expected type-name");
9523       return error_mark_node;
9524     }
9525
9526   /* There is no valid C++ program where a non-template type is
9527      followed by a "<".  That usually indicates that the user thought
9528      that the type was a template.  */
9529   if (type && type != error_mark_node)
9530     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9531
9532   return type;
9533 }
9534
9535 /* Parse a type-name.
9536
9537    type-name:
9538      class-name
9539      enum-name
9540      typedef-name
9541
9542    enum-name:
9543      identifier
9544
9545    typedef-name:
9546      identifier
9547
9548    Returns a TYPE_DECL for the the type.  */
9549
9550 static tree
9551 cp_parser_type_name (cp_parser* parser)
9552 {
9553   tree type_decl;
9554   tree identifier;
9555
9556   /* We can't know yet whether it is a class-name or not.  */
9557   cp_parser_parse_tentatively (parser);
9558   /* Try a class-name.  */
9559   type_decl = cp_parser_class_name (parser,
9560                                     /*typename_keyword_p=*/false,
9561                                     /*template_keyword_p=*/false,
9562                                     /*type_p=*/false,
9563                                     /*check_dependency_p=*/true,
9564                                     /*class_head_p=*/false,
9565                                     /*is_declaration=*/false);
9566   /* If it's not a class-name, keep looking.  */
9567   if (!cp_parser_parse_definitely (parser))
9568     {
9569       /* It must be a typedef-name or an enum-name.  */
9570       identifier = cp_parser_identifier (parser);
9571       if (identifier == error_mark_node)
9572         return error_mark_node;
9573
9574       /* Look up the type-name.  */
9575       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9576       /* Issue an error if we did not find a type-name.  */
9577       if (TREE_CODE (type_decl) != TYPE_DECL)
9578         {
9579           if (!cp_parser_simulate_error (parser))
9580             cp_parser_name_lookup_error (parser, identifier, type_decl,
9581                                          "is not a type");
9582           type_decl = error_mark_node;
9583         }
9584       /* Remember that the name was used in the definition of the
9585          current class so that we can check later to see if the
9586          meaning would have been different after the class was
9587          entirely defined.  */
9588       else if (type_decl != error_mark_node
9589                && !parser->scope)
9590         maybe_note_name_used_in_class (identifier, type_decl);
9591     }
9592
9593   return type_decl;
9594 }
9595
9596
9597 /* Parse an elaborated-type-specifier.  Note that the grammar given
9598    here incorporates the resolution to DR68.
9599
9600    elaborated-type-specifier:
9601      class-key :: [opt] nested-name-specifier [opt] identifier
9602      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9603      enum :: [opt] nested-name-specifier [opt] identifier
9604      typename :: [opt] nested-name-specifier identifier
9605      typename :: [opt] nested-name-specifier template [opt]
9606        template-id
9607
9608    GNU extension:
9609
9610    elaborated-type-specifier:
9611      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9612      class-key attributes :: [opt] nested-name-specifier [opt]
9613                template [opt] template-id
9614      enum attributes :: [opt] nested-name-specifier [opt] identifier
9615
9616    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9617    declared `friend'.  If IS_DECLARATION is TRUE, then this
9618    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9619    something is being declared.
9620
9621    Returns the TYPE specified.  */
9622
9623 static tree
9624 cp_parser_elaborated_type_specifier (cp_parser* parser,
9625                                      bool is_friend,
9626                                      bool is_declaration)
9627 {
9628   enum tag_types tag_type;
9629   tree identifier;
9630   tree type = NULL_TREE;
9631   tree attributes = NULL_TREE;
9632
9633   /* See if we're looking at the `enum' keyword.  */
9634   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9635     {
9636       /* Consume the `enum' token.  */
9637       cp_lexer_consume_token (parser->lexer);
9638       /* Remember that it's an enumeration type.  */
9639       tag_type = enum_type;
9640       /* Parse the attributes.  */
9641       attributes = cp_parser_attributes_opt (parser);
9642     }
9643   /* Or, it might be `typename'.  */
9644   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9645                                            RID_TYPENAME))
9646     {
9647       /* Consume the `typename' token.  */
9648       cp_lexer_consume_token (parser->lexer);
9649       /* Remember that it's a `typename' type.  */
9650       tag_type = typename_type;
9651       /* The `typename' keyword is only allowed in templates.  */
9652       if (!processing_template_decl)
9653         pedwarn ("using `typename' outside of template");
9654     }
9655   /* Otherwise it must be a class-key.  */
9656   else
9657     {
9658       tag_type = cp_parser_class_key (parser);
9659       if (tag_type == none_type)
9660         return error_mark_node;
9661       /* Parse the attributes.  */
9662       attributes = cp_parser_attributes_opt (parser);
9663     }
9664
9665   /* Look for the `::' operator.  */
9666   cp_parser_global_scope_opt (parser,
9667                               /*current_scope_valid_p=*/false);
9668   /* Look for the nested-name-specifier.  */
9669   if (tag_type == typename_type)
9670     {
9671       if (cp_parser_nested_name_specifier (parser,
9672                                            /*typename_keyword_p=*/true,
9673                                            /*check_dependency_p=*/true,
9674                                            /*type_p=*/true,
9675                                            is_declaration)
9676           == error_mark_node)
9677         return error_mark_node;
9678     }
9679   else
9680     /* Even though `typename' is not present, the proposed resolution
9681        to Core Issue 180 says that in `class A<T>::B', `B' should be
9682        considered a type-name, even if `A<T>' is dependent.  */
9683     cp_parser_nested_name_specifier_opt (parser,
9684                                          /*typename_keyword_p=*/true,
9685                                          /*check_dependency_p=*/true,
9686                                          /*type_p=*/true,
9687                                          is_declaration);
9688   /* For everything but enumeration types, consider a template-id.  */
9689   if (tag_type != enum_type)
9690     {
9691       bool template_p = false;
9692       tree decl;
9693
9694       /* Allow the `template' keyword.  */
9695       template_p = cp_parser_optional_template_keyword (parser);
9696       /* If we didn't see `template', we don't know if there's a
9697          template-id or not.  */
9698       if (!template_p)
9699         cp_parser_parse_tentatively (parser);
9700       /* Parse the template-id.  */
9701       decl = cp_parser_template_id (parser, template_p,
9702                                     /*check_dependency_p=*/true,
9703                                     is_declaration);
9704       /* If we didn't find a template-id, look for an ordinary
9705          identifier.  */
9706       if (!template_p && !cp_parser_parse_definitely (parser))
9707         ;
9708       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9709          in effect, then we must assume that, upon instantiation, the
9710          template will correspond to a class.  */
9711       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9712                && tag_type == typename_type)
9713         type = make_typename_type (parser->scope, decl,
9714                                    /*complain=*/1);
9715       else
9716         type = TREE_TYPE (decl);
9717     }
9718
9719   /* For an enumeration type, consider only a plain identifier.  */
9720   if (!type)
9721     {
9722       identifier = cp_parser_identifier (parser);
9723
9724       if (identifier == error_mark_node)
9725         {
9726           parser->scope = NULL_TREE;
9727           return error_mark_node;
9728         }
9729
9730       /* For a `typename', we needn't call xref_tag.  */
9731       if (tag_type == typename_type)
9732         return cp_parser_make_typename_type (parser, parser->scope,
9733                                              identifier);
9734       /* Look up a qualified name in the usual way.  */
9735       if (parser->scope)
9736         {
9737           tree decl;
9738
9739           /* In an elaborated-type-specifier, names are assumed to name
9740              types, so we set IS_TYPE to TRUE when calling
9741              cp_parser_lookup_name.  */
9742           decl = cp_parser_lookup_name (parser, identifier,
9743                                         /*is_type=*/true,
9744                                         /*is_template=*/false,
9745                                         /*is_namespace=*/false,
9746                                         /*check_dependency=*/true);
9747
9748           /* If we are parsing friend declaration, DECL may be a
9749              TEMPLATE_DECL tree node here.  However, we need to check
9750              whether this TEMPLATE_DECL results in valid code.  Consider
9751              the following example:
9752
9753                namespace N {
9754                  template <class T> class C {};
9755                }
9756                class X {
9757                  template <class T> friend class N::C; // #1, valid code
9758                };
9759                template <class T> class Y {
9760                  friend class N::C;                    // #2, invalid code
9761                };
9762
9763              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9764              name lookup of `N::C'.  We see that friend declaration must
9765              be template for the code to be valid.  Note that
9766              processing_template_decl does not work here since it is
9767              always 1 for the above two cases.  */
9768
9769           decl = (cp_parser_maybe_treat_template_as_class
9770                   (decl, /*tag_name_p=*/is_friend
9771                          && parser->num_template_parameter_lists));
9772
9773           if (TREE_CODE (decl) != TYPE_DECL)
9774             {
9775               error ("expected type-name");
9776               return error_mark_node;
9777             }
9778
9779           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9780             check_elaborated_type_specifier
9781               (tag_type, decl,
9782                (parser->num_template_parameter_lists
9783                 || DECL_SELF_REFERENCE_P (decl)));
9784
9785           type = TREE_TYPE (decl);
9786         }
9787       else
9788         {
9789           /* An elaborated-type-specifier sometimes introduces a new type and
9790              sometimes names an existing type.  Normally, the rule is that it
9791              introduces a new type only if there is not an existing type of
9792              the same name already in scope.  For example, given:
9793
9794                struct S {};
9795                void f() { struct S s; }
9796
9797              the `struct S' in the body of `f' is the same `struct S' as in
9798              the global scope; the existing definition is used.  However, if
9799              there were no global declaration, this would introduce a new
9800              local class named `S'.
9801
9802              An exception to this rule applies to the following code:
9803
9804                namespace N { struct S; }
9805
9806              Here, the elaborated-type-specifier names a new type
9807              unconditionally; even if there is already an `S' in the
9808              containing scope this declaration names a new type.
9809              This exception only applies if the elaborated-type-specifier
9810              forms the complete declaration:
9811
9812                [class.name]
9813
9814                A declaration consisting solely of `class-key identifier ;' is
9815                either a redeclaration of the name in the current scope or a
9816                forward declaration of the identifier as a class name.  It
9817                introduces the name into the current scope.
9818
9819              We are in this situation precisely when the next token is a `;'.
9820
9821              An exception to the exception is that a `friend' declaration does
9822              *not* name a new type; i.e., given:
9823
9824                struct S { friend struct T; };
9825
9826              `T' is not a new type in the scope of `S'.
9827
9828              Also, `new struct S' or `sizeof (struct S)' never results in the
9829              definition of a new type; a new type can only be declared in a
9830              declaration context.  */
9831
9832           /* Warn about attributes. They are ignored.  */
9833           if (attributes)
9834             warning ("type attributes are honored only at type definition");
9835
9836           type = xref_tag (tag_type, identifier,
9837                            (is_friend
9838                             || !is_declaration
9839                             || cp_lexer_next_token_is_not (parser->lexer,
9840                                                            CPP_SEMICOLON)),
9841                            parser->num_template_parameter_lists);
9842         }
9843     }
9844   if (tag_type != enum_type)
9845     cp_parser_check_class_key (tag_type, type);
9846
9847   /* A "<" cannot follow an elaborated type specifier.  If that
9848      happens, the user was probably trying to form a template-id.  */
9849   cp_parser_check_for_invalid_template_id (parser, type);
9850
9851   return type;
9852 }
9853
9854 /* Parse an enum-specifier.
9855
9856    enum-specifier:
9857      enum identifier [opt] { enumerator-list [opt] }
9858
9859    Returns an ENUM_TYPE representing the enumeration.  */
9860
9861 static tree
9862 cp_parser_enum_specifier (cp_parser* parser)
9863 {
9864   cp_token *token;
9865   tree identifier = NULL_TREE;
9866   tree type;
9867
9868   /* Look for the `enum' keyword.  */
9869   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9870     return error_mark_node;
9871   /* Peek at the next token.  */
9872   token = cp_lexer_peek_token (parser->lexer);
9873
9874   /* See if it is an identifier.  */
9875   if (token->type == CPP_NAME)
9876     identifier = cp_parser_identifier (parser);
9877
9878   /* Look for the `{'.  */
9879   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9880     return error_mark_node;
9881
9882   /* At this point, we're going ahead with the enum-specifier, even
9883      if some other problem occurs.  */
9884   cp_parser_commit_to_tentative_parse (parser);
9885
9886   /* Issue an error message if type-definitions are forbidden here.  */
9887   cp_parser_check_type_definition (parser);
9888
9889   /* Create the new type.  */
9890   type = start_enum (identifier ? identifier : make_anon_name ());
9891
9892   /* Peek at the next token.  */
9893   token = cp_lexer_peek_token (parser->lexer);
9894   /* If it's not a `}', then there are some enumerators.  */
9895   if (token->type != CPP_CLOSE_BRACE)
9896     cp_parser_enumerator_list (parser, type);
9897   /* Look for the `}'.  */
9898   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9899
9900   /* Finish up the enumeration.  */
9901   finish_enum (type);
9902
9903   return type;
9904 }
9905
9906 /* Parse an enumerator-list.  The enumerators all have the indicated
9907    TYPE.
9908
9909    enumerator-list:
9910      enumerator-definition
9911      enumerator-list , enumerator-definition  */
9912
9913 static void
9914 cp_parser_enumerator_list (cp_parser* parser, tree type)
9915 {
9916   while (true)
9917     {
9918       cp_token *token;
9919
9920       /* Parse an enumerator-definition.  */
9921       cp_parser_enumerator_definition (parser, type);
9922       /* Peek at the next token.  */
9923       token = cp_lexer_peek_token (parser->lexer);
9924       /* If it's not a `,', then we've reached the end of the
9925          list.  */
9926       if (token->type != CPP_COMMA)
9927         break;
9928       /* Otherwise, consume the `,' and keep going.  */
9929       cp_lexer_consume_token (parser->lexer);
9930       /* If the next token is a `}', there is a trailing comma.  */
9931       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9932         {
9933           if (pedantic && !in_system_header)
9934             pedwarn ("comma at end of enumerator list");
9935           break;
9936         }
9937     }
9938 }
9939
9940 /* Parse an enumerator-definition.  The enumerator has the indicated
9941    TYPE.
9942
9943    enumerator-definition:
9944      enumerator
9945      enumerator = constant-expression
9946
9947    enumerator:
9948      identifier  */
9949
9950 static void
9951 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9952 {
9953   cp_token *token;
9954   tree identifier;
9955   tree value;
9956
9957   /* Look for the identifier.  */
9958   identifier = cp_parser_identifier (parser);
9959   if (identifier == error_mark_node)
9960     return;
9961
9962   /* Peek at the next token.  */
9963   token = cp_lexer_peek_token (parser->lexer);
9964   /* If it's an `=', then there's an explicit value.  */
9965   if (token->type == CPP_EQ)
9966     {
9967       /* Consume the `=' token.  */
9968       cp_lexer_consume_token (parser->lexer);
9969       /* Parse the value.  */
9970       value = cp_parser_constant_expression (parser,
9971                                              /*allow_non_constant_p=*/false,
9972                                              NULL);
9973     }
9974   else
9975     value = NULL_TREE;
9976
9977   /* Create the enumerator.  */
9978   build_enumerator (identifier, value, type);
9979 }
9980
9981 /* Parse a namespace-name.
9982
9983    namespace-name:
9984      original-namespace-name
9985      namespace-alias
9986
9987    Returns the NAMESPACE_DECL for the namespace.  */
9988
9989 static tree
9990 cp_parser_namespace_name (cp_parser* parser)
9991 {
9992   tree identifier;
9993   tree namespace_decl;
9994
9995   /* Get the name of the namespace.  */
9996   identifier = cp_parser_identifier (parser);
9997   if (identifier == error_mark_node)
9998     return error_mark_node;
9999
10000   /* Look up the identifier in the currently active scope.  Look only
10001      for namespaces, due to:
10002
10003        [basic.lookup.udir]
10004
10005        When looking up a namespace-name in a using-directive or alias
10006        definition, only namespace names are considered.
10007
10008      And:
10009
10010        [basic.lookup.qual]
10011
10012        During the lookup of a name preceding the :: scope resolution
10013        operator, object, function, and enumerator names are ignored.
10014
10015      (Note that cp_parser_class_or_namespace_name only calls this
10016      function if the token after the name is the scope resolution
10017      operator.)  */
10018   namespace_decl = cp_parser_lookup_name (parser, identifier,
10019                                           /*is_type=*/false,
10020                                           /*is_template=*/false,
10021                                           /*is_namespace=*/true,
10022                                           /*check_dependency=*/true);
10023   /* If it's not a namespace, issue an error.  */
10024   if (namespace_decl == error_mark_node
10025       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10026     {
10027       cp_parser_error (parser, "expected namespace-name");
10028       namespace_decl = error_mark_node;
10029     }
10030
10031   return namespace_decl;
10032 }
10033
10034 /* Parse a namespace-definition.
10035
10036    namespace-definition:
10037      named-namespace-definition
10038      unnamed-namespace-definition
10039
10040    named-namespace-definition:
10041      original-namespace-definition
10042      extension-namespace-definition
10043
10044    original-namespace-definition:
10045      namespace identifier { namespace-body }
10046
10047    extension-namespace-definition:
10048      namespace original-namespace-name { namespace-body }
10049
10050    unnamed-namespace-definition:
10051      namespace { namespace-body } */
10052
10053 static void
10054 cp_parser_namespace_definition (cp_parser* parser)
10055 {
10056   tree identifier;
10057
10058   /* Look for the `namespace' keyword.  */
10059   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10060
10061   /* Get the name of the namespace.  We do not attempt to distinguish
10062      between an original-namespace-definition and an
10063      extension-namespace-definition at this point.  The semantic
10064      analysis routines are responsible for that.  */
10065   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10066     identifier = cp_parser_identifier (parser);
10067   else
10068     identifier = NULL_TREE;
10069
10070   /* Look for the `{' to start the namespace.  */
10071   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10072   /* Start the namespace.  */
10073   push_namespace (identifier);
10074   /* Parse the body of the namespace.  */
10075   cp_parser_namespace_body (parser);
10076   /* Finish the namespace.  */
10077   pop_namespace ();
10078   /* Look for the final `}'.  */
10079   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10080 }
10081
10082 /* Parse a namespace-body.
10083
10084    namespace-body:
10085      declaration-seq [opt]  */
10086
10087 static void
10088 cp_parser_namespace_body (cp_parser* parser)
10089 {
10090   cp_parser_declaration_seq_opt (parser);
10091 }
10092
10093 /* Parse a namespace-alias-definition.
10094
10095    namespace-alias-definition:
10096      namespace identifier = qualified-namespace-specifier ;  */
10097
10098 static void
10099 cp_parser_namespace_alias_definition (cp_parser* parser)
10100 {
10101   tree identifier;
10102   tree namespace_specifier;
10103
10104   /* Look for the `namespace' keyword.  */
10105   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10106   /* Look for the identifier.  */
10107   identifier = cp_parser_identifier (parser);
10108   if (identifier == error_mark_node)
10109     return;
10110   /* Look for the `=' token.  */
10111   cp_parser_require (parser, CPP_EQ, "`='");
10112   /* Look for the qualified-namespace-specifier.  */
10113   namespace_specifier
10114     = cp_parser_qualified_namespace_specifier (parser);
10115   /* Look for the `;' token.  */
10116   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10117
10118   /* Register the alias in the symbol table.  */
10119   do_namespace_alias (identifier, namespace_specifier);
10120 }
10121
10122 /* Parse a qualified-namespace-specifier.
10123
10124    qualified-namespace-specifier:
10125      :: [opt] nested-name-specifier [opt] namespace-name
10126
10127    Returns a NAMESPACE_DECL corresponding to the specified
10128    namespace.  */
10129
10130 static tree
10131 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10132 {
10133   /* Look for the optional `::'.  */
10134   cp_parser_global_scope_opt (parser,
10135                               /*current_scope_valid_p=*/false);
10136
10137   /* Look for the optional nested-name-specifier.  */
10138   cp_parser_nested_name_specifier_opt (parser,
10139                                        /*typename_keyword_p=*/false,
10140                                        /*check_dependency_p=*/true,
10141                                        /*type_p=*/false,
10142                                        /*is_declaration=*/true);
10143
10144   return cp_parser_namespace_name (parser);
10145 }
10146
10147 /* Parse a using-declaration.
10148
10149    using-declaration:
10150      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10151      using :: unqualified-id ;  */
10152
10153 static void
10154 cp_parser_using_declaration (cp_parser* parser)
10155 {
10156   cp_token *token;
10157   bool typename_p = false;
10158   bool global_scope_p;
10159   tree decl;
10160   tree identifier;
10161   tree scope;
10162   tree qscope;
10163
10164   /* Look for the `using' keyword.  */
10165   cp_parser_require_keyword (parser, RID_USING, "`using'");
10166
10167   /* Peek at the next token.  */
10168   token = cp_lexer_peek_token (parser->lexer);
10169   /* See if it's `typename'.  */
10170   if (token->keyword == RID_TYPENAME)
10171     {
10172       /* Remember that we've seen it.  */
10173       typename_p = true;
10174       /* Consume the `typename' token.  */
10175       cp_lexer_consume_token (parser->lexer);
10176     }
10177
10178   /* Look for the optional global scope qualification.  */
10179   global_scope_p
10180     = (cp_parser_global_scope_opt (parser,
10181                                    /*current_scope_valid_p=*/false)
10182        != NULL_TREE);
10183
10184   /* If we saw `typename', or didn't see `::', then there must be a
10185      nested-name-specifier present.  */
10186   if (typename_p || !global_scope_p)
10187     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10188                                               /*check_dependency_p=*/true,
10189                                               /*type_p=*/false,
10190                                               /*is_declaration=*/true);
10191   /* Otherwise, we could be in either of the two productions.  In that
10192      case, treat the nested-name-specifier as optional.  */
10193   else
10194     qscope = cp_parser_nested_name_specifier_opt (parser,
10195                                                   /*typename_keyword_p=*/false,
10196                                                   /*check_dependency_p=*/true,
10197                                                   /*type_p=*/false,
10198                                                   /*is_declaration=*/true);
10199   if (!qscope)
10200     qscope = global_namespace;
10201
10202   /* Parse the unqualified-id.  */
10203   identifier = cp_parser_unqualified_id (parser,
10204                                          /*template_keyword_p=*/false,
10205                                          /*check_dependency_p=*/true,
10206                                          /*declarator_p=*/true);
10207
10208   /* The function we call to handle a using-declaration is different
10209      depending on what scope we are in.  */
10210   if (identifier == error_mark_node)
10211     ;
10212   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10213            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10214     /* [namespace.udecl]
10215
10216        A using declaration shall not name a template-id.  */
10217     error ("a template-id may not appear in a using-declaration");
10218   else
10219     {
10220       scope = current_scope ();
10221       if (scope && TYPE_P (scope))
10222         {
10223           /* Create the USING_DECL.  */
10224           decl = do_class_using_decl (build_nt (SCOPE_REF,
10225                                                 parser->scope,
10226                                                 identifier));
10227           /* Add it to the list of members in this class.  */
10228           finish_member_declaration (decl);
10229         }
10230       else
10231         {
10232           decl = cp_parser_lookup_name_simple (parser, identifier);
10233           if (decl == error_mark_node)
10234             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10235           else if (scope)
10236             do_local_using_decl (decl, qscope, identifier);
10237           else
10238             do_toplevel_using_decl (decl, qscope, identifier);
10239         }
10240     }
10241
10242   /* Look for the final `;'.  */
10243   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10244 }
10245
10246 /* Parse a using-directive.
10247
10248    using-directive:
10249      using namespace :: [opt] nested-name-specifier [opt]
10250        namespace-name ;  */
10251
10252 static void
10253 cp_parser_using_directive (cp_parser* parser)
10254 {
10255   tree namespace_decl;
10256   tree attribs;
10257
10258   /* Look for the `using' keyword.  */
10259   cp_parser_require_keyword (parser, RID_USING, "`using'");
10260   /* And the `namespace' keyword.  */
10261   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10262   /* Look for the optional `::' operator.  */
10263   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10264   /* And the optional nested-name-specifier.  */
10265   cp_parser_nested_name_specifier_opt (parser,
10266                                        /*typename_keyword_p=*/false,
10267                                        /*check_dependency_p=*/true,
10268                                        /*type_p=*/false,
10269                                        /*is_declaration=*/true);
10270   /* Get the namespace being used.  */
10271   namespace_decl = cp_parser_namespace_name (parser);
10272   /* And any specified attributes.  */
10273   attribs = cp_parser_attributes_opt (parser);
10274   /* Update the symbol table.  */
10275   parse_using_directive (namespace_decl, attribs);
10276   /* Look for the final `;'.  */
10277   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10278 }
10279
10280 /* Parse an asm-definition.
10281
10282    asm-definition:
10283      asm ( string-literal ) ;
10284
10285    GNU Extension:
10286
10287    asm-definition:
10288      asm volatile [opt] ( string-literal ) ;
10289      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10290      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10291                           : asm-operand-list [opt] ) ;
10292      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10293                           : asm-operand-list [opt]
10294                           : asm-operand-list [opt] ) ;  */
10295
10296 static void
10297 cp_parser_asm_definition (cp_parser* parser)
10298 {
10299   cp_token *token;
10300   tree string;
10301   tree outputs = NULL_TREE;
10302   tree inputs = NULL_TREE;
10303   tree clobbers = NULL_TREE;
10304   tree asm_stmt;
10305   bool volatile_p = false;
10306   bool extended_p = false;
10307
10308   /* Look for the `asm' keyword.  */
10309   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10310   /* See if the next token is `volatile'.  */
10311   if (cp_parser_allow_gnu_extensions_p (parser)
10312       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10313     {
10314       /* Remember that we saw the `volatile' keyword.  */
10315       volatile_p = true;
10316       /* Consume the token.  */
10317       cp_lexer_consume_token (parser->lexer);
10318     }
10319   /* Look for the opening `('.  */
10320   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10321   /* Look for the string.  */
10322   c_lex_string_translate = 0;
10323   token = cp_parser_require (parser, CPP_STRING, "asm body");
10324   if (!token)
10325     goto finish;
10326   string = token->value;
10327   /* If we're allowing GNU extensions, check for the extended assembly
10328      syntax.  Unfortunately, the `:' tokens need not be separated by
10329      a space in C, and so, for compatibility, we tolerate that here
10330      too.  Doing that means that we have to treat the `::' operator as
10331      two `:' tokens.  */
10332   if (cp_parser_allow_gnu_extensions_p (parser)
10333       && at_function_scope_p ()
10334       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10335           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10336     {
10337       bool inputs_p = false;
10338       bool clobbers_p = false;
10339
10340       /* The extended syntax was used.  */
10341       extended_p = true;
10342
10343       /* Look for outputs.  */
10344       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10345         {
10346           /* Consume the `:'.  */
10347           cp_lexer_consume_token (parser->lexer);
10348           /* Parse the output-operands.  */
10349           if (cp_lexer_next_token_is_not (parser->lexer,
10350                                           CPP_COLON)
10351               && cp_lexer_next_token_is_not (parser->lexer,
10352                                              CPP_SCOPE)
10353               && cp_lexer_next_token_is_not (parser->lexer,
10354                                              CPP_CLOSE_PAREN))
10355             outputs = cp_parser_asm_operand_list (parser);
10356         }
10357       /* If the next token is `::', there are no outputs, and the
10358          next token is the beginning of the inputs.  */
10359       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10360         {
10361           /* Consume the `::' token.  */
10362           cp_lexer_consume_token (parser->lexer);
10363           /* The inputs are coming next.  */
10364           inputs_p = true;
10365         }
10366
10367       /* Look for inputs.  */
10368       if (inputs_p
10369           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10370         {
10371           if (!inputs_p)
10372             /* Consume the `:'.  */
10373             cp_lexer_consume_token (parser->lexer);
10374           /* Parse the output-operands.  */
10375           if (cp_lexer_next_token_is_not (parser->lexer,
10376                                           CPP_COLON)
10377               && cp_lexer_next_token_is_not (parser->lexer,
10378                                              CPP_SCOPE)
10379               && cp_lexer_next_token_is_not (parser->lexer,
10380                                              CPP_CLOSE_PAREN))
10381             inputs = cp_parser_asm_operand_list (parser);
10382         }
10383       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10384         /* The clobbers are coming next.  */
10385         clobbers_p = true;
10386
10387       /* Look for clobbers.  */
10388       if (clobbers_p
10389           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10390         {
10391           if (!clobbers_p)
10392             /* Consume the `:'.  */
10393             cp_lexer_consume_token (parser->lexer);
10394           /* Parse the clobbers.  */
10395           if (cp_lexer_next_token_is_not (parser->lexer,
10396                                           CPP_CLOSE_PAREN))
10397             clobbers = cp_parser_asm_clobber_list (parser);
10398         }
10399     }
10400   /* Look for the closing `)'.  */
10401   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10402     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10403                                            /*consume_paren=*/true);
10404   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10405
10406   /* Create the ASM_EXPR.  */
10407   if (at_function_scope_p ())
10408     {
10409       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10410                                   inputs, clobbers);
10411       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10412       if (!extended_p)
10413         ASM_INPUT_P (asm_stmt) = 1;
10414     }
10415   else
10416     assemble_asm (string);
10417
10418  finish:
10419   c_lex_string_translate = 1;
10420 }
10421
10422 /* Declarators [gram.dcl.decl] */
10423
10424 /* Parse an init-declarator.
10425
10426    init-declarator:
10427      declarator initializer [opt]
10428
10429    GNU Extension:
10430
10431    init-declarator:
10432      declarator asm-specification [opt] attributes [opt] initializer [opt]
10433
10434    function-definition:
10435      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10436        function-body
10437      decl-specifier-seq [opt] declarator function-try-block
10438
10439    GNU Extension:
10440
10441    function-definition:
10442      __extension__ function-definition
10443
10444    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10445    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10446    then this declarator appears in a class scope.  The new DECL created
10447    by this declarator is returned.
10448
10449    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10450    for a function-definition here as well.  If the declarator is a
10451    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10452    be TRUE upon return.  By that point, the function-definition will
10453    have been completely parsed.
10454
10455    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10456    is FALSE.  */
10457
10458 static tree
10459 cp_parser_init_declarator (cp_parser* parser,
10460                            cp_decl_specifier_seq *decl_specifiers,
10461                            bool function_definition_allowed_p,
10462                            bool member_p,
10463                            int declares_class_or_enum,
10464                            bool* function_definition_p)
10465 {
10466   cp_token *token;
10467   cp_declarator *declarator;
10468   tree prefix_attributes;
10469   tree attributes;
10470   tree asm_specification;
10471   tree initializer;
10472   tree decl = NULL_TREE;
10473   tree scope;
10474   bool is_initialized;
10475   bool is_parenthesized_init;
10476   bool is_non_constant_init;
10477   int ctor_dtor_or_conv_p;
10478   bool friend_p;
10479   bool pop_p = false;
10480
10481   /* Gather the attributes that were provided with the
10482      decl-specifiers.  */
10483   prefix_attributes = decl_specifiers->attributes;
10484   decl_specifiers->attributes = NULL_TREE;
10485
10486   /* Assume that this is not the declarator for a function
10487      definition.  */
10488   if (function_definition_p)
10489     *function_definition_p = false;
10490
10491   /* Defer access checks while parsing the declarator; we cannot know
10492      what names are accessible until we know what is being
10493      declared.  */
10494   resume_deferring_access_checks ();
10495
10496   /* Parse the declarator.  */
10497   declarator
10498     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10499                             &ctor_dtor_or_conv_p,
10500                             /*parenthesized_p=*/NULL);
10501   /* Gather up the deferred checks.  */
10502   stop_deferring_access_checks ();
10503
10504   /* If the DECLARATOR was erroneous, there's no need to go
10505      further.  */
10506   if (declarator == cp_error_declarator)
10507     return error_mark_node;
10508
10509   cp_parser_check_for_definition_in_return_type (declarator,
10510                                                  declares_class_or_enum);
10511
10512   /* Figure out what scope the entity declared by the DECLARATOR is
10513      located in.  `grokdeclarator' sometimes changes the scope, so
10514      we compute it now.  */
10515   scope = get_scope_of_declarator (declarator);
10516
10517   /* If we're allowing GNU extensions, look for an asm-specification
10518      and attributes.  */
10519   if (cp_parser_allow_gnu_extensions_p (parser))
10520     {
10521       /* Look for an asm-specification.  */
10522       asm_specification = cp_parser_asm_specification_opt (parser);
10523       /* And attributes.  */
10524       attributes = cp_parser_attributes_opt (parser);
10525     }
10526   else
10527     {
10528       asm_specification = NULL_TREE;
10529       attributes = NULL_TREE;
10530     }
10531
10532   /* Peek at the next token.  */
10533   token = cp_lexer_peek_token (parser->lexer);
10534   /* Check to see if the token indicates the start of a
10535      function-definition.  */
10536   if (cp_parser_token_starts_function_definition_p (token))
10537     {
10538       if (!function_definition_allowed_p)
10539         {
10540           /* If a function-definition should not appear here, issue an
10541              error message.  */
10542           cp_parser_error (parser,
10543                            "a function-definition is not allowed here");
10544           return error_mark_node;
10545         }
10546       else
10547         {
10548           /* Neither attributes nor an asm-specification are allowed
10549              on a function-definition.  */
10550           if (asm_specification)
10551             error ("an asm-specification is not allowed on a function-definition");
10552           if (attributes)
10553             error ("attributes are not allowed on a function-definition");
10554           /* This is a function-definition.  */
10555           *function_definition_p = true;
10556
10557           /* Parse the function definition.  */
10558           if (member_p)
10559             decl = cp_parser_save_member_function_body (parser,
10560                                                         decl_specifiers,
10561                                                         declarator,
10562                                                         prefix_attributes);
10563           else
10564             decl
10565               = (cp_parser_function_definition_from_specifiers_and_declarator
10566                  (parser, decl_specifiers, prefix_attributes, declarator));
10567
10568           return decl;
10569         }
10570     }
10571
10572   /* [dcl.dcl]
10573
10574      Only in function declarations for constructors, destructors, and
10575      type conversions can the decl-specifier-seq be omitted.
10576
10577      We explicitly postpone this check past the point where we handle
10578      function-definitions because we tolerate function-definitions
10579      that are missing their return types in some modes.  */
10580   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10581     {
10582       cp_parser_error (parser,
10583                        "expected constructor, destructor, or type conversion");
10584       return error_mark_node;
10585     }
10586
10587   /* An `=' or an `(' indicates an initializer.  */
10588   is_initialized = (token->type == CPP_EQ
10589                      || token->type == CPP_OPEN_PAREN);
10590   /* If the init-declarator isn't initialized and isn't followed by a
10591      `,' or `;', it's not a valid init-declarator.  */
10592   if (!is_initialized
10593       && token->type != CPP_COMMA
10594       && token->type != CPP_SEMICOLON)
10595     {
10596       cp_parser_error (parser, "expected init-declarator");
10597       return error_mark_node;
10598     }
10599
10600   /* Because start_decl has side-effects, we should only call it if we
10601      know we're going ahead.  By this point, we know that we cannot
10602      possibly be looking at any other construct.  */
10603   cp_parser_commit_to_tentative_parse (parser);
10604
10605   /* If the decl specifiers were bad, issue an error now that we're
10606      sure this was intended to be a declarator.  Then continue
10607      declaring the variable(s), as int, to try to cut down on further
10608      errors.  */
10609   if (decl_specifiers->any_specifiers_p
10610       && decl_specifiers->type == error_mark_node)
10611     {
10612       cp_parser_error (parser, "invalid type in declaration");
10613       decl_specifiers->type = integer_type_node;
10614     }
10615
10616   /* Check to see whether or not this declaration is a friend.  */
10617   friend_p = cp_parser_friend_p (decl_specifiers);
10618
10619   /* Check that the number of template-parameter-lists is OK.  */
10620   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10621     return error_mark_node;
10622
10623   /* Enter the newly declared entry in the symbol table.  If we're
10624      processing a declaration in a class-specifier, we wait until
10625      after processing the initializer.  */
10626   if (!member_p)
10627     {
10628       if (parser->in_unbraced_linkage_specification_p)
10629         {
10630           decl_specifiers->storage_class = sc_extern;
10631           have_extern_spec = false;
10632         }
10633       decl = start_decl (declarator, decl_specifiers,
10634                          is_initialized, attributes, prefix_attributes);
10635     }
10636
10637   /* Enter the SCOPE.  That way unqualified names appearing in the
10638      initializer will be looked up in SCOPE.  */
10639   if (scope)
10640     pop_p = push_scope (scope);
10641
10642   /* Perform deferred access control checks, now that we know in which
10643      SCOPE the declared entity resides.  */
10644   if (!member_p && decl)
10645     {
10646       tree saved_current_function_decl = NULL_TREE;
10647
10648       /* If the entity being declared is a function, pretend that we
10649          are in its scope.  If it is a `friend', it may have access to
10650          things that would not otherwise be accessible.  */
10651       if (TREE_CODE (decl) == FUNCTION_DECL)
10652         {
10653           saved_current_function_decl = current_function_decl;
10654           current_function_decl = decl;
10655         }
10656
10657       /* Perform the access control checks for the declarator and the
10658          the decl-specifiers.  */
10659       perform_deferred_access_checks ();
10660
10661       /* Restore the saved value.  */
10662       if (TREE_CODE (decl) == FUNCTION_DECL)
10663         current_function_decl = saved_current_function_decl;
10664     }
10665
10666   /* Parse the initializer.  */
10667   if (is_initialized)
10668     initializer = cp_parser_initializer (parser,
10669                                          &is_parenthesized_init,
10670                                          &is_non_constant_init);
10671   else
10672     {
10673       initializer = NULL_TREE;
10674       is_parenthesized_init = false;
10675       is_non_constant_init = true;
10676     }
10677
10678   /* The old parser allows attributes to appear after a parenthesized
10679      initializer.  Mark Mitchell proposed removing this functionality
10680      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10681      attributes -- but ignores them.  */
10682   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10683     if (cp_parser_attributes_opt (parser))
10684       warning ("attributes after parenthesized initializer ignored");
10685
10686   /* Leave the SCOPE, now that we have processed the initializer.  It
10687      is important to do this before calling cp_finish_decl because it
10688      makes decisions about whether to create DECL_EXPRs or not based
10689      on the current scope.  */
10690   if (pop_p)
10691     pop_scope (scope);
10692
10693   /* For an in-class declaration, use `grokfield' to create the
10694      declaration.  */
10695   if (member_p)
10696     {
10697       decl = grokfield (declarator, decl_specifiers,
10698                         initializer, /*asmspec=*/NULL_TREE,
10699                         /*attributes=*/NULL_TREE);
10700       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10701         cp_parser_save_default_args (parser, decl);
10702     }
10703
10704   /* Finish processing the declaration.  But, skip friend
10705      declarations.  */
10706   if (!friend_p && decl)
10707     cp_finish_decl (decl,
10708                     initializer,
10709                     asm_specification,
10710                     /* If the initializer is in parentheses, then this is
10711                        a direct-initialization, which means that an
10712                        `explicit' constructor is OK.  Otherwise, an
10713                        `explicit' constructor cannot be used.  */
10714                     ((is_parenthesized_init || !is_initialized)
10715                      ? 0 : LOOKUP_ONLYCONVERTING));
10716
10717   /* Remember whether or not variables were initialized by
10718      constant-expressions.  */
10719   if (decl && TREE_CODE (decl) == VAR_DECL
10720       && is_initialized && !is_non_constant_init)
10721     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10722
10723   return decl;
10724 }
10725
10726 /* Parse a declarator.
10727
10728    declarator:
10729      direct-declarator
10730      ptr-operator declarator
10731
10732    abstract-declarator:
10733      ptr-operator abstract-declarator [opt]
10734      direct-abstract-declarator
10735
10736    GNU Extensions:
10737
10738    declarator:
10739      attributes [opt] direct-declarator
10740      attributes [opt] ptr-operator declarator
10741
10742    abstract-declarator:
10743      attributes [opt] ptr-operator abstract-declarator [opt]
10744      attributes [opt] direct-abstract-declarator
10745
10746    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10747    detect constructor, destructor or conversion operators. It is set
10748    to -1 if the declarator is a name, and +1 if it is a
10749    function. Otherwise it is set to zero. Usually you just want to
10750    test for >0, but internally the negative value is used.
10751
10752    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10753    a decl-specifier-seq unless it declares a constructor, destructor,
10754    or conversion.  It might seem that we could check this condition in
10755    semantic analysis, rather than parsing, but that makes it difficult
10756    to handle something like `f()'.  We want to notice that there are
10757    no decl-specifiers, and therefore realize that this is an
10758    expression, not a declaration.)
10759
10760    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10761    the declarator is a direct-declarator of the form "(...)".  */
10762
10763 static cp_declarator *
10764 cp_parser_declarator (cp_parser* parser,
10765                       cp_parser_declarator_kind dcl_kind,
10766                       int* ctor_dtor_or_conv_p,
10767                       bool* parenthesized_p)
10768 {
10769   cp_token *token;
10770   cp_declarator *declarator;
10771   enum tree_code code;
10772   cp_cv_quals cv_quals;
10773   tree class_type;
10774   tree attributes = NULL_TREE;
10775
10776   /* Assume this is not a constructor, destructor, or type-conversion
10777      operator.  */
10778   if (ctor_dtor_or_conv_p)
10779     *ctor_dtor_or_conv_p = 0;
10780
10781   if (cp_parser_allow_gnu_extensions_p (parser))
10782     attributes = cp_parser_attributes_opt (parser);
10783
10784   /* Peek at the next token.  */
10785   token = cp_lexer_peek_token (parser->lexer);
10786
10787   /* Check for the ptr-operator production.  */
10788   cp_parser_parse_tentatively (parser);
10789   /* Parse the ptr-operator.  */
10790   code = cp_parser_ptr_operator (parser,
10791                                  &class_type,
10792                                  &cv_quals);
10793   /* If that worked, then we have a ptr-operator.  */
10794   if (cp_parser_parse_definitely (parser))
10795     {
10796       /* If a ptr-operator was found, then this declarator was not
10797          parenthesized.  */
10798       if (parenthesized_p)
10799         *parenthesized_p = true;
10800       /* The dependent declarator is optional if we are parsing an
10801          abstract-declarator.  */
10802       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10803         cp_parser_parse_tentatively (parser);
10804
10805       /* Parse the dependent declarator.  */
10806       declarator = cp_parser_declarator (parser, dcl_kind,
10807                                          /*ctor_dtor_or_conv_p=*/NULL,
10808                                          /*parenthesized_p=*/NULL);
10809
10810       /* If we are parsing an abstract-declarator, we must handle the
10811          case where the dependent declarator is absent.  */
10812       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10813           && !cp_parser_parse_definitely (parser))
10814         declarator = NULL;
10815
10816       /* Build the representation of the ptr-operator.  */
10817       if (class_type)
10818         declarator = make_ptrmem_declarator (cv_quals,
10819                                              class_type,
10820                                              declarator);
10821       else if (code == INDIRECT_REF)
10822         declarator = make_pointer_declarator (cv_quals, declarator);
10823       else
10824         declarator = make_reference_declarator (cv_quals, declarator);
10825     }
10826   /* Everything else is a direct-declarator.  */
10827   else
10828     {
10829       if (parenthesized_p)
10830         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10831                                                    CPP_OPEN_PAREN);
10832       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10833                                                 ctor_dtor_or_conv_p);
10834     }
10835
10836   if (attributes && declarator != cp_error_declarator)
10837     declarator->attributes = attributes;
10838
10839   return declarator;
10840 }
10841
10842 /* Parse a direct-declarator or direct-abstract-declarator.
10843
10844    direct-declarator:
10845      declarator-id
10846      direct-declarator ( parameter-declaration-clause )
10847        cv-qualifier-seq [opt]
10848        exception-specification [opt]
10849      direct-declarator [ constant-expression [opt] ]
10850      ( declarator )
10851
10852    direct-abstract-declarator:
10853      direct-abstract-declarator [opt]
10854        ( parameter-declaration-clause )
10855        cv-qualifier-seq [opt]
10856        exception-specification [opt]
10857      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10858      ( abstract-declarator )
10859
10860    Returns a representation of the declarator.  DCL_KIND is
10861    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10862    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10863    we are parsing a direct-declarator.  It is
10864    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10865    of ambiguity we prefer an abstract declarator, as per
10866    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10867    cp_parser_declarator.  */
10868
10869 static cp_declarator *
10870 cp_parser_direct_declarator (cp_parser* parser,
10871                              cp_parser_declarator_kind dcl_kind,
10872                              int* ctor_dtor_or_conv_p)
10873 {
10874   cp_token *token;
10875   cp_declarator *declarator = NULL;
10876   tree scope = NULL_TREE;
10877   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10878   bool saved_in_declarator_p = parser->in_declarator_p;
10879   bool first = true;
10880   bool pop_p = false;
10881
10882   while (true)
10883     {
10884       /* Peek at the next token.  */
10885       token = cp_lexer_peek_token (parser->lexer);
10886       if (token->type == CPP_OPEN_PAREN)
10887         {
10888           /* This is either a parameter-declaration-clause, or a
10889              parenthesized declarator. When we know we are parsing a
10890              named declarator, it must be a parenthesized declarator
10891              if FIRST is true. For instance, `(int)' is a
10892              parameter-declaration-clause, with an omitted
10893              direct-abstract-declarator. But `((*))', is a
10894              parenthesized abstract declarator. Finally, when T is a
10895              template parameter `(T)' is a
10896              parameter-declaration-clause, and not a parenthesized
10897              named declarator.
10898
10899              We first try and parse a parameter-declaration-clause,
10900              and then try a nested declarator (if FIRST is true).
10901
10902              It is not an error for it not to be a
10903              parameter-declaration-clause, even when FIRST is
10904              false. Consider,
10905
10906                int i (int);
10907                int i (3);
10908
10909              The first is the declaration of a function while the
10910              second is a the definition of a variable, including its
10911              initializer.
10912
10913              Having seen only the parenthesis, we cannot know which of
10914              these two alternatives should be selected.  Even more
10915              complex are examples like:
10916
10917                int i (int (a));
10918                int i (int (3));
10919
10920              The former is a function-declaration; the latter is a
10921              variable initialization.
10922
10923              Thus again, we try a parameter-declaration-clause, and if
10924              that fails, we back out and return.  */
10925
10926           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10927             {
10928               cp_parameter_declarator *params;
10929               unsigned saved_num_template_parameter_lists;
10930
10931               cp_parser_parse_tentatively (parser);
10932
10933               /* Consume the `('.  */
10934               cp_lexer_consume_token (parser->lexer);
10935               if (first)
10936                 {
10937                   /* If this is going to be an abstract declarator, we're
10938                      in a declarator and we can't have default args.  */
10939                   parser->default_arg_ok_p = false;
10940                   parser->in_declarator_p = true;
10941                 }
10942
10943               /* Inside the function parameter list, surrounding
10944                  template-parameter-lists do not apply.  */
10945               saved_num_template_parameter_lists
10946                 = parser->num_template_parameter_lists;
10947               parser->num_template_parameter_lists = 0;
10948
10949               /* Parse the parameter-declaration-clause.  */
10950               params = cp_parser_parameter_declaration_clause (parser);
10951
10952               parser->num_template_parameter_lists
10953                 = saved_num_template_parameter_lists;
10954
10955               /* If all went well, parse the cv-qualifier-seq and the
10956                  exception-specification.  */
10957               if (cp_parser_parse_definitely (parser))
10958                 {
10959                   cp_cv_quals cv_quals;
10960                   tree exception_specification;
10961
10962                   if (ctor_dtor_or_conv_p)
10963                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10964                   first = false;
10965                   /* Consume the `)'.  */
10966                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10967
10968                   /* Parse the cv-qualifier-seq.  */
10969                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10970                   /* And the exception-specification.  */
10971                   exception_specification
10972                     = cp_parser_exception_specification_opt (parser);
10973
10974                   /* Create the function-declarator.  */
10975                   declarator = make_call_declarator (declarator,
10976                                                      params,
10977                                                      cv_quals,
10978                                                      exception_specification);
10979                   /* Any subsequent parameter lists are to do with
10980                      return type, so are not those of the declared
10981                      function.  */
10982                   parser->default_arg_ok_p = false;
10983
10984                   /* Repeat the main loop.  */
10985                   continue;
10986                 }
10987             }
10988
10989           /* If this is the first, we can try a parenthesized
10990              declarator.  */
10991           if (first)
10992             {
10993               bool saved_in_type_id_in_expr_p;
10994
10995               parser->default_arg_ok_p = saved_default_arg_ok_p;
10996               parser->in_declarator_p = saved_in_declarator_p;
10997
10998               /* Consume the `('.  */
10999               cp_lexer_consume_token (parser->lexer);
11000               /* Parse the nested declarator.  */
11001               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11002               parser->in_type_id_in_expr_p = true;
11003               declarator
11004                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11005                                         /*parenthesized_p=*/NULL);
11006               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11007               first = false;
11008               /* Expect a `)'.  */
11009               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11010                 declarator = cp_error_declarator;
11011               if (declarator == cp_error_declarator)
11012                 break;
11013
11014               goto handle_declarator;
11015             }
11016           /* Otherwise, we must be done.  */
11017           else
11018             break;
11019         }
11020       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11021                && token->type == CPP_OPEN_SQUARE)
11022         {
11023           /* Parse an array-declarator.  */
11024           tree bounds;
11025
11026           if (ctor_dtor_or_conv_p)
11027             *ctor_dtor_or_conv_p = 0;
11028
11029           first = false;
11030           parser->default_arg_ok_p = false;
11031           parser->in_declarator_p = true;
11032           /* Consume the `['.  */
11033           cp_lexer_consume_token (parser->lexer);
11034           /* Peek at the next token.  */
11035           token = cp_lexer_peek_token (parser->lexer);
11036           /* If the next token is `]', then there is no
11037              constant-expression.  */
11038           if (token->type != CPP_CLOSE_SQUARE)
11039             {
11040               bool non_constant_p;
11041
11042               bounds
11043                 = cp_parser_constant_expression (parser,
11044                                                  /*allow_non_constant=*/true,
11045                                                  &non_constant_p);
11046               if (!non_constant_p)
11047                 bounds = fold_non_dependent_expr (bounds);
11048             }
11049           else
11050             bounds = NULL_TREE;
11051           /* Look for the closing `]'.  */
11052           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11053             {
11054               declarator = cp_error_declarator;
11055               break;
11056             }
11057
11058           declarator = make_array_declarator (declarator, bounds);
11059         }
11060       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11061         {
11062           tree id;
11063
11064           /* Parse a declarator-id */
11065           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11066             cp_parser_parse_tentatively (parser);
11067           id = cp_parser_declarator_id (parser);
11068           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11069             {
11070               if (!cp_parser_parse_definitely (parser))
11071                 id = error_mark_node;
11072               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11073                 {
11074                   cp_parser_error (parser, "expected unqualified-id");
11075                   id = error_mark_node;
11076                 }
11077             }
11078
11079           if (id == error_mark_node)
11080             {
11081               declarator = cp_error_declarator;
11082               break;
11083             }
11084
11085           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11086             {
11087               tree scope = TREE_OPERAND (id, 0);
11088
11089               /* In the declaration of a member of a template class
11090                  outside of the class itself, the SCOPE will sometimes
11091                  be a TYPENAME_TYPE.  For example, given:
11092
11093                  template <typename T>
11094                  int S<T>::R::i = 3;
11095
11096                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11097                  this context, we must resolve S<T>::R to an ordinary
11098                  type, rather than a typename type.
11099
11100                  The reason we normally avoid resolving TYPENAME_TYPEs
11101                  is that a specialization of `S' might render
11102                  `S<T>::R' not a type.  However, if `S' is
11103                  specialized, then this `i' will not be used, so there
11104                  is no harm in resolving the types here.  */
11105               if (TREE_CODE (scope) == TYPENAME_TYPE)
11106                 {
11107                   tree type;
11108
11109                   /* Resolve the TYPENAME_TYPE.  */
11110                   type = resolve_typename_type (scope,
11111                                                  /*only_current_p=*/false);
11112                   /* If that failed, the declarator is invalid.  */
11113                   if (type == error_mark_node)
11114                     error ("`%T::%D' is not a type",
11115                            TYPE_CONTEXT (scope),
11116                            TYPE_IDENTIFIER (scope));
11117                   /* Build a new DECLARATOR.  */
11118                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11119                 }
11120             }
11121
11122           declarator = make_id_declarator (id);
11123           if (id)
11124             {
11125               tree class_type;
11126               tree unqualified_name;
11127
11128               if (TREE_CODE (id) == SCOPE_REF
11129                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11130                 {
11131                   class_type = TREE_OPERAND (id, 0);
11132                   unqualified_name = TREE_OPERAND (id, 1);
11133                 }
11134               else
11135                 {
11136                   class_type = current_class_type;
11137                   unqualified_name = id;
11138                 }
11139
11140               if (class_type)
11141                 {
11142                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11143                     declarator->u.id.sfk = sfk_destructor;
11144                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11145                     declarator->u.id.sfk = sfk_conversion;
11146                   else if (constructor_name_p (unqualified_name,
11147                                                class_type)
11148                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11149                                && same_type_p (TREE_TYPE (unqualified_name),
11150                                                class_type)))
11151                     declarator->u.id.sfk = sfk_constructor;
11152
11153                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11154                     *ctor_dtor_or_conv_p = -1;
11155                   if (TREE_CODE (id) == SCOPE_REF
11156                       && TREE_CODE (unqualified_name) == TYPE_DECL 
11157                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11158                     {
11159                       error ("invalid use of constructor as a template");
11160                       inform ("use `%T::%D' instead of `%T::%T' to name the "
11161                               "constructor in a qualified name", class_type, 
11162                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11163                               class_type, class_type);
11164                     }
11165                 }
11166             }
11167
11168         handle_declarator:;
11169           scope = get_scope_of_declarator (declarator);
11170           if (scope)
11171             /* Any names that appear after the declarator-id for a
11172                member are looked up in the containing scope.  */
11173             pop_p = push_scope (scope);
11174           parser->in_declarator_p = true;
11175           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11176               || (declarator && declarator->kind == cdk_id))
11177             /* Default args are only allowed on function
11178                declarations.  */
11179             parser->default_arg_ok_p = saved_default_arg_ok_p;
11180           else
11181             parser->default_arg_ok_p = false;
11182
11183           first = false;
11184         }
11185       /* We're done.  */
11186       else
11187         break;
11188     }
11189
11190   /* For an abstract declarator, we might wind up with nothing at this
11191      point.  That's an error; the declarator is not optional.  */
11192   if (!declarator)
11193     cp_parser_error (parser, "expected declarator");
11194
11195   /* If we entered a scope, we must exit it now.  */
11196   if (pop_p)
11197     pop_scope (scope);
11198
11199   parser->default_arg_ok_p = saved_default_arg_ok_p;
11200   parser->in_declarator_p = saved_in_declarator_p;
11201
11202   return declarator;
11203 }
11204
11205 /* Parse a ptr-operator.
11206
11207    ptr-operator:
11208      * cv-qualifier-seq [opt]
11209      &
11210      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11211
11212    GNU Extension:
11213
11214    ptr-operator:
11215      & cv-qualifier-seq [opt]
11216
11217    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11218    Returns ADDR_EXPR if a reference was used.  In the case of a
11219    pointer-to-member, *TYPE is filled in with the TYPE containing the
11220    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11221    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11222    ERROR_MARK if an error occurred.  */
11223
11224 static enum tree_code
11225 cp_parser_ptr_operator (cp_parser* parser,
11226                         tree* type,
11227                         cp_cv_quals *cv_quals)
11228 {
11229   enum tree_code code = ERROR_MARK;
11230   cp_token *token;
11231
11232   /* Assume that it's not a pointer-to-member.  */
11233   *type = NULL_TREE;
11234   /* And that there are no cv-qualifiers.  */
11235   *cv_quals = TYPE_UNQUALIFIED;
11236
11237   /* Peek at the next token.  */
11238   token = cp_lexer_peek_token (parser->lexer);
11239   /* If it's a `*' or `&' we have a pointer or reference.  */
11240   if (token->type == CPP_MULT || token->type == CPP_AND)
11241     {
11242       /* Remember which ptr-operator we were processing.  */
11243       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11244
11245       /* Consume the `*' or `&'.  */
11246       cp_lexer_consume_token (parser->lexer);
11247
11248       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11249          `&', if we are allowing GNU extensions.  (The only qualifier
11250          that can legally appear after `&' is `restrict', but that is
11251          enforced during semantic analysis.  */
11252       if (code == INDIRECT_REF
11253           || cp_parser_allow_gnu_extensions_p (parser))
11254         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11255     }
11256   else
11257     {
11258       /* Try the pointer-to-member case.  */
11259       cp_parser_parse_tentatively (parser);
11260       /* Look for the optional `::' operator.  */
11261       cp_parser_global_scope_opt (parser,
11262                                   /*current_scope_valid_p=*/false);
11263       /* Look for the nested-name specifier.  */
11264       cp_parser_nested_name_specifier (parser,
11265                                        /*typename_keyword_p=*/false,
11266                                        /*check_dependency_p=*/true,
11267                                        /*type_p=*/false,
11268                                        /*is_declaration=*/false);
11269       /* If we found it, and the next token is a `*', then we are
11270          indeed looking at a pointer-to-member operator.  */
11271       if (!cp_parser_error_occurred (parser)
11272           && cp_parser_require (parser, CPP_MULT, "`*'"))
11273         {
11274           /* The type of which the member is a member is given by the
11275              current SCOPE.  */
11276           *type = parser->scope;
11277           /* The next name will not be qualified.  */
11278           parser->scope = NULL_TREE;
11279           parser->qualifying_scope = NULL_TREE;
11280           parser->object_scope = NULL_TREE;
11281           /* Indicate that the `*' operator was used.  */
11282           code = INDIRECT_REF;
11283           /* Look for the optional cv-qualifier-seq.  */
11284           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11285         }
11286       /* If that didn't work we don't have a ptr-operator.  */
11287       if (!cp_parser_parse_definitely (parser))
11288         cp_parser_error (parser, "expected ptr-operator");
11289     }
11290
11291   return code;
11292 }
11293
11294 /* Parse an (optional) cv-qualifier-seq.
11295
11296    cv-qualifier-seq:
11297      cv-qualifier cv-qualifier-seq [opt]
11298
11299    cv-qualifier:
11300      const
11301      volatile
11302
11303    GNU Extension:
11304
11305    cv-qualifier:
11306      __restrict__ 
11307
11308    Returns a bitmask representing the cv-qualifiers.  */
11309
11310 static cp_cv_quals
11311 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11312 {
11313   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11314
11315   while (true)
11316     {
11317       cp_token *token;
11318       cp_cv_quals cv_qualifier;
11319       
11320       /* Peek at the next token.  */
11321       token = cp_lexer_peek_token (parser->lexer);
11322       /* See if it's a cv-qualifier.  */
11323       switch (token->keyword)
11324         {
11325         case RID_CONST:
11326           cv_qualifier = TYPE_QUAL_CONST;
11327           break;
11328           
11329         case RID_VOLATILE:
11330           cv_qualifier = TYPE_QUAL_VOLATILE;
11331           break;
11332           
11333         case RID_RESTRICT:
11334           cv_qualifier = TYPE_QUAL_RESTRICT;
11335           break;
11336           
11337         default:
11338           cv_qualifier = TYPE_UNQUALIFIED;
11339           break;
11340         }
11341       
11342       if (!cv_qualifier)
11343         break;
11344
11345       if (cv_quals & cv_qualifier)
11346         {
11347           error ("duplicate cv-qualifier");
11348           cp_lexer_purge_token (parser->lexer);
11349         }
11350       else
11351         {
11352           cp_lexer_consume_token (parser->lexer);
11353           cv_quals |= cv_qualifier;
11354         }
11355     }
11356
11357   return cv_quals;
11358 }
11359
11360 /* Parse a declarator-id.
11361
11362    declarator-id:
11363      id-expression
11364      :: [opt] nested-name-specifier [opt] type-name
11365
11366    In the `id-expression' case, the value returned is as for
11367    cp_parser_id_expression if the id-expression was an unqualified-id.
11368    If the id-expression was a qualified-id, then a SCOPE_REF is
11369    returned.  The first operand is the scope (either a NAMESPACE_DECL
11370    or TREE_TYPE), but the second is still just a representation of an
11371    unqualified-id.  */
11372
11373 static tree
11374 cp_parser_declarator_id (cp_parser* parser)
11375 {
11376   tree id_expression;
11377
11378   /* The expression must be an id-expression.  Assume that qualified
11379      names are the names of types so that:
11380
11381        template <class T>
11382        int S<T>::R::i = 3;
11383
11384      will work; we must treat `S<T>::R' as the name of a type.
11385      Similarly, assume that qualified names are templates, where
11386      required, so that:
11387
11388        template <class T>
11389        int S<T>::R<T>::i = 3;
11390
11391      will work, too.  */
11392   id_expression = cp_parser_id_expression (parser,
11393                                            /*template_keyword_p=*/false,
11394                                            /*check_dependency_p=*/false,
11395                                            /*template_p=*/NULL,
11396                                            /*declarator_p=*/true);
11397   /* If the name was qualified, create a SCOPE_REF to represent
11398      that.  */
11399   if (parser->scope)
11400     {
11401       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11402       parser->scope = NULL_TREE;
11403     }
11404
11405   return id_expression;
11406 }
11407
11408 /* Parse a type-id.
11409
11410    type-id:
11411      type-specifier-seq abstract-declarator [opt]
11412
11413    Returns the TYPE specified.  */
11414
11415 static tree
11416 cp_parser_type_id (cp_parser* parser)
11417 {
11418   cp_decl_specifier_seq type_specifier_seq;
11419   cp_declarator *abstract_declarator;
11420
11421   /* Parse the type-specifier-seq.  */
11422   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11423   if (type_specifier_seq.type == error_mark_node)
11424     return error_mark_node;
11425
11426   /* There might or might not be an abstract declarator.  */
11427   cp_parser_parse_tentatively (parser);
11428   /* Look for the declarator.  */
11429   abstract_declarator
11430     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11431                             /*parenthesized_p=*/NULL);
11432   /* Check to see if there really was a declarator.  */
11433   if (!cp_parser_parse_definitely (parser))
11434     abstract_declarator = NULL;
11435
11436   return groktypename (&type_specifier_seq, abstract_declarator);
11437 }
11438
11439 /* Parse a type-specifier-seq.
11440
11441    type-specifier-seq:
11442      type-specifier type-specifier-seq [opt]
11443
11444    GNU extension:
11445
11446    type-specifier-seq:
11447      attributes type-specifier-seq [opt]
11448
11449    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11450
11451 static void
11452 cp_parser_type_specifier_seq (cp_parser* parser,
11453                               cp_decl_specifier_seq *type_specifier_seq)
11454 {
11455   bool seen_type_specifier = false;
11456
11457   /* Clear the TYPE_SPECIFIER_SEQ.  */
11458   clear_decl_specs (type_specifier_seq);
11459
11460   /* Parse the type-specifiers and attributes.  */
11461   while (true)
11462     {
11463       tree type_specifier;
11464
11465       /* Check for attributes first.  */
11466       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11467         {
11468           type_specifier_seq->attributes = 
11469             chainon (type_specifier_seq->attributes,
11470                      cp_parser_attributes_opt (parser));
11471           continue;
11472         }
11473
11474       /* Look for the type-specifier.  */
11475       type_specifier = cp_parser_type_specifier (parser,
11476                                                  CP_PARSER_FLAGS_OPTIONAL,
11477                                                  type_specifier_seq,
11478                                                  /*is_declaration=*/false,
11479                                                  NULL,
11480                                                  NULL);
11481       /* If the first type-specifier could not be found, this is not a
11482          type-specifier-seq at all.  */
11483       if (!seen_type_specifier && !type_specifier)
11484         {
11485           cp_parser_error (parser, "expected type-specifier");
11486           type_specifier_seq->type = error_mark_node;
11487           return;
11488         }
11489       /* If subsequent type-specifiers could not be found, the
11490          type-specifier-seq is complete.  */
11491       else if (seen_type_specifier && !type_specifier)
11492         break;
11493
11494       seen_type_specifier = true;
11495     }
11496
11497   return;
11498 }
11499
11500 /* Parse a parameter-declaration-clause.
11501
11502    parameter-declaration-clause:
11503      parameter-declaration-list [opt] ... [opt]
11504      parameter-declaration-list , ...
11505
11506    Returns a representation for the parameter declarations.  A return
11507    value of NULL indicates a parameter-declaration-clause consisting
11508    only of an ellipsis.  */
11509
11510 static cp_parameter_declarator *
11511 cp_parser_parameter_declaration_clause (cp_parser* parser)
11512 {
11513   cp_parameter_declarator *parameters;
11514   cp_token *token;
11515   bool ellipsis_p;
11516   bool is_error;
11517
11518   /* Peek at the next token.  */
11519   token = cp_lexer_peek_token (parser->lexer);
11520   /* Check for trivial parameter-declaration-clauses.  */
11521   if (token->type == CPP_ELLIPSIS)
11522     {
11523       /* Consume the `...' token.  */
11524       cp_lexer_consume_token (parser->lexer);
11525       return NULL;
11526     }
11527   else if (token->type == CPP_CLOSE_PAREN)
11528     /* There are no parameters.  */
11529     {
11530 #ifndef NO_IMPLICIT_EXTERN_C
11531       if (in_system_header && current_class_type == NULL
11532           && current_lang_name == lang_name_c)
11533         return NULL;
11534       else
11535 #endif
11536         return no_parameters;
11537     }
11538   /* Check for `(void)', too, which is a special case.  */
11539   else if (token->keyword == RID_VOID
11540            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11541                == CPP_CLOSE_PAREN))
11542     {
11543       /* Consume the `void' token.  */
11544       cp_lexer_consume_token (parser->lexer);
11545       /* There are no parameters.  */
11546       return no_parameters;
11547     }
11548
11549   /* Parse the parameter-declaration-list.  */
11550   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11551   /* If a parse error occurred while parsing the
11552      parameter-declaration-list, then the entire
11553      parameter-declaration-clause is erroneous.  */
11554   if (is_error)
11555     return NULL;
11556
11557   /* Peek at the next token.  */
11558   token = cp_lexer_peek_token (parser->lexer);
11559   /* If it's a `,', the clause should terminate with an ellipsis.  */
11560   if (token->type == CPP_COMMA)
11561     {
11562       /* Consume the `,'.  */
11563       cp_lexer_consume_token (parser->lexer);
11564       /* Expect an ellipsis.  */
11565       ellipsis_p
11566         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11567     }
11568   /* It might also be `...' if the optional trailing `,' was
11569      omitted.  */
11570   else if (token->type == CPP_ELLIPSIS)
11571     {
11572       /* Consume the `...' token.  */
11573       cp_lexer_consume_token (parser->lexer);
11574       /* And remember that we saw it.  */
11575       ellipsis_p = true;
11576     }
11577   else
11578     ellipsis_p = false;
11579
11580   /* Finish the parameter list.  */
11581   if (parameters && ellipsis_p)
11582     parameters->ellipsis_p = true;
11583   
11584   return parameters;
11585 }
11586
11587 /* Parse a parameter-declaration-list.
11588
11589    parameter-declaration-list:
11590      parameter-declaration
11591      parameter-declaration-list , parameter-declaration
11592
11593    Returns a representation of the parameter-declaration-list, as for
11594    cp_parser_parameter_declaration_clause.  However, the
11595    `void_list_node' is never appended to the list.  Upon return,
11596    *IS_ERROR will be true iff an error occurred.  */
11597
11598 static cp_parameter_declarator *
11599 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11600 {
11601   cp_parameter_declarator *parameters = NULL;
11602   cp_parameter_declarator **tail = &parameters;
11603
11604   /* Assume all will go well.  */
11605   *is_error = false;
11606
11607   /* Look for more parameters.  */
11608   while (true)
11609     {
11610       cp_parameter_declarator *parameter;
11611       bool parenthesized_p;
11612       /* Parse the parameter.  */
11613       parameter
11614         = cp_parser_parameter_declaration (parser,
11615                                            /*template_parm_p=*/false,
11616                                            &parenthesized_p);
11617
11618       /* If a parse error occurred parsing the parameter declaration,
11619          then the entire parameter-declaration-list is erroneous.  */
11620       if (!parameter)
11621         {
11622           *is_error = true;
11623           parameters = NULL;
11624           break;
11625         }
11626       /* Add the new parameter to the list.  */
11627       *tail = parameter;
11628       tail = &parameter->next;
11629
11630       /* Peek at the next token.  */
11631       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11632           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11633         /* The parameter-declaration-list is complete.  */
11634         break;
11635       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11636         {
11637           cp_token *token;
11638
11639           /* Peek at the next token.  */
11640           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11641           /* If it's an ellipsis, then the list is complete.  */
11642           if (token->type == CPP_ELLIPSIS)
11643             break;
11644           /* Otherwise, there must be more parameters.  Consume the
11645              `,'.  */
11646           cp_lexer_consume_token (parser->lexer);
11647           /* When parsing something like:
11648
11649                 int i(float f, double d)
11650
11651              we can tell after seeing the declaration for "f" that we
11652              are not looking at an initialization of a variable "i",
11653              but rather at the declaration of a function "i".
11654
11655              Due to the fact that the parsing of template arguments
11656              (as specified to a template-id) requires backtracking we
11657              cannot use this technique when inside a template argument
11658              list.  */
11659           if (!parser->in_template_argument_list_p
11660               && !parser->in_type_id_in_expr_p
11661               && cp_parser_parsing_tentatively (parser)
11662               && !cp_parser_committed_to_tentative_parse (parser)
11663               /* However, a parameter-declaration of the form
11664                  "foat(f)" (which is a valid declaration of a
11665                  parameter "f") can also be interpreted as an
11666                  expression (the conversion of "f" to "float").  */
11667               && !parenthesized_p)
11668             cp_parser_commit_to_tentative_parse (parser);
11669         }
11670       else
11671         {
11672           cp_parser_error (parser, "expected `,' or `...'");
11673           if (!cp_parser_parsing_tentatively (parser)
11674               || cp_parser_committed_to_tentative_parse (parser))
11675             cp_parser_skip_to_closing_parenthesis (parser,
11676                                                    /*recovering=*/true,
11677                                                    /*or_comma=*/false,
11678                                                    /*consume_paren=*/false);
11679           break;
11680         }
11681     }
11682
11683   return parameters;
11684 }
11685
11686 /* Parse a parameter declaration.
11687
11688    parameter-declaration:
11689      decl-specifier-seq declarator
11690      decl-specifier-seq declarator = assignment-expression
11691      decl-specifier-seq abstract-declarator [opt]
11692      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11693
11694    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11695    declares a template parameter.  (In that case, a non-nested `>'
11696    token encountered during the parsing of the assignment-expression
11697    is not interpreted as a greater-than operator.)
11698
11699    Returns a representation of the parameter, or NULL if an error
11700    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11701    true iff the declarator is of the form "(p)".  */
11702
11703 static cp_parameter_declarator *
11704 cp_parser_parameter_declaration (cp_parser *parser,
11705                                  bool template_parm_p,
11706                                  bool *parenthesized_p)
11707 {
11708   int declares_class_or_enum;
11709   bool greater_than_is_operator_p;
11710   cp_decl_specifier_seq decl_specifiers;
11711   cp_declarator *declarator;
11712   tree default_argument;
11713   cp_token *token;
11714   const char *saved_message;
11715
11716   /* In a template parameter, `>' is not an operator.
11717
11718      [temp.param]
11719
11720      When parsing a default template-argument for a non-type
11721      template-parameter, the first non-nested `>' is taken as the end
11722      of the template parameter-list rather than a greater-than
11723      operator.  */
11724   greater_than_is_operator_p = !template_parm_p;
11725
11726   /* Type definitions may not appear in parameter types.  */
11727   saved_message = parser->type_definition_forbidden_message;
11728   parser->type_definition_forbidden_message
11729     = "types may not be defined in parameter types";
11730
11731   /* Parse the declaration-specifiers.  */
11732   cp_parser_decl_specifier_seq (parser,
11733                                 CP_PARSER_FLAGS_NONE,
11734                                 &decl_specifiers,
11735                                 &declares_class_or_enum);
11736   /* If an error occurred, there's no reason to attempt to parse the
11737      rest of the declaration.  */
11738   if (cp_parser_error_occurred (parser))
11739     {
11740       parser->type_definition_forbidden_message = saved_message;
11741       return NULL;
11742     }
11743
11744   /* Peek at the next token.  */
11745   token = cp_lexer_peek_token (parser->lexer);
11746   /* If the next token is a `)', `,', `=', `>', or `...', then there
11747      is no declarator.  */
11748   if (token->type == CPP_CLOSE_PAREN
11749       || token->type == CPP_COMMA
11750       || token->type == CPP_EQ
11751       || token->type == CPP_ELLIPSIS
11752       || token->type == CPP_GREATER)
11753     {
11754       declarator = NULL;
11755       if (parenthesized_p)
11756         *parenthesized_p = false;
11757     }
11758   /* Otherwise, there should be a declarator.  */
11759   else
11760     {
11761       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11762       parser->default_arg_ok_p = false;
11763
11764       /* After seeing a decl-specifier-seq, if the next token is not a
11765          "(", there is no possibility that the code is a valid
11766          expression.  Therefore, if parsing tentatively, we commit at
11767          this point.  */
11768       if (!parser->in_template_argument_list_p
11769           /* In an expression context, having seen:
11770
11771                (int((char ...
11772
11773              we cannot be sure whether we are looking at a
11774              function-type (taking a "char" as a parameter) or a cast
11775              of some object of type "char" to "int".  */
11776           && !parser->in_type_id_in_expr_p
11777           && cp_parser_parsing_tentatively (parser)
11778           && !cp_parser_committed_to_tentative_parse (parser)
11779           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11780         cp_parser_commit_to_tentative_parse (parser);
11781       /* Parse the declarator.  */
11782       declarator = cp_parser_declarator (parser,
11783                                          CP_PARSER_DECLARATOR_EITHER,
11784                                          /*ctor_dtor_or_conv_p=*/NULL,
11785                                          parenthesized_p);
11786       parser->default_arg_ok_p = saved_default_arg_ok_p;
11787       /* After the declarator, allow more attributes.  */
11788       decl_specifiers.attributes
11789         = chainon (decl_specifiers.attributes, 
11790                    cp_parser_attributes_opt (parser));
11791     }
11792
11793   /* The restriction on defining new types applies only to the type
11794      of the parameter, not to the default argument.  */
11795   parser->type_definition_forbidden_message = saved_message;
11796
11797   /* If the next token is `=', then process a default argument.  */
11798   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11799     {
11800       bool saved_greater_than_is_operator_p;
11801       /* Consume the `='.  */
11802       cp_lexer_consume_token (parser->lexer);
11803
11804       /* If we are defining a class, then the tokens that make up the
11805          default argument must be saved and processed later.  */
11806       if (!template_parm_p && at_class_scope_p ()
11807           && TYPE_BEING_DEFINED (current_class_type))
11808         {
11809           unsigned depth = 0;
11810
11811           /* Create a DEFAULT_ARG to represented the unparsed default
11812              argument.  */
11813           default_argument = make_node (DEFAULT_ARG);
11814           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11815
11816           /* Add tokens until we have processed the entire default
11817              argument.  */
11818           while (true)
11819             {
11820               bool done = false;
11821               cp_token *token;
11822
11823               /* Peek at the next token.  */
11824               token = cp_lexer_peek_token (parser->lexer);
11825               /* What we do depends on what token we have.  */
11826               switch (token->type)
11827                 {
11828                   /* In valid code, a default argument must be
11829                      immediately followed by a `,' `)', or `...'.  */
11830                 case CPP_COMMA:
11831                 case CPP_CLOSE_PAREN:
11832                 case CPP_ELLIPSIS:
11833                   /* If we run into a non-nested `;', `}', or `]',
11834                      then the code is invalid -- but the default
11835                      argument is certainly over.  */
11836                 case CPP_SEMICOLON:
11837                 case CPP_CLOSE_BRACE:
11838                 case CPP_CLOSE_SQUARE:
11839                   if (depth == 0)
11840                     done = true;
11841                   /* Update DEPTH, if necessary.  */
11842                   else if (token->type == CPP_CLOSE_PAREN
11843                            || token->type == CPP_CLOSE_BRACE
11844                            || token->type == CPP_CLOSE_SQUARE)
11845                     --depth;
11846                   break;
11847
11848                 case CPP_OPEN_PAREN:
11849                 case CPP_OPEN_SQUARE:
11850                 case CPP_OPEN_BRACE:
11851                   ++depth;
11852                   break;
11853
11854                 case CPP_GREATER:
11855                   /* If we see a non-nested `>', and `>' is not an
11856                      operator, then it marks the end of the default
11857                      argument.  */
11858                   if (!depth && !greater_than_is_operator_p)
11859                     done = true;
11860                   break;
11861
11862                   /* If we run out of tokens, issue an error message.  */
11863                 case CPP_EOF:
11864                   error ("file ends in default argument");
11865                   done = true;
11866                   break;
11867
11868                 case CPP_NAME:
11869                 case CPP_SCOPE:
11870                   /* In these cases, we should look for template-ids.
11871                      For example, if the default argument is
11872                      `X<int, double>()', we need to do name lookup to
11873                      figure out whether or not `X' is a template; if
11874                      so, the `,' does not end the default argument.
11875
11876                      That is not yet done.  */
11877                   break;
11878
11879                 default:
11880                   break;
11881                 }
11882
11883               /* If we've reached the end, stop.  */
11884               if (done)
11885                 break;
11886
11887               /* Add the token to the token block.  */
11888               token = cp_lexer_consume_token (parser->lexer);
11889               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11890                                          token);
11891             }
11892         }
11893       /* Outside of a class definition, we can just parse the
11894          assignment-expression.  */
11895       else
11896         {
11897           bool saved_local_variables_forbidden_p;
11898
11899           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11900              set correctly.  */
11901           saved_greater_than_is_operator_p
11902             = parser->greater_than_is_operator_p;
11903           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11904           /* Local variable names (and the `this' keyword) may not
11905              appear in a default argument.  */
11906           saved_local_variables_forbidden_p
11907             = parser->local_variables_forbidden_p;
11908           parser->local_variables_forbidden_p = true;
11909           /* Parse the assignment-expression.  */
11910           default_argument = cp_parser_assignment_expression (parser);
11911           /* Restore saved state.  */
11912           parser->greater_than_is_operator_p
11913             = saved_greater_than_is_operator_p;
11914           parser->local_variables_forbidden_p
11915             = saved_local_variables_forbidden_p;
11916         }
11917       if (!parser->default_arg_ok_p)
11918         {
11919           if (!flag_pedantic_errors)
11920             warning ("deprecated use of default argument for parameter of non-function");
11921           else
11922             {
11923               error ("default arguments are only permitted for function parameters");
11924               default_argument = NULL_TREE;
11925             }
11926         }
11927     }
11928   else
11929     default_argument = NULL_TREE;
11930
11931   return make_parameter_declarator (&decl_specifiers,
11932                                     declarator,
11933                                     default_argument);
11934 }
11935
11936 /* Parse a function-body.
11937
11938    function-body:
11939      compound_statement  */
11940
11941 static void
11942 cp_parser_function_body (cp_parser *parser)
11943 {
11944   cp_parser_compound_statement (parser, NULL, false);
11945 }
11946
11947 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11948    true if a ctor-initializer was present.  */
11949
11950 static bool
11951 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11952 {
11953   tree body;
11954   bool ctor_initializer_p;
11955
11956   /* Begin the function body.  */
11957   body = begin_function_body ();
11958   /* Parse the optional ctor-initializer.  */
11959   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11960   /* Parse the function-body.  */
11961   cp_parser_function_body (parser);
11962   /* Finish the function body.  */
11963   finish_function_body (body);
11964
11965   return ctor_initializer_p;
11966 }
11967
11968 /* Parse an initializer.
11969
11970    initializer:
11971      = initializer-clause
11972      ( expression-list )
11973
11974    Returns a expression representing the initializer.  If no
11975    initializer is present, NULL_TREE is returned.
11976
11977    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11978    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11979    set to FALSE if there is no initializer present.  If there is an
11980    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11981    is set to true; otherwise it is set to false.  */
11982
11983 static tree
11984 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11985                        bool* non_constant_p)
11986 {
11987   cp_token *token;
11988   tree init;
11989
11990   /* Peek at the next token.  */
11991   token = cp_lexer_peek_token (parser->lexer);
11992
11993   /* Let our caller know whether or not this initializer was
11994      parenthesized.  */
11995   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11996   /* Assume that the initializer is constant.  */
11997   *non_constant_p = false;
11998
11999   if (token->type == CPP_EQ)
12000     {
12001       /* Consume the `='.  */
12002       cp_lexer_consume_token (parser->lexer);
12003       /* Parse the initializer-clause.  */
12004       init = cp_parser_initializer_clause (parser, non_constant_p);
12005     }
12006   else if (token->type == CPP_OPEN_PAREN)
12007     init = cp_parser_parenthesized_expression_list (parser, false,
12008                                                     non_constant_p);
12009   else
12010     {
12011       /* Anything else is an error.  */
12012       cp_parser_error (parser, "expected initializer");
12013       init = error_mark_node;
12014     }
12015
12016   return init;
12017 }
12018
12019 /* Parse an initializer-clause.
12020
12021    initializer-clause:
12022      assignment-expression
12023      { initializer-list , [opt] }
12024      { }
12025
12026    Returns an expression representing the initializer.
12027
12028    If the `assignment-expression' production is used the value
12029    returned is simply a representation for the expression.
12030
12031    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12032    the elements of the initializer-list (or NULL_TREE, if the last
12033    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12034    NULL_TREE.  There is no way to detect whether or not the optional
12035    trailing `,' was provided.  NON_CONSTANT_P is as for
12036    cp_parser_initializer.  */
12037
12038 static tree
12039 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12040 {
12041   tree initializer;
12042
12043   /* If it is not a `{', then we are looking at an
12044      assignment-expression.  */
12045   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12046     {
12047       initializer 
12048         = cp_parser_constant_expression (parser,
12049                                         /*allow_non_constant_p=*/true,
12050                                         non_constant_p);
12051       if (!*non_constant_p)
12052         initializer = fold_non_dependent_expr (initializer);
12053     }
12054   else
12055     {
12056       /* Consume the `{' token.  */
12057       cp_lexer_consume_token (parser->lexer);
12058       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12059       initializer = make_node (CONSTRUCTOR);
12060       /* If it's not a `}', then there is a non-trivial initializer.  */
12061       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12062         {
12063           /* Parse the initializer list.  */
12064           CONSTRUCTOR_ELTS (initializer)
12065             = cp_parser_initializer_list (parser, non_constant_p);
12066           /* A trailing `,' token is allowed.  */
12067           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12068             cp_lexer_consume_token (parser->lexer);
12069         }
12070       /* Now, there should be a trailing `}'.  */
12071       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12072     }
12073
12074   return initializer;
12075 }
12076
12077 /* Parse an initializer-list.
12078
12079    initializer-list:
12080      initializer-clause
12081      initializer-list , initializer-clause
12082
12083    GNU Extension:
12084
12085    initializer-list:
12086      identifier : initializer-clause
12087      initializer-list, identifier : initializer-clause
12088
12089    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12090    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12091    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12092    as for cp_parser_initializer.  */
12093
12094 static tree
12095 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12096 {
12097   tree initializers = NULL_TREE;
12098
12099   /* Assume all of the expressions are constant.  */
12100   *non_constant_p = false;
12101
12102   /* Parse the rest of the list.  */
12103   while (true)
12104     {
12105       cp_token *token;
12106       tree identifier;
12107       tree initializer;
12108       bool clause_non_constant_p;
12109
12110       /* If the next token is an identifier and the following one is a
12111          colon, we are looking at the GNU designated-initializer
12112          syntax.  */
12113       if (cp_parser_allow_gnu_extensions_p (parser)
12114           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12115           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12116         {
12117           /* Consume the identifier.  */
12118           identifier = cp_lexer_consume_token (parser->lexer)->value;
12119           /* Consume the `:'.  */
12120           cp_lexer_consume_token (parser->lexer);
12121         }
12122       else
12123         identifier = NULL_TREE;
12124
12125       /* Parse the initializer.  */
12126       initializer = cp_parser_initializer_clause (parser,
12127                                                   &clause_non_constant_p);
12128       /* If any clause is non-constant, so is the entire initializer.  */
12129       if (clause_non_constant_p)
12130         *non_constant_p = true;
12131       /* Add it to the list.  */
12132       initializers = tree_cons (identifier, initializer, initializers);
12133
12134       /* If the next token is not a comma, we have reached the end of
12135          the list.  */
12136       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12137         break;
12138
12139       /* Peek at the next token.  */
12140       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12141       /* If the next token is a `}', then we're still done.  An
12142          initializer-clause can have a trailing `,' after the
12143          initializer-list and before the closing `}'.  */
12144       if (token->type == CPP_CLOSE_BRACE)
12145         break;
12146
12147       /* Consume the `,' token.  */
12148       cp_lexer_consume_token (parser->lexer);
12149     }
12150
12151   /* The initializers were built up in reverse order, so we need to
12152      reverse them now.  */
12153   return nreverse (initializers);
12154 }
12155
12156 /* Classes [gram.class] */
12157
12158 /* Parse a class-name.
12159
12160    class-name:
12161      identifier
12162      template-id
12163
12164    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12165    to indicate that names looked up in dependent types should be
12166    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12167    keyword has been used to indicate that the name that appears next
12168    is a template.  TYPE_P is true iff the next name should be treated
12169    as class-name, even if it is declared to be some other kind of name
12170    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12171    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12172    being defined in a class-head.
12173
12174    Returns the TYPE_DECL representing the class.  */
12175
12176 static tree
12177 cp_parser_class_name (cp_parser *parser,
12178                       bool typename_keyword_p,
12179                       bool template_keyword_p,
12180                       bool type_p,
12181                       bool check_dependency_p,
12182                       bool class_head_p,
12183                       bool is_declaration)
12184 {
12185   tree decl;
12186   tree scope;
12187   bool typename_p;
12188   cp_token *token;
12189
12190   /* All class-names start with an identifier.  */
12191   token = cp_lexer_peek_token (parser->lexer);
12192   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12193     {
12194       cp_parser_error (parser, "expected class-name");
12195       return error_mark_node;
12196     }
12197
12198   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12199      to a template-id, so we save it here.  */
12200   scope = parser->scope;
12201   if (scope == error_mark_node)
12202     return error_mark_node;
12203
12204   /* Any name names a type if we're following the `typename' keyword
12205      in a qualified name where the enclosing scope is type-dependent.  */
12206   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12207                 && dependent_type_p (scope));
12208   /* Handle the common case (an identifier, but not a template-id)
12209      efficiently.  */
12210   if (token->type == CPP_NAME
12211       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12212     {
12213       tree identifier;
12214
12215       /* Look for the identifier.  */
12216       identifier = cp_parser_identifier (parser);
12217       /* If the next token isn't an identifier, we are certainly not
12218          looking at a class-name.  */
12219       if (identifier == error_mark_node)
12220         decl = error_mark_node;
12221       /* If we know this is a type-name, there's no need to look it
12222          up.  */
12223       else if (typename_p)
12224         decl = identifier;
12225       else
12226         {
12227           /* If the next token is a `::', then the name must be a type
12228              name.
12229
12230              [basic.lookup.qual]
12231
12232              During the lookup for a name preceding the :: scope
12233              resolution operator, object, function, and enumerator
12234              names are ignored.  */
12235           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12236             type_p = true;
12237           /* Look up the name.  */
12238           decl = cp_parser_lookup_name (parser, identifier,
12239                                         type_p,
12240                                         /*is_template=*/false,
12241                                         /*is_namespace=*/false,
12242                                         check_dependency_p);
12243         }
12244     }
12245   else
12246     {
12247       /* Try a template-id.  */
12248       decl = cp_parser_template_id (parser, template_keyword_p,
12249                                     check_dependency_p,
12250                                     is_declaration);
12251       if (decl == error_mark_node)
12252         return error_mark_node;
12253     }
12254
12255   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12256
12257   /* If this is a typename, create a TYPENAME_TYPE.  */
12258   if (typename_p && decl != error_mark_node)
12259     {
12260       decl = make_typename_type (scope, decl, /*complain=*/1);
12261       if (decl != error_mark_node)
12262         decl = TYPE_NAME (decl);
12263     }
12264
12265   /* Check to see that it is really the name of a class.  */
12266   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12267       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12268       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12269     /* Situations like this:
12270
12271          template <typename T> struct A {
12272            typename T::template X<int>::I i;
12273          };
12274
12275        are problematic.  Is `T::template X<int>' a class-name?  The
12276        standard does not seem to be definitive, but there is no other
12277        valid interpretation of the following `::'.  Therefore, those
12278        names are considered class-names.  */
12279     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12280   else if (decl == error_mark_node
12281            || TREE_CODE (decl) != TYPE_DECL
12282            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12283     {
12284       cp_parser_error (parser, "expected class-name");
12285       return error_mark_node;
12286     }
12287
12288   return decl;
12289 }
12290
12291 /* Parse a class-specifier.
12292
12293    class-specifier:
12294      class-head { member-specification [opt] }
12295
12296    Returns the TREE_TYPE representing the class.  */
12297
12298 static tree
12299 cp_parser_class_specifier (cp_parser* parser)
12300 {
12301   cp_token *token;
12302   tree type;
12303   tree attributes = NULL_TREE;
12304   int has_trailing_semicolon;
12305   bool nested_name_specifier_p;
12306   unsigned saved_num_template_parameter_lists;
12307   bool pop_p = false;
12308
12309   push_deferring_access_checks (dk_no_deferred);
12310
12311   /* Parse the class-head.  */
12312   type = cp_parser_class_head (parser,
12313                                &nested_name_specifier_p,
12314                                &attributes);
12315   /* If the class-head was a semantic disaster, skip the entire body
12316      of the class.  */
12317   if (!type)
12318     {
12319       cp_parser_skip_to_end_of_block_or_statement (parser);
12320       pop_deferring_access_checks ();
12321       return error_mark_node;
12322     }
12323
12324   /* Look for the `{'.  */
12325   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12326     {
12327       pop_deferring_access_checks ();
12328       return error_mark_node;
12329     }
12330
12331   /* Issue an error message if type-definitions are forbidden here.  */
12332   cp_parser_check_type_definition (parser);
12333   /* Remember that we are defining one more class.  */
12334   ++parser->num_classes_being_defined;
12335   /* Inside the class, surrounding template-parameter-lists do not
12336      apply.  */
12337   saved_num_template_parameter_lists
12338     = parser->num_template_parameter_lists;
12339   parser->num_template_parameter_lists = 0;
12340
12341   /* Start the class.  */
12342   if (nested_name_specifier_p)
12343     pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12344   type = begin_class_definition (type);
12345   
12346   if (processing_template_decl)
12347     /* There are no access checks when parsing a template, as we do no
12348        know if a specialization will be a friend.  */
12349     push_deferring_access_checks (dk_no_check);
12350   
12351   if (type == error_mark_node)
12352     /* If the type is erroneous, skip the entire body of the class.  */
12353     cp_parser_skip_to_closing_brace (parser);
12354   else
12355     /* Parse the member-specification.  */
12356     cp_parser_member_specification_opt (parser);
12357   
12358   if (processing_template_decl)
12359     pop_deferring_access_checks ();
12360   
12361   /* Look for the trailing `}'.  */
12362   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12363   /* We get better error messages by noticing a common problem: a
12364      missing trailing `;'.  */
12365   token = cp_lexer_peek_token (parser->lexer);
12366   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12367   /* Look for trailing attributes to apply to this class.  */
12368   if (cp_parser_allow_gnu_extensions_p (parser))
12369     {
12370       tree sub_attr = cp_parser_attributes_opt (parser);
12371       attributes = chainon (attributes, sub_attr);
12372     }
12373   if (type != error_mark_node)
12374     type = finish_struct (type, attributes);
12375   if (pop_p)
12376     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12377   /* If this class is not itself within the scope of another class,
12378      then we need to parse the bodies of all of the queued function
12379      definitions.  Note that the queued functions defined in a class
12380      are not always processed immediately following the
12381      class-specifier for that class.  Consider:
12382
12383        struct A {
12384          struct B { void f() { sizeof (A); } };
12385        };
12386
12387      If `f' were processed before the processing of `A' were
12388      completed, there would be no way to compute the size of `A'.
12389      Note that the nesting we are interested in here is lexical --
12390      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12391      for:
12392
12393        struct A { struct B; };
12394        struct A::B { void f() { } };
12395
12396      there is no need to delay the parsing of `A::B::f'.  */
12397   if (--parser->num_classes_being_defined == 0)
12398     {
12399       tree queue_entry;
12400       tree fn;
12401
12402       /* In a first pass, parse default arguments to the functions.
12403          Then, in a second pass, parse the bodies of the functions.
12404          This two-phased approach handles cases like:
12405
12406             struct S {
12407               void f() { g(); }
12408               void g(int i = 3);
12409             };
12410
12411          */
12412       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12413              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12414            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12415            TREE_PURPOSE (parser->unparsed_functions_queues)
12416              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12417         {
12418           fn = TREE_VALUE (queue_entry);
12419           /* Make sure that any template parameters are in scope.  */
12420           maybe_begin_member_template_processing (fn);
12421           /* If there are default arguments that have not yet been processed,
12422              take care of them now.  */
12423           cp_parser_late_parsing_default_args (parser, fn);
12424           /* Remove any template parameters from the symbol table.  */
12425           maybe_end_member_template_processing ();
12426         }
12427       /* Now parse the body of the functions.  */
12428       for (TREE_VALUE (parser->unparsed_functions_queues)
12429              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12430            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12431            TREE_VALUE (parser->unparsed_functions_queues)
12432              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12433         {
12434           /* Figure out which function we need to process.  */
12435           fn = TREE_VALUE (queue_entry);
12436
12437           /* A hack to prevent garbage collection.  */
12438           function_depth++;
12439
12440           /* Parse the function.  */
12441           cp_parser_late_parsing_for_member (parser, fn);
12442           function_depth--;
12443         }
12444
12445     }
12446
12447   /* Put back any saved access checks.  */
12448   pop_deferring_access_checks ();
12449
12450   /* Restore the count of active template-parameter-lists.  */
12451   parser->num_template_parameter_lists
12452     = saved_num_template_parameter_lists;
12453
12454   return type;
12455 }
12456
12457 /* Parse a class-head.
12458
12459    class-head:
12460      class-key identifier [opt] base-clause [opt]
12461      class-key nested-name-specifier identifier base-clause [opt]
12462      class-key nested-name-specifier [opt] template-id
12463        base-clause [opt]
12464
12465    GNU Extensions:
12466      class-key attributes identifier [opt] base-clause [opt]
12467      class-key attributes nested-name-specifier identifier base-clause [opt]
12468      class-key attributes nested-name-specifier [opt] template-id
12469        base-clause [opt]
12470
12471    Returns the TYPE of the indicated class.  Sets
12472    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12473    involving a nested-name-specifier was used, and FALSE otherwise.
12474
12475    Returns NULL_TREE if the class-head is syntactically valid, but
12476    semantically invalid in a way that means we should skip the entire
12477    body of the class.  */
12478
12479 static tree
12480 cp_parser_class_head (cp_parser* parser,
12481                       bool* nested_name_specifier_p,
12482                       tree *attributes_p)
12483 {
12484   cp_token *token;
12485   tree nested_name_specifier;
12486   enum tag_types class_key;
12487   tree id = NULL_TREE;
12488   tree type = NULL_TREE;
12489   tree attributes;
12490   bool template_id_p = false;
12491   bool qualified_p = false;
12492   bool invalid_nested_name_p = false;
12493   bool invalid_explicit_specialization_p = false;
12494   bool pop_p = false;
12495   unsigned num_templates;
12496
12497   /* Assume no nested-name-specifier will be present.  */
12498   *nested_name_specifier_p = false;
12499   /* Assume no template parameter lists will be used in defining the
12500      type.  */
12501   num_templates = 0;
12502
12503   /* Look for the class-key.  */
12504   class_key = cp_parser_class_key (parser);
12505   if (class_key == none_type)
12506     return error_mark_node;
12507
12508   /* Parse the attributes.  */
12509   attributes = cp_parser_attributes_opt (parser);
12510
12511   /* If the next token is `::', that is invalid -- but sometimes
12512      people do try to write:
12513
12514        struct ::S {};
12515
12516      Handle this gracefully by accepting the extra qualifier, and then
12517      issuing an error about it later if this really is a
12518      class-head.  If it turns out just to be an elaborated type
12519      specifier, remain silent.  */
12520   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12521     qualified_p = true;
12522
12523   push_deferring_access_checks (dk_no_check);
12524
12525   /* Determine the name of the class.  Begin by looking for an
12526      optional nested-name-specifier.  */
12527   nested_name_specifier
12528     = cp_parser_nested_name_specifier_opt (parser,
12529                                            /*typename_keyword_p=*/false,
12530                                            /*check_dependency_p=*/false,
12531                                            /*type_p=*/false,
12532                                            /*is_declaration=*/false);
12533   /* If there was a nested-name-specifier, then there *must* be an
12534      identifier.  */
12535   if (nested_name_specifier)
12536     {
12537       /* Although the grammar says `identifier', it really means
12538          `class-name' or `template-name'.  You are only allowed to
12539          define a class that has already been declared with this
12540          syntax.
12541
12542          The proposed resolution for Core Issue 180 says that whever
12543          you see `class T::X' you should treat `X' as a type-name.
12544
12545          It is OK to define an inaccessible class; for example:
12546
12547            class A { class B; };
12548            class A::B {};
12549
12550          We do not know if we will see a class-name, or a
12551          template-name.  We look for a class-name first, in case the
12552          class-name is a template-id; if we looked for the
12553          template-name first we would stop after the template-name.  */
12554       cp_parser_parse_tentatively (parser);
12555       type = cp_parser_class_name (parser,
12556                                    /*typename_keyword_p=*/false,
12557                                    /*template_keyword_p=*/false,
12558                                    /*type_p=*/true,
12559                                    /*check_dependency_p=*/false,
12560                                    /*class_head_p=*/true,
12561                                    /*is_declaration=*/false);
12562       /* If that didn't work, ignore the nested-name-specifier.  */
12563       if (!cp_parser_parse_definitely (parser))
12564         {
12565           invalid_nested_name_p = true;
12566           id = cp_parser_identifier (parser);
12567           if (id == error_mark_node)
12568             id = NULL_TREE;
12569         }
12570       /* If we could not find a corresponding TYPE, treat this
12571          declaration like an unqualified declaration.  */
12572       if (type == error_mark_node)
12573         nested_name_specifier = NULL_TREE;
12574       /* Otherwise, count the number of templates used in TYPE and its
12575          containing scopes.  */
12576       else
12577         {
12578           tree scope;
12579
12580           for (scope = TREE_TYPE (type);
12581                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12582                scope = (TYPE_P (scope)
12583                         ? TYPE_CONTEXT (scope)
12584                         : DECL_CONTEXT (scope)))
12585             if (TYPE_P (scope)
12586                 && CLASS_TYPE_P (scope)
12587                 && CLASSTYPE_TEMPLATE_INFO (scope)
12588                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12589                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12590               ++num_templates;
12591         }
12592     }
12593   /* Otherwise, the identifier is optional.  */
12594   else
12595     {
12596       /* We don't know whether what comes next is a template-id,
12597          an identifier, or nothing at all.  */
12598       cp_parser_parse_tentatively (parser);
12599       /* Check for a template-id.  */
12600       id = cp_parser_template_id (parser,
12601                                   /*template_keyword_p=*/false,
12602                                   /*check_dependency_p=*/true,
12603                                   /*is_declaration=*/true);
12604       /* If that didn't work, it could still be an identifier.  */
12605       if (!cp_parser_parse_definitely (parser))
12606         {
12607           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12608             id = cp_parser_identifier (parser);
12609           else
12610             id = NULL_TREE;
12611         }
12612       else
12613         {
12614           template_id_p = true;
12615           ++num_templates;
12616         }
12617     }
12618
12619   pop_deferring_access_checks ();
12620
12621   if (id)
12622     cp_parser_check_for_invalid_template_id (parser, id);
12623
12624   /* If it's not a `:' or a `{' then we can't really be looking at a
12625      class-head, since a class-head only appears as part of a
12626      class-specifier.  We have to detect this situation before calling
12627      xref_tag, since that has irreversible side-effects.  */
12628   if (!cp_parser_next_token_starts_class_definition_p (parser))
12629     {
12630       cp_parser_error (parser, "expected `{' or `:'");
12631       return error_mark_node;
12632     }
12633
12634   /* At this point, we're going ahead with the class-specifier, even
12635      if some other problem occurs.  */
12636   cp_parser_commit_to_tentative_parse (parser);
12637   /* Issue the error about the overly-qualified name now.  */
12638   if (qualified_p)
12639     cp_parser_error (parser,
12640                      "global qualification of class name is invalid");
12641   else if (invalid_nested_name_p)
12642     cp_parser_error (parser,
12643                      "qualified name does not name a class");
12644   else if (nested_name_specifier)
12645     {
12646       tree scope;
12647       /* Figure out in what scope the declaration is being placed.  */
12648       scope = current_scope ();
12649       if (!scope)
12650         scope = current_namespace;
12651       /* If that scope does not contain the scope in which the
12652          class was originally declared, the program is invalid.  */
12653       if (scope && !is_ancestor (scope, nested_name_specifier))
12654         {
12655           error ("declaration of `%D' in `%D' which does not "
12656                  "enclose `%D'", type, scope, nested_name_specifier);
12657           type = NULL_TREE;
12658           goto done;
12659         }
12660       /* [dcl.meaning]
12661
12662          A declarator-id shall not be qualified exception of the
12663          definition of a ... nested class outside of its class
12664          ... [or] a the definition or explicit instantiation of a
12665          class member of a namespace outside of its namespace.  */
12666       if (scope == nested_name_specifier)
12667         {
12668           pedwarn ("extra qualification ignored");
12669           nested_name_specifier = NULL_TREE;
12670           num_templates = 0;
12671         }
12672     }
12673   /* An explicit-specialization must be preceded by "template <>".  If
12674      it is not, try to recover gracefully.  */
12675   if (at_namespace_scope_p ()
12676       && parser->num_template_parameter_lists == 0
12677       && template_id_p)
12678     {
12679       error ("an explicit specialization must be preceded by 'template <>'");
12680       invalid_explicit_specialization_p = true;
12681       /* Take the same action that would have been taken by
12682          cp_parser_explicit_specialization.  */
12683       ++parser->num_template_parameter_lists;
12684       begin_specialization ();
12685     }
12686   /* There must be no "return" statements between this point and the
12687      end of this function; set "type "to the correct return value and
12688      use "goto done;" to return.  */
12689   /* Make sure that the right number of template parameters were
12690      present.  */
12691   if (!cp_parser_check_template_parameters (parser, num_templates))
12692     {
12693       /* If something went wrong, there is no point in even trying to
12694          process the class-definition.  */
12695       type = NULL_TREE;
12696       goto done;
12697     }
12698
12699   /* Look up the type.  */
12700   if (template_id_p)
12701     {
12702       type = TREE_TYPE (id);
12703       maybe_process_partial_specialization (type);
12704     }
12705   else if (!nested_name_specifier)
12706     {
12707       /* If the class was unnamed, create a dummy name.  */
12708       if (!id)
12709         id = make_anon_name ();
12710       type = xref_tag (class_key, id, /*globalize=*/false,
12711                        parser->num_template_parameter_lists);
12712     }
12713   else
12714     {
12715       tree class_type;
12716       bool pop_p = false;
12717
12718       /* Given:
12719
12720             template <typename T> struct S { struct T };
12721             template <typename T> struct S<T>::T { };
12722
12723          we will get a TYPENAME_TYPE when processing the definition of
12724          `S::T'.  We need to resolve it to the actual type before we
12725          try to define it.  */
12726       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12727         {
12728           class_type = resolve_typename_type (TREE_TYPE (type),
12729                                               /*only_current_p=*/false);
12730           if (class_type != error_mark_node)
12731             type = TYPE_NAME (class_type);
12732           else
12733             {
12734               cp_parser_error (parser, "could not resolve typename type");
12735               type = error_mark_node;
12736             }
12737         }
12738
12739       maybe_process_partial_specialization (TREE_TYPE (type));
12740       class_type = current_class_type;
12741       /* Enter the scope indicated by the nested-name-specifier.  */
12742       if (nested_name_specifier)
12743         pop_p = push_scope (nested_name_specifier);
12744       /* Get the canonical version of this type.  */
12745       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12746       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12747           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12748         type = push_template_decl (type);
12749       type = TREE_TYPE (type);
12750       if (nested_name_specifier)
12751         {
12752           *nested_name_specifier_p = true;
12753           if (pop_p)
12754             pop_scope (nested_name_specifier);
12755         }
12756     }
12757   /* Indicate whether this class was declared as a `class' or as a
12758      `struct'.  */
12759   if (TREE_CODE (type) == RECORD_TYPE)
12760     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12761   cp_parser_check_class_key (class_key, type);
12762
12763   /* Enter the scope containing the class; the names of base classes
12764      should be looked up in that context.  For example, given:
12765
12766        struct A { struct B {}; struct C; };
12767        struct A::C : B {};
12768
12769      is valid.  */
12770   if (nested_name_specifier)
12771     pop_p = push_scope (nested_name_specifier);
12772   /* Now, look for the base-clause.  */
12773   token = cp_lexer_peek_token (parser->lexer);
12774   if (token->type == CPP_COLON)
12775     {
12776       tree bases;
12777
12778       /* Get the list of base-classes.  */
12779       bases = cp_parser_base_clause (parser);
12780       /* Process them.  */
12781       xref_basetypes (type, bases);
12782     }
12783   /* Leave the scope given by the nested-name-specifier.  We will
12784      enter the class scope itself while processing the members.  */
12785   if (pop_p)
12786     pop_scope (nested_name_specifier);
12787
12788  done:
12789   if (invalid_explicit_specialization_p)
12790     {
12791       end_specialization ();
12792       --parser->num_template_parameter_lists;
12793     }
12794   *attributes_p = attributes;
12795   return type;
12796 }
12797
12798 /* Parse a class-key.
12799
12800    class-key:
12801      class
12802      struct
12803      union
12804
12805    Returns the kind of class-key specified, or none_type to indicate
12806    error.  */
12807
12808 static enum tag_types
12809 cp_parser_class_key (cp_parser* parser)
12810 {
12811   cp_token *token;
12812   enum tag_types tag_type;
12813
12814   /* Look for the class-key.  */
12815   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12816   if (!token)
12817     return none_type;
12818
12819   /* Check to see if the TOKEN is a class-key.  */
12820   tag_type = cp_parser_token_is_class_key (token);
12821   if (!tag_type)
12822     cp_parser_error (parser, "expected class-key");
12823   return tag_type;
12824 }
12825
12826 /* Parse an (optional) member-specification.
12827
12828    member-specification:
12829      member-declaration member-specification [opt]
12830      access-specifier : member-specification [opt]  */
12831
12832 static void
12833 cp_parser_member_specification_opt (cp_parser* parser)
12834 {
12835   while (true)
12836     {
12837       cp_token *token;
12838       enum rid keyword;
12839
12840       /* Peek at the next token.  */
12841       token = cp_lexer_peek_token (parser->lexer);
12842       /* If it's a `}', or EOF then we've seen all the members.  */
12843       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12844         break;
12845
12846       /* See if this token is a keyword.  */
12847       keyword = token->keyword;
12848       switch (keyword)
12849         {
12850         case RID_PUBLIC:
12851         case RID_PROTECTED:
12852         case RID_PRIVATE:
12853           /* Consume the access-specifier.  */
12854           cp_lexer_consume_token (parser->lexer);
12855           /* Remember which access-specifier is active.  */
12856           current_access_specifier = token->value;
12857           /* Look for the `:'.  */
12858           cp_parser_require (parser, CPP_COLON, "`:'");
12859           break;
12860
12861         default:
12862           /* Otherwise, the next construction must be a
12863              member-declaration.  */
12864           cp_parser_member_declaration (parser);
12865         }
12866     }
12867 }
12868
12869 /* Parse a member-declaration.
12870
12871    member-declaration:
12872      decl-specifier-seq [opt] member-declarator-list [opt] ;
12873      function-definition ; [opt]
12874      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12875      using-declaration
12876      template-declaration
12877
12878    member-declarator-list:
12879      member-declarator
12880      member-declarator-list , member-declarator
12881
12882    member-declarator:
12883      declarator pure-specifier [opt]
12884      declarator constant-initializer [opt]
12885      identifier [opt] : constant-expression
12886
12887    GNU Extensions:
12888
12889    member-declaration:
12890      __extension__ member-declaration
12891
12892    member-declarator:
12893      declarator attributes [opt] pure-specifier [opt]
12894      declarator attributes [opt] constant-initializer [opt]
12895      identifier [opt] attributes [opt] : constant-expression  */
12896
12897 static void
12898 cp_parser_member_declaration (cp_parser* parser)
12899 {
12900   cp_decl_specifier_seq decl_specifiers;
12901   tree prefix_attributes;
12902   tree decl;
12903   int declares_class_or_enum;
12904   bool friend_p;
12905   cp_token *token;
12906   int saved_pedantic;
12907
12908   /* Check for the `__extension__' keyword.  */
12909   if (cp_parser_extension_opt (parser, &saved_pedantic))
12910     {
12911       /* Recurse.  */
12912       cp_parser_member_declaration (parser);
12913       /* Restore the old value of the PEDANTIC flag.  */
12914       pedantic = saved_pedantic;
12915
12916       return;
12917     }
12918
12919   /* Check for a template-declaration.  */
12920   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12921     {
12922       /* Parse the template-declaration.  */
12923       cp_parser_template_declaration (parser, /*member_p=*/true);
12924
12925       return;
12926     }
12927
12928   /* Check for a using-declaration.  */
12929   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12930     {
12931       /* Parse the using-declaration.  */
12932       cp_parser_using_declaration (parser);
12933
12934       return;
12935     }
12936
12937   /* Parse the decl-specifier-seq.  */
12938   cp_parser_decl_specifier_seq (parser,
12939                                 CP_PARSER_FLAGS_OPTIONAL,
12940                                 &decl_specifiers,
12941                                 &declares_class_or_enum);
12942   prefix_attributes = decl_specifiers.attributes;
12943   decl_specifiers.attributes = NULL_TREE;
12944   /* Check for an invalid type-name.  */
12945   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12946     return;
12947   /* If there is no declarator, then the decl-specifier-seq should
12948      specify a type.  */
12949   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12950     {
12951       /* If there was no decl-specifier-seq, and the next token is a
12952          `;', then we have something like:
12953
12954            struct S { ; };
12955
12956          [class.mem]
12957
12958          Each member-declaration shall declare at least one member
12959          name of the class.  */
12960       if (!decl_specifiers.any_specifiers_p)
12961         {
12962           if (pedantic)
12963             pedwarn ("extra semicolon");
12964         }
12965       else
12966         {
12967           tree type;
12968
12969           /* See if this declaration is a friend.  */
12970           friend_p = cp_parser_friend_p (&decl_specifiers);
12971           /* If there were decl-specifiers, check to see if there was
12972              a class-declaration.  */
12973           type = check_tag_decl (&decl_specifiers);
12974           /* Nested classes have already been added to the class, but
12975              a `friend' needs to be explicitly registered.  */
12976           if (friend_p)
12977             {
12978               /* If the `friend' keyword was present, the friend must
12979                  be introduced with a class-key.  */
12980                if (!declares_class_or_enum)
12981                  error ("a class-key must be used when declaring a friend");
12982                /* In this case:
12983
12984                     template <typename T> struct A {
12985                       friend struct A<T>::B;
12986                     };
12987
12988                   A<T>::B will be represented by a TYPENAME_TYPE, and
12989                   therefore not recognized by check_tag_decl.  */
12990                if (!type 
12991                    && decl_specifiers.type
12992                    && TYPE_P (decl_specifiers.type))
12993                  type = decl_specifiers.type;
12994                if (!type || !TYPE_P (type))
12995                  error ("friend declaration does not name a class or "
12996                         "function");
12997                else
12998                  make_friend_class (current_class_type, type,
12999                                     /*complain=*/true);
13000             }
13001           /* If there is no TYPE, an error message will already have
13002              been issued.  */
13003           else if (!type || type == error_mark_node)
13004             ;
13005           /* An anonymous aggregate has to be handled specially; such
13006              a declaration really declares a data member (with a
13007              particular type), as opposed to a nested class.  */
13008           else if (ANON_AGGR_TYPE_P (type))
13009             {
13010               /* Remove constructors and such from TYPE, now that we
13011                  know it is an anonymous aggregate.  */
13012               fixup_anonymous_aggr (type);
13013               /* And make the corresponding data member.  */
13014               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13015               /* Add it to the class.  */
13016               finish_member_declaration (decl);
13017             }
13018           else
13019             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13020         }
13021     }
13022   else
13023     {
13024       /* See if these declarations will be friends.  */
13025       friend_p = cp_parser_friend_p (&decl_specifiers);
13026
13027       /* Keep going until we hit the `;' at the end of the
13028          declaration.  */
13029       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13030         {
13031           tree attributes = NULL_TREE;
13032           tree first_attribute;
13033
13034           /* Peek at the next token.  */
13035           token = cp_lexer_peek_token (parser->lexer);
13036
13037           /* Check for a bitfield declaration.  */
13038           if (token->type == CPP_COLON
13039               || (token->type == CPP_NAME
13040                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13041                   == CPP_COLON))
13042             {
13043               tree identifier;
13044               tree width;
13045
13046               /* Get the name of the bitfield.  Note that we cannot just
13047                  check TOKEN here because it may have been invalidated by
13048                  the call to cp_lexer_peek_nth_token above.  */
13049               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13050                 identifier = cp_parser_identifier (parser);
13051               else
13052                 identifier = NULL_TREE;
13053
13054               /* Consume the `:' token.  */
13055               cp_lexer_consume_token (parser->lexer);
13056               /* Get the width of the bitfield.  */
13057               width
13058                 = cp_parser_constant_expression (parser,
13059                                                  /*allow_non_constant=*/false,
13060                                                  NULL);
13061
13062               /* Look for attributes that apply to the bitfield.  */
13063               attributes = cp_parser_attributes_opt (parser);
13064               /* Remember which attributes are prefix attributes and
13065                  which are not.  */
13066               first_attribute = attributes;
13067               /* Combine the attributes.  */
13068               attributes = chainon (prefix_attributes, attributes);
13069
13070               /* Create the bitfield declaration.  */
13071               decl = grokbitfield (identifier 
13072                                    ? make_id_declarator (identifier)
13073                                    : NULL,
13074                                    &decl_specifiers,
13075                                    width);
13076               /* Apply the attributes.  */
13077               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13078             }
13079           else
13080             {
13081               cp_declarator *declarator;
13082               tree initializer;
13083               tree asm_specification;
13084               int ctor_dtor_or_conv_p;
13085
13086               /* Parse the declarator.  */
13087               declarator
13088                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13089                                         &ctor_dtor_or_conv_p,
13090                                         /*parenthesized_p=*/NULL);
13091
13092               /* If something went wrong parsing the declarator, make sure
13093                  that we at least consume some tokens.  */
13094               if (declarator == cp_error_declarator)
13095                 {
13096                   /* Skip to the end of the statement.  */
13097                   cp_parser_skip_to_end_of_statement (parser);
13098                   /* If the next token is not a semicolon, that is
13099                      probably because we just skipped over the body of
13100                      a function.  So, we consume a semicolon if
13101                      present, but do not issue an error message if it
13102                      is not present.  */
13103                   if (cp_lexer_next_token_is (parser->lexer,
13104                                               CPP_SEMICOLON))
13105                     cp_lexer_consume_token (parser->lexer);
13106                   return;
13107                 }
13108
13109               cp_parser_check_for_definition_in_return_type
13110                 (declarator, declares_class_or_enum);
13111
13112               /* Look for an asm-specification.  */
13113               asm_specification = cp_parser_asm_specification_opt (parser);
13114               /* Look for attributes that apply to the declaration.  */
13115               attributes = cp_parser_attributes_opt (parser);
13116               /* Remember which attributes are prefix attributes and
13117                  which are not.  */
13118               first_attribute = attributes;
13119               /* Combine the attributes.  */
13120               attributes = chainon (prefix_attributes, attributes);
13121
13122               /* If it's an `=', then we have a constant-initializer or a
13123                  pure-specifier.  It is not correct to parse the
13124                  initializer before registering the member declaration
13125                  since the member declaration should be in scope while
13126                  its initializer is processed.  However, the rest of the
13127                  front end does not yet provide an interface that allows
13128                  us to handle this correctly.  */
13129               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13130                 {
13131                   /* In [class.mem]:
13132
13133                      A pure-specifier shall be used only in the declaration of
13134                      a virtual function.
13135
13136                      A member-declarator can contain a constant-initializer
13137                      only if it declares a static member of integral or
13138                      enumeration type.
13139
13140                      Therefore, if the DECLARATOR is for a function, we look
13141                      for a pure-specifier; otherwise, we look for a
13142                      constant-initializer.  When we call `grokfield', it will
13143                      perform more stringent semantics checks.  */
13144                   if (declarator->kind == cdk_function)
13145                     initializer = cp_parser_pure_specifier (parser);
13146                   else
13147                     /* Parse the initializer.  */
13148                     initializer = cp_parser_constant_initializer (parser);
13149                 }
13150               /* Otherwise, there is no initializer.  */
13151               else
13152                 initializer = NULL_TREE;
13153
13154               /* See if we are probably looking at a function
13155                  definition.  We are certainly not looking at at a
13156                  member-declarator.  Calling `grokfield' has
13157                  side-effects, so we must not do it unless we are sure
13158                  that we are looking at a member-declarator.  */
13159               if (cp_parser_token_starts_function_definition_p
13160                   (cp_lexer_peek_token (parser->lexer)))
13161                 {
13162                   /* The grammar does not allow a pure-specifier to be
13163                      used when a member function is defined.  (It is
13164                      possible that this fact is an oversight in the
13165                      standard, since a pure function may be defined
13166                      outside of the class-specifier.  */
13167                   if (initializer)
13168                     error ("pure-specifier on function-definition");
13169                   decl = cp_parser_save_member_function_body (parser,
13170                                                               &decl_specifiers,
13171                                                               declarator,
13172                                                               attributes);
13173                   /* If the member was not a friend, declare it here.  */
13174                   if (!friend_p)
13175                     finish_member_declaration (decl);
13176                   /* Peek at the next token.  */
13177                   token = cp_lexer_peek_token (parser->lexer);
13178                   /* If the next token is a semicolon, consume it.  */
13179                   if (token->type == CPP_SEMICOLON)
13180                     cp_lexer_consume_token (parser->lexer);
13181                   return;
13182                 }
13183               else
13184                 {
13185                   /* Create the declaration.  */
13186                   decl = grokfield (declarator, &decl_specifiers,
13187                                     initializer, asm_specification,
13188                                     attributes);
13189                   /* Any initialization must have been from a
13190                      constant-expression.  */
13191                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13192                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13193                 }
13194             }
13195
13196           /* Reset PREFIX_ATTRIBUTES.  */
13197           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13198             attributes = TREE_CHAIN (attributes);
13199           if (attributes)
13200             TREE_CHAIN (attributes) = NULL_TREE;
13201
13202           /* If there is any qualification still in effect, clear it
13203              now; we will be starting fresh with the next declarator.  */
13204           parser->scope = NULL_TREE;
13205           parser->qualifying_scope = NULL_TREE;
13206           parser->object_scope = NULL_TREE;
13207           /* If it's a `,', then there are more declarators.  */
13208           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13209             cp_lexer_consume_token (parser->lexer);
13210           /* If the next token isn't a `;', then we have a parse error.  */
13211           else if (cp_lexer_next_token_is_not (parser->lexer,
13212                                                CPP_SEMICOLON))
13213             {
13214               cp_parser_error (parser, "expected `;'");
13215               /* Skip tokens until we find a `;'.  */
13216               cp_parser_skip_to_end_of_statement (parser);
13217
13218               break;
13219             }
13220
13221           if (decl)
13222             {
13223               /* Add DECL to the list of members.  */
13224               if (!friend_p)
13225                 finish_member_declaration (decl);
13226
13227               if (TREE_CODE (decl) == FUNCTION_DECL)
13228                 cp_parser_save_default_args (parser, decl);
13229             }
13230         }
13231     }
13232
13233   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13234 }
13235
13236 /* Parse a pure-specifier.
13237
13238    pure-specifier:
13239      = 0
13240
13241    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13242    Otherwise, ERROR_MARK_NODE is returned.  */
13243
13244 static tree
13245 cp_parser_pure_specifier (cp_parser* parser)
13246 {
13247   cp_token *token;
13248
13249   /* Look for the `=' token.  */
13250   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13251     return error_mark_node;
13252   /* Look for the `0' token.  */
13253   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13254   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13255      to get information from the lexer about how the number was
13256      spelled in order to fix this problem.  */
13257   if (!token || !integer_zerop (token->value))
13258     return error_mark_node;
13259
13260   return integer_zero_node;
13261 }
13262
13263 /* Parse a constant-initializer.
13264
13265    constant-initializer:
13266      = constant-expression
13267
13268    Returns a representation of the constant-expression.  */
13269
13270 static tree
13271 cp_parser_constant_initializer (cp_parser* parser)
13272 {
13273   /* Look for the `=' token.  */
13274   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13275     return error_mark_node;
13276
13277   /* It is invalid to write:
13278
13279        struct S { static const int i = { 7 }; };
13280
13281      */
13282   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13283     {
13284       cp_parser_error (parser,
13285                        "a brace-enclosed initializer is not allowed here");
13286       /* Consume the opening brace.  */
13287       cp_lexer_consume_token (parser->lexer);
13288       /* Skip the initializer.  */
13289       cp_parser_skip_to_closing_brace (parser);
13290       /* Look for the trailing `}'.  */
13291       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13292
13293       return error_mark_node;
13294     }
13295
13296   return cp_parser_constant_expression (parser,
13297                                         /*allow_non_constant=*/false,
13298                                         NULL);
13299 }
13300
13301 /* Derived classes [gram.class.derived] */
13302
13303 /* Parse a base-clause.
13304
13305    base-clause:
13306      : base-specifier-list
13307
13308    base-specifier-list:
13309      base-specifier
13310      base-specifier-list , base-specifier
13311
13312    Returns a TREE_LIST representing the base-classes, in the order in
13313    which they were declared.  The representation of each node is as
13314    described by cp_parser_base_specifier.
13315
13316    In the case that no bases are specified, this function will return
13317    NULL_TREE, not ERROR_MARK_NODE.  */
13318
13319 static tree
13320 cp_parser_base_clause (cp_parser* parser)
13321 {
13322   tree bases = NULL_TREE;
13323
13324   /* Look for the `:' that begins the list.  */
13325   cp_parser_require (parser, CPP_COLON, "`:'");
13326
13327   /* Scan the base-specifier-list.  */
13328   while (true)
13329     {
13330       cp_token *token;
13331       tree base;
13332
13333       /* Look for the base-specifier.  */
13334       base = cp_parser_base_specifier (parser);
13335       /* Add BASE to the front of the list.  */
13336       if (base != error_mark_node)
13337         {
13338           TREE_CHAIN (base) = bases;
13339           bases = base;
13340         }
13341       /* Peek at the next token.  */
13342       token = cp_lexer_peek_token (parser->lexer);
13343       /* If it's not a comma, then the list is complete.  */
13344       if (token->type != CPP_COMMA)
13345         break;
13346       /* Consume the `,'.  */
13347       cp_lexer_consume_token (parser->lexer);
13348     }
13349
13350   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13351      base class had a qualified name.  However, the next name that
13352      appears is certainly not qualified.  */
13353   parser->scope = NULL_TREE;
13354   parser->qualifying_scope = NULL_TREE;
13355   parser->object_scope = NULL_TREE;
13356
13357   return nreverse (bases);
13358 }
13359
13360 /* Parse a base-specifier.
13361
13362    base-specifier:
13363      :: [opt] nested-name-specifier [opt] class-name
13364      virtual access-specifier [opt] :: [opt] nested-name-specifier
13365        [opt] class-name
13366      access-specifier virtual [opt] :: [opt] nested-name-specifier
13367        [opt] class-name
13368
13369    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13370    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13371    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13372    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13373
13374 static tree
13375 cp_parser_base_specifier (cp_parser* parser)
13376 {
13377   cp_token *token;
13378   bool done = false;
13379   bool virtual_p = false;
13380   bool duplicate_virtual_error_issued_p = false;
13381   bool duplicate_access_error_issued_p = false;
13382   bool class_scope_p, template_p;
13383   tree access = access_default_node;
13384   tree type;
13385
13386   /* Process the optional `virtual' and `access-specifier'.  */
13387   while (!done)
13388     {
13389       /* Peek at the next token.  */
13390       token = cp_lexer_peek_token (parser->lexer);
13391       /* Process `virtual'.  */
13392       switch (token->keyword)
13393         {
13394         case RID_VIRTUAL:
13395           /* If `virtual' appears more than once, issue an error.  */
13396           if (virtual_p && !duplicate_virtual_error_issued_p)
13397             {
13398               cp_parser_error (parser,
13399                                "`virtual' specified more than once in base-specified");
13400               duplicate_virtual_error_issued_p = true;
13401             }
13402
13403           virtual_p = true;
13404
13405           /* Consume the `virtual' token.  */
13406           cp_lexer_consume_token (parser->lexer);
13407
13408           break;
13409
13410         case RID_PUBLIC:
13411         case RID_PROTECTED:
13412         case RID_PRIVATE:
13413           /* If more than one access specifier appears, issue an
13414              error.  */
13415           if (access != access_default_node
13416               && !duplicate_access_error_issued_p)
13417             {
13418               cp_parser_error (parser,
13419                                "more than one access specifier in base-specified");
13420               duplicate_access_error_issued_p = true;
13421             }
13422
13423           access = ridpointers[(int) token->keyword];
13424
13425           /* Consume the access-specifier.  */
13426           cp_lexer_consume_token (parser->lexer);
13427
13428           break;
13429
13430         default:
13431           done = true;
13432           break;
13433         }
13434     }
13435   /* It is not uncommon to see programs mechanically, erroneously, use
13436      the 'typename' keyword to denote (dependent) qualified types
13437      as base classes.  */
13438   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13439     {
13440       if (!processing_template_decl)
13441         error ("keyword `typename' not allowed outside of templates");
13442       else
13443         error ("keyword `typename' not allowed in this context "
13444                "(the base class is implicitly a type)");
13445       cp_lexer_consume_token (parser->lexer);
13446     }
13447
13448   /* Look for the optional `::' operator.  */
13449   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13450   /* Look for the nested-name-specifier.  The simplest way to
13451      implement:
13452
13453        [temp.res]
13454
13455        The keyword `typename' is not permitted in a base-specifier or
13456        mem-initializer; in these contexts a qualified name that
13457        depends on a template-parameter is implicitly assumed to be a
13458        type name.
13459
13460      is to pretend that we have seen the `typename' keyword at this
13461      point.  */
13462   cp_parser_nested_name_specifier_opt (parser,
13463                                        /*typename_keyword_p=*/true,
13464                                        /*check_dependency_p=*/true,
13465                                        /*type_p=*/true,
13466                                        /*is_declaration=*/true);
13467   /* If the base class is given by a qualified name, assume that names
13468      we see are type names or templates, as appropriate.  */
13469   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13470   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13471
13472   /* Finally, look for the class-name.  */
13473   type = cp_parser_class_name (parser,
13474                                class_scope_p,
13475                                template_p,
13476                                /*type_p=*/true,
13477                                /*check_dependency_p=*/true,
13478                                /*class_head_p=*/false,
13479                                /*is_declaration=*/true);
13480
13481   if (type == error_mark_node)
13482     return error_mark_node;
13483
13484   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13485 }
13486
13487 /* Exception handling [gram.exception] */
13488
13489 /* Parse an (optional) exception-specification.
13490
13491    exception-specification:
13492      throw ( type-id-list [opt] )
13493
13494    Returns a TREE_LIST representing the exception-specification.  The
13495    TREE_VALUE of each node is a type.  */
13496
13497 static tree
13498 cp_parser_exception_specification_opt (cp_parser* parser)
13499 {
13500   cp_token *token;
13501   tree type_id_list;
13502
13503   /* Peek at the next token.  */
13504   token = cp_lexer_peek_token (parser->lexer);
13505   /* If it's not `throw', then there's no exception-specification.  */
13506   if (!cp_parser_is_keyword (token, RID_THROW))
13507     return NULL_TREE;
13508
13509   /* Consume the `throw'.  */
13510   cp_lexer_consume_token (parser->lexer);
13511
13512   /* Look for the `('.  */
13513   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13514
13515   /* Peek at the next token.  */
13516   token = cp_lexer_peek_token (parser->lexer);
13517   /* If it's not a `)', then there is a type-id-list.  */
13518   if (token->type != CPP_CLOSE_PAREN)
13519     {
13520       const char *saved_message;
13521
13522       /* Types may not be defined in an exception-specification.  */
13523       saved_message = parser->type_definition_forbidden_message;
13524       parser->type_definition_forbidden_message
13525         = "types may not be defined in an exception-specification";
13526       /* Parse the type-id-list.  */
13527       type_id_list = cp_parser_type_id_list (parser);
13528       /* Restore the saved message.  */
13529       parser->type_definition_forbidden_message = saved_message;
13530     }
13531   else
13532     type_id_list = empty_except_spec;
13533
13534   /* Look for the `)'.  */
13535   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13536
13537   return type_id_list;
13538 }
13539
13540 /* Parse an (optional) type-id-list.
13541
13542    type-id-list:
13543      type-id
13544      type-id-list , type-id
13545
13546    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13547    in the order that the types were presented.  */
13548
13549 static tree
13550 cp_parser_type_id_list (cp_parser* parser)
13551 {
13552   tree types = NULL_TREE;
13553
13554   while (true)
13555     {
13556       cp_token *token;
13557       tree type;
13558
13559       /* Get the next type-id.  */
13560       type = cp_parser_type_id (parser);
13561       /* Add it to the list.  */
13562       types = add_exception_specifier (types, type, /*complain=*/1);
13563       /* Peek at the next token.  */
13564       token = cp_lexer_peek_token (parser->lexer);
13565       /* If it is not a `,', we are done.  */
13566       if (token->type != CPP_COMMA)
13567         break;
13568       /* Consume the `,'.  */
13569       cp_lexer_consume_token (parser->lexer);
13570     }
13571
13572   return nreverse (types);
13573 }
13574
13575 /* Parse a try-block.
13576
13577    try-block:
13578      try compound-statement handler-seq  */
13579
13580 static tree
13581 cp_parser_try_block (cp_parser* parser)
13582 {
13583   tree try_block;
13584
13585   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13586   try_block = begin_try_block ();
13587   cp_parser_compound_statement (parser, NULL, true);
13588   finish_try_block (try_block);
13589   cp_parser_handler_seq (parser);
13590   finish_handler_sequence (try_block);
13591
13592   return try_block;
13593 }
13594
13595 /* Parse a function-try-block.
13596
13597    function-try-block:
13598      try ctor-initializer [opt] function-body handler-seq  */
13599
13600 static bool
13601 cp_parser_function_try_block (cp_parser* parser)
13602 {
13603   tree try_block;
13604   bool ctor_initializer_p;
13605
13606   /* Look for the `try' keyword.  */
13607   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13608     return false;
13609   /* Let the rest of the front-end know where we are.  */
13610   try_block = begin_function_try_block ();
13611   /* Parse the function-body.  */
13612   ctor_initializer_p
13613     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13614   /* We're done with the `try' part.  */
13615   finish_function_try_block (try_block);
13616   /* Parse the handlers.  */
13617   cp_parser_handler_seq (parser);
13618   /* We're done with the handlers.  */
13619   finish_function_handler_sequence (try_block);
13620
13621   return ctor_initializer_p;
13622 }
13623
13624 /* Parse a handler-seq.
13625
13626    handler-seq:
13627      handler handler-seq [opt]  */
13628
13629 static void
13630 cp_parser_handler_seq (cp_parser* parser)
13631 {
13632   while (true)
13633     {
13634       cp_token *token;
13635
13636       /* Parse the handler.  */
13637       cp_parser_handler (parser);
13638       /* Peek at the next token.  */
13639       token = cp_lexer_peek_token (parser->lexer);
13640       /* If it's not `catch' then there are no more handlers.  */
13641       if (!cp_parser_is_keyword (token, RID_CATCH))
13642         break;
13643     }
13644 }
13645
13646 /* Parse a handler.
13647
13648    handler:
13649      catch ( exception-declaration ) compound-statement  */
13650
13651 static void
13652 cp_parser_handler (cp_parser* parser)
13653 {
13654   tree handler;
13655   tree declaration;
13656
13657   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13658   handler = begin_handler ();
13659   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13660   declaration = cp_parser_exception_declaration (parser);
13661   finish_handler_parms (declaration, handler);
13662   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13663   cp_parser_compound_statement (parser, NULL, false);
13664   finish_handler (handler);
13665 }
13666
13667 /* Parse an exception-declaration.
13668
13669    exception-declaration:
13670      type-specifier-seq declarator
13671      type-specifier-seq abstract-declarator
13672      type-specifier-seq
13673      ...
13674
13675    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13676    ellipsis variant is used.  */
13677
13678 static tree
13679 cp_parser_exception_declaration (cp_parser* parser)
13680 {
13681   tree decl;
13682   cp_decl_specifier_seq type_specifiers;
13683   cp_declarator *declarator;
13684   const char *saved_message;
13685
13686   /* If it's an ellipsis, it's easy to handle.  */
13687   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13688     {
13689       /* Consume the `...' token.  */
13690       cp_lexer_consume_token (parser->lexer);
13691       return NULL_TREE;
13692     }
13693
13694   /* Types may not be defined in exception-declarations.  */
13695   saved_message = parser->type_definition_forbidden_message;
13696   parser->type_definition_forbidden_message
13697     = "types may not be defined in exception-declarations";
13698
13699   /* Parse the type-specifier-seq.  */
13700   cp_parser_type_specifier_seq (parser, &type_specifiers);
13701   /* If it's a `)', then there is no declarator.  */
13702   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13703     declarator = NULL;
13704   else
13705     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13706                                        /*ctor_dtor_or_conv_p=*/NULL,
13707                                        /*parenthesized_p=*/NULL);
13708
13709   /* Restore the saved message.  */
13710   parser->type_definition_forbidden_message = saved_message;
13711
13712   if (type_specifiers.any_specifiers_p)
13713     {
13714       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13715       if (decl == NULL_TREE)
13716         error ("invalid catch parameter");
13717     }
13718   else
13719     decl = NULL_TREE;
13720
13721   return decl;
13722 }
13723
13724 /* Parse a throw-expression.
13725
13726    throw-expression:
13727      throw assignment-expression [opt]
13728
13729    Returns a THROW_EXPR representing the throw-expression.  */
13730
13731 static tree
13732 cp_parser_throw_expression (cp_parser* parser)
13733 {
13734   tree expression;
13735   cp_token* token;
13736
13737   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13738   token = cp_lexer_peek_token (parser->lexer);
13739   /* Figure out whether or not there is an assignment-expression
13740      following the "throw" keyword.  */
13741   if (token->type == CPP_COMMA
13742       || token->type == CPP_SEMICOLON
13743       || token->type == CPP_CLOSE_PAREN
13744       || token->type == CPP_CLOSE_SQUARE
13745       || token->type == CPP_CLOSE_BRACE
13746       || token->type == CPP_COLON)
13747     expression = NULL_TREE;
13748   else
13749     expression = cp_parser_assignment_expression (parser);
13750
13751   return build_throw (expression);
13752 }
13753
13754 /* GNU Extensions */
13755
13756 /* Parse an (optional) asm-specification.
13757
13758    asm-specification:
13759      asm ( string-literal )
13760
13761    If the asm-specification is present, returns a STRING_CST
13762    corresponding to the string-literal.  Otherwise, returns
13763    NULL_TREE.  */
13764
13765 static tree
13766 cp_parser_asm_specification_opt (cp_parser* parser)
13767 {
13768   cp_token *token;
13769   tree asm_specification;
13770
13771   /* Peek at the next token.  */
13772   token = cp_lexer_peek_token (parser->lexer);
13773   /* If the next token isn't the `asm' keyword, then there's no
13774      asm-specification.  */
13775   if (!cp_parser_is_keyword (token, RID_ASM))
13776     return NULL_TREE;
13777
13778   /* Consume the `asm' token.  */
13779   cp_lexer_consume_token (parser->lexer);
13780   /* Look for the `('.  */
13781   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13782
13783   /* Look for the string-literal.  */
13784   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13785   if (token)
13786     asm_specification = token->value;
13787   else
13788     asm_specification = NULL_TREE;
13789
13790   /* Look for the `)'.  */
13791   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13792
13793   return asm_specification;
13794 }
13795
13796 /* Parse an asm-operand-list.
13797
13798    asm-operand-list:
13799      asm-operand
13800      asm-operand-list , asm-operand
13801
13802    asm-operand:
13803      string-literal ( expression )
13804      [ string-literal ] string-literal ( expression )
13805
13806    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13807    each node is the expression.  The TREE_PURPOSE is itself a
13808    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13809    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13810    is a STRING_CST for the string literal before the parenthesis.  */
13811
13812 static tree
13813 cp_parser_asm_operand_list (cp_parser* parser)
13814 {
13815   tree asm_operands = NULL_TREE;
13816
13817   while (true)
13818     {
13819       tree string_literal;
13820       tree expression;
13821       tree name;
13822       cp_token *token;
13823
13824       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13825         {
13826           /* Consume the `[' token.  */
13827           cp_lexer_consume_token (parser->lexer);
13828           /* Read the operand name.  */
13829           name = cp_parser_identifier (parser);
13830           if (name != error_mark_node)
13831             name = build_string (IDENTIFIER_LENGTH (name),
13832                                  IDENTIFIER_POINTER (name));
13833           /* Look for the closing `]'.  */
13834           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13835         }
13836       else
13837         name = NULL_TREE;
13838       /* Look for the string-literal.  */
13839       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13840       string_literal = token ? token->value : error_mark_node;
13841       c_lex_string_translate = 1;
13842       /* Look for the `('.  */
13843       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13844       /* Parse the expression.  */
13845       expression = cp_parser_expression (parser);
13846       /* Look for the `)'.  */
13847       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13848       c_lex_string_translate = 0;
13849       /* Add this operand to the list.  */
13850       asm_operands = tree_cons (build_tree_list (name, string_literal),
13851                                 expression,
13852                                 asm_operands);
13853       /* If the next token is not a `,', there are no more
13854          operands.  */
13855       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13856         break;
13857       /* Consume the `,'.  */
13858       cp_lexer_consume_token (parser->lexer);
13859     }
13860
13861   return nreverse (asm_operands);
13862 }
13863
13864 /* Parse an asm-clobber-list.
13865
13866    asm-clobber-list:
13867      string-literal
13868      asm-clobber-list , string-literal
13869
13870    Returns a TREE_LIST, indicating the clobbers in the order that they
13871    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13872
13873 static tree
13874 cp_parser_asm_clobber_list (cp_parser* parser)
13875 {
13876   tree clobbers = NULL_TREE;
13877
13878   while (true)
13879     {
13880       cp_token *token;
13881       tree string_literal;
13882
13883       /* Look for the string literal.  */
13884       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13885       string_literal = token ? token->value : error_mark_node;
13886       /* Add it to the list.  */
13887       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13888       /* If the next token is not a `,', then the list is
13889          complete.  */
13890       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13891         break;
13892       /* Consume the `,' token.  */
13893       cp_lexer_consume_token (parser->lexer);
13894     }
13895
13896   return clobbers;
13897 }
13898
13899 /* Parse an (optional) series of attributes.
13900
13901    attributes:
13902      attributes attribute
13903
13904    attribute:
13905      __attribute__ (( attribute-list [opt] ))
13906
13907    The return value is as for cp_parser_attribute_list.  */
13908
13909 static tree
13910 cp_parser_attributes_opt (cp_parser* parser)
13911 {
13912   tree attributes = NULL_TREE;
13913
13914   while (true)
13915     {
13916       cp_token *token;
13917       tree attribute_list;
13918
13919       /* Peek at the next token.  */
13920       token = cp_lexer_peek_token (parser->lexer);
13921       /* If it's not `__attribute__', then we're done.  */
13922       if (token->keyword != RID_ATTRIBUTE)
13923         break;
13924
13925       /* Consume the `__attribute__' keyword.  */
13926       cp_lexer_consume_token (parser->lexer);
13927       /* Look for the two `(' tokens.  */
13928       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13929       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13930
13931       /* Peek at the next token.  */
13932       token = cp_lexer_peek_token (parser->lexer);
13933       if (token->type != CPP_CLOSE_PAREN)
13934         /* Parse the attribute-list.  */
13935         attribute_list = cp_parser_attribute_list (parser);
13936       else
13937         /* If the next token is a `)', then there is no attribute
13938            list.  */
13939         attribute_list = NULL;
13940
13941       /* Look for the two `)' tokens.  */
13942       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13943       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13944
13945       /* Add these new attributes to the list.  */
13946       attributes = chainon (attributes, attribute_list);
13947     }
13948
13949   return attributes;
13950 }
13951
13952 /* Parse an attribute-list.
13953
13954    attribute-list:
13955      attribute
13956      attribute-list , attribute
13957
13958    attribute:
13959      identifier
13960      identifier ( identifier )
13961      identifier ( identifier , expression-list )
13962      identifier ( expression-list )
13963
13964    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13965    TREE_PURPOSE of each node is the identifier indicating which
13966    attribute is in use.  The TREE_VALUE represents the arguments, if
13967    any.  */
13968
13969 static tree
13970 cp_parser_attribute_list (cp_parser* parser)
13971 {
13972   tree attribute_list = NULL_TREE;
13973
13974   c_lex_string_translate = 0;
13975   while (true)
13976     {
13977       cp_token *token;
13978       tree identifier;
13979       tree attribute;
13980
13981       /* Look for the identifier.  We also allow keywords here; for
13982          example `__attribute__ ((const))' is legal.  */
13983       token = cp_lexer_peek_token (parser->lexer);
13984       if (token->type != CPP_NAME
13985           && token->type != CPP_KEYWORD)
13986         return error_mark_node;
13987       /* Consume the token.  */
13988       token = cp_lexer_consume_token (parser->lexer);
13989
13990       /* Save away the identifier that indicates which attribute this is.  */
13991       identifier = token->value;
13992       attribute = build_tree_list (identifier, NULL_TREE);
13993
13994       /* Peek at the next token.  */
13995       token = cp_lexer_peek_token (parser->lexer);
13996       /* If it's an `(', then parse the attribute arguments.  */
13997       if (token->type == CPP_OPEN_PAREN)
13998         {
13999           tree arguments;
14000
14001           arguments = (cp_parser_parenthesized_expression_list
14002                        (parser, true, /*non_constant_p=*/NULL));
14003           /* Save the identifier and arguments away.  */
14004           TREE_VALUE (attribute) = arguments;
14005         }
14006
14007       /* Add this attribute to the list.  */
14008       TREE_CHAIN (attribute) = attribute_list;
14009       attribute_list = attribute;
14010
14011       /* Now, look for more attributes.  */
14012       token = cp_lexer_peek_token (parser->lexer);
14013       /* If the next token isn't a `,', we're done.  */
14014       if (token->type != CPP_COMMA)
14015         break;
14016
14017       /* Consume the comma and keep going.  */
14018       cp_lexer_consume_token (parser->lexer);
14019     }
14020   c_lex_string_translate = 1;
14021
14022   /* We built up the list in reverse order.  */
14023   return nreverse (attribute_list);
14024 }
14025
14026 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14027    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14028    current value of the PEDANTIC flag, regardless of whether or not
14029    the `__extension__' keyword is present.  The caller is responsible
14030    for restoring the value of the PEDANTIC flag.  */
14031
14032 static bool
14033 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14034 {
14035   /* Save the old value of the PEDANTIC flag.  */
14036   *saved_pedantic = pedantic;
14037
14038   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14039     {
14040       /* Consume the `__extension__' token.  */
14041       cp_lexer_consume_token (parser->lexer);
14042       /* We're not being pedantic while the `__extension__' keyword is
14043          in effect.  */
14044       pedantic = 0;
14045
14046       return true;
14047     }
14048
14049   return false;
14050 }
14051
14052 /* Parse a label declaration.
14053
14054    label-declaration:
14055      __label__ label-declarator-seq ;
14056
14057    label-declarator-seq:
14058      identifier , label-declarator-seq
14059      identifier  */
14060
14061 static void
14062 cp_parser_label_declaration (cp_parser* parser)
14063 {
14064   /* Look for the `__label__' keyword.  */
14065   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14066
14067   while (true)
14068     {
14069       tree identifier;
14070
14071       /* Look for an identifier.  */
14072       identifier = cp_parser_identifier (parser);
14073       /* Declare it as a lobel.  */
14074       finish_label_decl (identifier);
14075       /* If the next token is a `;', stop.  */
14076       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14077         break;
14078       /* Look for the `,' separating the label declarations.  */
14079       cp_parser_require (parser, CPP_COMMA, "`,'");
14080     }
14081
14082   /* Look for the final `;'.  */
14083   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14084 }
14085
14086 /* Support Functions */
14087
14088 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14089    NAME should have one of the representations used for an
14090    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14091    is returned.  If PARSER->SCOPE is a dependent type, then a
14092    SCOPE_REF is returned.
14093
14094    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14095    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14096    was formed.  Abstractly, such entities should not be passed to this
14097    function, because they do not need to be looked up, but it is
14098    simpler to check for this special case here, rather than at the
14099    call-sites.
14100
14101    In cases not explicitly covered above, this function returns a
14102    DECL, OVERLOAD, or baselink representing the result of the lookup.
14103    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14104    is returned.
14105
14106    If IS_TYPE is TRUE, bindings that do not refer to types are
14107    ignored.
14108
14109    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14110    ignored.
14111
14112    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14113    are ignored.
14114
14115    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14116    types.  */
14117
14118 static tree
14119 cp_parser_lookup_name (cp_parser *parser, tree name,
14120                        bool is_type, bool is_template, bool is_namespace,
14121                        bool check_dependency)
14122 {
14123   tree decl;
14124   tree object_type = parser->context->object_type;
14125
14126   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14127      no longer valid.  Note that if we are parsing tentatively, and
14128      the parse fails, OBJECT_TYPE will be automatically restored.  */
14129   parser->context->object_type = NULL_TREE;
14130
14131   if (name == error_mark_node)
14132     return error_mark_node;
14133
14134   /* A template-id has already been resolved; there is no lookup to
14135      do.  */
14136   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14137     return name;
14138   if (BASELINK_P (name))
14139     {
14140       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
14141                            == TEMPLATE_ID_EXPR),
14142                           20020909);
14143       return name;
14144     }
14145
14146   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14147      it should already have been checked to make sure that the name
14148      used matches the type being destroyed.  */
14149   if (TREE_CODE (name) == BIT_NOT_EXPR)
14150     {
14151       tree type;
14152
14153       /* Figure out to which type this destructor applies.  */
14154       if (parser->scope)
14155         type = parser->scope;
14156       else if (object_type)
14157         type = object_type;
14158       else
14159         type = current_class_type;
14160       /* If that's not a class type, there is no destructor.  */
14161       if (!type || !CLASS_TYPE_P (type))
14162         return error_mark_node;
14163       if (!CLASSTYPE_DESTRUCTORS (type))
14164           return error_mark_node;
14165       /* If it was a class type, return the destructor.  */
14166       return CLASSTYPE_DESTRUCTORS (type);
14167     }
14168
14169   /* By this point, the NAME should be an ordinary identifier.  If
14170      the id-expression was a qualified name, the qualifying scope is
14171      stored in PARSER->SCOPE at this point.  */
14172   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
14173                       20000619);
14174
14175   /* Perform the lookup.  */
14176   if (parser->scope)
14177     {
14178       bool dependent_p;
14179
14180       if (parser->scope == error_mark_node)
14181         return error_mark_node;
14182
14183       /* If the SCOPE is dependent, the lookup must be deferred until
14184          the template is instantiated -- unless we are explicitly
14185          looking up names in uninstantiated templates.  Even then, we
14186          cannot look up the name if the scope is not a class type; it
14187          might, for example, be a template type parameter.  */
14188       dependent_p = (TYPE_P (parser->scope)
14189                      && !(parser->in_declarator_p
14190                           && currently_open_class (parser->scope))
14191                      && dependent_type_p (parser->scope));
14192       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14193            && dependent_p)
14194         {
14195           if (is_type)
14196             /* The resolution to Core Issue 180 says that `struct A::B'
14197                should be considered a type-name, even if `A' is
14198                dependent.  */
14199             decl = TYPE_NAME (make_typename_type (parser->scope,
14200                                                   name,
14201                                                   /*complain=*/1));
14202           else if (is_template)
14203             decl = make_unbound_class_template (parser->scope,
14204                                                 name,
14205                                                 /*complain=*/1);
14206           else
14207             decl = build_nt (SCOPE_REF, parser->scope, name);
14208         }
14209       else
14210         {
14211           bool pop_p = false;
14212
14213           /* If PARSER->SCOPE is a dependent type, then it must be a
14214              class type, and we must not be checking dependencies;
14215              otherwise, we would have processed this lookup above.  So
14216              that PARSER->SCOPE is not considered a dependent base by
14217              lookup_member, we must enter the scope here.  */
14218           if (dependent_p)
14219             pop_p = push_scope (parser->scope);
14220           /* If the PARSER->SCOPE is a a template specialization, it
14221              may be instantiated during name lookup.  In that case,
14222              errors may be issued.  Even if we rollback the current
14223              tentative parse, those errors are valid.  */
14224           decl = lookup_qualified_name (parser->scope, name, is_type,
14225                                         /*complain=*/true);
14226           if (pop_p)
14227             pop_scope (parser->scope);
14228         }
14229       parser->qualifying_scope = parser->scope;
14230       parser->object_scope = NULL_TREE;
14231     }
14232   else if (object_type)
14233     {
14234       tree object_decl = NULL_TREE;
14235       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14236          OBJECT_TYPE is not a class.  */
14237       if (CLASS_TYPE_P (object_type))
14238         /* If the OBJECT_TYPE is a template specialization, it may
14239            be instantiated during name lookup.  In that case, errors
14240            may be issued.  Even if we rollback the current tentative
14241            parse, those errors are valid.  */
14242         object_decl = lookup_member (object_type,
14243                                      name,
14244                                      /*protect=*/0, is_type);
14245       /* Look it up in the enclosing context, too.  */
14246       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14247                                is_namespace,
14248                                /*flags=*/0);
14249       parser->object_scope = object_type;
14250       parser->qualifying_scope = NULL_TREE;
14251       if (object_decl)
14252         decl = object_decl;
14253     }
14254   else
14255     {
14256       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14257                                is_namespace,
14258                                /*flags=*/0);
14259       parser->qualifying_scope = NULL_TREE;
14260       parser->object_scope = NULL_TREE;
14261     }
14262
14263   /* If the lookup failed, let our caller know.  */
14264   if (!decl
14265       || decl == error_mark_node
14266       || (TREE_CODE (decl) == FUNCTION_DECL
14267           && DECL_ANTICIPATED (decl)))
14268     return error_mark_node;
14269
14270   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14271   if (TREE_CODE (decl) == TREE_LIST)
14272     {
14273       /* The error message we have to print is too complicated for
14274          cp_parser_error, so we incorporate its actions directly.  */
14275       if (!cp_parser_simulate_error (parser))
14276         {
14277           error ("reference to `%D' is ambiguous", name);
14278           print_candidates (decl);
14279         }
14280       return error_mark_node;
14281     }
14282
14283   my_friendly_assert (DECL_P (decl)
14284                       || TREE_CODE (decl) == OVERLOAD
14285                       || TREE_CODE (decl) == SCOPE_REF
14286                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14287                       || BASELINK_P (decl),
14288                       20000619);
14289
14290   /* If we have resolved the name of a member declaration, check to
14291      see if the declaration is accessible.  When the name resolves to
14292      set of overloaded functions, accessibility is checked when
14293      overload resolution is done.
14294
14295      During an explicit instantiation, access is not checked at all,
14296      as per [temp.explicit].  */
14297   if (DECL_P (decl))
14298     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14299
14300   return decl;
14301 }
14302
14303 /* Like cp_parser_lookup_name, but for use in the typical case where
14304    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14305    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14306
14307 static tree
14308 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14309 {
14310   return cp_parser_lookup_name (parser, name,
14311                                 /*is_type=*/false,
14312                                 /*is_template=*/false,
14313                                 /*is_namespace=*/false,
14314                                 /*check_dependency=*/true);
14315 }
14316
14317 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14318    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14319    true, the DECL indicates the class being defined in a class-head,
14320    or declared in an elaborated-type-specifier.
14321
14322    Otherwise, return DECL.  */
14323
14324 static tree
14325 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14326 {
14327   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14328      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14329
14330        struct A {
14331          template <typename T> struct B;
14332        };
14333
14334        template <typename T> struct A::B {};
14335
14336      Similarly, in a elaborated-type-specifier:
14337
14338        namespace N { struct X{}; }
14339
14340        struct A {
14341          template <typename T> friend struct N::X;
14342        };
14343
14344      However, if the DECL refers to a class type, and we are in
14345      the scope of the class, then the name lookup automatically
14346      finds the TYPE_DECL created by build_self_reference rather
14347      than a TEMPLATE_DECL.  For example, in:
14348
14349        template <class T> struct S {
14350          S s;
14351        };
14352
14353      there is no need to handle such case.  */
14354
14355   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14356     return DECL_TEMPLATE_RESULT (decl);
14357
14358   return decl;
14359 }
14360
14361 /* If too many, or too few, template-parameter lists apply to the
14362    declarator, issue an error message.  Returns TRUE if all went well,
14363    and FALSE otherwise.  */
14364
14365 static bool
14366 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14367                                                 cp_declarator *declarator)
14368 {
14369   unsigned num_templates;
14370
14371   /* We haven't seen any classes that involve template parameters yet.  */
14372   num_templates = 0;
14373
14374   switch (declarator->kind)
14375     {
14376     case cdk_id:
14377       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14378         {
14379           tree scope;
14380           tree member;
14381
14382           scope = TREE_OPERAND (declarator->u.id.name, 0);
14383           member = TREE_OPERAND (declarator->u.id.name, 1);
14384
14385           while (scope && CLASS_TYPE_P (scope))
14386             {
14387               /* You're supposed to have one `template <...>'
14388                  for every template class, but you don't need one
14389                  for a full specialization.  For example:
14390
14391                  template <class T> struct S{};
14392                  template <> struct S<int> { void f(); };
14393                  void S<int>::f () {}
14394
14395                  is correct; there shouldn't be a `template <>' for
14396                  the definition of `S<int>::f'.  */
14397               if (CLASSTYPE_TEMPLATE_INFO (scope)
14398                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14399                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14400                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14401                 ++num_templates;
14402
14403               scope = TYPE_CONTEXT (scope);
14404             }
14405         }
14406
14407       /* If the DECLARATOR has the form `X<y>' then it uses one
14408          additional level of template parameters.  */
14409       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14410         ++num_templates;
14411
14412       return cp_parser_check_template_parameters (parser,
14413                                                   num_templates);
14414
14415     case cdk_function:
14416     case cdk_array:
14417     case cdk_pointer:
14418     case cdk_reference:
14419     case cdk_ptrmem:
14420       return (cp_parser_check_declarator_template_parameters 
14421               (parser, declarator->declarator));
14422
14423     case cdk_error:
14424       return true;
14425
14426     default:
14427       abort ();
14428       return false;
14429     }
14430 }
14431
14432 /* NUM_TEMPLATES were used in the current declaration.  If that is
14433    invalid, return FALSE and issue an error messages.  Otherwise,
14434    return TRUE.  */
14435
14436 static bool
14437 cp_parser_check_template_parameters (cp_parser* parser,
14438                                      unsigned num_templates)
14439 {
14440   /* If there are more template classes than parameter lists, we have
14441      something like:
14442
14443        template <class T> void S<T>::R<T>::f ();  */
14444   if (parser->num_template_parameter_lists < num_templates)
14445     {
14446       error ("too few template-parameter-lists");
14447       return false;
14448     }
14449   /* If there are the same number of template classes and parameter
14450      lists, that's OK.  */
14451   if (parser->num_template_parameter_lists == num_templates)
14452     return true;
14453   /* If there are more, but only one more, then we are referring to a
14454      member template.  That's OK too.  */
14455   if (parser->num_template_parameter_lists == num_templates + 1)
14456       return true;
14457   /* Otherwise, there are too many template parameter lists.  We have
14458      something like:
14459
14460      template <class T> template <class U> void S::f();  */
14461   error ("too many template-parameter-lists");
14462   return false;
14463 }
14464
14465 /* Parse a binary-expression of the general form:
14466
14467    binary-expression:
14468      <expr>
14469      binary-expression <token> <expr>
14470
14471    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14472    to parser the <expr>s.  If the first production is used, then the
14473    value returned by FN is returned directly.  Otherwise, a node with
14474    the indicated EXPR_TYPE is returned, with operands corresponding to
14475    the two sub-expressions.  */
14476
14477 static tree
14478 cp_parser_binary_expression (cp_parser* parser,
14479                              const cp_parser_token_tree_map token_tree_map,
14480                              cp_parser_expression_fn fn)
14481 {
14482   tree lhs;
14483
14484   /* Parse the first expression.  */
14485   lhs = (*fn) (parser);
14486   /* Now, look for more expressions.  */
14487   while (true)
14488     {
14489       cp_token *token;
14490       const cp_parser_token_tree_map_node *map_node;
14491       tree rhs;
14492
14493       /* Peek at the next token.  */
14494       token = cp_lexer_peek_token (parser->lexer);
14495       /* If the token is `>', and that's not an operator at the
14496          moment, then we're done.  */
14497       if (token->type == CPP_GREATER
14498           && !parser->greater_than_is_operator_p)
14499         break;
14500       /* If we find one of the tokens we want, build the corresponding
14501          tree representation.  */
14502       for (map_node = token_tree_map;
14503            map_node->token_type != CPP_EOF;
14504            ++map_node)
14505         if (map_node->token_type == token->type)
14506           {
14507             /* Assume that an overloaded operator will not be used.  */
14508             bool overloaded_p = false;
14509
14510             /* Consume the operator token.  */
14511             cp_lexer_consume_token (parser->lexer);
14512             /* Parse the right-hand side of the expression.  */
14513             rhs = (*fn) (parser);
14514             /* Build the binary tree node.  */
14515             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs, 
14516                                      &overloaded_p);
14517             /* If the binary operator required the use of an
14518                overloaded operator, then this expression cannot be an
14519                integral constant-expression.  An overloaded operator
14520                can be used even if both operands are otherwise
14521                permissible in an integral constant-expression if at
14522                least one of the operands is of enumeration type.  */
14523             if (overloaded_p
14524                 && (cp_parser_non_integral_constant_expression 
14525                     (parser, "calls to overloaded operators")))
14526               lhs = error_mark_node;
14527             break;
14528           }
14529
14530       /* If the token wasn't one of the ones we want, we're done.  */
14531       if (map_node->token_type == CPP_EOF)
14532         break;
14533     }
14534
14535   return lhs;
14536 }
14537
14538 /* Parse an optional `::' token indicating that the following name is
14539    from the global namespace.  If so, PARSER->SCOPE is set to the
14540    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14541    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14542    Returns the new value of PARSER->SCOPE, if the `::' token is
14543    present, and NULL_TREE otherwise.  */
14544
14545 static tree
14546 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14547 {
14548   cp_token *token;
14549
14550   /* Peek at the next token.  */
14551   token = cp_lexer_peek_token (parser->lexer);
14552   /* If we're looking at a `::' token then we're starting from the
14553      global namespace, not our current location.  */
14554   if (token->type == CPP_SCOPE)
14555     {
14556       /* Consume the `::' token.  */
14557       cp_lexer_consume_token (parser->lexer);
14558       /* Set the SCOPE so that we know where to start the lookup.  */
14559       parser->scope = global_namespace;
14560       parser->qualifying_scope = global_namespace;
14561       parser->object_scope = NULL_TREE;
14562
14563       return parser->scope;
14564     }
14565   else if (!current_scope_valid_p)
14566     {
14567       parser->scope = NULL_TREE;
14568       parser->qualifying_scope = NULL_TREE;
14569       parser->object_scope = NULL_TREE;
14570     }
14571
14572   return NULL_TREE;
14573 }
14574
14575 /* Returns TRUE if the upcoming token sequence is the start of a
14576    constructor declarator.  If FRIEND_P is true, the declarator is
14577    preceded by the `friend' specifier.  */
14578
14579 static bool
14580 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14581 {
14582   bool constructor_p;
14583   tree type_decl = NULL_TREE;
14584   bool nested_name_p;
14585   cp_token *next_token;
14586
14587   /* The common case is that this is not a constructor declarator, so
14588      try to avoid doing lots of work if at all possible.  It's not
14589      valid declare a constructor at function scope.  */
14590   if (at_function_scope_p ())
14591     return false;
14592   /* And only certain tokens can begin a constructor declarator.  */
14593   next_token = cp_lexer_peek_token (parser->lexer);
14594   if (next_token->type != CPP_NAME
14595       && next_token->type != CPP_SCOPE
14596       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14597       && next_token->type != CPP_TEMPLATE_ID)
14598     return false;
14599
14600   /* Parse tentatively; we are going to roll back all of the tokens
14601      consumed here.  */
14602   cp_parser_parse_tentatively (parser);
14603   /* Assume that we are looking at a constructor declarator.  */
14604   constructor_p = true;
14605
14606   /* Look for the optional `::' operator.  */
14607   cp_parser_global_scope_opt (parser,
14608                               /*current_scope_valid_p=*/false);
14609   /* Look for the nested-name-specifier.  */
14610   nested_name_p
14611     = (cp_parser_nested_name_specifier_opt (parser,
14612                                             /*typename_keyword_p=*/false,
14613                                             /*check_dependency_p=*/false,
14614                                             /*type_p=*/false,
14615                                             /*is_declaration=*/false)
14616        != NULL_TREE);
14617   /* Outside of a class-specifier, there must be a
14618      nested-name-specifier.  */
14619   if (!nested_name_p &&
14620       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14621        || friend_p))
14622     constructor_p = false;
14623   /* If we still think that this might be a constructor-declarator,
14624      look for a class-name.  */
14625   if (constructor_p)
14626     {
14627       /* If we have:
14628
14629            template <typename T> struct S { S(); };
14630            template <typename T> S<T>::S ();
14631
14632          we must recognize that the nested `S' names a class.
14633          Similarly, for:
14634
14635            template <typename T> S<T>::S<T> ();
14636
14637          we must recognize that the nested `S' names a template.  */
14638       type_decl = cp_parser_class_name (parser,
14639                                         /*typename_keyword_p=*/false,
14640                                         /*template_keyword_p=*/false,
14641                                         /*type_p=*/false,
14642                                         /*check_dependency_p=*/false,
14643                                         /*class_head_p=*/false,
14644                                         /*is_declaration=*/false);
14645       /* If there was no class-name, then this is not a constructor.  */
14646       constructor_p = !cp_parser_error_occurred (parser);
14647     }
14648
14649   /* If we're still considering a constructor, we have to see a `(',
14650      to begin the parameter-declaration-clause, followed by either a
14651      `)', an `...', or a decl-specifier.  We need to check for a
14652      type-specifier to avoid being fooled into thinking that:
14653
14654        S::S (f) (int);
14655
14656      is a constructor.  (It is actually a function named `f' that
14657      takes one parameter (of type `int') and returns a value of type
14658      `S::S'.  */
14659   if (constructor_p
14660       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14661     {
14662       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14663           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14664           /* A parameter declaration begins with a decl-specifier,
14665              which is either the "attribute" keyword, a storage class
14666              specifier, or (usually) a type-specifier.  */
14667           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14668           && !cp_parser_storage_class_specifier_opt (parser))
14669         {
14670           tree type;
14671           bool pop_p = false;
14672           unsigned saved_num_template_parameter_lists;
14673
14674           /* Names appearing in the type-specifier should be looked up
14675              in the scope of the class.  */
14676           if (current_class_type)
14677             type = NULL_TREE;
14678           else
14679             {
14680               type = TREE_TYPE (type_decl);
14681               if (TREE_CODE (type) == TYPENAME_TYPE)
14682                 {
14683                   type = resolve_typename_type (type,
14684                                                 /*only_current_p=*/false);
14685                   if (type == error_mark_node)
14686                     {
14687                       cp_parser_abort_tentative_parse (parser);
14688                       return false;
14689                     }
14690                 }
14691               pop_p = push_scope (type);
14692             }
14693
14694           /* Inside the constructor parameter list, surrounding
14695              template-parameter-lists do not apply.  */
14696           saved_num_template_parameter_lists
14697             = parser->num_template_parameter_lists;
14698           parser->num_template_parameter_lists = 0;
14699
14700           /* Look for the type-specifier.  */
14701           cp_parser_type_specifier (parser,
14702                                     CP_PARSER_FLAGS_NONE,
14703                                     /*decl_specs=*/NULL,
14704                                     /*is_declarator=*/true,
14705                                     /*declares_class_or_enum=*/NULL,
14706                                     /*is_cv_qualifier=*/NULL);
14707
14708           parser->num_template_parameter_lists
14709             = saved_num_template_parameter_lists;
14710
14711           /* Leave the scope of the class.  */
14712           if (pop_p)
14713             pop_scope (type);
14714
14715           constructor_p = !cp_parser_error_occurred (parser);
14716         }
14717     }
14718   else
14719     constructor_p = false;
14720   /* We did not really want to consume any tokens.  */
14721   cp_parser_abort_tentative_parse (parser);
14722
14723   return constructor_p;
14724 }
14725
14726 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14727    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14728    they must be performed once we are in the scope of the function.
14729
14730    Returns the function defined.  */
14731
14732 static tree
14733 cp_parser_function_definition_from_specifiers_and_declarator
14734   (cp_parser* parser,
14735    cp_decl_specifier_seq *decl_specifiers,
14736    tree attributes,
14737    const cp_declarator *declarator)
14738 {
14739   tree fn;
14740   bool success_p;
14741
14742   /* Begin the function-definition.  */
14743   success_p = start_function (decl_specifiers, declarator, attributes);
14744
14745   /* The things we're about to see are not directly qualified by any
14746      template headers we've seen thus far.  */
14747   reset_specialization ();
14748
14749   /* If there were names looked up in the decl-specifier-seq that we
14750      did not check, check them now.  We must wait until we are in the
14751      scope of the function to perform the checks, since the function
14752      might be a friend.  */
14753   perform_deferred_access_checks ();
14754
14755   if (!success_p)
14756     {
14757       /* Skip the entire function.  */
14758       error ("invalid function declaration");
14759       cp_parser_skip_to_end_of_block_or_statement (parser);
14760       fn = error_mark_node;
14761     }
14762   else
14763     fn = cp_parser_function_definition_after_declarator (parser,
14764                                                          /*inline_p=*/false);
14765
14766   return fn;
14767 }
14768
14769 /* Parse the part of a function-definition that follows the
14770    declarator.  INLINE_P is TRUE iff this function is an inline
14771    function defined with a class-specifier.
14772
14773    Returns the function defined.  */
14774
14775 static tree
14776 cp_parser_function_definition_after_declarator (cp_parser* parser,
14777                                                 bool inline_p)
14778 {
14779   tree fn;
14780   bool ctor_initializer_p = false;
14781   bool saved_in_unbraced_linkage_specification_p;
14782   unsigned saved_num_template_parameter_lists;
14783
14784   /* If the next token is `return', then the code may be trying to
14785      make use of the "named return value" extension that G++ used to
14786      support.  */
14787   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14788     {
14789       /* Consume the `return' keyword.  */
14790       cp_lexer_consume_token (parser->lexer);
14791       /* Look for the identifier that indicates what value is to be
14792          returned.  */
14793       cp_parser_identifier (parser);
14794       /* Issue an error message.  */
14795       error ("named return values are no longer supported");
14796       /* Skip tokens until we reach the start of the function body.  */
14797       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14798              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14799         cp_lexer_consume_token (parser->lexer);
14800     }
14801   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14802      anything declared inside `f'.  */
14803   saved_in_unbraced_linkage_specification_p
14804     = parser->in_unbraced_linkage_specification_p;
14805   parser->in_unbraced_linkage_specification_p = false;
14806   /* Inside the function, surrounding template-parameter-lists do not
14807      apply.  */
14808   saved_num_template_parameter_lists
14809     = parser->num_template_parameter_lists;
14810   parser->num_template_parameter_lists = 0;
14811   /* If the next token is `try', then we are looking at a
14812      function-try-block.  */
14813   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14814     ctor_initializer_p = cp_parser_function_try_block (parser);
14815   /* A function-try-block includes the function-body, so we only do
14816      this next part if we're not processing a function-try-block.  */
14817   else
14818     ctor_initializer_p
14819       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14820
14821   /* Finish the function.  */
14822   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14823                         (inline_p ? 2 : 0));
14824   /* Generate code for it, if necessary.  */
14825   expand_or_defer_fn (fn);
14826   /* Restore the saved values.  */
14827   parser->in_unbraced_linkage_specification_p
14828     = saved_in_unbraced_linkage_specification_p;
14829   parser->num_template_parameter_lists
14830     = saved_num_template_parameter_lists;
14831
14832   return fn;
14833 }
14834
14835 /* Parse a template-declaration, assuming that the `export' (and
14836    `extern') keywords, if present, has already been scanned.  MEMBER_P
14837    is as for cp_parser_template_declaration.  */
14838
14839 static void
14840 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14841 {
14842   tree decl = NULL_TREE;
14843   tree parameter_list;
14844   bool friend_p = false;
14845
14846   /* Look for the `template' keyword.  */
14847   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14848     return;
14849
14850   /* And the `<'.  */
14851   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14852     return;
14853
14854   /* If the next token is `>', then we have an invalid
14855      specialization.  Rather than complain about an invalid template
14856      parameter, issue an error message here.  */
14857   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14858     {
14859       cp_parser_error (parser, "invalid explicit specialization");
14860       begin_specialization ();
14861       parameter_list = NULL_TREE;
14862     }
14863   else
14864     {
14865       /* Parse the template parameters.  */
14866       begin_template_parm_list ();
14867       parameter_list = cp_parser_template_parameter_list (parser);
14868       parameter_list = end_template_parm_list (parameter_list);
14869     }
14870
14871   /* Look for the `>'.  */
14872   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14873   /* We just processed one more parameter list.  */
14874   ++parser->num_template_parameter_lists;
14875   /* If the next token is `template', there are more template
14876      parameters.  */
14877   if (cp_lexer_next_token_is_keyword (parser->lexer,
14878                                       RID_TEMPLATE))
14879     cp_parser_template_declaration_after_export (parser, member_p);
14880   else
14881     {
14882       decl = cp_parser_single_declaration (parser,
14883                                            member_p,
14884                                            &friend_p);
14885
14886       /* If this is a member template declaration, let the front
14887          end know.  */
14888       if (member_p && !friend_p && decl)
14889         {
14890           if (TREE_CODE (decl) == TYPE_DECL)
14891             cp_parser_check_access_in_redeclaration (decl);
14892
14893           decl = finish_member_template_decl (decl);
14894         }
14895       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14896         make_friend_class (current_class_type, TREE_TYPE (decl),
14897                            /*complain=*/true);
14898     }
14899   /* We are done with the current parameter list.  */
14900   --parser->num_template_parameter_lists;
14901
14902   /* Finish up.  */
14903   finish_template_decl (parameter_list);
14904
14905   /* Register member declarations.  */
14906   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14907     finish_member_declaration (decl);
14908
14909   /* If DECL is a function template, we must return to parse it later.
14910      (Even though there is no definition, there might be default
14911      arguments that need handling.)  */
14912   if (member_p && decl
14913       && (TREE_CODE (decl) == FUNCTION_DECL
14914           || DECL_FUNCTION_TEMPLATE_P (decl)))
14915     TREE_VALUE (parser->unparsed_functions_queues)
14916       = tree_cons (NULL_TREE, decl,
14917                    TREE_VALUE (parser->unparsed_functions_queues));
14918 }
14919
14920 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14921    `function-definition' sequence.  MEMBER_P is true, this declaration
14922    appears in a class scope.
14923
14924    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14925    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14926
14927 static tree
14928 cp_parser_single_declaration (cp_parser* parser,
14929                               bool member_p,
14930                               bool* friend_p)
14931 {
14932   int declares_class_or_enum;
14933   tree decl = NULL_TREE;
14934   cp_decl_specifier_seq decl_specifiers;
14935   bool function_definition_p = false;
14936
14937   /* Defer access checks until we know what is being declared.  */
14938   push_deferring_access_checks (dk_deferred);
14939
14940   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14941      alternative.  */
14942   cp_parser_decl_specifier_seq (parser,
14943                                 CP_PARSER_FLAGS_OPTIONAL,
14944                                 &decl_specifiers,
14945                                 &declares_class_or_enum);
14946   if (friend_p)
14947     *friend_p = cp_parser_friend_p (&decl_specifiers);
14948   /* Gather up the access checks that occurred the
14949      decl-specifier-seq.  */
14950   stop_deferring_access_checks ();
14951
14952   /* Check for the declaration of a template class.  */
14953   if (declares_class_or_enum)
14954     {
14955       if (cp_parser_declares_only_class_p (parser))
14956         {
14957           decl = shadow_tag (&decl_specifiers);
14958           if (decl && decl != error_mark_node)
14959             decl = TYPE_NAME (decl);
14960           else
14961             decl = error_mark_node;
14962         }
14963     }
14964   else
14965     decl = NULL_TREE;
14966   /* If it's not a template class, try for a template function.  If
14967      the next token is a `;', then this declaration does not declare
14968      anything.  But, if there were errors in the decl-specifiers, then
14969      the error might well have come from an attempted class-specifier.
14970      In that case, there's no need to warn about a missing declarator.  */
14971   if (!decl
14972       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14973           || decl_specifiers.type != error_mark_node))
14974     decl = cp_parser_init_declarator (parser,
14975                                       &decl_specifiers,
14976                                       /*function_definition_allowed_p=*/true,
14977                                       member_p,
14978                                       declares_class_or_enum,
14979                                       &function_definition_p);
14980
14981   pop_deferring_access_checks ();
14982
14983   /* Clear any current qualification; whatever comes next is the start
14984      of something new.  */
14985   parser->scope = NULL_TREE;
14986   parser->qualifying_scope = NULL_TREE;
14987   parser->object_scope = NULL_TREE;
14988   /* Look for a trailing `;' after the declaration.  */
14989   if (!function_definition_p
14990       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14991     cp_parser_skip_to_end_of_block_or_statement (parser);
14992
14993   return decl;
14994 }
14995
14996 /* Parse a cast-expression that is not the operand of a unary "&".  */
14997
14998 static tree
14999 cp_parser_simple_cast_expression (cp_parser *parser)
15000 {
15001   return cp_parser_cast_expression (parser, /*address_p=*/false);
15002 }
15003
15004 /* Parse a functional cast to TYPE.  Returns an expression
15005    representing the cast.  */
15006
15007 static tree
15008 cp_parser_functional_cast (cp_parser* parser, tree type)
15009 {
15010   tree expression_list;
15011   tree cast;
15012
15013   expression_list
15014     = cp_parser_parenthesized_expression_list (parser, false,
15015                                                /*non_constant_p=*/NULL);
15016
15017   cast = build_functional_cast (type, expression_list);
15018   /* [expr.const]/1: In an integral constant expression "only type
15019      conversions to integral or enumeration type can be used".  */
15020   if (cast != error_mark_node && !type_dependent_expression_p (type) 
15021       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15022     {
15023       if (cp_parser_non_integral_constant_expression 
15024           (parser, "a call to a constructor"))
15025         return error_mark_node;
15026     }
15027   return cast;
15028 }
15029
15030 /* Save the tokens that make up the body of a member function defined
15031    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15032    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15033    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15034    for the member function.  */
15035
15036 static tree
15037 cp_parser_save_member_function_body (cp_parser* parser,
15038                                      cp_decl_specifier_seq *decl_specifiers,
15039                                      cp_declarator *declarator,
15040                                      tree attributes)
15041 {
15042   cp_token_cache *cache;
15043   tree fn;
15044
15045   /* Create the function-declaration.  */
15046   fn = start_method (decl_specifiers, declarator, attributes);
15047   /* If something went badly wrong, bail out now.  */
15048   if (fn == error_mark_node)
15049     {
15050       /* If there's a function-body, skip it.  */
15051       if (cp_parser_token_starts_function_definition_p
15052           (cp_lexer_peek_token (parser->lexer)))
15053         cp_parser_skip_to_end_of_block_or_statement (parser);
15054       return error_mark_node;
15055     }
15056
15057   /* Remember it, if there default args to post process.  */
15058   cp_parser_save_default_args (parser, fn);
15059
15060   /* Create a token cache.  */
15061   cache = cp_token_cache_new ();
15062   /* Save away the tokens that make up the body of the
15063      function.  */
15064   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15065   /* Handle function try blocks.  */
15066   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15067     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15068
15069   /* Save away the inline definition; we will process it when the
15070      class is complete.  */
15071   DECL_PENDING_INLINE_INFO (fn) = cache;
15072   DECL_PENDING_INLINE_P (fn) = 1;
15073
15074   /* We need to know that this was defined in the class, so that
15075      friend templates are handled correctly.  */
15076   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15077
15078   /* We're done with the inline definition.  */
15079   finish_method (fn);
15080
15081   /* Add FN to the queue of functions to be parsed later.  */
15082   TREE_VALUE (parser->unparsed_functions_queues)
15083     = tree_cons (NULL_TREE, fn,
15084                  TREE_VALUE (parser->unparsed_functions_queues));
15085
15086   return fn;
15087 }
15088
15089 /* Parse a template-argument-list, as well as the trailing ">" (but
15090    not the opening ">").  See cp_parser_template_argument_list for the
15091    return value.  */
15092
15093 static tree
15094 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15095 {
15096   tree arguments;
15097   tree saved_scope;
15098   tree saved_qualifying_scope;
15099   tree saved_object_scope;
15100   bool saved_greater_than_is_operator_p;
15101
15102   /* [temp.names]
15103
15104      When parsing a template-id, the first non-nested `>' is taken as
15105      the end of the template-argument-list rather than a greater-than
15106      operator.  */
15107   saved_greater_than_is_operator_p
15108     = parser->greater_than_is_operator_p;
15109   parser->greater_than_is_operator_p = false;
15110   /* Parsing the argument list may modify SCOPE, so we save it
15111      here.  */
15112   saved_scope = parser->scope;
15113   saved_qualifying_scope = parser->qualifying_scope;
15114   saved_object_scope = parser->object_scope;
15115   /* Parse the template-argument-list itself.  */
15116   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15117     arguments = NULL_TREE;
15118   else
15119     arguments = cp_parser_template_argument_list (parser);
15120   /* Look for the `>' that ends the template-argument-list. If we find
15121      a '>>' instead, it's probably just a typo.  */
15122   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15123     {
15124       if (!saved_greater_than_is_operator_p)
15125         {
15126           /* If we're in a nested template argument list, the '>>' has to be
15127             a typo for '> >'. We emit the error message, but we continue
15128             parsing and we push a '>' as next token, so that the argument
15129             list will be parsed correctly..  */
15130           cp_token* token;
15131           error ("`>>' should be `> >' within a nested template argument list");
15132           token = cp_lexer_peek_token (parser->lexer);
15133           token->type = CPP_GREATER;
15134         }
15135       else
15136         {
15137           /* If this is not a nested template argument list, the '>>' is
15138             a typo for '>'. Emit an error message and continue.  */
15139           error ("spurious `>>', use `>' to terminate a template argument list");
15140           cp_lexer_consume_token (parser->lexer);
15141         }
15142     }
15143   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15144     error ("missing `>' to terminate the template argument list");
15145   /* The `>' token might be a greater-than operator again now.  */
15146   parser->greater_than_is_operator_p
15147     = saved_greater_than_is_operator_p;
15148   /* Restore the SAVED_SCOPE.  */
15149   parser->scope = saved_scope;
15150   parser->qualifying_scope = saved_qualifying_scope;
15151   parser->object_scope = saved_object_scope;
15152
15153   return arguments;
15154 }
15155
15156 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15157    arguments, or the body of the function have not yet been parsed,
15158    parse them now.  */
15159
15160 static void
15161 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15162 {
15163   cp_lexer *saved_lexer;
15164
15165   /* If this member is a template, get the underlying
15166      FUNCTION_DECL.  */
15167   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15168     member_function = DECL_TEMPLATE_RESULT (member_function);
15169
15170   /* There should not be any class definitions in progress at this
15171      point; the bodies of members are only parsed outside of all class
15172      definitions.  */
15173   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
15174   /* While we're parsing the member functions we might encounter more
15175      classes.  We want to handle them right away, but we don't want
15176      them getting mixed up with functions that are currently in the
15177      queue.  */
15178   parser->unparsed_functions_queues
15179     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15180
15181   /* Make sure that any template parameters are in scope.  */
15182   maybe_begin_member_template_processing (member_function);
15183
15184   /* If the body of the function has not yet been parsed, parse it
15185      now.  */
15186   if (DECL_PENDING_INLINE_P (member_function))
15187     {
15188       tree function_scope;
15189       cp_token_cache *tokens;
15190
15191       /* The function is no longer pending; we are processing it.  */
15192       tokens = DECL_PENDING_INLINE_INFO (member_function);
15193       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15194       DECL_PENDING_INLINE_P (member_function) = 0;
15195       /* If this was an inline function in a local class, enter the scope
15196          of the containing function.  */
15197       function_scope = decl_function_context (member_function);
15198       if (function_scope)
15199         push_function_context_to (function_scope);
15200
15201       /* Save away the current lexer.  */
15202       saved_lexer = parser->lexer;
15203       /* Make a new lexer to feed us the tokens saved for this function.  */
15204       parser->lexer = cp_lexer_new_from_tokens (tokens);
15205       parser->lexer->next = saved_lexer;
15206
15207       /* Set the current source position to be the location of the first
15208          token in the saved inline body.  */
15209       cp_lexer_peek_token (parser->lexer);
15210
15211       /* Let the front end know that we going to be defining this
15212          function.  */
15213       start_preparsed_function (member_function, NULL_TREE,
15214                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15215
15216       /* Now, parse the body of the function.  */
15217       cp_parser_function_definition_after_declarator (parser,
15218                                                       /*inline_p=*/true);
15219
15220       /* Leave the scope of the containing function.  */
15221       if (function_scope)
15222         pop_function_context_from (function_scope);
15223       /* Restore the lexer.  */
15224       parser->lexer = saved_lexer;
15225     }
15226
15227   /* Remove any template parameters from the symbol table.  */
15228   maybe_end_member_template_processing ();
15229
15230   /* Restore the queue.  */
15231   parser->unparsed_functions_queues
15232     = TREE_CHAIN (parser->unparsed_functions_queues);
15233 }
15234
15235 /* If DECL contains any default args, remember it on the unparsed
15236    functions queue.  */
15237
15238 static void
15239 cp_parser_save_default_args (cp_parser* parser, tree decl)
15240 {
15241   tree probe;
15242
15243   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15244        probe;
15245        probe = TREE_CHAIN (probe))
15246     if (TREE_PURPOSE (probe))
15247       {
15248         TREE_PURPOSE (parser->unparsed_functions_queues)
15249           = tree_cons (NULL_TREE, decl,
15250                        TREE_PURPOSE (parser->unparsed_functions_queues));
15251         break;
15252       }
15253   return;
15254 }
15255
15256 /* FN is a FUNCTION_DECL which may contains a parameter with an
15257    unparsed DEFAULT_ARG.  Parse the default args now.  */
15258
15259 static void
15260 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15261 {
15262   cp_lexer *saved_lexer;
15263   cp_token_cache *tokens;
15264   bool saved_local_variables_forbidden_p;
15265   tree parameters;
15266
15267   /* While we're parsing the default args, we might (due to the
15268      statement expression extension) encounter more classes.  We want
15269      to handle them right away, but we don't want them getting mixed
15270      up with default args that are currently in the queue.  */
15271   parser->unparsed_functions_queues
15272     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15273
15274   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15275        parameters;
15276        parameters = TREE_CHAIN (parameters))
15277     {
15278       if (!TREE_PURPOSE (parameters)
15279           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15280         continue;
15281
15282        /* Save away the current lexer.  */
15283       saved_lexer = parser->lexer;
15284        /* Create a new one, using the tokens we have saved.  */
15285       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
15286       parser->lexer = cp_lexer_new_from_tokens (tokens);
15287
15288        /* Set the current source position to be the location of the
15289           first token in the default argument.  */
15290       cp_lexer_peek_token (parser->lexer);
15291
15292        /* Local variable names (and the `this' keyword) may not appear
15293           in a default argument.  */
15294       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15295       parser->local_variables_forbidden_p = true;
15296        /* Parse the assignment-expression.  */
15297       if (DECL_CLASS_SCOPE_P (fn))
15298         push_nested_class (DECL_CONTEXT (fn));
15299       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15300       if (DECL_CLASS_SCOPE_P (fn))
15301         pop_nested_class ();
15302
15303       /* If the token stream has not been completely used up, then
15304          there was extra junk after the end of the default
15305          argument.  */
15306       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15307         cp_parser_error (parser, "expected `,'");
15308
15309        /* Restore saved state.  */
15310       parser->lexer = saved_lexer;
15311       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15312     }
15313
15314   /* Restore the queue.  */
15315   parser->unparsed_functions_queues
15316     = TREE_CHAIN (parser->unparsed_functions_queues);
15317 }
15318
15319 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15320    either a TYPE or an expression, depending on the form of the
15321    input.  The KEYWORD indicates which kind of expression we have
15322    encountered.  */
15323
15324 static tree
15325 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15326 {
15327   static const char *format;
15328   tree expr = NULL_TREE;
15329   const char *saved_message;
15330   bool saved_integral_constant_expression_p;
15331
15332   /* Initialize FORMAT the first time we get here.  */
15333   if (!format)
15334     format = "types may not be defined in `%s' expressions";
15335
15336   /* Types cannot be defined in a `sizeof' expression.  Save away the
15337      old message.  */
15338   saved_message = parser->type_definition_forbidden_message;
15339   /* And create the new one.  */
15340   parser->type_definition_forbidden_message
15341     = xmalloc (strlen (format)
15342                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15343                + 1 /* `\0' */);
15344   sprintf ((char *) parser->type_definition_forbidden_message,
15345            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15346
15347   /* The restrictions on constant-expressions do not apply inside
15348      sizeof expressions.  */
15349   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15350   parser->integral_constant_expression_p = false;
15351
15352   /* Do not actually evaluate the expression.  */
15353   ++skip_evaluation;
15354   /* If it's a `(', then we might be looking at the type-id
15355      construction.  */
15356   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15357     {
15358       tree type;
15359       bool saved_in_type_id_in_expr_p;
15360
15361       /* We can't be sure yet whether we're looking at a type-id or an
15362          expression.  */
15363       cp_parser_parse_tentatively (parser);
15364       /* Consume the `('.  */
15365       cp_lexer_consume_token (parser->lexer);
15366       /* Parse the type-id.  */
15367       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15368       parser->in_type_id_in_expr_p = true;
15369       type = cp_parser_type_id (parser);
15370       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15371       /* Now, look for the trailing `)'.  */
15372       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15373       /* If all went well, then we're done.  */
15374       if (cp_parser_parse_definitely (parser))
15375         {
15376           cp_decl_specifier_seq decl_specs;
15377
15378           /* Build a trivial decl-specifier-seq.  */
15379           clear_decl_specs (&decl_specs);
15380           decl_specs.type = type;
15381
15382           /* Call grokdeclarator to figure out what type this is.  */
15383           expr = grokdeclarator (NULL,
15384                                  &decl_specs,
15385                                  TYPENAME,
15386                                  /*initialized=*/0,
15387                                  /*attrlist=*/NULL);
15388         }
15389     }
15390
15391   /* If the type-id production did not work out, then we must be
15392      looking at the unary-expression production.  */
15393   if (!expr)
15394     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15395   /* Go back to evaluating expressions.  */
15396   --skip_evaluation;
15397
15398   /* Free the message we created.  */
15399   free ((char *) parser->type_definition_forbidden_message);
15400   /* And restore the old one.  */
15401   parser->type_definition_forbidden_message = saved_message;
15402   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15403
15404   return expr;
15405 }
15406
15407 /* If the current declaration has no declarator, return true.  */
15408
15409 static bool
15410 cp_parser_declares_only_class_p (cp_parser *parser)
15411 {
15412   /* If the next token is a `;' or a `,' then there is no
15413      declarator.  */
15414   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15415           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15416 }
15417
15418 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15419
15420 static void
15421 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15422                              cp_storage_class storage_class)
15423 {
15424   if (decl_specs->storage_class != sc_none)
15425     decl_specs->multiple_storage_classes_p = true;
15426   else
15427     decl_specs->storage_class = storage_class;
15428 }
15429
15430 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15431    is true, the type is a user-defined type; otherwise it is a
15432    built-in type specified by a keyword.  */
15433
15434 static void
15435 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15436                               tree type_spec,
15437                               bool user_defined_p)
15438 {
15439   decl_specs->any_specifiers_p = true;
15440  
15441   /* If the user tries to redeclare a built-in type (with, for example,
15442      in "typedef int wchar_t;") we remember that this is what
15443      happened.  In system headers, we ignore these declarations so
15444      that G++ can work with system headers that are not C++-safe.  */
15445   if (decl_specs->specs[(int) ds_typedef] 
15446       && !user_defined_p
15447       && (decl_specs->type
15448           || decl_specs->specs[(int) ds_long]
15449           || decl_specs->specs[(int) ds_short]
15450           || decl_specs->specs[(int) ds_unsigned]
15451           || decl_specs->specs[(int) ds_signed]))
15452     {
15453       decl_specs->redefined_builtin_type = type_spec;
15454       if (!decl_specs->type)
15455         {
15456           decl_specs->type = type_spec;
15457           decl_specs->user_defined_type_p = false;
15458         }
15459     }
15460   else if (decl_specs->type)
15461     decl_specs->multiple_types_p = true;
15462   else
15463     {
15464       decl_specs->type = type_spec;
15465       decl_specs->user_defined_type_p = user_defined_p;
15466       decl_specs->redefined_builtin_type = NULL_TREE;
15467     }
15468 }
15469
15470 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15471    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15472
15473 static bool
15474 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15475 {
15476   return decl_specifiers->specs[(int) ds_friend] != 0;
15477 }
15478
15479 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15480    issue an error message indicating that TOKEN_DESC was expected.
15481
15482    Returns the token consumed, if the token had the appropriate type.
15483    Otherwise, returns NULL.  */
15484
15485 static cp_token *
15486 cp_parser_require (cp_parser* parser,
15487                    enum cpp_ttype type,
15488                    const char* token_desc)
15489 {
15490   if (cp_lexer_next_token_is (parser->lexer, type))
15491     return cp_lexer_consume_token (parser->lexer);
15492   else
15493     {
15494       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15495       if (!cp_parser_simulate_error (parser))
15496         {
15497           char *message = concat ("expected ", token_desc, NULL);
15498           cp_parser_error (parser, message);
15499           free (message);
15500         }
15501       return NULL;
15502     }
15503 }
15504
15505 /* Like cp_parser_require, except that tokens will be skipped until
15506    the desired token is found.  An error message is still produced if
15507    the next token is not as expected.  */
15508
15509 static void
15510 cp_parser_skip_until_found (cp_parser* parser,
15511                             enum cpp_ttype type,
15512                             const char* token_desc)
15513 {
15514   cp_token *token;
15515   unsigned nesting_depth = 0;
15516
15517   if (cp_parser_require (parser, type, token_desc))
15518     return;
15519
15520   /* Skip tokens until the desired token is found.  */
15521   while (true)
15522     {
15523       /* Peek at the next token.  */
15524       token = cp_lexer_peek_token (parser->lexer);
15525       /* If we've reached the token we want, consume it and
15526          stop.  */
15527       if (token->type == type && !nesting_depth)
15528         {
15529           cp_lexer_consume_token (parser->lexer);
15530           return;
15531         }
15532       /* If we've run out of tokens, stop.  */
15533       if (token->type == CPP_EOF)
15534         return;
15535       if (token->type == CPP_OPEN_BRACE
15536           || token->type == CPP_OPEN_PAREN
15537           || token->type == CPP_OPEN_SQUARE)
15538         ++nesting_depth;
15539       else if (token->type == CPP_CLOSE_BRACE
15540                || token->type == CPP_CLOSE_PAREN
15541                || token->type == CPP_CLOSE_SQUARE)
15542         {
15543           if (nesting_depth-- == 0)
15544             return;
15545         }
15546       /* Consume this token.  */
15547       cp_lexer_consume_token (parser->lexer);
15548     }
15549 }
15550
15551 /* If the next token is the indicated keyword, consume it.  Otherwise,
15552    issue an error message indicating that TOKEN_DESC was expected.
15553
15554    Returns the token consumed, if the token had the appropriate type.
15555    Otherwise, returns NULL.  */
15556
15557 static cp_token *
15558 cp_parser_require_keyword (cp_parser* parser,
15559                            enum rid keyword,
15560                            const char* token_desc)
15561 {
15562   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15563
15564   if (token && token->keyword != keyword)
15565     {
15566       dyn_string_t error_msg;
15567
15568       /* Format the error message.  */
15569       error_msg = dyn_string_new (0);
15570       dyn_string_append_cstr (error_msg, "expected ");
15571       dyn_string_append_cstr (error_msg, token_desc);
15572       cp_parser_error (parser, error_msg->s);
15573       dyn_string_delete (error_msg);
15574       return NULL;
15575     }
15576
15577   return token;
15578 }
15579
15580 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15581    function-definition.  */
15582
15583 static bool
15584 cp_parser_token_starts_function_definition_p (cp_token* token)
15585 {
15586   return (/* An ordinary function-body begins with an `{'.  */
15587           token->type == CPP_OPEN_BRACE
15588           /* A ctor-initializer begins with a `:'.  */
15589           || token->type == CPP_COLON
15590           /* A function-try-block begins with `try'.  */
15591           || token->keyword == RID_TRY
15592           /* The named return value extension begins with `return'.  */
15593           || token->keyword == RID_RETURN);
15594 }
15595
15596 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15597    definition.  */
15598
15599 static bool
15600 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15601 {
15602   cp_token *token;
15603
15604   token = cp_lexer_peek_token (parser->lexer);
15605   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15606 }
15607
15608 /* Returns TRUE iff the next token is the "," or ">" ending a
15609    template-argument. ">>" is also accepted (after the full
15610    argument was parsed) because it's probably a typo for "> >",
15611    and there is a specific diagnostic for this.  */
15612
15613 static bool
15614 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15615 {
15616   cp_token *token;
15617
15618   token = cp_lexer_peek_token (parser->lexer);
15619   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15620           || token->type == CPP_RSHIFT);
15621 }
15622
15623 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15624    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15625
15626 static bool
15627 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15628                                                      size_t n)
15629 {
15630   cp_token *token;
15631
15632   token = cp_lexer_peek_nth_token (parser->lexer, n);
15633   if (token->type == CPP_LESS)
15634     return true;
15635   /* Check for the sequence `<::' in the original code. It would be lexed as
15636      `[:', where `[' is a digraph, and there is no whitespace before
15637      `:'.  */
15638   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15639     {
15640       cp_token *token2;
15641       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15642       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15643         return true;
15644     }
15645   return false;
15646 }
15647
15648 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15649    or none_type otherwise.  */
15650
15651 static enum tag_types
15652 cp_parser_token_is_class_key (cp_token* token)
15653 {
15654   switch (token->keyword)
15655     {
15656     case RID_CLASS:
15657       return class_type;
15658     case RID_STRUCT:
15659       return record_type;
15660     case RID_UNION:
15661       return union_type;
15662
15663     default:
15664       return none_type;
15665     }
15666 }
15667
15668 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15669
15670 static void
15671 cp_parser_check_class_key (enum tag_types class_key, tree type)
15672 {
15673   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15674     pedwarn ("`%s' tag used in naming `%#T'",
15675             class_key == union_type ? "union"
15676              : class_key == record_type ? "struct" : "class",
15677              type);
15678 }
15679
15680 /* Issue an error message if DECL is redeclared with different
15681    access than its original declaration [class.access.spec/3].
15682    This applies to nested classes and nested class templates.
15683    [class.mem/1].  */
15684
15685 static void cp_parser_check_access_in_redeclaration (tree decl)
15686 {
15687   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15688     return;
15689
15690   if ((TREE_PRIVATE (decl)
15691        != (current_access_specifier == access_private_node))
15692       || (TREE_PROTECTED (decl)
15693           != (current_access_specifier == access_protected_node)))
15694     error ("%D redeclared with different access", decl);
15695 }
15696
15697 /* Look for the `template' keyword, as a syntactic disambiguator.
15698    Return TRUE iff it is present, in which case it will be
15699    consumed.  */
15700
15701 static bool
15702 cp_parser_optional_template_keyword (cp_parser *parser)
15703 {
15704   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15705     {
15706       /* The `template' keyword can only be used within templates;
15707          outside templates the parser can always figure out what is a
15708          template and what is not.  */
15709       if (!processing_template_decl)
15710         {
15711           error ("`template' (as a disambiguator) is only allowed "
15712                  "within templates");
15713           /* If this part of the token stream is rescanned, the same
15714              error message would be generated.  So, we purge the token
15715              from the stream.  */
15716           cp_lexer_purge_token (parser->lexer);
15717           return false;
15718         }
15719       else
15720         {
15721           /* Consume the `template' keyword.  */
15722           cp_lexer_consume_token (parser->lexer);
15723           return true;
15724         }
15725     }
15726
15727   return false;
15728 }
15729
15730 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15731    set PARSER->SCOPE, and perform other related actions.  */
15732
15733 static void
15734 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15735 {
15736   tree value;
15737   tree check;
15738
15739   /* Get the stored value.  */
15740   value = cp_lexer_consume_token (parser->lexer)->value;
15741   /* Perform any access checks that were deferred.  */
15742   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15743     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15744   /* Set the scope from the stored value.  */
15745   parser->scope = TREE_VALUE (value);
15746   parser->qualifying_scope = TREE_TYPE (value);
15747   parser->object_scope = NULL_TREE;
15748 }
15749
15750 /* Add tokens to CACHE until a non-nested END token appears.  */
15751
15752 static void
15753 cp_parser_cache_group_1 (cp_parser *parser,
15754                          cp_token_cache *cache,
15755                          enum cpp_ttype end,
15756                          unsigned depth)
15757 {
15758   while (true)
15759     {
15760       cp_token *token;
15761
15762       /* Abort a parenthesized expression if we encounter a brace.  */
15763       if ((end == CPP_CLOSE_PAREN || depth == 0)
15764           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15765         return;
15766       /* If we've reached the end of the file, stop.  */
15767       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15768         return;
15769       /* Consume the next token.  */
15770       token = cp_lexer_consume_token (parser->lexer);
15771       /* Add this token to the tokens we are saving.  */
15772       cp_token_cache_push_token (cache, token);
15773       /* See if it starts a new group.  */
15774       if (token->type == CPP_OPEN_BRACE)
15775         {
15776           cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15777           if (depth == 0)
15778             return;
15779         }
15780       else if (token->type == CPP_OPEN_PAREN)
15781         cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15782       else if (token->type == end)
15783         return;
15784     }
15785 }
15786
15787 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15788    preserve string tokens in both translated and untranslated
15789    forms.  */
15790
15791 static void
15792 cp_parser_cache_group (cp_parser *parser,
15793                          cp_token_cache *cache,
15794                          enum cpp_ttype end,
15795                          unsigned depth)
15796 {
15797   int saved_c_lex_string_translate;
15798
15799   saved_c_lex_string_translate = c_lex_string_translate;
15800   c_lex_string_translate = -1;
15801
15802   cp_parser_cache_group_1 (parser, cache, end, depth);
15803   
15804   c_lex_string_translate = saved_c_lex_string_translate;
15805 }
15806
15807
15808 /* Begin parsing tentatively.  We always save tokens while parsing
15809    tentatively so that if the tentative parsing fails we can restore the
15810    tokens.  */
15811
15812 static void
15813 cp_parser_parse_tentatively (cp_parser* parser)
15814 {
15815   /* Enter a new parsing context.  */
15816   parser->context = cp_parser_context_new (parser->context);
15817   /* Begin saving tokens.  */
15818   cp_lexer_save_tokens (parser->lexer);
15819   /* In order to avoid repetitive access control error messages,
15820      access checks are queued up until we are no longer parsing
15821      tentatively.  */
15822   push_deferring_access_checks (dk_deferred);
15823 }
15824
15825 /* Commit to the currently active tentative parse.  */
15826
15827 static void
15828 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15829 {
15830   cp_parser_context *context;
15831   cp_lexer *lexer;
15832
15833   /* Mark all of the levels as committed.  */
15834   lexer = parser->lexer;
15835   for (context = parser->context; context->next; context = context->next)
15836     {
15837       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15838         break;
15839       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15840       while (!cp_lexer_saving_tokens (lexer))
15841         lexer = lexer->next;
15842       cp_lexer_commit_tokens (lexer);
15843     }
15844 }
15845
15846 /* Abort the currently active tentative parse.  All consumed tokens
15847    will be rolled back, and no diagnostics will be issued.  */
15848
15849 static void
15850 cp_parser_abort_tentative_parse (cp_parser* parser)
15851 {
15852   cp_parser_simulate_error (parser);
15853   /* Now, pretend that we want to see if the construct was
15854      successfully parsed.  */
15855   cp_parser_parse_definitely (parser);
15856 }
15857
15858 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15859    token stream.  Otherwise, commit to the tokens we have consumed.
15860    Returns true if no error occurred; false otherwise.  */
15861
15862 static bool
15863 cp_parser_parse_definitely (cp_parser* parser)
15864 {
15865   bool error_occurred;
15866   cp_parser_context *context;
15867
15868   /* Remember whether or not an error occurred, since we are about to
15869      destroy that information.  */
15870   error_occurred = cp_parser_error_occurred (parser);
15871   /* Remove the topmost context from the stack.  */
15872   context = parser->context;
15873   parser->context = context->next;
15874   /* If no parse errors occurred, commit to the tentative parse.  */
15875   if (!error_occurred)
15876     {
15877       /* Commit to the tokens read tentatively, unless that was
15878          already done.  */
15879       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15880         cp_lexer_commit_tokens (parser->lexer);
15881
15882       pop_to_parent_deferring_access_checks ();
15883     }
15884   /* Otherwise, if errors occurred, roll back our state so that things
15885      are just as they were before we began the tentative parse.  */
15886   else
15887     {
15888       cp_lexer_rollback_tokens (parser->lexer);
15889       pop_deferring_access_checks ();
15890     }
15891   /* Add the context to the front of the free list.  */
15892   context->next = cp_parser_context_free_list;
15893   cp_parser_context_free_list = context;
15894
15895   return !error_occurred;
15896 }
15897
15898 /* Returns true if we are parsing tentatively -- but have decided that
15899    we will stick with this tentative parse, even if errors occur.  */
15900
15901 static bool
15902 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15903 {
15904   return (cp_parser_parsing_tentatively (parser)
15905           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15906 }
15907
15908 /* Returns nonzero iff an error has occurred during the most recent
15909    tentative parse.  */
15910
15911 static bool
15912 cp_parser_error_occurred (cp_parser* parser)
15913 {
15914   return (cp_parser_parsing_tentatively (parser)
15915           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15916 }
15917
15918 /* Returns nonzero if GNU extensions are allowed.  */
15919
15920 static bool
15921 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15922 {
15923   return parser->allow_gnu_extensions_p;
15924 }
15925
15926 \f
15927 /* The parser.  */
15928
15929 static GTY (()) cp_parser *the_parser;
15930
15931 /* External interface.  */
15932
15933 /* Parse one entire translation unit.  */
15934
15935 void
15936 c_parse_file (void)
15937 {
15938   bool error_occurred;
15939   static bool already_called = false;
15940
15941   if (already_called)
15942     {
15943       sorry ("inter-module optimizations not implemented for C++");
15944       return;
15945     }
15946   already_called = true;
15947
15948   the_parser = cp_parser_new ();
15949   push_deferring_access_checks (flag_access_control
15950                                 ? dk_no_deferred : dk_no_check);
15951   error_occurred = cp_parser_translation_unit (the_parser);
15952   the_parser = NULL;
15953 }
15954
15955 /* This variable must be provided by every front end.  */
15956
15957 int yydebug;
15958
15959 #include "gt-cp-parser.h"