OSDN Git Service

2004-10-19 Paolo Bonzini <bonzini@gnu.org>
[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   /* True if this token is from a context where it is implicitly extern "C" */
59   BOOL_BITFIELD implicit_extern_c : 1;
60   /* The value associated with this token, if any.  */
61   tree value;
62   /* The location at which this token was found.  */
63   location_t location;
64 } cp_token;
65
66 /* The cp_lexer structure represents the C++ lexer.  It is responsible
67    for managing the token stream from the preprocessor and supplying
68    it to the parser.  Tokens are never added to the cp_lexer after
69    it is created. */
70
71 typedef struct cp_lexer GTY (())
72 {
73   /* The memory allocated for the buffer.  Never NULL.  */
74   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
75   /* A pointer just past the end of the memory allocated for the buffer.  */
76   cp_token * GTY ((skip)) buffer_end;
77   /* A pointer just past the last available token.  The tokens
78      in this lexer are [buffer, last_token). */
79   cp_token * GTY ((skip)) last_token;
80
81   /* The next available token.  If NEXT_TOKEN is NULL, then there are
82      no more available tokens.  */
83   cp_token * GTY ((skip)) next_token;
84
85   /* A stack indicating positions at which cp_lexer_save_tokens was
86      called.  The top entry is the most recent position at which we
87      began saving tokens.  The entries are differences in token
88      position between BUFFER and the first saved token.
89      If the stack is non-empty, we are saving tokens.  */
90   varray_type saved_tokens;
91
92   /* True if we should output debugging information.  */
93   bool debugging_p;
94
95   /* The next lexer in a linked list of lexers.  */
96   struct cp_lexer *next;
97 } cp_lexer;
98
99 /* cp_token_cache is a range of tokens.  There is no need to represent
100    allocate heap memory for it, since tokens are never removed from the
101    lexer's array.  There is also no need for the GC to walk through
102    a cp_token_cache, since everything in here is referenced through
103    a lexer. */
104
105 typedef struct cp_token_cache GTY(())
106 {
107   /* The beginning of the token range. */
108   cp_token * GTY((skip)) first;
109
110   /* Points immediately after the last token in the range. */
111   cp_token * GTY ((skip)) last;
112 } cp_token_cache;
113
114 /* Prototypes.  */
115
116 static cp_lexer *cp_lexer_new_main
117   (void);
118 static cp_lexer *cp_lexer_new_from_tokens
119   (cp_token_cache *tokens);
120 static void cp_lexer_destroy
121   (cp_lexer *);
122 static int cp_lexer_saving_tokens
123   (const cp_lexer *);
124 static cp_token *cp_lexer_next_token
125   (cp_lexer *, cp_token *);
126 static cp_token *cp_lexer_prev_token
127   (cp_lexer *, cp_token *);
128 static ptrdiff_t cp_lexer_token_difference
129   (cp_lexer *, cp_token *, cp_token *);
130 static void cp_lexer_grow_buffer
131   (cp_lexer *);
132 static void cp_lexer_get_preprocessor_token
133   (cp_lexer *, cp_token *);
134 static inline cp_token *cp_lexer_peek_token
135   (cp_lexer *);
136 static cp_token *cp_lexer_peek_nth_token
137   (cp_lexer *, size_t);
138 static inline bool cp_lexer_next_token_is
139   (cp_lexer *, enum cpp_ttype);
140 static bool cp_lexer_next_token_is_not
141   (cp_lexer *, enum cpp_ttype);
142 static bool cp_lexer_next_token_is_keyword
143   (cp_lexer *, enum rid);
144 static cp_token *cp_lexer_consume_token
145   (cp_lexer *);
146 static void cp_lexer_purge_token
147   (cp_lexer *);
148 static void cp_lexer_purge_tokens_after
149   (cp_lexer *, cp_token *);
150 static void cp_lexer_handle_pragma
151   (cp_lexer *);
152 static void cp_lexer_save_tokens
153   (cp_lexer *);
154 static void cp_lexer_commit_tokens
155   (cp_lexer *);
156 static void cp_lexer_rollback_tokens
157   (cp_lexer *);
158 #ifdef ENABLE_CHECKING
159 static void cp_lexer_print_token
160   (FILE *, cp_token *);
161 static inline bool cp_lexer_debugging_p
162   (cp_lexer *);
163 static void cp_lexer_start_debugging
164   (cp_lexer *) ATTRIBUTE_UNUSED;
165 static void cp_lexer_stop_debugging
166   (cp_lexer *) ATTRIBUTE_UNUSED;
167 static void cp_lexer_peek_token_emit_debug_info
168   (cp_lexer *, cp_token *);
169 #else
170 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
171    about passing NULL to functions that require non-NULL arguments
172    (fputs, fprintf).  It will never be used, so all we need is a value
173    of the right type that's guaranteed not to be NULL.  */
174 #define cp_lexer_debug_stream stdout
175 #define cp_lexer_print_token(str, tok) (void) 0
176 #define cp_lexer_debugging_p(lexer) 0
177 #define cp_lexer_peek_token_emit_debug_info(lexer, tok) (void) 0
178 #endif /* ENABLE_CHECKING */
179
180 static cp_token_cache *cp_token_cache_new
181   (cp_token *, cp_token *);
182
183 /* Manifest constants.  */
184
185 #define CP_LEXER_BUFFER_SIZE 10000
186 #define CP_SAVED_TOKENS_SIZE 5
187
188 /* A token type for keywords, as opposed to ordinary identifiers.  */
189 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
190
191 /* A token type for template-ids.  If a template-id is processed while
192    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
193    the value of the CPP_TEMPLATE_ID is whatever was returned by
194    cp_parser_template_id.  */
195 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
196
197 /* A token type for nested-name-specifiers.  If a
198    nested-name-specifier is processed while parsing tentatively, it is
199    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
200    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
201    cp_parser_nested_name_specifier_opt.  */
202 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
203
204 /* A token type for tokens that are not tokens at all; these are used
205    to represent slots in the array where there used to be a token
206    that has now been deleted. */
207 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
208
209 /* The number of token types, including C++-specific ones.  */
210 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
211
212 /* Variables.  */
213
214 #ifdef ENABLE_CHECKING
215 /* The stream to which debugging output should be written.  */
216 static FILE *cp_lexer_debug_stream;
217 #endif /* ENABLE_CHECKING */
218
219 /* Create a new main C++ lexer, the lexer that gets tokens from the
220    preprocessor.  */
221
222 static cp_lexer *
223 cp_lexer_new_main (void)
224 {
225   cp_lexer *lexer;
226   cp_token first_token;
227
228   /* Tell cpplib we want CPP_PRAGMA tokens. */
229   cpp_get_options (parse_in)->defer_pragmas = true;
230
231   /* Tell c_lex not to merge string constants.  */
232   c_lex_return_raw_strings = true;
233
234   /* It's possible that lexing the first token will load a PCH file,
235      which is a GC collection point.  So we have to grab the first
236      token before allocating any memory.  */
237   cp_lexer_get_preprocessor_token (NULL, &first_token);
238   c_common_no_more_pch ();
239
240   /* Allocate the memory.  */
241   lexer = GGC_CNEW (cp_lexer);
242
243   /* Create the buffer.  */
244   lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
245   lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
246  
247   /* There is one token in the buffer.  */
248   lexer->last_token = lexer->buffer + 1;
249   lexer->next_token = lexer->buffer;
250   *lexer->next_token = first_token;
251
252   /* Create the SAVED_TOKENS stack.  */
253   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
254
255 #ifdef ENABLE_CHECKING  
256   /* Initially we are not debugging.  */
257   lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
259
260   /* Get the rest of the tokens from the preprocessor. */
261   while (lexer->last_token[-1].type != CPP_EOF)
262     {
263       if (lexer->last_token == lexer->buffer_end)
264         cp_lexer_grow_buffer (lexer);
265       cp_lexer_get_preprocessor_token (lexer, lexer->last_token++);
266     }
267
268   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
269      direct calls to c_lex.  Those callers all expect c_lex to do
270      string constant concatenation.  */
271   c_lex_return_raw_strings = false;
272
273   gcc_assert (lexer->next_token->type != CPP_PURGED);
274   return lexer;
275 }
276
277 /* Create a new lexer whose token stream is primed with the tokens in
278    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
279
280 static cp_lexer *
281 cp_lexer_new_from_tokens (cp_token_cache *cache)
282 {
283   cp_token *first = cache->first;
284   cp_token *last = cache->last;
285   cp_lexer *lexer = GGC_CNEW (cp_lexer);
286   cp_token *eof;
287
288   /* Allocate a new buffer.  The reason we do this is to make sure
289      there's a CPP_EOF token at the end.  An alternative would be to
290      modify cp_lexer_peek_token so that it checks for end-of-buffer
291      and returns a CPP_EOF when appropriate. */
292
293   lexer->buffer = GGC_NEWVEC (cp_token, (last - first) + 1);
294   memcpy (lexer->buffer, first, sizeof (cp_token) * (last - first));
295   lexer->next_token = lexer->buffer;
296   lexer->buffer_end = lexer->last_token = lexer->buffer + (last - first);
297
298   eof = lexer->buffer + (last - first);
299   eof->type = CPP_EOF;
300   eof->location = UNKNOWN_LOCATION;
301   eof->value = NULL_TREE;
302   eof->keyword = RID_MAX;
303
304   /* Create the SAVED_TOKENS stack.  */
305   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
306
307 #ifdef ENABLE_CHECKING
308   /* Initially we are not debugging.  */
309   lexer->debugging_p = false;
310 #endif
311
312   gcc_assert (lexer->next_token->type != CPP_PURGED);
313   return lexer;
314 }
315
316 /* Frees all resources associated with LEXER. */
317
318 static void
319 cp_lexer_destroy (cp_lexer *lexer)
320 {
321   ggc_free (lexer->buffer);
322   ggc_free (lexer);
323 }
324
325 /* Returns nonzero if debugging information should be output.  */
326
327 #ifdef ENABLE_CHECKING
328
329 static inline bool
330 cp_lexer_debugging_p (cp_lexer *lexer)
331 {
332   return lexer->debugging_p;
333 }
334
335 #endif /* ENABLE_CHECKING */
336
337 /* TOKEN points into the circular token buffer.  Return a pointer to
338    the next token in the buffer.  */
339
340 static inline cp_token *
341 cp_lexer_next_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
342 {
343   token++;
344   return token;
345 }
346
347 /* TOKEN points into the circular token buffer.  Return a pointer to
348    the previous token in the buffer.  */
349
350 static inline cp_token *
351 cp_lexer_prev_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
352 {
353   return token - 1;
354 }
355
356 /* nonzero if we are presently saving tokens.  */
357
358 static int
359 cp_lexer_saving_tokens (const cp_lexer* lexer)
360 {
361   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
362 }
363
364 /* Return a pointer to the token that is N tokens beyond TOKEN in the
365    buffer.  */
366
367 static inline cp_token *
368 cp_lexer_advance_token (cp_lexer *lexer ATTRIBUTE_UNUSED,
369                         cp_token *token, ptrdiff_t n)
370 {
371   return token + n;
372 }
373
374 /* Returns the number of times that START would have to be incremented
375    to reach FINISH.  If START and FINISH are the same, returns zero.  */
376
377 static inline ptrdiff_t
378 cp_lexer_token_difference (cp_lexer* lexer ATTRIBUTE_UNUSED,
379                            cp_token* start, cp_token* finish)
380 {
381   return finish - start;
382 }
383
384 /* If the buffer is full, make it bigger.  */
385 static void
386 cp_lexer_grow_buffer (cp_lexer* lexer)
387 {
388   cp_token *old_buffer;
389   cp_token *new_buffer;
390   ptrdiff_t buffer_length;
391
392   /* This function should only be called when buffer is full. */
393   gcc_assert (lexer->last_token == lexer->buffer_end);
394
395   /* Remember the current buffer pointer.  It will become invalid,
396      but we will need to do pointer arithmetic involving this
397      value.  */
398   old_buffer = lexer->buffer;
399   /* Compute the current buffer size.  */
400   buffer_length = lexer->buffer_end - lexer->buffer;
401   /* Allocate a buffer twice as big.  */
402   new_buffer = ggc_realloc (lexer->buffer,
403                             2 * buffer_length * sizeof (cp_token));
404
405   /* Recompute buffer positions. */
406   lexer->buffer = new_buffer;
407   lexer->buffer_end = new_buffer + 2 * buffer_length;
408   lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
409   lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
410
411   /* Clear the rest of the buffer.  We never look at this storage,
412      but the garbage collector may.  */
413   memset (lexer->last_token, 0,
414           (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
415 }
416
417 /* Store the next token from the preprocessor in *TOKEN.  */
418
419 static void
420 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
421                                  cp_token *token)
422 {
423   static int is_extern_c = 0;
424   bool done;
425
426   done = false;
427   /* Keep going until we get a token we like.  */
428   while (!done)
429     {
430       /* Get a new token from the preprocessor.  */
431       token->type = c_lex_with_flags (&token->value, &token->flags);
432       /* Issue messages about tokens we cannot process.  */
433       switch (token->type)
434         {
435         case CPP_ATSIGN:
436         case CPP_HASH:
437         case CPP_PASTE:
438           error ("invalid token");
439           break;
440
441         default:
442           /* This is a good token, so we exit the loop.  */
443           done = true;
444           break;
445         }
446     }
447   /* Now we've got our token.  */
448   token->location = input_location;
449   token->in_system_header = in_system_header;
450
451   /* On some systems, some header files are surrounded by an 
452      implicit extern "C" block.  Set a flag in the token if it
453      comes from such a header. */
454   is_extern_c += pending_lang_change;
455   pending_lang_change = 0;
456   token->implicit_extern_c = is_extern_c > 0;
457
458   /* Check to see if this token is a keyword.  */
459   if (token->type == CPP_NAME
460       && C_IS_RESERVED_WORD (token->value))
461     {
462       /* Mark this token as a keyword.  */
463       token->type = CPP_KEYWORD;
464       /* Record which keyword.  */
465       token->keyword = C_RID_CODE (token->value);
466       /* Update the value.  Some keywords are mapped to particular
467          entities, rather than simply having the value of the
468          corresponding IDENTIFIER_NODE.  For example, `__const' is
469          mapped to `const'.  */
470       token->value = ridpointers[token->keyword];
471     }
472   else
473     token->keyword = RID_MAX;
474 }
475
476 /* Update the globals input_location and in_system_header from TOKEN.   */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
479 {
480   if (token->type != CPP_EOF)
481     {
482       input_location = token->location;
483       in_system_header = token->in_system_header;
484     }
485 }
486
487 /* Return a pointer to the next token in the token stream, but do not
488    consume it.  */
489
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
492 {
493   if (cp_lexer_debugging_p (lexer))
494     cp_lexer_peek_token_emit_debug_info (lexer, lexer->next_token);
495   return lexer->next_token;
496 }
497
498 #ifdef ENABLE_CHECKING
499 /* Emit debug output for cp_lexer_peek_token.  Split out into a
500    separate function so that cp_lexer_peek_token can be small and
501    inlinable. */
502
503 static void
504 cp_lexer_peek_token_emit_debug_info (cp_lexer *lexer ATTRIBUTE_UNUSED,
505                                      cp_token *token ATTRIBUTE_UNUSED)
506 {
507   fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
508   cp_lexer_print_token (cp_lexer_debug_stream, token);
509   putc ('\n', cp_lexer_debug_stream);
510 }
511 #endif
512
513 /* Return true if the next token has the indicated TYPE.  */
514
515 static inline bool
516 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
517 {
518   return cp_lexer_peek_token (lexer)->type == type;
519 }
520
521 /* Return true if the next token does not have the indicated TYPE.  */
522
523 static inline bool
524 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
525 {
526   return !cp_lexer_next_token_is (lexer, type);
527 }
528
529 /* Return true if the next token is the indicated KEYWORD.  */
530
531 static inline bool
532 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
533 {
534   cp_token *token;
535
536   /* Peek at the next token.  */
537   token = cp_lexer_peek_token (lexer);
538   /* Check to see if it is the indicated keyword.  */
539   return token->keyword == keyword;
540 }
541
542 /* Return a pointer to the Nth token in the token stream.  If N is 1,
543    then this is precisely equivalent to cp_lexer_peek_token (except
544    that it is not inline).  One would like to disallow that case, but
545    there is one case (cp_parser_nth_token_starts_template_id) where
546    the caller passes a variable for N and it might be 1.  */
547
548 static cp_token *
549 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
550 {
551   cp_token *token;
552
553   /* N is 1-based, not zero-based.  */
554   gcc_assert (n > 0);
555
556   if (cp_lexer_debugging_p (lexer))
557     fprintf (cp_lexer_debug_stream,
558              "cp_lexer: peeking ahead %ld at token: ", (long)n);
559
560   --n;
561   token = lexer->next_token;
562   while (n != 0)
563     {
564       ++token;
565       if (token->type != CPP_PURGED)
566         --n;
567     }
568
569   if (cp_lexer_debugging_p (lexer))
570     {
571       cp_lexer_print_token (cp_lexer_debug_stream, token);
572       putc ('\n', cp_lexer_debug_stream);
573     }
574
575   return token;
576 }
577
578 /* Return the next token, and advance the lexer's next_token pointer
579    to point to the next non-purged token.  */
580
581 static cp_token *
582 cp_lexer_consume_token (cp_lexer* lexer)
583 {
584   cp_token *token = lexer->next_token;
585
586   do
587     ++lexer->next_token;
588   while (lexer->next_token->type == CPP_PURGED);
589
590   cp_lexer_set_source_position_from_token (token);
591
592   /* Provide debugging output.  */
593   if (cp_lexer_debugging_p (lexer))
594     {
595       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
596       cp_lexer_print_token (cp_lexer_debug_stream, token);
597       putc ('\n', cp_lexer_debug_stream);
598     }
599
600   return token;
601 }
602
603 /* Permanently remove the next token from the token stream, and
604    advance the next_token pointer to refer to the next non-purged
605    token.  */
606
607 static void
608 cp_lexer_purge_token (cp_lexer *lexer)
609 {
610   cp_token *tok = lexer->next_token;
611   tok->type = CPP_PURGED;
612   tok->location = UNKNOWN_LOCATION;
613   tok->value = NULL_TREE;
614   tok->keyword = RID_MAX;
615
616   do
617     ++lexer->next_token;
618   while (lexer->next_token->type == CPP_PURGED);
619 }
620
621 /* Permanently remove all tokens after TOK, up to, but not
622    including, the token that will be returned next by
623    cp_lexer_peek_token.  */
624
625 static void
626 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
627 {
628   cp_token *peek;
629
630   peek = cp_lexer_peek_token (lexer);
631   gcc_assert (tok < peek);
632
633   for ( tok += 1; tok != peek; tok += 1)
634     {
635       tok->type = CPP_PURGED;
636       tok->location = UNKNOWN_LOCATION;
637       tok->value = NULL_TREE;
638       tok->keyword = RID_MAX;
639     }
640 }
641
642 /* Consume and handle a pragma token.   */
643 static void
644 cp_lexer_handle_pragma (cp_lexer *lexer)
645 {
646   cpp_string s;
647   cp_token *token = cp_lexer_consume_token (lexer);
648   gcc_assert (token->type == CPP_PRAGMA);
649   gcc_assert (token->value);
650
651   s.len = TREE_STRING_LENGTH (token->value);
652   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
653
654   cpp_handle_deferred_pragma (parse_in, &s);
655
656   /* Clearing token->value here means that we will get an ICE if we
657      try to process this #pragma again (which should be impossible).  */
658   token->value = NULL;
659 }
660
661 /* Begin saving tokens.  All tokens consumed after this point will be
662    preserved.  */
663
664 static void
665 cp_lexer_save_tokens (cp_lexer* lexer)
666 {
667   /* Provide debugging output.  */
668   if (cp_lexer_debugging_p (lexer))
669     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
670
671   VARRAY_PUSH_INT (lexer->saved_tokens,
672                    cp_lexer_token_difference (lexer,
673                                               lexer->buffer,
674                                               lexer->next_token));
675 }
676
677 /* Commit to the portion of the token stream most recently saved.  */
678
679 static void
680 cp_lexer_commit_tokens (cp_lexer* lexer)
681 {
682   /* Provide debugging output.  */
683   if (cp_lexer_debugging_p (lexer))
684     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
685
686   VARRAY_POP (lexer->saved_tokens);
687 }
688
689 /* Return all tokens saved since the last call to cp_lexer_save_tokens
690    to the token stream.  Stop saving tokens.  */
691
692 static void
693 cp_lexer_rollback_tokens (cp_lexer* lexer)
694 {
695   size_t delta;
696
697   /* Provide debugging output.  */
698   if (cp_lexer_debugging_p (lexer))
699     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
700
701   /* Find the token that was the NEXT_TOKEN when we started saving
702      tokens.  */
703   delta = VARRAY_TOP_INT(lexer->saved_tokens);
704   /* Make it the next token again now.  */
705   lexer->next_token = cp_lexer_advance_token (lexer, lexer->buffer, delta);
706
707   /* Stop saving tokens.  */
708   VARRAY_POP (lexer->saved_tokens);
709 }
710
711 /* Print a representation of the TOKEN on the STREAM.  */
712
713 #ifdef ENABLE_CHECKING
714
715 static void
716 cp_lexer_print_token (FILE * stream, cp_token *token)
717 {
718   /* We don't use cpp_type2name here because the parser defines
719      a few tokens of its own.  */
720   static const char *const token_names[] = {
721     /* cpplib-defined token types */
722 #define OP(e, s) #e,
723 #define TK(e, s) #e,
724     TTYPE_TABLE
725 #undef OP
726 #undef TK
727     /* C++ parser token types - see "Manifest constants", above.  */
728     "KEYWORD",
729     "TEMPLATE_ID",
730     "NESTED_NAME_SPECIFIER",
731     "PURGED"
732   };
733   
734   /* If we have a name for the token, print it out.  Otherwise, we
735      simply give the numeric code.  */
736   gcc_assert (token->type < ARRAY_SIZE(token_names));
737   fputs (token_names[token->type], stream);
738
739   /* For some tokens, print the associated data.  */
740   switch (token->type)
741     {
742     case CPP_KEYWORD:
743       /* Some keywords have a value that is not an IDENTIFIER_NODE.
744          For example, `struct' is mapped to an INTEGER_CST.  */
745       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
746         break;
747       /* else fall through */
748     case CPP_NAME:
749       fputs (IDENTIFIER_POINTER (token->value), stream);
750       break;
751
752     case CPP_STRING:
753     case CPP_WSTRING:
754     case CPP_PRAGMA:
755       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
756       break;
757
758     default:
759       break;
760     }
761 }
762
763 /* Start emitting debugging information.  */
764
765 static void
766 cp_lexer_start_debugging (cp_lexer* lexer)
767 {
768   ++lexer->debugging_p;
769 }
770
771 /* Stop emitting debugging information.  */
772
773 static void
774 cp_lexer_stop_debugging (cp_lexer* lexer)
775 {
776   --lexer->debugging_p;
777 }
778
779 #endif /* ENABLE_CHECKING */
780
781 /* Create a new cp_token_cache, representing a range of tokens. */
782
783 static cp_token_cache *
784 cp_token_cache_new (cp_token *first, cp_token *last)
785 {
786   cp_token_cache *cache = GGC_NEW (cp_token_cache);
787   cache->first = first;
788   cache->last = last;
789   return cache;
790 }
791
792 \f
793 /* Decl-specifiers.  */
794
795 static void clear_decl_specs
796   (cp_decl_specifier_seq *);
797
798 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
799
800 static void
801 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
802 {
803   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
804 }
805
806 /* Declarators.  */
807
808 /* Nothing other than the parser should be creating declarators;
809    declarators are a semi-syntactic representation of C++ entities.
810    Other parts of the front end that need to create entities (like
811    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
812
813 static cp_declarator *make_id_declarator
814   (tree);
815 static cp_declarator *make_call_declarator
816   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
817 static cp_declarator *make_array_declarator
818   (cp_declarator *, tree);
819 static cp_declarator *make_pointer_declarator
820   (cp_cv_quals, cp_declarator *);
821 static cp_declarator *make_reference_declarator
822   (cp_cv_quals, cp_declarator *);
823 static cp_parameter_declarator *make_parameter_declarator
824   (cp_decl_specifier_seq *, cp_declarator *, tree);
825 static cp_declarator *make_ptrmem_declarator
826   (cp_cv_quals, tree, cp_declarator *);
827
828 cp_declarator *cp_error_declarator;
829
830 /* The obstack on which declarators and related data structures are
831    allocated.  */
832 static struct obstack declarator_obstack;
833
834 /* Alloc BYTES from the declarator memory pool.  */
835
836 static inline void *
837 alloc_declarator (size_t bytes)
838 {
839   return obstack_alloc (&declarator_obstack, bytes);
840 }
841
842 /* Allocate a declarator of the indicated KIND.  Clear fields that are
843    common to all declarators.  */
844
845 static cp_declarator *
846 make_declarator (cp_declarator_kind kind)
847 {
848   cp_declarator *declarator;
849
850   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
851   declarator->kind = kind;
852   declarator->attributes = NULL_TREE;
853   declarator->declarator = NULL;
854
855   return declarator;
856 }
857
858 /* Make a declarator for a generalized identifier.  */
859
860 cp_declarator *
861 make_id_declarator (tree id)
862 {
863   cp_declarator *declarator;
864
865   declarator = make_declarator (cdk_id);
866   declarator->u.id.name = id;
867   declarator->u.id.sfk = sfk_none;
868
869   return declarator;
870 }
871
872 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
873    of modifiers such as const or volatile to apply to the pointer
874    type, represented as identifiers.  */
875
876 cp_declarator *
877 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
878 {
879   cp_declarator *declarator;
880
881   declarator = make_declarator (cdk_pointer);
882   declarator->declarator = target;
883   declarator->u.pointer.qualifiers = cv_qualifiers;
884   declarator->u.pointer.class_type = NULL_TREE;
885
886   return declarator;
887 }
888
889 /* Like make_pointer_declarator -- but for references.  */
890
891 cp_declarator *
892 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
893 {
894   cp_declarator *declarator;
895
896   declarator = make_declarator (cdk_reference);
897   declarator->declarator = target;
898   declarator->u.pointer.qualifiers = cv_qualifiers;
899   declarator->u.pointer.class_type = NULL_TREE;
900
901   return declarator;
902 }
903
904 /* Like make_pointer_declarator -- but for a pointer to a non-static
905    member of CLASS_TYPE.  */
906
907 cp_declarator *
908 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
909                         cp_declarator *pointee)
910 {
911   cp_declarator *declarator;
912
913   declarator = make_declarator (cdk_ptrmem);
914   declarator->declarator = pointee;
915   declarator->u.pointer.qualifiers = cv_qualifiers;
916   declarator->u.pointer.class_type = class_type;
917
918   return declarator;
919 }
920
921 /* Make a declarator for the function given by TARGET, with the
922    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
923    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
924    indicates what exceptions can be thrown.  */
925
926 cp_declarator *
927 make_call_declarator (cp_declarator *target,
928                       cp_parameter_declarator *parms,
929                       cp_cv_quals cv_qualifiers,
930                       tree exception_specification)
931 {
932   cp_declarator *declarator;
933
934   declarator = make_declarator (cdk_function);
935   declarator->declarator = target;
936   declarator->u.function.parameters = parms;
937   declarator->u.function.qualifiers = cv_qualifiers;
938   declarator->u.function.exception_specification = exception_specification;
939
940   return declarator;
941 }
942
943 /* Make a declarator for an array of BOUNDS elements, each of which is
944    defined by ELEMENT.  */
945
946 cp_declarator *
947 make_array_declarator (cp_declarator *element, tree bounds)
948 {
949   cp_declarator *declarator;
950
951   declarator = make_declarator (cdk_array);
952   declarator->declarator = element;
953   declarator->u.array.bounds = bounds;
954
955   return declarator;
956 }
957
958 cp_parameter_declarator *no_parameters;
959
960 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
961    DECLARATOR and DEFAULT_ARGUMENT.  */
962
963 cp_parameter_declarator *
964 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
965                            cp_declarator *declarator,
966                            tree default_argument)
967 {
968   cp_parameter_declarator *parameter;
969
970   parameter = ((cp_parameter_declarator *)
971                alloc_declarator (sizeof (cp_parameter_declarator)));
972   parameter->next = NULL;
973   if (decl_specifiers)
974     parameter->decl_specifiers = *decl_specifiers;
975   else
976     clear_decl_specs (&parameter->decl_specifiers);
977   parameter->declarator = declarator;
978   parameter->default_argument = default_argument;
979   parameter->ellipsis_p = false;
980
981   return parameter;
982 }
983
984 /* The parser.  */
985
986 /* Overview
987    --------
988
989    A cp_parser parses the token stream as specified by the C++
990    grammar.  Its job is purely parsing, not semantic analysis.  For
991    example, the parser breaks the token stream into declarators,
992    expressions, statements, and other similar syntactic constructs.
993    It does not check that the types of the expressions on either side
994    of an assignment-statement are compatible, or that a function is
995    not declared with a parameter of type `void'.
996
997    The parser invokes routines elsewhere in the compiler to perform
998    semantic analysis and to build up the abstract syntax tree for the
999    code processed.
1000
1001    The parser (and the template instantiation code, which is, in a
1002    way, a close relative of parsing) are the only parts of the
1003    compiler that should be calling push_scope and pop_scope, or
1004    related functions.  The parser (and template instantiation code)
1005    keeps track of what scope is presently active; everything else
1006    should simply honor that.  (The code that generates static
1007    initializers may also need to set the scope, in order to check
1008    access control correctly when emitting the initializers.)
1009
1010    Methodology
1011    -----------
1012
1013    The parser is of the standard recursive-descent variety.  Upcoming
1014    tokens in the token stream are examined in order to determine which
1015    production to use when parsing a non-terminal.  Some C++ constructs
1016    require arbitrary look ahead to disambiguate.  For example, it is
1017    impossible, in the general case, to tell whether a statement is an
1018    expression or declaration without scanning the entire statement.
1019    Therefore, the parser is capable of "parsing tentatively."  When the
1020    parser is not sure what construct comes next, it enters this mode.
1021    Then, while we attempt to parse the construct, the parser queues up
1022    error messages, rather than issuing them immediately, and saves the
1023    tokens it consumes.  If the construct is parsed successfully, the
1024    parser "commits", i.e., it issues any queued error messages and
1025    the tokens that were being preserved are permanently discarded.
1026    If, however, the construct is not parsed successfully, the parser
1027    rolls back its state completely so that it can resume parsing using
1028    a different alternative.
1029
1030    Future Improvements
1031    -------------------
1032
1033    The performance of the parser could probably be improved substantially.
1034    We could often eliminate the need to parse tentatively by looking ahead
1035    a little bit.  In some places, this approach might not entirely eliminate
1036    the need to parse tentatively, but it might still speed up the average
1037    case.  */
1038
1039 /* Flags that are passed to some parsing functions.  These values can
1040    be bitwise-ored together.  */
1041
1042 typedef enum cp_parser_flags
1043 {
1044   /* No flags.  */
1045   CP_PARSER_FLAGS_NONE = 0x0,
1046   /* The construct is optional.  If it is not present, then no error
1047      should be issued.  */
1048   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1049   /* When parsing a type-specifier, do not allow user-defined types.  */
1050   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1051 } cp_parser_flags;
1052
1053 /* The different kinds of declarators we want to parse.  */
1054
1055 typedef enum cp_parser_declarator_kind
1056 {
1057   /* We want an abstract declarator.  */
1058   CP_PARSER_DECLARATOR_ABSTRACT,
1059   /* We want a named declarator.  */
1060   CP_PARSER_DECLARATOR_NAMED,
1061   /* We don't mind, but the name must be an unqualified-id.  */
1062   CP_PARSER_DECLARATOR_EITHER
1063 } cp_parser_declarator_kind;
1064
1065 /* The precedence values used to parse binary expressions.  The minimum value
1066    of PREC must be 1, because zero is reserved to quickly discriminate
1067    binary operators from other tokens.  */
1068
1069 enum cp_parser_prec
1070 {
1071   PREC_NOT_OPERATOR,
1072   PREC_LOGICAL_OR_EXPRESSION,
1073   PREC_LOGICAL_AND_EXPRESSION,
1074   PREC_INCLUSIVE_OR_EXPRESSION,
1075   PREC_EXCLUSIVE_OR_EXPRESSION,
1076   PREC_AND_EXPRESSION,
1077   PREC_EQUALITY_EXPRESSION,
1078   PREC_RELATIONAL_EXPRESSION,
1079   PREC_SHIFT_EXPRESSION,
1080   PREC_ADDITIVE_EXPRESSION,
1081   PREC_MULTIPLICATIVE_EXPRESSION,
1082   PREC_PM_EXPRESSION,
1083   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1084 };
1085
1086 /* A mapping from a token type to a corresponding tree node type, with a
1087    precedence value.  */
1088
1089 typedef struct cp_parser_binary_operations_map_node
1090 {
1091   /* The token type.  */
1092   enum cpp_ttype token_type;
1093   /* The corresponding tree code.  */
1094   enum tree_code tree_type;
1095   /* The precedence of this operator.  */
1096   enum cp_parser_prec prec;
1097 } cp_parser_binary_operations_map_node;
1098
1099 /* The status of a tentative parse.  */
1100
1101 typedef enum cp_parser_status_kind
1102 {
1103   /* No errors have occurred.  */
1104   CP_PARSER_STATUS_KIND_NO_ERROR,
1105   /* An error has occurred.  */
1106   CP_PARSER_STATUS_KIND_ERROR,
1107   /* We are committed to this tentative parse, whether or not an error
1108      has occurred.  */
1109   CP_PARSER_STATUS_KIND_COMMITTED
1110 } cp_parser_status_kind;
1111
1112 typedef struct cp_parser_expression_stack_entry
1113 {
1114   tree lhs;
1115   enum tree_code tree_type;
1116   int prec;
1117 } cp_parser_expression_stack_entry;
1118
1119 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1120    entries because precedence levels on the stack are monotonically
1121    increasing.  */
1122 typedef struct cp_parser_expression_stack_entry
1123   cp_parser_expression_stack[NUM_PREC_VALUES];
1124
1125 /* Context that is saved and restored when parsing tentatively.  */
1126 typedef struct cp_parser_context GTY (())
1127 {
1128   /* If this is a tentative parsing context, the status of the
1129      tentative parse.  */
1130   enum cp_parser_status_kind status;
1131   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1132      that are looked up in this context must be looked up both in the
1133      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1134      the context of the containing expression.  */
1135   tree object_type;
1136
1137   /* The next parsing context in the stack.  */
1138   struct cp_parser_context *next;
1139 } cp_parser_context;
1140
1141 /* Prototypes.  */
1142
1143 /* Constructors and destructors.  */
1144
1145 static cp_parser_context *cp_parser_context_new
1146   (cp_parser_context *);
1147
1148 /* Class variables.  */
1149
1150 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1151
1152 /* The operator-precedence table used by cp_parser_binary_expression.
1153    Transformed into an associative array (binops_by_token) by
1154    cp_parser_new.  */
1155
1156 static const cp_parser_binary_operations_map_node binops[] = {
1157   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1158   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1159
1160   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1161   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1162   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1163
1164   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1165   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1166
1167   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1168   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1169
1170   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1171   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1172   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1173   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1174   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1175   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1176
1177   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1178   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1179
1180   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1181
1182   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1183
1184   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1185
1186   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1187
1188   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1189 };
1190
1191 /* The same as binops, but initialized by cp_parser_new so that
1192    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1193    for speed.  */
1194 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1195
1196 /* Constructors and destructors.  */
1197
1198 /* Construct a new context.  The context below this one on the stack
1199    is given by NEXT.  */
1200
1201 static cp_parser_context *
1202 cp_parser_context_new (cp_parser_context* next)
1203 {
1204   cp_parser_context *context;
1205
1206   /* Allocate the storage.  */
1207   if (cp_parser_context_free_list != NULL)
1208     {
1209       /* Pull the first entry from the free list.  */
1210       context = cp_parser_context_free_list;
1211       cp_parser_context_free_list = context->next;
1212       memset (context, 0, sizeof (*context));
1213     }
1214   else
1215     context = GGC_CNEW (cp_parser_context);
1216
1217   /* No errors have occurred yet in this context.  */
1218   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1219   /* If this is not the bottomost context, copy information that we
1220      need from the previous context.  */
1221   if (next)
1222     {
1223       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224          expression, then we are parsing one in this context, too.  */
1225       context->object_type = next->object_type;
1226       /* Thread the stack.  */
1227       context->next = next;
1228     }
1229
1230   return context;
1231 }
1232
1233 /* The cp_parser structure represents the C++ parser.  */
1234
1235 typedef struct cp_parser GTY(())
1236 {
1237   /* The lexer from which we are obtaining tokens.  */
1238   cp_lexer *lexer;
1239
1240   /* The scope in which names should be looked up.  If NULL_TREE, then
1241      we look up names in the scope that is currently open in the
1242      source program.  If non-NULL, this is either a TYPE or
1243      NAMESPACE_DECL for the scope in which we should look.
1244
1245      This value is not cleared automatically after a name is looked
1246      up, so we must be careful to clear it before starting a new look
1247      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1248      will look up `Z' in the scope of `X', rather than the current
1249      scope.)  Unfortunately, it is difficult to tell when name lookup
1250      is complete, because we sometimes peek at a token, look it up,
1251      and then decide not to consume it.  */
1252   tree scope;
1253
1254   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255      last lookup took place.  OBJECT_SCOPE is used if an expression
1256      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257      respectively.  QUALIFYING_SCOPE is used for an expression of the
1258      form "X::Y"; it refers to X.  */
1259   tree object_scope;
1260   tree qualifying_scope;
1261
1262   /* A stack of parsing contexts.  All but the bottom entry on the
1263      stack will be tentative contexts.
1264
1265      We parse tentatively in order to determine which construct is in
1266      use in some situations.  For example, in order to determine
1267      whether a statement is an expression-statement or a
1268      declaration-statement we parse it tentatively as a
1269      declaration-statement.  If that fails, we then reparse the same
1270      token stream as an expression-statement.  */
1271   cp_parser_context *context;
1272
1273   /* True if we are parsing GNU C++.  If this flag is not set, then
1274      GNU extensions are not recognized.  */
1275   bool allow_gnu_extensions_p;
1276
1277   /* TRUE if the `>' token should be interpreted as the greater-than
1278      operator.  FALSE if it is the end of a template-id or
1279      template-parameter-list.  */
1280   bool greater_than_is_operator_p;
1281
1282   /* TRUE if default arguments are allowed within a parameter list
1283      that starts at this point. FALSE if only a gnu extension makes
1284      them permissible.  */
1285   bool default_arg_ok_p;
1286
1287   /* TRUE if we are parsing an integral constant-expression.  See
1288      [expr.const] for a precise definition.  */
1289   bool integral_constant_expression_p;
1290
1291   /* TRUE if we are parsing an integral constant-expression -- but a
1292      non-constant expression should be permitted as well.  This flag
1293      is used when parsing an array bound so that GNU variable-length
1294      arrays are tolerated.  */
1295   bool allow_non_integral_constant_expression_p;
1296
1297   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1298      been seen that makes the expression non-constant.  */
1299   bool non_integral_constant_expression_p;
1300
1301   /* TRUE if local variable names and `this' are forbidden in the
1302      current context.  */
1303   bool local_variables_forbidden_p;
1304
1305   /* TRUE if the declaration we are parsing is part of a
1306      linkage-specification of the form `extern string-literal
1307      declaration'.  */
1308   bool in_unbraced_linkage_specification_p;
1309
1310   /* TRUE if we are presently parsing a declarator, after the
1311      direct-declarator.  */
1312   bool in_declarator_p;
1313
1314   /* TRUE if we are presently parsing a template-argument-list.  */
1315   bool in_template_argument_list_p;
1316
1317   /* TRUE if we are presently parsing the body of an
1318      iteration-statement.  */
1319   bool in_iteration_statement_p;
1320
1321   /* TRUE if we are presently parsing the body of a switch
1322      statement.  */
1323   bool in_switch_statement_p;
1324
1325   /* TRUE if we are parsing a type-id in an expression context.  In
1326      such a situation, both "type (expr)" and "type (type)" are valid
1327      alternatives.  */
1328   bool in_type_id_in_expr_p;
1329
1330   /* TRUE if we are currently in a header file where declarations are
1331      implicitly extern "C". */
1332   bool implicit_extern_c;
1333
1334   /* TRUE if strings in expressions should be translated to the execution
1335      character set.  */
1336   bool translate_strings_p;
1337
1338   /* If non-NULL, then we are parsing a construct where new type
1339      definitions are not permitted.  The string stored here will be
1340      issued as an error message if a type is defined.  */
1341   const char *type_definition_forbidden_message;
1342
1343   /* A list of lists. The outer list is a stack, used for member
1344      functions of local classes. At each level there are two sub-list,
1345      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1346      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1347      TREE_VALUE's. The functions are chained in reverse declaration
1348      order.
1349
1350      The TREE_PURPOSE sublist contains those functions with default
1351      arguments that need post processing, and the TREE_VALUE sublist
1352      contains those functions with definitions that need post
1353      processing.
1354
1355      These lists can only be processed once the outermost class being
1356      defined is complete.  */
1357   tree unparsed_functions_queues;
1358
1359   /* The number of classes whose definitions are currently in
1360      progress.  */
1361   unsigned num_classes_being_defined;
1362
1363   /* The number of template parameter lists that apply directly to the
1364      current declaration.  */
1365   unsigned num_template_parameter_lists;
1366 } cp_parser;
1367
1368 /* The type of a function that parses some kind of expression.  */
1369 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1370
1371 /* Prototypes.  */
1372
1373 /* Constructors and destructors.  */
1374
1375 static cp_parser *cp_parser_new
1376   (void);
1377
1378 /* Routines to parse various constructs.
1379
1380    Those that return `tree' will return the error_mark_node (rather
1381    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1382    Sometimes, they will return an ordinary node if error-recovery was
1383    attempted, even though a parse error occurred.  So, to check
1384    whether or not a parse error occurred, you should always use
1385    cp_parser_error_occurred.  If the construct is optional (indicated
1386    either by an `_opt' in the name of the function that does the
1387    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1388    the construct is not present.  */
1389
1390 /* Lexical conventions [gram.lex]  */
1391
1392 static tree cp_parser_identifier
1393   (cp_parser *);
1394 static tree cp_parser_string_literal
1395   (cp_parser *, bool, bool);
1396
1397 /* Basic concepts [gram.basic]  */
1398
1399 static bool cp_parser_translation_unit
1400   (cp_parser *);
1401
1402 /* Expressions [gram.expr]  */
1403
1404 static tree cp_parser_primary_expression
1405   (cp_parser *, cp_id_kind *, tree *);
1406 static tree cp_parser_id_expression
1407   (cp_parser *, bool, bool, bool *, bool);
1408 static tree cp_parser_unqualified_id
1409   (cp_parser *, bool, bool, bool);
1410 static tree cp_parser_nested_name_specifier_opt
1411   (cp_parser *, bool, bool, bool, bool);
1412 static tree cp_parser_nested_name_specifier
1413   (cp_parser *, bool, bool, bool, bool);
1414 static tree cp_parser_class_or_namespace_name
1415   (cp_parser *, bool, bool, bool, bool, bool);
1416 static tree cp_parser_postfix_expression
1417   (cp_parser *, bool);
1418 static tree cp_parser_postfix_open_square_expression
1419   (cp_parser *, tree, bool);
1420 static tree cp_parser_postfix_dot_deref_expression
1421   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1422 static tree cp_parser_parenthesized_expression_list
1423   (cp_parser *, bool, bool *);
1424 static void cp_parser_pseudo_destructor_name
1425   (cp_parser *, tree *, tree *);
1426 static tree cp_parser_unary_expression
1427   (cp_parser *, bool);
1428 static enum tree_code cp_parser_unary_operator
1429   (cp_token *);
1430 static tree cp_parser_new_expression
1431   (cp_parser *);
1432 static tree cp_parser_new_placement
1433   (cp_parser *);
1434 static tree cp_parser_new_type_id
1435   (cp_parser *, tree *);
1436 static cp_declarator *cp_parser_new_declarator_opt
1437   (cp_parser *);
1438 static cp_declarator *cp_parser_direct_new_declarator
1439   (cp_parser *);
1440 static tree cp_parser_new_initializer
1441   (cp_parser *);
1442 static tree cp_parser_delete_expression
1443   (cp_parser *);
1444 static tree cp_parser_cast_expression
1445   (cp_parser *, bool);
1446 static tree cp_parser_binary_expression
1447   (cp_parser *);
1448 static tree cp_parser_question_colon_clause
1449   (cp_parser *, tree);
1450 static tree cp_parser_assignment_expression
1451   (cp_parser *);
1452 static enum tree_code cp_parser_assignment_operator_opt
1453   (cp_parser *);
1454 static tree cp_parser_expression
1455   (cp_parser *);
1456 static tree cp_parser_constant_expression
1457   (cp_parser *, bool, bool *);
1458 static tree cp_parser_builtin_offsetof
1459   (cp_parser *);
1460
1461 /* Statements [gram.stmt.stmt]  */
1462
1463 static void cp_parser_statement
1464   (cp_parser *, tree);
1465 static tree cp_parser_labeled_statement
1466   (cp_parser *, tree);
1467 static tree cp_parser_expression_statement
1468   (cp_parser *, tree);
1469 static tree cp_parser_compound_statement
1470   (cp_parser *, tree, bool);
1471 static void cp_parser_statement_seq_opt
1472   (cp_parser *, tree);
1473 static tree cp_parser_selection_statement
1474   (cp_parser *);
1475 static tree cp_parser_condition
1476   (cp_parser *);
1477 static tree cp_parser_iteration_statement
1478   (cp_parser *);
1479 static void cp_parser_for_init_statement
1480   (cp_parser *);
1481 static tree cp_parser_jump_statement
1482   (cp_parser *);
1483 static void cp_parser_declaration_statement
1484   (cp_parser *);
1485
1486 static tree cp_parser_implicitly_scoped_statement
1487   (cp_parser *);
1488 static void cp_parser_already_scoped_statement
1489   (cp_parser *);
1490
1491 /* Declarations [gram.dcl.dcl] */
1492
1493 static void cp_parser_declaration_seq_opt
1494   (cp_parser *);
1495 static void cp_parser_declaration
1496   (cp_parser *);
1497 static void cp_parser_block_declaration
1498   (cp_parser *, bool);
1499 static void cp_parser_simple_declaration
1500   (cp_parser *, bool);
1501 static void cp_parser_decl_specifier_seq
1502   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1503 static tree cp_parser_storage_class_specifier_opt
1504   (cp_parser *);
1505 static tree cp_parser_function_specifier_opt
1506   (cp_parser *, cp_decl_specifier_seq *);
1507 static tree cp_parser_type_specifier
1508   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1509    int *, bool *);
1510 static tree cp_parser_simple_type_specifier
1511   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1512 static tree cp_parser_type_name
1513   (cp_parser *);
1514 static tree cp_parser_elaborated_type_specifier
1515   (cp_parser *, bool, bool);
1516 static tree cp_parser_enum_specifier
1517   (cp_parser *);
1518 static void cp_parser_enumerator_list
1519   (cp_parser *, tree);
1520 static void cp_parser_enumerator_definition
1521   (cp_parser *, tree);
1522 static tree cp_parser_namespace_name
1523   (cp_parser *);
1524 static void cp_parser_namespace_definition
1525   (cp_parser *);
1526 static void cp_parser_namespace_body
1527   (cp_parser *);
1528 static tree cp_parser_qualified_namespace_specifier
1529   (cp_parser *);
1530 static void cp_parser_namespace_alias_definition
1531   (cp_parser *);
1532 static void cp_parser_using_declaration
1533   (cp_parser *);
1534 static void cp_parser_using_directive
1535   (cp_parser *);
1536 static void cp_parser_asm_definition
1537   (cp_parser *);
1538 static void cp_parser_linkage_specification
1539   (cp_parser *);
1540
1541 /* Declarators [gram.dcl.decl] */
1542
1543 static tree cp_parser_init_declarator
1544   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1545 static cp_declarator *cp_parser_declarator
1546   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1547 static cp_declarator *cp_parser_direct_declarator
1548   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1549 static enum tree_code cp_parser_ptr_operator
1550   (cp_parser *, tree *, cp_cv_quals *);
1551 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1552   (cp_parser *);
1553 static tree cp_parser_declarator_id
1554   (cp_parser *);
1555 static tree cp_parser_type_id
1556   (cp_parser *);
1557 static void cp_parser_type_specifier_seq
1558   (cp_parser *, cp_decl_specifier_seq *);
1559 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1560   (cp_parser *);
1561 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1562   (cp_parser *, bool *);
1563 static cp_parameter_declarator *cp_parser_parameter_declaration
1564   (cp_parser *, bool, bool *);
1565 static void cp_parser_function_body
1566   (cp_parser *);
1567 static tree cp_parser_initializer
1568   (cp_parser *, bool *, bool *);
1569 static tree cp_parser_initializer_clause
1570   (cp_parser *, bool *);
1571 static tree cp_parser_initializer_list
1572   (cp_parser *, bool *);
1573
1574 static bool cp_parser_ctor_initializer_opt_and_function_body
1575   (cp_parser *);
1576
1577 /* Classes [gram.class] */
1578
1579 static tree cp_parser_class_name
1580   (cp_parser *, bool, bool, bool, bool, bool, bool);
1581 static tree cp_parser_class_specifier
1582   (cp_parser *);
1583 static tree cp_parser_class_head
1584   (cp_parser *, bool *, tree *);
1585 static enum tag_types cp_parser_class_key
1586   (cp_parser *);
1587 static void cp_parser_member_specification_opt
1588   (cp_parser *);
1589 static void cp_parser_member_declaration
1590   (cp_parser *);
1591 static tree cp_parser_pure_specifier
1592   (cp_parser *);
1593 static tree cp_parser_constant_initializer
1594   (cp_parser *);
1595
1596 /* Derived classes [gram.class.derived] */
1597
1598 static tree cp_parser_base_clause
1599   (cp_parser *);
1600 static tree cp_parser_base_specifier
1601   (cp_parser *);
1602
1603 /* Special member functions [gram.special] */
1604
1605 static tree cp_parser_conversion_function_id
1606   (cp_parser *);
1607 static tree cp_parser_conversion_type_id
1608   (cp_parser *);
1609 static cp_declarator *cp_parser_conversion_declarator_opt
1610   (cp_parser *);
1611 static bool cp_parser_ctor_initializer_opt
1612   (cp_parser *);
1613 static void cp_parser_mem_initializer_list
1614   (cp_parser *);
1615 static tree cp_parser_mem_initializer
1616   (cp_parser *);
1617 static tree cp_parser_mem_initializer_id
1618   (cp_parser *);
1619
1620 /* Overloading [gram.over] */
1621
1622 static tree cp_parser_operator_function_id
1623   (cp_parser *);
1624 static tree cp_parser_operator
1625   (cp_parser *);
1626
1627 /* Templates [gram.temp] */
1628
1629 static void cp_parser_template_declaration
1630   (cp_parser *, bool);
1631 static tree cp_parser_template_parameter_list
1632   (cp_parser *);
1633 static tree cp_parser_template_parameter
1634   (cp_parser *, bool *);
1635 static tree cp_parser_type_parameter
1636   (cp_parser *);
1637 static tree cp_parser_template_id
1638   (cp_parser *, bool, bool, bool);
1639 static tree cp_parser_template_name
1640   (cp_parser *, bool, bool, bool, bool *);
1641 static tree cp_parser_template_argument_list
1642   (cp_parser *);
1643 static tree cp_parser_template_argument
1644   (cp_parser *);
1645 static void cp_parser_explicit_instantiation
1646   (cp_parser *);
1647 static void cp_parser_explicit_specialization
1648   (cp_parser *);
1649
1650 /* Exception handling [gram.exception] */
1651
1652 static tree cp_parser_try_block
1653   (cp_parser *);
1654 static bool cp_parser_function_try_block
1655   (cp_parser *);
1656 static void cp_parser_handler_seq
1657   (cp_parser *);
1658 static void cp_parser_handler
1659   (cp_parser *);
1660 static tree cp_parser_exception_declaration
1661   (cp_parser *);
1662 static tree cp_parser_throw_expression
1663   (cp_parser *);
1664 static tree cp_parser_exception_specification_opt
1665   (cp_parser *);
1666 static tree cp_parser_type_id_list
1667   (cp_parser *);
1668
1669 /* GNU Extensions */
1670
1671 static tree cp_parser_asm_specification_opt
1672   (cp_parser *);
1673 static tree cp_parser_asm_operand_list
1674   (cp_parser *);
1675 static tree cp_parser_asm_clobber_list
1676   (cp_parser *);
1677 static tree cp_parser_attributes_opt
1678   (cp_parser *);
1679 static tree cp_parser_attribute_list
1680   (cp_parser *);
1681 static bool cp_parser_extension_opt
1682   (cp_parser *, int *);
1683 static void cp_parser_label_declaration
1684   (cp_parser *);
1685
1686 /* Utility Routines */
1687
1688 static tree cp_parser_lookup_name
1689   (cp_parser *, tree, bool, bool, bool, bool, bool *);
1690 static tree cp_parser_lookup_name_simple
1691   (cp_parser *, tree);
1692 static tree cp_parser_maybe_treat_template_as_class
1693   (tree, bool);
1694 static bool cp_parser_check_declarator_template_parameters
1695   (cp_parser *, cp_declarator *);
1696 static bool cp_parser_check_template_parameters
1697   (cp_parser *, unsigned);
1698 static tree cp_parser_simple_cast_expression
1699   (cp_parser *);
1700 static tree cp_parser_global_scope_opt
1701   (cp_parser *, bool);
1702 static bool cp_parser_constructor_declarator_p
1703   (cp_parser *, bool);
1704 static tree cp_parser_function_definition_from_specifiers_and_declarator
1705   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1706 static tree cp_parser_function_definition_after_declarator
1707   (cp_parser *, bool);
1708 static void cp_parser_template_declaration_after_export
1709   (cp_parser *, bool);
1710 static tree cp_parser_single_declaration
1711   (cp_parser *, bool, bool *);
1712 static tree cp_parser_functional_cast
1713   (cp_parser *, tree);
1714 static tree cp_parser_save_member_function_body
1715   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1716 static tree cp_parser_enclosed_template_argument_list
1717   (cp_parser *);
1718 static void cp_parser_save_default_args
1719   (cp_parser *, tree);
1720 static void cp_parser_late_parsing_for_member
1721   (cp_parser *, tree);
1722 static void cp_parser_late_parsing_default_args
1723   (cp_parser *, tree);
1724 static tree cp_parser_sizeof_operand
1725   (cp_parser *, enum rid);
1726 static bool cp_parser_declares_only_class_p
1727   (cp_parser *);
1728 static void cp_parser_set_storage_class
1729   (cp_decl_specifier_seq *, cp_storage_class);
1730 static void cp_parser_set_decl_spec_type
1731   (cp_decl_specifier_seq *, tree, bool);
1732 static bool cp_parser_friend_p
1733   (const cp_decl_specifier_seq *);
1734 static cp_token *cp_parser_require
1735   (cp_parser *, enum cpp_ttype, const char *);
1736 static cp_token *cp_parser_require_keyword
1737   (cp_parser *, enum rid, const char *);
1738 static bool cp_parser_token_starts_function_definition_p
1739   (cp_token *);
1740 static bool cp_parser_next_token_starts_class_definition_p
1741   (cp_parser *);
1742 static bool cp_parser_next_token_ends_template_argument_p
1743   (cp_parser *);
1744 static bool cp_parser_nth_token_starts_template_argument_list_p
1745   (cp_parser *, size_t);
1746 static enum tag_types cp_parser_token_is_class_key
1747   (cp_token *);
1748 static void cp_parser_check_class_key
1749   (enum tag_types, tree type);
1750 static void cp_parser_check_access_in_redeclaration
1751   (tree type);
1752 static bool cp_parser_optional_template_keyword
1753   (cp_parser *);
1754 static void cp_parser_pre_parsed_nested_name_specifier
1755   (cp_parser *);
1756 static void cp_parser_cache_group
1757   (cp_parser *, enum cpp_ttype, unsigned);
1758 static void cp_parser_parse_tentatively
1759   (cp_parser *);
1760 static void cp_parser_commit_to_tentative_parse
1761   (cp_parser *);
1762 static void cp_parser_abort_tentative_parse
1763   (cp_parser *);
1764 static bool cp_parser_parse_definitely
1765   (cp_parser *);
1766 static inline bool cp_parser_parsing_tentatively
1767   (cp_parser *);
1768 static bool cp_parser_committed_to_tentative_parse
1769   (cp_parser *);
1770 static void cp_parser_error
1771   (cp_parser *, const char *);
1772 static void cp_parser_name_lookup_error
1773   (cp_parser *, tree, tree, const char *);
1774 static bool cp_parser_simulate_error
1775   (cp_parser *);
1776 static void cp_parser_check_type_definition
1777   (cp_parser *);
1778 static void cp_parser_check_for_definition_in_return_type
1779   (cp_declarator *, int);
1780 static void cp_parser_check_for_invalid_template_id
1781   (cp_parser *, tree);
1782 static bool cp_parser_non_integral_constant_expression
1783   (cp_parser *, const char *);
1784 static void cp_parser_diagnose_invalid_type_name
1785   (cp_parser *, tree, tree);
1786 static bool cp_parser_parse_and_diagnose_invalid_type_name
1787   (cp_parser *);
1788 static int cp_parser_skip_to_closing_parenthesis
1789   (cp_parser *, bool, bool, bool);
1790 static void cp_parser_skip_to_end_of_statement
1791   (cp_parser *);
1792 static void cp_parser_consume_semicolon_at_end_of_statement
1793   (cp_parser *);
1794 static void cp_parser_skip_to_end_of_block_or_statement
1795   (cp_parser *);
1796 static void cp_parser_skip_to_closing_brace
1797   (cp_parser *);
1798 static void cp_parser_skip_until_found
1799   (cp_parser *, enum cpp_ttype, const char *);
1800 static bool cp_parser_error_occurred
1801   (cp_parser *);
1802 static bool cp_parser_allow_gnu_extensions_p
1803   (cp_parser *);
1804 static bool cp_parser_is_string_literal
1805   (cp_token *);
1806 static bool cp_parser_is_keyword
1807   (cp_token *, enum rid);
1808 static tree cp_parser_make_typename_type
1809   (cp_parser *, tree, tree);
1810
1811 /* Returns nonzero if we are parsing tentatively.  */
1812
1813 static inline bool
1814 cp_parser_parsing_tentatively (cp_parser* parser)
1815 {
1816   return parser->context->next != NULL;
1817 }
1818
1819 /* Returns nonzero if TOKEN is a string literal.  */
1820
1821 static bool
1822 cp_parser_is_string_literal (cp_token* token)
1823 {
1824   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1825 }
1826
1827 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1828
1829 static bool
1830 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1831 {
1832   return token->keyword == keyword;
1833 }
1834
1835 /* If not parsing tentatively, issue a diagnostic of the form
1836       FILE:LINE: MESSAGE before TOKEN
1837    where TOKEN is the next token in the input stream.  MESSAGE
1838    (specified by the caller) is usually of the form "expected
1839    OTHER-TOKEN".  */
1840
1841 static void
1842 cp_parser_error (cp_parser* parser, const char* message)
1843 {
1844   if (!cp_parser_simulate_error (parser))
1845     {
1846       cp_token *token = cp_lexer_peek_token (parser->lexer);
1847       /* This diagnostic makes more sense if it is tagged to the line
1848          of the token we just peeked at.  */
1849       cp_lexer_set_source_position_from_token (token);
1850       c_parse_error (message,
1851                      /* Because c_parser_error does not understand
1852                         CPP_KEYWORD, keywords are treated like
1853                         identifiers.  */
1854                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1855                      token->value);
1856     }
1857 }
1858
1859 /* Issue an error about name-lookup failing.  NAME is the
1860    IDENTIFIER_NODE DECL is the result of
1861    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1862    the thing that we hoped to find.  */
1863
1864 static void
1865 cp_parser_name_lookup_error (cp_parser* parser,
1866                              tree name,
1867                              tree decl,
1868                              const char* desired)
1869 {
1870   /* If name lookup completely failed, tell the user that NAME was not
1871      declared.  */
1872   if (decl == error_mark_node)
1873     {
1874       if (parser->scope && parser->scope != global_namespace)
1875         error ("%<%D::%D%> has not been declared",
1876                parser->scope, name);
1877       else if (parser->scope == global_namespace)
1878         error ("%<::%D%> has not been declared", name);
1879       else if (parser->object_scope 
1880                && !CLASS_TYPE_P (parser->object_scope))
1881         error ("request for member %qD in non-class type %qT",
1882                name, parser->object_scope);
1883       else if (parser->object_scope)
1884         error ("%<%T::%D%> has not been declared", 
1885                parser->object_scope, name);
1886       else
1887         error ("`%D' has not been declared", name);
1888     }
1889   else if (parser->scope && parser->scope != global_namespace)
1890     error ("%<%D::%D%> %s", parser->scope, name, desired);
1891   else if (parser->scope == global_namespace)
1892     error ("%<::%D%> %s", name, desired);
1893   else
1894     error ("%qD %s", name, desired);
1895 }
1896
1897 /* If we are parsing tentatively, remember that an error has occurred
1898    during this tentative parse.  Returns true if the error was
1899    simulated; false if a message should be issued by the caller.  */
1900
1901 static bool
1902 cp_parser_simulate_error (cp_parser* parser)
1903 {
1904   if (cp_parser_parsing_tentatively (parser)
1905       && !cp_parser_committed_to_tentative_parse (parser))
1906     {
1907       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1908       return true;
1909     }
1910   return false;
1911 }
1912
1913 /* This function is called when a type is defined.  If type
1914    definitions are forbidden at this point, an error message is
1915    issued.  */
1916
1917 static void
1918 cp_parser_check_type_definition (cp_parser* parser)
1919 {
1920   /* If types are forbidden here, issue a message.  */
1921   if (parser->type_definition_forbidden_message)
1922     /* Use `%s' to print the string in case there are any escape
1923        characters in the message.  */
1924     error ("%s", parser->type_definition_forbidden_message);
1925 }
1926
1927 /* This function is called when a declaration is parsed.  If
1928    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1929    indicates that a type was defined in the decl-specifiers for DECL,
1930    then an error is issued.  */
1931
1932 static void
1933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1934                                                int declares_class_or_enum)
1935 {
1936   /* [dcl.fct] forbids type definitions in return types.
1937      Unfortunately, it's not easy to know whether or not we are
1938      processing a return type until after the fact.  */
1939   while (declarator
1940          && (declarator->kind == cdk_pointer
1941              || declarator->kind == cdk_reference
1942              || declarator->kind == cdk_ptrmem))
1943     declarator = declarator->declarator;
1944   if (declarator
1945       && declarator->kind == cdk_function
1946       && declares_class_or_enum & 2)
1947     error ("new types may not be defined in a return type");
1948 }
1949
1950 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1951    "<" in any valid C++ program.  If the next token is indeed "<",
1952    issue a message warning the user about what appears to be an
1953    invalid attempt to form a template-id.  */
1954
1955 static void
1956 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1957                                          tree type)
1958 {
1959   ptrdiff_t start;
1960   cp_token *token;
1961
1962   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1963     {
1964       if (TYPE_P (type))
1965         error ("%qT is not a template", type);
1966       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1967         error ("%qE is not a template", type);
1968       else
1969         error ("invalid template-id");
1970       /* Remember the location of the invalid "<".  */
1971       if (cp_parser_parsing_tentatively (parser)
1972           && !cp_parser_committed_to_tentative_parse (parser))
1973         {
1974           token = cp_lexer_peek_token (parser->lexer);
1975           token = cp_lexer_prev_token (parser->lexer, token);
1976           start = cp_lexer_token_difference (parser->lexer,
1977                                              parser->lexer->buffer,
1978                                              token);
1979         }
1980       else
1981         start = -1;
1982       /* Consume the "<".  */
1983       cp_lexer_consume_token (parser->lexer);
1984       /* Parse the template arguments.  */
1985       cp_parser_enclosed_template_argument_list (parser);
1986       /* Permanently remove the invalid template arguments so that
1987          this error message is not issued again.  */
1988       if (start >= 0)
1989         {
1990           token = cp_lexer_advance_token (parser->lexer,
1991                                           parser->lexer->buffer,
1992                                           start);
1993           cp_lexer_purge_tokens_after (parser->lexer, token);
1994         }
1995     }
1996 }
1997
1998 /* If parsing an integral constant-expression, issue an error message
1999    about the fact that THING appeared and return true.  Otherwise,
2000    return false, marking the current expression as non-constant.  */
2001
2002 static bool
2003 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2004                                             const char *thing)
2005 {
2006   if (parser->integral_constant_expression_p)
2007     {
2008       if (!parser->allow_non_integral_constant_expression_p)
2009         {
2010           error ("%s cannot appear in a constant-expression", thing);
2011           return true;
2012         }
2013       parser->non_integral_constant_expression_p = true;
2014     }
2015   return false;
2016 }
2017
2018 /* Emit a diagnostic for an invalid type name. Consider also if it is
2019    qualified or not and the result of a lookup, to provide a better
2020    message.  */
2021
2022 static void
2023 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2024 {
2025   tree decl, old_scope;
2026   /* Try to lookup the identifier.  */
2027   old_scope = parser->scope;
2028   parser->scope = scope;
2029   decl = cp_parser_lookup_name_simple (parser, id);
2030   parser->scope = old_scope;
2031   /* If the lookup found a template-name, it means that the user forgot
2032   to specify an argument list. Emit an useful error message.  */
2033   if (TREE_CODE (decl) == TEMPLATE_DECL)
2034     error ("invalid use of template-name %qE without an argument list",
2035       decl);
2036   else if (!parser->scope)
2037     {
2038       /* Issue an error message.  */
2039       error ("%qE does not name a type", id);
2040       /* If we're in a template class, it's possible that the user was
2041          referring to a type from a base class.  For example:
2042
2043            template <typename T> struct A { typedef T X; };
2044            template <typename T> struct B : public A<T> { X x; };
2045
2046          The user should have said "typename A<T>::X".  */
2047       if (processing_template_decl && current_class_type)
2048         {
2049           tree b;
2050
2051           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2052                b;
2053                b = TREE_CHAIN (b))
2054             {
2055               tree base_type = BINFO_TYPE (b);
2056               if (CLASS_TYPE_P (base_type)
2057                   && dependent_type_p (base_type))
2058                 {
2059                   tree field;
2060                   /* Go from a particular instantiation of the
2061                      template (which will have an empty TYPE_FIELDs),
2062                      to the main version.  */
2063                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2064                   for (field = TYPE_FIELDS (base_type);
2065                        field;
2066                        field = TREE_CHAIN (field))
2067                     if (TREE_CODE (field) == TYPE_DECL
2068                         && DECL_NAME (field) == id)
2069                       {
2070                         inform ("(perhaps `typename %T::%E' was intended)",
2071                                 BINFO_TYPE (b), id);
2072                         break;
2073                       }
2074                   if (field)
2075                     break;
2076                 }
2077             }
2078         }
2079     }
2080   /* Here we diagnose qualified-ids where the scope is actually correct,
2081      but the identifier does not resolve to a valid type name.  */
2082   else
2083     {
2084       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2085         error ("%qE in namespace %qE does not name a type",
2086                id, parser->scope);
2087       else if (TYPE_P (parser->scope))
2088         error ("q%E in class %qT does not name a type", id, parser->scope);
2089       else
2090         gcc_unreachable ();
2091     }
2092 }
2093
2094 /* Check for a common situation where a type-name should be present,
2095    but is not, and issue a sensible error message.  Returns true if an
2096    invalid type-name was detected.
2097
2098    The situation handled by this function are variable declarations of the
2099    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2100    Usually, `ID' should name a type, but if we got here it means that it
2101    does not. We try to emit the best possible error message depending on
2102    how exactly the id-expression looks like.
2103 */
2104
2105 static bool
2106 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2107 {
2108   tree id;
2109
2110   cp_parser_parse_tentatively (parser);
2111   id = cp_parser_id_expression (parser,
2112                                 /*template_keyword_p=*/false,
2113                                 /*check_dependency_p=*/true,
2114                                 /*template_p=*/NULL,
2115                                 /*declarator_p=*/true);
2116   /* After the id-expression, there should be a plain identifier,
2117      otherwise this is not a simple variable declaration. Also, if
2118      the scope is dependent, we cannot do much.  */
2119   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2120       || (parser->scope && TYPE_P (parser->scope)
2121           && dependent_type_p (parser->scope)))
2122     {
2123       cp_parser_abort_tentative_parse (parser);
2124       return false;
2125     }
2126   if (!cp_parser_parse_definitely (parser)
2127       || TREE_CODE (id) != IDENTIFIER_NODE)
2128     return false;
2129
2130   /* Emit a diagnostic for the invalid type.  */
2131   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2132   /* Skip to the end of the declaration; there's no point in
2133      trying to process it.  */
2134   cp_parser_skip_to_end_of_block_or_statement (parser);
2135   return true;
2136 }
2137
2138 /* Consume tokens up to, and including, the next non-nested closing `)'.
2139    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2140    are doing error recovery. Returns -1 if OR_COMMA is true and we
2141    found an unnested comma.  */
2142
2143 static int
2144 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2145                                        bool recovering,
2146                                        bool or_comma,
2147                                        bool consume_paren)
2148 {
2149   unsigned paren_depth = 0;
2150   unsigned brace_depth = 0;
2151   int result;
2152
2153   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2154       && !cp_parser_committed_to_tentative_parse (parser))
2155     return 0;
2156
2157   while (true)
2158     {
2159       cp_token *token;
2160
2161       /* If we've run out of tokens, then there is no closing `)'.  */
2162       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2163         {
2164           result = 0;
2165           break;
2166         }
2167
2168       token = cp_lexer_peek_token (parser->lexer);
2169
2170       /* This matches the processing in skip_to_end_of_statement.  */
2171       if (token->type == CPP_SEMICOLON && !brace_depth)
2172         {
2173           result = 0;
2174           break;
2175         }
2176       if (token->type == CPP_OPEN_BRACE)
2177         ++brace_depth;
2178       if (token->type == CPP_CLOSE_BRACE)
2179         {
2180           if (!brace_depth--)
2181             {
2182               result = 0;
2183               break;
2184             }
2185         }
2186       if (recovering && or_comma && token->type == CPP_COMMA
2187           && !brace_depth && !paren_depth)
2188         {
2189           result = -1;
2190           break;
2191         }
2192
2193       if (!brace_depth)
2194         {
2195           /* If it is an `(', we have entered another level of nesting.  */
2196           if (token->type == CPP_OPEN_PAREN)
2197             ++paren_depth;
2198           /* If it is a `)', then we might be done.  */
2199           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2200             {
2201               if (consume_paren)
2202                 cp_lexer_consume_token (parser->lexer);
2203               {
2204                 result = 1;
2205                 break;
2206               }
2207             }
2208         }
2209
2210       /* Consume the token.  */
2211       cp_lexer_consume_token (parser->lexer);
2212     }
2213
2214   return result;
2215 }
2216
2217 /* Consume tokens until we reach the end of the current statement.
2218    Normally, that will be just before consuming a `;'.  However, if a
2219    non-nested `}' comes first, then we stop before consuming that.  */
2220
2221 static void
2222 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2223 {
2224   unsigned nesting_depth = 0;
2225
2226   while (true)
2227     {
2228       cp_token *token;
2229
2230       /* Peek at the next token.  */
2231       token = cp_lexer_peek_token (parser->lexer);
2232       /* If we've run out of tokens, stop.  */
2233       if (token->type == CPP_EOF)
2234         break;
2235       /* If the next token is a `;', we have reached the end of the
2236          statement.  */
2237       if (token->type == CPP_SEMICOLON && !nesting_depth)
2238         break;
2239       /* If the next token is a non-nested `}', then we have reached
2240          the end of the current block.  */
2241       if (token->type == CPP_CLOSE_BRACE)
2242         {
2243           /* If this is a non-nested `}', stop before consuming it.
2244              That way, when confronted with something like:
2245
2246                { 3 + }
2247
2248              we stop before consuming the closing `}', even though we
2249              have not yet reached a `;'.  */
2250           if (nesting_depth == 0)
2251             break;
2252           /* If it is the closing `}' for a block that we have
2253              scanned, stop -- but only after consuming the token.
2254              That way given:
2255
2256                 void f g () { ... }
2257                 typedef int I;
2258
2259              we will stop after the body of the erroneously declared
2260              function, but before consuming the following `typedef'
2261              declaration.  */
2262           if (--nesting_depth == 0)
2263             {
2264               cp_lexer_consume_token (parser->lexer);
2265               break;
2266             }
2267         }
2268       /* If it the next token is a `{', then we are entering a new
2269          block.  Consume the entire block.  */
2270       else if (token->type == CPP_OPEN_BRACE)
2271         ++nesting_depth;
2272       /* Consume the token.  */
2273       cp_lexer_consume_token (parser->lexer);
2274     }
2275 }
2276
2277 /* This function is called at the end of a statement or declaration.
2278    If the next token is a semicolon, it is consumed; otherwise, error
2279    recovery is attempted.  */
2280
2281 static void
2282 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2283 {
2284   /* Look for the trailing `;'.  */
2285   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2286     {
2287       /* If there is additional (erroneous) input, skip to the end of
2288          the statement.  */
2289       cp_parser_skip_to_end_of_statement (parser);
2290       /* If the next token is now a `;', consume it.  */
2291       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2292         cp_lexer_consume_token (parser->lexer);
2293     }
2294 }
2295
2296 /* Skip tokens until we have consumed an entire block, or until we
2297    have consumed a non-nested `;'.  */
2298
2299 static void
2300 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2301 {
2302   unsigned nesting_depth = 0;
2303
2304   while (true)
2305     {
2306       cp_token *token;
2307
2308       /* Peek at the next token.  */
2309       token = cp_lexer_peek_token (parser->lexer);
2310       /* If we've run out of tokens, stop.  */
2311       if (token->type == CPP_EOF)
2312         break;
2313       /* If the next token is a `;', we have reached the end of the
2314          statement.  */
2315       if (token->type == CPP_SEMICOLON && !nesting_depth)
2316         {
2317           /* Consume the `;'.  */
2318           cp_lexer_consume_token (parser->lexer);
2319           break;
2320         }
2321       /* Consume the token.  */
2322       token = cp_lexer_consume_token (parser->lexer);
2323       /* If the next token is a non-nested `}', then we have reached
2324          the end of the current block.  */
2325       if (token->type == CPP_CLOSE_BRACE
2326           && (nesting_depth == 0 || --nesting_depth == 0))
2327         break;
2328       /* If it the next token is a `{', then we are entering a new
2329          block.  Consume the entire block.  */
2330       if (token->type == CPP_OPEN_BRACE)
2331         ++nesting_depth;
2332     }
2333 }
2334
2335 /* Skip tokens until a non-nested closing curly brace is the next
2336    token.  */
2337
2338 static void
2339 cp_parser_skip_to_closing_brace (cp_parser *parser)
2340 {
2341   unsigned nesting_depth = 0;
2342
2343   while (true)
2344     {
2345       cp_token *token;
2346
2347       /* Peek at the next token.  */
2348       token = cp_lexer_peek_token (parser->lexer);
2349       /* If we've run out of tokens, stop.  */
2350       if (token->type == CPP_EOF)
2351         break;
2352       /* If the next token is a non-nested `}', then we have reached
2353          the end of the current block.  */
2354       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2355         break;
2356       /* If it the next token is a `{', then we are entering a new
2357          block.  Consume the entire block.  */
2358       else if (token->type == CPP_OPEN_BRACE)
2359         ++nesting_depth;
2360       /* Consume the token.  */
2361       cp_lexer_consume_token (parser->lexer);
2362     }
2363 }
2364
2365 /* This is a simple wrapper around make_typename_type. When the id is
2366    an unresolved identifier node, we can provide a superior diagnostic
2367    using cp_parser_diagnose_invalid_type_name.  */
2368
2369 static tree
2370 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2371 {
2372   tree result;
2373   if (TREE_CODE (id) == IDENTIFIER_NODE)
2374     {
2375       result = make_typename_type (scope, id, /*complain=*/0);
2376       if (result == error_mark_node)
2377         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2378       return result;
2379     }
2380   return make_typename_type (scope, id, tf_error);
2381 }
2382
2383
2384 /* Create a new C++ parser.  */
2385
2386 static cp_parser *
2387 cp_parser_new (void)
2388 {
2389   cp_parser *parser;
2390   cp_lexer *lexer;
2391   unsigned i;
2392
2393   /* cp_lexer_new_main is called before calling ggc_alloc because
2394      cp_lexer_new_main might load a PCH file.  */
2395   lexer = cp_lexer_new_main ();
2396
2397   /* Initialize the binops_by_token so that we can get the tree
2398      directly from the token.  */
2399   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2400     binops_by_token[binops[i].token_type] = binops[i];
2401
2402   parser = GGC_CNEW (cp_parser);
2403   parser->lexer = lexer;
2404   parser->context = cp_parser_context_new (NULL);
2405
2406   /* For now, we always accept GNU extensions.  */
2407   parser->allow_gnu_extensions_p = 1;
2408
2409   /* The `>' token is a greater-than operator, not the end of a
2410      template-id.  */
2411   parser->greater_than_is_operator_p = true;
2412
2413   parser->default_arg_ok_p = true;
2414
2415   /* We are not parsing a constant-expression.  */
2416   parser->integral_constant_expression_p = false;
2417   parser->allow_non_integral_constant_expression_p = false;
2418   parser->non_integral_constant_expression_p = false;
2419
2420   /* Local variable names are not forbidden.  */
2421   parser->local_variables_forbidden_p = false;
2422
2423   /* We are not processing an `extern "C"' declaration.  */
2424   parser->in_unbraced_linkage_specification_p = false;
2425
2426   /* We are not processing a declarator.  */
2427   parser->in_declarator_p = false;
2428
2429   /* We are not processing a template-argument-list.  */
2430   parser->in_template_argument_list_p = false;
2431
2432   /* We are not in an iteration statement.  */
2433   parser->in_iteration_statement_p = false;
2434
2435   /* We are not in a switch statement.  */
2436   parser->in_switch_statement_p = false;
2437
2438   /* We are not parsing a type-id inside an expression.  */
2439   parser->in_type_id_in_expr_p = false;
2440
2441   /* Declarations aren't implicitly extern "C". */
2442   parser->implicit_extern_c = false;
2443
2444   /* String literals should be translated to the execution character set.  */
2445   parser->translate_strings_p = true;
2446
2447   /* The unparsed function queue is empty.  */
2448   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2449
2450   /* There are no classes being defined.  */
2451   parser->num_classes_being_defined = 0;
2452
2453   /* No template parameters apply.  */
2454   parser->num_template_parameter_lists = 0;
2455
2456   return parser;
2457 }
2458
2459 /* Create a cp_lexer structure which will emit the tokens in CACHE
2460    and push it onto the parser's lexer stack.  This is used for delayed
2461    parsing of in-class method bodies and default arguments, and should
2462    not be confused with tentative parsing.  */
2463 static void
2464 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2465 {
2466   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2467   lexer->next = parser->lexer;
2468   parser->lexer = lexer;
2469
2470   /* Move the current source position to that of the first token in the
2471      new lexer.  */
2472   cp_lexer_set_source_position_from_token (lexer->next_token);
2473 }
2474
2475 /* Pop the top lexer off the parser stack.  This is never used for the
2476    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2477 static void
2478 cp_parser_pop_lexer (cp_parser *parser)
2479 {
2480   cp_lexer *lexer = parser->lexer;
2481   parser->lexer = lexer->next;
2482   cp_lexer_destroy (lexer);
2483
2484   /* Put the current source position back where it was before this
2485      lexer was pushed.  */
2486   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2487 }
2488
2489 /* Lexical conventions [gram.lex]  */
2490
2491 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2492    identifier.  */
2493
2494 static tree
2495 cp_parser_identifier (cp_parser* parser)
2496 {
2497   cp_token *token;
2498
2499   /* Look for the identifier.  */
2500   token = cp_parser_require (parser, CPP_NAME, "identifier");
2501   /* Return the value.  */
2502   return token ? token->value : error_mark_node;
2503 }
2504
2505 /* Parse a sequence of adjacent string constants.  Returns a
2506    TREE_STRING representing the combined, nul-terminated string
2507    constant.  If TRANSLATE is true, translate the string to the
2508    execution character set.  If WIDE_OK is true, a wide string is
2509    invalid here.
2510
2511    C++98 [lex.string] says that if a narrow string literal token is
2512    adjacent to a wide string literal token, the behavior is undefined.
2513    However, C99 6.4.5p4 says that this results in a wide string literal.
2514    We follow C99 here, for consistency with the C front end.
2515
2516    This code is largely lifted from lex_string() in c-lex.c.
2517
2518    FUTURE: ObjC++ will need to handle @-strings here.  */
2519 static tree
2520 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2521 {
2522   tree value;
2523   bool wide = false;
2524   size_t count;
2525   struct obstack str_ob;
2526   cpp_string str, istr, *strs;
2527   cp_token *tok;
2528
2529   tok = cp_lexer_peek_token (parser->lexer);
2530   if (!cp_parser_is_string_literal (tok))
2531     {
2532       cp_parser_error (parser, "expected string-literal");
2533       return error_mark_node;
2534     }
2535
2536   /* Try to avoid the overhead of creating and destroying an obstack
2537      for the common case of just one string.  */
2538   if (!cp_parser_is_string_literal
2539       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2540     {
2541       cp_lexer_consume_token (parser->lexer);
2542
2543       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2544       str.len = TREE_STRING_LENGTH (tok->value);
2545       count = 1;
2546       if (tok->type == CPP_WSTRING)
2547         wide = true;
2548
2549       strs = &str;
2550     }
2551   else
2552     {
2553       gcc_obstack_init (&str_ob);
2554       count = 0;
2555
2556       do
2557         {
2558           cp_lexer_consume_token (parser->lexer);
2559           count++;
2560           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2561           str.len = TREE_STRING_LENGTH (tok->value);
2562           if (tok->type == CPP_WSTRING)
2563             wide = true;
2564
2565           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2566
2567           tok = cp_lexer_peek_token (parser->lexer);
2568         }
2569       while (cp_parser_is_string_literal (tok));
2570
2571       strs = (cpp_string *) obstack_finish (&str_ob);
2572     }
2573
2574   if (wide && !wide_ok)
2575     {
2576       cp_parser_error (parser, "a wide string is invalid in this context");
2577       wide = false;
2578     }
2579
2580   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2581       (parse_in, strs, count, &istr, wide))
2582     {
2583       value = build_string (istr.len, (char *)istr.text);
2584       free ((void *)istr.text);
2585
2586       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2587       value = fix_string_type (value);
2588     }
2589   else
2590     /* cpp_interpret_string has issued an error.  */
2591     value = error_mark_node;
2592
2593   if (count > 1)
2594     obstack_free (&str_ob, 0);
2595
2596   return value;
2597 }
2598
2599
2600 /* Basic concepts [gram.basic]  */
2601
2602 /* Parse a translation-unit.
2603
2604    translation-unit:
2605      declaration-seq [opt]
2606
2607    Returns TRUE if all went well.  */
2608
2609 static bool
2610 cp_parser_translation_unit (cp_parser* parser)
2611 {
2612   /* The address of the first non-permanent object on the declarator
2613      obstack.  */
2614   static void *declarator_obstack_base;
2615
2616   bool success;
2617
2618   /* Create the declarator obstack, if necessary.  */
2619   if (!cp_error_declarator)
2620     {
2621       gcc_obstack_init (&declarator_obstack);
2622       /* Create the error declarator.  */
2623       cp_error_declarator = make_declarator (cdk_error);
2624       /* Create the empty parameter list.  */
2625       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2626       /* Remember where the base of the declarator obstack lies.  */
2627       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2628     }
2629
2630   while (true)
2631     {
2632       cp_parser_declaration_seq_opt (parser);
2633
2634       /* If there are no tokens left then all went well.  */
2635       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2636         {
2637           /* Consume the EOF token.  */
2638           cp_parser_require (parser, CPP_EOF, "end-of-file");
2639
2640           /* Get rid of the token array; we don't need it any more. */
2641           cp_lexer_destroy (parser->lexer);
2642           parser->lexer = NULL;
2643
2644           /* This file might have been a context that's implicitly extern
2645              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2646           if (parser->implicit_extern_c)
2647             {
2648               pop_lang_context ();
2649               parser->implicit_extern_c = false;
2650             }
2651
2652           /* Finish up.  */
2653           finish_translation_unit ();
2654
2655           success = true;
2656           break;
2657         }
2658       else
2659         {
2660           cp_parser_error (parser, "expected declaration");
2661           success = false;
2662           break;
2663         }
2664     }
2665
2666   /* Make sure the declarator obstack was fully cleaned up.  */
2667   gcc_assert (obstack_next_free (&declarator_obstack)
2668               == declarator_obstack_base);
2669
2670   /* All went well.  */
2671   return success;
2672 }
2673
2674 /* Expressions [gram.expr] */
2675
2676 /* Parse a primary-expression.
2677
2678    primary-expression:
2679      literal
2680      this
2681      ( expression )
2682      id-expression
2683
2684    GNU Extensions:
2685
2686    primary-expression:
2687      ( compound-statement )
2688      __builtin_va_arg ( assignment-expression , type-id )
2689
2690    literal:
2691      __null
2692
2693    Returns a representation of the expression.
2694
2695    *IDK indicates what kind of id-expression (if any) was present.
2696
2697    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2698    used as the operand of a pointer-to-member.  In that case,
2699    *QUALIFYING_CLASS gives the class that is used as the qualifying
2700    class in the pointer-to-member.  */
2701
2702 static tree
2703 cp_parser_primary_expression (cp_parser *parser,
2704                               cp_id_kind *idk,
2705                               tree *qualifying_class)
2706 {
2707   cp_token *token;
2708
2709   /* Assume the primary expression is not an id-expression.  */
2710   *idk = CP_ID_KIND_NONE;
2711   /* And that it cannot be used as pointer-to-member.  */
2712   *qualifying_class = NULL_TREE;
2713
2714   /* Peek at the next token.  */
2715   token = cp_lexer_peek_token (parser->lexer);
2716   switch (token->type)
2717     {
2718       /* literal:
2719            integer-literal
2720            character-literal
2721            floating-literal
2722            string-literal
2723            boolean-literal  */
2724     case CPP_CHAR:
2725     case CPP_WCHAR:
2726     case CPP_NUMBER:
2727       token = cp_lexer_consume_token (parser->lexer);
2728       return token->value;
2729
2730     case CPP_STRING:
2731     case CPP_WSTRING:
2732       /* ??? Should wide strings be allowed when parser->translate_strings_p
2733          is false (i.e. in attributes)?  If not, we can kill the third
2734          argument to cp_parser_string_literal.  */
2735       return cp_parser_string_literal (parser,
2736                                        parser->translate_strings_p,
2737                                        true);
2738
2739     case CPP_OPEN_PAREN:
2740       {
2741         tree expr;
2742         bool saved_greater_than_is_operator_p;
2743
2744         /* Consume the `('.  */
2745         cp_lexer_consume_token (parser->lexer);
2746         /* Within a parenthesized expression, a `>' token is always
2747            the greater-than operator.  */
2748         saved_greater_than_is_operator_p
2749           = parser->greater_than_is_operator_p;
2750         parser->greater_than_is_operator_p = true;
2751         /* If we see `( { ' then we are looking at the beginning of
2752            a GNU statement-expression.  */
2753         if (cp_parser_allow_gnu_extensions_p (parser)
2754             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2755           {
2756             /* Statement-expressions are not allowed by the standard.  */
2757             if (pedantic)
2758               pedwarn ("ISO C++ forbids braced-groups within expressions");
2759
2760             /* And they're not allowed outside of a function-body; you
2761                cannot, for example, write:
2762
2763                  int i = ({ int j = 3; j + 1; });
2764
2765                at class or namespace scope.  */
2766             if (!at_function_scope_p ())
2767               error ("statement-expressions are allowed only inside functions");
2768             /* Start the statement-expression.  */
2769             expr = begin_stmt_expr ();
2770             /* Parse the compound-statement.  */
2771             cp_parser_compound_statement (parser, expr, false);
2772             /* Finish up.  */
2773             expr = finish_stmt_expr (expr, false);
2774           }
2775         else
2776           {
2777             /* Parse the parenthesized expression.  */
2778             expr = cp_parser_expression (parser);
2779             /* Let the front end know that this expression was
2780                enclosed in parentheses. This matters in case, for
2781                example, the expression is of the form `A::B', since
2782                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2783                not.  */
2784             finish_parenthesized_expr (expr);
2785           }
2786         /* The `>' token might be the end of a template-id or
2787            template-parameter-list now.  */
2788         parser->greater_than_is_operator_p
2789           = saved_greater_than_is_operator_p;
2790         /* Consume the `)'.  */
2791         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2792           cp_parser_skip_to_end_of_statement (parser);
2793
2794         return expr;
2795       }
2796
2797     case CPP_KEYWORD:
2798       switch (token->keyword)
2799         {
2800           /* These two are the boolean literals.  */
2801         case RID_TRUE:
2802           cp_lexer_consume_token (parser->lexer);
2803           return boolean_true_node;
2804         case RID_FALSE:
2805           cp_lexer_consume_token (parser->lexer);
2806           return boolean_false_node;
2807
2808           /* The `__null' literal.  */
2809         case RID_NULL:
2810           cp_lexer_consume_token (parser->lexer);
2811           return null_node;
2812
2813           /* Recognize the `this' keyword.  */
2814         case RID_THIS:
2815           cp_lexer_consume_token (parser->lexer);
2816           if (parser->local_variables_forbidden_p)
2817             {
2818               error ("%<this%> may not be used in this context");
2819               return error_mark_node;
2820             }
2821           /* Pointers cannot appear in constant-expressions.  */
2822           if (cp_parser_non_integral_constant_expression (parser,
2823                                                           "`this'"))
2824             return error_mark_node;
2825           return finish_this_expr ();
2826
2827           /* The `operator' keyword can be the beginning of an
2828              id-expression.  */
2829         case RID_OPERATOR:
2830           goto id_expression;
2831
2832         case RID_FUNCTION_NAME:
2833         case RID_PRETTY_FUNCTION_NAME:
2834         case RID_C99_FUNCTION_NAME:
2835           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836              __func__ are the names of variables -- but they are
2837              treated specially.  Therefore, they are handled here,
2838              rather than relying on the generic id-expression logic
2839              below.  Grammatically, these names are id-expressions.
2840
2841              Consume the token.  */
2842           token = cp_lexer_consume_token (parser->lexer);
2843           /* Look up the name.  */
2844           return finish_fname (token->value);
2845
2846         case RID_VA_ARG:
2847           {
2848             tree expression;
2849             tree type;
2850
2851             /* The `__builtin_va_arg' construct is used to handle
2852                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2853             cp_lexer_consume_token (parser->lexer);
2854             /* Look for the opening `('.  */
2855             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2856             /* Now, parse the assignment-expression.  */
2857             expression = cp_parser_assignment_expression (parser);
2858             /* Look for the `,'.  */
2859             cp_parser_require (parser, CPP_COMMA, "`,'");
2860             /* Parse the type-id.  */
2861             type = cp_parser_type_id (parser);
2862             /* Look for the closing `)'.  */
2863             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2864             /* Using `va_arg' in a constant-expression is not
2865                allowed.  */
2866             if (cp_parser_non_integral_constant_expression (parser,
2867                                                             "`va_arg'"))
2868               return error_mark_node;
2869             return build_x_va_arg (expression, type);
2870           }
2871
2872         case RID_OFFSETOF:
2873           return cp_parser_builtin_offsetof (parser);
2874
2875         default:
2876           cp_parser_error (parser, "expected primary-expression");
2877           return error_mark_node;
2878         }
2879
2880       /* An id-expression can start with either an identifier, a
2881          `::' as the beginning of a qualified-id, or the "operator"
2882          keyword.  */
2883     case CPP_NAME:
2884     case CPP_SCOPE:
2885     case CPP_TEMPLATE_ID:
2886     case CPP_NESTED_NAME_SPECIFIER:
2887       {
2888         tree id_expression;
2889         tree decl;
2890         const char *error_msg;
2891
2892       id_expression:
2893         /* Parse the id-expression.  */
2894         id_expression
2895           = cp_parser_id_expression (parser,
2896                                      /*template_keyword_p=*/false,
2897                                      /*check_dependency_p=*/true,
2898                                      /*template_p=*/NULL,
2899                                      /*declarator_p=*/false);
2900         if (id_expression == error_mark_node)
2901           return error_mark_node;
2902         /* If we have a template-id, then no further lookup is
2903            required.  If the template-id was for a template-class, we
2904            will sometimes have a TYPE_DECL at this point.  */
2905         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2906             || TREE_CODE (id_expression) == TYPE_DECL)
2907           decl = id_expression;
2908         /* Look up the name.  */
2909         else
2910           {
2911             bool ambiguous_p;
2912
2913             decl = cp_parser_lookup_name (parser, id_expression,
2914                                           /*is_type=*/false,
2915                                           /*is_template=*/false,
2916                                           /*is_namespace=*/false,
2917                                           /*check_dependency=*/true,
2918                                           &ambiguous_p);
2919             /* If the lookup was ambiguous, an error will already have
2920                been issued.  */
2921             if (ambiguous_p)
2922               return error_mark_node;
2923             /* If name lookup gives us a SCOPE_REF, then the
2924                qualifying scope was dependent.  Just propagate the
2925                name.  */
2926             if (TREE_CODE (decl) == SCOPE_REF)
2927               {
2928                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2929                   *qualifying_class = TREE_OPERAND (decl, 0);
2930                 return decl;
2931               }
2932             /* Check to see if DECL is a local variable in a context
2933                where that is forbidden.  */
2934             if (parser->local_variables_forbidden_p
2935                 && local_variable_p (decl))
2936               {
2937                 /* It might be that we only found DECL because we are
2938                    trying to be generous with pre-ISO scoping rules.
2939                    For example, consider:
2940
2941                      int i;
2942                      void g() {
2943                        for (int i = 0; i < 10; ++i) {}
2944                        extern void f(int j = i);
2945                      }
2946
2947                    Here, name look up will originally find the out
2948                    of scope `i'.  We need to issue a warning message,
2949                    but then use the global `i'.  */
2950                 decl = check_for_out_of_scope_variable (decl);
2951                 if (local_variable_p (decl))
2952                   {
2953                     error ("local variable %qD may not appear in this context",
2954                            decl);
2955                     return error_mark_node;
2956                   }
2957               }
2958           }
2959
2960         decl = finish_id_expression (id_expression, decl, parser->scope,
2961                                      idk, qualifying_class,
2962                                      parser->integral_constant_expression_p,
2963                                      parser->allow_non_integral_constant_expression_p,
2964                                      &parser->non_integral_constant_expression_p,
2965                                      &error_msg);
2966         if (error_msg)
2967           cp_parser_error (parser, error_msg);
2968         return decl;
2969       }
2970
2971       /* Anything else is an error.  */
2972     default:
2973       cp_parser_error (parser, "expected primary-expression");
2974       return error_mark_node;
2975     }
2976 }
2977
2978 /* Parse an id-expression.
2979
2980    id-expression:
2981      unqualified-id
2982      qualified-id
2983
2984    qualified-id:
2985      :: [opt] nested-name-specifier template [opt] unqualified-id
2986      :: identifier
2987      :: operator-function-id
2988      :: template-id
2989
2990    Return a representation of the unqualified portion of the
2991    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2992    a `::' or nested-name-specifier.
2993
2994    Often, if the id-expression was a qualified-id, the caller will
2995    want to make a SCOPE_REF to represent the qualified-id.  This
2996    function does not do this in order to avoid wastefully creating
2997    SCOPE_REFs when they are not required.
2998
2999    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3000    `template' keyword.
3001
3002    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3003    uninstantiated templates.
3004
3005    If *TEMPLATE_P is non-NULL, it is set to true iff the
3006    `template' keyword is used to explicitly indicate that the entity
3007    named is a template.
3008
3009    If DECLARATOR_P is true, the id-expression is appearing as part of
3010    a declarator, rather than as part of an expression.  */
3011
3012 static tree
3013 cp_parser_id_expression (cp_parser *parser,
3014                          bool template_keyword_p,
3015                          bool check_dependency_p,
3016                          bool *template_p,
3017                          bool declarator_p)
3018 {
3019   bool global_scope_p;
3020   bool nested_name_specifier_p;
3021
3022   /* Assume the `template' keyword was not used.  */
3023   if (template_p)
3024     *template_p = false;
3025
3026   /* Look for the optional `::' operator.  */
3027   global_scope_p
3028     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3029        != NULL_TREE);
3030   /* Look for the optional nested-name-specifier.  */
3031   nested_name_specifier_p
3032     = (cp_parser_nested_name_specifier_opt (parser,
3033                                             /*typename_keyword_p=*/false,
3034                                             check_dependency_p,
3035                                             /*type_p=*/false,
3036                                             declarator_p)
3037        != NULL_TREE);
3038   /* If there is a nested-name-specifier, then we are looking at
3039      the first qualified-id production.  */
3040   if (nested_name_specifier_p)
3041     {
3042       tree saved_scope;
3043       tree saved_object_scope;
3044       tree saved_qualifying_scope;
3045       tree unqualified_id;
3046       bool is_template;
3047
3048       /* See if the next token is the `template' keyword.  */
3049       if (!template_p)
3050         template_p = &is_template;
3051       *template_p = cp_parser_optional_template_keyword (parser);
3052       /* Name lookup we do during the processing of the
3053          unqualified-id might obliterate SCOPE.  */
3054       saved_scope = parser->scope;
3055       saved_object_scope = parser->object_scope;
3056       saved_qualifying_scope = parser->qualifying_scope;
3057       /* Process the final unqualified-id.  */
3058       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3059                                                  check_dependency_p,
3060                                                  declarator_p);
3061       /* Restore the SAVED_SCOPE for our caller.  */
3062       parser->scope = saved_scope;
3063       parser->object_scope = saved_object_scope;
3064       parser->qualifying_scope = saved_qualifying_scope;
3065
3066       return unqualified_id;
3067     }
3068   /* Otherwise, if we are in global scope, then we are looking at one
3069      of the other qualified-id productions.  */
3070   else if (global_scope_p)
3071     {
3072       cp_token *token;
3073       tree id;
3074
3075       /* Peek at the next token.  */
3076       token = cp_lexer_peek_token (parser->lexer);
3077
3078       /* If it's an identifier, and the next token is not a "<", then
3079          we can avoid the template-id case.  This is an optimization
3080          for this common case.  */
3081       if (token->type == CPP_NAME
3082           && !cp_parser_nth_token_starts_template_argument_list_p
3083                (parser, 2))
3084         return cp_parser_identifier (parser);
3085
3086       cp_parser_parse_tentatively (parser);
3087       /* Try a template-id.  */
3088       id = cp_parser_template_id (parser,
3089                                   /*template_keyword_p=*/false,
3090                                   /*check_dependency_p=*/true,
3091                                   declarator_p);
3092       /* If that worked, we're done.  */
3093       if (cp_parser_parse_definitely (parser))
3094         return id;
3095
3096       /* Peek at the next token.  (Changes in the token buffer may
3097          have invalidated the pointer obtained above.)  */
3098       token = cp_lexer_peek_token (parser->lexer);
3099
3100       switch (token->type)
3101         {
3102         case CPP_NAME:
3103           return cp_parser_identifier (parser);
3104
3105         case CPP_KEYWORD:
3106           if (token->keyword == RID_OPERATOR)
3107             return cp_parser_operator_function_id (parser);
3108           /* Fall through.  */
3109
3110         default:
3111           cp_parser_error (parser, "expected id-expression");
3112           return error_mark_node;
3113         }
3114     }
3115   else
3116     return cp_parser_unqualified_id (parser, template_keyword_p,
3117                                      /*check_dependency_p=*/true,
3118                                      declarator_p);
3119 }
3120
3121 /* Parse an unqualified-id.
3122
3123    unqualified-id:
3124      identifier
3125      operator-function-id
3126      conversion-function-id
3127      ~ class-name
3128      template-id
3129
3130    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3131    keyword, in a construct like `A::template ...'.
3132
3133    Returns a representation of unqualified-id.  For the `identifier'
3134    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3135    production a BIT_NOT_EXPR is returned; the operand of the
3136    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3137    other productions, see the documentation accompanying the
3138    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3139    names are looked up in uninstantiated templates.  If DECLARATOR_P
3140    is true, the unqualified-id is appearing as part of a declarator,
3141    rather than as part of an expression.  */
3142
3143 static tree
3144 cp_parser_unqualified_id (cp_parser* parser,
3145                           bool template_keyword_p,
3146                           bool check_dependency_p,
3147                           bool declarator_p)
3148 {
3149   cp_token *token;
3150
3151   /* Peek at the next token.  */
3152   token = cp_lexer_peek_token (parser->lexer);
3153
3154   switch (token->type)
3155     {
3156     case CPP_NAME:
3157       {
3158         tree id;
3159
3160         /* We don't know yet whether or not this will be a
3161            template-id.  */
3162         cp_parser_parse_tentatively (parser);
3163         /* Try a template-id.  */
3164         id = cp_parser_template_id (parser, template_keyword_p,
3165                                     check_dependency_p,
3166                                     declarator_p);
3167         /* If it worked, we're done.  */
3168         if (cp_parser_parse_definitely (parser))
3169           return id;
3170         /* Otherwise, it's an ordinary identifier.  */
3171         return cp_parser_identifier (parser);
3172       }
3173
3174     case CPP_TEMPLATE_ID:
3175       return cp_parser_template_id (parser, template_keyword_p,
3176                                     check_dependency_p,
3177                                     declarator_p);
3178
3179     case CPP_COMPL:
3180       {
3181         tree type_decl;
3182         tree qualifying_scope;
3183         tree object_scope;
3184         tree scope;
3185
3186         /* Consume the `~' token.  */
3187         cp_lexer_consume_token (parser->lexer);
3188         /* Parse the class-name.  The standard, as written, seems to
3189            say that:
3190
3191              template <typename T> struct S { ~S (); };
3192              template <typename T> S<T>::~S() {}
3193
3194            is invalid, since `~' must be followed by a class-name, but
3195            `S<T>' is dependent, and so not known to be a class.
3196            That's not right; we need to look in uninstantiated
3197            templates.  A further complication arises from:
3198
3199              template <typename T> void f(T t) {
3200                t.T::~T();
3201              }
3202
3203            Here, it is not possible to look up `T' in the scope of `T'
3204            itself.  We must look in both the current scope, and the
3205            scope of the containing complete expression.
3206
3207            Yet another issue is:
3208
3209              struct S {
3210                int S;
3211                ~S();
3212              };
3213
3214              S::~S() {}
3215
3216            The standard does not seem to say that the `S' in `~S'
3217            should refer to the type `S' and not the data member
3218            `S::S'.  */
3219
3220         /* DR 244 says that we look up the name after the "~" in the
3221            same scope as we looked up the qualifying name.  That idea
3222            isn't fully worked out; it's more complicated than that.  */
3223         scope = parser->scope;
3224         object_scope = parser->object_scope;
3225         qualifying_scope = parser->qualifying_scope;
3226
3227         /* If the name is of the form "X::~X" it's OK.  */
3228         if (scope && TYPE_P (scope)
3229             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3230             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3231                 == CPP_OPEN_PAREN)
3232             && (cp_lexer_peek_token (parser->lexer)->value
3233                 == TYPE_IDENTIFIER (scope)))
3234           {
3235             cp_lexer_consume_token (parser->lexer);
3236             return build_nt (BIT_NOT_EXPR, scope);
3237           }
3238
3239         /* If there was an explicit qualification (S::~T), first look
3240            in the scope given by the qualification (i.e., S).  */
3241         if (scope)
3242           {
3243             cp_parser_parse_tentatively (parser);
3244             type_decl = cp_parser_class_name (parser,
3245                                               /*typename_keyword_p=*/false,
3246                                               /*template_keyword_p=*/false,
3247                                               /*type_p=*/false,
3248                                               /*check_dependency=*/false,
3249                                               /*class_head_p=*/false,
3250                                               declarator_p);
3251             if (cp_parser_parse_definitely (parser))
3252               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3253           }
3254         /* In "N::S::~S", look in "N" as well.  */
3255         if (scope && qualifying_scope)
3256           {
3257             cp_parser_parse_tentatively (parser);
3258             parser->scope = qualifying_scope;
3259             parser->object_scope = NULL_TREE;
3260             parser->qualifying_scope = NULL_TREE;
3261             type_decl
3262               = cp_parser_class_name (parser,
3263                                       /*typename_keyword_p=*/false,
3264                                       /*template_keyword_p=*/false,
3265                                       /*type_p=*/false,
3266                                       /*check_dependency=*/false,
3267                                       /*class_head_p=*/false,
3268                                       declarator_p);
3269             if (cp_parser_parse_definitely (parser))
3270               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3271           }
3272         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3273         else if (object_scope)
3274           {
3275             cp_parser_parse_tentatively (parser);
3276             parser->scope = object_scope;
3277             parser->object_scope = NULL_TREE;
3278             parser->qualifying_scope = NULL_TREE;
3279             type_decl
3280               = cp_parser_class_name (parser,
3281                                       /*typename_keyword_p=*/false,
3282                                       /*template_keyword_p=*/false,
3283                                       /*type_p=*/false,
3284                                       /*check_dependency=*/false,
3285                                       /*class_head_p=*/false,
3286                                       declarator_p);
3287             if (cp_parser_parse_definitely (parser))
3288               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3289           }
3290         /* Look in the surrounding context.  */
3291         parser->scope = NULL_TREE;
3292         parser->object_scope = NULL_TREE;
3293         parser->qualifying_scope = NULL_TREE;
3294         type_decl
3295           = cp_parser_class_name (parser,
3296                                   /*typename_keyword_p=*/false,
3297                                   /*template_keyword_p=*/false,
3298                                   /*type_p=*/false,
3299                                   /*check_dependency=*/false,
3300                                   /*class_head_p=*/false,
3301                                   declarator_p);
3302         /* If an error occurred, assume that the name of the
3303            destructor is the same as the name of the qualifying
3304            class.  That allows us to keep parsing after running
3305            into ill-formed destructor names.  */
3306         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3307           return build_nt (BIT_NOT_EXPR, scope);
3308         else if (type_decl == error_mark_node)
3309           return error_mark_node;
3310
3311         /* [class.dtor]
3312
3313            A typedef-name that names a class shall not be used as the
3314            identifier in the declarator for a destructor declaration.  */
3315         if (declarator_p
3316             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3317             && !DECL_SELF_REFERENCE_P (type_decl))
3318           error ("typedef-name %qD used as destructor declarator",
3319                  type_decl);
3320
3321         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3322       }
3323
3324     case CPP_KEYWORD:
3325       if (token->keyword == RID_OPERATOR)
3326         {
3327           tree id;
3328
3329           /* This could be a template-id, so we try that first.  */
3330           cp_parser_parse_tentatively (parser);
3331           /* Try a template-id.  */
3332           id = cp_parser_template_id (parser, template_keyword_p,
3333                                       /*check_dependency_p=*/true,
3334                                       declarator_p);
3335           /* If that worked, we're done.  */
3336           if (cp_parser_parse_definitely (parser))
3337             return id;
3338           /* We still don't know whether we're looking at an
3339              operator-function-id or a conversion-function-id.  */
3340           cp_parser_parse_tentatively (parser);
3341           /* Try an operator-function-id.  */
3342           id = cp_parser_operator_function_id (parser);
3343           /* If that didn't work, try a conversion-function-id.  */
3344           if (!cp_parser_parse_definitely (parser))
3345             id = cp_parser_conversion_function_id (parser);
3346
3347           return id;
3348         }
3349       /* Fall through.  */
3350
3351     default:
3352       cp_parser_error (parser, "expected unqualified-id");
3353       return error_mark_node;
3354     }
3355 }
3356
3357 /* Parse an (optional) nested-name-specifier.
3358
3359    nested-name-specifier:
3360      class-or-namespace-name :: nested-name-specifier [opt]
3361      class-or-namespace-name :: template nested-name-specifier [opt]
3362
3363    PARSER->SCOPE should be set appropriately before this function is
3364    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3365    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3366    in name lookups.
3367
3368    Sets PARSER->SCOPE to the class (TYPE) or namespace
3369    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3370    it unchanged if there is no nested-name-specifier.  Returns the new
3371    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3372
3373    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3374    part of a declaration and/or decl-specifier.  */
3375
3376 static tree
3377 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3378                                      bool typename_keyword_p,
3379                                      bool check_dependency_p,
3380                                      bool type_p,
3381                                      bool is_declaration)
3382 {
3383   bool success = false;
3384   tree access_check = NULL_TREE;
3385   ptrdiff_t start;
3386   cp_token* token;
3387
3388   /* If the next token corresponds to a nested name specifier, there
3389      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3390      false, it may have been true before, in which case something
3391      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3392      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3393      CHECK_DEPENDENCY_P is false, we have to fall through into the
3394      main loop.  */
3395   if (check_dependency_p
3396       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3397     {
3398       cp_parser_pre_parsed_nested_name_specifier (parser);
3399       return parser->scope;
3400     }
3401
3402   /* Remember where the nested-name-specifier starts.  */
3403   if (cp_parser_parsing_tentatively (parser)
3404       && !cp_parser_committed_to_tentative_parse (parser))
3405     {
3406       token = cp_lexer_peek_token (parser->lexer);
3407       start = cp_lexer_token_difference (parser->lexer,
3408                                          parser->lexer->buffer,
3409                                          token);
3410     }
3411   else
3412     start = -1;
3413
3414   push_deferring_access_checks (dk_deferred);
3415
3416   while (true)
3417     {
3418       tree new_scope;
3419       tree old_scope;
3420       tree saved_qualifying_scope;
3421       bool template_keyword_p;
3422
3423       /* Spot cases that cannot be the beginning of a
3424          nested-name-specifier.  */
3425       token = cp_lexer_peek_token (parser->lexer);
3426
3427       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3428          the already parsed nested-name-specifier.  */
3429       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3430         {
3431           /* Grab the nested-name-specifier and continue the loop.  */
3432           cp_parser_pre_parsed_nested_name_specifier (parser);
3433           success = true;
3434           continue;
3435         }
3436
3437       /* Spot cases that cannot be the beginning of a
3438          nested-name-specifier.  On the second and subsequent times
3439          through the loop, we look for the `template' keyword.  */
3440       if (success && token->keyword == RID_TEMPLATE)
3441         ;
3442       /* A template-id can start a nested-name-specifier.  */
3443       else if (token->type == CPP_TEMPLATE_ID)
3444         ;
3445       else
3446         {
3447           /* If the next token is not an identifier, then it is
3448              definitely not a class-or-namespace-name.  */
3449           if (token->type != CPP_NAME)
3450             break;
3451           /* If the following token is neither a `<' (to begin a
3452              template-id), nor a `::', then we are not looking at a
3453              nested-name-specifier.  */
3454           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3455           if (token->type != CPP_SCOPE
3456               && !cp_parser_nth_token_starts_template_argument_list_p
3457                   (parser, 2))
3458             break;
3459         }
3460
3461       /* The nested-name-specifier is optional, so we parse
3462          tentatively.  */
3463       cp_parser_parse_tentatively (parser);
3464
3465       /* Look for the optional `template' keyword, if this isn't the
3466          first time through the loop.  */
3467       if (success)
3468         template_keyword_p = cp_parser_optional_template_keyword (parser);
3469       else
3470         template_keyword_p = false;
3471
3472       /* Save the old scope since the name lookup we are about to do
3473          might destroy it.  */
3474       old_scope = parser->scope;
3475       saved_qualifying_scope = parser->qualifying_scope;
3476       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3477          look up names in "X<T>::I" in order to determine that "Y" is
3478          a template.  So, if we have a typename at this point, we make
3479          an effort to look through it.  */
3480       if (is_declaration 
3481           && !typename_keyword_p
3482           && parser->scope 
3483           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3484         parser->scope = resolve_typename_type (parser->scope, 
3485                                                /*only_current_p=*/false);
3486       /* Parse the qualifying entity.  */
3487       new_scope
3488         = cp_parser_class_or_namespace_name (parser,
3489                                              typename_keyword_p,
3490                                              template_keyword_p,
3491                                              check_dependency_p,
3492                                              type_p,
3493                                              is_declaration);
3494       /* Look for the `::' token.  */
3495       cp_parser_require (parser, CPP_SCOPE, "`::'");
3496
3497       /* If we found what we wanted, we keep going; otherwise, we're
3498          done.  */
3499       if (!cp_parser_parse_definitely (parser))
3500         {
3501           bool error_p = false;
3502
3503           /* Restore the OLD_SCOPE since it was valid before the
3504              failed attempt at finding the last
3505              class-or-namespace-name.  */
3506           parser->scope = old_scope;
3507           parser->qualifying_scope = saved_qualifying_scope;
3508           /* If the next token is an identifier, and the one after
3509              that is a `::', then any valid interpretation would have
3510              found a class-or-namespace-name.  */
3511           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3512                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3513                      == CPP_SCOPE)
3514                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3515                      != CPP_COMPL))
3516             {
3517               token = cp_lexer_consume_token (parser->lexer);
3518               if (!error_p)
3519                 {
3520                   tree decl;
3521
3522                   decl = cp_parser_lookup_name_simple (parser, token->value);
3523                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3524                     error ("%qD used without template parameters", decl);
3525                   else
3526                     cp_parser_name_lookup_error
3527                       (parser, token->value, decl,
3528                        "is not a class or namespace");
3529                   parser->scope = NULL_TREE;
3530                   error_p = true;
3531                   /* Treat this as a successful nested-name-specifier
3532                      due to:
3533
3534                      [basic.lookup.qual]
3535
3536                      If the name found is not a class-name (clause
3537                      _class_) or namespace-name (_namespace.def_), the
3538                      program is ill-formed.  */
3539                   success = true;
3540                 }
3541               cp_lexer_consume_token (parser->lexer);
3542             }
3543           break;
3544         }
3545
3546       /* We've found one valid nested-name-specifier.  */
3547       success = true;
3548       /* Make sure we look in the right scope the next time through
3549          the loop.  */
3550       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3551                        ? TREE_TYPE (new_scope)
3552                        : new_scope);
3553       /* If it is a class scope, try to complete it; we are about to
3554          be looking up names inside the class.  */
3555       if (TYPE_P (parser->scope)
3556           /* Since checking types for dependency can be expensive,
3557              avoid doing it if the type is already complete.  */
3558           && !COMPLETE_TYPE_P (parser->scope)
3559           /* Do not try to complete dependent types.  */
3560           && !dependent_type_p (parser->scope))
3561         complete_type (parser->scope);
3562     }
3563
3564   /* Retrieve any deferred checks.  Do not pop this access checks yet
3565      so the memory will not be reclaimed during token replacing below.  */
3566   access_check = get_deferred_access_checks ();
3567
3568   /* If parsing tentatively, replace the sequence of tokens that makes
3569      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3570      token.  That way, should we re-parse the token stream, we will
3571      not have to repeat the effort required to do the parse, nor will
3572      we issue duplicate error messages.  */
3573   if (success && start >= 0)
3574     {
3575       /* Find the token that corresponds to the start of the
3576          template-id.  */
3577       token = cp_lexer_advance_token (parser->lexer,
3578                                       parser->lexer->buffer,
3579                                       start);
3580
3581       /* Reset the contents of the START token.  */
3582       token->type = CPP_NESTED_NAME_SPECIFIER;
3583       token->value = build_tree_list (access_check, parser->scope);
3584       TREE_TYPE (token->value) = parser->qualifying_scope;
3585       token->keyword = RID_MAX;
3586       /* Purge all subsequent tokens.  */
3587       cp_lexer_purge_tokens_after (parser->lexer, token);
3588     }
3589
3590   pop_deferring_access_checks ();
3591   return success ? parser->scope : NULL_TREE;
3592 }
3593
3594 /* Parse a nested-name-specifier.  See
3595    cp_parser_nested_name_specifier_opt for details.  This function
3596    behaves identically, except that it will an issue an error if no
3597    nested-name-specifier is present, and it will return
3598    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3599    is present.  */
3600
3601 static tree
3602 cp_parser_nested_name_specifier (cp_parser *parser,
3603                                  bool typename_keyword_p,
3604                                  bool check_dependency_p,
3605                                  bool type_p,
3606                                  bool is_declaration)
3607 {
3608   tree scope;
3609
3610   /* Look for the nested-name-specifier.  */
3611   scope = cp_parser_nested_name_specifier_opt (parser,
3612                                                typename_keyword_p,
3613                                                check_dependency_p,
3614                                                type_p,
3615                                                is_declaration);
3616   /* If it was not present, issue an error message.  */
3617   if (!scope)
3618     {
3619       cp_parser_error (parser, "expected nested-name-specifier");
3620       parser->scope = NULL_TREE;
3621       return error_mark_node;
3622     }
3623
3624   return scope;
3625 }
3626
3627 /* Parse a class-or-namespace-name.
3628
3629    class-or-namespace-name:
3630      class-name
3631      namespace-name
3632
3633    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3634    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3635    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3636    TYPE_P is TRUE iff the next name should be taken as a class-name,
3637    even the same name is declared to be another entity in the same
3638    scope.
3639
3640    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3641    specified by the class-or-namespace-name.  If neither is found the
3642    ERROR_MARK_NODE is returned.  */
3643
3644 static tree
3645 cp_parser_class_or_namespace_name (cp_parser *parser,
3646                                    bool typename_keyword_p,
3647                                    bool template_keyword_p,
3648                                    bool check_dependency_p,
3649                                    bool type_p,
3650                                    bool is_declaration)
3651 {
3652   tree saved_scope;
3653   tree saved_qualifying_scope;
3654   tree saved_object_scope;
3655   tree scope;
3656   bool only_class_p;
3657
3658   /* Before we try to parse the class-name, we must save away the
3659      current PARSER->SCOPE since cp_parser_class_name will destroy
3660      it.  */
3661   saved_scope = parser->scope;
3662   saved_qualifying_scope = parser->qualifying_scope;
3663   saved_object_scope = parser->object_scope;
3664   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3665      there is no need to look for a namespace-name.  */
3666   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3667   if (!only_class_p)
3668     cp_parser_parse_tentatively (parser);
3669   scope = cp_parser_class_name (parser,
3670                                 typename_keyword_p,
3671                                 template_keyword_p,
3672                                 type_p,
3673                                 check_dependency_p,
3674                                 /*class_head_p=*/false,
3675                                 is_declaration);
3676   /* If that didn't work, try for a namespace-name.  */
3677   if (!only_class_p && !cp_parser_parse_definitely (parser))
3678     {
3679       /* Restore the saved scope.  */
3680       parser->scope = saved_scope;
3681       parser->qualifying_scope = saved_qualifying_scope;
3682       parser->object_scope = saved_object_scope;
3683       /* If we are not looking at an identifier followed by the scope
3684          resolution operator, then this is not part of a
3685          nested-name-specifier.  (Note that this function is only used
3686          to parse the components of a nested-name-specifier.)  */
3687       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3688           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3689         return error_mark_node;
3690       scope = cp_parser_namespace_name (parser);
3691     }
3692
3693   return scope;
3694 }
3695
3696 /* Parse a postfix-expression.
3697
3698    postfix-expression:
3699      primary-expression
3700      postfix-expression [ expression ]
3701      postfix-expression ( expression-list [opt] )
3702      simple-type-specifier ( expression-list [opt] )
3703      typename :: [opt] nested-name-specifier identifier
3704        ( expression-list [opt] )
3705      typename :: [opt] nested-name-specifier template [opt] template-id
3706        ( expression-list [opt] )
3707      postfix-expression . template [opt] id-expression
3708      postfix-expression -> template [opt] id-expression
3709      postfix-expression . pseudo-destructor-name
3710      postfix-expression -> pseudo-destructor-name
3711      postfix-expression ++
3712      postfix-expression --
3713      dynamic_cast < type-id > ( expression )
3714      static_cast < type-id > ( expression )
3715      reinterpret_cast < type-id > ( expression )
3716      const_cast < type-id > ( expression )
3717      typeid ( expression )
3718      typeid ( type-id )
3719
3720    GNU Extension:
3721
3722    postfix-expression:
3723      ( type-id ) { initializer-list , [opt] }
3724
3725    This extension is a GNU version of the C99 compound-literal
3726    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3727    but they are essentially the same concept.)
3728
3729    If ADDRESS_P is true, the postfix expression is the operand of the
3730    `&' operator.
3731
3732    Returns a representation of the expression.  */
3733
3734 static tree
3735 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3736 {
3737   cp_token *token;
3738   enum rid keyword;
3739   cp_id_kind idk = CP_ID_KIND_NONE;
3740   tree postfix_expression = NULL_TREE;
3741   /* Non-NULL only if the current postfix-expression can be used to
3742      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3743      class used to qualify the member.  */
3744   tree qualifying_class = NULL_TREE;
3745
3746   /* Peek at the next token.  */
3747   token = cp_lexer_peek_token (parser->lexer);
3748   /* Some of the productions are determined by keywords.  */
3749   keyword = token->keyword;
3750   switch (keyword)
3751     {
3752     case RID_DYNCAST:
3753     case RID_STATCAST:
3754     case RID_REINTCAST:
3755     case RID_CONSTCAST:
3756       {
3757         tree type;
3758         tree expression;
3759         const char *saved_message;
3760
3761         /* All of these can be handled in the same way from the point
3762            of view of parsing.  Begin by consuming the token
3763            identifying the cast.  */
3764         cp_lexer_consume_token (parser->lexer);
3765
3766         /* New types cannot be defined in the cast.  */
3767         saved_message = parser->type_definition_forbidden_message;
3768         parser->type_definition_forbidden_message
3769           = "types may not be defined in casts";
3770
3771         /* Look for the opening `<'.  */
3772         cp_parser_require (parser, CPP_LESS, "`<'");
3773         /* Parse the type to which we are casting.  */
3774         type = cp_parser_type_id (parser);
3775         /* Look for the closing `>'.  */
3776         cp_parser_require (parser, CPP_GREATER, "`>'");
3777         /* Restore the old message.  */
3778         parser->type_definition_forbidden_message = saved_message;
3779
3780         /* And the expression which is being cast.  */
3781         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3782         expression = cp_parser_expression (parser);
3783         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3784
3785         /* Only type conversions to integral or enumeration types
3786            can be used in constant-expressions.  */
3787         if (parser->integral_constant_expression_p
3788             && !dependent_type_p (type)
3789             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3790             && (cp_parser_non_integral_constant_expression
3791                 (parser,
3792                  "a cast to a type other than an integral or "
3793                  "enumeration type")))
3794           return error_mark_node;
3795
3796         switch (keyword)
3797           {
3798           case RID_DYNCAST:
3799             postfix_expression
3800               = build_dynamic_cast (type, expression);
3801             break;
3802           case RID_STATCAST:
3803             postfix_expression
3804               = build_static_cast (type, expression);
3805             break;
3806           case RID_REINTCAST:
3807             postfix_expression
3808               = build_reinterpret_cast (type, expression);
3809             break;
3810           case RID_CONSTCAST:
3811             postfix_expression
3812               = build_const_cast (type, expression);
3813             break;
3814           default:
3815             gcc_unreachable ();
3816           }
3817       }
3818       break;
3819
3820     case RID_TYPEID:
3821       {
3822         tree type;
3823         const char *saved_message;
3824         bool saved_in_type_id_in_expr_p;
3825
3826         /* Consume the `typeid' token.  */
3827         cp_lexer_consume_token (parser->lexer);
3828         /* Look for the `(' token.  */
3829         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3830         /* Types cannot be defined in a `typeid' expression.  */
3831         saved_message = parser->type_definition_forbidden_message;
3832         parser->type_definition_forbidden_message
3833           = "types may not be defined in a `typeid\' expression";
3834         /* We can't be sure yet whether we're looking at a type-id or an
3835            expression.  */
3836         cp_parser_parse_tentatively (parser);
3837         /* Try a type-id first.  */
3838         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3839         parser->in_type_id_in_expr_p = true;
3840         type = cp_parser_type_id (parser);
3841         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3842         /* Look for the `)' token.  Otherwise, we can't be sure that
3843            we're not looking at an expression: consider `typeid (int
3844            (3))', for example.  */
3845         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3846         /* If all went well, simply lookup the type-id.  */
3847         if (cp_parser_parse_definitely (parser))
3848           postfix_expression = get_typeid (type);
3849         /* Otherwise, fall back to the expression variant.  */
3850         else
3851           {
3852             tree expression;
3853
3854             /* Look for an expression.  */
3855             expression = cp_parser_expression (parser);
3856             /* Compute its typeid.  */
3857             postfix_expression = build_typeid (expression);
3858             /* Look for the `)' token.  */
3859             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3860           }
3861         /* `typeid' may not appear in an integral constant expression.  */
3862         if (cp_parser_non_integral_constant_expression(parser,
3863                                                        "`typeid' operator"))
3864           return error_mark_node;
3865         /* Restore the saved message.  */
3866         parser->type_definition_forbidden_message = saved_message;
3867       }
3868       break;
3869
3870     case RID_TYPENAME:
3871       {
3872         bool template_p = false;
3873         tree id;
3874         tree type;
3875
3876         /* Consume the `typename' token.  */
3877         cp_lexer_consume_token (parser->lexer);
3878         /* Look for the optional `::' operator.  */
3879         cp_parser_global_scope_opt (parser,
3880                                     /*current_scope_valid_p=*/false);
3881         /* Look for the nested-name-specifier.  */
3882         cp_parser_nested_name_specifier (parser,
3883                                          /*typename_keyword_p=*/true,
3884                                          /*check_dependency_p=*/true,
3885                                          /*type_p=*/true,
3886                                          /*is_declaration=*/true);
3887         /* Look for the optional `template' keyword.  */
3888         template_p = cp_parser_optional_template_keyword (parser);
3889         /* We don't know whether we're looking at a template-id or an
3890            identifier.  */
3891         cp_parser_parse_tentatively (parser);
3892         /* Try a template-id.  */
3893         id = cp_parser_template_id (parser, template_p,
3894                                     /*check_dependency_p=*/true,
3895                                     /*is_declaration=*/true);
3896         /* If that didn't work, try an identifier.  */
3897         if (!cp_parser_parse_definitely (parser))
3898           id = cp_parser_identifier (parser);
3899         /* If we look up a template-id in a non-dependent qualifying
3900            scope, there's no need to create a dependent type.  */
3901         if (TREE_CODE (id) == TYPE_DECL
3902             && !dependent_type_p (parser->scope))
3903           type = TREE_TYPE (id);
3904         /* Create a TYPENAME_TYPE to represent the type to which the
3905            functional cast is being performed.  */
3906         else
3907           type = make_typename_type (parser->scope, id,
3908                                      /*complain=*/1);
3909
3910         postfix_expression = cp_parser_functional_cast (parser, type);
3911       }
3912       break;
3913
3914     default:
3915       {
3916         tree type;
3917
3918         /* If the next thing is a simple-type-specifier, we may be
3919            looking at a functional cast.  We could also be looking at
3920            an id-expression.  So, we try the functional cast, and if
3921            that doesn't work we fall back to the primary-expression.  */
3922         cp_parser_parse_tentatively (parser);
3923         /* Look for the simple-type-specifier.  */
3924         type = cp_parser_simple_type_specifier (parser,
3925                                                 /*decl_specs=*/NULL,
3926                                                 CP_PARSER_FLAGS_NONE);
3927         /* Parse the cast itself.  */
3928         if (!cp_parser_error_occurred (parser))
3929           postfix_expression
3930             = cp_parser_functional_cast (parser, type);
3931         /* If that worked, we're done.  */
3932         if (cp_parser_parse_definitely (parser))
3933           break;
3934
3935         /* If the functional-cast didn't work out, try a
3936            compound-literal.  */
3937         if (cp_parser_allow_gnu_extensions_p (parser)
3938             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3939           {
3940             tree initializer_list = NULL_TREE;
3941             bool saved_in_type_id_in_expr_p;
3942
3943             cp_parser_parse_tentatively (parser);
3944             /* Consume the `('.  */
3945             cp_lexer_consume_token (parser->lexer);
3946             /* Parse the type.  */
3947             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3948             parser->in_type_id_in_expr_p = true;
3949             type = cp_parser_type_id (parser);
3950             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3951             /* Look for the `)'.  */
3952             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3953             /* Look for the `{'.  */
3954             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3955             /* If things aren't going well, there's no need to
3956                keep going.  */
3957             if (!cp_parser_error_occurred (parser))
3958               {
3959                 bool non_constant_p;
3960                 /* Parse the initializer-list.  */
3961                 initializer_list
3962                   = cp_parser_initializer_list (parser, &non_constant_p);
3963                 /* Allow a trailing `,'.  */
3964                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3965                   cp_lexer_consume_token (parser->lexer);
3966                 /* Look for the final `}'.  */
3967                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3968               }
3969             /* If that worked, we're definitely looking at a
3970                compound-literal expression.  */
3971             if (cp_parser_parse_definitely (parser))
3972               {
3973                 /* Warn the user that a compound literal is not
3974                    allowed in standard C++.  */
3975                 if (pedantic)
3976                   pedwarn ("ISO C++ forbids compound-literals");
3977                 /* Form the representation of the compound-literal.  */
3978                 postfix_expression
3979                   = finish_compound_literal (type, initializer_list);
3980                 break;
3981               }
3982           }
3983
3984         /* It must be a primary-expression.  */
3985         postfix_expression = cp_parser_primary_expression (parser,
3986                                                            &idk,
3987                                                            &qualifying_class);
3988       }
3989       break;
3990     }
3991
3992   /* If we were avoiding committing to the processing of a
3993      qualified-id until we knew whether or not we had a
3994      pointer-to-member, we now know.  */
3995   if (qualifying_class)
3996     {
3997       bool done;
3998
3999       /* Peek at the next token.  */
4000       token = cp_lexer_peek_token (parser->lexer);
4001       done = (token->type != CPP_OPEN_SQUARE
4002               && token->type != CPP_OPEN_PAREN
4003               && token->type != CPP_DOT
4004               && token->type != CPP_DEREF
4005               && token->type != CPP_PLUS_PLUS
4006               && token->type != CPP_MINUS_MINUS);
4007
4008       postfix_expression = finish_qualified_id_expr (qualifying_class,
4009                                                      postfix_expression,
4010                                                      done,
4011                                                      address_p);
4012       if (done)
4013         return postfix_expression;
4014     }
4015
4016   /* Keep looping until the postfix-expression is complete.  */
4017   while (true)
4018     {
4019       if (idk == CP_ID_KIND_UNQUALIFIED
4020           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4021           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4022         /* It is not a Koenig lookup function call.  */
4023         postfix_expression
4024           = unqualified_name_lookup_error (postfix_expression);
4025
4026       /* Peek at the next token.  */
4027       token = cp_lexer_peek_token (parser->lexer);
4028
4029       switch (token->type)
4030         {
4031         case CPP_OPEN_SQUARE:
4032           postfix_expression
4033             = cp_parser_postfix_open_square_expression (parser,
4034                                                         postfix_expression,
4035                                                         false);
4036           idk = CP_ID_KIND_NONE;
4037           break;
4038
4039         case CPP_OPEN_PAREN:
4040           /* postfix-expression ( expression-list [opt] ) */
4041           {
4042             bool koenig_p;
4043             tree args = (cp_parser_parenthesized_expression_list
4044                          (parser, false, /*non_constant_p=*/NULL));
4045
4046             if (args == error_mark_node)
4047               {
4048                 postfix_expression = error_mark_node;
4049                 break;
4050               }
4051
4052             /* Function calls are not permitted in
4053                constant-expressions.  */
4054             if (cp_parser_non_integral_constant_expression (parser,
4055                                                             "a function call"))
4056               {
4057                 postfix_expression = error_mark_node;
4058                 break;
4059               }
4060
4061             koenig_p = false;
4062             if (idk == CP_ID_KIND_UNQUALIFIED)
4063               {
4064                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4065                   {
4066                     if (args)
4067                       {
4068                         koenig_p = true;
4069                         postfix_expression
4070                           = perform_koenig_lookup (postfix_expression, args);
4071                       }
4072                     else
4073                       postfix_expression
4074                         = unqualified_fn_lookup_error (postfix_expression);
4075                   }
4076                 /* We do not perform argument-dependent lookup if
4077                    normal lookup finds a non-function, in accordance
4078                    with the expected resolution of DR 218.  */
4079                 else if (args && is_overloaded_fn (postfix_expression))
4080                   {
4081                     tree fn = get_first_fn (postfix_expression);
4082
4083                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4084                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4085
4086                     /* Only do argument dependent lookup if regular
4087                        lookup does not find a set of member functions.
4088                        [basic.lookup.koenig]/2a  */
4089                     if (!DECL_FUNCTION_MEMBER_P (fn))
4090                       {
4091                         koenig_p = true;
4092                         postfix_expression
4093                           = perform_koenig_lookup (postfix_expression, args);
4094                       }
4095                   }
4096               }
4097
4098             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4099               {
4100                 tree instance = TREE_OPERAND (postfix_expression, 0);
4101                 tree fn = TREE_OPERAND (postfix_expression, 1);
4102
4103                 if (processing_template_decl
4104                     && (type_dependent_expression_p (instance)
4105                         || (!BASELINK_P (fn)
4106                             && TREE_CODE (fn) != FIELD_DECL)
4107                         || type_dependent_expression_p (fn)
4108                         || any_type_dependent_arguments_p (args)))
4109                   {
4110                     postfix_expression
4111                       = build_min_nt (CALL_EXPR, postfix_expression,
4112                                       args, NULL_TREE);
4113                     break;
4114                   }
4115
4116                 if (BASELINK_P (fn))
4117                   postfix_expression
4118                     = (build_new_method_call
4119                        (instance, fn, args, NULL_TREE,
4120                         (idk == CP_ID_KIND_QUALIFIED
4121                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4122                 else
4123                   postfix_expression
4124                     = finish_call_expr (postfix_expression, args,
4125                                         /*disallow_virtual=*/false,
4126                                         /*koenig_p=*/false);
4127               }
4128             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4129                      || TREE_CODE (postfix_expression) == MEMBER_REF
4130                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4131               postfix_expression = (build_offset_ref_call_from_tree
4132                                     (postfix_expression, args));
4133             else if (idk == CP_ID_KIND_QUALIFIED)
4134               /* A call to a static class member, or a namespace-scope
4135                  function.  */
4136               postfix_expression
4137                 = finish_call_expr (postfix_expression, args,
4138                                     /*disallow_virtual=*/true,
4139                                     koenig_p);
4140             else
4141               /* All other function calls.  */
4142               postfix_expression
4143                 = finish_call_expr (postfix_expression, args,
4144                                     /*disallow_virtual=*/false,
4145                                     koenig_p);
4146
4147             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4148             idk = CP_ID_KIND_NONE;
4149           }
4150           break;
4151
4152         case CPP_DOT:
4153         case CPP_DEREF:
4154           /* postfix-expression . template [opt] id-expression
4155              postfix-expression . pseudo-destructor-name
4156              postfix-expression -> template [opt] id-expression
4157              postfix-expression -> pseudo-destructor-name */
4158
4159           /* Consume the `.' or `->' operator.  */
4160           cp_lexer_consume_token (parser->lexer);
4161
4162           postfix_expression
4163             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4164                                                       postfix_expression,
4165                                                       false, &idk);
4166           break;
4167
4168         case CPP_PLUS_PLUS:
4169           /* postfix-expression ++  */
4170           /* Consume the `++' token.  */
4171           cp_lexer_consume_token (parser->lexer);
4172           /* Generate a representation for the complete expression.  */
4173           postfix_expression
4174             = finish_increment_expr (postfix_expression,
4175                                      POSTINCREMENT_EXPR);
4176           /* Increments may not appear in constant-expressions.  */
4177           if (cp_parser_non_integral_constant_expression (parser,
4178                                                           "an increment"))
4179             postfix_expression = error_mark_node;
4180           idk = CP_ID_KIND_NONE;
4181           break;
4182
4183         case CPP_MINUS_MINUS:
4184           /* postfix-expression -- */
4185           /* Consume the `--' token.  */
4186           cp_lexer_consume_token (parser->lexer);
4187           /* Generate a representation for the complete expression.  */
4188           postfix_expression
4189             = finish_increment_expr (postfix_expression,
4190                                      POSTDECREMENT_EXPR);
4191           /* Decrements may not appear in constant-expressions.  */
4192           if (cp_parser_non_integral_constant_expression (parser,
4193                                                           "a decrement"))
4194             postfix_expression = error_mark_node;
4195           idk = CP_ID_KIND_NONE;
4196           break;
4197
4198         default:
4199           return postfix_expression;
4200         }
4201     }
4202
4203   /* We should never get here.  */
4204   gcc_unreachable ();
4205   return error_mark_node;
4206 }
4207
4208 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4209    by cp_parser_builtin_offsetof.  We're looking for
4210
4211      postfix-expression [ expression ]
4212
4213    FOR_OFFSETOF is set if we're being called in that context, which
4214    changes how we deal with integer constant expressions.  */
4215
4216 static tree
4217 cp_parser_postfix_open_square_expression (cp_parser *parser,
4218                                           tree postfix_expression,
4219                                           bool for_offsetof)
4220 {
4221   tree index;
4222
4223   /* Consume the `[' token.  */
4224   cp_lexer_consume_token (parser->lexer);
4225
4226   /* Parse the index expression.  */
4227   /* ??? For offsetof, there is a question of what to allow here.  If
4228      offsetof is not being used in an integral constant expression context,
4229      then we *could* get the right answer by computing the value at runtime.
4230      If we are in an integral constant expression context, then we might
4231      could accept any constant expression; hard to say without analysis.
4232      Rather than open the barn door too wide right away, allow only integer
4233      constant expressions here.  */
4234   if (for_offsetof)
4235     index = cp_parser_constant_expression (parser, false, NULL);
4236   else
4237     index = cp_parser_expression (parser);
4238
4239   /* Look for the closing `]'.  */
4240   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4241
4242   /* Build the ARRAY_REF.  */
4243   postfix_expression = grok_array_decl (postfix_expression, index);
4244
4245   /* When not doing offsetof, array references are not permitted in
4246      constant-expressions.  */
4247   if (!for_offsetof
4248       && (cp_parser_non_integral_constant_expression
4249           (parser, "an array reference")))
4250     postfix_expression = error_mark_node;
4251
4252   return postfix_expression;
4253 }
4254
4255 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4256    by cp_parser_builtin_offsetof.  We're looking for
4257
4258      postfix-expression . template [opt] id-expression
4259      postfix-expression . pseudo-destructor-name
4260      postfix-expression -> template [opt] id-expression
4261      postfix-expression -> pseudo-destructor-name
4262
4263    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4264    limits what of the above we'll actually accept, but nevermind.
4265    TOKEN_TYPE is the "." or "->" token, which will already have been
4266    removed from the stream.  */
4267
4268 static tree
4269 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4270                                         enum cpp_ttype token_type,
4271                                         tree postfix_expression,
4272                                         bool for_offsetof, cp_id_kind *idk)
4273 {
4274   tree name;
4275   bool dependent_p;
4276   bool template_p;
4277   bool pseudo_destructor_p;
4278   tree scope = NULL_TREE;
4279
4280   /* If this is a `->' operator, dereference the pointer.  */
4281   if (token_type == CPP_DEREF)
4282     postfix_expression = build_x_arrow (postfix_expression);
4283   /* Check to see whether or not the expression is type-dependent.  */
4284   dependent_p = type_dependent_expression_p (postfix_expression);
4285   /* The identifier following the `->' or `.' is not qualified.  */
4286   parser->scope = NULL_TREE;
4287   parser->qualifying_scope = NULL_TREE;
4288   parser->object_scope = NULL_TREE;
4289   *idk = CP_ID_KIND_NONE;
4290   /* Enter the scope corresponding to the type of the object
4291      given by the POSTFIX_EXPRESSION.  */
4292   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4293     {
4294       scope = TREE_TYPE (postfix_expression);
4295       /* According to the standard, no expression should ever have
4296          reference type.  Unfortunately, we do not currently match
4297          the standard in this respect in that our internal representation
4298          of an expression may have reference type even when the standard
4299          says it does not.  Therefore, we have to manually obtain the
4300          underlying type here.  */
4301       scope = non_reference (scope);
4302       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4303       scope = complete_type_or_else (scope, NULL_TREE);
4304       /* Let the name lookup machinery know that we are processing a
4305          class member access expression.  */
4306       parser->context->object_type = scope;
4307       /* If something went wrong, we want to be able to discern that case,
4308          as opposed to the case where there was no SCOPE due to the type
4309          of expression being dependent.  */
4310       if (!scope)
4311         scope = error_mark_node;
4312       /* If the SCOPE was erroneous, make the various semantic analysis
4313          functions exit quickly -- and without issuing additional error
4314          messages.  */
4315       if (scope == error_mark_node)
4316         postfix_expression = error_mark_node;
4317     }
4318
4319   /* Assume this expression is not a pseudo-destructor access.  */
4320   pseudo_destructor_p = false;
4321
4322   /* If the SCOPE is a scalar type, then, if this is a valid program,
4323      we must be looking at a pseudo-destructor-name.  */
4324   if (scope && SCALAR_TYPE_P (scope))
4325     {
4326       tree s;
4327       tree type;
4328
4329       cp_parser_parse_tentatively (parser);
4330       /* Parse the pseudo-destructor-name.  */
4331       s = NULL_TREE;
4332       cp_parser_pseudo_destructor_name (parser, &s, &type);
4333       if (cp_parser_parse_definitely (parser))
4334         {
4335           pseudo_destructor_p = true;
4336           postfix_expression
4337             = finish_pseudo_destructor_expr (postfix_expression,
4338                                              s, TREE_TYPE (type));
4339         }
4340     }
4341
4342   if (!pseudo_destructor_p)
4343     {
4344       /* If the SCOPE is not a scalar type, we are looking at an
4345          ordinary class member access expression, rather than a
4346          pseudo-destructor-name.  */
4347       template_p = cp_parser_optional_template_keyword (parser);
4348       /* Parse the id-expression.  */
4349       name = cp_parser_id_expression (parser, template_p,
4350                                       /*check_dependency_p=*/true,
4351                                       /*template_p=*/NULL,
4352                                       /*declarator_p=*/false);
4353       /* In general, build a SCOPE_REF if the member name is qualified.
4354          However, if the name was not dependent and has already been
4355          resolved; there is no need to build the SCOPE_REF.  For example;
4356
4357              struct X { void f(); };
4358              template <typename T> void f(T* t) { t->X::f(); }
4359
4360          Even though "t" is dependent, "X::f" is not and has been resolved
4361          to a BASELINK; there is no need to include scope information.  */
4362
4363       /* But we do need to remember that there was an explicit scope for
4364          virtual function calls.  */
4365       if (parser->scope)
4366         *idk = CP_ID_KIND_QUALIFIED;
4367
4368       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4369         {
4370           name = build_nt (SCOPE_REF, parser->scope, name);
4371           parser->scope = NULL_TREE;
4372           parser->qualifying_scope = NULL_TREE;
4373           parser->object_scope = NULL_TREE;
4374         }
4375       if (scope && name && BASELINK_P (name))
4376         adjust_result_of_qualified_name_lookup
4377           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4378       postfix_expression
4379         = finish_class_member_access_expr (postfix_expression, name);
4380     }
4381
4382   /* We no longer need to look up names in the scope of the object on
4383      the left-hand side of the `.' or `->' operator.  */
4384   parser->context->object_type = NULL_TREE;
4385
4386   /* Outside of offsetof, these operators may not appear in
4387      constant-expressions.  */
4388   if (!for_offsetof
4389       && (cp_parser_non_integral_constant_expression
4390           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4391     postfix_expression = error_mark_node;
4392
4393   return postfix_expression;
4394 }
4395
4396 /* Parse a parenthesized expression-list.
4397
4398    expression-list:
4399      assignment-expression
4400      expression-list, assignment-expression
4401
4402    attribute-list:
4403      expression-list
4404      identifier
4405      identifier, expression-list
4406
4407    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4408    representation of an assignment-expression.  Note that a TREE_LIST
4409    is returned even if there is only a single expression in the list.
4410    error_mark_node is returned if the ( and or ) are
4411    missing. NULL_TREE is returned on no expressions. The parentheses
4412    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4413    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4414    indicates whether or not all of the expressions in the list were
4415    constant.  */
4416
4417 static tree
4418 cp_parser_parenthesized_expression_list (cp_parser* parser,
4419                                          bool is_attribute_list,
4420                                          bool *non_constant_p)
4421 {
4422   tree expression_list = NULL_TREE;
4423   bool fold_expr_p = is_attribute_list;
4424   tree identifier = NULL_TREE;
4425
4426   /* Assume all the expressions will be constant.  */
4427   if (non_constant_p)
4428     *non_constant_p = false;
4429
4430   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4431     return error_mark_node;
4432
4433   /* Consume expressions until there are no more.  */
4434   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4435     while (true)
4436       {
4437         tree expr;
4438
4439         /* At the beginning of attribute lists, check to see if the
4440            next token is an identifier.  */
4441         if (is_attribute_list
4442             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4443           {
4444             cp_token *token;
4445
4446             /* Consume the identifier.  */
4447             token = cp_lexer_consume_token (parser->lexer);
4448             /* Save the identifier.  */
4449             identifier = token->value;
4450           }
4451         else
4452           {
4453             /* Parse the next assignment-expression.  */
4454             if (non_constant_p)
4455               {
4456                 bool expr_non_constant_p;
4457                 expr = (cp_parser_constant_expression
4458                         (parser, /*allow_non_constant_p=*/true,
4459                          &expr_non_constant_p));
4460                 if (expr_non_constant_p)
4461                   *non_constant_p = true;
4462               }
4463             else
4464               expr = cp_parser_assignment_expression (parser);
4465
4466             if (fold_expr_p)
4467               expr = fold_non_dependent_expr (expr);
4468
4469              /* Add it to the list.  We add error_mark_node
4470                 expressions to the list, so that we can still tell if
4471                 the correct form for a parenthesized expression-list
4472                 is found. That gives better errors.  */
4473             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4474
4475             if (expr == error_mark_node)
4476               goto skip_comma;
4477           }
4478
4479         /* After the first item, attribute lists look the same as
4480            expression lists.  */
4481         is_attribute_list = false;
4482
4483       get_comma:;
4484         /* If the next token isn't a `,', then we are done.  */
4485         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4486           break;
4487
4488         /* Otherwise, consume the `,' and keep going.  */
4489         cp_lexer_consume_token (parser->lexer);
4490       }
4491
4492   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4493     {
4494       int ending;
4495
4496     skip_comma:;
4497       /* We try and resync to an unnested comma, as that will give the
4498          user better diagnostics.  */
4499       ending = cp_parser_skip_to_closing_parenthesis (parser,
4500                                                       /*recovering=*/true,
4501                                                       /*or_comma=*/true,
4502                                                       /*consume_paren=*/true);
4503       if (ending < 0)
4504         goto get_comma;
4505       if (!ending)
4506         return error_mark_node;
4507     }
4508
4509   /* We built up the list in reverse order so we must reverse it now.  */
4510   expression_list = nreverse (expression_list);
4511   if (identifier)
4512     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4513
4514   return expression_list;
4515 }
4516
4517 /* Parse a pseudo-destructor-name.
4518
4519    pseudo-destructor-name:
4520      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4521      :: [opt] nested-name-specifier template template-id :: ~ type-name
4522      :: [opt] nested-name-specifier [opt] ~ type-name
4523
4524    If either of the first two productions is used, sets *SCOPE to the
4525    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4526    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4527    or ERROR_MARK_NODE if the parse fails.  */
4528
4529 static void
4530 cp_parser_pseudo_destructor_name (cp_parser* parser,
4531                                   tree* scope,
4532                                   tree* type)
4533 {
4534   bool nested_name_specifier_p;
4535
4536   /* Assume that things will not work out.  */
4537   *type = error_mark_node;
4538
4539   /* Look for the optional `::' operator.  */
4540   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4541   /* Look for the optional nested-name-specifier.  */
4542   nested_name_specifier_p
4543     = (cp_parser_nested_name_specifier_opt (parser,
4544                                             /*typename_keyword_p=*/false,
4545                                             /*check_dependency_p=*/true,
4546                                             /*type_p=*/false,
4547                                             /*is_declaration=*/true)
4548        != NULL_TREE);
4549   /* Now, if we saw a nested-name-specifier, we might be doing the
4550      second production.  */
4551   if (nested_name_specifier_p
4552       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4553     {
4554       /* Consume the `template' keyword.  */
4555       cp_lexer_consume_token (parser->lexer);
4556       /* Parse the template-id.  */
4557       cp_parser_template_id (parser,
4558                              /*template_keyword_p=*/true,
4559                              /*check_dependency_p=*/false,
4560                              /*is_declaration=*/true);
4561       /* Look for the `::' token.  */
4562       cp_parser_require (parser, CPP_SCOPE, "`::'");
4563     }
4564   /* If the next token is not a `~', then there might be some
4565      additional qualification.  */
4566   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4567     {
4568       /* Look for the type-name.  */
4569       *scope = TREE_TYPE (cp_parser_type_name (parser));
4570
4571       if (*scope == error_mark_node)
4572         return;
4573
4574       /* If we don't have ::~, then something has gone wrong.  Since
4575          the only caller of this function is looking for something
4576          after `.' or `->' after a scalar type, most likely the
4577          program is trying to get a member of a non-aggregate
4578          type.  */
4579       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4580           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4581         {
4582           cp_parser_error (parser, "request for member of non-aggregate type");
4583           return;
4584         }
4585
4586       /* Look for the `::' token.  */
4587       cp_parser_require (parser, CPP_SCOPE, "`::'");
4588     }
4589   else
4590     *scope = NULL_TREE;
4591
4592   /* Look for the `~'.  */
4593   cp_parser_require (parser, CPP_COMPL, "`~'");
4594   /* Look for the type-name again.  We are not responsible for
4595      checking that it matches the first type-name.  */
4596   *type = cp_parser_type_name (parser);
4597 }
4598
4599 /* Parse a unary-expression.
4600
4601    unary-expression:
4602      postfix-expression
4603      ++ cast-expression
4604      -- cast-expression
4605      unary-operator cast-expression
4606      sizeof unary-expression
4607      sizeof ( type-id )
4608      new-expression
4609      delete-expression
4610
4611    GNU Extensions:
4612
4613    unary-expression:
4614      __extension__ cast-expression
4615      __alignof__ unary-expression
4616      __alignof__ ( type-id )
4617      __real__ cast-expression
4618      __imag__ cast-expression
4619      && identifier
4620
4621    ADDRESS_P is true iff the unary-expression is appearing as the
4622    operand of the `&' operator.
4623
4624    Returns a representation of the expression.  */
4625
4626 static tree
4627 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4628 {
4629   cp_token *token;
4630   enum tree_code unary_operator;
4631
4632   /* Peek at the next token.  */
4633   token = cp_lexer_peek_token (parser->lexer);
4634   /* Some keywords give away the kind of expression.  */
4635   if (token->type == CPP_KEYWORD)
4636     {
4637       enum rid keyword = token->keyword;
4638
4639       switch (keyword)
4640         {
4641         case RID_ALIGNOF:
4642         case RID_SIZEOF:
4643           {
4644             tree operand;
4645             enum tree_code op;
4646
4647             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4648             /* Consume the token.  */
4649             cp_lexer_consume_token (parser->lexer);
4650             /* Parse the operand.  */
4651             operand = cp_parser_sizeof_operand (parser, keyword);
4652
4653             if (TYPE_P (operand))
4654               return cxx_sizeof_or_alignof_type (operand, op, true);
4655             else
4656               return cxx_sizeof_or_alignof_expr (operand, op);
4657           }
4658
4659         case RID_NEW:
4660           return cp_parser_new_expression (parser);
4661
4662         case RID_DELETE:
4663           return cp_parser_delete_expression (parser);
4664
4665         case RID_EXTENSION:
4666           {
4667             /* The saved value of the PEDANTIC flag.  */
4668             int saved_pedantic;
4669             tree expr;
4670
4671             /* Save away the PEDANTIC flag.  */
4672             cp_parser_extension_opt (parser, &saved_pedantic);
4673             /* Parse the cast-expression.  */
4674             expr = cp_parser_simple_cast_expression (parser);
4675             /* Restore the PEDANTIC flag.  */
4676             pedantic = saved_pedantic;
4677
4678             return expr;
4679           }
4680
4681         case RID_REALPART:
4682         case RID_IMAGPART:
4683           {
4684             tree expression;
4685
4686             /* Consume the `__real__' or `__imag__' token.  */
4687             cp_lexer_consume_token (parser->lexer);
4688             /* Parse the cast-expression.  */
4689             expression = cp_parser_simple_cast_expression (parser);
4690             /* Create the complete representation.  */
4691             return build_x_unary_op ((keyword == RID_REALPART
4692                                       ? REALPART_EXPR : IMAGPART_EXPR),
4693                                      expression);
4694           }
4695           break;
4696
4697         default:
4698           break;
4699         }
4700     }
4701
4702   /* Look for the `:: new' and `:: delete', which also signal the
4703      beginning of a new-expression, or delete-expression,
4704      respectively.  If the next token is `::', then it might be one of
4705      these.  */
4706   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4707     {
4708       enum rid keyword;
4709
4710       /* See if the token after the `::' is one of the keywords in
4711          which we're interested.  */
4712       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4713       /* If it's `new', we have a new-expression.  */
4714       if (keyword == RID_NEW)
4715         return cp_parser_new_expression (parser);
4716       /* Similarly, for `delete'.  */
4717       else if (keyword == RID_DELETE)
4718         return cp_parser_delete_expression (parser);
4719     }
4720
4721   /* Look for a unary operator.  */
4722   unary_operator = cp_parser_unary_operator (token);
4723   /* The `++' and `--' operators can be handled similarly, even though
4724      they are not technically unary-operators in the grammar.  */
4725   if (unary_operator == ERROR_MARK)
4726     {
4727       if (token->type == CPP_PLUS_PLUS)
4728         unary_operator = PREINCREMENT_EXPR;
4729       else if (token->type == CPP_MINUS_MINUS)
4730         unary_operator = PREDECREMENT_EXPR;
4731       /* Handle the GNU address-of-label extension.  */
4732       else if (cp_parser_allow_gnu_extensions_p (parser)
4733                && token->type == CPP_AND_AND)
4734         {
4735           tree identifier;
4736
4737           /* Consume the '&&' token.  */
4738           cp_lexer_consume_token (parser->lexer);
4739           /* Look for the identifier.  */
4740           identifier = cp_parser_identifier (parser);
4741           /* Create an expression representing the address.  */
4742           return finish_label_address_expr (identifier);
4743         }
4744     }
4745   if (unary_operator != ERROR_MARK)
4746     {
4747       tree cast_expression;
4748       tree expression = error_mark_node;
4749       const char *non_constant_p = NULL;
4750
4751       /* Consume the operator token.  */
4752       token = cp_lexer_consume_token (parser->lexer);
4753       /* Parse the cast-expression.  */
4754       cast_expression
4755         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4756       /* Now, build an appropriate representation.  */
4757       switch (unary_operator)
4758         {
4759         case INDIRECT_REF:
4760           non_constant_p = "`*'";
4761           expression = build_x_indirect_ref (cast_expression, "unary *");
4762           break;
4763
4764         case ADDR_EXPR:
4765           non_constant_p = "`&'";
4766           /* Fall through.  */
4767         case BIT_NOT_EXPR:
4768           expression = build_x_unary_op (unary_operator, cast_expression);
4769           break;
4770
4771         case PREINCREMENT_EXPR:
4772         case PREDECREMENT_EXPR:
4773           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4774                             ? "`++'" : "`--'");
4775           /* Fall through.  */
4776         case CONVERT_EXPR:
4777         case NEGATE_EXPR:
4778         case TRUTH_NOT_EXPR:
4779           expression = finish_unary_op_expr (unary_operator, cast_expression);
4780           break;
4781
4782         default:
4783           gcc_unreachable ();
4784         }
4785
4786       if (non_constant_p
4787           && cp_parser_non_integral_constant_expression (parser,
4788                                                          non_constant_p))
4789         expression = error_mark_node;
4790
4791       return expression;
4792     }
4793
4794   return cp_parser_postfix_expression (parser, address_p);
4795 }
4796
4797 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4798    unary-operator, the corresponding tree code is returned.  */
4799
4800 static enum tree_code
4801 cp_parser_unary_operator (cp_token* token)
4802 {
4803   switch (token->type)
4804     {
4805     case CPP_MULT:
4806       return INDIRECT_REF;
4807
4808     case CPP_AND:
4809       return ADDR_EXPR;
4810
4811     case CPP_PLUS:
4812       return CONVERT_EXPR;
4813
4814     case CPP_MINUS:
4815       return NEGATE_EXPR;
4816
4817     case CPP_NOT:
4818       return TRUTH_NOT_EXPR;
4819
4820     case CPP_COMPL:
4821       return BIT_NOT_EXPR;
4822
4823     default:
4824       return ERROR_MARK;
4825     }
4826 }
4827
4828 /* Parse a new-expression.
4829
4830    new-expression:
4831      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4832      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4833
4834    Returns a representation of the expression.  */
4835
4836 static tree
4837 cp_parser_new_expression (cp_parser* parser)
4838 {
4839   bool global_scope_p;
4840   tree placement;
4841   tree type;
4842   tree initializer;
4843   tree nelts;
4844
4845   /* Look for the optional `::' operator.  */
4846   global_scope_p
4847     = (cp_parser_global_scope_opt (parser,
4848                                    /*current_scope_valid_p=*/false)
4849        != NULL_TREE);
4850   /* Look for the `new' operator.  */
4851   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4852   /* There's no easy way to tell a new-placement from the
4853      `( type-id )' construct.  */
4854   cp_parser_parse_tentatively (parser);
4855   /* Look for a new-placement.  */
4856   placement = cp_parser_new_placement (parser);
4857   /* If that didn't work out, there's no new-placement.  */
4858   if (!cp_parser_parse_definitely (parser))
4859     placement = NULL_TREE;
4860
4861   /* If the next token is a `(', then we have a parenthesized
4862      type-id.  */
4863   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4864     {
4865       /* Consume the `('.  */
4866       cp_lexer_consume_token (parser->lexer);
4867       /* Parse the type-id.  */
4868       type = cp_parser_type_id (parser);
4869       /* Look for the closing `)'.  */
4870       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4871       /* There should not be a direct-new-declarator in this production,
4872          but GCC used to allowed this, so we check and emit a sensible error
4873          message for this case.  */
4874       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4875         {
4876           error ("array bound forbidden after parenthesized type-id");
4877           inform ("try removing the parentheses around the type-id");
4878           cp_parser_direct_new_declarator (parser);
4879         }
4880       nelts = NULL_TREE;
4881     }
4882   /* Otherwise, there must be a new-type-id.  */
4883   else
4884     type = cp_parser_new_type_id (parser, &nelts);
4885
4886   /* If the next token is a `(', then we have a new-initializer.  */
4887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4888     initializer = cp_parser_new_initializer (parser);
4889   else
4890     initializer = NULL_TREE;
4891
4892   /* A new-expression may not appear in an integral constant
4893      expression.  */
4894   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4895     return error_mark_node;
4896
4897   /* Create a representation of the new-expression.  */
4898   return build_new (placement, type, nelts, initializer, global_scope_p);
4899 }
4900
4901 /* Parse a new-placement.
4902
4903    new-placement:
4904      ( expression-list )
4905
4906    Returns the same representation as for an expression-list.  */
4907
4908 static tree
4909 cp_parser_new_placement (cp_parser* parser)
4910 {
4911   tree expression_list;
4912
4913   /* Parse the expression-list.  */
4914   expression_list = (cp_parser_parenthesized_expression_list
4915                      (parser, false, /*non_constant_p=*/NULL));
4916
4917   return expression_list;
4918 }
4919
4920 /* Parse a new-type-id.
4921
4922    new-type-id:
4923      type-specifier-seq new-declarator [opt]
4924
4925    Returns the TYPE allocated.  If the new-type-id indicates an array
4926    type, *NELTS is set to the number of elements in the last array
4927    bound; the TYPE will not include the last array bound.  */
4928
4929 static tree
4930 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4931 {
4932   cp_decl_specifier_seq type_specifier_seq;
4933   cp_declarator *new_declarator;
4934   cp_declarator *declarator;
4935   cp_declarator *outer_declarator;
4936   const char *saved_message;
4937   tree type;
4938
4939   /* The type-specifier sequence must not contain type definitions.
4940      (It cannot contain declarations of new types either, but if they
4941      are not definitions we will catch that because they are not
4942      complete.)  */
4943   saved_message = parser->type_definition_forbidden_message;
4944   parser->type_definition_forbidden_message
4945     = "types may not be defined in a new-type-id";
4946   /* Parse the type-specifier-seq.  */
4947   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4948   /* Restore the old message.  */
4949   parser->type_definition_forbidden_message = saved_message;
4950   /* Parse the new-declarator.  */
4951   new_declarator = cp_parser_new_declarator_opt (parser);
4952
4953   /* Determine the number of elements in the last array dimension, if
4954      any.  */
4955   *nelts = NULL_TREE;
4956   /* Skip down to the last array dimension.  */
4957   declarator = new_declarator;
4958   outer_declarator = NULL;
4959   while (declarator && (declarator->kind == cdk_pointer
4960                         || declarator->kind == cdk_ptrmem))
4961     {
4962       outer_declarator = declarator;
4963       declarator = declarator->declarator;
4964     }
4965   while (declarator
4966          && declarator->kind == cdk_array
4967          && declarator->declarator
4968          && declarator->declarator->kind == cdk_array)
4969     {
4970       outer_declarator = declarator;
4971       declarator = declarator->declarator;
4972     }
4973
4974   if (declarator && declarator->kind == cdk_array)
4975     {
4976       *nelts = declarator->u.array.bounds;
4977       if (*nelts == error_mark_node)
4978         *nelts = integer_one_node;
4979       else if (!processing_template_decl)
4980         {
4981           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4982                                            false))
4983             pedwarn ("size in array new must have integral type");
4984           *nelts = save_expr (cp_convert (sizetype, *nelts));
4985           if (*nelts == integer_zero_node)
4986             warning ("zero size array reserves no space");
4987         }
4988       if (outer_declarator)
4989         outer_declarator->declarator = declarator->declarator;
4990       else
4991         new_declarator = NULL;
4992     }
4993
4994   type = groktypename (&type_specifier_seq, new_declarator);
4995   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4996     {
4997       *nelts = array_type_nelts_top (type);
4998       type = TREE_TYPE (type);
4999     }
5000   return type;
5001 }
5002
5003 /* Parse an (optional) new-declarator.
5004
5005    new-declarator:
5006      ptr-operator new-declarator [opt]
5007      direct-new-declarator
5008
5009    Returns the declarator.  */
5010
5011 static cp_declarator *
5012 cp_parser_new_declarator_opt (cp_parser* parser)
5013 {
5014   enum tree_code code;
5015   tree type;
5016   cp_cv_quals cv_quals;
5017
5018   /* We don't know if there's a ptr-operator next, or not.  */
5019   cp_parser_parse_tentatively (parser);
5020   /* Look for a ptr-operator.  */
5021   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5022   /* If that worked, look for more new-declarators.  */
5023   if (cp_parser_parse_definitely (parser))
5024     {
5025       cp_declarator *declarator;
5026
5027       /* Parse another optional declarator.  */
5028       declarator = cp_parser_new_declarator_opt (parser);
5029
5030       /* Create the representation of the declarator.  */
5031       if (type)
5032         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5033       else if (code == INDIRECT_REF)
5034         declarator = make_pointer_declarator (cv_quals, declarator);
5035       else
5036         declarator = make_reference_declarator (cv_quals, declarator);
5037
5038       return declarator;
5039     }
5040
5041   /* If the next token is a `[', there is a direct-new-declarator.  */
5042   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5043     return cp_parser_direct_new_declarator (parser);
5044
5045   return NULL;
5046 }
5047
5048 /* Parse a direct-new-declarator.
5049
5050    direct-new-declarator:
5051      [ expression ]
5052      direct-new-declarator [constant-expression]
5053
5054    */
5055
5056 static cp_declarator *
5057 cp_parser_direct_new_declarator (cp_parser* parser)
5058 {
5059   cp_declarator *declarator = NULL;
5060
5061   while (true)
5062     {
5063       tree expression;
5064
5065       /* Look for the opening `['.  */
5066       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5067       /* The first expression is not required to be constant.  */
5068       if (!declarator)
5069         {
5070           expression = cp_parser_expression (parser);
5071           /* The standard requires that the expression have integral
5072              type.  DR 74 adds enumeration types.  We believe that the
5073              real intent is that these expressions be handled like the
5074              expression in a `switch' condition, which also allows
5075              classes with a single conversion to integral or
5076              enumeration type.  */
5077           if (!processing_template_decl)
5078             {
5079               expression
5080                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5081                                               expression,
5082                                               /*complain=*/true);
5083               if (!expression)
5084                 {
5085                   error ("expression in new-declarator must have integral "
5086                          "or enumeration type");
5087                   expression = error_mark_node;
5088                 }
5089             }
5090         }
5091       /* But all the other expressions must be.  */
5092       else
5093         expression
5094           = cp_parser_constant_expression (parser,
5095                                            /*allow_non_constant=*/false,
5096                                            NULL);
5097       /* Look for the closing `]'.  */
5098       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5099
5100       /* Add this bound to the declarator.  */
5101       declarator = make_array_declarator (declarator, expression);
5102
5103       /* If the next token is not a `[', then there are no more
5104          bounds.  */
5105       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5106         break;
5107     }
5108
5109   return declarator;
5110 }
5111
5112 /* Parse a new-initializer.
5113
5114    new-initializer:
5115      ( expression-list [opt] )
5116
5117    Returns a representation of the expression-list.  If there is no
5118    expression-list, VOID_ZERO_NODE is returned.  */
5119
5120 static tree
5121 cp_parser_new_initializer (cp_parser* parser)
5122 {
5123   tree expression_list;
5124
5125   expression_list = (cp_parser_parenthesized_expression_list
5126                      (parser, false, /*non_constant_p=*/NULL));
5127   if (!expression_list)
5128     expression_list = void_zero_node;
5129
5130   return expression_list;
5131 }
5132
5133 /* Parse a delete-expression.
5134
5135    delete-expression:
5136      :: [opt] delete cast-expression
5137      :: [opt] delete [ ] cast-expression
5138
5139    Returns a representation of the expression.  */
5140
5141 static tree
5142 cp_parser_delete_expression (cp_parser* parser)
5143 {
5144   bool global_scope_p;
5145   bool array_p;
5146   tree expression;
5147
5148   /* Look for the optional `::' operator.  */
5149   global_scope_p
5150     = (cp_parser_global_scope_opt (parser,
5151                                    /*current_scope_valid_p=*/false)
5152        != NULL_TREE);
5153   /* Look for the `delete' keyword.  */
5154   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5155   /* See if the array syntax is in use.  */
5156   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5157     {
5158       /* Consume the `[' token.  */
5159       cp_lexer_consume_token (parser->lexer);
5160       /* Look for the `]' token.  */
5161       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5162       /* Remember that this is the `[]' construct.  */
5163       array_p = true;
5164     }
5165   else
5166     array_p = false;
5167
5168   /* Parse the cast-expression.  */
5169   expression = cp_parser_simple_cast_expression (parser);
5170
5171   /* A delete-expression may not appear in an integral constant
5172      expression.  */
5173   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5174     return error_mark_node;
5175
5176   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5177 }
5178
5179 /* Parse a cast-expression.
5180
5181    cast-expression:
5182      unary-expression
5183      ( type-id ) cast-expression
5184
5185    Returns a representation of the expression.  */
5186
5187 static tree
5188 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5189 {
5190   /* If it's a `(', then we might be looking at a cast.  */
5191   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5192     {
5193       tree type = NULL_TREE;
5194       tree expr = NULL_TREE;
5195       bool compound_literal_p;
5196       const char *saved_message;
5197
5198       /* There's no way to know yet whether or not this is a cast.
5199          For example, `(int (3))' is a unary-expression, while `(int)
5200          3' is a cast.  So, we resort to parsing tentatively.  */
5201       cp_parser_parse_tentatively (parser);
5202       /* Types may not be defined in a cast.  */
5203       saved_message = parser->type_definition_forbidden_message;
5204       parser->type_definition_forbidden_message
5205         = "types may not be defined in casts";
5206       /* Consume the `('.  */
5207       cp_lexer_consume_token (parser->lexer);
5208       /* A very tricky bit is that `(struct S) { 3 }' is a
5209          compound-literal (which we permit in C++ as an extension).
5210          But, that construct is not a cast-expression -- it is a
5211          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5212          is legal; if the compound-literal were a cast-expression,
5213          you'd need an extra set of parentheses.)  But, if we parse
5214          the type-id, and it happens to be a class-specifier, then we
5215          will commit to the parse at that point, because we cannot
5216          undo the action that is done when creating a new class.  So,
5217          then we cannot back up and do a postfix-expression.
5218
5219          Therefore, we scan ahead to the closing `)', and check to see
5220          if the token after the `)' is a `{'.  If so, we are not
5221          looking at a cast-expression.
5222
5223          Save tokens so that we can put them back.  */
5224       cp_lexer_save_tokens (parser->lexer);
5225       /* Skip tokens until the next token is a closing parenthesis.
5226          If we find the closing `)', and the next token is a `{', then
5227          we are looking at a compound-literal.  */
5228       compound_literal_p
5229         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5230                                                   /*consume_paren=*/true)
5231            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5232       /* Roll back the tokens we skipped.  */
5233       cp_lexer_rollback_tokens (parser->lexer);
5234       /* If we were looking at a compound-literal, simulate an error
5235          so that the call to cp_parser_parse_definitely below will
5236          fail.  */
5237       if (compound_literal_p)
5238         cp_parser_simulate_error (parser);
5239       else
5240         {
5241           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5242           parser->in_type_id_in_expr_p = true;
5243           /* Look for the type-id.  */
5244           type = cp_parser_type_id (parser);
5245           /* Look for the closing `)'.  */
5246           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5247           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5248         }
5249
5250       /* Restore the saved message.  */
5251       parser->type_definition_forbidden_message = saved_message;
5252
5253       /* If ok so far, parse the dependent expression. We cannot be
5254          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5255          ctor of T, but looks like a cast to function returning T
5256          without a dependent expression.  */
5257       if (!cp_parser_error_occurred (parser))
5258         expr = cp_parser_simple_cast_expression (parser);
5259
5260       if (cp_parser_parse_definitely (parser))
5261         {
5262           /* Warn about old-style casts, if so requested.  */
5263           if (warn_old_style_cast
5264               && !in_system_header
5265               && !VOID_TYPE_P (type)
5266               && current_lang_name != lang_name_c)
5267             warning ("use of old-style cast");
5268
5269           /* Only type conversions to integral or enumeration types
5270              can be used in constant-expressions.  */
5271           if (parser->integral_constant_expression_p
5272               && !dependent_type_p (type)
5273               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5274               && (cp_parser_non_integral_constant_expression
5275                   (parser,
5276                    "a cast to a type other than an integral or "
5277                    "enumeration type")))
5278             return error_mark_node;
5279
5280           /* Perform the cast.  */
5281           expr = build_c_cast (type, expr);
5282           return expr;
5283         }
5284     }
5285
5286   /* If we get here, then it's not a cast, so it must be a
5287      unary-expression.  */
5288   return cp_parser_unary_expression (parser, address_p);
5289 }
5290
5291 /* Parse a binary expression of the general form:
5292
5293    pm-expression:
5294      cast-expression
5295      pm-expression .* cast-expression
5296      pm-expression ->* cast-expression
5297
5298    multiplicative-expression:
5299      pm-expression
5300      multiplicative-expression * pm-expression
5301      multiplicative-expression / pm-expression
5302      multiplicative-expression % pm-expression
5303
5304    additive-expression:
5305      multiplicative-expression
5306      additive-expression + multiplicative-expression
5307      additive-expression - multiplicative-expression
5308
5309    shift-expression:
5310      additive-expression
5311      shift-expression << additive-expression
5312      shift-expression >> additive-expression
5313
5314    relational-expression:
5315      shift-expression
5316      relational-expression < shift-expression
5317      relational-expression > shift-expression
5318      relational-expression <= shift-expression
5319      relational-expression >= shift-expression
5320
5321   GNU Extension:
5322   
5323    relational-expression:
5324      relational-expression <? shift-expression
5325      relational-expression >? shift-expression
5326
5327    equality-expression:
5328      relational-expression
5329      equality-expression == relational-expression
5330      equality-expression != relational-expression
5331
5332    and-expression:
5333      equality-expression
5334      and-expression & equality-expression
5335
5336    exclusive-or-expression:
5337      and-expression
5338      exclusive-or-expression ^ and-expression
5339
5340    inclusive-or-expression:
5341      exclusive-or-expression
5342      inclusive-or-expression | exclusive-or-expression
5343
5344    logical-and-expression:
5345      inclusive-or-expression
5346      logical-and-expression && inclusive-or-expression
5347
5348    logical-or-expression:
5349      logical-and-expression
5350      logical-or-expression || logical-and-expression
5351
5352    All these are implemented with a single function like:
5353
5354    binary-expression:
5355      simple-cast-expression
5356      binary-expression <token> binary-expression
5357
5358    The binops_by_token map is used to get the tree codes for each <token> type.
5359    binary-expressions are associated according to a precedence table.  */
5360
5361 #define TOKEN_PRECEDENCE(token) \
5362   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5363    ? PREC_NOT_OPERATOR \
5364    : binops_by_token[token->type].prec)
5365
5366 static tree
5367 cp_parser_binary_expression (cp_parser* parser)
5368 {
5369   cp_parser_expression_stack stack;
5370   cp_parser_expression_stack_entry *sp = &stack[0];
5371   tree lhs, rhs;
5372   cp_token *token;
5373   enum tree_code tree_type;
5374   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5375   bool overloaded_p;
5376
5377   /* Parse the first expression.  */
5378   lhs = cp_parser_simple_cast_expression (parser);
5379
5380   for (;;)
5381     {
5382       /* Get an operator token.  */
5383       token = cp_lexer_peek_token (parser->lexer);
5384       new_prec = TOKEN_PRECEDENCE (token);
5385
5386       /* Popping an entry off the stack means we completed a subexpression:
5387          - either we found a token which is not an operator (`>' where it is not
5388            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5389            will happen repeatedly;
5390          - or, we found an operator which has lower priority.  This is the case 
5391            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5392            parsing `3 * 4'. */
5393       if (new_prec <= prec)
5394         {
5395           if (sp == stack)
5396             break;
5397           else
5398             goto pop;
5399         }
5400
5401      get_rhs:
5402       tree_type = binops_by_token[token->type].tree_type;
5403
5404       /* We used the operator token. */
5405       cp_lexer_consume_token (parser->lexer);
5406
5407       /* Extract another operand.  It may be the RHS of this expression
5408          or the LHS of a new, higher priority expression.  */
5409       rhs = cp_parser_simple_cast_expression (parser);
5410
5411       /* Get another operator token.  Look up its precedence to avoid
5412          building a useless (immediately popped) stack entry for common
5413          cases such as 3 + 4 + 5 or 3 * 4 + 5.   */
5414       token = cp_lexer_peek_token (parser->lexer);
5415       lookahead_prec = TOKEN_PRECEDENCE (token);
5416       if (lookahead_prec > new_prec)
5417         {
5418           /* ... and prepare to parse the RHS of the new, higher priority
5419              expression.  Since precedence levels on the stack are
5420              monotonically increasing, we do not have to care about
5421              stack overflows.  */
5422           sp->prec = prec;
5423           sp->tree_type = tree_type;
5424           sp->lhs = lhs;
5425           sp++;
5426           lhs = rhs;
5427           prec = new_prec;
5428           new_prec = lookahead_prec;
5429           goto get_rhs;
5430
5431          pop:
5432           /* If the stack is not empty, we have parsed into LHS the right side
5433              (`4' in the example above) of an expression we had suspended.
5434              We can use the information on the stack to recover the LHS (`3') 
5435              from the stack together with the tree code (`MULT_EXPR'), and
5436              the precedence of the higher level subexpression
5437              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5438              which will be used to actually build the additive expression.  */
5439           --sp;
5440           prec = sp->prec;
5441           tree_type = sp->tree_type;
5442           rhs = lhs;
5443           lhs = sp->lhs;
5444         }
5445
5446       overloaded_p = false;
5447       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5448
5449       /* If the binary operator required the use of an overloaded operator,
5450          then this expression cannot be an integral constant-expression.
5451          An overloaded operator can be used even if both operands are
5452          otherwise permissible in an integral constant-expression if at
5453          least one of the operands is of enumeration type.  */
5454
5455       if (overloaded_p
5456           && (cp_parser_non_integral_constant_expression 
5457               (parser, "calls to overloaded operators")))
5458         return error_mark_node;
5459     }
5460
5461   return lhs;
5462 }
5463
5464
5465 /* Parse the `? expression : assignment-expression' part of a
5466    conditional-expression.  The LOGICAL_OR_EXPR is the
5467    logical-or-expression that started the conditional-expression.
5468    Returns a representation of the entire conditional-expression.
5469
5470    This routine is used by cp_parser_assignment_expression.
5471
5472      ? expression : assignment-expression
5473
5474    GNU Extensions:
5475
5476      ? : assignment-expression */
5477
5478 static tree
5479 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5480 {
5481   tree expr;
5482   tree assignment_expr;
5483
5484   /* Consume the `?' token.  */
5485   cp_lexer_consume_token (parser->lexer);
5486   if (cp_parser_allow_gnu_extensions_p (parser)
5487       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5488     /* Implicit true clause.  */
5489     expr = NULL_TREE;
5490   else
5491     /* Parse the expression.  */
5492     expr = cp_parser_expression (parser);
5493
5494   /* The next token should be a `:'.  */
5495   cp_parser_require (parser, CPP_COLON, "`:'");
5496   /* Parse the assignment-expression.  */
5497   assignment_expr = cp_parser_assignment_expression (parser);
5498
5499   /* Build the conditional-expression.  */
5500   return build_x_conditional_expr (logical_or_expr,
5501                                    expr,
5502                                    assignment_expr);
5503 }
5504
5505 /* Parse an assignment-expression.
5506
5507    assignment-expression:
5508      conditional-expression
5509      logical-or-expression assignment-operator assignment_expression
5510      throw-expression
5511
5512    Returns a representation for the expression.  */
5513
5514 static tree
5515 cp_parser_assignment_expression (cp_parser* parser)
5516 {
5517   tree expr;
5518
5519   /* If the next token is the `throw' keyword, then we're looking at
5520      a throw-expression.  */
5521   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5522     expr = cp_parser_throw_expression (parser);
5523   /* Otherwise, it must be that we are looking at a
5524      logical-or-expression.  */
5525   else
5526     {
5527       /* Parse the binary expressions (logical-or-expression).  */
5528       expr = cp_parser_binary_expression (parser);
5529       /* If the next token is a `?' then we're actually looking at a
5530          conditional-expression.  */
5531       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5532         return cp_parser_question_colon_clause (parser, expr);
5533       else
5534         {
5535           enum tree_code assignment_operator;
5536
5537           /* If it's an assignment-operator, we're using the second
5538              production.  */
5539           assignment_operator
5540             = cp_parser_assignment_operator_opt (parser);
5541           if (assignment_operator != ERROR_MARK)
5542             {
5543               tree rhs;
5544
5545               /* Parse the right-hand side of the assignment.  */
5546               rhs = cp_parser_assignment_expression (parser);
5547               /* An assignment may not appear in a
5548                  constant-expression.  */
5549               if (cp_parser_non_integral_constant_expression (parser,
5550                                                               "an assignment"))
5551                 return error_mark_node;
5552               /* Build the assignment expression.  */
5553               expr = build_x_modify_expr (expr,
5554                                           assignment_operator,
5555                                           rhs);
5556             }
5557         }
5558     }
5559
5560   return expr;
5561 }
5562
5563 /* Parse an (optional) assignment-operator.
5564
5565    assignment-operator: one of
5566      = *= /= %= += -= >>= <<= &= ^= |=
5567
5568    GNU Extension:
5569
5570    assignment-operator: one of
5571      <?= >?=
5572
5573    If the next token is an assignment operator, the corresponding tree
5574    code is returned, and the token is consumed.  For example, for
5575    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5576    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5577    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5578    operator, ERROR_MARK is returned.  */
5579
5580 static enum tree_code
5581 cp_parser_assignment_operator_opt (cp_parser* parser)
5582 {
5583   enum tree_code op;
5584   cp_token *token;
5585
5586   /* Peek at the next toen.  */
5587   token = cp_lexer_peek_token (parser->lexer);
5588
5589   switch (token->type)
5590     {
5591     case CPP_EQ:
5592       op = NOP_EXPR;
5593       break;
5594
5595     case CPP_MULT_EQ:
5596       op = MULT_EXPR;
5597       break;
5598
5599     case CPP_DIV_EQ:
5600       op = TRUNC_DIV_EXPR;
5601       break;
5602
5603     case CPP_MOD_EQ:
5604       op = TRUNC_MOD_EXPR;
5605       break;
5606
5607     case CPP_PLUS_EQ:
5608       op = PLUS_EXPR;
5609       break;
5610
5611     case CPP_MINUS_EQ:
5612       op = MINUS_EXPR;
5613       break;
5614
5615     case CPP_RSHIFT_EQ:
5616       op = RSHIFT_EXPR;
5617       break;
5618
5619     case CPP_LSHIFT_EQ:
5620       op = LSHIFT_EXPR;
5621       break;
5622
5623     case CPP_AND_EQ:
5624       op = BIT_AND_EXPR;
5625       break;
5626
5627     case CPP_XOR_EQ:
5628       op = BIT_XOR_EXPR;
5629       break;
5630
5631     case CPP_OR_EQ:
5632       op = BIT_IOR_EXPR;
5633       break;
5634
5635     case CPP_MIN_EQ:
5636       op = MIN_EXPR;
5637       break;
5638
5639     case CPP_MAX_EQ:
5640       op = MAX_EXPR;
5641       break;
5642
5643     default:
5644       /* Nothing else is an assignment operator.  */
5645       op = ERROR_MARK;
5646     }
5647
5648   /* If it was an assignment operator, consume it.  */
5649   if (op != ERROR_MARK)
5650     cp_lexer_consume_token (parser->lexer);
5651
5652   return op;
5653 }
5654
5655 /* Parse an expression.
5656
5657    expression:
5658      assignment-expression
5659      expression , assignment-expression
5660
5661    Returns a representation of the expression.  */
5662
5663 static tree
5664 cp_parser_expression (cp_parser* parser)
5665 {
5666   tree expression = NULL_TREE;
5667
5668   while (true)
5669     {
5670       tree assignment_expression;
5671
5672       /* Parse the next assignment-expression.  */
5673       assignment_expression
5674         = cp_parser_assignment_expression (parser);
5675       /* If this is the first assignment-expression, we can just
5676          save it away.  */
5677       if (!expression)
5678         expression = assignment_expression;
5679       else
5680         expression = build_x_compound_expr (expression,
5681                                             assignment_expression);
5682       /* If the next token is not a comma, then we are done with the
5683          expression.  */
5684       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5685         break;
5686       /* Consume the `,'.  */
5687       cp_lexer_consume_token (parser->lexer);
5688       /* A comma operator cannot appear in a constant-expression.  */
5689       if (cp_parser_non_integral_constant_expression (parser,
5690                                                       "a comma operator"))
5691         expression = error_mark_node;
5692     }
5693
5694   return expression;
5695 }
5696
5697 /* Parse a constant-expression.
5698
5699    constant-expression:
5700      conditional-expression
5701
5702   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5703   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5704   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5705   is false, NON_CONSTANT_P should be NULL.  */
5706
5707 static tree
5708 cp_parser_constant_expression (cp_parser* parser,
5709                                bool allow_non_constant_p,
5710                                bool *non_constant_p)
5711 {
5712   bool saved_integral_constant_expression_p;
5713   bool saved_allow_non_integral_constant_expression_p;
5714   bool saved_non_integral_constant_expression_p;
5715   tree expression;
5716
5717   /* It might seem that we could simply parse the
5718      conditional-expression, and then check to see if it were
5719      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5720      one that the compiler can figure out is constant, possibly after
5721      doing some simplifications or optimizations.  The standard has a
5722      precise definition of constant-expression, and we must honor
5723      that, even though it is somewhat more restrictive.
5724
5725      For example:
5726
5727        int i[(2, 3)];
5728
5729      is not a legal declaration, because `(2, 3)' is not a
5730      constant-expression.  The `,' operator is forbidden in a
5731      constant-expression.  However, GCC's constant-folding machinery
5732      will fold this operation to an INTEGER_CST for `3'.  */
5733
5734   /* Save the old settings.  */
5735   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5736   saved_allow_non_integral_constant_expression_p
5737     = parser->allow_non_integral_constant_expression_p;
5738   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5739   /* We are now parsing a constant-expression.  */
5740   parser->integral_constant_expression_p = true;
5741   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5742   parser->non_integral_constant_expression_p = false;
5743   /* Although the grammar says "conditional-expression", we parse an
5744      "assignment-expression", which also permits "throw-expression"
5745      and the use of assignment operators.  In the case that
5746      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5747      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5748      actually essential that we look for an assignment-expression.
5749      For example, cp_parser_initializer_clauses uses this function to
5750      determine whether a particular assignment-expression is in fact
5751      constant.  */
5752   expression = cp_parser_assignment_expression (parser);
5753   /* Restore the old settings.  */
5754   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5755   parser->allow_non_integral_constant_expression_p
5756     = saved_allow_non_integral_constant_expression_p;
5757   if (allow_non_constant_p)
5758     *non_constant_p = parser->non_integral_constant_expression_p;
5759   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5760
5761   return expression;
5762 }
5763
5764 /* Parse __builtin_offsetof.
5765
5766    offsetof-expression:
5767      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5768
5769    offsetof-member-designator:
5770      id-expression
5771      | offsetof-member-designator "." id-expression
5772      | offsetof-member-designator "[" expression "]"
5773 */
5774
5775 static tree
5776 cp_parser_builtin_offsetof (cp_parser *parser)
5777 {
5778   int save_ice_p, save_non_ice_p;
5779   tree type, expr;
5780   cp_id_kind dummy;
5781
5782   /* We're about to accept non-integral-constant things, but will
5783      definitely yield an integral constant expression.  Save and
5784      restore these values around our local parsing.  */
5785   save_ice_p = parser->integral_constant_expression_p;
5786   save_non_ice_p = parser->non_integral_constant_expression_p;
5787
5788   /* Consume the "__builtin_offsetof" token.  */
5789   cp_lexer_consume_token (parser->lexer);
5790   /* Consume the opening `('.  */
5791   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5792   /* Parse the type-id.  */
5793   type = cp_parser_type_id (parser);
5794   /* Look for the `,'.  */
5795   cp_parser_require (parser, CPP_COMMA, "`,'");
5796
5797   /* Build the (type *)null that begins the traditional offsetof macro.  */
5798   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5799
5800   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5801   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5802                                                  true, &dummy);
5803   while (true)
5804     {
5805       cp_token *token = cp_lexer_peek_token (parser->lexer);
5806       switch (token->type)
5807         {
5808         case CPP_OPEN_SQUARE:
5809           /* offsetof-member-designator "[" expression "]" */
5810           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5811           break;
5812
5813         case CPP_DOT:
5814           /* offsetof-member-designator "." identifier */
5815           cp_lexer_consume_token (parser->lexer);
5816           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5817                                                          true, &dummy);
5818           break;
5819
5820         case CPP_CLOSE_PAREN:
5821           /* Consume the ")" token.  */
5822           cp_lexer_consume_token (parser->lexer);
5823           goto success;
5824
5825         default:
5826           /* Error.  We know the following require will fail, but
5827              that gives the proper error message.  */
5828           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5829           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5830           expr = error_mark_node;
5831           goto failure;
5832         }
5833     }
5834
5835  success:
5836   /* If we're processing a template, we can't finish the semantics yet.
5837      Otherwise we can fold the entire expression now.  */
5838   if (processing_template_decl)
5839     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5840   else
5841     expr = fold_offsetof (expr);
5842
5843  failure:
5844   parser->integral_constant_expression_p = save_ice_p;
5845   parser->non_integral_constant_expression_p = save_non_ice_p;
5846
5847   return expr;
5848 }
5849
5850 /* Statements [gram.stmt.stmt]  */
5851
5852 /* Parse a statement.
5853
5854    statement:
5855      labeled-statement
5856      expression-statement
5857      compound-statement
5858      selection-statement
5859      iteration-statement
5860      jump-statement
5861      declaration-statement
5862      try-block  */
5863
5864 static void
5865 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5866 {
5867   tree statement;
5868   cp_token *token;
5869   location_t statement_location;
5870
5871   /* There is no statement yet.  */
5872   statement = NULL_TREE;
5873   /* Peek at the next token.  */
5874   token = cp_lexer_peek_token (parser->lexer);
5875   /* Remember the location of the first token in the statement.  */
5876   statement_location = token->location;
5877   /* If this is a keyword, then that will often determine what kind of
5878      statement we have.  */
5879   if (token->type == CPP_KEYWORD)
5880     {
5881       enum rid keyword = token->keyword;
5882
5883       switch (keyword)
5884         {
5885         case RID_CASE:
5886         case RID_DEFAULT:
5887           statement = cp_parser_labeled_statement (parser,
5888                                                    in_statement_expr);
5889           break;
5890
5891         case RID_IF:
5892         case RID_SWITCH:
5893           statement = cp_parser_selection_statement (parser);
5894           break;
5895
5896         case RID_WHILE:
5897         case RID_DO:
5898         case RID_FOR:
5899           statement = cp_parser_iteration_statement (parser);
5900           break;
5901
5902         case RID_BREAK:
5903         case RID_CONTINUE:
5904         case RID_RETURN:
5905         case RID_GOTO:
5906           statement = cp_parser_jump_statement (parser);
5907           break;
5908
5909         case RID_TRY:
5910           statement = cp_parser_try_block (parser);
5911           break;
5912
5913         default:
5914           /* It might be a keyword like `int' that can start a
5915              declaration-statement.  */
5916           break;
5917         }
5918     }
5919   else if (token->type == CPP_NAME)
5920     {
5921       /* If the next token is a `:', then we are looking at a
5922          labeled-statement.  */
5923       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5924       if (token->type == CPP_COLON)
5925         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5926     }
5927   /* Anything that starts with a `{' must be a compound-statement.  */
5928   else if (token->type == CPP_OPEN_BRACE)
5929     statement = cp_parser_compound_statement (parser, NULL, false);
5930   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5931      a statement all its own.  */
5932   else if (token->type == CPP_PRAGMA)
5933     {
5934       cp_lexer_handle_pragma (parser->lexer);
5935       return;
5936     }
5937
5938   /* Everything else must be a declaration-statement or an
5939      expression-statement.  Try for the declaration-statement
5940      first, unless we are looking at a `;', in which case we know that
5941      we have an expression-statement.  */
5942   if (!statement)
5943     {
5944       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5945         {
5946           cp_parser_parse_tentatively (parser);
5947           /* Try to parse the declaration-statement.  */
5948           cp_parser_declaration_statement (parser);
5949           /* If that worked, we're done.  */
5950           if (cp_parser_parse_definitely (parser))
5951             return;
5952         }
5953       /* Look for an expression-statement instead.  */
5954       statement = cp_parser_expression_statement (parser, in_statement_expr);
5955     }
5956
5957   /* Set the line number for the statement.  */
5958   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5959     SET_EXPR_LOCATION (statement, statement_location);
5960 }
5961
5962 /* Parse a labeled-statement.
5963
5964    labeled-statement:
5965      identifier : statement
5966      case constant-expression : statement
5967      default : statement
5968
5969    GNU Extension:
5970
5971    labeled-statement:
5972      case constant-expression ... constant-expression : statement
5973
5974    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5975    For an ordinary label, returns a LABEL_EXPR.  */
5976
5977 static tree
5978 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5979 {
5980   cp_token *token;
5981   tree statement = error_mark_node;
5982
5983   /* The next token should be an identifier.  */
5984   token = cp_lexer_peek_token (parser->lexer);
5985   if (token->type != CPP_NAME
5986       && token->type != CPP_KEYWORD)
5987     {
5988       cp_parser_error (parser, "expected labeled-statement");
5989       return error_mark_node;
5990     }
5991
5992   switch (token->keyword)
5993     {
5994     case RID_CASE:
5995       {
5996         tree expr, expr_hi;
5997         cp_token *ellipsis;
5998
5999         /* Consume the `case' token.  */
6000         cp_lexer_consume_token (parser->lexer);
6001         /* Parse the constant-expression.  */
6002         expr = cp_parser_constant_expression (parser,
6003                                               /*allow_non_constant_p=*/false,
6004                                               NULL);
6005
6006         ellipsis = cp_lexer_peek_token (parser->lexer);
6007         if (ellipsis->type == CPP_ELLIPSIS)
6008           {
6009             /* Consume the `...' token.  */
6010             cp_lexer_consume_token (parser->lexer);
6011             expr_hi =
6012               cp_parser_constant_expression (parser,
6013                                              /*allow_non_constant_p=*/false,
6014                                              NULL);
6015             /* We don't need to emit warnings here, as the common code
6016                will do this for us.  */
6017           }
6018         else
6019           expr_hi = NULL_TREE;
6020
6021         if (!parser->in_switch_statement_p)
6022           error ("case label %qE not within a switch statement", expr);
6023         else
6024           statement = finish_case_label (expr, expr_hi);
6025       }
6026       break;
6027
6028     case RID_DEFAULT:
6029       /* Consume the `default' token.  */
6030       cp_lexer_consume_token (parser->lexer);
6031       if (!parser->in_switch_statement_p)
6032         error ("case label not within a switch statement");
6033       else
6034         statement = finish_case_label (NULL_TREE, NULL_TREE);
6035       break;
6036
6037     default:
6038       /* Anything else must be an ordinary label.  */
6039       statement = finish_label_stmt (cp_parser_identifier (parser));
6040       break;
6041     }
6042
6043   /* Require the `:' token.  */
6044   cp_parser_require (parser, CPP_COLON, "`:'");
6045   /* Parse the labeled statement.  */
6046   cp_parser_statement (parser, in_statement_expr);
6047
6048   /* Return the label, in the case of a `case' or `default' label.  */
6049   return statement;
6050 }
6051
6052 /* Parse an expression-statement.
6053
6054    expression-statement:
6055      expression [opt] ;
6056
6057    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6058    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6059    indicates whether this expression-statement is part of an
6060    expression statement.  */
6061
6062 static tree
6063 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6064 {
6065   tree statement = NULL_TREE;
6066
6067   /* If the next token is a ';', then there is no expression
6068      statement.  */
6069   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6070     statement = cp_parser_expression (parser);
6071
6072   /* Consume the final `;'.  */
6073   cp_parser_consume_semicolon_at_end_of_statement (parser);
6074
6075   if (in_statement_expr
6076       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6077     {
6078       /* This is the final expression statement of a statement
6079          expression.  */
6080       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6081     }
6082   else if (statement)
6083     statement = finish_expr_stmt (statement);
6084   else
6085     finish_stmt ();
6086
6087   return statement;
6088 }
6089
6090 /* Parse a compound-statement.
6091
6092    compound-statement:
6093      { statement-seq [opt] }
6094
6095    Returns a tree representing the statement.  */
6096
6097 static tree
6098 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6099                               bool in_try)
6100 {
6101   tree compound_stmt;
6102
6103   /* Consume the `{'.  */
6104   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6105     return error_mark_node;
6106   /* Begin the compound-statement.  */
6107   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6108   /* Parse an (optional) statement-seq.  */
6109   cp_parser_statement_seq_opt (parser, in_statement_expr);
6110   /* Finish the compound-statement.  */
6111   finish_compound_stmt (compound_stmt);
6112   /* Consume the `}'.  */
6113   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6114
6115   return compound_stmt;
6116 }
6117
6118 /* Parse an (optional) statement-seq.
6119
6120    statement-seq:
6121      statement
6122      statement-seq [opt] statement  */
6123
6124 static void
6125 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6126 {
6127   /* Scan statements until there aren't any more.  */
6128   while (true)
6129     {
6130       /* If we're looking at a `}', then we've run out of statements.  */
6131       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6132           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6133         break;
6134
6135       /* Parse the statement.  */
6136       cp_parser_statement (parser, in_statement_expr);
6137     }
6138 }
6139
6140 /* Parse a selection-statement.
6141
6142    selection-statement:
6143      if ( condition ) statement
6144      if ( condition ) statement else statement
6145      switch ( condition ) statement
6146
6147    Returns the new IF_STMT or SWITCH_STMT.  */
6148
6149 static tree
6150 cp_parser_selection_statement (cp_parser* parser)
6151 {
6152   cp_token *token;
6153   enum rid keyword;
6154
6155   /* Peek at the next token.  */
6156   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6157
6158   /* See what kind of keyword it is.  */
6159   keyword = token->keyword;
6160   switch (keyword)
6161     {
6162     case RID_IF:
6163     case RID_SWITCH:
6164       {
6165         tree statement;
6166         tree condition;
6167
6168         /* Look for the `('.  */
6169         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6170           {
6171             cp_parser_skip_to_end_of_statement (parser);
6172             return error_mark_node;
6173           }
6174
6175         /* Begin the selection-statement.  */
6176         if (keyword == RID_IF)
6177           statement = begin_if_stmt ();
6178         else
6179           statement = begin_switch_stmt ();
6180
6181         /* Parse the condition.  */
6182         condition = cp_parser_condition (parser);
6183         /* Look for the `)'.  */
6184         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6185           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6186                                                  /*consume_paren=*/true);
6187
6188         if (keyword == RID_IF)
6189           {
6190             /* Add the condition.  */
6191             finish_if_stmt_cond (condition, statement);
6192
6193             /* Parse the then-clause.  */
6194             cp_parser_implicitly_scoped_statement (parser);
6195             finish_then_clause (statement);
6196
6197             /* If the next token is `else', parse the else-clause.  */
6198             if (cp_lexer_next_token_is_keyword (parser->lexer,
6199                                                 RID_ELSE))
6200               {
6201                 /* Consume the `else' keyword.  */
6202                 cp_lexer_consume_token (parser->lexer);
6203                 begin_else_clause (statement);
6204                 /* Parse the else-clause.  */
6205                 cp_parser_implicitly_scoped_statement (parser);
6206                 finish_else_clause (statement);
6207               }
6208
6209             /* Now we're all done with the if-statement.  */
6210             finish_if_stmt (statement);
6211           }
6212         else
6213           {
6214             bool in_switch_statement_p;
6215
6216             /* Add the condition.  */
6217             finish_switch_cond (condition, statement);
6218
6219             /* Parse the body of the switch-statement.  */
6220             in_switch_statement_p = parser->in_switch_statement_p;
6221             parser->in_switch_statement_p = true;
6222             cp_parser_implicitly_scoped_statement (parser);
6223             parser->in_switch_statement_p = in_switch_statement_p;
6224
6225             /* Now we're all done with the switch-statement.  */
6226             finish_switch_stmt (statement);
6227           }
6228
6229         return statement;
6230       }
6231       break;
6232
6233     default:
6234       cp_parser_error (parser, "expected selection-statement");
6235       return error_mark_node;
6236     }
6237 }
6238
6239 /* Parse a condition.
6240
6241    condition:
6242      expression
6243      type-specifier-seq declarator = assignment-expression
6244
6245    GNU Extension:
6246
6247    condition:
6248      type-specifier-seq declarator asm-specification [opt]
6249        attributes [opt] = assignment-expression
6250
6251    Returns the expression that should be tested.  */
6252
6253 static tree
6254 cp_parser_condition (cp_parser* parser)
6255 {
6256   cp_decl_specifier_seq type_specifiers;
6257   const char *saved_message;
6258
6259   /* Try the declaration first.  */
6260   cp_parser_parse_tentatively (parser);
6261   /* New types are not allowed in the type-specifier-seq for a
6262      condition.  */
6263   saved_message = parser->type_definition_forbidden_message;
6264   parser->type_definition_forbidden_message
6265     = "types may not be defined in conditions";
6266   /* Parse the type-specifier-seq.  */
6267   cp_parser_type_specifier_seq (parser, &type_specifiers);
6268   /* Restore the saved message.  */
6269   parser->type_definition_forbidden_message = saved_message;
6270   /* If all is well, we might be looking at a declaration.  */
6271   if (!cp_parser_error_occurred (parser))
6272     {
6273       tree decl;
6274       tree asm_specification;
6275       tree attributes;
6276       cp_declarator *declarator;
6277       tree initializer = NULL_TREE;
6278
6279       /* Parse the declarator.  */
6280       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6281                                          /*ctor_dtor_or_conv_p=*/NULL,
6282                                          /*parenthesized_p=*/NULL,
6283                                          /*member_p=*/false);
6284       /* Parse the attributes.  */
6285       attributes = cp_parser_attributes_opt (parser);
6286       /* Parse the asm-specification.  */
6287       asm_specification = cp_parser_asm_specification_opt (parser);
6288       /* If the next token is not an `=', then we might still be
6289          looking at an expression.  For example:
6290
6291            if (A(a).x)
6292
6293          looks like a decl-specifier-seq and a declarator -- but then
6294          there is no `=', so this is an expression.  */
6295       cp_parser_require (parser, CPP_EQ, "`='");
6296       /* If we did see an `=', then we are looking at a declaration
6297          for sure.  */
6298       if (cp_parser_parse_definitely (parser))
6299         {
6300           bool pop_p;   
6301
6302           /* Create the declaration.  */
6303           decl = start_decl (declarator, &type_specifiers,
6304                              /*initialized_p=*/true,
6305                              attributes, /*prefix_attributes=*/NULL_TREE,
6306                              &pop_p);
6307           /* Parse the assignment-expression.  */
6308           initializer = cp_parser_assignment_expression (parser);
6309
6310           /* Process the initializer.  */
6311           cp_finish_decl (decl,
6312                           initializer,
6313                           asm_specification,
6314                           LOOKUP_ONLYCONVERTING);
6315
6316           if (pop_p)
6317             pop_scope (DECL_CONTEXT (decl));
6318
6319           return convert_from_reference (decl);
6320         }
6321     }
6322   /* If we didn't even get past the declarator successfully, we are
6323      definitely not looking at a declaration.  */
6324   else
6325     cp_parser_abort_tentative_parse (parser);
6326
6327   /* Otherwise, we are looking at an expression.  */
6328   return cp_parser_expression (parser);
6329 }
6330
6331 /* Parse an iteration-statement.
6332
6333    iteration-statement:
6334      while ( condition ) statement
6335      do statement while ( expression ) ;
6336      for ( for-init-statement condition [opt] ; expression [opt] )
6337        statement
6338
6339    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6340
6341 static tree
6342 cp_parser_iteration_statement (cp_parser* parser)
6343 {
6344   cp_token *token;
6345   enum rid keyword;
6346   tree statement;
6347   bool in_iteration_statement_p;
6348
6349
6350   /* Peek at the next token.  */
6351   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6352   if (!token)
6353     return error_mark_node;
6354
6355   /* Remember whether or not we are already within an iteration
6356      statement.  */
6357   in_iteration_statement_p = parser->in_iteration_statement_p;
6358
6359   /* See what kind of keyword it is.  */
6360   keyword = token->keyword;
6361   switch (keyword)
6362     {
6363     case RID_WHILE:
6364       {
6365         tree condition;
6366
6367         /* Begin the while-statement.  */
6368         statement = begin_while_stmt ();
6369         /* Look for the `('.  */
6370         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6371         /* Parse the condition.  */
6372         condition = cp_parser_condition (parser);
6373         finish_while_stmt_cond (condition, statement);
6374         /* Look for the `)'.  */
6375         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6376         /* Parse the dependent statement.  */
6377         parser->in_iteration_statement_p = true;
6378         cp_parser_already_scoped_statement (parser);
6379         parser->in_iteration_statement_p = in_iteration_statement_p;
6380         /* We're done with the while-statement.  */
6381         finish_while_stmt (statement);
6382       }
6383       break;
6384
6385     case RID_DO:
6386       {
6387         tree expression;
6388
6389         /* Begin the do-statement.  */
6390         statement = begin_do_stmt ();
6391         /* Parse the body of the do-statement.  */
6392         parser->in_iteration_statement_p = true;
6393         cp_parser_implicitly_scoped_statement (parser);
6394         parser->in_iteration_statement_p = in_iteration_statement_p;
6395         finish_do_body (statement);
6396         /* Look for the `while' keyword.  */
6397         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6398         /* Look for the `('.  */
6399         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6400         /* Parse the expression.  */
6401         expression = cp_parser_expression (parser);
6402         /* We're done with the do-statement.  */
6403         finish_do_stmt (expression, statement);
6404         /* Look for the `)'.  */
6405         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6406         /* Look for the `;'.  */
6407         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6408       }
6409       break;
6410
6411     case RID_FOR:
6412       {
6413         tree condition = NULL_TREE;
6414         tree expression = NULL_TREE;
6415
6416         /* Begin the for-statement.  */
6417         statement = begin_for_stmt ();
6418         /* Look for the `('.  */
6419         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6420         /* Parse the initialization.  */
6421         cp_parser_for_init_statement (parser);
6422         finish_for_init_stmt (statement);
6423
6424         /* If there's a condition, process it.  */
6425         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6426           condition = cp_parser_condition (parser);
6427         finish_for_cond (condition, statement);
6428         /* Look for the `;'.  */
6429         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6430
6431         /* If there's an expression, process it.  */
6432         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6433           expression = cp_parser_expression (parser);
6434         finish_for_expr (expression, statement);
6435         /* Look for the `)'.  */
6436         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6437
6438         /* Parse the body of the for-statement.  */
6439         parser->in_iteration_statement_p = true;
6440         cp_parser_already_scoped_statement (parser);
6441         parser->in_iteration_statement_p = in_iteration_statement_p;
6442
6443         /* We're done with the for-statement.  */
6444         finish_for_stmt (statement);
6445       }
6446       break;
6447
6448     default:
6449       cp_parser_error (parser, "expected iteration-statement");
6450       statement = error_mark_node;
6451       break;
6452     }
6453
6454   return statement;
6455 }
6456
6457 /* Parse a for-init-statement.
6458
6459    for-init-statement:
6460      expression-statement
6461      simple-declaration  */
6462
6463 static void
6464 cp_parser_for_init_statement (cp_parser* parser)
6465 {
6466   /* If the next token is a `;', then we have an empty
6467      expression-statement.  Grammatically, this is also a
6468      simple-declaration, but an invalid one, because it does not
6469      declare anything.  Therefore, if we did not handle this case
6470      specially, we would issue an error message about an invalid
6471      declaration.  */
6472   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6473     {
6474       /* We're going to speculatively look for a declaration, falling back
6475          to an expression, if necessary.  */
6476       cp_parser_parse_tentatively (parser);
6477       /* Parse the declaration.  */
6478       cp_parser_simple_declaration (parser,
6479                                     /*function_definition_allowed_p=*/false);
6480       /* If the tentative parse failed, then we shall need to look for an
6481          expression-statement.  */
6482       if (cp_parser_parse_definitely (parser))
6483         return;
6484     }
6485
6486   cp_parser_expression_statement (parser, false);
6487 }
6488
6489 /* Parse a jump-statement.
6490
6491    jump-statement:
6492      break ;
6493      continue ;
6494      return expression [opt] ;
6495      goto identifier ;
6496
6497    GNU extension:
6498
6499    jump-statement:
6500      goto * expression ;
6501
6502    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6503
6504 static tree
6505 cp_parser_jump_statement (cp_parser* parser)
6506 {
6507   tree statement = error_mark_node;
6508   cp_token *token;
6509   enum rid keyword;
6510
6511   /* Peek at the next token.  */
6512   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6513   if (!token)
6514     return error_mark_node;
6515
6516   /* See what kind of keyword it is.  */
6517   keyword = token->keyword;
6518   switch (keyword)
6519     {
6520     case RID_BREAK:
6521       if (!parser->in_switch_statement_p
6522           && !parser->in_iteration_statement_p)
6523         {
6524           error ("break statement not within loop or switch");
6525           statement = error_mark_node;
6526         }
6527       else
6528         statement = finish_break_stmt ();
6529       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6530       break;
6531
6532     case RID_CONTINUE:
6533       if (!parser->in_iteration_statement_p)
6534         {
6535           error ("continue statement not within a loop");
6536           statement = error_mark_node;
6537         }
6538       else
6539         statement = finish_continue_stmt ();
6540       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6541       break;
6542
6543     case RID_RETURN:
6544       {
6545         tree expr;
6546
6547         /* If the next token is a `;', then there is no
6548            expression.  */
6549         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6550           expr = cp_parser_expression (parser);
6551         else
6552           expr = NULL_TREE;
6553         /* Build the return-statement.  */
6554         statement = finish_return_stmt (expr);
6555         /* Look for the final `;'.  */
6556         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6557       }
6558       break;
6559
6560     case RID_GOTO:
6561       /* Create the goto-statement.  */
6562       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6563         {
6564           /* Issue a warning about this use of a GNU extension.  */
6565           if (pedantic)
6566             pedwarn ("ISO C++ forbids computed gotos");
6567           /* Consume the '*' token.  */
6568           cp_lexer_consume_token (parser->lexer);
6569           /* Parse the dependent expression.  */
6570           finish_goto_stmt (cp_parser_expression (parser));
6571         }
6572       else
6573         finish_goto_stmt (cp_parser_identifier (parser));
6574       /* Look for the final `;'.  */
6575       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6576       break;
6577
6578     default:
6579       cp_parser_error (parser, "expected jump-statement");
6580       break;
6581     }
6582
6583   return statement;
6584 }
6585
6586 /* Parse a declaration-statement.
6587
6588    declaration-statement:
6589      block-declaration  */
6590
6591 static void
6592 cp_parser_declaration_statement (cp_parser* parser)
6593 {
6594   void *p;
6595
6596   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6597   p = obstack_alloc (&declarator_obstack, 0);
6598
6599  /* Parse the block-declaration.  */
6600   cp_parser_block_declaration (parser, /*statement_p=*/true);
6601
6602   /* Free any declarators allocated.  */
6603   obstack_free (&declarator_obstack, p);
6604
6605   /* Finish off the statement.  */
6606   finish_stmt ();
6607 }
6608
6609 /* Some dependent statements (like `if (cond) statement'), are
6610    implicitly in their own scope.  In other words, if the statement is
6611    a single statement (as opposed to a compound-statement), it is
6612    none-the-less treated as if it were enclosed in braces.  Any
6613    declarations appearing in the dependent statement are out of scope
6614    after control passes that point.  This function parses a statement,
6615    but ensures that is in its own scope, even if it is not a
6616    compound-statement.
6617
6618    Returns the new statement.  */
6619
6620 static tree
6621 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6622 {
6623   tree statement;
6624
6625   /* If the token is not a `{', then we must take special action.  */
6626   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6627     {
6628       /* Create a compound-statement.  */
6629       statement = begin_compound_stmt (0);
6630       /* Parse the dependent-statement.  */
6631       cp_parser_statement (parser, false);
6632       /* Finish the dummy compound-statement.  */
6633       finish_compound_stmt (statement);
6634     }
6635   /* Otherwise, we simply parse the statement directly.  */
6636   else
6637     statement = cp_parser_compound_statement (parser, NULL, false);
6638
6639   /* Return the statement.  */
6640   return statement;
6641 }
6642
6643 /* For some dependent statements (like `while (cond) statement'), we
6644    have already created a scope.  Therefore, even if the dependent
6645    statement is a compound-statement, we do not want to create another
6646    scope.  */
6647
6648 static void
6649 cp_parser_already_scoped_statement (cp_parser* parser)
6650 {
6651   /* If the token is a `{', then we must take special action.  */
6652   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6653     cp_parser_statement (parser, false);
6654   else
6655     {
6656       /* Avoid calling cp_parser_compound_statement, so that we
6657          don't create a new scope.  Do everything else by hand.  */
6658       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6659       cp_parser_statement_seq_opt (parser, false);
6660       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6661     }
6662 }
6663
6664 /* Declarations [gram.dcl.dcl] */
6665
6666 /* Parse an optional declaration-sequence.
6667
6668    declaration-seq:
6669      declaration
6670      declaration-seq declaration  */
6671
6672 static void
6673 cp_parser_declaration_seq_opt (cp_parser* parser)
6674 {
6675   while (true)
6676     {
6677       cp_token *token;
6678
6679       token = cp_lexer_peek_token (parser->lexer);
6680
6681       if (token->type == CPP_CLOSE_BRACE
6682           || token->type == CPP_EOF)
6683         break;
6684
6685       if (token->type == CPP_SEMICOLON)
6686         {
6687           /* A declaration consisting of a single semicolon is
6688              invalid.  Allow it unless we're being pedantic.  */
6689           cp_lexer_consume_token (parser->lexer);
6690           if (pedantic && !in_system_header)
6691             pedwarn ("extra %<;%>");
6692           continue;
6693         }
6694
6695       /* If we're entering or exiting a region that's implicitly
6696          extern "C", modify the lang context appropriately. */
6697       if (!parser->implicit_extern_c && token->implicit_extern_c)
6698         {
6699           push_lang_context (lang_name_c);
6700           parser->implicit_extern_c = true;
6701         }
6702       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6703         {
6704           pop_lang_context ();
6705           parser->implicit_extern_c = false;
6706         }
6707
6708       if (token->type == CPP_PRAGMA)
6709         {
6710           /* A top-level declaration can consist solely of a #pragma.
6711              A nested declaration cannot, so this is done here and not
6712              in cp_parser_declaration.  (A #pragma at block scope is
6713              handled in cp_parser_statement.)  */
6714           cp_lexer_handle_pragma (parser->lexer);
6715           continue;
6716         }
6717
6718       /* Parse the declaration itself.  */
6719       cp_parser_declaration (parser);
6720     }
6721 }
6722
6723 /* Parse a declaration.
6724
6725    declaration:
6726      block-declaration
6727      function-definition
6728      template-declaration
6729      explicit-instantiation
6730      explicit-specialization
6731      linkage-specification
6732      namespace-definition
6733
6734    GNU extension:
6735
6736    declaration:
6737       __extension__ declaration */
6738
6739 static void
6740 cp_parser_declaration (cp_parser* parser)
6741 {
6742   cp_token token1;
6743   cp_token token2;
6744   int saved_pedantic;
6745   void *p;
6746
6747   /* Check for the `__extension__' keyword.  */
6748   if (cp_parser_extension_opt (parser, &saved_pedantic))
6749     {
6750       /* Parse the qualified declaration.  */
6751       cp_parser_declaration (parser);
6752       /* Restore the PEDANTIC flag.  */
6753       pedantic = saved_pedantic;
6754
6755       return;
6756     }
6757
6758   /* Try to figure out what kind of declaration is present.  */
6759   token1 = *cp_lexer_peek_token (parser->lexer);
6760
6761   if (token1.type != CPP_EOF)
6762     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6763
6764   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6765   p = obstack_alloc (&declarator_obstack, 0);
6766
6767   /* If the next token is `extern' and the following token is a string
6768      literal, then we have a linkage specification.  */
6769   if (token1.keyword == RID_EXTERN
6770       && cp_parser_is_string_literal (&token2))
6771     cp_parser_linkage_specification (parser);
6772   /* If the next token is `template', then we have either a template
6773      declaration, an explicit instantiation, or an explicit
6774      specialization.  */
6775   else if (token1.keyword == RID_TEMPLATE)
6776     {
6777       /* `template <>' indicates a template specialization.  */
6778       if (token2.type == CPP_LESS
6779           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6780         cp_parser_explicit_specialization (parser);
6781       /* `template <' indicates a template declaration.  */
6782       else if (token2.type == CPP_LESS)
6783         cp_parser_template_declaration (parser, /*member_p=*/false);
6784       /* Anything else must be an explicit instantiation.  */
6785       else
6786         cp_parser_explicit_instantiation (parser);
6787     }
6788   /* If the next token is `export', then we have a template
6789      declaration.  */
6790   else if (token1.keyword == RID_EXPORT)
6791     cp_parser_template_declaration (parser, /*member_p=*/false);
6792   /* If the next token is `extern', 'static' or 'inline' and the one
6793      after that is `template', we have a GNU extended explicit
6794      instantiation directive.  */
6795   else if (cp_parser_allow_gnu_extensions_p (parser)
6796            && (token1.keyword == RID_EXTERN
6797                || token1.keyword == RID_STATIC
6798                || token1.keyword == RID_INLINE)
6799            && token2.keyword == RID_TEMPLATE)
6800     cp_parser_explicit_instantiation (parser);
6801   /* If the next token is `namespace', check for a named or unnamed
6802      namespace definition.  */
6803   else if (token1.keyword == RID_NAMESPACE
6804            && (/* A named namespace definition.  */
6805                (token2.type == CPP_NAME
6806                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6807                     == CPP_OPEN_BRACE))
6808                /* An unnamed namespace definition.  */
6809                || token2.type == CPP_OPEN_BRACE))
6810     cp_parser_namespace_definition (parser);
6811   /* We must have either a block declaration or a function
6812      definition.  */
6813   else
6814     /* Try to parse a block-declaration, or a function-definition.  */
6815     cp_parser_block_declaration (parser, /*statement_p=*/false);
6816
6817   /* Free any declarators allocated.  */
6818   obstack_free (&declarator_obstack, p);
6819 }
6820
6821 /* Parse a block-declaration.
6822
6823    block-declaration:
6824      simple-declaration
6825      asm-definition
6826      namespace-alias-definition
6827      using-declaration
6828      using-directive
6829
6830    GNU Extension:
6831
6832    block-declaration:
6833      __extension__ block-declaration
6834      label-declaration
6835
6836    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6837    part of a declaration-statement.  */
6838
6839 static void
6840 cp_parser_block_declaration (cp_parser *parser,
6841                              bool      statement_p)
6842 {
6843   cp_token *token1;
6844   int saved_pedantic;
6845
6846   /* Check for the `__extension__' keyword.  */
6847   if (cp_parser_extension_opt (parser, &saved_pedantic))
6848     {
6849       /* Parse the qualified declaration.  */
6850       cp_parser_block_declaration (parser, statement_p);
6851       /* Restore the PEDANTIC flag.  */
6852       pedantic = saved_pedantic;
6853
6854       return;
6855     }
6856
6857   /* Peek at the next token to figure out which kind of declaration is
6858      present.  */
6859   token1 = cp_lexer_peek_token (parser->lexer);
6860
6861   /* If the next keyword is `asm', we have an asm-definition.  */
6862   if (token1->keyword == RID_ASM)
6863     {
6864       if (statement_p)
6865         cp_parser_commit_to_tentative_parse (parser);
6866       cp_parser_asm_definition (parser);
6867     }
6868   /* If the next keyword is `namespace', we have a
6869      namespace-alias-definition.  */
6870   else if (token1->keyword == RID_NAMESPACE)
6871     cp_parser_namespace_alias_definition (parser);
6872   /* If the next keyword is `using', we have either a
6873      using-declaration or a using-directive.  */
6874   else if (token1->keyword == RID_USING)
6875     {
6876       cp_token *token2;
6877
6878       if (statement_p)
6879         cp_parser_commit_to_tentative_parse (parser);
6880       /* If the token after `using' is `namespace', then we have a
6881          using-directive.  */
6882       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6883       if (token2->keyword == RID_NAMESPACE)
6884         cp_parser_using_directive (parser);
6885       /* Otherwise, it's a using-declaration.  */
6886       else
6887         cp_parser_using_declaration (parser);
6888     }
6889   /* If the next keyword is `__label__' we have a label declaration.  */
6890   else if (token1->keyword == RID_LABEL)
6891     {
6892       if (statement_p)
6893         cp_parser_commit_to_tentative_parse (parser);
6894       cp_parser_label_declaration (parser);
6895     }
6896   /* Anything else must be a simple-declaration.  */
6897   else
6898     cp_parser_simple_declaration (parser, !statement_p);
6899 }
6900
6901 /* Parse a simple-declaration.
6902
6903    simple-declaration:
6904      decl-specifier-seq [opt] init-declarator-list [opt] ;
6905
6906    init-declarator-list:
6907      init-declarator
6908      init-declarator-list , init-declarator
6909
6910    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6911    function-definition as a simple-declaration.  */
6912
6913 static void
6914 cp_parser_simple_declaration (cp_parser* parser,
6915                               bool function_definition_allowed_p)
6916 {
6917   cp_decl_specifier_seq decl_specifiers;
6918   int declares_class_or_enum;
6919   bool saw_declarator;
6920
6921   /* Defer access checks until we know what is being declared; the
6922      checks for names appearing in the decl-specifier-seq should be
6923      done as if we were in the scope of the thing being declared.  */
6924   push_deferring_access_checks (dk_deferred);
6925
6926   /* Parse the decl-specifier-seq.  We have to keep track of whether
6927      or not the decl-specifier-seq declares a named class or
6928      enumeration type, since that is the only case in which the
6929      init-declarator-list is allowed to be empty.
6930
6931      [dcl.dcl]
6932
6933      In a simple-declaration, the optional init-declarator-list can be
6934      omitted only when declaring a class or enumeration, that is when
6935      the decl-specifier-seq contains either a class-specifier, an
6936      elaborated-type-specifier, or an enum-specifier.  */
6937   cp_parser_decl_specifier_seq (parser,
6938                                 CP_PARSER_FLAGS_OPTIONAL,
6939                                 &decl_specifiers,
6940                                 &declares_class_or_enum);
6941   /* We no longer need to defer access checks.  */
6942   stop_deferring_access_checks ();
6943
6944   /* In a block scope, a valid declaration must always have a
6945      decl-specifier-seq.  By not trying to parse declarators, we can
6946      resolve the declaration/expression ambiguity more quickly.  */
6947   if (!function_definition_allowed_p
6948       && !decl_specifiers.any_specifiers_p)
6949     {
6950       cp_parser_error (parser, "expected declaration");
6951       goto done;
6952     }
6953
6954   /* If the next two tokens are both identifiers, the code is
6955      erroneous. The usual cause of this situation is code like:
6956
6957        T t;
6958
6959      where "T" should name a type -- but does not.  */
6960   if (!decl_specifiers.type
6961       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6962     {
6963       /* If parsing tentatively, we should commit; we really are
6964          looking at a declaration.  */
6965       cp_parser_commit_to_tentative_parse (parser);
6966       /* Give up.  */
6967       goto done;
6968     }
6969   
6970   /* If we have seen at least one decl-specifier, and the next token
6971      is not a parenthesis, then we must be looking at a declaration.
6972      (After "int (" we might be looking at a functional cast.)  */
6973   if (decl_specifiers.any_specifiers_p 
6974       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6975     cp_parser_commit_to_tentative_parse (parser);
6976
6977   /* Keep going until we hit the `;' at the end of the simple
6978      declaration.  */
6979   saw_declarator = false;
6980   while (cp_lexer_next_token_is_not (parser->lexer,
6981                                      CPP_SEMICOLON))
6982     {
6983       cp_token *token;
6984       bool function_definition_p;
6985       tree decl;
6986
6987       saw_declarator = true;
6988       /* Parse the init-declarator.  */
6989       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6990                                         function_definition_allowed_p,
6991                                         /*member_p=*/false,
6992                                         declares_class_or_enum,
6993                                         &function_definition_p);
6994       /* If an error occurred while parsing tentatively, exit quickly.
6995          (That usually happens when in the body of a function; each
6996          statement is treated as a declaration-statement until proven
6997          otherwise.)  */
6998       if (cp_parser_error_occurred (parser))
6999         goto done;
7000       /* Handle function definitions specially.  */
7001       if (function_definition_p)
7002         {
7003           /* If the next token is a `,', then we are probably
7004              processing something like:
7005
7006                void f() {}, *p;
7007
7008              which is erroneous.  */
7009           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7010             error ("mixing declarations and function-definitions is forbidden");
7011           /* Otherwise, we're done with the list of declarators.  */
7012           else
7013             {
7014               pop_deferring_access_checks ();
7015               return;
7016             }
7017         }
7018       /* The next token should be either a `,' or a `;'.  */
7019       token = cp_lexer_peek_token (parser->lexer);
7020       /* If it's a `,', there are more declarators to come.  */
7021       if (token->type == CPP_COMMA)
7022         cp_lexer_consume_token (parser->lexer);
7023       /* If it's a `;', we are done.  */
7024       else if (token->type == CPP_SEMICOLON)
7025         break;
7026       /* Anything else is an error.  */
7027       else
7028         {
7029           /* If we have already issued an error message we don't need
7030              to issue another one.  */
7031           if (decl != error_mark_node
7032               || (cp_parser_parsing_tentatively (parser)
7033                   && !cp_parser_committed_to_tentative_parse (parser)))
7034             cp_parser_error (parser, "expected %<,%> or %<;%>");
7035           /* Skip tokens until we reach the end of the statement.  */
7036           cp_parser_skip_to_end_of_statement (parser);
7037           /* If the next token is now a `;', consume it.  */
7038           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7039             cp_lexer_consume_token (parser->lexer);
7040           goto done;
7041         }
7042       /* After the first time around, a function-definition is not
7043          allowed -- even if it was OK at first.  For example:
7044
7045            int i, f() {}
7046
7047          is not valid.  */
7048       function_definition_allowed_p = false;
7049     }
7050
7051   /* Issue an error message if no declarators are present, and the
7052      decl-specifier-seq does not itself declare a class or
7053      enumeration.  */
7054   if (!saw_declarator)
7055     {
7056       if (cp_parser_declares_only_class_p (parser))
7057         shadow_tag (&decl_specifiers);
7058       /* Perform any deferred access checks.  */
7059       perform_deferred_access_checks ();
7060     }
7061
7062   /* Consume the `;'.  */
7063   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7064
7065  done:
7066   pop_deferring_access_checks ();
7067 }
7068
7069 /* Parse a decl-specifier-seq.
7070
7071    decl-specifier-seq:
7072      decl-specifier-seq [opt] decl-specifier
7073
7074    decl-specifier:
7075      storage-class-specifier
7076      type-specifier
7077      function-specifier
7078      friend
7079      typedef
7080
7081    GNU Extension:
7082
7083    decl-specifier:
7084      attributes
7085
7086    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7087
7088    The parser flags FLAGS is used to control type-specifier parsing.
7089
7090    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7091    flags:
7092
7093      1: one of the decl-specifiers is an elaborated-type-specifier
7094         (i.e., a type declaration)
7095      2: one of the decl-specifiers is an enum-specifier or a
7096         class-specifier (i.e., a type definition)
7097
7098    */
7099
7100 static void
7101 cp_parser_decl_specifier_seq (cp_parser* parser,
7102                               cp_parser_flags flags,
7103                               cp_decl_specifier_seq *decl_specs,
7104                               int* declares_class_or_enum)
7105 {
7106   bool constructor_possible_p = !parser->in_declarator_p;
7107
7108   /* Clear DECL_SPECS.  */
7109   clear_decl_specs (decl_specs);
7110
7111   /* Assume no class or enumeration type is declared.  */
7112   *declares_class_or_enum = 0;
7113
7114   /* Keep reading specifiers until there are no more to read.  */
7115   while (true)
7116     {
7117       bool constructor_p;
7118       bool found_decl_spec;
7119       cp_token *token;
7120
7121       /* Peek at the next token.  */
7122       token = cp_lexer_peek_token (parser->lexer);
7123       /* Handle attributes.  */
7124       if (token->keyword == RID_ATTRIBUTE)
7125         {
7126           /* Parse the attributes.  */
7127           decl_specs->attributes
7128             = chainon (decl_specs->attributes,
7129                        cp_parser_attributes_opt (parser));
7130           continue;
7131         }
7132       /* Assume we will find a decl-specifier keyword.  */
7133       found_decl_spec = true;
7134       /* If the next token is an appropriate keyword, we can simply
7135          add it to the list.  */
7136       switch (token->keyword)
7137         {
7138           /* decl-specifier:
7139                friend  */
7140         case RID_FRIEND:
7141           if (decl_specs->specs[(int) ds_friend]++)
7142             error ("duplicate %<friend%>");
7143           /* Consume the token.  */
7144           cp_lexer_consume_token (parser->lexer);
7145           break;
7146
7147           /* function-specifier:
7148                inline
7149                virtual
7150                explicit  */
7151         case RID_INLINE:
7152         case RID_VIRTUAL:
7153         case RID_EXPLICIT:
7154           cp_parser_function_specifier_opt (parser, decl_specs);
7155           break;
7156
7157           /* decl-specifier:
7158                typedef  */
7159         case RID_TYPEDEF:
7160           ++decl_specs->specs[(int) ds_typedef];
7161           /* Consume the token.  */
7162           cp_lexer_consume_token (parser->lexer);
7163           /* A constructor declarator cannot appear in a typedef.  */
7164           constructor_possible_p = false;
7165           /* The "typedef" keyword can only occur in a declaration; we
7166              may as well commit at this point.  */
7167           cp_parser_commit_to_tentative_parse (parser);
7168           break;
7169
7170           /* storage-class-specifier:
7171                auto
7172                register
7173                static
7174                extern
7175                mutable
7176
7177              GNU Extension:
7178                thread  */
7179         case RID_AUTO:
7180           /* Consume the token.  */
7181           cp_lexer_consume_token (parser->lexer);
7182           cp_parser_set_storage_class (decl_specs, sc_auto);
7183           break;
7184         case RID_REGISTER:
7185           /* Consume the token.  */
7186           cp_lexer_consume_token (parser->lexer);
7187           cp_parser_set_storage_class (decl_specs, sc_register);
7188           break;
7189         case RID_STATIC:
7190           /* Consume the token.  */
7191           cp_lexer_consume_token (parser->lexer);
7192           if (decl_specs->specs[(int) ds_thread])
7193             {
7194               error ("%<__thread%> before %<static%>");
7195               decl_specs->specs[(int) ds_thread] = 0;
7196             }
7197           cp_parser_set_storage_class (decl_specs, sc_static);
7198           break;
7199         case RID_EXTERN:
7200           /* Consume the token.  */
7201           cp_lexer_consume_token (parser->lexer);
7202           if (decl_specs->specs[(int) ds_thread])
7203             {
7204               error ("%<__thread%> before %<extern%>");
7205               decl_specs->specs[(int) ds_thread] = 0;
7206             }
7207           cp_parser_set_storage_class (decl_specs, sc_extern);
7208           break;
7209         case RID_MUTABLE:
7210           /* Consume the token.  */
7211           cp_lexer_consume_token (parser->lexer);
7212           cp_parser_set_storage_class (decl_specs, sc_mutable);
7213           break;
7214         case RID_THREAD:
7215           /* Consume the token.  */
7216           cp_lexer_consume_token (parser->lexer);
7217           ++decl_specs->specs[(int) ds_thread];
7218           break;
7219
7220         default:
7221           /* We did not yet find a decl-specifier yet.  */
7222           found_decl_spec = false;
7223           break;
7224         }
7225
7226       /* Constructors are a special case.  The `S' in `S()' is not a
7227          decl-specifier; it is the beginning of the declarator.  */
7228       constructor_p
7229         = (!found_decl_spec
7230            && constructor_possible_p
7231            && (cp_parser_constructor_declarator_p
7232                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7233
7234       /* If we don't have a DECL_SPEC yet, then we must be looking at
7235          a type-specifier.  */
7236       if (!found_decl_spec && !constructor_p)
7237         {
7238           int decl_spec_declares_class_or_enum;
7239           bool is_cv_qualifier;
7240           tree type_spec;
7241
7242           type_spec
7243             = cp_parser_type_specifier (parser, flags,
7244                                         decl_specs,
7245                                         /*is_declaration=*/true,
7246                                         &decl_spec_declares_class_or_enum,
7247                                         &is_cv_qualifier);
7248
7249           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7250
7251           /* If this type-specifier referenced a user-defined type
7252              (a typedef, class-name, etc.), then we can't allow any
7253              more such type-specifiers henceforth.
7254
7255              [dcl.spec]
7256
7257              The longest sequence of decl-specifiers that could
7258              possibly be a type name is taken as the
7259              decl-specifier-seq of a declaration.  The sequence shall
7260              be self-consistent as described below.
7261
7262              [dcl.type]
7263
7264              As a general rule, at most one type-specifier is allowed
7265              in the complete decl-specifier-seq of a declaration.  The
7266              only exceptions are the following:
7267
7268              -- const or volatile can be combined with any other
7269                 type-specifier.
7270
7271              -- signed or unsigned can be combined with char, long,
7272                 short, or int.
7273
7274              -- ..
7275
7276              Example:
7277
7278                typedef char* Pc;
7279                void g (const int Pc);
7280
7281              Here, Pc is *not* part of the decl-specifier seq; it's
7282              the declarator.  Therefore, once we see a type-specifier
7283              (other than a cv-qualifier), we forbid any additional
7284              user-defined types.  We *do* still allow things like `int
7285              int' to be considered a decl-specifier-seq, and issue the
7286              error message later.  */
7287           if (type_spec && !is_cv_qualifier)
7288             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7289           /* A constructor declarator cannot follow a type-specifier.  */
7290           if (type_spec)
7291             {
7292               constructor_possible_p = false;
7293               found_decl_spec = true;
7294             }
7295         }
7296
7297       /* If we still do not have a DECL_SPEC, then there are no more
7298          decl-specifiers.  */
7299       if (!found_decl_spec)
7300         break;
7301
7302       decl_specs->any_specifiers_p = true;
7303       /* After we see one decl-specifier, further decl-specifiers are
7304          always optional.  */
7305       flags |= CP_PARSER_FLAGS_OPTIONAL;
7306     }
7307
7308   /* Don't allow a friend specifier with a class definition.  */
7309   if (decl_specs->specs[(int) ds_friend] != 0
7310       && (*declares_class_or_enum & 2))
7311     error ("class definition may not be declared a friend");
7312 }
7313
7314 /* Parse an (optional) storage-class-specifier.
7315
7316    storage-class-specifier:
7317      auto
7318      register
7319      static
7320      extern
7321      mutable
7322
7323    GNU Extension:
7324
7325    storage-class-specifier:
7326      thread
7327
7328    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7329
7330 static tree
7331 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7332 {
7333   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7334     {
7335     case RID_AUTO:
7336     case RID_REGISTER:
7337     case RID_STATIC:
7338     case RID_EXTERN:
7339     case RID_MUTABLE:
7340     case RID_THREAD:
7341       /* Consume the token.  */
7342       return cp_lexer_consume_token (parser->lexer)->value;
7343
7344     default:
7345       return NULL_TREE;
7346     }
7347 }
7348
7349 /* Parse an (optional) function-specifier.
7350
7351    function-specifier:
7352      inline
7353      virtual
7354      explicit
7355
7356    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7357    Updates DECL_SPECS, if it is non-NULL.  */
7358
7359 static tree
7360 cp_parser_function_specifier_opt (cp_parser* parser,
7361                                   cp_decl_specifier_seq *decl_specs)
7362 {
7363   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7364     {
7365     case RID_INLINE:
7366       if (decl_specs)
7367         ++decl_specs->specs[(int) ds_inline];
7368       break;
7369
7370     case RID_VIRTUAL:
7371       if (decl_specs)
7372         ++decl_specs->specs[(int) ds_virtual];
7373       break;
7374
7375     case RID_EXPLICIT:
7376       if (decl_specs)
7377         ++decl_specs->specs[(int) ds_explicit];
7378       break;
7379
7380     default:
7381       return NULL_TREE;
7382     }
7383
7384   /* Consume the token.  */
7385   return cp_lexer_consume_token (parser->lexer)->value;
7386 }
7387
7388 /* Parse a linkage-specification.
7389
7390    linkage-specification:
7391      extern string-literal { declaration-seq [opt] }
7392      extern string-literal declaration  */
7393
7394 static void
7395 cp_parser_linkage_specification (cp_parser* parser)
7396 {
7397   tree linkage;
7398
7399   /* Look for the `extern' keyword.  */
7400   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7401
7402   /* Look for the string-literal.  */
7403   linkage = cp_parser_string_literal (parser, false, false);
7404
7405   /* Transform the literal into an identifier.  If the literal is a
7406      wide-character string, or contains embedded NULs, then we can't
7407      handle it as the user wants.  */
7408   if (strlen (TREE_STRING_POINTER (linkage))
7409       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7410     {
7411       cp_parser_error (parser, "invalid linkage-specification");
7412       /* Assume C++ linkage.  */
7413       linkage = lang_name_cplusplus;
7414     }
7415   else
7416     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7417
7418   /* We're now using the new linkage.  */
7419   push_lang_context (linkage);
7420
7421   /* If the next token is a `{', then we're using the first
7422      production.  */
7423   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7424     {
7425       /* Consume the `{' token.  */
7426       cp_lexer_consume_token (parser->lexer);
7427       /* Parse the declarations.  */
7428       cp_parser_declaration_seq_opt (parser);
7429       /* Look for the closing `}'.  */
7430       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7431     }
7432   /* Otherwise, there's just one declaration.  */
7433   else
7434     {
7435       bool saved_in_unbraced_linkage_specification_p;
7436
7437       saved_in_unbraced_linkage_specification_p
7438         = parser->in_unbraced_linkage_specification_p;
7439       parser->in_unbraced_linkage_specification_p = true;
7440       have_extern_spec = true;
7441       cp_parser_declaration (parser);
7442       have_extern_spec = false;
7443       parser->in_unbraced_linkage_specification_p
7444         = saved_in_unbraced_linkage_specification_p;
7445     }
7446
7447   /* We're done with the linkage-specification.  */
7448   pop_lang_context ();
7449 }
7450
7451 /* Special member functions [gram.special] */
7452
7453 /* Parse a conversion-function-id.
7454
7455    conversion-function-id:
7456      operator conversion-type-id
7457
7458    Returns an IDENTIFIER_NODE representing the operator.  */
7459
7460 static tree
7461 cp_parser_conversion_function_id (cp_parser* parser)
7462 {
7463   tree type;
7464   tree saved_scope;
7465   tree saved_qualifying_scope;
7466   tree saved_object_scope;
7467   bool pop_p = false;
7468
7469   /* Look for the `operator' token.  */
7470   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7471     return error_mark_node;
7472   /* When we parse the conversion-type-id, the current scope will be
7473      reset.  However, we need that information in able to look up the
7474      conversion function later, so we save it here.  */
7475   saved_scope = parser->scope;
7476   saved_qualifying_scope = parser->qualifying_scope;
7477   saved_object_scope = parser->object_scope;
7478   /* We must enter the scope of the class so that the names of
7479      entities declared within the class are available in the
7480      conversion-type-id.  For example, consider:
7481
7482        struct S {
7483          typedef int I;
7484          operator I();
7485        };
7486
7487        S::operator I() { ... }
7488
7489      In order to see that `I' is a type-name in the definition, we
7490      must be in the scope of `S'.  */
7491   if (saved_scope)
7492     pop_p = push_scope (saved_scope);
7493   /* Parse the conversion-type-id.  */
7494   type = cp_parser_conversion_type_id (parser);
7495   /* Leave the scope of the class, if any.  */
7496   if (pop_p)
7497     pop_scope (saved_scope);
7498   /* Restore the saved scope.  */
7499   parser->scope = saved_scope;
7500   parser->qualifying_scope = saved_qualifying_scope;
7501   parser->object_scope = saved_object_scope;
7502   /* If the TYPE is invalid, indicate failure.  */
7503   if (type == error_mark_node)
7504     return error_mark_node;
7505   return mangle_conv_op_name_for_type (type);
7506 }
7507
7508 /* Parse a conversion-type-id:
7509
7510    conversion-type-id:
7511      type-specifier-seq conversion-declarator [opt]
7512
7513    Returns the TYPE specified.  */
7514
7515 static tree
7516 cp_parser_conversion_type_id (cp_parser* parser)
7517 {
7518   tree attributes;
7519   cp_decl_specifier_seq type_specifiers;
7520   cp_declarator *declarator;
7521   tree type_specified;
7522
7523   /* Parse the attributes.  */
7524   attributes = cp_parser_attributes_opt (parser);
7525   /* Parse the type-specifiers.  */
7526   cp_parser_type_specifier_seq (parser, &type_specifiers);
7527   /* If that didn't work, stop.  */
7528   if (type_specifiers.type == error_mark_node)
7529     return error_mark_node;
7530   /* Parse the conversion-declarator.  */
7531   declarator = cp_parser_conversion_declarator_opt (parser);
7532
7533   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7534                                     /*initialized=*/0, &attributes);
7535   if (attributes)
7536     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7537   return type_specified;
7538 }
7539
7540 /* Parse an (optional) conversion-declarator.
7541
7542    conversion-declarator:
7543      ptr-operator conversion-declarator [opt]
7544
7545    */
7546
7547 static cp_declarator *
7548 cp_parser_conversion_declarator_opt (cp_parser* parser)
7549 {
7550   enum tree_code code;
7551   tree class_type;
7552   cp_cv_quals cv_quals;
7553
7554   /* We don't know if there's a ptr-operator next, or not.  */
7555   cp_parser_parse_tentatively (parser);
7556   /* Try the ptr-operator.  */
7557   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7558   /* If it worked, look for more conversion-declarators.  */
7559   if (cp_parser_parse_definitely (parser))
7560     {
7561       cp_declarator *declarator;
7562
7563       /* Parse another optional declarator.  */
7564       declarator = cp_parser_conversion_declarator_opt (parser);
7565
7566       /* Create the representation of the declarator.  */
7567       if (class_type)
7568         declarator = make_ptrmem_declarator (cv_quals, class_type,
7569                                              declarator);
7570       else if (code == INDIRECT_REF)
7571         declarator = make_pointer_declarator (cv_quals, declarator);
7572       else
7573         declarator = make_reference_declarator (cv_quals, declarator);
7574
7575       return declarator;
7576    }
7577
7578   return NULL;
7579 }
7580
7581 /* Parse an (optional) ctor-initializer.
7582
7583    ctor-initializer:
7584      : mem-initializer-list
7585
7586    Returns TRUE iff the ctor-initializer was actually present.  */
7587
7588 static bool
7589 cp_parser_ctor_initializer_opt (cp_parser* parser)
7590 {
7591   /* If the next token is not a `:', then there is no
7592      ctor-initializer.  */
7593   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7594     {
7595       /* Do default initialization of any bases and members.  */
7596       if (DECL_CONSTRUCTOR_P (current_function_decl))
7597         finish_mem_initializers (NULL_TREE);
7598
7599       return false;
7600     }
7601
7602   /* Consume the `:' token.  */
7603   cp_lexer_consume_token (parser->lexer);
7604   /* And the mem-initializer-list.  */
7605   cp_parser_mem_initializer_list (parser);
7606
7607   return true;
7608 }
7609
7610 /* Parse a mem-initializer-list.
7611
7612    mem-initializer-list:
7613      mem-initializer
7614      mem-initializer , mem-initializer-list  */
7615
7616 static void
7617 cp_parser_mem_initializer_list (cp_parser* parser)
7618 {
7619   tree mem_initializer_list = NULL_TREE;
7620
7621   /* Let the semantic analysis code know that we are starting the
7622      mem-initializer-list.  */
7623   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7624     error ("only constructors take base initializers");
7625
7626   /* Loop through the list.  */
7627   while (true)
7628     {
7629       tree mem_initializer;
7630
7631       /* Parse the mem-initializer.  */
7632       mem_initializer = cp_parser_mem_initializer (parser);
7633       /* Add it to the list, unless it was erroneous.  */
7634       if (mem_initializer)
7635         {
7636           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7637           mem_initializer_list = mem_initializer;
7638         }
7639       /* If the next token is not a `,', we're done.  */
7640       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7641         break;
7642       /* Consume the `,' token.  */
7643       cp_lexer_consume_token (parser->lexer);
7644     }
7645
7646   /* Perform semantic analysis.  */
7647   if (DECL_CONSTRUCTOR_P (current_function_decl))
7648     finish_mem_initializers (mem_initializer_list);
7649 }
7650
7651 /* Parse a mem-initializer.
7652
7653    mem-initializer:
7654      mem-initializer-id ( expression-list [opt] )
7655
7656    GNU extension:
7657
7658    mem-initializer:
7659      ( expression-list [opt] )
7660
7661    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7662    class) or FIELD_DECL (for a non-static data member) to initialize;
7663    the TREE_VALUE is the expression-list.  */
7664
7665 static tree
7666 cp_parser_mem_initializer (cp_parser* parser)
7667 {
7668   tree mem_initializer_id;
7669   tree expression_list;
7670   tree member;
7671
7672   /* Find out what is being initialized.  */
7673   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7674     {
7675       pedwarn ("anachronistic old-style base class initializer");
7676       mem_initializer_id = NULL_TREE;
7677     }
7678   else
7679     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7680   member = expand_member_init (mem_initializer_id);
7681   if (member && !DECL_P (member))
7682     in_base_initializer = 1;
7683
7684   expression_list
7685     = cp_parser_parenthesized_expression_list (parser, false,
7686                                                /*non_constant_p=*/NULL);
7687   if (!expression_list)
7688     expression_list = void_type_node;
7689
7690   in_base_initializer = 0;
7691
7692   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7693 }
7694
7695 /* Parse a mem-initializer-id.
7696
7697    mem-initializer-id:
7698      :: [opt] nested-name-specifier [opt] class-name
7699      identifier
7700
7701    Returns a TYPE indicating the class to be initializer for the first
7702    production.  Returns an IDENTIFIER_NODE indicating the data member
7703    to be initialized for the second production.  */
7704
7705 static tree
7706 cp_parser_mem_initializer_id (cp_parser* parser)
7707 {
7708   bool global_scope_p;
7709   bool nested_name_specifier_p;
7710   bool template_p = false;
7711   tree id;
7712
7713   /* `typename' is not allowed in this context ([temp.res]).  */
7714   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7715     {
7716       error ("keyword %<typename%> not allowed in this context (a qualified "
7717              "member initializer is implicitly a type)");
7718       cp_lexer_consume_token (parser->lexer);
7719     }
7720   /* Look for the optional `::' operator.  */
7721   global_scope_p
7722     = (cp_parser_global_scope_opt (parser,
7723                                    /*current_scope_valid_p=*/false)
7724        != NULL_TREE);
7725   /* Look for the optional nested-name-specifier.  The simplest way to
7726      implement:
7727
7728        [temp.res]
7729
7730        The keyword `typename' is not permitted in a base-specifier or
7731        mem-initializer; in these contexts a qualified name that
7732        depends on a template-parameter is implicitly assumed to be a
7733        type name.
7734
7735      is to assume that we have seen the `typename' keyword at this
7736      point.  */
7737   nested_name_specifier_p
7738     = (cp_parser_nested_name_specifier_opt (parser,
7739                                             /*typename_keyword_p=*/true,
7740                                             /*check_dependency_p=*/true,
7741                                             /*type_p=*/true,
7742                                             /*is_declaration=*/true)
7743        != NULL_TREE);
7744   if (nested_name_specifier_p)
7745     template_p = cp_parser_optional_template_keyword (parser);
7746   /* If there is a `::' operator or a nested-name-specifier, then we
7747      are definitely looking for a class-name.  */
7748   if (global_scope_p || nested_name_specifier_p)
7749     return cp_parser_class_name (parser,
7750                                  /*typename_keyword_p=*/true,
7751                                  /*template_keyword_p=*/template_p,
7752                                  /*type_p=*/false,
7753                                  /*check_dependency_p=*/true,
7754                                  /*class_head_p=*/false,
7755                                  /*is_declaration=*/true);
7756   /* Otherwise, we could also be looking for an ordinary identifier.  */
7757   cp_parser_parse_tentatively (parser);
7758   /* Try a class-name.  */
7759   id = cp_parser_class_name (parser,
7760                              /*typename_keyword_p=*/true,
7761                              /*template_keyword_p=*/false,
7762                              /*type_p=*/false,
7763                              /*check_dependency_p=*/true,
7764                              /*class_head_p=*/false,
7765                              /*is_declaration=*/true);
7766   /* If we found one, we're done.  */
7767   if (cp_parser_parse_definitely (parser))
7768     return id;
7769   /* Otherwise, look for an ordinary identifier.  */
7770   return cp_parser_identifier (parser);
7771 }
7772
7773 /* Overloading [gram.over] */
7774
7775 /* Parse an operator-function-id.
7776
7777    operator-function-id:
7778      operator operator
7779
7780    Returns an IDENTIFIER_NODE for the operator which is a
7781    human-readable spelling of the identifier, e.g., `operator +'.  */
7782
7783 static tree
7784 cp_parser_operator_function_id (cp_parser* parser)
7785 {
7786   /* Look for the `operator' keyword.  */
7787   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7788     return error_mark_node;
7789   /* And then the name of the operator itself.  */
7790   return cp_parser_operator (parser);
7791 }
7792
7793 /* Parse an operator.
7794
7795    operator:
7796      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7797      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7798      || ++ -- , ->* -> () []
7799
7800    GNU Extensions:
7801
7802    operator:
7803      <? >? <?= >?=
7804
7805    Returns an IDENTIFIER_NODE for the operator which is a
7806    human-readable spelling of the identifier, e.g., `operator +'.  */
7807
7808 static tree
7809 cp_parser_operator (cp_parser* parser)
7810 {
7811   tree id = NULL_TREE;
7812   cp_token *token;
7813
7814   /* Peek at the next token.  */
7815   token = cp_lexer_peek_token (parser->lexer);
7816   /* Figure out which operator we have.  */
7817   switch (token->type)
7818     {
7819     case CPP_KEYWORD:
7820       {
7821         enum tree_code op;
7822
7823         /* The keyword should be either `new' or `delete'.  */
7824         if (token->keyword == RID_NEW)
7825           op = NEW_EXPR;
7826         else if (token->keyword == RID_DELETE)
7827           op = DELETE_EXPR;
7828         else
7829           break;
7830
7831         /* Consume the `new' or `delete' token.  */
7832         cp_lexer_consume_token (parser->lexer);
7833
7834         /* Peek at the next token.  */
7835         token = cp_lexer_peek_token (parser->lexer);
7836         /* If it's a `[' token then this is the array variant of the
7837            operator.  */
7838         if (token->type == CPP_OPEN_SQUARE)
7839           {
7840             /* Consume the `[' token.  */
7841             cp_lexer_consume_token (parser->lexer);
7842             /* Look for the `]' token.  */
7843             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7844             id = ansi_opname (op == NEW_EXPR
7845                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7846           }
7847         /* Otherwise, we have the non-array variant.  */
7848         else
7849           id = ansi_opname (op);
7850
7851         return id;
7852       }
7853
7854     case CPP_PLUS:
7855       id = ansi_opname (PLUS_EXPR);
7856       break;
7857
7858     case CPP_MINUS:
7859       id = ansi_opname (MINUS_EXPR);
7860       break;
7861
7862     case CPP_MULT:
7863       id = ansi_opname (MULT_EXPR);
7864       break;
7865
7866     case CPP_DIV:
7867       id = ansi_opname (TRUNC_DIV_EXPR);
7868       break;
7869
7870     case CPP_MOD:
7871       id = ansi_opname (TRUNC_MOD_EXPR);
7872       break;
7873
7874     case CPP_XOR:
7875       id = ansi_opname (BIT_XOR_EXPR);
7876       break;
7877
7878     case CPP_AND:
7879       id = ansi_opname (BIT_AND_EXPR);
7880       break;
7881
7882     case CPP_OR:
7883       id = ansi_opname (BIT_IOR_EXPR);
7884       break;
7885
7886     case CPP_COMPL:
7887       id = ansi_opname (BIT_NOT_EXPR);
7888       break;
7889
7890     case CPP_NOT:
7891       id = ansi_opname (TRUTH_NOT_EXPR);
7892       break;
7893
7894     case CPP_EQ:
7895       id = ansi_assopname (NOP_EXPR);
7896       break;
7897
7898     case CPP_LESS:
7899       id = ansi_opname (LT_EXPR);
7900       break;
7901
7902     case CPP_GREATER:
7903       id = ansi_opname (GT_EXPR);
7904       break;
7905
7906     case CPP_PLUS_EQ:
7907       id = ansi_assopname (PLUS_EXPR);
7908       break;
7909
7910     case CPP_MINUS_EQ:
7911       id = ansi_assopname (MINUS_EXPR);
7912       break;
7913
7914     case CPP_MULT_EQ:
7915       id = ansi_assopname (MULT_EXPR);
7916       break;
7917
7918     case CPP_DIV_EQ:
7919       id = ansi_assopname (TRUNC_DIV_EXPR);
7920       break;
7921
7922     case CPP_MOD_EQ:
7923       id = ansi_assopname (TRUNC_MOD_EXPR);
7924       break;
7925
7926     case CPP_XOR_EQ:
7927       id = ansi_assopname (BIT_XOR_EXPR);
7928       break;
7929
7930     case CPP_AND_EQ:
7931       id = ansi_assopname (BIT_AND_EXPR);
7932       break;
7933
7934     case CPP_OR_EQ:
7935       id = ansi_assopname (BIT_IOR_EXPR);
7936       break;
7937
7938     case CPP_LSHIFT:
7939       id = ansi_opname (LSHIFT_EXPR);
7940       break;
7941
7942     case CPP_RSHIFT:
7943       id = ansi_opname (RSHIFT_EXPR);
7944       break;
7945
7946     case CPP_LSHIFT_EQ:
7947       id = ansi_assopname (LSHIFT_EXPR);
7948       break;
7949
7950     case CPP_RSHIFT_EQ:
7951       id = ansi_assopname (RSHIFT_EXPR);
7952       break;
7953
7954     case CPP_EQ_EQ:
7955       id = ansi_opname (EQ_EXPR);
7956       break;
7957
7958     case CPP_NOT_EQ:
7959       id = ansi_opname (NE_EXPR);
7960       break;
7961
7962     case CPP_LESS_EQ:
7963       id = ansi_opname (LE_EXPR);
7964       break;
7965
7966     case CPP_GREATER_EQ:
7967       id = ansi_opname (GE_EXPR);
7968       break;
7969
7970     case CPP_AND_AND:
7971       id = ansi_opname (TRUTH_ANDIF_EXPR);
7972       break;
7973
7974     case CPP_OR_OR:
7975       id = ansi_opname (TRUTH_ORIF_EXPR);
7976       break;
7977
7978     case CPP_PLUS_PLUS:
7979       id = ansi_opname (POSTINCREMENT_EXPR);
7980       break;
7981
7982     case CPP_MINUS_MINUS:
7983       id = ansi_opname (PREDECREMENT_EXPR);
7984       break;
7985
7986     case CPP_COMMA:
7987       id = ansi_opname (COMPOUND_EXPR);
7988       break;
7989
7990     case CPP_DEREF_STAR:
7991       id = ansi_opname (MEMBER_REF);
7992       break;
7993
7994     case CPP_DEREF:
7995       id = ansi_opname (COMPONENT_REF);
7996       break;
7997
7998     case CPP_OPEN_PAREN:
7999       /* Consume the `('.  */
8000       cp_lexer_consume_token (parser->lexer);
8001       /* Look for the matching `)'.  */
8002       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8003       return ansi_opname (CALL_EXPR);
8004
8005     case CPP_OPEN_SQUARE:
8006       /* Consume the `['.  */
8007       cp_lexer_consume_token (parser->lexer);
8008       /* Look for the matching `]'.  */
8009       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8010       return ansi_opname (ARRAY_REF);
8011
8012       /* Extensions.  */
8013     case CPP_MIN:
8014       id = ansi_opname (MIN_EXPR);
8015       break;
8016
8017     case CPP_MAX:
8018       id = ansi_opname (MAX_EXPR);
8019       break;
8020
8021     case CPP_MIN_EQ:
8022       id = ansi_assopname (MIN_EXPR);
8023       break;
8024
8025     case CPP_MAX_EQ:
8026       id = ansi_assopname (MAX_EXPR);
8027       break;
8028
8029     default:
8030       /* Anything else is an error.  */
8031       break;
8032     }
8033
8034   /* If we have selected an identifier, we need to consume the
8035      operator token.  */
8036   if (id)
8037     cp_lexer_consume_token (parser->lexer);
8038   /* Otherwise, no valid operator name was present.  */
8039   else
8040     {
8041       cp_parser_error (parser, "expected operator");
8042       id = error_mark_node;
8043     }
8044
8045   return id;
8046 }
8047
8048 /* Parse a template-declaration.
8049
8050    template-declaration:
8051      export [opt] template < template-parameter-list > declaration
8052
8053    If MEMBER_P is TRUE, this template-declaration occurs within a
8054    class-specifier.
8055
8056    The grammar rule given by the standard isn't correct.  What
8057    is really meant is:
8058
8059    template-declaration:
8060      export [opt] template-parameter-list-seq
8061        decl-specifier-seq [opt] init-declarator [opt] ;
8062      export [opt] template-parameter-list-seq
8063        function-definition
8064
8065    template-parameter-list-seq:
8066      template-parameter-list-seq [opt]
8067      template < template-parameter-list >  */
8068
8069 static void
8070 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8071 {
8072   /* Check for `export'.  */
8073   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8074     {
8075       /* Consume the `export' token.  */
8076       cp_lexer_consume_token (parser->lexer);
8077       /* Warn that we do not support `export'.  */
8078       warning ("keyword %<export%> not implemented, and will be ignored");
8079     }
8080
8081   cp_parser_template_declaration_after_export (parser, member_p);
8082 }
8083
8084 /* Parse a template-parameter-list.
8085
8086    template-parameter-list:
8087      template-parameter
8088      template-parameter-list , template-parameter
8089
8090    Returns a TREE_LIST.  Each node represents a template parameter.
8091    The nodes are connected via their TREE_CHAINs.  */
8092
8093 static tree
8094 cp_parser_template_parameter_list (cp_parser* parser)
8095 {
8096   tree parameter_list = NULL_TREE;
8097
8098   while (true)
8099     {
8100       tree parameter;
8101       cp_token *token;
8102       bool is_non_type;
8103
8104       /* Parse the template-parameter.  */
8105       parameter = cp_parser_template_parameter (parser, &is_non_type);
8106       /* Add it to the list.  */
8107       parameter_list = process_template_parm (parameter_list,
8108                                               parameter,
8109                                               is_non_type);
8110       /* Peek at the next token.  */
8111       token = cp_lexer_peek_token (parser->lexer);
8112       /* If it's not a `,', we're done.  */
8113       if (token->type != CPP_COMMA)
8114         break;
8115       /* Otherwise, consume the `,' token.  */
8116       cp_lexer_consume_token (parser->lexer);
8117     }
8118
8119   return parameter_list;
8120 }
8121
8122 /* Parse a template-parameter.
8123
8124    template-parameter:
8125      type-parameter
8126      parameter-declaration
8127
8128    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8129    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8130    true iff this parameter is a non-type parameter.  */
8131
8132 static tree
8133 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8134 {
8135   cp_token *token;
8136   cp_parameter_declarator *parameter_declarator;
8137
8138   /* Assume it is a type parameter or a template parameter.  */
8139   *is_non_type = false;
8140   /* Peek at the next token.  */
8141   token = cp_lexer_peek_token (parser->lexer);
8142   /* If it is `class' or `template', we have a type-parameter.  */
8143   if (token->keyword == RID_TEMPLATE)
8144     return cp_parser_type_parameter (parser);
8145   /* If it is `class' or `typename' we do not know yet whether it is a
8146      type parameter or a non-type parameter.  Consider:
8147
8148        template <typename T, typename T::X X> ...
8149
8150      or:
8151
8152        template <class C, class D*> ...
8153
8154      Here, the first parameter is a type parameter, and the second is
8155      a non-type parameter.  We can tell by looking at the token after
8156      the identifier -- if it is a `,', `=', or `>' then we have a type
8157      parameter.  */
8158   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8159     {
8160       /* Peek at the token after `class' or `typename'.  */
8161       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8162       /* If it's an identifier, skip it.  */
8163       if (token->type == CPP_NAME)
8164         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8165       /* Now, see if the token looks like the end of a template
8166          parameter.  */
8167       if (token->type == CPP_COMMA
8168           || token->type == CPP_EQ
8169           || token->type == CPP_GREATER)
8170         return cp_parser_type_parameter (parser);
8171     }
8172
8173   /* Otherwise, it is a non-type parameter.
8174
8175      [temp.param]
8176
8177      When parsing a default template-argument for a non-type
8178      template-parameter, the first non-nested `>' is taken as the end
8179      of the template parameter-list rather than a greater-than
8180      operator.  */
8181   *is_non_type = true;
8182   parameter_declarator
8183      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8184                                         /*parenthesized_p=*/NULL);
8185   return (build_tree_list
8186           (parameter_declarator->default_argument,
8187            grokdeclarator (parameter_declarator->declarator,
8188                            &parameter_declarator->decl_specifiers,
8189                            PARM, /*initialized=*/0,
8190                            /*attrlist=*/NULL)));
8191 }
8192
8193 /* Parse a type-parameter.
8194
8195    type-parameter:
8196      class identifier [opt]
8197      class identifier [opt] = type-id
8198      typename identifier [opt]
8199      typename identifier [opt] = type-id
8200      template < template-parameter-list > class identifier [opt]
8201      template < template-parameter-list > class identifier [opt]
8202        = id-expression
8203
8204    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8205    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8206    the declaration of the parameter.  */
8207
8208 static tree
8209 cp_parser_type_parameter (cp_parser* parser)
8210 {
8211   cp_token *token;
8212   tree parameter;
8213
8214   /* Look for a keyword to tell us what kind of parameter this is.  */
8215   token = cp_parser_require (parser, CPP_KEYWORD,
8216                              "`class', `typename', or `template'");
8217   if (!token)
8218     return error_mark_node;
8219
8220   switch (token->keyword)
8221     {
8222     case RID_CLASS:
8223     case RID_TYPENAME:
8224       {
8225         tree identifier;
8226         tree default_argument;
8227
8228         /* If the next token is an identifier, then it names the
8229            parameter.  */
8230         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8231           identifier = cp_parser_identifier (parser);
8232         else
8233           identifier = NULL_TREE;
8234
8235         /* Create the parameter.  */
8236         parameter = finish_template_type_parm (class_type_node, identifier);
8237
8238         /* If the next token is an `=', we have a default argument.  */
8239         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8240           {
8241             /* Consume the `=' token.  */
8242             cp_lexer_consume_token (parser->lexer);
8243             /* Parse the default-argument.  */
8244             default_argument = cp_parser_type_id (parser);
8245           }
8246         else
8247           default_argument = NULL_TREE;
8248
8249         /* Create the combined representation of the parameter and the
8250            default argument.  */
8251         parameter = build_tree_list (default_argument, parameter);
8252       }
8253       break;
8254
8255     case RID_TEMPLATE:
8256       {
8257         tree parameter_list;
8258         tree identifier;
8259         tree default_argument;
8260
8261         /* Look for the `<'.  */
8262         cp_parser_require (parser, CPP_LESS, "`<'");
8263         /* Parse the template-parameter-list.  */
8264         begin_template_parm_list ();
8265         parameter_list
8266           = cp_parser_template_parameter_list (parser);
8267         parameter_list = end_template_parm_list (parameter_list);
8268         /* Look for the `>'.  */
8269         cp_parser_require (parser, CPP_GREATER, "`>'");
8270         /* Look for the `class' keyword.  */
8271         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8272         /* If the next token is an `=', then there is a
8273            default-argument.  If the next token is a `>', we are at
8274            the end of the parameter-list.  If the next token is a `,',
8275            then we are at the end of this parameter.  */
8276         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8277             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8278             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8279           identifier = cp_parser_identifier (parser);
8280         else
8281           identifier = NULL_TREE;
8282         /* Create the template parameter.  */
8283         parameter = finish_template_template_parm (class_type_node,
8284                                                    identifier);
8285
8286         /* If the next token is an `=', then there is a
8287            default-argument.  */
8288         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8289           {
8290             bool is_template;
8291
8292             /* Consume the `='.  */
8293             cp_lexer_consume_token (parser->lexer);
8294             /* Parse the id-expression.  */
8295             default_argument
8296               = cp_parser_id_expression (parser,
8297                                          /*template_keyword_p=*/false,
8298                                          /*check_dependency_p=*/true,
8299                                          /*template_p=*/&is_template,
8300                                          /*declarator_p=*/false);
8301             if (TREE_CODE (default_argument) == TYPE_DECL)
8302               /* If the id-expression was a template-id that refers to
8303                  a template-class, we already have the declaration here,
8304                  so no further lookup is needed.  */
8305                  ;
8306             else
8307               /* Look up the name.  */
8308               default_argument
8309                 = cp_parser_lookup_name (parser, default_argument,
8310                                         /*is_type=*/false,
8311                                         /*is_template=*/is_template,
8312                                         /*is_namespace=*/false,
8313                                         /*check_dependency=*/true,
8314                                         /*ambiguous_p=*/NULL);
8315             /* See if the default argument is valid.  */
8316             default_argument
8317               = check_template_template_default_arg (default_argument);
8318           }
8319         else
8320           default_argument = NULL_TREE;
8321
8322         /* Create the combined representation of the parameter and the
8323            default argument.  */
8324         parameter =  build_tree_list (default_argument, parameter);
8325       }
8326       break;
8327
8328     default:
8329       /* Anything else is an error.  */
8330       cp_parser_error (parser,
8331                        "expected %<class%>, %<typename%>, or %<template%>");
8332       parameter = error_mark_node;
8333     }
8334
8335   return parameter;
8336 }
8337
8338 /* Parse a template-id.
8339
8340    template-id:
8341      template-name < template-argument-list [opt] >
8342
8343    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8344    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8345    returned.  Otherwise, if the template-name names a function, or set
8346    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8347    names a class, returns a TYPE_DECL for the specialization.
8348
8349    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8350    uninstantiated templates.  */
8351
8352 static tree
8353 cp_parser_template_id (cp_parser *parser,
8354                        bool template_keyword_p,
8355                        bool check_dependency_p,
8356                        bool is_declaration)
8357 {
8358   tree template;
8359   tree arguments;
8360   tree template_id;
8361   ptrdiff_t start_of_id;
8362   tree access_check = NULL_TREE;
8363   cp_token *next_token, *next_token_2;
8364   bool is_identifier;
8365
8366   /* If the next token corresponds to a template-id, there is no need
8367      to reparse it.  */
8368   next_token = cp_lexer_peek_token (parser->lexer);
8369   if (next_token->type == CPP_TEMPLATE_ID)
8370     {
8371       tree value;
8372       tree check;
8373
8374       /* Get the stored value.  */
8375       value = cp_lexer_consume_token (parser->lexer)->value;
8376       /* Perform any access checks that were deferred.  */
8377       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8378         perform_or_defer_access_check (TREE_PURPOSE (check),
8379                                        TREE_VALUE (check));
8380       /* Return the stored value.  */
8381       return TREE_VALUE (value);
8382     }
8383
8384   /* Avoid performing name lookup if there is no possibility of
8385      finding a template-id.  */
8386   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8387       || (next_token->type == CPP_NAME
8388           && !cp_parser_nth_token_starts_template_argument_list_p
8389                (parser, 2)))
8390     {
8391       cp_parser_error (parser, "expected template-id");
8392       return error_mark_node;
8393     }
8394
8395   /* Remember where the template-id starts.  */
8396   if (cp_parser_parsing_tentatively (parser)
8397       && !cp_parser_committed_to_tentative_parse (parser))
8398     {
8399       next_token = cp_lexer_peek_token (parser->lexer);
8400       start_of_id = cp_lexer_token_difference (parser->lexer,
8401                                                parser->lexer->buffer,
8402                                                next_token);
8403     }
8404   else
8405     start_of_id = -1;
8406
8407   push_deferring_access_checks (dk_deferred);
8408
8409   /* Parse the template-name.  */
8410   is_identifier = false;
8411   template = cp_parser_template_name (parser, template_keyword_p,
8412                                       check_dependency_p,
8413                                       is_declaration,
8414                                       &is_identifier);
8415   if (template == error_mark_node || is_identifier)
8416     {
8417       pop_deferring_access_checks ();
8418       return template;
8419     }
8420
8421   /* If we find the sequence `[:' after a template-name, it's probably
8422      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8423      parse correctly the argument list.  */
8424   next_token = cp_lexer_peek_token (parser->lexer);
8425   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8426   if (next_token->type == CPP_OPEN_SQUARE
8427       && next_token->flags & DIGRAPH
8428       && next_token_2->type == CPP_COLON
8429       && !(next_token_2->flags & PREV_WHITE))
8430     {
8431       cp_parser_parse_tentatively (parser);
8432       /* Change `:' into `::'.  */
8433       next_token_2->type = CPP_SCOPE;
8434       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8435          CPP_LESS.  */
8436       cp_lexer_consume_token (parser->lexer);
8437       /* Parse the arguments.  */
8438       arguments = cp_parser_enclosed_template_argument_list (parser);
8439       if (!cp_parser_parse_definitely (parser))
8440         {
8441           /* If we couldn't parse an argument list, then we revert our changes
8442              and return simply an error. Maybe this is not a template-id
8443              after all.  */
8444           next_token_2->type = CPP_COLON;
8445           cp_parser_error (parser, "expected %<<%>");
8446           pop_deferring_access_checks ();
8447           return error_mark_node;
8448         }
8449       /* Otherwise, emit an error about the invalid digraph, but continue
8450          parsing because we got our argument list.  */
8451       pedwarn ("%<<::%> cannot begin a template-argument list");
8452       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8453               "between %<<%> and %<::%>");
8454       if (!flag_permissive)
8455         {
8456           static bool hint;
8457           if (!hint)
8458             {
8459               inform ("(if you use -fpermissive G++ will accept your code)");
8460               hint = true;
8461             }
8462         }
8463     }
8464   else
8465     {
8466       /* Look for the `<' that starts the template-argument-list.  */
8467       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8468         {
8469           pop_deferring_access_checks ();
8470           return error_mark_node;
8471         }
8472       /* Parse the arguments.  */
8473       arguments = cp_parser_enclosed_template_argument_list (parser);
8474     }
8475
8476   /* Build a representation of the specialization.  */
8477   if (TREE_CODE (template) == IDENTIFIER_NODE)
8478     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8479   else if (DECL_CLASS_TEMPLATE_P (template)
8480            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8481     template_id
8482       = finish_template_type (template, arguments,
8483                               cp_lexer_next_token_is (parser->lexer,
8484                                                       CPP_SCOPE));
8485   else
8486     {
8487       /* If it's not a class-template or a template-template, it should be
8488          a function-template.  */
8489       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8490                    || TREE_CODE (template) == OVERLOAD
8491                    || BASELINK_P (template)));
8492
8493       template_id = lookup_template_function (template, arguments);
8494     }
8495
8496   /* Retrieve any deferred checks.  Do not pop this access checks yet
8497      so the memory will not be reclaimed during token replacing below.  */
8498   access_check = get_deferred_access_checks ();
8499
8500   /* If parsing tentatively, replace the sequence of tokens that makes
8501      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8502      should we re-parse the token stream, we will not have to repeat
8503      the effort required to do the parse, nor will we issue duplicate
8504      error messages about problems during instantiation of the
8505      template.  */
8506   if (start_of_id >= 0)
8507     {
8508       cp_token *token;
8509
8510       /* Find the token that corresponds to the start of the
8511          template-id.  */
8512       token = cp_lexer_advance_token (parser->lexer,
8513                                       parser->lexer->buffer,
8514                                       start_of_id);
8515
8516       /* Reset the contents of the START_OF_ID token.  */
8517       token->type = CPP_TEMPLATE_ID;
8518       token->value = build_tree_list (access_check, template_id);
8519       token->keyword = RID_MAX;
8520       /* Purge all subsequent tokens.  */
8521       cp_lexer_purge_tokens_after (parser->lexer, token);
8522     }
8523
8524   pop_deferring_access_checks ();
8525   return template_id;
8526 }
8527
8528 /* Parse a template-name.
8529
8530    template-name:
8531      identifier
8532
8533    The standard should actually say:
8534
8535    template-name:
8536      identifier
8537      operator-function-id
8538
8539    A defect report has been filed about this issue.
8540
8541    A conversion-function-id cannot be a template name because they cannot
8542    be part of a template-id. In fact, looking at this code:
8543
8544    a.operator K<int>()
8545
8546    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8547    It is impossible to call a templated conversion-function-id with an
8548    explicit argument list, since the only allowed template parameter is
8549    the type to which it is converting.
8550
8551    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8552    `template' keyword, in a construction like:
8553
8554      T::template f<3>()
8555
8556    In that case `f' is taken to be a template-name, even though there
8557    is no way of knowing for sure.
8558
8559    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8560    name refers to a set of overloaded functions, at least one of which
8561    is a template, or an IDENTIFIER_NODE with the name of the template,
8562    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8563    names are looked up inside uninstantiated templates.  */
8564
8565 static tree
8566 cp_parser_template_name (cp_parser* parser,
8567                          bool template_keyword_p,
8568                          bool check_dependency_p,
8569                          bool is_declaration,
8570                          bool *is_identifier)
8571 {
8572   tree identifier;
8573   tree decl;
8574   tree fns;
8575
8576   /* If the next token is `operator', then we have either an
8577      operator-function-id or a conversion-function-id.  */
8578   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8579     {
8580       /* We don't know whether we're looking at an
8581          operator-function-id or a conversion-function-id.  */
8582       cp_parser_parse_tentatively (parser);
8583       /* Try an operator-function-id.  */
8584       identifier = cp_parser_operator_function_id (parser);
8585       /* If that didn't work, try a conversion-function-id.  */
8586       if (!cp_parser_parse_definitely (parser))
8587         {
8588           cp_parser_error (parser, "expected template-name");
8589           return error_mark_node;
8590         }
8591     }
8592   /* Look for the identifier.  */
8593   else
8594     identifier = cp_parser_identifier (parser);
8595
8596   /* If we didn't find an identifier, we don't have a template-id.  */
8597   if (identifier == error_mark_node)
8598     return error_mark_node;
8599
8600   /* If the name immediately followed the `template' keyword, then it
8601      is a template-name.  However, if the next token is not `<', then
8602      we do not treat it as a template-name, since it is not being used
8603      as part of a template-id.  This enables us to handle constructs
8604      like:
8605
8606        template <typename T> struct S { S(); };
8607        template <typename T> S<T>::S();
8608
8609      correctly.  We would treat `S' as a template -- if it were `S<T>'
8610      -- but we do not if there is no `<'.  */
8611
8612   if (processing_template_decl
8613       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8614     {
8615       /* In a declaration, in a dependent context, we pretend that the
8616          "template" keyword was present in order to improve error
8617          recovery.  For example, given:
8618
8619            template <typename T> void f(T::X<int>);
8620
8621          we want to treat "X<int>" as a template-id.  */
8622       if (is_declaration
8623           && !template_keyword_p
8624           && parser->scope && TYPE_P (parser->scope)
8625           && check_dependency_p
8626           && dependent_type_p (parser->scope)
8627           /* Do not do this for dtors (or ctors), since they never
8628              need the template keyword before their name.  */
8629           && !constructor_name_p (identifier, parser->scope))
8630         {
8631           ptrdiff_t start;
8632           cp_token* token;
8633           /* Explain what went wrong.  */
8634           error ("non-template %qD used as template", identifier);
8635           inform ("use %<%T::template %D%> to indicate that it is a template",
8636                   parser->scope, identifier);
8637           /* If parsing tentatively, find the location of the "<"
8638              token.  */
8639           if (cp_parser_parsing_tentatively (parser)
8640               && !cp_parser_committed_to_tentative_parse (parser))
8641             {
8642               cp_parser_simulate_error (parser);
8643               token = cp_lexer_peek_token (parser->lexer);
8644               token = cp_lexer_prev_token (parser->lexer, token);
8645               start = cp_lexer_token_difference (parser->lexer,
8646                                                  parser->lexer->buffer,
8647                                                  token);
8648             }
8649           else
8650             start = -1;
8651           /* Parse the template arguments so that we can issue error
8652              messages about them.  */
8653           cp_lexer_consume_token (parser->lexer);
8654           cp_parser_enclosed_template_argument_list (parser);
8655           /* Skip tokens until we find a good place from which to
8656              continue parsing.  */
8657           cp_parser_skip_to_closing_parenthesis (parser,
8658                                                  /*recovering=*/true,
8659                                                  /*or_comma=*/true,
8660                                                  /*consume_paren=*/false);
8661           /* If parsing tentatively, permanently remove the
8662              template argument list.  That will prevent duplicate
8663              error messages from being issued about the missing
8664              "template" keyword.  */
8665           if (start >= 0)
8666             {
8667               token = cp_lexer_advance_token (parser->lexer,
8668                                               parser->lexer->buffer,
8669                                               start);
8670               cp_lexer_purge_tokens_after (parser->lexer, token);
8671             }
8672           if (is_identifier)
8673             *is_identifier = true;
8674           return identifier;
8675         }
8676
8677       /* If the "template" keyword is present, then there is generally
8678          no point in doing name-lookup, so we just return IDENTIFIER.
8679          But, if the qualifying scope is non-dependent then we can
8680          (and must) do name-lookup normally.  */
8681       if (template_keyword_p
8682           && (!parser->scope
8683               || (TYPE_P (parser->scope)
8684                   && dependent_type_p (parser->scope))))
8685         return identifier;
8686     }
8687
8688   /* Look up the name.  */
8689   decl = cp_parser_lookup_name (parser, identifier,
8690                                 /*is_type=*/false,
8691                                 /*is_template=*/false,
8692                                 /*is_namespace=*/false,
8693                                 check_dependency_p,
8694                                 /*ambiguous_p=*/NULL);
8695   decl = maybe_get_template_decl_from_type_decl (decl);
8696
8697   /* If DECL is a template, then the name was a template-name.  */
8698   if (TREE_CODE (decl) == TEMPLATE_DECL)
8699     ;
8700   else
8701     {
8702       /* The standard does not explicitly indicate whether a name that
8703          names a set of overloaded declarations, some of which are
8704          templates, is a template-name.  However, such a name should
8705          be a template-name; otherwise, there is no way to form a
8706          template-id for the overloaded templates.  */
8707       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8708       if (TREE_CODE (fns) == OVERLOAD)
8709         {
8710           tree fn;
8711
8712           for (fn = fns; fn; fn = OVL_NEXT (fn))
8713             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8714               break;
8715         }
8716       else
8717         {
8718           /* Otherwise, the name does not name a template.  */
8719           cp_parser_error (parser, "expected template-name");
8720           return error_mark_node;
8721         }
8722     }
8723
8724   /* If DECL is dependent, and refers to a function, then just return
8725      its name; we will look it up again during template instantiation.  */
8726   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8727     {
8728       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8729       if (TYPE_P (scope) && dependent_type_p (scope))
8730         return identifier;
8731     }
8732
8733   return decl;
8734 }
8735
8736 /* Parse a template-argument-list.
8737
8738    template-argument-list:
8739      template-argument
8740      template-argument-list , template-argument
8741
8742    Returns a TREE_VEC containing the arguments.  */
8743
8744 static tree
8745 cp_parser_template_argument_list (cp_parser* parser)
8746 {
8747   tree fixed_args[10];
8748   unsigned n_args = 0;
8749   unsigned alloced = 10;
8750   tree *arg_ary = fixed_args;
8751   tree vec;
8752   bool saved_in_template_argument_list_p;
8753
8754   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8755   parser->in_template_argument_list_p = true;
8756   do
8757     {
8758       tree argument;
8759
8760       if (n_args)
8761         /* Consume the comma.  */
8762         cp_lexer_consume_token (parser->lexer);
8763
8764       /* Parse the template-argument.  */
8765       argument = cp_parser_template_argument (parser);
8766       if (n_args == alloced)
8767         {
8768           alloced *= 2;
8769
8770           if (arg_ary == fixed_args)
8771             {
8772               arg_ary = xmalloc (sizeof (tree) * alloced);
8773               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8774             }
8775           else
8776             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8777         }
8778       arg_ary[n_args++] = argument;
8779     }
8780   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8781
8782   vec = make_tree_vec (n_args);
8783
8784   while (n_args--)
8785     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8786
8787   if (arg_ary != fixed_args)
8788     free (arg_ary);
8789   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8790   return vec;
8791 }
8792
8793 /* Parse a template-argument.
8794
8795    template-argument:
8796      assignment-expression
8797      type-id
8798      id-expression
8799
8800    The representation is that of an assignment-expression, type-id, or
8801    id-expression -- except that the qualified id-expression is
8802    evaluated, so that the value returned is either a DECL or an
8803    OVERLOAD.
8804
8805    Although the standard says "assignment-expression", it forbids
8806    throw-expressions or assignments in the template argument.
8807    Therefore, we use "conditional-expression" instead.  */
8808
8809 static tree
8810 cp_parser_template_argument (cp_parser* parser)
8811 {
8812   tree argument;
8813   bool template_p;
8814   bool address_p;
8815   bool maybe_type_id = false;
8816   cp_token *token;
8817   cp_id_kind idk;
8818   tree qualifying_class;
8819
8820   /* There's really no way to know what we're looking at, so we just
8821      try each alternative in order.
8822
8823        [temp.arg]
8824
8825        In a template-argument, an ambiguity between a type-id and an
8826        expression is resolved to a type-id, regardless of the form of
8827        the corresponding template-parameter.
8828
8829      Therefore, we try a type-id first.  */
8830   cp_parser_parse_tentatively (parser);
8831   argument = cp_parser_type_id (parser);
8832   /* If there was no error parsing the type-id but the next token is a '>>',
8833      we probably found a typo for '> >'. But there are type-id which are
8834      also valid expressions. For instance:
8835
8836      struct X { int operator >> (int); };
8837      template <int V> struct Foo {};
8838      Foo<X () >> 5> r;
8839
8840      Here 'X()' is a valid type-id of a function type, but the user just
8841      wanted to write the expression "X() >> 5". Thus, we remember that we
8842      found a valid type-id, but we still try to parse the argument as an
8843      expression to see what happens.  */
8844   if (!cp_parser_error_occurred (parser)
8845       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8846     {
8847       maybe_type_id = true;
8848       cp_parser_abort_tentative_parse (parser);
8849     }
8850   else
8851     {
8852       /* If the next token isn't a `,' or a `>', then this argument wasn't
8853       really finished. This means that the argument is not a valid
8854       type-id.  */
8855       if (!cp_parser_next_token_ends_template_argument_p (parser))
8856         cp_parser_error (parser, "expected template-argument");
8857       /* If that worked, we're done.  */
8858       if (cp_parser_parse_definitely (parser))
8859         return argument;
8860     }
8861   /* We're still not sure what the argument will be.  */
8862   cp_parser_parse_tentatively (parser);
8863   /* Try a template.  */
8864   argument = cp_parser_id_expression (parser,
8865                                       /*template_keyword_p=*/false,
8866                                       /*check_dependency_p=*/true,
8867                                       &template_p,
8868                                       /*declarator_p=*/false);
8869   /* If the next token isn't a `,' or a `>', then this argument wasn't
8870      really finished.  */
8871   if (!cp_parser_next_token_ends_template_argument_p (parser))
8872     cp_parser_error (parser, "expected template-argument");
8873   if (!cp_parser_error_occurred (parser))
8874     {
8875       /* Figure out what is being referred to.  If the id-expression
8876          was for a class template specialization, then we will have a
8877          TYPE_DECL at this point.  There is no need to do name lookup
8878          at this point in that case.  */
8879       if (TREE_CODE (argument) != TYPE_DECL)
8880         argument = cp_parser_lookup_name (parser, argument,
8881                                           /*is_type=*/false,
8882                                           /*is_template=*/template_p,
8883                                           /*is_namespace=*/false,
8884                                           /*check_dependency=*/true,
8885                                           /*ambiguous_p=*/NULL);
8886       if (TREE_CODE (argument) != TEMPLATE_DECL
8887           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8888         cp_parser_error (parser, "expected template-name");
8889     }
8890   if (cp_parser_parse_definitely (parser))
8891     return argument;
8892   /* It must be a non-type argument.  There permitted cases are given
8893      in [temp.arg.nontype]:
8894
8895      -- an integral constant-expression of integral or enumeration
8896         type; or
8897
8898      -- the name of a non-type template-parameter; or
8899
8900      -- the name of an object or function with external linkage...
8901
8902      -- the address of an object or function with external linkage...
8903
8904      -- a pointer to member...  */
8905   /* Look for a non-type template parameter.  */
8906   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8907     {
8908       cp_parser_parse_tentatively (parser);
8909       argument = cp_parser_primary_expression (parser,
8910                                                &idk,
8911                                                &qualifying_class);
8912       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8913           || !cp_parser_next_token_ends_template_argument_p (parser))
8914         cp_parser_simulate_error (parser);
8915       if (cp_parser_parse_definitely (parser))
8916         return argument;
8917     }
8918   /* If the next token is "&", the argument must be the address of an
8919      object or function with external linkage.  */
8920   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8921   if (address_p)
8922     cp_lexer_consume_token (parser->lexer);
8923   /* See if we might have an id-expression.  */
8924   token = cp_lexer_peek_token (parser->lexer);
8925   if (token->type == CPP_NAME
8926       || token->keyword == RID_OPERATOR
8927       || token->type == CPP_SCOPE
8928       || token->type == CPP_TEMPLATE_ID
8929       || token->type == CPP_NESTED_NAME_SPECIFIER)
8930     {
8931       cp_parser_parse_tentatively (parser);
8932       argument = cp_parser_primary_expression (parser,
8933                                                &idk,
8934                                                &qualifying_class);
8935       if (cp_parser_error_occurred (parser)
8936           || !cp_parser_next_token_ends_template_argument_p (parser))
8937         cp_parser_abort_tentative_parse (parser);
8938       else
8939         {
8940           if (qualifying_class)
8941             argument = finish_qualified_id_expr (qualifying_class,
8942                                                  argument,
8943                                                  /*done=*/true,
8944                                                  address_p);
8945           if (TREE_CODE (argument) == VAR_DECL)
8946             {
8947               /* A variable without external linkage might still be a
8948                  valid constant-expression, so no error is issued here
8949                  if the external-linkage check fails.  */
8950               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8951                 cp_parser_simulate_error (parser);
8952             }
8953           else if (is_overloaded_fn (argument))
8954             /* All overloaded functions are allowed; if the external
8955                linkage test does not pass, an error will be issued
8956                later.  */
8957             ;
8958           else if (address_p
8959                    && (TREE_CODE (argument) == OFFSET_REF
8960                        || TREE_CODE (argument) == SCOPE_REF))
8961             /* A pointer-to-member.  */
8962             ;
8963           else
8964             cp_parser_simulate_error (parser);
8965
8966           if (cp_parser_parse_definitely (parser))
8967             {
8968               if (address_p)
8969                 argument = build_x_unary_op (ADDR_EXPR, argument);
8970               return argument;
8971             }
8972         }
8973     }
8974   /* If the argument started with "&", there are no other valid
8975      alternatives at this point.  */
8976   if (address_p)
8977     {
8978       cp_parser_error (parser, "invalid non-type template argument");
8979       return error_mark_node;
8980     }
8981   /* If the argument wasn't successfully parsed as a type-id followed
8982      by '>>', the argument can only be a constant expression now.
8983      Otherwise, we try parsing the constant-expression tentatively,
8984      because the argument could really be a type-id.  */
8985   if (maybe_type_id)
8986     cp_parser_parse_tentatively (parser);
8987   argument = cp_parser_constant_expression (parser,
8988                                             /*allow_non_constant_p=*/false,
8989                                             /*non_constant_p=*/NULL);
8990   argument = fold_non_dependent_expr (argument);
8991   if (!maybe_type_id)
8992     return argument;
8993   if (!cp_parser_next_token_ends_template_argument_p (parser))
8994     cp_parser_error (parser, "expected template-argument");
8995   if (cp_parser_parse_definitely (parser))
8996     return argument;
8997   /* We did our best to parse the argument as a non type-id, but that
8998      was the only alternative that matched (albeit with a '>' after
8999      it). We can assume it's just a typo from the user, and a
9000      diagnostic will then be issued.  */
9001   return cp_parser_type_id (parser);
9002 }
9003
9004 /* Parse an explicit-instantiation.
9005
9006    explicit-instantiation:
9007      template declaration
9008
9009    Although the standard says `declaration', what it really means is:
9010
9011    explicit-instantiation:
9012      template decl-specifier-seq [opt] declarator [opt] ;
9013
9014    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9015    supposed to be allowed.  A defect report has been filed about this
9016    issue.
9017
9018    GNU Extension:
9019
9020    explicit-instantiation:
9021      storage-class-specifier template
9022        decl-specifier-seq [opt] declarator [opt] ;
9023      function-specifier template
9024        decl-specifier-seq [opt] declarator [opt] ;  */
9025
9026 static void
9027 cp_parser_explicit_instantiation (cp_parser* parser)
9028 {
9029   int declares_class_or_enum;
9030   cp_decl_specifier_seq decl_specifiers;
9031   tree extension_specifier = NULL_TREE;
9032
9033   /* Look for an (optional) storage-class-specifier or
9034      function-specifier.  */
9035   if (cp_parser_allow_gnu_extensions_p (parser))
9036     {
9037       extension_specifier
9038         = cp_parser_storage_class_specifier_opt (parser);
9039       if (!extension_specifier)
9040         extension_specifier
9041           = cp_parser_function_specifier_opt (parser,
9042                                               /*decl_specs=*/NULL);
9043     }
9044
9045   /* Look for the `template' keyword.  */
9046   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9047   /* Let the front end know that we are processing an explicit
9048      instantiation.  */
9049   begin_explicit_instantiation ();
9050   /* [temp.explicit] says that we are supposed to ignore access
9051      control while processing explicit instantiation directives.  */
9052   push_deferring_access_checks (dk_no_check);
9053   /* Parse a decl-specifier-seq.  */
9054   cp_parser_decl_specifier_seq (parser,
9055                                 CP_PARSER_FLAGS_OPTIONAL,
9056                                 &decl_specifiers,
9057                                 &declares_class_or_enum);
9058   /* If there was exactly one decl-specifier, and it declared a class,
9059      and there's no declarator, then we have an explicit type
9060      instantiation.  */
9061   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9062     {
9063       tree type;
9064
9065       type = check_tag_decl (&decl_specifiers);
9066       /* Turn access control back on for names used during
9067          template instantiation.  */
9068       pop_deferring_access_checks ();
9069       if (type)
9070         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9071     }
9072   else
9073     {
9074       cp_declarator *declarator;
9075       tree decl;
9076
9077       /* Parse the declarator.  */
9078       declarator
9079         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9080                                 /*ctor_dtor_or_conv_p=*/NULL,
9081                                 /*parenthesized_p=*/NULL,
9082                                 /*member_p=*/false);
9083       cp_parser_check_for_definition_in_return_type (declarator,
9084                                                      declares_class_or_enum);
9085       if (declarator != cp_error_declarator)
9086         {
9087           decl = grokdeclarator (declarator, &decl_specifiers,
9088                                  NORMAL, 0, NULL);
9089           /* Turn access control back on for names used during
9090              template instantiation.  */
9091           pop_deferring_access_checks ();
9092           /* Do the explicit instantiation.  */
9093           do_decl_instantiation (decl, extension_specifier);
9094         }
9095       else
9096         {
9097           pop_deferring_access_checks ();
9098           /* Skip the body of the explicit instantiation.  */
9099           cp_parser_skip_to_end_of_statement (parser);
9100         }
9101     }
9102   /* We're done with the instantiation.  */
9103   end_explicit_instantiation ();
9104
9105   cp_parser_consume_semicolon_at_end_of_statement (parser);
9106 }
9107
9108 /* Parse an explicit-specialization.
9109
9110    explicit-specialization:
9111      template < > declaration
9112
9113    Although the standard says `declaration', what it really means is:
9114
9115    explicit-specialization:
9116      template <> decl-specifier [opt] init-declarator [opt] ;
9117      template <> function-definition
9118      template <> explicit-specialization
9119      template <> template-declaration  */
9120
9121 static void
9122 cp_parser_explicit_specialization (cp_parser* parser)
9123 {
9124   /* Look for the `template' keyword.  */
9125   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9126   /* Look for the `<'.  */
9127   cp_parser_require (parser, CPP_LESS, "`<'");
9128   /* Look for the `>'.  */
9129   cp_parser_require (parser, CPP_GREATER, "`>'");
9130   /* We have processed another parameter list.  */
9131   ++parser->num_template_parameter_lists;
9132   /* Let the front end know that we are beginning a specialization.  */
9133   begin_specialization ();
9134
9135   /* If the next keyword is `template', we need to figure out whether
9136      or not we're looking a template-declaration.  */
9137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9138     {
9139       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9140           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9141         cp_parser_template_declaration_after_export (parser,
9142                                                      /*member_p=*/false);
9143       else
9144         cp_parser_explicit_specialization (parser);
9145     }
9146   else
9147     /* Parse the dependent declaration.  */
9148     cp_parser_single_declaration (parser,
9149                                   /*member_p=*/false,
9150                                   /*friend_p=*/NULL);
9151
9152   /* We're done with the specialization.  */
9153   end_specialization ();
9154   /* We're done with this parameter list.  */
9155   --parser->num_template_parameter_lists;
9156 }
9157
9158 /* Parse a type-specifier.
9159
9160    type-specifier:
9161      simple-type-specifier
9162      class-specifier
9163      enum-specifier
9164      elaborated-type-specifier
9165      cv-qualifier
9166
9167    GNU Extension:
9168
9169    type-specifier:
9170      __complex__
9171
9172    Returns a representation of the type-specifier.  For a
9173    class-specifier, enum-specifier, or elaborated-type-specifier, a
9174    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9175
9176    The parser flags FLAGS is used to control type-specifier parsing.
9177
9178    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9179    in a decl-specifier-seq.
9180
9181    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9182    class-specifier, enum-specifier, or elaborated-type-specifier, then
9183    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9184    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9185    zero.
9186
9187    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9188    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9189    is set to FALSE.  */
9190
9191 static tree
9192 cp_parser_type_specifier (cp_parser* parser,
9193                           cp_parser_flags flags,
9194                           cp_decl_specifier_seq *decl_specs,
9195                           bool is_declaration,
9196                           int* declares_class_or_enum,
9197                           bool* is_cv_qualifier)
9198 {
9199   tree type_spec = NULL_TREE;
9200   cp_token *token;
9201   enum rid keyword;
9202   cp_decl_spec ds = ds_last;
9203
9204   /* Assume this type-specifier does not declare a new type.  */
9205   if (declares_class_or_enum)
9206     *declares_class_or_enum = 0;
9207   /* And that it does not specify a cv-qualifier.  */
9208   if (is_cv_qualifier)
9209     *is_cv_qualifier = false;
9210   /* Peek at the next token.  */
9211   token = cp_lexer_peek_token (parser->lexer);
9212
9213   /* If we're looking at a keyword, we can use that to guide the
9214      production we choose.  */
9215   keyword = token->keyword;
9216   switch (keyword)
9217     {
9218     case RID_ENUM:
9219       /* 'enum' [identifier] '{' introduces an enum-specifier;
9220          'enum' <anything else> introduces an elaborated-type-specifier.  */
9221       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9222           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9223               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9224                  == CPP_OPEN_BRACE))
9225         {
9226           type_spec = cp_parser_enum_specifier (parser);
9227           if (declares_class_or_enum)
9228             *declares_class_or_enum = 2;
9229           if (decl_specs)
9230             cp_parser_set_decl_spec_type (decl_specs,
9231                                           type_spec,
9232                                           /*user_defined_p=*/true);
9233           return type_spec;
9234         }
9235       else
9236         goto elaborated_type_specifier;
9237
9238       /* Any of these indicate either a class-specifier, or an
9239          elaborated-type-specifier.  */
9240     case RID_CLASS:
9241     case RID_STRUCT:
9242     case RID_UNION:
9243       /* Parse tentatively so that we can back up if we don't find a
9244          class-specifier.  */
9245       cp_parser_parse_tentatively (parser);
9246       /* Look for the class-specifier.  */
9247       type_spec = cp_parser_class_specifier (parser);
9248       /* If that worked, we're done.  */
9249       if (cp_parser_parse_definitely (parser))
9250         {
9251           if (declares_class_or_enum)
9252             *declares_class_or_enum = 2;
9253           if (decl_specs)
9254             cp_parser_set_decl_spec_type (decl_specs,
9255                                           type_spec,
9256                                           /*user_defined_p=*/true);
9257           return type_spec;
9258         }
9259
9260       /* Fall through.  */
9261     elaborated_type_specifier:
9262       /* We're declaring (not defining) a class or enum.  */
9263       if (declares_class_or_enum)
9264         *declares_class_or_enum = 1;
9265
9266       /* Fall through.  */
9267     case RID_TYPENAME:
9268       /* Look for an elaborated-type-specifier.  */
9269       type_spec
9270         = (cp_parser_elaborated_type_specifier
9271            (parser,
9272             decl_specs && decl_specs->specs[(int) ds_friend],
9273             is_declaration));
9274       if (decl_specs)
9275         cp_parser_set_decl_spec_type (decl_specs,
9276                                       type_spec,
9277                                       /*user_defined_p=*/true);
9278       return type_spec;
9279
9280     case RID_CONST:
9281       ds = ds_const;
9282       if (is_cv_qualifier)
9283         *is_cv_qualifier = true;
9284       break;
9285
9286     case RID_VOLATILE:
9287       ds = ds_volatile;
9288       if (is_cv_qualifier)
9289         *is_cv_qualifier = true;
9290       break;
9291
9292     case RID_RESTRICT:
9293       ds = ds_restrict;
9294       if (is_cv_qualifier)
9295         *is_cv_qualifier = true;
9296       break;
9297
9298     case RID_COMPLEX:
9299       /* The `__complex__' keyword is a GNU extension.  */
9300       ds = ds_complex;
9301       break;
9302
9303     default:
9304       break;
9305     }
9306
9307   /* Handle simple keywords.  */
9308   if (ds != ds_last)
9309     {
9310       if (decl_specs)
9311         {
9312           ++decl_specs->specs[(int)ds];
9313           decl_specs->any_specifiers_p = true;
9314         }
9315       return cp_lexer_consume_token (parser->lexer)->value;
9316     }
9317
9318   /* If we do not already have a type-specifier, assume we are looking
9319      at a simple-type-specifier.  */
9320   type_spec = cp_parser_simple_type_specifier (parser,
9321                                                decl_specs,
9322                                                flags);
9323
9324   /* If we didn't find a type-specifier, and a type-specifier was not
9325      optional in this context, issue an error message.  */
9326   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9327     {
9328       cp_parser_error (parser, "expected type specifier");
9329       return error_mark_node;
9330     }
9331
9332   return type_spec;
9333 }
9334
9335 /* Parse a simple-type-specifier.
9336
9337    simple-type-specifier:
9338      :: [opt] nested-name-specifier [opt] type-name
9339      :: [opt] nested-name-specifier template template-id
9340      char
9341      wchar_t
9342      bool
9343      short
9344      int
9345      long
9346      signed
9347      unsigned
9348      float
9349      double
9350      void
9351
9352    GNU Extension:
9353
9354    simple-type-specifier:
9355      __typeof__ unary-expression
9356      __typeof__ ( type-id )
9357
9358    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9359    appropriately updated.  */
9360
9361 static tree
9362 cp_parser_simple_type_specifier (cp_parser* parser,
9363                                  cp_decl_specifier_seq *decl_specs,
9364                                  cp_parser_flags flags)
9365 {
9366   tree type = NULL_TREE;
9367   cp_token *token;
9368
9369   /* Peek at the next token.  */
9370   token = cp_lexer_peek_token (parser->lexer);
9371
9372   /* If we're looking at a keyword, things are easy.  */
9373   switch (token->keyword)
9374     {
9375     case RID_CHAR:
9376       if (decl_specs)
9377         decl_specs->explicit_char_p = true;
9378       type = char_type_node;
9379       break;
9380     case RID_WCHAR:
9381       type = wchar_type_node;
9382       break;
9383     case RID_BOOL:
9384       type = boolean_type_node;
9385       break;
9386     case RID_SHORT:
9387       if (decl_specs)
9388         ++decl_specs->specs[(int) ds_short];
9389       type = short_integer_type_node;
9390       break;
9391     case RID_INT:
9392       if (decl_specs)
9393         decl_specs->explicit_int_p = true;
9394       type = integer_type_node;
9395       break;
9396     case RID_LONG:
9397       if (decl_specs)
9398         ++decl_specs->specs[(int) ds_long];
9399       type = long_integer_type_node;
9400       break;
9401     case RID_SIGNED:
9402       if (decl_specs)
9403         ++decl_specs->specs[(int) ds_signed];
9404       type = integer_type_node;
9405       break;
9406     case RID_UNSIGNED:
9407       if (decl_specs)
9408         ++decl_specs->specs[(int) ds_unsigned];
9409       type = unsigned_type_node;
9410       break;
9411     case RID_FLOAT:
9412       type = float_type_node;
9413       break;
9414     case RID_DOUBLE:
9415       type = double_type_node;
9416       break;
9417     case RID_VOID:
9418       type = void_type_node;
9419       break;
9420
9421     case RID_TYPEOF:
9422       /* Consume the `typeof' token.  */
9423       cp_lexer_consume_token (parser->lexer);
9424       /* Parse the operand to `typeof'.  */
9425       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9426       /* If it is not already a TYPE, take its type.  */
9427       if (!TYPE_P (type))
9428         type = finish_typeof (type);
9429
9430       if (decl_specs)
9431         cp_parser_set_decl_spec_type (decl_specs, type,
9432                                       /*user_defined_p=*/true);
9433
9434       return type;
9435
9436     default:
9437       break;
9438     }
9439
9440   /* If the type-specifier was for a built-in type, we're done.  */
9441   if (type)
9442     {
9443       tree id;
9444
9445       /* Record the type.  */
9446       if (decl_specs
9447           && (token->keyword != RID_SIGNED
9448               && token->keyword != RID_UNSIGNED
9449               && token->keyword != RID_SHORT
9450               && token->keyword != RID_LONG))
9451         cp_parser_set_decl_spec_type (decl_specs,
9452                                       type,
9453                                       /*user_defined=*/false);
9454       if (decl_specs)
9455         decl_specs->any_specifiers_p = true;
9456
9457       /* Consume the token.  */
9458       id = cp_lexer_consume_token (parser->lexer)->value;
9459
9460       /* There is no valid C++ program where a non-template type is
9461          followed by a "<".  That usually indicates that the user thought
9462          that the type was a template.  */
9463       cp_parser_check_for_invalid_template_id (parser, type);
9464
9465       return TYPE_NAME (type);
9466     }
9467
9468   /* The type-specifier must be a user-defined type.  */
9469   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9470     {
9471       bool qualified_p;
9472       bool global_p;
9473
9474       /* Don't gobble tokens or issue error messages if this is an
9475          optional type-specifier.  */
9476       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9477         cp_parser_parse_tentatively (parser);
9478
9479       /* Look for the optional `::' operator.  */
9480       global_p
9481         = (cp_parser_global_scope_opt (parser,
9482                                        /*current_scope_valid_p=*/false)
9483            != NULL_TREE);
9484       /* Look for the nested-name specifier.  */
9485       qualified_p
9486         = (cp_parser_nested_name_specifier_opt (parser,
9487                                                 /*typename_keyword_p=*/false,
9488                                                 /*check_dependency_p=*/true,
9489                                                 /*type_p=*/false,
9490                                                 /*is_declaration=*/false)
9491            != NULL_TREE);
9492       /* If we have seen a nested-name-specifier, and the next token
9493          is `template', then we are using the template-id production.  */
9494       if (parser->scope
9495           && cp_parser_optional_template_keyword (parser))
9496         {
9497           /* Look for the template-id.  */
9498           type = cp_parser_template_id (parser,
9499                                         /*template_keyword_p=*/true,
9500                                         /*check_dependency_p=*/true,
9501                                         /*is_declaration=*/false);
9502           /* If the template-id did not name a type, we are out of
9503              luck.  */
9504           if (TREE_CODE (type) != TYPE_DECL)
9505             {
9506               cp_parser_error (parser, "expected template-id for type");
9507               type = NULL_TREE;
9508             }
9509         }
9510       /* Otherwise, look for a type-name.  */
9511       else
9512         type = cp_parser_type_name (parser);
9513       /* Keep track of all name-lookups performed in class scopes.  */
9514       if (type
9515           && !global_p
9516           && !qualified_p
9517           && TREE_CODE (type) == TYPE_DECL
9518           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9519         maybe_note_name_used_in_class (DECL_NAME (type), type);
9520       /* If it didn't work out, we don't have a TYPE.  */
9521       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9522           && !cp_parser_parse_definitely (parser))
9523         type = NULL_TREE;
9524       if (type && decl_specs)
9525         cp_parser_set_decl_spec_type (decl_specs, type,
9526                                       /*user_defined=*/true);
9527     }
9528
9529   /* If we didn't get a type-name, issue an error message.  */
9530   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9531     {
9532       cp_parser_error (parser, "expected type-name");
9533       return error_mark_node;
9534     }
9535
9536   /* There is no valid C++ program where a non-template type is
9537      followed by a "<".  That usually indicates that the user thought
9538      that the type was a template.  */
9539   if (type && type != error_mark_node)
9540     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9541
9542   return type;
9543 }
9544
9545 /* Parse a type-name.
9546
9547    type-name:
9548      class-name
9549      enum-name
9550      typedef-name
9551
9552    enum-name:
9553      identifier
9554
9555    typedef-name:
9556      identifier
9557
9558    Returns a TYPE_DECL for the the type.  */
9559
9560 static tree
9561 cp_parser_type_name (cp_parser* parser)
9562 {
9563   tree type_decl;
9564   tree identifier;
9565
9566   /* We can't know yet whether it is a class-name or not.  */
9567   cp_parser_parse_tentatively (parser);
9568   /* Try a class-name.  */
9569   type_decl = cp_parser_class_name (parser,
9570                                     /*typename_keyword_p=*/false,
9571                                     /*template_keyword_p=*/false,
9572                                     /*type_p=*/false,
9573                                     /*check_dependency_p=*/true,
9574                                     /*class_head_p=*/false,
9575                                     /*is_declaration=*/false);
9576   /* If it's not a class-name, keep looking.  */
9577   if (!cp_parser_parse_definitely (parser))
9578     {
9579       /* It must be a typedef-name or an enum-name.  */
9580       identifier = cp_parser_identifier (parser);
9581       if (identifier == error_mark_node)
9582         return error_mark_node;
9583
9584       /* Look up the type-name.  */
9585       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9586       /* Issue an error if we did not find a type-name.  */
9587       if (TREE_CODE (type_decl) != TYPE_DECL)
9588         {
9589           if (!cp_parser_simulate_error (parser))
9590             cp_parser_name_lookup_error (parser, identifier, type_decl,
9591                                          "is not a type");
9592           type_decl = error_mark_node;
9593         }
9594       /* Remember that the name was used in the definition of the
9595          current class so that we can check later to see if the
9596          meaning would have been different after the class was
9597          entirely defined.  */
9598       else if (type_decl != error_mark_node
9599                && !parser->scope)
9600         maybe_note_name_used_in_class (identifier, type_decl);
9601     }
9602
9603   return type_decl;
9604 }
9605
9606
9607 /* Parse an elaborated-type-specifier.  Note that the grammar given
9608    here incorporates the resolution to DR68.
9609
9610    elaborated-type-specifier:
9611      class-key :: [opt] nested-name-specifier [opt] identifier
9612      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9613      enum :: [opt] nested-name-specifier [opt] identifier
9614      typename :: [opt] nested-name-specifier identifier
9615      typename :: [opt] nested-name-specifier template [opt]
9616        template-id
9617
9618    GNU extension:
9619
9620    elaborated-type-specifier:
9621      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9622      class-key attributes :: [opt] nested-name-specifier [opt]
9623                template [opt] template-id
9624      enum attributes :: [opt] nested-name-specifier [opt] identifier
9625
9626    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9627    declared `friend'.  If IS_DECLARATION is TRUE, then this
9628    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9629    something is being declared.
9630
9631    Returns the TYPE specified.  */
9632
9633 static tree
9634 cp_parser_elaborated_type_specifier (cp_parser* parser,
9635                                      bool is_friend,
9636                                      bool is_declaration)
9637 {
9638   enum tag_types tag_type;
9639   tree identifier;
9640   tree type = NULL_TREE;
9641   tree attributes = NULL_TREE;
9642
9643   /* See if we're looking at the `enum' keyword.  */
9644   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9645     {
9646       /* Consume the `enum' token.  */
9647       cp_lexer_consume_token (parser->lexer);
9648       /* Remember that it's an enumeration type.  */
9649       tag_type = enum_type;
9650       /* Parse the attributes.  */
9651       attributes = cp_parser_attributes_opt (parser);
9652     }
9653   /* Or, it might be `typename'.  */
9654   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9655                                            RID_TYPENAME))
9656     {
9657       /* Consume the `typename' token.  */
9658       cp_lexer_consume_token (parser->lexer);
9659       /* Remember that it's a `typename' type.  */
9660       tag_type = typename_type;
9661       /* The `typename' keyword is only allowed in templates.  */
9662       if (!processing_template_decl)
9663         pedwarn ("using %<typename%> outside of template");
9664     }
9665   /* Otherwise it must be a class-key.  */
9666   else
9667     {
9668       tag_type = cp_parser_class_key (parser);
9669       if (tag_type == none_type)
9670         return error_mark_node;
9671       /* Parse the attributes.  */
9672       attributes = cp_parser_attributes_opt (parser);
9673     }
9674
9675   /* Look for the `::' operator.  */
9676   cp_parser_global_scope_opt (parser,
9677                               /*current_scope_valid_p=*/false);
9678   /* Look for the nested-name-specifier.  */
9679   if (tag_type == typename_type)
9680     {
9681       if (cp_parser_nested_name_specifier (parser,
9682                                            /*typename_keyword_p=*/true,
9683                                            /*check_dependency_p=*/true,
9684                                            /*type_p=*/true,
9685                                            is_declaration)
9686           == error_mark_node)
9687         return error_mark_node;
9688     }
9689   else
9690     /* Even though `typename' is not present, the proposed resolution
9691        to Core Issue 180 says that in `class A<T>::B', `B' should be
9692        considered a type-name, even if `A<T>' is dependent.  */
9693     cp_parser_nested_name_specifier_opt (parser,
9694                                          /*typename_keyword_p=*/true,
9695                                          /*check_dependency_p=*/true,
9696                                          /*type_p=*/true,
9697                                          is_declaration);
9698   /* For everything but enumeration types, consider a template-id.  */
9699   if (tag_type != enum_type)
9700     {
9701       bool template_p = false;
9702       tree decl;
9703
9704       /* Allow the `template' keyword.  */
9705       template_p = cp_parser_optional_template_keyword (parser);
9706       /* If we didn't see `template', we don't know if there's a
9707          template-id or not.  */
9708       if (!template_p)
9709         cp_parser_parse_tentatively (parser);
9710       /* Parse the template-id.  */
9711       decl = cp_parser_template_id (parser, template_p,
9712                                     /*check_dependency_p=*/true,
9713                                     is_declaration);
9714       /* If we didn't find a template-id, look for an ordinary
9715          identifier.  */
9716       if (!template_p && !cp_parser_parse_definitely (parser))
9717         ;
9718       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9719          in effect, then we must assume that, upon instantiation, the
9720          template will correspond to a class.  */
9721       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9722                && tag_type == typename_type)
9723         type = make_typename_type (parser->scope, decl,
9724                                    /*complain=*/1);
9725       else
9726         type = TREE_TYPE (decl);
9727     }
9728
9729   /* For an enumeration type, consider only a plain identifier.  */
9730   if (!type)
9731     {
9732       identifier = cp_parser_identifier (parser);
9733
9734       if (identifier == error_mark_node)
9735         {
9736           parser->scope = NULL_TREE;
9737           return error_mark_node;
9738         }
9739
9740       /* For a `typename', we needn't call xref_tag.  */
9741       if (tag_type == typename_type)
9742         return cp_parser_make_typename_type (parser, parser->scope,
9743                                              identifier);
9744       /* Look up a qualified name in the usual way.  */
9745       if (parser->scope)
9746         {
9747           tree decl;
9748
9749           /* In an elaborated-type-specifier, names are assumed to name
9750              types, so we set IS_TYPE to TRUE when calling
9751              cp_parser_lookup_name.  */
9752           decl = cp_parser_lookup_name (parser, identifier,
9753                                         /*is_type=*/true,
9754                                         /*is_template=*/false,
9755                                         /*is_namespace=*/false,
9756                                         /*check_dependency=*/true,
9757                                         /*ambiguous_p=*/NULL);
9758
9759           /* If we are parsing friend declaration, DECL may be a
9760              TEMPLATE_DECL tree node here.  However, we need to check
9761              whether this TEMPLATE_DECL results in valid code.  Consider
9762              the following example:
9763
9764                namespace N {
9765                  template <class T> class C {};
9766                }
9767                class X {
9768                  template <class T> friend class N::C; // #1, valid code
9769                };
9770                template <class T> class Y {
9771                  friend class N::C;                    // #2, invalid code
9772                };
9773
9774              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9775              name lookup of `N::C'.  We see that friend declaration must
9776              be template for the code to be valid.  Note that
9777              processing_template_decl does not work here since it is
9778              always 1 for the above two cases.  */
9779
9780           decl = (cp_parser_maybe_treat_template_as_class
9781                   (decl, /*tag_name_p=*/is_friend
9782                          && parser->num_template_parameter_lists));
9783
9784           if (TREE_CODE (decl) != TYPE_DECL)
9785             {
9786               error ("expected type-name");
9787               return error_mark_node;
9788             }
9789
9790           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9791             check_elaborated_type_specifier
9792               (tag_type, decl,
9793                (parser->num_template_parameter_lists
9794                 || DECL_SELF_REFERENCE_P (decl)));
9795
9796           type = TREE_TYPE (decl);
9797         }
9798       else
9799         {
9800           /* An elaborated-type-specifier sometimes introduces a new type and
9801              sometimes names an existing type.  Normally, the rule is that it
9802              introduces a new type only if there is not an existing type of
9803              the same name already in scope.  For example, given:
9804
9805                struct S {};
9806                void f() { struct S s; }
9807
9808              the `struct S' in the body of `f' is the same `struct S' as in
9809              the global scope; the existing definition is used.  However, if
9810              there were no global declaration, this would introduce a new
9811              local class named `S'.
9812
9813              An exception to this rule applies to the following code:
9814
9815                namespace N { struct S; }
9816
9817              Here, the elaborated-type-specifier names a new type
9818              unconditionally; even if there is already an `S' in the
9819              containing scope this declaration names a new type.
9820              This exception only applies if the elaborated-type-specifier
9821              forms the complete declaration:
9822
9823                [class.name]
9824
9825                A declaration consisting solely of `class-key identifier ;' is
9826                either a redeclaration of the name in the current scope or a
9827                forward declaration of the identifier as a class name.  It
9828                introduces the name into the current scope.
9829
9830              We are in this situation precisely when the next token is a `;'.
9831
9832              An exception to the exception is that a `friend' declaration does
9833              *not* name a new type; i.e., given:
9834
9835                struct S { friend struct T; };
9836
9837              `T' is not a new type in the scope of `S'.
9838
9839              Also, `new struct S' or `sizeof (struct S)' never results in the
9840              definition of a new type; a new type can only be declared in a
9841              declaration context.  */
9842
9843           /* Warn about attributes. They are ignored.  */
9844           if (attributes)
9845             warning ("type attributes are honored only at type definition");
9846
9847           type = xref_tag (tag_type, identifier,
9848                            (is_friend
9849                             || !is_declaration
9850                             || cp_lexer_next_token_is_not (parser->lexer,
9851                                                            CPP_SEMICOLON)),
9852                            parser->num_template_parameter_lists);
9853         }
9854     }
9855   if (tag_type != enum_type)
9856     cp_parser_check_class_key (tag_type, type);
9857
9858   /* A "<" cannot follow an elaborated type specifier.  If that
9859      happens, the user was probably trying to form a template-id.  */
9860   cp_parser_check_for_invalid_template_id (parser, type);
9861
9862   return type;
9863 }
9864
9865 /* Parse an enum-specifier.
9866
9867    enum-specifier:
9868      enum identifier [opt] { enumerator-list [opt] }
9869
9870    Returns an ENUM_TYPE representing the enumeration.  */
9871
9872 static tree
9873 cp_parser_enum_specifier (cp_parser* parser)
9874 {
9875   tree identifier;
9876   tree type;
9877
9878   /* Caller guarantees that the current token is 'enum', an identifier
9879      possibly follows, and the token after that is an opening brace.
9880      If we don't have an identifier, fabricate an anonymous name for
9881      the enumeration being defined.  */
9882   cp_lexer_consume_token (parser->lexer);
9883
9884   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9885     identifier = cp_parser_identifier (parser);
9886   else
9887     identifier = make_anon_name ();
9888
9889   /* Issue an error message if type-definitions are forbidden here.  */
9890   cp_parser_check_type_definition (parser);
9891
9892   /* Create the new type.  We do this before consuming the opening brace
9893      so the enum will be recorded as being on the line of its tag (or the
9894      'enum' keyword, if there is no tag).  */
9895   type = start_enum (identifier);
9896
9897   /* Consume the opening brace.  */
9898   cp_lexer_consume_token (parser->lexer);
9899
9900   /* If the next token is not '}', then there are some enumerators.  */
9901   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9902     cp_parser_enumerator_list (parser, type);
9903
9904   /* Consume the final '}'.  */
9905   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9906
9907   /* Finish up the enumeration.  */
9908   finish_enum (type);
9909
9910   return type;
9911 }
9912
9913 /* Parse an enumerator-list.  The enumerators all have the indicated
9914    TYPE.
9915
9916    enumerator-list:
9917      enumerator-definition
9918      enumerator-list , enumerator-definition  */
9919
9920 static void
9921 cp_parser_enumerator_list (cp_parser* parser, tree type)
9922 {
9923   while (true)
9924     {
9925       /* Parse an enumerator-definition.  */
9926       cp_parser_enumerator_definition (parser, type);
9927
9928       /* If the next token is not a ',', we've reached the end of
9929          the list.  */
9930       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9931         break;
9932       /* Otherwise, consume the `,' and keep going.  */
9933       cp_lexer_consume_token (parser->lexer);
9934       /* If the next token is a `}', there is a trailing comma.  */
9935       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9936         {
9937           if (pedantic && !in_system_header)
9938             pedwarn ("comma at end of enumerator list");
9939           break;
9940         }
9941     }
9942 }
9943
9944 /* Parse an enumerator-definition.  The enumerator has the indicated
9945    TYPE.
9946
9947    enumerator-definition:
9948      enumerator
9949      enumerator = constant-expression
9950
9951    enumerator:
9952      identifier  */
9953
9954 static void
9955 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9956 {
9957   tree identifier;
9958   tree value;
9959
9960   /* Look for the identifier.  */
9961   identifier = cp_parser_identifier (parser);
9962   if (identifier == error_mark_node)
9963     return;
9964
9965   /* If the next token is an '=', then there is an explicit value.  */
9966   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9967     {
9968       /* Consume the `=' token.  */
9969       cp_lexer_consume_token (parser->lexer);
9970       /* Parse the value.  */
9971       value = cp_parser_constant_expression (parser,
9972                                              /*allow_non_constant_p=*/false,
9973                                              NULL);
9974     }
9975   else
9976     value = NULL_TREE;
9977
9978   /* Create the enumerator.  */
9979   build_enumerator (identifier, value, type);
9980 }
9981
9982 /* Parse a namespace-name.
9983
9984    namespace-name:
9985      original-namespace-name
9986      namespace-alias
9987
9988    Returns the NAMESPACE_DECL for the namespace.  */
9989
9990 static tree
9991 cp_parser_namespace_name (cp_parser* parser)
9992 {
9993   tree identifier;
9994   tree namespace_decl;
9995
9996   /* Get the name of the namespace.  */
9997   identifier = cp_parser_identifier (parser);
9998   if (identifier == error_mark_node)
9999     return error_mark_node;
10000
10001   /* Look up the identifier in the currently active scope.  Look only
10002      for namespaces, due to:
10003
10004        [basic.lookup.udir]
10005
10006        When looking up a namespace-name in a using-directive or alias
10007        definition, only namespace names are considered.
10008
10009      And:
10010
10011        [basic.lookup.qual]
10012
10013        During the lookup of a name preceding the :: scope resolution
10014        operator, object, function, and enumerator names are ignored.
10015
10016      (Note that cp_parser_class_or_namespace_name only calls this
10017      function if the token after the name is the scope resolution
10018      operator.)  */
10019   namespace_decl = cp_parser_lookup_name (parser, identifier,
10020                                           /*is_type=*/false,
10021                                           /*is_template=*/false,
10022                                           /*is_namespace=*/true,
10023                                           /*check_dependency=*/true,
10024                                           /*ambiguous_p=*/NULL);
10025   /* If it's not a namespace, issue an error.  */
10026   if (namespace_decl == error_mark_node
10027       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10028     {
10029       cp_parser_error (parser, "expected namespace-name");
10030       namespace_decl = error_mark_node;
10031     }
10032
10033   return namespace_decl;
10034 }
10035
10036 /* Parse a namespace-definition.
10037
10038    namespace-definition:
10039      named-namespace-definition
10040      unnamed-namespace-definition
10041
10042    named-namespace-definition:
10043      original-namespace-definition
10044      extension-namespace-definition
10045
10046    original-namespace-definition:
10047      namespace identifier { namespace-body }
10048
10049    extension-namespace-definition:
10050      namespace original-namespace-name { namespace-body }
10051
10052    unnamed-namespace-definition:
10053      namespace { namespace-body } */
10054
10055 static void
10056 cp_parser_namespace_definition (cp_parser* parser)
10057 {
10058   tree identifier;
10059
10060   /* Look for the `namespace' keyword.  */
10061   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10062
10063   /* Get the name of the namespace.  We do not attempt to distinguish
10064      between an original-namespace-definition and an
10065      extension-namespace-definition at this point.  The semantic
10066      analysis routines are responsible for that.  */
10067   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10068     identifier = cp_parser_identifier (parser);
10069   else
10070     identifier = NULL_TREE;
10071
10072   /* Look for the `{' to start the namespace.  */
10073   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10074   /* Start the namespace.  */
10075   push_namespace (identifier);
10076   /* Parse the body of the namespace.  */
10077   cp_parser_namespace_body (parser);
10078   /* Finish the namespace.  */
10079   pop_namespace ();
10080   /* Look for the final `}'.  */
10081   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10082 }
10083
10084 /* Parse a namespace-body.
10085
10086    namespace-body:
10087      declaration-seq [opt]  */
10088
10089 static void
10090 cp_parser_namespace_body (cp_parser* parser)
10091 {
10092   cp_parser_declaration_seq_opt (parser);
10093 }
10094
10095 /* Parse a namespace-alias-definition.
10096
10097    namespace-alias-definition:
10098      namespace identifier = qualified-namespace-specifier ;  */
10099
10100 static void
10101 cp_parser_namespace_alias_definition (cp_parser* parser)
10102 {
10103   tree identifier;
10104   tree namespace_specifier;
10105
10106   /* Look for the `namespace' keyword.  */
10107   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10108   /* Look for the identifier.  */
10109   identifier = cp_parser_identifier (parser);
10110   if (identifier == error_mark_node)
10111     return;
10112   /* Look for the `=' token.  */
10113   cp_parser_require (parser, CPP_EQ, "`='");
10114   /* Look for the qualified-namespace-specifier.  */
10115   namespace_specifier
10116     = cp_parser_qualified_namespace_specifier (parser);
10117   /* Look for the `;' token.  */
10118   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10119
10120   /* Register the alias in the symbol table.  */
10121   do_namespace_alias (identifier, namespace_specifier);
10122 }
10123
10124 /* Parse a qualified-namespace-specifier.
10125
10126    qualified-namespace-specifier:
10127      :: [opt] nested-name-specifier [opt] namespace-name
10128
10129    Returns a NAMESPACE_DECL corresponding to the specified
10130    namespace.  */
10131
10132 static tree
10133 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10134 {
10135   /* Look for the optional `::'.  */
10136   cp_parser_global_scope_opt (parser,
10137                               /*current_scope_valid_p=*/false);
10138
10139   /* Look for the optional nested-name-specifier.  */
10140   cp_parser_nested_name_specifier_opt (parser,
10141                                        /*typename_keyword_p=*/false,
10142                                        /*check_dependency_p=*/true,
10143                                        /*type_p=*/false,
10144                                        /*is_declaration=*/true);
10145
10146   return cp_parser_namespace_name (parser);
10147 }
10148
10149 /* Parse a using-declaration.
10150
10151    using-declaration:
10152      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10153      using :: unqualified-id ;  */
10154
10155 static void
10156 cp_parser_using_declaration (cp_parser* parser)
10157 {
10158   cp_token *token;
10159   bool typename_p = false;
10160   bool global_scope_p;
10161   tree decl;
10162   tree identifier;
10163   tree scope;
10164   tree qscope;
10165
10166   /* Look for the `using' keyword.  */
10167   cp_parser_require_keyword (parser, RID_USING, "`using'");
10168
10169   /* Peek at the next token.  */
10170   token = cp_lexer_peek_token (parser->lexer);
10171   /* See if it's `typename'.  */
10172   if (token->keyword == RID_TYPENAME)
10173     {
10174       /* Remember that we've seen it.  */
10175       typename_p = true;
10176       /* Consume the `typename' token.  */
10177       cp_lexer_consume_token (parser->lexer);
10178     }
10179
10180   /* Look for the optional global scope qualification.  */
10181   global_scope_p
10182     = (cp_parser_global_scope_opt (parser,
10183                                    /*current_scope_valid_p=*/false)
10184        != NULL_TREE);
10185
10186   /* If we saw `typename', or didn't see `::', then there must be a
10187      nested-name-specifier present.  */
10188   if (typename_p || !global_scope_p)
10189     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10190                                               /*check_dependency_p=*/true,
10191                                               /*type_p=*/false,
10192                                               /*is_declaration=*/true);
10193   /* Otherwise, we could be in either of the two productions.  In that
10194      case, treat the nested-name-specifier as optional.  */
10195   else
10196     qscope = cp_parser_nested_name_specifier_opt (parser,
10197                                                   /*typename_keyword_p=*/false,
10198                                                   /*check_dependency_p=*/true,
10199                                                   /*type_p=*/false,
10200                                                   /*is_declaration=*/true);
10201   if (!qscope)
10202     qscope = global_namespace;
10203
10204   /* Parse the unqualified-id.  */
10205   identifier = cp_parser_unqualified_id (parser,
10206                                          /*template_keyword_p=*/false,
10207                                          /*check_dependency_p=*/true,
10208                                          /*declarator_p=*/true);
10209
10210   /* The function we call to handle a using-declaration is different
10211      depending on what scope we are in.  */
10212   if (identifier == error_mark_node)
10213     ;
10214   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10215            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10216     /* [namespace.udecl]
10217
10218        A using declaration shall not name a template-id.  */
10219     error ("a template-id may not appear in a using-declaration");
10220   else
10221     {
10222       scope = current_scope ();
10223       if (scope && TYPE_P (scope))
10224         {
10225           /* Create the USING_DECL.  */
10226           decl = do_class_using_decl (build_nt (SCOPE_REF,
10227                                                 parser->scope,
10228                                                 identifier));
10229           /* Add it to the list of members in this class.  */
10230           finish_member_declaration (decl);
10231         }
10232       else
10233         {
10234           decl = cp_parser_lookup_name_simple (parser, identifier);
10235           if (decl == error_mark_node)
10236             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10237           else if (scope)
10238             do_local_using_decl (decl, qscope, identifier);
10239           else
10240             do_toplevel_using_decl (decl, qscope, identifier);
10241         }
10242     }
10243
10244   /* Look for the final `;'.  */
10245   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10246 }
10247
10248 /* Parse a using-directive.
10249
10250    using-directive:
10251      using namespace :: [opt] nested-name-specifier [opt]
10252        namespace-name ;  */
10253
10254 static void
10255 cp_parser_using_directive (cp_parser* parser)
10256 {
10257   tree namespace_decl;
10258   tree attribs;
10259
10260   /* Look for the `using' keyword.  */
10261   cp_parser_require_keyword (parser, RID_USING, "`using'");
10262   /* And the `namespace' keyword.  */
10263   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10264   /* Look for the optional `::' operator.  */
10265   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10266   /* And the optional nested-name-specifier.  */
10267   cp_parser_nested_name_specifier_opt (parser,
10268                                        /*typename_keyword_p=*/false,
10269                                        /*check_dependency_p=*/true,
10270                                        /*type_p=*/false,
10271                                        /*is_declaration=*/true);
10272   /* Get the namespace being used.  */
10273   namespace_decl = cp_parser_namespace_name (parser);
10274   /* And any specified attributes.  */
10275   attribs = cp_parser_attributes_opt (parser);
10276   /* Update the symbol table.  */
10277   parse_using_directive (namespace_decl, attribs);
10278   /* Look for the final `;'.  */
10279   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10280 }
10281
10282 /* Parse an asm-definition.
10283
10284    asm-definition:
10285      asm ( string-literal ) ;
10286
10287    GNU Extension:
10288
10289    asm-definition:
10290      asm volatile [opt] ( string-literal ) ;
10291      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10292      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10293                           : asm-operand-list [opt] ) ;
10294      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10295                           : asm-operand-list [opt]
10296                           : asm-operand-list [opt] ) ;  */
10297
10298 static void
10299 cp_parser_asm_definition (cp_parser* parser)
10300 {
10301   tree string;
10302   tree outputs = NULL_TREE;
10303   tree inputs = NULL_TREE;
10304   tree clobbers = NULL_TREE;
10305   tree asm_stmt;
10306   bool volatile_p = false;
10307   bool extended_p = false;
10308
10309   /* Look for the `asm' keyword.  */
10310   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10311   /* See if the next token is `volatile'.  */
10312   if (cp_parser_allow_gnu_extensions_p (parser)
10313       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10314     {
10315       /* Remember that we saw the `volatile' keyword.  */
10316       volatile_p = true;
10317       /* Consume the token.  */
10318       cp_lexer_consume_token (parser->lexer);
10319     }
10320   /* Look for the opening `('.  */
10321   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10322     return;
10323   /* Look for the string.  */
10324   string = cp_parser_string_literal (parser, false, false);
10325   if (string == error_mark_node)
10326     {
10327       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10328                                              /*consume_paren=*/true);
10329       return;
10330     }
10331
10332   /* If we're allowing GNU extensions, check for the extended assembly
10333      syntax.  Unfortunately, the `:' tokens need not be separated by
10334      a space in C, and so, for compatibility, we tolerate that here
10335      too.  Doing that means that we have to treat the `::' operator as
10336      two `:' tokens.  */
10337   if (cp_parser_allow_gnu_extensions_p (parser)
10338       && at_function_scope_p ()
10339       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10340           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10341     {
10342       bool inputs_p = false;
10343       bool clobbers_p = false;
10344
10345       /* The extended syntax was used.  */
10346       extended_p = true;
10347
10348       /* Look for outputs.  */
10349       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10350         {
10351           /* Consume the `:'.  */
10352           cp_lexer_consume_token (parser->lexer);
10353           /* Parse the output-operands.  */
10354           if (cp_lexer_next_token_is_not (parser->lexer,
10355                                           CPP_COLON)
10356               && cp_lexer_next_token_is_not (parser->lexer,
10357                                              CPP_SCOPE)
10358               && cp_lexer_next_token_is_not (parser->lexer,
10359                                              CPP_CLOSE_PAREN))
10360             outputs = cp_parser_asm_operand_list (parser);
10361         }
10362       /* If the next token is `::', there are no outputs, and the
10363          next token is the beginning of the inputs.  */
10364       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10365         /* The inputs are coming next.  */
10366         inputs_p = true;
10367
10368       /* Look for inputs.  */
10369       if (inputs_p
10370           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10371         {
10372           /* Consume the `:' or `::'.  */
10373           cp_lexer_consume_token (parser->lexer);
10374           /* Parse the output-operands.  */
10375           if (cp_lexer_next_token_is_not (parser->lexer,
10376                                           CPP_COLON)
10377               && cp_lexer_next_token_is_not (parser->lexer,
10378                                              CPP_CLOSE_PAREN))
10379             inputs = cp_parser_asm_operand_list (parser);
10380         }
10381       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10382         /* The clobbers are coming next.  */
10383         clobbers_p = true;
10384
10385       /* Look for clobbers.  */
10386       if (clobbers_p
10387           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10388         {
10389           /* Consume the `:' or `::'.  */
10390           cp_lexer_consume_token (parser->lexer);
10391           /* Parse the clobbers.  */
10392           if (cp_lexer_next_token_is_not (parser->lexer,
10393                                           CPP_CLOSE_PAREN))
10394             clobbers = cp_parser_asm_clobber_list (parser);
10395         }
10396     }
10397   /* Look for the closing `)'.  */
10398   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10399     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10400                                            /*consume_paren=*/true);
10401   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10402
10403   /* Create the ASM_EXPR.  */
10404   if (at_function_scope_p ())
10405     {
10406       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10407                                   inputs, clobbers);
10408       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10409       if (!extended_p)
10410         {
10411           tree temp = asm_stmt;
10412           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10413             temp = TREE_OPERAND (temp, 0);
10414           
10415           ASM_INPUT_P (temp) = 1;
10416         }
10417     }
10418   else
10419     assemble_asm (string);
10420 }
10421
10422 /* Declarators [gram.dcl.decl] */
10423
10424 /* Parse an init-declarator.
10425
10426    init-declarator:
10427      declarator initializer [opt]
10428
10429    GNU Extension:
10430
10431    init-declarator:
10432      declarator asm-specification [opt] attributes [opt] initializer [opt]
10433
10434    function-definition:
10435      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10436        function-body
10437      decl-specifier-seq [opt] declarator function-try-block
10438
10439    GNU Extension:
10440
10441    function-definition:
10442      __extension__ function-definition
10443
10444    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10445    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10446    then this declarator appears in a class scope.  The new DECL created
10447    by this declarator is returned.
10448
10449    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10450    for a function-definition here as well.  If the declarator is a
10451    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10452    be TRUE upon return.  By that point, the function-definition will
10453    have been completely parsed.
10454
10455    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10456    is FALSE.  */
10457
10458 static tree
10459 cp_parser_init_declarator (cp_parser* parser,
10460                            cp_decl_specifier_seq *decl_specifiers,
10461                            bool function_definition_allowed_p,
10462                            bool member_p,
10463                            int declares_class_or_enum,
10464                            bool* function_definition_p)
10465 {
10466   cp_token *token;
10467   cp_declarator *declarator;
10468   tree prefix_attributes;
10469   tree attributes;
10470   tree asm_specification;
10471   tree initializer;
10472   tree decl = NULL_TREE;
10473   tree scope;
10474   bool is_initialized;
10475   bool is_parenthesized_init;
10476   bool is_non_constant_init;
10477   int ctor_dtor_or_conv_p;
10478   bool friend_p;
10479   bool pop_p = false;
10480
10481   /* Gather the attributes that were provided with the
10482      decl-specifiers.  */
10483   prefix_attributes = decl_specifiers->attributes;
10484
10485   /* Assume that this is not the declarator for a function
10486      definition.  */
10487   if (function_definition_p)
10488     *function_definition_p = false;
10489
10490   /* Defer access checks while parsing the declarator; we cannot know
10491      what names are accessible until we know what is being
10492      declared.  */
10493   resume_deferring_access_checks ();
10494
10495   /* Parse the declarator.  */
10496   declarator
10497     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10498                             &ctor_dtor_or_conv_p,
10499                             /*parenthesized_p=*/NULL,
10500                             /*member_p=*/false);
10501   /* Gather up the deferred checks.  */
10502   stop_deferring_access_checks ();
10503
10504   /* If the DECLARATOR was erroneous, there's no need to go
10505      further.  */
10506   if (declarator == cp_error_declarator)
10507     return error_mark_node;
10508
10509   cp_parser_check_for_definition_in_return_type (declarator,
10510                                                  declares_class_or_enum);
10511
10512   /* Figure out what scope the entity declared by the DECLARATOR is
10513      located in.  `grokdeclarator' sometimes changes the scope, so
10514      we compute it now.  */
10515   scope = get_scope_of_declarator (declarator);
10516
10517   /* If we're allowing GNU extensions, look for an asm-specification
10518      and attributes.  */
10519   if (cp_parser_allow_gnu_extensions_p (parser))
10520     {
10521       /* Look for an asm-specification.  */
10522       asm_specification = cp_parser_asm_specification_opt (parser);
10523       /* And attributes.  */
10524       attributes = cp_parser_attributes_opt (parser);
10525     }
10526   else
10527     {
10528       asm_specification = NULL_TREE;
10529       attributes = NULL_TREE;
10530     }
10531
10532   /* Peek at the next token.  */
10533   token = cp_lexer_peek_token (parser->lexer);
10534   /* Check to see if the token indicates the start of a
10535      function-definition.  */
10536   if (cp_parser_token_starts_function_definition_p (token))
10537     {
10538       if (!function_definition_allowed_p)
10539         {
10540           /* If a function-definition should not appear here, issue an
10541              error message.  */
10542           cp_parser_error (parser,
10543                            "a function-definition is not allowed here");
10544           return error_mark_node;
10545         }
10546       else
10547         {
10548           /* Neither attributes nor an asm-specification are allowed
10549              on a function-definition.  */
10550           if (asm_specification)
10551             error ("an asm-specification is not allowed on a function-definition");
10552           if (attributes)
10553             error ("attributes are not allowed on a function-definition");
10554           /* This is a function-definition.  */
10555           *function_definition_p = true;
10556
10557           /* Parse the function definition.  */
10558           if (member_p)
10559             decl = cp_parser_save_member_function_body (parser,
10560                                                         decl_specifiers,
10561                                                         declarator,
10562                                                         prefix_attributes);
10563           else
10564             decl
10565               = (cp_parser_function_definition_from_specifiers_and_declarator
10566                  (parser, decl_specifiers, prefix_attributes, declarator));
10567
10568           return decl;
10569         }
10570     }
10571
10572   /* [dcl.dcl]
10573
10574      Only in function declarations for constructors, destructors, and
10575      type conversions can the decl-specifier-seq be omitted.
10576
10577      We explicitly postpone this check past the point where we handle
10578      function-definitions because we tolerate function-definitions
10579      that are missing their return types in some modes.  */
10580   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10581     {
10582       cp_parser_error (parser,
10583                        "expected constructor, destructor, or type conversion");
10584       return error_mark_node;
10585     }
10586
10587   /* An `=' or an `(' indicates an initializer.  */
10588   is_initialized = (token->type == CPP_EQ
10589                      || token->type == CPP_OPEN_PAREN);
10590   /* If the init-declarator isn't initialized and isn't followed by a
10591      `,' or `;', it's not a valid init-declarator.  */
10592   if (!is_initialized
10593       && token->type != CPP_COMMA
10594       && token->type != CPP_SEMICOLON)
10595     {
10596       cp_parser_error (parser, "expected initializer");
10597       return error_mark_node;
10598     }
10599
10600   /* Because start_decl has side-effects, we should only call it if we
10601      know we're going ahead.  By this point, we know that we cannot
10602      possibly be looking at any other construct.  */
10603   cp_parser_commit_to_tentative_parse (parser);
10604
10605   /* If the decl specifiers were bad, issue an error now that we're
10606      sure this was intended to be a declarator.  Then continue
10607      declaring the variable(s), as int, to try to cut down on further
10608      errors.  */
10609   if (decl_specifiers->any_specifiers_p
10610       && decl_specifiers->type == error_mark_node)
10611     {
10612       cp_parser_error (parser, "invalid type in declaration");
10613       decl_specifiers->type = integer_type_node;
10614     }
10615
10616   /* Check to see whether or not this declaration is a friend.  */
10617   friend_p = cp_parser_friend_p (decl_specifiers);
10618
10619   /* Check that the number of template-parameter-lists is OK.  */
10620   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10621     return error_mark_node;
10622
10623   /* Enter the newly declared entry in the symbol table.  If we're
10624      processing a declaration in a class-specifier, we wait until
10625      after processing the initializer.  */
10626   if (!member_p)
10627     {
10628       if (parser->in_unbraced_linkage_specification_p)
10629         {
10630           decl_specifiers->storage_class = sc_extern;
10631           have_extern_spec = false;
10632         }
10633       decl = start_decl (declarator, decl_specifiers,
10634                          is_initialized, attributes, prefix_attributes,
10635                          &pop_p);
10636     }
10637   else if (scope)
10638     /* Enter the SCOPE.  That way unqualified names appearing in the
10639        initializer will be looked up in SCOPE.  */
10640     pop_p = push_scope (scope);
10641
10642   /* Perform deferred access control checks, now that we know in which
10643      SCOPE the declared entity resides.  */
10644   if (!member_p && decl)
10645     {
10646       tree saved_current_function_decl = NULL_TREE;
10647
10648       /* If the entity being declared is a function, pretend that we
10649          are in its scope.  If it is a `friend', it may have access to
10650          things that would not otherwise be accessible.  */
10651       if (TREE_CODE (decl) == FUNCTION_DECL)
10652         {
10653           saved_current_function_decl = current_function_decl;
10654           current_function_decl = decl;
10655         }
10656
10657       /* Perform the access control checks for the declarator and the
10658          the decl-specifiers.  */
10659       perform_deferred_access_checks ();
10660
10661       /* Restore the saved value.  */
10662       if (TREE_CODE (decl) == FUNCTION_DECL)
10663         current_function_decl = saved_current_function_decl;
10664     }
10665
10666   /* Parse the initializer.  */
10667   if (is_initialized)
10668     initializer = cp_parser_initializer (parser,
10669                                          &is_parenthesized_init,
10670                                          &is_non_constant_init);
10671   else
10672     {
10673       initializer = NULL_TREE;
10674       is_parenthesized_init = false;
10675       is_non_constant_init = true;
10676     }
10677
10678   /* The old parser allows attributes to appear after a parenthesized
10679      initializer.  Mark Mitchell proposed removing this functionality
10680      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10681      attributes -- but ignores them.  */
10682   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10683     if (cp_parser_attributes_opt (parser))
10684       warning ("attributes after parenthesized initializer ignored");
10685
10686   /* For an in-class declaration, use `grokfield' to create the
10687      declaration.  */
10688   if (member_p)
10689     {
10690       if (pop_p)
10691         pop_scope (scope);
10692       decl = grokfield (declarator, decl_specifiers,
10693                         initializer, /*asmspec=*/NULL_TREE,
10694                         /*attributes=*/NULL_TREE);
10695       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10696         cp_parser_save_default_args (parser, decl);
10697     }
10698
10699   /* Finish processing the declaration.  But, skip friend
10700      declarations.  */
10701   if (!friend_p && decl && decl != error_mark_node)
10702     {
10703       cp_finish_decl (decl,
10704                       initializer,
10705                       asm_specification,
10706                       /* If the initializer is in parentheses, then this is
10707                          a direct-initialization, which means that an
10708                          `explicit' constructor is OK.  Otherwise, an
10709                          `explicit' constructor cannot be used.  */
10710                       ((is_parenthesized_init || !is_initialized)
10711                      ? 0 : LOOKUP_ONLYCONVERTING));
10712       if (pop_p)
10713         pop_scope (DECL_CONTEXT (decl));
10714     }
10715
10716   /* Remember whether or not variables were initialized by
10717      constant-expressions.  */
10718   if (decl && TREE_CODE (decl) == VAR_DECL
10719       && is_initialized && !is_non_constant_init)
10720     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10721
10722   return decl;
10723 }
10724
10725 /* Parse a declarator.
10726
10727    declarator:
10728      direct-declarator
10729      ptr-operator declarator
10730
10731    abstract-declarator:
10732      ptr-operator abstract-declarator [opt]
10733      direct-abstract-declarator
10734
10735    GNU Extensions:
10736
10737    declarator:
10738      attributes [opt] direct-declarator
10739      attributes [opt] ptr-operator declarator
10740
10741    abstract-declarator:
10742      attributes [opt] ptr-operator abstract-declarator [opt]
10743      attributes [opt] direct-abstract-declarator
10744
10745    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10746    detect constructor, destructor or conversion operators. It is set
10747    to -1 if the declarator is a name, and +1 if it is a
10748    function. Otherwise it is set to zero. Usually you just want to
10749    test for >0, but internally the negative value is used.
10750
10751    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10752    a decl-specifier-seq unless it declares a constructor, destructor,
10753    or conversion.  It might seem that we could check this condition in
10754    semantic analysis, rather than parsing, but that makes it difficult
10755    to handle something like `f()'.  We want to notice that there are
10756    no decl-specifiers, and therefore realize that this is an
10757    expression, not a declaration.)
10758
10759    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10760    the declarator is a direct-declarator of the form "(...)".  
10761
10762    MEMBER_P is true iff this declarator is a member-declarator.  */
10763
10764 static cp_declarator *
10765 cp_parser_declarator (cp_parser* parser,
10766                       cp_parser_declarator_kind dcl_kind,
10767                       int* ctor_dtor_or_conv_p,
10768                       bool* parenthesized_p,
10769                       bool member_p)
10770 {
10771   cp_token *token;
10772   cp_declarator *declarator;
10773   enum tree_code code;
10774   cp_cv_quals cv_quals;
10775   tree class_type;
10776   tree attributes = NULL_TREE;
10777
10778   /* Assume this is not a constructor, destructor, or type-conversion
10779      operator.  */
10780   if (ctor_dtor_or_conv_p)
10781     *ctor_dtor_or_conv_p = 0;
10782
10783   if (cp_parser_allow_gnu_extensions_p (parser))
10784     attributes = cp_parser_attributes_opt (parser);
10785
10786   /* Peek at the next token.  */
10787   token = cp_lexer_peek_token (parser->lexer);
10788
10789   /* Check for the ptr-operator production.  */
10790   cp_parser_parse_tentatively (parser);
10791   /* Parse the ptr-operator.  */
10792   code = cp_parser_ptr_operator (parser,
10793                                  &class_type,
10794                                  &cv_quals);
10795   /* If that worked, then we have a ptr-operator.  */
10796   if (cp_parser_parse_definitely (parser))
10797     {
10798       /* If a ptr-operator was found, then this declarator was not
10799          parenthesized.  */
10800       if (parenthesized_p)
10801         *parenthesized_p = true;
10802       /* The dependent declarator is optional if we are parsing an
10803          abstract-declarator.  */
10804       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10805         cp_parser_parse_tentatively (parser);
10806
10807       /* Parse the dependent declarator.  */
10808       declarator = cp_parser_declarator (parser, dcl_kind,
10809                                          /*ctor_dtor_or_conv_p=*/NULL,
10810                                          /*parenthesized_p=*/NULL,
10811                                          /*member_p=*/false);
10812
10813       /* If we are parsing an abstract-declarator, we must handle the
10814          case where the dependent declarator is absent.  */
10815       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10816           && !cp_parser_parse_definitely (parser))
10817         declarator = NULL;
10818
10819       /* Build the representation of the ptr-operator.  */
10820       if (class_type)
10821         declarator = make_ptrmem_declarator (cv_quals,
10822                                              class_type,
10823                                              declarator);
10824       else if (code == INDIRECT_REF)
10825         declarator = make_pointer_declarator (cv_quals, declarator);
10826       else
10827         declarator = make_reference_declarator (cv_quals, declarator);
10828     }
10829   /* Everything else is a direct-declarator.  */
10830   else
10831     {
10832       if (parenthesized_p)
10833         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10834                                                    CPP_OPEN_PAREN);
10835       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10836                                                 ctor_dtor_or_conv_p,
10837                                                 member_p);
10838     }
10839
10840   if (attributes && declarator != cp_error_declarator)
10841     declarator->attributes = attributes;
10842
10843   return declarator;
10844 }
10845
10846 /* Parse a direct-declarator or direct-abstract-declarator.
10847
10848    direct-declarator:
10849      declarator-id
10850      direct-declarator ( parameter-declaration-clause )
10851        cv-qualifier-seq [opt]
10852        exception-specification [opt]
10853      direct-declarator [ constant-expression [opt] ]
10854      ( declarator )
10855
10856    direct-abstract-declarator:
10857      direct-abstract-declarator [opt]
10858        ( parameter-declaration-clause )
10859        cv-qualifier-seq [opt]
10860        exception-specification [opt]
10861      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10862      ( abstract-declarator )
10863
10864    Returns a representation of the declarator.  DCL_KIND is
10865    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10866    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10867    we are parsing a direct-declarator.  It is
10868    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10869    of ambiguity we prefer an abstract declarator, as per
10870    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10871    cp_parser_declarator.  */
10872
10873 static cp_declarator *
10874 cp_parser_direct_declarator (cp_parser* parser,
10875                              cp_parser_declarator_kind dcl_kind,
10876                              int* ctor_dtor_or_conv_p,
10877                              bool member_p)
10878 {
10879   cp_token *token;
10880   cp_declarator *declarator = NULL;
10881   tree scope = NULL_TREE;
10882   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10883   bool saved_in_declarator_p = parser->in_declarator_p;
10884   bool first = true;
10885   bool pop_p = false;
10886
10887   while (true)
10888     {
10889       /* Peek at the next token.  */
10890       token = cp_lexer_peek_token (parser->lexer);
10891       if (token->type == CPP_OPEN_PAREN)
10892         {
10893           /* This is either a parameter-declaration-clause, or a
10894              parenthesized declarator. When we know we are parsing a
10895              named declarator, it must be a parenthesized declarator
10896              if FIRST is true. For instance, `(int)' is a
10897              parameter-declaration-clause, with an omitted
10898              direct-abstract-declarator. But `((*))', is a
10899              parenthesized abstract declarator. Finally, when T is a
10900              template parameter `(T)' is a
10901              parameter-declaration-clause, and not a parenthesized
10902              named declarator.
10903
10904              We first try and parse a parameter-declaration-clause,
10905              and then try a nested declarator (if FIRST is true).
10906
10907              It is not an error for it not to be a
10908              parameter-declaration-clause, even when FIRST is
10909              false. Consider,
10910
10911                int i (int);
10912                int i (3);
10913
10914              The first is the declaration of a function while the
10915              second is a the definition of a variable, including its
10916              initializer.
10917
10918              Having seen only the parenthesis, we cannot know which of
10919              these two alternatives should be selected.  Even more
10920              complex are examples like:
10921
10922                int i (int (a));
10923                int i (int (3));
10924
10925              The former is a function-declaration; the latter is a
10926              variable initialization.
10927
10928              Thus again, we try a parameter-declaration-clause, and if
10929              that fails, we back out and return.  */
10930
10931           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10932             {
10933               cp_parameter_declarator *params;
10934               unsigned saved_num_template_parameter_lists;
10935
10936               /* In a member-declarator, the only valid interpretation
10937                  of a parenthesis is the start of a
10938                  parameter-declaration-clause.  (It is invalid to
10939                  initialize a static data member with a parenthesized
10940                  initializer; only the "=" form of initialization is
10941                  permitted.)  */
10942               if (!member_p)
10943                 cp_parser_parse_tentatively (parser);
10944
10945               /* Consume the `('.  */
10946               cp_lexer_consume_token (parser->lexer);
10947               if (first)
10948                 {
10949                   /* If this is going to be an abstract declarator, we're
10950                      in a declarator and we can't have default args.  */
10951                   parser->default_arg_ok_p = false;
10952                   parser->in_declarator_p = true;
10953                 }
10954
10955               /* Inside the function parameter list, surrounding
10956                  template-parameter-lists do not apply.  */
10957               saved_num_template_parameter_lists
10958                 = parser->num_template_parameter_lists;
10959               parser->num_template_parameter_lists = 0;
10960
10961               /* Parse the parameter-declaration-clause.  */
10962               params = cp_parser_parameter_declaration_clause (parser);
10963
10964               parser->num_template_parameter_lists
10965                 = saved_num_template_parameter_lists;
10966
10967               /* If all went well, parse the cv-qualifier-seq and the
10968                  exception-specification.  */
10969               if (member_p || cp_parser_parse_definitely (parser))
10970                 {
10971                   cp_cv_quals cv_quals;
10972                   tree exception_specification;
10973
10974                   if (ctor_dtor_or_conv_p)
10975                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10976                   first = false;
10977                   /* Consume the `)'.  */
10978                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10979
10980                   /* Parse the cv-qualifier-seq.  */
10981                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10982                   /* And the exception-specification.  */
10983                   exception_specification
10984                     = cp_parser_exception_specification_opt (parser);
10985
10986                   /* Create the function-declarator.  */
10987                   declarator = make_call_declarator (declarator,
10988                                                      params,
10989                                                      cv_quals,
10990                                                      exception_specification);
10991                   /* Any subsequent parameter lists are to do with
10992                      return type, so are not those of the declared
10993                      function.  */
10994                   parser->default_arg_ok_p = false;
10995
10996                   /* Repeat the main loop.  */
10997                   continue;
10998                 }
10999             }
11000
11001           /* If this is the first, we can try a parenthesized
11002              declarator.  */
11003           if (first)
11004             {
11005               bool saved_in_type_id_in_expr_p;
11006
11007               parser->default_arg_ok_p = saved_default_arg_ok_p;
11008               parser->in_declarator_p = saved_in_declarator_p;
11009
11010               /* Consume the `('.  */
11011               cp_lexer_consume_token (parser->lexer);
11012               /* Parse the nested declarator.  */
11013               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11014               parser->in_type_id_in_expr_p = true;
11015               declarator
11016                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11017                                         /*parenthesized_p=*/NULL,
11018                                         member_p);
11019               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11020               first = false;
11021               /* Expect a `)'.  */
11022               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11023                 declarator = cp_error_declarator;
11024               if (declarator == cp_error_declarator)
11025                 break;
11026
11027               goto handle_declarator;
11028             }
11029           /* Otherwise, we must be done.  */
11030           else
11031             break;
11032         }
11033       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11034                && token->type == CPP_OPEN_SQUARE)
11035         {
11036           /* Parse an array-declarator.  */
11037           tree bounds;
11038
11039           if (ctor_dtor_or_conv_p)
11040             *ctor_dtor_or_conv_p = 0;
11041
11042           first = false;
11043           parser->default_arg_ok_p = false;
11044           parser->in_declarator_p = true;
11045           /* Consume the `['.  */
11046           cp_lexer_consume_token (parser->lexer);
11047           /* Peek at the next token.  */
11048           token = cp_lexer_peek_token (parser->lexer);
11049           /* If the next token is `]', then there is no
11050              constant-expression.  */
11051           if (token->type != CPP_CLOSE_SQUARE)
11052             {
11053               bool non_constant_p;
11054
11055               bounds
11056                 = cp_parser_constant_expression (parser,
11057                                                  /*allow_non_constant=*/true,
11058                                                  &non_constant_p);
11059               if (!non_constant_p)
11060                 bounds = fold_non_dependent_expr (bounds);
11061             }
11062           else
11063             bounds = NULL_TREE;
11064           /* Look for the closing `]'.  */
11065           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11066             {
11067               declarator = cp_error_declarator;
11068               break;
11069             }
11070
11071           declarator = make_array_declarator (declarator, bounds);
11072         }
11073       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11074         {
11075           tree id;
11076
11077           /* Parse a declarator-id */
11078           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11079             cp_parser_parse_tentatively (parser);
11080           id = cp_parser_declarator_id (parser);
11081           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11082             {
11083               if (!cp_parser_parse_definitely (parser))
11084                 id = error_mark_node;
11085               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11086                 {
11087                   cp_parser_error (parser, "expected unqualified-id");
11088                   id = error_mark_node;
11089                 }
11090             }
11091
11092           if (id == error_mark_node)
11093             {
11094               declarator = cp_error_declarator;
11095               break;
11096             }
11097
11098           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11099             {
11100               tree scope = TREE_OPERAND (id, 0);
11101
11102               /* In the declaration of a member of a template class
11103                  outside of the class itself, the SCOPE will sometimes
11104                  be a TYPENAME_TYPE.  For example, given:
11105
11106                  template <typename T>
11107                  int S<T>::R::i = 3;
11108
11109                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11110                  this context, we must resolve S<T>::R to an ordinary
11111                  type, rather than a typename type.
11112
11113                  The reason we normally avoid resolving TYPENAME_TYPEs
11114                  is that a specialization of `S' might render
11115                  `S<T>::R' not a type.  However, if `S' is
11116                  specialized, then this `i' will not be used, so there
11117                  is no harm in resolving the types here.  */
11118               if (TREE_CODE (scope) == TYPENAME_TYPE)
11119                 {
11120                   tree type;
11121
11122                   /* Resolve the TYPENAME_TYPE.  */
11123                   type = resolve_typename_type (scope,
11124                                                  /*only_current_p=*/false);
11125                   /* If that failed, the declarator is invalid.  */
11126                   if (type == error_mark_node)
11127                     error ("%<%T::%D%> is not a type",
11128                            TYPE_CONTEXT (scope),
11129                            TYPE_IDENTIFIER (scope));
11130                   /* Build a new DECLARATOR.  */
11131                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11132                 }
11133             }
11134
11135           declarator = make_id_declarator (id);
11136           if (id)
11137             {
11138               tree class_type;
11139               tree unqualified_name;
11140
11141               if (TREE_CODE (id) == SCOPE_REF
11142                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11143                 {
11144                   class_type = TREE_OPERAND (id, 0);
11145                   unqualified_name = TREE_OPERAND (id, 1);
11146                 }
11147               else
11148                 {
11149                   class_type = current_class_type;
11150                   unqualified_name = id;
11151                 }
11152
11153               if (class_type)
11154                 {
11155                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11156                     declarator->u.id.sfk = sfk_destructor;
11157                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11158                     declarator->u.id.sfk = sfk_conversion;
11159                   else if (constructor_name_p (unqualified_name,
11160                                                class_type)
11161                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11162                                && same_type_p (TREE_TYPE (unqualified_name),
11163                                                class_type)))
11164                     declarator->u.id.sfk = sfk_constructor;
11165
11166                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11167                     *ctor_dtor_or_conv_p = -1;
11168                   if (TREE_CODE (id) == SCOPE_REF
11169                       && TREE_CODE (unqualified_name) == TYPE_DECL
11170                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11171                     {
11172                       error ("invalid use of constructor as a template");
11173                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11174                               "the constructor in a qualified name",
11175                               class_type,
11176                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11177                               class_type, class_type);
11178                     }
11179                 }
11180             }
11181
11182         handle_declarator:;
11183           scope = get_scope_of_declarator (declarator);
11184           if (scope)
11185             /* Any names that appear after the declarator-id for a
11186                member are looked up in the containing scope.  */
11187             pop_p = push_scope (scope);
11188           parser->in_declarator_p = true;
11189           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11190               || (declarator && declarator->kind == cdk_id))
11191             /* Default args are only allowed on function
11192                declarations.  */
11193             parser->default_arg_ok_p = saved_default_arg_ok_p;
11194           else
11195             parser->default_arg_ok_p = false;
11196
11197           first = false;
11198         }
11199       /* We're done.  */
11200       else
11201         break;
11202     }
11203
11204   /* For an abstract declarator, we might wind up with nothing at this
11205      point.  That's an error; the declarator is not optional.  */
11206   if (!declarator)
11207     cp_parser_error (parser, "expected declarator");
11208
11209   /* If we entered a scope, we must exit it now.  */
11210   if (pop_p)
11211     pop_scope (scope);
11212
11213   parser->default_arg_ok_p = saved_default_arg_ok_p;
11214   parser->in_declarator_p = saved_in_declarator_p;
11215
11216   return declarator;
11217 }
11218
11219 /* Parse a ptr-operator.
11220
11221    ptr-operator:
11222      * cv-qualifier-seq [opt]
11223      &
11224      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11225
11226    GNU Extension:
11227
11228    ptr-operator:
11229      & cv-qualifier-seq [opt]
11230
11231    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11232    Returns ADDR_EXPR if a reference was used.  In the case of a
11233    pointer-to-member, *TYPE is filled in with the TYPE containing the
11234    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11235    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11236    ERROR_MARK if an error occurred.  */
11237
11238 static enum tree_code
11239 cp_parser_ptr_operator (cp_parser* parser,
11240                         tree* type,
11241                         cp_cv_quals *cv_quals)
11242 {
11243   enum tree_code code = ERROR_MARK;
11244   cp_token *token;
11245
11246   /* Assume that it's not a pointer-to-member.  */
11247   *type = NULL_TREE;
11248   /* And that there are no cv-qualifiers.  */
11249   *cv_quals = TYPE_UNQUALIFIED;
11250
11251   /* Peek at the next token.  */
11252   token = cp_lexer_peek_token (parser->lexer);
11253   /* If it's a `*' or `&' we have a pointer or reference.  */
11254   if (token->type == CPP_MULT || token->type == CPP_AND)
11255     {
11256       /* Remember which ptr-operator we were processing.  */
11257       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11258
11259       /* Consume the `*' or `&'.  */
11260       cp_lexer_consume_token (parser->lexer);
11261
11262       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11263          `&', if we are allowing GNU extensions.  (The only qualifier
11264          that can legally appear after `&' is `restrict', but that is
11265          enforced during semantic analysis.  */
11266       if (code == INDIRECT_REF
11267           || cp_parser_allow_gnu_extensions_p (parser))
11268         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11269     }
11270   else
11271     {
11272       /* Try the pointer-to-member case.  */
11273       cp_parser_parse_tentatively (parser);
11274       /* Look for the optional `::' operator.  */
11275       cp_parser_global_scope_opt (parser,
11276                                   /*current_scope_valid_p=*/false);
11277       /* Look for the nested-name specifier.  */
11278       cp_parser_nested_name_specifier (parser,
11279                                        /*typename_keyword_p=*/false,
11280                                        /*check_dependency_p=*/true,
11281                                        /*type_p=*/false,
11282                                        /*is_declaration=*/false);
11283       /* If we found it, and the next token is a `*', then we are
11284          indeed looking at a pointer-to-member operator.  */
11285       if (!cp_parser_error_occurred (parser)
11286           && cp_parser_require (parser, CPP_MULT, "`*'"))
11287         {
11288           /* The type of which the member is a member is given by the
11289              current SCOPE.  */
11290           *type = parser->scope;
11291           /* The next name will not be qualified.  */
11292           parser->scope = NULL_TREE;
11293           parser->qualifying_scope = NULL_TREE;
11294           parser->object_scope = NULL_TREE;
11295           /* Indicate that the `*' operator was used.  */
11296           code = INDIRECT_REF;
11297           /* Look for the optional cv-qualifier-seq.  */
11298           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11299         }
11300       /* If that didn't work we don't have a ptr-operator.  */
11301       if (!cp_parser_parse_definitely (parser))
11302         cp_parser_error (parser, "expected ptr-operator");
11303     }
11304
11305   return code;
11306 }
11307
11308 /* Parse an (optional) cv-qualifier-seq.
11309
11310    cv-qualifier-seq:
11311      cv-qualifier cv-qualifier-seq [opt]
11312
11313    cv-qualifier:
11314      const
11315      volatile
11316
11317    GNU Extension:
11318
11319    cv-qualifier:
11320      __restrict__
11321
11322    Returns a bitmask representing the cv-qualifiers.  */
11323
11324 static cp_cv_quals
11325 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11326 {
11327   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11328
11329   while (true)
11330     {
11331       cp_token *token;
11332       cp_cv_quals cv_qualifier;
11333
11334       /* Peek at the next token.  */
11335       token = cp_lexer_peek_token (parser->lexer);
11336       /* See if it's a cv-qualifier.  */
11337       switch (token->keyword)
11338         {
11339         case RID_CONST:
11340           cv_qualifier = TYPE_QUAL_CONST;
11341           break;
11342
11343         case RID_VOLATILE:
11344           cv_qualifier = TYPE_QUAL_VOLATILE;
11345           break;
11346
11347         case RID_RESTRICT:
11348           cv_qualifier = TYPE_QUAL_RESTRICT;
11349           break;
11350
11351         default:
11352           cv_qualifier = TYPE_UNQUALIFIED;
11353           break;
11354         }
11355
11356       if (!cv_qualifier)
11357         break;
11358
11359       if (cv_quals & cv_qualifier)
11360         {
11361           error ("duplicate cv-qualifier");
11362           cp_lexer_purge_token (parser->lexer);
11363         }
11364       else
11365         {
11366           cp_lexer_consume_token (parser->lexer);
11367           cv_quals |= cv_qualifier;
11368         }
11369     }
11370
11371   return cv_quals;
11372 }
11373
11374 /* Parse a declarator-id.
11375
11376    declarator-id:
11377      id-expression
11378      :: [opt] nested-name-specifier [opt] type-name
11379
11380    In the `id-expression' case, the value returned is as for
11381    cp_parser_id_expression if the id-expression was an unqualified-id.
11382    If the id-expression was a qualified-id, then a SCOPE_REF is
11383    returned.  The first operand is the scope (either a NAMESPACE_DECL
11384    or TREE_TYPE), but the second is still just a representation of an
11385    unqualified-id.  */
11386
11387 static tree
11388 cp_parser_declarator_id (cp_parser* parser)
11389 {
11390   tree id_expression;
11391
11392   /* The expression must be an id-expression.  Assume that qualified
11393      names are the names of types so that:
11394
11395        template <class T>
11396        int S<T>::R::i = 3;
11397
11398      will work; we must treat `S<T>::R' as the name of a type.
11399      Similarly, assume that qualified names are templates, where
11400      required, so that:
11401
11402        template <class T>
11403        int S<T>::R<T>::i = 3;
11404
11405      will work, too.  */
11406   id_expression = cp_parser_id_expression (parser,
11407                                            /*template_keyword_p=*/false,
11408                                            /*check_dependency_p=*/false,
11409                                            /*template_p=*/NULL,
11410                                            /*declarator_p=*/true);
11411   /* If the name was qualified, create a SCOPE_REF to represent
11412      that.  */
11413   if (parser->scope)
11414     {
11415       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11416       parser->scope = NULL_TREE;
11417     }
11418
11419   return id_expression;
11420 }
11421
11422 /* Parse a type-id.
11423
11424    type-id:
11425      type-specifier-seq abstract-declarator [opt]
11426
11427    Returns the TYPE specified.  */
11428
11429 static tree
11430 cp_parser_type_id (cp_parser* parser)
11431 {
11432   cp_decl_specifier_seq type_specifier_seq;
11433   cp_declarator *abstract_declarator;
11434
11435   /* Parse the type-specifier-seq.  */
11436   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11437   if (type_specifier_seq.type == error_mark_node)
11438     return error_mark_node;
11439
11440   /* There might or might not be an abstract declarator.  */
11441   cp_parser_parse_tentatively (parser);
11442   /* Look for the declarator.  */
11443   abstract_declarator
11444     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11445                             /*parenthesized_p=*/NULL,
11446                             /*member_p=*/false);
11447   /* Check to see if there really was a declarator.  */
11448   if (!cp_parser_parse_definitely (parser))
11449     abstract_declarator = NULL;
11450
11451   return groktypename (&type_specifier_seq, abstract_declarator);
11452 }
11453
11454 /* Parse a type-specifier-seq.
11455
11456    type-specifier-seq:
11457      type-specifier type-specifier-seq [opt]
11458
11459    GNU extension:
11460
11461    type-specifier-seq:
11462      attributes type-specifier-seq [opt]
11463
11464    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11465
11466 static void
11467 cp_parser_type_specifier_seq (cp_parser* parser,
11468                               cp_decl_specifier_seq *type_specifier_seq)
11469 {
11470   bool seen_type_specifier = false;
11471
11472   /* Clear the TYPE_SPECIFIER_SEQ.  */
11473   clear_decl_specs (type_specifier_seq);
11474
11475   /* Parse the type-specifiers and attributes.  */
11476   while (true)
11477     {
11478       tree type_specifier;
11479
11480       /* Check for attributes first.  */
11481       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11482         {
11483           type_specifier_seq->attributes =
11484             chainon (type_specifier_seq->attributes,
11485                      cp_parser_attributes_opt (parser));
11486           continue;
11487         }
11488
11489       /* Look for the type-specifier.  */
11490       type_specifier = cp_parser_type_specifier (parser,
11491                                                  CP_PARSER_FLAGS_OPTIONAL,
11492                                                  type_specifier_seq,
11493                                                  /*is_declaration=*/false,
11494                                                  NULL,
11495                                                  NULL);
11496       /* If the first type-specifier could not be found, this is not a
11497          type-specifier-seq at all.  */
11498       if (!seen_type_specifier && !type_specifier)
11499         {
11500           cp_parser_error (parser, "expected type-specifier");
11501           type_specifier_seq->type = error_mark_node;
11502           return;
11503         }
11504       /* If subsequent type-specifiers could not be found, the
11505          type-specifier-seq is complete.  */
11506       else if (seen_type_specifier && !type_specifier)
11507         break;
11508
11509       seen_type_specifier = true;
11510     }
11511
11512   return;
11513 }
11514
11515 /* Parse a parameter-declaration-clause.
11516
11517    parameter-declaration-clause:
11518      parameter-declaration-list [opt] ... [opt]
11519      parameter-declaration-list , ...
11520
11521    Returns a representation for the parameter declarations.  A return
11522    value of NULL indicates a parameter-declaration-clause consisting
11523    only of an ellipsis.  */
11524
11525 static cp_parameter_declarator *
11526 cp_parser_parameter_declaration_clause (cp_parser* parser)
11527 {
11528   cp_parameter_declarator *parameters;
11529   cp_token *token;
11530   bool ellipsis_p;
11531   bool is_error;
11532
11533   /* Peek at the next token.  */
11534   token = cp_lexer_peek_token (parser->lexer);
11535   /* Check for trivial parameter-declaration-clauses.  */
11536   if (token->type == CPP_ELLIPSIS)
11537     {
11538       /* Consume the `...' token.  */
11539       cp_lexer_consume_token (parser->lexer);
11540       return NULL;
11541     }
11542   else if (token->type == CPP_CLOSE_PAREN)
11543     /* There are no parameters.  */
11544     {
11545 #ifndef NO_IMPLICIT_EXTERN_C
11546       if (in_system_header && current_class_type == NULL
11547           && current_lang_name == lang_name_c)
11548         return NULL;
11549       else
11550 #endif
11551         return no_parameters;
11552     }
11553   /* Check for `(void)', too, which is a special case.  */
11554   else if (token->keyword == RID_VOID
11555            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11556                == CPP_CLOSE_PAREN))
11557     {
11558       /* Consume the `void' token.  */
11559       cp_lexer_consume_token (parser->lexer);
11560       /* There are no parameters.  */
11561       return no_parameters;
11562     }
11563
11564   /* Parse the parameter-declaration-list.  */
11565   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11566   /* If a parse error occurred while parsing the
11567      parameter-declaration-list, then the entire
11568      parameter-declaration-clause is erroneous.  */
11569   if (is_error)
11570     return NULL;
11571
11572   /* Peek at the next token.  */
11573   token = cp_lexer_peek_token (parser->lexer);
11574   /* If it's a `,', the clause should terminate with an ellipsis.  */
11575   if (token->type == CPP_COMMA)
11576     {
11577       /* Consume the `,'.  */
11578       cp_lexer_consume_token (parser->lexer);
11579       /* Expect an ellipsis.  */
11580       ellipsis_p
11581         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11582     }
11583   /* It might also be `...' if the optional trailing `,' was
11584      omitted.  */
11585   else if (token->type == CPP_ELLIPSIS)
11586     {
11587       /* Consume the `...' token.  */
11588       cp_lexer_consume_token (parser->lexer);
11589       /* And remember that we saw it.  */
11590       ellipsis_p = true;
11591     }
11592   else
11593     ellipsis_p = false;
11594
11595   /* Finish the parameter list.  */
11596   if (parameters && ellipsis_p)
11597     parameters->ellipsis_p = true;
11598
11599   return parameters;
11600 }
11601
11602 /* Parse a parameter-declaration-list.
11603
11604    parameter-declaration-list:
11605      parameter-declaration
11606      parameter-declaration-list , parameter-declaration
11607
11608    Returns a representation of the parameter-declaration-list, as for
11609    cp_parser_parameter_declaration_clause.  However, the
11610    `void_list_node' is never appended to the list.  Upon return,
11611    *IS_ERROR will be true iff an error occurred.  */
11612
11613 static cp_parameter_declarator *
11614 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11615 {
11616   cp_parameter_declarator *parameters = NULL;
11617   cp_parameter_declarator **tail = &parameters;
11618
11619   /* Assume all will go well.  */
11620   *is_error = false;
11621
11622   /* Look for more parameters.  */
11623   while (true)
11624     {
11625       cp_parameter_declarator *parameter;
11626       bool parenthesized_p;
11627       /* Parse the parameter.  */
11628       parameter
11629         = cp_parser_parameter_declaration (parser,
11630                                            /*template_parm_p=*/false,
11631                                            &parenthesized_p);
11632
11633       /* If a parse error occurred parsing the parameter declaration,
11634          then the entire parameter-declaration-list is erroneous.  */
11635       if (!parameter)
11636         {
11637           *is_error = true;
11638           parameters = NULL;
11639           break;
11640         }
11641       /* Add the new parameter to the list.  */
11642       *tail = parameter;
11643       tail = &parameter->next;
11644
11645       /* Peek at the next token.  */
11646       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11647           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11648         /* The parameter-declaration-list is complete.  */
11649         break;
11650       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11651         {
11652           cp_token *token;
11653
11654           /* Peek at the next token.  */
11655           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11656           /* If it's an ellipsis, then the list is complete.  */
11657           if (token->type == CPP_ELLIPSIS)
11658             break;
11659           /* Otherwise, there must be more parameters.  Consume the
11660              `,'.  */
11661           cp_lexer_consume_token (parser->lexer);
11662           /* When parsing something like:
11663
11664                 int i(float f, double d)
11665
11666              we can tell after seeing the declaration for "f" that we
11667              are not looking at an initialization of a variable "i",
11668              but rather at the declaration of a function "i".
11669
11670              Due to the fact that the parsing of template arguments
11671              (as specified to a template-id) requires backtracking we
11672              cannot use this technique when inside a template argument
11673              list.  */
11674           if (!parser->in_template_argument_list_p
11675               && !parser->in_type_id_in_expr_p
11676               && cp_parser_parsing_tentatively (parser)
11677               && !cp_parser_committed_to_tentative_parse (parser)
11678               /* However, a parameter-declaration of the form
11679                  "foat(f)" (which is a valid declaration of a
11680                  parameter "f") can also be interpreted as an
11681                  expression (the conversion of "f" to "float").  */
11682               && !parenthesized_p)
11683             cp_parser_commit_to_tentative_parse (parser);
11684         }
11685       else
11686         {
11687           cp_parser_error (parser, "expected %<,%> or %<...%>");
11688           if (!cp_parser_parsing_tentatively (parser)
11689               || cp_parser_committed_to_tentative_parse (parser))
11690             cp_parser_skip_to_closing_parenthesis (parser,
11691                                                    /*recovering=*/true,
11692                                                    /*or_comma=*/false,
11693                                                    /*consume_paren=*/false);
11694           break;
11695         }
11696     }
11697
11698   return parameters;
11699 }
11700
11701 /* Parse a parameter declaration.
11702
11703    parameter-declaration:
11704      decl-specifier-seq declarator
11705      decl-specifier-seq declarator = assignment-expression
11706      decl-specifier-seq abstract-declarator [opt]
11707      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11708
11709    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11710    declares a template parameter.  (In that case, a non-nested `>'
11711    token encountered during the parsing of the assignment-expression
11712    is not interpreted as a greater-than operator.)
11713
11714    Returns a representation of the parameter, or NULL if an error
11715    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11716    true iff the declarator is of the form "(p)".  */
11717
11718 static cp_parameter_declarator *
11719 cp_parser_parameter_declaration (cp_parser *parser,
11720                                  bool template_parm_p,
11721                                  bool *parenthesized_p)
11722 {
11723   int declares_class_or_enum;
11724   bool greater_than_is_operator_p;
11725   cp_decl_specifier_seq decl_specifiers;
11726   cp_declarator *declarator;
11727   tree default_argument;
11728   cp_token *token;
11729   const char *saved_message;
11730
11731   /* In a template parameter, `>' is not an operator.
11732
11733      [temp.param]
11734
11735      When parsing a default template-argument for a non-type
11736      template-parameter, the first non-nested `>' is taken as the end
11737      of the template parameter-list rather than a greater-than
11738      operator.  */
11739   greater_than_is_operator_p = !template_parm_p;
11740
11741   /* Type definitions may not appear in parameter types.  */
11742   saved_message = parser->type_definition_forbidden_message;
11743   parser->type_definition_forbidden_message
11744     = "types may not be defined in parameter types";
11745
11746   /* Parse the declaration-specifiers.  */
11747   cp_parser_decl_specifier_seq (parser,
11748                                 CP_PARSER_FLAGS_NONE,
11749                                 &decl_specifiers,
11750                                 &declares_class_or_enum);
11751   /* If an error occurred, there's no reason to attempt to parse the
11752      rest of the declaration.  */
11753   if (cp_parser_error_occurred (parser))
11754     {
11755       parser->type_definition_forbidden_message = saved_message;
11756       return NULL;
11757     }
11758
11759   /* Peek at the next token.  */
11760   token = cp_lexer_peek_token (parser->lexer);
11761   /* If the next token is a `)', `,', `=', `>', or `...', then there
11762      is no declarator.  */
11763   if (token->type == CPP_CLOSE_PAREN
11764       || token->type == CPP_COMMA
11765       || token->type == CPP_EQ
11766       || token->type == CPP_ELLIPSIS
11767       || token->type == CPP_GREATER)
11768     {
11769       declarator = NULL;
11770       if (parenthesized_p)
11771         *parenthesized_p = false;
11772     }
11773   /* Otherwise, there should be a declarator.  */
11774   else
11775     {
11776       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11777       parser->default_arg_ok_p = false;
11778
11779       /* After seeing a decl-specifier-seq, if the next token is not a
11780          "(", there is no possibility that the code is a valid
11781          expression.  Therefore, if parsing tentatively, we commit at
11782          this point.  */
11783       if (!parser->in_template_argument_list_p
11784           /* In an expression context, having seen:
11785
11786                (int((char ...
11787
11788              we cannot be sure whether we are looking at a
11789              function-type (taking a "char" as a parameter) or a cast
11790              of some object of type "char" to "int".  */
11791           && !parser->in_type_id_in_expr_p
11792           && cp_parser_parsing_tentatively (parser)
11793           && !cp_parser_committed_to_tentative_parse (parser)
11794           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11795         cp_parser_commit_to_tentative_parse (parser);
11796       /* Parse the declarator.  */
11797       declarator = cp_parser_declarator (parser,
11798                                          CP_PARSER_DECLARATOR_EITHER,
11799                                          /*ctor_dtor_or_conv_p=*/NULL,
11800                                          parenthesized_p,
11801                                          /*member_p=*/false);
11802       parser->default_arg_ok_p = saved_default_arg_ok_p;
11803       /* After the declarator, allow more attributes.  */
11804       decl_specifiers.attributes
11805         = chainon (decl_specifiers.attributes,
11806                    cp_parser_attributes_opt (parser));
11807     }
11808
11809   /* The restriction on defining new types applies only to the type
11810      of the parameter, not to the default argument.  */
11811   parser->type_definition_forbidden_message = saved_message;
11812
11813   /* If the next token is `=', then process a default argument.  */
11814   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11815     {
11816       bool saved_greater_than_is_operator_p;
11817       /* Consume the `='.  */
11818       cp_lexer_consume_token (parser->lexer);
11819
11820       /* If we are defining a class, then the tokens that make up the
11821          default argument must be saved and processed later.  */
11822       if (!template_parm_p && at_class_scope_p ()
11823           && TYPE_BEING_DEFINED (current_class_type))
11824         {
11825           unsigned depth = 0;
11826           cp_token *first_token;
11827           cp_token *token;
11828
11829           /* Add tokens until we have processed the entire default
11830              argument.  We add the range [first_token, token). */
11831           first_token = cp_lexer_peek_token (parser->lexer);
11832           while (true)
11833             {
11834               bool done = false;
11835
11836               /* Peek at the next token.  */
11837               token = cp_lexer_peek_token (parser->lexer);
11838               /* What we do depends on what token we have.  */
11839               switch (token->type)
11840                 {
11841                   /* In valid code, a default argument must be
11842                      immediately followed by a `,' `)', or `...'.  */
11843                 case CPP_COMMA:
11844                 case CPP_CLOSE_PAREN:
11845                 case CPP_ELLIPSIS:
11846                   /* If we run into a non-nested `;', `}', or `]',
11847                      then the code is invalid -- but the default
11848                      argument is certainly over.  */
11849                 case CPP_SEMICOLON:
11850                 case CPP_CLOSE_BRACE:
11851                 case CPP_CLOSE_SQUARE:
11852                   if (depth == 0)
11853                     done = true;
11854                   /* Update DEPTH, if necessary.  */
11855                   else if (token->type == CPP_CLOSE_PAREN
11856                            || token->type == CPP_CLOSE_BRACE
11857                            || token->type == CPP_CLOSE_SQUARE)
11858                     --depth;
11859                   break;
11860
11861                 case CPP_OPEN_PAREN:
11862                 case CPP_OPEN_SQUARE:
11863                 case CPP_OPEN_BRACE:
11864                   ++depth;
11865                   break;
11866
11867                 case CPP_GREATER:
11868                   /* If we see a non-nested `>', and `>' is not an
11869                      operator, then it marks the end of the default
11870                      argument.  */
11871                   if (!depth && !greater_than_is_operator_p)
11872                     done = true;
11873                   break;
11874
11875                   /* If we run out of tokens, issue an error message.  */
11876                 case CPP_EOF:
11877                   error ("file ends in default argument");
11878                   done = true;
11879                   break;
11880
11881                 case CPP_NAME:
11882                 case CPP_SCOPE:
11883                   /* In these cases, we should look for template-ids.
11884                      For example, if the default argument is
11885                      `X<int, double>()', we need to do name lookup to
11886                      figure out whether or not `X' is a template; if
11887                      so, the `,' does not end the default argument.
11888
11889                      That is not yet done.  */
11890                   break;
11891
11892                 default:
11893                   break;
11894                 }
11895
11896               /* If we've reached the end, stop.  */
11897               if (done)
11898                 break;
11899
11900               /* Add the token to the token block.  */
11901               token = cp_lexer_consume_token (parser->lexer);
11902             }
11903
11904           /* Create a DEFAULT_ARG to represented the unparsed default
11905              argument.  */
11906           default_argument = make_node (DEFAULT_ARG);
11907           DEFARG_TOKENS (default_argument)
11908             = cp_token_cache_new (first_token, token);  
11909         }
11910       /* Outside of a class definition, we can just parse the
11911          assignment-expression.  */
11912       else
11913         {
11914           bool saved_local_variables_forbidden_p;
11915
11916           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11917              set correctly.  */
11918           saved_greater_than_is_operator_p
11919             = parser->greater_than_is_operator_p;
11920           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11921           /* Local variable names (and the `this' keyword) may not
11922              appear in a default argument.  */
11923           saved_local_variables_forbidden_p
11924             = parser->local_variables_forbidden_p;
11925           parser->local_variables_forbidden_p = true;
11926           /* Parse the assignment-expression.  */
11927           default_argument = cp_parser_assignment_expression (parser);
11928           /* Restore saved state.  */
11929           parser->greater_than_is_operator_p
11930             = saved_greater_than_is_operator_p;
11931           parser->local_variables_forbidden_p
11932             = saved_local_variables_forbidden_p;
11933         }
11934       if (!parser->default_arg_ok_p)
11935         {
11936           if (!flag_pedantic_errors)
11937             warning ("deprecated use of default argument for parameter of non-function");
11938           else
11939             {
11940               error ("default arguments are only permitted for function parameters");
11941               default_argument = NULL_TREE;
11942             }
11943         }
11944     }
11945   else
11946     default_argument = NULL_TREE;
11947
11948   return make_parameter_declarator (&decl_specifiers,
11949                                     declarator,
11950                                     default_argument);
11951 }
11952
11953 /* Parse a function-body.
11954
11955    function-body:
11956      compound_statement  */
11957
11958 static void
11959 cp_parser_function_body (cp_parser *parser)
11960 {
11961   cp_parser_compound_statement (parser, NULL, false);
11962 }
11963
11964 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11965    true if a ctor-initializer was present.  */
11966
11967 static bool
11968 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11969 {
11970   tree body;
11971   bool ctor_initializer_p;
11972
11973   /* Begin the function body.  */
11974   body = begin_function_body ();
11975   /* Parse the optional ctor-initializer.  */
11976   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11977   /* Parse the function-body.  */
11978   cp_parser_function_body (parser);
11979   /* Finish the function body.  */
11980   finish_function_body (body);
11981
11982   return ctor_initializer_p;
11983 }
11984
11985 /* Parse an initializer.
11986
11987    initializer:
11988      = initializer-clause
11989      ( expression-list )
11990
11991    Returns a expression representing the initializer.  If no
11992    initializer is present, NULL_TREE is returned.
11993
11994    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11995    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11996    set to FALSE if there is no initializer present.  If there is an
11997    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11998    is set to true; otherwise it is set to false.  */
11999
12000 static tree
12001 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12002                        bool* non_constant_p)
12003 {
12004   cp_token *token;
12005   tree init;
12006
12007   /* Peek at the next token.  */
12008   token = cp_lexer_peek_token (parser->lexer);
12009
12010   /* Let our caller know whether or not this initializer was
12011      parenthesized.  */
12012   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12013   /* Assume that the initializer is constant.  */
12014   *non_constant_p = false;
12015
12016   if (token->type == CPP_EQ)
12017     {
12018       /* Consume the `='.  */
12019       cp_lexer_consume_token (parser->lexer);
12020       /* Parse the initializer-clause.  */
12021       init = cp_parser_initializer_clause (parser, non_constant_p);
12022     }
12023   else if (token->type == CPP_OPEN_PAREN)
12024     init = cp_parser_parenthesized_expression_list (parser, false,
12025                                                     non_constant_p);
12026   else
12027     {
12028       /* Anything else is an error.  */
12029       cp_parser_error (parser, "expected initializer");
12030       init = error_mark_node;
12031     }
12032
12033   return init;
12034 }
12035
12036 /* Parse an initializer-clause.
12037
12038    initializer-clause:
12039      assignment-expression
12040      { initializer-list , [opt] }
12041      { }
12042
12043    Returns an expression representing the initializer.
12044
12045    If the `assignment-expression' production is used the value
12046    returned is simply a representation for the expression.
12047
12048    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12049    the elements of the initializer-list (or NULL_TREE, if the last
12050    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12051    NULL_TREE.  There is no way to detect whether or not the optional
12052    trailing `,' was provided.  NON_CONSTANT_P is as for
12053    cp_parser_initializer.  */
12054
12055 static tree
12056 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12057 {
12058   tree initializer;
12059
12060   /* If it is not a `{', then we are looking at an
12061      assignment-expression.  */
12062   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12063     {
12064       initializer
12065         = cp_parser_constant_expression (parser,
12066                                         /*allow_non_constant_p=*/true,
12067                                         non_constant_p);
12068       if (!*non_constant_p)
12069         initializer = fold_non_dependent_expr (initializer);
12070     }
12071   else
12072     {
12073       /* Consume the `{' token.  */
12074       cp_lexer_consume_token (parser->lexer);
12075       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12076       initializer = make_node (CONSTRUCTOR);
12077       /* If it's not a `}', then there is a non-trivial initializer.  */
12078       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12079         {
12080           /* Parse the initializer list.  */
12081           CONSTRUCTOR_ELTS (initializer)
12082             = cp_parser_initializer_list (parser, non_constant_p);
12083           /* A trailing `,' token is allowed.  */
12084           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12085             cp_lexer_consume_token (parser->lexer);
12086         }
12087       /* Now, there should be a trailing `}'.  */
12088       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12089     }
12090
12091   return initializer;
12092 }
12093
12094 /* Parse an initializer-list.
12095
12096    initializer-list:
12097      initializer-clause
12098      initializer-list , initializer-clause
12099
12100    GNU Extension:
12101
12102    initializer-list:
12103      identifier : initializer-clause
12104      initializer-list, identifier : initializer-clause
12105
12106    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12107    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12108    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12109    as for cp_parser_initializer.  */
12110
12111 static tree
12112 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12113 {
12114   tree initializers = NULL_TREE;
12115
12116   /* Assume all of the expressions are constant.  */
12117   *non_constant_p = false;
12118
12119   /* Parse the rest of the list.  */
12120   while (true)
12121     {
12122       cp_token *token;
12123       tree identifier;
12124       tree initializer;
12125       bool clause_non_constant_p;
12126
12127       /* If the next token is an identifier and the following one is a
12128          colon, we are looking at the GNU designated-initializer
12129          syntax.  */
12130       if (cp_parser_allow_gnu_extensions_p (parser)
12131           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12132           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12133         {
12134           /* Consume the identifier.  */
12135           identifier = cp_lexer_consume_token (parser->lexer)->value;
12136           /* Consume the `:'.  */
12137           cp_lexer_consume_token (parser->lexer);
12138         }
12139       else
12140         identifier = NULL_TREE;
12141
12142       /* Parse the initializer.  */
12143       initializer = cp_parser_initializer_clause (parser,
12144                                                   &clause_non_constant_p);
12145       /* If any clause is non-constant, so is the entire initializer.  */
12146       if (clause_non_constant_p)
12147         *non_constant_p = true;
12148       /* Add it to the list.  */
12149       initializers = tree_cons (identifier, initializer, initializers);
12150
12151       /* If the next token is not a comma, we have reached the end of
12152          the list.  */
12153       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12154         break;
12155
12156       /* Peek at the next token.  */
12157       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12158       /* If the next token is a `}', then we're still done.  An
12159          initializer-clause can have a trailing `,' after the
12160          initializer-list and before the closing `}'.  */
12161       if (token->type == CPP_CLOSE_BRACE)
12162         break;
12163
12164       /* Consume the `,' token.  */
12165       cp_lexer_consume_token (parser->lexer);
12166     }
12167
12168   /* The initializers were built up in reverse order, so we need to
12169      reverse them now.  */
12170   return nreverse (initializers);
12171 }
12172
12173 /* Classes [gram.class] */
12174
12175 /* Parse a class-name.
12176
12177    class-name:
12178      identifier
12179      template-id
12180
12181    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12182    to indicate that names looked up in dependent types should be
12183    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12184    keyword has been used to indicate that the name that appears next
12185    is a template.  TYPE_P is true iff the next name should be treated
12186    as class-name, even if it is declared to be some other kind of name
12187    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12188    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12189    being defined in a class-head.
12190
12191    Returns the TYPE_DECL representing the class.  */
12192
12193 static tree
12194 cp_parser_class_name (cp_parser *parser,
12195                       bool typename_keyword_p,
12196                       bool template_keyword_p,
12197                       bool type_p,
12198                       bool check_dependency_p,
12199                       bool class_head_p,
12200                       bool is_declaration)
12201 {
12202   tree decl;
12203   tree scope;
12204   bool typename_p;
12205   cp_token *token;
12206
12207   /* All class-names start with an identifier.  */
12208   token = cp_lexer_peek_token (parser->lexer);
12209   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12210     {
12211       cp_parser_error (parser, "expected class-name");
12212       return error_mark_node;
12213     }
12214
12215   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12216      to a template-id, so we save it here.  */
12217   scope = parser->scope;
12218   if (scope == error_mark_node)
12219     return error_mark_node;
12220
12221   /* Any name names a type if we're following the `typename' keyword
12222      in a qualified name where the enclosing scope is type-dependent.  */
12223   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12224                 && dependent_type_p (scope));
12225   /* Handle the common case (an identifier, but not a template-id)
12226      efficiently.  */
12227   if (token->type == CPP_NAME
12228       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12229     {
12230       tree identifier;
12231
12232       /* Look for the identifier.  */
12233       identifier = cp_parser_identifier (parser);
12234       /* If the next token isn't an identifier, we are certainly not
12235          looking at a class-name.  */
12236       if (identifier == error_mark_node)
12237         decl = error_mark_node;
12238       /* If we know this is a type-name, there's no need to look it
12239          up.  */
12240       else if (typename_p)
12241         decl = identifier;
12242       else
12243         {
12244           /* If the next token is a `::', then the name must be a type
12245              name.
12246
12247              [basic.lookup.qual]
12248
12249              During the lookup for a name preceding the :: scope
12250              resolution operator, object, function, and enumerator
12251              names are ignored.  */
12252           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12253             type_p = true;
12254           /* Look up the name.  */
12255           decl = cp_parser_lookup_name (parser, identifier,
12256                                         type_p,
12257                                         /*is_template=*/false,
12258                                         /*is_namespace=*/false,
12259                                         check_dependency_p,
12260                                         /*ambiguous_p=*/NULL);
12261         }
12262     }
12263   else
12264     {
12265       /* Try a template-id.  */
12266       decl = cp_parser_template_id (parser, template_keyword_p,
12267                                     check_dependency_p,
12268                                     is_declaration);
12269       if (decl == error_mark_node)
12270         return error_mark_node;
12271     }
12272
12273   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12274
12275   /* If this is a typename, create a TYPENAME_TYPE.  */
12276   if (typename_p && decl != error_mark_node)
12277     {
12278       decl = make_typename_type (scope, decl, /*complain=*/1);
12279       if (decl != error_mark_node)
12280         decl = TYPE_NAME (decl);
12281     }
12282
12283   /* Check to see that it is really the name of a class.  */
12284   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12285       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12286       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12287     /* Situations like this:
12288
12289          template <typename T> struct A {
12290            typename T::template X<int>::I i;
12291          };
12292
12293        are problematic.  Is `T::template X<int>' a class-name?  The
12294        standard does not seem to be definitive, but there is no other
12295        valid interpretation of the following `::'.  Therefore, those
12296        names are considered class-names.  */
12297     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12298   else if (decl == error_mark_node
12299            || TREE_CODE (decl) != TYPE_DECL
12300            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12301     {
12302       cp_parser_error (parser, "expected class-name");
12303       return error_mark_node;
12304     }
12305
12306   return decl;
12307 }
12308
12309 /* Parse a class-specifier.
12310
12311    class-specifier:
12312      class-head { member-specification [opt] }
12313
12314    Returns the TREE_TYPE representing the class.  */
12315
12316 static tree
12317 cp_parser_class_specifier (cp_parser* parser)
12318 {
12319   cp_token *token;
12320   tree type;
12321   tree attributes = NULL_TREE;
12322   int has_trailing_semicolon;
12323   bool nested_name_specifier_p;
12324   unsigned saved_num_template_parameter_lists;
12325   bool pop_p = false;
12326   tree scope = NULL_TREE;
12327
12328   push_deferring_access_checks (dk_no_deferred);
12329
12330   /* Parse the class-head.  */
12331   type = cp_parser_class_head (parser,
12332                                &nested_name_specifier_p,
12333                                &attributes);
12334   /* If the class-head was a semantic disaster, skip the entire body
12335      of the class.  */
12336   if (!type)
12337     {
12338       cp_parser_skip_to_end_of_block_or_statement (parser);
12339       pop_deferring_access_checks ();
12340       return error_mark_node;
12341     }
12342
12343   /* Look for the `{'.  */
12344   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12345     {
12346       pop_deferring_access_checks ();
12347       return error_mark_node;
12348     }
12349
12350   /* Issue an error message if type-definitions are forbidden here.  */
12351   cp_parser_check_type_definition (parser);
12352   /* Remember that we are defining one more class.  */
12353   ++parser->num_classes_being_defined;
12354   /* Inside the class, surrounding template-parameter-lists do not
12355      apply.  */
12356   saved_num_template_parameter_lists
12357     = parser->num_template_parameter_lists;
12358   parser->num_template_parameter_lists = 0;
12359
12360   /* Start the class.  */
12361   if (nested_name_specifier_p)
12362     {
12363       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12364       pop_p = push_scope (scope);
12365     }
12366   type = begin_class_definition (type);
12367
12368   if (type == error_mark_node)
12369     /* If the type is erroneous, skip the entire body of the class.  */
12370     cp_parser_skip_to_closing_brace (parser);
12371   else
12372     /* Parse the member-specification.  */
12373     cp_parser_member_specification_opt (parser);
12374
12375   /* Look for the trailing `}'.  */
12376   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12377   /* We get better error messages by noticing a common problem: a
12378      missing trailing `;'.  */
12379   token = cp_lexer_peek_token (parser->lexer);
12380   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12381   /* Look for trailing attributes to apply to this class.  */
12382   if (cp_parser_allow_gnu_extensions_p (parser))
12383     {
12384       tree sub_attr = cp_parser_attributes_opt (parser);
12385       attributes = chainon (attributes, sub_attr);
12386     }
12387   if (type != error_mark_node)
12388     type = finish_struct (type, attributes);
12389   if (pop_p)
12390     pop_scope (scope);
12391   /* If this class is not itself within the scope of another class,
12392      then we need to parse the bodies of all of the queued function
12393      definitions.  Note that the queued functions defined in a class
12394      are not always processed immediately following the
12395      class-specifier for that class.  Consider:
12396
12397        struct A {
12398          struct B { void f() { sizeof (A); } };
12399        };
12400
12401      If `f' were processed before the processing of `A' were
12402      completed, there would be no way to compute the size of `A'.
12403      Note that the nesting we are interested in here is lexical --
12404      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12405      for:
12406
12407        struct A { struct B; };
12408        struct A::B { void f() { } };
12409
12410      there is no need to delay the parsing of `A::B::f'.  */
12411   if (--parser->num_classes_being_defined == 0)
12412     {
12413       tree queue_entry;
12414       tree fn;
12415       tree class_type;
12416       bool pop_p;
12417
12418       /* In a first pass, parse default arguments to the functions.
12419          Then, in a second pass, parse the bodies of the functions.
12420          This two-phased approach handles cases like:
12421
12422             struct S {
12423               void f() { g(); }
12424               void g(int i = 3);
12425             };
12426
12427          */
12428       class_type = NULL_TREE;
12429       pop_p = false;
12430       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12431              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12432            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12433            TREE_PURPOSE (parser->unparsed_functions_queues)
12434              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12435         {
12436           fn = TREE_VALUE (queue_entry);
12437           /* If there are default arguments that have not yet been processed,
12438              take care of them now.  */
12439           if (class_type != TREE_PURPOSE (queue_entry))
12440             {
12441               if (pop_p)
12442                 pop_scope (class_type);
12443               class_type = TREE_PURPOSE (queue_entry);
12444               pop_p = push_scope (class_type);
12445             }
12446           /* Make sure that any template parameters are in scope.  */
12447           maybe_begin_member_template_processing (fn);
12448           /* Parse the default argument expressions.  */
12449           cp_parser_late_parsing_default_args (parser, fn);
12450           /* Remove any template parameters from the symbol table.  */
12451           maybe_end_member_template_processing ();
12452         }
12453       if (pop_p)
12454         pop_scope (class_type);
12455       /* Now parse the body of the functions.  */
12456       for (TREE_VALUE (parser->unparsed_functions_queues)
12457              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12458            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12459            TREE_VALUE (parser->unparsed_functions_queues)
12460              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12461         {
12462           /* Figure out which function we need to process.  */
12463           fn = TREE_VALUE (queue_entry);
12464
12465           /* A hack to prevent garbage collection.  */
12466           function_depth++;
12467
12468           /* Parse the function.  */
12469           cp_parser_late_parsing_for_member (parser, fn);
12470           function_depth--;
12471         }
12472     }
12473
12474   /* Put back any saved access checks.  */
12475   pop_deferring_access_checks ();
12476
12477   /* Restore the count of active template-parameter-lists.  */
12478   parser->num_template_parameter_lists
12479     = saved_num_template_parameter_lists;
12480
12481   return type;
12482 }
12483
12484 /* Parse a class-head.
12485
12486    class-head:
12487      class-key identifier [opt] base-clause [opt]
12488      class-key nested-name-specifier identifier base-clause [opt]
12489      class-key nested-name-specifier [opt] template-id
12490        base-clause [opt]
12491
12492    GNU Extensions:
12493      class-key attributes identifier [opt] base-clause [opt]
12494      class-key attributes nested-name-specifier identifier base-clause [opt]
12495      class-key attributes nested-name-specifier [opt] template-id
12496        base-clause [opt]
12497
12498    Returns the TYPE of the indicated class.  Sets
12499    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12500    involving a nested-name-specifier was used, and FALSE otherwise.
12501
12502    Returns NULL_TREE if the class-head is syntactically valid, but
12503    semantically invalid in a way that means we should skip the entire
12504    body of the class.  */
12505
12506 static tree
12507 cp_parser_class_head (cp_parser* parser,
12508                       bool* nested_name_specifier_p,
12509                       tree *attributes_p)
12510 {
12511   tree nested_name_specifier;
12512   enum tag_types class_key;
12513   tree id = NULL_TREE;
12514   tree type = NULL_TREE;
12515   tree attributes;
12516   bool template_id_p = false;
12517   bool qualified_p = false;
12518   bool invalid_nested_name_p = false;
12519   bool invalid_explicit_specialization_p = false;
12520   bool pop_p = false;
12521   unsigned num_templates;
12522   tree bases;
12523
12524   /* Assume no nested-name-specifier will be present.  */
12525   *nested_name_specifier_p = false;
12526   /* Assume no template parameter lists will be used in defining the
12527      type.  */
12528   num_templates = 0;
12529
12530   /* Look for the class-key.  */
12531   class_key = cp_parser_class_key (parser);
12532   if (class_key == none_type)
12533     return error_mark_node;
12534
12535   /* Parse the attributes.  */
12536   attributes = cp_parser_attributes_opt (parser);
12537
12538   /* If the next token is `::', that is invalid -- but sometimes
12539      people do try to write:
12540
12541        struct ::S {};
12542
12543      Handle this gracefully by accepting the extra qualifier, and then
12544      issuing an error about it later if this really is a
12545      class-head.  If it turns out just to be an elaborated type
12546      specifier, remain silent.  */
12547   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12548     qualified_p = true;
12549
12550   push_deferring_access_checks (dk_no_check);
12551
12552   /* Determine the name of the class.  Begin by looking for an
12553      optional nested-name-specifier.  */
12554   nested_name_specifier
12555     = cp_parser_nested_name_specifier_opt (parser,
12556                                            /*typename_keyword_p=*/false,
12557                                            /*check_dependency_p=*/false,
12558                                            /*type_p=*/false,
12559                                            /*is_declaration=*/false);
12560   /* If there was a nested-name-specifier, then there *must* be an
12561      identifier.  */
12562   if (nested_name_specifier)
12563     {
12564       /* Although the grammar says `identifier', it really means
12565          `class-name' or `template-name'.  You are only allowed to
12566          define a class that has already been declared with this
12567          syntax.
12568
12569          The proposed resolution for Core Issue 180 says that whever
12570          you see `class T::X' you should treat `X' as a type-name.
12571
12572          It is OK to define an inaccessible class; for example:
12573
12574            class A { class B; };
12575            class A::B {};
12576
12577          We do not know if we will see a class-name, or a
12578          template-name.  We look for a class-name first, in case the
12579          class-name is a template-id; if we looked for the
12580          template-name first we would stop after the template-name.  */
12581       cp_parser_parse_tentatively (parser);
12582       type = cp_parser_class_name (parser,
12583                                    /*typename_keyword_p=*/false,
12584                                    /*template_keyword_p=*/false,
12585                                    /*type_p=*/true,
12586                                    /*check_dependency_p=*/false,
12587                                    /*class_head_p=*/true,
12588                                    /*is_declaration=*/false);
12589       /* If that didn't work, ignore the nested-name-specifier.  */
12590       if (!cp_parser_parse_definitely (parser))
12591         {
12592           invalid_nested_name_p = true;
12593           id = cp_parser_identifier (parser);
12594           if (id == error_mark_node)
12595             id = NULL_TREE;
12596         }
12597       /* If we could not find a corresponding TYPE, treat this
12598          declaration like an unqualified declaration.  */
12599       if (type == error_mark_node)
12600         nested_name_specifier = NULL_TREE;
12601       /* Otherwise, count the number of templates used in TYPE and its
12602          containing scopes.  */
12603       else
12604         {
12605           tree scope;
12606
12607           for (scope = TREE_TYPE (type);
12608                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12609                scope = (TYPE_P (scope)
12610                         ? TYPE_CONTEXT (scope)
12611                         : DECL_CONTEXT (scope)))
12612             if (TYPE_P (scope)
12613                 && CLASS_TYPE_P (scope)
12614                 && CLASSTYPE_TEMPLATE_INFO (scope)
12615                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12616                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12617               ++num_templates;
12618         }
12619     }
12620   /* Otherwise, the identifier is optional.  */
12621   else
12622     {
12623       /* We don't know whether what comes next is a template-id,
12624          an identifier, or nothing at all.  */
12625       cp_parser_parse_tentatively (parser);
12626       /* Check for a template-id.  */
12627       id = cp_parser_template_id (parser,
12628                                   /*template_keyword_p=*/false,
12629                                   /*check_dependency_p=*/true,
12630                                   /*is_declaration=*/true);
12631       /* If that didn't work, it could still be an identifier.  */
12632       if (!cp_parser_parse_definitely (parser))
12633         {
12634           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12635             id = cp_parser_identifier (parser);
12636           else
12637             id = NULL_TREE;
12638         }
12639       else
12640         {
12641           template_id_p = true;
12642           ++num_templates;
12643         }
12644     }
12645
12646   pop_deferring_access_checks ();
12647
12648   if (id)
12649     cp_parser_check_for_invalid_template_id (parser, id);
12650
12651   /* If it's not a `:' or a `{' then we can't really be looking at a
12652      class-head, since a class-head only appears as part of a
12653      class-specifier.  We have to detect this situation before calling
12654      xref_tag, since that has irreversible side-effects.  */
12655   if (!cp_parser_next_token_starts_class_definition_p (parser))
12656     {
12657       cp_parser_error (parser, "expected %<{%> or %<:%>");
12658       return error_mark_node;
12659     }
12660
12661   /* At this point, we're going ahead with the class-specifier, even
12662      if some other problem occurs.  */
12663   cp_parser_commit_to_tentative_parse (parser);
12664   /* Issue the error about the overly-qualified name now.  */
12665   if (qualified_p)
12666     cp_parser_error (parser,
12667                      "global qualification of class name is invalid");
12668   else if (invalid_nested_name_p)
12669     cp_parser_error (parser,
12670                      "qualified name does not name a class");
12671   else if (nested_name_specifier)
12672     {
12673       tree scope;
12674       /* Figure out in what scope the declaration is being placed.  */
12675       scope = current_scope ();
12676       if (!scope)
12677         scope = current_namespace;
12678       /* If that scope does not contain the scope in which the
12679          class was originally declared, the program is invalid.  */
12680       if (scope && !is_ancestor (scope, nested_name_specifier))
12681         {
12682           error ("declaration of %qD in %qD which does not enclose %qD",
12683                  type, scope, nested_name_specifier);
12684           type = NULL_TREE;
12685           goto done;
12686         }
12687       /* [dcl.meaning]
12688
12689          A declarator-id shall not be qualified exception of the
12690          definition of a ... nested class outside of its class
12691          ... [or] a the definition or explicit instantiation of a
12692          class member of a namespace outside of its namespace.  */
12693       if (scope == nested_name_specifier)
12694         {
12695           pedwarn ("extra qualification ignored");
12696           nested_name_specifier = NULL_TREE;
12697           num_templates = 0;
12698         }
12699     }
12700   /* An explicit-specialization must be preceded by "template <>".  If
12701      it is not, try to recover gracefully.  */
12702   if (at_namespace_scope_p ()
12703       && parser->num_template_parameter_lists == 0
12704       && template_id_p)
12705     {
12706       error ("an explicit specialization must be preceded by %<template <>%>");
12707       invalid_explicit_specialization_p = true;
12708       /* Take the same action that would have been taken by
12709          cp_parser_explicit_specialization.  */
12710       ++parser->num_template_parameter_lists;
12711       begin_specialization ();
12712     }
12713   /* There must be no "return" statements between this point and the
12714      end of this function; set "type "to the correct return value and
12715      use "goto done;" to return.  */
12716   /* Make sure that the right number of template parameters were
12717      present.  */
12718   if (!cp_parser_check_template_parameters (parser, num_templates))
12719     {
12720       /* If something went wrong, there is no point in even trying to
12721          process the class-definition.  */
12722       type = NULL_TREE;
12723       goto done;
12724     }
12725
12726   /* Look up the type.  */
12727   if (template_id_p)
12728     {
12729       type = TREE_TYPE (id);
12730       maybe_process_partial_specialization (type);
12731     }
12732   else if (!nested_name_specifier)
12733     {
12734       /* If the class was unnamed, create a dummy name.  */
12735       if (!id)
12736         id = make_anon_name ();
12737       type = xref_tag (class_key, id, /*globalize=*/false,
12738                        parser->num_template_parameter_lists);
12739     }
12740   else
12741     {
12742       tree class_type;
12743       bool pop_p = false;
12744
12745       /* Given:
12746
12747             template <typename T> struct S { struct T };
12748             template <typename T> struct S<T>::T { };
12749
12750          we will get a TYPENAME_TYPE when processing the definition of
12751          `S::T'.  We need to resolve it to the actual type before we
12752          try to define it.  */
12753       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12754         {
12755           class_type = resolve_typename_type (TREE_TYPE (type),
12756                                               /*only_current_p=*/false);
12757           if (class_type != error_mark_node)
12758             type = TYPE_NAME (class_type);
12759           else
12760             {
12761               cp_parser_error (parser, "could not resolve typename type");
12762               type = error_mark_node;
12763             }
12764         }
12765
12766       maybe_process_partial_specialization (TREE_TYPE (type));
12767       class_type = current_class_type;
12768       /* Enter the scope indicated by the nested-name-specifier.  */
12769       if (nested_name_specifier)
12770         pop_p = push_scope (nested_name_specifier);
12771       /* Get the canonical version of this type.  */
12772       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12773       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12774           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12775         type = push_template_decl (type);
12776       type = TREE_TYPE (type);
12777       if (nested_name_specifier)
12778         {
12779           *nested_name_specifier_p = true;
12780           if (pop_p)
12781             pop_scope (nested_name_specifier);
12782         }
12783     }
12784   /* Indicate whether this class was declared as a `class' or as a
12785      `struct'.  */
12786   if (TREE_CODE (type) == RECORD_TYPE)
12787     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12788   cp_parser_check_class_key (class_key, type);
12789
12790   /* Enter the scope containing the class; the names of base classes
12791      should be looked up in that context.  For example, given:
12792
12793        struct A { struct B {}; struct C; };
12794        struct A::C : B {};
12795
12796      is valid.  */
12797   if (nested_name_specifier)
12798     pop_p = push_scope (nested_name_specifier);
12799
12800   bases = NULL_TREE;
12801
12802   /* Get the list of base-classes, if there is one.  */
12803   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12804     bases = cp_parser_base_clause (parser);
12805
12806   /* Process the base classes.  */
12807   xref_basetypes (type, bases);
12808
12809   /* Leave the scope given by the nested-name-specifier.  We will
12810      enter the class scope itself while processing the members.  */
12811   if (pop_p)
12812     pop_scope (nested_name_specifier);
12813
12814  done:
12815   if (invalid_explicit_specialization_p)
12816     {
12817       end_specialization ();
12818       --parser->num_template_parameter_lists;
12819     }
12820   *attributes_p = attributes;
12821   return type;
12822 }
12823
12824 /* Parse a class-key.
12825
12826    class-key:
12827      class
12828      struct
12829      union
12830
12831    Returns the kind of class-key specified, or none_type to indicate
12832    error.  */
12833
12834 static enum tag_types
12835 cp_parser_class_key (cp_parser* parser)
12836 {
12837   cp_token *token;
12838   enum tag_types tag_type;
12839
12840   /* Look for the class-key.  */
12841   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12842   if (!token)
12843     return none_type;
12844
12845   /* Check to see if the TOKEN is a class-key.  */
12846   tag_type = cp_parser_token_is_class_key (token);
12847   if (!tag_type)
12848     cp_parser_error (parser, "expected class-key");
12849   return tag_type;
12850 }
12851
12852 /* Parse an (optional) member-specification.
12853
12854    member-specification:
12855      member-declaration member-specification [opt]
12856      access-specifier : member-specification [opt]  */
12857
12858 static void
12859 cp_parser_member_specification_opt (cp_parser* parser)
12860 {
12861   while (true)
12862     {
12863       cp_token *token;
12864       enum rid keyword;
12865
12866       /* Peek at the next token.  */
12867       token = cp_lexer_peek_token (parser->lexer);
12868       /* If it's a `}', or EOF then we've seen all the members.  */
12869       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12870         break;
12871
12872       /* See if this token is a keyword.  */
12873       keyword = token->keyword;
12874       switch (keyword)
12875         {
12876         case RID_PUBLIC:
12877         case RID_PROTECTED:
12878         case RID_PRIVATE:
12879           /* Consume the access-specifier.  */
12880           cp_lexer_consume_token (parser->lexer);
12881           /* Remember which access-specifier is active.  */
12882           current_access_specifier = token->value;
12883           /* Look for the `:'.  */
12884           cp_parser_require (parser, CPP_COLON, "`:'");
12885           break;
12886
12887         default:
12888           /* Accept #pragmas at class scope.  */
12889           if (token->type == CPP_PRAGMA)
12890             {
12891               cp_lexer_handle_pragma (parser->lexer);
12892               break;
12893             }
12894
12895           /* Otherwise, the next construction must be a
12896              member-declaration.  */
12897           cp_parser_member_declaration (parser);
12898         }
12899     }
12900 }
12901
12902 /* Parse a member-declaration.
12903
12904    member-declaration:
12905      decl-specifier-seq [opt] member-declarator-list [opt] ;
12906      function-definition ; [opt]
12907      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12908      using-declaration
12909      template-declaration
12910
12911    member-declarator-list:
12912      member-declarator
12913      member-declarator-list , member-declarator
12914
12915    member-declarator:
12916      declarator pure-specifier [opt]
12917      declarator constant-initializer [opt]
12918      identifier [opt] : constant-expression
12919
12920    GNU Extensions:
12921
12922    member-declaration:
12923      __extension__ member-declaration
12924
12925    member-declarator:
12926      declarator attributes [opt] pure-specifier [opt]
12927      declarator attributes [opt] constant-initializer [opt]
12928      identifier [opt] attributes [opt] : constant-expression  */
12929
12930 static void
12931 cp_parser_member_declaration (cp_parser* parser)
12932 {
12933   cp_decl_specifier_seq decl_specifiers;
12934   tree prefix_attributes;
12935   tree decl;
12936   int declares_class_or_enum;
12937   bool friend_p;
12938   cp_token *token;
12939   int saved_pedantic;
12940
12941   /* Check for the `__extension__' keyword.  */
12942   if (cp_parser_extension_opt (parser, &saved_pedantic))
12943     {
12944       /* Recurse.  */
12945       cp_parser_member_declaration (parser);
12946       /* Restore the old value of the PEDANTIC flag.  */
12947       pedantic = saved_pedantic;
12948
12949       return;
12950     }
12951
12952   /* Check for a template-declaration.  */
12953   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12954     {
12955       /* Parse the template-declaration.  */
12956       cp_parser_template_declaration (parser, /*member_p=*/true);
12957
12958       return;
12959     }
12960
12961   /* Check for a using-declaration.  */
12962   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12963     {
12964       /* Parse the using-declaration.  */
12965       cp_parser_using_declaration (parser);
12966
12967       return;
12968     }
12969
12970   /* Parse the decl-specifier-seq.  */
12971   cp_parser_decl_specifier_seq (parser,
12972                                 CP_PARSER_FLAGS_OPTIONAL,
12973                                 &decl_specifiers,
12974                                 &declares_class_or_enum);
12975   prefix_attributes = decl_specifiers.attributes;
12976   decl_specifiers.attributes = NULL_TREE;
12977   /* Check for an invalid type-name.  */
12978   if (!decl_specifiers.type
12979       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12980     return;
12981   /* If there is no declarator, then the decl-specifier-seq should
12982      specify a type.  */
12983   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12984     {
12985       /* If there was no decl-specifier-seq, and the next token is a
12986          `;', then we have something like:
12987
12988            struct S { ; };
12989
12990          [class.mem]
12991
12992          Each member-declaration shall declare at least one member
12993          name of the class.  */
12994       if (!decl_specifiers.any_specifiers_p)
12995         {
12996           cp_token *token = cp_lexer_peek_token (parser->lexer);
12997           if (pedantic && !token->in_system_header)
12998             pedwarn ("%Hextra %<;%>", &token->location);
12999         }
13000       else
13001         {
13002           tree type;
13003
13004           /* See if this declaration is a friend.  */
13005           friend_p = cp_parser_friend_p (&decl_specifiers);
13006           /* If there were decl-specifiers, check to see if there was
13007              a class-declaration.  */
13008           type = check_tag_decl (&decl_specifiers);
13009           /* Nested classes have already been added to the class, but
13010              a `friend' needs to be explicitly registered.  */
13011           if (friend_p)
13012             {
13013               /* If the `friend' keyword was present, the friend must
13014                  be introduced with a class-key.  */
13015                if (!declares_class_or_enum)
13016                  error ("a class-key must be used when declaring a friend");
13017                /* In this case:
13018
13019                     template <typename T> struct A {
13020                       friend struct A<T>::B;
13021                     };
13022
13023                   A<T>::B will be represented by a TYPENAME_TYPE, and
13024                   therefore not recognized by check_tag_decl.  */
13025                if (!type
13026                    && decl_specifiers.type
13027                    && TYPE_P (decl_specifiers.type))
13028                  type = decl_specifiers.type;
13029                if (!type || !TYPE_P (type))
13030                  error ("friend declaration does not name a class or "
13031                         "function");
13032                else
13033                  make_friend_class (current_class_type, type,
13034                                     /*complain=*/true);
13035             }
13036           /* If there is no TYPE, an error message will already have
13037              been issued.  */
13038           else if (!type || type == error_mark_node)
13039             ;
13040           /* An anonymous aggregate has to be handled specially; such
13041              a declaration really declares a data member (with a
13042              particular type), as opposed to a nested class.  */
13043           else if (ANON_AGGR_TYPE_P (type))
13044             {
13045               /* Remove constructors and such from TYPE, now that we
13046                  know it is an anonymous aggregate.  */
13047               fixup_anonymous_aggr (type);
13048               /* And make the corresponding data member.  */
13049               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13050               /* Add it to the class.  */
13051               finish_member_declaration (decl);
13052             }
13053           else
13054             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13055         }
13056     }
13057   else
13058     {
13059       /* See if these declarations will be friends.  */
13060       friend_p = cp_parser_friend_p (&decl_specifiers);
13061
13062       /* Keep going until we hit the `;' at the end of the
13063          declaration.  */
13064       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13065         {
13066           tree attributes = NULL_TREE;
13067           tree first_attribute;
13068
13069           /* Peek at the next token.  */
13070           token = cp_lexer_peek_token (parser->lexer);
13071
13072           /* Check for a bitfield declaration.  */
13073           if (token->type == CPP_COLON
13074               || (token->type == CPP_NAME
13075                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13076                   == CPP_COLON))
13077             {
13078               tree identifier;
13079               tree width;
13080
13081               /* Get the name of the bitfield.  Note that we cannot just
13082                  check TOKEN here because it may have been invalidated by
13083                  the call to cp_lexer_peek_nth_token above.  */
13084               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13085                 identifier = cp_parser_identifier (parser);
13086               else
13087                 identifier = NULL_TREE;
13088
13089               /* Consume the `:' token.  */
13090               cp_lexer_consume_token (parser->lexer);
13091               /* Get the width of the bitfield.  */
13092               width
13093                 = cp_parser_constant_expression (parser,
13094                                                  /*allow_non_constant=*/false,
13095                                                  NULL);
13096
13097               /* Look for attributes that apply to the bitfield.  */
13098               attributes = cp_parser_attributes_opt (parser);
13099               /* Remember which attributes are prefix attributes and
13100                  which are not.  */
13101               first_attribute = attributes;
13102               /* Combine the attributes.  */
13103               attributes = chainon (prefix_attributes, attributes);
13104
13105               /* Create the bitfield declaration.  */
13106               decl = grokbitfield (identifier
13107                                    ? make_id_declarator (identifier)
13108                                    : NULL,
13109                                    &decl_specifiers,
13110                                    width);
13111               /* Apply the attributes.  */
13112               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13113             }
13114           else
13115             {
13116               cp_declarator *declarator;
13117               tree initializer;
13118               tree asm_specification;
13119               int ctor_dtor_or_conv_p;
13120
13121               /* Parse the declarator.  */
13122               declarator
13123                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13124                                         &ctor_dtor_or_conv_p,
13125                                         /*parenthesized_p=*/NULL,
13126                                         /*member_p=*/true);
13127
13128               /* If something went wrong parsing the declarator, make sure
13129                  that we at least consume some tokens.  */
13130               if (declarator == cp_error_declarator)
13131                 {
13132                   /* Skip to the end of the statement.  */
13133                   cp_parser_skip_to_end_of_statement (parser);
13134                   /* If the next token is not a semicolon, that is
13135                      probably because we just skipped over the body of
13136                      a function.  So, we consume a semicolon if
13137                      present, but do not issue an error message if it
13138                      is not present.  */
13139                   if (cp_lexer_next_token_is (parser->lexer,
13140                                               CPP_SEMICOLON))
13141                     cp_lexer_consume_token (parser->lexer);
13142                   return;
13143                 }
13144
13145               cp_parser_check_for_definition_in_return_type
13146                 (declarator, declares_class_or_enum);
13147
13148               /* Look for an asm-specification.  */
13149               asm_specification = cp_parser_asm_specification_opt (parser);
13150               /* Look for attributes that apply to the declaration.  */
13151               attributes = cp_parser_attributes_opt (parser);
13152               /* Remember which attributes are prefix attributes and
13153                  which are not.  */
13154               first_attribute = attributes;
13155               /* Combine the attributes.  */
13156               attributes = chainon (prefix_attributes, attributes);
13157
13158               /* If it's an `=', then we have a constant-initializer or a
13159                  pure-specifier.  It is not correct to parse the
13160                  initializer before registering the member declaration
13161                  since the member declaration should be in scope while
13162                  its initializer is processed.  However, the rest of the
13163                  front end does not yet provide an interface that allows
13164                  us to handle this correctly.  */
13165               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13166                 {
13167                   /* In [class.mem]:
13168
13169                      A pure-specifier shall be used only in the declaration of
13170                      a virtual function.
13171
13172                      A member-declarator can contain a constant-initializer
13173                      only if it declares a static member of integral or
13174                      enumeration type.
13175
13176                      Therefore, if the DECLARATOR is for a function, we look
13177                      for a pure-specifier; otherwise, we look for a
13178                      constant-initializer.  When we call `grokfield', it will
13179                      perform more stringent semantics checks.  */
13180                   if (declarator->kind == cdk_function)
13181                     initializer = cp_parser_pure_specifier (parser);
13182                   else
13183                     /* Parse the initializer.  */
13184                     initializer = cp_parser_constant_initializer (parser);
13185                 }
13186               /* Otherwise, there is no initializer.  */
13187               else
13188                 initializer = NULL_TREE;
13189
13190               /* See if we are probably looking at a function
13191                  definition.  We are certainly not looking at at a
13192                  member-declarator.  Calling `grokfield' has
13193                  side-effects, so we must not do it unless we are sure
13194                  that we are looking at a member-declarator.  */
13195               if (cp_parser_token_starts_function_definition_p
13196                   (cp_lexer_peek_token (parser->lexer)))
13197                 {
13198                   /* The grammar does not allow a pure-specifier to be
13199                      used when a member function is defined.  (It is
13200                      possible that this fact is an oversight in the
13201                      standard, since a pure function may be defined
13202                      outside of the class-specifier.  */
13203                   if (initializer)
13204                     error ("pure-specifier on function-definition");
13205                   decl = cp_parser_save_member_function_body (parser,
13206                                                               &decl_specifiers,
13207                                                               declarator,
13208                                                               attributes);
13209                   /* If the member was not a friend, declare it here.  */
13210                   if (!friend_p)
13211                     finish_member_declaration (decl);
13212                   /* Peek at the next token.  */
13213                   token = cp_lexer_peek_token (parser->lexer);
13214                   /* If the next token is a semicolon, consume it.  */
13215                   if (token->type == CPP_SEMICOLON)
13216                     cp_lexer_consume_token (parser->lexer);
13217                   return;
13218                 }
13219               else
13220                 {
13221                   /* Create the declaration.  */
13222                   decl = grokfield (declarator, &decl_specifiers,
13223                                     initializer, asm_specification,
13224                                     attributes);
13225                   /* Any initialization must have been from a
13226                      constant-expression.  */
13227                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13228                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13229                 }
13230             }
13231
13232           /* Reset PREFIX_ATTRIBUTES.  */
13233           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13234             attributes = TREE_CHAIN (attributes);
13235           if (attributes)
13236             TREE_CHAIN (attributes) = NULL_TREE;
13237
13238           /* If there is any qualification still in effect, clear it
13239              now; we will be starting fresh with the next declarator.  */
13240           parser->scope = NULL_TREE;
13241           parser->qualifying_scope = NULL_TREE;
13242           parser->object_scope = NULL_TREE;
13243           /* If it's a `,', then there are more declarators.  */
13244           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13245             cp_lexer_consume_token (parser->lexer);
13246           /* If the next token isn't a `;', then we have a parse error.  */
13247           else if (cp_lexer_next_token_is_not (parser->lexer,
13248                                                CPP_SEMICOLON))
13249             {
13250               cp_parser_error (parser, "expected %<;%>");
13251               /* Skip tokens until we find a `;'.  */
13252               cp_parser_skip_to_end_of_statement (parser);
13253
13254               break;
13255             }
13256
13257           if (decl)
13258             {
13259               /* Add DECL to the list of members.  */
13260               if (!friend_p)
13261                 finish_member_declaration (decl);
13262
13263               if (TREE_CODE (decl) == FUNCTION_DECL)
13264                 cp_parser_save_default_args (parser, decl);
13265             }
13266         }
13267     }
13268
13269   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13270 }
13271
13272 /* Parse a pure-specifier.
13273
13274    pure-specifier:
13275      = 0
13276
13277    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13278    Otherwise, ERROR_MARK_NODE is returned.  */
13279
13280 static tree
13281 cp_parser_pure_specifier (cp_parser* parser)
13282 {
13283   cp_token *token;
13284
13285   /* Look for the `=' token.  */
13286   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13287     return error_mark_node;
13288   /* Look for the `0' token.  */
13289   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13290   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13291      to get information from the lexer about how the number was
13292      spelled in order to fix this problem.  */
13293   if (!token || !integer_zerop (token->value))
13294     return error_mark_node;
13295
13296   return integer_zero_node;
13297 }
13298
13299 /* Parse a constant-initializer.
13300
13301    constant-initializer:
13302      = constant-expression
13303
13304    Returns a representation of the constant-expression.  */
13305
13306 static tree
13307 cp_parser_constant_initializer (cp_parser* parser)
13308 {
13309   /* Look for the `=' token.  */
13310   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13311     return error_mark_node;
13312
13313   /* It is invalid to write:
13314
13315        struct S { static const int i = { 7 }; };
13316
13317      */
13318   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13319     {
13320       cp_parser_error (parser,
13321                        "a brace-enclosed initializer is not allowed here");
13322       /* Consume the opening brace.  */
13323       cp_lexer_consume_token (parser->lexer);
13324       /* Skip the initializer.  */
13325       cp_parser_skip_to_closing_brace (parser);
13326       /* Look for the trailing `}'.  */
13327       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13328
13329       return error_mark_node;
13330     }
13331
13332   return cp_parser_constant_expression (parser,
13333                                         /*allow_non_constant=*/false,
13334                                         NULL);
13335 }
13336
13337 /* Derived classes [gram.class.derived] */
13338
13339 /* Parse a base-clause.
13340
13341    base-clause:
13342      : base-specifier-list
13343
13344    base-specifier-list:
13345      base-specifier
13346      base-specifier-list , base-specifier
13347
13348    Returns a TREE_LIST representing the base-classes, in the order in
13349    which they were declared.  The representation of each node is as
13350    described by cp_parser_base_specifier.
13351
13352    In the case that no bases are specified, this function will return
13353    NULL_TREE, not ERROR_MARK_NODE.  */
13354
13355 static tree
13356 cp_parser_base_clause (cp_parser* parser)
13357 {
13358   tree bases = NULL_TREE;
13359
13360   /* Look for the `:' that begins the list.  */
13361   cp_parser_require (parser, CPP_COLON, "`:'");
13362
13363   /* Scan the base-specifier-list.  */
13364   while (true)
13365     {
13366       cp_token *token;
13367       tree base;
13368
13369       /* Look for the base-specifier.  */
13370       base = cp_parser_base_specifier (parser);
13371       /* Add BASE to the front of the list.  */
13372       if (base != error_mark_node)
13373         {
13374           TREE_CHAIN (base) = bases;
13375           bases = base;
13376         }
13377       /* Peek at the next token.  */
13378       token = cp_lexer_peek_token (parser->lexer);
13379       /* If it's not a comma, then the list is complete.  */
13380       if (token->type != CPP_COMMA)
13381         break;
13382       /* Consume the `,'.  */
13383       cp_lexer_consume_token (parser->lexer);
13384     }
13385
13386   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13387      base class had a qualified name.  However, the next name that
13388      appears is certainly not qualified.  */
13389   parser->scope = NULL_TREE;
13390   parser->qualifying_scope = NULL_TREE;
13391   parser->object_scope = NULL_TREE;
13392
13393   return nreverse (bases);
13394 }
13395
13396 /* Parse a base-specifier.
13397
13398    base-specifier:
13399      :: [opt] nested-name-specifier [opt] class-name
13400      virtual access-specifier [opt] :: [opt] nested-name-specifier
13401        [opt] class-name
13402      access-specifier virtual [opt] :: [opt] nested-name-specifier
13403        [opt] class-name
13404
13405    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13406    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13407    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13408    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13409
13410 static tree
13411 cp_parser_base_specifier (cp_parser* parser)
13412 {
13413   cp_token *token;
13414   bool done = false;
13415   bool virtual_p = false;
13416   bool duplicate_virtual_error_issued_p = false;
13417   bool duplicate_access_error_issued_p = false;
13418   bool class_scope_p, template_p;
13419   tree access = access_default_node;
13420   tree type;
13421
13422   /* Process the optional `virtual' and `access-specifier'.  */
13423   while (!done)
13424     {
13425       /* Peek at the next token.  */
13426       token = cp_lexer_peek_token (parser->lexer);
13427       /* Process `virtual'.  */
13428       switch (token->keyword)
13429         {
13430         case RID_VIRTUAL:
13431           /* If `virtual' appears more than once, issue an error.  */
13432           if (virtual_p && !duplicate_virtual_error_issued_p)
13433             {
13434               cp_parser_error (parser,
13435                                "%<virtual%> specified more than once in base-specified");
13436               duplicate_virtual_error_issued_p = true;
13437             }
13438
13439           virtual_p = true;
13440
13441           /* Consume the `virtual' token.  */
13442           cp_lexer_consume_token (parser->lexer);
13443
13444           break;
13445
13446         case RID_PUBLIC:
13447         case RID_PROTECTED:
13448         case RID_PRIVATE:
13449           /* If more than one access specifier appears, issue an
13450              error.  */
13451           if (access != access_default_node
13452               && !duplicate_access_error_issued_p)
13453             {
13454               cp_parser_error (parser,
13455                                "more than one access specifier in base-specified");
13456               duplicate_access_error_issued_p = true;
13457             }
13458
13459           access = ridpointers[(int) token->keyword];
13460
13461           /* Consume the access-specifier.  */
13462           cp_lexer_consume_token (parser->lexer);
13463
13464           break;
13465
13466         default:
13467           done = true;
13468           break;
13469         }
13470     }
13471   /* It is not uncommon to see programs mechanically, erroneously, use
13472      the 'typename' keyword to denote (dependent) qualified types
13473      as base classes.  */
13474   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13475     {
13476       if (!processing_template_decl)
13477         error ("keyword %<typename%> not allowed outside of templates");
13478       else
13479         error ("keyword %<typename%> not allowed in this context "
13480                "(the base class is implicitly a type)");
13481       cp_lexer_consume_token (parser->lexer);
13482     }
13483
13484   /* Look for the optional `::' operator.  */
13485   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13486   /* Look for the nested-name-specifier.  The simplest way to
13487      implement:
13488
13489        [temp.res]
13490
13491        The keyword `typename' is not permitted in a base-specifier or
13492        mem-initializer; in these contexts a qualified name that
13493        depends on a template-parameter is implicitly assumed to be a
13494        type name.
13495
13496      is to pretend that we have seen the `typename' keyword at this
13497      point.  */
13498   cp_parser_nested_name_specifier_opt (parser,
13499                                        /*typename_keyword_p=*/true,
13500                                        /*check_dependency_p=*/true,
13501                                        /*type_p=*/true,
13502                                        /*is_declaration=*/true);
13503   /* If the base class is given by a qualified name, assume that names
13504      we see are type names or templates, as appropriate.  */
13505   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13506   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13507
13508   /* Finally, look for the class-name.  */
13509   type = cp_parser_class_name (parser,
13510                                class_scope_p,
13511                                template_p,
13512                                /*type_p=*/true,
13513                                /*check_dependency_p=*/true,
13514                                /*class_head_p=*/false,
13515                                /*is_declaration=*/true);
13516
13517   if (type == error_mark_node)
13518     return error_mark_node;
13519
13520   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13521 }
13522
13523 /* Exception handling [gram.exception] */
13524
13525 /* Parse an (optional) exception-specification.
13526
13527    exception-specification:
13528      throw ( type-id-list [opt] )
13529
13530    Returns a TREE_LIST representing the exception-specification.  The
13531    TREE_VALUE of each node is a type.  */
13532
13533 static tree
13534 cp_parser_exception_specification_opt (cp_parser* parser)
13535 {
13536   cp_token *token;
13537   tree type_id_list;
13538
13539   /* Peek at the next token.  */
13540   token = cp_lexer_peek_token (parser->lexer);
13541   /* If it's not `throw', then there's no exception-specification.  */
13542   if (!cp_parser_is_keyword (token, RID_THROW))
13543     return NULL_TREE;
13544
13545   /* Consume the `throw'.  */
13546   cp_lexer_consume_token (parser->lexer);
13547
13548   /* Look for the `('.  */
13549   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13550
13551   /* Peek at the next token.  */
13552   token = cp_lexer_peek_token (parser->lexer);
13553   /* If it's not a `)', then there is a type-id-list.  */
13554   if (token->type != CPP_CLOSE_PAREN)
13555     {
13556       const char *saved_message;
13557
13558       /* Types may not be defined in an exception-specification.  */
13559       saved_message = parser->type_definition_forbidden_message;
13560       parser->type_definition_forbidden_message
13561         = "types may not be defined in an exception-specification";
13562       /* Parse the type-id-list.  */
13563       type_id_list = cp_parser_type_id_list (parser);
13564       /* Restore the saved message.  */
13565       parser->type_definition_forbidden_message = saved_message;
13566     }
13567   else
13568     type_id_list = empty_except_spec;
13569
13570   /* Look for the `)'.  */
13571   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13572
13573   return type_id_list;
13574 }
13575
13576 /* Parse an (optional) type-id-list.
13577
13578    type-id-list:
13579      type-id
13580      type-id-list , type-id
13581
13582    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13583    in the order that the types were presented.  */
13584
13585 static tree
13586 cp_parser_type_id_list (cp_parser* parser)
13587 {
13588   tree types = NULL_TREE;
13589
13590   while (true)
13591     {
13592       cp_token *token;
13593       tree type;
13594
13595       /* Get the next type-id.  */
13596       type = cp_parser_type_id (parser);
13597       /* Add it to the list.  */
13598       types = add_exception_specifier (types, type, /*complain=*/1);
13599       /* Peek at the next token.  */
13600       token = cp_lexer_peek_token (parser->lexer);
13601       /* If it is not a `,', we are done.  */
13602       if (token->type != CPP_COMMA)
13603         break;
13604       /* Consume the `,'.  */
13605       cp_lexer_consume_token (parser->lexer);
13606     }
13607
13608   return nreverse (types);
13609 }
13610
13611 /* Parse a try-block.
13612
13613    try-block:
13614      try compound-statement handler-seq  */
13615
13616 static tree
13617 cp_parser_try_block (cp_parser* parser)
13618 {
13619   tree try_block;
13620
13621   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13622   try_block = begin_try_block ();
13623   cp_parser_compound_statement (parser, NULL, true);
13624   finish_try_block (try_block);
13625   cp_parser_handler_seq (parser);
13626   finish_handler_sequence (try_block);
13627
13628   return try_block;
13629 }
13630
13631 /* Parse a function-try-block.
13632
13633    function-try-block:
13634      try ctor-initializer [opt] function-body handler-seq  */
13635
13636 static bool
13637 cp_parser_function_try_block (cp_parser* parser)
13638 {
13639   tree try_block;
13640   bool ctor_initializer_p;
13641
13642   /* Look for the `try' keyword.  */
13643   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13644     return false;
13645   /* Let the rest of the front-end know where we are.  */
13646   try_block = begin_function_try_block ();
13647   /* Parse the function-body.  */
13648   ctor_initializer_p
13649     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13650   /* We're done with the `try' part.  */
13651   finish_function_try_block (try_block);
13652   /* Parse the handlers.  */
13653   cp_parser_handler_seq (parser);
13654   /* We're done with the handlers.  */
13655   finish_function_handler_sequence (try_block);
13656
13657   return ctor_initializer_p;
13658 }
13659
13660 /* Parse a handler-seq.
13661
13662    handler-seq:
13663      handler handler-seq [opt]  */
13664
13665 static void
13666 cp_parser_handler_seq (cp_parser* parser)
13667 {
13668   while (true)
13669     {
13670       cp_token *token;
13671
13672       /* Parse the handler.  */
13673       cp_parser_handler (parser);
13674       /* Peek at the next token.  */
13675       token = cp_lexer_peek_token (parser->lexer);
13676       /* If it's not `catch' then there are no more handlers.  */
13677       if (!cp_parser_is_keyword (token, RID_CATCH))
13678         break;
13679     }
13680 }
13681
13682 /* Parse a handler.
13683
13684    handler:
13685      catch ( exception-declaration ) compound-statement  */
13686
13687 static void
13688 cp_parser_handler (cp_parser* parser)
13689 {
13690   tree handler;
13691   tree declaration;
13692
13693   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13694   handler = begin_handler ();
13695   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13696   declaration = cp_parser_exception_declaration (parser);
13697   finish_handler_parms (declaration, handler);
13698   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13699   cp_parser_compound_statement (parser, NULL, false);
13700   finish_handler (handler);
13701 }
13702
13703 /* Parse an exception-declaration.
13704
13705    exception-declaration:
13706      type-specifier-seq declarator
13707      type-specifier-seq abstract-declarator
13708      type-specifier-seq
13709      ...
13710
13711    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13712    ellipsis variant is used.  */
13713
13714 static tree
13715 cp_parser_exception_declaration (cp_parser* parser)
13716 {
13717   tree decl;
13718   cp_decl_specifier_seq type_specifiers;
13719   cp_declarator *declarator;
13720   const char *saved_message;
13721
13722   /* If it's an ellipsis, it's easy to handle.  */
13723   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13724     {
13725       /* Consume the `...' token.  */
13726       cp_lexer_consume_token (parser->lexer);
13727       return NULL_TREE;
13728     }
13729
13730   /* Types may not be defined in exception-declarations.  */
13731   saved_message = parser->type_definition_forbidden_message;
13732   parser->type_definition_forbidden_message
13733     = "types may not be defined in exception-declarations";
13734
13735   /* Parse the type-specifier-seq.  */
13736   cp_parser_type_specifier_seq (parser, &type_specifiers);
13737   /* If it's a `)', then there is no declarator.  */
13738   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13739     declarator = NULL;
13740   else
13741     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13742                                        /*ctor_dtor_or_conv_p=*/NULL,
13743                                        /*parenthesized_p=*/NULL,
13744                                        /*member_p=*/false);
13745
13746   /* Restore the saved message.  */
13747   parser->type_definition_forbidden_message = saved_message;
13748
13749   if (type_specifiers.any_specifiers_p)
13750     {
13751       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13752       if (decl == NULL_TREE)
13753         error ("invalid catch parameter");
13754     }
13755   else
13756     decl = NULL_TREE;
13757
13758   return decl;
13759 }
13760
13761 /* Parse a throw-expression.
13762
13763    throw-expression:
13764      throw assignment-expression [opt]
13765
13766    Returns a THROW_EXPR representing the throw-expression.  */
13767
13768 static tree
13769 cp_parser_throw_expression (cp_parser* parser)
13770 {
13771   tree expression;
13772   cp_token* token;
13773
13774   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13775   token = cp_lexer_peek_token (parser->lexer);
13776   /* Figure out whether or not there is an assignment-expression
13777      following the "throw" keyword.  */
13778   if (token->type == CPP_COMMA
13779       || token->type == CPP_SEMICOLON
13780       || token->type == CPP_CLOSE_PAREN
13781       || token->type == CPP_CLOSE_SQUARE
13782       || token->type == CPP_CLOSE_BRACE
13783       || token->type == CPP_COLON)
13784     expression = NULL_TREE;
13785   else
13786     expression = cp_parser_assignment_expression (parser);
13787
13788   return build_throw (expression);
13789 }
13790
13791 /* GNU Extensions */
13792
13793 /* Parse an (optional) asm-specification.
13794
13795    asm-specification:
13796      asm ( string-literal )
13797
13798    If the asm-specification is present, returns a STRING_CST
13799    corresponding to the string-literal.  Otherwise, returns
13800    NULL_TREE.  */
13801
13802 static tree
13803 cp_parser_asm_specification_opt (cp_parser* parser)
13804 {
13805   cp_token *token;
13806   tree asm_specification;
13807
13808   /* Peek at the next token.  */
13809   token = cp_lexer_peek_token (parser->lexer);
13810   /* If the next token isn't the `asm' keyword, then there's no
13811      asm-specification.  */
13812   if (!cp_parser_is_keyword (token, RID_ASM))
13813     return NULL_TREE;
13814
13815   /* Consume the `asm' token.  */
13816   cp_lexer_consume_token (parser->lexer);
13817   /* Look for the `('.  */
13818   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13819
13820   /* Look for the string-literal.  */
13821   asm_specification = cp_parser_string_literal (parser, false, false);
13822
13823   /* Look for the `)'.  */
13824   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13825
13826   return asm_specification;
13827 }
13828
13829 /* Parse an asm-operand-list.
13830
13831    asm-operand-list:
13832      asm-operand
13833      asm-operand-list , asm-operand
13834
13835    asm-operand:
13836      string-literal ( expression )
13837      [ string-literal ] string-literal ( expression )
13838
13839    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13840    each node is the expression.  The TREE_PURPOSE is itself a
13841    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13842    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13843    is a STRING_CST for the string literal before the parenthesis.  */
13844
13845 static tree
13846 cp_parser_asm_operand_list (cp_parser* parser)
13847 {
13848   tree asm_operands = NULL_TREE;
13849
13850   while (true)
13851     {
13852       tree string_literal;
13853       tree expression;
13854       tree name;
13855
13856       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13857         {
13858           /* Consume the `[' token.  */
13859           cp_lexer_consume_token (parser->lexer);
13860           /* Read the operand name.  */
13861           name = cp_parser_identifier (parser);
13862           if (name != error_mark_node)
13863             name = build_string (IDENTIFIER_LENGTH (name),
13864                                  IDENTIFIER_POINTER (name));
13865           /* Look for the closing `]'.  */
13866           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13867         }
13868       else
13869         name = NULL_TREE;
13870       /* Look for the string-literal.  */
13871       string_literal = cp_parser_string_literal (parser, false, false);
13872
13873       /* Look for the `('.  */
13874       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13875       /* Parse the expression.  */
13876       expression = cp_parser_expression (parser);
13877       /* Look for the `)'.  */
13878       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13879
13880       /* Add this operand to the list.  */
13881       asm_operands = tree_cons (build_tree_list (name, string_literal),
13882                                 expression,
13883                                 asm_operands);
13884       /* If the next token is not a `,', there are no more
13885          operands.  */
13886       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13887         break;
13888       /* Consume the `,'.  */
13889       cp_lexer_consume_token (parser->lexer);
13890     }
13891
13892   return nreverse (asm_operands);
13893 }
13894
13895 /* Parse an asm-clobber-list.
13896
13897    asm-clobber-list:
13898      string-literal
13899      asm-clobber-list , string-literal
13900
13901    Returns a TREE_LIST, indicating the clobbers in the order that they
13902    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13903
13904 static tree
13905 cp_parser_asm_clobber_list (cp_parser* parser)
13906 {
13907   tree clobbers = NULL_TREE;
13908
13909   while (true)
13910     {
13911       tree string_literal;
13912
13913       /* Look for the string literal.  */
13914       string_literal = cp_parser_string_literal (parser, false, false);
13915       /* Add it to the list.  */
13916       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13917       /* If the next token is not a `,', then the list is
13918          complete.  */
13919       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13920         break;
13921       /* Consume the `,' token.  */
13922       cp_lexer_consume_token (parser->lexer);
13923     }
13924
13925   return clobbers;
13926 }
13927
13928 /* Parse an (optional) series of attributes.
13929
13930    attributes:
13931      attributes attribute
13932
13933    attribute:
13934      __attribute__ (( attribute-list [opt] ))
13935
13936    The return value is as for cp_parser_attribute_list.  */
13937
13938 static tree
13939 cp_parser_attributes_opt (cp_parser* parser)
13940 {
13941   tree attributes = NULL_TREE;
13942
13943   while (true)
13944     {
13945       cp_token *token;
13946       tree attribute_list;
13947
13948       /* Peek at the next token.  */
13949       token = cp_lexer_peek_token (parser->lexer);
13950       /* If it's not `__attribute__', then we're done.  */
13951       if (token->keyword != RID_ATTRIBUTE)
13952         break;
13953
13954       /* Consume the `__attribute__' keyword.  */
13955       cp_lexer_consume_token (parser->lexer);
13956       /* Look for the two `(' tokens.  */
13957       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13958       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13959
13960       /* Peek at the next token.  */
13961       token = cp_lexer_peek_token (parser->lexer);
13962       if (token->type != CPP_CLOSE_PAREN)
13963         /* Parse the attribute-list.  */
13964         attribute_list = cp_parser_attribute_list (parser);
13965       else
13966         /* If the next token is a `)', then there is no attribute
13967            list.  */
13968         attribute_list = NULL;
13969
13970       /* Look for the two `)' tokens.  */
13971       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13972       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13973
13974       /* Add these new attributes to the list.  */
13975       attributes = chainon (attributes, attribute_list);
13976     }
13977
13978   return attributes;
13979 }
13980
13981 /* Parse an attribute-list.
13982
13983    attribute-list:
13984      attribute
13985      attribute-list , attribute
13986
13987    attribute:
13988      identifier
13989      identifier ( identifier )
13990      identifier ( identifier , expression-list )
13991      identifier ( expression-list )
13992
13993    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13994    TREE_PURPOSE of each node is the identifier indicating which
13995    attribute is in use.  The TREE_VALUE represents the arguments, if
13996    any.  */
13997
13998 static tree
13999 cp_parser_attribute_list (cp_parser* parser)
14000 {
14001   tree attribute_list = NULL_TREE;
14002   bool save_translate_strings_p = parser->translate_strings_p;
14003
14004   parser->translate_strings_p = false;
14005   while (true)
14006     {
14007       cp_token *token;
14008       tree identifier;
14009       tree attribute;
14010
14011       /* Look for the identifier.  We also allow keywords here; for
14012          example `__attribute__ ((const))' is legal.  */
14013       token = cp_lexer_peek_token (parser->lexer);
14014       if (token->type != CPP_NAME
14015           && token->type != CPP_KEYWORD)
14016         return error_mark_node;
14017       /* Consume the token.  */
14018       token = cp_lexer_consume_token (parser->lexer);
14019
14020       /* Save away the identifier that indicates which attribute this is.  */
14021       identifier = token->value;
14022       attribute = build_tree_list (identifier, NULL_TREE);
14023
14024       /* Peek at the next token.  */
14025       token = cp_lexer_peek_token (parser->lexer);
14026       /* If it's an `(', then parse the attribute arguments.  */
14027       if (token->type == CPP_OPEN_PAREN)
14028         {
14029           tree arguments;
14030
14031           arguments = (cp_parser_parenthesized_expression_list
14032                        (parser, true, /*non_constant_p=*/NULL));
14033           /* Save the identifier and arguments away.  */
14034           TREE_VALUE (attribute) = arguments;
14035         }
14036
14037       /* Add this attribute to the list.  */
14038       TREE_CHAIN (attribute) = attribute_list;
14039       attribute_list = attribute;
14040
14041       /* Now, look for more attributes.  */
14042       token = cp_lexer_peek_token (parser->lexer);
14043       /* If the next token isn't a `,', we're done.  */
14044       if (token->type != CPP_COMMA)
14045         break;
14046
14047       /* Consume the comma and keep going.  */
14048       cp_lexer_consume_token (parser->lexer);
14049     }
14050   parser->translate_strings_p = save_translate_strings_p;
14051
14052   /* We built up the list in reverse order.  */
14053   return nreverse (attribute_list);
14054 }
14055
14056 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14057    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14058    current value of the PEDANTIC flag, regardless of whether or not
14059    the `__extension__' keyword is present.  The caller is responsible
14060    for restoring the value of the PEDANTIC flag.  */
14061
14062 static bool
14063 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14064 {
14065   /* Save the old value of the PEDANTIC flag.  */
14066   *saved_pedantic = pedantic;
14067
14068   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14069     {
14070       /* Consume the `__extension__' token.  */
14071       cp_lexer_consume_token (parser->lexer);
14072       /* We're not being pedantic while the `__extension__' keyword is
14073          in effect.  */
14074       pedantic = 0;
14075
14076       return true;
14077     }
14078
14079   return false;
14080 }
14081
14082 /* Parse a label declaration.
14083
14084    label-declaration:
14085      __label__ label-declarator-seq ;
14086
14087    label-declarator-seq:
14088      identifier , label-declarator-seq
14089      identifier  */
14090
14091 static void
14092 cp_parser_label_declaration (cp_parser* parser)
14093 {
14094   /* Look for the `__label__' keyword.  */
14095   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14096
14097   while (true)
14098     {
14099       tree identifier;
14100
14101       /* Look for an identifier.  */
14102       identifier = cp_parser_identifier (parser);
14103       /* Declare it as a lobel.  */
14104       finish_label_decl (identifier);
14105       /* If the next token is a `;', stop.  */
14106       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14107         break;
14108       /* Look for the `,' separating the label declarations.  */
14109       cp_parser_require (parser, CPP_COMMA, "`,'");
14110     }
14111
14112   /* Look for the final `;'.  */
14113   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14114 }
14115
14116 /* Support Functions */
14117
14118 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14119    NAME should have one of the representations used for an
14120    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14121    is returned.  If PARSER->SCOPE is a dependent type, then a
14122    SCOPE_REF is returned.
14123
14124    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14125    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14126    was formed.  Abstractly, such entities should not be passed to this
14127    function, because they do not need to be looked up, but it is
14128    simpler to check for this special case here, rather than at the
14129    call-sites.
14130
14131    In cases not explicitly covered above, this function returns a
14132    DECL, OVERLOAD, or baselink representing the result of the lookup.
14133    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14134    is returned.
14135
14136    If IS_TYPE is TRUE, bindings that do not refer to types are
14137    ignored.
14138
14139    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14140    ignored.
14141
14142    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14143    are ignored.
14144
14145    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14146    types.  
14147
14148    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14149    results in an ambiguity, and false otherwise.  */
14150
14151 static tree
14152 cp_parser_lookup_name (cp_parser *parser, tree name,
14153                        bool is_type, bool is_template, bool is_namespace,
14154                        bool check_dependency,
14155                        bool *ambiguous_p)
14156 {
14157   tree decl;
14158   tree object_type = parser->context->object_type;
14159
14160   /* Assume that the lookup will be unambiguous.  */
14161   if (ambiguous_p)
14162     *ambiguous_p = false;
14163
14164   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14165      no longer valid.  Note that if we are parsing tentatively, and
14166      the parse fails, OBJECT_TYPE will be automatically restored.  */
14167   parser->context->object_type = NULL_TREE;
14168
14169   if (name == error_mark_node)
14170     return error_mark_node;
14171
14172   /* A template-id has already been resolved; there is no lookup to
14173      do.  */
14174   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14175     return name;
14176   if (BASELINK_P (name))
14177     {
14178       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14179                   == TEMPLATE_ID_EXPR);
14180       return name;
14181     }
14182
14183   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14184      it should already have been checked to make sure that the name
14185      used matches the type being destroyed.  */
14186   if (TREE_CODE (name) == BIT_NOT_EXPR)
14187     {
14188       tree type;
14189
14190       /* Figure out to which type this destructor applies.  */
14191       if (parser->scope)
14192         type = parser->scope;
14193       else if (object_type)
14194         type = object_type;
14195       else
14196         type = current_class_type;
14197       /* If that's not a class type, there is no destructor.  */
14198       if (!type || !CLASS_TYPE_P (type))
14199         return error_mark_node;
14200       if (!CLASSTYPE_DESTRUCTORS (type))
14201           return error_mark_node;
14202       /* If it was a class type, return the destructor.  */
14203       return CLASSTYPE_DESTRUCTORS (type);
14204     }
14205
14206   /* By this point, the NAME should be an ordinary identifier.  If
14207      the id-expression was a qualified name, the qualifying scope is
14208      stored in PARSER->SCOPE at this point.  */
14209   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14210
14211   /* Perform the lookup.  */
14212   if (parser->scope)
14213     {
14214       bool dependent_p;
14215
14216       if (parser->scope == error_mark_node)
14217         return error_mark_node;
14218
14219       /* If the SCOPE is dependent, the lookup must be deferred until
14220          the template is instantiated -- unless we are explicitly
14221          looking up names in uninstantiated templates.  Even then, we
14222          cannot look up the name if the scope is not a class type; it
14223          might, for example, be a template type parameter.  */
14224       dependent_p = (TYPE_P (parser->scope)
14225                      && !(parser->in_declarator_p
14226                           && currently_open_class (parser->scope))
14227                      && dependent_type_p (parser->scope));
14228       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14229            && dependent_p)
14230         {
14231           if (is_type)
14232             /* The resolution to Core Issue 180 says that `struct A::B'
14233                should be considered a type-name, even if `A' is
14234                dependent.  */
14235             decl = TYPE_NAME (make_typename_type (parser->scope,
14236                                                   name,
14237                                                   /*complain=*/1));
14238           else if (is_template)
14239             decl = make_unbound_class_template (parser->scope,
14240                                                 name,
14241                                                 /*complain=*/1);
14242           else
14243             decl = build_nt (SCOPE_REF, parser->scope, name);
14244         }
14245       else
14246         {
14247           bool pop_p = false;
14248
14249           /* If PARSER->SCOPE is a dependent type, then it must be a
14250              class type, and we must not be checking dependencies;
14251              otherwise, we would have processed this lookup above.  So
14252              that PARSER->SCOPE is not considered a dependent base by
14253              lookup_member, we must enter the scope here.  */
14254           if (dependent_p)
14255             pop_p = push_scope (parser->scope);
14256           /* If the PARSER->SCOPE is a a template specialization, it
14257              may be instantiated during name lookup.  In that case,
14258              errors may be issued.  Even if we rollback the current
14259              tentative parse, those errors are valid.  */
14260           decl = lookup_qualified_name (parser->scope, name, is_type,
14261                                         /*complain=*/true);
14262           if (pop_p)
14263             pop_scope (parser->scope);
14264         }
14265       parser->qualifying_scope = parser->scope;
14266       parser->object_scope = NULL_TREE;
14267     }
14268   else if (object_type)
14269     {
14270       tree object_decl = NULL_TREE;
14271       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14272          OBJECT_TYPE is not a class.  */
14273       if (CLASS_TYPE_P (object_type))
14274         /* If the OBJECT_TYPE is a template specialization, it may
14275            be instantiated during name lookup.  In that case, errors
14276            may be issued.  Even if we rollback the current tentative
14277            parse, those errors are valid.  */
14278         object_decl = lookup_member (object_type,
14279                                      name,
14280                                      /*protect=*/0, is_type);
14281       /* Look it up in the enclosing context, too.  */
14282       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14283                                /*block_p=*/true, is_namespace,
14284                                /*flags=*/0);
14285       parser->object_scope = object_type;
14286       parser->qualifying_scope = NULL_TREE;
14287       if (object_decl)
14288         decl = object_decl;
14289     }
14290   else
14291     {
14292       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14293                                /*block_p=*/true, is_namespace,
14294                                /*flags=*/0);
14295       parser->qualifying_scope = NULL_TREE;
14296       parser->object_scope = NULL_TREE;
14297     }
14298
14299   /* If the lookup failed, let our caller know.  */
14300   if (!decl
14301       || decl == error_mark_node
14302       || (TREE_CODE (decl) == FUNCTION_DECL
14303           && DECL_ANTICIPATED (decl)))
14304     return error_mark_node;
14305
14306   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14307   if (TREE_CODE (decl) == TREE_LIST)
14308     {
14309       if (ambiguous_p)
14310         *ambiguous_p = true;
14311       /* The error message we have to print is too complicated for
14312          cp_parser_error, so we incorporate its actions directly.  */
14313       if (!cp_parser_simulate_error (parser))
14314         {
14315           error ("reference to %qD is ambiguous", name);
14316           print_candidates (decl);
14317         }
14318       return error_mark_node;
14319     }
14320
14321   gcc_assert (DECL_P (decl)
14322               || TREE_CODE (decl) == OVERLOAD
14323               || TREE_CODE (decl) == SCOPE_REF
14324               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14325               || BASELINK_P (decl));
14326
14327   /* If we have resolved the name of a member declaration, check to
14328      see if the declaration is accessible.  When the name resolves to
14329      set of overloaded functions, accessibility is checked when
14330      overload resolution is done.
14331
14332      During an explicit instantiation, access is not checked at all,
14333      as per [temp.explicit].  */
14334   if (DECL_P (decl))
14335     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14336
14337   return decl;
14338 }
14339
14340 /* Like cp_parser_lookup_name, but for use in the typical case where
14341    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14342    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14343
14344 static tree
14345 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14346 {
14347   return cp_parser_lookup_name (parser, name,
14348                                 /*is_type=*/false,
14349                                 /*is_template=*/false,
14350                                 /*is_namespace=*/false,
14351                                 /*check_dependency=*/true,
14352                                 /*ambiguous_p=*/NULL);
14353 }
14354
14355 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14356    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14357    true, the DECL indicates the class being defined in a class-head,
14358    or declared in an elaborated-type-specifier.
14359
14360    Otherwise, return DECL.  */
14361
14362 static tree
14363 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14364 {
14365   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14366      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14367
14368        struct A {
14369          template <typename T> struct B;
14370        };
14371
14372        template <typename T> struct A::B {};
14373
14374      Similarly, in a elaborated-type-specifier:
14375
14376        namespace N { struct X{}; }
14377
14378        struct A {
14379          template <typename T> friend struct N::X;
14380        };
14381
14382      However, if the DECL refers to a class type, and we are in
14383      the scope of the class, then the name lookup automatically
14384      finds the TYPE_DECL created by build_self_reference rather
14385      than a TEMPLATE_DECL.  For example, in:
14386
14387        template <class T> struct S {
14388          S s;
14389        };
14390
14391      there is no need to handle such case.  */
14392
14393   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14394     return DECL_TEMPLATE_RESULT (decl);
14395
14396   return decl;
14397 }
14398
14399 /* If too many, or too few, template-parameter lists apply to the
14400    declarator, issue an error message.  Returns TRUE if all went well,
14401    and FALSE otherwise.  */
14402
14403 static bool
14404 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14405                                                 cp_declarator *declarator)
14406 {
14407   unsigned num_templates;
14408
14409   /* We haven't seen any classes that involve template parameters yet.  */
14410   num_templates = 0;
14411
14412   switch (declarator->kind)
14413     {
14414     case cdk_id:
14415       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14416         {
14417           tree scope;
14418           tree member;
14419
14420           scope = TREE_OPERAND (declarator->u.id.name, 0);
14421           member = TREE_OPERAND (declarator->u.id.name, 1);
14422
14423           while (scope && CLASS_TYPE_P (scope))
14424             {
14425               /* You're supposed to have one `template <...>'
14426                  for every template class, but you don't need one
14427                  for a full specialization.  For example:
14428
14429                  template <class T> struct S{};
14430                  template <> struct S<int> { void f(); };
14431                  void S<int>::f () {}
14432
14433                  is correct; there shouldn't be a `template <>' for
14434                  the definition of `S<int>::f'.  */
14435               if (CLASSTYPE_TEMPLATE_INFO (scope)
14436                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14437                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14438                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14439                 ++num_templates;
14440
14441               scope = TYPE_CONTEXT (scope);
14442             }
14443         }
14444
14445       /* If the DECLARATOR has the form `X<y>' then it uses one
14446          additional level of template parameters.  */
14447       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14448         ++num_templates;
14449
14450       return cp_parser_check_template_parameters (parser,
14451                                                   num_templates);
14452
14453     case cdk_function:
14454     case cdk_array:
14455     case cdk_pointer:
14456     case cdk_reference:
14457     case cdk_ptrmem:
14458       return (cp_parser_check_declarator_template_parameters
14459               (parser, declarator->declarator));
14460
14461     case cdk_error:
14462       return true;
14463
14464     default:
14465       gcc_unreachable ();
14466     }
14467   return false;
14468 }
14469
14470 /* NUM_TEMPLATES were used in the current declaration.  If that is
14471    invalid, return FALSE and issue an error messages.  Otherwise,
14472    return TRUE.  */
14473
14474 static bool
14475 cp_parser_check_template_parameters (cp_parser* parser,
14476                                      unsigned num_templates)
14477 {
14478   /* If there are more template classes than parameter lists, we have
14479      something like:
14480
14481        template <class T> void S<T>::R<T>::f ();  */
14482   if (parser->num_template_parameter_lists < num_templates)
14483     {
14484       error ("too few template-parameter-lists");
14485       return false;
14486     }
14487   /* If there are the same number of template classes and parameter
14488      lists, that's OK.  */
14489   if (parser->num_template_parameter_lists == num_templates)
14490     return true;
14491   /* If there are more, but only one more, then we are referring to a
14492      member template.  That's OK too.  */
14493   if (parser->num_template_parameter_lists == num_templates + 1)
14494       return true;
14495   /* Otherwise, there are too many template parameter lists.  We have
14496      something like:
14497
14498      template <class T> template <class U> void S::f();  */
14499   error ("too many template-parameter-lists");
14500   return false;
14501 }
14502
14503 /* Parse an optional `::' token indicating that the following name is
14504    from the global namespace.  If so, PARSER->SCOPE is set to the
14505    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14506    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14507    Returns the new value of PARSER->SCOPE, if the `::' token is
14508    present, and NULL_TREE otherwise.  */
14509
14510 static tree
14511 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14512 {
14513   cp_token *token;
14514
14515   /* Peek at the next token.  */
14516   token = cp_lexer_peek_token (parser->lexer);
14517   /* If we're looking at a `::' token then we're starting from the
14518      global namespace, not our current location.  */
14519   if (token->type == CPP_SCOPE)
14520     {
14521       /* Consume the `::' token.  */
14522       cp_lexer_consume_token (parser->lexer);
14523       /* Set the SCOPE so that we know where to start the lookup.  */
14524       parser->scope = global_namespace;
14525       parser->qualifying_scope = global_namespace;
14526       parser->object_scope = NULL_TREE;
14527
14528       return parser->scope;
14529     }
14530   else if (!current_scope_valid_p)
14531     {
14532       parser->scope = NULL_TREE;
14533       parser->qualifying_scope = NULL_TREE;
14534       parser->object_scope = NULL_TREE;
14535     }
14536
14537   return NULL_TREE;
14538 }
14539
14540 /* Returns TRUE if the upcoming token sequence is the start of a
14541    constructor declarator.  If FRIEND_P is true, the declarator is
14542    preceded by the `friend' specifier.  */
14543
14544 static bool
14545 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14546 {
14547   bool constructor_p;
14548   tree type_decl = NULL_TREE;
14549   bool nested_name_p;
14550   cp_token *next_token;
14551
14552   /* The common case is that this is not a constructor declarator, so
14553      try to avoid doing lots of work if at all possible.  It's not
14554      valid declare a constructor at function scope.  */
14555   if (at_function_scope_p ())
14556     return false;
14557   /* And only certain tokens can begin a constructor declarator.  */
14558   next_token = cp_lexer_peek_token (parser->lexer);
14559   if (next_token->type != CPP_NAME
14560       && next_token->type != CPP_SCOPE
14561       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14562       && next_token->type != CPP_TEMPLATE_ID)
14563     return false;
14564
14565   /* Parse tentatively; we are going to roll back all of the tokens
14566      consumed here.  */
14567   cp_parser_parse_tentatively (parser);
14568   /* Assume that we are looking at a constructor declarator.  */
14569   constructor_p = true;
14570
14571   /* Look for the optional `::' operator.  */
14572   cp_parser_global_scope_opt (parser,
14573                               /*current_scope_valid_p=*/false);
14574   /* Look for the nested-name-specifier.  */
14575   nested_name_p
14576     = (cp_parser_nested_name_specifier_opt (parser,
14577                                             /*typename_keyword_p=*/false,
14578                                             /*check_dependency_p=*/false,
14579                                             /*type_p=*/false,
14580                                             /*is_declaration=*/false)
14581        != NULL_TREE);
14582   /* Outside of a class-specifier, there must be a
14583      nested-name-specifier.  */
14584   if (!nested_name_p &&
14585       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14586        || friend_p))
14587     constructor_p = false;
14588   /* If we still think that this might be a constructor-declarator,
14589      look for a class-name.  */
14590   if (constructor_p)
14591     {
14592       /* If we have:
14593
14594            template <typename T> struct S { S(); };
14595            template <typename T> S<T>::S ();
14596
14597          we must recognize that the nested `S' names a class.
14598          Similarly, for:
14599
14600            template <typename T> S<T>::S<T> ();
14601
14602          we must recognize that the nested `S' names a template.  */
14603       type_decl = cp_parser_class_name (parser,
14604                                         /*typename_keyword_p=*/false,
14605                                         /*template_keyword_p=*/false,
14606                                         /*type_p=*/false,
14607                                         /*check_dependency_p=*/false,
14608                                         /*class_head_p=*/false,
14609                                         /*is_declaration=*/false);
14610       /* If there was no class-name, then this is not a constructor.  */
14611       constructor_p = !cp_parser_error_occurred (parser);
14612     }
14613
14614   /* If we're still considering a constructor, we have to see a `(',
14615      to begin the parameter-declaration-clause, followed by either a
14616      `)', an `...', or a decl-specifier.  We need to check for a
14617      type-specifier to avoid being fooled into thinking that:
14618
14619        S::S (f) (int);
14620
14621      is a constructor.  (It is actually a function named `f' that
14622      takes one parameter (of type `int') and returns a value of type
14623      `S::S'.  */
14624   if (constructor_p
14625       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14626     {
14627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14628           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14629           /* A parameter declaration begins with a decl-specifier,
14630              which is either the "attribute" keyword, a storage class
14631              specifier, or (usually) a type-specifier.  */
14632           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14633           && !cp_parser_storage_class_specifier_opt (parser))
14634         {
14635           tree type;
14636           bool pop_p = false;
14637           unsigned saved_num_template_parameter_lists;
14638
14639           /* Names appearing in the type-specifier should be looked up
14640              in the scope of the class.  */
14641           if (current_class_type)
14642             type = NULL_TREE;
14643           else
14644             {
14645               type = TREE_TYPE (type_decl);
14646               if (TREE_CODE (type) == TYPENAME_TYPE)
14647                 {
14648                   type = resolve_typename_type (type,
14649                                                 /*only_current_p=*/false);
14650                   if (type == error_mark_node)
14651                     {
14652                       cp_parser_abort_tentative_parse (parser);
14653                       return false;
14654                     }
14655                 }
14656               pop_p = push_scope (type);
14657             }
14658
14659           /* Inside the constructor parameter list, surrounding
14660              template-parameter-lists do not apply.  */
14661           saved_num_template_parameter_lists
14662             = parser->num_template_parameter_lists;
14663           parser->num_template_parameter_lists = 0;
14664
14665           /* Look for the type-specifier.  */
14666           cp_parser_type_specifier (parser,
14667                                     CP_PARSER_FLAGS_NONE,
14668                                     /*decl_specs=*/NULL,
14669                                     /*is_declarator=*/true,
14670                                     /*declares_class_or_enum=*/NULL,
14671                                     /*is_cv_qualifier=*/NULL);
14672
14673           parser->num_template_parameter_lists
14674             = saved_num_template_parameter_lists;
14675
14676           /* Leave the scope of the class.  */
14677           if (pop_p)
14678             pop_scope (type);
14679
14680           constructor_p = !cp_parser_error_occurred (parser);
14681         }
14682     }
14683   else
14684     constructor_p = false;
14685   /* We did not really want to consume any tokens.  */
14686   cp_parser_abort_tentative_parse (parser);
14687
14688   return constructor_p;
14689 }
14690
14691 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14692    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14693    they must be performed once we are in the scope of the function.
14694
14695    Returns the function defined.  */
14696
14697 static tree
14698 cp_parser_function_definition_from_specifiers_and_declarator
14699   (cp_parser* parser,
14700    cp_decl_specifier_seq *decl_specifiers,
14701    tree attributes,
14702    const cp_declarator *declarator)
14703 {
14704   tree fn;
14705   bool success_p;
14706
14707   /* Begin the function-definition.  */
14708   success_p = start_function (decl_specifiers, declarator, attributes);
14709
14710   /* The things we're about to see are not directly qualified by any
14711      template headers we've seen thus far.  */
14712   reset_specialization ();
14713
14714   /* If there were names looked up in the decl-specifier-seq that we
14715      did not check, check them now.  We must wait until we are in the
14716      scope of the function to perform the checks, since the function
14717      might be a friend.  */
14718   perform_deferred_access_checks ();
14719
14720   if (!success_p)
14721     {
14722       /* Skip the entire function.  */
14723       error ("invalid function declaration");
14724       cp_parser_skip_to_end_of_block_or_statement (parser);
14725       fn = error_mark_node;
14726     }
14727   else
14728     fn = cp_parser_function_definition_after_declarator (parser,
14729                                                          /*inline_p=*/false);
14730
14731   return fn;
14732 }
14733
14734 /* Parse the part of a function-definition that follows the
14735    declarator.  INLINE_P is TRUE iff this function is an inline
14736    function defined with a class-specifier.
14737
14738    Returns the function defined.  */
14739
14740 static tree
14741 cp_parser_function_definition_after_declarator (cp_parser* parser,
14742                                                 bool inline_p)
14743 {
14744   tree fn;
14745   bool ctor_initializer_p = false;
14746   bool saved_in_unbraced_linkage_specification_p;
14747   unsigned saved_num_template_parameter_lists;
14748
14749   /* If the next token is `return', then the code may be trying to
14750      make use of the "named return value" extension that G++ used to
14751      support.  */
14752   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14753     {
14754       /* Consume the `return' keyword.  */
14755       cp_lexer_consume_token (parser->lexer);
14756       /* Look for the identifier that indicates what value is to be
14757          returned.  */
14758       cp_parser_identifier (parser);
14759       /* Issue an error message.  */
14760       error ("named return values are no longer supported");
14761       /* Skip tokens until we reach the start of the function body.  */
14762       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14763              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14764         cp_lexer_consume_token (parser->lexer);
14765     }
14766   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14767      anything declared inside `f'.  */
14768   saved_in_unbraced_linkage_specification_p
14769     = parser->in_unbraced_linkage_specification_p;
14770   parser->in_unbraced_linkage_specification_p = false;
14771   /* Inside the function, surrounding template-parameter-lists do not
14772      apply.  */
14773   saved_num_template_parameter_lists
14774     = parser->num_template_parameter_lists;
14775   parser->num_template_parameter_lists = 0;
14776   /* If the next token is `try', then we are looking at a
14777      function-try-block.  */
14778   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14779     ctor_initializer_p = cp_parser_function_try_block (parser);
14780   /* A function-try-block includes the function-body, so we only do
14781      this next part if we're not processing a function-try-block.  */
14782   else
14783     ctor_initializer_p
14784       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14785
14786   /* Finish the function.  */
14787   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14788                         (inline_p ? 2 : 0));
14789   /* Generate code for it, if necessary.  */
14790   expand_or_defer_fn (fn);
14791   /* Restore the saved values.  */
14792   parser->in_unbraced_linkage_specification_p
14793     = saved_in_unbraced_linkage_specification_p;
14794   parser->num_template_parameter_lists
14795     = saved_num_template_parameter_lists;
14796
14797   return fn;
14798 }
14799
14800 /* Parse a template-declaration, assuming that the `export' (and
14801    `extern') keywords, if present, has already been scanned.  MEMBER_P
14802    is as for cp_parser_template_declaration.  */
14803
14804 static void
14805 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14806 {
14807   tree decl = NULL_TREE;
14808   tree parameter_list;
14809   bool friend_p = false;
14810
14811   /* Look for the `template' keyword.  */
14812   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14813     return;
14814
14815   /* And the `<'.  */
14816   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14817     return;
14818
14819   /* If the next token is `>', then we have an invalid
14820      specialization.  Rather than complain about an invalid template
14821      parameter, issue an error message here.  */
14822   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14823     {
14824       cp_parser_error (parser, "invalid explicit specialization");
14825       begin_specialization ();
14826       parameter_list = NULL_TREE;
14827     }
14828   else
14829     {
14830       /* Parse the template parameters.  */
14831       begin_template_parm_list ();
14832       parameter_list = cp_parser_template_parameter_list (parser);
14833       parameter_list = end_template_parm_list (parameter_list);
14834     }
14835
14836   /* Look for the `>'.  */
14837   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14838   /* We just processed one more parameter list.  */
14839   ++parser->num_template_parameter_lists;
14840   /* If the next token is `template', there are more template
14841      parameters.  */
14842   if (cp_lexer_next_token_is_keyword (parser->lexer,
14843                                       RID_TEMPLATE))
14844     cp_parser_template_declaration_after_export (parser, member_p);
14845   else
14846     {
14847       /* There are no access checks when parsing a template, as we do not
14848          know if a specialization will be a friend.  */
14849       push_deferring_access_checks (dk_no_check);
14850
14851       decl = cp_parser_single_declaration (parser,
14852                                            member_p,
14853                                            &friend_p);
14854
14855       pop_deferring_access_checks ();
14856
14857       /* If this is a member template declaration, let the front
14858          end know.  */
14859       if (member_p && !friend_p && decl)
14860         {
14861           if (TREE_CODE (decl) == TYPE_DECL)
14862             cp_parser_check_access_in_redeclaration (decl);
14863
14864           decl = finish_member_template_decl (decl);
14865         }
14866       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14867         make_friend_class (current_class_type, TREE_TYPE (decl),
14868                            /*complain=*/true);
14869     }
14870   /* We are done with the current parameter list.  */
14871   --parser->num_template_parameter_lists;
14872
14873   /* Finish up.  */
14874   finish_template_decl (parameter_list);
14875
14876   /* Register member declarations.  */
14877   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14878     finish_member_declaration (decl);
14879
14880   /* If DECL is a function template, we must return to parse it later.
14881      (Even though there is no definition, there might be default
14882      arguments that need handling.)  */
14883   if (member_p && decl
14884       && (TREE_CODE (decl) == FUNCTION_DECL
14885           || DECL_FUNCTION_TEMPLATE_P (decl)))
14886     TREE_VALUE (parser->unparsed_functions_queues)
14887       = tree_cons (NULL_TREE, decl,
14888                    TREE_VALUE (parser->unparsed_functions_queues));
14889 }
14890
14891 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14892    `function-definition' sequence.  MEMBER_P is true, this declaration
14893    appears in a class scope.
14894
14895    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14896    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14897
14898 static tree
14899 cp_parser_single_declaration (cp_parser* parser,
14900                               bool member_p,
14901                               bool* friend_p)
14902 {
14903   int declares_class_or_enum;
14904   tree decl = NULL_TREE;
14905   cp_decl_specifier_seq decl_specifiers;
14906   bool function_definition_p = false;
14907
14908   /* Defer access checks until we know what is being declared.  */
14909   push_deferring_access_checks (dk_deferred);
14910
14911   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14912      alternative.  */
14913   cp_parser_decl_specifier_seq (parser,
14914                                 CP_PARSER_FLAGS_OPTIONAL,
14915                                 &decl_specifiers,
14916                                 &declares_class_or_enum);
14917   if (friend_p)
14918     *friend_p = cp_parser_friend_p (&decl_specifiers);
14919   /* Gather up the access checks that occurred the
14920      decl-specifier-seq.  */
14921   stop_deferring_access_checks ();
14922
14923   /* Check for the declaration of a template class.  */
14924   if (declares_class_or_enum)
14925     {
14926       if (cp_parser_declares_only_class_p (parser))
14927         {
14928           decl = shadow_tag (&decl_specifiers);
14929           if (decl && decl != error_mark_node)
14930             decl = TYPE_NAME (decl);
14931           else
14932             decl = error_mark_node;
14933         }
14934     }
14935   else
14936     decl = NULL_TREE;
14937   /* If it's not a template class, try for a template function.  If
14938      the next token is a `;', then this declaration does not declare
14939      anything.  But, if there were errors in the decl-specifiers, then
14940      the error might well have come from an attempted class-specifier.
14941      In that case, there's no need to warn about a missing declarator.  */
14942   if (!decl
14943       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14944           || decl_specifiers.type != error_mark_node))
14945     decl = cp_parser_init_declarator (parser,
14946                                       &decl_specifiers,
14947                                       /*function_definition_allowed_p=*/true,
14948                                       member_p,
14949                                       declares_class_or_enum,
14950                                       &function_definition_p);
14951
14952   pop_deferring_access_checks ();
14953
14954   /* Clear any current qualification; whatever comes next is the start
14955      of something new.  */
14956   parser->scope = NULL_TREE;
14957   parser->qualifying_scope = NULL_TREE;
14958   parser->object_scope = NULL_TREE;
14959   /* Look for a trailing `;' after the declaration.  */
14960   if (!function_definition_p
14961       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14962     cp_parser_skip_to_end_of_block_or_statement (parser);
14963
14964   return decl;
14965 }
14966
14967 /* Parse a cast-expression that is not the operand of a unary "&".  */
14968
14969 static tree
14970 cp_parser_simple_cast_expression (cp_parser *parser)
14971 {
14972   return cp_parser_cast_expression (parser, /*address_p=*/false);
14973 }
14974
14975 /* Parse a functional cast to TYPE.  Returns an expression
14976    representing the cast.  */
14977
14978 static tree
14979 cp_parser_functional_cast (cp_parser* parser, tree type)
14980 {
14981   tree expression_list;
14982   tree cast;
14983
14984   expression_list
14985     = cp_parser_parenthesized_expression_list (parser, false,
14986                                                /*non_constant_p=*/NULL);
14987
14988   cast = build_functional_cast (type, expression_list);
14989   /* [expr.const]/1: In an integral constant expression "only type
14990      conversions to integral or enumeration type can be used".  */
14991   if (cast != error_mark_node && !type_dependent_expression_p (type)
14992       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14993     {
14994       if (cp_parser_non_integral_constant_expression
14995           (parser, "a call to a constructor"))
14996         return error_mark_node;
14997     }
14998   return cast;
14999 }
15000
15001 /* Save the tokens that make up the body of a member function defined
15002    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15003    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15004    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15005    for the member function.  */
15006
15007 static tree
15008 cp_parser_save_member_function_body (cp_parser* parser,
15009                                      cp_decl_specifier_seq *decl_specifiers,
15010                                      cp_declarator *declarator,
15011                                      tree attributes)
15012 {
15013   cp_token *first;
15014   cp_token *last;
15015   tree fn;
15016
15017   /* Create the function-declaration.  */
15018   fn = start_method (decl_specifiers, declarator, attributes);
15019   /* If something went badly wrong, bail out now.  */
15020   if (fn == error_mark_node)
15021     {
15022       /* If there's a function-body, skip it.  */
15023       if (cp_parser_token_starts_function_definition_p
15024           (cp_lexer_peek_token (parser->lexer)))
15025         cp_parser_skip_to_end_of_block_or_statement (parser);
15026       return error_mark_node;
15027     }
15028
15029   /* Remember it, if there default args to post process.  */
15030   cp_parser_save_default_args (parser, fn);
15031
15032   /* Save away the tokens that make up the body of the
15033      function.  */
15034   first = parser->lexer->next_token;
15035   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15036   /* Handle function try blocks.  */
15037   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15038     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15039   last = parser->lexer->next_token;
15040
15041   /* Save away the inline definition; we will process it when the
15042      class is complete.  */
15043   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15044   DECL_PENDING_INLINE_P (fn) = 1;
15045
15046   /* We need to know that this was defined in the class, so that
15047      friend templates are handled correctly.  */
15048   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15049
15050   /* We're done with the inline definition.  */
15051   finish_method (fn);
15052
15053   /* Add FN to the queue of functions to be parsed later.  */
15054   TREE_VALUE (parser->unparsed_functions_queues)
15055     = tree_cons (NULL_TREE, fn,
15056                  TREE_VALUE (parser->unparsed_functions_queues));
15057
15058   return fn;
15059 }
15060
15061 /* Parse a template-argument-list, as well as the trailing ">" (but
15062    not the opening ">").  See cp_parser_template_argument_list for the
15063    return value.  */
15064
15065 static tree
15066 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15067 {
15068   tree arguments;
15069   tree saved_scope;
15070   tree saved_qualifying_scope;
15071   tree saved_object_scope;
15072   bool saved_greater_than_is_operator_p;
15073
15074   /* [temp.names]
15075
15076      When parsing a template-id, the first non-nested `>' is taken as
15077      the end of the template-argument-list rather than a greater-than
15078      operator.  */
15079   saved_greater_than_is_operator_p
15080     = parser->greater_than_is_operator_p;
15081   parser->greater_than_is_operator_p = false;
15082   /* Parsing the argument list may modify SCOPE, so we save it
15083      here.  */
15084   saved_scope = parser->scope;
15085   saved_qualifying_scope = parser->qualifying_scope;
15086   saved_object_scope = parser->object_scope;
15087   /* Parse the template-argument-list itself.  */
15088   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15089     arguments = NULL_TREE;
15090   else
15091     arguments = cp_parser_template_argument_list (parser);
15092   /* Look for the `>' that ends the template-argument-list. If we find
15093      a '>>' instead, it's probably just a typo.  */
15094   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15095     {
15096       if (!saved_greater_than_is_operator_p)
15097         {
15098           /* If we're in a nested template argument list, the '>>' has
15099             to be a typo for '> >'. We emit the error message, but we
15100             continue parsing and we push a '>' as next token, so that
15101             the argument list will be parsed correctly.  Note that the
15102             global source location is still on the token before the
15103             '>>', so we need to say explicitly where we want it.  */
15104           cp_token *token = cp_lexer_peek_token (parser->lexer);
15105           error ("%H%<>>%> should be %<> >%> "
15106                  "within a nested template argument list",
15107                  &token->location);
15108
15109           /* ??? Proper recovery should terminate two levels of
15110              template argument list here.  */
15111           token->type = CPP_GREATER;
15112         }
15113       else
15114         {
15115           /* If this is not a nested template argument list, the '>>'
15116             is a typo for '>'. Emit an error message and continue.
15117             Same deal about the token location, but here we can get it
15118             right by consuming the '>>' before issuing the diagnostic.  */
15119           cp_lexer_consume_token (parser->lexer);
15120           error ("spurious %<>>%>, use %<>%> to terminate "
15121                  "a template argument list");
15122         }
15123     }
15124   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15125     error ("missing %<>%> to terminate the template argument list");
15126   else
15127     /* It's what we want, a '>'; consume it.  */
15128     cp_lexer_consume_token (parser->lexer);
15129   /* The `>' token might be a greater-than operator again now.  */
15130   parser->greater_than_is_operator_p
15131     = saved_greater_than_is_operator_p;
15132   /* Restore the SAVED_SCOPE.  */
15133   parser->scope = saved_scope;
15134   parser->qualifying_scope = saved_qualifying_scope;
15135   parser->object_scope = saved_object_scope;
15136
15137   return arguments;
15138 }
15139
15140 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15141    arguments, or the body of the function have not yet been parsed,
15142    parse them now.  */
15143
15144 static void
15145 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15146 {
15147   /* If this member is a template, get the underlying
15148      FUNCTION_DECL.  */
15149   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15150     member_function = DECL_TEMPLATE_RESULT (member_function);
15151
15152   /* There should not be any class definitions in progress at this
15153      point; the bodies of members are only parsed outside of all class
15154      definitions.  */
15155   gcc_assert (parser->num_classes_being_defined == 0);
15156   /* While we're parsing the member functions we might encounter more
15157      classes.  We want to handle them right away, but we don't want
15158      them getting mixed up with functions that are currently in the
15159      queue.  */
15160   parser->unparsed_functions_queues
15161     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15162
15163   /* Make sure that any template parameters are in scope.  */
15164   maybe_begin_member_template_processing (member_function);
15165
15166   /* If the body of the function has not yet been parsed, parse it
15167      now.  */
15168   if (DECL_PENDING_INLINE_P (member_function))
15169     {
15170       tree function_scope;
15171       cp_token_cache *tokens;
15172
15173       /* The function is no longer pending; we are processing it.  */
15174       tokens = DECL_PENDING_INLINE_INFO (member_function);
15175       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15176       DECL_PENDING_INLINE_P (member_function) = 0;
15177       /* If this was an inline function in a local class, enter the scope
15178          of the containing function.  */
15179       function_scope = decl_function_context (member_function);
15180       if (function_scope)
15181         push_function_context_to (function_scope);
15182
15183       /* Push the body of the function onto the lexer stack.  */
15184       cp_parser_push_lexer_for_tokens (parser, tokens);
15185
15186       /* Let the front end know that we going to be defining this
15187          function.  */
15188       start_preparsed_function (member_function, NULL_TREE,
15189                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15190
15191       /* Now, parse the body of the function.  */
15192       cp_parser_function_definition_after_declarator (parser,
15193                                                       /*inline_p=*/true);
15194
15195       /* Leave the scope of the containing function.  */
15196       if (function_scope)
15197         pop_function_context_from (function_scope);
15198       cp_parser_pop_lexer (parser);
15199     }
15200
15201   /* Remove any template parameters from the symbol table.  */
15202   maybe_end_member_template_processing ();
15203
15204   /* Restore the queue.  */
15205   parser->unparsed_functions_queues
15206     = TREE_CHAIN (parser->unparsed_functions_queues);
15207 }
15208
15209 /* If DECL contains any default args, remember it on the unparsed
15210    functions queue.  */
15211
15212 static void
15213 cp_parser_save_default_args (cp_parser* parser, tree decl)
15214 {
15215   tree probe;
15216
15217   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15218        probe;
15219        probe = TREE_CHAIN (probe))
15220     if (TREE_PURPOSE (probe))
15221       {
15222         TREE_PURPOSE (parser->unparsed_functions_queues)
15223           = tree_cons (current_class_type, decl,
15224                        TREE_PURPOSE (parser->unparsed_functions_queues));
15225         break;
15226       }
15227   return;
15228 }
15229
15230 /* FN is a FUNCTION_DECL which may contains a parameter with an
15231    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15232    assumes that the current scope is the scope in which the default
15233    argument should be processed.  */
15234
15235 static void
15236 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15237 {
15238   bool saved_local_variables_forbidden_p;
15239   tree parm;
15240
15241   /* While we're parsing the default args, we might (due to the
15242      statement expression extension) encounter more classes.  We want
15243      to handle them right away, but we don't want them getting mixed
15244      up with default args that are currently in the queue.  */
15245   parser->unparsed_functions_queues
15246     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15247
15248   /* Local variable names (and the `this' keyword) may not appear
15249      in a default argument.  */
15250   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15251   parser->local_variables_forbidden_p = true;
15252
15253   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15254        parm;
15255        parm = TREE_CHAIN (parm))
15256     {
15257       cp_token_cache *tokens;
15258
15259       if (!TREE_PURPOSE (parm)
15260           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15261         continue;
15262
15263        /* Push the saved tokens for the default argument onto the parser's
15264           lexer stack.  */
15265       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15266       cp_parser_push_lexer_for_tokens (parser, tokens);
15267
15268       /* Parse the assignment-expression.  */
15269       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15270
15271       /* If the token stream has not been completely used up, then
15272          there was extra junk after the end of the default
15273          argument.  */
15274       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15275         cp_parser_error (parser, "expected %<,%>");
15276
15277       /* Revert to the main lexer.  */
15278       cp_parser_pop_lexer (parser);
15279     }
15280
15281   /* Restore the state of local_variables_forbidden_p.  */
15282   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15283
15284   /* Restore the queue.  */
15285   parser->unparsed_functions_queues
15286     = TREE_CHAIN (parser->unparsed_functions_queues);
15287 }
15288
15289 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15290    either a TYPE or an expression, depending on the form of the
15291    input.  The KEYWORD indicates which kind of expression we have
15292    encountered.  */
15293
15294 static tree
15295 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15296 {
15297   static const char *format;
15298   tree expr = NULL_TREE;
15299   const char *saved_message;
15300   bool saved_integral_constant_expression_p;
15301
15302   /* Initialize FORMAT the first time we get here.  */
15303   if (!format)
15304     format = "types may not be defined in `%s' expressions";
15305
15306   /* Types cannot be defined in a `sizeof' expression.  Save away the
15307      old message.  */
15308   saved_message = parser->type_definition_forbidden_message;
15309   /* And create the new one.  */
15310   parser->type_definition_forbidden_message
15311     = xmalloc (strlen (format)
15312                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15313                + 1 /* `\0' */);
15314   sprintf ((char *) parser->type_definition_forbidden_message,
15315            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15316
15317   /* The restrictions on constant-expressions do not apply inside
15318      sizeof expressions.  */
15319   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15320   parser->integral_constant_expression_p = false;
15321
15322   /* Do not actually evaluate the expression.  */
15323   ++skip_evaluation;
15324   /* If it's a `(', then we might be looking at the type-id
15325      construction.  */
15326   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15327     {
15328       tree type;
15329       bool saved_in_type_id_in_expr_p;
15330
15331       /* We can't be sure yet whether we're looking at a type-id or an
15332          expression.  */
15333       cp_parser_parse_tentatively (parser);
15334       /* Consume the `('.  */
15335       cp_lexer_consume_token (parser->lexer);
15336       /* Parse the type-id.  */
15337       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15338       parser->in_type_id_in_expr_p = true;
15339       type = cp_parser_type_id (parser);
15340       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15341       /* Now, look for the trailing `)'.  */
15342       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15343       /* If all went well, then we're done.  */
15344       if (cp_parser_parse_definitely (parser))
15345         {
15346           cp_decl_specifier_seq decl_specs;
15347
15348           /* Build a trivial decl-specifier-seq.  */
15349           clear_decl_specs (&decl_specs);
15350           decl_specs.type = type;
15351
15352           /* Call grokdeclarator to figure out what type this is.  */
15353           expr = grokdeclarator (NULL,
15354                                  &decl_specs,
15355                                  TYPENAME,
15356                                  /*initialized=*/0,
15357                                  /*attrlist=*/NULL);
15358         }
15359     }
15360
15361   /* If the type-id production did not work out, then we must be
15362      looking at the unary-expression production.  */
15363   if (!expr)
15364     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15365   /* Go back to evaluating expressions.  */
15366   --skip_evaluation;
15367
15368   /* Free the message we created.  */
15369   free ((char *) parser->type_definition_forbidden_message);
15370   /* And restore the old one.  */
15371   parser->type_definition_forbidden_message = saved_message;
15372   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15373
15374   return expr;
15375 }
15376
15377 /* If the current declaration has no declarator, return true.  */
15378
15379 static bool
15380 cp_parser_declares_only_class_p (cp_parser *parser)
15381 {
15382   /* If the next token is a `;' or a `,' then there is no
15383      declarator.  */
15384   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15385           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15386 }
15387
15388 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15389
15390 static void
15391 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15392                              cp_storage_class storage_class)
15393 {
15394   if (decl_specs->storage_class != sc_none)
15395     decl_specs->multiple_storage_classes_p = true;
15396   else
15397     decl_specs->storage_class = storage_class;
15398 }
15399
15400 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15401    is true, the type is a user-defined type; otherwise it is a
15402    built-in type specified by a keyword.  */
15403
15404 static void
15405 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15406                               tree type_spec,
15407                               bool user_defined_p)
15408 {
15409   decl_specs->any_specifiers_p = true;
15410
15411   /* If the user tries to redeclare a built-in type (with, for example,
15412      in "typedef int wchar_t;") we remember that this is what
15413      happened.  In system headers, we ignore these declarations so
15414      that G++ can work with system headers that are not C++-safe.  */
15415   if (decl_specs->specs[(int) ds_typedef]
15416       && !user_defined_p
15417       && (decl_specs->type
15418           || decl_specs->specs[(int) ds_long]
15419           || decl_specs->specs[(int) ds_short]
15420           || decl_specs->specs[(int) ds_unsigned]
15421           || decl_specs->specs[(int) ds_signed]))
15422     {
15423       decl_specs->redefined_builtin_type = type_spec;
15424       if (!decl_specs->type)
15425         {
15426           decl_specs->type = type_spec;
15427           decl_specs->user_defined_type_p = false;
15428         }
15429     }
15430   else if (decl_specs->type)
15431     decl_specs->multiple_types_p = true;
15432   else
15433     {
15434       decl_specs->type = type_spec;
15435       decl_specs->user_defined_type_p = user_defined_p;
15436       decl_specs->redefined_builtin_type = NULL_TREE;
15437     }
15438 }
15439
15440 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15441    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15442
15443 static bool
15444 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15445 {
15446   return decl_specifiers->specs[(int) ds_friend] != 0;
15447 }
15448
15449 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15450    issue an error message indicating that TOKEN_DESC was expected.
15451
15452    Returns the token consumed, if the token had the appropriate type.
15453    Otherwise, returns NULL.  */
15454
15455 static cp_token *
15456 cp_parser_require (cp_parser* parser,
15457                    enum cpp_ttype type,
15458                    const char* token_desc)
15459 {
15460   if (cp_lexer_next_token_is (parser->lexer, type))
15461     return cp_lexer_consume_token (parser->lexer);
15462   else
15463     {
15464       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15465       if (!cp_parser_simulate_error (parser))
15466         {
15467           char *message = concat ("expected ", token_desc, NULL);
15468           cp_parser_error (parser, message);
15469           free (message);
15470         }
15471       return NULL;
15472     }
15473 }
15474
15475 /* Like cp_parser_require, except that tokens will be skipped until
15476    the desired token is found.  An error message is still produced if
15477    the next token is not as expected.  */
15478
15479 static void
15480 cp_parser_skip_until_found (cp_parser* parser,
15481                             enum cpp_ttype type,
15482                             const char* token_desc)
15483 {
15484   cp_token *token;
15485   unsigned nesting_depth = 0;
15486
15487   if (cp_parser_require (parser, type, token_desc))
15488     return;
15489
15490   /* Skip tokens until the desired token is found.  */
15491   while (true)
15492     {
15493       /* Peek at the next token.  */
15494       token = cp_lexer_peek_token (parser->lexer);
15495       /* If we've reached the token we want, consume it and
15496          stop.  */
15497       if (token->type == type && !nesting_depth)
15498         {
15499           cp_lexer_consume_token (parser->lexer);
15500           return;
15501         }
15502       /* If we've run out of tokens, stop.  */
15503       if (token->type == CPP_EOF)
15504         return;
15505       if (token->type == CPP_OPEN_BRACE
15506           || token->type == CPP_OPEN_PAREN
15507           || token->type == CPP_OPEN_SQUARE)
15508         ++nesting_depth;
15509       else if (token->type == CPP_CLOSE_BRACE
15510                || token->type == CPP_CLOSE_PAREN
15511                || token->type == CPP_CLOSE_SQUARE)
15512         {
15513           if (nesting_depth-- == 0)
15514             return;
15515         }
15516       /* Consume this token.  */
15517       cp_lexer_consume_token (parser->lexer);
15518     }
15519 }
15520
15521 /* If the next token is the indicated keyword, consume it.  Otherwise,
15522    issue an error message indicating that TOKEN_DESC was expected.
15523
15524    Returns the token consumed, if the token had the appropriate type.
15525    Otherwise, returns NULL.  */
15526
15527 static cp_token *
15528 cp_parser_require_keyword (cp_parser* parser,
15529                            enum rid keyword,
15530                            const char* token_desc)
15531 {
15532   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15533
15534   if (token && token->keyword != keyword)
15535     {
15536       dyn_string_t error_msg;
15537
15538       /* Format the error message.  */
15539       error_msg = dyn_string_new (0);
15540       dyn_string_append_cstr (error_msg, "expected ");
15541       dyn_string_append_cstr (error_msg, token_desc);
15542       cp_parser_error (parser, error_msg->s);
15543       dyn_string_delete (error_msg);
15544       return NULL;
15545     }
15546
15547   return token;
15548 }
15549
15550 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15551    function-definition.  */
15552
15553 static bool
15554 cp_parser_token_starts_function_definition_p (cp_token* token)
15555 {
15556   return (/* An ordinary function-body begins with an `{'.  */
15557           token->type == CPP_OPEN_BRACE
15558           /* A ctor-initializer begins with a `:'.  */
15559           || token->type == CPP_COLON
15560           /* A function-try-block begins with `try'.  */
15561           || token->keyword == RID_TRY
15562           /* The named return value extension begins with `return'.  */
15563           || token->keyword == RID_RETURN);
15564 }
15565
15566 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15567    definition.  */
15568
15569 static bool
15570 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15571 {
15572   cp_token *token;
15573
15574   token = cp_lexer_peek_token (parser->lexer);
15575   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15576 }
15577
15578 /* Returns TRUE iff the next token is the "," or ">" ending a
15579    template-argument. ">>" is also accepted (after the full
15580    argument was parsed) because it's probably a typo for "> >",
15581    and there is a specific diagnostic for this.  */
15582
15583 static bool
15584 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15585 {
15586   cp_token *token;
15587
15588   token = cp_lexer_peek_token (parser->lexer);
15589   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15590           || token->type == CPP_RSHIFT);
15591 }
15592
15593 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15594    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15595
15596 static bool
15597 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15598                                                      size_t n)
15599 {
15600   cp_token *token;
15601
15602   token = cp_lexer_peek_nth_token (parser->lexer, n);
15603   if (token->type == CPP_LESS)
15604     return true;
15605   /* Check for the sequence `<::' in the original code. It would be lexed as
15606      `[:', where `[' is a digraph, and there is no whitespace before
15607      `:'.  */
15608   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15609     {
15610       cp_token *token2;
15611       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15612       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15613         return true;
15614     }
15615   return false;
15616 }
15617
15618 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15619    or none_type otherwise.  */
15620
15621 static enum tag_types
15622 cp_parser_token_is_class_key (cp_token* token)
15623 {
15624   switch (token->keyword)
15625     {
15626     case RID_CLASS:
15627       return class_type;
15628     case RID_STRUCT:
15629       return record_type;
15630     case RID_UNION:
15631       return union_type;
15632
15633     default:
15634       return none_type;
15635     }
15636 }
15637
15638 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15639
15640 static void
15641 cp_parser_check_class_key (enum tag_types class_key, tree type)
15642 {
15643   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15644     pedwarn ("%qs tag used in naming %q#T",
15645             class_key == union_type ? "union"
15646              : class_key == record_type ? "struct" : "class",
15647              type);
15648 }
15649
15650 /* Issue an error message if DECL is redeclared with different
15651    access than its original declaration [class.access.spec/3].
15652    This applies to nested classes and nested class templates.
15653    [class.mem/1].  */
15654
15655 static void
15656 cp_parser_check_access_in_redeclaration (tree decl)
15657 {
15658   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15659     return;
15660
15661   if ((TREE_PRIVATE (decl)
15662        != (current_access_specifier == access_private_node))
15663       || (TREE_PROTECTED (decl)
15664           != (current_access_specifier == access_protected_node)))
15665     error ("%qD redeclared with different access", decl);
15666 }
15667
15668 /* Look for the `template' keyword, as a syntactic disambiguator.
15669    Return TRUE iff it is present, in which case it will be
15670    consumed.  */
15671
15672 static bool
15673 cp_parser_optional_template_keyword (cp_parser *parser)
15674 {
15675   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15676     {
15677       /* The `template' keyword can only be used within templates;
15678          outside templates the parser can always figure out what is a
15679          template and what is not.  */
15680       if (!processing_template_decl)
15681         {
15682           error ("%<template%> (as a disambiguator) is only allowed "
15683                  "within templates");
15684           /* If this part of the token stream is rescanned, the same
15685              error message would be generated.  So, we purge the token
15686              from the stream.  */
15687           cp_lexer_purge_token (parser->lexer);
15688           return false;
15689         }
15690       else
15691         {
15692           /* Consume the `template' keyword.  */
15693           cp_lexer_consume_token (parser->lexer);
15694           return true;
15695         }
15696     }
15697
15698   return false;
15699 }
15700
15701 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15702    set PARSER->SCOPE, and perform other related actions.  */
15703
15704 static void
15705 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15706 {
15707   tree value;
15708   tree check;
15709
15710   /* Get the stored value.  */
15711   value = cp_lexer_consume_token (parser->lexer)->value;
15712   /* Perform any access checks that were deferred.  */
15713   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15714     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15715   /* Set the scope from the stored value.  */
15716   parser->scope = TREE_VALUE (value);
15717   parser->qualifying_scope = TREE_TYPE (value);
15718   parser->object_scope = NULL_TREE;
15719 }
15720
15721 /* Consume tokens up through a non-nested END token. */
15722
15723 static void
15724 cp_parser_cache_group (cp_parser *parser,
15725                        enum cpp_ttype end,
15726                        unsigned depth)
15727 {
15728   while (true)
15729     {
15730       cp_token *token;
15731
15732       /* Abort a parenthesized expression if we encounter a brace.  */
15733       if ((end == CPP_CLOSE_PAREN || depth == 0)
15734           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15735         return;
15736       /* If we've reached the end of the file, stop.  */
15737       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15738         return;
15739       /* Consume the next token.  */
15740       token = cp_lexer_consume_token (parser->lexer);
15741       /* See if it starts a new group.  */
15742       if (token->type == CPP_OPEN_BRACE)
15743         {
15744           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15745           if (depth == 0)
15746             return;
15747         }
15748       else if (token->type == CPP_OPEN_PAREN)
15749         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15750       else if (token->type == end)
15751         return;
15752     }
15753 }
15754
15755 /* Begin parsing tentatively.  We always save tokens while parsing
15756    tentatively so that if the tentative parsing fails we can restore the
15757    tokens.  */
15758
15759 static void
15760 cp_parser_parse_tentatively (cp_parser* parser)
15761 {
15762   /* Enter a new parsing context.  */
15763   parser->context = cp_parser_context_new (parser->context);
15764   /* Begin saving tokens.  */
15765   cp_lexer_save_tokens (parser->lexer);
15766   /* In order to avoid repetitive access control error messages,
15767      access checks are queued up until we are no longer parsing
15768      tentatively.  */
15769   push_deferring_access_checks (dk_deferred);
15770 }
15771
15772 /* Commit to the currently active tentative parse.  */
15773
15774 static void
15775 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15776 {
15777   cp_parser_context *context;
15778   cp_lexer *lexer;
15779
15780   /* Mark all of the levels as committed.  */
15781   lexer = parser->lexer;
15782   for (context = parser->context; context->next; context = context->next)
15783     {
15784       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15785         break;
15786       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15787       while (!cp_lexer_saving_tokens (lexer))
15788         lexer = lexer->next;
15789       cp_lexer_commit_tokens (lexer);
15790     }
15791 }
15792
15793 /* Abort the currently active tentative parse.  All consumed tokens
15794    will be rolled back, and no diagnostics will be issued.  */
15795
15796 static void
15797 cp_parser_abort_tentative_parse (cp_parser* parser)
15798 {
15799   cp_parser_simulate_error (parser);
15800   /* Now, pretend that we want to see if the construct was
15801      successfully parsed.  */
15802   cp_parser_parse_definitely (parser);
15803 }
15804
15805 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15806    token stream.  Otherwise, commit to the tokens we have consumed.
15807    Returns true if no error occurred; false otherwise.  */
15808
15809 static bool
15810 cp_parser_parse_definitely (cp_parser* parser)
15811 {
15812   bool error_occurred;
15813   cp_parser_context *context;
15814
15815   /* Remember whether or not an error occurred, since we are about to
15816      destroy that information.  */
15817   error_occurred = cp_parser_error_occurred (parser);
15818   /* Remove the topmost context from the stack.  */
15819   context = parser->context;
15820   parser->context = context->next;
15821   /* If no parse errors occurred, commit to the tentative parse.  */
15822   if (!error_occurred)
15823     {
15824       /* Commit to the tokens read tentatively, unless that was
15825          already done.  */
15826       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15827         cp_lexer_commit_tokens (parser->lexer);
15828
15829       pop_to_parent_deferring_access_checks ();
15830     }
15831   /* Otherwise, if errors occurred, roll back our state so that things
15832      are just as they were before we began the tentative parse.  */
15833   else
15834     {
15835       cp_lexer_rollback_tokens (parser->lexer);
15836       pop_deferring_access_checks ();
15837     }
15838   /* Add the context to the front of the free list.  */
15839   context->next = cp_parser_context_free_list;
15840   cp_parser_context_free_list = context;
15841
15842   return !error_occurred;
15843 }
15844
15845 /* Returns true if we are parsing tentatively -- but have decided that
15846    we will stick with this tentative parse, even if errors occur.  */
15847
15848 static bool
15849 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15850 {
15851   return (cp_parser_parsing_tentatively (parser)
15852           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15853 }
15854
15855 /* Returns nonzero iff an error has occurred during the most recent
15856    tentative parse.  */
15857
15858 static bool
15859 cp_parser_error_occurred (cp_parser* parser)
15860 {
15861   return (cp_parser_parsing_tentatively (parser)
15862           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15863 }
15864
15865 /* Returns nonzero if GNU extensions are allowed.  */
15866
15867 static bool
15868 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15869 {
15870   return parser->allow_gnu_extensions_p;
15871 }
15872
15873 \f
15874 /* The parser.  */
15875
15876 static GTY (()) cp_parser *the_parser;
15877
15878 /* External interface.  */
15879
15880 /* Parse one entire translation unit.  */
15881
15882 void
15883 c_parse_file (void)
15884 {
15885   bool error_occurred;
15886   static bool already_called = false;
15887
15888   if (already_called)
15889     {
15890       sorry ("inter-module optimizations not implemented for C++");
15891       return;
15892     }
15893   already_called = true;
15894
15895   the_parser = cp_parser_new ();
15896   push_deferring_access_checks (flag_access_control
15897                                 ? dk_no_deferred : dk_no_check);
15898   error_occurred = cp_parser_translation_unit (the_parser);
15899   the_parser = NULL;
15900 }
15901
15902 /* This variable must be provided by every front end.  */
15903
15904 int yydebug;
15905
15906 #include "gt-cp-parser.h"