OSDN Git Service

cp:
[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 #ifdef ENABLE_CHECKING
260 static void cp_lexer_print_token
261   (FILE *, cp_token *);
262 static inline bool cp_lexer_debugging_p
263   (cp_lexer *);
264 static void cp_lexer_start_debugging
265   (cp_lexer *) ATTRIBUTE_UNUSED;
266 static void cp_lexer_stop_debugging
267   (cp_lexer *) ATTRIBUTE_UNUSED;
268 #else
269 #define cp_lexer_debug_stream NULL
270 #define cp_lexer_print_token(str, tok)
271 #define cp_lexer_debugging_p(lexer) 0
272 #endif /* ENABLE_CHECKING */
273
274 /* Manifest constants.  */
275
276 #define CP_TOKEN_BUFFER_SIZE 5
277 #define CP_SAVED_TOKENS_SIZE 5
278
279 /* A token type for keywords, as opposed to ordinary identifiers.  */
280 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
281
282 /* A token type for template-ids.  If a template-id is processed while
283    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
284    the value of the CPP_TEMPLATE_ID is whatever was returned by
285    cp_parser_template_id.  */
286 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
287
288 /* A token type for nested-name-specifiers.  If a
289    nested-name-specifier is processed while parsing tentatively, it is
290    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
291    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
292    cp_parser_nested_name_specifier_opt.  */
293 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
294
295 /* A token type for tokens that are not tokens at all; these are used
296    to mark the end of a token block.  */
297 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
298
299 /* Variables.  */
300
301 #ifdef ENABLE_CHECKING
302 /* The stream to which debugging output should be written.  */
303 static FILE *cp_lexer_debug_stream;
304 #endif /* ENABLE_CHECKING */
305
306 /* Create a new main C++ lexer, the lexer that gets tokens from the
307    preprocessor.  */
308
309 static cp_lexer *
310 cp_lexer_new_main (void)
311 {
312   cp_lexer *lexer;
313   cp_token first_token;
314
315   /* It's possible that lexing the first token will load a PCH file,
316      which is a GC collection point.  So we have to grab the first
317      token before allocating any memory.  */
318   cp_lexer_get_preprocessor_token (NULL, &first_token);
319   c_common_no_more_pch ();
320
321   /* Allocate the memory.  */
322   lexer = GGC_CNEW (cp_lexer);
323
324   /* Create the circular buffer.  */
325   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
326   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
327
328   /* There is one token in the buffer.  */
329   lexer->last_token = lexer->buffer + 1;
330   lexer->first_token = lexer->buffer;
331   lexer->next_token = lexer->buffer;
332   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
333
334   /* This lexer obtains more tokens by calling c_lex.  */
335   lexer->main_lexer_p = true;
336
337   /* Create the SAVED_TOKENS stack.  */
338   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
339
340   /* Create the STRINGS array.  */
341   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
342
343 #ifdef ENABLE_CHECKING  
344   /* Assume we are not debugging.  */
345   lexer->debugging_p = false;
346 #endif /* ENABLE_CHECKING */
347
348   return lexer;
349 }
350
351 /* Create a new lexer whose token stream is primed with the TOKENS.
352    When these tokens are exhausted, no new tokens will be read.  */
353
354 static cp_lexer *
355 cp_lexer_new_from_tokens (cp_token_cache *tokens)
356 {
357   cp_lexer *lexer;
358   cp_token *token;
359   cp_token_block *block;
360   ptrdiff_t num_tokens;
361
362   /* Allocate the memory.  */
363   lexer = GGC_CNEW (cp_lexer);
364
365   /* Create a new buffer, appropriately sized.  */
366   num_tokens = 0;
367   for (block = tokens->first; block != NULL; block = block->next)
368     num_tokens += block->num_tokens;
369   lexer->buffer = GGC_NEWVEC (cp_token, num_tokens);
370   lexer->buffer_end = lexer->buffer + num_tokens;
371
372   /* Install the tokens.  */
373   token = lexer->buffer;
374   for (block = tokens->first; block != NULL; block = block->next)
375     {
376       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
377       token += block->num_tokens;
378     }
379
380   /* The FIRST_TOKEN is the beginning of the buffer.  */
381   lexer->first_token = lexer->buffer;
382   /* The next available token is also at the beginning of the buffer.  */
383   lexer->next_token = lexer->buffer;
384   /* The buffer is full.  */
385   lexer->last_token = lexer->first_token;
386
387   /* This lexer doesn't obtain more tokens.  */
388   lexer->main_lexer_p = false;
389
390   /* Create the SAVED_TOKENS stack.  */
391   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
392
393   /* Create the STRINGS array.  */
394   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
395
396 #ifdef ENABLE_CHECKING
397   /* Assume we are not debugging.  */
398   lexer->debugging_p = false;
399 #endif /* ENABLE_CHECKING */
400
401   return lexer;
402 }
403
404 /* Returns nonzero if debugging information should be output.  */
405
406 #ifdef ENABLE_CHECKING
407
408 static inline bool
409 cp_lexer_debugging_p (cp_lexer *lexer)
410 {
411   return lexer->debugging_p;
412 }
413
414 #endif /* ENABLE_CHECKING */
415
416 /* Set the current source position from the information stored in
417    TOKEN.  */
418
419 static inline void
420 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
421                                          const cp_token *token)
422 {
423   /* Ideally, the source position information would not be a global
424      variable, but it is.  */
425
426   /* Update the line number.  */
427   if (token->type != CPP_EOF)
428     input_location = token->location;
429 }
430
431 /* TOKEN points into the circular token buffer.  Return a pointer to
432    the next token in the buffer.  */
433
434 static inline cp_token *
435 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
436 {
437   token++;
438   if (token == lexer->buffer_end)
439     token = lexer->buffer;
440   return token;
441 }
442
443 /* TOKEN points into the circular token buffer.  Return a pointer to
444    the previous token in the buffer.  */
445
446 static inline cp_token *
447 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
448 {
449   if (token == lexer->buffer)
450     token = lexer->buffer_end;
451   return token - 1;
452 }
453
454 /* nonzero if we are presently saving tokens.  */
455
456 static int
457 cp_lexer_saving_tokens (const cp_lexer* lexer)
458 {
459   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
460 }
461
462 /* Return a pointer to the token that is N tokens beyond TOKEN in the
463    buffer.  */
464
465 static cp_token *
466 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
467 {
468   token += n;
469   if (token >= lexer->buffer_end)
470     token = lexer->buffer + (token - lexer->buffer_end);
471   return token;
472 }
473
474 /* Returns the number of times that START would have to be incremented
475    to reach FINISH.  If START and FINISH are the same, returns zero.  */
476
477 static ptrdiff_t
478 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
479 {
480   if (finish >= start)
481     return finish - start;
482   else
483     return ((lexer->buffer_end - lexer->buffer)
484             - (start - finish));
485 }
486
487 /* Obtain another token from the C preprocessor and add it to the
488    token buffer.  Returns the newly read token.  */
489
490 static cp_token *
491 cp_lexer_read_token (cp_lexer* lexer)
492 {
493   cp_token *token;
494
495   /* Make sure there is room in the buffer.  */
496   cp_lexer_maybe_grow_buffer (lexer);
497
498   /* If there weren't any tokens, then this one will be the first.  */
499   if (!lexer->first_token)
500     lexer->first_token = lexer->last_token;
501   /* Similarly, if there were no available tokens, there is one now.  */
502   if (!lexer->next_token)
503     lexer->next_token = lexer->last_token;
504
505   /* Figure out where we're going to store the new token.  */
506   token = lexer->last_token;
507
508   /* Get a new token from the preprocessor.  */
509   cp_lexer_get_preprocessor_token (lexer, token);
510
511   /* Increment LAST_TOKEN.  */
512   lexer->last_token = cp_lexer_next_token (lexer, token);
513
514   /* Strings should have type `const char []'.  Right now, we will
515      have an ARRAY_TYPE that is constant rather than an array of
516      constant elements.
517      FIXME: Make fix_string_type get this right in the first place.  */
518   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
519       && flag_const_strings)
520     {
521       if (c_lex_string_translate)
522         {
523           tree value = token->value;
524           tree type;
525
526           /* We might as well go ahead and release the chained
527              translated string such that we can reuse its memory.  */
528           if (TREE_CHAIN (value))
529             value = TREE_CHAIN (token->value);
530
531           /* Get the current type.  It will be an ARRAY_TYPE.  */
532           type = TREE_TYPE (value);
533           /* Use build_cplus_array_type to rebuild the array, thereby
534              getting the right type.  */
535           type = build_cplus_array_type (TREE_TYPE (type),
536                                          TYPE_DOMAIN (type));
537           /* Reset the type of the token.  */
538           TREE_TYPE (value) = type;
539         }
540     }
541
542   return token;
543 }
544
545 /* If the circular buffer is full, make it bigger.  */
546
547 static void
548 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
549 {
550   /* If the buffer is full, enlarge it.  */
551   if (lexer->last_token == lexer->first_token)
552     {
553       cp_token *new_buffer;
554       cp_token *old_buffer;
555       cp_token *new_first_token;
556       ptrdiff_t buffer_length;
557       size_t num_tokens_to_copy;
558
559       /* Remember the current buffer pointer.  It will become invalid,
560          but we will need to do pointer arithmetic involving this
561          value.  */
562       old_buffer = lexer->buffer;
563       /* Compute the current buffer size.  */
564       buffer_length = lexer->buffer_end - lexer->buffer;
565       /* Allocate a buffer twice as big.  */
566       new_buffer = ggc_realloc (lexer->buffer,
567                                 2 * buffer_length * sizeof (cp_token));
568
569       /* Because the buffer is circular, logically consecutive tokens
570          are not necessarily placed consecutively in memory.
571          Therefore, we must keep move the tokens that were before
572          FIRST_TOKEN to the second half of the newly allocated
573          buffer.  */
574       num_tokens_to_copy = (lexer->first_token - old_buffer);
575       memcpy (new_buffer + buffer_length,
576               new_buffer,
577               num_tokens_to_copy * sizeof (cp_token));
578       /* Clear the rest of the buffer.  We never look at this storage,
579          but the garbage collector may.  */
580       memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
581               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
582
583       /* Now recompute all of the buffer pointers.  */
584       new_first_token
585         = new_buffer + (lexer->first_token - old_buffer);
586       if (lexer->next_token != NULL)
587         {
588           ptrdiff_t next_token_delta;
589
590           if (lexer->next_token > lexer->first_token)
591             next_token_delta = lexer->next_token - lexer->first_token;
592           else
593             next_token_delta =
594               buffer_length - (lexer->first_token - lexer->next_token);
595           lexer->next_token = new_first_token + next_token_delta;
596         }
597       lexer->last_token = new_first_token + buffer_length;
598       lexer->buffer = new_buffer;
599       lexer->buffer_end = new_buffer + buffer_length * 2;
600       lexer->first_token = new_first_token;
601     }
602 }
603
604 /* Store the next token from the preprocessor in *TOKEN.  */
605
606 static void
607 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
608                                  cp_token *token)
609 {
610   bool done;
611
612   /* If this not the main lexer, return a terminating CPP_EOF token.  */
613   if (lexer != NULL && !lexer->main_lexer_p)
614     {
615       token->type = CPP_EOF;
616       token->location = UNKNOWN_LOCATION;
617       token->value = NULL_TREE;
618       token->keyword = RID_MAX;
619
620       return;
621     }
622
623   done = false;
624   /* Keep going until we get a token we like.  */
625   while (!done)
626     {
627       /* Get a new token from the preprocessor.  */
628       token->type = c_lex_with_flags (&token->value, &token->flags);
629       /* Issue messages about tokens we cannot process.  */
630       switch (token->type)
631         {
632         case CPP_ATSIGN:
633         case CPP_HASH:
634         case CPP_PASTE:
635           error ("invalid token");
636           break;
637
638         default:
639           /* This is a good token, so we exit the loop.  */
640           done = true;
641           break;
642         }
643     }
644   /* Now we've got our token.  */
645   token->location = input_location;
646
647   /* Check to see if this token is a keyword.  */
648   if (token->type == CPP_NAME
649       && C_IS_RESERVED_WORD (token->value))
650     {
651       /* Mark this token as a keyword.  */
652       token->type = CPP_KEYWORD;
653       /* Record which keyword.  */
654       token->keyword = C_RID_CODE (token->value);
655       /* Update the value.  Some keywords are mapped to particular
656          entities, rather than simply having the value of the
657          corresponding IDENTIFIER_NODE.  For example, `__const' is
658          mapped to `const'.  */
659       token->value = ridpointers[token->keyword];
660     }
661   else
662     token->keyword = RID_MAX;
663 }
664
665 /* Return a pointer to the next token in the token stream, but do not
666    consume it.  */
667
668 static cp_token *
669 cp_lexer_peek_token (cp_lexer* lexer)
670 {
671   cp_token *token;
672
673   /* If there are no tokens, read one now.  */
674   if (!lexer->next_token)
675     cp_lexer_read_token (lexer);
676
677   /* Provide debugging output.  */
678   if (cp_lexer_debugging_p (lexer))
679     {
680       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
681       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
682       fprintf (cp_lexer_debug_stream, "\n");
683     }
684
685   token = lexer->next_token;
686   cp_lexer_set_source_position_from_token (lexer, token);
687   return token;
688 }
689
690 /* Return true if the next token has the indicated TYPE.  */
691
692 static bool
693 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
694 {
695   cp_token *token;
696
697   /* Peek at the next token.  */
698   token = cp_lexer_peek_token (lexer);
699   /* Check to see if it has the indicated TYPE.  */
700   return token->type == type;
701 }
702
703 /* Return true if the next token does not have the indicated TYPE.  */
704
705 static bool
706 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
707 {
708   return !cp_lexer_next_token_is (lexer, type);
709 }
710
711 /* Return true if the next token is the indicated KEYWORD.  */
712
713 static bool
714 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
715 {
716   cp_token *token;
717
718   /* Peek at the next token.  */
719   token = cp_lexer_peek_token (lexer);
720   /* Check to see if it is the indicated keyword.  */
721   return token->keyword == keyword;
722 }
723
724 /* Return a pointer to the Nth token in the token stream.  If N is 1,
725    then this is precisely equivalent to cp_lexer_peek_token.  */
726
727 static cp_token *
728 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
729 {
730   cp_token *token;
731
732   /* N is 1-based, not zero-based.  */
733   gcc_assert (n > 0);
734
735   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
736   token = lexer->next_token;
737   /* If there are no tokens in the buffer, get one now.  */
738   if (!token)
739     {
740       cp_lexer_read_token (lexer);
741       token = lexer->next_token;
742     }
743
744   /* Now, read tokens until we have enough.  */
745   while (--n > 0)
746     {
747       /* Advance to the next token.  */
748       token = cp_lexer_next_token (lexer, token);
749       /* If that's all the tokens we have, read a new one.  */
750       if (token == lexer->last_token)
751         token = cp_lexer_read_token (lexer);
752     }
753
754   return token;
755 }
756
757 /* Consume the next token.  The pointer returned is valid only until
758    another token is read.  Callers should preserve copy the token
759    explicitly if they will need its value for a longer period of
760    time.  */
761
762 static cp_token *
763 cp_lexer_consume_token (cp_lexer* lexer)
764 {
765   cp_token *token;
766
767   /* If there are no tokens, read one now.  */
768   if (!lexer->next_token)
769     cp_lexer_read_token (lexer);
770
771   /* Remember the token we'll be returning.  */
772   token = lexer->next_token;
773
774   /* Increment NEXT_TOKEN.  */
775   lexer->next_token = cp_lexer_next_token (lexer,
776                                            lexer->next_token);
777   /* Check to see if we're all out of tokens.  */
778   if (lexer->next_token == lexer->last_token)
779     lexer->next_token = NULL;
780
781   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
782   if (!cp_lexer_saving_tokens (lexer))
783     {
784       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
785       if (!lexer->next_token)
786         lexer->first_token = NULL;
787       else
788         lexer->first_token = lexer->next_token;
789     }
790
791   /* Provide debugging output.  */
792   if (cp_lexer_debugging_p (lexer))
793     {
794       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
795       cp_lexer_print_token (cp_lexer_debug_stream, token);
796       fprintf (cp_lexer_debug_stream, "\n");
797     }
798
799   return token;
800 }
801
802 /* Permanently remove the next token from the token stream.  There
803    must be a valid next token already; this token never reads
804    additional tokens from the preprocessor.  */
805
806 static void
807 cp_lexer_purge_token (cp_lexer *lexer)
808 {
809   cp_token *token;
810   cp_token *next_token;
811
812   token = lexer->next_token;
813   while (true)
814     {
815       next_token = cp_lexer_next_token (lexer, token);
816       if (next_token == lexer->last_token)
817         break;
818       *token = *next_token;
819       token = next_token;
820     }
821
822   lexer->last_token = token;
823   /* The token purged may have been the only token remaining; if so,
824      clear NEXT_TOKEN.  */
825   if (lexer->next_token == token)
826     lexer->next_token = NULL;
827 }
828
829 /* Permanently remove all tokens after TOKEN, up to, but not
830    including, the token that will be returned next by
831    cp_lexer_peek_token.  */
832
833 static void
834 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
835 {
836   cp_token *peek;
837   cp_token *t1;
838   cp_token *t2;
839
840   if (lexer->next_token)
841     {
842       /* Copy the tokens that have not yet been read to the location
843          immediately following TOKEN.  */
844       t1 = cp_lexer_next_token (lexer, token);
845       t2 = peek = cp_lexer_peek_token (lexer);
846       /* Move tokens into the vacant area between TOKEN and PEEK.  */
847       while (t2 != lexer->last_token)
848         {
849           *t1 = *t2;
850           t1 = cp_lexer_next_token (lexer, t1);
851           t2 = cp_lexer_next_token (lexer, t2);
852         }
853       /* Now, the next available token is right after TOKEN.  */
854       lexer->next_token = cp_lexer_next_token (lexer, token);
855       /* And the last token is wherever we ended up.  */
856       lexer->last_token = t1;
857     }
858   else
859     {
860       /* There are no tokens in the buffer, so there is nothing to
861          copy.  The last token in the buffer is TOKEN itself.  */
862       lexer->last_token = cp_lexer_next_token (lexer, token);
863     }
864 }
865
866 /* Begin saving tokens.  All tokens consumed after this point will be
867    preserved.  */
868
869 static void
870 cp_lexer_save_tokens (cp_lexer* lexer)
871 {
872   /* Provide debugging output.  */
873   if (cp_lexer_debugging_p (lexer))
874     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
875
876   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
877      restore the tokens if required.  */
878   if (!lexer->next_token)
879     cp_lexer_read_token (lexer);
880
881   VARRAY_PUSH_INT (lexer->saved_tokens,
882                    cp_lexer_token_difference (lexer,
883                                               lexer->first_token,
884                                               lexer->next_token));
885 }
886
887 /* Commit to the portion of the token stream most recently saved.  */
888
889 static void
890 cp_lexer_commit_tokens (cp_lexer* lexer)
891 {
892   /* Provide debugging output.  */
893   if (cp_lexer_debugging_p (lexer))
894     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
895
896   VARRAY_POP (lexer->saved_tokens);
897 }
898
899 /* Return all tokens saved since the last call to cp_lexer_save_tokens
900    to the token stream.  Stop saving tokens.  */
901
902 static void
903 cp_lexer_rollback_tokens (cp_lexer* lexer)
904 {
905   size_t delta;
906
907   /* Provide debugging output.  */
908   if (cp_lexer_debugging_p (lexer))
909     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
910
911   /* Find the token that was the NEXT_TOKEN when we started saving
912      tokens.  */
913   delta = VARRAY_TOP_INT(lexer->saved_tokens);
914   /* Make it the next token again now.  */
915   lexer->next_token = cp_lexer_advance_token (lexer,
916                                               lexer->first_token,
917                                               delta);
918   /* It might be the case that there were no tokens when we started
919      saving tokens, but that there are some tokens now.  */
920   if (!lexer->next_token && lexer->first_token)
921     lexer->next_token = lexer->first_token;
922
923   /* Stop saving tokens.  */
924   VARRAY_POP (lexer->saved_tokens);
925 }
926
927 /* Print a representation of the TOKEN on the STREAM.  */
928
929 #ifdef ENABLE_CHECKING
930
931 static void
932 cp_lexer_print_token (FILE * stream, cp_token* token)
933 {
934   const char *token_type = NULL;
935
936   /* Figure out what kind of token this is.  */
937   switch (token->type)
938     {
939     case CPP_EQ:
940       token_type = "EQ";
941       break;
942
943     case CPP_COMMA:
944       token_type = "COMMA";
945       break;
946
947     case CPP_OPEN_PAREN:
948       token_type = "OPEN_PAREN";
949       break;
950
951     case CPP_CLOSE_PAREN:
952       token_type = "CLOSE_PAREN";
953       break;
954
955     case CPP_OPEN_BRACE:
956       token_type = "OPEN_BRACE";
957       break;
958
959     case CPP_CLOSE_BRACE:
960       token_type = "CLOSE_BRACE";
961       break;
962
963     case CPP_SEMICOLON:
964       token_type = "SEMICOLON";
965       break;
966
967     case CPP_NAME:
968       token_type = "NAME";
969       break;
970
971     case CPP_EOF:
972       token_type = "EOF";
973       break;
974
975     case CPP_KEYWORD:
976       token_type = "keyword";
977       break;
978
979       /* This is not a token that we know how to handle yet.  */
980     default:
981       break;
982     }
983
984   /* If we have a name for the token, print it out.  Otherwise, we
985      simply give the numeric code.  */
986   if (token_type)
987     fprintf (stream, "%s", token_type);
988   else
989     fprintf (stream, "%d", token->type);
990   /* And, for an identifier, print the identifier name.  */
991   if (token->type == CPP_NAME
992       /* Some keywords have a value that is not an IDENTIFIER_NODE.
993          For example, `struct' is mapped to an INTEGER_CST.  */
994       || (token->type == CPP_KEYWORD
995           && TREE_CODE (token->value) == IDENTIFIER_NODE))
996     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
997 }
998
999 /* Start emitting debugging information.  */
1000
1001 static void
1002 cp_lexer_start_debugging (cp_lexer* lexer)
1003 {
1004   ++lexer->debugging_p;
1005 }
1006
1007 /* Stop emitting debugging information.  */
1008
1009 static void
1010 cp_lexer_stop_debugging (cp_lexer* lexer)
1011 {
1012   --lexer->debugging_p;
1013 }
1014
1015 #endif /* ENABLE_CHECKING */
1016
1017 \f
1018 /* Decl-specifiers.  */
1019
1020 static void clear_decl_specs
1021   (cp_decl_specifier_seq *);
1022
1023 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1024
1025 static void
1026 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1027 {
1028   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1029 }
1030
1031 /* Declarators.  */
1032
1033 /* Nothing other than the parser should be creating declarators;
1034    declarators are a semi-syntactic representation of C++ entities.
1035    Other parts of the front end that need to create entities (like
1036    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1037
1038 static cp_declarator *make_id_declarator
1039   (tree);
1040 static cp_declarator *make_call_declarator
1041   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
1042 static cp_declarator *make_array_declarator
1043   (cp_declarator *, tree);
1044 static cp_declarator *make_pointer_declarator
1045   (cp_cv_quals, cp_declarator *);
1046 static cp_declarator *make_reference_declarator
1047   (cp_cv_quals, cp_declarator *);
1048 static cp_parameter_declarator *make_parameter_declarator
1049   (cp_decl_specifier_seq *, cp_declarator *, tree);
1050 static cp_declarator *make_ptrmem_declarator
1051   (cp_cv_quals, tree, cp_declarator *);
1052
1053 cp_declarator *cp_error_declarator;
1054
1055 /* The obstack on which declarators and related data structures are
1056    allocated.  */
1057 static struct obstack declarator_obstack;
1058
1059 /* Alloc BYTES from the declarator memory pool.  */
1060
1061 static inline void *
1062 alloc_declarator (size_t bytes)
1063 {
1064   return obstack_alloc (&declarator_obstack, bytes);
1065 }
1066
1067 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1068    common to all declarators.  */
1069
1070 static cp_declarator *
1071 make_declarator (cp_declarator_kind kind)
1072 {
1073   cp_declarator *declarator;
1074
1075   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1076   declarator->kind = kind;
1077   declarator->attributes = NULL_TREE;
1078   declarator->declarator = NULL;
1079
1080   return declarator;
1081 }
1082
1083 /* Make a declarator for a generalized identifier.  */
1084
1085 cp_declarator *
1086 make_id_declarator (tree id)
1087 {
1088   cp_declarator *declarator;
1089
1090   declarator = make_declarator (cdk_id);
1091   declarator->u.id.name = id;
1092   declarator->u.id.sfk = sfk_none;
1093
1094   return declarator;
1095 }
1096
1097 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1098    of modifiers such as const or volatile to apply to the pointer
1099    type, represented as identifiers.  */
1100
1101 cp_declarator *
1102 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1103 {
1104   cp_declarator *declarator;
1105
1106   declarator = make_declarator (cdk_pointer);
1107   declarator->declarator = target;
1108   declarator->u.pointer.qualifiers = cv_qualifiers;
1109   declarator->u.pointer.class_type = NULL_TREE;
1110
1111   return declarator;
1112 }
1113
1114 /* Like make_pointer_declarator -- but for references.  */
1115
1116 cp_declarator *
1117 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1118 {
1119   cp_declarator *declarator;
1120
1121   declarator = make_declarator (cdk_reference);
1122   declarator->declarator = target;
1123   declarator->u.pointer.qualifiers = cv_qualifiers;
1124   declarator->u.pointer.class_type = NULL_TREE;
1125
1126   return declarator;
1127 }
1128
1129 /* Like make_pointer_declarator -- but for a pointer to a non-static
1130    member of CLASS_TYPE.  */
1131
1132 cp_declarator *
1133 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1134                         cp_declarator *pointee)
1135 {
1136   cp_declarator *declarator;
1137
1138   declarator = make_declarator (cdk_ptrmem);
1139   declarator->declarator = pointee;
1140   declarator->u.pointer.qualifiers = cv_qualifiers;
1141   declarator->u.pointer.class_type = class_type;
1142
1143   return declarator;
1144 }
1145
1146 /* Make a declarator for the function given by TARGET, with the
1147    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1148    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1149    indicates what exceptions can be thrown.  */
1150
1151 cp_declarator *
1152 make_call_declarator (cp_declarator *target,
1153                       cp_parameter_declarator *parms,
1154                       cp_cv_quals cv_qualifiers,
1155                       tree exception_specification)
1156 {
1157   cp_declarator *declarator;
1158
1159   declarator = make_declarator (cdk_function);
1160   declarator->declarator = target;
1161   declarator->u.function.parameters = parms;
1162   declarator->u.function.qualifiers = cv_qualifiers;
1163   declarator->u.function.exception_specification = exception_specification;
1164
1165   return declarator;
1166 }
1167
1168 /* Make a declarator for an array of BOUNDS elements, each of which is
1169    defined by ELEMENT.  */
1170
1171 cp_declarator *
1172 make_array_declarator (cp_declarator *element, tree bounds)
1173 {
1174   cp_declarator *declarator;
1175
1176   declarator = make_declarator (cdk_array);
1177   declarator->declarator = element;
1178   declarator->u.array.bounds = bounds;
1179
1180   return declarator;
1181 }
1182
1183 cp_parameter_declarator *no_parameters;
1184
1185 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1186    DECLARATOR and DEFAULT_ARGUMENT.  */
1187
1188 cp_parameter_declarator *
1189 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1190                            cp_declarator *declarator,
1191                            tree default_argument)
1192 {
1193   cp_parameter_declarator *parameter;
1194
1195   parameter = ((cp_parameter_declarator *)
1196                alloc_declarator (sizeof (cp_parameter_declarator)));
1197   parameter->next = NULL;
1198   if (decl_specifiers)
1199     parameter->decl_specifiers = *decl_specifiers;
1200   else
1201     clear_decl_specs (&parameter->decl_specifiers);
1202   parameter->declarator = declarator;
1203   parameter->default_argument = default_argument;
1204   parameter->ellipsis_p = false;
1205
1206   return parameter;
1207 }
1208
1209 /* The parser.  */
1210
1211 /* Overview
1212    --------
1213
1214    A cp_parser parses the token stream as specified by the C++
1215    grammar.  Its job is purely parsing, not semantic analysis.  For
1216    example, the parser breaks the token stream into declarators,
1217    expressions, statements, and other similar syntactic constructs.
1218    It does not check that the types of the expressions on either side
1219    of an assignment-statement are compatible, or that a function is
1220    not declared with a parameter of type `void'.
1221
1222    The parser invokes routines elsewhere in the compiler to perform
1223    semantic analysis and to build up the abstract syntax tree for the
1224    code processed.
1225
1226    The parser (and the template instantiation code, which is, in a
1227    way, a close relative of parsing) are the only parts of the
1228    compiler that should be calling push_scope and pop_scope, or
1229    related functions.  The parser (and template instantiation code)
1230    keeps track of what scope is presently active; everything else
1231    should simply honor that.  (The code that generates static
1232    initializers may also need to set the scope, in order to check
1233    access control correctly when emitting the initializers.)
1234
1235    Methodology
1236    -----------
1237
1238    The parser is of the standard recursive-descent variety.  Upcoming
1239    tokens in the token stream are examined in order to determine which
1240    production to use when parsing a non-terminal.  Some C++ constructs
1241    require arbitrary look ahead to disambiguate.  For example, it is
1242    impossible, in the general case, to tell whether a statement is an
1243    expression or declaration without scanning the entire statement.
1244    Therefore, the parser is capable of "parsing tentatively."  When the
1245    parser is not sure what construct comes next, it enters this mode.
1246    Then, while we attempt to parse the construct, the parser queues up
1247    error messages, rather than issuing them immediately, and saves the
1248    tokens it consumes.  If the construct is parsed successfully, the
1249    parser "commits", i.e., it issues any queued error messages and
1250    the tokens that were being preserved are permanently discarded.
1251    If, however, the construct is not parsed successfully, the parser
1252    rolls back its state completely so that it can resume parsing using
1253    a different alternative.
1254
1255    Future Improvements
1256    -------------------
1257
1258    The performance of the parser could probably be improved
1259    substantially.  Some possible improvements include:
1260
1261      - The expression parser recurses through the various levels of
1262        precedence as specified in the grammar, rather than using an
1263        operator-precedence technique.  Therefore, parsing a simple
1264        identifier requires multiple recursive calls.
1265
1266      - We could often eliminate the need to parse tentatively by
1267        looking ahead a little bit.  In some places, this approach
1268        might not entirely eliminate the need to parse tentatively, but
1269        it might still speed up the average case.  */
1270
1271 /* Flags that are passed to some parsing functions.  These values can
1272    be bitwise-ored together.  */
1273
1274 typedef enum cp_parser_flags
1275 {
1276   /* No flags.  */
1277   CP_PARSER_FLAGS_NONE = 0x0,
1278   /* The construct is optional.  If it is not present, then no error
1279      should be issued.  */
1280   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1281   /* When parsing a type-specifier, do not allow user-defined types.  */
1282   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1283 } cp_parser_flags;
1284
1285 /* The different kinds of declarators we want to parse.  */
1286
1287 typedef enum cp_parser_declarator_kind
1288 {
1289   /* We want an abstract declarator.  */
1290   CP_PARSER_DECLARATOR_ABSTRACT,
1291   /* We want a named declarator.  */
1292   CP_PARSER_DECLARATOR_NAMED,
1293   /* We don't mind, but the name must be an unqualified-id.  */
1294   CP_PARSER_DECLARATOR_EITHER
1295 } cp_parser_declarator_kind;
1296
1297 /* A mapping from a token type to a corresponding tree node type.  */
1298
1299 typedef struct cp_parser_token_tree_map_node
1300 {
1301   /* The token type.  */
1302   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1303   /* The corresponding tree code.  */
1304   ENUM_BITFIELD (tree_code) tree_type : 8;
1305 } cp_parser_token_tree_map_node;
1306
1307 /* A complete map consists of several ordinary entries, followed by a
1308    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1309
1310 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1311
1312 /* The status of a tentative parse.  */
1313
1314 typedef enum cp_parser_status_kind
1315 {
1316   /* No errors have occurred.  */
1317   CP_PARSER_STATUS_KIND_NO_ERROR,
1318   /* An error has occurred.  */
1319   CP_PARSER_STATUS_KIND_ERROR,
1320   /* We are committed to this tentative parse, whether or not an error
1321      has occurred.  */
1322   CP_PARSER_STATUS_KIND_COMMITTED
1323 } cp_parser_status_kind;
1324
1325 /* Context that is saved and restored when parsing tentatively.  */
1326
1327 typedef struct cp_parser_context GTY (())
1328 {
1329   /* If this is a tentative parsing context, the status of the
1330      tentative parse.  */
1331   enum cp_parser_status_kind status;
1332   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1333      that are looked up in this context must be looked up both in the
1334      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1335      the context of the containing expression.  */
1336   tree object_type;
1337   /* The next parsing context in the stack.  */
1338   struct cp_parser_context *next;
1339 } cp_parser_context;
1340
1341 /* Prototypes.  */
1342
1343 /* Constructors and destructors.  */
1344
1345 static cp_parser_context *cp_parser_context_new
1346   (cp_parser_context *);
1347
1348 /* Class variables.  */
1349
1350 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372   /* No errors have occurred yet in this context.  */
1373   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1374   /* If this is not the bottomost context, copy information that we
1375      need from the previous context.  */
1376   if (next)
1377     {
1378       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1379          expression, then we are parsing one in this context, too.  */
1380       context->object_type = next->object_type;
1381       /* Thread the stack.  */
1382       context->next = next;
1383     }
1384
1385   return context;
1386 }
1387
1388 /* The cp_parser structure represents the C++ parser.  */
1389
1390 typedef struct cp_parser GTY(())
1391 {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.
1399
1400      This value is not cleared automatically after a name is looked
1401      up, so we must be careful to clear it before starting a new look
1402      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1403      will look up `Z' in the scope of `X', rather than the current
1404      scope.)  Unfortunately, it is difficult to tell when name lookup
1405      is complete, because we sometimes peek at a token, look it up,
1406      and then decide not to consume it.  */
1407   tree scope;
1408
1409   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410      last lookup took place.  OBJECT_SCOPE is used if an expression
1411      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412      respectively.  QUALIFYING_SCOPE is used for an expression of the
1413      form "X::Y"; it refers to X.  */
1414   tree object_scope;
1415   tree qualifying_scope;
1416
1417   /* A stack of parsing contexts.  All but the bottom entry on the
1418      stack will be tentative contexts.
1419
1420      We parse tentatively in order to determine which construct is in
1421      use in some situations.  For example, in order to determine
1422      whether a statement is an expression-statement or a
1423      declaration-statement we parse it tentatively as a
1424      declaration-statement.  If that fails, we then reparse the same
1425      token stream as an expression-statement.  */
1426   cp_parser_context *context;
1427
1428   /* True if we are parsing GNU C++.  If this flag is not set, then
1429      GNU extensions are not recognized.  */
1430   bool allow_gnu_extensions_p;
1431
1432   /* TRUE if the `>' token should be interpreted as the greater-than
1433      operator.  FALSE if it is the end of a template-id or
1434      template-parameter-list.  */
1435   bool greater_than_is_operator_p;
1436
1437   /* TRUE if default arguments are allowed within a parameter list
1438      that starts at this point. FALSE if only a gnu extension makes
1439      them permissible.  */
1440   bool default_arg_ok_p;
1441
1442   /* TRUE if we are parsing an integral constant-expression.  See
1443      [expr.const] for a precise definition.  */
1444   bool integral_constant_expression_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression -- but a
1447      non-constant expression should be permitted as well.  This flag
1448      is used when parsing an array bound so that GNU variable-length
1449      arrays are tolerated.  */
1450   bool allow_non_integral_constant_expression_p;
1451
1452   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1453      been seen that makes the expression non-constant.  */
1454   bool non_integral_constant_expression_p;
1455
1456   /* TRUE if local variable names and `this' are forbidden in the
1457      current context.  */
1458   bool local_variables_forbidden_p;
1459
1460   /* TRUE if the declaration we are parsing is part of a
1461      linkage-specification of the form `extern string-literal
1462      declaration'.  */
1463   bool in_unbraced_linkage_specification_p;
1464
1465   /* TRUE if we are presently parsing a declarator, after the
1466      direct-declarator.  */
1467   bool in_declarator_p;
1468
1469   /* TRUE if we are presently parsing a template-argument-list.  */
1470   bool in_template_argument_list_p;
1471
1472   /* TRUE if we are presently parsing the body of an
1473      iteration-statement.  */
1474   bool in_iteration_statement_p;
1475
1476   /* TRUE if we are presently parsing the body of a switch
1477      statement.  */
1478   bool in_switch_statement_p;
1479
1480   /* TRUE if we are parsing a type-id in an expression context.  In
1481      such a situation, both "type (expr)" and "type (type)" are valid
1482      alternatives.  */
1483   bool in_type_id_in_expr_p;
1484
1485   /* If non-NULL, then we are parsing a construct where new type
1486      definitions are not permitted.  The string stored here will be
1487      issued as an error message if a type is defined.  */
1488   const char *type_definition_forbidden_message;
1489
1490   /* A list of lists. The outer list is a stack, used for member
1491      functions of local classes. At each level there are two sub-list,
1492      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1493      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1494      TREE_VALUE's. The functions are chained in reverse declaration
1495      order.
1496
1497      The TREE_PURPOSE sublist contains those functions with default
1498      arguments that need post processing, and the TREE_VALUE sublist
1499      contains those functions with definitions that need post
1500      processing.
1501
1502      These lists can only be processed once the outermost class being
1503      defined is complete.  */
1504   tree unparsed_functions_queues;
1505
1506   /* The number of classes whose definitions are currently in
1507      progress.  */
1508   unsigned num_classes_being_defined;
1509
1510   /* The number of template parameter lists that apply directly to the
1511      current declaration.  */
1512   unsigned num_template_parameter_lists;
1513 } cp_parser;
1514
1515 /* The type of a function that parses some kind of expression.  */
1516 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1517
1518 /* Prototypes.  */
1519
1520 /* Constructors and destructors.  */
1521
1522 static cp_parser *cp_parser_new
1523   (void);
1524
1525 /* Routines to parse various constructs.
1526
1527    Those that return `tree' will return the error_mark_node (rather
1528    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1529    Sometimes, they will return an ordinary node if error-recovery was
1530    attempted, even though a parse error occurred.  So, to check
1531    whether or not a parse error occurred, you should always use
1532    cp_parser_error_occurred.  If the construct is optional (indicated
1533    either by an `_opt' in the name of the function that does the
1534    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1535    the construct is not present.  */
1536
1537 /* Lexical conventions [gram.lex]  */
1538
1539 static tree cp_parser_identifier
1540   (cp_parser *);
1541
1542 /* Basic concepts [gram.basic]  */
1543
1544 static bool cp_parser_translation_unit
1545   (cp_parser *);
1546
1547 /* Expressions [gram.expr]  */
1548
1549 static tree cp_parser_primary_expression
1550   (cp_parser *, cp_id_kind *, tree *);
1551 static tree cp_parser_id_expression
1552   (cp_parser *, bool, bool, bool *, bool);
1553 static tree cp_parser_unqualified_id
1554   (cp_parser *, bool, bool, bool);
1555 static tree cp_parser_nested_name_specifier_opt
1556   (cp_parser *, bool, bool, bool, bool);
1557 static tree cp_parser_nested_name_specifier
1558   (cp_parser *, bool, bool, bool, bool);
1559 static tree cp_parser_class_or_namespace_name
1560   (cp_parser *, bool, bool, bool, bool, bool);
1561 static tree cp_parser_postfix_expression
1562   (cp_parser *, bool);
1563 static tree cp_parser_postfix_open_square_expression
1564   (cp_parser *, tree, bool);
1565 static tree cp_parser_postfix_dot_deref_expression
1566   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1567 static tree cp_parser_parenthesized_expression_list
1568   (cp_parser *, bool, bool *);
1569 static void cp_parser_pseudo_destructor_name
1570   (cp_parser *, tree *, tree *);
1571 static tree cp_parser_unary_expression
1572   (cp_parser *, bool);
1573 static enum tree_code cp_parser_unary_operator
1574   (cp_token *);
1575 static tree cp_parser_new_expression
1576   (cp_parser *);
1577 static tree cp_parser_new_placement
1578   (cp_parser *);
1579 static tree cp_parser_new_type_id
1580   (cp_parser *, tree *);
1581 static cp_declarator *cp_parser_new_declarator_opt
1582   (cp_parser *);
1583 static cp_declarator *cp_parser_direct_new_declarator
1584   (cp_parser *);
1585 static tree cp_parser_new_initializer
1586   (cp_parser *);
1587 static tree cp_parser_delete_expression
1588   (cp_parser *);
1589 static tree cp_parser_cast_expression
1590   (cp_parser *, bool);
1591 static tree cp_parser_pm_expression
1592   (cp_parser *);
1593 static tree cp_parser_multiplicative_expression
1594   (cp_parser *);
1595 static tree cp_parser_additive_expression
1596   (cp_parser *);
1597 static tree cp_parser_shift_expression
1598   (cp_parser *);
1599 static tree cp_parser_relational_expression
1600   (cp_parser *);
1601 static tree cp_parser_equality_expression
1602   (cp_parser *);
1603 static tree cp_parser_and_expression
1604   (cp_parser *);
1605 static tree cp_parser_exclusive_or_expression
1606   (cp_parser *);
1607 static tree cp_parser_inclusive_or_expression
1608   (cp_parser *);
1609 static tree cp_parser_logical_and_expression
1610   (cp_parser *);
1611 static tree cp_parser_logical_or_expression
1612   (cp_parser *);
1613 static tree cp_parser_question_colon_clause
1614   (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616   (cp_parser *);
1617 static enum tree_code cp_parser_assignment_operator_opt
1618   (cp_parser *);
1619 static tree cp_parser_expression
1620   (cp_parser *);
1621 static tree cp_parser_constant_expression
1622   (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1624   (cp_parser *);
1625
1626 /* Statements [gram.stmt.stmt]  */
1627
1628 static void cp_parser_statement
1629   (cp_parser *, tree);
1630 static tree cp_parser_labeled_statement
1631   (cp_parser *, tree);
1632 static tree cp_parser_expression_statement
1633   (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635   (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637   (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1639   (cp_parser *);
1640 static tree cp_parser_condition
1641   (cp_parser *);
1642 static tree cp_parser_iteration_statement
1643   (cp_parser *);
1644 static void cp_parser_for_init_statement
1645   (cp_parser *);
1646 static tree cp_parser_jump_statement
1647   (cp_parser *);
1648 static void cp_parser_declaration_statement
1649   (cp_parser *);
1650
1651 static tree cp_parser_implicitly_scoped_statement
1652   (cp_parser *);
1653 static void cp_parser_already_scoped_statement
1654   (cp_parser *);
1655
1656 /* Declarations [gram.dcl.dcl] */
1657
1658 static void cp_parser_declaration_seq_opt
1659   (cp_parser *);
1660 static void cp_parser_declaration
1661   (cp_parser *);
1662 static void cp_parser_block_declaration
1663   (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665   (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1669   (cp_parser *);
1670 static tree cp_parser_function_specifier_opt
1671   (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1674    int *, bool *);
1675 static tree cp_parser_simple_type_specifier
1676   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1678   (cp_parser *);
1679 static tree cp_parser_elaborated_type_specifier
1680   (cp_parser *, bool, bool);
1681 static tree cp_parser_enum_specifier
1682   (cp_parser *);
1683 static void cp_parser_enumerator_list
1684   (cp_parser *, tree);
1685 static void cp_parser_enumerator_definition
1686   (cp_parser *, tree);
1687 static tree cp_parser_namespace_name
1688   (cp_parser *);
1689 static void cp_parser_namespace_definition
1690   (cp_parser *);
1691 static void cp_parser_namespace_body
1692   (cp_parser *);
1693 static tree cp_parser_qualified_namespace_specifier
1694   (cp_parser *);
1695 static void cp_parser_namespace_alias_definition
1696   (cp_parser *);
1697 static void cp_parser_using_declaration
1698   (cp_parser *);
1699 static void cp_parser_using_directive
1700   (cp_parser *);
1701 static void cp_parser_asm_definition
1702   (cp_parser *);
1703 static void cp_parser_linkage_specification
1704   (cp_parser *);
1705
1706 /* Declarators [gram.dcl.decl] */
1707
1708 static tree cp_parser_init_declarator
1709   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1710 static cp_declarator *cp_parser_declarator
1711   (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1712 static cp_declarator *cp_parser_direct_declarator
1713   (cp_parser *, cp_parser_declarator_kind, int *);
1714 static enum tree_code cp_parser_ptr_operator
1715   (cp_parser *, tree *, cp_cv_quals *);
1716 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1717   (cp_parser *);
1718 static tree cp_parser_declarator_id
1719   (cp_parser *);
1720 static tree cp_parser_type_id
1721   (cp_parser *);
1722 static void cp_parser_type_specifier_seq
1723   (cp_parser *, cp_decl_specifier_seq *);
1724 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1725   (cp_parser *);
1726 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1727   (cp_parser *, bool *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration
1729   (cp_parser *, bool, bool *);
1730 static void cp_parser_function_body
1731   (cp_parser *);
1732 static tree cp_parser_initializer
1733   (cp_parser *, bool *, bool *);
1734 static tree cp_parser_initializer_clause
1735   (cp_parser *, bool *);
1736 static tree cp_parser_initializer_list
1737   (cp_parser *, bool *);
1738
1739 static bool cp_parser_ctor_initializer_opt_and_function_body
1740   (cp_parser *);
1741
1742 /* Classes [gram.class] */
1743
1744 static tree cp_parser_class_name
1745   (cp_parser *, bool, bool, bool, bool, bool, bool);
1746 static tree cp_parser_class_specifier
1747   (cp_parser *);
1748 static tree cp_parser_class_head
1749   (cp_parser *, bool *, tree *);
1750 static enum tag_types cp_parser_class_key
1751   (cp_parser *);
1752 static void cp_parser_member_specification_opt
1753   (cp_parser *);
1754 static void cp_parser_member_declaration
1755   (cp_parser *);
1756 static tree cp_parser_pure_specifier
1757   (cp_parser *);
1758 static tree cp_parser_constant_initializer
1759   (cp_parser *);
1760
1761 /* Derived classes [gram.class.derived] */
1762
1763 static tree cp_parser_base_clause
1764   (cp_parser *);
1765 static tree cp_parser_base_specifier
1766   (cp_parser *);
1767
1768 /* Special member functions [gram.special] */
1769
1770 static tree cp_parser_conversion_function_id
1771   (cp_parser *);
1772 static tree cp_parser_conversion_type_id
1773   (cp_parser *);
1774 static cp_declarator *cp_parser_conversion_declarator_opt
1775   (cp_parser *);
1776 static bool cp_parser_ctor_initializer_opt
1777   (cp_parser *);
1778 static void cp_parser_mem_initializer_list
1779   (cp_parser *);
1780 static tree cp_parser_mem_initializer
1781   (cp_parser *);
1782 static tree cp_parser_mem_initializer_id
1783   (cp_parser *);
1784
1785 /* Overloading [gram.over] */
1786
1787 static tree cp_parser_operator_function_id
1788   (cp_parser *);
1789 static tree cp_parser_operator
1790   (cp_parser *);
1791
1792 /* Templates [gram.temp] */
1793
1794 static void cp_parser_template_declaration
1795   (cp_parser *, bool);
1796 static tree cp_parser_template_parameter_list
1797   (cp_parser *);
1798 static tree cp_parser_template_parameter
1799   (cp_parser *, bool *);
1800 static tree cp_parser_type_parameter
1801   (cp_parser *);
1802 static tree cp_parser_template_id
1803   (cp_parser *, bool, bool, bool);
1804 static tree cp_parser_template_name
1805   (cp_parser *, bool, bool, bool, bool *);
1806 static tree cp_parser_template_argument_list
1807   (cp_parser *);
1808 static tree cp_parser_template_argument
1809   (cp_parser *);
1810 static void cp_parser_explicit_instantiation
1811   (cp_parser *);
1812 static void cp_parser_explicit_specialization
1813   (cp_parser *);
1814
1815 /* Exception handling [gram.exception] */
1816
1817 static tree cp_parser_try_block
1818   (cp_parser *);
1819 static bool cp_parser_function_try_block
1820   (cp_parser *);
1821 static void cp_parser_handler_seq
1822   (cp_parser *);
1823 static void cp_parser_handler
1824   (cp_parser *);
1825 static tree cp_parser_exception_declaration
1826   (cp_parser *);
1827 static tree cp_parser_throw_expression
1828   (cp_parser *);
1829 static tree cp_parser_exception_specification_opt
1830   (cp_parser *);
1831 static tree cp_parser_type_id_list
1832   (cp_parser *);
1833
1834 /* GNU Extensions */
1835
1836 static tree cp_parser_asm_specification_opt
1837   (cp_parser *);
1838 static tree cp_parser_asm_operand_list
1839   (cp_parser *);
1840 static tree cp_parser_asm_clobber_list
1841   (cp_parser *);
1842 static tree cp_parser_attributes_opt
1843   (cp_parser *);
1844 static tree cp_parser_attribute_list
1845   (cp_parser *);
1846 static bool cp_parser_extension_opt
1847   (cp_parser *, int *);
1848 static void cp_parser_label_declaration
1849   (cp_parser *);
1850
1851 /* Utility Routines */
1852
1853 static tree cp_parser_lookup_name
1854   (cp_parser *, tree, bool, bool, bool, bool, bool *);
1855 static tree cp_parser_lookup_name_simple
1856   (cp_parser *, tree);
1857 static tree cp_parser_maybe_treat_template_as_class
1858   (tree, bool);
1859 static bool cp_parser_check_declarator_template_parameters
1860   (cp_parser *, cp_declarator *);
1861 static bool cp_parser_check_template_parameters
1862   (cp_parser *, unsigned);
1863 static tree cp_parser_simple_cast_expression
1864   (cp_parser *);
1865 static tree cp_parser_binary_expression
1866   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1867 static tree cp_parser_global_scope_opt
1868   (cp_parser *, bool);
1869 static bool cp_parser_constructor_declarator_p
1870   (cp_parser *, bool);
1871 static tree cp_parser_function_definition_from_specifiers_and_declarator
1872   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1873 static tree cp_parser_function_definition_after_declarator
1874   (cp_parser *, bool);
1875 static void cp_parser_template_declaration_after_export
1876   (cp_parser *, bool);
1877 static tree cp_parser_single_declaration
1878   (cp_parser *, bool, bool *);
1879 static tree cp_parser_functional_cast
1880   (cp_parser *, tree);
1881 static tree cp_parser_save_member_function_body
1882   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1883 static tree cp_parser_enclosed_template_argument_list
1884   (cp_parser *);
1885 static void cp_parser_save_default_args
1886   (cp_parser *, tree);
1887 static void cp_parser_late_parsing_for_member
1888   (cp_parser *, tree);
1889 static void cp_parser_late_parsing_default_args
1890   (cp_parser *, tree);
1891 static tree cp_parser_sizeof_operand
1892   (cp_parser *, enum rid);
1893 static bool cp_parser_declares_only_class_p
1894   (cp_parser *);
1895 static void cp_parser_set_storage_class
1896   (cp_decl_specifier_seq *, cp_storage_class);
1897 static void cp_parser_set_decl_spec_type
1898   (cp_decl_specifier_seq *, tree, bool);
1899 static bool cp_parser_friend_p
1900   (const cp_decl_specifier_seq *);
1901 static cp_token *cp_parser_require
1902   (cp_parser *, enum cpp_ttype, const char *);
1903 static cp_token *cp_parser_require_keyword
1904   (cp_parser *, enum rid, const char *);
1905 static bool cp_parser_token_starts_function_definition_p
1906   (cp_token *);
1907 static bool cp_parser_next_token_starts_class_definition_p
1908   (cp_parser *);
1909 static bool cp_parser_next_token_ends_template_argument_p
1910   (cp_parser *);
1911 static bool cp_parser_nth_token_starts_template_argument_list_p
1912   (cp_parser *, size_t);
1913 static enum tag_types cp_parser_token_is_class_key
1914   (cp_token *);
1915 static void cp_parser_check_class_key
1916   (enum tag_types, tree type);
1917 static void cp_parser_check_access_in_redeclaration
1918   (tree type);
1919 static bool cp_parser_optional_template_keyword
1920   (cp_parser *);
1921 static void cp_parser_pre_parsed_nested_name_specifier
1922   (cp_parser *);
1923 static void cp_parser_cache_group
1924   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1925 static void cp_parser_parse_tentatively
1926   (cp_parser *);
1927 static void cp_parser_commit_to_tentative_parse
1928   (cp_parser *);
1929 static void cp_parser_abort_tentative_parse
1930   (cp_parser *);
1931 static bool cp_parser_parse_definitely
1932   (cp_parser *);
1933 static inline bool cp_parser_parsing_tentatively
1934   (cp_parser *);
1935 static bool cp_parser_committed_to_tentative_parse
1936   (cp_parser *);
1937 static void cp_parser_error
1938   (cp_parser *, const char *);
1939 static void cp_parser_name_lookup_error
1940   (cp_parser *, tree, tree, const char *);
1941 static bool cp_parser_simulate_error
1942   (cp_parser *);
1943 static void cp_parser_check_type_definition
1944   (cp_parser *);
1945 static void cp_parser_check_for_definition_in_return_type
1946   (cp_declarator *, int);
1947 static void cp_parser_check_for_invalid_template_id
1948   (cp_parser *, tree);
1949 static bool cp_parser_non_integral_constant_expression
1950   (cp_parser *, const char *);
1951 static void cp_parser_diagnose_invalid_type_name
1952   (cp_parser *, tree, tree);
1953 static bool cp_parser_parse_and_diagnose_invalid_type_name
1954   (cp_parser *);
1955 static int cp_parser_skip_to_closing_parenthesis
1956   (cp_parser *, bool, bool, bool);
1957 static void cp_parser_skip_to_end_of_statement
1958   (cp_parser *);
1959 static void cp_parser_consume_semicolon_at_end_of_statement
1960   (cp_parser *);
1961 static void cp_parser_skip_to_end_of_block_or_statement
1962   (cp_parser *);
1963 static void cp_parser_skip_to_closing_brace
1964   (cp_parser *);
1965 static void cp_parser_skip_until_found
1966   (cp_parser *, enum cpp_ttype, const char *);
1967 static bool cp_parser_error_occurred
1968   (cp_parser *);
1969 static bool cp_parser_allow_gnu_extensions_p
1970   (cp_parser *);
1971 static bool cp_parser_is_string_literal
1972   (cp_token *);
1973 static bool cp_parser_is_keyword
1974   (cp_token *, enum rid);
1975 static tree cp_parser_make_typename_type
1976   (cp_parser *, tree, tree);
1977
1978 /* Returns nonzero if we are parsing tentatively.  */
1979
1980 static inline bool
1981 cp_parser_parsing_tentatively (cp_parser* parser)
1982 {
1983   return parser->context->next != NULL;
1984 }
1985
1986 /* Returns nonzero if TOKEN is a string literal.  */
1987
1988 static bool
1989 cp_parser_is_string_literal (cp_token* token)
1990 {
1991   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1992 }
1993
1994 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1995
1996 static bool
1997 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1998 {
1999   return token->keyword == keyword;
2000 }
2001
2002 /* Issue the indicated error MESSAGE.  */
2003
2004 static void
2005 cp_parser_error (cp_parser* parser, const char* message)
2006 {
2007   /* Output the MESSAGE -- unless we're parsing tentatively.  */
2008   if (!cp_parser_simulate_error (parser))
2009     {
2010       cp_token *token;
2011       token = cp_lexer_peek_token (parser->lexer);
2012       c_parse_error (message,
2013                      /* Because c_parser_error does not understand
2014                         CPP_KEYWORD, keywords are treated like
2015                         identifiers.  */
2016                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2017                      token->value);
2018     }
2019 }
2020
2021 /* Issue an error about name-lookup failing.  NAME is the
2022    IDENTIFIER_NODE DECL is the result of
2023    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2024    the thing that we hoped to find.  */
2025
2026 static void
2027 cp_parser_name_lookup_error (cp_parser* parser,
2028                              tree name,
2029                              tree decl,
2030                              const char* desired)
2031 {
2032   /* If name lookup completely failed, tell the user that NAME was not
2033      declared.  */
2034   if (decl == error_mark_node)
2035     {
2036       if (parser->scope && parser->scope != global_namespace)
2037         error ("`%D::%D' has not been declared",
2038                parser->scope, name);
2039       else if (parser->scope == global_namespace)
2040         error ("`::%D' has not been declared", name);
2041       else if (parser->object_scope 
2042                && !CLASS_TYPE_P (parser->object_scope))
2043         error ("request for member `%D' in non-class type `%T'",
2044                name, parser->object_scope);
2045       else if (parser->object_scope)
2046         error ("`%T::%D' has not been declared", 
2047                parser->object_scope, name);
2048       else
2049         error ("`%D' has not been declared", name);
2050     }
2051   else if (parser->scope && parser->scope != global_namespace)
2052     error ("`%D::%D' %s", parser->scope, name, desired);
2053   else if (parser->scope == global_namespace)
2054     error ("`::%D' %s", name, desired);
2055   else
2056     error ("`%D' %s", name, desired);
2057 }
2058
2059 /* If we are parsing tentatively, remember that an error has occurred
2060    during this tentative parse.  Returns true if the error was
2061    simulated; false if a message should be issued by the caller.  */
2062
2063 static bool
2064 cp_parser_simulate_error (cp_parser* parser)
2065 {
2066   if (cp_parser_parsing_tentatively (parser)
2067       && !cp_parser_committed_to_tentative_parse (parser))
2068     {
2069       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2070       return true;
2071     }
2072   return false;
2073 }
2074
2075 /* This function is called when a type is defined.  If type
2076    definitions are forbidden at this point, an error message is
2077    issued.  */
2078
2079 static void
2080 cp_parser_check_type_definition (cp_parser* parser)
2081 {
2082   /* If types are forbidden here, issue a message.  */
2083   if (parser->type_definition_forbidden_message)
2084     /* Use `%s' to print the string in case there are any escape
2085        characters in the message.  */
2086     error ("%s", parser->type_definition_forbidden_message);
2087 }
2088
2089 /* This function is called when a declaration is parsed.  If
2090    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2091    indicates that a type was defined in the decl-specifiers for DECL,
2092    then an error is issued.  */
2093
2094 static void
2095 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2096                                                int declares_class_or_enum)
2097 {
2098   /* [dcl.fct] forbids type definitions in return types.
2099      Unfortunately, it's not easy to know whether or not we are
2100      processing a return type until after the fact.  */
2101   while (declarator
2102          && (declarator->kind == cdk_pointer
2103              || declarator->kind == cdk_reference
2104              || declarator->kind == cdk_ptrmem))
2105     declarator = declarator->declarator;
2106   if (declarator
2107       && declarator->kind == cdk_function
2108       && declares_class_or_enum & 2)
2109     error ("new types may not be defined in a return type");
2110 }
2111
2112 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2113    "<" in any valid C++ program.  If the next token is indeed "<",
2114    issue a message warning the user about what appears to be an
2115    invalid attempt to form a template-id.  */
2116
2117 static void
2118 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2119                                          tree type)
2120 {
2121   ptrdiff_t start;
2122   cp_token *token;
2123
2124   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2125     {
2126       if (TYPE_P (type))
2127         error ("`%T' is not a template", type);
2128       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2129         error ("`%E' is not a template", type);
2130       else
2131         error ("invalid template-id");
2132       /* Remember the location of the invalid "<".  */
2133       if (cp_parser_parsing_tentatively (parser)
2134           && !cp_parser_committed_to_tentative_parse (parser))
2135         {
2136           token = cp_lexer_peek_token (parser->lexer);
2137           token = cp_lexer_prev_token (parser->lexer, token);
2138           start = cp_lexer_token_difference (parser->lexer,
2139                                              parser->lexer->first_token,
2140                                              token);
2141         }
2142       else
2143         start = -1;
2144       /* Consume the "<".  */
2145       cp_lexer_consume_token (parser->lexer);
2146       /* Parse the template arguments.  */
2147       cp_parser_enclosed_template_argument_list (parser);
2148       /* Permanently remove the invalid template arguments so that
2149          this error message is not issued again.  */
2150       if (start >= 0)
2151         {
2152           token = cp_lexer_advance_token (parser->lexer,
2153                                           parser->lexer->first_token,
2154                                           start);
2155           cp_lexer_purge_tokens_after (parser->lexer, token);
2156         }
2157     }
2158 }
2159
2160 /* If parsing an integral constant-expression, issue an error message
2161    about the fact that THING appeared and return true.  Otherwise,
2162    return false, marking the current expression as non-constant.  */
2163
2164 static bool
2165 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2166                                             const char *thing)
2167 {
2168   if (parser->integral_constant_expression_p)
2169     {
2170       if (!parser->allow_non_integral_constant_expression_p)
2171         {
2172           error ("%s cannot appear in a constant-expression", thing);
2173           return true;
2174         }
2175       parser->non_integral_constant_expression_p = true;
2176     }
2177   return false;
2178 }
2179
2180 /* Emit a diagnostic for an invalid type name. Consider also if it is
2181    qualified or not and the result of a lookup, to provide a better
2182    message.  */
2183
2184 static void
2185 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2186 {
2187   tree decl, old_scope;
2188   /* Try to lookup the identifier.  */
2189   old_scope = parser->scope;
2190   parser->scope = scope;
2191   decl = cp_parser_lookup_name_simple (parser, id);
2192   parser->scope = old_scope;
2193   /* If the lookup found a template-name, it means that the user forgot
2194   to specify an argument list. Emit an useful error message.  */
2195   if (TREE_CODE (decl) == TEMPLATE_DECL)
2196     error ("invalid use of template-name `%E' without an argument list",
2197       decl);
2198   else if (!parser->scope)
2199     {
2200       /* Issue an error message.  */
2201       error ("`%E' does not name a type", id);
2202       /* If we're in a template class, it's possible that the user was
2203          referring to a type from a base class.  For example:
2204
2205            template <typename T> struct A { typedef T X; };
2206            template <typename T> struct B : public A<T> { X x; };
2207
2208          The user should have said "typename A<T>::X".  */
2209       if (processing_template_decl && current_class_type)
2210         {
2211           tree b;
2212
2213           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2214                b;
2215                b = TREE_CHAIN (b))
2216             {
2217               tree base_type = BINFO_TYPE (b);
2218               if (CLASS_TYPE_P (base_type)
2219                   && dependent_type_p (base_type))
2220                 {
2221                   tree field;
2222                   /* Go from a particular instantiation of the
2223                      template (which will have an empty TYPE_FIELDs),
2224                      to the main version.  */
2225                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2226                   for (field = TYPE_FIELDS (base_type);
2227                        field;
2228                        field = TREE_CHAIN (field))
2229                     if (TREE_CODE (field) == TYPE_DECL
2230                         && DECL_NAME (field) == id)
2231                       {
2232                         inform ("(perhaps `typename %T::%E' was intended)",
2233                                 BINFO_TYPE (b), id);
2234                         break;
2235                       }
2236                   if (field)
2237                     break;
2238                 }
2239             }
2240         }
2241     }
2242   /* Here we diagnose qualified-ids where the scope is actually correct,
2243      but the identifier does not resolve to a valid type name.  */
2244   else
2245     {
2246       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2247         error ("`%E' in namespace `%E' does not name a type",
2248                id, parser->scope);
2249       else if (TYPE_P (parser->scope))
2250         error ("`%E' in class `%T' does not name a type",
2251                id, parser->scope);
2252       else
2253         gcc_unreachable ();
2254     }
2255 }
2256
2257 /* Check for a common situation where a type-name should be present,
2258    but is not, and issue a sensible error message.  Returns true if an
2259    invalid type-name was detected.
2260
2261    The situation handled by this function are variable declarations of the
2262    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2263    Usually, `ID' should name a type, but if we got here it means that it
2264    does not. We try to emit the best possible error message depending on
2265    how exactly the id-expression looks like.
2266 */
2267
2268 static bool
2269 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2270 {
2271   tree id;
2272
2273   cp_parser_parse_tentatively (parser);
2274   id = cp_parser_id_expression (parser,
2275                                 /*template_keyword_p=*/false,
2276                                 /*check_dependency_p=*/true,
2277                                 /*template_p=*/NULL,
2278                                 /*declarator_p=*/true);
2279   /* After the id-expression, there should be a plain identifier,
2280      otherwise this is not a simple variable declaration. Also, if
2281      the scope is dependent, we cannot do much.  */
2282   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2283       || (parser->scope && TYPE_P (parser->scope)
2284           && dependent_type_p (parser->scope)))
2285     {
2286       cp_parser_abort_tentative_parse (parser);
2287       return false;
2288     }
2289   if (!cp_parser_parse_definitely (parser))
2290     return false;
2291
2292   /* If we got here, this cannot be a valid variable declaration, thus
2293      the cp_parser_id_expression must have resolved to a plain identifier
2294      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
2295   gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
2296   /* Emit a diagnostic for the invalid type.  */
2297   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2298   /* Skip to the end of the declaration; there's no point in
2299      trying to process it.  */
2300   cp_parser_skip_to_end_of_block_or_statement (parser);
2301   return true;
2302 }
2303
2304 /* Consume tokens up to, and including, the next non-nested closing `)'.
2305    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2306    are doing error recovery. Returns -1 if OR_COMMA is true and we
2307    found an unnested comma.  */
2308
2309 static int
2310 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2311                                        bool recovering,
2312                                        bool or_comma,
2313                                        bool consume_paren)
2314 {
2315   unsigned paren_depth = 0;
2316   unsigned brace_depth = 0;
2317   int saved_c_lex_string_translate = c_lex_string_translate;
2318   int result;
2319
2320   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2321       && !cp_parser_committed_to_tentative_parse (parser))
2322     return 0;
2323
2324   if (! recovering)
2325     /* If we're looking ahead, keep both translated and untranslated
2326        strings.  */
2327     c_lex_string_translate = -1;
2328
2329   while (true)
2330     {
2331       cp_token *token;
2332
2333       /* If we've run out of tokens, then there is no closing `)'.  */
2334       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2335         {
2336           result = 0;
2337           break;
2338         }
2339
2340       token = cp_lexer_peek_token (parser->lexer);
2341
2342       /* This matches the processing in skip_to_end_of_statement.  */
2343       if (token->type == CPP_SEMICOLON && !brace_depth)
2344         {
2345           result = 0;
2346           break;
2347         }
2348       if (token->type == CPP_OPEN_BRACE)
2349         ++brace_depth;
2350       if (token->type == CPP_CLOSE_BRACE)
2351         {
2352           if (!brace_depth--)
2353             {
2354               result = 0;
2355               break;
2356             }
2357         }
2358       if (recovering && or_comma && token->type == CPP_COMMA
2359           && !brace_depth && !paren_depth)
2360         {
2361           result = -1;
2362           break;
2363         }
2364
2365       if (!brace_depth)
2366         {
2367           /* If it is an `(', we have entered another level of nesting.  */
2368           if (token->type == CPP_OPEN_PAREN)
2369             ++paren_depth;
2370           /* If it is a `)', then we might be done.  */
2371           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2372             {
2373               if (consume_paren)
2374                 cp_lexer_consume_token (parser->lexer);
2375               {
2376                 result = 1;
2377                 break;
2378               }
2379             }
2380         }
2381
2382       /* Consume the token.  */
2383       cp_lexer_consume_token (parser->lexer);
2384     }
2385
2386   c_lex_string_translate = saved_c_lex_string_translate;
2387   return result;
2388 }
2389
2390 /* Consume tokens until we reach the end of the current statement.
2391    Normally, that will be just before consuming a `;'.  However, if a
2392    non-nested `}' comes first, then we stop before consuming that.  */
2393
2394 static void
2395 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2396 {
2397   unsigned nesting_depth = 0;
2398
2399   while (true)
2400     {
2401       cp_token *token;
2402
2403       /* Peek at the next token.  */
2404       token = cp_lexer_peek_token (parser->lexer);
2405       /* If we've run out of tokens, stop.  */
2406       if (token->type == CPP_EOF)
2407         break;
2408       /* If the next token is a `;', we have reached the end of the
2409          statement.  */
2410       if (token->type == CPP_SEMICOLON && !nesting_depth)
2411         break;
2412       /* If the next token is a non-nested `}', then we have reached
2413          the end of the current block.  */
2414       if (token->type == CPP_CLOSE_BRACE)
2415         {
2416           /* If this is a non-nested `}', stop before consuming it.
2417              That way, when confronted with something like:
2418
2419                { 3 + }
2420
2421              we stop before consuming the closing `}', even though we
2422              have not yet reached a `;'.  */
2423           if (nesting_depth == 0)
2424             break;
2425           /* If it is the closing `}' for a block that we have
2426              scanned, stop -- but only after consuming the token.
2427              That way given:
2428
2429                 void f g () { ... }
2430                 typedef int I;
2431
2432              we will stop after the body of the erroneously declared
2433              function, but before consuming the following `typedef'
2434              declaration.  */
2435           if (--nesting_depth == 0)
2436             {
2437               cp_lexer_consume_token (parser->lexer);
2438               break;
2439             }
2440         }
2441       /* If it the next token is a `{', then we are entering a new
2442          block.  Consume the entire block.  */
2443       else if (token->type == CPP_OPEN_BRACE)
2444         ++nesting_depth;
2445       /* Consume the token.  */
2446       cp_lexer_consume_token (parser->lexer);
2447     }
2448 }
2449
2450 /* This function is called at the end of a statement or declaration.
2451    If the next token is a semicolon, it is consumed; otherwise, error
2452    recovery is attempted.  */
2453
2454 static void
2455 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2456 {
2457   /* Look for the trailing `;'.  */
2458   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2459     {
2460       /* If there is additional (erroneous) input, skip to the end of
2461          the statement.  */
2462       cp_parser_skip_to_end_of_statement (parser);
2463       /* If the next token is now a `;', consume it.  */
2464       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2465         cp_lexer_consume_token (parser->lexer);
2466     }
2467 }
2468
2469 /* Skip tokens until we have consumed an entire block, or until we
2470    have consumed a non-nested `;'.  */
2471
2472 static void
2473 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2474 {
2475   unsigned nesting_depth = 0;
2476
2477   while (true)
2478     {
2479       cp_token *token;
2480
2481       /* Peek at the next token.  */
2482       token = cp_lexer_peek_token (parser->lexer);
2483       /* If we've run out of tokens, stop.  */
2484       if (token->type == CPP_EOF)
2485         break;
2486       /* If the next token is a `;', we have reached the end of the
2487          statement.  */
2488       if (token->type == CPP_SEMICOLON && !nesting_depth)
2489         {
2490           /* Consume the `;'.  */
2491           cp_lexer_consume_token (parser->lexer);
2492           break;
2493         }
2494       /* Consume the token.  */
2495       token = cp_lexer_consume_token (parser->lexer);
2496       /* If the next token is a non-nested `}', then we have reached
2497          the end of the current block.  */
2498       if (token->type == CPP_CLOSE_BRACE
2499           && (nesting_depth == 0 || --nesting_depth == 0))
2500         break;
2501       /* If it the next token is a `{', then we are entering a new
2502          block.  Consume the entire block.  */
2503       if (token->type == CPP_OPEN_BRACE)
2504         ++nesting_depth;
2505     }
2506 }
2507
2508 /* Skip tokens until a non-nested closing curly brace is the next
2509    token.  */
2510
2511 static void
2512 cp_parser_skip_to_closing_brace (cp_parser *parser)
2513 {
2514   unsigned nesting_depth = 0;
2515
2516   while (true)
2517     {
2518       cp_token *token;
2519
2520       /* Peek at the next token.  */
2521       token = cp_lexer_peek_token (parser->lexer);
2522       /* If we've run out of tokens, stop.  */
2523       if (token->type == CPP_EOF)
2524         break;
2525       /* If the next token is a non-nested `}', then we have reached
2526          the end of the current block.  */
2527       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2528         break;
2529       /* If it the next token is a `{', then we are entering a new
2530          block.  Consume the entire block.  */
2531       else if (token->type == CPP_OPEN_BRACE)
2532         ++nesting_depth;
2533       /* Consume the token.  */
2534       cp_lexer_consume_token (parser->lexer);
2535     }
2536 }
2537
2538 /* This is a simple wrapper around make_typename_type. When the id is
2539    an unresolved identifier node, we can provide a superior diagnostic
2540    using cp_parser_diagnose_invalid_type_name.  */
2541
2542 static tree
2543 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2544 {
2545   tree result;
2546   if (TREE_CODE (id) == IDENTIFIER_NODE)
2547     {
2548       result = make_typename_type (scope, id, /*complain=*/0);
2549       if (result == error_mark_node)
2550         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2551       return result;
2552     }
2553   return make_typename_type (scope, id, tf_error);
2554 }
2555
2556
2557 /* Create a new C++ parser.  */
2558
2559 static cp_parser *
2560 cp_parser_new (void)
2561 {
2562   cp_parser *parser;
2563   cp_lexer *lexer;
2564
2565   /* cp_lexer_new_main is called before calling ggc_alloc because
2566      cp_lexer_new_main might load a PCH file.  */
2567   lexer = cp_lexer_new_main ();
2568
2569   parser = GGC_CNEW (cp_parser);
2570   parser->lexer = lexer;
2571   parser->context = cp_parser_context_new (NULL);
2572
2573   /* For now, we always accept GNU extensions.  */
2574   parser->allow_gnu_extensions_p = 1;
2575
2576   /* The `>' token is a greater-than operator, not the end of a
2577      template-id.  */
2578   parser->greater_than_is_operator_p = true;
2579
2580   parser->default_arg_ok_p = true;
2581
2582   /* We are not parsing a constant-expression.  */
2583   parser->integral_constant_expression_p = false;
2584   parser->allow_non_integral_constant_expression_p = false;
2585   parser->non_integral_constant_expression_p = false;
2586
2587   /* Local variable names are not forbidden.  */
2588   parser->local_variables_forbidden_p = false;
2589
2590   /* We are not processing an `extern "C"' declaration.  */
2591   parser->in_unbraced_linkage_specification_p = false;
2592
2593   /* We are not processing a declarator.  */
2594   parser->in_declarator_p = false;
2595
2596   /* We are not processing a template-argument-list.  */
2597   parser->in_template_argument_list_p = false;
2598
2599   /* We are not in an iteration statement.  */
2600   parser->in_iteration_statement_p = false;
2601
2602   /* We are not in a switch statement.  */
2603   parser->in_switch_statement_p = false;
2604
2605   /* We are not parsing a type-id inside an expression.  */
2606   parser->in_type_id_in_expr_p = false;
2607
2608   /* The unparsed function queue is empty.  */
2609   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2610
2611   /* There are no classes being defined.  */
2612   parser->num_classes_being_defined = 0;
2613
2614   /* No template parameters apply.  */
2615   parser->num_template_parameter_lists = 0;
2616
2617   return parser;
2618 }
2619
2620 /* Lexical conventions [gram.lex]  */
2621
2622 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2623    identifier.  */
2624
2625 static tree
2626 cp_parser_identifier (cp_parser* parser)
2627 {
2628   cp_token *token;
2629
2630   /* Look for the identifier.  */
2631   token = cp_parser_require (parser, CPP_NAME, "identifier");
2632   /* Return the value.  */
2633   return token ? token->value : error_mark_node;
2634 }
2635
2636 /* Basic concepts [gram.basic]  */
2637
2638 /* Parse a translation-unit.
2639
2640    translation-unit:
2641      declaration-seq [opt]
2642
2643    Returns TRUE if all went well.  */
2644
2645 static bool
2646 cp_parser_translation_unit (cp_parser* parser)
2647 {
2648   /* The address of the first non-permanent object on the declarator
2649      obstack.  */
2650   static void *declarator_obstack_base;
2651
2652   bool success;
2653
2654   /* Create the declarator obstack, if necessary.  */
2655   if (!cp_error_declarator)
2656     {
2657       gcc_obstack_init (&declarator_obstack);
2658       /* Create the error declarator.  */
2659       cp_error_declarator = make_declarator (cdk_error);
2660       /* Create the empty parameter list.  */
2661       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2662       /* Remember where the base of the declarator obstack lies.  */
2663       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2664     }
2665
2666   while (true)
2667     {
2668       cp_parser_declaration_seq_opt (parser);
2669
2670       /* If there are no tokens left then all went well.  */
2671       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2672         {
2673           /* Consume the EOF token.  */
2674           cp_parser_require (parser, CPP_EOF, "end-of-file");
2675
2676           /* Finish up.  */
2677           finish_translation_unit ();
2678
2679           success = true;
2680           break;
2681         }
2682       else
2683         {
2684           cp_parser_error (parser, "expected declaration");
2685           success = false;
2686           break;
2687         }
2688     }
2689
2690   /* Make sure the declarator obstack was fully cleaned up.  */
2691   gcc_assert (obstack_next_free (&declarator_obstack)
2692               == declarator_obstack_base);
2693
2694   /* All went well.  */
2695   return success;
2696 }
2697
2698 /* Expressions [gram.expr] */
2699
2700 /* Parse a primary-expression.
2701
2702    primary-expression:
2703      literal
2704      this
2705      ( expression )
2706      id-expression
2707
2708    GNU Extensions:
2709
2710    primary-expression:
2711      ( compound-statement )
2712      __builtin_va_arg ( assignment-expression , type-id )
2713
2714    literal:
2715      __null
2716
2717    Returns a representation of the expression.
2718
2719    *IDK indicates what kind of id-expression (if any) was present.
2720
2721    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2722    used as the operand of a pointer-to-member.  In that case,
2723    *QUALIFYING_CLASS gives the class that is used as the qualifying
2724    class in the pointer-to-member.  */
2725
2726 static tree
2727 cp_parser_primary_expression (cp_parser *parser,
2728                               cp_id_kind *idk,
2729                               tree *qualifying_class)
2730 {
2731   cp_token *token;
2732
2733   /* Assume the primary expression is not an id-expression.  */
2734   *idk = CP_ID_KIND_NONE;
2735   /* And that it cannot be used as pointer-to-member.  */
2736   *qualifying_class = NULL_TREE;
2737
2738   /* Peek at the next token.  */
2739   token = cp_lexer_peek_token (parser->lexer);
2740   switch (token->type)
2741     {
2742       /* literal:
2743            integer-literal
2744            character-literal
2745            floating-literal
2746            string-literal
2747            boolean-literal  */
2748     case CPP_CHAR:
2749     case CPP_WCHAR:
2750     case CPP_NUMBER:
2751       token = cp_lexer_consume_token (parser->lexer);
2752       return token->value;
2753
2754     case CPP_STRING:
2755     case CPP_WSTRING:
2756       token = cp_lexer_consume_token (parser->lexer);
2757       if (TREE_CHAIN (token->value))
2758         return TREE_CHAIN (token->value);
2759       else
2760         return token->value;
2761
2762     case CPP_OPEN_PAREN:
2763       {
2764         tree expr;
2765         bool saved_greater_than_is_operator_p;
2766
2767         /* Consume the `('.  */
2768         cp_lexer_consume_token (parser->lexer);
2769         /* Within a parenthesized expression, a `>' token is always
2770            the greater-than operator.  */
2771         saved_greater_than_is_operator_p
2772           = parser->greater_than_is_operator_p;
2773         parser->greater_than_is_operator_p = true;
2774         /* If we see `( { ' then we are looking at the beginning of
2775            a GNU statement-expression.  */
2776         if (cp_parser_allow_gnu_extensions_p (parser)
2777             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2778           {
2779             /* Statement-expressions are not allowed by the standard.  */
2780             if (pedantic)
2781               pedwarn ("ISO C++ forbids braced-groups within expressions");
2782
2783             /* And they're not allowed outside of a function-body; you
2784                cannot, for example, write:
2785
2786                  int i = ({ int j = 3; j + 1; });
2787
2788                at class or namespace scope.  */
2789             if (!at_function_scope_p ())
2790               error ("statement-expressions are allowed only inside functions");
2791             /* Start the statement-expression.  */
2792             expr = begin_stmt_expr ();
2793             /* Parse the compound-statement.  */
2794             cp_parser_compound_statement (parser, expr, false);
2795             /* Finish up.  */
2796             expr = finish_stmt_expr (expr, false);
2797           }
2798         else
2799           {
2800             /* Parse the parenthesized expression.  */
2801             expr = cp_parser_expression (parser);
2802             /* Let the front end know that this expression was
2803                enclosed in parentheses. This matters in case, for
2804                example, the expression is of the form `A::B', since
2805                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2806                not.  */
2807             finish_parenthesized_expr (expr);
2808           }
2809         /* The `>' token might be the end of a template-id or
2810            template-parameter-list now.  */
2811         parser->greater_than_is_operator_p
2812           = saved_greater_than_is_operator_p;
2813         /* Consume the `)'.  */
2814         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2815           cp_parser_skip_to_end_of_statement (parser);
2816
2817         return expr;
2818       }
2819
2820     case CPP_KEYWORD:
2821       switch (token->keyword)
2822         {
2823           /* These two are the boolean literals.  */
2824         case RID_TRUE:
2825           cp_lexer_consume_token (parser->lexer);
2826           return boolean_true_node;
2827         case RID_FALSE:
2828           cp_lexer_consume_token (parser->lexer);
2829           return boolean_false_node;
2830
2831           /* The `__null' literal.  */
2832         case RID_NULL:
2833           cp_lexer_consume_token (parser->lexer);
2834           return null_node;
2835
2836           /* Recognize the `this' keyword.  */
2837         case RID_THIS:
2838           cp_lexer_consume_token (parser->lexer);
2839           if (parser->local_variables_forbidden_p)
2840             {
2841               error ("`this' may not be used in this context");
2842               return error_mark_node;
2843             }
2844           /* Pointers cannot appear in constant-expressions.  */
2845           if (cp_parser_non_integral_constant_expression (parser,
2846                                                           "`this'"))
2847             return error_mark_node;
2848           return finish_this_expr ();
2849
2850           /* The `operator' keyword can be the beginning of an
2851              id-expression.  */
2852         case RID_OPERATOR:
2853           goto id_expression;
2854
2855         case RID_FUNCTION_NAME:
2856         case RID_PRETTY_FUNCTION_NAME:
2857         case RID_C99_FUNCTION_NAME:
2858           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2859              __func__ are the names of variables -- but they are
2860              treated specially.  Therefore, they are handled here,
2861              rather than relying on the generic id-expression logic
2862              below.  Grammatically, these names are id-expressions.
2863
2864              Consume the token.  */
2865           token = cp_lexer_consume_token (parser->lexer);
2866           /* Look up the name.  */
2867           return finish_fname (token->value);
2868
2869         case RID_VA_ARG:
2870           {
2871             tree expression;
2872             tree type;
2873
2874             /* The `__builtin_va_arg' construct is used to handle
2875                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2876             cp_lexer_consume_token (parser->lexer);
2877             /* Look for the opening `('.  */
2878             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2879             /* Now, parse the assignment-expression.  */
2880             expression = cp_parser_assignment_expression (parser);
2881             /* Look for the `,'.  */
2882             cp_parser_require (parser, CPP_COMMA, "`,'");
2883             /* Parse the type-id.  */
2884             type = cp_parser_type_id (parser);
2885             /* Look for the closing `)'.  */
2886             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2887             /* Using `va_arg' in a constant-expression is not
2888                allowed.  */
2889             if (cp_parser_non_integral_constant_expression (parser,
2890                                                             "`va_arg'"))
2891               return error_mark_node;
2892             return build_x_va_arg (expression, type);
2893           }
2894
2895         case RID_OFFSETOF:
2896           return cp_parser_builtin_offsetof (parser);
2897
2898         default:
2899           cp_parser_error (parser, "expected primary-expression");
2900           return error_mark_node;
2901         }
2902
2903       /* An id-expression can start with either an identifier, a
2904          `::' as the beginning of a qualified-id, or the "operator"
2905          keyword.  */
2906     case CPP_NAME:
2907     case CPP_SCOPE:
2908     case CPP_TEMPLATE_ID:
2909     case CPP_NESTED_NAME_SPECIFIER:
2910       {
2911         tree id_expression;
2912         tree decl;
2913         const char *error_msg;
2914
2915       id_expression:
2916         /* Parse the id-expression.  */
2917         id_expression
2918           = cp_parser_id_expression (parser,
2919                                      /*template_keyword_p=*/false,
2920                                      /*check_dependency_p=*/true,
2921                                      /*template_p=*/NULL,
2922                                      /*declarator_p=*/false);
2923         if (id_expression == error_mark_node)
2924           return error_mark_node;
2925         /* If we have a template-id, then no further lookup is
2926            required.  If the template-id was for a template-class, we
2927            will sometimes have a TYPE_DECL at this point.  */
2928         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2929             || TREE_CODE (id_expression) == TYPE_DECL)
2930           decl = id_expression;
2931         /* Look up the name.  */
2932         else
2933           {
2934             bool ambiguous_p;
2935
2936             decl = cp_parser_lookup_name (parser, id_expression,
2937                                           /*is_type=*/false,
2938                                           /*is_template=*/false,
2939                                           /*is_namespace=*/false,
2940                                           /*check_dependency=*/true,
2941                                           &ambiguous_p);
2942             /* If the lookup was ambiguous, an error will already have
2943                been issued.  */
2944             if (ambiguous_p)
2945               return error_mark_node;
2946             /* If name lookup gives us a SCOPE_REF, then the
2947                qualifying scope was dependent.  Just propagate the
2948                name.  */
2949             if (TREE_CODE (decl) == SCOPE_REF)
2950               {
2951                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2952                   *qualifying_class = TREE_OPERAND (decl, 0);
2953                 return decl;
2954               }
2955             /* Check to see if DECL is a local variable in a context
2956                where that is forbidden.  */
2957             if (parser->local_variables_forbidden_p
2958                 && local_variable_p (decl))
2959               {
2960                 /* It might be that we only found DECL because we are
2961                    trying to be generous with pre-ISO scoping rules.
2962                    For example, consider:
2963
2964                      int i;
2965                      void g() {
2966                        for (int i = 0; i < 10; ++i) {}
2967                        extern void f(int j = i);
2968                      }
2969
2970                    Here, name look up will originally find the out
2971                    of scope `i'.  We need to issue a warning message,
2972                    but then use the global `i'.  */
2973                 decl = check_for_out_of_scope_variable (decl);
2974                 if (local_variable_p (decl))
2975                   {
2976                     error ("local variable `%D' may not appear in this context",
2977                            decl);
2978                     return error_mark_node;
2979                   }
2980               }
2981           }
2982
2983         decl = finish_id_expression (id_expression, decl, parser->scope,
2984                                      idk, qualifying_class,
2985                                      parser->integral_constant_expression_p,
2986                                      parser->allow_non_integral_constant_expression_p,
2987                                      &parser->non_integral_constant_expression_p,
2988                                      &error_msg);
2989         if (error_msg)
2990           cp_parser_error (parser, error_msg);
2991         return decl;
2992       }
2993
2994       /* Anything else is an error.  */
2995     default:
2996       cp_parser_error (parser, "expected primary-expression");
2997       return error_mark_node;
2998     }
2999 }
3000
3001 /* Parse an id-expression.
3002
3003    id-expression:
3004      unqualified-id
3005      qualified-id
3006
3007    qualified-id:
3008      :: [opt] nested-name-specifier template [opt] unqualified-id
3009      :: identifier
3010      :: operator-function-id
3011      :: template-id
3012
3013    Return a representation of the unqualified portion of the
3014    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3015    a `::' or nested-name-specifier.
3016
3017    Often, if the id-expression was a qualified-id, the caller will
3018    want to make a SCOPE_REF to represent the qualified-id.  This
3019    function does not do this in order to avoid wastefully creating
3020    SCOPE_REFs when they are not required.
3021
3022    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3023    `template' keyword.
3024
3025    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3026    uninstantiated templates.
3027
3028    If *TEMPLATE_P is non-NULL, it is set to true iff the
3029    `template' keyword is used to explicitly indicate that the entity
3030    named is a template.
3031
3032    If DECLARATOR_P is true, the id-expression is appearing as part of
3033    a declarator, rather than as part of an expression.  */
3034
3035 static tree
3036 cp_parser_id_expression (cp_parser *parser,
3037                          bool template_keyword_p,
3038                          bool check_dependency_p,
3039                          bool *template_p,
3040                          bool declarator_p)
3041 {
3042   bool global_scope_p;
3043   bool nested_name_specifier_p;
3044
3045   /* Assume the `template' keyword was not used.  */
3046   if (template_p)
3047     *template_p = false;
3048
3049   /* Look for the optional `::' operator.  */
3050   global_scope_p
3051     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3052        != NULL_TREE);
3053   /* Look for the optional nested-name-specifier.  */
3054   nested_name_specifier_p
3055     = (cp_parser_nested_name_specifier_opt (parser,
3056                                             /*typename_keyword_p=*/false,
3057                                             check_dependency_p,
3058                                             /*type_p=*/false,
3059                                             /*is_declarator=*/false)
3060        != NULL_TREE);
3061   /* If there is a nested-name-specifier, then we are looking at
3062      the first qualified-id production.  */
3063   if (nested_name_specifier_p)
3064     {
3065       tree saved_scope;
3066       tree saved_object_scope;
3067       tree saved_qualifying_scope;
3068       tree unqualified_id;
3069       bool is_template;
3070
3071       /* See if the next token is the `template' keyword.  */
3072       if (!template_p)
3073         template_p = &is_template;
3074       *template_p = cp_parser_optional_template_keyword (parser);
3075       /* Name lookup we do during the processing of the
3076          unqualified-id might obliterate SCOPE.  */
3077       saved_scope = parser->scope;
3078       saved_object_scope = parser->object_scope;
3079       saved_qualifying_scope = parser->qualifying_scope;
3080       /* Process the final unqualified-id.  */
3081       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3082                                                  check_dependency_p,
3083                                                  declarator_p);
3084       /* Restore the SAVED_SCOPE for our caller.  */
3085       parser->scope = saved_scope;
3086       parser->object_scope = saved_object_scope;
3087       parser->qualifying_scope = saved_qualifying_scope;
3088
3089       return unqualified_id;
3090     }
3091   /* Otherwise, if we are in global scope, then we are looking at one
3092      of the other qualified-id productions.  */
3093   else if (global_scope_p)
3094     {
3095       cp_token *token;
3096       tree id;
3097
3098       /* Peek at the next token.  */
3099       token = cp_lexer_peek_token (parser->lexer);
3100
3101       /* If it's an identifier, and the next token is not a "<", then
3102          we can avoid the template-id case.  This is an optimization
3103          for this common case.  */
3104       if (token->type == CPP_NAME
3105           && !cp_parser_nth_token_starts_template_argument_list_p
3106                (parser, 2))
3107         return cp_parser_identifier (parser);
3108
3109       cp_parser_parse_tentatively (parser);
3110       /* Try a template-id.  */
3111       id = cp_parser_template_id (parser,
3112                                   /*template_keyword_p=*/false,
3113                                   /*check_dependency_p=*/true,
3114                                   declarator_p);
3115       /* If that worked, we're done.  */
3116       if (cp_parser_parse_definitely (parser))
3117         return id;
3118
3119       /* Peek at the next token.  (Changes in the token buffer may
3120          have invalidated the pointer obtained above.)  */
3121       token = cp_lexer_peek_token (parser->lexer);
3122
3123       switch (token->type)
3124         {
3125         case CPP_NAME:
3126           return cp_parser_identifier (parser);
3127
3128         case CPP_KEYWORD:
3129           if (token->keyword == RID_OPERATOR)
3130             return cp_parser_operator_function_id (parser);
3131           /* Fall through.  */
3132
3133         default:
3134           cp_parser_error (parser, "expected id-expression");
3135           return error_mark_node;
3136         }
3137     }
3138   else
3139     return cp_parser_unqualified_id (parser, template_keyword_p,
3140                                      /*check_dependency_p=*/true,
3141                                      declarator_p);
3142 }
3143
3144 /* Parse an unqualified-id.
3145
3146    unqualified-id:
3147      identifier
3148      operator-function-id
3149      conversion-function-id
3150      ~ class-name
3151      template-id
3152
3153    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3154    keyword, in a construct like `A::template ...'.
3155
3156    Returns a representation of unqualified-id.  For the `identifier'
3157    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3158    production a BIT_NOT_EXPR is returned; the operand of the
3159    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3160    other productions, see the documentation accompanying the
3161    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3162    names are looked up in uninstantiated templates.  If DECLARATOR_P
3163    is true, the unqualified-id is appearing as part of a declarator,
3164    rather than as part of an expression.  */
3165
3166 static tree
3167 cp_parser_unqualified_id (cp_parser* parser,
3168                           bool template_keyword_p,
3169                           bool check_dependency_p,
3170                           bool declarator_p)
3171 {
3172   cp_token *token;
3173
3174   /* Peek at the next token.  */
3175   token = cp_lexer_peek_token (parser->lexer);
3176
3177   switch (token->type)
3178     {
3179     case CPP_NAME:
3180       {
3181         tree id;
3182
3183         /* We don't know yet whether or not this will be a
3184            template-id.  */
3185         cp_parser_parse_tentatively (parser);
3186         /* Try a template-id.  */
3187         id = cp_parser_template_id (parser, template_keyword_p,
3188                                     check_dependency_p,
3189                                     declarator_p);
3190         /* If it worked, we're done.  */
3191         if (cp_parser_parse_definitely (parser))
3192           return id;
3193         /* Otherwise, it's an ordinary identifier.  */
3194         return cp_parser_identifier (parser);
3195       }
3196
3197     case CPP_TEMPLATE_ID:
3198       return cp_parser_template_id (parser, template_keyword_p,
3199                                     check_dependency_p,
3200                                     declarator_p);
3201
3202     case CPP_COMPL:
3203       {
3204         tree type_decl;
3205         tree qualifying_scope;
3206         tree object_scope;
3207         tree scope;
3208
3209         /* Consume the `~' token.  */
3210         cp_lexer_consume_token (parser->lexer);
3211         /* Parse the class-name.  The standard, as written, seems to
3212            say that:
3213
3214              template <typename T> struct S { ~S (); };
3215              template <typename T> S<T>::~S() {}
3216
3217            is invalid, since `~' must be followed by a class-name, but
3218            `S<T>' is dependent, and so not known to be a class.
3219            That's not right; we need to look in uninstantiated
3220            templates.  A further complication arises from:
3221
3222              template <typename T> void f(T t) {
3223                t.T::~T();
3224              }
3225
3226            Here, it is not possible to look up `T' in the scope of `T'
3227            itself.  We must look in both the current scope, and the
3228            scope of the containing complete expression.
3229
3230            Yet another issue is:
3231
3232              struct S {
3233                int S;
3234                ~S();
3235              };
3236
3237              S::~S() {}
3238
3239            The standard does not seem to say that the `S' in `~S'
3240            should refer to the type `S' and not the data member
3241            `S::S'.  */
3242
3243         /* DR 244 says that we look up the name after the "~" in the
3244            same scope as we looked up the qualifying name.  That idea
3245            isn't fully worked out; it's more complicated than that.  */
3246         scope = parser->scope;
3247         object_scope = parser->object_scope;
3248         qualifying_scope = parser->qualifying_scope;
3249
3250         /* If the name is of the form "X::~X" it's OK.  */
3251         if (scope && TYPE_P (scope)
3252             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3253             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3254                 == CPP_OPEN_PAREN)
3255             && (cp_lexer_peek_token (parser->lexer)->value
3256                 == TYPE_IDENTIFIER (scope)))
3257           {
3258             cp_lexer_consume_token (parser->lexer);
3259             return build_nt (BIT_NOT_EXPR, scope);
3260           }
3261
3262         /* If there was an explicit qualification (S::~T), first look
3263            in the scope given by the qualification (i.e., S).  */
3264         if (scope)
3265           {
3266             cp_parser_parse_tentatively (parser);
3267             type_decl = cp_parser_class_name (parser,
3268                                               /*typename_keyword_p=*/false,
3269                                               /*template_keyword_p=*/false,
3270                                               /*type_p=*/false,
3271                                               /*check_dependency=*/false,
3272                                               /*class_head_p=*/false,
3273                                               declarator_p);
3274             if (cp_parser_parse_definitely (parser))
3275               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3276           }
3277         /* In "N::S::~S", look in "N" as well.  */
3278         if (scope && qualifying_scope)
3279           {
3280             cp_parser_parse_tentatively (parser);
3281             parser->scope = qualifying_scope;
3282             parser->object_scope = NULL_TREE;
3283             parser->qualifying_scope = NULL_TREE;
3284             type_decl
3285               = cp_parser_class_name (parser,
3286                                       /*typename_keyword_p=*/false,
3287                                       /*template_keyword_p=*/false,
3288                                       /*type_p=*/false,
3289                                       /*check_dependency=*/false,
3290                                       /*class_head_p=*/false,
3291                                       declarator_p);
3292             if (cp_parser_parse_definitely (parser))
3293               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3294           }
3295         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3296         else if (object_scope)
3297           {
3298             cp_parser_parse_tentatively (parser);
3299             parser->scope = object_scope;
3300             parser->object_scope = NULL_TREE;
3301             parser->qualifying_scope = NULL_TREE;
3302             type_decl
3303               = cp_parser_class_name (parser,
3304                                       /*typename_keyword_p=*/false,
3305                                       /*template_keyword_p=*/false,
3306                                       /*type_p=*/false,
3307                                       /*check_dependency=*/false,
3308                                       /*class_head_p=*/false,
3309                                       declarator_p);
3310             if (cp_parser_parse_definitely (parser))
3311               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3312           }
3313         /* Look in the surrounding context.  */
3314         parser->scope = NULL_TREE;
3315         parser->object_scope = NULL_TREE;
3316         parser->qualifying_scope = NULL_TREE;
3317         type_decl
3318           = cp_parser_class_name (parser,
3319                                   /*typename_keyword_p=*/false,
3320                                   /*template_keyword_p=*/false,
3321                                   /*type_p=*/false,
3322                                   /*check_dependency=*/false,
3323                                   /*class_head_p=*/false,
3324                                   declarator_p);
3325         /* If an error occurred, assume that the name of the
3326            destructor is the same as the name of the qualifying
3327            class.  That allows us to keep parsing after running
3328            into ill-formed destructor names.  */
3329         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3330           return build_nt (BIT_NOT_EXPR, scope);
3331         else if (type_decl == error_mark_node)
3332           return error_mark_node;
3333
3334         /* [class.dtor]
3335
3336            A typedef-name that names a class shall not be used as the
3337            identifier in the declarator for a destructor declaration.  */
3338         if (declarator_p
3339             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3340             && !DECL_SELF_REFERENCE_P (type_decl))
3341           error ("typedef-name `%D' used as destructor declarator",
3342                  type_decl);
3343
3344         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3345       }
3346
3347     case CPP_KEYWORD:
3348       if (token->keyword == RID_OPERATOR)
3349         {
3350           tree id;
3351
3352           /* This could be a template-id, so we try that first.  */
3353           cp_parser_parse_tentatively (parser);
3354           /* Try a template-id.  */
3355           id = cp_parser_template_id (parser, template_keyword_p,
3356                                       /*check_dependency_p=*/true,
3357                                       declarator_p);
3358           /* If that worked, we're done.  */
3359           if (cp_parser_parse_definitely (parser))
3360             return id;
3361           /* We still don't know whether we're looking at an
3362              operator-function-id or a conversion-function-id.  */
3363           cp_parser_parse_tentatively (parser);
3364           /* Try an operator-function-id.  */
3365           id = cp_parser_operator_function_id (parser);
3366           /* If that didn't work, try a conversion-function-id.  */
3367           if (!cp_parser_parse_definitely (parser))
3368             id = cp_parser_conversion_function_id (parser);
3369
3370           return id;
3371         }
3372       /* Fall through.  */
3373
3374     default:
3375       cp_parser_error (parser, "expected unqualified-id");
3376       return error_mark_node;
3377     }
3378 }
3379
3380 /* Parse an (optional) nested-name-specifier.
3381
3382    nested-name-specifier:
3383      class-or-namespace-name :: nested-name-specifier [opt]
3384      class-or-namespace-name :: template nested-name-specifier [opt]
3385
3386    PARSER->SCOPE should be set appropriately before this function is
3387    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3388    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3389    in name lookups.
3390
3391    Sets PARSER->SCOPE to the class (TYPE) or namespace
3392    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3393    it unchanged if there is no nested-name-specifier.  Returns the new
3394    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3395
3396    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3397    part of a declaration and/or decl-specifier.  */
3398
3399 static tree
3400 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3401                                      bool typename_keyword_p,
3402                                      bool check_dependency_p,
3403                                      bool type_p,
3404                                      bool is_declaration)
3405 {
3406   bool success = false;
3407   tree access_check = NULL_TREE;
3408   ptrdiff_t start;
3409   cp_token* token;
3410
3411   /* If the next token corresponds to a nested name specifier, there
3412      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3413      false, it may have been true before, in which case something
3414      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3415      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3416      CHECK_DEPENDENCY_P is false, we have to fall through into the
3417      main loop.  */
3418   if (check_dependency_p
3419       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3420     {
3421       cp_parser_pre_parsed_nested_name_specifier (parser);
3422       return parser->scope;
3423     }
3424
3425   /* Remember where the nested-name-specifier starts.  */
3426   if (cp_parser_parsing_tentatively (parser)
3427       && !cp_parser_committed_to_tentative_parse (parser))
3428     {
3429       token = cp_lexer_peek_token (parser->lexer);
3430       start = cp_lexer_token_difference (parser->lexer,
3431                                          parser->lexer->first_token,
3432                                          token);
3433     }
3434   else
3435     start = -1;
3436
3437   push_deferring_access_checks (dk_deferred);
3438
3439   while (true)
3440     {
3441       tree new_scope;
3442       tree old_scope;
3443       tree saved_qualifying_scope;
3444       bool template_keyword_p;
3445
3446       /* Spot cases that cannot be the beginning of a
3447          nested-name-specifier.  */
3448       token = cp_lexer_peek_token (parser->lexer);
3449
3450       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3451          the already parsed nested-name-specifier.  */
3452       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3453         {
3454           /* Grab the nested-name-specifier and continue the loop.  */
3455           cp_parser_pre_parsed_nested_name_specifier (parser);
3456           success = true;
3457           continue;
3458         }
3459
3460       /* Spot cases that cannot be the beginning of a
3461          nested-name-specifier.  On the second and subsequent times
3462          through the loop, we look for the `template' keyword.  */
3463       if (success && token->keyword == RID_TEMPLATE)
3464         ;
3465       /* A template-id can start a nested-name-specifier.  */
3466       else if (token->type == CPP_TEMPLATE_ID)
3467         ;
3468       else
3469         {
3470           /* If the next token is not an identifier, then it is
3471              definitely not a class-or-namespace-name.  */
3472           if (token->type != CPP_NAME)
3473             break;
3474           /* If the following token is neither a `<' (to begin a
3475              template-id), nor a `::', then we are not looking at a
3476              nested-name-specifier.  */
3477           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3478           if (token->type != CPP_SCOPE
3479               && !cp_parser_nth_token_starts_template_argument_list_p
3480                   (parser, 2))
3481             break;
3482         }
3483
3484       /* The nested-name-specifier is optional, so we parse
3485          tentatively.  */
3486       cp_parser_parse_tentatively (parser);
3487
3488       /* Look for the optional `template' keyword, if this isn't the
3489          first time through the loop.  */
3490       if (success)
3491         template_keyword_p = cp_parser_optional_template_keyword (parser);
3492       else
3493         template_keyword_p = false;
3494
3495       /* Save the old scope since the name lookup we are about to do
3496          might destroy it.  */
3497       old_scope = parser->scope;
3498       saved_qualifying_scope = parser->qualifying_scope;
3499       /* Parse the qualifying entity.  */
3500       new_scope
3501         = cp_parser_class_or_namespace_name (parser,
3502                                              typename_keyword_p,
3503                                              template_keyword_p,
3504                                              check_dependency_p,
3505                                              type_p,
3506                                              is_declaration);
3507       /* Look for the `::' token.  */
3508       cp_parser_require (parser, CPP_SCOPE, "`::'");
3509
3510       /* If we found what we wanted, we keep going; otherwise, we're
3511          done.  */
3512       if (!cp_parser_parse_definitely (parser))
3513         {
3514           bool error_p = false;
3515
3516           /* Restore the OLD_SCOPE since it was valid before the
3517              failed attempt at finding the last
3518              class-or-namespace-name.  */
3519           parser->scope = old_scope;
3520           parser->qualifying_scope = saved_qualifying_scope;
3521           /* If the next token is an identifier, and the one after
3522              that is a `::', then any valid interpretation would have
3523              found a class-or-namespace-name.  */
3524           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3525                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3526                      == CPP_SCOPE)
3527                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3528                      != CPP_COMPL))
3529             {
3530               token = cp_lexer_consume_token (parser->lexer);
3531               if (!error_p)
3532                 {
3533                   tree decl;
3534
3535                   decl = cp_parser_lookup_name_simple (parser, token->value);
3536                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3537                     error ("`%D' used without template parameters",
3538                            decl);
3539                   else
3540                     cp_parser_name_lookup_error
3541                       (parser, token->value, decl,
3542                        "is not a class or namespace");
3543                   parser->scope = NULL_TREE;
3544                   error_p = true;
3545                   /* Treat this as a successful nested-name-specifier
3546                      due to:
3547
3548                      [basic.lookup.qual]
3549
3550                      If the name found is not a class-name (clause
3551                      _class_) or namespace-name (_namespace.def_), the
3552                      program is ill-formed.  */
3553                   success = true;
3554                 }
3555               cp_lexer_consume_token (parser->lexer);
3556             }
3557           break;
3558         }
3559
3560       /* We've found one valid nested-name-specifier.  */
3561       success = true;
3562       /* Make sure we look in the right scope the next time through
3563          the loop.  */
3564       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3565                        ? TREE_TYPE (new_scope)
3566                        : new_scope);
3567       /* If it is a class scope, try to complete it; we are about to
3568          be looking up names inside the class.  */
3569       if (TYPE_P (parser->scope)
3570           /* Since checking types for dependency can be expensive,
3571              avoid doing it if the type is already complete.  */
3572           && !COMPLETE_TYPE_P (parser->scope)
3573           /* Do not try to complete dependent types.  */
3574           && !dependent_type_p (parser->scope))
3575         complete_type (parser->scope);
3576     }
3577
3578   /* Retrieve any deferred checks.  Do not pop this access checks yet
3579      so the memory will not be reclaimed during token replacing below.  */
3580   access_check = get_deferred_access_checks ();
3581
3582   /* If parsing tentatively, replace the sequence of tokens that makes
3583      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3584      token.  That way, should we re-parse the token stream, we will
3585      not have to repeat the effort required to do the parse, nor will
3586      we issue duplicate error messages.  */
3587   if (success && start >= 0)
3588     {
3589       /* Find the token that corresponds to the start of the
3590          template-id.  */
3591       token = cp_lexer_advance_token (parser->lexer,
3592                                       parser->lexer->first_token,
3593                                       start);
3594
3595       /* Reset the contents of the START token.  */
3596       token->type = CPP_NESTED_NAME_SPECIFIER;
3597       token->value = build_tree_list (access_check, parser->scope);
3598       TREE_TYPE (token->value) = parser->qualifying_scope;
3599       token->keyword = RID_MAX;
3600       /* Purge all subsequent tokens.  */
3601       cp_lexer_purge_tokens_after (parser->lexer, token);
3602     }
3603
3604   pop_deferring_access_checks ();
3605   return success ? parser->scope : NULL_TREE;
3606 }
3607
3608 /* Parse a nested-name-specifier.  See
3609    cp_parser_nested_name_specifier_opt for details.  This function
3610    behaves identically, except that it will an issue an error if no
3611    nested-name-specifier is present, and it will return
3612    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3613    is present.  */
3614
3615 static tree
3616 cp_parser_nested_name_specifier (cp_parser *parser,
3617                                  bool typename_keyword_p,
3618                                  bool check_dependency_p,
3619                                  bool type_p,
3620                                  bool is_declaration)
3621 {
3622   tree scope;
3623
3624   /* Look for the nested-name-specifier.  */
3625   scope = cp_parser_nested_name_specifier_opt (parser,
3626                                                typename_keyword_p,
3627                                                check_dependency_p,
3628                                                type_p,
3629                                                is_declaration);
3630   /* If it was not present, issue an error message.  */
3631   if (!scope)
3632     {
3633       cp_parser_error (parser, "expected nested-name-specifier");
3634       parser->scope = NULL_TREE;
3635       return error_mark_node;
3636     }
3637
3638   return scope;
3639 }
3640
3641 /* Parse a class-or-namespace-name.
3642
3643    class-or-namespace-name:
3644      class-name
3645      namespace-name
3646
3647    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3648    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3649    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3650    TYPE_P is TRUE iff the next name should be taken as a class-name,
3651    even the same name is declared to be another entity in the same
3652    scope.
3653
3654    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3655    specified by the class-or-namespace-name.  If neither is found the
3656    ERROR_MARK_NODE is returned.  */
3657
3658 static tree
3659 cp_parser_class_or_namespace_name (cp_parser *parser,
3660                                    bool typename_keyword_p,
3661                                    bool template_keyword_p,
3662                                    bool check_dependency_p,
3663                                    bool type_p,
3664                                    bool is_declaration)
3665 {
3666   tree saved_scope;
3667   tree saved_qualifying_scope;
3668   tree saved_object_scope;
3669   tree scope;
3670   bool only_class_p;
3671
3672   /* Before we try to parse the class-name, we must save away the
3673      current PARSER->SCOPE since cp_parser_class_name will destroy
3674      it.  */
3675   saved_scope = parser->scope;
3676   saved_qualifying_scope = parser->qualifying_scope;
3677   saved_object_scope = parser->object_scope;
3678   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3679      there is no need to look for a namespace-name.  */
3680   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3681   if (!only_class_p)
3682     cp_parser_parse_tentatively (parser);
3683   scope = cp_parser_class_name (parser,
3684                                 typename_keyword_p,
3685                                 template_keyword_p,
3686                                 type_p,
3687                                 check_dependency_p,
3688                                 /*class_head_p=*/false,
3689                                 is_declaration);
3690   /* If that didn't work, try for a namespace-name.  */
3691   if (!only_class_p && !cp_parser_parse_definitely (parser))
3692     {
3693       /* Restore the saved scope.  */
3694       parser->scope = saved_scope;
3695       parser->qualifying_scope = saved_qualifying_scope;
3696       parser->object_scope = saved_object_scope;
3697       /* If we are not looking at an identifier followed by the scope
3698          resolution operator, then this is not part of a
3699          nested-name-specifier.  (Note that this function is only used
3700          to parse the components of a nested-name-specifier.)  */
3701       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3702           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3703         return error_mark_node;
3704       scope = cp_parser_namespace_name (parser);
3705     }
3706
3707   return scope;
3708 }
3709
3710 /* Parse a postfix-expression.
3711
3712    postfix-expression:
3713      primary-expression
3714      postfix-expression [ expression ]
3715      postfix-expression ( expression-list [opt] )
3716      simple-type-specifier ( expression-list [opt] )
3717      typename :: [opt] nested-name-specifier identifier
3718        ( expression-list [opt] )
3719      typename :: [opt] nested-name-specifier template [opt] template-id
3720        ( expression-list [opt] )
3721      postfix-expression . template [opt] id-expression
3722      postfix-expression -> template [opt] id-expression
3723      postfix-expression . pseudo-destructor-name
3724      postfix-expression -> pseudo-destructor-name
3725      postfix-expression ++
3726      postfix-expression --
3727      dynamic_cast < type-id > ( expression )
3728      static_cast < type-id > ( expression )
3729      reinterpret_cast < type-id > ( expression )
3730      const_cast < type-id > ( expression )
3731      typeid ( expression )
3732      typeid ( type-id )
3733
3734    GNU Extension:
3735
3736    postfix-expression:
3737      ( type-id ) { initializer-list , [opt] }
3738
3739    This extension is a GNU version of the C99 compound-literal
3740    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3741    but they are essentially the same concept.)
3742
3743    If ADDRESS_P is true, the postfix expression is the operand of the
3744    `&' operator.
3745
3746    Returns a representation of the expression.  */
3747
3748 static tree
3749 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3750 {
3751   cp_token *token;
3752   enum rid keyword;
3753   cp_id_kind idk = CP_ID_KIND_NONE;
3754   tree postfix_expression = NULL_TREE;
3755   /* Non-NULL only if the current postfix-expression can be used to
3756      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3757      class used to qualify the member.  */
3758   tree qualifying_class = NULL_TREE;
3759
3760   /* Peek at the next token.  */
3761   token = cp_lexer_peek_token (parser->lexer);
3762   /* Some of the productions are determined by keywords.  */
3763   keyword = token->keyword;
3764   switch (keyword)
3765     {
3766     case RID_DYNCAST:
3767     case RID_STATCAST:
3768     case RID_REINTCAST:
3769     case RID_CONSTCAST:
3770       {
3771         tree type;
3772         tree expression;
3773         const char *saved_message;
3774
3775         /* All of these can be handled in the same way from the point
3776            of view of parsing.  Begin by consuming the token
3777            identifying the cast.  */
3778         cp_lexer_consume_token (parser->lexer);
3779
3780         /* New types cannot be defined in the cast.  */
3781         saved_message = parser->type_definition_forbidden_message;
3782         parser->type_definition_forbidden_message
3783           = "types may not be defined in casts";
3784
3785         /* Look for the opening `<'.  */
3786         cp_parser_require (parser, CPP_LESS, "`<'");
3787         /* Parse the type to which we are casting.  */
3788         type = cp_parser_type_id (parser);
3789         /* Look for the closing `>'.  */
3790         cp_parser_require (parser, CPP_GREATER, "`>'");
3791         /* Restore the old message.  */
3792         parser->type_definition_forbidden_message = saved_message;
3793
3794         /* And the expression which is being cast.  */
3795         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3796         expression = cp_parser_expression (parser);
3797         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3798
3799         /* Only type conversions to integral or enumeration types
3800            can be used in constant-expressions.  */
3801         if (parser->integral_constant_expression_p
3802             && !dependent_type_p (type)
3803             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3804             && (cp_parser_non_integral_constant_expression
3805                 (parser,
3806                  "a cast to a type other than an integral or "
3807                  "enumeration type")))
3808           return error_mark_node;
3809
3810         switch (keyword)
3811           {
3812           case RID_DYNCAST:
3813             postfix_expression
3814               = build_dynamic_cast (type, expression);
3815             break;
3816           case RID_STATCAST:
3817             postfix_expression
3818               = build_static_cast (type, expression);
3819             break;
3820           case RID_REINTCAST:
3821             postfix_expression
3822               = build_reinterpret_cast (type, expression);
3823             break;
3824           case RID_CONSTCAST:
3825             postfix_expression
3826               = build_const_cast (type, expression);
3827             break;
3828           default:
3829             gcc_unreachable ();
3830           }
3831       }
3832       break;
3833
3834     case RID_TYPEID:
3835       {
3836         tree type;
3837         const char *saved_message;
3838         bool saved_in_type_id_in_expr_p;
3839
3840         /* Consume the `typeid' token.  */
3841         cp_lexer_consume_token (parser->lexer);
3842         /* Look for the `(' token.  */
3843         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3844         /* Types cannot be defined in a `typeid' expression.  */
3845         saved_message = parser->type_definition_forbidden_message;
3846         parser->type_definition_forbidden_message
3847           = "types may not be defined in a `typeid\' expression";
3848         /* We can't be sure yet whether we're looking at a type-id or an
3849            expression.  */
3850         cp_parser_parse_tentatively (parser);
3851         /* Try a type-id first.  */
3852         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3853         parser->in_type_id_in_expr_p = true;
3854         type = cp_parser_type_id (parser);
3855         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3856         /* Look for the `)' token.  Otherwise, we can't be sure that
3857            we're not looking at an expression: consider `typeid (int
3858            (3))', for example.  */
3859         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3860         /* If all went well, simply lookup the type-id.  */
3861         if (cp_parser_parse_definitely (parser))
3862           postfix_expression = get_typeid (type);
3863         /* Otherwise, fall back to the expression variant.  */
3864         else
3865           {
3866             tree expression;
3867
3868             /* Look for an expression.  */
3869             expression = cp_parser_expression (parser);
3870             /* Compute its typeid.  */
3871             postfix_expression = build_typeid (expression);
3872             /* Look for the `)' token.  */
3873             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3874           }
3875         /* `typeid' may not appear in an integral constant expression.  */
3876         if (cp_parser_non_integral_constant_expression(parser,
3877                                                        "`typeid' operator"))
3878           return error_mark_node;
3879         /* Restore the saved message.  */
3880         parser->type_definition_forbidden_message = saved_message;
3881       }
3882       break;
3883
3884     case RID_TYPENAME:
3885       {
3886         bool template_p = false;
3887         tree id;
3888         tree type;
3889
3890         /* Consume the `typename' token.  */
3891         cp_lexer_consume_token (parser->lexer);
3892         /* Look for the optional `::' operator.  */
3893         cp_parser_global_scope_opt (parser,
3894                                     /*current_scope_valid_p=*/false);
3895         /* Look for the nested-name-specifier.  */
3896         cp_parser_nested_name_specifier (parser,
3897                                          /*typename_keyword_p=*/true,
3898                                          /*check_dependency_p=*/true,
3899                                          /*type_p=*/true,
3900                                          /*is_declaration=*/true);
3901         /* Look for the optional `template' keyword.  */
3902         template_p = cp_parser_optional_template_keyword (parser);
3903         /* We don't know whether we're looking at a template-id or an
3904            identifier.  */
3905         cp_parser_parse_tentatively (parser);
3906         /* Try a template-id.  */
3907         id = cp_parser_template_id (parser, template_p,
3908                                     /*check_dependency_p=*/true,
3909                                     /*is_declaration=*/true);
3910         /* If that didn't work, try an identifier.  */
3911         if (!cp_parser_parse_definitely (parser))
3912           id = cp_parser_identifier (parser);
3913         /* If we look up a template-id in a non-dependent qualifying
3914            scope, there's no need to create a dependent type.  */
3915         if (TREE_CODE (id) == TYPE_DECL
3916             && !dependent_type_p (parser->scope))
3917           type = TREE_TYPE (id);
3918         /* Create a TYPENAME_TYPE to represent the type to which the
3919            functional cast is being performed.  */
3920         else
3921           type = make_typename_type (parser->scope, id,
3922                                      /*complain=*/1);
3923
3924         postfix_expression = cp_parser_functional_cast (parser, type);
3925       }
3926       break;
3927
3928     default:
3929       {
3930         tree type;
3931
3932         /* If the next thing is a simple-type-specifier, we may be
3933            looking at a functional cast.  We could also be looking at
3934            an id-expression.  So, we try the functional cast, and if
3935            that doesn't work we fall back to the primary-expression.  */
3936         cp_parser_parse_tentatively (parser);
3937         /* Look for the simple-type-specifier.  */
3938         type = cp_parser_simple_type_specifier (parser,
3939                                                 /*decl_specs=*/NULL,
3940                                                 CP_PARSER_FLAGS_NONE);
3941         /* Parse the cast itself.  */
3942         if (!cp_parser_error_occurred (parser))
3943           postfix_expression
3944             = cp_parser_functional_cast (parser, type);
3945         /* If that worked, we're done.  */
3946         if (cp_parser_parse_definitely (parser))
3947           break;
3948
3949         /* If the functional-cast didn't work out, try a
3950            compound-literal.  */
3951         if (cp_parser_allow_gnu_extensions_p (parser)
3952             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3953           {
3954             tree initializer_list = NULL_TREE;
3955             bool saved_in_type_id_in_expr_p;
3956
3957             cp_parser_parse_tentatively (parser);
3958             /* Consume the `('.  */
3959             cp_lexer_consume_token (parser->lexer);
3960             /* Parse the type.  */
3961             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3962             parser->in_type_id_in_expr_p = true;
3963             type = cp_parser_type_id (parser);
3964             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3965             /* Look for the `)'.  */
3966             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3967             /* Look for the `{'.  */
3968             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3969             /* If things aren't going well, there's no need to
3970                keep going.  */
3971             if (!cp_parser_error_occurred (parser))
3972               {
3973                 bool non_constant_p;
3974                 /* Parse the initializer-list.  */
3975                 initializer_list
3976                   = cp_parser_initializer_list (parser, &non_constant_p);
3977                 /* Allow a trailing `,'.  */
3978                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3979                   cp_lexer_consume_token (parser->lexer);
3980                 /* Look for the final `}'.  */
3981                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3982               }
3983             /* If that worked, we're definitely looking at a
3984                compound-literal expression.  */
3985             if (cp_parser_parse_definitely (parser))
3986               {
3987                 /* Warn the user that a compound literal is not
3988                    allowed in standard C++.  */
3989                 if (pedantic)
3990                   pedwarn ("ISO C++ forbids compound-literals");
3991                 /* Form the representation of the compound-literal.  */
3992                 postfix_expression
3993                   = finish_compound_literal (type, initializer_list);
3994                 break;
3995               }
3996           }
3997
3998         /* It must be a primary-expression.  */
3999         postfix_expression = cp_parser_primary_expression (parser,
4000                                                            &idk,
4001                                                            &qualifying_class);
4002       }
4003       break;
4004     }
4005
4006   /* If we were avoiding committing to the processing of a
4007      qualified-id until we knew whether or not we had a
4008      pointer-to-member, we now know.  */
4009   if (qualifying_class)
4010     {
4011       bool done;
4012
4013       /* Peek at the next token.  */
4014       token = cp_lexer_peek_token (parser->lexer);
4015       done = (token->type != CPP_OPEN_SQUARE
4016               && token->type != CPP_OPEN_PAREN
4017               && token->type != CPP_DOT
4018               && token->type != CPP_DEREF
4019               && token->type != CPP_PLUS_PLUS
4020               && token->type != CPP_MINUS_MINUS);
4021
4022       postfix_expression = finish_qualified_id_expr (qualifying_class,
4023                                                      postfix_expression,
4024                                                      done,
4025                                                      address_p);
4026       if (done)
4027         return postfix_expression;
4028     }
4029
4030   /* Keep looping until the postfix-expression is complete.  */
4031   while (true)
4032     {
4033       if (idk == CP_ID_KIND_UNQUALIFIED
4034           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4035           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4036         /* It is not a Koenig lookup function call.  */
4037         postfix_expression
4038           = unqualified_name_lookup_error (postfix_expression);
4039
4040       /* Peek at the next token.  */
4041       token = cp_lexer_peek_token (parser->lexer);
4042
4043       switch (token->type)
4044         {
4045         case CPP_OPEN_SQUARE:
4046           postfix_expression
4047             = cp_parser_postfix_open_square_expression (parser,
4048                                                         postfix_expression,
4049                                                         false);
4050           idk = CP_ID_KIND_NONE;
4051           break;
4052
4053         case CPP_OPEN_PAREN:
4054           /* postfix-expression ( expression-list [opt] ) */
4055           {
4056             bool koenig_p;
4057             tree args = (cp_parser_parenthesized_expression_list
4058                          (parser, false, /*non_constant_p=*/NULL));
4059
4060             if (args == error_mark_node)
4061               {
4062                 postfix_expression = error_mark_node;
4063                 break;
4064               }
4065
4066             /* Function calls are not permitted in
4067                constant-expressions.  */
4068             if (cp_parser_non_integral_constant_expression (parser,
4069                                                             "a function call"))
4070               {
4071                 postfix_expression = error_mark_node;
4072                 break;
4073               }
4074
4075             koenig_p = false;
4076             if (idk == CP_ID_KIND_UNQUALIFIED)
4077               {
4078                 /* We do not perform argument-dependent lookup if
4079                    normal lookup finds a non-function, in accordance
4080                    with the expected resolution of DR 218.  */
4081                 if (args
4082                     && (is_overloaded_fn (postfix_expression)
4083                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4084                   {
4085                     koenig_p = true;
4086                     postfix_expression
4087                       = perform_koenig_lookup (postfix_expression, args);
4088                   }
4089                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4090                   postfix_expression
4091                     = unqualified_fn_lookup_error (postfix_expression);
4092               }
4093
4094             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4095               {
4096                 tree instance = TREE_OPERAND (postfix_expression, 0);
4097                 tree fn = TREE_OPERAND (postfix_expression, 1);
4098
4099                 if (processing_template_decl
4100                     && (type_dependent_expression_p (instance)
4101                         || (!BASELINK_P (fn)
4102                             && TREE_CODE (fn) != FIELD_DECL)
4103                         || type_dependent_expression_p (fn)
4104                         || any_type_dependent_arguments_p (args)))
4105                   {
4106                     postfix_expression
4107                       = build_min_nt (CALL_EXPR, postfix_expression,
4108                                       args, NULL_TREE);
4109                     break;
4110                   }
4111
4112                 if (BASELINK_P (fn))
4113                   postfix_expression
4114                     = (build_new_method_call
4115                        (instance, fn, args, NULL_TREE,
4116                         (idk == CP_ID_KIND_QUALIFIED
4117                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4118                 else
4119                   postfix_expression
4120                     = finish_call_expr (postfix_expression, args,
4121                                         /*disallow_virtual=*/false,
4122                                         /*koenig_p=*/false);
4123               }
4124             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4125                      || TREE_CODE (postfix_expression) == MEMBER_REF
4126                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4127               postfix_expression = (build_offset_ref_call_from_tree
4128                                     (postfix_expression, args));
4129             else if (idk == CP_ID_KIND_QUALIFIED)
4130               /* A call to a static class member, or a namespace-scope
4131                  function.  */
4132               postfix_expression
4133                 = finish_call_expr (postfix_expression, args,
4134                                     /*disallow_virtual=*/true,
4135                                     koenig_p);
4136             else
4137               /* All other function calls.  */
4138               postfix_expression
4139                 = finish_call_expr (postfix_expression, args,
4140                                     /*disallow_virtual=*/false,
4141                                     koenig_p);
4142
4143             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4144             idk = CP_ID_KIND_NONE;
4145           }
4146           break;
4147
4148         case CPP_DOT:
4149         case CPP_DEREF:
4150           /* postfix-expression . template [opt] id-expression
4151              postfix-expression . pseudo-destructor-name
4152              postfix-expression -> template [opt] id-expression
4153              postfix-expression -> pseudo-destructor-name */
4154
4155           /* Consume the `.' or `->' operator.  */
4156           cp_lexer_consume_token (parser->lexer);
4157
4158           postfix_expression
4159             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4160                                                       postfix_expression,
4161                                                       false, &idk);
4162           break;
4163
4164         case CPP_PLUS_PLUS:
4165           /* postfix-expression ++  */
4166           /* Consume the `++' token.  */
4167           cp_lexer_consume_token (parser->lexer);
4168           /* Generate a representation for the complete expression.  */
4169           postfix_expression
4170             = finish_increment_expr (postfix_expression,
4171                                      POSTINCREMENT_EXPR);
4172           /* Increments may not appear in constant-expressions.  */
4173           if (cp_parser_non_integral_constant_expression (parser,
4174                                                           "an increment"))
4175             postfix_expression = error_mark_node;
4176           idk = CP_ID_KIND_NONE;
4177           break;
4178
4179         case CPP_MINUS_MINUS:
4180           /* postfix-expression -- */
4181           /* Consume the `--' token.  */
4182           cp_lexer_consume_token (parser->lexer);
4183           /* Generate a representation for the complete expression.  */
4184           postfix_expression
4185             = finish_increment_expr (postfix_expression,
4186                                      POSTDECREMENT_EXPR);
4187           /* Decrements may not appear in constant-expressions.  */
4188           if (cp_parser_non_integral_constant_expression (parser,
4189                                                           "a decrement"))
4190             postfix_expression = error_mark_node;
4191           idk = CP_ID_KIND_NONE;
4192           break;
4193
4194         default:
4195           return postfix_expression;
4196         }
4197     }
4198
4199   /* We should never get here.  */
4200   gcc_unreachable ();
4201   return error_mark_node;
4202 }
4203
4204 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4205    by cp_parser_builtin_offsetof.  We're looking for
4206
4207      postfix-expression [ expression ]
4208
4209    FOR_OFFSETOF is set if we're being called in that context, which
4210    changes how we deal with integer constant expressions.  */
4211
4212 static tree
4213 cp_parser_postfix_open_square_expression (cp_parser *parser,
4214                                           tree postfix_expression,
4215                                           bool for_offsetof)
4216 {
4217   tree index;
4218
4219   /* Consume the `[' token.  */
4220   cp_lexer_consume_token (parser->lexer);
4221
4222   /* Parse the index expression.  */
4223   /* ??? For offsetof, there is a question of what to allow here.  If
4224      offsetof is not being used in an integral constant expression context,
4225      then we *could* get the right answer by computing the value at runtime.
4226      If we are in an integral constant expression context, then we might
4227      could accept any constant expression; hard to say without analysis.
4228      Rather than open the barn door too wide right away, allow only integer
4229      constant expresions here.  */
4230   if (for_offsetof)
4231     index = cp_parser_constant_expression (parser, false, NULL);
4232   else
4233     index = cp_parser_expression (parser);
4234
4235   /* Look for the closing `]'.  */
4236   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4237
4238   /* Build the ARRAY_REF.  */
4239   postfix_expression = grok_array_decl (postfix_expression, index);
4240
4241   /* When not doing offsetof, array references are not permitted in
4242      constant-expressions.  */
4243   if (!for_offsetof
4244       && (cp_parser_non_integral_constant_expression
4245           (parser, "an array reference")))
4246     postfix_expression = error_mark_node;
4247
4248   return postfix_expression;
4249 }
4250
4251 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4252    by cp_parser_builtin_offsetof.  We're looking for
4253
4254      postfix-expression . template [opt] id-expression
4255      postfix-expression . pseudo-destructor-name
4256      postfix-expression -> template [opt] id-expression
4257      postfix-expression -> pseudo-destructor-name
4258
4259    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4260    limits what of the above we'll actually accept, but nevermind.
4261    TOKEN_TYPE is the "." or "->" token, which will already have been
4262    removed from the stream.  */
4263
4264 static tree
4265 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4266                                         enum cpp_ttype token_type,
4267                                         tree postfix_expression,
4268                                         bool for_offsetof, cp_id_kind *idk)
4269 {
4270   tree name;
4271   bool dependent_p;
4272   bool template_p;
4273   tree scope = NULL_TREE;
4274
4275   /* If this is a `->' operator, dereference the pointer.  */
4276   if (token_type == CPP_DEREF)
4277     postfix_expression = build_x_arrow (postfix_expression);
4278   /* Check to see whether or not the expression is type-dependent.  */
4279   dependent_p = type_dependent_expression_p (postfix_expression);
4280   /* The identifier following the `->' or `.' is not qualified.  */
4281   parser->scope = NULL_TREE;
4282   parser->qualifying_scope = NULL_TREE;
4283   parser->object_scope = NULL_TREE;
4284   *idk = CP_ID_KIND_NONE;
4285   /* Enter the scope corresponding to the type of the object
4286      given by the POSTFIX_EXPRESSION.  */
4287   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4288     {
4289       scope = TREE_TYPE (postfix_expression);
4290       /* According to the standard, no expression should ever have
4291          reference type.  Unfortunately, we do not currently match
4292          the standard in this respect in that our internal representation
4293          of an expression may have reference type even when the standard
4294          says it does not.  Therefore, we have to manually obtain the
4295          underlying type here.  */
4296       scope = non_reference (scope);
4297       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4298       scope = complete_type_or_else (scope, NULL_TREE);
4299       /* Let the name lookup machinery know that we are processing a
4300          class member access expression.  */
4301       parser->context->object_type = scope;
4302       /* If something went wrong, we want to be able to discern that case,
4303          as opposed to the case where there was no SCOPE due to the type
4304          of expression being dependent.  */
4305       if (!scope)
4306         scope = error_mark_node;
4307       /* If the SCOPE was erroneous, make the various semantic analysis
4308          functions exit quickly -- and without issuing additional error
4309          messages.  */
4310       if (scope == error_mark_node)
4311         postfix_expression = error_mark_node;
4312     }
4313
4314   /* If the SCOPE is not a scalar type, we are looking at an
4315      ordinary class member access expression, rather than a
4316      pseudo-destructor-name.  */
4317   if (!scope || !SCALAR_TYPE_P (scope))
4318     {
4319       template_p = cp_parser_optional_template_keyword (parser);
4320       /* Parse the id-expression.  */
4321       name = cp_parser_id_expression (parser, template_p,
4322                                       /*check_dependency_p=*/true,
4323                                       /*template_p=*/NULL,
4324                                       /*declarator_p=*/false);
4325       /* In general, build a SCOPE_REF if the member name is qualified.
4326          However, if the name was not dependent and has already been
4327          resolved; there is no need to build the SCOPE_REF.  For example;
4328
4329              struct X { void f(); };
4330              template <typename T> void f(T* t) { t->X::f(); }
4331
4332          Even though "t" is dependent, "X::f" is not and has been resolved
4333          to a BASELINK; there is no need to include scope information.  */
4334
4335       /* But we do need to remember that there was an explicit scope for
4336          virtual function calls.  */
4337       if (parser->scope)
4338         *idk = CP_ID_KIND_QUALIFIED;
4339
4340       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4341         {
4342           name = build_nt (SCOPE_REF, parser->scope, name);
4343           parser->scope = NULL_TREE;
4344           parser->qualifying_scope = NULL_TREE;
4345           parser->object_scope = NULL_TREE;
4346         }
4347       if (scope && name && BASELINK_P (name))
4348         adjust_result_of_qualified_name_lookup
4349           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4350       postfix_expression
4351         = finish_class_member_access_expr (postfix_expression, name);
4352     }
4353   /* Otherwise, try the pseudo-destructor-name production.  */
4354   else
4355     {
4356       tree s = NULL_TREE;
4357       tree type;
4358
4359       /* Parse the pseudo-destructor-name.  */
4360       cp_parser_pseudo_destructor_name (parser, &s, &type);
4361       /* Form the call.  */
4362       postfix_expression
4363         = finish_pseudo_destructor_expr (postfix_expression,
4364                                          s, TREE_TYPE (type));
4365     }
4366
4367   /* We no longer need to look up names in the scope of the object on
4368      the left-hand side of the `.' or `->' operator.  */
4369   parser->context->object_type = NULL_TREE;
4370
4371   /* Outside of offsetof, these operators may not appear in
4372      constant-expressions.  */
4373   if (!for_offsetof
4374       && (cp_parser_non_integral_constant_expression
4375           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4376     postfix_expression = error_mark_node;
4377
4378   return postfix_expression;
4379 }
4380
4381 /* Parse a parenthesized expression-list.
4382
4383    expression-list:
4384      assignment-expression
4385      expression-list, assignment-expression
4386
4387    attribute-list:
4388      expression-list
4389      identifier
4390      identifier, expression-list
4391
4392    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4393    representation of an assignment-expression.  Note that a TREE_LIST
4394    is returned even if there is only a single expression in the list.
4395    error_mark_node is returned if the ( and or ) are
4396    missing. NULL_TREE is returned on no expressions. The parentheses
4397    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4398    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4399    indicates whether or not all of the expressions in the list were
4400    constant.  */
4401
4402 static tree
4403 cp_parser_parenthesized_expression_list (cp_parser* parser,
4404                                          bool is_attribute_list,
4405                                          bool *non_constant_p)
4406 {
4407   tree expression_list = NULL_TREE;
4408   tree identifier = NULL_TREE;
4409
4410   /* Assume all the expressions will be constant.  */
4411   if (non_constant_p)
4412     *non_constant_p = false;
4413
4414   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4415     return error_mark_node;
4416
4417   /* Consume expressions until there are no more.  */
4418   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4419     while (true)
4420       {
4421         tree expr;
4422
4423         /* At the beginning of attribute lists, check to see if the
4424            next token is an identifier.  */
4425         if (is_attribute_list
4426             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4427           {
4428             cp_token *token;
4429
4430             /* Consume the identifier.  */
4431             token = cp_lexer_consume_token (parser->lexer);
4432             /* Save the identifier.  */
4433             identifier = token->value;
4434           }
4435         else
4436           {
4437             /* Parse the next assignment-expression.  */
4438             if (non_constant_p)
4439               {
4440                 bool expr_non_constant_p;
4441                 expr = (cp_parser_constant_expression
4442                         (parser, /*allow_non_constant_p=*/true,
4443                          &expr_non_constant_p));
4444                 if (expr_non_constant_p)
4445                   *non_constant_p = true;
4446               }
4447             else
4448               expr = cp_parser_assignment_expression (parser);
4449
4450              /* Add it to the list.  We add error_mark_node
4451                 expressions to the list, so that we can still tell if
4452                 the correct form for a parenthesized expression-list
4453                 is found. That gives better errors.  */
4454             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4455
4456             if (expr == error_mark_node)
4457               goto skip_comma;
4458           }
4459
4460         /* After the first item, attribute lists look the same as
4461            expression lists.  */
4462         is_attribute_list = false;
4463
4464       get_comma:;
4465         /* If the next token isn't a `,', then we are done.  */
4466         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4467           break;
4468
4469         /* Otherwise, consume the `,' and keep going.  */
4470         cp_lexer_consume_token (parser->lexer);
4471       }
4472
4473   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4474     {
4475       int ending;
4476
4477     skip_comma:;
4478       /* We try and resync to an unnested comma, as that will give the
4479          user better diagnostics.  */
4480       ending = cp_parser_skip_to_closing_parenthesis (parser,
4481                                                       /*recovering=*/true,
4482                                                       /*or_comma=*/true,
4483                                                       /*consume_paren=*/true);
4484       if (ending < 0)
4485         goto get_comma;
4486       if (!ending)
4487         return error_mark_node;
4488     }
4489
4490   /* We built up the list in reverse order so we must reverse it now.  */
4491   expression_list = nreverse (expression_list);
4492   if (identifier)
4493     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4494
4495   return expression_list;
4496 }
4497
4498 /* Parse a pseudo-destructor-name.
4499
4500    pseudo-destructor-name:
4501      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4502      :: [opt] nested-name-specifier template template-id :: ~ type-name
4503      :: [opt] nested-name-specifier [opt] ~ type-name
4504
4505    If either of the first two productions is used, sets *SCOPE to the
4506    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4507    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4508    or ERROR_MARK_NODE if the parse fails.  */
4509
4510 static void
4511 cp_parser_pseudo_destructor_name (cp_parser* parser,
4512                                   tree* scope,
4513                                   tree* type)
4514 {
4515   bool nested_name_specifier_p;
4516
4517   /* Assume that things will not work out.  */
4518   *type = error_mark_node;
4519
4520   /* Look for the optional `::' operator.  */
4521   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4522   /* Look for the optional nested-name-specifier.  */
4523   nested_name_specifier_p
4524     = (cp_parser_nested_name_specifier_opt (parser,
4525                                             /*typename_keyword_p=*/false,
4526                                             /*check_dependency_p=*/true,
4527                                             /*type_p=*/false,
4528                                             /*is_declaration=*/true)
4529        != NULL_TREE);
4530   /* Now, if we saw a nested-name-specifier, we might be doing the
4531      second production.  */
4532   if (nested_name_specifier_p
4533       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4534     {
4535       /* Consume the `template' keyword.  */
4536       cp_lexer_consume_token (parser->lexer);
4537       /* Parse the template-id.  */
4538       cp_parser_template_id (parser,
4539                              /*template_keyword_p=*/true,
4540                              /*check_dependency_p=*/false,
4541                              /*is_declaration=*/true);
4542       /* Look for the `::' token.  */
4543       cp_parser_require (parser, CPP_SCOPE, "`::'");
4544     }
4545   /* If the next token is not a `~', then there might be some
4546      additional qualification.  */
4547   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4548     {
4549       /* Look for the type-name.  */
4550       *scope = TREE_TYPE (cp_parser_type_name (parser));
4551
4552       if (*scope == error_mark_node)
4553         return;
4554
4555       /* If we don't have ::~, then something has gone wrong.  Since
4556          the only caller of this function is looking for something
4557          after `.' or `->' after a scalar type, most likely the
4558          program is trying to get a member of a non-aggregate
4559          type.  */
4560       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4561           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4562         {
4563           cp_parser_error (parser, "request for member of non-aggregate type");
4564           return;
4565         }
4566
4567       /* Look for the `::' token.  */
4568       cp_parser_require (parser, CPP_SCOPE, "`::'");
4569     }
4570   else
4571     *scope = NULL_TREE;
4572
4573   /* Look for the `~'.  */
4574   cp_parser_require (parser, CPP_COMPL, "`~'");
4575   /* Look for the type-name again.  We are not responsible for
4576      checking that it matches the first type-name.  */
4577   *type = cp_parser_type_name (parser);
4578 }
4579
4580 /* Parse a unary-expression.
4581
4582    unary-expression:
4583      postfix-expression
4584      ++ cast-expression
4585      -- cast-expression
4586      unary-operator cast-expression
4587      sizeof unary-expression
4588      sizeof ( type-id )
4589      new-expression
4590      delete-expression
4591
4592    GNU Extensions:
4593
4594    unary-expression:
4595      __extension__ cast-expression
4596      __alignof__ unary-expression
4597      __alignof__ ( type-id )
4598      __real__ cast-expression
4599      __imag__ cast-expression
4600      && identifier
4601
4602    ADDRESS_P is true iff the unary-expression is appearing as the
4603    operand of the `&' operator.
4604
4605    Returns a representation of the expression.  */
4606
4607 static tree
4608 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4609 {
4610   cp_token *token;
4611   enum tree_code unary_operator;
4612
4613   /* Peek at the next token.  */
4614   token = cp_lexer_peek_token (parser->lexer);
4615   /* Some keywords give away the kind of expression.  */
4616   if (token->type == CPP_KEYWORD)
4617     {
4618       enum rid keyword = token->keyword;
4619
4620       switch (keyword)
4621         {
4622         case RID_ALIGNOF:
4623         case RID_SIZEOF:
4624           {
4625             tree operand;
4626             enum tree_code op;
4627
4628             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4629             /* Consume the token.  */
4630             cp_lexer_consume_token (parser->lexer);
4631             /* Parse the operand.  */
4632             operand = cp_parser_sizeof_operand (parser, keyword);
4633
4634             if (TYPE_P (operand))
4635               return cxx_sizeof_or_alignof_type (operand, op, true);
4636             else
4637               return cxx_sizeof_or_alignof_expr (operand, op);
4638           }
4639
4640         case RID_NEW:
4641           return cp_parser_new_expression (parser);
4642
4643         case RID_DELETE:
4644           return cp_parser_delete_expression (parser);
4645
4646         case RID_EXTENSION:
4647           {
4648             /* The saved value of the PEDANTIC flag.  */
4649             int saved_pedantic;
4650             tree expr;
4651
4652             /* Save away the PEDANTIC flag.  */
4653             cp_parser_extension_opt (parser, &saved_pedantic);
4654             /* Parse the cast-expression.  */
4655             expr = cp_parser_simple_cast_expression (parser);
4656             /* Restore the PEDANTIC flag.  */
4657             pedantic = saved_pedantic;
4658
4659             return expr;
4660           }
4661
4662         case RID_REALPART:
4663         case RID_IMAGPART:
4664           {
4665             tree expression;
4666
4667             /* Consume the `__real__' or `__imag__' token.  */
4668             cp_lexer_consume_token (parser->lexer);
4669             /* Parse the cast-expression.  */
4670             expression = cp_parser_simple_cast_expression (parser);
4671             /* Create the complete representation.  */
4672             return build_x_unary_op ((keyword == RID_REALPART
4673                                       ? REALPART_EXPR : IMAGPART_EXPR),
4674                                      expression);
4675           }
4676           break;
4677
4678         default:
4679           break;
4680         }
4681     }
4682
4683   /* Look for the `:: new' and `:: delete', which also signal the
4684      beginning of a new-expression, or delete-expression,
4685      respectively.  If the next token is `::', then it might be one of
4686      these.  */
4687   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4688     {
4689       enum rid keyword;
4690
4691       /* See if the token after the `::' is one of the keywords in
4692          which we're interested.  */
4693       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4694       /* If it's `new', we have a new-expression.  */
4695       if (keyword == RID_NEW)
4696         return cp_parser_new_expression (parser);
4697       /* Similarly, for `delete'.  */
4698       else if (keyword == RID_DELETE)
4699         return cp_parser_delete_expression (parser);
4700     }
4701
4702   /* Look for a unary operator.  */
4703   unary_operator = cp_parser_unary_operator (token);
4704   /* The `++' and `--' operators can be handled similarly, even though
4705      they are not technically unary-operators in the grammar.  */
4706   if (unary_operator == ERROR_MARK)
4707     {
4708       if (token->type == CPP_PLUS_PLUS)
4709         unary_operator = PREINCREMENT_EXPR;
4710       else if (token->type == CPP_MINUS_MINUS)
4711         unary_operator = PREDECREMENT_EXPR;
4712       /* Handle the GNU address-of-label extension.  */
4713       else if (cp_parser_allow_gnu_extensions_p (parser)
4714                && token->type == CPP_AND_AND)
4715         {
4716           tree identifier;
4717
4718           /* Consume the '&&' token.  */
4719           cp_lexer_consume_token (parser->lexer);
4720           /* Look for the identifier.  */
4721           identifier = cp_parser_identifier (parser);
4722           /* Create an expression representing the address.  */
4723           return finish_label_address_expr (identifier);
4724         }
4725     }
4726   if (unary_operator != ERROR_MARK)
4727     {
4728       tree cast_expression;
4729       tree expression = error_mark_node;
4730       const char *non_constant_p = NULL;
4731
4732       /* Consume the operator token.  */
4733       token = cp_lexer_consume_token (parser->lexer);
4734       /* Parse the cast-expression.  */
4735       cast_expression
4736         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4737       /* Now, build an appropriate representation.  */
4738       switch (unary_operator)
4739         {
4740         case INDIRECT_REF:
4741           non_constant_p = "`*'";
4742           expression = build_x_indirect_ref (cast_expression, "unary *");
4743           break;
4744
4745         case ADDR_EXPR:
4746           non_constant_p = "`&'";
4747           /* Fall through.  */
4748         case BIT_NOT_EXPR:
4749           expression = build_x_unary_op (unary_operator, cast_expression);
4750           break;
4751
4752         case PREINCREMENT_EXPR:
4753         case PREDECREMENT_EXPR:
4754           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4755                             ? "`++'" : "`--'");
4756           /* Fall through.  */
4757         case CONVERT_EXPR:
4758         case NEGATE_EXPR:
4759         case TRUTH_NOT_EXPR:
4760           expression = finish_unary_op_expr (unary_operator, cast_expression);
4761           break;
4762
4763         default:
4764           gcc_unreachable ();
4765         }
4766
4767       if (non_constant_p
4768           && cp_parser_non_integral_constant_expression (parser,
4769                                                          non_constant_p))
4770         expression = error_mark_node;
4771
4772       return expression;
4773     }
4774
4775   return cp_parser_postfix_expression (parser, address_p);
4776 }
4777
4778 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4779    unary-operator, the corresponding tree code is returned.  */
4780
4781 static enum tree_code
4782 cp_parser_unary_operator (cp_token* token)
4783 {
4784   switch (token->type)
4785     {
4786     case CPP_MULT:
4787       return INDIRECT_REF;
4788
4789     case CPP_AND:
4790       return ADDR_EXPR;
4791
4792     case CPP_PLUS:
4793       return CONVERT_EXPR;
4794
4795     case CPP_MINUS:
4796       return NEGATE_EXPR;
4797
4798     case CPP_NOT:
4799       return TRUTH_NOT_EXPR;
4800
4801     case CPP_COMPL:
4802       return BIT_NOT_EXPR;
4803
4804     default:
4805       return ERROR_MARK;
4806     }
4807 }
4808
4809 /* Parse a new-expression.
4810
4811    new-expression:
4812      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4813      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4814
4815    Returns a representation of the expression.  */
4816
4817 static tree
4818 cp_parser_new_expression (cp_parser* parser)
4819 {
4820   bool global_scope_p;
4821   tree placement;
4822   tree type;
4823   tree initializer;
4824   tree nelts;
4825
4826   /* Look for the optional `::' operator.  */
4827   global_scope_p
4828     = (cp_parser_global_scope_opt (parser,
4829                                    /*current_scope_valid_p=*/false)
4830        != NULL_TREE);
4831   /* Look for the `new' operator.  */
4832   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4833   /* There's no easy way to tell a new-placement from the
4834      `( type-id )' construct.  */
4835   cp_parser_parse_tentatively (parser);
4836   /* Look for a new-placement.  */
4837   placement = cp_parser_new_placement (parser);
4838   /* If that didn't work out, there's no new-placement.  */
4839   if (!cp_parser_parse_definitely (parser))
4840     placement = NULL_TREE;
4841
4842   /* If the next token is a `(', then we have a parenthesized
4843      type-id.  */
4844   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4845     {
4846       /* Consume the `('.  */
4847       cp_lexer_consume_token (parser->lexer);
4848       /* Parse the type-id.  */
4849       type = cp_parser_type_id (parser);
4850       /* Look for the closing `)'.  */
4851       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4852       /* There should not be a direct-new-declarator in this production,
4853          but GCC used to allowed this, so we check and emit a sensible error
4854          message for this case.  */
4855       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4856         {
4857           error ("array bound forbidden after parenthesized type-id");
4858           inform ("try removing the parentheses around the type-id");
4859           cp_parser_direct_new_declarator (parser);
4860         }
4861       nelts = integer_one_node;
4862     }
4863   /* Otherwise, there must be a new-type-id.  */
4864   else
4865     type = cp_parser_new_type_id (parser, &nelts);
4866
4867   /* If the next token is a `(', then we have a new-initializer.  */
4868   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4869     initializer = cp_parser_new_initializer (parser);
4870   else
4871     initializer = NULL_TREE;
4872
4873   /* A new-expression may not appear in an integral constant
4874      expression.  */
4875   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4876     return error_mark_node;
4877
4878   /* Create a representation of the new-expression.  */
4879   return build_new (placement, type, nelts, initializer, global_scope_p);
4880 }
4881
4882 /* Parse a new-placement.
4883
4884    new-placement:
4885      ( expression-list )
4886
4887    Returns the same representation as for an expression-list.  */
4888
4889 static tree
4890 cp_parser_new_placement (cp_parser* parser)
4891 {
4892   tree expression_list;
4893
4894   /* Parse the expression-list.  */
4895   expression_list = (cp_parser_parenthesized_expression_list
4896                      (parser, false, /*non_constant_p=*/NULL));
4897
4898   return expression_list;
4899 }
4900
4901 /* Parse a new-type-id.
4902
4903    new-type-id:
4904      type-specifier-seq new-declarator [opt]
4905
4906    Returns the TYPE allocated.  If the new-type-id indicates an array
4907    type, *NELTS is set to the number of elements in the last array
4908    bound; the TYPE will not include the last array bound.  */
4909
4910 static tree
4911 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4912 {
4913   cp_decl_specifier_seq type_specifier_seq;
4914   cp_declarator *new_declarator;
4915   cp_declarator *declarator;
4916   cp_declarator *outer_declarator;
4917   const char *saved_message;
4918   tree type;
4919
4920   /* The type-specifier sequence must not contain type definitions.
4921      (It cannot contain declarations of new types either, but if they
4922      are not definitions we will catch that because they are not
4923      complete.)  */
4924   saved_message = parser->type_definition_forbidden_message;
4925   parser->type_definition_forbidden_message
4926     = "types may not be defined in a new-type-id";
4927   /* Parse the type-specifier-seq.  */
4928   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4929   /* Restore the old message.  */
4930   parser->type_definition_forbidden_message = saved_message;
4931   /* Parse the new-declarator.  */
4932   new_declarator = cp_parser_new_declarator_opt (parser);
4933
4934   /* Determine the number of elements in the last array dimension, if
4935      any.  */
4936   *nelts = NULL_TREE;
4937   /* Skip down to the last array dimension.  */
4938   declarator = new_declarator;
4939   outer_declarator = NULL;
4940   while (declarator && (declarator->kind == cdk_pointer
4941                         || declarator->kind == cdk_ptrmem))
4942     {
4943       outer_declarator = declarator;
4944       declarator = declarator->declarator;
4945     }
4946   while (declarator
4947          && declarator->kind == cdk_array
4948          && declarator->declarator
4949          && declarator->declarator->kind == cdk_array)
4950     {
4951       outer_declarator = declarator;
4952       declarator = declarator->declarator;
4953     }
4954
4955   if (declarator && declarator->kind == cdk_array)
4956     {
4957       *nelts = declarator->u.array.bounds;
4958       if (*nelts == error_mark_node)
4959         *nelts = integer_one_node;
4960       else if (!processing_template_decl)
4961         {
4962           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4963                                            false))
4964             pedwarn ("size in array new must have integral type");
4965           *nelts = save_expr (cp_convert (sizetype, *nelts));
4966           if (*nelts == integer_zero_node)
4967             warning ("zero size array reserves no space");
4968         }
4969       if (outer_declarator)
4970         outer_declarator->declarator = declarator->declarator;
4971       else
4972         new_declarator = NULL;
4973     }
4974
4975   type = groktypename (&type_specifier_seq, new_declarator);
4976   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4977     {
4978       *nelts = array_type_nelts_top (type);
4979       type = TREE_TYPE (type);
4980     }
4981   return type;
4982 }
4983
4984 /* Parse an (optional) new-declarator.
4985
4986    new-declarator:
4987      ptr-operator new-declarator [opt]
4988      direct-new-declarator
4989
4990    Returns the declarator.  */
4991
4992 static cp_declarator *
4993 cp_parser_new_declarator_opt (cp_parser* parser)
4994 {
4995   enum tree_code code;
4996   tree type;
4997   cp_cv_quals cv_quals;
4998
4999   /* We don't know if there's a ptr-operator next, or not.  */
5000   cp_parser_parse_tentatively (parser);
5001   /* Look for a ptr-operator.  */
5002   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5003   /* If that worked, look for more new-declarators.  */
5004   if (cp_parser_parse_definitely (parser))
5005     {
5006       cp_declarator *declarator;
5007
5008       /* Parse another optional declarator.  */
5009       declarator = cp_parser_new_declarator_opt (parser);
5010
5011       /* Create the representation of the declarator.  */
5012       if (type)
5013         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5014       else if (code == INDIRECT_REF)
5015         declarator = make_pointer_declarator (cv_quals, declarator);
5016       else
5017         declarator = make_reference_declarator (cv_quals, declarator);
5018
5019       return declarator;
5020     }
5021
5022   /* If the next token is a `[', there is a direct-new-declarator.  */
5023   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5024     return cp_parser_direct_new_declarator (parser);
5025
5026   return NULL;
5027 }
5028
5029 /* Parse a direct-new-declarator.
5030
5031    direct-new-declarator:
5032      [ expression ]
5033      direct-new-declarator [constant-expression]
5034
5035    */
5036
5037 static cp_declarator *
5038 cp_parser_direct_new_declarator (cp_parser* parser)
5039 {
5040   cp_declarator *declarator = NULL;
5041
5042   while (true)
5043     {
5044       tree expression;
5045
5046       /* Look for the opening `['.  */
5047       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5048       /* The first expression is not required to be constant.  */
5049       if (!declarator)
5050         {
5051           expression = cp_parser_expression (parser);
5052           /* The standard requires that the expression have integral
5053              type.  DR 74 adds enumeration types.  We believe that the
5054              real intent is that these expressions be handled like the
5055              expression in a `switch' condition, which also allows
5056              classes with a single conversion to integral or
5057              enumeration type.  */
5058           if (!processing_template_decl)
5059             {
5060               expression
5061                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5062                                               expression,
5063                                               /*complain=*/true);
5064               if (!expression)
5065                 {
5066                   error ("expression in new-declarator must have integral or enumeration type");
5067                   expression = error_mark_node;
5068                 }
5069             }
5070         }
5071       /* But all the other expressions must be.  */
5072       else
5073         expression
5074           = cp_parser_constant_expression (parser,
5075                                            /*allow_non_constant=*/false,
5076                                            NULL);
5077       /* Look for the closing `]'.  */
5078       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5079
5080       /* Add this bound to the declarator.  */
5081       declarator = make_array_declarator (declarator, expression);
5082
5083       /* If the next token is not a `[', then there are no more
5084          bounds.  */
5085       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5086         break;
5087     }
5088
5089   return declarator;
5090 }
5091
5092 /* Parse a new-initializer.
5093
5094    new-initializer:
5095      ( expression-list [opt] )
5096
5097    Returns a representation of the expression-list.  If there is no
5098    expression-list, VOID_ZERO_NODE is returned.  */
5099
5100 static tree
5101 cp_parser_new_initializer (cp_parser* parser)
5102 {
5103   tree expression_list;
5104
5105   expression_list = (cp_parser_parenthesized_expression_list
5106                      (parser, false, /*non_constant_p=*/NULL));
5107   if (!expression_list)
5108     expression_list = void_zero_node;
5109
5110   return expression_list;
5111 }
5112
5113 /* Parse a delete-expression.
5114
5115    delete-expression:
5116      :: [opt] delete cast-expression
5117      :: [opt] delete [ ] cast-expression
5118
5119    Returns a representation of the expression.  */
5120
5121 static tree
5122 cp_parser_delete_expression (cp_parser* parser)
5123 {
5124   bool global_scope_p;
5125   bool array_p;
5126   tree expression;
5127
5128   /* Look for the optional `::' operator.  */
5129   global_scope_p
5130     = (cp_parser_global_scope_opt (parser,
5131                                    /*current_scope_valid_p=*/false)
5132        != NULL_TREE);
5133   /* Look for the `delete' keyword.  */
5134   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5135   /* See if the array syntax is in use.  */
5136   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5137     {
5138       /* Consume the `[' token.  */
5139       cp_lexer_consume_token (parser->lexer);
5140       /* Look for the `]' token.  */
5141       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5142       /* Remember that this is the `[]' construct.  */
5143       array_p = true;
5144     }
5145   else
5146     array_p = false;
5147
5148   /* Parse the cast-expression.  */
5149   expression = cp_parser_simple_cast_expression (parser);
5150
5151   /* A delete-expression may not appear in an integral constant
5152      expression.  */
5153   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5154     return error_mark_node;
5155
5156   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5157 }
5158
5159 /* Parse a cast-expression.
5160
5161    cast-expression:
5162      unary-expression
5163      ( type-id ) cast-expression
5164
5165    Returns a representation of the expression.  */
5166
5167 static tree
5168 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5169 {
5170   /* If it's a `(', then we might be looking at a cast.  */
5171   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5172     {
5173       tree type = NULL_TREE;
5174       tree expr = NULL_TREE;
5175       bool compound_literal_p;
5176       const char *saved_message;
5177
5178       /* There's no way to know yet whether or not this is a cast.
5179          For example, `(int (3))' is a unary-expression, while `(int)
5180          3' is a cast.  So, we resort to parsing tentatively.  */
5181       cp_parser_parse_tentatively (parser);
5182       /* Types may not be defined in a cast.  */
5183       saved_message = parser->type_definition_forbidden_message;
5184       parser->type_definition_forbidden_message
5185         = "types may not be defined in casts";
5186       /* Consume the `('.  */
5187       cp_lexer_consume_token (parser->lexer);
5188       /* A very tricky bit is that `(struct S) { 3 }' is a
5189          compound-literal (which we permit in C++ as an extension).
5190          But, that construct is not a cast-expression -- it is a
5191          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5192          is legal; if the compound-literal were a cast-expression,
5193          you'd need an extra set of parentheses.)  But, if we parse
5194          the type-id, and it happens to be a class-specifier, then we
5195          will commit to the parse at that point, because we cannot
5196          undo the action that is done when creating a new class.  So,
5197          then we cannot back up and do a postfix-expression.
5198
5199          Therefore, we scan ahead to the closing `)', and check to see
5200          if the token after the `)' is a `{'.  If so, we are not
5201          looking at a cast-expression.
5202
5203          Save tokens so that we can put them back.  */
5204       cp_lexer_save_tokens (parser->lexer);
5205       /* Skip tokens until the next token is a closing parenthesis.
5206          If we find the closing `)', and the next token is a `{', then
5207          we are looking at a compound-literal.  */
5208       compound_literal_p
5209         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5210                                                   /*consume_paren=*/true)
5211            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5212       /* Roll back the tokens we skipped.  */
5213       cp_lexer_rollback_tokens (parser->lexer);
5214       /* If we were looking at a compound-literal, simulate an error
5215          so that the call to cp_parser_parse_definitely below will
5216          fail.  */
5217       if (compound_literal_p)
5218         cp_parser_simulate_error (parser);
5219       else
5220         {
5221           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5222           parser->in_type_id_in_expr_p = true;
5223           /* Look for the type-id.  */
5224           type = cp_parser_type_id (parser);
5225           /* Look for the closing `)'.  */
5226           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5227           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5228         }
5229
5230       /* Restore the saved message.  */
5231       parser->type_definition_forbidden_message = saved_message;
5232
5233       /* If ok so far, parse the dependent expression. We cannot be
5234          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5235          ctor of T, but looks like a cast to function returning T
5236          without a dependent expression.  */
5237       if (!cp_parser_error_occurred (parser))
5238         expr = cp_parser_simple_cast_expression (parser);
5239
5240       if (cp_parser_parse_definitely (parser))
5241         {
5242           /* Warn about old-style casts, if so requested.  */
5243           if (warn_old_style_cast
5244               && !in_system_header
5245               && !VOID_TYPE_P (type)
5246               && current_lang_name != lang_name_c)
5247             warning ("use of old-style cast");
5248
5249           /* Only type conversions to integral or enumeration types
5250              can be used in constant-expressions.  */
5251           if (parser->integral_constant_expression_p
5252               && !dependent_type_p (type)
5253               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5254               && (cp_parser_non_integral_constant_expression
5255                   (parser,
5256                    "a cast to a type other than an integral or "
5257                    "enumeration type")))
5258             return error_mark_node;
5259
5260           /* Perform the cast.  */
5261           expr = build_c_cast (type, expr);
5262           return expr;
5263         }
5264     }
5265
5266   /* If we get here, then it's not a cast, so it must be a
5267      unary-expression.  */
5268   return cp_parser_unary_expression (parser, address_p);
5269 }
5270
5271 /* Parse a pm-expression.
5272
5273    pm-expression:
5274      cast-expression
5275      pm-expression .* cast-expression
5276      pm-expression ->* cast-expression
5277
5278      Returns a representation of the expression.  */
5279
5280 static tree
5281 cp_parser_pm_expression (cp_parser* parser)
5282 {
5283   static const cp_parser_token_tree_map map = {
5284     { CPP_DEREF_STAR, MEMBER_REF },
5285     { CPP_DOT_STAR, DOTSTAR_EXPR },
5286     { CPP_EOF, ERROR_MARK }
5287   };
5288
5289   return cp_parser_binary_expression (parser, map,
5290                                       cp_parser_simple_cast_expression);
5291 }
5292
5293 /* Parse a multiplicative-expression.
5294
5295    multiplicative-expression:
5296      pm-expression
5297      multiplicative-expression * pm-expression
5298      multiplicative-expression / pm-expression
5299      multiplicative-expression % pm-expression
5300
5301    Returns a representation of the expression.  */
5302
5303 static tree
5304 cp_parser_multiplicative_expression (cp_parser* parser)
5305 {
5306   static const cp_parser_token_tree_map map = {
5307     { CPP_MULT, MULT_EXPR },
5308     { CPP_DIV, TRUNC_DIV_EXPR },
5309     { CPP_MOD, TRUNC_MOD_EXPR },
5310     { CPP_EOF, ERROR_MARK }
5311   };
5312
5313   return cp_parser_binary_expression (parser,
5314                                       map,
5315                                       cp_parser_pm_expression);
5316 }
5317
5318 /* Parse an additive-expression.
5319
5320    additive-expression:
5321      multiplicative-expression
5322      additive-expression + multiplicative-expression
5323      additive-expression - multiplicative-expression
5324
5325    Returns a representation of the expression.  */
5326
5327 static tree
5328 cp_parser_additive_expression (cp_parser* parser)
5329 {
5330   static const cp_parser_token_tree_map map = {
5331     { CPP_PLUS, PLUS_EXPR },
5332     { CPP_MINUS, MINUS_EXPR },
5333     { CPP_EOF, ERROR_MARK }
5334   };
5335
5336   return cp_parser_binary_expression (parser,
5337                                       map,
5338                                       cp_parser_multiplicative_expression);
5339 }
5340
5341 /* Parse a shift-expression.
5342
5343    shift-expression:
5344      additive-expression
5345      shift-expression << additive-expression
5346      shift-expression >> additive-expression
5347
5348    Returns a representation of the expression.  */
5349
5350 static tree
5351 cp_parser_shift_expression (cp_parser* parser)
5352 {
5353   static const cp_parser_token_tree_map map = {
5354     { CPP_LSHIFT, LSHIFT_EXPR },
5355     { CPP_RSHIFT, RSHIFT_EXPR },
5356     { CPP_EOF, ERROR_MARK }
5357   };
5358
5359   return cp_parser_binary_expression (parser,
5360                                       map,
5361                                       cp_parser_additive_expression);
5362 }
5363
5364 /* Parse a relational-expression.
5365
5366    relational-expression:
5367      shift-expression
5368      relational-expression < shift-expression
5369      relational-expression > shift-expression
5370      relational-expression <= shift-expression
5371      relational-expression >= shift-expression
5372
5373    GNU Extension:
5374
5375    relational-expression:
5376      relational-expression <? shift-expression
5377      relational-expression >? shift-expression
5378
5379    Returns a representation of the expression.  */
5380
5381 static tree
5382 cp_parser_relational_expression (cp_parser* parser)
5383 {
5384   static const cp_parser_token_tree_map map = {
5385     { CPP_LESS, LT_EXPR },
5386     { CPP_GREATER, GT_EXPR },
5387     { CPP_LESS_EQ, LE_EXPR },
5388     { CPP_GREATER_EQ, GE_EXPR },
5389     { CPP_MIN, MIN_EXPR },
5390     { CPP_MAX, MAX_EXPR },
5391     { CPP_EOF, ERROR_MARK }
5392   };
5393
5394   return cp_parser_binary_expression (parser,
5395                                       map,
5396                                       cp_parser_shift_expression);
5397 }
5398
5399 /* Parse an equality-expression.
5400
5401    equality-expression:
5402      relational-expression
5403      equality-expression == relational-expression
5404      equality-expression != relational-expression
5405
5406    Returns a representation of the expression.  */
5407
5408 static tree
5409 cp_parser_equality_expression (cp_parser* parser)
5410 {
5411   static const cp_parser_token_tree_map map = {
5412     { CPP_EQ_EQ, EQ_EXPR },
5413     { CPP_NOT_EQ, NE_EXPR },
5414     { CPP_EOF, ERROR_MARK }
5415   };
5416
5417   return cp_parser_binary_expression (parser,
5418                                       map,
5419                                       cp_parser_relational_expression);
5420 }
5421
5422 /* Parse an and-expression.
5423
5424    and-expression:
5425      equality-expression
5426      and-expression & equality-expression
5427
5428    Returns a representation of the expression.  */
5429
5430 static tree
5431 cp_parser_and_expression (cp_parser* parser)
5432 {
5433   static const cp_parser_token_tree_map map = {
5434     { CPP_AND, BIT_AND_EXPR },
5435     { CPP_EOF, ERROR_MARK }
5436   };
5437
5438   return cp_parser_binary_expression (parser,
5439                                       map,
5440                                       cp_parser_equality_expression);
5441 }
5442
5443 /* Parse an exclusive-or-expression.
5444
5445    exclusive-or-expression:
5446      and-expression
5447      exclusive-or-expression ^ and-expression
5448
5449    Returns a representation of the expression.  */
5450
5451 static tree
5452 cp_parser_exclusive_or_expression (cp_parser* parser)
5453 {
5454   static const cp_parser_token_tree_map map = {
5455     { CPP_XOR, BIT_XOR_EXPR },
5456     { CPP_EOF, ERROR_MARK }
5457   };
5458
5459   return cp_parser_binary_expression (parser,
5460                                       map,
5461                                       cp_parser_and_expression);
5462 }
5463
5464
5465 /* Parse an inclusive-or-expression.
5466
5467    inclusive-or-expression:
5468      exclusive-or-expression
5469      inclusive-or-expression | exclusive-or-expression
5470
5471    Returns a representation of the expression.  */
5472
5473 static tree
5474 cp_parser_inclusive_or_expression (cp_parser* parser)
5475 {
5476   static const cp_parser_token_tree_map map = {
5477     { CPP_OR, BIT_IOR_EXPR },
5478     { CPP_EOF, ERROR_MARK }
5479   };
5480
5481   return cp_parser_binary_expression (parser,
5482                                       map,
5483                                       cp_parser_exclusive_or_expression);
5484 }
5485
5486 /* Parse a logical-and-expression.
5487
5488    logical-and-expression:
5489      inclusive-or-expression
5490      logical-and-expression && inclusive-or-expression
5491
5492    Returns a representation of the expression.  */
5493
5494 static tree
5495 cp_parser_logical_and_expression (cp_parser* parser)
5496 {
5497   static const cp_parser_token_tree_map map = {
5498     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5499     { CPP_EOF, ERROR_MARK }
5500   };
5501
5502   return cp_parser_binary_expression (parser,
5503                                       map,
5504                                       cp_parser_inclusive_or_expression);
5505 }
5506
5507 /* Parse a logical-or-expression.
5508
5509    logical-or-expression:
5510      logical-and-expression
5511      logical-or-expression || logical-and-expression
5512
5513    Returns a representation of the expression.  */
5514
5515 static tree
5516 cp_parser_logical_or_expression (cp_parser* parser)
5517 {
5518   static const cp_parser_token_tree_map map = {
5519     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5520     { CPP_EOF, ERROR_MARK }
5521   };
5522
5523   return cp_parser_binary_expression (parser,
5524                                       map,
5525                                       cp_parser_logical_and_expression);
5526 }
5527
5528 /* Parse the `? expression : assignment-expression' part of a
5529    conditional-expression.  The LOGICAL_OR_EXPR is the
5530    logical-or-expression that started the conditional-expression.
5531    Returns a representation of the entire conditional-expression.
5532
5533    This routine is used by cp_parser_assignment_expression.
5534
5535      ? expression : assignment-expression
5536
5537    GNU Extensions:
5538
5539      ? : assignment-expression */
5540
5541 static tree
5542 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5543 {
5544   tree expr;
5545   tree assignment_expr;
5546
5547   /* Consume the `?' token.  */
5548   cp_lexer_consume_token (parser->lexer);
5549   if (cp_parser_allow_gnu_extensions_p (parser)
5550       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5551     /* Implicit true clause.  */
5552     expr = NULL_TREE;
5553   else
5554     /* Parse the expression.  */
5555     expr = cp_parser_expression (parser);
5556
5557   /* The next token should be a `:'.  */
5558   cp_parser_require (parser, CPP_COLON, "`:'");
5559   /* Parse the assignment-expression.  */
5560   assignment_expr = cp_parser_assignment_expression (parser);
5561
5562   /* Build the conditional-expression.  */
5563   return build_x_conditional_expr (logical_or_expr,
5564                                    expr,
5565                                    assignment_expr);
5566 }
5567
5568 /* Parse an assignment-expression.
5569
5570    assignment-expression:
5571      conditional-expression
5572      logical-or-expression assignment-operator assignment_expression
5573      throw-expression
5574
5575    Returns a representation for the expression.  */
5576
5577 static tree
5578 cp_parser_assignment_expression (cp_parser* parser)
5579 {
5580   tree expr;
5581
5582   /* If the next token is the `throw' keyword, then we're looking at
5583      a throw-expression.  */
5584   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5585     expr = cp_parser_throw_expression (parser);
5586   /* Otherwise, it must be that we are looking at a
5587      logical-or-expression.  */
5588   else
5589     {
5590       /* Parse the logical-or-expression.  */
5591       expr = cp_parser_logical_or_expression (parser);
5592       /* If the next token is a `?' then we're actually looking at a
5593          conditional-expression.  */
5594       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5595         return cp_parser_question_colon_clause (parser, expr);
5596       else
5597         {
5598           enum tree_code assignment_operator;
5599
5600           /* If it's an assignment-operator, we're using the second
5601              production.  */
5602           assignment_operator
5603             = cp_parser_assignment_operator_opt (parser);
5604           if (assignment_operator != ERROR_MARK)
5605             {
5606               tree rhs;
5607
5608               /* Parse the right-hand side of the assignment.  */
5609               rhs = cp_parser_assignment_expression (parser);
5610               /* An assignment may not appear in a
5611                  constant-expression.  */
5612               if (cp_parser_non_integral_constant_expression (parser,
5613                                                               "an assignment"))
5614                 return error_mark_node;
5615               /* Build the assignment expression.  */
5616               expr = build_x_modify_expr (expr,
5617                                           assignment_operator,
5618                                           rhs);
5619             }
5620         }
5621     }
5622
5623   return expr;
5624 }
5625
5626 /* Parse an (optional) assignment-operator.
5627
5628    assignment-operator: one of
5629      = *= /= %= += -= >>= <<= &= ^= |=
5630
5631    GNU Extension:
5632
5633    assignment-operator: one of
5634      <?= >?=
5635
5636    If the next token is an assignment operator, the corresponding tree
5637    code is returned, and the token is consumed.  For example, for
5638    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5639    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5640    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5641    operator, ERROR_MARK is returned.  */
5642
5643 static enum tree_code
5644 cp_parser_assignment_operator_opt (cp_parser* parser)
5645 {
5646   enum tree_code op;
5647   cp_token *token;
5648
5649   /* Peek at the next toen.  */
5650   token = cp_lexer_peek_token (parser->lexer);
5651
5652   switch (token->type)
5653     {
5654     case CPP_EQ:
5655       op = NOP_EXPR;
5656       break;
5657
5658     case CPP_MULT_EQ:
5659       op = MULT_EXPR;
5660       break;
5661
5662     case CPP_DIV_EQ:
5663       op = TRUNC_DIV_EXPR;
5664       break;
5665
5666     case CPP_MOD_EQ:
5667       op = TRUNC_MOD_EXPR;
5668       break;
5669
5670     case CPP_PLUS_EQ:
5671       op = PLUS_EXPR;
5672       break;
5673
5674     case CPP_MINUS_EQ:
5675       op = MINUS_EXPR;
5676       break;
5677
5678     case CPP_RSHIFT_EQ:
5679       op = RSHIFT_EXPR;
5680       break;
5681
5682     case CPP_LSHIFT_EQ:
5683       op = LSHIFT_EXPR;
5684       break;
5685
5686     case CPP_AND_EQ:
5687       op = BIT_AND_EXPR;
5688       break;
5689
5690     case CPP_XOR_EQ:
5691       op = BIT_XOR_EXPR;
5692       break;
5693
5694     case CPP_OR_EQ:
5695       op = BIT_IOR_EXPR;
5696       break;
5697
5698     case CPP_MIN_EQ:
5699       op = MIN_EXPR;
5700       break;
5701
5702     case CPP_MAX_EQ:
5703       op = MAX_EXPR;
5704       break;
5705
5706     default:
5707       /* Nothing else is an assignment operator.  */
5708       op = ERROR_MARK;
5709     }
5710
5711   /* If it was an assignment operator, consume it.  */
5712   if (op != ERROR_MARK)
5713     cp_lexer_consume_token (parser->lexer);
5714
5715   return op;
5716 }
5717
5718 /* Parse an expression.
5719
5720    expression:
5721      assignment-expression
5722      expression , assignment-expression
5723
5724    Returns a representation of the expression.  */
5725
5726 static tree
5727 cp_parser_expression (cp_parser* parser)
5728 {
5729   tree expression = NULL_TREE;
5730
5731   while (true)
5732     {
5733       tree assignment_expression;
5734
5735       /* Parse the next assignment-expression.  */
5736       assignment_expression
5737         = cp_parser_assignment_expression (parser);
5738       /* If this is the first assignment-expression, we can just
5739          save it away.  */
5740       if (!expression)
5741         expression = assignment_expression;
5742       else
5743         expression = build_x_compound_expr (expression,
5744                                             assignment_expression);
5745       /* If the next token is not a comma, then we are done with the
5746          expression.  */
5747       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5748         break;
5749       /* Consume the `,'.  */
5750       cp_lexer_consume_token (parser->lexer);
5751       /* A comma operator cannot appear in a constant-expression.  */
5752       if (cp_parser_non_integral_constant_expression (parser,
5753                                                       "a comma operator"))
5754         expression = error_mark_node;
5755     }
5756
5757   return expression;
5758 }
5759
5760 /* Parse a constant-expression.
5761
5762    constant-expression:
5763      conditional-expression
5764
5765   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5766   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5767   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5768   is false, NON_CONSTANT_P should be NULL.  */
5769
5770 static tree
5771 cp_parser_constant_expression (cp_parser* parser,
5772                                bool allow_non_constant_p,
5773                                bool *non_constant_p)
5774 {
5775   bool saved_integral_constant_expression_p;
5776   bool saved_allow_non_integral_constant_expression_p;
5777   bool saved_non_integral_constant_expression_p;
5778   tree expression;
5779
5780   /* It might seem that we could simply parse the
5781      conditional-expression, and then check to see if it were
5782      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5783      one that the compiler can figure out is constant, possibly after
5784      doing some simplifications or optimizations.  The standard has a
5785      precise definition of constant-expression, and we must honor
5786      that, even though it is somewhat more restrictive.
5787
5788      For example:
5789
5790        int i[(2, 3)];
5791
5792      is not a legal declaration, because `(2, 3)' is not a
5793      constant-expression.  The `,' operator is forbidden in a
5794      constant-expression.  However, GCC's constant-folding machinery
5795      will fold this operation to an INTEGER_CST for `3'.  */
5796
5797   /* Save the old settings.  */
5798   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5799   saved_allow_non_integral_constant_expression_p
5800     = parser->allow_non_integral_constant_expression_p;
5801   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5802   /* We are now parsing a constant-expression.  */
5803   parser->integral_constant_expression_p = true;
5804   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5805   parser->non_integral_constant_expression_p = false;
5806   /* Although the grammar says "conditional-expression", we parse an
5807      "assignment-expression", which also permits "throw-expression"
5808      and the use of assignment operators.  In the case that
5809      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5810      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5811      actually essential that we look for an assignment-expression.
5812      For example, cp_parser_initializer_clauses uses this function to
5813      determine whether a particular assignment-expression is in fact
5814      constant.  */
5815   expression = cp_parser_assignment_expression (parser);
5816   /* Restore the old settings.  */
5817   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5818   parser->allow_non_integral_constant_expression_p
5819     = saved_allow_non_integral_constant_expression_p;
5820   if (allow_non_constant_p)
5821     *non_constant_p = parser->non_integral_constant_expression_p;
5822   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5823
5824   return expression;
5825 }
5826
5827 /* Parse __builtin_offsetof.
5828
5829    offsetof-expression:
5830      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5831
5832    offsetof-member-designator:
5833      id-expression
5834      | offsetof-member-designator "." id-expression
5835      | offsetof-member-designator "[" expression "]"
5836 */
5837
5838 static tree
5839 cp_parser_builtin_offsetof (cp_parser *parser)
5840 {
5841   int save_ice_p, save_non_ice_p;
5842   tree type, expr;
5843   cp_id_kind dummy;
5844
5845   /* We're about to accept non-integral-constant things, but will
5846      definitely yield an integral constant expression.  Save and
5847      restore these values around our local parsing.  */
5848   save_ice_p = parser->integral_constant_expression_p;
5849   save_non_ice_p = parser->non_integral_constant_expression_p;
5850
5851   /* Consume the "__builtin_offsetof" token.  */
5852   cp_lexer_consume_token (parser->lexer);
5853   /* Consume the opening `('.  */
5854   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5855   /* Parse the type-id.  */
5856   type = cp_parser_type_id (parser);
5857   /* Look for the `,'.  */
5858   cp_parser_require (parser, CPP_COMMA, "`,'");
5859
5860   /* Build the (type *)null that begins the traditional offsetof macro.  */
5861   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5862
5863   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5864   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5865                                                  true, &dummy);
5866   while (true)
5867     {
5868       cp_token *token = cp_lexer_peek_token (parser->lexer);
5869       switch (token->type)
5870         {
5871         case CPP_OPEN_SQUARE:
5872           /* offsetof-member-designator "[" expression "]" */
5873           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5874           break;
5875
5876         case CPP_DOT:
5877           /* offsetof-member-designator "." identifier */
5878           cp_lexer_consume_token (parser->lexer);
5879           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5880                                                          true, &dummy);
5881           break;
5882
5883         case CPP_CLOSE_PAREN:
5884           /* Consume the ")" token.  */
5885           cp_lexer_consume_token (parser->lexer);
5886           goto success;
5887
5888         default:
5889           /* Error.  We know the following require will fail, but
5890              that gives the proper error message.  */
5891           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5892           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5893           expr = error_mark_node;
5894           goto failure;
5895         }
5896     }
5897
5898  success:
5899   /* If we're processing a template, we can't finish the semantics yet.
5900      Otherwise we can fold the entire expression now.  */
5901   if (processing_template_decl)
5902     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5903   else
5904     expr = fold_offsetof (expr);
5905
5906  failure:
5907   parser->integral_constant_expression_p = save_ice_p;
5908   parser->non_integral_constant_expression_p = save_non_ice_p;
5909
5910   return expr;
5911 }
5912
5913 /* Statements [gram.stmt.stmt]  */
5914
5915 /* Parse a statement.
5916
5917    statement:
5918      labeled-statement
5919      expression-statement
5920      compound-statement
5921      selection-statement
5922      iteration-statement
5923      jump-statement
5924      declaration-statement
5925      try-block  */
5926
5927 static void
5928 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5929 {
5930   tree statement;
5931   cp_token *token;
5932   location_t statement_location;
5933
5934   /* There is no statement yet.  */
5935   statement = NULL_TREE;
5936   /* Peek at the next token.  */
5937   token = cp_lexer_peek_token (parser->lexer);
5938   /* Remember the location of the first token in the statement.  */
5939   statement_location = token->location;
5940   /* If this is a keyword, then that will often determine what kind of
5941      statement we have.  */
5942   if (token->type == CPP_KEYWORD)
5943     {
5944       enum rid keyword = token->keyword;
5945
5946       switch (keyword)
5947         {
5948         case RID_CASE:
5949         case RID_DEFAULT:
5950           statement = cp_parser_labeled_statement (parser,
5951                                                    in_statement_expr);
5952           break;
5953
5954         case RID_IF:
5955         case RID_SWITCH:
5956           statement = cp_parser_selection_statement (parser);
5957           break;
5958
5959         case RID_WHILE:
5960         case RID_DO:
5961         case RID_FOR:
5962           statement = cp_parser_iteration_statement (parser);
5963           break;
5964
5965         case RID_BREAK:
5966         case RID_CONTINUE:
5967         case RID_RETURN:
5968         case RID_GOTO:
5969           statement = cp_parser_jump_statement (parser);
5970           break;
5971
5972         case RID_TRY:
5973           statement = cp_parser_try_block (parser);
5974           break;
5975
5976         default:
5977           /* It might be a keyword like `int' that can start a
5978              declaration-statement.  */
5979           break;
5980         }
5981     }
5982   else if (token->type == CPP_NAME)
5983     {
5984       /* If the next token is a `:', then we are looking at a
5985          labeled-statement.  */
5986       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5987       if (token->type == CPP_COLON)
5988         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5989     }
5990   /* Anything that starts with a `{' must be a compound-statement.  */
5991   else if (token->type == CPP_OPEN_BRACE)
5992     statement = cp_parser_compound_statement (parser, NULL, false);
5993
5994   /* Everything else must be a declaration-statement or an
5995      expression-statement.  Try for the declaration-statement
5996      first, unless we are looking at a `;', in which case we know that
5997      we have an expression-statement.  */
5998   if (!statement)
5999     {
6000       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6001         {
6002           cp_parser_parse_tentatively (parser);
6003           /* Try to parse the declaration-statement.  */
6004           cp_parser_declaration_statement (parser);
6005           /* If that worked, we're done.  */
6006           if (cp_parser_parse_definitely (parser))
6007             return;
6008         }
6009       /* Look for an expression-statement instead.  */
6010       statement = cp_parser_expression_statement (parser, in_statement_expr);
6011     }
6012
6013   /* Set the line number for the statement.  */
6014   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6015     SET_EXPR_LOCATION (statement, statement_location);
6016 }
6017
6018 /* Parse a labeled-statement.
6019
6020    labeled-statement:
6021      identifier : statement
6022      case constant-expression : statement
6023      default : statement
6024
6025    GNU Extension:
6026
6027    labeled-statement:
6028      case constant-expression ... constant-expression : statement
6029
6030    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6031    For an ordinary label, returns a LABEL_EXPR.  */
6032
6033 static tree
6034 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6035 {
6036   cp_token *token;
6037   tree statement = error_mark_node;
6038
6039   /* The next token should be an identifier.  */
6040   token = cp_lexer_peek_token (parser->lexer);
6041   if (token->type != CPP_NAME
6042       && token->type != CPP_KEYWORD)
6043     {
6044       cp_parser_error (parser, "expected labeled-statement");
6045       return error_mark_node;
6046     }
6047
6048   switch (token->keyword)
6049     {
6050     case RID_CASE:
6051       {
6052         tree expr, expr_hi;
6053         cp_token *ellipsis;
6054
6055         /* Consume the `case' token.  */
6056         cp_lexer_consume_token (parser->lexer);
6057         /* Parse the constant-expression.  */
6058         expr = cp_parser_constant_expression (parser,
6059                                               /*allow_non_constant_p=*/false,
6060                                               NULL);
6061
6062         ellipsis = cp_lexer_peek_token (parser->lexer);
6063         if (ellipsis->type == CPP_ELLIPSIS)
6064           {
6065             /* Consume the `...' token.  */
6066             cp_lexer_consume_token (parser->lexer);
6067             expr_hi =
6068               cp_parser_constant_expression (parser,
6069                                              /*allow_non_constant_p=*/false,
6070                                              NULL);
6071             /* We don't need to emit warnings here, as the common code
6072                will do this for us.  */
6073           }
6074         else
6075           expr_hi = NULL_TREE;
6076
6077         if (!parser->in_switch_statement_p)
6078           error ("case label `%E' not within a switch statement", expr);
6079         else
6080           statement = finish_case_label (expr, expr_hi);
6081       }
6082       break;
6083
6084     case RID_DEFAULT:
6085       /* Consume the `default' token.  */
6086       cp_lexer_consume_token (parser->lexer);
6087       if (!parser->in_switch_statement_p)
6088         error ("case label not within a switch statement");
6089       else
6090         statement = finish_case_label (NULL_TREE, NULL_TREE);
6091       break;
6092
6093     default:
6094       /* Anything else must be an ordinary label.  */
6095       statement = finish_label_stmt (cp_parser_identifier (parser));
6096       break;
6097     }
6098
6099   /* Require the `:' token.  */
6100   cp_parser_require (parser, CPP_COLON, "`:'");
6101   /* Parse the labeled statement.  */
6102   cp_parser_statement (parser, in_statement_expr);
6103
6104   /* Return the label, in the case of a `case' or `default' label.  */
6105   return statement;
6106 }
6107
6108 /* Parse an expression-statement.
6109
6110    expression-statement:
6111      expression [opt] ;
6112
6113    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6114    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6115    indicates whether this expression-statement is part of an
6116    expression statement.  */
6117
6118 static tree
6119 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6120 {
6121   tree statement = NULL_TREE;
6122
6123   /* If the next token is a ';', then there is no expression
6124      statement.  */
6125   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6126     statement = cp_parser_expression (parser);
6127
6128   /* Consume the final `;'.  */
6129   cp_parser_consume_semicolon_at_end_of_statement (parser);
6130
6131   if (in_statement_expr
6132       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6133     {
6134       /* This is the final expression statement of a statement
6135          expression.  */
6136       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6137     }
6138   else if (statement)
6139     statement = finish_expr_stmt (statement);
6140   else
6141     finish_stmt ();
6142
6143   return statement;
6144 }
6145
6146 /* Parse a compound-statement.
6147
6148    compound-statement:
6149      { statement-seq [opt] }
6150
6151    Returns a tree representing the statement.  */
6152
6153 static tree
6154 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6155                               bool in_try)
6156 {
6157   tree compound_stmt;
6158
6159   /* Consume the `{'.  */
6160   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6161     return error_mark_node;
6162   /* Begin the compound-statement.  */
6163   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6164   /* Parse an (optional) statement-seq.  */
6165   cp_parser_statement_seq_opt (parser, in_statement_expr);
6166   /* Finish the compound-statement.  */
6167   finish_compound_stmt (compound_stmt);
6168   /* Consume the `}'.  */
6169   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6170
6171   return compound_stmt;
6172 }
6173
6174 /* Parse an (optional) statement-seq.
6175
6176    statement-seq:
6177      statement
6178      statement-seq [opt] statement  */
6179
6180 static void
6181 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6182 {
6183   /* Scan statements until there aren't any more.  */
6184   while (true)
6185     {
6186       /* If we're looking at a `}', then we've run out of statements.  */
6187       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6188           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6189         break;
6190
6191       /* Parse the statement.  */
6192       cp_parser_statement (parser, in_statement_expr);
6193     }
6194 }
6195
6196 /* Parse a selection-statement.
6197
6198    selection-statement:
6199      if ( condition ) statement
6200      if ( condition ) statement else statement
6201      switch ( condition ) statement
6202
6203    Returns the new IF_STMT or SWITCH_STMT.  */
6204
6205 static tree
6206 cp_parser_selection_statement (cp_parser* parser)
6207 {
6208   cp_token *token;
6209   enum rid keyword;
6210
6211   /* Peek at the next token.  */
6212   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6213
6214   /* See what kind of keyword it is.  */
6215   keyword = token->keyword;
6216   switch (keyword)
6217     {
6218     case RID_IF:
6219     case RID_SWITCH:
6220       {
6221         tree statement;
6222         tree condition;
6223
6224         /* Look for the `('.  */
6225         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6226           {
6227             cp_parser_skip_to_end_of_statement (parser);
6228             return error_mark_node;
6229           }
6230
6231         /* Begin the selection-statement.  */
6232         if (keyword == RID_IF)
6233           statement = begin_if_stmt ();
6234         else
6235           statement = begin_switch_stmt ();
6236
6237         /* Parse the condition.  */
6238         condition = cp_parser_condition (parser);
6239         /* Look for the `)'.  */
6240         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6241           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6242                                                  /*consume_paren=*/true);
6243
6244         if (keyword == RID_IF)
6245           {
6246             /* Add the condition.  */
6247             finish_if_stmt_cond (condition, statement);
6248
6249             /* Parse the then-clause.  */
6250             cp_parser_implicitly_scoped_statement (parser);
6251             finish_then_clause (statement);
6252
6253             /* If the next token is `else', parse the else-clause.  */
6254             if (cp_lexer_next_token_is_keyword (parser->lexer,
6255                                                 RID_ELSE))
6256               {
6257                 /* Consume the `else' keyword.  */
6258                 cp_lexer_consume_token (parser->lexer);
6259                 begin_else_clause (statement);
6260                 /* Parse the else-clause.  */
6261                 cp_parser_implicitly_scoped_statement (parser);
6262                 finish_else_clause (statement);
6263               }
6264
6265             /* Now we're all done with the if-statement.  */
6266             finish_if_stmt (statement);
6267           }
6268         else
6269           {
6270             bool in_switch_statement_p;
6271
6272             /* Add the condition.  */
6273             finish_switch_cond (condition, statement);
6274
6275             /* Parse the body of the switch-statement.  */
6276             in_switch_statement_p = parser->in_switch_statement_p;
6277             parser->in_switch_statement_p = true;
6278             cp_parser_implicitly_scoped_statement (parser);
6279             parser->in_switch_statement_p = in_switch_statement_p;
6280
6281             /* Now we're all done with the switch-statement.  */
6282             finish_switch_stmt (statement);
6283           }
6284
6285         return statement;
6286       }
6287       break;
6288
6289     default:
6290       cp_parser_error (parser, "expected selection-statement");
6291       return error_mark_node;
6292     }
6293 }
6294
6295 /* Parse a condition.
6296
6297    condition:
6298      expression
6299      type-specifier-seq declarator = assignment-expression
6300
6301    GNU Extension:
6302
6303    condition:
6304      type-specifier-seq declarator asm-specification [opt]
6305        attributes [opt] = assignment-expression
6306
6307    Returns the expression that should be tested.  */
6308
6309 static tree
6310 cp_parser_condition (cp_parser* parser)
6311 {
6312   cp_decl_specifier_seq type_specifiers;
6313   const char *saved_message;
6314
6315   /* Try the declaration first.  */
6316   cp_parser_parse_tentatively (parser);
6317   /* New types are not allowed in the type-specifier-seq for a
6318      condition.  */
6319   saved_message = parser->type_definition_forbidden_message;
6320   parser->type_definition_forbidden_message
6321     = "types may not be defined in conditions";
6322   /* Parse the type-specifier-seq.  */
6323   cp_parser_type_specifier_seq (parser, &type_specifiers);
6324   /* Restore the saved message.  */
6325   parser->type_definition_forbidden_message = saved_message;
6326   /* If all is well, we might be looking at a declaration.  */
6327   if (!cp_parser_error_occurred (parser))
6328     {
6329       tree decl;
6330       tree asm_specification;
6331       tree attributes;
6332       cp_declarator *declarator;
6333       tree initializer = NULL_TREE;
6334
6335       /* Parse the declarator.  */
6336       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6337                                          /*ctor_dtor_or_conv_p=*/NULL,
6338                                          /*parenthesized_p=*/NULL);
6339       /* Parse the attributes.  */
6340       attributes = cp_parser_attributes_opt (parser);
6341       /* Parse the asm-specification.  */
6342       asm_specification = cp_parser_asm_specification_opt (parser);
6343       /* If the next token is not an `=', then we might still be
6344          looking at an expression.  For example:
6345
6346            if (A(a).x)
6347
6348          looks like a decl-specifier-seq and a declarator -- but then
6349          there is no `=', so this is an expression.  */
6350       cp_parser_require (parser, CPP_EQ, "`='");
6351       /* If we did see an `=', then we are looking at a declaration
6352          for sure.  */
6353       if (cp_parser_parse_definitely (parser))
6354         {
6355           bool pop_p;
6356
6357           /* Create the declaration.  */
6358           decl = start_decl (declarator, &type_specifiers,
6359                              /*initialized_p=*/true,
6360                              attributes, /*prefix_attributes=*/NULL_TREE,
6361                              &pop_p);
6362           /* Parse the assignment-expression.  */
6363           initializer = cp_parser_assignment_expression (parser);
6364
6365           /* Process the initializer.  */
6366           cp_finish_decl (decl,
6367                           initializer,
6368                           asm_specification,
6369                           LOOKUP_ONLYCONVERTING);
6370           if (pop_p)
6371             pop_scope (DECL_CONTEXT (decl));
6372
6373           return convert_from_reference (decl);
6374         }
6375     }
6376   /* If we didn't even get past the declarator successfully, we are
6377      definitely not looking at a declaration.  */
6378   else
6379     cp_parser_abort_tentative_parse (parser);
6380
6381   /* Otherwise, we are looking at an expression.  */
6382   return cp_parser_expression (parser);
6383 }
6384
6385 /* Parse an iteration-statement.
6386
6387    iteration-statement:
6388      while ( condition ) statement
6389      do statement while ( expression ) ;
6390      for ( for-init-statement condition [opt] ; expression [opt] )
6391        statement
6392
6393    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6394
6395 static tree
6396 cp_parser_iteration_statement (cp_parser* parser)
6397 {
6398   cp_token *token;
6399   enum rid keyword;
6400   tree statement;
6401   bool in_iteration_statement_p;
6402
6403
6404   /* Peek at the next token.  */
6405   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6406   if (!token)
6407     return error_mark_node;
6408
6409   /* Remember whether or not we are already within an iteration
6410      statement.  */
6411   in_iteration_statement_p = parser->in_iteration_statement_p;
6412
6413   /* See what kind of keyword it is.  */
6414   keyword = token->keyword;
6415   switch (keyword)
6416     {
6417     case RID_WHILE:
6418       {
6419         tree condition;
6420
6421         /* Begin the while-statement.  */
6422         statement = begin_while_stmt ();
6423         /* Look for the `('.  */
6424         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6425         /* Parse the condition.  */
6426         condition = cp_parser_condition (parser);
6427         finish_while_stmt_cond (condition, statement);
6428         /* Look for the `)'.  */
6429         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6430         /* Parse the dependent statement.  */
6431         parser->in_iteration_statement_p = true;
6432         cp_parser_already_scoped_statement (parser);
6433         parser->in_iteration_statement_p = in_iteration_statement_p;
6434         /* We're done with the while-statement.  */
6435         finish_while_stmt (statement);
6436       }
6437       break;
6438
6439     case RID_DO:
6440       {
6441         tree expression;
6442
6443         /* Begin the do-statement.  */
6444         statement = begin_do_stmt ();
6445         /* Parse the body of the do-statement.  */
6446         parser->in_iteration_statement_p = true;
6447         cp_parser_implicitly_scoped_statement (parser);
6448         parser->in_iteration_statement_p = in_iteration_statement_p;
6449         finish_do_body (statement);
6450         /* Look for the `while' keyword.  */
6451         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6452         /* Look for the `('.  */
6453         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6454         /* Parse the expression.  */
6455         expression = cp_parser_expression (parser);
6456         /* We're done with the do-statement.  */
6457         finish_do_stmt (expression, statement);
6458         /* Look for the `)'.  */
6459         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6460         /* Look for the `;'.  */
6461         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6462       }
6463       break;
6464
6465     case RID_FOR:
6466       {
6467         tree condition = NULL_TREE;
6468         tree expression = NULL_TREE;
6469
6470         /* Begin the for-statement.  */
6471         statement = begin_for_stmt ();
6472         /* Look for the `('.  */
6473         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6474         /* Parse the initialization.  */
6475         cp_parser_for_init_statement (parser);
6476         finish_for_init_stmt (statement);
6477
6478         /* If there's a condition, process it.  */
6479         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6480           condition = cp_parser_condition (parser);
6481         finish_for_cond (condition, statement);
6482         /* Look for the `;'.  */
6483         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6484
6485         /* If there's an expression, process it.  */
6486         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6487           expression = cp_parser_expression (parser);
6488         finish_for_expr (expression, statement);
6489         /* Look for the `)'.  */
6490         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6491
6492         /* Parse the body of the for-statement.  */
6493         parser->in_iteration_statement_p = true;
6494         cp_parser_already_scoped_statement (parser);
6495         parser->in_iteration_statement_p = in_iteration_statement_p;
6496
6497         /* We're done with the for-statement.  */
6498         finish_for_stmt (statement);
6499       }
6500       break;
6501
6502     default:
6503       cp_parser_error (parser, "expected iteration-statement");
6504       statement = error_mark_node;
6505       break;
6506     }
6507
6508   return statement;
6509 }
6510
6511 /* Parse a for-init-statement.
6512
6513    for-init-statement:
6514      expression-statement
6515      simple-declaration  */
6516
6517 static void
6518 cp_parser_for_init_statement (cp_parser* parser)
6519 {
6520   /* If the next token is a `;', then we have an empty
6521      expression-statement.  Grammatically, this is also a
6522      simple-declaration, but an invalid one, because it does not
6523      declare anything.  Therefore, if we did not handle this case
6524      specially, we would issue an error message about an invalid
6525      declaration.  */
6526   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6527     {
6528       /* We're going to speculatively look for a declaration, falling back
6529          to an expression, if necessary.  */
6530       cp_parser_parse_tentatively (parser);
6531       /* Parse the declaration.  */
6532       cp_parser_simple_declaration (parser,
6533                                     /*function_definition_allowed_p=*/false);
6534       /* If the tentative parse failed, then we shall need to look for an
6535          expression-statement.  */
6536       if (cp_parser_parse_definitely (parser))
6537         return;
6538     }
6539
6540   cp_parser_expression_statement (parser, false);
6541 }
6542
6543 /* Parse a jump-statement.
6544
6545    jump-statement:
6546      break ;
6547      continue ;
6548      return expression [opt] ;
6549      goto identifier ;
6550
6551    GNU extension:
6552
6553    jump-statement:
6554      goto * expression ;
6555
6556    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6557
6558 static tree
6559 cp_parser_jump_statement (cp_parser* parser)
6560 {
6561   tree statement = error_mark_node;
6562   cp_token *token;
6563   enum rid keyword;
6564
6565   /* Peek at the next token.  */
6566   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6567   if (!token)
6568     return error_mark_node;
6569
6570   /* See what kind of keyword it is.  */
6571   keyword = token->keyword;
6572   switch (keyword)
6573     {
6574     case RID_BREAK:
6575       if (!parser->in_switch_statement_p
6576           && !parser->in_iteration_statement_p)
6577         {
6578           error ("break statement not within loop or switch");
6579           statement = error_mark_node;
6580         }
6581       else
6582         statement = finish_break_stmt ();
6583       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6584       break;
6585
6586     case RID_CONTINUE:
6587       if (!parser->in_iteration_statement_p)
6588         {
6589           error ("continue statement not within a loop");
6590           statement = error_mark_node;
6591         }
6592       else
6593         statement = finish_continue_stmt ();
6594       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6595       break;
6596
6597     case RID_RETURN:
6598       {
6599         tree expr;
6600
6601         /* If the next token is a `;', then there is no
6602            expression.  */
6603         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6604           expr = cp_parser_expression (parser);
6605         else
6606           expr = NULL_TREE;
6607         /* Build the return-statement.  */
6608         statement = finish_return_stmt (expr);
6609         /* Look for the final `;'.  */
6610         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6611       }
6612       break;
6613
6614     case RID_GOTO:
6615       /* Create the goto-statement.  */
6616       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6617         {
6618           /* Issue a warning about this use of a GNU extension.  */
6619           if (pedantic)
6620             pedwarn ("ISO C++ forbids computed gotos");
6621           /* Consume the '*' token.  */
6622           cp_lexer_consume_token (parser->lexer);
6623           /* Parse the dependent expression.  */
6624           finish_goto_stmt (cp_parser_expression (parser));
6625         }
6626       else
6627         finish_goto_stmt (cp_parser_identifier (parser));
6628       /* Look for the final `;'.  */
6629       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6630       break;
6631
6632     default:
6633       cp_parser_error (parser, "expected jump-statement");
6634       break;
6635     }
6636
6637   return statement;
6638 }
6639
6640 /* Parse a declaration-statement.
6641
6642    declaration-statement:
6643      block-declaration  */
6644
6645 static void
6646 cp_parser_declaration_statement (cp_parser* parser)
6647 {
6648   void *p;
6649
6650   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6651   p = obstack_alloc (&declarator_obstack, 0);
6652
6653  /* Parse the block-declaration.  */
6654   cp_parser_block_declaration (parser, /*statement_p=*/true);
6655
6656   /* Free any declarators allocated.  */
6657   obstack_free (&declarator_obstack, p);
6658
6659   /* Finish off the statement.  */
6660   finish_stmt ();
6661 }
6662
6663 /* Some dependent statements (like `if (cond) statement'), are
6664    implicitly in their own scope.  In other words, if the statement is
6665    a single statement (as opposed to a compound-statement), it is
6666    none-the-less treated as if it were enclosed in braces.  Any
6667    declarations appearing in the dependent statement are out of scope
6668    after control passes that point.  This function parses a statement,
6669    but ensures that is in its own scope, even if it is not a
6670    compound-statement.
6671
6672    Returns the new statement.  */
6673
6674 static tree
6675 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6676 {
6677   tree statement;
6678
6679   /* If the token is not a `{', then we must take special action.  */
6680   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6681     {
6682       /* Create a compound-statement.  */
6683       statement = begin_compound_stmt (0);
6684       /* Parse the dependent-statement.  */
6685       cp_parser_statement (parser, false);
6686       /* Finish the dummy compound-statement.  */
6687       finish_compound_stmt (statement);
6688     }
6689   /* Otherwise, we simply parse the statement directly.  */
6690   else
6691     statement = cp_parser_compound_statement (parser, NULL, false);
6692
6693   /* Return the statement.  */
6694   return statement;
6695 }
6696
6697 /* For some dependent statements (like `while (cond) statement'), we
6698    have already created a scope.  Therefore, even if the dependent
6699    statement is a compound-statement, we do not want to create another
6700    scope.  */
6701
6702 static void
6703 cp_parser_already_scoped_statement (cp_parser* parser)
6704 {
6705   /* If the token is a `{', then we must take special action.  */
6706   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6707     cp_parser_statement (parser, false);
6708   else
6709     {
6710       /* Avoid calling cp_parser_compound_statement, so that we
6711          don't create a new scope.  Do everything else by hand.  */
6712       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6713       cp_parser_statement_seq_opt (parser, false);
6714       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6715     }
6716 }
6717
6718 /* Declarations [gram.dcl.dcl] */
6719
6720 /* Parse an optional declaration-sequence.
6721
6722    declaration-seq:
6723      declaration
6724      declaration-seq declaration  */
6725
6726 static void
6727 cp_parser_declaration_seq_opt (cp_parser* parser)
6728 {
6729   while (true)
6730     {
6731       cp_token *token;
6732
6733       token = cp_lexer_peek_token (parser->lexer);
6734
6735       if (token->type == CPP_CLOSE_BRACE
6736           || token->type == CPP_EOF)
6737         break;
6738
6739       if (token->type == CPP_SEMICOLON)
6740         {
6741           /* A declaration consisting of a single semicolon is
6742              invalid.  Allow it unless we're being pedantic.  */
6743           if (pedantic && !in_system_header)
6744             pedwarn ("extra `;'");
6745           cp_lexer_consume_token (parser->lexer);
6746           continue;
6747         }
6748
6749       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6750          parser to enter or exit implicit `extern "C"' blocks.  */
6751       while (pending_lang_change > 0)
6752         {
6753           push_lang_context (lang_name_c);
6754           --pending_lang_change;
6755         }
6756       while (pending_lang_change < 0)
6757         {
6758           pop_lang_context ();
6759           ++pending_lang_change;
6760         }
6761
6762       /* Parse the declaration itself.  */
6763       cp_parser_declaration (parser);
6764     }
6765 }
6766
6767 /* Parse a declaration.
6768
6769    declaration:
6770      block-declaration
6771      function-definition
6772      template-declaration
6773      explicit-instantiation
6774      explicit-specialization
6775      linkage-specification
6776      namespace-definition
6777
6778    GNU extension:
6779
6780    declaration:
6781       __extension__ declaration */
6782
6783 static void
6784 cp_parser_declaration (cp_parser* parser)
6785 {
6786   cp_token token1;
6787   cp_token token2;
6788   int saved_pedantic;
6789   void *p;
6790
6791   /* Set this here since we can be called after
6792      pushing the linkage specification.  */
6793   c_lex_string_translate = 1;
6794
6795   /* Check for the `__extension__' keyword.  */
6796   if (cp_parser_extension_opt (parser, &saved_pedantic))
6797     {
6798       /* Parse the qualified declaration.  */
6799       cp_parser_declaration (parser);
6800       /* Restore the PEDANTIC flag.  */
6801       pedantic = saved_pedantic;
6802
6803       return;
6804     }
6805
6806   /* Try to figure out what kind of declaration is present.  */
6807   token1 = *cp_lexer_peek_token (parser->lexer);
6808
6809   /* Don't translate the CPP_STRING in extern "C".  */
6810   if (token1.keyword == RID_EXTERN)
6811     c_lex_string_translate = 0;
6812
6813   if (token1.type != CPP_EOF)
6814     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6815
6816   c_lex_string_translate = 1;
6817
6818   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6819   p = obstack_alloc (&declarator_obstack, 0);
6820
6821   /* If the next token is `extern' and the following token is a string
6822      literal, then we have a linkage specification.  */
6823   if (token1.keyword == RID_EXTERN
6824       && cp_parser_is_string_literal (&token2))
6825     cp_parser_linkage_specification (parser);
6826   /* If the next token is `template', then we have either a template
6827      declaration, an explicit instantiation, or an explicit
6828      specialization.  */
6829   else if (token1.keyword == RID_TEMPLATE)
6830     {
6831       /* `template <>' indicates a template specialization.  */
6832       if (token2.type == CPP_LESS
6833           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6834         cp_parser_explicit_specialization (parser);
6835       /* `template <' indicates a template declaration.  */
6836       else if (token2.type == CPP_LESS)
6837         cp_parser_template_declaration (parser, /*member_p=*/false);
6838       /* Anything else must be an explicit instantiation.  */
6839       else
6840         cp_parser_explicit_instantiation (parser);
6841     }
6842   /* If the next token is `export', then we have a template
6843      declaration.  */
6844   else if (token1.keyword == RID_EXPORT)
6845     cp_parser_template_declaration (parser, /*member_p=*/false);
6846   /* If the next token is `extern', 'static' or 'inline' and the one
6847      after that is `template', we have a GNU extended explicit
6848      instantiation directive.  */
6849   else if (cp_parser_allow_gnu_extensions_p (parser)
6850            && (token1.keyword == RID_EXTERN
6851                || token1.keyword == RID_STATIC
6852                || token1.keyword == RID_INLINE)
6853            && token2.keyword == RID_TEMPLATE)
6854     cp_parser_explicit_instantiation (parser);
6855   /* If the next token is `namespace', check for a named or unnamed
6856      namespace definition.  */
6857   else if (token1.keyword == RID_NAMESPACE
6858            && (/* A named namespace definition.  */
6859                (token2.type == CPP_NAME
6860                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6861                     == CPP_OPEN_BRACE))
6862                /* An unnamed namespace definition.  */
6863                || token2.type == CPP_OPEN_BRACE))
6864     cp_parser_namespace_definition (parser);
6865   /* We must have either a block declaration or a function
6866      definition.  */
6867   else
6868     /* Try to parse a block-declaration, or a function-definition.  */
6869     cp_parser_block_declaration (parser, /*statement_p=*/false);
6870
6871   /* Free any declarators allocated.  */
6872   obstack_free (&declarator_obstack, p);
6873 }
6874
6875 /* Parse a block-declaration.
6876
6877    block-declaration:
6878      simple-declaration
6879      asm-definition
6880      namespace-alias-definition
6881      using-declaration
6882      using-directive
6883
6884    GNU Extension:
6885
6886    block-declaration:
6887      __extension__ block-declaration
6888      label-declaration
6889
6890    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6891    part of a declaration-statement.  */
6892
6893 static void
6894 cp_parser_block_declaration (cp_parser *parser,
6895                              bool      statement_p)
6896 {
6897   cp_token *token1;
6898   int saved_pedantic;
6899
6900   /* Check for the `__extension__' keyword.  */
6901   if (cp_parser_extension_opt (parser, &saved_pedantic))
6902     {
6903       /* Parse the qualified declaration.  */
6904       cp_parser_block_declaration (parser, statement_p);
6905       /* Restore the PEDANTIC flag.  */
6906       pedantic = saved_pedantic;
6907
6908       return;
6909     }
6910
6911   /* Peek at the next token to figure out which kind of declaration is
6912      present.  */
6913   token1 = cp_lexer_peek_token (parser->lexer);
6914
6915   /* If the next keyword is `asm', we have an asm-definition.  */
6916   if (token1->keyword == RID_ASM)
6917     {
6918       if (statement_p)
6919         cp_parser_commit_to_tentative_parse (parser);
6920       cp_parser_asm_definition (parser);
6921     }
6922   /* If the next keyword is `namespace', we have a
6923      namespace-alias-definition.  */
6924   else if (token1->keyword == RID_NAMESPACE)
6925     cp_parser_namespace_alias_definition (parser);
6926   /* If the next keyword is `using', we have either a
6927      using-declaration or a using-directive.  */
6928   else if (token1->keyword == RID_USING)
6929     {
6930       cp_token *token2;
6931
6932       if (statement_p)
6933         cp_parser_commit_to_tentative_parse (parser);
6934       /* If the token after `using' is `namespace', then we have a
6935          using-directive.  */
6936       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6937       if (token2->keyword == RID_NAMESPACE)
6938         cp_parser_using_directive (parser);
6939       /* Otherwise, it's a using-declaration.  */
6940       else
6941         cp_parser_using_declaration (parser);
6942     }
6943   /* If the next keyword is `__label__' we have a label declaration.  */
6944   else if (token1->keyword == RID_LABEL)
6945     {
6946       if (statement_p)
6947         cp_parser_commit_to_tentative_parse (parser);
6948       cp_parser_label_declaration (parser);
6949     }
6950   /* Anything else must be a simple-declaration.  */
6951   else
6952     cp_parser_simple_declaration (parser, !statement_p);
6953 }
6954
6955 /* Parse a simple-declaration.
6956
6957    simple-declaration:
6958      decl-specifier-seq [opt] init-declarator-list [opt] ;
6959
6960    init-declarator-list:
6961      init-declarator
6962      init-declarator-list , init-declarator
6963
6964    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6965    function-definition as a simple-declaration.  */
6966
6967 static void
6968 cp_parser_simple_declaration (cp_parser* parser,
6969                               bool function_definition_allowed_p)
6970 {
6971   cp_decl_specifier_seq decl_specifiers;
6972   int declares_class_or_enum;
6973   bool saw_declarator;
6974
6975   /* Defer access checks until we know what is being declared; the
6976      checks for names appearing in the decl-specifier-seq should be
6977      done as if we were in the scope of the thing being declared.  */
6978   push_deferring_access_checks (dk_deferred);
6979
6980   /* Parse the decl-specifier-seq.  We have to keep track of whether
6981      or not the decl-specifier-seq declares a named class or
6982      enumeration type, since that is the only case in which the
6983      init-declarator-list is allowed to be empty.
6984
6985      [dcl.dcl]
6986
6987      In a simple-declaration, the optional init-declarator-list can be
6988      omitted only when declaring a class or enumeration, that is when
6989      the decl-specifier-seq contains either a class-specifier, an
6990      elaborated-type-specifier, or an enum-specifier.  */
6991   cp_parser_decl_specifier_seq (parser,
6992                                 CP_PARSER_FLAGS_OPTIONAL,
6993                                 &decl_specifiers,
6994                                 &declares_class_or_enum);
6995   /* We no longer need to defer access checks.  */
6996   stop_deferring_access_checks ();
6997
6998   /* In a block scope, a valid declaration must always have a
6999      decl-specifier-seq.  By not trying to parse declarators, we can
7000      resolve the declaration/expression ambiguity more quickly.  */
7001   if (!function_definition_allowed_p
7002       && !decl_specifiers.any_specifiers_p)
7003     {
7004       cp_parser_error (parser, "expected declaration");
7005       goto done;
7006     }
7007
7008   /* If the next two tokens are both identifiers, the code is
7009      erroneous. The usual cause of this situation is code like:
7010
7011        T t;
7012
7013      where "T" should name a type -- but does not.  */
7014   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
7015     {
7016       /* If parsing tentatively, we should commit; we really are
7017          looking at a declaration.  */
7018       cp_parser_commit_to_tentative_parse (parser);
7019       /* Give up.  */
7020       goto done;
7021     }
7022
7023   /* Keep going until we hit the `;' at the end of the simple
7024      declaration.  */
7025   saw_declarator = false;
7026   while (cp_lexer_next_token_is_not (parser->lexer,
7027                                      CPP_SEMICOLON))
7028     {
7029       cp_token *token;
7030       bool function_definition_p;
7031       tree decl;
7032
7033       saw_declarator = true;
7034       /* Parse the init-declarator.  */
7035       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7036                                         function_definition_allowed_p,
7037                                         /*member_p=*/false,
7038                                         declares_class_or_enum,
7039                                         &function_definition_p);
7040       /* If an error occurred while parsing tentatively, exit quickly.
7041          (That usually happens when in the body of a function; each
7042          statement is treated as a declaration-statement until proven
7043          otherwise.)  */
7044       if (cp_parser_error_occurred (parser))
7045         goto done;
7046       /* Handle function definitions specially.  */
7047       if (function_definition_p)
7048         {
7049           /* If the next token is a `,', then we are probably
7050              processing something like:
7051
7052                void f() {}, *p;
7053
7054              which is erroneous.  */
7055           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7056             error ("mixing declarations and function-definitions is forbidden");
7057           /* Otherwise, we're done with the list of declarators.  */
7058           else
7059             {
7060               pop_deferring_access_checks ();
7061               return;
7062             }
7063         }
7064       /* The next token should be either a `,' or a `;'.  */
7065       token = cp_lexer_peek_token (parser->lexer);
7066       /* If it's a `,', there are more declarators to come.  */
7067       if (token->type == CPP_COMMA)
7068         cp_lexer_consume_token (parser->lexer);
7069       /* If it's a `;', we are done.  */
7070       else if (token->type == CPP_SEMICOLON)
7071         break;
7072       /* Anything else is an error.  */
7073       else
7074         {
7075           cp_parser_error (parser, "expected `,' or `;'");
7076           /* Skip tokens until we reach the end of the statement.  */
7077           cp_parser_skip_to_end_of_statement (parser);
7078           /* If the next token is now a `;', consume it.  */
7079           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7080             cp_lexer_consume_token (parser->lexer);
7081           goto done;
7082         }
7083       /* After the first time around, a function-definition is not
7084          allowed -- even if it was OK at first.  For example:
7085
7086            int i, f() {}
7087
7088          is not valid.  */
7089       function_definition_allowed_p = false;
7090     }
7091
7092   /* Issue an error message if no declarators are present, and the
7093      decl-specifier-seq does not itself declare a class or
7094      enumeration.  */
7095   if (!saw_declarator)
7096     {
7097       if (cp_parser_declares_only_class_p (parser))
7098         shadow_tag (&decl_specifiers);
7099       /* Perform any deferred access checks.  */
7100       perform_deferred_access_checks ();
7101     }
7102
7103   /* Consume the `;'.  */
7104   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7105
7106  done:
7107   pop_deferring_access_checks ();
7108 }
7109
7110 /* Parse a decl-specifier-seq.
7111
7112    decl-specifier-seq:
7113      decl-specifier-seq [opt] decl-specifier
7114
7115    decl-specifier:
7116      storage-class-specifier
7117      type-specifier
7118      function-specifier
7119      friend
7120      typedef
7121
7122    GNU Extension:
7123
7124    decl-specifier:
7125      attributes
7126
7127    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7128
7129    The parser flags FLAGS is used to control type-specifier parsing.
7130
7131    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7132    flags:
7133
7134      1: one of the decl-specifiers is an elaborated-type-specifier
7135         (i.e., a type declaration)
7136      2: one of the decl-specifiers is an enum-specifier or a
7137         class-specifier (i.e., a type definition)
7138
7139    */
7140
7141 static void
7142 cp_parser_decl_specifier_seq (cp_parser* parser,
7143                               cp_parser_flags flags,
7144                               cp_decl_specifier_seq *decl_specs,
7145                               int* declares_class_or_enum)
7146 {
7147   bool constructor_possible_p = !parser->in_declarator_p;
7148
7149   /* Clear DECL_SPECS.  */
7150   clear_decl_specs (decl_specs);
7151
7152   /* Assume no class or enumeration type is declared.  */
7153   *declares_class_or_enum = 0;
7154
7155   /* Keep reading specifiers until there are no more to read.  */
7156   while (true)
7157     {
7158       bool constructor_p;
7159       bool found_decl_spec;
7160       cp_token *token;
7161
7162       /* Peek at the next token.  */
7163       token = cp_lexer_peek_token (parser->lexer);
7164       /* Handle attributes.  */
7165       if (token->keyword == RID_ATTRIBUTE)
7166         {
7167           /* Parse the attributes.  */
7168           decl_specs->attributes
7169             = chainon (decl_specs->attributes,
7170                        cp_parser_attributes_opt (parser));
7171           continue;
7172         }
7173       /* Assume we will find a decl-specifier keyword.  */
7174       found_decl_spec = true;
7175       /* If the next token is an appropriate keyword, we can simply
7176          add it to the list.  */
7177       switch (token->keyword)
7178         {
7179           /* decl-specifier:
7180                friend  */
7181         case RID_FRIEND:
7182           if (decl_specs->specs[(int) ds_friend]++)
7183             error ("duplicate `friend'");
7184           /* Consume the token.  */
7185           cp_lexer_consume_token (parser->lexer);
7186           break;
7187
7188           /* function-specifier:
7189                inline
7190                virtual
7191                explicit  */
7192         case RID_INLINE:
7193         case RID_VIRTUAL:
7194         case RID_EXPLICIT:
7195           cp_parser_function_specifier_opt (parser, decl_specs);
7196           break;
7197
7198           /* decl-specifier:
7199                typedef  */
7200         case RID_TYPEDEF:
7201           ++decl_specs->specs[(int) ds_typedef];
7202           /* Consume the token.  */
7203           cp_lexer_consume_token (parser->lexer);
7204           /* A constructor declarator cannot appear in a typedef.  */
7205           constructor_possible_p = false;
7206           /* The "typedef" keyword can only occur in a declaration; we
7207              may as well commit at this point.  */
7208           cp_parser_commit_to_tentative_parse (parser);
7209           break;
7210
7211           /* storage-class-specifier:
7212                auto
7213                register
7214                static
7215                extern
7216                mutable
7217
7218              GNU Extension:
7219                thread  */
7220         case RID_AUTO:
7221           /* Consume the token.  */
7222           cp_lexer_consume_token (parser->lexer);
7223           cp_parser_set_storage_class (decl_specs, sc_auto);
7224           break;
7225         case RID_REGISTER:
7226           /* Consume the token.  */
7227           cp_lexer_consume_token (parser->lexer);
7228           cp_parser_set_storage_class (decl_specs, sc_register);
7229           break;
7230         case RID_STATIC:
7231           /* Consume the token.  */
7232           cp_lexer_consume_token (parser->lexer);
7233           if (decl_specs->specs[(int) ds_thread])
7234             {
7235               error ("`__thread' before `static'");
7236               decl_specs->specs[(int) ds_thread] = 0;
7237             }
7238           cp_parser_set_storage_class (decl_specs, sc_static);
7239           break;
7240         case RID_EXTERN:
7241           /* Consume the token.  */
7242           cp_lexer_consume_token (parser->lexer);
7243           if (decl_specs->specs[(int) ds_thread])
7244             {
7245               error ("`__thread' before `extern'");
7246               decl_specs->specs[(int) ds_thread] = 0;
7247             }
7248           cp_parser_set_storage_class (decl_specs, sc_extern);
7249           break;
7250         case RID_MUTABLE:
7251           /* Consume the token.  */
7252           cp_lexer_consume_token (parser->lexer);
7253           cp_parser_set_storage_class (decl_specs, sc_mutable);
7254           break;
7255         case RID_THREAD:
7256           /* Consume the token.  */
7257           cp_lexer_consume_token (parser->lexer);
7258           ++decl_specs->specs[(int) ds_thread];
7259           break;
7260
7261         default:
7262           /* We did not yet find a decl-specifier yet.  */
7263           found_decl_spec = false;
7264           break;
7265         }
7266
7267       /* Constructors are a special case.  The `S' in `S()' is not a
7268          decl-specifier; it is the beginning of the declarator.  */
7269       constructor_p
7270         = (!found_decl_spec
7271            && constructor_possible_p
7272            && (cp_parser_constructor_declarator_p
7273                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7274
7275       /* If we don't have a DECL_SPEC yet, then we must be looking at
7276          a type-specifier.  */
7277       if (!found_decl_spec && !constructor_p)
7278         {
7279           int decl_spec_declares_class_or_enum;
7280           bool is_cv_qualifier;
7281           tree type_spec;
7282
7283           type_spec
7284             = cp_parser_type_specifier (parser, flags,
7285                                         decl_specs,
7286                                         /*is_declaration=*/true,
7287                                         &decl_spec_declares_class_or_enum,
7288                                         &is_cv_qualifier);
7289
7290           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7291
7292           /* If this type-specifier referenced a user-defined type
7293              (a typedef, class-name, etc.), then we can't allow any
7294              more such type-specifiers henceforth.
7295
7296              [dcl.spec]
7297
7298              The longest sequence of decl-specifiers that could
7299              possibly be a type name is taken as the
7300              decl-specifier-seq of a declaration.  The sequence shall
7301              be self-consistent as described below.
7302
7303              [dcl.type]
7304
7305              As a general rule, at most one type-specifier is allowed
7306              in the complete decl-specifier-seq of a declaration.  The
7307              only exceptions are the following:
7308
7309              -- const or volatile can be combined with any other
7310                 type-specifier.
7311
7312              -- signed or unsigned can be combined with char, long,
7313                 short, or int.
7314
7315              -- ..
7316
7317              Example:
7318
7319                typedef char* Pc;
7320                void g (const int Pc);
7321
7322              Here, Pc is *not* part of the decl-specifier seq; it's
7323              the declarator.  Therefore, once we see a type-specifier
7324              (other than a cv-qualifier), we forbid any additional
7325              user-defined types.  We *do* still allow things like `int
7326              int' to be considered a decl-specifier-seq, and issue the
7327              error message later.  */
7328           if (type_spec && !is_cv_qualifier)
7329             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7330           /* A constructor declarator cannot follow a type-specifier.  */
7331           if (type_spec)
7332             {
7333               constructor_possible_p = false;
7334               found_decl_spec = true;
7335             }
7336         }
7337
7338       /* If we still do not have a DECL_SPEC, then there are no more
7339          decl-specifiers.  */
7340       if (!found_decl_spec)
7341         break;
7342
7343       decl_specs->any_specifiers_p = true;
7344       /* After we see one decl-specifier, further decl-specifiers are
7345          always optional.  */
7346       flags |= CP_PARSER_FLAGS_OPTIONAL;
7347     }
7348
7349   /* Don't allow a friend specifier with a class definition.  */
7350   if (decl_specs->specs[(int) ds_friend] != 0
7351       && (*declares_class_or_enum & 2))
7352     error ("class definition may not be declared a friend");
7353 }
7354
7355 /* Parse an (optional) storage-class-specifier.
7356
7357    storage-class-specifier:
7358      auto
7359      register
7360      static
7361      extern
7362      mutable
7363
7364    GNU Extension:
7365
7366    storage-class-specifier:
7367      thread
7368
7369    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7370
7371 static tree
7372 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7373 {
7374   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7375     {
7376     case RID_AUTO:
7377     case RID_REGISTER:
7378     case RID_STATIC:
7379     case RID_EXTERN:
7380     case RID_MUTABLE:
7381     case RID_THREAD:
7382       /* Consume the token.  */
7383       return cp_lexer_consume_token (parser->lexer)->value;
7384
7385     default:
7386       return NULL_TREE;
7387     }
7388 }
7389
7390 /* Parse an (optional) function-specifier.
7391
7392    function-specifier:
7393      inline
7394      virtual
7395      explicit
7396
7397    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7398    Updates DECL_SPECS, if it is non-NULL.  */
7399
7400 static tree
7401 cp_parser_function_specifier_opt (cp_parser* parser,
7402                                   cp_decl_specifier_seq *decl_specs)
7403 {
7404   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7405     {
7406     case RID_INLINE:
7407       if (decl_specs)
7408         ++decl_specs->specs[(int) ds_inline];
7409       break;
7410
7411     case RID_VIRTUAL:
7412       if (decl_specs)
7413         ++decl_specs->specs[(int) ds_virtual];
7414       break;
7415
7416     case RID_EXPLICIT:
7417       if (decl_specs)
7418         ++decl_specs->specs[(int) ds_explicit];
7419       break;
7420
7421     default:
7422       return NULL_TREE;
7423     }
7424
7425   /* Consume the token.  */
7426   return cp_lexer_consume_token (parser->lexer)->value;
7427 }
7428
7429 /* Parse a linkage-specification.
7430
7431    linkage-specification:
7432      extern string-literal { declaration-seq [opt] }
7433      extern string-literal declaration  */
7434
7435 static void
7436 cp_parser_linkage_specification (cp_parser* parser)
7437 {
7438   cp_token *token;
7439   tree linkage;
7440
7441   /* Look for the `extern' keyword.  */
7442   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7443
7444   /* Peek at the next token.  */
7445   token = cp_lexer_peek_token (parser->lexer);
7446   /* If it's not a string-literal, then there's a problem.  */
7447   if (!cp_parser_is_string_literal (token))
7448     {
7449       cp_parser_error (parser, "expected language-name");
7450       return;
7451     }
7452   /* Consume the token.  */
7453   cp_lexer_consume_token (parser->lexer);
7454
7455   /* Transform the literal into an identifier.  If the literal is a
7456      wide-character string, or contains embedded NULs, then we can't
7457      handle it as the user wants.  */
7458   if (token->type == CPP_WSTRING
7459       || (strlen (TREE_STRING_POINTER (token->value))
7460           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7461     {
7462       cp_parser_error (parser, "invalid linkage-specification");
7463       /* Assume C++ linkage.  */
7464       linkage = get_identifier ("c++");
7465     }
7466   /* If the string is chained to another string, take the latter,
7467      that's the untranslated string.  */
7468   else if (TREE_CHAIN (token->value))
7469     linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7470   /* If it's a simple string constant, things are easier.  */
7471   else
7472     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7473
7474   /* We're now using the new linkage.  */
7475   push_lang_context (linkage);
7476
7477   /* If the next token is a `{', then we're using the first
7478      production.  */
7479   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7480     {
7481       /* Consume the `{' token.  */
7482       cp_lexer_consume_token (parser->lexer);
7483       /* Parse the declarations.  */
7484       cp_parser_declaration_seq_opt (parser);
7485       /* Look for the closing `}'.  */
7486       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7487     }
7488   /* Otherwise, there's just one declaration.  */
7489   else
7490     {
7491       bool saved_in_unbraced_linkage_specification_p;
7492
7493       saved_in_unbraced_linkage_specification_p
7494         = parser->in_unbraced_linkage_specification_p;
7495       parser->in_unbraced_linkage_specification_p = true;
7496       have_extern_spec = true;
7497       cp_parser_declaration (parser);
7498       have_extern_spec = false;
7499       parser->in_unbraced_linkage_specification_p
7500         = saved_in_unbraced_linkage_specification_p;
7501     }
7502
7503   /* We're done with the linkage-specification.  */
7504   pop_lang_context ();
7505 }
7506
7507 /* Special member functions [gram.special] */
7508
7509 /* Parse a conversion-function-id.
7510
7511    conversion-function-id:
7512      operator conversion-type-id
7513
7514    Returns an IDENTIFIER_NODE representing the operator.  */
7515
7516 static tree
7517 cp_parser_conversion_function_id (cp_parser* parser)
7518 {
7519   tree type;
7520   tree saved_scope;
7521   tree saved_qualifying_scope;
7522   tree saved_object_scope;
7523   bool pop_p = false;
7524
7525   /* Look for the `operator' token.  */
7526   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7527     return error_mark_node;
7528   /* When we parse the conversion-type-id, the current scope will be
7529      reset.  However, we need that information in able to look up the
7530      conversion function later, so we save it here.  */
7531   saved_scope = parser->scope;
7532   saved_qualifying_scope = parser->qualifying_scope;
7533   saved_object_scope = parser->object_scope;
7534   /* We must enter the scope of the class so that the names of
7535      entities declared within the class are available in the
7536      conversion-type-id.  For example, consider:
7537
7538        struct S {
7539          typedef int I;
7540          operator I();
7541        };
7542
7543        S::operator I() { ... }
7544
7545      In order to see that `I' is a type-name in the definition, we
7546      must be in the scope of `S'.  */
7547   if (saved_scope)
7548     pop_p = push_scope (saved_scope);
7549   /* Parse the conversion-type-id.  */
7550   type = cp_parser_conversion_type_id (parser);
7551   /* Leave the scope of the class, if any.  */
7552   if (pop_p)
7553     pop_scope (saved_scope);
7554   /* Restore the saved scope.  */
7555   parser->scope = saved_scope;
7556   parser->qualifying_scope = saved_qualifying_scope;
7557   parser->object_scope = saved_object_scope;
7558   /* If the TYPE is invalid, indicate failure.  */
7559   if (type == error_mark_node)
7560     return error_mark_node;
7561   return mangle_conv_op_name_for_type (type);
7562 }
7563
7564 /* Parse a conversion-type-id:
7565
7566    conversion-type-id:
7567      type-specifier-seq conversion-declarator [opt]
7568
7569    Returns the TYPE specified.  */
7570
7571 static tree
7572 cp_parser_conversion_type_id (cp_parser* parser)
7573 {
7574   tree attributes;
7575   cp_decl_specifier_seq type_specifiers;
7576   cp_declarator *declarator;
7577
7578   /* Parse the attributes.  */
7579   attributes = cp_parser_attributes_opt (parser);
7580   /* Parse the type-specifiers.  */
7581   cp_parser_type_specifier_seq (parser, &type_specifiers);
7582   /* If that didn't work, stop.  */
7583   if (type_specifiers.type == error_mark_node)
7584     return error_mark_node;
7585   /* Parse the conversion-declarator.  */
7586   declarator = cp_parser_conversion_declarator_opt (parser);
7587
7588   return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7589                          /*initialized=*/0, &attributes);
7590 }
7591
7592 /* Parse an (optional) conversion-declarator.
7593
7594    conversion-declarator:
7595      ptr-operator conversion-declarator [opt]
7596
7597    */
7598
7599 static cp_declarator *
7600 cp_parser_conversion_declarator_opt (cp_parser* parser)
7601 {
7602   enum tree_code code;
7603   tree class_type;
7604   cp_cv_quals cv_quals;
7605
7606   /* We don't know if there's a ptr-operator next, or not.  */
7607   cp_parser_parse_tentatively (parser);
7608   /* Try the ptr-operator.  */
7609   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7610   /* If it worked, look for more conversion-declarators.  */
7611   if (cp_parser_parse_definitely (parser))
7612     {
7613       cp_declarator *declarator;
7614
7615       /* Parse another optional declarator.  */
7616       declarator = cp_parser_conversion_declarator_opt (parser);
7617
7618       /* Create the representation of the declarator.  */
7619       if (class_type)
7620         declarator = make_ptrmem_declarator (cv_quals, class_type,
7621                                              declarator);
7622       else if (code == INDIRECT_REF)
7623         declarator = make_pointer_declarator (cv_quals, declarator);
7624       else
7625         declarator = make_reference_declarator (cv_quals, declarator);
7626
7627       return declarator;
7628    }
7629
7630   return NULL;
7631 }
7632
7633 /* Parse an (optional) ctor-initializer.
7634
7635    ctor-initializer:
7636      : mem-initializer-list
7637
7638    Returns TRUE iff the ctor-initializer was actually present.  */
7639
7640 static bool
7641 cp_parser_ctor_initializer_opt (cp_parser* parser)
7642 {
7643   /* If the next token is not a `:', then there is no
7644      ctor-initializer.  */
7645   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7646     {
7647       /* Do default initialization of any bases and members.  */
7648       if (DECL_CONSTRUCTOR_P (current_function_decl))
7649         finish_mem_initializers (NULL_TREE);
7650
7651       return false;
7652     }
7653
7654   /* Consume the `:' token.  */
7655   cp_lexer_consume_token (parser->lexer);
7656   /* And the mem-initializer-list.  */
7657   cp_parser_mem_initializer_list (parser);
7658
7659   return true;
7660 }
7661
7662 /* Parse a mem-initializer-list.
7663
7664    mem-initializer-list:
7665      mem-initializer
7666      mem-initializer , mem-initializer-list  */
7667
7668 static void
7669 cp_parser_mem_initializer_list (cp_parser* parser)
7670 {
7671   tree mem_initializer_list = NULL_TREE;
7672
7673   /* Let the semantic analysis code know that we are starting the
7674      mem-initializer-list.  */
7675   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7676     error ("only constructors take base initializers");
7677
7678   /* Loop through the list.  */
7679   while (true)
7680     {
7681       tree mem_initializer;
7682
7683       /* Parse the mem-initializer.  */
7684       mem_initializer = cp_parser_mem_initializer (parser);
7685       /* Add it to the list, unless it was erroneous.  */
7686       if (mem_initializer)
7687         {
7688           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7689           mem_initializer_list = mem_initializer;
7690         }
7691       /* If the next token is not a `,', we're done.  */
7692       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7693         break;
7694       /* Consume the `,' token.  */
7695       cp_lexer_consume_token (parser->lexer);
7696     }
7697
7698   /* Perform semantic analysis.  */
7699   if (DECL_CONSTRUCTOR_P (current_function_decl))
7700     finish_mem_initializers (mem_initializer_list);
7701 }
7702
7703 /* Parse a mem-initializer.
7704
7705    mem-initializer:
7706      mem-initializer-id ( expression-list [opt] )
7707
7708    GNU extension:
7709
7710    mem-initializer:
7711      ( expression-list [opt] )
7712
7713    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7714    class) or FIELD_DECL (for a non-static data member) to initialize;
7715    the TREE_VALUE is the expression-list.  */
7716
7717 static tree
7718 cp_parser_mem_initializer (cp_parser* parser)
7719 {
7720   tree mem_initializer_id;
7721   tree expression_list;
7722   tree member;
7723
7724   /* Find out what is being initialized.  */
7725   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7726     {
7727       pedwarn ("anachronistic old-style base class initializer");
7728       mem_initializer_id = NULL_TREE;
7729     }
7730   else
7731     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7732   member = expand_member_init (mem_initializer_id);
7733   if (member && !DECL_P (member))
7734     in_base_initializer = 1;
7735
7736   expression_list
7737     = cp_parser_parenthesized_expression_list (parser, false,
7738                                                /*non_constant_p=*/NULL);
7739   if (!expression_list)
7740     expression_list = void_type_node;
7741
7742   in_base_initializer = 0;
7743
7744   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7745 }
7746
7747 /* Parse a mem-initializer-id.
7748
7749    mem-initializer-id:
7750      :: [opt] nested-name-specifier [opt] class-name
7751      identifier
7752
7753    Returns a TYPE indicating the class to be initializer for the first
7754    production.  Returns an IDENTIFIER_NODE indicating the data member
7755    to be initialized for the second production.  */
7756
7757 static tree
7758 cp_parser_mem_initializer_id (cp_parser* parser)
7759 {
7760   bool global_scope_p;
7761   bool nested_name_specifier_p;
7762   bool template_p = false;
7763   tree id;
7764
7765   /* `typename' is not allowed in this context ([temp.res]).  */
7766   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7767     {
7768       error ("keyword `typename' not allowed in this context (a qualified "
7769              "member initializer is implicitly a type)");
7770       cp_lexer_consume_token (parser->lexer);
7771     }
7772   /* Look for the optional `::' operator.  */
7773   global_scope_p
7774     = (cp_parser_global_scope_opt (parser,
7775                                    /*current_scope_valid_p=*/false)
7776        != NULL_TREE);
7777   /* Look for the optional nested-name-specifier.  The simplest way to
7778      implement:
7779
7780        [temp.res]
7781
7782        The keyword `typename' is not permitted in a base-specifier or
7783        mem-initializer; in these contexts a qualified name that
7784        depends on a template-parameter is implicitly assumed to be a
7785        type name.
7786
7787      is to assume that we have seen the `typename' keyword at this
7788      point.  */
7789   nested_name_specifier_p
7790     = (cp_parser_nested_name_specifier_opt (parser,
7791                                             /*typename_keyword_p=*/true,
7792                                             /*check_dependency_p=*/true,
7793                                             /*type_p=*/true,
7794                                             /*is_declaration=*/true)
7795        != NULL_TREE);
7796   if (nested_name_specifier_p)
7797     template_p = cp_parser_optional_template_keyword (parser);
7798   /* If there is a `::' operator or a nested-name-specifier, then we
7799      are definitely looking for a class-name.  */
7800   if (global_scope_p || nested_name_specifier_p)
7801     return cp_parser_class_name (parser,
7802                                  /*typename_keyword_p=*/true,
7803                                  /*template_keyword_p=*/template_p,
7804                                  /*type_p=*/false,
7805                                  /*check_dependency_p=*/true,
7806                                  /*class_head_p=*/false,
7807                                  /*is_declaration=*/true);
7808   /* Otherwise, we could also be looking for an ordinary identifier.  */
7809   cp_parser_parse_tentatively (parser);
7810   /* Try a class-name.  */
7811   id = cp_parser_class_name (parser,
7812                              /*typename_keyword_p=*/true,
7813                              /*template_keyword_p=*/false,
7814                              /*type_p=*/false,
7815                              /*check_dependency_p=*/true,
7816                              /*class_head_p=*/false,
7817                              /*is_declaration=*/true);
7818   /* If we found one, we're done.  */
7819   if (cp_parser_parse_definitely (parser))
7820     return id;
7821   /* Otherwise, look for an ordinary identifier.  */
7822   return cp_parser_identifier (parser);
7823 }
7824
7825 /* Overloading [gram.over] */
7826
7827 /* Parse an operator-function-id.
7828
7829    operator-function-id:
7830      operator operator
7831
7832    Returns an IDENTIFIER_NODE for the operator which is a
7833    human-readable spelling of the identifier, e.g., `operator +'.  */
7834
7835 static tree
7836 cp_parser_operator_function_id (cp_parser* parser)
7837 {
7838   /* Look for the `operator' keyword.  */
7839   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7840     return error_mark_node;
7841   /* And then the name of the operator itself.  */
7842   return cp_parser_operator (parser);
7843 }
7844
7845 /* Parse an operator.
7846
7847    operator:
7848      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7849      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7850      || ++ -- , ->* -> () []
7851
7852    GNU Extensions:
7853
7854    operator:
7855      <? >? <?= >?=
7856
7857    Returns an IDENTIFIER_NODE for the operator which is a
7858    human-readable spelling of the identifier, e.g., `operator +'.  */
7859
7860 static tree
7861 cp_parser_operator (cp_parser* parser)
7862 {
7863   tree id = NULL_TREE;
7864   cp_token *token;
7865
7866   /* Peek at the next token.  */
7867   token = cp_lexer_peek_token (parser->lexer);
7868   /* Figure out which operator we have.  */
7869   switch (token->type)
7870     {
7871     case CPP_KEYWORD:
7872       {
7873         enum tree_code op;
7874
7875         /* The keyword should be either `new' or `delete'.  */
7876         if (token->keyword == RID_NEW)
7877           op = NEW_EXPR;
7878         else if (token->keyword == RID_DELETE)
7879           op = DELETE_EXPR;
7880         else
7881           break;
7882
7883         /* Consume the `new' or `delete' token.  */
7884         cp_lexer_consume_token (parser->lexer);
7885
7886         /* Peek at the next token.  */
7887         token = cp_lexer_peek_token (parser->lexer);
7888         /* If it's a `[' token then this is the array variant of the
7889            operator.  */
7890         if (token->type == CPP_OPEN_SQUARE)
7891           {
7892             /* Consume the `[' token.  */
7893             cp_lexer_consume_token (parser->lexer);
7894             /* Look for the `]' token.  */
7895             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7896             id = ansi_opname (op == NEW_EXPR
7897                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7898           }
7899         /* Otherwise, we have the non-array variant.  */
7900         else
7901           id = ansi_opname (op);
7902
7903         return id;
7904       }
7905
7906     case CPP_PLUS:
7907       id = ansi_opname (PLUS_EXPR);
7908       break;
7909
7910     case CPP_MINUS:
7911       id = ansi_opname (MINUS_EXPR);
7912       break;
7913
7914     case CPP_MULT:
7915       id = ansi_opname (MULT_EXPR);
7916       break;
7917
7918     case CPP_DIV:
7919       id = ansi_opname (TRUNC_DIV_EXPR);
7920       break;
7921
7922     case CPP_MOD:
7923       id = ansi_opname (TRUNC_MOD_EXPR);
7924       break;
7925
7926     case CPP_XOR:
7927       id = ansi_opname (BIT_XOR_EXPR);
7928       break;
7929
7930     case CPP_AND:
7931       id = ansi_opname (BIT_AND_EXPR);
7932       break;
7933
7934     case CPP_OR:
7935       id = ansi_opname (BIT_IOR_EXPR);
7936       break;
7937
7938     case CPP_COMPL:
7939       id = ansi_opname (BIT_NOT_EXPR);
7940       break;
7941
7942     case CPP_NOT:
7943       id = ansi_opname (TRUTH_NOT_EXPR);
7944       break;
7945
7946     case CPP_EQ:
7947       id = ansi_assopname (NOP_EXPR);
7948       break;
7949
7950     case CPP_LESS:
7951       id = ansi_opname (LT_EXPR);
7952       break;
7953
7954     case CPP_GREATER:
7955       id = ansi_opname (GT_EXPR);
7956       break;
7957
7958     case CPP_PLUS_EQ:
7959       id = ansi_assopname (PLUS_EXPR);
7960       break;
7961
7962     case CPP_MINUS_EQ:
7963       id = ansi_assopname (MINUS_EXPR);
7964       break;
7965
7966     case CPP_MULT_EQ:
7967       id = ansi_assopname (MULT_EXPR);
7968       break;
7969
7970     case CPP_DIV_EQ:
7971       id = ansi_assopname (TRUNC_DIV_EXPR);
7972       break;
7973
7974     case CPP_MOD_EQ:
7975       id = ansi_assopname (TRUNC_MOD_EXPR);
7976       break;
7977
7978     case CPP_XOR_EQ:
7979       id = ansi_assopname (BIT_XOR_EXPR);
7980       break;
7981
7982     case CPP_AND_EQ:
7983       id = ansi_assopname (BIT_AND_EXPR);
7984       break;
7985
7986     case CPP_OR_EQ:
7987       id = ansi_assopname (BIT_IOR_EXPR);
7988       break;
7989
7990     case CPP_LSHIFT:
7991       id = ansi_opname (LSHIFT_EXPR);
7992       break;
7993
7994     case CPP_RSHIFT:
7995       id = ansi_opname (RSHIFT_EXPR);
7996       break;
7997
7998     case CPP_LSHIFT_EQ:
7999       id = ansi_assopname (LSHIFT_EXPR);
8000       break;
8001
8002     case CPP_RSHIFT_EQ:
8003       id = ansi_assopname (RSHIFT_EXPR);
8004       break;
8005
8006     case CPP_EQ_EQ:
8007       id = ansi_opname (EQ_EXPR);
8008       break;
8009
8010     case CPP_NOT_EQ:
8011       id = ansi_opname (NE_EXPR);
8012       break;
8013
8014     case CPP_LESS_EQ:
8015       id = ansi_opname (LE_EXPR);
8016       break;
8017
8018     case CPP_GREATER_EQ:
8019       id = ansi_opname (GE_EXPR);
8020       break;
8021
8022     case CPP_AND_AND:
8023       id = ansi_opname (TRUTH_ANDIF_EXPR);
8024       break;
8025
8026     case CPP_OR_OR:
8027       id = ansi_opname (TRUTH_ORIF_EXPR);
8028       break;
8029
8030     case CPP_PLUS_PLUS:
8031       id = ansi_opname (POSTINCREMENT_EXPR);
8032       break;
8033
8034     case CPP_MINUS_MINUS:
8035       id = ansi_opname (PREDECREMENT_EXPR);
8036       break;
8037
8038     case CPP_COMMA:
8039       id = ansi_opname (COMPOUND_EXPR);
8040       break;
8041
8042     case CPP_DEREF_STAR:
8043       id = ansi_opname (MEMBER_REF);
8044       break;
8045
8046     case CPP_DEREF:
8047       id = ansi_opname (COMPONENT_REF);
8048       break;
8049
8050     case CPP_OPEN_PAREN:
8051       /* Consume the `('.  */
8052       cp_lexer_consume_token (parser->lexer);
8053       /* Look for the matching `)'.  */
8054       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8055       return ansi_opname (CALL_EXPR);
8056
8057     case CPP_OPEN_SQUARE:
8058       /* Consume the `['.  */
8059       cp_lexer_consume_token (parser->lexer);
8060       /* Look for the matching `]'.  */
8061       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8062       return ansi_opname (ARRAY_REF);
8063
8064       /* Extensions.  */
8065     case CPP_MIN:
8066       id = ansi_opname (MIN_EXPR);
8067       break;
8068
8069     case CPP_MAX:
8070       id = ansi_opname (MAX_EXPR);
8071       break;
8072
8073     case CPP_MIN_EQ:
8074       id = ansi_assopname (MIN_EXPR);
8075       break;
8076
8077     case CPP_MAX_EQ:
8078       id = ansi_assopname (MAX_EXPR);
8079       break;
8080
8081     default:
8082       /* Anything else is an error.  */
8083       break;
8084     }
8085
8086   /* If we have selected an identifier, we need to consume the
8087      operator token.  */
8088   if (id)
8089     cp_lexer_consume_token (parser->lexer);
8090   /* Otherwise, no valid operator name was present.  */
8091   else
8092     {
8093       cp_parser_error (parser, "expected operator");
8094       id = error_mark_node;
8095     }
8096
8097   return id;
8098 }
8099
8100 /* Parse a template-declaration.
8101
8102    template-declaration:
8103      export [opt] template < template-parameter-list > declaration
8104
8105    If MEMBER_P is TRUE, this template-declaration occurs within a
8106    class-specifier.
8107
8108    The grammar rule given by the standard isn't correct.  What
8109    is really meant is:
8110
8111    template-declaration:
8112      export [opt] template-parameter-list-seq
8113        decl-specifier-seq [opt] init-declarator [opt] ;
8114      export [opt] template-parameter-list-seq
8115        function-definition
8116
8117    template-parameter-list-seq:
8118      template-parameter-list-seq [opt]
8119      template < template-parameter-list >  */
8120
8121 static void
8122 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8123 {
8124   /* Check for `export'.  */
8125   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8126     {
8127       /* Consume the `export' token.  */
8128       cp_lexer_consume_token (parser->lexer);
8129       /* Warn that we do not support `export'.  */
8130       warning ("keyword `export' not implemented, and will be ignored");
8131     }
8132
8133   cp_parser_template_declaration_after_export (parser, member_p);
8134 }
8135
8136 /* Parse a template-parameter-list.
8137
8138    template-parameter-list:
8139      template-parameter
8140      template-parameter-list , template-parameter
8141
8142    Returns a TREE_LIST.  Each node represents a template parameter.
8143    The nodes are connected via their TREE_CHAINs.  */
8144
8145 static tree
8146 cp_parser_template_parameter_list (cp_parser* parser)
8147 {
8148   tree parameter_list = NULL_TREE;
8149
8150   while (true)
8151     {
8152       tree parameter;
8153       cp_token *token;
8154       bool is_non_type;
8155
8156       /* Parse the template-parameter.  */
8157       parameter = cp_parser_template_parameter (parser, &is_non_type);
8158       /* Add it to the list.  */
8159       parameter_list = process_template_parm (parameter_list,
8160                                               parameter,
8161                                               is_non_type);
8162       /* Peek at the next token.  */
8163       token = cp_lexer_peek_token (parser->lexer);
8164       /* If it's not a `,', we're done.  */
8165       if (token->type != CPP_COMMA)
8166         break;
8167       /* Otherwise, consume the `,' token.  */
8168       cp_lexer_consume_token (parser->lexer);
8169     }
8170
8171   return parameter_list;
8172 }
8173
8174 /* Parse a template-parameter.
8175
8176    template-parameter:
8177      type-parameter
8178      parameter-declaration
8179
8180    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8181    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8182    true iff this parameter is a non-type parameter.  */
8183
8184 static tree
8185 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8186 {
8187   cp_token *token;
8188   cp_parameter_declarator *parameter_declarator;
8189
8190   /* Assume it is a type parameter or a template parameter.  */
8191   *is_non_type = false;
8192   /* Peek at the next token.  */
8193   token = cp_lexer_peek_token (parser->lexer);
8194   /* If it is `class' or `template', we have a type-parameter.  */
8195   if (token->keyword == RID_TEMPLATE)
8196     return cp_parser_type_parameter (parser);
8197   /* If it is `class' or `typename' we do not know yet whether it is a
8198      type parameter or a non-type parameter.  Consider:
8199
8200        template <typename T, typename T::X X> ...
8201
8202      or:
8203
8204        template <class C, class D*> ...
8205
8206      Here, the first parameter is a type parameter, and the second is
8207      a non-type parameter.  We can tell by looking at the token after
8208      the identifier -- if it is a `,', `=', or `>' then we have a type
8209      parameter.  */
8210   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8211     {
8212       /* Peek at the token after `class' or `typename'.  */
8213       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8214       /* If it's an identifier, skip it.  */
8215       if (token->type == CPP_NAME)
8216         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8217       /* Now, see if the token looks like the end of a template
8218          parameter.  */
8219       if (token->type == CPP_COMMA
8220           || token->type == CPP_EQ
8221           || token->type == CPP_GREATER)
8222         return cp_parser_type_parameter (parser);
8223     }
8224
8225   /* Otherwise, it is a non-type parameter.
8226
8227      [temp.param]
8228
8229      When parsing a default template-argument for a non-type
8230      template-parameter, the first non-nested `>' is taken as the end
8231      of the template parameter-list rather than a greater-than
8232      operator.  */
8233   *is_non_type = true;
8234   parameter_declarator
8235      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8236                                         /*parenthesized_p=*/NULL);
8237   return (build_tree_list
8238           (parameter_declarator->default_argument,
8239            grokdeclarator (parameter_declarator->declarator,
8240                            &parameter_declarator->decl_specifiers,
8241                            PARM, /*initialized=*/0,
8242                            /*attrlist=*/NULL)));
8243 }
8244
8245 /* Parse a type-parameter.
8246
8247    type-parameter:
8248      class identifier [opt]
8249      class identifier [opt] = type-id
8250      typename identifier [opt]
8251      typename identifier [opt] = type-id
8252      template < template-parameter-list > class identifier [opt]
8253      template < template-parameter-list > class identifier [opt]
8254        = id-expression
8255
8256    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8257    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8258    the declaration of the parameter.  */
8259
8260 static tree
8261 cp_parser_type_parameter (cp_parser* parser)
8262 {
8263   cp_token *token;
8264   tree parameter;
8265
8266   /* Look for a keyword to tell us what kind of parameter this is.  */
8267   token = cp_parser_require (parser, CPP_KEYWORD,
8268                              "`class', `typename', or `template'");
8269   if (!token)
8270     return error_mark_node;
8271
8272   switch (token->keyword)
8273     {
8274     case RID_CLASS:
8275     case RID_TYPENAME:
8276       {
8277         tree identifier;
8278         tree default_argument;
8279
8280         /* If the next token is an identifier, then it names the
8281            parameter.  */
8282         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8283           identifier = cp_parser_identifier (parser);
8284         else
8285           identifier = NULL_TREE;
8286
8287         /* Create the parameter.  */
8288         parameter = finish_template_type_parm (class_type_node, identifier);
8289
8290         /* If the next token is an `=', we have a default argument.  */
8291         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8292           {
8293             /* Consume the `=' token.  */
8294             cp_lexer_consume_token (parser->lexer);
8295             /* Parse the default-argument.  */
8296             default_argument = cp_parser_type_id (parser);
8297           }
8298         else
8299           default_argument = NULL_TREE;
8300
8301         /* Create the combined representation of the parameter and the
8302            default argument.  */
8303         parameter = build_tree_list (default_argument, parameter);
8304       }
8305       break;
8306
8307     case RID_TEMPLATE:
8308       {
8309         tree parameter_list;
8310         tree identifier;
8311         tree default_argument;
8312
8313         /* Look for the `<'.  */
8314         cp_parser_require (parser, CPP_LESS, "`<'");
8315         /* Parse the template-parameter-list.  */
8316         begin_template_parm_list ();
8317         parameter_list
8318           = cp_parser_template_parameter_list (parser);
8319         parameter_list = end_template_parm_list (parameter_list);
8320         /* Look for the `>'.  */
8321         cp_parser_require (parser, CPP_GREATER, "`>'");
8322         /* Look for the `class' keyword.  */
8323         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8324         /* If the next token is an `=', then there is a
8325            default-argument.  If the next token is a `>', we are at
8326            the end of the parameter-list.  If the next token is a `,',
8327            then we are at the end of this parameter.  */
8328         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8329             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8330             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8331           identifier = cp_parser_identifier (parser);
8332         else
8333           identifier = NULL_TREE;
8334         /* Create the template parameter.  */
8335         parameter = finish_template_template_parm (class_type_node,
8336                                                    identifier);
8337
8338         /* If the next token is an `=', then there is a
8339            default-argument.  */
8340         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8341           {
8342             bool is_template;
8343
8344             /* Consume the `='.  */
8345             cp_lexer_consume_token (parser->lexer);
8346             /* Parse the id-expression.  */
8347             default_argument
8348               = cp_parser_id_expression (parser,
8349                                          /*template_keyword_p=*/false,
8350                                          /*check_dependency_p=*/true,
8351                                          /*template_p=*/&is_template,
8352                                          /*declarator_p=*/false);
8353             if (TREE_CODE (default_argument) == TYPE_DECL)
8354               /* If the id-expression was a template-id that refers to
8355                  a template-class, we already have the declaration here,
8356                  so no further lookup is needed.  */
8357                  ;
8358             else
8359               /* Look up the name.  */
8360               default_argument
8361                 = cp_parser_lookup_name (parser, default_argument,
8362                                         /*is_type=*/false,
8363                                         /*is_template=*/is_template,
8364                                         /*is_namespace=*/false,
8365                                         /*check_dependency=*/true,
8366                                         /*ambiguous_p=*/NULL);
8367             /* See if the default argument is valid.  */
8368             default_argument
8369               = check_template_template_default_arg (default_argument);
8370           }
8371         else
8372           default_argument = NULL_TREE;
8373
8374         /* Create the combined representation of the parameter and the
8375            default argument.  */
8376         parameter =  build_tree_list (default_argument, parameter);
8377       }
8378       break;
8379
8380     default:
8381       /* Anything else is an error.  */
8382       cp_parser_error (parser,
8383                        "expected `class', `typename', or `template'");
8384       parameter = error_mark_node;
8385     }
8386
8387   return parameter;
8388 }
8389
8390 /* Parse a template-id.
8391
8392    template-id:
8393      template-name < template-argument-list [opt] >
8394
8395    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8396    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8397    returned.  Otherwise, if the template-name names a function, or set
8398    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8399    names a class, returns a TYPE_DECL for the specialization.
8400
8401    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8402    uninstantiated templates.  */
8403
8404 static tree
8405 cp_parser_template_id (cp_parser *parser,
8406                        bool template_keyword_p,
8407                        bool check_dependency_p,
8408                        bool is_declaration)
8409 {
8410   tree template;
8411   tree arguments;
8412   tree template_id;
8413   ptrdiff_t start_of_id;
8414   tree access_check = NULL_TREE;
8415   cp_token *next_token, *next_token_2;
8416   bool is_identifier;
8417
8418   /* If the next token corresponds to a template-id, there is no need
8419      to reparse it.  */
8420   next_token = cp_lexer_peek_token (parser->lexer);
8421   if (next_token->type == CPP_TEMPLATE_ID)
8422     {
8423       tree value;
8424       tree check;
8425
8426       /* Get the stored value.  */
8427       value = cp_lexer_consume_token (parser->lexer)->value;
8428       /* Perform any access checks that were deferred.  */
8429       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8430         perform_or_defer_access_check (TREE_PURPOSE (check),
8431                                        TREE_VALUE (check));
8432       /* Return the stored value.  */
8433       return TREE_VALUE (value);
8434     }
8435
8436   /* Avoid performing name lookup if there is no possibility of
8437      finding a template-id.  */
8438   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8439       || (next_token->type == CPP_NAME
8440           && !cp_parser_nth_token_starts_template_argument_list_p
8441                (parser, 2)))
8442     {
8443       cp_parser_error (parser, "expected template-id");
8444       return error_mark_node;
8445     }
8446
8447   /* Remember where the template-id starts.  */
8448   if (cp_parser_parsing_tentatively (parser)
8449       && !cp_parser_committed_to_tentative_parse (parser))
8450     {
8451       next_token = cp_lexer_peek_token (parser->lexer);
8452       start_of_id = cp_lexer_token_difference (parser->lexer,
8453                                                parser->lexer->first_token,
8454                                                next_token);
8455     }
8456   else
8457     start_of_id = -1;
8458
8459   push_deferring_access_checks (dk_deferred);
8460
8461   /* Parse the template-name.  */
8462   is_identifier = false;
8463   template = cp_parser_template_name (parser, template_keyword_p,
8464                                       check_dependency_p,
8465                                       is_declaration,
8466                                       &is_identifier);
8467   if (template == error_mark_node || is_identifier)
8468     {
8469       pop_deferring_access_checks ();
8470       return template;
8471     }
8472
8473   /* If we find the sequence `[:' after a template-name, it's probably
8474      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8475      parse correctly the argument list.  */
8476   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8477   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8478   if (next_token->type == CPP_OPEN_SQUARE
8479       && next_token->flags & DIGRAPH
8480       && next_token_2->type == CPP_COLON
8481       && !(next_token_2->flags & PREV_WHITE))
8482     {
8483       cp_parser_parse_tentatively (parser);
8484       /* Change `:' into `::'.  */
8485       next_token_2->type = CPP_SCOPE;
8486       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8487          CPP_LESS.  */
8488       cp_lexer_consume_token (parser->lexer);
8489       /* Parse the arguments.  */
8490       arguments = cp_parser_enclosed_template_argument_list (parser);
8491       if (!cp_parser_parse_definitely (parser))
8492         {
8493           /* If we couldn't parse an argument list, then we revert our changes
8494              and return simply an error. Maybe this is not a template-id
8495              after all.  */
8496           next_token_2->type = CPP_COLON;
8497           cp_parser_error (parser, "expected `<'");
8498           pop_deferring_access_checks ();
8499           return error_mark_node;
8500         }
8501       /* Otherwise, emit an error about the invalid digraph, but continue
8502          parsing because we got our argument list.  */
8503       pedwarn ("`<::' cannot begin a template-argument list");
8504       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8505               "between `<' and `::'");
8506       if (!flag_permissive)
8507         {
8508           static bool hint;
8509           if (!hint)
8510             {
8511               inform ("(if you use `-fpermissive' G++ will accept your code)");
8512               hint = true;
8513             }
8514         }
8515     }
8516   else
8517     {
8518       /* Look for the `<' that starts the template-argument-list.  */
8519       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8520         {
8521           pop_deferring_access_checks ();
8522           return error_mark_node;
8523         }
8524       /* Parse the arguments.  */
8525       arguments = cp_parser_enclosed_template_argument_list (parser);
8526     }
8527
8528   /* Build a representation of the specialization.  */
8529   if (TREE_CODE (template) == IDENTIFIER_NODE)
8530     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8531   else if (DECL_CLASS_TEMPLATE_P (template)
8532            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8533     template_id
8534       = finish_template_type (template, arguments,
8535                               cp_lexer_next_token_is (parser->lexer,
8536                                                       CPP_SCOPE));
8537   else
8538     {
8539       /* If it's not a class-template or a template-template, it should be
8540          a function-template.  */
8541       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8542                    || TREE_CODE (template) == OVERLOAD
8543                    || BASELINK_P (template)));
8544
8545       template_id = lookup_template_function (template, arguments);
8546     }
8547
8548   /* Retrieve any deferred checks.  Do not pop this access checks yet
8549      so the memory will not be reclaimed during token replacing below.  */
8550   access_check = get_deferred_access_checks ();
8551
8552   /* If parsing tentatively, replace the sequence of tokens that makes
8553      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8554      should we re-parse the token stream, we will not have to repeat
8555      the effort required to do the parse, nor will we issue duplicate
8556      error messages about problems during instantiation of the
8557      template.  */
8558   if (start_of_id >= 0)
8559     {
8560       cp_token *token;
8561
8562       /* Find the token that corresponds to the start of the
8563          template-id.  */
8564       token = cp_lexer_advance_token (parser->lexer,
8565                                       parser->lexer->first_token,
8566                                       start_of_id);
8567
8568       /* Reset the contents of the START_OF_ID token.  */
8569       token->type = CPP_TEMPLATE_ID;
8570       token->value = build_tree_list (access_check, template_id);
8571       token->keyword = RID_MAX;
8572       /* Purge all subsequent tokens.  */
8573       cp_lexer_purge_tokens_after (parser->lexer, token);
8574     }
8575
8576   pop_deferring_access_checks ();
8577   return template_id;
8578 }
8579
8580 /* Parse a template-name.
8581
8582    template-name:
8583      identifier
8584
8585    The standard should actually say:
8586
8587    template-name:
8588      identifier
8589      operator-function-id
8590
8591    A defect report has been filed about this issue.
8592
8593    A conversion-function-id cannot be a template name because they cannot
8594    be part of a template-id. In fact, looking at this code:
8595
8596    a.operator K<int>()
8597
8598    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8599    It is impossible to call a templated conversion-function-id with an
8600    explicit argument list, since the only allowed template parameter is
8601    the type to which it is converting.
8602
8603    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8604    `template' keyword, in a construction like:
8605
8606      T::template f<3>()
8607
8608    In that case `f' is taken to be a template-name, even though there
8609    is no way of knowing for sure.
8610
8611    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8612    name refers to a set of overloaded functions, at least one of which
8613    is a template, or an IDENTIFIER_NODE with the name of the template,
8614    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8615    names are looked up inside uninstantiated templates.  */
8616
8617 static tree
8618 cp_parser_template_name (cp_parser* parser,
8619                          bool template_keyword_p,
8620                          bool check_dependency_p,
8621                          bool is_declaration,
8622                          bool *is_identifier)
8623 {
8624   tree identifier;
8625   tree decl;
8626   tree fns;
8627
8628   /* If the next token is `operator', then we have either an
8629      operator-function-id or a conversion-function-id.  */
8630   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8631     {
8632       /* We don't know whether we're looking at an
8633          operator-function-id or a conversion-function-id.  */
8634       cp_parser_parse_tentatively (parser);
8635       /* Try an operator-function-id.  */
8636       identifier = cp_parser_operator_function_id (parser);
8637       /* If that didn't work, try a conversion-function-id.  */
8638       if (!cp_parser_parse_definitely (parser))
8639         {
8640           cp_parser_error (parser, "expected template-name");
8641           return error_mark_node;
8642         }
8643     }
8644   /* Look for the identifier.  */
8645   else
8646     identifier = cp_parser_identifier (parser);
8647
8648   /* If we didn't find an identifier, we don't have a template-id.  */
8649   if (identifier == error_mark_node)
8650     return error_mark_node;
8651
8652   /* If the name immediately followed the `template' keyword, then it
8653      is a template-name.  However, if the next token is not `<', then
8654      we do not treat it as a template-name, since it is not being used
8655      as part of a template-id.  This enables us to handle constructs
8656      like:
8657
8658        template <typename T> struct S { S(); };
8659        template <typename T> S<T>::S();
8660
8661      correctly.  We would treat `S' as a template -- if it were `S<T>'
8662      -- but we do not if there is no `<'.  */
8663
8664   if (processing_template_decl
8665       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8666     {
8667       /* In a declaration, in a dependent context, we pretend that the
8668          "template" keyword was present in order to improve error
8669          recovery.  For example, given:
8670
8671            template <typename T> void f(T::X<int>);
8672
8673          we want to treat "X<int>" as a template-id.  */
8674       if (is_declaration
8675           && !template_keyword_p
8676           && parser->scope && TYPE_P (parser->scope)
8677           && dependent_type_p (parser->scope)
8678           /* Do not do this for dtors (or ctors), since they never
8679              need the template keyword before their name.  */
8680           && !constructor_name_p (identifier, parser->scope))
8681         {
8682           ptrdiff_t start;
8683           cp_token* token;
8684           /* Explain what went wrong.  */
8685           error ("non-template `%D' used as template", identifier);
8686           inform ("use `%T::template %D' to indicate that it is a template",
8687                   parser->scope, identifier);
8688           /* If parsing tentatively, find the location of the "<"
8689              token.  */
8690           if (cp_parser_parsing_tentatively (parser)
8691               && !cp_parser_committed_to_tentative_parse (parser))
8692             {
8693               cp_parser_simulate_error (parser);
8694               token = cp_lexer_peek_token (parser->lexer);
8695               token = cp_lexer_prev_token (parser->lexer, token);
8696               start = cp_lexer_token_difference (parser->lexer,
8697                                                  parser->lexer->first_token,
8698                                                  token);
8699             }
8700           else
8701             start = -1;
8702           /* Parse the template arguments so that we can issue error
8703              messages about them.  */
8704           cp_lexer_consume_token (parser->lexer);
8705           cp_parser_enclosed_template_argument_list (parser);
8706           /* Skip tokens until we find a good place from which to
8707              continue parsing.  */
8708           cp_parser_skip_to_closing_parenthesis (parser,
8709                                                  /*recovering=*/true,
8710                                                  /*or_comma=*/true,
8711                                                  /*consume_paren=*/false);
8712           /* If parsing tentatively, permanently remove the
8713              template argument list.  That will prevent duplicate
8714              error messages from being issued about the missing
8715              "template" keyword.  */
8716           if (start >= 0)
8717             {
8718               token = cp_lexer_advance_token (parser->lexer,
8719                                               parser->lexer->first_token,
8720                                               start);
8721               cp_lexer_purge_tokens_after (parser->lexer, token);
8722             }
8723           if (is_identifier)
8724             *is_identifier = true;
8725           return identifier;
8726         }
8727
8728       /* If the "template" keyword is present, then there is generally
8729          no point in doing name-lookup, so we just return IDENTIFIER.
8730          But, if the qualifying scope is non-dependent then we can
8731          (and must) do name-lookup normally.  */
8732       if (template_keyword_p
8733           && (!parser->scope
8734               || (TYPE_P (parser->scope)
8735                   && dependent_type_p (parser->scope))))
8736         return identifier;
8737     }
8738
8739   /* Look up the name.  */
8740   decl = cp_parser_lookup_name (parser, identifier,
8741                                 /*is_type=*/false,
8742                                 /*is_template=*/false,
8743                                 /*is_namespace=*/false,
8744                                 check_dependency_p,
8745                                 /*ambiguous_p=*/NULL);
8746   decl = maybe_get_template_decl_from_type_decl (decl);
8747
8748   /* If DECL is a template, then the name was a template-name.  */
8749   if (TREE_CODE (decl) == TEMPLATE_DECL)
8750     ;
8751   else
8752     {
8753       /* The standard does not explicitly indicate whether a name that
8754          names a set of overloaded declarations, some of which are
8755          templates, is a template-name.  However, such a name should
8756          be a template-name; otherwise, there is no way to form a
8757          template-id for the overloaded templates.  */
8758       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8759       if (TREE_CODE (fns) == OVERLOAD)
8760         {
8761           tree fn;
8762
8763           for (fn = fns; fn; fn = OVL_NEXT (fn))
8764             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8765               break;
8766         }
8767       else
8768         {
8769           /* Otherwise, the name does not name a template.  */
8770           cp_parser_error (parser, "expected template-name");
8771           return error_mark_node;
8772         }
8773     }
8774
8775   /* If DECL is dependent, and refers to a function, then just return
8776      its name; we will look it up again during template instantiation.  */
8777   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8778     {
8779       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8780       if (TYPE_P (scope) && dependent_type_p (scope))
8781         return identifier;
8782     }
8783
8784   return decl;
8785 }
8786
8787 /* Parse a template-argument-list.
8788
8789    template-argument-list:
8790      template-argument
8791      template-argument-list , template-argument
8792
8793    Returns a TREE_VEC containing the arguments.  */
8794
8795 static tree
8796 cp_parser_template_argument_list (cp_parser* parser)
8797 {
8798   tree fixed_args[10];
8799   unsigned n_args = 0;
8800   unsigned alloced = 10;
8801   tree *arg_ary = fixed_args;
8802   tree vec;
8803   bool saved_in_template_argument_list_p;
8804
8805   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8806   parser->in_template_argument_list_p = true;
8807   do
8808     {
8809       tree argument;
8810
8811       if (n_args)
8812         /* Consume the comma.  */
8813         cp_lexer_consume_token (parser->lexer);
8814
8815       /* Parse the template-argument.  */
8816       argument = cp_parser_template_argument (parser);
8817       if (n_args == alloced)
8818         {
8819           alloced *= 2;
8820
8821           if (arg_ary == fixed_args)
8822             {
8823               arg_ary = xmalloc (sizeof (tree) * alloced);
8824               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8825             }
8826           else
8827             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8828         }
8829       arg_ary[n_args++] = argument;
8830     }
8831   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8832
8833   vec = make_tree_vec (n_args);
8834
8835   while (n_args--)
8836     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8837
8838   if (arg_ary != fixed_args)
8839     free (arg_ary);
8840   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8841   return vec;
8842 }
8843
8844 /* Parse a template-argument.
8845
8846    template-argument:
8847      assignment-expression
8848      type-id
8849      id-expression
8850
8851    The representation is that of an assignment-expression, type-id, or
8852    id-expression -- except that the qualified id-expression is
8853    evaluated, so that the value returned is either a DECL or an
8854    OVERLOAD.
8855
8856    Although the standard says "assignment-expression", it forbids
8857    throw-expressions or assignments in the template argument.
8858    Therefore, we use "conditional-expression" instead.  */
8859
8860 static tree
8861 cp_parser_template_argument (cp_parser* parser)
8862 {
8863   tree argument;
8864   bool template_p;
8865   bool address_p;
8866   bool maybe_type_id = false;
8867   cp_token *token;
8868   cp_id_kind idk;
8869   tree qualifying_class;
8870
8871   /* There's really no way to know what we're looking at, so we just
8872      try each alternative in order.
8873
8874        [temp.arg]
8875
8876        In a template-argument, an ambiguity between a type-id and an
8877        expression is resolved to a type-id, regardless of the form of
8878        the corresponding template-parameter.
8879
8880      Therefore, we try a type-id first.  */
8881   cp_parser_parse_tentatively (parser);
8882   argument = cp_parser_type_id (parser);
8883   /* If there was no error parsing the type-id but the next token is a '>>',
8884      we probably found a typo for '> >'. But there are type-id which are
8885      also valid expressions. For instance:
8886
8887      struct X { int operator >> (int); };
8888      template <int V> struct Foo {};
8889      Foo<X () >> 5> r;
8890
8891      Here 'X()' is a valid type-id of a function type, but the user just
8892      wanted to write the expression "X() >> 5". Thus, we remember that we
8893      found a valid type-id, but we still try to parse the argument as an
8894      expression to see what happens.  */
8895   if (!cp_parser_error_occurred (parser)
8896       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8897     {
8898       maybe_type_id = true;
8899       cp_parser_abort_tentative_parse (parser);
8900     }
8901   else
8902     {
8903       /* If the next token isn't a `,' or a `>', then this argument wasn't
8904       really finished. This means that the argument is not a valid
8905       type-id.  */
8906       if (!cp_parser_next_token_ends_template_argument_p (parser))
8907         cp_parser_error (parser, "expected template-argument");
8908       /* If that worked, we're done.  */
8909       if (cp_parser_parse_definitely (parser))
8910         return argument;
8911     }
8912   /* We're still not sure what the argument will be.  */
8913   cp_parser_parse_tentatively (parser);
8914   /* Try a template.  */
8915   argument = cp_parser_id_expression (parser,
8916                                       /*template_keyword_p=*/false,
8917                                       /*check_dependency_p=*/true,
8918                                       &template_p,
8919                                       /*declarator_p=*/false);
8920   /* If the next token isn't a `,' or a `>', then this argument wasn't
8921      really finished.  */
8922   if (!cp_parser_next_token_ends_template_argument_p (parser))
8923     cp_parser_error (parser, "expected template-argument");
8924   if (!cp_parser_error_occurred (parser))
8925     {
8926       /* Figure out what is being referred to.  If the id-expression
8927          was for a class template specialization, then we will have a
8928          TYPE_DECL at this point.  There is no need to do name lookup
8929          at this point in that case.  */
8930       if (TREE_CODE (argument) != TYPE_DECL)
8931         argument = cp_parser_lookup_name (parser, argument,
8932                                           /*is_type=*/false,
8933                                           /*is_template=*/template_p,
8934                                           /*is_namespace=*/false,
8935                                           /*check_dependency=*/true,
8936                                           /*ambiguous_p=*/NULL);
8937       if (TREE_CODE (argument) != TEMPLATE_DECL
8938           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8939         cp_parser_error (parser, "expected template-name");
8940     }
8941   if (cp_parser_parse_definitely (parser))
8942     return argument;
8943   /* It must be a non-type argument.  There permitted cases are given
8944      in [temp.arg.nontype]:
8945
8946      -- an integral constant-expression of integral or enumeration
8947         type; or
8948
8949      -- the name of a non-type template-parameter; or
8950
8951      -- the name of an object or function with external linkage...
8952
8953      -- the address of an object or function with external linkage...
8954
8955      -- a pointer to member...  */
8956   /* Look for a non-type template parameter.  */
8957   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8958     {
8959       cp_parser_parse_tentatively (parser);
8960       argument = cp_parser_primary_expression (parser,
8961                                                &idk,
8962                                                &qualifying_class);
8963       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8964           || !cp_parser_next_token_ends_template_argument_p (parser))
8965         cp_parser_simulate_error (parser);
8966       if (cp_parser_parse_definitely (parser))
8967         return argument;
8968     }
8969   /* If the next token is "&", the argument must be the address of an
8970      object or function with external linkage.  */
8971   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8972   if (address_p)
8973     cp_lexer_consume_token (parser->lexer);
8974   /* See if we might have an id-expression.  */
8975   token = cp_lexer_peek_token (parser->lexer);
8976   if (token->type == CPP_NAME
8977       || token->keyword == RID_OPERATOR
8978       || token->type == CPP_SCOPE
8979       || token->type == CPP_TEMPLATE_ID
8980       || token->type == CPP_NESTED_NAME_SPECIFIER)
8981     {
8982       cp_parser_parse_tentatively (parser);
8983       argument = cp_parser_primary_expression (parser,
8984                                                &idk,
8985                                                &qualifying_class);
8986       if (cp_parser_error_occurred (parser)
8987           || !cp_parser_next_token_ends_template_argument_p (parser))
8988         cp_parser_abort_tentative_parse (parser);
8989       else
8990         {
8991           if (qualifying_class)
8992             argument = finish_qualified_id_expr (qualifying_class,
8993                                                  argument,
8994                                                  /*done=*/true,
8995                                                  address_p);
8996           if (TREE_CODE (argument) == VAR_DECL)
8997             {
8998               /* A variable without external linkage might still be a
8999                  valid constant-expression, so no error is issued here
9000                  if the external-linkage check fails.  */
9001               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9002                 cp_parser_simulate_error (parser);
9003             }
9004           else if (is_overloaded_fn (argument))
9005             /* All overloaded functions are allowed; if the external
9006                linkage test does not pass, an error will be issued
9007                later.  */
9008             ;
9009           else if (address_p
9010                    && (TREE_CODE (argument) == OFFSET_REF
9011                        || TREE_CODE (argument) == SCOPE_REF))
9012             /* A pointer-to-member.  */
9013             ;
9014           else
9015             cp_parser_simulate_error (parser);
9016
9017           if (cp_parser_parse_definitely (parser))
9018             {
9019               if (address_p)
9020                 argument = build_x_unary_op (ADDR_EXPR, argument);
9021               return argument;
9022             }
9023         }
9024     }
9025   /* If the argument started with "&", there are no other valid
9026      alternatives at this point.  */
9027   if (address_p)
9028     {
9029       cp_parser_error (parser, "invalid non-type template argument");
9030       return error_mark_node;
9031     }
9032   /* If the argument wasn't successfully parsed as a type-id followed
9033      by '>>', the argument can only be a constant expression now.
9034      Otherwise, we try parsing the constant-expression tentatively,
9035      because the argument could really be a type-id.  */
9036   if (maybe_type_id)
9037     cp_parser_parse_tentatively (parser);
9038   argument = cp_parser_constant_expression (parser,
9039                                             /*allow_non_constant_p=*/false,
9040                                             /*non_constant_p=*/NULL);
9041   argument = fold_non_dependent_expr (argument);
9042   if (!maybe_type_id)
9043     return argument;
9044   if (!cp_parser_next_token_ends_template_argument_p (parser))
9045     cp_parser_error (parser, "expected template-argument");
9046   if (cp_parser_parse_definitely (parser))
9047     return argument;
9048   /* We did our best to parse the argument as a non type-id, but that
9049      was the only alternative that matched (albeit with a '>' after
9050      it). We can assume it's just a typo from the user, and a
9051      diagnostic will then be issued.  */
9052   return cp_parser_type_id (parser);
9053 }
9054
9055 /* Parse an explicit-instantiation.
9056
9057    explicit-instantiation:
9058      template declaration
9059
9060    Although the standard says `declaration', what it really means is:
9061
9062    explicit-instantiation:
9063      template decl-specifier-seq [opt] declarator [opt] ;
9064
9065    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9066    supposed to be allowed.  A defect report has been filed about this
9067    issue.
9068
9069    GNU Extension:
9070
9071    explicit-instantiation:
9072      storage-class-specifier template
9073        decl-specifier-seq [opt] declarator [opt] ;
9074      function-specifier template
9075        decl-specifier-seq [opt] declarator [opt] ;  */
9076
9077 static void
9078 cp_parser_explicit_instantiation (cp_parser* parser)
9079 {
9080   int declares_class_or_enum;
9081   cp_decl_specifier_seq decl_specifiers;
9082   tree extension_specifier = NULL_TREE;
9083
9084   /* Look for an (optional) storage-class-specifier or
9085      function-specifier.  */
9086   if (cp_parser_allow_gnu_extensions_p (parser))
9087     {
9088       extension_specifier
9089         = cp_parser_storage_class_specifier_opt (parser);
9090       if (!extension_specifier)
9091         extension_specifier
9092           = cp_parser_function_specifier_opt (parser,
9093                                               /*decl_specs=*/NULL);
9094     }
9095
9096   /* Look for the `template' keyword.  */
9097   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9098   /* Let the front end know that we are processing an explicit
9099      instantiation.  */
9100   begin_explicit_instantiation ();
9101   /* [temp.explicit] says that we are supposed to ignore access
9102      control while processing explicit instantiation directives.  */
9103   push_deferring_access_checks (dk_no_check);
9104   /* Parse a decl-specifier-seq.  */
9105   cp_parser_decl_specifier_seq (parser,
9106                                 CP_PARSER_FLAGS_OPTIONAL,
9107                                 &decl_specifiers,
9108                                 &declares_class_or_enum);
9109   /* If there was exactly one decl-specifier, and it declared a class,
9110      and there's no declarator, then we have an explicit type
9111      instantiation.  */
9112   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9113     {
9114       tree type;
9115
9116       type = check_tag_decl (&decl_specifiers);
9117       /* Turn access control back on for names used during
9118          template instantiation.  */
9119       pop_deferring_access_checks ();
9120       if (type)
9121         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9122     }
9123   else
9124     {
9125       cp_declarator *declarator;
9126       tree decl;
9127
9128       /* Parse the declarator.  */
9129       declarator
9130         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9131                                 /*ctor_dtor_or_conv_p=*/NULL,
9132                                 /*parenthesized_p=*/NULL);
9133       cp_parser_check_for_definition_in_return_type (declarator,
9134                                                      declares_class_or_enum);
9135       if (declarator != cp_error_declarator)
9136         {
9137           decl = grokdeclarator (declarator, &decl_specifiers,
9138                                  NORMAL, 0, NULL);
9139           /* Turn access control back on for names used during
9140              template instantiation.  */
9141           pop_deferring_access_checks ();
9142           /* Do the explicit instantiation.  */
9143           do_decl_instantiation (decl, extension_specifier);
9144         }
9145       else
9146         {
9147           pop_deferring_access_checks ();
9148           /* Skip the body of the explicit instantiation.  */
9149           cp_parser_skip_to_end_of_statement (parser);
9150         }
9151     }
9152   /* We're done with the instantiation.  */
9153   end_explicit_instantiation ();
9154
9155   cp_parser_consume_semicolon_at_end_of_statement (parser);
9156 }
9157
9158 /* Parse an explicit-specialization.
9159
9160    explicit-specialization:
9161      template < > declaration
9162
9163    Although the standard says `declaration', what it really means is:
9164
9165    explicit-specialization:
9166      template <> decl-specifier [opt] init-declarator [opt] ;
9167      template <> function-definition
9168      template <> explicit-specialization
9169      template <> template-declaration  */
9170
9171 static void
9172 cp_parser_explicit_specialization (cp_parser* parser)
9173 {
9174   /* Look for the `template' keyword.  */
9175   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9176   /* Look for the `<'.  */
9177   cp_parser_require (parser, CPP_LESS, "`<'");
9178   /* Look for the `>'.  */
9179   cp_parser_require (parser, CPP_GREATER, "`>'");
9180   /* We have processed another parameter list.  */
9181   ++parser->num_template_parameter_lists;
9182   /* Let the front end know that we are beginning a specialization.  */
9183   begin_specialization ();
9184
9185   /* If the next keyword is `template', we need to figure out whether
9186      or not we're looking a template-declaration.  */
9187   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9188     {
9189       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9190           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9191         cp_parser_template_declaration_after_export (parser,
9192                                                      /*member_p=*/false);
9193       else
9194         cp_parser_explicit_specialization (parser);
9195     }
9196   else
9197     /* Parse the dependent declaration.  */
9198     cp_parser_single_declaration (parser,
9199                                   /*member_p=*/false,
9200                                   /*friend_p=*/NULL);
9201
9202   /* We're done with the specialization.  */
9203   end_specialization ();
9204   /* We're done with this parameter list.  */
9205   --parser->num_template_parameter_lists;
9206 }
9207
9208 /* Parse a type-specifier.
9209
9210    type-specifier:
9211      simple-type-specifier
9212      class-specifier
9213      enum-specifier
9214      elaborated-type-specifier
9215      cv-qualifier
9216
9217    GNU Extension:
9218
9219    type-specifier:
9220      __complex__
9221
9222    Returns a representation of the type-specifier.  For a
9223    class-specifier, enum-specifier, or elaborated-type-specifier, a
9224    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9225
9226    The parser flags FLAGS is used to control type-specifier parsing.
9227
9228    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9229    in a decl-specifier-seq.
9230
9231    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9232    class-specifier, enum-specifier, or elaborated-type-specifier, then
9233    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9234    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9235    zero.
9236
9237    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9238    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9239    is set to FALSE.  */
9240
9241 static tree
9242 cp_parser_type_specifier (cp_parser* parser,
9243                           cp_parser_flags flags,
9244                           cp_decl_specifier_seq *decl_specs,
9245                           bool is_declaration,
9246                           int* declares_class_or_enum,
9247                           bool* is_cv_qualifier)
9248 {
9249   tree type_spec = NULL_TREE;
9250   cp_token *token;
9251   enum rid keyword;
9252   cp_decl_spec ds = ds_last;
9253
9254   /* Assume this type-specifier does not declare a new type.  */
9255   if (declares_class_or_enum)
9256     *declares_class_or_enum = 0;
9257   /* And that it does not specify a cv-qualifier.  */
9258   if (is_cv_qualifier)
9259     *is_cv_qualifier = false;
9260   /* Peek at the next token.  */
9261   token = cp_lexer_peek_token (parser->lexer);
9262
9263   /* If we're looking at a keyword, we can use that to guide the
9264      production we choose.  */
9265   keyword = token->keyword;
9266   switch (keyword)
9267     {
9268     case RID_ENUM:
9269       /* 'enum' [identifier] '{' introduces an enum-specifier;
9270          'enum' <anything else> introduces an elaborated-type-specifier.  */
9271       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9272           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9273               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9274                  == CPP_OPEN_BRACE))
9275         {
9276           type_spec = cp_parser_enum_specifier (parser);
9277           if (declares_class_or_enum)
9278             *declares_class_or_enum = 2;
9279           if (decl_specs)
9280             cp_parser_set_decl_spec_type (decl_specs,
9281                                           type_spec,
9282                                           /*user_defined_p=*/true);
9283           return type_spec;
9284         }
9285       else
9286         goto elaborated_type_specifier;
9287
9288       /* Any of these indicate either a class-specifier, or an
9289          elaborated-type-specifier.  */
9290     case RID_CLASS:
9291     case RID_STRUCT:
9292     case RID_UNION:
9293       /* Parse tentatively so that we can back up if we don't find a
9294          class-specifier.  */
9295       cp_parser_parse_tentatively (parser);
9296       /* Look for the class-specifier.  */
9297       type_spec = cp_parser_class_specifier (parser);
9298       /* If that worked, we're done.  */
9299       if (cp_parser_parse_definitely (parser))
9300         {
9301           if (declares_class_or_enum)
9302             *declares_class_or_enum = 2;
9303           if (decl_specs)
9304             cp_parser_set_decl_spec_type (decl_specs,
9305                                           type_spec,
9306                                           /*user_defined_p=*/true);
9307           return type_spec;
9308         }
9309
9310       /* Fall through.  */
9311     elaborated_type_specifier:
9312       /* We're declaring (not defining) a class or enum.  */
9313       if (declares_class_or_enum)
9314         *declares_class_or_enum = 1;
9315
9316       /* Fall through.  */
9317     case RID_TYPENAME:
9318       /* Look for an elaborated-type-specifier.  */
9319       type_spec
9320         = (cp_parser_elaborated_type_specifier
9321            (parser,
9322             decl_specs && decl_specs->specs[(int) ds_friend],
9323             is_declaration));
9324       if (decl_specs)
9325         cp_parser_set_decl_spec_type (decl_specs,
9326                                       type_spec,
9327                                       /*user_defined_p=*/true);
9328       return type_spec;
9329
9330     case RID_CONST:
9331       ds = ds_const;
9332       if (is_cv_qualifier)
9333         *is_cv_qualifier = true;
9334       break;
9335
9336     case RID_VOLATILE:
9337       ds = ds_volatile;
9338       if (is_cv_qualifier)
9339         *is_cv_qualifier = true;
9340       break;
9341
9342     case RID_RESTRICT:
9343       ds = ds_restrict;
9344       if (is_cv_qualifier)
9345         *is_cv_qualifier = true;
9346       break;
9347
9348     case RID_COMPLEX:
9349       /* The `__complex__' keyword is a GNU extension.  */
9350       ds = ds_complex;
9351       break;
9352
9353     default:
9354       break;
9355     }
9356
9357   /* Handle simple keywords.  */
9358   if (ds != ds_last)
9359     {
9360       if (decl_specs)
9361         {
9362           ++decl_specs->specs[(int)ds];
9363           decl_specs->any_specifiers_p = true;
9364         }
9365       return cp_lexer_consume_token (parser->lexer)->value;
9366     }
9367
9368   /* If we do not already have a type-specifier, assume we are looking
9369      at a simple-type-specifier.  */
9370   type_spec = cp_parser_simple_type_specifier (parser,
9371                                                decl_specs,
9372                                                flags);
9373
9374   /* If we didn't find a type-specifier, and a type-specifier was not
9375      optional in this context, issue an error message.  */
9376   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9377     {
9378       cp_parser_error (parser, "expected type specifier");
9379       return error_mark_node;
9380     }
9381
9382   return type_spec;
9383 }
9384
9385 /* Parse a simple-type-specifier.
9386
9387    simple-type-specifier:
9388      :: [opt] nested-name-specifier [opt] type-name
9389      :: [opt] nested-name-specifier template template-id
9390      char
9391      wchar_t
9392      bool
9393      short
9394      int
9395      long
9396      signed
9397      unsigned
9398      float
9399      double
9400      void
9401
9402    GNU Extension:
9403
9404    simple-type-specifier:
9405      __typeof__ unary-expression
9406      __typeof__ ( type-id )
9407
9408    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9409    appropriately updated.  */
9410
9411 static tree
9412 cp_parser_simple_type_specifier (cp_parser* parser,
9413                                  cp_decl_specifier_seq *decl_specs,
9414                                  cp_parser_flags flags)
9415 {
9416   tree type = NULL_TREE;
9417   cp_token *token;
9418
9419   /* Peek at the next token.  */
9420   token = cp_lexer_peek_token (parser->lexer);
9421
9422   /* If we're looking at a keyword, things are easy.  */
9423   switch (token->keyword)
9424     {
9425     case RID_CHAR:
9426       if (decl_specs)
9427         decl_specs->explicit_char_p = true;
9428       type = char_type_node;
9429       break;
9430     case RID_WCHAR:
9431       type = wchar_type_node;
9432       break;
9433     case RID_BOOL:
9434       type = boolean_type_node;
9435       break;
9436     case RID_SHORT:
9437       if (decl_specs)
9438         ++decl_specs->specs[(int) ds_short];
9439       type = short_integer_type_node;
9440       break;
9441     case RID_INT:
9442       if (decl_specs)
9443         decl_specs->explicit_int_p = true;
9444       type = integer_type_node;
9445       break;
9446     case RID_LONG:
9447       if (decl_specs)
9448         ++decl_specs->specs[(int) ds_long];
9449       type = long_integer_type_node;
9450       break;
9451     case RID_SIGNED:
9452       if (decl_specs)
9453         ++decl_specs->specs[(int) ds_signed];
9454       type = integer_type_node;
9455       break;
9456     case RID_UNSIGNED:
9457       if (decl_specs)
9458         ++decl_specs->specs[(int) ds_unsigned];
9459       type = unsigned_type_node;
9460       break;
9461     case RID_FLOAT:
9462       type = float_type_node;
9463       break;
9464     case RID_DOUBLE:
9465       type = double_type_node;
9466       break;
9467     case RID_VOID:
9468       type = void_type_node;
9469       break;
9470
9471     case RID_TYPEOF:
9472       /* Consume the `typeof' token.  */
9473       cp_lexer_consume_token (parser->lexer);
9474       /* Parse the operand to `typeof'.  */
9475       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9476       /* If it is not already a TYPE, take its type.  */
9477       if (!TYPE_P (type))
9478         type = finish_typeof (type);
9479
9480       if (decl_specs)
9481         cp_parser_set_decl_spec_type (decl_specs, type,
9482                                       /*user_defined_p=*/true);
9483
9484       return type;
9485
9486     default:
9487       break;
9488     }
9489
9490   /* If the type-specifier was for a built-in type, we're done.  */
9491   if (type)
9492     {
9493       tree id;
9494
9495       /* Record the type.  */
9496       if (decl_specs
9497           && (token->keyword != RID_SIGNED
9498               && token->keyword != RID_UNSIGNED
9499               && token->keyword != RID_SHORT
9500               && token->keyword != RID_LONG))
9501         cp_parser_set_decl_spec_type (decl_specs,
9502                                       type,
9503                                       /*user_defined=*/false);
9504       if (decl_specs)
9505         decl_specs->any_specifiers_p = true;
9506
9507       /* Consume the token.  */
9508       id = cp_lexer_consume_token (parser->lexer)->value;
9509
9510       /* There is no valid C++ program where a non-template type is
9511          followed by a "<".  That usually indicates that the user thought
9512          that the type was a template.  */
9513       cp_parser_check_for_invalid_template_id (parser, type);
9514
9515       return TYPE_NAME (type);
9516     }
9517
9518   /* The type-specifier must be a user-defined type.  */
9519   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9520     {
9521       bool qualified_p;
9522       bool global_p;
9523
9524       /* Don't gobble tokens or issue error messages if this is an
9525          optional type-specifier.  */
9526       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9527         cp_parser_parse_tentatively (parser);
9528
9529       /* Look for the optional `::' operator.  */
9530       global_p
9531         = (cp_parser_global_scope_opt (parser,
9532                                        /*current_scope_valid_p=*/false)
9533            != NULL_TREE);
9534       /* Look for the nested-name specifier.  */
9535       qualified_p
9536         = (cp_parser_nested_name_specifier_opt (parser,
9537                                                 /*typename_keyword_p=*/false,
9538                                                 /*check_dependency_p=*/true,
9539                                                 /*type_p=*/false,
9540                                                 /*is_declaration=*/false)
9541            != NULL_TREE);
9542       /* If we have seen a nested-name-specifier, and the next token
9543          is `template', then we are using the template-id production.  */
9544       if (parser->scope
9545           && cp_parser_optional_template_keyword (parser))
9546         {
9547           /* Look for the template-id.  */
9548           type = cp_parser_template_id (parser,
9549                                         /*template_keyword_p=*/true,
9550                                         /*check_dependency_p=*/true,
9551                                         /*is_declaration=*/false);
9552           /* If the template-id did not name a type, we are out of
9553              luck.  */
9554           if (TREE_CODE (type) != TYPE_DECL)
9555             {
9556               cp_parser_error (parser, "expected template-id for type");
9557               type = NULL_TREE;
9558             }
9559         }
9560       /* Otherwise, look for a type-name.  */
9561       else
9562         type = cp_parser_type_name (parser);
9563       /* Keep track of all name-lookups performed in class scopes.  */
9564       if (type
9565           && !global_p
9566           && !qualified_p
9567           && TREE_CODE (type) == TYPE_DECL
9568           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9569         maybe_note_name_used_in_class (DECL_NAME (type), type);
9570       /* If it didn't work out, we don't have a TYPE.  */
9571       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9572           && !cp_parser_parse_definitely (parser))
9573         type = NULL_TREE;
9574       if (type && decl_specs)
9575         cp_parser_set_decl_spec_type (decl_specs, type,
9576                                       /*user_defined=*/true);
9577     }
9578
9579   /* If we didn't get a type-name, issue an error message.  */
9580   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9581     {
9582       cp_parser_error (parser, "expected type-name");
9583       return error_mark_node;
9584     }
9585
9586   /* There is no valid C++ program where a non-template type is
9587      followed by a "<".  That usually indicates that the user thought
9588      that the type was a template.  */
9589   if (type && type != error_mark_node)
9590     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9591
9592   return type;
9593 }
9594
9595 /* Parse a type-name.
9596
9597    type-name:
9598      class-name
9599      enum-name
9600      typedef-name
9601
9602    enum-name:
9603      identifier
9604
9605    typedef-name:
9606      identifier
9607
9608    Returns a TYPE_DECL for the the type.  */
9609
9610 static tree
9611 cp_parser_type_name (cp_parser* parser)
9612 {
9613   tree type_decl;
9614   tree identifier;
9615
9616   /* We can't know yet whether it is a class-name or not.  */
9617   cp_parser_parse_tentatively (parser);
9618   /* Try a class-name.  */
9619   type_decl = cp_parser_class_name (parser,
9620                                     /*typename_keyword_p=*/false,
9621                                     /*template_keyword_p=*/false,
9622                                     /*type_p=*/false,
9623                                     /*check_dependency_p=*/true,
9624                                     /*class_head_p=*/false,
9625                                     /*is_declaration=*/false);
9626   /* If it's not a class-name, keep looking.  */
9627   if (!cp_parser_parse_definitely (parser))
9628     {
9629       /* It must be a typedef-name or an enum-name.  */
9630       identifier = cp_parser_identifier (parser);
9631       if (identifier == error_mark_node)
9632         return error_mark_node;
9633
9634       /* Look up the type-name.  */
9635       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9636       /* Issue an error if we did not find a type-name.  */
9637       if (TREE_CODE (type_decl) != TYPE_DECL)
9638         {
9639           if (!cp_parser_simulate_error (parser))
9640             cp_parser_name_lookup_error (parser, identifier, type_decl,
9641                                          "is not a type");
9642           type_decl = error_mark_node;
9643         }
9644       /* Remember that the name was used in the definition of the
9645          current class so that we can check later to see if the
9646          meaning would have been different after the class was
9647          entirely defined.  */
9648       else if (type_decl != error_mark_node
9649                && !parser->scope)
9650         maybe_note_name_used_in_class (identifier, type_decl);
9651     }
9652
9653   return type_decl;
9654 }
9655
9656
9657 /* Parse an elaborated-type-specifier.  Note that the grammar given
9658    here incorporates the resolution to DR68.
9659
9660    elaborated-type-specifier:
9661      class-key :: [opt] nested-name-specifier [opt] identifier
9662      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9663      enum :: [opt] nested-name-specifier [opt] identifier
9664      typename :: [opt] nested-name-specifier identifier
9665      typename :: [opt] nested-name-specifier template [opt]
9666        template-id
9667
9668    GNU extension:
9669
9670    elaborated-type-specifier:
9671      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9672      class-key attributes :: [opt] nested-name-specifier [opt]
9673                template [opt] template-id
9674      enum attributes :: [opt] nested-name-specifier [opt] identifier
9675
9676    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9677    declared `friend'.  If IS_DECLARATION is TRUE, then this
9678    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9679    something is being declared.
9680
9681    Returns the TYPE specified.  */
9682
9683 static tree
9684 cp_parser_elaborated_type_specifier (cp_parser* parser,
9685                                      bool is_friend,
9686                                      bool is_declaration)
9687 {
9688   enum tag_types tag_type;
9689   tree identifier;
9690   tree type = NULL_TREE;
9691   tree attributes = NULL_TREE;
9692
9693   /* See if we're looking at the `enum' keyword.  */
9694   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9695     {
9696       /* Consume the `enum' token.  */
9697       cp_lexer_consume_token (parser->lexer);
9698       /* Remember that it's an enumeration type.  */
9699       tag_type = enum_type;
9700       /* Parse the attributes.  */
9701       attributes = cp_parser_attributes_opt (parser);
9702     }
9703   /* Or, it might be `typename'.  */
9704   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9705                                            RID_TYPENAME))
9706     {
9707       /* Consume the `typename' token.  */
9708       cp_lexer_consume_token (parser->lexer);
9709       /* Remember that it's a `typename' type.  */
9710       tag_type = typename_type;
9711       /* The `typename' keyword is only allowed in templates.  */
9712       if (!processing_template_decl)
9713         pedwarn ("using `typename' outside of template");
9714     }
9715   /* Otherwise it must be a class-key.  */
9716   else
9717     {
9718       tag_type = cp_parser_class_key (parser);
9719       if (tag_type == none_type)
9720         return error_mark_node;
9721       /* Parse the attributes.  */
9722       attributes = cp_parser_attributes_opt (parser);
9723     }
9724
9725   /* Look for the `::' operator.  */
9726   cp_parser_global_scope_opt (parser,
9727                               /*current_scope_valid_p=*/false);
9728   /* Look for the nested-name-specifier.  */
9729   if (tag_type == typename_type)
9730     {
9731       if (cp_parser_nested_name_specifier (parser,
9732                                            /*typename_keyword_p=*/true,
9733                                            /*check_dependency_p=*/true,
9734                                            /*type_p=*/true,
9735                                            is_declaration)
9736           == error_mark_node)
9737         return error_mark_node;
9738     }
9739   else
9740     /* Even though `typename' is not present, the proposed resolution
9741        to Core Issue 180 says that in `class A<T>::B', `B' should be
9742        considered a type-name, even if `A<T>' is dependent.  */
9743     cp_parser_nested_name_specifier_opt (parser,
9744                                          /*typename_keyword_p=*/true,
9745                                          /*check_dependency_p=*/true,
9746                                          /*type_p=*/true,
9747                                          is_declaration);
9748   /* For everything but enumeration types, consider a template-id.  */
9749   if (tag_type != enum_type)
9750     {
9751       bool template_p = false;
9752       tree decl;
9753
9754       /* Allow the `template' keyword.  */
9755       template_p = cp_parser_optional_template_keyword (parser);
9756       /* If we didn't see `template', we don't know if there's a
9757          template-id or not.  */
9758       if (!template_p)
9759         cp_parser_parse_tentatively (parser);
9760       /* Parse the template-id.  */
9761       decl = cp_parser_template_id (parser, template_p,
9762                                     /*check_dependency_p=*/true,
9763                                     is_declaration);
9764       /* If we didn't find a template-id, look for an ordinary
9765          identifier.  */
9766       if (!template_p && !cp_parser_parse_definitely (parser))
9767         ;
9768       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9769          in effect, then we must assume that, upon instantiation, the
9770          template will correspond to a class.  */
9771       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9772                && tag_type == typename_type)
9773         type = make_typename_type (parser->scope, decl,
9774                                    /*complain=*/1);
9775       else
9776         type = TREE_TYPE (decl);
9777     }
9778
9779   /* For an enumeration type, consider only a plain identifier.  */
9780   if (!type)
9781     {
9782       identifier = cp_parser_identifier (parser);
9783
9784       if (identifier == error_mark_node)
9785         {
9786           parser->scope = NULL_TREE;
9787           return error_mark_node;
9788         }
9789
9790       /* For a `typename', we needn't call xref_tag.  */
9791       if (tag_type == typename_type)
9792         return cp_parser_make_typename_type (parser, parser->scope,
9793                                              identifier);
9794       /* Look up a qualified name in the usual way.  */
9795       if (parser->scope)
9796         {
9797           tree decl;
9798
9799           /* In an elaborated-type-specifier, names are assumed to name
9800              types, so we set IS_TYPE to TRUE when calling
9801              cp_parser_lookup_name.  */
9802           decl = cp_parser_lookup_name (parser, identifier,
9803                                         /*is_type=*/true,
9804                                         /*is_template=*/false,
9805                                         /*is_namespace=*/false,
9806                                         /*check_dependency=*/true,
9807                                         /*ambiguous_p=*/NULL);
9808
9809           /* If we are parsing friend declaration, DECL may be a
9810              TEMPLATE_DECL tree node here.  However, we need to check
9811              whether this TEMPLATE_DECL results in valid code.  Consider
9812              the following example:
9813
9814                namespace N {
9815                  template <class T> class C {};
9816                }
9817                class X {
9818                  template <class T> friend class N::C; // #1, valid code
9819                };
9820                template <class T> class Y {
9821                  friend class N::C;                    // #2, invalid code
9822                };
9823
9824              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9825              name lookup of `N::C'.  We see that friend declaration must
9826              be template for the code to be valid.  Note that
9827              processing_template_decl does not work here since it is
9828              always 1 for the above two cases.  */
9829
9830           decl = (cp_parser_maybe_treat_template_as_class
9831                   (decl, /*tag_name_p=*/is_friend
9832                          && parser->num_template_parameter_lists));
9833
9834           if (TREE_CODE (decl) != TYPE_DECL)
9835             {
9836               error ("expected type-name");
9837               return error_mark_node;
9838             }
9839
9840           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9841             check_elaborated_type_specifier
9842               (tag_type, decl,
9843                (parser->num_template_parameter_lists
9844                 || DECL_SELF_REFERENCE_P (decl)));
9845
9846           type = TREE_TYPE (decl);
9847         }
9848       else
9849         {
9850           /* An elaborated-type-specifier sometimes introduces a new type and
9851              sometimes names an existing type.  Normally, the rule is that it
9852              introduces a new type only if there is not an existing type of
9853              the same name already in scope.  For example, given:
9854
9855                struct S {};
9856                void f() { struct S s; }
9857
9858              the `struct S' in the body of `f' is the same `struct S' as in
9859              the global scope; the existing definition is used.  However, if
9860              there were no global declaration, this would introduce a new
9861              local class named `S'.
9862
9863              An exception to this rule applies to the following code:
9864
9865                namespace N { struct S; }
9866
9867              Here, the elaborated-type-specifier names a new type
9868              unconditionally; even if there is already an `S' in the
9869              containing scope this declaration names a new type.
9870              This exception only applies if the elaborated-type-specifier
9871              forms the complete declaration:
9872
9873                [class.name]
9874
9875                A declaration consisting solely of `class-key identifier ;' is
9876                either a redeclaration of the name in the current scope or a
9877                forward declaration of the identifier as a class name.  It
9878                introduces the name into the current scope.
9879
9880              We are in this situation precisely when the next token is a `;'.
9881
9882              An exception to the exception is that a `friend' declaration does
9883              *not* name a new type; i.e., given:
9884
9885                struct S { friend struct T; };
9886
9887              `T' is not a new type in the scope of `S'.
9888
9889              Also, `new struct S' or `sizeof (struct S)' never results in the
9890              definition of a new type; a new type can only be declared in a
9891              declaration context.  */
9892
9893           /* Warn about attributes. They are ignored.  */
9894           if (attributes)
9895             warning ("type attributes are honored only at type definition");
9896
9897           type = xref_tag (tag_type, identifier,
9898                            (is_friend
9899                             || !is_declaration
9900                             || cp_lexer_next_token_is_not (parser->lexer,
9901                                                            CPP_SEMICOLON)),
9902                            parser->num_template_parameter_lists);
9903         }
9904     }
9905   if (tag_type != enum_type)
9906     cp_parser_check_class_key (tag_type, type);
9907
9908   /* A "<" cannot follow an elaborated type specifier.  If that
9909      happens, the user was probably trying to form a template-id.  */
9910   cp_parser_check_for_invalid_template_id (parser, type);
9911
9912   return type;
9913 }
9914
9915 /* Parse an enum-specifier.
9916
9917    enum-specifier:
9918      enum identifier [opt] { enumerator-list [opt] }
9919
9920    Returns an ENUM_TYPE representing the enumeration.  */
9921
9922 static tree
9923 cp_parser_enum_specifier (cp_parser* parser)
9924 {
9925   tree identifier;
9926   tree type;
9927
9928   /* Caller guarantees that the current token is 'enum', an identifier
9929      possibly follows, and the token after that is an opening brace.
9930      If we don't have an identifier, fabricate an anonymous name for
9931      the enumeration being defined.  */
9932   cp_lexer_consume_token (parser->lexer);
9933
9934   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9935     identifier = cp_parser_identifier (parser);
9936   else
9937     identifier = make_anon_name ();
9938
9939   cp_lexer_consume_token (parser->lexer);
9940
9941   /* Issue an error message if type-definitions are forbidden here.  */
9942   cp_parser_check_type_definition (parser);
9943
9944   /* Create the new type.  */
9945   type = start_enum (identifier);
9946
9947   /* If the next token is not '}', then there are some enumerators.  */
9948   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9949     cp_parser_enumerator_list (parser, type);
9950
9951   /* Consume the final '}'.  */
9952   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9953
9954   /* Finish up the enumeration.  */
9955   finish_enum (type);
9956
9957   return type;
9958 }
9959
9960 /* Parse an enumerator-list.  The enumerators all have the indicated
9961    TYPE.
9962
9963    enumerator-list:
9964      enumerator-definition
9965      enumerator-list , enumerator-definition  */
9966
9967 static void
9968 cp_parser_enumerator_list (cp_parser* parser, tree type)
9969 {
9970   while (true)
9971     {
9972       /* Parse an enumerator-definition.  */
9973       cp_parser_enumerator_definition (parser, type);
9974
9975       /* If the next token is not a ',', we've reached the end of
9976          the list.  */
9977       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9978         break;
9979       /* Otherwise, consume the `,' and keep going.  */
9980       cp_lexer_consume_token (parser->lexer);
9981       /* If the next token is a `}', there is a trailing comma.  */
9982       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9983         {
9984           if (pedantic && !in_system_header)
9985             pedwarn ("comma at end of enumerator list");
9986           break;
9987         }
9988     }
9989 }
9990
9991 /* Parse an enumerator-definition.  The enumerator has the indicated
9992    TYPE.
9993
9994    enumerator-definition:
9995      enumerator
9996      enumerator = constant-expression
9997
9998    enumerator:
9999      identifier  */
10000
10001 static void
10002 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10003 {
10004   tree identifier;
10005   tree value;
10006
10007   /* Look for the identifier.  */
10008   identifier = cp_parser_identifier (parser);
10009   if (identifier == error_mark_node)
10010     return;
10011
10012   /* If the next token is an '=', then there is an explicit value.  */
10013   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10014     {
10015       /* Consume the `=' token.  */
10016       cp_lexer_consume_token (parser->lexer);
10017       /* Parse the value.  */
10018       value = cp_parser_constant_expression (parser,
10019                                              /*allow_non_constant_p=*/false,
10020                                              NULL);
10021     }
10022   else
10023     value = NULL_TREE;
10024
10025   /* Create the enumerator.  */
10026   build_enumerator (identifier, value, type);
10027 }
10028
10029 /* Parse a namespace-name.
10030
10031    namespace-name:
10032      original-namespace-name
10033      namespace-alias
10034
10035    Returns the NAMESPACE_DECL for the namespace.  */
10036
10037 static tree
10038 cp_parser_namespace_name (cp_parser* parser)
10039 {
10040   tree identifier;
10041   tree namespace_decl;
10042
10043   /* Get the name of the namespace.  */
10044   identifier = cp_parser_identifier (parser);
10045   if (identifier == error_mark_node)
10046     return error_mark_node;
10047
10048   /* Look up the identifier in the currently active scope.  Look only
10049      for namespaces, due to:
10050
10051        [basic.lookup.udir]
10052
10053        When looking up a namespace-name in a using-directive or alias
10054        definition, only namespace names are considered.
10055
10056      And:
10057
10058        [basic.lookup.qual]
10059
10060        During the lookup of a name preceding the :: scope resolution
10061        operator, object, function, and enumerator names are ignored.
10062
10063      (Note that cp_parser_class_or_namespace_name only calls this
10064      function if the token after the name is the scope resolution
10065      operator.)  */
10066   namespace_decl = cp_parser_lookup_name (parser, identifier,
10067                                           /*is_type=*/false,
10068                                           /*is_template=*/false,
10069                                           /*is_namespace=*/true,
10070                                           /*check_dependency=*/true,
10071                                           /*ambiguous_p=*/NULL);
10072   /* If it's not a namespace, issue an error.  */
10073   if (namespace_decl == error_mark_node
10074       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10075     {
10076       cp_parser_error (parser, "expected namespace-name");
10077       namespace_decl = error_mark_node;
10078     }
10079
10080   return namespace_decl;
10081 }
10082
10083 /* Parse a namespace-definition.
10084
10085    namespace-definition:
10086      named-namespace-definition
10087      unnamed-namespace-definition
10088
10089    named-namespace-definition:
10090      original-namespace-definition
10091      extension-namespace-definition
10092
10093    original-namespace-definition:
10094      namespace identifier { namespace-body }
10095
10096    extension-namespace-definition:
10097      namespace original-namespace-name { namespace-body }
10098
10099    unnamed-namespace-definition:
10100      namespace { namespace-body } */
10101
10102 static void
10103 cp_parser_namespace_definition (cp_parser* parser)
10104 {
10105   tree identifier;
10106
10107   /* Look for the `namespace' keyword.  */
10108   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10109
10110   /* Get the name of the namespace.  We do not attempt to distinguish
10111      between an original-namespace-definition and an
10112      extension-namespace-definition at this point.  The semantic
10113      analysis routines are responsible for that.  */
10114   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10115     identifier = cp_parser_identifier (parser);
10116   else
10117     identifier = NULL_TREE;
10118
10119   /* Look for the `{' to start the namespace.  */
10120   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10121   /* Start the namespace.  */
10122   push_namespace (identifier);
10123   /* Parse the body of the namespace.  */
10124   cp_parser_namespace_body (parser);
10125   /* Finish the namespace.  */
10126   pop_namespace ();
10127   /* Look for the final `}'.  */
10128   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10129 }
10130
10131 /* Parse a namespace-body.
10132
10133    namespace-body:
10134      declaration-seq [opt]  */
10135
10136 static void
10137 cp_parser_namespace_body (cp_parser* parser)
10138 {
10139   cp_parser_declaration_seq_opt (parser);
10140 }
10141
10142 /* Parse a namespace-alias-definition.
10143
10144    namespace-alias-definition:
10145      namespace identifier = qualified-namespace-specifier ;  */
10146
10147 static void
10148 cp_parser_namespace_alias_definition (cp_parser* parser)
10149 {
10150   tree identifier;
10151   tree namespace_specifier;
10152
10153   /* Look for the `namespace' keyword.  */
10154   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10155   /* Look for the identifier.  */
10156   identifier = cp_parser_identifier (parser);
10157   if (identifier == error_mark_node)
10158     return;
10159   /* Look for the `=' token.  */
10160   cp_parser_require (parser, CPP_EQ, "`='");
10161   /* Look for the qualified-namespace-specifier.  */
10162   namespace_specifier
10163     = cp_parser_qualified_namespace_specifier (parser);
10164   /* Look for the `;' token.  */
10165   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10166
10167   /* Register the alias in the symbol table.  */
10168   do_namespace_alias (identifier, namespace_specifier);
10169 }
10170
10171 /* Parse a qualified-namespace-specifier.
10172
10173    qualified-namespace-specifier:
10174      :: [opt] nested-name-specifier [opt] namespace-name
10175
10176    Returns a NAMESPACE_DECL corresponding to the specified
10177    namespace.  */
10178
10179 static tree
10180 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10181 {
10182   /* Look for the optional `::'.  */
10183   cp_parser_global_scope_opt (parser,
10184                               /*current_scope_valid_p=*/false);
10185
10186   /* Look for the optional nested-name-specifier.  */
10187   cp_parser_nested_name_specifier_opt (parser,
10188                                        /*typename_keyword_p=*/false,
10189                                        /*check_dependency_p=*/true,
10190                                        /*type_p=*/false,
10191                                        /*is_declaration=*/true);
10192
10193   return cp_parser_namespace_name (parser);
10194 }
10195
10196 /* Parse a using-declaration.
10197
10198    using-declaration:
10199      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10200      using :: unqualified-id ;  */
10201
10202 static void
10203 cp_parser_using_declaration (cp_parser* parser)
10204 {
10205   cp_token *token;
10206   bool typename_p = false;
10207   bool global_scope_p;
10208   tree decl;
10209   tree identifier;
10210   tree scope;
10211   tree qscope;
10212
10213   /* Look for the `using' keyword.  */
10214   cp_parser_require_keyword (parser, RID_USING, "`using'");
10215
10216   /* Peek at the next token.  */
10217   token = cp_lexer_peek_token (parser->lexer);
10218   /* See if it's `typename'.  */
10219   if (token->keyword == RID_TYPENAME)
10220     {
10221       /* Remember that we've seen it.  */
10222       typename_p = true;
10223       /* Consume the `typename' token.  */
10224       cp_lexer_consume_token (parser->lexer);
10225     }
10226
10227   /* Look for the optional global scope qualification.  */
10228   global_scope_p
10229     = (cp_parser_global_scope_opt (parser,
10230                                    /*current_scope_valid_p=*/false)
10231        != NULL_TREE);
10232
10233   /* If we saw `typename', or didn't see `::', then there must be a
10234      nested-name-specifier present.  */
10235   if (typename_p || !global_scope_p)
10236     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10237                                               /*check_dependency_p=*/true,
10238                                               /*type_p=*/false,
10239                                               /*is_declaration=*/true);
10240   /* Otherwise, we could be in either of the two productions.  In that
10241      case, treat the nested-name-specifier as optional.  */
10242   else
10243     qscope = cp_parser_nested_name_specifier_opt (parser,
10244                                                   /*typename_keyword_p=*/false,
10245                                                   /*check_dependency_p=*/true,
10246                                                   /*type_p=*/false,
10247                                                   /*is_declaration=*/true);
10248   if (!qscope)
10249     qscope = global_namespace;
10250
10251   /* Parse the unqualified-id.  */
10252   identifier = cp_parser_unqualified_id (parser,
10253                                          /*template_keyword_p=*/false,
10254                                          /*check_dependency_p=*/true,
10255                                          /*declarator_p=*/true);
10256
10257   /* The function we call to handle a using-declaration is different
10258      depending on what scope we are in.  */
10259   if (identifier == error_mark_node)
10260     ;
10261   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10262            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10263     /* [namespace.udecl]
10264
10265        A using declaration shall not name a template-id.  */
10266     error ("a template-id may not appear in a using-declaration");
10267   else
10268     {
10269       scope = current_scope ();
10270       if (scope && TYPE_P (scope))
10271         {
10272           /* Create the USING_DECL.  */
10273           decl = do_class_using_decl (build_nt (SCOPE_REF,
10274                                                 parser->scope,
10275                                                 identifier));
10276           /* Add it to the list of members in this class.  */
10277           finish_member_declaration (decl);
10278         }
10279       else
10280         {
10281           decl = cp_parser_lookup_name_simple (parser, identifier);
10282           if (decl == error_mark_node)
10283             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10284           else if (scope)
10285             do_local_using_decl (decl, qscope, identifier);
10286           else
10287             do_toplevel_using_decl (decl, qscope, identifier);
10288         }
10289     }
10290
10291   /* Look for the final `;'.  */
10292   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10293 }
10294
10295 /* Parse a using-directive.
10296
10297    using-directive:
10298      using namespace :: [opt] nested-name-specifier [opt]
10299        namespace-name ;  */
10300
10301 static void
10302 cp_parser_using_directive (cp_parser* parser)
10303 {
10304   tree namespace_decl;
10305   tree attribs;
10306
10307   /* Look for the `using' keyword.  */
10308   cp_parser_require_keyword (parser, RID_USING, "`using'");
10309   /* And the `namespace' keyword.  */
10310   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10311   /* Look for the optional `::' operator.  */
10312   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10313   /* And the optional nested-name-specifier.  */
10314   cp_parser_nested_name_specifier_opt (parser,
10315                                        /*typename_keyword_p=*/false,
10316                                        /*check_dependency_p=*/true,
10317                                        /*type_p=*/false,
10318                                        /*is_declaration=*/true);
10319   /* Get the namespace being used.  */
10320   namespace_decl = cp_parser_namespace_name (parser);
10321   /* And any specified attributes.  */
10322   attribs = cp_parser_attributes_opt (parser);
10323   /* Update the symbol table.  */
10324   parse_using_directive (namespace_decl, attribs);
10325   /* Look for the final `;'.  */
10326   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10327 }
10328
10329 /* Parse an asm-definition.
10330
10331    asm-definition:
10332      asm ( string-literal ) ;
10333
10334    GNU Extension:
10335
10336    asm-definition:
10337      asm volatile [opt] ( string-literal ) ;
10338      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10339      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10340                           : asm-operand-list [opt] ) ;
10341      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10342                           : asm-operand-list [opt]
10343                           : asm-operand-list [opt] ) ;  */
10344
10345 static void
10346 cp_parser_asm_definition (cp_parser* parser)
10347 {
10348   cp_token *token;
10349   tree string;
10350   tree outputs = NULL_TREE;
10351   tree inputs = NULL_TREE;
10352   tree clobbers = NULL_TREE;
10353   tree asm_stmt;
10354   bool volatile_p = false;
10355   bool extended_p = false;
10356
10357   /* Look for the `asm' keyword.  */
10358   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10359   /* See if the next token is `volatile'.  */
10360   if (cp_parser_allow_gnu_extensions_p (parser)
10361       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10362     {
10363       /* Remember that we saw the `volatile' keyword.  */
10364       volatile_p = true;
10365       /* Consume the token.  */
10366       cp_lexer_consume_token (parser->lexer);
10367     }
10368   /* Look for the opening `('.  */
10369   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10370   /* Look for the string.  */
10371   c_lex_string_translate = 0;
10372   token = cp_parser_require (parser, CPP_STRING, "asm body");
10373   if (!token)
10374     goto finish;
10375   string = token->value;
10376   /* If we're allowing GNU extensions, check for the extended assembly
10377      syntax.  Unfortunately, the `:' tokens need not be separated by
10378      a space in C, and so, for compatibility, we tolerate that here
10379      too.  Doing that means that we have to treat the `::' operator as
10380      two `:' tokens.  */
10381   if (cp_parser_allow_gnu_extensions_p (parser)
10382       && at_function_scope_p ()
10383       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10384           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10385     {
10386       bool inputs_p = false;
10387       bool clobbers_p = false;
10388
10389       /* The extended syntax was used.  */
10390       extended_p = true;
10391
10392       /* Look for outputs.  */
10393       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10394         {
10395           /* Consume the `:'.  */
10396           cp_lexer_consume_token (parser->lexer);
10397           /* Parse the output-operands.  */
10398           if (cp_lexer_next_token_is_not (parser->lexer,
10399                                           CPP_COLON)
10400               && cp_lexer_next_token_is_not (parser->lexer,
10401                                              CPP_SCOPE)
10402               && cp_lexer_next_token_is_not (parser->lexer,
10403                                              CPP_CLOSE_PAREN))
10404             outputs = cp_parser_asm_operand_list (parser);
10405         }
10406       /* If the next token is `::', there are no outputs, and the
10407          next token is the beginning of the inputs.  */
10408       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10409         /* The inputs are coming next.  */
10410         inputs_p = true;
10411
10412       /* Look for inputs.  */
10413       if (inputs_p
10414           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10415         {
10416           /* Consume the `:' or `::'.  */
10417           cp_lexer_consume_token (parser->lexer);
10418           /* Parse the output-operands.  */
10419           if (cp_lexer_next_token_is_not (parser->lexer,
10420                                           CPP_COLON)
10421               && cp_lexer_next_token_is_not (parser->lexer,
10422                                              CPP_CLOSE_PAREN))
10423             inputs = cp_parser_asm_operand_list (parser);
10424         }
10425       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10426         /* The clobbers are coming next.  */
10427         clobbers_p = true;
10428
10429       /* Look for clobbers.  */
10430       if (clobbers_p
10431           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10432         {
10433           /* Consume the `:' or `::'.  */
10434           cp_lexer_consume_token (parser->lexer);
10435           /* Parse the clobbers.  */
10436           if (cp_lexer_next_token_is_not (parser->lexer,
10437                                           CPP_CLOSE_PAREN))
10438             clobbers = cp_parser_asm_clobber_list (parser);
10439         }
10440     }
10441   /* Look for the closing `)'.  */
10442   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10443     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10444                                            /*consume_paren=*/true);
10445   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10446
10447   /* Create the ASM_EXPR.  */
10448   if (at_function_scope_p ())
10449     {
10450       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10451                                   inputs, clobbers);
10452       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10453       if (!extended_p)
10454         ASM_INPUT_P (asm_stmt) = 1;
10455     }
10456   else
10457     assemble_asm (string);
10458
10459  finish:
10460   c_lex_string_translate = 1;
10461 }
10462
10463 /* Declarators [gram.dcl.decl] */
10464
10465 /* Parse an init-declarator.
10466
10467    init-declarator:
10468      declarator initializer [opt]
10469
10470    GNU Extension:
10471
10472    init-declarator:
10473      declarator asm-specification [opt] attributes [opt] initializer [opt]
10474
10475    function-definition:
10476      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10477        function-body
10478      decl-specifier-seq [opt] declarator function-try-block
10479
10480    GNU Extension:
10481
10482    function-definition:
10483      __extension__ function-definition
10484
10485    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10486    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10487    then this declarator appears in a class scope.  The new DECL created
10488    by this declarator is returned.
10489
10490    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10491    for a function-definition here as well.  If the declarator is a
10492    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10493    be TRUE upon return.  By that point, the function-definition will
10494    have been completely parsed.
10495
10496    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10497    is FALSE.  */
10498
10499 static tree
10500 cp_parser_init_declarator (cp_parser* parser,
10501                            cp_decl_specifier_seq *decl_specifiers,
10502                            bool function_definition_allowed_p,
10503                            bool member_p,
10504                            int declares_class_or_enum,
10505                            bool* function_definition_p)
10506 {
10507   cp_token *token;
10508   cp_declarator *declarator;
10509   tree prefix_attributes;
10510   tree attributes;
10511   tree asm_specification;
10512   tree initializer;
10513   tree decl = NULL_TREE;
10514   tree scope;
10515   bool is_initialized;
10516   bool is_parenthesized_init;
10517   bool is_non_constant_init;
10518   int ctor_dtor_or_conv_p;
10519   bool friend_p;
10520   bool pop_p = false;
10521
10522   /* Gather the attributes that were provided with the
10523      decl-specifiers.  */
10524   prefix_attributes = decl_specifiers->attributes;
10525
10526   /* Assume that this is not the declarator for a function
10527      definition.  */
10528   if (function_definition_p)
10529     *function_definition_p = false;
10530
10531   /* Defer access checks while parsing the declarator; we cannot know
10532      what names are accessible until we know what is being
10533      declared.  */
10534   resume_deferring_access_checks ();
10535
10536   /* Parse the declarator.  */
10537   declarator
10538     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10539                             &ctor_dtor_or_conv_p,
10540                             /*parenthesized_p=*/NULL);
10541   /* Gather up the deferred checks.  */
10542   stop_deferring_access_checks ();
10543
10544   /* If the DECLARATOR was erroneous, there's no need to go
10545      further.  */
10546   if (declarator == cp_error_declarator)
10547     return error_mark_node;
10548
10549   cp_parser_check_for_definition_in_return_type (declarator,
10550                                                  declares_class_or_enum);
10551
10552   /* Figure out what scope the entity declared by the DECLARATOR is
10553      located in.  `grokdeclarator' sometimes changes the scope, so
10554      we compute it now.  */
10555   scope = get_scope_of_declarator (declarator);
10556
10557   /* If we're allowing GNU extensions, look for an asm-specification
10558      and attributes.  */
10559   if (cp_parser_allow_gnu_extensions_p (parser))
10560     {
10561       /* Look for an asm-specification.  */
10562       asm_specification = cp_parser_asm_specification_opt (parser);
10563       /* And attributes.  */
10564       attributes = cp_parser_attributes_opt (parser);
10565     }
10566   else
10567     {
10568       asm_specification = NULL_TREE;
10569       attributes = NULL_TREE;
10570     }
10571
10572   /* Peek at the next token.  */
10573   token = cp_lexer_peek_token (parser->lexer);
10574   /* Check to see if the token indicates the start of a
10575      function-definition.  */
10576   if (cp_parser_token_starts_function_definition_p (token))
10577     {
10578       if (!function_definition_allowed_p)
10579         {
10580           /* If a function-definition should not appear here, issue an
10581              error message.  */
10582           cp_parser_error (parser,
10583                            "a function-definition is not allowed here");
10584           return error_mark_node;
10585         }
10586       else
10587         {
10588           /* Neither attributes nor an asm-specification are allowed
10589              on a function-definition.  */
10590           if (asm_specification)
10591             error ("an asm-specification is not allowed on a function-definition");
10592           if (attributes)
10593             error ("attributes are not allowed on a function-definition");
10594           /* This is a function-definition.  */
10595           *function_definition_p = true;
10596
10597           /* Parse the function definition.  */
10598           if (member_p)
10599             decl = cp_parser_save_member_function_body (parser,
10600                                                         decl_specifiers,
10601                                                         declarator,
10602                                                         prefix_attributes);
10603           else
10604             decl
10605               = (cp_parser_function_definition_from_specifiers_and_declarator
10606                  (parser, decl_specifiers, prefix_attributes, declarator));
10607
10608           return decl;
10609         }
10610     }
10611
10612   /* [dcl.dcl]
10613
10614      Only in function declarations for constructors, destructors, and
10615      type conversions can the decl-specifier-seq be omitted.
10616
10617      We explicitly postpone this check past the point where we handle
10618      function-definitions because we tolerate function-definitions
10619      that are missing their return types in some modes.  */
10620   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10621     {
10622       cp_parser_error (parser,
10623                        "expected constructor, destructor, or type conversion");
10624       return error_mark_node;
10625     }
10626
10627   /* An `=' or an `(' indicates an initializer.  */
10628   is_initialized = (token->type == CPP_EQ
10629                      || token->type == CPP_OPEN_PAREN);
10630   /* If the init-declarator isn't initialized and isn't followed by a
10631      `,' or `;', it's not a valid init-declarator.  */
10632   if (!is_initialized
10633       && token->type != CPP_COMMA
10634       && token->type != CPP_SEMICOLON)
10635     {
10636       cp_parser_error (parser, "expected init-declarator");
10637       return error_mark_node;
10638     }
10639
10640   /* Because start_decl has side-effects, we should only call it if we
10641      know we're going ahead.  By this point, we know that we cannot
10642      possibly be looking at any other construct.  */
10643   cp_parser_commit_to_tentative_parse (parser);
10644
10645   /* If the decl specifiers were bad, issue an error now that we're
10646      sure this was intended to be a declarator.  Then continue
10647      declaring the variable(s), as int, to try to cut down on further
10648      errors.  */
10649   if (decl_specifiers->any_specifiers_p
10650       && decl_specifiers->type == error_mark_node)
10651     {
10652       cp_parser_error (parser, "invalid type in declaration");
10653       decl_specifiers->type = integer_type_node;
10654     }
10655
10656   /* Check to see whether or not this declaration is a friend.  */
10657   friend_p = cp_parser_friend_p (decl_specifiers);
10658
10659   /* Check that the number of template-parameter-lists is OK.  */
10660   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10661     return error_mark_node;
10662
10663   /* Enter the newly declared entry in the symbol table.  If we're
10664      processing a declaration in a class-specifier, we wait until
10665      after processing the initializer.  */
10666   if (!member_p)
10667     {
10668       if (parser->in_unbraced_linkage_specification_p)
10669         {
10670           decl_specifiers->storage_class = sc_extern;
10671           have_extern_spec = false;
10672         }
10673       decl = start_decl (declarator, decl_specifiers,
10674                          is_initialized, attributes, prefix_attributes,
10675                          &pop_p);
10676     }
10677   else if (scope)
10678     /* Enter the SCOPE.  That way unqualified names appearing in the
10679        initializer will be looked up in SCOPE.  */
10680     pop_p = push_scope (scope);
10681
10682   /* Perform deferred access control checks, now that we know in which
10683      SCOPE the declared entity resides.  */
10684   if (!member_p && decl)
10685     {
10686       tree saved_current_function_decl = NULL_TREE;
10687
10688       /* If the entity being declared is a function, pretend that we
10689          are in its scope.  If it is a `friend', it may have access to
10690          things that would not otherwise be accessible.  */
10691       if (TREE_CODE (decl) == FUNCTION_DECL)
10692         {
10693           saved_current_function_decl = current_function_decl;
10694           current_function_decl = decl;
10695         }
10696
10697       /* Perform the access control checks for the declarator and the
10698          the decl-specifiers.  */
10699       perform_deferred_access_checks ();
10700
10701       /* Restore the saved value.  */
10702       if (TREE_CODE (decl) == FUNCTION_DECL)
10703         current_function_decl = saved_current_function_decl;
10704     }
10705
10706   /* Parse the initializer.  */
10707   if (is_initialized)
10708     initializer = cp_parser_initializer (parser,
10709                                          &is_parenthesized_init,
10710                                          &is_non_constant_init);
10711   else
10712     {
10713       initializer = NULL_TREE;
10714       is_parenthesized_init = false;
10715       is_non_constant_init = true;
10716     }
10717
10718   /* The old parser allows attributes to appear after a parenthesized
10719      initializer.  Mark Mitchell proposed removing this functionality
10720      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10721      attributes -- but ignores them.  */
10722   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10723     if (cp_parser_attributes_opt (parser))
10724       warning ("attributes after parenthesized initializer ignored");
10725
10726   /* For an in-class declaration, use `grokfield' to create the
10727      declaration.  */
10728   if (member_p)
10729     {
10730       if (pop_p)
10731         pop_scope (scope);
10732       decl = grokfield (declarator, decl_specifiers,
10733                         initializer, /*asmspec=*/NULL_TREE,
10734                         /*attributes=*/NULL_TREE);
10735       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10736         cp_parser_save_default_args (parser, decl);
10737     }
10738
10739   /* Finish processing the declaration.  But, skip friend
10740      declarations.  */
10741   if (!friend_p && decl && decl != error_mark_node)
10742     {
10743       cp_finish_decl (decl,
10744                       initializer,
10745                       asm_specification,
10746                       /* If the initializer is in parentheses, then this is
10747                          a direct-initialization, which means that an
10748                          `explicit' constructor is OK.  Otherwise, an
10749                          `explicit' constructor cannot be used.  */
10750                       ((is_parenthesized_init || !is_initialized)
10751                      ? 0 : LOOKUP_ONLYCONVERTING));
10752       if (pop_p)
10753         pop_scope (DECL_CONTEXT (decl));
10754     }
10755
10756   /* Remember whether or not variables were initialized by
10757      constant-expressions.  */
10758   if (decl && TREE_CODE (decl) == VAR_DECL
10759       && is_initialized && !is_non_constant_init)
10760     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10761
10762   return decl;
10763 }
10764
10765 /* Parse a declarator.
10766
10767    declarator:
10768      direct-declarator
10769      ptr-operator declarator
10770
10771    abstract-declarator:
10772      ptr-operator abstract-declarator [opt]
10773      direct-abstract-declarator
10774
10775    GNU Extensions:
10776
10777    declarator:
10778      attributes [opt] direct-declarator
10779      attributes [opt] ptr-operator declarator
10780
10781    abstract-declarator:
10782      attributes [opt] ptr-operator abstract-declarator [opt]
10783      attributes [opt] direct-abstract-declarator
10784
10785    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10786    detect constructor, destructor or conversion operators. It is set
10787    to -1 if the declarator is a name, and +1 if it is a
10788    function. Otherwise it is set to zero. Usually you just want to
10789    test for >0, but internally the negative value is used.
10790
10791    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10792    a decl-specifier-seq unless it declares a constructor, destructor,
10793    or conversion.  It might seem that we could check this condition in
10794    semantic analysis, rather than parsing, but that makes it difficult
10795    to handle something like `f()'.  We want to notice that there are
10796    no decl-specifiers, and therefore realize that this is an
10797    expression, not a declaration.)
10798
10799    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10800    the declarator is a direct-declarator of the form "(...)".  */
10801
10802 static cp_declarator *
10803 cp_parser_declarator (cp_parser* parser,
10804                       cp_parser_declarator_kind dcl_kind,
10805                       int* ctor_dtor_or_conv_p,
10806                       bool* parenthesized_p)
10807 {
10808   cp_token *token;
10809   cp_declarator *declarator;
10810   enum tree_code code;
10811   cp_cv_quals cv_quals;
10812   tree class_type;
10813   tree attributes = NULL_TREE;
10814
10815   /* Assume this is not a constructor, destructor, or type-conversion
10816      operator.  */
10817   if (ctor_dtor_or_conv_p)
10818     *ctor_dtor_or_conv_p = 0;
10819
10820   if (cp_parser_allow_gnu_extensions_p (parser))
10821     attributes = cp_parser_attributes_opt (parser);
10822
10823   /* Peek at the next token.  */
10824   token = cp_lexer_peek_token (parser->lexer);
10825
10826   /* Check for the ptr-operator production.  */
10827   cp_parser_parse_tentatively (parser);
10828   /* Parse the ptr-operator.  */
10829   code = cp_parser_ptr_operator (parser,
10830                                  &class_type,
10831                                  &cv_quals);
10832   /* If that worked, then we have a ptr-operator.  */
10833   if (cp_parser_parse_definitely (parser))
10834     {
10835       /* If a ptr-operator was found, then this declarator was not
10836          parenthesized.  */
10837       if (parenthesized_p)
10838         *parenthesized_p = true;
10839       /* The dependent declarator is optional if we are parsing an
10840          abstract-declarator.  */
10841       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10842         cp_parser_parse_tentatively (parser);
10843
10844       /* Parse the dependent declarator.  */
10845       declarator = cp_parser_declarator (parser, dcl_kind,
10846                                          /*ctor_dtor_or_conv_p=*/NULL,
10847                                          /*parenthesized_p=*/NULL);
10848
10849       /* If we are parsing an abstract-declarator, we must handle the
10850          case where the dependent declarator is absent.  */
10851       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10852           && !cp_parser_parse_definitely (parser))
10853         declarator = NULL;
10854
10855       /* Build the representation of the ptr-operator.  */
10856       if (class_type)
10857         declarator = make_ptrmem_declarator (cv_quals,
10858                                              class_type,
10859                                              declarator);
10860       else if (code == INDIRECT_REF)
10861         declarator = make_pointer_declarator (cv_quals, declarator);
10862       else
10863         declarator = make_reference_declarator (cv_quals, declarator);
10864     }
10865   /* Everything else is a direct-declarator.  */
10866   else
10867     {
10868       if (parenthesized_p)
10869         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10870                                                    CPP_OPEN_PAREN);
10871       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10872                                                 ctor_dtor_or_conv_p);
10873     }
10874
10875   if (attributes && declarator != cp_error_declarator)
10876     declarator->attributes = attributes;
10877
10878   return declarator;
10879 }
10880
10881 /* Parse a direct-declarator or direct-abstract-declarator.
10882
10883    direct-declarator:
10884      declarator-id
10885      direct-declarator ( parameter-declaration-clause )
10886        cv-qualifier-seq [opt]
10887        exception-specification [opt]
10888      direct-declarator [ constant-expression [opt] ]
10889      ( declarator )
10890
10891    direct-abstract-declarator:
10892      direct-abstract-declarator [opt]
10893        ( parameter-declaration-clause )
10894        cv-qualifier-seq [opt]
10895        exception-specification [opt]
10896      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10897      ( abstract-declarator )
10898
10899    Returns a representation of the declarator.  DCL_KIND is
10900    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10901    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10902    we are parsing a direct-declarator.  It is
10903    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10904    of ambiguity we prefer an abstract declarator, as per
10905    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10906    cp_parser_declarator.  */
10907
10908 static cp_declarator *
10909 cp_parser_direct_declarator (cp_parser* parser,
10910                              cp_parser_declarator_kind dcl_kind,
10911                              int* ctor_dtor_or_conv_p)
10912 {
10913   cp_token *token;
10914   cp_declarator *declarator = NULL;
10915   tree scope = NULL_TREE;
10916   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10917   bool saved_in_declarator_p = parser->in_declarator_p;
10918   bool first = true;
10919   bool pop_p = false;
10920
10921   while (true)
10922     {
10923       /* Peek at the next token.  */
10924       token = cp_lexer_peek_token (parser->lexer);
10925       if (token->type == CPP_OPEN_PAREN)
10926         {
10927           /* This is either a parameter-declaration-clause, or a
10928              parenthesized declarator. When we know we are parsing a
10929              named declarator, it must be a parenthesized declarator
10930              if FIRST is true. For instance, `(int)' is a
10931              parameter-declaration-clause, with an omitted
10932              direct-abstract-declarator. But `((*))', is a
10933              parenthesized abstract declarator. Finally, when T is a
10934              template parameter `(T)' is a
10935              parameter-declaration-clause, and not a parenthesized
10936              named declarator.
10937
10938              We first try and parse a parameter-declaration-clause,
10939              and then try a nested declarator (if FIRST is true).
10940
10941              It is not an error for it not to be a
10942              parameter-declaration-clause, even when FIRST is
10943              false. Consider,
10944
10945                int i (int);
10946                int i (3);
10947
10948              The first is the declaration of a function while the
10949              second is a the definition of a variable, including its
10950              initializer.
10951
10952              Having seen only the parenthesis, we cannot know which of
10953              these two alternatives should be selected.  Even more
10954              complex are examples like:
10955
10956                int i (int (a));
10957                int i (int (3));
10958
10959              The former is a function-declaration; the latter is a
10960              variable initialization.
10961
10962              Thus again, we try a parameter-declaration-clause, and if
10963              that fails, we back out and return.  */
10964
10965           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10966             {
10967               cp_parameter_declarator *params;
10968               unsigned saved_num_template_parameter_lists;
10969
10970               cp_parser_parse_tentatively (parser);
10971
10972               /* Consume the `('.  */
10973               cp_lexer_consume_token (parser->lexer);
10974               if (first)
10975                 {
10976                   /* If this is going to be an abstract declarator, we're
10977                      in a declarator and we can't have default args.  */
10978                   parser->default_arg_ok_p = false;
10979                   parser->in_declarator_p = true;
10980                 }
10981
10982               /* Inside the function parameter list, surrounding
10983                  template-parameter-lists do not apply.  */
10984               saved_num_template_parameter_lists
10985                 = parser->num_template_parameter_lists;
10986               parser->num_template_parameter_lists = 0;
10987
10988               /* Parse the parameter-declaration-clause.  */
10989               params = cp_parser_parameter_declaration_clause (parser);
10990
10991               parser->num_template_parameter_lists
10992                 = saved_num_template_parameter_lists;
10993
10994               /* If all went well, parse the cv-qualifier-seq and the
10995                  exception-specification.  */
10996               if (cp_parser_parse_definitely (parser))
10997                 {
10998                   cp_cv_quals cv_quals;
10999                   tree exception_specification;
11000
11001                   if (ctor_dtor_or_conv_p)
11002                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11003                   first = false;
11004                   /* Consume the `)'.  */
11005                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11006
11007                   /* Parse the cv-qualifier-seq.  */
11008                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11009                   /* And the exception-specification.  */
11010                   exception_specification
11011                     = cp_parser_exception_specification_opt (parser);
11012
11013                   /* Create the function-declarator.  */
11014                   declarator = make_call_declarator (declarator,
11015                                                      params,
11016                                                      cv_quals,
11017                                                      exception_specification);
11018                   /* Any subsequent parameter lists are to do with
11019                      return type, so are not those of the declared
11020                      function.  */
11021                   parser->default_arg_ok_p = false;
11022
11023                   /* Repeat the main loop.  */
11024                   continue;
11025                 }
11026             }
11027
11028           /* If this is the first, we can try a parenthesized
11029              declarator.  */
11030           if (first)
11031             {
11032               bool saved_in_type_id_in_expr_p;
11033
11034               parser->default_arg_ok_p = saved_default_arg_ok_p;
11035               parser->in_declarator_p = saved_in_declarator_p;
11036
11037               /* Consume the `('.  */
11038               cp_lexer_consume_token (parser->lexer);
11039               /* Parse the nested declarator.  */
11040               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11041               parser->in_type_id_in_expr_p = true;
11042               declarator
11043                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11044                                         /*parenthesized_p=*/NULL);
11045               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11046               first = false;
11047               /* Expect a `)'.  */
11048               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11049                 declarator = cp_error_declarator;
11050               if (declarator == cp_error_declarator)
11051                 break;
11052
11053               goto handle_declarator;
11054             }
11055           /* Otherwise, we must be done.  */
11056           else
11057             break;
11058         }
11059       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11060                && token->type == CPP_OPEN_SQUARE)
11061         {
11062           /* Parse an array-declarator.  */
11063           tree bounds;
11064
11065           if (ctor_dtor_or_conv_p)
11066             *ctor_dtor_or_conv_p = 0;
11067
11068           first = false;
11069           parser->default_arg_ok_p = false;
11070           parser->in_declarator_p = true;
11071           /* Consume the `['.  */
11072           cp_lexer_consume_token (parser->lexer);
11073           /* Peek at the next token.  */
11074           token = cp_lexer_peek_token (parser->lexer);
11075           /* If the next token is `]', then there is no
11076              constant-expression.  */
11077           if (token->type != CPP_CLOSE_SQUARE)
11078             {
11079               bool non_constant_p;
11080
11081               bounds
11082                 = cp_parser_constant_expression (parser,
11083                                                  /*allow_non_constant=*/true,
11084                                                  &non_constant_p);
11085               if (!non_constant_p)
11086                 bounds = fold_non_dependent_expr (bounds);
11087             }
11088           else
11089             bounds = NULL_TREE;
11090           /* Look for the closing `]'.  */
11091           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11092             {
11093               declarator = cp_error_declarator;
11094               break;
11095             }
11096
11097           declarator = make_array_declarator (declarator, bounds);
11098         }
11099       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11100         {
11101           tree id;
11102
11103           /* Parse a declarator-id */
11104           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11105             cp_parser_parse_tentatively (parser);
11106           id = cp_parser_declarator_id (parser);
11107           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11108             {
11109               if (!cp_parser_parse_definitely (parser))
11110                 id = error_mark_node;
11111               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11112                 {
11113                   cp_parser_error (parser, "expected unqualified-id");
11114                   id = error_mark_node;
11115                 }
11116             }
11117
11118           if (id == error_mark_node)
11119             {
11120               declarator = cp_error_declarator;
11121               break;
11122             }
11123
11124           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11125             {
11126               tree scope = TREE_OPERAND (id, 0);
11127
11128               /* In the declaration of a member of a template class
11129                  outside of the class itself, the SCOPE will sometimes
11130                  be a TYPENAME_TYPE.  For example, given:
11131
11132                  template <typename T>
11133                  int S<T>::R::i = 3;
11134
11135                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11136                  this context, we must resolve S<T>::R to an ordinary
11137                  type, rather than a typename type.
11138
11139                  The reason we normally avoid resolving TYPENAME_TYPEs
11140                  is that a specialization of `S' might render
11141                  `S<T>::R' not a type.  However, if `S' is
11142                  specialized, then this `i' will not be used, so there
11143                  is no harm in resolving the types here.  */
11144               if (TREE_CODE (scope) == TYPENAME_TYPE)
11145                 {
11146                   tree type;
11147
11148                   /* Resolve the TYPENAME_TYPE.  */
11149                   type = resolve_typename_type (scope,
11150                                                  /*only_current_p=*/false);
11151                   /* If that failed, the declarator is invalid.  */
11152                   if (type == error_mark_node)
11153                     error ("`%T::%D' is not a type",
11154                            TYPE_CONTEXT (scope),
11155                            TYPE_IDENTIFIER (scope));
11156                   /* Build a new DECLARATOR.  */
11157                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11158                 }
11159             }
11160
11161           declarator = make_id_declarator (id);
11162           if (id)
11163             {
11164               tree class_type;
11165               tree unqualified_name;
11166
11167               if (TREE_CODE (id) == SCOPE_REF
11168                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11169                 {
11170                   class_type = TREE_OPERAND (id, 0);
11171                   unqualified_name = TREE_OPERAND (id, 1);
11172                 }
11173               else
11174                 {
11175                   class_type = current_class_type;
11176                   unqualified_name = id;
11177                 }
11178
11179               if (class_type)
11180                 {
11181                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11182                     declarator->u.id.sfk = sfk_destructor;
11183                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11184                     declarator->u.id.sfk = sfk_conversion;
11185                   else if (constructor_name_p (unqualified_name,
11186                                                class_type)
11187                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11188                                && same_type_p (TREE_TYPE (unqualified_name),
11189                                                class_type)))
11190                     declarator->u.id.sfk = sfk_constructor;
11191
11192                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11193                     *ctor_dtor_or_conv_p = -1;
11194                   if (TREE_CODE (id) == SCOPE_REF
11195                       && TREE_CODE (unqualified_name) == TYPE_DECL
11196                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11197                     {
11198                       error ("invalid use of constructor as a template");
11199                       inform ("use `%T::%D' instead of `%T::%T' to name the "
11200                               "constructor in a qualified name", class_type,
11201                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11202                               class_type, class_type);
11203                     }
11204                 }
11205             }
11206
11207         handle_declarator:;
11208           scope = get_scope_of_declarator (declarator);
11209           if (scope)
11210             /* Any names that appear after the declarator-id for a
11211                member are looked up in the containing scope.  */
11212             pop_p = push_scope (scope);
11213           parser->in_declarator_p = true;
11214           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11215               || (declarator && declarator->kind == cdk_id))
11216             /* Default args are only allowed on function
11217                declarations.  */
11218             parser->default_arg_ok_p = saved_default_arg_ok_p;
11219           else
11220             parser->default_arg_ok_p = false;
11221
11222           first = false;
11223         }
11224       /* We're done.  */
11225       else
11226         break;
11227     }
11228
11229   /* For an abstract declarator, we might wind up with nothing at this
11230      point.  That's an error; the declarator is not optional.  */
11231   if (!declarator)
11232     cp_parser_error (parser, "expected declarator");
11233
11234   /* If we entered a scope, we must exit it now.  */
11235   if (pop_p)
11236     pop_scope (scope);
11237
11238   parser->default_arg_ok_p = saved_default_arg_ok_p;
11239   parser->in_declarator_p = saved_in_declarator_p;
11240
11241   return declarator;
11242 }
11243
11244 /* Parse a ptr-operator.
11245
11246    ptr-operator:
11247      * cv-qualifier-seq [opt]
11248      &
11249      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11250
11251    GNU Extension:
11252
11253    ptr-operator:
11254      & cv-qualifier-seq [opt]
11255
11256    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11257    Returns ADDR_EXPR if a reference was used.  In the case of a
11258    pointer-to-member, *TYPE is filled in with the TYPE containing the
11259    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11260    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11261    ERROR_MARK if an error occurred.  */
11262
11263 static enum tree_code
11264 cp_parser_ptr_operator (cp_parser* parser,
11265                         tree* type,
11266                         cp_cv_quals *cv_quals)
11267 {
11268   enum tree_code code = ERROR_MARK;
11269   cp_token *token;
11270
11271   /* Assume that it's not a pointer-to-member.  */
11272   *type = NULL_TREE;
11273   /* And that there are no cv-qualifiers.  */
11274   *cv_quals = TYPE_UNQUALIFIED;
11275
11276   /* Peek at the next token.  */
11277   token = cp_lexer_peek_token (parser->lexer);
11278   /* If it's a `*' or `&' we have a pointer or reference.  */
11279   if (token->type == CPP_MULT || token->type == CPP_AND)
11280     {
11281       /* Remember which ptr-operator we were processing.  */
11282       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11283
11284       /* Consume the `*' or `&'.  */
11285       cp_lexer_consume_token (parser->lexer);
11286
11287       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11288          `&', if we are allowing GNU extensions.  (The only qualifier
11289          that can legally appear after `&' is `restrict', but that is
11290          enforced during semantic analysis.  */
11291       if (code == INDIRECT_REF
11292           || cp_parser_allow_gnu_extensions_p (parser))
11293         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11294     }
11295   else
11296     {
11297       /* Try the pointer-to-member case.  */
11298       cp_parser_parse_tentatively (parser);
11299       /* Look for the optional `::' operator.  */
11300       cp_parser_global_scope_opt (parser,
11301                                   /*current_scope_valid_p=*/false);
11302       /* Look for the nested-name specifier.  */
11303       cp_parser_nested_name_specifier (parser,
11304                                        /*typename_keyword_p=*/false,
11305                                        /*check_dependency_p=*/true,
11306                                        /*type_p=*/false,
11307                                        /*is_declaration=*/false);
11308       /* If we found it, and the next token is a `*', then we are
11309          indeed looking at a pointer-to-member operator.  */
11310       if (!cp_parser_error_occurred (parser)
11311           && cp_parser_require (parser, CPP_MULT, "`*'"))
11312         {
11313           /* The type of which the member is a member is given by the
11314              current SCOPE.  */
11315           *type = parser->scope;
11316           /* The next name will not be qualified.  */
11317           parser->scope = NULL_TREE;
11318           parser->qualifying_scope = NULL_TREE;
11319           parser->object_scope = NULL_TREE;
11320           /* Indicate that the `*' operator was used.  */
11321           code = INDIRECT_REF;
11322           /* Look for the optional cv-qualifier-seq.  */
11323           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11324         }
11325       /* If that didn't work we don't have a ptr-operator.  */
11326       if (!cp_parser_parse_definitely (parser))
11327         cp_parser_error (parser, "expected ptr-operator");
11328     }
11329
11330   return code;
11331 }
11332
11333 /* Parse an (optional) cv-qualifier-seq.
11334
11335    cv-qualifier-seq:
11336      cv-qualifier cv-qualifier-seq [opt]
11337
11338    cv-qualifier:
11339      const
11340      volatile
11341
11342    GNU Extension:
11343
11344    cv-qualifier:
11345      __restrict__
11346
11347    Returns a bitmask representing the cv-qualifiers.  */
11348
11349 static cp_cv_quals
11350 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11351 {
11352   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11353
11354   while (true)
11355     {
11356       cp_token *token;
11357       cp_cv_quals cv_qualifier;
11358
11359       /* Peek at the next token.  */
11360       token = cp_lexer_peek_token (parser->lexer);
11361       /* See if it's a cv-qualifier.  */
11362       switch (token->keyword)
11363         {
11364         case RID_CONST:
11365           cv_qualifier = TYPE_QUAL_CONST;
11366           break;
11367
11368         case RID_VOLATILE:
11369           cv_qualifier = TYPE_QUAL_VOLATILE;
11370           break;
11371
11372         case RID_RESTRICT:
11373           cv_qualifier = TYPE_QUAL_RESTRICT;
11374           break;
11375
11376         default:
11377           cv_qualifier = TYPE_UNQUALIFIED;
11378           break;
11379         }
11380
11381       if (!cv_qualifier)
11382         break;
11383
11384       if (cv_quals & cv_qualifier)
11385         {
11386           error ("duplicate cv-qualifier");
11387           cp_lexer_purge_token (parser->lexer);
11388         }
11389       else
11390         {
11391           cp_lexer_consume_token (parser->lexer);
11392           cv_quals |= cv_qualifier;
11393         }
11394     }
11395
11396   return cv_quals;
11397 }
11398
11399 /* Parse a declarator-id.
11400
11401    declarator-id:
11402      id-expression
11403      :: [opt] nested-name-specifier [opt] type-name
11404
11405    In the `id-expression' case, the value returned is as for
11406    cp_parser_id_expression if the id-expression was an unqualified-id.
11407    If the id-expression was a qualified-id, then a SCOPE_REF is
11408    returned.  The first operand is the scope (either a NAMESPACE_DECL
11409    or TREE_TYPE), but the second is still just a representation of an
11410    unqualified-id.  */
11411
11412 static tree
11413 cp_parser_declarator_id (cp_parser* parser)
11414 {
11415   tree id_expression;
11416
11417   /* The expression must be an id-expression.  Assume that qualified
11418      names are the names of types so that:
11419
11420        template <class T>
11421        int S<T>::R::i = 3;
11422
11423      will work; we must treat `S<T>::R' as the name of a type.
11424      Similarly, assume that qualified names are templates, where
11425      required, so that:
11426
11427        template <class T>
11428        int S<T>::R<T>::i = 3;
11429
11430      will work, too.  */
11431   id_expression = cp_parser_id_expression (parser,
11432                                            /*template_keyword_p=*/false,
11433                                            /*check_dependency_p=*/false,
11434                                            /*template_p=*/NULL,
11435                                            /*declarator_p=*/true);
11436   /* If the name was qualified, create a SCOPE_REF to represent
11437      that.  */
11438   if (parser->scope)
11439     {
11440       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11441       parser->scope = NULL_TREE;
11442     }
11443
11444   return id_expression;
11445 }
11446
11447 /* Parse a type-id.
11448
11449    type-id:
11450      type-specifier-seq abstract-declarator [opt]
11451
11452    Returns the TYPE specified.  */
11453
11454 static tree
11455 cp_parser_type_id (cp_parser* parser)
11456 {
11457   cp_decl_specifier_seq type_specifier_seq;
11458   cp_declarator *abstract_declarator;
11459
11460   /* Parse the type-specifier-seq.  */
11461   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11462   if (type_specifier_seq.type == error_mark_node)
11463     return error_mark_node;
11464
11465   /* There might or might not be an abstract declarator.  */
11466   cp_parser_parse_tentatively (parser);
11467   /* Look for the declarator.  */
11468   abstract_declarator
11469     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11470                             /*parenthesized_p=*/NULL);
11471   /* Check to see if there really was a declarator.  */
11472   if (!cp_parser_parse_definitely (parser))
11473     abstract_declarator = NULL;
11474
11475   return groktypename (&type_specifier_seq, abstract_declarator);
11476 }
11477
11478 /* Parse a type-specifier-seq.
11479
11480    type-specifier-seq:
11481      type-specifier type-specifier-seq [opt]
11482
11483    GNU extension:
11484
11485    type-specifier-seq:
11486      attributes type-specifier-seq [opt]
11487
11488    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11489
11490 static void
11491 cp_parser_type_specifier_seq (cp_parser* parser,
11492                               cp_decl_specifier_seq *type_specifier_seq)
11493 {
11494   bool seen_type_specifier = false;
11495
11496   /* Clear the TYPE_SPECIFIER_SEQ.  */
11497   clear_decl_specs (type_specifier_seq);
11498
11499   /* Parse the type-specifiers and attributes.  */
11500   while (true)
11501     {
11502       tree type_specifier;
11503
11504       /* Check for attributes first.  */
11505       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11506         {
11507           type_specifier_seq->attributes =
11508             chainon (type_specifier_seq->attributes,
11509                      cp_parser_attributes_opt (parser));
11510           continue;
11511         }
11512
11513       /* Look for the type-specifier.  */
11514       type_specifier = cp_parser_type_specifier (parser,
11515                                                  CP_PARSER_FLAGS_OPTIONAL,
11516                                                  type_specifier_seq,
11517                                                  /*is_declaration=*/false,
11518                                                  NULL,
11519                                                  NULL);
11520       /* If the first type-specifier could not be found, this is not a
11521          type-specifier-seq at all.  */
11522       if (!seen_type_specifier && !type_specifier)
11523         {
11524           cp_parser_error (parser, "expected type-specifier");
11525           type_specifier_seq->type = error_mark_node;
11526           return;
11527         }
11528       /* If subsequent type-specifiers could not be found, the
11529          type-specifier-seq is complete.  */
11530       else if (seen_type_specifier && !type_specifier)
11531         break;
11532
11533       seen_type_specifier = true;
11534     }
11535
11536   return;
11537 }
11538
11539 /* Parse a parameter-declaration-clause.
11540
11541    parameter-declaration-clause:
11542      parameter-declaration-list [opt] ... [opt]
11543      parameter-declaration-list , ...
11544
11545    Returns a representation for the parameter declarations.  A return
11546    value of NULL indicates a parameter-declaration-clause consisting
11547    only of an ellipsis.  */
11548
11549 static cp_parameter_declarator *
11550 cp_parser_parameter_declaration_clause (cp_parser* parser)
11551 {
11552   cp_parameter_declarator *parameters;
11553   cp_token *token;
11554   bool ellipsis_p;
11555   bool is_error;
11556
11557   /* Peek at the next token.  */
11558   token = cp_lexer_peek_token (parser->lexer);
11559   /* Check for trivial parameter-declaration-clauses.  */
11560   if (token->type == CPP_ELLIPSIS)
11561     {
11562       /* Consume the `...' token.  */
11563       cp_lexer_consume_token (parser->lexer);
11564       return NULL;
11565     }
11566   else if (token->type == CPP_CLOSE_PAREN)
11567     /* There are no parameters.  */
11568     {
11569 #ifndef NO_IMPLICIT_EXTERN_C
11570       if (in_system_header && current_class_type == NULL
11571           && current_lang_name == lang_name_c)
11572         return NULL;
11573       else
11574 #endif
11575         return no_parameters;
11576     }
11577   /* Check for `(void)', too, which is a special case.  */
11578   else if (token->keyword == RID_VOID
11579            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11580                == CPP_CLOSE_PAREN))
11581     {
11582       /* Consume the `void' token.  */
11583       cp_lexer_consume_token (parser->lexer);
11584       /* There are no parameters.  */
11585       return no_parameters;
11586     }
11587
11588   /* Parse the parameter-declaration-list.  */
11589   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11590   /* If a parse error occurred while parsing the
11591      parameter-declaration-list, then the entire
11592      parameter-declaration-clause is erroneous.  */
11593   if (is_error)
11594     return NULL;
11595
11596   /* Peek at the next token.  */
11597   token = cp_lexer_peek_token (parser->lexer);
11598   /* If it's a `,', the clause should terminate with an ellipsis.  */
11599   if (token->type == CPP_COMMA)
11600     {
11601       /* Consume the `,'.  */
11602       cp_lexer_consume_token (parser->lexer);
11603       /* Expect an ellipsis.  */
11604       ellipsis_p
11605         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11606     }
11607   /* It might also be `...' if the optional trailing `,' was
11608      omitted.  */
11609   else if (token->type == CPP_ELLIPSIS)
11610     {
11611       /* Consume the `...' token.  */
11612       cp_lexer_consume_token (parser->lexer);
11613       /* And remember that we saw it.  */
11614       ellipsis_p = true;
11615     }
11616   else
11617     ellipsis_p = false;
11618
11619   /* Finish the parameter list.  */
11620   if (parameters && ellipsis_p)
11621     parameters->ellipsis_p = true;
11622
11623   return parameters;
11624 }
11625
11626 /* Parse a parameter-declaration-list.
11627
11628    parameter-declaration-list:
11629      parameter-declaration
11630      parameter-declaration-list , parameter-declaration
11631
11632    Returns a representation of the parameter-declaration-list, as for
11633    cp_parser_parameter_declaration_clause.  However, the
11634    `void_list_node' is never appended to the list.  Upon return,
11635    *IS_ERROR will be true iff an error occurred.  */
11636
11637 static cp_parameter_declarator *
11638 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11639 {
11640   cp_parameter_declarator *parameters = NULL;
11641   cp_parameter_declarator **tail = &parameters;
11642
11643   /* Assume all will go well.  */
11644   *is_error = false;
11645
11646   /* Look for more parameters.  */
11647   while (true)
11648     {
11649       cp_parameter_declarator *parameter;
11650       bool parenthesized_p;
11651       /* Parse the parameter.  */
11652       parameter
11653         = cp_parser_parameter_declaration (parser,
11654                                            /*template_parm_p=*/false,
11655                                            &parenthesized_p);
11656
11657       /* If a parse error occurred parsing the parameter declaration,
11658          then the entire parameter-declaration-list is erroneous.  */
11659       if (!parameter)
11660         {
11661           *is_error = true;
11662           parameters = NULL;
11663           break;
11664         }
11665       /* Add the new parameter to the list.  */
11666       *tail = parameter;
11667       tail = &parameter->next;
11668
11669       /* Peek at the next token.  */
11670       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11671           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11672         /* The parameter-declaration-list is complete.  */
11673         break;
11674       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11675         {
11676           cp_token *token;
11677
11678           /* Peek at the next token.  */
11679           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11680           /* If it's an ellipsis, then the list is complete.  */
11681           if (token->type == CPP_ELLIPSIS)
11682             break;
11683           /* Otherwise, there must be more parameters.  Consume the
11684              `,'.  */
11685           cp_lexer_consume_token (parser->lexer);
11686           /* When parsing something like:
11687
11688                 int i(float f, double d)
11689
11690              we can tell after seeing the declaration for "f" that we
11691              are not looking at an initialization of a variable "i",
11692              but rather at the declaration of a function "i".
11693
11694              Due to the fact that the parsing of template arguments
11695              (as specified to a template-id) requires backtracking we
11696              cannot use this technique when inside a template argument
11697              list.  */
11698           if (!parser->in_template_argument_list_p
11699               && !parser->in_type_id_in_expr_p
11700               && cp_parser_parsing_tentatively (parser)
11701               && !cp_parser_committed_to_tentative_parse (parser)
11702               /* However, a parameter-declaration of the form
11703                  "foat(f)" (which is a valid declaration of a
11704                  parameter "f") can also be interpreted as an
11705                  expression (the conversion of "f" to "float").  */
11706               && !parenthesized_p)
11707             cp_parser_commit_to_tentative_parse (parser);
11708         }
11709       else
11710         {
11711           cp_parser_error (parser, "expected `,' or `...'");
11712           if (!cp_parser_parsing_tentatively (parser)
11713               || cp_parser_committed_to_tentative_parse (parser))
11714             cp_parser_skip_to_closing_parenthesis (parser,
11715                                                    /*recovering=*/true,
11716                                                    /*or_comma=*/false,
11717                                                    /*consume_paren=*/false);
11718           break;
11719         }
11720     }
11721
11722   return parameters;
11723 }
11724
11725 /* Parse a parameter declaration.
11726
11727    parameter-declaration:
11728      decl-specifier-seq declarator
11729      decl-specifier-seq declarator = assignment-expression
11730      decl-specifier-seq abstract-declarator [opt]
11731      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11732
11733    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11734    declares a template parameter.  (In that case, a non-nested `>'
11735    token encountered during the parsing of the assignment-expression
11736    is not interpreted as a greater-than operator.)
11737
11738    Returns a representation of the parameter, or NULL if an error
11739    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11740    true iff the declarator is of the form "(p)".  */
11741
11742 static cp_parameter_declarator *
11743 cp_parser_parameter_declaration (cp_parser *parser,
11744                                  bool template_parm_p,
11745                                  bool *parenthesized_p)
11746 {
11747   int declares_class_or_enum;
11748   bool greater_than_is_operator_p;
11749   cp_decl_specifier_seq decl_specifiers;
11750   cp_declarator *declarator;
11751   tree default_argument;
11752   cp_token *token;
11753   const char *saved_message;
11754
11755   /* In a template parameter, `>' is not an operator.
11756
11757      [temp.param]
11758
11759      When parsing a default template-argument for a non-type
11760      template-parameter, the first non-nested `>' is taken as the end
11761      of the template parameter-list rather than a greater-than
11762      operator.  */
11763   greater_than_is_operator_p = !template_parm_p;
11764
11765   /* Type definitions may not appear in parameter types.  */
11766   saved_message = parser->type_definition_forbidden_message;
11767   parser->type_definition_forbidden_message
11768     = "types may not be defined in parameter types";
11769
11770   /* Parse the declaration-specifiers.  */
11771   cp_parser_decl_specifier_seq (parser,
11772                                 CP_PARSER_FLAGS_NONE,
11773                                 &decl_specifiers,
11774                                 &declares_class_or_enum);
11775   /* If an error occurred, there's no reason to attempt to parse the
11776      rest of the declaration.  */
11777   if (cp_parser_error_occurred (parser))
11778     {
11779       parser->type_definition_forbidden_message = saved_message;
11780       return NULL;
11781     }
11782
11783   /* Peek at the next token.  */
11784   token = cp_lexer_peek_token (parser->lexer);
11785   /* If the next token is a `)', `,', `=', `>', or `...', then there
11786      is no declarator.  */
11787   if (token->type == CPP_CLOSE_PAREN
11788       || token->type == CPP_COMMA
11789       || token->type == CPP_EQ
11790       || token->type == CPP_ELLIPSIS
11791       || token->type == CPP_GREATER)
11792     {
11793       declarator = NULL;
11794       if (parenthesized_p)
11795         *parenthesized_p = false;
11796     }
11797   /* Otherwise, there should be a declarator.  */
11798   else
11799     {
11800       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11801       parser->default_arg_ok_p = false;
11802
11803       /* After seeing a decl-specifier-seq, if the next token is not a
11804          "(", there is no possibility that the code is a valid
11805          expression.  Therefore, if parsing tentatively, we commit at
11806          this point.  */
11807       if (!parser->in_template_argument_list_p
11808           /* In an expression context, having seen:
11809
11810                (int((char ...
11811
11812              we cannot be sure whether we are looking at a
11813              function-type (taking a "char" as a parameter) or a cast
11814              of some object of type "char" to "int".  */
11815           && !parser->in_type_id_in_expr_p
11816           && cp_parser_parsing_tentatively (parser)
11817           && !cp_parser_committed_to_tentative_parse (parser)
11818           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11819         cp_parser_commit_to_tentative_parse (parser);
11820       /* Parse the declarator.  */
11821       declarator = cp_parser_declarator (parser,
11822                                          CP_PARSER_DECLARATOR_EITHER,
11823                                          /*ctor_dtor_or_conv_p=*/NULL,
11824                                          parenthesized_p);
11825       parser->default_arg_ok_p = saved_default_arg_ok_p;
11826       /* After the declarator, allow more attributes.  */
11827       decl_specifiers.attributes
11828         = chainon (decl_specifiers.attributes,
11829                    cp_parser_attributes_opt (parser));
11830     }
11831
11832   /* The restriction on defining new types applies only to the type
11833      of the parameter, not to the default argument.  */
11834   parser->type_definition_forbidden_message = saved_message;
11835
11836   /* If the next token is `=', then process a default argument.  */
11837   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11838     {
11839       bool saved_greater_than_is_operator_p;
11840       /* Consume the `='.  */
11841       cp_lexer_consume_token (parser->lexer);
11842
11843       /* If we are defining a class, then the tokens that make up the
11844          default argument must be saved and processed later.  */
11845       if (!template_parm_p && at_class_scope_p ()
11846           && TYPE_BEING_DEFINED (current_class_type))
11847         {
11848           unsigned depth = 0;
11849
11850           /* Create a DEFAULT_ARG to represented the unparsed default
11851              argument.  */
11852           default_argument = make_node (DEFAULT_ARG);
11853           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11854
11855           /* Add tokens until we have processed the entire default
11856              argument.  */
11857           while (true)
11858             {
11859               bool done = false;
11860               cp_token *token;
11861
11862               /* Peek at the next token.  */
11863               token = cp_lexer_peek_token (parser->lexer);
11864               /* What we do depends on what token we have.  */
11865               switch (token->type)
11866                 {
11867                   /* In valid code, a default argument must be
11868                      immediately followed by a `,' `)', or `...'.  */
11869                 case CPP_COMMA:
11870                 case CPP_CLOSE_PAREN:
11871                 case CPP_ELLIPSIS:
11872                   /* If we run into a non-nested `;', `}', or `]',
11873                      then the code is invalid -- but the default
11874                      argument is certainly over.  */
11875                 case CPP_SEMICOLON:
11876                 case CPP_CLOSE_BRACE:
11877                 case CPP_CLOSE_SQUARE:
11878                   if (depth == 0)
11879                     done = true;
11880                   /* Update DEPTH, if necessary.  */
11881                   else if (token->type == CPP_CLOSE_PAREN
11882                            || token->type == CPP_CLOSE_BRACE
11883                            || token->type == CPP_CLOSE_SQUARE)
11884                     --depth;
11885                   break;
11886
11887                 case CPP_OPEN_PAREN:
11888                 case CPP_OPEN_SQUARE:
11889                 case CPP_OPEN_BRACE:
11890                   ++depth;
11891                   break;
11892
11893                 case CPP_GREATER:
11894                   /* If we see a non-nested `>', and `>' is not an
11895                      operator, then it marks the end of the default
11896                      argument.  */
11897                   if (!depth && !greater_than_is_operator_p)
11898                     done = true;
11899                   break;
11900
11901                   /* If we run out of tokens, issue an error message.  */
11902                 case CPP_EOF:
11903                   error ("file ends in default argument");
11904                   done = true;
11905                   break;
11906
11907                 case CPP_NAME:
11908                 case CPP_SCOPE:
11909                   /* In these cases, we should look for template-ids.
11910                      For example, if the default argument is
11911                      `X<int, double>()', we need to do name lookup to
11912                      figure out whether or not `X' is a template; if
11913                      so, the `,' does not end the default argument.
11914
11915                      That is not yet done.  */
11916                   break;
11917
11918                 default:
11919                   break;
11920                 }
11921
11922               /* If we've reached the end, stop.  */
11923               if (done)
11924                 break;
11925
11926               /* Add the token to the token block.  */
11927               token = cp_lexer_consume_token (parser->lexer);
11928               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11929                                          token);
11930             }
11931         }
11932       /* Outside of a class definition, we can just parse the
11933          assignment-expression.  */
11934       else
11935         {
11936           bool saved_local_variables_forbidden_p;
11937
11938           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11939              set correctly.  */
11940           saved_greater_than_is_operator_p
11941             = parser->greater_than_is_operator_p;
11942           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11943           /* Local variable names (and the `this' keyword) may not
11944              appear in a default argument.  */
11945           saved_local_variables_forbidden_p
11946             = parser->local_variables_forbidden_p;
11947           parser->local_variables_forbidden_p = true;
11948           /* Parse the assignment-expression.  */
11949           default_argument = cp_parser_assignment_expression (parser);
11950           /* Restore saved state.  */
11951           parser->greater_than_is_operator_p
11952             = saved_greater_than_is_operator_p;
11953           parser->local_variables_forbidden_p
11954             = saved_local_variables_forbidden_p;
11955         }
11956       if (!parser->default_arg_ok_p)
11957         {
11958           if (!flag_pedantic_errors)
11959             warning ("deprecated use of default argument for parameter of non-function");
11960           else
11961             {
11962               error ("default arguments are only permitted for function parameters");
11963               default_argument = NULL_TREE;
11964             }
11965         }
11966     }
11967   else
11968     default_argument = NULL_TREE;
11969
11970   return make_parameter_declarator (&decl_specifiers,
11971                                     declarator,
11972                                     default_argument);
11973 }
11974
11975 /* Parse a function-body.
11976
11977    function-body:
11978      compound_statement  */
11979
11980 static void
11981 cp_parser_function_body (cp_parser *parser)
11982 {
11983   cp_parser_compound_statement (parser, NULL, false);
11984 }
11985
11986 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11987    true if a ctor-initializer was present.  */
11988
11989 static bool
11990 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11991 {
11992   tree body;
11993   bool ctor_initializer_p;
11994
11995   /* Begin the function body.  */
11996   body = begin_function_body ();
11997   /* Parse the optional ctor-initializer.  */
11998   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11999   /* Parse the function-body.  */
12000   cp_parser_function_body (parser);
12001   /* Finish the function body.  */
12002   finish_function_body (body);
12003
12004   return ctor_initializer_p;
12005 }
12006
12007 /* Parse an initializer.
12008
12009    initializer:
12010      = initializer-clause
12011      ( expression-list )
12012
12013    Returns a expression representing the initializer.  If no
12014    initializer is present, NULL_TREE is returned.
12015
12016    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12017    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12018    set to FALSE if there is no initializer present.  If there is an
12019    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12020    is set to true; otherwise it is set to false.  */
12021
12022 static tree
12023 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12024                        bool* non_constant_p)
12025 {
12026   cp_token *token;
12027   tree init;
12028
12029   /* Peek at the next token.  */
12030   token = cp_lexer_peek_token (parser->lexer);
12031
12032   /* Let our caller know whether or not this initializer was
12033      parenthesized.  */
12034   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12035   /* Assume that the initializer is constant.  */
12036   *non_constant_p = false;
12037
12038   if (token->type == CPP_EQ)
12039     {
12040       /* Consume the `='.  */
12041       cp_lexer_consume_token (parser->lexer);
12042       /* Parse the initializer-clause.  */
12043       init = cp_parser_initializer_clause (parser, non_constant_p);
12044     }
12045   else if (token->type == CPP_OPEN_PAREN)
12046     init = cp_parser_parenthesized_expression_list (parser, false,
12047                                                     non_constant_p);
12048   else
12049     {
12050       /* Anything else is an error.  */
12051       cp_parser_error (parser, "expected initializer");
12052       init = error_mark_node;
12053     }
12054
12055   return init;
12056 }
12057
12058 /* Parse an initializer-clause.
12059
12060    initializer-clause:
12061      assignment-expression
12062      { initializer-list , [opt] }
12063      { }
12064
12065    Returns an expression representing the initializer.
12066
12067    If the `assignment-expression' production is used the value
12068    returned is simply a representation for the expression.
12069
12070    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12071    the elements of the initializer-list (or NULL_TREE, if the last
12072    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12073    NULL_TREE.  There is no way to detect whether or not the optional
12074    trailing `,' was provided.  NON_CONSTANT_P is as for
12075    cp_parser_initializer.  */
12076
12077 static tree
12078 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12079 {
12080   tree initializer;
12081
12082   /* If it is not a `{', then we are looking at an
12083      assignment-expression.  */
12084   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12085     {
12086       initializer
12087         = cp_parser_constant_expression (parser,
12088                                         /*allow_non_constant_p=*/true,
12089                                         non_constant_p);
12090       if (!*non_constant_p)
12091         initializer = fold_non_dependent_expr (initializer);
12092     }
12093   else
12094     {
12095       /* Consume the `{' token.  */
12096       cp_lexer_consume_token (parser->lexer);
12097       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12098       initializer = make_node (CONSTRUCTOR);
12099       /* If it's not a `}', then there is a non-trivial initializer.  */
12100       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12101         {
12102           /* Parse the initializer list.  */
12103           CONSTRUCTOR_ELTS (initializer)
12104             = cp_parser_initializer_list (parser, non_constant_p);
12105           /* A trailing `,' token is allowed.  */
12106           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12107             cp_lexer_consume_token (parser->lexer);
12108         }
12109       /* Now, there should be a trailing `}'.  */
12110       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12111     }
12112
12113   return initializer;
12114 }
12115
12116 /* Parse an initializer-list.
12117
12118    initializer-list:
12119      initializer-clause
12120      initializer-list , initializer-clause
12121
12122    GNU Extension:
12123
12124    initializer-list:
12125      identifier : initializer-clause
12126      initializer-list, identifier : initializer-clause
12127
12128    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12129    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12130    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12131    as for cp_parser_initializer.  */
12132
12133 static tree
12134 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12135 {
12136   tree initializers = NULL_TREE;
12137
12138   /* Assume all of the expressions are constant.  */
12139   *non_constant_p = false;
12140
12141   /* Parse the rest of the list.  */
12142   while (true)
12143     {
12144       cp_token *token;
12145       tree identifier;
12146       tree initializer;
12147       bool clause_non_constant_p;
12148
12149       /* If the next token is an identifier and the following one is a
12150          colon, we are looking at the GNU designated-initializer
12151          syntax.  */
12152       if (cp_parser_allow_gnu_extensions_p (parser)
12153           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12154           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12155         {
12156           /* Consume the identifier.  */
12157           identifier = cp_lexer_consume_token (parser->lexer)->value;
12158           /* Consume the `:'.  */
12159           cp_lexer_consume_token (parser->lexer);
12160         }
12161       else
12162         identifier = NULL_TREE;
12163
12164       /* Parse the initializer.  */
12165       initializer = cp_parser_initializer_clause (parser,
12166                                                   &clause_non_constant_p);
12167       /* If any clause is non-constant, so is the entire initializer.  */
12168       if (clause_non_constant_p)
12169         *non_constant_p = true;
12170       /* Add it to the list.  */
12171       initializers = tree_cons (identifier, initializer, initializers);
12172
12173       /* If the next token is not a comma, we have reached the end of
12174          the list.  */
12175       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12176         break;
12177
12178       /* Peek at the next token.  */
12179       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12180       /* If the next token is a `}', then we're still done.  An
12181          initializer-clause can have a trailing `,' after the
12182          initializer-list and before the closing `}'.  */
12183       if (token->type == CPP_CLOSE_BRACE)
12184         break;
12185
12186       /* Consume the `,' token.  */
12187       cp_lexer_consume_token (parser->lexer);
12188     }
12189
12190   /* The initializers were built up in reverse order, so we need to
12191      reverse them now.  */
12192   return nreverse (initializers);
12193 }
12194
12195 /* Classes [gram.class] */
12196
12197 /* Parse a class-name.
12198
12199    class-name:
12200      identifier
12201      template-id
12202
12203    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12204    to indicate that names looked up in dependent types should be
12205    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12206    keyword has been used to indicate that the name that appears next
12207    is a template.  TYPE_P is true iff the next name should be treated
12208    as class-name, even if it is declared to be some other kind of name
12209    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12210    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12211    being defined in a class-head.
12212
12213    Returns the TYPE_DECL representing the class.  */
12214
12215 static tree
12216 cp_parser_class_name (cp_parser *parser,
12217                       bool typename_keyword_p,
12218                       bool template_keyword_p,
12219                       bool type_p,
12220                       bool check_dependency_p,
12221                       bool class_head_p,
12222                       bool is_declaration)
12223 {
12224   tree decl;
12225   tree scope;
12226   bool typename_p;
12227   cp_token *token;
12228
12229   /* All class-names start with an identifier.  */
12230   token = cp_lexer_peek_token (parser->lexer);
12231   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12232     {
12233       cp_parser_error (parser, "expected class-name");
12234       return error_mark_node;
12235     }
12236
12237   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12238      to a template-id, so we save it here.  */
12239   scope = parser->scope;
12240   if (scope == error_mark_node)
12241     return error_mark_node;
12242
12243   /* Any name names a type if we're following the `typename' keyword
12244      in a qualified name where the enclosing scope is type-dependent.  */
12245   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12246                 && dependent_type_p (scope));
12247   /* Handle the common case (an identifier, but not a template-id)
12248      efficiently.  */
12249   if (token->type == CPP_NAME
12250       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12251     {
12252       tree identifier;
12253
12254       /* Look for the identifier.  */
12255       identifier = cp_parser_identifier (parser);
12256       /* If the next token isn't an identifier, we are certainly not
12257          looking at a class-name.  */
12258       if (identifier == error_mark_node)
12259         decl = error_mark_node;
12260       /* If we know this is a type-name, there's no need to look it
12261          up.  */
12262       else if (typename_p)
12263         decl = identifier;
12264       else
12265         {
12266           /* If the next token is a `::', then the name must be a type
12267              name.
12268
12269              [basic.lookup.qual]
12270
12271              During the lookup for a name preceding the :: scope
12272              resolution operator, object, function, and enumerator
12273              names are ignored.  */
12274           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12275             type_p = true;
12276           /* Look up the name.  */
12277           decl = cp_parser_lookup_name (parser, identifier,
12278                                         type_p,
12279                                         /*is_template=*/false,
12280                                         /*is_namespace=*/false,
12281                                         check_dependency_p,
12282                                         /*ambiguous_p=*/NULL);
12283         }
12284     }
12285   else
12286     {
12287       /* Try a template-id.  */
12288       decl = cp_parser_template_id (parser, template_keyword_p,
12289                                     check_dependency_p,
12290                                     is_declaration);
12291       if (decl == error_mark_node)
12292         return error_mark_node;
12293     }
12294
12295   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12296
12297   /* If this is a typename, create a TYPENAME_TYPE.  */
12298   if (typename_p && decl != error_mark_node)
12299     {
12300       decl = make_typename_type (scope, decl, /*complain=*/1);
12301       if (decl != error_mark_node)
12302         decl = TYPE_NAME (decl);
12303     }
12304
12305   /* Check to see that it is really the name of a class.  */
12306   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12307       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12308       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12309     /* Situations like this:
12310
12311          template <typename T> struct A {
12312            typename T::template X<int>::I i;
12313          };
12314
12315        are problematic.  Is `T::template X<int>' a class-name?  The
12316        standard does not seem to be definitive, but there is no other
12317        valid interpretation of the following `::'.  Therefore, those
12318        names are considered class-names.  */
12319     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12320   else if (decl == error_mark_node
12321            || TREE_CODE (decl) != TYPE_DECL
12322            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12323     {
12324       cp_parser_error (parser, "expected class-name");
12325       return error_mark_node;
12326     }
12327
12328   return decl;
12329 }
12330
12331 /* Parse a class-specifier.
12332
12333    class-specifier:
12334      class-head { member-specification [opt] }
12335
12336    Returns the TREE_TYPE representing the class.  */
12337
12338 static tree
12339 cp_parser_class_specifier (cp_parser* parser)
12340 {
12341   cp_token *token;
12342   tree type;
12343   tree attributes = NULL_TREE;
12344   int has_trailing_semicolon;
12345   bool nested_name_specifier_p;
12346   unsigned saved_num_template_parameter_lists;
12347   bool pop_p = false;
12348   tree scope = NULL_TREE;
12349
12350   push_deferring_access_checks (dk_no_deferred);
12351
12352   /* Parse the class-head.  */
12353   type = cp_parser_class_head (parser,
12354                                &nested_name_specifier_p,
12355                                &attributes);
12356   /* If the class-head was a semantic disaster, skip the entire body
12357      of the class.  */
12358   if (!type)
12359     {
12360       cp_parser_skip_to_end_of_block_or_statement (parser);
12361       pop_deferring_access_checks ();
12362       return error_mark_node;
12363     }
12364
12365   /* Look for the `{'.  */
12366   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12367     {
12368       pop_deferring_access_checks ();
12369       return error_mark_node;
12370     }
12371
12372   /* Issue an error message if type-definitions are forbidden here.  */
12373   cp_parser_check_type_definition (parser);
12374   /* Remember that we are defining one more class.  */
12375   ++parser->num_classes_being_defined;
12376   /* Inside the class, surrounding template-parameter-lists do not
12377      apply.  */
12378   saved_num_template_parameter_lists
12379     = parser->num_template_parameter_lists;
12380   parser->num_template_parameter_lists = 0;
12381
12382   /* Start the class.  */
12383   if (nested_name_specifier_p)
12384     {
12385       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12386       pop_p = push_scope (scope);
12387     }
12388   type = begin_class_definition (type);
12389
12390   if (type == error_mark_node)
12391     /* If the type is erroneous, skip the entire body of the class.  */
12392     cp_parser_skip_to_closing_brace (parser);
12393   else
12394     /* Parse the member-specification.  */
12395     cp_parser_member_specification_opt (parser);
12396
12397   /* Look for the trailing `}'.  */
12398   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12399   /* We get better error messages by noticing a common problem: a
12400      missing trailing `;'.  */
12401   token = cp_lexer_peek_token (parser->lexer);
12402   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12403   /* Look for trailing attributes to apply to this class.  */
12404   if (cp_parser_allow_gnu_extensions_p (parser))
12405     {
12406       tree sub_attr = cp_parser_attributes_opt (parser);
12407       attributes = chainon (attributes, sub_attr);
12408     }
12409   if (type != error_mark_node)
12410     type = finish_struct (type, attributes);
12411   if (pop_p)
12412     pop_scope (scope);
12413   /* If this class is not itself within the scope of another class,
12414      then we need to parse the bodies of all of the queued function
12415      definitions.  Note that the queued functions defined in a class
12416      are not always processed immediately following the
12417      class-specifier for that class.  Consider:
12418
12419        struct A {
12420          struct B { void f() { sizeof (A); } };
12421        };
12422
12423      If `f' were processed before the processing of `A' were
12424      completed, there would be no way to compute the size of `A'.
12425      Note that the nesting we are interested in here is lexical --
12426      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12427      for:
12428
12429        struct A { struct B; };
12430        struct A::B { void f() { } };
12431
12432      there is no need to delay the parsing of `A::B::f'.  */
12433   if (--parser->num_classes_being_defined == 0)
12434     {
12435       tree queue_entry;
12436       tree fn;
12437       tree class_type;
12438       bool pop_p;
12439
12440       /* In a first pass, parse default arguments to the functions.
12441          Then, in a second pass, parse the bodies of the functions.
12442          This two-phased approach handles cases like:
12443
12444             struct S {
12445               void f() { g(); }
12446               void g(int i = 3);
12447             };
12448
12449          */
12450       class_type = NULL_TREE;
12451       pop_p = false;
12452       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12453              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12454            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12455            TREE_PURPOSE (parser->unparsed_functions_queues)
12456              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12457         {
12458           fn = TREE_VALUE (queue_entry);
12459           /* If there are default arguments that have not yet been processed,
12460              take care of them now.  */
12461           if (class_type != TREE_PURPOSE (queue_entry))
12462             {
12463               if (pop_p)
12464                 pop_scope (class_type);
12465               class_type = TREE_PURPOSE (queue_entry);
12466               pop_p = push_scope (class_type);
12467             }
12468           /* Make sure that any template parameters are in scope.  */
12469           maybe_begin_member_template_processing (fn);
12470           /* Parse the default argument expressions.  */
12471           cp_parser_late_parsing_default_args (parser, fn);
12472           /* Remove any template parameters from the symbol table.  */
12473           maybe_end_member_template_processing ();
12474         }
12475       if (pop_p)
12476         pop_scope (class_type);
12477       /* Now parse the body of the functions.  */
12478       for (TREE_VALUE (parser->unparsed_functions_queues)
12479              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12480            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12481            TREE_VALUE (parser->unparsed_functions_queues)
12482              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12483         {
12484           /* Figure out which function we need to process.  */
12485           fn = TREE_VALUE (queue_entry);
12486
12487           /* A hack to prevent garbage collection.  */
12488           function_depth++;
12489
12490           /* Parse the function.  */
12491           cp_parser_late_parsing_for_member (parser, fn);
12492           function_depth--;
12493         }
12494     }
12495
12496   /* Put back any saved access checks.  */
12497   pop_deferring_access_checks ();
12498
12499   /* Restore the count of active template-parameter-lists.  */
12500   parser->num_template_parameter_lists
12501     = saved_num_template_parameter_lists;
12502
12503   return type;
12504 }
12505
12506 /* Parse a class-head.
12507
12508    class-head:
12509      class-key identifier [opt] base-clause [opt]
12510      class-key nested-name-specifier identifier base-clause [opt]
12511      class-key nested-name-specifier [opt] template-id
12512        base-clause [opt]
12513
12514    GNU Extensions:
12515      class-key attributes identifier [opt] base-clause [opt]
12516      class-key attributes nested-name-specifier identifier base-clause [opt]
12517      class-key attributes nested-name-specifier [opt] template-id
12518        base-clause [opt]
12519
12520    Returns the TYPE of the indicated class.  Sets
12521    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12522    involving a nested-name-specifier was used, and FALSE otherwise.
12523
12524    Returns NULL_TREE if the class-head is syntactically valid, but
12525    semantically invalid in a way that means we should skip the entire
12526    body of the class.  */
12527
12528 static tree
12529 cp_parser_class_head (cp_parser* parser,
12530                       bool* nested_name_specifier_p,
12531                       tree *attributes_p)
12532 {
12533   tree nested_name_specifier;
12534   enum tag_types class_key;
12535   tree id = NULL_TREE;
12536   tree type = NULL_TREE;
12537   tree attributes;
12538   bool template_id_p = false;
12539   bool qualified_p = false;
12540   bool invalid_nested_name_p = false;
12541   bool invalid_explicit_specialization_p = false;
12542   bool pop_p = false;
12543   unsigned num_templates;
12544   tree bases;
12545
12546   /* Assume no nested-name-specifier will be present.  */
12547   *nested_name_specifier_p = false;
12548   /* Assume no template parameter lists will be used in defining the
12549      type.  */
12550   num_templates = 0;
12551
12552   /* Look for the class-key.  */
12553   class_key = cp_parser_class_key (parser);
12554   if (class_key == none_type)
12555     return error_mark_node;
12556
12557   /* Parse the attributes.  */
12558   attributes = cp_parser_attributes_opt (parser);
12559
12560   /* If the next token is `::', that is invalid -- but sometimes
12561      people do try to write:
12562
12563        struct ::S {};
12564
12565      Handle this gracefully by accepting the extra qualifier, and then
12566      issuing an error about it later if this really is a
12567      class-head.  If it turns out just to be an elaborated type
12568      specifier, remain silent.  */
12569   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12570     qualified_p = true;
12571
12572   push_deferring_access_checks (dk_no_check);
12573
12574   /* Determine the name of the class.  Begin by looking for an
12575      optional nested-name-specifier.  */
12576   nested_name_specifier
12577     = cp_parser_nested_name_specifier_opt (parser,
12578                                            /*typename_keyword_p=*/false,
12579                                            /*check_dependency_p=*/false,
12580                                            /*type_p=*/false,
12581                                            /*is_declaration=*/false);
12582   /* If there was a nested-name-specifier, then there *must* be an
12583      identifier.  */
12584   if (nested_name_specifier)
12585     {
12586       /* Although the grammar says `identifier', it really means
12587          `class-name' or `template-name'.  You are only allowed to
12588          define a class that has already been declared with this
12589          syntax.
12590
12591          The proposed resolution for Core Issue 180 says that whever
12592          you see `class T::X' you should treat `X' as a type-name.
12593
12594          It is OK to define an inaccessible class; for example:
12595
12596            class A { class B; };
12597            class A::B {};
12598
12599          We do not know if we will see a class-name, or a
12600          template-name.  We look for a class-name first, in case the
12601          class-name is a template-id; if we looked for the
12602          template-name first we would stop after the template-name.  */
12603       cp_parser_parse_tentatively (parser);
12604       type = cp_parser_class_name (parser,
12605                                    /*typename_keyword_p=*/false,
12606                                    /*template_keyword_p=*/false,
12607                                    /*type_p=*/true,
12608                                    /*check_dependency_p=*/false,
12609                                    /*class_head_p=*/true,
12610                                    /*is_declaration=*/false);
12611       /* If that didn't work, ignore the nested-name-specifier.  */
12612       if (!cp_parser_parse_definitely (parser))
12613         {
12614           invalid_nested_name_p = true;
12615           id = cp_parser_identifier (parser);
12616           if (id == error_mark_node)
12617             id = NULL_TREE;
12618         }
12619       /* If we could not find a corresponding TYPE, treat this
12620          declaration like an unqualified declaration.  */
12621       if (type == error_mark_node)
12622         nested_name_specifier = NULL_TREE;
12623       /* Otherwise, count the number of templates used in TYPE and its
12624          containing scopes.  */
12625       else
12626         {
12627           tree scope;
12628
12629           for (scope = TREE_TYPE (type);
12630                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12631                scope = (TYPE_P (scope)
12632                         ? TYPE_CONTEXT (scope)
12633                         : DECL_CONTEXT (scope)))
12634             if (TYPE_P (scope)
12635                 && CLASS_TYPE_P (scope)
12636                 && CLASSTYPE_TEMPLATE_INFO (scope)
12637                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12638                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12639               ++num_templates;
12640         }
12641     }
12642   /* Otherwise, the identifier is optional.  */
12643   else
12644     {
12645       /* We don't know whether what comes next is a template-id,
12646          an identifier, or nothing at all.  */
12647       cp_parser_parse_tentatively (parser);
12648       /* Check for a template-id.  */
12649       id = cp_parser_template_id (parser,
12650                                   /*template_keyword_p=*/false,
12651                                   /*check_dependency_p=*/true,
12652                                   /*is_declaration=*/true);
12653       /* If that didn't work, it could still be an identifier.  */
12654       if (!cp_parser_parse_definitely (parser))
12655         {
12656           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12657             id = cp_parser_identifier (parser);
12658           else
12659             id = NULL_TREE;
12660         }
12661       else
12662         {
12663           template_id_p = true;
12664           ++num_templates;
12665         }
12666     }
12667
12668   pop_deferring_access_checks ();
12669
12670   if (id)
12671     cp_parser_check_for_invalid_template_id (parser, id);
12672
12673   /* If it's not a `:' or a `{' then we can't really be looking at a
12674      class-head, since a class-head only appears as part of a
12675      class-specifier.  We have to detect this situation before calling
12676      xref_tag, since that has irreversible side-effects.  */
12677   if (!cp_parser_next_token_starts_class_definition_p (parser))
12678     {
12679       cp_parser_error (parser, "expected `{' or `:'");
12680       return error_mark_node;
12681     }
12682
12683   /* At this point, we're going ahead with the class-specifier, even
12684      if some other problem occurs.  */
12685   cp_parser_commit_to_tentative_parse (parser);
12686   /* Issue the error about the overly-qualified name now.  */
12687   if (qualified_p)
12688     cp_parser_error (parser,
12689                      "global qualification of class name is invalid");
12690   else if (invalid_nested_name_p)
12691     cp_parser_error (parser,
12692                      "qualified name does not name a class");
12693   else if (nested_name_specifier)
12694     {
12695       tree scope;
12696       /* Figure out in what scope the declaration is being placed.  */
12697       scope = current_scope ();
12698       if (!scope)
12699         scope = current_namespace;
12700       /* If that scope does not contain the scope in which the
12701          class was originally declared, the program is invalid.  */
12702       if (scope && !is_ancestor (scope, nested_name_specifier))
12703         {
12704           error ("declaration of `%D' in `%D' which does not "
12705                  "enclose `%D'", type, scope, nested_name_specifier);
12706           type = NULL_TREE;
12707           goto done;
12708         }
12709       /* [dcl.meaning]
12710
12711          A declarator-id shall not be qualified exception of the
12712          definition of a ... nested class outside of its class
12713          ... [or] a the definition or explicit instantiation of a
12714          class member of a namespace outside of its namespace.  */
12715       if (scope == nested_name_specifier)
12716         {
12717           pedwarn ("extra qualification ignored");
12718           nested_name_specifier = NULL_TREE;
12719           num_templates = 0;
12720         }
12721     }
12722   /* An explicit-specialization must be preceded by "template <>".  If
12723      it is not, try to recover gracefully.  */
12724   if (at_namespace_scope_p ()
12725       && parser->num_template_parameter_lists == 0
12726       && template_id_p)
12727     {
12728       error ("an explicit specialization must be preceded by 'template <>'");
12729       invalid_explicit_specialization_p = true;
12730       /* Take the same action that would have been taken by
12731          cp_parser_explicit_specialization.  */
12732       ++parser->num_template_parameter_lists;
12733       begin_specialization ();
12734     }
12735   /* There must be no "return" statements between this point and the
12736      end of this function; set "type "to the correct return value and
12737      use "goto done;" to return.  */
12738   /* Make sure that the right number of template parameters were
12739      present.  */
12740   if (!cp_parser_check_template_parameters (parser, num_templates))
12741     {
12742       /* If something went wrong, there is no point in even trying to
12743          process the class-definition.  */
12744       type = NULL_TREE;
12745       goto done;
12746     }
12747
12748   /* Look up the type.  */
12749   if (template_id_p)
12750     {
12751       type = TREE_TYPE (id);
12752       maybe_process_partial_specialization (type);
12753     }
12754   else if (!nested_name_specifier)
12755     {
12756       /* If the class was unnamed, create a dummy name.  */
12757       if (!id)
12758         id = make_anon_name ();
12759       type = xref_tag (class_key, id, /*globalize=*/false,
12760                        parser->num_template_parameter_lists);
12761     }
12762   else
12763     {
12764       tree class_type;
12765       bool pop_p = false;
12766
12767       /* Given:
12768
12769             template <typename T> struct S { struct T };
12770             template <typename T> struct S<T>::T { };
12771
12772          we will get a TYPENAME_TYPE when processing the definition of
12773          `S::T'.  We need to resolve it to the actual type before we
12774          try to define it.  */
12775       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12776         {
12777           class_type = resolve_typename_type (TREE_TYPE (type),
12778                                               /*only_current_p=*/false);
12779           if (class_type != error_mark_node)
12780             type = TYPE_NAME (class_type);
12781           else
12782             {
12783               cp_parser_error (parser, "could not resolve typename type");
12784               type = error_mark_node;
12785             }
12786         }
12787
12788       maybe_process_partial_specialization (TREE_TYPE (type));
12789       class_type = current_class_type;
12790       /* Enter the scope indicated by the nested-name-specifier.  */
12791       if (nested_name_specifier)
12792         pop_p = push_scope (nested_name_specifier);
12793       /* Get the canonical version of this type.  */
12794       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12795       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12796           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12797         type = push_template_decl (type);
12798       type = TREE_TYPE (type);
12799       if (nested_name_specifier)
12800         {
12801           *nested_name_specifier_p = true;
12802           if (pop_p)
12803             pop_scope (nested_name_specifier);
12804         }
12805     }
12806   /* Indicate whether this class was declared as a `class' or as a
12807      `struct'.  */
12808   if (TREE_CODE (type) == RECORD_TYPE)
12809     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12810   cp_parser_check_class_key (class_key, type);
12811
12812   /* Enter the scope containing the class; the names of base classes
12813      should be looked up in that context.  For example, given:
12814
12815        struct A { struct B {}; struct C; };
12816        struct A::C : B {};
12817
12818      is valid.  */
12819   if (nested_name_specifier)
12820     pop_p = push_scope (nested_name_specifier);
12821
12822   bases = NULL_TREE;
12823
12824   /* Get the list of base-classes, if there is one.  */
12825   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12826     bases = cp_parser_base_clause (parser);
12827
12828   /* Process the base classes.  */
12829   xref_basetypes (type, bases);
12830
12831   /* Leave the scope given by the nested-name-specifier.  We will
12832      enter the class scope itself while processing the members.  */
12833   if (pop_p)
12834     pop_scope (nested_name_specifier);
12835
12836  done:
12837   if (invalid_explicit_specialization_p)
12838     {
12839       end_specialization ();
12840       --parser->num_template_parameter_lists;
12841     }
12842   *attributes_p = attributes;
12843   return type;
12844 }
12845
12846 /* Parse a class-key.
12847
12848    class-key:
12849      class
12850      struct
12851      union
12852
12853    Returns the kind of class-key specified, or none_type to indicate
12854    error.  */
12855
12856 static enum tag_types
12857 cp_parser_class_key (cp_parser* parser)
12858 {
12859   cp_token *token;
12860   enum tag_types tag_type;
12861
12862   /* Look for the class-key.  */
12863   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12864   if (!token)
12865     return none_type;
12866
12867   /* Check to see if the TOKEN is a class-key.  */
12868   tag_type = cp_parser_token_is_class_key (token);
12869   if (!tag_type)
12870     cp_parser_error (parser, "expected class-key");
12871   return tag_type;
12872 }
12873
12874 /* Parse an (optional) member-specification.
12875
12876    member-specification:
12877      member-declaration member-specification [opt]
12878      access-specifier : member-specification [opt]  */
12879
12880 static void
12881 cp_parser_member_specification_opt (cp_parser* parser)
12882 {
12883   while (true)
12884     {
12885       cp_token *token;
12886       enum rid keyword;
12887
12888       /* Peek at the next token.  */
12889       token = cp_lexer_peek_token (parser->lexer);
12890       /* If it's a `}', or EOF then we've seen all the members.  */
12891       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12892         break;
12893
12894       /* See if this token is a keyword.  */
12895       keyword = token->keyword;
12896       switch (keyword)
12897         {
12898         case RID_PUBLIC:
12899         case RID_PROTECTED:
12900         case RID_PRIVATE:
12901           /* Consume the access-specifier.  */
12902           cp_lexer_consume_token (parser->lexer);
12903           /* Remember which access-specifier is active.  */
12904           current_access_specifier = token->value;
12905           /* Look for the `:'.  */
12906           cp_parser_require (parser, CPP_COLON, "`:'");
12907           break;
12908
12909         default:
12910           /* Otherwise, the next construction must be a
12911              member-declaration.  */
12912           cp_parser_member_declaration (parser);
12913         }
12914     }
12915 }
12916
12917 /* Parse a member-declaration.
12918
12919    member-declaration:
12920      decl-specifier-seq [opt] member-declarator-list [opt] ;
12921      function-definition ; [opt]
12922      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12923      using-declaration
12924      template-declaration
12925
12926    member-declarator-list:
12927      member-declarator
12928      member-declarator-list , member-declarator
12929
12930    member-declarator:
12931      declarator pure-specifier [opt]
12932      declarator constant-initializer [opt]
12933      identifier [opt] : constant-expression
12934
12935    GNU Extensions:
12936
12937    member-declaration:
12938      __extension__ member-declaration
12939
12940    member-declarator:
12941      declarator attributes [opt] pure-specifier [opt]
12942      declarator attributes [opt] constant-initializer [opt]
12943      identifier [opt] attributes [opt] : constant-expression  */
12944
12945 static void
12946 cp_parser_member_declaration (cp_parser* parser)
12947 {
12948   cp_decl_specifier_seq decl_specifiers;
12949   tree prefix_attributes;
12950   tree decl;
12951   int declares_class_or_enum;
12952   bool friend_p;
12953   cp_token *token;
12954   int saved_pedantic;
12955
12956   /* Check for the `__extension__' keyword.  */
12957   if (cp_parser_extension_opt (parser, &saved_pedantic))
12958     {
12959       /* Recurse.  */
12960       cp_parser_member_declaration (parser);
12961       /* Restore the old value of the PEDANTIC flag.  */
12962       pedantic = saved_pedantic;
12963
12964       return;
12965     }
12966
12967   /* Check for a template-declaration.  */
12968   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12969     {
12970       /* Parse the template-declaration.  */
12971       cp_parser_template_declaration (parser, /*member_p=*/true);
12972
12973       return;
12974     }
12975
12976   /* Check for a using-declaration.  */
12977   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12978     {
12979       /* Parse the using-declaration.  */
12980       cp_parser_using_declaration (parser);
12981
12982       return;
12983     }
12984
12985   /* Parse the decl-specifier-seq.  */
12986   cp_parser_decl_specifier_seq (parser,
12987                                 CP_PARSER_FLAGS_OPTIONAL,
12988                                 &decl_specifiers,
12989                                 &declares_class_or_enum);
12990   prefix_attributes = decl_specifiers.attributes;
12991   decl_specifiers.attributes = NULL_TREE;
12992   /* Check for an invalid type-name.  */
12993   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12994     return;
12995   /* If there is no declarator, then the decl-specifier-seq should
12996      specify a type.  */
12997   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12998     {
12999       /* If there was no decl-specifier-seq, and the next token is a
13000          `;', then we have something like:
13001
13002            struct S { ; };
13003
13004          [class.mem]
13005
13006          Each member-declaration shall declare at least one member
13007          name of the class.  */
13008       if (!decl_specifiers.any_specifiers_p)
13009         {
13010           if (pedantic)
13011             pedwarn ("extra semicolon");
13012         }
13013       else
13014         {
13015           tree type;
13016
13017           /* See if this declaration is a friend.  */
13018           friend_p = cp_parser_friend_p (&decl_specifiers);
13019           /* If there were decl-specifiers, check to see if there was
13020              a class-declaration.  */
13021           type = check_tag_decl (&decl_specifiers);
13022           /* Nested classes have already been added to the class, but
13023              a `friend' needs to be explicitly registered.  */
13024           if (friend_p)
13025             {
13026               /* If the `friend' keyword was present, the friend must
13027                  be introduced with a class-key.  */
13028                if (!declares_class_or_enum)
13029                  error ("a class-key must be used when declaring a friend");
13030                /* In this case:
13031
13032                     template <typename T> struct A {
13033                       friend struct A<T>::B;
13034                     };
13035
13036                   A<T>::B will be represented by a TYPENAME_TYPE, and
13037                   therefore not recognized by check_tag_decl.  */
13038                if (!type
13039                    && decl_specifiers.type
13040                    && TYPE_P (decl_specifiers.type))
13041                  type = decl_specifiers.type;
13042                if (!type || !TYPE_P (type))
13043                  error ("friend declaration does not name a class or "
13044                         "function");
13045                else
13046                  make_friend_class (current_class_type, type,
13047                                     /*complain=*/true);
13048             }
13049           /* If there is no TYPE, an error message will already have
13050              been issued.  */
13051           else if (!type || type == error_mark_node)
13052             ;
13053           /* An anonymous aggregate has to be handled specially; such
13054              a declaration really declares a data member (with a
13055              particular type), as opposed to a nested class.  */
13056           else if (ANON_AGGR_TYPE_P (type))
13057             {
13058               /* Remove constructors and such from TYPE, now that we
13059                  know it is an anonymous aggregate.  */
13060               fixup_anonymous_aggr (type);
13061               /* And make the corresponding data member.  */
13062               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13063               /* Add it to the class.  */
13064               finish_member_declaration (decl);
13065             }
13066           else
13067             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13068         }
13069     }
13070   else
13071     {
13072       /* See if these declarations will be friends.  */
13073       friend_p = cp_parser_friend_p (&decl_specifiers);
13074
13075       /* Keep going until we hit the `;' at the end of the
13076          declaration.  */
13077       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13078         {
13079           tree attributes = NULL_TREE;
13080           tree first_attribute;
13081
13082           /* Peek at the next token.  */
13083           token = cp_lexer_peek_token (parser->lexer);
13084
13085           /* Check for a bitfield declaration.  */
13086           if (token->type == CPP_COLON
13087               || (token->type == CPP_NAME
13088                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13089                   == CPP_COLON))
13090             {
13091               tree identifier;
13092               tree width;
13093
13094               /* Get the name of the bitfield.  Note that we cannot just
13095                  check TOKEN here because it may have been invalidated by
13096                  the call to cp_lexer_peek_nth_token above.  */
13097               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13098                 identifier = cp_parser_identifier (parser);
13099               else
13100                 identifier = NULL_TREE;
13101
13102               /* Consume the `:' token.  */
13103               cp_lexer_consume_token (parser->lexer);
13104               /* Get the width of the bitfield.  */
13105               width
13106                 = cp_parser_constant_expression (parser,
13107                                                  /*allow_non_constant=*/false,
13108                                                  NULL);
13109
13110               /* Look for attributes that apply to the bitfield.  */
13111               attributes = cp_parser_attributes_opt (parser);
13112               /* Remember which attributes are prefix attributes and
13113                  which are not.  */
13114               first_attribute = attributes;
13115               /* Combine the attributes.  */
13116               attributes = chainon (prefix_attributes, attributes);
13117
13118               /* Create the bitfield declaration.  */
13119               decl = grokbitfield (identifier
13120                                    ? make_id_declarator (identifier)
13121                                    : NULL,
13122                                    &decl_specifiers,
13123                                    width);
13124               /* Apply the attributes.  */
13125               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13126             }
13127           else
13128             {
13129               cp_declarator *declarator;
13130               tree initializer;
13131               tree asm_specification;
13132               int ctor_dtor_or_conv_p;
13133
13134               /* Parse the declarator.  */
13135               declarator
13136                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13137                                         &ctor_dtor_or_conv_p,
13138                                         /*parenthesized_p=*/NULL);
13139
13140               /* If something went wrong parsing the declarator, make sure
13141                  that we at least consume some tokens.  */
13142               if (declarator == cp_error_declarator)
13143                 {
13144                   /* Skip to the end of the statement.  */
13145                   cp_parser_skip_to_end_of_statement (parser);
13146                   /* If the next token is not a semicolon, that is
13147                      probably because we just skipped over the body of
13148                      a function.  So, we consume a semicolon if
13149                      present, but do not issue an error message if it
13150                      is not present.  */
13151                   if (cp_lexer_next_token_is (parser->lexer,
13152                                               CPP_SEMICOLON))
13153                     cp_lexer_consume_token (parser->lexer);
13154                   return;
13155                 }
13156
13157               cp_parser_check_for_definition_in_return_type
13158                 (declarator, declares_class_or_enum);
13159
13160               /* Look for an asm-specification.  */
13161               asm_specification = cp_parser_asm_specification_opt (parser);
13162               /* Look for attributes that apply to the declaration.  */
13163               attributes = cp_parser_attributes_opt (parser);
13164               /* Remember which attributes are prefix attributes and
13165                  which are not.  */
13166               first_attribute = attributes;
13167               /* Combine the attributes.  */
13168               attributes = chainon (prefix_attributes, attributes);
13169
13170               /* If it's an `=', then we have a constant-initializer or a
13171                  pure-specifier.  It is not correct to parse the
13172                  initializer before registering the member declaration
13173                  since the member declaration should be in scope while
13174                  its initializer is processed.  However, the rest of the
13175                  front end does not yet provide an interface that allows
13176                  us to handle this correctly.  */
13177               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13178                 {
13179                   /* In [class.mem]:
13180
13181                      A pure-specifier shall be used only in the declaration of
13182                      a virtual function.
13183
13184                      A member-declarator can contain a constant-initializer
13185                      only if it declares a static member of integral or
13186                      enumeration type.
13187
13188                      Therefore, if the DECLARATOR is for a function, we look
13189                      for a pure-specifier; otherwise, we look for a
13190                      constant-initializer.  When we call `grokfield', it will
13191                      perform more stringent semantics checks.  */
13192                   if (declarator->kind == cdk_function)
13193                     initializer = cp_parser_pure_specifier (parser);
13194                   else
13195                     /* Parse the initializer.  */
13196                     initializer = cp_parser_constant_initializer (parser);
13197                 }
13198               /* Otherwise, there is no initializer.  */
13199               else
13200                 initializer = NULL_TREE;
13201
13202               /* See if we are probably looking at a function
13203                  definition.  We are certainly not looking at at a
13204                  member-declarator.  Calling `grokfield' has
13205                  side-effects, so we must not do it unless we are sure
13206                  that we are looking at a member-declarator.  */
13207               if (cp_parser_token_starts_function_definition_p
13208                   (cp_lexer_peek_token (parser->lexer)))
13209                 {
13210                   /* The grammar does not allow a pure-specifier to be
13211                      used when a member function is defined.  (It is
13212                      possible that this fact is an oversight in the
13213                      standard, since a pure function may be defined
13214                      outside of the class-specifier.  */
13215                   if (initializer)
13216                     error ("pure-specifier on function-definition");
13217                   decl = cp_parser_save_member_function_body (parser,
13218                                                               &decl_specifiers,
13219                                                               declarator,
13220                                                               attributes);
13221                   /* If the member was not a friend, declare it here.  */
13222                   if (!friend_p)
13223                     finish_member_declaration (decl);
13224                   /* Peek at the next token.  */
13225                   token = cp_lexer_peek_token (parser->lexer);
13226                   /* If the next token is a semicolon, consume it.  */
13227                   if (token->type == CPP_SEMICOLON)
13228                     cp_lexer_consume_token (parser->lexer);
13229                   return;
13230                 }
13231               else
13232                 {
13233                   /* Create the declaration.  */
13234                   decl = grokfield (declarator, &decl_specifiers,
13235                                     initializer, asm_specification,
13236                                     attributes);
13237                   /* Any initialization must have been from a
13238                      constant-expression.  */
13239                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13240                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13241                 }
13242             }
13243
13244           /* Reset PREFIX_ATTRIBUTES.  */
13245           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13246             attributes = TREE_CHAIN (attributes);
13247           if (attributes)
13248             TREE_CHAIN (attributes) = NULL_TREE;
13249
13250           /* If there is any qualification still in effect, clear it
13251              now; we will be starting fresh with the next declarator.  */
13252           parser->scope = NULL_TREE;
13253           parser->qualifying_scope = NULL_TREE;
13254           parser->object_scope = NULL_TREE;
13255           /* If it's a `,', then there are more declarators.  */
13256           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13257             cp_lexer_consume_token (parser->lexer);
13258           /* If the next token isn't a `;', then we have a parse error.  */
13259           else if (cp_lexer_next_token_is_not (parser->lexer,
13260                                                CPP_SEMICOLON))
13261             {
13262               cp_parser_error (parser, "expected `;'");
13263               /* Skip tokens until we find a `;'.  */
13264               cp_parser_skip_to_end_of_statement (parser);
13265
13266               break;
13267             }
13268
13269           if (decl)
13270             {
13271               /* Add DECL to the list of members.  */
13272               if (!friend_p)
13273                 finish_member_declaration (decl);
13274
13275               if (TREE_CODE (decl) == FUNCTION_DECL)
13276                 cp_parser_save_default_args (parser, decl);
13277             }
13278         }
13279     }
13280
13281   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13282 }
13283
13284 /* Parse a pure-specifier.
13285
13286    pure-specifier:
13287      = 0
13288
13289    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13290    Otherwise, ERROR_MARK_NODE is returned.  */
13291
13292 static tree
13293 cp_parser_pure_specifier (cp_parser* parser)
13294 {
13295   cp_token *token;
13296
13297   /* Look for the `=' token.  */
13298   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13299     return error_mark_node;
13300   /* Look for the `0' token.  */
13301   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13302   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13303      to get information from the lexer about how the number was
13304      spelled in order to fix this problem.  */
13305   if (!token || !integer_zerop (token->value))
13306     return error_mark_node;
13307
13308   return integer_zero_node;
13309 }
13310
13311 /* Parse a constant-initializer.
13312
13313    constant-initializer:
13314      = constant-expression
13315
13316    Returns a representation of the constant-expression.  */
13317
13318 static tree
13319 cp_parser_constant_initializer (cp_parser* parser)
13320 {
13321   /* Look for the `=' token.  */
13322   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13323     return error_mark_node;
13324
13325   /* It is invalid to write:
13326
13327        struct S { static const int i = { 7 }; };
13328
13329      */
13330   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13331     {
13332       cp_parser_error (parser,
13333                        "a brace-enclosed initializer is not allowed here");
13334       /* Consume the opening brace.  */
13335       cp_lexer_consume_token (parser->lexer);
13336       /* Skip the initializer.  */
13337       cp_parser_skip_to_closing_brace (parser);
13338       /* Look for the trailing `}'.  */
13339       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13340
13341       return error_mark_node;
13342     }
13343
13344   return cp_parser_constant_expression (parser,
13345                                         /*allow_non_constant=*/false,
13346                                         NULL);
13347 }
13348
13349 /* Derived classes [gram.class.derived] */
13350
13351 /* Parse a base-clause.
13352
13353    base-clause:
13354      : base-specifier-list
13355
13356    base-specifier-list:
13357      base-specifier
13358      base-specifier-list , base-specifier
13359
13360    Returns a TREE_LIST representing the base-classes, in the order in
13361    which they were declared.  The representation of each node is as
13362    described by cp_parser_base_specifier.
13363
13364    In the case that no bases are specified, this function will return
13365    NULL_TREE, not ERROR_MARK_NODE.  */
13366
13367 static tree
13368 cp_parser_base_clause (cp_parser* parser)
13369 {
13370   tree bases = NULL_TREE;
13371
13372   /* Look for the `:' that begins the list.  */
13373   cp_parser_require (parser, CPP_COLON, "`:'");
13374
13375   /* Scan the base-specifier-list.  */
13376   while (true)
13377     {
13378       cp_token *token;
13379       tree base;
13380
13381       /* Look for the base-specifier.  */
13382       base = cp_parser_base_specifier (parser);
13383       /* Add BASE to the front of the list.  */
13384       if (base != error_mark_node)
13385         {
13386           TREE_CHAIN (base) = bases;
13387           bases = base;
13388         }
13389       /* Peek at the next token.  */
13390       token = cp_lexer_peek_token (parser->lexer);
13391       /* If it's not a comma, then the list is complete.  */
13392       if (token->type != CPP_COMMA)
13393         break;
13394       /* Consume the `,'.  */
13395       cp_lexer_consume_token (parser->lexer);
13396     }
13397
13398   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13399      base class had a qualified name.  However, the next name that
13400      appears is certainly not qualified.  */
13401   parser->scope = NULL_TREE;
13402   parser->qualifying_scope = NULL_TREE;
13403   parser->object_scope = NULL_TREE;
13404
13405   return nreverse (bases);
13406 }
13407
13408 /* Parse a base-specifier.
13409
13410    base-specifier:
13411      :: [opt] nested-name-specifier [opt] class-name
13412      virtual access-specifier [opt] :: [opt] nested-name-specifier
13413        [opt] class-name
13414      access-specifier virtual [opt] :: [opt] nested-name-specifier
13415        [opt] class-name
13416
13417    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13418    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13419    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13420    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13421
13422 static tree
13423 cp_parser_base_specifier (cp_parser* parser)
13424 {
13425   cp_token *token;
13426   bool done = false;
13427   bool virtual_p = false;
13428   bool duplicate_virtual_error_issued_p = false;
13429   bool duplicate_access_error_issued_p = false;
13430   bool class_scope_p, template_p;
13431   tree access = access_default_node;
13432   tree type;
13433
13434   /* Process the optional `virtual' and `access-specifier'.  */
13435   while (!done)
13436     {
13437       /* Peek at the next token.  */
13438       token = cp_lexer_peek_token (parser->lexer);
13439       /* Process `virtual'.  */
13440       switch (token->keyword)
13441         {
13442         case RID_VIRTUAL:
13443           /* If `virtual' appears more than once, issue an error.  */
13444           if (virtual_p && !duplicate_virtual_error_issued_p)
13445             {
13446               cp_parser_error (parser,
13447                                "`virtual' specified more than once in base-specified");
13448               duplicate_virtual_error_issued_p = true;
13449             }
13450
13451           virtual_p = true;
13452
13453           /* Consume the `virtual' token.  */
13454           cp_lexer_consume_token (parser->lexer);
13455
13456           break;
13457
13458         case RID_PUBLIC:
13459         case RID_PROTECTED:
13460         case RID_PRIVATE:
13461           /* If more than one access specifier appears, issue an
13462              error.  */
13463           if (access != access_default_node
13464               && !duplicate_access_error_issued_p)
13465             {
13466               cp_parser_error (parser,
13467                                "more than one access specifier in base-specified");
13468               duplicate_access_error_issued_p = true;
13469             }
13470
13471           access = ridpointers[(int) token->keyword];
13472
13473           /* Consume the access-specifier.  */
13474           cp_lexer_consume_token (parser->lexer);
13475
13476           break;
13477
13478         default:
13479           done = true;
13480           break;
13481         }
13482     }
13483   /* It is not uncommon to see programs mechanically, erroneously, use
13484      the 'typename' keyword to denote (dependent) qualified types
13485      as base classes.  */
13486   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13487     {
13488       if (!processing_template_decl)
13489         error ("keyword `typename' not allowed outside of templates");
13490       else
13491         error ("keyword `typename' not allowed in this context "
13492                "(the base class is implicitly a type)");
13493       cp_lexer_consume_token (parser->lexer);
13494     }
13495
13496   /* Look for the optional `::' operator.  */
13497   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13498   /* Look for the nested-name-specifier.  The simplest way to
13499      implement:
13500
13501        [temp.res]
13502
13503        The keyword `typename' is not permitted in a base-specifier or
13504        mem-initializer; in these contexts a qualified name that
13505        depends on a template-parameter is implicitly assumed to be a
13506        type name.
13507
13508      is to pretend that we have seen the `typename' keyword at this
13509      point.  */
13510   cp_parser_nested_name_specifier_opt (parser,
13511                                        /*typename_keyword_p=*/true,
13512                                        /*check_dependency_p=*/true,
13513                                        /*type_p=*/true,
13514                                        /*is_declaration=*/true);
13515   /* If the base class is given by a qualified name, assume that names
13516      we see are type names or templates, as appropriate.  */
13517   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13518   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13519
13520   /* Finally, look for the class-name.  */
13521   type = cp_parser_class_name (parser,
13522                                class_scope_p,
13523                                template_p,
13524                                /*type_p=*/true,
13525                                /*check_dependency_p=*/true,
13526                                /*class_head_p=*/false,
13527                                /*is_declaration=*/true);
13528
13529   if (type == error_mark_node)
13530     return error_mark_node;
13531
13532   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13533 }
13534
13535 /* Exception handling [gram.exception] */
13536
13537 /* Parse an (optional) exception-specification.
13538
13539    exception-specification:
13540      throw ( type-id-list [opt] )
13541
13542    Returns a TREE_LIST representing the exception-specification.  The
13543    TREE_VALUE of each node is a type.  */
13544
13545 static tree
13546 cp_parser_exception_specification_opt (cp_parser* parser)
13547 {
13548   cp_token *token;
13549   tree type_id_list;
13550
13551   /* Peek at the next token.  */
13552   token = cp_lexer_peek_token (parser->lexer);
13553   /* If it's not `throw', then there's no exception-specification.  */
13554   if (!cp_parser_is_keyword (token, RID_THROW))
13555     return NULL_TREE;
13556
13557   /* Consume the `throw'.  */
13558   cp_lexer_consume_token (parser->lexer);
13559
13560   /* Look for the `('.  */
13561   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13562
13563   /* Peek at the next token.  */
13564   token = cp_lexer_peek_token (parser->lexer);
13565   /* If it's not a `)', then there is a type-id-list.  */
13566   if (token->type != CPP_CLOSE_PAREN)
13567     {
13568       const char *saved_message;
13569
13570       /* Types may not be defined in an exception-specification.  */
13571       saved_message = parser->type_definition_forbidden_message;
13572       parser->type_definition_forbidden_message
13573         = "types may not be defined in an exception-specification";
13574       /* Parse the type-id-list.  */
13575       type_id_list = cp_parser_type_id_list (parser);
13576       /* Restore the saved message.  */
13577       parser->type_definition_forbidden_message = saved_message;
13578     }
13579   else
13580     type_id_list = empty_except_spec;
13581
13582   /* Look for the `)'.  */
13583   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13584
13585   return type_id_list;
13586 }
13587
13588 /* Parse an (optional) type-id-list.
13589
13590    type-id-list:
13591      type-id
13592      type-id-list , type-id
13593
13594    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13595    in the order that the types were presented.  */
13596
13597 static tree
13598 cp_parser_type_id_list (cp_parser* parser)
13599 {
13600   tree types = NULL_TREE;
13601
13602   while (true)
13603     {
13604       cp_token *token;
13605       tree type;
13606
13607       /* Get the next type-id.  */
13608       type = cp_parser_type_id (parser);
13609       /* Add it to the list.  */
13610       types = add_exception_specifier (types, type, /*complain=*/1);
13611       /* Peek at the next token.  */
13612       token = cp_lexer_peek_token (parser->lexer);
13613       /* If it is not a `,', we are done.  */
13614       if (token->type != CPP_COMMA)
13615         break;
13616       /* Consume the `,'.  */
13617       cp_lexer_consume_token (parser->lexer);
13618     }
13619
13620   return nreverse (types);
13621 }
13622
13623 /* Parse a try-block.
13624
13625    try-block:
13626      try compound-statement handler-seq  */
13627
13628 static tree
13629 cp_parser_try_block (cp_parser* parser)
13630 {
13631   tree try_block;
13632
13633   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13634   try_block = begin_try_block ();
13635   cp_parser_compound_statement (parser, NULL, true);
13636   finish_try_block (try_block);
13637   cp_parser_handler_seq (parser);
13638   finish_handler_sequence (try_block);
13639
13640   return try_block;
13641 }
13642
13643 /* Parse a function-try-block.
13644
13645    function-try-block:
13646      try ctor-initializer [opt] function-body handler-seq  */
13647
13648 static bool
13649 cp_parser_function_try_block (cp_parser* parser)
13650 {
13651   tree try_block;
13652   bool ctor_initializer_p;
13653
13654   /* Look for the `try' keyword.  */
13655   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13656     return false;
13657   /* Let the rest of the front-end know where we are.  */
13658   try_block = begin_function_try_block ();
13659   /* Parse the function-body.  */
13660   ctor_initializer_p
13661     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13662   /* We're done with the `try' part.  */
13663   finish_function_try_block (try_block);
13664   /* Parse the handlers.  */
13665   cp_parser_handler_seq (parser);
13666   /* We're done with the handlers.  */
13667   finish_function_handler_sequence (try_block);
13668
13669   return ctor_initializer_p;
13670 }
13671
13672 /* Parse a handler-seq.
13673
13674    handler-seq:
13675      handler handler-seq [opt]  */
13676
13677 static void
13678 cp_parser_handler_seq (cp_parser* parser)
13679 {
13680   while (true)
13681     {
13682       cp_token *token;
13683
13684       /* Parse the handler.  */
13685       cp_parser_handler (parser);
13686       /* Peek at the next token.  */
13687       token = cp_lexer_peek_token (parser->lexer);
13688       /* If it's not `catch' then there are no more handlers.  */
13689       if (!cp_parser_is_keyword (token, RID_CATCH))
13690         break;
13691     }
13692 }
13693
13694 /* Parse a handler.
13695
13696    handler:
13697      catch ( exception-declaration ) compound-statement  */
13698
13699 static void
13700 cp_parser_handler (cp_parser* parser)
13701 {
13702   tree handler;
13703   tree declaration;
13704
13705   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13706   handler = begin_handler ();
13707   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13708   declaration = cp_parser_exception_declaration (parser);
13709   finish_handler_parms (declaration, handler);
13710   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13711   cp_parser_compound_statement (parser, NULL, false);
13712   finish_handler (handler);
13713 }
13714
13715 /* Parse an exception-declaration.
13716
13717    exception-declaration:
13718      type-specifier-seq declarator
13719      type-specifier-seq abstract-declarator
13720      type-specifier-seq
13721      ...
13722
13723    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13724    ellipsis variant is used.  */
13725
13726 static tree
13727 cp_parser_exception_declaration (cp_parser* parser)
13728 {
13729   tree decl;
13730   cp_decl_specifier_seq type_specifiers;
13731   cp_declarator *declarator;
13732   const char *saved_message;
13733
13734   /* If it's an ellipsis, it's easy to handle.  */
13735   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13736     {
13737       /* Consume the `...' token.  */
13738       cp_lexer_consume_token (parser->lexer);
13739       return NULL_TREE;
13740     }
13741
13742   /* Types may not be defined in exception-declarations.  */
13743   saved_message = parser->type_definition_forbidden_message;
13744   parser->type_definition_forbidden_message
13745     = "types may not be defined in exception-declarations";
13746
13747   /* Parse the type-specifier-seq.  */
13748   cp_parser_type_specifier_seq (parser, &type_specifiers);
13749   /* If it's a `)', then there is no declarator.  */
13750   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13751     declarator = NULL;
13752   else
13753     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13754                                        /*ctor_dtor_or_conv_p=*/NULL,
13755                                        /*parenthesized_p=*/NULL);
13756
13757   /* Restore the saved message.  */
13758   parser->type_definition_forbidden_message = saved_message;
13759
13760   if (type_specifiers.any_specifiers_p)
13761     {
13762       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13763       if (decl == NULL_TREE)
13764         error ("invalid catch parameter");
13765     }
13766   else
13767     decl = NULL_TREE;
13768
13769   return decl;
13770 }
13771
13772 /* Parse a throw-expression.
13773
13774    throw-expression:
13775      throw assignment-expression [opt]
13776
13777    Returns a THROW_EXPR representing the throw-expression.  */
13778
13779 static tree
13780 cp_parser_throw_expression (cp_parser* parser)
13781 {
13782   tree expression;
13783   cp_token* token;
13784
13785   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13786   token = cp_lexer_peek_token (parser->lexer);
13787   /* Figure out whether or not there is an assignment-expression
13788      following the "throw" keyword.  */
13789   if (token->type == CPP_COMMA
13790       || token->type == CPP_SEMICOLON
13791       || token->type == CPP_CLOSE_PAREN
13792       || token->type == CPP_CLOSE_SQUARE
13793       || token->type == CPP_CLOSE_BRACE
13794       || token->type == CPP_COLON)
13795     expression = NULL_TREE;
13796   else
13797     expression = cp_parser_assignment_expression (parser);
13798
13799   return build_throw (expression);
13800 }
13801
13802 /* GNU Extensions */
13803
13804 /* Parse an (optional) asm-specification.
13805
13806    asm-specification:
13807      asm ( string-literal )
13808
13809    If the asm-specification is present, returns a STRING_CST
13810    corresponding to the string-literal.  Otherwise, returns
13811    NULL_TREE.  */
13812
13813 static tree
13814 cp_parser_asm_specification_opt (cp_parser* parser)
13815 {
13816   cp_token *token;
13817   tree asm_specification;
13818
13819   /* Peek at the next token.  */
13820   token = cp_lexer_peek_token (parser->lexer);
13821   /* If the next token isn't the `asm' keyword, then there's no
13822      asm-specification.  */
13823   if (!cp_parser_is_keyword (token, RID_ASM))
13824     return NULL_TREE;
13825
13826   /* Consume the `asm' token.  */
13827   cp_lexer_consume_token (parser->lexer);
13828   /* Look for the `('.  */
13829   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13830
13831   /* Look for the string-literal.  */
13832   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13833   if (token)
13834     asm_specification = token->value;
13835   else
13836     asm_specification = NULL_TREE;
13837
13838   /* Look for the `)'.  */
13839   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13840
13841   return asm_specification;
13842 }
13843
13844 /* Parse an asm-operand-list.
13845
13846    asm-operand-list:
13847      asm-operand
13848      asm-operand-list , asm-operand
13849
13850    asm-operand:
13851      string-literal ( expression )
13852      [ string-literal ] string-literal ( expression )
13853
13854    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13855    each node is the expression.  The TREE_PURPOSE is itself a
13856    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13857    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13858    is a STRING_CST for the string literal before the parenthesis.  */
13859
13860 static tree
13861 cp_parser_asm_operand_list (cp_parser* parser)
13862 {
13863   tree asm_operands = NULL_TREE;
13864
13865   while (true)
13866     {
13867       tree string_literal;
13868       tree expression;
13869       tree name;
13870       cp_token *token;
13871
13872       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13873         {
13874           /* Consume the `[' token.  */
13875           cp_lexer_consume_token (parser->lexer);
13876           /* Read the operand name.  */
13877           name = cp_parser_identifier (parser);
13878           if (name != error_mark_node)
13879             name = build_string (IDENTIFIER_LENGTH (name),
13880                                  IDENTIFIER_POINTER (name));
13881           /* Look for the closing `]'.  */
13882           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13883         }
13884       else
13885         name = NULL_TREE;
13886       /* Look for the string-literal.  */
13887       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13888       string_literal = token ? token->value : error_mark_node;
13889       c_lex_string_translate = 1;
13890       /* Look for the `('.  */
13891       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13892       /* Parse the expression.  */
13893       expression = cp_parser_expression (parser);
13894       /* Look for the `)'.  */
13895       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13896       c_lex_string_translate = 0;
13897       /* Add this operand to the list.  */
13898       asm_operands = tree_cons (build_tree_list (name, string_literal),
13899                                 expression,
13900                                 asm_operands);
13901       /* If the next token is not a `,', there are no more
13902          operands.  */
13903       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13904         break;
13905       /* Consume the `,'.  */
13906       cp_lexer_consume_token (parser->lexer);
13907     }
13908
13909   return nreverse (asm_operands);
13910 }
13911
13912 /* Parse an asm-clobber-list.
13913
13914    asm-clobber-list:
13915      string-literal
13916      asm-clobber-list , string-literal
13917
13918    Returns a TREE_LIST, indicating the clobbers in the order that they
13919    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13920
13921 static tree
13922 cp_parser_asm_clobber_list (cp_parser* parser)
13923 {
13924   tree clobbers = NULL_TREE;
13925
13926   while (true)
13927     {
13928       cp_token *token;
13929       tree string_literal;
13930
13931       /* Look for the string literal.  */
13932       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13933       string_literal = token ? token->value : error_mark_node;
13934       /* Add it to the list.  */
13935       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13936       /* If the next token is not a `,', then the list is
13937          complete.  */
13938       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13939         break;
13940       /* Consume the `,' token.  */
13941       cp_lexer_consume_token (parser->lexer);
13942     }
13943
13944   return clobbers;
13945 }
13946
13947 /* Parse an (optional) series of attributes.
13948
13949    attributes:
13950      attributes attribute
13951
13952    attribute:
13953      __attribute__ (( attribute-list [opt] ))
13954
13955    The return value is as for cp_parser_attribute_list.  */
13956
13957 static tree
13958 cp_parser_attributes_opt (cp_parser* parser)
13959 {
13960   tree attributes = NULL_TREE;
13961
13962   while (true)
13963     {
13964       cp_token *token;
13965       tree attribute_list;
13966
13967       /* Peek at the next token.  */
13968       token = cp_lexer_peek_token (parser->lexer);
13969       /* If it's not `__attribute__', then we're done.  */
13970       if (token->keyword != RID_ATTRIBUTE)
13971         break;
13972
13973       /* Consume the `__attribute__' keyword.  */
13974       cp_lexer_consume_token (parser->lexer);
13975       /* Look for the two `(' tokens.  */
13976       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13977       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13978
13979       /* Peek at the next token.  */
13980       token = cp_lexer_peek_token (parser->lexer);
13981       if (token->type != CPP_CLOSE_PAREN)
13982         /* Parse the attribute-list.  */
13983         attribute_list = cp_parser_attribute_list (parser);
13984       else
13985         /* If the next token is a `)', then there is no attribute
13986            list.  */
13987         attribute_list = NULL;
13988
13989       /* Look for the two `)' tokens.  */
13990       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13991       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13992
13993       /* Add these new attributes to the list.  */
13994       attributes = chainon (attributes, attribute_list);
13995     }
13996
13997   return attributes;
13998 }
13999
14000 /* Parse an attribute-list.
14001
14002    attribute-list:
14003      attribute
14004      attribute-list , attribute
14005
14006    attribute:
14007      identifier
14008      identifier ( identifier )
14009      identifier ( identifier , expression-list )
14010      identifier ( expression-list )
14011
14012    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
14013    TREE_PURPOSE of each node is the identifier indicating which
14014    attribute is in use.  The TREE_VALUE represents the arguments, if
14015    any.  */
14016
14017 static tree
14018 cp_parser_attribute_list (cp_parser* parser)
14019 {
14020   tree attribute_list = NULL_TREE;
14021
14022   c_lex_string_translate = 0;
14023   while (true)
14024     {
14025       cp_token *token;
14026       tree identifier;
14027       tree attribute;
14028
14029       /* Look for the identifier.  We also allow keywords here; for
14030          example `__attribute__ ((const))' is legal.  */
14031       token = cp_lexer_peek_token (parser->lexer);
14032       if (token->type != CPP_NAME
14033           && token->type != CPP_KEYWORD)
14034         return error_mark_node;
14035       /* Consume the token.  */
14036       token = cp_lexer_consume_token (parser->lexer);
14037
14038       /* Save away the identifier that indicates which attribute this is.  */
14039       identifier = token->value;
14040       attribute = build_tree_list (identifier, NULL_TREE);
14041
14042       /* Peek at the next token.  */
14043       token = cp_lexer_peek_token (parser->lexer);
14044       /* If it's an `(', then parse the attribute arguments.  */
14045       if (token->type == CPP_OPEN_PAREN)
14046         {
14047           tree arguments;
14048
14049           arguments = (cp_parser_parenthesized_expression_list
14050                        (parser, true, /*non_constant_p=*/NULL));
14051           /* Save the identifier and arguments away.  */
14052           TREE_VALUE (attribute) = arguments;
14053         }
14054
14055       /* Add this attribute to the list.  */
14056       TREE_CHAIN (attribute) = attribute_list;
14057       attribute_list = attribute;
14058
14059       /* Now, look for more attributes.  */
14060       token = cp_lexer_peek_token (parser->lexer);
14061       /* If the next token isn't a `,', we're done.  */
14062       if (token->type != CPP_COMMA)
14063         break;
14064
14065       /* Consume the comma and keep going.  */
14066       cp_lexer_consume_token (parser->lexer);
14067     }
14068   c_lex_string_translate = 1;
14069
14070   /* We built up the list in reverse order.  */
14071   return nreverse (attribute_list);
14072 }
14073
14074 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14075    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14076    current value of the PEDANTIC flag, regardless of whether or not
14077    the `__extension__' keyword is present.  The caller is responsible
14078    for restoring the value of the PEDANTIC flag.  */
14079
14080 static bool
14081 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14082 {
14083   /* Save the old value of the PEDANTIC flag.  */
14084   *saved_pedantic = pedantic;
14085
14086   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14087     {
14088       /* Consume the `__extension__' token.  */
14089       cp_lexer_consume_token (parser->lexer);
14090       /* We're not being pedantic while the `__extension__' keyword is
14091          in effect.  */
14092       pedantic = 0;
14093
14094       return true;
14095     }
14096
14097   return false;
14098 }
14099
14100 /* Parse a label declaration.
14101
14102    label-declaration:
14103      __label__ label-declarator-seq ;
14104
14105    label-declarator-seq:
14106      identifier , label-declarator-seq
14107      identifier  */
14108
14109 static void
14110 cp_parser_label_declaration (cp_parser* parser)
14111 {
14112   /* Look for the `__label__' keyword.  */
14113   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14114
14115   while (true)
14116     {
14117       tree identifier;
14118
14119       /* Look for an identifier.  */
14120       identifier = cp_parser_identifier (parser);
14121       /* Declare it as a lobel.  */
14122       finish_label_decl (identifier);
14123       /* If the next token is a `;', stop.  */
14124       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14125         break;
14126       /* Look for the `,' separating the label declarations.  */
14127       cp_parser_require (parser, CPP_COMMA, "`,'");
14128     }
14129
14130   /* Look for the final `;'.  */
14131   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14132 }
14133
14134 /* Support Functions */
14135
14136 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14137    NAME should have one of the representations used for an
14138    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14139    is returned.  If PARSER->SCOPE is a dependent type, then a
14140    SCOPE_REF is returned.
14141
14142    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14143    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14144    was formed.  Abstractly, such entities should not be passed to this
14145    function, because they do not need to be looked up, but it is
14146    simpler to check for this special case here, rather than at the
14147    call-sites.
14148
14149    In cases not explicitly covered above, this function returns a
14150    DECL, OVERLOAD, or baselink representing the result of the lookup.
14151    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14152    is returned.
14153
14154    If IS_TYPE is TRUE, bindings that do not refer to types are
14155    ignored.
14156
14157    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14158    ignored.
14159
14160    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14161    are ignored.
14162
14163    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14164    types.  
14165
14166    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14167    results in an ambiguity, and false otherwise.  */
14168
14169 static tree
14170 cp_parser_lookup_name (cp_parser *parser, tree name,
14171                        bool is_type, bool is_template, bool is_namespace,
14172                        bool check_dependency,
14173                        bool *ambiguous_p)
14174 {
14175   tree decl;
14176   tree object_type = parser->context->object_type;
14177
14178   /* Assume that the lookup will be unambiguous.  */
14179   if (ambiguous_p)
14180     *ambiguous_p = false;
14181
14182   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14183      no longer valid.  Note that if we are parsing tentatively, and
14184      the parse fails, OBJECT_TYPE will be automatically restored.  */
14185   parser->context->object_type = NULL_TREE;
14186
14187   if (name == error_mark_node)
14188     return error_mark_node;
14189
14190   /* A template-id has already been resolved; there is no lookup to
14191      do.  */
14192   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14193     return name;
14194   if (BASELINK_P (name))
14195     {
14196       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14197                   == TEMPLATE_ID_EXPR);
14198       return name;
14199     }
14200
14201   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14202      it should already have been checked to make sure that the name
14203      used matches the type being destroyed.  */
14204   if (TREE_CODE (name) == BIT_NOT_EXPR)
14205     {
14206       tree type;
14207
14208       /* Figure out to which type this destructor applies.  */
14209       if (parser->scope)
14210         type = parser->scope;
14211       else if (object_type)
14212         type = object_type;
14213       else
14214         type = current_class_type;
14215       /* If that's not a class type, there is no destructor.  */
14216       if (!type || !CLASS_TYPE_P (type))
14217         return error_mark_node;
14218       if (!CLASSTYPE_DESTRUCTORS (type))
14219           return error_mark_node;
14220       /* If it was a class type, return the destructor.  */
14221       return CLASSTYPE_DESTRUCTORS (type);
14222     }
14223
14224   /* By this point, the NAME should be an ordinary identifier.  If
14225      the id-expression was a qualified name, the qualifying scope is
14226      stored in PARSER->SCOPE at this point.  */
14227   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14228
14229   /* Perform the lookup.  */
14230   if (parser->scope)
14231     {
14232       bool dependent_p;
14233
14234       if (parser->scope == error_mark_node)
14235         return error_mark_node;
14236
14237       /* If the SCOPE is dependent, the lookup must be deferred until
14238          the template is instantiated -- unless we are explicitly
14239          looking up names in uninstantiated templates.  Even then, we
14240          cannot look up the name if the scope is not a class type; it
14241          might, for example, be a template type parameter.  */
14242       dependent_p = (TYPE_P (parser->scope)
14243                      && !(parser->in_declarator_p
14244                           && currently_open_class (parser->scope))
14245                      && dependent_type_p (parser->scope));
14246       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14247            && dependent_p)
14248         {
14249           if (is_type)
14250             /* The resolution to Core Issue 180 says that `struct A::B'
14251                should be considered a type-name, even if `A' is
14252                dependent.  */
14253             decl = TYPE_NAME (make_typename_type (parser->scope,
14254                                                   name,
14255                                                   /*complain=*/1));
14256           else if (is_template)
14257             decl = make_unbound_class_template (parser->scope,
14258                                                 name,
14259                                                 /*complain=*/1);
14260           else
14261             decl = build_nt (SCOPE_REF, parser->scope, name);
14262         }
14263       else
14264         {
14265           bool pop_p = false;
14266
14267           /* If PARSER->SCOPE is a dependent type, then it must be a
14268              class type, and we must not be checking dependencies;
14269              otherwise, we would have processed this lookup above.  So
14270              that PARSER->SCOPE is not considered a dependent base by
14271              lookup_member, we must enter the scope here.  */
14272           if (dependent_p)
14273             pop_p = push_scope (parser->scope);
14274           /* If the PARSER->SCOPE is a a template specialization, it
14275              may be instantiated during name lookup.  In that case,
14276              errors may be issued.  Even if we rollback the current
14277              tentative parse, those errors are valid.  */
14278           decl = lookup_qualified_name (parser->scope, name, is_type,
14279                                         /*complain=*/true);
14280           if (pop_p)
14281             pop_scope (parser->scope);
14282         }
14283       parser->qualifying_scope = parser->scope;
14284       parser->object_scope = NULL_TREE;
14285     }
14286   else if (object_type)
14287     {
14288       tree object_decl = NULL_TREE;
14289       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14290          OBJECT_TYPE is not a class.  */
14291       if (CLASS_TYPE_P (object_type))
14292         /* If the OBJECT_TYPE is a template specialization, it may
14293            be instantiated during name lookup.  In that case, errors
14294            may be issued.  Even if we rollback the current tentative
14295            parse, those errors are valid.  */
14296         object_decl = lookup_member (object_type,
14297                                      name,
14298                                      /*protect=*/0, is_type);
14299       /* Look it up in the enclosing context, too.  */
14300       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14301                                /*block_p=*/true, is_namespace,
14302                                /*flags=*/0);
14303       parser->object_scope = object_type;
14304       parser->qualifying_scope = NULL_TREE;
14305       if (object_decl)
14306         decl = object_decl;
14307     }
14308   else
14309     {
14310       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14311                                /*block_p=*/true, is_namespace,
14312                                /*flags=*/0);
14313       parser->qualifying_scope = NULL_TREE;
14314       parser->object_scope = NULL_TREE;
14315     }
14316
14317   /* If the lookup failed, let our caller know.  */
14318   if (!decl
14319       || decl == error_mark_node
14320       || (TREE_CODE (decl) == FUNCTION_DECL
14321           && DECL_ANTICIPATED (decl)))
14322     return error_mark_node;
14323
14324   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14325   if (TREE_CODE (decl) == TREE_LIST)
14326     {
14327       if (ambiguous_p)
14328         *ambiguous_p = true;
14329       /* The error message we have to print is too complicated for
14330          cp_parser_error, so we incorporate its actions directly.  */
14331       if (!cp_parser_simulate_error (parser))
14332         {
14333           error ("reference to `%D' is ambiguous", name);
14334           print_candidates (decl);
14335         }
14336       return error_mark_node;
14337     }
14338
14339   gcc_assert (DECL_P (decl)
14340               || TREE_CODE (decl) == OVERLOAD
14341               || TREE_CODE (decl) == SCOPE_REF
14342               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14343               || BASELINK_P (decl));
14344
14345   /* If we have resolved the name of a member declaration, check to
14346      see if the declaration is accessible.  When the name resolves to
14347      set of overloaded functions, accessibility is checked when
14348      overload resolution is done.
14349
14350      During an explicit instantiation, access is not checked at all,
14351      as per [temp.explicit].  */
14352   if (DECL_P (decl))
14353     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14354
14355   return decl;
14356 }
14357
14358 /* Like cp_parser_lookup_name, but for use in the typical case where
14359    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14360    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14361
14362 static tree
14363 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14364 {
14365   return cp_parser_lookup_name (parser, name,
14366                                 /*is_type=*/false,
14367                                 /*is_template=*/false,
14368                                 /*is_namespace=*/false,
14369                                 /*check_dependency=*/true,
14370                                 /*ambiguous_p=*/NULL);
14371 }
14372
14373 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14374    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14375    true, the DECL indicates the class being defined in a class-head,
14376    or declared in an elaborated-type-specifier.
14377
14378    Otherwise, return DECL.  */
14379
14380 static tree
14381 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14382 {
14383   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14384      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14385
14386        struct A {
14387          template <typename T> struct B;
14388        };
14389
14390        template <typename T> struct A::B {};
14391
14392      Similarly, in a elaborated-type-specifier:
14393
14394        namespace N { struct X{}; }
14395
14396        struct A {
14397          template <typename T> friend struct N::X;
14398        };
14399
14400      However, if the DECL refers to a class type, and we are in
14401      the scope of the class, then the name lookup automatically
14402      finds the TYPE_DECL created by build_self_reference rather
14403      than a TEMPLATE_DECL.  For example, in:
14404
14405        template <class T> struct S {
14406          S s;
14407        };
14408
14409      there is no need to handle such case.  */
14410
14411   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14412     return DECL_TEMPLATE_RESULT (decl);
14413
14414   return decl;
14415 }
14416
14417 /* If too many, or too few, template-parameter lists apply to the
14418    declarator, issue an error message.  Returns TRUE if all went well,
14419    and FALSE otherwise.  */
14420
14421 static bool
14422 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14423                                                 cp_declarator *declarator)
14424 {
14425   unsigned num_templates;
14426
14427   /* We haven't seen any classes that involve template parameters yet.  */
14428   num_templates = 0;
14429
14430   switch (declarator->kind)
14431     {
14432     case cdk_id:
14433       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14434         {
14435           tree scope;
14436           tree member;
14437
14438           scope = TREE_OPERAND (declarator->u.id.name, 0);
14439           member = TREE_OPERAND (declarator->u.id.name, 1);
14440
14441           while (scope && CLASS_TYPE_P (scope))
14442             {
14443               /* You're supposed to have one `template <...>'
14444                  for every template class, but you don't need one
14445                  for a full specialization.  For example:
14446
14447                  template <class T> struct S{};
14448                  template <> struct S<int> { void f(); };
14449                  void S<int>::f () {}
14450
14451                  is correct; there shouldn't be a `template <>' for
14452                  the definition of `S<int>::f'.  */
14453               if (CLASSTYPE_TEMPLATE_INFO (scope)
14454                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14455                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14456                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14457                 ++num_templates;
14458
14459               scope = TYPE_CONTEXT (scope);
14460             }
14461         }
14462
14463       /* If the DECLARATOR has the form `X<y>' then it uses one
14464          additional level of template parameters.  */
14465       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14466         ++num_templates;
14467
14468       return cp_parser_check_template_parameters (parser,
14469                                                   num_templates);
14470
14471     case cdk_function:
14472     case cdk_array:
14473     case cdk_pointer:
14474     case cdk_reference:
14475     case cdk_ptrmem:
14476       return (cp_parser_check_declarator_template_parameters
14477               (parser, declarator->declarator));
14478
14479     case cdk_error:
14480       return true;
14481
14482     default:
14483       gcc_unreachable ();
14484     }
14485   return false;
14486 }
14487
14488 /* NUM_TEMPLATES were used in the current declaration.  If that is
14489    invalid, return FALSE and issue an error messages.  Otherwise,
14490    return TRUE.  */
14491
14492 static bool
14493 cp_parser_check_template_parameters (cp_parser* parser,
14494                                      unsigned num_templates)
14495 {
14496   /* If there are more template classes than parameter lists, we have
14497      something like:
14498
14499        template <class T> void S<T>::R<T>::f ();  */
14500   if (parser->num_template_parameter_lists < num_templates)
14501     {
14502       error ("too few template-parameter-lists");
14503       return false;
14504     }
14505   /* If there are the same number of template classes and parameter
14506      lists, that's OK.  */
14507   if (parser->num_template_parameter_lists == num_templates)
14508     return true;
14509   /* If there are more, but only one more, then we are referring to a
14510      member template.  That's OK too.  */
14511   if (parser->num_template_parameter_lists == num_templates + 1)
14512       return true;
14513   /* Otherwise, there are too many template parameter lists.  We have
14514      something like:
14515
14516      template <class T> template <class U> void S::f();  */
14517   error ("too many template-parameter-lists");
14518   return false;
14519 }
14520
14521 /* Parse a binary-expression of the general form:
14522
14523    binary-expression:
14524      <expr>
14525      binary-expression <token> <expr>
14526
14527    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14528    to parser the <expr>s.  If the first production is used, then the
14529    value returned by FN is returned directly.  Otherwise, a node with
14530    the indicated EXPR_TYPE is returned, with operands corresponding to
14531    the two sub-expressions.  */
14532
14533 static tree
14534 cp_parser_binary_expression (cp_parser* parser,
14535                              const cp_parser_token_tree_map token_tree_map,
14536                              cp_parser_expression_fn fn)
14537 {
14538   tree lhs;
14539
14540   /* Parse the first expression.  */
14541   lhs = (*fn) (parser);
14542   /* Now, look for more expressions.  */
14543   while (true)
14544     {
14545       cp_token *token;
14546       const cp_parser_token_tree_map_node *map_node;
14547       tree rhs;
14548
14549       /* Peek at the next token.  */
14550       token = cp_lexer_peek_token (parser->lexer);
14551       /* If the token is `>', and that's not an operator at the
14552          moment, then we're done.  */
14553       if (token->type == CPP_GREATER
14554           && !parser->greater_than_is_operator_p)
14555         break;
14556       /* If we find one of the tokens we want, build the corresponding
14557          tree representation.  */
14558       for (map_node = token_tree_map;
14559            map_node->token_type != CPP_EOF;
14560            ++map_node)
14561         if (map_node->token_type == token->type)
14562           {
14563             /* Assume that an overloaded operator will not be used.  */
14564             bool overloaded_p = false;
14565
14566             /* Consume the operator token.  */
14567             cp_lexer_consume_token (parser->lexer);
14568             /* Parse the right-hand side of the expression.  */
14569             rhs = (*fn) (parser);
14570             /* Build the binary tree node.  */
14571             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14572                                      &overloaded_p);
14573             /* If the binary operator required the use of an
14574                overloaded operator, then this expression cannot be an
14575                integral constant-expression.  An overloaded operator
14576                can be used even if both operands are otherwise
14577                permissible in an integral constant-expression if at
14578                least one of the operands is of enumeration type.  */
14579             if (overloaded_p
14580                 && (cp_parser_non_integral_constant_expression
14581                     (parser, "calls to overloaded operators")))
14582               lhs = error_mark_node;
14583             break;
14584           }
14585
14586       /* If the token wasn't one of the ones we want, we're done.  */
14587       if (map_node->token_type == CPP_EOF)
14588         break;
14589     }
14590
14591   return lhs;
14592 }
14593
14594 /* Parse an optional `::' token indicating that the following name is
14595    from the global namespace.  If so, PARSER->SCOPE is set to the
14596    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14597    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14598    Returns the new value of PARSER->SCOPE, if the `::' token is
14599    present, and NULL_TREE otherwise.  */
14600
14601 static tree
14602 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14603 {
14604   cp_token *token;
14605
14606   /* Peek at the next token.  */
14607   token = cp_lexer_peek_token (parser->lexer);
14608   /* If we're looking at a `::' token then we're starting from the
14609      global namespace, not our current location.  */
14610   if (token->type == CPP_SCOPE)
14611     {
14612       /* Consume the `::' token.  */
14613       cp_lexer_consume_token (parser->lexer);
14614       /* Set the SCOPE so that we know where to start the lookup.  */
14615       parser->scope = global_namespace;
14616       parser->qualifying_scope = global_namespace;
14617       parser->object_scope = NULL_TREE;
14618
14619       return parser->scope;
14620     }
14621   else if (!current_scope_valid_p)
14622     {
14623       parser->scope = NULL_TREE;
14624       parser->qualifying_scope = NULL_TREE;
14625       parser->object_scope = NULL_TREE;
14626     }
14627
14628   return NULL_TREE;
14629 }
14630
14631 /* Returns TRUE if the upcoming token sequence is the start of a
14632    constructor declarator.  If FRIEND_P is true, the declarator is
14633    preceded by the `friend' specifier.  */
14634
14635 static bool
14636 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14637 {
14638   bool constructor_p;
14639   tree type_decl = NULL_TREE;
14640   bool nested_name_p;
14641   cp_token *next_token;
14642
14643   /* The common case is that this is not a constructor declarator, so
14644      try to avoid doing lots of work if at all possible.  It's not
14645      valid declare a constructor at function scope.  */
14646   if (at_function_scope_p ())
14647     return false;
14648   /* And only certain tokens can begin a constructor declarator.  */
14649   next_token = cp_lexer_peek_token (parser->lexer);
14650   if (next_token->type != CPP_NAME
14651       && next_token->type != CPP_SCOPE
14652       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14653       && next_token->type != CPP_TEMPLATE_ID)
14654     return false;
14655
14656   /* Parse tentatively; we are going to roll back all of the tokens
14657      consumed here.  */
14658   cp_parser_parse_tentatively (parser);
14659   /* Assume that we are looking at a constructor declarator.  */
14660   constructor_p = true;
14661
14662   /* Look for the optional `::' operator.  */
14663   cp_parser_global_scope_opt (parser,
14664                               /*current_scope_valid_p=*/false);
14665   /* Look for the nested-name-specifier.  */
14666   nested_name_p
14667     = (cp_parser_nested_name_specifier_opt (parser,
14668                                             /*typename_keyword_p=*/false,
14669                                             /*check_dependency_p=*/false,
14670                                             /*type_p=*/false,
14671                                             /*is_declaration=*/false)
14672        != NULL_TREE);
14673   /* Outside of a class-specifier, there must be a
14674      nested-name-specifier.  */
14675   if (!nested_name_p &&
14676       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14677        || friend_p))
14678     constructor_p = false;
14679   /* If we still think that this might be a constructor-declarator,
14680      look for a class-name.  */
14681   if (constructor_p)
14682     {
14683       /* If we have:
14684
14685            template <typename T> struct S { S(); };
14686            template <typename T> S<T>::S ();
14687
14688          we must recognize that the nested `S' names a class.
14689          Similarly, for:
14690
14691            template <typename T> S<T>::S<T> ();
14692
14693          we must recognize that the nested `S' names a template.  */
14694       type_decl = cp_parser_class_name (parser,
14695                                         /*typename_keyword_p=*/false,
14696                                         /*template_keyword_p=*/false,
14697                                         /*type_p=*/false,
14698                                         /*check_dependency_p=*/false,
14699                                         /*class_head_p=*/false,
14700                                         /*is_declaration=*/false);
14701       /* If there was no class-name, then this is not a constructor.  */
14702       constructor_p = !cp_parser_error_occurred (parser);
14703     }
14704
14705   /* If we're still considering a constructor, we have to see a `(',
14706      to begin the parameter-declaration-clause, followed by either a
14707      `)', an `...', or a decl-specifier.  We need to check for a
14708      type-specifier to avoid being fooled into thinking that:
14709
14710        S::S (f) (int);
14711
14712      is a constructor.  (It is actually a function named `f' that
14713      takes one parameter (of type `int') and returns a value of type
14714      `S::S'.  */
14715   if (constructor_p
14716       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14717     {
14718       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14719           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14720           /* A parameter declaration begins with a decl-specifier,
14721              which is either the "attribute" keyword, a storage class
14722              specifier, or (usually) a type-specifier.  */
14723           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14724           && !cp_parser_storage_class_specifier_opt (parser))
14725         {
14726           tree type;
14727           bool pop_p = false;
14728           unsigned saved_num_template_parameter_lists;
14729
14730           /* Names appearing in the type-specifier should be looked up
14731              in the scope of the class.  */
14732           if (current_class_type)
14733             type = NULL_TREE;
14734           else
14735             {
14736               type = TREE_TYPE (type_decl);
14737               if (TREE_CODE (type) == TYPENAME_TYPE)
14738                 {
14739                   type = resolve_typename_type (type,
14740                                                 /*only_current_p=*/false);
14741                   if (type == error_mark_node)
14742                     {
14743                       cp_parser_abort_tentative_parse (parser);
14744                       return false;
14745                     }
14746                 }
14747               pop_p = push_scope (type);
14748             }
14749
14750           /* Inside the constructor parameter list, surrounding
14751              template-parameter-lists do not apply.  */
14752           saved_num_template_parameter_lists
14753             = parser->num_template_parameter_lists;
14754           parser->num_template_parameter_lists = 0;
14755
14756           /* Look for the type-specifier.  */
14757           cp_parser_type_specifier (parser,
14758                                     CP_PARSER_FLAGS_NONE,
14759                                     /*decl_specs=*/NULL,
14760                                     /*is_declarator=*/true,
14761                                     /*declares_class_or_enum=*/NULL,
14762                                     /*is_cv_qualifier=*/NULL);
14763
14764           parser->num_template_parameter_lists
14765             = saved_num_template_parameter_lists;
14766
14767           /* Leave the scope of the class.  */
14768           if (pop_p)
14769             pop_scope (type);
14770
14771           constructor_p = !cp_parser_error_occurred (parser);
14772         }
14773     }
14774   else
14775     constructor_p = false;
14776   /* We did not really want to consume any tokens.  */
14777   cp_parser_abort_tentative_parse (parser);
14778
14779   return constructor_p;
14780 }
14781
14782 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14783    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14784    they must be performed once we are in the scope of the function.
14785
14786    Returns the function defined.  */
14787
14788 static tree
14789 cp_parser_function_definition_from_specifiers_and_declarator
14790   (cp_parser* parser,
14791    cp_decl_specifier_seq *decl_specifiers,
14792    tree attributes,
14793    const cp_declarator *declarator)
14794 {
14795   tree fn;
14796   bool success_p;
14797
14798   /* Begin the function-definition.  */
14799   success_p = start_function (decl_specifiers, declarator, attributes);
14800
14801   /* The things we're about to see are not directly qualified by any
14802      template headers we've seen thus far.  */
14803   reset_specialization ();
14804
14805   /* If there were names looked up in the decl-specifier-seq that we
14806      did not check, check them now.  We must wait until we are in the
14807      scope of the function to perform the checks, since the function
14808      might be a friend.  */
14809   perform_deferred_access_checks ();
14810
14811   if (!success_p)
14812     {
14813       /* Skip the entire function.  */
14814       error ("invalid function declaration");
14815       cp_parser_skip_to_end_of_block_or_statement (parser);
14816       fn = error_mark_node;
14817     }
14818   else
14819     fn = cp_parser_function_definition_after_declarator (parser,
14820                                                          /*inline_p=*/false);
14821
14822   return fn;
14823 }
14824
14825 /* Parse the part of a function-definition that follows the
14826    declarator.  INLINE_P is TRUE iff this function is an inline
14827    function defined with a class-specifier.
14828
14829    Returns the function defined.  */
14830
14831 static tree
14832 cp_parser_function_definition_after_declarator (cp_parser* parser,
14833                                                 bool inline_p)
14834 {
14835   tree fn;
14836   bool ctor_initializer_p = false;
14837   bool saved_in_unbraced_linkage_specification_p;
14838   unsigned saved_num_template_parameter_lists;
14839
14840   /* If the next token is `return', then the code may be trying to
14841      make use of the "named return value" extension that G++ used to
14842      support.  */
14843   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14844     {
14845       /* Consume the `return' keyword.  */
14846       cp_lexer_consume_token (parser->lexer);
14847       /* Look for the identifier that indicates what value is to be
14848          returned.  */
14849       cp_parser_identifier (parser);
14850       /* Issue an error message.  */
14851       error ("named return values are no longer supported");
14852       /* Skip tokens until we reach the start of the function body.  */
14853       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14854              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14855         cp_lexer_consume_token (parser->lexer);
14856     }
14857   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14858      anything declared inside `f'.  */
14859   saved_in_unbraced_linkage_specification_p
14860     = parser->in_unbraced_linkage_specification_p;
14861   parser->in_unbraced_linkage_specification_p = false;
14862   /* Inside the function, surrounding template-parameter-lists do not
14863      apply.  */
14864   saved_num_template_parameter_lists
14865     = parser->num_template_parameter_lists;
14866   parser->num_template_parameter_lists = 0;
14867   /* If the next token is `try', then we are looking at a
14868      function-try-block.  */
14869   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14870     ctor_initializer_p = cp_parser_function_try_block (parser);
14871   /* A function-try-block includes the function-body, so we only do
14872      this next part if we're not processing a function-try-block.  */
14873   else
14874     ctor_initializer_p
14875       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14876
14877   /* Finish the function.  */
14878   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14879                         (inline_p ? 2 : 0));
14880   /* Generate code for it, if necessary.  */
14881   expand_or_defer_fn (fn);
14882   /* Restore the saved values.  */
14883   parser->in_unbraced_linkage_specification_p
14884     = saved_in_unbraced_linkage_specification_p;
14885   parser->num_template_parameter_lists
14886     = saved_num_template_parameter_lists;
14887
14888   return fn;
14889 }
14890
14891 /* Parse a template-declaration, assuming that the `export' (and
14892    `extern') keywords, if present, has already been scanned.  MEMBER_P
14893    is as for cp_parser_template_declaration.  */
14894
14895 static void
14896 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14897 {
14898   tree decl = NULL_TREE;
14899   tree parameter_list;
14900   bool friend_p = false;
14901
14902   /* Look for the `template' keyword.  */
14903   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14904     return;
14905
14906   /* And the `<'.  */
14907   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14908     return;
14909
14910   /* If the next token is `>', then we have an invalid
14911      specialization.  Rather than complain about an invalid template
14912      parameter, issue an error message here.  */
14913   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14914     {
14915       cp_parser_error (parser, "invalid explicit specialization");
14916       begin_specialization ();
14917       parameter_list = NULL_TREE;
14918     }
14919   else
14920     {
14921       /* Parse the template parameters.  */
14922       begin_template_parm_list ();
14923       parameter_list = cp_parser_template_parameter_list (parser);
14924       parameter_list = end_template_parm_list (parameter_list);
14925     }
14926
14927   /* Look for the `>'.  */
14928   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14929   /* We just processed one more parameter list.  */
14930   ++parser->num_template_parameter_lists;
14931   /* If the next token is `template', there are more template
14932      parameters.  */
14933   if (cp_lexer_next_token_is_keyword (parser->lexer,
14934                                       RID_TEMPLATE))
14935     cp_parser_template_declaration_after_export (parser, member_p);
14936   else
14937     {
14938       /* There are no access checks when parsing a template, as we do not
14939          know if a specialization will be a friend.  */
14940       push_deferring_access_checks (dk_no_check);
14941
14942       decl = cp_parser_single_declaration (parser,
14943                                            member_p,
14944                                            &friend_p);
14945
14946       pop_deferring_access_checks ();
14947
14948       /* If this is a member template declaration, let the front
14949          end know.  */
14950       if (member_p && !friend_p && decl)
14951         {
14952           if (TREE_CODE (decl) == TYPE_DECL)
14953             cp_parser_check_access_in_redeclaration (decl);
14954
14955           decl = finish_member_template_decl (decl);
14956         }
14957       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14958         make_friend_class (current_class_type, TREE_TYPE (decl),
14959                            /*complain=*/true);
14960     }
14961   /* We are done with the current parameter list.  */
14962   --parser->num_template_parameter_lists;
14963
14964   /* Finish up.  */
14965   finish_template_decl (parameter_list);
14966
14967   /* Register member declarations.  */
14968   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14969     finish_member_declaration (decl);
14970
14971   /* If DECL is a function template, we must return to parse it later.
14972      (Even though there is no definition, there might be default
14973      arguments that need handling.)  */
14974   if (member_p && decl
14975       && (TREE_CODE (decl) == FUNCTION_DECL
14976           || DECL_FUNCTION_TEMPLATE_P (decl)))
14977     TREE_VALUE (parser->unparsed_functions_queues)
14978       = tree_cons (NULL_TREE, decl,
14979                    TREE_VALUE (parser->unparsed_functions_queues));
14980 }
14981
14982 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14983    `function-definition' sequence.  MEMBER_P is true, this declaration
14984    appears in a class scope.
14985
14986    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14987    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14988
14989 static tree
14990 cp_parser_single_declaration (cp_parser* parser,
14991                               bool member_p,
14992                               bool* friend_p)
14993 {
14994   int declares_class_or_enum;
14995   tree decl = NULL_TREE;
14996   cp_decl_specifier_seq decl_specifiers;
14997   bool function_definition_p = false;
14998
14999   /* Defer access checks until we know what is being declared.  */
15000   push_deferring_access_checks (dk_deferred);
15001
15002   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15003      alternative.  */
15004   cp_parser_decl_specifier_seq (parser,
15005                                 CP_PARSER_FLAGS_OPTIONAL,
15006                                 &decl_specifiers,
15007                                 &declares_class_or_enum);
15008   if (friend_p)
15009     *friend_p = cp_parser_friend_p (&decl_specifiers);
15010   /* Gather up the access checks that occurred the
15011      decl-specifier-seq.  */
15012   stop_deferring_access_checks ();
15013
15014   /* Check for the declaration of a template class.  */
15015   if (declares_class_or_enum)
15016     {
15017       if (cp_parser_declares_only_class_p (parser))
15018         {
15019           decl = shadow_tag (&decl_specifiers);
15020           if (decl && decl != error_mark_node)
15021             decl = TYPE_NAME (decl);
15022           else
15023             decl = error_mark_node;
15024         }
15025     }
15026   else
15027     decl = NULL_TREE;
15028   /* If it's not a template class, try for a template function.  If
15029      the next token is a `;', then this declaration does not declare
15030      anything.  But, if there were errors in the decl-specifiers, then
15031      the error might well have come from an attempted class-specifier.
15032      In that case, there's no need to warn about a missing declarator.  */
15033   if (!decl
15034       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15035           || decl_specifiers.type != error_mark_node))
15036     decl = cp_parser_init_declarator (parser,
15037                                       &decl_specifiers,
15038                                       /*function_definition_allowed_p=*/true,
15039                                       member_p,
15040                                       declares_class_or_enum,
15041                                       &function_definition_p);
15042
15043   pop_deferring_access_checks ();
15044
15045   /* Clear any current qualification; whatever comes next is the start
15046      of something new.  */
15047   parser->scope = NULL_TREE;
15048   parser->qualifying_scope = NULL_TREE;
15049   parser->object_scope = NULL_TREE;
15050   /* Look for a trailing `;' after the declaration.  */
15051   if (!function_definition_p
15052       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
15053     cp_parser_skip_to_end_of_block_or_statement (parser);
15054
15055   return decl;
15056 }
15057
15058 /* Parse a cast-expression that is not the operand of a unary "&".  */
15059
15060 static tree
15061 cp_parser_simple_cast_expression (cp_parser *parser)
15062 {
15063   return cp_parser_cast_expression (parser, /*address_p=*/false);
15064 }
15065
15066 /* Parse a functional cast to TYPE.  Returns an expression
15067    representing the cast.  */
15068
15069 static tree
15070 cp_parser_functional_cast (cp_parser* parser, tree type)
15071 {
15072   tree expression_list;
15073   tree cast;
15074
15075   expression_list
15076     = cp_parser_parenthesized_expression_list (parser, false,
15077                                                /*non_constant_p=*/NULL);
15078
15079   cast = build_functional_cast (type, expression_list);
15080   /* [expr.const]/1: In an integral constant expression "only type
15081      conversions to integral or enumeration type can be used".  */
15082   if (cast != error_mark_node && !type_dependent_expression_p (type)
15083       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15084     {
15085       if (cp_parser_non_integral_constant_expression
15086           (parser, "a call to a constructor"))
15087         return error_mark_node;
15088     }
15089   return cast;
15090 }
15091
15092 /* Save the tokens that make up the body of a member function defined
15093    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15094    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15095    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15096    for the member function.  */
15097
15098 static tree
15099 cp_parser_save_member_function_body (cp_parser* parser,
15100                                      cp_decl_specifier_seq *decl_specifiers,
15101                                      cp_declarator *declarator,
15102                                      tree attributes)
15103 {
15104   cp_token_cache *cache;
15105   tree fn;
15106
15107   /* Create the function-declaration.  */
15108   fn = start_method (decl_specifiers, declarator, attributes);
15109   /* If something went badly wrong, bail out now.  */
15110   if (fn == error_mark_node)
15111     {
15112       /* If there's a function-body, skip it.  */
15113       if (cp_parser_token_starts_function_definition_p
15114           (cp_lexer_peek_token (parser->lexer)))
15115         cp_parser_skip_to_end_of_block_or_statement (parser);
15116       return error_mark_node;
15117     }
15118
15119   /* Remember it, if there default args to post process.  */
15120   cp_parser_save_default_args (parser, fn);
15121
15122   /* Create a token cache.  */
15123   cache = cp_token_cache_new ();
15124   /* Save away the tokens that make up the body of the
15125      function.  */
15126   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15127   /* Handle function try blocks.  */
15128   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15129     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15130
15131   /* Save away the inline definition; we will process it when the
15132      class is complete.  */
15133   DECL_PENDING_INLINE_INFO (fn) = cache;
15134   DECL_PENDING_INLINE_P (fn) = 1;
15135
15136   /* We need to know that this was defined in the class, so that
15137      friend templates are handled correctly.  */
15138   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15139
15140   /* We're done with the inline definition.  */
15141   finish_method (fn);
15142
15143   /* Add FN to the queue of functions to be parsed later.  */
15144   TREE_VALUE (parser->unparsed_functions_queues)
15145     = tree_cons (NULL_TREE, fn,
15146                  TREE_VALUE (parser->unparsed_functions_queues));
15147
15148   return fn;
15149 }
15150
15151 /* Parse a template-argument-list, as well as the trailing ">" (but
15152    not the opening ">").  See cp_parser_template_argument_list for the
15153    return value.  */
15154
15155 static tree
15156 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15157 {
15158   tree arguments;
15159   tree saved_scope;
15160   tree saved_qualifying_scope;
15161   tree saved_object_scope;
15162   bool saved_greater_than_is_operator_p;
15163
15164   /* [temp.names]
15165
15166      When parsing a template-id, the first non-nested `>' is taken as
15167      the end of the template-argument-list rather than a greater-than
15168      operator.  */
15169   saved_greater_than_is_operator_p
15170     = parser->greater_than_is_operator_p;
15171   parser->greater_than_is_operator_p = false;
15172   /* Parsing the argument list may modify SCOPE, so we save it
15173      here.  */
15174   saved_scope = parser->scope;
15175   saved_qualifying_scope = parser->qualifying_scope;
15176   saved_object_scope = parser->object_scope;
15177   /* Parse the template-argument-list itself.  */
15178   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15179     arguments = NULL_TREE;
15180   else
15181     arguments = cp_parser_template_argument_list (parser);
15182   /* Look for the `>' that ends the template-argument-list. If we find
15183      a '>>' instead, it's probably just a typo.  */
15184   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15185     {
15186       if (!saved_greater_than_is_operator_p)
15187         {
15188           /* If we're in a nested template argument list, the '>>' has to be
15189             a typo for '> >'. We emit the error message, but we continue
15190             parsing and we push a '>' as next token, so that the argument
15191             list will be parsed correctly..  */
15192           cp_token* token;
15193           error ("`>>' should be `> >' within a nested template argument list");
15194           token = cp_lexer_peek_token (parser->lexer);
15195           token->type = CPP_GREATER;
15196         }
15197       else
15198         {
15199           /* If this is not a nested template argument list, the '>>' is
15200             a typo for '>'. Emit an error message and continue.  */
15201           error ("spurious `>>', use `>' to terminate a template argument list");
15202           cp_lexer_consume_token (parser->lexer);
15203         }
15204     }
15205   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15206     error ("missing `>' to terminate the template argument list");
15207   /* The `>' token might be a greater-than operator again now.  */
15208   parser->greater_than_is_operator_p
15209     = saved_greater_than_is_operator_p;
15210   /* Restore the SAVED_SCOPE.  */
15211   parser->scope = saved_scope;
15212   parser->qualifying_scope = saved_qualifying_scope;
15213   parser->object_scope = saved_object_scope;
15214
15215   return arguments;
15216 }
15217
15218 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15219    arguments, or the body of the function have not yet been parsed,
15220    parse them now.  */
15221
15222 static void
15223 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15224 {
15225   cp_lexer *saved_lexer;
15226
15227   /* If this member is a template, get the underlying
15228      FUNCTION_DECL.  */
15229   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15230     member_function = DECL_TEMPLATE_RESULT (member_function);
15231
15232   /* There should not be any class definitions in progress at this
15233      point; the bodies of members are only parsed outside of all class
15234      definitions.  */
15235   gcc_assert (parser->num_classes_being_defined == 0);
15236   /* While we're parsing the member functions we might encounter more
15237      classes.  We want to handle them right away, but we don't want
15238      them getting mixed up with functions that are currently in the
15239      queue.  */
15240   parser->unparsed_functions_queues
15241     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15242
15243   /* Make sure that any template parameters are in scope.  */
15244   maybe_begin_member_template_processing (member_function);
15245
15246   /* If the body of the function has not yet been parsed, parse it
15247      now.  */
15248   if (DECL_PENDING_INLINE_P (member_function))
15249     {
15250       tree function_scope;
15251       cp_token_cache *tokens;
15252
15253       /* The function is no longer pending; we are processing it.  */
15254       tokens = DECL_PENDING_INLINE_INFO (member_function);
15255       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15256       DECL_PENDING_INLINE_P (member_function) = 0;
15257       /* If this was an inline function in a local class, enter the scope
15258          of the containing function.  */
15259       function_scope = decl_function_context (member_function);
15260       if (function_scope)
15261         push_function_context_to (function_scope);
15262
15263       /* Save away the current lexer.  */
15264       saved_lexer = parser->lexer;
15265       /* Make a new lexer to feed us the tokens saved for this function.  */
15266       parser->lexer = cp_lexer_new_from_tokens (tokens);
15267       parser->lexer->next = saved_lexer;
15268
15269       /* Set the current source position to be the location of the first
15270          token in the saved inline body.  */
15271       cp_lexer_peek_token (parser->lexer);
15272
15273       /* Let the front end know that we going to be defining this
15274          function.  */
15275       start_preparsed_function (member_function, NULL_TREE,
15276                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15277
15278       /* Now, parse the body of the function.  */
15279       cp_parser_function_definition_after_declarator (parser,
15280                                                       /*inline_p=*/true);
15281
15282       /* Leave the scope of the containing function.  */
15283       if (function_scope)
15284         pop_function_context_from (function_scope);
15285       /* Restore the lexer.  */
15286       parser->lexer = saved_lexer;
15287     }
15288
15289   /* Remove any template parameters from the symbol table.  */
15290   maybe_end_member_template_processing ();
15291
15292   /* Restore the queue.  */
15293   parser->unparsed_functions_queues
15294     = TREE_CHAIN (parser->unparsed_functions_queues);
15295 }
15296
15297 /* If DECL contains any default args, remember it on the unparsed
15298    functions queue.  */
15299
15300 static void
15301 cp_parser_save_default_args (cp_parser* parser, tree decl)
15302 {
15303   tree probe;
15304
15305   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15306        probe;
15307        probe = TREE_CHAIN (probe))
15308     if (TREE_PURPOSE (probe))
15309       {
15310         TREE_PURPOSE (parser->unparsed_functions_queues)
15311           = tree_cons (current_class_type, decl,
15312                        TREE_PURPOSE (parser->unparsed_functions_queues));
15313         break;
15314       }
15315   return;
15316 }
15317
15318 /* FN is a FUNCTION_DECL which may contains a parameter with an
15319    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15320    assumes that the current scope is the scope in which the default
15321    argument should be processed.  */
15322
15323 static void
15324 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15325 {
15326   cp_lexer *saved_lexer;
15327   cp_token_cache *tokens;
15328   bool saved_local_variables_forbidden_p;
15329   tree parameters;
15330
15331   /* While we're parsing the default args, we might (due to the
15332      statement expression extension) encounter more classes.  We want
15333      to handle them right away, but we don't want them getting mixed
15334      up with default args that are currently in the queue.  */
15335   parser->unparsed_functions_queues
15336     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15337
15338   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15339        parameters;
15340        parameters = TREE_CHAIN (parameters))
15341     {
15342       if (!TREE_PURPOSE (parameters)
15343           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15344         continue;
15345
15346        /* Save away the current lexer.  */
15347       saved_lexer = parser->lexer;
15348        /* Create a new one, using the tokens we have saved.  */
15349       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
15350       parser->lexer = cp_lexer_new_from_tokens (tokens);
15351
15352        /* Set the current source position to be the location of the
15353           first token in the default argument.  */
15354       cp_lexer_peek_token (parser->lexer);
15355
15356        /* Local variable names (and the `this' keyword) may not appear
15357           in a default argument.  */
15358       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15359       parser->local_variables_forbidden_p = true;
15360        /* Parse the assignment-expression.  */
15361       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15362
15363       /* If the token stream has not been completely used up, then
15364          there was extra junk after the end of the default
15365          argument.  */
15366       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15367         cp_parser_error (parser, "expected `,'");
15368
15369        /* Restore saved state.  */
15370       parser->lexer = saved_lexer;
15371       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15372     }
15373
15374   /* Restore the queue.  */
15375   parser->unparsed_functions_queues
15376     = TREE_CHAIN (parser->unparsed_functions_queues);
15377 }
15378
15379 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15380    either a TYPE or an expression, depending on the form of the
15381    input.  The KEYWORD indicates which kind of expression we have
15382    encountered.  */
15383
15384 static tree
15385 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15386 {
15387   static const char *format;
15388   tree expr = NULL_TREE;
15389   const char *saved_message;
15390   bool saved_integral_constant_expression_p;
15391
15392   /* Initialize FORMAT the first time we get here.  */
15393   if (!format)
15394     format = "types may not be defined in `%s' expressions";
15395
15396   /* Types cannot be defined in a `sizeof' expression.  Save away the
15397      old message.  */
15398   saved_message = parser->type_definition_forbidden_message;
15399   /* And create the new one.  */
15400   parser->type_definition_forbidden_message
15401     = xmalloc (strlen (format)
15402                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15403                + 1 /* `\0' */);
15404   sprintf ((char *) parser->type_definition_forbidden_message,
15405            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15406
15407   /* The restrictions on constant-expressions do not apply inside
15408      sizeof expressions.  */
15409   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15410   parser->integral_constant_expression_p = false;
15411
15412   /* Do not actually evaluate the expression.  */
15413   ++skip_evaluation;
15414   /* If it's a `(', then we might be looking at the type-id
15415      construction.  */
15416   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15417     {
15418       tree type;
15419       bool saved_in_type_id_in_expr_p;
15420
15421       /* We can't be sure yet whether we're looking at a type-id or an
15422          expression.  */
15423       cp_parser_parse_tentatively (parser);
15424       /* Consume the `('.  */
15425       cp_lexer_consume_token (parser->lexer);
15426       /* Parse the type-id.  */
15427       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15428       parser->in_type_id_in_expr_p = true;
15429       type = cp_parser_type_id (parser);
15430       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15431       /* Now, look for the trailing `)'.  */
15432       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15433       /* If all went well, then we're done.  */
15434       if (cp_parser_parse_definitely (parser))
15435         {
15436           cp_decl_specifier_seq decl_specs;
15437
15438           /* Build a trivial decl-specifier-seq.  */
15439           clear_decl_specs (&decl_specs);
15440           decl_specs.type = type;
15441
15442           /* Call grokdeclarator to figure out what type this is.  */
15443           expr = grokdeclarator (NULL,
15444                                  &decl_specs,
15445                                  TYPENAME,
15446                                  /*initialized=*/0,
15447                                  /*attrlist=*/NULL);
15448         }
15449     }
15450
15451   /* If the type-id production did not work out, then we must be
15452      looking at the unary-expression production.  */
15453   if (!expr)
15454     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15455   /* Go back to evaluating expressions.  */
15456   --skip_evaluation;
15457
15458   /* Free the message we created.  */
15459   free ((char *) parser->type_definition_forbidden_message);
15460   /* And restore the old one.  */
15461   parser->type_definition_forbidden_message = saved_message;
15462   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15463
15464   return expr;
15465 }
15466
15467 /* If the current declaration has no declarator, return true.  */
15468
15469 static bool
15470 cp_parser_declares_only_class_p (cp_parser *parser)
15471 {
15472   /* If the next token is a `;' or a `,' then there is no
15473      declarator.  */
15474   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15475           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15476 }
15477
15478 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15479
15480 static void
15481 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15482                              cp_storage_class storage_class)
15483 {
15484   if (decl_specs->storage_class != sc_none)
15485     decl_specs->multiple_storage_classes_p = true;
15486   else
15487     decl_specs->storage_class = storage_class;
15488 }
15489
15490 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15491    is true, the type is a user-defined type; otherwise it is a
15492    built-in type specified by a keyword.  */
15493
15494 static void
15495 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15496                               tree type_spec,
15497                               bool user_defined_p)
15498 {
15499   decl_specs->any_specifiers_p = true;
15500
15501   /* If the user tries to redeclare a built-in type (with, for example,
15502      in "typedef int wchar_t;") we remember that this is what
15503      happened.  In system headers, we ignore these declarations so
15504      that G++ can work with system headers that are not C++-safe.  */
15505   if (decl_specs->specs[(int) ds_typedef]
15506       && !user_defined_p
15507       && (decl_specs->type
15508           || decl_specs->specs[(int) ds_long]
15509           || decl_specs->specs[(int) ds_short]
15510           || decl_specs->specs[(int) ds_unsigned]
15511           || decl_specs->specs[(int) ds_signed]))
15512     {
15513       decl_specs->redefined_builtin_type = type_spec;
15514       if (!decl_specs->type)
15515         {
15516           decl_specs->type = type_spec;
15517           decl_specs->user_defined_type_p = false;
15518         }
15519     }
15520   else if (decl_specs->type)
15521     decl_specs->multiple_types_p = true;
15522   else
15523     {
15524       decl_specs->type = type_spec;
15525       decl_specs->user_defined_type_p = user_defined_p;
15526       decl_specs->redefined_builtin_type = NULL_TREE;
15527     }
15528 }
15529
15530 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15531    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15532
15533 static bool
15534 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15535 {
15536   return decl_specifiers->specs[(int) ds_friend] != 0;
15537 }
15538
15539 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15540    issue an error message indicating that TOKEN_DESC was expected.
15541
15542    Returns the token consumed, if the token had the appropriate type.
15543    Otherwise, returns NULL.  */
15544
15545 static cp_token *
15546 cp_parser_require (cp_parser* parser,
15547                    enum cpp_ttype type,
15548                    const char* token_desc)
15549 {
15550   if (cp_lexer_next_token_is (parser->lexer, type))
15551     return cp_lexer_consume_token (parser->lexer);
15552   else
15553     {
15554       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15555       if (!cp_parser_simulate_error (parser))
15556         {
15557           char *message = concat ("expected ", token_desc, NULL);
15558           cp_parser_error (parser, message);
15559           free (message);
15560         }
15561       return NULL;
15562     }
15563 }
15564
15565 /* Like cp_parser_require, except that tokens will be skipped until
15566    the desired token is found.  An error message is still produced if
15567    the next token is not as expected.  */
15568
15569 static void
15570 cp_parser_skip_until_found (cp_parser* parser,
15571                             enum cpp_ttype type,
15572                             const char* token_desc)
15573 {
15574   cp_token *token;
15575   unsigned nesting_depth = 0;
15576
15577   if (cp_parser_require (parser, type, token_desc))
15578     return;
15579
15580   /* Skip tokens until the desired token is found.  */
15581   while (true)
15582     {
15583       /* Peek at the next token.  */
15584       token = cp_lexer_peek_token (parser->lexer);
15585       /* If we've reached the token we want, consume it and
15586          stop.  */
15587       if (token->type == type && !nesting_depth)
15588         {
15589           cp_lexer_consume_token (parser->lexer);
15590           return;
15591         }
15592       /* If we've run out of tokens, stop.  */
15593       if (token->type == CPP_EOF)
15594         return;
15595       if (token->type == CPP_OPEN_BRACE
15596           || token->type == CPP_OPEN_PAREN
15597           || token->type == CPP_OPEN_SQUARE)
15598         ++nesting_depth;
15599       else if (token->type == CPP_CLOSE_BRACE
15600                || token->type == CPP_CLOSE_PAREN
15601                || token->type == CPP_CLOSE_SQUARE)
15602         {
15603           if (nesting_depth-- == 0)
15604             return;
15605         }
15606       /* Consume this token.  */
15607       cp_lexer_consume_token (parser->lexer);
15608     }
15609 }
15610
15611 /* If the next token is the indicated keyword, consume it.  Otherwise,
15612    issue an error message indicating that TOKEN_DESC was expected.
15613
15614    Returns the token consumed, if the token had the appropriate type.
15615    Otherwise, returns NULL.  */
15616
15617 static cp_token *
15618 cp_parser_require_keyword (cp_parser* parser,
15619                            enum rid keyword,
15620                            const char* token_desc)
15621 {
15622   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15623
15624   if (token && token->keyword != keyword)
15625     {
15626       dyn_string_t error_msg;
15627
15628       /* Format the error message.  */
15629       error_msg = dyn_string_new (0);
15630       dyn_string_append_cstr (error_msg, "expected ");
15631       dyn_string_append_cstr (error_msg, token_desc);
15632       cp_parser_error (parser, error_msg->s);
15633       dyn_string_delete (error_msg);
15634       return NULL;
15635     }
15636
15637   return token;
15638 }
15639
15640 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15641    function-definition.  */
15642
15643 static bool
15644 cp_parser_token_starts_function_definition_p (cp_token* token)
15645 {
15646   return (/* An ordinary function-body begins with an `{'.  */
15647           token->type == CPP_OPEN_BRACE
15648           /* A ctor-initializer begins with a `:'.  */
15649           || token->type == CPP_COLON
15650           /* A function-try-block begins with `try'.  */
15651           || token->keyword == RID_TRY
15652           /* The named return value extension begins with `return'.  */
15653           || token->keyword == RID_RETURN);
15654 }
15655
15656 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15657    definition.  */
15658
15659 static bool
15660 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15661 {
15662   cp_token *token;
15663
15664   token = cp_lexer_peek_token (parser->lexer);
15665   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15666 }
15667
15668 /* Returns TRUE iff the next token is the "," or ">" ending a
15669    template-argument. ">>" is also accepted (after the full
15670    argument was parsed) because it's probably a typo for "> >",
15671    and there is a specific diagnostic for this.  */
15672
15673 static bool
15674 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15675 {
15676   cp_token *token;
15677
15678   token = cp_lexer_peek_token (parser->lexer);
15679   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15680           || token->type == CPP_RSHIFT);
15681 }
15682
15683 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15684    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15685
15686 static bool
15687 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15688                                                      size_t n)
15689 {
15690   cp_token *token;
15691
15692   token = cp_lexer_peek_nth_token (parser->lexer, n);
15693   if (token->type == CPP_LESS)
15694     return true;
15695   /* Check for the sequence `<::' in the original code. It would be lexed as
15696      `[:', where `[' is a digraph, and there is no whitespace before
15697      `:'.  */
15698   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15699     {
15700       cp_token *token2;
15701       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15702       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15703         return true;
15704     }
15705   return false;
15706 }
15707
15708 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15709    or none_type otherwise.  */
15710
15711 static enum tag_types
15712 cp_parser_token_is_class_key (cp_token* token)
15713 {
15714   switch (token->keyword)
15715     {
15716     case RID_CLASS:
15717       return class_type;
15718     case RID_STRUCT:
15719       return record_type;
15720     case RID_UNION:
15721       return union_type;
15722
15723     default:
15724       return none_type;
15725     }
15726 }
15727
15728 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15729
15730 static void
15731 cp_parser_check_class_key (enum tag_types class_key, tree type)
15732 {
15733   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15734     pedwarn ("`%s' tag used in naming `%#T'",
15735             class_key == union_type ? "union"
15736              : class_key == record_type ? "struct" : "class",
15737              type);
15738 }
15739
15740 /* Issue an error message if DECL is redeclared with different
15741    access than its original declaration [class.access.spec/3].
15742    This applies to nested classes and nested class templates.
15743    [class.mem/1].  */
15744
15745 static void cp_parser_check_access_in_redeclaration (tree decl)
15746 {
15747   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15748     return;
15749
15750   if ((TREE_PRIVATE (decl)
15751        != (current_access_specifier == access_private_node))
15752       || (TREE_PROTECTED (decl)
15753           != (current_access_specifier == access_protected_node)))
15754     error ("%D redeclared with different access", decl);
15755 }
15756
15757 /* Look for the `template' keyword, as a syntactic disambiguator.
15758    Return TRUE iff it is present, in which case it will be
15759    consumed.  */
15760
15761 static bool
15762 cp_parser_optional_template_keyword (cp_parser *parser)
15763 {
15764   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15765     {
15766       /* The `template' keyword can only be used within templates;
15767          outside templates the parser can always figure out what is a
15768          template and what is not.  */
15769       if (!processing_template_decl)
15770         {
15771           error ("`template' (as a disambiguator) is only allowed "
15772                  "within templates");
15773           /* If this part of the token stream is rescanned, the same
15774              error message would be generated.  So, we purge the token
15775              from the stream.  */
15776           cp_lexer_purge_token (parser->lexer);
15777           return false;
15778         }
15779       else
15780         {
15781           /* Consume the `template' keyword.  */
15782           cp_lexer_consume_token (parser->lexer);
15783           return true;
15784         }
15785     }
15786
15787   return false;
15788 }
15789
15790 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15791    set PARSER->SCOPE, and perform other related actions.  */
15792
15793 static void
15794 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15795 {
15796   tree value;
15797   tree check;
15798
15799   /* Get the stored value.  */
15800   value = cp_lexer_consume_token (parser->lexer)->value;
15801   /* Perform any access checks that were deferred.  */
15802   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15803     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15804   /* Set the scope from the stored value.  */
15805   parser->scope = TREE_VALUE (value);
15806   parser->qualifying_scope = TREE_TYPE (value);
15807   parser->object_scope = NULL_TREE;
15808 }
15809
15810 /* Add tokens to CACHE until a non-nested END token appears.  */
15811
15812 static void
15813 cp_parser_cache_group_1 (cp_parser *parser,
15814                          cp_token_cache *cache,
15815                          enum cpp_ttype end,
15816                          unsigned depth)
15817 {
15818   while (true)
15819     {
15820       cp_token *token;
15821
15822       /* Abort a parenthesized expression if we encounter a brace.  */
15823       if ((end == CPP_CLOSE_PAREN || depth == 0)
15824           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15825         return;
15826       /* If we've reached the end of the file, stop.  */
15827       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15828         return;
15829       /* Consume the next token.  */
15830       token = cp_lexer_consume_token (parser->lexer);
15831       /* Add this token to the tokens we are saving.  */
15832       cp_token_cache_push_token (cache, token);
15833       /* See if it starts a new group.  */
15834       if (token->type == CPP_OPEN_BRACE)
15835         {
15836           cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15837           if (depth == 0)
15838             return;
15839         }
15840       else if (token->type == CPP_OPEN_PAREN)
15841         cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15842       else if (token->type == end)
15843         return;
15844     }
15845 }
15846
15847 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15848    preserve string tokens in both translated and untranslated
15849    forms.  */
15850
15851 static void
15852 cp_parser_cache_group (cp_parser *parser,
15853                          cp_token_cache *cache,
15854                          enum cpp_ttype end,
15855                          unsigned depth)
15856 {
15857   int saved_c_lex_string_translate;
15858
15859   saved_c_lex_string_translate = c_lex_string_translate;
15860   c_lex_string_translate = -1;
15861
15862   cp_parser_cache_group_1 (parser, cache, end, depth);
15863
15864   c_lex_string_translate = saved_c_lex_string_translate;
15865 }
15866
15867
15868 /* Begin parsing tentatively.  We always save tokens while parsing
15869    tentatively so that if the tentative parsing fails we can restore the
15870    tokens.  */
15871
15872 static void
15873 cp_parser_parse_tentatively (cp_parser* parser)
15874 {
15875   /* Enter a new parsing context.  */
15876   parser->context = cp_parser_context_new (parser->context);
15877   /* Begin saving tokens.  */
15878   cp_lexer_save_tokens (parser->lexer);
15879   /* In order to avoid repetitive access control error messages,
15880      access checks are queued up until we are no longer parsing
15881      tentatively.  */
15882   push_deferring_access_checks (dk_deferred);
15883 }
15884
15885 /* Commit to the currently active tentative parse.  */
15886
15887 static void
15888 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15889 {
15890   cp_parser_context *context;
15891   cp_lexer *lexer;
15892
15893   /* Mark all of the levels as committed.  */
15894   lexer = parser->lexer;
15895   for (context = parser->context; context->next; context = context->next)
15896     {
15897       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15898         break;
15899       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15900       while (!cp_lexer_saving_tokens (lexer))
15901         lexer = lexer->next;
15902       cp_lexer_commit_tokens (lexer);
15903     }
15904 }
15905
15906 /* Abort the currently active tentative parse.  All consumed tokens
15907    will be rolled back, and no diagnostics will be issued.  */
15908
15909 static void
15910 cp_parser_abort_tentative_parse (cp_parser* parser)
15911 {
15912   cp_parser_simulate_error (parser);
15913   /* Now, pretend that we want to see if the construct was
15914      successfully parsed.  */
15915   cp_parser_parse_definitely (parser);
15916 }
15917
15918 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15919    token stream.  Otherwise, commit to the tokens we have consumed.
15920    Returns true if no error occurred; false otherwise.  */
15921
15922 static bool
15923 cp_parser_parse_definitely (cp_parser* parser)
15924 {
15925   bool error_occurred;
15926   cp_parser_context *context;
15927
15928   /* Remember whether or not an error occurred, since we are about to
15929      destroy that information.  */
15930   error_occurred = cp_parser_error_occurred (parser);
15931   /* Remove the topmost context from the stack.  */
15932   context = parser->context;
15933   parser->context = context->next;
15934   /* If no parse errors occurred, commit to the tentative parse.  */
15935   if (!error_occurred)
15936     {
15937       /* Commit to the tokens read tentatively, unless that was
15938          already done.  */
15939       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15940         cp_lexer_commit_tokens (parser->lexer);
15941
15942       pop_to_parent_deferring_access_checks ();
15943     }
15944   /* Otherwise, if errors occurred, roll back our state so that things
15945      are just as they were before we began the tentative parse.  */
15946   else
15947     {
15948       cp_lexer_rollback_tokens (parser->lexer);
15949       pop_deferring_access_checks ();
15950     }
15951   /* Add the context to the front of the free list.  */
15952   context->next = cp_parser_context_free_list;
15953   cp_parser_context_free_list = context;
15954
15955   return !error_occurred;
15956 }
15957
15958 /* Returns true if we are parsing tentatively -- but have decided that
15959    we will stick with this tentative parse, even if errors occur.  */
15960
15961 static bool
15962 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15963 {
15964   return (cp_parser_parsing_tentatively (parser)
15965           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15966 }
15967
15968 /* Returns nonzero iff an error has occurred during the most recent
15969    tentative parse.  */
15970
15971 static bool
15972 cp_parser_error_occurred (cp_parser* parser)
15973 {
15974   return (cp_parser_parsing_tentatively (parser)
15975           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15976 }
15977
15978 /* Returns nonzero if GNU extensions are allowed.  */
15979
15980 static bool
15981 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15982 {
15983   return parser->allow_gnu_extensions_p;
15984 }
15985
15986 \f
15987 /* The parser.  */
15988
15989 static GTY (()) cp_parser *the_parser;
15990
15991 /* External interface.  */
15992
15993 /* Parse one entire translation unit.  */
15994
15995 void
15996 c_parse_file (void)
15997 {
15998   bool error_occurred;
15999   static bool already_called = false;
16000
16001   if (already_called)
16002     {
16003       sorry ("inter-module optimizations not implemented for C++");
16004       return;
16005     }
16006   already_called = true;
16007
16008   the_parser = cp_parser_new ();
16009   push_deferring_access_checks (flag_access_control
16010                                 ? dk_no_deferred : dk_no_check);
16011   error_occurred = cp_parser_translation_unit (the_parser);
16012   the_parser = NULL;
16013 }
16014
16015 /* This variable must be provided by every front end.  */
16016
16017 int yydebug;
16018
16019 #include "gt-cp-parser.h"