OSDN Git Service

PR c++/16853
[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_CNEW (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_CNEW (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_CNEW (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_CNEW (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_NEWVEC (cp_token, num_tokens);
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 = UNKNOWN_LOCATION;
601       token->value = NULL_TREE;
602       token->keyword = RID_MAX;
603
604       return;
605     }
606
607   done = false;
608   /* Keep going until we get a token we like.  */
609   while (!done)
610     {
611       /* Get a new token from the preprocessor.  */
612       token->type = c_lex_with_flags (&token->value, &token->flags);
613       /* Issue messages about tokens we cannot process.  */
614       switch (token->type)
615         {
616         case CPP_ATSIGN:
617         case CPP_HASH:
618         case CPP_PASTE:
619           error ("invalid token");
620           break;
621
622         default:
623           /* This is a good token, so we exit the loop.  */
624           done = true;
625           break;
626         }
627     }
628   /* Now we've got our token.  */
629   token->location = input_location;
630
631   /* Check to see if this token is a keyword.  */
632   if (token->type == CPP_NAME
633       && C_IS_RESERVED_WORD (token->value))
634     {
635       /* Mark this token as a keyword.  */
636       token->type = CPP_KEYWORD;
637       /* Record which keyword.  */
638       token->keyword = C_RID_CODE (token->value);
639       /* Update the value.  Some keywords are mapped to particular
640          entities, rather than simply having the value of the
641          corresponding IDENTIFIER_NODE.  For example, `__const' is
642          mapped to `const'.  */
643       token->value = ridpointers[token->keyword];
644     }
645   else
646     token->keyword = RID_MAX;
647 }
648
649 /* Return a pointer to the next token in the token stream, but do not
650    consume it.  */
651
652 static cp_token *
653 cp_lexer_peek_token (cp_lexer* lexer)
654 {
655   cp_token *token;
656
657   /* If there are no tokens, read one now.  */
658   if (!lexer->next_token)
659     cp_lexer_read_token (lexer);
660
661   /* Provide debugging output.  */
662   if (cp_lexer_debugging_p (lexer))
663     {
664       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
665       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
666       fprintf (cp_lexer_debug_stream, "\n");
667     }
668
669   token = lexer->next_token;
670   cp_lexer_set_source_position_from_token (lexer, token);
671   return token;
672 }
673
674 /* Return true if the next token has the indicated TYPE.  */
675
676 static bool
677 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
678 {
679   cp_token *token;
680
681   /* Peek at the next token.  */
682   token = cp_lexer_peek_token (lexer);
683   /* Check to see if it has the indicated TYPE.  */
684   return token->type == type;
685 }
686
687 /* Return true if the next token does not have the indicated TYPE.  */
688
689 static bool
690 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
691 {
692   return !cp_lexer_next_token_is (lexer, type);
693 }
694
695 /* Return true if the next token is the indicated KEYWORD.  */
696
697 static bool
698 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
699 {
700   cp_token *token;
701
702   /* Peek at the next token.  */
703   token = cp_lexer_peek_token (lexer);
704   /* Check to see if it is the indicated keyword.  */
705   return token->keyword == keyword;
706 }
707
708 /* Return a pointer to the Nth token in the token stream.  If N is 1,
709    then this is precisely equivalent to cp_lexer_peek_token.  */
710
711 static cp_token *
712 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
713 {
714   cp_token *token;
715
716   /* N is 1-based, not zero-based.  */
717   my_friendly_assert (n > 0, 20000224);
718
719   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
720   token = lexer->next_token;
721   /* If there are no tokens in the buffer, get one now.  */
722   if (!token)
723     {
724       cp_lexer_read_token (lexer);
725       token = lexer->next_token;
726     }
727
728   /* Now, read tokens until we have enough.  */
729   while (--n > 0)
730     {
731       /* Advance to the next token.  */
732       token = cp_lexer_next_token (lexer, token);
733       /* If that's all the tokens we have, read a new one.  */
734       if (token == lexer->last_token)
735         token = cp_lexer_read_token (lexer);
736     }
737
738   return token;
739 }
740
741 /* Consume the next token.  The pointer returned is valid only until
742    another token is read.  Callers should preserve copy the token
743    explicitly if they will need its value for a longer period of
744    time.  */
745
746 static cp_token *
747 cp_lexer_consume_token (cp_lexer* lexer)
748 {
749   cp_token *token;
750
751   /* If there are no tokens, read one now.  */
752   if (!lexer->next_token)
753     cp_lexer_read_token (lexer);
754
755   /* Remember the token we'll be returning.  */
756   token = lexer->next_token;
757
758   /* Increment NEXT_TOKEN.  */
759   lexer->next_token = cp_lexer_next_token (lexer,
760                                            lexer->next_token);
761   /* Check to see if we're all out of tokens.  */
762   if (lexer->next_token == lexer->last_token)
763     lexer->next_token = NULL;
764
765   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
766   if (!cp_lexer_saving_tokens (lexer))
767     {
768       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
769       if (!lexer->next_token)
770         lexer->first_token = NULL;
771       else
772         lexer->first_token = lexer->next_token;
773     }
774
775   /* Provide debugging output.  */
776   if (cp_lexer_debugging_p (lexer))
777     {
778       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
779       cp_lexer_print_token (cp_lexer_debug_stream, token);
780       fprintf (cp_lexer_debug_stream, "\n");
781     }
782
783   return token;
784 }
785
786 /* Permanently remove the next token from the token stream.  There
787    must be a valid next token already; this token never reads
788    additional tokens from the preprocessor.  */
789
790 static void
791 cp_lexer_purge_token (cp_lexer *lexer)
792 {
793   cp_token *token;
794   cp_token *next_token;
795
796   token = lexer->next_token;
797   while (true)
798     {
799       next_token = cp_lexer_next_token (lexer, token);
800       if (next_token == lexer->last_token)
801         break;
802       *token = *next_token;
803       token = next_token;
804     }
805
806   lexer->last_token = token;
807   /* The token purged may have been the only token remaining; if so,
808      clear NEXT_TOKEN.  */
809   if (lexer->next_token == token)
810     lexer->next_token = NULL;
811 }
812
813 /* Permanently remove all tokens after TOKEN, up to, but not
814    including, the token that will be returned next by
815    cp_lexer_peek_token.  */
816
817 static void
818 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
819 {
820   cp_token *peek;
821   cp_token *t1;
822   cp_token *t2;
823
824   if (lexer->next_token)
825     {
826       /* Copy the tokens that have not yet been read to the location
827          immediately following TOKEN.  */
828       t1 = cp_lexer_next_token (lexer, token);
829       t2 = peek = cp_lexer_peek_token (lexer);
830       /* Move tokens into the vacant area between TOKEN and PEEK.  */
831       while (t2 != lexer->last_token)
832         {
833           *t1 = *t2;
834           t1 = cp_lexer_next_token (lexer, t1);
835           t2 = cp_lexer_next_token (lexer, t2);
836         }
837       /* Now, the next available token is right after TOKEN.  */
838       lexer->next_token = cp_lexer_next_token (lexer, token);
839       /* And the last token is wherever we ended up.  */
840       lexer->last_token = t1;
841     }
842   else
843     {
844       /* There are no tokens in the buffer, so there is nothing to
845          copy.  The last token in the buffer is TOKEN itself.  */
846       lexer->last_token = cp_lexer_next_token (lexer, token);
847     }
848 }
849
850 /* Begin saving tokens.  All tokens consumed after this point will be
851    preserved.  */
852
853 static void
854 cp_lexer_save_tokens (cp_lexer* lexer)
855 {
856   /* Provide debugging output.  */
857   if (cp_lexer_debugging_p (lexer))
858     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
859
860   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
861      restore the tokens if required.  */
862   if (!lexer->next_token)
863     cp_lexer_read_token (lexer);
864
865   VARRAY_PUSH_INT (lexer->saved_tokens,
866                    cp_lexer_token_difference (lexer,
867                                               lexer->first_token,
868                                               lexer->next_token));
869 }
870
871 /* Commit to the portion of the token stream most recently saved.  */
872
873 static void
874 cp_lexer_commit_tokens (cp_lexer* lexer)
875 {
876   /* Provide debugging output.  */
877   if (cp_lexer_debugging_p (lexer))
878     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
879
880   VARRAY_POP (lexer->saved_tokens);
881 }
882
883 /* Return all tokens saved since the last call to cp_lexer_save_tokens
884    to the token stream.  Stop saving tokens.  */
885
886 static void
887 cp_lexer_rollback_tokens (cp_lexer* lexer)
888 {
889   size_t delta;
890
891   /* Provide debugging output.  */
892   if (cp_lexer_debugging_p (lexer))
893     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
894
895   /* Find the token that was the NEXT_TOKEN when we started saving
896      tokens.  */
897   delta = VARRAY_TOP_INT(lexer->saved_tokens);
898   /* Make it the next token again now.  */
899   lexer->next_token = cp_lexer_advance_token (lexer,
900                                               lexer->first_token,
901                                               delta);
902   /* It might be the case that there were no tokens when we started
903      saving tokens, but that there are some tokens now.  */
904   if (!lexer->next_token && lexer->first_token)
905     lexer->next_token = lexer->first_token;
906
907   /* Stop saving tokens.  */
908   VARRAY_POP (lexer->saved_tokens);
909 }
910
911 /* Print a representation of the TOKEN on the STREAM.  */
912
913 static void
914 cp_lexer_print_token (FILE * stream, cp_token* token)
915 {
916   const char *token_type = NULL;
917
918   /* Figure out what kind of token this is.  */
919   switch (token->type)
920     {
921     case CPP_EQ:
922       token_type = "EQ";
923       break;
924
925     case CPP_COMMA:
926       token_type = "COMMA";
927       break;
928
929     case CPP_OPEN_PAREN:
930       token_type = "OPEN_PAREN";
931       break;
932
933     case CPP_CLOSE_PAREN:
934       token_type = "CLOSE_PAREN";
935       break;
936
937     case CPP_OPEN_BRACE:
938       token_type = "OPEN_BRACE";
939       break;
940
941     case CPP_CLOSE_BRACE:
942       token_type = "CLOSE_BRACE";
943       break;
944
945     case CPP_SEMICOLON:
946       token_type = "SEMICOLON";
947       break;
948
949     case CPP_NAME:
950       token_type = "NAME";
951       break;
952
953     case CPP_EOF:
954       token_type = "EOF";
955       break;
956
957     case CPP_KEYWORD:
958       token_type = "keyword";
959       break;
960
961       /* This is not a token that we know how to handle yet.  */
962     default:
963       break;
964     }
965
966   /* If we have a name for the token, print it out.  Otherwise, we
967      simply give the numeric code.  */
968   if (token_type)
969     fprintf (stream, "%s", token_type);
970   else
971     fprintf (stream, "%d", token->type);
972   /* And, for an identifier, print the identifier name.  */
973   if (token->type == CPP_NAME
974       /* Some keywords have a value that is not an IDENTIFIER_NODE.
975          For example, `struct' is mapped to an INTEGER_CST.  */
976       || (token->type == CPP_KEYWORD
977           && TREE_CODE (token->value) == IDENTIFIER_NODE))
978     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
979 }
980
981 /* Start emitting debugging information.  */
982
983 static void
984 cp_lexer_start_debugging (cp_lexer* lexer)
985 {
986   ++lexer->debugging_p;
987 }
988
989 /* Stop emitting debugging information.  */
990
991 static void
992 cp_lexer_stop_debugging (cp_lexer* lexer)
993 {
994   --lexer->debugging_p;
995 }
996
997 \f
998 /* Decl-specifiers.  */
999
1000 static void clear_decl_specs
1001   (cp_decl_specifier_seq *);
1002
1003 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1004
1005 static void
1006 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1007 {
1008   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1009 }
1010
1011 /* Declarators.  */
1012
1013 /* Nothing other than the parser should be creating declarators;
1014    declarators are a semi-syntactic representation of C++ entities.
1015    Other parts of the front end that need to create entities (like
1016    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1017
1018 static cp_declarator *make_id_declarator
1019   (tree);
1020 static cp_declarator *make_call_declarator
1021   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
1022 static cp_declarator *make_array_declarator
1023   (cp_declarator *, tree);
1024 static cp_declarator *make_pointer_declarator
1025   (cp_cv_quals, cp_declarator *);
1026 static cp_declarator *make_reference_declarator
1027   (cp_cv_quals, cp_declarator *);
1028 static cp_parameter_declarator *make_parameter_declarator
1029   (cp_decl_specifier_seq *, cp_declarator *, tree);
1030 static cp_declarator *make_ptrmem_declarator
1031   (cp_cv_quals, tree, cp_declarator *);
1032
1033 cp_declarator *cp_error_declarator;
1034
1035 /* The obstack on which declarators and related data structures are
1036    allocated.  */
1037 static struct obstack declarator_obstack;
1038
1039 /* Alloc BYTES from the declarator memory pool.  */
1040
1041 static inline void *
1042 alloc_declarator (size_t bytes)
1043 {
1044   return obstack_alloc (&declarator_obstack, bytes);
1045 }
1046
1047 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1048    common to all declarators.  */
1049
1050 static cp_declarator *
1051 make_declarator (cp_declarator_kind kind)
1052 {
1053   cp_declarator *declarator;
1054
1055   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1056   declarator->kind = kind;
1057   declarator->attributes = NULL_TREE;
1058   declarator->declarator = NULL;
1059
1060   return declarator;
1061 }
1062
1063 /* Make a declarator for a generalized identifier.  */
1064
1065 cp_declarator *
1066 make_id_declarator (tree id)
1067 {
1068   cp_declarator *declarator;
1069
1070   declarator = make_declarator (cdk_id);
1071   declarator->u.id.name = id;
1072   declarator->u.id.sfk = sfk_none;
1073
1074   return declarator;
1075 }
1076
1077 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1078    of modifiers such as const or volatile to apply to the pointer
1079    type, represented as identifiers.  */
1080
1081 cp_declarator *
1082 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1083 {
1084   cp_declarator *declarator;
1085
1086   declarator = make_declarator (cdk_pointer);
1087   declarator->declarator = target;
1088   declarator->u.pointer.qualifiers = cv_qualifiers;
1089   declarator->u.pointer.class_type = NULL_TREE;
1090
1091   return declarator;
1092 }
1093
1094 /* Like make_pointer_declarator -- but for references.  */
1095
1096 cp_declarator *
1097 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1098 {
1099   cp_declarator *declarator;
1100
1101   declarator = make_declarator (cdk_reference);
1102   declarator->declarator = target;
1103   declarator->u.pointer.qualifiers = cv_qualifiers;
1104   declarator->u.pointer.class_type = NULL_TREE;
1105
1106   return declarator;
1107 }
1108
1109 /* Like make_pointer_declarator -- but for a pointer to a non-static
1110    member of CLASS_TYPE.  */
1111
1112 cp_declarator *
1113 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1114                         cp_declarator *pointee)
1115 {
1116   cp_declarator *declarator;
1117
1118   declarator = make_declarator (cdk_ptrmem);
1119   declarator->declarator = pointee;
1120   declarator->u.pointer.qualifiers = cv_qualifiers;
1121   declarator->u.pointer.class_type = class_type;
1122
1123   return declarator;
1124 }
1125
1126 /* Make a declarator for the function given by TARGET, with the
1127    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1128    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1129    indicates what exceptions can be thrown.  */
1130
1131 cp_declarator *
1132 make_call_declarator (cp_declarator *target,
1133                       cp_parameter_declarator *parms,
1134                       cp_cv_quals cv_qualifiers,
1135                       tree exception_specification)
1136 {
1137   cp_declarator *declarator;
1138
1139   declarator = make_declarator (cdk_function);
1140   declarator->declarator = target;
1141   declarator->u.function.parameters = parms;
1142   declarator->u.function.qualifiers = cv_qualifiers;
1143   declarator->u.function.exception_specification = exception_specification;
1144
1145   return declarator;
1146 }
1147
1148 /* Make a declarator for an array of BOUNDS elements, each of which is
1149    defined by ELEMENT.  */
1150
1151 cp_declarator *
1152 make_array_declarator (cp_declarator *element, tree bounds)
1153 {
1154   cp_declarator *declarator;
1155
1156   declarator = make_declarator (cdk_array);
1157   declarator->declarator = element;
1158   declarator->u.array.bounds = bounds;
1159
1160   return declarator;
1161 }
1162
1163 cp_parameter_declarator *no_parameters;
1164
1165 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1166    DECLARATOR and DEFAULT_ARGUMENT.  */
1167
1168 cp_parameter_declarator *
1169 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1170                            cp_declarator *declarator,
1171                            tree default_argument)
1172 {
1173   cp_parameter_declarator *parameter;
1174
1175   parameter = ((cp_parameter_declarator *)
1176                alloc_declarator (sizeof (cp_parameter_declarator)));
1177   parameter->next = NULL;
1178   if (decl_specifiers)
1179     parameter->decl_specifiers = *decl_specifiers;
1180   else
1181     clear_decl_specs (&parameter->decl_specifiers);
1182   parameter->declarator = declarator;
1183   parameter->default_argument = default_argument;
1184   parameter->ellipsis_p = false;
1185
1186   return parameter;
1187 }
1188
1189 /* The parser.  */
1190
1191 /* Overview
1192    --------
1193
1194    A cp_parser parses the token stream as specified by the C++
1195    grammar.  Its job is purely parsing, not semantic analysis.  For
1196    example, the parser breaks the token stream into declarators,
1197    expressions, statements, and other similar syntactic constructs.
1198    It does not check that the types of the expressions on either side
1199    of an assignment-statement are compatible, or that a function is
1200    not declared with a parameter of type `void'.
1201
1202    The parser invokes routines elsewhere in the compiler to perform
1203    semantic analysis and to build up the abstract syntax tree for the
1204    code processed.
1205
1206    The parser (and the template instantiation code, which is, in a
1207    way, a close relative of parsing) are the only parts of the
1208    compiler that should be calling push_scope and pop_scope, or
1209    related functions.  The parser (and template instantiation code)
1210    keeps track of what scope is presently active; everything else
1211    should simply honor that.  (The code that generates static
1212    initializers may also need to set the scope, in order to check
1213    access control correctly when emitting the initializers.)
1214
1215    Methodology
1216    -----------
1217
1218    The parser is of the standard recursive-descent variety.  Upcoming
1219    tokens in the token stream are examined in order to determine which
1220    production to use when parsing a non-terminal.  Some C++ constructs
1221    require arbitrary look ahead to disambiguate.  For example, it is
1222    impossible, in the general case, to tell whether a statement is an
1223    expression or declaration without scanning the entire statement.
1224    Therefore, the parser is capable of "parsing tentatively."  When the
1225    parser is not sure what construct comes next, it enters this mode.
1226    Then, while we attempt to parse the construct, the parser queues up
1227    error messages, rather than issuing them immediately, and saves the
1228    tokens it consumes.  If the construct is parsed successfully, the
1229    parser "commits", i.e., it issues any queued error messages and
1230    the tokens that were being preserved are permanently discarded.
1231    If, however, the construct is not parsed successfully, the parser
1232    rolls back its state completely so that it can resume parsing using
1233    a different alternative.
1234
1235    Future Improvements
1236    -------------------
1237
1238    The performance of the parser could probably be improved
1239    substantially.  Some possible improvements include:
1240
1241      - The expression parser recurses through the various levels of
1242        precedence as specified in the grammar, rather than using an
1243        operator-precedence technique.  Therefore, parsing a simple
1244        identifier requires multiple recursive calls.
1245
1246      - We could often eliminate the need to parse tentatively by
1247        looking ahead a little bit.  In some places, this approach
1248        might not entirely eliminate the need to parse tentatively, but
1249        it might still speed up the average case.  */
1250
1251 /* Flags that are passed to some parsing functions.  These values can
1252    be bitwise-ored together.  */
1253
1254 typedef enum cp_parser_flags
1255 {
1256   /* No flags.  */
1257   CP_PARSER_FLAGS_NONE = 0x0,
1258   /* The construct is optional.  If it is not present, then no error
1259      should be issued.  */
1260   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1261   /* When parsing a type-specifier, do not allow user-defined types.  */
1262   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1263 } cp_parser_flags;
1264
1265 /* The different kinds of declarators we want to parse.  */
1266
1267 typedef enum cp_parser_declarator_kind
1268 {
1269   /* We want an abstract declarator.  */
1270   CP_PARSER_DECLARATOR_ABSTRACT,
1271   /* We want a named declarator.  */
1272   CP_PARSER_DECLARATOR_NAMED,
1273   /* We don't mind, but the name must be an unqualified-id.  */
1274   CP_PARSER_DECLARATOR_EITHER
1275 } cp_parser_declarator_kind;
1276
1277 /* A mapping from a token type to a corresponding tree node type.  */
1278
1279 typedef struct cp_parser_token_tree_map_node
1280 {
1281   /* The token type.  */
1282   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1283   /* The corresponding tree code.  */
1284   ENUM_BITFIELD (tree_code) tree_type : 8;
1285 } cp_parser_token_tree_map_node;
1286
1287 /* A complete map consists of several ordinary entries, followed by a
1288    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1289
1290 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1291
1292 /* The status of a tentative parse.  */
1293
1294 typedef enum cp_parser_status_kind
1295 {
1296   /* No errors have occurred.  */
1297   CP_PARSER_STATUS_KIND_NO_ERROR,
1298   /* An error has occurred.  */
1299   CP_PARSER_STATUS_KIND_ERROR,
1300   /* We are committed to this tentative parse, whether or not an error
1301      has occurred.  */
1302   CP_PARSER_STATUS_KIND_COMMITTED
1303 } cp_parser_status_kind;
1304
1305 /* Context that is saved and restored when parsing tentatively.  */
1306
1307 typedef struct cp_parser_context GTY (())
1308 {
1309   /* If this is a tentative parsing context, the status of the
1310      tentative parse.  */
1311   enum cp_parser_status_kind status;
1312   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1313      that are looked up in this context must be looked up both in the
1314      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1315      the context of the containing expression.  */
1316   tree object_type;
1317   /* The next parsing context in the stack.  */
1318   struct cp_parser_context *next;
1319 } cp_parser_context;
1320
1321 /* Prototypes.  */
1322
1323 /* Constructors and destructors.  */
1324
1325 static cp_parser_context *cp_parser_context_new
1326   (cp_parser_context *);
1327
1328 /* Class variables.  */
1329
1330 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1331
1332 /* Constructors and destructors.  */
1333
1334 /* Construct a new context.  The context below this one on the stack
1335    is given by NEXT.  */
1336
1337 static cp_parser_context *
1338 cp_parser_context_new (cp_parser_context* next)
1339 {
1340   cp_parser_context *context;
1341
1342   /* Allocate the storage.  */
1343   if (cp_parser_context_free_list != NULL)
1344     {
1345       /* Pull the first entry from the free list.  */
1346       context = cp_parser_context_free_list;
1347       cp_parser_context_free_list = context->next;
1348       memset (context, 0, sizeof (*context));
1349     }
1350   else
1351     context = GGC_CNEW (cp_parser_context);
1352   /* No errors have occurred yet in this context.  */
1353   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1354   /* If this is not the bottomost context, copy information that we
1355      need from the previous context.  */
1356   if (next)
1357     {
1358       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1359          expression, then we are parsing one in this context, too.  */
1360       context->object_type = next->object_type;
1361       /* Thread the stack.  */
1362       context->next = next;
1363     }
1364
1365   return context;
1366 }
1367
1368 /* The cp_parser structure represents the C++ parser.  */
1369
1370 typedef struct cp_parser GTY(())
1371 {
1372   /* The lexer from which we are obtaining tokens.  */
1373   cp_lexer *lexer;
1374
1375   /* The scope in which names should be looked up.  If NULL_TREE, then
1376      we look up names in the scope that is currently open in the
1377      source program.  If non-NULL, this is either a TYPE or
1378      NAMESPACE_DECL for the scope in which we should look.
1379
1380      This value is not cleared automatically after a name is looked
1381      up, so we must be careful to clear it before starting a new look
1382      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1383      will look up `Z' in the scope of `X', rather than the current
1384      scope.)  Unfortunately, it is difficult to tell when name lookup
1385      is complete, because we sometimes peek at a token, look it up,
1386      and then decide not to consume it.  */
1387   tree scope;
1388
1389   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1390      last lookup took place.  OBJECT_SCOPE is used if an expression
1391      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1392      respectively.  QUALIFYING_SCOPE is used for an expression of the
1393      form "X::Y"; it refers to X.  */
1394   tree object_scope;
1395   tree qualifying_scope;
1396
1397   /* A stack of parsing contexts.  All but the bottom entry on the
1398      stack will be tentative contexts.
1399
1400      We parse tentatively in order to determine which construct is in
1401      use in some situations.  For example, in order to determine
1402      whether a statement is an expression-statement or a
1403      declaration-statement we parse it tentatively as a
1404      declaration-statement.  If that fails, we then reparse the same
1405      token stream as an expression-statement.  */
1406   cp_parser_context *context;
1407
1408   /* True if we are parsing GNU C++.  If this flag is not set, then
1409      GNU extensions are not recognized.  */
1410   bool allow_gnu_extensions_p;
1411
1412   /* TRUE if the `>' token should be interpreted as the greater-than
1413      operator.  FALSE if it is the end of a template-id or
1414      template-parameter-list.  */
1415   bool greater_than_is_operator_p;
1416
1417   /* TRUE if default arguments are allowed within a parameter list
1418      that starts at this point. FALSE if only a gnu extension makes
1419      them permissible.  */
1420   bool default_arg_ok_p;
1421
1422   /* TRUE if we are parsing an integral constant-expression.  See
1423      [expr.const] for a precise definition.  */
1424   bool integral_constant_expression_p;
1425
1426   /* TRUE if we are parsing an integral constant-expression -- but a
1427      non-constant expression should be permitted as well.  This flag
1428      is used when parsing an array bound so that GNU variable-length
1429      arrays are tolerated.  */
1430   bool allow_non_integral_constant_expression_p;
1431
1432   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1433      been seen that makes the expression non-constant.  */
1434   bool non_integral_constant_expression_p;
1435
1436   /* TRUE if local variable names and `this' are forbidden in the
1437      current context.  */
1438   bool local_variables_forbidden_p;
1439
1440   /* TRUE if the declaration we are parsing is part of a
1441      linkage-specification of the form `extern string-literal
1442      declaration'.  */
1443   bool in_unbraced_linkage_specification_p;
1444
1445   /* TRUE if we are presently parsing a declarator, after the
1446      direct-declarator.  */
1447   bool in_declarator_p;
1448
1449   /* TRUE if we are presently parsing a template-argument-list.  */
1450   bool in_template_argument_list_p;
1451
1452   /* TRUE if we are presently parsing the body of an
1453      iteration-statement.  */
1454   bool in_iteration_statement_p;
1455
1456   /* TRUE if we are presently parsing the body of a switch
1457      statement.  */
1458   bool in_switch_statement_p;
1459
1460   /* TRUE if we are parsing a type-id in an expression context.  In
1461      such a situation, both "type (expr)" and "type (type)" are valid
1462      alternatives.  */
1463   bool in_type_id_in_expr_p;
1464
1465   /* If non-NULL, then we are parsing a construct where new type
1466      definitions are not permitted.  The string stored here will be
1467      issued as an error message if a type is defined.  */
1468   const char *type_definition_forbidden_message;
1469
1470   /* A list of lists. The outer list is a stack, used for member
1471      functions of local classes. At each level there are two sub-list,
1472      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1473      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1474      TREE_VALUE's. The functions are chained in reverse declaration
1475      order.
1476
1477      The TREE_PURPOSE sublist contains those functions with default
1478      arguments that need post processing, and the TREE_VALUE sublist
1479      contains those functions with definitions that need post
1480      processing.
1481
1482      These lists can only be processed once the outermost class being
1483      defined is complete.  */
1484   tree unparsed_functions_queues;
1485
1486   /* The number of classes whose definitions are currently in
1487      progress.  */
1488   unsigned num_classes_being_defined;
1489
1490   /* The number of template parameter lists that apply directly to the
1491      current declaration.  */
1492   unsigned num_template_parameter_lists;
1493 } cp_parser;
1494
1495 /* The type of a function that parses some kind of expression.  */
1496 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1497
1498 /* Prototypes.  */
1499
1500 /* Constructors and destructors.  */
1501
1502 static cp_parser *cp_parser_new
1503   (void);
1504
1505 /* Routines to parse various constructs.
1506
1507    Those that return `tree' will return the error_mark_node (rather
1508    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1509    Sometimes, they will return an ordinary node if error-recovery was
1510    attempted, even though a parse error occurred.  So, to check
1511    whether or not a parse error occurred, you should always use
1512    cp_parser_error_occurred.  If the construct is optional (indicated
1513    either by an `_opt' in the name of the function that does the
1514    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1515    the construct is not present.  */
1516
1517 /* Lexical conventions [gram.lex]  */
1518
1519 static tree cp_parser_identifier
1520   (cp_parser *);
1521
1522 /* Basic concepts [gram.basic]  */
1523
1524 static bool cp_parser_translation_unit
1525   (cp_parser *);
1526
1527 /* Expressions [gram.expr]  */
1528
1529 static tree cp_parser_primary_expression
1530   (cp_parser *, cp_id_kind *, tree *);
1531 static tree cp_parser_id_expression
1532   (cp_parser *, bool, bool, bool *, bool);
1533 static tree cp_parser_unqualified_id
1534   (cp_parser *, bool, bool, bool);
1535 static tree cp_parser_nested_name_specifier_opt
1536   (cp_parser *, bool, bool, bool, bool);
1537 static tree cp_parser_nested_name_specifier
1538   (cp_parser *, bool, bool, bool, bool);
1539 static tree cp_parser_class_or_namespace_name
1540   (cp_parser *, bool, bool, bool, bool, bool);
1541 static tree cp_parser_postfix_expression
1542   (cp_parser *, bool);
1543 static tree cp_parser_postfix_open_square_expression
1544   (cp_parser *, tree, bool);
1545 static tree cp_parser_postfix_dot_deref_expression
1546   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1547 static tree cp_parser_parenthesized_expression_list
1548   (cp_parser *, bool, bool *);
1549 static void cp_parser_pseudo_destructor_name
1550   (cp_parser *, tree *, tree *);
1551 static tree cp_parser_unary_expression
1552   (cp_parser *, bool);
1553 static enum tree_code cp_parser_unary_operator
1554   (cp_token *);
1555 static tree cp_parser_new_expression
1556   (cp_parser *);
1557 static tree cp_parser_new_placement
1558   (cp_parser *);
1559 static tree cp_parser_new_type_id
1560   (cp_parser *, tree *);
1561 static cp_declarator *cp_parser_new_declarator_opt
1562   (cp_parser *);
1563 static cp_declarator *cp_parser_direct_new_declarator
1564   (cp_parser *);
1565 static tree cp_parser_new_initializer
1566   (cp_parser *);
1567 static tree cp_parser_delete_expression
1568   (cp_parser *);
1569 static tree cp_parser_cast_expression
1570   (cp_parser *, bool);
1571 static tree cp_parser_pm_expression
1572   (cp_parser *);
1573 static tree cp_parser_multiplicative_expression
1574   (cp_parser *);
1575 static tree cp_parser_additive_expression
1576   (cp_parser *);
1577 static tree cp_parser_shift_expression
1578   (cp_parser *);
1579 static tree cp_parser_relational_expression
1580   (cp_parser *);
1581 static tree cp_parser_equality_expression
1582   (cp_parser *);
1583 static tree cp_parser_and_expression
1584   (cp_parser *);
1585 static tree cp_parser_exclusive_or_expression
1586   (cp_parser *);
1587 static tree cp_parser_inclusive_or_expression
1588   (cp_parser *);
1589 static tree cp_parser_logical_and_expression
1590   (cp_parser *);
1591 static tree cp_parser_logical_or_expression
1592   (cp_parser *);
1593 static tree cp_parser_question_colon_clause
1594   (cp_parser *, tree);
1595 static tree cp_parser_assignment_expression
1596   (cp_parser *);
1597 static enum tree_code cp_parser_assignment_operator_opt
1598   (cp_parser *);
1599 static tree cp_parser_expression
1600   (cp_parser *);
1601 static tree cp_parser_constant_expression
1602   (cp_parser *, bool, bool *);
1603 static tree cp_parser_builtin_offsetof
1604   (cp_parser *);
1605
1606 /* Statements [gram.stmt.stmt]  */
1607
1608 static void cp_parser_statement
1609   (cp_parser *, tree);
1610 static tree cp_parser_labeled_statement
1611   (cp_parser *, tree);
1612 static tree cp_parser_expression_statement
1613   (cp_parser *, tree);
1614 static tree cp_parser_compound_statement
1615   (cp_parser *, tree, bool);
1616 static void cp_parser_statement_seq_opt
1617   (cp_parser *, tree);
1618 static tree cp_parser_selection_statement
1619   (cp_parser *);
1620 static tree cp_parser_condition
1621   (cp_parser *);
1622 static tree cp_parser_iteration_statement
1623   (cp_parser *);
1624 static void cp_parser_for_init_statement
1625   (cp_parser *);
1626 static tree cp_parser_jump_statement
1627   (cp_parser *);
1628 static void cp_parser_declaration_statement
1629   (cp_parser *);
1630
1631 static tree cp_parser_implicitly_scoped_statement
1632   (cp_parser *);
1633 static void cp_parser_already_scoped_statement
1634   (cp_parser *);
1635
1636 /* Declarations [gram.dcl.dcl] */
1637
1638 static void cp_parser_declaration_seq_opt
1639   (cp_parser *);
1640 static void cp_parser_declaration
1641   (cp_parser *);
1642 static void cp_parser_block_declaration
1643   (cp_parser *, bool);
1644 static void cp_parser_simple_declaration
1645   (cp_parser *, bool);
1646 static void cp_parser_decl_specifier_seq
1647   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1648 static tree cp_parser_storage_class_specifier_opt
1649   (cp_parser *);
1650 static tree cp_parser_function_specifier_opt
1651   (cp_parser *, cp_decl_specifier_seq *);
1652 static tree cp_parser_type_specifier
1653   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1654    int *, bool *);
1655 static tree cp_parser_simple_type_specifier
1656   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1657 static tree cp_parser_type_name
1658   (cp_parser *);
1659 static tree cp_parser_elaborated_type_specifier
1660   (cp_parser *, bool, bool);
1661 static tree cp_parser_enum_specifier
1662   (cp_parser *);
1663 static void cp_parser_enumerator_list
1664   (cp_parser *, tree);
1665 static void cp_parser_enumerator_definition
1666   (cp_parser *, tree);
1667 static tree cp_parser_namespace_name
1668   (cp_parser *);
1669 static void cp_parser_namespace_definition
1670   (cp_parser *);
1671 static void cp_parser_namespace_body
1672   (cp_parser *);
1673 static tree cp_parser_qualified_namespace_specifier
1674   (cp_parser *);
1675 static void cp_parser_namespace_alias_definition
1676   (cp_parser *);
1677 static void cp_parser_using_declaration
1678   (cp_parser *);
1679 static void cp_parser_using_directive
1680   (cp_parser *);
1681 static void cp_parser_asm_definition
1682   (cp_parser *);
1683 static void cp_parser_linkage_specification
1684   (cp_parser *);
1685
1686 /* Declarators [gram.dcl.decl] */
1687
1688 static tree cp_parser_init_declarator
1689   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1690 static cp_declarator *cp_parser_declarator
1691   (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1692 static cp_declarator *cp_parser_direct_declarator
1693   (cp_parser *, cp_parser_declarator_kind, int *);
1694 static enum tree_code cp_parser_ptr_operator
1695   (cp_parser *, tree *, cp_cv_quals *);
1696 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1697   (cp_parser *);
1698 static tree cp_parser_declarator_id
1699   (cp_parser *);
1700 static tree cp_parser_type_id
1701   (cp_parser *);
1702 static void cp_parser_type_specifier_seq
1703   (cp_parser *, cp_decl_specifier_seq *);
1704 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1705   (cp_parser *);
1706 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1707   (cp_parser *, bool *);
1708 static cp_parameter_declarator *cp_parser_parameter_declaration
1709   (cp_parser *, bool, bool *);
1710 static void cp_parser_function_body
1711   (cp_parser *);
1712 static tree cp_parser_initializer
1713   (cp_parser *, bool *, bool *);
1714 static tree cp_parser_initializer_clause
1715   (cp_parser *, bool *);
1716 static tree cp_parser_initializer_list
1717   (cp_parser *, bool *);
1718
1719 static bool cp_parser_ctor_initializer_opt_and_function_body
1720   (cp_parser *);
1721
1722 /* Classes [gram.class] */
1723
1724 static tree cp_parser_class_name
1725   (cp_parser *, bool, bool, bool, bool, bool, bool);
1726 static tree cp_parser_class_specifier
1727   (cp_parser *);
1728 static tree cp_parser_class_head
1729   (cp_parser *, bool *, tree *);
1730 static enum tag_types cp_parser_class_key
1731   (cp_parser *);
1732 static void cp_parser_member_specification_opt
1733   (cp_parser *);
1734 static void cp_parser_member_declaration
1735   (cp_parser *);
1736 static tree cp_parser_pure_specifier
1737   (cp_parser *);
1738 static tree cp_parser_constant_initializer
1739   (cp_parser *);
1740
1741 /* Derived classes [gram.class.derived] */
1742
1743 static tree cp_parser_base_clause
1744   (cp_parser *);
1745 static tree cp_parser_base_specifier
1746   (cp_parser *);
1747
1748 /* Special member functions [gram.special] */
1749
1750 static tree cp_parser_conversion_function_id
1751   (cp_parser *);
1752 static tree cp_parser_conversion_type_id
1753   (cp_parser *);
1754 static cp_declarator *cp_parser_conversion_declarator_opt
1755   (cp_parser *);
1756 static bool cp_parser_ctor_initializer_opt
1757   (cp_parser *);
1758 static void cp_parser_mem_initializer_list
1759   (cp_parser *);
1760 static tree cp_parser_mem_initializer
1761   (cp_parser *);
1762 static tree cp_parser_mem_initializer_id
1763   (cp_parser *);
1764
1765 /* Overloading [gram.over] */
1766
1767 static tree cp_parser_operator_function_id
1768   (cp_parser *);
1769 static tree cp_parser_operator
1770   (cp_parser *);
1771
1772 /* Templates [gram.temp] */
1773
1774 static void cp_parser_template_declaration
1775   (cp_parser *, bool);
1776 static tree cp_parser_template_parameter_list
1777   (cp_parser *);
1778 static tree cp_parser_template_parameter
1779   (cp_parser *, bool *);
1780 static tree cp_parser_type_parameter
1781   (cp_parser *);
1782 static tree cp_parser_template_id
1783   (cp_parser *, bool, bool, bool);
1784 static tree cp_parser_template_name
1785   (cp_parser *, bool, bool, bool, bool *);
1786 static tree cp_parser_template_argument_list
1787   (cp_parser *);
1788 static tree cp_parser_template_argument
1789   (cp_parser *);
1790 static void cp_parser_explicit_instantiation
1791   (cp_parser *);
1792 static void cp_parser_explicit_specialization
1793   (cp_parser *);
1794
1795 /* Exception handling [gram.exception] */
1796
1797 static tree cp_parser_try_block
1798   (cp_parser *);
1799 static bool cp_parser_function_try_block
1800   (cp_parser *);
1801 static void cp_parser_handler_seq
1802   (cp_parser *);
1803 static void cp_parser_handler
1804   (cp_parser *);
1805 static tree cp_parser_exception_declaration
1806   (cp_parser *);
1807 static tree cp_parser_throw_expression
1808   (cp_parser *);
1809 static tree cp_parser_exception_specification_opt
1810   (cp_parser *);
1811 static tree cp_parser_type_id_list
1812   (cp_parser *);
1813
1814 /* GNU Extensions */
1815
1816 static tree cp_parser_asm_specification_opt
1817   (cp_parser *);
1818 static tree cp_parser_asm_operand_list
1819   (cp_parser *);
1820 static tree cp_parser_asm_clobber_list
1821   (cp_parser *);
1822 static tree cp_parser_attributes_opt
1823   (cp_parser *);
1824 static tree cp_parser_attribute_list
1825   (cp_parser *);
1826 static bool cp_parser_extension_opt
1827   (cp_parser *, int *);
1828 static void cp_parser_label_declaration
1829   (cp_parser *);
1830
1831 /* Utility Routines */
1832
1833 static tree cp_parser_lookup_name
1834   (cp_parser *, tree, bool, bool, bool, bool);
1835 static tree cp_parser_lookup_name_simple
1836   (cp_parser *, tree);
1837 static tree cp_parser_maybe_treat_template_as_class
1838   (tree, bool);
1839 static bool cp_parser_check_declarator_template_parameters
1840   (cp_parser *, cp_declarator *);
1841 static bool cp_parser_check_template_parameters
1842   (cp_parser *, unsigned);
1843 static tree cp_parser_simple_cast_expression
1844   (cp_parser *);
1845 static tree cp_parser_binary_expression
1846   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1847 static tree cp_parser_global_scope_opt
1848   (cp_parser *, bool);
1849 static bool cp_parser_constructor_declarator_p
1850   (cp_parser *, bool);
1851 static tree cp_parser_function_definition_from_specifiers_and_declarator
1852   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1853 static tree cp_parser_function_definition_after_declarator
1854   (cp_parser *, bool);
1855 static void cp_parser_template_declaration_after_export
1856   (cp_parser *, bool);
1857 static tree cp_parser_single_declaration
1858   (cp_parser *, bool, bool *);
1859 static tree cp_parser_functional_cast
1860   (cp_parser *, tree);
1861 static tree cp_parser_save_member_function_body
1862   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1863 static tree cp_parser_enclosed_template_argument_list
1864   (cp_parser *);
1865 static void cp_parser_save_default_args
1866   (cp_parser *, tree);
1867 static void cp_parser_late_parsing_for_member
1868   (cp_parser *, tree);
1869 static void cp_parser_late_parsing_default_args
1870   (cp_parser *, tree);
1871 static tree cp_parser_sizeof_operand
1872   (cp_parser *, enum rid);
1873 static bool cp_parser_declares_only_class_p
1874   (cp_parser *);
1875 static void cp_parser_set_storage_class
1876   (cp_decl_specifier_seq *, cp_storage_class);
1877 static void cp_parser_set_decl_spec_type
1878   (cp_decl_specifier_seq *, tree, bool);
1879 static bool cp_parser_friend_p
1880   (const cp_decl_specifier_seq *);
1881 static cp_token *cp_parser_require
1882   (cp_parser *, enum cpp_ttype, const char *);
1883 static cp_token *cp_parser_require_keyword
1884   (cp_parser *, enum rid, const char *);
1885 static bool cp_parser_token_starts_function_definition_p
1886   (cp_token *);
1887 static bool cp_parser_next_token_starts_class_definition_p
1888   (cp_parser *);
1889 static bool cp_parser_next_token_ends_template_argument_p
1890   (cp_parser *);
1891 static bool cp_parser_nth_token_starts_template_argument_list_p
1892   (cp_parser *, size_t);
1893 static enum tag_types cp_parser_token_is_class_key
1894   (cp_token *);
1895 static void cp_parser_check_class_key
1896   (enum tag_types, tree type);
1897 static void cp_parser_check_access_in_redeclaration
1898   (tree type);
1899 static bool cp_parser_optional_template_keyword
1900   (cp_parser *);
1901 static void cp_parser_pre_parsed_nested_name_specifier
1902   (cp_parser *);
1903 static void cp_parser_cache_group
1904   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1905 static void cp_parser_parse_tentatively
1906   (cp_parser *);
1907 static void cp_parser_commit_to_tentative_parse
1908   (cp_parser *);
1909 static void cp_parser_abort_tentative_parse
1910   (cp_parser *);
1911 static bool cp_parser_parse_definitely
1912   (cp_parser *);
1913 static inline bool cp_parser_parsing_tentatively
1914   (cp_parser *);
1915 static bool cp_parser_committed_to_tentative_parse
1916   (cp_parser *);
1917 static void cp_parser_error
1918   (cp_parser *, const char *);
1919 static void cp_parser_name_lookup_error
1920   (cp_parser *, tree, tree, const char *);
1921 static bool cp_parser_simulate_error
1922   (cp_parser *);
1923 static void cp_parser_check_type_definition
1924   (cp_parser *);
1925 static void cp_parser_check_for_definition_in_return_type
1926   (cp_declarator *, int);
1927 static void cp_parser_check_for_invalid_template_id
1928   (cp_parser *, tree);
1929 static bool cp_parser_non_integral_constant_expression
1930   (cp_parser *, const char *);
1931 static void cp_parser_diagnose_invalid_type_name
1932   (cp_parser *, tree, tree);
1933 static bool cp_parser_parse_and_diagnose_invalid_type_name
1934   (cp_parser *);
1935 static int cp_parser_skip_to_closing_parenthesis
1936   (cp_parser *, bool, bool, bool);
1937 static void cp_parser_skip_to_end_of_statement
1938   (cp_parser *);
1939 static void cp_parser_consume_semicolon_at_end_of_statement
1940   (cp_parser *);
1941 static void cp_parser_skip_to_end_of_block_or_statement
1942   (cp_parser *);
1943 static void cp_parser_skip_to_closing_brace
1944   (cp_parser *);
1945 static void cp_parser_skip_until_found
1946   (cp_parser *, enum cpp_ttype, const char *);
1947 static bool cp_parser_error_occurred
1948   (cp_parser *);
1949 static bool cp_parser_allow_gnu_extensions_p
1950   (cp_parser *);
1951 static bool cp_parser_is_string_literal
1952   (cp_token *);
1953 static bool cp_parser_is_keyword
1954   (cp_token *, enum rid);
1955 static tree cp_parser_make_typename_type
1956   (cp_parser *, tree, tree);
1957
1958 /* Returns nonzero if we are parsing tentatively.  */
1959
1960 static inline bool
1961 cp_parser_parsing_tentatively (cp_parser* parser)
1962 {
1963   return parser->context->next != NULL;
1964 }
1965
1966 /* Returns nonzero if TOKEN is a string literal.  */
1967
1968 static bool
1969 cp_parser_is_string_literal (cp_token* token)
1970 {
1971   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1972 }
1973
1974 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1975
1976 static bool
1977 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1978 {
1979   return token->keyword == keyword;
1980 }
1981
1982 /* Issue the indicated error MESSAGE.  */
1983
1984 static void
1985 cp_parser_error (cp_parser* parser, const char* message)
1986 {
1987   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1988   if (!cp_parser_simulate_error (parser))
1989     {
1990       cp_token *token;
1991       token = cp_lexer_peek_token (parser->lexer);
1992       c_parse_error (message,
1993                      /* Because c_parser_error does not understand
1994                         CPP_KEYWORD, keywords are treated like
1995                         identifiers.  */
1996                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1997                      token->value);
1998     }
1999 }
2000
2001 /* Issue an error about name-lookup failing.  NAME is the
2002    IDENTIFIER_NODE DECL is the result of
2003    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2004    the thing that we hoped to find.  */
2005
2006 static void
2007 cp_parser_name_lookup_error (cp_parser* parser,
2008                              tree name,
2009                              tree decl,
2010                              const char* desired)
2011 {
2012   /* If name lookup completely failed, tell the user that NAME was not
2013      declared.  */
2014   if (decl == error_mark_node)
2015     {
2016       if (parser->scope && parser->scope != global_namespace)
2017         error ("`%D::%D' has not been declared",
2018                parser->scope, name);
2019       else if (parser->scope == global_namespace)
2020         error ("`::%D' has not been declared", name);
2021       else
2022         error ("`%D' has not been declared", name);
2023     }
2024   else if (parser->scope && parser->scope != global_namespace)
2025     error ("`%D::%D' %s", parser->scope, name, desired);
2026   else if (parser->scope == global_namespace)
2027     error ("`::%D' %s", name, desired);
2028   else
2029     error ("`%D' %s", name, desired);
2030 }
2031
2032 /* If we are parsing tentatively, remember that an error has occurred
2033    during this tentative parse.  Returns true if the error was
2034    simulated; false if a message should be issued by the caller.  */
2035
2036 static bool
2037 cp_parser_simulate_error (cp_parser* parser)
2038 {
2039   if (cp_parser_parsing_tentatively (parser)
2040       && !cp_parser_committed_to_tentative_parse (parser))
2041     {
2042       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2043       return true;
2044     }
2045   return false;
2046 }
2047
2048 /* This function is called when a type is defined.  If type
2049    definitions are forbidden at this point, an error message is
2050    issued.  */
2051
2052 static void
2053 cp_parser_check_type_definition (cp_parser* parser)
2054 {
2055   /* If types are forbidden here, issue a message.  */
2056   if (parser->type_definition_forbidden_message)
2057     /* Use `%s' to print the string in case there are any escape
2058        characters in the message.  */
2059     error ("%s", parser->type_definition_forbidden_message);
2060 }
2061
2062 /* This function is called when a declaration is parsed.  If
2063    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2064    indicates that a type was defined in the decl-specifiers for DECL,
2065    then an error is issued.  */
2066
2067 static void
2068 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2069                                                int declares_class_or_enum)
2070 {
2071   /* [dcl.fct] forbids type definitions in return types.
2072      Unfortunately, it's not easy to know whether or not we are
2073      processing a return type until after the fact.  */
2074   while (declarator
2075          && (declarator->kind == cdk_pointer
2076              || declarator->kind == cdk_reference
2077              || declarator->kind == cdk_ptrmem))
2078     declarator = declarator->declarator;
2079   if (declarator
2080       && declarator->kind == cdk_function
2081       && declares_class_or_enum & 2)
2082     error ("new types may not be defined in a return type");
2083 }
2084
2085 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2086    "<" in any valid C++ program.  If the next token is indeed "<",
2087    issue a message warning the user about what appears to be an
2088    invalid attempt to form a template-id.  */
2089
2090 static void
2091 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2092                                          tree type)
2093 {
2094   ptrdiff_t start;
2095   cp_token *token;
2096
2097   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2098     {
2099       if (TYPE_P (type))
2100         error ("`%T' is not a template", type);
2101       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2102         error ("`%E' is not a template", type);
2103       else
2104         error ("invalid template-id");
2105       /* Remember the location of the invalid "<".  */
2106       if (cp_parser_parsing_tentatively (parser)
2107           && !cp_parser_committed_to_tentative_parse (parser))
2108         {
2109           token = cp_lexer_peek_token (parser->lexer);
2110           token = cp_lexer_prev_token (parser->lexer, token);
2111           start = cp_lexer_token_difference (parser->lexer,
2112                                              parser->lexer->first_token,
2113                                              token);
2114         }
2115       else
2116         start = -1;
2117       /* Consume the "<".  */
2118       cp_lexer_consume_token (parser->lexer);
2119       /* Parse the template arguments.  */
2120       cp_parser_enclosed_template_argument_list (parser);
2121       /* Permanently remove the invalid template arguments so that
2122          this error message is not issued again.  */
2123       if (start >= 0)
2124         {
2125           token = cp_lexer_advance_token (parser->lexer,
2126                                           parser->lexer->first_token,
2127                                           start);
2128           cp_lexer_purge_tokens_after (parser->lexer, token);
2129         }
2130     }
2131 }
2132
2133 /* If parsing an integral constant-expression, issue an error message
2134    about the fact that THING appeared and return true.  Otherwise,
2135    return false, marking the current expression as non-constant.  */
2136
2137 static bool
2138 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2139                                             const char *thing)
2140 {
2141   if (parser->integral_constant_expression_p)
2142     {
2143       if (!parser->allow_non_integral_constant_expression_p)
2144         {
2145           error ("%s cannot appear in a constant-expression", thing);
2146           return true;
2147         }
2148       parser->non_integral_constant_expression_p = true;
2149     }
2150   return false;
2151 }
2152
2153 /* Emit a diagnostic for an invalid type name. Consider also if it is
2154    qualified or not and the result of a lookup, to provide a better
2155    message.  */
2156
2157 static void
2158 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2159 {
2160   tree decl, old_scope;
2161   /* Try to lookup the identifier.  */
2162   old_scope = parser->scope;
2163   parser->scope = scope;
2164   decl = cp_parser_lookup_name_simple (parser, id);
2165   parser->scope = old_scope;
2166   /* If the lookup found a template-name, it means that the user forgot
2167   to specify an argument list. Emit an useful error message.  */
2168   if (TREE_CODE (decl) == TEMPLATE_DECL)
2169     error ("invalid use of template-name `%E' without an argument list",
2170       decl);
2171   else if (!parser->scope)
2172     {
2173       /* Issue an error message.  */
2174       error ("`%E' does not name a type", id);
2175       /* If we're in a template class, it's possible that the user was
2176          referring to a type from a base class.  For example:
2177
2178            template <typename T> struct A { typedef T X; };
2179            template <typename T> struct B : public A<T> { X x; };
2180
2181          The user should have said "typename A<T>::X".  */
2182       if (processing_template_decl && current_class_type)
2183         {
2184           tree b;
2185
2186           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2187                b;
2188                b = TREE_CHAIN (b))
2189             {
2190               tree base_type = BINFO_TYPE (b);
2191               if (CLASS_TYPE_P (base_type)
2192                   && dependent_type_p (base_type))
2193                 {
2194                   tree field;
2195                   /* Go from a particular instantiation of the
2196                      template (which will have an empty TYPE_FIELDs),
2197                      to the main version.  */
2198                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2199                   for (field = TYPE_FIELDS (base_type);
2200                        field;
2201                        field = TREE_CHAIN (field))
2202                     if (TREE_CODE (field) == TYPE_DECL
2203                         && DECL_NAME (field) == id)
2204                       {
2205                         inform ("(perhaps `typename %T::%E' was intended)",
2206                                 BINFO_TYPE (b), id);
2207                         break;
2208                       }
2209                   if (field)
2210                     break;
2211                 }
2212             }
2213         }
2214     }
2215   /* Here we diagnose qualified-ids where the scope is actually correct,
2216      but the identifier does not resolve to a valid type name.  */
2217   else
2218     {
2219       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2220         error ("`%E' in namespace `%E' does not name a type",
2221                id, parser->scope);
2222       else if (TYPE_P (parser->scope))
2223         error ("`%E' in class `%T' does not name a type",
2224                id, parser->scope);
2225       else
2226         abort();
2227     }
2228 }
2229
2230 /* Check for a common situation where a type-name should be present,
2231    but is not, and issue a sensible error message.  Returns true if an
2232    invalid type-name was detected.
2233
2234    The situation handled by this function are variable declarations of the
2235    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2236    Usually, `ID' should name a type, but if we got here it means that it
2237    does not. We try to emit the best possible error message depending on
2238    how exactly the id-expression looks like.
2239 */
2240
2241 static bool
2242 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2243 {
2244   tree id;
2245
2246   cp_parser_parse_tentatively (parser);
2247   id = cp_parser_id_expression (parser,
2248                                 /*template_keyword_p=*/false,
2249                                 /*check_dependency_p=*/true,
2250                                 /*template_p=*/NULL,
2251                                 /*declarator_p=*/true);
2252   /* After the id-expression, there should be a plain identifier,
2253      otherwise this is not a simple variable declaration. Also, if
2254      the scope is dependent, we cannot do much.  */
2255   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2256       || (parser->scope && TYPE_P (parser->scope)
2257           && dependent_type_p (parser->scope)))
2258     {
2259       cp_parser_abort_tentative_parse (parser);
2260       return false;
2261     }
2262   if (!cp_parser_parse_definitely (parser))
2263     return false;
2264
2265   /* If we got here, this cannot be a valid variable declaration, thus
2266      the cp_parser_id_expression must have resolved to a plain identifier
2267      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
2268   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2269   /* Emit a diagnostic for the invalid type.  */
2270   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2271   /* Skip to the end of the declaration; there's no point in
2272      trying to process it.  */
2273   cp_parser_skip_to_end_of_block_or_statement (parser);
2274   return true;
2275 }
2276
2277 /* Consume tokens up to, and including, the next non-nested closing `)'.
2278    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2279    are doing error recovery. Returns -1 if OR_COMMA is true and we
2280    found an unnested comma.  */
2281
2282 static int
2283 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2284                                        bool recovering,
2285                                        bool or_comma,
2286                                        bool consume_paren)
2287 {
2288   unsigned paren_depth = 0;
2289   unsigned brace_depth = 0;
2290   int saved_c_lex_string_translate = c_lex_string_translate;
2291   int result;
2292
2293   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2294       && !cp_parser_committed_to_tentative_parse (parser))
2295     return 0;
2296
2297   if (! recovering)
2298     /* If we're looking ahead, keep both translated and untranslated
2299        strings.  */
2300     c_lex_string_translate = -1;
2301
2302   while (true)
2303     {
2304       cp_token *token;
2305
2306       /* If we've run out of tokens, then there is no closing `)'.  */
2307       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2308         {
2309           result = 0;
2310           break;
2311         }
2312
2313       token = cp_lexer_peek_token (parser->lexer);
2314
2315       /* This matches the processing in skip_to_end_of_statement.  */
2316       if (token->type == CPP_SEMICOLON && !brace_depth)
2317         {
2318           result = 0;
2319           break;
2320         }
2321       if (token->type == CPP_OPEN_BRACE)
2322         ++brace_depth;
2323       if (token->type == CPP_CLOSE_BRACE)
2324         {
2325           if (!brace_depth--)
2326             {
2327               result = 0;
2328               break;
2329             }
2330         }
2331       if (recovering && or_comma && token->type == CPP_COMMA
2332           && !brace_depth && !paren_depth)
2333         {
2334           result = -1;
2335           break;
2336         }
2337
2338       if (!brace_depth)
2339         {
2340           /* If it is an `(', we have entered another level of nesting.  */
2341           if (token->type == CPP_OPEN_PAREN)
2342             ++paren_depth;
2343           /* If it is a `)', then we might be done.  */
2344           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2345             {
2346               if (consume_paren)
2347                 cp_lexer_consume_token (parser->lexer);
2348               {
2349                 result = 1;
2350                 break;
2351               }
2352             }
2353         }
2354
2355       /* Consume the token.  */
2356       cp_lexer_consume_token (parser->lexer);
2357     }
2358
2359   c_lex_string_translate = saved_c_lex_string_translate;
2360   return result;
2361 }
2362
2363 /* Consume tokens until we reach the end of the current statement.
2364    Normally, that will be just before consuming a `;'.  However, if a
2365    non-nested `}' comes first, then we stop before consuming that.  */
2366
2367 static void
2368 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2369 {
2370   unsigned nesting_depth = 0;
2371
2372   while (true)
2373     {
2374       cp_token *token;
2375
2376       /* Peek at the next token.  */
2377       token = cp_lexer_peek_token (parser->lexer);
2378       /* If we've run out of tokens, stop.  */
2379       if (token->type == CPP_EOF)
2380         break;
2381       /* If the next token is a `;', we have reached the end of the
2382          statement.  */
2383       if (token->type == CPP_SEMICOLON && !nesting_depth)
2384         break;
2385       /* If the next token is a non-nested `}', then we have reached
2386          the end of the current block.  */
2387       if (token->type == CPP_CLOSE_BRACE)
2388         {
2389           /* If this is a non-nested `}', stop before consuming it.
2390              That way, when confronted with something like:
2391
2392                { 3 + }
2393
2394              we stop before consuming the closing `}', even though we
2395              have not yet reached a `;'.  */
2396           if (nesting_depth == 0)
2397             break;
2398           /* If it is the closing `}' for a block that we have
2399              scanned, stop -- but only after consuming the token.
2400              That way given:
2401
2402                 void f g () { ... }
2403                 typedef int I;
2404
2405              we will stop after the body of the erroneously declared
2406              function, but before consuming the following `typedef'
2407              declaration.  */
2408           if (--nesting_depth == 0)
2409             {
2410               cp_lexer_consume_token (parser->lexer);
2411               break;
2412             }
2413         }
2414       /* If it the next token is a `{', then we are entering a new
2415          block.  Consume the entire block.  */
2416       else if (token->type == CPP_OPEN_BRACE)
2417         ++nesting_depth;
2418       /* Consume the token.  */
2419       cp_lexer_consume_token (parser->lexer);
2420     }
2421 }
2422
2423 /* This function is called at the end of a statement or declaration.
2424    If the next token is a semicolon, it is consumed; otherwise, error
2425    recovery is attempted.  */
2426
2427 static void
2428 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2429 {
2430   /* Look for the trailing `;'.  */
2431   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2432     {
2433       /* If there is additional (erroneous) input, skip to the end of
2434          the statement.  */
2435       cp_parser_skip_to_end_of_statement (parser);
2436       /* If the next token is now a `;', consume it.  */
2437       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2438         cp_lexer_consume_token (parser->lexer);
2439     }
2440 }
2441
2442 /* Skip tokens until we have consumed an entire block, or until we
2443    have consumed a non-nested `;'.  */
2444
2445 static void
2446 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2447 {
2448   unsigned nesting_depth = 0;
2449
2450   while (true)
2451     {
2452       cp_token *token;
2453
2454       /* Peek at the next token.  */
2455       token = cp_lexer_peek_token (parser->lexer);
2456       /* If we've run out of tokens, stop.  */
2457       if (token->type == CPP_EOF)
2458         break;
2459       /* If the next token is a `;', we have reached the end of the
2460          statement.  */
2461       if (token->type == CPP_SEMICOLON && !nesting_depth)
2462         {
2463           /* Consume the `;'.  */
2464           cp_lexer_consume_token (parser->lexer);
2465           break;
2466         }
2467       /* Consume the token.  */
2468       token = cp_lexer_consume_token (parser->lexer);
2469       /* If the next token is a non-nested `}', then we have reached
2470          the end of the current block.  */
2471       if (token->type == CPP_CLOSE_BRACE
2472           && (nesting_depth == 0 || --nesting_depth == 0))
2473         break;
2474       /* If it the next token is a `{', then we are entering a new
2475          block.  Consume the entire block.  */
2476       if (token->type == CPP_OPEN_BRACE)
2477         ++nesting_depth;
2478     }
2479 }
2480
2481 /* Skip tokens until a non-nested closing curly brace is the next
2482    token.  */
2483
2484 static void
2485 cp_parser_skip_to_closing_brace (cp_parser *parser)
2486 {
2487   unsigned nesting_depth = 0;
2488
2489   while (true)
2490     {
2491       cp_token *token;
2492
2493       /* Peek at the next token.  */
2494       token = cp_lexer_peek_token (parser->lexer);
2495       /* If we've run out of tokens, stop.  */
2496       if (token->type == CPP_EOF)
2497         break;
2498       /* If the next token is a non-nested `}', then we have reached
2499          the end of the current block.  */
2500       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2501         break;
2502       /* If it the next token is a `{', then we are entering a new
2503          block.  Consume the entire block.  */
2504       else if (token->type == CPP_OPEN_BRACE)
2505         ++nesting_depth;
2506       /* Consume the token.  */
2507       cp_lexer_consume_token (parser->lexer);
2508     }
2509 }
2510
2511 /* This is a simple wrapper around make_typename_type. When the id is
2512    an unresolved identifier node, we can provide a superior diagnostic
2513    using cp_parser_diagnose_invalid_type_name.  */
2514
2515 static tree
2516 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2517 {
2518   tree result;
2519   if (TREE_CODE (id) == IDENTIFIER_NODE)
2520     {
2521       result = make_typename_type (scope, id, /*complain=*/0);
2522       if (result == error_mark_node)
2523         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2524       return result;
2525     }
2526   return make_typename_type (scope, id, tf_error);
2527 }
2528
2529
2530 /* Create a new C++ parser.  */
2531
2532 static cp_parser *
2533 cp_parser_new (void)
2534 {
2535   cp_parser *parser;
2536   cp_lexer *lexer;
2537
2538   /* cp_lexer_new_main is called before calling ggc_alloc because
2539      cp_lexer_new_main might load a PCH file.  */
2540   lexer = cp_lexer_new_main ();
2541
2542   parser = GGC_CNEW (cp_parser);
2543   parser->lexer = lexer;
2544   parser->context = cp_parser_context_new (NULL);
2545
2546   /* For now, we always accept GNU extensions.  */
2547   parser->allow_gnu_extensions_p = 1;
2548
2549   /* The `>' token is a greater-than operator, not the end of a
2550      template-id.  */
2551   parser->greater_than_is_operator_p = true;
2552
2553   parser->default_arg_ok_p = true;
2554
2555   /* We are not parsing a constant-expression.  */
2556   parser->integral_constant_expression_p = false;
2557   parser->allow_non_integral_constant_expression_p = false;
2558   parser->non_integral_constant_expression_p = false;
2559
2560   /* Local variable names are not forbidden.  */
2561   parser->local_variables_forbidden_p = false;
2562
2563   /* We are not processing an `extern "C"' declaration.  */
2564   parser->in_unbraced_linkage_specification_p = false;
2565
2566   /* We are not processing a declarator.  */
2567   parser->in_declarator_p = false;
2568
2569   /* We are not processing a template-argument-list.  */
2570   parser->in_template_argument_list_p = false;
2571
2572   /* We are not in an iteration statement.  */
2573   parser->in_iteration_statement_p = false;
2574
2575   /* We are not in a switch statement.  */
2576   parser->in_switch_statement_p = false;
2577
2578   /* We are not parsing a type-id inside an expression.  */
2579   parser->in_type_id_in_expr_p = false;
2580
2581   /* The unparsed function queue is empty.  */
2582   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2583
2584   /* There are no classes being defined.  */
2585   parser->num_classes_being_defined = 0;
2586
2587   /* No template parameters apply.  */
2588   parser->num_template_parameter_lists = 0;
2589
2590   return parser;
2591 }
2592
2593 /* Lexical conventions [gram.lex]  */
2594
2595 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2596    identifier.  */
2597
2598 static tree
2599 cp_parser_identifier (cp_parser* parser)
2600 {
2601   cp_token *token;
2602
2603   /* Look for the identifier.  */
2604   token = cp_parser_require (parser, CPP_NAME, "identifier");
2605   /* Return the value.  */
2606   return token ? token->value : error_mark_node;
2607 }
2608
2609 /* Basic concepts [gram.basic]  */
2610
2611 /* Parse a translation-unit.
2612
2613    translation-unit:
2614      declaration-seq [opt]
2615
2616    Returns TRUE if all went well.  */
2617
2618 static bool
2619 cp_parser_translation_unit (cp_parser* parser)
2620 {
2621   /* The address of the first non-permanent object on the declarator
2622      obstack.  */
2623   static void *declarator_obstack_base;
2624
2625   bool success;
2626
2627   /* Create the declarator obstack, if necessary.  */
2628   if (!cp_error_declarator)
2629     {
2630       gcc_obstack_init (&declarator_obstack);
2631       /* Create the error declarator.  */
2632       cp_error_declarator = make_declarator (cdk_error);
2633       /* Create the empty parameter list.  */
2634       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2635       /* Remember where the base of the declarator obstack lies.  */
2636       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2637     }
2638
2639   while (true)
2640     {
2641       cp_parser_declaration_seq_opt (parser);
2642
2643       /* If there are no tokens left then all went well.  */
2644       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2645         {
2646           /* Consume the EOF token.  */
2647           cp_parser_require (parser, CPP_EOF, "end-of-file");
2648
2649           /* Finish up.  */
2650           finish_translation_unit ();
2651
2652           success = true;
2653           break;
2654         }
2655       else
2656         {
2657           cp_parser_error (parser, "expected declaration");
2658           success = false;
2659           break;
2660         }
2661     }
2662
2663   /* Make sure the declarator obstack was fully cleaned up.  */
2664   my_friendly_assert (obstack_next_free (&declarator_obstack) ==
2665                       declarator_obstack_base,
2666                       20040621);
2667
2668   /* All went well.  */
2669   return success;
2670 }
2671
2672 /* Expressions [gram.expr] */
2673
2674 /* Parse a primary-expression.
2675
2676    primary-expression:
2677      literal
2678      this
2679      ( expression )
2680      id-expression
2681
2682    GNU Extensions:
2683
2684    primary-expression:
2685      ( compound-statement )
2686      __builtin_va_arg ( assignment-expression , type-id )
2687
2688    literal:
2689      __null
2690
2691    Returns a representation of the expression.
2692
2693    *IDK indicates what kind of id-expression (if any) was present.
2694
2695    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2696    used as the operand of a pointer-to-member.  In that case,
2697    *QUALIFYING_CLASS gives the class that is used as the qualifying
2698    class in the pointer-to-member.  */
2699
2700 static tree
2701 cp_parser_primary_expression (cp_parser *parser,
2702                               cp_id_kind *idk,
2703                               tree *qualifying_class)
2704 {
2705   cp_token *token;
2706
2707   /* Assume the primary expression is not an id-expression.  */
2708   *idk = CP_ID_KIND_NONE;
2709   /* And that it cannot be used as pointer-to-member.  */
2710   *qualifying_class = NULL_TREE;
2711
2712   /* Peek at the next token.  */
2713   token = cp_lexer_peek_token (parser->lexer);
2714   switch (token->type)
2715     {
2716       /* literal:
2717            integer-literal
2718            character-literal
2719            floating-literal
2720            string-literal
2721            boolean-literal  */
2722     case CPP_CHAR:
2723     case CPP_WCHAR:
2724     case CPP_NUMBER:
2725       token = cp_lexer_consume_token (parser->lexer);
2726       return token->value;
2727
2728     case CPP_STRING:
2729     case CPP_WSTRING:
2730       token = cp_lexer_consume_token (parser->lexer);
2731       if (TREE_CHAIN (token->value))
2732         return TREE_CHAIN (token->value);
2733       else
2734         return token->value;
2735
2736     case CPP_OPEN_PAREN:
2737       {
2738         tree expr;
2739         bool saved_greater_than_is_operator_p;
2740
2741         /* Consume the `('.  */
2742         cp_lexer_consume_token (parser->lexer);
2743         /* Within a parenthesized expression, a `>' token is always
2744            the greater-than operator.  */
2745         saved_greater_than_is_operator_p
2746           = parser->greater_than_is_operator_p;
2747         parser->greater_than_is_operator_p = true;
2748         /* If we see `( { ' then we are looking at the beginning of
2749            a GNU statement-expression.  */
2750         if (cp_parser_allow_gnu_extensions_p (parser)
2751             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2752           {
2753             /* Statement-expressions are not allowed by the standard.  */
2754             if (pedantic)
2755               pedwarn ("ISO C++ forbids braced-groups within expressions");
2756
2757             /* And they're not allowed outside of a function-body; you
2758                cannot, for example, write:
2759
2760                  int i = ({ int j = 3; j + 1; });
2761
2762                at class or namespace scope.  */
2763             if (!at_function_scope_p ())
2764               error ("statement-expressions are allowed only inside functions");
2765             /* Start the statement-expression.  */
2766             expr = begin_stmt_expr ();
2767             /* Parse the compound-statement.  */
2768             cp_parser_compound_statement (parser, expr, false);
2769             /* Finish up.  */
2770             expr = finish_stmt_expr (expr, false);
2771           }
2772         else
2773           {
2774             /* Parse the parenthesized expression.  */
2775             expr = cp_parser_expression (parser);
2776             /* Let the front end know that this expression was
2777                enclosed in parentheses. This matters in case, for
2778                example, the expression is of the form `A::B', since
2779                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2780                not.  */
2781             finish_parenthesized_expr (expr);
2782           }
2783         /* The `>' token might be the end of a template-id or
2784            template-parameter-list now.  */
2785         parser->greater_than_is_operator_p
2786           = saved_greater_than_is_operator_p;
2787         /* Consume the `)'.  */
2788         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2789           cp_parser_skip_to_end_of_statement (parser);
2790
2791         return expr;
2792       }
2793
2794     case CPP_KEYWORD:
2795       switch (token->keyword)
2796         {
2797           /* These two are the boolean literals.  */
2798         case RID_TRUE:
2799           cp_lexer_consume_token (parser->lexer);
2800           return boolean_true_node;
2801         case RID_FALSE:
2802           cp_lexer_consume_token (parser->lexer);
2803           return boolean_false_node;
2804
2805           /* The `__null' literal.  */
2806         case RID_NULL:
2807           cp_lexer_consume_token (parser->lexer);
2808           return null_node;
2809
2810           /* Recognize the `this' keyword.  */
2811         case RID_THIS:
2812           cp_lexer_consume_token (parser->lexer);
2813           if (parser->local_variables_forbidden_p)
2814             {
2815               error ("`this' may not be used in this context");
2816               return error_mark_node;
2817             }
2818           /* Pointers cannot appear in constant-expressions.  */
2819           if (cp_parser_non_integral_constant_expression (parser,
2820                                                           "`this'"))
2821             return error_mark_node;
2822           return finish_this_expr ();
2823
2824           /* The `operator' keyword can be the beginning of an
2825              id-expression.  */
2826         case RID_OPERATOR:
2827           goto id_expression;
2828
2829         case RID_FUNCTION_NAME:
2830         case RID_PRETTY_FUNCTION_NAME:
2831         case RID_C99_FUNCTION_NAME:
2832           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2833              __func__ are the names of variables -- but they are
2834              treated specially.  Therefore, they are handled here,
2835              rather than relying on the generic id-expression logic
2836              below.  Grammatically, these names are id-expressions.
2837
2838              Consume the token.  */
2839           token = cp_lexer_consume_token (parser->lexer);
2840           /* Look up the name.  */
2841           return finish_fname (token->value);
2842
2843         case RID_VA_ARG:
2844           {
2845             tree expression;
2846             tree type;
2847
2848             /* The `__builtin_va_arg' construct is used to handle
2849                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2850             cp_lexer_consume_token (parser->lexer);
2851             /* Look for the opening `('.  */
2852             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2853             /* Now, parse the assignment-expression.  */
2854             expression = cp_parser_assignment_expression (parser);
2855             /* Look for the `,'.  */
2856             cp_parser_require (parser, CPP_COMMA, "`,'");
2857             /* Parse the type-id.  */
2858             type = cp_parser_type_id (parser);
2859             /* Look for the closing `)'.  */
2860             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2861             /* Using `va_arg' in a constant-expression is not
2862                allowed.  */
2863             if (cp_parser_non_integral_constant_expression (parser,
2864                                                             "`va_arg'"))
2865               return error_mark_node;
2866             return build_x_va_arg (expression, type);
2867           }
2868
2869         case RID_OFFSETOF:
2870           return cp_parser_builtin_offsetof (parser);
2871
2872         default:
2873           cp_parser_error (parser, "expected primary-expression");
2874           return error_mark_node;
2875         }
2876
2877       /* An id-expression can start with either an identifier, a
2878          `::' as the beginning of a qualified-id, or the "operator"
2879          keyword.  */
2880     case CPP_NAME:
2881     case CPP_SCOPE:
2882     case CPP_TEMPLATE_ID:
2883     case CPP_NESTED_NAME_SPECIFIER:
2884       {
2885         tree id_expression;
2886         tree decl;
2887         const char *error_msg;
2888
2889       id_expression:
2890         /* Parse the id-expression.  */
2891         id_expression
2892           = cp_parser_id_expression (parser,
2893                                      /*template_keyword_p=*/false,
2894                                      /*check_dependency_p=*/true,
2895                                      /*template_p=*/NULL,
2896                                      /*declarator_p=*/false);
2897         if (id_expression == error_mark_node)
2898           return error_mark_node;
2899         /* If we have a template-id, then no further lookup is
2900            required.  If the template-id was for a template-class, we
2901            will sometimes have a TYPE_DECL at this point.  */
2902         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2903             || TREE_CODE (id_expression) == TYPE_DECL)
2904           decl = id_expression;
2905         /* Look up the name.  */
2906         else
2907           {
2908             decl = cp_parser_lookup_name_simple (parser, id_expression);
2909             /* If name lookup gives us a SCOPE_REF, then the
2910                qualifying scope was dependent.  Just propagate the
2911                name.  */
2912             if (TREE_CODE (decl) == SCOPE_REF)
2913               {
2914                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2915                   *qualifying_class = TREE_OPERAND (decl, 0);
2916                 return decl;
2917               }
2918             /* Check to see if DECL is a local variable in a context
2919                where that is forbidden.  */
2920             if (parser->local_variables_forbidden_p
2921                 && local_variable_p (decl))
2922               {
2923                 /* It might be that we only found DECL because we are
2924                    trying to be generous with pre-ISO scoping rules.
2925                    For example, consider:
2926
2927                      int i;
2928                      void g() {
2929                        for (int i = 0; i < 10; ++i) {}
2930                        extern void f(int j = i);
2931                      }
2932
2933                    Here, name look up will originally find the out
2934                    of scope `i'.  We need to issue a warning message,
2935                    but then use the global `i'.  */
2936                 decl = check_for_out_of_scope_variable (decl);
2937                 if (local_variable_p (decl))
2938                   {
2939                     error ("local variable `%D' may not appear in this context",
2940                            decl);
2941                     return error_mark_node;
2942                   }
2943               }
2944           }
2945
2946         decl = finish_id_expression (id_expression, decl, parser->scope,
2947                                      idk, qualifying_class,
2948                                      parser->integral_constant_expression_p,
2949                                      parser->allow_non_integral_constant_expression_p,
2950                                      &parser->non_integral_constant_expression_p,
2951                                      &error_msg);
2952         if (error_msg)
2953           cp_parser_error (parser, error_msg);
2954         return decl;
2955       }
2956
2957       /* Anything else is an error.  */
2958     default:
2959       cp_parser_error (parser, "expected primary-expression");
2960       return error_mark_node;
2961     }
2962 }
2963
2964 /* Parse an id-expression.
2965
2966    id-expression:
2967      unqualified-id
2968      qualified-id
2969
2970    qualified-id:
2971      :: [opt] nested-name-specifier template [opt] unqualified-id
2972      :: identifier
2973      :: operator-function-id
2974      :: template-id
2975
2976    Return a representation of the unqualified portion of the
2977    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2978    a `::' or nested-name-specifier.
2979
2980    Often, if the id-expression was a qualified-id, the caller will
2981    want to make a SCOPE_REF to represent the qualified-id.  This
2982    function does not do this in order to avoid wastefully creating
2983    SCOPE_REFs when they are not required.
2984
2985    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2986    `template' keyword.
2987
2988    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2989    uninstantiated templates.
2990
2991    If *TEMPLATE_P is non-NULL, it is set to true iff the
2992    `template' keyword is used to explicitly indicate that the entity
2993    named is a template.
2994
2995    If DECLARATOR_P is true, the id-expression is appearing as part of
2996    a declarator, rather than as part of an expression.  */
2997
2998 static tree
2999 cp_parser_id_expression (cp_parser *parser,
3000                          bool template_keyword_p,
3001                          bool check_dependency_p,
3002                          bool *template_p,
3003                          bool declarator_p)
3004 {
3005   bool global_scope_p;
3006   bool nested_name_specifier_p;
3007
3008   /* Assume the `template' keyword was not used.  */
3009   if (template_p)
3010     *template_p = false;
3011
3012   /* Look for the optional `::' operator.  */
3013   global_scope_p
3014     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3015        != NULL_TREE);
3016   /* Look for the optional nested-name-specifier.  */
3017   nested_name_specifier_p
3018     = (cp_parser_nested_name_specifier_opt (parser,
3019                                             /*typename_keyword_p=*/false,
3020                                             check_dependency_p,
3021                                             /*type_p=*/false,
3022                                             /*is_declarator=*/false)
3023        != NULL_TREE);
3024   /* If there is a nested-name-specifier, then we are looking at
3025      the first qualified-id production.  */
3026   if (nested_name_specifier_p)
3027     {
3028       tree saved_scope;
3029       tree saved_object_scope;
3030       tree saved_qualifying_scope;
3031       tree unqualified_id;
3032       bool is_template;
3033
3034       /* See if the next token is the `template' keyword.  */
3035       if (!template_p)
3036         template_p = &is_template;
3037       *template_p = cp_parser_optional_template_keyword (parser);
3038       /* Name lookup we do during the processing of the
3039          unqualified-id might obliterate SCOPE.  */
3040       saved_scope = parser->scope;
3041       saved_object_scope = parser->object_scope;
3042       saved_qualifying_scope = parser->qualifying_scope;
3043       /* Process the final unqualified-id.  */
3044       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3045                                                  check_dependency_p,
3046                                                  declarator_p);
3047       /* Restore the SAVED_SCOPE for our caller.  */
3048       parser->scope = saved_scope;
3049       parser->object_scope = saved_object_scope;
3050       parser->qualifying_scope = saved_qualifying_scope;
3051
3052       return unqualified_id;
3053     }
3054   /* Otherwise, if we are in global scope, then we are looking at one
3055      of the other qualified-id productions.  */
3056   else if (global_scope_p)
3057     {
3058       cp_token *token;
3059       tree id;
3060
3061       /* Peek at the next token.  */
3062       token = cp_lexer_peek_token (parser->lexer);
3063
3064       /* If it's an identifier, and the next token is not a "<", then
3065          we can avoid the template-id case.  This is an optimization
3066          for this common case.  */
3067       if (token->type == CPP_NAME
3068           && !cp_parser_nth_token_starts_template_argument_list_p
3069                (parser, 2))
3070         return cp_parser_identifier (parser);
3071
3072       cp_parser_parse_tentatively (parser);
3073       /* Try a template-id.  */
3074       id = cp_parser_template_id (parser,
3075                                   /*template_keyword_p=*/false,
3076                                   /*check_dependency_p=*/true,
3077                                   declarator_p);
3078       /* If that worked, we're done.  */
3079       if (cp_parser_parse_definitely (parser))
3080         return id;
3081
3082       /* Peek at the next token.  (Changes in the token buffer may
3083          have invalidated the pointer obtained above.)  */
3084       token = cp_lexer_peek_token (parser->lexer);
3085
3086       switch (token->type)
3087         {
3088         case CPP_NAME:
3089           return cp_parser_identifier (parser);
3090
3091         case CPP_KEYWORD:
3092           if (token->keyword == RID_OPERATOR)
3093             return cp_parser_operator_function_id (parser);
3094           /* Fall through.  */
3095
3096         default:
3097           cp_parser_error (parser, "expected id-expression");
3098           return error_mark_node;
3099         }
3100     }
3101   else
3102     return cp_parser_unqualified_id (parser, template_keyword_p,
3103                                      /*check_dependency_p=*/true,
3104                                      declarator_p);
3105 }
3106
3107 /* Parse an unqualified-id.
3108
3109    unqualified-id:
3110      identifier
3111      operator-function-id
3112      conversion-function-id
3113      ~ class-name
3114      template-id
3115
3116    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3117    keyword, in a construct like `A::template ...'.
3118
3119    Returns a representation of unqualified-id.  For the `identifier'
3120    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3121    production a BIT_NOT_EXPR is returned; the operand of the
3122    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3123    other productions, see the documentation accompanying the
3124    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3125    names are looked up in uninstantiated templates.  If DECLARATOR_P
3126    is true, the unqualified-id is appearing as part of a declarator,
3127    rather than as part of an expression.  */
3128
3129 static tree
3130 cp_parser_unqualified_id (cp_parser* parser,
3131                           bool template_keyword_p,
3132                           bool check_dependency_p,
3133                           bool declarator_p)
3134 {
3135   cp_token *token;
3136
3137   /* Peek at the next token.  */
3138   token = cp_lexer_peek_token (parser->lexer);
3139
3140   switch (token->type)
3141     {
3142     case CPP_NAME:
3143       {
3144         tree id;
3145
3146         /* We don't know yet whether or not this will be a
3147            template-id.  */
3148         cp_parser_parse_tentatively (parser);
3149         /* Try a template-id.  */
3150         id = cp_parser_template_id (parser, template_keyword_p,
3151                                     check_dependency_p,
3152                                     declarator_p);
3153         /* If it worked, we're done.  */
3154         if (cp_parser_parse_definitely (parser))
3155           return id;
3156         /* Otherwise, it's an ordinary identifier.  */
3157         return cp_parser_identifier (parser);
3158       }
3159
3160     case CPP_TEMPLATE_ID:
3161       return cp_parser_template_id (parser, template_keyword_p,
3162                                     check_dependency_p,
3163                                     declarator_p);
3164
3165     case CPP_COMPL:
3166       {
3167         tree type_decl;
3168         tree qualifying_scope;
3169         tree object_scope;
3170         tree scope;
3171
3172         /* Consume the `~' token.  */
3173         cp_lexer_consume_token (parser->lexer);
3174         /* Parse the class-name.  The standard, as written, seems to
3175            say that:
3176
3177              template <typename T> struct S { ~S (); };
3178              template <typename T> S<T>::~S() {}
3179
3180            is invalid, since `~' must be followed by a class-name, but
3181            `S<T>' is dependent, and so not known to be a class.
3182            That's not right; we need to look in uninstantiated
3183            templates.  A further complication arises from:
3184
3185              template <typename T> void f(T t) {
3186                t.T::~T();
3187              }
3188
3189            Here, it is not possible to look up `T' in the scope of `T'
3190            itself.  We must look in both the current scope, and the
3191            scope of the containing complete expression.
3192
3193            Yet another issue is:
3194
3195              struct S {
3196                int S;
3197                ~S();
3198              };
3199
3200              S::~S() {}
3201
3202            The standard does not seem to say that the `S' in `~S'
3203            should refer to the type `S' and not the data member
3204            `S::S'.  */
3205
3206         /* DR 244 says that we look up the name after the "~" in the
3207            same scope as we looked up the qualifying name.  That idea
3208            isn't fully worked out; it's more complicated than that.  */
3209         scope = parser->scope;
3210         object_scope = parser->object_scope;
3211         qualifying_scope = parser->qualifying_scope;
3212
3213         /* If the name is of the form "X::~X" it's OK.  */
3214         if (scope && TYPE_P (scope)
3215             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3216             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3217                 == CPP_OPEN_PAREN)
3218             && (cp_lexer_peek_token (parser->lexer)->value
3219                 == TYPE_IDENTIFIER (scope)))
3220           {
3221             cp_lexer_consume_token (parser->lexer);
3222             return build_nt (BIT_NOT_EXPR, scope);
3223           }
3224
3225         /* If there was an explicit qualification (S::~T), first look
3226            in the scope given by the qualification (i.e., S).  */
3227         if (scope)
3228           {
3229             cp_parser_parse_tentatively (parser);
3230             type_decl = cp_parser_class_name (parser,
3231                                               /*typename_keyword_p=*/false,
3232                                               /*template_keyword_p=*/false,
3233                                               /*type_p=*/false,
3234                                               /*check_dependency=*/false,
3235                                               /*class_head_p=*/false,
3236                                               declarator_p);
3237             if (cp_parser_parse_definitely (parser))
3238               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3239           }
3240         /* In "N::S::~S", look in "N" as well.  */
3241         if (scope && qualifying_scope)
3242           {
3243             cp_parser_parse_tentatively (parser);
3244             parser->scope = qualifying_scope;
3245             parser->object_scope = NULL_TREE;
3246             parser->qualifying_scope = NULL_TREE;
3247             type_decl
3248               = cp_parser_class_name (parser,
3249                                       /*typename_keyword_p=*/false,
3250                                       /*template_keyword_p=*/false,
3251                                       /*type_p=*/false,
3252                                       /*check_dependency=*/false,
3253                                       /*class_head_p=*/false,
3254                                       declarator_p);
3255             if (cp_parser_parse_definitely (parser))
3256               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3257           }
3258         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3259         else if (object_scope)
3260           {
3261             cp_parser_parse_tentatively (parser);
3262             parser->scope = object_scope;
3263             parser->object_scope = NULL_TREE;
3264             parser->qualifying_scope = NULL_TREE;
3265             type_decl
3266               = cp_parser_class_name (parser,
3267                                       /*typename_keyword_p=*/false,
3268                                       /*template_keyword_p=*/false,
3269                                       /*type_p=*/false,
3270                                       /*check_dependency=*/false,
3271                                       /*class_head_p=*/false,
3272                                       declarator_p);
3273             if (cp_parser_parse_definitely (parser))
3274               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3275           }
3276         /* Look in the surrounding context.  */
3277         parser->scope = NULL_TREE;
3278         parser->object_scope = NULL_TREE;
3279         parser->qualifying_scope = NULL_TREE;
3280         type_decl
3281           = cp_parser_class_name (parser,
3282                                   /*typename_keyword_p=*/false,
3283                                   /*template_keyword_p=*/false,
3284                                   /*type_p=*/false,
3285                                   /*check_dependency=*/false,
3286                                   /*class_head_p=*/false,
3287                                   declarator_p);
3288         /* If an error occurred, assume that the name of the
3289            destructor is the same as the name of the qualifying
3290            class.  That allows us to keep parsing after running
3291            into ill-formed destructor names.  */
3292         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3293           return build_nt (BIT_NOT_EXPR, scope);
3294         else if (type_decl == error_mark_node)
3295           return error_mark_node;
3296
3297         /* [class.dtor]
3298
3299            A typedef-name that names a class shall not be used as the
3300            identifier in the declarator for a destructor declaration.  */
3301         if (declarator_p
3302             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3303             && !DECL_SELF_REFERENCE_P (type_decl))
3304           error ("typedef-name `%D' used as destructor declarator",
3305                  type_decl);
3306
3307         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3308       }
3309
3310     case CPP_KEYWORD:
3311       if (token->keyword == RID_OPERATOR)
3312         {
3313           tree id;
3314
3315           /* This could be a template-id, so we try that first.  */
3316           cp_parser_parse_tentatively (parser);
3317           /* Try a template-id.  */
3318           id = cp_parser_template_id (parser, template_keyword_p,
3319                                       /*check_dependency_p=*/true,
3320                                       declarator_p);
3321           /* If that worked, we're done.  */
3322           if (cp_parser_parse_definitely (parser))
3323             return id;
3324           /* We still don't know whether we're looking at an
3325              operator-function-id or a conversion-function-id.  */
3326           cp_parser_parse_tentatively (parser);
3327           /* Try an operator-function-id.  */
3328           id = cp_parser_operator_function_id (parser);
3329           /* If that didn't work, try a conversion-function-id.  */
3330           if (!cp_parser_parse_definitely (parser))
3331             id = cp_parser_conversion_function_id (parser);
3332
3333           return id;
3334         }
3335       /* Fall through.  */
3336
3337     default:
3338       cp_parser_error (parser, "expected unqualified-id");
3339       return error_mark_node;
3340     }
3341 }
3342
3343 /* Parse an (optional) nested-name-specifier.
3344
3345    nested-name-specifier:
3346      class-or-namespace-name :: nested-name-specifier [opt]
3347      class-or-namespace-name :: template nested-name-specifier [opt]
3348
3349    PARSER->SCOPE should be set appropriately before this function is
3350    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3351    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3352    in name lookups.
3353
3354    Sets PARSER->SCOPE to the class (TYPE) or namespace
3355    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3356    it unchanged if there is no nested-name-specifier.  Returns the new
3357    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3358
3359    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3360    part of a declaration and/or decl-specifier.  */
3361
3362 static tree
3363 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3364                                      bool typename_keyword_p,
3365                                      bool check_dependency_p,
3366                                      bool type_p,
3367                                      bool is_declaration)
3368 {
3369   bool success = false;
3370   tree access_check = NULL_TREE;
3371   ptrdiff_t start;
3372   cp_token* token;
3373
3374   /* If the next token corresponds to a nested name specifier, there
3375      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3376      false, it may have been true before, in which case something
3377      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3378      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3379      CHECK_DEPENDENCY_P is false, we have to fall through into the
3380      main loop.  */
3381   if (check_dependency_p
3382       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3383     {
3384       cp_parser_pre_parsed_nested_name_specifier (parser);
3385       return parser->scope;
3386     }
3387
3388   /* Remember where the nested-name-specifier starts.  */
3389   if (cp_parser_parsing_tentatively (parser)
3390       && !cp_parser_committed_to_tentative_parse (parser))
3391     {
3392       token = cp_lexer_peek_token (parser->lexer);
3393       start = cp_lexer_token_difference (parser->lexer,
3394                                          parser->lexer->first_token,
3395                                          token);
3396     }
3397   else
3398     start = -1;
3399
3400   push_deferring_access_checks (dk_deferred);
3401
3402   while (true)
3403     {
3404       tree new_scope;
3405       tree old_scope;
3406       tree saved_qualifying_scope;
3407       bool template_keyword_p;
3408
3409       /* Spot cases that cannot be the beginning of a
3410          nested-name-specifier.  */
3411       token = cp_lexer_peek_token (parser->lexer);
3412
3413       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3414          the already parsed nested-name-specifier.  */
3415       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3416         {
3417           /* Grab the nested-name-specifier and continue the loop.  */
3418           cp_parser_pre_parsed_nested_name_specifier (parser);
3419           success = true;
3420           continue;
3421         }
3422
3423       /* Spot cases that cannot be the beginning of a
3424          nested-name-specifier.  On the second and subsequent times
3425          through the loop, we look for the `template' keyword.  */
3426       if (success && token->keyword == RID_TEMPLATE)
3427         ;
3428       /* A template-id can start a nested-name-specifier.  */
3429       else if (token->type == CPP_TEMPLATE_ID)
3430         ;
3431       else
3432         {
3433           /* If the next token is not an identifier, then it is
3434              definitely not a class-or-namespace-name.  */
3435           if (token->type != CPP_NAME)
3436             break;
3437           /* If the following token is neither a `<' (to begin a
3438              template-id), nor a `::', then we are not looking at a
3439              nested-name-specifier.  */
3440           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3441           if (token->type != CPP_SCOPE
3442               && !cp_parser_nth_token_starts_template_argument_list_p
3443                   (parser, 2))
3444             break;
3445         }
3446
3447       /* The nested-name-specifier is optional, so we parse
3448          tentatively.  */
3449       cp_parser_parse_tentatively (parser);
3450
3451       /* Look for the optional `template' keyword, if this isn't the
3452          first time through the loop.  */
3453       if (success)
3454         template_keyword_p = cp_parser_optional_template_keyword (parser);
3455       else
3456         template_keyword_p = false;
3457
3458       /* Save the old scope since the name lookup we are about to do
3459          might destroy it.  */
3460       old_scope = parser->scope;
3461       saved_qualifying_scope = parser->qualifying_scope;
3462       /* Parse the qualifying entity.  */
3463       new_scope
3464         = cp_parser_class_or_namespace_name (parser,
3465                                              typename_keyword_p,
3466                                              template_keyword_p,
3467                                              check_dependency_p,
3468                                              type_p,
3469                                              is_declaration);
3470       /* Look for the `::' token.  */
3471       cp_parser_require (parser, CPP_SCOPE, "`::'");
3472
3473       /* If we found what we wanted, we keep going; otherwise, we're
3474          done.  */
3475       if (!cp_parser_parse_definitely (parser))
3476         {
3477           bool error_p = false;
3478
3479           /* Restore the OLD_SCOPE since it was valid before the
3480              failed attempt at finding the last
3481              class-or-namespace-name.  */
3482           parser->scope = old_scope;
3483           parser->qualifying_scope = saved_qualifying_scope;
3484           /* If the next token is an identifier, and the one after
3485              that is a `::', then any valid interpretation would have
3486              found a class-or-namespace-name.  */
3487           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3488                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3489                      == CPP_SCOPE)
3490                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3491                      != CPP_COMPL))
3492             {
3493               token = cp_lexer_consume_token (parser->lexer);
3494               if (!error_p)
3495                 {
3496                   tree decl;
3497
3498                   decl = cp_parser_lookup_name_simple (parser, token->value);
3499                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3500                     error ("`%D' used without template parameters",
3501                            decl);
3502                   else
3503                     cp_parser_name_lookup_error
3504                       (parser, token->value, decl,
3505                        "is not a class or namespace");
3506                   parser->scope = NULL_TREE;
3507                   error_p = true;
3508                   /* Treat this as a successful nested-name-specifier
3509                      due to:
3510
3511                      [basic.lookup.qual]
3512
3513                      If the name found is not a class-name (clause
3514                      _class_) or namespace-name (_namespace.def_), the
3515                      program is ill-formed.  */
3516                   success = true;
3517                 }
3518               cp_lexer_consume_token (parser->lexer);
3519             }
3520           break;
3521         }
3522
3523       /* We've found one valid nested-name-specifier.  */
3524       success = true;
3525       /* Make sure we look in the right scope the next time through
3526          the loop.  */
3527       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3528                        ? TREE_TYPE (new_scope)
3529                        : new_scope);
3530       /* If it is a class scope, try to complete it; we are about to
3531          be looking up names inside the class.  */
3532       if (TYPE_P (parser->scope)
3533           /* Since checking types for dependency can be expensive,
3534              avoid doing it if the type is already complete.  */
3535           && !COMPLETE_TYPE_P (parser->scope)
3536           /* Do not try to complete dependent types.  */
3537           && !dependent_type_p (parser->scope))
3538         complete_type (parser->scope);
3539     }
3540
3541   /* Retrieve any deferred checks.  Do not pop this access checks yet
3542      so the memory will not be reclaimed during token replacing below.  */
3543   access_check = get_deferred_access_checks ();
3544
3545   /* If parsing tentatively, replace the sequence of tokens that makes
3546      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3547      token.  That way, should we re-parse the token stream, we will
3548      not have to repeat the effort required to do the parse, nor will
3549      we issue duplicate error messages.  */
3550   if (success && start >= 0)
3551     {
3552       /* Find the token that corresponds to the start of the
3553          template-id.  */
3554       token = cp_lexer_advance_token (parser->lexer,
3555                                       parser->lexer->first_token,
3556                                       start);
3557
3558       /* Reset the contents of the START token.  */
3559       token->type = CPP_NESTED_NAME_SPECIFIER;
3560       token->value = build_tree_list (access_check, parser->scope);
3561       TREE_TYPE (token->value) = parser->qualifying_scope;
3562       token->keyword = RID_MAX;
3563       /* Purge all subsequent tokens.  */
3564       cp_lexer_purge_tokens_after (parser->lexer, token);
3565     }
3566
3567   pop_deferring_access_checks ();
3568   return success ? parser->scope : NULL_TREE;
3569 }
3570
3571 /* Parse a nested-name-specifier.  See
3572    cp_parser_nested_name_specifier_opt for details.  This function
3573    behaves identically, except that it will an issue an error if no
3574    nested-name-specifier is present, and it will return
3575    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3576    is present.  */
3577
3578 static tree
3579 cp_parser_nested_name_specifier (cp_parser *parser,
3580                                  bool typename_keyword_p,
3581                                  bool check_dependency_p,
3582                                  bool type_p,
3583                                  bool is_declaration)
3584 {
3585   tree scope;
3586
3587   /* Look for the nested-name-specifier.  */
3588   scope = cp_parser_nested_name_specifier_opt (parser,
3589                                                typename_keyword_p,
3590                                                check_dependency_p,
3591                                                type_p,
3592                                                is_declaration);
3593   /* If it was not present, issue an error message.  */
3594   if (!scope)
3595     {
3596       cp_parser_error (parser, "expected nested-name-specifier");
3597       parser->scope = NULL_TREE;
3598       return error_mark_node;
3599     }
3600
3601   return scope;
3602 }
3603
3604 /* Parse a class-or-namespace-name.
3605
3606    class-or-namespace-name:
3607      class-name
3608      namespace-name
3609
3610    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3611    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3612    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3613    TYPE_P is TRUE iff the next name should be taken as a class-name,
3614    even the same name is declared to be another entity in the same
3615    scope.
3616
3617    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3618    specified by the class-or-namespace-name.  If neither is found the
3619    ERROR_MARK_NODE is returned.  */
3620
3621 static tree
3622 cp_parser_class_or_namespace_name (cp_parser *parser,
3623                                    bool typename_keyword_p,
3624                                    bool template_keyword_p,
3625                                    bool check_dependency_p,
3626                                    bool type_p,
3627                                    bool is_declaration)
3628 {
3629   tree saved_scope;
3630   tree saved_qualifying_scope;
3631   tree saved_object_scope;
3632   tree scope;
3633   bool only_class_p;
3634
3635   /* Before we try to parse the class-name, we must save away the
3636      current PARSER->SCOPE since cp_parser_class_name will destroy
3637      it.  */
3638   saved_scope = parser->scope;
3639   saved_qualifying_scope = parser->qualifying_scope;
3640   saved_object_scope = parser->object_scope;
3641   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3642      there is no need to look for a namespace-name.  */
3643   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3644   if (!only_class_p)
3645     cp_parser_parse_tentatively (parser);
3646   scope = cp_parser_class_name (parser,
3647                                 typename_keyword_p,
3648                                 template_keyword_p,
3649                                 type_p,
3650                                 check_dependency_p,
3651                                 /*class_head_p=*/false,
3652                                 is_declaration);
3653   /* If that didn't work, try for a namespace-name.  */
3654   if (!only_class_p && !cp_parser_parse_definitely (parser))
3655     {
3656       /* Restore the saved scope.  */
3657       parser->scope = saved_scope;
3658       parser->qualifying_scope = saved_qualifying_scope;
3659       parser->object_scope = saved_object_scope;
3660       /* If we are not looking at an identifier followed by the scope
3661          resolution operator, then this is not part of a
3662          nested-name-specifier.  (Note that this function is only used
3663          to parse the components of a nested-name-specifier.)  */
3664       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3665           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3666         return error_mark_node;
3667       scope = cp_parser_namespace_name (parser);
3668     }
3669
3670   return scope;
3671 }
3672
3673 /* Parse a postfix-expression.
3674
3675    postfix-expression:
3676      primary-expression
3677      postfix-expression [ expression ]
3678      postfix-expression ( expression-list [opt] )
3679      simple-type-specifier ( expression-list [opt] )
3680      typename :: [opt] nested-name-specifier identifier
3681        ( expression-list [opt] )
3682      typename :: [opt] nested-name-specifier template [opt] template-id
3683        ( expression-list [opt] )
3684      postfix-expression . template [opt] id-expression
3685      postfix-expression -> template [opt] id-expression
3686      postfix-expression . pseudo-destructor-name
3687      postfix-expression -> pseudo-destructor-name
3688      postfix-expression ++
3689      postfix-expression --
3690      dynamic_cast < type-id > ( expression )
3691      static_cast < type-id > ( expression )
3692      reinterpret_cast < type-id > ( expression )
3693      const_cast < type-id > ( expression )
3694      typeid ( expression )
3695      typeid ( type-id )
3696
3697    GNU Extension:
3698
3699    postfix-expression:
3700      ( type-id ) { initializer-list , [opt] }
3701
3702    This extension is a GNU version of the C99 compound-literal
3703    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3704    but they are essentially the same concept.)
3705
3706    If ADDRESS_P is true, the postfix expression is the operand of the
3707    `&' operator.
3708
3709    Returns a representation of the expression.  */
3710
3711 static tree
3712 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3713 {
3714   cp_token *token;
3715   enum rid keyword;
3716   cp_id_kind idk = CP_ID_KIND_NONE;
3717   tree postfix_expression = NULL_TREE;
3718   /* Non-NULL only if the current postfix-expression can be used to
3719      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3720      class used to qualify the member.  */
3721   tree qualifying_class = NULL_TREE;
3722
3723   /* Peek at the next token.  */
3724   token = cp_lexer_peek_token (parser->lexer);
3725   /* Some of the productions are determined by keywords.  */
3726   keyword = token->keyword;
3727   switch (keyword)
3728     {
3729     case RID_DYNCAST:
3730     case RID_STATCAST:
3731     case RID_REINTCAST:
3732     case RID_CONSTCAST:
3733       {
3734         tree type;
3735         tree expression;
3736         const char *saved_message;
3737
3738         /* All of these can be handled in the same way from the point
3739            of view of parsing.  Begin by consuming the token
3740            identifying the cast.  */
3741         cp_lexer_consume_token (parser->lexer);
3742
3743         /* New types cannot be defined in the cast.  */
3744         saved_message = parser->type_definition_forbidden_message;
3745         parser->type_definition_forbidden_message
3746           = "types may not be defined in casts";
3747
3748         /* Look for the opening `<'.  */
3749         cp_parser_require (parser, CPP_LESS, "`<'");
3750         /* Parse the type to which we are casting.  */
3751         type = cp_parser_type_id (parser);
3752         /* Look for the closing `>'.  */
3753         cp_parser_require (parser, CPP_GREATER, "`>'");
3754         /* Restore the old message.  */
3755         parser->type_definition_forbidden_message = saved_message;
3756
3757         /* And the expression which is being cast.  */
3758         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3759         expression = cp_parser_expression (parser);
3760         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3761
3762         /* Only type conversions to integral or enumeration types
3763            can be used in constant-expressions.  */
3764         if (parser->integral_constant_expression_p
3765             && !dependent_type_p (type)
3766             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3767             && (cp_parser_non_integral_constant_expression
3768                 (parser,
3769                  "a cast to a type other than an integral or "
3770                  "enumeration type")))
3771           return error_mark_node;
3772
3773         switch (keyword)
3774           {
3775           case RID_DYNCAST:
3776             postfix_expression
3777               = build_dynamic_cast (type, expression);
3778             break;
3779           case RID_STATCAST:
3780             postfix_expression
3781               = build_static_cast (type, expression);
3782             break;
3783           case RID_REINTCAST:
3784             postfix_expression
3785               = build_reinterpret_cast (type, expression);
3786             break;
3787           case RID_CONSTCAST:
3788             postfix_expression
3789               = build_const_cast (type, expression);
3790             break;
3791           default:
3792             abort ();
3793           }
3794       }
3795       break;
3796
3797     case RID_TYPEID:
3798       {
3799         tree type;
3800         const char *saved_message;
3801         bool saved_in_type_id_in_expr_p;
3802
3803         /* Consume the `typeid' token.  */
3804         cp_lexer_consume_token (parser->lexer);
3805         /* Look for the `(' token.  */
3806         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3807         /* Types cannot be defined in a `typeid' expression.  */
3808         saved_message = parser->type_definition_forbidden_message;
3809         parser->type_definition_forbidden_message
3810           = "types may not be defined in a `typeid\' expression";
3811         /* We can't be sure yet whether we're looking at a type-id or an
3812            expression.  */
3813         cp_parser_parse_tentatively (parser);
3814         /* Try a type-id first.  */
3815         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3816         parser->in_type_id_in_expr_p = true;
3817         type = cp_parser_type_id (parser);
3818         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3819         /* Look for the `)' token.  Otherwise, we can't be sure that
3820            we're not looking at an expression: consider `typeid (int
3821            (3))', for example.  */
3822         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3823         /* If all went well, simply lookup the type-id.  */
3824         if (cp_parser_parse_definitely (parser))
3825           postfix_expression = get_typeid (type);
3826         /* Otherwise, fall back to the expression variant.  */
3827         else
3828           {
3829             tree expression;
3830
3831             /* Look for an expression.  */
3832             expression = cp_parser_expression (parser);
3833             /* Compute its typeid.  */
3834             postfix_expression = build_typeid (expression);
3835             /* Look for the `)' token.  */
3836             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3837           }
3838         /* `typeid' may not appear in an integral constant expression.  */
3839         if (cp_parser_non_integral_constant_expression(parser,
3840                                                        "`typeid' operator"))
3841           return error_mark_node;
3842         /* Restore the saved message.  */
3843         parser->type_definition_forbidden_message = saved_message;
3844       }
3845       break;
3846
3847     case RID_TYPENAME:
3848       {
3849         bool template_p = false;
3850         tree id;
3851         tree type;
3852
3853         /* Consume the `typename' token.  */
3854         cp_lexer_consume_token (parser->lexer);
3855         /* Look for the optional `::' operator.  */
3856         cp_parser_global_scope_opt (parser,
3857                                     /*current_scope_valid_p=*/false);
3858         /* Look for the nested-name-specifier.  */
3859         cp_parser_nested_name_specifier (parser,
3860                                          /*typename_keyword_p=*/true,
3861                                          /*check_dependency_p=*/true,
3862                                          /*type_p=*/true,
3863                                          /*is_declaration=*/true);
3864         /* Look for the optional `template' keyword.  */
3865         template_p = cp_parser_optional_template_keyword (parser);
3866         /* We don't know whether we're looking at a template-id or an
3867            identifier.  */
3868         cp_parser_parse_tentatively (parser);
3869         /* Try a template-id.  */
3870         id = cp_parser_template_id (parser, template_p,
3871                                     /*check_dependency_p=*/true,
3872                                     /*is_declaration=*/true);
3873         /* If that didn't work, try an identifier.  */
3874         if (!cp_parser_parse_definitely (parser))
3875           id = cp_parser_identifier (parser);
3876         /* If we look up a template-id in a non-dependent qualifying
3877            scope, there's no need to create a dependent type.  */
3878         if (TREE_CODE (id) == TYPE_DECL
3879             && !dependent_type_p (parser->scope))
3880           type = TREE_TYPE (id);
3881         /* Create a TYPENAME_TYPE to represent the type to which the
3882            functional cast is being performed.  */
3883         else
3884           type = make_typename_type (parser->scope, id,
3885                                      /*complain=*/1);
3886
3887         postfix_expression = cp_parser_functional_cast (parser, type);
3888       }
3889       break;
3890
3891     default:
3892       {
3893         tree type;
3894
3895         /* If the next thing is a simple-type-specifier, we may be
3896            looking at a functional cast.  We could also be looking at
3897            an id-expression.  So, we try the functional cast, and if
3898            that doesn't work we fall back to the primary-expression.  */
3899         cp_parser_parse_tentatively (parser);
3900         /* Look for the simple-type-specifier.  */
3901         type = cp_parser_simple_type_specifier (parser,
3902                                                 /*decl_specs=*/NULL,
3903                                                 CP_PARSER_FLAGS_NONE);
3904         /* Parse the cast itself.  */
3905         if (!cp_parser_error_occurred (parser))
3906           postfix_expression
3907             = cp_parser_functional_cast (parser, type);
3908         /* If that worked, we're done.  */
3909         if (cp_parser_parse_definitely (parser))
3910           break;
3911
3912         /* If the functional-cast didn't work out, try a
3913            compound-literal.  */
3914         if (cp_parser_allow_gnu_extensions_p (parser)
3915             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3916           {
3917             tree initializer_list = NULL_TREE;
3918             bool saved_in_type_id_in_expr_p;
3919
3920             cp_parser_parse_tentatively (parser);
3921             /* Consume the `('.  */
3922             cp_lexer_consume_token (parser->lexer);
3923             /* Parse the type.  */
3924             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3925             parser->in_type_id_in_expr_p = true;
3926             type = cp_parser_type_id (parser);
3927             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3928             /* Look for the `)'.  */
3929             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3930             /* Look for the `{'.  */
3931             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3932             /* If things aren't going well, there's no need to
3933                keep going.  */
3934             if (!cp_parser_error_occurred (parser))
3935               {
3936                 bool non_constant_p;
3937                 /* Parse the initializer-list.  */
3938                 initializer_list
3939                   = cp_parser_initializer_list (parser, &non_constant_p);
3940                 /* Allow a trailing `,'.  */
3941                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3942                   cp_lexer_consume_token (parser->lexer);
3943                 /* Look for the final `}'.  */
3944                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3945               }
3946             /* If that worked, we're definitely looking at a
3947                compound-literal expression.  */
3948             if (cp_parser_parse_definitely (parser))
3949               {
3950                 /* Warn the user that a compound literal is not
3951                    allowed in standard C++.  */
3952                 if (pedantic)
3953                   pedwarn ("ISO C++ forbids compound-literals");
3954                 /* Form the representation of the compound-literal.  */
3955                 postfix_expression
3956                   = finish_compound_literal (type, initializer_list);
3957                 break;
3958               }
3959           }
3960
3961         /* It must be a primary-expression.  */
3962         postfix_expression = cp_parser_primary_expression (parser,
3963                                                            &idk,
3964                                                            &qualifying_class);
3965       }
3966       break;
3967     }
3968
3969   /* If we were avoiding committing to the processing of a
3970      qualified-id until we knew whether or not we had a
3971      pointer-to-member, we now know.  */
3972   if (qualifying_class)
3973     {
3974       bool done;
3975
3976       /* Peek at the next token.  */
3977       token = cp_lexer_peek_token (parser->lexer);
3978       done = (token->type != CPP_OPEN_SQUARE
3979               && token->type != CPP_OPEN_PAREN
3980               && token->type != CPP_DOT
3981               && token->type != CPP_DEREF
3982               && token->type != CPP_PLUS_PLUS
3983               && token->type != CPP_MINUS_MINUS);
3984
3985       postfix_expression = finish_qualified_id_expr (qualifying_class,
3986                                                      postfix_expression,
3987                                                      done,
3988                                                      address_p);
3989       if (done)
3990         return postfix_expression;
3991     }
3992
3993   /* Keep looping until the postfix-expression is complete.  */
3994   while (true)
3995     {
3996       if (idk == CP_ID_KIND_UNQUALIFIED
3997           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3998           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3999         /* It is not a Koenig lookup function call.  */
4000         postfix_expression
4001           = unqualified_name_lookup_error (postfix_expression);
4002
4003       /* Peek at the next token.  */
4004       token = cp_lexer_peek_token (parser->lexer);
4005
4006       switch (token->type)
4007         {
4008         case CPP_OPEN_SQUARE:
4009           postfix_expression
4010             = cp_parser_postfix_open_square_expression (parser,
4011                                                         postfix_expression,
4012                                                         false);
4013           idk = CP_ID_KIND_NONE;
4014           break;
4015
4016         case CPP_OPEN_PAREN:
4017           /* postfix-expression ( expression-list [opt] ) */
4018           {
4019             bool koenig_p;
4020             tree args = (cp_parser_parenthesized_expression_list
4021                          (parser, false, /*non_constant_p=*/NULL));
4022
4023             if (args == error_mark_node)
4024               {
4025                 postfix_expression = error_mark_node;
4026                 break;
4027               }
4028
4029             /* Function calls are not permitted in
4030                constant-expressions.  */
4031             if (cp_parser_non_integral_constant_expression (parser,
4032                                                             "a function call"))
4033               {
4034                 postfix_expression = error_mark_node;
4035                 break;
4036               }
4037
4038             koenig_p = false;
4039             if (idk == CP_ID_KIND_UNQUALIFIED)
4040               {
4041                 /* We do not perform argument-dependent lookup if
4042                    normal lookup finds a non-function, in accordance
4043                    with the expected resolution of DR 218.  */
4044                 if (args
4045                     && (is_overloaded_fn (postfix_expression)
4046                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4047                   {
4048                     koenig_p = true;
4049                     postfix_expression
4050                       = perform_koenig_lookup (postfix_expression, args);
4051                   }
4052                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4053                   postfix_expression
4054                     = unqualified_fn_lookup_error (postfix_expression);
4055               }
4056
4057             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4058               {
4059                 tree instance = TREE_OPERAND (postfix_expression, 0);
4060                 tree fn = TREE_OPERAND (postfix_expression, 1);
4061
4062                 if (processing_template_decl
4063                     && (type_dependent_expression_p (instance)
4064                         || (!BASELINK_P (fn)
4065                             && TREE_CODE (fn) != FIELD_DECL)
4066                         || type_dependent_expression_p (fn)
4067                         || any_type_dependent_arguments_p (args)))
4068                   {
4069                     postfix_expression
4070                       = build_min_nt (CALL_EXPR, postfix_expression,
4071                                       args, NULL_TREE);
4072                     break;
4073                   }
4074
4075                 if (BASELINK_P (fn))
4076                   postfix_expression
4077                     = (build_new_method_call
4078                        (instance, fn, args, NULL_TREE,
4079                         (idk == CP_ID_KIND_QUALIFIED
4080                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4081                 else
4082                   postfix_expression
4083                     = finish_call_expr (postfix_expression, args,
4084                                         /*disallow_virtual=*/false,
4085                                         /*koenig_p=*/false);
4086               }
4087             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4088                      || TREE_CODE (postfix_expression) == MEMBER_REF
4089                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4090               postfix_expression = (build_offset_ref_call_from_tree
4091                                     (postfix_expression, args));
4092             else if (idk == CP_ID_KIND_QUALIFIED)
4093               /* A call to a static class member, or a namespace-scope
4094                  function.  */
4095               postfix_expression
4096                 = finish_call_expr (postfix_expression, args,
4097                                     /*disallow_virtual=*/true,
4098                                     koenig_p);
4099             else
4100               /* All other function calls.  */
4101               postfix_expression
4102                 = finish_call_expr (postfix_expression, args,
4103                                     /*disallow_virtual=*/false,
4104                                     koenig_p);
4105
4106             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4107             idk = CP_ID_KIND_NONE;
4108           }
4109           break;
4110
4111         case CPP_DOT:
4112         case CPP_DEREF:
4113           /* postfix-expression . template [opt] id-expression
4114              postfix-expression . pseudo-destructor-name
4115              postfix-expression -> template [opt] id-expression
4116              postfix-expression -> pseudo-destructor-name */
4117
4118           /* Consume the `.' or `->' operator.  */
4119           cp_lexer_consume_token (parser->lexer);
4120
4121           postfix_expression
4122             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4123                                                       postfix_expression,
4124                                                       false, &idk);
4125           break;
4126
4127         case CPP_PLUS_PLUS:
4128           /* postfix-expression ++  */
4129           /* Consume the `++' token.  */
4130           cp_lexer_consume_token (parser->lexer);
4131           /* Generate a representation for the complete expression.  */
4132           postfix_expression
4133             = finish_increment_expr (postfix_expression,
4134                                      POSTINCREMENT_EXPR);
4135           /* Increments may not appear in constant-expressions.  */
4136           if (cp_parser_non_integral_constant_expression (parser,
4137                                                           "an increment"))
4138             postfix_expression = error_mark_node;
4139           idk = CP_ID_KIND_NONE;
4140           break;
4141
4142         case CPP_MINUS_MINUS:
4143           /* postfix-expression -- */
4144           /* Consume the `--' token.  */
4145           cp_lexer_consume_token (parser->lexer);
4146           /* Generate a representation for the complete expression.  */
4147           postfix_expression
4148             = finish_increment_expr (postfix_expression,
4149                                      POSTDECREMENT_EXPR);
4150           /* Decrements may not appear in constant-expressions.  */
4151           if (cp_parser_non_integral_constant_expression (parser,
4152                                                           "a decrement"))
4153             postfix_expression = error_mark_node;
4154           idk = CP_ID_KIND_NONE;
4155           break;
4156
4157         default:
4158           return postfix_expression;
4159         }
4160     }
4161
4162   /* We should never get here.  */
4163   abort ();
4164   return error_mark_node;
4165 }
4166
4167 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4168    by cp_parser_builtin_offsetof.  We're looking for
4169
4170      postfix-expression [ expression ]
4171
4172    FOR_OFFSETOF is set if we're being called in that context, which
4173    changes how we deal with integer constant expressions.  */
4174
4175 static tree
4176 cp_parser_postfix_open_square_expression (cp_parser *parser,
4177                                           tree postfix_expression,
4178                                           bool for_offsetof)
4179 {
4180   tree index;
4181
4182   /* Consume the `[' token.  */
4183   cp_lexer_consume_token (parser->lexer);
4184
4185   /* Parse the index expression.  */
4186   /* ??? For offsetof, there is a question of what to allow here.  If
4187      offsetof is not being used in an integral constant expression context,
4188      then we *could* get the right answer by computing the value at runtime.
4189      If we are in an integral constant expression context, then we might
4190      could accept any constant expression; hard to say without analysis.
4191      Rather than open the barn door too wide right away, allow only integer
4192      constant expresions here.  */
4193   if (for_offsetof)
4194     index = cp_parser_constant_expression (parser, false, NULL);
4195   else
4196     index = cp_parser_expression (parser);
4197
4198   /* Look for the closing `]'.  */
4199   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4200
4201   /* Build the ARRAY_REF.  */
4202   postfix_expression = grok_array_decl (postfix_expression, index);
4203
4204   /* When not doing offsetof, array references are not permitted in
4205      constant-expressions.  */
4206   if (!for_offsetof
4207       && (cp_parser_non_integral_constant_expression
4208           (parser, "an array reference")))
4209     postfix_expression = error_mark_node;
4210
4211   return postfix_expression;
4212 }
4213
4214 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4215    by cp_parser_builtin_offsetof.  We're looking for
4216
4217      postfix-expression . template [opt] id-expression
4218      postfix-expression . pseudo-destructor-name
4219      postfix-expression -> template [opt] id-expression
4220      postfix-expression -> pseudo-destructor-name
4221
4222    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4223    limits what of the above we'll actually accept, but nevermind.
4224    TOKEN_TYPE is the "." or "->" token, which will already have been
4225    removed from the stream.  */
4226
4227 static tree
4228 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4229                                         enum cpp_ttype token_type,
4230                                         tree postfix_expression,
4231                                         bool for_offsetof, cp_id_kind *idk)
4232 {
4233   tree name;
4234   bool dependent_p;
4235   bool template_p;
4236   tree scope = NULL_TREE;
4237
4238   /* If this is a `->' operator, dereference the pointer.  */
4239   if (token_type == CPP_DEREF)
4240     postfix_expression = build_x_arrow (postfix_expression);
4241   /* Check to see whether or not the expression is type-dependent.  */
4242   dependent_p = type_dependent_expression_p (postfix_expression);
4243   /* The identifier following the `->' or `.' is not qualified.  */
4244   parser->scope = NULL_TREE;
4245   parser->qualifying_scope = NULL_TREE;
4246   parser->object_scope = NULL_TREE;
4247   *idk = CP_ID_KIND_NONE;
4248   /* Enter the scope corresponding to the type of the object
4249      given by the POSTFIX_EXPRESSION.  */
4250   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4251     {
4252       scope = TREE_TYPE (postfix_expression);
4253       /* According to the standard, no expression should ever have
4254          reference type.  Unfortunately, we do not currently match
4255          the standard in this respect in that our internal representation
4256          of an expression may have reference type even when the standard
4257          says it does not.  Therefore, we have to manually obtain the
4258          underlying type here.  */
4259       scope = non_reference (scope);
4260       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4261       scope = complete_type_or_else (scope, NULL_TREE);
4262       /* Let the name lookup machinery know that we are processing a
4263          class member access expression.  */
4264       parser->context->object_type = scope;
4265       /* If something went wrong, we want to be able to discern that case,
4266          as opposed to the case where there was no SCOPE due to the type
4267          of expression being dependent.  */
4268       if (!scope)
4269         scope = error_mark_node;
4270       /* If the SCOPE was erroneous, make the various semantic analysis
4271          functions exit quickly -- and without issuing additional error
4272          messages.  */
4273       if (scope == error_mark_node)
4274         postfix_expression = error_mark_node;
4275     }
4276
4277   /* If the SCOPE is not a scalar type, we are looking at an
4278      ordinary class member access expression, rather than a
4279      pseudo-destructor-name.  */
4280   if (!scope || !SCALAR_TYPE_P (scope))
4281     {
4282       template_p = cp_parser_optional_template_keyword (parser);
4283       /* Parse the id-expression.  */
4284       name = cp_parser_id_expression (parser, template_p,
4285                                       /*check_dependency_p=*/true,
4286                                       /*template_p=*/NULL,
4287                                       /*declarator_p=*/false);
4288       /* In general, build a SCOPE_REF if the member name is qualified.
4289          However, if the name was not dependent and has already been
4290          resolved; there is no need to build the SCOPE_REF.  For example;
4291
4292              struct X { void f(); };
4293              template <typename T> void f(T* t) { t->X::f(); }
4294
4295          Even though "t" is dependent, "X::f" is not and has been resolved
4296          to a BASELINK; there is no need to include scope information.  */
4297
4298       /* But we do need to remember that there was an explicit scope for
4299          virtual function calls.  */
4300       if (parser->scope)
4301         *idk = CP_ID_KIND_QUALIFIED;
4302
4303       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4304         {
4305           name = build_nt (SCOPE_REF, parser->scope, name);
4306           parser->scope = NULL_TREE;
4307           parser->qualifying_scope = NULL_TREE;
4308           parser->object_scope = NULL_TREE;
4309         }
4310       if (scope && name && BASELINK_P (name))
4311         adjust_result_of_qualified_name_lookup
4312           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4313       postfix_expression
4314         = finish_class_member_access_expr (postfix_expression, name);
4315     }
4316   /* Otherwise, try the pseudo-destructor-name production.  */
4317   else
4318     {
4319       tree s = NULL_TREE;
4320       tree type;
4321
4322       /* Parse the pseudo-destructor-name.  */
4323       cp_parser_pseudo_destructor_name (parser, &s, &type);
4324       /* Form the call.  */
4325       postfix_expression
4326         = finish_pseudo_destructor_expr (postfix_expression,
4327                                          s, TREE_TYPE (type));
4328     }
4329
4330   /* We no longer need to look up names in the scope of the object on
4331      the left-hand side of the `.' or `->' operator.  */
4332   parser->context->object_type = NULL_TREE;
4333
4334   /* Outside of offsetof, these operators may not appear in
4335      constant-expressions.  */
4336   if (!for_offsetof
4337       && (cp_parser_non_integral_constant_expression
4338           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4339     postfix_expression = error_mark_node;
4340
4341   return postfix_expression;
4342 }
4343
4344 /* Parse a parenthesized expression-list.
4345
4346    expression-list:
4347      assignment-expression
4348      expression-list, assignment-expression
4349
4350    attribute-list:
4351      expression-list
4352      identifier
4353      identifier, expression-list
4354
4355    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4356    representation of an assignment-expression.  Note that a TREE_LIST
4357    is returned even if there is only a single expression in the list.
4358    error_mark_node is returned if the ( and or ) are
4359    missing. NULL_TREE is returned on no expressions. The parentheses
4360    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4361    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4362    indicates whether or not all of the expressions in the list were
4363    constant.  */
4364
4365 static tree
4366 cp_parser_parenthesized_expression_list (cp_parser* parser,
4367                                          bool is_attribute_list,
4368                                          bool *non_constant_p)
4369 {
4370   tree expression_list = NULL_TREE;
4371   tree identifier = NULL_TREE;
4372
4373   /* Assume all the expressions will be constant.  */
4374   if (non_constant_p)
4375     *non_constant_p = false;
4376
4377   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4378     return error_mark_node;
4379
4380   /* Consume expressions until there are no more.  */
4381   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4382     while (true)
4383       {
4384         tree expr;
4385
4386         /* At the beginning of attribute lists, check to see if the
4387            next token is an identifier.  */
4388         if (is_attribute_list
4389             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4390           {
4391             cp_token *token;
4392
4393             /* Consume the identifier.  */
4394             token = cp_lexer_consume_token (parser->lexer);
4395             /* Save the identifier.  */
4396             identifier = token->value;
4397           }
4398         else
4399           {
4400             /* Parse the next assignment-expression.  */
4401             if (non_constant_p)
4402               {
4403                 bool expr_non_constant_p;
4404                 expr = (cp_parser_constant_expression
4405                         (parser, /*allow_non_constant_p=*/true,
4406                          &expr_non_constant_p));
4407                 if (expr_non_constant_p)
4408                   *non_constant_p = true;
4409               }
4410             else
4411               expr = cp_parser_assignment_expression (parser);
4412
4413              /* Add it to the list.  We add error_mark_node
4414                 expressions to the list, so that we can still tell if
4415                 the correct form for a parenthesized expression-list
4416                 is found. That gives better errors.  */
4417             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4418
4419             if (expr == error_mark_node)
4420               goto skip_comma;
4421           }
4422
4423         /* After the first item, attribute lists look the same as
4424            expression lists.  */
4425         is_attribute_list = false;
4426
4427       get_comma:;
4428         /* If the next token isn't a `,', then we are done.  */
4429         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4430           break;
4431
4432         /* Otherwise, consume the `,' and keep going.  */
4433         cp_lexer_consume_token (parser->lexer);
4434       }
4435
4436   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4437     {
4438       int ending;
4439
4440     skip_comma:;
4441       /* We try and resync to an unnested comma, as that will give the
4442          user better diagnostics.  */
4443       ending = cp_parser_skip_to_closing_parenthesis (parser,
4444                                                       /*recovering=*/true,
4445                                                       /*or_comma=*/true,
4446                                                       /*consume_paren=*/true);
4447       if (ending < 0)
4448         goto get_comma;
4449       if (!ending)
4450         return error_mark_node;
4451     }
4452
4453   /* We built up the list in reverse order so we must reverse it now.  */
4454   expression_list = nreverse (expression_list);
4455   if (identifier)
4456     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4457
4458   return expression_list;
4459 }
4460
4461 /* Parse a pseudo-destructor-name.
4462
4463    pseudo-destructor-name:
4464      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4465      :: [opt] nested-name-specifier template template-id :: ~ type-name
4466      :: [opt] nested-name-specifier [opt] ~ type-name
4467
4468    If either of the first two productions is used, sets *SCOPE to the
4469    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4470    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4471    or ERROR_MARK_NODE if the parse fails.  */
4472
4473 static void
4474 cp_parser_pseudo_destructor_name (cp_parser* parser,
4475                                   tree* scope,
4476                                   tree* type)
4477 {
4478   bool nested_name_specifier_p;
4479
4480   /* Look for the optional `::' operator.  */
4481   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4482   /* Look for the optional nested-name-specifier.  */
4483   nested_name_specifier_p
4484     = (cp_parser_nested_name_specifier_opt (parser,
4485                                             /*typename_keyword_p=*/false,
4486                                             /*check_dependency_p=*/true,
4487                                             /*type_p=*/false,
4488                                             /*is_declaration=*/true)
4489        != NULL_TREE);
4490   /* Now, if we saw a nested-name-specifier, we might be doing the
4491      second production.  */
4492   if (nested_name_specifier_p
4493       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4494     {
4495       /* Consume the `template' keyword.  */
4496       cp_lexer_consume_token (parser->lexer);
4497       /* Parse the template-id.  */
4498       cp_parser_template_id (parser,
4499                              /*template_keyword_p=*/true,
4500                              /*check_dependency_p=*/false,
4501                              /*is_declaration=*/true);
4502       /* Look for the `::' token.  */
4503       cp_parser_require (parser, CPP_SCOPE, "`::'");
4504     }
4505   /* If the next token is not a `~', then there might be some
4506      additional qualification.  */
4507   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4508     {
4509       /* Look for the type-name.  */
4510       *scope = TREE_TYPE (cp_parser_type_name (parser));
4511
4512       /* If we didn't get an aggregate type, or we don't have ::~,
4513          then something has gone wrong.  Since the only caller of this
4514          function is looking for something after `.' or `->' after a
4515          scalar type, most likely the program is trying to get a
4516          member of a non-aggregate type.  */
4517       if (*scope == error_mark_node
4518           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4519           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4520         {
4521           cp_parser_error (parser, "request for member of non-aggregate type");
4522           *type = error_mark_node;
4523           return;
4524         }
4525
4526       /* Look for the `::' token.  */
4527       cp_parser_require (parser, CPP_SCOPE, "`::'");
4528     }
4529   else
4530     *scope = NULL_TREE;
4531
4532   /* Look for the `~'.  */
4533   cp_parser_require (parser, CPP_COMPL, "`~'");
4534   /* Look for the type-name again.  We are not responsible for
4535      checking that it matches the first type-name.  */
4536   *type = cp_parser_type_name (parser);
4537 }
4538
4539 /* Parse a unary-expression.
4540
4541    unary-expression:
4542      postfix-expression
4543      ++ cast-expression
4544      -- cast-expression
4545      unary-operator cast-expression
4546      sizeof unary-expression
4547      sizeof ( type-id )
4548      new-expression
4549      delete-expression
4550
4551    GNU Extensions:
4552
4553    unary-expression:
4554      __extension__ cast-expression
4555      __alignof__ unary-expression
4556      __alignof__ ( type-id )
4557      __real__ cast-expression
4558      __imag__ cast-expression
4559      && identifier
4560
4561    ADDRESS_P is true iff the unary-expression is appearing as the
4562    operand of the `&' operator.
4563
4564    Returns a representation of the expression.  */
4565
4566 static tree
4567 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4568 {
4569   cp_token *token;
4570   enum tree_code unary_operator;
4571
4572   /* Peek at the next token.  */
4573   token = cp_lexer_peek_token (parser->lexer);
4574   /* Some keywords give away the kind of expression.  */
4575   if (token->type == CPP_KEYWORD)
4576     {
4577       enum rid keyword = token->keyword;
4578
4579       switch (keyword)
4580         {
4581         case RID_ALIGNOF:
4582         case RID_SIZEOF:
4583           {
4584             tree operand;
4585             enum tree_code op;
4586
4587             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4588             /* Consume the token.  */
4589             cp_lexer_consume_token (parser->lexer);
4590             /* Parse the operand.  */
4591             operand = cp_parser_sizeof_operand (parser, keyword);
4592
4593             if (TYPE_P (operand))
4594               return cxx_sizeof_or_alignof_type (operand, op, true);
4595             else
4596               return cxx_sizeof_or_alignof_expr (operand, op);
4597           }
4598
4599         case RID_NEW:
4600           return cp_parser_new_expression (parser);
4601
4602         case RID_DELETE:
4603           return cp_parser_delete_expression (parser);
4604
4605         case RID_EXTENSION:
4606           {
4607             /* The saved value of the PEDANTIC flag.  */
4608             int saved_pedantic;
4609             tree expr;
4610
4611             /* Save away the PEDANTIC flag.  */
4612             cp_parser_extension_opt (parser, &saved_pedantic);
4613             /* Parse the cast-expression.  */
4614             expr = cp_parser_simple_cast_expression (parser);
4615             /* Restore the PEDANTIC flag.  */
4616             pedantic = saved_pedantic;
4617
4618             return expr;
4619           }
4620
4621         case RID_REALPART:
4622         case RID_IMAGPART:
4623           {
4624             tree expression;
4625
4626             /* Consume the `__real__' or `__imag__' token.  */
4627             cp_lexer_consume_token (parser->lexer);
4628             /* Parse the cast-expression.  */
4629             expression = cp_parser_simple_cast_expression (parser);
4630             /* Create the complete representation.  */
4631             return build_x_unary_op ((keyword == RID_REALPART
4632                                       ? REALPART_EXPR : IMAGPART_EXPR),
4633                                      expression);
4634           }
4635           break;
4636
4637         default:
4638           break;
4639         }
4640     }
4641
4642   /* Look for the `:: new' and `:: delete', which also signal the
4643      beginning of a new-expression, or delete-expression,
4644      respectively.  If the next token is `::', then it might be one of
4645      these.  */
4646   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4647     {
4648       enum rid keyword;
4649
4650       /* See if the token after the `::' is one of the keywords in
4651          which we're interested.  */
4652       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4653       /* If it's `new', we have a new-expression.  */
4654       if (keyword == RID_NEW)
4655         return cp_parser_new_expression (parser);
4656       /* Similarly, for `delete'.  */
4657       else if (keyword == RID_DELETE)
4658         return cp_parser_delete_expression (parser);
4659     }
4660
4661   /* Look for a unary operator.  */
4662   unary_operator = cp_parser_unary_operator (token);
4663   /* The `++' and `--' operators can be handled similarly, even though
4664      they are not technically unary-operators in the grammar.  */
4665   if (unary_operator == ERROR_MARK)
4666     {
4667       if (token->type == CPP_PLUS_PLUS)
4668         unary_operator = PREINCREMENT_EXPR;
4669       else if (token->type == CPP_MINUS_MINUS)
4670         unary_operator = PREDECREMENT_EXPR;
4671       /* Handle the GNU address-of-label extension.  */
4672       else if (cp_parser_allow_gnu_extensions_p (parser)
4673                && token->type == CPP_AND_AND)
4674         {
4675           tree identifier;
4676
4677           /* Consume the '&&' token.  */
4678           cp_lexer_consume_token (parser->lexer);
4679           /* Look for the identifier.  */
4680           identifier = cp_parser_identifier (parser);
4681           /* Create an expression representing the address.  */
4682           return finish_label_address_expr (identifier);
4683         }
4684     }
4685   if (unary_operator != ERROR_MARK)
4686     {
4687       tree cast_expression;
4688       tree expression = error_mark_node;
4689       const char *non_constant_p = NULL;
4690
4691       /* Consume the operator token.  */
4692       token = cp_lexer_consume_token (parser->lexer);
4693       /* Parse the cast-expression.  */
4694       cast_expression
4695         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4696       /* Now, build an appropriate representation.  */
4697       switch (unary_operator)
4698         {
4699         case INDIRECT_REF:
4700           non_constant_p = "`*'";
4701           expression = build_x_indirect_ref (cast_expression, "unary *");
4702           break;
4703
4704         case ADDR_EXPR:
4705           non_constant_p = "`&'";
4706           /* Fall through.  */
4707         case BIT_NOT_EXPR:
4708           expression = build_x_unary_op (unary_operator, cast_expression);
4709           break;
4710
4711         case PREINCREMENT_EXPR:
4712         case PREDECREMENT_EXPR:
4713           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4714                             ? "`++'" : "`--'");
4715           /* Fall through.  */
4716         case CONVERT_EXPR:
4717         case NEGATE_EXPR:
4718         case TRUTH_NOT_EXPR:
4719           expression = finish_unary_op_expr (unary_operator, cast_expression);
4720           break;
4721
4722         default:
4723           abort ();
4724         }
4725
4726       if (non_constant_p
4727           && cp_parser_non_integral_constant_expression (parser,
4728                                                          non_constant_p))
4729         expression = error_mark_node;
4730
4731       return expression;
4732     }
4733
4734   return cp_parser_postfix_expression (parser, address_p);
4735 }
4736
4737 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4738    unary-operator, the corresponding tree code is returned.  */
4739
4740 static enum tree_code
4741 cp_parser_unary_operator (cp_token* token)
4742 {
4743   switch (token->type)
4744     {
4745     case CPP_MULT:
4746       return INDIRECT_REF;
4747
4748     case CPP_AND:
4749       return ADDR_EXPR;
4750
4751     case CPP_PLUS:
4752       return CONVERT_EXPR;
4753
4754     case CPP_MINUS:
4755       return NEGATE_EXPR;
4756
4757     case CPP_NOT:
4758       return TRUTH_NOT_EXPR;
4759
4760     case CPP_COMPL:
4761       return BIT_NOT_EXPR;
4762
4763     default:
4764       return ERROR_MARK;
4765     }
4766 }
4767
4768 /* Parse a new-expression.
4769
4770    new-expression:
4771      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4772      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4773
4774    Returns a representation of the expression.  */
4775
4776 static tree
4777 cp_parser_new_expression (cp_parser* parser)
4778 {
4779   bool global_scope_p;
4780   tree placement;
4781   tree type;
4782   tree initializer;
4783   tree nelts;
4784
4785   /* Look for the optional `::' operator.  */
4786   global_scope_p
4787     = (cp_parser_global_scope_opt (parser,
4788                                    /*current_scope_valid_p=*/false)
4789        != NULL_TREE);
4790   /* Look for the `new' operator.  */
4791   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4792   /* There's no easy way to tell a new-placement from the
4793      `( type-id )' construct.  */
4794   cp_parser_parse_tentatively (parser);
4795   /* Look for a new-placement.  */
4796   placement = cp_parser_new_placement (parser);
4797   /* If that didn't work out, there's no new-placement.  */
4798   if (!cp_parser_parse_definitely (parser))
4799     placement = NULL_TREE;
4800
4801   /* If the next token is a `(', then we have a parenthesized
4802      type-id.  */
4803   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4804     {
4805       /* Consume the `('.  */
4806       cp_lexer_consume_token (parser->lexer);
4807       /* Parse the type-id.  */
4808       type = cp_parser_type_id (parser);
4809       /* Look for the closing `)'.  */
4810       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4811       /* There should not be a direct-new-declarator in this production,
4812          but GCC used to allowed this, so we check and emit a sensible error
4813          message for this case.  */
4814       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4815         {
4816           error ("array bound forbidden after parenthesized type-id");
4817           inform ("try removing the parentheses around the type-id");
4818           cp_parser_direct_new_declarator (parser);
4819         }
4820       nelts = integer_one_node;
4821     }
4822   /* Otherwise, there must be a new-type-id.  */
4823   else
4824     type = cp_parser_new_type_id (parser, &nelts);
4825
4826   /* If the next token is a `(', then we have a new-initializer.  */
4827   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4828     initializer = cp_parser_new_initializer (parser);
4829   else
4830     initializer = NULL_TREE;
4831
4832   /* A new-expression may not appear in an integral constant
4833      expression.  */
4834   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4835     return error_mark_node;
4836
4837   /* Create a representation of the new-expression.  */
4838   return build_new (placement, type, nelts, initializer, global_scope_p);
4839 }
4840
4841 /* Parse a new-placement.
4842
4843    new-placement:
4844      ( expression-list )
4845
4846    Returns the same representation as for an expression-list.  */
4847
4848 static tree
4849 cp_parser_new_placement (cp_parser* parser)
4850 {
4851   tree expression_list;
4852
4853   /* Parse the expression-list.  */
4854   expression_list = (cp_parser_parenthesized_expression_list
4855                      (parser, false, /*non_constant_p=*/NULL));
4856
4857   return expression_list;
4858 }
4859
4860 /* Parse a new-type-id.
4861
4862    new-type-id:
4863      type-specifier-seq new-declarator [opt]
4864
4865    Returns the TYPE allocated.  If the new-type-id indicates an array
4866    type, *NELTS is set to the number of elements in the last array
4867    bound; the TYPE will not include the last array bound.  */
4868
4869 static tree
4870 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4871 {
4872   cp_decl_specifier_seq type_specifier_seq;
4873   cp_declarator *new_declarator;
4874   cp_declarator *declarator;
4875   cp_declarator *outer_declarator;
4876   const char *saved_message;
4877   tree type;
4878
4879   /* The type-specifier sequence must not contain type definitions.
4880      (It cannot contain declarations of new types either, but if they
4881      are not definitions we will catch that because they are not
4882      complete.)  */
4883   saved_message = parser->type_definition_forbidden_message;
4884   parser->type_definition_forbidden_message
4885     = "types may not be defined in a new-type-id";
4886   /* Parse the type-specifier-seq.  */
4887   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4888   /* Restore the old message.  */
4889   parser->type_definition_forbidden_message = saved_message;
4890   /* Parse the new-declarator.  */
4891   new_declarator = cp_parser_new_declarator_opt (parser);
4892
4893   /* Determine the number of elements in the last array dimension, if
4894      any.  */
4895   *nelts = NULL_TREE;
4896   /* Skip down to the last array dimension.  */
4897   declarator = new_declarator;
4898   outer_declarator = NULL;
4899   while (declarator && (declarator->kind == cdk_pointer
4900                         || declarator->kind == cdk_ptrmem))
4901     {
4902       outer_declarator = declarator;
4903       declarator = declarator->declarator;
4904     }
4905   while (declarator
4906          && declarator->kind == cdk_array
4907          && declarator->declarator
4908          && declarator->declarator->kind == cdk_array)
4909     {
4910       outer_declarator = declarator;
4911       declarator = declarator->declarator;
4912     }
4913
4914   if (declarator && declarator->kind == cdk_array)
4915     {
4916       *nelts = declarator->u.array.bounds;
4917       if (*nelts == error_mark_node)
4918         *nelts = integer_one_node;
4919       else if (!processing_template_decl)
4920         {
4921           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4922                                            false))
4923             pedwarn ("size in array new must have integral type");
4924           *nelts = save_expr (cp_convert (sizetype, *nelts));
4925           if (*nelts == integer_zero_node)
4926             warning ("zero size array reserves no space");
4927         }
4928       if (outer_declarator)
4929         outer_declarator->declarator = declarator->declarator;
4930       else
4931         new_declarator = NULL;
4932     }
4933
4934   type = groktypename (&type_specifier_seq, new_declarator);
4935   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4936     {
4937       *nelts = array_type_nelts_top (type);
4938       type = TREE_TYPE (type);
4939     }
4940   return type;
4941 }
4942
4943 /* Parse an (optional) new-declarator.
4944
4945    new-declarator:
4946      ptr-operator new-declarator [opt]
4947      direct-new-declarator
4948
4949    Returns the declarator.  */
4950
4951 static cp_declarator *
4952 cp_parser_new_declarator_opt (cp_parser* parser)
4953 {
4954   enum tree_code code;
4955   tree type;
4956   cp_cv_quals cv_quals;
4957
4958   /* We don't know if there's a ptr-operator next, or not.  */
4959   cp_parser_parse_tentatively (parser);
4960   /* Look for a ptr-operator.  */
4961   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4962   /* If that worked, look for more new-declarators.  */
4963   if (cp_parser_parse_definitely (parser))
4964     {
4965       cp_declarator *declarator;
4966
4967       /* Parse another optional declarator.  */
4968       declarator = cp_parser_new_declarator_opt (parser);
4969
4970       /* Create the representation of the declarator.  */
4971       if (type)
4972         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4973       else if (code == INDIRECT_REF)
4974         declarator = make_pointer_declarator (cv_quals, declarator);
4975       else
4976         declarator = make_reference_declarator (cv_quals, declarator);
4977
4978       return declarator;
4979     }
4980
4981   /* If the next token is a `[', there is a direct-new-declarator.  */
4982   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4983     return cp_parser_direct_new_declarator (parser);
4984
4985   return NULL;
4986 }
4987
4988 /* Parse a direct-new-declarator.
4989
4990    direct-new-declarator:
4991      [ expression ]
4992      direct-new-declarator [constant-expression]
4993
4994    */
4995
4996 static cp_declarator *
4997 cp_parser_direct_new_declarator (cp_parser* parser)
4998 {
4999   cp_declarator *declarator = NULL;
5000
5001   while (true)
5002     {
5003       tree expression;
5004
5005       /* Look for the opening `['.  */
5006       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5007       /* The first expression is not required to be constant.  */
5008       if (!declarator)
5009         {
5010           expression = cp_parser_expression (parser);
5011           /* The standard requires that the expression have integral
5012              type.  DR 74 adds enumeration types.  We believe that the
5013              real intent is that these expressions be handled like the
5014              expression in a `switch' condition, which also allows
5015              classes with a single conversion to integral or
5016              enumeration type.  */
5017           if (!processing_template_decl)
5018             {
5019               expression
5020                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5021                                               expression,
5022                                               /*complain=*/true);
5023               if (!expression)
5024                 {
5025                   error ("expression in new-declarator must have integral or enumeration type");
5026                   expression = error_mark_node;
5027                 }
5028             }
5029         }
5030       /* But all the other expressions must be.  */
5031       else
5032         expression
5033           = cp_parser_constant_expression (parser,
5034                                            /*allow_non_constant=*/false,
5035                                            NULL);
5036       /* Look for the closing `]'.  */
5037       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5038
5039       /* Add this bound to the declarator.  */
5040       declarator = make_array_declarator (declarator, expression);
5041
5042       /* If the next token is not a `[', then there are no more
5043          bounds.  */
5044       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5045         break;
5046     }
5047
5048   return declarator;
5049 }
5050
5051 /* Parse a new-initializer.
5052
5053    new-initializer:
5054      ( expression-list [opt] )
5055
5056    Returns a representation of the expression-list.  If there is no
5057    expression-list, VOID_ZERO_NODE is returned.  */
5058
5059 static tree
5060 cp_parser_new_initializer (cp_parser* parser)
5061 {
5062   tree expression_list;
5063
5064   expression_list = (cp_parser_parenthesized_expression_list
5065                      (parser, false, /*non_constant_p=*/NULL));
5066   if (!expression_list)
5067     expression_list = void_zero_node;
5068
5069   return expression_list;
5070 }
5071
5072 /* Parse a delete-expression.
5073
5074    delete-expression:
5075      :: [opt] delete cast-expression
5076      :: [opt] delete [ ] cast-expression
5077
5078    Returns a representation of the expression.  */
5079
5080 static tree
5081 cp_parser_delete_expression (cp_parser* parser)
5082 {
5083   bool global_scope_p;
5084   bool array_p;
5085   tree expression;
5086
5087   /* Look for the optional `::' operator.  */
5088   global_scope_p
5089     = (cp_parser_global_scope_opt (parser,
5090                                    /*current_scope_valid_p=*/false)
5091        != NULL_TREE);
5092   /* Look for the `delete' keyword.  */
5093   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5094   /* See if the array syntax is in use.  */
5095   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5096     {
5097       /* Consume the `[' token.  */
5098       cp_lexer_consume_token (parser->lexer);
5099       /* Look for the `]' token.  */
5100       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5101       /* Remember that this is the `[]' construct.  */
5102       array_p = true;
5103     }
5104   else
5105     array_p = false;
5106
5107   /* Parse the cast-expression.  */
5108   expression = cp_parser_simple_cast_expression (parser);
5109
5110   /* A delete-expression may not appear in an integral constant
5111      expression.  */
5112   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5113     return error_mark_node;
5114
5115   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5116 }
5117
5118 /* Parse a cast-expression.
5119
5120    cast-expression:
5121      unary-expression
5122      ( type-id ) cast-expression
5123
5124    Returns a representation of the expression.  */
5125
5126 static tree
5127 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5128 {
5129   /* If it's a `(', then we might be looking at a cast.  */
5130   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5131     {
5132       tree type = NULL_TREE;
5133       tree expr = NULL_TREE;
5134       bool compound_literal_p;
5135       const char *saved_message;
5136
5137       /* There's no way to know yet whether or not this is a cast.
5138          For example, `(int (3))' is a unary-expression, while `(int)
5139          3' is a cast.  So, we resort to parsing tentatively.  */
5140       cp_parser_parse_tentatively (parser);
5141       /* Types may not be defined in a cast.  */
5142       saved_message = parser->type_definition_forbidden_message;
5143       parser->type_definition_forbidden_message
5144         = "types may not be defined in casts";
5145       /* Consume the `('.  */
5146       cp_lexer_consume_token (parser->lexer);
5147       /* A very tricky bit is that `(struct S) { 3 }' is a
5148          compound-literal (which we permit in C++ as an extension).
5149          But, that construct is not a cast-expression -- it is a
5150          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5151          is legal; if the compound-literal were a cast-expression,
5152          you'd need an extra set of parentheses.)  But, if we parse
5153          the type-id, and it happens to be a class-specifier, then we
5154          will commit to the parse at that point, because we cannot
5155          undo the action that is done when creating a new class.  So,
5156          then we cannot back up and do a postfix-expression.
5157
5158          Therefore, we scan ahead to the closing `)', and check to see
5159          if the token after the `)' is a `{'.  If so, we are not
5160          looking at a cast-expression.
5161
5162          Save tokens so that we can put them back.  */
5163       cp_lexer_save_tokens (parser->lexer);
5164       /* Skip tokens until the next token is a closing parenthesis.
5165          If we find the closing `)', and the next token is a `{', then
5166          we are looking at a compound-literal.  */
5167       compound_literal_p
5168         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5169                                                   /*consume_paren=*/true)
5170            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5171       /* Roll back the tokens we skipped.  */
5172       cp_lexer_rollback_tokens (parser->lexer);
5173       /* If we were looking at a compound-literal, simulate an error
5174          so that the call to cp_parser_parse_definitely below will
5175          fail.  */
5176       if (compound_literal_p)
5177         cp_parser_simulate_error (parser);
5178       else
5179         {
5180           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5181           parser->in_type_id_in_expr_p = true;
5182           /* Look for the type-id.  */
5183           type = cp_parser_type_id (parser);
5184           /* Look for the closing `)'.  */
5185           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5186           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5187         }
5188
5189       /* Restore the saved message.  */
5190       parser->type_definition_forbidden_message = saved_message;
5191
5192       /* If ok so far, parse the dependent expression. We cannot be
5193          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5194          ctor of T, but looks like a cast to function returning T
5195          without a dependent expression.  */
5196       if (!cp_parser_error_occurred (parser))
5197         expr = cp_parser_simple_cast_expression (parser);
5198
5199       if (cp_parser_parse_definitely (parser))
5200         {
5201           /* Warn about old-style casts, if so requested.  */
5202           if (warn_old_style_cast
5203               && !in_system_header
5204               && !VOID_TYPE_P (type)
5205               && current_lang_name != lang_name_c)
5206             warning ("use of old-style cast");
5207
5208           /* Only type conversions to integral or enumeration types
5209              can be used in constant-expressions.  */
5210           if (parser->integral_constant_expression_p
5211               && !dependent_type_p (type)
5212               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5213               && (cp_parser_non_integral_constant_expression
5214                   (parser,
5215                    "a cast to a type other than an integral or "
5216                    "enumeration type")))
5217             return error_mark_node;
5218
5219           /* Perform the cast.  */
5220           expr = build_c_cast (type, expr);
5221           return expr;
5222         }
5223     }
5224
5225   /* If we get here, then it's not a cast, so it must be a
5226      unary-expression.  */
5227   return cp_parser_unary_expression (parser, address_p);
5228 }
5229
5230 /* Parse a pm-expression.
5231
5232    pm-expression:
5233      cast-expression
5234      pm-expression .* cast-expression
5235      pm-expression ->* cast-expression
5236
5237      Returns a representation of the expression.  */
5238
5239 static tree
5240 cp_parser_pm_expression (cp_parser* parser)
5241 {
5242   static const cp_parser_token_tree_map map = {
5243     { CPP_DEREF_STAR, MEMBER_REF },
5244     { CPP_DOT_STAR, DOTSTAR_EXPR },
5245     { CPP_EOF, ERROR_MARK }
5246   };
5247
5248   return cp_parser_binary_expression (parser, map,
5249                                       cp_parser_simple_cast_expression);
5250 }
5251
5252 /* Parse a multiplicative-expression.
5253
5254    multiplicative-expression:
5255      pm-expression
5256      multiplicative-expression * pm-expression
5257      multiplicative-expression / pm-expression
5258      multiplicative-expression % pm-expression
5259
5260    Returns a representation of the expression.  */
5261
5262 static tree
5263 cp_parser_multiplicative_expression (cp_parser* parser)
5264 {
5265   static const cp_parser_token_tree_map map = {
5266     { CPP_MULT, MULT_EXPR },
5267     { CPP_DIV, TRUNC_DIV_EXPR },
5268     { CPP_MOD, TRUNC_MOD_EXPR },
5269     { CPP_EOF, ERROR_MARK }
5270   };
5271
5272   return cp_parser_binary_expression (parser,
5273                                       map,
5274                                       cp_parser_pm_expression);
5275 }
5276
5277 /* Parse an additive-expression.
5278
5279    additive-expression:
5280      multiplicative-expression
5281      additive-expression + multiplicative-expression
5282      additive-expression - multiplicative-expression
5283
5284    Returns a representation of the expression.  */
5285
5286 static tree
5287 cp_parser_additive_expression (cp_parser* parser)
5288 {
5289   static const cp_parser_token_tree_map map = {
5290     { CPP_PLUS, PLUS_EXPR },
5291     { CPP_MINUS, MINUS_EXPR },
5292     { CPP_EOF, ERROR_MARK }
5293   };
5294
5295   return cp_parser_binary_expression (parser,
5296                                       map,
5297                                       cp_parser_multiplicative_expression);
5298 }
5299
5300 /* Parse a shift-expression.
5301
5302    shift-expression:
5303      additive-expression
5304      shift-expression << additive-expression
5305      shift-expression >> additive-expression
5306
5307    Returns a representation of the expression.  */
5308
5309 static tree
5310 cp_parser_shift_expression (cp_parser* parser)
5311 {
5312   static const cp_parser_token_tree_map map = {
5313     { CPP_LSHIFT, LSHIFT_EXPR },
5314     { CPP_RSHIFT, RSHIFT_EXPR },
5315     { CPP_EOF, ERROR_MARK }
5316   };
5317
5318   return cp_parser_binary_expression (parser,
5319                                       map,
5320                                       cp_parser_additive_expression);
5321 }
5322
5323 /* Parse a relational-expression.
5324
5325    relational-expression:
5326      shift-expression
5327      relational-expression < shift-expression
5328      relational-expression > shift-expression
5329      relational-expression <= shift-expression
5330      relational-expression >= shift-expression
5331
5332    GNU Extension:
5333
5334    relational-expression:
5335      relational-expression <? shift-expression
5336      relational-expression >? shift-expression
5337
5338    Returns a representation of the expression.  */
5339
5340 static tree
5341 cp_parser_relational_expression (cp_parser* parser)
5342 {
5343   static const cp_parser_token_tree_map map = {
5344     { CPP_LESS, LT_EXPR },
5345     { CPP_GREATER, GT_EXPR },
5346     { CPP_LESS_EQ, LE_EXPR },
5347     { CPP_GREATER_EQ, GE_EXPR },
5348     { CPP_MIN, MIN_EXPR },
5349     { CPP_MAX, MAX_EXPR },
5350     { CPP_EOF, ERROR_MARK }
5351   };
5352
5353   return cp_parser_binary_expression (parser,
5354                                       map,
5355                                       cp_parser_shift_expression);
5356 }
5357
5358 /* Parse an equality-expression.
5359
5360    equality-expression:
5361      relational-expression
5362      equality-expression == relational-expression
5363      equality-expression != relational-expression
5364
5365    Returns a representation of the expression.  */
5366
5367 static tree
5368 cp_parser_equality_expression (cp_parser* parser)
5369 {
5370   static const cp_parser_token_tree_map map = {
5371     { CPP_EQ_EQ, EQ_EXPR },
5372     { CPP_NOT_EQ, NE_EXPR },
5373     { CPP_EOF, ERROR_MARK }
5374   };
5375
5376   return cp_parser_binary_expression (parser,
5377                                       map,
5378                                       cp_parser_relational_expression);
5379 }
5380
5381 /* Parse an and-expression.
5382
5383    and-expression:
5384      equality-expression
5385      and-expression & equality-expression
5386
5387    Returns a representation of the expression.  */
5388
5389 static tree
5390 cp_parser_and_expression (cp_parser* parser)
5391 {
5392   static const cp_parser_token_tree_map map = {
5393     { CPP_AND, BIT_AND_EXPR },
5394     { CPP_EOF, ERROR_MARK }
5395   };
5396
5397   return cp_parser_binary_expression (parser,
5398                                       map,
5399                                       cp_parser_equality_expression);
5400 }
5401
5402 /* Parse an exclusive-or-expression.
5403
5404    exclusive-or-expression:
5405      and-expression
5406      exclusive-or-expression ^ and-expression
5407
5408    Returns a representation of the expression.  */
5409
5410 static tree
5411 cp_parser_exclusive_or_expression (cp_parser* parser)
5412 {
5413   static const cp_parser_token_tree_map map = {
5414     { CPP_XOR, BIT_XOR_EXPR },
5415     { CPP_EOF, ERROR_MARK }
5416   };
5417
5418   return cp_parser_binary_expression (parser,
5419                                       map,
5420                                       cp_parser_and_expression);
5421 }
5422
5423
5424 /* Parse an inclusive-or-expression.
5425
5426    inclusive-or-expression:
5427      exclusive-or-expression
5428      inclusive-or-expression | exclusive-or-expression
5429
5430    Returns a representation of the expression.  */
5431
5432 static tree
5433 cp_parser_inclusive_or_expression (cp_parser* parser)
5434 {
5435   static const cp_parser_token_tree_map map = {
5436     { CPP_OR, BIT_IOR_EXPR },
5437     { CPP_EOF, ERROR_MARK }
5438   };
5439
5440   return cp_parser_binary_expression (parser,
5441                                       map,
5442                                       cp_parser_exclusive_or_expression);
5443 }
5444
5445 /* Parse a logical-and-expression.
5446
5447    logical-and-expression:
5448      inclusive-or-expression
5449      logical-and-expression && inclusive-or-expression
5450
5451    Returns a representation of the expression.  */
5452
5453 static tree
5454 cp_parser_logical_and_expression (cp_parser* parser)
5455 {
5456   static const cp_parser_token_tree_map map = {
5457     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5458     { CPP_EOF, ERROR_MARK }
5459   };
5460
5461   return cp_parser_binary_expression (parser,
5462                                       map,
5463                                       cp_parser_inclusive_or_expression);
5464 }
5465
5466 /* Parse a logical-or-expression.
5467
5468    logical-or-expression:
5469      logical-and-expression
5470      logical-or-expression || logical-and-expression
5471
5472    Returns a representation of the expression.  */
5473
5474 static tree
5475 cp_parser_logical_or_expression (cp_parser* parser)
5476 {
5477   static const cp_parser_token_tree_map map = {
5478     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5479     { CPP_EOF, ERROR_MARK }
5480   };
5481
5482   return cp_parser_binary_expression (parser,
5483                                       map,
5484                                       cp_parser_logical_and_expression);
5485 }
5486
5487 /* Parse the `? expression : assignment-expression' part of a
5488    conditional-expression.  The LOGICAL_OR_EXPR is the
5489    logical-or-expression that started the conditional-expression.
5490    Returns a representation of the entire conditional-expression.
5491
5492    This routine is used by cp_parser_assignment_expression.
5493
5494      ? expression : assignment-expression
5495
5496    GNU Extensions:
5497
5498      ? : assignment-expression */
5499
5500 static tree
5501 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5502 {
5503   tree expr;
5504   tree assignment_expr;
5505
5506   /* Consume the `?' token.  */
5507   cp_lexer_consume_token (parser->lexer);
5508   if (cp_parser_allow_gnu_extensions_p (parser)
5509       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5510     /* Implicit true clause.  */
5511     expr = NULL_TREE;
5512   else
5513     /* Parse the expression.  */
5514     expr = cp_parser_expression (parser);
5515
5516   /* The next token should be a `:'.  */
5517   cp_parser_require (parser, CPP_COLON, "`:'");
5518   /* Parse the assignment-expression.  */
5519   assignment_expr = cp_parser_assignment_expression (parser);
5520
5521   /* Build the conditional-expression.  */
5522   return build_x_conditional_expr (logical_or_expr,
5523                                    expr,
5524                                    assignment_expr);
5525 }
5526
5527 /* Parse an assignment-expression.
5528
5529    assignment-expression:
5530      conditional-expression
5531      logical-or-expression assignment-operator assignment_expression
5532      throw-expression
5533
5534    Returns a representation for the expression.  */
5535
5536 static tree
5537 cp_parser_assignment_expression (cp_parser* parser)
5538 {
5539   tree expr;
5540
5541   /* If the next token is the `throw' keyword, then we're looking at
5542      a throw-expression.  */
5543   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5544     expr = cp_parser_throw_expression (parser);
5545   /* Otherwise, it must be that we are looking at a
5546      logical-or-expression.  */
5547   else
5548     {
5549       /* Parse the logical-or-expression.  */
5550       expr = cp_parser_logical_or_expression (parser);
5551       /* If the next token is a `?' then we're actually looking at a
5552          conditional-expression.  */
5553       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5554         return cp_parser_question_colon_clause (parser, expr);
5555       else
5556         {
5557           enum tree_code assignment_operator;
5558
5559           /* If it's an assignment-operator, we're using the second
5560              production.  */
5561           assignment_operator
5562             = cp_parser_assignment_operator_opt (parser);
5563           if (assignment_operator != ERROR_MARK)
5564             {
5565               tree rhs;
5566
5567               /* Parse the right-hand side of the assignment.  */
5568               rhs = cp_parser_assignment_expression (parser);
5569               /* An assignment may not appear in a
5570                  constant-expression.  */
5571               if (cp_parser_non_integral_constant_expression (parser,
5572                                                               "an assignment"))
5573                 return error_mark_node;
5574               /* Build the assignment expression.  */
5575               expr = build_x_modify_expr (expr,
5576                                           assignment_operator,
5577                                           rhs);
5578             }
5579         }
5580     }
5581
5582   return expr;
5583 }
5584
5585 /* Parse an (optional) assignment-operator.
5586
5587    assignment-operator: one of
5588      = *= /= %= += -= >>= <<= &= ^= |=
5589
5590    GNU Extension:
5591
5592    assignment-operator: one of
5593      <?= >?=
5594
5595    If the next token is an assignment operator, the corresponding tree
5596    code is returned, and the token is consumed.  For example, for
5597    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5598    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5599    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5600    operator, ERROR_MARK is returned.  */
5601
5602 static enum tree_code
5603 cp_parser_assignment_operator_opt (cp_parser* parser)
5604 {
5605   enum tree_code op;
5606   cp_token *token;
5607
5608   /* Peek at the next toen.  */
5609   token = cp_lexer_peek_token (parser->lexer);
5610
5611   switch (token->type)
5612     {
5613     case CPP_EQ:
5614       op = NOP_EXPR;
5615       break;
5616
5617     case CPP_MULT_EQ:
5618       op = MULT_EXPR;
5619       break;
5620
5621     case CPP_DIV_EQ:
5622       op = TRUNC_DIV_EXPR;
5623       break;
5624
5625     case CPP_MOD_EQ:
5626       op = TRUNC_MOD_EXPR;
5627       break;
5628
5629     case CPP_PLUS_EQ:
5630       op = PLUS_EXPR;
5631       break;
5632
5633     case CPP_MINUS_EQ:
5634       op = MINUS_EXPR;
5635       break;
5636
5637     case CPP_RSHIFT_EQ:
5638       op = RSHIFT_EXPR;
5639       break;
5640
5641     case CPP_LSHIFT_EQ:
5642       op = LSHIFT_EXPR;
5643       break;
5644
5645     case CPP_AND_EQ:
5646       op = BIT_AND_EXPR;
5647       break;
5648
5649     case CPP_XOR_EQ:
5650       op = BIT_XOR_EXPR;
5651       break;
5652
5653     case CPP_OR_EQ:
5654       op = BIT_IOR_EXPR;
5655       break;
5656
5657     case CPP_MIN_EQ:
5658       op = MIN_EXPR;
5659       break;
5660
5661     case CPP_MAX_EQ:
5662       op = MAX_EXPR;
5663       break;
5664
5665     default:
5666       /* Nothing else is an assignment operator.  */
5667       op = ERROR_MARK;
5668     }
5669
5670   /* If it was an assignment operator, consume it.  */
5671   if (op != ERROR_MARK)
5672     cp_lexer_consume_token (parser->lexer);
5673
5674   return op;
5675 }
5676
5677 /* Parse an expression.
5678
5679    expression:
5680      assignment-expression
5681      expression , assignment-expression
5682
5683    Returns a representation of the expression.  */
5684
5685 static tree
5686 cp_parser_expression (cp_parser* parser)
5687 {
5688   tree expression = NULL_TREE;
5689
5690   while (true)
5691     {
5692       tree assignment_expression;
5693
5694       /* Parse the next assignment-expression.  */
5695       assignment_expression
5696         = cp_parser_assignment_expression (parser);
5697       /* If this is the first assignment-expression, we can just
5698          save it away.  */
5699       if (!expression)
5700         expression = assignment_expression;
5701       else
5702         expression = build_x_compound_expr (expression,
5703                                             assignment_expression);
5704       /* If the next token is not a comma, then we are done with the
5705          expression.  */
5706       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5707         break;
5708       /* Consume the `,'.  */
5709       cp_lexer_consume_token (parser->lexer);
5710       /* A comma operator cannot appear in a constant-expression.  */
5711       if (cp_parser_non_integral_constant_expression (parser,
5712                                                       "a comma operator"))
5713         expression = error_mark_node;
5714     }
5715
5716   return expression;
5717 }
5718
5719 /* Parse a constant-expression.
5720
5721    constant-expression:
5722      conditional-expression
5723
5724   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5725   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5726   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5727   is false, NON_CONSTANT_P should be NULL.  */
5728
5729 static tree
5730 cp_parser_constant_expression (cp_parser* parser,
5731                                bool allow_non_constant_p,
5732                                bool *non_constant_p)
5733 {
5734   bool saved_integral_constant_expression_p;
5735   bool saved_allow_non_integral_constant_expression_p;
5736   bool saved_non_integral_constant_expression_p;
5737   tree expression;
5738
5739   /* It might seem that we could simply parse the
5740      conditional-expression, and then check to see if it were
5741      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5742      one that the compiler can figure out is constant, possibly after
5743      doing some simplifications or optimizations.  The standard has a
5744      precise definition of constant-expression, and we must honor
5745      that, even though it is somewhat more restrictive.
5746
5747      For example:
5748
5749        int i[(2, 3)];
5750
5751      is not a legal declaration, because `(2, 3)' is not a
5752      constant-expression.  The `,' operator is forbidden in a
5753      constant-expression.  However, GCC's constant-folding machinery
5754      will fold this operation to an INTEGER_CST for `3'.  */
5755
5756   /* Save the old settings.  */
5757   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5758   saved_allow_non_integral_constant_expression_p
5759     = parser->allow_non_integral_constant_expression_p;
5760   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5761   /* We are now parsing a constant-expression.  */
5762   parser->integral_constant_expression_p = true;
5763   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5764   parser->non_integral_constant_expression_p = false;
5765   /* Although the grammar says "conditional-expression", we parse an
5766      "assignment-expression", which also permits "throw-expression"
5767      and the use of assignment operators.  In the case that
5768      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5769      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5770      actually essential that we look for an assignment-expression.
5771      For example, cp_parser_initializer_clauses uses this function to
5772      determine whether a particular assignment-expression is in fact
5773      constant.  */
5774   expression = cp_parser_assignment_expression (parser);
5775   /* Restore the old settings.  */
5776   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5777   parser->allow_non_integral_constant_expression_p
5778     = saved_allow_non_integral_constant_expression_p;
5779   if (allow_non_constant_p)
5780     *non_constant_p = parser->non_integral_constant_expression_p;
5781   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5782
5783   return expression;
5784 }
5785
5786 /* Parse __builtin_offsetof.
5787
5788    offsetof-expression:
5789      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5790
5791    offsetof-member-designator:
5792      id-expression
5793      | offsetof-member-designator "." id-expression
5794      | offsetof-member-designator "[" expression "]"
5795 */
5796
5797 static tree
5798 cp_parser_builtin_offsetof (cp_parser *parser)
5799 {
5800   int save_ice_p, save_non_ice_p;
5801   tree type, expr;
5802   cp_id_kind dummy;
5803
5804   /* We're about to accept non-integral-constant things, but will
5805      definitely yield an integral constant expression.  Save and
5806      restore these values around our local parsing.  */
5807   save_ice_p = parser->integral_constant_expression_p;
5808   save_non_ice_p = parser->non_integral_constant_expression_p;
5809
5810   /* Consume the "__builtin_offsetof" token.  */
5811   cp_lexer_consume_token (parser->lexer);
5812   /* Consume the opening `('.  */
5813   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5814   /* Parse the type-id.  */
5815   type = cp_parser_type_id (parser);
5816   /* Look for the `,'.  */
5817   cp_parser_require (parser, CPP_COMMA, "`,'");
5818
5819   /* Build the (type *)null that begins the traditional offsetof macro.  */
5820   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5821
5822   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5823   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5824                                                  true, &dummy);
5825   while (true)
5826     {
5827       cp_token *token = cp_lexer_peek_token (parser->lexer);
5828       switch (token->type)
5829         {
5830         case CPP_OPEN_SQUARE:
5831           /* offsetof-member-designator "[" expression "]" */
5832           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5833           break;
5834
5835         case CPP_DOT:
5836           /* offsetof-member-designator "." identifier */
5837           cp_lexer_consume_token (parser->lexer);
5838           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5839                                                          true, &dummy);
5840           break;
5841
5842         case CPP_CLOSE_PAREN:
5843           /* Consume the ")" token.  */
5844           cp_lexer_consume_token (parser->lexer);
5845           goto success;
5846
5847         default:
5848           /* Error.  We know the following require will fail, but
5849              that gives the proper error message.  */
5850           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5851           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5852           expr = error_mark_node;
5853           goto failure;
5854         }
5855     }
5856
5857  success:
5858   /* We've finished the parsing, now finish with the semantics.  At present
5859      we're just mirroring the traditional macro implementation.  Better
5860      would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5861      here rather than in build_x_unary_op.  */
5862   
5863   expr = (build_reinterpret_cast 
5864           (build_reference_type (cp_build_qualified_type 
5865                                  (char_type_node, 
5866                                   TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)), 
5867            expr));
5868   expr = build_x_unary_op (ADDR_EXPR, expr);
5869   expr = build_reinterpret_cast (size_type_node, expr);
5870
5871  failure:
5872   parser->integral_constant_expression_p = save_ice_p;
5873   parser->non_integral_constant_expression_p = save_non_ice_p;
5874
5875   return expr;
5876 }
5877
5878 /* Statements [gram.stmt.stmt]  */
5879
5880 /* Parse a statement.
5881
5882    statement:
5883      labeled-statement
5884      expression-statement
5885      compound-statement
5886      selection-statement
5887      iteration-statement
5888      jump-statement
5889      declaration-statement
5890      try-block  */
5891
5892 static void
5893 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5894 {
5895   tree statement;
5896   cp_token *token;
5897   location_t statement_location;
5898
5899   /* There is no statement yet.  */
5900   statement = NULL_TREE;
5901   /* Peek at the next token.  */
5902   token = cp_lexer_peek_token (parser->lexer);
5903   /* Remember the location of the first token in the statement.  */
5904   statement_location = token->location;
5905   /* If this is a keyword, then that will often determine what kind of
5906      statement we have.  */
5907   if (token->type == CPP_KEYWORD)
5908     {
5909       enum rid keyword = token->keyword;
5910
5911       switch (keyword)
5912         {
5913         case RID_CASE:
5914         case RID_DEFAULT:
5915           statement = cp_parser_labeled_statement (parser,
5916                                                    in_statement_expr);
5917           break;
5918
5919         case RID_IF:
5920         case RID_SWITCH:
5921           statement = cp_parser_selection_statement (parser);
5922           break;
5923
5924         case RID_WHILE:
5925         case RID_DO:
5926         case RID_FOR:
5927           statement = cp_parser_iteration_statement (parser);
5928           break;
5929
5930         case RID_BREAK:
5931         case RID_CONTINUE:
5932         case RID_RETURN:
5933         case RID_GOTO:
5934           statement = cp_parser_jump_statement (parser);
5935           break;
5936
5937         case RID_TRY:
5938           statement = cp_parser_try_block (parser);
5939           break;
5940
5941         default:
5942           /* It might be a keyword like `int' that can start a
5943              declaration-statement.  */
5944           break;
5945         }
5946     }
5947   else if (token->type == CPP_NAME)
5948     {
5949       /* If the next token is a `:', then we are looking at a
5950          labeled-statement.  */
5951       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5952       if (token->type == CPP_COLON)
5953         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5954     }
5955   /* Anything that starts with a `{' must be a compound-statement.  */
5956   else if (token->type == CPP_OPEN_BRACE)
5957     statement = cp_parser_compound_statement (parser, NULL, false);
5958
5959   /* Everything else must be a declaration-statement or an
5960      expression-statement.  Try for the declaration-statement
5961      first, unless we are looking at a `;', in which case we know that
5962      we have an expression-statement.  */
5963   if (!statement)
5964     {
5965       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5966         {
5967           cp_parser_parse_tentatively (parser);
5968           /* Try to parse the declaration-statement.  */
5969           cp_parser_declaration_statement (parser);
5970           /* If that worked, we're done.  */
5971           if (cp_parser_parse_definitely (parser))
5972             return;
5973         }
5974       /* Look for an expression-statement instead.  */
5975       statement = cp_parser_expression_statement (parser, in_statement_expr);
5976     }
5977
5978   /* Set the line number for the statement.  */
5979   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5980     SET_EXPR_LOCATION (statement, statement_location);
5981 }
5982
5983 /* Parse a labeled-statement.
5984
5985    labeled-statement:
5986      identifier : statement
5987      case constant-expression : statement
5988      default : statement
5989
5990    GNU Extension:
5991
5992    labeled-statement:
5993      case constant-expression ... constant-expression : statement
5994
5995    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5996    For an ordinary label, returns a LABEL_EXPR.  */
5997
5998 static tree
5999 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6000 {
6001   cp_token *token;
6002   tree statement = error_mark_node;
6003
6004   /* The next token should be an identifier.  */
6005   token = cp_lexer_peek_token (parser->lexer);
6006   if (token->type != CPP_NAME
6007       && token->type != CPP_KEYWORD)
6008     {
6009       cp_parser_error (parser, "expected labeled-statement");
6010       return error_mark_node;
6011     }
6012
6013   switch (token->keyword)
6014     {
6015     case RID_CASE:
6016       {
6017         tree expr, expr_hi;
6018         cp_token *ellipsis;
6019
6020         /* Consume the `case' token.  */
6021         cp_lexer_consume_token (parser->lexer);
6022         /* Parse the constant-expression.  */
6023         expr = cp_parser_constant_expression (parser,
6024                                               /*allow_non_constant_p=*/false,
6025                                               NULL);
6026
6027         ellipsis = cp_lexer_peek_token (parser->lexer);
6028         if (ellipsis->type == CPP_ELLIPSIS)
6029           {
6030             /* Consume the `...' token.  */
6031             cp_lexer_consume_token (parser->lexer);
6032             expr_hi =
6033               cp_parser_constant_expression (parser,
6034                                              /*allow_non_constant_p=*/false,
6035                                              NULL);
6036             /* We don't need to emit warnings here, as the common code
6037                will do this for us.  */
6038           }
6039         else
6040           expr_hi = NULL_TREE;
6041
6042         if (!parser->in_switch_statement_p)
6043           error ("case label `%E' not within a switch statement", expr);
6044         else
6045           statement = finish_case_label (expr, expr_hi);
6046       }
6047       break;
6048
6049     case RID_DEFAULT:
6050       /* Consume the `default' token.  */
6051       cp_lexer_consume_token (parser->lexer);
6052       if (!parser->in_switch_statement_p)
6053         error ("case label not within a switch statement");
6054       else
6055         statement = finish_case_label (NULL_TREE, NULL_TREE);
6056       break;
6057
6058     default:
6059       /* Anything else must be an ordinary label.  */
6060       statement = finish_label_stmt (cp_parser_identifier (parser));
6061       break;
6062     }
6063
6064   /* Require the `:' token.  */
6065   cp_parser_require (parser, CPP_COLON, "`:'");
6066   /* Parse the labeled statement.  */
6067   cp_parser_statement (parser, in_statement_expr);
6068
6069   /* Return the label, in the case of a `case' or `default' label.  */
6070   return statement;
6071 }
6072
6073 /* Parse an expression-statement.
6074
6075    expression-statement:
6076      expression [opt] ;
6077
6078    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6079    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6080    indicates whether this expression-statement is part of an
6081    expression statement.  */
6082
6083 static tree
6084 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6085 {
6086   tree statement = NULL_TREE;
6087
6088   /* If the next token is a ';', then there is no expression
6089      statement.  */
6090   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6091     statement = cp_parser_expression (parser);
6092
6093   /* Consume the final `;'.  */
6094   cp_parser_consume_semicolon_at_end_of_statement (parser);
6095
6096   if (in_statement_expr
6097       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6098     {
6099       /* This is the final expression statement of a statement
6100          expression.  */
6101       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6102     }
6103   else if (statement)
6104     statement = finish_expr_stmt (statement);
6105   else
6106     finish_stmt ();
6107
6108   return statement;
6109 }
6110
6111 /* Parse a compound-statement.
6112
6113    compound-statement:
6114      { statement-seq [opt] }
6115
6116    Returns a tree representing the statement.  */
6117
6118 static tree
6119 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6120                               bool in_try)
6121 {
6122   tree compound_stmt;
6123
6124   /* Consume the `{'.  */
6125   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6126     return error_mark_node;
6127   /* Begin the compound-statement.  */
6128   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6129   /* Parse an (optional) statement-seq.  */
6130   cp_parser_statement_seq_opt (parser, in_statement_expr);
6131   /* Finish the compound-statement.  */
6132   finish_compound_stmt (compound_stmt);
6133   /* Consume the `}'.  */
6134   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6135
6136   return compound_stmt;
6137 }
6138
6139 /* Parse an (optional) statement-seq.
6140
6141    statement-seq:
6142      statement
6143      statement-seq [opt] statement  */
6144
6145 static void
6146 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6147 {
6148   /* Scan statements until there aren't any more.  */
6149   while (true)
6150     {
6151       /* If we're looking at a `}', then we've run out of statements.  */
6152       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6153           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6154         break;
6155
6156       /* Parse the statement.  */
6157       cp_parser_statement (parser, in_statement_expr);
6158     }
6159 }
6160
6161 /* Parse a selection-statement.
6162
6163    selection-statement:
6164      if ( condition ) statement
6165      if ( condition ) statement else statement
6166      switch ( condition ) statement
6167
6168    Returns the new IF_STMT or SWITCH_STMT.  */
6169
6170 static tree
6171 cp_parser_selection_statement (cp_parser* parser)
6172 {
6173   cp_token *token;
6174   enum rid keyword;
6175
6176   /* Peek at the next token.  */
6177   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6178
6179   /* See what kind of keyword it is.  */
6180   keyword = token->keyword;
6181   switch (keyword)
6182     {
6183     case RID_IF:
6184     case RID_SWITCH:
6185       {
6186         tree statement;
6187         tree condition;
6188
6189         /* Look for the `('.  */
6190         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6191           {
6192             cp_parser_skip_to_end_of_statement (parser);
6193             return error_mark_node;
6194           }
6195
6196         /* Begin the selection-statement.  */
6197         if (keyword == RID_IF)
6198           statement = begin_if_stmt ();
6199         else
6200           statement = begin_switch_stmt ();
6201
6202         /* Parse the condition.  */
6203         condition = cp_parser_condition (parser);
6204         /* Look for the `)'.  */
6205         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6206           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6207                                                  /*consume_paren=*/true);
6208
6209         if (keyword == RID_IF)
6210           {
6211             /* Add the condition.  */
6212             finish_if_stmt_cond (condition, statement);
6213
6214             /* Parse the then-clause.  */
6215             cp_parser_implicitly_scoped_statement (parser);
6216             finish_then_clause (statement);
6217
6218             /* If the next token is `else', parse the else-clause.  */
6219             if (cp_lexer_next_token_is_keyword (parser->lexer,
6220                                                 RID_ELSE))
6221               {
6222                 /* Consume the `else' keyword.  */
6223                 cp_lexer_consume_token (parser->lexer);
6224                 begin_else_clause (statement);
6225                 /* Parse the else-clause.  */
6226                 cp_parser_implicitly_scoped_statement (parser);
6227                 finish_else_clause (statement);
6228               }
6229
6230             /* Now we're all done with the if-statement.  */
6231             finish_if_stmt (statement);
6232           }
6233         else
6234           {
6235             bool in_switch_statement_p;
6236
6237             /* Add the condition.  */
6238             finish_switch_cond (condition, statement);
6239
6240             /* Parse the body of the switch-statement.  */
6241             in_switch_statement_p = parser->in_switch_statement_p;
6242             parser->in_switch_statement_p = true;
6243             cp_parser_implicitly_scoped_statement (parser);
6244             parser->in_switch_statement_p = in_switch_statement_p;
6245
6246             /* Now we're all done with the switch-statement.  */
6247             finish_switch_stmt (statement);
6248           }
6249
6250         return statement;
6251       }
6252       break;
6253
6254     default:
6255       cp_parser_error (parser, "expected selection-statement");
6256       return error_mark_node;
6257     }
6258 }
6259
6260 /* Parse a condition.
6261
6262    condition:
6263      expression
6264      type-specifier-seq declarator = assignment-expression
6265
6266    GNU Extension:
6267
6268    condition:
6269      type-specifier-seq declarator asm-specification [opt]
6270        attributes [opt] = assignment-expression
6271
6272    Returns the expression that should be tested.  */
6273
6274 static tree
6275 cp_parser_condition (cp_parser* parser)
6276 {
6277   cp_decl_specifier_seq type_specifiers;
6278   const char *saved_message;
6279
6280   /* Try the declaration first.  */
6281   cp_parser_parse_tentatively (parser);
6282   /* New types are not allowed in the type-specifier-seq for a
6283      condition.  */
6284   saved_message = parser->type_definition_forbidden_message;
6285   parser->type_definition_forbidden_message
6286     = "types may not be defined in conditions";
6287   /* Parse the type-specifier-seq.  */
6288   cp_parser_type_specifier_seq (parser, &type_specifiers);
6289   /* Restore the saved message.  */
6290   parser->type_definition_forbidden_message = saved_message;
6291   /* If all is well, we might be looking at a declaration.  */
6292   if (!cp_parser_error_occurred (parser))
6293     {
6294       tree decl;
6295       tree asm_specification;
6296       tree attributes;
6297       cp_declarator *declarator;
6298       tree initializer = NULL_TREE;
6299
6300       /* Parse the declarator.  */
6301       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6302                                          /*ctor_dtor_or_conv_p=*/NULL,
6303                                          /*parenthesized_p=*/NULL);
6304       /* Parse the attributes.  */
6305       attributes = cp_parser_attributes_opt (parser);
6306       /* Parse the asm-specification.  */
6307       asm_specification = cp_parser_asm_specification_opt (parser);
6308       /* If the next token is not an `=', then we might still be
6309          looking at an expression.  For example:
6310
6311            if (A(a).x)
6312
6313          looks like a decl-specifier-seq and a declarator -- but then
6314          there is no `=', so this is an expression.  */
6315       cp_parser_require (parser, CPP_EQ, "`='");
6316       /* If we did see an `=', then we are looking at a declaration
6317          for sure.  */
6318       if (cp_parser_parse_definitely (parser))
6319         {
6320           bool pop_p;
6321
6322           /* Create the declaration.  */
6323           decl = start_decl (declarator, &type_specifiers,
6324                              /*initialized_p=*/true,
6325                              attributes, /*prefix_attributes=*/NULL_TREE,
6326                              &pop_p);
6327           /* Parse the assignment-expression.  */
6328           initializer = cp_parser_assignment_expression (parser);
6329
6330           /* Process the initializer.  */
6331           cp_finish_decl (decl,
6332                           initializer,
6333                           asm_specification,
6334                           LOOKUP_ONLYCONVERTING);
6335           if (pop_p)
6336             pop_scope (DECL_CONTEXT (decl));
6337
6338           return convert_from_reference (decl);
6339         }
6340     }
6341   /* If we didn't even get past the declarator successfully, we are
6342      definitely not looking at a declaration.  */
6343   else
6344     cp_parser_abort_tentative_parse (parser);
6345
6346   /* Otherwise, we are looking at an expression.  */
6347   return cp_parser_expression (parser);
6348 }
6349
6350 /* Parse an iteration-statement.
6351
6352    iteration-statement:
6353      while ( condition ) statement
6354      do statement while ( expression ) ;
6355      for ( for-init-statement condition [opt] ; expression [opt] )
6356        statement
6357
6358    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6359
6360 static tree
6361 cp_parser_iteration_statement (cp_parser* parser)
6362 {
6363   cp_token *token;
6364   enum rid keyword;
6365   tree statement;
6366   bool in_iteration_statement_p;
6367
6368
6369   /* Peek at the next token.  */
6370   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6371   if (!token)
6372     return error_mark_node;
6373
6374   /* Remember whether or not we are already within an iteration
6375      statement.  */
6376   in_iteration_statement_p = parser->in_iteration_statement_p;
6377
6378   /* See what kind of keyword it is.  */
6379   keyword = token->keyword;
6380   switch (keyword)
6381     {
6382     case RID_WHILE:
6383       {
6384         tree condition;
6385
6386         /* Begin the while-statement.  */
6387         statement = begin_while_stmt ();
6388         /* Look for the `('.  */
6389         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6390         /* Parse the condition.  */
6391         condition = cp_parser_condition (parser);
6392         finish_while_stmt_cond (condition, statement);
6393         /* Look for the `)'.  */
6394         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6395         /* Parse the dependent statement.  */
6396         parser->in_iteration_statement_p = true;
6397         cp_parser_already_scoped_statement (parser);
6398         parser->in_iteration_statement_p = in_iteration_statement_p;
6399         /* We're done with the while-statement.  */
6400         finish_while_stmt (statement);
6401       }
6402       break;
6403
6404     case RID_DO:
6405       {
6406         tree expression;
6407
6408         /* Begin the do-statement.  */
6409         statement = begin_do_stmt ();
6410         /* Parse the body of the do-statement.  */
6411         parser->in_iteration_statement_p = true;
6412         cp_parser_implicitly_scoped_statement (parser);
6413         parser->in_iteration_statement_p = in_iteration_statement_p;
6414         finish_do_body (statement);
6415         /* Look for the `while' keyword.  */
6416         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6417         /* Look for the `('.  */
6418         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6419         /* Parse the expression.  */
6420         expression = cp_parser_expression (parser);
6421         /* We're done with the do-statement.  */
6422         finish_do_stmt (expression, statement);
6423         /* Look for the `)'.  */
6424         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6425         /* Look for the `;'.  */
6426         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6427       }
6428       break;
6429
6430     case RID_FOR:
6431       {
6432         tree condition = NULL_TREE;
6433         tree expression = NULL_TREE;
6434
6435         /* Begin the for-statement.  */
6436         statement = begin_for_stmt ();
6437         /* Look for the `('.  */
6438         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6439         /* Parse the initialization.  */
6440         cp_parser_for_init_statement (parser);
6441         finish_for_init_stmt (statement);
6442
6443         /* If there's a condition, process it.  */
6444         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6445           condition = cp_parser_condition (parser);
6446         finish_for_cond (condition, statement);
6447         /* Look for the `;'.  */
6448         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6449
6450         /* If there's an expression, process it.  */
6451         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6452           expression = cp_parser_expression (parser);
6453         finish_for_expr (expression, statement);
6454         /* Look for the `)'.  */
6455         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6456
6457         /* Parse the body of the for-statement.  */
6458         parser->in_iteration_statement_p = true;
6459         cp_parser_already_scoped_statement (parser);
6460         parser->in_iteration_statement_p = in_iteration_statement_p;
6461
6462         /* We're done with the for-statement.  */
6463         finish_for_stmt (statement);
6464       }
6465       break;
6466
6467     default:
6468       cp_parser_error (parser, "expected iteration-statement");
6469       statement = error_mark_node;
6470       break;
6471     }
6472
6473   return statement;
6474 }
6475
6476 /* Parse a for-init-statement.
6477
6478    for-init-statement:
6479      expression-statement
6480      simple-declaration  */
6481
6482 static void
6483 cp_parser_for_init_statement (cp_parser* parser)
6484 {
6485   /* If the next token is a `;', then we have an empty
6486      expression-statement.  Grammatically, this is also a
6487      simple-declaration, but an invalid one, because it does not
6488      declare anything.  Therefore, if we did not handle this case
6489      specially, we would issue an error message about an invalid
6490      declaration.  */
6491   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6492     {
6493       /* We're going to speculatively look for a declaration, falling back
6494          to an expression, if necessary.  */
6495       cp_parser_parse_tentatively (parser);
6496       /* Parse the declaration.  */
6497       cp_parser_simple_declaration (parser,
6498                                     /*function_definition_allowed_p=*/false);
6499       /* If the tentative parse failed, then we shall need to look for an
6500          expression-statement.  */
6501       if (cp_parser_parse_definitely (parser))
6502         return;
6503     }
6504
6505   cp_parser_expression_statement (parser, false);
6506 }
6507
6508 /* Parse a jump-statement.
6509
6510    jump-statement:
6511      break ;
6512      continue ;
6513      return expression [opt] ;
6514      goto identifier ;
6515
6516    GNU extension:
6517
6518    jump-statement:
6519      goto * expression ;
6520
6521    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6522
6523 static tree
6524 cp_parser_jump_statement (cp_parser* parser)
6525 {
6526   tree statement = error_mark_node;
6527   cp_token *token;
6528   enum rid keyword;
6529
6530   /* Peek at the next token.  */
6531   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6532   if (!token)
6533     return error_mark_node;
6534
6535   /* See what kind of keyword it is.  */
6536   keyword = token->keyword;
6537   switch (keyword)
6538     {
6539     case RID_BREAK:
6540       if (!parser->in_switch_statement_p
6541           && !parser->in_iteration_statement_p)
6542         {
6543           error ("break statement not within loop or switch");
6544           statement = error_mark_node;
6545         }
6546       else
6547         statement = finish_break_stmt ();
6548       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6549       break;
6550
6551     case RID_CONTINUE:
6552       if (!parser->in_iteration_statement_p)
6553         {
6554           error ("continue statement not within a loop");
6555           statement = error_mark_node;
6556         }
6557       else
6558         statement = finish_continue_stmt ();
6559       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6560       break;
6561
6562     case RID_RETURN:
6563       {
6564         tree expr;
6565
6566         /* If the next token is a `;', then there is no
6567            expression.  */
6568         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6569           expr = cp_parser_expression (parser);
6570         else
6571           expr = NULL_TREE;
6572         /* Build the return-statement.  */
6573         statement = finish_return_stmt (expr);
6574         /* Look for the final `;'.  */
6575         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6576       }
6577       break;
6578
6579     case RID_GOTO:
6580       /* Create the goto-statement.  */
6581       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6582         {
6583           /* Issue a warning about this use of a GNU extension.  */
6584           if (pedantic)
6585             pedwarn ("ISO C++ forbids computed gotos");
6586           /* Consume the '*' token.  */
6587           cp_lexer_consume_token (parser->lexer);
6588           /* Parse the dependent expression.  */
6589           finish_goto_stmt (cp_parser_expression (parser));
6590         }
6591       else
6592         finish_goto_stmt (cp_parser_identifier (parser));
6593       /* Look for the final `;'.  */
6594       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6595       break;
6596
6597     default:
6598       cp_parser_error (parser, "expected jump-statement");
6599       break;
6600     }
6601
6602   return statement;
6603 }
6604
6605 /* Parse a declaration-statement.
6606
6607    declaration-statement:
6608      block-declaration  */
6609
6610 static void
6611 cp_parser_declaration_statement (cp_parser* parser)
6612 {
6613   void *p;
6614
6615   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6616   p = obstack_alloc (&declarator_obstack, 0);
6617
6618  /* Parse the block-declaration.  */
6619   cp_parser_block_declaration (parser, /*statement_p=*/true);
6620
6621   /* Free any declarators allocated.  */
6622   obstack_free (&declarator_obstack, p);
6623
6624   /* Finish off the statement.  */
6625   finish_stmt ();
6626 }
6627
6628 /* Some dependent statements (like `if (cond) statement'), are
6629    implicitly in their own scope.  In other words, if the statement is
6630    a single statement (as opposed to a compound-statement), it is
6631    none-the-less treated as if it were enclosed in braces.  Any
6632    declarations appearing in the dependent statement are out of scope
6633    after control passes that point.  This function parses a statement,
6634    but ensures that is in its own scope, even if it is not a
6635    compound-statement.
6636
6637    Returns the new statement.  */
6638
6639 static tree
6640 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6641 {
6642   tree statement;
6643
6644   /* If the token is not a `{', then we must take special action.  */
6645   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6646     {
6647       /* Create a compound-statement.  */
6648       statement = begin_compound_stmt (0);
6649       /* Parse the dependent-statement.  */
6650       cp_parser_statement (parser, false);
6651       /* Finish the dummy compound-statement.  */
6652       finish_compound_stmt (statement);
6653     }
6654   /* Otherwise, we simply parse the statement directly.  */
6655   else
6656     statement = cp_parser_compound_statement (parser, NULL, false);
6657
6658   /* Return the statement.  */
6659   return statement;
6660 }
6661
6662 /* For some dependent statements (like `while (cond) statement'), we
6663    have already created a scope.  Therefore, even if the dependent
6664    statement is a compound-statement, we do not want to create another
6665    scope.  */
6666
6667 static void
6668 cp_parser_already_scoped_statement (cp_parser* parser)
6669 {
6670   /* If the token is a `{', then we must take special action.  */
6671   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6672     cp_parser_statement (parser, false);
6673   else
6674     {
6675       /* Avoid calling cp_parser_compound_statement, so that we
6676          don't create a new scope.  Do everything else by hand.  */
6677       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6678       cp_parser_statement_seq_opt (parser, false);
6679       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6680     }
6681 }
6682
6683 /* Declarations [gram.dcl.dcl] */
6684
6685 /* Parse an optional declaration-sequence.
6686
6687    declaration-seq:
6688      declaration
6689      declaration-seq declaration  */
6690
6691 static void
6692 cp_parser_declaration_seq_opt (cp_parser* parser)
6693 {
6694   while (true)
6695     {
6696       cp_token *token;
6697
6698       token = cp_lexer_peek_token (parser->lexer);
6699
6700       if (token->type == CPP_CLOSE_BRACE
6701           || token->type == CPP_EOF)
6702         break;
6703
6704       if (token->type == CPP_SEMICOLON)
6705         {
6706           /* A declaration consisting of a single semicolon is
6707              invalid.  Allow it unless we're being pedantic.  */
6708           if (pedantic && !in_system_header)
6709             pedwarn ("extra `;'");
6710           cp_lexer_consume_token (parser->lexer);
6711           continue;
6712         }
6713
6714       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6715          parser to enter or exit implicit `extern "C"' blocks.  */
6716       while (pending_lang_change > 0)
6717         {
6718           push_lang_context (lang_name_c);
6719           --pending_lang_change;
6720         }
6721       while (pending_lang_change < 0)
6722         {
6723           pop_lang_context ();
6724           ++pending_lang_change;
6725         }
6726
6727       /* Parse the declaration itself.  */
6728       cp_parser_declaration (parser);
6729     }
6730 }
6731
6732 /* Parse a declaration.
6733
6734    declaration:
6735      block-declaration
6736      function-definition
6737      template-declaration
6738      explicit-instantiation
6739      explicit-specialization
6740      linkage-specification
6741      namespace-definition
6742
6743    GNU extension:
6744
6745    declaration:
6746       __extension__ declaration */
6747
6748 static void
6749 cp_parser_declaration (cp_parser* parser)
6750 {
6751   cp_token token1;
6752   cp_token token2;
6753   int saved_pedantic;
6754   void *p;
6755
6756   /* Set this here since we can be called after
6757      pushing the linkage specification.  */
6758   c_lex_string_translate = 1;
6759
6760   /* Check for the `__extension__' keyword.  */
6761   if (cp_parser_extension_opt (parser, &saved_pedantic))
6762     {
6763       /* Parse the qualified declaration.  */
6764       cp_parser_declaration (parser);
6765       /* Restore the PEDANTIC flag.  */
6766       pedantic = saved_pedantic;
6767
6768       return;
6769     }
6770
6771   /* Try to figure out what kind of declaration is present.  */
6772   token1 = *cp_lexer_peek_token (parser->lexer);
6773
6774   /* Don't translate the CPP_STRING in extern "C".  */
6775   if (token1.keyword == RID_EXTERN)
6776     c_lex_string_translate = 0;
6777
6778   if (token1.type != CPP_EOF)
6779     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6780
6781   c_lex_string_translate = 1;
6782
6783   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6784   p = obstack_alloc (&declarator_obstack, 0);
6785
6786   /* If the next token is `extern' and the following token is a string
6787      literal, then we have a linkage specification.  */
6788   if (token1.keyword == RID_EXTERN
6789       && cp_parser_is_string_literal (&token2))
6790     cp_parser_linkage_specification (parser);
6791   /* If the next token is `template', then we have either a template
6792      declaration, an explicit instantiation, or an explicit
6793      specialization.  */
6794   else if (token1.keyword == RID_TEMPLATE)
6795     {
6796       /* `template <>' indicates a template specialization.  */
6797       if (token2.type == CPP_LESS
6798           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6799         cp_parser_explicit_specialization (parser);
6800       /* `template <' indicates a template declaration.  */
6801       else if (token2.type == CPP_LESS)
6802         cp_parser_template_declaration (parser, /*member_p=*/false);
6803       /* Anything else must be an explicit instantiation.  */
6804       else
6805         cp_parser_explicit_instantiation (parser);
6806     }
6807   /* If the next token is `export', then we have a template
6808      declaration.  */
6809   else if (token1.keyword == RID_EXPORT)
6810     cp_parser_template_declaration (parser, /*member_p=*/false);
6811   /* If the next token is `extern', 'static' or 'inline' and the one
6812      after that is `template', we have a GNU extended explicit
6813      instantiation directive.  */
6814   else if (cp_parser_allow_gnu_extensions_p (parser)
6815            && (token1.keyword == RID_EXTERN
6816                || token1.keyword == RID_STATIC
6817                || token1.keyword == RID_INLINE)
6818            && token2.keyword == RID_TEMPLATE)
6819     cp_parser_explicit_instantiation (parser);
6820   /* If the next token is `namespace', check for a named or unnamed
6821      namespace definition.  */
6822   else if (token1.keyword == RID_NAMESPACE
6823            && (/* A named namespace definition.  */
6824                (token2.type == CPP_NAME
6825                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6826                     == CPP_OPEN_BRACE))
6827                /* An unnamed namespace definition.  */
6828                || token2.type == CPP_OPEN_BRACE))
6829     cp_parser_namespace_definition (parser);
6830   /* We must have either a block declaration or a function
6831      definition.  */
6832   else
6833     /* Try to parse a block-declaration, or a function-definition.  */
6834     cp_parser_block_declaration (parser, /*statement_p=*/false);
6835
6836   /* Free any declarators allocated.  */
6837   obstack_free (&declarator_obstack, p);
6838 }
6839
6840 /* Parse a block-declaration.
6841
6842    block-declaration:
6843      simple-declaration
6844      asm-definition
6845      namespace-alias-definition
6846      using-declaration
6847      using-directive
6848
6849    GNU Extension:
6850
6851    block-declaration:
6852      __extension__ block-declaration
6853      label-declaration
6854
6855    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6856    part of a declaration-statement.  */
6857
6858 static void
6859 cp_parser_block_declaration (cp_parser *parser,
6860                              bool      statement_p)
6861 {
6862   cp_token *token1;
6863   int saved_pedantic;
6864
6865   /* Check for the `__extension__' keyword.  */
6866   if (cp_parser_extension_opt (parser, &saved_pedantic))
6867     {
6868       /* Parse the qualified declaration.  */
6869       cp_parser_block_declaration (parser, statement_p);
6870       /* Restore the PEDANTIC flag.  */
6871       pedantic = saved_pedantic;
6872
6873       return;
6874     }
6875
6876   /* Peek at the next token to figure out which kind of declaration is
6877      present.  */
6878   token1 = cp_lexer_peek_token (parser->lexer);
6879
6880   /* If the next keyword is `asm', we have an asm-definition.  */
6881   if (token1->keyword == RID_ASM)
6882     {
6883       if (statement_p)
6884         cp_parser_commit_to_tentative_parse (parser);
6885       cp_parser_asm_definition (parser);
6886     }
6887   /* If the next keyword is `namespace', we have a
6888      namespace-alias-definition.  */
6889   else if (token1->keyword == RID_NAMESPACE)
6890     cp_parser_namespace_alias_definition (parser);
6891   /* If the next keyword is `using', we have either a
6892      using-declaration or a using-directive.  */
6893   else if (token1->keyword == RID_USING)
6894     {
6895       cp_token *token2;
6896
6897       if (statement_p)
6898         cp_parser_commit_to_tentative_parse (parser);
6899       /* If the token after `using' is `namespace', then we have a
6900          using-directive.  */
6901       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6902       if (token2->keyword == RID_NAMESPACE)
6903         cp_parser_using_directive (parser);
6904       /* Otherwise, it's a using-declaration.  */
6905       else
6906         cp_parser_using_declaration (parser);
6907     }
6908   /* If the next keyword is `__label__' we have a label declaration.  */
6909   else if (token1->keyword == RID_LABEL)
6910     {
6911       if (statement_p)
6912         cp_parser_commit_to_tentative_parse (parser);
6913       cp_parser_label_declaration (parser);
6914     }
6915   /* Anything else must be a simple-declaration.  */
6916   else
6917     cp_parser_simple_declaration (parser, !statement_p);
6918 }
6919
6920 /* Parse a simple-declaration.
6921
6922    simple-declaration:
6923      decl-specifier-seq [opt] init-declarator-list [opt] ;
6924
6925    init-declarator-list:
6926      init-declarator
6927      init-declarator-list , init-declarator
6928
6929    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6930    function-definition as a simple-declaration.  */
6931
6932 static void
6933 cp_parser_simple_declaration (cp_parser* parser,
6934                               bool function_definition_allowed_p)
6935 {
6936   cp_decl_specifier_seq decl_specifiers;
6937   int declares_class_or_enum;
6938   bool saw_declarator;
6939
6940   /* Defer access checks until we know what is being declared; the
6941      checks for names appearing in the decl-specifier-seq should be
6942      done as if we were in the scope of the thing being declared.  */
6943   push_deferring_access_checks (dk_deferred);
6944
6945   /* Parse the decl-specifier-seq.  We have to keep track of whether
6946      or not the decl-specifier-seq declares a named class or
6947      enumeration type, since that is the only case in which the
6948      init-declarator-list is allowed to be empty.
6949
6950      [dcl.dcl]
6951
6952      In a simple-declaration, the optional init-declarator-list can be
6953      omitted only when declaring a class or enumeration, that is when
6954      the decl-specifier-seq contains either a class-specifier, an
6955      elaborated-type-specifier, or an enum-specifier.  */
6956   cp_parser_decl_specifier_seq (parser,
6957                                 CP_PARSER_FLAGS_OPTIONAL,
6958                                 &decl_specifiers,
6959                                 &declares_class_or_enum);
6960   /* We no longer need to defer access checks.  */
6961   stop_deferring_access_checks ();
6962
6963   /* In a block scope, a valid declaration must always have a
6964      decl-specifier-seq.  By not trying to parse declarators, we can
6965      resolve the declaration/expression ambiguity more quickly.  */
6966   if (!function_definition_allowed_p
6967       && !decl_specifiers.any_specifiers_p)
6968     {
6969       cp_parser_error (parser, "expected declaration");
6970       goto done;
6971     }
6972
6973   /* If the next two tokens are both identifiers, the code is
6974      erroneous. The usual cause of this situation is code like:
6975
6976        T t;
6977
6978      where "T" should name a type -- but does not.  */
6979   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6980     {
6981       /* If parsing tentatively, we should commit; we really are
6982          looking at a declaration.  */
6983       cp_parser_commit_to_tentative_parse (parser);
6984       /* Give up.  */
6985       goto done;
6986     }
6987
6988   /* Keep going until we hit the `;' at the end of the simple
6989      declaration.  */
6990   saw_declarator = false;
6991   while (cp_lexer_next_token_is_not (parser->lexer,
6992                                      CPP_SEMICOLON))
6993     {
6994       cp_token *token;
6995       bool function_definition_p;
6996       tree decl;
6997
6998       saw_declarator = true;
6999       /* Parse the init-declarator.  */
7000       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7001                                         function_definition_allowed_p,
7002                                         /*member_p=*/false,
7003                                         declares_class_or_enum,
7004                                         &function_definition_p);
7005       /* If an error occurred while parsing tentatively, exit quickly.
7006          (That usually happens when in the body of a function; each
7007          statement is treated as a declaration-statement until proven
7008          otherwise.)  */
7009       if (cp_parser_error_occurred (parser))
7010         goto done;
7011       /* Handle function definitions specially.  */
7012       if (function_definition_p)
7013         {
7014           /* If the next token is a `,', then we are probably
7015              processing something like:
7016
7017                void f() {}, *p;
7018
7019              which is erroneous.  */
7020           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7021             error ("mixing declarations and function-definitions is forbidden");
7022           /* Otherwise, we're done with the list of declarators.  */
7023           else
7024             {
7025               pop_deferring_access_checks ();
7026               return;
7027             }
7028         }
7029       /* The next token should be either a `,' or a `;'.  */
7030       token = cp_lexer_peek_token (parser->lexer);
7031       /* If it's a `,', there are more declarators to come.  */
7032       if (token->type == CPP_COMMA)
7033         cp_lexer_consume_token (parser->lexer);
7034       /* If it's a `;', we are done.  */
7035       else if (token->type == CPP_SEMICOLON)
7036         break;
7037       /* Anything else is an error.  */
7038       else
7039         {
7040           cp_parser_error (parser, "expected `,' or `;'");
7041           /* Skip tokens until we reach the end of the statement.  */
7042           cp_parser_skip_to_end_of_statement (parser);
7043           /* If the next token is now a `;', consume it.  */
7044           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7045             cp_lexer_consume_token (parser->lexer);
7046           goto done;
7047         }
7048       /* After the first time around, a function-definition is not
7049          allowed -- even if it was OK at first.  For example:
7050
7051            int i, f() {}
7052
7053          is not valid.  */
7054       function_definition_allowed_p = false;
7055     }
7056
7057   /* Issue an error message if no declarators are present, and the
7058      decl-specifier-seq does not itself declare a class or
7059      enumeration.  */
7060   if (!saw_declarator)
7061     {
7062       if (cp_parser_declares_only_class_p (parser))
7063         shadow_tag (&decl_specifiers);
7064       /* Perform any deferred access checks.  */
7065       perform_deferred_access_checks ();
7066     }
7067
7068   /* Consume the `;'.  */
7069   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7070
7071  done:
7072   pop_deferring_access_checks ();
7073 }
7074
7075 /* Parse a decl-specifier-seq.
7076
7077    decl-specifier-seq:
7078      decl-specifier-seq [opt] decl-specifier
7079
7080    decl-specifier:
7081      storage-class-specifier
7082      type-specifier
7083      function-specifier
7084      friend
7085      typedef
7086
7087    GNU Extension:
7088
7089    decl-specifier:
7090      attributes
7091
7092    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7093
7094    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7095    appears, and the entity that will be a friend is not going to be a
7096    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
7097    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
7098    friendship is granted might not be a class.
7099
7100    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7101    flags:
7102
7103      1: one of the decl-specifiers is an elaborated-type-specifier
7104         (i.e., a type declaration)
7105      2: one of the decl-specifiers is an enum-specifier or a
7106         class-specifier (i.e., a type definition)
7107
7108    */
7109
7110 static void
7111 cp_parser_decl_specifier_seq (cp_parser* parser,
7112                               cp_parser_flags flags,
7113                               cp_decl_specifier_seq *decl_specs,
7114                               int* declares_class_or_enum)
7115 {
7116   bool constructor_possible_p = !parser->in_declarator_p;
7117
7118   /* Clear DECL_SPECS.  */
7119   clear_decl_specs (decl_specs);
7120
7121   /* Assume no class or enumeration type is declared.  */
7122   *declares_class_or_enum = 0;
7123
7124   /* Keep reading specifiers until there are no more to read.  */
7125   while (true)
7126     {
7127       bool constructor_p;
7128       bool found_decl_spec;
7129       cp_token *token;
7130
7131       /* Peek at the next token.  */
7132       token = cp_lexer_peek_token (parser->lexer);
7133       /* Handle attributes.  */
7134       if (token->keyword == RID_ATTRIBUTE)
7135         {
7136           /* Parse the attributes.  */
7137           decl_specs->attributes
7138             = chainon (decl_specs->attributes,
7139                        cp_parser_attributes_opt (parser));
7140           continue;
7141         }
7142       /* Assume we will find a decl-specifier keyword.  */
7143       found_decl_spec = true;
7144       /* If the next token is an appropriate keyword, we can simply
7145          add it to the list.  */
7146       switch (token->keyword)
7147         {
7148           /* decl-specifier:
7149                friend  */
7150         case RID_FRIEND:
7151           if (decl_specs->specs[(int) ds_friend]++)
7152             error ("duplicate `friend'");
7153           /* Consume the token.  */
7154           cp_lexer_consume_token (parser->lexer);
7155           break;
7156
7157           /* function-specifier:
7158                inline
7159                virtual
7160                explicit  */
7161         case RID_INLINE:
7162         case RID_VIRTUAL:
7163         case RID_EXPLICIT:
7164           cp_parser_function_specifier_opt (parser, decl_specs);
7165           break;
7166
7167           /* decl-specifier:
7168                typedef  */
7169         case RID_TYPEDEF:
7170           ++decl_specs->specs[(int) ds_typedef];
7171           /* Consume the token.  */
7172           cp_lexer_consume_token (parser->lexer);
7173           /* A constructor declarator cannot appear in a typedef.  */
7174           constructor_possible_p = false;
7175           /* The "typedef" keyword can only occur in a declaration; we
7176              may as well commit at this point.  */
7177           cp_parser_commit_to_tentative_parse (parser);
7178           break;
7179
7180           /* storage-class-specifier:
7181                auto
7182                register
7183                static
7184                extern
7185                mutable
7186
7187              GNU Extension:
7188                thread  */
7189         case RID_AUTO:
7190           /* Consume the token.  */
7191           cp_lexer_consume_token (parser->lexer);
7192           cp_parser_set_storage_class (decl_specs, sc_auto);
7193           break;
7194         case RID_REGISTER:
7195           /* Consume the token.  */
7196           cp_lexer_consume_token (parser->lexer);
7197           cp_parser_set_storage_class (decl_specs, sc_register);
7198           break;
7199         case RID_STATIC:
7200           /* Consume the token.  */
7201           cp_lexer_consume_token (parser->lexer);
7202           if (decl_specs->specs[(int) ds_thread])
7203             {
7204               error ("`__thread' before `static'");
7205               decl_specs->specs[(int) ds_thread] = 0;
7206             }
7207           cp_parser_set_storage_class (decl_specs, sc_static);
7208           break;
7209         case RID_EXTERN:
7210           /* Consume the token.  */
7211           cp_lexer_consume_token (parser->lexer);
7212           if (decl_specs->specs[(int) ds_thread])
7213             {
7214               error ("`__thread' before `extern'");
7215               decl_specs->specs[(int) ds_thread] = 0;
7216             }
7217           cp_parser_set_storage_class (decl_specs, sc_extern);
7218           break;
7219         case RID_MUTABLE:
7220           /* Consume the token.  */
7221           cp_lexer_consume_token (parser->lexer);
7222           cp_parser_set_storage_class (decl_specs, sc_mutable);
7223           break;
7224         case RID_THREAD:
7225           /* Consume the token.  */
7226           cp_lexer_consume_token (parser->lexer);
7227           ++decl_specs->specs[(int) ds_thread];
7228           break;
7229
7230         default:
7231           /* We did not yet find a decl-specifier yet.  */
7232           found_decl_spec = false;
7233           break;
7234         }
7235
7236       /* Constructors are a special case.  The `S' in `S()' is not a
7237          decl-specifier; it is the beginning of the declarator.  */
7238       constructor_p
7239         = (!found_decl_spec
7240            && constructor_possible_p
7241            && (cp_parser_constructor_declarator_p
7242                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7243
7244       /* If we don't have a DECL_SPEC yet, then we must be looking at
7245          a type-specifier.  */
7246       if (!found_decl_spec && !constructor_p)
7247         {
7248           int decl_spec_declares_class_or_enum;
7249           bool is_cv_qualifier;
7250           tree type_spec;
7251
7252           type_spec
7253             = cp_parser_type_specifier (parser, flags,
7254                                         decl_specs,
7255                                         /*is_declaration=*/true,
7256                                         &decl_spec_declares_class_or_enum,
7257                                         &is_cv_qualifier);
7258
7259           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7260
7261           /* If this type-specifier referenced a user-defined type
7262              (a typedef, class-name, etc.), then we can't allow any
7263              more such type-specifiers henceforth.
7264
7265              [dcl.spec]
7266
7267              The longest sequence of decl-specifiers that could
7268              possibly be a type name is taken as the
7269              decl-specifier-seq of a declaration.  The sequence shall
7270              be self-consistent as described below.
7271
7272              [dcl.type]
7273
7274              As a general rule, at most one type-specifier is allowed
7275              in the complete decl-specifier-seq of a declaration.  The
7276              only exceptions are the following:
7277
7278              -- const or volatile can be combined with any other
7279                 type-specifier.
7280
7281              -- signed or unsigned can be combined with char, long,
7282                 short, or int.
7283
7284              -- ..
7285
7286              Example:
7287
7288                typedef char* Pc;
7289                void g (const int Pc);
7290
7291              Here, Pc is *not* part of the decl-specifier seq; it's
7292              the declarator.  Therefore, once we see a type-specifier
7293              (other than a cv-qualifier), we forbid any additional
7294              user-defined types.  We *do* still allow things like `int
7295              int' to be considered a decl-specifier-seq, and issue the
7296              error message later.  */
7297           if (type_spec && !is_cv_qualifier)
7298             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7299           /* A constructor declarator cannot follow a type-specifier.  */
7300           if (type_spec)
7301             {
7302               constructor_possible_p = false;
7303               found_decl_spec = true;
7304             }
7305         }
7306
7307       /* If we still do not have a DECL_SPEC, then there are no more
7308          decl-specifiers.  */
7309       if (!found_decl_spec)
7310         break;
7311
7312       decl_specs->any_specifiers_p = true;
7313       /* After we see one decl-specifier, further decl-specifiers are
7314          always optional.  */
7315       flags |= CP_PARSER_FLAGS_OPTIONAL;
7316     }
7317
7318   /* Don't allow a friend specifier with a class definition.  */
7319   if (decl_specs->specs[(int) ds_friend] != 0
7320       && (*declares_class_or_enum & 2))
7321     error ("class definition may not be declared a friend");
7322 }
7323
7324 /* Parse an (optional) storage-class-specifier.
7325
7326    storage-class-specifier:
7327      auto
7328      register
7329      static
7330      extern
7331      mutable
7332
7333    GNU Extension:
7334
7335    storage-class-specifier:
7336      thread
7337
7338    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7339
7340 static tree
7341 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7342 {
7343   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7344     {
7345     case RID_AUTO:
7346     case RID_REGISTER:
7347     case RID_STATIC:
7348     case RID_EXTERN:
7349     case RID_MUTABLE:
7350     case RID_THREAD:
7351       /* Consume the token.  */
7352       return cp_lexer_consume_token (parser->lexer)->value;
7353
7354     default:
7355       return NULL_TREE;
7356     }
7357 }
7358
7359 /* Parse an (optional) function-specifier.
7360
7361    function-specifier:
7362      inline
7363      virtual
7364      explicit
7365
7366    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7367    Updates DECL_SPECS, if it is non-NULL.  */
7368
7369 static tree
7370 cp_parser_function_specifier_opt (cp_parser* parser,
7371                                   cp_decl_specifier_seq *decl_specs)
7372 {
7373   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7374     {
7375     case RID_INLINE:
7376       if (decl_specs)
7377         ++decl_specs->specs[(int) ds_inline];
7378       break;
7379
7380     case RID_VIRTUAL:
7381       if (decl_specs)
7382         ++decl_specs->specs[(int) ds_virtual];
7383       break;
7384
7385     case RID_EXPLICIT:
7386       if (decl_specs)
7387         ++decl_specs->specs[(int) ds_explicit];
7388       break;
7389
7390     default:
7391       return NULL_TREE;
7392     }
7393
7394   /* Consume the token.  */
7395   return cp_lexer_consume_token (parser->lexer)->value;
7396 }
7397
7398 /* Parse a linkage-specification.
7399
7400    linkage-specification:
7401      extern string-literal { declaration-seq [opt] }
7402      extern string-literal declaration  */
7403
7404 static void
7405 cp_parser_linkage_specification (cp_parser* parser)
7406 {
7407   cp_token *token;
7408   tree linkage;
7409
7410   /* Look for the `extern' keyword.  */
7411   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7412
7413   /* Peek at the next token.  */
7414   token = cp_lexer_peek_token (parser->lexer);
7415   /* If it's not a string-literal, then there's a problem.  */
7416   if (!cp_parser_is_string_literal (token))
7417     {
7418       cp_parser_error (parser, "expected language-name");
7419       return;
7420     }
7421   /* Consume the token.  */
7422   cp_lexer_consume_token (parser->lexer);
7423
7424   /* Transform the literal into an identifier.  If the literal is a
7425      wide-character string, or contains embedded NULs, then we can't
7426      handle it as the user wants.  */
7427   if (token->type == CPP_WSTRING
7428       || (strlen (TREE_STRING_POINTER (token->value))
7429           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7430     {
7431       cp_parser_error (parser, "invalid linkage-specification");
7432       /* Assume C++ linkage.  */
7433       linkage = get_identifier ("c++");
7434     }
7435   /* If the string is chained to another string, take the latter,
7436      that's the untranslated string.  */
7437   else if (TREE_CHAIN (token->value))
7438     linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7439   /* If it's a simple string constant, things are easier.  */
7440   else
7441     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7442
7443   /* We're now using the new linkage.  */
7444   push_lang_context (linkage);
7445
7446   /* If the next token is a `{', then we're using the first
7447      production.  */
7448   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7449     {
7450       /* Consume the `{' token.  */
7451       cp_lexer_consume_token (parser->lexer);
7452       /* Parse the declarations.  */
7453       cp_parser_declaration_seq_opt (parser);
7454       /* Look for the closing `}'.  */
7455       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7456     }
7457   /* Otherwise, there's just one declaration.  */
7458   else
7459     {
7460       bool saved_in_unbraced_linkage_specification_p;
7461
7462       saved_in_unbraced_linkage_specification_p
7463         = parser->in_unbraced_linkage_specification_p;
7464       parser->in_unbraced_linkage_specification_p = true;
7465       have_extern_spec = true;
7466       cp_parser_declaration (parser);
7467       have_extern_spec = false;
7468       parser->in_unbraced_linkage_specification_p
7469         = saved_in_unbraced_linkage_specification_p;
7470     }
7471
7472   /* We're done with the linkage-specification.  */
7473   pop_lang_context ();
7474 }
7475
7476 /* Special member functions [gram.special] */
7477
7478 /* Parse a conversion-function-id.
7479
7480    conversion-function-id:
7481      operator conversion-type-id
7482
7483    Returns an IDENTIFIER_NODE representing the operator.  */
7484
7485 static tree
7486 cp_parser_conversion_function_id (cp_parser* parser)
7487 {
7488   tree type;
7489   tree saved_scope;
7490   tree saved_qualifying_scope;
7491   tree saved_object_scope;
7492   bool pop_p = false;
7493
7494   /* Look for the `operator' token.  */
7495   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7496     return error_mark_node;
7497   /* When we parse the conversion-type-id, the current scope will be
7498      reset.  However, we need that information in able to look up the
7499      conversion function later, so we save it here.  */
7500   saved_scope = parser->scope;
7501   saved_qualifying_scope = parser->qualifying_scope;
7502   saved_object_scope = parser->object_scope;
7503   /* We must enter the scope of the class so that the names of
7504      entities declared within the class are available in the
7505      conversion-type-id.  For example, consider:
7506
7507        struct S {
7508          typedef int I;
7509          operator I();
7510        };
7511
7512        S::operator I() { ... }
7513
7514      In order to see that `I' is a type-name in the definition, we
7515      must be in the scope of `S'.  */
7516   if (saved_scope)
7517     pop_p = push_scope (saved_scope);
7518   /* Parse the conversion-type-id.  */
7519   type = cp_parser_conversion_type_id (parser);
7520   /* Leave the scope of the class, if any.  */
7521   if (pop_p)
7522     pop_scope (saved_scope);
7523   /* Restore the saved scope.  */
7524   parser->scope = saved_scope;
7525   parser->qualifying_scope = saved_qualifying_scope;
7526   parser->object_scope = saved_object_scope;
7527   /* If the TYPE is invalid, indicate failure.  */
7528   if (type == error_mark_node)
7529     return error_mark_node;
7530   return mangle_conv_op_name_for_type (type);
7531 }
7532
7533 /* Parse a conversion-type-id:
7534
7535    conversion-type-id:
7536      type-specifier-seq conversion-declarator [opt]
7537
7538    Returns the TYPE specified.  */
7539
7540 static tree
7541 cp_parser_conversion_type_id (cp_parser* parser)
7542 {
7543   tree attributes;
7544   cp_decl_specifier_seq type_specifiers;
7545   cp_declarator *declarator;
7546
7547   /* Parse the attributes.  */
7548   attributes = cp_parser_attributes_opt (parser);
7549   /* Parse the type-specifiers.  */
7550   cp_parser_type_specifier_seq (parser, &type_specifiers);
7551   /* If that didn't work, stop.  */
7552   if (type_specifiers.type == error_mark_node)
7553     return error_mark_node;
7554   /* Parse the conversion-declarator.  */
7555   declarator = cp_parser_conversion_declarator_opt (parser);
7556
7557   return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7558                          /*initialized=*/0, &attributes);
7559 }
7560
7561 /* Parse an (optional) conversion-declarator.
7562
7563    conversion-declarator:
7564      ptr-operator conversion-declarator [opt]
7565
7566    */
7567
7568 static cp_declarator *
7569 cp_parser_conversion_declarator_opt (cp_parser* parser)
7570 {
7571   enum tree_code code;
7572   tree class_type;
7573   cp_cv_quals cv_quals;
7574
7575   /* We don't know if there's a ptr-operator next, or not.  */
7576   cp_parser_parse_tentatively (parser);
7577   /* Try the ptr-operator.  */
7578   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7579   /* If it worked, look for more conversion-declarators.  */
7580   if (cp_parser_parse_definitely (parser))
7581     {
7582       cp_declarator *declarator;
7583
7584       /* Parse another optional declarator.  */
7585       declarator = cp_parser_conversion_declarator_opt (parser);
7586
7587       /* Create the representation of the declarator.  */
7588       if (class_type)
7589         declarator = make_ptrmem_declarator (cv_quals, class_type,
7590                                              declarator);
7591       else if (code == INDIRECT_REF)
7592         declarator = make_pointer_declarator (cv_quals, declarator);
7593       else
7594         declarator = make_reference_declarator (cv_quals, declarator);
7595
7596       return declarator;
7597    }
7598
7599   return NULL;
7600 }
7601
7602 /* Parse an (optional) ctor-initializer.
7603
7604    ctor-initializer:
7605      : mem-initializer-list
7606
7607    Returns TRUE iff the ctor-initializer was actually present.  */
7608
7609 static bool
7610 cp_parser_ctor_initializer_opt (cp_parser* parser)
7611 {
7612   /* If the next token is not a `:', then there is no
7613      ctor-initializer.  */
7614   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7615     {
7616       /* Do default initialization of any bases and members.  */
7617       if (DECL_CONSTRUCTOR_P (current_function_decl))
7618         finish_mem_initializers (NULL_TREE);
7619
7620       return false;
7621     }
7622
7623   /* Consume the `:' token.  */
7624   cp_lexer_consume_token (parser->lexer);
7625   /* And the mem-initializer-list.  */
7626   cp_parser_mem_initializer_list (parser);
7627
7628   return true;
7629 }
7630
7631 /* Parse a mem-initializer-list.
7632
7633    mem-initializer-list:
7634      mem-initializer
7635      mem-initializer , mem-initializer-list  */
7636
7637 static void
7638 cp_parser_mem_initializer_list (cp_parser* parser)
7639 {
7640   tree mem_initializer_list = NULL_TREE;
7641
7642   /* Let the semantic analysis code know that we are starting the
7643      mem-initializer-list.  */
7644   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7645     error ("only constructors take base initializers");
7646
7647   /* Loop through the list.  */
7648   while (true)
7649     {
7650       tree mem_initializer;
7651
7652       /* Parse the mem-initializer.  */
7653       mem_initializer = cp_parser_mem_initializer (parser);
7654       /* Add it to the list, unless it was erroneous.  */
7655       if (mem_initializer)
7656         {
7657           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7658           mem_initializer_list = mem_initializer;
7659         }
7660       /* If the next token is not a `,', we're done.  */
7661       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7662         break;
7663       /* Consume the `,' token.  */
7664       cp_lexer_consume_token (parser->lexer);
7665     }
7666
7667   /* Perform semantic analysis.  */
7668   if (DECL_CONSTRUCTOR_P (current_function_decl))
7669     finish_mem_initializers (mem_initializer_list);
7670 }
7671
7672 /* Parse a mem-initializer.
7673
7674    mem-initializer:
7675      mem-initializer-id ( expression-list [opt] )
7676
7677    GNU extension:
7678
7679    mem-initializer:
7680      ( expression-list [opt] )
7681
7682    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7683    class) or FIELD_DECL (for a non-static data member) to initialize;
7684    the TREE_VALUE is the expression-list.  */
7685
7686 static tree
7687 cp_parser_mem_initializer (cp_parser* parser)
7688 {
7689   tree mem_initializer_id;
7690   tree expression_list;
7691   tree member;
7692
7693   /* Find out what is being initialized.  */
7694   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7695     {
7696       pedwarn ("anachronistic old-style base class initializer");
7697       mem_initializer_id = NULL_TREE;
7698     }
7699   else
7700     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7701   member = expand_member_init (mem_initializer_id);
7702   if (member && !DECL_P (member))
7703     in_base_initializer = 1;
7704
7705   expression_list
7706     = cp_parser_parenthesized_expression_list (parser, false,
7707                                                /*non_constant_p=*/NULL);
7708   if (!expression_list)
7709     expression_list = void_type_node;
7710
7711   in_base_initializer = 0;
7712
7713   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7714 }
7715
7716 /* Parse a mem-initializer-id.
7717
7718    mem-initializer-id:
7719      :: [opt] nested-name-specifier [opt] class-name
7720      identifier
7721
7722    Returns a TYPE indicating the class to be initializer for the first
7723    production.  Returns an IDENTIFIER_NODE indicating the data member
7724    to be initialized for the second production.  */
7725
7726 static tree
7727 cp_parser_mem_initializer_id (cp_parser* parser)
7728 {
7729   bool global_scope_p;
7730   bool nested_name_specifier_p;
7731   bool template_p = false;
7732   tree id;
7733
7734   /* `typename' is not allowed in this context ([temp.res]).  */
7735   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7736     {
7737       error ("keyword `typename' not allowed in this context (a qualified "
7738              "member initializer is implicitly a type)");
7739       cp_lexer_consume_token (parser->lexer);
7740     }
7741   /* Look for the optional `::' operator.  */
7742   global_scope_p
7743     = (cp_parser_global_scope_opt (parser,
7744                                    /*current_scope_valid_p=*/false)
7745        != NULL_TREE);
7746   /* Look for the optional nested-name-specifier.  The simplest way to
7747      implement:
7748
7749        [temp.res]
7750
7751        The keyword `typename' is not permitted in a base-specifier or
7752        mem-initializer; in these contexts a qualified name that
7753        depends on a template-parameter is implicitly assumed to be a
7754        type name.
7755
7756      is to assume that we have seen the `typename' keyword at this
7757      point.  */
7758   nested_name_specifier_p
7759     = (cp_parser_nested_name_specifier_opt (parser,
7760                                             /*typename_keyword_p=*/true,
7761                                             /*check_dependency_p=*/true,
7762                                             /*type_p=*/true,
7763                                             /*is_declaration=*/true)
7764        != NULL_TREE);
7765   if (nested_name_specifier_p)
7766     template_p = cp_parser_optional_template_keyword (parser);
7767   /* If there is a `::' operator or a nested-name-specifier, then we
7768      are definitely looking for a class-name.  */
7769   if (global_scope_p || nested_name_specifier_p)
7770     return cp_parser_class_name (parser,
7771                                  /*typename_keyword_p=*/true,
7772                                  /*template_keyword_p=*/template_p,
7773                                  /*type_p=*/false,
7774                                  /*check_dependency_p=*/true,
7775                                  /*class_head_p=*/false,
7776                                  /*is_declaration=*/true);
7777   /* Otherwise, we could also be looking for an ordinary identifier.  */
7778   cp_parser_parse_tentatively (parser);
7779   /* Try a class-name.  */
7780   id = cp_parser_class_name (parser,
7781                              /*typename_keyword_p=*/true,
7782                              /*template_keyword_p=*/false,
7783                              /*type_p=*/false,
7784                              /*check_dependency_p=*/true,
7785                              /*class_head_p=*/false,
7786                              /*is_declaration=*/true);
7787   /* If we found one, we're done.  */
7788   if (cp_parser_parse_definitely (parser))
7789     return id;
7790   /* Otherwise, look for an ordinary identifier.  */
7791   return cp_parser_identifier (parser);
7792 }
7793
7794 /* Overloading [gram.over] */
7795
7796 /* Parse an operator-function-id.
7797
7798    operator-function-id:
7799      operator operator
7800
7801    Returns an IDENTIFIER_NODE for the operator which is a
7802    human-readable spelling of the identifier, e.g., `operator +'.  */
7803
7804 static tree
7805 cp_parser_operator_function_id (cp_parser* parser)
7806 {
7807   /* Look for the `operator' keyword.  */
7808   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7809     return error_mark_node;
7810   /* And then the name of the operator itself.  */
7811   return cp_parser_operator (parser);
7812 }
7813
7814 /* Parse an operator.
7815
7816    operator:
7817      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7818      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7819      || ++ -- , ->* -> () []
7820
7821    GNU Extensions:
7822
7823    operator:
7824      <? >? <?= >?=
7825
7826    Returns an IDENTIFIER_NODE for the operator which is a
7827    human-readable spelling of the identifier, e.g., `operator +'.  */
7828
7829 static tree
7830 cp_parser_operator (cp_parser* parser)
7831 {
7832   tree id = NULL_TREE;
7833   cp_token *token;
7834
7835   /* Peek at the next token.  */
7836   token = cp_lexer_peek_token (parser->lexer);
7837   /* Figure out which operator we have.  */
7838   switch (token->type)
7839     {
7840     case CPP_KEYWORD:
7841       {
7842         enum tree_code op;
7843
7844         /* The keyword should be either `new' or `delete'.  */
7845         if (token->keyword == RID_NEW)
7846           op = NEW_EXPR;
7847         else if (token->keyword == RID_DELETE)
7848           op = DELETE_EXPR;
7849         else
7850           break;
7851
7852         /* Consume the `new' or `delete' token.  */
7853         cp_lexer_consume_token (parser->lexer);
7854
7855         /* Peek at the next token.  */
7856         token = cp_lexer_peek_token (parser->lexer);
7857         /* If it's a `[' token then this is the array variant of the
7858            operator.  */
7859         if (token->type == CPP_OPEN_SQUARE)
7860           {
7861             /* Consume the `[' token.  */
7862             cp_lexer_consume_token (parser->lexer);
7863             /* Look for the `]' token.  */
7864             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7865             id = ansi_opname (op == NEW_EXPR
7866                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7867           }
7868         /* Otherwise, we have the non-array variant.  */
7869         else
7870           id = ansi_opname (op);
7871
7872         return id;
7873       }
7874
7875     case CPP_PLUS:
7876       id = ansi_opname (PLUS_EXPR);
7877       break;
7878
7879     case CPP_MINUS:
7880       id = ansi_opname (MINUS_EXPR);
7881       break;
7882
7883     case CPP_MULT:
7884       id = ansi_opname (MULT_EXPR);
7885       break;
7886
7887     case CPP_DIV:
7888       id = ansi_opname (TRUNC_DIV_EXPR);
7889       break;
7890
7891     case CPP_MOD:
7892       id = ansi_opname (TRUNC_MOD_EXPR);
7893       break;
7894
7895     case CPP_XOR:
7896       id = ansi_opname (BIT_XOR_EXPR);
7897       break;
7898
7899     case CPP_AND:
7900       id = ansi_opname (BIT_AND_EXPR);
7901       break;
7902
7903     case CPP_OR:
7904       id = ansi_opname (BIT_IOR_EXPR);
7905       break;
7906
7907     case CPP_COMPL:
7908       id = ansi_opname (BIT_NOT_EXPR);
7909       break;
7910
7911     case CPP_NOT:
7912       id = ansi_opname (TRUTH_NOT_EXPR);
7913       break;
7914
7915     case CPP_EQ:
7916       id = ansi_assopname (NOP_EXPR);
7917       break;
7918
7919     case CPP_LESS:
7920       id = ansi_opname (LT_EXPR);
7921       break;
7922
7923     case CPP_GREATER:
7924       id = ansi_opname (GT_EXPR);
7925       break;
7926
7927     case CPP_PLUS_EQ:
7928       id = ansi_assopname (PLUS_EXPR);
7929       break;
7930
7931     case CPP_MINUS_EQ:
7932       id = ansi_assopname (MINUS_EXPR);
7933       break;
7934
7935     case CPP_MULT_EQ:
7936       id = ansi_assopname (MULT_EXPR);
7937       break;
7938
7939     case CPP_DIV_EQ:
7940       id = ansi_assopname (TRUNC_DIV_EXPR);
7941       break;
7942
7943     case CPP_MOD_EQ:
7944       id = ansi_assopname (TRUNC_MOD_EXPR);
7945       break;
7946
7947     case CPP_XOR_EQ:
7948       id = ansi_assopname (BIT_XOR_EXPR);
7949       break;
7950
7951     case CPP_AND_EQ:
7952       id = ansi_assopname (BIT_AND_EXPR);
7953       break;
7954
7955     case CPP_OR_EQ:
7956       id = ansi_assopname (BIT_IOR_EXPR);
7957       break;
7958
7959     case CPP_LSHIFT:
7960       id = ansi_opname (LSHIFT_EXPR);
7961       break;
7962
7963     case CPP_RSHIFT:
7964       id = ansi_opname (RSHIFT_EXPR);
7965       break;
7966
7967     case CPP_LSHIFT_EQ:
7968       id = ansi_assopname (LSHIFT_EXPR);
7969       break;
7970
7971     case CPP_RSHIFT_EQ:
7972       id = ansi_assopname (RSHIFT_EXPR);
7973       break;
7974
7975     case CPP_EQ_EQ:
7976       id = ansi_opname (EQ_EXPR);
7977       break;
7978
7979     case CPP_NOT_EQ:
7980       id = ansi_opname (NE_EXPR);
7981       break;
7982
7983     case CPP_LESS_EQ:
7984       id = ansi_opname (LE_EXPR);
7985       break;
7986
7987     case CPP_GREATER_EQ:
7988       id = ansi_opname (GE_EXPR);
7989       break;
7990
7991     case CPP_AND_AND:
7992       id = ansi_opname (TRUTH_ANDIF_EXPR);
7993       break;
7994
7995     case CPP_OR_OR:
7996       id = ansi_opname (TRUTH_ORIF_EXPR);
7997       break;
7998
7999     case CPP_PLUS_PLUS:
8000       id = ansi_opname (POSTINCREMENT_EXPR);
8001       break;
8002
8003     case CPP_MINUS_MINUS:
8004       id = ansi_opname (PREDECREMENT_EXPR);
8005       break;
8006
8007     case CPP_COMMA:
8008       id = ansi_opname (COMPOUND_EXPR);
8009       break;
8010
8011     case CPP_DEREF_STAR:
8012       id = ansi_opname (MEMBER_REF);
8013       break;
8014
8015     case CPP_DEREF:
8016       id = ansi_opname (COMPONENT_REF);
8017       break;
8018
8019     case CPP_OPEN_PAREN:
8020       /* Consume the `('.  */
8021       cp_lexer_consume_token (parser->lexer);
8022       /* Look for the matching `)'.  */
8023       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8024       return ansi_opname (CALL_EXPR);
8025
8026     case CPP_OPEN_SQUARE:
8027       /* Consume the `['.  */
8028       cp_lexer_consume_token (parser->lexer);
8029       /* Look for the matching `]'.  */
8030       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8031       return ansi_opname (ARRAY_REF);
8032
8033       /* Extensions.  */
8034     case CPP_MIN:
8035       id = ansi_opname (MIN_EXPR);
8036       break;
8037
8038     case CPP_MAX:
8039       id = ansi_opname (MAX_EXPR);
8040       break;
8041
8042     case CPP_MIN_EQ:
8043       id = ansi_assopname (MIN_EXPR);
8044       break;
8045
8046     case CPP_MAX_EQ:
8047       id = ansi_assopname (MAX_EXPR);
8048       break;
8049
8050     default:
8051       /* Anything else is an error.  */
8052       break;
8053     }
8054
8055   /* If we have selected an identifier, we need to consume the
8056      operator token.  */
8057   if (id)
8058     cp_lexer_consume_token (parser->lexer);
8059   /* Otherwise, no valid operator name was present.  */
8060   else
8061     {
8062       cp_parser_error (parser, "expected operator");
8063       id = error_mark_node;
8064     }
8065
8066   return id;
8067 }
8068
8069 /* Parse a template-declaration.
8070
8071    template-declaration:
8072      export [opt] template < template-parameter-list > declaration
8073
8074    If MEMBER_P is TRUE, this template-declaration occurs within a
8075    class-specifier.
8076
8077    The grammar rule given by the standard isn't correct.  What
8078    is really meant is:
8079
8080    template-declaration:
8081      export [opt] template-parameter-list-seq
8082        decl-specifier-seq [opt] init-declarator [opt] ;
8083      export [opt] template-parameter-list-seq
8084        function-definition
8085
8086    template-parameter-list-seq:
8087      template-parameter-list-seq [opt]
8088      template < template-parameter-list >  */
8089
8090 static void
8091 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8092 {
8093   /* Check for `export'.  */
8094   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8095     {
8096       /* Consume the `export' token.  */
8097       cp_lexer_consume_token (parser->lexer);
8098       /* Warn that we do not support `export'.  */
8099       warning ("keyword `export' not implemented, and will be ignored");
8100     }
8101
8102   cp_parser_template_declaration_after_export (parser, member_p);
8103 }
8104
8105 /* Parse a template-parameter-list.
8106
8107    template-parameter-list:
8108      template-parameter
8109      template-parameter-list , template-parameter
8110
8111    Returns a TREE_LIST.  Each node represents a template parameter.
8112    The nodes are connected via their TREE_CHAINs.  */
8113
8114 static tree
8115 cp_parser_template_parameter_list (cp_parser* parser)
8116 {
8117   tree parameter_list = NULL_TREE;
8118
8119   while (true)
8120     {
8121       tree parameter;
8122       cp_token *token;
8123       bool is_non_type;
8124
8125       /* Parse the template-parameter.  */
8126       parameter = cp_parser_template_parameter (parser, &is_non_type);
8127       /* Add it to the list.  */
8128       parameter_list = process_template_parm (parameter_list,
8129                                               parameter,
8130                                               is_non_type);
8131       /* Peek at the next token.  */
8132       token = cp_lexer_peek_token (parser->lexer);
8133       /* If it's not a `,', we're done.  */
8134       if (token->type != CPP_COMMA)
8135         break;
8136       /* Otherwise, consume the `,' token.  */
8137       cp_lexer_consume_token (parser->lexer);
8138     }
8139
8140   return parameter_list;
8141 }
8142
8143 /* Parse a template-parameter.
8144
8145    template-parameter:
8146      type-parameter
8147      parameter-declaration
8148
8149    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8150    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8151    true iff this parameter is a non-type parameter.  */
8152
8153 static tree
8154 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8155 {
8156   cp_token *token;
8157   cp_parameter_declarator *parameter_declarator;
8158
8159   /* Assume it is a type parameter or a template parameter.  */
8160   *is_non_type = false;
8161   /* Peek at the next token.  */
8162   token = cp_lexer_peek_token (parser->lexer);
8163   /* If it is `class' or `template', we have a type-parameter.  */
8164   if (token->keyword == RID_TEMPLATE)
8165     return cp_parser_type_parameter (parser);
8166   /* If it is `class' or `typename' we do not know yet whether it is a
8167      type parameter or a non-type parameter.  Consider:
8168
8169        template <typename T, typename T::X X> ...
8170
8171      or:
8172
8173        template <class C, class D*> ...
8174
8175      Here, the first parameter is a type parameter, and the second is
8176      a non-type parameter.  We can tell by looking at the token after
8177      the identifier -- if it is a `,', `=', or `>' then we have a type
8178      parameter.  */
8179   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8180     {
8181       /* Peek at the token after `class' or `typename'.  */
8182       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8183       /* If it's an identifier, skip it.  */
8184       if (token->type == CPP_NAME)
8185         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8186       /* Now, see if the token looks like the end of a template
8187          parameter.  */
8188       if (token->type == CPP_COMMA
8189           || token->type == CPP_EQ
8190           || token->type == CPP_GREATER)
8191         return cp_parser_type_parameter (parser);
8192     }
8193
8194   /* Otherwise, it is a non-type parameter.
8195
8196      [temp.param]
8197
8198      When parsing a default template-argument for a non-type
8199      template-parameter, the first non-nested `>' is taken as the end
8200      of the template parameter-list rather than a greater-than
8201      operator.  */
8202   *is_non_type = true;
8203   parameter_declarator
8204      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8205                                         /*parenthesized_p=*/NULL);
8206   return (build_tree_list
8207           (parameter_declarator->default_argument,
8208            grokdeclarator (parameter_declarator->declarator,
8209                            &parameter_declarator->decl_specifiers,
8210                            PARM, /*initialized=*/0,
8211                            /*attrlist=*/NULL)));
8212 }
8213
8214 /* Parse a type-parameter.
8215
8216    type-parameter:
8217      class identifier [opt]
8218      class identifier [opt] = type-id
8219      typename identifier [opt]
8220      typename identifier [opt] = type-id
8221      template < template-parameter-list > class identifier [opt]
8222      template < template-parameter-list > class identifier [opt]
8223        = id-expression
8224
8225    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8226    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8227    the declaration of the parameter.  */
8228
8229 static tree
8230 cp_parser_type_parameter (cp_parser* parser)
8231 {
8232   cp_token *token;
8233   tree parameter;
8234
8235   /* Look for a keyword to tell us what kind of parameter this is.  */
8236   token = cp_parser_require (parser, CPP_KEYWORD,
8237                              "`class', `typename', or `template'");
8238   if (!token)
8239     return error_mark_node;
8240
8241   switch (token->keyword)
8242     {
8243     case RID_CLASS:
8244     case RID_TYPENAME:
8245       {
8246         tree identifier;
8247         tree default_argument;
8248
8249         /* If the next token is an identifier, then it names the
8250            parameter.  */
8251         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8252           identifier = cp_parser_identifier (parser);
8253         else
8254           identifier = NULL_TREE;
8255
8256         /* Create the parameter.  */
8257         parameter = finish_template_type_parm (class_type_node, identifier);
8258
8259         /* If the next token is an `=', we have a default argument.  */
8260         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8261           {
8262             /* Consume the `=' token.  */
8263             cp_lexer_consume_token (parser->lexer);
8264             /* Parse the default-argument.  */
8265             default_argument = cp_parser_type_id (parser);
8266           }
8267         else
8268           default_argument = NULL_TREE;
8269
8270         /* Create the combined representation of the parameter and the
8271            default argument.  */
8272         parameter = build_tree_list (default_argument, parameter);
8273       }
8274       break;
8275
8276     case RID_TEMPLATE:
8277       {
8278         tree parameter_list;
8279         tree identifier;
8280         tree default_argument;
8281
8282         /* Look for the `<'.  */
8283         cp_parser_require (parser, CPP_LESS, "`<'");
8284         /* Parse the template-parameter-list.  */
8285         begin_template_parm_list ();
8286         parameter_list
8287           = cp_parser_template_parameter_list (parser);
8288         parameter_list = end_template_parm_list (parameter_list);
8289         /* Look for the `>'.  */
8290         cp_parser_require (parser, CPP_GREATER, "`>'");
8291         /* Look for the `class' keyword.  */
8292         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8293         /* If the next token is an `=', then there is a
8294            default-argument.  If the next token is a `>', we are at
8295            the end of the parameter-list.  If the next token is a `,',
8296            then we are at the end of this parameter.  */
8297         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8298             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8299             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8300           identifier = cp_parser_identifier (parser);
8301         else
8302           identifier = NULL_TREE;
8303         /* Create the template parameter.  */
8304         parameter = finish_template_template_parm (class_type_node,
8305                                                    identifier);
8306
8307         /* If the next token is an `=', then there is a
8308            default-argument.  */
8309         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8310           {
8311             bool is_template;
8312
8313             /* Consume the `='.  */
8314             cp_lexer_consume_token (parser->lexer);
8315             /* Parse the id-expression.  */
8316             default_argument
8317               = cp_parser_id_expression (parser,
8318                                          /*template_keyword_p=*/false,
8319                                          /*check_dependency_p=*/true,
8320                                          /*template_p=*/&is_template,
8321                                          /*declarator_p=*/false);
8322             if (TREE_CODE (default_argument) == TYPE_DECL)
8323               /* If the id-expression was a template-id that refers to
8324                  a template-class, we already have the declaration here,
8325                  so no further lookup is needed.  */
8326                  ;
8327             else
8328               /* Look up the name.  */
8329               default_argument
8330                 = cp_parser_lookup_name (parser, default_argument,
8331                                         /*is_type=*/false,
8332                                         /*is_template=*/is_template,
8333                                         /*is_namespace=*/false,
8334                                         /*check_dependency=*/true);
8335             /* See if the default argument is valid.  */
8336             default_argument
8337               = check_template_template_default_arg (default_argument);
8338           }
8339         else
8340           default_argument = NULL_TREE;
8341
8342         /* Create the combined representation of the parameter and the
8343            default argument.  */
8344         parameter =  build_tree_list (default_argument, parameter);
8345       }
8346       break;
8347
8348     default:
8349       /* Anything else is an error.  */
8350       cp_parser_error (parser,
8351                        "expected `class', `typename', or `template'");
8352       parameter = error_mark_node;
8353     }
8354
8355   return parameter;
8356 }
8357
8358 /* Parse a template-id.
8359
8360    template-id:
8361      template-name < template-argument-list [opt] >
8362
8363    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8364    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8365    returned.  Otherwise, if the template-name names a function, or set
8366    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8367    names a class, returns a TYPE_DECL for the specialization.
8368
8369    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8370    uninstantiated templates.  */
8371
8372 static tree
8373 cp_parser_template_id (cp_parser *parser,
8374                        bool template_keyword_p,
8375                        bool check_dependency_p,
8376                        bool is_declaration)
8377 {
8378   tree template;
8379   tree arguments;
8380   tree template_id;
8381   ptrdiff_t start_of_id;
8382   tree access_check = NULL_TREE;
8383   cp_token *next_token, *next_token_2;
8384   bool is_identifier;
8385
8386   /* If the next token corresponds to a template-id, there is no need
8387      to reparse it.  */
8388   next_token = cp_lexer_peek_token (parser->lexer);
8389   if (next_token->type == CPP_TEMPLATE_ID)
8390     {
8391       tree value;
8392       tree check;
8393
8394       /* Get the stored value.  */
8395       value = cp_lexer_consume_token (parser->lexer)->value;
8396       /* Perform any access checks that were deferred.  */
8397       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8398         perform_or_defer_access_check (TREE_PURPOSE (check),
8399                                        TREE_VALUE (check));
8400       /* Return the stored value.  */
8401       return TREE_VALUE (value);
8402     }
8403
8404   /* Avoid performing name lookup if there is no possibility of
8405      finding a template-id.  */
8406   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8407       || (next_token->type == CPP_NAME
8408           && !cp_parser_nth_token_starts_template_argument_list_p
8409                (parser, 2)))
8410     {
8411       cp_parser_error (parser, "expected template-id");
8412       return error_mark_node;
8413     }
8414
8415   /* Remember where the template-id starts.  */
8416   if (cp_parser_parsing_tentatively (parser)
8417       && !cp_parser_committed_to_tentative_parse (parser))
8418     {
8419       next_token = cp_lexer_peek_token (parser->lexer);
8420       start_of_id = cp_lexer_token_difference (parser->lexer,
8421                                                parser->lexer->first_token,
8422                                                next_token);
8423     }
8424   else
8425     start_of_id = -1;
8426
8427   push_deferring_access_checks (dk_deferred);
8428
8429   /* Parse the template-name.  */
8430   is_identifier = false;
8431   template = cp_parser_template_name (parser, template_keyword_p,
8432                                       check_dependency_p,
8433                                       is_declaration,
8434                                       &is_identifier);
8435   if (template == error_mark_node || is_identifier)
8436     {
8437       pop_deferring_access_checks ();
8438       return template;
8439     }
8440
8441   /* If we find the sequence `[:' after a template-name, it's probably
8442      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8443      parse correctly the argument list.  */
8444   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8445   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8446   if (next_token->type == CPP_OPEN_SQUARE
8447       && next_token->flags & DIGRAPH
8448       && next_token_2->type == CPP_COLON
8449       && !(next_token_2->flags & PREV_WHITE))
8450     {
8451       cp_parser_parse_tentatively (parser);
8452       /* Change `:' into `::'.  */
8453       next_token_2->type = CPP_SCOPE;
8454       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8455          CPP_LESS.  */
8456       cp_lexer_consume_token (parser->lexer);
8457       /* Parse the arguments.  */
8458       arguments = cp_parser_enclosed_template_argument_list (parser);
8459       if (!cp_parser_parse_definitely (parser))
8460         {
8461           /* If we couldn't parse an argument list, then we revert our changes
8462              and return simply an error. Maybe this is not a template-id
8463              after all.  */
8464           next_token_2->type = CPP_COLON;
8465           cp_parser_error (parser, "expected `<'");
8466           pop_deferring_access_checks ();
8467           return error_mark_node;
8468         }
8469       /* Otherwise, emit an error about the invalid digraph, but continue
8470          parsing because we got our argument list.  */
8471       pedwarn ("`<::' cannot begin a template-argument list");
8472       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8473               "between `<' and `::'");
8474       if (!flag_permissive)
8475         {
8476           static bool hint;
8477           if (!hint)
8478             {
8479               inform ("(if you use `-fpermissive' G++ will accept your code)");
8480               hint = true;
8481             }
8482         }
8483     }
8484   else
8485     {
8486       /* Look for the `<' that starts the template-argument-list.  */
8487       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8488         {
8489           pop_deferring_access_checks ();
8490           return error_mark_node;
8491         }
8492       /* Parse the arguments.  */
8493       arguments = cp_parser_enclosed_template_argument_list (parser);
8494     }
8495
8496   /* Build a representation of the specialization.  */
8497   if (TREE_CODE (template) == IDENTIFIER_NODE)
8498     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8499   else if (DECL_CLASS_TEMPLATE_P (template)
8500            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8501     template_id
8502       = finish_template_type (template, arguments,
8503                               cp_lexer_next_token_is (parser->lexer,
8504                                                       CPP_SCOPE));
8505   else
8506     {
8507       /* If it's not a class-template or a template-template, it should be
8508          a function-template.  */
8509       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8510                            || TREE_CODE (template) == OVERLOAD
8511                            || BASELINK_P (template)),
8512                           20010716);
8513
8514       template_id = lookup_template_function (template, arguments);
8515     }
8516
8517   /* Retrieve any deferred checks.  Do not pop this access checks yet
8518      so the memory will not be reclaimed during token replacing below.  */
8519   access_check = get_deferred_access_checks ();
8520
8521   /* If parsing tentatively, replace the sequence of tokens that makes
8522      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8523      should we re-parse the token stream, we will not have to repeat
8524      the effort required to do the parse, nor will we issue duplicate
8525      error messages about problems during instantiation of the
8526      template.  */
8527   if (start_of_id >= 0)
8528     {
8529       cp_token *token;
8530
8531       /* Find the token that corresponds to the start of the
8532          template-id.  */
8533       token = cp_lexer_advance_token (parser->lexer,
8534                                       parser->lexer->first_token,
8535                                       start_of_id);
8536
8537       /* Reset the contents of the START_OF_ID token.  */
8538       token->type = CPP_TEMPLATE_ID;
8539       token->value = build_tree_list (access_check, template_id);
8540       token->keyword = RID_MAX;
8541       /* Purge all subsequent tokens.  */
8542       cp_lexer_purge_tokens_after (parser->lexer, token);
8543     }
8544
8545   pop_deferring_access_checks ();
8546   return template_id;
8547 }
8548
8549 /* Parse a template-name.
8550
8551    template-name:
8552      identifier
8553
8554    The standard should actually say:
8555
8556    template-name:
8557      identifier
8558      operator-function-id
8559
8560    A defect report has been filed about this issue.
8561
8562    A conversion-function-id cannot be a template name because they cannot
8563    be part of a template-id. In fact, looking at this code:
8564
8565    a.operator K<int>()
8566
8567    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8568    It is impossible to call a templated conversion-function-id with an
8569    explicit argument list, since the only allowed template parameter is
8570    the type to which it is converting.
8571
8572    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8573    `template' keyword, in a construction like:
8574
8575      T::template f<3>()
8576
8577    In that case `f' is taken to be a template-name, even though there
8578    is no way of knowing for sure.
8579
8580    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8581    name refers to a set of overloaded functions, at least one of which
8582    is a template, or an IDENTIFIER_NODE with the name of the template,
8583    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8584    names are looked up inside uninstantiated templates.  */
8585
8586 static tree
8587 cp_parser_template_name (cp_parser* parser,
8588                          bool template_keyword_p,
8589                          bool check_dependency_p,
8590                          bool is_declaration,
8591                          bool *is_identifier)
8592 {
8593   tree identifier;
8594   tree decl;
8595   tree fns;
8596
8597   /* If the next token is `operator', then we have either an
8598      operator-function-id or a conversion-function-id.  */
8599   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8600     {
8601       /* We don't know whether we're looking at an
8602          operator-function-id or a conversion-function-id.  */
8603       cp_parser_parse_tentatively (parser);
8604       /* Try an operator-function-id.  */
8605       identifier = cp_parser_operator_function_id (parser);
8606       /* If that didn't work, try a conversion-function-id.  */
8607       if (!cp_parser_parse_definitely (parser))
8608         {
8609           cp_parser_error (parser, "expected template-name");
8610           return error_mark_node;
8611         }
8612     }
8613   /* Look for the identifier.  */
8614   else
8615     identifier = cp_parser_identifier (parser);
8616
8617   /* If we didn't find an identifier, we don't have a template-id.  */
8618   if (identifier == error_mark_node)
8619     return error_mark_node;
8620
8621   /* If the name immediately followed the `template' keyword, then it
8622      is a template-name.  However, if the next token is not `<', then
8623      we do not treat it as a template-name, since it is not being used
8624      as part of a template-id.  This enables us to handle constructs
8625      like:
8626
8627        template <typename T> struct S { S(); };
8628        template <typename T> S<T>::S();
8629
8630      correctly.  We would treat `S' as a template -- if it were `S<T>'
8631      -- but we do not if there is no `<'.  */
8632
8633   if (processing_template_decl
8634       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8635     {
8636       /* In a declaration, in a dependent context, we pretend that the
8637          "template" keyword was present in order to improve error
8638          recovery.  For example, given:
8639
8640            template <typename T> void f(T::X<int>);
8641
8642          we want to treat "X<int>" as a template-id.  */
8643       if (is_declaration
8644           && !template_keyword_p
8645           && parser->scope && TYPE_P (parser->scope)
8646           && dependent_type_p (parser->scope)
8647           /* Do not do this for dtors (or ctors), since they never
8648              need the template keyword before their name.  */
8649           && !constructor_name_p (identifier, parser->scope))
8650         {
8651           ptrdiff_t start;
8652           cp_token* token;
8653           /* Explain what went wrong.  */
8654           error ("non-template `%D' used as template", identifier);
8655           inform ("use `%T::template %D' to indicate that it is a template",
8656                   parser->scope, identifier);
8657           /* If parsing tentatively, find the location of the "<"
8658              token.  */
8659           if (cp_parser_parsing_tentatively (parser)
8660               && !cp_parser_committed_to_tentative_parse (parser))
8661             {
8662               cp_parser_simulate_error (parser);
8663               token = cp_lexer_peek_token (parser->lexer);
8664               token = cp_lexer_prev_token (parser->lexer, token);
8665               start = cp_lexer_token_difference (parser->lexer,
8666                                                  parser->lexer->first_token,
8667                                                  token);
8668             }
8669           else
8670             start = -1;
8671           /* Parse the template arguments so that we can issue error
8672              messages about them.  */
8673           cp_lexer_consume_token (parser->lexer);
8674           cp_parser_enclosed_template_argument_list (parser);
8675           /* Skip tokens until we find a good place from which to
8676              continue parsing.  */
8677           cp_parser_skip_to_closing_parenthesis (parser,
8678                                                  /*recovering=*/true,
8679                                                  /*or_comma=*/true,
8680                                                  /*consume_paren=*/false);
8681           /* If parsing tentatively, permanently remove the
8682              template argument list.  That will prevent duplicate
8683              error messages from being issued about the missing
8684              "template" keyword.  */
8685           if (start >= 0)
8686             {
8687               token = cp_lexer_advance_token (parser->lexer,
8688                                               parser->lexer->first_token,
8689                                               start);
8690               cp_lexer_purge_tokens_after (parser->lexer, token);
8691             }
8692           if (is_identifier)
8693             *is_identifier = true;
8694           return identifier;
8695         }
8696
8697       /* If the "template" keyword is present, then there is generally
8698          no point in doing name-lookup, so we just return IDENTIFIER.
8699          But, if the qualifying scope is non-dependent then we can
8700          (and must) do name-lookup normally.  */
8701       if (template_keyword_p
8702           && (!parser->scope
8703               || (TYPE_P (parser->scope)
8704                   && dependent_type_p (parser->scope))))
8705         return identifier;
8706     }
8707
8708   /* Look up the name.  */
8709   decl = cp_parser_lookup_name (parser, identifier,
8710                                 /*is_type=*/false,
8711                                 /*is_template=*/false,
8712                                 /*is_namespace=*/false,
8713                                 check_dependency_p);
8714   decl = maybe_get_template_decl_from_type_decl (decl);
8715
8716   /* If DECL is a template, then the name was a template-name.  */
8717   if (TREE_CODE (decl) == TEMPLATE_DECL)
8718     ;
8719   else
8720     {
8721       /* The standard does not explicitly indicate whether a name that
8722          names a set of overloaded declarations, some of which are
8723          templates, is a template-name.  However, such a name should
8724          be a template-name; otherwise, there is no way to form a
8725          template-id for the overloaded templates.  */
8726       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8727       if (TREE_CODE (fns) == OVERLOAD)
8728         {
8729           tree fn;
8730
8731           for (fn = fns; fn; fn = OVL_NEXT (fn))
8732             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8733               break;
8734         }
8735       else
8736         {
8737           /* Otherwise, the name does not name a template.  */
8738           cp_parser_error (parser, "expected template-name");
8739           return error_mark_node;
8740         }
8741     }
8742
8743   /* If DECL is dependent, and refers to a function, then just return
8744      its name; we will look it up again during template instantiation.  */
8745   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8746     {
8747       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8748       if (TYPE_P (scope) && dependent_type_p (scope))
8749         return identifier;
8750     }
8751
8752   return decl;
8753 }
8754
8755 /* Parse a template-argument-list.
8756
8757    template-argument-list:
8758      template-argument
8759      template-argument-list , template-argument
8760
8761    Returns a TREE_VEC containing the arguments.  */
8762
8763 static tree
8764 cp_parser_template_argument_list (cp_parser* parser)
8765 {
8766   tree fixed_args[10];
8767   unsigned n_args = 0;
8768   unsigned alloced = 10;
8769   tree *arg_ary = fixed_args;
8770   tree vec;
8771   bool saved_in_template_argument_list_p;
8772
8773   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8774   parser->in_template_argument_list_p = true;
8775   do
8776     {
8777       tree argument;
8778
8779       if (n_args)
8780         /* Consume the comma.  */
8781         cp_lexer_consume_token (parser->lexer);
8782
8783       /* Parse the template-argument.  */
8784       argument = cp_parser_template_argument (parser);
8785       if (n_args == alloced)
8786         {
8787           alloced *= 2;
8788
8789           if (arg_ary == fixed_args)
8790             {
8791               arg_ary = xmalloc (sizeof (tree) * alloced);
8792               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8793             }
8794           else
8795             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8796         }
8797       arg_ary[n_args++] = argument;
8798     }
8799   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8800
8801   vec = make_tree_vec (n_args);
8802
8803   while (n_args--)
8804     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8805
8806   if (arg_ary != fixed_args)
8807     free (arg_ary);
8808   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8809   return vec;
8810 }
8811
8812 /* Parse a template-argument.
8813
8814    template-argument:
8815      assignment-expression
8816      type-id
8817      id-expression
8818
8819    The representation is that of an assignment-expression, type-id, or
8820    id-expression -- except that the qualified id-expression is
8821    evaluated, so that the value returned is either a DECL or an
8822    OVERLOAD.
8823
8824    Although the standard says "assignment-expression", it forbids
8825    throw-expressions or assignments in the template argument.
8826    Therefore, we use "conditional-expression" instead.  */
8827
8828 static tree
8829 cp_parser_template_argument (cp_parser* parser)
8830 {
8831   tree argument;
8832   bool template_p;
8833   bool address_p;
8834   bool maybe_type_id = false;
8835   cp_token *token;
8836   cp_id_kind idk;
8837   tree qualifying_class;
8838
8839   /* There's really no way to know what we're looking at, so we just
8840      try each alternative in order.
8841
8842        [temp.arg]
8843
8844        In a template-argument, an ambiguity between a type-id and an
8845        expression is resolved to a type-id, regardless of the form of
8846        the corresponding template-parameter.
8847
8848      Therefore, we try a type-id first.  */
8849   cp_parser_parse_tentatively (parser);
8850   argument = cp_parser_type_id (parser);
8851   /* If there was no error parsing the type-id but the next token is a '>>',
8852      we probably found a typo for '> >'. But there are type-id which are
8853      also valid expressions. For instance:
8854
8855      struct X { int operator >> (int); };
8856      template <int V> struct Foo {};
8857      Foo<X () >> 5> r;
8858
8859      Here 'X()' is a valid type-id of a function type, but the user just
8860      wanted to write the expression "X() >> 5". Thus, we remember that we
8861      found a valid type-id, but we still try to parse the argument as an
8862      expression to see what happens.  */
8863   if (!cp_parser_error_occurred (parser)
8864       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8865     {
8866       maybe_type_id = true;
8867       cp_parser_abort_tentative_parse (parser);
8868     }
8869   else
8870     {
8871       /* If the next token isn't a `,' or a `>', then this argument wasn't
8872       really finished. This means that the argument is not a valid
8873       type-id.  */
8874       if (!cp_parser_next_token_ends_template_argument_p (parser))
8875         cp_parser_error (parser, "expected template-argument");
8876       /* If that worked, we're done.  */
8877       if (cp_parser_parse_definitely (parser))
8878         return argument;
8879     }
8880   /* We're still not sure what the argument will be.  */
8881   cp_parser_parse_tentatively (parser);
8882   /* Try a template.  */
8883   argument = cp_parser_id_expression (parser,
8884                                       /*template_keyword_p=*/false,
8885                                       /*check_dependency_p=*/true,
8886                                       &template_p,
8887                                       /*declarator_p=*/false);
8888   /* If the next token isn't a `,' or a `>', then this argument wasn't
8889      really finished.  */
8890   if (!cp_parser_next_token_ends_template_argument_p (parser))
8891     cp_parser_error (parser, "expected template-argument");
8892   if (!cp_parser_error_occurred (parser))
8893     {
8894       /* Figure out what is being referred to.  If the id-expression
8895          was for a class template specialization, then we will have a
8896          TYPE_DECL at this point.  There is no need to do name lookup
8897          at this point in that case.  */
8898       if (TREE_CODE (argument) != TYPE_DECL)
8899         argument = cp_parser_lookup_name (parser, argument,
8900                                           /*is_type=*/false,
8901                                           /*is_template=*/template_p,
8902                                           /*is_namespace=*/false,
8903                                           /*check_dependency=*/true);
8904       if (TREE_CODE (argument) != TEMPLATE_DECL
8905           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8906         cp_parser_error (parser, "expected template-name");
8907     }
8908   if (cp_parser_parse_definitely (parser))
8909     return argument;
8910   /* It must be a non-type argument.  There permitted cases are given
8911      in [temp.arg.nontype]:
8912
8913      -- an integral constant-expression of integral or enumeration
8914         type; or
8915
8916      -- the name of a non-type template-parameter; or
8917
8918      -- the name of an object or function with external linkage...
8919
8920      -- the address of an object or function with external linkage...
8921
8922      -- a pointer to member...  */
8923   /* Look for a non-type template parameter.  */
8924   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8925     {
8926       cp_parser_parse_tentatively (parser);
8927       argument = cp_parser_primary_expression (parser,
8928                                                &idk,
8929                                                &qualifying_class);
8930       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8931           || !cp_parser_next_token_ends_template_argument_p (parser))
8932         cp_parser_simulate_error (parser);
8933       if (cp_parser_parse_definitely (parser))
8934         return argument;
8935     }
8936   /* If the next token is "&", the argument must be the address of an
8937      object or function with external linkage.  */
8938   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8939   if (address_p)
8940     cp_lexer_consume_token (parser->lexer);
8941   /* See if we might have an id-expression.  */
8942   token = cp_lexer_peek_token (parser->lexer);
8943   if (token->type == CPP_NAME
8944       || token->keyword == RID_OPERATOR
8945       || token->type == CPP_SCOPE
8946       || token->type == CPP_TEMPLATE_ID
8947       || token->type == CPP_NESTED_NAME_SPECIFIER)
8948     {
8949       cp_parser_parse_tentatively (parser);
8950       argument = cp_parser_primary_expression (parser,
8951                                                &idk,
8952                                                &qualifying_class);
8953       if (cp_parser_error_occurred (parser)
8954           || !cp_parser_next_token_ends_template_argument_p (parser))
8955         cp_parser_abort_tentative_parse (parser);
8956       else
8957         {
8958           if (qualifying_class)
8959             argument = finish_qualified_id_expr (qualifying_class,
8960                                                  argument,
8961                                                  /*done=*/true,
8962                                                  address_p);
8963           if (TREE_CODE (argument) == VAR_DECL)
8964             {
8965               /* A variable without external linkage might still be a
8966                  valid constant-expression, so no error is issued here
8967                  if the external-linkage check fails.  */
8968               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8969                 cp_parser_simulate_error (parser);
8970             }
8971           else if (is_overloaded_fn (argument))
8972             /* All overloaded functions are allowed; if the external
8973                linkage test does not pass, an error will be issued
8974                later.  */
8975             ;
8976           else if (address_p
8977                    && (TREE_CODE (argument) == OFFSET_REF
8978                        || TREE_CODE (argument) == SCOPE_REF))
8979             /* A pointer-to-member.  */
8980             ;
8981           else
8982             cp_parser_simulate_error (parser);
8983
8984           if (cp_parser_parse_definitely (parser))
8985             {
8986               if (address_p)
8987                 argument = build_x_unary_op (ADDR_EXPR, argument);
8988               return argument;
8989             }
8990         }
8991     }
8992   /* If the argument started with "&", there are no other valid
8993      alternatives at this point.  */
8994   if (address_p)
8995     {
8996       cp_parser_error (parser, "invalid non-type template argument");
8997       return error_mark_node;
8998     }
8999   /* If the argument wasn't successfully parsed as a type-id followed
9000      by '>>', the argument can only be a constant expression now.
9001      Otherwise, we try parsing the constant-expression tentatively,
9002      because the argument could really be a type-id.  */
9003   if (maybe_type_id)
9004     cp_parser_parse_tentatively (parser);
9005   argument = cp_parser_constant_expression (parser,
9006                                             /*allow_non_constant_p=*/false,
9007                                             /*non_constant_p=*/NULL);
9008   argument = fold_non_dependent_expr (argument);
9009   if (!maybe_type_id)
9010     return argument;
9011   if (!cp_parser_next_token_ends_template_argument_p (parser))
9012     cp_parser_error (parser, "expected template-argument");
9013   if (cp_parser_parse_definitely (parser))
9014     return argument;
9015   /* We did our best to parse the argument as a non type-id, but that
9016      was the only alternative that matched (albeit with a '>' after
9017      it). We can assume it's just a typo from the user, and a
9018      diagnostic will then be issued.  */
9019   return cp_parser_type_id (parser);
9020 }
9021
9022 /* Parse an explicit-instantiation.
9023
9024    explicit-instantiation:
9025      template declaration
9026
9027    Although the standard says `declaration', what it really means is:
9028
9029    explicit-instantiation:
9030      template decl-specifier-seq [opt] declarator [opt] ;
9031
9032    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9033    supposed to be allowed.  A defect report has been filed about this
9034    issue.
9035
9036    GNU Extension:
9037
9038    explicit-instantiation:
9039      storage-class-specifier template
9040        decl-specifier-seq [opt] declarator [opt] ;
9041      function-specifier template
9042        decl-specifier-seq [opt] declarator [opt] ;  */
9043
9044 static void
9045 cp_parser_explicit_instantiation (cp_parser* parser)
9046 {
9047   int declares_class_or_enum;
9048   cp_decl_specifier_seq decl_specifiers;
9049   tree extension_specifier = NULL_TREE;
9050
9051   /* Look for an (optional) storage-class-specifier or
9052      function-specifier.  */
9053   if (cp_parser_allow_gnu_extensions_p (parser))
9054     {
9055       extension_specifier
9056         = cp_parser_storage_class_specifier_opt (parser);
9057       if (!extension_specifier)
9058         extension_specifier
9059           = cp_parser_function_specifier_opt (parser,
9060                                               /*decl_specs=*/NULL);
9061     }
9062
9063   /* Look for the `template' keyword.  */
9064   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9065   /* Let the front end know that we are processing an explicit
9066      instantiation.  */
9067   begin_explicit_instantiation ();
9068   /* [temp.explicit] says that we are supposed to ignore access
9069      control while processing explicit instantiation directives.  */
9070   push_deferring_access_checks (dk_no_check);
9071   /* Parse a decl-specifier-seq.  */
9072   cp_parser_decl_specifier_seq (parser,
9073                                 CP_PARSER_FLAGS_OPTIONAL,
9074                                 &decl_specifiers,
9075                                 &declares_class_or_enum);
9076   /* If there was exactly one decl-specifier, and it declared a class,
9077      and there's no declarator, then we have an explicit type
9078      instantiation.  */
9079   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9080     {
9081       tree type;
9082
9083       type = check_tag_decl (&decl_specifiers);
9084       /* Turn access control back on for names used during
9085          template instantiation.  */
9086       pop_deferring_access_checks ();
9087       if (type)
9088         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9089     }
9090   else
9091     {
9092       cp_declarator *declarator;
9093       tree decl;
9094
9095       /* Parse the declarator.  */
9096       declarator
9097         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9098                                 /*ctor_dtor_or_conv_p=*/NULL,
9099                                 /*parenthesized_p=*/NULL);
9100       cp_parser_check_for_definition_in_return_type (declarator,
9101                                                      declares_class_or_enum);
9102       if (declarator != cp_error_declarator)
9103         {
9104           decl = grokdeclarator (declarator, &decl_specifiers,
9105                                  NORMAL, 0, NULL);
9106           /* Turn access control back on for names used during
9107              template instantiation.  */
9108           pop_deferring_access_checks ();
9109           /* Do the explicit instantiation.  */
9110           do_decl_instantiation (decl, extension_specifier);
9111         }
9112       else
9113         {
9114           pop_deferring_access_checks ();
9115           /* Skip the body of the explicit instantiation.  */
9116           cp_parser_skip_to_end_of_statement (parser);
9117         }
9118     }
9119   /* We're done with the instantiation.  */
9120   end_explicit_instantiation ();
9121
9122   cp_parser_consume_semicolon_at_end_of_statement (parser);
9123 }
9124
9125 /* Parse an explicit-specialization.
9126
9127    explicit-specialization:
9128      template < > declaration
9129
9130    Although the standard says `declaration', what it really means is:
9131
9132    explicit-specialization:
9133      template <> decl-specifier [opt] init-declarator [opt] ;
9134      template <> function-definition
9135      template <> explicit-specialization
9136      template <> template-declaration  */
9137
9138 static void
9139 cp_parser_explicit_specialization (cp_parser* parser)
9140 {
9141   /* Look for the `template' keyword.  */
9142   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9143   /* Look for the `<'.  */
9144   cp_parser_require (parser, CPP_LESS, "`<'");
9145   /* Look for the `>'.  */
9146   cp_parser_require (parser, CPP_GREATER, "`>'");
9147   /* We have processed another parameter list.  */
9148   ++parser->num_template_parameter_lists;
9149   /* Let the front end know that we are beginning a specialization.  */
9150   begin_specialization ();
9151
9152   /* If the next keyword is `template', we need to figure out whether
9153      or not we're looking a template-declaration.  */
9154   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9155     {
9156       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9157           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9158         cp_parser_template_declaration_after_export (parser,
9159                                                      /*member_p=*/false);
9160       else
9161         cp_parser_explicit_specialization (parser);
9162     }
9163   else
9164     /* Parse the dependent declaration.  */
9165     cp_parser_single_declaration (parser,
9166                                   /*member_p=*/false,
9167                                   /*friend_p=*/NULL);
9168
9169   /* We're done with the specialization.  */
9170   end_specialization ();
9171   /* We're done with this parameter list.  */
9172   --parser->num_template_parameter_lists;
9173 }
9174
9175 /* Parse a type-specifier.
9176
9177    type-specifier:
9178      simple-type-specifier
9179      class-specifier
9180      enum-specifier
9181      elaborated-type-specifier
9182      cv-qualifier
9183
9184    GNU Extension:
9185
9186    type-specifier:
9187      __complex__
9188
9189    Returns a representation of the type-specifier.  For a
9190    class-specifier, enum-specifier, or elaborated-type-specifier, a
9191    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9192
9193    If IS_FRIEND is TRUE then this type-specifier is being declared a
9194    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
9195    appearing in a decl-specifier-seq.
9196
9197    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9198    class-specifier, enum-specifier, or elaborated-type-specifier, then
9199    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9200    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9201    zero.
9202
9203    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9204    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9205    is set to FALSE.  */
9206
9207 static tree
9208 cp_parser_type_specifier (cp_parser* parser,
9209                           cp_parser_flags flags,
9210                           cp_decl_specifier_seq *decl_specs,
9211                           bool is_declaration,
9212                           int* declares_class_or_enum,
9213                           bool* is_cv_qualifier)
9214 {
9215   tree type_spec = NULL_TREE;
9216   cp_token *token;
9217   enum rid keyword;
9218   cp_decl_spec ds = ds_last;
9219
9220   /* Assume this type-specifier does not declare a new type.  */
9221   if (declares_class_or_enum)
9222     *declares_class_or_enum = 0;
9223   /* And that it does not specify a cv-qualifier.  */
9224   if (is_cv_qualifier)
9225     *is_cv_qualifier = false;
9226   /* Peek at the next token.  */
9227   token = cp_lexer_peek_token (parser->lexer);
9228
9229   /* If we're looking at a keyword, we can use that to guide the
9230      production we choose.  */
9231   keyword = token->keyword;
9232   switch (keyword)
9233     {
9234       /* Any of these indicate either a class-specifier, or an
9235          elaborated-type-specifier.  */
9236     case RID_CLASS:
9237     case RID_STRUCT:
9238     case RID_UNION:
9239     case RID_ENUM:
9240       /* Parse tentatively so that we can back up if we don't find a
9241          class-specifier or enum-specifier.  */
9242       cp_parser_parse_tentatively (parser);
9243       /* Look for the class-specifier or enum-specifier.  */
9244       if (keyword == RID_ENUM)
9245         type_spec = cp_parser_enum_specifier (parser);
9246       else
9247         type_spec = cp_parser_class_specifier (parser);
9248
9249       /* If that worked, we're done.  */
9250       if (cp_parser_parse_definitely (parser))
9251         {
9252           if (declares_class_or_enum)
9253             *declares_class_or_enum = 2;
9254           if (decl_specs)
9255             cp_parser_set_decl_spec_type (decl_specs,
9256                                           type_spec,
9257                                           /*user_defined_p=*/true);
9258           return type_spec;
9259         }
9260
9261       /* Fall through.  */
9262
9263     case RID_TYPENAME:
9264       /* Look for an elaborated-type-specifier.  */
9265       type_spec
9266         = (cp_parser_elaborated_type_specifier
9267            (parser,
9268             decl_specs && decl_specs->specs[(int) ds_friend],
9269             is_declaration));
9270       /* We're declaring a class or enum -- unless we're using
9271          `typename'.  */
9272       if (declares_class_or_enum && keyword != RID_TYPENAME)
9273         *declares_class_or_enum = 1;
9274       if (decl_specs)
9275         cp_parser_set_decl_spec_type (decl_specs,
9276                                       type_spec,
9277                                       /*user_defined_p=*/true);
9278       return type_spec;
9279
9280     case RID_CONST:
9281       ds = ds_const;
9282       if (is_cv_qualifier)
9283         *is_cv_qualifier = true;
9284       break;
9285
9286     case RID_VOLATILE:
9287       ds = ds_volatile;
9288       if (is_cv_qualifier)
9289         *is_cv_qualifier = true;
9290       break;
9291
9292     case RID_RESTRICT:
9293       ds = ds_restrict;
9294       if (is_cv_qualifier)
9295         *is_cv_qualifier = true;
9296       break;
9297
9298     case RID_COMPLEX:
9299       /* The `__complex__' keyword is a GNU extension.  */
9300       ds = ds_complex;
9301       break;
9302
9303     default:
9304       break;
9305     }
9306
9307   /* Handle simple keywords.  */
9308   if (ds != ds_last)
9309     {
9310       if (decl_specs)
9311         {
9312           ++decl_specs->specs[(int)ds];
9313           decl_specs->any_specifiers_p = true;
9314         }
9315       return cp_lexer_consume_token (parser->lexer)->value;
9316     }
9317
9318   /* If we do not already have a type-specifier, assume we are looking
9319      at a simple-type-specifier.  */
9320   type_spec = cp_parser_simple_type_specifier (parser,
9321                                                decl_specs,
9322                                                flags);
9323
9324   /* If we didn't find a type-specifier, and a type-specifier was not
9325      optional in this context, issue an error message.  */
9326   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9327     {
9328       cp_parser_error (parser, "expected type specifier");
9329       return error_mark_node;
9330     }
9331
9332   return type_spec;
9333 }
9334
9335 /* Parse a simple-type-specifier.
9336
9337    simple-type-specifier:
9338      :: [opt] nested-name-specifier [opt] type-name
9339      :: [opt] nested-name-specifier template template-id
9340      char
9341      wchar_t
9342      bool
9343      short
9344      int
9345      long
9346      signed
9347      unsigned
9348      float
9349      double
9350      void
9351
9352    GNU Extension:
9353
9354    simple-type-specifier:
9355      __typeof__ unary-expression
9356      __typeof__ ( type-id )
9357
9358    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9359    appropriately updated.  */
9360
9361 static tree
9362 cp_parser_simple_type_specifier (cp_parser* parser,
9363                                  cp_decl_specifier_seq *decl_specs,
9364                                  cp_parser_flags flags)
9365 {
9366   tree type = NULL_TREE;
9367   cp_token *token;
9368
9369   /* Peek at the next token.  */
9370   token = cp_lexer_peek_token (parser->lexer);
9371
9372   /* If we're looking at a keyword, things are easy.  */
9373   switch (token->keyword)
9374     {
9375     case RID_CHAR:
9376       if (decl_specs)
9377         decl_specs->explicit_char_p = true;
9378       type = char_type_node;
9379       break;
9380     case RID_WCHAR:
9381       type = wchar_type_node;
9382       break;
9383     case RID_BOOL:
9384       type = boolean_type_node;
9385       break;
9386     case RID_SHORT:
9387       if (decl_specs)
9388         ++decl_specs->specs[(int) ds_short];
9389       type = short_integer_type_node;
9390       break;
9391     case RID_INT:
9392       if (decl_specs)
9393         decl_specs->explicit_int_p = true;
9394       type = integer_type_node;
9395       break;
9396     case RID_LONG:
9397       if (decl_specs)
9398         ++decl_specs->specs[(int) ds_long];
9399       type = long_integer_type_node;
9400       break;
9401     case RID_SIGNED:
9402       if (decl_specs)
9403         ++decl_specs->specs[(int) ds_signed];
9404       type = integer_type_node;
9405       break;
9406     case RID_UNSIGNED:
9407       if (decl_specs)
9408         ++decl_specs->specs[(int) ds_unsigned];
9409       type = unsigned_type_node;
9410       break;
9411     case RID_FLOAT:
9412       type = float_type_node;
9413       break;
9414     case RID_DOUBLE:
9415       type = double_type_node;
9416       break;
9417     case RID_VOID:
9418       type = void_type_node;
9419       break;
9420
9421     case RID_TYPEOF:
9422       /* Consume the `typeof' token.  */
9423       cp_lexer_consume_token (parser->lexer);
9424       /* Parse the operand to `typeof'.  */
9425       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9426       /* If it is not already a TYPE, take its type.  */
9427       if (!TYPE_P (type))
9428         type = finish_typeof (type);
9429
9430       if (decl_specs)
9431         cp_parser_set_decl_spec_type (decl_specs, type,
9432                                       /*user_defined_p=*/true);
9433
9434       return type;
9435
9436     default:
9437       break;
9438     }
9439
9440   /* If the type-specifier was for a built-in type, we're done.  */
9441   if (type)
9442     {
9443       tree id;
9444
9445       /* Record the type.  */
9446       if (decl_specs
9447           && (token->keyword != RID_SIGNED
9448               && token->keyword != RID_UNSIGNED
9449               && token->keyword != RID_SHORT
9450               && token->keyword != RID_LONG))
9451         cp_parser_set_decl_spec_type (decl_specs,
9452                                       type,
9453                                       /*user_defined=*/false);
9454       if (decl_specs)
9455         decl_specs->any_specifiers_p = true;
9456
9457       /* Consume the token.  */
9458       id = cp_lexer_consume_token (parser->lexer)->value;
9459
9460       /* There is no valid C++ program where a non-template type is
9461          followed by a "<".  That usually indicates that the user thought
9462          that the type was a template.  */
9463       cp_parser_check_for_invalid_template_id (parser, type);
9464
9465       return TYPE_NAME (type);
9466     }
9467
9468   /* The type-specifier must be a user-defined type.  */
9469   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9470     {
9471       bool qualified_p;
9472       bool global_p;
9473
9474       /* Don't gobble tokens or issue error messages if this is an
9475          optional type-specifier.  */
9476       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9477         cp_parser_parse_tentatively (parser);
9478
9479       /* Look for the optional `::' operator.  */
9480       global_p
9481         = (cp_parser_global_scope_opt (parser,
9482                                        /*current_scope_valid_p=*/false)
9483            != NULL_TREE);
9484       /* Look for the nested-name specifier.  */
9485       qualified_p
9486         = (cp_parser_nested_name_specifier_opt (parser,
9487                                                 /*typename_keyword_p=*/false,
9488                                                 /*check_dependency_p=*/true,
9489                                                 /*type_p=*/false,
9490                                                 /*is_declaration=*/false)
9491            != NULL_TREE);
9492       /* If we have seen a nested-name-specifier, and the next token
9493          is `template', then we are using the template-id production.  */
9494       if (parser->scope
9495           && cp_parser_optional_template_keyword (parser))
9496         {
9497           /* Look for the template-id.  */
9498           type = cp_parser_template_id (parser,
9499                                         /*template_keyword_p=*/true,
9500                                         /*check_dependency_p=*/true,
9501                                         /*is_declaration=*/false);
9502           /* If the template-id did not name a type, we are out of
9503              luck.  */
9504           if (TREE_CODE (type) != TYPE_DECL)
9505             {
9506               cp_parser_error (parser, "expected template-id for type");
9507               type = NULL_TREE;
9508             }
9509         }
9510       /* Otherwise, look for a type-name.  */
9511       else
9512         type = cp_parser_type_name (parser);
9513       /* Keep track of all name-lookups performed in class scopes.  */
9514       if (type
9515           && !global_p
9516           && !qualified_p
9517           && TREE_CODE (type) == TYPE_DECL
9518           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9519         maybe_note_name_used_in_class (DECL_NAME (type), type);
9520       /* If it didn't work out, we don't have a TYPE.  */
9521       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9522           && !cp_parser_parse_definitely (parser))
9523         type = NULL_TREE;
9524       if (type && decl_specs)
9525         cp_parser_set_decl_spec_type (decl_specs, type,
9526                                       /*user_defined=*/true);
9527     }
9528
9529   /* If we didn't get a type-name, issue an error message.  */
9530   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9531     {
9532       cp_parser_error (parser, "expected type-name");
9533       return error_mark_node;
9534     }
9535
9536   /* There is no valid C++ program where a non-template type is
9537      followed by a "<".  That usually indicates that the user thought
9538      that the type was a template.  */
9539   if (type && type != error_mark_node)
9540     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9541
9542   return type;
9543 }
9544
9545 /* Parse a type-name.
9546
9547    type-name:
9548      class-name
9549      enum-name
9550      typedef-name
9551
9552    enum-name:
9553      identifier
9554
9555    typedef-name:
9556      identifier
9557
9558    Returns a TYPE_DECL for the the type.  */
9559
9560 static tree
9561 cp_parser_type_name (cp_parser* parser)
9562 {
9563   tree type_decl;
9564   tree identifier;
9565
9566   /* We can't know yet whether it is a class-name or not.  */
9567   cp_parser_parse_tentatively (parser);
9568   /* Try a class-name.  */
9569   type_decl = cp_parser_class_name (parser,
9570                                     /*typename_keyword_p=*/false,
9571                                     /*template_keyword_p=*/false,
9572                                     /*type_p=*/false,
9573                                     /*check_dependency_p=*/true,
9574                                     /*class_head_p=*/false,
9575                                     /*is_declaration=*/false);
9576   /* If it's not a class-name, keep looking.  */
9577   if (!cp_parser_parse_definitely (parser))
9578     {
9579       /* It must be a typedef-name or an enum-name.  */
9580       identifier = cp_parser_identifier (parser);
9581       if (identifier == error_mark_node)
9582         return error_mark_node;
9583
9584       /* Look up the type-name.  */
9585       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9586       /* Issue an error if we did not find a type-name.  */
9587       if (TREE_CODE (type_decl) != TYPE_DECL)
9588         {
9589           if (!cp_parser_simulate_error (parser))
9590             cp_parser_name_lookup_error (parser, identifier, type_decl,
9591                                          "is not a type");
9592           type_decl = error_mark_node;
9593         }
9594       /* Remember that the name was used in the definition of the
9595          current class so that we can check later to see if the
9596          meaning would have been different after the class was
9597          entirely defined.  */
9598       else if (type_decl != error_mark_node
9599                && !parser->scope)
9600         maybe_note_name_used_in_class (identifier, type_decl);
9601     }
9602
9603   return type_decl;
9604 }
9605
9606
9607 /* Parse an elaborated-type-specifier.  Note that the grammar given
9608    here incorporates the resolution to DR68.
9609
9610    elaborated-type-specifier:
9611      class-key :: [opt] nested-name-specifier [opt] identifier
9612      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9613      enum :: [opt] nested-name-specifier [opt] identifier
9614      typename :: [opt] nested-name-specifier identifier
9615      typename :: [opt] nested-name-specifier template [opt]
9616        template-id
9617
9618    GNU extension:
9619
9620    elaborated-type-specifier:
9621      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9622      class-key attributes :: [opt] nested-name-specifier [opt]
9623                template [opt] template-id
9624      enum attributes :: [opt] nested-name-specifier [opt] identifier
9625
9626    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9627    declared `friend'.  If IS_DECLARATION is TRUE, then this
9628    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9629    something is being declared.
9630
9631    Returns the TYPE specified.  */
9632
9633 static tree
9634 cp_parser_elaborated_type_specifier (cp_parser* parser,
9635                                      bool is_friend,
9636                                      bool is_declaration)
9637 {
9638   enum tag_types tag_type;
9639   tree identifier;
9640   tree type = NULL_TREE;
9641   tree attributes = NULL_TREE;
9642
9643   /* See if we're looking at the `enum' keyword.  */
9644   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9645     {
9646       /* Consume the `enum' token.  */
9647       cp_lexer_consume_token (parser->lexer);
9648       /* Remember that it's an enumeration type.  */
9649       tag_type = enum_type;
9650       /* Parse the attributes.  */
9651       attributes = cp_parser_attributes_opt (parser);
9652     }
9653   /* Or, it might be `typename'.  */
9654   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9655                                            RID_TYPENAME))
9656     {
9657       /* Consume the `typename' token.  */
9658       cp_lexer_consume_token (parser->lexer);
9659       /* Remember that it's a `typename' type.  */
9660       tag_type = typename_type;
9661       /* The `typename' keyword is only allowed in templates.  */
9662       if (!processing_template_decl)
9663         pedwarn ("using `typename' outside of template");
9664     }
9665   /* Otherwise it must be a class-key.  */
9666   else
9667     {
9668       tag_type = cp_parser_class_key (parser);
9669       if (tag_type == none_type)
9670         return error_mark_node;
9671       /* Parse the attributes.  */
9672       attributes = cp_parser_attributes_opt (parser);
9673     }
9674
9675   /* Look for the `::' operator.  */
9676   cp_parser_global_scope_opt (parser,
9677                               /*current_scope_valid_p=*/false);
9678   /* Look for the nested-name-specifier.  */
9679   if (tag_type == typename_type)
9680     {
9681       if (cp_parser_nested_name_specifier (parser,
9682                                            /*typename_keyword_p=*/true,
9683                                            /*check_dependency_p=*/true,
9684                                            /*type_p=*/true,
9685                                            is_declaration)
9686           == error_mark_node)
9687         return error_mark_node;
9688     }
9689   else
9690     /* Even though `typename' is not present, the proposed resolution
9691        to Core Issue 180 says that in `class A<T>::B', `B' should be
9692        considered a type-name, even if `A<T>' is dependent.  */
9693     cp_parser_nested_name_specifier_opt (parser,
9694                                          /*typename_keyword_p=*/true,
9695                                          /*check_dependency_p=*/true,
9696                                          /*type_p=*/true,
9697                                          is_declaration);
9698   /* For everything but enumeration types, consider a template-id.  */
9699   if (tag_type != enum_type)
9700     {
9701       bool template_p = false;
9702       tree decl;
9703
9704       /* Allow the `template' keyword.  */
9705       template_p = cp_parser_optional_template_keyword (parser);
9706       /* If we didn't see `template', we don't know if there's a
9707          template-id or not.  */
9708       if (!template_p)
9709         cp_parser_parse_tentatively (parser);
9710       /* Parse the template-id.  */
9711       decl = cp_parser_template_id (parser, template_p,
9712                                     /*check_dependency_p=*/true,
9713                                     is_declaration);
9714       /* If we didn't find a template-id, look for an ordinary
9715          identifier.  */
9716       if (!template_p && !cp_parser_parse_definitely (parser))
9717         ;
9718       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9719          in effect, then we must assume that, upon instantiation, the
9720          template will correspond to a class.  */
9721       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9722                && tag_type == typename_type)
9723         type = make_typename_type (parser->scope, decl,
9724                                    /*complain=*/1);
9725       else
9726         type = TREE_TYPE (decl);
9727     }
9728
9729   /* For an enumeration type, consider only a plain identifier.  */
9730   if (!type)
9731     {
9732       identifier = cp_parser_identifier (parser);
9733
9734       if (identifier == error_mark_node)
9735         {
9736           parser->scope = NULL_TREE;
9737           return error_mark_node;
9738         }
9739
9740       /* For a `typename', we needn't call xref_tag.  */
9741       if (tag_type == typename_type)
9742         return cp_parser_make_typename_type (parser, parser->scope,
9743                                              identifier);
9744       /* Look up a qualified name in the usual way.  */
9745       if (parser->scope)
9746         {
9747           tree decl;
9748
9749           /* In an elaborated-type-specifier, names are assumed to name
9750              types, so we set IS_TYPE to TRUE when calling
9751              cp_parser_lookup_name.  */
9752           decl = cp_parser_lookup_name (parser, identifier,
9753                                         /*is_type=*/true,
9754                                         /*is_template=*/false,
9755                                         /*is_namespace=*/false,
9756                                         /*check_dependency=*/true);
9757
9758           /* If we are parsing friend declaration, DECL may be a
9759              TEMPLATE_DECL tree node here.  However, we need to check
9760              whether this TEMPLATE_DECL results in valid code.  Consider
9761              the following example:
9762
9763                namespace N {
9764                  template <class T> class C {};
9765                }
9766                class X {
9767                  template <class T> friend class N::C; // #1, valid code
9768                };
9769                template <class T> class Y {
9770                  friend class N::C;                    // #2, invalid code
9771                };
9772
9773              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9774              name lookup of `N::C'.  We see that friend declaration must
9775              be template for the code to be valid.  Note that
9776              processing_template_decl does not work here since it is
9777              always 1 for the above two cases.  */
9778
9779           decl = (cp_parser_maybe_treat_template_as_class
9780                   (decl, /*tag_name_p=*/is_friend
9781                          && parser->num_template_parameter_lists));
9782
9783           if (TREE_CODE (decl) != TYPE_DECL)
9784             {
9785               error ("expected type-name");
9786               return error_mark_node;
9787             }
9788
9789           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9790             check_elaborated_type_specifier
9791               (tag_type, decl,
9792                (parser->num_template_parameter_lists
9793                 || DECL_SELF_REFERENCE_P (decl)));
9794
9795           type = TREE_TYPE (decl);
9796         }
9797       else
9798         {
9799           /* An elaborated-type-specifier sometimes introduces a new type and
9800              sometimes names an existing type.  Normally, the rule is that it
9801              introduces a new type only if there is not an existing type of
9802              the same name already in scope.  For example, given:
9803
9804                struct S {};
9805                void f() { struct S s; }
9806
9807              the `struct S' in the body of `f' is the same `struct S' as in
9808              the global scope; the existing definition is used.  However, if
9809              there were no global declaration, this would introduce a new
9810              local class named `S'.
9811
9812              An exception to this rule applies to the following code:
9813
9814                namespace N { struct S; }
9815
9816              Here, the elaborated-type-specifier names a new type
9817              unconditionally; even if there is already an `S' in the
9818              containing scope this declaration names a new type.
9819              This exception only applies if the elaborated-type-specifier
9820              forms the complete declaration:
9821
9822                [class.name]
9823
9824                A declaration consisting solely of `class-key identifier ;' is
9825                either a redeclaration of the name in the current scope or a
9826                forward declaration of the identifier as a class name.  It
9827                introduces the name into the current scope.
9828
9829              We are in this situation precisely when the next token is a `;'.
9830
9831              An exception to the exception is that a `friend' declaration does
9832              *not* name a new type; i.e., given:
9833
9834                struct S { friend struct T; };
9835
9836              `T' is not a new type in the scope of `S'.
9837
9838              Also, `new struct S' or `sizeof (struct S)' never results in the
9839              definition of a new type; a new type can only be declared in a
9840              declaration context.  */
9841
9842           /* Warn about attributes. They are ignored.  */
9843           if (attributes)
9844             warning ("type attributes are honored only at type definition");
9845
9846           type = xref_tag (tag_type, identifier,
9847                            (is_friend
9848                             || !is_declaration
9849                             || cp_lexer_next_token_is_not (parser->lexer,
9850                                                            CPP_SEMICOLON)),
9851                            parser->num_template_parameter_lists);
9852         }
9853     }
9854   if (tag_type != enum_type)
9855     cp_parser_check_class_key (tag_type, type);
9856
9857   /* A "<" cannot follow an elaborated type specifier.  If that
9858      happens, the user was probably trying to form a template-id.  */
9859   cp_parser_check_for_invalid_template_id (parser, type);
9860
9861   return type;
9862 }
9863
9864 /* Parse an enum-specifier.
9865
9866    enum-specifier:
9867      enum identifier [opt] { enumerator-list [opt] }
9868
9869    Returns an ENUM_TYPE representing the enumeration.  */
9870
9871 static tree
9872 cp_parser_enum_specifier (cp_parser* parser)
9873 {
9874   cp_token *token;
9875   tree identifier = NULL_TREE;
9876   tree type;
9877
9878   /* Look for the `enum' keyword.  */
9879   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9880     return error_mark_node;
9881   /* Peek at the next token.  */
9882   token = cp_lexer_peek_token (parser->lexer);
9883
9884   /* See if it is an identifier.  */
9885   if (token->type == CPP_NAME)
9886     identifier = cp_parser_identifier (parser);
9887
9888   /* Look for the `{'.  */
9889   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9890     return error_mark_node;
9891
9892   /* At this point, we're going ahead with the enum-specifier, even
9893      if some other problem occurs.  */
9894   cp_parser_commit_to_tentative_parse (parser);
9895
9896   /* Issue an error message if type-definitions are forbidden here.  */
9897   cp_parser_check_type_definition (parser);
9898
9899   /* Create the new type.  */
9900   type = start_enum (identifier ? identifier : make_anon_name ());
9901
9902   /* Peek at the next token.  */
9903   token = cp_lexer_peek_token (parser->lexer);
9904   /* If it's not a `}', then there are some enumerators.  */
9905   if (token->type != CPP_CLOSE_BRACE)
9906     cp_parser_enumerator_list (parser, type);
9907   /* Look for the `}'.  */
9908   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9909
9910   /* Finish up the enumeration.  */
9911   finish_enum (type);
9912
9913   return type;
9914 }
9915
9916 /* Parse an enumerator-list.  The enumerators all have the indicated
9917    TYPE.
9918
9919    enumerator-list:
9920      enumerator-definition
9921      enumerator-list , enumerator-definition  */
9922
9923 static void
9924 cp_parser_enumerator_list (cp_parser* parser, tree type)
9925 {
9926   while (true)
9927     {
9928       cp_token *token;
9929
9930       /* Parse an enumerator-definition.  */
9931       cp_parser_enumerator_definition (parser, type);
9932       /* Peek at the next token.  */
9933       token = cp_lexer_peek_token (parser->lexer);
9934       /* If it's not a `,', then we've reached the end of the
9935          list.  */
9936       if (token->type != CPP_COMMA)
9937         break;
9938       /* Otherwise, consume the `,' and keep going.  */
9939       cp_lexer_consume_token (parser->lexer);
9940       /* If the next token is a `}', there is a trailing comma.  */
9941       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9942         {
9943           if (pedantic && !in_system_header)
9944             pedwarn ("comma at end of enumerator list");
9945           break;
9946         }
9947     }
9948 }
9949
9950 /* Parse an enumerator-definition.  The enumerator has the indicated
9951    TYPE.
9952
9953    enumerator-definition:
9954      enumerator
9955      enumerator = constant-expression
9956
9957    enumerator:
9958      identifier  */
9959
9960 static void
9961 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9962 {
9963   cp_token *token;
9964   tree identifier;
9965   tree value;
9966
9967   /* Look for the identifier.  */
9968   identifier = cp_parser_identifier (parser);
9969   if (identifier == error_mark_node)
9970     return;
9971
9972   /* Peek at the next token.  */
9973   token = cp_lexer_peek_token (parser->lexer);
9974   /* If it's an `=', then there's an explicit value.  */
9975   if (token->type == CPP_EQ)
9976     {
9977       /* Consume the `=' token.  */
9978       cp_lexer_consume_token (parser->lexer);
9979       /* Parse the value.  */
9980       value = cp_parser_constant_expression (parser,
9981                                              /*allow_non_constant_p=*/false,
9982                                              NULL);
9983     }
9984   else
9985     value = NULL_TREE;
9986
9987   /* Create the enumerator.  */
9988   build_enumerator (identifier, value, type);
9989 }
9990
9991 /* Parse a namespace-name.
9992
9993    namespace-name:
9994      original-namespace-name
9995      namespace-alias
9996
9997    Returns the NAMESPACE_DECL for the namespace.  */
9998
9999 static tree
10000 cp_parser_namespace_name (cp_parser* parser)
10001 {
10002   tree identifier;
10003   tree namespace_decl;
10004
10005   /* Get the name of the namespace.  */
10006   identifier = cp_parser_identifier (parser);
10007   if (identifier == error_mark_node)
10008     return error_mark_node;
10009
10010   /* Look up the identifier in the currently active scope.  Look only
10011      for namespaces, due to:
10012
10013        [basic.lookup.udir]
10014
10015        When looking up a namespace-name in a using-directive or alias
10016        definition, only namespace names are considered.
10017
10018      And:
10019
10020        [basic.lookup.qual]
10021
10022        During the lookup of a name preceding the :: scope resolution
10023        operator, object, function, and enumerator names are ignored.
10024
10025      (Note that cp_parser_class_or_namespace_name only calls this
10026      function if the token after the name is the scope resolution
10027      operator.)  */
10028   namespace_decl = cp_parser_lookup_name (parser, identifier,
10029                                           /*is_type=*/false,
10030                                           /*is_template=*/false,
10031                                           /*is_namespace=*/true,
10032                                           /*check_dependency=*/true);
10033   /* If it's not a namespace, issue an error.  */
10034   if (namespace_decl == error_mark_node
10035       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10036     {
10037       cp_parser_error (parser, "expected namespace-name");
10038       namespace_decl = error_mark_node;
10039     }
10040
10041   return namespace_decl;
10042 }
10043
10044 /* Parse a namespace-definition.
10045
10046    namespace-definition:
10047      named-namespace-definition
10048      unnamed-namespace-definition
10049
10050    named-namespace-definition:
10051      original-namespace-definition
10052      extension-namespace-definition
10053
10054    original-namespace-definition:
10055      namespace identifier { namespace-body }
10056
10057    extension-namespace-definition:
10058      namespace original-namespace-name { namespace-body }
10059
10060    unnamed-namespace-definition:
10061      namespace { namespace-body } */
10062
10063 static void
10064 cp_parser_namespace_definition (cp_parser* parser)
10065 {
10066   tree identifier;
10067
10068   /* Look for the `namespace' keyword.  */
10069   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10070
10071   /* Get the name of the namespace.  We do not attempt to distinguish
10072      between an original-namespace-definition and an
10073      extension-namespace-definition at this point.  The semantic
10074      analysis routines are responsible for that.  */
10075   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10076     identifier = cp_parser_identifier (parser);
10077   else
10078     identifier = NULL_TREE;
10079
10080   /* Look for the `{' to start the namespace.  */
10081   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10082   /* Start the namespace.  */
10083   push_namespace (identifier);
10084   /* Parse the body of the namespace.  */
10085   cp_parser_namespace_body (parser);
10086   /* Finish the namespace.  */
10087   pop_namespace ();
10088   /* Look for the final `}'.  */
10089   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10090 }
10091
10092 /* Parse a namespace-body.
10093
10094    namespace-body:
10095      declaration-seq [opt]  */
10096
10097 static void
10098 cp_parser_namespace_body (cp_parser* parser)
10099 {
10100   cp_parser_declaration_seq_opt (parser);
10101 }
10102
10103 /* Parse a namespace-alias-definition.
10104
10105    namespace-alias-definition:
10106      namespace identifier = qualified-namespace-specifier ;  */
10107
10108 static void
10109 cp_parser_namespace_alias_definition (cp_parser* parser)
10110 {
10111   tree identifier;
10112   tree namespace_specifier;
10113
10114   /* Look for the `namespace' keyword.  */
10115   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10116   /* Look for the identifier.  */
10117   identifier = cp_parser_identifier (parser);
10118   if (identifier == error_mark_node)
10119     return;
10120   /* Look for the `=' token.  */
10121   cp_parser_require (parser, CPP_EQ, "`='");
10122   /* Look for the qualified-namespace-specifier.  */
10123   namespace_specifier
10124     = cp_parser_qualified_namespace_specifier (parser);
10125   /* Look for the `;' token.  */
10126   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10127
10128   /* Register the alias in the symbol table.  */
10129   do_namespace_alias (identifier, namespace_specifier);
10130 }
10131
10132 /* Parse a qualified-namespace-specifier.
10133
10134    qualified-namespace-specifier:
10135      :: [opt] nested-name-specifier [opt] namespace-name
10136
10137    Returns a NAMESPACE_DECL corresponding to the specified
10138    namespace.  */
10139
10140 static tree
10141 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10142 {
10143   /* Look for the optional `::'.  */
10144   cp_parser_global_scope_opt (parser,
10145                               /*current_scope_valid_p=*/false);
10146
10147   /* Look for the optional nested-name-specifier.  */
10148   cp_parser_nested_name_specifier_opt (parser,
10149                                        /*typename_keyword_p=*/false,
10150                                        /*check_dependency_p=*/true,
10151                                        /*type_p=*/false,
10152                                        /*is_declaration=*/true);
10153
10154   return cp_parser_namespace_name (parser);
10155 }
10156
10157 /* Parse a using-declaration.
10158
10159    using-declaration:
10160      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10161      using :: unqualified-id ;  */
10162
10163 static void
10164 cp_parser_using_declaration (cp_parser* parser)
10165 {
10166   cp_token *token;
10167   bool typename_p = false;
10168   bool global_scope_p;
10169   tree decl;
10170   tree identifier;
10171   tree scope;
10172   tree qscope;
10173
10174   /* Look for the `using' keyword.  */
10175   cp_parser_require_keyword (parser, RID_USING, "`using'");
10176
10177   /* Peek at the next token.  */
10178   token = cp_lexer_peek_token (parser->lexer);
10179   /* See if it's `typename'.  */
10180   if (token->keyword == RID_TYPENAME)
10181     {
10182       /* Remember that we've seen it.  */
10183       typename_p = true;
10184       /* Consume the `typename' token.  */
10185       cp_lexer_consume_token (parser->lexer);
10186     }
10187
10188   /* Look for the optional global scope qualification.  */
10189   global_scope_p
10190     = (cp_parser_global_scope_opt (parser,
10191                                    /*current_scope_valid_p=*/false)
10192        != NULL_TREE);
10193
10194   /* If we saw `typename', or didn't see `::', then there must be a
10195      nested-name-specifier present.  */
10196   if (typename_p || !global_scope_p)
10197     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10198                                               /*check_dependency_p=*/true,
10199                                               /*type_p=*/false,
10200                                               /*is_declaration=*/true);
10201   /* Otherwise, we could be in either of the two productions.  In that
10202      case, treat the nested-name-specifier as optional.  */
10203   else
10204     qscope = cp_parser_nested_name_specifier_opt (parser,
10205                                                   /*typename_keyword_p=*/false,
10206                                                   /*check_dependency_p=*/true,
10207                                                   /*type_p=*/false,
10208                                                   /*is_declaration=*/true);
10209   if (!qscope)
10210     qscope = global_namespace;
10211
10212   /* Parse the unqualified-id.  */
10213   identifier = cp_parser_unqualified_id (parser,
10214                                          /*template_keyword_p=*/false,
10215                                          /*check_dependency_p=*/true,
10216                                          /*declarator_p=*/true);
10217
10218   /* The function we call to handle a using-declaration is different
10219      depending on what scope we are in.  */
10220   if (identifier == error_mark_node)
10221     ;
10222   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10223            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10224     /* [namespace.udecl]
10225
10226        A using declaration shall not name a template-id.  */
10227     error ("a template-id may not appear in a using-declaration");
10228   else
10229     {
10230       scope = current_scope ();
10231       if (scope && TYPE_P (scope))
10232         {
10233           /* Create the USING_DECL.  */
10234           decl = do_class_using_decl (build_nt (SCOPE_REF,
10235                                                 parser->scope,
10236                                                 identifier));
10237           /* Add it to the list of members in this class.  */
10238           finish_member_declaration (decl);
10239         }
10240       else
10241         {
10242           decl = cp_parser_lookup_name_simple (parser, identifier);
10243           if (decl == error_mark_node)
10244             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10245           else if (scope)
10246             do_local_using_decl (decl, qscope, identifier);
10247           else
10248             do_toplevel_using_decl (decl, qscope, identifier);
10249         }
10250     }
10251
10252   /* Look for the final `;'.  */
10253   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10254 }
10255
10256 /* Parse a using-directive.
10257
10258    using-directive:
10259      using namespace :: [opt] nested-name-specifier [opt]
10260        namespace-name ;  */
10261
10262 static void
10263 cp_parser_using_directive (cp_parser* parser)
10264 {
10265   tree namespace_decl;
10266   tree attribs;
10267
10268   /* Look for the `using' keyword.  */
10269   cp_parser_require_keyword (parser, RID_USING, "`using'");
10270   /* And the `namespace' keyword.  */
10271   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10272   /* Look for the optional `::' operator.  */
10273   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10274   /* And the optional nested-name-specifier.  */
10275   cp_parser_nested_name_specifier_opt (parser,
10276                                        /*typename_keyword_p=*/false,
10277                                        /*check_dependency_p=*/true,
10278                                        /*type_p=*/false,
10279                                        /*is_declaration=*/true);
10280   /* Get the namespace being used.  */
10281   namespace_decl = cp_parser_namespace_name (parser);
10282   /* And any specified attributes.  */
10283   attribs = cp_parser_attributes_opt (parser);
10284   /* Update the symbol table.  */
10285   parse_using_directive (namespace_decl, attribs);
10286   /* Look for the final `;'.  */
10287   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10288 }
10289
10290 /* Parse an asm-definition.
10291
10292    asm-definition:
10293      asm ( string-literal ) ;
10294
10295    GNU Extension:
10296
10297    asm-definition:
10298      asm volatile [opt] ( string-literal ) ;
10299      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10300      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10301                           : asm-operand-list [opt] ) ;
10302      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10303                           : asm-operand-list [opt]
10304                           : asm-operand-list [opt] ) ;  */
10305
10306 static void
10307 cp_parser_asm_definition (cp_parser* parser)
10308 {
10309   cp_token *token;
10310   tree string;
10311   tree outputs = NULL_TREE;
10312   tree inputs = NULL_TREE;
10313   tree clobbers = NULL_TREE;
10314   tree asm_stmt;
10315   bool volatile_p = false;
10316   bool extended_p = false;
10317
10318   /* Look for the `asm' keyword.  */
10319   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10320   /* See if the next token is `volatile'.  */
10321   if (cp_parser_allow_gnu_extensions_p (parser)
10322       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10323     {
10324       /* Remember that we saw the `volatile' keyword.  */
10325       volatile_p = true;
10326       /* Consume the token.  */
10327       cp_lexer_consume_token (parser->lexer);
10328     }
10329   /* Look for the opening `('.  */
10330   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10331   /* Look for the string.  */
10332   c_lex_string_translate = 0;
10333   token = cp_parser_require (parser, CPP_STRING, "asm body");
10334   if (!token)
10335     goto finish;
10336   string = token->value;
10337   /* If we're allowing GNU extensions, check for the extended assembly
10338      syntax.  Unfortunately, the `:' tokens need not be separated by
10339      a space in C, and so, for compatibility, we tolerate that here
10340      too.  Doing that means that we have to treat the `::' operator as
10341      two `:' tokens.  */
10342   if (cp_parser_allow_gnu_extensions_p (parser)
10343       && at_function_scope_p ()
10344       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10345           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10346     {
10347       bool inputs_p = false;
10348       bool clobbers_p = false;
10349
10350       /* The extended syntax was used.  */
10351       extended_p = true;
10352
10353       /* Look for outputs.  */
10354       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10355         {
10356           /* Consume the `:'.  */
10357           cp_lexer_consume_token (parser->lexer);
10358           /* Parse the output-operands.  */
10359           if (cp_lexer_next_token_is_not (parser->lexer,
10360                                           CPP_COLON)
10361               && cp_lexer_next_token_is_not (parser->lexer,
10362                                              CPP_SCOPE)
10363               && cp_lexer_next_token_is_not (parser->lexer,
10364                                              CPP_CLOSE_PAREN))
10365             outputs = cp_parser_asm_operand_list (parser);
10366         }
10367       /* If the next token is `::', there are no outputs, and the
10368          next token is the beginning of the inputs.  */
10369       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10370         {
10371           /* Consume the `::' token.  */
10372           cp_lexer_consume_token (parser->lexer);
10373           /* The inputs are coming next.  */
10374           inputs_p = true;
10375         }
10376
10377       /* Look for inputs.  */
10378       if (inputs_p
10379           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10380         {
10381           if (!inputs_p)
10382             /* Consume the `:'.  */
10383             cp_lexer_consume_token (parser->lexer);
10384           /* Parse the output-operands.  */
10385           if (cp_lexer_next_token_is_not (parser->lexer,
10386                                           CPP_COLON)
10387               && cp_lexer_next_token_is_not (parser->lexer,
10388                                              CPP_SCOPE)
10389               && cp_lexer_next_token_is_not (parser->lexer,
10390                                              CPP_CLOSE_PAREN))
10391             inputs = cp_parser_asm_operand_list (parser);
10392         }
10393       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10394         /* The clobbers are coming next.  */
10395         clobbers_p = true;
10396
10397       /* Look for clobbers.  */
10398       if (clobbers_p
10399           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10400         {
10401           if (!clobbers_p)
10402             /* Consume the `:'.  */
10403             cp_lexer_consume_token (parser->lexer);
10404           /* Parse the clobbers.  */
10405           if (cp_lexer_next_token_is_not (parser->lexer,
10406                                           CPP_CLOSE_PAREN))
10407             clobbers = cp_parser_asm_clobber_list (parser);
10408         }
10409     }
10410   /* Look for the closing `)'.  */
10411   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10412     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10413                                            /*consume_paren=*/true);
10414   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10415
10416   /* Create the ASM_EXPR.  */
10417   if (at_function_scope_p ())
10418     {
10419       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10420                                   inputs, clobbers);
10421       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10422       if (!extended_p)
10423         ASM_INPUT_P (asm_stmt) = 1;
10424     }
10425   else
10426     assemble_asm (string);
10427
10428  finish:
10429   c_lex_string_translate = 1;
10430 }
10431
10432 /* Declarators [gram.dcl.decl] */
10433
10434 /* Parse an init-declarator.
10435
10436    init-declarator:
10437      declarator initializer [opt]
10438
10439    GNU Extension:
10440
10441    init-declarator:
10442      declarator asm-specification [opt] attributes [opt] initializer [opt]
10443
10444    function-definition:
10445      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10446        function-body
10447      decl-specifier-seq [opt] declarator function-try-block
10448
10449    GNU Extension:
10450
10451    function-definition:
10452      __extension__ function-definition
10453
10454    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10455    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10456    then this declarator appears in a class scope.  The new DECL created
10457    by this declarator is returned.
10458
10459    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10460    for a function-definition here as well.  If the declarator is a
10461    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10462    be TRUE upon return.  By that point, the function-definition will
10463    have been completely parsed.
10464
10465    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10466    is FALSE.  */
10467
10468 static tree
10469 cp_parser_init_declarator (cp_parser* parser,
10470                            cp_decl_specifier_seq *decl_specifiers,
10471                            bool function_definition_allowed_p,
10472                            bool member_p,
10473                            int declares_class_or_enum,
10474                            bool* function_definition_p)
10475 {
10476   cp_token *token;
10477   cp_declarator *declarator;
10478   tree prefix_attributes;
10479   tree attributes;
10480   tree asm_specification;
10481   tree initializer;
10482   tree decl = NULL_TREE;
10483   tree scope;
10484   bool is_initialized;
10485   bool is_parenthesized_init;
10486   bool is_non_constant_init;
10487   int ctor_dtor_or_conv_p;
10488   bool friend_p;
10489   bool pop_p = false;
10490
10491   /* Gather the attributes that were provided with the
10492      decl-specifiers.  */
10493   prefix_attributes = decl_specifiers->attributes;
10494
10495   /* Assume that this is not the declarator for a function
10496      definition.  */
10497   if (function_definition_p)
10498     *function_definition_p = false;
10499
10500   /* Defer access checks while parsing the declarator; we cannot know
10501      what names are accessible until we know what is being
10502      declared.  */
10503   resume_deferring_access_checks ();
10504
10505   /* Parse the declarator.  */
10506   declarator
10507     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10508                             &ctor_dtor_or_conv_p,
10509                             /*parenthesized_p=*/NULL);
10510   /* Gather up the deferred checks.  */
10511   stop_deferring_access_checks ();
10512
10513   /* If the DECLARATOR was erroneous, there's no need to go
10514      further.  */
10515   if (declarator == cp_error_declarator)
10516     return error_mark_node;
10517
10518   cp_parser_check_for_definition_in_return_type (declarator,
10519                                                  declares_class_or_enum);
10520
10521   /* Figure out what scope the entity declared by the DECLARATOR is
10522      located in.  `grokdeclarator' sometimes changes the scope, so
10523      we compute it now.  */
10524   scope = get_scope_of_declarator (declarator);
10525
10526   /* If we're allowing GNU extensions, look for an asm-specification
10527      and attributes.  */
10528   if (cp_parser_allow_gnu_extensions_p (parser))
10529     {
10530       /* Look for an asm-specification.  */
10531       asm_specification = cp_parser_asm_specification_opt (parser);
10532       /* And attributes.  */
10533       attributes = cp_parser_attributes_opt (parser);
10534     }
10535   else
10536     {
10537       asm_specification = NULL_TREE;
10538       attributes = NULL_TREE;
10539     }
10540
10541   /* Peek at the next token.  */
10542   token = cp_lexer_peek_token (parser->lexer);
10543   /* Check to see if the token indicates the start of a
10544      function-definition.  */
10545   if (cp_parser_token_starts_function_definition_p (token))
10546     {
10547       if (!function_definition_allowed_p)
10548         {
10549           /* If a function-definition should not appear here, issue an
10550              error message.  */
10551           cp_parser_error (parser,
10552                            "a function-definition is not allowed here");
10553           return error_mark_node;
10554         }
10555       else
10556         {
10557           /* Neither attributes nor an asm-specification are allowed
10558              on a function-definition.  */
10559           if (asm_specification)
10560             error ("an asm-specification is not allowed on a function-definition");
10561           if (attributes)
10562             error ("attributes are not allowed on a function-definition");
10563           /* This is a function-definition.  */
10564           *function_definition_p = true;
10565
10566           /* Parse the function definition.  */
10567           if (member_p)
10568             decl = cp_parser_save_member_function_body (parser,
10569                                                         decl_specifiers,
10570                                                         declarator,
10571                                                         prefix_attributes);
10572           else
10573             decl
10574               = (cp_parser_function_definition_from_specifiers_and_declarator
10575                  (parser, decl_specifiers, prefix_attributes, declarator));
10576
10577           return decl;
10578         }
10579     }
10580
10581   /* [dcl.dcl]
10582
10583      Only in function declarations for constructors, destructors, and
10584      type conversions can the decl-specifier-seq be omitted.
10585
10586      We explicitly postpone this check past the point where we handle
10587      function-definitions because we tolerate function-definitions
10588      that are missing their return types in some modes.  */
10589   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10590     {
10591       cp_parser_error (parser,
10592                        "expected constructor, destructor, or type conversion");
10593       return error_mark_node;
10594     }
10595
10596   /* An `=' or an `(' indicates an initializer.  */
10597   is_initialized = (token->type == CPP_EQ
10598                      || token->type == CPP_OPEN_PAREN);
10599   /* If the init-declarator isn't initialized and isn't followed by a
10600      `,' or `;', it's not a valid init-declarator.  */
10601   if (!is_initialized
10602       && token->type != CPP_COMMA
10603       && token->type != CPP_SEMICOLON)
10604     {
10605       cp_parser_error (parser, "expected init-declarator");
10606       return error_mark_node;
10607     }
10608
10609   /* Because start_decl has side-effects, we should only call it if we
10610      know we're going ahead.  By this point, we know that we cannot
10611      possibly be looking at any other construct.  */
10612   cp_parser_commit_to_tentative_parse (parser);
10613
10614   /* If the decl specifiers were bad, issue an error now that we're
10615      sure this was intended to be a declarator.  Then continue
10616      declaring the variable(s), as int, to try to cut down on further
10617      errors.  */
10618   if (decl_specifiers->any_specifiers_p
10619       && decl_specifiers->type == error_mark_node)
10620     {
10621       cp_parser_error (parser, "invalid type in declaration");
10622       decl_specifiers->type = integer_type_node;
10623     }
10624
10625   /* Check to see whether or not this declaration is a friend.  */
10626   friend_p = cp_parser_friend_p (decl_specifiers);
10627
10628   /* Check that the number of template-parameter-lists is OK.  */
10629   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10630     return error_mark_node;
10631
10632   /* Enter the newly declared entry in the symbol table.  If we're
10633      processing a declaration in a class-specifier, we wait until
10634      after processing the initializer.  */
10635   if (!member_p)
10636     {
10637       if (parser->in_unbraced_linkage_specification_p)
10638         {
10639           decl_specifiers->storage_class = sc_extern;
10640           have_extern_spec = false;
10641         }
10642       decl = start_decl (declarator, decl_specifiers,
10643                          is_initialized, attributes, prefix_attributes,
10644                          &pop_p);
10645     }
10646   else if (scope)
10647     /* Enter the SCOPE.  That way unqualified names appearing in the
10648        initializer will be looked up in SCOPE.  */
10649     pop_p = push_scope (scope);
10650
10651   /* Perform deferred access control checks, now that we know in which
10652      SCOPE the declared entity resides.  */
10653   if (!member_p && decl)
10654     {
10655       tree saved_current_function_decl = NULL_TREE;
10656
10657       /* If the entity being declared is a function, pretend that we
10658          are in its scope.  If it is a `friend', it may have access to
10659          things that would not otherwise be accessible.  */
10660       if (TREE_CODE (decl) == FUNCTION_DECL)
10661         {
10662           saved_current_function_decl = current_function_decl;
10663           current_function_decl = decl;
10664         }
10665
10666       /* Perform the access control checks for the declarator and the
10667          the decl-specifiers.  */
10668       perform_deferred_access_checks ();
10669
10670       /* Restore the saved value.  */
10671       if (TREE_CODE (decl) == FUNCTION_DECL)
10672         current_function_decl = saved_current_function_decl;
10673     }
10674
10675   /* Parse the initializer.  */
10676   if (is_initialized)
10677     initializer = cp_parser_initializer (parser,
10678                                          &is_parenthesized_init,
10679                                          &is_non_constant_init);
10680   else
10681     {
10682       initializer = NULL_TREE;
10683       is_parenthesized_init = false;
10684       is_non_constant_init = true;
10685     }
10686
10687   /* The old parser allows attributes to appear after a parenthesized
10688      initializer.  Mark Mitchell proposed removing this functionality
10689      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10690      attributes -- but ignores them.  */
10691   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10692     if (cp_parser_attributes_opt (parser))
10693       warning ("attributes after parenthesized initializer ignored");
10694
10695   /* For an in-class declaration, use `grokfield' to create the
10696      declaration.  */
10697   if (member_p)
10698     {
10699       if (pop_p)
10700         pop_scope (scope);
10701       decl = grokfield (declarator, decl_specifiers,
10702                         initializer, /*asmspec=*/NULL_TREE,
10703                         /*attributes=*/NULL_TREE);
10704       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10705         cp_parser_save_default_args (parser, decl);
10706     }
10707
10708   /* Finish processing the declaration.  But, skip friend
10709      declarations.  */
10710   if (!friend_p && decl && decl != error_mark_node)
10711     {
10712       cp_finish_decl (decl,
10713                       initializer,
10714                       asm_specification,
10715                       /* If the initializer is in parentheses, then this is
10716                          a direct-initialization, which means that an
10717                          `explicit' constructor is OK.  Otherwise, an
10718                          `explicit' constructor cannot be used.  */
10719                       ((is_parenthesized_init || !is_initialized)
10720                      ? 0 : LOOKUP_ONLYCONVERTING));
10721       if (pop_p)
10722         pop_scope (DECL_CONTEXT (decl));
10723     }
10724
10725   /* Remember whether or not variables were initialized by
10726      constant-expressions.  */
10727   if (decl && TREE_CODE (decl) == VAR_DECL
10728       && is_initialized && !is_non_constant_init)
10729     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10730
10731   return decl;
10732 }
10733
10734 /* Parse a declarator.
10735
10736    declarator:
10737      direct-declarator
10738      ptr-operator declarator
10739
10740    abstract-declarator:
10741      ptr-operator abstract-declarator [opt]
10742      direct-abstract-declarator
10743
10744    GNU Extensions:
10745
10746    declarator:
10747      attributes [opt] direct-declarator
10748      attributes [opt] ptr-operator declarator
10749
10750    abstract-declarator:
10751      attributes [opt] ptr-operator abstract-declarator [opt]
10752      attributes [opt] direct-abstract-declarator
10753
10754    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10755    detect constructor, destructor or conversion operators. It is set
10756    to -1 if the declarator is a name, and +1 if it is a
10757    function. Otherwise it is set to zero. Usually you just want to
10758    test for >0, but internally the negative value is used.
10759
10760    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10761    a decl-specifier-seq unless it declares a constructor, destructor,
10762    or conversion.  It might seem that we could check this condition in
10763    semantic analysis, rather than parsing, but that makes it difficult
10764    to handle something like `f()'.  We want to notice that there are
10765    no decl-specifiers, and therefore realize that this is an
10766    expression, not a declaration.)
10767
10768    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10769    the declarator is a direct-declarator of the form "(...)".  */
10770
10771 static cp_declarator *
10772 cp_parser_declarator (cp_parser* parser,
10773                       cp_parser_declarator_kind dcl_kind,
10774                       int* ctor_dtor_or_conv_p,
10775                       bool* parenthesized_p)
10776 {
10777   cp_token *token;
10778   cp_declarator *declarator;
10779   enum tree_code code;
10780   cp_cv_quals cv_quals;
10781   tree class_type;
10782   tree attributes = NULL_TREE;
10783
10784   /* Assume this is not a constructor, destructor, or type-conversion
10785      operator.  */
10786   if (ctor_dtor_or_conv_p)
10787     *ctor_dtor_or_conv_p = 0;
10788
10789   if (cp_parser_allow_gnu_extensions_p (parser))
10790     attributes = cp_parser_attributes_opt (parser);
10791
10792   /* Peek at the next token.  */
10793   token = cp_lexer_peek_token (parser->lexer);
10794
10795   /* Check for the ptr-operator production.  */
10796   cp_parser_parse_tentatively (parser);
10797   /* Parse the ptr-operator.  */
10798   code = cp_parser_ptr_operator (parser,
10799                                  &class_type,
10800                                  &cv_quals);
10801   /* If that worked, then we have a ptr-operator.  */
10802   if (cp_parser_parse_definitely (parser))
10803     {
10804       /* If a ptr-operator was found, then this declarator was not
10805          parenthesized.  */
10806       if (parenthesized_p)
10807         *parenthesized_p = true;
10808       /* The dependent declarator is optional if we are parsing an
10809          abstract-declarator.  */
10810       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10811         cp_parser_parse_tentatively (parser);
10812
10813       /* Parse the dependent declarator.  */
10814       declarator = cp_parser_declarator (parser, dcl_kind,
10815                                          /*ctor_dtor_or_conv_p=*/NULL,
10816                                          /*parenthesized_p=*/NULL);
10817
10818       /* If we are parsing an abstract-declarator, we must handle the
10819          case where the dependent declarator is absent.  */
10820       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10821           && !cp_parser_parse_definitely (parser))
10822         declarator = NULL;
10823
10824       /* Build the representation of the ptr-operator.  */
10825       if (class_type)
10826         declarator = make_ptrmem_declarator (cv_quals,
10827                                              class_type,
10828                                              declarator);
10829       else if (code == INDIRECT_REF)
10830         declarator = make_pointer_declarator (cv_quals, declarator);
10831       else
10832         declarator = make_reference_declarator (cv_quals, declarator);
10833     }
10834   /* Everything else is a direct-declarator.  */
10835   else
10836     {
10837       if (parenthesized_p)
10838         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10839                                                    CPP_OPEN_PAREN);
10840       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10841                                                 ctor_dtor_or_conv_p);
10842     }
10843
10844   if (attributes && declarator != cp_error_declarator)
10845     declarator->attributes = attributes;
10846
10847   return declarator;
10848 }
10849
10850 /* Parse a direct-declarator or direct-abstract-declarator.
10851
10852    direct-declarator:
10853      declarator-id
10854      direct-declarator ( parameter-declaration-clause )
10855        cv-qualifier-seq [opt]
10856        exception-specification [opt]
10857      direct-declarator [ constant-expression [opt] ]
10858      ( declarator )
10859
10860    direct-abstract-declarator:
10861      direct-abstract-declarator [opt]
10862        ( parameter-declaration-clause )
10863        cv-qualifier-seq [opt]
10864        exception-specification [opt]
10865      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10866      ( abstract-declarator )
10867
10868    Returns a representation of the declarator.  DCL_KIND is
10869    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10870    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10871    we are parsing a direct-declarator.  It is
10872    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10873    of ambiguity we prefer an abstract declarator, as per
10874    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10875    cp_parser_declarator.  */
10876
10877 static cp_declarator *
10878 cp_parser_direct_declarator (cp_parser* parser,
10879                              cp_parser_declarator_kind dcl_kind,
10880                              int* ctor_dtor_or_conv_p)
10881 {
10882   cp_token *token;
10883   cp_declarator *declarator = NULL;
10884   tree scope = NULL_TREE;
10885   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10886   bool saved_in_declarator_p = parser->in_declarator_p;
10887   bool first = true;
10888   bool pop_p = false;
10889
10890   while (true)
10891     {
10892       /* Peek at the next token.  */
10893       token = cp_lexer_peek_token (parser->lexer);
10894       if (token->type == CPP_OPEN_PAREN)
10895         {
10896           /* This is either a parameter-declaration-clause, or a
10897              parenthesized declarator. When we know we are parsing a
10898              named declarator, it must be a parenthesized declarator
10899              if FIRST is true. For instance, `(int)' is a
10900              parameter-declaration-clause, with an omitted
10901              direct-abstract-declarator. But `((*))', is a
10902              parenthesized abstract declarator. Finally, when T is a
10903              template parameter `(T)' is a
10904              parameter-declaration-clause, and not a parenthesized
10905              named declarator.
10906
10907              We first try and parse a parameter-declaration-clause,
10908              and then try a nested declarator (if FIRST is true).
10909
10910              It is not an error for it not to be a
10911              parameter-declaration-clause, even when FIRST is
10912              false. Consider,
10913
10914                int i (int);
10915                int i (3);
10916
10917              The first is the declaration of a function while the
10918              second is a the definition of a variable, including its
10919              initializer.
10920
10921              Having seen only the parenthesis, we cannot know which of
10922              these two alternatives should be selected.  Even more
10923              complex are examples like:
10924
10925                int i (int (a));
10926                int i (int (3));
10927
10928              The former is a function-declaration; the latter is a
10929              variable initialization.
10930
10931              Thus again, we try a parameter-declaration-clause, and if
10932              that fails, we back out and return.  */
10933
10934           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10935             {
10936               cp_parameter_declarator *params;
10937               unsigned saved_num_template_parameter_lists;
10938
10939               cp_parser_parse_tentatively (parser);
10940
10941               /* Consume the `('.  */
10942               cp_lexer_consume_token (parser->lexer);
10943               if (first)
10944                 {
10945                   /* If this is going to be an abstract declarator, we're
10946                      in a declarator and we can't have default args.  */
10947                   parser->default_arg_ok_p = false;
10948                   parser->in_declarator_p = true;
10949                 }
10950
10951               /* Inside the function parameter list, surrounding
10952                  template-parameter-lists do not apply.  */
10953               saved_num_template_parameter_lists
10954                 = parser->num_template_parameter_lists;
10955               parser->num_template_parameter_lists = 0;
10956
10957               /* Parse the parameter-declaration-clause.  */
10958               params = cp_parser_parameter_declaration_clause (parser);
10959
10960               parser->num_template_parameter_lists
10961                 = saved_num_template_parameter_lists;
10962
10963               /* If all went well, parse the cv-qualifier-seq and the
10964                  exception-specification.  */
10965               if (cp_parser_parse_definitely (parser))
10966                 {
10967                   cp_cv_quals cv_quals;
10968                   tree exception_specification;
10969
10970                   if (ctor_dtor_or_conv_p)
10971                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10972                   first = false;
10973                   /* Consume the `)'.  */
10974                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10975
10976                   /* Parse the cv-qualifier-seq.  */
10977                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10978                   /* And the exception-specification.  */
10979                   exception_specification
10980                     = cp_parser_exception_specification_opt (parser);
10981
10982                   /* Create the function-declarator.  */
10983                   declarator = make_call_declarator (declarator,
10984                                                      params,
10985                                                      cv_quals,
10986                                                      exception_specification);
10987                   /* Any subsequent parameter lists are to do with
10988                      return type, so are not those of the declared
10989                      function.  */
10990                   parser->default_arg_ok_p = false;
10991
10992                   /* Repeat the main loop.  */
10993                   continue;
10994                 }
10995             }
10996
10997           /* If this is the first, we can try a parenthesized
10998              declarator.  */
10999           if (first)
11000             {
11001               bool saved_in_type_id_in_expr_p;
11002
11003               parser->default_arg_ok_p = saved_default_arg_ok_p;
11004               parser->in_declarator_p = saved_in_declarator_p;
11005
11006               /* Consume the `('.  */
11007               cp_lexer_consume_token (parser->lexer);
11008               /* Parse the nested declarator.  */
11009               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11010               parser->in_type_id_in_expr_p = true;
11011               declarator
11012                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11013                                         /*parenthesized_p=*/NULL);
11014               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11015               first = false;
11016               /* Expect a `)'.  */
11017               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11018                 declarator = cp_error_declarator;
11019               if (declarator == cp_error_declarator)
11020                 break;
11021
11022               goto handle_declarator;
11023             }
11024           /* Otherwise, we must be done.  */
11025           else
11026             break;
11027         }
11028       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11029                && token->type == CPP_OPEN_SQUARE)
11030         {
11031           /* Parse an array-declarator.  */
11032           tree bounds;
11033
11034           if (ctor_dtor_or_conv_p)
11035             *ctor_dtor_or_conv_p = 0;
11036
11037           first = false;
11038           parser->default_arg_ok_p = false;
11039           parser->in_declarator_p = true;
11040           /* Consume the `['.  */
11041           cp_lexer_consume_token (parser->lexer);
11042           /* Peek at the next token.  */
11043           token = cp_lexer_peek_token (parser->lexer);
11044           /* If the next token is `]', then there is no
11045              constant-expression.  */
11046           if (token->type != CPP_CLOSE_SQUARE)
11047             {
11048               bool non_constant_p;
11049
11050               bounds
11051                 = cp_parser_constant_expression (parser,
11052                                                  /*allow_non_constant=*/true,
11053                                                  &non_constant_p);
11054               if (!non_constant_p)
11055                 bounds = fold_non_dependent_expr (bounds);
11056             }
11057           else
11058             bounds = NULL_TREE;
11059           /* Look for the closing `]'.  */
11060           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11061             {
11062               declarator = cp_error_declarator;
11063               break;
11064             }
11065
11066           declarator = make_array_declarator (declarator, bounds);
11067         }
11068       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11069         {
11070           tree id;
11071
11072           /* Parse a declarator-id */
11073           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11074             cp_parser_parse_tentatively (parser);
11075           id = cp_parser_declarator_id (parser);
11076           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11077             {
11078               if (!cp_parser_parse_definitely (parser))
11079                 id = error_mark_node;
11080               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11081                 {
11082                   cp_parser_error (parser, "expected unqualified-id");
11083                   id = error_mark_node;
11084                 }
11085             }
11086
11087           if (id == error_mark_node)
11088             {
11089               declarator = cp_error_declarator;
11090               break;
11091             }
11092
11093           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11094             {
11095               tree scope = TREE_OPERAND (id, 0);
11096
11097               /* In the declaration of a member of a template class
11098                  outside of the class itself, the SCOPE will sometimes
11099                  be a TYPENAME_TYPE.  For example, given:
11100
11101                  template <typename T>
11102                  int S<T>::R::i = 3;
11103
11104                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11105                  this context, we must resolve S<T>::R to an ordinary
11106                  type, rather than a typename type.
11107
11108                  The reason we normally avoid resolving TYPENAME_TYPEs
11109                  is that a specialization of `S' might render
11110                  `S<T>::R' not a type.  However, if `S' is
11111                  specialized, then this `i' will not be used, so there
11112                  is no harm in resolving the types here.  */
11113               if (TREE_CODE (scope) == TYPENAME_TYPE)
11114                 {
11115                   tree type;
11116
11117                   /* Resolve the TYPENAME_TYPE.  */
11118                   type = resolve_typename_type (scope,
11119                                                  /*only_current_p=*/false);
11120                   /* If that failed, the declarator is invalid.  */
11121                   if (type == error_mark_node)
11122                     error ("`%T::%D' is not a type",
11123                            TYPE_CONTEXT (scope),
11124                            TYPE_IDENTIFIER (scope));
11125                   /* Build a new DECLARATOR.  */
11126                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11127                 }
11128             }
11129
11130           declarator = make_id_declarator (id);
11131           if (id)
11132             {
11133               tree class_type;
11134               tree unqualified_name;
11135
11136               if (TREE_CODE (id) == SCOPE_REF
11137                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11138                 {
11139                   class_type = TREE_OPERAND (id, 0);
11140                   unqualified_name = TREE_OPERAND (id, 1);
11141                 }
11142               else
11143                 {
11144                   class_type = current_class_type;
11145                   unqualified_name = id;
11146                 }
11147
11148               if (class_type)
11149                 {
11150                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11151                     declarator->u.id.sfk = sfk_destructor;
11152                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11153                     declarator->u.id.sfk = sfk_conversion;
11154                   else if (constructor_name_p (unqualified_name,
11155                                                class_type)
11156                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11157                                && same_type_p (TREE_TYPE (unqualified_name),
11158                                                class_type)))
11159                     declarator->u.id.sfk = sfk_constructor;
11160
11161                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11162                     *ctor_dtor_or_conv_p = -1;
11163                   if (TREE_CODE (id) == SCOPE_REF
11164                       && TREE_CODE (unqualified_name) == TYPE_DECL
11165                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11166                     {
11167                       error ("invalid use of constructor as a template");
11168                       inform ("use `%T::%D' instead of `%T::%T' to name the "
11169                               "constructor in a qualified name", class_type,
11170                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11171                               class_type, class_type);
11172                     }
11173                 }
11174             }
11175
11176         handle_declarator:;
11177           scope = get_scope_of_declarator (declarator);
11178           if (scope)
11179             /* Any names that appear after the declarator-id for a
11180                member are looked up in the containing scope.  */
11181             pop_p = push_scope (scope);
11182           parser->in_declarator_p = true;
11183           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11184               || (declarator && declarator->kind == cdk_id))
11185             /* Default args are only allowed on function
11186                declarations.  */
11187             parser->default_arg_ok_p = saved_default_arg_ok_p;
11188           else
11189             parser->default_arg_ok_p = false;
11190
11191           first = false;
11192         }
11193       /* We're done.  */
11194       else
11195         break;
11196     }
11197
11198   /* For an abstract declarator, we might wind up with nothing at this
11199      point.  That's an error; the declarator is not optional.  */
11200   if (!declarator)
11201     cp_parser_error (parser, "expected declarator");
11202
11203   /* If we entered a scope, we must exit it now.  */
11204   if (pop_p)
11205     pop_scope (scope);
11206
11207   parser->default_arg_ok_p = saved_default_arg_ok_p;
11208   parser->in_declarator_p = saved_in_declarator_p;
11209
11210   return declarator;
11211 }
11212
11213 /* Parse a ptr-operator.
11214
11215    ptr-operator:
11216      * cv-qualifier-seq [opt]
11217      &
11218      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11219
11220    GNU Extension:
11221
11222    ptr-operator:
11223      & cv-qualifier-seq [opt]
11224
11225    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11226    Returns ADDR_EXPR if a reference was used.  In the case of a
11227    pointer-to-member, *TYPE is filled in with the TYPE containing the
11228    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11229    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11230    ERROR_MARK if an error occurred.  */
11231
11232 static enum tree_code
11233 cp_parser_ptr_operator (cp_parser* parser,
11234                         tree* type,
11235                         cp_cv_quals *cv_quals)
11236 {
11237   enum tree_code code = ERROR_MARK;
11238   cp_token *token;
11239
11240   /* Assume that it's not a pointer-to-member.  */
11241   *type = NULL_TREE;
11242   /* And that there are no cv-qualifiers.  */
11243   *cv_quals = TYPE_UNQUALIFIED;
11244
11245   /* Peek at the next token.  */
11246   token = cp_lexer_peek_token (parser->lexer);
11247   /* If it's a `*' or `&' we have a pointer or reference.  */
11248   if (token->type == CPP_MULT || token->type == CPP_AND)
11249     {
11250       /* Remember which ptr-operator we were processing.  */
11251       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11252
11253       /* Consume the `*' or `&'.  */
11254       cp_lexer_consume_token (parser->lexer);
11255
11256       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11257          `&', if we are allowing GNU extensions.  (The only qualifier
11258          that can legally appear after `&' is `restrict', but that is
11259          enforced during semantic analysis.  */
11260       if (code == INDIRECT_REF
11261           || cp_parser_allow_gnu_extensions_p (parser))
11262         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11263     }
11264   else
11265     {
11266       /* Try the pointer-to-member case.  */
11267       cp_parser_parse_tentatively (parser);
11268       /* Look for the optional `::' operator.  */
11269       cp_parser_global_scope_opt (parser,
11270                                   /*current_scope_valid_p=*/false);
11271       /* Look for the nested-name specifier.  */
11272       cp_parser_nested_name_specifier (parser,
11273                                        /*typename_keyword_p=*/false,
11274                                        /*check_dependency_p=*/true,
11275                                        /*type_p=*/false,
11276                                        /*is_declaration=*/false);
11277       /* If we found it, and the next token is a `*', then we are
11278          indeed looking at a pointer-to-member operator.  */
11279       if (!cp_parser_error_occurred (parser)
11280           && cp_parser_require (parser, CPP_MULT, "`*'"))
11281         {
11282           /* The type of which the member is a member is given by the
11283              current SCOPE.  */
11284           *type = parser->scope;
11285           /* The next name will not be qualified.  */
11286           parser->scope = NULL_TREE;
11287           parser->qualifying_scope = NULL_TREE;
11288           parser->object_scope = NULL_TREE;
11289           /* Indicate that the `*' operator was used.  */
11290           code = INDIRECT_REF;
11291           /* Look for the optional cv-qualifier-seq.  */
11292           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11293         }
11294       /* If that didn't work we don't have a ptr-operator.  */
11295       if (!cp_parser_parse_definitely (parser))
11296         cp_parser_error (parser, "expected ptr-operator");
11297     }
11298
11299   return code;
11300 }
11301
11302 /* Parse an (optional) cv-qualifier-seq.
11303
11304    cv-qualifier-seq:
11305      cv-qualifier cv-qualifier-seq [opt]
11306
11307    cv-qualifier:
11308      const
11309      volatile
11310
11311    GNU Extension:
11312
11313    cv-qualifier:
11314      __restrict__
11315
11316    Returns a bitmask representing the cv-qualifiers.  */
11317
11318 static cp_cv_quals
11319 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11320 {
11321   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11322
11323   while (true)
11324     {
11325       cp_token *token;
11326       cp_cv_quals cv_qualifier;
11327
11328       /* Peek at the next token.  */
11329       token = cp_lexer_peek_token (parser->lexer);
11330       /* See if it's a cv-qualifier.  */
11331       switch (token->keyword)
11332         {
11333         case RID_CONST:
11334           cv_qualifier = TYPE_QUAL_CONST;
11335           break;
11336
11337         case RID_VOLATILE:
11338           cv_qualifier = TYPE_QUAL_VOLATILE;
11339           break;
11340
11341         case RID_RESTRICT:
11342           cv_qualifier = TYPE_QUAL_RESTRICT;
11343           break;
11344
11345         default:
11346           cv_qualifier = TYPE_UNQUALIFIED;
11347           break;
11348         }
11349
11350       if (!cv_qualifier)
11351         break;
11352
11353       if (cv_quals & cv_qualifier)
11354         {
11355           error ("duplicate cv-qualifier");
11356           cp_lexer_purge_token (parser->lexer);
11357         }
11358       else
11359         {
11360           cp_lexer_consume_token (parser->lexer);
11361           cv_quals |= cv_qualifier;
11362         }
11363     }
11364
11365   return cv_quals;
11366 }
11367
11368 /* Parse a declarator-id.
11369
11370    declarator-id:
11371      id-expression
11372      :: [opt] nested-name-specifier [opt] type-name
11373
11374    In the `id-expression' case, the value returned is as for
11375    cp_parser_id_expression if the id-expression was an unqualified-id.
11376    If the id-expression was a qualified-id, then a SCOPE_REF is
11377    returned.  The first operand is the scope (either a NAMESPACE_DECL
11378    or TREE_TYPE), but the second is still just a representation of an
11379    unqualified-id.  */
11380
11381 static tree
11382 cp_parser_declarator_id (cp_parser* parser)
11383 {
11384   tree id_expression;
11385
11386   /* The expression must be an id-expression.  Assume that qualified
11387      names are the names of types so that:
11388
11389        template <class T>
11390        int S<T>::R::i = 3;
11391
11392      will work; we must treat `S<T>::R' as the name of a type.
11393      Similarly, assume that qualified names are templates, where
11394      required, so that:
11395
11396        template <class T>
11397        int S<T>::R<T>::i = 3;
11398
11399      will work, too.  */
11400   id_expression = cp_parser_id_expression (parser,
11401                                            /*template_keyword_p=*/false,
11402                                            /*check_dependency_p=*/false,
11403                                            /*template_p=*/NULL,
11404                                            /*declarator_p=*/true);
11405   /* If the name was qualified, create a SCOPE_REF to represent
11406      that.  */
11407   if (parser->scope)
11408     {
11409       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11410       parser->scope = NULL_TREE;
11411     }
11412
11413   return id_expression;
11414 }
11415
11416 /* Parse a type-id.
11417
11418    type-id:
11419      type-specifier-seq abstract-declarator [opt]
11420
11421    Returns the TYPE specified.  */
11422
11423 static tree
11424 cp_parser_type_id (cp_parser* parser)
11425 {
11426   cp_decl_specifier_seq type_specifier_seq;
11427   cp_declarator *abstract_declarator;
11428
11429   /* Parse the type-specifier-seq.  */
11430   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11431   if (type_specifier_seq.type == error_mark_node)
11432     return error_mark_node;
11433
11434   /* There might or might not be an abstract declarator.  */
11435   cp_parser_parse_tentatively (parser);
11436   /* Look for the declarator.  */
11437   abstract_declarator
11438     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11439                             /*parenthesized_p=*/NULL);
11440   /* Check to see if there really was a declarator.  */
11441   if (!cp_parser_parse_definitely (parser))
11442     abstract_declarator = NULL;
11443
11444   return groktypename (&type_specifier_seq, abstract_declarator);
11445 }
11446
11447 /* Parse a type-specifier-seq.
11448
11449    type-specifier-seq:
11450      type-specifier type-specifier-seq [opt]
11451
11452    GNU extension:
11453
11454    type-specifier-seq:
11455      attributes type-specifier-seq [opt]
11456
11457    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11458
11459 static void
11460 cp_parser_type_specifier_seq (cp_parser* parser,
11461                               cp_decl_specifier_seq *type_specifier_seq)
11462 {
11463   bool seen_type_specifier = false;
11464
11465   /* Clear the TYPE_SPECIFIER_SEQ.  */
11466   clear_decl_specs (type_specifier_seq);
11467
11468   /* Parse the type-specifiers and attributes.  */
11469   while (true)
11470     {
11471       tree type_specifier;
11472
11473       /* Check for attributes first.  */
11474       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11475         {
11476           type_specifier_seq->attributes =
11477             chainon (type_specifier_seq->attributes,
11478                      cp_parser_attributes_opt (parser));
11479           continue;
11480         }
11481
11482       /* Look for the type-specifier.  */
11483       type_specifier = cp_parser_type_specifier (parser,
11484                                                  CP_PARSER_FLAGS_OPTIONAL,
11485                                                  type_specifier_seq,
11486                                                  /*is_declaration=*/false,
11487                                                  NULL,
11488                                                  NULL);
11489       /* If the first type-specifier could not be found, this is not a
11490          type-specifier-seq at all.  */
11491       if (!seen_type_specifier && !type_specifier)
11492         {
11493           cp_parser_error (parser, "expected type-specifier");
11494           type_specifier_seq->type = error_mark_node;
11495           return;
11496         }
11497       /* If subsequent type-specifiers could not be found, the
11498          type-specifier-seq is complete.  */
11499       else if (seen_type_specifier && !type_specifier)
11500         break;
11501
11502       seen_type_specifier = true;
11503     }
11504
11505   return;
11506 }
11507
11508 /* Parse a parameter-declaration-clause.
11509
11510    parameter-declaration-clause:
11511      parameter-declaration-list [opt] ... [opt]
11512      parameter-declaration-list , ...
11513
11514    Returns a representation for the parameter declarations.  A return
11515    value of NULL indicates a parameter-declaration-clause consisting
11516    only of an ellipsis.  */
11517
11518 static cp_parameter_declarator *
11519 cp_parser_parameter_declaration_clause (cp_parser* parser)
11520 {
11521   cp_parameter_declarator *parameters;
11522   cp_token *token;
11523   bool ellipsis_p;
11524   bool is_error;
11525
11526   /* Peek at the next token.  */
11527   token = cp_lexer_peek_token (parser->lexer);
11528   /* Check for trivial parameter-declaration-clauses.  */
11529   if (token->type == CPP_ELLIPSIS)
11530     {
11531       /* Consume the `...' token.  */
11532       cp_lexer_consume_token (parser->lexer);
11533       return NULL;
11534     }
11535   else if (token->type == CPP_CLOSE_PAREN)
11536     /* There are no parameters.  */
11537     {
11538 #ifndef NO_IMPLICIT_EXTERN_C
11539       if (in_system_header && current_class_type == NULL
11540           && current_lang_name == lang_name_c)
11541         return NULL;
11542       else
11543 #endif
11544         return no_parameters;
11545     }
11546   /* Check for `(void)', too, which is a special case.  */
11547   else if (token->keyword == RID_VOID
11548            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11549                == CPP_CLOSE_PAREN))
11550     {
11551       /* Consume the `void' token.  */
11552       cp_lexer_consume_token (parser->lexer);
11553       /* There are no parameters.  */
11554       return no_parameters;
11555     }
11556
11557   /* Parse the parameter-declaration-list.  */
11558   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11559   /* If a parse error occurred while parsing the
11560      parameter-declaration-list, then the entire
11561      parameter-declaration-clause is erroneous.  */
11562   if (is_error)
11563     return NULL;
11564
11565   /* Peek at the next token.  */
11566   token = cp_lexer_peek_token (parser->lexer);
11567   /* If it's a `,', the clause should terminate with an ellipsis.  */
11568   if (token->type == CPP_COMMA)
11569     {
11570       /* Consume the `,'.  */
11571       cp_lexer_consume_token (parser->lexer);
11572       /* Expect an ellipsis.  */
11573       ellipsis_p
11574         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11575     }
11576   /* It might also be `...' if the optional trailing `,' was
11577      omitted.  */
11578   else if (token->type == CPP_ELLIPSIS)
11579     {
11580       /* Consume the `...' token.  */
11581       cp_lexer_consume_token (parser->lexer);
11582       /* And remember that we saw it.  */
11583       ellipsis_p = true;
11584     }
11585   else
11586     ellipsis_p = false;
11587
11588   /* Finish the parameter list.  */
11589   if (parameters && ellipsis_p)
11590     parameters->ellipsis_p = true;
11591
11592   return parameters;
11593 }
11594
11595 /* Parse a parameter-declaration-list.
11596
11597    parameter-declaration-list:
11598      parameter-declaration
11599      parameter-declaration-list , parameter-declaration
11600
11601    Returns a representation of the parameter-declaration-list, as for
11602    cp_parser_parameter_declaration_clause.  However, the
11603    `void_list_node' is never appended to the list.  Upon return,
11604    *IS_ERROR will be true iff an error occurred.  */
11605
11606 static cp_parameter_declarator *
11607 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11608 {
11609   cp_parameter_declarator *parameters = NULL;
11610   cp_parameter_declarator **tail = &parameters;
11611
11612   /* Assume all will go well.  */
11613   *is_error = false;
11614
11615   /* Look for more parameters.  */
11616   while (true)
11617     {
11618       cp_parameter_declarator *parameter;
11619       bool parenthesized_p;
11620       /* Parse the parameter.  */
11621       parameter
11622         = cp_parser_parameter_declaration (parser,
11623                                            /*template_parm_p=*/false,
11624                                            &parenthesized_p);
11625
11626       /* If a parse error occurred parsing the parameter declaration,
11627          then the entire parameter-declaration-list is erroneous.  */
11628       if (!parameter)
11629         {
11630           *is_error = true;
11631           parameters = NULL;
11632           break;
11633         }
11634       /* Add the new parameter to the list.  */
11635       *tail = parameter;
11636       tail = &parameter->next;
11637
11638       /* Peek at the next token.  */
11639       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11640           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11641         /* The parameter-declaration-list is complete.  */
11642         break;
11643       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11644         {
11645           cp_token *token;
11646
11647           /* Peek at the next token.  */
11648           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11649           /* If it's an ellipsis, then the list is complete.  */
11650           if (token->type == CPP_ELLIPSIS)
11651             break;
11652           /* Otherwise, there must be more parameters.  Consume the
11653              `,'.  */
11654           cp_lexer_consume_token (parser->lexer);
11655           /* When parsing something like:
11656
11657                 int i(float f, double d)
11658
11659              we can tell after seeing the declaration for "f" that we
11660              are not looking at an initialization of a variable "i",
11661              but rather at the declaration of a function "i".
11662
11663              Due to the fact that the parsing of template arguments
11664              (as specified to a template-id) requires backtracking we
11665              cannot use this technique when inside a template argument
11666              list.  */
11667           if (!parser->in_template_argument_list_p
11668               && !parser->in_type_id_in_expr_p
11669               && cp_parser_parsing_tentatively (parser)
11670               && !cp_parser_committed_to_tentative_parse (parser)
11671               /* However, a parameter-declaration of the form
11672                  "foat(f)" (which is a valid declaration of a
11673                  parameter "f") can also be interpreted as an
11674                  expression (the conversion of "f" to "float").  */
11675               && !parenthesized_p)
11676             cp_parser_commit_to_tentative_parse (parser);
11677         }
11678       else
11679         {
11680           cp_parser_error (parser, "expected `,' or `...'");
11681           if (!cp_parser_parsing_tentatively (parser)
11682               || cp_parser_committed_to_tentative_parse (parser))
11683             cp_parser_skip_to_closing_parenthesis (parser,
11684                                                    /*recovering=*/true,
11685                                                    /*or_comma=*/false,
11686                                                    /*consume_paren=*/false);
11687           break;
11688         }
11689     }
11690
11691   return parameters;
11692 }
11693
11694 /* Parse a parameter declaration.
11695
11696    parameter-declaration:
11697      decl-specifier-seq declarator
11698      decl-specifier-seq declarator = assignment-expression
11699      decl-specifier-seq abstract-declarator [opt]
11700      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11701
11702    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11703    declares a template parameter.  (In that case, a non-nested `>'
11704    token encountered during the parsing of the assignment-expression
11705    is not interpreted as a greater-than operator.)
11706
11707    Returns a representation of the parameter, or NULL if an error
11708    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11709    true iff the declarator is of the form "(p)".  */
11710
11711 static cp_parameter_declarator *
11712 cp_parser_parameter_declaration (cp_parser *parser,
11713                                  bool template_parm_p,
11714                                  bool *parenthesized_p)
11715 {
11716   int declares_class_or_enum;
11717   bool greater_than_is_operator_p;
11718   cp_decl_specifier_seq decl_specifiers;
11719   cp_declarator *declarator;
11720   tree default_argument;
11721   cp_token *token;
11722   const char *saved_message;
11723
11724   /* In a template parameter, `>' is not an operator.
11725
11726      [temp.param]
11727
11728      When parsing a default template-argument for a non-type
11729      template-parameter, the first non-nested `>' is taken as the end
11730      of the template parameter-list rather than a greater-than
11731      operator.  */
11732   greater_than_is_operator_p = !template_parm_p;
11733
11734   /* Type definitions may not appear in parameter types.  */
11735   saved_message = parser->type_definition_forbidden_message;
11736   parser->type_definition_forbidden_message
11737     = "types may not be defined in parameter types";
11738
11739   /* Parse the declaration-specifiers.  */
11740   cp_parser_decl_specifier_seq (parser,
11741                                 CP_PARSER_FLAGS_NONE,
11742                                 &decl_specifiers,
11743                                 &declares_class_or_enum);
11744   /* If an error occurred, there's no reason to attempt to parse the
11745      rest of the declaration.  */
11746   if (cp_parser_error_occurred (parser))
11747     {
11748       parser->type_definition_forbidden_message = saved_message;
11749       return NULL;
11750     }
11751
11752   /* Peek at the next token.  */
11753   token = cp_lexer_peek_token (parser->lexer);
11754   /* If the next token is a `)', `,', `=', `>', or `...', then there
11755      is no declarator.  */
11756   if (token->type == CPP_CLOSE_PAREN
11757       || token->type == CPP_COMMA
11758       || token->type == CPP_EQ
11759       || token->type == CPP_ELLIPSIS
11760       || token->type == CPP_GREATER)
11761     {
11762       declarator = NULL;
11763       if (parenthesized_p)
11764         *parenthesized_p = false;
11765     }
11766   /* Otherwise, there should be a declarator.  */
11767   else
11768     {
11769       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11770       parser->default_arg_ok_p = false;
11771
11772       /* After seeing a decl-specifier-seq, if the next token is not a
11773          "(", there is no possibility that the code is a valid
11774          expression.  Therefore, if parsing tentatively, we commit at
11775          this point.  */
11776       if (!parser->in_template_argument_list_p
11777           /* In an expression context, having seen:
11778
11779                (int((char ...
11780
11781              we cannot be sure whether we are looking at a
11782              function-type (taking a "char" as a parameter) or a cast
11783              of some object of type "char" to "int".  */
11784           && !parser->in_type_id_in_expr_p
11785           && cp_parser_parsing_tentatively (parser)
11786           && !cp_parser_committed_to_tentative_parse (parser)
11787           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11788         cp_parser_commit_to_tentative_parse (parser);
11789       /* Parse the declarator.  */
11790       declarator = cp_parser_declarator (parser,
11791                                          CP_PARSER_DECLARATOR_EITHER,
11792                                          /*ctor_dtor_or_conv_p=*/NULL,
11793                                          parenthesized_p);
11794       parser->default_arg_ok_p = saved_default_arg_ok_p;
11795       /* After the declarator, allow more attributes.  */
11796       decl_specifiers.attributes
11797         = chainon (decl_specifiers.attributes,
11798                    cp_parser_attributes_opt (parser));
11799     }
11800
11801   /* The restriction on defining new types applies only to the type
11802      of the parameter, not to the default argument.  */
11803   parser->type_definition_forbidden_message = saved_message;
11804
11805   /* If the next token is `=', then process a default argument.  */
11806   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11807     {
11808       bool saved_greater_than_is_operator_p;
11809       /* Consume the `='.  */
11810       cp_lexer_consume_token (parser->lexer);
11811
11812       /* If we are defining a class, then the tokens that make up the
11813          default argument must be saved and processed later.  */
11814       if (!template_parm_p && at_class_scope_p ()
11815           && TYPE_BEING_DEFINED (current_class_type))
11816         {
11817           unsigned depth = 0;
11818
11819           /* Create a DEFAULT_ARG to represented the unparsed default
11820              argument.  */
11821           default_argument = make_node (DEFAULT_ARG);
11822           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11823
11824           /* Add tokens until we have processed the entire default
11825              argument.  */
11826           while (true)
11827             {
11828               bool done = false;
11829               cp_token *token;
11830
11831               /* Peek at the next token.  */
11832               token = cp_lexer_peek_token (parser->lexer);
11833               /* What we do depends on what token we have.  */
11834               switch (token->type)
11835                 {
11836                   /* In valid code, a default argument must be
11837                      immediately followed by a `,' `)', or `...'.  */
11838                 case CPP_COMMA:
11839                 case CPP_CLOSE_PAREN:
11840                 case CPP_ELLIPSIS:
11841                   /* If we run into a non-nested `;', `}', or `]',
11842                      then the code is invalid -- but the default
11843                      argument is certainly over.  */
11844                 case CPP_SEMICOLON:
11845                 case CPP_CLOSE_BRACE:
11846                 case CPP_CLOSE_SQUARE:
11847                   if (depth == 0)
11848                     done = true;
11849                   /* Update DEPTH, if necessary.  */
11850                   else if (token->type == CPP_CLOSE_PAREN
11851                            || token->type == CPP_CLOSE_BRACE
11852                            || token->type == CPP_CLOSE_SQUARE)
11853                     --depth;
11854                   break;
11855
11856                 case CPP_OPEN_PAREN:
11857                 case CPP_OPEN_SQUARE:
11858                 case CPP_OPEN_BRACE:
11859                   ++depth;
11860                   break;
11861
11862                 case CPP_GREATER:
11863                   /* If we see a non-nested `>', and `>' is not an
11864                      operator, then it marks the end of the default
11865                      argument.  */
11866                   if (!depth && !greater_than_is_operator_p)
11867                     done = true;
11868                   break;
11869
11870                   /* If we run out of tokens, issue an error message.  */
11871                 case CPP_EOF:
11872                   error ("file ends in default argument");
11873                   done = true;
11874                   break;
11875
11876                 case CPP_NAME:
11877                 case CPP_SCOPE:
11878                   /* In these cases, we should look for template-ids.
11879                      For example, if the default argument is
11880                      `X<int, double>()', we need to do name lookup to
11881                      figure out whether or not `X' is a template; if
11882                      so, the `,' does not end the default argument.
11883
11884                      That is not yet done.  */
11885                   break;
11886
11887                 default:
11888                   break;
11889                 }
11890
11891               /* If we've reached the end, stop.  */
11892               if (done)
11893                 break;
11894
11895               /* Add the token to the token block.  */
11896               token = cp_lexer_consume_token (parser->lexer);
11897               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11898                                          token);
11899             }
11900         }
11901       /* Outside of a class definition, we can just parse the
11902          assignment-expression.  */
11903       else
11904         {
11905           bool saved_local_variables_forbidden_p;
11906
11907           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11908              set correctly.  */
11909           saved_greater_than_is_operator_p
11910             = parser->greater_than_is_operator_p;
11911           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11912           /* Local variable names (and the `this' keyword) may not
11913              appear in a default argument.  */
11914           saved_local_variables_forbidden_p
11915             = parser->local_variables_forbidden_p;
11916           parser->local_variables_forbidden_p = true;
11917           /* Parse the assignment-expression.  */
11918           default_argument = cp_parser_assignment_expression (parser);
11919           /* Restore saved state.  */
11920           parser->greater_than_is_operator_p
11921             = saved_greater_than_is_operator_p;
11922           parser->local_variables_forbidden_p
11923             = saved_local_variables_forbidden_p;
11924         }
11925       if (!parser->default_arg_ok_p)
11926         {
11927           if (!flag_pedantic_errors)
11928             warning ("deprecated use of default argument for parameter of non-function");
11929           else
11930             {
11931               error ("default arguments are only permitted for function parameters");
11932               default_argument = NULL_TREE;
11933             }
11934         }
11935     }
11936   else
11937     default_argument = NULL_TREE;
11938
11939   return make_parameter_declarator (&decl_specifiers,
11940                                     declarator,
11941                                     default_argument);
11942 }
11943
11944 /* Parse a function-body.
11945
11946    function-body:
11947      compound_statement  */
11948
11949 static void
11950 cp_parser_function_body (cp_parser *parser)
11951 {
11952   cp_parser_compound_statement (parser, NULL, false);
11953 }
11954
11955 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11956    true if a ctor-initializer was present.  */
11957
11958 static bool
11959 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11960 {
11961   tree body;
11962   bool ctor_initializer_p;
11963
11964   /* Begin the function body.  */
11965   body = begin_function_body ();
11966   /* Parse the optional ctor-initializer.  */
11967   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11968   /* Parse the function-body.  */
11969   cp_parser_function_body (parser);
11970   /* Finish the function body.  */
11971   finish_function_body (body);
11972
11973   return ctor_initializer_p;
11974 }
11975
11976 /* Parse an initializer.
11977
11978    initializer:
11979      = initializer-clause
11980      ( expression-list )
11981
11982    Returns a expression representing the initializer.  If no
11983    initializer is present, NULL_TREE is returned.
11984
11985    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11986    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11987    set to FALSE if there is no initializer present.  If there is an
11988    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11989    is set to true; otherwise it is set to false.  */
11990
11991 static tree
11992 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11993                        bool* non_constant_p)
11994 {
11995   cp_token *token;
11996   tree init;
11997
11998   /* Peek at the next token.  */
11999   token = cp_lexer_peek_token (parser->lexer);
12000
12001   /* Let our caller know whether or not this initializer was
12002      parenthesized.  */
12003   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12004   /* Assume that the initializer is constant.  */
12005   *non_constant_p = false;
12006
12007   if (token->type == CPP_EQ)
12008     {
12009       /* Consume the `='.  */
12010       cp_lexer_consume_token (parser->lexer);
12011       /* Parse the initializer-clause.  */
12012       init = cp_parser_initializer_clause (parser, non_constant_p);
12013     }
12014   else if (token->type == CPP_OPEN_PAREN)
12015     init = cp_parser_parenthesized_expression_list (parser, false,
12016                                                     non_constant_p);
12017   else
12018     {
12019       /* Anything else is an error.  */
12020       cp_parser_error (parser, "expected initializer");
12021       init = error_mark_node;
12022     }
12023
12024   return init;
12025 }
12026
12027 /* Parse an initializer-clause.
12028
12029    initializer-clause:
12030      assignment-expression
12031      { initializer-list , [opt] }
12032      { }
12033
12034    Returns an expression representing the initializer.
12035
12036    If the `assignment-expression' production is used the value
12037    returned is simply a representation for the expression.
12038
12039    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12040    the elements of the initializer-list (or NULL_TREE, if the last
12041    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12042    NULL_TREE.  There is no way to detect whether or not the optional
12043    trailing `,' was provided.  NON_CONSTANT_P is as for
12044    cp_parser_initializer.  */
12045
12046 static tree
12047 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12048 {
12049   tree initializer;
12050
12051   /* If it is not a `{', then we are looking at an
12052      assignment-expression.  */
12053   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12054     {
12055       initializer
12056         = cp_parser_constant_expression (parser,
12057                                         /*allow_non_constant_p=*/true,
12058                                         non_constant_p);
12059       if (!*non_constant_p)
12060         initializer = fold_non_dependent_expr (initializer);
12061     }
12062   else
12063     {
12064       /* Consume the `{' token.  */
12065       cp_lexer_consume_token (parser->lexer);
12066       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12067       initializer = make_node (CONSTRUCTOR);
12068       /* If it's not a `}', then there is a non-trivial initializer.  */
12069       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12070         {
12071           /* Parse the initializer list.  */
12072           CONSTRUCTOR_ELTS (initializer)
12073             = cp_parser_initializer_list (parser, non_constant_p);
12074           /* A trailing `,' token is allowed.  */
12075           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12076             cp_lexer_consume_token (parser->lexer);
12077         }
12078       /* Now, there should be a trailing `}'.  */
12079       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12080     }
12081
12082   return initializer;
12083 }
12084
12085 /* Parse an initializer-list.
12086
12087    initializer-list:
12088      initializer-clause
12089      initializer-list , initializer-clause
12090
12091    GNU Extension:
12092
12093    initializer-list:
12094      identifier : initializer-clause
12095      initializer-list, identifier : initializer-clause
12096
12097    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12098    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12099    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12100    as for cp_parser_initializer.  */
12101
12102 static tree
12103 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12104 {
12105   tree initializers = NULL_TREE;
12106
12107   /* Assume all of the expressions are constant.  */
12108   *non_constant_p = false;
12109
12110   /* Parse the rest of the list.  */
12111   while (true)
12112     {
12113       cp_token *token;
12114       tree identifier;
12115       tree initializer;
12116       bool clause_non_constant_p;
12117
12118       /* If the next token is an identifier and the following one is a
12119          colon, we are looking at the GNU designated-initializer
12120          syntax.  */
12121       if (cp_parser_allow_gnu_extensions_p (parser)
12122           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12123           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12124         {
12125           /* Consume the identifier.  */
12126           identifier = cp_lexer_consume_token (parser->lexer)->value;
12127           /* Consume the `:'.  */
12128           cp_lexer_consume_token (parser->lexer);
12129         }
12130       else
12131         identifier = NULL_TREE;
12132
12133       /* Parse the initializer.  */
12134       initializer = cp_parser_initializer_clause (parser,
12135                                                   &clause_non_constant_p);
12136       /* If any clause is non-constant, so is the entire initializer.  */
12137       if (clause_non_constant_p)
12138         *non_constant_p = true;
12139       /* Add it to the list.  */
12140       initializers = tree_cons (identifier, initializer, initializers);
12141
12142       /* If the next token is not a comma, we have reached the end of
12143          the list.  */
12144       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12145         break;
12146
12147       /* Peek at the next token.  */
12148       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12149       /* If the next token is a `}', then we're still done.  An
12150          initializer-clause can have a trailing `,' after the
12151          initializer-list and before the closing `}'.  */
12152       if (token->type == CPP_CLOSE_BRACE)
12153         break;
12154
12155       /* Consume the `,' token.  */
12156       cp_lexer_consume_token (parser->lexer);
12157     }
12158
12159   /* The initializers were built up in reverse order, so we need to
12160      reverse them now.  */
12161   return nreverse (initializers);
12162 }
12163
12164 /* Classes [gram.class] */
12165
12166 /* Parse a class-name.
12167
12168    class-name:
12169      identifier
12170      template-id
12171
12172    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12173    to indicate that names looked up in dependent types should be
12174    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12175    keyword has been used to indicate that the name that appears next
12176    is a template.  TYPE_P is true iff the next name should be treated
12177    as class-name, even if it is declared to be some other kind of name
12178    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12179    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12180    being defined in a class-head.
12181
12182    Returns the TYPE_DECL representing the class.  */
12183
12184 static tree
12185 cp_parser_class_name (cp_parser *parser,
12186                       bool typename_keyword_p,
12187                       bool template_keyword_p,
12188                       bool type_p,
12189                       bool check_dependency_p,
12190                       bool class_head_p,
12191                       bool is_declaration)
12192 {
12193   tree decl;
12194   tree scope;
12195   bool typename_p;
12196   cp_token *token;
12197
12198   /* All class-names start with an identifier.  */
12199   token = cp_lexer_peek_token (parser->lexer);
12200   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12201     {
12202       cp_parser_error (parser, "expected class-name");
12203       return error_mark_node;
12204     }
12205
12206   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12207      to a template-id, so we save it here.  */
12208   scope = parser->scope;
12209   if (scope == error_mark_node)
12210     return error_mark_node;
12211
12212   /* Any name names a type if we're following the `typename' keyword
12213      in a qualified name where the enclosing scope is type-dependent.  */
12214   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12215                 && dependent_type_p (scope));
12216   /* Handle the common case (an identifier, but not a template-id)
12217      efficiently.  */
12218   if (token->type == CPP_NAME
12219       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12220     {
12221       tree identifier;
12222
12223       /* Look for the identifier.  */
12224       identifier = cp_parser_identifier (parser);
12225       /* If the next token isn't an identifier, we are certainly not
12226          looking at a class-name.  */
12227       if (identifier == error_mark_node)
12228         decl = error_mark_node;
12229       /* If we know this is a type-name, there's no need to look it
12230          up.  */
12231       else if (typename_p)
12232         decl = identifier;
12233       else
12234         {
12235           /* If the next token is a `::', then the name must be a type
12236              name.
12237
12238              [basic.lookup.qual]
12239
12240              During the lookup for a name preceding the :: scope
12241              resolution operator, object, function, and enumerator
12242              names are ignored.  */
12243           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12244             type_p = true;
12245           /* Look up the name.  */
12246           decl = cp_parser_lookup_name (parser, identifier,
12247                                         type_p,
12248                                         /*is_template=*/false,
12249                                         /*is_namespace=*/false,
12250                                         check_dependency_p);
12251         }
12252     }
12253   else
12254     {
12255       /* Try a template-id.  */
12256       decl = cp_parser_template_id (parser, template_keyword_p,
12257                                     check_dependency_p,
12258                                     is_declaration);
12259       if (decl == error_mark_node)
12260         return error_mark_node;
12261     }
12262
12263   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12264
12265   /* If this is a typename, create a TYPENAME_TYPE.  */
12266   if (typename_p && decl != error_mark_node)
12267     {
12268       decl = make_typename_type (scope, decl, /*complain=*/1);
12269       if (decl != error_mark_node)
12270         decl = TYPE_NAME (decl);
12271     }
12272
12273   /* Check to see that it is really the name of a class.  */
12274   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12275       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12276       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12277     /* Situations like this:
12278
12279          template <typename T> struct A {
12280            typename T::template X<int>::I i;
12281          };
12282
12283        are problematic.  Is `T::template X<int>' a class-name?  The
12284        standard does not seem to be definitive, but there is no other
12285        valid interpretation of the following `::'.  Therefore, those
12286        names are considered class-names.  */
12287     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12288   else if (decl == error_mark_node
12289            || TREE_CODE (decl) != TYPE_DECL
12290            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12291     {
12292       cp_parser_error (parser, "expected class-name");
12293       return error_mark_node;
12294     }
12295
12296   return decl;
12297 }
12298
12299 /* Parse a class-specifier.
12300
12301    class-specifier:
12302      class-head { member-specification [opt] }
12303
12304    Returns the TREE_TYPE representing the class.  */
12305
12306 static tree
12307 cp_parser_class_specifier (cp_parser* parser)
12308 {
12309   cp_token *token;
12310   tree type;
12311   tree attributes = NULL_TREE;
12312   int has_trailing_semicolon;
12313   bool nested_name_specifier_p;
12314   unsigned saved_num_template_parameter_lists;
12315   bool pop_p = false;
12316   tree scope = NULL_TREE;
12317
12318   push_deferring_access_checks (dk_no_deferred);
12319
12320   /* Parse the class-head.  */
12321   type = cp_parser_class_head (parser,
12322                                &nested_name_specifier_p,
12323                                &attributes);
12324   /* If the class-head was a semantic disaster, skip the entire body
12325      of the class.  */
12326   if (!type)
12327     {
12328       cp_parser_skip_to_end_of_block_or_statement (parser);
12329       pop_deferring_access_checks ();
12330       return error_mark_node;
12331     }
12332
12333   /* Look for the `{'.  */
12334   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12335     {
12336       pop_deferring_access_checks ();
12337       return error_mark_node;
12338     }
12339
12340   /* Issue an error message if type-definitions are forbidden here.  */
12341   cp_parser_check_type_definition (parser);
12342   /* Remember that we are defining one more class.  */
12343   ++parser->num_classes_being_defined;
12344   /* Inside the class, surrounding template-parameter-lists do not
12345      apply.  */
12346   saved_num_template_parameter_lists
12347     = parser->num_template_parameter_lists;
12348   parser->num_template_parameter_lists = 0;
12349
12350   /* Start the class.  */
12351   if (nested_name_specifier_p)
12352     {
12353       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12354       pop_p = push_scope (scope);
12355     }
12356   type = begin_class_definition (type);
12357
12358   if (type == error_mark_node)
12359     /* If the type is erroneous, skip the entire body of the class.  */
12360     cp_parser_skip_to_closing_brace (parser);
12361   else
12362     /* Parse the member-specification.  */
12363     cp_parser_member_specification_opt (parser);
12364
12365   /* Look for the trailing `}'.  */
12366   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12367   /* We get better error messages by noticing a common problem: a
12368      missing trailing `;'.  */
12369   token = cp_lexer_peek_token (parser->lexer);
12370   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12371   /* Look for trailing attributes to apply to this class.  */
12372   if (cp_parser_allow_gnu_extensions_p (parser))
12373     {
12374       tree sub_attr = cp_parser_attributes_opt (parser);
12375       attributes = chainon (attributes, sub_attr);
12376     }
12377   if (type != error_mark_node)
12378     type = finish_struct (type, attributes);
12379   if (pop_p)
12380     pop_scope (scope);
12381   /* If this class is not itself within the scope of another class,
12382      then we need to parse the bodies of all of the queued function
12383      definitions.  Note that the queued functions defined in a class
12384      are not always processed immediately following the
12385      class-specifier for that class.  Consider:
12386
12387        struct A {
12388          struct B { void f() { sizeof (A); } };
12389        };
12390
12391      If `f' were processed before the processing of `A' were
12392      completed, there would be no way to compute the size of `A'.
12393      Note that the nesting we are interested in here is lexical --
12394      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12395      for:
12396
12397        struct A { struct B; };
12398        struct A::B { void f() { } };
12399
12400      there is no need to delay the parsing of `A::B::f'.  */
12401   if (--parser->num_classes_being_defined == 0)
12402     {
12403       tree queue_entry;
12404       tree fn;
12405       tree class_type;
12406       bool pop_p;
12407
12408       /* In a first pass, parse default arguments to the functions.
12409          Then, in a second pass, parse the bodies of the functions.
12410          This two-phased approach handles cases like:
12411
12412             struct S {
12413               void f() { g(); }
12414               void g(int i = 3);
12415             };
12416
12417          */
12418       class_type = NULL_TREE;
12419       pop_p = false;
12420       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12421              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12422            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12423            TREE_PURPOSE (parser->unparsed_functions_queues)
12424              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12425         {
12426           fn = TREE_VALUE (queue_entry);
12427           /* If there are default arguments that have not yet been processed,
12428              take care of them now.  */
12429           if (class_type != TREE_PURPOSE (queue_entry))
12430             {
12431               if (pop_p)
12432                 pop_scope (class_type);
12433               class_type = TREE_PURPOSE (queue_entry);
12434               pop_p = push_scope (class_type);
12435             }
12436           /* Make sure that any template parameters are in scope.  */
12437           maybe_begin_member_template_processing (fn);
12438           /* Parse the default argument expressions.  */
12439           cp_parser_late_parsing_default_args (parser, fn);
12440           /* Remove any template parameters from the symbol table.  */
12441           maybe_end_member_template_processing ();
12442         }
12443       if (pop_p)
12444         pop_scope (class_type);
12445       /* Now parse the body of the functions.  */
12446       for (TREE_VALUE (parser->unparsed_functions_queues)
12447              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12448            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12449            TREE_VALUE (parser->unparsed_functions_queues)
12450              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12451         {
12452           /* Figure out which function we need to process.  */
12453           fn = TREE_VALUE (queue_entry);
12454
12455           /* A hack to prevent garbage collection.  */
12456           function_depth++;
12457
12458           /* Parse the function.  */
12459           cp_parser_late_parsing_for_member (parser, fn);
12460           function_depth--;
12461         }
12462     }
12463
12464   /* Put back any saved access checks.  */
12465   pop_deferring_access_checks ();
12466
12467   /* Restore the count of active template-parameter-lists.  */
12468   parser->num_template_parameter_lists
12469     = saved_num_template_parameter_lists;
12470
12471   return type;
12472 }
12473
12474 /* Parse a class-head.
12475
12476    class-head:
12477      class-key identifier [opt] base-clause [opt]
12478      class-key nested-name-specifier identifier base-clause [opt]
12479      class-key nested-name-specifier [opt] template-id
12480        base-clause [opt]
12481
12482    GNU Extensions:
12483      class-key attributes identifier [opt] base-clause [opt]
12484      class-key attributes nested-name-specifier identifier base-clause [opt]
12485      class-key attributes nested-name-specifier [opt] template-id
12486        base-clause [opt]
12487
12488    Returns the TYPE of the indicated class.  Sets
12489    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12490    involving a nested-name-specifier was used, and FALSE otherwise.
12491
12492    Returns NULL_TREE if the class-head is syntactically valid, but
12493    semantically invalid in a way that means we should skip the entire
12494    body of the class.  */
12495
12496 static tree
12497 cp_parser_class_head (cp_parser* parser,
12498                       bool* nested_name_specifier_p,
12499                       tree *attributes_p)
12500 {
12501   tree nested_name_specifier;
12502   enum tag_types class_key;
12503   tree id = NULL_TREE;
12504   tree type = NULL_TREE;
12505   tree attributes;
12506   bool template_id_p = false;
12507   bool qualified_p = false;
12508   bool invalid_nested_name_p = false;
12509   bool invalid_explicit_specialization_p = false;
12510   bool pop_p = false;
12511   unsigned num_templates;
12512   tree bases;
12513
12514   /* Assume no nested-name-specifier will be present.  */
12515   *nested_name_specifier_p = false;
12516   /* Assume no template parameter lists will be used in defining the
12517      type.  */
12518   num_templates = 0;
12519
12520   /* Look for the class-key.  */
12521   class_key = cp_parser_class_key (parser);
12522   if (class_key == none_type)
12523     return error_mark_node;
12524
12525   /* Parse the attributes.  */
12526   attributes = cp_parser_attributes_opt (parser);
12527
12528   /* If the next token is `::', that is invalid -- but sometimes
12529      people do try to write:
12530
12531        struct ::S {};
12532
12533      Handle this gracefully by accepting the extra qualifier, and then
12534      issuing an error about it later if this really is a
12535      class-head.  If it turns out just to be an elaborated type
12536      specifier, remain silent.  */
12537   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12538     qualified_p = true;
12539
12540   push_deferring_access_checks (dk_no_check);
12541
12542   /* Determine the name of the class.  Begin by looking for an
12543      optional nested-name-specifier.  */
12544   nested_name_specifier
12545     = cp_parser_nested_name_specifier_opt (parser,
12546                                            /*typename_keyword_p=*/false,
12547                                            /*check_dependency_p=*/false,
12548                                            /*type_p=*/false,
12549                                            /*is_declaration=*/false);
12550   /* If there was a nested-name-specifier, then there *must* be an
12551      identifier.  */
12552   if (nested_name_specifier)
12553     {
12554       /* Although the grammar says `identifier', it really means
12555          `class-name' or `template-name'.  You are only allowed to
12556          define a class that has already been declared with this
12557          syntax.
12558
12559          The proposed resolution for Core Issue 180 says that whever
12560          you see `class T::X' you should treat `X' as a type-name.
12561
12562          It is OK to define an inaccessible class; for example:
12563
12564            class A { class B; };
12565            class A::B {};
12566
12567          We do not know if we will see a class-name, or a
12568          template-name.  We look for a class-name first, in case the
12569          class-name is a template-id; if we looked for the
12570          template-name first we would stop after the template-name.  */
12571       cp_parser_parse_tentatively (parser);
12572       type = cp_parser_class_name (parser,
12573                                    /*typename_keyword_p=*/false,
12574                                    /*template_keyword_p=*/false,
12575                                    /*type_p=*/true,
12576                                    /*check_dependency_p=*/false,
12577                                    /*class_head_p=*/true,
12578                                    /*is_declaration=*/false);
12579       /* If that didn't work, ignore the nested-name-specifier.  */
12580       if (!cp_parser_parse_definitely (parser))
12581         {
12582           invalid_nested_name_p = true;
12583           id = cp_parser_identifier (parser);
12584           if (id == error_mark_node)
12585             id = NULL_TREE;
12586         }
12587       /* If we could not find a corresponding TYPE, treat this
12588          declaration like an unqualified declaration.  */
12589       if (type == error_mark_node)
12590         nested_name_specifier = NULL_TREE;
12591       /* Otherwise, count the number of templates used in TYPE and its
12592          containing scopes.  */
12593       else
12594         {
12595           tree scope;
12596
12597           for (scope = TREE_TYPE (type);
12598                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12599                scope = (TYPE_P (scope)
12600                         ? TYPE_CONTEXT (scope)
12601                         : DECL_CONTEXT (scope)))
12602             if (TYPE_P (scope)
12603                 && CLASS_TYPE_P (scope)
12604                 && CLASSTYPE_TEMPLATE_INFO (scope)
12605                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12606                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12607               ++num_templates;
12608         }
12609     }
12610   /* Otherwise, the identifier is optional.  */
12611   else
12612     {
12613       /* We don't know whether what comes next is a template-id,
12614          an identifier, or nothing at all.  */
12615       cp_parser_parse_tentatively (parser);
12616       /* Check for a template-id.  */
12617       id = cp_parser_template_id (parser,
12618                                   /*template_keyword_p=*/false,
12619                                   /*check_dependency_p=*/true,
12620                                   /*is_declaration=*/true);
12621       /* If that didn't work, it could still be an identifier.  */
12622       if (!cp_parser_parse_definitely (parser))
12623         {
12624           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12625             id = cp_parser_identifier (parser);
12626           else
12627             id = NULL_TREE;
12628         }
12629       else
12630         {
12631           template_id_p = true;
12632           ++num_templates;
12633         }
12634     }
12635
12636   pop_deferring_access_checks ();
12637
12638   if (id)
12639     cp_parser_check_for_invalid_template_id (parser, id);
12640
12641   /* If it's not a `:' or a `{' then we can't really be looking at a
12642      class-head, since a class-head only appears as part of a
12643      class-specifier.  We have to detect this situation before calling
12644      xref_tag, since that has irreversible side-effects.  */
12645   if (!cp_parser_next_token_starts_class_definition_p (parser))
12646     {
12647       cp_parser_error (parser, "expected `{' or `:'");
12648       return error_mark_node;
12649     }
12650
12651   /* At this point, we're going ahead with the class-specifier, even
12652      if some other problem occurs.  */
12653   cp_parser_commit_to_tentative_parse (parser);
12654   /* Issue the error about the overly-qualified name now.  */
12655   if (qualified_p)
12656     cp_parser_error (parser,
12657                      "global qualification of class name is invalid");
12658   else if (invalid_nested_name_p)
12659     cp_parser_error (parser,
12660                      "qualified name does not name a class");
12661   else if (nested_name_specifier)
12662     {
12663       tree scope;
12664       /* Figure out in what scope the declaration is being placed.  */
12665       scope = current_scope ();
12666       if (!scope)
12667         scope = current_namespace;
12668       /* If that scope does not contain the scope in which the
12669          class was originally declared, the program is invalid.  */
12670       if (scope && !is_ancestor (scope, nested_name_specifier))
12671         {
12672           error ("declaration of `%D' in `%D' which does not "
12673                  "enclose `%D'", type, scope, nested_name_specifier);
12674           type = NULL_TREE;
12675           goto done;
12676         }
12677       /* [dcl.meaning]
12678
12679          A declarator-id shall not be qualified exception of the
12680          definition of a ... nested class outside of its class
12681          ... [or] a the definition or explicit instantiation of a
12682          class member of a namespace outside of its namespace.  */
12683       if (scope == nested_name_specifier)
12684         {
12685           pedwarn ("extra qualification ignored");
12686           nested_name_specifier = NULL_TREE;
12687           num_templates = 0;
12688         }
12689     }
12690   /* An explicit-specialization must be preceded by "template <>".  If
12691      it is not, try to recover gracefully.  */
12692   if (at_namespace_scope_p ()
12693       && parser->num_template_parameter_lists == 0
12694       && template_id_p)
12695     {
12696       error ("an explicit specialization must be preceded by 'template <>'");
12697       invalid_explicit_specialization_p = true;
12698       /* Take the same action that would have been taken by
12699          cp_parser_explicit_specialization.  */
12700       ++parser->num_template_parameter_lists;
12701       begin_specialization ();
12702     }
12703   /* There must be no "return" statements between this point and the
12704      end of this function; set "type "to the correct return value and
12705      use "goto done;" to return.  */
12706   /* Make sure that the right number of template parameters were
12707      present.  */
12708   if (!cp_parser_check_template_parameters (parser, num_templates))
12709     {
12710       /* If something went wrong, there is no point in even trying to
12711          process the class-definition.  */
12712       type = NULL_TREE;
12713       goto done;
12714     }
12715
12716   /* Look up the type.  */
12717   if (template_id_p)
12718     {
12719       type = TREE_TYPE (id);
12720       maybe_process_partial_specialization (type);
12721     }
12722   else if (!nested_name_specifier)
12723     {
12724       /* If the class was unnamed, create a dummy name.  */
12725       if (!id)
12726         id = make_anon_name ();
12727       type = xref_tag (class_key, id, /*globalize=*/false,
12728                        parser->num_template_parameter_lists);
12729     }
12730   else
12731     {
12732       tree class_type;
12733       bool pop_p = false;
12734
12735       /* Given:
12736
12737             template <typename T> struct S { struct T };
12738             template <typename T> struct S<T>::T { };
12739
12740          we will get a TYPENAME_TYPE when processing the definition of
12741          `S::T'.  We need to resolve it to the actual type before we
12742          try to define it.  */
12743       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12744         {
12745           class_type = resolve_typename_type (TREE_TYPE (type),
12746                                               /*only_current_p=*/false);
12747           if (class_type != error_mark_node)
12748             type = TYPE_NAME (class_type);
12749           else
12750             {
12751               cp_parser_error (parser, "could not resolve typename type");
12752               type = error_mark_node;
12753             }
12754         }
12755
12756       maybe_process_partial_specialization (TREE_TYPE (type));
12757       class_type = current_class_type;
12758       /* Enter the scope indicated by the nested-name-specifier.  */
12759       if (nested_name_specifier)
12760         pop_p = push_scope (nested_name_specifier);
12761       /* Get the canonical version of this type.  */
12762       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12763       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12764           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12765         type = push_template_decl (type);
12766       type = TREE_TYPE (type);
12767       if (nested_name_specifier)
12768         {
12769           *nested_name_specifier_p = true;
12770           if (pop_p)
12771             pop_scope (nested_name_specifier);
12772         }
12773     }
12774   /* Indicate whether this class was declared as a `class' or as a
12775      `struct'.  */
12776   if (TREE_CODE (type) == RECORD_TYPE)
12777     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12778   cp_parser_check_class_key (class_key, type);
12779
12780   /* Enter the scope containing the class; the names of base classes
12781      should be looked up in that context.  For example, given:
12782
12783        struct A { struct B {}; struct C; };
12784        struct A::C : B {};
12785
12786      is valid.  */
12787   if (nested_name_specifier)
12788     pop_p = push_scope (nested_name_specifier);
12789
12790   bases = NULL_TREE;
12791
12792   /* Get the list of base-classes, if there is one.  */
12793   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12794     bases = cp_parser_base_clause (parser);
12795
12796   /* Process the base classes.  */
12797   xref_basetypes (type, bases);
12798
12799   /* Leave the scope given by the nested-name-specifier.  We will
12800      enter the class scope itself while processing the members.  */
12801   if (pop_p)
12802     pop_scope (nested_name_specifier);
12803
12804  done:
12805   if (invalid_explicit_specialization_p)
12806     {
12807       end_specialization ();
12808       --parser->num_template_parameter_lists;
12809     }
12810   *attributes_p = attributes;
12811   return type;
12812 }
12813
12814 /* Parse a class-key.
12815
12816    class-key:
12817      class
12818      struct
12819      union
12820
12821    Returns the kind of class-key specified, or none_type to indicate
12822    error.  */
12823
12824 static enum tag_types
12825 cp_parser_class_key (cp_parser* parser)
12826 {
12827   cp_token *token;
12828   enum tag_types tag_type;
12829
12830   /* Look for the class-key.  */
12831   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12832   if (!token)
12833     return none_type;
12834
12835   /* Check to see if the TOKEN is a class-key.  */
12836   tag_type = cp_parser_token_is_class_key (token);
12837   if (!tag_type)
12838     cp_parser_error (parser, "expected class-key");
12839   return tag_type;
12840 }
12841
12842 /* Parse an (optional) member-specification.
12843
12844    member-specification:
12845      member-declaration member-specification [opt]
12846      access-specifier : member-specification [opt]  */
12847
12848 static void
12849 cp_parser_member_specification_opt (cp_parser* parser)
12850 {
12851   while (true)
12852     {
12853       cp_token *token;
12854       enum rid keyword;
12855
12856       /* Peek at the next token.  */
12857       token = cp_lexer_peek_token (parser->lexer);
12858       /* If it's a `}', or EOF then we've seen all the members.  */
12859       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12860         break;
12861
12862       /* See if this token is a keyword.  */
12863       keyword = token->keyword;
12864       switch (keyword)
12865         {
12866         case RID_PUBLIC:
12867         case RID_PROTECTED:
12868         case RID_PRIVATE:
12869           /* Consume the access-specifier.  */
12870           cp_lexer_consume_token (parser->lexer);
12871           /* Remember which access-specifier is active.  */
12872           current_access_specifier = token->value;
12873           /* Look for the `:'.  */
12874           cp_parser_require (parser, CPP_COLON, "`:'");
12875           break;
12876
12877         default:
12878           /* Otherwise, the next construction must be a
12879              member-declaration.  */
12880           cp_parser_member_declaration (parser);
12881         }
12882     }
12883 }
12884
12885 /* Parse a member-declaration.
12886
12887    member-declaration:
12888      decl-specifier-seq [opt] member-declarator-list [opt] ;
12889      function-definition ; [opt]
12890      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12891      using-declaration
12892      template-declaration
12893
12894    member-declarator-list:
12895      member-declarator
12896      member-declarator-list , member-declarator
12897
12898    member-declarator:
12899      declarator pure-specifier [opt]
12900      declarator constant-initializer [opt]
12901      identifier [opt] : constant-expression
12902
12903    GNU Extensions:
12904
12905    member-declaration:
12906      __extension__ member-declaration
12907
12908    member-declarator:
12909      declarator attributes [opt] pure-specifier [opt]
12910      declarator attributes [opt] constant-initializer [opt]
12911      identifier [opt] attributes [opt] : constant-expression  */
12912
12913 static void
12914 cp_parser_member_declaration (cp_parser* parser)
12915 {
12916   cp_decl_specifier_seq decl_specifiers;
12917   tree prefix_attributes;
12918   tree decl;
12919   int declares_class_or_enum;
12920   bool friend_p;
12921   cp_token *token;
12922   int saved_pedantic;
12923
12924   /* Check for the `__extension__' keyword.  */
12925   if (cp_parser_extension_opt (parser, &saved_pedantic))
12926     {
12927       /* Recurse.  */
12928       cp_parser_member_declaration (parser);
12929       /* Restore the old value of the PEDANTIC flag.  */
12930       pedantic = saved_pedantic;
12931
12932       return;
12933     }
12934
12935   /* Check for a template-declaration.  */
12936   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12937     {
12938       /* Parse the template-declaration.  */
12939       cp_parser_template_declaration (parser, /*member_p=*/true);
12940
12941       return;
12942     }
12943
12944   /* Check for a using-declaration.  */
12945   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12946     {
12947       /* Parse the using-declaration.  */
12948       cp_parser_using_declaration (parser);
12949
12950       return;
12951     }
12952
12953   /* Parse the decl-specifier-seq.  */
12954   cp_parser_decl_specifier_seq (parser,
12955                                 CP_PARSER_FLAGS_OPTIONAL,
12956                                 &decl_specifiers,
12957                                 &declares_class_or_enum);
12958   prefix_attributes = decl_specifiers.attributes;
12959   decl_specifiers.attributes = NULL_TREE;
12960   /* Check for an invalid type-name.  */
12961   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12962     return;
12963   /* If there is no declarator, then the decl-specifier-seq should
12964      specify a type.  */
12965   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12966     {
12967       /* If there was no decl-specifier-seq, and the next token is a
12968          `;', then we have something like:
12969
12970            struct S { ; };
12971
12972          [class.mem]
12973
12974          Each member-declaration shall declare at least one member
12975          name of the class.  */
12976       if (!decl_specifiers.any_specifiers_p)
12977         {
12978           if (pedantic)
12979             pedwarn ("extra semicolon");
12980         }
12981       else
12982         {
12983           tree type;
12984
12985           /* See if this declaration is a friend.  */
12986           friend_p = cp_parser_friend_p (&decl_specifiers);
12987           /* If there were decl-specifiers, check to see if there was
12988              a class-declaration.  */
12989           type = check_tag_decl (&decl_specifiers);
12990           /* Nested classes have already been added to the class, but
12991              a `friend' needs to be explicitly registered.  */
12992           if (friend_p)
12993             {
12994               /* If the `friend' keyword was present, the friend must
12995                  be introduced with a class-key.  */
12996                if (!declares_class_or_enum)
12997                  error ("a class-key must be used when declaring a friend");
12998                /* In this case:
12999
13000                     template <typename T> struct A {
13001                       friend struct A<T>::B;
13002                     };
13003
13004                   A<T>::B will be represented by a TYPENAME_TYPE, and
13005                   therefore not recognized by check_tag_decl.  */
13006                if (!type
13007                    && decl_specifiers.type
13008                    && TYPE_P (decl_specifiers.type))
13009                  type = decl_specifiers.type;
13010                if (!type || !TYPE_P (type))
13011                  error ("friend declaration does not name a class or "
13012                         "function");
13013                else
13014                  make_friend_class (current_class_type, type,
13015                                     /*complain=*/true);
13016             }
13017           /* If there is no TYPE, an error message will already have
13018              been issued.  */
13019           else if (!type || type == error_mark_node)
13020             ;
13021           /* An anonymous aggregate has to be handled specially; such
13022              a declaration really declares a data member (with a
13023              particular type), as opposed to a nested class.  */
13024           else if (ANON_AGGR_TYPE_P (type))
13025             {
13026               /* Remove constructors and such from TYPE, now that we
13027                  know it is an anonymous aggregate.  */
13028               fixup_anonymous_aggr (type);
13029               /* And make the corresponding data member.  */
13030               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13031               /* Add it to the class.  */
13032               finish_member_declaration (decl);
13033             }
13034           else
13035             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13036         }
13037     }
13038   else
13039     {
13040       /* See if these declarations will be friends.  */
13041       friend_p = cp_parser_friend_p (&decl_specifiers);
13042
13043       /* Keep going until we hit the `;' at the end of the
13044          declaration.  */
13045       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13046         {
13047           tree attributes = NULL_TREE;
13048           tree first_attribute;
13049
13050           /* Peek at the next token.  */
13051           token = cp_lexer_peek_token (parser->lexer);
13052
13053           /* Check for a bitfield declaration.  */
13054           if (token->type == CPP_COLON
13055               || (token->type == CPP_NAME
13056                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13057                   == CPP_COLON))
13058             {
13059               tree identifier;
13060               tree width;
13061
13062               /* Get the name of the bitfield.  Note that we cannot just
13063                  check TOKEN here because it may have been invalidated by
13064                  the call to cp_lexer_peek_nth_token above.  */
13065               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13066                 identifier = cp_parser_identifier (parser);
13067               else
13068                 identifier = NULL_TREE;
13069
13070               /* Consume the `:' token.  */
13071               cp_lexer_consume_token (parser->lexer);
13072               /* Get the width of the bitfield.  */
13073               width
13074                 = cp_parser_constant_expression (parser,
13075                                                  /*allow_non_constant=*/false,
13076                                                  NULL);
13077
13078               /* Look for attributes that apply to the bitfield.  */
13079               attributes = cp_parser_attributes_opt (parser);
13080               /* Remember which attributes are prefix attributes and
13081                  which are not.  */
13082               first_attribute = attributes;
13083               /* Combine the attributes.  */
13084               attributes = chainon (prefix_attributes, attributes);
13085
13086               /* Create the bitfield declaration.  */
13087               decl = grokbitfield (identifier
13088                                    ? make_id_declarator (identifier)
13089                                    : NULL,
13090                                    &decl_specifiers,
13091                                    width);
13092               /* Apply the attributes.  */
13093               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13094             }
13095           else
13096             {
13097               cp_declarator *declarator;
13098               tree initializer;
13099               tree asm_specification;
13100               int ctor_dtor_or_conv_p;
13101
13102               /* Parse the declarator.  */
13103               declarator
13104                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13105                                         &ctor_dtor_or_conv_p,
13106                                         /*parenthesized_p=*/NULL);
13107
13108               /* If something went wrong parsing the declarator, make sure
13109                  that we at least consume some tokens.  */
13110               if (declarator == cp_error_declarator)
13111                 {
13112                   /* Skip to the end of the statement.  */
13113                   cp_parser_skip_to_end_of_statement (parser);
13114                   /* If the next token is not a semicolon, that is
13115                      probably because we just skipped over the body of
13116                      a function.  So, we consume a semicolon if
13117                      present, but do not issue an error message if it
13118                      is not present.  */
13119                   if (cp_lexer_next_token_is (parser->lexer,
13120                                               CPP_SEMICOLON))
13121                     cp_lexer_consume_token (parser->lexer);
13122                   return;
13123                 }
13124
13125               cp_parser_check_for_definition_in_return_type
13126                 (declarator, declares_class_or_enum);
13127
13128               /* Look for an asm-specification.  */
13129               asm_specification = cp_parser_asm_specification_opt (parser);
13130               /* Look for attributes that apply to the declaration.  */
13131               attributes = cp_parser_attributes_opt (parser);
13132               /* Remember which attributes are prefix attributes and
13133                  which are not.  */
13134               first_attribute = attributes;
13135               /* Combine the attributes.  */
13136               attributes = chainon (prefix_attributes, attributes);
13137
13138               /* If it's an `=', then we have a constant-initializer or a
13139                  pure-specifier.  It is not correct to parse the
13140                  initializer before registering the member declaration
13141                  since the member declaration should be in scope while
13142                  its initializer is processed.  However, the rest of the
13143                  front end does not yet provide an interface that allows
13144                  us to handle this correctly.  */
13145               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13146                 {
13147                   /* In [class.mem]:
13148
13149                      A pure-specifier shall be used only in the declaration of
13150                      a virtual function.
13151
13152                      A member-declarator can contain a constant-initializer
13153                      only if it declares a static member of integral or
13154                      enumeration type.
13155
13156                      Therefore, if the DECLARATOR is for a function, we look
13157                      for a pure-specifier; otherwise, we look for a
13158                      constant-initializer.  When we call `grokfield', it will
13159                      perform more stringent semantics checks.  */
13160                   if (declarator->kind == cdk_function)
13161                     initializer = cp_parser_pure_specifier (parser);
13162                   else
13163                     /* Parse the initializer.  */
13164                     initializer = cp_parser_constant_initializer (parser);
13165                 }
13166               /* Otherwise, there is no initializer.  */
13167               else
13168                 initializer = NULL_TREE;
13169
13170               /* See if we are probably looking at a function
13171                  definition.  We are certainly not looking at at a
13172                  member-declarator.  Calling `grokfield' has
13173                  side-effects, so we must not do it unless we are sure
13174                  that we are looking at a member-declarator.  */
13175               if (cp_parser_token_starts_function_definition_p
13176                   (cp_lexer_peek_token (parser->lexer)))
13177                 {
13178                   /* The grammar does not allow a pure-specifier to be
13179                      used when a member function is defined.  (It is
13180                      possible that this fact is an oversight in the
13181                      standard, since a pure function may be defined
13182                      outside of the class-specifier.  */
13183                   if (initializer)
13184                     error ("pure-specifier on function-definition");
13185                   decl = cp_parser_save_member_function_body (parser,
13186                                                               &decl_specifiers,
13187                                                               declarator,
13188                                                               attributes);
13189                   /* If the member was not a friend, declare it here.  */
13190                   if (!friend_p)
13191                     finish_member_declaration (decl);
13192                   /* Peek at the next token.  */
13193                   token = cp_lexer_peek_token (parser->lexer);
13194                   /* If the next token is a semicolon, consume it.  */
13195                   if (token->type == CPP_SEMICOLON)
13196                     cp_lexer_consume_token (parser->lexer);
13197                   return;
13198                 }
13199               else
13200                 {
13201                   /* Create the declaration.  */
13202                   decl = grokfield (declarator, &decl_specifiers,
13203                                     initializer, asm_specification,
13204                                     attributes);
13205                   /* Any initialization must have been from a
13206                      constant-expression.  */
13207                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13208                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13209                 }
13210             }
13211
13212           /* Reset PREFIX_ATTRIBUTES.  */
13213           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13214             attributes = TREE_CHAIN (attributes);
13215           if (attributes)
13216             TREE_CHAIN (attributes) = NULL_TREE;
13217
13218           /* If there is any qualification still in effect, clear it
13219              now; we will be starting fresh with the next declarator.  */
13220           parser->scope = NULL_TREE;
13221           parser->qualifying_scope = NULL_TREE;
13222           parser->object_scope = NULL_TREE;
13223           /* If it's a `,', then there are more declarators.  */
13224           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13225             cp_lexer_consume_token (parser->lexer);
13226           /* If the next token isn't a `;', then we have a parse error.  */
13227           else if (cp_lexer_next_token_is_not (parser->lexer,
13228                                                CPP_SEMICOLON))
13229             {
13230               cp_parser_error (parser, "expected `;'");
13231               /* Skip tokens until we find a `;'.  */
13232               cp_parser_skip_to_end_of_statement (parser);
13233
13234               break;
13235             }
13236
13237           if (decl)
13238             {
13239               /* Add DECL to the list of members.  */
13240               if (!friend_p)
13241                 finish_member_declaration (decl);
13242
13243               if (TREE_CODE (decl) == FUNCTION_DECL)
13244                 cp_parser_save_default_args (parser, decl);
13245             }
13246         }
13247     }
13248
13249   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13250 }
13251
13252 /* Parse a pure-specifier.
13253
13254    pure-specifier:
13255      = 0
13256
13257    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13258    Otherwise, ERROR_MARK_NODE is returned.  */
13259
13260 static tree
13261 cp_parser_pure_specifier (cp_parser* parser)
13262 {
13263   cp_token *token;
13264
13265   /* Look for the `=' token.  */
13266   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13267     return error_mark_node;
13268   /* Look for the `0' token.  */
13269   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13270   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13271      to get information from the lexer about how the number was
13272      spelled in order to fix this problem.  */
13273   if (!token || !integer_zerop (token->value))
13274     return error_mark_node;
13275
13276   return integer_zero_node;
13277 }
13278
13279 /* Parse a constant-initializer.
13280
13281    constant-initializer:
13282      = constant-expression
13283
13284    Returns a representation of the constant-expression.  */
13285
13286 static tree
13287 cp_parser_constant_initializer (cp_parser* parser)
13288 {
13289   /* Look for the `=' token.  */
13290   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13291     return error_mark_node;
13292
13293   /* It is invalid to write:
13294
13295        struct S { static const int i = { 7 }; };
13296
13297      */
13298   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13299     {
13300       cp_parser_error (parser,
13301                        "a brace-enclosed initializer is not allowed here");
13302       /* Consume the opening brace.  */
13303       cp_lexer_consume_token (parser->lexer);
13304       /* Skip the initializer.  */
13305       cp_parser_skip_to_closing_brace (parser);
13306       /* Look for the trailing `}'.  */
13307       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13308
13309       return error_mark_node;
13310     }
13311
13312   return cp_parser_constant_expression (parser,
13313                                         /*allow_non_constant=*/false,
13314                                         NULL);
13315 }
13316
13317 /* Derived classes [gram.class.derived] */
13318
13319 /* Parse a base-clause.
13320
13321    base-clause:
13322      : base-specifier-list
13323
13324    base-specifier-list:
13325      base-specifier
13326      base-specifier-list , base-specifier
13327
13328    Returns a TREE_LIST representing the base-classes, in the order in
13329    which they were declared.  The representation of each node is as
13330    described by cp_parser_base_specifier.
13331
13332    In the case that no bases are specified, this function will return
13333    NULL_TREE, not ERROR_MARK_NODE.  */
13334
13335 static tree
13336 cp_parser_base_clause (cp_parser* parser)
13337 {
13338   tree bases = NULL_TREE;
13339
13340   /* Look for the `:' that begins the list.  */
13341   cp_parser_require (parser, CPP_COLON, "`:'");
13342
13343   /* Scan the base-specifier-list.  */
13344   while (true)
13345     {
13346       cp_token *token;
13347       tree base;
13348
13349       /* Look for the base-specifier.  */
13350       base = cp_parser_base_specifier (parser);
13351       /* Add BASE to the front of the list.  */
13352       if (base != error_mark_node)
13353         {
13354           TREE_CHAIN (base) = bases;
13355           bases = base;
13356         }
13357       /* Peek at the next token.  */
13358       token = cp_lexer_peek_token (parser->lexer);
13359       /* If it's not a comma, then the list is complete.  */
13360       if (token->type != CPP_COMMA)
13361         break;
13362       /* Consume the `,'.  */
13363       cp_lexer_consume_token (parser->lexer);
13364     }
13365
13366   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13367      base class had a qualified name.  However, the next name that
13368      appears is certainly not qualified.  */
13369   parser->scope = NULL_TREE;
13370   parser->qualifying_scope = NULL_TREE;
13371   parser->object_scope = NULL_TREE;
13372
13373   return nreverse (bases);
13374 }
13375
13376 /* Parse a base-specifier.
13377
13378    base-specifier:
13379      :: [opt] nested-name-specifier [opt] class-name
13380      virtual access-specifier [opt] :: [opt] nested-name-specifier
13381        [opt] class-name
13382      access-specifier virtual [opt] :: [opt] nested-name-specifier
13383        [opt] class-name
13384
13385    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13386    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13387    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13388    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13389
13390 static tree
13391 cp_parser_base_specifier (cp_parser* parser)
13392 {
13393   cp_token *token;
13394   bool done = false;
13395   bool virtual_p = false;
13396   bool duplicate_virtual_error_issued_p = false;
13397   bool duplicate_access_error_issued_p = false;
13398   bool class_scope_p, template_p;
13399   tree access = access_default_node;
13400   tree type;
13401
13402   /* Process the optional `virtual' and `access-specifier'.  */
13403   while (!done)
13404     {
13405       /* Peek at the next token.  */
13406       token = cp_lexer_peek_token (parser->lexer);
13407       /* Process `virtual'.  */
13408       switch (token->keyword)
13409         {
13410         case RID_VIRTUAL:
13411           /* If `virtual' appears more than once, issue an error.  */
13412           if (virtual_p && !duplicate_virtual_error_issued_p)
13413             {
13414               cp_parser_error (parser,
13415                                "`virtual' specified more than once in base-specified");
13416               duplicate_virtual_error_issued_p = true;
13417             }
13418
13419           virtual_p = true;
13420
13421           /* Consume the `virtual' token.  */
13422           cp_lexer_consume_token (parser->lexer);
13423
13424           break;
13425
13426         case RID_PUBLIC:
13427         case RID_PROTECTED:
13428         case RID_PRIVATE:
13429           /* If more than one access specifier appears, issue an
13430              error.  */
13431           if (access != access_default_node
13432               && !duplicate_access_error_issued_p)
13433             {
13434               cp_parser_error (parser,
13435                                "more than one access specifier in base-specified");
13436               duplicate_access_error_issued_p = true;
13437             }
13438
13439           access = ridpointers[(int) token->keyword];
13440
13441           /* Consume the access-specifier.  */
13442           cp_lexer_consume_token (parser->lexer);
13443
13444           break;
13445
13446         default:
13447           done = true;
13448           break;
13449         }
13450     }
13451   /* It is not uncommon to see programs mechanically, erroneously, use
13452      the 'typename' keyword to denote (dependent) qualified types
13453      as base classes.  */
13454   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13455     {
13456       if (!processing_template_decl)
13457         error ("keyword `typename' not allowed outside of templates");
13458       else
13459         error ("keyword `typename' not allowed in this context "
13460                "(the base class is implicitly a type)");
13461       cp_lexer_consume_token (parser->lexer);
13462     }
13463
13464   /* Look for the optional `::' operator.  */
13465   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13466   /* Look for the nested-name-specifier.  The simplest way to
13467      implement:
13468
13469        [temp.res]
13470
13471        The keyword `typename' is not permitted in a base-specifier or
13472        mem-initializer; in these contexts a qualified name that
13473        depends on a template-parameter is implicitly assumed to be a
13474        type name.
13475
13476      is to pretend that we have seen the `typename' keyword at this
13477      point.  */
13478   cp_parser_nested_name_specifier_opt (parser,
13479                                        /*typename_keyword_p=*/true,
13480                                        /*check_dependency_p=*/true,
13481                                        /*type_p=*/true,
13482                                        /*is_declaration=*/true);
13483   /* If the base class is given by a qualified name, assume that names
13484      we see are type names or templates, as appropriate.  */
13485   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13486   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13487
13488   /* Finally, look for the class-name.  */
13489   type = cp_parser_class_name (parser,
13490                                class_scope_p,
13491                                template_p,
13492                                /*type_p=*/true,
13493                                /*check_dependency_p=*/true,
13494                                /*class_head_p=*/false,
13495                                /*is_declaration=*/true);
13496
13497   if (type == error_mark_node)
13498     return error_mark_node;
13499
13500   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13501 }
13502
13503 /* Exception handling [gram.exception] */
13504
13505 /* Parse an (optional) exception-specification.
13506
13507    exception-specification:
13508      throw ( type-id-list [opt] )
13509
13510    Returns a TREE_LIST representing the exception-specification.  The
13511    TREE_VALUE of each node is a type.  */
13512
13513 static tree
13514 cp_parser_exception_specification_opt (cp_parser* parser)
13515 {
13516   cp_token *token;
13517   tree type_id_list;
13518
13519   /* Peek at the next token.  */
13520   token = cp_lexer_peek_token (parser->lexer);
13521   /* If it's not `throw', then there's no exception-specification.  */
13522   if (!cp_parser_is_keyword (token, RID_THROW))
13523     return NULL_TREE;
13524
13525   /* Consume the `throw'.  */
13526   cp_lexer_consume_token (parser->lexer);
13527
13528   /* Look for the `('.  */
13529   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13530
13531   /* Peek at the next token.  */
13532   token = cp_lexer_peek_token (parser->lexer);
13533   /* If it's not a `)', then there is a type-id-list.  */
13534   if (token->type != CPP_CLOSE_PAREN)
13535     {
13536       const char *saved_message;
13537
13538       /* Types may not be defined in an exception-specification.  */
13539       saved_message = parser->type_definition_forbidden_message;
13540       parser->type_definition_forbidden_message
13541         = "types may not be defined in an exception-specification";
13542       /* Parse the type-id-list.  */
13543       type_id_list = cp_parser_type_id_list (parser);
13544       /* Restore the saved message.  */
13545       parser->type_definition_forbidden_message = saved_message;
13546     }
13547   else
13548     type_id_list = empty_except_spec;
13549
13550   /* Look for the `)'.  */
13551   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13552
13553   return type_id_list;
13554 }
13555
13556 /* Parse an (optional) type-id-list.
13557
13558    type-id-list:
13559      type-id
13560      type-id-list , type-id
13561
13562    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13563    in the order that the types were presented.  */
13564
13565 static tree
13566 cp_parser_type_id_list (cp_parser* parser)
13567 {
13568   tree types = NULL_TREE;
13569
13570   while (true)
13571     {
13572       cp_token *token;
13573       tree type;
13574
13575       /* Get the next type-id.  */
13576       type = cp_parser_type_id (parser);
13577       /* Add it to the list.  */
13578       types = add_exception_specifier (types, type, /*complain=*/1);
13579       /* Peek at the next token.  */
13580       token = cp_lexer_peek_token (parser->lexer);
13581       /* If it is not a `,', we are done.  */
13582       if (token->type != CPP_COMMA)
13583         break;
13584       /* Consume the `,'.  */
13585       cp_lexer_consume_token (parser->lexer);
13586     }
13587
13588   return nreverse (types);
13589 }
13590
13591 /* Parse a try-block.
13592
13593    try-block:
13594      try compound-statement handler-seq  */
13595
13596 static tree
13597 cp_parser_try_block (cp_parser* parser)
13598 {
13599   tree try_block;
13600
13601   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13602   try_block = begin_try_block ();
13603   cp_parser_compound_statement (parser, NULL, true);
13604   finish_try_block (try_block);
13605   cp_parser_handler_seq (parser);
13606   finish_handler_sequence (try_block);
13607
13608   return try_block;
13609 }
13610
13611 /* Parse a function-try-block.
13612
13613    function-try-block:
13614      try ctor-initializer [opt] function-body handler-seq  */
13615
13616 static bool
13617 cp_parser_function_try_block (cp_parser* parser)
13618 {
13619   tree try_block;
13620   bool ctor_initializer_p;
13621
13622   /* Look for the `try' keyword.  */
13623   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13624     return false;
13625   /* Let the rest of the front-end know where we are.  */
13626   try_block = begin_function_try_block ();
13627   /* Parse the function-body.  */
13628   ctor_initializer_p
13629     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13630   /* We're done with the `try' part.  */
13631   finish_function_try_block (try_block);
13632   /* Parse the handlers.  */
13633   cp_parser_handler_seq (parser);
13634   /* We're done with the handlers.  */
13635   finish_function_handler_sequence (try_block);
13636
13637   return ctor_initializer_p;
13638 }
13639
13640 /* Parse a handler-seq.
13641
13642    handler-seq:
13643      handler handler-seq [opt]  */
13644
13645 static void
13646 cp_parser_handler_seq (cp_parser* parser)
13647 {
13648   while (true)
13649     {
13650       cp_token *token;
13651
13652       /* Parse the handler.  */
13653       cp_parser_handler (parser);
13654       /* Peek at the next token.  */
13655       token = cp_lexer_peek_token (parser->lexer);
13656       /* If it's not `catch' then there are no more handlers.  */
13657       if (!cp_parser_is_keyword (token, RID_CATCH))
13658         break;
13659     }
13660 }
13661
13662 /* Parse a handler.
13663
13664    handler:
13665      catch ( exception-declaration ) compound-statement  */
13666
13667 static void
13668 cp_parser_handler (cp_parser* parser)
13669 {
13670   tree handler;
13671   tree declaration;
13672
13673   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13674   handler = begin_handler ();
13675   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13676   declaration = cp_parser_exception_declaration (parser);
13677   finish_handler_parms (declaration, handler);
13678   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13679   cp_parser_compound_statement (parser, NULL, false);
13680   finish_handler (handler);
13681 }
13682
13683 /* Parse an exception-declaration.
13684
13685    exception-declaration:
13686      type-specifier-seq declarator
13687      type-specifier-seq abstract-declarator
13688      type-specifier-seq
13689      ...
13690
13691    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13692    ellipsis variant is used.  */
13693
13694 static tree
13695 cp_parser_exception_declaration (cp_parser* parser)
13696 {
13697   tree decl;
13698   cp_decl_specifier_seq type_specifiers;
13699   cp_declarator *declarator;
13700   const char *saved_message;
13701
13702   /* If it's an ellipsis, it's easy to handle.  */
13703   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13704     {
13705       /* Consume the `...' token.  */
13706       cp_lexer_consume_token (parser->lexer);
13707       return NULL_TREE;
13708     }
13709
13710   /* Types may not be defined in exception-declarations.  */
13711   saved_message = parser->type_definition_forbidden_message;
13712   parser->type_definition_forbidden_message
13713     = "types may not be defined in exception-declarations";
13714
13715   /* Parse the type-specifier-seq.  */
13716   cp_parser_type_specifier_seq (parser, &type_specifiers);
13717   /* If it's a `)', then there is no declarator.  */
13718   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13719     declarator = NULL;
13720   else
13721     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13722                                        /*ctor_dtor_or_conv_p=*/NULL,
13723                                        /*parenthesized_p=*/NULL);
13724
13725   /* Restore the saved message.  */
13726   parser->type_definition_forbidden_message = saved_message;
13727
13728   if (type_specifiers.any_specifiers_p)
13729     {
13730       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13731       if (decl == NULL_TREE)
13732         error ("invalid catch parameter");
13733     }
13734   else
13735     decl = NULL_TREE;
13736
13737   return decl;
13738 }
13739
13740 /* Parse a throw-expression.
13741
13742    throw-expression:
13743      throw assignment-expression [opt]
13744
13745    Returns a THROW_EXPR representing the throw-expression.  */
13746
13747 static tree
13748 cp_parser_throw_expression (cp_parser* parser)
13749 {
13750   tree expression;
13751   cp_token* token;
13752
13753   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13754   token = cp_lexer_peek_token (parser->lexer);
13755   /* Figure out whether or not there is an assignment-expression
13756      following the "throw" keyword.  */
13757   if (token->type == CPP_COMMA
13758       || token->type == CPP_SEMICOLON
13759       || token->type == CPP_CLOSE_PAREN
13760       || token->type == CPP_CLOSE_SQUARE
13761       || token->type == CPP_CLOSE_BRACE
13762       || token->type == CPP_COLON)
13763     expression = NULL_TREE;
13764   else
13765     expression = cp_parser_assignment_expression (parser);
13766
13767   return build_throw (expression);
13768 }
13769
13770 /* GNU Extensions */
13771
13772 /* Parse an (optional) asm-specification.
13773
13774    asm-specification:
13775      asm ( string-literal )
13776
13777    If the asm-specification is present, returns a STRING_CST
13778    corresponding to the string-literal.  Otherwise, returns
13779    NULL_TREE.  */
13780
13781 static tree
13782 cp_parser_asm_specification_opt (cp_parser* parser)
13783 {
13784   cp_token *token;
13785   tree asm_specification;
13786
13787   /* Peek at the next token.  */
13788   token = cp_lexer_peek_token (parser->lexer);
13789   /* If the next token isn't the `asm' keyword, then there's no
13790      asm-specification.  */
13791   if (!cp_parser_is_keyword (token, RID_ASM))
13792     return NULL_TREE;
13793
13794   /* Consume the `asm' token.  */
13795   cp_lexer_consume_token (parser->lexer);
13796   /* Look for the `('.  */
13797   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13798
13799   /* Look for the string-literal.  */
13800   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13801   if (token)
13802     asm_specification = token->value;
13803   else
13804     asm_specification = NULL_TREE;
13805
13806   /* Look for the `)'.  */
13807   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13808
13809   return asm_specification;
13810 }
13811
13812 /* Parse an asm-operand-list.
13813
13814    asm-operand-list:
13815      asm-operand
13816      asm-operand-list , asm-operand
13817
13818    asm-operand:
13819      string-literal ( expression )
13820      [ string-literal ] string-literal ( expression )
13821
13822    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13823    each node is the expression.  The TREE_PURPOSE is itself a
13824    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13825    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13826    is a STRING_CST for the string literal before the parenthesis.  */
13827
13828 static tree
13829 cp_parser_asm_operand_list (cp_parser* parser)
13830 {
13831   tree asm_operands = NULL_TREE;
13832
13833   while (true)
13834     {
13835       tree string_literal;
13836       tree expression;
13837       tree name;
13838       cp_token *token;
13839
13840       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13841         {
13842           /* Consume the `[' token.  */
13843           cp_lexer_consume_token (parser->lexer);
13844           /* Read the operand name.  */
13845           name = cp_parser_identifier (parser);
13846           if (name != error_mark_node)
13847             name = build_string (IDENTIFIER_LENGTH (name),
13848                                  IDENTIFIER_POINTER (name));
13849           /* Look for the closing `]'.  */
13850           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13851         }
13852       else
13853         name = NULL_TREE;
13854       /* Look for the string-literal.  */
13855       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13856       string_literal = token ? token->value : error_mark_node;
13857       c_lex_string_translate = 1;
13858       /* Look for the `('.  */
13859       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13860       /* Parse the expression.  */
13861       expression = cp_parser_expression (parser);
13862       /* Look for the `)'.  */
13863       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13864       c_lex_string_translate = 0;
13865       /* Add this operand to the list.  */
13866       asm_operands = tree_cons (build_tree_list (name, string_literal),
13867                                 expression,
13868                                 asm_operands);
13869       /* If the next token is not a `,', there are no more
13870          operands.  */
13871       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13872         break;
13873       /* Consume the `,'.  */
13874       cp_lexer_consume_token (parser->lexer);
13875     }
13876
13877   return nreverse (asm_operands);
13878 }
13879
13880 /* Parse an asm-clobber-list.
13881
13882    asm-clobber-list:
13883      string-literal
13884      asm-clobber-list , string-literal
13885
13886    Returns a TREE_LIST, indicating the clobbers in the order that they
13887    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13888
13889 static tree
13890 cp_parser_asm_clobber_list (cp_parser* parser)
13891 {
13892   tree clobbers = NULL_TREE;
13893
13894   while (true)
13895     {
13896       cp_token *token;
13897       tree string_literal;
13898
13899       /* Look for the string literal.  */
13900       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13901       string_literal = token ? token->value : error_mark_node;
13902       /* Add it to the list.  */
13903       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13904       /* If the next token is not a `,', then the list is
13905          complete.  */
13906       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13907         break;
13908       /* Consume the `,' token.  */
13909       cp_lexer_consume_token (parser->lexer);
13910     }
13911
13912   return clobbers;
13913 }
13914
13915 /* Parse an (optional) series of attributes.
13916
13917    attributes:
13918      attributes attribute
13919
13920    attribute:
13921      __attribute__ (( attribute-list [opt] ))
13922
13923    The return value is as for cp_parser_attribute_list.  */
13924
13925 static tree
13926 cp_parser_attributes_opt (cp_parser* parser)
13927 {
13928   tree attributes = NULL_TREE;
13929
13930   while (true)
13931     {
13932       cp_token *token;
13933       tree attribute_list;
13934
13935       /* Peek at the next token.  */
13936       token = cp_lexer_peek_token (parser->lexer);
13937       /* If it's not `__attribute__', then we're done.  */
13938       if (token->keyword != RID_ATTRIBUTE)
13939         break;
13940
13941       /* Consume the `__attribute__' keyword.  */
13942       cp_lexer_consume_token (parser->lexer);
13943       /* Look for the two `(' tokens.  */
13944       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13945       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13946
13947       /* Peek at the next token.  */
13948       token = cp_lexer_peek_token (parser->lexer);
13949       if (token->type != CPP_CLOSE_PAREN)
13950         /* Parse the attribute-list.  */
13951         attribute_list = cp_parser_attribute_list (parser);
13952       else
13953         /* If the next token is a `)', then there is no attribute
13954            list.  */
13955         attribute_list = NULL;
13956
13957       /* Look for the two `)' tokens.  */
13958       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13959       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13960
13961       /* Add these new attributes to the list.  */
13962       attributes = chainon (attributes, attribute_list);
13963     }
13964
13965   return attributes;
13966 }
13967
13968 /* Parse an attribute-list.
13969
13970    attribute-list:
13971      attribute
13972      attribute-list , attribute
13973
13974    attribute:
13975      identifier
13976      identifier ( identifier )
13977      identifier ( identifier , expression-list )
13978      identifier ( expression-list )
13979
13980    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13981    TREE_PURPOSE of each node is the identifier indicating which
13982    attribute is in use.  The TREE_VALUE represents the arguments, if
13983    any.  */
13984
13985 static tree
13986 cp_parser_attribute_list (cp_parser* parser)
13987 {
13988   tree attribute_list = NULL_TREE;
13989
13990   c_lex_string_translate = 0;
13991   while (true)
13992     {
13993       cp_token *token;
13994       tree identifier;
13995       tree attribute;
13996
13997       /* Look for the identifier.  We also allow keywords here; for
13998          example `__attribute__ ((const))' is legal.  */
13999       token = cp_lexer_peek_token (parser->lexer);
14000       if (token->type != CPP_NAME
14001           && token->type != CPP_KEYWORD)
14002         return error_mark_node;
14003       /* Consume the token.  */
14004       token = cp_lexer_consume_token (parser->lexer);
14005
14006       /* Save away the identifier that indicates which attribute this is.  */
14007       identifier = token->value;
14008       attribute = build_tree_list (identifier, NULL_TREE);
14009
14010       /* Peek at the next token.  */
14011       token = cp_lexer_peek_token (parser->lexer);
14012       /* If it's an `(', then parse the attribute arguments.  */
14013       if (token->type == CPP_OPEN_PAREN)
14014         {
14015           tree arguments;
14016
14017           arguments = (cp_parser_parenthesized_expression_list
14018                        (parser, true, /*non_constant_p=*/NULL));
14019           /* Save the identifier and arguments away.  */
14020           TREE_VALUE (attribute) = arguments;
14021         }
14022
14023       /* Add this attribute to the list.  */
14024       TREE_CHAIN (attribute) = attribute_list;
14025       attribute_list = attribute;
14026
14027       /* Now, look for more attributes.  */
14028       token = cp_lexer_peek_token (parser->lexer);
14029       /* If the next token isn't a `,', we're done.  */
14030       if (token->type != CPP_COMMA)
14031         break;
14032
14033       /* Consume the comma and keep going.  */
14034       cp_lexer_consume_token (parser->lexer);
14035     }
14036   c_lex_string_translate = 1;
14037
14038   /* We built up the list in reverse order.  */
14039   return nreverse (attribute_list);
14040 }
14041
14042 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14043    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14044    current value of the PEDANTIC flag, regardless of whether or not
14045    the `__extension__' keyword is present.  The caller is responsible
14046    for restoring the value of the PEDANTIC flag.  */
14047
14048 static bool
14049 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14050 {
14051   /* Save the old value of the PEDANTIC flag.  */
14052   *saved_pedantic = pedantic;
14053
14054   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14055     {
14056       /* Consume the `__extension__' token.  */
14057       cp_lexer_consume_token (parser->lexer);
14058       /* We're not being pedantic while the `__extension__' keyword is
14059          in effect.  */
14060       pedantic = 0;
14061
14062       return true;
14063     }
14064
14065   return false;
14066 }
14067
14068 /* Parse a label declaration.
14069
14070    label-declaration:
14071      __label__ label-declarator-seq ;
14072
14073    label-declarator-seq:
14074      identifier , label-declarator-seq
14075      identifier  */
14076
14077 static void
14078 cp_parser_label_declaration (cp_parser* parser)
14079 {
14080   /* Look for the `__label__' keyword.  */
14081   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14082
14083   while (true)
14084     {
14085       tree identifier;
14086
14087       /* Look for an identifier.  */
14088       identifier = cp_parser_identifier (parser);
14089       /* Declare it as a lobel.  */
14090       finish_label_decl (identifier);
14091       /* If the next token is a `;', stop.  */
14092       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14093         break;
14094       /* Look for the `,' separating the label declarations.  */
14095       cp_parser_require (parser, CPP_COMMA, "`,'");
14096     }
14097
14098   /* Look for the final `;'.  */
14099   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14100 }
14101
14102 /* Support Functions */
14103
14104 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14105    NAME should have one of the representations used for an
14106    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14107    is returned.  If PARSER->SCOPE is a dependent type, then a
14108    SCOPE_REF is returned.
14109
14110    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14111    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14112    was formed.  Abstractly, such entities should not be passed to this
14113    function, because they do not need to be looked up, but it is
14114    simpler to check for this special case here, rather than at the
14115    call-sites.
14116
14117    In cases not explicitly covered above, this function returns a
14118    DECL, OVERLOAD, or baselink representing the result of the lookup.
14119    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14120    is returned.
14121
14122    If IS_TYPE is TRUE, bindings that do not refer to types are
14123    ignored.
14124
14125    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14126    ignored.
14127
14128    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14129    are ignored.
14130
14131    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14132    types.  */
14133
14134 static tree
14135 cp_parser_lookup_name (cp_parser *parser, tree name,
14136                        bool is_type, bool is_template, bool is_namespace,
14137                        bool check_dependency)
14138 {
14139   tree decl;
14140   tree object_type = parser->context->object_type;
14141
14142   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14143      no longer valid.  Note that if we are parsing tentatively, and
14144      the parse fails, OBJECT_TYPE will be automatically restored.  */
14145   parser->context->object_type = NULL_TREE;
14146
14147   if (name == error_mark_node)
14148     return error_mark_node;
14149
14150   /* A template-id has already been resolved; there is no lookup to
14151      do.  */
14152   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14153     return name;
14154   if (BASELINK_P (name))
14155     {
14156       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
14157                            == TEMPLATE_ID_EXPR),
14158                           20020909);
14159       return name;
14160     }
14161
14162   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14163      it should already have been checked to make sure that the name
14164      used matches the type being destroyed.  */
14165   if (TREE_CODE (name) == BIT_NOT_EXPR)
14166     {
14167       tree type;
14168
14169       /* Figure out to which type this destructor applies.  */
14170       if (parser->scope)
14171         type = parser->scope;
14172       else if (object_type)
14173         type = object_type;
14174       else
14175         type = current_class_type;
14176       /* If that's not a class type, there is no destructor.  */
14177       if (!type || !CLASS_TYPE_P (type))
14178         return error_mark_node;
14179       if (!CLASSTYPE_DESTRUCTORS (type))
14180           return error_mark_node;
14181       /* If it was a class type, return the destructor.  */
14182       return CLASSTYPE_DESTRUCTORS (type);
14183     }
14184
14185   /* By this point, the NAME should be an ordinary identifier.  If
14186      the id-expression was a qualified name, the qualifying scope is
14187      stored in PARSER->SCOPE at this point.  */
14188   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
14189                       20000619);
14190
14191   /* Perform the lookup.  */
14192   if (parser->scope)
14193     {
14194       bool dependent_p;
14195
14196       if (parser->scope == error_mark_node)
14197         return error_mark_node;
14198
14199       /* If the SCOPE is dependent, the lookup must be deferred until
14200          the template is instantiated -- unless we are explicitly
14201          looking up names in uninstantiated templates.  Even then, we
14202          cannot look up the name if the scope is not a class type; it
14203          might, for example, be a template type parameter.  */
14204       dependent_p = (TYPE_P (parser->scope)
14205                      && !(parser->in_declarator_p
14206                           && currently_open_class (parser->scope))
14207                      && dependent_type_p (parser->scope));
14208       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14209            && dependent_p)
14210         {
14211           if (is_type)
14212             /* The resolution to Core Issue 180 says that `struct A::B'
14213                should be considered a type-name, even if `A' is
14214                dependent.  */
14215             decl = TYPE_NAME (make_typename_type (parser->scope,
14216                                                   name,
14217                                                   /*complain=*/1));
14218           else if (is_template)
14219             decl = make_unbound_class_template (parser->scope,
14220                                                 name,
14221                                                 /*complain=*/1);
14222           else
14223             decl = build_nt (SCOPE_REF, parser->scope, name);
14224         }
14225       else
14226         {
14227           bool pop_p = false;
14228
14229           /* If PARSER->SCOPE is a dependent type, then it must be a
14230              class type, and we must not be checking dependencies;
14231              otherwise, we would have processed this lookup above.  So
14232              that PARSER->SCOPE is not considered a dependent base by
14233              lookup_member, we must enter the scope here.  */
14234           if (dependent_p)
14235             pop_p = push_scope (parser->scope);
14236           /* If the PARSER->SCOPE is a a template specialization, it
14237              may be instantiated during name lookup.  In that case,
14238              errors may be issued.  Even if we rollback the current
14239              tentative parse, those errors are valid.  */
14240           decl = lookup_qualified_name (parser->scope, name, is_type,
14241                                         /*complain=*/true);
14242           if (pop_p)
14243             pop_scope (parser->scope);
14244         }
14245       parser->qualifying_scope = parser->scope;
14246       parser->object_scope = NULL_TREE;
14247     }
14248   else if (object_type)
14249     {
14250       tree object_decl = NULL_TREE;
14251       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14252          OBJECT_TYPE is not a class.  */
14253       if (CLASS_TYPE_P (object_type))
14254         /* If the OBJECT_TYPE is a template specialization, it may
14255            be instantiated during name lookup.  In that case, errors
14256            may be issued.  Even if we rollback the current tentative
14257            parse, those errors are valid.  */
14258         object_decl = lookup_member (object_type,
14259                                      name,
14260                                      /*protect=*/0, is_type);
14261       /* Look it up in the enclosing context, too.  */
14262       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14263                                /*block_p=*/true, is_namespace,
14264                                /*flags=*/0);
14265       parser->object_scope = object_type;
14266       parser->qualifying_scope = NULL_TREE;
14267       if (object_decl)
14268         decl = object_decl;
14269     }
14270   else
14271     {
14272       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14273                                /*block_p=*/true, is_namespace,
14274                                /*flags=*/0);
14275       parser->qualifying_scope = NULL_TREE;
14276       parser->object_scope = NULL_TREE;
14277     }
14278
14279   /* If the lookup failed, let our caller know.  */
14280   if (!decl
14281       || decl == error_mark_node
14282       || (TREE_CODE (decl) == FUNCTION_DECL
14283           && DECL_ANTICIPATED (decl)))
14284     return error_mark_node;
14285
14286   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14287   if (TREE_CODE (decl) == TREE_LIST)
14288     {
14289       /* The error message we have to print is too complicated for
14290          cp_parser_error, so we incorporate its actions directly.  */
14291       if (!cp_parser_simulate_error (parser))
14292         {
14293           error ("reference to `%D' is ambiguous", name);
14294           print_candidates (decl);
14295         }
14296       return error_mark_node;
14297     }
14298
14299   my_friendly_assert (DECL_P (decl)
14300                       || TREE_CODE (decl) == OVERLOAD
14301                       || TREE_CODE (decl) == SCOPE_REF
14302                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14303                       || BASELINK_P (decl),
14304                       20000619);
14305
14306   /* If we have resolved the name of a member declaration, check to
14307      see if the declaration is accessible.  When the name resolves to
14308      set of overloaded functions, accessibility is checked when
14309      overload resolution is done.
14310
14311      During an explicit instantiation, access is not checked at all,
14312      as per [temp.explicit].  */
14313   if (DECL_P (decl))
14314     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14315
14316   return decl;
14317 }
14318
14319 /* Like cp_parser_lookup_name, but for use in the typical case where
14320    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14321    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14322
14323 static tree
14324 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14325 {
14326   return cp_parser_lookup_name (parser, name,
14327                                 /*is_type=*/false,
14328                                 /*is_template=*/false,
14329                                 /*is_namespace=*/false,
14330                                 /*check_dependency=*/true);
14331 }
14332
14333 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14334    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14335    true, the DECL indicates the class being defined in a class-head,
14336    or declared in an elaborated-type-specifier.
14337
14338    Otherwise, return DECL.  */
14339
14340 static tree
14341 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14342 {
14343   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14344      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14345
14346        struct A {
14347          template <typename T> struct B;
14348        };
14349
14350        template <typename T> struct A::B {};
14351
14352      Similarly, in a elaborated-type-specifier:
14353
14354        namespace N { struct X{}; }
14355
14356        struct A {
14357          template <typename T> friend struct N::X;
14358        };
14359
14360      However, if the DECL refers to a class type, and we are in
14361      the scope of the class, then the name lookup automatically
14362      finds the TYPE_DECL created by build_self_reference rather
14363      than a TEMPLATE_DECL.  For example, in:
14364
14365        template <class T> struct S {
14366          S s;
14367        };
14368
14369      there is no need to handle such case.  */
14370
14371   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14372     return DECL_TEMPLATE_RESULT (decl);
14373
14374   return decl;
14375 }
14376
14377 /* If too many, or too few, template-parameter lists apply to the
14378    declarator, issue an error message.  Returns TRUE if all went well,
14379    and FALSE otherwise.  */
14380
14381 static bool
14382 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14383                                                 cp_declarator *declarator)
14384 {
14385   unsigned num_templates;
14386
14387   /* We haven't seen any classes that involve template parameters yet.  */
14388   num_templates = 0;
14389
14390   switch (declarator->kind)
14391     {
14392     case cdk_id:
14393       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14394         {
14395           tree scope;
14396           tree member;
14397
14398           scope = TREE_OPERAND (declarator->u.id.name, 0);
14399           member = TREE_OPERAND (declarator->u.id.name, 1);
14400
14401           while (scope && CLASS_TYPE_P (scope))
14402             {
14403               /* You're supposed to have one `template <...>'
14404                  for every template class, but you don't need one
14405                  for a full specialization.  For example:
14406
14407                  template <class T> struct S{};
14408                  template <> struct S<int> { void f(); };
14409                  void S<int>::f () {}
14410
14411                  is correct; there shouldn't be a `template <>' for
14412                  the definition of `S<int>::f'.  */
14413               if (CLASSTYPE_TEMPLATE_INFO (scope)
14414                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14415                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14416                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14417                 ++num_templates;
14418
14419               scope = TYPE_CONTEXT (scope);
14420             }
14421         }
14422
14423       /* If the DECLARATOR has the form `X<y>' then it uses one
14424          additional level of template parameters.  */
14425       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14426         ++num_templates;
14427
14428       return cp_parser_check_template_parameters (parser,
14429                                                   num_templates);
14430
14431     case cdk_function:
14432     case cdk_array:
14433     case cdk_pointer:
14434     case cdk_reference:
14435     case cdk_ptrmem:
14436       return (cp_parser_check_declarator_template_parameters
14437               (parser, declarator->declarator));
14438
14439     case cdk_error:
14440       return true;
14441
14442     default:
14443       abort ();
14444       return false;
14445     }
14446 }
14447
14448 /* NUM_TEMPLATES were used in the current declaration.  If that is
14449    invalid, return FALSE and issue an error messages.  Otherwise,
14450    return TRUE.  */
14451
14452 static bool
14453 cp_parser_check_template_parameters (cp_parser* parser,
14454                                      unsigned num_templates)
14455 {
14456   /* If there are more template classes than parameter lists, we have
14457      something like:
14458
14459        template <class T> void S<T>::R<T>::f ();  */
14460   if (parser->num_template_parameter_lists < num_templates)
14461     {
14462       error ("too few template-parameter-lists");
14463       return false;
14464     }
14465   /* If there are the same number of template classes and parameter
14466      lists, that's OK.  */
14467   if (parser->num_template_parameter_lists == num_templates)
14468     return true;
14469   /* If there are more, but only one more, then we are referring to a
14470      member template.  That's OK too.  */
14471   if (parser->num_template_parameter_lists == num_templates + 1)
14472       return true;
14473   /* Otherwise, there are too many template parameter lists.  We have
14474      something like:
14475
14476      template <class T> template <class U> void S::f();  */
14477   error ("too many template-parameter-lists");
14478   return false;
14479 }
14480
14481 /* Parse a binary-expression of the general form:
14482
14483    binary-expression:
14484      <expr>
14485      binary-expression <token> <expr>
14486
14487    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14488    to parser the <expr>s.  If the first production is used, then the
14489    value returned by FN is returned directly.  Otherwise, a node with
14490    the indicated EXPR_TYPE is returned, with operands corresponding to
14491    the two sub-expressions.  */
14492
14493 static tree
14494 cp_parser_binary_expression (cp_parser* parser,
14495                              const cp_parser_token_tree_map token_tree_map,
14496                              cp_parser_expression_fn fn)
14497 {
14498   tree lhs;
14499
14500   /* Parse the first expression.  */
14501   lhs = (*fn) (parser);
14502   /* Now, look for more expressions.  */
14503   while (true)
14504     {
14505       cp_token *token;
14506       const cp_parser_token_tree_map_node *map_node;
14507       tree rhs;
14508
14509       /* Peek at the next token.  */
14510       token = cp_lexer_peek_token (parser->lexer);
14511       /* If the token is `>', and that's not an operator at the
14512          moment, then we're done.  */
14513       if (token->type == CPP_GREATER
14514           && !parser->greater_than_is_operator_p)
14515         break;
14516       /* If we find one of the tokens we want, build the corresponding
14517          tree representation.  */
14518       for (map_node = token_tree_map;
14519            map_node->token_type != CPP_EOF;
14520            ++map_node)
14521         if (map_node->token_type == token->type)
14522           {
14523             /* Assume that an overloaded operator will not be used.  */
14524             bool overloaded_p = false;
14525
14526             /* Consume the operator token.  */
14527             cp_lexer_consume_token (parser->lexer);
14528             /* Parse the right-hand side of the expression.  */
14529             rhs = (*fn) (parser);
14530             /* Build the binary tree node.  */
14531             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14532                                      &overloaded_p);
14533             /* If the binary operator required the use of an
14534                overloaded operator, then this expression cannot be an
14535                integral constant-expression.  An overloaded operator
14536                can be used even if both operands are otherwise
14537                permissible in an integral constant-expression if at
14538                least one of the operands is of enumeration type.  */
14539             if (overloaded_p
14540                 && (cp_parser_non_integral_constant_expression
14541                     (parser, "calls to overloaded operators")))
14542               lhs = error_mark_node;
14543             break;
14544           }
14545
14546       /* If the token wasn't one of the ones we want, we're done.  */
14547       if (map_node->token_type == CPP_EOF)
14548         break;
14549     }
14550
14551   return lhs;
14552 }
14553
14554 /* Parse an optional `::' token indicating that the following name is
14555    from the global namespace.  If so, PARSER->SCOPE is set to the
14556    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14557    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14558    Returns the new value of PARSER->SCOPE, if the `::' token is
14559    present, and NULL_TREE otherwise.  */
14560
14561 static tree
14562 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14563 {
14564   cp_token *token;
14565
14566   /* Peek at the next token.  */
14567   token = cp_lexer_peek_token (parser->lexer);
14568   /* If we're looking at a `::' token then we're starting from the
14569      global namespace, not our current location.  */
14570   if (token->type == CPP_SCOPE)
14571     {
14572       /* Consume the `::' token.  */
14573       cp_lexer_consume_token (parser->lexer);
14574       /* Set the SCOPE so that we know where to start the lookup.  */
14575       parser->scope = global_namespace;
14576       parser->qualifying_scope = global_namespace;
14577       parser->object_scope = NULL_TREE;
14578
14579       return parser->scope;
14580     }
14581   else if (!current_scope_valid_p)
14582     {
14583       parser->scope = NULL_TREE;
14584       parser->qualifying_scope = NULL_TREE;
14585       parser->object_scope = NULL_TREE;
14586     }
14587
14588   return NULL_TREE;
14589 }
14590
14591 /* Returns TRUE if the upcoming token sequence is the start of a
14592    constructor declarator.  If FRIEND_P is true, the declarator is
14593    preceded by the `friend' specifier.  */
14594
14595 static bool
14596 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14597 {
14598   bool constructor_p;
14599   tree type_decl = NULL_TREE;
14600   bool nested_name_p;
14601   cp_token *next_token;
14602
14603   /* The common case is that this is not a constructor declarator, so
14604      try to avoid doing lots of work if at all possible.  It's not
14605      valid declare a constructor at function scope.  */
14606   if (at_function_scope_p ())
14607     return false;
14608   /* And only certain tokens can begin a constructor declarator.  */
14609   next_token = cp_lexer_peek_token (parser->lexer);
14610   if (next_token->type != CPP_NAME
14611       && next_token->type != CPP_SCOPE
14612       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14613       && next_token->type != CPP_TEMPLATE_ID)
14614     return false;
14615
14616   /* Parse tentatively; we are going to roll back all of the tokens
14617      consumed here.  */
14618   cp_parser_parse_tentatively (parser);
14619   /* Assume that we are looking at a constructor declarator.  */
14620   constructor_p = true;
14621
14622   /* Look for the optional `::' operator.  */
14623   cp_parser_global_scope_opt (parser,
14624                               /*current_scope_valid_p=*/false);
14625   /* Look for the nested-name-specifier.  */
14626   nested_name_p
14627     = (cp_parser_nested_name_specifier_opt (parser,
14628                                             /*typename_keyword_p=*/false,
14629                                             /*check_dependency_p=*/false,
14630                                             /*type_p=*/false,
14631                                             /*is_declaration=*/false)
14632        != NULL_TREE);
14633   /* Outside of a class-specifier, there must be a
14634      nested-name-specifier.  */
14635   if (!nested_name_p &&
14636       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14637        || friend_p))
14638     constructor_p = false;
14639   /* If we still think that this might be a constructor-declarator,
14640      look for a class-name.  */
14641   if (constructor_p)
14642     {
14643       /* If we have:
14644
14645            template <typename T> struct S { S(); };
14646            template <typename T> S<T>::S ();
14647
14648          we must recognize that the nested `S' names a class.
14649          Similarly, for:
14650
14651            template <typename T> S<T>::S<T> ();
14652
14653          we must recognize that the nested `S' names a template.  */
14654       type_decl = cp_parser_class_name (parser,
14655                                         /*typename_keyword_p=*/false,
14656                                         /*template_keyword_p=*/false,
14657                                         /*type_p=*/false,
14658                                         /*check_dependency_p=*/false,
14659                                         /*class_head_p=*/false,
14660                                         /*is_declaration=*/false);
14661       /* If there was no class-name, then this is not a constructor.  */
14662       constructor_p = !cp_parser_error_occurred (parser);
14663     }
14664
14665   /* If we're still considering a constructor, we have to see a `(',
14666      to begin the parameter-declaration-clause, followed by either a
14667      `)', an `...', or a decl-specifier.  We need to check for a
14668      type-specifier to avoid being fooled into thinking that:
14669
14670        S::S (f) (int);
14671
14672      is a constructor.  (It is actually a function named `f' that
14673      takes one parameter (of type `int') and returns a value of type
14674      `S::S'.  */
14675   if (constructor_p
14676       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14677     {
14678       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14679           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14680           /* A parameter declaration begins with a decl-specifier,
14681              which is either the "attribute" keyword, a storage class
14682              specifier, or (usually) a type-specifier.  */
14683           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14684           && !cp_parser_storage_class_specifier_opt (parser))
14685         {
14686           tree type;
14687           bool pop_p = false;
14688           unsigned saved_num_template_parameter_lists;
14689
14690           /* Names appearing in the type-specifier should be looked up
14691              in the scope of the class.  */
14692           if (current_class_type)
14693             type = NULL_TREE;
14694           else
14695             {
14696               type = TREE_TYPE (type_decl);
14697               if (TREE_CODE (type) == TYPENAME_TYPE)
14698                 {
14699                   type = resolve_typename_type (type,
14700                                                 /*only_current_p=*/false);
14701                   if (type == error_mark_node)
14702                     {
14703                       cp_parser_abort_tentative_parse (parser);
14704                       return false;
14705                     }
14706                 }
14707               pop_p = push_scope (type);
14708             }
14709
14710           /* Inside the constructor parameter list, surrounding
14711              template-parameter-lists do not apply.  */
14712           saved_num_template_parameter_lists
14713             = parser->num_template_parameter_lists;
14714           parser->num_template_parameter_lists = 0;
14715
14716           /* Look for the type-specifier.  */
14717           cp_parser_type_specifier (parser,
14718                                     CP_PARSER_FLAGS_NONE,
14719                                     /*decl_specs=*/NULL,
14720                                     /*is_declarator=*/true,
14721                                     /*declares_class_or_enum=*/NULL,
14722                                     /*is_cv_qualifier=*/NULL);
14723
14724           parser->num_template_parameter_lists
14725             = saved_num_template_parameter_lists;
14726
14727           /* Leave the scope of the class.  */
14728           if (pop_p)
14729             pop_scope (type);
14730
14731           constructor_p = !cp_parser_error_occurred (parser);
14732         }
14733     }
14734   else
14735     constructor_p = false;
14736   /* We did not really want to consume any tokens.  */
14737   cp_parser_abort_tentative_parse (parser);
14738
14739   return constructor_p;
14740 }
14741
14742 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14743    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14744    they must be performed once we are in the scope of the function.
14745
14746    Returns the function defined.  */
14747
14748 static tree
14749 cp_parser_function_definition_from_specifiers_and_declarator
14750   (cp_parser* parser,
14751    cp_decl_specifier_seq *decl_specifiers,
14752    tree attributes,
14753    const cp_declarator *declarator)
14754 {
14755   tree fn;
14756   bool success_p;
14757
14758   /* Begin the function-definition.  */
14759   success_p = start_function (decl_specifiers, declarator, attributes);
14760
14761   /* The things we're about to see are not directly qualified by any
14762      template headers we've seen thus far.  */
14763   reset_specialization ();
14764
14765   /* If there were names looked up in the decl-specifier-seq that we
14766      did not check, check them now.  We must wait until we are in the
14767      scope of the function to perform the checks, since the function
14768      might be a friend.  */
14769   perform_deferred_access_checks ();
14770
14771   if (!success_p)
14772     {
14773       /* Skip the entire function.  */
14774       error ("invalid function declaration");
14775       cp_parser_skip_to_end_of_block_or_statement (parser);
14776       fn = error_mark_node;
14777     }
14778   else
14779     fn = cp_parser_function_definition_after_declarator (parser,
14780                                                          /*inline_p=*/false);
14781
14782   return fn;
14783 }
14784
14785 /* Parse the part of a function-definition that follows the
14786    declarator.  INLINE_P is TRUE iff this function is an inline
14787    function defined with a class-specifier.
14788
14789    Returns the function defined.  */
14790
14791 static tree
14792 cp_parser_function_definition_after_declarator (cp_parser* parser,
14793                                                 bool inline_p)
14794 {
14795   tree fn;
14796   bool ctor_initializer_p = false;
14797   bool saved_in_unbraced_linkage_specification_p;
14798   unsigned saved_num_template_parameter_lists;
14799
14800   /* If the next token is `return', then the code may be trying to
14801      make use of the "named return value" extension that G++ used to
14802      support.  */
14803   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14804     {
14805       /* Consume the `return' keyword.  */
14806       cp_lexer_consume_token (parser->lexer);
14807       /* Look for the identifier that indicates what value is to be
14808          returned.  */
14809       cp_parser_identifier (parser);
14810       /* Issue an error message.  */
14811       error ("named return values are no longer supported");
14812       /* Skip tokens until we reach the start of the function body.  */
14813       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14814              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14815         cp_lexer_consume_token (parser->lexer);
14816     }
14817   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14818      anything declared inside `f'.  */
14819   saved_in_unbraced_linkage_specification_p
14820     = parser->in_unbraced_linkage_specification_p;
14821   parser->in_unbraced_linkage_specification_p = false;
14822   /* Inside the function, surrounding template-parameter-lists do not
14823      apply.  */
14824   saved_num_template_parameter_lists
14825     = parser->num_template_parameter_lists;
14826   parser->num_template_parameter_lists = 0;
14827   /* If the next token is `try', then we are looking at a
14828      function-try-block.  */
14829   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14830     ctor_initializer_p = cp_parser_function_try_block (parser);
14831   /* A function-try-block includes the function-body, so we only do
14832      this next part if we're not processing a function-try-block.  */
14833   else
14834     ctor_initializer_p
14835       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14836
14837   /* Finish the function.  */
14838   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14839                         (inline_p ? 2 : 0));
14840   /* Generate code for it, if necessary.  */
14841   expand_or_defer_fn (fn);
14842   /* Restore the saved values.  */
14843   parser->in_unbraced_linkage_specification_p
14844     = saved_in_unbraced_linkage_specification_p;
14845   parser->num_template_parameter_lists
14846     = saved_num_template_parameter_lists;
14847
14848   return fn;
14849 }
14850
14851 /* Parse a template-declaration, assuming that the `export' (and
14852    `extern') keywords, if present, has already been scanned.  MEMBER_P
14853    is as for cp_parser_template_declaration.  */
14854
14855 static void
14856 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14857 {
14858   tree decl = NULL_TREE;
14859   tree parameter_list;
14860   bool friend_p = false;
14861
14862   /* Look for the `template' keyword.  */
14863   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14864     return;
14865
14866   /* And the `<'.  */
14867   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14868     return;
14869
14870   /* If the next token is `>', then we have an invalid
14871      specialization.  Rather than complain about an invalid template
14872      parameter, issue an error message here.  */
14873   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14874     {
14875       cp_parser_error (parser, "invalid explicit specialization");
14876       begin_specialization ();
14877       parameter_list = NULL_TREE;
14878     }
14879   else
14880     {
14881       /* Parse the template parameters.  */
14882       begin_template_parm_list ();
14883       parameter_list = cp_parser_template_parameter_list (parser);
14884       parameter_list = end_template_parm_list (parameter_list);
14885     }
14886
14887   /* Look for the `>'.  */
14888   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14889   /* We just processed one more parameter list.  */
14890   ++parser->num_template_parameter_lists;
14891   /* If the next token is `template', there are more template
14892      parameters.  */
14893   if (cp_lexer_next_token_is_keyword (parser->lexer,
14894                                       RID_TEMPLATE))
14895     cp_parser_template_declaration_after_export (parser, member_p);
14896   else
14897     {
14898       /* There are no access checks when parsing a template, as we do not
14899          know if a specialization will be a friend.  */
14900       push_deferring_access_checks (dk_no_check);
14901
14902       decl = cp_parser_single_declaration (parser,
14903                                            member_p,
14904                                            &friend_p);
14905
14906       pop_deferring_access_checks ();
14907
14908       /* If this is a member template declaration, let the front
14909          end know.  */
14910       if (member_p && !friend_p && decl)
14911         {
14912           if (TREE_CODE (decl) == TYPE_DECL)
14913             cp_parser_check_access_in_redeclaration (decl);
14914
14915           decl = finish_member_template_decl (decl);
14916         }
14917       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14918         make_friend_class (current_class_type, TREE_TYPE (decl),
14919                            /*complain=*/true);
14920     }
14921   /* We are done with the current parameter list.  */
14922   --parser->num_template_parameter_lists;
14923
14924   /* Finish up.  */
14925   finish_template_decl (parameter_list);
14926
14927   /* Register member declarations.  */
14928   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14929     finish_member_declaration (decl);
14930
14931   /* If DECL is a function template, we must return to parse it later.
14932      (Even though there is no definition, there might be default
14933      arguments that need handling.)  */
14934   if (member_p && decl
14935       && (TREE_CODE (decl) == FUNCTION_DECL
14936           || DECL_FUNCTION_TEMPLATE_P (decl)))
14937     TREE_VALUE (parser->unparsed_functions_queues)
14938       = tree_cons (NULL_TREE, decl,
14939                    TREE_VALUE (parser->unparsed_functions_queues));
14940 }
14941
14942 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14943    `function-definition' sequence.  MEMBER_P is true, this declaration
14944    appears in a class scope.
14945
14946    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14947    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14948
14949 static tree
14950 cp_parser_single_declaration (cp_parser* parser,
14951                               bool member_p,
14952                               bool* friend_p)
14953 {
14954   int declares_class_or_enum;
14955   tree decl = NULL_TREE;
14956   cp_decl_specifier_seq decl_specifiers;
14957   bool function_definition_p = false;
14958
14959   /* Defer access checks until we know what is being declared.  */
14960   push_deferring_access_checks (dk_deferred);
14961
14962   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14963      alternative.  */
14964   cp_parser_decl_specifier_seq (parser,
14965                                 CP_PARSER_FLAGS_OPTIONAL,
14966                                 &decl_specifiers,
14967                                 &declares_class_or_enum);
14968   if (friend_p)
14969     *friend_p = cp_parser_friend_p (&decl_specifiers);
14970   /* Gather up the access checks that occurred the
14971      decl-specifier-seq.  */
14972   stop_deferring_access_checks ();
14973
14974   /* Check for the declaration of a template class.  */
14975   if (declares_class_or_enum)
14976     {
14977       if (cp_parser_declares_only_class_p (parser))
14978         {
14979           decl = shadow_tag (&decl_specifiers);
14980           if (decl && decl != error_mark_node)
14981             decl = TYPE_NAME (decl);
14982           else
14983             decl = error_mark_node;
14984         }
14985     }
14986   else
14987     decl = NULL_TREE;
14988   /* If it's not a template class, try for a template function.  If
14989      the next token is a `;', then this declaration does not declare
14990      anything.  But, if there were errors in the decl-specifiers, then
14991      the error might well have come from an attempted class-specifier.
14992      In that case, there's no need to warn about a missing declarator.  */
14993   if (!decl
14994       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14995           || decl_specifiers.type != error_mark_node))
14996     decl = cp_parser_init_declarator (parser,
14997                                       &decl_specifiers,
14998                                       /*function_definition_allowed_p=*/true,
14999                                       member_p,
15000                                       declares_class_or_enum,
15001                                       &function_definition_p);
15002
15003   pop_deferring_access_checks ();
15004
15005   /* Clear any current qualification; whatever comes next is the start
15006      of something new.  */
15007   parser->scope = NULL_TREE;
15008   parser->qualifying_scope = NULL_TREE;
15009   parser->object_scope = NULL_TREE;
15010   /* Look for a trailing `;' after the declaration.  */
15011   if (!function_definition_p
15012       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
15013     cp_parser_skip_to_end_of_block_or_statement (parser);
15014
15015   return decl;
15016 }
15017
15018 /* Parse a cast-expression that is not the operand of a unary "&".  */
15019
15020 static tree
15021 cp_parser_simple_cast_expression (cp_parser *parser)
15022 {
15023   return cp_parser_cast_expression (parser, /*address_p=*/false);
15024 }
15025
15026 /* Parse a functional cast to TYPE.  Returns an expression
15027    representing the cast.  */
15028
15029 static tree
15030 cp_parser_functional_cast (cp_parser* parser, tree type)
15031 {
15032   tree expression_list;
15033   tree cast;
15034
15035   expression_list
15036     = cp_parser_parenthesized_expression_list (parser, false,
15037                                                /*non_constant_p=*/NULL);
15038
15039   cast = build_functional_cast (type, expression_list);
15040   /* [expr.const]/1: In an integral constant expression "only type
15041      conversions to integral or enumeration type can be used".  */
15042   if (cast != error_mark_node && !type_dependent_expression_p (type)
15043       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15044     {
15045       if (cp_parser_non_integral_constant_expression
15046           (parser, "a call to a constructor"))
15047         return error_mark_node;
15048     }
15049   return cast;
15050 }
15051
15052 /* Save the tokens that make up the body of a member function defined
15053    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15054    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15055    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15056    for the member function.  */
15057
15058 static tree
15059 cp_parser_save_member_function_body (cp_parser* parser,
15060                                      cp_decl_specifier_seq *decl_specifiers,
15061                                      cp_declarator *declarator,
15062                                      tree attributes)
15063 {
15064   cp_token_cache *cache;
15065   tree fn;
15066
15067   /* Create the function-declaration.  */
15068   fn = start_method (decl_specifiers, declarator, attributes);
15069   /* If something went badly wrong, bail out now.  */
15070   if (fn == error_mark_node)
15071     {
15072       /* If there's a function-body, skip it.  */
15073       if (cp_parser_token_starts_function_definition_p
15074           (cp_lexer_peek_token (parser->lexer)))
15075         cp_parser_skip_to_end_of_block_or_statement (parser);
15076       return error_mark_node;
15077     }
15078
15079   /* Remember it, if there default args to post process.  */
15080   cp_parser_save_default_args (parser, fn);
15081
15082   /* Create a token cache.  */
15083   cache = cp_token_cache_new ();
15084   /* Save away the tokens that make up the body of the
15085      function.  */
15086   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15087   /* Handle function try blocks.  */
15088   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15089     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15090
15091   /* Save away the inline definition; we will process it when the
15092      class is complete.  */
15093   DECL_PENDING_INLINE_INFO (fn) = cache;
15094   DECL_PENDING_INLINE_P (fn) = 1;
15095
15096   /* We need to know that this was defined in the class, so that
15097      friend templates are handled correctly.  */
15098   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15099
15100   /* We're done with the inline definition.  */
15101   finish_method (fn);
15102
15103   /* Add FN to the queue of functions to be parsed later.  */
15104   TREE_VALUE (parser->unparsed_functions_queues)
15105     = tree_cons (NULL_TREE, fn,
15106                  TREE_VALUE (parser->unparsed_functions_queues));
15107
15108   return fn;
15109 }
15110
15111 /* Parse a template-argument-list, as well as the trailing ">" (but
15112    not the opening ">").  See cp_parser_template_argument_list for the
15113    return value.  */
15114
15115 static tree
15116 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15117 {
15118   tree arguments;
15119   tree saved_scope;
15120   tree saved_qualifying_scope;
15121   tree saved_object_scope;
15122   bool saved_greater_than_is_operator_p;
15123
15124   /* [temp.names]
15125
15126      When parsing a template-id, the first non-nested `>' is taken as
15127      the end of the template-argument-list rather than a greater-than
15128      operator.  */
15129   saved_greater_than_is_operator_p
15130     = parser->greater_than_is_operator_p;
15131   parser->greater_than_is_operator_p = false;
15132   /* Parsing the argument list may modify SCOPE, so we save it
15133      here.  */
15134   saved_scope = parser->scope;
15135   saved_qualifying_scope = parser->qualifying_scope;
15136   saved_object_scope = parser->object_scope;
15137   /* Parse the template-argument-list itself.  */
15138   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15139     arguments = NULL_TREE;
15140   else
15141     arguments = cp_parser_template_argument_list (parser);
15142   /* Look for the `>' that ends the template-argument-list. If we find
15143      a '>>' instead, it's probably just a typo.  */
15144   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15145     {
15146       if (!saved_greater_than_is_operator_p)
15147         {
15148           /* If we're in a nested template argument list, the '>>' has to be
15149             a typo for '> >'. We emit the error message, but we continue
15150             parsing and we push a '>' as next token, so that the argument
15151             list will be parsed correctly..  */
15152           cp_token* token;
15153           error ("`>>' should be `> >' within a nested template argument list");
15154           token = cp_lexer_peek_token (parser->lexer);
15155           token->type = CPP_GREATER;
15156         }
15157       else
15158         {
15159           /* If this is not a nested template argument list, the '>>' is
15160             a typo for '>'. Emit an error message and continue.  */
15161           error ("spurious `>>', use `>' to terminate a template argument list");
15162           cp_lexer_consume_token (parser->lexer);
15163         }
15164     }
15165   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15166     error ("missing `>' to terminate the template argument list");
15167   /* The `>' token might be a greater-than operator again now.  */
15168   parser->greater_than_is_operator_p
15169     = saved_greater_than_is_operator_p;
15170   /* Restore the SAVED_SCOPE.  */
15171   parser->scope = saved_scope;
15172   parser->qualifying_scope = saved_qualifying_scope;
15173   parser->object_scope = saved_object_scope;
15174
15175   return arguments;
15176 }
15177
15178 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15179    arguments, or the body of the function have not yet been parsed,
15180    parse them now.  */
15181
15182 static void
15183 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15184 {
15185   cp_lexer *saved_lexer;
15186
15187   /* If this member is a template, get the underlying
15188      FUNCTION_DECL.  */
15189   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15190     member_function = DECL_TEMPLATE_RESULT (member_function);
15191
15192   /* There should not be any class definitions in progress at this
15193      point; the bodies of members are only parsed outside of all class
15194      definitions.  */
15195   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
15196   /* While we're parsing the member functions we might encounter more
15197      classes.  We want to handle them right away, but we don't want
15198      them getting mixed up with functions that are currently in the
15199      queue.  */
15200   parser->unparsed_functions_queues
15201     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15202
15203   /* Make sure that any template parameters are in scope.  */
15204   maybe_begin_member_template_processing (member_function);
15205
15206   /* If the body of the function has not yet been parsed, parse it
15207      now.  */
15208   if (DECL_PENDING_INLINE_P (member_function))
15209     {
15210       tree function_scope;
15211       cp_token_cache *tokens;
15212
15213       /* The function is no longer pending; we are processing it.  */
15214       tokens = DECL_PENDING_INLINE_INFO (member_function);
15215       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15216       DECL_PENDING_INLINE_P (member_function) = 0;
15217       /* If this was an inline function in a local class, enter the scope
15218          of the containing function.  */
15219       function_scope = decl_function_context (member_function);
15220       if (function_scope)
15221         push_function_context_to (function_scope);
15222
15223       /* Save away the current lexer.  */
15224       saved_lexer = parser->lexer;
15225       /* Make a new lexer to feed us the tokens saved for this function.  */
15226       parser->lexer = cp_lexer_new_from_tokens (tokens);
15227       parser->lexer->next = saved_lexer;
15228
15229       /* Set the current source position to be the location of the first
15230          token in the saved inline body.  */
15231       cp_lexer_peek_token (parser->lexer);
15232
15233       /* Let the front end know that we going to be defining this
15234          function.  */
15235       start_preparsed_function (member_function, NULL_TREE,
15236                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15237
15238       /* Now, parse the body of the function.  */
15239       cp_parser_function_definition_after_declarator (parser,
15240                                                       /*inline_p=*/true);
15241
15242       /* Leave the scope of the containing function.  */
15243       if (function_scope)
15244         pop_function_context_from (function_scope);
15245       /* Restore the lexer.  */
15246       parser->lexer = saved_lexer;
15247     }
15248
15249   /* Remove any template parameters from the symbol table.  */
15250   maybe_end_member_template_processing ();
15251
15252   /* Restore the queue.  */
15253   parser->unparsed_functions_queues
15254     = TREE_CHAIN (parser->unparsed_functions_queues);
15255 }
15256
15257 /* If DECL contains any default args, remember it on the unparsed
15258    functions queue.  */
15259
15260 static void
15261 cp_parser_save_default_args (cp_parser* parser, tree decl)
15262 {
15263   tree probe;
15264
15265   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15266        probe;
15267        probe = TREE_CHAIN (probe))
15268     if (TREE_PURPOSE (probe))
15269       {
15270         TREE_PURPOSE (parser->unparsed_functions_queues)
15271           = tree_cons (current_class_type, decl,
15272                        TREE_PURPOSE (parser->unparsed_functions_queues));
15273         break;
15274       }
15275   return;
15276 }
15277
15278 /* FN is a FUNCTION_DECL which may contains a parameter with an
15279    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15280    assumes that the current scope is the scope in which the default
15281    argument should be processed.  */
15282
15283 static void
15284 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15285 {
15286   cp_lexer *saved_lexer;
15287   cp_token_cache *tokens;
15288   bool saved_local_variables_forbidden_p;
15289   tree parameters;
15290
15291   /* While we're parsing the default args, we might (due to the
15292      statement expression extension) encounter more classes.  We want
15293      to handle them right away, but we don't want them getting mixed
15294      up with default args that are currently in the queue.  */
15295   parser->unparsed_functions_queues
15296     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15297
15298   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15299        parameters;
15300        parameters = TREE_CHAIN (parameters))
15301     {
15302       if (!TREE_PURPOSE (parameters)
15303           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15304         continue;
15305
15306        /* Save away the current lexer.  */
15307       saved_lexer = parser->lexer;
15308        /* Create a new one, using the tokens we have saved.  */
15309       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
15310       parser->lexer = cp_lexer_new_from_tokens (tokens);
15311
15312        /* Set the current source position to be the location of the
15313           first token in the default argument.  */
15314       cp_lexer_peek_token (parser->lexer);
15315
15316        /* Local variable names (and the `this' keyword) may not appear
15317           in a default argument.  */
15318       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15319       parser->local_variables_forbidden_p = true;
15320        /* Parse the assignment-expression.  */
15321       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15322
15323       /* If the token stream has not been completely used up, then
15324          there was extra junk after the end of the default
15325          argument.  */
15326       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15327         cp_parser_error (parser, "expected `,'");
15328
15329        /* Restore saved state.  */
15330       parser->lexer = saved_lexer;
15331       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15332     }
15333
15334   /* Restore the queue.  */
15335   parser->unparsed_functions_queues
15336     = TREE_CHAIN (parser->unparsed_functions_queues);
15337 }
15338
15339 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15340    either a TYPE or an expression, depending on the form of the
15341    input.  The KEYWORD indicates which kind of expression we have
15342    encountered.  */
15343
15344 static tree
15345 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15346 {
15347   static const char *format;
15348   tree expr = NULL_TREE;
15349   const char *saved_message;
15350   bool saved_integral_constant_expression_p;
15351
15352   /* Initialize FORMAT the first time we get here.  */
15353   if (!format)
15354     format = "types may not be defined in `%s' expressions";
15355
15356   /* Types cannot be defined in a `sizeof' expression.  Save away the
15357      old message.  */
15358   saved_message = parser->type_definition_forbidden_message;
15359   /* And create the new one.  */
15360   parser->type_definition_forbidden_message
15361     = xmalloc (strlen (format)
15362                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15363                + 1 /* `\0' */);
15364   sprintf ((char *) parser->type_definition_forbidden_message,
15365            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15366
15367   /* The restrictions on constant-expressions do not apply inside
15368      sizeof expressions.  */
15369   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15370   parser->integral_constant_expression_p = false;
15371
15372   /* Do not actually evaluate the expression.  */
15373   ++skip_evaluation;
15374   /* If it's a `(', then we might be looking at the type-id
15375      construction.  */
15376   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15377     {
15378       tree type;
15379       bool saved_in_type_id_in_expr_p;
15380
15381       /* We can't be sure yet whether we're looking at a type-id or an
15382          expression.  */
15383       cp_parser_parse_tentatively (parser);
15384       /* Consume the `('.  */
15385       cp_lexer_consume_token (parser->lexer);
15386       /* Parse the type-id.  */
15387       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15388       parser->in_type_id_in_expr_p = true;
15389       type = cp_parser_type_id (parser);
15390       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15391       /* Now, look for the trailing `)'.  */
15392       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15393       /* If all went well, then we're done.  */
15394       if (cp_parser_parse_definitely (parser))
15395         {
15396           cp_decl_specifier_seq decl_specs;
15397
15398           /* Build a trivial decl-specifier-seq.  */
15399           clear_decl_specs (&decl_specs);
15400           decl_specs.type = type;
15401
15402           /* Call grokdeclarator to figure out what type this is.  */
15403           expr = grokdeclarator (NULL,
15404                                  &decl_specs,
15405                                  TYPENAME,
15406                                  /*initialized=*/0,
15407                                  /*attrlist=*/NULL);
15408         }
15409     }
15410
15411   /* If the type-id production did not work out, then we must be
15412      looking at the unary-expression production.  */
15413   if (!expr)
15414     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15415   /* Go back to evaluating expressions.  */
15416   --skip_evaluation;
15417
15418   /* Free the message we created.  */
15419   free ((char *) parser->type_definition_forbidden_message);
15420   /* And restore the old one.  */
15421   parser->type_definition_forbidden_message = saved_message;
15422   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15423
15424   return expr;
15425 }
15426
15427 /* If the current declaration has no declarator, return true.  */
15428
15429 static bool
15430 cp_parser_declares_only_class_p (cp_parser *parser)
15431 {
15432   /* If the next token is a `;' or a `,' then there is no
15433      declarator.  */
15434   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15435           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15436 }
15437
15438 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15439
15440 static void
15441 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15442                              cp_storage_class storage_class)
15443 {
15444   if (decl_specs->storage_class != sc_none)
15445     decl_specs->multiple_storage_classes_p = true;
15446   else
15447     decl_specs->storage_class = storage_class;
15448 }
15449
15450 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15451    is true, the type is a user-defined type; otherwise it is a
15452    built-in type specified by a keyword.  */
15453
15454 static void
15455 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15456                               tree type_spec,
15457                               bool user_defined_p)
15458 {
15459   decl_specs->any_specifiers_p = true;
15460
15461   /* If the user tries to redeclare a built-in type (with, for example,
15462      in "typedef int wchar_t;") we remember that this is what
15463      happened.  In system headers, we ignore these declarations so
15464      that G++ can work with system headers that are not C++-safe.  */
15465   if (decl_specs->specs[(int) ds_typedef]
15466       && !user_defined_p
15467       && (decl_specs->type
15468           || decl_specs->specs[(int) ds_long]
15469           || decl_specs->specs[(int) ds_short]
15470           || decl_specs->specs[(int) ds_unsigned]
15471           || decl_specs->specs[(int) ds_signed]))
15472     {
15473       decl_specs->redefined_builtin_type = type_spec;
15474       if (!decl_specs->type)
15475         {
15476           decl_specs->type = type_spec;
15477           decl_specs->user_defined_type_p = false;
15478         }
15479     }
15480   else if (decl_specs->type)
15481     decl_specs->multiple_types_p = true;
15482   else
15483     {
15484       decl_specs->type = type_spec;
15485       decl_specs->user_defined_type_p = user_defined_p;
15486       decl_specs->redefined_builtin_type = NULL_TREE;
15487     }
15488 }
15489
15490 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15491    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15492
15493 static bool
15494 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15495 {
15496   return decl_specifiers->specs[(int) ds_friend] != 0;
15497 }
15498
15499 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15500    issue an error message indicating that TOKEN_DESC was expected.
15501
15502    Returns the token consumed, if the token had the appropriate type.
15503    Otherwise, returns NULL.  */
15504
15505 static cp_token *
15506 cp_parser_require (cp_parser* parser,
15507                    enum cpp_ttype type,
15508                    const char* token_desc)
15509 {
15510   if (cp_lexer_next_token_is (parser->lexer, type))
15511     return cp_lexer_consume_token (parser->lexer);
15512   else
15513     {
15514       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15515       if (!cp_parser_simulate_error (parser))
15516         {
15517           char *message = concat ("expected ", token_desc, NULL);
15518           cp_parser_error (parser, message);
15519           free (message);
15520         }
15521       return NULL;
15522     }
15523 }
15524
15525 /* Like cp_parser_require, except that tokens will be skipped until
15526    the desired token is found.  An error message is still produced if
15527    the next token is not as expected.  */
15528
15529 static void
15530 cp_parser_skip_until_found (cp_parser* parser,
15531                             enum cpp_ttype type,
15532                             const char* token_desc)
15533 {
15534   cp_token *token;
15535   unsigned nesting_depth = 0;
15536
15537   if (cp_parser_require (parser, type, token_desc))
15538     return;
15539
15540   /* Skip tokens until the desired token is found.  */
15541   while (true)
15542     {
15543       /* Peek at the next token.  */
15544       token = cp_lexer_peek_token (parser->lexer);
15545       /* If we've reached the token we want, consume it and
15546          stop.  */
15547       if (token->type == type && !nesting_depth)
15548         {
15549           cp_lexer_consume_token (parser->lexer);
15550           return;
15551         }
15552       /* If we've run out of tokens, stop.  */
15553       if (token->type == CPP_EOF)
15554         return;
15555       if (token->type == CPP_OPEN_BRACE
15556           || token->type == CPP_OPEN_PAREN
15557           || token->type == CPP_OPEN_SQUARE)
15558         ++nesting_depth;
15559       else if (token->type == CPP_CLOSE_BRACE
15560                || token->type == CPP_CLOSE_PAREN
15561                || token->type == CPP_CLOSE_SQUARE)
15562         {
15563           if (nesting_depth-- == 0)
15564             return;
15565         }
15566       /* Consume this token.  */
15567       cp_lexer_consume_token (parser->lexer);
15568     }
15569 }
15570
15571 /* If the next token is the indicated keyword, consume it.  Otherwise,
15572    issue an error message indicating that TOKEN_DESC was expected.
15573
15574    Returns the token consumed, if the token had the appropriate type.
15575    Otherwise, returns NULL.  */
15576
15577 static cp_token *
15578 cp_parser_require_keyword (cp_parser* parser,
15579                            enum rid keyword,
15580                            const char* token_desc)
15581 {
15582   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15583
15584   if (token && token->keyword != keyword)
15585     {
15586       dyn_string_t error_msg;
15587
15588       /* Format the error message.  */
15589       error_msg = dyn_string_new (0);
15590       dyn_string_append_cstr (error_msg, "expected ");
15591       dyn_string_append_cstr (error_msg, token_desc);
15592       cp_parser_error (parser, error_msg->s);
15593       dyn_string_delete (error_msg);
15594       return NULL;
15595     }
15596
15597   return token;
15598 }
15599
15600 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15601    function-definition.  */
15602
15603 static bool
15604 cp_parser_token_starts_function_definition_p (cp_token* token)
15605 {
15606   return (/* An ordinary function-body begins with an `{'.  */
15607           token->type == CPP_OPEN_BRACE
15608           /* A ctor-initializer begins with a `:'.  */
15609           || token->type == CPP_COLON
15610           /* A function-try-block begins with `try'.  */
15611           || token->keyword == RID_TRY
15612           /* The named return value extension begins with `return'.  */
15613           || token->keyword == RID_RETURN);
15614 }
15615
15616 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15617    definition.  */
15618
15619 static bool
15620 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15621 {
15622   cp_token *token;
15623
15624   token = cp_lexer_peek_token (parser->lexer);
15625   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15626 }
15627
15628 /* Returns TRUE iff the next token is the "," or ">" ending a
15629    template-argument. ">>" is also accepted (after the full
15630    argument was parsed) because it's probably a typo for "> >",
15631    and there is a specific diagnostic for this.  */
15632
15633 static bool
15634 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15635 {
15636   cp_token *token;
15637
15638   token = cp_lexer_peek_token (parser->lexer);
15639   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15640           || token->type == CPP_RSHIFT);
15641 }
15642
15643 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15644    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15645
15646 static bool
15647 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15648                                                      size_t n)
15649 {
15650   cp_token *token;
15651
15652   token = cp_lexer_peek_nth_token (parser->lexer, n);
15653   if (token->type == CPP_LESS)
15654     return true;
15655   /* Check for the sequence `<::' in the original code. It would be lexed as
15656      `[:', where `[' is a digraph, and there is no whitespace before
15657      `:'.  */
15658   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15659     {
15660       cp_token *token2;
15661       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15662       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15663         return true;
15664     }
15665   return false;
15666 }
15667
15668 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15669    or none_type otherwise.  */
15670
15671 static enum tag_types
15672 cp_parser_token_is_class_key (cp_token* token)
15673 {
15674   switch (token->keyword)
15675     {
15676     case RID_CLASS:
15677       return class_type;
15678     case RID_STRUCT:
15679       return record_type;
15680     case RID_UNION:
15681       return union_type;
15682
15683     default:
15684       return none_type;
15685     }
15686 }
15687
15688 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15689
15690 static void
15691 cp_parser_check_class_key (enum tag_types class_key, tree type)
15692 {
15693   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15694     pedwarn ("`%s' tag used in naming `%#T'",
15695             class_key == union_type ? "union"
15696              : class_key == record_type ? "struct" : "class",
15697              type);
15698 }
15699
15700 /* Issue an error message if DECL is redeclared with different
15701    access than its original declaration [class.access.spec/3].
15702    This applies to nested classes and nested class templates.
15703    [class.mem/1].  */
15704
15705 static void cp_parser_check_access_in_redeclaration (tree decl)
15706 {
15707   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15708     return;
15709
15710   if ((TREE_PRIVATE (decl)
15711        != (current_access_specifier == access_private_node))
15712       || (TREE_PROTECTED (decl)
15713           != (current_access_specifier == access_protected_node)))
15714     error ("%D redeclared with different access", decl);
15715 }
15716
15717 /* Look for the `template' keyword, as a syntactic disambiguator.
15718    Return TRUE iff it is present, in which case it will be
15719    consumed.  */
15720
15721 static bool
15722 cp_parser_optional_template_keyword (cp_parser *parser)
15723 {
15724   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15725     {
15726       /* The `template' keyword can only be used within templates;
15727          outside templates the parser can always figure out what is a
15728          template and what is not.  */
15729       if (!processing_template_decl)
15730         {
15731           error ("`template' (as a disambiguator) is only allowed "
15732                  "within templates");
15733           /* If this part of the token stream is rescanned, the same
15734              error message would be generated.  So, we purge the token
15735              from the stream.  */
15736           cp_lexer_purge_token (parser->lexer);
15737           return false;
15738         }
15739       else
15740         {
15741           /* Consume the `template' keyword.  */
15742           cp_lexer_consume_token (parser->lexer);
15743           return true;
15744         }
15745     }
15746
15747   return false;
15748 }
15749
15750 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15751    set PARSER->SCOPE, and perform other related actions.  */
15752
15753 static void
15754 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15755 {
15756   tree value;
15757   tree check;
15758
15759   /* Get the stored value.  */
15760   value = cp_lexer_consume_token (parser->lexer)->value;
15761   /* Perform any access checks that were deferred.  */
15762   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15763     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15764   /* Set the scope from the stored value.  */
15765   parser->scope = TREE_VALUE (value);
15766   parser->qualifying_scope = TREE_TYPE (value);
15767   parser->object_scope = NULL_TREE;
15768 }
15769
15770 /* Add tokens to CACHE until a non-nested END token appears.  */
15771
15772 static void
15773 cp_parser_cache_group_1 (cp_parser *parser,
15774                          cp_token_cache *cache,
15775                          enum cpp_ttype end,
15776                          unsigned depth)
15777 {
15778   while (true)
15779     {
15780       cp_token *token;
15781
15782       /* Abort a parenthesized expression if we encounter a brace.  */
15783       if ((end == CPP_CLOSE_PAREN || depth == 0)
15784           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15785         return;
15786       /* If we've reached the end of the file, stop.  */
15787       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15788         return;
15789       /* Consume the next token.  */
15790       token = cp_lexer_consume_token (parser->lexer);
15791       /* Add this token to the tokens we are saving.  */
15792       cp_token_cache_push_token (cache, token);
15793       /* See if it starts a new group.  */
15794       if (token->type == CPP_OPEN_BRACE)
15795         {
15796           cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15797           if (depth == 0)
15798             return;
15799         }
15800       else if (token->type == CPP_OPEN_PAREN)
15801         cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15802       else if (token->type == end)
15803         return;
15804     }
15805 }
15806
15807 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15808    preserve string tokens in both translated and untranslated
15809    forms.  */
15810
15811 static void
15812 cp_parser_cache_group (cp_parser *parser,
15813                          cp_token_cache *cache,
15814                          enum cpp_ttype end,
15815                          unsigned depth)
15816 {
15817   int saved_c_lex_string_translate;
15818
15819   saved_c_lex_string_translate = c_lex_string_translate;
15820   c_lex_string_translate = -1;
15821
15822   cp_parser_cache_group_1 (parser, cache, end, depth);
15823
15824   c_lex_string_translate = saved_c_lex_string_translate;
15825 }
15826
15827
15828 /* Begin parsing tentatively.  We always save tokens while parsing
15829    tentatively so that if the tentative parsing fails we can restore the
15830    tokens.  */
15831
15832 static void
15833 cp_parser_parse_tentatively (cp_parser* parser)
15834 {
15835   /* Enter a new parsing context.  */
15836   parser->context = cp_parser_context_new (parser->context);
15837   /* Begin saving tokens.  */
15838   cp_lexer_save_tokens (parser->lexer);
15839   /* In order to avoid repetitive access control error messages,
15840      access checks are queued up until we are no longer parsing
15841      tentatively.  */
15842   push_deferring_access_checks (dk_deferred);
15843 }
15844
15845 /* Commit to the currently active tentative parse.  */
15846
15847 static void
15848 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15849 {
15850   cp_parser_context *context;
15851   cp_lexer *lexer;
15852
15853   /* Mark all of the levels as committed.  */
15854   lexer = parser->lexer;
15855   for (context = parser->context; context->next; context = context->next)
15856     {
15857       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15858         break;
15859       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15860       while (!cp_lexer_saving_tokens (lexer))
15861         lexer = lexer->next;
15862       cp_lexer_commit_tokens (lexer);
15863     }
15864 }
15865
15866 /* Abort the currently active tentative parse.  All consumed tokens
15867    will be rolled back, and no diagnostics will be issued.  */
15868
15869 static void
15870 cp_parser_abort_tentative_parse (cp_parser* parser)
15871 {
15872   cp_parser_simulate_error (parser);
15873   /* Now, pretend that we want to see if the construct was
15874      successfully parsed.  */
15875   cp_parser_parse_definitely (parser);
15876 }
15877
15878 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15879    token stream.  Otherwise, commit to the tokens we have consumed.
15880    Returns true if no error occurred; false otherwise.  */
15881
15882 static bool
15883 cp_parser_parse_definitely (cp_parser* parser)
15884 {
15885   bool error_occurred;
15886   cp_parser_context *context;
15887
15888   /* Remember whether or not an error occurred, since we are about to
15889      destroy that information.  */
15890   error_occurred = cp_parser_error_occurred (parser);
15891   /* Remove the topmost context from the stack.  */
15892   context = parser->context;
15893   parser->context = context->next;
15894   /* If no parse errors occurred, commit to the tentative parse.  */
15895   if (!error_occurred)
15896     {
15897       /* Commit to the tokens read tentatively, unless that was
15898          already done.  */
15899       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15900         cp_lexer_commit_tokens (parser->lexer);
15901
15902       pop_to_parent_deferring_access_checks ();
15903     }
15904   /* Otherwise, if errors occurred, roll back our state so that things
15905      are just as they were before we began the tentative parse.  */
15906   else
15907     {
15908       cp_lexer_rollback_tokens (parser->lexer);
15909       pop_deferring_access_checks ();
15910     }
15911   /* Add the context to the front of the free list.  */
15912   context->next = cp_parser_context_free_list;
15913   cp_parser_context_free_list = context;
15914
15915   return !error_occurred;
15916 }
15917
15918 /* Returns true if we are parsing tentatively -- but have decided that
15919    we will stick with this tentative parse, even if errors occur.  */
15920
15921 static bool
15922 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15923 {
15924   return (cp_parser_parsing_tentatively (parser)
15925           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15926 }
15927
15928 /* Returns nonzero iff an error has occurred during the most recent
15929    tentative parse.  */
15930
15931 static bool
15932 cp_parser_error_occurred (cp_parser* parser)
15933 {
15934   return (cp_parser_parsing_tentatively (parser)
15935           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15936 }
15937
15938 /* Returns nonzero if GNU extensions are allowed.  */
15939
15940 static bool
15941 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15942 {
15943   return parser->allow_gnu_extensions_p;
15944 }
15945
15946 \f
15947 /* The parser.  */
15948
15949 static GTY (()) cp_parser *the_parser;
15950
15951 /* External interface.  */
15952
15953 /* Parse one entire translation unit.  */
15954
15955 void
15956 c_parse_file (void)
15957 {
15958   bool error_occurred;
15959   static bool already_called = false;
15960
15961   if (already_called)
15962     {
15963       sorry ("inter-module optimizations not implemented for C++");
15964       return;
15965     }
15966   already_called = true;
15967
15968   the_parser = cp_parser_new ();
15969   push_deferring_access_checks (flag_access_control
15970                                 ? dk_no_deferred : dk_no_check);
15971   error_occurred = cp_parser_translation_unit (the_parser);
15972   the_parser = NULL;
15973 }
15974
15975 /* This variable must be provided by every front end.  */
15976
15977 int yydebug;
15978
15979 #include "gt-cp-parser.h"