OSDN Git Service

gcc/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 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43    and c-lex.c) and the C++ parser.  */
44
45 /* A C++ token.  */
46
47 typedef struct cp_token GTY (())
48 {
49   /* The kind of token.  */
50   ENUM_BITFIELD (cpp_ttype) type : 8;
51   /* If this token is a keyword, this value indicates which keyword.
52      Otherwise, this value is RID_MAX.  */
53   ENUM_BITFIELD (rid) keyword : 8;
54   /* Token flags.  */
55   unsigned char flags;
56   /* True if this token is from a system header. */
57   BOOL_BITFIELD in_system_header : 1;
58   /* The value associated with this token, if any.  */
59   tree value;
60   /* The location at which this token was found.  */
61   location_t location;
62 } cp_token;
63
64 /* The cp_lexer structure represents the C++ lexer.  It is responsible
65    for managing the token stream from the preprocessor and supplying
66    it to the parser.  Tokens are never added to the cp_lexer after
67    it is created. */
68
69 typedef struct cp_lexer GTY (())
70 {
71   /* The memory allocated for the buffer.  Never NULL.  */
72   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
73   /* A pointer just past the end of the memory allocated for the buffer.  */
74   cp_token * GTY ((skip)) buffer_end;
75   /* A pointer just past the last available token.  The tokens
76      in this lexer are [buffer, last_token). */
77   cp_token * GTY ((skip)) last_token;
78
79   /* The next available token.  If NEXT_TOKEN is NULL, then there are
80      no more available tokens.  */
81   cp_token * GTY ((skip)) next_token;
82
83   /* A stack indicating positions at which cp_lexer_save_tokens was
84      called.  The top entry is the most recent position at which we
85      began saving tokens.  The entries are differences in token
86      position between BUFFER and the first saved token.
87      If the stack is non-empty, we are saving tokens.  */
88   varray_type saved_tokens;
89
90   /* True if we should output debugging information.  */
91   bool debugging_p;
92
93   /* The next lexer in a linked list of lexers.  */
94   struct cp_lexer *next;
95 } cp_lexer;
96
97 /* cp_token_cache is a range of tokens.  There is no need to represent
98    allocate heap memory for it, since tokens are never removed from the
99    lexer's array.  There is also no need for the GC to walk through
100    a cp_token_cache, since everything in here is referenced through
101    a lexer. */
102
103 typedef struct cp_token_cache GTY(())
104 {
105   /* The beginning of the token range. */
106   cp_token * GTY((skip)) first;
107
108   /* Points immediately after the last token in the range. */
109   cp_token * GTY ((skip)) last;
110 } cp_token_cache;
111
112 /* Prototypes.  */
113
114 static cp_lexer *cp_lexer_new_main
115   (void);
116 static cp_lexer *cp_lexer_new_from_tokens
117   (cp_token_cache *tokens);
118 static void cp_lexer_destroy
119   (cp_lexer *);
120 static int cp_lexer_saving_tokens
121   (const cp_lexer *);
122 static cp_token *cp_lexer_next_token
123   (cp_lexer *, cp_token *);
124 static cp_token *cp_lexer_prev_token
125   (cp_lexer *, cp_token *);
126 static ptrdiff_t cp_lexer_token_difference
127   (cp_lexer *, cp_token *, cp_token *);
128 static void cp_lexer_grow_buffer
129   (cp_lexer *);
130 static void cp_lexer_get_preprocessor_token
131   (cp_lexer *, cp_token *);
132 static inline cp_token *cp_lexer_peek_token
133   (cp_lexer *);
134 static cp_token *cp_lexer_peek_nth_token
135   (cp_lexer *, size_t);
136 static inline bool cp_lexer_next_token_is
137   (cp_lexer *, enum cpp_ttype);
138 static bool cp_lexer_next_token_is_not
139   (cp_lexer *, enum cpp_ttype);
140 static bool cp_lexer_next_token_is_keyword
141   (cp_lexer *, enum rid);
142 static cp_token *cp_lexer_consume_token
143   (cp_lexer *);
144 static void cp_lexer_purge_token
145   (cp_lexer *);
146 static void cp_lexer_purge_tokens_after
147   (cp_lexer *, cp_token *);
148 static void cp_lexer_handle_pragma
149   (cp_lexer *);
150 static void cp_lexer_save_tokens
151   (cp_lexer *);
152 static void cp_lexer_commit_tokens
153   (cp_lexer *);
154 static void cp_lexer_rollback_tokens
155   (cp_lexer *);
156 #ifdef ENABLE_CHECKING
157 static void cp_lexer_print_token
158   (FILE *, cp_token *);
159 static inline bool cp_lexer_debugging_p
160   (cp_lexer *);
161 static void cp_lexer_start_debugging
162   (cp_lexer *) ATTRIBUTE_UNUSED;
163 static void cp_lexer_stop_debugging
164   (cp_lexer *) ATTRIBUTE_UNUSED;
165 static void cp_lexer_peek_token_emit_debug_info
166   (cp_lexer *, cp_token *);
167 #else
168 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
169    about passing NULL to functions that require non-NULL arguments
170    (fputs, fprintf).  It will never be used, so all we need is a value
171    of the right type that's guaranteed not to be NULL.  */
172 #define cp_lexer_debug_stream stdout
173 #define cp_lexer_print_token(str, tok) (void) 0
174 #define cp_lexer_debugging_p(lexer) 0
175 #define cp_lexer_peek_token_emit_debug_info(lexer, tok) (void) 0
176 #endif /* ENABLE_CHECKING */
177
178 static cp_token_cache *cp_token_cache_new
179   (cp_token *, cp_token *);
180
181 /* Manifest constants.  */
182
183 #define CP_LEXER_BUFFER_SIZE 10000
184 #define CP_SAVED_TOKENS_SIZE 5
185
186 /* A token type for keywords, as opposed to ordinary identifiers.  */
187 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
188
189 /* A token type for template-ids.  If a template-id is processed while
190    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
191    the value of the CPP_TEMPLATE_ID is whatever was returned by
192    cp_parser_template_id.  */
193 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
194
195 /* A token type for nested-name-specifiers.  If a
196    nested-name-specifier is processed while parsing tentatively, it is
197    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
198    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
199    cp_parser_nested_name_specifier_opt.  */
200 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
201
202 /* A token type for tokens that are not tokens at all; these are used
203    to represent slots in the array where there used to be a token
204    that has now been deleted. */
205 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
206
207 /* The number of token types, including C++-specific ones.  */
208 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
209
210 /* Variables.  */
211
212 #ifdef ENABLE_CHECKING
213 /* The stream to which debugging output should be written.  */
214 static FILE *cp_lexer_debug_stream;
215 #endif /* ENABLE_CHECKING */
216
217 /* Create a new main C++ lexer, the lexer that gets tokens from the
218    preprocessor.  */
219
220 static cp_lexer *
221 cp_lexer_new_main (void)
222 {
223   cp_lexer *lexer;
224   cp_token first_token;
225
226   /* Tell cpplib we want CPP_PRAGMA tokens. */
227   cpp_get_options (parse_in)->defer_pragmas = true;
228
229   /* Tell c_lex not to merge string constants.  */
230   c_lex_return_raw_strings = true;
231
232   /* It's possible that lexing the first token will load a PCH file,
233      which is a GC collection point.  So we have to grab the first
234      token before allocating any memory.  */
235   cp_lexer_get_preprocessor_token (NULL, &first_token);
236   c_common_no_more_pch ();
237
238   /* Allocate the memory.  */
239   lexer = GGC_CNEW (cp_lexer);
240
241   /* Create the buffer.  */
242   lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
243   lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
244  
245   /* There is one token in the buffer.  */
246   lexer->last_token = lexer->buffer + 1;
247   lexer->next_token = lexer->buffer;
248   *lexer->next_token = first_token;
249
250   /* Create the SAVED_TOKENS stack.  */
251   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
252
253 #ifdef ENABLE_CHECKING  
254   /* Initially we are not debugging.  */
255   lexer->debugging_p = false;
256 #endif /* ENABLE_CHECKING */
257
258   /* Get the rest of the tokens from the preprocessor. */
259   while (lexer->last_token[-1].type != CPP_EOF)
260     {
261       if (lexer->last_token == lexer->buffer_end)
262         cp_lexer_grow_buffer (lexer);
263       cp_lexer_get_preprocessor_token (lexer, lexer->last_token++);
264     }
265
266   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
267      direct calls to c_lex.  Those callers all expect c_lex to do
268      string constant concatenation.  */
269   c_lex_return_raw_strings = false;
270
271   gcc_assert (lexer->next_token->type != CPP_PURGED);
272   return lexer;
273 }
274
275 /* Create a new lexer whose token stream is primed with the tokens in
276    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
277
278 static cp_lexer *
279 cp_lexer_new_from_tokens (cp_token_cache *cache)
280 {
281   cp_token *first = cache->first;
282   cp_token *last = cache->last;
283   cp_lexer *lexer = GGC_CNEW (cp_lexer);
284   cp_token *eof;
285
286   /* Allocate a new buffer.  The reason we do this is to make sure
287      there's a CPP_EOF token at the end.  An alternative would be to
288      modify cp_lexer_peek_token so that it checks for end-of-buffer
289      and returns a CPP_EOF when appropriate. */
290
291   lexer->buffer = GGC_NEWVEC (cp_token, (last - first) + 1);
292   memcpy (lexer->buffer, first, sizeof (cp_token) * (last - first));
293   lexer->next_token = lexer->buffer;
294   lexer->buffer_end = lexer->last_token = lexer->buffer + (last - first);
295
296   eof = lexer->buffer + (last - first);
297   eof->type = CPP_EOF;
298   eof->location = UNKNOWN_LOCATION;
299   eof->value = NULL_TREE;
300   eof->keyword = RID_MAX;
301
302   /* Create the SAVED_TOKENS stack.  */
303   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
304
305 #ifdef ENABLE_CHECKING
306   /* Initially we are not debugging.  */
307   lexer->debugging_p = false;
308 #endif
309
310   gcc_assert (lexer->next_token->type != CPP_PURGED);
311   return lexer;
312 }
313
314 /* Frees all resources associated with LEXER. */
315
316 static void
317 cp_lexer_destroy (cp_lexer *lexer)
318 {
319   ggc_free (lexer->buffer);
320   ggc_free (lexer);
321 }
322
323 /* Returns nonzero if debugging information should be output.  */
324
325 #ifdef ENABLE_CHECKING
326
327 static inline bool
328 cp_lexer_debugging_p (cp_lexer *lexer)
329 {
330   return lexer->debugging_p;
331 }
332
333 #endif /* ENABLE_CHECKING */
334
335 /* TOKEN points into the circular token buffer.  Return a pointer to
336    the next token in the buffer.  */
337
338 static inline cp_token *
339 cp_lexer_next_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
340 {
341   token++;
342   return token;
343 }
344
345 /* TOKEN points into the circular token buffer.  Return a pointer to
346    the previous token in the buffer.  */
347
348 static inline cp_token *
349 cp_lexer_prev_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
350 {
351   return token - 1;
352 }
353
354 /* nonzero if we are presently saving tokens.  */
355
356 static int
357 cp_lexer_saving_tokens (const cp_lexer* lexer)
358 {
359   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
360 }
361
362 /* Return a pointer to the token that is N tokens beyond TOKEN in the
363    buffer.  */
364
365 static inline cp_token *
366 cp_lexer_advance_token (cp_lexer *lexer ATTRIBUTE_UNUSED,
367                         cp_token *token, ptrdiff_t n)
368 {
369   return token + n;
370 }
371
372 /* Returns the number of times that START would have to be incremented
373    to reach FINISH.  If START and FINISH are the same, returns zero.  */
374
375 static inline ptrdiff_t
376 cp_lexer_token_difference (cp_lexer* lexer ATTRIBUTE_UNUSED,
377                            cp_token* start, cp_token* finish)
378 {
379   return finish - start;
380 }
381
382 /* If the buffer is full, make it bigger.  */
383 static void
384 cp_lexer_grow_buffer (cp_lexer* lexer)
385 {
386   cp_token *old_buffer;
387   cp_token *new_buffer;
388   ptrdiff_t buffer_length;
389
390   /* This function should only be called when buffer is full. */
391   gcc_assert (lexer->last_token == lexer->buffer_end);
392
393   /* Remember the current buffer pointer.  It will become invalid,
394      but we will need to do pointer arithmetic involving this
395      value.  */
396   old_buffer = lexer->buffer;
397   /* Compute the current buffer size.  */
398   buffer_length = lexer->buffer_end - lexer->buffer;
399   /* Allocate a buffer twice as big.  */
400   new_buffer = ggc_realloc (lexer->buffer,
401                             2 * buffer_length * sizeof (cp_token));
402
403   /* Recompute buffer positions. */
404   lexer->buffer = new_buffer;
405   lexer->buffer_end = new_buffer + 2 * buffer_length;
406   lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
407   lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
408
409   /* Clear the rest of the buffer.  We never look at this storage,
410      but the garbage collector may.  */
411   memset (lexer->last_token, 0,
412           (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
413 }
414
415 /* Store the next token from the preprocessor in *TOKEN.  */
416
417 static void
418 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
419                                  cp_token *token)
420 {
421   bool done;
422
423   done = false;
424   /* Keep going until we get a token we like.  */
425   while (!done)
426     {
427       /* Get a new token from the preprocessor.  */
428       token->type = c_lex_with_flags (&token->value, &token->flags);
429       /* Issue messages about tokens we cannot process.  */
430       switch (token->type)
431         {
432         case CPP_ATSIGN:
433         case CPP_HASH:
434         case CPP_PASTE:
435           error ("invalid token");
436           break;
437
438         default:
439           /* This is a good token, so we exit the loop.  */
440           done = true;
441           break;
442         }
443     }
444   /* Now we've got our token.  */
445   token->location = input_location;
446   token->in_system_header = in_system_header;
447
448   /* Check to see if this token is a keyword.  */
449   if (token->type == CPP_NAME
450       && C_IS_RESERVED_WORD (token->value))
451     {
452       /* Mark this token as a keyword.  */
453       token->type = CPP_KEYWORD;
454       /* Record which keyword.  */
455       token->keyword = C_RID_CODE (token->value);
456       /* Update the value.  Some keywords are mapped to particular
457          entities, rather than simply having the value of the
458          corresponding IDENTIFIER_NODE.  For example, `__const' is
459          mapped to `const'.  */
460       token->value = ridpointers[token->keyword];
461     }
462   else
463     token->keyword = RID_MAX;
464 }
465
466 /* Update the globals input_location and in_system_header from TOKEN.   */
467 static inline void
468 cp_lexer_set_source_position_from_token (cp_token *token)
469 {
470   if (token->type != CPP_EOF)
471     {
472       input_location = token->location;
473       in_system_header = token->in_system_header;
474     }
475 }
476
477 /* Return a pointer to the next token in the token stream, but do not
478    consume it.  */
479
480 static inline cp_token *
481 cp_lexer_peek_token (cp_lexer *lexer)
482 {
483   if (cp_lexer_debugging_p (lexer))
484     cp_lexer_peek_token_emit_debug_info (lexer, lexer->next_token);
485   return lexer->next_token;
486 }
487
488 #ifdef ENABLE_CHECKING
489 /* Emit debug output for cp_lexer_peek_token.  Split out into a
490    separate function so that cp_lexer_peek_token can be small and
491    inlinable. */
492
493 static void
494 cp_lexer_peek_token_emit_debug_info (cp_lexer *lexer ATTRIBUTE_UNUSED,
495                                      cp_token *token ATTRIBUTE_UNUSED)
496 {
497   fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
498   cp_lexer_print_token (cp_lexer_debug_stream, token);
499   putc ('\n', cp_lexer_debug_stream);
500 }
501 #endif
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   cp_token *token;
525
526   /* Peek at the next token.  */
527   token = cp_lexer_peek_token (lexer);
528   /* Check to see if it is the indicated keyword.  */
529   return token->keyword == keyword;
530 }
531
532 /* Return a pointer to the Nth token in the token stream.  If N is 1,
533    then this is precisely equivalent to cp_lexer_peek_token (except
534    that it is not inline).  One would like to disallow that case, but
535    there is one case (cp_parser_nth_token_starts_template_id) where
536    the caller passes a variable for N and it might be 1.  */
537
538 static cp_token *
539 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
540 {
541   cp_token *token;
542
543   /* N is 1-based, not zero-based.  */
544   gcc_assert (n > 0);
545
546   if (cp_lexer_debugging_p (lexer))
547     fprintf (cp_lexer_debug_stream,
548              "cp_lexer: peeking ahead %ld at token: ", (long)n);
549
550   --n;
551   token = lexer->next_token;
552   while (n != 0)
553     {
554       ++token;
555       if (token->type != CPP_PURGED)
556         --n;
557     }
558
559   if (cp_lexer_debugging_p (lexer))
560     {
561       cp_lexer_print_token (cp_lexer_debug_stream, token);
562       putc ('\n', cp_lexer_debug_stream);
563     }
564
565   return token;
566 }
567
568 /* Return the next token, and advance the lexer's next_token pointer
569    to point to the next non-purged token.  */
570
571 static cp_token *
572 cp_lexer_consume_token (cp_lexer* lexer)
573 {
574   cp_token *token = lexer->next_token;
575
576   do
577     ++lexer->next_token;
578   while (lexer->next_token->type == CPP_PURGED);
579
580   cp_lexer_set_source_position_from_token (token);
581
582   /* Provide debugging output.  */
583   if (cp_lexer_debugging_p (lexer))
584     {
585       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
586       cp_lexer_print_token (cp_lexer_debug_stream, token);
587       putc ('\n', cp_lexer_debug_stream);
588     }
589
590   return token;
591 }
592
593 /* Permanently remove the next token from the token stream, and
594    advance the next_token pointer to refer to the next non-purged
595    token.  */
596
597 static void
598 cp_lexer_purge_token (cp_lexer *lexer)
599 {
600   cp_token *tok = lexer->next_token;
601   tok->type = CPP_PURGED;
602   tok->location = UNKNOWN_LOCATION;
603   tok->value = NULL_TREE;
604   tok->keyword = RID_MAX;
605
606   do
607     ++lexer->next_token;
608   while (lexer->next_token->type == CPP_PURGED);
609 }
610
611 /* Permanently remove all tokens after TOK, up to, but not
612    including, the token that will be returned next by
613    cp_lexer_peek_token.  */
614
615 static void
616 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 {
618   cp_token *peek;
619
620   peek = cp_lexer_peek_token (lexer);
621   gcc_assert (tok < peek);
622
623   for ( tok += 1; tok != peek; tok += 1)
624     {
625       tok->type = CPP_PURGED;
626       tok->location = UNKNOWN_LOCATION;
627       tok->value = NULL_TREE;
628       tok->keyword = RID_MAX;
629     }
630 }
631
632 /* Consume and handle a pragma token.   */
633 static void
634 cp_lexer_handle_pragma (cp_lexer *lexer)
635 {
636   cpp_string s;
637   cp_token *token = cp_lexer_consume_token (lexer);
638   gcc_assert (token->type == CPP_PRAGMA);
639   gcc_assert (token->value);
640
641   s.len = TREE_STRING_LENGTH (token->value);
642   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
643
644   cpp_handle_deferred_pragma (parse_in, &s);
645
646   /* Clearing token->value here means that we will get an ICE if we
647      try to process this #pragma again (which should be impossible).  */
648   token->value = NULL;
649 }
650
651 /* Begin saving tokens.  All tokens consumed after this point will be
652    preserved.  */
653
654 static void
655 cp_lexer_save_tokens (cp_lexer* lexer)
656 {
657   /* Provide debugging output.  */
658   if (cp_lexer_debugging_p (lexer))
659     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
660
661   VARRAY_PUSH_INT (lexer->saved_tokens,
662                    cp_lexer_token_difference (lexer,
663                                               lexer->buffer,
664                                               lexer->next_token));
665 }
666
667 /* Commit to the portion of the token stream most recently saved.  */
668
669 static void
670 cp_lexer_commit_tokens (cp_lexer* lexer)
671 {
672   /* Provide debugging output.  */
673   if (cp_lexer_debugging_p (lexer))
674     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
675
676   VARRAY_POP (lexer->saved_tokens);
677 }
678
679 /* Return all tokens saved since the last call to cp_lexer_save_tokens
680    to the token stream.  Stop saving tokens.  */
681
682 static void
683 cp_lexer_rollback_tokens (cp_lexer* lexer)
684 {
685   size_t delta;
686
687   /* Provide debugging output.  */
688   if (cp_lexer_debugging_p (lexer))
689     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
690
691   /* Find the token that was the NEXT_TOKEN when we started saving
692      tokens.  */
693   delta = VARRAY_TOP_INT(lexer->saved_tokens);
694   /* Make it the next token again now.  */
695   lexer->next_token = cp_lexer_advance_token (lexer, lexer->buffer, delta);
696
697   /* Stop saving tokens.  */
698   VARRAY_POP (lexer->saved_tokens);
699 }
700
701 /* Print a representation of the TOKEN on the STREAM.  */
702
703 #ifdef ENABLE_CHECKING
704
705 static void
706 cp_lexer_print_token (FILE * stream, cp_token *token)
707 {
708   /* We don't use cpp_type2name here because the parser defines
709      a few tokens of its own.  */
710   static const char *const token_names[] = {
711     /* cpplib-defined token types */
712 #define OP(e, s) #e,
713 #define TK(e, s) #e,
714     TTYPE_TABLE
715 #undef OP
716 #undef TK
717     /* C++ parser token types - see "Manifest constants", above.  */
718     "KEYWORD",
719     "TEMPLATE_ID",
720     "NESTED_NAME_SPECIFIER",
721     "PURGED"
722   };
723   
724   /* If we have a name for the token, print it out.  Otherwise, we
725      simply give the numeric code.  */
726   gcc_assert (token->type < ARRAY_SIZE(token_names));
727   fputs (token_names[token->type], stream);
728
729   /* For some tokens, print the associated data.  */
730   switch (token->type)
731     {
732     case CPP_KEYWORD:
733       /* Some keywords have a value that is not an IDENTIFIER_NODE.
734          For example, `struct' is mapped to an INTEGER_CST.  */
735       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
736         break;
737       /* else fall through */
738     case CPP_NAME:
739       fputs (IDENTIFIER_POINTER (token->value), stream);
740       break;
741
742     case CPP_STRING:
743     case CPP_WSTRING:
744     case CPP_PRAGMA:
745       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
746       break;
747
748     default:
749       break;
750     }
751 }
752
753 /* Start emitting debugging information.  */
754
755 static void
756 cp_lexer_start_debugging (cp_lexer* lexer)
757 {
758   ++lexer->debugging_p;
759 }
760
761 /* Stop emitting debugging information.  */
762
763 static void
764 cp_lexer_stop_debugging (cp_lexer* lexer)
765 {
766   --lexer->debugging_p;
767 }
768
769 #endif /* ENABLE_CHECKING */
770
771 /* Create a new cp_token_cache, representing a range of tokens. */
772
773 static cp_token_cache *
774 cp_token_cache_new (cp_token *first, cp_token *last)
775 {
776   cp_token_cache *cache = GGC_NEW (cp_token_cache);
777   cache->first = first;
778   cache->last = last;
779   return cache;
780 }
781
782 \f
783 /* Decl-specifiers.  */
784
785 static void clear_decl_specs
786   (cp_decl_specifier_seq *);
787
788 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
789
790 static void
791 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
792 {
793   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
794 }
795
796 /* Declarators.  */
797
798 /* Nothing other than the parser should be creating declarators;
799    declarators are a semi-syntactic representation of C++ entities.
800    Other parts of the front end that need to create entities (like
801    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
802
803 static cp_declarator *make_id_declarator
804   (tree);
805 static cp_declarator *make_call_declarator
806   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
807 static cp_declarator *make_array_declarator
808   (cp_declarator *, tree);
809 static cp_declarator *make_pointer_declarator
810   (cp_cv_quals, cp_declarator *);
811 static cp_declarator *make_reference_declarator
812   (cp_cv_quals, cp_declarator *);
813 static cp_parameter_declarator *make_parameter_declarator
814   (cp_decl_specifier_seq *, cp_declarator *, tree);
815 static cp_declarator *make_ptrmem_declarator
816   (cp_cv_quals, tree, cp_declarator *);
817
818 cp_declarator *cp_error_declarator;
819
820 /* The obstack on which declarators and related data structures are
821    allocated.  */
822 static struct obstack declarator_obstack;
823
824 /* Alloc BYTES from the declarator memory pool.  */
825
826 static inline void *
827 alloc_declarator (size_t bytes)
828 {
829   return obstack_alloc (&declarator_obstack, bytes);
830 }
831
832 /* Allocate a declarator of the indicated KIND.  Clear fields that are
833    common to all declarators.  */
834
835 static cp_declarator *
836 make_declarator (cp_declarator_kind kind)
837 {
838   cp_declarator *declarator;
839
840   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
841   declarator->kind = kind;
842   declarator->attributes = NULL_TREE;
843   declarator->declarator = NULL;
844
845   return declarator;
846 }
847
848 /* Make a declarator for a generalized identifier.  */
849
850 cp_declarator *
851 make_id_declarator (tree id)
852 {
853   cp_declarator *declarator;
854
855   declarator = make_declarator (cdk_id);
856   declarator->u.id.name = id;
857   declarator->u.id.sfk = sfk_none;
858
859   return declarator;
860 }
861
862 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
863    of modifiers such as const or volatile to apply to the pointer
864    type, represented as identifiers.  */
865
866 cp_declarator *
867 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
868 {
869   cp_declarator *declarator;
870
871   declarator = make_declarator (cdk_pointer);
872   declarator->declarator = target;
873   declarator->u.pointer.qualifiers = cv_qualifiers;
874   declarator->u.pointer.class_type = NULL_TREE;
875
876   return declarator;
877 }
878
879 /* Like make_pointer_declarator -- but for references.  */
880
881 cp_declarator *
882 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
883 {
884   cp_declarator *declarator;
885
886   declarator = make_declarator (cdk_reference);
887   declarator->declarator = target;
888   declarator->u.pointer.qualifiers = cv_qualifiers;
889   declarator->u.pointer.class_type = NULL_TREE;
890
891   return declarator;
892 }
893
894 /* Like make_pointer_declarator -- but for a pointer to a non-static
895    member of CLASS_TYPE.  */
896
897 cp_declarator *
898 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
899                         cp_declarator *pointee)
900 {
901   cp_declarator *declarator;
902
903   declarator = make_declarator (cdk_ptrmem);
904   declarator->declarator = pointee;
905   declarator->u.pointer.qualifiers = cv_qualifiers;
906   declarator->u.pointer.class_type = class_type;
907
908   return declarator;
909 }
910
911 /* Make a declarator for the function given by TARGET, with the
912    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
913    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
914    indicates what exceptions can be thrown.  */
915
916 cp_declarator *
917 make_call_declarator (cp_declarator *target,
918                       cp_parameter_declarator *parms,
919                       cp_cv_quals cv_qualifiers,
920                       tree exception_specification)
921 {
922   cp_declarator *declarator;
923
924   declarator = make_declarator (cdk_function);
925   declarator->declarator = target;
926   declarator->u.function.parameters = parms;
927   declarator->u.function.qualifiers = cv_qualifiers;
928   declarator->u.function.exception_specification = exception_specification;
929
930   return declarator;
931 }
932
933 /* Make a declarator for an array of BOUNDS elements, each of which is
934    defined by ELEMENT.  */
935
936 cp_declarator *
937 make_array_declarator (cp_declarator *element, tree bounds)
938 {
939   cp_declarator *declarator;
940
941   declarator = make_declarator (cdk_array);
942   declarator->declarator = element;
943   declarator->u.array.bounds = bounds;
944
945   return declarator;
946 }
947
948 cp_parameter_declarator *no_parameters;
949
950 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
951    DECLARATOR and DEFAULT_ARGUMENT.  */
952
953 cp_parameter_declarator *
954 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
955                            cp_declarator *declarator,
956                            tree default_argument)
957 {
958   cp_parameter_declarator *parameter;
959
960   parameter = ((cp_parameter_declarator *)
961                alloc_declarator (sizeof (cp_parameter_declarator)));
962   parameter->next = NULL;
963   if (decl_specifiers)
964     parameter->decl_specifiers = *decl_specifiers;
965   else
966     clear_decl_specs (&parameter->decl_specifiers);
967   parameter->declarator = declarator;
968   parameter->default_argument = default_argument;
969   parameter->ellipsis_p = false;
970
971   return parameter;
972 }
973
974 /* The parser.  */
975
976 /* Overview
977    --------
978
979    A cp_parser parses the token stream as specified by the C++
980    grammar.  Its job is purely parsing, not semantic analysis.  For
981    example, the parser breaks the token stream into declarators,
982    expressions, statements, and other similar syntactic constructs.
983    It does not check that the types of the expressions on either side
984    of an assignment-statement are compatible, or that a function is
985    not declared with a parameter of type `void'.
986
987    The parser invokes routines elsewhere in the compiler to perform
988    semantic analysis and to build up the abstract syntax tree for the
989    code processed.
990
991    The parser (and the template instantiation code, which is, in a
992    way, a close relative of parsing) are the only parts of the
993    compiler that should be calling push_scope and pop_scope, or
994    related functions.  The parser (and template instantiation code)
995    keeps track of what scope is presently active; everything else
996    should simply honor that.  (The code that generates static
997    initializers may also need to set the scope, in order to check
998    access control correctly when emitting the initializers.)
999
1000    Methodology
1001    -----------
1002
1003    The parser is of the standard recursive-descent variety.  Upcoming
1004    tokens in the token stream are examined in order to determine which
1005    production to use when parsing a non-terminal.  Some C++ constructs
1006    require arbitrary look ahead to disambiguate.  For example, it is
1007    impossible, in the general case, to tell whether a statement is an
1008    expression or declaration without scanning the entire statement.
1009    Therefore, the parser is capable of "parsing tentatively."  When the
1010    parser is not sure what construct comes next, it enters this mode.
1011    Then, while we attempt to parse the construct, the parser queues up
1012    error messages, rather than issuing them immediately, and saves the
1013    tokens it consumes.  If the construct is parsed successfully, the
1014    parser "commits", i.e., it issues any queued error messages and
1015    the tokens that were being preserved are permanently discarded.
1016    If, however, the construct is not parsed successfully, the parser
1017    rolls back its state completely so that it can resume parsing using
1018    a different alternative.
1019
1020    Future Improvements
1021    -------------------
1022
1023    The performance of the parser could probably be improved substantially.
1024    We could often eliminate the need to parse tentatively by looking ahead
1025    a little bit.  In some places, this approach might not entirely eliminate
1026    the need to parse tentatively, but it might still speed up the average
1027    case.  */
1028
1029 /* Flags that are passed to some parsing functions.  These values can
1030    be bitwise-ored together.  */
1031
1032 typedef enum cp_parser_flags
1033 {
1034   /* No flags.  */
1035   CP_PARSER_FLAGS_NONE = 0x0,
1036   /* The construct is optional.  If it is not present, then no error
1037      should be issued.  */
1038   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1039   /* When parsing a type-specifier, do not allow user-defined types.  */
1040   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1041 } cp_parser_flags;
1042
1043 /* The different kinds of declarators we want to parse.  */
1044
1045 typedef enum cp_parser_declarator_kind
1046 {
1047   /* We want an abstract declarator.  */
1048   CP_PARSER_DECLARATOR_ABSTRACT,
1049   /* We want a named declarator.  */
1050   CP_PARSER_DECLARATOR_NAMED,
1051   /* We don't mind, but the name must be an unqualified-id.  */
1052   CP_PARSER_DECLARATOR_EITHER
1053 } cp_parser_declarator_kind;
1054
1055 /* The precedence values used to parse binary expressions.  The minimum value
1056    of PREC must be 1, because zero is reserved to quickly discriminate
1057    binary operators from other tokens.  */
1058
1059 enum cp_parser_prec
1060 {
1061   PREC_NOT_OPERATOR,
1062   PREC_LOGICAL_OR_EXPRESSION,
1063   PREC_LOGICAL_AND_EXPRESSION,
1064   PREC_INCLUSIVE_OR_EXPRESSION,
1065   PREC_EXCLUSIVE_OR_EXPRESSION,
1066   PREC_AND_EXPRESSION,
1067   PREC_RELATIONAL_EXPRESSION,
1068   PREC_EQUALITY_EXPRESSION,
1069   PREC_SHIFT_EXPRESSION,
1070   PREC_ADDITIVE_EXPRESSION,
1071   PREC_MULTIPLICATIVE_EXPRESSION,
1072   PREC_PM_EXPRESSION,
1073   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1074 };
1075
1076 /* A mapping from a token type to a corresponding tree node type, with a
1077    precedence value.  */
1078
1079 typedef struct cp_parser_binary_operations_map_node
1080 {
1081   /* The token type.  */
1082   enum cpp_ttype token_type;
1083   /* The corresponding tree code.  */
1084   enum tree_code tree_type;
1085   /* The precedence of this operator.  */
1086   enum cp_parser_prec prec;
1087 } cp_parser_binary_operations_map_node;
1088
1089 /* The status of a tentative parse.  */
1090
1091 typedef enum cp_parser_status_kind
1092 {
1093   /* No errors have occurred.  */
1094   CP_PARSER_STATUS_KIND_NO_ERROR,
1095   /* An error has occurred.  */
1096   CP_PARSER_STATUS_KIND_ERROR,
1097   /* We are committed to this tentative parse, whether or not an error
1098      has occurred.  */
1099   CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind;
1101
1102 typedef struct cp_parser_expression_stack_entry
1103 {
1104   tree lhs;
1105   enum tree_code tree_type;
1106   int prec;
1107 } cp_parser_expression_stack_entry;
1108
1109 typedef struct cp_parser_expression_stack_entry
1110   cp_parser_expression_stack[NUM_PREC_VALUES];
1111
1112 /* Context that is saved and restored when parsing tentatively.  */
1113 typedef struct cp_parser_context GTY (())
1114 {
1115   /* If this is a tentative parsing context, the status of the
1116      tentative parse.  */
1117   enum cp_parser_status_kind status;
1118   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1119      that are looked up in this context must be looked up both in the
1120      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1121      the context of the containing expression.  */
1122   tree object_type;
1123
1124   /* The next parsing context in the stack.  */
1125   struct cp_parser_context *next;
1126 } cp_parser_context;
1127
1128 /* Prototypes.  */
1129
1130 /* Constructors and destructors.  */
1131
1132 static cp_parser_context *cp_parser_context_new
1133   (cp_parser_context *);
1134
1135 /* Class variables.  */
1136
1137 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1138
1139 /* The operator-precedence table used by cp_parser_binary_expression.
1140    Transformed into an associative array (binops_by_token) by
1141    cp_parser_new.  */
1142
1143 static const cp_parser_binary_operations_map_node binops[] = {
1144   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1145   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1146
1147   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1148   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1149   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1150
1151   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1152   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1153
1154   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1155   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1156
1157   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1158   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1159   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1160   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1161   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1162   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1163
1164   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1165   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1166
1167   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1168
1169   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1170
1171   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1172
1173   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1174
1175   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1176 };
1177
1178 /* The same as binops, but initialized by cp_parser_new so that
1179    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1180    for speed.  */
1181 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1182
1183 /* Constructors and destructors.  */
1184
1185 /* Construct a new context.  The context below this one on the stack
1186    is given by NEXT.  */
1187
1188 static cp_parser_context *
1189 cp_parser_context_new (cp_parser_context* next)
1190 {
1191   cp_parser_context *context;
1192
1193   /* Allocate the storage.  */
1194   if (cp_parser_context_free_list != NULL)
1195     {
1196       /* Pull the first entry from the free list.  */
1197       context = cp_parser_context_free_list;
1198       cp_parser_context_free_list = context->next;
1199       memset (context, 0, sizeof (*context));
1200     }
1201   else
1202     context = GGC_CNEW (cp_parser_context);
1203
1204   /* No errors have occurred yet in this context.  */
1205   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1206   /* If this is not the bottomost context, copy information that we
1207      need from the previous context.  */
1208   if (next)
1209     {
1210       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1211          expression, then we are parsing one in this context, too.  */
1212       context->object_type = next->object_type;
1213       /* Thread the stack.  */
1214       context->next = next;
1215     }
1216
1217   return context;
1218 }
1219
1220 /* The cp_parser structure represents the C++ parser.  */
1221
1222 typedef struct cp_parser GTY(())
1223 {
1224   /* The lexer from which we are obtaining tokens.  */
1225   cp_lexer *lexer;
1226
1227   /* The scope in which names should be looked up.  If NULL_TREE, then
1228      we look up names in the scope that is currently open in the
1229      source program.  If non-NULL, this is either a TYPE or
1230      NAMESPACE_DECL for the scope in which we should look.
1231
1232      This value is not cleared automatically after a name is looked
1233      up, so we must be careful to clear it before starting a new look
1234      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1235      will look up `Z' in the scope of `X', rather than the current
1236      scope.)  Unfortunately, it is difficult to tell when name lookup
1237      is complete, because we sometimes peek at a token, look it up,
1238      and then decide not to consume it.  */
1239   tree scope;
1240
1241   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1242      last lookup took place.  OBJECT_SCOPE is used if an expression
1243      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1244      respectively.  QUALIFYING_SCOPE is used for an expression of the
1245      form "X::Y"; it refers to X.  */
1246   tree object_scope;
1247   tree qualifying_scope;
1248
1249   /* A stack of parsing contexts.  All but the bottom entry on the
1250      stack will be tentative contexts.
1251
1252      We parse tentatively in order to determine which construct is in
1253      use in some situations.  For example, in order to determine
1254      whether a statement is an expression-statement or a
1255      declaration-statement we parse it tentatively as a
1256      declaration-statement.  If that fails, we then reparse the same
1257      token stream as an expression-statement.  */
1258   cp_parser_context *context;
1259
1260   /* True if we are parsing GNU C++.  If this flag is not set, then
1261      GNU extensions are not recognized.  */
1262   bool allow_gnu_extensions_p;
1263
1264   /* TRUE if the `>' token should be interpreted as the greater-than
1265      operator.  FALSE if it is the end of a template-id or
1266      template-parameter-list.  */
1267   bool greater_than_is_operator_p;
1268
1269   /* TRUE if default arguments are allowed within a parameter list
1270      that starts at this point. FALSE if only a gnu extension makes
1271      them permissible.  */
1272   bool default_arg_ok_p;
1273
1274   /* TRUE if we are parsing an integral constant-expression.  See
1275      [expr.const] for a precise definition.  */
1276   bool integral_constant_expression_p;
1277
1278   /* TRUE if we are parsing an integral constant-expression -- but a
1279      non-constant expression should be permitted as well.  This flag
1280      is used when parsing an array bound so that GNU variable-length
1281      arrays are tolerated.  */
1282   bool allow_non_integral_constant_expression_p;
1283
1284   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1285      been seen that makes the expression non-constant.  */
1286   bool non_integral_constant_expression_p;
1287
1288   /* TRUE if local variable names and `this' are forbidden in the
1289      current context.  */
1290   bool local_variables_forbidden_p;
1291
1292   /* TRUE if the declaration we are parsing is part of a
1293      linkage-specification of the form `extern string-literal
1294      declaration'.  */
1295   bool in_unbraced_linkage_specification_p;
1296
1297   /* TRUE if we are presently parsing a declarator, after the
1298      direct-declarator.  */
1299   bool in_declarator_p;
1300
1301   /* TRUE if we are presently parsing a template-argument-list.  */
1302   bool in_template_argument_list_p;
1303
1304   /* TRUE if we are presently parsing the body of an
1305      iteration-statement.  */
1306   bool in_iteration_statement_p;
1307
1308   /* TRUE if we are presently parsing the body of a switch
1309      statement.  */
1310   bool in_switch_statement_p;
1311
1312   /* TRUE if we are parsing a type-id in an expression context.  In
1313      such a situation, both "type (expr)" and "type (type)" are valid
1314      alternatives.  */
1315   bool in_type_id_in_expr_p;
1316
1317   /* TRUE if strings in expressions should be translated to the execution
1318      character set.  */
1319   bool translate_strings_p;
1320
1321   /* If non-NULL, then we are parsing a construct where new type
1322      definitions are not permitted.  The string stored here will be
1323      issued as an error message if a type is defined.  */
1324   const char *type_definition_forbidden_message;
1325
1326   /* A list of lists. The outer list is a stack, used for member
1327      functions of local classes. At each level there are two sub-list,
1328      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1329      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1330      TREE_VALUE's. The functions are chained in reverse declaration
1331      order.
1332
1333      The TREE_PURPOSE sublist contains those functions with default
1334      arguments that need post processing, and the TREE_VALUE sublist
1335      contains those functions with definitions that need post
1336      processing.
1337
1338      These lists can only be processed once the outermost class being
1339      defined is complete.  */
1340   tree unparsed_functions_queues;
1341
1342   /* The number of classes whose definitions are currently in
1343      progress.  */
1344   unsigned num_classes_being_defined;
1345
1346   /* The number of template parameter lists that apply directly to the
1347      current declaration.  */
1348   unsigned num_template_parameter_lists;
1349 } cp_parser;
1350
1351 /* The type of a function that parses some kind of expression.  */
1352 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1353
1354 /* Prototypes.  */
1355
1356 /* Constructors and destructors.  */
1357
1358 static cp_parser *cp_parser_new
1359   (void);
1360
1361 /* Routines to parse various constructs.
1362
1363    Those that return `tree' will return the error_mark_node (rather
1364    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1365    Sometimes, they will return an ordinary node if error-recovery was
1366    attempted, even though a parse error occurred.  So, to check
1367    whether or not a parse error occurred, you should always use
1368    cp_parser_error_occurred.  If the construct is optional (indicated
1369    either by an `_opt' in the name of the function that does the
1370    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1371    the construct is not present.  */
1372
1373 /* Lexical conventions [gram.lex]  */
1374
1375 static tree cp_parser_identifier
1376   (cp_parser *);
1377 static tree cp_parser_string_literal
1378   (cp_parser *, bool, bool);
1379
1380 /* Basic concepts [gram.basic]  */
1381
1382 static bool cp_parser_translation_unit
1383   (cp_parser *);
1384
1385 /* Expressions [gram.expr]  */
1386
1387 static tree cp_parser_primary_expression
1388   (cp_parser *, cp_id_kind *, tree *);
1389 static tree cp_parser_id_expression
1390   (cp_parser *, bool, bool, bool *, bool);
1391 static tree cp_parser_unqualified_id
1392   (cp_parser *, bool, bool, bool);
1393 static tree cp_parser_nested_name_specifier_opt
1394   (cp_parser *, bool, bool, bool, bool);
1395 static tree cp_parser_nested_name_specifier
1396   (cp_parser *, bool, bool, bool, bool);
1397 static tree cp_parser_class_or_namespace_name
1398   (cp_parser *, bool, bool, bool, bool, bool);
1399 static tree cp_parser_postfix_expression
1400   (cp_parser *, bool);
1401 static tree cp_parser_postfix_open_square_expression
1402   (cp_parser *, tree, bool);
1403 static tree cp_parser_postfix_dot_deref_expression
1404   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1405 static tree cp_parser_parenthesized_expression_list
1406   (cp_parser *, bool, bool *);
1407 static void cp_parser_pseudo_destructor_name
1408   (cp_parser *, tree *, tree *);
1409 static tree cp_parser_unary_expression
1410   (cp_parser *, bool);
1411 static enum tree_code cp_parser_unary_operator
1412   (cp_token *);
1413 static tree cp_parser_new_expression
1414   (cp_parser *);
1415 static tree cp_parser_new_placement
1416   (cp_parser *);
1417 static tree cp_parser_new_type_id
1418   (cp_parser *, tree *);
1419 static cp_declarator *cp_parser_new_declarator_opt
1420   (cp_parser *);
1421 static cp_declarator *cp_parser_direct_new_declarator
1422   (cp_parser *);
1423 static tree cp_parser_new_initializer
1424   (cp_parser *);
1425 static tree cp_parser_delete_expression
1426   (cp_parser *);
1427 static tree cp_parser_cast_expression
1428   (cp_parser *, bool);
1429 static tree cp_parser_binary_expression
1430   (cp_parser *);
1431 static tree cp_parser_question_colon_clause
1432   (cp_parser *, tree);
1433 static tree cp_parser_assignment_expression
1434   (cp_parser *);
1435 static enum tree_code cp_parser_assignment_operator_opt
1436   (cp_parser *);
1437 static tree cp_parser_expression
1438   (cp_parser *);
1439 static tree cp_parser_constant_expression
1440   (cp_parser *, bool, bool *);
1441 static tree cp_parser_builtin_offsetof
1442   (cp_parser *);
1443
1444 /* Statements [gram.stmt.stmt]  */
1445
1446 static void cp_parser_statement
1447   (cp_parser *, tree);
1448 static tree cp_parser_labeled_statement
1449   (cp_parser *, tree);
1450 static tree cp_parser_expression_statement
1451   (cp_parser *, tree);
1452 static tree cp_parser_compound_statement
1453   (cp_parser *, tree, bool);
1454 static void cp_parser_statement_seq_opt
1455   (cp_parser *, tree);
1456 static tree cp_parser_selection_statement
1457   (cp_parser *);
1458 static tree cp_parser_condition
1459   (cp_parser *);
1460 static tree cp_parser_iteration_statement
1461   (cp_parser *);
1462 static void cp_parser_for_init_statement
1463   (cp_parser *);
1464 static tree cp_parser_jump_statement
1465   (cp_parser *);
1466 static void cp_parser_declaration_statement
1467   (cp_parser *);
1468
1469 static tree cp_parser_implicitly_scoped_statement
1470   (cp_parser *);
1471 static void cp_parser_already_scoped_statement
1472   (cp_parser *);
1473
1474 /* Declarations [gram.dcl.dcl] */
1475
1476 static void cp_parser_declaration_seq_opt
1477   (cp_parser *);
1478 static void cp_parser_declaration
1479   (cp_parser *);
1480 static void cp_parser_block_declaration
1481   (cp_parser *, bool);
1482 static void cp_parser_simple_declaration
1483   (cp_parser *, bool);
1484 static void cp_parser_decl_specifier_seq
1485   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1486 static tree cp_parser_storage_class_specifier_opt
1487   (cp_parser *);
1488 static tree cp_parser_function_specifier_opt
1489   (cp_parser *, cp_decl_specifier_seq *);
1490 static tree cp_parser_type_specifier
1491   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1492    int *, bool *);
1493 static tree cp_parser_simple_type_specifier
1494   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1495 static tree cp_parser_type_name
1496   (cp_parser *);
1497 static tree cp_parser_elaborated_type_specifier
1498   (cp_parser *, bool, bool);
1499 static tree cp_parser_enum_specifier
1500   (cp_parser *);
1501 static void cp_parser_enumerator_list
1502   (cp_parser *, tree);
1503 static void cp_parser_enumerator_definition
1504   (cp_parser *, tree);
1505 static tree cp_parser_namespace_name
1506   (cp_parser *);
1507 static void cp_parser_namespace_definition
1508   (cp_parser *);
1509 static void cp_parser_namespace_body
1510   (cp_parser *);
1511 static tree cp_parser_qualified_namespace_specifier
1512   (cp_parser *);
1513 static void cp_parser_namespace_alias_definition
1514   (cp_parser *);
1515 static void cp_parser_using_declaration
1516   (cp_parser *);
1517 static void cp_parser_using_directive
1518   (cp_parser *);
1519 static void cp_parser_asm_definition
1520   (cp_parser *);
1521 static void cp_parser_linkage_specification
1522   (cp_parser *);
1523
1524 /* Declarators [gram.dcl.decl] */
1525
1526 static tree cp_parser_init_declarator
1527   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1528 static cp_declarator *cp_parser_declarator
1529   (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1530 static cp_declarator *cp_parser_direct_declarator
1531   (cp_parser *, cp_parser_declarator_kind, int *);
1532 static enum tree_code cp_parser_ptr_operator
1533   (cp_parser *, tree *, cp_cv_quals *);
1534 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1535   (cp_parser *);
1536 static tree cp_parser_declarator_id
1537   (cp_parser *);
1538 static tree cp_parser_type_id
1539   (cp_parser *);
1540 static void cp_parser_type_specifier_seq
1541   (cp_parser *, cp_decl_specifier_seq *);
1542 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1543   (cp_parser *);
1544 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1545   (cp_parser *, bool *);
1546 static cp_parameter_declarator *cp_parser_parameter_declaration
1547   (cp_parser *, bool, bool *);
1548 static void cp_parser_function_body
1549   (cp_parser *);
1550 static tree cp_parser_initializer
1551   (cp_parser *, bool *, bool *);
1552 static tree cp_parser_initializer_clause
1553   (cp_parser *, bool *);
1554 static tree cp_parser_initializer_list
1555   (cp_parser *, bool *);
1556
1557 static bool cp_parser_ctor_initializer_opt_and_function_body
1558   (cp_parser *);
1559
1560 /* Classes [gram.class] */
1561
1562 static tree cp_parser_class_name
1563   (cp_parser *, bool, bool, bool, bool, bool, bool);
1564 static tree cp_parser_class_specifier
1565   (cp_parser *);
1566 static tree cp_parser_class_head
1567   (cp_parser *, bool *, tree *);
1568 static enum tag_types cp_parser_class_key
1569   (cp_parser *);
1570 static void cp_parser_member_specification_opt
1571   (cp_parser *);
1572 static void cp_parser_member_declaration
1573   (cp_parser *);
1574 static tree cp_parser_pure_specifier
1575   (cp_parser *);
1576 static tree cp_parser_constant_initializer
1577   (cp_parser *);
1578
1579 /* Derived classes [gram.class.derived] */
1580
1581 static tree cp_parser_base_clause
1582   (cp_parser *);
1583 static tree cp_parser_base_specifier
1584   (cp_parser *);
1585
1586 /* Special member functions [gram.special] */
1587
1588 static tree cp_parser_conversion_function_id
1589   (cp_parser *);
1590 static tree cp_parser_conversion_type_id
1591   (cp_parser *);
1592 static cp_declarator *cp_parser_conversion_declarator_opt
1593   (cp_parser *);
1594 static bool cp_parser_ctor_initializer_opt
1595   (cp_parser *);
1596 static void cp_parser_mem_initializer_list
1597   (cp_parser *);
1598 static tree cp_parser_mem_initializer
1599   (cp_parser *);
1600 static tree cp_parser_mem_initializer_id
1601   (cp_parser *);
1602
1603 /* Overloading [gram.over] */
1604
1605 static tree cp_parser_operator_function_id
1606   (cp_parser *);
1607 static tree cp_parser_operator
1608   (cp_parser *);
1609
1610 /* Templates [gram.temp] */
1611
1612 static void cp_parser_template_declaration
1613   (cp_parser *, bool);
1614 static tree cp_parser_template_parameter_list
1615   (cp_parser *);
1616 static tree cp_parser_template_parameter
1617   (cp_parser *, bool *);
1618 static tree cp_parser_type_parameter
1619   (cp_parser *);
1620 static tree cp_parser_template_id
1621   (cp_parser *, bool, bool, bool);
1622 static tree cp_parser_template_name
1623   (cp_parser *, bool, bool, bool, bool *);
1624 static tree cp_parser_template_argument_list
1625   (cp_parser *);
1626 static tree cp_parser_template_argument
1627   (cp_parser *);
1628 static void cp_parser_explicit_instantiation
1629   (cp_parser *);
1630 static void cp_parser_explicit_specialization
1631   (cp_parser *);
1632
1633 /* Exception handling [gram.exception] */
1634
1635 static tree cp_parser_try_block
1636   (cp_parser *);
1637 static bool cp_parser_function_try_block
1638   (cp_parser *);
1639 static void cp_parser_handler_seq
1640   (cp_parser *);
1641 static void cp_parser_handler
1642   (cp_parser *);
1643 static tree cp_parser_exception_declaration
1644   (cp_parser *);
1645 static tree cp_parser_throw_expression
1646   (cp_parser *);
1647 static tree cp_parser_exception_specification_opt
1648   (cp_parser *);
1649 static tree cp_parser_type_id_list
1650   (cp_parser *);
1651
1652 /* GNU Extensions */
1653
1654 static tree cp_parser_asm_specification_opt
1655   (cp_parser *);
1656 static tree cp_parser_asm_operand_list
1657   (cp_parser *);
1658 static tree cp_parser_asm_clobber_list
1659   (cp_parser *);
1660 static tree cp_parser_attributes_opt
1661   (cp_parser *);
1662 static tree cp_parser_attribute_list
1663   (cp_parser *);
1664 static bool cp_parser_extension_opt
1665   (cp_parser *, int *);
1666 static void cp_parser_label_declaration
1667   (cp_parser *);
1668
1669 /* Utility Routines */
1670
1671 static tree cp_parser_lookup_name
1672   (cp_parser *, tree, bool, bool, bool, bool, bool *);
1673 static tree cp_parser_lookup_name_simple
1674   (cp_parser *, tree);
1675 static tree cp_parser_maybe_treat_template_as_class
1676   (tree, bool);
1677 static bool cp_parser_check_declarator_template_parameters
1678   (cp_parser *, cp_declarator *);
1679 static bool cp_parser_check_template_parameters
1680   (cp_parser *, unsigned);
1681 static tree cp_parser_simple_cast_expression
1682   (cp_parser *);
1683 static tree cp_parser_global_scope_opt
1684   (cp_parser *, bool);
1685 static bool cp_parser_constructor_declarator_p
1686   (cp_parser *, bool);
1687 static tree cp_parser_function_definition_from_specifiers_and_declarator
1688   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1689 static tree cp_parser_function_definition_after_declarator
1690   (cp_parser *, bool);
1691 static void cp_parser_template_declaration_after_export
1692   (cp_parser *, bool);
1693 static tree cp_parser_single_declaration
1694   (cp_parser *, bool, bool *);
1695 static tree cp_parser_functional_cast
1696   (cp_parser *, tree);
1697 static tree cp_parser_save_member_function_body
1698   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1699 static tree cp_parser_enclosed_template_argument_list
1700   (cp_parser *);
1701 static void cp_parser_save_default_args
1702   (cp_parser *, tree);
1703 static void cp_parser_late_parsing_for_member
1704   (cp_parser *, tree);
1705 static void cp_parser_late_parsing_default_args
1706   (cp_parser *, tree);
1707 static tree cp_parser_sizeof_operand
1708   (cp_parser *, enum rid);
1709 static bool cp_parser_declares_only_class_p
1710   (cp_parser *);
1711 static void cp_parser_set_storage_class
1712   (cp_decl_specifier_seq *, cp_storage_class);
1713 static void cp_parser_set_decl_spec_type
1714   (cp_decl_specifier_seq *, tree, bool);
1715 static bool cp_parser_friend_p
1716   (const cp_decl_specifier_seq *);
1717 static cp_token *cp_parser_require
1718   (cp_parser *, enum cpp_ttype, const char *);
1719 static cp_token *cp_parser_require_keyword
1720   (cp_parser *, enum rid, const char *);
1721 static bool cp_parser_token_starts_function_definition_p
1722   (cp_token *);
1723 static bool cp_parser_next_token_starts_class_definition_p
1724   (cp_parser *);
1725 static bool cp_parser_next_token_ends_template_argument_p
1726   (cp_parser *);
1727 static bool cp_parser_nth_token_starts_template_argument_list_p
1728   (cp_parser *, size_t);
1729 static enum tag_types cp_parser_token_is_class_key
1730   (cp_token *);
1731 static void cp_parser_check_class_key
1732   (enum tag_types, tree type);
1733 static void cp_parser_check_access_in_redeclaration
1734   (tree type);
1735 static bool cp_parser_optional_template_keyword
1736   (cp_parser *);
1737 static void cp_parser_pre_parsed_nested_name_specifier
1738   (cp_parser *);
1739 static void cp_parser_cache_group
1740   (cp_parser *, enum cpp_ttype, unsigned);
1741 static void cp_parser_parse_tentatively
1742   (cp_parser *);
1743 static void cp_parser_commit_to_tentative_parse
1744   (cp_parser *);
1745 static void cp_parser_abort_tentative_parse
1746   (cp_parser *);
1747 static bool cp_parser_parse_definitely
1748   (cp_parser *);
1749 static inline bool cp_parser_parsing_tentatively
1750   (cp_parser *);
1751 static bool cp_parser_committed_to_tentative_parse
1752   (cp_parser *);
1753 static void cp_parser_error
1754   (cp_parser *, const char *);
1755 static void cp_parser_name_lookup_error
1756   (cp_parser *, tree, tree, const char *);
1757 static bool cp_parser_simulate_error
1758   (cp_parser *);
1759 static void cp_parser_check_type_definition
1760   (cp_parser *);
1761 static void cp_parser_check_for_definition_in_return_type
1762   (cp_declarator *, int);
1763 static void cp_parser_check_for_invalid_template_id
1764   (cp_parser *, tree);
1765 static bool cp_parser_non_integral_constant_expression
1766   (cp_parser *, const char *);
1767 static void cp_parser_diagnose_invalid_type_name
1768   (cp_parser *, tree, tree);
1769 static bool cp_parser_parse_and_diagnose_invalid_type_name
1770   (cp_parser *);
1771 static int cp_parser_skip_to_closing_parenthesis
1772   (cp_parser *, bool, bool, bool);
1773 static void cp_parser_skip_to_end_of_statement
1774   (cp_parser *);
1775 static void cp_parser_consume_semicolon_at_end_of_statement
1776   (cp_parser *);
1777 static void cp_parser_skip_to_end_of_block_or_statement
1778   (cp_parser *);
1779 static void cp_parser_skip_to_closing_brace
1780   (cp_parser *);
1781 static void cp_parser_skip_until_found
1782   (cp_parser *, enum cpp_ttype, const char *);
1783 static bool cp_parser_error_occurred
1784   (cp_parser *);
1785 static bool cp_parser_allow_gnu_extensions_p
1786   (cp_parser *);
1787 static bool cp_parser_is_string_literal
1788   (cp_token *);
1789 static bool cp_parser_is_keyword
1790   (cp_token *, enum rid);
1791 static tree cp_parser_make_typename_type
1792   (cp_parser *, tree, tree);
1793
1794 /* Returns nonzero if we are parsing tentatively.  */
1795
1796 static inline bool
1797 cp_parser_parsing_tentatively (cp_parser* parser)
1798 {
1799   return parser->context->next != NULL;
1800 }
1801
1802 /* Returns nonzero if TOKEN is a string literal.  */
1803
1804 static bool
1805 cp_parser_is_string_literal (cp_token* token)
1806 {
1807   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1808 }
1809
1810 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1811
1812 static bool
1813 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1814 {
1815   return token->keyword == keyword;
1816 }
1817
1818 /* If not parsing tentatively, issue a diagnostic of the form
1819       FILE:LINE: MESSAGE before TOKEN
1820    where TOKEN is the next token in the input stream.  MESSAGE
1821    (specified by the caller) is usually of the form "expected
1822    OTHER-TOKEN".  */
1823
1824 static void
1825 cp_parser_error (cp_parser* parser, const char* message)
1826 {
1827   if (!cp_parser_simulate_error (parser))
1828     {
1829       cp_token *token = cp_lexer_peek_token (parser->lexer);
1830       /* This diagnostic makes more sense if it is tagged to the line
1831          of the token we just peeked at.  */
1832       cp_lexer_set_source_position_from_token (token);
1833       c_parse_error (message,
1834                      /* Because c_parser_error does not understand
1835                         CPP_KEYWORD, keywords are treated like
1836                         identifiers.  */
1837                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1838                      token->value);
1839     }
1840 }
1841
1842 /* Issue an error about name-lookup failing.  NAME is the
1843    IDENTIFIER_NODE DECL is the result of
1844    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1845    the thing that we hoped to find.  */
1846
1847 static void
1848 cp_parser_name_lookup_error (cp_parser* parser,
1849                              tree name,
1850                              tree decl,
1851                              const char* desired)
1852 {
1853   /* If name lookup completely failed, tell the user that NAME was not
1854      declared.  */
1855   if (decl == error_mark_node)
1856     {
1857       if (parser->scope && parser->scope != global_namespace)
1858         error ("`%D::%D' has not been declared",
1859                parser->scope, name);
1860       else if (parser->scope == global_namespace)
1861         error ("`::%D' has not been declared", name);
1862       else if (parser->object_scope 
1863                && !CLASS_TYPE_P (parser->object_scope))
1864         error ("request for member `%D' in non-class type `%T'",
1865                name, parser->object_scope);
1866       else if (parser->object_scope)
1867         error ("`%T::%D' has not been declared", 
1868                parser->object_scope, name);
1869       else
1870         error ("`%D' has not been declared", name);
1871     }
1872   else if (parser->scope && parser->scope != global_namespace)
1873     error ("`%D::%D' %s", parser->scope, name, desired);
1874   else if (parser->scope == global_namespace)
1875     error ("`::%D' %s", name, desired);
1876   else
1877     error ("`%D' %s", name, desired);
1878 }
1879
1880 /* If we are parsing tentatively, remember that an error has occurred
1881    during this tentative parse.  Returns true if the error was
1882    simulated; false if a message should be issued by the caller.  */
1883
1884 static bool
1885 cp_parser_simulate_error (cp_parser* parser)
1886 {
1887   if (cp_parser_parsing_tentatively (parser)
1888       && !cp_parser_committed_to_tentative_parse (parser))
1889     {
1890       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1891       return true;
1892     }
1893   return false;
1894 }
1895
1896 /* This function is called when a type is defined.  If type
1897    definitions are forbidden at this point, an error message is
1898    issued.  */
1899
1900 static void
1901 cp_parser_check_type_definition (cp_parser* parser)
1902 {
1903   /* If types are forbidden here, issue a message.  */
1904   if (parser->type_definition_forbidden_message)
1905     /* Use `%s' to print the string in case there are any escape
1906        characters in the message.  */
1907     error ("%s", parser->type_definition_forbidden_message);
1908 }
1909
1910 /* This function is called when a declaration is parsed.  If
1911    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1912    indicates that a type was defined in the decl-specifiers for DECL,
1913    then an error is issued.  */
1914
1915 static void
1916 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1917                                                int declares_class_or_enum)
1918 {
1919   /* [dcl.fct] forbids type definitions in return types.
1920      Unfortunately, it's not easy to know whether or not we are
1921      processing a return type until after the fact.  */
1922   while (declarator
1923          && (declarator->kind == cdk_pointer
1924              || declarator->kind == cdk_reference
1925              || declarator->kind == cdk_ptrmem))
1926     declarator = declarator->declarator;
1927   if (declarator
1928       && declarator->kind == cdk_function
1929       && declares_class_or_enum & 2)
1930     error ("new types may not be defined in a return type");
1931 }
1932
1933 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1934    "<" in any valid C++ program.  If the next token is indeed "<",
1935    issue a message warning the user about what appears to be an
1936    invalid attempt to form a template-id.  */
1937
1938 static void
1939 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1940                                          tree type)
1941 {
1942   ptrdiff_t start;
1943   cp_token *token;
1944
1945   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1946     {
1947       if (TYPE_P (type))
1948         error ("`%T' is not a template", type);
1949       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1950         error ("`%E' is not a template", type);
1951       else
1952         error ("invalid template-id");
1953       /* Remember the location of the invalid "<".  */
1954       if (cp_parser_parsing_tentatively (parser)
1955           && !cp_parser_committed_to_tentative_parse (parser))
1956         {
1957           token = cp_lexer_peek_token (parser->lexer);
1958           token = cp_lexer_prev_token (parser->lexer, token);
1959           start = cp_lexer_token_difference (parser->lexer,
1960                                              parser->lexer->buffer,
1961                                              token);
1962         }
1963       else
1964         start = -1;
1965       /* Consume the "<".  */
1966       cp_lexer_consume_token (parser->lexer);
1967       /* Parse the template arguments.  */
1968       cp_parser_enclosed_template_argument_list (parser);
1969       /* Permanently remove the invalid template arguments so that
1970          this error message is not issued again.  */
1971       if (start >= 0)
1972         {
1973           token = cp_lexer_advance_token (parser->lexer,
1974                                           parser->lexer->buffer,
1975                                           start);
1976           cp_lexer_purge_tokens_after (parser->lexer, token);
1977         }
1978     }
1979 }
1980
1981 /* If parsing an integral constant-expression, issue an error message
1982    about the fact that THING appeared and return true.  Otherwise,
1983    return false, marking the current expression as non-constant.  */
1984
1985 static bool
1986 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1987                                             const char *thing)
1988 {
1989   if (parser->integral_constant_expression_p)
1990     {
1991       if (!parser->allow_non_integral_constant_expression_p)
1992         {
1993           error ("%s cannot appear in a constant-expression", thing);
1994           return true;
1995         }
1996       parser->non_integral_constant_expression_p = true;
1997     }
1998   return false;
1999 }
2000
2001 /* Emit a diagnostic for an invalid type name. Consider also if it is
2002    qualified or not and the result of a lookup, to provide a better
2003    message.  */
2004
2005 static void
2006 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2007 {
2008   tree decl, old_scope;
2009   /* Try to lookup the identifier.  */
2010   old_scope = parser->scope;
2011   parser->scope = scope;
2012   decl = cp_parser_lookup_name_simple (parser, id);
2013   parser->scope = old_scope;
2014   /* If the lookup found a template-name, it means that the user forgot
2015   to specify an argument list. Emit an useful error message.  */
2016   if (TREE_CODE (decl) == TEMPLATE_DECL)
2017     error ("invalid use of template-name `%E' without an argument list",
2018       decl);
2019   else if (!parser->scope)
2020     {
2021       /* Issue an error message.  */
2022       error ("`%E' does not name a type", id);
2023       /* If we're in a template class, it's possible that the user was
2024          referring to a type from a base class.  For example:
2025
2026            template <typename T> struct A { typedef T X; };
2027            template <typename T> struct B : public A<T> { X x; };
2028
2029          The user should have said "typename A<T>::X".  */
2030       if (processing_template_decl && current_class_type)
2031         {
2032           tree b;
2033
2034           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2035                b;
2036                b = TREE_CHAIN (b))
2037             {
2038               tree base_type = BINFO_TYPE (b);
2039               if (CLASS_TYPE_P (base_type)
2040                   && dependent_type_p (base_type))
2041                 {
2042                   tree field;
2043                   /* Go from a particular instantiation of the
2044                      template (which will have an empty TYPE_FIELDs),
2045                      to the main version.  */
2046                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2047                   for (field = TYPE_FIELDS (base_type);
2048                        field;
2049                        field = TREE_CHAIN (field))
2050                     if (TREE_CODE (field) == TYPE_DECL
2051                         && DECL_NAME (field) == id)
2052                       {
2053                         inform ("(perhaps `typename %T::%E' was intended)",
2054                                 BINFO_TYPE (b), id);
2055                         break;
2056                       }
2057                   if (field)
2058                     break;
2059                 }
2060             }
2061         }
2062     }
2063   /* Here we diagnose qualified-ids where the scope is actually correct,
2064      but the identifier does not resolve to a valid type name.  */
2065   else
2066     {
2067       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2068         error ("`%E' in namespace `%E' does not name a type",
2069                id, parser->scope);
2070       else if (TYPE_P (parser->scope))
2071         error ("`%E' in class `%T' does not name a type",
2072                id, parser->scope);
2073       else
2074         gcc_unreachable ();
2075     }
2076 }
2077
2078 /* Check for a common situation where a type-name should be present,
2079    but is not, and issue a sensible error message.  Returns true if an
2080    invalid type-name was detected.
2081
2082    The situation handled by this function are variable declarations of the
2083    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2084    Usually, `ID' should name a type, but if we got here it means that it
2085    does not. We try to emit the best possible error message depending on
2086    how exactly the id-expression looks like.
2087 */
2088
2089 static bool
2090 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2091 {
2092   tree id;
2093
2094   cp_parser_parse_tentatively (parser);
2095   id = cp_parser_id_expression (parser,
2096                                 /*template_keyword_p=*/false,
2097                                 /*check_dependency_p=*/true,
2098                                 /*template_p=*/NULL,
2099                                 /*declarator_p=*/true);
2100   /* After the id-expression, there should be a plain identifier,
2101      otherwise this is not a simple variable declaration. Also, if
2102      the scope is dependent, we cannot do much.  */
2103   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2104       || (parser->scope && TYPE_P (parser->scope)
2105           && dependent_type_p (parser->scope)))
2106     {
2107       cp_parser_abort_tentative_parse (parser);
2108       return false;
2109     }
2110   if (!cp_parser_parse_definitely (parser)
2111       || TREE_CODE (id) != IDENTIFIER_NODE)
2112     return false;
2113
2114   /* Emit a diagnostic for the invalid type.  */
2115   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2116   /* Skip to the end of the declaration; there's no point in
2117      trying to process it.  */
2118   cp_parser_skip_to_end_of_block_or_statement (parser);
2119   return true;
2120 }
2121
2122 /* Consume tokens up to, and including, the next non-nested closing `)'.
2123    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2124    are doing error recovery. Returns -1 if OR_COMMA is true and we
2125    found an unnested comma.  */
2126
2127 static int
2128 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2129                                        bool recovering,
2130                                        bool or_comma,
2131                                        bool consume_paren)
2132 {
2133   unsigned paren_depth = 0;
2134   unsigned brace_depth = 0;
2135   int result;
2136
2137   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2138       && !cp_parser_committed_to_tentative_parse (parser))
2139     return 0;
2140
2141   while (true)
2142     {
2143       cp_token *token;
2144
2145       /* If we've run out of tokens, then there is no closing `)'.  */
2146       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2147         {
2148           result = 0;
2149           break;
2150         }
2151
2152       token = cp_lexer_peek_token (parser->lexer);
2153
2154       /* This matches the processing in skip_to_end_of_statement.  */
2155       if (token->type == CPP_SEMICOLON && !brace_depth)
2156         {
2157           result = 0;
2158           break;
2159         }
2160       if (token->type == CPP_OPEN_BRACE)
2161         ++brace_depth;
2162       if (token->type == CPP_CLOSE_BRACE)
2163         {
2164           if (!brace_depth--)
2165             {
2166               result = 0;
2167               break;
2168             }
2169         }
2170       if (recovering && or_comma && token->type == CPP_COMMA
2171           && !brace_depth && !paren_depth)
2172         {
2173           result = -1;
2174           break;
2175         }
2176
2177       if (!brace_depth)
2178         {
2179           /* If it is an `(', we have entered another level of nesting.  */
2180           if (token->type == CPP_OPEN_PAREN)
2181             ++paren_depth;
2182           /* If it is a `)', then we might be done.  */
2183           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2184             {
2185               if (consume_paren)
2186                 cp_lexer_consume_token (parser->lexer);
2187               {
2188                 result = 1;
2189                 break;
2190               }
2191             }
2192         }
2193
2194       /* Consume the token.  */
2195       cp_lexer_consume_token (parser->lexer);
2196     }
2197
2198   return result;
2199 }
2200
2201 /* Consume tokens until we reach the end of the current statement.
2202    Normally, that will be just before consuming a `;'.  However, if a
2203    non-nested `}' comes first, then we stop before consuming that.  */
2204
2205 static void
2206 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2207 {
2208   unsigned nesting_depth = 0;
2209
2210   while (true)
2211     {
2212       cp_token *token;
2213
2214       /* Peek at the next token.  */
2215       token = cp_lexer_peek_token (parser->lexer);
2216       /* If we've run out of tokens, stop.  */
2217       if (token->type == CPP_EOF)
2218         break;
2219       /* If the next token is a `;', we have reached the end of the
2220          statement.  */
2221       if (token->type == CPP_SEMICOLON && !nesting_depth)
2222         break;
2223       /* If the next token is a non-nested `}', then we have reached
2224          the end of the current block.  */
2225       if (token->type == CPP_CLOSE_BRACE)
2226         {
2227           /* If this is a non-nested `}', stop before consuming it.
2228              That way, when confronted with something like:
2229
2230                { 3 + }
2231
2232              we stop before consuming the closing `}', even though we
2233              have not yet reached a `;'.  */
2234           if (nesting_depth == 0)
2235             break;
2236           /* If it is the closing `}' for a block that we have
2237              scanned, stop -- but only after consuming the token.
2238              That way given:
2239
2240                 void f g () { ... }
2241                 typedef int I;
2242
2243              we will stop after the body of the erroneously declared
2244              function, but before consuming the following `typedef'
2245              declaration.  */
2246           if (--nesting_depth == 0)
2247             {
2248               cp_lexer_consume_token (parser->lexer);
2249               break;
2250             }
2251         }
2252       /* If it the next token is a `{', then we are entering a new
2253          block.  Consume the entire block.  */
2254       else if (token->type == CPP_OPEN_BRACE)
2255         ++nesting_depth;
2256       /* Consume the token.  */
2257       cp_lexer_consume_token (parser->lexer);
2258     }
2259 }
2260
2261 /* This function is called at the end of a statement or declaration.
2262    If the next token is a semicolon, it is consumed; otherwise, error
2263    recovery is attempted.  */
2264
2265 static void
2266 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2267 {
2268   /* Look for the trailing `;'.  */
2269   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2270     {
2271       /* If there is additional (erroneous) input, skip to the end of
2272          the statement.  */
2273       cp_parser_skip_to_end_of_statement (parser);
2274       /* If the next token is now a `;', consume it.  */
2275       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2276         cp_lexer_consume_token (parser->lexer);
2277     }
2278 }
2279
2280 /* Skip tokens until we have consumed an entire block, or until we
2281    have consumed a non-nested `;'.  */
2282
2283 static void
2284 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2285 {
2286   unsigned nesting_depth = 0;
2287
2288   while (true)
2289     {
2290       cp_token *token;
2291
2292       /* Peek at the next token.  */
2293       token = cp_lexer_peek_token (parser->lexer);
2294       /* If we've run out of tokens, stop.  */
2295       if (token->type == CPP_EOF)
2296         break;
2297       /* If the next token is a `;', we have reached the end of the
2298          statement.  */
2299       if (token->type == CPP_SEMICOLON && !nesting_depth)
2300         {
2301           /* Consume the `;'.  */
2302           cp_lexer_consume_token (parser->lexer);
2303           break;
2304         }
2305       /* Consume the token.  */
2306       token = cp_lexer_consume_token (parser->lexer);
2307       /* If the next token is a non-nested `}', then we have reached
2308          the end of the current block.  */
2309       if (token->type == CPP_CLOSE_BRACE
2310           && (nesting_depth == 0 || --nesting_depth == 0))
2311         break;
2312       /* If it the next token is a `{', then we are entering a new
2313          block.  Consume the entire block.  */
2314       if (token->type == CPP_OPEN_BRACE)
2315         ++nesting_depth;
2316     }
2317 }
2318
2319 /* Skip tokens until a non-nested closing curly brace is the next
2320    token.  */
2321
2322 static void
2323 cp_parser_skip_to_closing_brace (cp_parser *parser)
2324 {
2325   unsigned nesting_depth = 0;
2326
2327   while (true)
2328     {
2329       cp_token *token;
2330
2331       /* Peek at the next token.  */
2332       token = cp_lexer_peek_token (parser->lexer);
2333       /* If we've run out of tokens, stop.  */
2334       if (token->type == CPP_EOF)
2335         break;
2336       /* If the next token is a non-nested `}', then we have reached
2337          the end of the current block.  */
2338       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2339         break;
2340       /* If it the next token is a `{', then we are entering a new
2341          block.  Consume the entire block.  */
2342       else if (token->type == CPP_OPEN_BRACE)
2343         ++nesting_depth;
2344       /* Consume the token.  */
2345       cp_lexer_consume_token (parser->lexer);
2346     }
2347 }
2348
2349 /* This is a simple wrapper around make_typename_type. When the id is
2350    an unresolved identifier node, we can provide a superior diagnostic
2351    using cp_parser_diagnose_invalid_type_name.  */
2352
2353 static tree
2354 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2355 {
2356   tree result;
2357   if (TREE_CODE (id) == IDENTIFIER_NODE)
2358     {
2359       result = make_typename_type (scope, id, /*complain=*/0);
2360       if (result == error_mark_node)
2361         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2362       return result;
2363     }
2364   return make_typename_type (scope, id, tf_error);
2365 }
2366
2367
2368 /* Create a new C++ parser.  */
2369
2370 static cp_parser *
2371 cp_parser_new (void)
2372 {
2373   cp_parser *parser;
2374   cp_lexer *lexer;
2375   unsigned i;
2376
2377   /* cp_lexer_new_main is called before calling ggc_alloc because
2378      cp_lexer_new_main might load a PCH file.  */
2379   lexer = cp_lexer_new_main ();
2380
2381   /* Initialize the binops_by_token so that we can get the tree
2382      directly from the token.  */
2383   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2384     binops_by_token[binops[i].token_type] = binops[i];
2385
2386   parser = GGC_CNEW (cp_parser);
2387   parser->lexer = lexer;
2388   parser->context = cp_parser_context_new (NULL);
2389
2390   /* For now, we always accept GNU extensions.  */
2391   parser->allow_gnu_extensions_p = 1;
2392
2393   /* The `>' token is a greater-than operator, not the end of a
2394      template-id.  */
2395   parser->greater_than_is_operator_p = true;
2396
2397   parser->default_arg_ok_p = true;
2398
2399   /* We are not parsing a constant-expression.  */
2400   parser->integral_constant_expression_p = false;
2401   parser->allow_non_integral_constant_expression_p = false;
2402   parser->non_integral_constant_expression_p = false;
2403
2404   /* Local variable names are not forbidden.  */
2405   parser->local_variables_forbidden_p = false;
2406
2407   /* We are not processing an `extern "C"' declaration.  */
2408   parser->in_unbraced_linkage_specification_p = false;
2409
2410   /* We are not processing a declarator.  */
2411   parser->in_declarator_p = false;
2412
2413   /* We are not processing a template-argument-list.  */
2414   parser->in_template_argument_list_p = false;
2415
2416   /* We are not in an iteration statement.  */
2417   parser->in_iteration_statement_p = false;
2418
2419   /* We are not in a switch statement.  */
2420   parser->in_switch_statement_p = false;
2421
2422   /* We are not parsing a type-id inside an expression.  */
2423   parser->in_type_id_in_expr_p = false;
2424
2425   /* String literals should be translated to the execution character set.  */
2426   parser->translate_strings_p = true;
2427
2428   /* The unparsed function queue is empty.  */
2429   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2430
2431   /* There are no classes being defined.  */
2432   parser->num_classes_being_defined = 0;
2433
2434   /* No template parameters apply.  */
2435   parser->num_template_parameter_lists = 0;
2436
2437   return parser;
2438 }
2439
2440 /* Create a cp_lexer structure which will emit the tokens in CACHE
2441    and push it onto the parser's lexer stack.  This is used for delayed
2442    parsing of in-class method bodies and default arguments, and should
2443    not be confused with tentative parsing.  */
2444 static void
2445 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2446 {
2447   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2448   lexer->next = parser->lexer;
2449   parser->lexer = lexer;
2450
2451   /* Move the current source position to that of the first token in the
2452      new lexer.  */
2453   cp_lexer_set_source_position_from_token (lexer->next_token);
2454 }
2455
2456 /* Pop the top lexer off the parser stack.  This is never used for the
2457    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2458 static void
2459 cp_parser_pop_lexer (cp_parser *parser)
2460 {
2461   cp_lexer *lexer = parser->lexer;
2462   parser->lexer = lexer->next;
2463   cp_lexer_destroy (lexer);
2464
2465   /* Put the current source position back where it was before this
2466      lexer was pushed.  */
2467   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2468 }
2469
2470 /* Lexical conventions [gram.lex]  */
2471
2472 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2473    identifier.  */
2474
2475 static tree
2476 cp_parser_identifier (cp_parser* parser)
2477 {
2478   cp_token *token;
2479
2480   /* Look for the identifier.  */
2481   token = cp_parser_require (parser, CPP_NAME, "identifier");
2482   /* Return the value.  */
2483   return token ? token->value : error_mark_node;
2484 }
2485
2486 /* Parse a sequence of adjacent string constants.  Returns a
2487    TREE_STRING representing the combined, nul-terminated string
2488    constant.  If TRANSLATE is true, translate the string to the
2489    execution character set.  If WIDE_OK is true, a wide string is
2490    invalid here.
2491
2492    C++98 [lex.string] says that if a narrow string literal token is
2493    adjacent to a wide string literal token, the behavior is undefined.
2494    However, C99 6.4.5p4 says that this results in a wide string literal.
2495    We follow C99 here, for consistency with the C front end.
2496
2497    This code is largely lifted from lex_string() in c-lex.c.
2498
2499    FUTURE: ObjC++ will need to handle @-strings here.  */
2500 static tree
2501 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2502 {
2503   tree value;
2504   bool wide = false;
2505   size_t count;
2506   struct obstack str_ob;
2507   cpp_string str, istr, *strs;
2508   cp_token *tok;
2509
2510   tok = cp_lexer_peek_token (parser->lexer);
2511   if (!cp_parser_is_string_literal (tok))
2512     {
2513       cp_parser_error (parser, "expected string-literal");
2514       return error_mark_node;
2515     }
2516
2517   /* Try to avoid the overhead of creating and destroying an obstack
2518      for the common case of just one string.  */
2519   if (!cp_parser_is_string_literal
2520       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2521     {
2522       cp_lexer_consume_token (parser->lexer);
2523
2524       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2525       str.len = TREE_STRING_LENGTH (tok->value);
2526       count = 1;
2527       if (tok->type == CPP_WSTRING)
2528         wide = true;
2529
2530       strs = &str;
2531     }
2532   else
2533     {
2534       gcc_obstack_init (&str_ob);
2535       count = 0;
2536
2537       do
2538         {
2539           cp_lexer_consume_token (parser->lexer);
2540           count++;
2541           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2542           str.len = TREE_STRING_LENGTH (tok->value);
2543           if (tok->type == CPP_WSTRING)
2544             wide = true;
2545
2546           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2547
2548           tok = cp_lexer_peek_token (parser->lexer);
2549         }
2550       while (cp_parser_is_string_literal (tok));
2551
2552       strs = (cpp_string *) obstack_finish (&str_ob);
2553     }
2554
2555   if (wide && !wide_ok)
2556     {
2557       cp_parser_error (parser, "a wide string is invalid in this context");
2558       wide = false;
2559     }
2560
2561   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2562       (parse_in, strs, count, &istr, wide))
2563     {
2564       value = build_string (istr.len, (char *)istr.text);
2565       free ((void *)istr.text);
2566
2567       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2568       value = fix_string_type (value);
2569     }
2570   else
2571     /* cpp_interpret_string has issued an error.  */
2572     value = error_mark_node;
2573
2574   if (count > 1)
2575     obstack_free (&str_ob, 0);
2576
2577   return value;
2578 }
2579
2580
2581 /* Basic concepts [gram.basic]  */
2582
2583 /* Parse a translation-unit.
2584
2585    translation-unit:
2586      declaration-seq [opt]
2587
2588    Returns TRUE if all went well.  */
2589
2590 static bool
2591 cp_parser_translation_unit (cp_parser* parser)
2592 {
2593   /* The address of the first non-permanent object on the declarator
2594      obstack.  */
2595   static void *declarator_obstack_base;
2596
2597   bool success;
2598
2599   /* Create the declarator obstack, if necessary.  */
2600   if (!cp_error_declarator)
2601     {
2602       gcc_obstack_init (&declarator_obstack);
2603       /* Create the error declarator.  */
2604       cp_error_declarator = make_declarator (cdk_error);
2605       /* Create the empty parameter list.  */
2606       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2607       /* Remember where the base of the declarator obstack lies.  */
2608       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2609     }
2610
2611   while (true)
2612     {
2613       cp_parser_declaration_seq_opt (parser);
2614
2615       /* If there are no tokens left then all went well.  */
2616       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2617         {
2618           /* Consume the EOF token.  */
2619           cp_parser_require (parser, CPP_EOF, "end-of-file");
2620
2621           /* Get rid of the token array; we don't need it any more. */
2622           cp_lexer_destroy (parser->lexer);
2623           parser->lexer = NULL;
2624
2625           /* Finish up.  */
2626           finish_translation_unit ();
2627
2628           success = true;
2629           break;
2630         }
2631       else
2632         {
2633           cp_parser_error (parser, "expected declaration");
2634           success = false;
2635           break;
2636         }
2637     }
2638
2639   /* Make sure the declarator obstack was fully cleaned up.  */
2640   gcc_assert (obstack_next_free (&declarator_obstack)
2641               == declarator_obstack_base);
2642
2643   /* All went well.  */
2644   return success;
2645 }
2646
2647 /* Expressions [gram.expr] */
2648
2649 /* Parse a primary-expression.
2650
2651    primary-expression:
2652      literal
2653      this
2654      ( expression )
2655      id-expression
2656
2657    GNU Extensions:
2658
2659    primary-expression:
2660      ( compound-statement )
2661      __builtin_va_arg ( assignment-expression , type-id )
2662
2663    literal:
2664      __null
2665
2666    Returns a representation of the expression.
2667
2668    *IDK indicates what kind of id-expression (if any) was present.
2669
2670    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2671    used as the operand of a pointer-to-member.  In that case,
2672    *QUALIFYING_CLASS gives the class that is used as the qualifying
2673    class in the pointer-to-member.  */
2674
2675 static tree
2676 cp_parser_primary_expression (cp_parser *parser,
2677                               cp_id_kind *idk,
2678                               tree *qualifying_class)
2679 {
2680   cp_token *token;
2681
2682   /* Assume the primary expression is not an id-expression.  */
2683   *idk = CP_ID_KIND_NONE;
2684   /* And that it cannot be used as pointer-to-member.  */
2685   *qualifying_class = NULL_TREE;
2686
2687   /* Peek at the next token.  */
2688   token = cp_lexer_peek_token (parser->lexer);
2689   switch (token->type)
2690     {
2691       /* literal:
2692            integer-literal
2693            character-literal
2694            floating-literal
2695            string-literal
2696            boolean-literal  */
2697     case CPP_CHAR:
2698     case CPP_WCHAR:
2699     case CPP_NUMBER:
2700       token = cp_lexer_consume_token (parser->lexer);
2701       return token->value;
2702
2703     case CPP_STRING:
2704     case CPP_WSTRING:
2705       /* ??? Should wide strings be allowed when parser->translate_strings_p
2706          is false (i.e. in attributes)?  If not, we can kill the third
2707          argument to cp_parser_string_literal.  */
2708       return cp_parser_string_literal (parser,
2709                                        parser->translate_strings_p,
2710                                        true);
2711
2712     case CPP_OPEN_PAREN:
2713       {
2714         tree expr;
2715         bool saved_greater_than_is_operator_p;
2716
2717         /* Consume the `('.  */
2718         cp_lexer_consume_token (parser->lexer);
2719         /* Within a parenthesized expression, a `>' token is always
2720            the greater-than operator.  */
2721         saved_greater_than_is_operator_p
2722           = parser->greater_than_is_operator_p;
2723         parser->greater_than_is_operator_p = true;
2724         /* If we see `( { ' then we are looking at the beginning of
2725            a GNU statement-expression.  */
2726         if (cp_parser_allow_gnu_extensions_p (parser)
2727             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2728           {
2729             /* Statement-expressions are not allowed by the standard.  */
2730             if (pedantic)
2731               pedwarn ("ISO C++ forbids braced-groups within expressions");
2732
2733             /* And they're not allowed outside of a function-body; you
2734                cannot, for example, write:
2735
2736                  int i = ({ int j = 3; j + 1; });
2737
2738                at class or namespace scope.  */
2739             if (!at_function_scope_p ())
2740               error ("statement-expressions are allowed only inside functions");
2741             /* Start the statement-expression.  */
2742             expr = begin_stmt_expr ();
2743             /* Parse the compound-statement.  */
2744             cp_parser_compound_statement (parser, expr, false);
2745             /* Finish up.  */
2746             expr = finish_stmt_expr (expr, false);
2747           }
2748         else
2749           {
2750             /* Parse the parenthesized expression.  */
2751             expr = cp_parser_expression (parser);
2752             /* Let the front end know that this expression was
2753                enclosed in parentheses. This matters in case, for
2754                example, the expression is of the form `A::B', since
2755                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2756                not.  */
2757             finish_parenthesized_expr (expr);
2758           }
2759         /* The `>' token might be the end of a template-id or
2760            template-parameter-list now.  */
2761         parser->greater_than_is_operator_p
2762           = saved_greater_than_is_operator_p;
2763         /* Consume the `)'.  */
2764         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2765           cp_parser_skip_to_end_of_statement (parser);
2766
2767         return expr;
2768       }
2769
2770     case CPP_KEYWORD:
2771       switch (token->keyword)
2772         {
2773           /* These two are the boolean literals.  */
2774         case RID_TRUE:
2775           cp_lexer_consume_token (parser->lexer);
2776           return boolean_true_node;
2777         case RID_FALSE:
2778           cp_lexer_consume_token (parser->lexer);
2779           return boolean_false_node;
2780
2781           /* The `__null' literal.  */
2782         case RID_NULL:
2783           cp_lexer_consume_token (parser->lexer);
2784           return null_node;
2785
2786           /* Recognize the `this' keyword.  */
2787         case RID_THIS:
2788           cp_lexer_consume_token (parser->lexer);
2789           if (parser->local_variables_forbidden_p)
2790             {
2791               error ("`this' may not be used in this context");
2792               return error_mark_node;
2793             }
2794           /* Pointers cannot appear in constant-expressions.  */
2795           if (cp_parser_non_integral_constant_expression (parser,
2796                                                           "`this'"))
2797             return error_mark_node;
2798           return finish_this_expr ();
2799
2800           /* The `operator' keyword can be the beginning of an
2801              id-expression.  */
2802         case RID_OPERATOR:
2803           goto id_expression;
2804
2805         case RID_FUNCTION_NAME:
2806         case RID_PRETTY_FUNCTION_NAME:
2807         case RID_C99_FUNCTION_NAME:
2808           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2809              __func__ are the names of variables -- but they are
2810              treated specially.  Therefore, they are handled here,
2811              rather than relying on the generic id-expression logic
2812              below.  Grammatically, these names are id-expressions.
2813
2814              Consume the token.  */
2815           token = cp_lexer_consume_token (parser->lexer);
2816           /* Look up the name.  */
2817           return finish_fname (token->value);
2818
2819         case RID_VA_ARG:
2820           {
2821             tree expression;
2822             tree type;
2823
2824             /* The `__builtin_va_arg' construct is used to handle
2825                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2826             cp_lexer_consume_token (parser->lexer);
2827             /* Look for the opening `('.  */
2828             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2829             /* Now, parse the assignment-expression.  */
2830             expression = cp_parser_assignment_expression (parser);
2831             /* Look for the `,'.  */
2832             cp_parser_require (parser, CPP_COMMA, "`,'");
2833             /* Parse the type-id.  */
2834             type = cp_parser_type_id (parser);
2835             /* Look for the closing `)'.  */
2836             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2837             /* Using `va_arg' in a constant-expression is not
2838                allowed.  */
2839             if (cp_parser_non_integral_constant_expression (parser,
2840                                                             "`va_arg'"))
2841               return error_mark_node;
2842             return build_x_va_arg (expression, type);
2843           }
2844
2845         case RID_OFFSETOF:
2846           return cp_parser_builtin_offsetof (parser);
2847
2848         default:
2849           cp_parser_error (parser, "expected primary-expression");
2850           return error_mark_node;
2851         }
2852
2853       /* An id-expression can start with either an identifier, a
2854          `::' as the beginning of a qualified-id, or the "operator"
2855          keyword.  */
2856     case CPP_NAME:
2857     case CPP_SCOPE:
2858     case CPP_TEMPLATE_ID:
2859     case CPP_NESTED_NAME_SPECIFIER:
2860       {
2861         tree id_expression;
2862         tree decl;
2863         const char *error_msg;
2864
2865       id_expression:
2866         /* Parse the id-expression.  */
2867         id_expression
2868           = cp_parser_id_expression (parser,
2869                                      /*template_keyword_p=*/false,
2870                                      /*check_dependency_p=*/true,
2871                                      /*template_p=*/NULL,
2872                                      /*declarator_p=*/false);
2873         if (id_expression == error_mark_node)
2874           return error_mark_node;
2875         /* If we have a template-id, then no further lookup is
2876            required.  If the template-id was for a template-class, we
2877            will sometimes have a TYPE_DECL at this point.  */
2878         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2879             || TREE_CODE (id_expression) == TYPE_DECL)
2880           decl = id_expression;
2881         /* Look up the name.  */
2882         else
2883           {
2884             bool ambiguous_p;
2885
2886             decl = cp_parser_lookup_name (parser, id_expression,
2887                                           /*is_type=*/false,
2888                                           /*is_template=*/false,
2889                                           /*is_namespace=*/false,
2890                                           /*check_dependency=*/true,
2891                                           &ambiguous_p);
2892             /* If the lookup was ambiguous, an error will already have
2893                been issued.  */
2894             if (ambiguous_p)
2895               return error_mark_node;
2896             /* If name lookup gives us a SCOPE_REF, then the
2897                qualifying scope was dependent.  Just propagate the
2898                name.  */
2899             if (TREE_CODE (decl) == SCOPE_REF)
2900               {
2901                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2902                   *qualifying_class = TREE_OPERAND (decl, 0);
2903                 return decl;
2904               }
2905             /* Check to see if DECL is a local variable in a context
2906                where that is forbidden.  */
2907             if (parser->local_variables_forbidden_p
2908                 && local_variable_p (decl))
2909               {
2910                 /* It might be that we only found DECL because we are
2911                    trying to be generous with pre-ISO scoping rules.
2912                    For example, consider:
2913
2914                      int i;
2915                      void g() {
2916                        for (int i = 0; i < 10; ++i) {}
2917                        extern void f(int j = i);
2918                      }
2919
2920                    Here, name look up will originally find the out
2921                    of scope `i'.  We need to issue a warning message,
2922                    but then use the global `i'.  */
2923                 decl = check_for_out_of_scope_variable (decl);
2924                 if (local_variable_p (decl))
2925                   {
2926                     error ("local variable `%D' may not appear in this context",
2927                            decl);
2928                     return error_mark_node;
2929                   }
2930               }
2931           }
2932
2933         decl = finish_id_expression (id_expression, decl, parser->scope,
2934                                      idk, qualifying_class,
2935                                      parser->integral_constant_expression_p,
2936                                      parser->allow_non_integral_constant_expression_p,
2937                                      &parser->non_integral_constant_expression_p,
2938                                      &error_msg);
2939         if (error_msg)
2940           cp_parser_error (parser, error_msg);
2941         return decl;
2942       }
2943
2944       /* Anything else is an error.  */
2945     default:
2946       cp_parser_error (parser, "expected primary-expression");
2947       return error_mark_node;
2948     }
2949 }
2950
2951 /* Parse an id-expression.
2952
2953    id-expression:
2954      unqualified-id
2955      qualified-id
2956
2957    qualified-id:
2958      :: [opt] nested-name-specifier template [opt] unqualified-id
2959      :: identifier
2960      :: operator-function-id
2961      :: template-id
2962
2963    Return a representation of the unqualified portion of the
2964    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2965    a `::' or nested-name-specifier.
2966
2967    Often, if the id-expression was a qualified-id, the caller will
2968    want to make a SCOPE_REF to represent the qualified-id.  This
2969    function does not do this in order to avoid wastefully creating
2970    SCOPE_REFs when they are not required.
2971
2972    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2973    `template' keyword.
2974
2975    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2976    uninstantiated templates.
2977
2978    If *TEMPLATE_P is non-NULL, it is set to true iff the
2979    `template' keyword is used to explicitly indicate that the entity
2980    named is a template.
2981
2982    If DECLARATOR_P is true, the id-expression is appearing as part of
2983    a declarator, rather than as part of an expression.  */
2984
2985 static tree
2986 cp_parser_id_expression (cp_parser *parser,
2987                          bool template_keyword_p,
2988                          bool check_dependency_p,
2989                          bool *template_p,
2990                          bool declarator_p)
2991 {
2992   bool global_scope_p;
2993   bool nested_name_specifier_p;
2994
2995   /* Assume the `template' keyword was not used.  */
2996   if (template_p)
2997     *template_p = false;
2998
2999   /* Look for the optional `::' operator.  */
3000   global_scope_p
3001     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3002        != NULL_TREE);
3003   /* Look for the optional nested-name-specifier.  */
3004   nested_name_specifier_p
3005     = (cp_parser_nested_name_specifier_opt (parser,
3006                                             /*typename_keyword_p=*/false,
3007                                             check_dependency_p,
3008                                             /*type_p=*/false,
3009                                             declarator_p)
3010        != NULL_TREE);
3011   /* If there is a nested-name-specifier, then we are looking at
3012      the first qualified-id production.  */
3013   if (nested_name_specifier_p)
3014     {
3015       tree saved_scope;
3016       tree saved_object_scope;
3017       tree saved_qualifying_scope;
3018       tree unqualified_id;
3019       bool is_template;
3020
3021       /* See if the next token is the `template' keyword.  */
3022       if (!template_p)
3023         template_p = &is_template;
3024       *template_p = cp_parser_optional_template_keyword (parser);
3025       /* Name lookup we do during the processing of the
3026          unqualified-id might obliterate SCOPE.  */
3027       saved_scope = parser->scope;
3028       saved_object_scope = parser->object_scope;
3029       saved_qualifying_scope = parser->qualifying_scope;
3030       /* Process the final unqualified-id.  */
3031       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3032                                                  check_dependency_p,
3033                                                  declarator_p);
3034       /* Restore the SAVED_SCOPE for our caller.  */
3035       parser->scope = saved_scope;
3036       parser->object_scope = saved_object_scope;
3037       parser->qualifying_scope = saved_qualifying_scope;
3038
3039       return unqualified_id;
3040     }
3041   /* Otherwise, if we are in global scope, then we are looking at one
3042      of the other qualified-id productions.  */
3043   else if (global_scope_p)
3044     {
3045       cp_token *token;
3046       tree id;
3047
3048       /* Peek at the next token.  */
3049       token = cp_lexer_peek_token (parser->lexer);
3050
3051       /* If it's an identifier, and the next token is not a "<", then
3052          we can avoid the template-id case.  This is an optimization
3053          for this common case.  */
3054       if (token->type == CPP_NAME
3055           && !cp_parser_nth_token_starts_template_argument_list_p
3056                (parser, 2))
3057         return cp_parser_identifier (parser);
3058
3059       cp_parser_parse_tentatively (parser);
3060       /* Try a template-id.  */
3061       id = cp_parser_template_id (parser,
3062                                   /*template_keyword_p=*/false,
3063                                   /*check_dependency_p=*/true,
3064                                   declarator_p);
3065       /* If that worked, we're done.  */
3066       if (cp_parser_parse_definitely (parser))
3067         return id;
3068
3069       /* Peek at the next token.  (Changes in the token buffer may
3070          have invalidated the pointer obtained above.)  */
3071       token = cp_lexer_peek_token (parser->lexer);
3072
3073       switch (token->type)
3074         {
3075         case CPP_NAME:
3076           return cp_parser_identifier (parser);
3077
3078         case CPP_KEYWORD:
3079           if (token->keyword == RID_OPERATOR)
3080             return cp_parser_operator_function_id (parser);
3081           /* Fall through.  */
3082
3083         default:
3084           cp_parser_error (parser, "expected id-expression");
3085           return error_mark_node;
3086         }
3087     }
3088   else
3089     return cp_parser_unqualified_id (parser, template_keyword_p,
3090                                      /*check_dependency_p=*/true,
3091                                      declarator_p);
3092 }
3093
3094 /* Parse an unqualified-id.
3095
3096    unqualified-id:
3097      identifier
3098      operator-function-id
3099      conversion-function-id
3100      ~ class-name
3101      template-id
3102
3103    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3104    keyword, in a construct like `A::template ...'.
3105
3106    Returns a representation of unqualified-id.  For the `identifier'
3107    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3108    production a BIT_NOT_EXPR is returned; the operand of the
3109    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3110    other productions, see the documentation accompanying the
3111    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3112    names are looked up in uninstantiated templates.  If DECLARATOR_P
3113    is true, the unqualified-id is appearing as part of a declarator,
3114    rather than as part of an expression.  */
3115
3116 static tree
3117 cp_parser_unqualified_id (cp_parser* parser,
3118                           bool template_keyword_p,
3119                           bool check_dependency_p,
3120                           bool declarator_p)
3121 {
3122   cp_token *token;
3123
3124   /* Peek at the next token.  */
3125   token = cp_lexer_peek_token (parser->lexer);
3126
3127   switch (token->type)
3128     {
3129     case CPP_NAME:
3130       {
3131         tree id;
3132
3133         /* We don't know yet whether or not this will be a
3134            template-id.  */
3135         cp_parser_parse_tentatively (parser);
3136         /* Try a template-id.  */
3137         id = cp_parser_template_id (parser, template_keyword_p,
3138                                     check_dependency_p,
3139                                     declarator_p);
3140         /* If it worked, we're done.  */
3141         if (cp_parser_parse_definitely (parser))
3142           return id;
3143         /* Otherwise, it's an ordinary identifier.  */
3144         return cp_parser_identifier (parser);
3145       }
3146
3147     case CPP_TEMPLATE_ID:
3148       return cp_parser_template_id (parser, template_keyword_p,
3149                                     check_dependency_p,
3150                                     declarator_p);
3151
3152     case CPP_COMPL:
3153       {
3154         tree type_decl;
3155         tree qualifying_scope;
3156         tree object_scope;
3157         tree scope;
3158
3159         /* Consume the `~' token.  */
3160         cp_lexer_consume_token (parser->lexer);
3161         /* Parse the class-name.  The standard, as written, seems to
3162            say that:
3163
3164              template <typename T> struct S { ~S (); };
3165              template <typename T> S<T>::~S() {}
3166
3167            is invalid, since `~' must be followed by a class-name, but
3168            `S<T>' is dependent, and so not known to be a class.
3169            That's not right; we need to look in uninstantiated
3170            templates.  A further complication arises from:
3171
3172              template <typename T> void f(T t) {
3173                t.T::~T();
3174              }
3175
3176            Here, it is not possible to look up `T' in the scope of `T'
3177            itself.  We must look in both the current scope, and the
3178            scope of the containing complete expression.
3179
3180            Yet another issue is:
3181
3182              struct S {
3183                int S;
3184                ~S();
3185              };
3186
3187              S::~S() {}
3188
3189            The standard does not seem to say that the `S' in `~S'
3190            should refer to the type `S' and not the data member
3191            `S::S'.  */
3192
3193         /* DR 244 says that we look up the name after the "~" in the
3194            same scope as we looked up the qualifying name.  That idea
3195            isn't fully worked out; it's more complicated than that.  */
3196         scope = parser->scope;
3197         object_scope = parser->object_scope;
3198         qualifying_scope = parser->qualifying_scope;
3199
3200         /* If the name is of the form "X::~X" it's OK.  */
3201         if (scope && TYPE_P (scope)
3202             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3203             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3204                 == CPP_OPEN_PAREN)
3205             && (cp_lexer_peek_token (parser->lexer)->value
3206                 == TYPE_IDENTIFIER (scope)))
3207           {
3208             cp_lexer_consume_token (parser->lexer);
3209             return build_nt (BIT_NOT_EXPR, scope);
3210           }
3211
3212         /* If there was an explicit qualification (S::~T), first look
3213            in the scope given by the qualification (i.e., S).  */
3214         if (scope)
3215           {
3216             cp_parser_parse_tentatively (parser);
3217             type_decl = cp_parser_class_name (parser,
3218                                               /*typename_keyword_p=*/false,
3219                                               /*template_keyword_p=*/false,
3220                                               /*type_p=*/false,
3221                                               /*check_dependency=*/false,
3222                                               /*class_head_p=*/false,
3223                                               declarator_p);
3224             if (cp_parser_parse_definitely (parser))
3225               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3226           }
3227         /* In "N::S::~S", look in "N" as well.  */
3228         if (scope && qualifying_scope)
3229           {
3230             cp_parser_parse_tentatively (parser);
3231             parser->scope = qualifying_scope;
3232             parser->object_scope = NULL_TREE;
3233             parser->qualifying_scope = NULL_TREE;
3234             type_decl
3235               = cp_parser_class_name (parser,
3236                                       /*typename_keyword_p=*/false,
3237                                       /*template_keyword_p=*/false,
3238                                       /*type_p=*/false,
3239                                       /*check_dependency=*/false,
3240                                       /*class_head_p=*/false,
3241                                       declarator_p);
3242             if (cp_parser_parse_definitely (parser))
3243               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3244           }
3245         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3246         else if (object_scope)
3247           {
3248             cp_parser_parse_tentatively (parser);
3249             parser->scope = object_scope;
3250             parser->object_scope = NULL_TREE;
3251             parser->qualifying_scope = NULL_TREE;
3252             type_decl
3253               = cp_parser_class_name (parser,
3254                                       /*typename_keyword_p=*/false,
3255                                       /*template_keyword_p=*/false,
3256                                       /*type_p=*/false,
3257                                       /*check_dependency=*/false,
3258                                       /*class_head_p=*/false,
3259                                       declarator_p);
3260             if (cp_parser_parse_definitely (parser))
3261               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3262           }
3263         /* Look in the surrounding context.  */
3264         parser->scope = NULL_TREE;
3265         parser->object_scope = NULL_TREE;
3266         parser->qualifying_scope = NULL_TREE;
3267         type_decl
3268           = cp_parser_class_name (parser,
3269                                   /*typename_keyword_p=*/false,
3270                                   /*template_keyword_p=*/false,
3271                                   /*type_p=*/false,
3272                                   /*check_dependency=*/false,
3273                                   /*class_head_p=*/false,
3274                                   declarator_p);
3275         /* If an error occurred, assume that the name of the
3276            destructor is the same as the name of the qualifying
3277            class.  That allows us to keep parsing after running
3278            into ill-formed destructor names.  */
3279         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3280           return build_nt (BIT_NOT_EXPR, scope);
3281         else if (type_decl == error_mark_node)
3282           return error_mark_node;
3283
3284         /* [class.dtor]
3285
3286            A typedef-name that names a class shall not be used as the
3287            identifier in the declarator for a destructor declaration.  */
3288         if (declarator_p
3289             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3290             && !DECL_SELF_REFERENCE_P (type_decl))
3291           error ("typedef-name `%D' used as destructor declarator",
3292                  type_decl);
3293
3294         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3295       }
3296
3297     case CPP_KEYWORD:
3298       if (token->keyword == RID_OPERATOR)
3299         {
3300           tree id;
3301
3302           /* This could be a template-id, so we try that first.  */
3303           cp_parser_parse_tentatively (parser);
3304           /* Try a template-id.  */
3305           id = cp_parser_template_id (parser, template_keyword_p,
3306                                       /*check_dependency_p=*/true,
3307                                       declarator_p);
3308           /* If that worked, we're done.  */
3309           if (cp_parser_parse_definitely (parser))
3310             return id;
3311           /* We still don't know whether we're looking at an
3312              operator-function-id or a conversion-function-id.  */
3313           cp_parser_parse_tentatively (parser);
3314           /* Try an operator-function-id.  */
3315           id = cp_parser_operator_function_id (parser);
3316           /* If that didn't work, try a conversion-function-id.  */
3317           if (!cp_parser_parse_definitely (parser))
3318             id = cp_parser_conversion_function_id (parser);
3319
3320           return id;
3321         }
3322       /* Fall through.  */
3323
3324     default:
3325       cp_parser_error (parser, "expected unqualified-id");
3326       return error_mark_node;
3327     }
3328 }
3329
3330 /* Parse an (optional) nested-name-specifier.
3331
3332    nested-name-specifier:
3333      class-or-namespace-name :: nested-name-specifier [opt]
3334      class-or-namespace-name :: template nested-name-specifier [opt]
3335
3336    PARSER->SCOPE should be set appropriately before this function is
3337    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3338    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3339    in name lookups.
3340
3341    Sets PARSER->SCOPE to the class (TYPE) or namespace
3342    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3343    it unchanged if there is no nested-name-specifier.  Returns the new
3344    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3345
3346    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3347    part of a declaration and/or decl-specifier.  */
3348
3349 static tree
3350 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3351                                      bool typename_keyword_p,
3352                                      bool check_dependency_p,
3353                                      bool type_p,
3354                                      bool is_declaration)
3355 {
3356   bool success = false;
3357   tree access_check = NULL_TREE;
3358   ptrdiff_t start;
3359   cp_token* token;
3360
3361   /* If the next token corresponds to a nested name specifier, there
3362      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3363      false, it may have been true before, in which case something
3364      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3365      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3366      CHECK_DEPENDENCY_P is false, we have to fall through into the
3367      main loop.  */
3368   if (check_dependency_p
3369       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3370     {
3371       cp_parser_pre_parsed_nested_name_specifier (parser);
3372       return parser->scope;
3373     }
3374
3375   /* Remember where the nested-name-specifier starts.  */
3376   if (cp_parser_parsing_tentatively (parser)
3377       && !cp_parser_committed_to_tentative_parse (parser))
3378     {
3379       token = cp_lexer_peek_token (parser->lexer);
3380       start = cp_lexer_token_difference (parser->lexer,
3381                                          parser->lexer->buffer,
3382                                          token);
3383     }
3384   else
3385     start = -1;
3386
3387   push_deferring_access_checks (dk_deferred);
3388
3389   while (true)
3390     {
3391       tree new_scope;
3392       tree old_scope;
3393       tree saved_qualifying_scope;
3394       bool template_keyword_p;
3395
3396       /* Spot cases that cannot be the beginning of a
3397          nested-name-specifier.  */
3398       token = cp_lexer_peek_token (parser->lexer);
3399
3400       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3401          the already parsed nested-name-specifier.  */
3402       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3403         {
3404           /* Grab the nested-name-specifier and continue the loop.  */
3405           cp_parser_pre_parsed_nested_name_specifier (parser);
3406           success = true;
3407           continue;
3408         }
3409
3410       /* Spot cases that cannot be the beginning of a
3411          nested-name-specifier.  On the second and subsequent times
3412          through the loop, we look for the `template' keyword.  */
3413       if (success && token->keyword == RID_TEMPLATE)
3414         ;
3415       /* A template-id can start a nested-name-specifier.  */
3416       else if (token->type == CPP_TEMPLATE_ID)
3417         ;
3418       else
3419         {
3420           /* If the next token is not an identifier, then it is
3421              definitely not a class-or-namespace-name.  */
3422           if (token->type != CPP_NAME)
3423             break;
3424           /* If the following token is neither a `<' (to begin a
3425              template-id), nor a `::', then we are not looking at a
3426              nested-name-specifier.  */
3427           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3428           if (token->type != CPP_SCOPE
3429               && !cp_parser_nth_token_starts_template_argument_list_p
3430                   (parser, 2))
3431             break;
3432         }
3433
3434       /* The nested-name-specifier is optional, so we parse
3435          tentatively.  */
3436       cp_parser_parse_tentatively (parser);
3437
3438       /* Look for the optional `template' keyword, if this isn't the
3439          first time through the loop.  */
3440       if (success)
3441         template_keyword_p = cp_parser_optional_template_keyword (parser);
3442       else
3443         template_keyword_p = false;
3444
3445       /* Save the old scope since the name lookup we are about to do
3446          might destroy it.  */
3447       old_scope = parser->scope;
3448       saved_qualifying_scope = parser->qualifying_scope;
3449       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3450          look up names in "X<T>::I" in order to determine that "Y" is
3451          a template.  So, if we have a typename at this point, we make
3452          an effort to look through it.  */
3453       if (is_declaration 
3454           && !typename_keyword_p
3455           && parser->scope 
3456           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3457         parser->scope = resolve_typename_type (parser->scope, 
3458                                                /*only_current_p=*/false);
3459       /* Parse the qualifying entity.  */
3460       new_scope
3461         = cp_parser_class_or_namespace_name (parser,
3462                                              typename_keyword_p,
3463                                              template_keyword_p,
3464                                              check_dependency_p,
3465                                              type_p,
3466                                              is_declaration);
3467       /* Look for the `::' token.  */
3468       cp_parser_require (parser, CPP_SCOPE, "`::'");
3469
3470       /* If we found what we wanted, we keep going; otherwise, we're
3471          done.  */
3472       if (!cp_parser_parse_definitely (parser))
3473         {
3474           bool error_p = false;
3475
3476           /* Restore the OLD_SCOPE since it was valid before the
3477              failed attempt at finding the last
3478              class-or-namespace-name.  */
3479           parser->scope = old_scope;
3480           parser->qualifying_scope = saved_qualifying_scope;
3481           /* If the next token is an identifier, and the one after
3482              that is a `::', then any valid interpretation would have
3483              found a class-or-namespace-name.  */
3484           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3485                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3486                      == CPP_SCOPE)
3487                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3488                      != CPP_COMPL))
3489             {
3490               token = cp_lexer_consume_token (parser->lexer);
3491               if (!error_p)
3492                 {
3493                   tree decl;
3494
3495                   decl = cp_parser_lookup_name_simple (parser, token->value);
3496                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3497                     error ("`%D' used without template parameters",
3498                            decl);
3499                   else
3500                     cp_parser_name_lookup_error
3501                       (parser, token->value, decl,
3502                        "is not a class or namespace");
3503                   parser->scope = NULL_TREE;
3504                   error_p = true;
3505                   /* Treat this as a successful nested-name-specifier
3506                      due to:
3507
3508                      [basic.lookup.qual]
3509
3510                      If the name found is not a class-name (clause
3511                      _class_) or namespace-name (_namespace.def_), the
3512                      program is ill-formed.  */
3513                   success = true;
3514                 }
3515               cp_lexer_consume_token (parser->lexer);
3516             }
3517           break;
3518         }
3519
3520       /* We've found one valid nested-name-specifier.  */
3521       success = true;
3522       /* Make sure we look in the right scope the next time through
3523          the loop.  */
3524       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3525                        ? TREE_TYPE (new_scope)
3526                        : new_scope);
3527       /* If it is a class scope, try to complete it; we are about to
3528          be looking up names inside the class.  */
3529       if (TYPE_P (parser->scope)
3530           /* Since checking types for dependency can be expensive,
3531              avoid doing it if the type is already complete.  */
3532           && !COMPLETE_TYPE_P (parser->scope)
3533           /* Do not try to complete dependent types.  */
3534           && !dependent_type_p (parser->scope))
3535         complete_type (parser->scope);
3536     }
3537
3538   /* Retrieve any deferred checks.  Do not pop this access checks yet
3539      so the memory will not be reclaimed during token replacing below.  */
3540   access_check = get_deferred_access_checks ();
3541
3542   /* If parsing tentatively, replace the sequence of tokens that makes
3543      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3544      token.  That way, should we re-parse the token stream, we will
3545      not have to repeat the effort required to do the parse, nor will
3546      we issue duplicate error messages.  */
3547   if (success && start >= 0)
3548     {
3549       /* Find the token that corresponds to the start of the
3550          template-id.  */
3551       token = cp_lexer_advance_token (parser->lexer,
3552                                       parser->lexer->buffer,
3553                                       start);
3554
3555       /* Reset the contents of the START token.  */
3556       token->type = CPP_NESTED_NAME_SPECIFIER;
3557       token->value = build_tree_list (access_check, parser->scope);
3558       TREE_TYPE (token->value) = parser->qualifying_scope;
3559       token->keyword = RID_MAX;
3560       /* Purge all subsequent tokens.  */
3561       cp_lexer_purge_tokens_after (parser->lexer, token);
3562     }
3563
3564   pop_deferring_access_checks ();
3565   return success ? parser->scope : NULL_TREE;
3566 }
3567
3568 /* Parse a nested-name-specifier.  See
3569    cp_parser_nested_name_specifier_opt for details.  This function
3570    behaves identically, except that it will an issue an error if no
3571    nested-name-specifier is present, and it will return
3572    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3573    is present.  */
3574
3575 static tree
3576 cp_parser_nested_name_specifier (cp_parser *parser,
3577                                  bool typename_keyword_p,
3578                                  bool check_dependency_p,
3579                                  bool type_p,
3580                                  bool is_declaration)
3581 {
3582   tree scope;
3583
3584   /* Look for the nested-name-specifier.  */
3585   scope = cp_parser_nested_name_specifier_opt (parser,
3586                                                typename_keyword_p,
3587                                                check_dependency_p,
3588                                                type_p,
3589                                                is_declaration);
3590   /* If it was not present, issue an error message.  */
3591   if (!scope)
3592     {
3593       cp_parser_error (parser, "expected nested-name-specifier");
3594       parser->scope = NULL_TREE;
3595       return error_mark_node;
3596     }
3597
3598   return scope;
3599 }
3600
3601 /* Parse a class-or-namespace-name.
3602
3603    class-or-namespace-name:
3604      class-name
3605      namespace-name
3606
3607    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3608    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3609    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3610    TYPE_P is TRUE iff the next name should be taken as a class-name,
3611    even the same name is declared to be another entity in the same
3612    scope.
3613
3614    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3615    specified by the class-or-namespace-name.  If neither is found the
3616    ERROR_MARK_NODE is returned.  */
3617
3618 static tree
3619 cp_parser_class_or_namespace_name (cp_parser *parser,
3620                                    bool typename_keyword_p,
3621                                    bool template_keyword_p,
3622                                    bool check_dependency_p,
3623                                    bool type_p,
3624                                    bool is_declaration)
3625 {
3626   tree saved_scope;
3627   tree saved_qualifying_scope;
3628   tree saved_object_scope;
3629   tree scope;
3630   bool only_class_p;
3631
3632   /* Before we try to parse the class-name, we must save away the
3633      current PARSER->SCOPE since cp_parser_class_name will destroy
3634      it.  */
3635   saved_scope = parser->scope;
3636   saved_qualifying_scope = parser->qualifying_scope;
3637   saved_object_scope = parser->object_scope;
3638   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3639      there is no need to look for a namespace-name.  */
3640   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3641   if (!only_class_p)
3642     cp_parser_parse_tentatively (parser);
3643   scope = cp_parser_class_name (parser,
3644                                 typename_keyword_p,
3645                                 template_keyword_p,
3646                                 type_p,
3647                                 check_dependency_p,
3648                                 /*class_head_p=*/false,
3649                                 is_declaration);
3650   /* If that didn't work, try for a namespace-name.  */
3651   if (!only_class_p && !cp_parser_parse_definitely (parser))
3652     {
3653       /* Restore the saved scope.  */
3654       parser->scope = saved_scope;
3655       parser->qualifying_scope = saved_qualifying_scope;
3656       parser->object_scope = saved_object_scope;
3657       /* If we are not looking at an identifier followed by the scope
3658          resolution operator, then this is not part of a
3659          nested-name-specifier.  (Note that this function is only used
3660          to parse the components of a nested-name-specifier.)  */
3661       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3662           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3663         return error_mark_node;
3664       scope = cp_parser_namespace_name (parser);
3665     }
3666
3667   return scope;
3668 }
3669
3670 /* Parse a postfix-expression.
3671
3672    postfix-expression:
3673      primary-expression
3674      postfix-expression [ expression ]
3675      postfix-expression ( expression-list [opt] )
3676      simple-type-specifier ( expression-list [opt] )
3677      typename :: [opt] nested-name-specifier identifier
3678        ( expression-list [opt] )
3679      typename :: [opt] nested-name-specifier template [opt] template-id
3680        ( expression-list [opt] )
3681      postfix-expression . template [opt] id-expression
3682      postfix-expression -> template [opt] id-expression
3683      postfix-expression . pseudo-destructor-name
3684      postfix-expression -> pseudo-destructor-name
3685      postfix-expression ++
3686      postfix-expression --
3687      dynamic_cast < type-id > ( expression )
3688      static_cast < type-id > ( expression )
3689      reinterpret_cast < type-id > ( expression )
3690      const_cast < type-id > ( expression )
3691      typeid ( expression )
3692      typeid ( type-id )
3693
3694    GNU Extension:
3695
3696    postfix-expression:
3697      ( type-id ) { initializer-list , [opt] }
3698
3699    This extension is a GNU version of the C99 compound-literal
3700    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3701    but they are essentially the same concept.)
3702
3703    If ADDRESS_P is true, the postfix expression is the operand of the
3704    `&' operator.
3705
3706    Returns a representation of the expression.  */
3707
3708 static tree
3709 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3710 {
3711   cp_token *token;
3712   enum rid keyword;
3713   cp_id_kind idk = CP_ID_KIND_NONE;
3714   tree postfix_expression = NULL_TREE;
3715   /* Non-NULL only if the current postfix-expression can be used to
3716      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3717      class used to qualify the member.  */
3718   tree qualifying_class = NULL_TREE;
3719
3720   /* Peek at the next token.  */
3721   token = cp_lexer_peek_token (parser->lexer);
3722   /* Some of the productions are determined by keywords.  */
3723   keyword = token->keyword;
3724   switch (keyword)
3725     {
3726     case RID_DYNCAST:
3727     case RID_STATCAST:
3728     case RID_REINTCAST:
3729     case RID_CONSTCAST:
3730       {
3731         tree type;
3732         tree expression;
3733         const char *saved_message;
3734
3735         /* All of these can be handled in the same way from the point
3736            of view of parsing.  Begin by consuming the token
3737            identifying the cast.  */
3738         cp_lexer_consume_token (parser->lexer);
3739
3740         /* New types cannot be defined in the cast.  */
3741         saved_message = parser->type_definition_forbidden_message;
3742         parser->type_definition_forbidden_message
3743           = "types may not be defined in casts";
3744
3745         /* Look for the opening `<'.  */
3746         cp_parser_require (parser, CPP_LESS, "`<'");
3747         /* Parse the type to which we are casting.  */
3748         type = cp_parser_type_id (parser);
3749         /* Look for the closing `>'.  */
3750         cp_parser_require (parser, CPP_GREATER, "`>'");
3751         /* Restore the old message.  */
3752         parser->type_definition_forbidden_message = saved_message;
3753
3754         /* And the expression which is being cast.  */
3755         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3756         expression = cp_parser_expression (parser);
3757         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3758
3759         /* Only type conversions to integral or enumeration types
3760            can be used in constant-expressions.  */
3761         if (parser->integral_constant_expression_p
3762             && !dependent_type_p (type)
3763             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3764             && (cp_parser_non_integral_constant_expression
3765                 (parser,
3766                  "a cast to a type other than an integral or "
3767                  "enumeration type")))
3768           return error_mark_node;
3769
3770         switch (keyword)
3771           {
3772           case RID_DYNCAST:
3773             postfix_expression
3774               = build_dynamic_cast (type, expression);
3775             break;
3776           case RID_STATCAST:
3777             postfix_expression
3778               = build_static_cast (type, expression);
3779             break;
3780           case RID_REINTCAST:
3781             postfix_expression
3782               = build_reinterpret_cast (type, expression);
3783             break;
3784           case RID_CONSTCAST:
3785             postfix_expression
3786               = build_const_cast (type, expression);
3787             break;
3788           default:
3789             gcc_unreachable ();
3790           }
3791       }
3792       break;
3793
3794     case RID_TYPEID:
3795       {
3796         tree type;
3797         const char *saved_message;
3798         bool saved_in_type_id_in_expr_p;
3799
3800         /* Consume the `typeid' token.  */
3801         cp_lexer_consume_token (parser->lexer);
3802         /* Look for the `(' token.  */
3803         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3804         /* Types cannot be defined in a `typeid' expression.  */
3805         saved_message = parser->type_definition_forbidden_message;
3806         parser->type_definition_forbidden_message
3807           = "types may not be defined in a `typeid\' expression";
3808         /* We can't be sure yet whether we're looking at a type-id or an
3809            expression.  */
3810         cp_parser_parse_tentatively (parser);
3811         /* Try a type-id first.  */
3812         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3813         parser->in_type_id_in_expr_p = true;
3814         type = cp_parser_type_id (parser);
3815         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3816         /* Look for the `)' token.  Otherwise, we can't be sure that
3817            we're not looking at an expression: consider `typeid (int
3818            (3))', for example.  */
3819         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3820         /* If all went well, simply lookup the type-id.  */
3821         if (cp_parser_parse_definitely (parser))
3822           postfix_expression = get_typeid (type);
3823         /* Otherwise, fall back to the expression variant.  */
3824         else
3825           {
3826             tree expression;
3827
3828             /* Look for an expression.  */
3829             expression = cp_parser_expression (parser);
3830             /* Compute its typeid.  */
3831             postfix_expression = build_typeid (expression);
3832             /* Look for the `)' token.  */
3833             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3834           }
3835         /* `typeid' may not appear in an integral constant expression.  */
3836         if (cp_parser_non_integral_constant_expression(parser,
3837                                                        "`typeid' operator"))
3838           return error_mark_node;
3839         /* Restore the saved message.  */
3840         parser->type_definition_forbidden_message = saved_message;
3841       }
3842       break;
3843
3844     case RID_TYPENAME:
3845       {
3846         bool template_p = false;
3847         tree id;
3848         tree type;
3849
3850         /* Consume the `typename' token.  */
3851         cp_lexer_consume_token (parser->lexer);
3852         /* Look for the optional `::' operator.  */
3853         cp_parser_global_scope_opt (parser,
3854                                     /*current_scope_valid_p=*/false);
3855         /* Look for the nested-name-specifier.  */
3856         cp_parser_nested_name_specifier (parser,
3857                                          /*typename_keyword_p=*/true,
3858                                          /*check_dependency_p=*/true,
3859                                          /*type_p=*/true,
3860                                          /*is_declaration=*/true);
3861         /* Look for the optional `template' keyword.  */
3862         template_p = cp_parser_optional_template_keyword (parser);
3863         /* We don't know whether we're looking at a template-id or an
3864            identifier.  */
3865         cp_parser_parse_tentatively (parser);
3866         /* Try a template-id.  */
3867         id = cp_parser_template_id (parser, template_p,
3868                                     /*check_dependency_p=*/true,
3869                                     /*is_declaration=*/true);
3870         /* If that didn't work, try an identifier.  */
3871         if (!cp_parser_parse_definitely (parser))
3872           id = cp_parser_identifier (parser);
3873         /* If we look up a template-id in a non-dependent qualifying
3874            scope, there's no need to create a dependent type.  */
3875         if (TREE_CODE (id) == TYPE_DECL
3876             && !dependent_type_p (parser->scope))
3877           type = TREE_TYPE (id);
3878         /* Create a TYPENAME_TYPE to represent the type to which the
3879            functional cast is being performed.  */
3880         else
3881           type = make_typename_type (parser->scope, id,
3882                                      /*complain=*/1);
3883
3884         postfix_expression = cp_parser_functional_cast (parser, type);
3885       }
3886       break;
3887
3888     default:
3889       {
3890         tree type;
3891
3892         /* If the next thing is a simple-type-specifier, we may be
3893            looking at a functional cast.  We could also be looking at
3894            an id-expression.  So, we try the functional cast, and if
3895            that doesn't work we fall back to the primary-expression.  */
3896         cp_parser_parse_tentatively (parser);
3897         /* Look for the simple-type-specifier.  */
3898         type = cp_parser_simple_type_specifier (parser,
3899                                                 /*decl_specs=*/NULL,
3900                                                 CP_PARSER_FLAGS_NONE);
3901         /* Parse the cast itself.  */
3902         if (!cp_parser_error_occurred (parser))
3903           postfix_expression
3904             = cp_parser_functional_cast (parser, type);
3905         /* If that worked, we're done.  */
3906         if (cp_parser_parse_definitely (parser))
3907           break;
3908
3909         /* If the functional-cast didn't work out, try a
3910            compound-literal.  */
3911         if (cp_parser_allow_gnu_extensions_p (parser)
3912             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3913           {
3914             tree initializer_list = NULL_TREE;
3915             bool saved_in_type_id_in_expr_p;
3916
3917             cp_parser_parse_tentatively (parser);
3918             /* Consume the `('.  */
3919             cp_lexer_consume_token (parser->lexer);
3920             /* Parse the type.  */
3921             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3922             parser->in_type_id_in_expr_p = true;
3923             type = cp_parser_type_id (parser);
3924             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3925             /* Look for the `)'.  */
3926             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3927             /* Look for the `{'.  */
3928             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3929             /* If things aren't going well, there's no need to
3930                keep going.  */
3931             if (!cp_parser_error_occurred (parser))
3932               {
3933                 bool non_constant_p;
3934                 /* Parse the initializer-list.  */
3935                 initializer_list
3936                   = cp_parser_initializer_list (parser, &non_constant_p);
3937                 /* Allow a trailing `,'.  */
3938                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3939                   cp_lexer_consume_token (parser->lexer);
3940                 /* Look for the final `}'.  */
3941                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3942               }
3943             /* If that worked, we're definitely looking at a
3944                compound-literal expression.  */
3945             if (cp_parser_parse_definitely (parser))
3946               {
3947                 /* Warn the user that a compound literal is not
3948                    allowed in standard C++.  */
3949                 if (pedantic)
3950                   pedwarn ("ISO C++ forbids compound-literals");
3951                 /* Form the representation of the compound-literal.  */
3952                 postfix_expression
3953                   = finish_compound_literal (type, initializer_list);
3954                 break;
3955               }
3956           }
3957
3958         /* It must be a primary-expression.  */
3959         postfix_expression = cp_parser_primary_expression (parser,
3960                                                            &idk,
3961                                                            &qualifying_class);
3962       }
3963       break;
3964     }
3965
3966   /* If we were avoiding committing to the processing of a
3967      qualified-id until we knew whether or not we had a
3968      pointer-to-member, we now know.  */
3969   if (qualifying_class)
3970     {
3971       bool done;
3972
3973       /* Peek at the next token.  */
3974       token = cp_lexer_peek_token (parser->lexer);
3975       done = (token->type != CPP_OPEN_SQUARE
3976               && token->type != CPP_OPEN_PAREN
3977               && token->type != CPP_DOT
3978               && token->type != CPP_DEREF
3979               && token->type != CPP_PLUS_PLUS
3980               && token->type != CPP_MINUS_MINUS);
3981
3982       postfix_expression = finish_qualified_id_expr (qualifying_class,
3983                                                      postfix_expression,
3984                                                      done,
3985                                                      address_p);
3986       if (done)
3987         return postfix_expression;
3988     }
3989
3990   /* Keep looping until the postfix-expression is complete.  */
3991   while (true)
3992     {
3993       if (idk == CP_ID_KIND_UNQUALIFIED
3994           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3995           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3996         /* It is not a Koenig lookup function call.  */
3997         postfix_expression
3998           = unqualified_name_lookup_error (postfix_expression);
3999
4000       /* Peek at the next token.  */
4001       token = cp_lexer_peek_token (parser->lexer);
4002
4003       switch (token->type)
4004         {
4005         case CPP_OPEN_SQUARE:
4006           postfix_expression
4007             = cp_parser_postfix_open_square_expression (parser,
4008                                                         postfix_expression,
4009                                                         false);
4010           idk = CP_ID_KIND_NONE;
4011           break;
4012
4013         case CPP_OPEN_PAREN:
4014           /* postfix-expression ( expression-list [opt] ) */
4015           {
4016             bool koenig_p;
4017             tree args = (cp_parser_parenthesized_expression_list
4018                          (parser, false, /*non_constant_p=*/NULL));
4019
4020             if (args == error_mark_node)
4021               {
4022                 postfix_expression = error_mark_node;
4023                 break;
4024               }
4025
4026             /* Function calls are not permitted in
4027                constant-expressions.  */
4028             if (cp_parser_non_integral_constant_expression (parser,
4029                                                             "a function call"))
4030               {
4031                 postfix_expression = error_mark_node;
4032                 break;
4033               }
4034
4035             koenig_p = false;
4036             if (idk == CP_ID_KIND_UNQUALIFIED)
4037               {
4038                 /* We do not perform argument-dependent lookup if
4039                    normal lookup finds a non-function, in accordance
4040                    with the expected resolution of DR 218.  */
4041                 if (args
4042                     && (is_overloaded_fn (postfix_expression)
4043                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4044                   {
4045                     koenig_p = true;
4046                     postfix_expression
4047                       = perform_koenig_lookup (postfix_expression, args);
4048                   }
4049                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4050                   postfix_expression
4051                     = unqualified_fn_lookup_error (postfix_expression);
4052               }
4053
4054             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4055               {
4056                 tree instance = TREE_OPERAND (postfix_expression, 0);
4057                 tree fn = TREE_OPERAND (postfix_expression, 1);
4058
4059                 if (processing_template_decl
4060                     && (type_dependent_expression_p (instance)
4061                         || (!BASELINK_P (fn)
4062                             && TREE_CODE (fn) != FIELD_DECL)
4063                         || type_dependent_expression_p (fn)
4064                         || any_type_dependent_arguments_p (args)))
4065                   {
4066                     postfix_expression
4067                       = build_min_nt (CALL_EXPR, postfix_expression,
4068                                       args, NULL_TREE);
4069                     break;
4070                   }
4071
4072                 if (BASELINK_P (fn))
4073                   postfix_expression
4074                     = (build_new_method_call
4075                        (instance, fn, args, NULL_TREE,
4076                         (idk == CP_ID_KIND_QUALIFIED
4077                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4078                 else
4079                   postfix_expression
4080                     = finish_call_expr (postfix_expression, args,
4081                                         /*disallow_virtual=*/false,
4082                                         /*koenig_p=*/false);
4083               }
4084             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4085                      || TREE_CODE (postfix_expression) == MEMBER_REF
4086                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4087               postfix_expression = (build_offset_ref_call_from_tree
4088                                     (postfix_expression, args));
4089             else if (idk == CP_ID_KIND_QUALIFIED)
4090               /* A call to a static class member, or a namespace-scope
4091                  function.  */
4092               postfix_expression
4093                 = finish_call_expr (postfix_expression, args,
4094                                     /*disallow_virtual=*/true,
4095                                     koenig_p);
4096             else
4097               /* All other function calls.  */
4098               postfix_expression
4099                 = finish_call_expr (postfix_expression, args,
4100                                     /*disallow_virtual=*/false,
4101                                     koenig_p);
4102
4103             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4104             idk = CP_ID_KIND_NONE;
4105           }
4106           break;
4107
4108         case CPP_DOT:
4109         case CPP_DEREF:
4110           /* postfix-expression . template [opt] id-expression
4111              postfix-expression . pseudo-destructor-name
4112              postfix-expression -> template [opt] id-expression
4113              postfix-expression -> pseudo-destructor-name */
4114
4115           /* Consume the `.' or `->' operator.  */
4116           cp_lexer_consume_token (parser->lexer);
4117
4118           postfix_expression
4119             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4120                                                       postfix_expression,
4121                                                       false, &idk);
4122           break;
4123
4124         case CPP_PLUS_PLUS:
4125           /* postfix-expression ++  */
4126           /* Consume the `++' token.  */
4127           cp_lexer_consume_token (parser->lexer);
4128           /* Generate a representation for the complete expression.  */
4129           postfix_expression
4130             = finish_increment_expr (postfix_expression,
4131                                      POSTINCREMENT_EXPR);
4132           /* Increments may not appear in constant-expressions.  */
4133           if (cp_parser_non_integral_constant_expression (parser,
4134                                                           "an increment"))
4135             postfix_expression = error_mark_node;
4136           idk = CP_ID_KIND_NONE;
4137           break;
4138
4139         case CPP_MINUS_MINUS:
4140           /* postfix-expression -- */
4141           /* Consume the `--' token.  */
4142           cp_lexer_consume_token (parser->lexer);
4143           /* Generate a representation for the complete expression.  */
4144           postfix_expression
4145             = finish_increment_expr (postfix_expression,
4146                                      POSTDECREMENT_EXPR);
4147           /* Decrements may not appear in constant-expressions.  */
4148           if (cp_parser_non_integral_constant_expression (parser,
4149                                                           "a decrement"))
4150             postfix_expression = error_mark_node;
4151           idk = CP_ID_KIND_NONE;
4152           break;
4153
4154         default:
4155           return postfix_expression;
4156         }
4157     }
4158
4159   /* We should never get here.  */
4160   gcc_unreachable ();
4161   return error_mark_node;
4162 }
4163
4164 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4165    by cp_parser_builtin_offsetof.  We're looking for
4166
4167      postfix-expression [ expression ]
4168
4169    FOR_OFFSETOF is set if we're being called in that context, which
4170    changes how we deal with integer constant expressions.  */
4171
4172 static tree
4173 cp_parser_postfix_open_square_expression (cp_parser *parser,
4174                                           tree postfix_expression,
4175                                           bool for_offsetof)
4176 {
4177   tree index;
4178
4179   /* Consume the `[' token.  */
4180   cp_lexer_consume_token (parser->lexer);
4181
4182   /* Parse the index expression.  */
4183   /* ??? For offsetof, there is a question of what to allow here.  If
4184      offsetof is not being used in an integral constant expression context,
4185      then we *could* get the right answer by computing the value at runtime.
4186      If we are in an integral constant expression context, then we might
4187      could accept any constant expression; hard to say without analysis.
4188      Rather than open the barn door too wide right away, allow only integer
4189      constant expressions here.  */
4190   if (for_offsetof)
4191     index = cp_parser_constant_expression (parser, false, NULL);
4192   else
4193     index = cp_parser_expression (parser);
4194
4195   /* Look for the closing `]'.  */
4196   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4197
4198   /* Build the ARRAY_REF.  */
4199   postfix_expression = grok_array_decl (postfix_expression, index);
4200
4201   /* When not doing offsetof, array references are not permitted in
4202      constant-expressions.  */
4203   if (!for_offsetof
4204       && (cp_parser_non_integral_constant_expression
4205           (parser, "an array reference")))
4206     postfix_expression = error_mark_node;
4207
4208   return postfix_expression;
4209 }
4210
4211 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4212    by cp_parser_builtin_offsetof.  We're looking for
4213
4214      postfix-expression . template [opt] id-expression
4215      postfix-expression . pseudo-destructor-name
4216      postfix-expression -> template [opt] id-expression
4217      postfix-expression -> pseudo-destructor-name
4218
4219    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4220    limits what of the above we'll actually accept, but nevermind.
4221    TOKEN_TYPE is the "." or "->" token, which will already have been
4222    removed from the stream.  */
4223
4224 static tree
4225 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4226                                         enum cpp_ttype token_type,
4227                                         tree postfix_expression,
4228                                         bool for_offsetof, cp_id_kind *idk)
4229 {
4230   tree name;
4231   bool dependent_p;
4232   bool template_p;
4233   tree scope = NULL_TREE;
4234
4235   /* If this is a `->' operator, dereference the pointer.  */
4236   if (token_type == CPP_DEREF)
4237     postfix_expression = build_x_arrow (postfix_expression);
4238   /* Check to see whether or not the expression is type-dependent.  */
4239   dependent_p = type_dependent_expression_p (postfix_expression);
4240   /* The identifier following the `->' or `.' is not qualified.  */
4241   parser->scope = NULL_TREE;
4242   parser->qualifying_scope = NULL_TREE;
4243   parser->object_scope = NULL_TREE;
4244   *idk = CP_ID_KIND_NONE;
4245   /* Enter the scope corresponding to the type of the object
4246      given by the POSTFIX_EXPRESSION.  */
4247   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4248     {
4249       scope = TREE_TYPE (postfix_expression);
4250       /* According to the standard, no expression should ever have
4251          reference type.  Unfortunately, we do not currently match
4252          the standard in this respect in that our internal representation
4253          of an expression may have reference type even when the standard
4254          says it does not.  Therefore, we have to manually obtain the
4255          underlying type here.  */
4256       scope = non_reference (scope);
4257       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4258       scope = complete_type_or_else (scope, NULL_TREE);
4259       /* Let the name lookup machinery know that we are processing a
4260          class member access expression.  */
4261       parser->context->object_type = scope;
4262       /* If something went wrong, we want to be able to discern that case,
4263          as opposed to the case where there was no SCOPE due to the type
4264          of expression being dependent.  */
4265       if (!scope)
4266         scope = error_mark_node;
4267       /* If the SCOPE was erroneous, make the various semantic analysis
4268          functions exit quickly -- and without issuing additional error
4269          messages.  */
4270       if (scope == error_mark_node)
4271         postfix_expression = error_mark_node;
4272     }
4273
4274   /* If the SCOPE is not a scalar type, we are looking at an
4275      ordinary class member access expression, rather than a
4276      pseudo-destructor-name.  */
4277   if (!scope || !SCALAR_TYPE_P (scope))
4278     {
4279       template_p = cp_parser_optional_template_keyword (parser);
4280       /* Parse the id-expression.  */
4281       name = cp_parser_id_expression (parser, template_p,
4282                                       /*check_dependency_p=*/true,
4283                                       /*template_p=*/NULL,
4284                                       /*declarator_p=*/false);
4285       /* In general, build a SCOPE_REF if the member name is qualified.
4286          However, if the name was not dependent and has already been
4287          resolved; there is no need to build the SCOPE_REF.  For example;
4288
4289              struct X { void f(); };
4290              template <typename T> void f(T* t) { t->X::f(); }
4291
4292          Even though "t" is dependent, "X::f" is not and has been resolved
4293          to a BASELINK; there is no need to include scope information.  */
4294
4295       /* But we do need to remember that there was an explicit scope for
4296          virtual function calls.  */
4297       if (parser->scope)
4298         *idk = CP_ID_KIND_QUALIFIED;
4299
4300       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4301         {
4302           name = build_nt (SCOPE_REF, parser->scope, name);
4303           parser->scope = NULL_TREE;
4304           parser->qualifying_scope = NULL_TREE;
4305           parser->object_scope = NULL_TREE;
4306         }
4307       if (scope && name && BASELINK_P (name))
4308         adjust_result_of_qualified_name_lookup
4309           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4310       postfix_expression
4311         = finish_class_member_access_expr (postfix_expression, name);
4312     }
4313   /* Otherwise, try the pseudo-destructor-name production.  */
4314   else
4315     {
4316       tree s = NULL_TREE;
4317       tree type;
4318
4319       /* Parse the pseudo-destructor-name.  */
4320       cp_parser_pseudo_destructor_name (parser, &s, &type);
4321       /* Form the call.  */
4322       postfix_expression
4323         = finish_pseudo_destructor_expr (postfix_expression,
4324                                          s, TREE_TYPE (type));
4325     }
4326
4327   /* We no longer need to look up names in the scope of the object on
4328      the left-hand side of the `.' or `->' operator.  */
4329   parser->context->object_type = NULL_TREE;
4330
4331   /* Outside of offsetof, these operators may not appear in
4332      constant-expressions.  */
4333   if (!for_offsetof
4334       && (cp_parser_non_integral_constant_expression
4335           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4336     postfix_expression = error_mark_node;
4337
4338   return postfix_expression;
4339 }
4340
4341 /* Parse a parenthesized expression-list.
4342
4343    expression-list:
4344      assignment-expression
4345      expression-list, assignment-expression
4346
4347    attribute-list:
4348      expression-list
4349      identifier
4350      identifier, expression-list
4351
4352    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4353    representation of an assignment-expression.  Note that a TREE_LIST
4354    is returned even if there is only a single expression in the list.
4355    error_mark_node is returned if the ( and or ) are
4356    missing. NULL_TREE is returned on no expressions. The parentheses
4357    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4358    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4359    indicates whether or not all of the expressions in the list were
4360    constant.  */
4361
4362 static tree
4363 cp_parser_parenthesized_expression_list (cp_parser* parser,
4364                                          bool is_attribute_list,
4365                                          bool *non_constant_p)
4366 {
4367   tree expression_list = NULL_TREE;
4368   tree identifier = NULL_TREE;
4369
4370   /* Assume all the expressions will be constant.  */
4371   if (non_constant_p)
4372     *non_constant_p = false;
4373
4374   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4375     return error_mark_node;
4376
4377   /* Consume expressions until there are no more.  */
4378   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4379     while (true)
4380       {
4381         tree expr;
4382
4383         /* At the beginning of attribute lists, check to see if the
4384            next token is an identifier.  */
4385         if (is_attribute_list
4386             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4387           {
4388             cp_token *token;
4389
4390             /* Consume the identifier.  */
4391             token = cp_lexer_consume_token (parser->lexer);
4392             /* Save the identifier.  */
4393             identifier = token->value;
4394           }
4395         else
4396           {
4397             /* Parse the next assignment-expression.  */
4398             if (non_constant_p)
4399               {
4400                 bool expr_non_constant_p;
4401                 expr = (cp_parser_constant_expression
4402                         (parser, /*allow_non_constant_p=*/true,
4403                          &expr_non_constant_p));
4404                 if (expr_non_constant_p)
4405                   *non_constant_p = true;
4406               }
4407             else
4408               expr = cp_parser_assignment_expression (parser);
4409
4410              /* Add it to the list.  We add error_mark_node
4411                 expressions to the list, so that we can still tell if
4412                 the correct form for a parenthesized expression-list
4413                 is found. That gives better errors.  */
4414             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4415
4416             if (expr == error_mark_node)
4417               goto skip_comma;
4418           }
4419
4420         /* After the first item, attribute lists look the same as
4421            expression lists.  */
4422         is_attribute_list = false;
4423
4424       get_comma:;
4425         /* If the next token isn't a `,', then we are done.  */
4426         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4427           break;
4428
4429         /* Otherwise, consume the `,' and keep going.  */
4430         cp_lexer_consume_token (parser->lexer);
4431       }
4432
4433   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4434     {
4435       int ending;
4436
4437     skip_comma:;
4438       /* We try and resync to an unnested comma, as that will give the
4439          user better diagnostics.  */
4440       ending = cp_parser_skip_to_closing_parenthesis (parser,
4441                                                       /*recovering=*/true,
4442                                                       /*or_comma=*/true,
4443                                                       /*consume_paren=*/true);
4444       if (ending < 0)
4445         goto get_comma;
4446       if (!ending)
4447         return error_mark_node;
4448     }
4449
4450   /* We built up the list in reverse order so we must reverse it now.  */
4451   expression_list = nreverse (expression_list);
4452   if (identifier)
4453     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4454
4455   return expression_list;
4456 }
4457
4458 /* Parse a pseudo-destructor-name.
4459
4460    pseudo-destructor-name:
4461      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4462      :: [opt] nested-name-specifier template template-id :: ~ type-name
4463      :: [opt] nested-name-specifier [opt] ~ type-name
4464
4465    If either of the first two productions is used, sets *SCOPE to the
4466    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4467    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4468    or ERROR_MARK_NODE if the parse fails.  */
4469
4470 static void
4471 cp_parser_pseudo_destructor_name (cp_parser* parser,
4472                                   tree* scope,
4473                                   tree* type)
4474 {
4475   bool nested_name_specifier_p;
4476
4477   /* Assume that things will not work out.  */
4478   *type = error_mark_node;
4479
4480   /* Look for the optional `::' operator.  */
4481   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4482   /* Look for the optional nested-name-specifier.  */
4483   nested_name_specifier_p
4484     = (cp_parser_nested_name_specifier_opt (parser,
4485                                             /*typename_keyword_p=*/false,
4486                                             /*check_dependency_p=*/true,
4487                                             /*type_p=*/false,
4488                                             /*is_declaration=*/true)
4489        != NULL_TREE);
4490   /* Now, if we saw a nested-name-specifier, we might be doing the
4491      second production.  */
4492   if (nested_name_specifier_p
4493       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4494     {
4495       /* Consume the `template' keyword.  */
4496       cp_lexer_consume_token (parser->lexer);
4497       /* Parse the template-id.  */
4498       cp_parser_template_id (parser,
4499                              /*template_keyword_p=*/true,
4500                              /*check_dependency_p=*/false,
4501                              /*is_declaration=*/true);
4502       /* Look for the `::' token.  */
4503       cp_parser_require (parser, CPP_SCOPE, "`::'");
4504     }
4505   /* If the next token is not a `~', then there might be some
4506      additional qualification.  */
4507   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4508     {
4509       /* Look for the type-name.  */
4510       *scope = TREE_TYPE (cp_parser_type_name (parser));
4511
4512       if (*scope == error_mark_node)
4513         return;
4514
4515       /* If we don't have ::~, then something has gone wrong.  Since
4516          the only caller of this function is looking for something
4517          after `.' or `->' after a scalar type, most likely the
4518          program is trying to get a member of a non-aggregate
4519          type.  */
4520       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4521           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4522         {
4523           cp_parser_error (parser, "request for member of non-aggregate type");
4524           return;
4525         }
4526
4527       /* Look for the `::' token.  */
4528       cp_parser_require (parser, CPP_SCOPE, "`::'");
4529     }
4530   else
4531     *scope = NULL_TREE;
4532
4533   /* Look for the `~'.  */
4534   cp_parser_require (parser, CPP_COMPL, "`~'");
4535   /* Look for the type-name again.  We are not responsible for
4536      checking that it matches the first type-name.  */
4537   *type = cp_parser_type_name (parser);
4538 }
4539
4540 /* Parse a unary-expression.
4541
4542    unary-expression:
4543      postfix-expression
4544      ++ cast-expression
4545      -- cast-expression
4546      unary-operator cast-expression
4547      sizeof unary-expression
4548      sizeof ( type-id )
4549      new-expression
4550      delete-expression
4551
4552    GNU Extensions:
4553
4554    unary-expression:
4555      __extension__ cast-expression
4556      __alignof__ unary-expression
4557      __alignof__ ( type-id )
4558      __real__ cast-expression
4559      __imag__ cast-expression
4560      && identifier
4561
4562    ADDRESS_P is true iff the unary-expression is appearing as the
4563    operand of the `&' operator.
4564
4565    Returns a representation of the expression.  */
4566
4567 static tree
4568 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4569 {
4570   cp_token *token;
4571   enum tree_code unary_operator;
4572
4573   /* Peek at the next token.  */
4574   token = cp_lexer_peek_token (parser->lexer);
4575   /* Some keywords give away the kind of expression.  */
4576   if (token->type == CPP_KEYWORD)
4577     {
4578       enum rid keyword = token->keyword;
4579
4580       switch (keyword)
4581         {
4582         case RID_ALIGNOF:
4583         case RID_SIZEOF:
4584           {
4585             tree operand;
4586             enum tree_code op;
4587
4588             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4589             /* Consume the token.  */
4590             cp_lexer_consume_token (parser->lexer);
4591             /* Parse the operand.  */
4592             operand = cp_parser_sizeof_operand (parser, keyword);
4593
4594             if (TYPE_P (operand))
4595               return cxx_sizeof_or_alignof_type (operand, op, true);
4596             else
4597               return cxx_sizeof_or_alignof_expr (operand, op);
4598           }
4599
4600         case RID_NEW:
4601           return cp_parser_new_expression (parser);
4602
4603         case RID_DELETE:
4604           return cp_parser_delete_expression (parser);
4605
4606         case RID_EXTENSION:
4607           {
4608             /* The saved value of the PEDANTIC flag.  */
4609             int saved_pedantic;
4610             tree expr;
4611
4612             /* Save away the PEDANTIC flag.  */
4613             cp_parser_extension_opt (parser, &saved_pedantic);
4614             /* Parse the cast-expression.  */
4615             expr = cp_parser_simple_cast_expression (parser);
4616             /* Restore the PEDANTIC flag.  */
4617             pedantic = saved_pedantic;
4618
4619             return expr;
4620           }
4621
4622         case RID_REALPART:
4623         case RID_IMAGPART:
4624           {
4625             tree expression;
4626
4627             /* Consume the `__real__' or `__imag__' token.  */
4628             cp_lexer_consume_token (parser->lexer);
4629             /* Parse the cast-expression.  */
4630             expression = cp_parser_simple_cast_expression (parser);
4631             /* Create the complete representation.  */
4632             return build_x_unary_op ((keyword == RID_REALPART
4633                                       ? REALPART_EXPR : IMAGPART_EXPR),
4634                                      expression);
4635           }
4636           break;
4637
4638         default:
4639           break;
4640         }
4641     }
4642
4643   /* Look for the `:: new' and `:: delete', which also signal the
4644      beginning of a new-expression, or delete-expression,
4645      respectively.  If the next token is `::', then it might be one of
4646      these.  */
4647   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4648     {
4649       enum rid keyword;
4650
4651       /* See if the token after the `::' is one of the keywords in
4652          which we're interested.  */
4653       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4654       /* If it's `new', we have a new-expression.  */
4655       if (keyword == RID_NEW)
4656         return cp_parser_new_expression (parser);
4657       /* Similarly, for `delete'.  */
4658       else if (keyword == RID_DELETE)
4659         return cp_parser_delete_expression (parser);
4660     }
4661
4662   /* Look for a unary operator.  */
4663   unary_operator = cp_parser_unary_operator (token);
4664   /* The `++' and `--' operators can be handled similarly, even though
4665      they are not technically unary-operators in the grammar.  */
4666   if (unary_operator == ERROR_MARK)
4667     {
4668       if (token->type == CPP_PLUS_PLUS)
4669         unary_operator = PREINCREMENT_EXPR;
4670       else if (token->type == CPP_MINUS_MINUS)
4671         unary_operator = PREDECREMENT_EXPR;
4672       /* Handle the GNU address-of-label extension.  */
4673       else if (cp_parser_allow_gnu_extensions_p (parser)
4674                && token->type == CPP_AND_AND)
4675         {
4676           tree identifier;
4677
4678           /* Consume the '&&' token.  */
4679           cp_lexer_consume_token (parser->lexer);
4680           /* Look for the identifier.  */
4681           identifier = cp_parser_identifier (parser);
4682           /* Create an expression representing the address.  */
4683           return finish_label_address_expr (identifier);
4684         }
4685     }
4686   if (unary_operator != ERROR_MARK)
4687     {
4688       tree cast_expression;
4689       tree expression = error_mark_node;
4690       const char *non_constant_p = NULL;
4691
4692       /* Consume the operator token.  */
4693       token = cp_lexer_consume_token (parser->lexer);
4694       /* Parse the cast-expression.  */
4695       cast_expression
4696         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4697       /* Now, build an appropriate representation.  */
4698       switch (unary_operator)
4699         {
4700         case INDIRECT_REF:
4701           non_constant_p = "`*'";
4702           expression = build_x_indirect_ref (cast_expression, "unary *");
4703           break;
4704
4705         case ADDR_EXPR:
4706           non_constant_p = "`&'";
4707           /* Fall through.  */
4708         case BIT_NOT_EXPR:
4709           expression = build_x_unary_op (unary_operator, cast_expression);
4710           break;
4711
4712         case PREINCREMENT_EXPR:
4713         case PREDECREMENT_EXPR:
4714           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4715                             ? "`++'" : "`--'");
4716           /* Fall through.  */
4717         case CONVERT_EXPR:
4718         case NEGATE_EXPR:
4719         case TRUTH_NOT_EXPR:
4720           expression = finish_unary_op_expr (unary_operator, cast_expression);
4721           break;
4722
4723         default:
4724           gcc_unreachable ();
4725         }
4726
4727       if (non_constant_p
4728           && cp_parser_non_integral_constant_expression (parser,
4729                                                          non_constant_p))
4730         expression = error_mark_node;
4731
4732       return expression;
4733     }
4734
4735   return cp_parser_postfix_expression (parser, address_p);
4736 }
4737
4738 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4739    unary-operator, the corresponding tree code is returned.  */
4740
4741 static enum tree_code
4742 cp_parser_unary_operator (cp_token* token)
4743 {
4744   switch (token->type)
4745     {
4746     case CPP_MULT:
4747       return INDIRECT_REF;
4748
4749     case CPP_AND:
4750       return ADDR_EXPR;
4751
4752     case CPP_PLUS:
4753       return CONVERT_EXPR;
4754
4755     case CPP_MINUS:
4756       return NEGATE_EXPR;
4757
4758     case CPP_NOT:
4759       return TRUTH_NOT_EXPR;
4760
4761     case CPP_COMPL:
4762       return BIT_NOT_EXPR;
4763
4764     default:
4765       return ERROR_MARK;
4766     }
4767 }
4768
4769 /* Parse a new-expression.
4770
4771    new-expression:
4772      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4773      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4774
4775    Returns a representation of the expression.  */
4776
4777 static tree
4778 cp_parser_new_expression (cp_parser* parser)
4779 {
4780   bool global_scope_p;
4781   tree placement;
4782   tree type;
4783   tree initializer;
4784   tree nelts;
4785
4786   /* Look for the optional `::' operator.  */
4787   global_scope_p
4788     = (cp_parser_global_scope_opt (parser,
4789                                    /*current_scope_valid_p=*/false)
4790        != NULL_TREE);
4791   /* Look for the `new' operator.  */
4792   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4793   /* There's no easy way to tell a new-placement from the
4794      `( type-id )' construct.  */
4795   cp_parser_parse_tentatively (parser);
4796   /* Look for a new-placement.  */
4797   placement = cp_parser_new_placement (parser);
4798   /* If that didn't work out, there's no new-placement.  */
4799   if (!cp_parser_parse_definitely (parser))
4800     placement = NULL_TREE;
4801
4802   /* If the next token is a `(', then we have a parenthesized
4803      type-id.  */
4804   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4805     {
4806       /* Consume the `('.  */
4807       cp_lexer_consume_token (parser->lexer);
4808       /* Parse the type-id.  */
4809       type = cp_parser_type_id (parser);
4810       /* Look for the closing `)'.  */
4811       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4812       /* There should not be a direct-new-declarator in this production,
4813          but GCC used to allowed this, so we check and emit a sensible error
4814          message for this case.  */
4815       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4816         {
4817           error ("array bound forbidden after parenthesized type-id");
4818           inform ("try removing the parentheses around the type-id");
4819           cp_parser_direct_new_declarator (parser);
4820         }
4821       nelts = integer_one_node;
4822     }
4823   /* Otherwise, there must be a new-type-id.  */
4824   else
4825     type = cp_parser_new_type_id (parser, &nelts);
4826
4827   /* If the next token is a `(', then we have a new-initializer.  */
4828   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4829     initializer = cp_parser_new_initializer (parser);
4830   else
4831     initializer = NULL_TREE;
4832
4833   /* A new-expression may not appear in an integral constant
4834      expression.  */
4835   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4836     return error_mark_node;
4837
4838   /* Create a representation of the new-expression.  */
4839   return build_new (placement, type, nelts, initializer, global_scope_p);
4840 }
4841
4842 /* Parse a new-placement.
4843
4844    new-placement:
4845      ( expression-list )
4846
4847    Returns the same representation as for an expression-list.  */
4848
4849 static tree
4850 cp_parser_new_placement (cp_parser* parser)
4851 {
4852   tree expression_list;
4853
4854   /* Parse the expression-list.  */
4855   expression_list = (cp_parser_parenthesized_expression_list
4856                      (parser, false, /*non_constant_p=*/NULL));
4857
4858   return expression_list;
4859 }
4860
4861 /* Parse a new-type-id.
4862
4863    new-type-id:
4864      type-specifier-seq new-declarator [opt]
4865
4866    Returns the TYPE allocated.  If the new-type-id indicates an array
4867    type, *NELTS is set to the number of elements in the last array
4868    bound; the TYPE will not include the last array bound.  */
4869
4870 static tree
4871 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4872 {
4873   cp_decl_specifier_seq type_specifier_seq;
4874   cp_declarator *new_declarator;
4875   cp_declarator *declarator;
4876   cp_declarator *outer_declarator;
4877   const char *saved_message;
4878   tree type;
4879
4880   /* The type-specifier sequence must not contain type definitions.
4881      (It cannot contain declarations of new types either, but if they
4882      are not definitions we will catch that because they are not
4883      complete.)  */
4884   saved_message = parser->type_definition_forbidden_message;
4885   parser->type_definition_forbidden_message
4886     = "types may not be defined in a new-type-id";
4887   /* Parse the type-specifier-seq.  */
4888   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4889   /* Restore the old message.  */
4890   parser->type_definition_forbidden_message = saved_message;
4891   /* Parse the new-declarator.  */
4892   new_declarator = cp_parser_new_declarator_opt (parser);
4893
4894   /* Determine the number of elements in the last array dimension, if
4895      any.  */
4896   *nelts = NULL_TREE;
4897   /* Skip down to the last array dimension.  */
4898   declarator = new_declarator;
4899   outer_declarator = NULL;
4900   while (declarator && (declarator->kind == cdk_pointer
4901                         || declarator->kind == cdk_ptrmem))
4902     {
4903       outer_declarator = declarator;
4904       declarator = declarator->declarator;
4905     }
4906   while (declarator
4907          && declarator->kind == cdk_array
4908          && declarator->declarator
4909          && declarator->declarator->kind == cdk_array)
4910     {
4911       outer_declarator = declarator;
4912       declarator = declarator->declarator;
4913     }
4914
4915   if (declarator && declarator->kind == cdk_array)
4916     {
4917       *nelts = declarator->u.array.bounds;
4918       if (*nelts == error_mark_node)
4919         *nelts = integer_one_node;
4920       else if (!processing_template_decl)
4921         {
4922           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4923                                            false))
4924             pedwarn ("size in array new must have integral type");
4925           *nelts = save_expr (cp_convert (sizetype, *nelts));
4926           if (*nelts == integer_zero_node)
4927             warning ("zero size array reserves no space");
4928         }
4929       if (outer_declarator)
4930         outer_declarator->declarator = declarator->declarator;
4931       else
4932         new_declarator = NULL;
4933     }
4934
4935   type = groktypename (&type_specifier_seq, new_declarator);
4936   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4937     {
4938       *nelts = array_type_nelts_top (type);
4939       type = TREE_TYPE (type);
4940     }
4941   return type;
4942 }
4943
4944 /* Parse an (optional) new-declarator.
4945
4946    new-declarator:
4947      ptr-operator new-declarator [opt]
4948      direct-new-declarator
4949
4950    Returns the declarator.  */
4951
4952 static cp_declarator *
4953 cp_parser_new_declarator_opt (cp_parser* parser)
4954 {
4955   enum tree_code code;
4956   tree type;
4957   cp_cv_quals cv_quals;
4958
4959   /* We don't know if there's a ptr-operator next, or not.  */
4960   cp_parser_parse_tentatively (parser);
4961   /* Look for a ptr-operator.  */
4962   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4963   /* If that worked, look for more new-declarators.  */
4964   if (cp_parser_parse_definitely (parser))
4965     {
4966       cp_declarator *declarator;
4967
4968       /* Parse another optional declarator.  */
4969       declarator = cp_parser_new_declarator_opt (parser);
4970
4971       /* Create the representation of the declarator.  */
4972       if (type)
4973         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4974       else if (code == INDIRECT_REF)
4975         declarator = make_pointer_declarator (cv_quals, declarator);
4976       else
4977         declarator = make_reference_declarator (cv_quals, declarator);
4978
4979       return declarator;
4980     }
4981
4982   /* If the next token is a `[', there is a direct-new-declarator.  */
4983   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4984     return cp_parser_direct_new_declarator (parser);
4985
4986   return NULL;
4987 }
4988
4989 /* Parse a direct-new-declarator.
4990
4991    direct-new-declarator:
4992      [ expression ]
4993      direct-new-declarator [constant-expression]
4994
4995    */
4996
4997 static cp_declarator *
4998 cp_parser_direct_new_declarator (cp_parser* parser)
4999 {
5000   cp_declarator *declarator = NULL;
5001
5002   while (true)
5003     {
5004       tree expression;
5005
5006       /* Look for the opening `['.  */
5007       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5008       /* The first expression is not required to be constant.  */
5009       if (!declarator)
5010         {
5011           expression = cp_parser_expression (parser);
5012           /* The standard requires that the expression have integral
5013              type.  DR 74 adds enumeration types.  We believe that the
5014              real intent is that these expressions be handled like the
5015              expression in a `switch' condition, which also allows
5016              classes with a single conversion to integral or
5017              enumeration type.  */
5018           if (!processing_template_decl)
5019             {
5020               expression
5021                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5022                                               expression,
5023                                               /*complain=*/true);
5024               if (!expression)
5025                 {
5026                   error ("expression in new-declarator must have integral or enumeration type");
5027                   expression = error_mark_node;
5028                 }
5029             }
5030         }
5031       /* But all the other expressions must be.  */
5032       else
5033         expression
5034           = cp_parser_constant_expression (parser,
5035                                            /*allow_non_constant=*/false,
5036                                            NULL);
5037       /* Look for the closing `]'.  */
5038       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5039
5040       /* Add this bound to the declarator.  */
5041       declarator = make_array_declarator (declarator, expression);
5042
5043       /* If the next token is not a `[', then there are no more
5044          bounds.  */
5045       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5046         break;
5047     }
5048
5049   return declarator;
5050 }
5051
5052 /* Parse a new-initializer.
5053
5054    new-initializer:
5055      ( expression-list [opt] )
5056
5057    Returns a representation of the expression-list.  If there is no
5058    expression-list, VOID_ZERO_NODE is returned.  */
5059
5060 static tree
5061 cp_parser_new_initializer (cp_parser* parser)
5062 {
5063   tree expression_list;
5064
5065   expression_list = (cp_parser_parenthesized_expression_list
5066                      (parser, false, /*non_constant_p=*/NULL));
5067   if (!expression_list)
5068     expression_list = void_zero_node;
5069
5070   return expression_list;
5071 }
5072
5073 /* Parse a delete-expression.
5074
5075    delete-expression:
5076      :: [opt] delete cast-expression
5077      :: [opt] delete [ ] cast-expression
5078
5079    Returns a representation of the expression.  */
5080
5081 static tree
5082 cp_parser_delete_expression (cp_parser* parser)
5083 {
5084   bool global_scope_p;
5085   bool array_p;
5086   tree expression;
5087
5088   /* Look for the optional `::' operator.  */
5089   global_scope_p
5090     = (cp_parser_global_scope_opt (parser,
5091                                    /*current_scope_valid_p=*/false)
5092        != NULL_TREE);
5093   /* Look for the `delete' keyword.  */
5094   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5095   /* See if the array syntax is in use.  */
5096   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5097     {
5098       /* Consume the `[' token.  */
5099       cp_lexer_consume_token (parser->lexer);
5100       /* Look for the `]' token.  */
5101       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5102       /* Remember that this is the `[]' construct.  */
5103       array_p = true;
5104     }
5105   else
5106     array_p = false;
5107
5108   /* Parse the cast-expression.  */
5109   expression = cp_parser_simple_cast_expression (parser);
5110
5111   /* A delete-expression may not appear in an integral constant
5112      expression.  */
5113   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5114     return error_mark_node;
5115
5116   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5117 }
5118
5119 /* Parse a cast-expression.
5120
5121    cast-expression:
5122      unary-expression
5123      ( type-id ) cast-expression
5124
5125    Returns a representation of the expression.  */
5126
5127 static tree
5128 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5129 {
5130   /* If it's a `(', then we might be looking at a cast.  */
5131   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5132     {
5133       tree type = NULL_TREE;
5134       tree expr = NULL_TREE;
5135       bool compound_literal_p;
5136       const char *saved_message;
5137
5138       /* There's no way to know yet whether or not this is a cast.
5139          For example, `(int (3))' is a unary-expression, while `(int)
5140          3' is a cast.  So, we resort to parsing tentatively.  */
5141       cp_parser_parse_tentatively (parser);
5142       /* Types may not be defined in a cast.  */
5143       saved_message = parser->type_definition_forbidden_message;
5144       parser->type_definition_forbidden_message
5145         = "types may not be defined in casts";
5146       /* Consume the `('.  */
5147       cp_lexer_consume_token (parser->lexer);
5148       /* A very tricky bit is that `(struct S) { 3 }' is a
5149          compound-literal (which we permit in C++ as an extension).
5150          But, that construct is not a cast-expression -- it is a
5151          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5152          is legal; if the compound-literal were a cast-expression,
5153          you'd need an extra set of parentheses.)  But, if we parse
5154          the type-id, and it happens to be a class-specifier, then we
5155          will commit to the parse at that point, because we cannot
5156          undo the action that is done when creating a new class.  So,
5157          then we cannot back up and do a postfix-expression.
5158
5159          Therefore, we scan ahead to the closing `)', and check to see
5160          if the token after the `)' is a `{'.  If so, we are not
5161          looking at a cast-expression.
5162
5163          Save tokens so that we can put them back.  */
5164       cp_lexer_save_tokens (parser->lexer);
5165       /* Skip tokens until the next token is a closing parenthesis.
5166          If we find the closing `)', and the next token is a `{', then
5167          we are looking at a compound-literal.  */
5168       compound_literal_p
5169         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5170                                                   /*consume_paren=*/true)
5171            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5172       /* Roll back the tokens we skipped.  */
5173       cp_lexer_rollback_tokens (parser->lexer);
5174       /* If we were looking at a compound-literal, simulate an error
5175          so that the call to cp_parser_parse_definitely below will
5176          fail.  */
5177       if (compound_literal_p)
5178         cp_parser_simulate_error (parser);
5179       else
5180         {
5181           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5182           parser->in_type_id_in_expr_p = true;
5183           /* Look for the type-id.  */
5184           type = cp_parser_type_id (parser);
5185           /* Look for the closing `)'.  */
5186           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5187           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5188         }
5189
5190       /* Restore the saved message.  */
5191       parser->type_definition_forbidden_message = saved_message;
5192
5193       /* If ok so far, parse the dependent expression. We cannot be
5194          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5195          ctor of T, but looks like a cast to function returning T
5196          without a dependent expression.  */
5197       if (!cp_parser_error_occurred (parser))
5198         expr = cp_parser_simple_cast_expression (parser);
5199
5200       if (cp_parser_parse_definitely (parser))
5201         {
5202           /* Warn about old-style casts, if so requested.  */
5203           if (warn_old_style_cast
5204               && !in_system_header
5205               && !VOID_TYPE_P (type)
5206               && current_lang_name != lang_name_c)
5207             warning ("use of old-style cast");
5208
5209           /* Only type conversions to integral or enumeration types
5210              can be used in constant-expressions.  */
5211           if (parser->integral_constant_expression_p
5212               && !dependent_type_p (type)
5213               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5214               && (cp_parser_non_integral_constant_expression
5215                   (parser,
5216                    "a cast to a type other than an integral or "
5217                    "enumeration type")))
5218             return error_mark_node;
5219
5220           /* Perform the cast.  */
5221           expr = build_c_cast (type, expr);
5222           return expr;
5223         }
5224     }
5225
5226   /* If we get here, then it's not a cast, so it must be a
5227      unary-expression.  */
5228   return cp_parser_unary_expression (parser, address_p);
5229 }
5230
5231 /* Parse a binary expression of the general form:
5232
5233    pm-expression:
5234      cast-expression
5235      pm-expression .* cast-expression
5236      pm-expression ->* cast-expression
5237
5238    multiplicative-expression:
5239      pm-expression
5240      multiplicative-expression * pm-expression
5241      multiplicative-expression / pm-expression
5242      multiplicative-expression % pm-expression
5243
5244    additive-expression:
5245      multiplicative-expression
5246      additive-expression + multiplicative-expression
5247      additive-expression - multiplicative-expression
5248
5249    shift-expression:
5250      additive-expression
5251      shift-expression << additive-expression
5252      shift-expression >> additive-expression
5253
5254    relational-expression:
5255      shift-expression
5256      relational-expression < shift-expression
5257      relational-expression > shift-expression
5258      relational-expression <= shift-expression
5259      relational-expression >= shift-expression
5260
5261   GNU Extension:
5262   
5263    relational-expression:
5264      relational-expression <? shift-expression
5265      relational-expression >? shift-expression
5266
5267    equality-expression:
5268      relational-expression
5269      equality-expression == relational-expression
5270      equality-expression != relational-expression
5271
5272    and-expression:
5273      equality-expression
5274      and-expression & equality-expression
5275
5276    exclusive-or-expression:
5277      and-expression
5278      exclusive-or-expression ^ and-expression
5279
5280    inclusive-or-expression:
5281      exclusive-or-expression
5282      inclusive-or-expression | exclusive-or-expression
5283
5284    logical-and-expression:
5285      inclusive-or-expression
5286      logical-and-expression && inclusive-or-expression
5287
5288    logical-or-expression:
5289      logical-and-expression
5290      logical-or-expression || logical-and-expression
5291
5292    All these are implemented with a single function like:
5293
5294    binary-expression:
5295      simple-cast-expression
5296      binary-expression <token> binary-expression
5297
5298    The binops_by_token map is used to get the tree codes for each <token> type.
5299    binary-expressions are associated according to a precedence table.  */
5300
5301 #define TOKEN_PRECEDENCE(token) \
5302   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5303    ? PREC_NOT_OPERATOR \
5304    : binops_by_token[token->type].prec)
5305
5306 static tree
5307 cp_parser_binary_expression (cp_parser* parser)
5308 {
5309   cp_parser_expression_stack stack;
5310   cp_parser_expression_stack_entry *sp = &stack[0];
5311   tree lhs, rhs;
5312   cp_token *token;
5313   enum tree_code tree_type;
5314   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5315   bool overloaded_p;
5316
5317   /* Parse the first expression.  */
5318   lhs = cp_parser_simple_cast_expression (parser);
5319
5320   for (;;)
5321     {
5322       /* Get an operator token.  */
5323       token = cp_lexer_peek_token (parser->lexer);
5324       new_prec = TOKEN_PRECEDENCE (token);
5325
5326       /* Popping an entry off the stack means we completed a subexpression:
5327          - either we found a token which is not an operator (`>' where it is not
5328            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5329            will happen repeatedly;
5330          - or, we found an operator which has lower priority.  This is the case 
5331            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5332            parsing `3 * 4'. */
5333       if (new_prec <= prec)
5334         {
5335           if (sp == stack)
5336             break;
5337           else
5338             goto pop;
5339         }
5340
5341      get_rhs:
5342       tree_type = binops_by_token[token->type].tree_type;
5343
5344       /* We used the operator token. */
5345       cp_lexer_consume_token (parser->lexer);
5346
5347       /* Extract another operand.  It may be the RHS of this expression
5348          or the LHS of a new, higher priority expression.  */
5349       rhs = cp_parser_simple_cast_expression (parser);
5350
5351       /* Get another operator token.  Look up its precedence to avoid
5352          building a useless (immediately popped) stack entry for common
5353          cases such as 3 + 4 + 5 or 3 * 4 + 5.   */
5354       token = cp_lexer_peek_token (parser->lexer);
5355       lookahead_prec = TOKEN_PRECEDENCE (token);
5356       if (lookahead_prec > new_prec)
5357         {
5358           /* ... and prepare to parse the RHS of the new, higher priority
5359              expression.  */
5360           sp->prec = prec;
5361           sp->tree_type = tree_type;
5362           sp->lhs = lhs;
5363           sp++;
5364           lhs = rhs;
5365           prec = new_prec;
5366           new_prec = lookahead_prec;
5367           goto get_rhs;
5368
5369          pop:
5370           /* If the stack is not empty, we have parsed into LHS the right side
5371              (`4' in the example above) of an expression we had suspended.
5372              We can use the information on the stack to recover the LHS (`3') 
5373              from the stack together with the tree code (`MULT_EXPR'), and
5374              the precedence of the higher level subexpression
5375              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5376              which will be used to actually build the additive expression.  */
5377           --sp;
5378           prec = sp->prec;
5379           tree_type = sp->tree_type;
5380           rhs = lhs;
5381           lhs = sp->lhs;
5382         }
5383
5384       overloaded_p = false;
5385       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5386
5387       /* If the binary operator required the use of an overloaded operator,
5388          then this expression cannot be an integral constant-expression.
5389          An overloaded operator can be used even if both operands are
5390          otherwise permissible in an integral constant-expression if at
5391          least one of the operands is of enumeration type.  */
5392
5393       if (overloaded_p
5394           && (cp_parser_non_integral_constant_expression 
5395               (parser, "calls to overloaded operators")))
5396         return error_mark_node;
5397     }
5398
5399   return lhs;
5400 }
5401
5402
5403 /* Parse the `? expression : assignment-expression' part of a
5404    conditional-expression.  The LOGICAL_OR_EXPR is the
5405    logical-or-expression that started the conditional-expression.
5406    Returns a representation of the entire conditional-expression.
5407
5408    This routine is used by cp_parser_assignment_expression.
5409
5410      ? expression : assignment-expression
5411
5412    GNU Extensions:
5413
5414      ? : assignment-expression */
5415
5416 static tree
5417 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5418 {
5419   tree expr;
5420   tree assignment_expr;
5421
5422   /* Consume the `?' token.  */
5423   cp_lexer_consume_token (parser->lexer);
5424   if (cp_parser_allow_gnu_extensions_p (parser)
5425       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5426     /* Implicit true clause.  */
5427     expr = NULL_TREE;
5428   else
5429     /* Parse the expression.  */
5430     expr = cp_parser_expression (parser);
5431
5432   /* The next token should be a `:'.  */
5433   cp_parser_require (parser, CPP_COLON, "`:'");
5434   /* Parse the assignment-expression.  */
5435   assignment_expr = cp_parser_assignment_expression (parser);
5436
5437   /* Build the conditional-expression.  */
5438   return build_x_conditional_expr (logical_or_expr,
5439                                    expr,
5440                                    assignment_expr);
5441 }
5442
5443 /* Parse an assignment-expression.
5444
5445    assignment-expression:
5446      conditional-expression
5447      logical-or-expression assignment-operator assignment_expression
5448      throw-expression
5449
5450    Returns a representation for the expression.  */
5451
5452 static tree
5453 cp_parser_assignment_expression (cp_parser* parser)
5454 {
5455   tree expr;
5456
5457   /* If the next token is the `throw' keyword, then we're looking at
5458      a throw-expression.  */
5459   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5460     expr = cp_parser_throw_expression (parser);
5461   /* Otherwise, it must be that we are looking at a
5462      logical-or-expression.  */
5463   else
5464     {
5465       /* Parse the binary expressions (logical-or-expression).  */
5466       expr = cp_parser_binary_expression (parser);
5467       /* If the next token is a `?' then we're actually looking at a
5468          conditional-expression.  */
5469       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5470         return cp_parser_question_colon_clause (parser, expr);
5471       else
5472         {
5473           enum tree_code assignment_operator;
5474
5475           /* If it's an assignment-operator, we're using the second
5476              production.  */
5477           assignment_operator
5478             = cp_parser_assignment_operator_opt (parser);
5479           if (assignment_operator != ERROR_MARK)
5480             {
5481               tree rhs;
5482
5483               /* Parse the right-hand side of the assignment.  */
5484               rhs = cp_parser_assignment_expression (parser);
5485               /* An assignment may not appear in a
5486                  constant-expression.  */
5487               if (cp_parser_non_integral_constant_expression (parser,
5488                                                               "an assignment"))
5489                 return error_mark_node;
5490               /* Build the assignment expression.  */
5491               expr = build_x_modify_expr (expr,
5492                                           assignment_operator,
5493                                           rhs);
5494             }
5495         }
5496     }
5497
5498   return expr;
5499 }
5500
5501 /* Parse an (optional) assignment-operator.
5502
5503    assignment-operator: one of
5504      = *= /= %= += -= >>= <<= &= ^= |=
5505
5506    GNU Extension:
5507
5508    assignment-operator: one of
5509      <?= >?=
5510
5511    If the next token is an assignment operator, the corresponding tree
5512    code is returned, and the token is consumed.  For example, for
5513    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5514    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5515    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5516    operator, ERROR_MARK is returned.  */
5517
5518 static enum tree_code
5519 cp_parser_assignment_operator_opt (cp_parser* parser)
5520 {
5521   enum tree_code op;
5522   cp_token *token;
5523
5524   /* Peek at the next toen.  */
5525   token = cp_lexer_peek_token (parser->lexer);
5526
5527   switch (token->type)
5528     {
5529     case CPP_EQ:
5530       op = NOP_EXPR;
5531       break;
5532
5533     case CPP_MULT_EQ:
5534       op = MULT_EXPR;
5535       break;
5536
5537     case CPP_DIV_EQ:
5538       op = TRUNC_DIV_EXPR;
5539       break;
5540
5541     case CPP_MOD_EQ:
5542       op = TRUNC_MOD_EXPR;
5543       break;
5544
5545     case CPP_PLUS_EQ:
5546       op = PLUS_EXPR;
5547       break;
5548
5549     case CPP_MINUS_EQ:
5550       op = MINUS_EXPR;
5551       break;
5552
5553     case CPP_RSHIFT_EQ:
5554       op = RSHIFT_EXPR;
5555       break;
5556
5557     case CPP_LSHIFT_EQ:
5558       op = LSHIFT_EXPR;
5559       break;
5560
5561     case CPP_AND_EQ:
5562       op = BIT_AND_EXPR;
5563       break;
5564
5565     case CPP_XOR_EQ:
5566       op = BIT_XOR_EXPR;
5567       break;
5568
5569     case CPP_OR_EQ:
5570       op = BIT_IOR_EXPR;
5571       break;
5572
5573     case CPP_MIN_EQ:
5574       op = MIN_EXPR;
5575       break;
5576
5577     case CPP_MAX_EQ:
5578       op = MAX_EXPR;
5579       break;
5580
5581     default:
5582       /* Nothing else is an assignment operator.  */
5583       op = ERROR_MARK;
5584     }
5585
5586   /* If it was an assignment operator, consume it.  */
5587   if (op != ERROR_MARK)
5588     cp_lexer_consume_token (parser->lexer);
5589
5590   return op;
5591 }
5592
5593 /* Parse an expression.
5594
5595    expression:
5596      assignment-expression
5597      expression , assignment-expression
5598
5599    Returns a representation of the expression.  */
5600
5601 static tree
5602 cp_parser_expression (cp_parser* parser)
5603 {
5604   tree expression = NULL_TREE;
5605
5606   while (true)
5607     {
5608       tree assignment_expression;
5609
5610       /* Parse the next assignment-expression.  */
5611       assignment_expression
5612         = cp_parser_assignment_expression (parser);
5613       /* If this is the first assignment-expression, we can just
5614          save it away.  */
5615       if (!expression)
5616         expression = assignment_expression;
5617       else
5618         expression = build_x_compound_expr (expression,
5619                                             assignment_expression);
5620       /* If the next token is not a comma, then we are done with the
5621          expression.  */
5622       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5623         break;
5624       /* Consume the `,'.  */
5625       cp_lexer_consume_token (parser->lexer);
5626       /* A comma operator cannot appear in a constant-expression.  */
5627       if (cp_parser_non_integral_constant_expression (parser,
5628                                                       "a comma operator"))
5629         expression = error_mark_node;
5630     }
5631
5632   return expression;
5633 }
5634
5635 /* Parse a constant-expression.
5636
5637    constant-expression:
5638      conditional-expression
5639
5640   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5641   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5642   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5643   is false, NON_CONSTANT_P should be NULL.  */
5644
5645 static tree
5646 cp_parser_constant_expression (cp_parser* parser,
5647                                bool allow_non_constant_p,
5648                                bool *non_constant_p)
5649 {
5650   bool saved_integral_constant_expression_p;
5651   bool saved_allow_non_integral_constant_expression_p;
5652   bool saved_non_integral_constant_expression_p;
5653   tree expression;
5654
5655   /* It might seem that we could simply parse the
5656      conditional-expression, and then check to see if it were
5657      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5658      one that the compiler can figure out is constant, possibly after
5659      doing some simplifications or optimizations.  The standard has a
5660      precise definition of constant-expression, and we must honor
5661      that, even though it is somewhat more restrictive.
5662
5663      For example:
5664
5665        int i[(2, 3)];
5666
5667      is not a legal declaration, because `(2, 3)' is not a
5668      constant-expression.  The `,' operator is forbidden in a
5669      constant-expression.  However, GCC's constant-folding machinery
5670      will fold this operation to an INTEGER_CST for `3'.  */
5671
5672   /* Save the old settings.  */
5673   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5674   saved_allow_non_integral_constant_expression_p
5675     = parser->allow_non_integral_constant_expression_p;
5676   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5677   /* We are now parsing a constant-expression.  */
5678   parser->integral_constant_expression_p = true;
5679   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5680   parser->non_integral_constant_expression_p = false;
5681   /* Although the grammar says "conditional-expression", we parse an
5682      "assignment-expression", which also permits "throw-expression"
5683      and the use of assignment operators.  In the case that
5684      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5685      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5686      actually essential that we look for an assignment-expression.
5687      For example, cp_parser_initializer_clauses uses this function to
5688      determine whether a particular assignment-expression is in fact
5689      constant.  */
5690   expression = cp_parser_assignment_expression (parser);
5691   /* Restore the old settings.  */
5692   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5693   parser->allow_non_integral_constant_expression_p
5694     = saved_allow_non_integral_constant_expression_p;
5695   if (allow_non_constant_p)
5696     *non_constant_p = parser->non_integral_constant_expression_p;
5697   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5698
5699   return expression;
5700 }
5701
5702 /* Parse __builtin_offsetof.
5703
5704    offsetof-expression:
5705      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5706
5707    offsetof-member-designator:
5708      id-expression
5709      | offsetof-member-designator "." id-expression
5710      | offsetof-member-designator "[" expression "]"
5711 */
5712
5713 static tree
5714 cp_parser_builtin_offsetof (cp_parser *parser)
5715 {
5716   int save_ice_p, save_non_ice_p;
5717   tree type, expr;
5718   cp_id_kind dummy;
5719
5720   /* We're about to accept non-integral-constant things, but will
5721      definitely yield an integral constant expression.  Save and
5722      restore these values around our local parsing.  */
5723   save_ice_p = parser->integral_constant_expression_p;
5724   save_non_ice_p = parser->non_integral_constant_expression_p;
5725
5726   /* Consume the "__builtin_offsetof" token.  */
5727   cp_lexer_consume_token (parser->lexer);
5728   /* Consume the opening `('.  */
5729   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5730   /* Parse the type-id.  */
5731   type = cp_parser_type_id (parser);
5732   /* Look for the `,'.  */
5733   cp_parser_require (parser, CPP_COMMA, "`,'");
5734
5735   /* Build the (type *)null that begins the traditional offsetof macro.  */
5736   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5737
5738   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5739   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5740                                                  true, &dummy);
5741   while (true)
5742     {
5743       cp_token *token = cp_lexer_peek_token (parser->lexer);
5744       switch (token->type)
5745         {
5746         case CPP_OPEN_SQUARE:
5747           /* offsetof-member-designator "[" expression "]" */
5748           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5749           break;
5750
5751         case CPP_DOT:
5752           /* offsetof-member-designator "." identifier */
5753           cp_lexer_consume_token (parser->lexer);
5754           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5755                                                          true, &dummy);
5756           break;
5757
5758         case CPP_CLOSE_PAREN:
5759           /* Consume the ")" token.  */
5760           cp_lexer_consume_token (parser->lexer);
5761           goto success;
5762
5763         default:
5764           /* Error.  We know the following require will fail, but
5765              that gives the proper error message.  */
5766           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5767           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5768           expr = error_mark_node;
5769           goto failure;
5770         }
5771     }
5772
5773  success:
5774   /* If we're processing a template, we can't finish the semantics yet.
5775      Otherwise we can fold the entire expression now.  */
5776   if (processing_template_decl)
5777     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5778   else
5779     expr = fold_offsetof (expr);
5780
5781  failure:
5782   parser->integral_constant_expression_p = save_ice_p;
5783   parser->non_integral_constant_expression_p = save_non_ice_p;
5784
5785   return expr;
5786 }
5787
5788 /* Statements [gram.stmt.stmt]  */
5789
5790 /* Parse a statement.
5791
5792    statement:
5793      labeled-statement
5794      expression-statement
5795      compound-statement
5796      selection-statement
5797      iteration-statement
5798      jump-statement
5799      declaration-statement
5800      try-block  */
5801
5802 static void
5803 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5804 {
5805   tree statement;
5806   cp_token *token;
5807   location_t statement_location;
5808
5809   /* There is no statement yet.  */
5810   statement = NULL_TREE;
5811   /* Peek at the next token.  */
5812   token = cp_lexer_peek_token (parser->lexer);
5813   /* Remember the location of the first token in the statement.  */
5814   statement_location = token->location;
5815   /* If this is a keyword, then that will often determine what kind of
5816      statement we have.  */
5817   if (token->type == CPP_KEYWORD)
5818     {
5819       enum rid keyword = token->keyword;
5820
5821       switch (keyword)
5822         {
5823         case RID_CASE:
5824         case RID_DEFAULT:
5825           statement = cp_parser_labeled_statement (parser,
5826                                                    in_statement_expr);
5827           break;
5828
5829         case RID_IF:
5830         case RID_SWITCH:
5831           statement = cp_parser_selection_statement (parser);
5832           break;
5833
5834         case RID_WHILE:
5835         case RID_DO:
5836         case RID_FOR:
5837           statement = cp_parser_iteration_statement (parser);
5838           break;
5839
5840         case RID_BREAK:
5841         case RID_CONTINUE:
5842         case RID_RETURN:
5843         case RID_GOTO:
5844           statement = cp_parser_jump_statement (parser);
5845           break;
5846
5847         case RID_TRY:
5848           statement = cp_parser_try_block (parser);
5849           break;
5850
5851         default:
5852           /* It might be a keyword like `int' that can start a
5853              declaration-statement.  */
5854           break;
5855         }
5856     }
5857   else if (token->type == CPP_NAME)
5858     {
5859       /* If the next token is a `:', then we are looking at a
5860          labeled-statement.  */
5861       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5862       if (token->type == CPP_COLON)
5863         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5864     }
5865   /* Anything that starts with a `{' must be a compound-statement.  */
5866   else if (token->type == CPP_OPEN_BRACE)
5867     statement = cp_parser_compound_statement (parser, NULL, false);
5868   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5869      a statement all its own.  */
5870   else if (token->type == CPP_PRAGMA)
5871     {
5872       cp_lexer_handle_pragma (parser->lexer);
5873       return;
5874     }
5875
5876   /* Everything else must be a declaration-statement or an
5877      expression-statement.  Try for the declaration-statement
5878      first, unless we are looking at a `;', in which case we know that
5879      we have an expression-statement.  */
5880   if (!statement)
5881     {
5882       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5883         {
5884           cp_parser_parse_tentatively (parser);
5885           /* Try to parse the declaration-statement.  */
5886           cp_parser_declaration_statement (parser);
5887           /* If that worked, we're done.  */
5888           if (cp_parser_parse_definitely (parser))
5889             return;
5890         }
5891       /* Look for an expression-statement instead.  */
5892       statement = cp_parser_expression_statement (parser, in_statement_expr);
5893     }
5894
5895   /* Set the line number for the statement.  */
5896   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5897     SET_EXPR_LOCATION (statement, statement_location);
5898 }
5899
5900 /* Parse a labeled-statement.
5901
5902    labeled-statement:
5903      identifier : statement
5904      case constant-expression : statement
5905      default : statement
5906
5907    GNU Extension:
5908
5909    labeled-statement:
5910      case constant-expression ... constant-expression : statement
5911
5912    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5913    For an ordinary label, returns a LABEL_EXPR.  */
5914
5915 static tree
5916 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5917 {
5918   cp_token *token;
5919   tree statement = error_mark_node;
5920
5921   /* The next token should be an identifier.  */
5922   token = cp_lexer_peek_token (parser->lexer);
5923   if (token->type != CPP_NAME
5924       && token->type != CPP_KEYWORD)
5925     {
5926       cp_parser_error (parser, "expected labeled-statement");
5927       return error_mark_node;
5928     }
5929
5930   switch (token->keyword)
5931     {
5932     case RID_CASE:
5933       {
5934         tree expr, expr_hi;
5935         cp_token *ellipsis;
5936
5937         /* Consume the `case' token.  */
5938         cp_lexer_consume_token (parser->lexer);
5939         /* Parse the constant-expression.  */
5940         expr = cp_parser_constant_expression (parser,
5941                                               /*allow_non_constant_p=*/false,
5942                                               NULL);
5943
5944         ellipsis = cp_lexer_peek_token (parser->lexer);
5945         if (ellipsis->type == CPP_ELLIPSIS)
5946           {
5947             /* Consume the `...' token.  */
5948             cp_lexer_consume_token (parser->lexer);
5949             expr_hi =
5950               cp_parser_constant_expression (parser,
5951                                              /*allow_non_constant_p=*/false,
5952                                              NULL);
5953             /* We don't need to emit warnings here, as the common code
5954                will do this for us.  */
5955           }
5956         else
5957           expr_hi = NULL_TREE;
5958
5959         if (!parser->in_switch_statement_p)
5960           error ("case label `%E' not within a switch statement", expr);
5961         else
5962           statement = finish_case_label (expr, expr_hi);
5963       }
5964       break;
5965
5966     case RID_DEFAULT:
5967       /* Consume the `default' token.  */
5968       cp_lexer_consume_token (parser->lexer);
5969       if (!parser->in_switch_statement_p)
5970         error ("case label not within a switch statement");
5971       else
5972         statement = finish_case_label (NULL_TREE, NULL_TREE);
5973       break;
5974
5975     default:
5976       /* Anything else must be an ordinary label.  */
5977       statement = finish_label_stmt (cp_parser_identifier (parser));
5978       break;
5979     }
5980
5981   /* Require the `:' token.  */
5982   cp_parser_require (parser, CPP_COLON, "`:'");
5983   /* Parse the labeled statement.  */
5984   cp_parser_statement (parser, in_statement_expr);
5985
5986   /* Return the label, in the case of a `case' or `default' label.  */
5987   return statement;
5988 }
5989
5990 /* Parse an expression-statement.
5991
5992    expression-statement:
5993      expression [opt] ;
5994
5995    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5996    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5997    indicates whether this expression-statement is part of an
5998    expression statement.  */
5999
6000 static tree
6001 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6002 {
6003   tree statement = NULL_TREE;
6004
6005   /* If the next token is a ';', then there is no expression
6006      statement.  */
6007   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6008     statement = cp_parser_expression (parser);
6009
6010   /* Consume the final `;'.  */
6011   cp_parser_consume_semicolon_at_end_of_statement (parser);
6012
6013   if (in_statement_expr
6014       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6015     {
6016       /* This is the final expression statement of a statement
6017          expression.  */
6018       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6019     }
6020   else if (statement)
6021     statement = finish_expr_stmt (statement);
6022   else
6023     finish_stmt ();
6024
6025   return statement;
6026 }
6027
6028 /* Parse a compound-statement.
6029
6030    compound-statement:
6031      { statement-seq [opt] }
6032
6033    Returns a tree representing the statement.  */
6034
6035 static tree
6036 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6037                               bool in_try)
6038 {
6039   tree compound_stmt;
6040
6041   /* Consume the `{'.  */
6042   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6043     return error_mark_node;
6044   /* Begin the compound-statement.  */
6045   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6046   /* Parse an (optional) statement-seq.  */
6047   cp_parser_statement_seq_opt (parser, in_statement_expr);
6048   /* Finish the compound-statement.  */
6049   finish_compound_stmt (compound_stmt);
6050   /* Consume the `}'.  */
6051   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6052
6053   return compound_stmt;
6054 }
6055
6056 /* Parse an (optional) statement-seq.
6057
6058    statement-seq:
6059      statement
6060      statement-seq [opt] statement  */
6061
6062 static void
6063 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6064 {
6065   /* Scan statements until there aren't any more.  */
6066   while (true)
6067     {
6068       /* If we're looking at a `}', then we've run out of statements.  */
6069       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6070           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6071         break;
6072
6073       /* Parse the statement.  */
6074       cp_parser_statement (parser, in_statement_expr);
6075     }
6076 }
6077
6078 /* Parse a selection-statement.
6079
6080    selection-statement:
6081      if ( condition ) statement
6082      if ( condition ) statement else statement
6083      switch ( condition ) statement
6084
6085    Returns the new IF_STMT or SWITCH_STMT.  */
6086
6087 static tree
6088 cp_parser_selection_statement (cp_parser* parser)
6089 {
6090   cp_token *token;
6091   enum rid keyword;
6092
6093   /* Peek at the next token.  */
6094   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6095
6096   /* See what kind of keyword it is.  */
6097   keyword = token->keyword;
6098   switch (keyword)
6099     {
6100     case RID_IF:
6101     case RID_SWITCH:
6102       {
6103         tree statement;
6104         tree condition;
6105
6106         /* Look for the `('.  */
6107         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6108           {
6109             cp_parser_skip_to_end_of_statement (parser);
6110             return error_mark_node;
6111           }
6112
6113         /* Begin the selection-statement.  */
6114         if (keyword == RID_IF)
6115           statement = begin_if_stmt ();
6116         else
6117           statement = begin_switch_stmt ();
6118
6119         /* Parse the condition.  */
6120         condition = cp_parser_condition (parser);
6121         /* Look for the `)'.  */
6122         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6123           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6124                                                  /*consume_paren=*/true);
6125
6126         if (keyword == RID_IF)
6127           {
6128             /* Add the condition.  */
6129             finish_if_stmt_cond (condition, statement);
6130
6131             /* Parse the then-clause.  */
6132             cp_parser_implicitly_scoped_statement (parser);
6133             finish_then_clause (statement);
6134
6135             /* If the next token is `else', parse the else-clause.  */
6136             if (cp_lexer_next_token_is_keyword (parser->lexer,
6137                                                 RID_ELSE))
6138               {
6139                 /* Consume the `else' keyword.  */
6140                 cp_lexer_consume_token (parser->lexer);
6141                 begin_else_clause (statement);
6142                 /* Parse the else-clause.  */
6143                 cp_parser_implicitly_scoped_statement (parser);
6144                 finish_else_clause (statement);
6145               }
6146
6147             /* Now we're all done with the if-statement.  */
6148             finish_if_stmt (statement);
6149           }
6150         else
6151           {
6152             bool in_switch_statement_p;
6153
6154             /* Add the condition.  */
6155             finish_switch_cond (condition, statement);
6156
6157             /* Parse the body of the switch-statement.  */
6158             in_switch_statement_p = parser->in_switch_statement_p;
6159             parser->in_switch_statement_p = true;
6160             cp_parser_implicitly_scoped_statement (parser);
6161             parser->in_switch_statement_p = in_switch_statement_p;
6162
6163             /* Now we're all done with the switch-statement.  */
6164             finish_switch_stmt (statement);
6165           }
6166
6167         return statement;
6168       }
6169       break;
6170
6171     default:
6172       cp_parser_error (parser, "expected selection-statement");
6173       return error_mark_node;
6174     }
6175 }
6176
6177 /* Parse a condition.
6178
6179    condition:
6180      expression
6181      type-specifier-seq declarator = assignment-expression
6182
6183    GNU Extension:
6184
6185    condition:
6186      type-specifier-seq declarator asm-specification [opt]
6187        attributes [opt] = assignment-expression
6188
6189    Returns the expression that should be tested.  */
6190
6191 static tree
6192 cp_parser_condition (cp_parser* parser)
6193 {
6194   cp_decl_specifier_seq type_specifiers;
6195   const char *saved_message;
6196
6197   /* Try the declaration first.  */
6198   cp_parser_parse_tentatively (parser);
6199   /* New types are not allowed in the type-specifier-seq for a
6200      condition.  */
6201   saved_message = parser->type_definition_forbidden_message;
6202   parser->type_definition_forbidden_message
6203     = "types may not be defined in conditions";
6204   /* Parse the type-specifier-seq.  */
6205   cp_parser_type_specifier_seq (parser, &type_specifiers);
6206   /* Restore the saved message.  */
6207   parser->type_definition_forbidden_message = saved_message;
6208   /* If all is well, we might be looking at a declaration.  */
6209   if (!cp_parser_error_occurred (parser))
6210     {
6211       tree decl;
6212       tree asm_specification;
6213       tree attributes;
6214       cp_declarator *declarator;
6215       tree initializer = NULL_TREE;
6216
6217       /* Parse the declarator.  */
6218       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6219                                          /*ctor_dtor_or_conv_p=*/NULL,
6220                                          /*parenthesized_p=*/NULL);
6221       /* Parse the attributes.  */
6222       attributes = cp_parser_attributes_opt (parser);
6223       /* Parse the asm-specification.  */
6224       asm_specification = cp_parser_asm_specification_opt (parser);
6225       /* If the next token is not an `=', then we might still be
6226          looking at an expression.  For example:
6227
6228            if (A(a).x)
6229
6230          looks like a decl-specifier-seq and a declarator -- but then
6231          there is no `=', so this is an expression.  */
6232       cp_parser_require (parser, CPP_EQ, "`='");
6233       /* If we did see an `=', then we are looking at a declaration
6234          for sure.  */
6235       if (cp_parser_parse_definitely (parser))
6236         {
6237           bool pop_p;   
6238
6239           /* Create the declaration.  */
6240           decl = start_decl (declarator, &type_specifiers,
6241                              /*initialized_p=*/true,
6242                              attributes, /*prefix_attributes=*/NULL_TREE,
6243                              &pop_p);
6244           /* Parse the assignment-expression.  */
6245           initializer = cp_parser_assignment_expression (parser);
6246
6247           /* Process the initializer.  */
6248           cp_finish_decl (decl,
6249                           initializer,
6250                           asm_specification,
6251                           LOOKUP_ONLYCONVERTING);
6252
6253           if (pop_p)
6254             pop_scope (DECL_CONTEXT (decl));
6255
6256           return convert_from_reference (decl);
6257         }
6258     }
6259   /* If we didn't even get past the declarator successfully, we are
6260      definitely not looking at a declaration.  */
6261   else
6262     cp_parser_abort_tentative_parse (parser);
6263
6264   /* Otherwise, we are looking at an expression.  */
6265   return cp_parser_expression (parser);
6266 }
6267
6268 /* Parse an iteration-statement.
6269
6270    iteration-statement:
6271      while ( condition ) statement
6272      do statement while ( expression ) ;
6273      for ( for-init-statement condition [opt] ; expression [opt] )
6274        statement
6275
6276    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6277
6278 static tree
6279 cp_parser_iteration_statement (cp_parser* parser)
6280 {
6281   cp_token *token;
6282   enum rid keyword;
6283   tree statement;
6284   bool in_iteration_statement_p;
6285
6286
6287   /* Peek at the next token.  */
6288   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6289   if (!token)
6290     return error_mark_node;
6291
6292   /* Remember whether or not we are already within an iteration
6293      statement.  */
6294   in_iteration_statement_p = parser->in_iteration_statement_p;
6295
6296   /* See what kind of keyword it is.  */
6297   keyword = token->keyword;
6298   switch (keyword)
6299     {
6300     case RID_WHILE:
6301       {
6302         tree condition;
6303
6304         /* Begin the while-statement.  */
6305         statement = begin_while_stmt ();
6306         /* Look for the `('.  */
6307         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6308         /* Parse the condition.  */
6309         condition = cp_parser_condition (parser);
6310         finish_while_stmt_cond (condition, statement);
6311         /* Look for the `)'.  */
6312         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6313         /* Parse the dependent statement.  */
6314         parser->in_iteration_statement_p = true;
6315         cp_parser_already_scoped_statement (parser);
6316         parser->in_iteration_statement_p = in_iteration_statement_p;
6317         /* We're done with the while-statement.  */
6318         finish_while_stmt (statement);
6319       }
6320       break;
6321
6322     case RID_DO:
6323       {
6324         tree expression;
6325
6326         /* Begin the do-statement.  */
6327         statement = begin_do_stmt ();
6328         /* Parse the body of the do-statement.  */
6329         parser->in_iteration_statement_p = true;
6330         cp_parser_implicitly_scoped_statement (parser);
6331         parser->in_iteration_statement_p = in_iteration_statement_p;
6332         finish_do_body (statement);
6333         /* Look for the `while' keyword.  */
6334         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6335         /* Look for the `('.  */
6336         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6337         /* Parse the expression.  */
6338         expression = cp_parser_expression (parser);
6339         /* We're done with the do-statement.  */
6340         finish_do_stmt (expression, statement);
6341         /* Look for the `)'.  */
6342         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6343         /* Look for the `;'.  */
6344         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6345       }
6346       break;
6347
6348     case RID_FOR:
6349       {
6350         tree condition = NULL_TREE;
6351         tree expression = NULL_TREE;
6352
6353         /* Begin the for-statement.  */
6354         statement = begin_for_stmt ();
6355         /* Look for the `('.  */
6356         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6357         /* Parse the initialization.  */
6358         cp_parser_for_init_statement (parser);
6359         finish_for_init_stmt (statement);
6360
6361         /* If there's a condition, process it.  */
6362         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6363           condition = cp_parser_condition (parser);
6364         finish_for_cond (condition, statement);
6365         /* Look for the `;'.  */
6366         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6367
6368         /* If there's an expression, process it.  */
6369         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6370           expression = cp_parser_expression (parser);
6371         finish_for_expr (expression, statement);
6372         /* Look for the `)'.  */
6373         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6374
6375         /* Parse the body of the for-statement.  */
6376         parser->in_iteration_statement_p = true;
6377         cp_parser_already_scoped_statement (parser);
6378         parser->in_iteration_statement_p = in_iteration_statement_p;
6379
6380         /* We're done with the for-statement.  */
6381         finish_for_stmt (statement);
6382       }
6383       break;
6384
6385     default:
6386       cp_parser_error (parser, "expected iteration-statement");
6387       statement = error_mark_node;
6388       break;
6389     }
6390
6391   return statement;
6392 }
6393
6394 /* Parse a for-init-statement.
6395
6396    for-init-statement:
6397      expression-statement
6398      simple-declaration  */
6399
6400 static void
6401 cp_parser_for_init_statement (cp_parser* parser)
6402 {
6403   /* If the next token is a `;', then we have an empty
6404      expression-statement.  Grammatically, this is also a
6405      simple-declaration, but an invalid one, because it does not
6406      declare anything.  Therefore, if we did not handle this case
6407      specially, we would issue an error message about an invalid
6408      declaration.  */
6409   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6410     {
6411       /* We're going to speculatively look for a declaration, falling back
6412          to an expression, if necessary.  */
6413       cp_parser_parse_tentatively (parser);
6414       /* Parse the declaration.  */
6415       cp_parser_simple_declaration (parser,
6416                                     /*function_definition_allowed_p=*/false);
6417       /* If the tentative parse failed, then we shall need to look for an
6418          expression-statement.  */
6419       if (cp_parser_parse_definitely (parser))
6420         return;
6421     }
6422
6423   cp_parser_expression_statement (parser, false);
6424 }
6425
6426 /* Parse a jump-statement.
6427
6428    jump-statement:
6429      break ;
6430      continue ;
6431      return expression [opt] ;
6432      goto identifier ;
6433
6434    GNU extension:
6435
6436    jump-statement:
6437      goto * expression ;
6438
6439    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6440
6441 static tree
6442 cp_parser_jump_statement (cp_parser* parser)
6443 {
6444   tree statement = error_mark_node;
6445   cp_token *token;
6446   enum rid keyword;
6447
6448   /* Peek at the next token.  */
6449   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6450   if (!token)
6451     return error_mark_node;
6452
6453   /* See what kind of keyword it is.  */
6454   keyword = token->keyword;
6455   switch (keyword)
6456     {
6457     case RID_BREAK:
6458       if (!parser->in_switch_statement_p
6459           && !parser->in_iteration_statement_p)
6460         {
6461           error ("break statement not within loop or switch");
6462           statement = error_mark_node;
6463         }
6464       else
6465         statement = finish_break_stmt ();
6466       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6467       break;
6468
6469     case RID_CONTINUE:
6470       if (!parser->in_iteration_statement_p)
6471         {
6472           error ("continue statement not within a loop");
6473           statement = error_mark_node;
6474         }
6475       else
6476         statement = finish_continue_stmt ();
6477       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6478       break;
6479
6480     case RID_RETURN:
6481       {
6482         tree expr;
6483
6484         /* If the next token is a `;', then there is no
6485            expression.  */
6486         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6487           expr = cp_parser_expression (parser);
6488         else
6489           expr = NULL_TREE;
6490         /* Build the return-statement.  */
6491         statement = finish_return_stmt (expr);
6492         /* Look for the final `;'.  */
6493         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6494       }
6495       break;
6496
6497     case RID_GOTO:
6498       /* Create the goto-statement.  */
6499       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6500         {
6501           /* Issue a warning about this use of a GNU extension.  */
6502           if (pedantic)
6503             pedwarn ("ISO C++ forbids computed gotos");
6504           /* Consume the '*' token.  */
6505           cp_lexer_consume_token (parser->lexer);
6506           /* Parse the dependent expression.  */
6507           finish_goto_stmt (cp_parser_expression (parser));
6508         }
6509       else
6510         finish_goto_stmt (cp_parser_identifier (parser));
6511       /* Look for the final `;'.  */
6512       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6513       break;
6514
6515     default:
6516       cp_parser_error (parser, "expected jump-statement");
6517       break;
6518     }
6519
6520   return statement;
6521 }
6522
6523 /* Parse a declaration-statement.
6524
6525    declaration-statement:
6526      block-declaration  */
6527
6528 static void
6529 cp_parser_declaration_statement (cp_parser* parser)
6530 {
6531   void *p;
6532
6533   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6534   p = obstack_alloc (&declarator_obstack, 0);
6535
6536  /* Parse the block-declaration.  */
6537   cp_parser_block_declaration (parser, /*statement_p=*/true);
6538
6539   /* Free any declarators allocated.  */
6540   obstack_free (&declarator_obstack, p);
6541
6542   /* Finish off the statement.  */
6543   finish_stmt ();
6544 }
6545
6546 /* Some dependent statements (like `if (cond) statement'), are
6547    implicitly in their own scope.  In other words, if the statement is
6548    a single statement (as opposed to a compound-statement), it is
6549    none-the-less treated as if it were enclosed in braces.  Any
6550    declarations appearing in the dependent statement are out of scope
6551    after control passes that point.  This function parses a statement,
6552    but ensures that is in its own scope, even if it is not a
6553    compound-statement.
6554
6555    Returns the new statement.  */
6556
6557 static tree
6558 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6559 {
6560   tree statement;
6561
6562   /* If the token is not a `{', then we must take special action.  */
6563   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6564     {
6565       /* Create a compound-statement.  */
6566       statement = begin_compound_stmt (0);
6567       /* Parse the dependent-statement.  */
6568       cp_parser_statement (parser, false);
6569       /* Finish the dummy compound-statement.  */
6570       finish_compound_stmt (statement);
6571     }
6572   /* Otherwise, we simply parse the statement directly.  */
6573   else
6574     statement = cp_parser_compound_statement (parser, NULL, false);
6575
6576   /* Return the statement.  */
6577   return statement;
6578 }
6579
6580 /* For some dependent statements (like `while (cond) statement'), we
6581    have already created a scope.  Therefore, even if the dependent
6582    statement is a compound-statement, we do not want to create another
6583    scope.  */
6584
6585 static void
6586 cp_parser_already_scoped_statement (cp_parser* parser)
6587 {
6588   /* If the token is a `{', then we must take special action.  */
6589   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6590     cp_parser_statement (parser, false);
6591   else
6592     {
6593       /* Avoid calling cp_parser_compound_statement, so that we
6594          don't create a new scope.  Do everything else by hand.  */
6595       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6596       cp_parser_statement_seq_opt (parser, false);
6597       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6598     }
6599 }
6600
6601 /* Declarations [gram.dcl.dcl] */
6602
6603 /* Parse an optional declaration-sequence.
6604
6605    declaration-seq:
6606      declaration
6607      declaration-seq declaration  */
6608
6609 static void
6610 cp_parser_declaration_seq_opt (cp_parser* parser)
6611 {
6612   while (true)
6613     {
6614       cp_token *token;
6615
6616       token = cp_lexer_peek_token (parser->lexer);
6617
6618       if (token->type == CPP_CLOSE_BRACE
6619           || token->type == CPP_EOF)
6620         break;
6621
6622       if (token->type == CPP_SEMICOLON)
6623         {
6624           /* A declaration consisting of a single semicolon is
6625              invalid.  Allow it unless we're being pedantic.  */
6626           cp_lexer_consume_token (parser->lexer);
6627           if (pedantic && !in_system_header)
6628             pedwarn ("extra %<;%>");
6629           continue;
6630         }
6631
6632       if (token->type == CPP_PRAGMA)
6633         {
6634           /* A top-level declaration can consist solely of a #pragma.
6635              A nested declaration cannot, so this is done here and not
6636              in cp_parser_declaration.  (A #pragma at block scope is
6637              handled in cp_parser_statement.)  */
6638           cp_lexer_handle_pragma (parser->lexer);
6639           continue;
6640         }
6641
6642       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6643          parser to enter or exit implicit `extern "C"' blocks.  */
6644       while (pending_lang_change > 0)
6645         {
6646           push_lang_context (lang_name_c);
6647           --pending_lang_change;
6648         }
6649       while (pending_lang_change < 0)
6650         {
6651           pop_lang_context ();
6652           ++pending_lang_change;
6653         }
6654
6655       /* Parse the declaration itself.  */
6656       cp_parser_declaration (parser);
6657     }
6658 }
6659
6660 /* Parse a declaration.
6661
6662    declaration:
6663      block-declaration
6664      function-definition
6665      template-declaration
6666      explicit-instantiation
6667      explicit-specialization
6668      linkage-specification
6669      namespace-definition
6670
6671    GNU extension:
6672
6673    declaration:
6674       __extension__ declaration */
6675
6676 static void
6677 cp_parser_declaration (cp_parser* parser)
6678 {
6679   cp_token token1;
6680   cp_token token2;
6681   int saved_pedantic;
6682   void *p;
6683
6684   /* Check for the `__extension__' keyword.  */
6685   if (cp_parser_extension_opt (parser, &saved_pedantic))
6686     {
6687       /* Parse the qualified declaration.  */
6688       cp_parser_declaration (parser);
6689       /* Restore the PEDANTIC flag.  */
6690       pedantic = saved_pedantic;
6691
6692       return;
6693     }
6694
6695   /* Try to figure out what kind of declaration is present.  */
6696   token1 = *cp_lexer_peek_token (parser->lexer);
6697
6698   if (token1.type != CPP_EOF)
6699     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6700
6701   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6702   p = obstack_alloc (&declarator_obstack, 0);
6703
6704   /* If the next token is `extern' and the following token is a string
6705      literal, then we have a linkage specification.  */
6706   if (token1.keyword == RID_EXTERN
6707       && cp_parser_is_string_literal (&token2))
6708     cp_parser_linkage_specification (parser);
6709   /* If the next token is `template', then we have either a template
6710      declaration, an explicit instantiation, or an explicit
6711      specialization.  */
6712   else if (token1.keyword == RID_TEMPLATE)
6713     {
6714       /* `template <>' indicates a template specialization.  */
6715       if (token2.type == CPP_LESS
6716           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6717         cp_parser_explicit_specialization (parser);
6718       /* `template <' indicates a template declaration.  */
6719       else if (token2.type == CPP_LESS)
6720         cp_parser_template_declaration (parser, /*member_p=*/false);
6721       /* Anything else must be an explicit instantiation.  */
6722       else
6723         cp_parser_explicit_instantiation (parser);
6724     }
6725   /* If the next token is `export', then we have a template
6726      declaration.  */
6727   else if (token1.keyword == RID_EXPORT)
6728     cp_parser_template_declaration (parser, /*member_p=*/false);
6729   /* If the next token is `extern', 'static' or 'inline' and the one
6730      after that is `template', we have a GNU extended explicit
6731      instantiation directive.  */
6732   else if (cp_parser_allow_gnu_extensions_p (parser)
6733            && (token1.keyword == RID_EXTERN
6734                || token1.keyword == RID_STATIC
6735                || token1.keyword == RID_INLINE)
6736            && token2.keyword == RID_TEMPLATE)
6737     cp_parser_explicit_instantiation (parser);
6738   /* If the next token is `namespace', check for a named or unnamed
6739      namespace definition.  */
6740   else if (token1.keyword == RID_NAMESPACE
6741            && (/* A named namespace definition.  */
6742                (token2.type == CPP_NAME
6743                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6744                     == CPP_OPEN_BRACE))
6745                /* An unnamed namespace definition.  */
6746                || token2.type == CPP_OPEN_BRACE))
6747     cp_parser_namespace_definition (parser);
6748   /* We must have either a block declaration or a function
6749      definition.  */
6750   else
6751     /* Try to parse a block-declaration, or a function-definition.  */
6752     cp_parser_block_declaration (parser, /*statement_p=*/false);
6753
6754   /* Free any declarators allocated.  */
6755   obstack_free (&declarator_obstack, p);
6756 }
6757
6758 /* Parse a block-declaration.
6759
6760    block-declaration:
6761      simple-declaration
6762      asm-definition
6763      namespace-alias-definition
6764      using-declaration
6765      using-directive
6766
6767    GNU Extension:
6768
6769    block-declaration:
6770      __extension__ block-declaration
6771      label-declaration
6772
6773    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6774    part of a declaration-statement.  */
6775
6776 static void
6777 cp_parser_block_declaration (cp_parser *parser,
6778                              bool      statement_p)
6779 {
6780   cp_token *token1;
6781   int saved_pedantic;
6782
6783   /* Check for the `__extension__' keyword.  */
6784   if (cp_parser_extension_opt (parser, &saved_pedantic))
6785     {
6786       /* Parse the qualified declaration.  */
6787       cp_parser_block_declaration (parser, statement_p);
6788       /* Restore the PEDANTIC flag.  */
6789       pedantic = saved_pedantic;
6790
6791       return;
6792     }
6793
6794   /* Peek at the next token to figure out which kind of declaration is
6795      present.  */
6796   token1 = cp_lexer_peek_token (parser->lexer);
6797
6798   /* If the next keyword is `asm', we have an asm-definition.  */
6799   if (token1->keyword == RID_ASM)
6800     {
6801       if (statement_p)
6802         cp_parser_commit_to_tentative_parse (parser);
6803       cp_parser_asm_definition (parser);
6804     }
6805   /* If the next keyword is `namespace', we have a
6806      namespace-alias-definition.  */
6807   else if (token1->keyword == RID_NAMESPACE)
6808     cp_parser_namespace_alias_definition (parser);
6809   /* If the next keyword is `using', we have either a
6810      using-declaration or a using-directive.  */
6811   else if (token1->keyword == RID_USING)
6812     {
6813       cp_token *token2;
6814
6815       if (statement_p)
6816         cp_parser_commit_to_tentative_parse (parser);
6817       /* If the token after `using' is `namespace', then we have a
6818          using-directive.  */
6819       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6820       if (token2->keyword == RID_NAMESPACE)
6821         cp_parser_using_directive (parser);
6822       /* Otherwise, it's a using-declaration.  */
6823       else
6824         cp_parser_using_declaration (parser);
6825     }
6826   /* If the next keyword is `__label__' we have a label declaration.  */
6827   else if (token1->keyword == RID_LABEL)
6828     {
6829       if (statement_p)
6830         cp_parser_commit_to_tentative_parse (parser);
6831       cp_parser_label_declaration (parser);
6832     }
6833   /* Anything else must be a simple-declaration.  */
6834   else
6835     cp_parser_simple_declaration (parser, !statement_p);
6836 }
6837
6838 /* Parse a simple-declaration.
6839
6840    simple-declaration:
6841      decl-specifier-seq [opt] init-declarator-list [opt] ;
6842
6843    init-declarator-list:
6844      init-declarator
6845      init-declarator-list , init-declarator
6846
6847    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6848    function-definition as a simple-declaration.  */
6849
6850 static void
6851 cp_parser_simple_declaration (cp_parser* parser,
6852                               bool function_definition_allowed_p)
6853 {
6854   cp_decl_specifier_seq decl_specifiers;
6855   int declares_class_or_enum;
6856   bool saw_declarator;
6857
6858   /* Defer access checks until we know what is being declared; the
6859      checks for names appearing in the decl-specifier-seq should be
6860      done as if we were in the scope of the thing being declared.  */
6861   push_deferring_access_checks (dk_deferred);
6862
6863   /* Parse the decl-specifier-seq.  We have to keep track of whether
6864      or not the decl-specifier-seq declares a named class or
6865      enumeration type, since that is the only case in which the
6866      init-declarator-list is allowed to be empty.
6867
6868      [dcl.dcl]
6869
6870      In a simple-declaration, the optional init-declarator-list can be
6871      omitted only when declaring a class or enumeration, that is when
6872      the decl-specifier-seq contains either a class-specifier, an
6873      elaborated-type-specifier, or an enum-specifier.  */
6874   cp_parser_decl_specifier_seq (parser,
6875                                 CP_PARSER_FLAGS_OPTIONAL,
6876                                 &decl_specifiers,
6877                                 &declares_class_or_enum);
6878   /* We no longer need to defer access checks.  */
6879   stop_deferring_access_checks ();
6880
6881   /* In a block scope, a valid declaration must always have a
6882      decl-specifier-seq.  By not trying to parse declarators, we can
6883      resolve the declaration/expression ambiguity more quickly.  */
6884   if (!function_definition_allowed_p
6885       && !decl_specifiers.any_specifiers_p)
6886     {
6887       cp_parser_error (parser, "expected declaration");
6888       goto done;
6889     }
6890
6891   /* If the next two tokens are both identifiers, the code is
6892      erroneous. The usual cause of this situation is code like:
6893
6894        T t;
6895
6896      where "T" should name a type -- but does not.  */
6897   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6898     {
6899       /* If parsing tentatively, we should commit; we really are
6900          looking at a declaration.  */
6901       cp_parser_commit_to_tentative_parse (parser);
6902       /* Give up.  */
6903       goto done;
6904     }
6905   
6906   /* If we have seen at least one decl-specifier, and the next token
6907      is not a parenthesis, then we must be looking at a declaration.
6908      (After "int (" we might be looking at a functional cast.)  */
6909   if (decl_specifiers.any_specifiers_p 
6910       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6911     cp_parser_commit_to_tentative_parse (parser);
6912
6913   /* Keep going until we hit the `;' at the end of the simple
6914      declaration.  */
6915   saw_declarator = false;
6916   while (cp_lexer_next_token_is_not (parser->lexer,
6917                                      CPP_SEMICOLON))
6918     {
6919       cp_token *token;
6920       bool function_definition_p;
6921       tree decl;
6922
6923       saw_declarator = true;
6924       /* Parse the init-declarator.  */
6925       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6926                                         function_definition_allowed_p,
6927                                         /*member_p=*/false,
6928                                         declares_class_or_enum,
6929                                         &function_definition_p);
6930       /* If an error occurred while parsing tentatively, exit quickly.
6931          (That usually happens when in the body of a function; each
6932          statement is treated as a declaration-statement until proven
6933          otherwise.)  */
6934       if (cp_parser_error_occurred (parser))
6935         goto done;
6936       /* Handle function definitions specially.  */
6937       if (function_definition_p)
6938         {
6939           /* If the next token is a `,', then we are probably
6940              processing something like:
6941
6942                void f() {}, *p;
6943
6944              which is erroneous.  */
6945           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6946             error ("mixing declarations and function-definitions is forbidden");
6947           /* Otherwise, we're done with the list of declarators.  */
6948           else
6949             {
6950               pop_deferring_access_checks ();
6951               return;
6952             }
6953         }
6954       /* The next token should be either a `,' or a `;'.  */
6955       token = cp_lexer_peek_token (parser->lexer);
6956       /* If it's a `,', there are more declarators to come.  */
6957       if (token->type == CPP_COMMA)
6958         cp_lexer_consume_token (parser->lexer);
6959       /* If it's a `;', we are done.  */
6960       else if (token->type == CPP_SEMICOLON)
6961         break;
6962       /* Anything else is an error.  */
6963       else
6964         {
6965           /* If we have already issued an error message we don't need
6966              to issue another one.  */
6967           if (decl != error_mark_node
6968               || (cp_parser_parsing_tentatively (parser)
6969                   && !cp_parser_committed_to_tentative_parse (parser)))
6970             cp_parser_error (parser, "expected `,' or `;'");
6971           /* Skip tokens until we reach the end of the statement.  */
6972           cp_parser_skip_to_end_of_statement (parser);
6973           /* If the next token is now a `;', consume it.  */
6974           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6975             cp_lexer_consume_token (parser->lexer);
6976           goto done;
6977         }
6978       /* After the first time around, a function-definition is not
6979          allowed -- even if it was OK at first.  For example:
6980
6981            int i, f() {}
6982
6983          is not valid.  */
6984       function_definition_allowed_p = false;
6985     }
6986
6987   /* Issue an error message if no declarators are present, and the
6988      decl-specifier-seq does not itself declare a class or
6989      enumeration.  */
6990   if (!saw_declarator)
6991     {
6992       if (cp_parser_declares_only_class_p (parser))
6993         shadow_tag (&decl_specifiers);
6994       /* Perform any deferred access checks.  */
6995       perform_deferred_access_checks ();
6996     }
6997
6998   /* Consume the `;'.  */
6999   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7000
7001  done:
7002   pop_deferring_access_checks ();
7003 }
7004
7005 /* Parse a decl-specifier-seq.
7006
7007    decl-specifier-seq:
7008      decl-specifier-seq [opt] decl-specifier
7009
7010    decl-specifier:
7011      storage-class-specifier
7012      type-specifier
7013      function-specifier
7014      friend
7015      typedef
7016
7017    GNU Extension:
7018
7019    decl-specifier:
7020      attributes
7021
7022    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7023
7024    The parser flags FLAGS is used to control type-specifier parsing.
7025
7026    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7027    flags:
7028
7029      1: one of the decl-specifiers is an elaborated-type-specifier
7030         (i.e., a type declaration)
7031      2: one of the decl-specifiers is an enum-specifier or a
7032         class-specifier (i.e., a type definition)
7033
7034    */
7035
7036 static void
7037 cp_parser_decl_specifier_seq (cp_parser* parser,
7038                               cp_parser_flags flags,
7039                               cp_decl_specifier_seq *decl_specs,
7040                               int* declares_class_or_enum)
7041 {
7042   bool constructor_possible_p = !parser->in_declarator_p;
7043
7044   /* Clear DECL_SPECS.  */
7045   clear_decl_specs (decl_specs);
7046
7047   /* Assume no class or enumeration type is declared.  */
7048   *declares_class_or_enum = 0;
7049
7050   /* Keep reading specifiers until there are no more to read.  */
7051   while (true)
7052     {
7053       bool constructor_p;
7054       bool found_decl_spec;
7055       cp_token *token;
7056
7057       /* Peek at the next token.  */
7058       token = cp_lexer_peek_token (parser->lexer);
7059       /* Handle attributes.  */
7060       if (token->keyword == RID_ATTRIBUTE)
7061         {
7062           /* Parse the attributes.  */
7063           decl_specs->attributes
7064             = chainon (decl_specs->attributes,
7065                        cp_parser_attributes_opt (parser));
7066           continue;
7067         }
7068       /* Assume we will find a decl-specifier keyword.  */
7069       found_decl_spec = true;
7070       /* If the next token is an appropriate keyword, we can simply
7071          add it to the list.  */
7072       switch (token->keyword)
7073         {
7074           /* decl-specifier:
7075                friend  */
7076         case RID_FRIEND:
7077           if (decl_specs->specs[(int) ds_friend]++)
7078             error ("duplicate `friend'");
7079           /* Consume the token.  */
7080           cp_lexer_consume_token (parser->lexer);
7081           break;
7082
7083           /* function-specifier:
7084                inline
7085                virtual
7086                explicit  */
7087         case RID_INLINE:
7088         case RID_VIRTUAL:
7089         case RID_EXPLICIT:
7090           cp_parser_function_specifier_opt (parser, decl_specs);
7091           break;
7092
7093           /* decl-specifier:
7094                typedef  */
7095         case RID_TYPEDEF:
7096           ++decl_specs->specs[(int) ds_typedef];
7097           /* Consume the token.  */
7098           cp_lexer_consume_token (parser->lexer);
7099           /* A constructor declarator cannot appear in a typedef.  */
7100           constructor_possible_p = false;
7101           /* The "typedef" keyword can only occur in a declaration; we
7102              may as well commit at this point.  */
7103           cp_parser_commit_to_tentative_parse (parser);
7104           break;
7105
7106           /* storage-class-specifier:
7107                auto
7108                register
7109                static
7110                extern
7111                mutable
7112
7113              GNU Extension:
7114                thread  */
7115         case RID_AUTO:
7116           /* Consume the token.  */
7117           cp_lexer_consume_token (parser->lexer);
7118           cp_parser_set_storage_class (decl_specs, sc_auto);
7119           break;
7120         case RID_REGISTER:
7121           /* Consume the token.  */
7122           cp_lexer_consume_token (parser->lexer);
7123           cp_parser_set_storage_class (decl_specs, sc_register);
7124           break;
7125         case RID_STATIC:
7126           /* Consume the token.  */
7127           cp_lexer_consume_token (parser->lexer);
7128           if (decl_specs->specs[(int) ds_thread])
7129             {
7130               error ("`__thread' before `static'");
7131               decl_specs->specs[(int) ds_thread] = 0;
7132             }
7133           cp_parser_set_storage_class (decl_specs, sc_static);
7134           break;
7135         case RID_EXTERN:
7136           /* Consume the token.  */
7137           cp_lexer_consume_token (parser->lexer);
7138           if (decl_specs->specs[(int) ds_thread])
7139             {
7140               error ("`__thread' before `extern'");
7141               decl_specs->specs[(int) ds_thread] = 0;
7142             }
7143           cp_parser_set_storage_class (decl_specs, sc_extern);
7144           break;
7145         case RID_MUTABLE:
7146           /* Consume the token.  */
7147           cp_lexer_consume_token (parser->lexer);
7148           cp_parser_set_storage_class (decl_specs, sc_mutable);
7149           break;
7150         case RID_THREAD:
7151           /* Consume the token.  */
7152           cp_lexer_consume_token (parser->lexer);
7153           ++decl_specs->specs[(int) ds_thread];
7154           break;
7155
7156         default:
7157           /* We did not yet find a decl-specifier yet.  */
7158           found_decl_spec = false;
7159           break;
7160         }
7161
7162       /* Constructors are a special case.  The `S' in `S()' is not a
7163          decl-specifier; it is the beginning of the declarator.  */
7164       constructor_p
7165         = (!found_decl_spec
7166            && constructor_possible_p
7167            && (cp_parser_constructor_declarator_p
7168                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7169
7170       /* If we don't have a DECL_SPEC yet, then we must be looking at
7171          a type-specifier.  */
7172       if (!found_decl_spec && !constructor_p)
7173         {
7174           int decl_spec_declares_class_or_enum;
7175           bool is_cv_qualifier;
7176           tree type_spec;
7177
7178           type_spec
7179             = cp_parser_type_specifier (parser, flags,
7180                                         decl_specs,
7181                                         /*is_declaration=*/true,
7182                                         &decl_spec_declares_class_or_enum,
7183                                         &is_cv_qualifier);
7184
7185           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7186
7187           /* If this type-specifier referenced a user-defined type
7188              (a typedef, class-name, etc.), then we can't allow any
7189              more such type-specifiers henceforth.
7190
7191              [dcl.spec]
7192
7193              The longest sequence of decl-specifiers that could
7194              possibly be a type name is taken as the
7195              decl-specifier-seq of a declaration.  The sequence shall
7196              be self-consistent as described below.
7197
7198              [dcl.type]
7199
7200              As a general rule, at most one type-specifier is allowed
7201              in the complete decl-specifier-seq of a declaration.  The
7202              only exceptions are the following:
7203
7204              -- const or volatile can be combined with any other
7205                 type-specifier.
7206
7207              -- signed or unsigned can be combined with char, long,
7208                 short, or int.
7209
7210              -- ..
7211
7212              Example:
7213
7214                typedef char* Pc;
7215                void g (const int Pc);
7216
7217              Here, Pc is *not* part of the decl-specifier seq; it's
7218              the declarator.  Therefore, once we see a type-specifier
7219              (other than a cv-qualifier), we forbid any additional
7220              user-defined types.  We *do* still allow things like `int
7221              int' to be considered a decl-specifier-seq, and issue the
7222              error message later.  */
7223           if (type_spec && !is_cv_qualifier)
7224             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7225           /* A constructor declarator cannot follow a type-specifier.  */
7226           if (type_spec)
7227             {
7228               constructor_possible_p = false;
7229               found_decl_spec = true;
7230             }
7231         }
7232
7233       /* If we still do not have a DECL_SPEC, then there are no more
7234          decl-specifiers.  */
7235       if (!found_decl_spec)
7236         break;
7237
7238       decl_specs->any_specifiers_p = true;
7239       /* After we see one decl-specifier, further decl-specifiers are
7240          always optional.  */
7241       flags |= CP_PARSER_FLAGS_OPTIONAL;
7242     }
7243
7244   /* Don't allow a friend specifier with a class definition.  */
7245   if (decl_specs->specs[(int) ds_friend] != 0
7246       && (*declares_class_or_enum & 2))
7247     error ("class definition may not be declared a friend");
7248 }
7249
7250 /* Parse an (optional) storage-class-specifier.
7251
7252    storage-class-specifier:
7253      auto
7254      register
7255      static
7256      extern
7257      mutable
7258
7259    GNU Extension:
7260
7261    storage-class-specifier:
7262      thread
7263
7264    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7265
7266 static tree
7267 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7268 {
7269   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7270     {
7271     case RID_AUTO:
7272     case RID_REGISTER:
7273     case RID_STATIC:
7274     case RID_EXTERN:
7275     case RID_MUTABLE:
7276     case RID_THREAD:
7277       /* Consume the token.  */
7278       return cp_lexer_consume_token (parser->lexer)->value;
7279
7280     default:
7281       return NULL_TREE;
7282     }
7283 }
7284
7285 /* Parse an (optional) function-specifier.
7286
7287    function-specifier:
7288      inline
7289      virtual
7290      explicit
7291
7292    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7293    Updates DECL_SPECS, if it is non-NULL.  */
7294
7295 static tree
7296 cp_parser_function_specifier_opt (cp_parser* parser,
7297                                   cp_decl_specifier_seq *decl_specs)
7298 {
7299   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7300     {
7301     case RID_INLINE:
7302       if (decl_specs)
7303         ++decl_specs->specs[(int) ds_inline];
7304       break;
7305
7306     case RID_VIRTUAL:
7307       if (decl_specs)
7308         ++decl_specs->specs[(int) ds_virtual];
7309       break;
7310
7311     case RID_EXPLICIT:
7312       if (decl_specs)
7313         ++decl_specs->specs[(int) ds_explicit];
7314       break;
7315
7316     default:
7317       return NULL_TREE;
7318     }
7319
7320   /* Consume the token.  */
7321   return cp_lexer_consume_token (parser->lexer)->value;
7322 }
7323
7324 /* Parse a linkage-specification.
7325
7326    linkage-specification:
7327      extern string-literal { declaration-seq [opt] }
7328      extern string-literal declaration  */
7329
7330 static void
7331 cp_parser_linkage_specification (cp_parser* parser)
7332 {
7333   tree linkage;
7334
7335   /* Look for the `extern' keyword.  */
7336   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7337
7338   /* Look for the string-literal.  */
7339   linkage = cp_parser_string_literal (parser, false, false);
7340
7341   /* Transform the literal into an identifier.  If the literal is a
7342      wide-character string, or contains embedded NULs, then we can't
7343      handle it as the user wants.  */
7344   if (strlen (TREE_STRING_POINTER (linkage))
7345       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7346     {
7347       cp_parser_error (parser, "invalid linkage-specification");
7348       /* Assume C++ linkage.  */
7349       linkage = lang_name_cplusplus;
7350     }
7351   else
7352     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7353
7354   /* We're now using the new linkage.  */
7355   push_lang_context (linkage);
7356
7357   /* If the next token is a `{', then we're using the first
7358      production.  */
7359   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7360     {
7361       /* Consume the `{' token.  */
7362       cp_lexer_consume_token (parser->lexer);
7363       /* Parse the declarations.  */
7364       cp_parser_declaration_seq_opt (parser);
7365       /* Look for the closing `}'.  */
7366       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7367     }
7368   /* Otherwise, there's just one declaration.  */
7369   else
7370     {
7371       bool saved_in_unbraced_linkage_specification_p;
7372
7373       saved_in_unbraced_linkage_specification_p
7374         = parser->in_unbraced_linkage_specification_p;
7375       parser->in_unbraced_linkage_specification_p = true;
7376       have_extern_spec = true;
7377       cp_parser_declaration (parser);
7378       have_extern_spec = false;
7379       parser->in_unbraced_linkage_specification_p
7380         = saved_in_unbraced_linkage_specification_p;
7381     }
7382
7383   /* We're done with the linkage-specification.  */
7384   pop_lang_context ();
7385 }
7386
7387 /* Special member functions [gram.special] */
7388
7389 /* Parse a conversion-function-id.
7390
7391    conversion-function-id:
7392      operator conversion-type-id
7393
7394    Returns an IDENTIFIER_NODE representing the operator.  */
7395
7396 static tree
7397 cp_parser_conversion_function_id (cp_parser* parser)
7398 {
7399   tree type;
7400   tree saved_scope;
7401   tree saved_qualifying_scope;
7402   tree saved_object_scope;
7403   bool pop_p = false;
7404
7405   /* Look for the `operator' token.  */
7406   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7407     return error_mark_node;
7408   /* When we parse the conversion-type-id, the current scope will be
7409      reset.  However, we need that information in able to look up the
7410      conversion function later, so we save it here.  */
7411   saved_scope = parser->scope;
7412   saved_qualifying_scope = parser->qualifying_scope;
7413   saved_object_scope = parser->object_scope;
7414   /* We must enter the scope of the class so that the names of
7415      entities declared within the class are available in the
7416      conversion-type-id.  For example, consider:
7417
7418        struct S {
7419          typedef int I;
7420          operator I();
7421        };
7422
7423        S::operator I() { ... }
7424
7425      In order to see that `I' is a type-name in the definition, we
7426      must be in the scope of `S'.  */
7427   if (saved_scope)
7428     pop_p = push_scope (saved_scope);
7429   /* Parse the conversion-type-id.  */
7430   type = cp_parser_conversion_type_id (parser);
7431   /* Leave the scope of the class, if any.  */
7432   if (pop_p)
7433     pop_scope (saved_scope);
7434   /* Restore the saved scope.  */
7435   parser->scope = saved_scope;
7436   parser->qualifying_scope = saved_qualifying_scope;
7437   parser->object_scope = saved_object_scope;
7438   /* If the TYPE is invalid, indicate failure.  */
7439   if (type == error_mark_node)
7440     return error_mark_node;
7441   return mangle_conv_op_name_for_type (type);
7442 }
7443
7444 /* Parse a conversion-type-id:
7445
7446    conversion-type-id:
7447      type-specifier-seq conversion-declarator [opt]
7448
7449    Returns the TYPE specified.  */
7450
7451 static tree
7452 cp_parser_conversion_type_id (cp_parser* parser)
7453 {
7454   tree attributes;
7455   cp_decl_specifier_seq type_specifiers;
7456   cp_declarator *declarator;
7457   tree type_specified;
7458
7459   /* Parse the attributes.  */
7460   attributes = cp_parser_attributes_opt (parser);
7461   /* Parse the type-specifiers.  */
7462   cp_parser_type_specifier_seq (parser, &type_specifiers);
7463   /* If that didn't work, stop.  */
7464   if (type_specifiers.type == error_mark_node)
7465     return error_mark_node;
7466   /* Parse the conversion-declarator.  */
7467   declarator = cp_parser_conversion_declarator_opt (parser);
7468
7469   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7470                                     /*initialized=*/0, &attributes);
7471   if (attributes)
7472     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7473   return type_specified;
7474 }
7475
7476 /* Parse an (optional) conversion-declarator.
7477
7478    conversion-declarator:
7479      ptr-operator conversion-declarator [opt]
7480
7481    */
7482
7483 static cp_declarator *
7484 cp_parser_conversion_declarator_opt (cp_parser* parser)
7485 {
7486   enum tree_code code;
7487   tree class_type;
7488   cp_cv_quals cv_quals;
7489
7490   /* We don't know if there's a ptr-operator next, or not.  */
7491   cp_parser_parse_tentatively (parser);
7492   /* Try the ptr-operator.  */
7493   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7494   /* If it worked, look for more conversion-declarators.  */
7495   if (cp_parser_parse_definitely (parser))
7496     {
7497       cp_declarator *declarator;
7498
7499       /* Parse another optional declarator.  */
7500       declarator = cp_parser_conversion_declarator_opt (parser);
7501
7502       /* Create the representation of the declarator.  */
7503       if (class_type)
7504         declarator = make_ptrmem_declarator (cv_quals, class_type,
7505                                              declarator);
7506       else if (code == INDIRECT_REF)
7507         declarator = make_pointer_declarator (cv_quals, declarator);
7508       else
7509         declarator = make_reference_declarator (cv_quals, declarator);
7510
7511       return declarator;
7512    }
7513
7514   return NULL;
7515 }
7516
7517 /* Parse an (optional) ctor-initializer.
7518
7519    ctor-initializer:
7520      : mem-initializer-list
7521
7522    Returns TRUE iff the ctor-initializer was actually present.  */
7523
7524 static bool
7525 cp_parser_ctor_initializer_opt (cp_parser* parser)
7526 {
7527   /* If the next token is not a `:', then there is no
7528      ctor-initializer.  */
7529   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7530     {
7531       /* Do default initialization of any bases and members.  */
7532       if (DECL_CONSTRUCTOR_P (current_function_decl))
7533         finish_mem_initializers (NULL_TREE);
7534
7535       return false;
7536     }
7537
7538   /* Consume the `:' token.  */
7539   cp_lexer_consume_token (parser->lexer);
7540   /* And the mem-initializer-list.  */
7541   cp_parser_mem_initializer_list (parser);
7542
7543   return true;
7544 }
7545
7546 /* Parse a mem-initializer-list.
7547
7548    mem-initializer-list:
7549      mem-initializer
7550      mem-initializer , mem-initializer-list  */
7551
7552 static void
7553 cp_parser_mem_initializer_list (cp_parser* parser)
7554 {
7555   tree mem_initializer_list = NULL_TREE;
7556
7557   /* Let the semantic analysis code know that we are starting the
7558      mem-initializer-list.  */
7559   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7560     error ("only constructors take base initializers");
7561
7562   /* Loop through the list.  */
7563   while (true)
7564     {
7565       tree mem_initializer;
7566
7567       /* Parse the mem-initializer.  */
7568       mem_initializer = cp_parser_mem_initializer (parser);
7569       /* Add it to the list, unless it was erroneous.  */
7570       if (mem_initializer)
7571         {
7572           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7573           mem_initializer_list = mem_initializer;
7574         }
7575       /* If the next token is not a `,', we're done.  */
7576       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7577         break;
7578       /* Consume the `,' token.  */
7579       cp_lexer_consume_token (parser->lexer);
7580     }
7581
7582   /* Perform semantic analysis.  */
7583   if (DECL_CONSTRUCTOR_P (current_function_decl))
7584     finish_mem_initializers (mem_initializer_list);
7585 }
7586
7587 /* Parse a mem-initializer.
7588
7589    mem-initializer:
7590      mem-initializer-id ( expression-list [opt] )
7591
7592    GNU extension:
7593
7594    mem-initializer:
7595      ( expression-list [opt] )
7596
7597    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7598    class) or FIELD_DECL (for a non-static data member) to initialize;
7599    the TREE_VALUE is the expression-list.  */
7600
7601 static tree
7602 cp_parser_mem_initializer (cp_parser* parser)
7603 {
7604   tree mem_initializer_id;
7605   tree expression_list;
7606   tree member;
7607
7608   /* Find out what is being initialized.  */
7609   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7610     {
7611       pedwarn ("anachronistic old-style base class initializer");
7612       mem_initializer_id = NULL_TREE;
7613     }
7614   else
7615     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7616   member = expand_member_init (mem_initializer_id);
7617   if (member && !DECL_P (member))
7618     in_base_initializer = 1;
7619
7620   expression_list
7621     = cp_parser_parenthesized_expression_list (parser, false,
7622                                                /*non_constant_p=*/NULL);
7623   if (!expression_list)
7624     expression_list = void_type_node;
7625
7626   in_base_initializer = 0;
7627
7628   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7629 }
7630
7631 /* Parse a mem-initializer-id.
7632
7633    mem-initializer-id:
7634      :: [opt] nested-name-specifier [opt] class-name
7635      identifier
7636
7637    Returns a TYPE indicating the class to be initializer for the first
7638    production.  Returns an IDENTIFIER_NODE indicating the data member
7639    to be initialized for the second production.  */
7640
7641 static tree
7642 cp_parser_mem_initializer_id (cp_parser* parser)
7643 {
7644   bool global_scope_p;
7645   bool nested_name_specifier_p;
7646   bool template_p = false;
7647   tree id;
7648
7649   /* `typename' is not allowed in this context ([temp.res]).  */
7650   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7651     {
7652       error ("keyword `typename' not allowed in this context (a qualified "
7653              "member initializer is implicitly a type)");
7654       cp_lexer_consume_token (parser->lexer);
7655     }
7656   /* Look for the optional `::' operator.  */
7657   global_scope_p
7658     = (cp_parser_global_scope_opt (parser,
7659                                    /*current_scope_valid_p=*/false)
7660        != NULL_TREE);
7661   /* Look for the optional nested-name-specifier.  The simplest way to
7662      implement:
7663
7664        [temp.res]
7665
7666        The keyword `typename' is not permitted in a base-specifier or
7667        mem-initializer; in these contexts a qualified name that
7668        depends on a template-parameter is implicitly assumed to be a
7669        type name.
7670
7671      is to assume that we have seen the `typename' keyword at this
7672      point.  */
7673   nested_name_specifier_p
7674     = (cp_parser_nested_name_specifier_opt (parser,
7675                                             /*typename_keyword_p=*/true,
7676                                             /*check_dependency_p=*/true,
7677                                             /*type_p=*/true,
7678                                             /*is_declaration=*/true)
7679        != NULL_TREE);
7680   if (nested_name_specifier_p)
7681     template_p = cp_parser_optional_template_keyword (parser);
7682   /* If there is a `::' operator or a nested-name-specifier, then we
7683      are definitely looking for a class-name.  */
7684   if (global_scope_p || nested_name_specifier_p)
7685     return cp_parser_class_name (parser,
7686                                  /*typename_keyword_p=*/true,
7687                                  /*template_keyword_p=*/template_p,
7688                                  /*type_p=*/false,
7689                                  /*check_dependency_p=*/true,
7690                                  /*class_head_p=*/false,
7691                                  /*is_declaration=*/true);
7692   /* Otherwise, we could also be looking for an ordinary identifier.  */
7693   cp_parser_parse_tentatively (parser);
7694   /* Try a class-name.  */
7695   id = cp_parser_class_name (parser,
7696                              /*typename_keyword_p=*/true,
7697                              /*template_keyword_p=*/false,
7698                              /*type_p=*/false,
7699                              /*check_dependency_p=*/true,
7700                              /*class_head_p=*/false,
7701                              /*is_declaration=*/true);
7702   /* If we found one, we're done.  */
7703   if (cp_parser_parse_definitely (parser))
7704     return id;
7705   /* Otherwise, look for an ordinary identifier.  */
7706   return cp_parser_identifier (parser);
7707 }
7708
7709 /* Overloading [gram.over] */
7710
7711 /* Parse an operator-function-id.
7712
7713    operator-function-id:
7714      operator operator
7715
7716    Returns an IDENTIFIER_NODE for the operator which is a
7717    human-readable spelling of the identifier, e.g., `operator +'.  */
7718
7719 static tree
7720 cp_parser_operator_function_id (cp_parser* parser)
7721 {
7722   /* Look for the `operator' keyword.  */
7723   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7724     return error_mark_node;
7725   /* And then the name of the operator itself.  */
7726   return cp_parser_operator (parser);
7727 }
7728
7729 /* Parse an operator.
7730
7731    operator:
7732      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7733      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7734      || ++ -- , ->* -> () []
7735
7736    GNU Extensions:
7737
7738    operator:
7739      <? >? <?= >?=
7740
7741    Returns an IDENTIFIER_NODE for the operator which is a
7742    human-readable spelling of the identifier, e.g., `operator +'.  */
7743
7744 static tree
7745 cp_parser_operator (cp_parser* parser)
7746 {
7747   tree id = NULL_TREE;
7748   cp_token *token;
7749
7750   /* Peek at the next token.  */
7751   token = cp_lexer_peek_token (parser->lexer);
7752   /* Figure out which operator we have.  */
7753   switch (token->type)
7754     {
7755     case CPP_KEYWORD:
7756       {
7757         enum tree_code op;
7758
7759         /* The keyword should be either `new' or `delete'.  */
7760         if (token->keyword == RID_NEW)
7761           op = NEW_EXPR;
7762         else if (token->keyword == RID_DELETE)
7763           op = DELETE_EXPR;
7764         else
7765           break;
7766
7767         /* Consume the `new' or `delete' token.  */
7768         cp_lexer_consume_token (parser->lexer);
7769
7770         /* Peek at the next token.  */
7771         token = cp_lexer_peek_token (parser->lexer);
7772         /* If it's a `[' token then this is the array variant of the
7773            operator.  */
7774         if (token->type == CPP_OPEN_SQUARE)
7775           {
7776             /* Consume the `[' token.  */
7777             cp_lexer_consume_token (parser->lexer);
7778             /* Look for the `]' token.  */
7779             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7780             id = ansi_opname (op == NEW_EXPR
7781                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7782           }
7783         /* Otherwise, we have the non-array variant.  */
7784         else
7785           id = ansi_opname (op);
7786
7787         return id;
7788       }
7789
7790     case CPP_PLUS:
7791       id = ansi_opname (PLUS_EXPR);
7792       break;
7793
7794     case CPP_MINUS:
7795       id = ansi_opname (MINUS_EXPR);
7796       break;
7797
7798     case CPP_MULT:
7799       id = ansi_opname (MULT_EXPR);
7800       break;
7801
7802     case CPP_DIV:
7803       id = ansi_opname (TRUNC_DIV_EXPR);
7804       break;
7805
7806     case CPP_MOD:
7807       id = ansi_opname (TRUNC_MOD_EXPR);
7808       break;
7809
7810     case CPP_XOR:
7811       id = ansi_opname (BIT_XOR_EXPR);
7812       break;
7813
7814     case CPP_AND:
7815       id = ansi_opname (BIT_AND_EXPR);
7816       break;
7817
7818     case CPP_OR:
7819       id = ansi_opname (BIT_IOR_EXPR);
7820       break;
7821
7822     case CPP_COMPL:
7823       id = ansi_opname (BIT_NOT_EXPR);
7824       break;
7825
7826     case CPP_NOT:
7827       id = ansi_opname (TRUTH_NOT_EXPR);
7828       break;
7829
7830     case CPP_EQ:
7831       id = ansi_assopname (NOP_EXPR);
7832       break;
7833
7834     case CPP_LESS:
7835       id = ansi_opname (LT_EXPR);
7836       break;
7837
7838     case CPP_GREATER:
7839       id = ansi_opname (GT_EXPR);
7840       break;
7841
7842     case CPP_PLUS_EQ:
7843       id = ansi_assopname (PLUS_EXPR);
7844       break;
7845
7846     case CPP_MINUS_EQ:
7847       id = ansi_assopname (MINUS_EXPR);
7848       break;
7849
7850     case CPP_MULT_EQ:
7851       id = ansi_assopname (MULT_EXPR);
7852       break;
7853
7854     case CPP_DIV_EQ:
7855       id = ansi_assopname (TRUNC_DIV_EXPR);
7856       break;
7857
7858     case CPP_MOD_EQ:
7859       id = ansi_assopname (TRUNC_MOD_EXPR);
7860       break;
7861
7862     case CPP_XOR_EQ:
7863       id = ansi_assopname (BIT_XOR_EXPR);
7864       break;
7865
7866     case CPP_AND_EQ:
7867       id = ansi_assopname (BIT_AND_EXPR);
7868       break;
7869
7870     case CPP_OR_EQ:
7871       id = ansi_assopname (BIT_IOR_EXPR);
7872       break;
7873
7874     case CPP_LSHIFT:
7875       id = ansi_opname (LSHIFT_EXPR);
7876       break;
7877
7878     case CPP_RSHIFT:
7879       id = ansi_opname (RSHIFT_EXPR);
7880       break;
7881
7882     case CPP_LSHIFT_EQ:
7883       id = ansi_assopname (LSHIFT_EXPR);
7884       break;
7885
7886     case CPP_RSHIFT_EQ:
7887       id = ansi_assopname (RSHIFT_EXPR);
7888       break;
7889
7890     case CPP_EQ_EQ:
7891       id = ansi_opname (EQ_EXPR);
7892       break;
7893
7894     case CPP_NOT_EQ:
7895       id = ansi_opname (NE_EXPR);
7896       break;
7897
7898     case CPP_LESS_EQ:
7899       id = ansi_opname (LE_EXPR);
7900       break;
7901
7902     case CPP_GREATER_EQ:
7903       id = ansi_opname (GE_EXPR);
7904       break;
7905
7906     case CPP_AND_AND:
7907       id = ansi_opname (TRUTH_ANDIF_EXPR);
7908       break;
7909
7910     case CPP_OR_OR:
7911       id = ansi_opname (TRUTH_ORIF_EXPR);
7912       break;
7913
7914     case CPP_PLUS_PLUS:
7915       id = ansi_opname (POSTINCREMENT_EXPR);
7916       break;
7917
7918     case CPP_MINUS_MINUS:
7919       id = ansi_opname (PREDECREMENT_EXPR);
7920       break;
7921
7922     case CPP_COMMA:
7923       id = ansi_opname (COMPOUND_EXPR);
7924       break;
7925
7926     case CPP_DEREF_STAR:
7927       id = ansi_opname (MEMBER_REF);
7928       break;
7929
7930     case CPP_DEREF:
7931       id = ansi_opname (COMPONENT_REF);
7932       break;
7933
7934     case CPP_OPEN_PAREN:
7935       /* Consume the `('.  */
7936       cp_lexer_consume_token (parser->lexer);
7937       /* Look for the matching `)'.  */
7938       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7939       return ansi_opname (CALL_EXPR);
7940
7941     case CPP_OPEN_SQUARE:
7942       /* Consume the `['.  */
7943       cp_lexer_consume_token (parser->lexer);
7944       /* Look for the matching `]'.  */
7945       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7946       return ansi_opname (ARRAY_REF);
7947
7948       /* Extensions.  */
7949     case CPP_MIN:
7950       id = ansi_opname (MIN_EXPR);
7951       break;
7952
7953     case CPP_MAX:
7954       id = ansi_opname (MAX_EXPR);
7955       break;
7956
7957     case CPP_MIN_EQ:
7958       id = ansi_assopname (MIN_EXPR);
7959       break;
7960
7961     case CPP_MAX_EQ:
7962       id = ansi_assopname (MAX_EXPR);
7963       break;
7964
7965     default:
7966       /* Anything else is an error.  */
7967       break;
7968     }
7969
7970   /* If we have selected an identifier, we need to consume the
7971      operator token.  */
7972   if (id)
7973     cp_lexer_consume_token (parser->lexer);
7974   /* Otherwise, no valid operator name was present.  */
7975   else
7976     {
7977       cp_parser_error (parser, "expected operator");
7978       id = error_mark_node;
7979     }
7980
7981   return id;
7982 }
7983
7984 /* Parse a template-declaration.
7985
7986    template-declaration:
7987      export [opt] template < template-parameter-list > declaration
7988
7989    If MEMBER_P is TRUE, this template-declaration occurs within a
7990    class-specifier.
7991
7992    The grammar rule given by the standard isn't correct.  What
7993    is really meant is:
7994
7995    template-declaration:
7996      export [opt] template-parameter-list-seq
7997        decl-specifier-seq [opt] init-declarator [opt] ;
7998      export [opt] template-parameter-list-seq
7999        function-definition
8000
8001    template-parameter-list-seq:
8002      template-parameter-list-seq [opt]
8003      template < template-parameter-list >  */
8004
8005 static void
8006 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8007 {
8008   /* Check for `export'.  */
8009   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8010     {
8011       /* Consume the `export' token.  */
8012       cp_lexer_consume_token (parser->lexer);
8013       /* Warn that we do not support `export'.  */
8014       warning ("keyword `export' not implemented, and will be ignored");
8015     }
8016
8017   cp_parser_template_declaration_after_export (parser, member_p);
8018 }
8019
8020 /* Parse a template-parameter-list.
8021
8022    template-parameter-list:
8023      template-parameter
8024      template-parameter-list , template-parameter
8025
8026    Returns a TREE_LIST.  Each node represents a template parameter.
8027    The nodes are connected via their TREE_CHAINs.  */
8028
8029 static tree
8030 cp_parser_template_parameter_list (cp_parser* parser)
8031 {
8032   tree parameter_list = NULL_TREE;
8033
8034   while (true)
8035     {
8036       tree parameter;
8037       cp_token *token;
8038       bool is_non_type;
8039
8040       /* Parse the template-parameter.  */
8041       parameter = cp_parser_template_parameter (parser, &is_non_type);
8042       /* Add it to the list.  */
8043       parameter_list = process_template_parm (parameter_list,
8044                                               parameter,
8045                                               is_non_type);
8046       /* Peek at the next token.  */
8047       token = cp_lexer_peek_token (parser->lexer);
8048       /* If it's not a `,', we're done.  */
8049       if (token->type != CPP_COMMA)
8050         break;
8051       /* Otherwise, consume the `,' token.  */
8052       cp_lexer_consume_token (parser->lexer);
8053     }
8054
8055   return parameter_list;
8056 }
8057
8058 /* Parse a template-parameter.
8059
8060    template-parameter:
8061      type-parameter
8062      parameter-declaration
8063
8064    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8065    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8066    true iff this parameter is a non-type parameter.  */
8067
8068 static tree
8069 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8070 {
8071   cp_token *token;
8072   cp_parameter_declarator *parameter_declarator;
8073
8074   /* Assume it is a type parameter or a template parameter.  */
8075   *is_non_type = false;
8076   /* Peek at the next token.  */
8077   token = cp_lexer_peek_token (parser->lexer);
8078   /* If it is `class' or `template', we have a type-parameter.  */
8079   if (token->keyword == RID_TEMPLATE)
8080     return cp_parser_type_parameter (parser);
8081   /* If it is `class' or `typename' we do not know yet whether it is a
8082      type parameter or a non-type parameter.  Consider:
8083
8084        template <typename T, typename T::X X> ...
8085
8086      or:
8087
8088        template <class C, class D*> ...
8089
8090      Here, the first parameter is a type parameter, and the second is
8091      a non-type parameter.  We can tell by looking at the token after
8092      the identifier -- if it is a `,', `=', or `>' then we have a type
8093      parameter.  */
8094   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8095     {
8096       /* Peek at the token after `class' or `typename'.  */
8097       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8098       /* If it's an identifier, skip it.  */
8099       if (token->type == CPP_NAME)
8100         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8101       /* Now, see if the token looks like the end of a template
8102          parameter.  */
8103       if (token->type == CPP_COMMA
8104           || token->type == CPP_EQ
8105           || token->type == CPP_GREATER)
8106         return cp_parser_type_parameter (parser);
8107     }
8108
8109   /* Otherwise, it is a non-type parameter.
8110
8111      [temp.param]
8112
8113      When parsing a default template-argument for a non-type
8114      template-parameter, the first non-nested `>' is taken as the end
8115      of the template parameter-list rather than a greater-than
8116      operator.  */
8117   *is_non_type = true;
8118   parameter_declarator
8119      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8120                                         /*parenthesized_p=*/NULL);
8121   return (build_tree_list
8122           (parameter_declarator->default_argument,
8123            grokdeclarator (parameter_declarator->declarator,
8124                            &parameter_declarator->decl_specifiers,
8125                            PARM, /*initialized=*/0,
8126                            /*attrlist=*/NULL)));
8127 }
8128
8129 /* Parse a type-parameter.
8130
8131    type-parameter:
8132      class identifier [opt]
8133      class identifier [opt] = type-id
8134      typename identifier [opt]
8135      typename identifier [opt] = type-id
8136      template < template-parameter-list > class identifier [opt]
8137      template < template-parameter-list > class identifier [opt]
8138        = id-expression
8139
8140    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8141    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8142    the declaration of the parameter.  */
8143
8144 static tree
8145 cp_parser_type_parameter (cp_parser* parser)
8146 {
8147   cp_token *token;
8148   tree parameter;
8149
8150   /* Look for a keyword to tell us what kind of parameter this is.  */
8151   token = cp_parser_require (parser, CPP_KEYWORD,
8152                              "`class', `typename', or `template'");
8153   if (!token)
8154     return error_mark_node;
8155
8156   switch (token->keyword)
8157     {
8158     case RID_CLASS:
8159     case RID_TYPENAME:
8160       {
8161         tree identifier;
8162         tree default_argument;
8163
8164         /* If the next token is an identifier, then it names the
8165            parameter.  */
8166         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8167           identifier = cp_parser_identifier (parser);
8168         else
8169           identifier = NULL_TREE;
8170
8171         /* Create the parameter.  */
8172         parameter = finish_template_type_parm (class_type_node, identifier);
8173
8174         /* If the next token is an `=', we have a default argument.  */
8175         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8176           {
8177             /* Consume the `=' token.  */
8178             cp_lexer_consume_token (parser->lexer);
8179             /* Parse the default-argument.  */
8180             default_argument = cp_parser_type_id (parser);
8181           }
8182         else
8183           default_argument = NULL_TREE;
8184
8185         /* Create the combined representation of the parameter and the
8186            default argument.  */
8187         parameter = build_tree_list (default_argument, parameter);
8188       }
8189       break;
8190
8191     case RID_TEMPLATE:
8192       {
8193         tree parameter_list;
8194         tree identifier;
8195         tree default_argument;
8196
8197         /* Look for the `<'.  */
8198         cp_parser_require (parser, CPP_LESS, "`<'");
8199         /* Parse the template-parameter-list.  */
8200         begin_template_parm_list ();
8201         parameter_list
8202           = cp_parser_template_parameter_list (parser);
8203         parameter_list = end_template_parm_list (parameter_list);
8204         /* Look for the `>'.  */
8205         cp_parser_require (parser, CPP_GREATER, "`>'");
8206         /* Look for the `class' keyword.  */
8207         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8208         /* If the next token is an `=', then there is a
8209            default-argument.  If the next token is a `>', we are at
8210            the end of the parameter-list.  If the next token is a `,',
8211            then we are at the end of this parameter.  */
8212         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8213             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8214             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8215           identifier = cp_parser_identifier (parser);
8216         else
8217           identifier = NULL_TREE;
8218         /* Create the template parameter.  */
8219         parameter = finish_template_template_parm (class_type_node,
8220                                                    identifier);
8221
8222         /* If the next token is an `=', then there is a
8223            default-argument.  */
8224         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8225           {
8226             bool is_template;
8227
8228             /* Consume the `='.  */
8229             cp_lexer_consume_token (parser->lexer);
8230             /* Parse the id-expression.  */
8231             default_argument
8232               = cp_parser_id_expression (parser,
8233                                          /*template_keyword_p=*/false,
8234                                          /*check_dependency_p=*/true,
8235                                          /*template_p=*/&is_template,
8236                                          /*declarator_p=*/false);
8237             if (TREE_CODE (default_argument) == TYPE_DECL)
8238               /* If the id-expression was a template-id that refers to
8239                  a template-class, we already have the declaration here,
8240                  so no further lookup is needed.  */
8241                  ;
8242             else
8243               /* Look up the name.  */
8244               default_argument
8245                 = cp_parser_lookup_name (parser, default_argument,
8246                                         /*is_type=*/false,
8247                                         /*is_template=*/is_template,
8248                                         /*is_namespace=*/false,
8249                                         /*check_dependency=*/true,
8250                                         /*ambiguous_p=*/NULL);
8251             /* See if the default argument is valid.  */
8252             default_argument
8253               = check_template_template_default_arg (default_argument);
8254           }
8255         else
8256           default_argument = NULL_TREE;
8257
8258         /* Create the combined representation of the parameter and the
8259            default argument.  */
8260         parameter =  build_tree_list (default_argument, parameter);
8261       }
8262       break;
8263
8264     default:
8265       /* Anything else is an error.  */
8266       cp_parser_error (parser,
8267                        "expected `class', `typename', or `template'");
8268       parameter = error_mark_node;
8269     }
8270
8271   return parameter;
8272 }
8273
8274 /* Parse a template-id.
8275
8276    template-id:
8277      template-name < template-argument-list [opt] >
8278
8279    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8280    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8281    returned.  Otherwise, if the template-name names a function, or set
8282    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8283    names a class, returns a TYPE_DECL for the specialization.
8284
8285    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8286    uninstantiated templates.  */
8287
8288 static tree
8289 cp_parser_template_id (cp_parser *parser,
8290                        bool template_keyword_p,
8291                        bool check_dependency_p,
8292                        bool is_declaration)
8293 {
8294   tree template;
8295   tree arguments;
8296   tree template_id;
8297   ptrdiff_t start_of_id;
8298   tree access_check = NULL_TREE;
8299   cp_token *next_token, *next_token_2;
8300   bool is_identifier;
8301
8302   /* If the next token corresponds to a template-id, there is no need
8303      to reparse it.  */
8304   next_token = cp_lexer_peek_token (parser->lexer);
8305   if (next_token->type == CPP_TEMPLATE_ID)
8306     {
8307       tree value;
8308       tree check;
8309
8310       /* Get the stored value.  */
8311       value = cp_lexer_consume_token (parser->lexer)->value;
8312       /* Perform any access checks that were deferred.  */
8313       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8314         perform_or_defer_access_check (TREE_PURPOSE (check),
8315                                        TREE_VALUE (check));
8316       /* Return the stored value.  */
8317       return TREE_VALUE (value);
8318     }
8319
8320   /* Avoid performing name lookup if there is no possibility of
8321      finding a template-id.  */
8322   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8323       || (next_token->type == CPP_NAME
8324           && !cp_parser_nth_token_starts_template_argument_list_p
8325                (parser, 2)))
8326     {
8327       cp_parser_error (parser, "expected template-id");
8328       return error_mark_node;
8329     }
8330
8331   /* Remember where the template-id starts.  */
8332   if (cp_parser_parsing_tentatively (parser)
8333       && !cp_parser_committed_to_tentative_parse (parser))
8334     {
8335       next_token = cp_lexer_peek_token (parser->lexer);
8336       start_of_id = cp_lexer_token_difference (parser->lexer,
8337                                                parser->lexer->buffer,
8338                                                next_token);
8339     }
8340   else
8341     start_of_id = -1;
8342
8343   push_deferring_access_checks (dk_deferred);
8344
8345   /* Parse the template-name.  */
8346   is_identifier = false;
8347   template = cp_parser_template_name (parser, template_keyword_p,
8348                                       check_dependency_p,
8349                                       is_declaration,
8350                                       &is_identifier);
8351   if (template == error_mark_node || is_identifier)
8352     {
8353       pop_deferring_access_checks ();
8354       return template;
8355     }
8356
8357   /* If we find the sequence `[:' after a template-name, it's probably
8358      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8359      parse correctly the argument list.  */
8360   next_token = cp_lexer_peek_token (parser->lexer);
8361   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8362   if (next_token->type == CPP_OPEN_SQUARE
8363       && next_token->flags & DIGRAPH
8364       && next_token_2->type == CPP_COLON
8365       && !(next_token_2->flags & PREV_WHITE))
8366     {
8367       cp_parser_parse_tentatively (parser);
8368       /* Change `:' into `::'.  */
8369       next_token_2->type = CPP_SCOPE;
8370       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8371          CPP_LESS.  */
8372       cp_lexer_consume_token (parser->lexer);
8373       /* Parse the arguments.  */
8374       arguments = cp_parser_enclosed_template_argument_list (parser);
8375       if (!cp_parser_parse_definitely (parser))
8376         {
8377           /* If we couldn't parse an argument list, then we revert our changes
8378              and return simply an error. Maybe this is not a template-id
8379              after all.  */
8380           next_token_2->type = CPP_COLON;
8381           cp_parser_error (parser, "expected `<'");
8382           pop_deferring_access_checks ();
8383           return error_mark_node;
8384         }
8385       /* Otherwise, emit an error about the invalid digraph, but continue
8386          parsing because we got our argument list.  */
8387       pedwarn ("`<::' cannot begin a template-argument list");
8388       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8389               "between `<' and `::'");
8390       if (!flag_permissive)
8391         {
8392           static bool hint;
8393           if (!hint)
8394             {
8395               inform ("(if you use `-fpermissive' G++ will accept your code)");
8396               hint = true;
8397             }
8398         }
8399     }
8400   else
8401     {
8402       /* Look for the `<' that starts the template-argument-list.  */
8403       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8404         {
8405           pop_deferring_access_checks ();
8406           return error_mark_node;
8407         }
8408       /* Parse the arguments.  */
8409       arguments = cp_parser_enclosed_template_argument_list (parser);
8410     }
8411
8412   /* Build a representation of the specialization.  */
8413   if (TREE_CODE (template) == IDENTIFIER_NODE)
8414     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8415   else if (DECL_CLASS_TEMPLATE_P (template)
8416            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8417     template_id
8418       = finish_template_type (template, arguments,
8419                               cp_lexer_next_token_is (parser->lexer,
8420                                                       CPP_SCOPE));
8421   else
8422     {
8423       /* If it's not a class-template or a template-template, it should be
8424          a function-template.  */
8425       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8426                    || TREE_CODE (template) == OVERLOAD
8427                    || BASELINK_P (template)));
8428
8429       template_id = lookup_template_function (template, arguments);
8430     }
8431
8432   /* Retrieve any deferred checks.  Do not pop this access checks yet
8433      so the memory will not be reclaimed during token replacing below.  */
8434   access_check = get_deferred_access_checks ();
8435
8436   /* If parsing tentatively, replace the sequence of tokens that makes
8437      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8438      should we re-parse the token stream, we will not have to repeat
8439      the effort required to do the parse, nor will we issue duplicate
8440      error messages about problems during instantiation of the
8441      template.  */
8442   if (start_of_id >= 0)
8443     {
8444       cp_token *token;
8445
8446       /* Find the token that corresponds to the start of the
8447          template-id.  */
8448       token = cp_lexer_advance_token (parser->lexer,
8449                                       parser->lexer->buffer,
8450                                       start_of_id);
8451
8452       /* Reset the contents of the START_OF_ID token.  */
8453       token->type = CPP_TEMPLATE_ID;
8454       token->value = build_tree_list (access_check, template_id);
8455       token->keyword = RID_MAX;
8456       /* Purge all subsequent tokens.  */
8457       cp_lexer_purge_tokens_after (parser->lexer, token);
8458     }
8459
8460   pop_deferring_access_checks ();
8461   return template_id;
8462 }
8463
8464 /* Parse a template-name.
8465
8466    template-name:
8467      identifier
8468
8469    The standard should actually say:
8470
8471    template-name:
8472      identifier
8473      operator-function-id
8474
8475    A defect report has been filed about this issue.
8476
8477    A conversion-function-id cannot be a template name because they cannot
8478    be part of a template-id. In fact, looking at this code:
8479
8480    a.operator K<int>()
8481
8482    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8483    It is impossible to call a templated conversion-function-id with an
8484    explicit argument list, since the only allowed template parameter is
8485    the type to which it is converting.
8486
8487    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8488    `template' keyword, in a construction like:
8489
8490      T::template f<3>()
8491
8492    In that case `f' is taken to be a template-name, even though there
8493    is no way of knowing for sure.
8494
8495    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8496    name refers to a set of overloaded functions, at least one of which
8497    is a template, or an IDENTIFIER_NODE with the name of the template,
8498    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8499    names are looked up inside uninstantiated templates.  */
8500
8501 static tree
8502 cp_parser_template_name (cp_parser* parser,
8503                          bool template_keyword_p,
8504                          bool check_dependency_p,
8505                          bool is_declaration,
8506                          bool *is_identifier)
8507 {
8508   tree identifier;
8509   tree decl;
8510   tree fns;
8511
8512   /* If the next token is `operator', then we have either an
8513      operator-function-id or a conversion-function-id.  */
8514   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8515     {
8516       /* We don't know whether we're looking at an
8517          operator-function-id or a conversion-function-id.  */
8518       cp_parser_parse_tentatively (parser);
8519       /* Try an operator-function-id.  */
8520       identifier = cp_parser_operator_function_id (parser);
8521       /* If that didn't work, try a conversion-function-id.  */
8522       if (!cp_parser_parse_definitely (parser))
8523         {
8524           cp_parser_error (parser, "expected template-name");
8525           return error_mark_node;
8526         }
8527     }
8528   /* Look for the identifier.  */
8529   else
8530     identifier = cp_parser_identifier (parser);
8531
8532   /* If we didn't find an identifier, we don't have a template-id.  */
8533   if (identifier == error_mark_node)
8534     return error_mark_node;
8535
8536   /* If the name immediately followed the `template' keyword, then it
8537      is a template-name.  However, if the next token is not `<', then
8538      we do not treat it as a template-name, since it is not being used
8539      as part of a template-id.  This enables us to handle constructs
8540      like:
8541
8542        template <typename T> struct S { S(); };
8543        template <typename T> S<T>::S();
8544
8545      correctly.  We would treat `S' as a template -- if it were `S<T>'
8546      -- but we do not if there is no `<'.  */
8547
8548   if (processing_template_decl
8549       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8550     {
8551       /* In a declaration, in a dependent context, we pretend that the
8552          "template" keyword was present in order to improve error
8553          recovery.  For example, given:
8554
8555            template <typename T> void f(T::X<int>);
8556
8557          we want to treat "X<int>" as a template-id.  */
8558       if (is_declaration
8559           && !template_keyword_p
8560           && parser->scope && TYPE_P (parser->scope)
8561           && check_dependency_p
8562           && dependent_type_p (parser->scope)
8563           /* Do not do this for dtors (or ctors), since they never
8564              need the template keyword before their name.  */
8565           && !constructor_name_p (identifier, parser->scope))
8566         {
8567           ptrdiff_t start;
8568           cp_token* token;
8569           /* Explain what went wrong.  */
8570           error ("non-template `%D' used as template", identifier);
8571           inform ("use `%T::template %D' to indicate that it is a template",
8572                   parser->scope, identifier);
8573           /* If parsing tentatively, find the location of the "<"
8574              token.  */
8575           if (cp_parser_parsing_tentatively (parser)
8576               && !cp_parser_committed_to_tentative_parse (parser))
8577             {
8578               cp_parser_simulate_error (parser);
8579               token = cp_lexer_peek_token (parser->lexer);
8580               token = cp_lexer_prev_token (parser->lexer, token);
8581               start = cp_lexer_token_difference (parser->lexer,
8582                                                  parser->lexer->buffer,
8583                                                  token);
8584             }
8585           else
8586             start = -1;
8587           /* Parse the template arguments so that we can issue error
8588              messages about them.  */
8589           cp_lexer_consume_token (parser->lexer);
8590           cp_parser_enclosed_template_argument_list (parser);
8591           /* Skip tokens until we find a good place from which to
8592              continue parsing.  */
8593           cp_parser_skip_to_closing_parenthesis (parser,
8594                                                  /*recovering=*/true,
8595                                                  /*or_comma=*/true,
8596                                                  /*consume_paren=*/false);
8597           /* If parsing tentatively, permanently remove the
8598              template argument list.  That will prevent duplicate
8599              error messages from being issued about the missing
8600              "template" keyword.  */
8601           if (start >= 0)
8602             {
8603               token = cp_lexer_advance_token (parser->lexer,
8604                                               parser->lexer->buffer,
8605                                               start);
8606               cp_lexer_purge_tokens_after (parser->lexer, token);
8607             }
8608           if (is_identifier)
8609             *is_identifier = true;
8610           return identifier;
8611         }
8612
8613       /* If the "template" keyword is present, then there is generally
8614          no point in doing name-lookup, so we just return IDENTIFIER.
8615          But, if the qualifying scope is non-dependent then we can
8616          (and must) do name-lookup normally.  */
8617       if (template_keyword_p
8618           && (!parser->scope
8619               || (TYPE_P (parser->scope)
8620                   && dependent_type_p (parser->scope))))
8621         return identifier;
8622     }
8623
8624   /* Look up the name.  */
8625   decl = cp_parser_lookup_name (parser, identifier,
8626                                 /*is_type=*/false,
8627                                 /*is_template=*/false,
8628                                 /*is_namespace=*/false,
8629                                 check_dependency_p,
8630                                 /*ambiguous_p=*/NULL);
8631   decl = maybe_get_template_decl_from_type_decl (decl);
8632
8633   /* If DECL is a template, then the name was a template-name.  */
8634   if (TREE_CODE (decl) == TEMPLATE_DECL)
8635     ;
8636   else
8637     {
8638       /* The standard does not explicitly indicate whether a name that
8639          names a set of overloaded declarations, some of which are
8640          templates, is a template-name.  However, such a name should
8641          be a template-name; otherwise, there is no way to form a
8642          template-id for the overloaded templates.  */
8643       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8644       if (TREE_CODE (fns) == OVERLOAD)
8645         {
8646           tree fn;
8647
8648           for (fn = fns; fn; fn = OVL_NEXT (fn))
8649             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8650               break;
8651         }
8652       else
8653         {
8654           /* Otherwise, the name does not name a template.  */
8655           cp_parser_error (parser, "expected template-name");
8656           return error_mark_node;
8657         }
8658     }
8659
8660   /* If DECL is dependent, and refers to a function, then just return
8661      its name; we will look it up again during template instantiation.  */
8662   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8663     {
8664       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8665       if (TYPE_P (scope) && dependent_type_p (scope))
8666         return identifier;
8667     }
8668
8669   return decl;
8670 }
8671
8672 /* Parse a template-argument-list.
8673
8674    template-argument-list:
8675      template-argument
8676      template-argument-list , template-argument
8677
8678    Returns a TREE_VEC containing the arguments.  */
8679
8680 static tree
8681 cp_parser_template_argument_list (cp_parser* parser)
8682 {
8683   tree fixed_args[10];
8684   unsigned n_args = 0;
8685   unsigned alloced = 10;
8686   tree *arg_ary = fixed_args;
8687   tree vec;
8688   bool saved_in_template_argument_list_p;
8689
8690   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8691   parser->in_template_argument_list_p = true;
8692   do
8693     {
8694       tree argument;
8695
8696       if (n_args)
8697         /* Consume the comma.  */
8698         cp_lexer_consume_token (parser->lexer);
8699
8700       /* Parse the template-argument.  */
8701       argument = cp_parser_template_argument (parser);
8702       if (n_args == alloced)
8703         {
8704           alloced *= 2;
8705
8706           if (arg_ary == fixed_args)
8707             {
8708               arg_ary = xmalloc (sizeof (tree) * alloced);
8709               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8710             }
8711           else
8712             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8713         }
8714       arg_ary[n_args++] = argument;
8715     }
8716   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8717
8718   vec = make_tree_vec (n_args);
8719
8720   while (n_args--)
8721     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8722
8723   if (arg_ary != fixed_args)
8724     free (arg_ary);
8725   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8726   return vec;
8727 }
8728
8729 /* Parse a template-argument.
8730
8731    template-argument:
8732      assignment-expression
8733      type-id
8734      id-expression
8735
8736    The representation is that of an assignment-expression, type-id, or
8737    id-expression -- except that the qualified id-expression is
8738    evaluated, so that the value returned is either a DECL or an
8739    OVERLOAD.
8740
8741    Although the standard says "assignment-expression", it forbids
8742    throw-expressions or assignments in the template argument.
8743    Therefore, we use "conditional-expression" instead.  */
8744
8745 static tree
8746 cp_parser_template_argument (cp_parser* parser)
8747 {
8748   tree argument;
8749   bool template_p;
8750   bool address_p;
8751   bool maybe_type_id = false;
8752   cp_token *token;
8753   cp_id_kind idk;
8754   tree qualifying_class;
8755
8756   /* There's really no way to know what we're looking at, so we just
8757      try each alternative in order.
8758
8759        [temp.arg]
8760
8761        In a template-argument, an ambiguity between a type-id and an
8762        expression is resolved to a type-id, regardless of the form of
8763        the corresponding template-parameter.
8764
8765      Therefore, we try a type-id first.  */
8766   cp_parser_parse_tentatively (parser);
8767   argument = cp_parser_type_id (parser);
8768   /* If there was no error parsing the type-id but the next token is a '>>',
8769      we probably found a typo for '> >'. But there are type-id which are
8770      also valid expressions. For instance:
8771
8772      struct X { int operator >> (int); };
8773      template <int V> struct Foo {};
8774      Foo<X () >> 5> r;
8775
8776      Here 'X()' is a valid type-id of a function type, but the user just
8777      wanted to write the expression "X() >> 5". Thus, we remember that we
8778      found a valid type-id, but we still try to parse the argument as an
8779      expression to see what happens.  */
8780   if (!cp_parser_error_occurred (parser)
8781       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8782     {
8783       maybe_type_id = true;
8784       cp_parser_abort_tentative_parse (parser);
8785     }
8786   else
8787     {
8788       /* If the next token isn't a `,' or a `>', then this argument wasn't
8789       really finished. This means that the argument is not a valid
8790       type-id.  */
8791       if (!cp_parser_next_token_ends_template_argument_p (parser))
8792         cp_parser_error (parser, "expected template-argument");
8793       /* If that worked, we're done.  */
8794       if (cp_parser_parse_definitely (parser))
8795         return argument;
8796     }
8797   /* We're still not sure what the argument will be.  */
8798   cp_parser_parse_tentatively (parser);
8799   /* Try a template.  */
8800   argument = cp_parser_id_expression (parser,
8801                                       /*template_keyword_p=*/false,
8802                                       /*check_dependency_p=*/true,
8803                                       &template_p,
8804                                       /*declarator_p=*/false);
8805   /* If the next token isn't a `,' or a `>', then this argument wasn't
8806      really finished.  */
8807   if (!cp_parser_next_token_ends_template_argument_p (parser))
8808     cp_parser_error (parser, "expected template-argument");
8809   if (!cp_parser_error_occurred (parser))
8810     {
8811       /* Figure out what is being referred to.  If the id-expression
8812          was for a class template specialization, then we will have a
8813          TYPE_DECL at this point.  There is no need to do name lookup
8814          at this point in that case.  */
8815       if (TREE_CODE (argument) != TYPE_DECL)
8816         argument = cp_parser_lookup_name (parser, argument,
8817                                           /*is_type=*/false,
8818                                           /*is_template=*/template_p,
8819                                           /*is_namespace=*/false,
8820                                           /*check_dependency=*/true,
8821                                           /*ambiguous_p=*/NULL);
8822       if (TREE_CODE (argument) != TEMPLATE_DECL
8823           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8824         cp_parser_error (parser, "expected template-name");
8825     }
8826   if (cp_parser_parse_definitely (parser))
8827     return argument;
8828   /* It must be a non-type argument.  There permitted cases are given
8829      in [temp.arg.nontype]:
8830
8831      -- an integral constant-expression of integral or enumeration
8832         type; or
8833
8834      -- the name of a non-type template-parameter; or
8835
8836      -- the name of an object or function with external linkage...
8837
8838      -- the address of an object or function with external linkage...
8839
8840      -- a pointer to member...  */
8841   /* Look for a non-type template parameter.  */
8842   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8843     {
8844       cp_parser_parse_tentatively (parser);
8845       argument = cp_parser_primary_expression (parser,
8846                                                &idk,
8847                                                &qualifying_class);
8848       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8849           || !cp_parser_next_token_ends_template_argument_p (parser))
8850         cp_parser_simulate_error (parser);
8851       if (cp_parser_parse_definitely (parser))
8852         return argument;
8853     }
8854   /* If the next token is "&", the argument must be the address of an
8855      object or function with external linkage.  */
8856   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8857   if (address_p)
8858     cp_lexer_consume_token (parser->lexer);
8859   /* See if we might have an id-expression.  */
8860   token = cp_lexer_peek_token (parser->lexer);
8861   if (token->type == CPP_NAME
8862       || token->keyword == RID_OPERATOR
8863       || token->type == CPP_SCOPE
8864       || token->type == CPP_TEMPLATE_ID
8865       || token->type == CPP_NESTED_NAME_SPECIFIER)
8866     {
8867       cp_parser_parse_tentatively (parser);
8868       argument = cp_parser_primary_expression (parser,
8869                                                &idk,
8870                                                &qualifying_class);
8871       if (cp_parser_error_occurred (parser)
8872           || !cp_parser_next_token_ends_template_argument_p (parser))
8873         cp_parser_abort_tentative_parse (parser);
8874       else
8875         {
8876           if (qualifying_class)
8877             argument = finish_qualified_id_expr (qualifying_class,
8878                                                  argument,
8879                                                  /*done=*/true,
8880                                                  address_p);
8881           if (TREE_CODE (argument) == VAR_DECL)
8882             {
8883               /* A variable without external linkage might still be a
8884                  valid constant-expression, so no error is issued here
8885                  if the external-linkage check fails.  */
8886               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8887                 cp_parser_simulate_error (parser);
8888             }
8889           else if (is_overloaded_fn (argument))
8890             /* All overloaded functions are allowed; if the external
8891                linkage test does not pass, an error will be issued
8892                later.  */
8893             ;
8894           else if (address_p
8895                    && (TREE_CODE (argument) == OFFSET_REF
8896                        || TREE_CODE (argument) == SCOPE_REF))
8897             /* A pointer-to-member.  */
8898             ;
8899           else
8900             cp_parser_simulate_error (parser);
8901
8902           if (cp_parser_parse_definitely (parser))
8903             {
8904               if (address_p)
8905                 argument = build_x_unary_op (ADDR_EXPR, argument);
8906               return argument;
8907             }
8908         }
8909     }
8910   /* If the argument started with "&", there are no other valid
8911      alternatives at this point.  */
8912   if (address_p)
8913     {
8914       cp_parser_error (parser, "invalid non-type template argument");
8915       return error_mark_node;
8916     }
8917   /* If the argument wasn't successfully parsed as a type-id followed
8918      by '>>', the argument can only be a constant expression now.
8919      Otherwise, we try parsing the constant-expression tentatively,
8920      because the argument could really be a type-id.  */
8921   if (maybe_type_id)
8922     cp_parser_parse_tentatively (parser);
8923   argument = cp_parser_constant_expression (parser,
8924                                             /*allow_non_constant_p=*/false,
8925                                             /*non_constant_p=*/NULL);
8926   argument = fold_non_dependent_expr (argument);
8927   if (!maybe_type_id)
8928     return argument;
8929   if (!cp_parser_next_token_ends_template_argument_p (parser))
8930     cp_parser_error (parser, "expected template-argument");
8931   if (cp_parser_parse_definitely (parser))
8932     return argument;
8933   /* We did our best to parse the argument as a non type-id, but that
8934      was the only alternative that matched (albeit with a '>' after
8935      it). We can assume it's just a typo from the user, and a
8936      diagnostic will then be issued.  */
8937   return cp_parser_type_id (parser);
8938 }
8939
8940 /* Parse an explicit-instantiation.
8941
8942    explicit-instantiation:
8943      template declaration
8944
8945    Although the standard says `declaration', what it really means is:
8946
8947    explicit-instantiation:
8948      template decl-specifier-seq [opt] declarator [opt] ;
8949
8950    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8951    supposed to be allowed.  A defect report has been filed about this
8952    issue.
8953
8954    GNU Extension:
8955
8956    explicit-instantiation:
8957      storage-class-specifier template
8958        decl-specifier-seq [opt] declarator [opt] ;
8959      function-specifier template
8960        decl-specifier-seq [opt] declarator [opt] ;  */
8961
8962 static void
8963 cp_parser_explicit_instantiation (cp_parser* parser)
8964 {
8965   int declares_class_or_enum;
8966   cp_decl_specifier_seq decl_specifiers;
8967   tree extension_specifier = NULL_TREE;
8968
8969   /* Look for an (optional) storage-class-specifier or
8970      function-specifier.  */
8971   if (cp_parser_allow_gnu_extensions_p (parser))
8972     {
8973       extension_specifier
8974         = cp_parser_storage_class_specifier_opt (parser);
8975       if (!extension_specifier)
8976         extension_specifier
8977           = cp_parser_function_specifier_opt (parser,
8978                                               /*decl_specs=*/NULL);
8979     }
8980
8981   /* Look for the `template' keyword.  */
8982   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8983   /* Let the front end know that we are processing an explicit
8984      instantiation.  */
8985   begin_explicit_instantiation ();
8986   /* [temp.explicit] says that we are supposed to ignore access
8987      control while processing explicit instantiation directives.  */
8988   push_deferring_access_checks (dk_no_check);
8989   /* Parse a decl-specifier-seq.  */
8990   cp_parser_decl_specifier_seq (parser,
8991                                 CP_PARSER_FLAGS_OPTIONAL,
8992                                 &decl_specifiers,
8993                                 &declares_class_or_enum);
8994   /* If there was exactly one decl-specifier, and it declared a class,
8995      and there's no declarator, then we have an explicit type
8996      instantiation.  */
8997   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8998     {
8999       tree type;
9000
9001       type = check_tag_decl (&decl_specifiers);
9002       /* Turn access control back on for names used during
9003          template instantiation.  */
9004       pop_deferring_access_checks ();
9005       if (type)
9006         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9007     }
9008   else
9009     {
9010       cp_declarator *declarator;
9011       tree decl;
9012
9013       /* Parse the declarator.  */
9014       declarator
9015         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9016                                 /*ctor_dtor_or_conv_p=*/NULL,
9017                                 /*parenthesized_p=*/NULL);
9018       cp_parser_check_for_definition_in_return_type (declarator,
9019                                                      declares_class_or_enum);
9020       if (declarator != cp_error_declarator)
9021         {
9022           decl = grokdeclarator (declarator, &decl_specifiers,
9023                                  NORMAL, 0, NULL);
9024           /* Turn access control back on for names used during
9025              template instantiation.  */
9026           pop_deferring_access_checks ();
9027           /* Do the explicit instantiation.  */
9028           do_decl_instantiation (decl, extension_specifier);
9029         }
9030       else
9031         {
9032           pop_deferring_access_checks ();
9033           /* Skip the body of the explicit instantiation.  */
9034           cp_parser_skip_to_end_of_statement (parser);
9035         }
9036     }
9037   /* We're done with the instantiation.  */
9038   end_explicit_instantiation ();
9039
9040   cp_parser_consume_semicolon_at_end_of_statement (parser);
9041 }
9042
9043 /* Parse an explicit-specialization.
9044
9045    explicit-specialization:
9046      template < > declaration
9047
9048    Although the standard says `declaration', what it really means is:
9049
9050    explicit-specialization:
9051      template <> decl-specifier [opt] init-declarator [opt] ;
9052      template <> function-definition
9053      template <> explicit-specialization
9054      template <> template-declaration  */
9055
9056 static void
9057 cp_parser_explicit_specialization (cp_parser* parser)
9058 {
9059   /* Look for the `template' keyword.  */
9060   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9061   /* Look for the `<'.  */
9062   cp_parser_require (parser, CPP_LESS, "`<'");
9063   /* Look for the `>'.  */
9064   cp_parser_require (parser, CPP_GREATER, "`>'");
9065   /* We have processed another parameter list.  */
9066   ++parser->num_template_parameter_lists;
9067   /* Let the front end know that we are beginning a specialization.  */
9068   begin_specialization ();
9069
9070   /* If the next keyword is `template', we need to figure out whether
9071      or not we're looking a template-declaration.  */
9072   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9073     {
9074       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9075           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9076         cp_parser_template_declaration_after_export (parser,
9077                                                      /*member_p=*/false);
9078       else
9079         cp_parser_explicit_specialization (parser);
9080     }
9081   else
9082     /* Parse the dependent declaration.  */
9083     cp_parser_single_declaration (parser,
9084                                   /*member_p=*/false,
9085                                   /*friend_p=*/NULL);
9086
9087   /* We're done with the specialization.  */
9088   end_specialization ();
9089   /* We're done with this parameter list.  */
9090   --parser->num_template_parameter_lists;
9091 }
9092
9093 /* Parse a type-specifier.
9094
9095    type-specifier:
9096      simple-type-specifier
9097      class-specifier
9098      enum-specifier
9099      elaborated-type-specifier
9100      cv-qualifier
9101
9102    GNU Extension:
9103
9104    type-specifier:
9105      __complex__
9106
9107    Returns a representation of the type-specifier.  For a
9108    class-specifier, enum-specifier, or elaborated-type-specifier, a
9109    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9110
9111    The parser flags FLAGS is used to control type-specifier parsing.
9112
9113    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9114    in a decl-specifier-seq.
9115
9116    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9117    class-specifier, enum-specifier, or elaborated-type-specifier, then
9118    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9119    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9120    zero.
9121
9122    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9123    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9124    is set to FALSE.  */
9125
9126 static tree
9127 cp_parser_type_specifier (cp_parser* parser,
9128                           cp_parser_flags flags,
9129                           cp_decl_specifier_seq *decl_specs,
9130                           bool is_declaration,
9131                           int* declares_class_or_enum,
9132                           bool* is_cv_qualifier)
9133 {
9134   tree type_spec = NULL_TREE;
9135   cp_token *token;
9136   enum rid keyword;
9137   cp_decl_spec ds = ds_last;
9138
9139   /* Assume this type-specifier does not declare a new type.  */
9140   if (declares_class_or_enum)
9141     *declares_class_or_enum = 0;
9142   /* And that it does not specify a cv-qualifier.  */
9143   if (is_cv_qualifier)
9144     *is_cv_qualifier = false;
9145   /* Peek at the next token.  */
9146   token = cp_lexer_peek_token (parser->lexer);
9147
9148   /* If we're looking at a keyword, we can use that to guide the
9149      production we choose.  */
9150   keyword = token->keyword;
9151   switch (keyword)
9152     {
9153     case RID_ENUM:
9154       /* 'enum' [identifier] '{' introduces an enum-specifier;
9155          'enum' <anything else> introduces an elaborated-type-specifier.  */
9156       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9157           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9158               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9159                  == CPP_OPEN_BRACE))
9160         {
9161           type_spec = cp_parser_enum_specifier (parser);
9162           if (declares_class_or_enum)
9163             *declares_class_or_enum = 2;
9164           if (decl_specs)
9165             cp_parser_set_decl_spec_type (decl_specs,
9166                                           type_spec,
9167                                           /*user_defined_p=*/true);
9168           return type_spec;
9169         }
9170       else
9171         goto elaborated_type_specifier;
9172
9173       /* Any of these indicate either a class-specifier, or an
9174          elaborated-type-specifier.  */
9175     case RID_CLASS:
9176     case RID_STRUCT:
9177     case RID_UNION:
9178       /* Parse tentatively so that we can back up if we don't find a
9179          class-specifier.  */
9180       cp_parser_parse_tentatively (parser);
9181       /* Look for the class-specifier.  */
9182       type_spec = cp_parser_class_specifier (parser);
9183       /* If that worked, we're done.  */
9184       if (cp_parser_parse_definitely (parser))
9185         {
9186           if (declares_class_or_enum)
9187             *declares_class_or_enum = 2;
9188           if (decl_specs)
9189             cp_parser_set_decl_spec_type (decl_specs,
9190                                           type_spec,
9191                                           /*user_defined_p=*/true);
9192           return type_spec;
9193         }
9194
9195       /* Fall through.  */
9196     elaborated_type_specifier:
9197       /* We're declaring (not defining) a class or enum.  */
9198       if (declares_class_or_enum)
9199         *declares_class_or_enum = 1;
9200
9201       /* Fall through.  */
9202     case RID_TYPENAME:
9203       /* Look for an elaborated-type-specifier.  */
9204       type_spec
9205         = (cp_parser_elaborated_type_specifier
9206            (parser,
9207             decl_specs && decl_specs->specs[(int) ds_friend],
9208             is_declaration));
9209       if (decl_specs)
9210         cp_parser_set_decl_spec_type (decl_specs,
9211                                       type_spec,
9212                                       /*user_defined_p=*/true);
9213       return type_spec;
9214
9215     case RID_CONST:
9216       ds = ds_const;
9217       if (is_cv_qualifier)
9218         *is_cv_qualifier = true;
9219       break;
9220
9221     case RID_VOLATILE:
9222       ds = ds_volatile;
9223       if (is_cv_qualifier)
9224         *is_cv_qualifier = true;
9225       break;
9226
9227     case RID_RESTRICT:
9228       ds = ds_restrict;
9229       if (is_cv_qualifier)
9230         *is_cv_qualifier = true;
9231       break;
9232
9233     case RID_COMPLEX:
9234       /* The `__complex__' keyword is a GNU extension.  */
9235       ds = ds_complex;
9236       break;
9237
9238     default:
9239       break;
9240     }
9241
9242   /* Handle simple keywords.  */
9243   if (ds != ds_last)
9244     {
9245       if (decl_specs)
9246         {
9247           ++decl_specs->specs[(int)ds];
9248           decl_specs->any_specifiers_p = true;
9249         }
9250       return cp_lexer_consume_token (parser->lexer)->value;
9251     }
9252
9253   /* If we do not already have a type-specifier, assume we are looking
9254      at a simple-type-specifier.  */
9255   type_spec = cp_parser_simple_type_specifier (parser,
9256                                                decl_specs,
9257                                                flags);
9258
9259   /* If we didn't find a type-specifier, and a type-specifier was not
9260      optional in this context, issue an error message.  */
9261   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9262     {
9263       cp_parser_error (parser, "expected type specifier");
9264       return error_mark_node;
9265     }
9266
9267   return type_spec;
9268 }
9269
9270 /* Parse a simple-type-specifier.
9271
9272    simple-type-specifier:
9273      :: [opt] nested-name-specifier [opt] type-name
9274      :: [opt] nested-name-specifier template template-id
9275      char
9276      wchar_t
9277      bool
9278      short
9279      int
9280      long
9281      signed
9282      unsigned
9283      float
9284      double
9285      void
9286
9287    GNU Extension:
9288
9289    simple-type-specifier:
9290      __typeof__ unary-expression
9291      __typeof__ ( type-id )
9292
9293    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9294    appropriately updated.  */
9295
9296 static tree
9297 cp_parser_simple_type_specifier (cp_parser* parser,
9298                                  cp_decl_specifier_seq *decl_specs,
9299                                  cp_parser_flags flags)
9300 {
9301   tree type = NULL_TREE;
9302   cp_token *token;
9303
9304   /* Peek at the next token.  */
9305   token = cp_lexer_peek_token (parser->lexer);
9306
9307   /* If we're looking at a keyword, things are easy.  */
9308   switch (token->keyword)
9309     {
9310     case RID_CHAR:
9311       if (decl_specs)
9312         decl_specs->explicit_char_p = true;
9313       type = char_type_node;
9314       break;
9315     case RID_WCHAR:
9316       type = wchar_type_node;
9317       break;
9318     case RID_BOOL:
9319       type = boolean_type_node;
9320       break;
9321     case RID_SHORT:
9322       if (decl_specs)
9323         ++decl_specs->specs[(int) ds_short];
9324       type = short_integer_type_node;
9325       break;
9326     case RID_INT:
9327       if (decl_specs)
9328         decl_specs->explicit_int_p = true;
9329       type = integer_type_node;
9330       break;
9331     case RID_LONG:
9332       if (decl_specs)
9333         ++decl_specs->specs[(int) ds_long];
9334       type = long_integer_type_node;
9335       break;
9336     case RID_SIGNED:
9337       if (decl_specs)
9338         ++decl_specs->specs[(int) ds_signed];
9339       type = integer_type_node;
9340       break;
9341     case RID_UNSIGNED:
9342       if (decl_specs)
9343         ++decl_specs->specs[(int) ds_unsigned];
9344       type = unsigned_type_node;
9345       break;
9346     case RID_FLOAT:
9347       type = float_type_node;
9348       break;
9349     case RID_DOUBLE:
9350       type = double_type_node;
9351       break;
9352     case RID_VOID:
9353       type = void_type_node;
9354       break;
9355
9356     case RID_TYPEOF:
9357       /* Consume the `typeof' token.  */
9358       cp_lexer_consume_token (parser->lexer);
9359       /* Parse the operand to `typeof'.  */
9360       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9361       /* If it is not already a TYPE, take its type.  */
9362       if (!TYPE_P (type))
9363         type = finish_typeof (type);
9364
9365       if (decl_specs)
9366         cp_parser_set_decl_spec_type (decl_specs, type,
9367                                       /*user_defined_p=*/true);
9368
9369       return type;
9370
9371     default:
9372       break;
9373     }
9374
9375   /* If the type-specifier was for a built-in type, we're done.  */
9376   if (type)
9377     {
9378       tree id;
9379
9380       /* Record the type.  */
9381       if (decl_specs
9382           && (token->keyword != RID_SIGNED
9383               && token->keyword != RID_UNSIGNED
9384               && token->keyword != RID_SHORT
9385               && token->keyword != RID_LONG))
9386         cp_parser_set_decl_spec_type (decl_specs,
9387                                       type,
9388                                       /*user_defined=*/false);
9389       if (decl_specs)
9390         decl_specs->any_specifiers_p = true;
9391
9392       /* Consume the token.  */
9393       id = cp_lexer_consume_token (parser->lexer)->value;
9394
9395       /* There is no valid C++ program where a non-template type is
9396          followed by a "<".  That usually indicates that the user thought
9397          that the type was a template.  */
9398       cp_parser_check_for_invalid_template_id (parser, type);
9399
9400       return TYPE_NAME (type);
9401     }
9402
9403   /* The type-specifier must be a user-defined type.  */
9404   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9405     {
9406       bool qualified_p;
9407       bool global_p;
9408
9409       /* Don't gobble tokens or issue error messages if this is an
9410          optional type-specifier.  */
9411       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9412         cp_parser_parse_tentatively (parser);
9413
9414       /* Look for the optional `::' operator.  */
9415       global_p
9416         = (cp_parser_global_scope_opt (parser,
9417                                        /*current_scope_valid_p=*/false)
9418            != NULL_TREE);
9419       /* Look for the nested-name specifier.  */
9420       qualified_p
9421         = (cp_parser_nested_name_specifier_opt (parser,
9422                                                 /*typename_keyword_p=*/false,
9423                                                 /*check_dependency_p=*/true,
9424                                                 /*type_p=*/false,
9425                                                 /*is_declaration=*/false)
9426            != NULL_TREE);
9427       /* If we have seen a nested-name-specifier, and the next token
9428          is `template', then we are using the template-id production.  */
9429       if (parser->scope
9430           && cp_parser_optional_template_keyword (parser))
9431         {
9432           /* Look for the template-id.  */
9433           type = cp_parser_template_id (parser,
9434                                         /*template_keyword_p=*/true,
9435                                         /*check_dependency_p=*/true,
9436                                         /*is_declaration=*/false);
9437           /* If the template-id did not name a type, we are out of
9438              luck.  */
9439           if (TREE_CODE (type) != TYPE_DECL)
9440             {
9441               cp_parser_error (parser, "expected template-id for type");
9442               type = NULL_TREE;
9443             }
9444         }
9445       /* Otherwise, look for a type-name.  */
9446       else
9447         type = cp_parser_type_name (parser);
9448       /* Keep track of all name-lookups performed in class scopes.  */
9449       if (type
9450           && !global_p
9451           && !qualified_p
9452           && TREE_CODE (type) == TYPE_DECL
9453           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9454         maybe_note_name_used_in_class (DECL_NAME (type), type);
9455       /* If it didn't work out, we don't have a TYPE.  */
9456       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9457           && !cp_parser_parse_definitely (parser))
9458         type = NULL_TREE;
9459       if (type && decl_specs)
9460         cp_parser_set_decl_spec_type (decl_specs, type,
9461                                       /*user_defined=*/true);
9462     }
9463
9464   /* If we didn't get a type-name, issue an error message.  */
9465   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9466     {
9467       cp_parser_error (parser, "expected type-name");
9468       return error_mark_node;
9469     }
9470
9471   /* There is no valid C++ program where a non-template type is
9472      followed by a "<".  That usually indicates that the user thought
9473      that the type was a template.  */
9474   if (type && type != error_mark_node)
9475     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9476
9477   return type;
9478 }
9479
9480 /* Parse a type-name.
9481
9482    type-name:
9483      class-name
9484      enum-name
9485      typedef-name
9486
9487    enum-name:
9488      identifier
9489
9490    typedef-name:
9491      identifier
9492
9493    Returns a TYPE_DECL for the the type.  */
9494
9495 static tree
9496 cp_parser_type_name (cp_parser* parser)
9497 {
9498   tree type_decl;
9499   tree identifier;
9500
9501   /* We can't know yet whether it is a class-name or not.  */
9502   cp_parser_parse_tentatively (parser);
9503   /* Try a class-name.  */
9504   type_decl = cp_parser_class_name (parser,
9505                                     /*typename_keyword_p=*/false,
9506                                     /*template_keyword_p=*/false,
9507                                     /*type_p=*/false,
9508                                     /*check_dependency_p=*/true,
9509                                     /*class_head_p=*/false,
9510                                     /*is_declaration=*/false);
9511   /* If it's not a class-name, keep looking.  */
9512   if (!cp_parser_parse_definitely (parser))
9513     {
9514       /* It must be a typedef-name or an enum-name.  */
9515       identifier = cp_parser_identifier (parser);
9516       if (identifier == error_mark_node)
9517         return error_mark_node;
9518
9519       /* Look up the type-name.  */
9520       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9521       /* Issue an error if we did not find a type-name.  */
9522       if (TREE_CODE (type_decl) != TYPE_DECL)
9523         {
9524           if (!cp_parser_simulate_error (parser))
9525             cp_parser_name_lookup_error (parser, identifier, type_decl,
9526                                          "is not a type");
9527           type_decl = error_mark_node;
9528         }
9529       /* Remember that the name was used in the definition of the
9530          current class so that we can check later to see if the
9531          meaning would have been different after the class was
9532          entirely defined.  */
9533       else if (type_decl != error_mark_node
9534                && !parser->scope)
9535         maybe_note_name_used_in_class (identifier, type_decl);
9536     }
9537
9538   return type_decl;
9539 }
9540
9541
9542 /* Parse an elaborated-type-specifier.  Note that the grammar given
9543    here incorporates the resolution to DR68.
9544
9545    elaborated-type-specifier:
9546      class-key :: [opt] nested-name-specifier [opt] identifier
9547      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9548      enum :: [opt] nested-name-specifier [opt] identifier
9549      typename :: [opt] nested-name-specifier identifier
9550      typename :: [opt] nested-name-specifier template [opt]
9551        template-id
9552
9553    GNU extension:
9554
9555    elaborated-type-specifier:
9556      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9557      class-key attributes :: [opt] nested-name-specifier [opt]
9558                template [opt] template-id
9559      enum attributes :: [opt] nested-name-specifier [opt] identifier
9560
9561    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9562    declared `friend'.  If IS_DECLARATION is TRUE, then this
9563    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9564    something is being declared.
9565
9566    Returns the TYPE specified.  */
9567
9568 static tree
9569 cp_parser_elaborated_type_specifier (cp_parser* parser,
9570                                      bool is_friend,
9571                                      bool is_declaration)
9572 {
9573   enum tag_types tag_type;
9574   tree identifier;
9575   tree type = NULL_TREE;
9576   tree attributes = NULL_TREE;
9577
9578   /* See if we're looking at the `enum' keyword.  */
9579   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9580     {
9581       /* Consume the `enum' token.  */
9582       cp_lexer_consume_token (parser->lexer);
9583       /* Remember that it's an enumeration type.  */
9584       tag_type = enum_type;
9585       /* Parse the attributes.  */
9586       attributes = cp_parser_attributes_opt (parser);
9587     }
9588   /* Or, it might be `typename'.  */
9589   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9590                                            RID_TYPENAME))
9591     {
9592       /* Consume the `typename' token.  */
9593       cp_lexer_consume_token (parser->lexer);
9594       /* Remember that it's a `typename' type.  */
9595       tag_type = typename_type;
9596       /* The `typename' keyword is only allowed in templates.  */
9597       if (!processing_template_decl)
9598         pedwarn ("using `typename' outside of template");
9599     }
9600   /* Otherwise it must be a class-key.  */
9601   else
9602     {
9603       tag_type = cp_parser_class_key (parser);
9604       if (tag_type == none_type)
9605         return error_mark_node;
9606       /* Parse the attributes.  */
9607       attributes = cp_parser_attributes_opt (parser);
9608     }
9609
9610   /* Look for the `::' operator.  */
9611   cp_parser_global_scope_opt (parser,
9612                               /*current_scope_valid_p=*/false);
9613   /* Look for the nested-name-specifier.  */
9614   if (tag_type == typename_type)
9615     {
9616       if (cp_parser_nested_name_specifier (parser,
9617                                            /*typename_keyword_p=*/true,
9618                                            /*check_dependency_p=*/true,
9619                                            /*type_p=*/true,
9620                                            is_declaration)
9621           == error_mark_node)
9622         return error_mark_node;
9623     }
9624   else
9625     /* Even though `typename' is not present, the proposed resolution
9626        to Core Issue 180 says that in `class A<T>::B', `B' should be
9627        considered a type-name, even if `A<T>' is dependent.  */
9628     cp_parser_nested_name_specifier_opt (parser,
9629                                          /*typename_keyword_p=*/true,
9630                                          /*check_dependency_p=*/true,
9631                                          /*type_p=*/true,
9632                                          is_declaration);
9633   /* For everything but enumeration types, consider a template-id.  */
9634   if (tag_type != enum_type)
9635     {
9636       bool template_p = false;
9637       tree decl;
9638
9639       /* Allow the `template' keyword.  */
9640       template_p = cp_parser_optional_template_keyword (parser);
9641       /* If we didn't see `template', we don't know if there's a
9642          template-id or not.  */
9643       if (!template_p)
9644         cp_parser_parse_tentatively (parser);
9645       /* Parse the template-id.  */
9646       decl = cp_parser_template_id (parser, template_p,
9647                                     /*check_dependency_p=*/true,
9648                                     is_declaration);
9649       /* If we didn't find a template-id, look for an ordinary
9650          identifier.  */
9651       if (!template_p && !cp_parser_parse_definitely (parser))
9652         ;
9653       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9654          in effect, then we must assume that, upon instantiation, the
9655          template will correspond to a class.  */
9656       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9657                && tag_type == typename_type)
9658         type = make_typename_type (parser->scope, decl,
9659                                    /*complain=*/1);
9660       else
9661         type = TREE_TYPE (decl);
9662     }
9663
9664   /* For an enumeration type, consider only a plain identifier.  */
9665   if (!type)
9666     {
9667       identifier = cp_parser_identifier (parser);
9668
9669       if (identifier == error_mark_node)
9670         {
9671           parser->scope = NULL_TREE;
9672           return error_mark_node;
9673         }
9674
9675       /* For a `typename', we needn't call xref_tag.  */
9676       if (tag_type == typename_type)
9677         return cp_parser_make_typename_type (parser, parser->scope,
9678                                              identifier);
9679       /* Look up a qualified name in the usual way.  */
9680       if (parser->scope)
9681         {
9682           tree decl;
9683
9684           /* In an elaborated-type-specifier, names are assumed to name
9685              types, so we set IS_TYPE to TRUE when calling
9686              cp_parser_lookup_name.  */
9687           decl = cp_parser_lookup_name (parser, identifier,
9688                                         /*is_type=*/true,
9689                                         /*is_template=*/false,
9690                                         /*is_namespace=*/false,
9691                                         /*check_dependency=*/true,
9692                                         /*ambiguous_p=*/NULL);
9693
9694           /* If we are parsing friend declaration, DECL may be a
9695              TEMPLATE_DECL tree node here.  However, we need to check
9696              whether this TEMPLATE_DECL results in valid code.  Consider
9697              the following example:
9698
9699                namespace N {
9700                  template <class T> class C {};
9701                }
9702                class X {
9703                  template <class T> friend class N::C; // #1, valid code
9704                };
9705                template <class T> class Y {
9706                  friend class N::C;                    // #2, invalid code
9707                };
9708
9709              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9710              name lookup of `N::C'.  We see that friend declaration must
9711              be template for the code to be valid.  Note that
9712              processing_template_decl does not work here since it is
9713              always 1 for the above two cases.  */
9714
9715           decl = (cp_parser_maybe_treat_template_as_class
9716                   (decl, /*tag_name_p=*/is_friend
9717                          && parser->num_template_parameter_lists));
9718
9719           if (TREE_CODE (decl) != TYPE_DECL)
9720             {
9721               error ("expected type-name");
9722               return error_mark_node;
9723             }
9724
9725           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9726             check_elaborated_type_specifier
9727               (tag_type, decl,
9728                (parser->num_template_parameter_lists
9729                 || DECL_SELF_REFERENCE_P (decl)));
9730
9731           type = TREE_TYPE (decl);
9732         }
9733       else
9734         {
9735           /* An elaborated-type-specifier sometimes introduces a new type and
9736              sometimes names an existing type.  Normally, the rule is that it
9737              introduces a new type only if there is not an existing type of
9738              the same name already in scope.  For example, given:
9739
9740                struct S {};
9741                void f() { struct S s; }
9742
9743              the `struct S' in the body of `f' is the same `struct S' as in
9744              the global scope; the existing definition is used.  However, if
9745              there were no global declaration, this would introduce a new
9746              local class named `S'.
9747
9748              An exception to this rule applies to the following code:
9749
9750                namespace N { struct S; }
9751
9752              Here, the elaborated-type-specifier names a new type
9753              unconditionally; even if there is already an `S' in the
9754              containing scope this declaration names a new type.
9755              This exception only applies if the elaborated-type-specifier
9756              forms the complete declaration:
9757
9758                [class.name]
9759
9760                A declaration consisting solely of `class-key identifier ;' is
9761                either a redeclaration of the name in the current scope or a
9762                forward declaration of the identifier as a class name.  It
9763                introduces the name into the current scope.
9764
9765              We are in this situation precisely when the next token is a `;'.
9766
9767              An exception to the exception is that a `friend' declaration does
9768              *not* name a new type; i.e., given:
9769
9770                struct S { friend struct T; };
9771
9772              `T' is not a new type in the scope of `S'.
9773
9774              Also, `new struct S' or `sizeof (struct S)' never results in the
9775              definition of a new type; a new type can only be declared in a
9776              declaration context.  */
9777
9778           /* Warn about attributes. They are ignored.  */
9779           if (attributes)
9780             warning ("type attributes are honored only at type definition");
9781
9782           type = xref_tag (tag_type, identifier,
9783                            (is_friend
9784                             || !is_declaration
9785                             || cp_lexer_next_token_is_not (parser->lexer,
9786                                                            CPP_SEMICOLON)),
9787                            parser->num_template_parameter_lists);
9788         }
9789     }
9790   if (tag_type != enum_type)
9791     cp_parser_check_class_key (tag_type, type);
9792
9793   /* A "<" cannot follow an elaborated type specifier.  If that
9794      happens, the user was probably trying to form a template-id.  */
9795   cp_parser_check_for_invalid_template_id (parser, type);
9796
9797   return type;
9798 }
9799
9800 /* Parse an enum-specifier.
9801
9802    enum-specifier:
9803      enum identifier [opt] { enumerator-list [opt] }
9804
9805    Returns an ENUM_TYPE representing the enumeration.  */
9806
9807 static tree
9808 cp_parser_enum_specifier (cp_parser* parser)
9809 {
9810   tree identifier;
9811   tree type;
9812
9813   /* Caller guarantees that the current token is 'enum', an identifier
9814      possibly follows, and the token after that is an opening brace.
9815      If we don't have an identifier, fabricate an anonymous name for
9816      the enumeration being defined.  */
9817   cp_lexer_consume_token (parser->lexer);
9818
9819   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9820     identifier = cp_parser_identifier (parser);
9821   else
9822     identifier = make_anon_name ();
9823
9824   /* Issue an error message if type-definitions are forbidden here.  */
9825   cp_parser_check_type_definition (parser);
9826
9827   /* Create the new type.  We do this before consuming the opening brace
9828      so the enum will be recorded as being on the line of its tag (or the
9829      'enum' keyword, if there is no tag).  */
9830   type = start_enum (identifier);
9831
9832   /* Consume the opening brace.  */
9833   cp_lexer_consume_token (parser->lexer);
9834
9835   /* If the next token is not '}', then there are some enumerators.  */
9836   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9837     cp_parser_enumerator_list (parser, type);
9838
9839   /* Consume the final '}'.  */
9840   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9841
9842   /* Finish up the enumeration.  */
9843   finish_enum (type);
9844
9845   return type;
9846 }
9847
9848 /* Parse an enumerator-list.  The enumerators all have the indicated
9849    TYPE.
9850
9851    enumerator-list:
9852      enumerator-definition
9853      enumerator-list , enumerator-definition  */
9854
9855 static void
9856 cp_parser_enumerator_list (cp_parser* parser, tree type)
9857 {
9858   while (true)
9859     {
9860       /* Parse an enumerator-definition.  */
9861       cp_parser_enumerator_definition (parser, type);
9862
9863       /* If the next token is not a ',', we've reached the end of
9864          the list.  */
9865       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9866         break;
9867       /* Otherwise, consume the `,' and keep going.  */
9868       cp_lexer_consume_token (parser->lexer);
9869       /* If the next token is a `}', there is a trailing comma.  */
9870       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9871         {
9872           if (pedantic && !in_system_header)
9873             pedwarn ("comma at end of enumerator list");
9874           break;
9875         }
9876     }
9877 }
9878
9879 /* Parse an enumerator-definition.  The enumerator has the indicated
9880    TYPE.
9881
9882    enumerator-definition:
9883      enumerator
9884      enumerator = constant-expression
9885
9886    enumerator:
9887      identifier  */
9888
9889 static void
9890 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9891 {
9892   tree identifier;
9893   tree value;
9894
9895   /* Look for the identifier.  */
9896   identifier = cp_parser_identifier (parser);
9897   if (identifier == error_mark_node)
9898     return;
9899
9900   /* If the next token is an '=', then there is an explicit value.  */
9901   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9902     {
9903       /* Consume the `=' token.  */
9904       cp_lexer_consume_token (parser->lexer);
9905       /* Parse the value.  */
9906       value = cp_parser_constant_expression (parser,
9907                                              /*allow_non_constant_p=*/false,
9908                                              NULL);
9909     }
9910   else
9911     value = NULL_TREE;
9912
9913   /* Create the enumerator.  */
9914   build_enumerator (identifier, value, type);
9915 }
9916
9917 /* Parse a namespace-name.
9918
9919    namespace-name:
9920      original-namespace-name
9921      namespace-alias
9922
9923    Returns the NAMESPACE_DECL for the namespace.  */
9924
9925 static tree
9926 cp_parser_namespace_name (cp_parser* parser)
9927 {
9928   tree identifier;
9929   tree namespace_decl;
9930
9931   /* Get the name of the namespace.  */
9932   identifier = cp_parser_identifier (parser);
9933   if (identifier == error_mark_node)
9934     return error_mark_node;
9935
9936   /* Look up the identifier in the currently active scope.  Look only
9937      for namespaces, due to:
9938
9939        [basic.lookup.udir]
9940
9941        When looking up a namespace-name in a using-directive or alias
9942        definition, only namespace names are considered.
9943
9944      And:
9945
9946        [basic.lookup.qual]
9947
9948        During the lookup of a name preceding the :: scope resolution
9949        operator, object, function, and enumerator names are ignored.
9950
9951      (Note that cp_parser_class_or_namespace_name only calls this
9952      function if the token after the name is the scope resolution
9953      operator.)  */
9954   namespace_decl = cp_parser_lookup_name (parser, identifier,
9955                                           /*is_type=*/false,
9956                                           /*is_template=*/false,
9957                                           /*is_namespace=*/true,
9958                                           /*check_dependency=*/true,
9959                                           /*ambiguous_p=*/NULL);
9960   /* If it's not a namespace, issue an error.  */
9961   if (namespace_decl == error_mark_node
9962       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9963     {
9964       cp_parser_error (parser, "expected namespace-name");
9965       namespace_decl = error_mark_node;
9966     }
9967
9968   return namespace_decl;
9969 }
9970
9971 /* Parse a namespace-definition.
9972
9973    namespace-definition:
9974      named-namespace-definition
9975      unnamed-namespace-definition
9976
9977    named-namespace-definition:
9978      original-namespace-definition
9979      extension-namespace-definition
9980
9981    original-namespace-definition:
9982      namespace identifier { namespace-body }
9983
9984    extension-namespace-definition:
9985      namespace original-namespace-name { namespace-body }
9986
9987    unnamed-namespace-definition:
9988      namespace { namespace-body } */
9989
9990 static void
9991 cp_parser_namespace_definition (cp_parser* parser)
9992 {
9993   tree identifier;
9994
9995   /* Look for the `namespace' keyword.  */
9996   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9997
9998   /* Get the name of the namespace.  We do not attempt to distinguish
9999      between an original-namespace-definition and an
10000      extension-namespace-definition at this point.  The semantic
10001      analysis routines are responsible for that.  */
10002   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10003     identifier = cp_parser_identifier (parser);
10004   else
10005     identifier = NULL_TREE;
10006
10007   /* Look for the `{' to start the namespace.  */
10008   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10009   /* Start the namespace.  */
10010   push_namespace (identifier);
10011   /* Parse the body of the namespace.  */
10012   cp_parser_namespace_body (parser);
10013   /* Finish the namespace.  */
10014   pop_namespace ();
10015   /* Look for the final `}'.  */
10016   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10017 }
10018
10019 /* Parse a namespace-body.
10020
10021    namespace-body:
10022      declaration-seq [opt]  */
10023
10024 static void
10025 cp_parser_namespace_body (cp_parser* parser)
10026 {
10027   cp_parser_declaration_seq_opt (parser);
10028 }
10029
10030 /* Parse a namespace-alias-definition.
10031
10032    namespace-alias-definition:
10033      namespace identifier = qualified-namespace-specifier ;  */
10034
10035 static void
10036 cp_parser_namespace_alias_definition (cp_parser* parser)
10037 {
10038   tree identifier;
10039   tree namespace_specifier;
10040
10041   /* Look for the `namespace' keyword.  */
10042   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10043   /* Look for the identifier.  */
10044   identifier = cp_parser_identifier (parser);
10045   if (identifier == error_mark_node)
10046     return;
10047   /* Look for the `=' token.  */
10048   cp_parser_require (parser, CPP_EQ, "`='");
10049   /* Look for the qualified-namespace-specifier.  */
10050   namespace_specifier
10051     = cp_parser_qualified_namespace_specifier (parser);
10052   /* Look for the `;' token.  */
10053   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10054
10055   /* Register the alias in the symbol table.  */
10056   do_namespace_alias (identifier, namespace_specifier);
10057 }
10058
10059 /* Parse a qualified-namespace-specifier.
10060
10061    qualified-namespace-specifier:
10062      :: [opt] nested-name-specifier [opt] namespace-name
10063
10064    Returns a NAMESPACE_DECL corresponding to the specified
10065    namespace.  */
10066
10067 static tree
10068 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10069 {
10070   /* Look for the optional `::'.  */
10071   cp_parser_global_scope_opt (parser,
10072                               /*current_scope_valid_p=*/false);
10073
10074   /* Look for the optional nested-name-specifier.  */
10075   cp_parser_nested_name_specifier_opt (parser,
10076                                        /*typename_keyword_p=*/false,
10077                                        /*check_dependency_p=*/true,
10078                                        /*type_p=*/false,
10079                                        /*is_declaration=*/true);
10080
10081   return cp_parser_namespace_name (parser);
10082 }
10083
10084 /* Parse a using-declaration.
10085
10086    using-declaration:
10087      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10088      using :: unqualified-id ;  */
10089
10090 static void
10091 cp_parser_using_declaration (cp_parser* parser)
10092 {
10093   cp_token *token;
10094   bool typename_p = false;
10095   bool global_scope_p;
10096   tree decl;
10097   tree identifier;
10098   tree scope;
10099   tree qscope;
10100
10101   /* Look for the `using' keyword.  */
10102   cp_parser_require_keyword (parser, RID_USING, "`using'");
10103
10104   /* Peek at the next token.  */
10105   token = cp_lexer_peek_token (parser->lexer);
10106   /* See if it's `typename'.  */
10107   if (token->keyword == RID_TYPENAME)
10108     {
10109       /* Remember that we've seen it.  */
10110       typename_p = true;
10111       /* Consume the `typename' token.  */
10112       cp_lexer_consume_token (parser->lexer);
10113     }
10114
10115   /* Look for the optional global scope qualification.  */
10116   global_scope_p
10117     = (cp_parser_global_scope_opt (parser,
10118                                    /*current_scope_valid_p=*/false)
10119        != NULL_TREE);
10120
10121   /* If we saw `typename', or didn't see `::', then there must be a
10122      nested-name-specifier present.  */
10123   if (typename_p || !global_scope_p)
10124     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10125                                               /*check_dependency_p=*/true,
10126                                               /*type_p=*/false,
10127                                               /*is_declaration=*/true);
10128   /* Otherwise, we could be in either of the two productions.  In that
10129      case, treat the nested-name-specifier as optional.  */
10130   else
10131     qscope = cp_parser_nested_name_specifier_opt (parser,
10132                                                   /*typename_keyword_p=*/false,
10133                                                   /*check_dependency_p=*/true,
10134                                                   /*type_p=*/false,
10135                                                   /*is_declaration=*/true);
10136   if (!qscope)
10137     qscope = global_namespace;
10138
10139   /* Parse the unqualified-id.  */
10140   identifier = cp_parser_unqualified_id (parser,
10141                                          /*template_keyword_p=*/false,
10142                                          /*check_dependency_p=*/true,
10143                                          /*declarator_p=*/true);
10144
10145   /* The function we call to handle a using-declaration is different
10146      depending on what scope we are in.  */
10147   if (identifier == error_mark_node)
10148     ;
10149   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10150            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10151     /* [namespace.udecl]
10152
10153        A using declaration shall not name a template-id.  */
10154     error ("a template-id may not appear in a using-declaration");
10155   else
10156     {
10157       scope = current_scope ();
10158       if (scope && TYPE_P (scope))
10159         {
10160           /* Create the USING_DECL.  */
10161           decl = do_class_using_decl (build_nt (SCOPE_REF,
10162                                                 parser->scope,
10163                                                 identifier));
10164           /* Add it to the list of members in this class.  */
10165           finish_member_declaration (decl);
10166         }
10167       else
10168         {
10169           decl = cp_parser_lookup_name_simple (parser, identifier);
10170           if (decl == error_mark_node)
10171             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10172           else if (scope)
10173             do_local_using_decl (decl, qscope, identifier);
10174           else
10175             do_toplevel_using_decl (decl, qscope, identifier);
10176         }
10177     }
10178
10179   /* Look for the final `;'.  */
10180   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10181 }
10182
10183 /* Parse a using-directive.
10184
10185    using-directive:
10186      using namespace :: [opt] nested-name-specifier [opt]
10187        namespace-name ;  */
10188
10189 static void
10190 cp_parser_using_directive (cp_parser* parser)
10191 {
10192   tree namespace_decl;
10193   tree attribs;
10194
10195   /* Look for the `using' keyword.  */
10196   cp_parser_require_keyword (parser, RID_USING, "`using'");
10197   /* And the `namespace' keyword.  */
10198   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10199   /* Look for the optional `::' operator.  */
10200   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10201   /* And the optional nested-name-specifier.  */
10202   cp_parser_nested_name_specifier_opt (parser,
10203                                        /*typename_keyword_p=*/false,
10204                                        /*check_dependency_p=*/true,
10205                                        /*type_p=*/false,
10206                                        /*is_declaration=*/true);
10207   /* Get the namespace being used.  */
10208   namespace_decl = cp_parser_namespace_name (parser);
10209   /* And any specified attributes.  */
10210   attribs = cp_parser_attributes_opt (parser);
10211   /* Update the symbol table.  */
10212   parse_using_directive (namespace_decl, attribs);
10213   /* Look for the final `;'.  */
10214   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10215 }
10216
10217 /* Parse an asm-definition.
10218
10219    asm-definition:
10220      asm ( string-literal ) ;
10221
10222    GNU Extension:
10223
10224    asm-definition:
10225      asm volatile [opt] ( string-literal ) ;
10226      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10227      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10228                           : asm-operand-list [opt] ) ;
10229      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10230                           : asm-operand-list [opt]
10231                           : asm-operand-list [opt] ) ;  */
10232
10233 static void
10234 cp_parser_asm_definition (cp_parser* parser)
10235 {
10236   tree string;
10237   tree outputs = NULL_TREE;
10238   tree inputs = NULL_TREE;
10239   tree clobbers = NULL_TREE;
10240   tree asm_stmt;
10241   bool volatile_p = false;
10242   bool extended_p = false;
10243
10244   /* Look for the `asm' keyword.  */
10245   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10246   /* See if the next token is `volatile'.  */
10247   if (cp_parser_allow_gnu_extensions_p (parser)
10248       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10249     {
10250       /* Remember that we saw the `volatile' keyword.  */
10251       volatile_p = true;
10252       /* Consume the token.  */
10253       cp_lexer_consume_token (parser->lexer);
10254     }
10255   /* Look for the opening `('.  */
10256   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10257     return;
10258   /* Look for the string.  */
10259   string = cp_parser_string_literal (parser, false, false);
10260   if (string == error_mark_node)
10261     {
10262       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10263                                              /*consume_paren=*/true);
10264       return;
10265     }
10266
10267   /* If we're allowing GNU extensions, check for the extended assembly
10268      syntax.  Unfortunately, the `:' tokens need not be separated by
10269      a space in C, and so, for compatibility, we tolerate that here
10270      too.  Doing that means that we have to treat the `::' operator as
10271      two `:' tokens.  */
10272   if (cp_parser_allow_gnu_extensions_p (parser)
10273       && at_function_scope_p ()
10274       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10275           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10276     {
10277       bool inputs_p = false;
10278       bool clobbers_p = false;
10279
10280       /* The extended syntax was used.  */
10281       extended_p = true;
10282
10283       /* Look for outputs.  */
10284       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10285         {
10286           /* Consume the `:'.  */
10287           cp_lexer_consume_token (parser->lexer);
10288           /* Parse the output-operands.  */
10289           if (cp_lexer_next_token_is_not (parser->lexer,
10290                                           CPP_COLON)
10291               && cp_lexer_next_token_is_not (parser->lexer,
10292                                              CPP_SCOPE)
10293               && cp_lexer_next_token_is_not (parser->lexer,
10294                                              CPP_CLOSE_PAREN))
10295             outputs = cp_parser_asm_operand_list (parser);
10296         }
10297       /* If the next token is `::', there are no outputs, and the
10298          next token is the beginning of the inputs.  */
10299       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10300         /* The inputs are coming next.  */
10301         inputs_p = true;
10302
10303       /* Look for inputs.  */
10304       if (inputs_p
10305           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10306         {
10307           /* Consume the `:' or `::'.  */
10308           cp_lexer_consume_token (parser->lexer);
10309           /* Parse the output-operands.  */
10310           if (cp_lexer_next_token_is_not (parser->lexer,
10311                                           CPP_COLON)
10312               && cp_lexer_next_token_is_not (parser->lexer,
10313                                              CPP_CLOSE_PAREN))
10314             inputs = cp_parser_asm_operand_list (parser);
10315         }
10316       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10317         /* The clobbers are coming next.  */
10318         clobbers_p = true;
10319
10320       /* Look for clobbers.  */
10321       if (clobbers_p
10322           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10323         {
10324           /* Consume the `:' or `::'.  */
10325           cp_lexer_consume_token (parser->lexer);
10326           /* Parse the clobbers.  */
10327           if (cp_lexer_next_token_is_not (parser->lexer,
10328                                           CPP_CLOSE_PAREN))
10329             clobbers = cp_parser_asm_clobber_list (parser);
10330         }
10331     }
10332   /* Look for the closing `)'.  */
10333   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10334     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10335                                            /*consume_paren=*/true);
10336   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10337
10338   /* Create the ASM_EXPR.  */
10339   if (at_function_scope_p ())
10340     {
10341       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10342                                   inputs, clobbers);
10343       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10344       if (!extended_p)
10345         ASM_INPUT_P (asm_stmt) = 1;
10346     }
10347   else
10348     assemble_asm (string);
10349 }
10350
10351 /* Declarators [gram.dcl.decl] */
10352
10353 /* Parse an init-declarator.
10354
10355    init-declarator:
10356      declarator initializer [opt]
10357
10358    GNU Extension:
10359
10360    init-declarator:
10361      declarator asm-specification [opt] attributes [opt] initializer [opt]
10362
10363    function-definition:
10364      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10365        function-body
10366      decl-specifier-seq [opt] declarator function-try-block
10367
10368    GNU Extension:
10369
10370    function-definition:
10371      __extension__ function-definition
10372
10373    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10374    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10375    then this declarator appears in a class scope.  The new DECL created
10376    by this declarator is returned.
10377
10378    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10379    for a function-definition here as well.  If the declarator is a
10380    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10381    be TRUE upon return.  By that point, the function-definition will
10382    have been completely parsed.
10383
10384    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10385    is FALSE.  */
10386
10387 static tree
10388 cp_parser_init_declarator (cp_parser* parser,
10389                            cp_decl_specifier_seq *decl_specifiers,
10390                            bool function_definition_allowed_p,
10391                            bool member_p,
10392                            int declares_class_or_enum,
10393                            bool* function_definition_p)
10394 {
10395   cp_token *token;
10396   cp_declarator *declarator;
10397   tree prefix_attributes;
10398   tree attributes;
10399   tree asm_specification;
10400   tree initializer;
10401   tree decl = NULL_TREE;
10402   tree scope;
10403   bool is_initialized;
10404   bool is_parenthesized_init;
10405   bool is_non_constant_init;
10406   int ctor_dtor_or_conv_p;
10407   bool friend_p;
10408   bool pop_p = false;
10409
10410   /* Gather the attributes that were provided with the
10411      decl-specifiers.  */
10412   prefix_attributes = decl_specifiers->attributes;
10413
10414   /* Assume that this is not the declarator for a function
10415      definition.  */
10416   if (function_definition_p)
10417     *function_definition_p = false;
10418
10419   /* Defer access checks while parsing the declarator; we cannot know
10420      what names are accessible until we know what is being
10421      declared.  */
10422   resume_deferring_access_checks ();
10423
10424   /* Parse the declarator.  */
10425   declarator
10426     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10427                             &ctor_dtor_or_conv_p,
10428                             /*parenthesized_p=*/NULL);
10429   /* Gather up the deferred checks.  */
10430   stop_deferring_access_checks ();
10431
10432   /* If the DECLARATOR was erroneous, there's no need to go
10433      further.  */
10434   if (declarator == cp_error_declarator)
10435     return error_mark_node;
10436
10437   cp_parser_check_for_definition_in_return_type (declarator,
10438                                                  declares_class_or_enum);
10439
10440   /* Figure out what scope the entity declared by the DECLARATOR is
10441      located in.  `grokdeclarator' sometimes changes the scope, so
10442      we compute it now.  */
10443   scope = get_scope_of_declarator (declarator);
10444
10445   /* If we're allowing GNU extensions, look for an asm-specification
10446      and attributes.  */
10447   if (cp_parser_allow_gnu_extensions_p (parser))
10448     {
10449       /* Look for an asm-specification.  */
10450       asm_specification = cp_parser_asm_specification_opt (parser);
10451       /* And attributes.  */
10452       attributes = cp_parser_attributes_opt (parser);
10453     }
10454   else
10455     {
10456       asm_specification = NULL_TREE;
10457       attributes = NULL_TREE;
10458     }
10459
10460   /* Peek at the next token.  */
10461   token = cp_lexer_peek_token (parser->lexer);
10462   /* Check to see if the token indicates the start of a
10463      function-definition.  */
10464   if (cp_parser_token_starts_function_definition_p (token))
10465     {
10466       if (!function_definition_allowed_p)
10467         {
10468           /* If a function-definition should not appear here, issue an
10469              error message.  */
10470           cp_parser_error (parser,
10471                            "a function-definition is not allowed here");
10472           return error_mark_node;
10473         }
10474       else
10475         {
10476           /* Neither attributes nor an asm-specification are allowed
10477              on a function-definition.  */
10478           if (asm_specification)
10479             error ("an asm-specification is not allowed on a function-definition");
10480           if (attributes)
10481             error ("attributes are not allowed on a function-definition");
10482           /* This is a function-definition.  */
10483           *function_definition_p = true;
10484
10485           /* Parse the function definition.  */
10486           if (member_p)
10487             decl = cp_parser_save_member_function_body (parser,
10488                                                         decl_specifiers,
10489                                                         declarator,
10490                                                         prefix_attributes);
10491           else
10492             decl
10493               = (cp_parser_function_definition_from_specifiers_and_declarator
10494                  (parser, decl_specifiers, prefix_attributes, declarator));
10495
10496           return decl;
10497         }
10498     }
10499
10500   /* [dcl.dcl]
10501
10502      Only in function declarations for constructors, destructors, and
10503      type conversions can the decl-specifier-seq be omitted.
10504
10505      We explicitly postpone this check past the point where we handle
10506      function-definitions because we tolerate function-definitions
10507      that are missing their return types in some modes.  */
10508   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10509     {
10510       cp_parser_error (parser,
10511                        "expected constructor, destructor, or type conversion");
10512       return error_mark_node;
10513     }
10514
10515   /* An `=' or an `(' indicates an initializer.  */
10516   is_initialized = (token->type == CPP_EQ
10517                      || token->type == CPP_OPEN_PAREN);
10518   /* If the init-declarator isn't initialized and isn't followed by a
10519      `,' or `;', it's not a valid init-declarator.  */
10520   if (!is_initialized
10521       && token->type != CPP_COMMA
10522       && token->type != CPP_SEMICOLON)
10523     {
10524       cp_parser_error (parser, "expected initializer");
10525       return error_mark_node;
10526     }
10527
10528   /* Because start_decl has side-effects, we should only call it if we
10529      know we're going ahead.  By this point, we know that we cannot
10530      possibly be looking at any other construct.  */
10531   cp_parser_commit_to_tentative_parse (parser);
10532
10533   /* If the decl specifiers were bad, issue an error now that we're
10534      sure this was intended to be a declarator.  Then continue
10535      declaring the variable(s), as int, to try to cut down on further
10536      errors.  */
10537   if (decl_specifiers->any_specifiers_p
10538       && decl_specifiers->type == error_mark_node)
10539     {
10540       cp_parser_error (parser, "invalid type in declaration");
10541       decl_specifiers->type = integer_type_node;
10542     }
10543
10544   /* Check to see whether or not this declaration is a friend.  */
10545   friend_p = cp_parser_friend_p (decl_specifiers);
10546
10547   /* Check that the number of template-parameter-lists is OK.  */
10548   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10549     return error_mark_node;
10550
10551   /* Enter the newly declared entry in the symbol table.  If we're
10552      processing a declaration in a class-specifier, we wait until
10553      after processing the initializer.  */
10554   if (!member_p)
10555     {
10556       if (parser->in_unbraced_linkage_specification_p)
10557         {
10558           decl_specifiers->storage_class = sc_extern;
10559           have_extern_spec = false;
10560         }
10561       decl = start_decl (declarator, decl_specifiers,
10562                          is_initialized, attributes, prefix_attributes,
10563                          &pop_p);
10564     }
10565   else if (scope)
10566     /* Enter the SCOPE.  That way unqualified names appearing in the
10567        initializer will be looked up in SCOPE.  */
10568     pop_p = push_scope (scope);
10569
10570   /* Perform deferred access control checks, now that we know in which
10571      SCOPE the declared entity resides.  */
10572   if (!member_p && decl)
10573     {
10574       tree saved_current_function_decl = NULL_TREE;
10575
10576       /* If the entity being declared is a function, pretend that we
10577          are in its scope.  If it is a `friend', it may have access to
10578          things that would not otherwise be accessible.  */
10579       if (TREE_CODE (decl) == FUNCTION_DECL)
10580         {
10581           saved_current_function_decl = current_function_decl;
10582           current_function_decl = decl;
10583         }
10584
10585       /* Perform the access control checks for the declarator and the
10586          the decl-specifiers.  */
10587       perform_deferred_access_checks ();
10588
10589       /* Restore the saved value.  */
10590       if (TREE_CODE (decl) == FUNCTION_DECL)
10591         current_function_decl = saved_current_function_decl;
10592     }
10593
10594   /* Parse the initializer.  */
10595   if (is_initialized)
10596     initializer = cp_parser_initializer (parser,
10597                                          &is_parenthesized_init,
10598                                          &is_non_constant_init);
10599   else
10600     {
10601       initializer = NULL_TREE;
10602       is_parenthesized_init = false;
10603       is_non_constant_init = true;
10604     }
10605
10606   /* The old parser allows attributes to appear after a parenthesized
10607      initializer.  Mark Mitchell proposed removing this functionality
10608      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10609      attributes -- but ignores them.  */
10610   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10611     if (cp_parser_attributes_opt (parser))
10612       warning ("attributes after parenthesized initializer ignored");
10613
10614   /* For an in-class declaration, use `grokfield' to create the
10615      declaration.  */
10616   if (member_p)
10617     {
10618       if (pop_p)
10619         pop_scope (scope);
10620       decl = grokfield (declarator, decl_specifiers,
10621                         initializer, /*asmspec=*/NULL_TREE,
10622                         /*attributes=*/NULL_TREE);
10623       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10624         cp_parser_save_default_args (parser, decl);
10625     }
10626
10627   /* Finish processing the declaration.  But, skip friend
10628      declarations.  */
10629   if (!friend_p && decl && decl != error_mark_node)
10630     {
10631       cp_finish_decl (decl,
10632                       initializer,
10633                       asm_specification,
10634                       /* If the initializer is in parentheses, then this is
10635                          a direct-initialization, which means that an
10636                          `explicit' constructor is OK.  Otherwise, an
10637                          `explicit' constructor cannot be used.  */
10638                       ((is_parenthesized_init || !is_initialized)
10639                      ? 0 : LOOKUP_ONLYCONVERTING));
10640       if (pop_p)
10641         pop_scope (DECL_CONTEXT (decl));
10642     }
10643
10644   /* Remember whether or not variables were initialized by
10645      constant-expressions.  */
10646   if (decl && TREE_CODE (decl) == VAR_DECL
10647       && is_initialized && !is_non_constant_init)
10648     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10649
10650   return decl;
10651 }
10652
10653 /* Parse a declarator.
10654
10655    declarator:
10656      direct-declarator
10657      ptr-operator declarator
10658
10659    abstract-declarator:
10660      ptr-operator abstract-declarator [opt]
10661      direct-abstract-declarator
10662
10663    GNU Extensions:
10664
10665    declarator:
10666      attributes [opt] direct-declarator
10667      attributes [opt] ptr-operator declarator
10668
10669    abstract-declarator:
10670      attributes [opt] ptr-operator abstract-declarator [opt]
10671      attributes [opt] direct-abstract-declarator
10672
10673    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10674    detect constructor, destructor or conversion operators. It is set
10675    to -1 if the declarator is a name, and +1 if it is a
10676    function. Otherwise it is set to zero. Usually you just want to
10677    test for >0, but internally the negative value is used.
10678
10679    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10680    a decl-specifier-seq unless it declares a constructor, destructor,
10681    or conversion.  It might seem that we could check this condition in
10682    semantic analysis, rather than parsing, but that makes it difficult
10683    to handle something like `f()'.  We want to notice that there are
10684    no decl-specifiers, and therefore realize that this is an
10685    expression, not a declaration.)
10686
10687    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10688    the declarator is a direct-declarator of the form "(...)".  */
10689
10690 static cp_declarator *
10691 cp_parser_declarator (cp_parser* parser,
10692                       cp_parser_declarator_kind dcl_kind,
10693                       int* ctor_dtor_or_conv_p,
10694                       bool* parenthesized_p)
10695 {
10696   cp_token *token;
10697   cp_declarator *declarator;
10698   enum tree_code code;
10699   cp_cv_quals cv_quals;
10700   tree class_type;
10701   tree attributes = NULL_TREE;
10702
10703   /* Assume this is not a constructor, destructor, or type-conversion
10704      operator.  */
10705   if (ctor_dtor_or_conv_p)
10706     *ctor_dtor_or_conv_p = 0;
10707
10708   if (cp_parser_allow_gnu_extensions_p (parser))
10709     attributes = cp_parser_attributes_opt (parser);
10710
10711   /* Peek at the next token.  */
10712   token = cp_lexer_peek_token (parser->lexer);
10713
10714   /* Check for the ptr-operator production.  */
10715   cp_parser_parse_tentatively (parser);
10716   /* Parse the ptr-operator.  */
10717   code = cp_parser_ptr_operator (parser,
10718                                  &class_type,
10719                                  &cv_quals);
10720   /* If that worked, then we have a ptr-operator.  */
10721   if (cp_parser_parse_definitely (parser))
10722     {
10723       /* If a ptr-operator was found, then this declarator was not
10724          parenthesized.  */
10725       if (parenthesized_p)
10726         *parenthesized_p = true;
10727       /* The dependent declarator is optional if we are parsing an
10728          abstract-declarator.  */
10729       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10730         cp_parser_parse_tentatively (parser);
10731
10732       /* Parse the dependent declarator.  */
10733       declarator = cp_parser_declarator (parser, dcl_kind,
10734                                          /*ctor_dtor_or_conv_p=*/NULL,
10735                                          /*parenthesized_p=*/NULL);
10736
10737       /* If we are parsing an abstract-declarator, we must handle the
10738          case where the dependent declarator is absent.  */
10739       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10740           && !cp_parser_parse_definitely (parser))
10741         declarator = NULL;
10742
10743       /* Build the representation of the ptr-operator.  */
10744       if (class_type)
10745         declarator = make_ptrmem_declarator (cv_quals,
10746                                              class_type,
10747                                              declarator);
10748       else if (code == INDIRECT_REF)
10749         declarator = make_pointer_declarator (cv_quals, declarator);
10750       else
10751         declarator = make_reference_declarator (cv_quals, declarator);
10752     }
10753   /* Everything else is a direct-declarator.  */
10754   else
10755     {
10756       if (parenthesized_p)
10757         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10758                                                    CPP_OPEN_PAREN);
10759       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10760                                                 ctor_dtor_or_conv_p);
10761     }
10762
10763   if (attributes && declarator != cp_error_declarator)
10764     declarator->attributes = attributes;
10765
10766   return declarator;
10767 }
10768
10769 /* Parse a direct-declarator or direct-abstract-declarator.
10770
10771    direct-declarator:
10772      declarator-id
10773      direct-declarator ( parameter-declaration-clause )
10774        cv-qualifier-seq [opt]
10775        exception-specification [opt]
10776      direct-declarator [ constant-expression [opt] ]
10777      ( declarator )
10778
10779    direct-abstract-declarator:
10780      direct-abstract-declarator [opt]
10781        ( parameter-declaration-clause )
10782        cv-qualifier-seq [opt]
10783        exception-specification [opt]
10784      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10785      ( abstract-declarator )
10786
10787    Returns a representation of the declarator.  DCL_KIND is
10788    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10789    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10790    we are parsing a direct-declarator.  It is
10791    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10792    of ambiguity we prefer an abstract declarator, as per
10793    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10794    cp_parser_declarator.  */
10795
10796 static cp_declarator *
10797 cp_parser_direct_declarator (cp_parser* parser,
10798                              cp_parser_declarator_kind dcl_kind,
10799                              int* ctor_dtor_or_conv_p)
10800 {
10801   cp_token *token;
10802   cp_declarator *declarator = NULL;
10803   tree scope = NULL_TREE;
10804   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10805   bool saved_in_declarator_p = parser->in_declarator_p;
10806   bool first = true;
10807   bool pop_p = false;
10808
10809   while (true)
10810     {
10811       /* Peek at the next token.  */
10812       token = cp_lexer_peek_token (parser->lexer);
10813       if (token->type == CPP_OPEN_PAREN)
10814         {
10815           /* This is either a parameter-declaration-clause, or a
10816              parenthesized declarator. When we know we are parsing a
10817              named declarator, it must be a parenthesized declarator
10818              if FIRST is true. For instance, `(int)' is a
10819              parameter-declaration-clause, with an omitted
10820              direct-abstract-declarator. But `((*))', is a
10821              parenthesized abstract declarator. Finally, when T is a
10822              template parameter `(T)' is a
10823              parameter-declaration-clause, and not a parenthesized
10824              named declarator.
10825
10826              We first try and parse a parameter-declaration-clause,
10827              and then try a nested declarator (if FIRST is true).
10828
10829              It is not an error for it not to be a
10830              parameter-declaration-clause, even when FIRST is
10831              false. Consider,
10832
10833                int i (int);
10834                int i (3);
10835
10836              The first is the declaration of a function while the
10837              second is a the definition of a variable, including its
10838              initializer.
10839
10840              Having seen only the parenthesis, we cannot know which of
10841              these two alternatives should be selected.  Even more
10842              complex are examples like:
10843
10844                int i (int (a));
10845                int i (int (3));
10846
10847              The former is a function-declaration; the latter is a
10848              variable initialization.
10849
10850              Thus again, we try a parameter-declaration-clause, and if
10851              that fails, we back out and return.  */
10852
10853           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10854             {
10855               cp_parameter_declarator *params;
10856               unsigned saved_num_template_parameter_lists;
10857
10858               cp_parser_parse_tentatively (parser);
10859
10860               /* Consume the `('.  */
10861               cp_lexer_consume_token (parser->lexer);
10862               if (first)
10863                 {
10864                   /* If this is going to be an abstract declarator, we're
10865                      in a declarator and we can't have default args.  */
10866                   parser->default_arg_ok_p = false;
10867                   parser->in_declarator_p = true;
10868                 }
10869
10870               /* Inside the function parameter list, surrounding
10871                  template-parameter-lists do not apply.  */
10872               saved_num_template_parameter_lists
10873                 = parser->num_template_parameter_lists;
10874               parser->num_template_parameter_lists = 0;
10875
10876               /* Parse the parameter-declaration-clause.  */
10877               params = cp_parser_parameter_declaration_clause (parser);
10878
10879               parser->num_template_parameter_lists
10880                 = saved_num_template_parameter_lists;
10881
10882               /* If all went well, parse the cv-qualifier-seq and the
10883                  exception-specification.  */
10884               if (cp_parser_parse_definitely (parser))
10885                 {
10886                   cp_cv_quals cv_quals;
10887                   tree exception_specification;
10888
10889                   if (ctor_dtor_or_conv_p)
10890                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10891                   first = false;
10892                   /* Consume the `)'.  */
10893                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10894
10895                   /* Parse the cv-qualifier-seq.  */
10896                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10897                   /* And the exception-specification.  */
10898                   exception_specification
10899                     = cp_parser_exception_specification_opt (parser);
10900
10901                   /* Create the function-declarator.  */
10902                   declarator = make_call_declarator (declarator,
10903                                                      params,
10904                                                      cv_quals,
10905                                                      exception_specification);
10906                   /* Any subsequent parameter lists are to do with
10907                      return type, so are not those of the declared
10908                      function.  */
10909                   parser->default_arg_ok_p = false;
10910
10911                   /* Repeat the main loop.  */
10912                   continue;
10913                 }
10914             }
10915
10916           /* If this is the first, we can try a parenthesized
10917              declarator.  */
10918           if (first)
10919             {
10920               bool saved_in_type_id_in_expr_p;
10921
10922               parser->default_arg_ok_p = saved_default_arg_ok_p;
10923               parser->in_declarator_p = saved_in_declarator_p;
10924
10925               /* Consume the `('.  */
10926               cp_lexer_consume_token (parser->lexer);
10927               /* Parse the nested declarator.  */
10928               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10929               parser->in_type_id_in_expr_p = true;
10930               declarator
10931                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10932                                         /*parenthesized_p=*/NULL);
10933               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10934               first = false;
10935               /* Expect a `)'.  */
10936               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10937                 declarator = cp_error_declarator;
10938               if (declarator == cp_error_declarator)
10939                 break;
10940
10941               goto handle_declarator;
10942             }
10943           /* Otherwise, we must be done.  */
10944           else
10945             break;
10946         }
10947       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10948                && token->type == CPP_OPEN_SQUARE)
10949         {
10950           /* Parse an array-declarator.  */
10951           tree bounds;
10952
10953           if (ctor_dtor_or_conv_p)
10954             *ctor_dtor_or_conv_p = 0;
10955
10956           first = false;
10957           parser->default_arg_ok_p = false;
10958           parser->in_declarator_p = true;
10959           /* Consume the `['.  */
10960           cp_lexer_consume_token (parser->lexer);
10961           /* Peek at the next token.  */
10962           token = cp_lexer_peek_token (parser->lexer);
10963           /* If the next token is `]', then there is no
10964              constant-expression.  */
10965           if (token->type != CPP_CLOSE_SQUARE)
10966             {
10967               bool non_constant_p;
10968
10969               bounds
10970                 = cp_parser_constant_expression (parser,
10971                                                  /*allow_non_constant=*/true,
10972                                                  &non_constant_p);
10973               if (!non_constant_p)
10974                 bounds = fold_non_dependent_expr (bounds);
10975             }
10976           else
10977             bounds = NULL_TREE;
10978           /* Look for the closing `]'.  */
10979           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10980             {
10981               declarator = cp_error_declarator;
10982               break;
10983             }
10984
10985           declarator = make_array_declarator (declarator, bounds);
10986         }
10987       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10988         {
10989           tree id;
10990
10991           /* Parse a declarator-id */
10992           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10993             cp_parser_parse_tentatively (parser);
10994           id = cp_parser_declarator_id (parser);
10995           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10996             {
10997               if (!cp_parser_parse_definitely (parser))
10998                 id = error_mark_node;
10999               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11000                 {
11001                   cp_parser_error (parser, "expected unqualified-id");
11002                   id = error_mark_node;
11003                 }
11004             }
11005
11006           if (id == error_mark_node)
11007             {
11008               declarator = cp_error_declarator;
11009               break;
11010             }
11011
11012           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11013             {
11014               tree scope = TREE_OPERAND (id, 0);
11015
11016               /* In the declaration of a member of a template class
11017                  outside of the class itself, the SCOPE will sometimes
11018                  be a TYPENAME_TYPE.  For example, given:
11019
11020                  template <typename T>
11021                  int S<T>::R::i = 3;
11022
11023                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11024                  this context, we must resolve S<T>::R to an ordinary
11025                  type, rather than a typename type.
11026
11027                  The reason we normally avoid resolving TYPENAME_TYPEs
11028                  is that a specialization of `S' might render
11029                  `S<T>::R' not a type.  However, if `S' is
11030                  specialized, then this `i' will not be used, so there
11031                  is no harm in resolving the types here.  */
11032               if (TREE_CODE (scope) == TYPENAME_TYPE)
11033                 {
11034                   tree type;
11035
11036                   /* Resolve the TYPENAME_TYPE.  */
11037                   type = resolve_typename_type (scope,
11038                                                  /*only_current_p=*/false);
11039                   /* If that failed, the declarator is invalid.  */
11040                   if (type == error_mark_node)
11041                     error ("`%T::%D' is not a type",
11042                            TYPE_CONTEXT (scope),
11043                            TYPE_IDENTIFIER (scope));
11044                   /* Build a new DECLARATOR.  */
11045                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11046                 }
11047             }
11048
11049           declarator = make_id_declarator (id);
11050           if (id)
11051             {
11052               tree class_type;
11053               tree unqualified_name;
11054
11055               if (TREE_CODE (id) == SCOPE_REF
11056                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11057                 {
11058                   class_type = TREE_OPERAND (id, 0);
11059                   unqualified_name = TREE_OPERAND (id, 1);
11060                 }
11061               else
11062                 {
11063                   class_type = current_class_type;
11064                   unqualified_name = id;
11065                 }
11066
11067               if (class_type)
11068                 {
11069                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11070                     declarator->u.id.sfk = sfk_destructor;
11071                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11072                     declarator->u.id.sfk = sfk_conversion;
11073                   else if (constructor_name_p (unqualified_name,
11074                                                class_type)
11075                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11076                                && same_type_p (TREE_TYPE (unqualified_name),
11077                                                class_type)))
11078                     declarator->u.id.sfk = sfk_constructor;
11079
11080                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11081                     *ctor_dtor_or_conv_p = -1;
11082                   if (TREE_CODE (id) == SCOPE_REF
11083                       && TREE_CODE (unqualified_name) == TYPE_DECL
11084                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11085                     {
11086                       error ("invalid use of constructor as a template");
11087                       inform ("use `%T::%D' instead of `%T::%T' to name the "
11088                               "constructor in a qualified name", class_type,
11089                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11090                               class_type, class_type);
11091                     }
11092                 }
11093             }
11094
11095         handle_declarator:;
11096           scope = get_scope_of_declarator (declarator);
11097           if (scope)
11098             /* Any names that appear after the declarator-id for a
11099                member are looked up in the containing scope.  */
11100             pop_p = push_scope (scope);
11101           parser->in_declarator_p = true;
11102           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11103               || (declarator && declarator->kind == cdk_id))
11104             /* Default args are only allowed on function
11105                declarations.  */
11106             parser->default_arg_ok_p = saved_default_arg_ok_p;
11107           else
11108             parser->default_arg_ok_p = false;
11109
11110           first = false;
11111         }
11112       /* We're done.  */
11113       else
11114         break;
11115     }
11116
11117   /* For an abstract declarator, we might wind up with nothing at this
11118      point.  That's an error; the declarator is not optional.  */
11119   if (!declarator)
11120     cp_parser_error (parser, "expected declarator");
11121
11122   /* If we entered a scope, we must exit it now.  */
11123   if (pop_p)
11124     pop_scope (scope);
11125
11126   parser->default_arg_ok_p = saved_default_arg_ok_p;
11127   parser->in_declarator_p = saved_in_declarator_p;
11128
11129   return declarator;
11130 }
11131
11132 /* Parse a ptr-operator.
11133
11134    ptr-operator:
11135      * cv-qualifier-seq [opt]
11136      &
11137      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11138
11139    GNU Extension:
11140
11141    ptr-operator:
11142      & cv-qualifier-seq [opt]
11143
11144    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11145    Returns ADDR_EXPR if a reference was used.  In the case of a
11146    pointer-to-member, *TYPE is filled in with the TYPE containing the
11147    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11148    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11149    ERROR_MARK if an error occurred.  */
11150
11151 static enum tree_code
11152 cp_parser_ptr_operator (cp_parser* parser,
11153                         tree* type,
11154                         cp_cv_quals *cv_quals)
11155 {
11156   enum tree_code code = ERROR_MARK;
11157   cp_token *token;
11158
11159   /* Assume that it's not a pointer-to-member.  */
11160   *type = NULL_TREE;
11161   /* And that there are no cv-qualifiers.  */
11162   *cv_quals = TYPE_UNQUALIFIED;
11163
11164   /* Peek at the next token.  */
11165   token = cp_lexer_peek_token (parser->lexer);
11166   /* If it's a `*' or `&' we have a pointer or reference.  */
11167   if (token->type == CPP_MULT || token->type == CPP_AND)
11168     {
11169       /* Remember which ptr-operator we were processing.  */
11170       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11171
11172       /* Consume the `*' or `&'.  */
11173       cp_lexer_consume_token (parser->lexer);
11174
11175       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11176          `&', if we are allowing GNU extensions.  (The only qualifier
11177          that can legally appear after `&' is `restrict', but that is
11178          enforced during semantic analysis.  */
11179       if (code == INDIRECT_REF
11180           || cp_parser_allow_gnu_extensions_p (parser))
11181         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11182     }
11183   else
11184     {
11185       /* Try the pointer-to-member case.  */
11186       cp_parser_parse_tentatively (parser);
11187       /* Look for the optional `::' operator.  */
11188       cp_parser_global_scope_opt (parser,
11189                                   /*current_scope_valid_p=*/false);
11190       /* Look for the nested-name specifier.  */
11191       cp_parser_nested_name_specifier (parser,
11192                                        /*typename_keyword_p=*/false,
11193                                        /*check_dependency_p=*/true,
11194                                        /*type_p=*/false,
11195                                        /*is_declaration=*/false);
11196       /* If we found it, and the next token is a `*', then we are
11197          indeed looking at a pointer-to-member operator.  */
11198       if (!cp_parser_error_occurred (parser)
11199           && cp_parser_require (parser, CPP_MULT, "`*'"))
11200         {
11201           /* The type of which the member is a member is given by the
11202              current SCOPE.  */
11203           *type = parser->scope;
11204           /* The next name will not be qualified.  */
11205           parser->scope = NULL_TREE;
11206           parser->qualifying_scope = NULL_TREE;
11207           parser->object_scope = NULL_TREE;
11208           /* Indicate that the `*' operator was used.  */
11209           code = INDIRECT_REF;
11210           /* Look for the optional cv-qualifier-seq.  */
11211           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11212         }
11213       /* If that didn't work we don't have a ptr-operator.  */
11214       if (!cp_parser_parse_definitely (parser))
11215         cp_parser_error (parser, "expected ptr-operator");
11216     }
11217
11218   return code;
11219 }
11220
11221 /* Parse an (optional) cv-qualifier-seq.
11222
11223    cv-qualifier-seq:
11224      cv-qualifier cv-qualifier-seq [opt]
11225
11226    cv-qualifier:
11227      const
11228      volatile
11229
11230    GNU Extension:
11231
11232    cv-qualifier:
11233      __restrict__
11234
11235    Returns a bitmask representing the cv-qualifiers.  */
11236
11237 static cp_cv_quals
11238 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11239 {
11240   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11241
11242   while (true)
11243     {
11244       cp_token *token;
11245       cp_cv_quals cv_qualifier;
11246
11247       /* Peek at the next token.  */
11248       token = cp_lexer_peek_token (parser->lexer);
11249       /* See if it's a cv-qualifier.  */
11250       switch (token->keyword)
11251         {
11252         case RID_CONST:
11253           cv_qualifier = TYPE_QUAL_CONST;
11254           break;
11255
11256         case RID_VOLATILE:
11257           cv_qualifier = TYPE_QUAL_VOLATILE;
11258           break;
11259
11260         case RID_RESTRICT:
11261           cv_qualifier = TYPE_QUAL_RESTRICT;
11262           break;
11263
11264         default:
11265           cv_qualifier = TYPE_UNQUALIFIED;
11266           break;
11267         }
11268
11269       if (!cv_qualifier)
11270         break;
11271
11272       if (cv_quals & cv_qualifier)
11273         {
11274           error ("duplicate cv-qualifier");
11275           cp_lexer_purge_token (parser->lexer);
11276         }
11277       else
11278         {
11279           cp_lexer_consume_token (parser->lexer);
11280           cv_quals |= cv_qualifier;
11281         }
11282     }
11283
11284   return cv_quals;
11285 }
11286
11287 /* Parse a declarator-id.
11288
11289    declarator-id:
11290      id-expression
11291      :: [opt] nested-name-specifier [opt] type-name
11292
11293    In the `id-expression' case, the value returned is as for
11294    cp_parser_id_expression if the id-expression was an unqualified-id.
11295    If the id-expression was a qualified-id, then a SCOPE_REF is
11296    returned.  The first operand is the scope (either a NAMESPACE_DECL
11297    or TREE_TYPE), but the second is still just a representation of an
11298    unqualified-id.  */
11299
11300 static tree
11301 cp_parser_declarator_id (cp_parser* parser)
11302 {
11303   tree id_expression;
11304
11305   /* The expression must be an id-expression.  Assume that qualified
11306      names are the names of types so that:
11307
11308        template <class T>
11309        int S<T>::R::i = 3;
11310
11311      will work; we must treat `S<T>::R' as the name of a type.
11312      Similarly, assume that qualified names are templates, where
11313      required, so that:
11314
11315        template <class T>
11316        int S<T>::R<T>::i = 3;
11317
11318      will work, too.  */
11319   id_expression = cp_parser_id_expression (parser,
11320                                            /*template_keyword_p=*/false,
11321                                            /*check_dependency_p=*/false,
11322                                            /*template_p=*/NULL,
11323                                            /*declarator_p=*/true);
11324   /* If the name was qualified, create a SCOPE_REF to represent
11325      that.  */
11326   if (parser->scope)
11327     {
11328       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11329       parser->scope = NULL_TREE;
11330     }
11331
11332   return id_expression;
11333 }
11334
11335 /* Parse a type-id.
11336
11337    type-id:
11338      type-specifier-seq abstract-declarator [opt]
11339
11340    Returns the TYPE specified.  */
11341
11342 static tree
11343 cp_parser_type_id (cp_parser* parser)
11344 {
11345   cp_decl_specifier_seq type_specifier_seq;
11346   cp_declarator *abstract_declarator;
11347
11348   /* Parse the type-specifier-seq.  */
11349   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11350   if (type_specifier_seq.type == error_mark_node)
11351     return error_mark_node;
11352
11353   /* There might or might not be an abstract declarator.  */
11354   cp_parser_parse_tentatively (parser);
11355   /* Look for the declarator.  */
11356   abstract_declarator
11357     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11358                             /*parenthesized_p=*/NULL);
11359   /* Check to see if there really was a declarator.  */
11360   if (!cp_parser_parse_definitely (parser))
11361     abstract_declarator = NULL;
11362
11363   return groktypename (&type_specifier_seq, abstract_declarator);
11364 }
11365
11366 /* Parse a type-specifier-seq.
11367
11368    type-specifier-seq:
11369      type-specifier type-specifier-seq [opt]
11370
11371    GNU extension:
11372
11373    type-specifier-seq:
11374      attributes type-specifier-seq [opt]
11375
11376    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11377
11378 static void
11379 cp_parser_type_specifier_seq (cp_parser* parser,
11380                               cp_decl_specifier_seq *type_specifier_seq)
11381 {
11382   bool seen_type_specifier = false;
11383
11384   /* Clear the TYPE_SPECIFIER_SEQ.  */
11385   clear_decl_specs (type_specifier_seq);
11386
11387   /* Parse the type-specifiers and attributes.  */
11388   while (true)
11389     {
11390       tree type_specifier;
11391
11392       /* Check for attributes first.  */
11393       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11394         {
11395           type_specifier_seq->attributes =
11396             chainon (type_specifier_seq->attributes,
11397                      cp_parser_attributes_opt (parser));
11398           continue;
11399         }
11400
11401       /* Look for the type-specifier.  */
11402       type_specifier = cp_parser_type_specifier (parser,
11403                                                  CP_PARSER_FLAGS_OPTIONAL,
11404                                                  type_specifier_seq,
11405                                                  /*is_declaration=*/false,
11406                                                  NULL,
11407                                                  NULL);
11408       /* If the first type-specifier could not be found, this is not a
11409          type-specifier-seq at all.  */
11410       if (!seen_type_specifier && !type_specifier)
11411         {
11412           cp_parser_error (parser, "expected type-specifier");
11413           type_specifier_seq->type = error_mark_node;
11414           return;
11415         }
11416       /* If subsequent type-specifiers could not be found, the
11417          type-specifier-seq is complete.  */
11418       else if (seen_type_specifier && !type_specifier)
11419         break;
11420
11421       seen_type_specifier = true;
11422     }
11423
11424   return;
11425 }
11426
11427 /* Parse a parameter-declaration-clause.
11428
11429    parameter-declaration-clause:
11430      parameter-declaration-list [opt] ... [opt]
11431      parameter-declaration-list , ...
11432
11433    Returns a representation for the parameter declarations.  A return
11434    value of NULL indicates a parameter-declaration-clause consisting
11435    only of an ellipsis.  */
11436
11437 static cp_parameter_declarator *
11438 cp_parser_parameter_declaration_clause (cp_parser* parser)
11439 {
11440   cp_parameter_declarator *parameters;
11441   cp_token *token;
11442   bool ellipsis_p;
11443   bool is_error;
11444
11445   /* Peek at the next token.  */
11446   token = cp_lexer_peek_token (parser->lexer);
11447   /* Check for trivial parameter-declaration-clauses.  */
11448   if (token->type == CPP_ELLIPSIS)
11449     {
11450       /* Consume the `...' token.  */
11451       cp_lexer_consume_token (parser->lexer);
11452       return NULL;
11453     }
11454   else if (token->type == CPP_CLOSE_PAREN)
11455     /* There are no parameters.  */
11456     {
11457 #ifndef NO_IMPLICIT_EXTERN_C
11458       if (in_system_header && current_class_type == NULL
11459           && current_lang_name == lang_name_c)
11460         return NULL;
11461       else
11462 #endif
11463         return no_parameters;
11464     }
11465   /* Check for `(void)', too, which is a special case.  */
11466   else if (token->keyword == RID_VOID
11467            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11468                == CPP_CLOSE_PAREN))
11469     {
11470       /* Consume the `void' token.  */
11471       cp_lexer_consume_token (parser->lexer);
11472       /* There are no parameters.  */
11473       return no_parameters;
11474     }
11475
11476   /* Parse the parameter-declaration-list.  */
11477   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11478   /* If a parse error occurred while parsing the
11479      parameter-declaration-list, then the entire
11480      parameter-declaration-clause is erroneous.  */
11481   if (is_error)
11482     return NULL;
11483
11484   /* Peek at the next token.  */
11485   token = cp_lexer_peek_token (parser->lexer);
11486   /* If it's a `,', the clause should terminate with an ellipsis.  */
11487   if (token->type == CPP_COMMA)
11488     {
11489       /* Consume the `,'.  */
11490       cp_lexer_consume_token (parser->lexer);
11491       /* Expect an ellipsis.  */
11492       ellipsis_p
11493         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11494     }
11495   /* It might also be `...' if the optional trailing `,' was
11496      omitted.  */
11497   else if (token->type == CPP_ELLIPSIS)
11498     {
11499       /* Consume the `...' token.  */
11500       cp_lexer_consume_token (parser->lexer);
11501       /* And remember that we saw it.  */
11502       ellipsis_p = true;
11503     }
11504   else
11505     ellipsis_p = false;
11506
11507   /* Finish the parameter list.  */
11508   if (parameters && ellipsis_p)
11509     parameters->ellipsis_p = true;
11510
11511   return parameters;
11512 }
11513
11514 /* Parse a parameter-declaration-list.
11515
11516    parameter-declaration-list:
11517      parameter-declaration
11518      parameter-declaration-list , parameter-declaration
11519
11520    Returns a representation of the parameter-declaration-list, as for
11521    cp_parser_parameter_declaration_clause.  However, the
11522    `void_list_node' is never appended to the list.  Upon return,
11523    *IS_ERROR will be true iff an error occurred.  */
11524
11525 static cp_parameter_declarator *
11526 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11527 {
11528   cp_parameter_declarator *parameters = NULL;
11529   cp_parameter_declarator **tail = &parameters;
11530
11531   /* Assume all will go well.  */
11532   *is_error = false;
11533
11534   /* Look for more parameters.  */
11535   while (true)
11536     {
11537       cp_parameter_declarator *parameter;
11538       bool parenthesized_p;
11539       /* Parse the parameter.  */
11540       parameter
11541         = cp_parser_parameter_declaration (parser,
11542                                            /*template_parm_p=*/false,
11543                                            &parenthesized_p);
11544
11545       /* If a parse error occurred parsing the parameter declaration,
11546          then the entire parameter-declaration-list is erroneous.  */
11547       if (!parameter)
11548         {
11549           *is_error = true;
11550           parameters = NULL;
11551           break;
11552         }
11553       /* Add the new parameter to the list.  */
11554       *tail = parameter;
11555       tail = &parameter->next;
11556
11557       /* Peek at the next token.  */
11558       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11559           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11560         /* The parameter-declaration-list is complete.  */
11561         break;
11562       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11563         {
11564           cp_token *token;
11565
11566           /* Peek at the next token.  */
11567           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11568           /* If it's an ellipsis, then the list is complete.  */
11569           if (token->type == CPP_ELLIPSIS)
11570             break;
11571           /* Otherwise, there must be more parameters.  Consume the
11572              `,'.  */
11573           cp_lexer_consume_token (parser->lexer);
11574           /* When parsing something like:
11575
11576                 int i(float f, double d)
11577
11578              we can tell after seeing the declaration for "f" that we
11579              are not looking at an initialization of a variable "i",
11580              but rather at the declaration of a function "i".
11581
11582              Due to the fact that the parsing of template arguments
11583              (as specified to a template-id) requires backtracking we
11584              cannot use this technique when inside a template argument
11585              list.  */
11586           if (!parser->in_template_argument_list_p
11587               && !parser->in_type_id_in_expr_p
11588               && cp_parser_parsing_tentatively (parser)
11589               && !cp_parser_committed_to_tentative_parse (parser)
11590               /* However, a parameter-declaration of the form
11591                  "foat(f)" (which is a valid declaration of a
11592                  parameter "f") can also be interpreted as an
11593                  expression (the conversion of "f" to "float").  */
11594               && !parenthesized_p)
11595             cp_parser_commit_to_tentative_parse (parser);
11596         }
11597       else
11598         {
11599           cp_parser_error (parser, "expected `,' or `...'");
11600           if (!cp_parser_parsing_tentatively (parser)
11601               || cp_parser_committed_to_tentative_parse (parser))
11602             cp_parser_skip_to_closing_parenthesis (parser,
11603                                                    /*recovering=*/true,
11604                                                    /*or_comma=*/false,
11605                                                    /*consume_paren=*/false);
11606           break;
11607         }
11608     }
11609
11610   return parameters;
11611 }
11612
11613 /* Parse a parameter declaration.
11614
11615    parameter-declaration:
11616      decl-specifier-seq declarator
11617      decl-specifier-seq declarator = assignment-expression
11618      decl-specifier-seq abstract-declarator [opt]
11619      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11620
11621    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11622    declares a template parameter.  (In that case, a non-nested `>'
11623    token encountered during the parsing of the assignment-expression
11624    is not interpreted as a greater-than operator.)
11625
11626    Returns a representation of the parameter, or NULL if an error
11627    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11628    true iff the declarator is of the form "(p)".  */
11629
11630 static cp_parameter_declarator *
11631 cp_parser_parameter_declaration (cp_parser *parser,
11632                                  bool template_parm_p,
11633                                  bool *parenthesized_p)
11634 {
11635   int declares_class_or_enum;
11636   bool greater_than_is_operator_p;
11637   cp_decl_specifier_seq decl_specifiers;
11638   cp_declarator *declarator;
11639   tree default_argument;
11640   cp_token *token;
11641   const char *saved_message;
11642
11643   /* In a template parameter, `>' is not an operator.
11644
11645      [temp.param]
11646
11647      When parsing a default template-argument for a non-type
11648      template-parameter, the first non-nested `>' is taken as the end
11649      of the template parameter-list rather than a greater-than
11650      operator.  */
11651   greater_than_is_operator_p = !template_parm_p;
11652
11653   /* Type definitions may not appear in parameter types.  */
11654   saved_message = parser->type_definition_forbidden_message;
11655   parser->type_definition_forbidden_message
11656     = "types may not be defined in parameter types";
11657
11658   /* Parse the declaration-specifiers.  */
11659   cp_parser_decl_specifier_seq (parser,
11660                                 CP_PARSER_FLAGS_NONE,
11661                                 &decl_specifiers,
11662                                 &declares_class_or_enum);
11663   /* If an error occurred, there's no reason to attempt to parse the
11664      rest of the declaration.  */
11665   if (cp_parser_error_occurred (parser))
11666     {
11667       parser->type_definition_forbidden_message = saved_message;
11668       return NULL;
11669     }
11670
11671   /* Peek at the next token.  */
11672   token = cp_lexer_peek_token (parser->lexer);
11673   /* If the next token is a `)', `,', `=', `>', or `...', then there
11674      is no declarator.  */
11675   if (token->type == CPP_CLOSE_PAREN
11676       || token->type == CPP_COMMA
11677       || token->type == CPP_EQ
11678       || token->type == CPP_ELLIPSIS
11679       || token->type == CPP_GREATER)
11680     {
11681       declarator = NULL;
11682       if (parenthesized_p)
11683         *parenthesized_p = false;
11684     }
11685   /* Otherwise, there should be a declarator.  */
11686   else
11687     {
11688       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11689       parser->default_arg_ok_p = false;
11690
11691       /* After seeing a decl-specifier-seq, if the next token is not a
11692          "(", there is no possibility that the code is a valid
11693          expression.  Therefore, if parsing tentatively, we commit at
11694          this point.  */
11695       if (!parser->in_template_argument_list_p
11696           /* In an expression context, having seen:
11697
11698                (int((char ...
11699
11700              we cannot be sure whether we are looking at a
11701              function-type (taking a "char" as a parameter) or a cast
11702              of some object of type "char" to "int".  */
11703           && !parser->in_type_id_in_expr_p
11704           && cp_parser_parsing_tentatively (parser)
11705           && !cp_parser_committed_to_tentative_parse (parser)
11706           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11707         cp_parser_commit_to_tentative_parse (parser);
11708       /* Parse the declarator.  */
11709       declarator = cp_parser_declarator (parser,
11710                                          CP_PARSER_DECLARATOR_EITHER,
11711                                          /*ctor_dtor_or_conv_p=*/NULL,
11712                                          parenthesized_p);
11713       parser->default_arg_ok_p = saved_default_arg_ok_p;
11714       /* After the declarator, allow more attributes.  */
11715       decl_specifiers.attributes
11716         = chainon (decl_specifiers.attributes,
11717                    cp_parser_attributes_opt (parser));
11718     }
11719
11720   /* The restriction on defining new types applies only to the type
11721      of the parameter, not to the default argument.  */
11722   parser->type_definition_forbidden_message = saved_message;
11723
11724   /* If the next token is `=', then process a default argument.  */
11725   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11726     {
11727       bool saved_greater_than_is_operator_p;
11728       /* Consume the `='.  */
11729       cp_lexer_consume_token (parser->lexer);
11730
11731       /* If we are defining a class, then the tokens that make up the
11732          default argument must be saved and processed later.  */
11733       if (!template_parm_p && at_class_scope_p ()
11734           && TYPE_BEING_DEFINED (current_class_type))
11735         {
11736           unsigned depth = 0;
11737           cp_token *first_token;
11738           cp_token *token;
11739
11740           /* Add tokens until we have processed the entire default
11741              argument.  We add the range [first_token, token). */
11742           first_token = cp_lexer_peek_token (parser->lexer);
11743           while (true)
11744             {
11745               bool done = false;
11746
11747               /* Peek at the next token.  */
11748               token = cp_lexer_peek_token (parser->lexer);
11749               /* What we do depends on what token we have.  */
11750               switch (token->type)
11751                 {
11752                   /* In valid code, a default argument must be
11753                      immediately followed by a `,' `)', or `...'.  */
11754                 case CPP_COMMA:
11755                 case CPP_CLOSE_PAREN:
11756                 case CPP_ELLIPSIS:
11757                   /* If we run into a non-nested `;', `}', or `]',
11758                      then the code is invalid -- but the default
11759                      argument is certainly over.  */
11760                 case CPP_SEMICOLON:
11761                 case CPP_CLOSE_BRACE:
11762                 case CPP_CLOSE_SQUARE:
11763                   if (depth == 0)
11764                     done = true;
11765                   /* Update DEPTH, if necessary.  */
11766                   else if (token->type == CPP_CLOSE_PAREN
11767                            || token->type == CPP_CLOSE_BRACE
11768                            || token->type == CPP_CLOSE_SQUARE)
11769                     --depth;
11770                   break;
11771
11772                 case CPP_OPEN_PAREN:
11773                 case CPP_OPEN_SQUARE:
11774                 case CPP_OPEN_BRACE:
11775                   ++depth;
11776                   break;
11777
11778                 case CPP_GREATER:
11779                   /* If we see a non-nested `>', and `>' is not an
11780                      operator, then it marks the end of the default
11781                      argument.  */
11782                   if (!depth && !greater_than_is_operator_p)
11783                     done = true;
11784                   break;
11785
11786                   /* If we run out of tokens, issue an error message.  */
11787                 case CPP_EOF:
11788                   error ("file ends in default argument");
11789                   done = true;
11790                   break;
11791
11792                 case CPP_NAME:
11793                 case CPP_SCOPE:
11794                   /* In these cases, we should look for template-ids.
11795                      For example, if the default argument is
11796                      `X<int, double>()', we need to do name lookup to
11797                      figure out whether or not `X' is a template; if
11798                      so, the `,' does not end the default argument.
11799
11800                      That is not yet done.  */
11801                   break;
11802
11803                 default:
11804                   break;
11805                 }
11806
11807               /* If we've reached the end, stop.  */
11808               if (done)
11809                 break;
11810
11811               /* Add the token to the token block.  */
11812               token = cp_lexer_consume_token (parser->lexer);
11813             }
11814
11815           /* Create a DEFAULT_ARG to represented the unparsed default
11816              argument.  */
11817           default_argument = make_node (DEFAULT_ARG);
11818           DEFARG_TOKENS (default_argument)
11819             = cp_token_cache_new (first_token, token);  
11820         }
11821       /* Outside of a class definition, we can just parse the
11822          assignment-expression.  */
11823       else
11824         {
11825           bool saved_local_variables_forbidden_p;
11826
11827           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11828              set correctly.  */
11829           saved_greater_than_is_operator_p
11830             = parser->greater_than_is_operator_p;
11831           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11832           /* Local variable names (and the `this' keyword) may not
11833              appear in a default argument.  */
11834           saved_local_variables_forbidden_p
11835             = parser->local_variables_forbidden_p;
11836           parser->local_variables_forbidden_p = true;
11837           /* Parse the assignment-expression.  */
11838           default_argument = cp_parser_assignment_expression (parser);
11839           /* Restore saved state.  */
11840           parser->greater_than_is_operator_p
11841             = saved_greater_than_is_operator_p;
11842           parser->local_variables_forbidden_p
11843             = saved_local_variables_forbidden_p;
11844         }
11845       if (!parser->default_arg_ok_p)
11846         {
11847           if (!flag_pedantic_errors)
11848             warning ("deprecated use of default argument for parameter of non-function");
11849           else
11850             {
11851               error ("default arguments are only permitted for function parameters");
11852               default_argument = NULL_TREE;
11853             }
11854         }
11855     }
11856   else
11857     default_argument = NULL_TREE;
11858
11859   return make_parameter_declarator (&decl_specifiers,
11860                                     declarator,
11861                                     default_argument);
11862 }
11863
11864 /* Parse a function-body.
11865
11866    function-body:
11867      compound_statement  */
11868
11869 static void
11870 cp_parser_function_body (cp_parser *parser)
11871 {
11872   cp_parser_compound_statement (parser, NULL, false);
11873 }
11874
11875 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11876    true if a ctor-initializer was present.  */
11877
11878 static bool
11879 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11880 {
11881   tree body;
11882   bool ctor_initializer_p;
11883
11884   /* Begin the function body.  */
11885   body = begin_function_body ();
11886   /* Parse the optional ctor-initializer.  */
11887   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11888   /* Parse the function-body.  */
11889   cp_parser_function_body (parser);
11890   /* Finish the function body.  */
11891   finish_function_body (body);
11892
11893   return ctor_initializer_p;
11894 }
11895
11896 /* Parse an initializer.
11897
11898    initializer:
11899      = initializer-clause
11900      ( expression-list )
11901
11902    Returns a expression representing the initializer.  If no
11903    initializer is present, NULL_TREE is returned.
11904
11905    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11906    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11907    set to FALSE if there is no initializer present.  If there is an
11908    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11909    is set to true; otherwise it is set to false.  */
11910
11911 static tree
11912 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11913                        bool* non_constant_p)
11914 {
11915   cp_token *token;
11916   tree init;
11917
11918   /* Peek at the next token.  */
11919   token = cp_lexer_peek_token (parser->lexer);
11920
11921   /* Let our caller know whether or not this initializer was
11922      parenthesized.  */
11923   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11924   /* Assume that the initializer is constant.  */
11925   *non_constant_p = false;
11926
11927   if (token->type == CPP_EQ)
11928     {
11929       /* Consume the `='.  */
11930       cp_lexer_consume_token (parser->lexer);
11931       /* Parse the initializer-clause.  */
11932       init = cp_parser_initializer_clause (parser, non_constant_p);
11933     }
11934   else if (token->type == CPP_OPEN_PAREN)
11935     init = cp_parser_parenthesized_expression_list (parser, false,
11936                                                     non_constant_p);
11937   else
11938     {
11939       /* Anything else is an error.  */
11940       cp_parser_error (parser, "expected initializer");
11941       init = error_mark_node;
11942     }
11943
11944   return init;
11945 }
11946
11947 /* Parse an initializer-clause.
11948
11949    initializer-clause:
11950      assignment-expression
11951      { initializer-list , [opt] }
11952      { }
11953
11954    Returns an expression representing the initializer.
11955
11956    If the `assignment-expression' production is used the value
11957    returned is simply a representation for the expression.
11958
11959    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11960    the elements of the initializer-list (or NULL_TREE, if the last
11961    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11962    NULL_TREE.  There is no way to detect whether or not the optional
11963    trailing `,' was provided.  NON_CONSTANT_P is as for
11964    cp_parser_initializer.  */
11965
11966 static tree
11967 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11968 {
11969   tree initializer;
11970
11971   /* If it is not a `{', then we are looking at an
11972      assignment-expression.  */
11973   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11974     {
11975       initializer
11976         = cp_parser_constant_expression (parser,
11977                                         /*allow_non_constant_p=*/true,
11978                                         non_constant_p);
11979       if (!*non_constant_p)
11980         initializer = fold_non_dependent_expr (initializer);
11981     }
11982   else
11983     {
11984       /* Consume the `{' token.  */
11985       cp_lexer_consume_token (parser->lexer);
11986       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11987       initializer = make_node (CONSTRUCTOR);
11988       /* If it's not a `}', then there is a non-trivial initializer.  */
11989       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11990         {
11991           /* Parse the initializer list.  */
11992           CONSTRUCTOR_ELTS (initializer)
11993             = cp_parser_initializer_list (parser, non_constant_p);
11994           /* A trailing `,' token is allowed.  */
11995           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11996             cp_lexer_consume_token (parser->lexer);
11997         }
11998       /* Now, there should be a trailing `}'.  */
11999       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12000     }
12001
12002   return initializer;
12003 }
12004
12005 /* Parse an initializer-list.
12006
12007    initializer-list:
12008      initializer-clause
12009      initializer-list , initializer-clause
12010
12011    GNU Extension:
12012
12013    initializer-list:
12014      identifier : initializer-clause
12015      initializer-list, identifier : initializer-clause
12016
12017    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12018    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12019    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12020    as for cp_parser_initializer.  */
12021
12022 static tree
12023 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12024 {
12025   tree initializers = NULL_TREE;
12026
12027   /* Assume all of the expressions are constant.  */
12028   *non_constant_p = false;
12029
12030   /* Parse the rest of the list.  */
12031   while (true)
12032     {
12033       cp_token *token;
12034       tree identifier;
12035       tree initializer;
12036       bool clause_non_constant_p;
12037
12038       /* If the next token is an identifier and the following one is a
12039          colon, we are looking at the GNU designated-initializer
12040          syntax.  */
12041       if (cp_parser_allow_gnu_extensions_p (parser)
12042           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12043           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12044         {
12045           /* Consume the identifier.  */
12046           identifier = cp_lexer_consume_token (parser->lexer)->value;
12047           /* Consume the `:'.  */
12048           cp_lexer_consume_token (parser->lexer);
12049         }
12050       else
12051         identifier = NULL_TREE;
12052
12053       /* Parse the initializer.  */
12054       initializer = cp_parser_initializer_clause (parser,
12055                                                   &clause_non_constant_p);
12056       /* If any clause is non-constant, so is the entire initializer.  */
12057       if (clause_non_constant_p)
12058         *non_constant_p = true;
12059       /* Add it to the list.  */
12060       initializers = tree_cons (identifier, initializer, initializers);
12061
12062       /* If the next token is not a comma, we have reached the end of
12063          the list.  */
12064       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12065         break;
12066
12067       /* Peek at the next token.  */
12068       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12069       /* If the next token is a `}', then we're still done.  An
12070          initializer-clause can have a trailing `,' after the
12071          initializer-list and before the closing `}'.  */
12072       if (token->type == CPP_CLOSE_BRACE)
12073         break;
12074
12075       /* Consume the `,' token.  */
12076       cp_lexer_consume_token (parser->lexer);
12077     }
12078
12079   /* The initializers were built up in reverse order, so we need to
12080      reverse them now.  */
12081   return nreverse (initializers);
12082 }
12083
12084 /* Classes [gram.class] */
12085
12086 /* Parse a class-name.
12087
12088    class-name:
12089      identifier
12090      template-id
12091
12092    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12093    to indicate that names looked up in dependent types should be
12094    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12095    keyword has been used to indicate that the name that appears next
12096    is a template.  TYPE_P is true iff the next name should be treated
12097    as class-name, even if it is declared to be some other kind of name
12098    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12099    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12100    being defined in a class-head.
12101
12102    Returns the TYPE_DECL representing the class.  */
12103
12104 static tree
12105 cp_parser_class_name (cp_parser *parser,
12106                       bool typename_keyword_p,
12107                       bool template_keyword_p,
12108                       bool type_p,
12109                       bool check_dependency_p,
12110                       bool class_head_p,
12111                       bool is_declaration)
12112 {
12113   tree decl;
12114   tree scope;
12115   bool typename_p;
12116   cp_token *token;
12117
12118   /* All class-names start with an identifier.  */
12119   token = cp_lexer_peek_token (parser->lexer);
12120   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12121     {
12122       cp_parser_error (parser, "expected class-name");
12123       return error_mark_node;
12124     }
12125
12126   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12127      to a template-id, so we save it here.  */
12128   scope = parser->scope;
12129   if (scope == error_mark_node)
12130     return error_mark_node;
12131
12132   /* Any name names a type if we're following the `typename' keyword
12133      in a qualified name where the enclosing scope is type-dependent.  */
12134   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12135                 && dependent_type_p (scope));
12136   /* Handle the common case (an identifier, but not a template-id)
12137      efficiently.  */
12138   if (token->type == CPP_NAME
12139       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12140     {
12141       tree identifier;
12142
12143       /* Look for the identifier.  */
12144       identifier = cp_parser_identifier (parser);
12145       /* If the next token isn't an identifier, we are certainly not
12146          looking at a class-name.  */
12147       if (identifier == error_mark_node)
12148         decl = error_mark_node;
12149       /* If we know this is a type-name, there's no need to look it
12150          up.  */
12151       else if (typename_p)
12152         decl = identifier;
12153       else
12154         {
12155           /* If the next token is a `::', then the name must be a type
12156              name.
12157
12158              [basic.lookup.qual]
12159
12160              During the lookup for a name preceding the :: scope
12161              resolution operator, object, function, and enumerator
12162              names are ignored.  */
12163           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12164             type_p = true;
12165           /* Look up the name.  */
12166           decl = cp_parser_lookup_name (parser, identifier,
12167                                         type_p,
12168                                         /*is_template=*/false,
12169                                         /*is_namespace=*/false,
12170                                         check_dependency_p,
12171                                         /*ambiguous_p=*/NULL);
12172         }
12173     }
12174   else
12175     {
12176       /* Try a template-id.  */
12177       decl = cp_parser_template_id (parser, template_keyword_p,
12178                                     check_dependency_p,
12179                                     is_declaration);
12180       if (decl == error_mark_node)
12181         return error_mark_node;
12182     }
12183
12184   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12185
12186   /* If this is a typename, create a TYPENAME_TYPE.  */
12187   if (typename_p && decl != error_mark_node)
12188     {
12189       decl = make_typename_type (scope, decl, /*complain=*/1);
12190       if (decl != error_mark_node)
12191         decl = TYPE_NAME (decl);
12192     }
12193
12194   /* Check to see that it is really the name of a class.  */
12195   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12196       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12197       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12198     /* Situations like this:
12199
12200          template <typename T> struct A {
12201            typename T::template X<int>::I i;
12202          };
12203
12204        are problematic.  Is `T::template X<int>' a class-name?  The
12205        standard does not seem to be definitive, but there is no other
12206        valid interpretation of the following `::'.  Therefore, those
12207        names are considered class-names.  */
12208     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12209   else if (decl == error_mark_node
12210            || TREE_CODE (decl) != TYPE_DECL
12211            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12212     {
12213       cp_parser_error (parser, "expected class-name");
12214       return error_mark_node;
12215     }
12216
12217   return decl;
12218 }
12219
12220 /* Parse a class-specifier.
12221
12222    class-specifier:
12223      class-head { member-specification [opt] }
12224
12225    Returns the TREE_TYPE representing the class.  */
12226
12227 static tree
12228 cp_parser_class_specifier (cp_parser* parser)
12229 {
12230   cp_token *token;
12231   tree type;
12232   tree attributes = NULL_TREE;
12233   int has_trailing_semicolon;
12234   bool nested_name_specifier_p;
12235   unsigned saved_num_template_parameter_lists;
12236   bool pop_p = false;
12237   tree scope = NULL_TREE;
12238
12239   push_deferring_access_checks (dk_no_deferred);
12240
12241   /* Parse the class-head.  */
12242   type = cp_parser_class_head (parser,
12243                                &nested_name_specifier_p,
12244                                &attributes);
12245   /* If the class-head was a semantic disaster, skip the entire body
12246      of the class.  */
12247   if (!type)
12248     {
12249       cp_parser_skip_to_end_of_block_or_statement (parser);
12250       pop_deferring_access_checks ();
12251       return error_mark_node;
12252     }
12253
12254   /* Look for the `{'.  */
12255   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12256     {
12257       pop_deferring_access_checks ();
12258       return error_mark_node;
12259     }
12260
12261   /* Issue an error message if type-definitions are forbidden here.  */
12262   cp_parser_check_type_definition (parser);
12263   /* Remember that we are defining one more class.  */
12264   ++parser->num_classes_being_defined;
12265   /* Inside the class, surrounding template-parameter-lists do not
12266      apply.  */
12267   saved_num_template_parameter_lists
12268     = parser->num_template_parameter_lists;
12269   parser->num_template_parameter_lists = 0;
12270
12271   /* Start the class.  */
12272   if (nested_name_specifier_p)
12273     {
12274       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12275       pop_p = push_scope (scope);
12276     }
12277   type = begin_class_definition (type);
12278
12279   if (type == error_mark_node)
12280     /* If the type is erroneous, skip the entire body of the class.  */
12281     cp_parser_skip_to_closing_brace (parser);
12282   else
12283     /* Parse the member-specification.  */
12284     cp_parser_member_specification_opt (parser);
12285
12286   /* Look for the trailing `}'.  */
12287   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12288   /* We get better error messages by noticing a common problem: a
12289      missing trailing `;'.  */
12290   token = cp_lexer_peek_token (parser->lexer);
12291   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12292   /* Look for trailing attributes to apply to this class.  */
12293   if (cp_parser_allow_gnu_extensions_p (parser))
12294     {
12295       tree sub_attr = cp_parser_attributes_opt (parser);
12296       attributes = chainon (attributes, sub_attr);
12297     }
12298   if (type != error_mark_node)
12299     type = finish_struct (type, attributes);
12300   if (pop_p)
12301     pop_scope (scope);
12302   /* If this class is not itself within the scope of another class,
12303      then we need to parse the bodies of all of the queued function
12304      definitions.  Note that the queued functions defined in a class
12305      are not always processed immediately following the
12306      class-specifier for that class.  Consider:
12307
12308        struct A {
12309          struct B { void f() { sizeof (A); } };
12310        };
12311
12312      If `f' were processed before the processing of `A' were
12313      completed, there would be no way to compute the size of `A'.
12314      Note that the nesting we are interested in here is lexical --
12315      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12316      for:
12317
12318        struct A { struct B; };
12319        struct A::B { void f() { } };
12320
12321      there is no need to delay the parsing of `A::B::f'.  */
12322   if (--parser->num_classes_being_defined == 0)
12323     {
12324       tree queue_entry;
12325       tree fn;
12326       tree class_type;
12327       bool pop_p;
12328
12329       /* In a first pass, parse default arguments to the functions.
12330          Then, in a second pass, parse the bodies of the functions.
12331          This two-phased approach handles cases like:
12332
12333             struct S {
12334               void f() { g(); }
12335               void g(int i = 3);
12336             };
12337
12338          */
12339       class_type = NULL_TREE;
12340       pop_p = false;
12341       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12342              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12343            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12344            TREE_PURPOSE (parser->unparsed_functions_queues)
12345              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12346         {
12347           fn = TREE_VALUE (queue_entry);
12348           /* If there are default arguments that have not yet been processed,
12349              take care of them now.  */
12350           if (class_type != TREE_PURPOSE (queue_entry))
12351             {
12352               if (pop_p)
12353                 pop_scope (class_type);
12354               class_type = TREE_PURPOSE (queue_entry);
12355               pop_p = push_scope (class_type);
12356             }
12357           /* Make sure that any template parameters are in scope.  */
12358           maybe_begin_member_template_processing (fn);
12359           /* Parse the default argument expressions.  */
12360           cp_parser_late_parsing_default_args (parser, fn);
12361           /* Remove any template parameters from the symbol table.  */
12362           maybe_end_member_template_processing ();
12363         }
12364       if (pop_p)
12365         pop_scope (class_type);
12366       /* Now parse the body of the functions.  */
12367       for (TREE_VALUE (parser->unparsed_functions_queues)
12368              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12369            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12370            TREE_VALUE (parser->unparsed_functions_queues)
12371              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12372         {
12373           /* Figure out which function we need to process.  */
12374           fn = TREE_VALUE (queue_entry);
12375
12376           /* A hack to prevent garbage collection.  */
12377           function_depth++;
12378
12379           /* Parse the function.  */
12380           cp_parser_late_parsing_for_member (parser, fn);
12381           function_depth--;
12382         }
12383     }
12384
12385   /* Put back any saved access checks.  */
12386   pop_deferring_access_checks ();
12387
12388   /* Restore the count of active template-parameter-lists.  */
12389   parser->num_template_parameter_lists
12390     = saved_num_template_parameter_lists;
12391
12392   return type;
12393 }
12394
12395 /* Parse a class-head.
12396
12397    class-head:
12398      class-key identifier [opt] base-clause [opt]
12399      class-key nested-name-specifier identifier base-clause [opt]
12400      class-key nested-name-specifier [opt] template-id
12401        base-clause [opt]
12402
12403    GNU Extensions:
12404      class-key attributes identifier [opt] base-clause [opt]
12405      class-key attributes nested-name-specifier identifier base-clause [opt]
12406      class-key attributes nested-name-specifier [opt] template-id
12407        base-clause [opt]
12408
12409    Returns the TYPE of the indicated class.  Sets
12410    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12411    involving a nested-name-specifier was used, and FALSE otherwise.
12412
12413    Returns NULL_TREE if the class-head is syntactically valid, but
12414    semantically invalid in a way that means we should skip the entire
12415    body of the class.  */
12416
12417 static tree
12418 cp_parser_class_head (cp_parser* parser,
12419                       bool* nested_name_specifier_p,
12420                       tree *attributes_p)
12421 {
12422   tree nested_name_specifier;
12423   enum tag_types class_key;
12424   tree id = NULL_TREE;
12425   tree type = NULL_TREE;
12426   tree attributes;
12427   bool template_id_p = false;
12428   bool qualified_p = false;
12429   bool invalid_nested_name_p = false;
12430   bool invalid_explicit_specialization_p = false;
12431   bool pop_p = false;
12432   unsigned num_templates;
12433   tree bases;
12434
12435   /* Assume no nested-name-specifier will be present.  */
12436   *nested_name_specifier_p = false;
12437   /* Assume no template parameter lists will be used in defining the
12438      type.  */
12439   num_templates = 0;
12440
12441   /* Look for the class-key.  */
12442   class_key = cp_parser_class_key (parser);
12443   if (class_key == none_type)
12444     return error_mark_node;
12445
12446   /* Parse the attributes.  */
12447   attributes = cp_parser_attributes_opt (parser);
12448
12449   /* If the next token is `::', that is invalid -- but sometimes
12450      people do try to write:
12451
12452        struct ::S {};
12453
12454      Handle this gracefully by accepting the extra qualifier, and then
12455      issuing an error about it later if this really is a
12456      class-head.  If it turns out just to be an elaborated type
12457      specifier, remain silent.  */
12458   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12459     qualified_p = true;
12460
12461   push_deferring_access_checks (dk_no_check);
12462
12463   /* Determine the name of the class.  Begin by looking for an
12464      optional nested-name-specifier.  */
12465   nested_name_specifier
12466     = cp_parser_nested_name_specifier_opt (parser,
12467                                            /*typename_keyword_p=*/false,
12468                                            /*check_dependency_p=*/false,
12469                                            /*type_p=*/false,
12470                                            /*is_declaration=*/false);
12471   /* If there was a nested-name-specifier, then there *must* be an
12472      identifier.  */
12473   if (nested_name_specifier)
12474     {
12475       /* Although the grammar says `identifier', it really means
12476          `class-name' or `template-name'.  You are only allowed to
12477          define a class that has already been declared with this
12478          syntax.
12479
12480          The proposed resolution for Core Issue 180 says that whever
12481          you see `class T::X' you should treat `X' as a type-name.
12482
12483          It is OK to define an inaccessible class; for example:
12484
12485            class A { class B; };
12486            class A::B {};
12487
12488          We do not know if we will see a class-name, or a
12489          template-name.  We look for a class-name first, in case the
12490          class-name is a template-id; if we looked for the
12491          template-name first we would stop after the template-name.  */
12492       cp_parser_parse_tentatively (parser);
12493       type = cp_parser_class_name (parser,
12494                                    /*typename_keyword_p=*/false,
12495                                    /*template_keyword_p=*/false,
12496                                    /*type_p=*/true,
12497                                    /*check_dependency_p=*/false,
12498                                    /*class_head_p=*/true,
12499                                    /*is_declaration=*/false);
12500       /* If that didn't work, ignore the nested-name-specifier.  */
12501       if (!cp_parser_parse_definitely (parser))
12502         {
12503           invalid_nested_name_p = true;
12504           id = cp_parser_identifier (parser);
12505           if (id == error_mark_node)
12506             id = NULL_TREE;
12507         }
12508       /* If we could not find a corresponding TYPE, treat this
12509          declaration like an unqualified declaration.  */
12510       if (type == error_mark_node)
12511         nested_name_specifier = NULL_TREE;
12512       /* Otherwise, count the number of templates used in TYPE and its
12513          containing scopes.  */
12514       else
12515         {
12516           tree scope;
12517
12518           for (scope = TREE_TYPE (type);
12519                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12520                scope = (TYPE_P (scope)
12521                         ? TYPE_CONTEXT (scope)
12522                         : DECL_CONTEXT (scope)))
12523             if (TYPE_P (scope)
12524                 && CLASS_TYPE_P (scope)
12525                 && CLASSTYPE_TEMPLATE_INFO (scope)
12526                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12527                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12528               ++num_templates;
12529         }
12530     }
12531   /* Otherwise, the identifier is optional.  */
12532   else
12533     {
12534       /* We don't know whether what comes next is a template-id,
12535          an identifier, or nothing at all.  */
12536       cp_parser_parse_tentatively (parser);
12537       /* Check for a template-id.  */
12538       id = cp_parser_template_id (parser,
12539                                   /*template_keyword_p=*/false,
12540                                   /*check_dependency_p=*/true,
12541                                   /*is_declaration=*/true);
12542       /* If that didn't work, it could still be an identifier.  */
12543       if (!cp_parser_parse_definitely (parser))
12544         {
12545           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12546             id = cp_parser_identifier (parser);
12547           else
12548             id = NULL_TREE;
12549         }
12550       else
12551         {
12552           template_id_p = true;
12553           ++num_templates;
12554         }
12555     }
12556
12557   pop_deferring_access_checks ();
12558
12559   if (id)
12560     cp_parser_check_for_invalid_template_id (parser, id);
12561
12562   /* If it's not a `:' or a `{' then we can't really be looking at a
12563      class-head, since a class-head only appears as part of a
12564      class-specifier.  We have to detect this situation before calling
12565      xref_tag, since that has irreversible side-effects.  */
12566   if (!cp_parser_next_token_starts_class_definition_p (parser))
12567     {
12568       cp_parser_error (parser, "expected `{' or `:'");
12569       return error_mark_node;
12570     }
12571
12572   /* At this point, we're going ahead with the class-specifier, even
12573      if some other problem occurs.  */
12574   cp_parser_commit_to_tentative_parse (parser);
12575   /* Issue the error about the overly-qualified name now.  */
12576   if (qualified_p)
12577     cp_parser_error (parser,
12578                      "global qualification of class name is invalid");
12579   else if (invalid_nested_name_p)
12580     cp_parser_error (parser,
12581                      "qualified name does not name a class");
12582   else if (nested_name_specifier)
12583     {
12584       tree scope;
12585       /* Figure out in what scope the declaration is being placed.  */
12586       scope = current_scope ();
12587       if (!scope)
12588         scope = current_namespace;
12589       /* If that scope does not contain the scope in which the
12590          class was originally declared, the program is invalid.  */
12591       if (scope && !is_ancestor (scope, nested_name_specifier))
12592         {
12593           error ("declaration of `%D' in `%D' which does not "
12594                  "enclose `%D'", type, scope, nested_name_specifier);
12595           type = NULL_TREE;
12596           goto done;
12597         }
12598       /* [dcl.meaning]
12599
12600          A declarator-id shall not be qualified exception of the
12601          definition of a ... nested class outside of its class
12602          ... [or] a the definition or explicit instantiation of a
12603          class member of a namespace outside of its namespace.  */
12604       if (scope == nested_name_specifier)
12605         {
12606           pedwarn ("extra qualification ignored");
12607           nested_name_specifier = NULL_TREE;
12608           num_templates = 0;
12609         }
12610     }
12611   /* An explicit-specialization must be preceded by "template <>".  If
12612      it is not, try to recover gracefully.  */
12613   if (at_namespace_scope_p ()
12614       && parser->num_template_parameter_lists == 0
12615       && template_id_p)
12616     {
12617       error ("an explicit specialization must be preceded by 'template <>'");
12618       invalid_explicit_specialization_p = true;
12619       /* Take the same action that would have been taken by
12620          cp_parser_explicit_specialization.  */
12621       ++parser->num_template_parameter_lists;
12622       begin_specialization ();
12623     }
12624   /* There must be no "return" statements between this point and the
12625      end of this function; set "type "to the correct return value and
12626      use "goto done;" to return.  */
12627   /* Make sure that the right number of template parameters were
12628      present.  */
12629   if (!cp_parser_check_template_parameters (parser, num_templates))
12630     {
12631       /* If something went wrong, there is no point in even trying to
12632          process the class-definition.  */
12633       type = NULL_TREE;
12634       goto done;
12635     }
12636
12637   /* Look up the type.  */
12638   if (template_id_p)
12639     {
12640       type = TREE_TYPE (id);
12641       maybe_process_partial_specialization (type);
12642     }
12643   else if (!nested_name_specifier)
12644     {
12645       /* If the class was unnamed, create a dummy name.  */
12646       if (!id)
12647         id = make_anon_name ();
12648       type = xref_tag (class_key, id, /*globalize=*/false,
12649                        parser->num_template_parameter_lists);
12650     }
12651   else
12652     {
12653       tree class_type;
12654       bool pop_p = false;
12655
12656       /* Given:
12657
12658             template <typename T> struct S { struct T };
12659             template <typename T> struct S<T>::T { };
12660
12661          we will get a TYPENAME_TYPE when processing the definition of
12662          `S::T'.  We need to resolve it to the actual type before we
12663          try to define it.  */
12664       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12665         {
12666           class_type = resolve_typename_type (TREE_TYPE (type),
12667                                               /*only_current_p=*/false);
12668           if (class_type != error_mark_node)
12669             type = TYPE_NAME (class_type);
12670           else
12671             {
12672               cp_parser_error (parser, "could not resolve typename type");
12673               type = error_mark_node;
12674             }
12675         }
12676
12677       maybe_process_partial_specialization (TREE_TYPE (type));
12678       class_type = current_class_type;
12679       /* Enter the scope indicated by the nested-name-specifier.  */
12680       if (nested_name_specifier)
12681         pop_p = push_scope (nested_name_specifier);
12682       /* Get the canonical version of this type.  */
12683       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12684       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12685           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12686         type = push_template_decl (type);
12687       type = TREE_TYPE (type);
12688       if (nested_name_specifier)
12689         {
12690           *nested_name_specifier_p = true;
12691           if (pop_p)
12692             pop_scope (nested_name_specifier);
12693         }
12694     }
12695   /* Indicate whether this class was declared as a `class' or as a
12696      `struct'.  */
12697   if (TREE_CODE (type) == RECORD_TYPE)
12698     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12699   cp_parser_check_class_key (class_key, type);
12700
12701   /* Enter the scope containing the class; the names of base classes
12702      should be looked up in that context.  For example, given:
12703
12704        struct A { struct B {}; struct C; };
12705        struct A::C : B {};
12706
12707      is valid.  */
12708   if (nested_name_specifier)
12709     pop_p = push_scope (nested_name_specifier);
12710
12711   bases = NULL_TREE;
12712
12713   /* Get the list of base-classes, if there is one.  */
12714   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12715     bases = cp_parser_base_clause (parser);
12716
12717   /* Process the base classes.  */
12718   xref_basetypes (type, bases);
12719
12720   /* Leave the scope given by the nested-name-specifier.  We will
12721      enter the class scope itself while processing the members.  */
12722   if (pop_p)
12723     pop_scope (nested_name_specifier);
12724
12725  done:
12726   if (invalid_explicit_specialization_p)
12727     {
12728       end_specialization ();
12729       --parser->num_template_parameter_lists;
12730     }
12731   *attributes_p = attributes;
12732   return type;
12733 }
12734
12735 /* Parse a class-key.
12736
12737    class-key:
12738      class
12739      struct
12740      union
12741
12742    Returns the kind of class-key specified, or none_type to indicate
12743    error.  */
12744
12745 static enum tag_types
12746 cp_parser_class_key (cp_parser* parser)
12747 {
12748   cp_token *token;
12749   enum tag_types tag_type;
12750
12751   /* Look for the class-key.  */
12752   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12753   if (!token)
12754     return none_type;
12755
12756   /* Check to see if the TOKEN is a class-key.  */
12757   tag_type = cp_parser_token_is_class_key (token);
12758   if (!tag_type)
12759     cp_parser_error (parser, "expected class-key");
12760   return tag_type;
12761 }
12762
12763 /* Parse an (optional) member-specification.
12764
12765    member-specification:
12766      member-declaration member-specification [opt]
12767      access-specifier : member-specification [opt]  */
12768
12769 static void
12770 cp_parser_member_specification_opt (cp_parser* parser)
12771 {
12772   while (true)
12773     {
12774       cp_token *token;
12775       enum rid keyword;
12776
12777       /* Peek at the next token.  */
12778       token = cp_lexer_peek_token (parser->lexer);
12779       /* If it's a `}', or EOF then we've seen all the members.  */
12780       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12781         break;
12782
12783       /* See if this token is a keyword.  */
12784       keyword = token->keyword;
12785       switch (keyword)
12786         {
12787         case RID_PUBLIC:
12788         case RID_PROTECTED:
12789         case RID_PRIVATE:
12790           /* Consume the access-specifier.  */
12791           cp_lexer_consume_token (parser->lexer);
12792           /* Remember which access-specifier is active.  */
12793           current_access_specifier = token->value;
12794           /* Look for the `:'.  */
12795           cp_parser_require (parser, CPP_COLON, "`:'");
12796           break;
12797
12798         default:
12799           /* Otherwise, the next construction must be a
12800              member-declaration.  */
12801           cp_parser_member_declaration (parser);
12802         }
12803     }
12804 }
12805
12806 /* Parse a member-declaration.
12807
12808    member-declaration:
12809      decl-specifier-seq [opt] member-declarator-list [opt] ;
12810      function-definition ; [opt]
12811      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12812      using-declaration
12813      template-declaration
12814
12815    member-declarator-list:
12816      member-declarator
12817      member-declarator-list , member-declarator
12818
12819    member-declarator:
12820      declarator pure-specifier [opt]
12821      declarator constant-initializer [opt]
12822      identifier [opt] : constant-expression
12823
12824    GNU Extensions:
12825
12826    member-declaration:
12827      __extension__ member-declaration
12828
12829    member-declarator:
12830      declarator attributes [opt] pure-specifier [opt]
12831      declarator attributes [opt] constant-initializer [opt]
12832      identifier [opt] attributes [opt] : constant-expression  */
12833
12834 static void
12835 cp_parser_member_declaration (cp_parser* parser)
12836 {
12837   cp_decl_specifier_seq decl_specifiers;
12838   tree prefix_attributes;
12839   tree decl;
12840   int declares_class_or_enum;
12841   bool friend_p;
12842   cp_token *token;
12843   int saved_pedantic;
12844
12845   /* Check for the `__extension__' keyword.  */
12846   if (cp_parser_extension_opt (parser, &saved_pedantic))
12847     {
12848       /* Recurse.  */
12849       cp_parser_member_declaration (parser);
12850       /* Restore the old value of the PEDANTIC flag.  */
12851       pedantic = saved_pedantic;
12852
12853       return;
12854     }
12855
12856   /* Check for a template-declaration.  */
12857   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12858     {
12859       /* Parse the template-declaration.  */
12860       cp_parser_template_declaration (parser, /*member_p=*/true);
12861
12862       return;
12863     }
12864
12865   /* Check for a using-declaration.  */
12866   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12867     {
12868       /* Parse the using-declaration.  */
12869       cp_parser_using_declaration (parser);
12870
12871       return;
12872     }
12873
12874   /* Parse the decl-specifier-seq.  */
12875   cp_parser_decl_specifier_seq (parser,
12876                                 CP_PARSER_FLAGS_OPTIONAL,
12877                                 &decl_specifiers,
12878                                 &declares_class_or_enum);
12879   prefix_attributes = decl_specifiers.attributes;
12880   decl_specifiers.attributes = NULL_TREE;
12881   /* Check for an invalid type-name.  */
12882   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12883     return;
12884   /* If there is no declarator, then the decl-specifier-seq should
12885      specify a type.  */
12886   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12887     {
12888       /* If there was no decl-specifier-seq, and the next token is a
12889          `;', then we have something like:
12890
12891            struct S { ; };
12892
12893          [class.mem]
12894
12895          Each member-declaration shall declare at least one member
12896          name of the class.  */
12897       if (!decl_specifiers.any_specifiers_p)
12898         {
12899           cp_token *token = cp_lexer_peek_token (parser->lexer);
12900           if (pedantic && !token->in_system_header)
12901             pedwarn ("%Hextra %<;%>", &token->location);
12902         }
12903       else
12904         {
12905           tree type;
12906
12907           /* See if this declaration is a friend.  */
12908           friend_p = cp_parser_friend_p (&decl_specifiers);
12909           /* If there were decl-specifiers, check to see if there was
12910              a class-declaration.  */
12911           type = check_tag_decl (&decl_specifiers);
12912           /* Nested classes have already been added to the class, but
12913              a `friend' needs to be explicitly registered.  */
12914           if (friend_p)
12915             {
12916               /* If the `friend' keyword was present, the friend must
12917                  be introduced with a class-key.  */
12918                if (!declares_class_or_enum)
12919                  error ("a class-key must be used when declaring a friend");
12920                /* In this case:
12921
12922                     template <typename T> struct A {
12923                       friend struct A<T>::B;
12924                     };
12925
12926                   A<T>::B will be represented by a TYPENAME_TYPE, and
12927                   therefore not recognized by check_tag_decl.  */
12928                if (!type
12929                    && decl_specifiers.type
12930                    && TYPE_P (decl_specifiers.type))
12931                  type = decl_specifiers.type;
12932                if (!type || !TYPE_P (type))
12933                  error ("friend declaration does not name a class or "
12934                         "function");
12935                else
12936                  make_friend_class (current_class_type, type,
12937                                     /*complain=*/true);
12938             }
12939           /* If there is no TYPE, an error message will already have
12940              been issued.  */
12941           else if (!type || type == error_mark_node)
12942             ;
12943           /* An anonymous aggregate has to be handled specially; such
12944              a declaration really declares a data member (with a
12945              particular type), as opposed to a nested class.  */
12946           else if (ANON_AGGR_TYPE_P (type))
12947             {
12948               /* Remove constructors and such from TYPE, now that we
12949                  know it is an anonymous aggregate.  */
12950               fixup_anonymous_aggr (type);
12951               /* And make the corresponding data member.  */
12952               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12953               /* Add it to the class.  */
12954               finish_member_declaration (decl);
12955             }
12956           else
12957             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12958         }
12959     }
12960   else
12961     {
12962       /* See if these declarations will be friends.  */
12963       friend_p = cp_parser_friend_p (&decl_specifiers);
12964
12965       /* Keep going until we hit the `;' at the end of the
12966          declaration.  */
12967       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12968         {
12969           tree attributes = NULL_TREE;
12970           tree first_attribute;
12971
12972           /* Peek at the next token.  */
12973           token = cp_lexer_peek_token (parser->lexer);
12974
12975           /* Check for a bitfield declaration.  */
12976           if (token->type == CPP_COLON
12977               || (token->type == CPP_NAME
12978                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12979                   == CPP_COLON))
12980             {
12981               tree identifier;
12982               tree width;
12983
12984               /* Get the name of the bitfield.  Note that we cannot just
12985                  check TOKEN here because it may have been invalidated by
12986                  the call to cp_lexer_peek_nth_token above.  */
12987               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12988                 identifier = cp_parser_identifier (parser);
12989               else
12990                 identifier = NULL_TREE;
12991
12992               /* Consume the `:' token.  */
12993               cp_lexer_consume_token (parser->lexer);
12994               /* Get the width of the bitfield.  */
12995               width
12996                 = cp_parser_constant_expression (parser,
12997                                                  /*allow_non_constant=*/false,
12998                                                  NULL);
12999
13000               /* Look for attributes that apply to the bitfield.  */
13001               attributes = cp_parser_attributes_opt (parser);
13002               /* Remember which attributes are prefix attributes and
13003                  which are not.  */
13004               first_attribute = attributes;
13005               /* Combine the attributes.  */
13006               attributes = chainon (prefix_attributes, attributes);
13007
13008               /* Create the bitfield declaration.  */
13009               decl = grokbitfield (identifier
13010                                    ? make_id_declarator (identifier)
13011                                    : NULL,
13012                                    &decl_specifiers,
13013                                    width);
13014               /* Apply the attributes.  */
13015               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13016             }
13017           else
13018             {
13019               cp_declarator *declarator;
13020               tree initializer;
13021               tree asm_specification;
13022               int ctor_dtor_or_conv_p;
13023
13024               /* Parse the declarator.  */
13025               declarator
13026                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13027                                         &ctor_dtor_or_conv_p,
13028                                         /*parenthesized_p=*/NULL);
13029
13030               /* If something went wrong parsing the declarator, make sure
13031                  that we at least consume some tokens.  */
13032               if (declarator == cp_error_declarator)
13033                 {
13034                   /* Skip to the end of the statement.  */
13035                   cp_parser_skip_to_end_of_statement (parser);
13036                   /* If the next token is not a semicolon, that is
13037                      probably because we just skipped over the body of
13038                      a function.  So, we consume a semicolon if
13039                      present, but do not issue an error message if it
13040                      is not present.  */
13041                   if (cp_lexer_next_token_is (parser->lexer,
13042                                               CPP_SEMICOLON))
13043                     cp_lexer_consume_token (parser->lexer);
13044                   return;
13045                 }
13046
13047               cp_parser_check_for_definition_in_return_type
13048                 (declarator, declares_class_or_enum);
13049
13050               /* Look for an asm-specification.  */
13051               asm_specification = cp_parser_asm_specification_opt (parser);
13052               /* Look for attributes that apply to the declaration.  */
13053               attributes = cp_parser_attributes_opt (parser);
13054               /* Remember which attributes are prefix attributes and
13055                  which are not.  */
13056               first_attribute = attributes;
13057               /* Combine the attributes.  */
13058               attributes = chainon (prefix_attributes, attributes);
13059
13060               /* If it's an `=', then we have a constant-initializer or a
13061                  pure-specifier.  It is not correct to parse the
13062                  initializer before registering the member declaration
13063                  since the member declaration should be in scope while
13064                  its initializer is processed.  However, the rest of the
13065                  front end does not yet provide an interface that allows
13066                  us to handle this correctly.  */
13067               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13068                 {
13069                   /* In [class.mem]:
13070
13071                      A pure-specifier shall be used only in the declaration of
13072                      a virtual function.
13073
13074                      A member-declarator can contain a constant-initializer
13075                      only if it declares a static member of integral or
13076                      enumeration type.
13077
13078                      Therefore, if the DECLARATOR is for a function, we look
13079                      for a pure-specifier; otherwise, we look for a
13080                      constant-initializer.  When we call `grokfield', it will
13081                      perform more stringent semantics checks.  */
13082                   if (declarator->kind == cdk_function)
13083                     initializer = cp_parser_pure_specifier (parser);
13084                   else
13085                     /* Parse the initializer.  */
13086                     initializer = cp_parser_constant_initializer (parser);
13087                 }
13088               /* Otherwise, there is no initializer.  */
13089               else
13090                 initializer = NULL_TREE;
13091
13092               /* See if we are probably looking at a function
13093                  definition.  We are certainly not looking at at a
13094                  member-declarator.  Calling `grokfield' has
13095                  side-effects, so we must not do it unless we are sure
13096                  that we are looking at a member-declarator.  */
13097               if (cp_parser_token_starts_function_definition_p
13098                   (cp_lexer_peek_token (parser->lexer)))
13099                 {
13100                   /* The grammar does not allow a pure-specifier to be
13101                      used when a member function is defined.  (It is
13102                      possible that this fact is an oversight in the
13103                      standard, since a pure function may be defined
13104                      outside of the class-specifier.  */
13105                   if (initializer)
13106                     error ("pure-specifier on function-definition");
13107                   decl = cp_parser_save_member_function_body (parser,
13108                                                               &decl_specifiers,
13109                                                               declarator,
13110                                                               attributes);
13111                   /* If the member was not a friend, declare it here.  */
13112                   if (!friend_p)
13113                     finish_member_declaration (decl);
13114                   /* Peek at the next token.  */
13115                   token = cp_lexer_peek_token (parser->lexer);
13116                   /* If the next token is a semicolon, consume it.  */
13117                   if (token->type == CPP_SEMICOLON)
13118                     cp_lexer_consume_token (parser->lexer);
13119                   return;
13120                 }
13121               else
13122                 {
13123                   /* Create the declaration.  */
13124                   decl = grokfield (declarator, &decl_specifiers,
13125                                     initializer, asm_specification,
13126                                     attributes);
13127                   /* Any initialization must have been from a
13128                      constant-expression.  */
13129                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13130                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13131                 }
13132             }
13133
13134           /* Reset PREFIX_ATTRIBUTES.  */
13135           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13136             attributes = TREE_CHAIN (attributes);
13137           if (attributes)
13138             TREE_CHAIN (attributes) = NULL_TREE;
13139
13140           /* If there is any qualification still in effect, clear it
13141              now; we will be starting fresh with the next declarator.  */
13142           parser->scope = NULL_TREE;
13143           parser->qualifying_scope = NULL_TREE;
13144           parser->object_scope = NULL_TREE;
13145           /* If it's a `,', then there are more declarators.  */
13146           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13147             cp_lexer_consume_token (parser->lexer);
13148           /* If the next token isn't a `;', then we have a parse error.  */
13149           else if (cp_lexer_next_token_is_not (parser->lexer,
13150                                                CPP_SEMICOLON))
13151             {
13152               cp_parser_error (parser, "expected `;'");
13153               /* Skip tokens until we find a `;'.  */
13154               cp_parser_skip_to_end_of_statement (parser);
13155
13156               break;
13157             }
13158
13159           if (decl)
13160             {
13161               /* Add DECL to the list of members.  */
13162               if (!friend_p)
13163                 finish_member_declaration (decl);
13164
13165               if (TREE_CODE (decl) == FUNCTION_DECL)
13166                 cp_parser_save_default_args (parser, decl);
13167             }
13168         }
13169     }
13170
13171   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13172 }
13173
13174 /* Parse a pure-specifier.
13175
13176    pure-specifier:
13177      = 0
13178
13179    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13180    Otherwise, ERROR_MARK_NODE is returned.  */
13181
13182 static tree
13183 cp_parser_pure_specifier (cp_parser* parser)
13184 {
13185   cp_token *token;
13186
13187   /* Look for the `=' token.  */
13188   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13189     return error_mark_node;
13190   /* Look for the `0' token.  */
13191   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13192   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13193      to get information from the lexer about how the number was
13194      spelled in order to fix this problem.  */
13195   if (!token || !integer_zerop (token->value))
13196     return error_mark_node;
13197
13198   return integer_zero_node;
13199 }
13200
13201 /* Parse a constant-initializer.
13202
13203    constant-initializer:
13204      = constant-expression
13205
13206    Returns a representation of the constant-expression.  */
13207
13208 static tree
13209 cp_parser_constant_initializer (cp_parser* parser)
13210 {
13211   /* Look for the `=' token.  */
13212   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13213     return error_mark_node;
13214
13215   /* It is invalid to write:
13216
13217        struct S { static const int i = { 7 }; };
13218
13219      */
13220   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13221     {
13222       cp_parser_error (parser,
13223                        "a brace-enclosed initializer is not allowed here");
13224       /* Consume the opening brace.  */
13225       cp_lexer_consume_token (parser->lexer);
13226       /* Skip the initializer.  */
13227       cp_parser_skip_to_closing_brace (parser);
13228       /* Look for the trailing `}'.  */
13229       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13230
13231       return error_mark_node;
13232     }
13233
13234   return cp_parser_constant_expression (parser,
13235                                         /*allow_non_constant=*/false,
13236                                         NULL);
13237 }
13238
13239 /* Derived classes [gram.class.derived] */
13240
13241 /* Parse a base-clause.
13242
13243    base-clause:
13244      : base-specifier-list
13245
13246    base-specifier-list:
13247      base-specifier
13248      base-specifier-list , base-specifier
13249
13250    Returns a TREE_LIST representing the base-classes, in the order in
13251    which they were declared.  The representation of each node is as
13252    described by cp_parser_base_specifier.
13253
13254    In the case that no bases are specified, this function will return
13255    NULL_TREE, not ERROR_MARK_NODE.  */
13256
13257 static tree
13258 cp_parser_base_clause (cp_parser* parser)
13259 {
13260   tree bases = NULL_TREE;
13261
13262   /* Look for the `:' that begins the list.  */
13263   cp_parser_require (parser, CPP_COLON, "`:'");
13264
13265   /* Scan the base-specifier-list.  */
13266   while (true)
13267     {
13268       cp_token *token;
13269       tree base;
13270
13271       /* Look for the base-specifier.  */
13272       base = cp_parser_base_specifier (parser);
13273       /* Add BASE to the front of the list.  */
13274       if (base != error_mark_node)
13275         {
13276           TREE_CHAIN (base) = bases;
13277           bases = base;
13278         }
13279       /* Peek at the next token.  */
13280       token = cp_lexer_peek_token (parser->lexer);
13281       /* If it's not a comma, then the list is complete.  */
13282       if (token->type != CPP_COMMA)
13283         break;
13284       /* Consume the `,'.  */
13285       cp_lexer_consume_token (parser->lexer);
13286     }
13287
13288   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13289      base class had a qualified name.  However, the next name that
13290      appears is certainly not qualified.  */
13291   parser->scope = NULL_TREE;
13292   parser->qualifying_scope = NULL_TREE;
13293   parser->object_scope = NULL_TREE;
13294
13295   return nreverse (bases);
13296 }
13297
13298 /* Parse a base-specifier.
13299
13300    base-specifier:
13301      :: [opt] nested-name-specifier [opt] class-name
13302      virtual access-specifier [opt] :: [opt] nested-name-specifier
13303        [opt] class-name
13304      access-specifier virtual [opt] :: [opt] nested-name-specifier
13305        [opt] class-name
13306
13307    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13308    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13309    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13310    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13311
13312 static tree
13313 cp_parser_base_specifier (cp_parser* parser)
13314 {
13315   cp_token *token;
13316   bool done = false;
13317   bool virtual_p = false;
13318   bool duplicate_virtual_error_issued_p = false;
13319   bool duplicate_access_error_issued_p = false;
13320   bool class_scope_p, template_p;
13321   tree access = access_default_node;
13322   tree type;
13323
13324   /* Process the optional `virtual' and `access-specifier'.  */
13325   while (!done)
13326     {
13327       /* Peek at the next token.  */
13328       token = cp_lexer_peek_token (parser->lexer);
13329       /* Process `virtual'.  */
13330       switch (token->keyword)
13331         {
13332         case RID_VIRTUAL:
13333           /* If `virtual' appears more than once, issue an error.  */
13334           if (virtual_p && !duplicate_virtual_error_issued_p)
13335             {
13336               cp_parser_error (parser,
13337                                "`virtual' specified more than once in base-specified");
13338               duplicate_virtual_error_issued_p = true;
13339             }
13340
13341           virtual_p = true;
13342
13343           /* Consume the `virtual' token.  */
13344           cp_lexer_consume_token (parser->lexer);
13345
13346           break;
13347
13348         case RID_PUBLIC:
13349         case RID_PROTECTED:
13350         case RID_PRIVATE:
13351           /* If more than one access specifier appears, issue an
13352              error.  */
13353           if (access != access_default_node
13354               && !duplicate_access_error_issued_p)
13355             {
13356               cp_parser_error (parser,
13357                                "more than one access specifier in base-specified");
13358               duplicate_access_error_issued_p = true;
13359             }
13360
13361           access = ridpointers[(int) token->keyword];
13362
13363           /* Consume the access-specifier.  */
13364           cp_lexer_consume_token (parser->lexer);
13365
13366           break;
13367
13368         default:
13369           done = true;
13370           break;
13371         }
13372     }
13373   /* It is not uncommon to see programs mechanically, erroneously, use
13374      the 'typename' keyword to denote (dependent) qualified types
13375      as base classes.  */
13376   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13377     {
13378       if (!processing_template_decl)
13379         error ("keyword `typename' not allowed outside of templates");
13380       else
13381         error ("keyword `typename' not allowed in this context "
13382                "(the base class is implicitly a type)");
13383       cp_lexer_consume_token (parser->lexer);
13384     }
13385
13386   /* Look for the optional `::' operator.  */
13387   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13388   /* Look for the nested-name-specifier.  The simplest way to
13389      implement:
13390
13391        [temp.res]
13392
13393        The keyword `typename' is not permitted in a base-specifier or
13394        mem-initializer; in these contexts a qualified name that
13395        depends on a template-parameter is implicitly assumed to be a
13396        type name.
13397
13398      is to pretend that we have seen the `typename' keyword at this
13399      point.  */
13400   cp_parser_nested_name_specifier_opt (parser,
13401                                        /*typename_keyword_p=*/true,
13402                                        /*check_dependency_p=*/true,
13403                                        /*type_p=*/true,
13404                                        /*is_declaration=*/true);
13405   /* If the base class is given by a qualified name, assume that names
13406      we see are type names or templates, as appropriate.  */
13407   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13408   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13409
13410   /* Finally, look for the class-name.  */
13411   type = cp_parser_class_name (parser,
13412                                class_scope_p,
13413                                template_p,
13414                                /*type_p=*/true,
13415                                /*check_dependency_p=*/true,
13416                                /*class_head_p=*/false,
13417                                /*is_declaration=*/true);
13418
13419   if (type == error_mark_node)
13420     return error_mark_node;
13421
13422   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13423 }
13424
13425 /* Exception handling [gram.exception] */
13426
13427 /* Parse an (optional) exception-specification.
13428
13429    exception-specification:
13430      throw ( type-id-list [opt] )
13431
13432    Returns a TREE_LIST representing the exception-specification.  The
13433    TREE_VALUE of each node is a type.  */
13434
13435 static tree
13436 cp_parser_exception_specification_opt (cp_parser* parser)
13437 {
13438   cp_token *token;
13439   tree type_id_list;
13440
13441   /* Peek at the next token.  */
13442   token = cp_lexer_peek_token (parser->lexer);
13443   /* If it's not `throw', then there's no exception-specification.  */
13444   if (!cp_parser_is_keyword (token, RID_THROW))
13445     return NULL_TREE;
13446
13447   /* Consume the `throw'.  */
13448   cp_lexer_consume_token (parser->lexer);
13449
13450   /* Look for the `('.  */
13451   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13452
13453   /* Peek at the next token.  */
13454   token = cp_lexer_peek_token (parser->lexer);
13455   /* If it's not a `)', then there is a type-id-list.  */
13456   if (token->type != CPP_CLOSE_PAREN)
13457     {
13458       const char *saved_message;
13459
13460       /* Types may not be defined in an exception-specification.  */
13461       saved_message = parser->type_definition_forbidden_message;
13462       parser->type_definition_forbidden_message
13463         = "types may not be defined in an exception-specification";
13464       /* Parse the type-id-list.  */
13465       type_id_list = cp_parser_type_id_list (parser);
13466       /* Restore the saved message.  */
13467       parser->type_definition_forbidden_message = saved_message;
13468     }
13469   else
13470     type_id_list = empty_except_spec;
13471
13472   /* Look for the `)'.  */
13473   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13474
13475   return type_id_list;
13476 }
13477
13478 /* Parse an (optional) type-id-list.
13479
13480    type-id-list:
13481      type-id
13482      type-id-list , type-id
13483
13484    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13485    in the order that the types were presented.  */
13486
13487 static tree
13488 cp_parser_type_id_list (cp_parser* parser)
13489 {
13490   tree types = NULL_TREE;
13491
13492   while (true)
13493     {
13494       cp_token *token;
13495       tree type;
13496
13497       /* Get the next type-id.  */
13498       type = cp_parser_type_id (parser);
13499       /* Add it to the list.  */
13500       types = add_exception_specifier (types, type, /*complain=*/1);
13501       /* Peek at the next token.  */
13502       token = cp_lexer_peek_token (parser->lexer);
13503       /* If it is not a `,', we are done.  */
13504       if (token->type != CPP_COMMA)
13505         break;
13506       /* Consume the `,'.  */
13507       cp_lexer_consume_token (parser->lexer);
13508     }
13509
13510   return nreverse (types);
13511 }
13512
13513 /* Parse a try-block.
13514
13515    try-block:
13516      try compound-statement handler-seq  */
13517
13518 static tree
13519 cp_parser_try_block (cp_parser* parser)
13520 {
13521   tree try_block;
13522
13523   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13524   try_block = begin_try_block ();
13525   cp_parser_compound_statement (parser, NULL, true);
13526   finish_try_block (try_block);
13527   cp_parser_handler_seq (parser);
13528   finish_handler_sequence (try_block);
13529
13530   return try_block;
13531 }
13532
13533 /* Parse a function-try-block.
13534
13535    function-try-block:
13536      try ctor-initializer [opt] function-body handler-seq  */
13537
13538 static bool
13539 cp_parser_function_try_block (cp_parser* parser)
13540 {
13541   tree try_block;
13542   bool ctor_initializer_p;
13543
13544   /* Look for the `try' keyword.  */
13545   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13546     return false;
13547   /* Let the rest of the front-end know where we are.  */
13548   try_block = begin_function_try_block ();
13549   /* Parse the function-body.  */
13550   ctor_initializer_p
13551     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13552   /* We're done with the `try' part.  */
13553   finish_function_try_block (try_block);
13554   /* Parse the handlers.  */
13555   cp_parser_handler_seq (parser);
13556   /* We're done with the handlers.  */
13557   finish_function_handler_sequence (try_block);
13558
13559   return ctor_initializer_p;
13560 }
13561
13562 /* Parse a handler-seq.
13563
13564    handler-seq:
13565      handler handler-seq [opt]  */
13566
13567 static void
13568 cp_parser_handler_seq (cp_parser* parser)
13569 {
13570   while (true)
13571     {
13572       cp_token *token;
13573
13574       /* Parse the handler.  */
13575       cp_parser_handler (parser);
13576       /* Peek at the next token.  */
13577       token = cp_lexer_peek_token (parser->lexer);
13578       /* If it's not `catch' then there are no more handlers.  */
13579       if (!cp_parser_is_keyword (token, RID_CATCH))
13580         break;
13581     }
13582 }
13583
13584 /* Parse a handler.
13585
13586    handler:
13587      catch ( exception-declaration ) compound-statement  */
13588
13589 static void
13590 cp_parser_handler (cp_parser* parser)
13591 {
13592   tree handler;
13593   tree declaration;
13594
13595   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13596   handler = begin_handler ();
13597   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13598   declaration = cp_parser_exception_declaration (parser);
13599   finish_handler_parms (declaration, handler);
13600   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13601   cp_parser_compound_statement (parser, NULL, false);
13602   finish_handler (handler);
13603 }
13604
13605 /* Parse an exception-declaration.
13606
13607    exception-declaration:
13608      type-specifier-seq declarator
13609      type-specifier-seq abstract-declarator
13610      type-specifier-seq
13611      ...
13612
13613    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13614    ellipsis variant is used.  */
13615
13616 static tree
13617 cp_parser_exception_declaration (cp_parser* parser)
13618 {
13619   tree decl;
13620   cp_decl_specifier_seq type_specifiers;
13621   cp_declarator *declarator;
13622   const char *saved_message;
13623
13624   /* If it's an ellipsis, it's easy to handle.  */
13625   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13626     {
13627       /* Consume the `...' token.  */
13628       cp_lexer_consume_token (parser->lexer);
13629       return NULL_TREE;
13630     }
13631
13632   /* Types may not be defined in exception-declarations.  */
13633   saved_message = parser->type_definition_forbidden_message;
13634   parser->type_definition_forbidden_message
13635     = "types may not be defined in exception-declarations";
13636
13637   /* Parse the type-specifier-seq.  */
13638   cp_parser_type_specifier_seq (parser, &type_specifiers);
13639   /* If it's a `)', then there is no declarator.  */
13640   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13641     declarator = NULL;
13642   else
13643     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13644                                        /*ctor_dtor_or_conv_p=*/NULL,
13645                                        /*parenthesized_p=*/NULL);
13646
13647   /* Restore the saved message.  */
13648   parser->type_definition_forbidden_message = saved_message;
13649
13650   if (type_specifiers.any_specifiers_p)
13651     {
13652       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13653       if (decl == NULL_TREE)
13654         error ("invalid catch parameter");
13655     }
13656   else
13657     decl = NULL_TREE;
13658
13659   return decl;
13660 }
13661
13662 /* Parse a throw-expression.
13663
13664    throw-expression:
13665      throw assignment-expression [opt]
13666
13667    Returns a THROW_EXPR representing the throw-expression.  */
13668
13669 static tree
13670 cp_parser_throw_expression (cp_parser* parser)
13671 {
13672   tree expression;
13673   cp_token* token;
13674
13675   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13676   token = cp_lexer_peek_token (parser->lexer);
13677   /* Figure out whether or not there is an assignment-expression
13678      following the "throw" keyword.  */
13679   if (token->type == CPP_COMMA
13680       || token->type == CPP_SEMICOLON
13681       || token->type == CPP_CLOSE_PAREN
13682       || token->type == CPP_CLOSE_SQUARE
13683       || token->type == CPP_CLOSE_BRACE
13684       || token->type == CPP_COLON)
13685     expression = NULL_TREE;
13686   else
13687     expression = cp_parser_assignment_expression (parser);
13688
13689   return build_throw (expression);
13690 }
13691
13692 /* GNU Extensions */
13693
13694 /* Parse an (optional) asm-specification.
13695
13696    asm-specification:
13697      asm ( string-literal )
13698
13699    If the asm-specification is present, returns a STRING_CST
13700    corresponding to the string-literal.  Otherwise, returns
13701    NULL_TREE.  */
13702
13703 static tree
13704 cp_parser_asm_specification_opt (cp_parser* parser)
13705 {
13706   cp_token *token;
13707   tree asm_specification;
13708
13709   /* Peek at the next token.  */
13710   token = cp_lexer_peek_token (parser->lexer);
13711   /* If the next token isn't the `asm' keyword, then there's no
13712      asm-specification.  */
13713   if (!cp_parser_is_keyword (token, RID_ASM))
13714     return NULL_TREE;
13715
13716   /* Consume the `asm' token.  */
13717   cp_lexer_consume_token (parser->lexer);
13718   /* Look for the `('.  */
13719   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13720
13721   /* Look for the string-literal.  */
13722   asm_specification = cp_parser_string_literal (parser, false, false);
13723
13724   /* Look for the `)'.  */
13725   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13726
13727   return asm_specification;
13728 }
13729
13730 /* Parse an asm-operand-list.
13731
13732    asm-operand-list:
13733      asm-operand
13734      asm-operand-list , asm-operand
13735
13736    asm-operand:
13737      string-literal ( expression )
13738      [ string-literal ] string-literal ( expression )
13739
13740    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13741    each node is the expression.  The TREE_PURPOSE is itself a
13742    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13743    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13744    is a STRING_CST for the string literal before the parenthesis.  */
13745
13746 static tree
13747 cp_parser_asm_operand_list (cp_parser* parser)
13748 {
13749   tree asm_operands = NULL_TREE;
13750
13751   while (true)
13752     {
13753       tree string_literal;
13754       tree expression;
13755       tree name;
13756
13757       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13758         {
13759           /* Consume the `[' token.  */
13760           cp_lexer_consume_token (parser->lexer);
13761           /* Read the operand name.  */
13762           name = cp_parser_identifier (parser);
13763           if (name != error_mark_node)
13764             name = build_string (IDENTIFIER_LENGTH (name),
13765                                  IDENTIFIER_POINTER (name));
13766           /* Look for the closing `]'.  */
13767           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13768         }
13769       else
13770         name = NULL_TREE;
13771       /* Look for the string-literal.  */
13772       string_literal = cp_parser_string_literal (parser, false, false);
13773
13774       /* Look for the `('.  */
13775       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13776       /* Parse the expression.  */
13777       expression = cp_parser_expression (parser);
13778       /* Look for the `)'.  */
13779       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13780
13781       /* Add this operand to the list.  */
13782       asm_operands = tree_cons (build_tree_list (name, string_literal),
13783                                 expression,
13784                                 asm_operands);
13785       /* If the next token is not a `,', there are no more
13786          operands.  */
13787       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13788         break;
13789       /* Consume the `,'.  */
13790       cp_lexer_consume_token (parser->lexer);
13791     }
13792
13793   return nreverse (asm_operands);
13794 }
13795
13796 /* Parse an asm-clobber-list.
13797
13798    asm-clobber-list:
13799      string-literal
13800      asm-clobber-list , string-literal
13801
13802    Returns a TREE_LIST, indicating the clobbers in the order that they
13803    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13804
13805 static tree
13806 cp_parser_asm_clobber_list (cp_parser* parser)
13807 {
13808   tree clobbers = NULL_TREE;
13809
13810   while (true)
13811     {
13812       tree string_literal;
13813
13814       /* Look for the string literal.  */
13815       string_literal = cp_parser_string_literal (parser, false, false);
13816       /* Add it to the list.  */
13817       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13818       /* If the next token is not a `,', then the list is
13819          complete.  */
13820       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13821         break;
13822       /* Consume the `,' token.  */
13823       cp_lexer_consume_token (parser->lexer);
13824     }
13825
13826   return clobbers;
13827 }
13828
13829 /* Parse an (optional) series of attributes.
13830
13831    attributes:
13832      attributes attribute
13833
13834    attribute:
13835      __attribute__ (( attribute-list [opt] ))
13836
13837    The return value is as for cp_parser_attribute_list.  */
13838
13839 static tree
13840 cp_parser_attributes_opt (cp_parser* parser)
13841 {
13842   tree attributes = NULL_TREE;
13843
13844   while (true)
13845     {
13846       cp_token *token;
13847       tree attribute_list;
13848
13849       /* Peek at the next token.  */
13850       token = cp_lexer_peek_token (parser->lexer);
13851       /* If it's not `__attribute__', then we're done.  */
13852       if (token->keyword != RID_ATTRIBUTE)
13853         break;
13854
13855       /* Consume the `__attribute__' keyword.  */
13856       cp_lexer_consume_token (parser->lexer);
13857       /* Look for the two `(' tokens.  */
13858       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13859       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13860
13861       /* Peek at the next token.  */
13862       token = cp_lexer_peek_token (parser->lexer);
13863       if (token->type != CPP_CLOSE_PAREN)
13864         /* Parse the attribute-list.  */
13865         attribute_list = cp_parser_attribute_list (parser);
13866       else
13867         /* If the next token is a `)', then there is no attribute
13868            list.  */
13869         attribute_list = NULL;
13870
13871       /* Look for the two `)' tokens.  */
13872       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13873       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13874
13875       /* Add these new attributes to the list.  */
13876       attributes = chainon (attributes, attribute_list);
13877     }
13878
13879   return attributes;
13880 }
13881
13882 /* Parse an attribute-list.
13883
13884    attribute-list:
13885      attribute
13886      attribute-list , attribute
13887
13888    attribute:
13889      identifier
13890      identifier ( identifier )
13891      identifier ( identifier , expression-list )
13892      identifier ( expression-list )
13893
13894    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13895    TREE_PURPOSE of each node is the identifier indicating which
13896    attribute is in use.  The TREE_VALUE represents the arguments, if
13897    any.  */
13898
13899 static tree
13900 cp_parser_attribute_list (cp_parser* parser)
13901 {
13902   tree attribute_list = NULL_TREE;
13903   bool save_translate_strings_p = parser->translate_strings_p;
13904
13905   parser->translate_strings_p = false;
13906   while (true)
13907     {
13908       cp_token *token;
13909       tree identifier;
13910       tree attribute;
13911
13912       /* Look for the identifier.  We also allow keywords here; for
13913          example `__attribute__ ((const))' is legal.  */
13914       token = cp_lexer_peek_token (parser->lexer);
13915       if (token->type != CPP_NAME
13916           && token->type != CPP_KEYWORD)
13917         return error_mark_node;
13918       /* Consume the token.  */
13919       token = cp_lexer_consume_token (parser->lexer);
13920
13921       /* Save away the identifier that indicates which attribute this is.  */
13922       identifier = token->value;
13923       attribute = build_tree_list (identifier, NULL_TREE);
13924
13925       /* Peek at the next token.  */
13926       token = cp_lexer_peek_token (parser->lexer);
13927       /* If it's an `(', then parse the attribute arguments.  */
13928       if (token->type == CPP_OPEN_PAREN)
13929         {
13930           tree arguments;
13931
13932           arguments = (cp_parser_parenthesized_expression_list
13933                        (parser, true, /*non_constant_p=*/NULL));
13934           /* Save the identifier and arguments away.  */
13935           TREE_VALUE (attribute) = arguments;
13936         }
13937
13938       /* Add this attribute to the list.  */
13939       TREE_CHAIN (attribute) = attribute_list;
13940       attribute_list = attribute;
13941
13942       /* Now, look for more attributes.  */
13943       token = cp_lexer_peek_token (parser->lexer);
13944       /* If the next token isn't a `,', we're done.  */
13945       if (token->type != CPP_COMMA)
13946         break;
13947
13948       /* Consume the comma and keep going.  */
13949       cp_lexer_consume_token (parser->lexer);
13950     }
13951   parser->translate_strings_p = save_translate_strings_p;
13952
13953   /* We built up the list in reverse order.  */
13954   return nreverse (attribute_list);
13955 }
13956
13957 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13958    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13959    current value of the PEDANTIC flag, regardless of whether or not
13960    the `__extension__' keyword is present.  The caller is responsible
13961    for restoring the value of the PEDANTIC flag.  */
13962
13963 static bool
13964 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13965 {
13966   /* Save the old value of the PEDANTIC flag.  */
13967   *saved_pedantic = pedantic;
13968
13969   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13970     {
13971       /* Consume the `__extension__' token.  */
13972       cp_lexer_consume_token (parser->lexer);
13973       /* We're not being pedantic while the `__extension__' keyword is
13974          in effect.  */
13975       pedantic = 0;
13976
13977       return true;
13978     }
13979
13980   return false;
13981 }
13982
13983 /* Parse a label declaration.
13984
13985    label-declaration:
13986      __label__ label-declarator-seq ;
13987
13988    label-declarator-seq:
13989      identifier , label-declarator-seq
13990      identifier  */
13991
13992 static void
13993 cp_parser_label_declaration (cp_parser* parser)
13994 {
13995   /* Look for the `__label__' keyword.  */
13996   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13997
13998   while (true)
13999     {
14000       tree identifier;
14001
14002       /* Look for an identifier.  */
14003       identifier = cp_parser_identifier (parser);
14004       /* Declare it as a lobel.  */
14005       finish_label_decl (identifier);
14006       /* If the next token is a `;', stop.  */
14007       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14008         break;
14009       /* Look for the `,' separating the label declarations.  */
14010       cp_parser_require (parser, CPP_COMMA, "`,'");
14011     }
14012
14013   /* Look for the final `;'.  */
14014   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14015 }
14016
14017 /* Support Functions */
14018
14019 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14020    NAME should have one of the representations used for an
14021    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14022    is returned.  If PARSER->SCOPE is a dependent type, then a
14023    SCOPE_REF is returned.
14024
14025    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14026    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14027    was formed.  Abstractly, such entities should not be passed to this
14028    function, because they do not need to be looked up, but it is
14029    simpler to check for this special case here, rather than at the
14030    call-sites.
14031
14032    In cases not explicitly covered above, this function returns a
14033    DECL, OVERLOAD, or baselink representing the result of the lookup.
14034    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14035    is returned.
14036
14037    If IS_TYPE is TRUE, bindings that do not refer to types are
14038    ignored.
14039
14040    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14041    ignored.
14042
14043    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14044    are ignored.
14045
14046    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14047    types.  
14048
14049    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14050    results in an ambiguity, and false otherwise.  */
14051
14052 static tree
14053 cp_parser_lookup_name (cp_parser *parser, tree name,
14054                        bool is_type, bool is_template, bool is_namespace,
14055                        bool check_dependency,
14056                        bool *ambiguous_p)
14057 {
14058   tree decl;
14059   tree object_type = parser->context->object_type;
14060
14061   /* Assume that the lookup will be unambiguous.  */
14062   if (ambiguous_p)
14063     *ambiguous_p = false;
14064
14065   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14066      no longer valid.  Note that if we are parsing tentatively, and
14067      the parse fails, OBJECT_TYPE will be automatically restored.  */
14068   parser->context->object_type = NULL_TREE;
14069
14070   if (name == error_mark_node)
14071     return error_mark_node;
14072
14073   /* A template-id has already been resolved; there is no lookup to
14074      do.  */
14075   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14076     return name;
14077   if (BASELINK_P (name))
14078     {
14079       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14080                   == TEMPLATE_ID_EXPR);
14081       return name;
14082     }
14083
14084   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14085      it should already have been checked to make sure that the name
14086      used matches the type being destroyed.  */
14087   if (TREE_CODE (name) == BIT_NOT_EXPR)
14088     {
14089       tree type;
14090
14091       /* Figure out to which type this destructor applies.  */
14092       if (parser->scope)
14093         type = parser->scope;
14094       else if (object_type)
14095         type = object_type;
14096       else
14097         type = current_class_type;
14098       /* If that's not a class type, there is no destructor.  */
14099       if (!type || !CLASS_TYPE_P (type))
14100         return error_mark_node;
14101       if (!CLASSTYPE_DESTRUCTORS (type))
14102           return error_mark_node;
14103       /* If it was a class type, return the destructor.  */
14104       return CLASSTYPE_DESTRUCTORS (type);
14105     }
14106
14107   /* By this point, the NAME should be an ordinary identifier.  If
14108      the id-expression was a qualified name, the qualifying scope is
14109      stored in PARSER->SCOPE at this point.  */
14110   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14111
14112   /* Perform the lookup.  */
14113   if (parser->scope)
14114     {
14115       bool dependent_p;
14116
14117       if (parser->scope == error_mark_node)
14118         return error_mark_node;
14119
14120       /* If the SCOPE is dependent, the lookup must be deferred until
14121          the template is instantiated -- unless we are explicitly
14122          looking up names in uninstantiated templates.  Even then, we
14123          cannot look up the name if the scope is not a class type; it
14124          might, for example, be a template type parameter.  */
14125       dependent_p = (TYPE_P (parser->scope)
14126                      && !(parser->in_declarator_p
14127                           && currently_open_class (parser->scope))
14128                      && dependent_type_p (parser->scope));
14129       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14130            && dependent_p)
14131         {
14132           if (is_type)
14133             /* The resolution to Core Issue 180 says that `struct A::B'
14134                should be considered a type-name, even if `A' is
14135                dependent.  */
14136             decl = TYPE_NAME (make_typename_type (parser->scope,
14137                                                   name,
14138                                                   /*complain=*/1));
14139           else if (is_template)
14140             decl = make_unbound_class_template (parser->scope,
14141                                                 name,
14142                                                 /*complain=*/1);
14143           else
14144             decl = build_nt (SCOPE_REF, parser->scope, name);
14145         }
14146       else
14147         {
14148           bool pop_p = false;
14149
14150           /* If PARSER->SCOPE is a dependent type, then it must be a
14151              class type, and we must not be checking dependencies;
14152              otherwise, we would have processed this lookup above.  So
14153              that PARSER->SCOPE is not considered a dependent base by
14154              lookup_member, we must enter the scope here.  */
14155           if (dependent_p)
14156             pop_p = push_scope (parser->scope);
14157           /* If the PARSER->SCOPE is a a template specialization, it
14158              may be instantiated during name lookup.  In that case,
14159              errors may be issued.  Even if we rollback the current
14160              tentative parse, those errors are valid.  */
14161           decl = lookup_qualified_name (parser->scope, name, is_type,
14162                                         /*complain=*/true);
14163           if (pop_p)
14164             pop_scope (parser->scope);
14165         }
14166       parser->qualifying_scope = parser->scope;
14167       parser->object_scope = NULL_TREE;
14168     }
14169   else if (object_type)
14170     {
14171       tree object_decl = NULL_TREE;
14172       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14173          OBJECT_TYPE is not a class.  */
14174       if (CLASS_TYPE_P (object_type))
14175         /* If the OBJECT_TYPE is a template specialization, it may
14176            be instantiated during name lookup.  In that case, errors
14177            may be issued.  Even if we rollback the current tentative
14178            parse, those errors are valid.  */
14179         object_decl = lookup_member (object_type,
14180                                      name,
14181                                      /*protect=*/0, is_type);
14182       /* Look it up in the enclosing context, too.  */
14183       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14184                                /*block_p=*/true, is_namespace,
14185                                /*flags=*/0);
14186       parser->object_scope = object_type;
14187       parser->qualifying_scope = NULL_TREE;
14188       if (object_decl)
14189         decl = object_decl;
14190     }
14191   else
14192     {
14193       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14194                                /*block_p=*/true, is_namespace,
14195                                /*flags=*/0);
14196       parser->qualifying_scope = NULL_TREE;
14197       parser->object_scope = NULL_TREE;
14198     }
14199
14200   /* If the lookup failed, let our caller know.  */
14201   if (!decl
14202       || decl == error_mark_node
14203       || (TREE_CODE (decl) == FUNCTION_DECL
14204           && DECL_ANTICIPATED (decl)))
14205     return error_mark_node;
14206
14207   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14208   if (TREE_CODE (decl) == TREE_LIST)
14209     {
14210       if (ambiguous_p)
14211         *ambiguous_p = true;
14212       /* The error message we have to print is too complicated for
14213          cp_parser_error, so we incorporate its actions directly.  */
14214       if (!cp_parser_simulate_error (parser))
14215         {
14216           error ("reference to `%D' is ambiguous", name);
14217           print_candidates (decl);
14218         }
14219       return error_mark_node;
14220     }
14221
14222   gcc_assert (DECL_P (decl)
14223               || TREE_CODE (decl) == OVERLOAD
14224               || TREE_CODE (decl) == SCOPE_REF
14225               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14226               || BASELINK_P (decl));
14227
14228   /* If we have resolved the name of a member declaration, check to
14229      see if the declaration is accessible.  When the name resolves to
14230      set of overloaded functions, accessibility is checked when
14231      overload resolution is done.
14232
14233      During an explicit instantiation, access is not checked at all,
14234      as per [temp.explicit].  */
14235   if (DECL_P (decl))
14236     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14237
14238   return decl;
14239 }
14240
14241 /* Like cp_parser_lookup_name, but for use in the typical case where
14242    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14243    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14244
14245 static tree
14246 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14247 {
14248   return cp_parser_lookup_name (parser, name,
14249                                 /*is_type=*/false,
14250                                 /*is_template=*/false,
14251                                 /*is_namespace=*/false,
14252                                 /*check_dependency=*/true,
14253                                 /*ambiguous_p=*/NULL);
14254 }
14255
14256 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14257    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14258    true, the DECL indicates the class being defined in a class-head,
14259    or declared in an elaborated-type-specifier.
14260
14261    Otherwise, return DECL.  */
14262
14263 static tree
14264 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14265 {
14266   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14267      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14268
14269        struct A {
14270          template <typename T> struct B;
14271        };
14272
14273        template <typename T> struct A::B {};
14274
14275      Similarly, in a elaborated-type-specifier:
14276
14277        namespace N { struct X{}; }
14278
14279        struct A {
14280          template <typename T> friend struct N::X;
14281        };
14282
14283      However, if the DECL refers to a class type, and we are in
14284      the scope of the class, then the name lookup automatically
14285      finds the TYPE_DECL created by build_self_reference rather
14286      than a TEMPLATE_DECL.  For example, in:
14287
14288        template <class T> struct S {
14289          S s;
14290        };
14291
14292      there is no need to handle such case.  */
14293
14294   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14295     return DECL_TEMPLATE_RESULT (decl);
14296
14297   return decl;
14298 }
14299
14300 /* If too many, or too few, template-parameter lists apply to the
14301    declarator, issue an error message.  Returns TRUE if all went well,
14302    and FALSE otherwise.  */
14303
14304 static bool
14305 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14306                                                 cp_declarator *declarator)
14307 {
14308   unsigned num_templates;
14309
14310   /* We haven't seen any classes that involve template parameters yet.  */
14311   num_templates = 0;
14312
14313   switch (declarator->kind)
14314     {
14315     case cdk_id:
14316       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14317         {
14318           tree scope;
14319           tree member;
14320
14321           scope = TREE_OPERAND (declarator->u.id.name, 0);
14322           member = TREE_OPERAND (declarator->u.id.name, 1);
14323
14324           while (scope && CLASS_TYPE_P (scope))
14325             {
14326               /* You're supposed to have one `template <...>'
14327                  for every template class, but you don't need one
14328                  for a full specialization.  For example:
14329
14330                  template <class T> struct S{};
14331                  template <> struct S<int> { void f(); };
14332                  void S<int>::f () {}
14333
14334                  is correct; there shouldn't be a `template <>' for
14335                  the definition of `S<int>::f'.  */
14336               if (CLASSTYPE_TEMPLATE_INFO (scope)
14337                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14338                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14339                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14340                 ++num_templates;
14341
14342               scope = TYPE_CONTEXT (scope);
14343             }
14344         }
14345
14346       /* If the DECLARATOR has the form `X<y>' then it uses one
14347          additional level of template parameters.  */
14348       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14349         ++num_templates;
14350
14351       return cp_parser_check_template_parameters (parser,
14352                                                   num_templates);
14353
14354     case cdk_function:
14355     case cdk_array:
14356     case cdk_pointer:
14357     case cdk_reference:
14358     case cdk_ptrmem:
14359       return (cp_parser_check_declarator_template_parameters
14360               (parser, declarator->declarator));
14361
14362     case cdk_error:
14363       return true;
14364
14365     default:
14366       gcc_unreachable ();
14367     }
14368   return false;
14369 }
14370
14371 /* NUM_TEMPLATES were used in the current declaration.  If that is
14372    invalid, return FALSE and issue an error messages.  Otherwise,
14373    return TRUE.  */
14374
14375 static bool
14376 cp_parser_check_template_parameters (cp_parser* parser,
14377                                      unsigned num_templates)
14378 {
14379   /* If there are more template classes than parameter lists, we have
14380      something like:
14381
14382        template <class T> void S<T>::R<T>::f ();  */
14383   if (parser->num_template_parameter_lists < num_templates)
14384     {
14385       error ("too few template-parameter-lists");
14386       return false;
14387     }
14388   /* If there are the same number of template classes and parameter
14389      lists, that's OK.  */
14390   if (parser->num_template_parameter_lists == num_templates)
14391     return true;
14392   /* If there are more, but only one more, then we are referring to a
14393      member template.  That's OK too.  */
14394   if (parser->num_template_parameter_lists == num_templates + 1)
14395       return true;
14396   /* Otherwise, there are too many template parameter lists.  We have
14397      something like:
14398
14399      template <class T> template <class U> void S::f();  */
14400   error ("too many template-parameter-lists");
14401   return false;
14402 }
14403
14404 /* Parse an optional `::' token indicating that the following name is
14405    from the global namespace.  If so, PARSER->SCOPE is set to the
14406    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14407    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14408    Returns the new value of PARSER->SCOPE, if the `::' token is
14409    present, and NULL_TREE otherwise.  */
14410
14411 static tree
14412 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14413 {
14414   cp_token *token;
14415
14416   /* Peek at the next token.  */
14417   token = cp_lexer_peek_token (parser->lexer);
14418   /* If we're looking at a `::' token then we're starting from the
14419      global namespace, not our current location.  */
14420   if (token->type == CPP_SCOPE)
14421     {
14422       /* Consume the `::' token.  */
14423       cp_lexer_consume_token (parser->lexer);
14424       /* Set the SCOPE so that we know where to start the lookup.  */
14425       parser->scope = global_namespace;
14426       parser->qualifying_scope = global_namespace;
14427       parser->object_scope = NULL_TREE;
14428
14429       return parser->scope;
14430     }
14431   else if (!current_scope_valid_p)
14432     {
14433       parser->scope = NULL_TREE;
14434       parser->qualifying_scope = NULL_TREE;
14435       parser->object_scope = NULL_TREE;
14436     }
14437
14438   return NULL_TREE;
14439 }
14440
14441 /* Returns TRUE if the upcoming token sequence is the start of a
14442    constructor declarator.  If FRIEND_P is true, the declarator is
14443    preceded by the `friend' specifier.  */
14444
14445 static bool
14446 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14447 {
14448   bool constructor_p;
14449   tree type_decl = NULL_TREE;
14450   bool nested_name_p;
14451   cp_token *next_token;
14452
14453   /* The common case is that this is not a constructor declarator, so
14454      try to avoid doing lots of work if at all possible.  It's not
14455      valid declare a constructor at function scope.  */
14456   if (at_function_scope_p ())
14457     return false;
14458   /* And only certain tokens can begin a constructor declarator.  */
14459   next_token = cp_lexer_peek_token (parser->lexer);
14460   if (next_token->type != CPP_NAME
14461       && next_token->type != CPP_SCOPE
14462       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14463       && next_token->type != CPP_TEMPLATE_ID)
14464     return false;
14465
14466   /* Parse tentatively; we are going to roll back all of the tokens
14467      consumed here.  */
14468   cp_parser_parse_tentatively (parser);
14469   /* Assume that we are looking at a constructor declarator.  */
14470   constructor_p = true;
14471
14472   /* Look for the optional `::' operator.  */
14473   cp_parser_global_scope_opt (parser,
14474                               /*current_scope_valid_p=*/false);
14475   /* Look for the nested-name-specifier.  */
14476   nested_name_p
14477     = (cp_parser_nested_name_specifier_opt (parser,
14478                                             /*typename_keyword_p=*/false,
14479                                             /*check_dependency_p=*/false,
14480                                             /*type_p=*/false,
14481                                             /*is_declaration=*/false)
14482        != NULL_TREE);
14483   /* Outside of a class-specifier, there must be a
14484      nested-name-specifier.  */
14485   if (!nested_name_p &&
14486       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14487        || friend_p))
14488     constructor_p = false;
14489   /* If we still think that this might be a constructor-declarator,
14490      look for a class-name.  */
14491   if (constructor_p)
14492     {
14493       /* If we have:
14494
14495            template <typename T> struct S { S(); };
14496            template <typename T> S<T>::S ();
14497
14498          we must recognize that the nested `S' names a class.
14499          Similarly, for:
14500
14501            template <typename T> S<T>::S<T> ();
14502
14503          we must recognize that the nested `S' names a template.  */
14504       type_decl = cp_parser_class_name (parser,
14505                                         /*typename_keyword_p=*/false,
14506                                         /*template_keyword_p=*/false,
14507                                         /*type_p=*/false,
14508                                         /*check_dependency_p=*/false,
14509                                         /*class_head_p=*/false,
14510                                         /*is_declaration=*/false);
14511       /* If there was no class-name, then this is not a constructor.  */
14512       constructor_p = !cp_parser_error_occurred (parser);
14513     }
14514
14515   /* If we're still considering a constructor, we have to see a `(',
14516      to begin the parameter-declaration-clause, followed by either a
14517      `)', an `...', or a decl-specifier.  We need to check for a
14518      type-specifier to avoid being fooled into thinking that:
14519
14520        S::S (f) (int);
14521
14522      is a constructor.  (It is actually a function named `f' that
14523      takes one parameter (of type `int') and returns a value of type
14524      `S::S'.  */
14525   if (constructor_p
14526       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14527     {
14528       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14529           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14530           /* A parameter declaration begins with a decl-specifier,
14531              which is either the "attribute" keyword, a storage class
14532              specifier, or (usually) a type-specifier.  */
14533           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14534           && !cp_parser_storage_class_specifier_opt (parser))
14535         {
14536           tree type;
14537           bool pop_p = false;
14538           unsigned saved_num_template_parameter_lists;
14539
14540           /* Names appearing in the type-specifier should be looked up
14541              in the scope of the class.  */
14542           if (current_class_type)
14543             type = NULL_TREE;
14544           else
14545             {
14546               type = TREE_TYPE (type_decl);
14547               if (TREE_CODE (type) == TYPENAME_TYPE)
14548                 {
14549                   type = resolve_typename_type (type,
14550                                                 /*only_current_p=*/false);
14551                   if (type == error_mark_node)
14552                     {
14553                       cp_parser_abort_tentative_parse (parser);
14554                       return false;
14555                     }
14556                 }
14557               pop_p = push_scope (type);
14558             }
14559
14560           /* Inside the constructor parameter list, surrounding
14561              template-parameter-lists do not apply.  */
14562           saved_num_template_parameter_lists
14563             = parser->num_template_parameter_lists;
14564           parser->num_template_parameter_lists = 0;
14565
14566           /* Look for the type-specifier.  */
14567           cp_parser_type_specifier (parser,
14568                                     CP_PARSER_FLAGS_NONE,
14569                                     /*decl_specs=*/NULL,
14570                                     /*is_declarator=*/true,
14571                                     /*declares_class_or_enum=*/NULL,
14572                                     /*is_cv_qualifier=*/NULL);
14573
14574           parser->num_template_parameter_lists
14575             = saved_num_template_parameter_lists;
14576
14577           /* Leave the scope of the class.  */
14578           if (pop_p)
14579             pop_scope (type);
14580
14581           constructor_p = !cp_parser_error_occurred (parser);
14582         }
14583     }
14584   else
14585     constructor_p = false;
14586   /* We did not really want to consume any tokens.  */
14587   cp_parser_abort_tentative_parse (parser);
14588
14589   return constructor_p;
14590 }
14591
14592 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14593    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14594    they must be performed once we are in the scope of the function.
14595
14596    Returns the function defined.  */
14597
14598 static tree
14599 cp_parser_function_definition_from_specifiers_and_declarator
14600   (cp_parser* parser,
14601    cp_decl_specifier_seq *decl_specifiers,
14602    tree attributes,
14603    const cp_declarator *declarator)
14604 {
14605   tree fn;
14606   bool success_p;
14607
14608   /* Begin the function-definition.  */
14609   success_p = start_function (decl_specifiers, declarator, attributes);
14610
14611   /* The things we're about to see are not directly qualified by any
14612      template headers we've seen thus far.  */
14613   reset_specialization ();
14614
14615   /* If there were names looked up in the decl-specifier-seq that we
14616      did not check, check them now.  We must wait until we are in the
14617      scope of the function to perform the checks, since the function
14618      might be a friend.  */
14619   perform_deferred_access_checks ();
14620
14621   if (!success_p)
14622     {
14623       /* Skip the entire function.  */
14624       error ("invalid function declaration");
14625       cp_parser_skip_to_end_of_block_or_statement (parser);
14626       fn = error_mark_node;
14627     }
14628   else
14629     fn = cp_parser_function_definition_after_declarator (parser,
14630                                                          /*inline_p=*/false);
14631
14632   return fn;
14633 }
14634
14635 /* Parse the part of a function-definition that follows the
14636    declarator.  INLINE_P is TRUE iff this function is an inline
14637    function defined with a class-specifier.
14638
14639    Returns the function defined.  */
14640
14641 static tree
14642 cp_parser_function_definition_after_declarator (cp_parser* parser,
14643                                                 bool inline_p)
14644 {
14645   tree fn;
14646   bool ctor_initializer_p = false;
14647   bool saved_in_unbraced_linkage_specification_p;
14648   unsigned saved_num_template_parameter_lists;
14649
14650   /* If the next token is `return', then the code may be trying to
14651      make use of the "named return value" extension that G++ used to
14652      support.  */
14653   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14654     {
14655       /* Consume the `return' keyword.  */
14656       cp_lexer_consume_token (parser->lexer);
14657       /* Look for the identifier that indicates what value is to be
14658          returned.  */
14659       cp_parser_identifier (parser);
14660       /* Issue an error message.  */
14661       error ("named return values are no longer supported");
14662       /* Skip tokens until we reach the start of the function body.  */
14663       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14664              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14665         cp_lexer_consume_token (parser->lexer);
14666     }
14667   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14668      anything declared inside `f'.  */
14669   saved_in_unbraced_linkage_specification_p
14670     = parser->in_unbraced_linkage_specification_p;
14671   parser->in_unbraced_linkage_specification_p = false;
14672   /* Inside the function, surrounding template-parameter-lists do not
14673      apply.  */
14674   saved_num_template_parameter_lists
14675     = parser->num_template_parameter_lists;
14676   parser->num_template_parameter_lists = 0;
14677   /* If the next token is `try', then we are looking at a
14678      function-try-block.  */
14679   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14680     ctor_initializer_p = cp_parser_function_try_block (parser);
14681   /* A function-try-block includes the function-body, so we only do
14682      this next part if we're not processing a function-try-block.  */
14683   else
14684     ctor_initializer_p
14685       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14686
14687   /* Finish the function.  */
14688   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14689                         (inline_p ? 2 : 0));
14690   /* Generate code for it, if necessary.  */
14691   expand_or_defer_fn (fn);
14692   /* Restore the saved values.  */
14693   parser->in_unbraced_linkage_specification_p
14694     = saved_in_unbraced_linkage_specification_p;
14695   parser->num_template_parameter_lists
14696     = saved_num_template_parameter_lists;
14697
14698   return fn;
14699 }
14700
14701 /* Parse a template-declaration, assuming that the `export' (and
14702    `extern') keywords, if present, has already been scanned.  MEMBER_P
14703    is as for cp_parser_template_declaration.  */
14704
14705 static void
14706 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14707 {
14708   tree decl = NULL_TREE;
14709   tree parameter_list;
14710   bool friend_p = false;
14711
14712   /* Look for the `template' keyword.  */
14713   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14714     return;
14715
14716   /* And the `<'.  */
14717   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14718     return;
14719
14720   /* If the next token is `>', then we have an invalid
14721      specialization.  Rather than complain about an invalid template
14722      parameter, issue an error message here.  */
14723   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14724     {
14725       cp_parser_error (parser, "invalid explicit specialization");
14726       begin_specialization ();
14727       parameter_list = NULL_TREE;
14728     }
14729   else
14730     {
14731       /* Parse the template parameters.  */
14732       begin_template_parm_list ();
14733       parameter_list = cp_parser_template_parameter_list (parser);
14734       parameter_list = end_template_parm_list (parameter_list);
14735     }
14736
14737   /* Look for the `>'.  */
14738   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14739   /* We just processed one more parameter list.  */
14740   ++parser->num_template_parameter_lists;
14741   /* If the next token is `template', there are more template
14742      parameters.  */
14743   if (cp_lexer_next_token_is_keyword (parser->lexer,
14744                                       RID_TEMPLATE))
14745     cp_parser_template_declaration_after_export (parser, member_p);
14746   else
14747     {
14748       /* There are no access checks when parsing a template, as we do not
14749          know if a specialization will be a friend.  */
14750       push_deferring_access_checks (dk_no_check);
14751
14752       decl = cp_parser_single_declaration (parser,
14753                                            member_p,
14754                                            &friend_p);
14755
14756       pop_deferring_access_checks ();
14757
14758       /* If this is a member template declaration, let the front
14759          end know.  */
14760       if (member_p && !friend_p && decl)
14761         {
14762           if (TREE_CODE (decl) == TYPE_DECL)
14763             cp_parser_check_access_in_redeclaration (decl);
14764
14765           decl = finish_member_template_decl (decl);
14766         }
14767       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14768         make_friend_class (current_class_type, TREE_TYPE (decl),
14769                            /*complain=*/true);
14770     }
14771   /* We are done with the current parameter list.  */
14772   --parser->num_template_parameter_lists;
14773
14774   /* Finish up.  */
14775   finish_template_decl (parameter_list);
14776
14777   /* Register member declarations.  */
14778   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14779     finish_member_declaration (decl);
14780
14781   /* If DECL is a function template, we must return to parse it later.
14782      (Even though there is no definition, there might be default
14783      arguments that need handling.)  */
14784   if (member_p && decl
14785       && (TREE_CODE (decl) == FUNCTION_DECL
14786           || DECL_FUNCTION_TEMPLATE_P (decl)))
14787     TREE_VALUE (parser->unparsed_functions_queues)
14788       = tree_cons (NULL_TREE, decl,
14789                    TREE_VALUE (parser->unparsed_functions_queues));
14790 }
14791
14792 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14793    `function-definition' sequence.  MEMBER_P is true, this declaration
14794    appears in a class scope.
14795
14796    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14797    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14798
14799 static tree
14800 cp_parser_single_declaration (cp_parser* parser,
14801                               bool member_p,
14802                               bool* friend_p)
14803 {
14804   int declares_class_or_enum;
14805   tree decl = NULL_TREE;
14806   cp_decl_specifier_seq decl_specifiers;
14807   bool function_definition_p = false;
14808
14809   /* Defer access checks until we know what is being declared.  */
14810   push_deferring_access_checks (dk_deferred);
14811
14812   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14813      alternative.  */
14814   cp_parser_decl_specifier_seq (parser,
14815                                 CP_PARSER_FLAGS_OPTIONAL,
14816                                 &decl_specifiers,
14817                                 &declares_class_or_enum);
14818   if (friend_p)
14819     *friend_p = cp_parser_friend_p (&decl_specifiers);
14820   /* Gather up the access checks that occurred the
14821      decl-specifier-seq.  */
14822   stop_deferring_access_checks ();
14823
14824   /* Check for the declaration of a template class.  */
14825   if (declares_class_or_enum)
14826     {
14827       if (cp_parser_declares_only_class_p (parser))
14828         {
14829           decl = shadow_tag (&decl_specifiers);
14830           if (decl && decl != error_mark_node)
14831             decl = TYPE_NAME (decl);
14832           else
14833             decl = error_mark_node;
14834         }
14835     }
14836   else
14837     decl = NULL_TREE;
14838   /* If it's not a template class, try for a template function.  If
14839      the next token is a `;', then this declaration does not declare
14840      anything.  But, if there were errors in the decl-specifiers, then
14841      the error might well have come from an attempted class-specifier.
14842      In that case, there's no need to warn about a missing declarator.  */
14843   if (!decl
14844       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14845           || decl_specifiers.type != error_mark_node))
14846     decl = cp_parser_init_declarator (parser,
14847                                       &decl_specifiers,
14848                                       /*function_definition_allowed_p=*/true,
14849                                       member_p,
14850                                       declares_class_or_enum,
14851                                       &function_definition_p);
14852
14853   pop_deferring_access_checks ();
14854
14855   /* Clear any current qualification; whatever comes next is the start
14856      of something new.  */
14857   parser->scope = NULL_TREE;
14858   parser->qualifying_scope = NULL_TREE;
14859   parser->object_scope = NULL_TREE;
14860   /* Look for a trailing `;' after the declaration.  */
14861   if (!function_definition_p
14862       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14863     cp_parser_skip_to_end_of_block_or_statement (parser);
14864
14865   return decl;
14866 }
14867
14868 /* Parse a cast-expression that is not the operand of a unary "&".  */
14869
14870 static tree
14871 cp_parser_simple_cast_expression (cp_parser *parser)
14872 {
14873   return cp_parser_cast_expression (parser, /*address_p=*/false);
14874 }
14875
14876 /* Parse a functional cast to TYPE.  Returns an expression
14877    representing the cast.  */
14878
14879 static tree
14880 cp_parser_functional_cast (cp_parser* parser, tree type)
14881 {
14882   tree expression_list;
14883   tree cast;
14884
14885   expression_list
14886     = cp_parser_parenthesized_expression_list (parser, false,
14887                                                /*non_constant_p=*/NULL);
14888
14889   cast = build_functional_cast (type, expression_list);
14890   /* [expr.const]/1: In an integral constant expression "only type
14891      conversions to integral or enumeration type can be used".  */
14892   if (cast != error_mark_node && !type_dependent_expression_p (type)
14893       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14894     {
14895       if (cp_parser_non_integral_constant_expression
14896           (parser, "a call to a constructor"))
14897         return error_mark_node;
14898     }
14899   return cast;
14900 }
14901
14902 /* Save the tokens that make up the body of a member function defined
14903    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14904    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14905    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14906    for the member function.  */
14907
14908 static tree
14909 cp_parser_save_member_function_body (cp_parser* parser,
14910                                      cp_decl_specifier_seq *decl_specifiers,
14911                                      cp_declarator *declarator,
14912                                      tree attributes)
14913 {
14914   cp_token *first;
14915   cp_token *last;
14916   tree fn;
14917
14918   /* Create the function-declaration.  */
14919   fn = start_method (decl_specifiers, declarator, attributes);
14920   /* If something went badly wrong, bail out now.  */
14921   if (fn == error_mark_node)
14922     {
14923       /* If there's a function-body, skip it.  */
14924       if (cp_parser_token_starts_function_definition_p
14925           (cp_lexer_peek_token (parser->lexer)))
14926         cp_parser_skip_to_end_of_block_or_statement (parser);
14927       return error_mark_node;
14928     }
14929
14930   /* Remember it, if there default args to post process.  */
14931   cp_parser_save_default_args (parser, fn);
14932
14933   /* Save away the tokens that make up the body of the
14934      function.  */
14935   first = parser->lexer->next_token;
14936   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14937   /* Handle function try blocks.  */
14938   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14939     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14940   last = parser->lexer->next_token;
14941
14942   /* Save away the inline definition; we will process it when the
14943      class is complete.  */
14944   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14945   DECL_PENDING_INLINE_P (fn) = 1;
14946
14947   /* We need to know that this was defined in the class, so that
14948      friend templates are handled correctly.  */
14949   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14950
14951   /* We're done with the inline definition.  */
14952   finish_method (fn);
14953
14954   /* Add FN to the queue of functions to be parsed later.  */
14955   TREE_VALUE (parser->unparsed_functions_queues)
14956     = tree_cons (NULL_TREE, fn,
14957                  TREE_VALUE (parser->unparsed_functions_queues));
14958
14959   return fn;
14960 }
14961
14962 /* Parse a template-argument-list, as well as the trailing ">" (but
14963    not the opening ">").  See cp_parser_template_argument_list for the
14964    return value.  */
14965
14966 static tree
14967 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14968 {
14969   tree arguments;
14970   tree saved_scope;
14971   tree saved_qualifying_scope;
14972   tree saved_object_scope;
14973   bool saved_greater_than_is_operator_p;
14974
14975   /* [temp.names]
14976
14977      When parsing a template-id, the first non-nested `>' is taken as
14978      the end of the template-argument-list rather than a greater-than
14979      operator.  */
14980   saved_greater_than_is_operator_p
14981     = parser->greater_than_is_operator_p;
14982   parser->greater_than_is_operator_p = false;
14983   /* Parsing the argument list may modify SCOPE, so we save it
14984      here.  */
14985   saved_scope = parser->scope;
14986   saved_qualifying_scope = parser->qualifying_scope;
14987   saved_object_scope = parser->object_scope;
14988   /* Parse the template-argument-list itself.  */
14989   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14990     arguments = NULL_TREE;
14991   else
14992     arguments = cp_parser_template_argument_list (parser);
14993   /* Look for the `>' that ends the template-argument-list. If we find
14994      a '>>' instead, it's probably just a typo.  */
14995   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14996     {
14997       if (!saved_greater_than_is_operator_p)
14998         {
14999           /* If we're in a nested template argument list, the '>>' has
15000             to be a typo for '> >'. We emit the error message, but we
15001             continue parsing and we push a '>' as next token, so that
15002             the argument list will be parsed correctly.  Note that the
15003             global source location is still on the token before the
15004             '>>', so we need to say explicitly where we want it.  */
15005           cp_token *token = cp_lexer_peek_token (parser->lexer);
15006           error ("%H%<>>%> should be %<> >%> "
15007                  "within a nested template argument list",
15008                  &token->location);
15009
15010           /* ??? Proper recovery should terminate two levels of
15011              template argument list here.  */
15012           token->type = CPP_GREATER;
15013         }
15014       else
15015         {
15016           /* If this is not a nested template argument list, the '>>'
15017             is a typo for '>'. Emit an error message and continue.
15018             Same deal about the token location, but here we can get it
15019             right by consuming the '>>' before issuing the diagnostic.  */
15020           cp_lexer_consume_token (parser->lexer);
15021           error ("spurious %<>>%>, use %<>%> to terminate "
15022                  "a template argument list");
15023         }
15024     }
15025   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15026     error ("missing %<>%> to terminate the template argument list");
15027   else
15028     /* It's what we want, a '>'; consume it.  */
15029     cp_lexer_consume_token (parser->lexer);
15030   /* The `>' token might be a greater-than operator again now.  */
15031   parser->greater_than_is_operator_p
15032     = saved_greater_than_is_operator_p;
15033   /* Restore the SAVED_SCOPE.  */
15034   parser->scope = saved_scope;
15035   parser->qualifying_scope = saved_qualifying_scope;
15036   parser->object_scope = saved_object_scope;
15037
15038   return arguments;
15039 }
15040
15041 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15042    arguments, or the body of the function have not yet been parsed,
15043    parse them now.  */
15044
15045 static void
15046 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15047 {
15048   /* If this member is a template, get the underlying
15049      FUNCTION_DECL.  */
15050   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15051     member_function = DECL_TEMPLATE_RESULT (member_function);
15052
15053   /* There should not be any class definitions in progress at this
15054      point; the bodies of members are only parsed outside of all class
15055      definitions.  */
15056   gcc_assert (parser->num_classes_being_defined == 0);
15057   /* While we're parsing the member functions we might encounter more
15058      classes.  We want to handle them right away, but we don't want
15059      them getting mixed up with functions that are currently in the
15060      queue.  */
15061   parser->unparsed_functions_queues
15062     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15063
15064   /* Make sure that any template parameters are in scope.  */
15065   maybe_begin_member_template_processing (member_function);
15066
15067   /* If the body of the function has not yet been parsed, parse it
15068      now.  */
15069   if (DECL_PENDING_INLINE_P (member_function))
15070     {
15071       tree function_scope;
15072       cp_token_cache *tokens;
15073
15074       /* The function is no longer pending; we are processing it.  */
15075       tokens = DECL_PENDING_INLINE_INFO (member_function);
15076       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15077       DECL_PENDING_INLINE_P (member_function) = 0;
15078       /* If this was an inline function in a local class, enter the scope
15079          of the containing function.  */
15080       function_scope = decl_function_context (member_function);
15081       if (function_scope)
15082         push_function_context_to (function_scope);
15083
15084       /* Push the body of the function onto the lexer stack.  */
15085       cp_parser_push_lexer_for_tokens (parser, tokens);
15086
15087       /* Let the front end know that we going to be defining this
15088          function.  */
15089       start_preparsed_function (member_function, NULL_TREE,
15090                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15091
15092       /* Now, parse the body of the function.  */
15093       cp_parser_function_definition_after_declarator (parser,
15094                                                       /*inline_p=*/true);
15095
15096       /* Leave the scope of the containing function.  */
15097       if (function_scope)
15098         pop_function_context_from (function_scope);
15099       cp_parser_pop_lexer (parser);
15100     }
15101
15102   /* Remove any template parameters from the symbol table.  */
15103   maybe_end_member_template_processing ();
15104
15105   /* Restore the queue.  */
15106   parser->unparsed_functions_queues
15107     = TREE_CHAIN (parser->unparsed_functions_queues);
15108 }
15109
15110 /* If DECL contains any default args, remember it on the unparsed
15111    functions queue.  */
15112
15113 static void
15114 cp_parser_save_default_args (cp_parser* parser, tree decl)
15115 {
15116   tree probe;
15117
15118   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15119        probe;
15120        probe = TREE_CHAIN (probe))
15121     if (TREE_PURPOSE (probe))
15122       {
15123         TREE_PURPOSE (parser->unparsed_functions_queues)
15124           = tree_cons (current_class_type, decl,
15125                        TREE_PURPOSE (parser->unparsed_functions_queues));
15126         break;
15127       }
15128   return;
15129 }
15130
15131 /* FN is a FUNCTION_DECL which may contains a parameter with an
15132    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15133    assumes that the current scope is the scope in which the default
15134    argument should be processed.  */
15135
15136 static void
15137 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15138 {
15139   bool saved_local_variables_forbidden_p;
15140   tree parm;
15141
15142   /* While we're parsing the default args, we might (due to the
15143      statement expression extension) encounter more classes.  We want
15144      to handle them right away, but we don't want them getting mixed
15145      up with default args that are currently in the queue.  */
15146   parser->unparsed_functions_queues
15147     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15148
15149   /* Local variable names (and the `this' keyword) may not appear
15150      in a default argument.  */
15151   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15152   parser->local_variables_forbidden_p = true;
15153
15154   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15155        parm;
15156        parm = TREE_CHAIN (parm))
15157     {
15158       cp_token_cache *tokens;
15159
15160       if (!TREE_PURPOSE (parm)
15161           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15162         continue;
15163
15164        /* Push the saved tokens for the default argument onto the parser's
15165           lexer stack.  */
15166       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15167       cp_parser_push_lexer_for_tokens (parser, tokens);
15168
15169       /* Parse the assignment-expression.  */
15170       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15171
15172       /* If the token stream has not been completely used up, then
15173          there was extra junk after the end of the default
15174          argument.  */
15175       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15176         cp_parser_error (parser, "expected `,'");
15177
15178       /* Revert to the main lexer.  */
15179       cp_parser_pop_lexer (parser);
15180     }
15181
15182   /* Restore the state of local_variables_forbidden_p.  */
15183   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15184
15185   /* Restore the queue.  */
15186   parser->unparsed_functions_queues
15187     = TREE_CHAIN (parser->unparsed_functions_queues);
15188 }
15189
15190 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15191    either a TYPE or an expression, depending on the form of the
15192    input.  The KEYWORD indicates which kind of expression we have
15193    encountered.  */
15194
15195 static tree
15196 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15197 {
15198   static const char *format;
15199   tree expr = NULL_TREE;
15200   const char *saved_message;
15201   bool saved_integral_constant_expression_p;
15202
15203   /* Initialize FORMAT the first time we get here.  */
15204   if (!format)
15205     format = "types may not be defined in `%s' expressions";
15206
15207   /* Types cannot be defined in a `sizeof' expression.  Save away the
15208      old message.  */
15209   saved_message = parser->type_definition_forbidden_message;
15210   /* And create the new one.  */
15211   parser->type_definition_forbidden_message
15212     = xmalloc (strlen (format)
15213                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15214                + 1 /* `\0' */);
15215   sprintf ((char *) parser->type_definition_forbidden_message,
15216            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15217
15218   /* The restrictions on constant-expressions do not apply inside
15219      sizeof expressions.  */
15220   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15221   parser->integral_constant_expression_p = false;
15222
15223   /* Do not actually evaluate the expression.  */
15224   ++skip_evaluation;
15225   /* If it's a `(', then we might be looking at the type-id
15226      construction.  */
15227   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15228     {
15229       tree type;
15230       bool saved_in_type_id_in_expr_p;
15231
15232       /* We can't be sure yet whether we're looking at a type-id or an
15233          expression.  */
15234       cp_parser_parse_tentatively (parser);
15235       /* Consume the `('.  */
15236       cp_lexer_consume_token (parser->lexer);
15237       /* Parse the type-id.  */
15238       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15239       parser->in_type_id_in_expr_p = true;
15240       type = cp_parser_type_id (parser);
15241       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15242       /* Now, look for the trailing `)'.  */
15243       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15244       /* If all went well, then we're done.  */
15245       if (cp_parser_parse_definitely (parser))
15246         {
15247           cp_decl_specifier_seq decl_specs;
15248
15249           /* Build a trivial decl-specifier-seq.  */
15250           clear_decl_specs (&decl_specs);
15251           decl_specs.type = type;
15252
15253           /* Call grokdeclarator to figure out what type this is.  */
15254           expr = grokdeclarator (NULL,
15255                                  &decl_specs,
15256                                  TYPENAME,
15257                                  /*initialized=*/0,
15258                                  /*attrlist=*/NULL);
15259         }
15260     }
15261
15262   /* If the type-id production did not work out, then we must be
15263      looking at the unary-expression production.  */
15264   if (!expr)
15265     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15266   /* Go back to evaluating expressions.  */
15267   --skip_evaluation;
15268
15269   /* Free the message we created.  */
15270   free ((char *) parser->type_definition_forbidden_message);
15271   /* And restore the old one.  */
15272   parser->type_definition_forbidden_message = saved_message;
15273   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15274
15275   return expr;
15276 }
15277
15278 /* If the current declaration has no declarator, return true.  */
15279
15280 static bool
15281 cp_parser_declares_only_class_p (cp_parser *parser)
15282 {
15283   /* If the next token is a `;' or a `,' then there is no
15284      declarator.  */
15285   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15286           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15287 }
15288
15289 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15290
15291 static void
15292 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15293                              cp_storage_class storage_class)
15294 {
15295   if (decl_specs->storage_class != sc_none)
15296     decl_specs->multiple_storage_classes_p = true;
15297   else
15298     decl_specs->storage_class = storage_class;
15299 }
15300
15301 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15302    is true, the type is a user-defined type; otherwise it is a
15303    built-in type specified by a keyword.  */
15304
15305 static void
15306 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15307                               tree type_spec,
15308                               bool user_defined_p)
15309 {
15310   decl_specs->any_specifiers_p = true;
15311
15312   /* If the user tries to redeclare a built-in type (with, for example,
15313      in "typedef int wchar_t;") we remember that this is what
15314      happened.  In system headers, we ignore these declarations so
15315      that G++ can work with system headers that are not C++-safe.  */
15316   if (decl_specs->specs[(int) ds_typedef]
15317       && !user_defined_p
15318       && (decl_specs->type
15319           || decl_specs->specs[(int) ds_long]
15320           || decl_specs->specs[(int) ds_short]
15321           || decl_specs->specs[(int) ds_unsigned]
15322           || decl_specs->specs[(int) ds_signed]))
15323     {
15324       decl_specs->redefined_builtin_type = type_spec;
15325       if (!decl_specs->type)
15326         {
15327           decl_specs->type = type_spec;
15328           decl_specs->user_defined_type_p = false;
15329         }
15330     }
15331   else if (decl_specs->type)
15332     decl_specs->multiple_types_p = true;
15333   else
15334     {
15335       decl_specs->type = type_spec;
15336       decl_specs->user_defined_type_p = user_defined_p;
15337       decl_specs->redefined_builtin_type = NULL_TREE;
15338     }
15339 }
15340
15341 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15342    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15343
15344 static bool
15345 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15346 {
15347   return decl_specifiers->specs[(int) ds_friend] != 0;
15348 }
15349
15350 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15351    issue an error message indicating that TOKEN_DESC was expected.
15352
15353    Returns the token consumed, if the token had the appropriate type.
15354    Otherwise, returns NULL.  */
15355
15356 static cp_token *
15357 cp_parser_require (cp_parser* parser,
15358                    enum cpp_ttype type,
15359                    const char* token_desc)
15360 {
15361   if (cp_lexer_next_token_is (parser->lexer, type))
15362     return cp_lexer_consume_token (parser->lexer);
15363   else
15364     {
15365       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15366       if (!cp_parser_simulate_error (parser))
15367         {
15368           char *message = concat ("expected ", token_desc, NULL);
15369           cp_parser_error (parser, message);
15370           free (message);
15371         }
15372       return NULL;
15373     }
15374 }
15375
15376 /* Like cp_parser_require, except that tokens will be skipped until
15377    the desired token is found.  An error message is still produced if
15378    the next token is not as expected.  */
15379
15380 static void
15381 cp_parser_skip_until_found (cp_parser* parser,
15382                             enum cpp_ttype type,
15383                             const char* token_desc)
15384 {
15385   cp_token *token;
15386   unsigned nesting_depth = 0;
15387
15388   if (cp_parser_require (parser, type, token_desc))
15389     return;
15390
15391   /* Skip tokens until the desired token is found.  */
15392   while (true)
15393     {
15394       /* Peek at the next token.  */
15395       token = cp_lexer_peek_token (parser->lexer);
15396       /* If we've reached the token we want, consume it and
15397          stop.  */
15398       if (token->type == type && !nesting_depth)
15399         {
15400           cp_lexer_consume_token (parser->lexer);
15401           return;
15402         }
15403       /* If we've run out of tokens, stop.  */
15404       if (token->type == CPP_EOF)
15405         return;
15406       if (token->type == CPP_OPEN_BRACE
15407           || token->type == CPP_OPEN_PAREN
15408           || token->type == CPP_OPEN_SQUARE)
15409         ++nesting_depth;
15410       else if (token->type == CPP_CLOSE_BRACE
15411                || token->type == CPP_CLOSE_PAREN
15412                || token->type == CPP_CLOSE_SQUARE)
15413         {
15414           if (nesting_depth-- == 0)
15415             return;
15416         }
15417       /* Consume this token.  */
15418       cp_lexer_consume_token (parser->lexer);
15419     }
15420 }
15421
15422 /* If the next token is the indicated keyword, consume it.  Otherwise,
15423    issue an error message indicating that TOKEN_DESC was expected.
15424
15425    Returns the token consumed, if the token had the appropriate type.
15426    Otherwise, returns NULL.  */
15427
15428 static cp_token *
15429 cp_parser_require_keyword (cp_parser* parser,
15430                            enum rid keyword,
15431                            const char* token_desc)
15432 {
15433   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15434
15435   if (token && token->keyword != keyword)
15436     {
15437       dyn_string_t error_msg;
15438
15439       /* Format the error message.  */
15440       error_msg = dyn_string_new (0);
15441       dyn_string_append_cstr (error_msg, "expected ");
15442       dyn_string_append_cstr (error_msg, token_desc);
15443       cp_parser_error (parser, error_msg->s);
15444       dyn_string_delete (error_msg);
15445       return NULL;
15446     }
15447
15448   return token;
15449 }
15450
15451 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15452    function-definition.  */
15453
15454 static bool
15455 cp_parser_token_starts_function_definition_p (cp_token* token)
15456 {
15457   return (/* An ordinary function-body begins with an `{'.  */
15458           token->type == CPP_OPEN_BRACE
15459           /* A ctor-initializer begins with a `:'.  */
15460           || token->type == CPP_COLON
15461           /* A function-try-block begins with `try'.  */
15462           || token->keyword == RID_TRY
15463           /* The named return value extension begins with `return'.  */
15464           || token->keyword == RID_RETURN);
15465 }
15466
15467 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15468    definition.  */
15469
15470 static bool
15471 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15472 {
15473   cp_token *token;
15474
15475   token = cp_lexer_peek_token (parser->lexer);
15476   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15477 }
15478
15479 /* Returns TRUE iff the next token is the "," or ">" ending a
15480    template-argument. ">>" is also accepted (after the full
15481    argument was parsed) because it's probably a typo for "> >",
15482    and there is a specific diagnostic for this.  */
15483
15484 static bool
15485 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15486 {
15487   cp_token *token;
15488
15489   token = cp_lexer_peek_token (parser->lexer);
15490   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15491           || token->type == CPP_RSHIFT);
15492 }
15493
15494 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15495    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15496
15497 static bool
15498 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15499                                                      size_t n)
15500 {
15501   cp_token *token;
15502
15503   token = cp_lexer_peek_nth_token (parser->lexer, n);
15504   if (token->type == CPP_LESS)
15505     return true;
15506   /* Check for the sequence `<::' in the original code. It would be lexed as
15507      `[:', where `[' is a digraph, and there is no whitespace before
15508      `:'.  */
15509   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15510     {
15511       cp_token *token2;
15512       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15513       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15514         return true;
15515     }
15516   return false;
15517 }
15518
15519 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15520    or none_type otherwise.  */
15521
15522 static enum tag_types
15523 cp_parser_token_is_class_key (cp_token* token)
15524 {
15525   switch (token->keyword)
15526     {
15527     case RID_CLASS:
15528       return class_type;
15529     case RID_STRUCT:
15530       return record_type;
15531     case RID_UNION:
15532       return union_type;
15533
15534     default:
15535       return none_type;
15536     }
15537 }
15538
15539 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15540
15541 static void
15542 cp_parser_check_class_key (enum tag_types class_key, tree type)
15543 {
15544   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15545     pedwarn ("`%s' tag used in naming `%#T'",
15546             class_key == union_type ? "union"
15547              : class_key == record_type ? "struct" : "class",
15548              type);
15549 }
15550
15551 /* Issue an error message if DECL is redeclared with different
15552    access than its original declaration [class.access.spec/3].
15553    This applies to nested classes and nested class templates.
15554    [class.mem/1].  */
15555
15556 static void cp_parser_check_access_in_redeclaration (tree decl)
15557 {
15558   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15559     return;
15560
15561   if ((TREE_PRIVATE (decl)
15562        != (current_access_specifier == access_private_node))
15563       || (TREE_PROTECTED (decl)
15564           != (current_access_specifier == access_protected_node)))
15565     error ("%D redeclared with different access", decl);
15566 }
15567
15568 /* Look for the `template' keyword, as a syntactic disambiguator.
15569    Return TRUE iff it is present, in which case it will be
15570    consumed.  */
15571
15572 static bool
15573 cp_parser_optional_template_keyword (cp_parser *parser)
15574 {
15575   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15576     {
15577       /* The `template' keyword can only be used within templates;
15578          outside templates the parser can always figure out what is a
15579          template and what is not.  */
15580       if (!processing_template_decl)
15581         {
15582           error ("`template' (as a disambiguator) is only allowed "
15583                  "within templates");
15584           /* If this part of the token stream is rescanned, the same
15585              error message would be generated.  So, we purge the token
15586              from the stream.  */
15587           cp_lexer_purge_token (parser->lexer);
15588           return false;
15589         }
15590       else
15591         {
15592           /* Consume the `template' keyword.  */
15593           cp_lexer_consume_token (parser->lexer);
15594           return true;
15595         }
15596     }
15597
15598   return false;
15599 }
15600
15601 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15602    set PARSER->SCOPE, and perform other related actions.  */
15603
15604 static void
15605 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15606 {
15607   tree value;
15608   tree check;
15609
15610   /* Get the stored value.  */
15611   value = cp_lexer_consume_token (parser->lexer)->value;
15612   /* Perform any access checks that were deferred.  */
15613   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15614     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15615   /* Set the scope from the stored value.  */
15616   parser->scope = TREE_VALUE (value);
15617   parser->qualifying_scope = TREE_TYPE (value);
15618   parser->object_scope = NULL_TREE;
15619 }
15620
15621 /* Consume tokens up through a non-nested END token. */
15622
15623 static void
15624 cp_parser_cache_group (cp_parser *parser,
15625                        enum cpp_ttype end,
15626                        unsigned depth)
15627 {
15628   while (true)
15629     {
15630       cp_token *token;
15631
15632       /* Abort a parenthesized expression if we encounter a brace.  */
15633       if ((end == CPP_CLOSE_PAREN || depth == 0)
15634           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15635         return;
15636       /* If we've reached the end of the file, stop.  */
15637       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15638         return;
15639       /* Consume the next token.  */
15640       token = cp_lexer_consume_token (parser->lexer);
15641       /* See if it starts a new group.  */
15642       if (token->type == CPP_OPEN_BRACE)
15643         {
15644           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15645           if (depth == 0)
15646             return;
15647         }
15648       else if (token->type == CPP_OPEN_PAREN)
15649         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15650       else if (token->type == end)
15651         return;
15652     }
15653 }
15654
15655 /* Begin parsing tentatively.  We always save tokens while parsing
15656    tentatively so that if the tentative parsing fails we can restore the
15657    tokens.  */
15658
15659 static void
15660 cp_parser_parse_tentatively (cp_parser* parser)
15661 {
15662   /* Enter a new parsing context.  */
15663   parser->context = cp_parser_context_new (parser->context);
15664   /* Begin saving tokens.  */
15665   cp_lexer_save_tokens (parser->lexer);
15666   /* In order to avoid repetitive access control error messages,
15667      access checks are queued up until we are no longer parsing
15668      tentatively.  */
15669   push_deferring_access_checks (dk_deferred);
15670 }
15671
15672 /* Commit to the currently active tentative parse.  */
15673
15674 static void
15675 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15676 {
15677   cp_parser_context *context;
15678   cp_lexer *lexer;
15679
15680   /* Mark all of the levels as committed.  */
15681   lexer = parser->lexer;
15682   for (context = parser->context; context->next; context = context->next)
15683     {
15684       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15685         break;
15686       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15687       while (!cp_lexer_saving_tokens (lexer))
15688         lexer = lexer->next;
15689       cp_lexer_commit_tokens (lexer);
15690     }
15691 }
15692
15693 /* Abort the currently active tentative parse.  All consumed tokens
15694    will be rolled back, and no diagnostics will be issued.  */
15695
15696 static void
15697 cp_parser_abort_tentative_parse (cp_parser* parser)
15698 {
15699   cp_parser_simulate_error (parser);
15700   /* Now, pretend that we want to see if the construct was
15701      successfully parsed.  */
15702   cp_parser_parse_definitely (parser);
15703 }
15704
15705 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15706    token stream.  Otherwise, commit to the tokens we have consumed.
15707    Returns true if no error occurred; false otherwise.  */
15708
15709 static bool
15710 cp_parser_parse_definitely (cp_parser* parser)
15711 {
15712   bool error_occurred;
15713   cp_parser_context *context;
15714
15715   /* Remember whether or not an error occurred, since we are about to
15716      destroy that information.  */
15717   error_occurred = cp_parser_error_occurred (parser);
15718   /* Remove the topmost context from the stack.  */
15719   context = parser->context;
15720   parser->context = context->next;
15721   /* If no parse errors occurred, commit to the tentative parse.  */
15722   if (!error_occurred)
15723     {
15724       /* Commit to the tokens read tentatively, unless that was
15725          already done.  */
15726       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15727         cp_lexer_commit_tokens (parser->lexer);
15728
15729       pop_to_parent_deferring_access_checks ();
15730     }
15731   /* Otherwise, if errors occurred, roll back our state so that things
15732      are just as they were before we began the tentative parse.  */
15733   else
15734     {
15735       cp_lexer_rollback_tokens (parser->lexer);
15736       pop_deferring_access_checks ();
15737     }
15738   /* Add the context to the front of the free list.  */
15739   context->next = cp_parser_context_free_list;
15740   cp_parser_context_free_list = context;
15741
15742   return !error_occurred;
15743 }
15744
15745 /* Returns true if we are parsing tentatively -- but have decided that
15746    we will stick with this tentative parse, even if errors occur.  */
15747
15748 static bool
15749 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15750 {
15751   return (cp_parser_parsing_tentatively (parser)
15752           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15753 }
15754
15755 /* Returns nonzero iff an error has occurred during the most recent
15756    tentative parse.  */
15757
15758 static bool
15759 cp_parser_error_occurred (cp_parser* parser)
15760 {
15761   return (cp_parser_parsing_tentatively (parser)
15762           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15763 }
15764
15765 /* Returns nonzero if GNU extensions are allowed.  */
15766
15767 static bool
15768 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15769 {
15770   return parser->allow_gnu_extensions_p;
15771 }
15772
15773 \f
15774 /* The parser.  */
15775
15776 static GTY (()) cp_parser *the_parser;
15777
15778 /* External interface.  */
15779
15780 /* Parse one entire translation unit.  */
15781
15782 void
15783 c_parse_file (void)
15784 {
15785   bool error_occurred;
15786   static bool already_called = false;
15787
15788   if (already_called)
15789     {
15790       sorry ("inter-module optimizations not implemented for C++");
15791       return;
15792     }
15793   already_called = true;
15794
15795   the_parser = cp_parser_new ();
15796   push_deferring_access_checks (flag_access_control
15797                                 ? dk_no_deferred : dk_no_check);
15798   error_occurred = cp_parser_translation_unit (the_parser);
15799   the_parser = NULL;
15800 }
15801
15802 /* This variable must be provided by every front end.  */
15803
15804 int yydebug;
15805
15806 #include "gt-cp-parser.h"