OSDN Git Service

* name-lookup.c (pop_binding, pushdecl,
[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   gcc_assert (n > 0);
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, 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 if (parser->object_scope 
2022                && !CLASS_TYPE_P (parser->object_scope))
2023         error ("request for member `%D' in non-class type `%T'",
2024                name, parser->object_scope);
2025       else if (parser->object_scope)
2026         error ("`%T::%D' has not been declared", 
2027                parser->object_scope, name);
2028       else
2029         error ("`%D' has not been declared", name);
2030     }
2031   else if (parser->scope && parser->scope != global_namespace)
2032     error ("`%D::%D' %s", parser->scope, name, desired);
2033   else if (parser->scope == global_namespace)
2034     error ("`::%D' %s", name, desired);
2035   else
2036     error ("`%D' %s", name, desired);
2037 }
2038
2039 /* If we are parsing tentatively, remember that an error has occurred
2040    during this tentative parse.  Returns true if the error was
2041    simulated; false if a message should be issued by the caller.  */
2042
2043 static bool
2044 cp_parser_simulate_error (cp_parser* parser)
2045 {
2046   if (cp_parser_parsing_tentatively (parser)
2047       && !cp_parser_committed_to_tentative_parse (parser))
2048     {
2049       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2050       return true;
2051     }
2052   return false;
2053 }
2054
2055 /* This function is called when a type is defined.  If type
2056    definitions are forbidden at this point, an error message is
2057    issued.  */
2058
2059 static void
2060 cp_parser_check_type_definition (cp_parser* parser)
2061 {
2062   /* If types are forbidden here, issue a message.  */
2063   if (parser->type_definition_forbidden_message)
2064     /* Use `%s' to print the string in case there are any escape
2065        characters in the message.  */
2066     error ("%s", parser->type_definition_forbidden_message);
2067 }
2068
2069 /* This function is called when a declaration is parsed.  If
2070    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2071    indicates that a type was defined in the decl-specifiers for DECL,
2072    then an error is issued.  */
2073
2074 static void
2075 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2076                                                int declares_class_or_enum)
2077 {
2078   /* [dcl.fct] forbids type definitions in return types.
2079      Unfortunately, it's not easy to know whether or not we are
2080      processing a return type until after the fact.  */
2081   while (declarator
2082          && (declarator->kind == cdk_pointer
2083              || declarator->kind == cdk_reference
2084              || declarator->kind == cdk_ptrmem))
2085     declarator = declarator->declarator;
2086   if (declarator
2087       && declarator->kind == cdk_function
2088       && declares_class_or_enum & 2)
2089     error ("new types may not be defined in a return type");
2090 }
2091
2092 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2093    "<" in any valid C++ program.  If the next token is indeed "<",
2094    issue a message warning the user about what appears to be an
2095    invalid attempt to form a template-id.  */
2096
2097 static void
2098 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2099                                          tree type)
2100 {
2101   ptrdiff_t start;
2102   cp_token *token;
2103
2104   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2105     {
2106       if (TYPE_P (type))
2107         error ("`%T' is not a template", type);
2108       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2109         error ("`%E' is not a template", type);
2110       else
2111         error ("invalid template-id");
2112       /* Remember the location of the invalid "<".  */
2113       if (cp_parser_parsing_tentatively (parser)
2114           && !cp_parser_committed_to_tentative_parse (parser))
2115         {
2116           token = cp_lexer_peek_token (parser->lexer);
2117           token = cp_lexer_prev_token (parser->lexer, token);
2118           start = cp_lexer_token_difference (parser->lexer,
2119                                              parser->lexer->first_token,
2120                                              token);
2121         }
2122       else
2123         start = -1;
2124       /* Consume the "<".  */
2125       cp_lexer_consume_token (parser->lexer);
2126       /* Parse the template arguments.  */
2127       cp_parser_enclosed_template_argument_list (parser);
2128       /* Permanently remove the invalid template arguments so that
2129          this error message is not issued again.  */
2130       if (start >= 0)
2131         {
2132           token = cp_lexer_advance_token (parser->lexer,
2133                                           parser->lexer->first_token,
2134                                           start);
2135           cp_lexer_purge_tokens_after (parser->lexer, token);
2136         }
2137     }
2138 }
2139
2140 /* If parsing an integral constant-expression, issue an error message
2141    about the fact that THING appeared and return true.  Otherwise,
2142    return false, marking the current expression as non-constant.  */
2143
2144 static bool
2145 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2146                                             const char *thing)
2147 {
2148   if (parser->integral_constant_expression_p)
2149     {
2150       if (!parser->allow_non_integral_constant_expression_p)
2151         {
2152           error ("%s cannot appear in a constant-expression", thing);
2153           return true;
2154         }
2155       parser->non_integral_constant_expression_p = true;
2156     }
2157   return false;
2158 }
2159
2160 /* Emit a diagnostic for an invalid type name. Consider also if it is
2161    qualified or not and the result of a lookup, to provide a better
2162    message.  */
2163
2164 static void
2165 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2166 {
2167   tree decl, old_scope;
2168   /* Try to lookup the identifier.  */
2169   old_scope = parser->scope;
2170   parser->scope = scope;
2171   decl = cp_parser_lookup_name_simple (parser, id);
2172   parser->scope = old_scope;
2173   /* If the lookup found a template-name, it means that the user forgot
2174   to specify an argument list. Emit an useful error message.  */
2175   if (TREE_CODE (decl) == TEMPLATE_DECL)
2176     error ("invalid use of template-name `%E' without an argument list",
2177       decl);
2178   else if (!parser->scope)
2179     {
2180       /* Issue an error message.  */
2181       error ("`%E' does not name a type", id);
2182       /* If we're in a template class, it's possible that the user was
2183          referring to a type from a base class.  For example:
2184
2185            template <typename T> struct A { typedef T X; };
2186            template <typename T> struct B : public A<T> { X x; };
2187
2188          The user should have said "typename A<T>::X".  */
2189       if (processing_template_decl && current_class_type)
2190         {
2191           tree b;
2192
2193           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2194                b;
2195                b = TREE_CHAIN (b))
2196             {
2197               tree base_type = BINFO_TYPE (b);
2198               if (CLASS_TYPE_P (base_type)
2199                   && dependent_type_p (base_type))
2200                 {
2201                   tree field;
2202                   /* Go from a particular instantiation of the
2203                      template (which will have an empty TYPE_FIELDs),
2204                      to the main version.  */
2205                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2206                   for (field = TYPE_FIELDS (base_type);
2207                        field;
2208                        field = TREE_CHAIN (field))
2209                     if (TREE_CODE (field) == TYPE_DECL
2210                         && DECL_NAME (field) == id)
2211                       {
2212                         inform ("(perhaps `typename %T::%E' was intended)",
2213                                 BINFO_TYPE (b), id);
2214                         break;
2215                       }
2216                   if (field)
2217                     break;
2218                 }
2219             }
2220         }
2221     }
2222   /* Here we diagnose qualified-ids where the scope is actually correct,
2223      but the identifier does not resolve to a valid type name.  */
2224   else
2225     {
2226       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2227         error ("`%E' in namespace `%E' does not name a type",
2228                id, parser->scope);
2229       else if (TYPE_P (parser->scope))
2230         error ("`%E' in class `%T' does not name a type",
2231                id, parser->scope);
2232       else
2233         gcc_unreachable ();
2234     }
2235 }
2236
2237 /* Check for a common situation where a type-name should be present,
2238    but is not, and issue a sensible error message.  Returns true if an
2239    invalid type-name was detected.
2240
2241    The situation handled by this function are variable declarations of the
2242    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2243    Usually, `ID' should name a type, but if we got here it means that it
2244    does not. We try to emit the best possible error message depending on
2245    how exactly the id-expression looks like.
2246 */
2247
2248 static bool
2249 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2250 {
2251   tree id;
2252
2253   cp_parser_parse_tentatively (parser);
2254   id = cp_parser_id_expression (parser,
2255                                 /*template_keyword_p=*/false,
2256                                 /*check_dependency_p=*/true,
2257                                 /*template_p=*/NULL,
2258                                 /*declarator_p=*/true);
2259   /* After the id-expression, there should be a plain identifier,
2260      otherwise this is not a simple variable declaration. Also, if
2261      the scope is dependent, we cannot do much.  */
2262   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2263       || (parser->scope && TYPE_P (parser->scope)
2264           && dependent_type_p (parser->scope)))
2265     {
2266       cp_parser_abort_tentative_parse (parser);
2267       return false;
2268     }
2269   if (!cp_parser_parse_definitely (parser))
2270     return false;
2271
2272   /* If we got here, this cannot be a valid variable declaration, thus
2273      the cp_parser_id_expression must have resolved to a plain identifier
2274      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
2275   gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
2276   /* Emit a diagnostic for the invalid type.  */
2277   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2278   /* Skip to the end of the declaration; there's no point in
2279      trying to process it.  */
2280   cp_parser_skip_to_end_of_block_or_statement (parser);
2281   return true;
2282 }
2283
2284 /* Consume tokens up to, and including, the next non-nested closing `)'.
2285    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2286    are doing error recovery. Returns -1 if OR_COMMA is true and we
2287    found an unnested comma.  */
2288
2289 static int
2290 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2291                                        bool recovering,
2292                                        bool or_comma,
2293                                        bool consume_paren)
2294 {
2295   unsigned paren_depth = 0;
2296   unsigned brace_depth = 0;
2297   int saved_c_lex_string_translate = c_lex_string_translate;
2298   int result;
2299
2300   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2301       && !cp_parser_committed_to_tentative_parse (parser))
2302     return 0;
2303
2304   if (! recovering)
2305     /* If we're looking ahead, keep both translated and untranslated
2306        strings.  */
2307     c_lex_string_translate = -1;
2308
2309   while (true)
2310     {
2311       cp_token *token;
2312
2313       /* If we've run out of tokens, then there is no closing `)'.  */
2314       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2315         {
2316           result = 0;
2317           break;
2318         }
2319
2320       token = cp_lexer_peek_token (parser->lexer);
2321
2322       /* This matches the processing in skip_to_end_of_statement.  */
2323       if (token->type == CPP_SEMICOLON && !brace_depth)
2324         {
2325           result = 0;
2326           break;
2327         }
2328       if (token->type == CPP_OPEN_BRACE)
2329         ++brace_depth;
2330       if (token->type == CPP_CLOSE_BRACE)
2331         {
2332           if (!brace_depth--)
2333             {
2334               result = 0;
2335               break;
2336             }
2337         }
2338       if (recovering && or_comma && token->type == CPP_COMMA
2339           && !brace_depth && !paren_depth)
2340         {
2341           result = -1;
2342           break;
2343         }
2344
2345       if (!brace_depth)
2346         {
2347           /* If it is an `(', we have entered another level of nesting.  */
2348           if (token->type == CPP_OPEN_PAREN)
2349             ++paren_depth;
2350           /* If it is a `)', then we might be done.  */
2351           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2352             {
2353               if (consume_paren)
2354                 cp_lexer_consume_token (parser->lexer);
2355               {
2356                 result = 1;
2357                 break;
2358               }
2359             }
2360         }
2361
2362       /* Consume the token.  */
2363       cp_lexer_consume_token (parser->lexer);
2364     }
2365
2366   c_lex_string_translate = saved_c_lex_string_translate;
2367   return result;
2368 }
2369
2370 /* Consume tokens until we reach the end of the current statement.
2371    Normally, that will be just before consuming a `;'.  However, if a
2372    non-nested `}' comes first, then we stop before consuming that.  */
2373
2374 static void
2375 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2376 {
2377   unsigned nesting_depth = 0;
2378
2379   while (true)
2380     {
2381       cp_token *token;
2382
2383       /* Peek at the next token.  */
2384       token = cp_lexer_peek_token (parser->lexer);
2385       /* If we've run out of tokens, stop.  */
2386       if (token->type == CPP_EOF)
2387         break;
2388       /* If the next token is a `;', we have reached the end of the
2389          statement.  */
2390       if (token->type == CPP_SEMICOLON && !nesting_depth)
2391         break;
2392       /* If the next token is a non-nested `}', then we have reached
2393          the end of the current block.  */
2394       if (token->type == CPP_CLOSE_BRACE)
2395         {
2396           /* If this is a non-nested `}', stop before consuming it.
2397              That way, when confronted with something like:
2398
2399                { 3 + }
2400
2401              we stop before consuming the closing `}', even though we
2402              have not yet reached a `;'.  */
2403           if (nesting_depth == 0)
2404             break;
2405           /* If it is the closing `}' for a block that we have
2406              scanned, stop -- but only after consuming the token.
2407              That way given:
2408
2409                 void f g () { ... }
2410                 typedef int I;
2411
2412              we will stop after the body of the erroneously declared
2413              function, but before consuming the following `typedef'
2414              declaration.  */
2415           if (--nesting_depth == 0)
2416             {
2417               cp_lexer_consume_token (parser->lexer);
2418               break;
2419             }
2420         }
2421       /* If it the next token is a `{', then we are entering a new
2422          block.  Consume the entire block.  */
2423       else if (token->type == CPP_OPEN_BRACE)
2424         ++nesting_depth;
2425       /* Consume the token.  */
2426       cp_lexer_consume_token (parser->lexer);
2427     }
2428 }
2429
2430 /* This function is called at the end of a statement or declaration.
2431    If the next token is a semicolon, it is consumed; otherwise, error
2432    recovery is attempted.  */
2433
2434 static void
2435 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2436 {
2437   /* Look for the trailing `;'.  */
2438   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2439     {
2440       /* If there is additional (erroneous) input, skip to the end of
2441          the statement.  */
2442       cp_parser_skip_to_end_of_statement (parser);
2443       /* If the next token is now a `;', consume it.  */
2444       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2445         cp_lexer_consume_token (parser->lexer);
2446     }
2447 }
2448
2449 /* Skip tokens until we have consumed an entire block, or until we
2450    have consumed a non-nested `;'.  */
2451
2452 static void
2453 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2454 {
2455   unsigned nesting_depth = 0;
2456
2457   while (true)
2458     {
2459       cp_token *token;
2460
2461       /* Peek at the next token.  */
2462       token = cp_lexer_peek_token (parser->lexer);
2463       /* If we've run out of tokens, stop.  */
2464       if (token->type == CPP_EOF)
2465         break;
2466       /* If the next token is a `;', we have reached the end of the
2467          statement.  */
2468       if (token->type == CPP_SEMICOLON && !nesting_depth)
2469         {
2470           /* Consume the `;'.  */
2471           cp_lexer_consume_token (parser->lexer);
2472           break;
2473         }
2474       /* Consume the token.  */
2475       token = cp_lexer_consume_token (parser->lexer);
2476       /* If the next token is a non-nested `}', then we have reached
2477          the end of the current block.  */
2478       if (token->type == CPP_CLOSE_BRACE
2479           && (nesting_depth == 0 || --nesting_depth == 0))
2480         break;
2481       /* If it the next token is a `{', then we are entering a new
2482          block.  Consume the entire block.  */
2483       if (token->type == CPP_OPEN_BRACE)
2484         ++nesting_depth;
2485     }
2486 }
2487
2488 /* Skip tokens until a non-nested closing curly brace is the next
2489    token.  */
2490
2491 static void
2492 cp_parser_skip_to_closing_brace (cp_parser *parser)
2493 {
2494   unsigned nesting_depth = 0;
2495
2496   while (true)
2497     {
2498       cp_token *token;
2499
2500       /* Peek at the next token.  */
2501       token = cp_lexer_peek_token (parser->lexer);
2502       /* If we've run out of tokens, stop.  */
2503       if (token->type == CPP_EOF)
2504         break;
2505       /* If the next token is a non-nested `}', then we have reached
2506          the end of the current block.  */
2507       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2508         break;
2509       /* If it the next token is a `{', then we are entering a new
2510          block.  Consume the entire block.  */
2511       else if (token->type == CPP_OPEN_BRACE)
2512         ++nesting_depth;
2513       /* Consume the token.  */
2514       cp_lexer_consume_token (parser->lexer);
2515     }
2516 }
2517
2518 /* This is a simple wrapper around make_typename_type. When the id is
2519    an unresolved identifier node, we can provide a superior diagnostic
2520    using cp_parser_diagnose_invalid_type_name.  */
2521
2522 static tree
2523 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2524 {
2525   tree result;
2526   if (TREE_CODE (id) == IDENTIFIER_NODE)
2527     {
2528       result = make_typename_type (scope, id, /*complain=*/0);
2529       if (result == error_mark_node)
2530         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2531       return result;
2532     }
2533   return make_typename_type (scope, id, tf_error);
2534 }
2535
2536
2537 /* Create a new C++ parser.  */
2538
2539 static cp_parser *
2540 cp_parser_new (void)
2541 {
2542   cp_parser *parser;
2543   cp_lexer *lexer;
2544
2545   /* cp_lexer_new_main is called before calling ggc_alloc because
2546      cp_lexer_new_main might load a PCH file.  */
2547   lexer = cp_lexer_new_main ();
2548
2549   parser = GGC_CNEW (cp_parser);
2550   parser->lexer = lexer;
2551   parser->context = cp_parser_context_new (NULL);
2552
2553   /* For now, we always accept GNU extensions.  */
2554   parser->allow_gnu_extensions_p = 1;
2555
2556   /* The `>' token is a greater-than operator, not the end of a
2557      template-id.  */
2558   parser->greater_than_is_operator_p = true;
2559
2560   parser->default_arg_ok_p = true;
2561
2562   /* We are not parsing a constant-expression.  */
2563   parser->integral_constant_expression_p = false;
2564   parser->allow_non_integral_constant_expression_p = false;
2565   parser->non_integral_constant_expression_p = false;
2566
2567   /* Local variable names are not forbidden.  */
2568   parser->local_variables_forbidden_p = false;
2569
2570   /* We are not processing an `extern "C"' declaration.  */
2571   parser->in_unbraced_linkage_specification_p = false;
2572
2573   /* We are not processing a declarator.  */
2574   parser->in_declarator_p = false;
2575
2576   /* We are not processing a template-argument-list.  */
2577   parser->in_template_argument_list_p = false;
2578
2579   /* We are not in an iteration statement.  */
2580   parser->in_iteration_statement_p = false;
2581
2582   /* We are not in a switch statement.  */
2583   parser->in_switch_statement_p = false;
2584
2585   /* We are not parsing a type-id inside an expression.  */
2586   parser->in_type_id_in_expr_p = false;
2587
2588   /* The unparsed function queue is empty.  */
2589   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2590
2591   /* There are no classes being defined.  */
2592   parser->num_classes_being_defined = 0;
2593
2594   /* No template parameters apply.  */
2595   parser->num_template_parameter_lists = 0;
2596
2597   return parser;
2598 }
2599
2600 /* Lexical conventions [gram.lex]  */
2601
2602 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2603    identifier.  */
2604
2605 static tree
2606 cp_parser_identifier (cp_parser* parser)
2607 {
2608   cp_token *token;
2609
2610   /* Look for the identifier.  */
2611   token = cp_parser_require (parser, CPP_NAME, "identifier");
2612   /* Return the value.  */
2613   return token ? token->value : error_mark_node;
2614 }
2615
2616 /* Basic concepts [gram.basic]  */
2617
2618 /* Parse a translation-unit.
2619
2620    translation-unit:
2621      declaration-seq [opt]
2622
2623    Returns TRUE if all went well.  */
2624
2625 static bool
2626 cp_parser_translation_unit (cp_parser* parser)
2627 {
2628   /* The address of the first non-permanent object on the declarator
2629      obstack.  */
2630   static void *declarator_obstack_base;
2631
2632   bool success;
2633
2634   /* Create the declarator obstack, if necessary.  */
2635   if (!cp_error_declarator)
2636     {
2637       gcc_obstack_init (&declarator_obstack);
2638       /* Create the error declarator.  */
2639       cp_error_declarator = make_declarator (cdk_error);
2640       /* Create the empty parameter list.  */
2641       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2642       /* Remember where the base of the declarator obstack lies.  */
2643       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2644     }
2645
2646   while (true)
2647     {
2648       cp_parser_declaration_seq_opt (parser);
2649
2650       /* If there are no tokens left then all went well.  */
2651       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2652         {
2653           /* Consume the EOF token.  */
2654           cp_parser_require (parser, CPP_EOF, "end-of-file");
2655
2656           /* Finish up.  */
2657           finish_translation_unit ();
2658
2659           success = true;
2660           break;
2661         }
2662       else
2663         {
2664           cp_parser_error (parser, "expected declaration");
2665           success = false;
2666           break;
2667         }
2668     }
2669
2670   /* Make sure the declarator obstack was fully cleaned up.  */
2671   gcc_assert (obstack_next_free (&declarator_obstack)
2672               == declarator_obstack_base);
2673
2674   /* All went well.  */
2675   return success;
2676 }
2677
2678 /* Expressions [gram.expr] */
2679
2680 /* Parse a primary-expression.
2681
2682    primary-expression:
2683      literal
2684      this
2685      ( expression )
2686      id-expression
2687
2688    GNU Extensions:
2689
2690    primary-expression:
2691      ( compound-statement )
2692      __builtin_va_arg ( assignment-expression , type-id )
2693
2694    literal:
2695      __null
2696
2697    Returns a representation of the expression.
2698
2699    *IDK indicates what kind of id-expression (if any) was present.
2700
2701    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2702    used as the operand of a pointer-to-member.  In that case,
2703    *QUALIFYING_CLASS gives the class that is used as the qualifying
2704    class in the pointer-to-member.  */
2705
2706 static tree
2707 cp_parser_primary_expression (cp_parser *parser,
2708                               cp_id_kind *idk,
2709                               tree *qualifying_class)
2710 {
2711   cp_token *token;
2712
2713   /* Assume the primary expression is not an id-expression.  */
2714   *idk = CP_ID_KIND_NONE;
2715   /* And that it cannot be used as pointer-to-member.  */
2716   *qualifying_class = NULL_TREE;
2717
2718   /* Peek at the next token.  */
2719   token = cp_lexer_peek_token (parser->lexer);
2720   switch (token->type)
2721     {
2722       /* literal:
2723            integer-literal
2724            character-literal
2725            floating-literal
2726            string-literal
2727            boolean-literal  */
2728     case CPP_CHAR:
2729     case CPP_WCHAR:
2730     case CPP_NUMBER:
2731       token = cp_lexer_consume_token (parser->lexer);
2732       return token->value;
2733
2734     case CPP_STRING:
2735     case CPP_WSTRING:
2736       token = cp_lexer_consume_token (parser->lexer);
2737       if (TREE_CHAIN (token->value))
2738         return TREE_CHAIN (token->value);
2739       else
2740         return token->value;
2741
2742     case CPP_OPEN_PAREN:
2743       {
2744         tree expr;
2745         bool saved_greater_than_is_operator_p;
2746
2747         /* Consume the `('.  */
2748         cp_lexer_consume_token (parser->lexer);
2749         /* Within a parenthesized expression, a `>' token is always
2750            the greater-than operator.  */
2751         saved_greater_than_is_operator_p
2752           = parser->greater_than_is_operator_p;
2753         parser->greater_than_is_operator_p = true;
2754         /* If we see `( { ' then we are looking at the beginning of
2755            a GNU statement-expression.  */
2756         if (cp_parser_allow_gnu_extensions_p (parser)
2757             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2758           {
2759             /* Statement-expressions are not allowed by the standard.  */
2760             if (pedantic)
2761               pedwarn ("ISO C++ forbids braced-groups within expressions");
2762
2763             /* And they're not allowed outside of a function-body; you
2764                cannot, for example, write:
2765
2766                  int i = ({ int j = 3; j + 1; });
2767
2768                at class or namespace scope.  */
2769             if (!at_function_scope_p ())
2770               error ("statement-expressions are allowed only inside functions");
2771             /* Start the statement-expression.  */
2772             expr = begin_stmt_expr ();
2773             /* Parse the compound-statement.  */
2774             cp_parser_compound_statement (parser, expr, false);
2775             /* Finish up.  */
2776             expr = finish_stmt_expr (expr, false);
2777           }
2778         else
2779           {
2780             /* Parse the parenthesized expression.  */
2781             expr = cp_parser_expression (parser);
2782             /* Let the front end know that this expression was
2783                enclosed in parentheses. This matters in case, for
2784                example, the expression is of the form `A::B', since
2785                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2786                not.  */
2787             finish_parenthesized_expr (expr);
2788           }
2789         /* The `>' token might be the end of a template-id or
2790            template-parameter-list now.  */
2791         parser->greater_than_is_operator_p
2792           = saved_greater_than_is_operator_p;
2793         /* Consume the `)'.  */
2794         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2795           cp_parser_skip_to_end_of_statement (parser);
2796
2797         return expr;
2798       }
2799
2800     case CPP_KEYWORD:
2801       switch (token->keyword)
2802         {
2803           /* These two are the boolean literals.  */
2804         case RID_TRUE:
2805           cp_lexer_consume_token (parser->lexer);
2806           return boolean_true_node;
2807         case RID_FALSE:
2808           cp_lexer_consume_token (parser->lexer);
2809           return boolean_false_node;
2810
2811           /* The `__null' literal.  */
2812         case RID_NULL:
2813           cp_lexer_consume_token (parser->lexer);
2814           return null_node;
2815
2816           /* Recognize the `this' keyword.  */
2817         case RID_THIS:
2818           cp_lexer_consume_token (parser->lexer);
2819           if (parser->local_variables_forbidden_p)
2820             {
2821               error ("`this' may not be used in this context");
2822               return error_mark_node;
2823             }
2824           /* Pointers cannot appear in constant-expressions.  */
2825           if (cp_parser_non_integral_constant_expression (parser,
2826                                                           "`this'"))
2827             return error_mark_node;
2828           return finish_this_expr ();
2829
2830           /* The `operator' keyword can be the beginning of an
2831              id-expression.  */
2832         case RID_OPERATOR:
2833           goto id_expression;
2834
2835         case RID_FUNCTION_NAME:
2836         case RID_PRETTY_FUNCTION_NAME:
2837         case RID_C99_FUNCTION_NAME:
2838           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2839              __func__ are the names of variables -- but they are
2840              treated specially.  Therefore, they are handled here,
2841              rather than relying on the generic id-expression logic
2842              below.  Grammatically, these names are id-expressions.
2843
2844              Consume the token.  */
2845           token = cp_lexer_consume_token (parser->lexer);
2846           /* Look up the name.  */
2847           return finish_fname (token->value);
2848
2849         case RID_VA_ARG:
2850           {
2851             tree expression;
2852             tree type;
2853
2854             /* The `__builtin_va_arg' construct is used to handle
2855                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2856             cp_lexer_consume_token (parser->lexer);
2857             /* Look for the opening `('.  */
2858             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2859             /* Now, parse the assignment-expression.  */
2860             expression = cp_parser_assignment_expression (parser);
2861             /* Look for the `,'.  */
2862             cp_parser_require (parser, CPP_COMMA, "`,'");
2863             /* Parse the type-id.  */
2864             type = cp_parser_type_id (parser);
2865             /* Look for the closing `)'.  */
2866             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2867             /* Using `va_arg' in a constant-expression is not
2868                allowed.  */
2869             if (cp_parser_non_integral_constant_expression (parser,
2870                                                             "`va_arg'"))
2871               return error_mark_node;
2872             return build_x_va_arg (expression, type);
2873           }
2874
2875         case RID_OFFSETOF:
2876           return cp_parser_builtin_offsetof (parser);
2877
2878         default:
2879           cp_parser_error (parser, "expected primary-expression");
2880           return error_mark_node;
2881         }
2882
2883       /* An id-expression can start with either an identifier, a
2884          `::' as the beginning of a qualified-id, or the "operator"
2885          keyword.  */
2886     case CPP_NAME:
2887     case CPP_SCOPE:
2888     case CPP_TEMPLATE_ID:
2889     case CPP_NESTED_NAME_SPECIFIER:
2890       {
2891         tree id_expression;
2892         tree decl;
2893         const char *error_msg;
2894
2895       id_expression:
2896         /* Parse the id-expression.  */
2897         id_expression
2898           = cp_parser_id_expression (parser,
2899                                      /*template_keyword_p=*/false,
2900                                      /*check_dependency_p=*/true,
2901                                      /*template_p=*/NULL,
2902                                      /*declarator_p=*/false);
2903         if (id_expression == error_mark_node)
2904           return error_mark_node;
2905         /* If we have a template-id, then no further lookup is
2906            required.  If the template-id was for a template-class, we
2907            will sometimes have a TYPE_DECL at this point.  */
2908         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2909             || TREE_CODE (id_expression) == TYPE_DECL)
2910           decl = id_expression;
2911         /* Look up the name.  */
2912         else
2913           {
2914             bool ambiguous_p;
2915
2916             decl = cp_parser_lookup_name (parser, id_expression,
2917                                           /*is_type=*/false,
2918                                           /*is_template=*/false,
2919                                           /*is_namespace=*/false,
2920                                           /*check_dependency=*/true,
2921                                           &ambiguous_p);
2922             /* If the lookup was ambiguous, an error will already have
2923                been issued.  */
2924             if (ambiguous_p)
2925               return error_mark_node;
2926             /* If name lookup gives us a SCOPE_REF, then the
2927                qualifying scope was dependent.  Just propagate the
2928                name.  */
2929             if (TREE_CODE (decl) == SCOPE_REF)
2930               {
2931                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2932                   *qualifying_class = TREE_OPERAND (decl, 0);
2933                 return decl;
2934               }
2935             /* Check to see if DECL is a local variable in a context
2936                where that is forbidden.  */
2937             if (parser->local_variables_forbidden_p
2938                 && local_variable_p (decl))
2939               {
2940                 /* It might be that we only found DECL because we are
2941                    trying to be generous with pre-ISO scoping rules.
2942                    For example, consider:
2943
2944                      int i;
2945                      void g() {
2946                        for (int i = 0; i < 10; ++i) {}
2947                        extern void f(int j = i);
2948                      }
2949
2950                    Here, name look up will originally find the out
2951                    of scope `i'.  We need to issue a warning message,
2952                    but then use the global `i'.  */
2953                 decl = check_for_out_of_scope_variable (decl);
2954                 if (local_variable_p (decl))
2955                   {
2956                     error ("local variable `%D' may not appear in this context",
2957                            decl);
2958                     return error_mark_node;
2959                   }
2960               }
2961           }
2962
2963         decl = finish_id_expression (id_expression, decl, parser->scope,
2964                                      idk, qualifying_class,
2965                                      parser->integral_constant_expression_p,
2966                                      parser->allow_non_integral_constant_expression_p,
2967                                      &parser->non_integral_constant_expression_p,
2968                                      &error_msg);
2969         if (error_msg)
2970           cp_parser_error (parser, error_msg);
2971         return decl;
2972       }
2973
2974       /* Anything else is an error.  */
2975     default:
2976       cp_parser_error (parser, "expected primary-expression");
2977       return error_mark_node;
2978     }
2979 }
2980
2981 /* Parse an id-expression.
2982
2983    id-expression:
2984      unqualified-id
2985      qualified-id
2986
2987    qualified-id:
2988      :: [opt] nested-name-specifier template [opt] unqualified-id
2989      :: identifier
2990      :: operator-function-id
2991      :: template-id
2992
2993    Return a representation of the unqualified portion of the
2994    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2995    a `::' or nested-name-specifier.
2996
2997    Often, if the id-expression was a qualified-id, the caller will
2998    want to make a SCOPE_REF to represent the qualified-id.  This
2999    function does not do this in order to avoid wastefully creating
3000    SCOPE_REFs when they are not required.
3001
3002    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3003    `template' keyword.
3004
3005    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3006    uninstantiated templates.
3007
3008    If *TEMPLATE_P is non-NULL, it is set to true iff the
3009    `template' keyword is used to explicitly indicate that the entity
3010    named is a template.
3011
3012    If DECLARATOR_P is true, the id-expression is appearing as part of
3013    a declarator, rather than as part of an expression.  */
3014
3015 static tree
3016 cp_parser_id_expression (cp_parser *parser,
3017                          bool template_keyword_p,
3018                          bool check_dependency_p,
3019                          bool *template_p,
3020                          bool declarator_p)
3021 {
3022   bool global_scope_p;
3023   bool nested_name_specifier_p;
3024
3025   /* Assume the `template' keyword was not used.  */
3026   if (template_p)
3027     *template_p = false;
3028
3029   /* Look for the optional `::' operator.  */
3030   global_scope_p
3031     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3032        != NULL_TREE);
3033   /* Look for the optional nested-name-specifier.  */
3034   nested_name_specifier_p
3035     = (cp_parser_nested_name_specifier_opt (parser,
3036                                             /*typename_keyword_p=*/false,
3037                                             check_dependency_p,
3038                                             /*type_p=*/false,
3039                                             /*is_declarator=*/false)
3040        != NULL_TREE);
3041   /* If there is a nested-name-specifier, then we are looking at
3042      the first qualified-id production.  */
3043   if (nested_name_specifier_p)
3044     {
3045       tree saved_scope;
3046       tree saved_object_scope;
3047       tree saved_qualifying_scope;
3048       tree unqualified_id;
3049       bool is_template;
3050
3051       /* See if the next token is the `template' keyword.  */
3052       if (!template_p)
3053         template_p = &is_template;
3054       *template_p = cp_parser_optional_template_keyword (parser);
3055       /* Name lookup we do during the processing of the
3056          unqualified-id might obliterate SCOPE.  */
3057       saved_scope = parser->scope;
3058       saved_object_scope = parser->object_scope;
3059       saved_qualifying_scope = parser->qualifying_scope;
3060       /* Process the final unqualified-id.  */
3061       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3062                                                  check_dependency_p,
3063                                                  declarator_p);
3064       /* Restore the SAVED_SCOPE for our caller.  */
3065       parser->scope = saved_scope;
3066       parser->object_scope = saved_object_scope;
3067       parser->qualifying_scope = saved_qualifying_scope;
3068
3069       return unqualified_id;
3070     }
3071   /* Otherwise, if we are in global scope, then we are looking at one
3072      of the other qualified-id productions.  */
3073   else if (global_scope_p)
3074     {
3075       cp_token *token;
3076       tree id;
3077
3078       /* Peek at the next token.  */
3079       token = cp_lexer_peek_token (parser->lexer);
3080
3081       /* If it's an identifier, and the next token is not a "<", then
3082          we can avoid the template-id case.  This is an optimization
3083          for this common case.  */
3084       if (token->type == CPP_NAME
3085           && !cp_parser_nth_token_starts_template_argument_list_p
3086                (parser, 2))
3087         return cp_parser_identifier (parser);
3088
3089       cp_parser_parse_tentatively (parser);
3090       /* Try a template-id.  */
3091       id = cp_parser_template_id (parser,
3092                                   /*template_keyword_p=*/false,
3093                                   /*check_dependency_p=*/true,
3094                                   declarator_p);
3095       /* If that worked, we're done.  */
3096       if (cp_parser_parse_definitely (parser))
3097         return id;
3098
3099       /* Peek at the next token.  (Changes in the token buffer may
3100          have invalidated the pointer obtained above.)  */
3101       token = cp_lexer_peek_token (parser->lexer);
3102
3103       switch (token->type)
3104         {
3105         case CPP_NAME:
3106           return cp_parser_identifier (parser);
3107
3108         case CPP_KEYWORD:
3109           if (token->keyword == RID_OPERATOR)
3110             return cp_parser_operator_function_id (parser);
3111           /* Fall through.  */
3112
3113         default:
3114           cp_parser_error (parser, "expected id-expression");
3115           return error_mark_node;
3116         }
3117     }
3118   else
3119     return cp_parser_unqualified_id (parser, template_keyword_p,
3120                                      /*check_dependency_p=*/true,
3121                                      declarator_p);
3122 }
3123
3124 /* Parse an unqualified-id.
3125
3126    unqualified-id:
3127      identifier
3128      operator-function-id
3129      conversion-function-id
3130      ~ class-name
3131      template-id
3132
3133    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3134    keyword, in a construct like `A::template ...'.
3135
3136    Returns a representation of unqualified-id.  For the `identifier'
3137    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3138    production a BIT_NOT_EXPR is returned; the operand of the
3139    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3140    other productions, see the documentation accompanying the
3141    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3142    names are looked up in uninstantiated templates.  If DECLARATOR_P
3143    is true, the unqualified-id is appearing as part of a declarator,
3144    rather than as part of an expression.  */
3145
3146 static tree
3147 cp_parser_unqualified_id (cp_parser* parser,
3148                           bool template_keyword_p,
3149                           bool check_dependency_p,
3150                           bool declarator_p)
3151 {
3152   cp_token *token;
3153
3154   /* Peek at the next token.  */
3155   token = cp_lexer_peek_token (parser->lexer);
3156
3157   switch (token->type)
3158     {
3159     case CPP_NAME:
3160       {
3161         tree id;
3162
3163         /* We don't know yet whether or not this will be a
3164            template-id.  */
3165         cp_parser_parse_tentatively (parser);
3166         /* Try a template-id.  */
3167         id = cp_parser_template_id (parser, template_keyword_p,
3168                                     check_dependency_p,
3169                                     declarator_p);
3170         /* If it worked, we're done.  */
3171         if (cp_parser_parse_definitely (parser))
3172           return id;
3173         /* Otherwise, it's an ordinary identifier.  */
3174         return cp_parser_identifier (parser);
3175       }
3176
3177     case CPP_TEMPLATE_ID:
3178       return cp_parser_template_id (parser, template_keyword_p,
3179                                     check_dependency_p,
3180                                     declarator_p);
3181
3182     case CPP_COMPL:
3183       {
3184         tree type_decl;
3185         tree qualifying_scope;
3186         tree object_scope;
3187         tree scope;
3188
3189         /* Consume the `~' token.  */
3190         cp_lexer_consume_token (parser->lexer);
3191         /* Parse the class-name.  The standard, as written, seems to
3192            say that:
3193
3194              template <typename T> struct S { ~S (); };
3195              template <typename T> S<T>::~S() {}
3196
3197            is invalid, since `~' must be followed by a class-name, but
3198            `S<T>' is dependent, and so not known to be a class.
3199            That's not right; we need to look in uninstantiated
3200            templates.  A further complication arises from:
3201
3202              template <typename T> void f(T t) {
3203                t.T::~T();
3204              }
3205
3206            Here, it is not possible to look up `T' in the scope of `T'
3207            itself.  We must look in both the current scope, and the
3208            scope of the containing complete expression.
3209
3210            Yet another issue is:
3211
3212              struct S {
3213                int S;
3214                ~S();
3215              };
3216
3217              S::~S() {}
3218
3219            The standard does not seem to say that the `S' in `~S'
3220            should refer to the type `S' and not the data member
3221            `S::S'.  */
3222
3223         /* DR 244 says that we look up the name after the "~" in the
3224            same scope as we looked up the qualifying name.  That idea
3225            isn't fully worked out; it's more complicated than that.  */
3226         scope = parser->scope;
3227         object_scope = parser->object_scope;
3228         qualifying_scope = parser->qualifying_scope;
3229
3230         /* If the name is of the form "X::~X" it's OK.  */
3231         if (scope && TYPE_P (scope)
3232             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3233             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3234                 == CPP_OPEN_PAREN)
3235             && (cp_lexer_peek_token (parser->lexer)->value
3236                 == TYPE_IDENTIFIER (scope)))
3237           {
3238             cp_lexer_consume_token (parser->lexer);
3239             return build_nt (BIT_NOT_EXPR, scope);
3240           }
3241
3242         /* If there was an explicit qualification (S::~T), first look
3243            in the scope given by the qualification (i.e., S).  */
3244         if (scope)
3245           {
3246             cp_parser_parse_tentatively (parser);
3247             type_decl = cp_parser_class_name (parser,
3248                                               /*typename_keyword_p=*/false,
3249                                               /*template_keyword_p=*/false,
3250                                               /*type_p=*/false,
3251                                               /*check_dependency=*/false,
3252                                               /*class_head_p=*/false,
3253                                               declarator_p);
3254             if (cp_parser_parse_definitely (parser))
3255               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3256           }
3257         /* In "N::S::~S", look in "N" as well.  */
3258         if (scope && qualifying_scope)
3259           {
3260             cp_parser_parse_tentatively (parser);
3261             parser->scope = qualifying_scope;
3262             parser->object_scope = NULL_TREE;
3263             parser->qualifying_scope = NULL_TREE;
3264             type_decl
3265               = cp_parser_class_name (parser,
3266                                       /*typename_keyword_p=*/false,
3267                                       /*template_keyword_p=*/false,
3268                                       /*type_p=*/false,
3269                                       /*check_dependency=*/false,
3270                                       /*class_head_p=*/false,
3271                                       declarator_p);
3272             if (cp_parser_parse_definitely (parser))
3273               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3274           }
3275         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3276         else if (object_scope)
3277           {
3278             cp_parser_parse_tentatively (parser);
3279             parser->scope = object_scope;
3280             parser->object_scope = NULL_TREE;
3281             parser->qualifying_scope = NULL_TREE;
3282             type_decl
3283               = cp_parser_class_name (parser,
3284                                       /*typename_keyword_p=*/false,
3285                                       /*template_keyword_p=*/false,
3286                                       /*type_p=*/false,
3287                                       /*check_dependency=*/false,
3288                                       /*class_head_p=*/false,
3289                                       declarator_p);
3290             if (cp_parser_parse_definitely (parser))
3291               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3292           }
3293         /* Look in the surrounding context.  */
3294         parser->scope = NULL_TREE;
3295         parser->object_scope = NULL_TREE;
3296         parser->qualifying_scope = NULL_TREE;
3297         type_decl
3298           = cp_parser_class_name (parser,
3299                                   /*typename_keyword_p=*/false,
3300                                   /*template_keyword_p=*/false,
3301                                   /*type_p=*/false,
3302                                   /*check_dependency=*/false,
3303                                   /*class_head_p=*/false,
3304                                   declarator_p);
3305         /* If an error occurred, assume that the name of the
3306            destructor is the same as the name of the qualifying
3307            class.  That allows us to keep parsing after running
3308            into ill-formed destructor names.  */
3309         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3310           return build_nt (BIT_NOT_EXPR, scope);
3311         else if (type_decl == error_mark_node)
3312           return error_mark_node;
3313
3314         /* [class.dtor]
3315
3316            A typedef-name that names a class shall not be used as the
3317            identifier in the declarator for a destructor declaration.  */
3318         if (declarator_p
3319             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3320             && !DECL_SELF_REFERENCE_P (type_decl))
3321           error ("typedef-name `%D' used as destructor declarator",
3322                  type_decl);
3323
3324         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3325       }
3326
3327     case CPP_KEYWORD:
3328       if (token->keyword == RID_OPERATOR)
3329         {
3330           tree id;
3331
3332           /* This could be a template-id, so we try that first.  */
3333           cp_parser_parse_tentatively (parser);
3334           /* Try a template-id.  */
3335           id = cp_parser_template_id (parser, template_keyword_p,
3336                                       /*check_dependency_p=*/true,
3337                                       declarator_p);
3338           /* If that worked, we're done.  */
3339           if (cp_parser_parse_definitely (parser))
3340             return id;
3341           /* We still don't know whether we're looking at an
3342              operator-function-id or a conversion-function-id.  */
3343           cp_parser_parse_tentatively (parser);
3344           /* Try an operator-function-id.  */
3345           id = cp_parser_operator_function_id (parser);
3346           /* If that didn't work, try a conversion-function-id.  */
3347           if (!cp_parser_parse_definitely (parser))
3348             id = cp_parser_conversion_function_id (parser);
3349
3350           return id;
3351         }
3352       /* Fall through.  */
3353
3354     default:
3355       cp_parser_error (parser, "expected unqualified-id");
3356       return error_mark_node;
3357     }
3358 }
3359
3360 /* Parse an (optional) nested-name-specifier.
3361
3362    nested-name-specifier:
3363      class-or-namespace-name :: nested-name-specifier [opt]
3364      class-or-namespace-name :: template nested-name-specifier [opt]
3365
3366    PARSER->SCOPE should be set appropriately before this function is
3367    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3368    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3369    in name lookups.
3370
3371    Sets PARSER->SCOPE to the class (TYPE) or namespace
3372    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3373    it unchanged if there is no nested-name-specifier.  Returns the new
3374    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3375
3376    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3377    part of a declaration and/or decl-specifier.  */
3378
3379 static tree
3380 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3381                                      bool typename_keyword_p,
3382                                      bool check_dependency_p,
3383                                      bool type_p,
3384                                      bool is_declaration)
3385 {
3386   bool success = false;
3387   tree access_check = NULL_TREE;
3388   ptrdiff_t start;
3389   cp_token* token;
3390
3391   /* If the next token corresponds to a nested name specifier, there
3392      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3393      false, it may have been true before, in which case something
3394      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3395      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3396      CHECK_DEPENDENCY_P is false, we have to fall through into the
3397      main loop.  */
3398   if (check_dependency_p
3399       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3400     {
3401       cp_parser_pre_parsed_nested_name_specifier (parser);
3402       return parser->scope;
3403     }
3404
3405   /* Remember where the nested-name-specifier starts.  */
3406   if (cp_parser_parsing_tentatively (parser)
3407       && !cp_parser_committed_to_tentative_parse (parser))
3408     {
3409       token = cp_lexer_peek_token (parser->lexer);
3410       start = cp_lexer_token_difference (parser->lexer,
3411                                          parser->lexer->first_token,
3412                                          token);
3413     }
3414   else
3415     start = -1;
3416
3417   push_deferring_access_checks (dk_deferred);
3418
3419   while (true)
3420     {
3421       tree new_scope;
3422       tree old_scope;
3423       tree saved_qualifying_scope;
3424       bool template_keyword_p;
3425
3426       /* Spot cases that cannot be the beginning of a
3427          nested-name-specifier.  */
3428       token = cp_lexer_peek_token (parser->lexer);
3429
3430       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3431          the already parsed nested-name-specifier.  */
3432       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3433         {
3434           /* Grab the nested-name-specifier and continue the loop.  */
3435           cp_parser_pre_parsed_nested_name_specifier (parser);
3436           success = true;
3437           continue;
3438         }
3439
3440       /* Spot cases that cannot be the beginning of a
3441          nested-name-specifier.  On the second and subsequent times
3442          through the loop, we look for the `template' keyword.  */
3443       if (success && token->keyword == RID_TEMPLATE)
3444         ;
3445       /* A template-id can start a nested-name-specifier.  */
3446       else if (token->type == CPP_TEMPLATE_ID)
3447         ;
3448       else
3449         {
3450           /* If the next token is not an identifier, then it is
3451              definitely not a class-or-namespace-name.  */
3452           if (token->type != CPP_NAME)
3453             break;
3454           /* If the following token is neither a `<' (to begin a
3455              template-id), nor a `::', then we are not looking at a
3456              nested-name-specifier.  */
3457           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3458           if (token->type != CPP_SCOPE
3459               && !cp_parser_nth_token_starts_template_argument_list_p
3460                   (parser, 2))
3461             break;
3462         }
3463
3464       /* The nested-name-specifier is optional, so we parse
3465          tentatively.  */
3466       cp_parser_parse_tentatively (parser);
3467
3468       /* Look for the optional `template' keyword, if this isn't the
3469          first time through the loop.  */
3470       if (success)
3471         template_keyword_p = cp_parser_optional_template_keyword (parser);
3472       else
3473         template_keyword_p = false;
3474
3475       /* Save the old scope since the name lookup we are about to do
3476          might destroy it.  */
3477       old_scope = parser->scope;
3478       saved_qualifying_scope = parser->qualifying_scope;
3479       /* Parse the qualifying entity.  */
3480       new_scope
3481         = cp_parser_class_or_namespace_name (parser,
3482                                              typename_keyword_p,
3483                                              template_keyword_p,
3484                                              check_dependency_p,
3485                                              type_p,
3486                                              is_declaration);
3487       /* Look for the `::' token.  */
3488       cp_parser_require (parser, CPP_SCOPE, "`::'");
3489
3490       /* If we found what we wanted, we keep going; otherwise, we're
3491          done.  */
3492       if (!cp_parser_parse_definitely (parser))
3493         {
3494           bool error_p = false;
3495
3496           /* Restore the OLD_SCOPE since it was valid before the
3497              failed attempt at finding the last
3498              class-or-namespace-name.  */
3499           parser->scope = old_scope;
3500           parser->qualifying_scope = saved_qualifying_scope;
3501           /* If the next token is an identifier, and the one after
3502              that is a `::', then any valid interpretation would have
3503              found a class-or-namespace-name.  */
3504           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3505                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3506                      == CPP_SCOPE)
3507                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3508                      != CPP_COMPL))
3509             {
3510               token = cp_lexer_consume_token (parser->lexer);
3511               if (!error_p)
3512                 {
3513                   tree decl;
3514
3515                   decl = cp_parser_lookup_name_simple (parser, token->value);
3516                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3517                     error ("`%D' used without template parameters",
3518                            decl);
3519                   else
3520                     cp_parser_name_lookup_error
3521                       (parser, token->value, decl,
3522                        "is not a class or namespace");
3523                   parser->scope = NULL_TREE;
3524                   error_p = true;
3525                   /* Treat this as a successful nested-name-specifier
3526                      due to:
3527
3528                      [basic.lookup.qual]
3529
3530                      If the name found is not a class-name (clause
3531                      _class_) or namespace-name (_namespace.def_), the
3532                      program is ill-formed.  */
3533                   success = true;
3534                 }
3535               cp_lexer_consume_token (parser->lexer);
3536             }
3537           break;
3538         }
3539
3540       /* We've found one valid nested-name-specifier.  */
3541       success = true;
3542       /* Make sure we look in the right scope the next time through
3543          the loop.  */
3544       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3545                        ? TREE_TYPE (new_scope)
3546                        : new_scope);
3547       /* If it is a class scope, try to complete it; we are about to
3548          be looking up names inside the class.  */
3549       if (TYPE_P (parser->scope)
3550           /* Since checking types for dependency can be expensive,
3551              avoid doing it if the type is already complete.  */
3552           && !COMPLETE_TYPE_P (parser->scope)
3553           /* Do not try to complete dependent types.  */
3554           && !dependent_type_p (parser->scope))
3555         complete_type (parser->scope);
3556     }
3557
3558   /* Retrieve any deferred checks.  Do not pop this access checks yet
3559      so the memory will not be reclaimed during token replacing below.  */
3560   access_check = get_deferred_access_checks ();
3561
3562   /* If parsing tentatively, replace the sequence of tokens that makes
3563      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3564      token.  That way, should we re-parse the token stream, we will
3565      not have to repeat the effort required to do the parse, nor will
3566      we issue duplicate error messages.  */
3567   if (success && start >= 0)
3568     {
3569       /* Find the token that corresponds to the start of the
3570          template-id.  */
3571       token = cp_lexer_advance_token (parser->lexer,
3572                                       parser->lexer->first_token,
3573                                       start);
3574
3575       /* Reset the contents of the START token.  */
3576       token->type = CPP_NESTED_NAME_SPECIFIER;
3577       token->value = build_tree_list (access_check, parser->scope);
3578       TREE_TYPE (token->value) = parser->qualifying_scope;
3579       token->keyword = RID_MAX;
3580       /* Purge all subsequent tokens.  */
3581       cp_lexer_purge_tokens_after (parser->lexer, token);
3582     }
3583
3584   pop_deferring_access_checks ();
3585   return success ? parser->scope : NULL_TREE;
3586 }
3587
3588 /* Parse a nested-name-specifier.  See
3589    cp_parser_nested_name_specifier_opt for details.  This function
3590    behaves identically, except that it will an issue an error if no
3591    nested-name-specifier is present, and it will return
3592    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3593    is present.  */
3594
3595 static tree
3596 cp_parser_nested_name_specifier (cp_parser *parser,
3597                                  bool typename_keyword_p,
3598                                  bool check_dependency_p,
3599                                  bool type_p,
3600                                  bool is_declaration)
3601 {
3602   tree scope;
3603
3604   /* Look for the nested-name-specifier.  */
3605   scope = cp_parser_nested_name_specifier_opt (parser,
3606                                                typename_keyword_p,
3607                                                check_dependency_p,
3608                                                type_p,
3609                                                is_declaration);
3610   /* If it was not present, issue an error message.  */
3611   if (!scope)
3612     {
3613       cp_parser_error (parser, "expected nested-name-specifier");
3614       parser->scope = NULL_TREE;
3615       return error_mark_node;
3616     }
3617
3618   return scope;
3619 }
3620
3621 /* Parse a class-or-namespace-name.
3622
3623    class-or-namespace-name:
3624      class-name
3625      namespace-name
3626
3627    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3628    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3629    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3630    TYPE_P is TRUE iff the next name should be taken as a class-name,
3631    even the same name is declared to be another entity in the same
3632    scope.
3633
3634    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3635    specified by the class-or-namespace-name.  If neither is found the
3636    ERROR_MARK_NODE is returned.  */
3637
3638 static tree
3639 cp_parser_class_or_namespace_name (cp_parser *parser,
3640                                    bool typename_keyword_p,
3641                                    bool template_keyword_p,
3642                                    bool check_dependency_p,
3643                                    bool type_p,
3644                                    bool is_declaration)
3645 {
3646   tree saved_scope;
3647   tree saved_qualifying_scope;
3648   tree saved_object_scope;
3649   tree scope;
3650   bool only_class_p;
3651
3652   /* Before we try to parse the class-name, we must save away the
3653      current PARSER->SCOPE since cp_parser_class_name will destroy
3654      it.  */
3655   saved_scope = parser->scope;
3656   saved_qualifying_scope = parser->qualifying_scope;
3657   saved_object_scope = parser->object_scope;
3658   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3659      there is no need to look for a namespace-name.  */
3660   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3661   if (!only_class_p)
3662     cp_parser_parse_tentatively (parser);
3663   scope = cp_parser_class_name (parser,
3664                                 typename_keyword_p,
3665                                 template_keyword_p,
3666                                 type_p,
3667                                 check_dependency_p,
3668                                 /*class_head_p=*/false,
3669                                 is_declaration);
3670   /* If that didn't work, try for a namespace-name.  */
3671   if (!only_class_p && !cp_parser_parse_definitely (parser))
3672     {
3673       /* Restore the saved scope.  */
3674       parser->scope = saved_scope;
3675       parser->qualifying_scope = saved_qualifying_scope;
3676       parser->object_scope = saved_object_scope;
3677       /* If we are not looking at an identifier followed by the scope
3678          resolution operator, then this is not part of a
3679          nested-name-specifier.  (Note that this function is only used
3680          to parse the components of a nested-name-specifier.)  */
3681       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3682           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3683         return error_mark_node;
3684       scope = cp_parser_namespace_name (parser);
3685     }
3686
3687   return scope;
3688 }
3689
3690 /* Parse a postfix-expression.
3691
3692    postfix-expression:
3693      primary-expression
3694      postfix-expression [ expression ]
3695      postfix-expression ( expression-list [opt] )
3696      simple-type-specifier ( expression-list [opt] )
3697      typename :: [opt] nested-name-specifier identifier
3698        ( expression-list [opt] )
3699      typename :: [opt] nested-name-specifier template [opt] template-id
3700        ( expression-list [opt] )
3701      postfix-expression . template [opt] id-expression
3702      postfix-expression -> template [opt] id-expression
3703      postfix-expression . pseudo-destructor-name
3704      postfix-expression -> pseudo-destructor-name
3705      postfix-expression ++
3706      postfix-expression --
3707      dynamic_cast < type-id > ( expression )
3708      static_cast < type-id > ( expression )
3709      reinterpret_cast < type-id > ( expression )
3710      const_cast < type-id > ( expression )
3711      typeid ( expression )
3712      typeid ( type-id )
3713
3714    GNU Extension:
3715
3716    postfix-expression:
3717      ( type-id ) { initializer-list , [opt] }
3718
3719    This extension is a GNU version of the C99 compound-literal
3720    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3721    but they are essentially the same concept.)
3722
3723    If ADDRESS_P is true, the postfix expression is the operand of the
3724    `&' operator.
3725
3726    Returns a representation of the expression.  */
3727
3728 static tree
3729 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3730 {
3731   cp_token *token;
3732   enum rid keyword;
3733   cp_id_kind idk = CP_ID_KIND_NONE;
3734   tree postfix_expression = NULL_TREE;
3735   /* Non-NULL only if the current postfix-expression can be used to
3736      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3737      class used to qualify the member.  */
3738   tree qualifying_class = NULL_TREE;
3739
3740   /* Peek at the next token.  */
3741   token = cp_lexer_peek_token (parser->lexer);
3742   /* Some of the productions are determined by keywords.  */
3743   keyword = token->keyword;
3744   switch (keyword)
3745     {
3746     case RID_DYNCAST:
3747     case RID_STATCAST:
3748     case RID_REINTCAST:
3749     case RID_CONSTCAST:
3750       {
3751         tree type;
3752         tree expression;
3753         const char *saved_message;
3754
3755         /* All of these can be handled in the same way from the point
3756            of view of parsing.  Begin by consuming the token
3757            identifying the cast.  */
3758         cp_lexer_consume_token (parser->lexer);
3759
3760         /* New types cannot be defined in the cast.  */
3761         saved_message = parser->type_definition_forbidden_message;
3762         parser->type_definition_forbidden_message
3763           = "types may not be defined in casts";
3764
3765         /* Look for the opening `<'.  */
3766         cp_parser_require (parser, CPP_LESS, "`<'");
3767         /* Parse the type to which we are casting.  */
3768         type = cp_parser_type_id (parser);
3769         /* Look for the closing `>'.  */
3770         cp_parser_require (parser, CPP_GREATER, "`>'");
3771         /* Restore the old message.  */
3772         parser->type_definition_forbidden_message = saved_message;
3773
3774         /* And the expression which is being cast.  */
3775         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3776         expression = cp_parser_expression (parser);
3777         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3778
3779         /* Only type conversions to integral or enumeration types
3780            can be used in constant-expressions.  */
3781         if (parser->integral_constant_expression_p
3782             && !dependent_type_p (type)
3783             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3784             && (cp_parser_non_integral_constant_expression
3785                 (parser,
3786                  "a cast to a type other than an integral or "
3787                  "enumeration type")))
3788           return error_mark_node;
3789
3790         switch (keyword)
3791           {
3792           case RID_DYNCAST:
3793             postfix_expression
3794               = build_dynamic_cast (type, expression);
3795             break;
3796           case RID_STATCAST:
3797             postfix_expression
3798               = build_static_cast (type, expression);
3799             break;
3800           case RID_REINTCAST:
3801             postfix_expression
3802               = build_reinterpret_cast (type, expression);
3803             break;
3804           case RID_CONSTCAST:
3805             postfix_expression
3806               = build_const_cast (type, expression);
3807             break;
3808           default:
3809             gcc_unreachable ();
3810           }
3811       }
3812       break;
3813
3814     case RID_TYPEID:
3815       {
3816         tree type;
3817         const char *saved_message;
3818         bool saved_in_type_id_in_expr_p;
3819
3820         /* Consume the `typeid' token.  */
3821         cp_lexer_consume_token (parser->lexer);
3822         /* Look for the `(' token.  */
3823         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3824         /* Types cannot be defined in a `typeid' expression.  */
3825         saved_message = parser->type_definition_forbidden_message;
3826         parser->type_definition_forbidden_message
3827           = "types may not be defined in a `typeid\' expression";
3828         /* We can't be sure yet whether we're looking at a type-id or an
3829            expression.  */
3830         cp_parser_parse_tentatively (parser);
3831         /* Try a type-id first.  */
3832         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3833         parser->in_type_id_in_expr_p = true;
3834         type = cp_parser_type_id (parser);
3835         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3836         /* Look for the `)' token.  Otherwise, we can't be sure that
3837            we're not looking at an expression: consider `typeid (int
3838            (3))', for example.  */
3839         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3840         /* If all went well, simply lookup the type-id.  */
3841         if (cp_parser_parse_definitely (parser))
3842           postfix_expression = get_typeid (type);
3843         /* Otherwise, fall back to the expression variant.  */
3844         else
3845           {
3846             tree expression;
3847
3848             /* Look for an expression.  */
3849             expression = cp_parser_expression (parser);
3850             /* Compute its typeid.  */
3851             postfix_expression = build_typeid (expression);
3852             /* Look for the `)' token.  */
3853             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3854           }
3855         /* `typeid' may not appear in an integral constant expression.  */
3856         if (cp_parser_non_integral_constant_expression(parser,
3857                                                        "`typeid' operator"))
3858           return error_mark_node;
3859         /* Restore the saved message.  */
3860         parser->type_definition_forbidden_message = saved_message;
3861       }
3862       break;
3863
3864     case RID_TYPENAME:
3865       {
3866         bool template_p = false;
3867         tree id;
3868         tree type;
3869
3870         /* Consume the `typename' token.  */
3871         cp_lexer_consume_token (parser->lexer);
3872         /* Look for the optional `::' operator.  */
3873         cp_parser_global_scope_opt (parser,
3874                                     /*current_scope_valid_p=*/false);
3875         /* Look for the nested-name-specifier.  */
3876         cp_parser_nested_name_specifier (parser,
3877                                          /*typename_keyword_p=*/true,
3878                                          /*check_dependency_p=*/true,
3879                                          /*type_p=*/true,
3880                                          /*is_declaration=*/true);
3881         /* Look for the optional `template' keyword.  */
3882         template_p = cp_parser_optional_template_keyword (parser);
3883         /* We don't know whether we're looking at a template-id or an
3884            identifier.  */
3885         cp_parser_parse_tentatively (parser);
3886         /* Try a template-id.  */
3887         id = cp_parser_template_id (parser, template_p,
3888                                     /*check_dependency_p=*/true,
3889                                     /*is_declaration=*/true);
3890         /* If that didn't work, try an identifier.  */
3891         if (!cp_parser_parse_definitely (parser))
3892           id = cp_parser_identifier (parser);
3893         /* If we look up a template-id in a non-dependent qualifying
3894            scope, there's no need to create a dependent type.  */
3895         if (TREE_CODE (id) == TYPE_DECL
3896             && !dependent_type_p (parser->scope))
3897           type = TREE_TYPE (id);
3898         /* Create a TYPENAME_TYPE to represent the type to which the
3899            functional cast is being performed.  */
3900         else
3901           type = make_typename_type (parser->scope, id,
3902                                      /*complain=*/1);
3903
3904         postfix_expression = cp_parser_functional_cast (parser, type);
3905       }
3906       break;
3907
3908     default:
3909       {
3910         tree type;
3911
3912         /* If the next thing is a simple-type-specifier, we may be
3913            looking at a functional cast.  We could also be looking at
3914            an id-expression.  So, we try the functional cast, and if
3915            that doesn't work we fall back to the primary-expression.  */
3916         cp_parser_parse_tentatively (parser);
3917         /* Look for the simple-type-specifier.  */
3918         type = cp_parser_simple_type_specifier (parser,
3919                                                 /*decl_specs=*/NULL,
3920                                                 CP_PARSER_FLAGS_NONE);
3921         /* Parse the cast itself.  */
3922         if (!cp_parser_error_occurred (parser))
3923           postfix_expression
3924             = cp_parser_functional_cast (parser, type);
3925         /* If that worked, we're done.  */
3926         if (cp_parser_parse_definitely (parser))
3927           break;
3928
3929         /* If the functional-cast didn't work out, try a
3930            compound-literal.  */
3931         if (cp_parser_allow_gnu_extensions_p (parser)
3932             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3933           {
3934             tree initializer_list = NULL_TREE;
3935             bool saved_in_type_id_in_expr_p;
3936
3937             cp_parser_parse_tentatively (parser);
3938             /* Consume the `('.  */
3939             cp_lexer_consume_token (parser->lexer);
3940             /* Parse the type.  */
3941             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3942             parser->in_type_id_in_expr_p = true;
3943             type = cp_parser_type_id (parser);
3944             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3945             /* Look for the `)'.  */
3946             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3947             /* Look for the `{'.  */
3948             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3949             /* If things aren't going well, there's no need to
3950                keep going.  */
3951             if (!cp_parser_error_occurred (parser))
3952               {
3953                 bool non_constant_p;
3954                 /* Parse the initializer-list.  */
3955                 initializer_list
3956                   = cp_parser_initializer_list (parser, &non_constant_p);
3957                 /* Allow a trailing `,'.  */
3958                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3959                   cp_lexer_consume_token (parser->lexer);
3960                 /* Look for the final `}'.  */
3961                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3962               }
3963             /* If that worked, we're definitely looking at a
3964                compound-literal expression.  */
3965             if (cp_parser_parse_definitely (parser))
3966               {
3967                 /* Warn the user that a compound literal is not
3968                    allowed in standard C++.  */
3969                 if (pedantic)
3970                   pedwarn ("ISO C++ forbids compound-literals");
3971                 /* Form the representation of the compound-literal.  */
3972                 postfix_expression
3973                   = finish_compound_literal (type, initializer_list);
3974                 break;
3975               }
3976           }
3977
3978         /* It must be a primary-expression.  */
3979         postfix_expression = cp_parser_primary_expression (parser,
3980                                                            &idk,
3981                                                            &qualifying_class);
3982       }
3983       break;
3984     }
3985
3986   /* If we were avoiding committing to the processing of a
3987      qualified-id until we knew whether or not we had a
3988      pointer-to-member, we now know.  */
3989   if (qualifying_class)
3990     {
3991       bool done;
3992
3993       /* Peek at the next token.  */
3994       token = cp_lexer_peek_token (parser->lexer);
3995       done = (token->type != CPP_OPEN_SQUARE
3996               && token->type != CPP_OPEN_PAREN
3997               && token->type != CPP_DOT
3998               && token->type != CPP_DEREF
3999               && token->type != CPP_PLUS_PLUS
4000               && token->type != CPP_MINUS_MINUS);
4001
4002       postfix_expression = finish_qualified_id_expr (qualifying_class,
4003                                                      postfix_expression,
4004                                                      done,
4005                                                      address_p);
4006       if (done)
4007         return postfix_expression;
4008     }
4009
4010   /* Keep looping until the postfix-expression is complete.  */
4011   while (true)
4012     {
4013       if (idk == CP_ID_KIND_UNQUALIFIED
4014           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4015           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4016         /* It is not a Koenig lookup function call.  */
4017         postfix_expression
4018           = unqualified_name_lookup_error (postfix_expression);
4019
4020       /* Peek at the next token.  */
4021       token = cp_lexer_peek_token (parser->lexer);
4022
4023       switch (token->type)
4024         {
4025         case CPP_OPEN_SQUARE:
4026           postfix_expression
4027             = cp_parser_postfix_open_square_expression (parser,
4028                                                         postfix_expression,
4029                                                         false);
4030           idk = CP_ID_KIND_NONE;
4031           break;
4032
4033         case CPP_OPEN_PAREN:
4034           /* postfix-expression ( expression-list [opt] ) */
4035           {
4036             bool koenig_p;
4037             tree args = (cp_parser_parenthesized_expression_list
4038                          (parser, false, /*non_constant_p=*/NULL));
4039
4040             if (args == error_mark_node)
4041               {
4042                 postfix_expression = error_mark_node;
4043                 break;
4044               }
4045
4046             /* Function calls are not permitted in
4047                constant-expressions.  */
4048             if (cp_parser_non_integral_constant_expression (parser,
4049                                                             "a function call"))
4050               {
4051                 postfix_expression = error_mark_node;
4052                 break;
4053               }
4054
4055             koenig_p = false;
4056             if (idk == CP_ID_KIND_UNQUALIFIED)
4057               {
4058                 /* We do not perform argument-dependent lookup if
4059                    normal lookup finds a non-function, in accordance
4060                    with the expected resolution of DR 218.  */
4061                 if (args
4062                     && (is_overloaded_fn (postfix_expression)
4063                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4064                   {
4065                     koenig_p = true;
4066                     postfix_expression
4067                       = perform_koenig_lookup (postfix_expression, args);
4068                   }
4069                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4070                   postfix_expression
4071                     = unqualified_fn_lookup_error (postfix_expression);
4072               }
4073
4074             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4075               {
4076                 tree instance = TREE_OPERAND (postfix_expression, 0);
4077                 tree fn = TREE_OPERAND (postfix_expression, 1);
4078
4079                 if (processing_template_decl
4080                     && (type_dependent_expression_p (instance)
4081                         || (!BASELINK_P (fn)
4082                             && TREE_CODE (fn) != FIELD_DECL)
4083                         || type_dependent_expression_p (fn)
4084                         || any_type_dependent_arguments_p (args)))
4085                   {
4086                     postfix_expression
4087                       = build_min_nt (CALL_EXPR, postfix_expression,
4088                                       args, NULL_TREE);
4089                     break;
4090                   }
4091
4092                 if (BASELINK_P (fn))
4093                   postfix_expression
4094                     = (build_new_method_call
4095                        (instance, fn, args, NULL_TREE,
4096                         (idk == CP_ID_KIND_QUALIFIED
4097                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4098                 else
4099                   postfix_expression
4100                     = finish_call_expr (postfix_expression, args,
4101                                         /*disallow_virtual=*/false,
4102                                         /*koenig_p=*/false);
4103               }
4104             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4105                      || TREE_CODE (postfix_expression) == MEMBER_REF
4106                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4107               postfix_expression = (build_offset_ref_call_from_tree
4108                                     (postfix_expression, args));
4109             else if (idk == CP_ID_KIND_QUALIFIED)
4110               /* A call to a static class member, or a namespace-scope
4111                  function.  */
4112               postfix_expression
4113                 = finish_call_expr (postfix_expression, args,
4114                                     /*disallow_virtual=*/true,
4115                                     koenig_p);
4116             else
4117               /* All other function calls.  */
4118               postfix_expression
4119                 = finish_call_expr (postfix_expression, args,
4120                                     /*disallow_virtual=*/false,
4121                                     koenig_p);
4122
4123             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4124             idk = CP_ID_KIND_NONE;
4125           }
4126           break;
4127
4128         case CPP_DOT:
4129         case CPP_DEREF:
4130           /* postfix-expression . template [opt] id-expression
4131              postfix-expression . pseudo-destructor-name
4132              postfix-expression -> template [opt] id-expression
4133              postfix-expression -> pseudo-destructor-name */
4134
4135           /* Consume the `.' or `->' operator.  */
4136           cp_lexer_consume_token (parser->lexer);
4137
4138           postfix_expression
4139             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4140                                                       postfix_expression,
4141                                                       false, &idk);
4142           break;
4143
4144         case CPP_PLUS_PLUS:
4145           /* postfix-expression ++  */
4146           /* Consume the `++' token.  */
4147           cp_lexer_consume_token (parser->lexer);
4148           /* Generate a representation for the complete expression.  */
4149           postfix_expression
4150             = finish_increment_expr (postfix_expression,
4151                                      POSTINCREMENT_EXPR);
4152           /* Increments may not appear in constant-expressions.  */
4153           if (cp_parser_non_integral_constant_expression (parser,
4154                                                           "an increment"))
4155             postfix_expression = error_mark_node;
4156           idk = CP_ID_KIND_NONE;
4157           break;
4158
4159         case CPP_MINUS_MINUS:
4160           /* postfix-expression -- */
4161           /* Consume the `--' token.  */
4162           cp_lexer_consume_token (parser->lexer);
4163           /* Generate a representation for the complete expression.  */
4164           postfix_expression
4165             = finish_increment_expr (postfix_expression,
4166                                      POSTDECREMENT_EXPR);
4167           /* Decrements may not appear in constant-expressions.  */
4168           if (cp_parser_non_integral_constant_expression (parser,
4169                                                           "a decrement"))
4170             postfix_expression = error_mark_node;
4171           idk = CP_ID_KIND_NONE;
4172           break;
4173
4174         default:
4175           return postfix_expression;
4176         }
4177     }
4178
4179   /* We should never get here.  */
4180   gcc_unreachable ();
4181   return error_mark_node;
4182 }
4183
4184 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4185    by cp_parser_builtin_offsetof.  We're looking for
4186
4187      postfix-expression [ expression ]
4188
4189    FOR_OFFSETOF is set if we're being called in that context, which
4190    changes how we deal with integer constant expressions.  */
4191
4192 static tree
4193 cp_parser_postfix_open_square_expression (cp_parser *parser,
4194                                           tree postfix_expression,
4195                                           bool for_offsetof)
4196 {
4197   tree index;
4198
4199   /* Consume the `[' token.  */
4200   cp_lexer_consume_token (parser->lexer);
4201
4202   /* Parse the index expression.  */
4203   /* ??? For offsetof, there is a question of what to allow here.  If
4204      offsetof is not being used in an integral constant expression context,
4205      then we *could* get the right answer by computing the value at runtime.
4206      If we are in an integral constant expression context, then we might
4207      could accept any constant expression; hard to say without analysis.
4208      Rather than open the barn door too wide right away, allow only integer
4209      constant expresions here.  */
4210   if (for_offsetof)
4211     index = cp_parser_constant_expression (parser, false, NULL);
4212   else
4213     index = cp_parser_expression (parser);
4214
4215   /* Look for the closing `]'.  */
4216   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4217
4218   /* Build the ARRAY_REF.  */
4219   postfix_expression = grok_array_decl (postfix_expression, index);
4220
4221   /* When not doing offsetof, array references are not permitted in
4222      constant-expressions.  */
4223   if (!for_offsetof
4224       && (cp_parser_non_integral_constant_expression
4225           (parser, "an array reference")))
4226     postfix_expression = error_mark_node;
4227
4228   return postfix_expression;
4229 }
4230
4231 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4232    by cp_parser_builtin_offsetof.  We're looking for
4233
4234      postfix-expression . template [opt] id-expression
4235      postfix-expression . pseudo-destructor-name
4236      postfix-expression -> template [opt] id-expression
4237      postfix-expression -> pseudo-destructor-name
4238
4239    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4240    limits what of the above we'll actually accept, but nevermind.
4241    TOKEN_TYPE is the "." or "->" token, which will already have been
4242    removed from the stream.  */
4243
4244 static tree
4245 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4246                                         enum cpp_ttype token_type,
4247                                         tree postfix_expression,
4248                                         bool for_offsetof, cp_id_kind *idk)
4249 {
4250   tree name;
4251   bool dependent_p;
4252   bool template_p;
4253   tree scope = NULL_TREE;
4254
4255   /* If this is a `->' operator, dereference the pointer.  */
4256   if (token_type == CPP_DEREF)
4257     postfix_expression = build_x_arrow (postfix_expression);
4258   /* Check to see whether or not the expression is type-dependent.  */
4259   dependent_p = type_dependent_expression_p (postfix_expression);
4260   /* The identifier following the `->' or `.' is not qualified.  */
4261   parser->scope = NULL_TREE;
4262   parser->qualifying_scope = NULL_TREE;
4263   parser->object_scope = NULL_TREE;
4264   *idk = CP_ID_KIND_NONE;
4265   /* Enter the scope corresponding to the type of the object
4266      given by the POSTFIX_EXPRESSION.  */
4267   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4268     {
4269       scope = TREE_TYPE (postfix_expression);
4270       /* According to the standard, no expression should ever have
4271          reference type.  Unfortunately, we do not currently match
4272          the standard in this respect in that our internal representation
4273          of an expression may have reference type even when the standard
4274          says it does not.  Therefore, we have to manually obtain the
4275          underlying type here.  */
4276       scope = non_reference (scope);
4277       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4278       scope = complete_type_or_else (scope, NULL_TREE);
4279       /* Let the name lookup machinery know that we are processing a
4280          class member access expression.  */
4281       parser->context->object_type = scope;
4282       /* If something went wrong, we want to be able to discern that case,
4283          as opposed to the case where there was no SCOPE due to the type
4284          of expression being dependent.  */
4285       if (!scope)
4286         scope = error_mark_node;
4287       /* If the SCOPE was erroneous, make the various semantic analysis
4288          functions exit quickly -- and without issuing additional error
4289          messages.  */
4290       if (scope == error_mark_node)
4291         postfix_expression = error_mark_node;
4292     }
4293
4294   /* If the SCOPE is not a scalar type, we are looking at an
4295      ordinary class member access expression, rather than a
4296      pseudo-destructor-name.  */
4297   if (!scope || !SCALAR_TYPE_P (scope))
4298     {
4299       template_p = cp_parser_optional_template_keyword (parser);
4300       /* Parse the id-expression.  */
4301       name = cp_parser_id_expression (parser, template_p,
4302                                       /*check_dependency_p=*/true,
4303                                       /*template_p=*/NULL,
4304                                       /*declarator_p=*/false);
4305       /* In general, build a SCOPE_REF if the member name is qualified.
4306          However, if the name was not dependent and has already been
4307          resolved; there is no need to build the SCOPE_REF.  For example;
4308
4309              struct X { void f(); };
4310              template <typename T> void f(T* t) { t->X::f(); }
4311
4312          Even though "t" is dependent, "X::f" is not and has been resolved
4313          to a BASELINK; there is no need to include scope information.  */
4314
4315       /* But we do need to remember that there was an explicit scope for
4316          virtual function calls.  */
4317       if (parser->scope)
4318         *idk = CP_ID_KIND_QUALIFIED;
4319
4320       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4321         {
4322           name = build_nt (SCOPE_REF, parser->scope, name);
4323           parser->scope = NULL_TREE;
4324           parser->qualifying_scope = NULL_TREE;
4325           parser->object_scope = NULL_TREE;
4326         }
4327       if (scope && name && BASELINK_P (name))
4328         adjust_result_of_qualified_name_lookup
4329           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4330       postfix_expression
4331         = finish_class_member_access_expr (postfix_expression, name);
4332     }
4333   /* Otherwise, try the pseudo-destructor-name production.  */
4334   else
4335     {
4336       tree s = NULL_TREE;
4337       tree type;
4338
4339       /* Parse the pseudo-destructor-name.  */
4340       cp_parser_pseudo_destructor_name (parser, &s, &type);
4341       /* Form the call.  */
4342       postfix_expression
4343         = finish_pseudo_destructor_expr (postfix_expression,
4344                                          s, TREE_TYPE (type));
4345     }
4346
4347   /* We no longer need to look up names in the scope of the object on
4348      the left-hand side of the `.' or `->' operator.  */
4349   parser->context->object_type = NULL_TREE;
4350
4351   /* Outside of offsetof, these operators may not appear in
4352      constant-expressions.  */
4353   if (!for_offsetof
4354       && (cp_parser_non_integral_constant_expression
4355           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4356     postfix_expression = error_mark_node;
4357
4358   return postfix_expression;
4359 }
4360
4361 /* Parse a parenthesized expression-list.
4362
4363    expression-list:
4364      assignment-expression
4365      expression-list, assignment-expression
4366
4367    attribute-list:
4368      expression-list
4369      identifier
4370      identifier, expression-list
4371
4372    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4373    representation of an assignment-expression.  Note that a TREE_LIST
4374    is returned even if there is only a single expression in the list.
4375    error_mark_node is returned if the ( and or ) are
4376    missing. NULL_TREE is returned on no expressions. The parentheses
4377    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4378    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4379    indicates whether or not all of the expressions in the list were
4380    constant.  */
4381
4382 static tree
4383 cp_parser_parenthesized_expression_list (cp_parser* parser,
4384                                          bool is_attribute_list,
4385                                          bool *non_constant_p)
4386 {
4387   tree expression_list = NULL_TREE;
4388   tree identifier = NULL_TREE;
4389
4390   /* Assume all the expressions will be constant.  */
4391   if (non_constant_p)
4392     *non_constant_p = false;
4393
4394   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4395     return error_mark_node;
4396
4397   /* Consume expressions until there are no more.  */
4398   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4399     while (true)
4400       {
4401         tree expr;
4402
4403         /* At the beginning of attribute lists, check to see if the
4404            next token is an identifier.  */
4405         if (is_attribute_list
4406             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4407           {
4408             cp_token *token;
4409
4410             /* Consume the identifier.  */
4411             token = cp_lexer_consume_token (parser->lexer);
4412             /* Save the identifier.  */
4413             identifier = token->value;
4414           }
4415         else
4416           {
4417             /* Parse the next assignment-expression.  */
4418             if (non_constant_p)
4419               {
4420                 bool expr_non_constant_p;
4421                 expr = (cp_parser_constant_expression
4422                         (parser, /*allow_non_constant_p=*/true,
4423                          &expr_non_constant_p));
4424                 if (expr_non_constant_p)
4425                   *non_constant_p = true;
4426               }
4427             else
4428               expr = cp_parser_assignment_expression (parser);
4429
4430              /* Add it to the list.  We add error_mark_node
4431                 expressions to the list, so that we can still tell if
4432                 the correct form for a parenthesized expression-list
4433                 is found. That gives better errors.  */
4434             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4435
4436             if (expr == error_mark_node)
4437               goto skip_comma;
4438           }
4439
4440         /* After the first item, attribute lists look the same as
4441            expression lists.  */
4442         is_attribute_list = false;
4443
4444       get_comma:;
4445         /* If the next token isn't a `,', then we are done.  */
4446         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4447           break;
4448
4449         /* Otherwise, consume the `,' and keep going.  */
4450         cp_lexer_consume_token (parser->lexer);
4451       }
4452
4453   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4454     {
4455       int ending;
4456
4457     skip_comma:;
4458       /* We try and resync to an unnested comma, as that will give the
4459          user better diagnostics.  */
4460       ending = cp_parser_skip_to_closing_parenthesis (parser,
4461                                                       /*recovering=*/true,
4462                                                       /*or_comma=*/true,
4463                                                       /*consume_paren=*/true);
4464       if (ending < 0)
4465         goto get_comma;
4466       if (!ending)
4467         return error_mark_node;
4468     }
4469
4470   /* We built up the list in reverse order so we must reverse it now.  */
4471   expression_list = nreverse (expression_list);
4472   if (identifier)
4473     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4474
4475   return expression_list;
4476 }
4477
4478 /* Parse a pseudo-destructor-name.
4479
4480    pseudo-destructor-name:
4481      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4482      :: [opt] nested-name-specifier template template-id :: ~ type-name
4483      :: [opt] nested-name-specifier [opt] ~ type-name
4484
4485    If either of the first two productions is used, sets *SCOPE to the
4486    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4487    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4488    or ERROR_MARK_NODE if the parse fails.  */
4489
4490 static void
4491 cp_parser_pseudo_destructor_name (cp_parser* parser,
4492                                   tree* scope,
4493                                   tree* type)
4494 {
4495   bool nested_name_specifier_p;
4496
4497   /* Assume that things will not work out.  */
4498   *type = error_mark_node;
4499
4500   /* Look for the optional `::' operator.  */
4501   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4502   /* Look for the optional nested-name-specifier.  */
4503   nested_name_specifier_p
4504     = (cp_parser_nested_name_specifier_opt (parser,
4505                                             /*typename_keyword_p=*/false,
4506                                             /*check_dependency_p=*/true,
4507                                             /*type_p=*/false,
4508                                             /*is_declaration=*/true)
4509        != NULL_TREE);
4510   /* Now, if we saw a nested-name-specifier, we might be doing the
4511      second production.  */
4512   if (nested_name_specifier_p
4513       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4514     {
4515       /* Consume the `template' keyword.  */
4516       cp_lexer_consume_token (parser->lexer);
4517       /* Parse the template-id.  */
4518       cp_parser_template_id (parser,
4519                              /*template_keyword_p=*/true,
4520                              /*check_dependency_p=*/false,
4521                              /*is_declaration=*/true);
4522       /* Look for the `::' token.  */
4523       cp_parser_require (parser, CPP_SCOPE, "`::'");
4524     }
4525   /* If the next token is not a `~', then there might be some
4526      additional qualification.  */
4527   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4528     {
4529       /* Look for the type-name.  */
4530       *scope = TREE_TYPE (cp_parser_type_name (parser));
4531
4532       if (*scope == error_mark_node)
4533         return;
4534
4535       /* If we don't have ::~, then something has gone wrong.  Since
4536          the only caller of this function is looking for something
4537          after `.' or `->' after a scalar type, most likely the
4538          program is trying to get a member of a non-aggregate
4539          type.  */
4540       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4541           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4542         {
4543           cp_parser_error (parser, "request for member of non-aggregate type");
4544           return;
4545         }
4546
4547       /* Look for the `::' token.  */
4548       cp_parser_require (parser, CPP_SCOPE, "`::'");
4549     }
4550   else
4551     *scope = NULL_TREE;
4552
4553   /* Look for the `~'.  */
4554   cp_parser_require (parser, CPP_COMPL, "`~'");
4555   /* Look for the type-name again.  We are not responsible for
4556      checking that it matches the first type-name.  */
4557   *type = cp_parser_type_name (parser);
4558 }
4559
4560 /* Parse a unary-expression.
4561
4562    unary-expression:
4563      postfix-expression
4564      ++ cast-expression
4565      -- cast-expression
4566      unary-operator cast-expression
4567      sizeof unary-expression
4568      sizeof ( type-id )
4569      new-expression
4570      delete-expression
4571
4572    GNU Extensions:
4573
4574    unary-expression:
4575      __extension__ cast-expression
4576      __alignof__ unary-expression
4577      __alignof__ ( type-id )
4578      __real__ cast-expression
4579      __imag__ cast-expression
4580      && identifier
4581
4582    ADDRESS_P is true iff the unary-expression is appearing as the
4583    operand of the `&' operator.
4584
4585    Returns a representation of the expression.  */
4586
4587 static tree
4588 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4589 {
4590   cp_token *token;
4591   enum tree_code unary_operator;
4592
4593   /* Peek at the next token.  */
4594   token = cp_lexer_peek_token (parser->lexer);
4595   /* Some keywords give away the kind of expression.  */
4596   if (token->type == CPP_KEYWORD)
4597     {
4598       enum rid keyword = token->keyword;
4599
4600       switch (keyword)
4601         {
4602         case RID_ALIGNOF:
4603         case RID_SIZEOF:
4604           {
4605             tree operand;
4606             enum tree_code op;
4607
4608             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4609             /* Consume the token.  */
4610             cp_lexer_consume_token (parser->lexer);
4611             /* Parse the operand.  */
4612             operand = cp_parser_sizeof_operand (parser, keyword);
4613
4614             if (TYPE_P (operand))
4615               return cxx_sizeof_or_alignof_type (operand, op, true);
4616             else
4617               return cxx_sizeof_or_alignof_expr (operand, op);
4618           }
4619
4620         case RID_NEW:
4621           return cp_parser_new_expression (parser);
4622
4623         case RID_DELETE:
4624           return cp_parser_delete_expression (parser);
4625
4626         case RID_EXTENSION:
4627           {
4628             /* The saved value of the PEDANTIC flag.  */
4629             int saved_pedantic;
4630             tree expr;
4631
4632             /* Save away the PEDANTIC flag.  */
4633             cp_parser_extension_opt (parser, &saved_pedantic);
4634             /* Parse the cast-expression.  */
4635             expr = cp_parser_simple_cast_expression (parser);
4636             /* Restore the PEDANTIC flag.  */
4637             pedantic = saved_pedantic;
4638
4639             return expr;
4640           }
4641
4642         case RID_REALPART:
4643         case RID_IMAGPART:
4644           {
4645             tree expression;
4646
4647             /* Consume the `__real__' or `__imag__' token.  */
4648             cp_lexer_consume_token (parser->lexer);
4649             /* Parse the cast-expression.  */
4650             expression = cp_parser_simple_cast_expression (parser);
4651             /* Create the complete representation.  */
4652             return build_x_unary_op ((keyword == RID_REALPART
4653                                       ? REALPART_EXPR : IMAGPART_EXPR),
4654                                      expression);
4655           }
4656           break;
4657
4658         default:
4659           break;
4660         }
4661     }
4662
4663   /* Look for the `:: new' and `:: delete', which also signal the
4664      beginning of a new-expression, or delete-expression,
4665      respectively.  If the next token is `::', then it might be one of
4666      these.  */
4667   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4668     {
4669       enum rid keyword;
4670
4671       /* See if the token after the `::' is one of the keywords in
4672          which we're interested.  */
4673       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4674       /* If it's `new', we have a new-expression.  */
4675       if (keyword == RID_NEW)
4676         return cp_parser_new_expression (parser);
4677       /* Similarly, for `delete'.  */
4678       else if (keyword == RID_DELETE)
4679         return cp_parser_delete_expression (parser);
4680     }
4681
4682   /* Look for a unary operator.  */
4683   unary_operator = cp_parser_unary_operator (token);
4684   /* The `++' and `--' operators can be handled similarly, even though
4685      they are not technically unary-operators in the grammar.  */
4686   if (unary_operator == ERROR_MARK)
4687     {
4688       if (token->type == CPP_PLUS_PLUS)
4689         unary_operator = PREINCREMENT_EXPR;
4690       else if (token->type == CPP_MINUS_MINUS)
4691         unary_operator = PREDECREMENT_EXPR;
4692       /* Handle the GNU address-of-label extension.  */
4693       else if (cp_parser_allow_gnu_extensions_p (parser)
4694                && token->type == CPP_AND_AND)
4695         {
4696           tree identifier;
4697
4698           /* Consume the '&&' token.  */
4699           cp_lexer_consume_token (parser->lexer);
4700           /* Look for the identifier.  */
4701           identifier = cp_parser_identifier (parser);
4702           /* Create an expression representing the address.  */
4703           return finish_label_address_expr (identifier);
4704         }
4705     }
4706   if (unary_operator != ERROR_MARK)
4707     {
4708       tree cast_expression;
4709       tree expression = error_mark_node;
4710       const char *non_constant_p = NULL;
4711
4712       /* Consume the operator token.  */
4713       token = cp_lexer_consume_token (parser->lexer);
4714       /* Parse the cast-expression.  */
4715       cast_expression
4716         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4717       /* Now, build an appropriate representation.  */
4718       switch (unary_operator)
4719         {
4720         case INDIRECT_REF:
4721           non_constant_p = "`*'";
4722           expression = build_x_indirect_ref (cast_expression, "unary *");
4723           break;
4724
4725         case ADDR_EXPR:
4726           non_constant_p = "`&'";
4727           /* Fall through.  */
4728         case BIT_NOT_EXPR:
4729           expression = build_x_unary_op (unary_operator, cast_expression);
4730           break;
4731
4732         case PREINCREMENT_EXPR:
4733         case PREDECREMENT_EXPR:
4734           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4735                             ? "`++'" : "`--'");
4736           /* Fall through.  */
4737         case CONVERT_EXPR:
4738         case NEGATE_EXPR:
4739         case TRUTH_NOT_EXPR:
4740           expression = finish_unary_op_expr (unary_operator, cast_expression);
4741           break;
4742
4743         default:
4744           gcc_unreachable ();
4745         }
4746
4747       if (non_constant_p
4748           && cp_parser_non_integral_constant_expression (parser,
4749                                                          non_constant_p))
4750         expression = error_mark_node;
4751
4752       return expression;
4753     }
4754
4755   return cp_parser_postfix_expression (parser, address_p);
4756 }
4757
4758 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4759    unary-operator, the corresponding tree code is returned.  */
4760
4761 static enum tree_code
4762 cp_parser_unary_operator (cp_token* token)
4763 {
4764   switch (token->type)
4765     {
4766     case CPP_MULT:
4767       return INDIRECT_REF;
4768
4769     case CPP_AND:
4770       return ADDR_EXPR;
4771
4772     case CPP_PLUS:
4773       return CONVERT_EXPR;
4774
4775     case CPP_MINUS:
4776       return NEGATE_EXPR;
4777
4778     case CPP_NOT:
4779       return TRUTH_NOT_EXPR;
4780
4781     case CPP_COMPL:
4782       return BIT_NOT_EXPR;
4783
4784     default:
4785       return ERROR_MARK;
4786     }
4787 }
4788
4789 /* Parse a new-expression.
4790
4791    new-expression:
4792      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4793      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4794
4795    Returns a representation of the expression.  */
4796
4797 static tree
4798 cp_parser_new_expression (cp_parser* parser)
4799 {
4800   bool global_scope_p;
4801   tree placement;
4802   tree type;
4803   tree initializer;
4804   tree nelts;
4805
4806   /* Look for the optional `::' operator.  */
4807   global_scope_p
4808     = (cp_parser_global_scope_opt (parser,
4809                                    /*current_scope_valid_p=*/false)
4810        != NULL_TREE);
4811   /* Look for the `new' operator.  */
4812   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4813   /* There's no easy way to tell a new-placement from the
4814      `( type-id )' construct.  */
4815   cp_parser_parse_tentatively (parser);
4816   /* Look for a new-placement.  */
4817   placement = cp_parser_new_placement (parser);
4818   /* If that didn't work out, there's no new-placement.  */
4819   if (!cp_parser_parse_definitely (parser))
4820     placement = NULL_TREE;
4821
4822   /* If the next token is a `(', then we have a parenthesized
4823      type-id.  */
4824   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4825     {
4826       /* Consume the `('.  */
4827       cp_lexer_consume_token (parser->lexer);
4828       /* Parse the type-id.  */
4829       type = cp_parser_type_id (parser);
4830       /* Look for the closing `)'.  */
4831       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4832       /* There should not be a direct-new-declarator in this production,
4833          but GCC used to allowed this, so we check and emit a sensible error
4834          message for this case.  */
4835       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4836         {
4837           error ("array bound forbidden after parenthesized type-id");
4838           inform ("try removing the parentheses around the type-id");
4839           cp_parser_direct_new_declarator (parser);
4840         }
4841       nelts = integer_one_node;
4842     }
4843   /* Otherwise, there must be a new-type-id.  */
4844   else
4845     type = cp_parser_new_type_id (parser, &nelts);
4846
4847   /* If the next token is a `(', then we have a new-initializer.  */
4848   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4849     initializer = cp_parser_new_initializer (parser);
4850   else
4851     initializer = NULL_TREE;
4852
4853   /* A new-expression may not appear in an integral constant
4854      expression.  */
4855   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4856     return error_mark_node;
4857
4858   /* Create a representation of the new-expression.  */
4859   return build_new (placement, type, nelts, initializer, global_scope_p);
4860 }
4861
4862 /* Parse a new-placement.
4863
4864    new-placement:
4865      ( expression-list )
4866
4867    Returns the same representation as for an expression-list.  */
4868
4869 static tree
4870 cp_parser_new_placement (cp_parser* parser)
4871 {
4872   tree expression_list;
4873
4874   /* Parse the expression-list.  */
4875   expression_list = (cp_parser_parenthesized_expression_list
4876                      (parser, false, /*non_constant_p=*/NULL));
4877
4878   return expression_list;
4879 }
4880
4881 /* Parse a new-type-id.
4882
4883    new-type-id:
4884      type-specifier-seq new-declarator [opt]
4885
4886    Returns the TYPE allocated.  If the new-type-id indicates an array
4887    type, *NELTS is set to the number of elements in the last array
4888    bound; the TYPE will not include the last array bound.  */
4889
4890 static tree
4891 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4892 {
4893   cp_decl_specifier_seq type_specifier_seq;
4894   cp_declarator *new_declarator;
4895   cp_declarator *declarator;
4896   cp_declarator *outer_declarator;
4897   const char *saved_message;
4898   tree type;
4899
4900   /* The type-specifier sequence must not contain type definitions.
4901      (It cannot contain declarations of new types either, but if they
4902      are not definitions we will catch that because they are not
4903      complete.)  */
4904   saved_message = parser->type_definition_forbidden_message;
4905   parser->type_definition_forbidden_message
4906     = "types may not be defined in a new-type-id";
4907   /* Parse the type-specifier-seq.  */
4908   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4909   /* Restore the old message.  */
4910   parser->type_definition_forbidden_message = saved_message;
4911   /* Parse the new-declarator.  */
4912   new_declarator = cp_parser_new_declarator_opt (parser);
4913
4914   /* Determine the number of elements in the last array dimension, if
4915      any.  */
4916   *nelts = NULL_TREE;
4917   /* Skip down to the last array dimension.  */
4918   declarator = new_declarator;
4919   outer_declarator = NULL;
4920   while (declarator && (declarator->kind == cdk_pointer
4921                         || declarator->kind == cdk_ptrmem))
4922     {
4923       outer_declarator = declarator;
4924       declarator = declarator->declarator;
4925     }
4926   while (declarator
4927          && declarator->kind == cdk_array
4928          && declarator->declarator
4929          && declarator->declarator->kind == cdk_array)
4930     {
4931       outer_declarator = declarator;
4932       declarator = declarator->declarator;
4933     }
4934
4935   if (declarator && declarator->kind == cdk_array)
4936     {
4937       *nelts = declarator->u.array.bounds;
4938       if (*nelts == error_mark_node)
4939         *nelts = integer_one_node;
4940       else if (!processing_template_decl)
4941         {
4942           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4943                                            false))
4944             pedwarn ("size in array new must have integral type");
4945           *nelts = save_expr (cp_convert (sizetype, *nelts));
4946           if (*nelts == integer_zero_node)
4947             warning ("zero size array reserves no space");
4948         }
4949       if (outer_declarator)
4950         outer_declarator->declarator = declarator->declarator;
4951       else
4952         new_declarator = NULL;
4953     }
4954
4955   type = groktypename (&type_specifier_seq, new_declarator);
4956   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4957     {
4958       *nelts = array_type_nelts_top (type);
4959       type = TREE_TYPE (type);
4960     }
4961   return type;
4962 }
4963
4964 /* Parse an (optional) new-declarator.
4965
4966    new-declarator:
4967      ptr-operator new-declarator [opt]
4968      direct-new-declarator
4969
4970    Returns the declarator.  */
4971
4972 static cp_declarator *
4973 cp_parser_new_declarator_opt (cp_parser* parser)
4974 {
4975   enum tree_code code;
4976   tree type;
4977   cp_cv_quals cv_quals;
4978
4979   /* We don't know if there's a ptr-operator next, or not.  */
4980   cp_parser_parse_tentatively (parser);
4981   /* Look for a ptr-operator.  */
4982   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4983   /* If that worked, look for more new-declarators.  */
4984   if (cp_parser_parse_definitely (parser))
4985     {
4986       cp_declarator *declarator;
4987
4988       /* Parse another optional declarator.  */
4989       declarator = cp_parser_new_declarator_opt (parser);
4990
4991       /* Create the representation of the declarator.  */
4992       if (type)
4993         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4994       else if (code == INDIRECT_REF)
4995         declarator = make_pointer_declarator (cv_quals, declarator);
4996       else
4997         declarator = make_reference_declarator (cv_quals, declarator);
4998
4999       return declarator;
5000     }
5001
5002   /* If the next token is a `[', there is a direct-new-declarator.  */
5003   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5004     return cp_parser_direct_new_declarator (parser);
5005
5006   return NULL;
5007 }
5008
5009 /* Parse a direct-new-declarator.
5010
5011    direct-new-declarator:
5012      [ expression ]
5013      direct-new-declarator [constant-expression]
5014
5015    */
5016
5017 static cp_declarator *
5018 cp_parser_direct_new_declarator (cp_parser* parser)
5019 {
5020   cp_declarator *declarator = NULL;
5021
5022   while (true)
5023     {
5024       tree expression;
5025
5026       /* Look for the opening `['.  */
5027       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5028       /* The first expression is not required to be constant.  */
5029       if (!declarator)
5030         {
5031           expression = cp_parser_expression (parser);
5032           /* The standard requires that the expression have integral
5033              type.  DR 74 adds enumeration types.  We believe that the
5034              real intent is that these expressions be handled like the
5035              expression in a `switch' condition, which also allows
5036              classes with a single conversion to integral or
5037              enumeration type.  */
5038           if (!processing_template_decl)
5039             {
5040               expression
5041                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5042                                               expression,
5043                                               /*complain=*/true);
5044               if (!expression)
5045                 {
5046                   error ("expression in new-declarator must have integral or enumeration type");
5047                   expression = error_mark_node;
5048                 }
5049             }
5050         }
5051       /* But all the other expressions must be.  */
5052       else
5053         expression
5054           = cp_parser_constant_expression (parser,
5055                                            /*allow_non_constant=*/false,
5056                                            NULL);
5057       /* Look for the closing `]'.  */
5058       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5059
5060       /* Add this bound to the declarator.  */
5061       declarator = make_array_declarator (declarator, expression);
5062
5063       /* If the next token is not a `[', then there are no more
5064          bounds.  */
5065       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5066         break;
5067     }
5068
5069   return declarator;
5070 }
5071
5072 /* Parse a new-initializer.
5073
5074    new-initializer:
5075      ( expression-list [opt] )
5076
5077    Returns a representation of the expression-list.  If there is no
5078    expression-list, VOID_ZERO_NODE is returned.  */
5079
5080 static tree
5081 cp_parser_new_initializer (cp_parser* parser)
5082 {
5083   tree expression_list;
5084
5085   expression_list = (cp_parser_parenthesized_expression_list
5086                      (parser, false, /*non_constant_p=*/NULL));
5087   if (!expression_list)
5088     expression_list = void_zero_node;
5089
5090   return expression_list;
5091 }
5092
5093 /* Parse a delete-expression.
5094
5095    delete-expression:
5096      :: [opt] delete cast-expression
5097      :: [opt] delete [ ] cast-expression
5098
5099    Returns a representation of the expression.  */
5100
5101 static tree
5102 cp_parser_delete_expression (cp_parser* parser)
5103 {
5104   bool global_scope_p;
5105   bool array_p;
5106   tree expression;
5107
5108   /* Look for the optional `::' operator.  */
5109   global_scope_p
5110     = (cp_parser_global_scope_opt (parser,
5111                                    /*current_scope_valid_p=*/false)
5112        != NULL_TREE);
5113   /* Look for the `delete' keyword.  */
5114   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5115   /* See if the array syntax is in use.  */
5116   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5117     {
5118       /* Consume the `[' token.  */
5119       cp_lexer_consume_token (parser->lexer);
5120       /* Look for the `]' token.  */
5121       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5122       /* Remember that this is the `[]' construct.  */
5123       array_p = true;
5124     }
5125   else
5126     array_p = false;
5127
5128   /* Parse the cast-expression.  */
5129   expression = cp_parser_simple_cast_expression (parser);
5130
5131   /* A delete-expression may not appear in an integral constant
5132      expression.  */
5133   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5134     return error_mark_node;
5135
5136   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5137 }
5138
5139 /* Parse a cast-expression.
5140
5141    cast-expression:
5142      unary-expression
5143      ( type-id ) cast-expression
5144
5145    Returns a representation of the expression.  */
5146
5147 static tree
5148 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5149 {
5150   /* If it's a `(', then we might be looking at a cast.  */
5151   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5152     {
5153       tree type = NULL_TREE;
5154       tree expr = NULL_TREE;
5155       bool compound_literal_p;
5156       const char *saved_message;
5157
5158       /* There's no way to know yet whether or not this is a cast.
5159          For example, `(int (3))' is a unary-expression, while `(int)
5160          3' is a cast.  So, we resort to parsing tentatively.  */
5161       cp_parser_parse_tentatively (parser);
5162       /* Types may not be defined in a cast.  */
5163       saved_message = parser->type_definition_forbidden_message;
5164       parser->type_definition_forbidden_message
5165         = "types may not be defined in casts";
5166       /* Consume the `('.  */
5167       cp_lexer_consume_token (parser->lexer);
5168       /* A very tricky bit is that `(struct S) { 3 }' is a
5169          compound-literal (which we permit in C++ as an extension).
5170          But, that construct is not a cast-expression -- it is a
5171          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5172          is legal; if the compound-literal were a cast-expression,
5173          you'd need an extra set of parentheses.)  But, if we parse
5174          the type-id, and it happens to be a class-specifier, then we
5175          will commit to the parse at that point, because we cannot
5176          undo the action that is done when creating a new class.  So,
5177          then we cannot back up and do a postfix-expression.
5178
5179          Therefore, we scan ahead to the closing `)', and check to see
5180          if the token after the `)' is a `{'.  If so, we are not
5181          looking at a cast-expression.
5182
5183          Save tokens so that we can put them back.  */
5184       cp_lexer_save_tokens (parser->lexer);
5185       /* Skip tokens until the next token is a closing parenthesis.
5186          If we find the closing `)', and the next token is a `{', then
5187          we are looking at a compound-literal.  */
5188       compound_literal_p
5189         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5190                                                   /*consume_paren=*/true)
5191            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5192       /* Roll back the tokens we skipped.  */
5193       cp_lexer_rollback_tokens (parser->lexer);
5194       /* If we were looking at a compound-literal, simulate an error
5195          so that the call to cp_parser_parse_definitely below will
5196          fail.  */
5197       if (compound_literal_p)
5198         cp_parser_simulate_error (parser);
5199       else
5200         {
5201           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5202           parser->in_type_id_in_expr_p = true;
5203           /* Look for the type-id.  */
5204           type = cp_parser_type_id (parser);
5205           /* Look for the closing `)'.  */
5206           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5207           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5208         }
5209
5210       /* Restore the saved message.  */
5211       parser->type_definition_forbidden_message = saved_message;
5212
5213       /* If ok so far, parse the dependent expression. We cannot be
5214          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5215          ctor of T, but looks like a cast to function returning T
5216          without a dependent expression.  */
5217       if (!cp_parser_error_occurred (parser))
5218         expr = cp_parser_simple_cast_expression (parser);
5219
5220       if (cp_parser_parse_definitely (parser))
5221         {
5222           /* Warn about old-style casts, if so requested.  */
5223           if (warn_old_style_cast
5224               && !in_system_header
5225               && !VOID_TYPE_P (type)
5226               && current_lang_name != lang_name_c)
5227             warning ("use of old-style cast");
5228
5229           /* Only type conversions to integral or enumeration types
5230              can be used in constant-expressions.  */
5231           if (parser->integral_constant_expression_p
5232               && !dependent_type_p (type)
5233               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5234               && (cp_parser_non_integral_constant_expression
5235                   (parser,
5236                    "a cast to a type other than an integral or "
5237                    "enumeration type")))
5238             return error_mark_node;
5239
5240           /* Perform the cast.  */
5241           expr = build_c_cast (type, expr);
5242           return expr;
5243         }
5244     }
5245
5246   /* If we get here, then it's not a cast, so it must be a
5247      unary-expression.  */
5248   return cp_parser_unary_expression (parser, address_p);
5249 }
5250
5251 /* Parse a pm-expression.
5252
5253    pm-expression:
5254      cast-expression
5255      pm-expression .* cast-expression
5256      pm-expression ->* cast-expression
5257
5258      Returns a representation of the expression.  */
5259
5260 static tree
5261 cp_parser_pm_expression (cp_parser* parser)
5262 {
5263   static const cp_parser_token_tree_map map = {
5264     { CPP_DEREF_STAR, MEMBER_REF },
5265     { CPP_DOT_STAR, DOTSTAR_EXPR },
5266     { CPP_EOF, ERROR_MARK }
5267   };
5268
5269   return cp_parser_binary_expression (parser, map,
5270                                       cp_parser_simple_cast_expression);
5271 }
5272
5273 /* Parse a multiplicative-expression.
5274
5275    multiplicative-expression:
5276      pm-expression
5277      multiplicative-expression * pm-expression
5278      multiplicative-expression / pm-expression
5279      multiplicative-expression % pm-expression
5280
5281    Returns a representation of the expression.  */
5282
5283 static tree
5284 cp_parser_multiplicative_expression (cp_parser* parser)
5285 {
5286   static const cp_parser_token_tree_map map = {
5287     { CPP_MULT, MULT_EXPR },
5288     { CPP_DIV, TRUNC_DIV_EXPR },
5289     { CPP_MOD, TRUNC_MOD_EXPR },
5290     { CPP_EOF, ERROR_MARK }
5291   };
5292
5293   return cp_parser_binary_expression (parser,
5294                                       map,
5295                                       cp_parser_pm_expression);
5296 }
5297
5298 /* Parse an additive-expression.
5299
5300    additive-expression:
5301      multiplicative-expression
5302      additive-expression + multiplicative-expression
5303      additive-expression - multiplicative-expression
5304
5305    Returns a representation of the expression.  */
5306
5307 static tree
5308 cp_parser_additive_expression (cp_parser* parser)
5309 {
5310   static const cp_parser_token_tree_map map = {
5311     { CPP_PLUS, PLUS_EXPR },
5312     { CPP_MINUS, MINUS_EXPR },
5313     { CPP_EOF, ERROR_MARK }
5314   };
5315
5316   return cp_parser_binary_expression (parser,
5317                                       map,
5318                                       cp_parser_multiplicative_expression);
5319 }
5320
5321 /* Parse a shift-expression.
5322
5323    shift-expression:
5324      additive-expression
5325      shift-expression << additive-expression
5326      shift-expression >> additive-expression
5327
5328    Returns a representation of the expression.  */
5329
5330 static tree
5331 cp_parser_shift_expression (cp_parser* parser)
5332 {
5333   static const cp_parser_token_tree_map map = {
5334     { CPP_LSHIFT, LSHIFT_EXPR },
5335     { CPP_RSHIFT, RSHIFT_EXPR },
5336     { CPP_EOF, ERROR_MARK }
5337   };
5338
5339   return cp_parser_binary_expression (parser,
5340                                       map,
5341                                       cp_parser_additive_expression);
5342 }
5343
5344 /* Parse a relational-expression.
5345
5346    relational-expression:
5347      shift-expression
5348      relational-expression < shift-expression
5349      relational-expression > shift-expression
5350      relational-expression <= shift-expression
5351      relational-expression >= shift-expression
5352
5353    GNU Extension:
5354
5355    relational-expression:
5356      relational-expression <? shift-expression
5357      relational-expression >? shift-expression
5358
5359    Returns a representation of the expression.  */
5360
5361 static tree
5362 cp_parser_relational_expression (cp_parser* parser)
5363 {
5364   static const cp_parser_token_tree_map map = {
5365     { CPP_LESS, LT_EXPR },
5366     { CPP_GREATER, GT_EXPR },
5367     { CPP_LESS_EQ, LE_EXPR },
5368     { CPP_GREATER_EQ, GE_EXPR },
5369     { CPP_MIN, MIN_EXPR },
5370     { CPP_MAX, MAX_EXPR },
5371     { CPP_EOF, ERROR_MARK }
5372   };
5373
5374   return cp_parser_binary_expression (parser,
5375                                       map,
5376                                       cp_parser_shift_expression);
5377 }
5378
5379 /* Parse an equality-expression.
5380
5381    equality-expression:
5382      relational-expression
5383      equality-expression == relational-expression
5384      equality-expression != relational-expression
5385
5386    Returns a representation of the expression.  */
5387
5388 static tree
5389 cp_parser_equality_expression (cp_parser* parser)
5390 {
5391   static const cp_parser_token_tree_map map = {
5392     { CPP_EQ_EQ, EQ_EXPR },
5393     { CPP_NOT_EQ, NE_EXPR },
5394     { CPP_EOF, ERROR_MARK }
5395   };
5396
5397   return cp_parser_binary_expression (parser,
5398                                       map,
5399                                       cp_parser_relational_expression);
5400 }
5401
5402 /* Parse an and-expression.
5403
5404    and-expression:
5405      equality-expression
5406      and-expression & equality-expression
5407
5408    Returns a representation of the expression.  */
5409
5410 static tree
5411 cp_parser_and_expression (cp_parser* parser)
5412 {
5413   static const cp_parser_token_tree_map map = {
5414     { CPP_AND, BIT_AND_EXPR },
5415     { CPP_EOF, ERROR_MARK }
5416   };
5417
5418   return cp_parser_binary_expression (parser,
5419                                       map,
5420                                       cp_parser_equality_expression);
5421 }
5422
5423 /* Parse an exclusive-or-expression.
5424
5425    exclusive-or-expression:
5426      and-expression
5427      exclusive-or-expression ^ and-expression
5428
5429    Returns a representation of the expression.  */
5430
5431 static tree
5432 cp_parser_exclusive_or_expression (cp_parser* parser)
5433 {
5434   static const cp_parser_token_tree_map map = {
5435     { CPP_XOR, BIT_XOR_EXPR },
5436     { CPP_EOF, ERROR_MARK }
5437   };
5438
5439   return cp_parser_binary_expression (parser,
5440                                       map,
5441                                       cp_parser_and_expression);
5442 }
5443
5444
5445 /* Parse an inclusive-or-expression.
5446
5447    inclusive-or-expression:
5448      exclusive-or-expression
5449      inclusive-or-expression | exclusive-or-expression
5450
5451    Returns a representation of the expression.  */
5452
5453 static tree
5454 cp_parser_inclusive_or_expression (cp_parser* parser)
5455 {
5456   static const cp_parser_token_tree_map map = {
5457     { CPP_OR, BIT_IOR_EXPR },
5458     { CPP_EOF, ERROR_MARK }
5459   };
5460
5461   return cp_parser_binary_expression (parser,
5462                                       map,
5463                                       cp_parser_exclusive_or_expression);
5464 }
5465
5466 /* Parse a logical-and-expression.
5467
5468    logical-and-expression:
5469      inclusive-or-expression
5470      logical-and-expression && inclusive-or-expression
5471
5472    Returns a representation of the expression.  */
5473
5474 static tree
5475 cp_parser_logical_and_expression (cp_parser* parser)
5476 {
5477   static const cp_parser_token_tree_map map = {
5478     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5479     { CPP_EOF, ERROR_MARK }
5480   };
5481
5482   return cp_parser_binary_expression (parser,
5483                                       map,
5484                                       cp_parser_inclusive_or_expression);
5485 }
5486
5487 /* Parse a logical-or-expression.
5488
5489    logical-or-expression:
5490      logical-and-expression
5491      logical-or-expression || logical-and-expression
5492
5493    Returns a representation of the expression.  */
5494
5495 static tree
5496 cp_parser_logical_or_expression (cp_parser* parser)
5497 {
5498   static const cp_parser_token_tree_map map = {
5499     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5500     { CPP_EOF, ERROR_MARK }
5501   };
5502
5503   return cp_parser_binary_expression (parser,
5504                                       map,
5505                                       cp_parser_logical_and_expression);
5506 }
5507
5508 /* Parse the `? expression : assignment-expression' part of a
5509    conditional-expression.  The LOGICAL_OR_EXPR is the
5510    logical-or-expression that started the conditional-expression.
5511    Returns a representation of the entire conditional-expression.
5512
5513    This routine is used by cp_parser_assignment_expression.
5514
5515      ? expression : assignment-expression
5516
5517    GNU Extensions:
5518
5519      ? : assignment-expression */
5520
5521 static tree
5522 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5523 {
5524   tree expr;
5525   tree assignment_expr;
5526
5527   /* Consume the `?' token.  */
5528   cp_lexer_consume_token (parser->lexer);
5529   if (cp_parser_allow_gnu_extensions_p (parser)
5530       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5531     /* Implicit true clause.  */
5532     expr = NULL_TREE;
5533   else
5534     /* Parse the expression.  */
5535     expr = cp_parser_expression (parser);
5536
5537   /* The next token should be a `:'.  */
5538   cp_parser_require (parser, CPP_COLON, "`:'");
5539   /* Parse the assignment-expression.  */
5540   assignment_expr = cp_parser_assignment_expression (parser);
5541
5542   /* Build the conditional-expression.  */
5543   return build_x_conditional_expr (logical_or_expr,
5544                                    expr,
5545                                    assignment_expr);
5546 }
5547
5548 /* Parse an assignment-expression.
5549
5550    assignment-expression:
5551      conditional-expression
5552      logical-or-expression assignment-operator assignment_expression
5553      throw-expression
5554
5555    Returns a representation for the expression.  */
5556
5557 static tree
5558 cp_parser_assignment_expression (cp_parser* parser)
5559 {
5560   tree expr;
5561
5562   /* If the next token is the `throw' keyword, then we're looking at
5563      a throw-expression.  */
5564   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5565     expr = cp_parser_throw_expression (parser);
5566   /* Otherwise, it must be that we are looking at a
5567      logical-or-expression.  */
5568   else
5569     {
5570       /* Parse the logical-or-expression.  */
5571       expr = cp_parser_logical_or_expression (parser);
5572       /* If the next token is a `?' then we're actually looking at a
5573          conditional-expression.  */
5574       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5575         return cp_parser_question_colon_clause (parser, expr);
5576       else
5577         {
5578           enum tree_code assignment_operator;
5579
5580           /* If it's an assignment-operator, we're using the second
5581              production.  */
5582           assignment_operator
5583             = cp_parser_assignment_operator_opt (parser);
5584           if (assignment_operator != ERROR_MARK)
5585             {
5586               tree rhs;
5587
5588               /* Parse the right-hand side of the assignment.  */
5589               rhs = cp_parser_assignment_expression (parser);
5590               /* An assignment may not appear in a
5591                  constant-expression.  */
5592               if (cp_parser_non_integral_constant_expression (parser,
5593                                                               "an assignment"))
5594                 return error_mark_node;
5595               /* Build the assignment expression.  */
5596               expr = build_x_modify_expr (expr,
5597                                           assignment_operator,
5598                                           rhs);
5599             }
5600         }
5601     }
5602
5603   return expr;
5604 }
5605
5606 /* Parse an (optional) assignment-operator.
5607
5608    assignment-operator: one of
5609      = *= /= %= += -= >>= <<= &= ^= |=
5610
5611    GNU Extension:
5612
5613    assignment-operator: one of
5614      <?= >?=
5615
5616    If the next token is an assignment operator, the corresponding tree
5617    code is returned, and the token is consumed.  For example, for
5618    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5619    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5620    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5621    operator, ERROR_MARK is returned.  */
5622
5623 static enum tree_code
5624 cp_parser_assignment_operator_opt (cp_parser* parser)
5625 {
5626   enum tree_code op;
5627   cp_token *token;
5628
5629   /* Peek at the next toen.  */
5630   token = cp_lexer_peek_token (parser->lexer);
5631
5632   switch (token->type)
5633     {
5634     case CPP_EQ:
5635       op = NOP_EXPR;
5636       break;
5637
5638     case CPP_MULT_EQ:
5639       op = MULT_EXPR;
5640       break;
5641
5642     case CPP_DIV_EQ:
5643       op = TRUNC_DIV_EXPR;
5644       break;
5645
5646     case CPP_MOD_EQ:
5647       op = TRUNC_MOD_EXPR;
5648       break;
5649
5650     case CPP_PLUS_EQ:
5651       op = PLUS_EXPR;
5652       break;
5653
5654     case CPP_MINUS_EQ:
5655       op = MINUS_EXPR;
5656       break;
5657
5658     case CPP_RSHIFT_EQ:
5659       op = RSHIFT_EXPR;
5660       break;
5661
5662     case CPP_LSHIFT_EQ:
5663       op = LSHIFT_EXPR;
5664       break;
5665
5666     case CPP_AND_EQ:
5667       op = BIT_AND_EXPR;
5668       break;
5669
5670     case CPP_XOR_EQ:
5671       op = BIT_XOR_EXPR;
5672       break;
5673
5674     case CPP_OR_EQ:
5675       op = BIT_IOR_EXPR;
5676       break;
5677
5678     case CPP_MIN_EQ:
5679       op = MIN_EXPR;
5680       break;
5681
5682     case CPP_MAX_EQ:
5683       op = MAX_EXPR;
5684       break;
5685
5686     default:
5687       /* Nothing else is an assignment operator.  */
5688       op = ERROR_MARK;
5689     }
5690
5691   /* If it was an assignment operator, consume it.  */
5692   if (op != ERROR_MARK)
5693     cp_lexer_consume_token (parser->lexer);
5694
5695   return op;
5696 }
5697
5698 /* Parse an expression.
5699
5700    expression:
5701      assignment-expression
5702      expression , assignment-expression
5703
5704    Returns a representation of the expression.  */
5705
5706 static tree
5707 cp_parser_expression (cp_parser* parser)
5708 {
5709   tree expression = NULL_TREE;
5710
5711   while (true)
5712     {
5713       tree assignment_expression;
5714
5715       /* Parse the next assignment-expression.  */
5716       assignment_expression
5717         = cp_parser_assignment_expression (parser);
5718       /* If this is the first assignment-expression, we can just
5719          save it away.  */
5720       if (!expression)
5721         expression = assignment_expression;
5722       else
5723         expression = build_x_compound_expr (expression,
5724                                             assignment_expression);
5725       /* If the next token is not a comma, then we are done with the
5726          expression.  */
5727       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5728         break;
5729       /* Consume the `,'.  */
5730       cp_lexer_consume_token (parser->lexer);
5731       /* A comma operator cannot appear in a constant-expression.  */
5732       if (cp_parser_non_integral_constant_expression (parser,
5733                                                       "a comma operator"))
5734         expression = error_mark_node;
5735     }
5736
5737   return expression;
5738 }
5739
5740 /* Parse a constant-expression.
5741
5742    constant-expression:
5743      conditional-expression
5744
5745   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5746   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5747   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5748   is false, NON_CONSTANT_P should be NULL.  */
5749
5750 static tree
5751 cp_parser_constant_expression (cp_parser* parser,
5752                                bool allow_non_constant_p,
5753                                bool *non_constant_p)
5754 {
5755   bool saved_integral_constant_expression_p;
5756   bool saved_allow_non_integral_constant_expression_p;
5757   bool saved_non_integral_constant_expression_p;
5758   tree expression;
5759
5760   /* It might seem that we could simply parse the
5761      conditional-expression, and then check to see if it were
5762      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5763      one that the compiler can figure out is constant, possibly after
5764      doing some simplifications or optimizations.  The standard has a
5765      precise definition of constant-expression, and we must honor
5766      that, even though it is somewhat more restrictive.
5767
5768      For example:
5769
5770        int i[(2, 3)];
5771
5772      is not a legal declaration, because `(2, 3)' is not a
5773      constant-expression.  The `,' operator is forbidden in a
5774      constant-expression.  However, GCC's constant-folding machinery
5775      will fold this operation to an INTEGER_CST for `3'.  */
5776
5777   /* Save the old settings.  */
5778   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5779   saved_allow_non_integral_constant_expression_p
5780     = parser->allow_non_integral_constant_expression_p;
5781   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5782   /* We are now parsing a constant-expression.  */
5783   parser->integral_constant_expression_p = true;
5784   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5785   parser->non_integral_constant_expression_p = false;
5786   /* Although the grammar says "conditional-expression", we parse an
5787      "assignment-expression", which also permits "throw-expression"
5788      and the use of assignment operators.  In the case that
5789      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5790      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5791      actually essential that we look for an assignment-expression.
5792      For example, cp_parser_initializer_clauses uses this function to
5793      determine whether a particular assignment-expression is in fact
5794      constant.  */
5795   expression = cp_parser_assignment_expression (parser);
5796   /* Restore the old settings.  */
5797   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5798   parser->allow_non_integral_constant_expression_p
5799     = saved_allow_non_integral_constant_expression_p;
5800   if (allow_non_constant_p)
5801     *non_constant_p = parser->non_integral_constant_expression_p;
5802   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5803
5804   return expression;
5805 }
5806
5807 /* Parse __builtin_offsetof.
5808
5809    offsetof-expression:
5810      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5811
5812    offsetof-member-designator:
5813      id-expression
5814      | offsetof-member-designator "." id-expression
5815      | offsetof-member-designator "[" expression "]"
5816 */
5817
5818 static tree
5819 cp_parser_builtin_offsetof (cp_parser *parser)
5820 {
5821   int save_ice_p, save_non_ice_p;
5822   tree type, expr;
5823   cp_id_kind dummy;
5824
5825   /* We're about to accept non-integral-constant things, but will
5826      definitely yield an integral constant expression.  Save and
5827      restore these values around our local parsing.  */
5828   save_ice_p = parser->integral_constant_expression_p;
5829   save_non_ice_p = parser->non_integral_constant_expression_p;
5830
5831   /* Consume the "__builtin_offsetof" token.  */
5832   cp_lexer_consume_token (parser->lexer);
5833   /* Consume the opening `('.  */
5834   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5835   /* Parse the type-id.  */
5836   type = cp_parser_type_id (parser);
5837   /* Look for the `,'.  */
5838   cp_parser_require (parser, CPP_COMMA, "`,'");
5839
5840   /* Build the (type *)null that begins the traditional offsetof macro.  */
5841   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5842
5843   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5844   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5845                                                  true, &dummy);
5846   while (true)
5847     {
5848       cp_token *token = cp_lexer_peek_token (parser->lexer);
5849       switch (token->type)
5850         {
5851         case CPP_OPEN_SQUARE:
5852           /* offsetof-member-designator "[" expression "]" */
5853           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5854           break;
5855
5856         case CPP_DOT:
5857           /* offsetof-member-designator "." identifier */
5858           cp_lexer_consume_token (parser->lexer);
5859           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5860                                                          true, &dummy);
5861           break;
5862
5863         case CPP_CLOSE_PAREN:
5864           /* Consume the ")" token.  */
5865           cp_lexer_consume_token (parser->lexer);
5866           goto success;
5867
5868         default:
5869           /* Error.  We know the following require will fail, but
5870              that gives the proper error message.  */
5871           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5872           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5873           expr = error_mark_node;
5874           goto failure;
5875         }
5876     }
5877
5878  success:
5879   /* If we're processing a template, we can't finish the semantics yet.
5880      Otherwise we can fold the entire expression now.  */
5881   if (processing_template_decl)
5882     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5883   else
5884     expr = fold_offsetof (expr);
5885
5886  failure:
5887   parser->integral_constant_expression_p = save_ice_p;
5888   parser->non_integral_constant_expression_p = save_non_ice_p;
5889
5890   return expr;
5891 }
5892
5893 /* Statements [gram.stmt.stmt]  */
5894
5895 /* Parse a statement.
5896
5897    statement:
5898      labeled-statement
5899      expression-statement
5900      compound-statement
5901      selection-statement
5902      iteration-statement
5903      jump-statement
5904      declaration-statement
5905      try-block  */
5906
5907 static void
5908 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5909 {
5910   tree statement;
5911   cp_token *token;
5912   location_t statement_location;
5913
5914   /* There is no statement yet.  */
5915   statement = NULL_TREE;
5916   /* Peek at the next token.  */
5917   token = cp_lexer_peek_token (parser->lexer);
5918   /* Remember the location of the first token in the statement.  */
5919   statement_location = token->location;
5920   /* If this is a keyword, then that will often determine what kind of
5921      statement we have.  */
5922   if (token->type == CPP_KEYWORD)
5923     {
5924       enum rid keyword = token->keyword;
5925
5926       switch (keyword)
5927         {
5928         case RID_CASE:
5929         case RID_DEFAULT:
5930           statement = cp_parser_labeled_statement (parser,
5931                                                    in_statement_expr);
5932           break;
5933
5934         case RID_IF:
5935         case RID_SWITCH:
5936           statement = cp_parser_selection_statement (parser);
5937           break;
5938
5939         case RID_WHILE:
5940         case RID_DO:
5941         case RID_FOR:
5942           statement = cp_parser_iteration_statement (parser);
5943           break;
5944
5945         case RID_BREAK:
5946         case RID_CONTINUE:
5947         case RID_RETURN:
5948         case RID_GOTO:
5949           statement = cp_parser_jump_statement (parser);
5950           break;
5951
5952         case RID_TRY:
5953           statement = cp_parser_try_block (parser);
5954           break;
5955
5956         default:
5957           /* It might be a keyword like `int' that can start a
5958              declaration-statement.  */
5959           break;
5960         }
5961     }
5962   else if (token->type == CPP_NAME)
5963     {
5964       /* If the next token is a `:', then we are looking at a
5965          labeled-statement.  */
5966       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5967       if (token->type == CPP_COLON)
5968         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5969     }
5970   /* Anything that starts with a `{' must be a compound-statement.  */
5971   else if (token->type == CPP_OPEN_BRACE)
5972     statement = cp_parser_compound_statement (parser, NULL, false);
5973
5974   /* Everything else must be a declaration-statement or an
5975      expression-statement.  Try for the declaration-statement
5976      first, unless we are looking at a `;', in which case we know that
5977      we have an expression-statement.  */
5978   if (!statement)
5979     {
5980       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5981         {
5982           cp_parser_parse_tentatively (parser);
5983           /* Try to parse the declaration-statement.  */
5984           cp_parser_declaration_statement (parser);
5985           /* If that worked, we're done.  */
5986           if (cp_parser_parse_definitely (parser))
5987             return;
5988         }
5989       /* Look for an expression-statement instead.  */
5990       statement = cp_parser_expression_statement (parser, in_statement_expr);
5991     }
5992
5993   /* Set the line number for the statement.  */
5994   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5995     SET_EXPR_LOCATION (statement, statement_location);
5996 }
5997
5998 /* Parse a labeled-statement.
5999
6000    labeled-statement:
6001      identifier : statement
6002      case constant-expression : statement
6003      default : statement
6004
6005    GNU Extension:
6006
6007    labeled-statement:
6008      case constant-expression ... constant-expression : statement
6009
6010    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6011    For an ordinary label, returns a LABEL_EXPR.  */
6012
6013 static tree
6014 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6015 {
6016   cp_token *token;
6017   tree statement = error_mark_node;
6018
6019   /* The next token should be an identifier.  */
6020   token = cp_lexer_peek_token (parser->lexer);
6021   if (token->type != CPP_NAME
6022       && token->type != CPP_KEYWORD)
6023     {
6024       cp_parser_error (parser, "expected labeled-statement");
6025       return error_mark_node;
6026     }
6027
6028   switch (token->keyword)
6029     {
6030     case RID_CASE:
6031       {
6032         tree expr, expr_hi;
6033         cp_token *ellipsis;
6034
6035         /* Consume the `case' token.  */
6036         cp_lexer_consume_token (parser->lexer);
6037         /* Parse the constant-expression.  */
6038         expr = cp_parser_constant_expression (parser,
6039                                               /*allow_non_constant_p=*/false,
6040                                               NULL);
6041
6042         ellipsis = cp_lexer_peek_token (parser->lexer);
6043         if (ellipsis->type == CPP_ELLIPSIS)
6044           {
6045             /* Consume the `...' token.  */
6046             cp_lexer_consume_token (parser->lexer);
6047             expr_hi =
6048               cp_parser_constant_expression (parser,
6049                                              /*allow_non_constant_p=*/false,
6050                                              NULL);
6051             /* We don't need to emit warnings here, as the common code
6052                will do this for us.  */
6053           }
6054         else
6055           expr_hi = NULL_TREE;
6056
6057         if (!parser->in_switch_statement_p)
6058           error ("case label `%E' not within a switch statement", expr);
6059         else
6060           statement = finish_case_label (expr, expr_hi);
6061       }
6062       break;
6063
6064     case RID_DEFAULT:
6065       /* Consume the `default' token.  */
6066       cp_lexer_consume_token (parser->lexer);
6067       if (!parser->in_switch_statement_p)
6068         error ("case label not within a switch statement");
6069       else
6070         statement = finish_case_label (NULL_TREE, NULL_TREE);
6071       break;
6072
6073     default:
6074       /* Anything else must be an ordinary label.  */
6075       statement = finish_label_stmt (cp_parser_identifier (parser));
6076       break;
6077     }
6078
6079   /* Require the `:' token.  */
6080   cp_parser_require (parser, CPP_COLON, "`:'");
6081   /* Parse the labeled statement.  */
6082   cp_parser_statement (parser, in_statement_expr);
6083
6084   /* Return the label, in the case of a `case' or `default' label.  */
6085   return statement;
6086 }
6087
6088 /* Parse an expression-statement.
6089
6090    expression-statement:
6091      expression [opt] ;
6092
6093    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6094    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6095    indicates whether this expression-statement is part of an
6096    expression statement.  */
6097
6098 static tree
6099 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6100 {
6101   tree statement = NULL_TREE;
6102
6103   /* If the next token is a ';', then there is no expression
6104      statement.  */
6105   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6106     statement = cp_parser_expression (parser);
6107
6108   /* Consume the final `;'.  */
6109   cp_parser_consume_semicolon_at_end_of_statement (parser);
6110
6111   if (in_statement_expr
6112       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6113     {
6114       /* This is the final expression statement of a statement
6115          expression.  */
6116       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6117     }
6118   else if (statement)
6119     statement = finish_expr_stmt (statement);
6120   else
6121     finish_stmt ();
6122
6123   return statement;
6124 }
6125
6126 /* Parse a compound-statement.
6127
6128    compound-statement:
6129      { statement-seq [opt] }
6130
6131    Returns a tree representing the statement.  */
6132
6133 static tree
6134 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6135                               bool in_try)
6136 {
6137   tree compound_stmt;
6138
6139   /* Consume the `{'.  */
6140   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6141     return error_mark_node;
6142   /* Begin the compound-statement.  */
6143   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6144   /* Parse an (optional) statement-seq.  */
6145   cp_parser_statement_seq_opt (parser, in_statement_expr);
6146   /* Finish the compound-statement.  */
6147   finish_compound_stmt (compound_stmt);
6148   /* Consume the `}'.  */
6149   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6150
6151   return compound_stmt;
6152 }
6153
6154 /* Parse an (optional) statement-seq.
6155
6156    statement-seq:
6157      statement
6158      statement-seq [opt] statement  */
6159
6160 static void
6161 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6162 {
6163   /* Scan statements until there aren't any more.  */
6164   while (true)
6165     {
6166       /* If we're looking at a `}', then we've run out of statements.  */
6167       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6168           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6169         break;
6170
6171       /* Parse the statement.  */
6172       cp_parser_statement (parser, in_statement_expr);
6173     }
6174 }
6175
6176 /* Parse a selection-statement.
6177
6178    selection-statement:
6179      if ( condition ) statement
6180      if ( condition ) statement else statement
6181      switch ( condition ) statement
6182
6183    Returns the new IF_STMT or SWITCH_STMT.  */
6184
6185 static tree
6186 cp_parser_selection_statement (cp_parser* parser)
6187 {
6188   cp_token *token;
6189   enum rid keyword;
6190
6191   /* Peek at the next token.  */
6192   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6193
6194   /* See what kind of keyword it is.  */
6195   keyword = token->keyword;
6196   switch (keyword)
6197     {
6198     case RID_IF:
6199     case RID_SWITCH:
6200       {
6201         tree statement;
6202         tree condition;
6203
6204         /* Look for the `('.  */
6205         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6206           {
6207             cp_parser_skip_to_end_of_statement (parser);
6208             return error_mark_node;
6209           }
6210
6211         /* Begin the selection-statement.  */
6212         if (keyword == RID_IF)
6213           statement = begin_if_stmt ();
6214         else
6215           statement = begin_switch_stmt ();
6216
6217         /* Parse the condition.  */
6218         condition = cp_parser_condition (parser);
6219         /* Look for the `)'.  */
6220         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6221           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6222                                                  /*consume_paren=*/true);
6223
6224         if (keyword == RID_IF)
6225           {
6226             /* Add the condition.  */
6227             finish_if_stmt_cond (condition, statement);
6228
6229             /* Parse the then-clause.  */
6230             cp_parser_implicitly_scoped_statement (parser);
6231             finish_then_clause (statement);
6232
6233             /* If the next token is `else', parse the else-clause.  */
6234             if (cp_lexer_next_token_is_keyword (parser->lexer,
6235                                                 RID_ELSE))
6236               {
6237                 /* Consume the `else' keyword.  */
6238                 cp_lexer_consume_token (parser->lexer);
6239                 begin_else_clause (statement);
6240                 /* Parse the else-clause.  */
6241                 cp_parser_implicitly_scoped_statement (parser);
6242                 finish_else_clause (statement);
6243               }
6244
6245             /* Now we're all done with the if-statement.  */
6246             finish_if_stmt (statement);
6247           }
6248         else
6249           {
6250             bool in_switch_statement_p;
6251
6252             /* Add the condition.  */
6253             finish_switch_cond (condition, statement);
6254
6255             /* Parse the body of the switch-statement.  */
6256             in_switch_statement_p = parser->in_switch_statement_p;
6257             parser->in_switch_statement_p = true;
6258             cp_parser_implicitly_scoped_statement (parser);
6259             parser->in_switch_statement_p = in_switch_statement_p;
6260
6261             /* Now we're all done with the switch-statement.  */
6262             finish_switch_stmt (statement);
6263           }
6264
6265         return statement;
6266       }
6267       break;
6268
6269     default:
6270       cp_parser_error (parser, "expected selection-statement");
6271       return error_mark_node;
6272     }
6273 }
6274
6275 /* Parse a condition.
6276
6277    condition:
6278      expression
6279      type-specifier-seq declarator = assignment-expression
6280
6281    GNU Extension:
6282
6283    condition:
6284      type-specifier-seq declarator asm-specification [opt]
6285        attributes [opt] = assignment-expression
6286
6287    Returns the expression that should be tested.  */
6288
6289 static tree
6290 cp_parser_condition (cp_parser* parser)
6291 {
6292   cp_decl_specifier_seq type_specifiers;
6293   const char *saved_message;
6294
6295   /* Try the declaration first.  */
6296   cp_parser_parse_tentatively (parser);
6297   /* New types are not allowed in the type-specifier-seq for a
6298      condition.  */
6299   saved_message = parser->type_definition_forbidden_message;
6300   parser->type_definition_forbidden_message
6301     = "types may not be defined in conditions";
6302   /* Parse the type-specifier-seq.  */
6303   cp_parser_type_specifier_seq (parser, &type_specifiers);
6304   /* Restore the saved message.  */
6305   parser->type_definition_forbidden_message = saved_message;
6306   /* If all is well, we might be looking at a declaration.  */
6307   if (!cp_parser_error_occurred (parser))
6308     {
6309       tree decl;
6310       tree asm_specification;
6311       tree attributes;
6312       cp_declarator *declarator;
6313       tree initializer = NULL_TREE;
6314
6315       /* Parse the declarator.  */
6316       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6317                                          /*ctor_dtor_or_conv_p=*/NULL,
6318                                          /*parenthesized_p=*/NULL);
6319       /* Parse the attributes.  */
6320       attributes = cp_parser_attributes_opt (parser);
6321       /* Parse the asm-specification.  */
6322       asm_specification = cp_parser_asm_specification_opt (parser);
6323       /* If the next token is not an `=', then we might still be
6324          looking at an expression.  For example:
6325
6326            if (A(a).x)
6327
6328          looks like a decl-specifier-seq and a declarator -- but then
6329          there is no `=', so this is an expression.  */
6330       cp_parser_require (parser, CPP_EQ, "`='");
6331       /* If we did see an `=', then we are looking at a declaration
6332          for sure.  */
6333       if (cp_parser_parse_definitely (parser))
6334         {
6335           bool pop_p;
6336
6337           /* Create the declaration.  */
6338           decl = start_decl (declarator, &type_specifiers,
6339                              /*initialized_p=*/true,
6340                              attributes, /*prefix_attributes=*/NULL_TREE,
6341                              &pop_p);
6342           /* Parse the assignment-expression.  */
6343           initializer = cp_parser_assignment_expression (parser);
6344
6345           /* Process the initializer.  */
6346           cp_finish_decl (decl,
6347                           initializer,
6348                           asm_specification,
6349                           LOOKUP_ONLYCONVERTING);
6350           if (pop_p)
6351             pop_scope (DECL_CONTEXT (decl));
6352
6353           return convert_from_reference (decl);
6354         }
6355     }
6356   /* If we didn't even get past the declarator successfully, we are
6357      definitely not looking at a declaration.  */
6358   else
6359     cp_parser_abort_tentative_parse (parser);
6360
6361   /* Otherwise, we are looking at an expression.  */
6362   return cp_parser_expression (parser);
6363 }
6364
6365 /* Parse an iteration-statement.
6366
6367    iteration-statement:
6368      while ( condition ) statement
6369      do statement while ( expression ) ;
6370      for ( for-init-statement condition [opt] ; expression [opt] )
6371        statement
6372
6373    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6374
6375 static tree
6376 cp_parser_iteration_statement (cp_parser* parser)
6377 {
6378   cp_token *token;
6379   enum rid keyword;
6380   tree statement;
6381   bool in_iteration_statement_p;
6382
6383
6384   /* Peek at the next token.  */
6385   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6386   if (!token)
6387     return error_mark_node;
6388
6389   /* Remember whether or not we are already within an iteration
6390      statement.  */
6391   in_iteration_statement_p = parser->in_iteration_statement_p;
6392
6393   /* See what kind of keyword it is.  */
6394   keyword = token->keyword;
6395   switch (keyword)
6396     {
6397     case RID_WHILE:
6398       {
6399         tree condition;
6400
6401         /* Begin the while-statement.  */
6402         statement = begin_while_stmt ();
6403         /* Look for the `('.  */
6404         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6405         /* Parse the condition.  */
6406         condition = cp_parser_condition (parser);
6407         finish_while_stmt_cond (condition, statement);
6408         /* Look for the `)'.  */
6409         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6410         /* Parse the dependent statement.  */
6411         parser->in_iteration_statement_p = true;
6412         cp_parser_already_scoped_statement (parser);
6413         parser->in_iteration_statement_p = in_iteration_statement_p;
6414         /* We're done with the while-statement.  */
6415         finish_while_stmt (statement);
6416       }
6417       break;
6418
6419     case RID_DO:
6420       {
6421         tree expression;
6422
6423         /* Begin the do-statement.  */
6424         statement = begin_do_stmt ();
6425         /* Parse the body of the do-statement.  */
6426         parser->in_iteration_statement_p = true;
6427         cp_parser_implicitly_scoped_statement (parser);
6428         parser->in_iteration_statement_p = in_iteration_statement_p;
6429         finish_do_body (statement);
6430         /* Look for the `while' keyword.  */
6431         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6432         /* Look for the `('.  */
6433         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6434         /* Parse the expression.  */
6435         expression = cp_parser_expression (parser);
6436         /* We're done with the do-statement.  */
6437         finish_do_stmt (expression, statement);
6438         /* Look for the `)'.  */
6439         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6440         /* Look for the `;'.  */
6441         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6442       }
6443       break;
6444
6445     case RID_FOR:
6446       {
6447         tree condition = NULL_TREE;
6448         tree expression = NULL_TREE;
6449
6450         /* Begin the for-statement.  */
6451         statement = begin_for_stmt ();
6452         /* Look for the `('.  */
6453         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6454         /* Parse the initialization.  */
6455         cp_parser_for_init_statement (parser);
6456         finish_for_init_stmt (statement);
6457
6458         /* If there's a condition, process it.  */
6459         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6460           condition = cp_parser_condition (parser);
6461         finish_for_cond (condition, statement);
6462         /* Look for the `;'.  */
6463         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6464
6465         /* If there's an expression, process it.  */
6466         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6467           expression = cp_parser_expression (parser);
6468         finish_for_expr (expression, statement);
6469         /* Look for the `)'.  */
6470         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6471
6472         /* Parse the body of the for-statement.  */
6473         parser->in_iteration_statement_p = true;
6474         cp_parser_already_scoped_statement (parser);
6475         parser->in_iteration_statement_p = in_iteration_statement_p;
6476
6477         /* We're done with the for-statement.  */
6478         finish_for_stmt (statement);
6479       }
6480       break;
6481
6482     default:
6483       cp_parser_error (parser, "expected iteration-statement");
6484       statement = error_mark_node;
6485       break;
6486     }
6487
6488   return statement;
6489 }
6490
6491 /* Parse a for-init-statement.
6492
6493    for-init-statement:
6494      expression-statement
6495      simple-declaration  */
6496
6497 static void
6498 cp_parser_for_init_statement (cp_parser* parser)
6499 {
6500   /* If the next token is a `;', then we have an empty
6501      expression-statement.  Grammatically, this is also a
6502      simple-declaration, but an invalid one, because it does not
6503      declare anything.  Therefore, if we did not handle this case
6504      specially, we would issue an error message about an invalid
6505      declaration.  */
6506   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6507     {
6508       /* We're going to speculatively look for a declaration, falling back
6509          to an expression, if necessary.  */
6510       cp_parser_parse_tentatively (parser);
6511       /* Parse the declaration.  */
6512       cp_parser_simple_declaration (parser,
6513                                     /*function_definition_allowed_p=*/false);
6514       /* If the tentative parse failed, then we shall need to look for an
6515          expression-statement.  */
6516       if (cp_parser_parse_definitely (parser))
6517         return;
6518     }
6519
6520   cp_parser_expression_statement (parser, false);
6521 }
6522
6523 /* Parse a jump-statement.
6524
6525    jump-statement:
6526      break ;
6527      continue ;
6528      return expression [opt] ;
6529      goto identifier ;
6530
6531    GNU extension:
6532
6533    jump-statement:
6534      goto * expression ;
6535
6536    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6537
6538 static tree
6539 cp_parser_jump_statement (cp_parser* parser)
6540 {
6541   tree statement = error_mark_node;
6542   cp_token *token;
6543   enum rid keyword;
6544
6545   /* Peek at the next token.  */
6546   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6547   if (!token)
6548     return error_mark_node;
6549
6550   /* See what kind of keyword it is.  */
6551   keyword = token->keyword;
6552   switch (keyword)
6553     {
6554     case RID_BREAK:
6555       if (!parser->in_switch_statement_p
6556           && !parser->in_iteration_statement_p)
6557         {
6558           error ("break statement not within loop or switch");
6559           statement = error_mark_node;
6560         }
6561       else
6562         statement = finish_break_stmt ();
6563       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6564       break;
6565
6566     case RID_CONTINUE:
6567       if (!parser->in_iteration_statement_p)
6568         {
6569           error ("continue statement not within a loop");
6570           statement = error_mark_node;
6571         }
6572       else
6573         statement = finish_continue_stmt ();
6574       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6575       break;
6576
6577     case RID_RETURN:
6578       {
6579         tree expr;
6580
6581         /* If the next token is a `;', then there is no
6582            expression.  */
6583         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6584           expr = cp_parser_expression (parser);
6585         else
6586           expr = NULL_TREE;
6587         /* Build the return-statement.  */
6588         statement = finish_return_stmt (expr);
6589         /* Look for the final `;'.  */
6590         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6591       }
6592       break;
6593
6594     case RID_GOTO:
6595       /* Create the goto-statement.  */
6596       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6597         {
6598           /* Issue a warning about this use of a GNU extension.  */
6599           if (pedantic)
6600             pedwarn ("ISO C++ forbids computed gotos");
6601           /* Consume the '*' token.  */
6602           cp_lexer_consume_token (parser->lexer);
6603           /* Parse the dependent expression.  */
6604           finish_goto_stmt (cp_parser_expression (parser));
6605         }
6606       else
6607         finish_goto_stmt (cp_parser_identifier (parser));
6608       /* Look for the final `;'.  */
6609       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6610       break;
6611
6612     default:
6613       cp_parser_error (parser, "expected jump-statement");
6614       break;
6615     }
6616
6617   return statement;
6618 }
6619
6620 /* Parse a declaration-statement.
6621
6622    declaration-statement:
6623      block-declaration  */
6624
6625 static void
6626 cp_parser_declaration_statement (cp_parser* parser)
6627 {
6628   void *p;
6629
6630   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6631   p = obstack_alloc (&declarator_obstack, 0);
6632
6633  /* Parse the block-declaration.  */
6634   cp_parser_block_declaration (parser, /*statement_p=*/true);
6635
6636   /* Free any declarators allocated.  */
6637   obstack_free (&declarator_obstack, p);
6638
6639   /* Finish off the statement.  */
6640   finish_stmt ();
6641 }
6642
6643 /* Some dependent statements (like `if (cond) statement'), are
6644    implicitly in their own scope.  In other words, if the statement is
6645    a single statement (as opposed to a compound-statement), it is
6646    none-the-less treated as if it were enclosed in braces.  Any
6647    declarations appearing in the dependent statement are out of scope
6648    after control passes that point.  This function parses a statement,
6649    but ensures that is in its own scope, even if it is not a
6650    compound-statement.
6651
6652    Returns the new statement.  */
6653
6654 static tree
6655 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6656 {
6657   tree statement;
6658
6659   /* If the token is not a `{', then we must take special action.  */
6660   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6661     {
6662       /* Create a compound-statement.  */
6663       statement = begin_compound_stmt (0);
6664       /* Parse the dependent-statement.  */
6665       cp_parser_statement (parser, false);
6666       /* Finish the dummy compound-statement.  */
6667       finish_compound_stmt (statement);
6668     }
6669   /* Otherwise, we simply parse the statement directly.  */
6670   else
6671     statement = cp_parser_compound_statement (parser, NULL, false);
6672
6673   /* Return the statement.  */
6674   return statement;
6675 }
6676
6677 /* For some dependent statements (like `while (cond) statement'), we
6678    have already created a scope.  Therefore, even if the dependent
6679    statement is a compound-statement, we do not want to create another
6680    scope.  */
6681
6682 static void
6683 cp_parser_already_scoped_statement (cp_parser* parser)
6684 {
6685   /* If the token is a `{', then we must take special action.  */
6686   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6687     cp_parser_statement (parser, false);
6688   else
6689     {
6690       /* Avoid calling cp_parser_compound_statement, so that we
6691          don't create a new scope.  Do everything else by hand.  */
6692       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6693       cp_parser_statement_seq_opt (parser, false);
6694       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6695     }
6696 }
6697
6698 /* Declarations [gram.dcl.dcl] */
6699
6700 /* Parse an optional declaration-sequence.
6701
6702    declaration-seq:
6703      declaration
6704      declaration-seq declaration  */
6705
6706 static void
6707 cp_parser_declaration_seq_opt (cp_parser* parser)
6708 {
6709   while (true)
6710     {
6711       cp_token *token;
6712
6713       token = cp_lexer_peek_token (parser->lexer);
6714
6715       if (token->type == CPP_CLOSE_BRACE
6716           || token->type == CPP_EOF)
6717         break;
6718
6719       if (token->type == CPP_SEMICOLON)
6720         {
6721           /* A declaration consisting of a single semicolon is
6722              invalid.  Allow it unless we're being pedantic.  */
6723           if (pedantic && !in_system_header)
6724             pedwarn ("extra `;'");
6725           cp_lexer_consume_token (parser->lexer);
6726           continue;
6727         }
6728
6729       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6730          parser to enter or exit implicit `extern "C"' blocks.  */
6731       while (pending_lang_change > 0)
6732         {
6733           push_lang_context (lang_name_c);
6734           --pending_lang_change;
6735         }
6736       while (pending_lang_change < 0)
6737         {
6738           pop_lang_context ();
6739           ++pending_lang_change;
6740         }
6741
6742       /* Parse the declaration itself.  */
6743       cp_parser_declaration (parser);
6744     }
6745 }
6746
6747 /* Parse a declaration.
6748
6749    declaration:
6750      block-declaration
6751      function-definition
6752      template-declaration
6753      explicit-instantiation
6754      explicit-specialization
6755      linkage-specification
6756      namespace-definition
6757
6758    GNU extension:
6759
6760    declaration:
6761       __extension__ declaration */
6762
6763 static void
6764 cp_parser_declaration (cp_parser* parser)
6765 {
6766   cp_token token1;
6767   cp_token token2;
6768   int saved_pedantic;
6769   void *p;
6770
6771   /* Set this here since we can be called after
6772      pushing the linkage specification.  */
6773   c_lex_string_translate = 1;
6774
6775   /* Check for the `__extension__' keyword.  */
6776   if (cp_parser_extension_opt (parser, &saved_pedantic))
6777     {
6778       /* Parse the qualified declaration.  */
6779       cp_parser_declaration (parser);
6780       /* Restore the PEDANTIC flag.  */
6781       pedantic = saved_pedantic;
6782
6783       return;
6784     }
6785
6786   /* Try to figure out what kind of declaration is present.  */
6787   token1 = *cp_lexer_peek_token (parser->lexer);
6788
6789   /* Don't translate the CPP_STRING in extern "C".  */
6790   if (token1.keyword == RID_EXTERN)
6791     c_lex_string_translate = 0;
6792
6793   if (token1.type != CPP_EOF)
6794     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6795
6796   c_lex_string_translate = 1;
6797
6798   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6799   p = obstack_alloc (&declarator_obstack, 0);
6800
6801   /* If the next token is `extern' and the following token is a string
6802      literal, then we have a linkage specification.  */
6803   if (token1.keyword == RID_EXTERN
6804       && cp_parser_is_string_literal (&token2))
6805     cp_parser_linkage_specification (parser);
6806   /* If the next token is `template', then we have either a template
6807      declaration, an explicit instantiation, or an explicit
6808      specialization.  */
6809   else if (token1.keyword == RID_TEMPLATE)
6810     {
6811       /* `template <>' indicates a template specialization.  */
6812       if (token2.type == CPP_LESS
6813           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6814         cp_parser_explicit_specialization (parser);
6815       /* `template <' indicates a template declaration.  */
6816       else if (token2.type == CPP_LESS)
6817         cp_parser_template_declaration (parser, /*member_p=*/false);
6818       /* Anything else must be an explicit instantiation.  */
6819       else
6820         cp_parser_explicit_instantiation (parser);
6821     }
6822   /* If the next token is `export', then we have a template
6823      declaration.  */
6824   else if (token1.keyword == RID_EXPORT)
6825     cp_parser_template_declaration (parser, /*member_p=*/false);
6826   /* If the next token is `extern', 'static' or 'inline' and the one
6827      after that is `template', we have a GNU extended explicit
6828      instantiation directive.  */
6829   else if (cp_parser_allow_gnu_extensions_p (parser)
6830            && (token1.keyword == RID_EXTERN
6831                || token1.keyword == RID_STATIC
6832                || token1.keyword == RID_INLINE)
6833            && token2.keyword == RID_TEMPLATE)
6834     cp_parser_explicit_instantiation (parser);
6835   /* If the next token is `namespace', check for a named or unnamed
6836      namespace definition.  */
6837   else if (token1.keyword == RID_NAMESPACE
6838            && (/* A named namespace definition.  */
6839                (token2.type == CPP_NAME
6840                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6841                     == CPP_OPEN_BRACE))
6842                /* An unnamed namespace definition.  */
6843                || token2.type == CPP_OPEN_BRACE))
6844     cp_parser_namespace_definition (parser);
6845   /* We must have either a block declaration or a function
6846      definition.  */
6847   else
6848     /* Try to parse a block-declaration, or a function-definition.  */
6849     cp_parser_block_declaration (parser, /*statement_p=*/false);
6850
6851   /* Free any declarators allocated.  */
6852   obstack_free (&declarator_obstack, p);
6853 }
6854
6855 /* Parse a block-declaration.
6856
6857    block-declaration:
6858      simple-declaration
6859      asm-definition
6860      namespace-alias-definition
6861      using-declaration
6862      using-directive
6863
6864    GNU Extension:
6865
6866    block-declaration:
6867      __extension__ block-declaration
6868      label-declaration
6869
6870    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6871    part of a declaration-statement.  */
6872
6873 static void
6874 cp_parser_block_declaration (cp_parser *parser,
6875                              bool      statement_p)
6876 {
6877   cp_token *token1;
6878   int saved_pedantic;
6879
6880   /* Check for the `__extension__' keyword.  */
6881   if (cp_parser_extension_opt (parser, &saved_pedantic))
6882     {
6883       /* Parse the qualified declaration.  */
6884       cp_parser_block_declaration (parser, statement_p);
6885       /* Restore the PEDANTIC flag.  */
6886       pedantic = saved_pedantic;
6887
6888       return;
6889     }
6890
6891   /* Peek at the next token to figure out which kind of declaration is
6892      present.  */
6893   token1 = cp_lexer_peek_token (parser->lexer);
6894
6895   /* If the next keyword is `asm', we have an asm-definition.  */
6896   if (token1->keyword == RID_ASM)
6897     {
6898       if (statement_p)
6899         cp_parser_commit_to_tentative_parse (parser);
6900       cp_parser_asm_definition (parser);
6901     }
6902   /* If the next keyword is `namespace', we have a
6903      namespace-alias-definition.  */
6904   else if (token1->keyword == RID_NAMESPACE)
6905     cp_parser_namespace_alias_definition (parser);
6906   /* If the next keyword is `using', we have either a
6907      using-declaration or a using-directive.  */
6908   else if (token1->keyword == RID_USING)
6909     {
6910       cp_token *token2;
6911
6912       if (statement_p)
6913         cp_parser_commit_to_tentative_parse (parser);
6914       /* If the token after `using' is `namespace', then we have a
6915          using-directive.  */
6916       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6917       if (token2->keyword == RID_NAMESPACE)
6918         cp_parser_using_directive (parser);
6919       /* Otherwise, it's a using-declaration.  */
6920       else
6921         cp_parser_using_declaration (parser);
6922     }
6923   /* If the next keyword is `__label__' we have a label declaration.  */
6924   else if (token1->keyword == RID_LABEL)
6925     {
6926       if (statement_p)
6927         cp_parser_commit_to_tentative_parse (parser);
6928       cp_parser_label_declaration (parser);
6929     }
6930   /* Anything else must be a simple-declaration.  */
6931   else
6932     cp_parser_simple_declaration (parser, !statement_p);
6933 }
6934
6935 /* Parse a simple-declaration.
6936
6937    simple-declaration:
6938      decl-specifier-seq [opt] init-declarator-list [opt] ;
6939
6940    init-declarator-list:
6941      init-declarator
6942      init-declarator-list , init-declarator
6943
6944    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6945    function-definition as a simple-declaration.  */
6946
6947 static void
6948 cp_parser_simple_declaration (cp_parser* parser,
6949                               bool function_definition_allowed_p)
6950 {
6951   cp_decl_specifier_seq decl_specifiers;
6952   int declares_class_or_enum;
6953   bool saw_declarator;
6954
6955   /* Defer access checks until we know what is being declared; the
6956      checks for names appearing in the decl-specifier-seq should be
6957      done as if we were in the scope of the thing being declared.  */
6958   push_deferring_access_checks (dk_deferred);
6959
6960   /* Parse the decl-specifier-seq.  We have to keep track of whether
6961      or not the decl-specifier-seq declares a named class or
6962      enumeration type, since that is the only case in which the
6963      init-declarator-list is allowed to be empty.
6964
6965      [dcl.dcl]
6966
6967      In a simple-declaration, the optional init-declarator-list can be
6968      omitted only when declaring a class or enumeration, that is when
6969      the decl-specifier-seq contains either a class-specifier, an
6970      elaborated-type-specifier, or an enum-specifier.  */
6971   cp_parser_decl_specifier_seq (parser,
6972                                 CP_PARSER_FLAGS_OPTIONAL,
6973                                 &decl_specifiers,
6974                                 &declares_class_or_enum);
6975   /* We no longer need to defer access checks.  */
6976   stop_deferring_access_checks ();
6977
6978   /* In a block scope, a valid declaration must always have a
6979      decl-specifier-seq.  By not trying to parse declarators, we can
6980      resolve the declaration/expression ambiguity more quickly.  */
6981   if (!function_definition_allowed_p
6982       && !decl_specifiers.any_specifiers_p)
6983     {
6984       cp_parser_error (parser, "expected declaration");
6985       goto done;
6986     }
6987
6988   /* If the next two tokens are both identifiers, the code is
6989      erroneous. The usual cause of this situation is code like:
6990
6991        T t;
6992
6993      where "T" should name a type -- but does not.  */
6994   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6995     {
6996       /* If parsing tentatively, we should commit; we really are
6997          looking at a declaration.  */
6998       cp_parser_commit_to_tentative_parse (parser);
6999       /* Give up.  */
7000       goto done;
7001     }
7002
7003   /* Keep going until we hit the `;' at the end of the simple
7004      declaration.  */
7005   saw_declarator = false;
7006   while (cp_lexer_next_token_is_not (parser->lexer,
7007                                      CPP_SEMICOLON))
7008     {
7009       cp_token *token;
7010       bool function_definition_p;
7011       tree decl;
7012
7013       saw_declarator = true;
7014       /* Parse the init-declarator.  */
7015       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7016                                         function_definition_allowed_p,
7017                                         /*member_p=*/false,
7018                                         declares_class_or_enum,
7019                                         &function_definition_p);
7020       /* If an error occurred while parsing tentatively, exit quickly.
7021          (That usually happens when in the body of a function; each
7022          statement is treated as a declaration-statement until proven
7023          otherwise.)  */
7024       if (cp_parser_error_occurred (parser))
7025         goto done;
7026       /* Handle function definitions specially.  */
7027       if (function_definition_p)
7028         {
7029           /* If the next token is a `,', then we are probably
7030              processing something like:
7031
7032                void f() {}, *p;
7033
7034              which is erroneous.  */
7035           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7036             error ("mixing declarations and function-definitions is forbidden");
7037           /* Otherwise, we're done with the list of declarators.  */
7038           else
7039             {
7040               pop_deferring_access_checks ();
7041               return;
7042             }
7043         }
7044       /* The next token should be either a `,' or a `;'.  */
7045       token = cp_lexer_peek_token (parser->lexer);
7046       /* If it's a `,', there are more declarators to come.  */
7047       if (token->type == CPP_COMMA)
7048         cp_lexer_consume_token (parser->lexer);
7049       /* If it's a `;', we are done.  */
7050       else if (token->type == CPP_SEMICOLON)
7051         break;
7052       /* Anything else is an error.  */
7053       else
7054         {
7055           cp_parser_error (parser, "expected `,' or `;'");
7056           /* Skip tokens until we reach the end of the statement.  */
7057           cp_parser_skip_to_end_of_statement (parser);
7058           /* If the next token is now a `;', consume it.  */
7059           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7060             cp_lexer_consume_token (parser->lexer);
7061           goto done;
7062         }
7063       /* After the first time around, a function-definition is not
7064          allowed -- even if it was OK at first.  For example:
7065
7066            int i, f() {}
7067
7068          is not valid.  */
7069       function_definition_allowed_p = false;
7070     }
7071
7072   /* Issue an error message if no declarators are present, and the
7073      decl-specifier-seq does not itself declare a class or
7074      enumeration.  */
7075   if (!saw_declarator)
7076     {
7077       if (cp_parser_declares_only_class_p (parser))
7078         shadow_tag (&decl_specifiers);
7079       /* Perform any deferred access checks.  */
7080       perform_deferred_access_checks ();
7081     }
7082
7083   /* Consume the `;'.  */
7084   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7085
7086  done:
7087   pop_deferring_access_checks ();
7088 }
7089
7090 /* Parse a decl-specifier-seq.
7091
7092    decl-specifier-seq:
7093      decl-specifier-seq [opt] decl-specifier
7094
7095    decl-specifier:
7096      storage-class-specifier
7097      type-specifier
7098      function-specifier
7099      friend
7100      typedef
7101
7102    GNU Extension:
7103
7104    decl-specifier:
7105      attributes
7106
7107    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7108
7109    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7110    appears, and the entity that will be a friend is not going to be a
7111    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
7112    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
7113    friendship is granted might not be a class.
7114
7115    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7116    flags:
7117
7118      1: one of the decl-specifiers is an elaborated-type-specifier
7119         (i.e., a type declaration)
7120      2: one of the decl-specifiers is an enum-specifier or a
7121         class-specifier (i.e., a type definition)
7122
7123    */
7124
7125 static void
7126 cp_parser_decl_specifier_seq (cp_parser* parser,
7127                               cp_parser_flags flags,
7128                               cp_decl_specifier_seq *decl_specs,
7129                               int* declares_class_or_enum)
7130 {
7131   bool constructor_possible_p = !parser->in_declarator_p;
7132
7133   /* Clear DECL_SPECS.  */
7134   clear_decl_specs (decl_specs);
7135
7136   /* Assume no class or enumeration type is declared.  */
7137   *declares_class_or_enum = 0;
7138
7139   /* Keep reading specifiers until there are no more to read.  */
7140   while (true)
7141     {
7142       bool constructor_p;
7143       bool found_decl_spec;
7144       cp_token *token;
7145
7146       /* Peek at the next token.  */
7147       token = cp_lexer_peek_token (parser->lexer);
7148       /* Handle attributes.  */
7149       if (token->keyword == RID_ATTRIBUTE)
7150         {
7151           /* Parse the attributes.  */
7152           decl_specs->attributes
7153             = chainon (decl_specs->attributes,
7154                        cp_parser_attributes_opt (parser));
7155           continue;
7156         }
7157       /* Assume we will find a decl-specifier keyword.  */
7158       found_decl_spec = true;
7159       /* If the next token is an appropriate keyword, we can simply
7160          add it to the list.  */
7161       switch (token->keyword)
7162         {
7163           /* decl-specifier:
7164                friend  */
7165         case RID_FRIEND:
7166           if (decl_specs->specs[(int) ds_friend]++)
7167             error ("duplicate `friend'");
7168           /* Consume the token.  */
7169           cp_lexer_consume_token (parser->lexer);
7170           break;
7171
7172           /* function-specifier:
7173                inline
7174                virtual
7175                explicit  */
7176         case RID_INLINE:
7177         case RID_VIRTUAL:
7178         case RID_EXPLICIT:
7179           cp_parser_function_specifier_opt (parser, decl_specs);
7180           break;
7181
7182           /* decl-specifier:
7183                typedef  */
7184         case RID_TYPEDEF:
7185           ++decl_specs->specs[(int) ds_typedef];
7186           /* Consume the token.  */
7187           cp_lexer_consume_token (parser->lexer);
7188           /* A constructor declarator cannot appear in a typedef.  */
7189           constructor_possible_p = false;
7190           /* The "typedef" keyword can only occur in a declaration; we
7191              may as well commit at this point.  */
7192           cp_parser_commit_to_tentative_parse (parser);
7193           break;
7194
7195           /* storage-class-specifier:
7196                auto
7197                register
7198                static
7199                extern
7200                mutable
7201
7202              GNU Extension:
7203                thread  */
7204         case RID_AUTO:
7205           /* Consume the token.  */
7206           cp_lexer_consume_token (parser->lexer);
7207           cp_parser_set_storage_class (decl_specs, sc_auto);
7208           break;
7209         case RID_REGISTER:
7210           /* Consume the token.  */
7211           cp_lexer_consume_token (parser->lexer);
7212           cp_parser_set_storage_class (decl_specs, sc_register);
7213           break;
7214         case RID_STATIC:
7215           /* Consume the token.  */
7216           cp_lexer_consume_token (parser->lexer);
7217           if (decl_specs->specs[(int) ds_thread])
7218             {
7219               error ("`__thread' before `static'");
7220               decl_specs->specs[(int) ds_thread] = 0;
7221             }
7222           cp_parser_set_storage_class (decl_specs, sc_static);
7223           break;
7224         case RID_EXTERN:
7225           /* Consume the token.  */
7226           cp_lexer_consume_token (parser->lexer);
7227           if (decl_specs->specs[(int) ds_thread])
7228             {
7229               error ("`__thread' before `extern'");
7230               decl_specs->specs[(int) ds_thread] = 0;
7231             }
7232           cp_parser_set_storage_class (decl_specs, sc_extern);
7233           break;
7234         case RID_MUTABLE:
7235           /* Consume the token.  */
7236           cp_lexer_consume_token (parser->lexer);
7237           cp_parser_set_storage_class (decl_specs, sc_mutable);
7238           break;
7239         case RID_THREAD:
7240           /* Consume the token.  */
7241           cp_lexer_consume_token (parser->lexer);
7242           ++decl_specs->specs[(int) ds_thread];
7243           break;
7244
7245         default:
7246           /* We did not yet find a decl-specifier yet.  */
7247           found_decl_spec = false;
7248           break;
7249         }
7250
7251       /* Constructors are a special case.  The `S' in `S()' is not a
7252          decl-specifier; it is the beginning of the declarator.  */
7253       constructor_p
7254         = (!found_decl_spec
7255            && constructor_possible_p
7256            && (cp_parser_constructor_declarator_p
7257                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7258
7259       /* If we don't have a DECL_SPEC yet, then we must be looking at
7260          a type-specifier.  */
7261       if (!found_decl_spec && !constructor_p)
7262         {
7263           int decl_spec_declares_class_or_enum;
7264           bool is_cv_qualifier;
7265           tree type_spec;
7266
7267           type_spec
7268             = cp_parser_type_specifier (parser, flags,
7269                                         decl_specs,
7270                                         /*is_declaration=*/true,
7271                                         &decl_spec_declares_class_or_enum,
7272                                         &is_cv_qualifier);
7273
7274           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7275
7276           /* If this type-specifier referenced a user-defined type
7277              (a typedef, class-name, etc.), then we can't allow any
7278              more such type-specifiers henceforth.
7279
7280              [dcl.spec]
7281
7282              The longest sequence of decl-specifiers that could
7283              possibly be a type name is taken as the
7284              decl-specifier-seq of a declaration.  The sequence shall
7285              be self-consistent as described below.
7286
7287              [dcl.type]
7288
7289              As a general rule, at most one type-specifier is allowed
7290              in the complete decl-specifier-seq of a declaration.  The
7291              only exceptions are the following:
7292
7293              -- const or volatile can be combined with any other
7294                 type-specifier.
7295
7296              -- signed or unsigned can be combined with char, long,
7297                 short, or int.
7298
7299              -- ..
7300
7301              Example:
7302
7303                typedef char* Pc;
7304                void g (const int Pc);
7305
7306              Here, Pc is *not* part of the decl-specifier seq; it's
7307              the declarator.  Therefore, once we see a type-specifier
7308              (other than a cv-qualifier), we forbid any additional
7309              user-defined types.  We *do* still allow things like `int
7310              int' to be considered a decl-specifier-seq, and issue the
7311              error message later.  */
7312           if (type_spec && !is_cv_qualifier)
7313             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7314           /* A constructor declarator cannot follow a type-specifier.  */
7315           if (type_spec)
7316             {
7317               constructor_possible_p = false;
7318               found_decl_spec = true;
7319             }
7320         }
7321
7322       /* If we still do not have a DECL_SPEC, then there are no more
7323          decl-specifiers.  */
7324       if (!found_decl_spec)
7325         break;
7326
7327       decl_specs->any_specifiers_p = true;
7328       /* After we see one decl-specifier, further decl-specifiers are
7329          always optional.  */
7330       flags |= CP_PARSER_FLAGS_OPTIONAL;
7331     }
7332
7333   /* Don't allow a friend specifier with a class definition.  */
7334   if (decl_specs->specs[(int) ds_friend] != 0
7335       && (*declares_class_or_enum & 2))
7336     error ("class definition may not be declared a friend");
7337 }
7338
7339 /* Parse an (optional) storage-class-specifier.
7340
7341    storage-class-specifier:
7342      auto
7343      register
7344      static
7345      extern
7346      mutable
7347
7348    GNU Extension:
7349
7350    storage-class-specifier:
7351      thread
7352
7353    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7354
7355 static tree
7356 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7357 {
7358   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7359     {
7360     case RID_AUTO:
7361     case RID_REGISTER:
7362     case RID_STATIC:
7363     case RID_EXTERN:
7364     case RID_MUTABLE:
7365     case RID_THREAD:
7366       /* Consume the token.  */
7367       return cp_lexer_consume_token (parser->lexer)->value;
7368
7369     default:
7370       return NULL_TREE;
7371     }
7372 }
7373
7374 /* Parse an (optional) function-specifier.
7375
7376    function-specifier:
7377      inline
7378      virtual
7379      explicit
7380
7381    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7382    Updates DECL_SPECS, if it is non-NULL.  */
7383
7384 static tree
7385 cp_parser_function_specifier_opt (cp_parser* parser,
7386                                   cp_decl_specifier_seq *decl_specs)
7387 {
7388   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7389     {
7390     case RID_INLINE:
7391       if (decl_specs)
7392         ++decl_specs->specs[(int) ds_inline];
7393       break;
7394
7395     case RID_VIRTUAL:
7396       if (decl_specs)
7397         ++decl_specs->specs[(int) ds_virtual];
7398       break;
7399
7400     case RID_EXPLICIT:
7401       if (decl_specs)
7402         ++decl_specs->specs[(int) ds_explicit];
7403       break;
7404
7405     default:
7406       return NULL_TREE;
7407     }
7408
7409   /* Consume the token.  */
7410   return cp_lexer_consume_token (parser->lexer)->value;
7411 }
7412
7413 /* Parse a linkage-specification.
7414
7415    linkage-specification:
7416      extern string-literal { declaration-seq [opt] }
7417      extern string-literal declaration  */
7418
7419 static void
7420 cp_parser_linkage_specification (cp_parser* parser)
7421 {
7422   cp_token *token;
7423   tree linkage;
7424
7425   /* Look for the `extern' keyword.  */
7426   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7427
7428   /* Peek at the next token.  */
7429   token = cp_lexer_peek_token (parser->lexer);
7430   /* If it's not a string-literal, then there's a problem.  */
7431   if (!cp_parser_is_string_literal (token))
7432     {
7433       cp_parser_error (parser, "expected language-name");
7434       return;
7435     }
7436   /* Consume the token.  */
7437   cp_lexer_consume_token (parser->lexer);
7438
7439   /* Transform the literal into an identifier.  If the literal is a
7440      wide-character string, or contains embedded NULs, then we can't
7441      handle it as the user wants.  */
7442   if (token->type == CPP_WSTRING
7443       || (strlen (TREE_STRING_POINTER (token->value))
7444           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7445     {
7446       cp_parser_error (parser, "invalid linkage-specification");
7447       /* Assume C++ linkage.  */
7448       linkage = get_identifier ("c++");
7449     }
7450   /* If the string is chained to another string, take the latter,
7451      that's the untranslated string.  */
7452   else if (TREE_CHAIN (token->value))
7453     linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7454   /* If it's a simple string constant, things are easier.  */
7455   else
7456     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7457
7458   /* We're now using the new linkage.  */
7459   push_lang_context (linkage);
7460
7461   /* If the next token is a `{', then we're using the first
7462      production.  */
7463   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7464     {
7465       /* Consume the `{' token.  */
7466       cp_lexer_consume_token (parser->lexer);
7467       /* Parse the declarations.  */
7468       cp_parser_declaration_seq_opt (parser);
7469       /* Look for the closing `}'.  */
7470       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7471     }
7472   /* Otherwise, there's just one declaration.  */
7473   else
7474     {
7475       bool saved_in_unbraced_linkage_specification_p;
7476
7477       saved_in_unbraced_linkage_specification_p
7478         = parser->in_unbraced_linkage_specification_p;
7479       parser->in_unbraced_linkage_specification_p = true;
7480       have_extern_spec = true;
7481       cp_parser_declaration (parser);
7482       have_extern_spec = false;
7483       parser->in_unbraced_linkage_specification_p
7484         = saved_in_unbraced_linkage_specification_p;
7485     }
7486
7487   /* We're done with the linkage-specification.  */
7488   pop_lang_context ();
7489 }
7490
7491 /* Special member functions [gram.special] */
7492
7493 /* Parse a conversion-function-id.
7494
7495    conversion-function-id:
7496      operator conversion-type-id
7497
7498    Returns an IDENTIFIER_NODE representing the operator.  */
7499
7500 static tree
7501 cp_parser_conversion_function_id (cp_parser* parser)
7502 {
7503   tree type;
7504   tree saved_scope;
7505   tree saved_qualifying_scope;
7506   tree saved_object_scope;
7507   bool pop_p = false;
7508
7509   /* Look for the `operator' token.  */
7510   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7511     return error_mark_node;
7512   /* When we parse the conversion-type-id, the current scope will be
7513      reset.  However, we need that information in able to look up the
7514      conversion function later, so we save it here.  */
7515   saved_scope = parser->scope;
7516   saved_qualifying_scope = parser->qualifying_scope;
7517   saved_object_scope = parser->object_scope;
7518   /* We must enter the scope of the class so that the names of
7519      entities declared within the class are available in the
7520      conversion-type-id.  For example, consider:
7521
7522        struct S {
7523          typedef int I;
7524          operator I();
7525        };
7526
7527        S::operator I() { ... }
7528
7529      In order to see that `I' is a type-name in the definition, we
7530      must be in the scope of `S'.  */
7531   if (saved_scope)
7532     pop_p = push_scope (saved_scope);
7533   /* Parse the conversion-type-id.  */
7534   type = cp_parser_conversion_type_id (parser);
7535   /* Leave the scope of the class, if any.  */
7536   if (pop_p)
7537     pop_scope (saved_scope);
7538   /* Restore the saved scope.  */
7539   parser->scope = saved_scope;
7540   parser->qualifying_scope = saved_qualifying_scope;
7541   parser->object_scope = saved_object_scope;
7542   /* If the TYPE is invalid, indicate failure.  */
7543   if (type == error_mark_node)
7544     return error_mark_node;
7545   return mangle_conv_op_name_for_type (type);
7546 }
7547
7548 /* Parse a conversion-type-id:
7549
7550    conversion-type-id:
7551      type-specifier-seq conversion-declarator [opt]
7552
7553    Returns the TYPE specified.  */
7554
7555 static tree
7556 cp_parser_conversion_type_id (cp_parser* parser)
7557 {
7558   tree attributes;
7559   cp_decl_specifier_seq type_specifiers;
7560   cp_declarator *declarator;
7561
7562   /* Parse the attributes.  */
7563   attributes = cp_parser_attributes_opt (parser);
7564   /* Parse the type-specifiers.  */
7565   cp_parser_type_specifier_seq (parser, &type_specifiers);
7566   /* If that didn't work, stop.  */
7567   if (type_specifiers.type == error_mark_node)
7568     return error_mark_node;
7569   /* Parse the conversion-declarator.  */
7570   declarator = cp_parser_conversion_declarator_opt (parser);
7571
7572   return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7573                          /*initialized=*/0, &attributes);
7574 }
7575
7576 /* Parse an (optional) conversion-declarator.
7577
7578    conversion-declarator:
7579      ptr-operator conversion-declarator [opt]
7580
7581    */
7582
7583 static cp_declarator *
7584 cp_parser_conversion_declarator_opt (cp_parser* parser)
7585 {
7586   enum tree_code code;
7587   tree class_type;
7588   cp_cv_quals cv_quals;
7589
7590   /* We don't know if there's a ptr-operator next, or not.  */
7591   cp_parser_parse_tentatively (parser);
7592   /* Try the ptr-operator.  */
7593   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7594   /* If it worked, look for more conversion-declarators.  */
7595   if (cp_parser_parse_definitely (parser))
7596     {
7597       cp_declarator *declarator;
7598
7599       /* Parse another optional declarator.  */
7600       declarator = cp_parser_conversion_declarator_opt (parser);
7601
7602       /* Create the representation of the declarator.  */
7603       if (class_type)
7604         declarator = make_ptrmem_declarator (cv_quals, class_type,
7605                                              declarator);
7606       else if (code == INDIRECT_REF)
7607         declarator = make_pointer_declarator (cv_quals, declarator);
7608       else
7609         declarator = make_reference_declarator (cv_quals, declarator);
7610
7611       return declarator;
7612    }
7613
7614   return NULL;
7615 }
7616
7617 /* Parse an (optional) ctor-initializer.
7618
7619    ctor-initializer:
7620      : mem-initializer-list
7621
7622    Returns TRUE iff the ctor-initializer was actually present.  */
7623
7624 static bool
7625 cp_parser_ctor_initializer_opt (cp_parser* parser)
7626 {
7627   /* If the next token is not a `:', then there is no
7628      ctor-initializer.  */
7629   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7630     {
7631       /* Do default initialization of any bases and members.  */
7632       if (DECL_CONSTRUCTOR_P (current_function_decl))
7633         finish_mem_initializers (NULL_TREE);
7634
7635       return false;
7636     }
7637
7638   /* Consume the `:' token.  */
7639   cp_lexer_consume_token (parser->lexer);
7640   /* And the mem-initializer-list.  */
7641   cp_parser_mem_initializer_list (parser);
7642
7643   return true;
7644 }
7645
7646 /* Parse a mem-initializer-list.
7647
7648    mem-initializer-list:
7649      mem-initializer
7650      mem-initializer , mem-initializer-list  */
7651
7652 static void
7653 cp_parser_mem_initializer_list (cp_parser* parser)
7654 {
7655   tree mem_initializer_list = NULL_TREE;
7656
7657   /* Let the semantic analysis code know that we are starting the
7658      mem-initializer-list.  */
7659   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7660     error ("only constructors take base initializers");
7661
7662   /* Loop through the list.  */
7663   while (true)
7664     {
7665       tree mem_initializer;
7666
7667       /* Parse the mem-initializer.  */
7668       mem_initializer = cp_parser_mem_initializer (parser);
7669       /* Add it to the list, unless it was erroneous.  */
7670       if (mem_initializer)
7671         {
7672           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7673           mem_initializer_list = mem_initializer;
7674         }
7675       /* If the next token is not a `,', we're done.  */
7676       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7677         break;
7678       /* Consume the `,' token.  */
7679       cp_lexer_consume_token (parser->lexer);
7680     }
7681
7682   /* Perform semantic analysis.  */
7683   if (DECL_CONSTRUCTOR_P (current_function_decl))
7684     finish_mem_initializers (mem_initializer_list);
7685 }
7686
7687 /* Parse a mem-initializer.
7688
7689    mem-initializer:
7690      mem-initializer-id ( expression-list [opt] )
7691
7692    GNU extension:
7693
7694    mem-initializer:
7695      ( expression-list [opt] )
7696
7697    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7698    class) or FIELD_DECL (for a non-static data member) to initialize;
7699    the TREE_VALUE is the expression-list.  */
7700
7701 static tree
7702 cp_parser_mem_initializer (cp_parser* parser)
7703 {
7704   tree mem_initializer_id;
7705   tree expression_list;
7706   tree member;
7707
7708   /* Find out what is being initialized.  */
7709   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7710     {
7711       pedwarn ("anachronistic old-style base class initializer");
7712       mem_initializer_id = NULL_TREE;
7713     }
7714   else
7715     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7716   member = expand_member_init (mem_initializer_id);
7717   if (member && !DECL_P (member))
7718     in_base_initializer = 1;
7719
7720   expression_list
7721     = cp_parser_parenthesized_expression_list (parser, false,
7722                                                /*non_constant_p=*/NULL);
7723   if (!expression_list)
7724     expression_list = void_type_node;
7725
7726   in_base_initializer = 0;
7727
7728   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7729 }
7730
7731 /* Parse a mem-initializer-id.
7732
7733    mem-initializer-id:
7734      :: [opt] nested-name-specifier [opt] class-name
7735      identifier
7736
7737    Returns a TYPE indicating the class to be initializer for the first
7738    production.  Returns an IDENTIFIER_NODE indicating the data member
7739    to be initialized for the second production.  */
7740
7741 static tree
7742 cp_parser_mem_initializer_id (cp_parser* parser)
7743 {
7744   bool global_scope_p;
7745   bool nested_name_specifier_p;
7746   bool template_p = false;
7747   tree id;
7748
7749   /* `typename' is not allowed in this context ([temp.res]).  */
7750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7751     {
7752       error ("keyword `typename' not allowed in this context (a qualified "
7753              "member initializer is implicitly a type)");
7754       cp_lexer_consume_token (parser->lexer);
7755     }
7756   /* Look for the optional `::' operator.  */
7757   global_scope_p
7758     = (cp_parser_global_scope_opt (parser,
7759                                    /*current_scope_valid_p=*/false)
7760        != NULL_TREE);
7761   /* Look for the optional nested-name-specifier.  The simplest way to
7762      implement:
7763
7764        [temp.res]
7765
7766        The keyword `typename' is not permitted in a base-specifier or
7767        mem-initializer; in these contexts a qualified name that
7768        depends on a template-parameter is implicitly assumed to be a
7769        type name.
7770
7771      is to assume that we have seen the `typename' keyword at this
7772      point.  */
7773   nested_name_specifier_p
7774     = (cp_parser_nested_name_specifier_opt (parser,
7775                                             /*typename_keyword_p=*/true,
7776                                             /*check_dependency_p=*/true,
7777                                             /*type_p=*/true,
7778                                             /*is_declaration=*/true)
7779        != NULL_TREE);
7780   if (nested_name_specifier_p)
7781     template_p = cp_parser_optional_template_keyword (parser);
7782   /* If there is a `::' operator or a nested-name-specifier, then we
7783      are definitely looking for a class-name.  */
7784   if (global_scope_p || nested_name_specifier_p)
7785     return cp_parser_class_name (parser,
7786                                  /*typename_keyword_p=*/true,
7787                                  /*template_keyword_p=*/template_p,
7788                                  /*type_p=*/false,
7789                                  /*check_dependency_p=*/true,
7790                                  /*class_head_p=*/false,
7791                                  /*is_declaration=*/true);
7792   /* Otherwise, we could also be looking for an ordinary identifier.  */
7793   cp_parser_parse_tentatively (parser);
7794   /* Try a class-name.  */
7795   id = cp_parser_class_name (parser,
7796                              /*typename_keyword_p=*/true,
7797                              /*template_keyword_p=*/false,
7798                              /*type_p=*/false,
7799                              /*check_dependency_p=*/true,
7800                              /*class_head_p=*/false,
7801                              /*is_declaration=*/true);
7802   /* If we found one, we're done.  */
7803   if (cp_parser_parse_definitely (parser))
7804     return id;
7805   /* Otherwise, look for an ordinary identifier.  */
7806   return cp_parser_identifier (parser);
7807 }
7808
7809 /* Overloading [gram.over] */
7810
7811 /* Parse an operator-function-id.
7812
7813    operator-function-id:
7814      operator operator
7815
7816    Returns an IDENTIFIER_NODE for the operator which is a
7817    human-readable spelling of the identifier, e.g., `operator +'.  */
7818
7819 static tree
7820 cp_parser_operator_function_id (cp_parser* parser)
7821 {
7822   /* Look for the `operator' keyword.  */
7823   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7824     return error_mark_node;
7825   /* And then the name of the operator itself.  */
7826   return cp_parser_operator (parser);
7827 }
7828
7829 /* Parse an operator.
7830
7831    operator:
7832      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7833      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7834      || ++ -- , ->* -> () []
7835
7836    GNU Extensions:
7837
7838    operator:
7839      <? >? <?= >?=
7840
7841    Returns an IDENTIFIER_NODE for the operator which is a
7842    human-readable spelling of the identifier, e.g., `operator +'.  */
7843
7844 static tree
7845 cp_parser_operator (cp_parser* parser)
7846 {
7847   tree id = NULL_TREE;
7848   cp_token *token;
7849
7850   /* Peek at the next token.  */
7851   token = cp_lexer_peek_token (parser->lexer);
7852   /* Figure out which operator we have.  */
7853   switch (token->type)
7854     {
7855     case CPP_KEYWORD:
7856       {
7857         enum tree_code op;
7858
7859         /* The keyword should be either `new' or `delete'.  */
7860         if (token->keyword == RID_NEW)
7861           op = NEW_EXPR;
7862         else if (token->keyword == RID_DELETE)
7863           op = DELETE_EXPR;
7864         else
7865           break;
7866
7867         /* Consume the `new' or `delete' token.  */
7868         cp_lexer_consume_token (parser->lexer);
7869
7870         /* Peek at the next token.  */
7871         token = cp_lexer_peek_token (parser->lexer);
7872         /* If it's a `[' token then this is the array variant of the
7873            operator.  */
7874         if (token->type == CPP_OPEN_SQUARE)
7875           {
7876             /* Consume the `[' token.  */
7877             cp_lexer_consume_token (parser->lexer);
7878             /* Look for the `]' token.  */
7879             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7880             id = ansi_opname (op == NEW_EXPR
7881                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7882           }
7883         /* Otherwise, we have the non-array variant.  */
7884         else
7885           id = ansi_opname (op);
7886
7887         return id;
7888       }
7889
7890     case CPP_PLUS:
7891       id = ansi_opname (PLUS_EXPR);
7892       break;
7893
7894     case CPP_MINUS:
7895       id = ansi_opname (MINUS_EXPR);
7896       break;
7897
7898     case CPP_MULT:
7899       id = ansi_opname (MULT_EXPR);
7900       break;
7901
7902     case CPP_DIV:
7903       id = ansi_opname (TRUNC_DIV_EXPR);
7904       break;
7905
7906     case CPP_MOD:
7907       id = ansi_opname (TRUNC_MOD_EXPR);
7908       break;
7909
7910     case CPP_XOR:
7911       id = ansi_opname (BIT_XOR_EXPR);
7912       break;
7913
7914     case CPP_AND:
7915       id = ansi_opname (BIT_AND_EXPR);
7916       break;
7917
7918     case CPP_OR:
7919       id = ansi_opname (BIT_IOR_EXPR);
7920       break;
7921
7922     case CPP_COMPL:
7923       id = ansi_opname (BIT_NOT_EXPR);
7924       break;
7925
7926     case CPP_NOT:
7927       id = ansi_opname (TRUTH_NOT_EXPR);
7928       break;
7929
7930     case CPP_EQ:
7931       id = ansi_assopname (NOP_EXPR);
7932       break;
7933
7934     case CPP_LESS:
7935       id = ansi_opname (LT_EXPR);
7936       break;
7937
7938     case CPP_GREATER:
7939       id = ansi_opname (GT_EXPR);
7940       break;
7941
7942     case CPP_PLUS_EQ:
7943       id = ansi_assopname (PLUS_EXPR);
7944       break;
7945
7946     case CPP_MINUS_EQ:
7947       id = ansi_assopname (MINUS_EXPR);
7948       break;
7949
7950     case CPP_MULT_EQ:
7951       id = ansi_assopname (MULT_EXPR);
7952       break;
7953
7954     case CPP_DIV_EQ:
7955       id = ansi_assopname (TRUNC_DIV_EXPR);
7956       break;
7957
7958     case CPP_MOD_EQ:
7959       id = ansi_assopname (TRUNC_MOD_EXPR);
7960       break;
7961
7962     case CPP_XOR_EQ:
7963       id = ansi_assopname (BIT_XOR_EXPR);
7964       break;
7965
7966     case CPP_AND_EQ:
7967       id = ansi_assopname (BIT_AND_EXPR);
7968       break;
7969
7970     case CPP_OR_EQ:
7971       id = ansi_assopname (BIT_IOR_EXPR);
7972       break;
7973
7974     case CPP_LSHIFT:
7975       id = ansi_opname (LSHIFT_EXPR);
7976       break;
7977
7978     case CPP_RSHIFT:
7979       id = ansi_opname (RSHIFT_EXPR);
7980       break;
7981
7982     case CPP_LSHIFT_EQ:
7983       id = ansi_assopname (LSHIFT_EXPR);
7984       break;
7985
7986     case CPP_RSHIFT_EQ:
7987       id = ansi_assopname (RSHIFT_EXPR);
7988       break;
7989
7990     case CPP_EQ_EQ:
7991       id = ansi_opname (EQ_EXPR);
7992       break;
7993
7994     case CPP_NOT_EQ:
7995       id = ansi_opname (NE_EXPR);
7996       break;
7997
7998     case CPP_LESS_EQ:
7999       id = ansi_opname (LE_EXPR);
8000       break;
8001
8002     case CPP_GREATER_EQ:
8003       id = ansi_opname (GE_EXPR);
8004       break;
8005
8006     case CPP_AND_AND:
8007       id = ansi_opname (TRUTH_ANDIF_EXPR);
8008       break;
8009
8010     case CPP_OR_OR:
8011       id = ansi_opname (TRUTH_ORIF_EXPR);
8012       break;
8013
8014     case CPP_PLUS_PLUS:
8015       id = ansi_opname (POSTINCREMENT_EXPR);
8016       break;
8017
8018     case CPP_MINUS_MINUS:
8019       id = ansi_opname (PREDECREMENT_EXPR);
8020       break;
8021
8022     case CPP_COMMA:
8023       id = ansi_opname (COMPOUND_EXPR);
8024       break;
8025
8026     case CPP_DEREF_STAR:
8027       id = ansi_opname (MEMBER_REF);
8028       break;
8029
8030     case CPP_DEREF:
8031       id = ansi_opname (COMPONENT_REF);
8032       break;
8033
8034     case CPP_OPEN_PAREN:
8035       /* Consume the `('.  */
8036       cp_lexer_consume_token (parser->lexer);
8037       /* Look for the matching `)'.  */
8038       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8039       return ansi_opname (CALL_EXPR);
8040
8041     case CPP_OPEN_SQUARE:
8042       /* Consume the `['.  */
8043       cp_lexer_consume_token (parser->lexer);
8044       /* Look for the matching `]'.  */
8045       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8046       return ansi_opname (ARRAY_REF);
8047
8048       /* Extensions.  */
8049     case CPP_MIN:
8050       id = ansi_opname (MIN_EXPR);
8051       break;
8052
8053     case CPP_MAX:
8054       id = ansi_opname (MAX_EXPR);
8055       break;
8056
8057     case CPP_MIN_EQ:
8058       id = ansi_assopname (MIN_EXPR);
8059       break;
8060
8061     case CPP_MAX_EQ:
8062       id = ansi_assopname (MAX_EXPR);
8063       break;
8064
8065     default:
8066       /* Anything else is an error.  */
8067       break;
8068     }
8069
8070   /* If we have selected an identifier, we need to consume the
8071      operator token.  */
8072   if (id)
8073     cp_lexer_consume_token (parser->lexer);
8074   /* Otherwise, no valid operator name was present.  */
8075   else
8076     {
8077       cp_parser_error (parser, "expected operator");
8078       id = error_mark_node;
8079     }
8080
8081   return id;
8082 }
8083
8084 /* Parse a template-declaration.
8085
8086    template-declaration:
8087      export [opt] template < template-parameter-list > declaration
8088
8089    If MEMBER_P is TRUE, this template-declaration occurs within a
8090    class-specifier.
8091
8092    The grammar rule given by the standard isn't correct.  What
8093    is really meant is:
8094
8095    template-declaration:
8096      export [opt] template-parameter-list-seq
8097        decl-specifier-seq [opt] init-declarator [opt] ;
8098      export [opt] template-parameter-list-seq
8099        function-definition
8100
8101    template-parameter-list-seq:
8102      template-parameter-list-seq [opt]
8103      template < template-parameter-list >  */
8104
8105 static void
8106 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8107 {
8108   /* Check for `export'.  */
8109   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8110     {
8111       /* Consume the `export' token.  */
8112       cp_lexer_consume_token (parser->lexer);
8113       /* Warn that we do not support `export'.  */
8114       warning ("keyword `export' not implemented, and will be ignored");
8115     }
8116
8117   cp_parser_template_declaration_after_export (parser, member_p);
8118 }
8119
8120 /* Parse a template-parameter-list.
8121
8122    template-parameter-list:
8123      template-parameter
8124      template-parameter-list , template-parameter
8125
8126    Returns a TREE_LIST.  Each node represents a template parameter.
8127    The nodes are connected via their TREE_CHAINs.  */
8128
8129 static tree
8130 cp_parser_template_parameter_list (cp_parser* parser)
8131 {
8132   tree parameter_list = NULL_TREE;
8133
8134   while (true)
8135     {
8136       tree parameter;
8137       cp_token *token;
8138       bool is_non_type;
8139
8140       /* Parse the template-parameter.  */
8141       parameter = cp_parser_template_parameter (parser, &is_non_type);
8142       /* Add it to the list.  */
8143       parameter_list = process_template_parm (parameter_list,
8144                                               parameter,
8145                                               is_non_type);
8146       /* Peek at the next token.  */
8147       token = cp_lexer_peek_token (parser->lexer);
8148       /* If it's not a `,', we're done.  */
8149       if (token->type != CPP_COMMA)
8150         break;
8151       /* Otherwise, consume the `,' token.  */
8152       cp_lexer_consume_token (parser->lexer);
8153     }
8154
8155   return parameter_list;
8156 }
8157
8158 /* Parse a template-parameter.
8159
8160    template-parameter:
8161      type-parameter
8162      parameter-declaration
8163
8164    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8165    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8166    true iff this parameter is a non-type parameter.  */
8167
8168 static tree
8169 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8170 {
8171   cp_token *token;
8172   cp_parameter_declarator *parameter_declarator;
8173
8174   /* Assume it is a type parameter or a template parameter.  */
8175   *is_non_type = false;
8176   /* Peek at the next token.  */
8177   token = cp_lexer_peek_token (parser->lexer);
8178   /* If it is `class' or `template', we have a type-parameter.  */
8179   if (token->keyword == RID_TEMPLATE)
8180     return cp_parser_type_parameter (parser);
8181   /* If it is `class' or `typename' we do not know yet whether it is a
8182      type parameter or a non-type parameter.  Consider:
8183
8184        template <typename T, typename T::X X> ...
8185
8186      or:
8187
8188        template <class C, class D*> ...
8189
8190      Here, the first parameter is a type parameter, and the second is
8191      a non-type parameter.  We can tell by looking at the token after
8192      the identifier -- if it is a `,', `=', or `>' then we have a type
8193      parameter.  */
8194   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8195     {
8196       /* Peek at the token after `class' or `typename'.  */
8197       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8198       /* If it's an identifier, skip it.  */
8199       if (token->type == CPP_NAME)
8200         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8201       /* Now, see if the token looks like the end of a template
8202          parameter.  */
8203       if (token->type == CPP_COMMA
8204           || token->type == CPP_EQ
8205           || token->type == CPP_GREATER)
8206         return cp_parser_type_parameter (parser);
8207     }
8208
8209   /* Otherwise, it is a non-type parameter.
8210
8211      [temp.param]
8212
8213      When parsing a default template-argument for a non-type
8214      template-parameter, the first non-nested `>' is taken as the end
8215      of the template parameter-list rather than a greater-than
8216      operator.  */
8217   *is_non_type = true;
8218   parameter_declarator
8219      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8220                                         /*parenthesized_p=*/NULL);
8221   return (build_tree_list
8222           (parameter_declarator->default_argument,
8223            grokdeclarator (parameter_declarator->declarator,
8224                            &parameter_declarator->decl_specifiers,
8225                            PARM, /*initialized=*/0,
8226                            /*attrlist=*/NULL)));
8227 }
8228
8229 /* Parse a type-parameter.
8230
8231    type-parameter:
8232      class identifier [opt]
8233      class identifier [opt] = type-id
8234      typename identifier [opt]
8235      typename identifier [opt] = type-id
8236      template < template-parameter-list > class identifier [opt]
8237      template < template-parameter-list > class identifier [opt]
8238        = id-expression
8239
8240    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8241    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8242    the declaration of the parameter.  */
8243
8244 static tree
8245 cp_parser_type_parameter (cp_parser* parser)
8246 {
8247   cp_token *token;
8248   tree parameter;
8249
8250   /* Look for a keyword to tell us what kind of parameter this is.  */
8251   token = cp_parser_require (parser, CPP_KEYWORD,
8252                              "`class', `typename', or `template'");
8253   if (!token)
8254     return error_mark_node;
8255
8256   switch (token->keyword)
8257     {
8258     case RID_CLASS:
8259     case RID_TYPENAME:
8260       {
8261         tree identifier;
8262         tree default_argument;
8263
8264         /* If the next token is an identifier, then it names the
8265            parameter.  */
8266         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8267           identifier = cp_parser_identifier (parser);
8268         else
8269           identifier = NULL_TREE;
8270
8271         /* Create the parameter.  */
8272         parameter = finish_template_type_parm (class_type_node, identifier);
8273
8274         /* If the next token is an `=', we have a default argument.  */
8275         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8276           {
8277             /* Consume the `=' token.  */
8278             cp_lexer_consume_token (parser->lexer);
8279             /* Parse the default-argument.  */
8280             default_argument = cp_parser_type_id (parser);
8281           }
8282         else
8283           default_argument = NULL_TREE;
8284
8285         /* Create the combined representation of the parameter and the
8286            default argument.  */
8287         parameter = build_tree_list (default_argument, parameter);
8288       }
8289       break;
8290
8291     case RID_TEMPLATE:
8292       {
8293         tree parameter_list;
8294         tree identifier;
8295         tree default_argument;
8296
8297         /* Look for the `<'.  */
8298         cp_parser_require (parser, CPP_LESS, "`<'");
8299         /* Parse the template-parameter-list.  */
8300         begin_template_parm_list ();
8301         parameter_list
8302           = cp_parser_template_parameter_list (parser);
8303         parameter_list = end_template_parm_list (parameter_list);
8304         /* Look for the `>'.  */
8305         cp_parser_require (parser, CPP_GREATER, "`>'");
8306         /* Look for the `class' keyword.  */
8307         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8308         /* If the next token is an `=', then there is a
8309            default-argument.  If the next token is a `>', we are at
8310            the end of the parameter-list.  If the next token is a `,',
8311            then we are at the end of this parameter.  */
8312         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8313             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8314             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8315           identifier = cp_parser_identifier (parser);
8316         else
8317           identifier = NULL_TREE;
8318         /* Create the template parameter.  */
8319         parameter = finish_template_template_parm (class_type_node,
8320                                                    identifier);
8321
8322         /* If the next token is an `=', then there is a
8323            default-argument.  */
8324         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8325           {
8326             bool is_template;
8327
8328             /* Consume the `='.  */
8329             cp_lexer_consume_token (parser->lexer);
8330             /* Parse the id-expression.  */
8331             default_argument
8332               = cp_parser_id_expression (parser,
8333                                          /*template_keyword_p=*/false,
8334                                          /*check_dependency_p=*/true,
8335                                          /*template_p=*/&is_template,
8336                                          /*declarator_p=*/false);
8337             if (TREE_CODE (default_argument) == TYPE_DECL)
8338               /* If the id-expression was a template-id that refers to
8339                  a template-class, we already have the declaration here,
8340                  so no further lookup is needed.  */
8341                  ;
8342             else
8343               /* Look up the name.  */
8344               default_argument
8345                 = cp_parser_lookup_name (parser, default_argument,
8346                                         /*is_type=*/false,
8347                                         /*is_template=*/is_template,
8348                                         /*is_namespace=*/false,
8349                                         /*check_dependency=*/true,
8350                                         /*ambiguous_p=*/NULL);
8351             /* See if the default argument is valid.  */
8352             default_argument
8353               = check_template_template_default_arg (default_argument);
8354           }
8355         else
8356           default_argument = NULL_TREE;
8357
8358         /* Create the combined representation of the parameter and the
8359            default argument.  */
8360         parameter =  build_tree_list (default_argument, parameter);
8361       }
8362       break;
8363
8364     default:
8365       /* Anything else is an error.  */
8366       cp_parser_error (parser,
8367                        "expected `class', `typename', or `template'");
8368       parameter = error_mark_node;
8369     }
8370
8371   return parameter;
8372 }
8373
8374 /* Parse a template-id.
8375
8376    template-id:
8377      template-name < template-argument-list [opt] >
8378
8379    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8380    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8381    returned.  Otherwise, if the template-name names a function, or set
8382    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8383    names a class, returns a TYPE_DECL for the specialization.
8384
8385    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8386    uninstantiated templates.  */
8387
8388 static tree
8389 cp_parser_template_id (cp_parser *parser,
8390                        bool template_keyword_p,
8391                        bool check_dependency_p,
8392                        bool is_declaration)
8393 {
8394   tree template;
8395   tree arguments;
8396   tree template_id;
8397   ptrdiff_t start_of_id;
8398   tree access_check = NULL_TREE;
8399   cp_token *next_token, *next_token_2;
8400   bool is_identifier;
8401
8402   /* If the next token corresponds to a template-id, there is no need
8403      to reparse it.  */
8404   next_token = cp_lexer_peek_token (parser->lexer);
8405   if (next_token->type == CPP_TEMPLATE_ID)
8406     {
8407       tree value;
8408       tree check;
8409
8410       /* Get the stored value.  */
8411       value = cp_lexer_consume_token (parser->lexer)->value;
8412       /* Perform any access checks that were deferred.  */
8413       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8414         perform_or_defer_access_check (TREE_PURPOSE (check),
8415                                        TREE_VALUE (check));
8416       /* Return the stored value.  */
8417       return TREE_VALUE (value);
8418     }
8419
8420   /* Avoid performing name lookup if there is no possibility of
8421      finding a template-id.  */
8422   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8423       || (next_token->type == CPP_NAME
8424           && !cp_parser_nth_token_starts_template_argument_list_p
8425                (parser, 2)))
8426     {
8427       cp_parser_error (parser, "expected template-id");
8428       return error_mark_node;
8429     }
8430
8431   /* Remember where the template-id starts.  */
8432   if (cp_parser_parsing_tentatively (parser)
8433       && !cp_parser_committed_to_tentative_parse (parser))
8434     {
8435       next_token = cp_lexer_peek_token (parser->lexer);
8436       start_of_id = cp_lexer_token_difference (parser->lexer,
8437                                                parser->lexer->first_token,
8438                                                next_token);
8439     }
8440   else
8441     start_of_id = -1;
8442
8443   push_deferring_access_checks (dk_deferred);
8444
8445   /* Parse the template-name.  */
8446   is_identifier = false;
8447   template = cp_parser_template_name (parser, template_keyword_p,
8448                                       check_dependency_p,
8449                                       is_declaration,
8450                                       &is_identifier);
8451   if (template == error_mark_node || is_identifier)
8452     {
8453       pop_deferring_access_checks ();
8454       return template;
8455     }
8456
8457   /* If we find the sequence `[:' after a template-name, it's probably
8458      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8459      parse correctly the argument list.  */
8460   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8461   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8462   if (next_token->type == CPP_OPEN_SQUARE
8463       && next_token->flags & DIGRAPH
8464       && next_token_2->type == CPP_COLON
8465       && !(next_token_2->flags & PREV_WHITE))
8466     {
8467       cp_parser_parse_tentatively (parser);
8468       /* Change `:' into `::'.  */
8469       next_token_2->type = CPP_SCOPE;
8470       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8471          CPP_LESS.  */
8472       cp_lexer_consume_token (parser->lexer);
8473       /* Parse the arguments.  */
8474       arguments = cp_parser_enclosed_template_argument_list (parser);
8475       if (!cp_parser_parse_definitely (parser))
8476         {
8477           /* If we couldn't parse an argument list, then we revert our changes
8478              and return simply an error. Maybe this is not a template-id
8479              after all.  */
8480           next_token_2->type = CPP_COLON;
8481           cp_parser_error (parser, "expected `<'");
8482           pop_deferring_access_checks ();
8483           return error_mark_node;
8484         }
8485       /* Otherwise, emit an error about the invalid digraph, but continue
8486          parsing because we got our argument list.  */
8487       pedwarn ("`<::' cannot begin a template-argument list");
8488       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8489               "between `<' and `::'");
8490       if (!flag_permissive)
8491         {
8492           static bool hint;
8493           if (!hint)
8494             {
8495               inform ("(if you use `-fpermissive' G++ will accept your code)");
8496               hint = true;
8497             }
8498         }
8499     }
8500   else
8501     {
8502       /* Look for the `<' that starts the template-argument-list.  */
8503       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8504         {
8505           pop_deferring_access_checks ();
8506           return error_mark_node;
8507         }
8508       /* Parse the arguments.  */
8509       arguments = cp_parser_enclosed_template_argument_list (parser);
8510     }
8511
8512   /* Build a representation of the specialization.  */
8513   if (TREE_CODE (template) == IDENTIFIER_NODE)
8514     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8515   else if (DECL_CLASS_TEMPLATE_P (template)
8516            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8517     template_id
8518       = finish_template_type (template, arguments,
8519                               cp_lexer_next_token_is (parser->lexer,
8520                                                       CPP_SCOPE));
8521   else
8522     {
8523       /* If it's not a class-template or a template-template, it should be
8524          a function-template.  */
8525       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8526                    || TREE_CODE (template) == OVERLOAD
8527                    || BASELINK_P (template)));
8528
8529       template_id = lookup_template_function (template, arguments);
8530     }
8531
8532   /* Retrieve any deferred checks.  Do not pop this access checks yet
8533      so the memory will not be reclaimed during token replacing below.  */
8534   access_check = get_deferred_access_checks ();
8535
8536   /* If parsing tentatively, replace the sequence of tokens that makes
8537      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8538      should we re-parse the token stream, we will not have to repeat
8539      the effort required to do the parse, nor will we issue duplicate
8540      error messages about problems during instantiation of the
8541      template.  */
8542   if (start_of_id >= 0)
8543     {
8544       cp_token *token;
8545
8546       /* Find the token that corresponds to the start of the
8547          template-id.  */
8548       token = cp_lexer_advance_token (parser->lexer,
8549                                       parser->lexer->first_token,
8550                                       start_of_id);
8551
8552       /* Reset the contents of the START_OF_ID token.  */
8553       token->type = CPP_TEMPLATE_ID;
8554       token->value = build_tree_list (access_check, template_id);
8555       token->keyword = RID_MAX;
8556       /* Purge all subsequent tokens.  */
8557       cp_lexer_purge_tokens_after (parser->lexer, token);
8558     }
8559
8560   pop_deferring_access_checks ();
8561   return template_id;
8562 }
8563
8564 /* Parse a template-name.
8565
8566    template-name:
8567      identifier
8568
8569    The standard should actually say:
8570
8571    template-name:
8572      identifier
8573      operator-function-id
8574
8575    A defect report has been filed about this issue.
8576
8577    A conversion-function-id cannot be a template name because they cannot
8578    be part of a template-id. In fact, looking at this code:
8579
8580    a.operator K<int>()
8581
8582    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8583    It is impossible to call a templated conversion-function-id with an
8584    explicit argument list, since the only allowed template parameter is
8585    the type to which it is converting.
8586
8587    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8588    `template' keyword, in a construction like:
8589
8590      T::template f<3>()
8591
8592    In that case `f' is taken to be a template-name, even though there
8593    is no way of knowing for sure.
8594
8595    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8596    name refers to a set of overloaded functions, at least one of which
8597    is a template, or an IDENTIFIER_NODE with the name of the template,
8598    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8599    names are looked up inside uninstantiated templates.  */
8600
8601 static tree
8602 cp_parser_template_name (cp_parser* parser,
8603                          bool template_keyword_p,
8604                          bool check_dependency_p,
8605                          bool is_declaration,
8606                          bool *is_identifier)
8607 {
8608   tree identifier;
8609   tree decl;
8610   tree fns;
8611
8612   /* If the next token is `operator', then we have either an
8613      operator-function-id or a conversion-function-id.  */
8614   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8615     {
8616       /* We don't know whether we're looking at an
8617          operator-function-id or a conversion-function-id.  */
8618       cp_parser_parse_tentatively (parser);
8619       /* Try an operator-function-id.  */
8620       identifier = cp_parser_operator_function_id (parser);
8621       /* If that didn't work, try a conversion-function-id.  */
8622       if (!cp_parser_parse_definitely (parser))
8623         {
8624           cp_parser_error (parser, "expected template-name");
8625           return error_mark_node;
8626         }
8627     }
8628   /* Look for the identifier.  */
8629   else
8630     identifier = cp_parser_identifier (parser);
8631
8632   /* If we didn't find an identifier, we don't have a template-id.  */
8633   if (identifier == error_mark_node)
8634     return error_mark_node;
8635
8636   /* If the name immediately followed the `template' keyword, then it
8637      is a template-name.  However, if the next token is not `<', then
8638      we do not treat it as a template-name, since it is not being used
8639      as part of a template-id.  This enables us to handle constructs
8640      like:
8641
8642        template <typename T> struct S { S(); };
8643        template <typename T> S<T>::S();
8644
8645      correctly.  We would treat `S' as a template -- if it were `S<T>'
8646      -- but we do not if there is no `<'.  */
8647
8648   if (processing_template_decl
8649       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8650     {
8651       /* In a declaration, in a dependent context, we pretend that the
8652          "template" keyword was present in order to improve error
8653          recovery.  For example, given:
8654
8655            template <typename T> void f(T::X<int>);
8656
8657          we want to treat "X<int>" as a template-id.  */
8658       if (is_declaration
8659           && !template_keyword_p
8660           && parser->scope && TYPE_P (parser->scope)
8661           && dependent_type_p (parser->scope)
8662           /* Do not do this for dtors (or ctors), since they never
8663              need the template keyword before their name.  */
8664           && !constructor_name_p (identifier, parser->scope))
8665         {
8666           ptrdiff_t start;
8667           cp_token* token;
8668           /* Explain what went wrong.  */
8669           error ("non-template `%D' used as template", identifier);
8670           inform ("use `%T::template %D' to indicate that it is a template",
8671                   parser->scope, identifier);
8672           /* If parsing tentatively, find the location of the "<"
8673              token.  */
8674           if (cp_parser_parsing_tentatively (parser)
8675               && !cp_parser_committed_to_tentative_parse (parser))
8676             {
8677               cp_parser_simulate_error (parser);
8678               token = cp_lexer_peek_token (parser->lexer);
8679               token = cp_lexer_prev_token (parser->lexer, token);
8680               start = cp_lexer_token_difference (parser->lexer,
8681                                                  parser->lexer->first_token,
8682                                                  token);
8683             }
8684           else
8685             start = -1;
8686           /* Parse the template arguments so that we can issue error
8687              messages about them.  */
8688           cp_lexer_consume_token (parser->lexer);
8689           cp_parser_enclosed_template_argument_list (parser);
8690           /* Skip tokens until we find a good place from which to
8691              continue parsing.  */
8692           cp_parser_skip_to_closing_parenthesis (parser,
8693                                                  /*recovering=*/true,
8694                                                  /*or_comma=*/true,
8695                                                  /*consume_paren=*/false);
8696           /* If parsing tentatively, permanently remove the
8697              template argument list.  That will prevent duplicate
8698              error messages from being issued about the missing
8699              "template" keyword.  */
8700           if (start >= 0)
8701             {
8702               token = cp_lexer_advance_token (parser->lexer,
8703                                               parser->lexer->first_token,
8704                                               start);
8705               cp_lexer_purge_tokens_after (parser->lexer, token);
8706             }
8707           if (is_identifier)
8708             *is_identifier = true;
8709           return identifier;
8710         }
8711
8712       /* If the "template" keyword is present, then there is generally
8713          no point in doing name-lookup, so we just return IDENTIFIER.
8714          But, if the qualifying scope is non-dependent then we can
8715          (and must) do name-lookup normally.  */
8716       if (template_keyword_p
8717           && (!parser->scope
8718               || (TYPE_P (parser->scope)
8719                   && dependent_type_p (parser->scope))))
8720         return identifier;
8721     }
8722
8723   /* Look up the name.  */
8724   decl = cp_parser_lookup_name (parser, identifier,
8725                                 /*is_type=*/false,
8726                                 /*is_template=*/false,
8727                                 /*is_namespace=*/false,
8728                                 check_dependency_p,
8729                                 /*ambiguous_p=*/NULL);
8730   decl = maybe_get_template_decl_from_type_decl (decl);
8731
8732   /* If DECL is a template, then the name was a template-name.  */
8733   if (TREE_CODE (decl) == TEMPLATE_DECL)
8734     ;
8735   else
8736     {
8737       /* The standard does not explicitly indicate whether a name that
8738          names a set of overloaded declarations, some of which are
8739          templates, is a template-name.  However, such a name should
8740          be a template-name; otherwise, there is no way to form a
8741          template-id for the overloaded templates.  */
8742       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8743       if (TREE_CODE (fns) == OVERLOAD)
8744         {
8745           tree fn;
8746
8747           for (fn = fns; fn; fn = OVL_NEXT (fn))
8748             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8749               break;
8750         }
8751       else
8752         {
8753           /* Otherwise, the name does not name a template.  */
8754           cp_parser_error (parser, "expected template-name");
8755           return error_mark_node;
8756         }
8757     }
8758
8759   /* If DECL is dependent, and refers to a function, then just return
8760      its name; we will look it up again during template instantiation.  */
8761   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8762     {
8763       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8764       if (TYPE_P (scope) && dependent_type_p (scope))
8765         return identifier;
8766     }
8767
8768   return decl;
8769 }
8770
8771 /* Parse a template-argument-list.
8772
8773    template-argument-list:
8774      template-argument
8775      template-argument-list , template-argument
8776
8777    Returns a TREE_VEC containing the arguments.  */
8778
8779 static tree
8780 cp_parser_template_argument_list (cp_parser* parser)
8781 {
8782   tree fixed_args[10];
8783   unsigned n_args = 0;
8784   unsigned alloced = 10;
8785   tree *arg_ary = fixed_args;
8786   tree vec;
8787   bool saved_in_template_argument_list_p;
8788
8789   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8790   parser->in_template_argument_list_p = true;
8791   do
8792     {
8793       tree argument;
8794
8795       if (n_args)
8796         /* Consume the comma.  */
8797         cp_lexer_consume_token (parser->lexer);
8798
8799       /* Parse the template-argument.  */
8800       argument = cp_parser_template_argument (parser);
8801       if (n_args == alloced)
8802         {
8803           alloced *= 2;
8804
8805           if (arg_ary == fixed_args)
8806             {
8807               arg_ary = xmalloc (sizeof (tree) * alloced);
8808               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8809             }
8810           else
8811             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8812         }
8813       arg_ary[n_args++] = argument;
8814     }
8815   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8816
8817   vec = make_tree_vec (n_args);
8818
8819   while (n_args--)
8820     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8821
8822   if (arg_ary != fixed_args)
8823     free (arg_ary);
8824   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8825   return vec;
8826 }
8827
8828 /* Parse a template-argument.
8829
8830    template-argument:
8831      assignment-expression
8832      type-id
8833      id-expression
8834
8835    The representation is that of an assignment-expression, type-id, or
8836    id-expression -- except that the qualified id-expression is
8837    evaluated, so that the value returned is either a DECL or an
8838    OVERLOAD.
8839
8840    Although the standard says "assignment-expression", it forbids
8841    throw-expressions or assignments in the template argument.
8842    Therefore, we use "conditional-expression" instead.  */
8843
8844 static tree
8845 cp_parser_template_argument (cp_parser* parser)
8846 {
8847   tree argument;
8848   bool template_p;
8849   bool address_p;
8850   bool maybe_type_id = false;
8851   cp_token *token;
8852   cp_id_kind idk;
8853   tree qualifying_class;
8854
8855   /* There's really no way to know what we're looking at, so we just
8856      try each alternative in order.
8857
8858        [temp.arg]
8859
8860        In a template-argument, an ambiguity between a type-id and an
8861        expression is resolved to a type-id, regardless of the form of
8862        the corresponding template-parameter.
8863
8864      Therefore, we try a type-id first.  */
8865   cp_parser_parse_tentatively (parser);
8866   argument = cp_parser_type_id (parser);
8867   /* If there was no error parsing the type-id but the next token is a '>>',
8868      we probably found a typo for '> >'. But there are type-id which are
8869      also valid expressions. For instance:
8870
8871      struct X { int operator >> (int); };
8872      template <int V> struct Foo {};
8873      Foo<X () >> 5> r;
8874
8875      Here 'X()' is a valid type-id of a function type, but the user just
8876      wanted to write the expression "X() >> 5". Thus, we remember that we
8877      found a valid type-id, but we still try to parse the argument as an
8878      expression to see what happens.  */
8879   if (!cp_parser_error_occurred (parser)
8880       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8881     {
8882       maybe_type_id = true;
8883       cp_parser_abort_tentative_parse (parser);
8884     }
8885   else
8886     {
8887       /* If the next token isn't a `,' or a `>', then this argument wasn't
8888       really finished. This means that the argument is not a valid
8889       type-id.  */
8890       if (!cp_parser_next_token_ends_template_argument_p (parser))
8891         cp_parser_error (parser, "expected template-argument");
8892       /* If that worked, we're done.  */
8893       if (cp_parser_parse_definitely (parser))
8894         return argument;
8895     }
8896   /* We're still not sure what the argument will be.  */
8897   cp_parser_parse_tentatively (parser);
8898   /* Try a template.  */
8899   argument = cp_parser_id_expression (parser,
8900                                       /*template_keyword_p=*/false,
8901                                       /*check_dependency_p=*/true,
8902                                       &template_p,
8903                                       /*declarator_p=*/false);
8904   /* If the next token isn't a `,' or a `>', then this argument wasn't
8905      really finished.  */
8906   if (!cp_parser_next_token_ends_template_argument_p (parser))
8907     cp_parser_error (parser, "expected template-argument");
8908   if (!cp_parser_error_occurred (parser))
8909     {
8910       /* Figure out what is being referred to.  If the id-expression
8911          was for a class template specialization, then we will have a
8912          TYPE_DECL at this point.  There is no need to do name lookup
8913          at this point in that case.  */
8914       if (TREE_CODE (argument) != TYPE_DECL)
8915         argument = cp_parser_lookup_name (parser, argument,
8916                                           /*is_type=*/false,
8917                                           /*is_template=*/template_p,
8918                                           /*is_namespace=*/false,
8919                                           /*check_dependency=*/true,
8920                                           /*ambiguous_p=*/NULL);
8921       if (TREE_CODE (argument) != TEMPLATE_DECL
8922           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8923         cp_parser_error (parser, "expected template-name");
8924     }
8925   if (cp_parser_parse_definitely (parser))
8926     return argument;
8927   /* It must be a non-type argument.  There permitted cases are given
8928      in [temp.arg.nontype]:
8929
8930      -- an integral constant-expression of integral or enumeration
8931         type; or
8932
8933      -- the name of a non-type template-parameter; or
8934
8935      -- the name of an object or function with external linkage...
8936
8937      -- the address of an object or function with external linkage...
8938
8939      -- a pointer to member...  */
8940   /* Look for a non-type template parameter.  */
8941   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8942     {
8943       cp_parser_parse_tentatively (parser);
8944       argument = cp_parser_primary_expression (parser,
8945                                                &idk,
8946                                                &qualifying_class);
8947       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8948           || !cp_parser_next_token_ends_template_argument_p (parser))
8949         cp_parser_simulate_error (parser);
8950       if (cp_parser_parse_definitely (parser))
8951         return argument;
8952     }
8953   /* If the next token is "&", the argument must be the address of an
8954      object or function with external linkage.  */
8955   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8956   if (address_p)
8957     cp_lexer_consume_token (parser->lexer);
8958   /* See if we might have an id-expression.  */
8959   token = cp_lexer_peek_token (parser->lexer);
8960   if (token->type == CPP_NAME
8961       || token->keyword == RID_OPERATOR
8962       || token->type == CPP_SCOPE
8963       || token->type == CPP_TEMPLATE_ID
8964       || token->type == CPP_NESTED_NAME_SPECIFIER)
8965     {
8966       cp_parser_parse_tentatively (parser);
8967       argument = cp_parser_primary_expression (parser,
8968                                                &idk,
8969                                                &qualifying_class);
8970       if (cp_parser_error_occurred (parser)
8971           || !cp_parser_next_token_ends_template_argument_p (parser))
8972         cp_parser_abort_tentative_parse (parser);
8973       else
8974         {
8975           if (qualifying_class)
8976             argument = finish_qualified_id_expr (qualifying_class,
8977                                                  argument,
8978                                                  /*done=*/true,
8979                                                  address_p);
8980           if (TREE_CODE (argument) == VAR_DECL)
8981             {
8982               /* A variable without external linkage might still be a
8983                  valid constant-expression, so no error is issued here
8984                  if the external-linkage check fails.  */
8985               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8986                 cp_parser_simulate_error (parser);
8987             }
8988           else if (is_overloaded_fn (argument))
8989             /* All overloaded functions are allowed; if the external
8990                linkage test does not pass, an error will be issued
8991                later.  */
8992             ;
8993           else if (address_p
8994                    && (TREE_CODE (argument) == OFFSET_REF
8995                        || TREE_CODE (argument) == SCOPE_REF))
8996             /* A pointer-to-member.  */
8997             ;
8998           else
8999             cp_parser_simulate_error (parser);
9000
9001           if (cp_parser_parse_definitely (parser))
9002             {
9003               if (address_p)
9004                 argument = build_x_unary_op (ADDR_EXPR, argument);
9005               return argument;
9006             }
9007         }
9008     }
9009   /* If the argument started with "&", there are no other valid
9010      alternatives at this point.  */
9011   if (address_p)
9012     {
9013       cp_parser_error (parser, "invalid non-type template argument");
9014       return error_mark_node;
9015     }
9016   /* If the argument wasn't successfully parsed as a type-id followed
9017      by '>>', the argument can only be a constant expression now.
9018      Otherwise, we try parsing the constant-expression tentatively,
9019      because the argument could really be a type-id.  */
9020   if (maybe_type_id)
9021     cp_parser_parse_tentatively (parser);
9022   argument = cp_parser_constant_expression (parser,
9023                                             /*allow_non_constant_p=*/false,
9024                                             /*non_constant_p=*/NULL);
9025   argument = fold_non_dependent_expr (argument);
9026   if (!maybe_type_id)
9027     return argument;
9028   if (!cp_parser_next_token_ends_template_argument_p (parser))
9029     cp_parser_error (parser, "expected template-argument");
9030   if (cp_parser_parse_definitely (parser))
9031     return argument;
9032   /* We did our best to parse the argument as a non type-id, but that
9033      was the only alternative that matched (albeit with a '>' after
9034      it). We can assume it's just a typo from the user, and a
9035      diagnostic will then be issued.  */
9036   return cp_parser_type_id (parser);
9037 }
9038
9039 /* Parse an explicit-instantiation.
9040
9041    explicit-instantiation:
9042      template declaration
9043
9044    Although the standard says `declaration', what it really means is:
9045
9046    explicit-instantiation:
9047      template decl-specifier-seq [opt] declarator [opt] ;
9048
9049    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9050    supposed to be allowed.  A defect report has been filed about this
9051    issue.
9052
9053    GNU Extension:
9054
9055    explicit-instantiation:
9056      storage-class-specifier template
9057        decl-specifier-seq [opt] declarator [opt] ;
9058      function-specifier template
9059        decl-specifier-seq [opt] declarator [opt] ;  */
9060
9061 static void
9062 cp_parser_explicit_instantiation (cp_parser* parser)
9063 {
9064   int declares_class_or_enum;
9065   cp_decl_specifier_seq decl_specifiers;
9066   tree extension_specifier = NULL_TREE;
9067
9068   /* Look for an (optional) storage-class-specifier or
9069      function-specifier.  */
9070   if (cp_parser_allow_gnu_extensions_p (parser))
9071     {
9072       extension_specifier
9073         = cp_parser_storage_class_specifier_opt (parser);
9074       if (!extension_specifier)
9075         extension_specifier
9076           = cp_parser_function_specifier_opt (parser,
9077                                               /*decl_specs=*/NULL);
9078     }
9079
9080   /* Look for the `template' keyword.  */
9081   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9082   /* Let the front end know that we are processing an explicit
9083      instantiation.  */
9084   begin_explicit_instantiation ();
9085   /* [temp.explicit] says that we are supposed to ignore access
9086      control while processing explicit instantiation directives.  */
9087   push_deferring_access_checks (dk_no_check);
9088   /* Parse a decl-specifier-seq.  */
9089   cp_parser_decl_specifier_seq (parser,
9090                                 CP_PARSER_FLAGS_OPTIONAL,
9091                                 &decl_specifiers,
9092                                 &declares_class_or_enum);
9093   /* If there was exactly one decl-specifier, and it declared a class,
9094      and there's no declarator, then we have an explicit type
9095      instantiation.  */
9096   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9097     {
9098       tree type;
9099
9100       type = check_tag_decl (&decl_specifiers);
9101       /* Turn access control back on for names used during
9102          template instantiation.  */
9103       pop_deferring_access_checks ();
9104       if (type)
9105         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9106     }
9107   else
9108     {
9109       cp_declarator *declarator;
9110       tree decl;
9111
9112       /* Parse the declarator.  */
9113       declarator
9114         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9115                                 /*ctor_dtor_or_conv_p=*/NULL,
9116                                 /*parenthesized_p=*/NULL);
9117       cp_parser_check_for_definition_in_return_type (declarator,
9118                                                      declares_class_or_enum);
9119       if (declarator != cp_error_declarator)
9120         {
9121           decl = grokdeclarator (declarator, &decl_specifiers,
9122                                  NORMAL, 0, NULL);
9123           /* Turn access control back on for names used during
9124              template instantiation.  */
9125           pop_deferring_access_checks ();
9126           /* Do the explicit instantiation.  */
9127           do_decl_instantiation (decl, extension_specifier);
9128         }
9129       else
9130         {
9131           pop_deferring_access_checks ();
9132           /* Skip the body of the explicit instantiation.  */
9133           cp_parser_skip_to_end_of_statement (parser);
9134         }
9135     }
9136   /* We're done with the instantiation.  */
9137   end_explicit_instantiation ();
9138
9139   cp_parser_consume_semicolon_at_end_of_statement (parser);
9140 }
9141
9142 /* Parse an explicit-specialization.
9143
9144    explicit-specialization:
9145      template < > declaration
9146
9147    Although the standard says `declaration', what it really means is:
9148
9149    explicit-specialization:
9150      template <> decl-specifier [opt] init-declarator [opt] ;
9151      template <> function-definition
9152      template <> explicit-specialization
9153      template <> template-declaration  */
9154
9155 static void
9156 cp_parser_explicit_specialization (cp_parser* parser)
9157 {
9158   /* Look for the `template' keyword.  */
9159   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9160   /* Look for the `<'.  */
9161   cp_parser_require (parser, CPP_LESS, "`<'");
9162   /* Look for the `>'.  */
9163   cp_parser_require (parser, CPP_GREATER, "`>'");
9164   /* We have processed another parameter list.  */
9165   ++parser->num_template_parameter_lists;
9166   /* Let the front end know that we are beginning a specialization.  */
9167   begin_specialization ();
9168
9169   /* If the next keyword is `template', we need to figure out whether
9170      or not we're looking a template-declaration.  */
9171   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9172     {
9173       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9174           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9175         cp_parser_template_declaration_after_export (parser,
9176                                                      /*member_p=*/false);
9177       else
9178         cp_parser_explicit_specialization (parser);
9179     }
9180   else
9181     /* Parse the dependent declaration.  */
9182     cp_parser_single_declaration (parser,
9183                                   /*member_p=*/false,
9184                                   /*friend_p=*/NULL);
9185
9186   /* We're done with the specialization.  */
9187   end_specialization ();
9188   /* We're done with this parameter list.  */
9189   --parser->num_template_parameter_lists;
9190 }
9191
9192 /* Parse a type-specifier.
9193
9194    type-specifier:
9195      simple-type-specifier
9196      class-specifier
9197      enum-specifier
9198      elaborated-type-specifier
9199      cv-qualifier
9200
9201    GNU Extension:
9202
9203    type-specifier:
9204      __complex__
9205
9206    Returns a representation of the type-specifier.  For a
9207    class-specifier, enum-specifier, or elaborated-type-specifier, a
9208    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9209
9210    If IS_FRIEND is TRUE then this type-specifier is being declared a
9211    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
9212    appearing in a decl-specifier-seq.
9213
9214    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9215    class-specifier, enum-specifier, or elaborated-type-specifier, then
9216    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9217    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9218    zero.
9219
9220    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9221    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9222    is set to FALSE.  */
9223
9224 static tree
9225 cp_parser_type_specifier (cp_parser* parser,
9226                           cp_parser_flags flags,
9227                           cp_decl_specifier_seq *decl_specs,
9228                           bool is_declaration,
9229                           int* declares_class_or_enum,
9230                           bool* is_cv_qualifier)
9231 {
9232   tree type_spec = NULL_TREE;
9233   cp_token *token;
9234   enum rid keyword;
9235   cp_decl_spec ds = ds_last;
9236
9237   /* Assume this type-specifier does not declare a new type.  */
9238   if (declares_class_or_enum)
9239     *declares_class_or_enum = 0;
9240   /* And that it does not specify a cv-qualifier.  */
9241   if (is_cv_qualifier)
9242     *is_cv_qualifier = false;
9243   /* Peek at the next token.  */
9244   token = cp_lexer_peek_token (parser->lexer);
9245
9246   /* If we're looking at a keyword, we can use that to guide the
9247      production we choose.  */
9248   keyword = token->keyword;
9249   switch (keyword)
9250     {
9251       /* Any of these indicate either a class-specifier, or an
9252          elaborated-type-specifier.  */
9253     case RID_CLASS:
9254     case RID_STRUCT:
9255     case RID_UNION:
9256     case RID_ENUM:
9257       /* Parse tentatively so that we can back up if we don't find a
9258          class-specifier or enum-specifier.  */
9259       cp_parser_parse_tentatively (parser);
9260       /* Look for the class-specifier or enum-specifier.  */
9261       if (keyword == RID_ENUM)
9262         type_spec = cp_parser_enum_specifier (parser);
9263       else
9264         type_spec = cp_parser_class_specifier (parser);
9265
9266       /* If that worked, we're done.  */
9267       if (cp_parser_parse_definitely (parser))
9268         {
9269           if (declares_class_or_enum)
9270             *declares_class_or_enum = 2;
9271           if (decl_specs)
9272             cp_parser_set_decl_spec_type (decl_specs,
9273                                           type_spec,
9274                                           /*user_defined_p=*/true);
9275           return type_spec;
9276         }
9277
9278       /* Fall through.  */
9279
9280     case RID_TYPENAME:
9281       /* Look for an elaborated-type-specifier.  */
9282       type_spec
9283         = (cp_parser_elaborated_type_specifier
9284            (parser,
9285             decl_specs && decl_specs->specs[(int) ds_friend],
9286             is_declaration));
9287       /* We're declaring a class or enum -- unless we're using
9288          `typename'.  */
9289       if (declares_class_or_enum && keyword != RID_TYPENAME)
9290         *declares_class_or_enum = 1;
9291       if (decl_specs)
9292         cp_parser_set_decl_spec_type (decl_specs,
9293                                       type_spec,
9294                                       /*user_defined_p=*/true);
9295       return type_spec;
9296
9297     case RID_CONST:
9298       ds = ds_const;
9299       if (is_cv_qualifier)
9300         *is_cv_qualifier = true;
9301       break;
9302
9303     case RID_VOLATILE:
9304       ds = ds_volatile;
9305       if (is_cv_qualifier)
9306         *is_cv_qualifier = true;
9307       break;
9308
9309     case RID_RESTRICT:
9310       ds = ds_restrict;
9311       if (is_cv_qualifier)
9312         *is_cv_qualifier = true;
9313       break;
9314
9315     case RID_COMPLEX:
9316       /* The `__complex__' keyword is a GNU extension.  */
9317       ds = ds_complex;
9318       break;
9319
9320     default:
9321       break;
9322     }
9323
9324   /* Handle simple keywords.  */
9325   if (ds != ds_last)
9326     {
9327       if (decl_specs)
9328         {
9329           ++decl_specs->specs[(int)ds];
9330           decl_specs->any_specifiers_p = true;
9331         }
9332       return cp_lexer_consume_token (parser->lexer)->value;
9333     }
9334
9335   /* If we do not already have a type-specifier, assume we are looking
9336      at a simple-type-specifier.  */
9337   type_spec = cp_parser_simple_type_specifier (parser,
9338                                                decl_specs,
9339                                                flags);
9340
9341   /* If we didn't find a type-specifier, and a type-specifier was not
9342      optional in this context, issue an error message.  */
9343   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9344     {
9345       cp_parser_error (parser, "expected type specifier");
9346       return error_mark_node;
9347     }
9348
9349   return type_spec;
9350 }
9351
9352 /* Parse a simple-type-specifier.
9353
9354    simple-type-specifier:
9355      :: [opt] nested-name-specifier [opt] type-name
9356      :: [opt] nested-name-specifier template template-id
9357      char
9358      wchar_t
9359      bool
9360      short
9361      int
9362      long
9363      signed
9364      unsigned
9365      float
9366      double
9367      void
9368
9369    GNU Extension:
9370
9371    simple-type-specifier:
9372      __typeof__ unary-expression
9373      __typeof__ ( type-id )
9374
9375    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9376    appropriately updated.  */
9377
9378 static tree
9379 cp_parser_simple_type_specifier (cp_parser* parser,
9380                                  cp_decl_specifier_seq *decl_specs,
9381                                  cp_parser_flags flags)
9382 {
9383   tree type = NULL_TREE;
9384   cp_token *token;
9385
9386   /* Peek at the next token.  */
9387   token = cp_lexer_peek_token (parser->lexer);
9388
9389   /* If we're looking at a keyword, things are easy.  */
9390   switch (token->keyword)
9391     {
9392     case RID_CHAR:
9393       if (decl_specs)
9394         decl_specs->explicit_char_p = true;
9395       type = char_type_node;
9396       break;
9397     case RID_WCHAR:
9398       type = wchar_type_node;
9399       break;
9400     case RID_BOOL:
9401       type = boolean_type_node;
9402       break;
9403     case RID_SHORT:
9404       if (decl_specs)
9405         ++decl_specs->specs[(int) ds_short];
9406       type = short_integer_type_node;
9407       break;
9408     case RID_INT:
9409       if (decl_specs)
9410         decl_specs->explicit_int_p = true;
9411       type = integer_type_node;
9412       break;
9413     case RID_LONG:
9414       if (decl_specs)
9415         ++decl_specs->specs[(int) ds_long];
9416       type = long_integer_type_node;
9417       break;
9418     case RID_SIGNED:
9419       if (decl_specs)
9420         ++decl_specs->specs[(int) ds_signed];
9421       type = integer_type_node;
9422       break;
9423     case RID_UNSIGNED:
9424       if (decl_specs)
9425         ++decl_specs->specs[(int) ds_unsigned];
9426       type = unsigned_type_node;
9427       break;
9428     case RID_FLOAT:
9429       type = float_type_node;
9430       break;
9431     case RID_DOUBLE:
9432       type = double_type_node;
9433       break;
9434     case RID_VOID:
9435       type = void_type_node;
9436       break;
9437
9438     case RID_TYPEOF:
9439       /* Consume the `typeof' token.  */
9440       cp_lexer_consume_token (parser->lexer);
9441       /* Parse the operand to `typeof'.  */
9442       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9443       /* If it is not already a TYPE, take its type.  */
9444       if (!TYPE_P (type))
9445         type = finish_typeof (type);
9446
9447       if (decl_specs)
9448         cp_parser_set_decl_spec_type (decl_specs, type,
9449                                       /*user_defined_p=*/true);
9450
9451       return type;
9452
9453     default:
9454       break;
9455     }
9456
9457   /* If the type-specifier was for a built-in type, we're done.  */
9458   if (type)
9459     {
9460       tree id;
9461
9462       /* Record the type.  */
9463       if (decl_specs
9464           && (token->keyword != RID_SIGNED
9465               && token->keyword != RID_UNSIGNED
9466               && token->keyword != RID_SHORT
9467               && token->keyword != RID_LONG))
9468         cp_parser_set_decl_spec_type (decl_specs,
9469                                       type,
9470                                       /*user_defined=*/false);
9471       if (decl_specs)
9472         decl_specs->any_specifiers_p = true;
9473
9474       /* Consume the token.  */
9475       id = cp_lexer_consume_token (parser->lexer)->value;
9476
9477       /* There is no valid C++ program where a non-template type is
9478          followed by a "<".  That usually indicates that the user thought
9479          that the type was a template.  */
9480       cp_parser_check_for_invalid_template_id (parser, type);
9481
9482       return TYPE_NAME (type);
9483     }
9484
9485   /* The type-specifier must be a user-defined type.  */
9486   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9487     {
9488       bool qualified_p;
9489       bool global_p;
9490
9491       /* Don't gobble tokens or issue error messages if this is an
9492          optional type-specifier.  */
9493       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9494         cp_parser_parse_tentatively (parser);
9495
9496       /* Look for the optional `::' operator.  */
9497       global_p
9498         = (cp_parser_global_scope_opt (parser,
9499                                        /*current_scope_valid_p=*/false)
9500            != NULL_TREE);
9501       /* Look for the nested-name specifier.  */
9502       qualified_p
9503         = (cp_parser_nested_name_specifier_opt (parser,
9504                                                 /*typename_keyword_p=*/false,
9505                                                 /*check_dependency_p=*/true,
9506                                                 /*type_p=*/false,
9507                                                 /*is_declaration=*/false)
9508            != NULL_TREE);
9509       /* If we have seen a nested-name-specifier, and the next token
9510          is `template', then we are using the template-id production.  */
9511       if (parser->scope
9512           && cp_parser_optional_template_keyword (parser))
9513         {
9514           /* Look for the template-id.  */
9515           type = cp_parser_template_id (parser,
9516                                         /*template_keyword_p=*/true,
9517                                         /*check_dependency_p=*/true,
9518                                         /*is_declaration=*/false);
9519           /* If the template-id did not name a type, we are out of
9520              luck.  */
9521           if (TREE_CODE (type) != TYPE_DECL)
9522             {
9523               cp_parser_error (parser, "expected template-id for type");
9524               type = NULL_TREE;
9525             }
9526         }
9527       /* Otherwise, look for a type-name.  */
9528       else
9529         type = cp_parser_type_name (parser);
9530       /* Keep track of all name-lookups performed in class scopes.  */
9531       if (type
9532           && !global_p
9533           && !qualified_p
9534           && TREE_CODE (type) == TYPE_DECL
9535           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9536         maybe_note_name_used_in_class (DECL_NAME (type), type);
9537       /* If it didn't work out, we don't have a TYPE.  */
9538       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9539           && !cp_parser_parse_definitely (parser))
9540         type = NULL_TREE;
9541       if (type && decl_specs)
9542         cp_parser_set_decl_spec_type (decl_specs, type,
9543                                       /*user_defined=*/true);
9544     }
9545
9546   /* If we didn't get a type-name, issue an error message.  */
9547   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9548     {
9549       cp_parser_error (parser, "expected type-name");
9550       return error_mark_node;
9551     }
9552
9553   /* There is no valid C++ program where a non-template type is
9554      followed by a "<".  That usually indicates that the user thought
9555      that the type was a template.  */
9556   if (type && type != error_mark_node)
9557     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9558
9559   return type;
9560 }
9561
9562 /* Parse a type-name.
9563
9564    type-name:
9565      class-name
9566      enum-name
9567      typedef-name
9568
9569    enum-name:
9570      identifier
9571
9572    typedef-name:
9573      identifier
9574
9575    Returns a TYPE_DECL for the the type.  */
9576
9577 static tree
9578 cp_parser_type_name (cp_parser* parser)
9579 {
9580   tree type_decl;
9581   tree identifier;
9582
9583   /* We can't know yet whether it is a class-name or not.  */
9584   cp_parser_parse_tentatively (parser);
9585   /* Try a class-name.  */
9586   type_decl = cp_parser_class_name (parser,
9587                                     /*typename_keyword_p=*/false,
9588                                     /*template_keyword_p=*/false,
9589                                     /*type_p=*/false,
9590                                     /*check_dependency_p=*/true,
9591                                     /*class_head_p=*/false,
9592                                     /*is_declaration=*/false);
9593   /* If it's not a class-name, keep looking.  */
9594   if (!cp_parser_parse_definitely (parser))
9595     {
9596       /* It must be a typedef-name or an enum-name.  */
9597       identifier = cp_parser_identifier (parser);
9598       if (identifier == error_mark_node)
9599         return error_mark_node;
9600
9601       /* Look up the type-name.  */
9602       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9603       /* Issue an error if we did not find a type-name.  */
9604       if (TREE_CODE (type_decl) != TYPE_DECL)
9605         {
9606           if (!cp_parser_simulate_error (parser))
9607             cp_parser_name_lookup_error (parser, identifier, type_decl,
9608                                          "is not a type");
9609           type_decl = error_mark_node;
9610         }
9611       /* Remember that the name was used in the definition of the
9612          current class so that we can check later to see if the
9613          meaning would have been different after the class was
9614          entirely defined.  */
9615       else if (type_decl != error_mark_node
9616                && !parser->scope)
9617         maybe_note_name_used_in_class (identifier, type_decl);
9618     }
9619
9620   return type_decl;
9621 }
9622
9623
9624 /* Parse an elaborated-type-specifier.  Note that the grammar given
9625    here incorporates the resolution to DR68.
9626
9627    elaborated-type-specifier:
9628      class-key :: [opt] nested-name-specifier [opt] identifier
9629      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9630      enum :: [opt] nested-name-specifier [opt] identifier
9631      typename :: [opt] nested-name-specifier identifier
9632      typename :: [opt] nested-name-specifier template [opt]
9633        template-id
9634
9635    GNU extension:
9636
9637    elaborated-type-specifier:
9638      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9639      class-key attributes :: [opt] nested-name-specifier [opt]
9640                template [opt] template-id
9641      enum attributes :: [opt] nested-name-specifier [opt] identifier
9642
9643    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9644    declared `friend'.  If IS_DECLARATION is TRUE, then this
9645    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9646    something is being declared.
9647
9648    Returns the TYPE specified.  */
9649
9650 static tree
9651 cp_parser_elaborated_type_specifier (cp_parser* parser,
9652                                      bool is_friend,
9653                                      bool is_declaration)
9654 {
9655   enum tag_types tag_type;
9656   tree identifier;
9657   tree type = NULL_TREE;
9658   tree attributes = NULL_TREE;
9659
9660   /* See if we're looking at the `enum' keyword.  */
9661   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9662     {
9663       /* Consume the `enum' token.  */
9664       cp_lexer_consume_token (parser->lexer);
9665       /* Remember that it's an enumeration type.  */
9666       tag_type = enum_type;
9667       /* Parse the attributes.  */
9668       attributes = cp_parser_attributes_opt (parser);
9669     }
9670   /* Or, it might be `typename'.  */
9671   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9672                                            RID_TYPENAME))
9673     {
9674       /* Consume the `typename' token.  */
9675       cp_lexer_consume_token (parser->lexer);
9676       /* Remember that it's a `typename' type.  */
9677       tag_type = typename_type;
9678       /* The `typename' keyword is only allowed in templates.  */
9679       if (!processing_template_decl)
9680         pedwarn ("using `typename' outside of template");
9681     }
9682   /* Otherwise it must be a class-key.  */
9683   else
9684     {
9685       tag_type = cp_parser_class_key (parser);
9686       if (tag_type == none_type)
9687         return error_mark_node;
9688       /* Parse the attributes.  */
9689       attributes = cp_parser_attributes_opt (parser);
9690     }
9691
9692   /* Look for the `::' operator.  */
9693   cp_parser_global_scope_opt (parser,
9694                               /*current_scope_valid_p=*/false);
9695   /* Look for the nested-name-specifier.  */
9696   if (tag_type == typename_type)
9697     {
9698       if (cp_parser_nested_name_specifier (parser,
9699                                            /*typename_keyword_p=*/true,
9700                                            /*check_dependency_p=*/true,
9701                                            /*type_p=*/true,
9702                                            is_declaration)
9703           == error_mark_node)
9704         return error_mark_node;
9705     }
9706   else
9707     /* Even though `typename' is not present, the proposed resolution
9708        to Core Issue 180 says that in `class A<T>::B', `B' should be
9709        considered a type-name, even if `A<T>' is dependent.  */
9710     cp_parser_nested_name_specifier_opt (parser,
9711                                          /*typename_keyword_p=*/true,
9712                                          /*check_dependency_p=*/true,
9713                                          /*type_p=*/true,
9714                                          is_declaration);
9715   /* For everything but enumeration types, consider a template-id.  */
9716   if (tag_type != enum_type)
9717     {
9718       bool template_p = false;
9719       tree decl;
9720
9721       /* Allow the `template' keyword.  */
9722       template_p = cp_parser_optional_template_keyword (parser);
9723       /* If we didn't see `template', we don't know if there's a
9724          template-id or not.  */
9725       if (!template_p)
9726         cp_parser_parse_tentatively (parser);
9727       /* Parse the template-id.  */
9728       decl = cp_parser_template_id (parser, template_p,
9729                                     /*check_dependency_p=*/true,
9730                                     is_declaration);
9731       /* If we didn't find a template-id, look for an ordinary
9732          identifier.  */
9733       if (!template_p && !cp_parser_parse_definitely (parser))
9734         ;
9735       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9736          in effect, then we must assume that, upon instantiation, the
9737          template will correspond to a class.  */
9738       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9739                && tag_type == typename_type)
9740         type = make_typename_type (parser->scope, decl,
9741                                    /*complain=*/1);
9742       else
9743         type = TREE_TYPE (decl);
9744     }
9745
9746   /* For an enumeration type, consider only a plain identifier.  */
9747   if (!type)
9748     {
9749       identifier = cp_parser_identifier (parser);
9750
9751       if (identifier == error_mark_node)
9752         {
9753           parser->scope = NULL_TREE;
9754           return error_mark_node;
9755         }
9756
9757       /* For a `typename', we needn't call xref_tag.  */
9758       if (tag_type == typename_type)
9759         return cp_parser_make_typename_type (parser, parser->scope,
9760                                              identifier);
9761       /* Look up a qualified name in the usual way.  */
9762       if (parser->scope)
9763         {
9764           tree decl;
9765
9766           /* In an elaborated-type-specifier, names are assumed to name
9767              types, so we set IS_TYPE to TRUE when calling
9768              cp_parser_lookup_name.  */
9769           decl = cp_parser_lookup_name (parser, identifier,
9770                                         /*is_type=*/true,
9771                                         /*is_template=*/false,
9772                                         /*is_namespace=*/false,
9773                                         /*check_dependency=*/true,
9774                                         /*ambiguous_p=*/NULL);
9775
9776           /* If we are parsing friend declaration, DECL may be a
9777              TEMPLATE_DECL tree node here.  However, we need to check
9778              whether this TEMPLATE_DECL results in valid code.  Consider
9779              the following example:
9780
9781                namespace N {
9782                  template <class T> class C {};
9783                }
9784                class X {
9785                  template <class T> friend class N::C; // #1, valid code
9786                };
9787                template <class T> class Y {
9788                  friend class N::C;                    // #2, invalid code
9789                };
9790
9791              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9792              name lookup of `N::C'.  We see that friend declaration must
9793              be template for the code to be valid.  Note that
9794              processing_template_decl does not work here since it is
9795              always 1 for the above two cases.  */
9796
9797           decl = (cp_parser_maybe_treat_template_as_class
9798                   (decl, /*tag_name_p=*/is_friend
9799                          && parser->num_template_parameter_lists));
9800
9801           if (TREE_CODE (decl) != TYPE_DECL)
9802             {
9803               error ("expected type-name");
9804               return error_mark_node;
9805             }
9806
9807           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9808             check_elaborated_type_specifier
9809               (tag_type, decl,
9810                (parser->num_template_parameter_lists
9811                 || DECL_SELF_REFERENCE_P (decl)));
9812
9813           type = TREE_TYPE (decl);
9814         }
9815       else
9816         {
9817           /* An elaborated-type-specifier sometimes introduces a new type and
9818              sometimes names an existing type.  Normally, the rule is that it
9819              introduces a new type only if there is not an existing type of
9820              the same name already in scope.  For example, given:
9821
9822                struct S {};
9823                void f() { struct S s; }
9824
9825              the `struct S' in the body of `f' is the same `struct S' as in
9826              the global scope; the existing definition is used.  However, if
9827              there were no global declaration, this would introduce a new
9828              local class named `S'.
9829
9830              An exception to this rule applies to the following code:
9831
9832                namespace N { struct S; }
9833
9834              Here, the elaborated-type-specifier names a new type
9835              unconditionally; even if there is already an `S' in the
9836              containing scope this declaration names a new type.
9837              This exception only applies if the elaborated-type-specifier
9838              forms the complete declaration:
9839
9840                [class.name]
9841
9842                A declaration consisting solely of `class-key identifier ;' is
9843                either a redeclaration of the name in the current scope or a
9844                forward declaration of the identifier as a class name.  It
9845                introduces the name into the current scope.
9846
9847              We are in this situation precisely when the next token is a `;'.
9848
9849              An exception to the exception is that a `friend' declaration does
9850              *not* name a new type; i.e., given:
9851
9852                struct S { friend struct T; };
9853
9854              `T' is not a new type in the scope of `S'.
9855
9856              Also, `new struct S' or `sizeof (struct S)' never results in the
9857              definition of a new type; a new type can only be declared in a
9858              declaration context.  */
9859
9860           /* Warn about attributes. They are ignored.  */
9861           if (attributes)
9862             warning ("type attributes are honored only at type definition");
9863
9864           type = xref_tag (tag_type, identifier,
9865                            (is_friend
9866                             || !is_declaration
9867                             || cp_lexer_next_token_is_not (parser->lexer,
9868                                                            CPP_SEMICOLON)),
9869                            parser->num_template_parameter_lists);
9870         }
9871     }
9872   if (tag_type != enum_type)
9873     cp_parser_check_class_key (tag_type, type);
9874
9875   /* A "<" cannot follow an elaborated type specifier.  If that
9876      happens, the user was probably trying to form a template-id.  */
9877   cp_parser_check_for_invalid_template_id (parser, type);
9878
9879   return type;
9880 }
9881
9882 /* Parse an enum-specifier.
9883
9884    enum-specifier:
9885      enum identifier [opt] { enumerator-list [opt] }
9886
9887    Returns an ENUM_TYPE representing the enumeration.  */
9888
9889 static tree
9890 cp_parser_enum_specifier (cp_parser* parser)
9891 {
9892   cp_token *token;
9893   tree identifier = NULL_TREE;
9894   tree type;
9895
9896   /* Look for the `enum' keyword.  */
9897   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9898     return error_mark_node;
9899   /* Peek at the next token.  */
9900   token = cp_lexer_peek_token (parser->lexer);
9901
9902   /* See if it is an identifier.  */
9903   if (token->type == CPP_NAME)
9904     identifier = cp_parser_identifier (parser);
9905
9906   /* Look for the `{'.  */
9907   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9908     return error_mark_node;
9909
9910   /* At this point, we're going ahead with the enum-specifier, even
9911      if some other problem occurs.  */
9912   cp_parser_commit_to_tentative_parse (parser);
9913
9914   /* Issue an error message if type-definitions are forbidden here.  */
9915   cp_parser_check_type_definition (parser);
9916
9917   /* Create the new type.  */
9918   type = start_enum (identifier ? identifier : make_anon_name ());
9919
9920   /* Peek at the next token.  */
9921   token = cp_lexer_peek_token (parser->lexer);
9922   /* If it's not a `}', then there are some enumerators.  */
9923   if (token->type != CPP_CLOSE_BRACE)
9924     cp_parser_enumerator_list (parser, type);
9925   /* Look for the `}'.  */
9926   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9927
9928   /* Finish up the enumeration.  */
9929   finish_enum (type);
9930
9931   return type;
9932 }
9933
9934 /* Parse an enumerator-list.  The enumerators all have the indicated
9935    TYPE.
9936
9937    enumerator-list:
9938      enumerator-definition
9939      enumerator-list , enumerator-definition  */
9940
9941 static void
9942 cp_parser_enumerator_list (cp_parser* parser, tree type)
9943 {
9944   while (true)
9945     {
9946       cp_token *token;
9947
9948       /* Parse an enumerator-definition.  */
9949       cp_parser_enumerator_definition (parser, type);
9950       /* Peek at the next token.  */
9951       token = cp_lexer_peek_token (parser->lexer);
9952       /* If it's not a `,', then we've reached the end of the
9953          list.  */
9954       if (token->type != CPP_COMMA)
9955         break;
9956       /* Otherwise, consume the `,' and keep going.  */
9957       cp_lexer_consume_token (parser->lexer);
9958       /* If the next token is a `}', there is a trailing comma.  */
9959       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9960         {
9961           if (pedantic && !in_system_header)
9962             pedwarn ("comma at end of enumerator list");
9963           break;
9964         }
9965     }
9966 }
9967
9968 /* Parse an enumerator-definition.  The enumerator has the indicated
9969    TYPE.
9970
9971    enumerator-definition:
9972      enumerator
9973      enumerator = constant-expression
9974
9975    enumerator:
9976      identifier  */
9977
9978 static void
9979 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9980 {
9981   cp_token *token;
9982   tree identifier;
9983   tree value;
9984
9985   /* Look for the identifier.  */
9986   identifier = cp_parser_identifier (parser);
9987   if (identifier == error_mark_node)
9988     return;
9989
9990   /* Peek at the next token.  */
9991   token = cp_lexer_peek_token (parser->lexer);
9992   /* If it's an `=', then there's an explicit value.  */
9993   if (token->type == CPP_EQ)
9994     {
9995       /* Consume the `=' token.  */
9996       cp_lexer_consume_token (parser->lexer);
9997       /* Parse the value.  */
9998       value = cp_parser_constant_expression (parser,
9999                                              /*allow_non_constant_p=*/false,
10000                                              NULL);
10001     }
10002   else
10003     value = NULL_TREE;
10004
10005   /* Create the enumerator.  */
10006   build_enumerator (identifier, value, type);
10007 }
10008
10009 /* Parse a namespace-name.
10010
10011    namespace-name:
10012      original-namespace-name
10013      namespace-alias
10014
10015    Returns the NAMESPACE_DECL for the namespace.  */
10016
10017 static tree
10018 cp_parser_namespace_name (cp_parser* parser)
10019 {
10020   tree identifier;
10021   tree namespace_decl;
10022
10023   /* Get the name of the namespace.  */
10024   identifier = cp_parser_identifier (parser);
10025   if (identifier == error_mark_node)
10026     return error_mark_node;
10027
10028   /* Look up the identifier in the currently active scope.  Look only
10029      for namespaces, due to:
10030
10031        [basic.lookup.udir]
10032
10033        When looking up a namespace-name in a using-directive or alias
10034        definition, only namespace names are considered.
10035
10036      And:
10037
10038        [basic.lookup.qual]
10039
10040        During the lookup of a name preceding the :: scope resolution
10041        operator, object, function, and enumerator names are ignored.
10042
10043      (Note that cp_parser_class_or_namespace_name only calls this
10044      function if the token after the name is the scope resolution
10045      operator.)  */
10046   namespace_decl = cp_parser_lookup_name (parser, identifier,
10047                                           /*is_type=*/false,
10048                                           /*is_template=*/false,
10049                                           /*is_namespace=*/true,
10050                                           /*check_dependency=*/true,
10051                                           /*ambiguous_p=*/NULL);
10052   /* If it's not a namespace, issue an error.  */
10053   if (namespace_decl == error_mark_node
10054       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10055     {
10056       cp_parser_error (parser, "expected namespace-name");
10057       namespace_decl = error_mark_node;
10058     }
10059
10060   return namespace_decl;
10061 }
10062
10063 /* Parse a namespace-definition.
10064
10065    namespace-definition:
10066      named-namespace-definition
10067      unnamed-namespace-definition
10068
10069    named-namespace-definition:
10070      original-namespace-definition
10071      extension-namespace-definition
10072
10073    original-namespace-definition:
10074      namespace identifier { namespace-body }
10075
10076    extension-namespace-definition:
10077      namespace original-namespace-name { namespace-body }
10078
10079    unnamed-namespace-definition:
10080      namespace { namespace-body } */
10081
10082 static void
10083 cp_parser_namespace_definition (cp_parser* parser)
10084 {
10085   tree identifier;
10086
10087   /* Look for the `namespace' keyword.  */
10088   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10089
10090   /* Get the name of the namespace.  We do not attempt to distinguish
10091      between an original-namespace-definition and an
10092      extension-namespace-definition at this point.  The semantic
10093      analysis routines are responsible for that.  */
10094   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10095     identifier = cp_parser_identifier (parser);
10096   else
10097     identifier = NULL_TREE;
10098
10099   /* Look for the `{' to start the namespace.  */
10100   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10101   /* Start the namespace.  */
10102   push_namespace (identifier);
10103   /* Parse the body of the namespace.  */
10104   cp_parser_namespace_body (parser);
10105   /* Finish the namespace.  */
10106   pop_namespace ();
10107   /* Look for the final `}'.  */
10108   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10109 }
10110
10111 /* Parse a namespace-body.
10112
10113    namespace-body:
10114      declaration-seq [opt]  */
10115
10116 static void
10117 cp_parser_namespace_body (cp_parser* parser)
10118 {
10119   cp_parser_declaration_seq_opt (parser);
10120 }
10121
10122 /* Parse a namespace-alias-definition.
10123
10124    namespace-alias-definition:
10125      namespace identifier = qualified-namespace-specifier ;  */
10126
10127 static void
10128 cp_parser_namespace_alias_definition (cp_parser* parser)
10129 {
10130   tree identifier;
10131   tree namespace_specifier;
10132
10133   /* Look for the `namespace' keyword.  */
10134   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10135   /* Look for the identifier.  */
10136   identifier = cp_parser_identifier (parser);
10137   if (identifier == error_mark_node)
10138     return;
10139   /* Look for the `=' token.  */
10140   cp_parser_require (parser, CPP_EQ, "`='");
10141   /* Look for the qualified-namespace-specifier.  */
10142   namespace_specifier
10143     = cp_parser_qualified_namespace_specifier (parser);
10144   /* Look for the `;' token.  */
10145   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10146
10147   /* Register the alias in the symbol table.  */
10148   do_namespace_alias (identifier, namespace_specifier);
10149 }
10150
10151 /* Parse a qualified-namespace-specifier.
10152
10153    qualified-namespace-specifier:
10154      :: [opt] nested-name-specifier [opt] namespace-name
10155
10156    Returns a NAMESPACE_DECL corresponding to the specified
10157    namespace.  */
10158
10159 static tree
10160 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10161 {
10162   /* Look for the optional `::'.  */
10163   cp_parser_global_scope_opt (parser,
10164                               /*current_scope_valid_p=*/false);
10165
10166   /* Look for the optional nested-name-specifier.  */
10167   cp_parser_nested_name_specifier_opt (parser,
10168                                        /*typename_keyword_p=*/false,
10169                                        /*check_dependency_p=*/true,
10170                                        /*type_p=*/false,
10171                                        /*is_declaration=*/true);
10172
10173   return cp_parser_namespace_name (parser);
10174 }
10175
10176 /* Parse a using-declaration.
10177
10178    using-declaration:
10179      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10180      using :: unqualified-id ;  */
10181
10182 static void
10183 cp_parser_using_declaration (cp_parser* parser)
10184 {
10185   cp_token *token;
10186   bool typename_p = false;
10187   bool global_scope_p;
10188   tree decl;
10189   tree identifier;
10190   tree scope;
10191   tree qscope;
10192
10193   /* Look for the `using' keyword.  */
10194   cp_parser_require_keyword (parser, RID_USING, "`using'");
10195
10196   /* Peek at the next token.  */
10197   token = cp_lexer_peek_token (parser->lexer);
10198   /* See if it's `typename'.  */
10199   if (token->keyword == RID_TYPENAME)
10200     {
10201       /* Remember that we've seen it.  */
10202       typename_p = true;
10203       /* Consume the `typename' token.  */
10204       cp_lexer_consume_token (parser->lexer);
10205     }
10206
10207   /* Look for the optional global scope qualification.  */
10208   global_scope_p
10209     = (cp_parser_global_scope_opt (parser,
10210                                    /*current_scope_valid_p=*/false)
10211        != NULL_TREE);
10212
10213   /* If we saw `typename', or didn't see `::', then there must be a
10214      nested-name-specifier present.  */
10215   if (typename_p || !global_scope_p)
10216     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10217                                               /*check_dependency_p=*/true,
10218                                               /*type_p=*/false,
10219                                               /*is_declaration=*/true);
10220   /* Otherwise, we could be in either of the two productions.  In that
10221      case, treat the nested-name-specifier as optional.  */
10222   else
10223     qscope = cp_parser_nested_name_specifier_opt (parser,
10224                                                   /*typename_keyword_p=*/false,
10225                                                   /*check_dependency_p=*/true,
10226                                                   /*type_p=*/false,
10227                                                   /*is_declaration=*/true);
10228   if (!qscope)
10229     qscope = global_namespace;
10230
10231   /* Parse the unqualified-id.  */
10232   identifier = cp_parser_unqualified_id (parser,
10233                                          /*template_keyword_p=*/false,
10234                                          /*check_dependency_p=*/true,
10235                                          /*declarator_p=*/true);
10236
10237   /* The function we call to handle a using-declaration is different
10238      depending on what scope we are in.  */
10239   if (identifier == error_mark_node)
10240     ;
10241   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10242            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10243     /* [namespace.udecl]
10244
10245        A using declaration shall not name a template-id.  */
10246     error ("a template-id may not appear in a using-declaration");
10247   else
10248     {
10249       scope = current_scope ();
10250       if (scope && TYPE_P (scope))
10251         {
10252           /* Create the USING_DECL.  */
10253           decl = do_class_using_decl (build_nt (SCOPE_REF,
10254                                                 parser->scope,
10255                                                 identifier));
10256           /* Add it to the list of members in this class.  */
10257           finish_member_declaration (decl);
10258         }
10259       else
10260         {
10261           decl = cp_parser_lookup_name_simple (parser, identifier);
10262           if (decl == error_mark_node)
10263             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10264           else if (scope)
10265             do_local_using_decl (decl, qscope, identifier);
10266           else
10267             do_toplevel_using_decl (decl, qscope, identifier);
10268         }
10269     }
10270
10271   /* Look for the final `;'.  */
10272   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10273 }
10274
10275 /* Parse a using-directive.
10276
10277    using-directive:
10278      using namespace :: [opt] nested-name-specifier [opt]
10279        namespace-name ;  */
10280
10281 static void
10282 cp_parser_using_directive (cp_parser* parser)
10283 {
10284   tree namespace_decl;
10285   tree attribs;
10286
10287   /* Look for the `using' keyword.  */
10288   cp_parser_require_keyword (parser, RID_USING, "`using'");
10289   /* And the `namespace' keyword.  */
10290   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10291   /* Look for the optional `::' operator.  */
10292   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10293   /* And the optional nested-name-specifier.  */
10294   cp_parser_nested_name_specifier_opt (parser,
10295                                        /*typename_keyword_p=*/false,
10296                                        /*check_dependency_p=*/true,
10297                                        /*type_p=*/false,
10298                                        /*is_declaration=*/true);
10299   /* Get the namespace being used.  */
10300   namespace_decl = cp_parser_namespace_name (parser);
10301   /* And any specified attributes.  */
10302   attribs = cp_parser_attributes_opt (parser);
10303   /* Update the symbol table.  */
10304   parse_using_directive (namespace_decl, attribs);
10305   /* Look for the final `;'.  */
10306   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10307 }
10308
10309 /* Parse an asm-definition.
10310
10311    asm-definition:
10312      asm ( string-literal ) ;
10313
10314    GNU Extension:
10315
10316    asm-definition:
10317      asm volatile [opt] ( string-literal ) ;
10318      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10319      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10320                           : asm-operand-list [opt] ) ;
10321      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10322                           : asm-operand-list [opt]
10323                           : asm-operand-list [opt] ) ;  */
10324
10325 static void
10326 cp_parser_asm_definition (cp_parser* parser)
10327 {
10328   cp_token *token;
10329   tree string;
10330   tree outputs = NULL_TREE;
10331   tree inputs = NULL_TREE;
10332   tree clobbers = NULL_TREE;
10333   tree asm_stmt;
10334   bool volatile_p = false;
10335   bool extended_p = false;
10336
10337   /* Look for the `asm' keyword.  */
10338   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10339   /* See if the next token is `volatile'.  */
10340   if (cp_parser_allow_gnu_extensions_p (parser)
10341       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10342     {
10343       /* Remember that we saw the `volatile' keyword.  */
10344       volatile_p = true;
10345       /* Consume the token.  */
10346       cp_lexer_consume_token (parser->lexer);
10347     }
10348   /* Look for the opening `('.  */
10349   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10350   /* Look for the string.  */
10351   c_lex_string_translate = 0;
10352   token = cp_parser_require (parser, CPP_STRING, "asm body");
10353   if (!token)
10354     goto finish;
10355   string = token->value;
10356   /* If we're allowing GNU extensions, check for the extended assembly
10357      syntax.  Unfortunately, the `:' tokens need not be separated by
10358      a space in C, and so, for compatibility, we tolerate that here
10359      too.  Doing that means that we have to treat the `::' operator as
10360      two `:' tokens.  */
10361   if (cp_parser_allow_gnu_extensions_p (parser)
10362       && at_function_scope_p ()
10363       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10364           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10365     {
10366       bool inputs_p = false;
10367       bool clobbers_p = false;
10368
10369       /* The extended syntax was used.  */
10370       extended_p = true;
10371
10372       /* Look for outputs.  */
10373       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10374         {
10375           /* Consume the `:'.  */
10376           cp_lexer_consume_token (parser->lexer);
10377           /* Parse the output-operands.  */
10378           if (cp_lexer_next_token_is_not (parser->lexer,
10379                                           CPP_COLON)
10380               && cp_lexer_next_token_is_not (parser->lexer,
10381                                              CPP_SCOPE)
10382               && cp_lexer_next_token_is_not (parser->lexer,
10383                                              CPP_CLOSE_PAREN))
10384             outputs = cp_parser_asm_operand_list (parser);
10385         }
10386       /* If the next token is `::', there are no outputs, and the
10387          next token is the beginning of the inputs.  */
10388       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10389         /* The inputs are coming next.  */
10390         inputs_p = true;
10391
10392       /* Look for inputs.  */
10393       if (inputs_p
10394           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10395         {
10396           /* Consume the `:' or `::'.  */
10397           cp_lexer_consume_token (parser->lexer);
10398           /* Parse the output-operands.  */
10399           if (cp_lexer_next_token_is_not (parser->lexer,
10400                                           CPP_COLON)
10401               && cp_lexer_next_token_is_not (parser->lexer,
10402                                              CPP_CLOSE_PAREN))
10403             inputs = cp_parser_asm_operand_list (parser);
10404         }
10405       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10406         /* The clobbers are coming next.  */
10407         clobbers_p = true;
10408
10409       /* Look for clobbers.  */
10410       if (clobbers_p
10411           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10412         {
10413           /* Consume the `:' or `::'.  */
10414           cp_lexer_consume_token (parser->lexer);
10415           /* Parse the clobbers.  */
10416           if (cp_lexer_next_token_is_not (parser->lexer,
10417                                           CPP_CLOSE_PAREN))
10418             clobbers = cp_parser_asm_clobber_list (parser);
10419         }
10420     }
10421   /* Look for the closing `)'.  */
10422   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10423     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10424                                            /*consume_paren=*/true);
10425   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10426
10427   /* Create the ASM_EXPR.  */
10428   if (at_function_scope_p ())
10429     {
10430       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10431                                   inputs, clobbers);
10432       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10433       if (!extended_p)
10434         ASM_INPUT_P (asm_stmt) = 1;
10435     }
10436   else
10437     assemble_asm (string);
10438
10439  finish:
10440   c_lex_string_translate = 1;
10441 }
10442
10443 /* Declarators [gram.dcl.decl] */
10444
10445 /* Parse an init-declarator.
10446
10447    init-declarator:
10448      declarator initializer [opt]
10449
10450    GNU Extension:
10451
10452    init-declarator:
10453      declarator asm-specification [opt] attributes [opt] initializer [opt]
10454
10455    function-definition:
10456      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10457        function-body
10458      decl-specifier-seq [opt] declarator function-try-block
10459
10460    GNU Extension:
10461
10462    function-definition:
10463      __extension__ function-definition
10464
10465    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10466    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10467    then this declarator appears in a class scope.  The new DECL created
10468    by this declarator is returned.
10469
10470    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10471    for a function-definition here as well.  If the declarator is a
10472    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10473    be TRUE upon return.  By that point, the function-definition will
10474    have been completely parsed.
10475
10476    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10477    is FALSE.  */
10478
10479 static tree
10480 cp_parser_init_declarator (cp_parser* parser,
10481                            cp_decl_specifier_seq *decl_specifiers,
10482                            bool function_definition_allowed_p,
10483                            bool member_p,
10484                            int declares_class_or_enum,
10485                            bool* function_definition_p)
10486 {
10487   cp_token *token;
10488   cp_declarator *declarator;
10489   tree prefix_attributes;
10490   tree attributes;
10491   tree asm_specification;
10492   tree initializer;
10493   tree decl = NULL_TREE;
10494   tree scope;
10495   bool is_initialized;
10496   bool is_parenthesized_init;
10497   bool is_non_constant_init;
10498   int ctor_dtor_or_conv_p;
10499   bool friend_p;
10500   bool pop_p = false;
10501
10502   /* Gather the attributes that were provided with the
10503      decl-specifiers.  */
10504   prefix_attributes = decl_specifiers->attributes;
10505
10506   /* Assume that this is not the declarator for a function
10507      definition.  */
10508   if (function_definition_p)
10509     *function_definition_p = false;
10510
10511   /* Defer access checks while parsing the declarator; we cannot know
10512      what names are accessible until we know what is being
10513      declared.  */
10514   resume_deferring_access_checks ();
10515
10516   /* Parse the declarator.  */
10517   declarator
10518     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10519                             &ctor_dtor_or_conv_p,
10520                             /*parenthesized_p=*/NULL);
10521   /* Gather up the deferred checks.  */
10522   stop_deferring_access_checks ();
10523
10524   /* If the DECLARATOR was erroneous, there's no need to go
10525      further.  */
10526   if (declarator == cp_error_declarator)
10527     return error_mark_node;
10528
10529   cp_parser_check_for_definition_in_return_type (declarator,
10530                                                  declares_class_or_enum);
10531
10532   /* Figure out what scope the entity declared by the DECLARATOR is
10533      located in.  `grokdeclarator' sometimes changes the scope, so
10534      we compute it now.  */
10535   scope = get_scope_of_declarator (declarator);
10536
10537   /* If we're allowing GNU extensions, look for an asm-specification
10538      and attributes.  */
10539   if (cp_parser_allow_gnu_extensions_p (parser))
10540     {
10541       /* Look for an asm-specification.  */
10542       asm_specification = cp_parser_asm_specification_opt (parser);
10543       /* And attributes.  */
10544       attributes = cp_parser_attributes_opt (parser);
10545     }
10546   else
10547     {
10548       asm_specification = NULL_TREE;
10549       attributes = NULL_TREE;
10550     }
10551
10552   /* Peek at the next token.  */
10553   token = cp_lexer_peek_token (parser->lexer);
10554   /* Check to see if the token indicates the start of a
10555      function-definition.  */
10556   if (cp_parser_token_starts_function_definition_p (token))
10557     {
10558       if (!function_definition_allowed_p)
10559         {
10560           /* If a function-definition should not appear here, issue an
10561              error message.  */
10562           cp_parser_error (parser,
10563                            "a function-definition is not allowed here");
10564           return error_mark_node;
10565         }
10566       else
10567         {
10568           /* Neither attributes nor an asm-specification are allowed
10569              on a function-definition.  */
10570           if (asm_specification)
10571             error ("an asm-specification is not allowed on a function-definition");
10572           if (attributes)
10573             error ("attributes are not allowed on a function-definition");
10574           /* This is a function-definition.  */
10575           *function_definition_p = true;
10576
10577           /* Parse the function definition.  */
10578           if (member_p)
10579             decl = cp_parser_save_member_function_body (parser,
10580                                                         decl_specifiers,
10581                                                         declarator,
10582                                                         prefix_attributes);
10583           else
10584             decl
10585               = (cp_parser_function_definition_from_specifiers_and_declarator
10586                  (parser, decl_specifiers, prefix_attributes, declarator));
10587
10588           return decl;
10589         }
10590     }
10591
10592   /* [dcl.dcl]
10593
10594      Only in function declarations for constructors, destructors, and
10595      type conversions can the decl-specifier-seq be omitted.
10596
10597      We explicitly postpone this check past the point where we handle
10598      function-definitions because we tolerate function-definitions
10599      that are missing their return types in some modes.  */
10600   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10601     {
10602       cp_parser_error (parser,
10603                        "expected constructor, destructor, or type conversion");
10604       return error_mark_node;
10605     }
10606
10607   /* An `=' or an `(' indicates an initializer.  */
10608   is_initialized = (token->type == CPP_EQ
10609                      || token->type == CPP_OPEN_PAREN);
10610   /* If the init-declarator isn't initialized and isn't followed by a
10611      `,' or `;', it's not a valid init-declarator.  */
10612   if (!is_initialized
10613       && token->type != CPP_COMMA
10614       && token->type != CPP_SEMICOLON)
10615     {
10616       cp_parser_error (parser, "expected init-declarator");
10617       return error_mark_node;
10618     }
10619
10620   /* Because start_decl has side-effects, we should only call it if we
10621      know we're going ahead.  By this point, we know that we cannot
10622      possibly be looking at any other construct.  */
10623   cp_parser_commit_to_tentative_parse (parser);
10624
10625   /* If the decl specifiers were bad, issue an error now that we're
10626      sure this was intended to be a declarator.  Then continue
10627      declaring the variable(s), as int, to try to cut down on further
10628      errors.  */
10629   if (decl_specifiers->any_specifiers_p
10630       && decl_specifiers->type == error_mark_node)
10631     {
10632       cp_parser_error (parser, "invalid type in declaration");
10633       decl_specifiers->type = integer_type_node;
10634     }
10635
10636   /* Check to see whether or not this declaration is a friend.  */
10637   friend_p = cp_parser_friend_p (decl_specifiers);
10638
10639   /* Check that the number of template-parameter-lists is OK.  */
10640   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10641     return error_mark_node;
10642
10643   /* Enter the newly declared entry in the symbol table.  If we're
10644      processing a declaration in a class-specifier, we wait until
10645      after processing the initializer.  */
10646   if (!member_p)
10647     {
10648       if (parser->in_unbraced_linkage_specification_p)
10649         {
10650           decl_specifiers->storage_class = sc_extern;
10651           have_extern_spec = false;
10652         }
10653       decl = start_decl (declarator, decl_specifiers,
10654                          is_initialized, attributes, prefix_attributes,
10655                          &pop_p);
10656     }
10657   else if (scope)
10658     /* Enter the SCOPE.  That way unqualified names appearing in the
10659        initializer will be looked up in SCOPE.  */
10660     pop_p = push_scope (scope);
10661
10662   /* Perform deferred access control checks, now that we know in which
10663      SCOPE the declared entity resides.  */
10664   if (!member_p && decl)
10665     {
10666       tree saved_current_function_decl = NULL_TREE;
10667
10668       /* If the entity being declared is a function, pretend that we
10669          are in its scope.  If it is a `friend', it may have access to
10670          things that would not otherwise be accessible.  */
10671       if (TREE_CODE (decl) == FUNCTION_DECL)
10672         {
10673           saved_current_function_decl = current_function_decl;
10674           current_function_decl = decl;
10675         }
10676
10677       /* Perform the access control checks for the declarator and the
10678          the decl-specifiers.  */
10679       perform_deferred_access_checks ();
10680
10681       /* Restore the saved value.  */
10682       if (TREE_CODE (decl) == FUNCTION_DECL)
10683         current_function_decl = saved_current_function_decl;
10684     }
10685
10686   /* Parse the initializer.  */
10687   if (is_initialized)
10688     initializer = cp_parser_initializer (parser,
10689                                          &is_parenthesized_init,
10690                                          &is_non_constant_init);
10691   else
10692     {
10693       initializer = NULL_TREE;
10694       is_parenthesized_init = false;
10695       is_non_constant_init = true;
10696     }
10697
10698   /* The old parser allows attributes to appear after a parenthesized
10699      initializer.  Mark Mitchell proposed removing this functionality
10700      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10701      attributes -- but ignores them.  */
10702   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10703     if (cp_parser_attributes_opt (parser))
10704       warning ("attributes after parenthesized initializer ignored");
10705
10706   /* For an in-class declaration, use `grokfield' to create the
10707      declaration.  */
10708   if (member_p)
10709     {
10710       if (pop_p)
10711         pop_scope (scope);
10712       decl = grokfield (declarator, decl_specifiers,
10713                         initializer, /*asmspec=*/NULL_TREE,
10714                         /*attributes=*/NULL_TREE);
10715       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10716         cp_parser_save_default_args (parser, decl);
10717     }
10718
10719   /* Finish processing the declaration.  But, skip friend
10720      declarations.  */
10721   if (!friend_p && decl && decl != error_mark_node)
10722     {
10723       cp_finish_decl (decl,
10724                       initializer,
10725                       asm_specification,
10726                       /* If the initializer is in parentheses, then this is
10727                          a direct-initialization, which means that an
10728                          `explicit' constructor is OK.  Otherwise, an
10729                          `explicit' constructor cannot be used.  */
10730                       ((is_parenthesized_init || !is_initialized)
10731                      ? 0 : LOOKUP_ONLYCONVERTING));
10732       if (pop_p)
10733         pop_scope (DECL_CONTEXT (decl));
10734     }
10735
10736   /* Remember whether or not variables were initialized by
10737      constant-expressions.  */
10738   if (decl && TREE_CODE (decl) == VAR_DECL
10739       && is_initialized && !is_non_constant_init)
10740     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10741
10742   return decl;
10743 }
10744
10745 /* Parse a declarator.
10746
10747    declarator:
10748      direct-declarator
10749      ptr-operator declarator
10750
10751    abstract-declarator:
10752      ptr-operator abstract-declarator [opt]
10753      direct-abstract-declarator
10754
10755    GNU Extensions:
10756
10757    declarator:
10758      attributes [opt] direct-declarator
10759      attributes [opt] ptr-operator declarator
10760
10761    abstract-declarator:
10762      attributes [opt] ptr-operator abstract-declarator [opt]
10763      attributes [opt] direct-abstract-declarator
10764
10765    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10766    detect constructor, destructor or conversion operators. It is set
10767    to -1 if the declarator is a name, and +1 if it is a
10768    function. Otherwise it is set to zero. Usually you just want to
10769    test for >0, but internally the negative value is used.
10770
10771    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10772    a decl-specifier-seq unless it declares a constructor, destructor,
10773    or conversion.  It might seem that we could check this condition in
10774    semantic analysis, rather than parsing, but that makes it difficult
10775    to handle something like `f()'.  We want to notice that there are
10776    no decl-specifiers, and therefore realize that this is an
10777    expression, not a declaration.)
10778
10779    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10780    the declarator is a direct-declarator of the form "(...)".  */
10781
10782 static cp_declarator *
10783 cp_parser_declarator (cp_parser* parser,
10784                       cp_parser_declarator_kind dcl_kind,
10785                       int* ctor_dtor_or_conv_p,
10786                       bool* parenthesized_p)
10787 {
10788   cp_token *token;
10789   cp_declarator *declarator;
10790   enum tree_code code;
10791   cp_cv_quals cv_quals;
10792   tree class_type;
10793   tree attributes = NULL_TREE;
10794
10795   /* Assume this is not a constructor, destructor, or type-conversion
10796      operator.  */
10797   if (ctor_dtor_or_conv_p)
10798     *ctor_dtor_or_conv_p = 0;
10799
10800   if (cp_parser_allow_gnu_extensions_p (parser))
10801     attributes = cp_parser_attributes_opt (parser);
10802
10803   /* Peek at the next token.  */
10804   token = cp_lexer_peek_token (parser->lexer);
10805
10806   /* Check for the ptr-operator production.  */
10807   cp_parser_parse_tentatively (parser);
10808   /* Parse the ptr-operator.  */
10809   code = cp_parser_ptr_operator (parser,
10810                                  &class_type,
10811                                  &cv_quals);
10812   /* If that worked, then we have a ptr-operator.  */
10813   if (cp_parser_parse_definitely (parser))
10814     {
10815       /* If a ptr-operator was found, then this declarator was not
10816          parenthesized.  */
10817       if (parenthesized_p)
10818         *parenthesized_p = true;
10819       /* The dependent declarator is optional if we are parsing an
10820          abstract-declarator.  */
10821       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10822         cp_parser_parse_tentatively (parser);
10823
10824       /* Parse the dependent declarator.  */
10825       declarator = cp_parser_declarator (parser, dcl_kind,
10826                                          /*ctor_dtor_or_conv_p=*/NULL,
10827                                          /*parenthesized_p=*/NULL);
10828
10829       /* If we are parsing an abstract-declarator, we must handle the
10830          case where the dependent declarator is absent.  */
10831       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10832           && !cp_parser_parse_definitely (parser))
10833         declarator = NULL;
10834
10835       /* Build the representation of the ptr-operator.  */
10836       if (class_type)
10837         declarator = make_ptrmem_declarator (cv_quals,
10838                                              class_type,
10839                                              declarator);
10840       else if (code == INDIRECT_REF)
10841         declarator = make_pointer_declarator (cv_quals, declarator);
10842       else
10843         declarator = make_reference_declarator (cv_quals, declarator);
10844     }
10845   /* Everything else is a direct-declarator.  */
10846   else
10847     {
10848       if (parenthesized_p)
10849         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10850                                                    CPP_OPEN_PAREN);
10851       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10852                                                 ctor_dtor_or_conv_p);
10853     }
10854
10855   if (attributes && declarator != cp_error_declarator)
10856     declarator->attributes = attributes;
10857
10858   return declarator;
10859 }
10860
10861 /* Parse a direct-declarator or direct-abstract-declarator.
10862
10863    direct-declarator:
10864      declarator-id
10865      direct-declarator ( parameter-declaration-clause )
10866        cv-qualifier-seq [opt]
10867        exception-specification [opt]
10868      direct-declarator [ constant-expression [opt] ]
10869      ( declarator )
10870
10871    direct-abstract-declarator:
10872      direct-abstract-declarator [opt]
10873        ( parameter-declaration-clause )
10874        cv-qualifier-seq [opt]
10875        exception-specification [opt]
10876      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10877      ( abstract-declarator )
10878
10879    Returns a representation of the declarator.  DCL_KIND is
10880    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10881    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10882    we are parsing a direct-declarator.  It is
10883    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10884    of ambiguity we prefer an abstract declarator, as per
10885    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10886    cp_parser_declarator.  */
10887
10888 static cp_declarator *
10889 cp_parser_direct_declarator (cp_parser* parser,
10890                              cp_parser_declarator_kind dcl_kind,
10891                              int* ctor_dtor_or_conv_p)
10892 {
10893   cp_token *token;
10894   cp_declarator *declarator = NULL;
10895   tree scope = NULL_TREE;
10896   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10897   bool saved_in_declarator_p = parser->in_declarator_p;
10898   bool first = true;
10899   bool pop_p = false;
10900
10901   while (true)
10902     {
10903       /* Peek at the next token.  */
10904       token = cp_lexer_peek_token (parser->lexer);
10905       if (token->type == CPP_OPEN_PAREN)
10906         {
10907           /* This is either a parameter-declaration-clause, or a
10908              parenthesized declarator. When we know we are parsing a
10909              named declarator, it must be a parenthesized declarator
10910              if FIRST is true. For instance, `(int)' is a
10911              parameter-declaration-clause, with an omitted
10912              direct-abstract-declarator. But `((*))', is a
10913              parenthesized abstract declarator. Finally, when T is a
10914              template parameter `(T)' is a
10915              parameter-declaration-clause, and not a parenthesized
10916              named declarator.
10917
10918              We first try and parse a parameter-declaration-clause,
10919              and then try a nested declarator (if FIRST is true).
10920
10921              It is not an error for it not to be a
10922              parameter-declaration-clause, even when FIRST is
10923              false. Consider,
10924
10925                int i (int);
10926                int i (3);
10927
10928              The first is the declaration of a function while the
10929              second is a the definition of a variable, including its
10930              initializer.
10931
10932              Having seen only the parenthesis, we cannot know which of
10933              these two alternatives should be selected.  Even more
10934              complex are examples like:
10935
10936                int i (int (a));
10937                int i (int (3));
10938
10939              The former is a function-declaration; the latter is a
10940              variable initialization.
10941
10942              Thus again, we try a parameter-declaration-clause, and if
10943              that fails, we back out and return.  */
10944
10945           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10946             {
10947               cp_parameter_declarator *params;
10948               unsigned saved_num_template_parameter_lists;
10949
10950               cp_parser_parse_tentatively (parser);
10951
10952               /* Consume the `('.  */
10953               cp_lexer_consume_token (parser->lexer);
10954               if (first)
10955                 {
10956                   /* If this is going to be an abstract declarator, we're
10957                      in a declarator and we can't have default args.  */
10958                   parser->default_arg_ok_p = false;
10959                   parser->in_declarator_p = true;
10960                 }
10961
10962               /* Inside the function parameter list, surrounding
10963                  template-parameter-lists do not apply.  */
10964               saved_num_template_parameter_lists
10965                 = parser->num_template_parameter_lists;
10966               parser->num_template_parameter_lists = 0;
10967
10968               /* Parse the parameter-declaration-clause.  */
10969               params = cp_parser_parameter_declaration_clause (parser);
10970
10971               parser->num_template_parameter_lists
10972                 = saved_num_template_parameter_lists;
10973
10974               /* If all went well, parse the cv-qualifier-seq and the
10975                  exception-specification.  */
10976               if (cp_parser_parse_definitely (parser))
10977                 {
10978                   cp_cv_quals cv_quals;
10979                   tree exception_specification;
10980
10981                   if (ctor_dtor_or_conv_p)
10982                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10983                   first = false;
10984                   /* Consume the `)'.  */
10985                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10986
10987                   /* Parse the cv-qualifier-seq.  */
10988                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10989                   /* And the exception-specification.  */
10990                   exception_specification
10991                     = cp_parser_exception_specification_opt (parser);
10992
10993                   /* Create the function-declarator.  */
10994                   declarator = make_call_declarator (declarator,
10995                                                      params,
10996                                                      cv_quals,
10997                                                      exception_specification);
10998                   /* Any subsequent parameter lists are to do with
10999                      return type, so are not those of the declared
11000                      function.  */
11001                   parser->default_arg_ok_p = false;
11002
11003                   /* Repeat the main loop.  */
11004                   continue;
11005                 }
11006             }
11007
11008           /* If this is the first, we can try a parenthesized
11009              declarator.  */
11010           if (first)
11011             {
11012               bool saved_in_type_id_in_expr_p;
11013
11014               parser->default_arg_ok_p = saved_default_arg_ok_p;
11015               parser->in_declarator_p = saved_in_declarator_p;
11016
11017               /* Consume the `('.  */
11018               cp_lexer_consume_token (parser->lexer);
11019               /* Parse the nested declarator.  */
11020               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11021               parser->in_type_id_in_expr_p = true;
11022               declarator
11023                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11024                                         /*parenthesized_p=*/NULL);
11025               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11026               first = false;
11027               /* Expect a `)'.  */
11028               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11029                 declarator = cp_error_declarator;
11030               if (declarator == cp_error_declarator)
11031                 break;
11032
11033               goto handle_declarator;
11034             }
11035           /* Otherwise, we must be done.  */
11036           else
11037             break;
11038         }
11039       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11040                && token->type == CPP_OPEN_SQUARE)
11041         {
11042           /* Parse an array-declarator.  */
11043           tree bounds;
11044
11045           if (ctor_dtor_or_conv_p)
11046             *ctor_dtor_or_conv_p = 0;
11047
11048           first = false;
11049           parser->default_arg_ok_p = false;
11050           parser->in_declarator_p = true;
11051           /* Consume the `['.  */
11052           cp_lexer_consume_token (parser->lexer);
11053           /* Peek at the next token.  */
11054           token = cp_lexer_peek_token (parser->lexer);
11055           /* If the next token is `]', then there is no
11056              constant-expression.  */
11057           if (token->type != CPP_CLOSE_SQUARE)
11058             {
11059               bool non_constant_p;
11060
11061               bounds
11062                 = cp_parser_constant_expression (parser,
11063                                                  /*allow_non_constant=*/true,
11064                                                  &non_constant_p);
11065               if (!non_constant_p)
11066                 bounds = fold_non_dependent_expr (bounds);
11067             }
11068           else
11069             bounds = NULL_TREE;
11070           /* Look for the closing `]'.  */
11071           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11072             {
11073               declarator = cp_error_declarator;
11074               break;
11075             }
11076
11077           declarator = make_array_declarator (declarator, bounds);
11078         }
11079       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11080         {
11081           tree id;
11082
11083           /* Parse a declarator-id */
11084           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11085             cp_parser_parse_tentatively (parser);
11086           id = cp_parser_declarator_id (parser);
11087           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11088             {
11089               if (!cp_parser_parse_definitely (parser))
11090                 id = error_mark_node;
11091               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11092                 {
11093                   cp_parser_error (parser, "expected unqualified-id");
11094                   id = error_mark_node;
11095                 }
11096             }
11097
11098           if (id == error_mark_node)
11099             {
11100               declarator = cp_error_declarator;
11101               break;
11102             }
11103
11104           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11105             {
11106               tree scope = TREE_OPERAND (id, 0);
11107
11108               /* In the declaration of a member of a template class
11109                  outside of the class itself, the SCOPE will sometimes
11110                  be a TYPENAME_TYPE.  For example, given:
11111
11112                  template <typename T>
11113                  int S<T>::R::i = 3;
11114
11115                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11116                  this context, we must resolve S<T>::R to an ordinary
11117                  type, rather than a typename type.
11118
11119                  The reason we normally avoid resolving TYPENAME_TYPEs
11120                  is that a specialization of `S' might render
11121                  `S<T>::R' not a type.  However, if `S' is
11122                  specialized, then this `i' will not be used, so there
11123                  is no harm in resolving the types here.  */
11124               if (TREE_CODE (scope) == TYPENAME_TYPE)
11125                 {
11126                   tree type;
11127
11128                   /* Resolve the TYPENAME_TYPE.  */
11129                   type = resolve_typename_type (scope,
11130                                                  /*only_current_p=*/false);
11131                   /* If that failed, the declarator is invalid.  */
11132                   if (type == error_mark_node)
11133                     error ("`%T::%D' is not a type",
11134                            TYPE_CONTEXT (scope),
11135                            TYPE_IDENTIFIER (scope));
11136                   /* Build a new DECLARATOR.  */
11137                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11138                 }
11139             }
11140
11141           declarator = make_id_declarator (id);
11142           if (id)
11143             {
11144               tree class_type;
11145               tree unqualified_name;
11146
11147               if (TREE_CODE (id) == SCOPE_REF
11148                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11149                 {
11150                   class_type = TREE_OPERAND (id, 0);
11151                   unqualified_name = TREE_OPERAND (id, 1);
11152                 }
11153               else
11154                 {
11155                   class_type = current_class_type;
11156                   unqualified_name = id;
11157                 }
11158
11159               if (class_type)
11160                 {
11161                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11162                     declarator->u.id.sfk = sfk_destructor;
11163                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11164                     declarator->u.id.sfk = sfk_conversion;
11165                   else if (constructor_name_p (unqualified_name,
11166                                                class_type)
11167                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11168                                && same_type_p (TREE_TYPE (unqualified_name),
11169                                                class_type)))
11170                     declarator->u.id.sfk = sfk_constructor;
11171
11172                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11173                     *ctor_dtor_or_conv_p = -1;
11174                   if (TREE_CODE (id) == SCOPE_REF
11175                       && TREE_CODE (unqualified_name) == TYPE_DECL
11176                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11177                     {
11178                       error ("invalid use of constructor as a template");
11179                       inform ("use `%T::%D' instead of `%T::%T' to name the "
11180                               "constructor in a qualified name", class_type,
11181                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11182                               class_type, class_type);
11183                     }
11184                 }
11185             }
11186
11187         handle_declarator:;
11188           scope = get_scope_of_declarator (declarator);
11189           if (scope)
11190             /* Any names that appear after the declarator-id for a
11191                member are looked up in the containing scope.  */
11192             pop_p = push_scope (scope);
11193           parser->in_declarator_p = true;
11194           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11195               || (declarator && declarator->kind == cdk_id))
11196             /* Default args are only allowed on function
11197                declarations.  */
11198             parser->default_arg_ok_p = saved_default_arg_ok_p;
11199           else
11200             parser->default_arg_ok_p = false;
11201
11202           first = false;
11203         }
11204       /* We're done.  */
11205       else
11206         break;
11207     }
11208
11209   /* For an abstract declarator, we might wind up with nothing at this
11210      point.  That's an error; the declarator is not optional.  */
11211   if (!declarator)
11212     cp_parser_error (parser, "expected declarator");
11213
11214   /* If we entered a scope, we must exit it now.  */
11215   if (pop_p)
11216     pop_scope (scope);
11217
11218   parser->default_arg_ok_p = saved_default_arg_ok_p;
11219   parser->in_declarator_p = saved_in_declarator_p;
11220
11221   return declarator;
11222 }
11223
11224 /* Parse a ptr-operator.
11225
11226    ptr-operator:
11227      * cv-qualifier-seq [opt]
11228      &
11229      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11230
11231    GNU Extension:
11232
11233    ptr-operator:
11234      & cv-qualifier-seq [opt]
11235
11236    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11237    Returns ADDR_EXPR if a reference was used.  In the case of a
11238    pointer-to-member, *TYPE is filled in with the TYPE containing the
11239    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11240    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11241    ERROR_MARK if an error occurred.  */
11242
11243 static enum tree_code
11244 cp_parser_ptr_operator (cp_parser* parser,
11245                         tree* type,
11246                         cp_cv_quals *cv_quals)
11247 {
11248   enum tree_code code = ERROR_MARK;
11249   cp_token *token;
11250
11251   /* Assume that it's not a pointer-to-member.  */
11252   *type = NULL_TREE;
11253   /* And that there are no cv-qualifiers.  */
11254   *cv_quals = TYPE_UNQUALIFIED;
11255
11256   /* Peek at the next token.  */
11257   token = cp_lexer_peek_token (parser->lexer);
11258   /* If it's a `*' or `&' we have a pointer or reference.  */
11259   if (token->type == CPP_MULT || token->type == CPP_AND)
11260     {
11261       /* Remember which ptr-operator we were processing.  */
11262       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11263
11264       /* Consume the `*' or `&'.  */
11265       cp_lexer_consume_token (parser->lexer);
11266
11267       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11268          `&', if we are allowing GNU extensions.  (The only qualifier
11269          that can legally appear after `&' is `restrict', but that is
11270          enforced during semantic analysis.  */
11271       if (code == INDIRECT_REF
11272           || cp_parser_allow_gnu_extensions_p (parser))
11273         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11274     }
11275   else
11276     {
11277       /* Try the pointer-to-member case.  */
11278       cp_parser_parse_tentatively (parser);
11279       /* Look for the optional `::' operator.  */
11280       cp_parser_global_scope_opt (parser,
11281                                   /*current_scope_valid_p=*/false);
11282       /* Look for the nested-name specifier.  */
11283       cp_parser_nested_name_specifier (parser,
11284                                        /*typename_keyword_p=*/false,
11285                                        /*check_dependency_p=*/true,
11286                                        /*type_p=*/false,
11287                                        /*is_declaration=*/false);
11288       /* If we found it, and the next token is a `*', then we are
11289          indeed looking at a pointer-to-member operator.  */
11290       if (!cp_parser_error_occurred (parser)
11291           && cp_parser_require (parser, CPP_MULT, "`*'"))
11292         {
11293           /* The type of which the member is a member is given by the
11294              current SCOPE.  */
11295           *type = parser->scope;
11296           /* The next name will not be qualified.  */
11297           parser->scope = NULL_TREE;
11298           parser->qualifying_scope = NULL_TREE;
11299           parser->object_scope = NULL_TREE;
11300           /* Indicate that the `*' operator was used.  */
11301           code = INDIRECT_REF;
11302           /* Look for the optional cv-qualifier-seq.  */
11303           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11304         }
11305       /* If that didn't work we don't have a ptr-operator.  */
11306       if (!cp_parser_parse_definitely (parser))
11307         cp_parser_error (parser, "expected ptr-operator");
11308     }
11309
11310   return code;
11311 }
11312
11313 /* Parse an (optional) cv-qualifier-seq.
11314
11315    cv-qualifier-seq:
11316      cv-qualifier cv-qualifier-seq [opt]
11317
11318    cv-qualifier:
11319      const
11320      volatile
11321
11322    GNU Extension:
11323
11324    cv-qualifier:
11325      __restrict__
11326
11327    Returns a bitmask representing the cv-qualifiers.  */
11328
11329 static cp_cv_quals
11330 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11331 {
11332   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11333
11334   while (true)
11335     {
11336       cp_token *token;
11337       cp_cv_quals cv_qualifier;
11338
11339       /* Peek at the next token.  */
11340       token = cp_lexer_peek_token (parser->lexer);
11341       /* See if it's a cv-qualifier.  */
11342       switch (token->keyword)
11343         {
11344         case RID_CONST:
11345           cv_qualifier = TYPE_QUAL_CONST;
11346           break;
11347
11348         case RID_VOLATILE:
11349           cv_qualifier = TYPE_QUAL_VOLATILE;
11350           break;
11351
11352         case RID_RESTRICT:
11353           cv_qualifier = TYPE_QUAL_RESTRICT;
11354           break;
11355
11356         default:
11357           cv_qualifier = TYPE_UNQUALIFIED;
11358           break;
11359         }
11360
11361       if (!cv_qualifier)
11362         break;
11363
11364       if (cv_quals & cv_qualifier)
11365         {
11366           error ("duplicate cv-qualifier");
11367           cp_lexer_purge_token (parser->lexer);
11368         }
11369       else
11370         {
11371           cp_lexer_consume_token (parser->lexer);
11372           cv_quals |= cv_qualifier;
11373         }
11374     }
11375
11376   return cv_quals;
11377 }
11378
11379 /* Parse a declarator-id.
11380
11381    declarator-id:
11382      id-expression
11383      :: [opt] nested-name-specifier [opt] type-name
11384
11385    In the `id-expression' case, the value returned is as for
11386    cp_parser_id_expression if the id-expression was an unqualified-id.
11387    If the id-expression was a qualified-id, then a SCOPE_REF is
11388    returned.  The first operand is the scope (either a NAMESPACE_DECL
11389    or TREE_TYPE), but the second is still just a representation of an
11390    unqualified-id.  */
11391
11392 static tree
11393 cp_parser_declarator_id (cp_parser* parser)
11394 {
11395   tree id_expression;
11396
11397   /* The expression must be an id-expression.  Assume that qualified
11398      names are the names of types so that:
11399
11400        template <class T>
11401        int S<T>::R::i = 3;
11402
11403      will work; we must treat `S<T>::R' as the name of a type.
11404      Similarly, assume that qualified names are templates, where
11405      required, so that:
11406
11407        template <class T>
11408        int S<T>::R<T>::i = 3;
11409
11410      will work, too.  */
11411   id_expression = cp_parser_id_expression (parser,
11412                                            /*template_keyword_p=*/false,
11413                                            /*check_dependency_p=*/false,
11414                                            /*template_p=*/NULL,
11415                                            /*declarator_p=*/true);
11416   /* If the name was qualified, create a SCOPE_REF to represent
11417      that.  */
11418   if (parser->scope)
11419     {
11420       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11421       parser->scope = NULL_TREE;
11422     }
11423
11424   return id_expression;
11425 }
11426
11427 /* Parse a type-id.
11428
11429    type-id:
11430      type-specifier-seq abstract-declarator [opt]
11431
11432    Returns the TYPE specified.  */
11433
11434 static tree
11435 cp_parser_type_id (cp_parser* parser)
11436 {
11437   cp_decl_specifier_seq type_specifier_seq;
11438   cp_declarator *abstract_declarator;
11439
11440   /* Parse the type-specifier-seq.  */
11441   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11442   if (type_specifier_seq.type == error_mark_node)
11443     return error_mark_node;
11444
11445   /* There might or might not be an abstract declarator.  */
11446   cp_parser_parse_tentatively (parser);
11447   /* Look for the declarator.  */
11448   abstract_declarator
11449     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11450                             /*parenthesized_p=*/NULL);
11451   /* Check to see if there really was a declarator.  */
11452   if (!cp_parser_parse_definitely (parser))
11453     abstract_declarator = NULL;
11454
11455   return groktypename (&type_specifier_seq, abstract_declarator);
11456 }
11457
11458 /* Parse a type-specifier-seq.
11459
11460    type-specifier-seq:
11461      type-specifier type-specifier-seq [opt]
11462
11463    GNU extension:
11464
11465    type-specifier-seq:
11466      attributes type-specifier-seq [opt]
11467
11468    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11469
11470 static void
11471 cp_parser_type_specifier_seq (cp_parser* parser,
11472                               cp_decl_specifier_seq *type_specifier_seq)
11473 {
11474   bool seen_type_specifier = false;
11475
11476   /* Clear the TYPE_SPECIFIER_SEQ.  */
11477   clear_decl_specs (type_specifier_seq);
11478
11479   /* Parse the type-specifiers and attributes.  */
11480   while (true)
11481     {
11482       tree type_specifier;
11483
11484       /* Check for attributes first.  */
11485       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11486         {
11487           type_specifier_seq->attributes =
11488             chainon (type_specifier_seq->attributes,
11489                      cp_parser_attributes_opt (parser));
11490           continue;
11491         }
11492
11493       /* Look for the type-specifier.  */
11494       type_specifier = cp_parser_type_specifier (parser,
11495                                                  CP_PARSER_FLAGS_OPTIONAL,
11496                                                  type_specifier_seq,
11497                                                  /*is_declaration=*/false,
11498                                                  NULL,
11499                                                  NULL);
11500       /* If the first type-specifier could not be found, this is not a
11501          type-specifier-seq at all.  */
11502       if (!seen_type_specifier && !type_specifier)
11503         {
11504           cp_parser_error (parser, "expected type-specifier");
11505           type_specifier_seq->type = error_mark_node;
11506           return;
11507         }
11508       /* If subsequent type-specifiers could not be found, the
11509          type-specifier-seq is complete.  */
11510       else if (seen_type_specifier && !type_specifier)
11511         break;
11512
11513       seen_type_specifier = true;
11514     }
11515
11516   return;
11517 }
11518
11519 /* Parse a parameter-declaration-clause.
11520
11521    parameter-declaration-clause:
11522      parameter-declaration-list [opt] ... [opt]
11523      parameter-declaration-list , ...
11524
11525    Returns a representation for the parameter declarations.  A return
11526    value of NULL indicates a parameter-declaration-clause consisting
11527    only of an ellipsis.  */
11528
11529 static cp_parameter_declarator *
11530 cp_parser_parameter_declaration_clause (cp_parser* parser)
11531 {
11532   cp_parameter_declarator *parameters;
11533   cp_token *token;
11534   bool ellipsis_p;
11535   bool is_error;
11536
11537   /* Peek at the next token.  */
11538   token = cp_lexer_peek_token (parser->lexer);
11539   /* Check for trivial parameter-declaration-clauses.  */
11540   if (token->type == CPP_ELLIPSIS)
11541     {
11542       /* Consume the `...' token.  */
11543       cp_lexer_consume_token (parser->lexer);
11544       return NULL;
11545     }
11546   else if (token->type == CPP_CLOSE_PAREN)
11547     /* There are no parameters.  */
11548     {
11549 #ifndef NO_IMPLICIT_EXTERN_C
11550       if (in_system_header && current_class_type == NULL
11551           && current_lang_name == lang_name_c)
11552         return NULL;
11553       else
11554 #endif
11555         return no_parameters;
11556     }
11557   /* Check for `(void)', too, which is a special case.  */
11558   else if (token->keyword == RID_VOID
11559            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11560                == CPP_CLOSE_PAREN))
11561     {
11562       /* Consume the `void' token.  */
11563       cp_lexer_consume_token (parser->lexer);
11564       /* There are no parameters.  */
11565       return no_parameters;
11566     }
11567
11568   /* Parse the parameter-declaration-list.  */
11569   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11570   /* If a parse error occurred while parsing the
11571      parameter-declaration-list, then the entire
11572      parameter-declaration-clause is erroneous.  */
11573   if (is_error)
11574     return NULL;
11575
11576   /* Peek at the next token.  */
11577   token = cp_lexer_peek_token (parser->lexer);
11578   /* If it's a `,', the clause should terminate with an ellipsis.  */
11579   if (token->type == CPP_COMMA)
11580     {
11581       /* Consume the `,'.  */
11582       cp_lexer_consume_token (parser->lexer);
11583       /* Expect an ellipsis.  */
11584       ellipsis_p
11585         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11586     }
11587   /* It might also be `...' if the optional trailing `,' was
11588      omitted.  */
11589   else if (token->type == CPP_ELLIPSIS)
11590     {
11591       /* Consume the `...' token.  */
11592       cp_lexer_consume_token (parser->lexer);
11593       /* And remember that we saw it.  */
11594       ellipsis_p = true;
11595     }
11596   else
11597     ellipsis_p = false;
11598
11599   /* Finish the parameter list.  */
11600   if (parameters && ellipsis_p)
11601     parameters->ellipsis_p = true;
11602
11603   return parameters;
11604 }
11605
11606 /* Parse a parameter-declaration-list.
11607
11608    parameter-declaration-list:
11609      parameter-declaration
11610      parameter-declaration-list , parameter-declaration
11611
11612    Returns a representation of the parameter-declaration-list, as for
11613    cp_parser_parameter_declaration_clause.  However, the
11614    `void_list_node' is never appended to the list.  Upon return,
11615    *IS_ERROR will be true iff an error occurred.  */
11616
11617 static cp_parameter_declarator *
11618 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11619 {
11620   cp_parameter_declarator *parameters = NULL;
11621   cp_parameter_declarator **tail = &parameters;
11622
11623   /* Assume all will go well.  */
11624   *is_error = false;
11625
11626   /* Look for more parameters.  */
11627   while (true)
11628     {
11629       cp_parameter_declarator *parameter;
11630       bool parenthesized_p;
11631       /* Parse the parameter.  */
11632       parameter
11633         = cp_parser_parameter_declaration (parser,
11634                                            /*template_parm_p=*/false,
11635                                            &parenthesized_p);
11636
11637       /* If a parse error occurred parsing the parameter declaration,
11638          then the entire parameter-declaration-list is erroneous.  */
11639       if (!parameter)
11640         {
11641           *is_error = true;
11642           parameters = NULL;
11643           break;
11644         }
11645       /* Add the new parameter to the list.  */
11646       *tail = parameter;
11647       tail = &parameter->next;
11648
11649       /* Peek at the next token.  */
11650       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11651           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11652         /* The parameter-declaration-list is complete.  */
11653         break;
11654       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11655         {
11656           cp_token *token;
11657
11658           /* Peek at the next token.  */
11659           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11660           /* If it's an ellipsis, then the list is complete.  */
11661           if (token->type == CPP_ELLIPSIS)
11662             break;
11663           /* Otherwise, there must be more parameters.  Consume the
11664              `,'.  */
11665           cp_lexer_consume_token (parser->lexer);
11666           /* When parsing something like:
11667
11668                 int i(float f, double d)
11669
11670              we can tell after seeing the declaration for "f" that we
11671              are not looking at an initialization of a variable "i",
11672              but rather at the declaration of a function "i".
11673
11674              Due to the fact that the parsing of template arguments
11675              (as specified to a template-id) requires backtracking we
11676              cannot use this technique when inside a template argument
11677              list.  */
11678           if (!parser->in_template_argument_list_p
11679               && !parser->in_type_id_in_expr_p
11680               && cp_parser_parsing_tentatively (parser)
11681               && !cp_parser_committed_to_tentative_parse (parser)
11682               /* However, a parameter-declaration of the form
11683                  "foat(f)" (which is a valid declaration of a
11684                  parameter "f") can also be interpreted as an
11685                  expression (the conversion of "f" to "float").  */
11686               && !parenthesized_p)
11687             cp_parser_commit_to_tentative_parse (parser);
11688         }
11689       else
11690         {
11691           cp_parser_error (parser, "expected `,' or `...'");
11692           if (!cp_parser_parsing_tentatively (parser)
11693               || cp_parser_committed_to_tentative_parse (parser))
11694             cp_parser_skip_to_closing_parenthesis (parser,
11695                                                    /*recovering=*/true,
11696                                                    /*or_comma=*/false,
11697                                                    /*consume_paren=*/false);
11698           break;
11699         }
11700     }
11701
11702   return parameters;
11703 }
11704
11705 /* Parse a parameter declaration.
11706
11707    parameter-declaration:
11708      decl-specifier-seq declarator
11709      decl-specifier-seq declarator = assignment-expression
11710      decl-specifier-seq abstract-declarator [opt]
11711      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11712
11713    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11714    declares a template parameter.  (In that case, a non-nested `>'
11715    token encountered during the parsing of the assignment-expression
11716    is not interpreted as a greater-than operator.)
11717
11718    Returns a representation of the parameter, or NULL if an error
11719    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11720    true iff the declarator is of the form "(p)".  */
11721
11722 static cp_parameter_declarator *
11723 cp_parser_parameter_declaration (cp_parser *parser,
11724                                  bool template_parm_p,
11725                                  bool *parenthesized_p)
11726 {
11727   int declares_class_or_enum;
11728   bool greater_than_is_operator_p;
11729   cp_decl_specifier_seq decl_specifiers;
11730   cp_declarator *declarator;
11731   tree default_argument;
11732   cp_token *token;
11733   const char *saved_message;
11734
11735   /* In a template parameter, `>' is not an operator.
11736
11737      [temp.param]
11738
11739      When parsing a default template-argument for a non-type
11740      template-parameter, the first non-nested `>' is taken as the end
11741      of the template parameter-list rather than a greater-than
11742      operator.  */
11743   greater_than_is_operator_p = !template_parm_p;
11744
11745   /* Type definitions may not appear in parameter types.  */
11746   saved_message = parser->type_definition_forbidden_message;
11747   parser->type_definition_forbidden_message
11748     = "types may not be defined in parameter types";
11749
11750   /* Parse the declaration-specifiers.  */
11751   cp_parser_decl_specifier_seq (parser,
11752                                 CP_PARSER_FLAGS_NONE,
11753                                 &decl_specifiers,
11754                                 &declares_class_or_enum);
11755   /* If an error occurred, there's no reason to attempt to parse the
11756      rest of the declaration.  */
11757   if (cp_parser_error_occurred (parser))
11758     {
11759       parser->type_definition_forbidden_message = saved_message;
11760       return NULL;
11761     }
11762
11763   /* Peek at the next token.  */
11764   token = cp_lexer_peek_token (parser->lexer);
11765   /* If the next token is a `)', `,', `=', `>', or `...', then there
11766      is no declarator.  */
11767   if (token->type == CPP_CLOSE_PAREN
11768       || token->type == CPP_COMMA
11769       || token->type == CPP_EQ
11770       || token->type == CPP_ELLIPSIS
11771       || token->type == CPP_GREATER)
11772     {
11773       declarator = NULL;
11774       if (parenthesized_p)
11775         *parenthesized_p = false;
11776     }
11777   /* Otherwise, there should be a declarator.  */
11778   else
11779     {
11780       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11781       parser->default_arg_ok_p = false;
11782
11783       /* After seeing a decl-specifier-seq, if the next token is not a
11784          "(", there is no possibility that the code is a valid
11785          expression.  Therefore, if parsing tentatively, we commit at
11786          this point.  */
11787       if (!parser->in_template_argument_list_p
11788           /* In an expression context, having seen:
11789
11790                (int((char ...
11791
11792              we cannot be sure whether we are looking at a
11793              function-type (taking a "char" as a parameter) or a cast
11794              of some object of type "char" to "int".  */
11795           && !parser->in_type_id_in_expr_p
11796           && cp_parser_parsing_tentatively (parser)
11797           && !cp_parser_committed_to_tentative_parse (parser)
11798           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11799         cp_parser_commit_to_tentative_parse (parser);
11800       /* Parse the declarator.  */
11801       declarator = cp_parser_declarator (parser,
11802                                          CP_PARSER_DECLARATOR_EITHER,
11803                                          /*ctor_dtor_or_conv_p=*/NULL,
11804                                          parenthesized_p);
11805       parser->default_arg_ok_p = saved_default_arg_ok_p;
11806       /* After the declarator, allow more attributes.  */
11807       decl_specifiers.attributes
11808         = chainon (decl_specifiers.attributes,
11809                    cp_parser_attributes_opt (parser));
11810     }
11811
11812   /* The restriction on defining new types applies only to the type
11813      of the parameter, not to the default argument.  */
11814   parser->type_definition_forbidden_message = saved_message;
11815
11816   /* If the next token is `=', then process a default argument.  */
11817   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11818     {
11819       bool saved_greater_than_is_operator_p;
11820       /* Consume the `='.  */
11821       cp_lexer_consume_token (parser->lexer);
11822
11823       /* If we are defining a class, then the tokens that make up the
11824          default argument must be saved and processed later.  */
11825       if (!template_parm_p && at_class_scope_p ()
11826           && TYPE_BEING_DEFINED (current_class_type))
11827         {
11828           unsigned depth = 0;
11829
11830           /* Create a DEFAULT_ARG to represented the unparsed default
11831              argument.  */
11832           default_argument = make_node (DEFAULT_ARG);
11833           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11834
11835           /* Add tokens until we have processed the entire default
11836              argument.  */
11837           while (true)
11838             {
11839               bool done = false;
11840               cp_token *token;
11841
11842               /* Peek at the next token.  */
11843               token = cp_lexer_peek_token (parser->lexer);
11844               /* What we do depends on what token we have.  */
11845               switch (token->type)
11846                 {
11847                   /* In valid code, a default argument must be
11848                      immediately followed by a `,' `)', or `...'.  */
11849                 case CPP_COMMA:
11850                 case CPP_CLOSE_PAREN:
11851                 case CPP_ELLIPSIS:
11852                   /* If we run into a non-nested `;', `}', or `]',
11853                      then the code is invalid -- but the default
11854                      argument is certainly over.  */
11855                 case CPP_SEMICOLON:
11856                 case CPP_CLOSE_BRACE:
11857                 case CPP_CLOSE_SQUARE:
11858                   if (depth == 0)
11859                     done = true;
11860                   /* Update DEPTH, if necessary.  */
11861                   else if (token->type == CPP_CLOSE_PAREN
11862                            || token->type == CPP_CLOSE_BRACE
11863                            || token->type == CPP_CLOSE_SQUARE)
11864                     --depth;
11865                   break;
11866
11867                 case CPP_OPEN_PAREN:
11868                 case CPP_OPEN_SQUARE:
11869                 case CPP_OPEN_BRACE:
11870                   ++depth;
11871                   break;
11872
11873                 case CPP_GREATER:
11874                   /* If we see a non-nested `>', and `>' is not an
11875                      operator, then it marks the end of the default
11876                      argument.  */
11877                   if (!depth && !greater_than_is_operator_p)
11878                     done = true;
11879                   break;
11880
11881                   /* If we run out of tokens, issue an error message.  */
11882                 case CPP_EOF:
11883                   error ("file ends in default argument");
11884                   done = true;
11885                   break;
11886
11887                 case CPP_NAME:
11888                 case CPP_SCOPE:
11889                   /* In these cases, we should look for template-ids.
11890                      For example, if the default argument is
11891                      `X<int, double>()', we need to do name lookup to
11892                      figure out whether or not `X' is a template; if
11893                      so, the `,' does not end the default argument.
11894
11895                      That is not yet done.  */
11896                   break;
11897
11898                 default:
11899                   break;
11900                 }
11901
11902               /* If we've reached the end, stop.  */
11903               if (done)
11904                 break;
11905
11906               /* Add the token to the token block.  */
11907               token = cp_lexer_consume_token (parser->lexer);
11908               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11909                                          token);
11910             }
11911         }
11912       /* Outside of a class definition, we can just parse the
11913          assignment-expression.  */
11914       else
11915         {
11916           bool saved_local_variables_forbidden_p;
11917
11918           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11919              set correctly.  */
11920           saved_greater_than_is_operator_p
11921             = parser->greater_than_is_operator_p;
11922           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11923           /* Local variable names (and the `this' keyword) may not
11924              appear in a default argument.  */
11925           saved_local_variables_forbidden_p
11926             = parser->local_variables_forbidden_p;
11927           parser->local_variables_forbidden_p = true;
11928           /* Parse the assignment-expression.  */
11929           default_argument = cp_parser_assignment_expression (parser);
11930           /* Restore saved state.  */
11931           parser->greater_than_is_operator_p
11932             = saved_greater_than_is_operator_p;
11933           parser->local_variables_forbidden_p
11934             = saved_local_variables_forbidden_p;
11935         }
11936       if (!parser->default_arg_ok_p)
11937         {
11938           if (!flag_pedantic_errors)
11939             warning ("deprecated use of default argument for parameter of non-function");
11940           else
11941             {
11942               error ("default arguments are only permitted for function parameters");
11943               default_argument = NULL_TREE;
11944             }
11945         }
11946     }
11947   else
11948     default_argument = NULL_TREE;
11949
11950   return make_parameter_declarator (&decl_specifiers,
11951                                     declarator,
11952                                     default_argument);
11953 }
11954
11955 /* Parse a function-body.
11956
11957    function-body:
11958      compound_statement  */
11959
11960 static void
11961 cp_parser_function_body (cp_parser *parser)
11962 {
11963   cp_parser_compound_statement (parser, NULL, false);
11964 }
11965
11966 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11967    true if a ctor-initializer was present.  */
11968
11969 static bool
11970 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11971 {
11972   tree body;
11973   bool ctor_initializer_p;
11974
11975   /* Begin the function body.  */
11976   body = begin_function_body ();
11977   /* Parse the optional ctor-initializer.  */
11978   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11979   /* Parse the function-body.  */
11980   cp_parser_function_body (parser);
11981   /* Finish the function body.  */
11982   finish_function_body (body);
11983
11984   return ctor_initializer_p;
11985 }
11986
11987 /* Parse an initializer.
11988
11989    initializer:
11990      = initializer-clause
11991      ( expression-list )
11992
11993    Returns a expression representing the initializer.  If no
11994    initializer is present, NULL_TREE is returned.
11995
11996    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11997    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11998    set to FALSE if there is no initializer present.  If there is an
11999    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12000    is set to true; otherwise it is set to false.  */
12001
12002 static tree
12003 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12004                        bool* non_constant_p)
12005 {
12006   cp_token *token;
12007   tree init;
12008
12009   /* Peek at the next token.  */
12010   token = cp_lexer_peek_token (parser->lexer);
12011
12012   /* Let our caller know whether or not this initializer was
12013      parenthesized.  */
12014   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12015   /* Assume that the initializer is constant.  */
12016   *non_constant_p = false;
12017
12018   if (token->type == CPP_EQ)
12019     {
12020       /* Consume the `='.  */
12021       cp_lexer_consume_token (parser->lexer);
12022       /* Parse the initializer-clause.  */
12023       init = cp_parser_initializer_clause (parser, non_constant_p);
12024     }
12025   else if (token->type == CPP_OPEN_PAREN)
12026     init = cp_parser_parenthesized_expression_list (parser, false,
12027                                                     non_constant_p);
12028   else
12029     {
12030       /* Anything else is an error.  */
12031       cp_parser_error (parser, "expected initializer");
12032       init = error_mark_node;
12033     }
12034
12035   return init;
12036 }
12037
12038 /* Parse an initializer-clause.
12039
12040    initializer-clause:
12041      assignment-expression
12042      { initializer-list , [opt] }
12043      { }
12044
12045    Returns an expression representing the initializer.
12046
12047    If the `assignment-expression' production is used the value
12048    returned is simply a representation for the expression.
12049
12050    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12051    the elements of the initializer-list (or NULL_TREE, if the last
12052    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12053    NULL_TREE.  There is no way to detect whether or not the optional
12054    trailing `,' was provided.  NON_CONSTANT_P is as for
12055    cp_parser_initializer.  */
12056
12057 static tree
12058 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12059 {
12060   tree initializer;
12061
12062   /* If it is not a `{', then we are looking at an
12063      assignment-expression.  */
12064   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12065     {
12066       initializer
12067         = cp_parser_constant_expression (parser,
12068                                         /*allow_non_constant_p=*/true,
12069                                         non_constant_p);
12070       if (!*non_constant_p)
12071         initializer = fold_non_dependent_expr (initializer);
12072     }
12073   else
12074     {
12075       /* Consume the `{' token.  */
12076       cp_lexer_consume_token (parser->lexer);
12077       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12078       initializer = make_node (CONSTRUCTOR);
12079       /* If it's not a `}', then there is a non-trivial initializer.  */
12080       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12081         {
12082           /* Parse the initializer list.  */
12083           CONSTRUCTOR_ELTS (initializer)
12084             = cp_parser_initializer_list (parser, non_constant_p);
12085           /* A trailing `,' token is allowed.  */
12086           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12087             cp_lexer_consume_token (parser->lexer);
12088         }
12089       /* Now, there should be a trailing `}'.  */
12090       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12091     }
12092
12093   return initializer;
12094 }
12095
12096 /* Parse an initializer-list.
12097
12098    initializer-list:
12099      initializer-clause
12100      initializer-list , initializer-clause
12101
12102    GNU Extension:
12103
12104    initializer-list:
12105      identifier : initializer-clause
12106      initializer-list, identifier : initializer-clause
12107
12108    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12109    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12110    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12111    as for cp_parser_initializer.  */
12112
12113 static tree
12114 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12115 {
12116   tree initializers = NULL_TREE;
12117
12118   /* Assume all of the expressions are constant.  */
12119   *non_constant_p = false;
12120
12121   /* Parse the rest of the list.  */
12122   while (true)
12123     {
12124       cp_token *token;
12125       tree identifier;
12126       tree initializer;
12127       bool clause_non_constant_p;
12128
12129       /* If the next token is an identifier and the following one is a
12130          colon, we are looking at the GNU designated-initializer
12131          syntax.  */
12132       if (cp_parser_allow_gnu_extensions_p (parser)
12133           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12134           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12135         {
12136           /* Consume the identifier.  */
12137           identifier = cp_lexer_consume_token (parser->lexer)->value;
12138           /* Consume the `:'.  */
12139           cp_lexer_consume_token (parser->lexer);
12140         }
12141       else
12142         identifier = NULL_TREE;
12143
12144       /* Parse the initializer.  */
12145       initializer = cp_parser_initializer_clause (parser,
12146                                                   &clause_non_constant_p);
12147       /* If any clause is non-constant, so is the entire initializer.  */
12148       if (clause_non_constant_p)
12149         *non_constant_p = true;
12150       /* Add it to the list.  */
12151       initializers = tree_cons (identifier, initializer, initializers);
12152
12153       /* If the next token is not a comma, we have reached the end of
12154          the list.  */
12155       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12156         break;
12157
12158       /* Peek at the next token.  */
12159       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12160       /* If the next token is a `}', then we're still done.  An
12161          initializer-clause can have a trailing `,' after the
12162          initializer-list and before the closing `}'.  */
12163       if (token->type == CPP_CLOSE_BRACE)
12164         break;
12165
12166       /* Consume the `,' token.  */
12167       cp_lexer_consume_token (parser->lexer);
12168     }
12169
12170   /* The initializers were built up in reverse order, so we need to
12171      reverse them now.  */
12172   return nreverse (initializers);
12173 }
12174
12175 /* Classes [gram.class] */
12176
12177 /* Parse a class-name.
12178
12179    class-name:
12180      identifier
12181      template-id
12182
12183    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12184    to indicate that names looked up in dependent types should be
12185    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12186    keyword has been used to indicate that the name that appears next
12187    is a template.  TYPE_P is true iff the next name should be treated
12188    as class-name, even if it is declared to be some other kind of name
12189    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12190    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12191    being defined in a class-head.
12192
12193    Returns the TYPE_DECL representing the class.  */
12194
12195 static tree
12196 cp_parser_class_name (cp_parser *parser,
12197                       bool typename_keyword_p,
12198                       bool template_keyword_p,
12199                       bool type_p,
12200                       bool check_dependency_p,
12201                       bool class_head_p,
12202                       bool is_declaration)
12203 {
12204   tree decl;
12205   tree scope;
12206   bool typename_p;
12207   cp_token *token;
12208
12209   /* All class-names start with an identifier.  */
12210   token = cp_lexer_peek_token (parser->lexer);
12211   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12212     {
12213       cp_parser_error (parser, "expected class-name");
12214       return error_mark_node;
12215     }
12216
12217   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12218      to a template-id, so we save it here.  */
12219   scope = parser->scope;
12220   if (scope == error_mark_node)
12221     return error_mark_node;
12222
12223   /* Any name names a type if we're following the `typename' keyword
12224      in a qualified name where the enclosing scope is type-dependent.  */
12225   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12226                 && dependent_type_p (scope));
12227   /* Handle the common case (an identifier, but not a template-id)
12228      efficiently.  */
12229   if (token->type == CPP_NAME
12230       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12231     {
12232       tree identifier;
12233
12234       /* Look for the identifier.  */
12235       identifier = cp_parser_identifier (parser);
12236       /* If the next token isn't an identifier, we are certainly not
12237          looking at a class-name.  */
12238       if (identifier == error_mark_node)
12239         decl = error_mark_node;
12240       /* If we know this is a type-name, there's no need to look it
12241          up.  */
12242       else if (typename_p)
12243         decl = identifier;
12244       else
12245         {
12246           /* If the next token is a `::', then the name must be a type
12247              name.
12248
12249              [basic.lookup.qual]
12250
12251              During the lookup for a name preceding the :: scope
12252              resolution operator, object, function, and enumerator
12253              names are ignored.  */
12254           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12255             type_p = true;
12256           /* Look up the name.  */
12257           decl = cp_parser_lookup_name (parser, identifier,
12258                                         type_p,
12259                                         /*is_template=*/false,
12260                                         /*is_namespace=*/false,
12261                                         check_dependency_p,
12262                                         /*ambiguous_p=*/NULL);
12263         }
12264     }
12265   else
12266     {
12267       /* Try a template-id.  */
12268       decl = cp_parser_template_id (parser, template_keyword_p,
12269                                     check_dependency_p,
12270                                     is_declaration);
12271       if (decl == error_mark_node)
12272         return error_mark_node;
12273     }
12274
12275   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12276
12277   /* If this is a typename, create a TYPENAME_TYPE.  */
12278   if (typename_p && decl != error_mark_node)
12279     {
12280       decl = make_typename_type (scope, decl, /*complain=*/1);
12281       if (decl != error_mark_node)
12282         decl = TYPE_NAME (decl);
12283     }
12284
12285   /* Check to see that it is really the name of a class.  */
12286   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12287       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12288       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12289     /* Situations like this:
12290
12291          template <typename T> struct A {
12292            typename T::template X<int>::I i;
12293          };
12294
12295        are problematic.  Is `T::template X<int>' a class-name?  The
12296        standard does not seem to be definitive, but there is no other
12297        valid interpretation of the following `::'.  Therefore, those
12298        names are considered class-names.  */
12299     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12300   else if (decl == error_mark_node
12301            || TREE_CODE (decl) != TYPE_DECL
12302            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12303     {
12304       cp_parser_error (parser, "expected class-name");
12305       return error_mark_node;
12306     }
12307
12308   return decl;
12309 }
12310
12311 /* Parse a class-specifier.
12312
12313    class-specifier:
12314      class-head { member-specification [opt] }
12315
12316    Returns the TREE_TYPE representing the class.  */
12317
12318 static tree
12319 cp_parser_class_specifier (cp_parser* parser)
12320 {
12321   cp_token *token;
12322   tree type;
12323   tree attributes = NULL_TREE;
12324   int has_trailing_semicolon;
12325   bool nested_name_specifier_p;
12326   unsigned saved_num_template_parameter_lists;
12327   bool pop_p = false;
12328   tree scope = NULL_TREE;
12329
12330   push_deferring_access_checks (dk_no_deferred);
12331
12332   /* Parse the class-head.  */
12333   type = cp_parser_class_head (parser,
12334                                &nested_name_specifier_p,
12335                                &attributes);
12336   /* If the class-head was a semantic disaster, skip the entire body
12337      of the class.  */
12338   if (!type)
12339     {
12340       cp_parser_skip_to_end_of_block_or_statement (parser);
12341       pop_deferring_access_checks ();
12342       return error_mark_node;
12343     }
12344
12345   /* Look for the `{'.  */
12346   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12347     {
12348       pop_deferring_access_checks ();
12349       return error_mark_node;
12350     }
12351
12352   /* Issue an error message if type-definitions are forbidden here.  */
12353   cp_parser_check_type_definition (parser);
12354   /* Remember that we are defining one more class.  */
12355   ++parser->num_classes_being_defined;
12356   /* Inside the class, surrounding template-parameter-lists do not
12357      apply.  */
12358   saved_num_template_parameter_lists
12359     = parser->num_template_parameter_lists;
12360   parser->num_template_parameter_lists = 0;
12361
12362   /* Start the class.  */
12363   if (nested_name_specifier_p)
12364     {
12365       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12366       pop_p = push_scope (scope);
12367     }
12368   type = begin_class_definition (type);
12369
12370   if (type == error_mark_node)
12371     /* If the type is erroneous, skip the entire body of the class.  */
12372     cp_parser_skip_to_closing_brace (parser);
12373   else
12374     /* Parse the member-specification.  */
12375     cp_parser_member_specification_opt (parser);
12376
12377   /* Look for the trailing `}'.  */
12378   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12379   /* We get better error messages by noticing a common problem: a
12380      missing trailing `;'.  */
12381   token = cp_lexer_peek_token (parser->lexer);
12382   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12383   /* Look for trailing attributes to apply to this class.  */
12384   if (cp_parser_allow_gnu_extensions_p (parser))
12385     {
12386       tree sub_attr = cp_parser_attributes_opt (parser);
12387       attributes = chainon (attributes, sub_attr);
12388     }
12389   if (type != error_mark_node)
12390     type = finish_struct (type, attributes);
12391   if (pop_p)
12392     pop_scope (scope);
12393   /* If this class is not itself within the scope of another class,
12394      then we need to parse the bodies of all of the queued function
12395      definitions.  Note that the queued functions defined in a class
12396      are not always processed immediately following the
12397      class-specifier for that class.  Consider:
12398
12399        struct A {
12400          struct B { void f() { sizeof (A); } };
12401        };
12402
12403      If `f' were processed before the processing of `A' were
12404      completed, there would be no way to compute the size of `A'.
12405      Note that the nesting we are interested in here is lexical --
12406      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12407      for:
12408
12409        struct A { struct B; };
12410        struct A::B { void f() { } };
12411
12412      there is no need to delay the parsing of `A::B::f'.  */
12413   if (--parser->num_classes_being_defined == 0)
12414     {
12415       tree queue_entry;
12416       tree fn;
12417       tree class_type;
12418       bool pop_p;
12419
12420       /* In a first pass, parse default arguments to the functions.
12421          Then, in a second pass, parse the bodies of the functions.
12422          This two-phased approach handles cases like:
12423
12424             struct S {
12425               void f() { g(); }
12426               void g(int i = 3);
12427             };
12428
12429          */
12430       class_type = NULL_TREE;
12431       pop_p = false;
12432       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12433              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12434            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12435            TREE_PURPOSE (parser->unparsed_functions_queues)
12436              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12437         {
12438           fn = TREE_VALUE (queue_entry);
12439           /* If there are default arguments that have not yet been processed,
12440              take care of them now.  */
12441           if (class_type != TREE_PURPOSE (queue_entry))
12442             {
12443               if (pop_p)
12444                 pop_scope (class_type);
12445               class_type = TREE_PURPOSE (queue_entry);
12446               pop_p = push_scope (class_type);
12447             }
12448           /* Make sure that any template parameters are in scope.  */
12449           maybe_begin_member_template_processing (fn);
12450           /* Parse the default argument expressions.  */
12451           cp_parser_late_parsing_default_args (parser, fn);
12452           /* Remove any template parameters from the symbol table.  */
12453           maybe_end_member_template_processing ();
12454         }
12455       if (pop_p)
12456         pop_scope (class_type);
12457       /* Now parse the body of the functions.  */
12458       for (TREE_VALUE (parser->unparsed_functions_queues)
12459              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12460            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12461            TREE_VALUE (parser->unparsed_functions_queues)
12462              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12463         {
12464           /* Figure out which function we need to process.  */
12465           fn = TREE_VALUE (queue_entry);
12466
12467           /* A hack to prevent garbage collection.  */
12468           function_depth++;
12469
12470           /* Parse the function.  */
12471           cp_parser_late_parsing_for_member (parser, fn);
12472           function_depth--;
12473         }
12474     }
12475
12476   /* Put back any saved access checks.  */
12477   pop_deferring_access_checks ();
12478
12479   /* Restore the count of active template-parameter-lists.  */
12480   parser->num_template_parameter_lists
12481     = saved_num_template_parameter_lists;
12482
12483   return type;
12484 }
12485
12486 /* Parse a class-head.
12487
12488    class-head:
12489      class-key identifier [opt] base-clause [opt]
12490      class-key nested-name-specifier identifier base-clause [opt]
12491      class-key nested-name-specifier [opt] template-id
12492        base-clause [opt]
12493
12494    GNU Extensions:
12495      class-key attributes identifier [opt] base-clause [opt]
12496      class-key attributes nested-name-specifier identifier base-clause [opt]
12497      class-key attributes nested-name-specifier [opt] template-id
12498        base-clause [opt]
12499
12500    Returns the TYPE of the indicated class.  Sets
12501    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12502    involving a nested-name-specifier was used, and FALSE otherwise.
12503
12504    Returns NULL_TREE if the class-head is syntactically valid, but
12505    semantically invalid in a way that means we should skip the entire
12506    body of the class.  */
12507
12508 static tree
12509 cp_parser_class_head (cp_parser* parser,
12510                       bool* nested_name_specifier_p,
12511                       tree *attributes_p)
12512 {
12513   tree nested_name_specifier;
12514   enum tag_types class_key;
12515   tree id = NULL_TREE;
12516   tree type = NULL_TREE;
12517   tree attributes;
12518   bool template_id_p = false;
12519   bool qualified_p = false;
12520   bool invalid_nested_name_p = false;
12521   bool invalid_explicit_specialization_p = false;
12522   bool pop_p = false;
12523   unsigned num_templates;
12524   tree bases;
12525
12526   /* Assume no nested-name-specifier will be present.  */
12527   *nested_name_specifier_p = false;
12528   /* Assume no template parameter lists will be used in defining the
12529      type.  */
12530   num_templates = 0;
12531
12532   /* Look for the class-key.  */
12533   class_key = cp_parser_class_key (parser);
12534   if (class_key == none_type)
12535     return error_mark_node;
12536
12537   /* Parse the attributes.  */
12538   attributes = cp_parser_attributes_opt (parser);
12539
12540   /* If the next token is `::', that is invalid -- but sometimes
12541      people do try to write:
12542
12543        struct ::S {};
12544
12545      Handle this gracefully by accepting the extra qualifier, and then
12546      issuing an error about it later if this really is a
12547      class-head.  If it turns out just to be an elaborated type
12548      specifier, remain silent.  */
12549   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12550     qualified_p = true;
12551
12552   push_deferring_access_checks (dk_no_check);
12553
12554   /* Determine the name of the class.  Begin by looking for an
12555      optional nested-name-specifier.  */
12556   nested_name_specifier
12557     = cp_parser_nested_name_specifier_opt (parser,
12558                                            /*typename_keyword_p=*/false,
12559                                            /*check_dependency_p=*/false,
12560                                            /*type_p=*/false,
12561                                            /*is_declaration=*/false);
12562   /* If there was a nested-name-specifier, then there *must* be an
12563      identifier.  */
12564   if (nested_name_specifier)
12565     {
12566       /* Although the grammar says `identifier', it really means
12567          `class-name' or `template-name'.  You are only allowed to
12568          define a class that has already been declared with this
12569          syntax.
12570
12571          The proposed resolution for Core Issue 180 says that whever
12572          you see `class T::X' you should treat `X' as a type-name.
12573
12574          It is OK to define an inaccessible class; for example:
12575
12576            class A { class B; };
12577            class A::B {};
12578
12579          We do not know if we will see a class-name, or a
12580          template-name.  We look for a class-name first, in case the
12581          class-name is a template-id; if we looked for the
12582          template-name first we would stop after the template-name.  */
12583       cp_parser_parse_tentatively (parser);
12584       type = cp_parser_class_name (parser,
12585                                    /*typename_keyword_p=*/false,
12586                                    /*template_keyword_p=*/false,
12587                                    /*type_p=*/true,
12588                                    /*check_dependency_p=*/false,
12589                                    /*class_head_p=*/true,
12590                                    /*is_declaration=*/false);
12591       /* If that didn't work, ignore the nested-name-specifier.  */
12592       if (!cp_parser_parse_definitely (parser))
12593         {
12594           invalid_nested_name_p = true;
12595           id = cp_parser_identifier (parser);
12596           if (id == error_mark_node)
12597             id = NULL_TREE;
12598         }
12599       /* If we could not find a corresponding TYPE, treat this
12600          declaration like an unqualified declaration.  */
12601       if (type == error_mark_node)
12602         nested_name_specifier = NULL_TREE;
12603       /* Otherwise, count the number of templates used in TYPE and its
12604          containing scopes.  */
12605       else
12606         {
12607           tree scope;
12608
12609           for (scope = TREE_TYPE (type);
12610                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12611                scope = (TYPE_P (scope)
12612                         ? TYPE_CONTEXT (scope)
12613                         : DECL_CONTEXT (scope)))
12614             if (TYPE_P (scope)
12615                 && CLASS_TYPE_P (scope)
12616                 && CLASSTYPE_TEMPLATE_INFO (scope)
12617                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12618                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12619               ++num_templates;
12620         }
12621     }
12622   /* Otherwise, the identifier is optional.  */
12623   else
12624     {
12625       /* We don't know whether what comes next is a template-id,
12626          an identifier, or nothing at all.  */
12627       cp_parser_parse_tentatively (parser);
12628       /* Check for a template-id.  */
12629       id = cp_parser_template_id (parser,
12630                                   /*template_keyword_p=*/false,
12631                                   /*check_dependency_p=*/true,
12632                                   /*is_declaration=*/true);
12633       /* If that didn't work, it could still be an identifier.  */
12634       if (!cp_parser_parse_definitely (parser))
12635         {
12636           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12637             id = cp_parser_identifier (parser);
12638           else
12639             id = NULL_TREE;
12640         }
12641       else
12642         {
12643           template_id_p = true;
12644           ++num_templates;
12645         }
12646     }
12647
12648   pop_deferring_access_checks ();
12649
12650   if (id)
12651     cp_parser_check_for_invalid_template_id (parser, id);
12652
12653   /* If it's not a `:' or a `{' then we can't really be looking at a
12654      class-head, since a class-head only appears as part of a
12655      class-specifier.  We have to detect this situation before calling
12656      xref_tag, since that has irreversible side-effects.  */
12657   if (!cp_parser_next_token_starts_class_definition_p (parser))
12658     {
12659       cp_parser_error (parser, "expected `{' or `:'");
12660       return error_mark_node;
12661     }
12662
12663   /* At this point, we're going ahead with the class-specifier, even
12664      if some other problem occurs.  */
12665   cp_parser_commit_to_tentative_parse (parser);
12666   /* Issue the error about the overly-qualified name now.  */
12667   if (qualified_p)
12668     cp_parser_error (parser,
12669                      "global qualification of class name is invalid");
12670   else if (invalid_nested_name_p)
12671     cp_parser_error (parser,
12672                      "qualified name does not name a class");
12673   else if (nested_name_specifier)
12674     {
12675       tree scope;
12676       /* Figure out in what scope the declaration is being placed.  */
12677       scope = current_scope ();
12678       if (!scope)
12679         scope = current_namespace;
12680       /* If that scope does not contain the scope in which the
12681          class was originally declared, the program is invalid.  */
12682       if (scope && !is_ancestor (scope, nested_name_specifier))
12683         {
12684           error ("declaration of `%D' in `%D' which does not "
12685                  "enclose `%D'", type, scope, nested_name_specifier);
12686           type = NULL_TREE;
12687           goto done;
12688         }
12689       /* [dcl.meaning]
12690
12691          A declarator-id shall not be qualified exception of the
12692          definition of a ... nested class outside of its class
12693          ... [or] a the definition or explicit instantiation of a
12694          class member of a namespace outside of its namespace.  */
12695       if (scope == nested_name_specifier)
12696         {
12697           pedwarn ("extra qualification ignored");
12698           nested_name_specifier = NULL_TREE;
12699           num_templates = 0;
12700         }
12701     }
12702   /* An explicit-specialization must be preceded by "template <>".  If
12703      it is not, try to recover gracefully.  */
12704   if (at_namespace_scope_p ()
12705       && parser->num_template_parameter_lists == 0
12706       && template_id_p)
12707     {
12708       error ("an explicit specialization must be preceded by 'template <>'");
12709       invalid_explicit_specialization_p = true;
12710       /* Take the same action that would have been taken by
12711          cp_parser_explicit_specialization.  */
12712       ++parser->num_template_parameter_lists;
12713       begin_specialization ();
12714     }
12715   /* There must be no "return" statements between this point and the
12716      end of this function; set "type "to the correct return value and
12717      use "goto done;" to return.  */
12718   /* Make sure that the right number of template parameters were
12719      present.  */
12720   if (!cp_parser_check_template_parameters (parser, num_templates))
12721     {
12722       /* If something went wrong, there is no point in even trying to
12723          process the class-definition.  */
12724       type = NULL_TREE;
12725       goto done;
12726     }
12727
12728   /* Look up the type.  */
12729   if (template_id_p)
12730     {
12731       type = TREE_TYPE (id);
12732       maybe_process_partial_specialization (type);
12733     }
12734   else if (!nested_name_specifier)
12735     {
12736       /* If the class was unnamed, create a dummy name.  */
12737       if (!id)
12738         id = make_anon_name ();
12739       type = xref_tag (class_key, id, /*globalize=*/false,
12740                        parser->num_template_parameter_lists);
12741     }
12742   else
12743     {
12744       tree class_type;
12745       bool pop_p = false;
12746
12747       /* Given:
12748
12749             template <typename T> struct S { struct T };
12750             template <typename T> struct S<T>::T { };
12751
12752          we will get a TYPENAME_TYPE when processing the definition of
12753          `S::T'.  We need to resolve it to the actual type before we
12754          try to define it.  */
12755       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12756         {
12757           class_type = resolve_typename_type (TREE_TYPE (type),
12758                                               /*only_current_p=*/false);
12759           if (class_type != error_mark_node)
12760             type = TYPE_NAME (class_type);
12761           else
12762             {
12763               cp_parser_error (parser, "could not resolve typename type");
12764               type = error_mark_node;
12765             }
12766         }
12767
12768       maybe_process_partial_specialization (TREE_TYPE (type));
12769       class_type = current_class_type;
12770       /* Enter the scope indicated by the nested-name-specifier.  */
12771       if (nested_name_specifier)
12772         pop_p = push_scope (nested_name_specifier);
12773       /* Get the canonical version of this type.  */
12774       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12775       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12776           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12777         type = push_template_decl (type);
12778       type = TREE_TYPE (type);
12779       if (nested_name_specifier)
12780         {
12781           *nested_name_specifier_p = true;
12782           if (pop_p)
12783             pop_scope (nested_name_specifier);
12784         }
12785     }
12786   /* Indicate whether this class was declared as a `class' or as a
12787      `struct'.  */
12788   if (TREE_CODE (type) == RECORD_TYPE)
12789     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12790   cp_parser_check_class_key (class_key, type);
12791
12792   /* Enter the scope containing the class; the names of base classes
12793      should be looked up in that context.  For example, given:
12794
12795        struct A { struct B {}; struct C; };
12796        struct A::C : B {};
12797
12798      is valid.  */
12799   if (nested_name_specifier)
12800     pop_p = push_scope (nested_name_specifier);
12801
12802   bases = NULL_TREE;
12803
12804   /* Get the list of base-classes, if there is one.  */
12805   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12806     bases = cp_parser_base_clause (parser);
12807
12808   /* Process the base classes.  */
12809   xref_basetypes (type, bases);
12810
12811   /* Leave the scope given by the nested-name-specifier.  We will
12812      enter the class scope itself while processing the members.  */
12813   if (pop_p)
12814     pop_scope (nested_name_specifier);
12815
12816  done:
12817   if (invalid_explicit_specialization_p)
12818     {
12819       end_specialization ();
12820       --parser->num_template_parameter_lists;
12821     }
12822   *attributes_p = attributes;
12823   return type;
12824 }
12825
12826 /* Parse a class-key.
12827
12828    class-key:
12829      class
12830      struct
12831      union
12832
12833    Returns the kind of class-key specified, or none_type to indicate
12834    error.  */
12835
12836 static enum tag_types
12837 cp_parser_class_key (cp_parser* parser)
12838 {
12839   cp_token *token;
12840   enum tag_types tag_type;
12841
12842   /* Look for the class-key.  */
12843   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12844   if (!token)
12845     return none_type;
12846
12847   /* Check to see if the TOKEN is a class-key.  */
12848   tag_type = cp_parser_token_is_class_key (token);
12849   if (!tag_type)
12850     cp_parser_error (parser, "expected class-key");
12851   return tag_type;
12852 }
12853
12854 /* Parse an (optional) member-specification.
12855
12856    member-specification:
12857      member-declaration member-specification [opt]
12858      access-specifier : member-specification [opt]  */
12859
12860 static void
12861 cp_parser_member_specification_opt (cp_parser* parser)
12862 {
12863   while (true)
12864     {
12865       cp_token *token;
12866       enum rid keyword;
12867
12868       /* Peek at the next token.  */
12869       token = cp_lexer_peek_token (parser->lexer);
12870       /* If it's a `}', or EOF then we've seen all the members.  */
12871       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12872         break;
12873
12874       /* See if this token is a keyword.  */
12875       keyword = token->keyword;
12876       switch (keyword)
12877         {
12878         case RID_PUBLIC:
12879         case RID_PROTECTED:
12880         case RID_PRIVATE:
12881           /* Consume the access-specifier.  */
12882           cp_lexer_consume_token (parser->lexer);
12883           /* Remember which access-specifier is active.  */
12884           current_access_specifier = token->value;
12885           /* Look for the `:'.  */
12886           cp_parser_require (parser, CPP_COLON, "`:'");
12887           break;
12888
12889         default:
12890           /* Otherwise, the next construction must be a
12891              member-declaration.  */
12892           cp_parser_member_declaration (parser);
12893         }
12894     }
12895 }
12896
12897 /* Parse a member-declaration.
12898
12899    member-declaration:
12900      decl-specifier-seq [opt] member-declarator-list [opt] ;
12901      function-definition ; [opt]
12902      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12903      using-declaration
12904      template-declaration
12905
12906    member-declarator-list:
12907      member-declarator
12908      member-declarator-list , member-declarator
12909
12910    member-declarator:
12911      declarator pure-specifier [opt]
12912      declarator constant-initializer [opt]
12913      identifier [opt] : constant-expression
12914
12915    GNU Extensions:
12916
12917    member-declaration:
12918      __extension__ member-declaration
12919
12920    member-declarator:
12921      declarator attributes [opt] pure-specifier [opt]
12922      declarator attributes [opt] constant-initializer [opt]
12923      identifier [opt] attributes [opt] : constant-expression  */
12924
12925 static void
12926 cp_parser_member_declaration (cp_parser* parser)
12927 {
12928   cp_decl_specifier_seq decl_specifiers;
12929   tree prefix_attributes;
12930   tree decl;
12931   int declares_class_or_enum;
12932   bool friend_p;
12933   cp_token *token;
12934   int saved_pedantic;
12935
12936   /* Check for the `__extension__' keyword.  */
12937   if (cp_parser_extension_opt (parser, &saved_pedantic))
12938     {
12939       /* Recurse.  */
12940       cp_parser_member_declaration (parser);
12941       /* Restore the old value of the PEDANTIC flag.  */
12942       pedantic = saved_pedantic;
12943
12944       return;
12945     }
12946
12947   /* Check for a template-declaration.  */
12948   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12949     {
12950       /* Parse the template-declaration.  */
12951       cp_parser_template_declaration (parser, /*member_p=*/true);
12952
12953       return;
12954     }
12955
12956   /* Check for a using-declaration.  */
12957   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12958     {
12959       /* Parse the using-declaration.  */
12960       cp_parser_using_declaration (parser);
12961
12962       return;
12963     }
12964
12965   /* Parse the decl-specifier-seq.  */
12966   cp_parser_decl_specifier_seq (parser,
12967                                 CP_PARSER_FLAGS_OPTIONAL,
12968                                 &decl_specifiers,
12969                                 &declares_class_or_enum);
12970   prefix_attributes = decl_specifiers.attributes;
12971   decl_specifiers.attributes = NULL_TREE;
12972   /* Check for an invalid type-name.  */
12973   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12974     return;
12975   /* If there is no declarator, then the decl-specifier-seq should
12976      specify a type.  */
12977   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12978     {
12979       /* If there was no decl-specifier-seq, and the next token is a
12980          `;', then we have something like:
12981
12982            struct S { ; };
12983
12984          [class.mem]
12985
12986          Each member-declaration shall declare at least one member
12987          name of the class.  */
12988       if (!decl_specifiers.any_specifiers_p)
12989         {
12990           if (pedantic)
12991             pedwarn ("extra semicolon");
12992         }
12993       else
12994         {
12995           tree type;
12996
12997           /* See if this declaration is a friend.  */
12998           friend_p = cp_parser_friend_p (&decl_specifiers);
12999           /* If there were decl-specifiers, check to see if there was
13000              a class-declaration.  */
13001           type = check_tag_decl (&decl_specifiers);
13002           /* Nested classes have already been added to the class, but
13003              a `friend' needs to be explicitly registered.  */
13004           if (friend_p)
13005             {
13006               /* If the `friend' keyword was present, the friend must
13007                  be introduced with a class-key.  */
13008                if (!declares_class_or_enum)
13009                  error ("a class-key must be used when declaring a friend");
13010                /* In this case:
13011
13012                     template <typename T> struct A {
13013                       friend struct A<T>::B;
13014                     };
13015
13016                   A<T>::B will be represented by a TYPENAME_TYPE, and
13017                   therefore not recognized by check_tag_decl.  */
13018                if (!type
13019                    && decl_specifiers.type
13020                    && TYPE_P (decl_specifiers.type))
13021                  type = decl_specifiers.type;
13022                if (!type || !TYPE_P (type))
13023                  error ("friend declaration does not name a class or "
13024                         "function");
13025                else
13026                  make_friend_class (current_class_type, type,
13027                                     /*complain=*/true);
13028             }
13029           /* If there is no TYPE, an error message will already have
13030              been issued.  */
13031           else if (!type || type == error_mark_node)
13032             ;
13033           /* An anonymous aggregate has to be handled specially; such
13034              a declaration really declares a data member (with a
13035              particular type), as opposed to a nested class.  */
13036           else if (ANON_AGGR_TYPE_P (type))
13037             {
13038               /* Remove constructors and such from TYPE, now that we
13039                  know it is an anonymous aggregate.  */
13040               fixup_anonymous_aggr (type);
13041               /* And make the corresponding data member.  */
13042               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13043               /* Add it to the class.  */
13044               finish_member_declaration (decl);
13045             }
13046           else
13047             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13048         }
13049     }
13050   else
13051     {
13052       /* See if these declarations will be friends.  */
13053       friend_p = cp_parser_friend_p (&decl_specifiers);
13054
13055       /* Keep going until we hit the `;' at the end of the
13056          declaration.  */
13057       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13058         {
13059           tree attributes = NULL_TREE;
13060           tree first_attribute;
13061
13062           /* Peek at the next token.  */
13063           token = cp_lexer_peek_token (parser->lexer);
13064
13065           /* Check for a bitfield declaration.  */
13066           if (token->type == CPP_COLON
13067               || (token->type == CPP_NAME
13068                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13069                   == CPP_COLON))
13070             {
13071               tree identifier;
13072               tree width;
13073
13074               /* Get the name of the bitfield.  Note that we cannot just
13075                  check TOKEN here because it may have been invalidated by
13076                  the call to cp_lexer_peek_nth_token above.  */
13077               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13078                 identifier = cp_parser_identifier (parser);
13079               else
13080                 identifier = NULL_TREE;
13081
13082               /* Consume the `:' token.  */
13083               cp_lexer_consume_token (parser->lexer);
13084               /* Get the width of the bitfield.  */
13085               width
13086                 = cp_parser_constant_expression (parser,
13087                                                  /*allow_non_constant=*/false,
13088                                                  NULL);
13089
13090               /* Look for attributes that apply to the bitfield.  */
13091               attributes = cp_parser_attributes_opt (parser);
13092               /* Remember which attributes are prefix attributes and
13093                  which are not.  */
13094               first_attribute = attributes;
13095               /* Combine the attributes.  */
13096               attributes = chainon (prefix_attributes, attributes);
13097
13098               /* Create the bitfield declaration.  */
13099               decl = grokbitfield (identifier
13100                                    ? make_id_declarator (identifier)
13101                                    : NULL,
13102                                    &decl_specifiers,
13103                                    width);
13104               /* Apply the attributes.  */
13105               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13106             }
13107           else
13108             {
13109               cp_declarator *declarator;
13110               tree initializer;
13111               tree asm_specification;
13112               int ctor_dtor_or_conv_p;
13113
13114               /* Parse the declarator.  */
13115               declarator
13116                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13117                                         &ctor_dtor_or_conv_p,
13118                                         /*parenthesized_p=*/NULL);
13119
13120               /* If something went wrong parsing the declarator, make sure
13121                  that we at least consume some tokens.  */
13122               if (declarator == cp_error_declarator)
13123                 {
13124                   /* Skip to the end of the statement.  */
13125                   cp_parser_skip_to_end_of_statement (parser);
13126                   /* If the next token is not a semicolon, that is
13127                      probably because we just skipped over the body of
13128                      a function.  So, we consume a semicolon if
13129                      present, but do not issue an error message if it
13130                      is not present.  */
13131                   if (cp_lexer_next_token_is (parser->lexer,
13132                                               CPP_SEMICOLON))
13133                     cp_lexer_consume_token (parser->lexer);
13134                   return;
13135                 }
13136
13137               cp_parser_check_for_definition_in_return_type
13138                 (declarator, declares_class_or_enum);
13139
13140               /* Look for an asm-specification.  */
13141               asm_specification = cp_parser_asm_specification_opt (parser);
13142               /* Look for attributes that apply to the declaration.  */
13143               attributes = cp_parser_attributes_opt (parser);
13144               /* Remember which attributes are prefix attributes and
13145                  which are not.  */
13146               first_attribute = attributes;
13147               /* Combine the attributes.  */
13148               attributes = chainon (prefix_attributes, attributes);
13149
13150               /* If it's an `=', then we have a constant-initializer or a
13151                  pure-specifier.  It is not correct to parse the
13152                  initializer before registering the member declaration
13153                  since the member declaration should be in scope while
13154                  its initializer is processed.  However, the rest of the
13155                  front end does not yet provide an interface that allows
13156                  us to handle this correctly.  */
13157               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13158                 {
13159                   /* In [class.mem]:
13160
13161                      A pure-specifier shall be used only in the declaration of
13162                      a virtual function.
13163
13164                      A member-declarator can contain a constant-initializer
13165                      only if it declares a static member of integral or
13166                      enumeration type.
13167
13168                      Therefore, if the DECLARATOR is for a function, we look
13169                      for a pure-specifier; otherwise, we look for a
13170                      constant-initializer.  When we call `grokfield', it will
13171                      perform more stringent semantics checks.  */
13172                   if (declarator->kind == cdk_function)
13173                     initializer = cp_parser_pure_specifier (parser);
13174                   else
13175                     /* Parse the initializer.  */
13176                     initializer = cp_parser_constant_initializer (parser);
13177                 }
13178               /* Otherwise, there is no initializer.  */
13179               else
13180                 initializer = NULL_TREE;
13181
13182               /* See if we are probably looking at a function
13183                  definition.  We are certainly not looking at at a
13184                  member-declarator.  Calling `grokfield' has
13185                  side-effects, so we must not do it unless we are sure
13186                  that we are looking at a member-declarator.  */
13187               if (cp_parser_token_starts_function_definition_p
13188                   (cp_lexer_peek_token (parser->lexer)))
13189                 {
13190                   /* The grammar does not allow a pure-specifier to be
13191                      used when a member function is defined.  (It is
13192                      possible that this fact is an oversight in the
13193                      standard, since a pure function may be defined
13194                      outside of the class-specifier.  */
13195                   if (initializer)
13196                     error ("pure-specifier on function-definition");
13197                   decl = cp_parser_save_member_function_body (parser,
13198                                                               &decl_specifiers,
13199                                                               declarator,
13200                                                               attributes);
13201                   /* If the member was not a friend, declare it here.  */
13202                   if (!friend_p)
13203                     finish_member_declaration (decl);
13204                   /* Peek at the next token.  */
13205                   token = cp_lexer_peek_token (parser->lexer);
13206                   /* If the next token is a semicolon, consume it.  */
13207                   if (token->type == CPP_SEMICOLON)
13208                     cp_lexer_consume_token (parser->lexer);
13209                   return;
13210                 }
13211               else
13212                 {
13213                   /* Create the declaration.  */
13214                   decl = grokfield (declarator, &decl_specifiers,
13215                                     initializer, asm_specification,
13216                                     attributes);
13217                   /* Any initialization must have been from a
13218                      constant-expression.  */
13219                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13220                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13221                 }
13222             }
13223
13224           /* Reset PREFIX_ATTRIBUTES.  */
13225           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13226             attributes = TREE_CHAIN (attributes);
13227           if (attributes)
13228             TREE_CHAIN (attributes) = NULL_TREE;
13229
13230           /* If there is any qualification still in effect, clear it
13231              now; we will be starting fresh with the next declarator.  */
13232           parser->scope = NULL_TREE;
13233           parser->qualifying_scope = NULL_TREE;
13234           parser->object_scope = NULL_TREE;
13235           /* If it's a `,', then there are more declarators.  */
13236           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13237             cp_lexer_consume_token (parser->lexer);
13238           /* If the next token isn't a `;', then we have a parse error.  */
13239           else if (cp_lexer_next_token_is_not (parser->lexer,
13240                                                CPP_SEMICOLON))
13241             {
13242               cp_parser_error (parser, "expected `;'");
13243               /* Skip tokens until we find a `;'.  */
13244               cp_parser_skip_to_end_of_statement (parser);
13245
13246               break;
13247             }
13248
13249           if (decl)
13250             {
13251               /* Add DECL to the list of members.  */
13252               if (!friend_p)
13253                 finish_member_declaration (decl);
13254
13255               if (TREE_CODE (decl) == FUNCTION_DECL)
13256                 cp_parser_save_default_args (parser, decl);
13257             }
13258         }
13259     }
13260
13261   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13262 }
13263
13264 /* Parse a pure-specifier.
13265
13266    pure-specifier:
13267      = 0
13268
13269    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13270    Otherwise, ERROR_MARK_NODE is returned.  */
13271
13272 static tree
13273 cp_parser_pure_specifier (cp_parser* parser)
13274 {
13275   cp_token *token;
13276
13277   /* Look for the `=' token.  */
13278   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13279     return error_mark_node;
13280   /* Look for the `0' token.  */
13281   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13282   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13283      to get information from the lexer about how the number was
13284      spelled in order to fix this problem.  */
13285   if (!token || !integer_zerop (token->value))
13286     return error_mark_node;
13287
13288   return integer_zero_node;
13289 }
13290
13291 /* Parse a constant-initializer.
13292
13293    constant-initializer:
13294      = constant-expression
13295
13296    Returns a representation of the constant-expression.  */
13297
13298 static tree
13299 cp_parser_constant_initializer (cp_parser* parser)
13300 {
13301   /* Look for the `=' token.  */
13302   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13303     return error_mark_node;
13304
13305   /* It is invalid to write:
13306
13307        struct S { static const int i = { 7 }; };
13308
13309      */
13310   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13311     {
13312       cp_parser_error (parser,
13313                        "a brace-enclosed initializer is not allowed here");
13314       /* Consume the opening brace.  */
13315       cp_lexer_consume_token (parser->lexer);
13316       /* Skip the initializer.  */
13317       cp_parser_skip_to_closing_brace (parser);
13318       /* Look for the trailing `}'.  */
13319       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13320
13321       return error_mark_node;
13322     }
13323
13324   return cp_parser_constant_expression (parser,
13325                                         /*allow_non_constant=*/false,
13326                                         NULL);
13327 }
13328
13329 /* Derived classes [gram.class.derived] */
13330
13331 /* Parse a base-clause.
13332
13333    base-clause:
13334      : base-specifier-list
13335
13336    base-specifier-list:
13337      base-specifier
13338      base-specifier-list , base-specifier
13339
13340    Returns a TREE_LIST representing the base-classes, in the order in
13341    which they were declared.  The representation of each node is as
13342    described by cp_parser_base_specifier.
13343
13344    In the case that no bases are specified, this function will return
13345    NULL_TREE, not ERROR_MARK_NODE.  */
13346
13347 static tree
13348 cp_parser_base_clause (cp_parser* parser)
13349 {
13350   tree bases = NULL_TREE;
13351
13352   /* Look for the `:' that begins the list.  */
13353   cp_parser_require (parser, CPP_COLON, "`:'");
13354
13355   /* Scan the base-specifier-list.  */
13356   while (true)
13357     {
13358       cp_token *token;
13359       tree base;
13360
13361       /* Look for the base-specifier.  */
13362       base = cp_parser_base_specifier (parser);
13363       /* Add BASE to the front of the list.  */
13364       if (base != error_mark_node)
13365         {
13366           TREE_CHAIN (base) = bases;
13367           bases = base;
13368         }
13369       /* Peek at the next token.  */
13370       token = cp_lexer_peek_token (parser->lexer);
13371       /* If it's not a comma, then the list is complete.  */
13372       if (token->type != CPP_COMMA)
13373         break;
13374       /* Consume the `,'.  */
13375       cp_lexer_consume_token (parser->lexer);
13376     }
13377
13378   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13379      base class had a qualified name.  However, the next name that
13380      appears is certainly not qualified.  */
13381   parser->scope = NULL_TREE;
13382   parser->qualifying_scope = NULL_TREE;
13383   parser->object_scope = NULL_TREE;
13384
13385   return nreverse (bases);
13386 }
13387
13388 /* Parse a base-specifier.
13389
13390    base-specifier:
13391      :: [opt] nested-name-specifier [opt] class-name
13392      virtual access-specifier [opt] :: [opt] nested-name-specifier
13393        [opt] class-name
13394      access-specifier virtual [opt] :: [opt] nested-name-specifier
13395        [opt] class-name
13396
13397    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13398    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13399    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13400    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13401
13402 static tree
13403 cp_parser_base_specifier (cp_parser* parser)
13404 {
13405   cp_token *token;
13406   bool done = false;
13407   bool virtual_p = false;
13408   bool duplicate_virtual_error_issued_p = false;
13409   bool duplicate_access_error_issued_p = false;
13410   bool class_scope_p, template_p;
13411   tree access = access_default_node;
13412   tree type;
13413
13414   /* Process the optional `virtual' and `access-specifier'.  */
13415   while (!done)
13416     {
13417       /* Peek at the next token.  */
13418       token = cp_lexer_peek_token (parser->lexer);
13419       /* Process `virtual'.  */
13420       switch (token->keyword)
13421         {
13422         case RID_VIRTUAL:
13423           /* If `virtual' appears more than once, issue an error.  */
13424           if (virtual_p && !duplicate_virtual_error_issued_p)
13425             {
13426               cp_parser_error (parser,
13427                                "`virtual' specified more than once in base-specified");
13428               duplicate_virtual_error_issued_p = true;
13429             }
13430
13431           virtual_p = true;
13432
13433           /* Consume the `virtual' token.  */
13434           cp_lexer_consume_token (parser->lexer);
13435
13436           break;
13437
13438         case RID_PUBLIC:
13439         case RID_PROTECTED:
13440         case RID_PRIVATE:
13441           /* If more than one access specifier appears, issue an
13442              error.  */
13443           if (access != access_default_node
13444               && !duplicate_access_error_issued_p)
13445             {
13446               cp_parser_error (parser,
13447                                "more than one access specifier in base-specified");
13448               duplicate_access_error_issued_p = true;
13449             }
13450
13451           access = ridpointers[(int) token->keyword];
13452
13453           /* Consume the access-specifier.  */
13454           cp_lexer_consume_token (parser->lexer);
13455
13456           break;
13457
13458         default:
13459           done = true;
13460           break;
13461         }
13462     }
13463   /* It is not uncommon to see programs mechanically, erroneously, use
13464      the 'typename' keyword to denote (dependent) qualified types
13465      as base classes.  */
13466   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13467     {
13468       if (!processing_template_decl)
13469         error ("keyword `typename' not allowed outside of templates");
13470       else
13471         error ("keyword `typename' not allowed in this context "
13472                "(the base class is implicitly a type)");
13473       cp_lexer_consume_token (parser->lexer);
13474     }
13475
13476   /* Look for the optional `::' operator.  */
13477   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13478   /* Look for the nested-name-specifier.  The simplest way to
13479      implement:
13480
13481        [temp.res]
13482
13483        The keyword `typename' is not permitted in a base-specifier or
13484        mem-initializer; in these contexts a qualified name that
13485        depends on a template-parameter is implicitly assumed to be a
13486        type name.
13487
13488      is to pretend that we have seen the `typename' keyword at this
13489      point.  */
13490   cp_parser_nested_name_specifier_opt (parser,
13491                                        /*typename_keyword_p=*/true,
13492                                        /*check_dependency_p=*/true,
13493                                        /*type_p=*/true,
13494                                        /*is_declaration=*/true);
13495   /* If the base class is given by a qualified name, assume that names
13496      we see are type names or templates, as appropriate.  */
13497   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13498   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13499
13500   /* Finally, look for the class-name.  */
13501   type = cp_parser_class_name (parser,
13502                                class_scope_p,
13503                                template_p,
13504                                /*type_p=*/true,
13505                                /*check_dependency_p=*/true,
13506                                /*class_head_p=*/false,
13507                                /*is_declaration=*/true);
13508
13509   if (type == error_mark_node)
13510     return error_mark_node;
13511
13512   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13513 }
13514
13515 /* Exception handling [gram.exception] */
13516
13517 /* Parse an (optional) exception-specification.
13518
13519    exception-specification:
13520      throw ( type-id-list [opt] )
13521
13522    Returns a TREE_LIST representing the exception-specification.  The
13523    TREE_VALUE of each node is a type.  */
13524
13525 static tree
13526 cp_parser_exception_specification_opt (cp_parser* parser)
13527 {
13528   cp_token *token;
13529   tree type_id_list;
13530
13531   /* Peek at the next token.  */
13532   token = cp_lexer_peek_token (parser->lexer);
13533   /* If it's not `throw', then there's no exception-specification.  */
13534   if (!cp_parser_is_keyword (token, RID_THROW))
13535     return NULL_TREE;
13536
13537   /* Consume the `throw'.  */
13538   cp_lexer_consume_token (parser->lexer);
13539
13540   /* Look for the `('.  */
13541   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13542
13543   /* Peek at the next token.  */
13544   token = cp_lexer_peek_token (parser->lexer);
13545   /* If it's not a `)', then there is a type-id-list.  */
13546   if (token->type != CPP_CLOSE_PAREN)
13547     {
13548       const char *saved_message;
13549
13550       /* Types may not be defined in an exception-specification.  */
13551       saved_message = parser->type_definition_forbidden_message;
13552       parser->type_definition_forbidden_message
13553         = "types may not be defined in an exception-specification";
13554       /* Parse the type-id-list.  */
13555       type_id_list = cp_parser_type_id_list (parser);
13556       /* Restore the saved message.  */
13557       parser->type_definition_forbidden_message = saved_message;
13558     }
13559   else
13560     type_id_list = empty_except_spec;
13561
13562   /* Look for the `)'.  */
13563   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13564
13565   return type_id_list;
13566 }
13567
13568 /* Parse an (optional) type-id-list.
13569
13570    type-id-list:
13571      type-id
13572      type-id-list , type-id
13573
13574    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13575    in the order that the types were presented.  */
13576
13577 static tree
13578 cp_parser_type_id_list (cp_parser* parser)
13579 {
13580   tree types = NULL_TREE;
13581
13582   while (true)
13583     {
13584       cp_token *token;
13585       tree type;
13586
13587       /* Get the next type-id.  */
13588       type = cp_parser_type_id (parser);
13589       /* Add it to the list.  */
13590       types = add_exception_specifier (types, type, /*complain=*/1);
13591       /* Peek at the next token.  */
13592       token = cp_lexer_peek_token (parser->lexer);
13593       /* If it is not a `,', we are done.  */
13594       if (token->type != CPP_COMMA)
13595         break;
13596       /* Consume the `,'.  */
13597       cp_lexer_consume_token (parser->lexer);
13598     }
13599
13600   return nreverse (types);
13601 }
13602
13603 /* Parse a try-block.
13604
13605    try-block:
13606      try compound-statement handler-seq  */
13607
13608 static tree
13609 cp_parser_try_block (cp_parser* parser)
13610 {
13611   tree try_block;
13612
13613   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13614   try_block = begin_try_block ();
13615   cp_parser_compound_statement (parser, NULL, true);
13616   finish_try_block (try_block);
13617   cp_parser_handler_seq (parser);
13618   finish_handler_sequence (try_block);
13619
13620   return try_block;
13621 }
13622
13623 /* Parse a function-try-block.
13624
13625    function-try-block:
13626      try ctor-initializer [opt] function-body handler-seq  */
13627
13628 static bool
13629 cp_parser_function_try_block (cp_parser* parser)
13630 {
13631   tree try_block;
13632   bool ctor_initializer_p;
13633
13634   /* Look for the `try' keyword.  */
13635   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13636     return false;
13637   /* Let the rest of the front-end know where we are.  */
13638   try_block = begin_function_try_block ();
13639   /* Parse the function-body.  */
13640   ctor_initializer_p
13641     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13642   /* We're done with the `try' part.  */
13643   finish_function_try_block (try_block);
13644   /* Parse the handlers.  */
13645   cp_parser_handler_seq (parser);
13646   /* We're done with the handlers.  */
13647   finish_function_handler_sequence (try_block);
13648
13649   return ctor_initializer_p;
13650 }
13651
13652 /* Parse a handler-seq.
13653
13654    handler-seq:
13655      handler handler-seq [opt]  */
13656
13657 static void
13658 cp_parser_handler_seq (cp_parser* parser)
13659 {
13660   while (true)
13661     {
13662       cp_token *token;
13663
13664       /* Parse the handler.  */
13665       cp_parser_handler (parser);
13666       /* Peek at the next token.  */
13667       token = cp_lexer_peek_token (parser->lexer);
13668       /* If it's not `catch' then there are no more handlers.  */
13669       if (!cp_parser_is_keyword (token, RID_CATCH))
13670         break;
13671     }
13672 }
13673
13674 /* Parse a handler.
13675
13676    handler:
13677      catch ( exception-declaration ) compound-statement  */
13678
13679 static void
13680 cp_parser_handler (cp_parser* parser)
13681 {
13682   tree handler;
13683   tree declaration;
13684
13685   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13686   handler = begin_handler ();
13687   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13688   declaration = cp_parser_exception_declaration (parser);
13689   finish_handler_parms (declaration, handler);
13690   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13691   cp_parser_compound_statement (parser, NULL, false);
13692   finish_handler (handler);
13693 }
13694
13695 /* Parse an exception-declaration.
13696
13697    exception-declaration:
13698      type-specifier-seq declarator
13699      type-specifier-seq abstract-declarator
13700      type-specifier-seq
13701      ...
13702
13703    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13704    ellipsis variant is used.  */
13705
13706 static tree
13707 cp_parser_exception_declaration (cp_parser* parser)
13708 {
13709   tree decl;
13710   cp_decl_specifier_seq type_specifiers;
13711   cp_declarator *declarator;
13712   const char *saved_message;
13713
13714   /* If it's an ellipsis, it's easy to handle.  */
13715   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13716     {
13717       /* Consume the `...' token.  */
13718       cp_lexer_consume_token (parser->lexer);
13719       return NULL_TREE;
13720     }
13721
13722   /* Types may not be defined in exception-declarations.  */
13723   saved_message = parser->type_definition_forbidden_message;
13724   parser->type_definition_forbidden_message
13725     = "types may not be defined in exception-declarations";
13726
13727   /* Parse the type-specifier-seq.  */
13728   cp_parser_type_specifier_seq (parser, &type_specifiers);
13729   /* If it's a `)', then there is no declarator.  */
13730   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13731     declarator = NULL;
13732   else
13733     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13734                                        /*ctor_dtor_or_conv_p=*/NULL,
13735                                        /*parenthesized_p=*/NULL);
13736
13737   /* Restore the saved message.  */
13738   parser->type_definition_forbidden_message = saved_message;
13739
13740   if (type_specifiers.any_specifiers_p)
13741     {
13742       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13743       if (decl == NULL_TREE)
13744         error ("invalid catch parameter");
13745     }
13746   else
13747     decl = NULL_TREE;
13748
13749   return decl;
13750 }
13751
13752 /* Parse a throw-expression.
13753
13754    throw-expression:
13755      throw assignment-expression [opt]
13756
13757    Returns a THROW_EXPR representing the throw-expression.  */
13758
13759 static tree
13760 cp_parser_throw_expression (cp_parser* parser)
13761 {
13762   tree expression;
13763   cp_token* token;
13764
13765   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13766   token = cp_lexer_peek_token (parser->lexer);
13767   /* Figure out whether or not there is an assignment-expression
13768      following the "throw" keyword.  */
13769   if (token->type == CPP_COMMA
13770       || token->type == CPP_SEMICOLON
13771       || token->type == CPP_CLOSE_PAREN
13772       || token->type == CPP_CLOSE_SQUARE
13773       || token->type == CPP_CLOSE_BRACE
13774       || token->type == CPP_COLON)
13775     expression = NULL_TREE;
13776   else
13777     expression = cp_parser_assignment_expression (parser);
13778
13779   return build_throw (expression);
13780 }
13781
13782 /* GNU Extensions */
13783
13784 /* Parse an (optional) asm-specification.
13785
13786    asm-specification:
13787      asm ( string-literal )
13788
13789    If the asm-specification is present, returns a STRING_CST
13790    corresponding to the string-literal.  Otherwise, returns
13791    NULL_TREE.  */
13792
13793 static tree
13794 cp_parser_asm_specification_opt (cp_parser* parser)
13795 {
13796   cp_token *token;
13797   tree asm_specification;
13798
13799   /* Peek at the next token.  */
13800   token = cp_lexer_peek_token (parser->lexer);
13801   /* If the next token isn't the `asm' keyword, then there's no
13802      asm-specification.  */
13803   if (!cp_parser_is_keyword (token, RID_ASM))
13804     return NULL_TREE;
13805
13806   /* Consume the `asm' token.  */
13807   cp_lexer_consume_token (parser->lexer);
13808   /* Look for the `('.  */
13809   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13810
13811   /* Look for the string-literal.  */
13812   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13813   if (token)
13814     asm_specification = token->value;
13815   else
13816     asm_specification = NULL_TREE;
13817
13818   /* Look for the `)'.  */
13819   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13820
13821   return asm_specification;
13822 }
13823
13824 /* Parse an asm-operand-list.
13825
13826    asm-operand-list:
13827      asm-operand
13828      asm-operand-list , asm-operand
13829
13830    asm-operand:
13831      string-literal ( expression )
13832      [ string-literal ] string-literal ( expression )
13833
13834    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13835    each node is the expression.  The TREE_PURPOSE is itself a
13836    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13837    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13838    is a STRING_CST for the string literal before the parenthesis.  */
13839
13840 static tree
13841 cp_parser_asm_operand_list (cp_parser* parser)
13842 {
13843   tree asm_operands = NULL_TREE;
13844
13845   while (true)
13846     {
13847       tree string_literal;
13848       tree expression;
13849       tree name;
13850       cp_token *token;
13851
13852       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13853         {
13854           /* Consume the `[' token.  */
13855           cp_lexer_consume_token (parser->lexer);
13856           /* Read the operand name.  */
13857           name = cp_parser_identifier (parser);
13858           if (name != error_mark_node)
13859             name = build_string (IDENTIFIER_LENGTH (name),
13860                                  IDENTIFIER_POINTER (name));
13861           /* Look for the closing `]'.  */
13862           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13863         }
13864       else
13865         name = NULL_TREE;
13866       /* Look for the string-literal.  */
13867       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13868       string_literal = token ? token->value : error_mark_node;
13869       c_lex_string_translate = 1;
13870       /* Look for the `('.  */
13871       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13872       /* Parse the expression.  */
13873       expression = cp_parser_expression (parser);
13874       /* Look for the `)'.  */
13875       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13876       c_lex_string_translate = 0;
13877       /* Add this operand to the list.  */
13878       asm_operands = tree_cons (build_tree_list (name, string_literal),
13879                                 expression,
13880                                 asm_operands);
13881       /* If the next token is not a `,', there are no more
13882          operands.  */
13883       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13884         break;
13885       /* Consume the `,'.  */
13886       cp_lexer_consume_token (parser->lexer);
13887     }
13888
13889   return nreverse (asm_operands);
13890 }
13891
13892 /* Parse an asm-clobber-list.
13893
13894    asm-clobber-list:
13895      string-literal
13896      asm-clobber-list , string-literal
13897
13898    Returns a TREE_LIST, indicating the clobbers in the order that they
13899    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13900
13901 static tree
13902 cp_parser_asm_clobber_list (cp_parser* parser)
13903 {
13904   tree clobbers = NULL_TREE;
13905
13906   while (true)
13907     {
13908       cp_token *token;
13909       tree string_literal;
13910
13911       /* Look for the string literal.  */
13912       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13913       string_literal = token ? token->value : error_mark_node;
13914       /* Add it to the list.  */
13915       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13916       /* If the next token is not a `,', then the list is
13917          complete.  */
13918       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13919         break;
13920       /* Consume the `,' token.  */
13921       cp_lexer_consume_token (parser->lexer);
13922     }
13923
13924   return clobbers;
13925 }
13926
13927 /* Parse an (optional) series of attributes.
13928
13929    attributes:
13930      attributes attribute
13931
13932    attribute:
13933      __attribute__ (( attribute-list [opt] ))
13934
13935    The return value is as for cp_parser_attribute_list.  */
13936
13937 static tree
13938 cp_parser_attributes_opt (cp_parser* parser)
13939 {
13940   tree attributes = NULL_TREE;
13941
13942   while (true)
13943     {
13944       cp_token *token;
13945       tree attribute_list;
13946
13947       /* Peek at the next token.  */
13948       token = cp_lexer_peek_token (parser->lexer);
13949       /* If it's not `__attribute__', then we're done.  */
13950       if (token->keyword != RID_ATTRIBUTE)
13951         break;
13952
13953       /* Consume the `__attribute__' keyword.  */
13954       cp_lexer_consume_token (parser->lexer);
13955       /* Look for the two `(' tokens.  */
13956       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13957       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13958
13959       /* Peek at the next token.  */
13960       token = cp_lexer_peek_token (parser->lexer);
13961       if (token->type != CPP_CLOSE_PAREN)
13962         /* Parse the attribute-list.  */
13963         attribute_list = cp_parser_attribute_list (parser);
13964       else
13965         /* If the next token is a `)', then there is no attribute
13966            list.  */
13967         attribute_list = NULL;
13968
13969       /* Look for the two `)' tokens.  */
13970       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13971       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13972
13973       /* Add these new attributes to the list.  */
13974       attributes = chainon (attributes, attribute_list);
13975     }
13976
13977   return attributes;
13978 }
13979
13980 /* Parse an attribute-list.
13981
13982    attribute-list:
13983      attribute
13984      attribute-list , attribute
13985
13986    attribute:
13987      identifier
13988      identifier ( identifier )
13989      identifier ( identifier , expression-list )
13990      identifier ( expression-list )
13991
13992    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13993    TREE_PURPOSE of each node is the identifier indicating which
13994    attribute is in use.  The TREE_VALUE represents the arguments, if
13995    any.  */
13996
13997 static tree
13998 cp_parser_attribute_list (cp_parser* parser)
13999 {
14000   tree attribute_list = NULL_TREE;
14001
14002   c_lex_string_translate = 0;
14003   while (true)
14004     {
14005       cp_token *token;
14006       tree identifier;
14007       tree attribute;
14008
14009       /* Look for the identifier.  We also allow keywords here; for
14010          example `__attribute__ ((const))' is legal.  */
14011       token = cp_lexer_peek_token (parser->lexer);
14012       if (token->type != CPP_NAME
14013           && token->type != CPP_KEYWORD)
14014         return error_mark_node;
14015       /* Consume the token.  */
14016       token = cp_lexer_consume_token (parser->lexer);
14017
14018       /* Save away the identifier that indicates which attribute this is.  */
14019       identifier = token->value;
14020       attribute = build_tree_list (identifier, NULL_TREE);
14021
14022       /* Peek at the next token.  */
14023       token = cp_lexer_peek_token (parser->lexer);
14024       /* If it's an `(', then parse the attribute arguments.  */
14025       if (token->type == CPP_OPEN_PAREN)
14026         {
14027           tree arguments;
14028
14029           arguments = (cp_parser_parenthesized_expression_list
14030                        (parser, true, /*non_constant_p=*/NULL));
14031           /* Save the identifier and arguments away.  */
14032           TREE_VALUE (attribute) = arguments;
14033         }
14034
14035       /* Add this attribute to the list.  */
14036       TREE_CHAIN (attribute) = attribute_list;
14037       attribute_list = attribute;
14038
14039       /* Now, look for more attributes.  */
14040       token = cp_lexer_peek_token (parser->lexer);
14041       /* If the next token isn't a `,', we're done.  */
14042       if (token->type != CPP_COMMA)
14043         break;
14044
14045       /* Consume the comma and keep going.  */
14046       cp_lexer_consume_token (parser->lexer);
14047     }
14048   c_lex_string_translate = 1;
14049
14050   /* We built up the list in reverse order.  */
14051   return nreverse (attribute_list);
14052 }
14053
14054 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14055    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14056    current value of the PEDANTIC flag, regardless of whether or not
14057    the `__extension__' keyword is present.  The caller is responsible
14058    for restoring the value of the PEDANTIC flag.  */
14059
14060 static bool
14061 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14062 {
14063   /* Save the old value of the PEDANTIC flag.  */
14064   *saved_pedantic = pedantic;
14065
14066   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14067     {
14068       /* Consume the `__extension__' token.  */
14069       cp_lexer_consume_token (parser->lexer);
14070       /* We're not being pedantic while the `__extension__' keyword is
14071          in effect.  */
14072       pedantic = 0;
14073
14074       return true;
14075     }
14076
14077   return false;
14078 }
14079
14080 /* Parse a label declaration.
14081
14082    label-declaration:
14083      __label__ label-declarator-seq ;
14084
14085    label-declarator-seq:
14086      identifier , label-declarator-seq
14087      identifier  */
14088
14089 static void
14090 cp_parser_label_declaration (cp_parser* parser)
14091 {
14092   /* Look for the `__label__' keyword.  */
14093   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14094
14095   while (true)
14096     {
14097       tree identifier;
14098
14099       /* Look for an identifier.  */
14100       identifier = cp_parser_identifier (parser);
14101       /* Declare it as a lobel.  */
14102       finish_label_decl (identifier);
14103       /* If the next token is a `;', stop.  */
14104       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14105         break;
14106       /* Look for the `,' separating the label declarations.  */
14107       cp_parser_require (parser, CPP_COMMA, "`,'");
14108     }
14109
14110   /* Look for the final `;'.  */
14111   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14112 }
14113
14114 /* Support Functions */
14115
14116 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14117    NAME should have one of the representations used for an
14118    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14119    is returned.  If PARSER->SCOPE is a dependent type, then a
14120    SCOPE_REF is returned.
14121
14122    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14123    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14124    was formed.  Abstractly, such entities should not be passed to this
14125    function, because they do not need to be looked up, but it is
14126    simpler to check for this special case here, rather than at the
14127    call-sites.
14128
14129    In cases not explicitly covered above, this function returns a
14130    DECL, OVERLOAD, or baselink representing the result of the lookup.
14131    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14132    is returned.
14133
14134    If IS_TYPE is TRUE, bindings that do not refer to types are
14135    ignored.
14136
14137    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14138    ignored.
14139
14140    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14141    are ignored.
14142
14143    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14144    types.  
14145
14146    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14147    results in an ambiguity, and false otherwise.  */
14148
14149 static tree
14150 cp_parser_lookup_name (cp_parser *parser, tree name,
14151                        bool is_type, bool is_template, bool is_namespace,
14152                        bool check_dependency,
14153                        bool *ambiguous_p)
14154 {
14155   tree decl;
14156   tree object_type = parser->context->object_type;
14157
14158   /* Assume that the lookup will be unambiguous.  */
14159   if (ambiguous_p)
14160     *ambiguous_p = false;
14161
14162   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14163      no longer valid.  Note that if we are parsing tentatively, and
14164      the parse fails, OBJECT_TYPE will be automatically restored.  */
14165   parser->context->object_type = NULL_TREE;
14166
14167   if (name == error_mark_node)
14168     return error_mark_node;
14169
14170   /* A template-id has already been resolved; there is no lookup to
14171      do.  */
14172   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14173     return name;
14174   if (BASELINK_P (name))
14175     {
14176       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14177                   == TEMPLATE_ID_EXPR);
14178       return name;
14179     }
14180
14181   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14182      it should already have been checked to make sure that the name
14183      used matches the type being destroyed.  */
14184   if (TREE_CODE (name) == BIT_NOT_EXPR)
14185     {
14186       tree type;
14187
14188       /* Figure out to which type this destructor applies.  */
14189       if (parser->scope)
14190         type = parser->scope;
14191       else if (object_type)
14192         type = object_type;
14193       else
14194         type = current_class_type;
14195       /* If that's not a class type, there is no destructor.  */
14196       if (!type || !CLASS_TYPE_P (type))
14197         return error_mark_node;
14198       if (!CLASSTYPE_DESTRUCTORS (type))
14199           return error_mark_node;
14200       /* If it was a class type, return the destructor.  */
14201       return CLASSTYPE_DESTRUCTORS (type);
14202     }
14203
14204   /* By this point, the NAME should be an ordinary identifier.  If
14205      the id-expression was a qualified name, the qualifying scope is
14206      stored in PARSER->SCOPE at this point.  */
14207   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14208
14209   /* Perform the lookup.  */
14210   if (parser->scope)
14211     {
14212       bool dependent_p;
14213
14214       if (parser->scope == error_mark_node)
14215         return error_mark_node;
14216
14217       /* If the SCOPE is dependent, the lookup must be deferred until
14218          the template is instantiated -- unless we are explicitly
14219          looking up names in uninstantiated templates.  Even then, we
14220          cannot look up the name if the scope is not a class type; it
14221          might, for example, be a template type parameter.  */
14222       dependent_p = (TYPE_P (parser->scope)
14223                      && !(parser->in_declarator_p
14224                           && currently_open_class (parser->scope))
14225                      && dependent_type_p (parser->scope));
14226       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14227            && dependent_p)
14228         {
14229           if (is_type)
14230             /* The resolution to Core Issue 180 says that `struct A::B'
14231                should be considered a type-name, even if `A' is
14232                dependent.  */
14233             decl = TYPE_NAME (make_typename_type (parser->scope,
14234                                                   name,
14235                                                   /*complain=*/1));
14236           else if (is_template)
14237             decl = make_unbound_class_template (parser->scope,
14238                                                 name,
14239                                                 /*complain=*/1);
14240           else
14241             decl = build_nt (SCOPE_REF, parser->scope, name);
14242         }
14243       else
14244         {
14245           bool pop_p = false;
14246
14247           /* If PARSER->SCOPE is a dependent type, then it must be a
14248              class type, and we must not be checking dependencies;
14249              otherwise, we would have processed this lookup above.  So
14250              that PARSER->SCOPE is not considered a dependent base by
14251              lookup_member, we must enter the scope here.  */
14252           if (dependent_p)
14253             pop_p = push_scope (parser->scope);
14254           /* If the PARSER->SCOPE is a a template specialization, it
14255              may be instantiated during name lookup.  In that case,
14256              errors may be issued.  Even if we rollback the current
14257              tentative parse, those errors are valid.  */
14258           decl = lookup_qualified_name (parser->scope, name, is_type,
14259                                         /*complain=*/true);
14260           if (pop_p)
14261             pop_scope (parser->scope);
14262         }
14263       parser->qualifying_scope = parser->scope;
14264       parser->object_scope = NULL_TREE;
14265     }
14266   else if (object_type)
14267     {
14268       tree object_decl = NULL_TREE;
14269       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14270          OBJECT_TYPE is not a class.  */
14271       if (CLASS_TYPE_P (object_type))
14272         /* If the OBJECT_TYPE is a template specialization, it may
14273            be instantiated during name lookup.  In that case, errors
14274            may be issued.  Even if we rollback the current tentative
14275            parse, those errors are valid.  */
14276         object_decl = lookup_member (object_type,
14277                                      name,
14278                                      /*protect=*/0, is_type);
14279       /* Look it up in the enclosing context, too.  */
14280       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14281                                /*block_p=*/true, is_namespace,
14282                                /*flags=*/0);
14283       parser->object_scope = object_type;
14284       parser->qualifying_scope = NULL_TREE;
14285       if (object_decl)
14286         decl = object_decl;
14287     }
14288   else
14289     {
14290       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14291                                /*block_p=*/true, is_namespace,
14292                                /*flags=*/0);
14293       parser->qualifying_scope = NULL_TREE;
14294       parser->object_scope = NULL_TREE;
14295     }
14296
14297   /* If the lookup failed, let our caller know.  */
14298   if (!decl
14299       || decl == error_mark_node
14300       || (TREE_CODE (decl) == FUNCTION_DECL
14301           && DECL_ANTICIPATED (decl)))
14302     return error_mark_node;
14303
14304   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14305   if (TREE_CODE (decl) == TREE_LIST)
14306     {
14307       if (ambiguous_p)
14308         *ambiguous_p = true;
14309       /* The error message we have to print is too complicated for
14310          cp_parser_error, so we incorporate its actions directly.  */
14311       if (!cp_parser_simulate_error (parser))
14312         {
14313           error ("reference to `%D' is ambiguous", name);
14314           print_candidates (decl);
14315         }
14316       return error_mark_node;
14317     }
14318
14319   gcc_assert (DECL_P (decl)
14320               || TREE_CODE (decl) == OVERLOAD
14321               || TREE_CODE (decl) == SCOPE_REF
14322               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14323               || BASELINK_P (decl));
14324
14325   /* If we have resolved the name of a member declaration, check to
14326      see if the declaration is accessible.  When the name resolves to
14327      set of overloaded functions, accessibility is checked when
14328      overload resolution is done.
14329
14330      During an explicit instantiation, access is not checked at all,
14331      as per [temp.explicit].  */
14332   if (DECL_P (decl))
14333     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14334
14335   return decl;
14336 }
14337
14338 /* Like cp_parser_lookup_name, but for use in the typical case where
14339    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14340    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14341
14342 static tree
14343 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14344 {
14345   return cp_parser_lookup_name (parser, name,
14346                                 /*is_type=*/false,
14347                                 /*is_template=*/false,
14348                                 /*is_namespace=*/false,
14349                                 /*check_dependency=*/true,
14350                                 /*ambiguous_p=*/NULL);
14351 }
14352
14353 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14354    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14355    true, the DECL indicates the class being defined in a class-head,
14356    or declared in an elaborated-type-specifier.
14357
14358    Otherwise, return DECL.  */
14359
14360 static tree
14361 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14362 {
14363   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14364      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14365
14366        struct A {
14367          template <typename T> struct B;
14368        };
14369
14370        template <typename T> struct A::B {};
14371
14372      Similarly, in a elaborated-type-specifier:
14373
14374        namespace N { struct X{}; }
14375
14376        struct A {
14377          template <typename T> friend struct N::X;
14378        };
14379
14380      However, if the DECL refers to a class type, and we are in
14381      the scope of the class, then the name lookup automatically
14382      finds the TYPE_DECL created by build_self_reference rather
14383      than a TEMPLATE_DECL.  For example, in:
14384
14385        template <class T> struct S {
14386          S s;
14387        };
14388
14389      there is no need to handle such case.  */
14390
14391   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14392     return DECL_TEMPLATE_RESULT (decl);
14393
14394   return decl;
14395 }
14396
14397 /* If too many, or too few, template-parameter lists apply to the
14398    declarator, issue an error message.  Returns TRUE if all went well,
14399    and FALSE otherwise.  */
14400
14401 static bool
14402 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14403                                                 cp_declarator *declarator)
14404 {
14405   unsigned num_templates;
14406
14407   /* We haven't seen any classes that involve template parameters yet.  */
14408   num_templates = 0;
14409
14410   switch (declarator->kind)
14411     {
14412     case cdk_id:
14413       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14414         {
14415           tree scope;
14416           tree member;
14417
14418           scope = TREE_OPERAND (declarator->u.id.name, 0);
14419           member = TREE_OPERAND (declarator->u.id.name, 1);
14420
14421           while (scope && CLASS_TYPE_P (scope))
14422             {
14423               /* You're supposed to have one `template <...>'
14424                  for every template class, but you don't need one
14425                  for a full specialization.  For example:
14426
14427                  template <class T> struct S{};
14428                  template <> struct S<int> { void f(); };
14429                  void S<int>::f () {}
14430
14431                  is correct; there shouldn't be a `template <>' for
14432                  the definition of `S<int>::f'.  */
14433               if (CLASSTYPE_TEMPLATE_INFO (scope)
14434                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14435                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14436                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14437                 ++num_templates;
14438
14439               scope = TYPE_CONTEXT (scope);
14440             }
14441         }
14442
14443       /* If the DECLARATOR has the form `X<y>' then it uses one
14444          additional level of template parameters.  */
14445       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14446         ++num_templates;
14447
14448       return cp_parser_check_template_parameters (parser,
14449                                                   num_templates);
14450
14451     case cdk_function:
14452     case cdk_array:
14453     case cdk_pointer:
14454     case cdk_reference:
14455     case cdk_ptrmem:
14456       return (cp_parser_check_declarator_template_parameters
14457               (parser, declarator->declarator));
14458
14459     case cdk_error:
14460       return true;
14461
14462     default:
14463       gcc_unreachable ();
14464     }
14465   return false;
14466 }
14467
14468 /* NUM_TEMPLATES were used in the current declaration.  If that is
14469    invalid, return FALSE and issue an error messages.  Otherwise,
14470    return TRUE.  */
14471
14472 static bool
14473 cp_parser_check_template_parameters (cp_parser* parser,
14474                                      unsigned num_templates)
14475 {
14476   /* If there are more template classes than parameter lists, we have
14477      something like:
14478
14479        template <class T> void S<T>::R<T>::f ();  */
14480   if (parser->num_template_parameter_lists < num_templates)
14481     {
14482       error ("too few template-parameter-lists");
14483       return false;
14484     }
14485   /* If there are the same number of template classes and parameter
14486      lists, that's OK.  */
14487   if (parser->num_template_parameter_lists == num_templates)
14488     return true;
14489   /* If there are more, but only one more, then we are referring to a
14490      member template.  That's OK too.  */
14491   if (parser->num_template_parameter_lists == num_templates + 1)
14492       return true;
14493   /* Otherwise, there are too many template parameter lists.  We have
14494      something like:
14495
14496      template <class T> template <class U> void S::f();  */
14497   error ("too many template-parameter-lists");
14498   return false;
14499 }
14500
14501 /* Parse a binary-expression of the general form:
14502
14503    binary-expression:
14504      <expr>
14505      binary-expression <token> <expr>
14506
14507    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14508    to parser the <expr>s.  If the first production is used, then the
14509    value returned by FN is returned directly.  Otherwise, a node with
14510    the indicated EXPR_TYPE is returned, with operands corresponding to
14511    the two sub-expressions.  */
14512
14513 static tree
14514 cp_parser_binary_expression (cp_parser* parser,
14515                              const cp_parser_token_tree_map token_tree_map,
14516                              cp_parser_expression_fn fn)
14517 {
14518   tree lhs;
14519
14520   /* Parse the first expression.  */
14521   lhs = (*fn) (parser);
14522   /* Now, look for more expressions.  */
14523   while (true)
14524     {
14525       cp_token *token;
14526       const cp_parser_token_tree_map_node *map_node;
14527       tree rhs;
14528
14529       /* Peek at the next token.  */
14530       token = cp_lexer_peek_token (parser->lexer);
14531       /* If the token is `>', and that's not an operator at the
14532          moment, then we're done.  */
14533       if (token->type == CPP_GREATER
14534           && !parser->greater_than_is_operator_p)
14535         break;
14536       /* If we find one of the tokens we want, build the corresponding
14537          tree representation.  */
14538       for (map_node = token_tree_map;
14539            map_node->token_type != CPP_EOF;
14540            ++map_node)
14541         if (map_node->token_type == token->type)
14542           {
14543             /* Assume that an overloaded operator will not be used.  */
14544             bool overloaded_p = false;
14545
14546             /* Consume the operator token.  */
14547             cp_lexer_consume_token (parser->lexer);
14548             /* Parse the right-hand side of the expression.  */
14549             rhs = (*fn) (parser);
14550             /* Build the binary tree node.  */
14551             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14552                                      &overloaded_p);
14553             /* If the binary operator required the use of an
14554                overloaded operator, then this expression cannot be an
14555                integral constant-expression.  An overloaded operator
14556                can be used even if both operands are otherwise
14557                permissible in an integral constant-expression if at
14558                least one of the operands is of enumeration type.  */
14559             if (overloaded_p
14560                 && (cp_parser_non_integral_constant_expression
14561                     (parser, "calls to overloaded operators")))
14562               lhs = error_mark_node;
14563             break;
14564           }
14565
14566       /* If the token wasn't one of the ones we want, we're done.  */
14567       if (map_node->token_type == CPP_EOF)
14568         break;
14569     }
14570
14571   return lhs;
14572 }
14573
14574 /* Parse an optional `::' token indicating that the following name is
14575    from the global namespace.  If so, PARSER->SCOPE is set to the
14576    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14577    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14578    Returns the new value of PARSER->SCOPE, if the `::' token is
14579    present, and NULL_TREE otherwise.  */
14580
14581 static tree
14582 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14583 {
14584   cp_token *token;
14585
14586   /* Peek at the next token.  */
14587   token = cp_lexer_peek_token (parser->lexer);
14588   /* If we're looking at a `::' token then we're starting from the
14589      global namespace, not our current location.  */
14590   if (token->type == CPP_SCOPE)
14591     {
14592       /* Consume the `::' token.  */
14593       cp_lexer_consume_token (parser->lexer);
14594       /* Set the SCOPE so that we know where to start the lookup.  */
14595       parser->scope = global_namespace;
14596       parser->qualifying_scope = global_namespace;
14597       parser->object_scope = NULL_TREE;
14598
14599       return parser->scope;
14600     }
14601   else if (!current_scope_valid_p)
14602     {
14603       parser->scope = NULL_TREE;
14604       parser->qualifying_scope = NULL_TREE;
14605       parser->object_scope = NULL_TREE;
14606     }
14607
14608   return NULL_TREE;
14609 }
14610
14611 /* Returns TRUE if the upcoming token sequence is the start of a
14612    constructor declarator.  If FRIEND_P is true, the declarator is
14613    preceded by the `friend' specifier.  */
14614
14615 static bool
14616 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14617 {
14618   bool constructor_p;
14619   tree type_decl = NULL_TREE;
14620   bool nested_name_p;
14621   cp_token *next_token;
14622
14623   /* The common case is that this is not a constructor declarator, so
14624      try to avoid doing lots of work if at all possible.  It's not
14625      valid declare a constructor at function scope.  */
14626   if (at_function_scope_p ())
14627     return false;
14628   /* And only certain tokens can begin a constructor declarator.  */
14629   next_token = cp_lexer_peek_token (parser->lexer);
14630   if (next_token->type != CPP_NAME
14631       && next_token->type != CPP_SCOPE
14632       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14633       && next_token->type != CPP_TEMPLATE_ID)
14634     return false;
14635
14636   /* Parse tentatively; we are going to roll back all of the tokens
14637      consumed here.  */
14638   cp_parser_parse_tentatively (parser);
14639   /* Assume that we are looking at a constructor declarator.  */
14640   constructor_p = true;
14641
14642   /* Look for the optional `::' operator.  */
14643   cp_parser_global_scope_opt (parser,
14644                               /*current_scope_valid_p=*/false);
14645   /* Look for the nested-name-specifier.  */
14646   nested_name_p
14647     = (cp_parser_nested_name_specifier_opt (parser,
14648                                             /*typename_keyword_p=*/false,
14649                                             /*check_dependency_p=*/false,
14650                                             /*type_p=*/false,
14651                                             /*is_declaration=*/false)
14652        != NULL_TREE);
14653   /* Outside of a class-specifier, there must be a
14654      nested-name-specifier.  */
14655   if (!nested_name_p &&
14656       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14657        || friend_p))
14658     constructor_p = false;
14659   /* If we still think that this might be a constructor-declarator,
14660      look for a class-name.  */
14661   if (constructor_p)
14662     {
14663       /* If we have:
14664
14665            template <typename T> struct S { S(); };
14666            template <typename T> S<T>::S ();
14667
14668          we must recognize that the nested `S' names a class.
14669          Similarly, for:
14670
14671            template <typename T> S<T>::S<T> ();
14672
14673          we must recognize that the nested `S' names a template.  */
14674       type_decl = cp_parser_class_name (parser,
14675                                         /*typename_keyword_p=*/false,
14676                                         /*template_keyword_p=*/false,
14677                                         /*type_p=*/false,
14678                                         /*check_dependency_p=*/false,
14679                                         /*class_head_p=*/false,
14680                                         /*is_declaration=*/false);
14681       /* If there was no class-name, then this is not a constructor.  */
14682       constructor_p = !cp_parser_error_occurred (parser);
14683     }
14684
14685   /* If we're still considering a constructor, we have to see a `(',
14686      to begin the parameter-declaration-clause, followed by either a
14687      `)', an `...', or a decl-specifier.  We need to check for a
14688      type-specifier to avoid being fooled into thinking that:
14689
14690        S::S (f) (int);
14691
14692      is a constructor.  (It is actually a function named `f' that
14693      takes one parameter (of type `int') and returns a value of type
14694      `S::S'.  */
14695   if (constructor_p
14696       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14697     {
14698       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14699           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14700           /* A parameter declaration begins with a decl-specifier,
14701              which is either the "attribute" keyword, a storage class
14702              specifier, or (usually) a type-specifier.  */
14703           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14704           && !cp_parser_storage_class_specifier_opt (parser))
14705         {
14706           tree type;
14707           bool pop_p = false;
14708           unsigned saved_num_template_parameter_lists;
14709
14710           /* Names appearing in the type-specifier should be looked up
14711              in the scope of the class.  */
14712           if (current_class_type)
14713             type = NULL_TREE;
14714           else
14715             {
14716               type = TREE_TYPE (type_decl);
14717               if (TREE_CODE (type) == TYPENAME_TYPE)
14718                 {
14719                   type = resolve_typename_type (type,
14720                                                 /*only_current_p=*/false);
14721                   if (type == error_mark_node)
14722                     {
14723                       cp_parser_abort_tentative_parse (parser);
14724                       return false;
14725                     }
14726                 }
14727               pop_p = push_scope (type);
14728             }
14729
14730           /* Inside the constructor parameter list, surrounding
14731              template-parameter-lists do not apply.  */
14732           saved_num_template_parameter_lists
14733             = parser->num_template_parameter_lists;
14734           parser->num_template_parameter_lists = 0;
14735
14736           /* Look for the type-specifier.  */
14737           cp_parser_type_specifier (parser,
14738                                     CP_PARSER_FLAGS_NONE,
14739                                     /*decl_specs=*/NULL,
14740                                     /*is_declarator=*/true,
14741                                     /*declares_class_or_enum=*/NULL,
14742                                     /*is_cv_qualifier=*/NULL);
14743
14744           parser->num_template_parameter_lists
14745             = saved_num_template_parameter_lists;
14746
14747           /* Leave the scope of the class.  */
14748           if (pop_p)
14749             pop_scope (type);
14750
14751           constructor_p = !cp_parser_error_occurred (parser);
14752         }
14753     }
14754   else
14755     constructor_p = false;
14756   /* We did not really want to consume any tokens.  */
14757   cp_parser_abort_tentative_parse (parser);
14758
14759   return constructor_p;
14760 }
14761
14762 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14763    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14764    they must be performed once we are in the scope of the function.
14765
14766    Returns the function defined.  */
14767
14768 static tree
14769 cp_parser_function_definition_from_specifiers_and_declarator
14770   (cp_parser* parser,
14771    cp_decl_specifier_seq *decl_specifiers,
14772    tree attributes,
14773    const cp_declarator *declarator)
14774 {
14775   tree fn;
14776   bool success_p;
14777
14778   /* Begin the function-definition.  */
14779   success_p = start_function (decl_specifiers, declarator, attributes);
14780
14781   /* The things we're about to see are not directly qualified by any
14782      template headers we've seen thus far.  */
14783   reset_specialization ();
14784
14785   /* If there were names looked up in the decl-specifier-seq that we
14786      did not check, check them now.  We must wait until we are in the
14787      scope of the function to perform the checks, since the function
14788      might be a friend.  */
14789   perform_deferred_access_checks ();
14790
14791   if (!success_p)
14792     {
14793       /* Skip the entire function.  */
14794       error ("invalid function declaration");
14795       cp_parser_skip_to_end_of_block_or_statement (parser);
14796       fn = error_mark_node;
14797     }
14798   else
14799     fn = cp_parser_function_definition_after_declarator (parser,
14800                                                          /*inline_p=*/false);
14801
14802   return fn;
14803 }
14804
14805 /* Parse the part of a function-definition that follows the
14806    declarator.  INLINE_P is TRUE iff this function is an inline
14807    function defined with a class-specifier.
14808
14809    Returns the function defined.  */
14810
14811 static tree
14812 cp_parser_function_definition_after_declarator (cp_parser* parser,
14813                                                 bool inline_p)
14814 {
14815   tree fn;
14816   bool ctor_initializer_p = false;
14817   bool saved_in_unbraced_linkage_specification_p;
14818   unsigned saved_num_template_parameter_lists;
14819
14820   /* If the next token is `return', then the code may be trying to
14821      make use of the "named return value" extension that G++ used to
14822      support.  */
14823   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14824     {
14825       /* Consume the `return' keyword.  */
14826       cp_lexer_consume_token (parser->lexer);
14827       /* Look for the identifier that indicates what value is to be
14828          returned.  */
14829       cp_parser_identifier (parser);
14830       /* Issue an error message.  */
14831       error ("named return values are no longer supported");
14832       /* Skip tokens until we reach the start of the function body.  */
14833       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14834              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14835         cp_lexer_consume_token (parser->lexer);
14836     }
14837   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14838      anything declared inside `f'.  */
14839   saved_in_unbraced_linkage_specification_p
14840     = parser->in_unbraced_linkage_specification_p;
14841   parser->in_unbraced_linkage_specification_p = false;
14842   /* Inside the function, surrounding template-parameter-lists do not
14843      apply.  */
14844   saved_num_template_parameter_lists
14845     = parser->num_template_parameter_lists;
14846   parser->num_template_parameter_lists = 0;
14847   /* If the next token is `try', then we are looking at a
14848      function-try-block.  */
14849   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14850     ctor_initializer_p = cp_parser_function_try_block (parser);
14851   /* A function-try-block includes the function-body, so we only do
14852      this next part if we're not processing a function-try-block.  */
14853   else
14854     ctor_initializer_p
14855       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14856
14857   /* Finish the function.  */
14858   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14859                         (inline_p ? 2 : 0));
14860   /* Generate code for it, if necessary.  */
14861   expand_or_defer_fn (fn);
14862   /* Restore the saved values.  */
14863   parser->in_unbraced_linkage_specification_p
14864     = saved_in_unbraced_linkage_specification_p;
14865   parser->num_template_parameter_lists
14866     = saved_num_template_parameter_lists;
14867
14868   return fn;
14869 }
14870
14871 /* Parse a template-declaration, assuming that the `export' (and
14872    `extern') keywords, if present, has already been scanned.  MEMBER_P
14873    is as for cp_parser_template_declaration.  */
14874
14875 static void
14876 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14877 {
14878   tree decl = NULL_TREE;
14879   tree parameter_list;
14880   bool friend_p = false;
14881
14882   /* Look for the `template' keyword.  */
14883   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14884     return;
14885
14886   /* And the `<'.  */
14887   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14888     return;
14889
14890   /* If the next token is `>', then we have an invalid
14891      specialization.  Rather than complain about an invalid template
14892      parameter, issue an error message here.  */
14893   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14894     {
14895       cp_parser_error (parser, "invalid explicit specialization");
14896       begin_specialization ();
14897       parameter_list = NULL_TREE;
14898     }
14899   else
14900     {
14901       /* Parse the template parameters.  */
14902       begin_template_parm_list ();
14903       parameter_list = cp_parser_template_parameter_list (parser);
14904       parameter_list = end_template_parm_list (parameter_list);
14905     }
14906
14907   /* Look for the `>'.  */
14908   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14909   /* We just processed one more parameter list.  */
14910   ++parser->num_template_parameter_lists;
14911   /* If the next token is `template', there are more template
14912      parameters.  */
14913   if (cp_lexer_next_token_is_keyword (parser->lexer,
14914                                       RID_TEMPLATE))
14915     cp_parser_template_declaration_after_export (parser, member_p);
14916   else
14917     {
14918       /* There are no access checks when parsing a template, as we do not
14919          know if a specialization will be a friend.  */
14920       push_deferring_access_checks (dk_no_check);
14921
14922       decl = cp_parser_single_declaration (parser,
14923                                            member_p,
14924                                            &friend_p);
14925
14926       pop_deferring_access_checks ();
14927
14928       /* If this is a member template declaration, let the front
14929          end know.  */
14930       if (member_p && !friend_p && decl)
14931         {
14932           if (TREE_CODE (decl) == TYPE_DECL)
14933             cp_parser_check_access_in_redeclaration (decl);
14934
14935           decl = finish_member_template_decl (decl);
14936         }
14937       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14938         make_friend_class (current_class_type, TREE_TYPE (decl),
14939                            /*complain=*/true);
14940     }
14941   /* We are done with the current parameter list.  */
14942   --parser->num_template_parameter_lists;
14943
14944   /* Finish up.  */
14945   finish_template_decl (parameter_list);
14946
14947   /* Register member declarations.  */
14948   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14949     finish_member_declaration (decl);
14950
14951   /* If DECL is a function template, we must return to parse it later.
14952      (Even though there is no definition, there might be default
14953      arguments that need handling.)  */
14954   if (member_p && decl
14955       && (TREE_CODE (decl) == FUNCTION_DECL
14956           || DECL_FUNCTION_TEMPLATE_P (decl)))
14957     TREE_VALUE (parser->unparsed_functions_queues)
14958       = tree_cons (NULL_TREE, decl,
14959                    TREE_VALUE (parser->unparsed_functions_queues));
14960 }
14961
14962 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14963    `function-definition' sequence.  MEMBER_P is true, this declaration
14964    appears in a class scope.
14965
14966    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14967    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14968
14969 static tree
14970 cp_parser_single_declaration (cp_parser* parser,
14971                               bool member_p,
14972                               bool* friend_p)
14973 {
14974   int declares_class_or_enum;
14975   tree decl = NULL_TREE;
14976   cp_decl_specifier_seq decl_specifiers;
14977   bool function_definition_p = false;
14978
14979   /* Defer access checks until we know what is being declared.  */
14980   push_deferring_access_checks (dk_deferred);
14981
14982   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14983      alternative.  */
14984   cp_parser_decl_specifier_seq (parser,
14985                                 CP_PARSER_FLAGS_OPTIONAL,
14986                                 &decl_specifiers,
14987                                 &declares_class_or_enum);
14988   if (friend_p)
14989     *friend_p = cp_parser_friend_p (&decl_specifiers);
14990   /* Gather up the access checks that occurred the
14991      decl-specifier-seq.  */
14992   stop_deferring_access_checks ();
14993
14994   /* Check for the declaration of a template class.  */
14995   if (declares_class_or_enum)
14996     {
14997       if (cp_parser_declares_only_class_p (parser))
14998         {
14999           decl = shadow_tag (&decl_specifiers);
15000           if (decl && decl != error_mark_node)
15001             decl = TYPE_NAME (decl);
15002           else
15003             decl = error_mark_node;
15004         }
15005     }
15006   else
15007     decl = NULL_TREE;
15008   /* If it's not a template class, try for a template function.  If
15009      the next token is a `;', then this declaration does not declare
15010      anything.  But, if there were errors in the decl-specifiers, then
15011      the error might well have come from an attempted class-specifier.
15012      In that case, there's no need to warn about a missing declarator.  */
15013   if (!decl
15014       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15015           || decl_specifiers.type != error_mark_node))
15016     decl = cp_parser_init_declarator (parser,
15017                                       &decl_specifiers,
15018                                       /*function_definition_allowed_p=*/true,
15019                                       member_p,
15020                                       declares_class_or_enum,
15021                                       &function_definition_p);
15022
15023   pop_deferring_access_checks ();
15024
15025   /* Clear any current qualification; whatever comes next is the start
15026      of something new.  */
15027   parser->scope = NULL_TREE;
15028   parser->qualifying_scope = NULL_TREE;
15029   parser->object_scope = NULL_TREE;
15030   /* Look for a trailing `;' after the declaration.  */
15031   if (!function_definition_p
15032       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
15033     cp_parser_skip_to_end_of_block_or_statement (parser);
15034
15035   return decl;
15036 }
15037
15038 /* Parse a cast-expression that is not the operand of a unary "&".  */
15039
15040 static tree
15041 cp_parser_simple_cast_expression (cp_parser *parser)
15042 {
15043   return cp_parser_cast_expression (parser, /*address_p=*/false);
15044 }
15045
15046 /* Parse a functional cast to TYPE.  Returns an expression
15047    representing the cast.  */
15048
15049 static tree
15050 cp_parser_functional_cast (cp_parser* parser, tree type)
15051 {
15052   tree expression_list;
15053   tree cast;
15054
15055   expression_list
15056     = cp_parser_parenthesized_expression_list (parser, false,
15057                                                /*non_constant_p=*/NULL);
15058
15059   cast = build_functional_cast (type, expression_list);
15060   /* [expr.const]/1: In an integral constant expression "only type
15061      conversions to integral or enumeration type can be used".  */
15062   if (cast != error_mark_node && !type_dependent_expression_p (type)
15063       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15064     {
15065       if (cp_parser_non_integral_constant_expression
15066           (parser, "a call to a constructor"))
15067         return error_mark_node;
15068     }
15069   return cast;
15070 }
15071
15072 /* Save the tokens that make up the body of a member function defined
15073    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15074    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15075    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15076    for the member function.  */
15077
15078 static tree
15079 cp_parser_save_member_function_body (cp_parser* parser,
15080                                      cp_decl_specifier_seq *decl_specifiers,
15081                                      cp_declarator *declarator,
15082                                      tree attributes)
15083 {
15084   cp_token_cache *cache;
15085   tree fn;
15086
15087   /* Create the function-declaration.  */
15088   fn = start_method (decl_specifiers, declarator, attributes);
15089   /* If something went badly wrong, bail out now.  */
15090   if (fn == error_mark_node)
15091     {
15092       /* If there's a function-body, skip it.  */
15093       if (cp_parser_token_starts_function_definition_p
15094           (cp_lexer_peek_token (parser->lexer)))
15095         cp_parser_skip_to_end_of_block_or_statement (parser);
15096       return error_mark_node;
15097     }
15098
15099   /* Remember it, if there default args to post process.  */
15100   cp_parser_save_default_args (parser, fn);
15101
15102   /* Create a token cache.  */
15103   cache = cp_token_cache_new ();
15104   /* Save away the tokens that make up the body of the
15105      function.  */
15106   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15107   /* Handle function try blocks.  */
15108   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15109     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15110
15111   /* Save away the inline definition; we will process it when the
15112      class is complete.  */
15113   DECL_PENDING_INLINE_INFO (fn) = cache;
15114   DECL_PENDING_INLINE_P (fn) = 1;
15115
15116   /* We need to know that this was defined in the class, so that
15117      friend templates are handled correctly.  */
15118   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15119
15120   /* We're done with the inline definition.  */
15121   finish_method (fn);
15122
15123   /* Add FN to the queue of functions to be parsed later.  */
15124   TREE_VALUE (parser->unparsed_functions_queues)
15125     = tree_cons (NULL_TREE, fn,
15126                  TREE_VALUE (parser->unparsed_functions_queues));
15127
15128   return fn;
15129 }
15130
15131 /* Parse a template-argument-list, as well as the trailing ">" (but
15132    not the opening ">").  See cp_parser_template_argument_list for the
15133    return value.  */
15134
15135 static tree
15136 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15137 {
15138   tree arguments;
15139   tree saved_scope;
15140   tree saved_qualifying_scope;
15141   tree saved_object_scope;
15142   bool saved_greater_than_is_operator_p;
15143
15144   /* [temp.names]
15145
15146      When parsing a template-id, the first non-nested `>' is taken as
15147      the end of the template-argument-list rather than a greater-than
15148      operator.  */
15149   saved_greater_than_is_operator_p
15150     = parser->greater_than_is_operator_p;
15151   parser->greater_than_is_operator_p = false;
15152   /* Parsing the argument list may modify SCOPE, so we save it
15153      here.  */
15154   saved_scope = parser->scope;
15155   saved_qualifying_scope = parser->qualifying_scope;
15156   saved_object_scope = parser->object_scope;
15157   /* Parse the template-argument-list itself.  */
15158   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15159     arguments = NULL_TREE;
15160   else
15161     arguments = cp_parser_template_argument_list (parser);
15162   /* Look for the `>' that ends the template-argument-list. If we find
15163      a '>>' instead, it's probably just a typo.  */
15164   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15165     {
15166       if (!saved_greater_than_is_operator_p)
15167         {
15168           /* If we're in a nested template argument list, the '>>' has to be
15169             a typo for '> >'. We emit the error message, but we continue
15170             parsing and we push a '>' as next token, so that the argument
15171             list will be parsed correctly..  */
15172           cp_token* token;
15173           error ("`>>' should be `> >' within a nested template argument list");
15174           token = cp_lexer_peek_token (parser->lexer);
15175           token->type = CPP_GREATER;
15176         }
15177       else
15178         {
15179           /* If this is not a nested template argument list, the '>>' is
15180             a typo for '>'. Emit an error message and continue.  */
15181           error ("spurious `>>', use `>' to terminate a template argument list");
15182           cp_lexer_consume_token (parser->lexer);
15183         }
15184     }
15185   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15186     error ("missing `>' to terminate the template argument list");
15187   /* The `>' token might be a greater-than operator again now.  */
15188   parser->greater_than_is_operator_p
15189     = saved_greater_than_is_operator_p;
15190   /* Restore the SAVED_SCOPE.  */
15191   parser->scope = saved_scope;
15192   parser->qualifying_scope = saved_qualifying_scope;
15193   parser->object_scope = saved_object_scope;
15194
15195   return arguments;
15196 }
15197
15198 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15199    arguments, or the body of the function have not yet been parsed,
15200    parse them now.  */
15201
15202 static void
15203 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15204 {
15205   cp_lexer *saved_lexer;
15206
15207   /* If this member is a template, get the underlying
15208      FUNCTION_DECL.  */
15209   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15210     member_function = DECL_TEMPLATE_RESULT (member_function);
15211
15212   /* There should not be any class definitions in progress at this
15213      point; the bodies of members are only parsed outside of all class
15214      definitions.  */
15215   gcc_assert (parser->num_classes_being_defined == 0);
15216   /* While we're parsing the member functions we might encounter more
15217      classes.  We want to handle them right away, but we don't want
15218      them getting mixed up with functions that are currently in the
15219      queue.  */
15220   parser->unparsed_functions_queues
15221     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15222
15223   /* Make sure that any template parameters are in scope.  */
15224   maybe_begin_member_template_processing (member_function);
15225
15226   /* If the body of the function has not yet been parsed, parse it
15227      now.  */
15228   if (DECL_PENDING_INLINE_P (member_function))
15229     {
15230       tree function_scope;
15231       cp_token_cache *tokens;
15232
15233       /* The function is no longer pending; we are processing it.  */
15234       tokens = DECL_PENDING_INLINE_INFO (member_function);
15235       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15236       DECL_PENDING_INLINE_P (member_function) = 0;
15237       /* If this was an inline function in a local class, enter the scope
15238          of the containing function.  */
15239       function_scope = decl_function_context (member_function);
15240       if (function_scope)
15241         push_function_context_to (function_scope);
15242
15243       /* Save away the current lexer.  */
15244       saved_lexer = parser->lexer;
15245       /* Make a new lexer to feed us the tokens saved for this function.  */
15246       parser->lexer = cp_lexer_new_from_tokens (tokens);
15247       parser->lexer->next = saved_lexer;
15248
15249       /* Set the current source position to be the location of the first
15250          token in the saved inline body.  */
15251       cp_lexer_peek_token (parser->lexer);
15252
15253       /* Let the front end know that we going to be defining this
15254          function.  */
15255       start_preparsed_function (member_function, NULL_TREE,
15256                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15257
15258       /* Now, parse the body of the function.  */
15259       cp_parser_function_definition_after_declarator (parser,
15260                                                       /*inline_p=*/true);
15261
15262       /* Leave the scope of the containing function.  */
15263       if (function_scope)
15264         pop_function_context_from (function_scope);
15265       /* Restore the lexer.  */
15266       parser->lexer = saved_lexer;
15267     }
15268
15269   /* Remove any template parameters from the symbol table.  */
15270   maybe_end_member_template_processing ();
15271
15272   /* Restore the queue.  */
15273   parser->unparsed_functions_queues
15274     = TREE_CHAIN (parser->unparsed_functions_queues);
15275 }
15276
15277 /* If DECL contains any default args, remember it on the unparsed
15278    functions queue.  */
15279
15280 static void
15281 cp_parser_save_default_args (cp_parser* parser, tree decl)
15282 {
15283   tree probe;
15284
15285   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15286        probe;
15287        probe = TREE_CHAIN (probe))
15288     if (TREE_PURPOSE (probe))
15289       {
15290         TREE_PURPOSE (parser->unparsed_functions_queues)
15291           = tree_cons (current_class_type, decl,
15292                        TREE_PURPOSE (parser->unparsed_functions_queues));
15293         break;
15294       }
15295   return;
15296 }
15297
15298 /* FN is a FUNCTION_DECL which may contains a parameter with an
15299    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15300    assumes that the current scope is the scope in which the default
15301    argument should be processed.  */
15302
15303 static void
15304 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15305 {
15306   cp_lexer *saved_lexer;
15307   cp_token_cache *tokens;
15308   bool saved_local_variables_forbidden_p;
15309   tree parameters;
15310
15311   /* While we're parsing the default args, we might (due to the
15312      statement expression extension) encounter more classes.  We want
15313      to handle them right away, but we don't want them getting mixed
15314      up with default args that are currently in the queue.  */
15315   parser->unparsed_functions_queues
15316     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15317
15318   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15319        parameters;
15320        parameters = TREE_CHAIN (parameters))
15321     {
15322       if (!TREE_PURPOSE (parameters)
15323           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15324         continue;
15325
15326        /* Save away the current lexer.  */
15327       saved_lexer = parser->lexer;
15328        /* Create a new one, using the tokens we have saved.  */
15329       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
15330       parser->lexer = cp_lexer_new_from_tokens (tokens);
15331
15332        /* Set the current source position to be the location of the
15333           first token in the default argument.  */
15334       cp_lexer_peek_token (parser->lexer);
15335
15336        /* Local variable names (and the `this' keyword) may not appear
15337           in a default argument.  */
15338       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15339       parser->local_variables_forbidden_p = true;
15340        /* Parse the assignment-expression.  */
15341       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15342
15343       /* If the token stream has not been completely used up, then
15344          there was extra junk after the end of the default
15345          argument.  */
15346       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15347         cp_parser_error (parser, "expected `,'");
15348
15349        /* Restore saved state.  */
15350       parser->lexer = saved_lexer;
15351       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15352     }
15353
15354   /* Restore the queue.  */
15355   parser->unparsed_functions_queues
15356     = TREE_CHAIN (parser->unparsed_functions_queues);
15357 }
15358
15359 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15360    either a TYPE or an expression, depending on the form of the
15361    input.  The KEYWORD indicates which kind of expression we have
15362    encountered.  */
15363
15364 static tree
15365 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15366 {
15367   static const char *format;
15368   tree expr = NULL_TREE;
15369   const char *saved_message;
15370   bool saved_integral_constant_expression_p;
15371
15372   /* Initialize FORMAT the first time we get here.  */
15373   if (!format)
15374     format = "types may not be defined in `%s' expressions";
15375
15376   /* Types cannot be defined in a `sizeof' expression.  Save away the
15377      old message.  */
15378   saved_message = parser->type_definition_forbidden_message;
15379   /* And create the new one.  */
15380   parser->type_definition_forbidden_message
15381     = xmalloc (strlen (format)
15382                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15383                + 1 /* `\0' */);
15384   sprintf ((char *) parser->type_definition_forbidden_message,
15385            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15386
15387   /* The restrictions on constant-expressions do not apply inside
15388      sizeof expressions.  */
15389   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15390   parser->integral_constant_expression_p = false;
15391
15392   /* Do not actually evaluate the expression.  */
15393   ++skip_evaluation;
15394   /* If it's a `(', then we might be looking at the type-id
15395      construction.  */
15396   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15397     {
15398       tree type;
15399       bool saved_in_type_id_in_expr_p;
15400
15401       /* We can't be sure yet whether we're looking at a type-id or an
15402          expression.  */
15403       cp_parser_parse_tentatively (parser);
15404       /* Consume the `('.  */
15405       cp_lexer_consume_token (parser->lexer);
15406       /* Parse the type-id.  */
15407       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15408       parser->in_type_id_in_expr_p = true;
15409       type = cp_parser_type_id (parser);
15410       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15411       /* Now, look for the trailing `)'.  */
15412       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15413       /* If all went well, then we're done.  */
15414       if (cp_parser_parse_definitely (parser))
15415         {
15416           cp_decl_specifier_seq decl_specs;
15417
15418           /* Build a trivial decl-specifier-seq.  */
15419           clear_decl_specs (&decl_specs);
15420           decl_specs.type = type;
15421
15422           /* Call grokdeclarator to figure out what type this is.  */
15423           expr = grokdeclarator (NULL,
15424                                  &decl_specs,
15425                                  TYPENAME,
15426                                  /*initialized=*/0,
15427                                  /*attrlist=*/NULL);
15428         }
15429     }
15430
15431   /* If the type-id production did not work out, then we must be
15432      looking at the unary-expression production.  */
15433   if (!expr)
15434     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15435   /* Go back to evaluating expressions.  */
15436   --skip_evaluation;
15437
15438   /* Free the message we created.  */
15439   free ((char *) parser->type_definition_forbidden_message);
15440   /* And restore the old one.  */
15441   parser->type_definition_forbidden_message = saved_message;
15442   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15443
15444   return expr;
15445 }
15446
15447 /* If the current declaration has no declarator, return true.  */
15448
15449 static bool
15450 cp_parser_declares_only_class_p (cp_parser *parser)
15451 {
15452   /* If the next token is a `;' or a `,' then there is no
15453      declarator.  */
15454   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15455           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15456 }
15457
15458 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15459
15460 static void
15461 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15462                              cp_storage_class storage_class)
15463 {
15464   if (decl_specs->storage_class != sc_none)
15465     decl_specs->multiple_storage_classes_p = true;
15466   else
15467     decl_specs->storage_class = storage_class;
15468 }
15469
15470 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15471    is true, the type is a user-defined type; otherwise it is a
15472    built-in type specified by a keyword.  */
15473
15474 static void
15475 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15476                               tree type_spec,
15477                               bool user_defined_p)
15478 {
15479   decl_specs->any_specifiers_p = true;
15480
15481   /* If the user tries to redeclare a built-in type (with, for example,
15482      in "typedef int wchar_t;") we remember that this is what
15483      happened.  In system headers, we ignore these declarations so
15484      that G++ can work with system headers that are not C++-safe.  */
15485   if (decl_specs->specs[(int) ds_typedef]
15486       && !user_defined_p
15487       && (decl_specs->type
15488           || decl_specs->specs[(int) ds_long]
15489           || decl_specs->specs[(int) ds_short]
15490           || decl_specs->specs[(int) ds_unsigned]
15491           || decl_specs->specs[(int) ds_signed]))
15492     {
15493       decl_specs->redefined_builtin_type = type_spec;
15494       if (!decl_specs->type)
15495         {
15496           decl_specs->type = type_spec;
15497           decl_specs->user_defined_type_p = false;
15498         }
15499     }
15500   else if (decl_specs->type)
15501     decl_specs->multiple_types_p = true;
15502   else
15503     {
15504       decl_specs->type = type_spec;
15505       decl_specs->user_defined_type_p = user_defined_p;
15506       decl_specs->redefined_builtin_type = NULL_TREE;
15507     }
15508 }
15509
15510 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15511    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15512
15513 static bool
15514 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15515 {
15516   return decl_specifiers->specs[(int) ds_friend] != 0;
15517 }
15518
15519 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15520    issue an error message indicating that TOKEN_DESC was expected.
15521
15522    Returns the token consumed, if the token had the appropriate type.
15523    Otherwise, returns NULL.  */
15524
15525 static cp_token *
15526 cp_parser_require (cp_parser* parser,
15527                    enum cpp_ttype type,
15528                    const char* token_desc)
15529 {
15530   if (cp_lexer_next_token_is (parser->lexer, type))
15531     return cp_lexer_consume_token (parser->lexer);
15532   else
15533     {
15534       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15535       if (!cp_parser_simulate_error (parser))
15536         {
15537           char *message = concat ("expected ", token_desc, NULL);
15538           cp_parser_error (parser, message);
15539           free (message);
15540         }
15541       return NULL;
15542     }
15543 }
15544
15545 /* Like cp_parser_require, except that tokens will be skipped until
15546    the desired token is found.  An error message is still produced if
15547    the next token is not as expected.  */
15548
15549 static void
15550 cp_parser_skip_until_found (cp_parser* parser,
15551                             enum cpp_ttype type,
15552                             const char* token_desc)
15553 {
15554   cp_token *token;
15555   unsigned nesting_depth = 0;
15556
15557   if (cp_parser_require (parser, type, token_desc))
15558     return;
15559
15560   /* Skip tokens until the desired token is found.  */
15561   while (true)
15562     {
15563       /* Peek at the next token.  */
15564       token = cp_lexer_peek_token (parser->lexer);
15565       /* If we've reached the token we want, consume it and
15566          stop.  */
15567       if (token->type == type && !nesting_depth)
15568         {
15569           cp_lexer_consume_token (parser->lexer);
15570           return;
15571         }
15572       /* If we've run out of tokens, stop.  */
15573       if (token->type == CPP_EOF)
15574         return;
15575       if (token->type == CPP_OPEN_BRACE
15576           || token->type == CPP_OPEN_PAREN
15577           || token->type == CPP_OPEN_SQUARE)
15578         ++nesting_depth;
15579       else if (token->type == CPP_CLOSE_BRACE
15580                || token->type == CPP_CLOSE_PAREN
15581                || token->type == CPP_CLOSE_SQUARE)
15582         {
15583           if (nesting_depth-- == 0)
15584             return;
15585         }
15586       /* Consume this token.  */
15587       cp_lexer_consume_token (parser->lexer);
15588     }
15589 }
15590
15591 /* If the next token is the indicated keyword, consume it.  Otherwise,
15592    issue an error message indicating that TOKEN_DESC was expected.
15593
15594    Returns the token consumed, if the token had the appropriate type.
15595    Otherwise, returns NULL.  */
15596
15597 static cp_token *
15598 cp_parser_require_keyword (cp_parser* parser,
15599                            enum rid keyword,
15600                            const char* token_desc)
15601 {
15602   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15603
15604   if (token && token->keyword != keyword)
15605     {
15606       dyn_string_t error_msg;
15607
15608       /* Format the error message.  */
15609       error_msg = dyn_string_new (0);
15610       dyn_string_append_cstr (error_msg, "expected ");
15611       dyn_string_append_cstr (error_msg, token_desc);
15612       cp_parser_error (parser, error_msg->s);
15613       dyn_string_delete (error_msg);
15614       return NULL;
15615     }
15616
15617   return token;
15618 }
15619
15620 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15621    function-definition.  */
15622
15623 static bool
15624 cp_parser_token_starts_function_definition_p (cp_token* token)
15625 {
15626   return (/* An ordinary function-body begins with an `{'.  */
15627           token->type == CPP_OPEN_BRACE
15628           /* A ctor-initializer begins with a `:'.  */
15629           || token->type == CPP_COLON
15630           /* A function-try-block begins with `try'.  */
15631           || token->keyword == RID_TRY
15632           /* The named return value extension begins with `return'.  */
15633           || token->keyword == RID_RETURN);
15634 }
15635
15636 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15637    definition.  */
15638
15639 static bool
15640 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15641 {
15642   cp_token *token;
15643
15644   token = cp_lexer_peek_token (parser->lexer);
15645   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15646 }
15647
15648 /* Returns TRUE iff the next token is the "," or ">" ending a
15649    template-argument. ">>" is also accepted (after the full
15650    argument was parsed) because it's probably a typo for "> >",
15651    and there is a specific diagnostic for this.  */
15652
15653 static bool
15654 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15655 {
15656   cp_token *token;
15657
15658   token = cp_lexer_peek_token (parser->lexer);
15659   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15660           || token->type == CPP_RSHIFT);
15661 }
15662
15663 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15664    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15665
15666 static bool
15667 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15668                                                      size_t n)
15669 {
15670   cp_token *token;
15671
15672   token = cp_lexer_peek_nth_token (parser->lexer, n);
15673   if (token->type == CPP_LESS)
15674     return true;
15675   /* Check for the sequence `<::' in the original code. It would be lexed as
15676      `[:', where `[' is a digraph, and there is no whitespace before
15677      `:'.  */
15678   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15679     {
15680       cp_token *token2;
15681       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15682       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15683         return true;
15684     }
15685   return false;
15686 }
15687
15688 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15689    or none_type otherwise.  */
15690
15691 static enum tag_types
15692 cp_parser_token_is_class_key (cp_token* token)
15693 {
15694   switch (token->keyword)
15695     {
15696     case RID_CLASS:
15697       return class_type;
15698     case RID_STRUCT:
15699       return record_type;
15700     case RID_UNION:
15701       return union_type;
15702
15703     default:
15704       return none_type;
15705     }
15706 }
15707
15708 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15709
15710 static void
15711 cp_parser_check_class_key (enum tag_types class_key, tree type)
15712 {
15713   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15714     pedwarn ("`%s' tag used in naming `%#T'",
15715             class_key == union_type ? "union"
15716              : class_key == record_type ? "struct" : "class",
15717              type);
15718 }
15719
15720 /* Issue an error message if DECL is redeclared with different
15721    access than its original declaration [class.access.spec/3].
15722    This applies to nested classes and nested class templates.
15723    [class.mem/1].  */
15724
15725 static void cp_parser_check_access_in_redeclaration (tree decl)
15726 {
15727   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15728     return;
15729
15730   if ((TREE_PRIVATE (decl)
15731        != (current_access_specifier == access_private_node))
15732       || (TREE_PROTECTED (decl)
15733           != (current_access_specifier == access_protected_node)))
15734     error ("%D redeclared with different access", decl);
15735 }
15736
15737 /* Look for the `template' keyword, as a syntactic disambiguator.
15738    Return TRUE iff it is present, in which case it will be
15739    consumed.  */
15740
15741 static bool
15742 cp_parser_optional_template_keyword (cp_parser *parser)
15743 {
15744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15745     {
15746       /* The `template' keyword can only be used within templates;
15747          outside templates the parser can always figure out what is a
15748          template and what is not.  */
15749       if (!processing_template_decl)
15750         {
15751           error ("`template' (as a disambiguator) is only allowed "
15752                  "within templates");
15753           /* If this part of the token stream is rescanned, the same
15754              error message would be generated.  So, we purge the token
15755              from the stream.  */
15756           cp_lexer_purge_token (parser->lexer);
15757           return false;
15758         }
15759       else
15760         {
15761           /* Consume the `template' keyword.  */
15762           cp_lexer_consume_token (parser->lexer);
15763           return true;
15764         }
15765     }
15766
15767   return false;
15768 }
15769
15770 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15771    set PARSER->SCOPE, and perform other related actions.  */
15772
15773 static void
15774 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15775 {
15776   tree value;
15777   tree check;
15778
15779   /* Get the stored value.  */
15780   value = cp_lexer_consume_token (parser->lexer)->value;
15781   /* Perform any access checks that were deferred.  */
15782   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15783     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15784   /* Set the scope from the stored value.  */
15785   parser->scope = TREE_VALUE (value);
15786   parser->qualifying_scope = TREE_TYPE (value);
15787   parser->object_scope = NULL_TREE;
15788 }
15789
15790 /* Add tokens to CACHE until a non-nested END token appears.  */
15791
15792 static void
15793 cp_parser_cache_group_1 (cp_parser *parser,
15794                          cp_token_cache *cache,
15795                          enum cpp_ttype end,
15796                          unsigned depth)
15797 {
15798   while (true)
15799     {
15800       cp_token *token;
15801
15802       /* Abort a parenthesized expression if we encounter a brace.  */
15803       if ((end == CPP_CLOSE_PAREN || depth == 0)
15804           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15805         return;
15806       /* If we've reached the end of the file, stop.  */
15807       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15808         return;
15809       /* Consume the next token.  */
15810       token = cp_lexer_consume_token (parser->lexer);
15811       /* Add this token to the tokens we are saving.  */
15812       cp_token_cache_push_token (cache, token);
15813       /* See if it starts a new group.  */
15814       if (token->type == CPP_OPEN_BRACE)
15815         {
15816           cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15817           if (depth == 0)
15818             return;
15819         }
15820       else if (token->type == CPP_OPEN_PAREN)
15821         cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15822       else if (token->type == end)
15823         return;
15824     }
15825 }
15826
15827 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15828    preserve string tokens in both translated and untranslated
15829    forms.  */
15830
15831 static void
15832 cp_parser_cache_group (cp_parser *parser,
15833                          cp_token_cache *cache,
15834                          enum cpp_ttype end,
15835                          unsigned depth)
15836 {
15837   int saved_c_lex_string_translate;
15838
15839   saved_c_lex_string_translate = c_lex_string_translate;
15840   c_lex_string_translate = -1;
15841
15842   cp_parser_cache_group_1 (parser, cache, end, depth);
15843
15844   c_lex_string_translate = saved_c_lex_string_translate;
15845 }
15846
15847
15848 /* Begin parsing tentatively.  We always save tokens while parsing
15849    tentatively so that if the tentative parsing fails we can restore the
15850    tokens.  */
15851
15852 static void
15853 cp_parser_parse_tentatively (cp_parser* parser)
15854 {
15855   /* Enter a new parsing context.  */
15856   parser->context = cp_parser_context_new (parser->context);
15857   /* Begin saving tokens.  */
15858   cp_lexer_save_tokens (parser->lexer);
15859   /* In order to avoid repetitive access control error messages,
15860      access checks are queued up until we are no longer parsing
15861      tentatively.  */
15862   push_deferring_access_checks (dk_deferred);
15863 }
15864
15865 /* Commit to the currently active tentative parse.  */
15866
15867 static void
15868 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15869 {
15870   cp_parser_context *context;
15871   cp_lexer *lexer;
15872
15873   /* Mark all of the levels as committed.  */
15874   lexer = parser->lexer;
15875   for (context = parser->context; context->next; context = context->next)
15876     {
15877       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15878         break;
15879       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15880       while (!cp_lexer_saving_tokens (lexer))
15881         lexer = lexer->next;
15882       cp_lexer_commit_tokens (lexer);
15883     }
15884 }
15885
15886 /* Abort the currently active tentative parse.  All consumed tokens
15887    will be rolled back, and no diagnostics will be issued.  */
15888
15889 static void
15890 cp_parser_abort_tentative_parse (cp_parser* parser)
15891 {
15892   cp_parser_simulate_error (parser);
15893   /* Now, pretend that we want to see if the construct was
15894      successfully parsed.  */
15895   cp_parser_parse_definitely (parser);
15896 }
15897
15898 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15899    token stream.  Otherwise, commit to the tokens we have consumed.
15900    Returns true if no error occurred; false otherwise.  */
15901
15902 static bool
15903 cp_parser_parse_definitely (cp_parser* parser)
15904 {
15905   bool error_occurred;
15906   cp_parser_context *context;
15907
15908   /* Remember whether or not an error occurred, since we are about to
15909      destroy that information.  */
15910   error_occurred = cp_parser_error_occurred (parser);
15911   /* Remove the topmost context from the stack.  */
15912   context = parser->context;
15913   parser->context = context->next;
15914   /* If no parse errors occurred, commit to the tentative parse.  */
15915   if (!error_occurred)
15916     {
15917       /* Commit to the tokens read tentatively, unless that was
15918          already done.  */
15919       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15920         cp_lexer_commit_tokens (parser->lexer);
15921
15922       pop_to_parent_deferring_access_checks ();
15923     }
15924   /* Otherwise, if errors occurred, roll back our state so that things
15925      are just as they were before we began the tentative parse.  */
15926   else
15927     {
15928       cp_lexer_rollback_tokens (parser->lexer);
15929       pop_deferring_access_checks ();
15930     }
15931   /* Add the context to the front of the free list.  */
15932   context->next = cp_parser_context_free_list;
15933   cp_parser_context_free_list = context;
15934
15935   return !error_occurred;
15936 }
15937
15938 /* Returns true if we are parsing tentatively -- but have decided that
15939    we will stick with this tentative parse, even if errors occur.  */
15940
15941 static bool
15942 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15943 {
15944   return (cp_parser_parsing_tentatively (parser)
15945           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15946 }
15947
15948 /* Returns nonzero iff an error has occurred during the most recent
15949    tentative parse.  */
15950
15951 static bool
15952 cp_parser_error_occurred (cp_parser* parser)
15953 {
15954   return (cp_parser_parsing_tentatively (parser)
15955           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15956 }
15957
15958 /* Returns nonzero if GNU extensions are allowed.  */
15959
15960 static bool
15961 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15962 {
15963   return parser->allow_gnu_extensions_p;
15964 }
15965
15966 \f
15967 /* The parser.  */
15968
15969 static GTY (()) cp_parser *the_parser;
15970
15971 /* External interface.  */
15972
15973 /* Parse one entire translation unit.  */
15974
15975 void
15976 c_parse_file (void)
15977 {
15978   bool error_occurred;
15979   static bool already_called = false;
15980
15981   if (already_called)
15982     {
15983       sorry ("inter-module optimizations not implemented for C++");
15984       return;
15985     }
15986   already_called = true;
15987
15988   the_parser = cp_parser_new ();
15989   push_deferring_access_checks (flag_access_control
15990                                 ? dk_no_deferred : dk_no_check);
15991   error_occurred = cp_parser_translation_unit (the_parser);
15992   the_parser = NULL;
15993 }
15994
15995 /* This variable must be provided by every front end.  */
15996
15997 int yydebug;
15998
15999 #include "gt-cp-parser.h"