OSDN Git Service

gcc
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
82   /* The input file stack index at which this token was found.  */
83   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84   /* The value associated with this token, if any.  */
85   union cp_token_value {
86     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
87     struct tree_check* GTY((tag ("1"))) tree_check_value;
88     /* Use for all other tokens.  */
89     tree GTY((tag ("0"))) value;
90   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91   /* The location at which this token was found.  */
92   location_t location;
93 } cp_token;
94
95 /* We use a stack of token pointer for saving token sets.  */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
99
100 static cp_token eof_token =
101 {
102   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103   0
104 };
105
106 /* The cp_lexer structure represents the C++ lexer.  It is responsible
107    for managing the token stream from the preprocessor and supplying
108    it to the parser.  Tokens are never added to the cp_lexer after
109    it is created.  */
110
111 typedef struct cp_lexer GTY (())
112 {
113   /* The memory allocated for the buffer.  NULL if this lexer does not
114      own the token buffer.  */
115   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
116   /* If the lexer owns the buffer, this is the number of tokens in the
117      buffer.  */
118   size_t buffer_length;
119
120   /* A pointer just past the last available token.  The tokens
121      in this lexer are [buffer, last_token).  */
122   cp_token_position GTY ((skip)) last_token;
123
124   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
125      no more available tokens.  */
126   cp_token_position GTY ((skip)) next_token;
127
128   /* A stack indicating positions at which cp_lexer_save_tokens was
129      called.  The top entry is the most recent position at which we
130      began saving tokens.  If the stack is non-empty, we are saving
131      tokens.  */
132   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
133
134   /* The next lexer in a linked list of lexers.  */
135   struct cp_lexer *next;
136
137   /* True if we should output debugging information.  */
138   bool debugging_p;
139
140   /* True if we're in the context of parsing a pragma, and should not
141      increment past the end-of-line marker.  */
142   bool in_pragma;
143 } cp_lexer;
144
145 /* cp_token_cache is a range of tokens.  There is no need to represent
146    allocate heap memory for it, since tokens are never removed from the
147    lexer's array.  There is also no need for the GC to walk through
148    a cp_token_cache, since everything in here is referenced through
149    a lexer.  */
150
151 typedef struct cp_token_cache GTY(())
152 {
153   /* The beginning of the token range.  */
154   cp_token * GTY((skip)) first;
155
156   /* Points immediately after the last token in the range.  */
157   cp_token * GTY ((skip)) last;
158 } cp_token_cache;
159
160 /* Prototypes.  */
161
162 static cp_lexer *cp_lexer_new_main
163   (void);
164 static cp_lexer *cp_lexer_new_from_tokens
165   (cp_token_cache *tokens);
166 static void cp_lexer_destroy
167   (cp_lexer *);
168 static int cp_lexer_saving_tokens
169   (const cp_lexer *);
170 static cp_token_position cp_lexer_token_position
171   (cp_lexer *, bool);
172 static cp_token *cp_lexer_token_at
173   (cp_lexer *, cp_token_position);
174 static void cp_lexer_get_preprocessor_token
175   (cp_lexer *, cp_token *);
176 static inline cp_token *cp_lexer_peek_token
177   (cp_lexer *);
178 static cp_token *cp_lexer_peek_nth_token
179   (cp_lexer *, size_t);
180 static inline bool cp_lexer_next_token_is
181   (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_not
183   (cp_lexer *, enum cpp_ttype);
184 static bool cp_lexer_next_token_is_keyword
185   (cp_lexer *, enum rid);
186 static cp_token *cp_lexer_consume_token
187   (cp_lexer *);
188 static void cp_lexer_purge_token
189   (cp_lexer *);
190 static void cp_lexer_purge_tokens_after
191   (cp_lexer *, cp_token_position);
192 static void cp_lexer_save_tokens
193   (cp_lexer *);
194 static void cp_lexer_commit_tokens
195   (cp_lexer *);
196 static void cp_lexer_rollback_tokens
197   (cp_lexer *);
198 #ifdef ENABLE_CHECKING
199 static void cp_lexer_print_token
200   (FILE *, cp_token *);
201 static inline bool cp_lexer_debugging_p
202   (cp_lexer *);
203 static void cp_lexer_start_debugging
204   (cp_lexer *) ATTRIBUTE_UNUSED;
205 static void cp_lexer_stop_debugging
206   (cp_lexer *) ATTRIBUTE_UNUSED;
207 #else
208 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
209    about passing NULL to functions that require non-NULL arguments
210    (fputs, fprintf).  It will never be used, so all we need is a value
211    of the right type that's guaranteed not to be NULL.  */
212 #define cp_lexer_debug_stream stdout
213 #define cp_lexer_print_token(str, tok) (void) 0
214 #define cp_lexer_debugging_p(lexer) 0
215 #endif /* ENABLE_CHECKING */
216
217 static cp_token_cache *cp_token_cache_new
218   (cp_token *, cp_token *);
219
220 static void cp_parser_initial_pragma
221   (cp_token *);
222
223 /* Manifest constants.  */
224 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
225 #define CP_SAVED_TOKEN_STACK 5
226
227 /* A token type for keywords, as opposed to ordinary identifiers.  */
228 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
229
230 /* A token type for template-ids.  If a template-id is processed while
231    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
232    the value of the CPP_TEMPLATE_ID is whatever was returned by
233    cp_parser_template_id.  */
234 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
235
236 /* A token type for nested-name-specifiers.  If a
237    nested-name-specifier is processed while parsing tentatively, it is
238    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
239    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
240    cp_parser_nested_name_specifier_opt.  */
241 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
242
243 /* A token type for tokens that are not tokens at all; these are used
244    to represent slots in the array where there used to be a token
245    that has now been deleted.  */
246 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
247
248 /* The number of token types, including C++-specific ones.  */
249 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
250
251 /* Variables.  */
252
253 #ifdef ENABLE_CHECKING
254 /* The stream to which debugging output should be written.  */
255 static FILE *cp_lexer_debug_stream;
256 #endif /* ENABLE_CHECKING */
257
258 /* Create a new main C++ lexer, the lexer that gets tokens from the
259    preprocessor.  */
260
261 static cp_lexer *
262 cp_lexer_new_main (void)
263 {
264   cp_token first_token;
265   cp_lexer *lexer;
266   cp_token *pos;
267   size_t alloc;
268   size_t space;
269   cp_token *buffer;
270
271   /* It's possible that parsing the first pragma will load a PCH file,
272      which is a GC collection point.  So we have to do that before
273      allocating any memory.  */
274   cp_parser_initial_pragma (&first_token);
275
276   c_common_no_more_pch ();
277
278   /* Allocate the memory.  */
279   lexer = GGC_CNEW (cp_lexer);
280
281 #ifdef ENABLE_CHECKING
282   /* Initially we are not debugging.  */
283   lexer->debugging_p = false;
284 #endif /* ENABLE_CHECKING */
285   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
286                                    CP_SAVED_TOKEN_STACK);
287
288   /* Create the buffer.  */
289   alloc = CP_LEXER_BUFFER_SIZE;
290   buffer = GGC_NEWVEC (cp_token, alloc);
291
292   /* Put the first token in the buffer.  */
293   space = alloc;
294   pos = buffer;
295   *pos = first_token;
296
297   /* Get the remaining tokens from the preprocessor.  */
298   while (pos->type != CPP_EOF)
299     {
300       pos++;
301       if (!--space)
302         {
303           space = alloc;
304           alloc *= 2;
305           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
306           pos = buffer + space;
307         }
308       cp_lexer_get_preprocessor_token (lexer, pos);
309     }
310   lexer->buffer = buffer;
311   lexer->buffer_length = alloc - space;
312   lexer->last_token = pos;
313   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
314
315   /* Subsequent preprocessor diagnostics should use compiler
316      diagnostic functions to get the compiler source location.  */
317   cpp_get_options (parse_in)->client_diagnostic = true;
318   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
319
320   gcc_assert (lexer->next_token->type != CPP_PURGED);
321   return lexer;
322 }
323
324 /* Create a new lexer whose token stream is primed with the tokens in
325    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
326
327 static cp_lexer *
328 cp_lexer_new_from_tokens (cp_token_cache *cache)
329 {
330   cp_token *first = cache->first;
331   cp_token *last = cache->last;
332   cp_lexer *lexer = GGC_CNEW (cp_lexer);
333
334   /* We do not own the buffer.  */
335   lexer->buffer = NULL;
336   lexer->buffer_length = 0;
337   lexer->next_token = first == last ? &eof_token : first;
338   lexer->last_token = last;
339
340   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
341                                    CP_SAVED_TOKEN_STACK);
342
343 #ifdef ENABLE_CHECKING
344   /* Initially we are not debugging.  */
345   lexer->debugging_p = false;
346 #endif
347
348   gcc_assert (lexer->next_token->type != CPP_PURGED);
349   return lexer;
350 }
351
352 /* Frees all resources associated with LEXER.  */
353
354 static void
355 cp_lexer_destroy (cp_lexer *lexer)
356 {
357   if (lexer->buffer)
358     ggc_free (lexer->buffer);
359   VEC_free (cp_token_position, heap, lexer->saved_tokens);
360   ggc_free (lexer);
361 }
362
363 /* Returns nonzero if debugging information should be output.  */
364
365 #ifdef ENABLE_CHECKING
366
367 static inline bool
368 cp_lexer_debugging_p (cp_lexer *lexer)
369 {
370   return lexer->debugging_p;
371 }
372
373 #endif /* ENABLE_CHECKING */
374
375 static inline cp_token_position
376 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
377 {
378   gcc_assert (!previous_p || lexer->next_token != &eof_token);
379
380   return lexer->next_token - previous_p;
381 }
382
383 static inline cp_token *
384 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 {
386   return pos;
387 }
388
389 /* nonzero if we are presently saving tokens.  */
390
391 static inline int
392 cp_lexer_saving_tokens (const cp_lexer* lexer)
393 {
394   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
395 }
396
397 /* Store the next token from the preprocessor in *TOKEN.  Return true
398    if we reach EOF.  If LEXER is NULL, assume we are handling an
399    initial #pragma pch_preprocess, and thus want the lexer to return
400    processed strings.  */
401
402 static void
403 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
404 {
405   static int is_extern_c = 0;
406
407    /* Get a new token from the preprocessor.  */
408   token->type
409     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
410                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
411   token->input_file_stack_index = input_file_stack_tick;
412   token->keyword = RID_MAX;
413   token->pragma_kind = PRAGMA_NONE;
414   token->in_system_header = in_system_header;
415
416   /* On some systems, some header files are surrounded by an
417      implicit extern "C" block.  Set a flag in the token if it
418      comes from such a header.  */
419   is_extern_c += pending_lang_change;
420   pending_lang_change = 0;
421   token->implicit_extern_c = is_extern_c > 0;
422
423   /* Check to see if this token is a keyword.  */
424   if (token->type == CPP_NAME)
425     {
426       if (C_IS_RESERVED_WORD (token->u.value))
427         {
428           /* Mark this token as a keyword.  */
429           token->type = CPP_KEYWORD;
430           /* Record which keyword.  */
431           token->keyword = C_RID_CODE (token->u.value);
432           /* Update the value.  Some keywords are mapped to particular
433              entities, rather than simply having the value of the
434              corresponding IDENTIFIER_NODE.  For example, `__const' is
435              mapped to `const'.  */
436           token->u.value = ridpointers[token->keyword];
437         }
438       else
439         {
440           if (warn_cxx0x_compat
441               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
442               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
443             {
444               /* Warn about the C++0x keyword (but still treat it as
445                  an identifier).  */
446               warning (OPT_Wc__0x_compat, 
447                        "identifier %<%s%> will become a keyword in C++0x",
448                        IDENTIFIER_POINTER (token->u.value));
449
450               /* Clear out the C_RID_CODE so we don't warn about this
451                  particular identifier-turned-keyword again.  */
452               C_RID_CODE (token->u.value) = RID_MAX;
453             }
454
455           token->ambiguous_p = false;
456           token->keyword = RID_MAX;
457         }
458     }
459   /* Handle Objective-C++ keywords.  */
460   else if (token->type == CPP_AT_NAME)
461     {
462       token->type = CPP_KEYWORD;
463       switch (C_RID_CODE (token->u.value))
464         {
465         /* Map 'class' to '@class', 'private' to '@private', etc.  */
466         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
467         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
468         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
469         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
470         case RID_THROW: token->keyword = RID_AT_THROW; break;
471         case RID_TRY: token->keyword = RID_AT_TRY; break;
472         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
473         default: token->keyword = C_RID_CODE (token->u.value);
474         }
475     }
476   else if (token->type == CPP_PRAGMA)
477     {
478       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
479       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
480       token->u.value = NULL_TREE;
481     }
482 }
483
484 /* Update the globals input_location and in_system_header and the
485    input file stack from TOKEN.  */
486 static inline void
487 cp_lexer_set_source_position_from_token (cp_token *token)
488 {
489   if (token->type != CPP_EOF)
490     {
491       input_location = token->location;
492       in_system_header = token->in_system_header;
493       restore_input_file_stack (token->input_file_stack_index);
494     }
495 }
496
497 /* Return a pointer to the next token in the token stream, but do not
498    consume it.  */
499
500 static inline cp_token *
501 cp_lexer_peek_token (cp_lexer *lexer)
502 {
503   if (cp_lexer_debugging_p (lexer))
504     {
505       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
506       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
507       putc ('\n', cp_lexer_debug_stream);
508     }
509   return lexer->next_token;
510 }
511
512 /* Return true if the next token has the indicated TYPE.  */
513
514 static inline bool
515 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
516 {
517   return cp_lexer_peek_token (lexer)->type == type;
518 }
519
520 /* Return true if the next token does not have the indicated TYPE.  */
521
522 static inline bool
523 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
524 {
525   return !cp_lexer_next_token_is (lexer, type);
526 }
527
528 /* Return true if the next token is the indicated KEYWORD.  */
529
530 static inline bool
531 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
532 {
533   return cp_lexer_peek_token (lexer)->keyword == keyword;
534 }
535
536 /* Return true if the next token is a keyword for a decl-specifier.  */
537
538 static bool
539 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
540 {
541   cp_token *token;
542
543   token = cp_lexer_peek_token (lexer);
544   switch (token->keyword) 
545     {
546       /* Storage classes.  */
547     case RID_AUTO:
548     case RID_REGISTER:
549     case RID_STATIC:
550     case RID_EXTERN:
551     case RID_MUTABLE:
552     case RID_THREAD:
553       /* Elaborated type specifiers.  */
554     case RID_ENUM:
555     case RID_CLASS:
556     case RID_STRUCT:
557     case RID_UNION:
558     case RID_TYPENAME:
559       /* Simple type specifiers.  */
560     case RID_CHAR:
561     case RID_WCHAR:
562     case RID_BOOL:
563     case RID_SHORT:
564     case RID_INT:
565     case RID_LONG:
566     case RID_SIGNED:
567     case RID_UNSIGNED:
568     case RID_FLOAT:
569     case RID_DOUBLE:
570     case RID_VOID:
571       /* GNU extensions.  */ 
572     case RID_ATTRIBUTE:
573     case RID_TYPEOF:
574       /* C++0x extensions.  */
575     case RID_DECLTYPE:
576       return true;
577
578     default:
579       return false;
580     }
581 }
582
583 /* Return a pointer to the Nth token in the token stream.  If N is 1,
584    then this is precisely equivalent to cp_lexer_peek_token (except
585    that it is not inline).  One would like to disallow that case, but
586    there is one case (cp_parser_nth_token_starts_template_id) where
587    the caller passes a variable for N and it might be 1.  */
588
589 static cp_token *
590 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
591 {
592   cp_token *token;
593
594   /* N is 1-based, not zero-based.  */
595   gcc_assert (n > 0);
596
597   if (cp_lexer_debugging_p (lexer))
598     fprintf (cp_lexer_debug_stream,
599              "cp_lexer: peeking ahead %ld at token: ", (long)n);
600
601   --n;
602   token = lexer->next_token;
603   gcc_assert (!n || token != &eof_token);
604   while (n != 0)
605     {
606       ++token;
607       if (token == lexer->last_token)
608         {
609           token = &eof_token;
610           break;
611         }
612
613       if (token->type != CPP_PURGED)
614         --n;
615     }
616
617   if (cp_lexer_debugging_p (lexer))
618     {
619       cp_lexer_print_token (cp_lexer_debug_stream, token);
620       putc ('\n', cp_lexer_debug_stream);
621     }
622
623   return token;
624 }
625
626 /* Return the next token, and advance the lexer's next_token pointer
627    to point to the next non-purged token.  */
628
629 static cp_token *
630 cp_lexer_consume_token (cp_lexer* lexer)
631 {
632   cp_token *token = lexer->next_token;
633
634   gcc_assert (token != &eof_token);
635   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
636
637   do
638     {
639       lexer->next_token++;
640       if (lexer->next_token == lexer->last_token)
641         {
642           lexer->next_token = &eof_token;
643           break;
644         }
645
646     }
647   while (lexer->next_token->type == CPP_PURGED);
648
649   cp_lexer_set_source_position_from_token (token);
650
651   /* Provide debugging output.  */
652   if (cp_lexer_debugging_p (lexer))
653     {
654       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
655       cp_lexer_print_token (cp_lexer_debug_stream, token);
656       putc ('\n', cp_lexer_debug_stream);
657     }
658
659   return token;
660 }
661
662 /* Permanently remove the next token from the token stream, and
663    advance the next_token pointer to refer to the next non-purged
664    token.  */
665
666 static void
667 cp_lexer_purge_token (cp_lexer *lexer)
668 {
669   cp_token *tok = lexer->next_token;
670
671   gcc_assert (tok != &eof_token);
672   tok->type = CPP_PURGED;
673   tok->location = UNKNOWN_LOCATION;
674   tok->u.value = NULL_TREE;
675   tok->keyword = RID_MAX;
676
677   do
678     {
679       tok++;
680       if (tok == lexer->last_token)
681         {
682           tok = &eof_token;
683           break;
684         }
685     }
686   while (tok->type == CPP_PURGED);
687   lexer->next_token = tok;
688 }
689
690 /* Permanently remove all tokens after TOK, up to, but not
691    including, the token that will be returned next by
692    cp_lexer_peek_token.  */
693
694 static void
695 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 {
697   cp_token *peek = lexer->next_token;
698
699   if (peek == &eof_token)
700     peek = lexer->last_token;
701
702   gcc_assert (tok < peek);
703
704   for ( tok += 1; tok != peek; tok += 1)
705     {
706       tok->type = CPP_PURGED;
707       tok->location = UNKNOWN_LOCATION;
708       tok->u.value = NULL_TREE;
709       tok->keyword = RID_MAX;
710     }
711 }
712
713 /* Begin saving tokens.  All tokens consumed after this point will be
714    preserved.  */
715
716 static void
717 cp_lexer_save_tokens (cp_lexer* lexer)
718 {
719   /* Provide debugging output.  */
720   if (cp_lexer_debugging_p (lexer))
721     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722
723   VEC_safe_push (cp_token_position, heap,
724                  lexer->saved_tokens, lexer->next_token);
725 }
726
727 /* Commit to the portion of the token stream most recently saved.  */
728
729 static void
730 cp_lexer_commit_tokens (cp_lexer* lexer)
731 {
732   /* Provide debugging output.  */
733   if (cp_lexer_debugging_p (lexer))
734     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735
736   VEC_pop (cp_token_position, lexer->saved_tokens);
737 }
738
739 /* Return all tokens saved since the last call to cp_lexer_save_tokens
740    to the token stream.  Stop saving tokens.  */
741
742 static void
743 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 {
745   /* Provide debugging output.  */
746   if (cp_lexer_debugging_p (lexer))
747     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748
749   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 }
751
752 /* Print a representation of the TOKEN on the STREAM.  */
753
754 #ifdef ENABLE_CHECKING
755
756 static void
757 cp_lexer_print_token (FILE * stream, cp_token *token)
758 {
759   /* We don't use cpp_type2name here because the parser defines
760      a few tokens of its own.  */
761   static const char *const token_names[] = {
762     /* cpplib-defined token types */
763 #define OP(e, s) #e,
764 #define TK(e, s) #e,
765     TTYPE_TABLE
766 #undef OP
767 #undef TK
768     /* C++ parser token types - see "Manifest constants", above.  */
769     "KEYWORD",
770     "TEMPLATE_ID",
771     "NESTED_NAME_SPECIFIER",
772     "PURGED"
773   };
774
775   /* If we have a name for the token, print it out.  Otherwise, we
776      simply give the numeric code.  */
777   gcc_assert (token->type < ARRAY_SIZE(token_names));
778   fputs (token_names[token->type], stream);
779
780   /* For some tokens, print the associated data.  */
781   switch (token->type)
782     {
783     case CPP_KEYWORD:
784       /* Some keywords have a value that is not an IDENTIFIER_NODE.
785          For example, `struct' is mapped to an INTEGER_CST.  */
786       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
787         break;
788       /* else fall through */
789     case CPP_NAME:
790       fputs (IDENTIFIER_POINTER (token->u.value), stream);
791       break;
792
793     case CPP_STRING:
794     case CPP_WSTRING:
795       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796       break;
797
798     default:
799       break;
800     }
801 }
802
803 /* Start emitting debugging information.  */
804
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
807 {
808   lexer->debugging_p = true;
809 }
810
811 /* Stop emitting debugging information.  */
812
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
815 {
816   lexer->debugging_p = false;
817 }
818
819 #endif /* ENABLE_CHECKING */
820
821 /* Create a new cp_token_cache, representing a range of tokens.  */
822
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
825 {
826   cp_token_cache *cache = GGC_NEW (cp_token_cache);
827   cache->first = first;
828   cache->last = last;
829   return cache;
830 }
831
832 \f
833 /* Decl-specifiers.  */
834
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
836
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
839 {
840   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 }
842
843 /* Declarators.  */
844
845 /* Nothing other than the parser should be creating declarators;
846    declarators are a semi-syntactic representation of C++ entities.
847    Other parts of the front end that need to create entities (like
848    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
849
850 static cp_declarator *make_call_declarator
851   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
852 static cp_declarator *make_array_declarator
853   (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855   (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857   (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859   (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861   (cp_cv_quals, tree, cp_declarator *);
862
863 /* An erroneous declarator.  */
864 static cp_declarator *cp_error_declarator;
865
866 /* The obstack on which declarators and related data structures are
867    allocated.  */
868 static struct obstack declarator_obstack;
869
870 /* Alloc BYTES from the declarator memory pool.  */
871
872 static inline void *
873 alloc_declarator (size_t bytes)
874 {
875   return obstack_alloc (&declarator_obstack, bytes);
876 }
877
878 /* Allocate a declarator of the indicated KIND.  Clear fields that are
879    common to all declarators.  */
880
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
883 {
884   cp_declarator *declarator;
885
886   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887   declarator->kind = kind;
888   declarator->attributes = NULL_TREE;
889   declarator->declarator = NULL;
890   declarator->parameter_pack_p = false;
891
892   return declarator;
893 }
894
895 /* Make a declarator for a generalized identifier.  If
896    QUALIFYING_SCOPE is non-NULL, the identifier is
897    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
899    is, if any.   */
900
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903                     special_function_kind sfk)
904 {
905   cp_declarator *declarator;
906
907   /* It is valid to write:
908
909        class C { void f(); };
910        typedef C D;
911        void D::f();
912
913      The standard is not clear about whether `typedef const C D' is
914      legal; as of 2002-09-15 the committee is considering that
915      question.  EDG 3.0 allows that syntax.  Therefore, we do as
916      well.  */
917   if (qualifying_scope && TYPE_P (qualifying_scope))
918     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
919
920   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
923
924   declarator = make_declarator (cdk_id);
925   declarator->u.id.qualifying_scope = qualifying_scope;
926   declarator->u.id.unqualified_name = unqualified_name;
927   declarator->u.id.sfk = sfk;
928   
929   return declarator;
930 }
931
932 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
933    of modifiers such as const or volatile to apply to the pointer
934    type, represented as identifiers.  */
935
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 {
939   cp_declarator *declarator;
940
941   declarator = make_declarator (cdk_pointer);
942   declarator->declarator = target;
943   declarator->u.pointer.qualifiers = cv_qualifiers;
944   declarator->u.pointer.class_type = NULL_TREE;
945   if (target)
946     {
947       declarator->parameter_pack_p = target->parameter_pack_p;
948       target->parameter_pack_p = false;
949     }
950   else
951     declarator->parameter_pack_p = false;
952
953   return declarator;
954 }
955
956 /* Like make_pointer_declarator -- but for references.  */
957
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960                            bool rvalue_ref)
961 {
962   cp_declarator *declarator;
963
964   declarator = make_declarator (cdk_reference);
965   declarator->declarator = target;
966   declarator->u.reference.qualifiers = cv_qualifiers;
967   declarator->u.reference.rvalue_ref = rvalue_ref;
968   if (target)
969     {
970       declarator->parameter_pack_p = target->parameter_pack_p;
971       target->parameter_pack_p = false;
972     }
973   else
974     declarator->parameter_pack_p = false;
975
976   return declarator;
977 }
978
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980    member of CLASS_TYPE.  */
981
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984                         cp_declarator *pointee)
985 {
986   cp_declarator *declarator;
987
988   declarator = make_declarator (cdk_ptrmem);
989   declarator->declarator = pointee;
990   declarator->u.pointer.qualifiers = cv_qualifiers;
991   declarator->u.pointer.class_type = class_type;
992
993   if (pointee)
994     {
995       declarator->parameter_pack_p = pointee->parameter_pack_p;
996       pointee->parameter_pack_p = false;
997     }
998   else
999     declarator->parameter_pack_p = false;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for the function given by TARGET, with the
1005    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1006    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1007    indicates what exceptions can be thrown.  */
1008
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011                       cp_parameter_declarator *parms,
1012                       cp_cv_quals cv_qualifiers,
1013                       tree exception_specification)
1014 {
1015   cp_declarator *declarator;
1016
1017   declarator = make_declarator (cdk_function);
1018   declarator->declarator = target;
1019   declarator->u.function.parameters = parms;
1020   declarator->u.function.qualifiers = cv_qualifiers;
1021   declarator->u.function.exception_specification = exception_specification;
1022   if (target)
1023     {
1024       declarator->parameter_pack_p = target->parameter_pack_p;
1025       target->parameter_pack_p = false;
1026     }
1027   else
1028     declarator->parameter_pack_p = false;
1029
1030   return declarator;
1031 }
1032
1033 /* Make a declarator for an array of BOUNDS elements, each of which is
1034    defined by ELEMENT.  */
1035
1036 cp_declarator *
1037 make_array_declarator (cp_declarator *element, tree bounds)
1038 {
1039   cp_declarator *declarator;
1040
1041   declarator = make_declarator (cdk_array);
1042   declarator->declarator = element;
1043   declarator->u.array.bounds = bounds;
1044   if (element)
1045     {
1046       declarator->parameter_pack_p = element->parameter_pack_p;
1047       element->parameter_pack_p = false;
1048     }
1049   else
1050     declarator->parameter_pack_p = false;
1051
1052   return declarator;
1053 }
1054
1055 /* Determine whether the declarator we've seen so far can be a
1056    parameter pack, when followed by an ellipsis.  */
1057 static bool 
1058 declarator_can_be_parameter_pack (cp_declarator *declarator)
1059 {
1060   /* Search for a declarator name, or any other declarator that goes
1061      after the point where the ellipsis could appear in a parameter
1062      pack. If we find any of these, then this declarator can not be
1063      made into a parameter pack.  */
1064   bool found = false;
1065   while (declarator && !found)
1066     {
1067       switch ((int)declarator->kind)
1068         {
1069         case cdk_id:
1070         case cdk_array:
1071           found = true;
1072           break;
1073
1074         case cdk_error:
1075           return true;
1076
1077         default:
1078           declarator = declarator->declarator;
1079           break;
1080         }
1081     }
1082
1083   return !found;
1084 }
1085
1086 cp_parameter_declarator *no_parameters;
1087
1088 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1089    DECLARATOR and DEFAULT_ARGUMENT.  */
1090
1091 cp_parameter_declarator *
1092 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1093                            cp_declarator *declarator,
1094                            tree default_argument)
1095 {
1096   cp_parameter_declarator *parameter;
1097
1098   parameter = ((cp_parameter_declarator *)
1099                alloc_declarator (sizeof (cp_parameter_declarator)));
1100   parameter->next = NULL;
1101   if (decl_specifiers)
1102     parameter->decl_specifiers = *decl_specifiers;
1103   else
1104     clear_decl_specs (&parameter->decl_specifiers);
1105   parameter->declarator = declarator;
1106   parameter->default_argument = default_argument;
1107   parameter->ellipsis_p = false;
1108
1109   return parameter;
1110 }
1111
1112 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1113
1114 static bool
1115 function_declarator_p (const cp_declarator *declarator)
1116 {
1117   while (declarator)
1118     {
1119       if (declarator->kind == cdk_function
1120           && declarator->declarator->kind == cdk_id)
1121         return true;
1122       if (declarator->kind == cdk_id
1123           || declarator->kind == cdk_error)
1124         return false;
1125       declarator = declarator->declarator;
1126     }
1127   return false;
1128 }
1129  
1130 /* The parser.  */
1131
1132 /* Overview
1133    --------
1134
1135    A cp_parser parses the token stream as specified by the C++
1136    grammar.  Its job is purely parsing, not semantic analysis.  For
1137    example, the parser breaks the token stream into declarators,
1138    expressions, statements, and other similar syntactic constructs.
1139    It does not check that the types of the expressions on either side
1140    of an assignment-statement are compatible, or that a function is
1141    not declared with a parameter of type `void'.
1142
1143    The parser invokes routines elsewhere in the compiler to perform
1144    semantic analysis and to build up the abstract syntax tree for the
1145    code processed.
1146
1147    The parser (and the template instantiation code, which is, in a
1148    way, a close relative of parsing) are the only parts of the
1149    compiler that should be calling push_scope and pop_scope, or
1150    related functions.  The parser (and template instantiation code)
1151    keeps track of what scope is presently active; everything else
1152    should simply honor that.  (The code that generates static
1153    initializers may also need to set the scope, in order to check
1154    access control correctly when emitting the initializers.)
1155
1156    Methodology
1157    -----------
1158
1159    The parser is of the standard recursive-descent variety.  Upcoming
1160    tokens in the token stream are examined in order to determine which
1161    production to use when parsing a non-terminal.  Some C++ constructs
1162    require arbitrary look ahead to disambiguate.  For example, it is
1163    impossible, in the general case, to tell whether a statement is an
1164    expression or declaration without scanning the entire statement.
1165    Therefore, the parser is capable of "parsing tentatively."  When the
1166    parser is not sure what construct comes next, it enters this mode.
1167    Then, while we attempt to parse the construct, the parser queues up
1168    error messages, rather than issuing them immediately, and saves the
1169    tokens it consumes.  If the construct is parsed successfully, the
1170    parser "commits", i.e., it issues any queued error messages and
1171    the tokens that were being preserved are permanently discarded.
1172    If, however, the construct is not parsed successfully, the parser
1173    rolls back its state completely so that it can resume parsing using
1174    a different alternative.
1175
1176    Future Improvements
1177    -------------------
1178
1179    The performance of the parser could probably be improved substantially.
1180    We could often eliminate the need to parse tentatively by looking ahead
1181    a little bit.  In some places, this approach might not entirely eliminate
1182    the need to parse tentatively, but it might still speed up the average
1183    case.  */
1184
1185 /* Flags that are passed to some parsing functions.  These values can
1186    be bitwise-ored together.  */
1187
1188 typedef enum cp_parser_flags
1189 {
1190   /* No flags.  */
1191   CP_PARSER_FLAGS_NONE = 0x0,
1192   /* The construct is optional.  If it is not present, then no error
1193      should be issued.  */
1194   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1195   /* When parsing a type-specifier, do not allow user-defined types.  */
1196   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1197 } cp_parser_flags;
1198
1199 /* The different kinds of declarators we want to parse.  */
1200
1201 typedef enum cp_parser_declarator_kind
1202 {
1203   /* We want an abstract declarator.  */
1204   CP_PARSER_DECLARATOR_ABSTRACT,
1205   /* We want a named declarator.  */
1206   CP_PARSER_DECLARATOR_NAMED,
1207   /* We don't mind, but the name must be an unqualified-id.  */
1208   CP_PARSER_DECLARATOR_EITHER
1209 } cp_parser_declarator_kind;
1210
1211 /* The precedence values used to parse binary expressions.  The minimum value
1212    of PREC must be 1, because zero is reserved to quickly discriminate
1213    binary operators from other tokens.  */
1214
1215 enum cp_parser_prec
1216 {
1217   PREC_NOT_OPERATOR,
1218   PREC_LOGICAL_OR_EXPRESSION,
1219   PREC_LOGICAL_AND_EXPRESSION,
1220   PREC_INCLUSIVE_OR_EXPRESSION,
1221   PREC_EXCLUSIVE_OR_EXPRESSION,
1222   PREC_AND_EXPRESSION,
1223   PREC_EQUALITY_EXPRESSION,
1224   PREC_RELATIONAL_EXPRESSION,
1225   PREC_SHIFT_EXPRESSION,
1226   PREC_ADDITIVE_EXPRESSION,
1227   PREC_MULTIPLICATIVE_EXPRESSION,
1228   PREC_PM_EXPRESSION,
1229   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1230 };
1231
1232 /* A mapping from a token type to a corresponding tree node type, with a
1233    precedence value.  */
1234
1235 typedef struct cp_parser_binary_operations_map_node
1236 {
1237   /* The token type.  */
1238   enum cpp_ttype token_type;
1239   /* The corresponding tree code.  */
1240   enum tree_code tree_type;
1241   /* The precedence of this operator.  */
1242   enum cp_parser_prec prec;
1243 } cp_parser_binary_operations_map_node;
1244
1245 /* The status of a tentative parse.  */
1246
1247 typedef enum cp_parser_status_kind
1248 {
1249   /* No errors have occurred.  */
1250   CP_PARSER_STATUS_KIND_NO_ERROR,
1251   /* An error has occurred.  */
1252   CP_PARSER_STATUS_KIND_ERROR,
1253   /* We are committed to this tentative parse, whether or not an error
1254      has occurred.  */
1255   CP_PARSER_STATUS_KIND_COMMITTED
1256 } cp_parser_status_kind;
1257
1258 typedef struct cp_parser_expression_stack_entry
1259 {
1260   /* Left hand side of the binary operation we are currently
1261      parsing.  */
1262   tree lhs;
1263   /* Original tree code for left hand side, if it was a binary
1264      expression itself (used for -Wparentheses).  */
1265   enum tree_code lhs_type;
1266   /* Tree code for the binary operation we are parsing.  */
1267   enum tree_code tree_type;
1268   /* Precedence of the binary operation we are parsing.  */
1269   int prec;
1270 } cp_parser_expression_stack_entry;
1271
1272 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1273    entries because precedence levels on the stack are monotonically
1274    increasing.  */
1275 typedef struct cp_parser_expression_stack_entry
1276   cp_parser_expression_stack[NUM_PREC_VALUES];
1277
1278 /* Context that is saved and restored when parsing tentatively.  */
1279 typedef struct cp_parser_context GTY (())
1280 {
1281   /* If this is a tentative parsing context, the status of the
1282      tentative parse.  */
1283   enum cp_parser_status_kind status;
1284   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1285      that are looked up in this context must be looked up both in the
1286      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1287      the context of the containing expression.  */
1288   tree object_type;
1289
1290   /* The next parsing context in the stack.  */
1291   struct cp_parser_context *next;
1292 } cp_parser_context;
1293
1294 /* Prototypes.  */
1295
1296 /* Constructors and destructors.  */
1297
1298 static cp_parser_context *cp_parser_context_new
1299   (cp_parser_context *);
1300
1301 /* Class variables.  */
1302
1303 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1304
1305 /* The operator-precedence table used by cp_parser_binary_expression.
1306    Transformed into an associative array (binops_by_token) by
1307    cp_parser_new.  */
1308
1309 static const cp_parser_binary_operations_map_node binops[] = {
1310   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1311   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1312
1313   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1314   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316
1317   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1318   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1319
1320   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1321   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1322
1323   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1324   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1326   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1327
1328   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1329   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1330
1331   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1332
1333   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1334
1335   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1336
1337   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1338
1339   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1340 };
1341
1342 /* The same as binops, but initialized by cp_parser_new so that
1343    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1344    for speed.  */
1345 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1346
1347 /* Constructors and destructors.  */
1348
1349 /* Construct a new context.  The context below this one on the stack
1350    is given by NEXT.  */
1351
1352 static cp_parser_context *
1353 cp_parser_context_new (cp_parser_context* next)
1354 {
1355   cp_parser_context *context;
1356
1357   /* Allocate the storage.  */
1358   if (cp_parser_context_free_list != NULL)
1359     {
1360       /* Pull the first entry from the free list.  */
1361       context = cp_parser_context_free_list;
1362       cp_parser_context_free_list = context->next;
1363       memset (context, 0, sizeof (*context));
1364     }
1365   else
1366     context = GGC_CNEW (cp_parser_context);
1367
1368   /* No errors have occurred yet in this context.  */
1369   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1370   /* If this is not the bottomost context, copy information that we
1371      need from the previous context.  */
1372   if (next)
1373     {
1374       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1375          expression, then we are parsing one in this context, too.  */
1376       context->object_type = next->object_type;
1377       /* Thread the stack.  */
1378       context->next = next;
1379     }
1380
1381   return context;
1382 }
1383
1384 /* The cp_parser structure represents the C++ parser.  */
1385
1386 typedef struct cp_parser GTY(())
1387 {
1388   /* The lexer from which we are obtaining tokens.  */
1389   cp_lexer *lexer;
1390
1391   /* The scope in which names should be looked up.  If NULL_TREE, then
1392      we look up names in the scope that is currently open in the
1393      source program.  If non-NULL, this is either a TYPE or
1394      NAMESPACE_DECL for the scope in which we should look.  It can
1395      also be ERROR_MARK, when we've parsed a bogus scope.
1396
1397      This value is not cleared automatically after a name is looked
1398      up, so we must be careful to clear it before starting a new look
1399      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1400      will look up `Z' in the scope of `X', rather than the current
1401      scope.)  Unfortunately, it is difficult to tell when name lookup
1402      is complete, because we sometimes peek at a token, look it up,
1403      and then decide not to consume it.   */
1404   tree scope;
1405
1406   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1407      last lookup took place.  OBJECT_SCOPE is used if an expression
1408      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1409      respectively.  QUALIFYING_SCOPE is used for an expression of the
1410      form "X::Y"; it refers to X.  */
1411   tree object_scope;
1412   tree qualifying_scope;
1413
1414   /* A stack of parsing contexts.  All but the bottom entry on the
1415      stack will be tentative contexts.
1416
1417      We parse tentatively in order to determine which construct is in
1418      use in some situations.  For example, in order to determine
1419      whether a statement is an expression-statement or a
1420      declaration-statement we parse it tentatively as a
1421      declaration-statement.  If that fails, we then reparse the same
1422      token stream as an expression-statement.  */
1423   cp_parser_context *context;
1424
1425   /* True if we are parsing GNU C++.  If this flag is not set, then
1426      GNU extensions are not recognized.  */
1427   bool allow_gnu_extensions_p;
1428
1429   /* TRUE if the `>' token should be interpreted as the greater-than
1430      operator.  FALSE if it is the end of a template-id or
1431      template-parameter-list. In C++0x mode, this flag also applies to
1432      `>>' tokens, which are viewed as two consecutive `>' tokens when
1433      this flag is FALSE.  */
1434   bool greater_than_is_operator_p;
1435
1436   /* TRUE if default arguments are allowed within a parameter list
1437      that starts at this point. FALSE if only a gnu extension makes
1438      them permissible.  */
1439   bool default_arg_ok_p;
1440
1441   /* TRUE if we are parsing an integral constant-expression.  See
1442      [expr.const] for a precise definition.  */
1443   bool integral_constant_expression_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression -- but a
1446      non-constant expression should be permitted as well.  This flag
1447      is used when parsing an array bound so that GNU variable-length
1448      arrays are tolerated.  */
1449   bool allow_non_integral_constant_expression_p;
1450
1451   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1452      been seen that makes the expression non-constant.  */
1453   bool non_integral_constant_expression_p;
1454
1455   /* TRUE if local variable names and `this' are forbidden in the
1456      current context.  */
1457   bool local_variables_forbidden_p;
1458
1459   /* TRUE if the declaration we are parsing is part of a
1460      linkage-specification of the form `extern string-literal
1461      declaration'.  */
1462   bool in_unbraced_linkage_specification_p;
1463
1464   /* TRUE if we are presently parsing a declarator, after the
1465      direct-declarator.  */
1466   bool in_declarator_p;
1467
1468   /* TRUE if we are presently parsing a template-argument-list.  */
1469   bool in_template_argument_list_p;
1470
1471   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1472      to IN_OMP_BLOCK if parsing OpenMP structured block and
1473      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1474      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1475      iteration-statement, OpenMP block or loop within that switch.  */
1476 #define IN_SWITCH_STMT          1
1477 #define IN_ITERATION_STMT       2
1478 #define IN_OMP_BLOCK            4
1479 #define IN_OMP_FOR              8
1480 #define IN_IF_STMT             16
1481   unsigned char in_statement;
1482
1483   /* TRUE if we are presently parsing the body of a switch statement.
1484      Note that this doesn't quite overlap with in_statement above.
1485      The difference relates to giving the right sets of error messages:
1486      "case not in switch" vs "break statement used with OpenMP...".  */
1487   bool in_switch_statement_p;
1488
1489   /* TRUE if we are parsing a type-id in an expression context.  In
1490      such a situation, both "type (expr)" and "type (type)" are valid
1491      alternatives.  */
1492   bool in_type_id_in_expr_p;
1493
1494   /* TRUE if we are currently in a header file where declarations are
1495      implicitly extern "C".  */
1496   bool implicit_extern_c;
1497
1498   /* TRUE if strings in expressions should be translated to the execution
1499      character set.  */
1500   bool translate_strings_p;
1501
1502   /* TRUE if we are presently parsing the body of a function, but not
1503      a local class.  */
1504   bool in_function_body;
1505
1506   /* If non-NULL, then we are parsing a construct where new type
1507      definitions are not permitted.  The string stored here will be
1508      issued as an error message if a type is defined.  */
1509   const char *type_definition_forbidden_message;
1510
1511   /* A list of lists. The outer list is a stack, used for member
1512      functions of local classes. At each level there are two sub-list,
1513      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1514      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1515      TREE_VALUE's. The functions are chained in reverse declaration
1516      order.
1517
1518      The TREE_PURPOSE sublist contains those functions with default
1519      arguments that need post processing, and the TREE_VALUE sublist
1520      contains those functions with definitions that need post
1521      processing.
1522
1523      These lists can only be processed once the outermost class being
1524      defined is complete.  */
1525   tree unparsed_functions_queues;
1526
1527   /* The number of classes whose definitions are currently in
1528      progress.  */
1529   unsigned num_classes_being_defined;
1530
1531   /* The number of template parameter lists that apply directly to the
1532      current declaration.  */
1533   unsigned num_template_parameter_lists;
1534 } cp_parser;
1535
1536 /* Prototypes.  */
1537
1538 /* Constructors and destructors.  */
1539
1540 static cp_parser *cp_parser_new
1541   (void);
1542
1543 /* Routines to parse various constructs.
1544
1545    Those that return `tree' will return the error_mark_node (rather
1546    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1547    Sometimes, they will return an ordinary node if error-recovery was
1548    attempted, even though a parse error occurred.  So, to check
1549    whether or not a parse error occurred, you should always use
1550    cp_parser_error_occurred.  If the construct is optional (indicated
1551    either by an `_opt' in the name of the function that does the
1552    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1553    the construct is not present.  */
1554
1555 /* Lexical conventions [gram.lex]  */
1556
1557 static tree cp_parser_identifier
1558   (cp_parser *);
1559 static tree cp_parser_string_literal
1560   (cp_parser *, bool, bool);
1561
1562 /* Basic concepts [gram.basic]  */
1563
1564 static bool cp_parser_translation_unit
1565   (cp_parser *);
1566
1567 /* Expressions [gram.expr]  */
1568
1569 static tree cp_parser_primary_expression
1570   (cp_parser *, bool, bool, bool, cp_id_kind *);
1571 static tree cp_parser_id_expression
1572   (cp_parser *, bool, bool, bool *, bool, bool);
1573 static tree cp_parser_unqualified_id
1574   (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier_opt
1576   (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_class_or_namespace_name
1580   (cp_parser *, bool, bool, bool, bool, bool);
1581 static tree cp_parser_postfix_expression
1582   (cp_parser *, bool, bool, bool);
1583 static tree cp_parser_postfix_open_square_expression
1584   (cp_parser *, tree, bool);
1585 static tree cp_parser_postfix_dot_deref_expression
1586   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1587 static tree cp_parser_parenthesized_expression_list
1588   (cp_parser *, bool, bool, bool, bool *);
1589 static void cp_parser_pseudo_destructor_name
1590   (cp_parser *, tree *, tree *);
1591 static tree cp_parser_unary_expression
1592   (cp_parser *, bool, bool);
1593 static enum tree_code cp_parser_unary_operator
1594   (cp_token *);
1595 static tree cp_parser_new_expression
1596   (cp_parser *);
1597 static tree cp_parser_new_placement
1598   (cp_parser *);
1599 static tree cp_parser_new_type_id
1600   (cp_parser *, tree *);
1601 static cp_declarator *cp_parser_new_declarator_opt
1602   (cp_parser *);
1603 static cp_declarator *cp_parser_direct_new_declarator
1604   (cp_parser *);
1605 static tree cp_parser_new_initializer
1606   (cp_parser *);
1607 static tree cp_parser_delete_expression
1608   (cp_parser *);
1609 static tree cp_parser_cast_expression
1610   (cp_parser *, bool, bool);
1611 static tree cp_parser_binary_expression
1612   (cp_parser *, bool);
1613 static tree cp_parser_question_colon_clause
1614   (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616   (cp_parser *, bool);
1617 static enum tree_code cp_parser_assignment_operator_opt
1618   (cp_parser *);
1619 static tree cp_parser_expression
1620   (cp_parser *, bool);
1621 static tree cp_parser_constant_expression
1622   (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1624   (cp_parser *);
1625
1626 /* Statements [gram.stmt.stmt]  */
1627
1628 static void cp_parser_statement
1629   (cp_parser *, tree, bool, bool *);
1630 static void cp_parser_label_for_labeled_statement
1631   (cp_parser *);
1632 static tree cp_parser_expression_statement
1633   (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635   (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637   (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1639   (cp_parser *, bool *);
1640 static tree cp_parser_condition
1641   (cp_parser *);
1642 static tree cp_parser_iteration_statement
1643   (cp_parser *);
1644 static void cp_parser_for_init_statement
1645   (cp_parser *);
1646 static tree cp_parser_jump_statement
1647   (cp_parser *);
1648 static void cp_parser_declaration_statement
1649   (cp_parser *);
1650
1651 static tree cp_parser_implicitly_scoped_statement
1652   (cp_parser *, bool *);
1653 static void cp_parser_already_scoped_statement
1654   (cp_parser *);
1655
1656 /* Declarations [gram.dcl.dcl] */
1657
1658 static void cp_parser_declaration_seq_opt
1659   (cp_parser *);
1660 static void cp_parser_declaration
1661   (cp_parser *);
1662 static void cp_parser_block_declaration
1663   (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665   (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1669   (cp_parser *);
1670 static tree cp_parser_function_specifier_opt
1671   (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1674    int *, bool *);
1675 static tree cp_parser_simple_type_specifier
1676   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1678   (cp_parser *);
1679 static tree cp_parser_elaborated_type_specifier
1680   (cp_parser *, bool, bool);
1681 static tree cp_parser_enum_specifier
1682   (cp_parser *);
1683 static void cp_parser_enumerator_list
1684   (cp_parser *, tree);
1685 static void cp_parser_enumerator_definition
1686   (cp_parser *, tree);
1687 static tree cp_parser_namespace_name
1688   (cp_parser *);
1689 static void cp_parser_namespace_definition
1690   (cp_parser *);
1691 static void cp_parser_namespace_body
1692   (cp_parser *);
1693 static tree cp_parser_qualified_namespace_specifier
1694   (cp_parser *);
1695 static void cp_parser_namespace_alias_definition
1696   (cp_parser *);
1697 static bool cp_parser_using_declaration
1698   (cp_parser *, bool);
1699 static void cp_parser_using_directive
1700   (cp_parser *);
1701 static void cp_parser_asm_definition
1702   (cp_parser *);
1703 static void cp_parser_linkage_specification
1704   (cp_parser *);
1705 static void cp_parser_static_assert
1706   (cp_parser *, bool);
1707 static tree cp_parser_decltype
1708   (cp_parser *);
1709
1710 /* Declarators [gram.dcl.decl] */
1711
1712 static tree cp_parser_init_declarator
1713   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1714 static cp_declarator *cp_parser_declarator
1715   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1716 static cp_declarator *cp_parser_direct_declarator
1717   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1718 static enum tree_code cp_parser_ptr_operator
1719   (cp_parser *, tree *, cp_cv_quals *);
1720 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1721   (cp_parser *);
1722 static tree cp_parser_declarator_id
1723   (cp_parser *, bool);
1724 static tree cp_parser_type_id
1725   (cp_parser *);
1726 static void cp_parser_type_specifier_seq
1727   (cp_parser *, bool, cp_decl_specifier_seq *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1729   (cp_parser *);
1730 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1731   (cp_parser *, bool *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration
1733   (cp_parser *, bool, bool *);
1734 static tree cp_parser_default_argument 
1735   (cp_parser *, bool);
1736 static void cp_parser_function_body
1737   (cp_parser *);
1738 static tree cp_parser_initializer
1739   (cp_parser *, bool *, bool *);
1740 static tree cp_parser_initializer_clause
1741   (cp_parser *, bool *);
1742 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1743   (cp_parser *, bool *);
1744
1745 static bool cp_parser_ctor_initializer_opt_and_function_body
1746   (cp_parser *);
1747
1748 /* Classes [gram.class] */
1749
1750 static tree cp_parser_class_name
1751   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1752 static tree cp_parser_class_specifier
1753   (cp_parser *);
1754 static tree cp_parser_class_head
1755   (cp_parser *, bool *, tree *, tree *);
1756 static enum tag_types cp_parser_class_key
1757   (cp_parser *);
1758 static void cp_parser_member_specification_opt
1759   (cp_parser *);
1760 static void cp_parser_member_declaration
1761   (cp_parser *);
1762 static tree cp_parser_pure_specifier
1763   (cp_parser *);
1764 static tree cp_parser_constant_initializer
1765   (cp_parser *);
1766
1767 /* Derived classes [gram.class.derived] */
1768
1769 static tree cp_parser_base_clause
1770   (cp_parser *);
1771 static tree cp_parser_base_specifier
1772   (cp_parser *);
1773
1774 /* Special member functions [gram.special] */
1775
1776 static tree cp_parser_conversion_function_id
1777   (cp_parser *);
1778 static tree cp_parser_conversion_type_id
1779   (cp_parser *);
1780 static cp_declarator *cp_parser_conversion_declarator_opt
1781   (cp_parser *);
1782 static bool cp_parser_ctor_initializer_opt
1783   (cp_parser *);
1784 static void cp_parser_mem_initializer_list
1785   (cp_parser *);
1786 static tree cp_parser_mem_initializer
1787   (cp_parser *);
1788 static tree cp_parser_mem_initializer_id
1789   (cp_parser *);
1790
1791 /* Overloading [gram.over] */
1792
1793 static tree cp_parser_operator_function_id
1794   (cp_parser *);
1795 static tree cp_parser_operator
1796   (cp_parser *);
1797
1798 /* Templates [gram.temp] */
1799
1800 static void cp_parser_template_declaration
1801   (cp_parser *, bool);
1802 static tree cp_parser_template_parameter_list
1803   (cp_parser *);
1804 static tree cp_parser_template_parameter
1805   (cp_parser *, bool *, bool *);
1806 static tree cp_parser_type_parameter
1807   (cp_parser *, bool *);
1808 static tree cp_parser_template_id
1809   (cp_parser *, bool, bool, bool);
1810 static tree cp_parser_template_name
1811   (cp_parser *, bool, bool, bool, bool *);
1812 static tree cp_parser_template_argument_list
1813   (cp_parser *);
1814 static tree cp_parser_template_argument
1815   (cp_parser *);
1816 static void cp_parser_explicit_instantiation
1817   (cp_parser *);
1818 static void cp_parser_explicit_specialization
1819   (cp_parser *);
1820
1821 /* Exception handling [gram.exception] */
1822
1823 static tree cp_parser_try_block
1824   (cp_parser *);
1825 static bool cp_parser_function_try_block
1826   (cp_parser *);
1827 static void cp_parser_handler_seq
1828   (cp_parser *);
1829 static void cp_parser_handler
1830   (cp_parser *);
1831 static tree cp_parser_exception_declaration
1832   (cp_parser *);
1833 static tree cp_parser_throw_expression
1834   (cp_parser *);
1835 static tree cp_parser_exception_specification_opt
1836   (cp_parser *);
1837 static tree cp_parser_type_id_list
1838   (cp_parser *);
1839
1840 /* GNU Extensions */
1841
1842 static tree cp_parser_asm_specification_opt
1843   (cp_parser *);
1844 static tree cp_parser_asm_operand_list
1845   (cp_parser *);
1846 static tree cp_parser_asm_clobber_list
1847   (cp_parser *);
1848 static tree cp_parser_attributes_opt
1849   (cp_parser *);
1850 static tree cp_parser_attribute_list
1851   (cp_parser *);
1852 static bool cp_parser_extension_opt
1853   (cp_parser *, int *);
1854 static void cp_parser_label_declaration
1855   (cp_parser *);
1856
1857 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1858 static bool cp_parser_pragma
1859   (cp_parser *, enum pragma_context);
1860
1861 /* Objective-C++ Productions */
1862
1863 static tree cp_parser_objc_message_receiver
1864   (cp_parser *);
1865 static tree cp_parser_objc_message_args
1866   (cp_parser *);
1867 static tree cp_parser_objc_message_expression
1868   (cp_parser *);
1869 static tree cp_parser_objc_encode_expression
1870   (cp_parser *);
1871 static tree cp_parser_objc_defs_expression
1872   (cp_parser *);
1873 static tree cp_parser_objc_protocol_expression
1874   (cp_parser *);
1875 static tree cp_parser_objc_selector_expression
1876   (cp_parser *);
1877 static tree cp_parser_objc_expression
1878   (cp_parser *);
1879 static bool cp_parser_objc_selector_p
1880   (enum cpp_ttype);
1881 static tree cp_parser_objc_selector
1882   (cp_parser *);
1883 static tree cp_parser_objc_protocol_refs_opt
1884   (cp_parser *);
1885 static void cp_parser_objc_declaration
1886   (cp_parser *);
1887 static tree cp_parser_objc_statement
1888   (cp_parser *);
1889
1890 /* Utility Routines */
1891
1892 static tree cp_parser_lookup_name
1893   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1894 static tree cp_parser_lookup_name_simple
1895   (cp_parser *, tree);
1896 static tree cp_parser_maybe_treat_template_as_class
1897   (tree, bool);
1898 static bool cp_parser_check_declarator_template_parameters
1899   (cp_parser *, cp_declarator *);
1900 static bool cp_parser_check_template_parameters
1901   (cp_parser *, unsigned);
1902 static tree cp_parser_simple_cast_expression
1903   (cp_parser *);
1904 static tree cp_parser_global_scope_opt
1905   (cp_parser *, bool);
1906 static bool cp_parser_constructor_declarator_p
1907   (cp_parser *, bool);
1908 static tree cp_parser_function_definition_from_specifiers_and_declarator
1909   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1910 static tree cp_parser_function_definition_after_declarator
1911   (cp_parser *, bool);
1912 static void cp_parser_template_declaration_after_export
1913   (cp_parser *, bool);
1914 static void cp_parser_perform_template_parameter_access_checks
1915   (VEC (deferred_access_check,gc)*);
1916 static tree cp_parser_single_declaration
1917   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1918 static tree cp_parser_functional_cast
1919   (cp_parser *, tree);
1920 static tree cp_parser_save_member_function_body
1921   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1922 static tree cp_parser_enclosed_template_argument_list
1923   (cp_parser *);
1924 static void cp_parser_save_default_args
1925   (cp_parser *, tree);
1926 static void cp_parser_late_parsing_for_member
1927   (cp_parser *, tree);
1928 static void cp_parser_late_parsing_default_args
1929   (cp_parser *, tree);
1930 static tree cp_parser_sizeof_operand
1931   (cp_parser *, enum rid);
1932 static tree cp_parser_trait_expr
1933   (cp_parser *, enum rid);
1934 static bool cp_parser_declares_only_class_p
1935   (cp_parser *);
1936 static void cp_parser_set_storage_class
1937   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1938 static void cp_parser_set_decl_spec_type
1939   (cp_decl_specifier_seq *, tree, bool);
1940 static bool cp_parser_friend_p
1941   (const cp_decl_specifier_seq *);
1942 static cp_token *cp_parser_require
1943   (cp_parser *, enum cpp_ttype, const char *);
1944 static cp_token *cp_parser_require_keyword
1945   (cp_parser *, enum rid, const char *);
1946 static bool cp_parser_token_starts_function_definition_p
1947   (cp_token *);
1948 static bool cp_parser_next_token_starts_class_definition_p
1949   (cp_parser *);
1950 static bool cp_parser_next_token_ends_template_argument_p
1951   (cp_parser *);
1952 static bool cp_parser_nth_token_starts_template_argument_list_p
1953   (cp_parser *, size_t);
1954 static enum tag_types cp_parser_token_is_class_key
1955   (cp_token *);
1956 static void cp_parser_check_class_key
1957   (enum tag_types, tree type);
1958 static void cp_parser_check_access_in_redeclaration
1959   (tree type);
1960 static bool cp_parser_optional_template_keyword
1961   (cp_parser *);
1962 static void cp_parser_pre_parsed_nested_name_specifier
1963   (cp_parser *);
1964 static void cp_parser_cache_group
1965   (cp_parser *, enum cpp_ttype, unsigned);
1966 static void cp_parser_parse_tentatively
1967   (cp_parser *);
1968 static void cp_parser_commit_to_tentative_parse
1969   (cp_parser *);
1970 static void cp_parser_abort_tentative_parse
1971   (cp_parser *);
1972 static bool cp_parser_parse_definitely
1973   (cp_parser *);
1974 static inline bool cp_parser_parsing_tentatively
1975   (cp_parser *);
1976 static bool cp_parser_uncommitted_to_tentative_parse_p
1977   (cp_parser *);
1978 static void cp_parser_error
1979   (cp_parser *, const char *);
1980 static void cp_parser_name_lookup_error
1981   (cp_parser *, tree, tree, const char *);
1982 static bool cp_parser_simulate_error
1983   (cp_parser *);
1984 static bool cp_parser_check_type_definition
1985   (cp_parser *);
1986 static void cp_parser_check_for_definition_in_return_type
1987   (cp_declarator *, tree);
1988 static void cp_parser_check_for_invalid_template_id
1989   (cp_parser *, tree);
1990 static bool cp_parser_non_integral_constant_expression
1991   (cp_parser *, const char *);
1992 static void cp_parser_diagnose_invalid_type_name
1993   (cp_parser *, tree, tree);
1994 static bool cp_parser_parse_and_diagnose_invalid_type_name
1995   (cp_parser *);
1996 static int cp_parser_skip_to_closing_parenthesis
1997   (cp_parser *, bool, bool, bool);
1998 static void cp_parser_skip_to_end_of_statement
1999   (cp_parser *);
2000 static void cp_parser_consume_semicolon_at_end_of_statement
2001   (cp_parser *);
2002 static void cp_parser_skip_to_end_of_block_or_statement
2003   (cp_parser *);
2004 static bool cp_parser_skip_to_closing_brace
2005   (cp_parser *);
2006 static void cp_parser_skip_to_end_of_template_parameter_list
2007   (cp_parser *);
2008 static void cp_parser_skip_to_pragma_eol
2009   (cp_parser*, cp_token *);
2010 static bool cp_parser_error_occurred
2011   (cp_parser *);
2012 static bool cp_parser_allow_gnu_extensions_p
2013   (cp_parser *);
2014 static bool cp_parser_is_string_literal
2015   (cp_token *);
2016 static bool cp_parser_is_keyword
2017   (cp_token *, enum rid);
2018 static tree cp_parser_make_typename_type
2019   (cp_parser *, tree, tree);
2020 static cp_declarator * cp_parser_make_indirect_declarator
2021   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2022
2023 /* Returns nonzero if we are parsing tentatively.  */
2024
2025 static inline bool
2026 cp_parser_parsing_tentatively (cp_parser* parser)
2027 {
2028   return parser->context->next != NULL;
2029 }
2030
2031 /* Returns nonzero if TOKEN is a string literal.  */
2032
2033 static bool
2034 cp_parser_is_string_literal (cp_token* token)
2035 {
2036   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2037 }
2038
2039 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2040
2041 static bool
2042 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2043 {
2044   return token->keyword == keyword;
2045 }
2046
2047 /* If not parsing tentatively, issue a diagnostic of the form
2048       FILE:LINE: MESSAGE before TOKEN
2049    where TOKEN is the next token in the input stream.  MESSAGE
2050    (specified by the caller) is usually of the form "expected
2051    OTHER-TOKEN".  */
2052
2053 static void
2054 cp_parser_error (cp_parser* parser, const char* message)
2055 {
2056   if (!cp_parser_simulate_error (parser))
2057     {
2058       cp_token *token = cp_lexer_peek_token (parser->lexer);
2059       /* This diagnostic makes more sense if it is tagged to the line
2060          of the token we just peeked at.  */
2061       cp_lexer_set_source_position_from_token (token);
2062
2063       if (token->type == CPP_PRAGMA)
2064         {
2065           error ("%<#pragma%> is not allowed here");
2066           cp_parser_skip_to_pragma_eol (parser, token);
2067           return;
2068         }
2069
2070       c_parse_error (message,
2071                      /* Because c_parser_error does not understand
2072                         CPP_KEYWORD, keywords are treated like
2073                         identifiers.  */
2074                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2075                      token->u.value);
2076     }
2077 }
2078
2079 /* Issue an error about name-lookup failing.  NAME is the
2080    IDENTIFIER_NODE DECL is the result of
2081    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2082    the thing that we hoped to find.  */
2083
2084 static void
2085 cp_parser_name_lookup_error (cp_parser* parser,
2086                              tree name,
2087                              tree decl,
2088                              const char* desired)
2089 {
2090   /* If name lookup completely failed, tell the user that NAME was not
2091      declared.  */
2092   if (decl == error_mark_node)
2093     {
2094       if (parser->scope && parser->scope != global_namespace)
2095         error ("%<%E::%E%> has not been declared",
2096                parser->scope, name);
2097       else if (parser->scope == global_namespace)
2098         error ("%<::%E%> has not been declared", name);
2099       else if (parser->object_scope
2100                && !CLASS_TYPE_P (parser->object_scope))
2101         error ("request for member %qE in non-class type %qT",
2102                name, parser->object_scope);
2103       else if (parser->object_scope)
2104         error ("%<%T::%E%> has not been declared",
2105                parser->object_scope, name);
2106       else
2107         error ("%qE has not been declared", name);
2108     }
2109   else if (parser->scope && parser->scope != global_namespace)
2110     error ("%<%E::%E%> %s", parser->scope, name, desired);
2111   else if (parser->scope == global_namespace)
2112     error ("%<::%E%> %s", name, desired);
2113   else
2114     error ("%qE %s", name, desired);
2115 }
2116
2117 /* If we are parsing tentatively, remember that an error has occurred
2118    during this tentative parse.  Returns true if the error was
2119    simulated; false if a message should be issued by the caller.  */
2120
2121 static bool
2122 cp_parser_simulate_error (cp_parser* parser)
2123 {
2124   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2125     {
2126       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2127       return true;
2128     }
2129   return false;
2130 }
2131
2132 /* Check for repeated decl-specifiers.  */
2133
2134 static void
2135 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2136 {
2137   cp_decl_spec ds;
2138
2139   for (ds = ds_first; ds != ds_last; ++ds)
2140     {
2141       unsigned count = decl_specs->specs[(int)ds];
2142       if (count < 2)
2143         continue;
2144       /* The "long" specifier is a special case because of "long long".  */
2145       if (ds == ds_long)
2146         {
2147           if (count > 2)
2148             error ("%<long long long%> is too long for GCC");
2149           else if (pedantic && !in_system_header && warn_long_long
2150                    && cxx_dialect == cxx98)
2151             pedwarn ("ISO C++ 1998 does not support %<long long%>");
2152         }
2153       else if (count > 1)
2154         {
2155           static const char *const decl_spec_names[] = {
2156             "signed",
2157             "unsigned",
2158             "short",
2159             "long",
2160             "const",
2161             "volatile",
2162             "restrict",
2163             "inline",
2164             "virtual",
2165             "explicit",
2166             "friend",
2167             "typedef",
2168             "__complex",
2169             "__thread"
2170           };
2171           error ("duplicate %qs", decl_spec_names[(int)ds]);
2172         }
2173     }
2174 }
2175
2176 /* This function is called when a type is defined.  If type
2177    definitions are forbidden at this point, an error message is
2178    issued.  */
2179
2180 static bool
2181 cp_parser_check_type_definition (cp_parser* parser)
2182 {
2183   /* If types are forbidden here, issue a message.  */
2184   if (parser->type_definition_forbidden_message)
2185     {
2186       /* Use `%s' to print the string in case there are any escape
2187          characters in the message.  */
2188       error ("%s", parser->type_definition_forbidden_message);
2189       return false;
2190     }
2191   return true;
2192 }
2193
2194 /* This function is called when the DECLARATOR is processed.  The TYPE
2195    was a type defined in the decl-specifiers.  If it is invalid to
2196    define a type in the decl-specifiers for DECLARATOR, an error is
2197    issued.  */
2198
2199 static void
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2201                                                tree type)
2202 {
2203   /* [dcl.fct] forbids type definitions in return types.
2204      Unfortunately, it's not easy to know whether or not we are
2205      processing a return type until after the fact.  */
2206   while (declarator
2207          && (declarator->kind == cdk_pointer
2208              || declarator->kind == cdk_reference
2209              || declarator->kind == cdk_ptrmem))
2210     declarator = declarator->declarator;
2211   if (declarator
2212       && declarator->kind == cdk_function)
2213     {
2214       error ("new types may not be defined in a return type");
2215       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2216               type);
2217     }
2218 }
2219
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221    "<" in any valid C++ program.  If the next token is indeed "<",
2222    issue a message warning the user about what appears to be an
2223    invalid attempt to form a template-id.  */
2224
2225 static void
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2227                                          tree type)
2228 {
2229   cp_token_position start = 0;
2230
2231   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2232     {
2233       if (TYPE_P (type))
2234         error ("%qT is not a template", type);
2235       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236         error ("%qE is not a template", type);
2237       else
2238         error ("invalid template-id");
2239       /* Remember the location of the invalid "<".  */
2240       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241         start = cp_lexer_token_position (parser->lexer, true);
2242       /* Consume the "<".  */
2243       cp_lexer_consume_token (parser->lexer);
2244       /* Parse the template arguments.  */
2245       cp_parser_enclosed_template_argument_list (parser);
2246       /* Permanently remove the invalid template arguments so that
2247          this error message is not issued again.  */
2248       if (start)
2249         cp_lexer_purge_tokens_after (parser->lexer, start);
2250     }
2251 }
2252
2253 /* If parsing an integral constant-expression, issue an error message
2254    about the fact that THING appeared and return true.  Otherwise,
2255    return false.  In either case, set
2256    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2257
2258 static bool
2259 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2260                                             const char *thing)
2261 {
2262   parser->non_integral_constant_expression_p = true;
2263   if (parser->integral_constant_expression_p)
2264     {
2265       if (!parser->allow_non_integral_constant_expression_p)
2266         {
2267           error ("%s cannot appear in a constant-expression", thing);
2268           return true;
2269         }
2270     }
2271   return false;
2272 }
2273
2274 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2275    qualifying scope (or NULL, if none) for ID.  This function commits
2276    to the current active tentative parse, if any.  (Otherwise, the
2277    problematic construct might be encountered again later, resulting
2278    in duplicate error messages.)  */
2279
2280 static void
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2282 {
2283   tree decl, old_scope;
2284   /* Try to lookup the identifier.  */
2285   old_scope = parser->scope;
2286   parser->scope = scope;
2287   decl = cp_parser_lookup_name_simple (parser, id);
2288   parser->scope = old_scope;
2289   /* If the lookup found a template-name, it means that the user forgot
2290   to specify an argument list. Emit a useful error message.  */
2291   if (TREE_CODE (decl) == TEMPLATE_DECL)
2292     error ("invalid use of template-name %qE without an argument list", decl);
2293   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294     error ("invalid use of destructor %qD as a type", id);
2295   else if (TREE_CODE (decl) == TYPE_DECL)
2296     /* Something like 'unsigned A a;'  */
2297     error ("invalid combination of multiple type-specifiers");
2298   else if (!parser->scope)
2299     {
2300       /* Issue an error message.  */
2301       error ("%qE does not name a type", id);
2302       /* If we're in a template class, it's possible that the user was
2303          referring to a type from a base class.  For example:
2304
2305            template <typename T> struct A { typedef T X; };
2306            template <typename T> struct B : public A<T> { X x; };
2307
2308          The user should have said "typename A<T>::X".  */
2309       if (processing_template_decl && current_class_type
2310           && TYPE_BINFO (current_class_type))
2311         {
2312           tree b;
2313
2314           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2315                b;
2316                b = TREE_CHAIN (b))
2317             {
2318               tree base_type = BINFO_TYPE (b);
2319               if (CLASS_TYPE_P (base_type)
2320                   && dependent_type_p (base_type))
2321                 {
2322                   tree field;
2323                   /* Go from a particular instantiation of the
2324                      template (which will have an empty TYPE_FIELDs),
2325                      to the main version.  */
2326                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327                   for (field = TYPE_FIELDS (base_type);
2328                        field;
2329                        field = TREE_CHAIN (field))
2330                     if (TREE_CODE (field) == TYPE_DECL
2331                         && DECL_NAME (field) == id)
2332                       {
2333                         inform ("(perhaps %<typename %T::%E%> was intended)",
2334                                 BINFO_TYPE (b), id);
2335                         break;
2336                       }
2337                   if (field)
2338                     break;
2339                 }
2340             }
2341         }
2342     }
2343   /* Here we diagnose qualified-ids where the scope is actually correct,
2344      but the identifier does not resolve to a valid type name.  */
2345   else if (parser->scope != error_mark_node)
2346     {
2347       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348         error ("%qE in namespace %qE does not name a type",
2349                id, parser->scope);
2350       else if (TYPE_P (parser->scope))
2351         error ("%qE in class %qT does not name a type", id, parser->scope);
2352       else
2353         gcc_unreachable ();
2354     }
2355   cp_parser_commit_to_tentative_parse (parser);
2356 }
2357
2358 /* Check for a common situation where a type-name should be present,
2359    but is not, and issue a sensible error message.  Returns true if an
2360    invalid type-name was detected.
2361
2362    The situation handled by this function are variable declarations of the
2363    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364    Usually, `ID' should name a type, but if we got here it means that it
2365    does not. We try to emit the best possible error message depending on
2366    how exactly the id-expression looks like.  */
2367
2368 static bool
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2370 {
2371   tree id;
2372
2373   cp_parser_parse_tentatively (parser);
2374   id = cp_parser_id_expression (parser,
2375                                 /*template_keyword_p=*/false,
2376                                 /*check_dependency_p=*/true,
2377                                 /*template_p=*/NULL,
2378                                 /*declarator_p=*/true,
2379                                 /*optional_p=*/false);
2380   /* After the id-expression, there should be a plain identifier,
2381      otherwise this is not a simple variable declaration. Also, if
2382      the scope is dependent, we cannot do much.  */
2383   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384       || (parser->scope && TYPE_P (parser->scope)
2385           && dependent_type_p (parser->scope))
2386       || TREE_CODE (id) == TYPE_DECL)
2387     {
2388       cp_parser_abort_tentative_parse (parser);
2389       return false;
2390     }
2391   if (!cp_parser_parse_definitely (parser))
2392     return false;
2393
2394   /* Emit a diagnostic for the invalid type.  */
2395   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396   /* Skip to the end of the declaration; there's no point in
2397      trying to process it.  */
2398   cp_parser_skip_to_end_of_block_or_statement (parser);
2399   return true;
2400 }
2401
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2404    are doing error recovery. Returns -1 if OR_COMMA is true and we
2405    found an unnested comma.  */
2406
2407 static int
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2409                                        bool recovering,
2410                                        bool or_comma,
2411                                        bool consume_paren)
2412 {
2413   unsigned paren_depth = 0;
2414   unsigned brace_depth = 0;
2415
2416   if (recovering && !or_comma
2417       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2418     return 0;
2419
2420   while (true)
2421     {
2422       cp_token * token = cp_lexer_peek_token (parser->lexer);
2423
2424       switch (token->type)
2425         {
2426         case CPP_EOF:
2427         case CPP_PRAGMA_EOL:
2428           /* If we've run out of tokens, then there is no closing `)'.  */
2429           return 0;
2430
2431         case CPP_SEMICOLON:
2432           /* This matches the processing in skip_to_end_of_statement.  */
2433           if (!brace_depth)
2434             return 0;
2435           break;
2436
2437         case CPP_OPEN_BRACE:
2438           ++brace_depth;
2439           break;
2440         case CPP_CLOSE_BRACE:
2441           if (!brace_depth--)
2442             return 0;
2443           break;
2444
2445         case CPP_COMMA:
2446           if (recovering && or_comma && !brace_depth && !paren_depth)
2447             return -1;
2448           break;
2449
2450         case CPP_OPEN_PAREN:
2451           if (!brace_depth)
2452             ++paren_depth;
2453           break;
2454
2455         case CPP_CLOSE_PAREN:
2456           if (!brace_depth && !paren_depth--)
2457             {
2458               if (consume_paren)
2459                 cp_lexer_consume_token (parser->lexer);
2460               return 1;
2461             }
2462           break;
2463
2464         default:
2465           break;
2466         }
2467
2468       /* Consume the token.  */
2469       cp_lexer_consume_token (parser->lexer);
2470     }
2471 }
2472
2473 /* Consume tokens until we reach the end of the current statement.
2474    Normally, that will be just before consuming a `;'.  However, if a
2475    non-nested `}' comes first, then we stop before consuming that.  */
2476
2477 static void
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2479 {
2480   unsigned nesting_depth = 0;
2481
2482   while (true)
2483     {
2484       cp_token *token = cp_lexer_peek_token (parser->lexer);
2485
2486       switch (token->type)
2487         {
2488         case CPP_EOF:
2489         case CPP_PRAGMA_EOL:
2490           /* If we've run out of tokens, stop.  */
2491           return;
2492
2493         case CPP_SEMICOLON:
2494           /* If the next token is a `;', we have reached the end of the
2495              statement.  */
2496           if (!nesting_depth)
2497             return;
2498           break;
2499
2500         case CPP_CLOSE_BRACE:
2501           /* If this is a non-nested '}', stop before consuming it.
2502              That way, when confronted with something like:
2503
2504                { 3 + }
2505
2506              we stop before consuming the closing '}', even though we
2507              have not yet reached a `;'.  */
2508           if (nesting_depth == 0)
2509             return;
2510
2511           /* If it is the closing '}' for a block that we have
2512              scanned, stop -- but only after consuming the token.
2513              That way given:
2514
2515                 void f g () { ... }
2516                 typedef int I;
2517
2518              we will stop after the body of the erroneously declared
2519              function, but before consuming the following `typedef'
2520              declaration.  */
2521           if (--nesting_depth == 0)
2522             {
2523               cp_lexer_consume_token (parser->lexer);
2524               return;
2525             }
2526
2527         case CPP_OPEN_BRACE:
2528           ++nesting_depth;
2529           break;
2530
2531         default:
2532           break;
2533         }
2534
2535       /* Consume the token.  */
2536       cp_lexer_consume_token (parser->lexer);
2537     }
2538 }
2539
2540 /* This function is called at the end of a statement or declaration.
2541    If the next token is a semicolon, it is consumed; otherwise, error
2542    recovery is attempted.  */
2543
2544 static void
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2546 {
2547   /* Look for the trailing `;'.  */
2548   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2549     {
2550       /* If there is additional (erroneous) input, skip to the end of
2551          the statement.  */
2552       cp_parser_skip_to_end_of_statement (parser);
2553       /* If the next token is now a `;', consume it.  */
2554       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555         cp_lexer_consume_token (parser->lexer);
2556     }
2557 }
2558
2559 /* Skip tokens until we have consumed an entire block, or until we
2560    have consumed a non-nested `;'.  */
2561
2562 static void
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2564 {
2565   int nesting_depth = 0;
2566
2567   while (nesting_depth >= 0)
2568     {
2569       cp_token *token = cp_lexer_peek_token (parser->lexer);
2570
2571       switch (token->type)
2572         {
2573         case CPP_EOF:
2574         case CPP_PRAGMA_EOL:
2575           /* If we've run out of tokens, stop.  */
2576           return;
2577
2578         case CPP_SEMICOLON:
2579           /* Stop if this is an unnested ';'. */
2580           if (!nesting_depth)
2581             nesting_depth = -1;
2582           break;
2583
2584         case CPP_CLOSE_BRACE:
2585           /* Stop if this is an unnested '}', or closes the outermost
2586              nesting level.  */
2587           nesting_depth--;
2588           if (!nesting_depth)
2589             nesting_depth = -1;
2590           break;
2591
2592         case CPP_OPEN_BRACE:
2593           /* Nest. */
2594           nesting_depth++;
2595           break;
2596
2597         default:
2598           break;
2599         }
2600
2601       /* Consume the token.  */
2602       cp_lexer_consume_token (parser->lexer);
2603     }
2604 }
2605
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607    token, or there are no more tokens. Return true in the first case,
2608    false otherwise.  */
2609
2610 static bool
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2612 {
2613   unsigned nesting_depth = 0;
2614
2615   while (true)
2616     {
2617       cp_token *token = cp_lexer_peek_token (parser->lexer);
2618
2619       switch (token->type)
2620         {
2621         case CPP_EOF:
2622         case CPP_PRAGMA_EOL:
2623           /* If we've run out of tokens, stop.  */
2624           return false;
2625
2626         case CPP_CLOSE_BRACE:
2627           /* If the next token is a non-nested `}', then we have reached
2628              the end of the current block.  */
2629           if (nesting_depth-- == 0)
2630             return true;
2631           break;
2632
2633         case CPP_OPEN_BRACE:
2634           /* If it the next token is a `{', then we are entering a new
2635              block.  Consume the entire block.  */
2636           ++nesting_depth;
2637           break;
2638
2639         default:
2640           break;
2641         }
2642
2643       /* Consume the token.  */
2644       cp_lexer_consume_token (parser->lexer);
2645     }
2646 }
2647
2648 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2649    parameter is the PRAGMA token, allowing us to purge the entire pragma
2650    sequence.  */
2651
2652 static void
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2654 {
2655   cp_token *token;
2656
2657   parser->lexer->in_pragma = false;
2658
2659   do
2660     token = cp_lexer_consume_token (parser->lexer);
2661   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2662
2663   /* Ensure that the pragma is not parsed again.  */
2664   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2665 }
2666
2667 /* Require pragma end of line, resyncing with it as necessary.  The
2668    arguments are as for cp_parser_skip_to_pragma_eol.  */
2669
2670 static void
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2672 {
2673   parser->lexer->in_pragma = false;
2674   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2676 }
2677
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679    an unresolved identifier node, we can provide a superior diagnostic
2680    using cp_parser_diagnose_invalid_type_name.  */
2681
2682 static tree
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2684 {
2685   tree result;
2686   if (TREE_CODE (id) == IDENTIFIER_NODE)
2687     {
2688       result = make_typename_type (scope, id, typename_type,
2689                                    /*complain=*/tf_none);
2690       if (result == error_mark_node)
2691         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2692       return result;
2693     }
2694   return make_typename_type (scope, id, typename_type, tf_error);
2695 }
2696
2697 /* This is a wrapper around the
2698    make_{pointer,ptrmem,reference}_declarator functions that decides
2699    which one to call based on the CODE and CLASS_TYPE arguments. The
2700    CODE argument should be one of the values returned by
2701    cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704                                     cp_cv_quals cv_qualifiers,
2705                                     cp_declarator *target)
2706 {
2707   if (code == ERROR_MARK)
2708     return cp_error_declarator;
2709
2710   if (code == INDIRECT_REF)
2711     if (class_type == NULL_TREE)
2712       return make_pointer_declarator (cv_qualifiers, target);
2713     else
2714       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716     return make_reference_declarator (cv_qualifiers, target, false);
2717   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718     return make_reference_declarator (cv_qualifiers, target, true);
2719   gcc_unreachable ();
2720 }
2721
2722 /* Create a new C++ parser.  */
2723
2724 static cp_parser *
2725 cp_parser_new (void)
2726 {
2727   cp_parser *parser;
2728   cp_lexer *lexer;
2729   unsigned i;
2730
2731   /* cp_lexer_new_main is called before calling ggc_alloc because
2732      cp_lexer_new_main might load a PCH file.  */
2733   lexer = cp_lexer_new_main ();
2734
2735   /* Initialize the binops_by_token so that we can get the tree
2736      directly from the token.  */
2737   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738     binops_by_token[binops[i].token_type] = binops[i];
2739
2740   parser = GGC_CNEW (cp_parser);
2741   parser->lexer = lexer;
2742   parser->context = cp_parser_context_new (NULL);
2743
2744   /* For now, we always accept GNU extensions.  */
2745   parser->allow_gnu_extensions_p = 1;
2746
2747   /* The `>' token is a greater-than operator, not the end of a
2748      template-id.  */
2749   parser->greater_than_is_operator_p = true;
2750
2751   parser->default_arg_ok_p = true;
2752
2753   /* We are not parsing a constant-expression.  */
2754   parser->integral_constant_expression_p = false;
2755   parser->allow_non_integral_constant_expression_p = false;
2756   parser->non_integral_constant_expression_p = false;
2757
2758   /* Local variable names are not forbidden.  */
2759   parser->local_variables_forbidden_p = false;
2760
2761   /* We are not processing an `extern "C"' declaration.  */
2762   parser->in_unbraced_linkage_specification_p = false;
2763
2764   /* We are not processing a declarator.  */
2765   parser->in_declarator_p = false;
2766
2767   /* We are not processing a template-argument-list.  */
2768   parser->in_template_argument_list_p = false;
2769
2770   /* We are not in an iteration statement.  */
2771   parser->in_statement = 0;
2772
2773   /* We are not in a switch statement.  */
2774   parser->in_switch_statement_p = false;
2775
2776   /* We are not parsing a type-id inside an expression.  */
2777   parser->in_type_id_in_expr_p = false;
2778
2779   /* Declarations aren't implicitly extern "C".  */
2780   parser->implicit_extern_c = false;
2781
2782   /* String literals should be translated to the execution character set.  */
2783   parser->translate_strings_p = true;
2784
2785   /* We are not parsing a function body.  */
2786   parser->in_function_body = false;
2787
2788   /* The unparsed function queue is empty.  */
2789   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2790
2791   /* There are no classes being defined.  */
2792   parser->num_classes_being_defined = 0;
2793
2794   /* No template parameters apply.  */
2795   parser->num_template_parameter_lists = 0;
2796
2797   return parser;
2798 }
2799
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801    and push it onto the parser's lexer stack.  This is used for delayed
2802    parsing of in-class method bodies and default arguments, and should
2803    not be confused with tentative parsing.  */
2804 static void
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2806 {
2807   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808   lexer->next = parser->lexer;
2809   parser->lexer = lexer;
2810
2811   /* Move the current source position to that of the first token in the
2812      new lexer.  */
2813   cp_lexer_set_source_position_from_token (lexer->next_token);
2814 }
2815
2816 /* Pop the top lexer off the parser stack.  This is never used for the
2817    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2818 static void
2819 cp_parser_pop_lexer (cp_parser *parser)
2820 {
2821   cp_lexer *lexer = parser->lexer;
2822   parser->lexer = lexer->next;
2823   cp_lexer_destroy (lexer);
2824
2825   /* Put the current source position back where it was before this
2826      lexer was pushed.  */
2827   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2828 }
2829
2830 /* Lexical conventions [gram.lex]  */
2831
2832 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2833    identifier.  */
2834
2835 static tree
2836 cp_parser_identifier (cp_parser* parser)
2837 {
2838   cp_token *token;
2839
2840   /* Look for the identifier.  */
2841   token = cp_parser_require (parser, CPP_NAME, "identifier");
2842   /* Return the value.  */
2843   return token ? token->u.value : error_mark_node;
2844 }
2845
2846 /* Parse a sequence of adjacent string constants.  Returns a
2847    TREE_STRING representing the combined, nul-terminated string
2848    constant.  If TRANSLATE is true, translate the string to the
2849    execution character set.  If WIDE_OK is true, a wide string is
2850    invalid here.
2851
2852    C++98 [lex.string] says that if a narrow string literal token is
2853    adjacent to a wide string literal token, the behavior is undefined.
2854    However, C99 6.4.5p4 says that this results in a wide string literal.
2855    We follow C99 here, for consistency with the C front end.
2856
2857    This code is largely lifted from lex_string() in c-lex.c.
2858
2859    FUTURE: ObjC++ will need to handle @-strings here.  */
2860 static tree
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2862 {
2863   tree value;
2864   bool wide = false;
2865   size_t count;
2866   struct obstack str_ob;
2867   cpp_string str, istr, *strs;
2868   cp_token *tok;
2869
2870   tok = cp_lexer_peek_token (parser->lexer);
2871   if (!cp_parser_is_string_literal (tok))
2872     {
2873       cp_parser_error (parser, "expected string-literal");
2874       return error_mark_node;
2875     }
2876
2877   /* Try to avoid the overhead of creating and destroying an obstack
2878      for the common case of just one string.  */
2879   if (!cp_parser_is_string_literal
2880       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2881     {
2882       cp_lexer_consume_token (parser->lexer);
2883
2884       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885       str.len = TREE_STRING_LENGTH (tok->u.value);
2886       count = 1;
2887       if (tok->type == CPP_WSTRING)
2888         wide = true;
2889
2890       strs = &str;
2891     }
2892   else
2893     {
2894       gcc_obstack_init (&str_ob);
2895       count = 0;
2896
2897       do
2898         {
2899           cp_lexer_consume_token (parser->lexer);
2900           count++;
2901           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902           str.len = TREE_STRING_LENGTH (tok->u.value);
2903           if (tok->type == CPP_WSTRING)
2904             wide = true;
2905
2906           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2907
2908           tok = cp_lexer_peek_token (parser->lexer);
2909         }
2910       while (cp_parser_is_string_literal (tok));
2911
2912       strs = (cpp_string *) obstack_finish (&str_ob);
2913     }
2914
2915   if (wide && !wide_ok)
2916     {
2917       cp_parser_error (parser, "a wide string is invalid in this context");
2918       wide = false;
2919     }
2920
2921   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922       (parse_in, strs, count, &istr, wide))
2923     {
2924       value = build_string (istr.len, (const char *)istr.text);
2925       free (CONST_CAST (unsigned char *, istr.text));
2926
2927       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928       value = fix_string_type (value);
2929     }
2930   else
2931     /* cpp_interpret_string has issued an error.  */
2932     value = error_mark_node;
2933
2934   if (count > 1)
2935     obstack_free (&str_ob, 0);
2936
2937   return value;
2938 }
2939
2940
2941 /* Basic concepts [gram.basic]  */
2942
2943 /* Parse a translation-unit.
2944
2945    translation-unit:
2946      declaration-seq [opt]
2947
2948    Returns TRUE if all went well.  */
2949
2950 static bool
2951 cp_parser_translation_unit (cp_parser* parser)
2952 {
2953   /* The address of the first non-permanent object on the declarator
2954      obstack.  */
2955   static void *declarator_obstack_base;
2956
2957   bool success;
2958
2959   /* Create the declarator obstack, if necessary.  */
2960   if (!cp_error_declarator)
2961     {
2962       gcc_obstack_init (&declarator_obstack);
2963       /* Create the error declarator.  */
2964       cp_error_declarator = make_declarator (cdk_error);
2965       /* Create the empty parameter list.  */
2966       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967       /* Remember where the base of the declarator obstack lies.  */
2968       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2969     }
2970
2971   cp_parser_declaration_seq_opt (parser);
2972
2973   /* If there are no tokens left then all went well.  */
2974   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2975     {
2976       /* Get rid of the token array; we don't need it any more.  */
2977       cp_lexer_destroy (parser->lexer);
2978       parser->lexer = NULL;
2979
2980       /* This file might have been a context that's implicitly extern
2981          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2982       if (parser->implicit_extern_c)
2983         {
2984           pop_lang_context ();
2985           parser->implicit_extern_c = false;
2986         }
2987
2988       /* Finish up.  */
2989       finish_translation_unit ();
2990
2991       success = true;
2992     }
2993   else
2994     {
2995       cp_parser_error (parser, "expected declaration");
2996       success = false;
2997     }
2998
2999   /* Make sure the declarator obstack was fully cleaned up.  */
3000   gcc_assert (obstack_next_free (&declarator_obstack)
3001               == declarator_obstack_base);
3002
3003   /* All went well.  */
3004   return success;
3005 }
3006
3007 /* Expressions [gram.expr] */
3008
3009 /* Parse a primary-expression.
3010
3011    primary-expression:
3012      literal
3013      this
3014      ( expression )
3015      id-expression
3016
3017    GNU Extensions:
3018
3019    primary-expression:
3020      ( compound-statement )
3021      __builtin_va_arg ( assignment-expression , type-id )
3022      __builtin_offsetof ( type-id , offsetof-expression )
3023
3024    C++ Extensions:
3025      __has_nothrow_assign ( type-id )   
3026      __has_nothrow_constructor ( type-id )
3027      __has_nothrow_copy ( type-id )
3028      __has_trivial_assign ( type-id )   
3029      __has_trivial_constructor ( type-id )
3030      __has_trivial_copy ( type-id )
3031      __has_trivial_destructor ( type-id )
3032      __has_virtual_destructor ( type-id )     
3033      __is_abstract ( type-id )
3034      __is_base_of ( type-id , type-id )
3035      __is_class ( type-id )
3036      __is_convertible_to ( type-id , type-id )     
3037      __is_empty ( type-id )
3038      __is_enum ( type-id )
3039      __is_pod ( type-id )
3040      __is_polymorphic ( type-id )
3041      __is_union ( type-id )
3042
3043    Objective-C++ Extension:
3044
3045    primary-expression:
3046      objc-expression
3047
3048    literal:
3049      __null
3050
3051    ADDRESS_P is true iff this expression was immediately preceded by
3052    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3053    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3054    true iff this expression is a template argument.
3055
3056    Returns a representation of the expression.  Upon return, *IDK
3057    indicates what kind of id-expression (if any) was present.  */
3058
3059 static tree
3060 cp_parser_primary_expression (cp_parser *parser,
3061                               bool address_p,
3062                               bool cast_p,
3063                               bool template_arg_p,
3064                               cp_id_kind *idk)
3065 {
3066   cp_token *token;
3067
3068   /* Assume the primary expression is not an id-expression.  */
3069   *idk = CP_ID_KIND_NONE;
3070
3071   /* Peek at the next token.  */
3072   token = cp_lexer_peek_token (parser->lexer);
3073   switch (token->type)
3074     {
3075       /* literal:
3076            integer-literal
3077            character-literal
3078            floating-literal
3079            string-literal
3080            boolean-literal  */
3081     case CPP_CHAR:
3082     case CPP_WCHAR:
3083     case CPP_NUMBER:
3084       token = cp_lexer_consume_token (parser->lexer);
3085       /* Floating-point literals are only allowed in an integral
3086          constant expression if they are cast to an integral or
3087          enumeration type.  */
3088       if (TREE_CODE (token->u.value) == REAL_CST
3089           && parser->integral_constant_expression_p
3090           && pedantic)
3091         {
3092           /* CAST_P will be set even in invalid code like "int(2.7 +
3093              ...)".   Therefore, we have to check that the next token
3094              is sure to end the cast.  */
3095           if (cast_p)
3096             {
3097               cp_token *next_token;
3098
3099               next_token = cp_lexer_peek_token (parser->lexer);
3100               if (/* The comma at the end of an
3101                      enumerator-definition.  */
3102                   next_token->type != CPP_COMMA
3103                   /* The curly brace at the end of an enum-specifier.  */
3104                   && next_token->type != CPP_CLOSE_BRACE
3105                   /* The end of a statement.  */
3106                   && next_token->type != CPP_SEMICOLON
3107                   /* The end of the cast-expression.  */
3108                   && next_token->type != CPP_CLOSE_PAREN
3109                   /* The end of an array bound.  */
3110                   && next_token->type != CPP_CLOSE_SQUARE
3111                   /* The closing ">" in a template-argument-list.  */
3112                   && (next_token->type != CPP_GREATER
3113                       || parser->greater_than_is_operator_p)
3114                   /* C++0x only: A ">>" treated like two ">" tokens,
3115                      in a template-argument-list.  */
3116                   && (next_token->type != CPP_RSHIFT
3117                       || (cxx_dialect == cxx98)
3118                       || parser->greater_than_is_operator_p))
3119                 cast_p = false;
3120             }
3121
3122           /* If we are within a cast, then the constraint that the
3123              cast is to an integral or enumeration type will be
3124              checked at that point.  If we are not within a cast, then
3125              this code is invalid.  */
3126           if (!cast_p)
3127             cp_parser_non_integral_constant_expression
3128               (parser, "floating-point literal");
3129         }
3130       return token->u.value;
3131
3132     case CPP_STRING:
3133     case CPP_WSTRING:
3134       /* ??? Should wide strings be allowed when parser->translate_strings_p
3135          is false (i.e. in attributes)?  If not, we can kill the third
3136          argument to cp_parser_string_literal.  */
3137       return cp_parser_string_literal (parser,
3138                                        parser->translate_strings_p,
3139                                        true);
3140
3141     case CPP_OPEN_PAREN:
3142       {
3143         tree expr;
3144         bool saved_greater_than_is_operator_p;
3145
3146         /* Consume the `('.  */
3147         cp_lexer_consume_token (parser->lexer);
3148         /* Within a parenthesized expression, a `>' token is always
3149            the greater-than operator.  */
3150         saved_greater_than_is_operator_p
3151           = parser->greater_than_is_operator_p;
3152         parser->greater_than_is_operator_p = true;
3153         /* If we see `( { ' then we are looking at the beginning of
3154            a GNU statement-expression.  */
3155         if (cp_parser_allow_gnu_extensions_p (parser)
3156             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3157           {
3158             /* Statement-expressions are not allowed by the standard.  */
3159             if (pedantic)
3160               pedwarn ("ISO C++ forbids braced-groups within expressions");
3161
3162             /* And they're not allowed outside of a function-body; you
3163                cannot, for example, write:
3164
3165                  int i = ({ int j = 3; j + 1; });
3166
3167                at class or namespace scope.  */
3168             if (!parser->in_function_body
3169                 || parser->in_template_argument_list_p)
3170               {
3171                 error ("statement-expressions are not allowed outside "
3172                        "functions nor in template-argument lists");
3173                 cp_parser_skip_to_end_of_block_or_statement (parser);
3174                 expr = error_mark_node;
3175               }
3176             else
3177               {
3178                 /* Start the statement-expression.  */
3179                 expr = begin_stmt_expr ();
3180                 /* Parse the compound-statement.  */
3181                 cp_parser_compound_statement (parser, expr, false);
3182                 /* Finish up.  */
3183                 expr = finish_stmt_expr (expr, false);
3184               }
3185           }
3186         else
3187           {
3188             /* Parse the parenthesized expression.  */
3189             expr = cp_parser_expression (parser, cast_p);
3190             /* Let the front end know that this expression was
3191                enclosed in parentheses. This matters in case, for
3192                example, the expression is of the form `A::B', since
3193                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3194                not.  */
3195             finish_parenthesized_expr (expr);
3196           }
3197         /* The `>' token might be the end of a template-id or
3198            template-parameter-list now.  */
3199         parser->greater_than_is_operator_p
3200           = saved_greater_than_is_operator_p;
3201         /* Consume the `)'.  */
3202         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3203           cp_parser_skip_to_end_of_statement (parser);
3204
3205         return expr;
3206       }
3207
3208     case CPP_KEYWORD:
3209       switch (token->keyword)
3210         {
3211           /* These two are the boolean literals.  */
3212         case RID_TRUE:
3213           cp_lexer_consume_token (parser->lexer);
3214           return boolean_true_node;
3215         case RID_FALSE:
3216           cp_lexer_consume_token (parser->lexer);
3217           return boolean_false_node;
3218
3219           /* The `__null' literal.  */
3220         case RID_NULL:
3221           cp_lexer_consume_token (parser->lexer);
3222           return null_node;
3223
3224           /* Recognize the `this' keyword.  */
3225         case RID_THIS:
3226           cp_lexer_consume_token (parser->lexer);
3227           if (parser->local_variables_forbidden_p)
3228             {
3229               error ("%<this%> may not be used in this context");
3230               return error_mark_node;
3231             }
3232           /* Pointers cannot appear in constant-expressions.  */
3233           if (cp_parser_non_integral_constant_expression (parser,
3234                                                           "`this'"))
3235             return error_mark_node;
3236           return finish_this_expr ();
3237
3238           /* The `operator' keyword can be the beginning of an
3239              id-expression.  */
3240         case RID_OPERATOR:
3241           goto id_expression;
3242
3243         case RID_FUNCTION_NAME:
3244         case RID_PRETTY_FUNCTION_NAME:
3245         case RID_C99_FUNCTION_NAME:
3246           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247              __func__ are the names of variables -- but they are
3248              treated specially.  Therefore, they are handled here,
3249              rather than relying on the generic id-expression logic
3250              below.  Grammatically, these names are id-expressions.
3251
3252              Consume the token.  */
3253           token = cp_lexer_consume_token (parser->lexer);
3254           /* Look up the name.  */
3255           return finish_fname (token->u.value);
3256
3257         case RID_VA_ARG:
3258           {
3259             tree expression;
3260             tree type;
3261
3262             /* The `__builtin_va_arg' construct is used to handle
3263                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3264             cp_lexer_consume_token (parser->lexer);
3265             /* Look for the opening `('.  */
3266             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3267             /* Now, parse the assignment-expression.  */
3268             expression = cp_parser_assignment_expression (parser,
3269                                                           /*cast_p=*/false);
3270             /* Look for the `,'.  */
3271             cp_parser_require (parser, CPP_COMMA, "`,'");
3272             /* Parse the type-id.  */
3273             type = cp_parser_type_id (parser);
3274             /* Look for the closing `)'.  */
3275             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3276             /* Using `va_arg' in a constant-expression is not
3277                allowed.  */
3278             if (cp_parser_non_integral_constant_expression (parser,
3279                                                             "`va_arg'"))
3280               return error_mark_node;
3281             return build_x_va_arg (expression, type);
3282           }
3283
3284         case RID_OFFSETOF:
3285           return cp_parser_builtin_offsetof (parser);
3286
3287         case RID_HAS_NOTHROW_ASSIGN:
3288         case RID_HAS_NOTHROW_CONSTRUCTOR:
3289         case RID_HAS_NOTHROW_COPY:        
3290         case RID_HAS_TRIVIAL_ASSIGN:
3291         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292         case RID_HAS_TRIVIAL_COPY:        
3293         case RID_HAS_TRIVIAL_DESTRUCTOR:
3294         case RID_HAS_VIRTUAL_DESTRUCTOR:
3295         case RID_IS_ABSTRACT:
3296         case RID_IS_BASE_OF:
3297         case RID_IS_CLASS:
3298         case RID_IS_CONVERTIBLE_TO:
3299         case RID_IS_EMPTY:
3300         case RID_IS_ENUM:
3301         case RID_IS_POD:
3302         case RID_IS_POLYMORPHIC:
3303         case RID_IS_UNION:
3304           return cp_parser_trait_expr (parser, token->keyword);
3305
3306         /* Objective-C++ expressions.  */
3307         case RID_AT_ENCODE:
3308         case RID_AT_PROTOCOL:
3309         case RID_AT_SELECTOR:
3310           return cp_parser_objc_expression (parser);
3311
3312         default:
3313           cp_parser_error (parser, "expected primary-expression");
3314           return error_mark_node;
3315         }
3316
3317       /* An id-expression can start with either an identifier, a
3318          `::' as the beginning of a qualified-id, or the "operator"
3319          keyword.  */
3320     case CPP_NAME:
3321     case CPP_SCOPE:
3322     case CPP_TEMPLATE_ID:
3323     case CPP_NESTED_NAME_SPECIFIER:
3324       {
3325         tree id_expression;
3326         tree decl;
3327         const char *error_msg;
3328         bool template_p;
3329         bool done;
3330
3331       id_expression:
3332         /* Parse the id-expression.  */
3333         id_expression
3334           = cp_parser_id_expression (parser,
3335                                      /*template_keyword_p=*/false,
3336                                      /*check_dependency_p=*/true,
3337                                      &template_p,
3338                                      /*declarator_p=*/false,
3339                                      /*optional_p=*/false);
3340         if (id_expression == error_mark_node)
3341           return error_mark_node;
3342         token = cp_lexer_peek_token (parser->lexer);
3343         done = (token->type != CPP_OPEN_SQUARE
3344                 && token->type != CPP_OPEN_PAREN
3345                 && token->type != CPP_DOT
3346                 && token->type != CPP_DEREF
3347                 && token->type != CPP_PLUS_PLUS
3348                 && token->type != CPP_MINUS_MINUS);
3349         /* If we have a template-id, then no further lookup is
3350            required.  If the template-id was for a template-class, we
3351            will sometimes have a TYPE_DECL at this point.  */
3352         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353                  || TREE_CODE (id_expression) == TYPE_DECL)
3354           decl = id_expression;
3355         /* Look up the name.  */
3356         else
3357           {
3358             tree ambiguous_decls;
3359
3360             decl = cp_parser_lookup_name (parser, id_expression,
3361                                           none_type,
3362                                           template_p,
3363                                           /*is_namespace=*/false,
3364                                           /*check_dependency=*/true,
3365                                           &ambiguous_decls);
3366             /* If the lookup was ambiguous, an error will already have
3367                been issued.  */
3368             if (ambiguous_decls)
3369               return error_mark_node;
3370
3371             /* In Objective-C++, an instance variable (ivar) may be preferred
3372                to whatever cp_parser_lookup_name() found.  */
3373             decl = objc_lookup_ivar (decl, id_expression);
3374
3375             /* If name lookup gives us a SCOPE_REF, then the
3376                qualifying scope was dependent.  */
3377             if (TREE_CODE (decl) == SCOPE_REF)
3378               {
3379                 /* At this point, we do not know if DECL is a valid
3380                    integral constant expression.  We assume that it is
3381                    in fact such an expression, so that code like:
3382
3383                       template <int N> struct A {
3384                         int a[B<N>::i];
3385                       };
3386                      
3387                    is accepted.  At template-instantiation time, we
3388                    will check that B<N>::i is actually a constant.  */
3389                 return decl;
3390               }
3391             /* Check to see if DECL is a local variable in a context
3392                where that is forbidden.  */
3393             if (parser->local_variables_forbidden_p
3394                 && local_variable_p (decl))
3395               {
3396                 /* It might be that we only found DECL because we are
3397                    trying to be generous with pre-ISO scoping rules.
3398                    For example, consider:
3399
3400                      int i;
3401                      void g() {
3402                        for (int i = 0; i < 10; ++i) {}
3403                        extern void f(int j = i);
3404                      }
3405
3406                    Here, name look up will originally find the out
3407                    of scope `i'.  We need to issue a warning message,
3408                    but then use the global `i'.  */
3409                 decl = check_for_out_of_scope_variable (decl);
3410                 if (local_variable_p (decl))
3411                   {
3412                     error ("local variable %qD may not appear in this context",
3413                            decl);
3414                     return error_mark_node;
3415                   }
3416               }
3417           }
3418
3419         decl = (finish_id_expression
3420                 (id_expression, decl, parser->scope,
3421                  idk,
3422                  parser->integral_constant_expression_p,
3423                  parser->allow_non_integral_constant_expression_p,
3424                  &parser->non_integral_constant_expression_p,
3425                  template_p, done, address_p,
3426                  template_arg_p,
3427                  &error_msg));
3428         if (error_msg)
3429           cp_parser_error (parser, error_msg);
3430         return decl;
3431       }
3432
3433       /* Anything else is an error.  */
3434     default:
3435       /* ...unless we have an Objective-C++ message or string literal,
3436          that is.  */
3437       if (c_dialect_objc ()
3438           && (token->type == CPP_OPEN_SQUARE
3439               || token->type == CPP_OBJC_STRING))
3440         return cp_parser_objc_expression (parser);
3441
3442       cp_parser_error (parser, "expected primary-expression");
3443       return error_mark_node;
3444     }
3445 }
3446
3447 /* Parse an id-expression.
3448
3449    id-expression:
3450      unqualified-id
3451      qualified-id
3452
3453    qualified-id:
3454      :: [opt] nested-name-specifier template [opt] unqualified-id
3455      :: identifier
3456      :: operator-function-id
3457      :: template-id
3458
3459    Return a representation of the unqualified portion of the
3460    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3461    a `::' or nested-name-specifier.
3462
3463    Often, if the id-expression was a qualified-id, the caller will
3464    want to make a SCOPE_REF to represent the qualified-id.  This
3465    function does not do this in order to avoid wastefully creating
3466    SCOPE_REFs when they are not required.
3467
3468    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3469    `template' keyword.
3470
3471    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472    uninstantiated templates.
3473
3474    If *TEMPLATE_P is non-NULL, it is set to true iff the
3475    `template' keyword is used to explicitly indicate that the entity
3476    named is a template.
3477
3478    If DECLARATOR_P is true, the id-expression is appearing as part of
3479    a declarator, rather than as part of an expression.  */
3480
3481 static tree
3482 cp_parser_id_expression (cp_parser *parser,
3483                          bool template_keyword_p,
3484                          bool check_dependency_p,
3485                          bool *template_p,
3486                          bool declarator_p,
3487                          bool optional_p)
3488 {
3489   bool global_scope_p;
3490   bool nested_name_specifier_p;
3491
3492   /* Assume the `template' keyword was not used.  */
3493   if (template_p)
3494     *template_p = template_keyword_p;
3495
3496   /* Look for the optional `::' operator.  */
3497   global_scope_p
3498     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3499        != NULL_TREE);
3500   /* Look for the optional nested-name-specifier.  */
3501   nested_name_specifier_p
3502     = (cp_parser_nested_name_specifier_opt (parser,
3503                                             /*typename_keyword_p=*/false,
3504                                             check_dependency_p,
3505                                             /*type_p=*/false,
3506                                             declarator_p)
3507        != NULL_TREE);
3508   /* If there is a nested-name-specifier, then we are looking at
3509      the first qualified-id production.  */
3510   if (nested_name_specifier_p)
3511     {
3512       tree saved_scope;
3513       tree saved_object_scope;
3514       tree saved_qualifying_scope;
3515       tree unqualified_id;
3516       bool is_template;
3517
3518       /* See if the next token is the `template' keyword.  */
3519       if (!template_p)
3520         template_p = &is_template;
3521       *template_p = cp_parser_optional_template_keyword (parser);
3522       /* Name lookup we do during the processing of the
3523          unqualified-id might obliterate SCOPE.  */
3524       saved_scope = parser->scope;
3525       saved_object_scope = parser->object_scope;
3526       saved_qualifying_scope = parser->qualifying_scope;
3527       /* Process the final unqualified-id.  */
3528       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3529                                                  check_dependency_p,
3530                                                  declarator_p,
3531                                                  /*optional_p=*/false);
3532       /* Restore the SAVED_SCOPE for our caller.  */
3533       parser->scope = saved_scope;
3534       parser->object_scope = saved_object_scope;
3535       parser->qualifying_scope = saved_qualifying_scope;
3536
3537       return unqualified_id;
3538     }
3539   /* Otherwise, if we are in global scope, then we are looking at one
3540      of the other qualified-id productions.  */
3541   else if (global_scope_p)
3542     {
3543       cp_token *token;
3544       tree id;
3545
3546       /* Peek at the next token.  */
3547       token = cp_lexer_peek_token (parser->lexer);
3548
3549       /* If it's an identifier, and the next token is not a "<", then
3550          we can avoid the template-id case.  This is an optimization
3551          for this common case.  */
3552       if (token->type == CPP_NAME
3553           && !cp_parser_nth_token_starts_template_argument_list_p
3554                (parser, 2))
3555         return cp_parser_identifier (parser);
3556
3557       cp_parser_parse_tentatively (parser);
3558       /* Try a template-id.  */
3559       id = cp_parser_template_id (parser,
3560                                   /*template_keyword_p=*/false,
3561                                   /*check_dependency_p=*/true,
3562                                   declarator_p);
3563       /* If that worked, we're done.  */
3564       if (cp_parser_parse_definitely (parser))
3565         return id;
3566
3567       /* Peek at the next token.  (Changes in the token buffer may
3568          have invalidated the pointer obtained above.)  */
3569       token = cp_lexer_peek_token (parser->lexer);
3570
3571       switch (token->type)
3572         {
3573         case CPP_NAME:
3574           return cp_parser_identifier (parser);
3575
3576         case CPP_KEYWORD:
3577           if (token->keyword == RID_OPERATOR)
3578             return cp_parser_operator_function_id (parser);
3579           /* Fall through.  */
3580
3581         default:
3582           cp_parser_error (parser, "expected id-expression");
3583           return error_mark_node;
3584         }
3585     }
3586   else
3587     return cp_parser_unqualified_id (parser, template_keyword_p,
3588                                      /*check_dependency_p=*/true,
3589                                      declarator_p,
3590                                      optional_p);
3591 }
3592
3593 /* Parse an unqualified-id.
3594
3595    unqualified-id:
3596      identifier
3597      operator-function-id
3598      conversion-function-id
3599      ~ class-name
3600      template-id
3601
3602    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603    keyword, in a construct like `A::template ...'.
3604
3605    Returns a representation of unqualified-id.  For the `identifier'
3606    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3607    production a BIT_NOT_EXPR is returned; the operand of the
3608    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3609    other productions, see the documentation accompanying the
3610    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3611    names are looked up in uninstantiated templates.  If DECLARATOR_P
3612    is true, the unqualified-id is appearing as part of a declarator,
3613    rather than as part of an expression.  */
3614
3615 static tree
3616 cp_parser_unqualified_id (cp_parser* parser,
3617                           bool template_keyword_p,
3618                           bool check_dependency_p,
3619                           bool declarator_p,
3620                           bool optional_p)
3621 {
3622   cp_token *token;
3623
3624   /* Peek at the next token.  */
3625   token = cp_lexer_peek_token (parser->lexer);
3626
3627   switch (token->type)
3628     {
3629     case CPP_NAME:
3630       {
3631         tree id;
3632
3633         /* We don't know yet whether or not this will be a
3634            template-id.  */
3635         cp_parser_parse_tentatively (parser);
3636         /* Try a template-id.  */
3637         id = cp_parser_template_id (parser, template_keyword_p,
3638                                     check_dependency_p,
3639                                     declarator_p);
3640         /* If it worked, we're done.  */
3641         if (cp_parser_parse_definitely (parser))
3642           return id;
3643         /* Otherwise, it's an ordinary identifier.  */
3644         return cp_parser_identifier (parser);
3645       }
3646
3647     case CPP_TEMPLATE_ID:
3648       return cp_parser_template_id (parser, template_keyword_p,
3649                                     check_dependency_p,
3650                                     declarator_p);
3651
3652     case CPP_COMPL:
3653       {
3654         tree type_decl;
3655         tree qualifying_scope;
3656         tree object_scope;
3657         tree scope;
3658         bool done;
3659
3660         /* Consume the `~' token.  */
3661         cp_lexer_consume_token (parser->lexer);
3662         /* Parse the class-name.  The standard, as written, seems to
3663            say that:
3664
3665              template <typename T> struct S { ~S (); };
3666              template <typename T> S<T>::~S() {}
3667
3668            is invalid, since `~' must be followed by a class-name, but
3669            `S<T>' is dependent, and so not known to be a class.
3670            That's not right; we need to look in uninstantiated
3671            templates.  A further complication arises from:
3672
3673              template <typename T> void f(T t) {
3674                t.T::~T();
3675              }
3676
3677            Here, it is not possible to look up `T' in the scope of `T'
3678            itself.  We must look in both the current scope, and the
3679            scope of the containing complete expression.
3680
3681            Yet another issue is:
3682
3683              struct S {
3684                int S;
3685                ~S();
3686              };
3687
3688              S::~S() {}
3689
3690            The standard does not seem to say that the `S' in `~S'
3691            should refer to the type `S' and not the data member
3692            `S::S'.  */
3693
3694         /* DR 244 says that we look up the name after the "~" in the
3695            same scope as we looked up the qualifying name.  That idea
3696            isn't fully worked out; it's more complicated than that.  */
3697         scope = parser->scope;
3698         object_scope = parser->object_scope;
3699         qualifying_scope = parser->qualifying_scope;
3700
3701         /* Check for invalid scopes.  */
3702         if (scope == error_mark_node)
3703           {
3704             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705               cp_lexer_consume_token (parser->lexer);
3706             return error_mark_node;
3707           }
3708         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3709           {
3710             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711               error ("scope %qT before %<~%> is not a class-name", scope);
3712             cp_parser_simulate_error (parser);
3713             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714               cp_lexer_consume_token (parser->lexer);
3715             return error_mark_node;
3716           }
3717         gcc_assert (!scope || TYPE_P (scope));
3718
3719         /* If the name is of the form "X::~X" it's OK.  */
3720         token = cp_lexer_peek_token (parser->lexer);
3721         if (scope
3722             && token->type == CPP_NAME
3723             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3724                 == CPP_OPEN_PAREN)
3725             && constructor_name_p (token->u.value, scope))
3726           {
3727             cp_lexer_consume_token (parser->lexer);
3728             return build_nt (BIT_NOT_EXPR, scope);
3729           }
3730
3731         /* If there was an explicit qualification (S::~T), first look
3732            in the scope given by the qualification (i.e., S).  */
3733         done = false;
3734         type_decl = NULL_TREE;
3735         if (scope)
3736           {
3737             cp_parser_parse_tentatively (parser);
3738             type_decl = cp_parser_class_name (parser,
3739                                               /*typename_keyword_p=*/false,
3740                                               /*template_keyword_p=*/false,
3741                                               none_type,
3742                                               /*check_dependency=*/false,
3743                                               /*class_head_p=*/false,
3744                                               declarator_p);
3745             if (cp_parser_parse_definitely (parser))
3746               done = true;
3747           }
3748         /* In "N::S::~S", look in "N" as well.  */
3749         if (!done && scope && qualifying_scope)
3750           {
3751             cp_parser_parse_tentatively (parser);
3752             parser->scope = qualifying_scope;
3753             parser->object_scope = NULL_TREE;
3754             parser->qualifying_scope = NULL_TREE;
3755             type_decl
3756               = cp_parser_class_name (parser,
3757                                       /*typename_keyword_p=*/false,
3758                                       /*template_keyword_p=*/false,
3759                                       none_type,
3760                                       /*check_dependency=*/false,
3761                                       /*class_head_p=*/false,
3762                                       declarator_p);
3763             if (cp_parser_parse_definitely (parser))
3764               done = true;
3765           }
3766         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3767         else if (!done && object_scope)
3768           {
3769             cp_parser_parse_tentatively (parser);
3770             parser->scope = object_scope;
3771             parser->object_scope = NULL_TREE;
3772             parser->qualifying_scope = NULL_TREE;
3773             type_decl
3774               = cp_parser_class_name (parser,
3775                                       /*typename_keyword_p=*/false,
3776                                       /*template_keyword_p=*/false,
3777                                       none_type,
3778                                       /*check_dependency=*/false,
3779                                       /*class_head_p=*/false,
3780                                       declarator_p);
3781             if (cp_parser_parse_definitely (parser))
3782               done = true;
3783           }
3784         /* Look in the surrounding context.  */
3785         if (!done)
3786           {
3787             parser->scope = NULL_TREE;
3788             parser->object_scope = NULL_TREE;
3789             parser->qualifying_scope = NULL_TREE;
3790             type_decl
3791               = cp_parser_class_name (parser,
3792                                       /*typename_keyword_p=*/false,
3793                                       /*template_keyword_p=*/false,
3794                                       none_type,
3795                                       /*check_dependency=*/false,
3796                                       /*class_head_p=*/false,
3797                                       declarator_p);
3798           }
3799         /* If an error occurred, assume that the name of the
3800            destructor is the same as the name of the qualifying
3801            class.  That allows us to keep parsing after running
3802            into ill-formed destructor names.  */
3803         if (type_decl == error_mark_node && scope)
3804           return build_nt (BIT_NOT_EXPR, scope);
3805         else if (type_decl == error_mark_node)
3806           return error_mark_node;
3807
3808         /* Check that destructor name and scope match.  */
3809         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3810           {
3811             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812               error ("declaration of %<~%T%> as member of %qT",
3813                      type_decl, scope);
3814             cp_parser_simulate_error (parser);
3815             return error_mark_node;
3816           }
3817
3818         /* [class.dtor]
3819
3820            A typedef-name that names a class shall not be used as the
3821            identifier in the declarator for a destructor declaration.  */
3822         if (declarator_p
3823             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824             && !DECL_SELF_REFERENCE_P (type_decl)
3825             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826           error ("typedef-name %qD used as destructor declarator",
3827                  type_decl);
3828
3829         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3830       }
3831
3832     case CPP_KEYWORD:
3833       if (token->keyword == RID_OPERATOR)
3834         {
3835           tree id;
3836
3837           /* This could be a template-id, so we try that first.  */
3838           cp_parser_parse_tentatively (parser);
3839           /* Try a template-id.  */
3840           id = cp_parser_template_id (parser, template_keyword_p,
3841                                       /*check_dependency_p=*/true,
3842                                       declarator_p);
3843           /* If that worked, we're done.  */
3844           if (cp_parser_parse_definitely (parser))
3845             return id;
3846           /* We still don't know whether we're looking at an
3847              operator-function-id or a conversion-function-id.  */
3848           cp_parser_parse_tentatively (parser);
3849           /* Try an operator-function-id.  */
3850           id = cp_parser_operator_function_id (parser);
3851           /* If that didn't work, try a conversion-function-id.  */
3852           if (!cp_parser_parse_definitely (parser))
3853             id = cp_parser_conversion_function_id (parser);
3854
3855           return id;
3856         }
3857       /* Fall through.  */
3858
3859     default:
3860       if (optional_p)
3861         return NULL_TREE;
3862       cp_parser_error (parser, "expected unqualified-id");
3863       return error_mark_node;
3864     }
3865 }
3866
3867 /* Parse an (optional) nested-name-specifier.
3868
3869    nested-name-specifier:
3870      class-or-namespace-name :: nested-name-specifier [opt]
3871      class-or-namespace-name :: template nested-name-specifier [opt]
3872
3873    PARSER->SCOPE should be set appropriately before this function is
3874    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3876    in name lookups.
3877
3878    Sets PARSER->SCOPE to the class (TYPE) or namespace
3879    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880    it unchanged if there is no nested-name-specifier.  Returns the new
3881    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3882
3883    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884    part of a declaration and/or decl-specifier.  */
3885
3886 static tree
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888                                      bool typename_keyword_p,
3889                                      bool check_dependency_p,
3890                                      bool type_p,
3891                                      bool is_declaration)
3892 {
3893   bool success = false;
3894   cp_token_position start = 0;
3895   cp_token *token;
3896
3897   /* Remember where the nested-name-specifier starts.  */
3898   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3899     {
3900       start = cp_lexer_token_position (parser->lexer, false);
3901       push_deferring_access_checks (dk_deferred);
3902     }
3903
3904   while (true)
3905     {
3906       tree new_scope;
3907       tree old_scope;
3908       tree saved_qualifying_scope;
3909       bool template_keyword_p;
3910
3911       /* Spot cases that cannot be the beginning of a
3912          nested-name-specifier.  */
3913       token = cp_lexer_peek_token (parser->lexer);
3914
3915       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916          the already parsed nested-name-specifier.  */
3917       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3918         {
3919           /* Grab the nested-name-specifier and continue the loop.  */
3920           cp_parser_pre_parsed_nested_name_specifier (parser);
3921           /* If we originally encountered this nested-name-specifier
3922              with IS_DECLARATION set to false, we will not have
3923              resolved TYPENAME_TYPEs, so we must do so here.  */
3924           if (is_declaration
3925               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3926             {
3927               new_scope = resolve_typename_type (parser->scope,
3928                                                  /*only_current_p=*/false);
3929               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930                 parser->scope = new_scope;
3931             }
3932           success = true;
3933           continue;
3934         }
3935
3936       /* Spot cases that cannot be the beginning of a
3937          nested-name-specifier.  On the second and subsequent times
3938          through the loop, we look for the `template' keyword.  */
3939       if (success && token->keyword == RID_TEMPLATE)
3940         ;
3941       /* A template-id can start a nested-name-specifier.  */
3942       else if (token->type == CPP_TEMPLATE_ID)
3943         ;
3944       else
3945         {
3946           /* If the next token is not an identifier, then it is
3947              definitely not a class-or-namespace-name.  */
3948           if (token->type != CPP_NAME)
3949             break;
3950           /* If the following token is neither a `<' (to begin a
3951              template-id), nor a `::', then we are not looking at a
3952              nested-name-specifier.  */
3953           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954           if (token->type != CPP_SCOPE
3955               && !cp_parser_nth_token_starts_template_argument_list_p
3956                   (parser, 2))
3957             break;
3958         }
3959
3960       /* The nested-name-specifier is optional, so we parse
3961          tentatively.  */
3962       cp_parser_parse_tentatively (parser);
3963
3964       /* Look for the optional `template' keyword, if this isn't the
3965          first time through the loop.  */
3966       if (success)
3967         template_keyword_p = cp_parser_optional_template_keyword (parser);
3968       else
3969         template_keyword_p = false;
3970
3971       /* Save the old scope since the name lookup we are about to do
3972          might destroy it.  */
3973       old_scope = parser->scope;
3974       saved_qualifying_scope = parser->qualifying_scope;
3975       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976          look up names in "X<T>::I" in order to determine that "Y" is
3977          a template.  So, if we have a typename at this point, we make
3978          an effort to look through it.  */
3979       if (is_declaration
3980           && !typename_keyword_p
3981           && parser->scope
3982           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983         parser->scope = resolve_typename_type (parser->scope,
3984                                                /*only_current_p=*/false);
3985       /* Parse the qualifying entity.  */
3986       new_scope
3987         = cp_parser_class_or_namespace_name (parser,
3988                                              typename_keyword_p,
3989                                              template_keyword_p,
3990                                              check_dependency_p,
3991                                              type_p,
3992                                              is_declaration);
3993       /* Look for the `::' token.  */
3994       cp_parser_require (parser, CPP_SCOPE, "`::'");
3995
3996       /* If we found what we wanted, we keep going; otherwise, we're
3997          done.  */
3998       if (!cp_parser_parse_definitely (parser))
3999         {
4000           bool error_p = false;
4001
4002           /* Restore the OLD_SCOPE since it was valid before the
4003              failed attempt at finding the last
4004              class-or-namespace-name.  */
4005           parser->scope = old_scope;
4006           parser->qualifying_scope = saved_qualifying_scope;
4007           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4008             break;
4009           /* If the next token is an identifier, and the one after
4010              that is a `::', then any valid interpretation would have
4011              found a class-or-namespace-name.  */
4012           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4014                      == CPP_SCOPE)
4015                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4016                      != CPP_COMPL))
4017             {
4018               token = cp_lexer_consume_token (parser->lexer);
4019               if (!error_p)
4020                 {
4021                   if (!token->ambiguous_p)
4022                     {
4023                       tree decl;
4024                       tree ambiguous_decls;
4025
4026                       decl = cp_parser_lookup_name (parser, token->u.value,
4027                                                     none_type,
4028                                                     /*is_template=*/false,
4029                                                     /*is_namespace=*/false,
4030                                                     /*check_dependency=*/true,
4031                                                     &ambiguous_decls);
4032                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4033                         error ("%qD used without template parameters", decl);
4034                       else if (ambiguous_decls)
4035                         {
4036                           error ("reference to %qD is ambiguous",
4037                                  token->u.value);
4038                           print_candidates (ambiguous_decls);
4039                           decl = error_mark_node;
4040                         }
4041                       else
4042                         cp_parser_name_lookup_error
4043                           (parser, token->u.value, decl,
4044                            "is not a class or namespace");
4045                     }
4046                   parser->scope = error_mark_node;
4047                   error_p = true;
4048                   /* Treat this as a successful nested-name-specifier
4049                      due to:
4050
4051                      [basic.lookup.qual]
4052
4053                      If the name found is not a class-name (clause
4054                      _class_) or namespace-name (_namespace.def_), the
4055                      program is ill-formed.  */
4056                   success = true;
4057                 }
4058               cp_lexer_consume_token (parser->lexer);
4059             }
4060           break;
4061         }
4062       /* We've found one valid nested-name-specifier.  */
4063       success = true;
4064       /* Name lookup always gives us a DECL.  */
4065       if (TREE_CODE (new_scope) == TYPE_DECL)
4066         new_scope = TREE_TYPE (new_scope);
4067       /* Uses of "template" must be followed by actual templates.  */
4068       if (template_keyword_p
4069           && !(CLASS_TYPE_P (new_scope)
4070                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075                    == TEMPLATE_ID_EXPR)))
4076         pedwarn (TYPE_P (new_scope)
4077                  ? "%qT is not a template"
4078                  : "%qD is not a template",
4079                  new_scope);
4080       /* If it is a class scope, try to complete it; we are about to
4081          be looking up names inside the class.  */
4082       if (TYPE_P (new_scope)
4083           /* Since checking types for dependency can be expensive,
4084              avoid doing it if the type is already complete.  */
4085           && !COMPLETE_TYPE_P (new_scope)
4086           /* Do not try to complete dependent types.  */
4087           && !dependent_type_p (new_scope))
4088         {
4089           new_scope = complete_type (new_scope);
4090           /* If it is a typedef to current class, use the current
4091              class instead, as the typedef won't have any names inside
4092              it yet.  */
4093           if (!COMPLETE_TYPE_P (new_scope)
4094               && currently_open_class (new_scope))
4095             new_scope = TYPE_MAIN_VARIANT (new_scope);
4096         }
4097       /* Make sure we look in the right scope the next time through
4098          the loop.  */
4099       parser->scope = new_scope;
4100     }
4101
4102   /* If parsing tentatively, replace the sequence of tokens that makes
4103      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4104      token.  That way, should we re-parse the token stream, we will
4105      not have to repeat the effort required to do the parse, nor will
4106      we issue duplicate error messages.  */
4107   if (success && start)
4108     {
4109       cp_token *token;
4110
4111       token = cp_lexer_token_at (parser->lexer, start);
4112       /* Reset the contents of the START token.  */
4113       token->type = CPP_NESTED_NAME_SPECIFIER;
4114       /* Retrieve any deferred checks.  Do not pop this access checks yet
4115          so the memory will not be reclaimed during token replacing below.  */
4116       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4117       token->u.tree_check_value->value = parser->scope;
4118       token->u.tree_check_value->checks = get_deferred_access_checks ();
4119       token->u.tree_check_value->qualifying_scope =
4120         parser->qualifying_scope;
4121       token->keyword = RID_MAX;
4122
4123       /* Purge all subsequent tokens.  */
4124       cp_lexer_purge_tokens_after (parser->lexer, start);
4125     }
4126
4127   if (start)
4128     pop_to_parent_deferring_access_checks ();
4129
4130   return success ? parser->scope : NULL_TREE;
4131 }
4132
4133 /* Parse a nested-name-specifier.  See
4134    cp_parser_nested_name_specifier_opt for details.  This function
4135    behaves identically, except that it will an issue an error if no
4136    nested-name-specifier is present.  */
4137
4138 static tree
4139 cp_parser_nested_name_specifier (cp_parser *parser,
4140                                  bool typename_keyword_p,
4141                                  bool check_dependency_p,
4142                                  bool type_p,
4143                                  bool is_declaration)
4144 {
4145   tree scope;
4146
4147   /* Look for the nested-name-specifier.  */
4148   scope = cp_parser_nested_name_specifier_opt (parser,
4149                                                typename_keyword_p,
4150                                                check_dependency_p,
4151                                                type_p,
4152                                                is_declaration);
4153   /* If it was not present, issue an error message.  */
4154   if (!scope)
4155     {
4156       cp_parser_error (parser, "expected nested-name-specifier");
4157       parser->scope = NULL_TREE;
4158     }
4159
4160   return scope;
4161 }
4162
4163 /* Parse a class-or-namespace-name.
4164
4165    class-or-namespace-name:
4166      class-name
4167      namespace-name
4168
4169    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4170    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4171    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4172    TYPE_P is TRUE iff the next name should be taken as a class-name,
4173    even the same name is declared to be another entity in the same
4174    scope.
4175
4176    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4177    specified by the class-or-namespace-name.  If neither is found the
4178    ERROR_MARK_NODE is returned.  */
4179
4180 static tree
4181 cp_parser_class_or_namespace_name (cp_parser *parser,
4182                                    bool typename_keyword_p,
4183                                    bool template_keyword_p,
4184                                    bool check_dependency_p,
4185                                    bool type_p,
4186                                    bool is_declaration)
4187 {
4188   tree saved_scope;
4189   tree saved_qualifying_scope;
4190   tree saved_object_scope;
4191   tree scope;
4192   bool only_class_p;
4193
4194   /* Before we try to parse the class-name, we must save away the
4195      current PARSER->SCOPE since cp_parser_class_name will destroy
4196      it.  */
4197   saved_scope = parser->scope;
4198   saved_qualifying_scope = parser->qualifying_scope;
4199   saved_object_scope = parser->object_scope;
4200   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4201      there is no need to look for a namespace-name.  */
4202   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4203   if (!only_class_p)
4204     cp_parser_parse_tentatively (parser);
4205   scope = cp_parser_class_name (parser,
4206                                 typename_keyword_p,
4207                                 template_keyword_p,
4208                                 type_p ? class_type : none_type,
4209                                 check_dependency_p,
4210                                 /*class_head_p=*/false,
4211                                 is_declaration);
4212   /* If that didn't work, try for a namespace-name.  */
4213   if (!only_class_p && !cp_parser_parse_definitely (parser))
4214     {
4215       /* Restore the saved scope.  */
4216       parser->scope = saved_scope;
4217       parser->qualifying_scope = saved_qualifying_scope;
4218       parser->object_scope = saved_object_scope;
4219       /* If we are not looking at an identifier followed by the scope
4220          resolution operator, then this is not part of a
4221          nested-name-specifier.  (Note that this function is only used
4222          to parse the components of a nested-name-specifier.)  */
4223       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4224           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4225         return error_mark_node;
4226       scope = cp_parser_namespace_name (parser);
4227     }
4228
4229   return scope;
4230 }
4231
4232 /* Parse a postfix-expression.
4233
4234    postfix-expression:
4235      primary-expression
4236      postfix-expression [ expression ]
4237      postfix-expression ( expression-list [opt] )
4238      simple-type-specifier ( expression-list [opt] )
4239      typename :: [opt] nested-name-specifier identifier
4240        ( expression-list [opt] )
4241      typename :: [opt] nested-name-specifier template [opt] template-id
4242        ( expression-list [opt] )
4243      postfix-expression . template [opt] id-expression
4244      postfix-expression -> template [opt] id-expression
4245      postfix-expression . pseudo-destructor-name
4246      postfix-expression -> pseudo-destructor-name
4247      postfix-expression ++
4248      postfix-expression --
4249      dynamic_cast < type-id > ( expression )
4250      static_cast < type-id > ( expression )
4251      reinterpret_cast < type-id > ( expression )
4252      const_cast < type-id > ( expression )
4253      typeid ( expression )
4254      typeid ( type-id )
4255
4256    GNU Extension:
4257
4258    postfix-expression:
4259      ( type-id ) { initializer-list , [opt] }
4260
4261    This extension is a GNU version of the C99 compound-literal
4262    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4263    but they are essentially the same concept.)
4264
4265    If ADDRESS_P is true, the postfix expression is the operand of the
4266    `&' operator.  CAST_P is true if this expression is the target of a
4267    cast.
4268
4269    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4270    class member access expressions [expr.ref].
4271
4272    Returns a representation of the expression.  */
4273
4274 static tree
4275 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4276                               bool member_access_only_p)
4277 {
4278   cp_token *token;
4279   enum rid keyword;
4280   cp_id_kind idk = CP_ID_KIND_NONE;
4281   tree postfix_expression = NULL_TREE;
4282   bool is_member_access = false;
4283
4284   /* Peek at the next token.  */
4285   token = cp_lexer_peek_token (parser->lexer);
4286   /* Some of the productions are determined by keywords.  */
4287   keyword = token->keyword;
4288   switch (keyword)
4289     {
4290     case RID_DYNCAST:
4291     case RID_STATCAST:
4292     case RID_REINTCAST:
4293     case RID_CONSTCAST:
4294       {
4295         tree type;
4296         tree expression;
4297         const char *saved_message;
4298
4299         /* All of these can be handled in the same way from the point
4300            of view of parsing.  Begin by consuming the token
4301            identifying the cast.  */
4302         cp_lexer_consume_token (parser->lexer);
4303
4304         /* New types cannot be defined in the cast.  */
4305         saved_message = parser->type_definition_forbidden_message;
4306         parser->type_definition_forbidden_message
4307           = "types may not be defined in casts";
4308
4309         /* Look for the opening `<'.  */
4310         cp_parser_require (parser, CPP_LESS, "`<'");
4311         /* Parse the type to which we are casting.  */
4312         type = cp_parser_type_id (parser);
4313         /* Look for the closing `>'.  */
4314         cp_parser_require (parser, CPP_GREATER, "`>'");
4315         /* Restore the old message.  */
4316         parser->type_definition_forbidden_message = saved_message;
4317
4318         /* And the expression which is being cast.  */
4319         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4320         expression = cp_parser_expression (parser, /*cast_p=*/true);
4321         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4322
4323         /* Only type conversions to integral or enumeration types
4324            can be used in constant-expressions.  */
4325         if (!cast_valid_in_integral_constant_expression_p (type)
4326             && (cp_parser_non_integral_constant_expression
4327                 (parser,
4328                  "a cast to a type other than an integral or "
4329                  "enumeration type")))
4330           return error_mark_node;
4331
4332         switch (keyword)
4333           {
4334           case RID_DYNCAST:
4335             postfix_expression
4336               = build_dynamic_cast (type, expression);
4337             break;
4338           case RID_STATCAST:
4339             postfix_expression
4340               = build_static_cast (type, expression);
4341             break;
4342           case RID_REINTCAST:
4343             postfix_expression
4344               = build_reinterpret_cast (type, expression);
4345             break;
4346           case RID_CONSTCAST:
4347             postfix_expression
4348               = build_const_cast (type, expression);
4349             break;
4350           default:
4351             gcc_unreachable ();
4352           }
4353       }
4354       break;
4355
4356     case RID_TYPEID:
4357       {
4358         tree type;
4359         const char *saved_message;
4360         bool saved_in_type_id_in_expr_p;
4361
4362         /* Consume the `typeid' token.  */
4363         cp_lexer_consume_token (parser->lexer);
4364         /* Look for the `(' token.  */
4365         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4366         /* Types cannot be defined in a `typeid' expression.  */
4367         saved_message = parser->type_definition_forbidden_message;
4368         parser->type_definition_forbidden_message
4369           = "types may not be defined in a `typeid\' expression";
4370         /* We can't be sure yet whether we're looking at a type-id or an
4371            expression.  */
4372         cp_parser_parse_tentatively (parser);
4373         /* Try a type-id first.  */
4374         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4375         parser->in_type_id_in_expr_p = true;
4376         type = cp_parser_type_id (parser);
4377         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4378         /* Look for the `)' token.  Otherwise, we can't be sure that
4379            we're not looking at an expression: consider `typeid (int
4380            (3))', for example.  */
4381         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4382         /* If all went well, simply lookup the type-id.  */
4383         if (cp_parser_parse_definitely (parser))
4384           postfix_expression = get_typeid (type);
4385         /* Otherwise, fall back to the expression variant.  */
4386         else
4387           {
4388             tree expression;
4389
4390             /* Look for an expression.  */
4391             expression = cp_parser_expression (parser, /*cast_p=*/false);
4392             /* Compute its typeid.  */
4393             postfix_expression = build_typeid (expression);
4394             /* Look for the `)' token.  */
4395             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4396           }
4397         /* Restore the saved message.  */
4398         parser->type_definition_forbidden_message = saved_message;
4399         /* `typeid' may not appear in an integral constant expression.  */
4400         if (cp_parser_non_integral_constant_expression(parser,
4401                                                        "`typeid' operator"))
4402           return error_mark_node;
4403       }
4404       break;
4405
4406     case RID_TYPENAME:
4407       {
4408         tree type;
4409         /* The syntax permitted here is the same permitted for an
4410            elaborated-type-specifier.  */
4411         type = cp_parser_elaborated_type_specifier (parser,
4412                                                     /*is_friend=*/false,
4413                                                     /*is_declaration=*/false);
4414         postfix_expression = cp_parser_functional_cast (parser, type);
4415       }
4416       break;
4417
4418     default:
4419       {
4420         tree type;
4421
4422         /* If the next thing is a simple-type-specifier, we may be
4423            looking at a functional cast.  We could also be looking at
4424            an id-expression.  So, we try the functional cast, and if
4425            that doesn't work we fall back to the primary-expression.  */
4426         cp_parser_parse_tentatively (parser);
4427         /* Look for the simple-type-specifier.  */
4428         type = cp_parser_simple_type_specifier (parser,
4429                                                 /*decl_specs=*/NULL,
4430                                                 CP_PARSER_FLAGS_NONE);
4431         /* Parse the cast itself.  */
4432         if (!cp_parser_error_occurred (parser))
4433           postfix_expression
4434             = cp_parser_functional_cast (parser, type);
4435         /* If that worked, we're done.  */
4436         if (cp_parser_parse_definitely (parser))
4437           break;
4438
4439         /* If the functional-cast didn't work out, try a
4440            compound-literal.  */
4441         if (cp_parser_allow_gnu_extensions_p (parser)
4442             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4443           {
4444             VEC(constructor_elt,gc) *initializer_list = NULL;
4445             bool saved_in_type_id_in_expr_p;
4446
4447             cp_parser_parse_tentatively (parser);
4448             /* Consume the `('.  */
4449             cp_lexer_consume_token (parser->lexer);
4450             /* Parse the type.  */
4451             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4452             parser->in_type_id_in_expr_p = true;
4453             type = cp_parser_type_id (parser);
4454             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4455             /* Look for the `)'.  */
4456             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4457             /* Look for the `{'.  */
4458             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4459             /* If things aren't going well, there's no need to
4460                keep going.  */
4461             if (!cp_parser_error_occurred (parser))
4462               {
4463                 bool non_constant_p;
4464                 /* Parse the initializer-list.  */
4465                 initializer_list
4466                   = cp_parser_initializer_list (parser, &non_constant_p);
4467                 /* Allow a trailing `,'.  */
4468                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4469                   cp_lexer_consume_token (parser->lexer);
4470                 /* Look for the final `}'.  */
4471                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4472               }
4473             /* If that worked, we're definitely looking at a
4474                compound-literal expression.  */
4475             if (cp_parser_parse_definitely (parser))
4476               {
4477                 /* Warn the user that a compound literal is not
4478                    allowed in standard C++.  */
4479                 if (pedantic)
4480                   pedwarn ("ISO C++ forbids compound-literals");
4481                 /* For simplicity, we disallow compound literals in
4482                    constant-expressions.  We could
4483                    allow compound literals of integer type, whose
4484                    initializer was a constant, in constant
4485                    expressions.  Permitting that usage, as a further
4486                    extension, would not change the meaning of any
4487                    currently accepted programs.  (Of course, as
4488                    compound literals are not part of ISO C++, the
4489                    standard has nothing to say.)  */
4490                 if (cp_parser_non_integral_constant_expression 
4491                     (parser, "non-constant compound literals"))
4492                   {
4493                     postfix_expression = error_mark_node;
4494                     break;
4495                   }
4496                 /* Form the representation of the compound-literal.  */
4497                 postfix_expression
4498                   = finish_compound_literal (type, initializer_list);
4499                 break;
4500               }
4501           }
4502
4503         /* It must be a primary-expression.  */
4504         postfix_expression
4505           = cp_parser_primary_expression (parser, address_p, cast_p,
4506                                           /*template_arg_p=*/false,
4507                                           &idk);
4508       }
4509       break;
4510     }
4511
4512   /* Keep looping until the postfix-expression is complete.  */
4513   while (true)
4514     {
4515       if (idk == CP_ID_KIND_UNQUALIFIED
4516           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4517           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4518         /* It is not a Koenig lookup function call.  */
4519         postfix_expression
4520           = unqualified_name_lookup_error (postfix_expression);
4521
4522       /* Peek at the next token.  */
4523       token = cp_lexer_peek_token (parser->lexer);
4524
4525       switch (token->type)
4526         {
4527         case CPP_OPEN_SQUARE:
4528           postfix_expression
4529             = cp_parser_postfix_open_square_expression (parser,
4530                                                         postfix_expression,
4531                                                         false);
4532           idk = CP_ID_KIND_NONE;
4533           is_member_access = false;
4534           break;
4535
4536         case CPP_OPEN_PAREN:
4537           /* postfix-expression ( expression-list [opt] ) */
4538           {
4539             bool koenig_p;
4540             bool is_builtin_constant_p;
4541             bool saved_integral_constant_expression_p = false;
4542             bool saved_non_integral_constant_expression_p = false;
4543             tree args;
4544
4545             is_member_access = false;
4546
4547             is_builtin_constant_p
4548               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4549             if (is_builtin_constant_p)
4550               {
4551                 /* The whole point of __builtin_constant_p is to allow
4552                    non-constant expressions to appear as arguments.  */
4553                 saved_integral_constant_expression_p
4554                   = parser->integral_constant_expression_p;
4555                 saved_non_integral_constant_expression_p
4556                   = parser->non_integral_constant_expression_p;
4557                 parser->integral_constant_expression_p = false;
4558               }
4559             args = (cp_parser_parenthesized_expression_list
4560                     (parser, /*is_attribute_list=*/false,
4561                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4562                      /*non_constant_p=*/NULL));
4563             if (is_builtin_constant_p)
4564               {
4565                 parser->integral_constant_expression_p
4566                   = saved_integral_constant_expression_p;
4567                 parser->non_integral_constant_expression_p
4568                   = saved_non_integral_constant_expression_p;
4569               }
4570
4571             if (args == error_mark_node)
4572               {
4573                 postfix_expression = error_mark_node;
4574                 break;
4575               }
4576
4577             /* Function calls are not permitted in
4578                constant-expressions.  */
4579             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4580                 && cp_parser_non_integral_constant_expression (parser,
4581                                                                "a function call"))
4582               {
4583                 postfix_expression = error_mark_node;
4584                 break;
4585               }
4586
4587             koenig_p = false;
4588             if (idk == CP_ID_KIND_UNQUALIFIED)
4589               {
4590                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4591                   {
4592                     if (args)
4593                       {
4594                         koenig_p = true;
4595                         postfix_expression
4596                           = perform_koenig_lookup (postfix_expression, args);
4597                       }
4598                     else
4599                       postfix_expression
4600                         = unqualified_fn_lookup_error (postfix_expression);
4601                   }
4602                 /* We do not perform argument-dependent lookup if
4603                    normal lookup finds a non-function, in accordance
4604                    with the expected resolution of DR 218.  */
4605                 else if (args && is_overloaded_fn (postfix_expression))
4606                   {
4607                     tree fn = get_first_fn (postfix_expression);
4608
4609                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4610                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4611
4612                     /* Only do argument dependent lookup if regular
4613                        lookup does not find a set of member functions.
4614                        [basic.lookup.koenig]/2a  */
4615                     if (!DECL_FUNCTION_MEMBER_P (fn))
4616                       {
4617                         koenig_p = true;
4618                         postfix_expression
4619                           = perform_koenig_lookup (postfix_expression, args);
4620                       }
4621                   }
4622               }
4623
4624             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4625               {
4626                 tree instance = TREE_OPERAND (postfix_expression, 0);
4627                 tree fn = TREE_OPERAND (postfix_expression, 1);
4628
4629                 if (processing_template_decl
4630                     && (type_dependent_expression_p (instance)
4631                         || (!BASELINK_P (fn)
4632                             && TREE_CODE (fn) != FIELD_DECL)
4633                         || type_dependent_expression_p (fn)
4634                         || any_type_dependent_arguments_p (args)))
4635                   {
4636                     postfix_expression
4637                       = build_nt_call_list (postfix_expression, args);
4638                     break;
4639                   }
4640
4641                 if (BASELINK_P (fn))
4642                   postfix_expression
4643                     = (build_new_method_call
4644                        (instance, fn, args, NULL_TREE,
4645                         (idk == CP_ID_KIND_QUALIFIED
4646                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4647                         /*fn_p=*/NULL));
4648                 else
4649                   postfix_expression
4650                     = finish_call_expr (postfix_expression, args,
4651                                         /*disallow_virtual=*/false,
4652                                         /*koenig_p=*/false);
4653               }
4654             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4655                      || TREE_CODE (postfix_expression) == MEMBER_REF
4656                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4657               postfix_expression = (build_offset_ref_call_from_tree
4658                                     (postfix_expression, args));
4659             else if (idk == CP_ID_KIND_QUALIFIED)
4660               /* A call to a static class member, or a namespace-scope
4661                  function.  */
4662               postfix_expression
4663                 = finish_call_expr (postfix_expression, args,
4664                                     /*disallow_virtual=*/true,
4665                                     koenig_p);
4666             else
4667               /* All other function calls.  */
4668               postfix_expression
4669                 = finish_call_expr (postfix_expression, args,
4670                                     /*disallow_virtual=*/false,
4671                                     koenig_p);
4672
4673             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4674             idk = CP_ID_KIND_NONE;
4675           }
4676           break;
4677
4678         case CPP_DOT:
4679         case CPP_DEREF:
4680           /* postfix-expression . template [opt] id-expression
4681              postfix-expression . pseudo-destructor-name
4682              postfix-expression -> template [opt] id-expression
4683              postfix-expression -> pseudo-destructor-name */
4684
4685           /* Consume the `.' or `->' operator.  */
4686           cp_lexer_consume_token (parser->lexer);
4687
4688           postfix_expression
4689             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4690                                                       postfix_expression,
4691                                                       false, &idk);
4692
4693           is_member_access = true;
4694           break;
4695
4696         case CPP_PLUS_PLUS:
4697           /* postfix-expression ++  */
4698           /* Consume the `++' token.  */
4699           cp_lexer_consume_token (parser->lexer);
4700           /* Generate a representation for the complete expression.  */
4701           postfix_expression
4702             = finish_increment_expr (postfix_expression,
4703                                      POSTINCREMENT_EXPR);
4704           /* Increments may not appear in constant-expressions.  */
4705           if (cp_parser_non_integral_constant_expression (parser,
4706                                                           "an increment"))
4707             postfix_expression = error_mark_node;
4708           idk = CP_ID_KIND_NONE;
4709           is_member_access = false;
4710           break;
4711
4712         case CPP_MINUS_MINUS:
4713           /* postfix-expression -- */
4714           /* Consume the `--' token.  */
4715           cp_lexer_consume_token (parser->lexer);
4716           /* Generate a representation for the complete expression.  */
4717           postfix_expression
4718             = finish_increment_expr (postfix_expression,
4719                                      POSTDECREMENT_EXPR);
4720           /* Decrements may not appear in constant-expressions.  */
4721           if (cp_parser_non_integral_constant_expression (parser,
4722                                                           "a decrement"))
4723             postfix_expression = error_mark_node;
4724           idk = CP_ID_KIND_NONE;
4725           is_member_access = false;
4726           break;
4727
4728         default:
4729           if (member_access_only_p)
4730             return is_member_access? postfix_expression : error_mark_node;
4731           else
4732             return postfix_expression;
4733         }
4734     }
4735
4736   /* We should never get here.  */
4737   gcc_unreachable ();
4738   return error_mark_node;
4739 }
4740
4741 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4742    by cp_parser_builtin_offsetof.  We're looking for
4743
4744      postfix-expression [ expression ]
4745
4746    FOR_OFFSETOF is set if we're being called in that context, which
4747    changes how we deal with integer constant expressions.  */
4748
4749 static tree
4750 cp_parser_postfix_open_square_expression (cp_parser *parser,
4751                                           tree postfix_expression,
4752                                           bool for_offsetof)
4753 {
4754   tree index;
4755
4756   /* Consume the `[' token.  */
4757   cp_lexer_consume_token (parser->lexer);
4758
4759   /* Parse the index expression.  */
4760   /* ??? For offsetof, there is a question of what to allow here.  If
4761      offsetof is not being used in an integral constant expression context,
4762      then we *could* get the right answer by computing the value at runtime.
4763      If we are in an integral constant expression context, then we might
4764      could accept any constant expression; hard to say without analysis.
4765      Rather than open the barn door too wide right away, allow only integer
4766      constant expressions here.  */
4767   if (for_offsetof)
4768     index = cp_parser_constant_expression (parser, false, NULL);
4769   else
4770     index = cp_parser_expression (parser, /*cast_p=*/false);
4771
4772   /* Look for the closing `]'.  */
4773   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4774
4775   /* Build the ARRAY_REF.  */
4776   postfix_expression = grok_array_decl (postfix_expression, index);
4777
4778   /* When not doing offsetof, array references are not permitted in
4779      constant-expressions.  */
4780   if (!for_offsetof
4781       && (cp_parser_non_integral_constant_expression
4782           (parser, "an array reference")))
4783     postfix_expression = error_mark_node;
4784
4785   return postfix_expression;
4786 }
4787
4788 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4789    by cp_parser_builtin_offsetof.  We're looking for
4790
4791      postfix-expression . template [opt] id-expression
4792      postfix-expression . pseudo-destructor-name
4793      postfix-expression -> template [opt] id-expression
4794      postfix-expression -> pseudo-destructor-name
4795
4796    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4797    limits what of the above we'll actually accept, but nevermind.
4798    TOKEN_TYPE is the "." or "->" token, which will already have been
4799    removed from the stream.  */
4800
4801 static tree
4802 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4803                                         enum cpp_ttype token_type,
4804                                         tree postfix_expression,
4805                                         bool for_offsetof, cp_id_kind *idk)
4806 {
4807   tree name;
4808   bool dependent_p;
4809   bool pseudo_destructor_p;
4810   tree scope = NULL_TREE;
4811
4812   /* If this is a `->' operator, dereference the pointer.  */
4813   if (token_type == CPP_DEREF)
4814     postfix_expression = build_x_arrow (postfix_expression);
4815   /* Check to see whether or not the expression is type-dependent.  */
4816   dependent_p = type_dependent_expression_p (postfix_expression);
4817   /* The identifier following the `->' or `.' is not qualified.  */
4818   parser->scope = NULL_TREE;
4819   parser->qualifying_scope = NULL_TREE;
4820   parser->object_scope = NULL_TREE;
4821   *idk = CP_ID_KIND_NONE;
4822   /* Enter the scope corresponding to the type of the object
4823      given by the POSTFIX_EXPRESSION.  */
4824   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4825     {
4826       scope = TREE_TYPE (postfix_expression);
4827       /* According to the standard, no expression should ever have
4828          reference type.  Unfortunately, we do not currently match
4829          the standard in this respect in that our internal representation
4830          of an expression may have reference type even when the standard
4831          says it does not.  Therefore, we have to manually obtain the
4832          underlying type here.  */
4833       scope = non_reference (scope);
4834       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4835       if (scope == unknown_type_node)
4836         {
4837           error ("%qE does not have class type", postfix_expression);
4838           scope = NULL_TREE;
4839         }
4840       else
4841         scope = complete_type_or_else (scope, NULL_TREE);
4842       /* Let the name lookup machinery know that we are processing a
4843          class member access expression.  */
4844       parser->context->object_type = scope;
4845       /* If something went wrong, we want to be able to discern that case,
4846          as opposed to the case where there was no SCOPE due to the type
4847          of expression being dependent.  */
4848       if (!scope)
4849         scope = error_mark_node;
4850       /* If the SCOPE was erroneous, make the various semantic analysis
4851          functions exit quickly -- and without issuing additional error
4852          messages.  */
4853       if (scope == error_mark_node)
4854         postfix_expression = error_mark_node;
4855     }
4856
4857   /* Assume this expression is not a pseudo-destructor access.  */
4858   pseudo_destructor_p = false;
4859
4860   /* If the SCOPE is a scalar type, then, if this is a valid program,
4861      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4862      is type dependent, it can be pseudo-destructor-name or something else.
4863      Try to parse it as pseudo-destructor-name first.  */
4864   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4865     {
4866       tree s;
4867       tree type;
4868
4869       cp_parser_parse_tentatively (parser);
4870       /* Parse the pseudo-destructor-name.  */
4871       s = NULL_TREE;
4872       cp_parser_pseudo_destructor_name (parser, &s, &type);
4873       if (dependent_p
4874           && (cp_parser_error_occurred (parser)
4875               || TREE_CODE (type) != TYPE_DECL
4876               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4877         cp_parser_abort_tentative_parse (parser);
4878       else if (cp_parser_parse_definitely (parser))
4879         {
4880           pseudo_destructor_p = true;
4881           postfix_expression
4882             = finish_pseudo_destructor_expr (postfix_expression,
4883                                              s, TREE_TYPE (type));
4884         }
4885     }
4886
4887   if (!pseudo_destructor_p)
4888     {
4889       /* If the SCOPE is not a scalar type, we are looking at an
4890          ordinary class member access expression, rather than a
4891          pseudo-destructor-name.  */
4892       bool template_p;
4893       /* Parse the id-expression.  */
4894       name = (cp_parser_id_expression
4895               (parser,
4896                cp_parser_optional_template_keyword (parser),
4897                /*check_dependency_p=*/true,
4898                &template_p,
4899                /*declarator_p=*/false,
4900                /*optional_p=*/false));
4901       /* In general, build a SCOPE_REF if the member name is qualified.
4902          However, if the name was not dependent and has already been
4903          resolved; there is no need to build the SCOPE_REF.  For example;
4904
4905              struct X { void f(); };
4906              template <typename T> void f(T* t) { t->X::f(); }
4907
4908          Even though "t" is dependent, "X::f" is not and has been resolved
4909          to a BASELINK; there is no need to include scope information.  */
4910
4911       /* But we do need to remember that there was an explicit scope for
4912          virtual function calls.  */
4913       if (parser->scope)
4914         *idk = CP_ID_KIND_QUALIFIED;
4915
4916       /* If the name is a template-id that names a type, we will get a
4917          TYPE_DECL here.  That is invalid code.  */
4918       if (TREE_CODE (name) == TYPE_DECL)
4919         {
4920           error ("invalid use of %qD", name);
4921           postfix_expression = error_mark_node;
4922         }
4923       else
4924         {
4925           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4926             {
4927               name = build_qualified_name (/*type=*/NULL_TREE,
4928                                            parser->scope,
4929                                            name,
4930                                            template_p);
4931               parser->scope = NULL_TREE;
4932               parser->qualifying_scope = NULL_TREE;
4933               parser->object_scope = NULL_TREE;
4934             }
4935           if (scope && name && BASELINK_P (name))
4936             adjust_result_of_qualified_name_lookup
4937               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4938           postfix_expression
4939             = finish_class_member_access_expr (postfix_expression, name,
4940                                                template_p);
4941         }
4942     }
4943
4944   /* We no longer need to look up names in the scope of the object on
4945      the left-hand side of the `.' or `->' operator.  */
4946   parser->context->object_type = NULL_TREE;
4947
4948   /* Outside of offsetof, these operators may not appear in
4949      constant-expressions.  */
4950   if (!for_offsetof
4951       && (cp_parser_non_integral_constant_expression
4952           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4953     postfix_expression = error_mark_node;
4954
4955   return postfix_expression;
4956 }
4957
4958 /* Parse a parenthesized expression-list.
4959
4960    expression-list:
4961      assignment-expression
4962      expression-list, assignment-expression
4963
4964    attribute-list:
4965      expression-list
4966      identifier
4967      identifier, expression-list
4968
4969    CAST_P is true if this expression is the target of a cast.
4970
4971    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4972    argument pack.
4973
4974    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4975    representation of an assignment-expression.  Note that a TREE_LIST
4976    is returned even if there is only a single expression in the list.
4977    error_mark_node is returned if the ( and or ) are
4978    missing. NULL_TREE is returned on no expressions. The parentheses
4979    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4980    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4981    indicates whether or not all of the expressions in the list were
4982    constant.  */
4983
4984 static tree
4985 cp_parser_parenthesized_expression_list (cp_parser* parser,
4986                                          bool is_attribute_list,
4987                                          bool cast_p,
4988                                          bool allow_expansion_p,
4989                                          bool *non_constant_p)
4990 {
4991   tree expression_list = NULL_TREE;
4992   bool fold_expr_p = is_attribute_list;
4993   tree identifier = NULL_TREE;
4994   bool saved_greater_than_is_operator_p;
4995
4996   /* Assume all the expressions will be constant.  */
4997   if (non_constant_p)
4998     *non_constant_p = false;
4999
5000   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5001     return error_mark_node;
5002
5003   /* Within a parenthesized expression, a `>' token is always
5004      the greater-than operator.  */
5005   saved_greater_than_is_operator_p
5006     = parser->greater_than_is_operator_p;
5007   parser->greater_than_is_operator_p = true;
5008
5009   /* Consume expressions until there are no more.  */
5010   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5011     while (true)
5012       {
5013         tree expr;
5014
5015         /* At the beginning of attribute lists, check to see if the
5016            next token is an identifier.  */
5017         if (is_attribute_list
5018             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5019           {
5020             cp_token *token;
5021
5022             /* Consume the identifier.  */
5023             token = cp_lexer_consume_token (parser->lexer);
5024             /* Save the identifier.  */
5025             identifier = token->u.value;
5026           }
5027         else
5028           {
5029             /* Parse the next assignment-expression.  */
5030             if (non_constant_p)
5031               {
5032                 bool expr_non_constant_p;
5033                 expr = (cp_parser_constant_expression
5034                         (parser, /*allow_non_constant_p=*/true,
5035                          &expr_non_constant_p));
5036                 if (expr_non_constant_p)
5037                   *non_constant_p = true;
5038               }
5039             else
5040               expr = cp_parser_assignment_expression (parser, cast_p);
5041
5042             if (fold_expr_p)
5043               expr = fold_non_dependent_expr (expr);
5044
5045             /* If we have an ellipsis, then this is an expression
5046                expansion.  */
5047             if (allow_expansion_p
5048                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5049               {
5050                 /* Consume the `...'.  */
5051                 cp_lexer_consume_token (parser->lexer);
5052
5053                 /* Build the argument pack.  */
5054                 expr = make_pack_expansion (expr);
5055               }
5056
5057              /* Add it to the list.  We add error_mark_node
5058                 expressions to the list, so that we can still tell if
5059                 the correct form for a parenthesized expression-list
5060                 is found. That gives better errors.  */
5061             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5062
5063             if (expr == error_mark_node)
5064               goto skip_comma;
5065           }
5066
5067         /* After the first item, attribute lists look the same as
5068            expression lists.  */
5069         is_attribute_list = false;
5070
5071       get_comma:;
5072         /* If the next token isn't a `,', then we are done.  */
5073         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5074           break;
5075
5076         /* Otherwise, consume the `,' and keep going.  */
5077         cp_lexer_consume_token (parser->lexer);
5078       }
5079
5080   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5081     {
5082       int ending;
5083
5084     skip_comma:;
5085       /* We try and resync to an unnested comma, as that will give the
5086          user better diagnostics.  */
5087       ending = cp_parser_skip_to_closing_parenthesis (parser,
5088                                                       /*recovering=*/true,
5089                                                       /*or_comma=*/true,
5090                                                       /*consume_paren=*/true);
5091       if (ending < 0)
5092         goto get_comma;
5093       if (!ending)
5094         {
5095           parser->greater_than_is_operator_p
5096             = saved_greater_than_is_operator_p;
5097           return error_mark_node;
5098         }
5099     }
5100
5101   parser->greater_than_is_operator_p
5102     = saved_greater_than_is_operator_p;
5103
5104   /* We built up the list in reverse order so we must reverse it now.  */
5105   expression_list = nreverse (expression_list);
5106   if (identifier)
5107     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5108
5109   return expression_list;
5110 }
5111
5112 /* Parse a pseudo-destructor-name.
5113
5114    pseudo-destructor-name:
5115      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5116      :: [opt] nested-name-specifier template template-id :: ~ type-name
5117      :: [opt] nested-name-specifier [opt] ~ type-name
5118
5119    If either of the first two productions is used, sets *SCOPE to the
5120    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5121    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5122    or ERROR_MARK_NODE if the parse fails.  */
5123
5124 static void
5125 cp_parser_pseudo_destructor_name (cp_parser* parser,
5126                                   tree* scope,
5127                                   tree* type)
5128 {
5129   bool nested_name_specifier_p;
5130
5131   /* Assume that things will not work out.  */
5132   *type = error_mark_node;
5133
5134   /* Look for the optional `::' operator.  */
5135   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5136   /* Look for the optional nested-name-specifier.  */
5137   nested_name_specifier_p
5138     = (cp_parser_nested_name_specifier_opt (parser,
5139                                             /*typename_keyword_p=*/false,
5140                                             /*check_dependency_p=*/true,
5141                                             /*type_p=*/false,
5142                                             /*is_declaration=*/true)
5143        != NULL_TREE);
5144   /* Now, if we saw a nested-name-specifier, we might be doing the
5145      second production.  */
5146   if (nested_name_specifier_p
5147       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5148     {
5149       /* Consume the `template' keyword.  */
5150       cp_lexer_consume_token (parser->lexer);
5151       /* Parse the template-id.  */
5152       cp_parser_template_id (parser,
5153                              /*template_keyword_p=*/true,
5154                              /*check_dependency_p=*/false,
5155                              /*is_declaration=*/true);
5156       /* Look for the `::' token.  */
5157       cp_parser_require (parser, CPP_SCOPE, "`::'");
5158     }
5159   /* If the next token is not a `~', then there might be some
5160      additional qualification.  */
5161   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5162     {
5163       /* At this point, we're looking for "type-name :: ~".  The type-name
5164          must not be a class-name, since this is a pseudo-destructor.  So,
5165          it must be either an enum-name, or a typedef-name -- both of which
5166          are just identifiers.  So, we peek ahead to check that the "::"
5167          and "~" tokens are present; if they are not, then we can avoid
5168          calling type_name.  */
5169       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5170           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5171           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5172         {
5173           cp_parser_error (parser, "non-scalar type");
5174           return;
5175         }
5176
5177       /* Look for the type-name.  */
5178       *scope = TREE_TYPE (cp_parser_type_name (parser));
5179
5180       if (*scope == error_mark_node)
5181         return;
5182
5183       /* Look for the `::' token.  */
5184       cp_parser_require (parser, CPP_SCOPE, "`::'");
5185     }
5186   else
5187     *scope = NULL_TREE;
5188
5189   /* Look for the `~'.  */
5190   cp_parser_require (parser, CPP_COMPL, "`~'");
5191   /* Look for the type-name again.  We are not responsible for
5192      checking that it matches the first type-name.  */
5193   *type = cp_parser_type_name (parser);
5194 }
5195
5196 /* Parse a unary-expression.
5197
5198    unary-expression:
5199      postfix-expression
5200      ++ cast-expression
5201      -- cast-expression
5202      unary-operator cast-expression
5203      sizeof unary-expression
5204      sizeof ( type-id )
5205      new-expression
5206      delete-expression
5207
5208    GNU Extensions:
5209
5210    unary-expression:
5211      __extension__ cast-expression
5212      __alignof__ unary-expression
5213      __alignof__ ( type-id )
5214      __real__ cast-expression
5215      __imag__ cast-expression
5216      && identifier
5217
5218    ADDRESS_P is true iff the unary-expression is appearing as the
5219    operand of the `&' operator.   CAST_P is true if this expression is
5220    the target of a cast.
5221
5222    Returns a representation of the expression.  */
5223
5224 static tree
5225 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5226 {
5227   cp_token *token;
5228   enum tree_code unary_operator;
5229
5230   /* Peek at the next token.  */
5231   token = cp_lexer_peek_token (parser->lexer);
5232   /* Some keywords give away the kind of expression.  */
5233   if (token->type == CPP_KEYWORD)
5234     {
5235       enum rid keyword = token->keyword;
5236
5237       switch (keyword)
5238         {
5239         case RID_ALIGNOF:
5240         case RID_SIZEOF:
5241           {
5242             tree operand;
5243             enum tree_code op;
5244
5245             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5246             /* Consume the token.  */
5247             cp_lexer_consume_token (parser->lexer);
5248             /* Parse the operand.  */
5249             operand = cp_parser_sizeof_operand (parser, keyword);
5250
5251             if (TYPE_P (operand))
5252               return cxx_sizeof_or_alignof_type (operand, op, true);
5253             else
5254               return cxx_sizeof_or_alignof_expr (operand, op);
5255           }
5256
5257         case RID_NEW:
5258           return cp_parser_new_expression (parser);
5259
5260         case RID_DELETE:
5261           return cp_parser_delete_expression (parser);
5262
5263         case RID_EXTENSION:
5264           {
5265             /* The saved value of the PEDANTIC flag.  */
5266             int saved_pedantic;
5267             tree expr;
5268
5269             /* Save away the PEDANTIC flag.  */
5270             cp_parser_extension_opt (parser, &saved_pedantic);
5271             /* Parse the cast-expression.  */
5272             expr = cp_parser_simple_cast_expression (parser);
5273             /* Restore the PEDANTIC flag.  */
5274             pedantic = saved_pedantic;
5275
5276             return expr;
5277           }
5278
5279         case RID_REALPART:
5280         case RID_IMAGPART:
5281           {
5282             tree expression;
5283
5284             /* Consume the `__real__' or `__imag__' token.  */
5285             cp_lexer_consume_token (parser->lexer);
5286             /* Parse the cast-expression.  */
5287             expression = cp_parser_simple_cast_expression (parser);
5288             /* Create the complete representation.  */
5289             return build_x_unary_op ((keyword == RID_REALPART
5290                                       ? REALPART_EXPR : IMAGPART_EXPR),
5291                                      expression);
5292           }
5293           break;
5294
5295         default:
5296           break;
5297         }
5298     }
5299
5300   /* Look for the `:: new' and `:: delete', which also signal the
5301      beginning of a new-expression, or delete-expression,
5302      respectively.  If the next token is `::', then it might be one of
5303      these.  */
5304   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5305     {
5306       enum rid keyword;
5307
5308       /* See if the token after the `::' is one of the keywords in
5309          which we're interested.  */
5310       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5311       /* If it's `new', we have a new-expression.  */
5312       if (keyword == RID_NEW)
5313         return cp_parser_new_expression (parser);
5314       /* Similarly, for `delete'.  */
5315       else if (keyword == RID_DELETE)
5316         return cp_parser_delete_expression (parser);
5317     }
5318
5319   /* Look for a unary operator.  */
5320   unary_operator = cp_parser_unary_operator (token);
5321   /* The `++' and `--' operators can be handled similarly, even though
5322      they are not technically unary-operators in the grammar.  */
5323   if (unary_operator == ERROR_MARK)
5324     {
5325       if (token->type == CPP_PLUS_PLUS)
5326         unary_operator = PREINCREMENT_EXPR;
5327       else if (token->type == CPP_MINUS_MINUS)
5328         unary_operator = PREDECREMENT_EXPR;
5329       /* Handle the GNU address-of-label extension.  */
5330       else if (cp_parser_allow_gnu_extensions_p (parser)
5331                && token->type == CPP_AND_AND)
5332         {
5333           tree identifier;
5334           tree expression;
5335
5336           /* Consume the '&&' token.  */
5337           cp_lexer_consume_token (parser->lexer);
5338           /* Look for the identifier.  */
5339           identifier = cp_parser_identifier (parser);
5340           /* Create an expression representing the address.  */
5341           expression = finish_label_address_expr (identifier);
5342           if (cp_parser_non_integral_constant_expression (parser,
5343                                                 "the address of a label"))
5344             expression = error_mark_node;
5345           return expression;
5346         }
5347     }
5348   if (unary_operator != ERROR_MARK)
5349     {
5350       tree cast_expression;
5351       tree expression = error_mark_node;
5352       const char *non_constant_p = NULL;
5353
5354       /* Consume the operator token.  */
5355       token = cp_lexer_consume_token (parser->lexer);
5356       /* Parse the cast-expression.  */
5357       cast_expression
5358         = cp_parser_cast_expression (parser,
5359                                      unary_operator == ADDR_EXPR,
5360                                      /*cast_p=*/false);
5361       /* Now, build an appropriate representation.  */
5362       switch (unary_operator)
5363         {
5364         case INDIRECT_REF:
5365           non_constant_p = "`*'";
5366           expression = build_x_indirect_ref (cast_expression, "unary *");
5367           break;
5368
5369         case ADDR_EXPR:
5370           non_constant_p = "`&'";
5371           /* Fall through.  */
5372         case BIT_NOT_EXPR:
5373           expression = build_x_unary_op (unary_operator, cast_expression);
5374           break;
5375
5376         case PREINCREMENT_EXPR:
5377         case PREDECREMENT_EXPR:
5378           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5379                             ? "`++'" : "`--'");
5380           /* Fall through.  */
5381         case UNARY_PLUS_EXPR:
5382         case NEGATE_EXPR:
5383         case TRUTH_NOT_EXPR:
5384           expression = finish_unary_op_expr (unary_operator, cast_expression);
5385           break;
5386
5387         default:
5388           gcc_unreachable ();
5389         }
5390
5391       if (non_constant_p
5392           && cp_parser_non_integral_constant_expression (parser,
5393                                                          non_constant_p))
5394         expression = error_mark_node;
5395
5396       return expression;
5397     }
5398
5399   return cp_parser_postfix_expression (parser, address_p, cast_p,
5400                                        /*member_access_only_p=*/false);
5401 }
5402
5403 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5404    unary-operator, the corresponding tree code is returned.  */
5405
5406 static enum tree_code
5407 cp_parser_unary_operator (cp_token* token)
5408 {
5409   switch (token->type)
5410     {
5411     case CPP_MULT:
5412       return INDIRECT_REF;
5413
5414     case CPP_AND:
5415       return ADDR_EXPR;
5416
5417     case CPP_PLUS:
5418       return UNARY_PLUS_EXPR;
5419
5420     case CPP_MINUS:
5421       return NEGATE_EXPR;
5422
5423     case CPP_NOT:
5424       return TRUTH_NOT_EXPR;
5425
5426     case CPP_COMPL:
5427       return BIT_NOT_EXPR;
5428
5429     default:
5430       return ERROR_MARK;
5431     }
5432 }
5433
5434 /* Parse a new-expression.
5435
5436    new-expression:
5437      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5438      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5439
5440    Returns a representation of the expression.  */
5441
5442 static tree
5443 cp_parser_new_expression (cp_parser* parser)
5444 {
5445   bool global_scope_p;
5446   tree placement;
5447   tree type;
5448   tree initializer;
5449   tree nelts;
5450
5451   /* Look for the optional `::' operator.  */
5452   global_scope_p
5453     = (cp_parser_global_scope_opt (parser,
5454                                    /*current_scope_valid_p=*/false)
5455        != NULL_TREE);
5456   /* Look for the `new' operator.  */
5457   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5458   /* There's no easy way to tell a new-placement from the
5459      `( type-id )' construct.  */
5460   cp_parser_parse_tentatively (parser);
5461   /* Look for a new-placement.  */
5462   placement = cp_parser_new_placement (parser);
5463   /* If that didn't work out, there's no new-placement.  */
5464   if (!cp_parser_parse_definitely (parser))
5465     placement = NULL_TREE;
5466
5467   /* If the next token is a `(', then we have a parenthesized
5468      type-id.  */
5469   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5470     {
5471       /* Consume the `('.  */
5472       cp_lexer_consume_token (parser->lexer);
5473       /* Parse the type-id.  */
5474       type = cp_parser_type_id (parser);
5475       /* Look for the closing `)'.  */
5476       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5477       /* There should not be a direct-new-declarator in this production,
5478          but GCC used to allowed this, so we check and emit a sensible error
5479          message for this case.  */
5480       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5481         {
5482           error ("array bound forbidden after parenthesized type-id");
5483           inform ("try removing the parentheses around the type-id");
5484           cp_parser_direct_new_declarator (parser);
5485         }
5486       nelts = NULL_TREE;
5487     }
5488   /* Otherwise, there must be a new-type-id.  */
5489   else
5490     type = cp_parser_new_type_id (parser, &nelts);
5491
5492   /* If the next token is a `(', then we have a new-initializer.  */
5493   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5494     initializer = cp_parser_new_initializer (parser);
5495   else
5496     initializer = NULL_TREE;
5497
5498   /* A new-expression may not appear in an integral constant
5499      expression.  */
5500   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5501     return error_mark_node;
5502
5503   /* Create a representation of the new-expression.  */
5504   return build_new (placement, type, nelts, initializer, global_scope_p);
5505 }
5506
5507 /* Parse a new-placement.
5508
5509    new-placement:
5510      ( expression-list )
5511
5512    Returns the same representation as for an expression-list.  */
5513
5514 static tree
5515 cp_parser_new_placement (cp_parser* parser)
5516 {
5517   tree expression_list;
5518
5519   /* Parse the expression-list.  */
5520   expression_list = (cp_parser_parenthesized_expression_list
5521                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5522                       /*non_constant_p=*/NULL));
5523
5524   return expression_list;
5525 }
5526
5527 /* Parse a new-type-id.
5528
5529    new-type-id:
5530      type-specifier-seq new-declarator [opt]
5531
5532    Returns the TYPE allocated.  If the new-type-id indicates an array
5533    type, *NELTS is set to the number of elements in the last array
5534    bound; the TYPE will not include the last array bound.  */
5535
5536 static tree
5537 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5538 {
5539   cp_decl_specifier_seq type_specifier_seq;
5540   cp_declarator *new_declarator;
5541   cp_declarator *declarator;
5542   cp_declarator *outer_declarator;
5543   const char *saved_message;
5544   tree type;
5545
5546   /* The type-specifier sequence must not contain type definitions.
5547      (It cannot contain declarations of new types either, but if they
5548      are not definitions we will catch that because they are not
5549      complete.)  */
5550   saved_message = parser->type_definition_forbidden_message;
5551   parser->type_definition_forbidden_message
5552     = "types may not be defined in a new-type-id";
5553   /* Parse the type-specifier-seq.  */
5554   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5555                                 &type_specifier_seq);
5556   /* Restore the old message.  */
5557   parser->type_definition_forbidden_message = saved_message;
5558   /* Parse the new-declarator.  */
5559   new_declarator = cp_parser_new_declarator_opt (parser);
5560
5561   /* Determine the number of elements in the last array dimension, if
5562      any.  */
5563   *nelts = NULL_TREE;
5564   /* Skip down to the last array dimension.  */
5565   declarator = new_declarator;
5566   outer_declarator = NULL;
5567   while (declarator && (declarator->kind == cdk_pointer
5568                         || declarator->kind == cdk_ptrmem))
5569     {
5570       outer_declarator = declarator;
5571       declarator = declarator->declarator;
5572     }
5573   while (declarator
5574          && declarator->kind == cdk_array
5575          && declarator->declarator
5576          && declarator->declarator->kind == cdk_array)
5577     {
5578       outer_declarator = declarator;
5579       declarator = declarator->declarator;
5580     }
5581
5582   if (declarator && declarator->kind == cdk_array)
5583     {
5584       *nelts = declarator->u.array.bounds;
5585       if (*nelts == error_mark_node)
5586         *nelts = integer_one_node;
5587
5588       if (outer_declarator)
5589         outer_declarator->declarator = declarator->declarator;
5590       else
5591         new_declarator = NULL;
5592     }
5593
5594   type = groktypename (&type_specifier_seq, new_declarator);
5595   return type;
5596 }
5597
5598 /* Parse an (optional) new-declarator.
5599
5600    new-declarator:
5601      ptr-operator new-declarator [opt]
5602      direct-new-declarator
5603
5604    Returns the declarator.  */
5605
5606 static cp_declarator *
5607 cp_parser_new_declarator_opt (cp_parser* parser)
5608 {
5609   enum tree_code code;
5610   tree type;
5611   cp_cv_quals cv_quals;
5612
5613   /* We don't know if there's a ptr-operator next, or not.  */
5614   cp_parser_parse_tentatively (parser);
5615   /* Look for a ptr-operator.  */
5616   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5617   /* If that worked, look for more new-declarators.  */
5618   if (cp_parser_parse_definitely (parser))
5619     {
5620       cp_declarator *declarator;
5621
5622       /* Parse another optional declarator.  */
5623       declarator = cp_parser_new_declarator_opt (parser);
5624
5625       return cp_parser_make_indirect_declarator
5626         (code, type, cv_quals, declarator);
5627     }
5628
5629   /* If the next token is a `[', there is a direct-new-declarator.  */
5630   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5631     return cp_parser_direct_new_declarator (parser);
5632
5633   return NULL;
5634 }
5635
5636 /* Parse a direct-new-declarator.
5637
5638    direct-new-declarator:
5639      [ expression ]
5640      direct-new-declarator [constant-expression]
5641
5642    */
5643
5644 static cp_declarator *
5645 cp_parser_direct_new_declarator (cp_parser* parser)
5646 {
5647   cp_declarator *declarator = NULL;
5648
5649   while (true)
5650     {
5651       tree expression;
5652
5653       /* Look for the opening `['.  */
5654       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5655       /* The first expression is not required to be constant.  */
5656       if (!declarator)
5657         {
5658           expression = cp_parser_expression (parser, /*cast_p=*/false);
5659           /* The standard requires that the expression have integral
5660              type.  DR 74 adds enumeration types.  We believe that the
5661              real intent is that these expressions be handled like the
5662              expression in a `switch' condition, which also allows
5663              classes with a single conversion to integral or
5664              enumeration type.  */
5665           if (!processing_template_decl)
5666             {
5667               expression
5668                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5669                                               expression,
5670                                               /*complain=*/true);
5671               if (!expression)
5672                 {
5673                   error ("expression in new-declarator must have integral "
5674                          "or enumeration type");
5675                   expression = error_mark_node;
5676                 }
5677             }
5678         }
5679       /* But all the other expressions must be.  */
5680       else
5681         expression
5682           = cp_parser_constant_expression (parser,
5683                                            /*allow_non_constant=*/false,
5684                                            NULL);
5685       /* Look for the closing `]'.  */
5686       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5687
5688       /* Add this bound to the declarator.  */
5689       declarator = make_array_declarator (declarator, expression);
5690
5691       /* If the next token is not a `[', then there are no more
5692          bounds.  */
5693       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5694         break;
5695     }
5696
5697   return declarator;
5698 }
5699
5700 /* Parse a new-initializer.
5701
5702    new-initializer:
5703      ( expression-list [opt] )
5704
5705    Returns a representation of the expression-list.  If there is no
5706    expression-list, VOID_ZERO_NODE is returned.  */
5707
5708 static tree
5709 cp_parser_new_initializer (cp_parser* parser)
5710 {
5711   tree expression_list;
5712
5713   expression_list = (cp_parser_parenthesized_expression_list
5714                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5715                       /*non_constant_p=*/NULL));
5716   if (!expression_list)
5717     expression_list = void_zero_node;
5718
5719   return expression_list;
5720 }
5721
5722 /* Parse a delete-expression.
5723
5724    delete-expression:
5725      :: [opt] delete cast-expression
5726      :: [opt] delete [ ] cast-expression
5727
5728    Returns a representation of the expression.  */
5729
5730 static tree
5731 cp_parser_delete_expression (cp_parser* parser)
5732 {
5733   bool global_scope_p;
5734   bool array_p;
5735   tree expression;
5736
5737   /* Look for the optional `::' operator.  */
5738   global_scope_p
5739     = (cp_parser_global_scope_opt (parser,
5740                                    /*current_scope_valid_p=*/false)
5741        != NULL_TREE);
5742   /* Look for the `delete' keyword.  */
5743   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5744   /* See if the array syntax is in use.  */
5745   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5746     {
5747       /* Consume the `[' token.  */
5748       cp_lexer_consume_token (parser->lexer);
5749       /* Look for the `]' token.  */
5750       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5751       /* Remember that this is the `[]' construct.  */
5752       array_p = true;
5753     }
5754   else
5755     array_p = false;
5756
5757   /* Parse the cast-expression.  */
5758   expression = cp_parser_simple_cast_expression (parser);
5759
5760   /* A delete-expression may not appear in an integral constant
5761      expression.  */
5762   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5763     return error_mark_node;
5764
5765   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5766 }
5767
5768 /* Parse a cast-expression.
5769
5770    cast-expression:
5771      unary-expression
5772      ( type-id ) cast-expression
5773
5774    ADDRESS_P is true iff the unary-expression is appearing as the
5775    operand of the `&' operator.   CAST_P is true if this expression is
5776    the target of a cast.
5777
5778    Returns a representation of the expression.  */
5779
5780 static tree
5781 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5782 {
5783   /* If it's a `(', then we might be looking at a cast.  */
5784   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5785     {
5786       tree type = NULL_TREE;
5787       tree expr = NULL_TREE;
5788       bool compound_literal_p;
5789       const char *saved_message;
5790
5791       /* There's no way to know yet whether or not this is a cast.
5792          For example, `(int (3))' is a unary-expression, while `(int)
5793          3' is a cast.  So, we resort to parsing tentatively.  */
5794       cp_parser_parse_tentatively (parser);
5795       /* Types may not be defined in a cast.  */
5796       saved_message = parser->type_definition_forbidden_message;
5797       parser->type_definition_forbidden_message
5798         = "types may not be defined in casts";
5799       /* Consume the `('.  */
5800       cp_lexer_consume_token (parser->lexer);
5801       /* A very tricky bit is that `(struct S) { 3 }' is a
5802          compound-literal (which we permit in C++ as an extension).
5803          But, that construct is not a cast-expression -- it is a
5804          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5805          is legal; if the compound-literal were a cast-expression,
5806          you'd need an extra set of parentheses.)  But, if we parse
5807          the type-id, and it happens to be a class-specifier, then we
5808          will commit to the parse at that point, because we cannot
5809          undo the action that is done when creating a new class.  So,
5810          then we cannot back up and do a postfix-expression.
5811
5812          Therefore, we scan ahead to the closing `)', and check to see
5813          if the token after the `)' is a `{'.  If so, we are not
5814          looking at a cast-expression.
5815
5816          Save tokens so that we can put them back.  */
5817       cp_lexer_save_tokens (parser->lexer);
5818       /* Skip tokens until the next token is a closing parenthesis.
5819          If we find the closing `)', and the next token is a `{', then
5820          we are looking at a compound-literal.  */
5821       compound_literal_p
5822         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5823                                                   /*consume_paren=*/true)
5824            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5825       /* Roll back the tokens we skipped.  */
5826       cp_lexer_rollback_tokens (parser->lexer);
5827       /* If we were looking at a compound-literal, simulate an error
5828          so that the call to cp_parser_parse_definitely below will
5829          fail.  */
5830       if (compound_literal_p)
5831         cp_parser_simulate_error (parser);
5832       else
5833         {
5834           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5835           parser->in_type_id_in_expr_p = true;
5836           /* Look for the type-id.  */
5837           type = cp_parser_type_id (parser);
5838           /* Look for the closing `)'.  */
5839           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5840           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5841         }
5842
5843       /* Restore the saved message.  */
5844       parser->type_definition_forbidden_message = saved_message;
5845
5846       /* If ok so far, parse the dependent expression. We cannot be
5847          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5848          ctor of T, but looks like a cast to function returning T
5849          without a dependent expression.  */
5850       if (!cp_parser_error_occurred (parser))
5851         expr = cp_parser_cast_expression (parser,
5852                                           /*address_p=*/false,
5853                                           /*cast_p=*/true);
5854
5855       if (cp_parser_parse_definitely (parser))
5856         {
5857           /* Warn about old-style casts, if so requested.  */
5858           if (warn_old_style_cast
5859               && !in_system_header
5860               && !VOID_TYPE_P (type)
5861               && current_lang_name != lang_name_c)
5862             warning (OPT_Wold_style_cast, "use of old-style cast");
5863
5864           /* Only type conversions to integral or enumeration types
5865              can be used in constant-expressions.  */
5866           if (!cast_valid_in_integral_constant_expression_p (type)
5867               && (cp_parser_non_integral_constant_expression
5868                   (parser,
5869                    "a cast to a type other than an integral or "
5870                    "enumeration type")))
5871             return error_mark_node;
5872
5873           /* Perform the cast.  */
5874           expr = build_c_cast (type, expr);
5875           return expr;
5876         }
5877     }
5878
5879   /* If we get here, then it's not a cast, so it must be a
5880      unary-expression.  */
5881   return cp_parser_unary_expression (parser, address_p, cast_p);
5882 }
5883
5884 /* Parse a binary expression of the general form:
5885
5886    pm-expression:
5887      cast-expression
5888      pm-expression .* cast-expression
5889      pm-expression ->* cast-expression
5890
5891    multiplicative-expression:
5892      pm-expression
5893      multiplicative-expression * pm-expression
5894      multiplicative-expression / pm-expression
5895      multiplicative-expression % pm-expression
5896
5897    additive-expression:
5898      multiplicative-expression
5899      additive-expression + multiplicative-expression
5900      additive-expression - multiplicative-expression
5901
5902    shift-expression:
5903      additive-expression
5904      shift-expression << additive-expression
5905      shift-expression >> additive-expression
5906
5907    relational-expression:
5908      shift-expression
5909      relational-expression < shift-expression
5910      relational-expression > shift-expression
5911      relational-expression <= shift-expression
5912      relational-expression >= shift-expression
5913
5914   GNU Extension:
5915
5916    relational-expression:
5917      relational-expression <? shift-expression
5918      relational-expression >? shift-expression
5919
5920    equality-expression:
5921      relational-expression
5922      equality-expression == relational-expression
5923      equality-expression != relational-expression
5924
5925    and-expression:
5926      equality-expression
5927      and-expression & equality-expression
5928
5929    exclusive-or-expression:
5930      and-expression
5931      exclusive-or-expression ^ and-expression
5932
5933    inclusive-or-expression:
5934      exclusive-or-expression
5935      inclusive-or-expression | exclusive-or-expression
5936
5937    logical-and-expression:
5938      inclusive-or-expression
5939      logical-and-expression && inclusive-or-expression
5940
5941    logical-or-expression:
5942      logical-and-expression
5943      logical-or-expression || logical-and-expression
5944
5945    All these are implemented with a single function like:
5946
5947    binary-expression:
5948      simple-cast-expression
5949      binary-expression <token> binary-expression
5950
5951    CAST_P is true if this expression is the target of a cast.
5952
5953    The binops_by_token map is used to get the tree codes for each <token> type.
5954    binary-expressions are associated according to a precedence table.  */
5955
5956 #define TOKEN_PRECEDENCE(token)                              \
5957 (((token->type == CPP_GREATER                                \
5958    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5959   && !parser->greater_than_is_operator_p)                    \
5960  ? PREC_NOT_OPERATOR                                         \
5961  : binops_by_token[token->type].prec)
5962
5963 static tree
5964 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5965 {
5966   cp_parser_expression_stack stack;
5967   cp_parser_expression_stack_entry *sp = &stack[0];
5968   tree lhs, rhs;
5969   cp_token *token;
5970   enum tree_code tree_type, lhs_type, rhs_type;
5971   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5972   bool overloaded_p;
5973
5974   /* Parse the first expression.  */
5975   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5976   lhs_type = ERROR_MARK;
5977
5978   for (;;)
5979     {
5980       /* Get an operator token.  */
5981       token = cp_lexer_peek_token (parser->lexer);
5982
5983       if (warn_cxx0x_compat
5984           && token->type == CPP_RSHIFT
5985           && !parser->greater_than_is_operator_p)
5986         {
5987           warning (OPT_Wc__0x_compat, 
5988                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5989                    &token->location);
5990           warning (OPT_Wc__0x_compat, 
5991                    "suggest parentheses around %<>>%> expression");
5992         }
5993
5994       new_prec = TOKEN_PRECEDENCE (token);
5995
5996       /* Popping an entry off the stack means we completed a subexpression:
5997          - either we found a token which is not an operator (`>' where it is not
5998            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5999            will happen repeatedly;
6000          - or, we found an operator which has lower priority.  This is the case
6001            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6002            parsing `3 * 4'.  */
6003       if (new_prec <= prec)
6004         {
6005           if (sp == stack)
6006             break;
6007           else
6008             goto pop;
6009         }
6010
6011      get_rhs:
6012       tree_type = binops_by_token[token->type].tree_type;
6013
6014       /* We used the operator token.  */
6015       cp_lexer_consume_token (parser->lexer);
6016
6017       /* Extract another operand.  It may be the RHS of this expression
6018          or the LHS of a new, higher priority expression.  */
6019       rhs = cp_parser_simple_cast_expression (parser);
6020       rhs_type = ERROR_MARK;
6021
6022       /* Get another operator token.  Look up its precedence to avoid
6023          building a useless (immediately popped) stack entry for common
6024          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6025       token = cp_lexer_peek_token (parser->lexer);
6026       lookahead_prec = TOKEN_PRECEDENCE (token);
6027       if (lookahead_prec > new_prec)
6028         {
6029           /* ... and prepare to parse the RHS of the new, higher priority
6030              expression.  Since precedence levels on the stack are
6031              monotonically increasing, we do not have to care about
6032              stack overflows.  */
6033           sp->prec = prec;
6034           sp->tree_type = tree_type;
6035           sp->lhs = lhs;
6036           sp->lhs_type = lhs_type;
6037           sp++;
6038           lhs = rhs;
6039           lhs_type = rhs_type;
6040           prec = new_prec;
6041           new_prec = lookahead_prec;
6042           goto get_rhs;
6043
6044          pop:
6045           /* If the stack is not empty, we have parsed into LHS the right side
6046              (`4' in the example above) of an expression we had suspended.
6047              We can use the information on the stack to recover the LHS (`3')
6048              from the stack together with the tree code (`MULT_EXPR'), and
6049              the precedence of the higher level subexpression
6050              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6051              which will be used to actually build the additive expression.  */
6052           --sp;
6053           prec = sp->prec;
6054           tree_type = sp->tree_type;
6055           rhs = lhs;
6056           rhs_type = lhs_type;
6057           lhs = sp->lhs;
6058           lhs_type = sp->lhs_type;
6059         }
6060
6061       overloaded_p = false;
6062       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6063                                &overloaded_p);
6064       lhs_type = tree_type;
6065
6066       /* If the binary operator required the use of an overloaded operator,
6067          then this expression cannot be an integral constant-expression.
6068          An overloaded operator can be used even if both operands are
6069          otherwise permissible in an integral constant-expression if at
6070          least one of the operands is of enumeration type.  */
6071
6072       if (overloaded_p
6073           && (cp_parser_non_integral_constant_expression
6074               (parser, "calls to overloaded operators")))
6075         return error_mark_node;
6076     }
6077
6078   return lhs;
6079 }
6080
6081
6082 /* Parse the `? expression : assignment-expression' part of a
6083    conditional-expression.  The LOGICAL_OR_EXPR is the
6084    logical-or-expression that started the conditional-expression.
6085    Returns a representation of the entire conditional-expression.
6086
6087    This routine is used by cp_parser_assignment_expression.
6088
6089      ? expression : assignment-expression
6090
6091    GNU Extensions:
6092
6093      ? : assignment-expression */
6094
6095 static tree
6096 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6097 {
6098   tree expr;
6099   tree assignment_expr;
6100
6101   /* Consume the `?' token.  */
6102   cp_lexer_consume_token (parser->lexer);
6103   if (cp_parser_allow_gnu_extensions_p (parser)
6104       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6105     /* Implicit true clause.  */
6106     expr = NULL_TREE;
6107   else
6108     /* Parse the expression.  */
6109     expr = cp_parser_expression (parser, /*cast_p=*/false);
6110
6111   /* The next token should be a `:'.  */
6112   cp_parser_require (parser, CPP_COLON, "`:'");
6113   /* Parse the assignment-expression.  */
6114   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6115
6116   /* Build the conditional-expression.  */
6117   return build_x_conditional_expr (logical_or_expr,
6118                                    expr,
6119                                    assignment_expr);
6120 }
6121
6122 /* Parse an assignment-expression.
6123
6124    assignment-expression:
6125      conditional-expression
6126      logical-or-expression assignment-operator assignment_expression
6127      throw-expression
6128
6129    CAST_P is true if this expression is the target of a cast.
6130
6131    Returns a representation for the expression.  */
6132
6133 static tree
6134 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6135 {
6136   tree expr;
6137
6138   /* If the next token is the `throw' keyword, then we're looking at
6139      a throw-expression.  */
6140   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6141     expr = cp_parser_throw_expression (parser);
6142   /* Otherwise, it must be that we are looking at a
6143      logical-or-expression.  */
6144   else
6145     {
6146       /* Parse the binary expressions (logical-or-expression).  */
6147       expr = cp_parser_binary_expression (parser, cast_p);
6148       /* If the next token is a `?' then we're actually looking at a
6149          conditional-expression.  */
6150       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6151         return cp_parser_question_colon_clause (parser, expr);
6152       else
6153         {
6154           enum tree_code assignment_operator;
6155
6156           /* If it's an assignment-operator, we're using the second
6157              production.  */
6158           assignment_operator
6159             = cp_parser_assignment_operator_opt (parser);
6160           if (assignment_operator != ERROR_MARK)
6161             {
6162               tree rhs;
6163
6164               /* Parse the right-hand side of the assignment.  */
6165               rhs = cp_parser_assignment_expression (parser, cast_p);
6166               /* An assignment may not appear in a
6167                  constant-expression.  */
6168               if (cp_parser_non_integral_constant_expression (parser,
6169                                                               "an assignment"))
6170                 return error_mark_node;
6171               /* Build the assignment expression.  */
6172               expr = build_x_modify_expr (expr,
6173                                           assignment_operator,
6174                                           rhs);
6175             }
6176         }
6177     }
6178
6179   return expr;
6180 }
6181
6182 /* Parse an (optional) assignment-operator.
6183
6184    assignment-operator: one of
6185      = *= /= %= += -= >>= <<= &= ^= |=
6186
6187    GNU Extension:
6188
6189    assignment-operator: one of
6190      <?= >?=
6191
6192    If the next token is an assignment operator, the corresponding tree
6193    code is returned, and the token is consumed.  For example, for
6194    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6195    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6196    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6197    operator, ERROR_MARK is returned.  */
6198
6199 static enum tree_code
6200 cp_parser_assignment_operator_opt (cp_parser* parser)
6201 {
6202   enum tree_code op;
6203   cp_token *token;
6204
6205   /* Peek at the next toen.  */
6206   token = cp_lexer_peek_token (parser->lexer);
6207
6208   switch (token->type)
6209     {
6210     case CPP_EQ:
6211       op = NOP_EXPR;
6212       break;
6213
6214     case CPP_MULT_EQ:
6215       op = MULT_EXPR;
6216       break;
6217
6218     case CPP_DIV_EQ:
6219       op = TRUNC_DIV_EXPR;
6220       break;
6221
6222     case CPP_MOD_EQ:
6223       op = TRUNC_MOD_EXPR;
6224       break;
6225
6226     case CPP_PLUS_EQ:
6227       op = PLUS_EXPR;
6228       break;
6229
6230     case CPP_MINUS_EQ:
6231       op = MINUS_EXPR;
6232       break;
6233
6234     case CPP_RSHIFT_EQ:
6235       op = RSHIFT_EXPR;
6236       break;
6237
6238     case CPP_LSHIFT_EQ:
6239       op = LSHIFT_EXPR;
6240       break;
6241
6242     case CPP_AND_EQ:
6243       op = BIT_AND_EXPR;
6244       break;
6245
6246     case CPP_XOR_EQ:
6247       op = BIT_XOR_EXPR;
6248       break;
6249
6250     case CPP_OR_EQ:
6251       op = BIT_IOR_EXPR;
6252       break;
6253
6254     default:
6255       /* Nothing else is an assignment operator.  */
6256       op = ERROR_MARK;
6257     }
6258
6259   /* If it was an assignment operator, consume it.  */
6260   if (op != ERROR_MARK)
6261     cp_lexer_consume_token (parser->lexer);
6262
6263   return op;
6264 }
6265
6266 /* Parse an expression.
6267
6268    expression:
6269      assignment-expression
6270      expression , assignment-expression
6271
6272    CAST_P is true if this expression is the target of a cast.
6273
6274    Returns a representation of the expression.  */
6275
6276 static tree
6277 cp_parser_expression (cp_parser* parser, bool cast_p)
6278 {
6279   tree expression = NULL_TREE;
6280
6281   while (true)
6282     {
6283       tree assignment_expression;
6284
6285       /* Parse the next assignment-expression.  */
6286       assignment_expression
6287         = cp_parser_assignment_expression (parser, cast_p);
6288       /* If this is the first assignment-expression, we can just
6289          save it away.  */
6290       if (!expression)
6291         expression = assignment_expression;
6292       else
6293         expression = build_x_compound_expr (expression,
6294                                             assignment_expression);
6295       /* If the next token is not a comma, then we are done with the
6296          expression.  */
6297       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6298         break;
6299       /* Consume the `,'.  */
6300       cp_lexer_consume_token (parser->lexer);
6301       /* A comma operator cannot appear in a constant-expression.  */
6302       if (cp_parser_non_integral_constant_expression (parser,
6303                                                       "a comma operator"))
6304         expression = error_mark_node;
6305     }
6306
6307   return expression;
6308 }
6309
6310 /* Parse a constant-expression.
6311
6312    constant-expression:
6313      conditional-expression
6314
6315   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6316   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6317   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6318   is false, NON_CONSTANT_P should be NULL.  */
6319
6320 static tree
6321 cp_parser_constant_expression (cp_parser* parser,
6322                                bool allow_non_constant_p,
6323                                bool *non_constant_p)
6324 {
6325   bool saved_integral_constant_expression_p;
6326   bool saved_allow_non_integral_constant_expression_p;
6327   bool saved_non_integral_constant_expression_p;
6328   tree expression;
6329
6330   /* It might seem that we could simply parse the
6331      conditional-expression, and then check to see if it were
6332      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6333      one that the compiler can figure out is constant, possibly after
6334      doing some simplifications or optimizations.  The standard has a
6335      precise definition of constant-expression, and we must honor
6336      that, even though it is somewhat more restrictive.
6337
6338      For example:
6339
6340        int i[(2, 3)];
6341
6342      is not a legal declaration, because `(2, 3)' is not a
6343      constant-expression.  The `,' operator is forbidden in a
6344      constant-expression.  However, GCC's constant-folding machinery
6345      will fold this operation to an INTEGER_CST for `3'.  */
6346
6347   /* Save the old settings.  */
6348   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6349   saved_allow_non_integral_constant_expression_p
6350     = parser->allow_non_integral_constant_expression_p;
6351   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6352   /* We are now parsing a constant-expression.  */
6353   parser->integral_constant_expression_p = true;
6354   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6355   parser->non_integral_constant_expression_p = false;
6356   /* Although the grammar says "conditional-expression", we parse an
6357      "assignment-expression", which also permits "throw-expression"
6358      and the use of assignment operators.  In the case that
6359      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6360      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6361      actually essential that we look for an assignment-expression.
6362      For example, cp_parser_initializer_clauses uses this function to
6363      determine whether a particular assignment-expression is in fact
6364      constant.  */
6365   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6366   /* Restore the old settings.  */
6367   parser->integral_constant_expression_p
6368     = saved_integral_constant_expression_p;
6369   parser->allow_non_integral_constant_expression_p
6370     = saved_allow_non_integral_constant_expression_p;
6371   if (allow_non_constant_p)
6372     *non_constant_p = parser->non_integral_constant_expression_p;
6373   else if (parser->non_integral_constant_expression_p)
6374     expression = error_mark_node;
6375   parser->non_integral_constant_expression_p
6376     = saved_non_integral_constant_expression_p;
6377
6378   return expression;
6379 }
6380
6381 /* Parse __builtin_offsetof.
6382
6383    offsetof-expression:
6384      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6385
6386    offsetof-member-designator:
6387      id-expression
6388      | offsetof-member-designator "." id-expression
6389      | offsetof-member-designator "[" expression "]"  */
6390
6391 static tree
6392 cp_parser_builtin_offsetof (cp_parser *parser)
6393 {
6394   int save_ice_p, save_non_ice_p;
6395   tree type, expr;
6396   cp_id_kind dummy;
6397
6398   /* We're about to accept non-integral-constant things, but will
6399      definitely yield an integral constant expression.  Save and
6400      restore these values around our local parsing.  */
6401   save_ice_p = parser->integral_constant_expression_p;
6402   save_non_ice_p = parser->non_integral_constant_expression_p;
6403
6404   /* Consume the "__builtin_offsetof" token.  */
6405   cp_lexer_consume_token (parser->lexer);
6406   /* Consume the opening `('.  */
6407   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6408   /* Parse the type-id.  */
6409   type = cp_parser_type_id (parser);
6410   /* Look for the `,'.  */
6411   cp_parser_require (parser, CPP_COMMA, "`,'");
6412
6413   /* Build the (type *)null that begins the traditional offsetof macro.  */
6414   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6415
6416   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6417   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6418                                                  true, &dummy);
6419   while (true)
6420     {
6421       cp_token *token = cp_lexer_peek_token (parser->lexer);
6422       switch (token->type)
6423         {
6424         case CPP_OPEN_SQUARE:
6425           /* offsetof-member-designator "[" expression "]" */
6426           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6427           break;
6428
6429         case CPP_DOT:
6430           /* offsetof-member-designator "." identifier */
6431           cp_lexer_consume_token (parser->lexer);
6432           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6433                                                          true, &dummy);
6434           break;
6435
6436         case CPP_CLOSE_PAREN:
6437           /* Consume the ")" token.  */
6438           cp_lexer_consume_token (parser->lexer);
6439           goto success;
6440
6441         default:
6442           /* Error.  We know the following require will fail, but
6443              that gives the proper error message.  */
6444           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6445           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6446           expr = error_mark_node;
6447           goto failure;
6448         }
6449     }
6450
6451  success:
6452   /* If we're processing a template, we can't finish the semantics yet.
6453      Otherwise we can fold the entire expression now.  */
6454   if (processing_template_decl)
6455     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6456   else
6457     expr = finish_offsetof (expr);
6458
6459  failure:
6460   parser->integral_constant_expression_p = save_ice_p;
6461   parser->non_integral_constant_expression_p = save_non_ice_p;
6462
6463   return expr;
6464 }
6465
6466 /* Parse a trait expression.  */
6467
6468 static tree
6469 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6470 {
6471   cp_trait_kind kind;
6472   tree type1, type2 = NULL_TREE;
6473   bool binary = false;
6474   cp_decl_specifier_seq decl_specs;
6475
6476   switch (keyword)
6477     {
6478     case RID_HAS_NOTHROW_ASSIGN:
6479       kind = CPTK_HAS_NOTHROW_ASSIGN;
6480       break;
6481     case RID_HAS_NOTHROW_CONSTRUCTOR:
6482       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6483       break;
6484     case RID_HAS_NOTHROW_COPY:
6485       kind = CPTK_HAS_NOTHROW_COPY;
6486       break;
6487     case RID_HAS_TRIVIAL_ASSIGN:
6488       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6489       break;
6490     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6491       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6492       break;
6493     case RID_HAS_TRIVIAL_COPY:
6494       kind = CPTK_HAS_TRIVIAL_COPY;
6495       break;
6496     case RID_HAS_TRIVIAL_DESTRUCTOR:
6497       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6498       break;
6499     case RID_HAS_VIRTUAL_DESTRUCTOR:
6500       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6501       break;
6502     case RID_IS_ABSTRACT:
6503       kind = CPTK_IS_ABSTRACT;
6504       break;
6505     case RID_IS_BASE_OF:
6506       kind = CPTK_IS_BASE_OF;
6507       binary = true;
6508       break;
6509     case RID_IS_CLASS:
6510       kind = CPTK_IS_CLASS;
6511       break;
6512     case RID_IS_CONVERTIBLE_TO:
6513       kind = CPTK_IS_CONVERTIBLE_TO;
6514       binary = true;
6515       break;
6516     case RID_IS_EMPTY:
6517       kind = CPTK_IS_EMPTY;
6518       break;
6519     case RID_IS_ENUM:
6520       kind = CPTK_IS_ENUM;
6521       break;
6522     case RID_IS_POD:
6523       kind = CPTK_IS_POD;
6524       break;
6525     case RID_IS_POLYMORPHIC:
6526       kind = CPTK_IS_POLYMORPHIC;
6527       break;
6528     case RID_IS_UNION:
6529       kind = CPTK_IS_UNION;
6530       break;
6531     default:
6532       gcc_unreachable ();
6533     }
6534
6535   /* Consume the token.  */
6536   cp_lexer_consume_token (parser->lexer);
6537
6538   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6539
6540   type1 = cp_parser_type_id (parser);
6541
6542   if (type1 == error_mark_node)
6543     return error_mark_node;
6544
6545   /* Build a trivial decl-specifier-seq.  */
6546   clear_decl_specs (&decl_specs);
6547   decl_specs.type = type1;
6548
6549   /* Call grokdeclarator to figure out what type this is.  */
6550   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6551                           /*initialized=*/0, /*attrlist=*/NULL);
6552
6553   if (binary)
6554     {
6555       cp_parser_require (parser, CPP_COMMA, "`,'");
6556  
6557       type2 = cp_parser_type_id (parser);
6558
6559       if (type2 == error_mark_node)
6560         return error_mark_node;
6561
6562       /* Build a trivial decl-specifier-seq.  */
6563       clear_decl_specs (&decl_specs);
6564       decl_specs.type = type2;
6565
6566       /* Call grokdeclarator to figure out what type this is.  */
6567       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6568                               /*initialized=*/0, /*attrlist=*/NULL);
6569     }
6570
6571   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6572
6573   /* Complete the trait expression, which may mean either processing
6574      the trait expr now or saving it for template instantiation.  */
6575   return finish_trait_expr (kind, type1, type2);
6576 }
6577
6578 /* Statements [gram.stmt.stmt]  */
6579
6580 /* Parse a statement.
6581
6582    statement:
6583      labeled-statement
6584      expression-statement
6585      compound-statement
6586      selection-statement
6587      iteration-statement
6588      jump-statement
6589      declaration-statement
6590      try-block
6591
6592   IN_COMPOUND is true when the statement is nested inside a
6593   cp_parser_compound_statement; this matters for certain pragmas.
6594
6595   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6596   is a (possibly labeled) if statement which is not enclosed in braces
6597   and has an else clause.  This is used to implement -Wparentheses.  */
6598
6599 static void
6600 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6601                      bool in_compound, bool *if_p)
6602 {
6603   tree statement;
6604   cp_token *token;
6605   location_t statement_location;
6606
6607  restart:
6608   if (if_p != NULL)
6609     *if_p = false;
6610   /* There is no statement yet.  */
6611   statement = NULL_TREE;
6612   /* Peek at the next token.  */
6613   token = cp_lexer_peek_token (parser->lexer);
6614   /* Remember the location of the first token in the statement.  */
6615   statement_location = token->location;
6616   /* If this is a keyword, then that will often determine what kind of
6617      statement we have.  */
6618   if (token->type == CPP_KEYWORD)
6619     {
6620       enum rid keyword = token->keyword;
6621
6622       switch (keyword)
6623         {
6624         case RID_CASE:
6625         case RID_DEFAULT:
6626           /* Looks like a labeled-statement with a case label.
6627              Parse the label, and then use tail recursion to parse
6628              the statement.  */
6629           cp_parser_label_for_labeled_statement (parser);
6630           goto restart;
6631
6632         case RID_IF:
6633         case RID_SWITCH:
6634           statement = cp_parser_selection_statement (parser, if_p);
6635           break;
6636
6637         case RID_WHILE:
6638         case RID_DO:
6639         case RID_FOR:
6640           statement = cp_parser_iteration_statement (parser);
6641           break;
6642
6643         case RID_BREAK:
6644         case RID_CONTINUE:
6645         case RID_RETURN:
6646         case RID_GOTO:
6647           statement = cp_parser_jump_statement (parser);
6648           break;
6649
6650           /* Objective-C++ exception-handling constructs.  */
6651         case RID_AT_TRY:
6652         case RID_AT_CATCH:
6653         case RID_AT_FINALLY:
6654         case RID_AT_SYNCHRONIZED:
6655         case RID_AT_THROW:
6656           statement = cp_parser_objc_statement (parser);
6657           break;
6658
6659         case RID_TRY:
6660           statement = cp_parser_try_block (parser);
6661           break;
6662
6663         case RID_NAMESPACE:
6664           /* This must be a namespace alias definition.  */
6665           cp_parser_declaration_statement (parser);
6666           return;
6667           
6668         default:
6669           /* It might be a keyword like `int' that can start a
6670              declaration-statement.  */
6671           break;
6672         }
6673     }
6674   else if (token->type == CPP_NAME)
6675     {
6676       /* If the next token is a `:', then we are looking at a
6677          labeled-statement.  */
6678       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6679       if (token->type == CPP_COLON)
6680         {
6681           /* Looks like a labeled-statement with an ordinary label.
6682              Parse the label, and then use tail recursion to parse
6683              the statement.  */
6684           cp_parser_label_for_labeled_statement (parser);
6685           goto restart;
6686         }
6687     }
6688   /* Anything that starts with a `{' must be a compound-statement.  */
6689   else if (token->type == CPP_OPEN_BRACE)
6690     statement = cp_parser_compound_statement (parser, NULL, false);
6691   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6692      a statement all its own.  */
6693   else if (token->type == CPP_PRAGMA)
6694     {
6695       /* Only certain OpenMP pragmas are attached to statements, and thus
6696          are considered statements themselves.  All others are not.  In
6697          the context of a compound, accept the pragma as a "statement" and
6698          return so that we can check for a close brace.  Otherwise we
6699          require a real statement and must go back and read one.  */
6700       if (in_compound)
6701         cp_parser_pragma (parser, pragma_compound);
6702       else if (!cp_parser_pragma (parser, pragma_stmt))
6703         goto restart;
6704       return;
6705     }
6706   else if (token->type == CPP_EOF)
6707     {
6708       cp_parser_error (parser, "expected statement");
6709       return;
6710     }
6711
6712   /* Everything else must be a declaration-statement or an
6713      expression-statement.  Try for the declaration-statement
6714      first, unless we are looking at a `;', in which case we know that
6715      we have an expression-statement.  */
6716   if (!statement)
6717     {
6718       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6719         {
6720           cp_parser_parse_tentatively (parser);
6721           /* Try to parse the declaration-statement.  */
6722           cp_parser_declaration_statement (parser);
6723           /* If that worked, we're done.  */
6724           if (cp_parser_parse_definitely (parser))
6725             return;
6726         }
6727       /* Look for an expression-statement instead.  */
6728       statement = cp_parser_expression_statement (parser, in_statement_expr);
6729     }
6730
6731   /* Set the line number for the statement.  */
6732   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6733     SET_EXPR_LOCATION (statement, statement_location);
6734 }
6735
6736 /* Parse the label for a labeled-statement, i.e.
6737
6738    identifier :
6739    case constant-expression :
6740    default :
6741
6742    GNU Extension:
6743    case constant-expression ... constant-expression : statement
6744
6745    When a label is parsed without errors, the label is added to the
6746    parse tree by the finish_* functions, so this function doesn't
6747    have to return the label.  */
6748
6749 static void
6750 cp_parser_label_for_labeled_statement (cp_parser* parser)
6751 {
6752   cp_token *token;
6753
6754   /* The next token should be an identifier.  */
6755   token = cp_lexer_peek_token (parser->lexer);
6756   if (token->type != CPP_NAME
6757       && token->type != CPP_KEYWORD)
6758     {
6759       cp_parser_error (parser, "expected labeled-statement");
6760       return;
6761     }
6762
6763   switch (token->keyword)
6764     {
6765     case RID_CASE:
6766       {
6767         tree expr, expr_hi;
6768         cp_token *ellipsis;
6769
6770         /* Consume the `case' token.  */
6771         cp_lexer_consume_token (parser->lexer);
6772         /* Parse the constant-expression.  */
6773         expr = cp_parser_constant_expression (parser,
6774                                               /*allow_non_constant_p=*/false,
6775                                               NULL);
6776
6777         ellipsis = cp_lexer_peek_token (parser->lexer);
6778         if (ellipsis->type == CPP_ELLIPSIS)
6779           {
6780             /* Consume the `...' token.  */
6781             cp_lexer_consume_token (parser->lexer);
6782             expr_hi =
6783               cp_parser_constant_expression (parser,
6784                                              /*allow_non_constant_p=*/false,
6785                                              NULL);
6786             /* We don't need to emit warnings here, as the common code
6787                will do this for us.  */
6788           }
6789         else
6790           expr_hi = NULL_TREE;
6791
6792         if (parser->in_switch_statement_p)
6793           finish_case_label (expr, expr_hi);
6794         else
6795           error ("case label %qE not within a switch statement", expr);
6796       }
6797       break;
6798
6799     case RID_DEFAULT:
6800       /* Consume the `default' token.  */
6801       cp_lexer_consume_token (parser->lexer);
6802
6803       if (parser->in_switch_statement_p)
6804         finish_case_label (NULL_TREE, NULL_TREE);
6805       else
6806         error ("case label not within a switch statement");
6807       break;
6808
6809     default:
6810       /* Anything else must be an ordinary label.  */
6811       finish_label_stmt (cp_parser_identifier (parser));
6812       break;
6813     }
6814
6815   /* Require the `:' token.  */
6816   cp_parser_require (parser, CPP_COLON, "`:'");
6817 }
6818
6819 /* Parse an expression-statement.
6820
6821    expression-statement:
6822      expression [opt] ;
6823
6824    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6825    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6826    indicates whether this expression-statement is part of an
6827    expression statement.  */
6828
6829 static tree
6830 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6831 {
6832   tree statement = NULL_TREE;
6833
6834   /* If the next token is a ';', then there is no expression
6835      statement.  */
6836   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6837     statement = cp_parser_expression (parser, /*cast_p=*/false);
6838
6839   /* Consume the final `;'.  */
6840   cp_parser_consume_semicolon_at_end_of_statement (parser);
6841
6842   if (in_statement_expr
6843       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6844     /* This is the final expression statement of a statement
6845        expression.  */
6846     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6847   else if (statement)
6848     statement = finish_expr_stmt (statement);
6849   else
6850     finish_stmt ();
6851
6852   return statement;
6853 }
6854
6855 /* Parse a compound-statement.
6856
6857    compound-statement:
6858      { statement-seq [opt] }
6859
6860    GNU extension:
6861
6862    compound-statement:
6863      { label-declaration-seq [opt] statement-seq [opt] }
6864
6865    label-declaration-seq:
6866      label-declaration
6867      label-declaration-seq label-declaration
6868
6869    Returns a tree representing the statement.  */
6870
6871 static tree
6872 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6873                               bool in_try)
6874 {
6875   tree compound_stmt;
6876
6877   /* Consume the `{'.  */
6878   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6879     return error_mark_node;
6880   /* Begin the compound-statement.  */
6881   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6882   /* If the next keyword is `__label__' we have a label declaration.  */
6883   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6884     cp_parser_label_declaration (parser);
6885   /* Parse an (optional) statement-seq.  */
6886   cp_parser_statement_seq_opt (parser, in_statement_expr);
6887   /* Finish the compound-statement.  */
6888   finish_compound_stmt (compound_stmt);
6889   /* Consume the `}'.  */
6890   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6891
6892   return compound_stmt;
6893 }
6894
6895 /* Parse an (optional) statement-seq.
6896
6897    statement-seq:
6898      statement
6899      statement-seq [opt] statement  */
6900
6901 static void
6902 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6903 {
6904   /* Scan statements until there aren't any more.  */
6905   while (true)
6906     {
6907       cp_token *token = cp_lexer_peek_token (parser->lexer);
6908
6909       /* If we're looking at a `}', then we've run out of statements.  */
6910       if (token->type == CPP_CLOSE_BRACE
6911           || token->type == CPP_EOF
6912           || token->type == CPP_PRAGMA_EOL)
6913         break;
6914       
6915       /* If we are in a compound statement and find 'else' then
6916          something went wrong.  */
6917       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6918         {
6919           if (parser->in_statement & IN_IF_STMT) 
6920             break;
6921           else
6922             {
6923               token = cp_lexer_consume_token (parser->lexer);
6924               error ("%<else%> without a previous %<if%>");
6925             }
6926         }
6927
6928       /* Parse the statement.  */
6929       cp_parser_statement (parser, in_statement_expr, true, NULL);
6930     }
6931 }
6932
6933 /* Parse a selection-statement.
6934
6935    selection-statement:
6936      if ( condition ) statement
6937      if ( condition ) statement else statement
6938      switch ( condition ) statement
6939
6940    Returns the new IF_STMT or SWITCH_STMT.
6941
6942    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6943    is a (possibly labeled) if statement which is not enclosed in
6944    braces and has an else clause.  This is used to implement
6945    -Wparentheses.  */
6946
6947 static tree
6948 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6949 {
6950   cp_token *token;
6951   enum rid keyword;
6952
6953   if (if_p != NULL)
6954     *if_p = false;
6955
6956   /* Peek at the next token.  */
6957   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6958
6959   /* See what kind of keyword it is.  */
6960   keyword = token->keyword;
6961   switch (keyword)
6962     {
6963     case RID_IF:
6964     case RID_SWITCH:
6965       {
6966         tree statement;
6967         tree condition;
6968
6969         /* Look for the `('.  */
6970         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6971           {
6972             cp_parser_skip_to_end_of_statement (parser);
6973             return error_mark_node;
6974           }
6975
6976         /* Begin the selection-statement.  */
6977         if (keyword == RID_IF)
6978           statement = begin_if_stmt ();
6979         else
6980           statement = begin_switch_stmt ();
6981
6982         /* Parse the condition.  */
6983         condition = cp_parser_condition (parser);
6984         /* Look for the `)'.  */
6985         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6986           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6987                                                  /*consume_paren=*/true);
6988
6989         if (keyword == RID_IF)
6990           {
6991             bool nested_if;
6992             unsigned char in_statement;
6993
6994             /* Add the condition.  */
6995             finish_if_stmt_cond (condition, statement);
6996
6997             /* Parse the then-clause.  */
6998             in_statement = parser->in_statement;
6999             parser->in_statement |= IN_IF_STMT;
7000             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7001             parser->in_statement = in_statement;
7002
7003             finish_then_clause (statement);
7004
7005             /* If the next token is `else', parse the else-clause.  */
7006             if (cp_lexer_next_token_is_keyword (parser->lexer,
7007                                                 RID_ELSE))
7008               {
7009                 /* Consume the `else' keyword.  */
7010                 cp_lexer_consume_token (parser->lexer);
7011                 begin_else_clause (statement);
7012                 /* Parse the else-clause.  */
7013                 cp_parser_implicitly_scoped_statement (parser, NULL);
7014                 finish_else_clause (statement);
7015
7016                 /* If we are currently parsing a then-clause, then
7017                    IF_P will not be NULL.  We set it to true to
7018                    indicate that this if statement has an else clause.
7019                    This may trigger the Wparentheses warning below
7020                    when we get back up to the parent if statement.  */
7021                 if (if_p != NULL)
7022                   *if_p = true;
7023               }
7024             else
7025               {
7026                 /* This if statement does not have an else clause.  If
7027                    NESTED_IF is true, then the then-clause is an if
7028                    statement which does have an else clause.  We warn
7029                    about the potential ambiguity.  */
7030                 if (nested_if)
7031                   warning (OPT_Wparentheses,
7032                            ("%Hsuggest explicit braces "
7033                             "to avoid ambiguous %<else%>"),
7034                            EXPR_LOCUS (statement));
7035               }
7036
7037             /* Now we're all done with the if-statement.  */
7038             finish_if_stmt (statement);
7039           }
7040         else
7041           {
7042             bool in_switch_statement_p;
7043             unsigned char in_statement;
7044
7045             /* Add the condition.  */
7046             finish_switch_cond (condition, statement);
7047
7048             /* Parse the body of the switch-statement.  */
7049             in_switch_statement_p = parser->in_switch_statement_p;
7050             in_statement = parser->in_statement;
7051             parser->in_switch_statement_p = true;
7052             parser->in_statement |= IN_SWITCH_STMT;
7053             cp_parser_implicitly_scoped_statement (parser, NULL);
7054             parser->in_switch_statement_p = in_switch_statement_p;
7055             parser->in_statement = in_statement;
7056
7057             /* Now we're all done with the switch-statement.  */
7058             finish_switch_stmt (statement);
7059           }
7060
7061         return statement;
7062       }
7063       break;
7064
7065     default:
7066       cp_parser_error (parser, "expected selection-statement");
7067       return error_mark_node;
7068     }
7069 }
7070
7071 /* Parse a condition.
7072
7073    condition:
7074      expression
7075      type-specifier-seq declarator = assignment-expression
7076
7077    GNU Extension:
7078
7079    condition:
7080      type-specifier-seq declarator asm-specification [opt]
7081        attributes [opt] = assignment-expression
7082
7083    Returns the expression that should be tested.  */
7084
7085 static tree
7086 cp_parser_condition (cp_parser* parser)
7087 {
7088   cp_decl_specifier_seq type_specifiers;
7089   const char *saved_message;
7090
7091   /* Try the declaration first.  */
7092   cp_parser_parse_tentatively (parser);
7093   /* New types are not allowed in the type-specifier-seq for a
7094      condition.  */
7095   saved_message = parser->type_definition_forbidden_message;
7096   parser->type_definition_forbidden_message
7097     = "types may not be defined in conditions";
7098   /* Parse the type-specifier-seq.  */
7099   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7100                                 &type_specifiers);
7101   /* Restore the saved message.  */
7102   parser->type_definition_forbidden_message = saved_message;
7103   /* If all is well, we might be looking at a declaration.  */
7104   if (!cp_parser_error_occurred (parser))
7105     {
7106       tree decl;
7107       tree asm_specification;
7108       tree attributes;
7109       cp_declarator *declarator;
7110       tree initializer = NULL_TREE;
7111
7112       /* Parse the declarator.  */
7113       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7114                                          /*ctor_dtor_or_conv_p=*/NULL,
7115                                          /*parenthesized_p=*/NULL,
7116                                          /*member_p=*/false);
7117       /* Parse the attributes.  */
7118       attributes = cp_parser_attributes_opt (parser);
7119       /* Parse the asm-specification.  */
7120       asm_specification = cp_parser_asm_specification_opt (parser);
7121       /* If the next token is not an `=', then we might still be
7122          looking at an expression.  For example:
7123
7124            if (A(a).x)
7125
7126          looks like a decl-specifier-seq and a declarator -- but then
7127          there is no `=', so this is an expression.  */
7128       cp_parser_require (parser, CPP_EQ, "`='");
7129       /* If we did see an `=', then we are looking at a declaration
7130          for sure.  */
7131       if (cp_parser_parse_definitely (parser))
7132         {
7133           tree pushed_scope;
7134           bool non_constant_p;
7135
7136           /* Create the declaration.  */
7137           decl = start_decl (declarator, &type_specifiers,
7138                              /*initialized_p=*/true,
7139                              attributes, /*prefix_attributes=*/NULL_TREE,
7140                              &pushed_scope);
7141           /* Parse the assignment-expression.  */
7142           initializer
7143             = cp_parser_constant_expression (parser,
7144                                              /*allow_non_constant_p=*/true,
7145                                              &non_constant_p);
7146           if (!non_constant_p)
7147             initializer = fold_non_dependent_expr (initializer);
7148
7149           /* Process the initializer.  */
7150           cp_finish_decl (decl,
7151                           initializer, !non_constant_p,
7152                           asm_specification,
7153                           LOOKUP_ONLYCONVERTING);
7154
7155           if (pushed_scope)
7156             pop_scope (pushed_scope);
7157
7158           return convert_from_reference (decl);
7159         }
7160     }
7161   /* If we didn't even get past the declarator successfully, we are
7162      definitely not looking at a declaration.  */
7163   else
7164     cp_parser_abort_tentative_parse (parser);
7165
7166   /* Otherwise, we are looking at an expression.  */
7167   return cp_parser_expression (parser, /*cast_p=*/false);
7168 }
7169
7170 /* We check for a ) immediately followed by ; with no whitespacing
7171    between.  This is used to issue a warning for:
7172
7173      while (...);
7174
7175    and:
7176
7177      for (...);
7178
7179    as the semicolon is probably extraneous.
7180
7181    On parse errors, the next token might not be a ), so do nothing in
7182    that case. */
7183
7184 static void
7185 check_empty_body (cp_parser* parser, const char* type)
7186 {
7187   cp_token *token;
7188   cp_token *close_paren;
7189   expanded_location close_loc;
7190   expanded_location semi_loc;
7191   
7192   close_paren = cp_lexer_peek_token (parser->lexer);
7193   if (close_paren->type != CPP_CLOSE_PAREN)
7194     return;
7195
7196   close_loc = expand_location (close_paren->location);
7197   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7198
7199   if (token->type != CPP_SEMICOLON
7200       || (token->flags & PREV_WHITE))
7201     return;
7202
7203   semi_loc =  expand_location (token->location);
7204   if (close_loc.line == semi_loc.line
7205       && close_loc.column+1 == semi_loc.column)
7206     warning (OPT_Wempty_body,
7207              "suggest a space before %<;%> or explicit braces around empty "
7208              "body in %<%s%> statement",
7209              type);
7210 }
7211
7212 /* Parse an iteration-statement.
7213
7214    iteration-statement:
7215      while ( condition ) statement
7216      do statement while ( expression ) ;
7217      for ( for-init-statement condition [opt] ; expression [opt] )
7218        statement
7219
7220    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7221
7222 static tree
7223 cp_parser_iteration_statement (cp_parser* parser)
7224 {
7225   cp_token *token;
7226   enum rid keyword;
7227   tree statement;
7228   unsigned char in_statement;
7229
7230   /* Peek at the next token.  */
7231   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7232   if (!token)
7233     return error_mark_node;
7234
7235   /* Remember whether or not we are already within an iteration
7236      statement.  */
7237   in_statement = parser->in_statement;
7238
7239   /* See what kind of keyword it is.  */
7240   keyword = token->keyword;
7241   switch (keyword)
7242     {
7243     case RID_WHILE:
7244       {
7245         tree condition;
7246
7247         /* Begin the while-statement.  */
7248         statement = begin_while_stmt ();
7249         /* Look for the `('.  */
7250         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7251         /* Parse the condition.  */
7252         condition = cp_parser_condition (parser);
7253         finish_while_stmt_cond (condition, statement);
7254         check_empty_body (parser, "while");
7255         /* Look for the `)'.  */
7256         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7257         /* Parse the dependent statement.  */
7258         parser->in_statement = IN_ITERATION_STMT;
7259         cp_parser_already_scoped_statement (parser);
7260         parser->in_statement = in_statement;
7261         /* We're done with the while-statement.  */
7262         finish_while_stmt (statement);
7263       }
7264       break;
7265
7266     case RID_DO:
7267       {
7268         tree expression;
7269
7270         /* Begin the do-statement.  */
7271         statement = begin_do_stmt ();
7272         /* Parse the body of the do-statement.  */
7273         parser->in_statement = IN_ITERATION_STMT;
7274         cp_parser_implicitly_scoped_statement (parser, NULL);
7275         parser->in_statement = in_statement;
7276         finish_do_body (statement);
7277         /* Look for the `while' keyword.  */
7278         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7279         /* Look for the `('.  */
7280         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7281         /* Parse the expression.  */
7282         expression = cp_parser_expression (parser, /*cast_p=*/false);
7283         /* We're done with the do-statement.  */
7284         finish_do_stmt (expression, statement);
7285         /* Look for the `)'.  */
7286         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7287         /* Look for the `;'.  */
7288         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7289       }
7290       break;
7291
7292     case RID_FOR:
7293       {
7294         tree condition = NULL_TREE;
7295         tree expression = NULL_TREE;
7296
7297         /* Begin the for-statement.  */
7298         statement = begin_for_stmt ();
7299         /* Look for the `('.  */
7300         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7301         /* Parse the initialization.  */
7302         cp_parser_for_init_statement (parser);
7303         finish_for_init_stmt (statement);
7304
7305         /* If there's a condition, process it.  */
7306         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7307           condition = cp_parser_condition (parser);
7308         finish_for_cond (condition, statement);
7309         /* Look for the `;'.  */
7310         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7311
7312         /* If there's an expression, process it.  */
7313         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7314           expression = cp_parser_expression (parser, /*cast_p=*/false);
7315         finish_for_expr (expression, statement);
7316         check_empty_body (parser, "for");
7317         /* Look for the `)'.  */
7318         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7319
7320         /* Parse the body of the for-statement.  */
7321         parser->in_statement = IN_ITERATION_STMT;
7322         cp_parser_already_scoped_statement (parser);
7323         parser->in_statement = in_statement;
7324
7325         /* We're done with the for-statement.  */
7326         finish_for_stmt (statement);
7327       }
7328       break;
7329
7330     default:
7331       cp_parser_error (parser, "expected iteration-statement");
7332       statement = error_mark_node;
7333       break;
7334     }
7335
7336   return statement;
7337 }
7338
7339 /* Parse a for-init-statement.
7340
7341    for-init-statement:
7342      expression-statement
7343      simple-declaration  */
7344
7345 static void
7346 cp_parser_for_init_statement (cp_parser* parser)
7347 {
7348   /* If the next token is a `;', then we have an empty
7349      expression-statement.  Grammatically, this is also a
7350      simple-declaration, but an invalid one, because it does not
7351      declare anything.  Therefore, if we did not handle this case
7352      specially, we would issue an error message about an invalid
7353      declaration.  */
7354   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7355     {
7356       /* We're going to speculatively look for a declaration, falling back
7357          to an expression, if necessary.  */
7358       cp_parser_parse_tentatively (parser);
7359       /* Parse the declaration.  */
7360       cp_parser_simple_declaration (parser,
7361                                     /*function_definition_allowed_p=*/false);
7362       /* If the tentative parse failed, then we shall need to look for an
7363          expression-statement.  */
7364       if (cp_parser_parse_definitely (parser))
7365         return;
7366     }
7367
7368   cp_parser_expression_statement (parser, false);
7369 }
7370
7371 /* Parse a jump-statement.
7372
7373    jump-statement:
7374      break ;
7375      continue ;
7376      return expression [opt] ;
7377      goto identifier ;
7378
7379    GNU extension:
7380
7381    jump-statement:
7382      goto * expression ;
7383
7384    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7385
7386 static tree
7387 cp_parser_jump_statement (cp_parser* parser)
7388 {
7389   tree statement = error_mark_node;
7390   cp_token *token;
7391   enum rid keyword;
7392   unsigned char in_statement;
7393
7394   /* Peek at the next token.  */
7395   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7396   if (!token)
7397     return error_mark_node;
7398
7399   /* See what kind of keyword it is.  */
7400   keyword = token->keyword;
7401   switch (keyword)
7402     {
7403     case RID_BREAK:
7404       in_statement = parser->in_statement & ~IN_IF_STMT;      
7405       switch (in_statement)
7406         {
7407         case 0:
7408           error ("break statement not within loop or switch");
7409           break;
7410         default:
7411           gcc_assert ((in_statement & IN_SWITCH_STMT)
7412                       || in_statement == IN_ITERATION_STMT);
7413           statement = finish_break_stmt ();
7414           break;
7415         case IN_OMP_BLOCK:
7416           error ("invalid exit from OpenMP structured block");
7417           break;
7418         case IN_OMP_FOR:
7419           error ("break statement used with OpenMP for loop");
7420           break;
7421         }
7422       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7423       break;
7424
7425     case RID_CONTINUE:
7426       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7427         {
7428         case 0:
7429           error ("continue statement not within a loop");
7430           break;
7431         case IN_ITERATION_STMT:
7432         case IN_OMP_FOR:
7433           statement = finish_continue_stmt ();
7434           break;
7435         case IN_OMP_BLOCK:
7436           error ("invalid exit from OpenMP structured block");
7437           break;
7438         default:
7439           gcc_unreachable ();
7440         }
7441       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7442       break;
7443
7444     case RID_RETURN:
7445       {
7446         tree expr;
7447
7448         /* If the next token is a `;', then there is no
7449            expression.  */
7450         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7451           expr = cp_parser_expression (parser, /*cast_p=*/false);
7452         else
7453           expr = NULL_TREE;
7454         /* Build the return-statement.  */
7455         statement = finish_return_stmt (expr);
7456         /* Look for the final `;'.  */
7457         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7458       }
7459       break;
7460
7461     case RID_GOTO:
7462       /* Create the goto-statement.  */
7463       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7464         {
7465           /* Issue a warning about this use of a GNU extension.  */
7466           if (pedantic)
7467             pedwarn ("ISO C++ forbids computed gotos");
7468           /* Consume the '*' token.  */
7469           cp_lexer_consume_token (parser->lexer);
7470           /* Parse the dependent expression.  */
7471           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7472         }
7473       else
7474         finish_goto_stmt (cp_parser_identifier (parser));
7475       /* Look for the final `;'.  */
7476       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7477       break;
7478
7479     default:
7480       cp_parser_error (parser, "expected jump-statement");
7481       break;
7482     }
7483
7484   return statement;
7485 }
7486
7487 /* Parse a declaration-statement.
7488
7489    declaration-statement:
7490      block-declaration  */
7491
7492 static void
7493 cp_parser_declaration_statement (cp_parser* parser)
7494 {
7495   void *p;
7496
7497   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7498   p = obstack_alloc (&declarator_obstack, 0);
7499
7500  /* Parse the block-declaration.  */
7501   cp_parser_block_declaration (parser, /*statement_p=*/true);
7502
7503   /* Free any declarators allocated.  */
7504   obstack_free (&declarator_obstack, p);
7505
7506   /* Finish off the statement.  */
7507   finish_stmt ();
7508 }
7509
7510 /* Some dependent statements (like `if (cond) statement'), are
7511    implicitly in their own scope.  In other words, if the statement is
7512    a single statement (as opposed to a compound-statement), it is
7513    none-the-less treated as if it were enclosed in braces.  Any
7514    declarations appearing in the dependent statement are out of scope
7515    after control passes that point.  This function parses a statement,
7516    but ensures that is in its own scope, even if it is not a
7517    compound-statement.
7518
7519    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7520    is a (possibly labeled) if statement which is not enclosed in
7521    braces and has an else clause.  This is used to implement
7522    -Wparentheses.
7523
7524    Returns the new statement.  */
7525
7526 static tree
7527 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7528 {
7529   tree statement;
7530
7531   if (if_p != NULL)
7532     *if_p = false;
7533
7534   /* Mark if () ; with a special NOP_EXPR.  */
7535   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7536     {
7537       cp_lexer_consume_token (parser->lexer);
7538       statement = add_stmt (build_empty_stmt ());
7539     }
7540   /* if a compound is opened, we simply parse the statement directly.  */
7541   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7542     statement = cp_parser_compound_statement (parser, NULL, false);
7543   /* If the token is not a `{', then we must take special action.  */
7544   else
7545     {
7546       /* Create a compound-statement.  */
7547       statement = begin_compound_stmt (0);
7548       /* Parse the dependent-statement.  */
7549       cp_parser_statement (parser, NULL_TREE, false, if_p);
7550       /* Finish the dummy compound-statement.  */
7551       finish_compound_stmt (statement);
7552     }
7553
7554   /* Return the statement.  */
7555   return statement;
7556 }
7557
7558 /* For some dependent statements (like `while (cond) statement'), we
7559    have already created a scope.  Therefore, even if the dependent
7560    statement is a compound-statement, we do not want to create another
7561    scope.  */
7562
7563 static void
7564 cp_parser_already_scoped_statement (cp_parser* parser)
7565 {
7566   /* If the token is a `{', then we must take special action.  */
7567   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7568     cp_parser_statement (parser, NULL_TREE, false, NULL);
7569   else
7570     {
7571       /* Avoid calling cp_parser_compound_statement, so that we
7572          don't create a new scope.  Do everything else by hand.  */
7573       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7574       cp_parser_statement_seq_opt (parser, NULL_TREE);
7575       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7576     }
7577 }
7578
7579 /* Declarations [gram.dcl.dcl] */
7580
7581 /* Parse an optional declaration-sequence.
7582
7583    declaration-seq:
7584      declaration
7585      declaration-seq declaration  */
7586
7587 static void
7588 cp_parser_declaration_seq_opt (cp_parser* parser)
7589 {
7590   while (true)
7591     {
7592       cp_token *token;
7593
7594       token = cp_lexer_peek_token (parser->lexer);
7595
7596       if (token->type == CPP_CLOSE_BRACE
7597           || token->type == CPP_EOF
7598           || token->type == CPP_PRAGMA_EOL)
7599         break;
7600
7601       if (token->type == CPP_SEMICOLON)
7602         {
7603           /* A declaration consisting of a single semicolon is
7604              invalid.  Allow it unless we're being pedantic.  */
7605           cp_lexer_consume_token (parser->lexer);
7606           if (pedantic && !in_system_header)
7607             pedwarn ("extra %<;%>");
7608           continue;
7609         }
7610
7611       /* If we're entering or exiting a region that's implicitly
7612          extern "C", modify the lang context appropriately.  */
7613       if (!parser->implicit_extern_c && token->implicit_extern_c)
7614         {
7615           push_lang_context (lang_name_c);
7616           parser->implicit_extern_c = true;
7617         }
7618       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7619         {
7620           pop_lang_context ();
7621           parser->implicit_extern_c = false;
7622         }
7623
7624       if (token->type == CPP_PRAGMA)
7625         {
7626           /* A top-level declaration can consist solely of a #pragma.
7627              A nested declaration cannot, so this is done here and not
7628              in cp_parser_declaration.  (A #pragma at block scope is
7629              handled in cp_parser_statement.)  */
7630           cp_parser_pragma (parser, pragma_external);
7631           continue;
7632         }
7633
7634       /* Parse the declaration itself.  */
7635       cp_parser_declaration (parser);
7636     }
7637 }
7638
7639 /* Parse a declaration.
7640
7641    declaration:
7642      block-declaration
7643      function-definition
7644      template-declaration
7645      explicit-instantiation
7646      explicit-specialization
7647      linkage-specification
7648      namespace-definition
7649
7650    GNU extension:
7651
7652    declaration:
7653       __extension__ declaration */
7654
7655 static void
7656 cp_parser_declaration (cp_parser* parser)
7657 {
7658   cp_token token1;
7659   cp_token token2;
7660   int saved_pedantic;
7661   void *p;
7662
7663   /* Check for the `__extension__' keyword.  */
7664   if (cp_parser_extension_opt (parser, &saved_pedantic))
7665     {
7666       /* Parse the qualified declaration.  */
7667       cp_parser_declaration (parser);
7668       /* Restore the PEDANTIC flag.  */
7669       pedantic = saved_pedantic;
7670
7671       return;
7672     }
7673
7674   /* Try to figure out what kind of declaration is present.  */
7675   token1 = *cp_lexer_peek_token (parser->lexer);
7676
7677   if (token1.type != CPP_EOF)
7678     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7679   else
7680     {
7681       token2.type = CPP_EOF;
7682       token2.keyword = RID_MAX;
7683     }
7684
7685   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7686   p = obstack_alloc (&declarator_obstack, 0);
7687
7688   /* If the next token is `extern' and the following token is a string
7689      literal, then we have a linkage specification.  */
7690   if (token1.keyword == RID_EXTERN
7691       && cp_parser_is_string_literal (&token2))
7692     cp_parser_linkage_specification (parser);
7693   /* If the next token is `template', then we have either a template
7694      declaration, an explicit instantiation, or an explicit
7695      specialization.  */
7696   else if (token1.keyword == RID_TEMPLATE)
7697     {
7698       /* `template <>' indicates a template specialization.  */
7699       if (token2.type == CPP_LESS
7700           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7701         cp_parser_explicit_specialization (parser);
7702       /* `template <' indicates a template declaration.  */
7703       else if (token2.type == CPP_LESS)
7704         cp_parser_template_declaration (parser, /*member_p=*/false);
7705       /* Anything else must be an explicit instantiation.  */
7706       else
7707         cp_parser_explicit_instantiation (parser);
7708     }
7709   /* If the next token is `export', then we have a template
7710      declaration.  */
7711   else if (token1.keyword == RID_EXPORT)
7712     cp_parser_template_declaration (parser, /*member_p=*/false);
7713   /* If the next token is `extern', 'static' or 'inline' and the one
7714      after that is `template', we have a GNU extended explicit
7715      instantiation directive.  */
7716   else if (cp_parser_allow_gnu_extensions_p (parser)
7717            && (token1.keyword == RID_EXTERN
7718                || token1.keyword == RID_STATIC
7719                || token1.keyword == RID_INLINE)
7720            && token2.keyword == RID_TEMPLATE)
7721     cp_parser_explicit_instantiation (parser);
7722   /* If the next token is `namespace', check for a named or unnamed
7723      namespace definition.  */
7724   else if (token1.keyword == RID_NAMESPACE
7725            && (/* A named namespace definition.  */
7726                (token2.type == CPP_NAME
7727                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7728                     != CPP_EQ))
7729                /* An unnamed namespace definition.  */
7730                || token2.type == CPP_OPEN_BRACE
7731                || token2.keyword == RID_ATTRIBUTE))
7732     cp_parser_namespace_definition (parser);
7733   /* An inline (associated) namespace definition.  */
7734   else if (token1.keyword == RID_INLINE
7735            && token2.keyword == RID_NAMESPACE)
7736     cp_parser_namespace_definition (parser);
7737   /* Objective-C++ declaration/definition.  */
7738   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7739     cp_parser_objc_declaration (parser);
7740   /* We must have either a block declaration or a function
7741      definition.  */
7742   else
7743     /* Try to parse a block-declaration, or a function-definition.  */
7744     cp_parser_block_declaration (parser, /*statement_p=*/false);
7745
7746   /* Free any declarators allocated.  */
7747   obstack_free (&declarator_obstack, p);
7748 }
7749
7750 /* Parse a block-declaration.
7751
7752    block-declaration:
7753      simple-declaration
7754      asm-definition
7755      namespace-alias-definition
7756      using-declaration
7757      using-directive
7758
7759    GNU Extension:
7760
7761    block-declaration:
7762      __extension__ block-declaration
7763
7764    C++0x Extension:
7765
7766    block-declaration:
7767      static_assert-declaration
7768
7769    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7770    part of a declaration-statement.  */
7771
7772 static void
7773 cp_parser_block_declaration (cp_parser *parser,
7774                              bool      statement_p)
7775 {
7776   cp_token *token1;
7777   int saved_pedantic;
7778
7779   /* Check for the `__extension__' keyword.  */
7780   if (cp_parser_extension_opt (parser, &saved_pedantic))
7781     {
7782       /* Parse the qualified declaration.  */
7783       cp_parser_block_declaration (parser, statement_p);
7784       /* Restore the PEDANTIC flag.  */
7785       pedantic = saved_pedantic;
7786
7787       return;
7788     }
7789
7790   /* Peek at the next token to figure out which kind of declaration is
7791      present.  */
7792   token1 = cp_lexer_peek_token (parser->lexer);
7793
7794   /* If the next keyword is `asm', we have an asm-definition.  */
7795   if (token1->keyword == RID_ASM)
7796     {
7797       if (statement_p)
7798         cp_parser_commit_to_tentative_parse (parser);
7799       cp_parser_asm_definition (parser);
7800     }
7801   /* If the next keyword is `namespace', we have a
7802      namespace-alias-definition.  */
7803   else if (token1->keyword == RID_NAMESPACE)
7804     cp_parser_namespace_alias_definition (parser);
7805   /* If the next keyword is `using', we have either a
7806      using-declaration or a using-directive.  */
7807   else if (token1->keyword == RID_USING)
7808     {
7809       cp_token *token2;
7810
7811       if (statement_p)
7812         cp_parser_commit_to_tentative_parse (parser);
7813       /* If the token after `using' is `namespace', then we have a
7814          using-directive.  */
7815       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7816       if (token2->keyword == RID_NAMESPACE)
7817         cp_parser_using_directive (parser);
7818       /* Otherwise, it's a using-declaration.  */
7819       else
7820         cp_parser_using_declaration (parser,
7821                                      /*access_declaration_p=*/false);
7822     }
7823   /* If the next keyword is `__label__' we have a misplaced label
7824      declaration.  */
7825   else if (token1->keyword == RID_LABEL)
7826     {
7827       cp_lexer_consume_token (parser->lexer);
7828       error ("%<__label__%> not at the beginning of a block");
7829       cp_parser_skip_to_end_of_statement (parser);
7830       /* If the next token is now a `;', consume it.  */
7831       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7832         cp_lexer_consume_token (parser->lexer);
7833     }
7834   /* If the next token is `static_assert' we have a static assertion.  */
7835   else if (token1->keyword == RID_STATIC_ASSERT)
7836     cp_parser_static_assert (parser, /*member_p=*/false);
7837   /* Anything else must be a simple-declaration.  */
7838   else
7839     cp_parser_simple_declaration (parser, !statement_p);
7840 }
7841
7842 /* Parse a simple-declaration.
7843
7844    simple-declaration:
7845      decl-specifier-seq [opt] init-declarator-list [opt] ;
7846
7847    init-declarator-list:
7848      init-declarator
7849      init-declarator-list , init-declarator
7850
7851    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7852    function-definition as a simple-declaration.  */
7853
7854 static void
7855 cp_parser_simple_declaration (cp_parser* parser,
7856                               bool function_definition_allowed_p)
7857 {
7858   cp_decl_specifier_seq decl_specifiers;
7859   int declares_class_or_enum;
7860   bool saw_declarator;
7861
7862   /* Defer access checks until we know what is being declared; the
7863      checks for names appearing in the decl-specifier-seq should be
7864      done as if we were in the scope of the thing being declared.  */
7865   push_deferring_access_checks (dk_deferred);
7866
7867   /* Parse the decl-specifier-seq.  We have to keep track of whether
7868      or not the decl-specifier-seq declares a named class or
7869      enumeration type, since that is the only case in which the
7870      init-declarator-list is allowed to be empty.
7871
7872      [dcl.dcl]
7873
7874      In a simple-declaration, the optional init-declarator-list can be
7875      omitted only when declaring a class or enumeration, that is when
7876      the decl-specifier-seq contains either a class-specifier, an
7877      elaborated-type-specifier, or an enum-specifier.  */
7878   cp_parser_decl_specifier_seq (parser,
7879                                 CP_PARSER_FLAGS_OPTIONAL,
7880                                 &decl_specifiers,
7881                                 &declares_class_or_enum);
7882   /* We no longer need to defer access checks.  */
7883   stop_deferring_access_checks ();
7884
7885   /* In a block scope, a valid declaration must always have a
7886      decl-specifier-seq.  By not trying to parse declarators, we can
7887      resolve the declaration/expression ambiguity more quickly.  */
7888   if (!function_definition_allowed_p
7889       && !decl_specifiers.any_specifiers_p)
7890     {
7891       cp_parser_error (parser, "expected declaration");
7892       goto done;
7893     }
7894
7895   /* If the next two tokens are both identifiers, the code is
7896      erroneous. The usual cause of this situation is code like:
7897
7898        T t;
7899
7900      where "T" should name a type -- but does not.  */
7901   if (!decl_specifiers.type
7902       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7903     {
7904       /* If parsing tentatively, we should commit; we really are
7905          looking at a declaration.  */
7906       cp_parser_commit_to_tentative_parse (parser);
7907       /* Give up.  */
7908       goto done;
7909     }
7910
7911   /* If we have seen at least one decl-specifier, and the next token
7912      is not a parenthesis, then we must be looking at a declaration.
7913      (After "int (" we might be looking at a functional cast.)  */
7914   if (decl_specifiers.any_specifiers_p
7915       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7916     cp_parser_commit_to_tentative_parse (parser);
7917
7918   /* Keep going until we hit the `;' at the end of the simple
7919      declaration.  */
7920   saw_declarator = false;
7921   while (cp_lexer_next_token_is_not (parser->lexer,
7922                                      CPP_SEMICOLON))
7923     {
7924       cp_token *token;
7925       bool function_definition_p;
7926       tree decl;
7927
7928       if (saw_declarator)
7929         {
7930           /* If we are processing next declarator, coma is expected */
7931           token = cp_lexer_peek_token (parser->lexer);
7932           gcc_assert (token->type == CPP_COMMA);
7933           cp_lexer_consume_token (parser->lexer);
7934         }
7935       else
7936         saw_declarator = true;
7937
7938       /* Parse the init-declarator.  */
7939       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7940                                         /*checks=*/NULL,
7941                                         function_definition_allowed_p,
7942                                         /*member_p=*/false,
7943                                         declares_class_or_enum,
7944                                         &function_definition_p);
7945       /* If an error occurred while parsing tentatively, exit quickly.
7946          (That usually happens when in the body of a function; each
7947          statement is treated as a declaration-statement until proven
7948          otherwise.)  */
7949       if (cp_parser_error_occurred (parser))
7950         goto done;
7951       /* Handle function definitions specially.  */
7952       if (function_definition_p)
7953         {
7954           /* If the next token is a `,', then we are probably
7955              processing something like:
7956
7957                void f() {}, *p;
7958
7959              which is erroneous.  */
7960           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7961             error ("mixing declarations and function-definitions is forbidden");
7962           /* Otherwise, we're done with the list of declarators.  */
7963           else
7964             {
7965               pop_deferring_access_checks ();
7966               return;
7967             }
7968         }
7969       /* The next token should be either a `,' or a `;'.  */
7970       token = cp_lexer_peek_token (parser->lexer);
7971       /* If it's a `,', there are more declarators to come.  */
7972       if (token->type == CPP_COMMA)
7973         /* will be consumed next time around */;
7974       /* If it's a `;', we are done.  */
7975       else if (token->type == CPP_SEMICOLON)
7976         break;
7977       /* Anything else is an error.  */
7978       else
7979         {
7980           /* If we have already issued an error message we don't need
7981              to issue another one.  */
7982           if (decl != error_mark_node
7983               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7984             cp_parser_error (parser, "expected %<,%> or %<;%>");
7985           /* Skip tokens until we reach the end of the statement.  */
7986           cp_parser_skip_to_end_of_statement (parser);
7987           /* If the next token is now a `;', consume it.  */
7988           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7989             cp_lexer_consume_token (parser->lexer);
7990           goto done;
7991         }
7992       /* After the first time around, a function-definition is not
7993          allowed -- even if it was OK at first.  For example:
7994
7995            int i, f() {}
7996
7997          is not valid.  */
7998       function_definition_allowed_p = false;
7999     }
8000
8001   /* Issue an error message if no declarators are present, and the
8002      decl-specifier-seq does not itself declare a class or
8003      enumeration.  */
8004   if (!saw_declarator)
8005     {
8006       if (cp_parser_declares_only_class_p (parser))
8007         shadow_tag (&decl_specifiers);
8008       /* Perform any deferred access checks.  */
8009       perform_deferred_access_checks ();
8010     }
8011
8012   /* Consume the `;'.  */
8013   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8014
8015  done:
8016   pop_deferring_access_checks ();
8017 }
8018
8019 /* Parse a decl-specifier-seq.
8020
8021    decl-specifier-seq:
8022      decl-specifier-seq [opt] decl-specifier
8023
8024    decl-specifier:
8025      storage-class-specifier
8026      type-specifier
8027      function-specifier
8028      friend
8029      typedef
8030
8031    GNU Extension:
8032
8033    decl-specifier:
8034      attributes
8035
8036    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8037
8038    The parser flags FLAGS is used to control type-specifier parsing.
8039
8040    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8041    flags:
8042
8043      1: one of the decl-specifiers is an elaborated-type-specifier
8044         (i.e., a type declaration)
8045      2: one of the decl-specifiers is an enum-specifier or a
8046         class-specifier (i.e., a type definition)
8047
8048    */
8049
8050 static void
8051 cp_parser_decl_specifier_seq (cp_parser* parser,
8052                               cp_parser_flags flags,
8053                               cp_decl_specifier_seq *decl_specs,
8054                               int* declares_class_or_enum)
8055 {
8056   bool constructor_possible_p = !parser->in_declarator_p;
8057
8058   /* Clear DECL_SPECS.  */
8059   clear_decl_specs (decl_specs);
8060
8061   /* Assume no class or enumeration type is declared.  */
8062   *declares_class_or_enum = 0;
8063
8064   /* Keep reading specifiers until there are no more to read.  */
8065   while (true)
8066     {
8067       bool constructor_p;
8068       bool found_decl_spec;
8069       cp_token *token;
8070
8071       /* Peek at the next token.  */
8072       token = cp_lexer_peek_token (parser->lexer);
8073       /* Handle attributes.  */
8074       if (token->keyword == RID_ATTRIBUTE)
8075         {
8076           /* Parse the attributes.  */
8077           decl_specs->attributes
8078             = chainon (decl_specs->attributes,
8079                        cp_parser_attributes_opt (parser));
8080           continue;
8081         }
8082       /* Assume we will find a decl-specifier keyword.  */
8083       found_decl_spec = true;
8084       /* If the next token is an appropriate keyword, we can simply
8085          add it to the list.  */
8086       switch (token->keyword)
8087         {
8088           /* decl-specifier:
8089                friend  */
8090         case RID_FRIEND:
8091           if (!at_class_scope_p ())
8092             {
8093               error ("%<friend%> used outside of class");
8094               cp_lexer_purge_token (parser->lexer);
8095             }
8096           else
8097             {
8098               ++decl_specs->specs[(int) ds_friend];
8099               /* Consume the token.  */
8100               cp_lexer_consume_token (parser->lexer);
8101             }
8102           break;
8103
8104           /* function-specifier:
8105                inline
8106                virtual
8107                explicit  */
8108         case RID_INLINE:
8109         case RID_VIRTUAL:
8110         case RID_EXPLICIT:
8111           cp_parser_function_specifier_opt (parser, decl_specs);
8112           break;
8113
8114           /* decl-specifier:
8115                typedef  */
8116         case RID_TYPEDEF:
8117           ++decl_specs->specs[(int) ds_typedef];
8118           /* Consume the token.  */
8119           cp_lexer_consume_token (parser->lexer);
8120           /* A constructor declarator cannot appear in a typedef.  */
8121           constructor_possible_p = false;
8122           /* The "typedef" keyword can only occur in a declaration; we
8123              may as well commit at this point.  */
8124           cp_parser_commit_to_tentative_parse (parser);
8125
8126           if (decl_specs->storage_class != sc_none)
8127             decl_specs->conflicting_specifiers_p = true;
8128           break;
8129
8130           /* storage-class-specifier:
8131                auto
8132                register
8133                static
8134                extern
8135                mutable
8136
8137              GNU Extension:
8138                thread  */
8139         case RID_AUTO:
8140         case RID_REGISTER:
8141         case RID_STATIC:
8142         case RID_EXTERN:
8143         case RID_MUTABLE:
8144           /* Consume the token.  */
8145           cp_lexer_consume_token (parser->lexer);
8146           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8147           break;
8148         case RID_THREAD:
8149           /* Consume the token.  */
8150           cp_lexer_consume_token (parser->lexer);
8151           ++decl_specs->specs[(int) ds_thread];
8152           break;
8153
8154         default:
8155           /* We did not yet find a decl-specifier yet.  */
8156           found_decl_spec = false;
8157           break;
8158         }
8159
8160       /* Constructors are a special case.  The `S' in `S()' is not a
8161          decl-specifier; it is the beginning of the declarator.  */
8162       constructor_p
8163         = (!found_decl_spec
8164            && constructor_possible_p
8165            && (cp_parser_constructor_declarator_p
8166                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8167
8168       /* If we don't have a DECL_SPEC yet, then we must be looking at
8169          a type-specifier.  */
8170       if (!found_decl_spec && !constructor_p)
8171         {
8172           int decl_spec_declares_class_or_enum;
8173           bool is_cv_qualifier;
8174           tree type_spec;
8175
8176           type_spec
8177             = cp_parser_type_specifier (parser, flags,
8178                                         decl_specs,
8179                                         /*is_declaration=*/true,
8180                                         &decl_spec_declares_class_or_enum,
8181                                         &is_cv_qualifier);
8182
8183           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8184
8185           /* If this type-specifier referenced a user-defined type
8186              (a typedef, class-name, etc.), then we can't allow any
8187              more such type-specifiers henceforth.
8188
8189              [dcl.spec]
8190
8191              The longest sequence of decl-specifiers that could
8192              possibly be a type name is taken as the
8193              decl-specifier-seq of a declaration.  The sequence shall
8194              be self-consistent as described below.
8195
8196              [dcl.type]
8197
8198              As a general rule, at most one type-specifier is allowed
8199              in the complete decl-specifier-seq of a declaration.  The
8200              only exceptions are the following:
8201
8202              -- const or volatile can be combined with any other
8203                 type-specifier.
8204
8205              -- signed or unsigned can be combined with char, long,
8206                 short, or int.
8207
8208              -- ..
8209
8210              Example:
8211
8212                typedef char* Pc;
8213                void g (const int Pc);
8214
8215              Here, Pc is *not* part of the decl-specifier seq; it's
8216              the declarator.  Therefore, once we see a type-specifier
8217              (other than a cv-qualifier), we forbid any additional
8218              user-defined types.  We *do* still allow things like `int
8219              int' to be considered a decl-specifier-seq, and issue the
8220              error message later.  */
8221           if (type_spec && !is_cv_qualifier)
8222             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8223           /* A constructor declarator cannot follow a type-specifier.  */
8224           if (type_spec)
8225             {
8226               constructor_possible_p = false;
8227               found_decl_spec = true;
8228             }
8229         }
8230
8231       /* If we still do not have a DECL_SPEC, then there are no more
8232          decl-specifiers.  */
8233       if (!found_decl_spec)
8234         break;
8235
8236       decl_specs->any_specifiers_p = true;
8237       /* After we see one decl-specifier, further decl-specifiers are
8238          always optional.  */
8239       flags |= CP_PARSER_FLAGS_OPTIONAL;
8240     }
8241
8242   cp_parser_check_decl_spec (decl_specs);
8243
8244   /* Don't allow a friend specifier with a class definition.  */
8245   if (decl_specs->specs[(int) ds_friend] != 0
8246       && (*declares_class_or_enum & 2))
8247     error ("class definition may not be declared a friend");
8248 }
8249
8250 /* Parse an (optional) storage-class-specifier.
8251
8252    storage-class-specifier:
8253      auto
8254      register
8255      static
8256      extern
8257      mutable
8258
8259    GNU Extension:
8260
8261    storage-class-specifier:
8262      thread
8263
8264    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8265
8266 static tree
8267 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8268 {
8269   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8270     {
8271     case RID_AUTO:
8272     case RID_REGISTER:
8273     case RID_STATIC:
8274     case RID_EXTERN:
8275     case RID_MUTABLE:
8276     case RID_THREAD:
8277       /* Consume the token.  */
8278       return cp_lexer_consume_token (parser->lexer)->u.value;
8279
8280     default:
8281       return NULL_TREE;
8282     }
8283 }
8284
8285 /* Parse an (optional) function-specifier.
8286
8287    function-specifier:
8288      inline
8289      virtual
8290      explicit
8291
8292    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8293    Updates DECL_SPECS, if it is non-NULL.  */
8294
8295 static tree
8296 cp_parser_function_specifier_opt (cp_parser* parser,
8297                                   cp_decl_specifier_seq *decl_specs)
8298 {
8299   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8300     {
8301     case RID_INLINE:
8302       if (decl_specs)
8303         ++decl_specs->specs[(int) ds_inline];
8304       break;
8305
8306     case RID_VIRTUAL:
8307       /* 14.5.2.3 [temp.mem]
8308
8309          A member function template shall not be virtual.  */
8310       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8311         error ("templates may not be %<virtual%>");
8312       else if (decl_specs)
8313         ++decl_specs->specs[(int) ds_virtual];
8314       break;
8315
8316     case RID_EXPLICIT:
8317       if (decl_specs)
8318         ++decl_specs->specs[(int) ds_explicit];
8319       break;
8320
8321     default:
8322       return NULL_TREE;
8323     }
8324
8325   /* Consume the token.  */
8326   return cp_lexer_consume_token (parser->lexer)->u.value;
8327 }
8328
8329 /* Parse a linkage-specification.
8330
8331    linkage-specification:
8332      extern string-literal { declaration-seq [opt] }
8333      extern string-literal declaration  */
8334
8335 static void
8336 cp_parser_linkage_specification (cp_parser* parser)
8337 {
8338   tree linkage;
8339
8340   /* Look for the `extern' keyword.  */
8341   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8342
8343   /* Look for the string-literal.  */
8344   linkage = cp_parser_string_literal (parser, false, false);
8345
8346   /* Transform the literal into an identifier.  If the literal is a
8347      wide-character string, or contains embedded NULs, then we can't
8348      handle it as the user wants.  */
8349   if (strlen (TREE_STRING_POINTER (linkage))
8350       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8351     {
8352       cp_parser_error (parser, "invalid linkage-specification");
8353       /* Assume C++ linkage.  */
8354       linkage = lang_name_cplusplus;
8355     }
8356   else
8357     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8358
8359   /* We're now using the new linkage.  */
8360   push_lang_context (linkage);
8361
8362   /* If the next token is a `{', then we're using the first
8363      production.  */
8364   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8365     {
8366       /* Consume the `{' token.  */
8367       cp_lexer_consume_token (parser->lexer);
8368       /* Parse the declarations.  */
8369       cp_parser_declaration_seq_opt (parser);
8370       /* Look for the closing `}'.  */
8371       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8372     }
8373   /* Otherwise, there's just one declaration.  */
8374   else
8375     {
8376       bool saved_in_unbraced_linkage_specification_p;
8377
8378       saved_in_unbraced_linkage_specification_p
8379         = parser->in_unbraced_linkage_specification_p;
8380       parser->in_unbraced_linkage_specification_p = true;
8381       cp_parser_declaration (parser);
8382       parser->in_unbraced_linkage_specification_p
8383         = saved_in_unbraced_linkage_specification_p;
8384     }
8385
8386   /* We're done with the linkage-specification.  */
8387   pop_lang_context ();
8388 }
8389
8390 /* Parse a static_assert-declaration.
8391
8392    static_assert-declaration:
8393      static_assert ( constant-expression , string-literal ) ; 
8394
8395    If MEMBER_P, this static_assert is a class member.  */
8396
8397 static void 
8398 cp_parser_static_assert(cp_parser *parser, bool member_p)
8399 {
8400   tree condition;
8401   tree message;
8402   cp_token *token;
8403   location_t saved_loc;
8404
8405   /* Peek at the `static_assert' token so we can keep track of exactly
8406      where the static assertion started.  */
8407   token = cp_lexer_peek_token (parser->lexer);
8408   saved_loc = token->location;
8409
8410   /* Look for the `static_assert' keyword.  */
8411   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8412                                   "`static_assert'"))
8413     return;
8414
8415   /*  We know we are in a static assertion; commit to any tentative
8416       parse.  */
8417   if (cp_parser_parsing_tentatively (parser))
8418     cp_parser_commit_to_tentative_parse (parser);
8419
8420   /* Parse the `(' starting the static assertion condition.  */
8421   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8422
8423   /* Parse the constant-expression.  */
8424   condition = 
8425     cp_parser_constant_expression (parser,
8426                                    /*allow_non_constant_p=*/false,
8427                                    /*non_constant_p=*/NULL);
8428
8429   /* Parse the separating `,'.  */
8430   cp_parser_require (parser, CPP_COMMA, "`,'");
8431
8432   /* Parse the string-literal message.  */
8433   message = cp_parser_string_literal (parser, 
8434                                       /*translate=*/false,
8435                                       /*wide_ok=*/true);
8436
8437   /* A `)' completes the static assertion.  */
8438   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8439     cp_parser_skip_to_closing_parenthesis (parser, 
8440                                            /*recovering=*/true, 
8441                                            /*or_comma=*/false,
8442                                            /*consume_paren=*/true);
8443
8444   /* A semicolon terminates the declaration.  */
8445   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8446
8447   /* Complete the static assertion, which may mean either processing 
8448      the static assert now or saving it for template instantiation.  */
8449   finish_static_assert (condition, message, saved_loc, member_p);
8450 }
8451
8452 /* Parse a `decltype' type. Returns the type. 
8453
8454    simple-type-specifier:
8455      decltype ( expression )  */
8456
8457 static tree
8458 cp_parser_decltype (cp_parser *parser)
8459 {
8460   tree expr;
8461   bool id_expression_or_member_access_p = false;
8462   const char *saved_message;
8463   bool saved_integral_constant_expression_p;
8464   bool saved_non_integral_constant_expression_p;
8465
8466   /* Look for the `decltype' token.  */
8467   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8468     return error_mark_node;
8469
8470   /* Types cannot be defined in a `decltype' expression.  Save away the
8471      old message.  */
8472   saved_message = parser->type_definition_forbidden_message;
8473
8474   /* And create the new one.  */
8475   parser->type_definition_forbidden_message
8476     = "types may not be defined in `decltype' expressions";
8477
8478   /* The restrictions on constant-expressions do not apply inside
8479      decltype expressions.  */
8480   saved_integral_constant_expression_p
8481     = parser->integral_constant_expression_p;
8482   saved_non_integral_constant_expression_p
8483     = parser->non_integral_constant_expression_p;
8484   parser->integral_constant_expression_p = false;
8485
8486   /* Do not actually evaluate the expression.  */
8487   ++skip_evaluation;
8488
8489   /* Parse the opening `('.  */
8490   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8491     return error_mark_node;
8492   
8493   /* First, try parsing an id-expression.  */
8494   cp_parser_parse_tentatively (parser);
8495   expr = cp_parser_id_expression (parser,
8496                                   /*template_keyword_p=*/false,
8497                                   /*check_dependency_p=*/true,
8498                                   /*template_p=*/NULL,
8499                                   /*declarator_p=*/false,
8500                                   /*optional_p=*/false);
8501
8502   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8503     {
8504       bool non_integral_constant_expression_p = false;
8505       tree id_expression = expr;
8506       cp_id_kind idk;
8507       const char *error_msg;
8508
8509       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8510         /* Lookup the name we got back from the id-expression.  */
8511         expr = cp_parser_lookup_name (parser, expr,
8512                                       none_type,
8513                                       /*is_template=*/false,
8514                                       /*is_namespace=*/false,
8515                                       /*check_dependency=*/true,
8516                                       /*ambiguous_decls=*/NULL);
8517
8518       if (expr
8519           && expr != error_mark_node
8520           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8521           && TREE_CODE (expr) != TYPE_DECL
8522           && (TREE_CODE (expr) != BIT_NOT_EXPR
8523               || !TYPE_P (TREE_OPERAND (expr, 0)))
8524           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8525         {
8526           /* Complete lookup of the id-expression.  */
8527           expr = (finish_id_expression
8528                   (id_expression, expr, parser->scope, &idk,
8529                    /*integral_constant_expression_p=*/false,
8530                    /*allow_non_integral_constant_expression_p=*/true,
8531                    &non_integral_constant_expression_p,
8532                    /*template_p=*/false,
8533                    /*done=*/true,
8534                    /*address_p=*/false,
8535                    /*template_arg_p=*/false,
8536                    &error_msg));
8537
8538           if (expr == error_mark_node)
8539             /* We found an id-expression, but it was something that we
8540                should not have found. This is an error, not something
8541                we can recover from, so note that we found an
8542                id-expression and we'll recover as gracefully as
8543                possible.  */
8544             id_expression_or_member_access_p = true;
8545         }
8546
8547       if (expr 
8548           && expr != error_mark_node
8549           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8550         /* We have an id-expression.  */
8551         id_expression_or_member_access_p = true;
8552     }
8553
8554   if (!id_expression_or_member_access_p)
8555     {
8556       /* Abort the id-expression parse.  */
8557       cp_parser_abort_tentative_parse (parser);
8558
8559       /* Parsing tentatively, again.  */
8560       cp_parser_parse_tentatively (parser);
8561
8562       /* Parse a class member access.  */
8563       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8564                                            /*cast_p=*/false,
8565                                            /*member_access_only_p=*/true);
8566
8567       if (expr 
8568           && expr != error_mark_node
8569           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8570         /* We have an id-expression.  */
8571         id_expression_or_member_access_p = true;
8572     }
8573
8574   if (id_expression_or_member_access_p)
8575     /* We have parsed the complete id-expression or member access.  */
8576     cp_parser_parse_definitely (parser);
8577   else
8578     {
8579       /* Abort our attempt to parse an id-expression or member access
8580          expression.  */
8581       cp_parser_abort_tentative_parse (parser);
8582
8583       /* Parse a full expression.  */
8584       expr = cp_parser_expression (parser, /*cast_p=*/false);
8585     }
8586
8587   /* Go back to evaluating expressions.  */
8588   --skip_evaluation;
8589
8590   /* Restore the old message and the integral constant expression
8591      flags.  */
8592   parser->type_definition_forbidden_message = saved_message;
8593   parser->integral_constant_expression_p
8594     = saved_integral_constant_expression_p;
8595   parser->non_integral_constant_expression_p
8596     = saved_non_integral_constant_expression_p;
8597
8598   if (expr == error_mark_node)
8599     {
8600       /* Skip everything up to the closing `)'.  */
8601       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8602                                              /*consume_paren=*/true);
8603       return error_mark_node;
8604     }
8605   
8606   /* Parse to the closing `)'.  */
8607   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8608     {
8609       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8610                                              /*consume_paren=*/true);
8611       return error_mark_node;
8612     }
8613
8614   return finish_decltype_type (expr, id_expression_or_member_access_p);
8615 }
8616
8617 /* Special member functions [gram.special] */
8618
8619 /* Parse a conversion-function-id.
8620
8621    conversion-function-id:
8622      operator conversion-type-id
8623
8624    Returns an IDENTIFIER_NODE representing the operator.  */
8625
8626 static tree
8627 cp_parser_conversion_function_id (cp_parser* parser)
8628 {
8629   tree type;
8630   tree saved_scope;
8631   tree saved_qualifying_scope;
8632   tree saved_object_scope;
8633   tree pushed_scope = NULL_TREE;
8634
8635   /* Look for the `operator' token.  */
8636   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8637     return error_mark_node;
8638   /* When we parse the conversion-type-id, the current scope will be
8639      reset.  However, we need that information in able to look up the
8640      conversion function later, so we save it here.  */
8641   saved_scope = parser->scope;
8642   saved_qualifying_scope = parser->qualifying_scope;
8643   saved_object_scope = parser->object_scope;
8644   /* We must enter the scope of the class so that the names of
8645      entities declared within the class are available in the
8646      conversion-type-id.  For example, consider:
8647
8648        struct S {
8649          typedef int I;
8650          operator I();
8651        };
8652
8653        S::operator I() { ... }
8654
8655      In order to see that `I' is a type-name in the definition, we
8656      must be in the scope of `S'.  */
8657   if (saved_scope)
8658     pushed_scope = push_scope (saved_scope);
8659   /* Parse the conversion-type-id.  */
8660   type = cp_parser_conversion_type_id (parser);
8661   /* Leave the scope of the class, if any.  */
8662   if (pushed_scope)
8663     pop_scope (pushed_scope);
8664   /* Restore the saved scope.  */
8665   parser->scope = saved_scope;
8666   parser->qualifying_scope = saved_qualifying_scope;
8667   parser->object_scope = saved_object_scope;
8668   /* If the TYPE is invalid, indicate failure.  */
8669   if (type == error_mark_node)
8670     return error_mark_node;
8671   return mangle_conv_op_name_for_type (type);
8672 }
8673
8674 /* Parse a conversion-type-id:
8675
8676    conversion-type-id:
8677      type-specifier-seq conversion-declarator [opt]
8678
8679    Returns the TYPE specified.  */
8680
8681 static tree
8682 cp_parser_conversion_type_id (cp_parser* parser)
8683 {
8684   tree attributes;
8685   cp_decl_specifier_seq type_specifiers;
8686   cp_declarator *declarator;
8687   tree type_specified;
8688
8689   /* Parse the attributes.  */
8690   attributes = cp_parser_attributes_opt (parser);
8691   /* Parse the type-specifiers.  */
8692   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8693                                 &type_specifiers);
8694   /* If that didn't work, stop.  */
8695   if (type_specifiers.type == error_mark_node)
8696     return error_mark_node;
8697   /* Parse the conversion-declarator.  */
8698   declarator = cp_parser_conversion_declarator_opt (parser);
8699
8700   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8701                                     /*initialized=*/0, &attributes);
8702   if (attributes)
8703     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8704   return type_specified;
8705 }
8706
8707 /* Parse an (optional) conversion-declarator.
8708
8709    conversion-declarator:
8710      ptr-operator conversion-declarator [opt]
8711
8712    */
8713
8714 static cp_declarator *
8715 cp_parser_conversion_declarator_opt (cp_parser* parser)
8716 {
8717   enum tree_code code;
8718   tree class_type;
8719   cp_cv_quals cv_quals;
8720
8721   /* We don't know if there's a ptr-operator next, or not.  */
8722   cp_parser_parse_tentatively (parser);
8723   /* Try the ptr-operator.  */
8724   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8725   /* If it worked, look for more conversion-declarators.  */
8726   if (cp_parser_parse_definitely (parser))
8727     {
8728       cp_declarator *declarator;
8729
8730       /* Parse another optional declarator.  */
8731       declarator = cp_parser_conversion_declarator_opt (parser);
8732
8733       return cp_parser_make_indirect_declarator
8734         (code, class_type, cv_quals, declarator);
8735    }
8736
8737   return NULL;
8738 }
8739
8740 /* Parse an (optional) ctor-initializer.
8741
8742    ctor-initializer:
8743      : mem-initializer-list
8744
8745    Returns TRUE iff the ctor-initializer was actually present.  */
8746
8747 static bool
8748 cp_parser_ctor_initializer_opt (cp_parser* parser)
8749 {
8750   /* If the next token is not a `:', then there is no
8751      ctor-initializer.  */
8752   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8753     {
8754       /* Do default initialization of any bases and members.  */
8755       if (DECL_CONSTRUCTOR_P (current_function_decl))
8756         finish_mem_initializers (NULL_TREE);
8757
8758       return false;
8759     }
8760
8761   /* Consume the `:' token.  */
8762   cp_lexer_consume_token (parser->lexer);
8763   /* And the mem-initializer-list.  */
8764   cp_parser_mem_initializer_list (parser);
8765
8766   return true;
8767 }
8768
8769 /* Parse a mem-initializer-list.
8770
8771    mem-initializer-list:
8772      mem-initializer ... [opt]
8773      mem-initializer ... [opt] , mem-initializer-list  */
8774
8775 static void
8776 cp_parser_mem_initializer_list (cp_parser* parser)
8777 {
8778   tree mem_initializer_list = NULL_TREE;
8779
8780   /* Let the semantic analysis code know that we are starting the
8781      mem-initializer-list.  */
8782   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8783     error ("only constructors take base initializers");
8784
8785   /* Loop through the list.  */
8786   while (true)
8787     {
8788       tree mem_initializer;
8789
8790       /* Parse the mem-initializer.  */
8791       mem_initializer = cp_parser_mem_initializer (parser);
8792       /* If the next token is a `...', we're expanding member initializers. */
8793       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8794         {
8795           /* Consume the `...'. */
8796           cp_lexer_consume_token (parser->lexer);
8797
8798           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8799              can be expanded but members cannot. */
8800           if (mem_initializer != error_mark_node
8801               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8802             {
8803               error ("cannot expand initializer for member %<%D%>", 
8804                      TREE_PURPOSE (mem_initializer));
8805               mem_initializer = error_mark_node;
8806             }
8807
8808           /* Construct the pack expansion type. */
8809           if (mem_initializer != error_mark_node)
8810             mem_initializer = make_pack_expansion (mem_initializer);
8811         }
8812       /* Add it to the list, unless it was erroneous.  */
8813       if (mem_initializer != error_mark_node)
8814         {
8815           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8816           mem_initializer_list = mem_initializer;
8817         }
8818       /* If the next token is not a `,', we're done.  */
8819       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8820         break;
8821       /* Consume the `,' token.  */
8822       cp_lexer_consume_token (parser->lexer);
8823     }
8824
8825   /* Perform semantic analysis.  */
8826   if (DECL_CONSTRUCTOR_P (current_function_decl))
8827     finish_mem_initializers (mem_initializer_list);
8828 }
8829
8830 /* Parse a mem-initializer.
8831
8832    mem-initializer:
8833      mem-initializer-id ( expression-list [opt] )
8834
8835    GNU extension:
8836
8837    mem-initializer:
8838      ( expression-list [opt] )
8839
8840    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8841    class) or FIELD_DECL (for a non-static data member) to initialize;
8842    the TREE_VALUE is the expression-list.  An empty initialization
8843    list is represented by void_list_node.  */
8844
8845 static tree
8846 cp_parser_mem_initializer (cp_parser* parser)
8847 {
8848   tree mem_initializer_id;
8849   tree expression_list;
8850   tree member;
8851
8852   /* Find out what is being initialized.  */
8853   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8854     {
8855       pedwarn ("anachronistic old-style base class initializer");
8856       mem_initializer_id = NULL_TREE;
8857     }
8858   else
8859     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8860   member = expand_member_init (mem_initializer_id);
8861   if (member && !DECL_P (member))
8862     in_base_initializer = 1;
8863
8864   expression_list
8865     = cp_parser_parenthesized_expression_list (parser, false,
8866                                                /*cast_p=*/false,
8867                                                /*allow_expansion_p=*/true,
8868                                                /*non_constant_p=*/NULL);
8869   if (expression_list == error_mark_node)
8870     return error_mark_node;
8871   if (!expression_list)
8872     expression_list = void_type_node;
8873
8874   in_base_initializer = 0;
8875
8876   return member ? build_tree_list (member, expression_list) : error_mark_node;
8877 }
8878
8879 /* Parse a mem-initializer-id.
8880
8881    mem-initializer-id:
8882      :: [opt] nested-name-specifier [opt] class-name
8883      identifier
8884
8885    Returns a TYPE indicating the class to be initializer for the first
8886    production.  Returns an IDENTIFIER_NODE indicating the data member
8887    to be initialized for the second production.  */
8888
8889 static tree
8890 cp_parser_mem_initializer_id (cp_parser* parser)
8891 {
8892   bool global_scope_p;
8893   bool nested_name_specifier_p;
8894   bool template_p = false;
8895   tree id;
8896
8897   /* `typename' is not allowed in this context ([temp.res]).  */
8898   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8899     {
8900       error ("keyword %<typename%> not allowed in this context (a qualified "
8901              "member initializer is implicitly a type)");
8902       cp_lexer_consume_token (parser->lexer);
8903     }
8904   /* Look for the optional `::' operator.  */
8905   global_scope_p
8906     = (cp_parser_global_scope_opt (parser,
8907                                    /*current_scope_valid_p=*/false)
8908        != NULL_TREE);
8909   /* Look for the optional nested-name-specifier.  The simplest way to
8910      implement:
8911
8912        [temp.res]
8913
8914        The keyword `typename' is not permitted in a base-specifier or
8915        mem-initializer; in these contexts a qualified name that
8916        depends on a template-parameter is implicitly assumed to be a
8917        type name.
8918
8919      is to assume that we have seen the `typename' keyword at this
8920      point.  */
8921   nested_name_specifier_p
8922     = (cp_parser_nested_name_specifier_opt (parser,
8923                                             /*typename_keyword_p=*/true,
8924                                             /*check_dependency_p=*/true,
8925                                             /*type_p=*/true,
8926                                             /*is_declaration=*/true)
8927        != NULL_TREE);
8928   if (nested_name_specifier_p)
8929     template_p = cp_parser_optional_template_keyword (parser);
8930   /* If there is a `::' operator or a nested-name-specifier, then we
8931      are definitely looking for a class-name.  */
8932   if (global_scope_p || nested_name_specifier_p)
8933     return cp_parser_class_name (parser,
8934                                  /*typename_keyword_p=*/true,
8935                                  /*template_keyword_p=*/template_p,
8936                                  none_type,
8937                                  /*check_dependency_p=*/true,
8938                                  /*class_head_p=*/false,
8939                                  /*is_declaration=*/true);
8940   /* Otherwise, we could also be looking for an ordinary identifier.  */
8941   cp_parser_parse_tentatively (parser);
8942   /* Try a class-name.  */
8943   id = cp_parser_class_name (parser,
8944                              /*typename_keyword_p=*/true,
8945                              /*template_keyword_p=*/false,
8946                              none_type,
8947                              /*check_dependency_p=*/true,
8948                              /*class_head_p=*/false,
8949                              /*is_declaration=*/true);
8950   /* If we found one, we're done.  */
8951   if (cp_parser_parse_definitely (parser))
8952     return id;
8953   /* Otherwise, look for an ordinary identifier.  */
8954   return cp_parser_identifier (parser);
8955 }
8956
8957 /* Overloading [gram.over] */
8958
8959 /* Parse an operator-function-id.
8960
8961    operator-function-id:
8962      operator operator
8963
8964    Returns an IDENTIFIER_NODE for the operator which is a
8965    human-readable spelling of the identifier, e.g., `operator +'.  */
8966
8967 static tree
8968 cp_parser_operator_function_id (cp_parser* parser)
8969 {
8970   /* Look for the `operator' keyword.  */
8971   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8972     return error_mark_node;
8973   /* And then the name of the operator itself.  */
8974   return cp_parser_operator (parser);
8975 }
8976
8977 /* Parse an operator.
8978
8979    operator:
8980      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8981      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8982      || ++ -- , ->* -> () []
8983
8984    GNU Extensions:
8985
8986    operator:
8987      <? >? <?= >?=
8988
8989    Returns an IDENTIFIER_NODE for the operator which is a
8990    human-readable spelling of the identifier, e.g., `operator +'.  */
8991
8992 static tree
8993 cp_parser_operator (cp_parser* parser)
8994 {
8995   tree id = NULL_TREE;
8996   cp_token *token;
8997
8998   /* Peek at the next token.  */
8999   token = cp_lexer_peek_token (parser->lexer);
9000   /* Figure out which operator we have.  */
9001   switch (token->type)
9002     {
9003     case CPP_KEYWORD:
9004       {
9005         enum tree_code op;
9006
9007         /* The keyword should be either `new' or `delete'.  */
9008         if (token->keyword == RID_NEW)
9009           op = NEW_EXPR;
9010         else if (token->keyword == RID_DELETE)
9011           op = DELETE_EXPR;
9012         else
9013           break;
9014
9015         /* Consume the `new' or `delete' token.  */
9016         cp_lexer_consume_token (parser->lexer);
9017
9018         /* Peek at the next token.  */
9019         token = cp_lexer_peek_token (parser->lexer);
9020         /* If it's a `[' token then this is the array variant of the
9021            operator.  */
9022         if (token->type == CPP_OPEN_SQUARE)
9023           {
9024             /* Consume the `[' token.  */
9025             cp_lexer_consume_token (parser->lexer);
9026             /* Look for the `]' token.  */
9027             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9028             id = ansi_opname (op == NEW_EXPR
9029                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9030           }
9031         /* Otherwise, we have the non-array variant.  */
9032         else
9033           id = ansi_opname (op);
9034
9035         return id;
9036       }
9037
9038     case CPP_PLUS:
9039       id = ansi_opname (PLUS_EXPR);
9040       break;
9041
9042     case CPP_MINUS:
9043       id = ansi_opname (MINUS_EXPR);
9044       break;
9045
9046     case CPP_MULT:
9047       id = ansi_opname (MULT_EXPR);
9048       break;
9049
9050     case CPP_DIV:
9051       id = ansi_opname (TRUNC_DIV_EXPR);
9052       break;
9053
9054     case CPP_MOD:
9055       id = ansi_opname (TRUNC_MOD_EXPR);
9056       break;
9057
9058     case CPP_XOR:
9059       id = ansi_opname (BIT_XOR_EXPR);
9060       break;
9061
9062     case CPP_AND:
9063       id = ansi_opname (BIT_AND_EXPR);
9064       break;
9065
9066     case CPP_OR:
9067       id = ansi_opname (BIT_IOR_EXPR);
9068       break;
9069
9070     case CPP_COMPL:
9071       id = ansi_opname (BIT_NOT_EXPR);
9072       break;
9073
9074     case CPP_NOT:
9075       id = ansi_opname (TRUTH_NOT_EXPR);
9076       break;
9077
9078     case CPP_EQ:
9079       id = ansi_assopname (NOP_EXPR);
9080       break;
9081
9082     case CPP_LESS:
9083       id = ansi_opname (LT_EXPR);
9084       break;
9085
9086     case CPP_GREATER:
9087       id = ansi_opname (GT_EXPR);
9088       break;
9089
9090     case CPP_PLUS_EQ:
9091       id = ansi_assopname (PLUS_EXPR);
9092       break;
9093
9094     case CPP_MINUS_EQ:
9095       id = ansi_assopname (MINUS_EXPR);
9096       break;
9097
9098     case CPP_MULT_EQ:
9099       id = ansi_assopname (MULT_EXPR);
9100       break;
9101
9102     case CPP_DIV_EQ:
9103       id = ansi_assopname (TRUNC_DIV_EXPR);
9104       break;
9105
9106     case CPP_MOD_EQ:
9107       id = ansi_assopname (TRUNC_MOD_EXPR);
9108       break;
9109
9110     case CPP_XOR_EQ:
9111       id = ansi_assopname (BIT_XOR_EXPR);
9112       break;
9113
9114     case CPP_AND_EQ:
9115       id = ansi_assopname (BIT_AND_EXPR);
9116       break;
9117
9118     case CPP_OR_EQ:
9119       id = ansi_assopname (BIT_IOR_EXPR);
9120       break;
9121
9122     case CPP_LSHIFT:
9123       id = ansi_opname (LSHIFT_EXPR);
9124       break;
9125
9126     case CPP_RSHIFT:
9127       id = ansi_opname (RSHIFT_EXPR);
9128       break;
9129
9130     case CPP_LSHIFT_EQ:
9131       id = ansi_assopname (LSHIFT_EXPR);
9132       break;
9133
9134     case CPP_RSHIFT_EQ:
9135       id = ansi_assopname (RSHIFT_EXPR);
9136       break;
9137
9138     case CPP_EQ_EQ:
9139       id = ansi_opname (EQ_EXPR);
9140       break;
9141
9142     case CPP_NOT_EQ:
9143       id = ansi_opname (NE_EXPR);
9144       break;
9145
9146     case CPP_LESS_EQ:
9147       id = ansi_opname (LE_EXPR);
9148       break;
9149
9150     case CPP_GREATER_EQ:
9151       id = ansi_opname (GE_EXPR);
9152       break;
9153
9154     case CPP_AND_AND:
9155       id = ansi_opname (TRUTH_ANDIF_EXPR);
9156       break;
9157
9158     case CPP_OR_OR:
9159       id = ansi_opname (TRUTH_ORIF_EXPR);
9160       break;
9161
9162     case CPP_PLUS_PLUS:
9163       id = ansi_opname (POSTINCREMENT_EXPR);
9164       break;
9165
9166     case CPP_MINUS_MINUS:
9167       id = ansi_opname (PREDECREMENT_EXPR);
9168       break;
9169
9170     case CPP_COMMA:
9171       id = ansi_opname (COMPOUND_EXPR);
9172       break;
9173
9174     case CPP_DEREF_STAR:
9175       id = ansi_opname (MEMBER_REF);
9176       break;
9177
9178     case CPP_DEREF:
9179       id = ansi_opname (COMPONENT_REF);
9180       break;
9181
9182     case CPP_OPEN_PAREN:
9183       /* Consume the `('.  */
9184       cp_lexer_consume_token (parser->lexer);
9185       /* Look for the matching `)'.  */
9186       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9187       return ansi_opname (CALL_EXPR);
9188
9189     case CPP_OPEN_SQUARE:
9190       /* Consume the `['.  */
9191       cp_lexer_consume_token (parser->lexer);
9192       /* Look for the matching `]'.  */
9193       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9194       return ansi_opname (ARRAY_REF);
9195
9196     default:
9197       /* Anything else is an error.  */
9198       break;
9199     }
9200
9201   /* If we have selected an identifier, we need to consume the
9202      operator token.  */
9203   if (id)
9204     cp_lexer_consume_token (parser->lexer);
9205   /* Otherwise, no valid operator name was present.  */
9206   else
9207     {
9208       cp_parser_error (parser, "expected operator");
9209       id = error_mark_node;
9210     }
9211
9212   return id;
9213 }
9214
9215 /* Parse a template-declaration.
9216
9217    template-declaration:
9218      export [opt] template < template-parameter-list > declaration
9219
9220    If MEMBER_P is TRUE, this template-declaration occurs within a
9221    class-specifier.
9222
9223    The grammar rule given by the standard isn't correct.  What
9224    is really meant is:
9225
9226    template-declaration:
9227      export [opt] template-parameter-list-seq
9228        decl-specifier-seq [opt] init-declarator [opt] ;
9229      export [opt] template-parameter-list-seq
9230        function-definition
9231
9232    template-parameter-list-seq:
9233      template-parameter-list-seq [opt]
9234      template < template-parameter-list >  */
9235
9236 static void
9237 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9238 {
9239   /* Check for `export'.  */
9240   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9241     {
9242       /* Consume the `export' token.  */
9243       cp_lexer_consume_token (parser->lexer);
9244       /* Warn that we do not support `export'.  */
9245       warning (0, "keyword %<export%> not implemented, and will be ignored");
9246     }
9247
9248   cp_parser_template_declaration_after_export (parser, member_p);
9249 }
9250
9251 /* Parse a template-parameter-list.
9252
9253    template-parameter-list:
9254      template-parameter
9255      template-parameter-list , template-parameter
9256
9257    Returns a TREE_LIST.  Each node represents a template parameter.
9258    The nodes are connected via their TREE_CHAINs.  */
9259
9260 static tree
9261 cp_parser_template_parameter_list (cp_parser* parser)
9262 {
9263   tree parameter_list = NULL_TREE;
9264
9265   begin_template_parm_list ();
9266   while (true)
9267     {
9268       tree parameter;
9269       cp_token *token;
9270       bool is_non_type;
9271       bool is_parameter_pack;
9272
9273       /* Parse the template-parameter.  */
9274       parameter = cp_parser_template_parameter (parser, 
9275                                                 &is_non_type,
9276                                                 &is_parameter_pack);
9277       /* Add it to the list.  */
9278       if (parameter != error_mark_node)
9279         parameter_list = process_template_parm (parameter_list,
9280                                                 parameter,
9281                                                 is_non_type,
9282                                                 is_parameter_pack);
9283       else
9284        {
9285          tree err_parm = build_tree_list (parameter, parameter);
9286          TREE_VALUE (err_parm) = error_mark_node;
9287          parameter_list = chainon (parameter_list, err_parm);
9288        }
9289
9290       /* Peek at the next token.  */
9291       token = cp_lexer_peek_token (parser->lexer);
9292       /* If it's not a `,', we're done.  */
9293       if (token->type != CPP_COMMA)
9294         break;
9295       /* Otherwise, consume the `,' token.  */
9296       cp_lexer_consume_token (parser->lexer);
9297     }
9298
9299   return end_template_parm_list (parameter_list);
9300 }
9301
9302 /* Parse a template-parameter.
9303
9304    template-parameter:
9305      type-parameter
9306      parameter-declaration
9307
9308    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9309    the parameter.  The TREE_PURPOSE is the default value, if any.
9310    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9311    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9312    set to true iff this parameter is a parameter pack. */
9313
9314 static tree
9315 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9316                               bool *is_parameter_pack)
9317 {
9318   cp_token *token;
9319   cp_parameter_declarator *parameter_declarator;
9320   cp_declarator *id_declarator;
9321   tree parm;
9322
9323   /* Assume it is a type parameter or a template parameter.  */
9324   *is_non_type = false;
9325   /* Assume it not a parameter pack. */
9326   *is_parameter_pack = false;
9327   /* Peek at the next token.  */
9328   token = cp_lexer_peek_token (parser->lexer);
9329   /* If it is `class' or `template', we have a type-parameter.  */
9330   if (token->keyword == RID_TEMPLATE)
9331     return cp_parser_type_parameter (parser, is_parameter_pack);
9332   /* If it is `class' or `typename' we do not know yet whether it is a
9333      type parameter or a non-type parameter.  Consider:
9334
9335        template <typename T, typename T::X X> ...
9336
9337      or:
9338
9339        template <class C, class D*> ...
9340
9341      Here, the first parameter is a type parameter, and the second is
9342      a non-type parameter.  We can tell by looking at the token after
9343      the identifier -- if it is a `,', `=', or `>' then we have a type
9344      parameter.  */
9345   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9346     {
9347       /* Peek at the token after `class' or `typename'.  */
9348       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9349       /* If it's an ellipsis, we have a template type parameter
9350          pack. */
9351       if (token->type == CPP_ELLIPSIS)
9352         return cp_parser_type_parameter (parser, is_parameter_pack);
9353       /* If it's an identifier, skip it.  */
9354       if (token->type == CPP_NAME)
9355         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9356       /* Now, see if the token looks like the end of a template
9357          parameter.  */
9358       if (token->type == CPP_COMMA
9359           || token->type == CPP_EQ
9360           || token->type == CPP_GREATER)
9361         return cp_parser_type_parameter (parser, is_parameter_pack);
9362     }
9363
9364   /* Otherwise, it is a non-type parameter.
9365
9366      [temp.param]
9367
9368      When parsing a default template-argument for a non-type
9369      template-parameter, the first non-nested `>' is taken as the end
9370      of the template parameter-list rather than a greater-than
9371      operator.  */
9372   *is_non_type = true;
9373   parameter_declarator
9374      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9375                                         /*parenthesized_p=*/NULL);
9376
9377   /* If the parameter declaration is marked as a parameter pack, set
9378      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9379      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9380      grokdeclarator. */
9381   if (parameter_declarator
9382       && parameter_declarator->declarator
9383       && parameter_declarator->declarator->parameter_pack_p)
9384     {
9385       *is_parameter_pack = true;
9386       parameter_declarator->declarator->parameter_pack_p = false;
9387     }
9388
9389   /* If the next token is an ellipsis, and we don't already have it
9390      marked as a parameter pack, then we have a parameter pack (that
9391      has no declarator).  */
9392   if (!*is_parameter_pack
9393       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9394       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9395     {
9396       /* Consume the `...'.  */
9397       cp_lexer_consume_token (parser->lexer);
9398       maybe_warn_variadic_templates ();
9399       
9400       *is_parameter_pack = true;
9401
9402       /* Parameter packs cannot have default arguments.  However, a
9403          user may try to do so, so we'll parse them and give an
9404          appropriate diagnostic here.  */
9405       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9406         {
9407           /* Consume the `='.  */
9408           cp_lexer_consume_token (parser->lexer);
9409
9410           /* Find the name of the parameter pack.  */     
9411           id_declarator = parameter_declarator->declarator;
9412           while (id_declarator && id_declarator->kind != cdk_id)
9413             id_declarator = id_declarator->declarator;
9414           
9415           if (id_declarator && id_declarator->kind == cdk_id)
9416             error ("template parameter pack %qD cannot have a default argument",
9417                    id_declarator->u.id.unqualified_name);
9418           else
9419             error ("template parameter pack cannot have a default argument");
9420
9421           /* Parse the default argument, but throw away the result.  */
9422           cp_parser_default_argument (parser, /*template_parm_p=*/true);
9423         }
9424     }
9425
9426   parm = grokdeclarator (parameter_declarator->declarator,
9427                          &parameter_declarator->decl_specifiers,
9428                          PARM, /*initialized=*/0,
9429                          /*attrlist=*/NULL);
9430   if (parm == error_mark_node)
9431     return error_mark_node;
9432
9433   return build_tree_list (parameter_declarator->default_argument, parm);
9434 }
9435
9436 /* Parse a type-parameter.
9437
9438    type-parameter:
9439      class identifier [opt]
9440      class identifier [opt] = type-id
9441      typename identifier [opt]
9442      typename identifier [opt] = type-id
9443      template < template-parameter-list > class identifier [opt]
9444      template < template-parameter-list > class identifier [opt]
9445        = id-expression
9446
9447    GNU Extension (variadic templates):
9448
9449    type-parameter:
9450      class ... identifier [opt]
9451      typename ... identifier [opt]
9452
9453    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9454    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9455    the declaration of the parameter.
9456
9457    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9458
9459 static tree
9460 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9461 {
9462   cp_token *token;
9463   tree parameter;
9464
9465   /* Look for a keyword to tell us what kind of parameter this is.  */
9466   token = cp_parser_require (parser, CPP_KEYWORD,
9467                              "`class', `typename', or `template'");
9468   if (!token)
9469     return error_mark_node;
9470
9471   switch (token->keyword)
9472     {
9473     case RID_CLASS:
9474     case RID_TYPENAME:
9475       {
9476         tree identifier;
9477         tree default_argument;
9478
9479         /* If the next token is an ellipsis, we have a template
9480            argument pack. */
9481         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9482           {
9483             /* Consume the `...' token. */
9484             cp_lexer_consume_token (parser->lexer);
9485             maybe_warn_variadic_templates ();
9486
9487             *is_parameter_pack = true;
9488           }
9489
9490         /* If the next token is an identifier, then it names the
9491            parameter.  */
9492         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9493           identifier = cp_parser_identifier (parser);
9494         else
9495           identifier = NULL_TREE;
9496
9497         /* Create the parameter.  */
9498         parameter = finish_template_type_parm (class_type_node, identifier);
9499
9500         /* If the next token is an `=', we have a default argument.  */
9501         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9502           {
9503             /* Consume the `=' token.  */
9504             cp_lexer_consume_token (parser->lexer);
9505             /* Parse the default-argument.  */
9506             push_deferring_access_checks (dk_no_deferred);
9507             default_argument = cp_parser_type_id (parser);
9508
9509             /* Template parameter packs cannot have default
9510                arguments. */
9511             if (*is_parameter_pack)
9512               {
9513                 if (identifier)
9514                   error ("template parameter pack %qD cannot have a default argument", 
9515                          identifier);
9516                 else
9517                   error ("template parameter packs cannot have default arguments");
9518                 default_argument = NULL_TREE;
9519               }
9520             pop_deferring_access_checks ();
9521           }
9522         else
9523           default_argument = NULL_TREE;
9524
9525         /* Create the combined representation of the parameter and the
9526            default argument.  */
9527         parameter = build_tree_list (default_argument, parameter);
9528       }
9529       break;
9530
9531     case RID_TEMPLATE:
9532       {
9533         tree parameter_list;
9534         tree identifier;
9535         tree default_argument;
9536
9537         /* Look for the `<'.  */
9538         cp_parser_require (parser, CPP_LESS, "`<'");
9539         /* Parse the template-parameter-list.  */
9540         parameter_list = cp_parser_template_parameter_list (parser);
9541         /* Look for the `>'.  */
9542         cp_parser_require (parser, CPP_GREATER, "`>'");
9543         /* Look for the `class' keyword.  */
9544         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9545         /* If the next token is an ellipsis, we have a template
9546            argument pack. */
9547         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9548           {
9549             /* Consume the `...' token. */
9550             cp_lexer_consume_token (parser->lexer);
9551             maybe_warn_variadic_templates ();
9552
9553             *is_parameter_pack = true;
9554           }
9555         /* If the next token is an `=', then there is a
9556            default-argument.  If the next token is a `>', we are at
9557            the end of the parameter-list.  If the next token is a `,',
9558            then we are at the end of this parameter.  */
9559         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9560             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9561             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9562           {
9563             identifier = cp_parser_identifier (parser);
9564             /* Treat invalid names as if the parameter were nameless.  */
9565             if (identifier == error_mark_node)
9566               identifier = NULL_TREE;
9567           }
9568         else
9569           identifier = NULL_TREE;
9570
9571         /* Create the template parameter.  */
9572         parameter = finish_template_template_parm (class_type_node,
9573                                                    identifier);
9574
9575         /* If the next token is an `=', then there is a
9576            default-argument.  */
9577         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9578           {
9579             bool is_template;
9580
9581             /* Consume the `='.  */
9582             cp_lexer_consume_token (parser->lexer);
9583             /* Parse the id-expression.  */
9584             push_deferring_access_checks (dk_no_deferred);
9585             default_argument
9586               = cp_parser_id_expression (parser,
9587                                          /*template_keyword_p=*/false,
9588                                          /*check_dependency_p=*/true,
9589                                          /*template_p=*/&is_template,
9590                                          /*declarator_p=*/false,
9591                                          /*optional_p=*/false);
9592             if (TREE_CODE (default_argument) == TYPE_DECL)
9593               /* If the id-expression was a template-id that refers to
9594                  a template-class, we already have the declaration here,
9595                  so no further lookup is needed.  */
9596                  ;
9597             else
9598               /* Look up the name.  */
9599               default_argument
9600                 = cp_parser_lookup_name (parser, default_argument,
9601                                          none_type,
9602                                          /*is_template=*/is_template,
9603                                          /*is_namespace=*/false,
9604                                          /*check_dependency=*/true,
9605                                          /*ambiguous_decls=*/NULL);
9606             /* See if the default argument is valid.  */
9607             default_argument
9608               = check_template_template_default_arg (default_argument);
9609
9610             /* Template parameter packs cannot have default
9611                arguments. */
9612             if (*is_parameter_pack)
9613               {
9614                 if (identifier)
9615                   error ("template parameter pack %qD cannot have a default argument", 
9616                          identifier);
9617                 else
9618                   error ("template parameter packs cannot have default arguments");
9619                 default_argument = NULL_TREE;
9620               }
9621             pop_deferring_access_checks ();
9622           }
9623         else
9624           default_argument = NULL_TREE;
9625
9626         /* Create the combined representation of the parameter and the
9627            default argument.  */
9628         parameter = build_tree_list (default_argument, parameter);
9629       }
9630       break;
9631
9632     default:
9633       gcc_unreachable ();
9634       break;
9635     }
9636
9637   return parameter;
9638 }
9639
9640 /* Parse a template-id.
9641
9642    template-id:
9643      template-name < template-argument-list [opt] >
9644
9645    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9646    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9647    returned.  Otherwise, if the template-name names a function, or set
9648    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9649    names a class, returns a TYPE_DECL for the specialization.
9650
9651    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9652    uninstantiated templates.  */
9653
9654 static tree
9655 cp_parser_template_id (cp_parser *parser,
9656                        bool template_keyword_p,
9657                        bool check_dependency_p,
9658                        bool is_declaration)
9659 {
9660   int i;
9661   tree template;
9662   tree arguments;
9663   tree template_id;
9664   cp_token_position start_of_id = 0;
9665   deferred_access_check *chk;
9666   VEC (deferred_access_check,gc) *access_check;
9667   cp_token *next_token, *next_token_2;
9668   bool is_identifier;
9669
9670   /* If the next token corresponds to a template-id, there is no need
9671      to reparse it.  */
9672   next_token = cp_lexer_peek_token (parser->lexer);
9673   if (next_token->type == CPP_TEMPLATE_ID)
9674     {
9675       struct tree_check *check_value;
9676
9677       /* Get the stored value.  */
9678       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9679       /* Perform any access checks that were deferred.  */
9680       access_check = check_value->checks;
9681       if (access_check)
9682         {
9683           for (i = 0 ;
9684                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9685                ++i)
9686             {
9687               perform_or_defer_access_check (chk->binfo,
9688                                              chk->decl,
9689                                              chk->diag_decl);
9690             }
9691         }
9692       /* Return the stored value.  */
9693       return check_value->value;
9694     }
9695
9696   /* Avoid performing name lookup if there is no possibility of
9697      finding a template-id.  */
9698   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9699       || (next_token->type == CPP_NAME
9700           && !cp_parser_nth_token_starts_template_argument_list_p
9701                (parser, 2)))
9702     {
9703       cp_parser_error (parser, "expected template-id");
9704       return error_mark_node;
9705     }
9706
9707   /* Remember where the template-id starts.  */
9708   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9709     start_of_id = cp_lexer_token_position (parser->lexer, false);
9710
9711   push_deferring_access_checks (dk_deferred);
9712
9713   /* Parse the template-name.  */
9714   is_identifier = false;
9715   template = cp_parser_template_name (parser, template_keyword_p,
9716                                       check_dependency_p,
9717                                       is_declaration,
9718                                       &is_identifier);
9719   if (template == error_mark_node || is_identifier)
9720     {
9721       pop_deferring_access_checks ();
9722       return template;
9723     }
9724
9725   /* If we find the sequence `[:' after a template-name, it's probably
9726      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9727      parse correctly the argument list.  */
9728   next_token = cp_lexer_peek_token (parser->lexer);
9729   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9730   if (next_token->type == CPP_OPEN_SQUARE
9731       && next_token->flags & DIGRAPH
9732       && next_token_2->type == CPP_COLON
9733       && !(next_token_2->flags & PREV_WHITE))
9734     {
9735       cp_parser_parse_tentatively (parser);
9736       /* Change `:' into `::'.  */
9737       next_token_2->type = CPP_SCOPE;
9738       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9739          CPP_LESS.  */
9740       cp_lexer_consume_token (parser->lexer);
9741       /* Parse the arguments.  */
9742       arguments = cp_parser_enclosed_template_argument_list (parser);
9743       if (!cp_parser_parse_definitely (parser))
9744         {
9745           /* If we couldn't parse an argument list, then we revert our changes
9746              and return simply an error. Maybe this is not a template-id
9747              after all.  */
9748           next_token_2->type = CPP_COLON;
9749           cp_parser_error (parser, "expected %<<%>");
9750           pop_deferring_access_checks ();
9751           return error_mark_node;
9752         }
9753       /* Otherwise, emit an error about the invalid digraph, but continue
9754          parsing because we got our argument list.  */
9755       pedwarn ("%<<::%> cannot begin a template-argument list");
9756       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9757               "between %<<%> and %<::%>");
9758       if (!flag_permissive)
9759         {
9760           static bool hint;
9761           if (!hint)
9762             {
9763               inform ("(if you use -fpermissive G++ will accept your code)");
9764               hint = true;
9765             }
9766         }
9767     }
9768   else
9769     {
9770       /* Look for the `<' that starts the template-argument-list.  */
9771       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9772         {
9773           pop_deferring_access_checks ();
9774           return error_mark_node;
9775         }
9776       /* Parse the arguments.  */
9777       arguments = cp_parser_enclosed_template_argument_list (parser);
9778     }
9779
9780   /* Build a representation of the specialization.  */
9781   if (TREE_CODE (template) == IDENTIFIER_NODE)
9782     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9783   else if (DECL_CLASS_TEMPLATE_P (template)
9784            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9785     {
9786       bool entering_scope;
9787       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9788          template (rather than some instantiation thereof) only if
9789          is not nested within some other construct.  For example, in
9790          "template <typename T> void f(T) { A<T>::", A<T> is just an
9791          instantiation of A.  */
9792       entering_scope = (template_parm_scope_p ()
9793                         && cp_lexer_next_token_is (parser->lexer,
9794                                                    CPP_SCOPE));
9795       template_id
9796         = finish_template_type (template, arguments, entering_scope);
9797     }
9798   else
9799     {
9800       /* If it's not a class-template or a template-template, it should be
9801          a function-template.  */
9802       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9803                    || TREE_CODE (template) == OVERLOAD
9804                    || BASELINK_P (template)));
9805
9806       template_id = lookup_template_function (template, arguments);
9807     }
9808
9809   /* If parsing tentatively, replace the sequence of tokens that makes
9810      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9811      should we re-parse the token stream, we will not have to repeat
9812      the effort required to do the parse, nor will we issue duplicate
9813      error messages about problems during instantiation of the
9814      template.  */
9815   if (start_of_id)
9816     {
9817       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9818
9819       /* Reset the contents of the START_OF_ID token.  */
9820       token->type = CPP_TEMPLATE_ID;
9821       /* Retrieve any deferred checks.  Do not pop this access checks yet
9822          so the memory will not be reclaimed during token replacing below.  */
9823       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9824       token->u.tree_check_value->value = template_id;
9825       token->u.tree_check_value->checks = get_deferred_access_checks ();
9826       token->keyword = RID_MAX;
9827
9828       /* Purge all subsequent tokens.  */
9829       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9830
9831       /* ??? Can we actually assume that, if template_id ==
9832          error_mark_node, we will have issued a diagnostic to the
9833          user, as opposed to simply marking the tentative parse as
9834          failed?  */
9835       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9836         error ("parse error in template argument list");
9837     }
9838
9839   pop_deferring_access_checks ();
9840   return template_id;
9841 }
9842
9843 /* Parse a template-name.
9844
9845    template-name:
9846      identifier
9847
9848    The standard should actually say:
9849
9850    template-name:
9851      identifier
9852      operator-function-id
9853
9854    A defect report has been filed about this issue.
9855
9856    A conversion-function-id cannot be a template name because they cannot
9857    be part of a template-id. In fact, looking at this code:
9858
9859    a.operator K<int>()
9860
9861    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9862    It is impossible to call a templated conversion-function-id with an
9863    explicit argument list, since the only allowed template parameter is
9864    the type to which it is converting.
9865
9866    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9867    `template' keyword, in a construction like:
9868
9869      T::template f<3>()
9870
9871    In that case `f' is taken to be a template-name, even though there
9872    is no way of knowing for sure.
9873
9874    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9875    name refers to a set of overloaded functions, at least one of which
9876    is a template, or an IDENTIFIER_NODE with the name of the template,
9877    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9878    names are looked up inside uninstantiated templates.  */
9879
9880 static tree
9881 cp_parser_template_name (cp_parser* parser,
9882                          bool template_keyword_p,
9883                          bool check_dependency_p,
9884                          bool is_declaration,
9885                          bool *is_identifier)
9886 {
9887   tree identifier;
9888   tree decl;
9889   tree fns;
9890
9891   /* If the next token is `operator', then we have either an
9892      operator-function-id or a conversion-function-id.  */
9893   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9894     {
9895       /* We don't know whether we're looking at an
9896          operator-function-id or a conversion-function-id.  */
9897       cp_parser_parse_tentatively (parser);
9898       /* Try an operator-function-id.  */
9899       identifier = cp_parser_operator_function_id (parser);
9900       /* If that didn't work, try a conversion-function-id.  */
9901       if (!cp_parser_parse_definitely (parser))
9902         {
9903           cp_parser_error (parser, "expected template-name");
9904           return error_mark_node;
9905         }
9906     }
9907   /* Look for the identifier.  */
9908   else
9909     identifier = cp_parser_identifier (parser);
9910
9911   /* If we didn't find an identifier, we don't have a template-id.  */
9912   if (identifier == error_mark_node)
9913     return error_mark_node;
9914
9915   /* If the name immediately followed the `template' keyword, then it
9916      is a template-name.  However, if the next token is not `<', then
9917      we do not treat it as a template-name, since it is not being used
9918      as part of a template-id.  This enables us to handle constructs
9919      like:
9920
9921        template <typename T> struct S { S(); };
9922        template <typename T> S<T>::S();
9923
9924      correctly.  We would treat `S' as a template -- if it were `S<T>'
9925      -- but we do not if there is no `<'.  */
9926
9927   if (processing_template_decl
9928       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9929     {
9930       /* In a declaration, in a dependent context, we pretend that the
9931          "template" keyword was present in order to improve error
9932          recovery.  For example, given:
9933
9934            template <typename T> void f(T::X<int>);
9935
9936          we want to treat "X<int>" as a template-id.  */
9937       if (is_declaration
9938           && !template_keyword_p
9939           && parser->scope && TYPE_P (parser->scope)
9940           && check_dependency_p
9941           && dependent_type_p (parser->scope)
9942           /* Do not do this for dtors (or ctors), since they never
9943              need the template keyword before their name.  */
9944           && !constructor_name_p (identifier, parser->scope))
9945         {
9946           cp_token_position start = 0;
9947
9948           /* Explain what went wrong.  */
9949           error ("non-template %qD used as template", identifier);
9950           inform ("use %<%T::template %D%> to indicate that it is a template",
9951                   parser->scope, identifier);
9952           /* If parsing tentatively, find the location of the "<" token.  */
9953           if (cp_parser_simulate_error (parser))
9954             start = cp_lexer_token_position (parser->lexer, true);
9955           /* Parse the template arguments so that we can issue error
9956              messages about them.  */
9957           cp_lexer_consume_token (parser->lexer);
9958           cp_parser_enclosed_template_argument_list (parser);
9959           /* Skip tokens until we find a good place from which to
9960              continue parsing.  */
9961           cp_parser_skip_to_closing_parenthesis (parser,
9962                                                  /*recovering=*/true,
9963                                                  /*or_comma=*/true,
9964                                                  /*consume_paren=*/false);
9965           /* If parsing tentatively, permanently remove the
9966              template argument list.  That will prevent duplicate
9967              error messages from being issued about the missing
9968              "template" keyword.  */
9969           if (start)
9970             cp_lexer_purge_tokens_after (parser->lexer, start);
9971           if (is_identifier)
9972             *is_identifier = true;
9973           return identifier;
9974         }
9975
9976       /* If the "template" keyword is present, then there is generally
9977          no point in doing name-lookup, so we just return IDENTIFIER.
9978          But, if the qualifying scope is non-dependent then we can
9979          (and must) do name-lookup normally.  */
9980       if (template_keyword_p
9981           && (!parser->scope
9982               || (TYPE_P (parser->scope)
9983                   && dependent_type_p (parser->scope))))
9984         return identifier;
9985     }
9986
9987   /* Look up the name.  */
9988   decl = cp_parser_lookup_name (parser, identifier,
9989                                 none_type,
9990                                 /*is_template=*/false,
9991                                 /*is_namespace=*/false,
9992                                 check_dependency_p,
9993                                 /*ambiguous_decls=*/NULL);
9994   decl = maybe_get_template_decl_from_type_decl (decl);
9995
9996   /* If DECL is a template, then the name was a template-name.  */
9997   if (TREE_CODE (decl) == TEMPLATE_DECL)
9998     ;
9999   else
10000     {
10001       tree fn = NULL_TREE;
10002
10003       /* The standard does not explicitly indicate whether a name that
10004          names a set of overloaded declarations, some of which are
10005          templates, is a template-name.  However, such a name should
10006          be a template-name; otherwise, there is no way to form a
10007          template-id for the overloaded templates.  */
10008       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10009       if (TREE_CODE (fns) == OVERLOAD)
10010         for (fn = fns; fn; fn = OVL_NEXT (fn))
10011           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10012             break;
10013
10014       if (!fn)
10015         {
10016           /* The name does not name a template.  */
10017           cp_parser_error (parser, "expected template-name");
10018           return error_mark_node;
10019         }
10020     }
10021
10022   /* If DECL is dependent, and refers to a function, then just return
10023      its name; we will look it up again during template instantiation.  */
10024   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10025     {
10026       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10027       if (TYPE_P (scope) && dependent_type_p (scope))
10028         return identifier;
10029     }
10030
10031   return decl;
10032 }
10033
10034 /* Parse a template-argument-list.
10035
10036    template-argument-list:
10037      template-argument ... [opt]
10038      template-argument-list , template-argument ... [opt]
10039
10040    Returns a TREE_VEC containing the arguments.  */
10041
10042 static tree
10043 cp_parser_template_argument_list (cp_parser* parser)
10044 {
10045   tree fixed_args[10];
10046   unsigned n_args = 0;
10047   unsigned alloced = 10;
10048   tree *arg_ary = fixed_args;
10049   tree vec;
10050   bool saved_in_template_argument_list_p;
10051   bool saved_ice_p;
10052   bool saved_non_ice_p;
10053
10054   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10055   parser->in_template_argument_list_p = true;
10056   /* Even if the template-id appears in an integral
10057      constant-expression, the contents of the argument list do
10058      not.  */
10059   saved_ice_p = parser->integral_constant_expression_p;
10060   parser->integral_constant_expression_p = false;
10061   saved_non_ice_p = parser->non_integral_constant_expression_p;
10062   parser->non_integral_constant_expression_p = false;
10063   /* Parse the arguments.  */
10064   do
10065     {
10066       tree argument;
10067
10068       if (n_args)
10069         /* Consume the comma.  */
10070         cp_lexer_consume_token (parser->lexer);
10071
10072       /* Parse the template-argument.  */
10073       argument = cp_parser_template_argument (parser);
10074
10075       /* If the next token is an ellipsis, we're expanding a template
10076          argument pack. */
10077       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10078         {
10079           /* Consume the `...' token. */
10080           cp_lexer_consume_token (parser->lexer);
10081
10082           /* Make the argument into a TYPE_PACK_EXPANSION or
10083              EXPR_PACK_EXPANSION. */
10084           argument = make_pack_expansion (argument);
10085         }
10086
10087       if (n_args == alloced)
10088         {
10089           alloced *= 2;
10090
10091           if (arg_ary == fixed_args)
10092             {
10093               arg_ary = XNEWVEC (tree, alloced);
10094               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10095             }
10096           else
10097             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10098         }
10099       arg_ary[n_args++] = argument;
10100     }
10101   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10102
10103   vec = make_tree_vec (n_args);
10104
10105   while (n_args--)
10106     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10107
10108   if (arg_ary != fixed_args)
10109     free (arg_ary);
10110   parser->non_integral_constant_expression_p = saved_non_ice_p;
10111   parser->integral_constant_expression_p = saved_ice_p;
10112   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10113   return vec;
10114 }
10115
10116 /* Parse a template-argument.
10117
10118    template-argument:
10119      assignment-expression
10120      type-id
10121      id-expression
10122
10123    The representation is that of an assignment-expression, type-id, or
10124    id-expression -- except that the qualified id-expression is
10125    evaluated, so that the value returned is either a DECL or an
10126    OVERLOAD.
10127
10128    Although the standard says "assignment-expression", it forbids
10129    throw-expressions or assignments in the template argument.
10130    Therefore, we use "conditional-expression" instead.  */
10131
10132 static tree
10133 cp_parser_template_argument (cp_parser* parser)
10134 {
10135   tree argument;
10136   bool template_p;
10137   bool address_p;
10138   bool maybe_type_id = false;
10139   cp_token *token;
10140   cp_id_kind idk;
10141
10142   /* There's really no way to know what we're looking at, so we just
10143      try each alternative in order.
10144
10145        [temp.arg]
10146
10147        In a template-argument, an ambiguity between a type-id and an
10148        expression is resolved to a type-id, regardless of the form of
10149        the corresponding template-parameter.
10150
10151      Therefore, we try a type-id first.  */
10152   cp_parser_parse_tentatively (parser);
10153   argument = cp_parser_type_id (parser);
10154   /* If there was no error parsing the type-id but the next token is a '>>',
10155      we probably found a typo for '> >'. But there are type-id which are
10156      also valid expressions. For instance:
10157
10158      struct X { int operator >> (int); };
10159      template <int V> struct Foo {};
10160      Foo<X () >> 5> r;
10161
10162      Here 'X()' is a valid type-id of a function type, but the user just
10163      wanted to write the expression "X() >> 5". Thus, we remember that we
10164      found a valid type-id, but we still try to parse the argument as an
10165      expression to see what happens.  */
10166   if (!cp_parser_error_occurred (parser)
10167       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10168     {
10169       maybe_type_id = true;
10170       cp_parser_abort_tentative_parse (parser);
10171     }
10172   else
10173     {
10174       /* If the next token isn't a `,' or a `>', then this argument wasn't
10175       really finished. This means that the argument is not a valid
10176       type-id.  */
10177       if (!cp_parser_next_token_ends_template_argument_p (parser))
10178         cp_parser_error (parser, "expected template-argument");
10179       /* If that worked, we're done.  */
10180       if (cp_parser_parse_definitely (parser))
10181         return argument;
10182     }
10183   /* We're still not sure what the argument will be.  */
10184   cp_parser_parse_tentatively (parser);
10185   /* Try a template.  */
10186   argument = cp_parser_id_expression (parser,
10187                                       /*template_keyword_p=*/false,
10188                                       /*check_dependency_p=*/true,
10189                                       &template_p,
10190                                       /*declarator_p=*/false,
10191                                       /*optional_p=*/false);
10192   /* If the next token isn't a `,' or a `>', then this argument wasn't
10193      really finished.  */
10194   if (!cp_parser_next_token_ends_template_argument_p (parser))
10195     cp_parser_error (parser, "expected template-argument");
10196   if (!cp_parser_error_occurred (parser))
10197     {
10198       /* Figure out what is being referred to.  If the id-expression
10199          was for a class template specialization, then we will have a
10200          TYPE_DECL at this point.  There is no need to do name lookup
10201          at this point in that case.  */
10202       if (TREE_CODE (argument) != TYPE_DECL)
10203         argument = cp_parser_lookup_name (parser, argument,
10204                                           none_type,
10205                                           /*is_template=*/template_p,
10206                                           /*is_namespace=*/false,
10207                                           /*check_dependency=*/true,
10208                                           /*ambiguous_decls=*/NULL);
10209       if (TREE_CODE (argument) != TEMPLATE_DECL
10210           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10211         cp_parser_error (parser, "expected template-name");
10212     }
10213   if (cp_parser_parse_definitely (parser))
10214     return argument;
10215   /* It must be a non-type argument.  There permitted cases are given
10216      in [temp.arg.nontype]:
10217
10218      -- an integral constant-expression of integral or enumeration
10219         type; or
10220
10221      -- the name of a non-type template-parameter; or
10222
10223      -- the name of an object or function with external linkage...
10224
10225      -- the address of an object or function with external linkage...
10226
10227      -- a pointer to member...  */
10228   /* Look for a non-type template parameter.  */
10229   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10230     {
10231       cp_parser_parse_tentatively (parser);
10232       argument = cp_parser_primary_expression (parser,
10233                                                /*adress_p=*/false,
10234                                                /*cast_p=*/false,
10235                                                /*template_arg_p=*/true,
10236                                                &idk);
10237       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10238           || !cp_parser_next_token_ends_template_argument_p (parser))
10239         cp_parser_simulate_error (parser);
10240       if (cp_parser_parse_definitely (parser))
10241         return argument;
10242     }
10243
10244   /* If the next token is "&", the argument must be the address of an
10245      object or function with external linkage.  */
10246   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10247   if (address_p)
10248     cp_lexer_consume_token (parser->lexer);
10249   /* See if we might have an id-expression.  */
10250   token = cp_lexer_peek_token (parser->lexer);
10251   if (token->type == CPP_NAME
10252       || token->keyword == RID_OPERATOR
10253       || token->type == CPP_SCOPE
10254       || token->type == CPP_TEMPLATE_ID
10255       || token->type == CPP_NESTED_NAME_SPECIFIER)
10256     {
10257       cp_parser_parse_tentatively (parser);
10258       argument = cp_parser_primary_expression (parser,
10259                                                address_p,
10260                                                /*cast_p=*/false,
10261                                                /*template_arg_p=*/true,
10262                                                &idk);
10263       if (cp_parser_error_occurred (parser)
10264           || !cp_parser_next_token_ends_template_argument_p (parser))
10265         cp_parser_abort_tentative_parse (parser);
10266       else
10267         {
10268           if (TREE_CODE (argument) == INDIRECT_REF)
10269             {
10270               gcc_assert (REFERENCE_REF_P (argument));
10271               argument = TREE_OPERAND (argument, 0);
10272             }
10273
10274           if (TREE_CODE (argument) == VAR_DECL)
10275             {
10276               /* A variable without external linkage might still be a
10277                  valid constant-expression, so no error is issued here
10278                  if the external-linkage check fails.  */
10279               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10280                 cp_parser_simulate_error (parser);
10281             }
10282           else if (is_overloaded_fn (argument))
10283             /* All overloaded functions are allowed; if the external
10284                linkage test does not pass, an error will be issued
10285                later.  */
10286             ;
10287           else if (address_p
10288                    && (TREE_CODE (argument) == OFFSET_REF
10289                        || TREE_CODE (argument) == SCOPE_REF))
10290             /* A pointer-to-member.  */
10291             ;
10292           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10293             ;
10294           else
10295             cp_parser_simulate_error (parser);
10296
10297           if (cp_parser_parse_definitely (parser))
10298             {
10299               if (address_p)
10300                 argument = build_x_unary_op (ADDR_EXPR, argument);
10301               return argument;
10302             }
10303         }
10304     }
10305   /* If the argument started with "&", there are no other valid
10306      alternatives at this point.  */
10307   if (address_p)
10308     {
10309       cp_parser_error (parser, "invalid non-type template argument");
10310       return error_mark_node;
10311     }
10312
10313   /* If the argument wasn't successfully parsed as a type-id followed
10314      by '>>', the argument can only be a constant expression now.
10315      Otherwise, we try parsing the constant-expression tentatively,
10316      because the argument could really be a type-id.  */
10317   if (maybe_type_id)
10318     cp_parser_parse_tentatively (parser);
10319   argument = cp_parser_constant_expression (parser,
10320                                             /*allow_non_constant_p=*/false,
10321                                             /*non_constant_p=*/NULL);
10322   argument = fold_non_dependent_expr (argument);
10323   if (!maybe_type_id)
10324     return argument;
10325   if (!cp_parser_next_token_ends_template_argument_p (parser))
10326     cp_parser_error (parser, "expected template-argument");
10327   if (cp_parser_parse_definitely (parser))
10328     return argument;
10329   /* We did our best to parse the argument as a non type-id, but that
10330      was the only alternative that matched (albeit with a '>' after
10331      it). We can assume it's just a typo from the user, and a
10332      diagnostic will then be issued.  */
10333   return cp_parser_type_id (parser);
10334 }
10335
10336 /* Parse an explicit-instantiation.
10337
10338    explicit-instantiation:
10339      template declaration
10340
10341    Although the standard says `declaration', what it really means is:
10342
10343    explicit-instantiation:
10344      template decl-specifier-seq [opt] declarator [opt] ;
10345
10346    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10347    supposed to be allowed.  A defect report has been filed about this
10348    issue.
10349
10350    GNU Extension:
10351
10352    explicit-instantiation:
10353      storage-class-specifier template
10354        decl-specifier-seq [opt] declarator [opt] ;
10355      function-specifier template
10356        decl-specifier-seq [opt] declarator [opt] ;  */
10357
10358 static void
10359 cp_parser_explicit_instantiation (cp_parser* parser)
10360 {
10361   int declares_class_or_enum;
10362   cp_decl_specifier_seq decl_specifiers;
10363   tree extension_specifier = NULL_TREE;
10364
10365   /* Look for an (optional) storage-class-specifier or
10366      function-specifier.  */
10367   if (cp_parser_allow_gnu_extensions_p (parser))
10368     {
10369       extension_specifier
10370         = cp_parser_storage_class_specifier_opt (parser);
10371       if (!extension_specifier)
10372         extension_specifier
10373           = cp_parser_function_specifier_opt (parser,
10374                                               /*decl_specs=*/NULL);
10375     }
10376
10377   /* Look for the `template' keyword.  */
10378   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10379   /* Let the front end know that we are processing an explicit
10380      instantiation.  */
10381   begin_explicit_instantiation ();
10382   /* [temp.explicit] says that we are supposed to ignore access
10383      control while processing explicit instantiation directives.  */
10384   push_deferring_access_checks (dk_no_check);
10385   /* Parse a decl-specifier-seq.  */
10386   cp_parser_decl_specifier_seq (parser,
10387                                 CP_PARSER_FLAGS_OPTIONAL,
10388                                 &decl_specifiers,
10389                                 &declares_class_or_enum);
10390   /* If there was exactly one decl-specifier, and it declared a class,
10391      and there's no declarator, then we have an explicit type
10392      instantiation.  */
10393   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10394     {
10395       tree type;
10396
10397       type = check_tag_decl (&decl_specifiers);
10398       /* Turn access control back on for names used during
10399          template instantiation.  */
10400       pop_deferring_access_checks ();
10401       if (type)
10402         do_type_instantiation (type, extension_specifier,
10403                                /*complain=*/tf_error);
10404     }
10405   else
10406     {
10407       cp_declarator *declarator;
10408       tree decl;
10409
10410       /* Parse the declarator.  */
10411       declarator
10412         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10413                                 /*ctor_dtor_or_conv_p=*/NULL,
10414                                 /*parenthesized_p=*/NULL,
10415                                 /*member_p=*/false);
10416       if (declares_class_or_enum & 2)
10417         cp_parser_check_for_definition_in_return_type (declarator,
10418                                                        decl_specifiers.type);
10419       if (declarator != cp_error_declarator)
10420         {
10421           decl = grokdeclarator (declarator, &decl_specifiers,
10422                                  NORMAL, 0, &decl_specifiers.attributes);
10423           /* Turn access control back on for names used during
10424              template instantiation.  */
10425           pop_deferring_access_checks ();
10426           /* Do the explicit instantiation.  */
10427           do_decl_instantiation (decl, extension_specifier);
10428         }
10429       else
10430         {
10431           pop_deferring_access_checks ();
10432           /* Skip the body of the explicit instantiation.  */
10433           cp_parser_skip_to_end_of_statement (parser);
10434         }
10435     }
10436   /* We're done with the instantiation.  */
10437   end_explicit_instantiation ();
10438
10439   cp_parser_consume_semicolon_at_end_of_statement (parser);
10440 }
10441
10442 /* Parse an explicit-specialization.
10443
10444    explicit-specialization:
10445      template < > declaration
10446
10447    Although the standard says `declaration', what it really means is:
10448
10449    explicit-specialization:
10450      template <> decl-specifier [opt] init-declarator [opt] ;
10451      template <> function-definition
10452      template <> explicit-specialization
10453      template <> template-declaration  */
10454
10455 static void
10456 cp_parser_explicit_specialization (cp_parser* parser)
10457 {
10458   bool need_lang_pop;
10459   /* Look for the `template' keyword.  */
10460   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10461   /* Look for the `<'.  */
10462   cp_parser_require (parser, CPP_LESS, "`<'");
10463   /* Look for the `>'.  */
10464   cp_parser_require (parser, CPP_GREATER, "`>'");
10465   /* We have processed another parameter list.  */
10466   ++parser->num_template_parameter_lists;
10467   /* [temp]
10468
10469      A template ... explicit specialization ... shall not have C
10470      linkage.  */
10471   if (current_lang_name == lang_name_c)
10472     {
10473       error ("template specialization with C linkage");
10474       /* Give it C++ linkage to avoid confusing other parts of the
10475          front end.  */
10476       push_lang_context (lang_name_cplusplus);
10477       need_lang_pop = true;
10478     }
10479   else
10480     need_lang_pop = false;
10481   /* Let the front end know that we are beginning a specialization.  */
10482   if (!begin_specialization ())
10483     {
10484       end_specialization ();
10485       cp_parser_skip_to_end_of_block_or_statement (parser);
10486       return;
10487     }
10488
10489   /* If the next keyword is `template', we need to figure out whether
10490      or not we're looking a template-declaration.  */
10491   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10492     {
10493       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10494           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10495         cp_parser_template_declaration_after_export (parser,
10496                                                      /*member_p=*/false);
10497       else
10498         cp_parser_explicit_specialization (parser);
10499     }
10500   else
10501     /* Parse the dependent declaration.  */
10502     cp_parser_single_declaration (parser,
10503                                   /*checks=*/NULL,
10504                                   /*member_p=*/false,
10505                                   /*explicit_specialization_p=*/true,
10506                                   /*friend_p=*/NULL);
10507   /* We're done with the specialization.  */
10508   end_specialization ();
10509   /* For the erroneous case of a template with C linkage, we pushed an
10510      implicit C++ linkage scope; exit that scope now.  */
10511   if (need_lang_pop)
10512     pop_lang_context ();
10513   /* We're done with this parameter list.  */
10514   --parser->num_template_parameter_lists;
10515 }
10516
10517 /* Parse a type-specifier.
10518
10519    type-specifier:
10520      simple-type-specifier
10521      class-specifier
10522      enum-specifier
10523      elaborated-type-specifier
10524      cv-qualifier
10525
10526    GNU Extension:
10527
10528    type-specifier:
10529      __complex__
10530
10531    Returns a representation of the type-specifier.  For a
10532    class-specifier, enum-specifier, or elaborated-type-specifier, a
10533    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10534
10535    The parser flags FLAGS is used to control type-specifier parsing.
10536
10537    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10538    in a decl-specifier-seq.
10539
10540    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10541    class-specifier, enum-specifier, or elaborated-type-specifier, then
10542    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10543    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10544    zero.
10545
10546    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10547    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10548    is set to FALSE.  */
10549
10550 static tree
10551 cp_parser_type_specifier (cp_parser* parser,
10552                           cp_parser_flags flags,
10553                           cp_decl_specifier_seq *decl_specs,
10554                           bool is_declaration,
10555                           int* declares_class_or_enum,
10556                           bool* is_cv_qualifier)
10557 {
10558   tree type_spec = NULL_TREE;
10559   cp_token *token;
10560   enum rid keyword;
10561   cp_decl_spec ds = ds_last;
10562
10563   /* Assume this type-specifier does not declare a new type.  */
10564   if (declares_class_or_enum)
10565     *declares_class_or_enum = 0;
10566   /* And that it does not specify a cv-qualifier.  */
10567   if (is_cv_qualifier)
10568     *is_cv_qualifier = false;
10569   /* Peek at the next token.  */
10570   token = cp_lexer_peek_token (parser->lexer);
10571
10572   /* If we're looking at a keyword, we can use that to guide the
10573      production we choose.  */
10574   keyword = token->keyword;
10575   switch (keyword)
10576     {
10577     case RID_ENUM:
10578       /* Look for the enum-specifier.  */
10579       type_spec = cp_parser_enum_specifier (parser);
10580       /* If that worked, we're done.  */
10581       if (type_spec)
10582         {
10583           if (declares_class_or_enum)
10584             *declares_class_or_enum = 2;
10585           if (decl_specs)
10586             cp_parser_set_decl_spec_type (decl_specs,
10587                                           type_spec,
10588                                           /*user_defined_p=*/true);
10589           return type_spec;
10590         }
10591       else
10592         goto elaborated_type_specifier;
10593
10594       /* Any of these indicate either a class-specifier, or an
10595          elaborated-type-specifier.  */
10596     case RID_CLASS:
10597     case RID_STRUCT:
10598     case RID_UNION:
10599       /* Parse tentatively so that we can back up if we don't find a
10600          class-specifier.  */
10601       cp_parser_parse_tentatively (parser);
10602       /* Look for the class-specifier.  */
10603       type_spec = cp_parser_class_specifier (parser);
10604       /* If that worked, we're done.  */
10605       if (cp_parser_parse_definitely (parser))
10606         {
10607           if (declares_class_or_enum)
10608             *declares_class_or_enum = 2;
10609           if (decl_specs)
10610             cp_parser_set_decl_spec_type (decl_specs,
10611                                           type_spec,
10612                                           /*user_defined_p=*/true);
10613           return type_spec;
10614         }
10615
10616       /* Fall through.  */
10617     elaborated_type_specifier:
10618       /* We're declaring (not defining) a class or enum.  */
10619       if (declares_class_or_enum)
10620         *declares_class_or_enum = 1;
10621
10622       /* Fall through.  */
10623     case RID_TYPENAME:
10624       /* Look for an elaborated-type-specifier.  */
10625       type_spec
10626         = (cp_parser_elaborated_type_specifier
10627            (parser,
10628             decl_specs && decl_specs->specs[(int) ds_friend],
10629             is_declaration));
10630       if (decl_specs)
10631         cp_parser_set_decl_spec_type (decl_specs,
10632                                       type_spec,
10633                                       /*user_defined_p=*/true);
10634       return type_spec;
10635
10636     case RID_CONST:
10637       ds = ds_const;
10638       if (is_cv_qualifier)
10639         *is_cv_qualifier = true;
10640       break;
10641
10642     case RID_VOLATILE:
10643       ds = ds_volatile;
10644       if (is_cv_qualifier)
10645         *is_cv_qualifier = true;
10646       break;
10647
10648     case RID_RESTRICT:
10649       ds = ds_restrict;
10650       if (is_cv_qualifier)
10651         *is_cv_qualifier = true;
10652       break;
10653
10654     case RID_COMPLEX:
10655       /* The `__complex__' keyword is a GNU extension.  */
10656       ds = ds_complex;
10657       break;
10658
10659     default:
10660       break;
10661     }
10662
10663   /* Handle simple keywords.  */
10664   if (ds != ds_last)
10665     {
10666       if (decl_specs)
10667         {
10668           ++decl_specs->specs[(int)ds];
10669           decl_specs->any_specifiers_p = true;
10670         }
10671       return cp_lexer_consume_token (parser->lexer)->u.value;
10672     }
10673
10674   /* If we do not already have a type-specifier, assume we are looking
10675      at a simple-type-specifier.  */
10676   type_spec = cp_parser_simple_type_specifier (parser,
10677                                                decl_specs,
10678                                                flags);
10679
10680   /* If we didn't find a type-specifier, and a type-specifier was not
10681      optional in this context, issue an error message.  */
10682   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10683     {
10684       cp_parser_error (parser, "expected type specifier");
10685       return error_mark_node;
10686     }
10687
10688   return type_spec;
10689 }
10690
10691 /* Parse a simple-type-specifier.
10692
10693    simple-type-specifier:
10694      :: [opt] nested-name-specifier [opt] type-name
10695      :: [opt] nested-name-specifier template template-id
10696      char
10697      wchar_t
10698      bool
10699      short
10700      int
10701      long
10702      signed
10703      unsigned
10704      float
10705      double
10706      void
10707
10708    C++0x Extension:
10709
10710    simple-type-specifier:
10711      decltype ( expression )   
10712
10713    GNU Extension:
10714
10715    simple-type-specifier:
10716      __typeof__ unary-expression
10717      __typeof__ ( type-id )
10718
10719    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10720    appropriately updated.  */
10721
10722 static tree
10723 cp_parser_simple_type_specifier (cp_parser* parser,
10724                                  cp_decl_specifier_seq *decl_specs,
10725                                  cp_parser_flags flags)
10726 {
10727   tree type = NULL_TREE;
10728   cp_token *token;
10729
10730   /* Peek at the next token.  */
10731   token = cp_lexer_peek_token (parser->lexer);
10732
10733   /* If we're looking at a keyword, things are easy.  */
10734   switch (token->keyword)
10735     {
10736     case RID_CHAR:
10737       if (decl_specs)
10738         decl_specs->explicit_char_p = true;
10739       type = char_type_node;
10740       break;
10741     case RID_WCHAR:
10742       type = wchar_type_node;
10743       break;
10744     case RID_BOOL:
10745       type = boolean_type_node;
10746       break;
10747     case RID_SHORT:
10748       if (decl_specs)
10749         ++decl_specs->specs[(int) ds_short];
10750       type = short_integer_type_node;
10751       break;
10752     case RID_INT:
10753       if (decl_specs)
10754         decl_specs->explicit_int_p = true;
10755       type = integer_type_node;
10756       break;
10757     case RID_LONG:
10758       if (decl_specs)
10759         ++decl_specs->specs[(int) ds_long];
10760       type = long_integer_type_node;
10761       break;
10762     case RID_SIGNED:
10763       if (decl_specs)
10764         ++decl_specs->specs[(int) ds_signed];
10765       type = integer_type_node;
10766       break;
10767     case RID_UNSIGNED:
10768       if (decl_specs)
10769         ++decl_specs->specs[(int) ds_unsigned];
10770       type = unsigned_type_node;
10771       break;
10772     case RID_FLOAT:
10773       type = float_type_node;
10774       break;
10775     case RID_DOUBLE:
10776       type = double_type_node;
10777       break;
10778     case RID_VOID:
10779       type = void_type_node;
10780       break;
10781
10782     case RID_DECLTYPE:
10783       /* Parse the `decltype' type.  */
10784       type = cp_parser_decltype (parser);
10785
10786       if (decl_specs)
10787         cp_parser_set_decl_spec_type (decl_specs, type,
10788                                       /*user_defined_p=*/true);
10789
10790       return type;
10791
10792     case RID_TYPEOF:
10793       /* Consume the `typeof' token.  */
10794       cp_lexer_consume_token (parser->lexer);
10795       /* Parse the operand to `typeof'.  */
10796       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10797       /* If it is not already a TYPE, take its type.  */
10798       if (!TYPE_P (type))
10799         type = finish_typeof (type);
10800
10801       if (decl_specs)
10802         cp_parser_set_decl_spec_type (decl_specs, type,
10803                                       /*user_defined_p=*/true);
10804
10805       return type;
10806
10807     default:
10808       break;
10809     }
10810
10811   /* If the type-specifier was for a built-in type, we're done.  */
10812   if (type)
10813     {
10814       tree id;
10815
10816       /* Record the type.  */
10817       if (decl_specs
10818           && (token->keyword != RID_SIGNED
10819               && token->keyword != RID_UNSIGNED
10820               && token->keyword != RID_SHORT
10821               && token->keyword != RID_LONG))
10822         cp_parser_set_decl_spec_type (decl_specs,
10823                                       type,
10824                                       /*user_defined=*/false);
10825       if (decl_specs)
10826         decl_specs->any_specifiers_p = true;
10827
10828       /* Consume the token.  */
10829       id = cp_lexer_consume_token (parser->lexer)->u.value;
10830
10831       /* There is no valid C++ program where a non-template type is
10832          followed by a "<".  That usually indicates that the user thought
10833          that the type was a template.  */
10834       cp_parser_check_for_invalid_template_id (parser, type);
10835
10836       return TYPE_NAME (type);
10837     }
10838
10839   /* The type-specifier must be a user-defined type.  */
10840   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10841     {
10842       bool qualified_p;
10843       bool global_p;
10844
10845       /* Don't gobble tokens or issue error messages if this is an
10846          optional type-specifier.  */
10847       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10848         cp_parser_parse_tentatively (parser);
10849
10850       /* Look for the optional `::' operator.  */
10851       global_p
10852         = (cp_parser_global_scope_opt (parser,
10853                                        /*current_scope_valid_p=*/false)
10854            != NULL_TREE);
10855       /* Look for the nested-name specifier.  */
10856       qualified_p
10857         = (cp_parser_nested_name_specifier_opt (parser,
10858                                                 /*typename_keyword_p=*/false,
10859                                                 /*check_dependency_p=*/true,
10860                                                 /*type_p=*/false,
10861                                                 /*is_declaration=*/false)
10862            != NULL_TREE);
10863       /* If we have seen a nested-name-specifier, and the next token
10864          is `template', then we are using the template-id production.  */
10865       if (parser->scope
10866           && cp_parser_optional_template_keyword (parser))
10867         {
10868           /* Look for the template-id.  */
10869           type = cp_parser_template_id (parser,
10870                                         /*template_keyword_p=*/true,
10871                                         /*check_dependency_p=*/true,
10872                                         /*is_declaration=*/false);
10873           /* If the template-id did not name a type, we are out of
10874              luck.  */
10875           if (TREE_CODE (type) != TYPE_DECL)
10876             {
10877               cp_parser_error (parser, "expected template-id for type");
10878               type = NULL_TREE;
10879             }
10880         }
10881       /* Otherwise, look for a type-name.  */
10882       else
10883         type = cp_parser_type_name (parser);
10884       /* Keep track of all name-lookups performed in class scopes.  */
10885       if (type
10886           && !global_p
10887           && !qualified_p
10888           && TREE_CODE (type) == TYPE_DECL
10889           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10890         maybe_note_name_used_in_class (DECL_NAME (type), type);
10891       /* If it didn't work out, we don't have a TYPE.  */
10892       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10893           && !cp_parser_parse_definitely (parser))
10894         type = NULL_TREE;
10895       if (type && decl_specs)
10896         cp_parser_set_decl_spec_type (decl_specs, type,
10897                                       /*user_defined=*/true);
10898     }
10899
10900   /* If we didn't get a type-name, issue an error message.  */
10901   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10902     {
10903       cp_parser_error (parser, "expected type-name");
10904       return error_mark_node;
10905     }
10906
10907   /* There is no valid C++ program where a non-template type is
10908      followed by a "<".  That usually indicates that the user thought
10909      that the type was a template.  */
10910   if (type && type != error_mark_node)
10911     {
10912       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10913          If it is, then the '<'...'>' enclose protocol names rather than
10914          template arguments, and so everything is fine.  */
10915       if (c_dialect_objc ()
10916           && (objc_is_id (type) || objc_is_class_name (type)))
10917         {
10918           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10919           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10920
10921           /* Clobber the "unqualified" type previously entered into
10922              DECL_SPECS with the new, improved protocol-qualified version.  */
10923           if (decl_specs)
10924             decl_specs->type = qual_type;
10925
10926           return qual_type;
10927         }
10928
10929       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10930     }
10931
10932   return type;
10933 }
10934
10935 /* Parse a type-name.
10936
10937    type-name:
10938      class-name
10939      enum-name
10940      typedef-name
10941
10942    enum-name:
10943      identifier
10944
10945    typedef-name:
10946      identifier
10947
10948    Returns a TYPE_DECL for the type.  */
10949
10950 static tree
10951 cp_parser_type_name (cp_parser* parser)
10952 {
10953   tree type_decl;
10954   tree identifier;
10955
10956   /* We can't know yet whether it is a class-name or not.  */
10957   cp_parser_parse_tentatively (parser);
10958   /* Try a class-name.  */
10959   type_decl = cp_parser_class_name (parser,
10960                                     /*typename_keyword_p=*/false,
10961                                     /*template_keyword_p=*/false,
10962                                     none_type,
10963                                     /*check_dependency_p=*/true,
10964                                     /*class_head_p=*/false,
10965                                     /*is_declaration=*/false);
10966   /* If it's not a class-name, keep looking.  */
10967   if (!cp_parser_parse_definitely (parser))
10968     {
10969       /* It must be a typedef-name or an enum-name.  */
10970       identifier = cp_parser_identifier (parser);
10971       if (identifier == error_mark_node)
10972         return error_mark_node;
10973
10974       /* Look up the type-name.  */
10975       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10976
10977       if (TREE_CODE (type_decl) != TYPE_DECL
10978           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10979         {
10980           /* See if this is an Objective-C type.  */
10981           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10982           tree type = objc_get_protocol_qualified_type (identifier, protos);
10983           if (type)
10984             type_decl = TYPE_NAME (type);
10985         }
10986
10987       /* Issue an error if we did not find a type-name.  */
10988       if (TREE_CODE (type_decl) != TYPE_DECL)
10989         {
10990           if (!cp_parser_simulate_error (parser))
10991             cp_parser_name_lookup_error (parser, identifier, type_decl,
10992                                          "is not a type");
10993           type_decl = error_mark_node;
10994         }
10995       /* Remember that the name was used in the definition of the
10996          current class so that we can check later to see if the
10997          meaning would have been different after the class was
10998          entirely defined.  */
10999       else if (type_decl != error_mark_node
11000                && !parser->scope)
11001         maybe_note_name_used_in_class (identifier, type_decl);
11002     }
11003
11004   return type_decl;
11005 }
11006
11007
11008 /* Parse an elaborated-type-specifier.  Note that the grammar given
11009    here incorporates the resolution to DR68.
11010
11011    elaborated-type-specifier:
11012      class-key :: [opt] nested-name-specifier [opt] identifier
11013      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11014      enum :: [opt] nested-name-specifier [opt] identifier
11015      typename :: [opt] nested-name-specifier identifier
11016      typename :: [opt] nested-name-specifier template [opt]
11017        template-id
11018
11019    GNU extension:
11020
11021    elaborated-type-specifier:
11022      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11023      class-key attributes :: [opt] nested-name-specifier [opt]
11024                template [opt] template-id
11025      enum attributes :: [opt] nested-name-specifier [opt] identifier
11026
11027    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11028    declared `friend'.  If IS_DECLARATION is TRUE, then this
11029    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11030    something is being declared.
11031
11032    Returns the TYPE specified.  */
11033
11034 static tree
11035 cp_parser_elaborated_type_specifier (cp_parser* parser,
11036                                      bool is_friend,
11037                                      bool is_declaration)
11038 {
11039   enum tag_types tag_type;
11040   tree identifier;
11041   tree type = NULL_TREE;
11042   tree attributes = NULL_TREE;
11043
11044   /* See if we're looking at the `enum' keyword.  */
11045   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11046     {
11047       /* Consume the `enum' token.  */
11048       cp_lexer_consume_token (parser->lexer);
11049       /* Remember that it's an enumeration type.  */
11050       tag_type = enum_type;
11051       /* Parse the attributes.  */
11052       attributes = cp_parser_attributes_opt (parser);
11053     }
11054   /* Or, it might be `typename'.  */
11055   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11056                                            RID_TYPENAME))
11057     {
11058       /* Consume the `typename' token.  */
11059       cp_lexer_consume_token (parser->lexer);
11060       /* Remember that it's a `typename' type.  */
11061       tag_type = typename_type;
11062       /* The `typename' keyword is only allowed in templates.  */
11063       if (!processing_template_decl)
11064         pedwarn ("using %<typename%> outside of template");
11065     }
11066   /* Otherwise it must be a class-key.  */
11067   else
11068     {
11069       tag_type = cp_parser_class_key (parser);
11070       if (tag_type == none_type)
11071         return error_mark_node;
11072       /* Parse the attributes.  */
11073       attributes = cp_parser_attributes_opt (parser);
11074     }
11075
11076   /* Look for the `::' operator.  */
11077   cp_parser_global_scope_opt (parser,
11078                               /*current_scope_valid_p=*/false);
11079   /* Look for the nested-name-specifier.  */
11080   if (tag_type == typename_type)
11081     {
11082       if (!cp_parser_nested_name_specifier (parser,
11083                                            /*typename_keyword_p=*/true,
11084                                            /*check_dependency_p=*/true,
11085                                            /*type_p=*/true,
11086                                             is_declaration))
11087         return error_mark_node;
11088     }
11089   else
11090     /* Even though `typename' is not present, the proposed resolution
11091        to Core Issue 180 says that in `class A<T>::B', `B' should be
11092        considered a type-name, even if `A<T>' is dependent.  */
11093     cp_parser_nested_name_specifier_opt (parser,
11094                                          /*typename_keyword_p=*/true,
11095                                          /*check_dependency_p=*/true,
11096                                          /*type_p=*/true,
11097                                          is_declaration);
11098  /* For everything but enumeration types, consider a template-id.
11099     For an enumeration type, consider only a plain identifier.  */
11100   if (tag_type != enum_type)
11101     {
11102       bool template_p = false;
11103       tree decl;
11104
11105       /* Allow the `template' keyword.  */
11106       template_p = cp_parser_optional_template_keyword (parser);
11107       /* If we didn't see `template', we don't know if there's a
11108          template-id or not.  */
11109       if (!template_p)
11110         cp_parser_parse_tentatively (parser);
11111       /* Parse the template-id.  */
11112       decl = cp_parser_template_id (parser, template_p,
11113                                     /*check_dependency_p=*/true,
11114                                     is_declaration);
11115       /* If we didn't find a template-id, look for an ordinary
11116          identifier.  */
11117       if (!template_p && !cp_parser_parse_definitely (parser))
11118         ;
11119       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11120          in effect, then we must assume that, upon instantiation, the
11121          template will correspond to a class.  */
11122       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11123                && tag_type == typename_type)
11124         type = make_typename_type (parser->scope, decl,
11125                                    typename_type,
11126                                    /*complain=*/tf_error);
11127       else
11128         type = TREE_TYPE (decl);
11129     }
11130
11131   if (!type)
11132     {
11133       identifier = cp_parser_identifier (parser);
11134
11135       if (identifier == error_mark_node)
11136         {
11137           parser->scope = NULL_TREE;
11138           return error_mark_node;
11139         }
11140
11141       /* For a `typename', we needn't call xref_tag.  */
11142       if (tag_type == typename_type
11143           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11144         return cp_parser_make_typename_type (parser, parser->scope,
11145                                              identifier);
11146       /* Look up a qualified name in the usual way.  */
11147       if (parser->scope)
11148         {
11149           tree decl;
11150           tree ambiguous_decls;
11151
11152           decl = cp_parser_lookup_name (parser, identifier,
11153                                         tag_type,
11154                                         /*is_template=*/false,
11155                                         /*is_namespace=*/false,
11156                                         /*check_dependency=*/true,
11157                                         &ambiguous_decls);
11158
11159           /* If the lookup was ambiguous, an error will already have been
11160              issued.  */
11161           if (ambiguous_decls)
11162             return error_mark_node;
11163
11164           /* If we are parsing friend declaration, DECL may be a
11165              TEMPLATE_DECL tree node here.  However, we need to check
11166              whether this TEMPLATE_DECL results in valid code.  Consider
11167              the following example:
11168
11169                namespace N {
11170                  template <class T> class C {};
11171                }
11172                class X {
11173                  template <class T> friend class N::C; // #1, valid code
11174                };
11175                template <class T> class Y {
11176                  friend class N::C;                    // #2, invalid code
11177                };
11178
11179              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11180              name lookup of `N::C'.  We see that friend declaration must
11181              be template for the code to be valid.  Note that
11182              processing_template_decl does not work here since it is
11183              always 1 for the above two cases.  */
11184
11185           decl = (cp_parser_maybe_treat_template_as_class
11186                   (decl, /*tag_name_p=*/is_friend
11187                          && parser->num_template_parameter_lists));
11188
11189           if (TREE_CODE (decl) != TYPE_DECL)
11190             {
11191               cp_parser_diagnose_invalid_type_name (parser,
11192                                                     parser->scope,
11193                                                     identifier);
11194               return error_mark_node;
11195             }
11196
11197           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11198             {
11199               bool allow_template = (parser->num_template_parameter_lists
11200                                       || DECL_SELF_REFERENCE_P (decl));
11201               type = check_elaborated_type_specifier (tag_type, decl, 
11202                                                       allow_template);
11203
11204               if (type == error_mark_node)
11205                 return error_mark_node;
11206             }
11207
11208           /* Forward declarations of nested types, such as
11209
11210                class C1::C2;
11211                class C1::C2::C3;
11212
11213              are invalid unless all components preceding the final '::'
11214              are complete.  If all enclosing types are complete, these
11215              declarations become merely pointless.
11216
11217              Invalid forward declarations of nested types are errors
11218              caught elsewhere in parsing.  Those that are pointless arrive
11219              here.  */
11220
11221           if (cp_parser_declares_only_class_p (parser)
11222               && !is_friend && !processing_explicit_instantiation)
11223             warning (0, "declaration %qD does not declare anything", decl);
11224
11225           type = TREE_TYPE (decl);
11226         }
11227       else
11228         {
11229           /* An elaborated-type-specifier sometimes introduces a new type and
11230              sometimes names an existing type.  Normally, the rule is that it
11231              introduces a new type only if there is not an existing type of
11232              the same name already in scope.  For example, given:
11233
11234                struct S {};
11235                void f() { struct S s; }
11236
11237              the `struct S' in the body of `f' is the same `struct S' as in
11238              the global scope; the existing definition is used.  However, if
11239              there were no global declaration, this would introduce a new
11240              local class named `S'.
11241
11242              An exception to this rule applies to the following code:
11243
11244                namespace N { struct S; }
11245
11246              Here, the elaborated-type-specifier names a new type
11247              unconditionally; even if there is already an `S' in the
11248              containing scope this declaration names a new type.
11249              This exception only applies if the elaborated-type-specifier
11250              forms the complete declaration:
11251
11252                [class.name]
11253
11254                A declaration consisting solely of `class-key identifier ;' is
11255                either a redeclaration of the name in the current scope or a
11256                forward declaration of the identifier as a class name.  It
11257                introduces the name into the current scope.
11258
11259              We are in this situation precisely when the next token is a `;'.
11260
11261              An exception to the exception is that a `friend' declaration does
11262              *not* name a new type; i.e., given:
11263
11264                struct S { friend struct T; };
11265
11266              `T' is not a new type in the scope of `S'.
11267
11268              Also, `new struct S' or `sizeof (struct S)' never results in the
11269              definition of a new type; a new type can only be declared in a
11270              declaration context.  */
11271
11272           tag_scope ts;
11273           bool template_p;
11274
11275           if (is_friend)
11276             /* Friends have special name lookup rules.  */
11277             ts = ts_within_enclosing_non_class;
11278           else if (is_declaration
11279                    && cp_lexer_next_token_is (parser->lexer,
11280                                               CPP_SEMICOLON))
11281             /* This is a `class-key identifier ;' */
11282             ts = ts_current;
11283           else
11284             ts = ts_global;
11285
11286           template_p =
11287             (parser->num_template_parameter_lists
11288              && (cp_parser_next_token_starts_class_definition_p (parser)
11289                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11290           /* An unqualified name was used to reference this type, so
11291              there were no qualifying templates.  */
11292           if (!cp_parser_check_template_parameters (parser,
11293                                                     /*num_templates=*/0))
11294             return error_mark_node;
11295           type = xref_tag (tag_type, identifier, ts, template_p);
11296         }
11297     }
11298
11299   if (type == error_mark_node)
11300     return error_mark_node;
11301
11302   /* Allow attributes on forward declarations of classes.  */
11303   if (attributes)
11304     {
11305       if (TREE_CODE (type) == TYPENAME_TYPE)
11306         warning (OPT_Wattributes,
11307                  "attributes ignored on uninstantiated type");
11308       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11309                && ! processing_explicit_instantiation)
11310         warning (OPT_Wattributes,
11311                  "attributes ignored on template instantiation");
11312       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11313         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11314       else
11315         warning (OPT_Wattributes,
11316                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11317     }
11318
11319   if (tag_type != enum_type)
11320     cp_parser_check_class_key (tag_type, type);
11321
11322   /* A "<" cannot follow an elaborated type specifier.  If that
11323      happens, the user was probably trying to form a template-id.  */
11324   cp_parser_check_for_invalid_template_id (parser, type);
11325
11326   return type;
11327 }
11328
11329 /* Parse an enum-specifier.
11330
11331    enum-specifier:
11332      enum identifier [opt] { enumerator-list [opt] }
11333
11334    GNU Extensions:
11335      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11336        attributes[opt]
11337
11338    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11339    if the token stream isn't an enum-specifier after all.  */
11340
11341 static tree
11342 cp_parser_enum_specifier (cp_parser* parser)
11343 {
11344   tree identifier;
11345   tree type;
11346   tree attributes;
11347
11348   /* Parse tentatively so that we can back up if we don't find a
11349      enum-specifier.  */
11350   cp_parser_parse_tentatively (parser);
11351
11352   /* Caller guarantees that the current token is 'enum', an identifier
11353      possibly follows, and the token after that is an opening brace.
11354      If we don't have an identifier, fabricate an anonymous name for
11355      the enumeration being defined.  */
11356   cp_lexer_consume_token (parser->lexer);
11357
11358   attributes = cp_parser_attributes_opt (parser);
11359
11360   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11361     identifier = cp_parser_identifier (parser);
11362   else
11363     identifier = make_anon_name ();
11364
11365   /* Look for the `{' but don't consume it yet.  */
11366   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11367     cp_parser_simulate_error (parser);
11368
11369   if (!cp_parser_parse_definitely (parser))
11370     return NULL_TREE;
11371
11372   /* Issue an error message if type-definitions are forbidden here.  */
11373   if (!cp_parser_check_type_definition (parser))
11374     type = error_mark_node;
11375   else
11376     /* Create the new type.  We do this before consuming the opening
11377        brace so the enum will be recorded as being on the line of its
11378        tag (or the 'enum' keyword, if there is no tag).  */
11379     type = start_enum (identifier);
11380   
11381   /* Consume the opening brace.  */
11382   cp_lexer_consume_token (parser->lexer);
11383
11384   if (type == error_mark_node)
11385     {
11386       cp_parser_skip_to_end_of_block_or_statement (parser);
11387       return error_mark_node;
11388     }
11389
11390   /* If the next token is not '}', then there are some enumerators.  */
11391   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11392     cp_parser_enumerator_list (parser, type);
11393
11394   /* Consume the final '}'.  */
11395   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11396
11397   /* Look for trailing attributes to apply to this enumeration, and
11398      apply them if appropriate.  */
11399   if (cp_parser_allow_gnu_extensions_p (parser))
11400     {
11401       tree trailing_attr = cp_parser_attributes_opt (parser);
11402       cplus_decl_attributes (&type,
11403                              trailing_attr,
11404                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11405     }
11406
11407   /* Finish up the enumeration.  */
11408   finish_enum (type);
11409
11410   return type;
11411 }
11412
11413 /* Parse an enumerator-list.  The enumerators all have the indicated
11414    TYPE.
11415
11416    enumerator-list:
11417      enumerator-definition
11418      enumerator-list , enumerator-definition  */
11419
11420 static void
11421 cp_parser_enumerator_list (cp_parser* parser, tree type)
11422 {
11423   while (true)
11424     {
11425       /* Parse an enumerator-definition.  */
11426       cp_parser_enumerator_definition (parser, type);
11427
11428       /* If the next token is not a ',', we've reached the end of
11429          the list.  */
11430       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11431         break;
11432       /* Otherwise, consume the `,' and keep going.  */
11433       cp_lexer_consume_token (parser->lexer);
11434       /* If the next token is a `}', there is a trailing comma.  */
11435       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11436         {
11437           if (pedantic && !in_system_header)
11438             pedwarn ("comma at end of enumerator list");
11439           break;
11440         }
11441     }
11442 }
11443
11444 /* Parse an enumerator-definition.  The enumerator has the indicated
11445    TYPE.
11446
11447    enumerator-definition:
11448      enumerator
11449      enumerator = constant-expression
11450
11451    enumerator:
11452      identifier  */
11453
11454 static void
11455 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11456 {
11457   tree identifier;
11458   tree value;
11459
11460   /* Look for the identifier.  */
11461   identifier = cp_parser_identifier (parser);
11462   if (identifier == error_mark_node)
11463     return;
11464
11465   /* If the next token is an '=', then there is an explicit value.  */
11466   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11467     {
11468       /* Consume the `=' token.  */
11469       cp_lexer_consume_token (parser->lexer);
11470       /* Parse the value.  */
11471       value = cp_parser_constant_expression (parser,
11472                                              /*allow_non_constant_p=*/false,
11473                                              NULL);
11474     }
11475   else
11476     value = NULL_TREE;
11477
11478   /* Create the enumerator.  */
11479   build_enumerator (identifier, value, type);
11480 }
11481
11482 /* Parse a namespace-name.
11483
11484    namespace-name:
11485      original-namespace-name
11486      namespace-alias
11487
11488    Returns the NAMESPACE_DECL for the namespace.  */
11489
11490 static tree
11491 cp_parser_namespace_name (cp_parser* parser)
11492 {
11493   tree identifier;
11494   tree namespace_decl;
11495
11496   /* Get the name of the namespace.  */
11497   identifier = cp_parser_identifier (parser);
11498   if (identifier == error_mark_node)
11499     return error_mark_node;
11500
11501   /* Look up the identifier in the currently active scope.  Look only
11502      for namespaces, due to:
11503
11504        [basic.lookup.udir]
11505
11506        When looking up a namespace-name in a using-directive or alias
11507        definition, only namespace names are considered.
11508
11509      And:
11510
11511        [basic.lookup.qual]
11512
11513        During the lookup of a name preceding the :: scope resolution
11514        operator, object, function, and enumerator names are ignored.
11515
11516      (Note that cp_parser_class_or_namespace_name only calls this
11517      function if the token after the name is the scope resolution
11518      operator.)  */
11519   namespace_decl = cp_parser_lookup_name (parser, identifier,
11520                                           none_type,
11521                                           /*is_template=*/false,
11522                                           /*is_namespace=*/true,
11523                                           /*check_dependency=*/true,
11524                                           /*ambiguous_decls=*/NULL);
11525   /* If it's not a namespace, issue an error.  */
11526   if (namespace_decl == error_mark_node
11527       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11528     {
11529       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11530         error ("%qD is not a namespace-name", identifier);
11531       cp_parser_error (parser, "expected namespace-name");
11532       namespace_decl = error_mark_node;
11533     }
11534
11535   return namespace_decl;
11536 }
11537
11538 /* Parse a namespace-definition.
11539
11540    namespace-definition:
11541      named-namespace-definition
11542      unnamed-namespace-definition
11543
11544    named-namespace-definition:
11545      original-namespace-definition
11546      extension-namespace-definition
11547
11548    original-namespace-definition:
11549      namespace identifier { namespace-body }
11550
11551    extension-namespace-definition:
11552      namespace original-namespace-name { namespace-body }
11553
11554    unnamed-namespace-definition:
11555      namespace { namespace-body } */
11556
11557 static void
11558 cp_parser_namespace_definition (cp_parser* parser)
11559 {
11560   tree identifier, attribs;
11561   bool has_visibility;
11562   bool is_inline;
11563
11564   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11565     {
11566       is_inline = true;
11567       cp_lexer_consume_token (parser->lexer);
11568     }
11569   else
11570     is_inline = false;
11571
11572   /* Look for the `namespace' keyword.  */
11573   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11574
11575   /* Get the name of the namespace.  We do not attempt to distinguish
11576      between an original-namespace-definition and an
11577      extension-namespace-definition at this point.  The semantic
11578      analysis routines are responsible for that.  */
11579   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11580     identifier = cp_parser_identifier (parser);
11581   else
11582     identifier = NULL_TREE;
11583
11584   /* Parse any specified attributes.  */
11585   attribs = cp_parser_attributes_opt (parser);
11586
11587   /* Look for the `{' to start the namespace.  */
11588   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11589   /* Start the namespace.  */
11590   push_namespace (identifier);
11591
11592   /* "inline namespace" is equivalent to a stub namespace definition
11593      followed by a strong using directive.  */
11594   if (is_inline)
11595     {
11596       tree namespace = current_namespace;
11597       /* Set up namespace association.  */
11598       DECL_NAMESPACE_ASSOCIATIONS (namespace)
11599         = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11600                      DECL_NAMESPACE_ASSOCIATIONS (namespace));
11601       /* Import the contents of the inline namespace.  */
11602       pop_namespace ();
11603       do_using_directive (namespace);
11604       push_namespace (identifier);
11605     }
11606
11607   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11608
11609   /* Parse the body of the namespace.  */
11610   cp_parser_namespace_body (parser);
11611
11612 #ifdef HANDLE_PRAGMA_VISIBILITY
11613   if (has_visibility)
11614     pop_visibility ();
11615 #endif
11616
11617   /* Finish the namespace.  */
11618   pop_namespace ();
11619   /* Look for the final `}'.  */
11620   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11621 }
11622
11623 /* Parse a namespace-body.
11624
11625    namespace-body:
11626      declaration-seq [opt]  */
11627
11628 static void
11629 cp_parser_namespace_body (cp_parser* parser)
11630 {
11631   cp_parser_declaration_seq_opt (parser);
11632 }
11633
11634 /* Parse a namespace-alias-definition.
11635
11636    namespace-alias-definition:
11637      namespace identifier = qualified-namespace-specifier ;  */
11638
11639 static void
11640 cp_parser_namespace_alias_definition (cp_parser* parser)
11641 {
11642   tree identifier;
11643   tree namespace_specifier;
11644
11645   /* Look for the `namespace' keyword.  */
11646   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11647   /* Look for the identifier.  */
11648   identifier = cp_parser_identifier (parser);
11649   if (identifier == error_mark_node)
11650     return;
11651   /* Look for the `=' token.  */
11652   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11653       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11654     {
11655       error ("%<namespace%> definition is not allowed here");
11656       /* Skip the definition.  */
11657       cp_lexer_consume_token (parser->lexer);
11658       if (cp_parser_skip_to_closing_brace (parser))
11659         cp_lexer_consume_token (parser->lexer);
11660       return;
11661     }
11662   cp_parser_require (parser, CPP_EQ, "`='");
11663   /* Look for the qualified-namespace-specifier.  */
11664   namespace_specifier
11665     = cp_parser_qualified_namespace_specifier (parser);
11666   /* Look for the `;' token.  */
11667   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11668
11669   /* Register the alias in the symbol table.  */
11670   do_namespace_alias (identifier, namespace_specifier);
11671 }
11672
11673 /* Parse a qualified-namespace-specifier.
11674
11675    qualified-namespace-specifier:
11676      :: [opt] nested-name-specifier [opt] namespace-name
11677
11678    Returns a NAMESPACE_DECL corresponding to the specified
11679    namespace.  */
11680
11681 static tree
11682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11683 {
11684   /* Look for the optional `::'.  */
11685   cp_parser_global_scope_opt (parser,
11686                               /*current_scope_valid_p=*/false);
11687
11688   /* Look for the optional nested-name-specifier.  */
11689   cp_parser_nested_name_specifier_opt (parser,
11690                                        /*typename_keyword_p=*/false,
11691                                        /*check_dependency_p=*/true,
11692                                        /*type_p=*/false,
11693                                        /*is_declaration=*/true);
11694
11695   return cp_parser_namespace_name (parser);
11696 }
11697
11698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11699    access declaration.
11700
11701    using-declaration:
11702      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11703      using :: unqualified-id ;  
11704
11705    access-declaration:
11706      qualified-id ;  
11707
11708    */
11709
11710 static bool
11711 cp_parser_using_declaration (cp_parser* parser, 
11712                              bool access_declaration_p)
11713 {
11714   cp_token *token;
11715   bool typename_p = false;
11716   bool global_scope_p;
11717   tree decl;
11718   tree identifier;
11719   tree qscope;
11720
11721   if (access_declaration_p)
11722     cp_parser_parse_tentatively (parser);
11723   else
11724     {
11725       /* Look for the `using' keyword.  */
11726       cp_parser_require_keyword (parser, RID_USING, "`using'");
11727       
11728       /* Peek at the next token.  */
11729       token = cp_lexer_peek_token (parser->lexer);
11730       /* See if it's `typename'.  */
11731       if (token->keyword == RID_TYPENAME)
11732         {
11733           /* Remember that we've seen it.  */
11734           typename_p = true;
11735           /* Consume the `typename' token.  */
11736           cp_lexer_consume_token (parser->lexer);
11737         }
11738     }
11739
11740   /* Look for the optional global scope qualification.  */
11741   global_scope_p
11742     = (cp_parser_global_scope_opt (parser,
11743                                    /*current_scope_valid_p=*/false)
11744        != NULL_TREE);
11745
11746   /* If we saw `typename', or didn't see `::', then there must be a
11747      nested-name-specifier present.  */
11748   if (typename_p || !global_scope_p)
11749     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11750                                               /*check_dependency_p=*/true,
11751                                               /*type_p=*/false,
11752                                               /*is_declaration=*/true);
11753   /* Otherwise, we could be in either of the two productions.  In that
11754      case, treat the nested-name-specifier as optional.  */
11755   else
11756     qscope = cp_parser_nested_name_specifier_opt (parser,
11757                                                   /*typename_keyword_p=*/false,
11758                                                   /*check_dependency_p=*/true,
11759                                                   /*type_p=*/false,
11760                                                   /*is_declaration=*/true);
11761   if (!qscope)
11762     qscope = global_namespace;
11763
11764   if (access_declaration_p && cp_parser_error_occurred (parser))
11765     /* Something has already gone wrong; there's no need to parse
11766        further.  Since an error has occurred, the return value of
11767        cp_parser_parse_definitely will be false, as required.  */
11768     return cp_parser_parse_definitely (parser);
11769
11770   /* Parse the unqualified-id.  */
11771   identifier = cp_parser_unqualified_id (parser,
11772                                          /*template_keyword_p=*/false,
11773                                          /*check_dependency_p=*/true,
11774                                          /*declarator_p=*/true,
11775                                          /*optional_p=*/false);
11776
11777   if (access_declaration_p)
11778     {
11779       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11780         cp_parser_simulate_error (parser);
11781       if (!cp_parser_parse_definitely (parser))
11782         return false;
11783     }
11784
11785   /* The function we call to handle a using-declaration is different
11786      depending on what scope we are in.  */
11787   if (qscope == error_mark_node || identifier == error_mark_node)
11788     ;
11789   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11790            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11791     /* [namespace.udecl]
11792
11793        A using declaration shall not name a template-id.  */
11794     error ("a template-id may not appear in a using-declaration");
11795   else
11796     {
11797       if (at_class_scope_p ())
11798         {
11799           /* Create the USING_DECL.  */
11800           decl = do_class_using_decl (parser->scope, identifier);
11801
11802           if (check_for_bare_parameter_packs (decl))
11803             return false;
11804           else
11805             /* Add it to the list of members in this class.  */
11806             finish_member_declaration (decl);
11807         }
11808       else
11809         {
11810           decl = cp_parser_lookup_name_simple (parser, identifier);
11811           if (decl == error_mark_node)
11812             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11813           else if (check_for_bare_parameter_packs (decl))
11814             return false;
11815           else if (!at_namespace_scope_p ())
11816             do_local_using_decl (decl, qscope, identifier);
11817           else
11818             do_toplevel_using_decl (decl, qscope, identifier);
11819         }
11820     }
11821
11822   /* Look for the final `;'.  */
11823   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11824   
11825   return true;
11826 }
11827
11828 /* Parse a using-directive.
11829
11830    using-directive:
11831      using namespace :: [opt] nested-name-specifier [opt]
11832        namespace-name ;  */
11833
11834 static void
11835 cp_parser_using_directive (cp_parser* parser)
11836 {
11837   tree namespace_decl;
11838   tree attribs;
11839
11840   /* Look for the `using' keyword.  */
11841   cp_parser_require_keyword (parser, RID_USING, "`using'");
11842   /* And the `namespace' keyword.  */
11843   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11844   /* Look for the optional `::' operator.  */
11845   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11846   /* And the optional nested-name-specifier.  */
11847   cp_parser_nested_name_specifier_opt (parser,
11848                                        /*typename_keyword_p=*/false,
11849                                        /*check_dependency_p=*/true,
11850                                        /*type_p=*/false,
11851                                        /*is_declaration=*/true);
11852   /* Get the namespace being used.  */
11853   namespace_decl = cp_parser_namespace_name (parser);
11854   /* And any specified attributes.  */
11855   attribs = cp_parser_attributes_opt (parser);
11856   /* Update the symbol table.  */
11857   parse_using_directive (namespace_decl, attribs);
11858   /* Look for the final `;'.  */
11859   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11860 }
11861
11862 /* Parse an asm-definition.
11863
11864    asm-definition:
11865      asm ( string-literal ) ;
11866
11867    GNU Extension:
11868
11869    asm-definition:
11870      asm volatile [opt] ( string-literal ) ;
11871      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11872      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11873                           : asm-operand-list [opt] ) ;
11874      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11875                           : asm-operand-list [opt]
11876                           : asm-operand-list [opt] ) ;  */
11877
11878 static void
11879 cp_parser_asm_definition (cp_parser* parser)
11880 {
11881   tree string;
11882   tree outputs = NULL_TREE;
11883   tree inputs = NULL_TREE;
11884   tree clobbers = NULL_TREE;
11885   tree asm_stmt;
11886   bool volatile_p = false;
11887   bool extended_p = false;
11888   bool invalid_inputs_p = false;
11889   bool invalid_outputs_p = false;
11890
11891   /* Look for the `asm' keyword.  */
11892   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11893   /* See if the next token is `volatile'.  */
11894   if (cp_parser_allow_gnu_extensions_p (parser)
11895       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11896     {
11897       /* Remember that we saw the `volatile' keyword.  */
11898       volatile_p = true;
11899       /* Consume the token.  */
11900       cp_lexer_consume_token (parser->lexer);
11901     }
11902   /* Look for the opening `('.  */
11903   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11904     return;
11905   /* Look for the string.  */
11906   string = cp_parser_string_literal (parser, false, false);
11907   if (string == error_mark_node)
11908     {
11909       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11910                                              /*consume_paren=*/true);
11911       return;
11912     }
11913
11914   /* If we're allowing GNU extensions, check for the extended assembly
11915      syntax.  Unfortunately, the `:' tokens need not be separated by
11916      a space in C, and so, for compatibility, we tolerate that here
11917      too.  Doing that means that we have to treat the `::' operator as
11918      two `:' tokens.  */
11919   if (cp_parser_allow_gnu_extensions_p (parser)
11920       && parser->in_function_body
11921       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11922           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11923     {
11924       bool inputs_p = false;
11925       bool clobbers_p = false;
11926
11927       /* The extended syntax was used.  */
11928       extended_p = true;
11929
11930       /* Look for outputs.  */
11931       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11932         {
11933           /* Consume the `:'.  */
11934           cp_lexer_consume_token (parser->lexer);
11935           /* Parse the output-operands.  */
11936           if (cp_lexer_next_token_is_not (parser->lexer,
11937                                           CPP_COLON)
11938               && cp_lexer_next_token_is_not (parser->lexer,
11939                                              CPP_SCOPE)
11940               && cp_lexer_next_token_is_not (parser->lexer,
11941                                              CPP_CLOSE_PAREN))
11942             outputs = cp_parser_asm_operand_list (parser);
11943
11944             if (outputs == error_mark_node)
11945               invalid_outputs_p = true;
11946         }
11947       /* If the next token is `::', there are no outputs, and the
11948          next token is the beginning of the inputs.  */
11949       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11950         /* The inputs are coming next.  */
11951         inputs_p = true;
11952
11953       /* Look for inputs.  */
11954       if (inputs_p
11955           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11956         {
11957           /* Consume the `:' or `::'.  */
11958           cp_lexer_consume_token (parser->lexer);
11959           /* Parse the output-operands.  */
11960           if (cp_lexer_next_token_is_not (parser->lexer,
11961                                           CPP_COLON)
11962               && cp_lexer_next_token_is_not (parser->lexer,
11963                                              CPP_CLOSE_PAREN))
11964             inputs = cp_parser_asm_operand_list (parser);
11965
11966             if (inputs == error_mark_node)
11967               invalid_inputs_p = true;
11968         }
11969       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11970         /* The clobbers are coming next.  */
11971         clobbers_p = true;
11972
11973       /* Look for clobbers.  */
11974       if (clobbers_p
11975           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11976         {
11977           /* Consume the `:' or `::'.  */
11978           cp_lexer_consume_token (parser->lexer);
11979           /* Parse the clobbers.  */
11980           if (cp_lexer_next_token_is_not (parser->lexer,
11981                                           CPP_CLOSE_PAREN))
11982             clobbers = cp_parser_asm_clobber_list (parser);
11983         }
11984     }
11985   /* Look for the closing `)'.  */
11986   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11987     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11988                                            /*consume_paren=*/true);
11989   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11990
11991   if (!invalid_inputs_p && !invalid_outputs_p)
11992     {
11993       /* Create the ASM_EXPR.  */
11994       if (parser->in_function_body)
11995         {
11996           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11997                                       inputs, clobbers);
11998           /* If the extended syntax was not used, mark the ASM_EXPR.  */
11999           if (!extended_p)
12000             {
12001               tree temp = asm_stmt;
12002               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12003                 temp = TREE_OPERAND (temp, 0);
12004
12005               ASM_INPUT_P (temp) = 1;
12006             }
12007         }
12008       else
12009         cgraph_add_asm_node (string);
12010     }
12011 }
12012
12013 /* Declarators [gram.dcl.decl] */
12014
12015 /* Parse an init-declarator.
12016
12017    init-declarator:
12018      declarator initializer [opt]
12019
12020    GNU Extension:
12021
12022    init-declarator:
12023      declarator asm-specification [opt] attributes [opt] initializer [opt]
12024
12025    function-definition:
12026      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12027        function-body
12028      decl-specifier-seq [opt] declarator function-try-block
12029
12030    GNU Extension:
12031
12032    function-definition:
12033      __extension__ function-definition
12034
12035    The DECL_SPECIFIERS apply to this declarator.  Returns a
12036    representation of the entity declared.  If MEMBER_P is TRUE, then
12037    this declarator appears in a class scope.  The new DECL created by
12038    this declarator is returned.
12039
12040    The CHECKS are access checks that should be performed once we know
12041    what entity is being declared (and, therefore, what classes have
12042    befriended it).
12043
12044    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12045    for a function-definition here as well.  If the declarator is a
12046    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12047    be TRUE upon return.  By that point, the function-definition will
12048    have been completely parsed.
12049
12050    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12051    is FALSE.  */
12052
12053 static tree
12054 cp_parser_init_declarator (cp_parser* parser,
12055                            cp_decl_specifier_seq *decl_specifiers,
12056                            VEC (deferred_access_check,gc)* checks,
12057                            bool function_definition_allowed_p,
12058                            bool member_p,
12059                            int declares_class_or_enum,
12060                            bool* function_definition_p)
12061 {
12062   cp_token *token;
12063   cp_declarator *declarator;
12064   tree prefix_attributes;
12065   tree attributes;
12066   tree asm_specification;
12067   tree initializer;
12068   tree decl = NULL_TREE;
12069   tree scope;
12070   bool is_initialized;
12071   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12072      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12073      "(...)".  */
12074   enum cpp_ttype initialization_kind;
12075   bool is_parenthesized_init = false;
12076   bool is_non_constant_init;
12077   int ctor_dtor_or_conv_p;
12078   bool friend_p;
12079   tree pushed_scope = NULL;
12080
12081   /* Gather the attributes that were provided with the
12082      decl-specifiers.  */
12083   prefix_attributes = decl_specifiers->attributes;
12084
12085   /* Assume that this is not the declarator for a function
12086      definition.  */
12087   if (function_definition_p)
12088     *function_definition_p = false;
12089
12090   /* Defer access checks while parsing the declarator; we cannot know
12091      what names are accessible until we know what is being
12092      declared.  */
12093   resume_deferring_access_checks ();
12094
12095   /* Parse the declarator.  */
12096   declarator
12097     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12098                             &ctor_dtor_or_conv_p,
12099                             /*parenthesized_p=*/NULL,
12100                             /*member_p=*/false);
12101   /* Gather up the deferred checks.  */
12102   stop_deferring_access_checks ();
12103
12104   /* If the DECLARATOR was erroneous, there's no need to go
12105      further.  */
12106   if (declarator == cp_error_declarator)
12107     return error_mark_node;
12108
12109   /* Check that the number of template-parameter-lists is OK.  */
12110   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12111     return error_mark_node;
12112
12113   if (declares_class_or_enum & 2)
12114     cp_parser_check_for_definition_in_return_type (declarator,
12115                                                    decl_specifiers->type);
12116
12117   /* Figure out what scope the entity declared by the DECLARATOR is
12118      located in.  `grokdeclarator' sometimes changes the scope, so
12119      we compute it now.  */
12120   scope = get_scope_of_declarator (declarator);
12121
12122   /* If we're allowing GNU extensions, look for an asm-specification
12123      and attributes.  */
12124   if (cp_parser_allow_gnu_extensions_p (parser))
12125     {
12126       /* Look for an asm-specification.  */
12127       asm_specification = cp_parser_asm_specification_opt (parser);
12128       /* And attributes.  */
12129       attributes = cp_parser_attributes_opt (parser);
12130     }
12131   else
12132     {
12133       asm_specification = NULL_TREE;
12134       attributes = NULL_TREE;
12135     }
12136
12137   /* Peek at the next token.  */
12138   token = cp_lexer_peek_token (parser->lexer);
12139   /* Check to see if the token indicates the start of a
12140      function-definition.  */
12141   if (cp_parser_token_starts_function_definition_p (token))
12142     {
12143       if (!function_definition_allowed_p)
12144         {
12145           /* If a function-definition should not appear here, issue an
12146              error message.  */
12147           cp_parser_error (parser,
12148                            "a function-definition is not allowed here");
12149           return error_mark_node;
12150         }
12151       else
12152         {
12153           /* Neither attributes nor an asm-specification are allowed
12154              on a function-definition.  */
12155           if (asm_specification)
12156             error ("an asm-specification is not allowed on a function-definition");
12157           if (attributes)
12158             error ("attributes are not allowed on a function-definition");
12159           /* This is a function-definition.  */
12160           *function_definition_p = true;
12161
12162           /* Parse the function definition.  */
12163           if (member_p)
12164             decl = cp_parser_save_member_function_body (parser,
12165                                                         decl_specifiers,
12166                                                         declarator,
12167                                                         prefix_attributes);
12168           else
12169             decl
12170               = (cp_parser_function_definition_from_specifiers_and_declarator
12171                  (parser, decl_specifiers, prefix_attributes, declarator));
12172
12173           return decl;
12174         }
12175     }
12176
12177   /* [dcl.dcl]
12178
12179      Only in function declarations for constructors, destructors, and
12180      type conversions can the decl-specifier-seq be omitted.
12181
12182      We explicitly postpone this check past the point where we handle
12183      function-definitions because we tolerate function-definitions
12184      that are missing their return types in some modes.  */
12185   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12186     {
12187       cp_parser_error (parser,
12188                        "expected constructor, destructor, or type conversion");
12189       return error_mark_node;
12190     }
12191
12192   /* An `=' or an `(' indicates an initializer.  */
12193   if (token->type == CPP_EQ
12194       || token->type == CPP_OPEN_PAREN)
12195     {
12196       is_initialized = true;
12197       initialization_kind = token->type;
12198     }
12199   else
12200     {
12201       /* If the init-declarator isn't initialized and isn't followed by a
12202          `,' or `;', it's not a valid init-declarator.  */
12203       if (token->type != CPP_COMMA
12204           && token->type != CPP_SEMICOLON)
12205         {
12206           cp_parser_error (parser, "expected initializer");
12207           return error_mark_node;
12208         }
12209       is_initialized = false;
12210       initialization_kind = CPP_EOF;
12211     }
12212
12213   /* Because start_decl has side-effects, we should only call it if we
12214      know we're going ahead.  By this point, we know that we cannot
12215      possibly be looking at any other construct.  */
12216   cp_parser_commit_to_tentative_parse (parser);
12217
12218   /* If the decl specifiers were bad, issue an error now that we're
12219      sure this was intended to be a declarator.  Then continue
12220      declaring the variable(s), as int, to try to cut down on further
12221      errors.  */
12222   if (decl_specifiers->any_specifiers_p
12223       && decl_specifiers->type == error_mark_node)
12224     {
12225       cp_parser_error (parser, "invalid type in declaration");
12226       decl_specifiers->type = integer_type_node;
12227     }
12228
12229   /* Check to see whether or not this declaration is a friend.  */
12230   friend_p = cp_parser_friend_p (decl_specifiers);
12231
12232   /* Enter the newly declared entry in the symbol table.  If we're
12233      processing a declaration in a class-specifier, we wait until
12234      after processing the initializer.  */
12235   if (!member_p)
12236     {
12237       if (parser->in_unbraced_linkage_specification_p)
12238         decl_specifiers->storage_class = sc_extern;
12239       decl = start_decl (declarator, decl_specifiers,
12240                          is_initialized, attributes, prefix_attributes,
12241                          &pushed_scope);
12242     }
12243   else if (scope)
12244     /* Enter the SCOPE.  That way unqualified names appearing in the
12245        initializer will be looked up in SCOPE.  */
12246     pushed_scope = push_scope (scope);
12247
12248   /* Perform deferred access control checks, now that we know in which
12249      SCOPE the declared entity resides.  */
12250   if (!member_p && decl)
12251     {
12252       tree saved_current_function_decl = NULL_TREE;
12253
12254       /* If the entity being declared is a function, pretend that we
12255          are in its scope.  If it is a `friend', it may have access to
12256          things that would not otherwise be accessible.  */
12257       if (TREE_CODE (decl) == FUNCTION_DECL)
12258         {
12259           saved_current_function_decl = current_function_decl;
12260           current_function_decl = decl;
12261         }
12262
12263       /* Perform access checks for template parameters.  */
12264       cp_parser_perform_template_parameter_access_checks (checks);
12265
12266       /* Perform the access control checks for the declarator and the
12267          the decl-specifiers.  */
12268       perform_deferred_access_checks ();
12269
12270       /* Restore the saved value.  */
12271       if (TREE_CODE (decl) == FUNCTION_DECL)
12272         current_function_decl = saved_current_function_decl;
12273     }
12274
12275   /* Parse the initializer.  */
12276   initializer = NULL_TREE;
12277   is_parenthesized_init = false;
12278   is_non_constant_init = true;
12279   if (is_initialized)
12280     {
12281       if (function_declarator_p (declarator))
12282         {
12283            if (initialization_kind == CPP_EQ)
12284              initializer = cp_parser_pure_specifier (parser);
12285            else
12286              {
12287                /* If the declaration was erroneous, we don't really
12288                   know what the user intended, so just silently
12289                   consume the initializer.  */
12290                if (decl != error_mark_node)
12291                  error ("initializer provided for function");
12292                cp_parser_skip_to_closing_parenthesis (parser,
12293                                                       /*recovering=*/true,
12294                                                       /*or_comma=*/false,
12295                                                       /*consume_paren=*/true);
12296              }
12297         }
12298       else
12299         initializer = cp_parser_initializer (parser,
12300                                              &is_parenthesized_init,
12301                                              &is_non_constant_init);
12302     }
12303
12304   /* The old parser allows attributes to appear after a parenthesized
12305      initializer.  Mark Mitchell proposed removing this functionality
12306      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12307      attributes -- but ignores them.  */
12308   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12309     if (cp_parser_attributes_opt (parser))
12310       warning (OPT_Wattributes,
12311                "attributes after parenthesized initializer ignored");
12312
12313   /* For an in-class declaration, use `grokfield' to create the
12314      declaration.  */
12315   if (member_p)
12316     {
12317       if (pushed_scope)
12318         {
12319           pop_scope (pushed_scope);
12320           pushed_scope = false;
12321         }
12322       decl = grokfield (declarator, decl_specifiers,
12323                         initializer, !is_non_constant_init,
12324                         /*asmspec=*/NULL_TREE,
12325                         prefix_attributes);
12326       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12327         cp_parser_save_default_args (parser, decl);
12328     }
12329
12330   /* Finish processing the declaration.  But, skip friend
12331      declarations.  */
12332   if (!friend_p && decl && decl != error_mark_node)
12333     {
12334       cp_finish_decl (decl,
12335                       initializer, !is_non_constant_init,
12336                       asm_specification,
12337                       /* If the initializer is in parentheses, then this is
12338                          a direct-initialization, which means that an
12339                          `explicit' constructor is OK.  Otherwise, an
12340                          `explicit' constructor cannot be used.  */
12341                       ((is_parenthesized_init || !is_initialized)
12342                      ? 0 : LOOKUP_ONLYCONVERTING));
12343     }
12344   else if ((cxx_dialect != cxx98) && friend_p
12345            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12346     /* Core issue #226 (C++0x only): A default template-argument
12347        shall not be specified in a friend class template
12348        declaration. */
12349     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12350                              /*is_partial=*/0, /*is_friend_decl=*/1);
12351
12352   if (!friend_p && pushed_scope)
12353     pop_scope (pushed_scope);
12354
12355   return decl;
12356 }
12357
12358 /* Parse a declarator.
12359
12360    declarator:
12361      direct-declarator
12362      ptr-operator declarator
12363
12364    abstract-declarator:
12365      ptr-operator abstract-declarator [opt]
12366      direct-abstract-declarator
12367
12368    GNU Extensions:
12369
12370    declarator:
12371      attributes [opt] direct-declarator
12372      attributes [opt] ptr-operator declarator
12373
12374    abstract-declarator:
12375      attributes [opt] ptr-operator abstract-declarator [opt]
12376      attributes [opt] direct-abstract-declarator
12377
12378    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12379    detect constructor, destructor or conversion operators. It is set
12380    to -1 if the declarator is a name, and +1 if it is a
12381    function. Otherwise it is set to zero. Usually you just want to
12382    test for >0, but internally the negative value is used.
12383
12384    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12385    a decl-specifier-seq unless it declares a constructor, destructor,
12386    or conversion.  It might seem that we could check this condition in
12387    semantic analysis, rather than parsing, but that makes it difficult
12388    to handle something like `f()'.  We want to notice that there are
12389    no decl-specifiers, and therefore realize that this is an
12390    expression, not a declaration.)
12391
12392    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12393    the declarator is a direct-declarator of the form "(...)".
12394
12395    MEMBER_P is true iff this declarator is a member-declarator.  */
12396
12397 static cp_declarator *
12398 cp_parser_declarator (cp_parser* parser,
12399                       cp_parser_declarator_kind dcl_kind,
12400                       int* ctor_dtor_or_conv_p,
12401                       bool* parenthesized_p,
12402                       bool member_p)
12403 {
12404   cp_token *token;
12405   cp_declarator *declarator;
12406   enum tree_code code;
12407   cp_cv_quals cv_quals;
12408   tree class_type;
12409   tree attributes = NULL_TREE;
12410
12411   /* Assume this is not a constructor, destructor, or type-conversion
12412      operator.  */
12413   if (ctor_dtor_or_conv_p)
12414     *ctor_dtor_or_conv_p = 0;
12415
12416   if (cp_parser_allow_gnu_extensions_p (parser))
12417     attributes = cp_parser_attributes_opt (parser);
12418
12419   /* Peek at the next token.  */
12420   token = cp_lexer_peek_token (parser->lexer);
12421
12422   /* Check for the ptr-operator production.  */
12423   cp_parser_parse_tentatively (parser);
12424   /* Parse the ptr-operator.  */
12425   code = cp_parser_ptr_operator (parser,
12426                                  &class_type,
12427                                  &cv_quals);
12428   /* If that worked, then we have a ptr-operator.  */
12429   if (cp_parser_parse_definitely (parser))
12430     {
12431       /* If a ptr-operator was found, then this declarator was not
12432          parenthesized.  */
12433       if (parenthesized_p)
12434         *parenthesized_p = true;
12435       /* The dependent declarator is optional if we are parsing an
12436          abstract-declarator.  */
12437       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12438         cp_parser_parse_tentatively (parser);
12439
12440       /* Parse the dependent declarator.  */
12441       declarator = cp_parser_declarator (parser, dcl_kind,
12442                                          /*ctor_dtor_or_conv_p=*/NULL,
12443                                          /*parenthesized_p=*/NULL,
12444                                          /*member_p=*/false);
12445
12446       /* If we are parsing an abstract-declarator, we must handle the
12447          case where the dependent declarator is absent.  */
12448       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12449           && !cp_parser_parse_definitely (parser))
12450         declarator = NULL;
12451
12452       declarator = cp_parser_make_indirect_declarator
12453         (code, class_type, cv_quals, declarator);
12454     }
12455   /* Everything else is a direct-declarator.  */
12456   else
12457     {
12458       if (parenthesized_p)
12459         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12460                                                    CPP_OPEN_PAREN);
12461       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12462                                                 ctor_dtor_or_conv_p,
12463                                                 member_p);
12464     }
12465
12466   if (attributes && declarator && declarator != cp_error_declarator)
12467     declarator->attributes = attributes;
12468
12469   return declarator;
12470 }
12471
12472 /* Parse a direct-declarator or direct-abstract-declarator.
12473
12474    direct-declarator:
12475      declarator-id
12476      direct-declarator ( parameter-declaration-clause )
12477        cv-qualifier-seq [opt]
12478        exception-specification [opt]
12479      direct-declarator [ constant-expression [opt] ]
12480      ( declarator )
12481
12482    direct-abstract-declarator:
12483      direct-abstract-declarator [opt]
12484        ( parameter-declaration-clause )
12485        cv-qualifier-seq [opt]
12486        exception-specification [opt]
12487      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12488      ( abstract-declarator )
12489
12490    Returns a representation of the declarator.  DCL_KIND is
12491    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12492    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12493    we are parsing a direct-declarator.  It is
12494    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12495    of ambiguity we prefer an abstract declarator, as per
12496    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12497    cp_parser_declarator.  */
12498
12499 static cp_declarator *
12500 cp_parser_direct_declarator (cp_parser* parser,
12501                              cp_parser_declarator_kind dcl_kind,
12502                              int* ctor_dtor_or_conv_p,
12503                              bool member_p)
12504 {
12505   cp_token *token;
12506   cp_declarator *declarator = NULL;
12507   tree scope = NULL_TREE;
12508   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12509   bool saved_in_declarator_p = parser->in_declarator_p;
12510   bool first = true;
12511   tree pushed_scope = NULL_TREE;
12512
12513   while (true)
12514     {
12515       /* Peek at the next token.  */
12516       token = cp_lexer_peek_token (parser->lexer);
12517       if (token->type == CPP_OPEN_PAREN)
12518         {
12519           /* This is either a parameter-declaration-clause, or a
12520              parenthesized declarator. When we know we are parsing a
12521              named declarator, it must be a parenthesized declarator
12522              if FIRST is true. For instance, `(int)' is a
12523              parameter-declaration-clause, with an omitted
12524              direct-abstract-declarator. But `((*))', is a
12525              parenthesized abstract declarator. Finally, when T is a
12526              template parameter `(T)' is a
12527              parameter-declaration-clause, and not a parenthesized
12528              named declarator.
12529
12530              We first try and parse a parameter-declaration-clause,
12531              and then try a nested declarator (if FIRST is true).
12532
12533              It is not an error for it not to be a
12534              parameter-declaration-clause, even when FIRST is
12535              false. Consider,
12536
12537                int i (int);
12538                int i (3);
12539
12540              The first is the declaration of a function while the
12541              second is a the definition of a variable, including its
12542              initializer.
12543
12544              Having seen only the parenthesis, we cannot know which of
12545              these two alternatives should be selected.  Even more
12546              complex are examples like:
12547
12548                int i (int (a));
12549                int i (int (3));
12550
12551              The former is a function-declaration; the latter is a
12552              variable initialization.
12553
12554              Thus again, we try a parameter-declaration-clause, and if
12555              that fails, we back out and return.  */
12556
12557           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12558             {
12559               cp_parameter_declarator *params;
12560               unsigned saved_num_template_parameter_lists;
12561
12562               /* In a member-declarator, the only valid interpretation
12563                  of a parenthesis is the start of a
12564                  parameter-declaration-clause.  (It is invalid to
12565                  initialize a static data member with a parenthesized
12566                  initializer; only the "=" form of initialization is
12567                  permitted.)  */
12568               if (!member_p)
12569                 cp_parser_parse_tentatively (parser);
12570
12571               /* Consume the `('.  */
12572               cp_lexer_consume_token (parser->lexer);
12573               if (first)
12574                 {
12575                   /* If this is going to be an abstract declarator, we're
12576                      in a declarator and we can't have default args.  */
12577                   parser->default_arg_ok_p = false;
12578                   parser->in_declarator_p = true;
12579                 }
12580
12581               /* Inside the function parameter list, surrounding
12582                  template-parameter-lists do not apply.  */
12583               saved_num_template_parameter_lists
12584                 = parser->num_template_parameter_lists;
12585               parser->num_template_parameter_lists = 0;
12586
12587               /* Parse the parameter-declaration-clause.  */
12588               params = cp_parser_parameter_declaration_clause (parser);
12589
12590               parser->num_template_parameter_lists
12591                 = saved_num_template_parameter_lists;
12592
12593               /* If all went well, parse the cv-qualifier-seq and the
12594                  exception-specification.  */
12595               if (member_p || cp_parser_parse_definitely (parser))
12596                 {
12597                   cp_cv_quals cv_quals;
12598                   tree exception_specification;
12599
12600                   if (ctor_dtor_or_conv_p)
12601                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12602                   first = false;
12603                   /* Consume the `)'.  */
12604                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12605
12606                   /* Parse the cv-qualifier-seq.  */
12607                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12608                   /* And the exception-specification.  */
12609                   exception_specification
12610                     = cp_parser_exception_specification_opt (parser);
12611
12612                   /* Create the function-declarator.  */
12613                   declarator = make_call_declarator (declarator,
12614                                                      params,
12615                                                      cv_quals,
12616                                                      exception_specification);
12617                   /* Any subsequent parameter lists are to do with
12618                      return type, so are not those of the declared
12619                      function.  */
12620                   parser->default_arg_ok_p = false;
12621
12622                   /* Repeat the main loop.  */
12623                   continue;
12624                 }
12625             }
12626
12627           /* If this is the first, we can try a parenthesized
12628              declarator.  */
12629           if (first)
12630             {
12631               bool saved_in_type_id_in_expr_p;
12632
12633               parser->default_arg_ok_p = saved_default_arg_ok_p;
12634               parser->in_declarator_p = saved_in_declarator_p;
12635
12636               /* Consume the `('.  */
12637               cp_lexer_consume_token (parser->lexer);
12638               /* Parse the nested declarator.  */
12639               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12640               parser->in_type_id_in_expr_p = true;
12641               declarator
12642                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12643                                         /*parenthesized_p=*/NULL,
12644                                         member_p);
12645               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12646               first = false;
12647               /* Expect a `)'.  */
12648               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12649                 declarator = cp_error_declarator;
12650               if (declarator == cp_error_declarator)
12651                 break;
12652
12653               goto handle_declarator;
12654             }
12655           /* Otherwise, we must be done.  */
12656           else
12657             break;
12658         }
12659       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12660                && token->type == CPP_OPEN_SQUARE)
12661         {
12662           /* Parse an array-declarator.  */
12663           tree bounds;
12664
12665           if (ctor_dtor_or_conv_p)
12666             *ctor_dtor_or_conv_p = 0;
12667
12668           first = false;
12669           parser->default_arg_ok_p = false;
12670           parser->in_declarator_p = true;
12671           /* Consume the `['.  */
12672           cp_lexer_consume_token (parser->lexer);
12673           /* Peek at the next token.  */
12674           token = cp_lexer_peek_token (parser->lexer);
12675           /* If the next token is `]', then there is no
12676              constant-expression.  */
12677           if (token->type != CPP_CLOSE_SQUARE)
12678             {
12679               bool non_constant_p;
12680
12681               bounds
12682                 = cp_parser_constant_expression (parser,
12683                                                  /*allow_non_constant=*/true,
12684                                                  &non_constant_p);
12685               if (!non_constant_p)
12686                 bounds = fold_non_dependent_expr (bounds);
12687               /* Normally, the array bound must be an integral constant
12688                  expression.  However, as an extension, we allow VLAs
12689                  in function scopes.  */
12690               else if (!parser->in_function_body)
12691                 {
12692                   error ("array bound is not an integer constant");
12693                   bounds = error_mark_node;
12694                 }
12695             }
12696           else
12697             bounds = NULL_TREE;
12698           /* Look for the closing `]'.  */
12699           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12700             {
12701               declarator = cp_error_declarator;
12702               break;
12703             }
12704
12705           declarator = make_array_declarator (declarator, bounds);
12706         }
12707       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12708         {
12709           tree qualifying_scope;
12710           tree unqualified_name;
12711           special_function_kind sfk;
12712           bool abstract_ok;
12713           bool pack_expansion_p = false;
12714
12715           /* Parse a declarator-id */
12716           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12717           if (abstract_ok)
12718             {
12719               cp_parser_parse_tentatively (parser);
12720
12721               /* If we see an ellipsis, we should be looking at a
12722                  parameter pack. */
12723               if (token->type == CPP_ELLIPSIS)
12724                 {
12725                   /* Consume the `...' */
12726                   cp_lexer_consume_token (parser->lexer);
12727
12728                   pack_expansion_p = true;
12729                 }
12730             }
12731
12732           unqualified_name
12733             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12734           qualifying_scope = parser->scope;
12735           if (abstract_ok)
12736             {
12737               bool okay = false;
12738
12739               if (!unqualified_name && pack_expansion_p)
12740                 {
12741                   /* Check whether an error occurred. */
12742                   okay = !cp_parser_error_occurred (parser);
12743
12744                   /* We already consumed the ellipsis to mark a
12745                      parameter pack, but we have no way to report it,
12746                      so abort the tentative parse. We will be exiting
12747                      immediately anyway. */
12748                   cp_parser_abort_tentative_parse (parser);
12749                 }
12750               else
12751                 okay = cp_parser_parse_definitely (parser);
12752
12753               if (!okay)
12754                 unqualified_name = error_mark_node;
12755               else if (unqualified_name
12756                        && (qualifying_scope
12757                            || (TREE_CODE (unqualified_name)
12758                                != IDENTIFIER_NODE)))
12759                 {
12760                   cp_parser_error (parser, "expected unqualified-id");
12761                   unqualified_name = error_mark_node;
12762                 }
12763             }
12764
12765           if (!unqualified_name)
12766             return NULL;
12767           if (unqualified_name == error_mark_node)
12768             {
12769               declarator = cp_error_declarator;
12770               pack_expansion_p = false;
12771               declarator->parameter_pack_p = false;
12772               break;
12773             }
12774
12775           if (qualifying_scope && at_namespace_scope_p ()
12776               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12777             {
12778               /* In the declaration of a member of a template class
12779                  outside of the class itself, the SCOPE will sometimes
12780                  be a TYPENAME_TYPE.  For example, given:
12781
12782                  template <typename T>
12783                  int S<T>::R::i = 3;
12784
12785                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12786                  this context, we must resolve S<T>::R to an ordinary
12787                  type, rather than a typename type.
12788
12789                  The reason we normally avoid resolving TYPENAME_TYPEs
12790                  is that a specialization of `S' might render
12791                  `S<T>::R' not a type.  However, if `S' is
12792                  specialized, then this `i' will not be used, so there
12793                  is no harm in resolving the types here.  */
12794               tree type;
12795
12796               /* Resolve the TYPENAME_TYPE.  */
12797               type = resolve_typename_type (qualifying_scope,
12798                                             /*only_current_p=*/false);
12799               /* If that failed, the declarator is invalid.  */
12800               if (TREE_CODE (type) == TYPENAME_TYPE)
12801                 error ("%<%T::%E%> is not a type",
12802                        TYPE_CONTEXT (qualifying_scope),
12803                        TYPE_IDENTIFIER (qualifying_scope));
12804               qualifying_scope = type;
12805             }
12806
12807           sfk = sfk_none;
12808
12809           if (unqualified_name)
12810             {
12811               tree class_type;
12812
12813               if (qualifying_scope
12814                   && CLASS_TYPE_P (qualifying_scope))
12815                 class_type = qualifying_scope;
12816               else
12817                 class_type = current_class_type;
12818
12819               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12820                 {
12821                   tree name_type = TREE_TYPE (unqualified_name);
12822                   if (class_type && same_type_p (name_type, class_type))
12823                     {
12824                       if (qualifying_scope
12825                           && CLASSTYPE_USE_TEMPLATE (name_type))
12826                         {
12827                           error ("invalid use of constructor as a template");
12828                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12829                                   "name the constructor in a qualified name",
12830                                   class_type,
12831                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12832                                   class_type, name_type);
12833                           declarator = cp_error_declarator;
12834                           break;
12835                         }
12836                       else
12837                         unqualified_name = constructor_name (class_type);
12838                     }
12839                   else
12840                     {
12841                       /* We do not attempt to print the declarator
12842                          here because we do not have enough
12843                          information about its original syntactic
12844                          form.  */
12845                       cp_parser_error (parser, "invalid declarator");
12846                       declarator = cp_error_declarator;
12847                       break;
12848                     }
12849                 }
12850
12851               if (class_type)
12852                 {
12853                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12854                     sfk = sfk_destructor;
12855                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12856                     sfk = sfk_conversion;
12857                   else if (/* There's no way to declare a constructor
12858                               for an anonymous type, even if the type
12859                               got a name for linkage purposes.  */
12860                            !TYPE_WAS_ANONYMOUS (class_type)
12861                            && constructor_name_p (unqualified_name,
12862                                                   class_type))
12863                     {
12864                       unqualified_name = constructor_name (class_type);
12865                       sfk = sfk_constructor;
12866                     }
12867
12868                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12869                     *ctor_dtor_or_conv_p = -1;
12870                 }
12871             }
12872           declarator = make_id_declarator (qualifying_scope,
12873                                            unqualified_name,
12874                                            sfk);
12875           declarator->id_loc = token->location;
12876           declarator->parameter_pack_p = pack_expansion_p;
12877
12878           if (pack_expansion_p)
12879             maybe_warn_variadic_templates ();
12880
12881         handle_declarator:;
12882           scope = get_scope_of_declarator (declarator);
12883           if (scope)
12884             /* Any names that appear after the declarator-id for a
12885                member are looked up in the containing scope.  */
12886             pushed_scope = push_scope (scope);
12887           parser->in_declarator_p = true;
12888           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12889               || (declarator && declarator->kind == cdk_id))
12890             /* Default args are only allowed on function
12891                declarations.  */
12892             parser->default_arg_ok_p = saved_default_arg_ok_p;
12893           else
12894             parser->default_arg_ok_p = false;
12895
12896           first = false;
12897         }
12898       /* We're done.  */
12899       else
12900         break;
12901     }
12902
12903   /* For an abstract declarator, we might wind up with nothing at this
12904      point.  That's an error; the declarator is not optional.  */
12905   if (!declarator)
12906     cp_parser_error (parser, "expected declarator");
12907
12908   /* If we entered a scope, we must exit it now.  */
12909   if (pushed_scope)
12910     pop_scope (pushed_scope);
12911
12912   parser->default_arg_ok_p = saved_default_arg_ok_p;
12913   parser->in_declarator_p = saved_in_declarator_p;
12914
12915   return declarator;
12916 }
12917
12918 /* Parse a ptr-operator.
12919
12920    ptr-operator:
12921      * cv-qualifier-seq [opt]
12922      &
12923      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12924
12925    GNU Extension:
12926
12927    ptr-operator:
12928      & cv-qualifier-seq [opt]
12929
12930    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12931    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12932    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12933    filled in with the TYPE containing the member.  *CV_QUALS is
12934    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12935    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12936    Note that the tree codes returned by this function have nothing
12937    to do with the types of trees that will be eventually be created
12938    to represent the pointer or reference type being parsed. They are
12939    just constants with suggestive names. */
12940 static enum tree_code
12941 cp_parser_ptr_operator (cp_parser* parser,
12942                         tree* type,
12943                         cp_cv_quals *cv_quals)
12944 {
12945   enum tree_code code = ERROR_MARK;
12946   cp_token *token;
12947
12948   /* Assume that it's not a pointer-to-member.  */
12949   *type = NULL_TREE;
12950   /* And that there are no cv-qualifiers.  */
12951   *cv_quals = TYPE_UNQUALIFIED;
12952
12953   /* Peek at the next token.  */
12954   token = cp_lexer_peek_token (parser->lexer);
12955
12956   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12957   if (token->type == CPP_MULT)
12958     code = INDIRECT_REF;
12959   else if (token->type == CPP_AND)
12960     code = ADDR_EXPR;
12961   else if ((cxx_dialect != cxx98) &&
12962            token->type == CPP_AND_AND) /* C++0x only */
12963     code = NON_LVALUE_EXPR;
12964
12965   if (code != ERROR_MARK)
12966     {
12967       /* Consume the `*', `&' or `&&'.  */
12968       cp_lexer_consume_token (parser->lexer);
12969
12970       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12971          `&', if we are allowing GNU extensions.  (The only qualifier
12972          that can legally appear after `&' is `restrict', but that is
12973          enforced during semantic analysis.  */
12974       if (code == INDIRECT_REF
12975           || cp_parser_allow_gnu_extensions_p (parser))
12976         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12977     }
12978   else
12979     {
12980       /* Try the pointer-to-member case.  */
12981       cp_parser_parse_tentatively (parser);
12982       /* Look for the optional `::' operator.  */
12983       cp_parser_global_scope_opt (parser,
12984                                   /*current_scope_valid_p=*/false);
12985       /* Look for the nested-name specifier.  */
12986       cp_parser_nested_name_specifier (parser,
12987                                        /*typename_keyword_p=*/false,
12988                                        /*check_dependency_p=*/true,
12989                                        /*type_p=*/false,
12990                                        /*is_declaration=*/false);
12991       /* If we found it, and the next token is a `*', then we are
12992          indeed looking at a pointer-to-member operator.  */
12993       if (!cp_parser_error_occurred (parser)
12994           && cp_parser_require (parser, CPP_MULT, "`*'"))
12995         {
12996           /* Indicate that the `*' operator was used.  */
12997           code = INDIRECT_REF;
12998
12999           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13000             error ("%qD is a namespace", parser->scope);
13001           else
13002             {
13003               /* The type of which the member is a member is given by the
13004                  current SCOPE.  */
13005               *type = parser->scope;
13006               /* The next name will not be qualified.  */
13007               parser->scope = NULL_TREE;
13008               parser->qualifying_scope = NULL_TREE;
13009               parser->object_scope = NULL_TREE;
13010               /* Look for the optional cv-qualifier-seq.  */
13011               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13012             }
13013         }
13014       /* If that didn't work we don't have a ptr-operator.  */
13015       if (!cp_parser_parse_definitely (parser))
13016         cp_parser_error (parser, "expected ptr-operator");
13017     }
13018
13019   return code;
13020 }
13021
13022 /* Parse an (optional) cv-qualifier-seq.
13023
13024    cv-qualifier-seq:
13025      cv-qualifier cv-qualifier-seq [opt]
13026
13027    cv-qualifier:
13028      const
13029      volatile
13030
13031    GNU Extension:
13032
13033    cv-qualifier:
13034      __restrict__
13035
13036    Returns a bitmask representing the cv-qualifiers.  */
13037
13038 static cp_cv_quals
13039 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13040 {
13041   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13042
13043   while (true)
13044     {
13045       cp_token *token;
13046       cp_cv_quals cv_qualifier;
13047
13048       /* Peek at the next token.  */
13049       token = cp_lexer_peek_token (parser->lexer);
13050       /* See if it's a cv-qualifier.  */
13051       switch (token->keyword)
13052         {
13053         case RID_CONST:
13054           cv_qualifier = TYPE_QUAL_CONST;
13055           break;
13056
13057         case RID_VOLATILE:
13058           cv_qualifier = TYPE_QUAL_VOLATILE;
13059           break;
13060
13061         case RID_RESTRICT:
13062           cv_qualifier = TYPE_QUAL_RESTRICT;
13063           break;
13064
13065         default:
13066           cv_qualifier = TYPE_UNQUALIFIED;
13067           break;
13068         }
13069
13070       if (!cv_qualifier)
13071         break;
13072
13073       if (cv_quals & cv_qualifier)
13074         {
13075           error ("duplicate cv-qualifier");
13076           cp_lexer_purge_token (parser->lexer);
13077         }
13078       else
13079         {
13080           cp_lexer_consume_token (parser->lexer);
13081           cv_quals |= cv_qualifier;
13082         }
13083     }
13084
13085   return cv_quals;
13086 }
13087
13088 /* Parse a declarator-id.
13089
13090    declarator-id:
13091      id-expression
13092      :: [opt] nested-name-specifier [opt] type-name
13093
13094    In the `id-expression' case, the value returned is as for
13095    cp_parser_id_expression if the id-expression was an unqualified-id.
13096    If the id-expression was a qualified-id, then a SCOPE_REF is
13097    returned.  The first operand is the scope (either a NAMESPACE_DECL
13098    or TREE_TYPE), but the second is still just a representation of an
13099    unqualified-id.  */
13100
13101 static tree
13102 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13103 {
13104   tree id;
13105   /* The expression must be an id-expression.  Assume that qualified
13106      names are the names of types so that:
13107
13108        template <class T>
13109        int S<T>::R::i = 3;
13110
13111      will work; we must treat `S<T>::R' as the name of a type.
13112      Similarly, assume that qualified names are templates, where
13113      required, so that:
13114
13115        template <class T>
13116        int S<T>::R<T>::i = 3;
13117
13118      will work, too.  */
13119   id = cp_parser_id_expression (parser,
13120                                 /*template_keyword_p=*/false,
13121                                 /*check_dependency_p=*/false,
13122                                 /*template_p=*/NULL,
13123                                 /*declarator_p=*/true,
13124                                 optional_p);
13125   if (id && BASELINK_P (id))
13126     id = BASELINK_FUNCTIONS (id);
13127   return id;
13128 }
13129
13130 /* Parse a type-id.
13131
13132    type-id:
13133      type-specifier-seq abstract-declarator [opt]
13134
13135    Returns the TYPE specified.  */
13136
13137 static tree
13138 cp_parser_type_id (cp_parser* parser)
13139 {
13140   cp_decl_specifier_seq type_specifier_seq;
13141   cp_declarator *abstract_declarator;
13142
13143   /* Parse the type-specifier-seq.  */
13144   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13145                                 &type_specifier_seq);
13146   if (type_specifier_seq.type == error_mark_node)
13147     return error_mark_node;
13148
13149   /* There might or might not be an abstract declarator.  */
13150   cp_parser_parse_tentatively (parser);
13151   /* Look for the declarator.  */
13152   abstract_declarator
13153     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13154                             /*parenthesized_p=*/NULL,
13155                             /*member_p=*/false);
13156   /* Check to see if there really was a declarator.  */
13157   if (!cp_parser_parse_definitely (parser))
13158     abstract_declarator = NULL;
13159
13160   return groktypename (&type_specifier_seq, abstract_declarator);
13161 }
13162
13163 /* Parse a type-specifier-seq.
13164
13165    type-specifier-seq:
13166      type-specifier type-specifier-seq [opt]
13167
13168    GNU extension:
13169
13170    type-specifier-seq:
13171      attributes type-specifier-seq [opt]
13172
13173    If IS_CONDITION is true, we are at the start of a "condition",
13174    e.g., we've just seen "if (".
13175
13176    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13177
13178 static void
13179 cp_parser_type_specifier_seq (cp_parser* parser,
13180                               bool is_condition,
13181                               cp_decl_specifier_seq *type_specifier_seq)
13182 {
13183   bool seen_type_specifier = false;
13184   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13185
13186   /* Clear the TYPE_SPECIFIER_SEQ.  */
13187   clear_decl_specs (type_specifier_seq);
13188
13189   /* Parse the type-specifiers and attributes.  */
13190   while (true)
13191     {
13192       tree type_specifier;
13193       bool is_cv_qualifier;
13194
13195       /* Check for attributes first.  */
13196       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13197         {
13198           type_specifier_seq->attributes =
13199             chainon (type_specifier_seq->attributes,
13200                      cp_parser_attributes_opt (parser));
13201           continue;
13202         }
13203
13204       /* Look for the type-specifier.  */
13205       type_specifier = cp_parser_type_specifier (parser,
13206                                                  flags,
13207                                                  type_specifier_seq,
13208                                                  /*is_declaration=*/false,
13209                                                  NULL,
13210                                                  &is_cv_qualifier);
13211       if (!type_specifier)
13212         {
13213           /* If the first type-specifier could not be found, this is not a
13214              type-specifier-seq at all.  */
13215           if (!seen_type_specifier)
13216             {
13217               cp_parser_error (parser, "expected type-specifier");
13218               type_specifier_seq->type = error_mark_node;
13219               return;
13220             }
13221           /* If subsequent type-specifiers could not be found, the
13222              type-specifier-seq is complete.  */
13223           break;
13224         }
13225
13226       seen_type_specifier = true;
13227       /* The standard says that a condition can be:
13228
13229             type-specifier-seq declarator = assignment-expression
13230
13231          However, given:
13232
13233            struct S {};
13234            if (int S = ...)
13235
13236          we should treat the "S" as a declarator, not as a
13237          type-specifier.  The standard doesn't say that explicitly for
13238          type-specifier-seq, but it does say that for
13239          decl-specifier-seq in an ordinary declaration.  Perhaps it
13240          would be clearer just to allow a decl-specifier-seq here, and
13241          then add a semantic restriction that if any decl-specifiers
13242          that are not type-specifiers appear, the program is invalid.  */
13243       if (is_condition && !is_cv_qualifier)
13244         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13245     }
13246
13247   cp_parser_check_decl_spec (type_specifier_seq);
13248 }
13249
13250 /* Parse a parameter-declaration-clause.
13251
13252    parameter-declaration-clause:
13253      parameter-declaration-list [opt] ... [opt]
13254      parameter-declaration-list , ...
13255
13256    Returns a representation for the parameter declarations.  A return
13257    value of NULL indicates a parameter-declaration-clause consisting
13258    only of an ellipsis.  */
13259
13260 static cp_parameter_declarator *
13261 cp_parser_parameter_declaration_clause (cp_parser* parser)
13262 {
13263   cp_parameter_declarator *parameters;
13264   cp_token *token;
13265   bool ellipsis_p;
13266   bool is_error;
13267
13268   /* Peek at the next token.  */
13269   token = cp_lexer_peek_token (parser->lexer);
13270   /* Check for trivial parameter-declaration-clauses.  */
13271   if (token->type == CPP_ELLIPSIS)
13272     {
13273       /* Consume the `...' token.  */
13274       cp_lexer_consume_token (parser->lexer);
13275       return NULL;
13276     }
13277   else if (token->type == CPP_CLOSE_PAREN)
13278     /* There are no parameters.  */
13279     {
13280 #ifndef NO_IMPLICIT_EXTERN_C
13281       if (in_system_header && current_class_type == NULL
13282           && current_lang_name == lang_name_c)
13283         return NULL;
13284       else
13285 #endif
13286         return no_parameters;
13287     }
13288   /* Check for `(void)', too, which is a special case.  */
13289   else if (token->keyword == RID_VOID
13290            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13291                == CPP_CLOSE_PAREN))
13292     {
13293       /* Consume the `void' token.  */
13294       cp_lexer_consume_token (parser->lexer);
13295       /* There are no parameters.  */
13296       return no_parameters;
13297     }
13298
13299   /* Parse the parameter-declaration-list.  */
13300   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13301   /* If a parse error occurred while parsing the
13302      parameter-declaration-list, then the entire
13303      parameter-declaration-clause is erroneous.  */
13304   if (is_error)
13305     return NULL;
13306
13307   /* Peek at the next token.  */
13308   token = cp_lexer_peek_token (parser->lexer);
13309   /* If it's a `,', the clause should terminate with an ellipsis.  */
13310   if (token->type == CPP_COMMA)
13311     {
13312       /* Consume the `,'.  */
13313       cp_lexer_consume_token (parser->lexer);
13314       /* Expect an ellipsis.  */
13315       ellipsis_p
13316         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13317     }
13318   /* It might also be `...' if the optional trailing `,' was
13319      omitted.  */
13320   else if (token->type == CPP_ELLIPSIS)
13321     {
13322       /* Consume the `...' token.  */
13323       cp_lexer_consume_token (parser->lexer);
13324       /* And remember that we saw it.  */
13325       ellipsis_p = true;
13326     }
13327   else
13328     ellipsis_p = false;
13329
13330   /* Finish the parameter list.  */
13331   if (parameters && ellipsis_p)
13332     parameters->ellipsis_p = true;
13333
13334   return parameters;
13335 }
13336
13337 /* Parse a parameter-declaration-list.
13338
13339    parameter-declaration-list:
13340      parameter-declaration
13341      parameter-declaration-list , parameter-declaration
13342
13343    Returns a representation of the parameter-declaration-list, as for
13344    cp_parser_parameter_declaration_clause.  However, the
13345    `void_list_node' is never appended to the list.  Upon return,
13346    *IS_ERROR will be true iff an error occurred.  */
13347
13348 static cp_parameter_declarator *
13349 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13350 {
13351   cp_parameter_declarator *parameters = NULL;
13352   cp_parameter_declarator **tail = &parameters;
13353   bool saved_in_unbraced_linkage_specification_p;
13354
13355   /* Assume all will go well.  */
13356   *is_error = false;
13357   /* The special considerations that apply to a function within an
13358      unbraced linkage specifications do not apply to the parameters
13359      to the function.  */
13360   saved_in_unbraced_linkage_specification_p 
13361     = parser->in_unbraced_linkage_specification_p;
13362   parser->in_unbraced_linkage_specification_p = false;
13363
13364   /* Look for more parameters.  */
13365   while (true)
13366     {
13367       cp_parameter_declarator *parameter;
13368       bool parenthesized_p;
13369       /* Parse the parameter.  */
13370       parameter
13371         = cp_parser_parameter_declaration (parser,
13372                                            /*template_parm_p=*/false,
13373                                            &parenthesized_p);
13374
13375       /* If a parse error occurred parsing the parameter declaration,
13376          then the entire parameter-declaration-list is erroneous.  */
13377       if (!parameter)
13378         {
13379           *is_error = true;
13380           parameters = NULL;
13381           break;
13382         }
13383       /* Add the new parameter to the list.  */
13384       *tail = parameter;
13385       tail = &parameter->next;
13386
13387       /* Peek at the next token.  */
13388       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13389           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13390           /* These are for Objective-C++ */
13391           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13392           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13393         /* The parameter-declaration-list is complete.  */
13394         break;
13395       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13396         {
13397           cp_token *token;
13398
13399           /* Peek at the next token.  */
13400           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13401           /* If it's an ellipsis, then the list is complete.  */
13402           if (token->type == CPP_ELLIPSIS)
13403             break;
13404           /* Otherwise, there must be more parameters.  Consume the
13405              `,'.  */
13406           cp_lexer_consume_token (parser->lexer);
13407           /* When parsing something like:
13408
13409                 int i(float f, double d)
13410
13411              we can tell after seeing the declaration for "f" that we
13412              are not looking at an initialization of a variable "i",
13413              but rather at the declaration of a function "i".
13414
13415              Due to the fact that the parsing of template arguments
13416              (as specified to a template-id) requires backtracking we
13417              cannot use this technique when inside a template argument
13418              list.  */
13419           if (!parser->in_template_argument_list_p
13420               && !parser->in_type_id_in_expr_p
13421               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13422               /* However, a parameter-declaration of the form
13423                  "foat(f)" (which is a valid declaration of a
13424                  parameter "f") can also be interpreted as an
13425                  expression (the conversion of "f" to "float").  */
13426               && !parenthesized_p)
13427             cp_parser_commit_to_tentative_parse (parser);
13428         }
13429       else
13430         {
13431           cp_parser_error (parser, "expected %<,%> or %<...%>");
13432           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13433             cp_parser_skip_to_closing_parenthesis (parser,
13434                                                    /*recovering=*/true,
13435                                                    /*or_comma=*/false,
13436                                                    /*consume_paren=*/false);
13437           break;
13438         }
13439     }
13440
13441   parser->in_unbraced_linkage_specification_p
13442     = saved_in_unbraced_linkage_specification_p;
13443
13444   return parameters;
13445 }
13446
13447 /* Parse a parameter declaration.
13448
13449    parameter-declaration:
13450      decl-specifier-seq ... [opt] declarator
13451      decl-specifier-seq declarator = assignment-expression
13452      decl-specifier-seq ... [opt] abstract-declarator [opt]
13453      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13454
13455    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13456    declares a template parameter.  (In that case, a non-nested `>'
13457    token encountered during the parsing of the assignment-expression
13458    is not interpreted as a greater-than operator.)
13459
13460    Returns a representation of the parameter, or NULL if an error
13461    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13462    true iff the declarator is of the form "(p)".  */
13463
13464 static cp_parameter_declarator *
13465 cp_parser_parameter_declaration (cp_parser *parser,
13466                                  bool template_parm_p,
13467                                  bool *parenthesized_p)
13468 {
13469   int declares_class_or_enum;
13470   bool greater_than_is_operator_p;
13471   cp_decl_specifier_seq decl_specifiers;
13472   cp_declarator *declarator;
13473   tree default_argument;
13474   cp_token *token;
13475   const char *saved_message;
13476
13477   /* In a template parameter, `>' is not an operator.
13478
13479      [temp.param]
13480
13481      When parsing a default template-argument for a non-type
13482      template-parameter, the first non-nested `>' is taken as the end
13483      of the template parameter-list rather than a greater-than
13484      operator.  */
13485   greater_than_is_operator_p = !template_parm_p;
13486
13487   /* Type definitions may not appear in parameter types.  */
13488   saved_message = parser->type_definition_forbidden_message;
13489   parser->type_definition_forbidden_message
13490     = "types may not be defined in parameter types";
13491
13492   /* Parse the declaration-specifiers.  */
13493   cp_parser_decl_specifier_seq (parser,
13494                                 CP_PARSER_FLAGS_NONE,
13495                                 &decl_specifiers,
13496                                 &declares_class_or_enum);
13497   /* If an error occurred, there's no reason to attempt to parse the
13498      rest of the declaration.  */
13499   if (cp_parser_error_occurred (parser))
13500     {
13501       parser->type_definition_forbidden_message = saved_message;
13502       return NULL;
13503     }
13504
13505   /* Peek at the next token.  */
13506   token = cp_lexer_peek_token (parser->lexer);
13507
13508   /* If the next token is a `)', `,', `=', `>', or `...', then there
13509      is no declarator. However, when variadic templates are enabled,
13510      there may be a declarator following `...'.  */
13511   if (token->type == CPP_CLOSE_PAREN
13512       || token->type == CPP_COMMA
13513       || token->type == CPP_EQ
13514       || token->type == CPP_GREATER)
13515     {
13516       declarator = NULL;
13517       if (parenthesized_p)
13518         *parenthesized_p = false;
13519     }
13520   /* Otherwise, there should be a declarator.  */
13521   else
13522     {
13523       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13524       parser->default_arg_ok_p = false;
13525
13526       /* After seeing a decl-specifier-seq, if the next token is not a
13527          "(", there is no possibility that the code is a valid
13528          expression.  Therefore, if parsing tentatively, we commit at
13529          this point.  */
13530       if (!parser->in_template_argument_list_p
13531           /* In an expression context, having seen:
13532
13533                (int((char ...
13534
13535              we cannot be sure whether we are looking at a
13536              function-type (taking a "char" as a parameter) or a cast
13537              of some object of type "char" to "int".  */
13538           && !parser->in_type_id_in_expr_p
13539           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13540           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13541         cp_parser_commit_to_tentative_parse (parser);
13542       /* Parse the declarator.  */
13543       declarator = cp_parser_declarator (parser,
13544                                          CP_PARSER_DECLARATOR_EITHER,
13545                                          /*ctor_dtor_or_conv_p=*/NULL,
13546                                          parenthesized_p,
13547                                          /*member_p=*/false);
13548       parser->default_arg_ok_p = saved_default_arg_ok_p;
13549       /* After the declarator, allow more attributes.  */
13550       decl_specifiers.attributes
13551         = chainon (decl_specifiers.attributes,
13552                    cp_parser_attributes_opt (parser));
13553     }
13554
13555   /* If the next token is an ellipsis, and we have not seen a
13556      declarator name, and the type of the declarator contains parameter
13557      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13558      a parameter pack expansion expression. Otherwise, leave the
13559      ellipsis for a C-style variadic function. */
13560   token = cp_lexer_peek_token (parser->lexer);
13561   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13562     {
13563       tree type = decl_specifiers.type;
13564
13565       if (type && DECL_P (type))
13566         type = TREE_TYPE (type);
13567
13568       if (type
13569           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13570           && declarator_can_be_parameter_pack (declarator)
13571           && (!declarator || !declarator->parameter_pack_p)
13572           && uses_parameter_packs (type))
13573         {
13574           /* Consume the `...'. */
13575           cp_lexer_consume_token (parser->lexer);
13576           maybe_warn_variadic_templates ();
13577           
13578           /* Build a pack expansion type */
13579           if (declarator)
13580             declarator->parameter_pack_p = true;
13581           else
13582             decl_specifiers.type = make_pack_expansion (type);
13583         }
13584     }
13585
13586   /* The restriction on defining new types applies only to the type
13587      of the parameter, not to the default argument.  */
13588   parser->type_definition_forbidden_message = saved_message;
13589
13590   /* If the next token is `=', then process a default argument.  */
13591   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13592     {
13593       /* Consume the `='.  */
13594       cp_lexer_consume_token (parser->lexer);
13595
13596       /* If we are defining a class, then the tokens that make up the
13597          default argument must be saved and processed later.  */
13598       if (!template_parm_p && at_class_scope_p ()
13599           && TYPE_BEING_DEFINED (current_class_type))
13600         {
13601           unsigned depth = 0;
13602           cp_token *first_token;
13603           cp_token *token;
13604
13605           /* Add tokens until we have processed the entire default
13606              argument.  We add the range [first_token, token).  */
13607           first_token = cp_lexer_peek_token (parser->lexer);
13608           while (true)
13609             {
13610               bool done = false;
13611
13612               /* Peek at the next token.  */
13613               token = cp_lexer_peek_token (parser->lexer);
13614               /* What we do depends on what token we have.  */
13615               switch (token->type)
13616                 {
13617                   /* In valid code, a default argument must be
13618                      immediately followed by a `,' `)', or `...'.  */
13619                 case CPP_COMMA:
13620                 case CPP_CLOSE_PAREN:
13621                 case CPP_ELLIPSIS:
13622                   /* If we run into a non-nested `;', `}', or `]',
13623                      then the code is invalid -- but the default
13624                      argument is certainly over.  */
13625                 case CPP_SEMICOLON:
13626                 case CPP_CLOSE_BRACE:
13627                 case CPP_CLOSE_SQUARE:
13628                   if (depth == 0)
13629                     done = true;
13630                   /* Update DEPTH, if necessary.  */
13631                   else if (token->type == CPP_CLOSE_PAREN
13632                            || token->type == CPP_CLOSE_BRACE
13633                            || token->type == CPP_CLOSE_SQUARE)
13634                     --depth;
13635                   break;
13636
13637                 case CPP_OPEN_PAREN:
13638                 case CPP_OPEN_SQUARE:
13639                 case CPP_OPEN_BRACE:
13640                   ++depth;
13641                   break;
13642
13643                 case CPP_RSHIFT:
13644                   if (cxx_dialect == cxx98)
13645                     break;
13646                   /* Fall through for C++0x, which treats the `>>'
13647                      operator like two `>' tokens in certain
13648                      cases.  */
13649
13650                 case CPP_GREATER:
13651                   /* If we see a non-nested `>', and `>' is not an
13652                      operator, then it marks the end of the default
13653                      argument.  */
13654                   if (!depth && !greater_than_is_operator_p)
13655                     done = true;
13656                   break;
13657
13658                   /* If we run out of tokens, issue an error message.  */
13659                 case CPP_EOF:
13660                 case CPP_PRAGMA_EOL:
13661                   error ("file ends in default argument");
13662                   done = true;
13663                   break;
13664
13665                 case CPP_NAME:
13666                 case CPP_SCOPE:
13667                   /* In these cases, we should look for template-ids.
13668                      For example, if the default argument is
13669                      `X<int, double>()', we need to do name lookup to
13670                      figure out whether or not `X' is a template; if
13671                      so, the `,' does not end the default argument.
13672
13673                      That is not yet done.  */
13674                   break;
13675
13676                 default:
13677                   break;
13678                 }
13679
13680               /* If we've reached the end, stop.  */
13681               if (done)
13682                 break;
13683
13684               /* Add the token to the token block.  */
13685               token = cp_lexer_consume_token (parser->lexer);
13686             }
13687
13688           /* Create a DEFAULT_ARG to represent the unparsed default
13689              argument.  */
13690           default_argument = make_node (DEFAULT_ARG);
13691           DEFARG_TOKENS (default_argument)
13692             = cp_token_cache_new (first_token, token);
13693           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13694         }
13695       /* Outside of a class definition, we can just parse the
13696          assignment-expression.  */
13697       else
13698         default_argument 
13699           = cp_parser_default_argument (parser, template_parm_p);
13700
13701       if (!parser->default_arg_ok_p)
13702         {
13703           if (!flag_pedantic_errors)
13704             warning (0, "deprecated use of default argument for parameter of non-function");
13705           else
13706             {
13707               error ("default arguments are only permitted for function parameters");
13708               default_argument = NULL_TREE;
13709             }
13710         }
13711       else if ((declarator && declarator->parameter_pack_p)
13712                || (decl_specifiers.type
13713                    && PACK_EXPANSION_P (decl_specifiers.type)))
13714         {
13715           const char* kind = template_parm_p? "template " : "";
13716           
13717           /* Find the name of the parameter pack.  */     
13718           cp_declarator *id_declarator = declarator;
13719           while (id_declarator && id_declarator->kind != cdk_id)
13720             id_declarator = id_declarator->declarator;
13721           
13722           if (id_declarator && id_declarator->kind == cdk_id)
13723             error ("%sparameter pack %qD cannot have a default argument",
13724                    kind, id_declarator->u.id.unqualified_name);
13725           else
13726             error ("%sparameter pack cannot have a default argument",
13727                    kind);
13728           
13729           default_argument = NULL_TREE;
13730         }
13731     }
13732   else
13733     default_argument = NULL_TREE;
13734
13735   return make_parameter_declarator (&decl_specifiers,
13736                                     declarator,
13737                                     default_argument);
13738 }
13739
13740 /* Parse a default argument and return it.
13741
13742    TEMPLATE_PARM_P is true if this is a default argument for a
13743    non-type template parameter.  */
13744 static tree
13745 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13746 {
13747   tree default_argument = NULL_TREE;
13748   bool saved_greater_than_is_operator_p;
13749   bool saved_local_variables_forbidden_p;
13750
13751   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13752      set correctly.  */
13753   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13754   parser->greater_than_is_operator_p = !template_parm_p;
13755   /* Local variable names (and the `this' keyword) may not
13756      appear in a default argument.  */
13757   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13758   parser->local_variables_forbidden_p = true;
13759   /* The default argument expression may cause implicitly
13760      defined member functions to be synthesized, which will
13761      result in garbage collection.  We must treat this
13762      situation as if we were within the body of function so as
13763      to avoid collecting live data on the stack.  */
13764   ++function_depth;
13765   /* Parse the assignment-expression.  */
13766   if (template_parm_p)
13767     push_deferring_access_checks (dk_no_deferred);
13768   default_argument
13769     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13770   if (template_parm_p)
13771     pop_deferring_access_checks ();
13772   /* Restore saved state.  */
13773   --function_depth;
13774   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13775   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13776
13777   return default_argument;
13778 }
13779
13780 /* Parse a function-body.
13781
13782    function-body:
13783      compound_statement  */
13784
13785 static void
13786 cp_parser_function_body (cp_parser *parser)
13787 {
13788   cp_parser_compound_statement (parser, NULL, false);
13789 }
13790
13791 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13792    true if a ctor-initializer was present.  */
13793
13794 static bool
13795 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13796 {
13797   tree body;
13798   bool ctor_initializer_p;
13799
13800   /* Begin the function body.  */
13801   body = begin_function_body ();
13802   /* Parse the optional ctor-initializer.  */
13803   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13804   /* Parse the function-body.  */
13805   cp_parser_function_body (parser);
13806   /* Finish the function body.  */
13807   finish_function_body (body);
13808
13809   return ctor_initializer_p;
13810 }
13811
13812 /* Parse an initializer.
13813
13814    initializer:
13815      = initializer-clause
13816      ( expression-list )
13817
13818    Returns an expression representing the initializer.  If no
13819    initializer is present, NULL_TREE is returned.
13820
13821    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13822    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13823    set to FALSE if there is no initializer present.  If there is an
13824    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13825    is set to true; otherwise it is set to false.  */
13826
13827 static tree
13828 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13829                        bool* non_constant_p)
13830 {
13831   cp_token *token;
13832   tree init;
13833
13834   /* Peek at the next token.  */
13835   token = cp_lexer_peek_token (parser->lexer);
13836
13837   /* Let our caller know whether or not this initializer was
13838      parenthesized.  */
13839   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13840   /* Assume that the initializer is constant.  */
13841   *non_constant_p = false;
13842
13843   if (token->type == CPP_EQ)
13844     {
13845       /* Consume the `='.  */
13846       cp_lexer_consume_token (parser->lexer);
13847       /* Parse the initializer-clause.  */
13848       init = cp_parser_initializer_clause (parser, non_constant_p);
13849     }
13850   else if (token->type == CPP_OPEN_PAREN)
13851     init = cp_parser_parenthesized_expression_list (parser, false,
13852                                                     /*cast_p=*/false,
13853                                                     /*allow_expansion_p=*/true,
13854                                                     non_constant_p);
13855   else
13856     {
13857       /* Anything else is an error.  */
13858       cp_parser_error (parser, "expected initializer");
13859       init = error_mark_node;
13860     }
13861
13862   return init;
13863 }
13864
13865 /* Parse an initializer-clause.
13866
13867    initializer-clause:
13868      assignment-expression
13869      { initializer-list , [opt] }
13870      { }
13871
13872    Returns an expression representing the initializer.
13873
13874    If the `assignment-expression' production is used the value
13875    returned is simply a representation for the expression.
13876
13877    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13878    the elements of the initializer-list (or NULL, if the last
13879    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13880    NULL_TREE.  There is no way to detect whether or not the optional
13881    trailing `,' was provided.  NON_CONSTANT_P is as for
13882    cp_parser_initializer.  */
13883
13884 static tree
13885 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13886 {
13887   tree initializer;
13888
13889   /* Assume the expression is constant.  */
13890   *non_constant_p = false;
13891
13892   /* If it is not a `{', then we are looking at an
13893      assignment-expression.  */
13894   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13895     {
13896       initializer
13897         = cp_parser_constant_expression (parser,
13898                                         /*allow_non_constant_p=*/true,
13899                                         non_constant_p);
13900       if (!*non_constant_p)
13901         initializer = fold_non_dependent_expr (initializer);
13902     }
13903   else
13904     {
13905       /* Consume the `{' token.  */
13906       cp_lexer_consume_token (parser->lexer);
13907       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13908       initializer = make_node (CONSTRUCTOR);
13909       /* If it's not a `}', then there is a non-trivial initializer.  */
13910       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13911         {
13912           /* Parse the initializer list.  */
13913           CONSTRUCTOR_ELTS (initializer)
13914             = cp_parser_initializer_list (parser, non_constant_p);
13915           /* A trailing `,' token is allowed.  */
13916           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13917             cp_lexer_consume_token (parser->lexer);
13918         }
13919       /* Now, there should be a trailing `}'.  */
13920       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13921     }
13922
13923   return initializer;
13924 }
13925
13926 /* Parse an initializer-list.
13927
13928    initializer-list:
13929      initializer-clause ... [opt]
13930      initializer-list , initializer-clause ... [opt]
13931
13932    GNU Extension:
13933
13934    initializer-list:
13935      identifier : initializer-clause
13936      initializer-list, identifier : initializer-clause
13937
13938    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13939    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13940    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13941    as for cp_parser_initializer.  */
13942
13943 static VEC(constructor_elt,gc) *
13944 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13945 {
13946   VEC(constructor_elt,gc) *v = NULL;
13947
13948   /* Assume all of the expressions are constant.  */
13949   *non_constant_p = false;
13950
13951   /* Parse the rest of the list.  */
13952   while (true)
13953     {
13954       cp_token *token;
13955       tree identifier;
13956       tree initializer;
13957       bool clause_non_constant_p;
13958
13959       /* If the next token is an identifier and the following one is a
13960          colon, we are looking at the GNU designated-initializer
13961          syntax.  */
13962       if (cp_parser_allow_gnu_extensions_p (parser)
13963           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13964           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13965         {
13966           /* Warn the user that they are using an extension.  */
13967           if (pedantic)
13968             pedwarn ("ISO C++ does not allow designated initializers");
13969           /* Consume the identifier.  */
13970           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13971           /* Consume the `:'.  */
13972           cp_lexer_consume_token (parser->lexer);
13973         }
13974       else
13975         identifier = NULL_TREE;
13976
13977       /* Parse the initializer.  */
13978       initializer = cp_parser_initializer_clause (parser,
13979                                                   &clause_non_constant_p);
13980       /* If any clause is non-constant, so is the entire initializer.  */
13981       if (clause_non_constant_p)
13982         *non_constant_p = true;
13983
13984       /* If we have an ellipsis, this is an initializer pack
13985          expansion.  */
13986       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13987         {
13988           /* Consume the `...'.  */
13989           cp_lexer_consume_token (parser->lexer);
13990
13991           /* Turn the initializer into an initializer expansion.  */
13992           initializer = make_pack_expansion (initializer);
13993         }
13994
13995       /* Add it to the vector.  */
13996       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13997
13998       /* If the next token is not a comma, we have reached the end of
13999          the list.  */
14000       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14001         break;
14002
14003       /* Peek at the next token.  */
14004       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14005       /* If the next token is a `}', then we're still done.  An
14006          initializer-clause can have a trailing `,' after the
14007          initializer-list and before the closing `}'.  */
14008       if (token->type == CPP_CLOSE_BRACE)
14009         break;
14010
14011       /* Consume the `,' token.  */
14012       cp_lexer_consume_token (parser->lexer);
14013     }
14014
14015   return v;
14016 }
14017
14018 /* Classes [gram.class] */
14019
14020 /* Parse a class-name.
14021
14022    class-name:
14023      identifier
14024      template-id
14025
14026    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14027    to indicate that names looked up in dependent types should be
14028    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14029    keyword has been used to indicate that the name that appears next
14030    is a template.  TAG_TYPE indicates the explicit tag given before
14031    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14032    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14033    is the class being defined in a class-head.
14034
14035    Returns the TYPE_DECL representing the class.  */
14036
14037 static tree
14038 cp_parser_class_name (cp_parser *parser,
14039                       bool typename_keyword_p,
14040                       bool template_keyword_p,
14041                       enum tag_types tag_type,
14042                       bool check_dependency_p,
14043                       bool class_head_p,
14044                       bool is_declaration)
14045 {
14046   tree decl;
14047   tree scope;
14048   bool typename_p;
14049   cp_token *token;
14050
14051   /* All class-names start with an identifier.  */
14052   token = cp_lexer_peek_token (parser->lexer);
14053   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14054     {
14055       cp_parser_error (parser, "expected class-name");
14056       return error_mark_node;
14057     }
14058
14059   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14060      to a template-id, so we save it here.  */
14061   scope = parser->scope;
14062   if (scope == error_mark_node)
14063     return error_mark_node;
14064
14065   /* Any name names a type if we're following the `typename' keyword
14066      in a qualified name where the enclosing scope is type-dependent.  */
14067   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14068                 && dependent_type_p (scope));
14069   /* Handle the common case (an identifier, but not a template-id)
14070      efficiently.  */
14071   if (token->type == CPP_NAME
14072       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14073     {
14074       cp_token *identifier_token;
14075       tree identifier;
14076       bool ambiguous_p;
14077
14078       /* Look for the identifier.  */
14079       identifier_token = cp_lexer_peek_token (parser->lexer);
14080       ambiguous_p = identifier_token->ambiguous_p;
14081       identifier = cp_parser_identifier (parser);
14082       /* If the next token isn't an identifier, we are certainly not
14083          looking at a class-name.  */
14084       if (identifier == error_mark_node)
14085         decl = error_mark_node;
14086       /* If we know this is a type-name, there's no need to look it
14087          up.  */
14088       else if (typename_p)
14089         decl = identifier;
14090       else
14091         {
14092           tree ambiguous_decls;
14093           /* If we already know that this lookup is ambiguous, then
14094              we've already issued an error message; there's no reason
14095              to check again.  */
14096           if (ambiguous_p)
14097             {
14098               cp_parser_simulate_error (parser);
14099               return error_mark_node;
14100             }
14101           /* If the next token is a `::', then the name must be a type
14102              name.
14103
14104              [basic.lookup.qual]
14105
14106              During the lookup for a name preceding the :: scope
14107              resolution operator, object, function, and enumerator
14108              names are ignored.  */
14109           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14110             tag_type = typename_type;
14111           /* Look up the name.  */
14112           decl = cp_parser_lookup_name (parser, identifier,
14113                                         tag_type,
14114                                         /*is_template=*/false,
14115                                         /*is_namespace=*/false,
14116                                         check_dependency_p,
14117                                         &ambiguous_decls);
14118           if (ambiguous_decls)
14119             {
14120               error ("reference to %qD is ambiguous", identifier);
14121               print_candidates (ambiguous_decls);
14122               if (cp_parser_parsing_tentatively (parser))
14123                 {
14124                   identifier_token->ambiguous_p = true;
14125                   cp_parser_simulate_error (parser);
14126                 }
14127               return error_mark_node;
14128             }
14129         }
14130     }
14131   else
14132     {
14133       /* Try a template-id.  */
14134       decl = cp_parser_template_id (parser, template_keyword_p,
14135                                     check_dependency_p,
14136                                     is_declaration);
14137       if (decl == error_mark_node)
14138         return error_mark_node;
14139     }
14140
14141   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14142
14143   /* If this is a typename, create a TYPENAME_TYPE.  */
14144   if (typename_p && decl != error_mark_node)
14145     {
14146       decl = make_typename_type (scope, decl, typename_type,
14147                                  /*complain=*/tf_error);
14148       if (decl != error_mark_node)
14149         decl = TYPE_NAME (decl);
14150     }
14151
14152   /* Check to see that it is really the name of a class.  */
14153   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14154       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14155       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14156     /* Situations like this:
14157
14158          template <typename T> struct A {
14159            typename T::template X<int>::I i;
14160          };
14161
14162        are problematic.  Is `T::template X<int>' a class-name?  The
14163        standard does not seem to be definitive, but there is no other
14164        valid interpretation of the following `::'.  Therefore, those
14165        names are considered class-names.  */
14166     {
14167       decl = make_typename_type (scope, decl, tag_type, tf_error);
14168       if (decl != error_mark_node)
14169         decl = TYPE_NAME (decl);
14170     }
14171   else if (TREE_CODE (decl) != TYPE_DECL
14172            || TREE_TYPE (decl) == error_mark_node
14173            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14174     decl = error_mark_node;
14175
14176   if (decl == error_mark_node)
14177     cp_parser_error (parser, "expected class-name");
14178
14179   return decl;
14180 }
14181
14182 /* Parse a class-specifier.
14183
14184    class-specifier:
14185      class-head { member-specification [opt] }
14186
14187    Returns the TREE_TYPE representing the class.  */
14188
14189 static tree
14190 cp_parser_class_specifier (cp_parser* parser)
14191 {
14192   cp_token *token;
14193   tree type;
14194   tree attributes = NULL_TREE;
14195   int has_trailing_semicolon;
14196   bool nested_name_specifier_p;
14197   unsigned saved_num_template_parameter_lists;
14198   bool saved_in_function_body;
14199   tree old_scope = NULL_TREE;
14200   tree scope = NULL_TREE;
14201   tree bases;
14202
14203   push_deferring_access_checks (dk_no_deferred);
14204
14205   /* Parse the class-head.  */
14206   type = cp_parser_class_head (parser,
14207                                &nested_name_specifier_p,
14208                                &attributes,
14209                                &bases);
14210   /* If the class-head was a semantic disaster, skip the entire body
14211      of the class.  */
14212   if (!type)
14213     {
14214       cp_parser_skip_to_end_of_block_or_statement (parser);
14215       pop_deferring_access_checks ();
14216       return error_mark_node;
14217     }
14218
14219   /* Look for the `{'.  */
14220   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14221     {
14222       pop_deferring_access_checks ();
14223       return error_mark_node;
14224     }
14225
14226   /* Process the base classes. If they're invalid, skip the 
14227      entire class body.  */
14228   if (!xref_basetypes (type, bases))
14229     {
14230       /* Consuming the closing brace yields better error messages
14231          later on.  */
14232       if (cp_parser_skip_to_closing_brace (parser))
14233         cp_lexer_consume_token (parser->lexer);
14234       pop_deferring_access_checks ();
14235       return error_mark_node;
14236     }
14237
14238   /* Issue an error message if type-definitions are forbidden here.  */
14239   cp_parser_check_type_definition (parser);
14240   /* Remember that we are defining one more class.  */
14241   ++parser->num_classes_being_defined;
14242   /* Inside the class, surrounding template-parameter-lists do not
14243      apply.  */
14244   saved_num_template_parameter_lists
14245     = parser->num_template_parameter_lists;
14246   parser->num_template_parameter_lists = 0;
14247   /* We are not in a function body.  */
14248   saved_in_function_body = parser->in_function_body;
14249   parser->in_function_body = false;
14250
14251   /* Start the class.  */
14252   if (nested_name_specifier_p)
14253     {
14254       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14255       old_scope = push_inner_scope (scope);
14256     }
14257   type = begin_class_definition (type, attributes);
14258
14259   if (type == error_mark_node)
14260     /* If the type is erroneous, skip the entire body of the class.  */
14261     cp_parser_skip_to_closing_brace (parser);
14262   else
14263     /* Parse the member-specification.  */
14264     cp_parser_member_specification_opt (parser);
14265
14266   /* Look for the trailing `}'.  */
14267   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14268   /* We get better error messages by noticing a common problem: a
14269      missing trailing `;'.  */
14270   token = cp_lexer_peek_token (parser->lexer);
14271   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14272   /* Look for trailing attributes to apply to this class.  */
14273   if (cp_parser_allow_gnu_extensions_p (parser))
14274     attributes = cp_parser_attributes_opt (parser);
14275   if (type != error_mark_node)
14276     type = finish_struct (type, attributes);
14277   if (nested_name_specifier_p)
14278     pop_inner_scope (old_scope, scope);
14279   /* If this class is not itself within the scope of another class,
14280      then we need to parse the bodies of all of the queued function
14281      definitions.  Note that the queued functions defined in a class
14282      are not always processed immediately following the
14283      class-specifier for that class.  Consider:
14284
14285        struct A {
14286          struct B { void f() { sizeof (A); } };
14287        };
14288
14289      If `f' were processed before the processing of `A' were
14290      completed, there would be no way to compute the size of `A'.
14291      Note that the nesting we are interested in here is lexical --
14292      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14293      for:
14294
14295        struct A { struct B; };
14296        struct A::B { void f() { } };
14297
14298      there is no need to delay the parsing of `A::B::f'.  */
14299   if (--parser->num_classes_being_defined == 0)
14300     {
14301       tree queue_entry;
14302       tree fn;
14303       tree class_type = NULL_TREE;
14304       tree pushed_scope = NULL_TREE;
14305
14306       /* In a first pass, parse default arguments to the functions.
14307          Then, in a second pass, parse the bodies of the functions.
14308          This two-phased approach handles cases like:
14309
14310             struct S {
14311               void f() { g(); }
14312               void g(int i = 3);
14313             };
14314
14315          */
14316       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14317              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14318            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14319            TREE_PURPOSE (parser->unparsed_functions_queues)
14320              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14321         {
14322           fn = TREE_VALUE (queue_entry);
14323           /* If there are default arguments that have not yet been processed,
14324              take care of them now.  */
14325           if (class_type != TREE_PURPOSE (queue_entry))
14326             {
14327               if (pushed_scope)
14328                 pop_scope (pushed_scope);
14329               class_type = TREE_PURPOSE (queue_entry);
14330               pushed_scope = push_scope (class_type);
14331             }
14332           /* Make sure that any template parameters are in scope.  */
14333           maybe_begin_member_template_processing (fn);
14334           /* Parse the default argument expressions.  */
14335           cp_parser_late_parsing_default_args (parser, fn);
14336           /* Remove any template parameters from the symbol table.  */
14337           maybe_end_member_template_processing ();
14338         }
14339       if (pushed_scope)
14340         pop_scope (pushed_scope);
14341       /* Now parse the body of the functions.  */
14342       for (TREE_VALUE (parser->unparsed_functions_queues)
14343              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14344            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14345            TREE_VALUE (parser->unparsed_functions_queues)
14346              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14347         {
14348           /* Figure out which function we need to process.  */
14349           fn = TREE_VALUE (queue_entry);
14350           /* Parse the function.  */
14351           cp_parser_late_parsing_for_member (parser, fn);
14352         }
14353     }
14354
14355   /* Put back any saved access checks.  */
14356   pop_deferring_access_checks ();
14357
14358   /* Restore saved state.  */
14359   parser->in_function_body = saved_in_function_body;
14360   parser->num_template_parameter_lists
14361     = saved_num_template_parameter_lists;
14362
14363   return type;
14364 }
14365
14366 /* Parse a class-head.
14367
14368    class-head:
14369      class-key identifier [opt] base-clause [opt]
14370      class-key nested-name-specifier identifier base-clause [opt]
14371      class-key nested-name-specifier [opt] template-id
14372        base-clause [opt]
14373
14374    GNU Extensions:
14375      class-key attributes identifier [opt] base-clause [opt]
14376      class-key attributes nested-name-specifier identifier base-clause [opt]
14377      class-key attributes nested-name-specifier [opt] template-id
14378        base-clause [opt]
14379
14380    Upon return BASES is initialized to the list of base classes (or
14381    NULL, if there are none) in the same form returned by
14382    cp_parser_base_clause.
14383
14384    Returns the TYPE of the indicated class.  Sets
14385    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14386    involving a nested-name-specifier was used, and FALSE otherwise.
14387
14388    Returns error_mark_node if this is not a class-head.
14389
14390    Returns NULL_TREE if the class-head is syntactically valid, but
14391    semantically invalid in a way that means we should skip the entire
14392    body of the class.  */
14393
14394 static tree
14395 cp_parser_class_head (cp_parser* parser,
14396                       bool* nested_name_specifier_p,
14397                       tree *attributes_p,
14398                       tree *bases)
14399 {
14400   tree nested_name_specifier;
14401   enum tag_types class_key;
14402   tree id = NULL_TREE;
14403   tree type = NULL_TREE;
14404   tree attributes;
14405   bool template_id_p = false;
14406   bool qualified_p = false;
14407   bool invalid_nested_name_p = false;
14408   bool invalid_explicit_specialization_p = false;
14409   tree pushed_scope = NULL_TREE;
14410   unsigned num_templates;
14411
14412   /* Assume no nested-name-specifier will be present.  */
14413   *nested_name_specifier_p = false;
14414   /* Assume no template parameter lists will be used in defining the
14415      type.  */
14416   num_templates = 0;
14417
14418   *bases = NULL_TREE;
14419
14420   /* Look for the class-key.  */
14421   class_key = cp_parser_class_key (parser);
14422   if (class_key == none_type)
14423     return error_mark_node;
14424
14425   /* Parse the attributes.  */
14426   attributes = cp_parser_attributes_opt (parser);
14427
14428   /* If the next token is `::', that is invalid -- but sometimes
14429      people do try to write:
14430
14431        struct ::S {};
14432
14433      Handle this gracefully by accepting the extra qualifier, and then
14434      issuing an error about it later if this really is a
14435      class-head.  If it turns out just to be an elaborated type
14436      specifier, remain silent.  */
14437   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14438     qualified_p = true;
14439
14440   push_deferring_access_checks (dk_no_check);
14441
14442   /* Determine the name of the class.  Begin by looking for an
14443      optional nested-name-specifier.  */
14444   nested_name_specifier
14445     = cp_parser_nested_name_specifier_opt (parser,
14446                                            /*typename_keyword_p=*/false,
14447                                            /*check_dependency_p=*/false,
14448                                            /*type_p=*/false,
14449                                            /*is_declaration=*/false);
14450   /* If there was a nested-name-specifier, then there *must* be an
14451      identifier.  */
14452   if (nested_name_specifier)
14453     {
14454       /* Although the grammar says `identifier', it really means
14455          `class-name' or `template-name'.  You are only allowed to
14456          define a class that has already been declared with this
14457          syntax.
14458
14459          The proposed resolution for Core Issue 180 says that wherever
14460          you see `class T::X' you should treat `X' as a type-name.
14461
14462          It is OK to define an inaccessible class; for example:
14463
14464            class A { class B; };
14465            class A::B {};
14466
14467          We do not know if we will see a class-name, or a
14468          template-name.  We look for a class-name first, in case the
14469          class-name is a template-id; if we looked for the
14470          template-name first we would stop after the template-name.  */
14471       cp_parser_parse_tentatively (parser);
14472       type = cp_parser_class_name (parser,
14473                                    /*typename_keyword_p=*/false,
14474                                    /*template_keyword_p=*/false,
14475                                    class_type,
14476                                    /*check_dependency_p=*/false,
14477                                    /*class_head_p=*/true,
14478                                    /*is_declaration=*/false);
14479       /* If that didn't work, ignore the nested-name-specifier.  */
14480       if (!cp_parser_parse_definitely (parser))
14481         {
14482           invalid_nested_name_p = true;
14483           id = cp_parser_identifier (parser);
14484           if (id == error_mark_node)
14485             id = NULL_TREE;
14486         }
14487       /* If we could not find a corresponding TYPE, treat this
14488          declaration like an unqualified declaration.  */
14489       if (type == error_mark_node)
14490         nested_name_specifier = NULL_TREE;
14491       /* Otherwise, count the number of templates used in TYPE and its
14492          containing scopes.  */
14493       else
14494         {
14495           tree scope;
14496
14497           for (scope = TREE_TYPE (type);
14498                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14499                scope = (TYPE_P (scope)
14500                         ? TYPE_CONTEXT (scope)
14501                         : DECL_CONTEXT (scope)))
14502             if (TYPE_P (scope)
14503                 && CLASS_TYPE_P (scope)
14504                 && CLASSTYPE_TEMPLATE_INFO (scope)
14505                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14506                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14507               ++num_templates;
14508         }
14509     }
14510   /* Otherwise, the identifier is optional.  */
14511   else
14512     {
14513       /* We don't know whether what comes next is a template-id,
14514          an identifier, or nothing at all.  */
14515       cp_parser_parse_tentatively (parser);
14516       /* Check for a template-id.  */
14517       id = cp_parser_template_id (parser,
14518                                   /*template_keyword_p=*/false,
14519                                   /*check_dependency_p=*/true,
14520                                   /*is_declaration=*/true);
14521       /* If that didn't work, it could still be an identifier.  */
14522       if (!cp_parser_parse_definitely (parser))
14523         {
14524           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14525             id = cp_parser_identifier (parser);
14526           else
14527             id = NULL_TREE;
14528         }
14529       else
14530         {
14531           template_id_p = true;
14532           ++num_templates;
14533         }
14534     }
14535
14536   pop_deferring_access_checks ();
14537
14538   if (id)
14539     cp_parser_check_for_invalid_template_id (parser, id);
14540
14541   /* If it's not a `:' or a `{' then we can't really be looking at a
14542      class-head, since a class-head only appears as part of a
14543      class-specifier.  We have to detect this situation before calling
14544      xref_tag, since that has irreversible side-effects.  */
14545   if (!cp_parser_next_token_starts_class_definition_p (parser))
14546     {
14547       cp_parser_error (parser, "expected %<{%> or %<:%>");
14548       return error_mark_node;
14549     }
14550
14551   /* At this point, we're going ahead with the class-specifier, even
14552      if some other problem occurs.  */
14553   cp_parser_commit_to_tentative_parse (parser);
14554   /* Issue the error about the overly-qualified name now.  */
14555   if (qualified_p)
14556     cp_parser_error (parser,
14557                      "global qualification of class name is invalid");
14558   else if (invalid_nested_name_p)
14559     cp_parser_error (parser,
14560                      "qualified name does not name a class");
14561   else if (nested_name_specifier)
14562     {
14563       tree scope;
14564
14565       /* Reject typedef-names in class heads.  */
14566       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14567         {
14568           error ("invalid class name in declaration of %qD", type);
14569           type = NULL_TREE;
14570           goto done;
14571         }
14572
14573       /* Figure out in what scope the declaration is being placed.  */
14574       scope = current_scope ();
14575       /* If that scope does not contain the scope in which the
14576          class was originally declared, the program is invalid.  */
14577       if (scope && !is_ancestor (scope, nested_name_specifier))
14578         {
14579           if (at_namespace_scope_p ())
14580             error ("declaration of %qD in namespace %qD which does not "
14581                    "enclose %qD", type, scope, nested_name_specifier);
14582           else
14583             error ("declaration of %qD in %qD which does not enclose %qD",
14584                    type, scope, nested_name_specifier);
14585           type = NULL_TREE;
14586           goto done;
14587         }
14588       /* [dcl.meaning]
14589
14590          A declarator-id shall not be qualified exception of the
14591          definition of a ... nested class outside of its class
14592          ... [or] a the definition or explicit instantiation of a
14593          class member of a namespace outside of its namespace.  */
14594       if (scope == nested_name_specifier)
14595         {
14596           pedwarn ("extra qualification ignored");
14597           nested_name_specifier = NULL_TREE;
14598           num_templates = 0;
14599         }
14600     }
14601   /* An explicit-specialization must be preceded by "template <>".  If
14602      it is not, try to recover gracefully.  */
14603   if (at_namespace_scope_p ()
14604       && parser->num_template_parameter_lists == 0
14605       && template_id_p)
14606     {
14607       error ("an explicit specialization must be preceded by %<template <>%>");
14608       invalid_explicit_specialization_p = true;
14609       /* Take the same action that would have been taken by
14610          cp_parser_explicit_specialization.  */
14611       ++parser->num_template_parameter_lists;
14612       begin_specialization ();
14613     }
14614   /* There must be no "return" statements between this point and the
14615      end of this function; set "type "to the correct return value and
14616      use "goto done;" to return.  */
14617   /* Make sure that the right number of template parameters were
14618      present.  */
14619   if (!cp_parser_check_template_parameters (parser, num_templates))
14620     {
14621       /* If something went wrong, there is no point in even trying to
14622          process the class-definition.  */
14623       type = NULL_TREE;
14624       goto done;
14625     }
14626
14627   /* Look up the type.  */
14628   if (template_id_p)
14629     {
14630       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14631           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14632               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14633         {
14634           error ("function template %qD redeclared as a class template", id);
14635           type = error_mark_node;
14636         }
14637       else
14638         {
14639           type = TREE_TYPE (id);
14640           type = maybe_process_partial_specialization (type);
14641         }
14642       if (nested_name_specifier)
14643         pushed_scope = push_scope (nested_name_specifier);
14644     }
14645   else if (nested_name_specifier)
14646     {
14647       tree class_type;
14648
14649       /* Given:
14650
14651             template <typename T> struct S { struct T };
14652             template <typename T> struct S<T>::T { };
14653
14654          we will get a TYPENAME_TYPE when processing the definition of
14655          `S::T'.  We need to resolve it to the actual type before we
14656          try to define it.  */
14657       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14658         {
14659           class_type = resolve_typename_type (TREE_TYPE (type),
14660                                               /*only_current_p=*/false);
14661           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14662             type = TYPE_NAME (class_type);
14663           else
14664             {
14665               cp_parser_error (parser, "could not resolve typename type");
14666               type = error_mark_node;
14667             }
14668         }
14669
14670       maybe_process_partial_specialization (TREE_TYPE (type));
14671       class_type = current_class_type;
14672       /* Enter the scope indicated by the nested-name-specifier.  */
14673       pushed_scope = push_scope (nested_name_specifier);
14674       /* Get the canonical version of this type.  */
14675       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14676       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14677           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14678         {
14679           type = push_template_decl (type);
14680           if (type == error_mark_node)
14681             {
14682               type = NULL_TREE;
14683               goto done;
14684             }
14685         }
14686
14687       type = TREE_TYPE (type);
14688       *nested_name_specifier_p = true;
14689     }
14690   else      /* The name is not a nested name.  */
14691     {
14692       /* If the class was unnamed, create a dummy name.  */
14693       if (!id)
14694         id = make_anon_name ();
14695       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14696                        parser->num_template_parameter_lists);
14697     }
14698
14699   /* Indicate whether this class was declared as a `class' or as a
14700      `struct'.  */
14701   if (TREE_CODE (type) == RECORD_TYPE)
14702     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14703   cp_parser_check_class_key (class_key, type);
14704
14705   /* If this type was already complete, and we see another definition,
14706      that's an error.  */
14707   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14708     {
14709       error ("redefinition of %q#T", type);
14710       error ("previous definition of %q+#T", type);
14711       type = NULL_TREE;
14712       goto done;
14713     }
14714   else if (type == error_mark_node)
14715     type = NULL_TREE;
14716
14717   /* We will have entered the scope containing the class; the names of
14718      base classes should be looked up in that context.  For example:
14719
14720        struct A { struct B {}; struct C; };
14721        struct A::C : B {};
14722
14723      is valid.  */
14724
14725   /* Get the list of base-classes, if there is one.  */
14726   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14727     *bases = cp_parser_base_clause (parser);
14728
14729  done:
14730   /* Leave the scope given by the nested-name-specifier.  We will
14731      enter the class scope itself while processing the members.  */
14732   if (pushed_scope)
14733     pop_scope (pushed_scope);
14734
14735   if (invalid_explicit_specialization_p)
14736     {
14737       end_specialization ();
14738       --parser->num_template_parameter_lists;
14739     }
14740   *attributes_p = attributes;
14741   return type;
14742 }
14743
14744 /* Parse a class-key.
14745
14746    class-key:
14747      class
14748      struct
14749      union
14750
14751    Returns the kind of class-key specified, or none_type to indicate
14752    error.  */
14753
14754 static enum tag_types
14755 cp_parser_class_key (cp_parser* parser)
14756 {
14757   cp_token *token;
14758   enum tag_types tag_type;
14759
14760   /* Look for the class-key.  */
14761   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14762   if (!token)
14763     return none_type;
14764
14765   /* Check to see if the TOKEN is a class-key.  */
14766   tag_type = cp_parser_token_is_class_key (token);
14767   if (!tag_type)
14768     cp_parser_error (parser, "expected class-key");
14769   return tag_type;
14770 }
14771
14772 /* Parse an (optional) member-specification.
14773
14774    member-specification:
14775      member-declaration member-specification [opt]
14776      access-specifier : member-specification [opt]  */
14777
14778 static void
14779 cp_parser_member_specification_opt (cp_parser* parser)
14780 {
14781   while (true)
14782     {
14783       cp_token *token;
14784       enum rid keyword;
14785
14786       /* Peek at the next token.  */
14787       token = cp_lexer_peek_token (parser->lexer);
14788       /* If it's a `}', or EOF then we've seen all the members.  */
14789       if (token->type == CPP_CLOSE_BRACE
14790           || token->type == CPP_EOF
14791           || token->type == CPP_PRAGMA_EOL)
14792         break;
14793
14794       /* See if this token is a keyword.  */
14795       keyword = token->keyword;
14796       switch (keyword)
14797         {
14798         case RID_PUBLIC:
14799         case RID_PROTECTED:
14800         case RID_PRIVATE:
14801           /* Consume the access-specifier.  */
14802           cp_lexer_consume_token (parser->lexer);
14803           /* Remember which access-specifier is active.  */
14804           current_access_specifier = token->u.value;
14805           /* Look for the `:'.  */
14806           cp_parser_require (parser, CPP_COLON, "`:'");
14807           break;
14808
14809         default:
14810           /* Accept #pragmas at class scope.  */
14811           if (token->type == CPP_PRAGMA)
14812             {
14813               cp_parser_pragma (parser, pragma_external);
14814               break;
14815             }
14816
14817           /* Otherwise, the next construction must be a
14818              member-declaration.  */
14819           cp_parser_member_declaration (parser);
14820         }
14821     }
14822 }
14823
14824 /* Parse a member-declaration.
14825
14826    member-declaration:
14827      decl-specifier-seq [opt] member-declarator-list [opt] ;
14828      function-definition ; [opt]
14829      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14830      using-declaration
14831      template-declaration
14832
14833    member-declarator-list:
14834      member-declarator
14835      member-declarator-list , member-declarator
14836
14837    member-declarator:
14838      declarator pure-specifier [opt]
14839      declarator constant-initializer [opt]
14840      identifier [opt] : constant-expression
14841
14842    GNU Extensions:
14843
14844    member-declaration:
14845      __extension__ member-declaration
14846
14847    member-declarator:
14848      declarator attributes [opt] pure-specifier [opt]
14849      declarator attributes [opt] constant-initializer [opt]
14850      identifier [opt] attributes [opt] : constant-expression  
14851
14852    C++0x Extensions:
14853
14854    member-declaration:
14855      static_assert-declaration  */
14856
14857 static void
14858 cp_parser_member_declaration (cp_parser* parser)
14859 {
14860   cp_decl_specifier_seq decl_specifiers;
14861   tree prefix_attributes;
14862   tree decl;
14863   int declares_class_or_enum;
14864   bool friend_p;
14865   cp_token *token;
14866   int saved_pedantic;
14867
14868   /* Check for the `__extension__' keyword.  */
14869   if (cp_parser_extension_opt (parser, &saved_pedantic))
14870     {
14871       /* Recurse.  */
14872       cp_parser_member_declaration (parser);
14873       /* Restore the old value of the PEDANTIC flag.  */
14874       pedantic = saved_pedantic;
14875
14876       return;
14877     }
14878
14879   /* Check for a template-declaration.  */
14880   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14881     {
14882       /* An explicit specialization here is an error condition, and we
14883          expect the specialization handler to detect and report this.  */
14884       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14885           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14886         cp_parser_explicit_specialization (parser);
14887       else
14888         cp_parser_template_declaration (parser, /*member_p=*/true);
14889
14890       return;
14891     }
14892
14893   /* Check for a using-declaration.  */
14894   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14895     {
14896       /* Parse the using-declaration.  */
14897       cp_parser_using_declaration (parser,
14898                                    /*access_declaration_p=*/false);
14899       return;
14900     }
14901
14902   /* Check for @defs.  */
14903   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14904     {
14905       tree ivar, member;
14906       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14907       ivar = ivar_chains;
14908       while (ivar)
14909         {
14910           member = ivar;
14911           ivar = TREE_CHAIN (member);
14912           TREE_CHAIN (member) = NULL_TREE;
14913           finish_member_declaration (member);
14914         }
14915       return;
14916     }
14917
14918   /* If the next token is `static_assert' we have a static assertion.  */
14919   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14920     {
14921       cp_parser_static_assert (parser, /*member_p=*/true);
14922       return;
14923     }
14924
14925   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14926     return;
14927
14928   /* Parse the decl-specifier-seq.  */
14929   cp_parser_decl_specifier_seq (parser,
14930                                 CP_PARSER_FLAGS_OPTIONAL,
14931                                 &decl_specifiers,
14932                                 &declares_class_or_enum);
14933   prefix_attributes = decl_specifiers.attributes;
14934   decl_specifiers.attributes = NULL_TREE;
14935   /* Check for an invalid type-name.  */
14936   if (!decl_specifiers.type
14937       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14938     return;
14939   /* If there is no declarator, then the decl-specifier-seq should
14940      specify a type.  */
14941   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14942     {
14943       /* If there was no decl-specifier-seq, and the next token is a
14944          `;', then we have something like:
14945
14946            struct S { ; };
14947
14948          [class.mem]
14949
14950          Each member-declaration shall declare at least one member
14951          name of the class.  */
14952       if (!decl_specifiers.any_specifiers_p)
14953         {
14954           cp_token *token = cp_lexer_peek_token (parser->lexer);
14955           if (pedantic && !token->in_system_header)
14956             pedwarn ("%Hextra %<;%>", &token->location);
14957         }
14958       else
14959         {
14960           tree type;
14961
14962           /* See if this declaration is a friend.  */
14963           friend_p = cp_parser_friend_p (&decl_specifiers);
14964           /* If there were decl-specifiers, check to see if there was
14965              a class-declaration.  */
14966           type = check_tag_decl (&decl_specifiers);
14967           /* Nested classes have already been added to the class, but
14968              a `friend' needs to be explicitly registered.  */
14969           if (friend_p)
14970             {
14971               /* If the `friend' keyword was present, the friend must
14972                  be introduced with a class-key.  */
14973                if (!declares_class_or_enum)
14974                  error ("a class-key must be used when declaring a friend");
14975                /* In this case:
14976
14977                     template <typename T> struct A {
14978                       friend struct A<T>::B;
14979                     };
14980
14981                   A<T>::B will be represented by a TYPENAME_TYPE, and
14982                   therefore not recognized by check_tag_decl.  */
14983                if (!type
14984                    && decl_specifiers.type
14985                    && TYPE_P (decl_specifiers.type))
14986                  type = decl_specifiers.type;
14987                if (!type || !TYPE_P (type))
14988                  error ("friend declaration does not name a class or "
14989                         "function");
14990                else
14991                  make_friend_class (current_class_type, type,
14992                                     /*complain=*/true);
14993             }
14994           /* If there is no TYPE, an error message will already have
14995              been issued.  */
14996           else if (!type || type == error_mark_node)
14997             ;
14998           /* An anonymous aggregate has to be handled specially; such
14999              a declaration really declares a data member (with a
15000              particular type), as opposed to a nested class.  */
15001           else if (ANON_AGGR_TYPE_P (type))
15002             {
15003               /* Remove constructors and such from TYPE, now that we
15004                  know it is an anonymous aggregate.  */
15005               fixup_anonymous_aggr (type);
15006               /* And make the corresponding data member.  */
15007               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15008               /* Add it to the class.  */
15009               finish_member_declaration (decl);
15010             }
15011           else
15012             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15013         }
15014     }
15015   else
15016     {
15017       /* See if these declarations will be friends.  */
15018       friend_p = cp_parser_friend_p (&decl_specifiers);
15019
15020       /* Keep going until we hit the `;' at the end of the
15021          declaration.  */
15022       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15023         {
15024           tree attributes = NULL_TREE;
15025           tree first_attribute;
15026
15027           /* Peek at the next token.  */
15028           token = cp_lexer_peek_token (parser->lexer);
15029
15030           /* Check for a bitfield declaration.  */
15031           if (token->type == CPP_COLON
15032               || (token->type == CPP_NAME
15033                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15034                   == CPP_COLON))
15035             {
15036               tree identifier;
15037               tree width;
15038
15039               /* Get the name of the bitfield.  Note that we cannot just
15040                  check TOKEN here because it may have been invalidated by
15041                  the call to cp_lexer_peek_nth_token above.  */
15042               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15043                 identifier = cp_parser_identifier (parser);
15044               else
15045                 identifier = NULL_TREE;
15046
15047               /* Consume the `:' token.  */
15048               cp_lexer_consume_token (parser->lexer);
15049               /* Get the width of the bitfield.  */
15050               width
15051                 = cp_parser_constant_expression (parser,
15052                                                  /*allow_non_constant=*/false,
15053                                                  NULL);
15054
15055               /* Look for attributes that apply to the bitfield.  */
15056               attributes = cp_parser_attributes_opt (parser);
15057               /* Remember which attributes are prefix attributes and
15058                  which are not.  */
15059               first_attribute = attributes;
15060               /* Combine the attributes.  */
15061               attributes = chainon (prefix_attributes, attributes);
15062
15063               /* Create the bitfield declaration.  */
15064               decl = grokbitfield (identifier
15065                                    ? make_id_declarator (NULL_TREE,
15066                                                          identifier,
15067                                                          sfk_none)
15068                                    : NULL,
15069                                    &decl_specifiers,
15070                                    width);
15071               /* Apply the attributes.  */
15072               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15073             }
15074           else
15075             {
15076               cp_declarator *declarator;
15077               tree initializer;
15078               tree asm_specification;
15079               int ctor_dtor_or_conv_p;
15080
15081               /* Parse the declarator.  */
15082               declarator
15083                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15084                                         &ctor_dtor_or_conv_p,
15085                                         /*parenthesized_p=*/NULL,
15086                                         /*member_p=*/true);
15087
15088               /* If something went wrong parsing the declarator, make sure
15089                  that we at least consume some tokens.  */
15090               if (declarator == cp_error_declarator)
15091                 {
15092                   /* Skip to the end of the statement.  */
15093                   cp_parser_skip_to_end_of_statement (parser);
15094                   /* If the next token is not a semicolon, that is
15095                      probably because we just skipped over the body of
15096                      a function.  So, we consume a semicolon if
15097                      present, but do not issue an error message if it
15098                      is not present.  */
15099                   if (cp_lexer_next_token_is (parser->lexer,
15100                                               CPP_SEMICOLON))
15101                     cp_lexer_consume_token (parser->lexer);
15102                   return;
15103                 }
15104
15105               if (declares_class_or_enum & 2)
15106                 cp_parser_check_for_definition_in_return_type
15107                   (declarator, decl_specifiers.type);
15108
15109               /* Look for an asm-specification.  */
15110               asm_specification = cp_parser_asm_specification_opt (parser);
15111               /* Look for attributes that apply to the declaration.  */
15112               attributes = cp_parser_attributes_opt (parser);
15113               /* Remember which attributes are prefix attributes and
15114                  which are not.  */
15115               first_attribute = attributes;
15116               /* Combine the attributes.  */
15117               attributes = chainon (prefix_attributes, attributes);
15118
15119               /* If it's an `=', then we have a constant-initializer or a
15120                  pure-specifier.  It is not correct to parse the
15121                  initializer before registering the member declaration
15122                  since the member declaration should be in scope while
15123                  its initializer is processed.  However, the rest of the
15124                  front end does not yet provide an interface that allows
15125                  us to handle this correctly.  */
15126               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15127                 {
15128                   /* In [class.mem]:
15129
15130                      A pure-specifier shall be used only in the declaration of
15131                      a virtual function.
15132
15133                      A member-declarator can contain a constant-initializer
15134                      only if it declares a static member of integral or
15135                      enumeration type.
15136
15137                      Therefore, if the DECLARATOR is for a function, we look
15138                      for a pure-specifier; otherwise, we look for a
15139                      constant-initializer.  When we call `grokfield', it will
15140                      perform more stringent semantics checks.  */
15141                   if (function_declarator_p (declarator))
15142                     initializer = cp_parser_pure_specifier (parser);
15143                   else
15144                     /* Parse the initializer.  */
15145                     initializer = cp_parser_constant_initializer (parser);
15146                 }
15147               /* Otherwise, there is no initializer.  */
15148               else
15149                 initializer = NULL_TREE;
15150
15151               /* See if we are probably looking at a function
15152                  definition.  We are certainly not looking at a
15153                  member-declarator.  Calling `grokfield' has
15154                  side-effects, so we must not do it unless we are sure
15155                  that we are looking at a member-declarator.  */
15156               if (cp_parser_token_starts_function_definition_p
15157                   (cp_lexer_peek_token (parser->lexer)))
15158                 {
15159                   /* The grammar does not allow a pure-specifier to be
15160                      used when a member function is defined.  (It is
15161                      possible that this fact is an oversight in the
15162                      standard, since a pure function may be defined
15163                      outside of the class-specifier.  */
15164                   if (initializer)
15165                     error ("pure-specifier on function-definition");
15166                   decl = cp_parser_save_member_function_body (parser,
15167                                                               &decl_specifiers,
15168                                                               declarator,
15169                                                               attributes);
15170                   /* If the member was not a friend, declare it here.  */
15171                   if (!friend_p)
15172                     finish_member_declaration (decl);
15173                   /* Peek at the next token.  */
15174                   token = cp_lexer_peek_token (parser->lexer);
15175                   /* If the next token is a semicolon, consume it.  */
15176                   if (token->type == CPP_SEMICOLON)
15177                     cp_lexer_consume_token (parser->lexer);
15178                   return;
15179                 }
15180               else
15181                 /* Create the declaration.  */
15182                 decl = grokfield (declarator, &decl_specifiers,
15183                                   initializer, /*init_const_expr_p=*/true,
15184                                   asm_specification,
15185                                   attributes);
15186             }
15187
15188           /* Reset PREFIX_ATTRIBUTES.  */
15189           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15190             attributes = TREE_CHAIN (attributes);
15191           if (attributes)
15192             TREE_CHAIN (attributes) = NULL_TREE;
15193
15194           /* If there is any qualification still in effect, clear it
15195              now; we will be starting fresh with the next declarator.  */
15196           parser->scope = NULL_TREE;
15197           parser->qualifying_scope = NULL_TREE;
15198           parser->object_scope = NULL_TREE;
15199           /* If it's a `,', then there are more declarators.  */
15200           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15201             cp_lexer_consume_token (parser->lexer);
15202           /* If the next token isn't a `;', then we have a parse error.  */
15203           else if (cp_lexer_next_token_is_not (parser->lexer,
15204                                                CPP_SEMICOLON))
15205             {
15206               cp_parser_error (parser, "expected %<;%>");
15207               /* Skip tokens until we find a `;'.  */
15208               cp_parser_skip_to_end_of_statement (parser);
15209
15210               break;
15211             }
15212
15213           if (decl)
15214             {
15215               /* Add DECL to the list of members.  */
15216               if (!friend_p)
15217                 finish_member_declaration (decl);
15218
15219               if (TREE_CODE (decl) == FUNCTION_DECL)
15220                 cp_parser_save_default_args (parser, decl);
15221             }
15222         }
15223     }
15224
15225   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15226 }
15227
15228 /* Parse a pure-specifier.
15229
15230    pure-specifier:
15231      = 0
15232
15233    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15234    Otherwise, ERROR_MARK_NODE is returned.  */
15235
15236 static tree
15237 cp_parser_pure_specifier (cp_parser* parser)
15238 {
15239   cp_token *token;
15240
15241   /* Look for the `=' token.  */
15242   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15243     return error_mark_node;
15244   /* Look for the `0' token.  */
15245   token = cp_lexer_consume_token (parser->lexer);
15246   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15247   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15248     {
15249       cp_parser_error (parser,
15250                        "invalid pure specifier (only `= 0' is allowed)");
15251       cp_parser_skip_to_end_of_statement (parser);
15252       return error_mark_node;
15253     }
15254   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15255     {
15256       error ("templates may not be %<virtual%>");
15257       return error_mark_node;
15258     }
15259
15260   return integer_zero_node;
15261 }
15262
15263 /* Parse a constant-initializer.
15264
15265    constant-initializer:
15266      = constant-expression
15267
15268    Returns a representation of the constant-expression.  */
15269
15270 static tree
15271 cp_parser_constant_initializer (cp_parser* parser)
15272 {
15273   /* Look for the `=' token.  */
15274   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15275     return error_mark_node;
15276
15277   /* It is invalid to write:
15278
15279        struct S { static const int i = { 7 }; };
15280
15281      */
15282   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15283     {
15284       cp_parser_error (parser,
15285                        "a brace-enclosed initializer is not allowed here");
15286       /* Consume the opening brace.  */
15287       cp_lexer_consume_token (parser->lexer);
15288       /* Skip the initializer.  */
15289       cp_parser_skip_to_closing_brace (parser);
15290       /* Look for the trailing `}'.  */
15291       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15292
15293       return error_mark_node;
15294     }
15295
15296   return cp_parser_constant_expression (parser,
15297                                         /*allow_non_constant=*/false,
15298                                         NULL);
15299 }
15300
15301 /* Derived classes [gram.class.derived] */
15302
15303 /* Parse a base-clause.
15304
15305    base-clause:
15306      : base-specifier-list
15307
15308    base-specifier-list:
15309      base-specifier ... [opt]
15310      base-specifier-list , base-specifier ... [opt]
15311
15312    Returns a TREE_LIST representing the base-classes, in the order in
15313    which they were declared.  The representation of each node is as
15314    described by cp_parser_base_specifier.
15315
15316    In the case that no bases are specified, this function will return
15317    NULL_TREE, not ERROR_MARK_NODE.  */
15318
15319 static tree
15320 cp_parser_base_clause (cp_parser* parser)
15321 {
15322   tree bases = NULL_TREE;
15323
15324   /* Look for the `:' that begins the list.  */
15325   cp_parser_require (parser, CPP_COLON, "`:'");
15326
15327   /* Scan the base-specifier-list.  */
15328   while (true)
15329     {
15330       cp_token *token;
15331       tree base;
15332       bool pack_expansion_p = false;
15333
15334       /* Look for the base-specifier.  */
15335       base = cp_parser_base_specifier (parser);
15336       /* Look for the (optional) ellipsis. */
15337       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15338         {
15339           /* Consume the `...'. */
15340           cp_lexer_consume_token (parser->lexer);
15341
15342           pack_expansion_p = true;
15343         }
15344
15345       /* Add BASE to the front of the list.  */
15346       if (base != error_mark_node)
15347         {
15348           if (pack_expansion_p)
15349             /* Make this a pack expansion type. */
15350             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15351           
15352
15353           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15354             {
15355               TREE_CHAIN (base) = bases;
15356               bases = base;
15357             }
15358         }
15359       /* Peek at the next token.  */
15360       token = cp_lexer_peek_token (parser->lexer);
15361       /* If it's not a comma, then the list is complete.  */
15362       if (token->type != CPP_COMMA)
15363         break;
15364       /* Consume the `,'.  */
15365       cp_lexer_consume_token (parser->lexer);
15366     }
15367
15368   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15369      base class had a qualified name.  However, the next name that
15370      appears is certainly not qualified.  */
15371   parser->scope = NULL_TREE;
15372   parser->qualifying_scope = NULL_TREE;
15373   parser->object_scope = NULL_TREE;
15374
15375   return nreverse (bases);
15376 }
15377
15378 /* Parse a base-specifier.
15379
15380    base-specifier:
15381      :: [opt] nested-name-specifier [opt] class-name
15382      virtual access-specifier [opt] :: [opt] nested-name-specifier
15383        [opt] class-name
15384      access-specifier virtual [opt] :: [opt] nested-name-specifier
15385        [opt] class-name
15386
15387    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15388    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15389    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15390    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15391
15392 static tree
15393 cp_parser_base_specifier (cp_parser* parser)
15394 {
15395   cp_token *token;
15396   bool done = false;
15397   bool virtual_p = false;
15398   bool duplicate_virtual_error_issued_p = false;
15399   bool duplicate_access_error_issued_p = false;
15400   bool class_scope_p, template_p;
15401   tree access = access_default_node;
15402   tree type;
15403
15404   /* Process the optional `virtual' and `access-specifier'.  */
15405   while (!done)
15406     {
15407       /* Peek at the next token.  */
15408       token = cp_lexer_peek_token (parser->lexer);
15409       /* Process `virtual'.  */
15410       switch (token->keyword)
15411         {
15412         case RID_VIRTUAL:
15413           /* If `virtual' appears more than once, issue an error.  */
15414           if (virtual_p && !duplicate_virtual_error_issued_p)
15415             {
15416               cp_parser_error (parser,
15417                                "%<virtual%> specified more than once in base-specified");
15418               duplicate_virtual_error_issued_p = true;
15419             }
15420
15421           virtual_p = true;
15422
15423           /* Consume the `virtual' token.  */
15424           cp_lexer_consume_token (parser->lexer);
15425
15426           break;
15427
15428         case RID_PUBLIC:
15429         case RID_PROTECTED:
15430         case RID_PRIVATE:
15431           /* If more than one access specifier appears, issue an
15432              error.  */
15433           if (access != access_default_node
15434               && !duplicate_access_error_issued_p)
15435             {
15436               cp_parser_error (parser,
15437                                "more than one access specifier in base-specified");
15438               duplicate_access_error_issued_p = true;
15439             }
15440
15441           access = ridpointers[(int) token->keyword];
15442
15443           /* Consume the access-specifier.  */
15444           cp_lexer_consume_token (parser->lexer);
15445
15446           break;
15447
15448         default:
15449           done = true;
15450           break;
15451         }
15452     }
15453   /* It is not uncommon to see programs mechanically, erroneously, use
15454      the 'typename' keyword to denote (dependent) qualified types
15455      as base classes.  */
15456   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15457     {
15458       if (!processing_template_decl)
15459         error ("keyword %<typename%> not allowed outside of templates");
15460       else
15461         error ("keyword %<typename%> not allowed in this context "
15462                "(the base class is implicitly a type)");
15463       cp_lexer_consume_token (parser->lexer);
15464     }
15465
15466   /* Look for the optional `::' operator.  */
15467   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15468   /* Look for the nested-name-specifier.  The simplest way to
15469      implement:
15470
15471        [temp.res]
15472
15473        The keyword `typename' is not permitted in a base-specifier or
15474        mem-initializer; in these contexts a qualified name that
15475        depends on a template-parameter is implicitly assumed to be a
15476        type name.
15477
15478      is to pretend that we have seen the `typename' keyword at this
15479      point.  */
15480   cp_parser_nested_name_specifier_opt (parser,
15481                                        /*typename_keyword_p=*/true,
15482                                        /*check_dependency_p=*/true,
15483                                        typename_type,
15484                                        /*is_declaration=*/true);
15485   /* If the base class is given by a qualified name, assume that names
15486      we see are type names or templates, as appropriate.  */
15487   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15488   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15489
15490   /* Finally, look for the class-name.  */
15491   type = cp_parser_class_name (parser,
15492                                class_scope_p,
15493                                template_p,
15494                                typename_type,
15495                                /*check_dependency_p=*/true,
15496                                /*class_head_p=*/false,
15497                                /*is_declaration=*/true);
15498
15499   if (type == error_mark_node)
15500     return error_mark_node;
15501
15502   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15503 }
15504
15505 /* Exception handling [gram.exception] */
15506
15507 /* Parse an (optional) exception-specification.
15508
15509    exception-specification:
15510      throw ( type-id-list [opt] )
15511
15512    Returns a TREE_LIST representing the exception-specification.  The
15513    TREE_VALUE of each node is a type.  */
15514
15515 static tree
15516 cp_parser_exception_specification_opt (cp_parser* parser)
15517 {
15518   cp_token *token;
15519   tree type_id_list;
15520
15521   /* Peek at the next token.  */
15522   token = cp_lexer_peek_token (parser->lexer);
15523   /* If it's not `throw', then there's no exception-specification.  */
15524   if (!cp_parser_is_keyword (token, RID_THROW))
15525     return NULL_TREE;
15526
15527   /* Consume the `throw'.  */
15528   cp_lexer_consume_token (parser->lexer);
15529
15530   /* Look for the `('.  */
15531   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15532
15533   /* Peek at the next token.  */
15534   token = cp_lexer_peek_token (parser->lexer);
15535   /* If it's not a `)', then there is a type-id-list.  */
15536   if (token->type != CPP_CLOSE_PAREN)
15537     {
15538       const char *saved_message;
15539
15540       /* Types may not be defined in an exception-specification.  */
15541       saved_message = parser->type_definition_forbidden_message;
15542       parser->type_definition_forbidden_message
15543         = "types may not be defined in an exception-specification";
15544       /* Parse the type-id-list.  */
15545       type_id_list = cp_parser_type_id_list (parser);
15546       /* Restore the saved message.  */
15547       parser->type_definition_forbidden_message = saved_message;
15548     }
15549   else
15550     type_id_list = empty_except_spec;
15551
15552   /* Look for the `)'.  */
15553   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15554
15555   return type_id_list;
15556 }
15557
15558 /* Parse an (optional) type-id-list.
15559
15560    type-id-list:
15561      type-id ... [opt]
15562      type-id-list , type-id ... [opt]
15563
15564    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15565    in the order that the types were presented.  */
15566
15567 static tree
15568 cp_parser_type_id_list (cp_parser* parser)
15569 {
15570   tree types = NULL_TREE;
15571
15572   while (true)
15573     {
15574       cp_token *token;
15575       tree type;
15576
15577       /* Get the next type-id.  */
15578       type = cp_parser_type_id (parser);
15579       /* Parse the optional ellipsis. */
15580       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15581         {
15582           /* Consume the `...'. */
15583           cp_lexer_consume_token (parser->lexer);
15584
15585           /* Turn the type into a pack expansion expression. */
15586           type = make_pack_expansion (type);
15587         }
15588       /* Add it to the list.  */
15589       types = add_exception_specifier (types, type, /*complain=*/1);
15590       /* Peek at the next token.  */
15591       token = cp_lexer_peek_token (parser->lexer);
15592       /* If it is not a `,', we are done.  */
15593       if (token->type != CPP_COMMA)
15594         break;
15595       /* Consume the `,'.  */
15596       cp_lexer_consume_token (parser->lexer);
15597     }
15598
15599   return nreverse (types);
15600 }
15601
15602 /* Parse a try-block.
15603
15604    try-block:
15605      try compound-statement handler-seq  */
15606
15607 static tree
15608 cp_parser_try_block (cp_parser* parser)
15609 {
15610   tree try_block;
15611
15612   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15613   try_block = begin_try_block ();
15614   cp_parser_compound_statement (parser, NULL, true);
15615   finish_try_block (try_block);
15616   cp_parser_handler_seq (parser);
15617   finish_handler_sequence (try_block);
15618
15619   return try_block;
15620 }
15621
15622 /* Parse a function-try-block.
15623
15624    function-try-block:
15625      try ctor-initializer [opt] function-body handler-seq  */
15626
15627 static bool
15628 cp_parser_function_try_block (cp_parser* parser)
15629 {
15630   tree compound_stmt;
15631   tree try_block;
15632   bool ctor_initializer_p;
15633
15634   /* Look for the `try' keyword.  */
15635   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15636     return false;
15637   /* Let the rest of the front end know where we are.  */
15638   try_block = begin_function_try_block (&compound_stmt);
15639   /* Parse the function-body.  */
15640   ctor_initializer_p
15641     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15642   /* We're done with the `try' part.  */
15643   finish_function_try_block (try_block);
15644   /* Parse the handlers.  */
15645   cp_parser_handler_seq (parser);
15646   /* We're done with the handlers.  */
15647   finish_function_handler_sequence (try_block, compound_stmt);
15648
15649   return ctor_initializer_p;
15650 }
15651
15652 /* Parse a handler-seq.
15653
15654    handler-seq:
15655      handler handler-seq [opt]  */
15656
15657 static void
15658 cp_parser_handler_seq (cp_parser* parser)
15659 {
15660   while (true)
15661     {
15662       cp_token *token;
15663
15664       /* Parse the handler.  */
15665       cp_parser_handler (parser);
15666       /* Peek at the next token.  */
15667       token = cp_lexer_peek_token (parser->lexer);
15668       /* If it's not `catch' then there are no more handlers.  */
15669       if (!cp_parser_is_keyword (token, RID_CATCH))
15670         break;
15671     }
15672 }
15673
15674 /* Parse a handler.
15675
15676    handler:
15677      catch ( exception-declaration ) compound-statement  */
15678
15679 static void
15680 cp_parser_handler (cp_parser* parser)
15681 {
15682   tree handler;
15683   tree declaration;
15684
15685   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15686   handler = begin_handler ();
15687   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15688   declaration = cp_parser_exception_declaration (parser);
15689   finish_handler_parms (declaration, handler);
15690   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15691   cp_parser_compound_statement (parser, NULL, false);
15692   finish_handler (handler);
15693 }
15694
15695 /* Parse an exception-declaration.
15696
15697    exception-declaration:
15698      type-specifier-seq declarator
15699      type-specifier-seq abstract-declarator
15700      type-specifier-seq
15701      ...
15702
15703    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15704    ellipsis variant is used.  */
15705
15706 static tree
15707 cp_parser_exception_declaration (cp_parser* parser)
15708 {
15709   cp_decl_specifier_seq type_specifiers;
15710   cp_declarator *declarator;
15711   const char *saved_message;
15712
15713   /* If it's an ellipsis, it's easy to handle.  */
15714   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15715     {
15716       /* Consume the `...' token.  */
15717       cp_lexer_consume_token (parser->lexer);
15718       return NULL_TREE;
15719     }
15720
15721   /* Types may not be defined in exception-declarations.  */
15722   saved_message = parser->type_definition_forbidden_message;
15723   parser->type_definition_forbidden_message
15724     = "types may not be defined in exception-declarations";
15725
15726   /* Parse the type-specifier-seq.  */
15727   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15728                                 &type_specifiers);
15729   /* If it's a `)', then there is no declarator.  */
15730   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15731     declarator = NULL;
15732   else
15733     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15734                                        /*ctor_dtor_or_conv_p=*/NULL,
15735                                        /*parenthesized_p=*/NULL,
15736                                        /*member_p=*/false);
15737
15738   /* Restore the saved message.  */
15739   parser->type_definition_forbidden_message = saved_message;
15740
15741   if (!type_specifiers.any_specifiers_p)
15742     return error_mark_node;
15743
15744   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15745 }
15746
15747 /* Parse a throw-expression.
15748
15749    throw-expression:
15750      throw assignment-expression [opt]
15751
15752    Returns a THROW_EXPR representing the throw-expression.  */
15753
15754 static tree
15755 cp_parser_throw_expression (cp_parser* parser)
15756 {
15757   tree expression;
15758   cp_token* token;
15759
15760   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15761   token = cp_lexer_peek_token (parser->lexer);
15762   /* Figure out whether or not there is an assignment-expression
15763      following the "throw" keyword.  */
15764   if (token->type == CPP_COMMA
15765       || token->type == CPP_SEMICOLON
15766       || token->type == CPP_CLOSE_PAREN
15767       || token->type == CPP_CLOSE_SQUARE
15768       || token->type == CPP_CLOSE_BRACE
15769       || token->type == CPP_COLON)
15770     expression = NULL_TREE;
15771   else
15772     expression = cp_parser_assignment_expression (parser,
15773                                                   /*cast_p=*/false);
15774
15775   return build_throw (expression);
15776 }
15777
15778 /* GNU Extensions */
15779
15780 /* Parse an (optional) asm-specification.
15781
15782    asm-specification:
15783      asm ( string-literal )
15784
15785    If the asm-specification is present, returns a STRING_CST
15786    corresponding to the string-literal.  Otherwise, returns
15787    NULL_TREE.  */
15788
15789 static tree
15790 cp_parser_asm_specification_opt (cp_parser* parser)
15791 {
15792   cp_token *token;
15793   tree asm_specification;
15794
15795   /* Peek at the next token.  */
15796   token = cp_lexer_peek_token (parser->lexer);
15797   /* If the next token isn't the `asm' keyword, then there's no
15798      asm-specification.  */
15799   if (!cp_parser_is_keyword (token, RID_ASM))
15800     return NULL_TREE;
15801
15802   /* Consume the `asm' token.  */
15803   cp_lexer_consume_token (parser->lexer);
15804   /* Look for the `('.  */
15805   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15806
15807   /* Look for the string-literal.  */
15808   asm_specification = cp_parser_string_literal (parser, false, false);
15809
15810   /* Look for the `)'.  */
15811   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15812
15813   return asm_specification;
15814 }
15815
15816 /* Parse an asm-operand-list.
15817
15818    asm-operand-list:
15819      asm-operand
15820      asm-operand-list , asm-operand
15821
15822    asm-operand:
15823      string-literal ( expression )
15824      [ string-literal ] string-literal ( expression )
15825
15826    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15827    each node is the expression.  The TREE_PURPOSE is itself a
15828    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15829    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15830    is a STRING_CST for the string literal before the parenthesis. Returns
15831    ERROR_MARK_NODE if any of the operands are invalid.  */
15832
15833 static tree
15834 cp_parser_asm_operand_list (cp_parser* parser)
15835 {
15836   tree asm_operands = NULL_TREE;
15837   bool invalid_operands = false;
15838
15839   while (true)
15840     {
15841       tree string_literal;
15842       tree expression;
15843       tree name;
15844
15845       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15846         {
15847           /* Consume the `[' token.  */
15848           cp_lexer_consume_token (parser->lexer);
15849           /* Read the operand name.  */
15850           name = cp_parser_identifier (parser);
15851           if (name != error_mark_node)
15852             name = build_string (IDENTIFIER_LENGTH (name),
15853                                  IDENTIFIER_POINTER (name));
15854           /* Look for the closing `]'.  */
15855           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15856         }
15857       else
15858         name = NULL_TREE;
15859       /* Look for the string-literal.  */
15860       string_literal = cp_parser_string_literal (parser, false, false);
15861
15862       /* Look for the `('.  */
15863       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15864       /* Parse the expression.  */
15865       expression = cp_parser_expression (parser, /*cast_p=*/false);
15866       /* Look for the `)'.  */
15867       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15868
15869       if (name == error_mark_node 
15870           || string_literal == error_mark_node 
15871           || expression == error_mark_node)
15872         invalid_operands = true;
15873
15874       /* Add this operand to the list.  */
15875       asm_operands = tree_cons (build_tree_list (name, string_literal),
15876                                 expression,
15877                                 asm_operands);
15878       /* If the next token is not a `,', there are no more
15879          operands.  */
15880       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15881         break;
15882       /* Consume the `,'.  */
15883       cp_lexer_consume_token (parser->lexer);
15884     }
15885
15886   return invalid_operands ? error_mark_node : nreverse (asm_operands);
15887 }
15888
15889 /* Parse an asm-clobber-list.
15890
15891    asm-clobber-list:
15892      string-literal
15893      asm-clobber-list , string-literal
15894
15895    Returns a TREE_LIST, indicating the clobbers in the order that they
15896    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15897
15898 static tree
15899 cp_parser_asm_clobber_list (cp_parser* parser)
15900 {
15901   tree clobbers = NULL_TREE;
15902
15903   while (true)
15904     {
15905       tree string_literal;
15906
15907       /* Look for the string literal.  */
15908       string_literal = cp_parser_string_literal (parser, false, false);
15909       /* Add it to the list.  */
15910       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15911       /* If the next token is not a `,', then the list is
15912          complete.  */
15913       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15914         break;
15915       /* Consume the `,' token.  */
15916       cp_lexer_consume_token (parser->lexer);
15917     }
15918
15919   return clobbers;
15920 }
15921
15922 /* Parse an (optional) series of attributes.
15923
15924    attributes:
15925      attributes attribute
15926
15927    attribute:
15928      __attribute__ (( attribute-list [opt] ))
15929
15930    The return value is as for cp_parser_attribute_list.  */
15931
15932 static tree
15933 cp_parser_attributes_opt (cp_parser* parser)
15934 {
15935   tree attributes = NULL_TREE;
15936
15937   while (true)
15938     {
15939       cp_token *token;
15940       tree attribute_list;
15941
15942       /* Peek at the next token.  */
15943       token = cp_lexer_peek_token (parser->lexer);
15944       /* If it's not `__attribute__', then we're done.  */
15945       if (token->keyword != RID_ATTRIBUTE)
15946         break;
15947
15948       /* Consume the `__attribute__' keyword.  */
15949       cp_lexer_consume_token (parser->lexer);
15950       /* Look for the two `(' tokens.  */
15951       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15952       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15953
15954       /* Peek at the next token.  */
15955       token = cp_lexer_peek_token (parser->lexer);
15956       if (token->type != CPP_CLOSE_PAREN)
15957         /* Parse the attribute-list.  */
15958         attribute_list = cp_parser_attribute_list (parser);
15959       else
15960         /* If the next token is a `)', then there is no attribute
15961            list.  */
15962         attribute_list = NULL;
15963
15964       /* Look for the two `)' tokens.  */
15965       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15966       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15967
15968       /* Add these new attributes to the list.  */
15969       attributes = chainon (attributes, attribute_list);
15970     }
15971
15972   return attributes;
15973 }
15974
15975 /* Parse an attribute-list.
15976
15977    attribute-list:
15978      attribute
15979      attribute-list , attribute
15980
15981    attribute:
15982      identifier
15983      identifier ( identifier )
15984      identifier ( identifier , expression-list )
15985      identifier ( expression-list )
15986
15987    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15988    to an attribute.  The TREE_PURPOSE of each node is the identifier
15989    indicating which attribute is in use.  The TREE_VALUE represents
15990    the arguments, if any.  */
15991
15992 static tree
15993 cp_parser_attribute_list (cp_parser* parser)
15994 {
15995   tree attribute_list = NULL_TREE;
15996   bool save_translate_strings_p = parser->translate_strings_p;
15997
15998   parser->translate_strings_p = false;
15999   while (true)
16000     {
16001       cp_token *token;
16002       tree identifier;
16003       tree attribute;
16004
16005       /* Look for the identifier.  We also allow keywords here; for
16006          example `__attribute__ ((const))' is legal.  */
16007       token = cp_lexer_peek_token (parser->lexer);
16008       if (token->type == CPP_NAME
16009           || token->type == CPP_KEYWORD)
16010         {
16011           tree arguments = NULL_TREE;
16012
16013           /* Consume the token.  */
16014           token = cp_lexer_consume_token (parser->lexer);
16015
16016           /* Save away the identifier that indicates which attribute
16017              this is.  */
16018           identifier = token->u.value;
16019           attribute = build_tree_list (identifier, NULL_TREE);
16020
16021           /* Peek at the next token.  */
16022           token = cp_lexer_peek_token (parser->lexer);
16023           /* If it's an `(', then parse the attribute arguments.  */
16024           if (token->type == CPP_OPEN_PAREN)
16025             {
16026               arguments = cp_parser_parenthesized_expression_list
16027                           (parser, true, /*cast_p=*/false,
16028                            /*allow_expansion_p=*/false,
16029                            /*non_constant_p=*/NULL);
16030               /* Save the arguments away.  */
16031               TREE_VALUE (attribute) = arguments;
16032             }
16033
16034           if (arguments != error_mark_node)
16035             {
16036               /* Add this attribute to the list.  */
16037               TREE_CHAIN (attribute) = attribute_list;
16038               attribute_list = attribute;
16039             }
16040
16041           token = cp_lexer_peek_token (parser->lexer);
16042         }
16043       /* Now, look for more attributes.  If the next token isn't a
16044          `,', we're done.  */
16045       if (token->type != CPP_COMMA)
16046         break;
16047
16048       /* Consume the comma and keep going.  */
16049       cp_lexer_consume_token (parser->lexer);
16050     }
16051   parser->translate_strings_p = save_translate_strings_p;
16052
16053   /* We built up the list in reverse order.  */
16054   return nreverse (attribute_list);
16055 }
16056
16057 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16058    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16059    current value of the PEDANTIC flag, regardless of whether or not
16060    the `__extension__' keyword is present.  The caller is responsible
16061    for restoring the value of the PEDANTIC flag.  */
16062
16063 static bool
16064 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16065 {
16066   /* Save the old value of the PEDANTIC flag.  */
16067   *saved_pedantic = pedantic;
16068
16069   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16070     {
16071       /* Consume the `__extension__' token.  */
16072       cp_lexer_consume_token (parser->lexer);
16073       /* We're not being pedantic while the `__extension__' keyword is
16074          in effect.  */
16075       pedantic = 0;
16076
16077       return true;
16078     }
16079
16080   return false;
16081 }
16082
16083 /* Parse a label declaration.
16084
16085    label-declaration:
16086      __label__ label-declarator-seq ;
16087
16088    label-declarator-seq:
16089      identifier , label-declarator-seq
16090      identifier  */
16091
16092 static void
16093 cp_parser_label_declaration (cp_parser* parser)
16094 {
16095   /* Look for the `__label__' keyword.  */
16096   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16097
16098   while (true)
16099     {
16100       tree identifier;
16101
16102       /* Look for an identifier.  */
16103       identifier = cp_parser_identifier (parser);
16104       /* If we failed, stop.  */
16105       if (identifier == error_mark_node)
16106         break;
16107       /* Declare it as a label.  */
16108       finish_label_decl (identifier);
16109       /* If the next token is a `;', stop.  */
16110       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16111         break;
16112       /* Look for the `,' separating the label declarations.  */
16113       cp_parser_require (parser, CPP_COMMA, "`,'");
16114     }
16115
16116   /* Look for the final `;'.  */
16117   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16118 }
16119
16120 /* Support Functions */
16121
16122 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16123    NAME should have one of the representations used for an
16124    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16125    is returned.  If PARSER->SCOPE is a dependent type, then a
16126    SCOPE_REF is returned.
16127
16128    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16129    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16130    was formed.  Abstractly, such entities should not be passed to this
16131    function, because they do not need to be looked up, but it is
16132    simpler to check for this special case here, rather than at the
16133    call-sites.
16134
16135    In cases not explicitly covered above, this function returns a
16136    DECL, OVERLOAD, or baselink representing the result of the lookup.
16137    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16138    is returned.
16139
16140    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16141    (e.g., "struct") that was used.  In that case bindings that do not
16142    refer to types are ignored.
16143
16144    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16145    ignored.
16146
16147    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16148    are ignored.
16149
16150    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16151    types.
16152
16153    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16154    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16155    NULL_TREE otherwise.  */
16156
16157 static tree
16158 cp_parser_lookup_name (cp_parser *parser, tree name,
16159                        enum tag_types tag_type,
16160                        bool is_template,
16161                        bool is_namespace,
16162                        bool check_dependency,
16163                        tree *ambiguous_decls)
16164 {
16165   int flags = 0;
16166   tree decl;
16167   tree object_type = parser->context->object_type;
16168
16169   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16170     flags |= LOOKUP_COMPLAIN;
16171
16172   /* Assume that the lookup will be unambiguous.  */
16173   if (ambiguous_decls)
16174     *ambiguous_decls = NULL_TREE;
16175
16176   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16177      no longer valid.  Note that if we are parsing tentatively, and
16178      the parse fails, OBJECT_TYPE will be automatically restored.  */
16179   parser->context->object_type = NULL_TREE;
16180
16181   if (name == error_mark_node)
16182     return error_mark_node;
16183
16184   /* A template-id has already been resolved; there is no lookup to
16185      do.  */
16186   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16187     return name;
16188   if (BASELINK_P (name))
16189     {
16190       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16191                   == TEMPLATE_ID_EXPR);
16192       return name;
16193     }
16194
16195   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16196      it should already have been checked to make sure that the name
16197      used matches the type being destroyed.  */
16198   if (TREE_CODE (name) == BIT_NOT_EXPR)
16199     {
16200       tree type;
16201
16202       /* Figure out to which type this destructor applies.  */
16203       if (parser->scope)
16204         type = parser->scope;
16205       else if (object_type)
16206         type = object_type;
16207       else
16208         type = current_class_type;
16209       /* If that's not a class type, there is no destructor.  */
16210       if (!type || !CLASS_TYPE_P (type))
16211         return error_mark_node;
16212       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16213         lazily_declare_fn (sfk_destructor, type);
16214       if (!CLASSTYPE_DESTRUCTORS (type))
16215           return error_mark_node;
16216       /* If it was a class type, return the destructor.  */
16217       return CLASSTYPE_DESTRUCTORS (type);
16218     }
16219
16220   /* By this point, the NAME should be an ordinary identifier.  If
16221      the id-expression was a qualified name, the qualifying scope is
16222      stored in PARSER->SCOPE at this point.  */
16223   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16224
16225   /* Perform the lookup.  */
16226   if (parser->scope)
16227     {
16228       bool dependent_p;
16229
16230       if (parser->scope == error_mark_node)
16231         return error_mark_node;
16232
16233       /* If the SCOPE is dependent, the lookup must be deferred until
16234          the template is instantiated -- unless we are explicitly
16235          looking up names in uninstantiated templates.  Even then, we
16236          cannot look up the name if the scope is not a class type; it
16237          might, for example, be a template type parameter.  */
16238       dependent_p = (TYPE_P (parser->scope)
16239                      && !(parser->in_declarator_p
16240                           && currently_open_class (parser->scope))
16241                      && dependent_type_p (parser->scope));
16242       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16243            && dependent_p)
16244         {
16245           if (tag_type)
16246             {
16247               tree type;
16248
16249               /* The resolution to Core Issue 180 says that `struct
16250                  A::B' should be considered a type-name, even if `A'
16251                  is dependent.  */
16252               type = make_typename_type (parser->scope, name, tag_type,
16253                                          /*complain=*/tf_error);
16254               decl = TYPE_NAME (type);
16255             }
16256           else if (is_template
16257                    && (cp_parser_next_token_ends_template_argument_p (parser)
16258                        || cp_lexer_next_token_is (parser->lexer,
16259                                                   CPP_CLOSE_PAREN)))
16260             decl = make_unbound_class_template (parser->scope,
16261                                                 name, NULL_TREE,
16262                                                 /*complain=*/tf_error);
16263           else
16264             decl = build_qualified_name (/*type=*/NULL_TREE,
16265                                          parser->scope, name,
16266                                          is_template);
16267         }
16268       else
16269         {
16270           tree pushed_scope = NULL_TREE;
16271
16272           /* If PARSER->SCOPE is a dependent type, then it must be a
16273              class type, and we must not be checking dependencies;
16274              otherwise, we would have processed this lookup above.  So
16275              that PARSER->SCOPE is not considered a dependent base by
16276              lookup_member, we must enter the scope here.  */
16277           if (dependent_p)
16278             pushed_scope = push_scope (parser->scope);
16279           /* If the PARSER->SCOPE is a template specialization, it
16280              may be instantiated during name lookup.  In that case,
16281              errors may be issued.  Even if we rollback the current
16282              tentative parse, those errors are valid.  */
16283           decl = lookup_qualified_name (parser->scope, name,
16284                                         tag_type != none_type,
16285                                         /*complain=*/true);
16286           if (pushed_scope)
16287             pop_scope (pushed_scope);
16288         }
16289       parser->qualifying_scope = parser->scope;
16290       parser->object_scope = NULL_TREE;
16291     }
16292   else if (object_type)
16293     {
16294       tree object_decl = NULL_TREE;
16295       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16296          OBJECT_TYPE is not a class.  */
16297       if (CLASS_TYPE_P (object_type))
16298         /* If the OBJECT_TYPE is a template specialization, it may
16299            be instantiated during name lookup.  In that case, errors
16300            may be issued.  Even if we rollback the current tentative
16301            parse, those errors are valid.  */
16302         object_decl = lookup_member (object_type,
16303                                      name,
16304                                      /*protect=*/0,
16305                                      tag_type != none_type);
16306       /* Look it up in the enclosing context, too.  */
16307       decl = lookup_name_real (name, tag_type != none_type,
16308                                /*nonclass=*/0,
16309                                /*block_p=*/true, is_namespace, flags);
16310       parser->object_scope = object_type;
16311       parser->qualifying_scope = NULL_TREE;
16312       if (object_decl)
16313         decl = object_decl;
16314     }
16315   else
16316     {
16317       decl = lookup_name_real (name, tag_type != none_type,
16318                                /*nonclass=*/0,
16319                                /*block_p=*/true, is_namespace, flags);
16320       parser->qualifying_scope = NULL_TREE;
16321       parser->object_scope = NULL_TREE;
16322     }
16323
16324   /* If the lookup failed, let our caller know.  */
16325   if (!decl || decl == error_mark_node)
16326     return error_mark_node;
16327
16328   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16329   if (TREE_CODE (decl) == TREE_LIST)
16330     {
16331       if (ambiguous_decls)
16332         *ambiguous_decls = decl;
16333       /* The error message we have to print is too complicated for
16334          cp_parser_error, so we incorporate its actions directly.  */
16335       if (!cp_parser_simulate_error (parser))
16336         {
16337           error ("reference to %qD is ambiguous", name);
16338           print_candidates (decl);
16339         }
16340       return error_mark_node;
16341     }
16342
16343   gcc_assert (DECL_P (decl)
16344               || TREE_CODE (decl) == OVERLOAD
16345               || TREE_CODE (decl) == SCOPE_REF
16346               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16347               || BASELINK_P (decl));
16348
16349   /* If we have resolved the name of a member declaration, check to
16350      see if the declaration is accessible.  When the name resolves to
16351      set of overloaded functions, accessibility is checked when
16352      overload resolution is done.
16353
16354      During an explicit instantiation, access is not checked at all,
16355      as per [temp.explicit].  */
16356   if (DECL_P (decl))
16357     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16358
16359   return decl;
16360 }
16361
16362 /* Like cp_parser_lookup_name, but for use in the typical case where
16363    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16364    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16365
16366 static tree
16367 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16368 {
16369   return cp_parser_lookup_name (parser, name,
16370                                 none_type,
16371                                 /*is_template=*/false,
16372                                 /*is_namespace=*/false,
16373                                 /*check_dependency=*/true,
16374                                 /*ambiguous_decls=*/NULL);
16375 }
16376
16377 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16378    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16379    true, the DECL indicates the class being defined in a class-head,
16380    or declared in an elaborated-type-specifier.
16381
16382    Otherwise, return DECL.  */
16383
16384 static tree
16385 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16386 {
16387   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16388      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16389
16390        struct A {
16391          template <typename T> struct B;
16392        };
16393
16394        template <typename T> struct A::B {};
16395
16396      Similarly, in an elaborated-type-specifier:
16397
16398        namespace N { struct X{}; }
16399
16400        struct A {
16401          template <typename T> friend struct N::X;
16402        };
16403
16404      However, if the DECL refers to a class type, and we are in
16405      the scope of the class, then the name lookup automatically
16406      finds the TYPE_DECL created by build_self_reference rather
16407      than a TEMPLATE_DECL.  For example, in:
16408
16409        template <class T> struct S {
16410          S s;
16411        };
16412
16413      there is no need to handle such case.  */
16414
16415   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16416     return DECL_TEMPLATE_RESULT (decl);
16417
16418   return decl;
16419 }
16420
16421 /* If too many, or too few, template-parameter lists apply to the
16422    declarator, issue an error message.  Returns TRUE if all went well,
16423    and FALSE otherwise.  */
16424
16425 static bool
16426 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16427                                                 cp_declarator *declarator)
16428 {
16429   unsigned num_templates;
16430
16431   /* We haven't seen any classes that involve template parameters yet.  */
16432   num_templates = 0;
16433
16434   switch (declarator->kind)
16435     {
16436     case cdk_id:
16437       if (declarator->u.id.qualifying_scope)
16438         {
16439           tree scope;
16440           tree member;
16441
16442           scope = declarator->u.id.qualifying_scope;
16443           member = declarator->u.id.unqualified_name;
16444
16445           while (scope && CLASS_TYPE_P (scope))
16446             {
16447               /* You're supposed to have one `template <...>'
16448                  for every template class, but you don't need one
16449                  for a full specialization.  For example:
16450
16451                  template <class T> struct S{};
16452                  template <> struct S<int> { void f(); };
16453                  void S<int>::f () {}
16454
16455                  is correct; there shouldn't be a `template <>' for
16456                  the definition of `S<int>::f'.  */
16457               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16458                 /* If SCOPE does not have template information of any
16459                    kind, then it is not a template, nor is it nested
16460                    within a template.  */
16461                 break;
16462               if (explicit_class_specialization_p (scope))
16463                 break;
16464               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16465                 ++num_templates;
16466
16467               scope = TYPE_CONTEXT (scope);
16468             }
16469         }
16470       else if (TREE_CODE (declarator->u.id.unqualified_name)
16471                == TEMPLATE_ID_EXPR)
16472         /* If the DECLARATOR has the form `X<y>' then it uses one
16473            additional level of template parameters.  */
16474         ++num_templates;
16475
16476       return cp_parser_check_template_parameters (parser,
16477                                                   num_templates);
16478
16479     case cdk_function:
16480     case cdk_array:
16481     case cdk_pointer:
16482     case cdk_reference:
16483     case cdk_ptrmem:
16484       return (cp_parser_check_declarator_template_parameters
16485               (parser, declarator->declarator));
16486
16487     case cdk_error:
16488       return true;
16489
16490     default:
16491       gcc_unreachable ();
16492     }
16493   return false;
16494 }
16495
16496 /* NUM_TEMPLATES were used in the current declaration.  If that is
16497    invalid, return FALSE and issue an error messages.  Otherwise,
16498    return TRUE.  */
16499
16500 static bool
16501 cp_parser_check_template_parameters (cp_parser* parser,
16502                                      unsigned num_templates)
16503 {
16504   /* If there are more template classes than parameter lists, we have
16505      something like:
16506
16507        template <class T> void S<T>::R<T>::f ();  */
16508   if (parser->num_template_parameter_lists < num_templates)
16509     {
16510       error ("too few template-parameter-lists");
16511       return false;
16512     }
16513   /* If there are the same number of template classes and parameter
16514      lists, that's OK.  */
16515   if (parser->num_template_parameter_lists == num_templates)
16516     return true;
16517   /* If there are more, but only one more, then we are referring to a
16518      member template.  That's OK too.  */
16519   if (parser->num_template_parameter_lists == num_templates + 1)
16520       return true;
16521   /* Otherwise, there are too many template parameter lists.  We have
16522      something like:
16523
16524      template <class T> template <class U> void S::f();  */
16525   error ("too many template-parameter-lists");
16526   return false;
16527 }
16528
16529 /* Parse an optional `::' token indicating that the following name is
16530    from the global namespace.  If so, PARSER->SCOPE is set to the
16531    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16532    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16533    Returns the new value of PARSER->SCOPE, if the `::' token is
16534    present, and NULL_TREE otherwise.  */
16535
16536 static tree
16537 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16538 {
16539   cp_token *token;
16540
16541   /* Peek at the next token.  */
16542   token = cp_lexer_peek_token (parser->lexer);
16543   /* If we're looking at a `::' token then we're starting from the
16544      global namespace, not our current location.  */
16545   if (token->type == CPP_SCOPE)
16546     {
16547       /* Consume the `::' token.  */
16548       cp_lexer_consume_token (parser->lexer);
16549       /* Set the SCOPE so that we know where to start the lookup.  */
16550       parser->scope = global_namespace;
16551       parser->qualifying_scope = global_namespace;
16552       parser->object_scope = NULL_TREE;
16553
16554       return parser->scope;
16555     }
16556   else if (!current_scope_valid_p)
16557     {
16558       parser->scope = NULL_TREE;
16559       parser->qualifying_scope = NULL_TREE;
16560       parser->object_scope = NULL_TREE;
16561     }
16562
16563   return NULL_TREE;
16564 }
16565
16566 /* Returns TRUE if the upcoming token sequence is the start of a
16567    constructor declarator.  If FRIEND_P is true, the declarator is
16568    preceded by the `friend' specifier.  */
16569
16570 static bool
16571 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16572 {
16573   bool constructor_p;
16574   tree type_decl = NULL_TREE;
16575   bool nested_name_p;
16576   cp_token *next_token;
16577
16578   /* The common case is that this is not a constructor declarator, so
16579      try to avoid doing lots of work if at all possible.  It's not
16580      valid declare a constructor at function scope.  */
16581   if (parser->in_function_body)
16582     return false;
16583   /* And only certain tokens can begin a constructor declarator.  */
16584   next_token = cp_lexer_peek_token (parser->lexer);
16585   if (next_token->type != CPP_NAME
16586       && next_token->type != CPP_SCOPE
16587       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16588       && next_token->type != CPP_TEMPLATE_ID)
16589     return false;
16590
16591   /* Parse tentatively; we are going to roll back all of the tokens
16592      consumed here.  */
16593   cp_parser_parse_tentatively (parser);
16594   /* Assume that we are looking at a constructor declarator.  */
16595   constructor_p = true;
16596
16597   /* Look for the optional `::' operator.  */
16598   cp_parser_global_scope_opt (parser,
16599                               /*current_scope_valid_p=*/false);
16600   /* Look for the nested-name-specifier.  */
16601   nested_name_p
16602     = (cp_parser_nested_name_specifier_opt (parser,
16603                                             /*typename_keyword_p=*/false,
16604                                             /*check_dependency_p=*/false,
16605                                             /*type_p=*/false,
16606                                             /*is_declaration=*/false)
16607        != NULL_TREE);
16608   /* Outside of a class-specifier, there must be a
16609      nested-name-specifier.  */
16610   if (!nested_name_p &&
16611       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16612        || friend_p))
16613     constructor_p = false;
16614   /* If we still think that this might be a constructor-declarator,
16615      look for a class-name.  */
16616   if (constructor_p)
16617     {
16618       /* If we have:
16619
16620            template <typename T> struct S { S(); };
16621            template <typename T> S<T>::S ();
16622
16623          we must recognize that the nested `S' names a class.
16624          Similarly, for:
16625
16626            template <typename T> S<T>::S<T> ();
16627
16628          we must recognize that the nested `S' names a template.  */
16629       type_decl = cp_parser_class_name (parser,
16630                                         /*typename_keyword_p=*/false,
16631                                         /*template_keyword_p=*/false,
16632                                         none_type,
16633                                         /*check_dependency_p=*/false,
16634                                         /*class_head_p=*/false,
16635                                         /*is_declaration=*/false);
16636       /* If there was no class-name, then this is not a constructor.  */
16637       constructor_p = !cp_parser_error_occurred (parser);
16638     }
16639
16640   /* If we're still considering a constructor, we have to see a `(',
16641      to begin the parameter-declaration-clause, followed by either a
16642      `)', an `...', or a decl-specifier.  We need to check for a
16643      type-specifier to avoid being fooled into thinking that:
16644
16645        S::S (f) (int);
16646
16647      is a constructor.  (It is actually a function named `f' that
16648      takes one parameter (of type `int') and returns a value of type
16649      `S::S'.  */
16650   if (constructor_p
16651       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16652     {
16653       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16654           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16655           /* A parameter declaration begins with a decl-specifier,
16656              which is either the "attribute" keyword, a storage class
16657              specifier, or (usually) a type-specifier.  */
16658           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16659         {
16660           tree type;
16661           tree pushed_scope = NULL_TREE;
16662           unsigned saved_num_template_parameter_lists;
16663
16664           /* Names appearing in the type-specifier should be looked up
16665              in the scope of the class.  */
16666           if (current_class_type)
16667             type = NULL_TREE;
16668           else
16669             {
16670               type = TREE_TYPE (type_decl);
16671               if (TREE_CODE (type) == TYPENAME_TYPE)
16672                 {
16673                   type = resolve_typename_type (type,
16674                                                 /*only_current_p=*/false);
16675                   if (TREE_CODE (type) == TYPENAME_TYPE)
16676                     {
16677                       cp_parser_abort_tentative_parse (parser);
16678                       return false;
16679                     }
16680                 }
16681               pushed_scope = push_scope (type);
16682             }
16683
16684           /* Inside the constructor parameter list, surrounding
16685              template-parameter-lists do not apply.  */
16686           saved_num_template_parameter_lists
16687             = parser->num_template_parameter_lists;
16688           parser->num_template_parameter_lists = 0;
16689
16690           /* Look for the type-specifier.  */
16691           cp_parser_type_specifier (parser,
16692                                     CP_PARSER_FLAGS_NONE,
16693                                     /*decl_specs=*/NULL,
16694                                     /*is_declarator=*/true,
16695                                     /*declares_class_or_enum=*/NULL,
16696                                     /*is_cv_qualifier=*/NULL);
16697
16698           parser->num_template_parameter_lists
16699             = saved_num_template_parameter_lists;
16700
16701           /* Leave the scope of the class.  */
16702           if (pushed_scope)
16703             pop_scope (pushed_scope);
16704
16705           constructor_p = !cp_parser_error_occurred (parser);
16706         }
16707     }
16708   else
16709     constructor_p = false;
16710   /* We did not really want to consume any tokens.  */
16711   cp_parser_abort_tentative_parse (parser);
16712
16713   return constructor_p;
16714 }
16715
16716 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16717    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16718    they must be performed once we are in the scope of the function.
16719
16720    Returns the function defined.  */
16721
16722 static tree
16723 cp_parser_function_definition_from_specifiers_and_declarator
16724   (cp_parser* parser,
16725    cp_decl_specifier_seq *decl_specifiers,
16726    tree attributes,
16727    const cp_declarator *declarator)
16728 {
16729   tree fn;
16730   bool success_p;
16731
16732   /* Begin the function-definition.  */
16733   success_p = start_function (decl_specifiers, declarator, attributes);
16734
16735   /* The things we're about to see are not directly qualified by any
16736      template headers we've seen thus far.  */
16737   reset_specialization ();
16738
16739   /* If there were names looked up in the decl-specifier-seq that we
16740      did not check, check them now.  We must wait until we are in the
16741      scope of the function to perform the checks, since the function
16742      might be a friend.  */
16743   perform_deferred_access_checks ();
16744
16745   if (!success_p)
16746     {
16747       /* Skip the entire function.  */
16748       cp_parser_skip_to_end_of_block_or_statement (parser);
16749       fn = error_mark_node;
16750     }
16751   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16752     {
16753       /* Seen already, skip it.  An error message has already been output.  */
16754       cp_parser_skip_to_end_of_block_or_statement (parser);
16755       fn = current_function_decl;
16756       current_function_decl = NULL_TREE;
16757       /* If this is a function from a class, pop the nested class.  */
16758       if (current_class_name)
16759         pop_nested_class ();
16760     }
16761   else
16762     fn = cp_parser_function_definition_after_declarator (parser,
16763                                                          /*inline_p=*/false);
16764
16765   return fn;
16766 }
16767
16768 /* Parse the part of a function-definition that follows the
16769    declarator.  INLINE_P is TRUE iff this function is an inline
16770    function defined with a class-specifier.
16771
16772    Returns the function defined.  */
16773
16774 static tree
16775 cp_parser_function_definition_after_declarator (cp_parser* parser,
16776                                                 bool inline_p)
16777 {
16778   tree fn;
16779   bool ctor_initializer_p = false;
16780   bool saved_in_unbraced_linkage_specification_p;
16781   bool saved_in_function_body;
16782   unsigned saved_num_template_parameter_lists;
16783
16784   saved_in_function_body = parser->in_function_body;
16785   parser->in_function_body = true;
16786   /* If the next token is `return', then the code may be trying to
16787      make use of the "named return value" extension that G++ used to
16788      support.  */
16789   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16790     {
16791       /* Consume the `return' keyword.  */
16792       cp_lexer_consume_token (parser->lexer);
16793       /* Look for the identifier that indicates what value is to be
16794          returned.  */
16795       cp_parser_identifier (parser);
16796       /* Issue an error message.  */
16797       error ("named return values are no longer supported");
16798       /* Skip tokens until we reach the start of the function body.  */
16799       while (true)
16800         {
16801           cp_token *token = cp_lexer_peek_token (parser->lexer);
16802           if (token->type == CPP_OPEN_BRACE
16803               || token->type == CPP_EOF
16804               || token->type == CPP_PRAGMA_EOL)
16805             break;
16806           cp_lexer_consume_token (parser->lexer);
16807         }
16808     }
16809   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16810      anything declared inside `f'.  */
16811   saved_in_unbraced_linkage_specification_p
16812     = parser->in_unbraced_linkage_specification_p;
16813   parser->in_unbraced_linkage_specification_p = false;
16814   /* Inside the function, surrounding template-parameter-lists do not
16815      apply.  */
16816   saved_num_template_parameter_lists
16817     = parser->num_template_parameter_lists;
16818   parser->num_template_parameter_lists = 0;
16819   /* If the next token is `try', then we are looking at a
16820      function-try-block.  */
16821   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16822     ctor_initializer_p = cp_parser_function_try_block (parser);
16823   /* A function-try-block includes the function-body, so we only do
16824      this next part if we're not processing a function-try-block.  */
16825   else
16826     ctor_initializer_p
16827       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16828
16829   /* Finish the function.  */
16830   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16831                         (inline_p ? 2 : 0));
16832   /* Generate code for it, if necessary.  */
16833   expand_or_defer_fn (fn);
16834   /* Restore the saved values.  */
16835   parser->in_unbraced_linkage_specification_p
16836     = saved_in_unbraced_linkage_specification_p;
16837   parser->num_template_parameter_lists
16838     = saved_num_template_parameter_lists;
16839   parser->in_function_body = saved_in_function_body;
16840
16841   return fn;
16842 }
16843
16844 /* Parse a template-declaration, assuming that the `export' (and
16845    `extern') keywords, if present, has already been scanned.  MEMBER_P
16846    is as for cp_parser_template_declaration.  */
16847
16848 static void
16849 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16850 {
16851   tree decl = NULL_TREE;
16852   VEC (deferred_access_check,gc) *checks;
16853   tree parameter_list;
16854   bool friend_p = false;
16855   bool need_lang_pop;
16856
16857   /* Look for the `template' keyword.  */
16858   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16859     return;
16860
16861   /* And the `<'.  */
16862   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16863     return;
16864   if (at_class_scope_p () && current_function_decl)
16865     {
16866       /* 14.5.2.2 [temp.mem]
16867
16868          A local class shall not have member templates.  */
16869       error ("invalid declaration of member template in local class");
16870       cp_parser_skip_to_end_of_block_or_statement (parser);
16871       return;
16872     }
16873   /* [temp]
16874
16875      A template ... shall not have C linkage.  */
16876   if (current_lang_name == lang_name_c)
16877     {
16878       error ("template with C linkage");
16879       /* Give it C++ linkage to avoid confusing other parts of the
16880          front end.  */
16881       push_lang_context (lang_name_cplusplus);
16882       need_lang_pop = true;
16883     }
16884   else
16885     need_lang_pop = false;
16886
16887   /* We cannot perform access checks on the template parameter
16888      declarations until we know what is being declared, just as we
16889      cannot check the decl-specifier list.  */
16890   push_deferring_access_checks (dk_deferred);
16891
16892   /* If the next token is `>', then we have an invalid
16893      specialization.  Rather than complain about an invalid template
16894      parameter, issue an error message here.  */
16895   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16896     {
16897       cp_parser_error (parser, "invalid explicit specialization");
16898       begin_specialization ();
16899       parameter_list = NULL_TREE;
16900     }
16901   else
16902     /* Parse the template parameters.  */
16903     parameter_list = cp_parser_template_parameter_list (parser);
16904
16905   /* Get the deferred access checks from the parameter list.  These
16906      will be checked once we know what is being declared, as for a
16907      member template the checks must be performed in the scope of the
16908      class containing the member.  */
16909   checks = get_deferred_access_checks ();
16910
16911   /* Look for the `>'.  */
16912   cp_parser_skip_to_end_of_template_parameter_list (parser);
16913   /* We just processed one more parameter list.  */
16914   ++parser->num_template_parameter_lists;
16915   /* If the next token is `template', there are more template
16916      parameters.  */
16917   if (cp_lexer_next_token_is_keyword (parser->lexer,
16918                                       RID_TEMPLATE))
16919     cp_parser_template_declaration_after_export (parser, member_p);
16920   else
16921     {
16922       /* There are no access checks when parsing a template, as we do not
16923          know if a specialization will be a friend.  */
16924       push_deferring_access_checks (dk_no_check);
16925       decl = cp_parser_single_declaration (parser,
16926                                            checks,
16927                                            member_p,
16928                                            /*explicit_specialization_p=*/false,
16929                                            &friend_p);
16930       pop_deferring_access_checks ();
16931
16932       /* If this is a member template declaration, let the front
16933          end know.  */
16934       if (member_p && !friend_p && decl)
16935         {
16936           if (TREE_CODE (decl) == TYPE_DECL)
16937             cp_parser_check_access_in_redeclaration (decl);
16938
16939           decl = finish_member_template_decl (decl);
16940         }
16941       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16942         make_friend_class (current_class_type, TREE_TYPE (decl),
16943                            /*complain=*/true);
16944     }
16945   /* We are done with the current parameter list.  */
16946   --parser->num_template_parameter_lists;
16947
16948   pop_deferring_access_checks ();
16949
16950   /* Finish up.  */
16951   finish_template_decl (parameter_list);
16952
16953   /* Register member declarations.  */
16954   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16955     finish_member_declaration (decl);
16956   /* For the erroneous case of a template with C linkage, we pushed an
16957      implicit C++ linkage scope; exit that scope now.  */
16958   if (need_lang_pop)
16959     pop_lang_context ();
16960   /* If DECL is a function template, we must return to parse it later.
16961      (Even though there is no definition, there might be default
16962      arguments that need handling.)  */
16963   if (member_p && decl
16964       && (TREE_CODE (decl) == FUNCTION_DECL
16965           || DECL_FUNCTION_TEMPLATE_P (decl)))
16966     TREE_VALUE (parser->unparsed_functions_queues)
16967       = tree_cons (NULL_TREE, decl,
16968                    TREE_VALUE (parser->unparsed_functions_queues));
16969 }
16970
16971 /* Perform the deferred access checks from a template-parameter-list.
16972    CHECKS is a TREE_LIST of access checks, as returned by
16973    get_deferred_access_checks.  */
16974
16975 static void
16976 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16977 {
16978   ++processing_template_parmlist;
16979   perform_access_checks (checks);
16980   --processing_template_parmlist;
16981 }
16982
16983 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16984    `function-definition' sequence.  MEMBER_P is true, this declaration
16985    appears in a class scope.
16986
16987    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16988    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16989
16990 static tree
16991 cp_parser_single_declaration (cp_parser* parser,
16992                               VEC (deferred_access_check,gc)* checks,
16993                               bool member_p,
16994                               bool explicit_specialization_p,
16995                               bool* friend_p)
16996 {
16997   int declares_class_or_enum;
16998   tree decl = NULL_TREE;
16999   cp_decl_specifier_seq decl_specifiers;
17000   bool function_definition_p = false;
17001
17002   /* This function is only used when processing a template
17003      declaration.  */
17004   gcc_assert (innermost_scope_kind () == sk_template_parms
17005               || innermost_scope_kind () == sk_template_spec);
17006
17007   /* Defer access checks until we know what is being declared.  */
17008   push_deferring_access_checks (dk_deferred);
17009
17010   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17011      alternative.  */
17012   cp_parser_decl_specifier_seq (parser,
17013                                 CP_PARSER_FLAGS_OPTIONAL,
17014                                 &decl_specifiers,
17015                                 &declares_class_or_enum);
17016   if (friend_p)
17017     *friend_p = cp_parser_friend_p (&decl_specifiers);
17018
17019   /* There are no template typedefs.  */
17020   if (decl_specifiers.specs[(int) ds_typedef])
17021     {
17022       error ("template declaration of %qs", "typedef");
17023       decl = error_mark_node;
17024     }
17025
17026   /* Gather up the access checks that occurred the
17027      decl-specifier-seq.  */
17028   stop_deferring_access_checks ();
17029
17030   /* Check for the declaration of a template class.  */
17031   if (declares_class_or_enum)
17032     {
17033       if (cp_parser_declares_only_class_p (parser))
17034         {
17035           decl = shadow_tag (&decl_specifiers);
17036
17037           /* In this case:
17038
17039                struct C {
17040                  friend template <typename T> struct A<T>::B;
17041                };
17042
17043              A<T>::B will be represented by a TYPENAME_TYPE, and
17044              therefore not recognized by shadow_tag.  */
17045           if (friend_p && *friend_p
17046               && !decl
17047               && decl_specifiers.type
17048               && TYPE_P (decl_specifiers.type))
17049             decl = decl_specifiers.type;
17050
17051           if (decl && decl != error_mark_node)
17052             decl = TYPE_NAME (decl);
17053           else
17054             decl = error_mark_node;
17055
17056           /* Perform access checks for template parameters.  */
17057           cp_parser_perform_template_parameter_access_checks (checks);
17058         }
17059     }
17060   /* If it's not a template class, try for a template function.  If
17061      the next token is a `;', then this declaration does not declare
17062      anything.  But, if there were errors in the decl-specifiers, then
17063      the error might well have come from an attempted class-specifier.
17064      In that case, there's no need to warn about a missing declarator.  */
17065   if (!decl
17066       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17067           || decl_specifiers.type != error_mark_node))
17068     {
17069       decl = cp_parser_init_declarator (parser,
17070                                         &decl_specifiers,
17071                                         checks,
17072                                         /*function_definition_allowed_p=*/true,
17073                                         member_p,
17074                                         declares_class_or_enum,
17075                                         &function_definition_p);
17076
17077     /* 7.1.1-1 [dcl.stc]
17078
17079        A storage-class-specifier shall not be specified in an explicit
17080        specialization...  */
17081     if (decl
17082         && explicit_specialization_p
17083         && decl_specifiers.storage_class != sc_none)
17084       {
17085         error ("explicit template specialization cannot have a storage class");
17086         decl = error_mark_node;
17087       }
17088     }
17089
17090   pop_deferring_access_checks ();
17091
17092   /* Clear any current qualification; whatever comes next is the start
17093      of something new.  */
17094   parser->scope = NULL_TREE;
17095   parser->qualifying_scope = NULL_TREE;
17096   parser->object_scope = NULL_TREE;
17097   /* Look for a trailing `;' after the declaration.  */
17098   if (!function_definition_p
17099       && (decl == error_mark_node
17100           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17101     cp_parser_skip_to_end_of_block_or_statement (parser);
17102
17103   return decl;
17104 }
17105
17106 /* Parse a cast-expression that is not the operand of a unary "&".  */
17107
17108 static tree
17109 cp_parser_simple_cast_expression (cp_parser *parser)
17110 {
17111   return cp_parser_cast_expression (parser, /*address_p=*/false,
17112                                     /*cast_p=*/false);
17113 }
17114
17115 /* Parse a functional cast to TYPE.  Returns an expression
17116    representing the cast.  */
17117
17118 static tree
17119 cp_parser_functional_cast (cp_parser* parser, tree type)
17120 {
17121   tree expression_list;
17122   tree cast;
17123
17124   expression_list
17125     = cp_parser_parenthesized_expression_list (parser, false,
17126                                                /*cast_p=*/true,
17127                                                /*allow_expansion_p=*/true,
17128                                                /*non_constant_p=*/NULL);
17129
17130   cast = build_functional_cast (type, expression_list);
17131   /* [expr.const]/1: In an integral constant expression "only type
17132      conversions to integral or enumeration type can be used".  */
17133   if (TREE_CODE (type) == TYPE_DECL)
17134     type = TREE_TYPE (type);
17135   if (cast != error_mark_node
17136       && !cast_valid_in_integral_constant_expression_p (type)
17137       && (cp_parser_non_integral_constant_expression
17138           (parser, "a call to a constructor")))
17139     return error_mark_node;
17140   return cast;
17141 }
17142
17143 /* Save the tokens that make up the body of a member function defined
17144    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17145    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17146    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17147    for the member function.  */
17148
17149 static tree
17150 cp_parser_save_member_function_body (cp_parser* parser,
17151                                      cp_decl_specifier_seq *decl_specifiers,
17152                                      cp_declarator *declarator,
17153                                      tree attributes)
17154 {
17155   cp_token *first;
17156   cp_token *last;
17157   tree fn;
17158
17159   /* Create the function-declaration.  */
17160   fn = start_method (decl_specifiers, declarator, attributes);
17161   /* If something went badly wrong, bail out now.  */
17162   if (fn == error_mark_node)
17163     {
17164       /* If there's a function-body, skip it.  */
17165       if (cp_parser_token_starts_function_definition_p
17166           (cp_lexer_peek_token (parser->lexer)))
17167         cp_parser_skip_to_end_of_block_or_statement (parser);
17168       return error_mark_node;
17169     }
17170
17171   /* Remember it, if there default args to post process.  */
17172   cp_parser_save_default_args (parser, fn);
17173
17174   /* Save away the tokens that make up the body of the
17175      function.  */
17176   first = parser->lexer->next_token;
17177   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17178   /* Handle function try blocks.  */
17179   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17180     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17181   last = parser->lexer->next_token;
17182
17183   /* Save away the inline definition; we will process it when the
17184      class is complete.  */
17185   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17186   DECL_PENDING_INLINE_P (fn) = 1;
17187
17188   /* We need to know that this was defined in the class, so that
17189      friend templates are handled correctly.  */
17190   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17191
17192   /* We're done with the inline definition.  */
17193   finish_method (fn);
17194
17195   /* Add FN to the queue of functions to be parsed later.  */
17196   TREE_VALUE (parser->unparsed_functions_queues)
17197     = tree_cons (NULL_TREE, fn,
17198                  TREE_VALUE (parser->unparsed_functions_queues));
17199
17200   return fn;
17201 }
17202
17203 /* Parse a template-argument-list, as well as the trailing ">" (but
17204    not the opening ">").  See cp_parser_template_argument_list for the
17205    return value.  */
17206
17207 static tree
17208 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17209 {
17210   tree arguments;
17211   tree saved_scope;
17212   tree saved_qualifying_scope;
17213   tree saved_object_scope;
17214   bool saved_greater_than_is_operator_p;
17215   bool saved_skip_evaluation;
17216
17217   /* [temp.names]
17218
17219      When parsing a template-id, the first non-nested `>' is taken as
17220      the end of the template-argument-list rather than a greater-than
17221      operator.  */
17222   saved_greater_than_is_operator_p
17223     = parser->greater_than_is_operator_p;
17224   parser->greater_than_is_operator_p = false;
17225   /* Parsing the argument list may modify SCOPE, so we save it
17226      here.  */
17227   saved_scope = parser->scope;
17228   saved_qualifying_scope = parser->qualifying_scope;
17229   saved_object_scope = parser->object_scope;
17230   /* We need to evaluate the template arguments, even though this
17231      template-id may be nested within a "sizeof".  */
17232   saved_skip_evaluation = skip_evaluation;
17233   skip_evaluation = false;
17234   /* Parse the template-argument-list itself.  */
17235   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17236       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17237     arguments = NULL_TREE;
17238   else
17239     arguments = cp_parser_template_argument_list (parser);
17240   /* Look for the `>' that ends the template-argument-list. If we find
17241      a '>>' instead, it's probably just a typo.  */
17242   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17243     {
17244       if (cxx_dialect != cxx98)
17245         {
17246           /* In C++0x, a `>>' in a template argument list or cast
17247              expression is considered to be two separate `>'
17248              tokens. So, change the current token to a `>', but don't
17249              consume it: it will be consumed later when the outer
17250              template argument list (or cast expression) is parsed.
17251              Note that this replacement of `>' for `>>' is necessary
17252              even if we are parsing tentatively: in the tentative
17253              case, after calling
17254              cp_parser_enclosed_template_argument_list we will always
17255              throw away all of the template arguments and the first
17256              closing `>', either because the template argument list
17257              was erroneous or because we are replacing those tokens
17258              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17259              not have been thrown away) is needed either to close an
17260              outer template argument list or to complete a new-style
17261              cast.  */
17262           cp_token *token = cp_lexer_peek_token (parser->lexer);
17263           token->type = CPP_GREATER;
17264         }
17265       else if (!saved_greater_than_is_operator_p)
17266         {
17267           /* If we're in a nested template argument list, the '>>' has
17268             to be a typo for '> >'. We emit the error message, but we
17269             continue parsing and we push a '>' as next token, so that
17270             the argument list will be parsed correctly.  Note that the
17271             global source location is still on the token before the
17272             '>>', so we need to say explicitly where we want it.  */
17273           cp_token *token = cp_lexer_peek_token (parser->lexer);
17274           error ("%H%<>>%> should be %<> >%> "
17275                  "within a nested template argument list",
17276                  &token->location);
17277
17278           token->type = CPP_GREATER;
17279         }
17280       else
17281         {
17282           /* If this is not a nested template argument list, the '>>'
17283             is a typo for '>'. Emit an error message and continue.
17284             Same deal about the token location, but here we can get it
17285             right by consuming the '>>' before issuing the diagnostic.  */
17286           cp_lexer_consume_token (parser->lexer);
17287           error ("spurious %<>>%>, use %<>%> to terminate "
17288                  "a template argument list");
17289         }
17290     }
17291   else
17292     cp_parser_skip_to_end_of_template_parameter_list (parser);
17293   /* The `>' token might be a greater-than operator again now.  */
17294   parser->greater_than_is_operator_p
17295     = saved_greater_than_is_operator_p;
17296   /* Restore the SAVED_SCOPE.  */
17297   parser->scope = saved_scope;
17298   parser->qualifying_scope = saved_qualifying_scope;
17299   parser->object_scope = saved_object_scope;
17300   skip_evaluation = saved_skip_evaluation;
17301
17302   return arguments;
17303 }
17304
17305 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17306    arguments, or the body of the function have not yet been parsed,
17307    parse them now.  */
17308
17309 static void
17310 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17311 {
17312   /* If this member is a template, get the underlying
17313      FUNCTION_DECL.  */
17314   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17315     member_function = DECL_TEMPLATE_RESULT (member_function);
17316
17317   /* There should not be any class definitions in progress at this
17318      point; the bodies of members are only parsed outside of all class
17319      definitions.  */
17320   gcc_assert (parser->num_classes_being_defined == 0);
17321   /* While we're parsing the member functions we might encounter more
17322      classes.  We want to handle them right away, but we don't want
17323      them getting mixed up with functions that are currently in the
17324      queue.  */
17325   parser->unparsed_functions_queues
17326     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17327
17328   /* Make sure that any template parameters are in scope.  */
17329   maybe_begin_member_template_processing (member_function);
17330
17331   /* If the body of the function has not yet been parsed, parse it
17332      now.  */
17333   if (DECL_PENDING_INLINE_P (member_function))
17334     {
17335       tree function_scope;
17336       cp_token_cache *tokens;
17337
17338       /* The function is no longer pending; we are processing it.  */
17339       tokens = DECL_PENDING_INLINE_INFO (member_function);
17340       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17341       DECL_PENDING_INLINE_P (member_function) = 0;
17342
17343       /* If this is a local class, enter the scope of the containing
17344          function.  */
17345       function_scope = current_function_decl;
17346       if (function_scope)
17347         push_function_context_to (function_scope);
17348
17349
17350       /* Push the body of the function onto the lexer stack.  */
17351       cp_parser_push_lexer_for_tokens (parser, tokens);
17352
17353       /* Let the front end know that we going to be defining this
17354          function.  */
17355       start_preparsed_function (member_function, NULL_TREE,
17356                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17357
17358       /* Don't do access checking if it is a templated function.  */
17359       if (processing_template_decl)
17360         push_deferring_access_checks (dk_no_check);
17361
17362       /* Now, parse the body of the function.  */
17363       cp_parser_function_definition_after_declarator (parser,
17364                                                       /*inline_p=*/true);
17365
17366       if (processing_template_decl)
17367         pop_deferring_access_checks ();
17368
17369       /* Leave the scope of the containing function.  */
17370       if (function_scope)
17371         pop_function_context_from (function_scope);
17372       cp_parser_pop_lexer (parser);
17373     }
17374
17375   /* Remove any template parameters from the symbol table.  */
17376   maybe_end_member_template_processing ();
17377
17378   /* Restore the queue.  */
17379   parser->unparsed_functions_queues
17380     = TREE_CHAIN (parser->unparsed_functions_queues);
17381 }
17382
17383 /* If DECL contains any default args, remember it on the unparsed
17384    functions queue.  */
17385
17386 static void
17387 cp_parser_save_default_args (cp_parser* parser, tree decl)
17388 {
17389   tree probe;
17390
17391   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17392        probe;
17393        probe = TREE_CHAIN (probe))
17394     if (TREE_PURPOSE (probe))
17395       {
17396         TREE_PURPOSE (parser->unparsed_functions_queues)
17397           = tree_cons (current_class_type, decl,
17398                        TREE_PURPOSE (parser->unparsed_functions_queues));
17399         break;
17400       }
17401 }
17402
17403 /* FN is a FUNCTION_DECL which may contains a parameter with an
17404    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17405    assumes that the current scope is the scope in which the default
17406    argument should be processed.  */
17407
17408 static void
17409 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17410 {
17411   bool saved_local_variables_forbidden_p;
17412   tree parm;
17413
17414   /* While we're parsing the default args, we might (due to the
17415      statement expression extension) encounter more classes.  We want
17416      to handle them right away, but we don't want them getting mixed
17417      up with default args that are currently in the queue.  */
17418   parser->unparsed_functions_queues
17419     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17420
17421   /* Local variable names (and the `this' keyword) may not appear
17422      in a default argument.  */
17423   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17424   parser->local_variables_forbidden_p = true;
17425
17426   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17427        parm;
17428        parm = TREE_CHAIN (parm))
17429     {
17430       cp_token_cache *tokens;
17431       tree default_arg = TREE_PURPOSE (parm);
17432       tree parsed_arg;
17433       VEC(tree,gc) *insts;
17434       tree copy;
17435       unsigned ix;
17436
17437       if (!default_arg)
17438         continue;
17439
17440       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17441         /* This can happen for a friend declaration for a function
17442            already declared with default arguments.  */
17443         continue;
17444
17445        /* Push the saved tokens for the default argument onto the parser's
17446           lexer stack.  */
17447       tokens = DEFARG_TOKENS (default_arg);
17448       cp_parser_push_lexer_for_tokens (parser, tokens);
17449
17450       /* Parse the assignment-expression.  */
17451       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17452
17453       if (!processing_template_decl)
17454         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17455
17456       TREE_PURPOSE (parm) = parsed_arg;
17457
17458       /* Update any instantiations we've already created.  */
17459       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17460            VEC_iterate (tree, insts, ix, copy); ix++)
17461         TREE_PURPOSE (copy) = parsed_arg;
17462
17463       /* If the token stream has not been completely used up, then
17464          there was extra junk after the end of the default
17465          argument.  */
17466       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17467         cp_parser_error (parser, "expected %<,%>");
17468
17469       /* Revert to the main lexer.  */
17470       cp_parser_pop_lexer (parser);
17471     }
17472
17473   /* Make sure no default arg is missing.  */
17474   check_default_args (fn);
17475
17476   /* Restore the state of local_variables_forbidden_p.  */
17477   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17478
17479   /* Restore the queue.  */
17480   parser->unparsed_functions_queues
17481     = TREE_CHAIN (parser->unparsed_functions_queues);
17482 }
17483
17484 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17485    either a TYPE or an expression, depending on the form of the
17486    input.  The KEYWORD indicates which kind of expression we have
17487    encountered.  */
17488
17489 static tree
17490 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17491 {
17492   static const char *format;
17493   tree expr = NULL_TREE;
17494   const char *saved_message;
17495   char *tmp;
17496   bool saved_integral_constant_expression_p;
17497   bool saved_non_integral_constant_expression_p;
17498   bool pack_expansion_p = false;
17499
17500   /* Initialize FORMAT the first time we get here.  */
17501   if (!format)
17502     format = "types may not be defined in '%s' expressions";
17503
17504   /* Types cannot be defined in a `sizeof' expression.  Save away the
17505      old message.  */
17506   saved_message = parser->type_definition_forbidden_message;
17507   /* And create the new one.  */
17508   parser->type_definition_forbidden_message = tmp
17509     = XNEWVEC (char, strlen (format)
17510                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17511                + 1 /* `\0' */);
17512   sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17513
17514   /* The restrictions on constant-expressions do not apply inside
17515      sizeof expressions.  */
17516   saved_integral_constant_expression_p
17517     = parser->integral_constant_expression_p;
17518   saved_non_integral_constant_expression_p
17519     = parser->non_integral_constant_expression_p;
17520   parser->integral_constant_expression_p = false;
17521
17522   /* If it's a `...', then we are computing the length of a parameter
17523      pack.  */
17524   if (keyword == RID_SIZEOF
17525       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17526     {
17527       /* Consume the `...'.  */
17528       cp_lexer_consume_token (parser->lexer);
17529       maybe_warn_variadic_templates ();
17530
17531       /* Note that this is an expansion.  */
17532       pack_expansion_p = true;
17533     }
17534
17535   /* Do not actually evaluate the expression.  */
17536   ++skip_evaluation;
17537   /* If it's a `(', then we might be looking at the type-id
17538      construction.  */
17539   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17540     {
17541       tree type;
17542       bool saved_in_type_id_in_expr_p;
17543
17544       /* We can't be sure yet whether we're looking at a type-id or an
17545          expression.  */
17546       cp_parser_parse_tentatively (parser);
17547       /* Consume the `('.  */
17548       cp_lexer_consume_token (parser->lexer);
17549       /* Parse the type-id.  */
17550       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17551       parser->in_type_id_in_expr_p = true;
17552       type = cp_parser_type_id (parser);
17553       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17554       /* Now, look for the trailing `)'.  */
17555       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17556       /* If all went well, then we're done.  */
17557       if (cp_parser_parse_definitely (parser))
17558         {
17559           cp_decl_specifier_seq decl_specs;
17560
17561           /* Build a trivial decl-specifier-seq.  */
17562           clear_decl_specs (&decl_specs);
17563           decl_specs.type = type;
17564
17565           /* Call grokdeclarator to figure out what type this is.  */
17566           expr = grokdeclarator (NULL,
17567                                  &decl_specs,
17568                                  TYPENAME,
17569                                  /*initialized=*/0,
17570                                  /*attrlist=*/NULL);
17571         }
17572     }
17573
17574   /* If the type-id production did not work out, then we must be
17575      looking at the unary-expression production.  */
17576   if (!expr)
17577     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17578                                        /*cast_p=*/false);
17579
17580   if (pack_expansion_p)
17581     /* Build a pack expansion. */
17582     expr = make_pack_expansion (expr);
17583
17584   /* Go back to evaluating expressions.  */
17585   --skip_evaluation;
17586
17587   /* Free the message we created.  */
17588   free (tmp);
17589   /* And restore the old one.  */
17590   parser->type_definition_forbidden_message = saved_message;
17591   parser->integral_constant_expression_p
17592     = saved_integral_constant_expression_p;
17593   parser->non_integral_constant_expression_p
17594     = saved_non_integral_constant_expression_p;
17595
17596   return expr;
17597 }
17598
17599 /* If the current declaration has no declarator, return true.  */
17600
17601 static bool
17602 cp_parser_declares_only_class_p (cp_parser *parser)
17603 {
17604   /* If the next token is a `;' or a `,' then there is no
17605      declarator.  */
17606   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17607           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17608 }
17609
17610 /* Update the DECL_SPECS to reflect the storage class indicated by
17611    KEYWORD.  */
17612
17613 static void
17614 cp_parser_set_storage_class (cp_parser *parser,
17615                              cp_decl_specifier_seq *decl_specs,
17616                              enum rid keyword)
17617 {
17618   cp_storage_class storage_class;
17619
17620   if (parser->in_unbraced_linkage_specification_p)
17621     {
17622       error ("invalid use of %qD in linkage specification",
17623              ridpointers[keyword]);
17624       return;
17625     }
17626   else if (decl_specs->storage_class != sc_none)
17627     {
17628       decl_specs->conflicting_specifiers_p = true;
17629       return;
17630     }
17631
17632   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17633       && decl_specs->specs[(int) ds_thread])
17634     {
17635       error ("%<__thread%> before %qD", ridpointers[keyword]);
17636       decl_specs->specs[(int) ds_thread] = 0;
17637     }
17638
17639   switch (keyword)
17640     {
17641     case RID_AUTO:
17642       storage_class = sc_auto;
17643       break;
17644     case RID_REGISTER:
17645       storage_class = sc_register;
17646       break;
17647     case RID_STATIC:
17648       storage_class = sc_static;
17649       break;
17650     case RID_EXTERN:
17651       storage_class = sc_extern;
17652       break;
17653     case RID_MUTABLE:
17654       storage_class = sc_mutable;
17655       break;
17656     default:
17657       gcc_unreachable ();
17658     }
17659   decl_specs->storage_class = storage_class;
17660
17661   /* A storage class specifier cannot be applied alongside a typedef 
17662      specifier. If there is a typedef specifier present then set 
17663      conflicting_specifiers_p which will trigger an error later
17664      on in grokdeclarator. */
17665   if (decl_specs->specs[(int)ds_typedef])
17666     decl_specs->conflicting_specifiers_p = true;
17667 }
17668
17669 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17670    is true, the type is a user-defined type; otherwise it is a
17671    built-in type specified by a keyword.  */
17672
17673 static void
17674 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17675                               tree type_spec,
17676                               bool user_defined_p)
17677 {
17678   decl_specs->any_specifiers_p = true;
17679
17680   /* If the user tries to redeclare bool or wchar_t (with, for
17681      example, in "typedef int wchar_t;") we remember that this is what
17682      happened.  In system headers, we ignore these declarations so
17683      that G++ can work with system headers that are not C++-safe.  */
17684   if (decl_specs->specs[(int) ds_typedef]
17685       && !user_defined_p
17686       && (type_spec == boolean_type_node
17687           || type_spec == wchar_type_node)
17688       && (decl_specs->type
17689           || decl_specs->specs[(int) ds_long]
17690           || decl_specs->specs[(int) ds_short]
17691           || decl_specs->specs[(int) ds_unsigned]
17692           || decl_specs->specs[(int) ds_signed]))
17693     {
17694       decl_specs->redefined_builtin_type = type_spec;
17695       if (!decl_specs->type)
17696         {
17697           decl_specs->type = type_spec;
17698           decl_specs->user_defined_type_p = false;
17699         }
17700     }
17701   else if (decl_specs->type)
17702     decl_specs->multiple_types_p = true;
17703   else
17704     {
17705       decl_specs->type = type_spec;
17706       decl_specs->user_defined_type_p = user_defined_p;
17707       decl_specs->redefined_builtin_type = NULL_TREE;
17708     }
17709 }
17710
17711 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17712    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17713
17714 static bool
17715 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17716 {
17717   return decl_specifiers->specs[(int) ds_friend] != 0;
17718 }
17719
17720 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17721    issue an error message indicating that TOKEN_DESC was expected.
17722
17723    Returns the token consumed, if the token had the appropriate type.
17724    Otherwise, returns NULL.  */
17725
17726 static cp_token *
17727 cp_parser_require (cp_parser* parser,
17728                    enum cpp_ttype type,
17729                    const char* token_desc)
17730 {
17731   if (cp_lexer_next_token_is (parser->lexer, type))
17732     return cp_lexer_consume_token (parser->lexer);
17733   else
17734     {
17735       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17736       if (!cp_parser_simulate_error (parser))
17737         {
17738           char *message = concat ("expected ", token_desc, NULL);
17739           cp_parser_error (parser, message);
17740           free (message);
17741         }
17742       return NULL;
17743     }
17744 }
17745
17746 /* An error message is produced if the next token is not '>'.
17747    All further tokens are skipped until the desired token is
17748    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17749
17750 static void
17751 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17752 {
17753   /* Current level of '< ... >'.  */
17754   unsigned level = 0;
17755   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17756   unsigned nesting_depth = 0;
17757
17758   /* Are we ready, yet?  If not, issue error message.  */
17759   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17760     return;
17761
17762   /* Skip tokens until the desired token is found.  */
17763   while (true)
17764     {
17765       /* Peek at the next token.  */
17766       switch (cp_lexer_peek_token (parser->lexer)->type)
17767         {
17768         case CPP_LESS:
17769           if (!nesting_depth)
17770             ++level;
17771           break;
17772
17773         case CPP_RSHIFT:
17774           if (cxx_dialect == cxx98)
17775             /* C++0x views the `>>' operator as two `>' tokens, but
17776                C++98 does not. */
17777             break;
17778           else if (!nesting_depth && level-- == 0)
17779             {
17780               /* We've hit a `>>' where the first `>' closes the
17781                  template argument list, and the second `>' is
17782                  spurious.  Just consume the `>>' and stop; we've
17783                  already produced at least one error.  */
17784               cp_lexer_consume_token (parser->lexer);
17785               return;
17786             }
17787           /* Fall through for C++0x, so we handle the second `>' in
17788              the `>>'.  */
17789
17790         case CPP_GREATER:
17791           if (!nesting_depth && level-- == 0)
17792             {
17793               /* We've reached the token we want, consume it and stop.  */
17794               cp_lexer_consume_token (parser->lexer);
17795               return;
17796             }
17797           break;
17798
17799         case CPP_OPEN_PAREN:
17800         case CPP_OPEN_SQUARE:
17801           ++nesting_depth;
17802           break;
17803
17804         case CPP_CLOSE_PAREN:
17805         case CPP_CLOSE_SQUARE:
17806           if (nesting_depth-- == 0)
17807             return;
17808           break;
17809
17810         case CPP_EOF:
17811         case CPP_PRAGMA_EOL:
17812         case CPP_SEMICOLON:
17813         case CPP_OPEN_BRACE:
17814         case CPP_CLOSE_BRACE:
17815           /* The '>' was probably forgotten, don't look further.  */
17816           return;
17817
17818         default:
17819           break;
17820         }
17821
17822       /* Consume this token.  */
17823       cp_lexer_consume_token (parser->lexer);
17824     }
17825 }
17826
17827 /* If the next token is the indicated keyword, consume it.  Otherwise,
17828    issue an error message indicating that TOKEN_DESC was expected.
17829
17830    Returns the token consumed, if the token had the appropriate type.
17831    Otherwise, returns NULL.  */
17832
17833 static cp_token *
17834 cp_parser_require_keyword (cp_parser* parser,
17835                            enum rid keyword,
17836                            const char* token_desc)
17837 {
17838   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17839
17840   if (token && token->keyword != keyword)
17841     {
17842       dyn_string_t error_msg;
17843
17844       /* Format the error message.  */
17845       error_msg = dyn_string_new (0);
17846       dyn_string_append_cstr (error_msg, "expected ");
17847       dyn_string_append_cstr (error_msg, token_desc);
17848       cp_parser_error (parser, error_msg->s);
17849       dyn_string_delete (error_msg);
17850       return NULL;
17851     }
17852
17853   return token;
17854 }
17855
17856 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17857    function-definition.  */
17858
17859 static bool
17860 cp_parser_token_starts_function_definition_p (cp_token* token)
17861 {
17862   return (/* An ordinary function-body begins with an `{'.  */
17863           token->type == CPP_OPEN_BRACE
17864           /* A ctor-initializer begins with a `:'.  */
17865           || token->type == CPP_COLON
17866           /* A function-try-block begins with `try'.  */
17867           || token->keyword == RID_TRY
17868           /* The named return value extension begins with `return'.  */
17869           || token->keyword == RID_RETURN);
17870 }
17871
17872 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17873    definition.  */
17874
17875 static bool
17876 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17877 {
17878   cp_token *token;
17879
17880   token = cp_lexer_peek_token (parser->lexer);
17881   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17882 }
17883
17884 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17885    C++0x) ending a template-argument.  */
17886
17887 static bool
17888 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17889 {
17890   cp_token *token;
17891
17892   token = cp_lexer_peek_token (parser->lexer);
17893   return (token->type == CPP_COMMA 
17894           || token->type == CPP_GREATER
17895           || token->type == CPP_ELLIPSIS
17896           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17897 }
17898
17899 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17900    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17901
17902 static bool
17903 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17904                                                      size_t n)
17905 {
17906   cp_token *token;
17907
17908   token = cp_lexer_peek_nth_token (parser->lexer, n);
17909   if (token->type == CPP_LESS)
17910     return true;
17911   /* Check for the sequence `<::' in the original code. It would be lexed as
17912      `[:', where `[' is a digraph, and there is no whitespace before
17913      `:'.  */
17914   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17915     {
17916       cp_token *token2;
17917       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17918       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17919         return true;
17920     }
17921   return false;
17922 }
17923
17924 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17925    or none_type otherwise.  */
17926
17927 static enum tag_types
17928 cp_parser_token_is_class_key (cp_token* token)
17929 {
17930   switch (token->keyword)
17931     {
17932     case RID_CLASS:
17933       return class_type;
17934     case RID_STRUCT:
17935       return record_type;
17936     case RID_UNION:
17937       return union_type;
17938
17939     default:
17940       return none_type;
17941     }
17942 }
17943
17944 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17945
17946 static void
17947 cp_parser_check_class_key (enum tag_types class_key, tree type)
17948 {
17949   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17950     pedwarn ("%qs tag used in naming %q#T",
17951             class_key == union_type ? "union"
17952              : class_key == record_type ? "struct" : "class",
17953              type);
17954 }
17955
17956 /* Issue an error message if DECL is redeclared with different
17957    access than its original declaration [class.access.spec/3].
17958    This applies to nested classes and nested class templates.
17959    [class.mem/1].  */
17960
17961 static void
17962 cp_parser_check_access_in_redeclaration (tree decl)
17963 {
17964   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17965     return;
17966
17967   if ((TREE_PRIVATE (decl)
17968        != (current_access_specifier == access_private_node))
17969       || (TREE_PROTECTED (decl)
17970           != (current_access_specifier == access_protected_node)))
17971     error ("%qD redeclared with different access", decl);
17972 }
17973
17974 /* Look for the `template' keyword, as a syntactic disambiguator.
17975    Return TRUE iff it is present, in which case it will be
17976    consumed.  */
17977
17978 static bool
17979 cp_parser_optional_template_keyword (cp_parser *parser)
17980 {
17981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17982     {
17983       /* The `template' keyword can only be used within templates;
17984          outside templates the parser can always figure out what is a
17985          template and what is not.  */
17986       if (!processing_template_decl)
17987         {
17988           error ("%<template%> (as a disambiguator) is only allowed "
17989                  "within templates");
17990           /* If this part of the token stream is rescanned, the same
17991              error message would be generated.  So, we purge the token
17992              from the stream.  */
17993           cp_lexer_purge_token (parser->lexer);
17994           return false;
17995         }
17996       else
17997         {
17998           /* Consume the `template' keyword.  */
17999           cp_lexer_consume_token (parser->lexer);
18000           return true;
18001         }
18002     }
18003
18004   return false;
18005 }
18006
18007 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18008    set PARSER->SCOPE, and perform other related actions.  */
18009
18010 static void
18011 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18012 {
18013   int i;
18014   struct tree_check *check_value;
18015   deferred_access_check *chk;
18016   VEC (deferred_access_check,gc) *checks;
18017
18018   /* Get the stored value.  */
18019   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18020   /* Perform any access checks that were deferred.  */
18021   checks = check_value->checks;
18022   if (checks)
18023     {
18024       for (i = 0 ;
18025            VEC_iterate (deferred_access_check, checks, i, chk) ;
18026            ++i)
18027         {
18028           perform_or_defer_access_check (chk->binfo,
18029                                          chk->decl,
18030                                          chk->diag_decl);
18031         }
18032     }
18033   /* Set the scope from the stored value.  */
18034   parser->scope = check_value->value;
18035   parser->qualifying_scope = check_value->qualifying_scope;
18036   parser->object_scope = NULL_TREE;
18037 }
18038
18039 /* Consume tokens up through a non-nested END token.  */
18040
18041 static void
18042 cp_parser_cache_group (cp_parser *parser,
18043                        enum cpp_ttype end,
18044                        unsigned depth)
18045 {
18046   while (true)
18047     {
18048       cp_token *token;
18049
18050       /* Abort a parenthesized expression if we encounter a brace.  */
18051       if ((end == CPP_CLOSE_PAREN || depth == 0)
18052           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18053         return;
18054       /* If we've reached the end of the file, stop.  */
18055       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18056           || (end != CPP_PRAGMA_EOL
18057               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18058         return;
18059       /* Consume the next token.  */
18060       token = cp_lexer_consume_token (parser->lexer);
18061       /* See if it starts a new group.  */
18062       if (token->type == CPP_OPEN_BRACE)
18063         {
18064           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18065           if (depth == 0)
18066             return;
18067         }
18068       else if (token->type == CPP_OPEN_PAREN)
18069         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18070       else if (token->type == CPP_PRAGMA)
18071         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18072       else if (token->type == end)
18073         return;
18074     }
18075 }
18076
18077 /* Begin parsing tentatively.  We always save tokens while parsing
18078    tentatively so that if the tentative parsing fails we can restore the
18079    tokens.  */
18080
18081 static void
18082 cp_parser_parse_tentatively (cp_parser* parser)
18083 {
18084   /* Enter a new parsing context.  */
18085   parser->context = cp_parser_context_new (parser->context);
18086   /* Begin saving tokens.  */
18087   cp_lexer_save_tokens (parser->lexer);
18088   /* In order to avoid repetitive access control error messages,
18089      access checks are queued up until we are no longer parsing
18090      tentatively.  */
18091   push_deferring_access_checks (dk_deferred);
18092 }
18093
18094 /* Commit to the currently active tentative parse.  */
18095
18096 static void
18097 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18098 {
18099   cp_parser_context *context;
18100   cp_lexer *lexer;
18101
18102   /* Mark all of the levels as committed.  */
18103   lexer = parser->lexer;
18104   for (context = parser->context; context->next; context = context->next)
18105     {
18106       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18107         break;
18108       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18109       while (!cp_lexer_saving_tokens (lexer))
18110         lexer = lexer->next;
18111       cp_lexer_commit_tokens (lexer);
18112     }
18113 }
18114
18115 /* Abort the currently active tentative parse.  All consumed tokens
18116    will be rolled back, and no diagnostics will be issued.  */
18117
18118 static void
18119 cp_parser_abort_tentative_parse (cp_parser* parser)
18120 {
18121   cp_parser_simulate_error (parser);
18122   /* Now, pretend that we want to see if the construct was
18123      successfully parsed.  */
18124   cp_parser_parse_definitely (parser);
18125 }
18126
18127 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18128    token stream.  Otherwise, commit to the tokens we have consumed.
18129    Returns true if no error occurred; false otherwise.  */
18130
18131 static bool
18132 cp_parser_parse_definitely (cp_parser* parser)
18133 {
18134   bool error_occurred;
18135   cp_parser_context *context;
18136
18137   /* Remember whether or not an error occurred, since we are about to
18138      destroy that information.  */
18139   error_occurred = cp_parser_error_occurred (parser);
18140   /* Remove the topmost context from the stack.  */
18141   context = parser->context;
18142   parser->context = context->next;
18143   /* If no parse errors occurred, commit to the tentative parse.  */
18144   if (!error_occurred)
18145     {
18146       /* Commit to the tokens read tentatively, unless that was
18147          already done.  */
18148       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18149         cp_lexer_commit_tokens (parser->lexer);
18150
18151       pop_to_parent_deferring_access_checks ();
18152     }
18153   /* Otherwise, if errors occurred, roll back our state so that things
18154      are just as they were before we began the tentative parse.  */
18155   else
18156     {
18157       cp_lexer_rollback_tokens (parser->lexer);
18158       pop_deferring_access_checks ();
18159     }
18160   /* Add the context to the front of the free list.  */
18161   context->next = cp_parser_context_free_list;
18162   cp_parser_context_free_list = context;
18163
18164   return !error_occurred;
18165 }
18166
18167 /* Returns true if we are parsing tentatively and are not committed to
18168    this tentative parse.  */
18169
18170 static bool
18171 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18172 {
18173   return (cp_parser_parsing_tentatively (parser)
18174           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18175 }
18176
18177 /* Returns nonzero iff an error has occurred during the most recent
18178    tentative parse.  */
18179
18180 static bool
18181 cp_parser_error_occurred (cp_parser* parser)
18182 {
18183   return (cp_parser_parsing_tentatively (parser)
18184           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18185 }
18186
18187 /* Returns nonzero if GNU extensions are allowed.  */
18188
18189 static bool
18190 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18191 {
18192   return parser->allow_gnu_extensions_p;
18193 }
18194 \f
18195 /* Objective-C++ Productions */
18196
18197
18198 /* Parse an Objective-C expression, which feeds into a primary-expression
18199    above.
18200
18201    objc-expression:
18202      objc-message-expression
18203      objc-string-literal
18204      objc-encode-expression
18205      objc-protocol-expression
18206      objc-selector-expression
18207
18208   Returns a tree representation of the expression.  */
18209
18210 static tree
18211 cp_parser_objc_expression (cp_parser* parser)
18212 {
18213   /* Try to figure out what kind of declaration is present.  */
18214   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18215
18216   switch (kwd->type)
18217     {
18218     case CPP_OPEN_SQUARE:
18219       return cp_parser_objc_message_expression (parser);
18220
18221     case CPP_OBJC_STRING:
18222       kwd = cp_lexer_consume_token (parser->lexer);
18223       return objc_build_string_object (kwd->u.value);
18224
18225     case CPP_KEYWORD:
18226       switch (kwd->keyword)
18227         {
18228         case RID_AT_ENCODE:
18229           return cp_parser_objc_encode_expression (parser);
18230
18231         case RID_AT_PROTOCOL:
18232           return cp_parser_objc_protocol_expression (parser);
18233
18234         case RID_AT_SELECTOR:
18235           return cp_parser_objc_selector_expression (parser);
18236
18237         default:
18238           break;
18239         }
18240     default:
18241       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18242       cp_parser_skip_to_end_of_block_or_statement (parser);
18243     }
18244
18245   return error_mark_node;
18246 }
18247
18248 /* Parse an Objective-C message expression.
18249
18250    objc-message-expression:
18251      [ objc-message-receiver objc-message-args ]
18252
18253    Returns a representation of an Objective-C message.  */
18254
18255 static tree
18256 cp_parser_objc_message_expression (cp_parser* parser)
18257 {
18258   tree receiver, messageargs;
18259
18260   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18261   receiver = cp_parser_objc_message_receiver (parser);
18262   messageargs = cp_parser_objc_message_args (parser);
18263   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18264
18265   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18266 }
18267
18268 /* Parse an objc-message-receiver.
18269
18270    objc-message-receiver:
18271      expression
18272      simple-type-specifier
18273
18274   Returns a representation of the type or expression.  */
18275
18276 static tree
18277 cp_parser_objc_message_receiver (cp_parser* parser)
18278 {
18279   tree rcv;
18280
18281   /* An Objective-C message receiver may be either (1) a type
18282      or (2) an expression.  */
18283   cp_parser_parse_tentatively (parser);
18284   rcv = cp_parser_expression (parser, false);
18285
18286   if (cp_parser_parse_definitely (parser))
18287     return rcv;
18288
18289   rcv = cp_parser_simple_type_specifier (parser,
18290                                          /*decl_specs=*/NULL,
18291                                          CP_PARSER_FLAGS_NONE);
18292
18293   return objc_get_class_reference (rcv);
18294 }
18295
18296 /* Parse the arguments and selectors comprising an Objective-C message.
18297
18298    objc-message-args:
18299      objc-selector
18300      objc-selector-args
18301      objc-selector-args , objc-comma-args
18302
18303    objc-selector-args:
18304      objc-selector [opt] : assignment-expression
18305      objc-selector-args objc-selector [opt] : assignment-expression
18306
18307    objc-comma-args:
18308      assignment-expression
18309      objc-comma-args , assignment-expression
18310
18311    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18312    selector arguments and TREE_VALUE containing a list of comma
18313    arguments.  */
18314
18315 static tree
18316 cp_parser_objc_message_args (cp_parser* parser)
18317 {
18318   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18319   bool maybe_unary_selector_p = true;
18320   cp_token *token = cp_lexer_peek_token (parser->lexer);
18321
18322   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18323     {
18324       tree selector = NULL_TREE, arg;
18325
18326       if (token->type != CPP_COLON)
18327         selector = cp_parser_objc_selector (parser);
18328
18329       /* Detect if we have a unary selector.  */
18330       if (maybe_unary_selector_p
18331           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18332         return build_tree_list (selector, NULL_TREE);
18333
18334       maybe_unary_selector_p = false;
18335       cp_parser_require (parser, CPP_COLON, "`:'");
18336       arg = cp_parser_assignment_expression (parser, false);
18337
18338       sel_args
18339         = chainon (sel_args,
18340                    build_tree_list (selector, arg));
18341
18342       token = cp_lexer_peek_token (parser->lexer);
18343     }
18344
18345   /* Handle non-selector arguments, if any. */
18346   while (token->type == CPP_COMMA)
18347     {
18348       tree arg;
18349
18350       cp_lexer_consume_token (parser->lexer);
18351       arg = cp_parser_assignment_expression (parser, false);
18352
18353       addl_args
18354         = chainon (addl_args,
18355                    build_tree_list (NULL_TREE, arg));
18356
18357       token = cp_lexer_peek_token (parser->lexer);
18358     }
18359
18360   return build_tree_list (sel_args, addl_args);
18361 }
18362
18363 /* Parse an Objective-C encode expression.
18364
18365    objc-encode-expression:
18366      @encode objc-typename
18367
18368    Returns an encoded representation of the type argument.  */
18369
18370 static tree
18371 cp_parser_objc_encode_expression (cp_parser* parser)
18372 {
18373   tree type;
18374
18375   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18376   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18377   type = complete_type (cp_parser_type_id (parser));
18378   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18379
18380   if (!type)
18381     {
18382       error ("%<@encode%> must specify a type as an argument");
18383       return error_mark_node;
18384     }
18385
18386   return objc_build_encode_expr (type);
18387 }
18388
18389 /* Parse an Objective-C @defs expression.  */
18390
18391 static tree
18392 cp_parser_objc_defs_expression (cp_parser *parser)
18393 {
18394   tree name;
18395
18396   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18397   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18398   name = cp_parser_identifier (parser);
18399   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18400
18401   return objc_get_class_ivars (name);
18402 }
18403
18404 /* Parse an Objective-C protocol expression.
18405
18406   objc-protocol-expression:
18407     @protocol ( identifier )
18408
18409   Returns a representation of the protocol expression.  */
18410
18411 static tree
18412 cp_parser_objc_protocol_expression (cp_parser* parser)
18413 {
18414   tree proto;
18415
18416   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18417   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18418   proto = cp_parser_identifier (parser);
18419   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18420
18421   return objc_build_protocol_expr (proto);
18422 }
18423
18424 /* Parse an Objective-C selector expression.
18425
18426    objc-selector-expression:
18427      @selector ( objc-method-signature )
18428
18429    objc-method-signature:
18430      objc-selector
18431      objc-selector-seq
18432
18433    objc-selector-seq:
18434      objc-selector :
18435      objc-selector-seq objc-selector :
18436
18437   Returns a representation of the method selector.  */
18438
18439 static tree
18440 cp_parser_objc_selector_expression (cp_parser* parser)
18441 {
18442   tree sel_seq = NULL_TREE;
18443   bool maybe_unary_selector_p = true;
18444   cp_token *token;
18445
18446   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18447   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18448   token = cp_lexer_peek_token (parser->lexer);
18449
18450   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18451          || token->type == CPP_SCOPE)
18452     {
18453       tree selector = NULL_TREE;
18454
18455       if (token->type != CPP_COLON
18456           || token->type == CPP_SCOPE)
18457         selector = cp_parser_objc_selector (parser);
18458
18459       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18460           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18461         {
18462           /* Detect if we have a unary selector.  */
18463           if (maybe_unary_selector_p)
18464             {
18465               sel_seq = selector;
18466               goto finish_selector;
18467             }
18468           else
18469             {
18470               cp_parser_error (parser, "expected %<:%>");
18471             }
18472         }
18473       maybe_unary_selector_p = false;
18474       token = cp_lexer_consume_token (parser->lexer);
18475
18476       if (token->type == CPP_SCOPE)
18477         {
18478           sel_seq
18479             = chainon (sel_seq,
18480                        build_tree_list (selector, NULL_TREE));
18481           sel_seq
18482             = chainon (sel_seq,
18483                        build_tree_list (NULL_TREE, NULL_TREE));
18484         }
18485       else
18486         sel_seq
18487           = chainon (sel_seq,
18488                      build_tree_list (selector, NULL_TREE));
18489
18490       token = cp_lexer_peek_token (parser->lexer);
18491     }
18492
18493  finish_selector:
18494   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18495
18496   return objc_build_selector_expr (sel_seq);
18497 }
18498
18499 /* Parse a list of identifiers.
18500
18501    objc-identifier-list:
18502      identifier
18503      objc-identifier-list , identifier
18504
18505    Returns a TREE_LIST of identifier nodes.  */
18506
18507 static tree
18508 cp_parser_objc_identifier_list (cp_parser* parser)
18509 {
18510   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18511   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18512
18513   while (sep->type == CPP_COMMA)
18514     {
18515       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18516       list = chainon (list,
18517                       build_tree_list (NULL_TREE,
18518                                        cp_parser_identifier (parser)));
18519       sep = cp_lexer_peek_token (parser->lexer);
18520     }
18521
18522   return list;
18523 }
18524
18525 /* Parse an Objective-C alias declaration.
18526
18527    objc-alias-declaration:
18528      @compatibility_alias identifier identifier ;
18529
18530    This function registers the alias mapping with the Objective-C front end.
18531    It returns nothing.  */
18532
18533 static void
18534 cp_parser_objc_alias_declaration (cp_parser* parser)
18535 {
18536   tree alias, orig;
18537
18538   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18539   alias = cp_parser_identifier (parser);
18540   orig = cp_parser_identifier (parser);
18541   objc_declare_alias (alias, orig);
18542   cp_parser_consume_semicolon_at_end_of_statement (parser);
18543 }
18544
18545 /* Parse an Objective-C class forward-declaration.
18546
18547    objc-class-declaration:
18548      @class objc-identifier-list ;
18549
18550    The function registers the forward declarations with the Objective-C
18551    front end.  It returns nothing.  */
18552
18553 static void
18554 cp_parser_objc_class_declaration (cp_parser* parser)
18555 {
18556   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18557   objc_declare_class (cp_parser_objc_identifier_list (parser));
18558   cp_parser_consume_semicolon_at_end_of_statement (parser);
18559 }
18560
18561 /* Parse a list of Objective-C protocol references.
18562
18563    objc-protocol-refs-opt:
18564      objc-protocol-refs [opt]
18565
18566    objc-protocol-refs:
18567      < objc-identifier-list >
18568
18569    Returns a TREE_LIST of identifiers, if any.  */
18570
18571 static tree
18572 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18573 {
18574   tree protorefs = NULL_TREE;
18575
18576   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18577     {
18578       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18579       protorefs = cp_parser_objc_identifier_list (parser);
18580       cp_parser_require (parser, CPP_GREATER, "`>'");
18581     }
18582
18583   return protorefs;
18584 }
18585
18586 /* Parse a Objective-C visibility specification.  */
18587
18588 static void
18589 cp_parser_objc_visibility_spec (cp_parser* parser)
18590 {
18591   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18592
18593   switch (vis->keyword)
18594     {
18595     case RID_AT_PRIVATE:
18596       objc_set_visibility (2);
18597       break;
18598     case RID_AT_PROTECTED:
18599       objc_set_visibility (0);
18600       break;
18601     case RID_AT_PUBLIC:
18602       objc_set_visibility (1);
18603       break;
18604     default:
18605       return;
18606     }
18607
18608   /* Eat '@private'/'@protected'/'@public'.  */
18609   cp_lexer_consume_token (parser->lexer);
18610 }
18611
18612 /* Parse an Objective-C method type.  */
18613
18614 static void
18615 cp_parser_objc_method_type (cp_parser* parser)
18616 {
18617   objc_set_method_type
18618    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18619     ? PLUS_EXPR
18620     : MINUS_EXPR);
18621 }
18622
18623 /* Parse an Objective-C protocol qualifier.  */
18624
18625 static tree
18626 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18627 {
18628   tree quals = NULL_TREE, node;
18629   cp_token *token = cp_lexer_peek_token (parser->lexer);
18630
18631   node = token->u.value;
18632
18633   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18634          && (node == ridpointers [(int) RID_IN]
18635              || node == ridpointers [(int) RID_OUT]
18636              || node == ridpointers [(int) RID_INOUT]
18637              || node == ridpointers [(int) RID_BYCOPY]
18638              || node == ridpointers [(int) RID_BYREF]
18639              || node == ridpointers [(int) RID_ONEWAY]))
18640     {
18641       quals = tree_cons (NULL_TREE, node, quals);
18642       cp_lexer_consume_token (parser->lexer);
18643       token = cp_lexer_peek_token (parser->lexer);
18644       node = token->u.value;
18645     }
18646
18647   return quals;
18648 }
18649
18650 /* Parse an Objective-C typename.  */
18651
18652 static tree
18653 cp_parser_objc_typename (cp_parser* parser)
18654 {
18655   tree typename = NULL_TREE;
18656
18657   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18658     {
18659       tree proto_quals, cp_type = NULL_TREE;
18660
18661       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18662       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18663
18664       /* An ObjC type name may consist of just protocol qualifiers, in which
18665          case the type shall default to 'id'.  */
18666       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18667         cp_type = cp_parser_type_id (parser);
18668
18669       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18670       typename = build_tree_list (proto_quals, cp_type);
18671     }
18672
18673   return typename;
18674 }
18675
18676 /* Check to see if TYPE refers to an Objective-C selector name.  */
18677
18678 static bool
18679 cp_parser_objc_selector_p (enum cpp_ttype type)
18680 {
18681   return (type == CPP_NAME || type == CPP_KEYWORD
18682           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18683           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18684           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18685           || type == CPP_XOR || type == CPP_XOR_EQ);
18686 }
18687
18688 /* Parse an Objective-C selector.  */
18689
18690 static tree
18691 cp_parser_objc_selector (cp_parser* parser)
18692 {
18693   cp_token *token = cp_lexer_consume_token (parser->lexer);
18694
18695   if (!cp_parser_objc_selector_p (token->type))
18696     {
18697       error ("invalid Objective-C++ selector name");
18698       return error_mark_node;
18699     }
18700
18701   /* C++ operator names are allowed to appear in ObjC selectors.  */
18702   switch (token->type)
18703     {
18704     case CPP_AND_AND: return get_identifier ("and");
18705     case CPP_AND_EQ: return get_identifier ("and_eq");
18706     case CPP_AND: return get_identifier ("bitand");
18707     case CPP_OR: return get_identifier ("bitor");
18708     case CPP_COMPL: return get_identifier ("compl");
18709     case CPP_NOT: return get_identifier ("not");
18710     case CPP_NOT_EQ: return get_identifier ("not_eq");
18711     case CPP_OR_OR: return get_identifier ("or");
18712     case CPP_OR_EQ: return get_identifier ("or_eq");
18713     case CPP_XOR: return get_identifier ("xor");
18714     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18715     default: return token->u.value;
18716     }
18717 }
18718
18719 /* Parse an Objective-C params list.  */
18720
18721 static tree
18722 cp_parser_objc_method_keyword_params (cp_parser* parser)
18723 {
18724   tree params = NULL_TREE;
18725   bool maybe_unary_selector_p = true;
18726   cp_token *token = cp_lexer_peek_token (parser->lexer);
18727
18728   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18729     {
18730       tree selector = NULL_TREE, typename, identifier;
18731
18732       if (token->type != CPP_COLON)
18733         selector = cp_parser_objc_selector (parser);
18734
18735       /* Detect if we have a unary selector.  */
18736       if (maybe_unary_selector_p
18737           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18738         return selector;
18739
18740       maybe_unary_selector_p = false;
18741       cp_parser_require (parser, CPP_COLON, "`:'");
18742       typename = cp_parser_objc_typename (parser);
18743       identifier = cp_parser_identifier (parser);
18744
18745       params
18746         = chainon (params,
18747                    objc_build_keyword_decl (selector,
18748                                             typename,
18749                                             identifier));
18750
18751       token = cp_lexer_peek_token (parser->lexer);
18752     }
18753
18754   return params;
18755 }
18756
18757 /* Parse the non-keyword Objective-C params.  */
18758
18759 static tree
18760 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18761 {
18762   tree params = make_node (TREE_LIST);
18763   cp_token *token = cp_lexer_peek_token (parser->lexer);
18764   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18765
18766   while (token->type == CPP_COMMA)
18767     {
18768       cp_parameter_declarator *parmdecl;
18769       tree parm;
18770
18771       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18772       token = cp_lexer_peek_token (parser->lexer);
18773
18774       if (token->type == CPP_ELLIPSIS)
18775         {
18776           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18777           *ellipsisp = true;
18778           break;
18779         }
18780
18781       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18782       parm = grokdeclarator (parmdecl->declarator,
18783                              &parmdecl->decl_specifiers,
18784                              PARM, /*initialized=*/0,
18785                              /*attrlist=*/NULL);
18786
18787       chainon (params, build_tree_list (NULL_TREE, parm));
18788       token = cp_lexer_peek_token (parser->lexer);
18789     }
18790
18791   return params;
18792 }
18793
18794 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18795
18796 static void
18797 cp_parser_objc_interstitial_code (cp_parser* parser)
18798 {
18799   cp_token *token = cp_lexer_peek_token (parser->lexer);
18800
18801   /* If the next token is `extern' and the following token is a string
18802      literal, then we have a linkage specification.  */
18803   if (token->keyword == RID_EXTERN
18804       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18805     cp_parser_linkage_specification (parser);
18806   /* Handle #pragma, if any.  */
18807   else if (token->type == CPP_PRAGMA)
18808     cp_parser_pragma (parser, pragma_external);
18809   /* Allow stray semicolons.  */
18810   else if (token->type == CPP_SEMICOLON)
18811     cp_lexer_consume_token (parser->lexer);
18812   /* Finally, try to parse a block-declaration, or a function-definition.  */
18813   else
18814     cp_parser_block_declaration (parser, /*statement_p=*/false);
18815 }
18816
18817 /* Parse a method signature.  */
18818
18819 static tree
18820 cp_parser_objc_method_signature (cp_parser* parser)
18821 {
18822   tree rettype, kwdparms, optparms;
18823   bool ellipsis = false;
18824
18825   cp_parser_objc_method_type (parser);
18826   rettype = cp_parser_objc_typename (parser);
18827   kwdparms = cp_parser_objc_method_keyword_params (parser);
18828   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18829
18830   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18831 }
18832
18833 /* Pars an Objective-C method prototype list.  */
18834
18835 static void
18836 cp_parser_objc_method_prototype_list (cp_parser* parser)
18837 {
18838   cp_token *token = cp_lexer_peek_token (parser->lexer);
18839
18840   while (token->keyword != RID_AT_END)
18841     {
18842       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18843         {
18844           objc_add_method_declaration
18845            (cp_parser_objc_method_signature (parser));
18846           cp_parser_consume_semicolon_at_end_of_statement (parser);
18847         }
18848       else
18849         /* Allow for interspersed non-ObjC++ code.  */
18850         cp_parser_objc_interstitial_code (parser);
18851
18852       token = cp_lexer_peek_token (parser->lexer);
18853     }
18854
18855   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18856   objc_finish_interface ();
18857 }
18858
18859 /* Parse an Objective-C method definition list.  */
18860
18861 static void
18862 cp_parser_objc_method_definition_list (cp_parser* parser)
18863 {
18864   cp_token *token = cp_lexer_peek_token (parser->lexer);
18865
18866   while (token->keyword != RID_AT_END)
18867     {
18868       tree meth;
18869
18870       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18871         {
18872           push_deferring_access_checks (dk_deferred);
18873           objc_start_method_definition
18874            (cp_parser_objc_method_signature (parser));
18875
18876           /* For historical reasons, we accept an optional semicolon.  */
18877           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18878             cp_lexer_consume_token (parser->lexer);
18879
18880           perform_deferred_access_checks ();
18881           stop_deferring_access_checks ();
18882           meth = cp_parser_function_definition_after_declarator (parser,
18883                                                                  false);
18884           pop_deferring_access_checks ();
18885           objc_finish_method_definition (meth);
18886         }
18887       else
18888         /* Allow for interspersed non-ObjC++ code.  */
18889         cp_parser_objc_interstitial_code (parser);
18890
18891       token = cp_lexer_peek_token (parser->lexer);
18892     }
18893
18894   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18895   objc_finish_implementation ();
18896 }
18897
18898 /* Parse Objective-C ivars.  */
18899
18900 static void
18901 cp_parser_objc_class_ivars (cp_parser* parser)
18902 {
18903   cp_token *token = cp_lexer_peek_token (parser->lexer);
18904
18905   if (token->type != CPP_OPEN_BRACE)
18906     return;     /* No ivars specified.  */
18907
18908   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18909   token = cp_lexer_peek_token (parser->lexer);
18910
18911   while (token->type != CPP_CLOSE_BRACE)
18912     {
18913       cp_decl_specifier_seq declspecs;
18914       int decl_class_or_enum_p;
18915       tree prefix_attributes;
18916
18917       cp_parser_objc_visibility_spec (parser);
18918
18919       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18920         break;
18921
18922       cp_parser_decl_specifier_seq (parser,
18923                                     CP_PARSER_FLAGS_OPTIONAL,
18924                                     &declspecs,
18925                                     &decl_class_or_enum_p);
18926       prefix_attributes = declspecs.attributes;
18927       declspecs.attributes = NULL_TREE;
18928
18929       /* Keep going until we hit the `;' at the end of the
18930          declaration.  */
18931       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18932         {
18933           tree width = NULL_TREE, attributes, first_attribute, decl;
18934           cp_declarator *declarator = NULL;
18935           int ctor_dtor_or_conv_p;
18936
18937           /* Check for a (possibly unnamed) bitfield declaration.  */
18938           token = cp_lexer_peek_token (parser->lexer);
18939           if (token->type == CPP_COLON)
18940             goto eat_colon;
18941
18942           if (token->type == CPP_NAME
18943               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18944                   == CPP_COLON))
18945             {
18946               /* Get the name of the bitfield.  */
18947               declarator = make_id_declarator (NULL_TREE,
18948                                                cp_parser_identifier (parser),
18949                                                sfk_none);
18950
18951              eat_colon:
18952               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18953               /* Get the width of the bitfield.  */
18954               width
18955                 = cp_parser_constant_expression (parser,
18956                                                  /*allow_non_constant=*/false,
18957                                                  NULL);
18958             }
18959           else
18960             {
18961               /* Parse the declarator.  */
18962               declarator
18963                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18964                                         &ctor_dtor_or_conv_p,
18965                                         /*parenthesized_p=*/NULL,
18966                                         /*member_p=*/false);
18967             }
18968
18969           /* Look for attributes that apply to the ivar.  */
18970           attributes = cp_parser_attributes_opt (parser);
18971           /* Remember which attributes are prefix attributes and
18972              which are not.  */
18973           first_attribute = attributes;
18974           /* Combine the attributes.  */
18975           attributes = chainon (prefix_attributes, attributes);
18976
18977           if (width)
18978             {
18979               /* Create the bitfield declaration.  */
18980               decl = grokbitfield (declarator, &declspecs, width);
18981               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18982             }
18983           else
18984             decl = grokfield (declarator, &declspecs,
18985                               NULL_TREE, /*init_const_expr_p=*/false,
18986                               NULL_TREE, attributes);
18987
18988           /* Add the instance variable.  */
18989           objc_add_instance_variable (decl);
18990
18991           /* Reset PREFIX_ATTRIBUTES.  */
18992           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18993             attributes = TREE_CHAIN (attributes);
18994           if (attributes)
18995             TREE_CHAIN (attributes) = NULL_TREE;
18996
18997           token = cp_lexer_peek_token (parser->lexer);
18998
18999           if (token->type == CPP_COMMA)
19000             {
19001               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19002               continue;
19003             }
19004           break;
19005         }
19006
19007       cp_parser_consume_semicolon_at_end_of_statement (parser);
19008       token = cp_lexer_peek_token (parser->lexer);
19009     }
19010
19011   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19012   /* For historical reasons, we accept an optional semicolon.  */
19013   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19014     cp_lexer_consume_token (parser->lexer);
19015 }
19016
19017 /* Parse an Objective-C protocol declaration.  */
19018
19019 static void
19020 cp_parser_objc_protocol_declaration (cp_parser* parser)
19021 {
19022   tree proto, protorefs;
19023   cp_token *tok;
19024
19025   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19026   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19027     {
19028       error ("identifier expected after %<@protocol%>");
19029       goto finish;
19030     }
19031
19032   /* See if we have a forward declaration or a definition.  */
19033   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19034
19035   /* Try a forward declaration first.  */
19036   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19037     {
19038       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19039      finish:
19040       cp_parser_consume_semicolon_at_end_of_statement (parser);
19041     }
19042
19043   /* Ok, we got a full-fledged definition (or at least should).  */
19044   else
19045     {
19046       proto = cp_parser_identifier (parser);
19047       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19048       objc_start_protocol (proto, protorefs);
19049       cp_parser_objc_method_prototype_list (parser);
19050     }
19051 }
19052
19053 /* Parse an Objective-C superclass or category.  */
19054
19055 static void
19056 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19057                                                           tree *categ)
19058 {
19059   cp_token *next = cp_lexer_peek_token (parser->lexer);
19060
19061   *super = *categ = NULL_TREE;
19062   if (next->type == CPP_COLON)
19063     {
19064       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19065       *super = cp_parser_identifier (parser);
19066     }
19067   else if (next->type == CPP_OPEN_PAREN)
19068     {
19069       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19070       *categ = cp_parser_identifier (parser);
19071       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19072     }
19073 }
19074
19075 /* Parse an Objective-C class interface.  */
19076
19077 static void
19078 cp_parser_objc_class_interface (cp_parser* parser)
19079 {
19080   tree name, super, categ, protos;
19081
19082   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19083   name = cp_parser_identifier (parser);
19084   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19085   protos = cp_parser_objc_protocol_refs_opt (parser);
19086
19087   /* We have either a class or a category on our hands.  */
19088   if (categ)
19089     objc_start_category_interface (name, categ, protos);
19090   else
19091     {
19092       objc_start_class_interface (name, super, protos);
19093       /* Handle instance variable declarations, if any.  */
19094       cp_parser_objc_class_ivars (parser);
19095       objc_continue_interface ();
19096     }
19097
19098   cp_parser_objc_method_prototype_list (parser);
19099 }
19100
19101 /* Parse an Objective-C class implementation.  */
19102
19103 static void
19104 cp_parser_objc_class_implementation (cp_parser* parser)
19105 {
19106   tree name, super, categ;
19107
19108   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19109   name = cp_parser_identifier (parser);
19110   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19111
19112   /* We have either a class or a category on our hands.  */
19113   if (categ)
19114     objc_start_category_implementation (name, categ);
19115   else
19116     {
19117       objc_start_class_implementation (name, super);
19118       /* Handle instance variable declarations, if any.  */
19119       cp_parser_objc_class_ivars (parser);
19120       objc_continue_implementation ();
19121     }
19122
19123   cp_parser_objc_method_definition_list (parser);
19124 }
19125
19126 /* Consume the @end token and finish off the implementation.  */
19127
19128 static void
19129 cp_parser_objc_end_implementation (cp_parser* parser)
19130 {
19131   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19132   objc_finish_implementation ();
19133 }
19134
19135 /* Parse an Objective-C declaration.  */
19136
19137 static void
19138 cp_parser_objc_declaration (cp_parser* parser)
19139 {
19140   /* Try to figure out what kind of declaration is present.  */
19141   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19142
19143   switch (kwd->keyword)
19144     {
19145     case RID_AT_ALIAS:
19146       cp_parser_objc_alias_declaration (parser);
19147       break;
19148     case RID_AT_CLASS:
19149       cp_parser_objc_class_declaration (parser);
19150       break;
19151     case RID_AT_PROTOCOL:
19152       cp_parser_objc_protocol_declaration (parser);
19153       break;
19154     case RID_AT_INTERFACE:
19155       cp_parser_objc_class_interface (parser);
19156       break;
19157     case RID_AT_IMPLEMENTATION:
19158       cp_parser_objc_class_implementation (parser);
19159       break;
19160     case RID_AT_END:
19161       cp_parser_objc_end_implementation (parser);
19162       break;
19163     default:
19164       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19165       cp_parser_skip_to_end_of_block_or_statement (parser);
19166     }
19167 }
19168
19169 /* Parse an Objective-C try-catch-finally statement.
19170
19171    objc-try-catch-finally-stmt:
19172      @try compound-statement objc-catch-clause-seq [opt]
19173        objc-finally-clause [opt]
19174
19175    objc-catch-clause-seq:
19176      objc-catch-clause objc-catch-clause-seq [opt]
19177
19178    objc-catch-clause:
19179      @catch ( exception-declaration ) compound-statement
19180
19181    objc-finally-clause
19182      @finally compound-statement
19183
19184    Returns NULL_TREE.  */
19185
19186 static tree
19187 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19188   location_t location;
19189   tree stmt;
19190
19191   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19192   location = cp_lexer_peek_token (parser->lexer)->location;
19193   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19194      node, lest it get absorbed into the surrounding block.  */
19195   stmt = push_stmt_list ();
19196   cp_parser_compound_statement (parser, NULL, false);
19197   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19198
19199   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19200     {
19201       cp_parameter_declarator *parmdecl;
19202       tree parm;
19203
19204       cp_lexer_consume_token (parser->lexer);
19205       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19206       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19207       parm = grokdeclarator (parmdecl->declarator,
19208                              &parmdecl->decl_specifiers,
19209                              PARM, /*initialized=*/0,
19210                              /*attrlist=*/NULL);
19211       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19212       objc_begin_catch_clause (parm);
19213       cp_parser_compound_statement (parser, NULL, false);
19214       objc_finish_catch_clause ();
19215     }
19216
19217   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19218     {
19219       cp_lexer_consume_token (parser->lexer);
19220       location = cp_lexer_peek_token (parser->lexer)->location;
19221       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19222          node, lest it get absorbed into the surrounding block.  */
19223       stmt = push_stmt_list ();
19224       cp_parser_compound_statement (parser, NULL, false);
19225       objc_build_finally_clause (location, pop_stmt_list (stmt));
19226     }
19227
19228   return objc_finish_try_stmt ();
19229 }
19230
19231 /* Parse an Objective-C synchronized statement.
19232
19233    objc-synchronized-stmt:
19234      @synchronized ( expression ) compound-statement
19235
19236    Returns NULL_TREE.  */
19237
19238 static tree
19239 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19240   location_t location;
19241   tree lock, stmt;
19242
19243   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19244
19245   location = cp_lexer_peek_token (parser->lexer)->location;
19246   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19247   lock = cp_parser_expression (parser, false);
19248   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19249
19250   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19251      node, lest it get absorbed into the surrounding block.  */
19252   stmt = push_stmt_list ();
19253   cp_parser_compound_statement (parser, NULL, false);
19254
19255   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19256 }
19257
19258 /* Parse an Objective-C throw statement.
19259
19260    objc-throw-stmt:
19261      @throw assignment-expression [opt] ;
19262
19263    Returns a constructed '@throw' statement.  */
19264
19265 static tree
19266 cp_parser_objc_throw_statement (cp_parser *parser) {
19267   tree expr = NULL_TREE;
19268
19269   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19270
19271   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19272     expr = cp_parser_assignment_expression (parser, false);
19273
19274   cp_parser_consume_semicolon_at_end_of_statement (parser);
19275
19276   return objc_build_throw_stmt (expr);
19277 }
19278
19279 /* Parse an Objective-C statement.  */
19280
19281 static tree
19282 cp_parser_objc_statement (cp_parser * parser) {
19283   /* Try to figure out what kind of declaration is present.  */
19284   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19285
19286   switch (kwd->keyword)
19287     {
19288     case RID_AT_TRY:
19289       return cp_parser_objc_try_catch_finally_statement (parser);
19290     case RID_AT_SYNCHRONIZED:
19291       return cp_parser_objc_synchronized_statement (parser);
19292     case RID_AT_THROW:
19293       return cp_parser_objc_throw_statement (parser);
19294     default:
19295       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19296       cp_parser_skip_to_end_of_block_or_statement (parser);
19297     }
19298
19299   return error_mark_node;
19300 }
19301 \f
19302 /* OpenMP 2.5 parsing routines.  */
19303
19304 /* Returns name of the next clause.
19305    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19306    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19307    returned and the token is consumed.  */
19308
19309 static pragma_omp_clause
19310 cp_parser_omp_clause_name (cp_parser *parser)
19311 {
19312   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19313
19314   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19315     result = PRAGMA_OMP_CLAUSE_IF;
19316   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19317     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19318   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19319     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19320   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19321     {
19322       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19323       const char *p = IDENTIFIER_POINTER (id);
19324
19325       switch (p[0])
19326         {
19327         case 'c':
19328           if (!strcmp ("copyin", p))
19329             result = PRAGMA_OMP_CLAUSE_COPYIN;
19330           else if (!strcmp ("copyprivate", p))
19331             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19332           break;
19333         case 'f':
19334           if (!strcmp ("firstprivate", p))
19335             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19336           break;
19337         case 'l':
19338           if (!strcmp ("lastprivate", p))
19339             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19340           break;
19341         case 'n':
19342           if (!strcmp ("nowait", p))
19343             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19344           else if (!strcmp ("num_threads", p))
19345             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19346           break;
19347         case 'o':
19348           if (!strcmp ("ordered", p))
19349             result = PRAGMA_OMP_CLAUSE_ORDERED;
19350           break;
19351         case 'r':
19352           if (!strcmp ("reduction", p))
19353             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19354           break;
19355         case 's':
19356           if (!strcmp ("schedule", p))
19357             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19358           else if (!strcmp ("shared", p))
19359             result = PRAGMA_OMP_CLAUSE_SHARED;
19360           break;
19361         }
19362     }
19363
19364   if (result != PRAGMA_OMP_CLAUSE_NONE)
19365     cp_lexer_consume_token (parser->lexer);
19366
19367   return result;
19368 }
19369
19370 /* Validate that a clause of the given type does not already exist.  */
19371
19372 static void
19373 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19374 {
19375   tree c;
19376
19377   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19378     if (OMP_CLAUSE_CODE (c) == code)
19379       {
19380         error ("too many %qs clauses", name);
19381         break;
19382       }
19383 }
19384
19385 /* OpenMP 2.5:
19386    variable-list:
19387      identifier
19388      variable-list , identifier
19389
19390    In addition, we match a closing parenthesis.  An opening parenthesis
19391    will have been consumed by the caller.
19392
19393    If KIND is nonzero, create the appropriate node and install the decl
19394    in OMP_CLAUSE_DECL and add the node to the head of the list.
19395
19396    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19397    return the list created.  */
19398
19399 static tree
19400 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19401                                 tree list)
19402 {
19403   while (1)
19404     {
19405       tree name, decl;
19406
19407       name = cp_parser_id_expression (parser, /*template_p=*/false,
19408                                       /*check_dependency_p=*/true,
19409                                       /*template_p=*/NULL,
19410                                       /*declarator_p=*/false,
19411                                       /*optional_p=*/false);
19412       if (name == error_mark_node)
19413         goto skip_comma;
19414
19415       decl = cp_parser_lookup_name_simple (parser, name);
19416       if (decl == error_mark_node)
19417         cp_parser_name_lookup_error (parser, name, decl, NULL);
19418       else if (kind != 0)
19419         {
19420           tree u = build_omp_clause (kind);
19421           OMP_CLAUSE_DECL (u) = decl;
19422           OMP_CLAUSE_CHAIN (u) = list;
19423           list = u;
19424         }
19425       else
19426         list = tree_cons (decl, NULL_TREE, list);
19427
19428     get_comma:
19429       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19430         break;
19431       cp_lexer_consume_token (parser->lexer);
19432     }
19433
19434   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19435     {
19436       int ending;
19437
19438       /* Try to resync to an unnested comma.  Copied from
19439          cp_parser_parenthesized_expression_list.  */
19440     skip_comma:
19441       ending = cp_parser_skip_to_closing_parenthesis (parser,
19442                                                       /*recovering=*/true,
19443                                                       /*or_comma=*/true,
19444                                                       /*consume_paren=*/true);
19445       if (ending < 0)
19446         goto get_comma;
19447     }
19448
19449   return list;
19450 }
19451
19452 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19453    common case for omp clauses.  */
19454
19455 static tree
19456 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19457 {
19458   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19459     return cp_parser_omp_var_list_no_open (parser, kind, list);
19460   return list;
19461 }
19462
19463 /* OpenMP 2.5:
19464    default ( shared | none ) */
19465
19466 static tree
19467 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19468 {
19469   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19470   tree c;
19471
19472   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19473     return list;
19474   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19475     {
19476       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19477       const char *p = IDENTIFIER_POINTER (id);
19478
19479       switch (p[0])
19480         {
19481         case 'n':
19482           if (strcmp ("none", p) != 0)
19483             goto invalid_kind;
19484           kind = OMP_CLAUSE_DEFAULT_NONE;
19485           break;
19486
19487         case 's':
19488           if (strcmp ("shared", p) != 0)
19489             goto invalid_kind;
19490           kind = OMP_CLAUSE_DEFAULT_SHARED;
19491           break;
19492
19493         default:
19494           goto invalid_kind;
19495         }
19496
19497       cp_lexer_consume_token (parser->lexer);
19498     }
19499   else
19500     {
19501     invalid_kind:
19502       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19503     }
19504
19505   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19506     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19507                                            /*or_comma=*/false,
19508                                            /*consume_paren=*/true);
19509
19510   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19511     return list;
19512
19513   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19514   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19515   OMP_CLAUSE_CHAIN (c) = list;
19516   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19517
19518   return c;
19519 }
19520
19521 /* OpenMP 2.5:
19522    if ( expression ) */
19523
19524 static tree
19525 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19526 {
19527   tree t, c;
19528
19529   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19530     return list;
19531
19532   t = cp_parser_condition (parser);
19533
19534   if (t == error_mark_node
19535       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19536     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19537                                            /*or_comma=*/false,
19538                                            /*consume_paren=*/true);
19539
19540   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19541
19542   c = build_omp_clause (OMP_CLAUSE_IF);
19543   OMP_CLAUSE_IF_EXPR (c) = t;
19544   OMP_CLAUSE_CHAIN (c) = list;
19545
19546   return c;
19547 }
19548
19549 /* OpenMP 2.5:
19550    nowait */
19551
19552 static tree
19553 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19554 {
19555   tree c;
19556
19557   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19558
19559   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19560   OMP_CLAUSE_CHAIN (c) = list;
19561   return c;
19562 }
19563
19564 /* OpenMP 2.5:
19565    num_threads ( expression ) */
19566
19567 static tree
19568 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19569 {
19570   tree t, c;
19571
19572   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19573     return list;
19574
19575   t = cp_parser_expression (parser, false);
19576
19577   if (t == error_mark_node
19578       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19579     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19580                                            /*or_comma=*/false,
19581                                            /*consume_paren=*/true);
19582
19583   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19584
19585   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19586   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19587   OMP_CLAUSE_CHAIN (c) = list;
19588
19589   return c;
19590 }
19591
19592 /* OpenMP 2.5:
19593    ordered */
19594
19595 static tree
19596 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19597 {
19598   tree c;
19599
19600   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19601
19602   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19603   OMP_CLAUSE_CHAIN (c) = list;
19604   return c;
19605 }
19606
19607 /* OpenMP 2.5:
19608    reduction ( reduction-operator : variable-list )
19609
19610    reduction-operator:
19611      One of: + * - & ^ | && || */
19612
19613 static tree
19614 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19615 {
19616   enum tree_code code;
19617   tree nlist, c;
19618
19619   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19620     return list;
19621
19622   switch (cp_lexer_peek_token (parser->lexer)->type)
19623     {
19624     case CPP_PLUS:
19625       code = PLUS_EXPR;
19626       break;
19627     case CPP_MULT:
19628       code = MULT_EXPR;
19629       break;
19630     case CPP_MINUS:
19631       code = MINUS_EXPR;
19632       break;
19633     case CPP_AND:
19634       code = BIT_AND_EXPR;
19635       break;
19636     case CPP_XOR:
19637       code = BIT_XOR_EXPR;
19638       break;
19639     case CPP_OR:
19640       code = BIT_IOR_EXPR;
19641       break;
19642     case CPP_AND_AND:
19643       code = TRUTH_ANDIF_EXPR;
19644       break;
19645     case CPP_OR_OR:
19646       code = TRUTH_ORIF_EXPR;
19647       break;
19648     default:
19649       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19650     resync_fail:
19651       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19652                                              /*or_comma=*/false,
19653                                              /*consume_paren=*/true);
19654       return list;
19655     }
19656   cp_lexer_consume_token (parser->lexer);
19657
19658   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19659     goto resync_fail;
19660
19661   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19662   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19663     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19664
19665   return nlist;
19666 }
19667
19668 /* OpenMP 2.5:
19669    schedule ( schedule-kind )
19670    schedule ( schedule-kind , expression )
19671
19672    schedule-kind:
19673      static | dynamic | guided | runtime  */
19674
19675 static tree
19676 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19677 {
19678   tree c, t;
19679
19680   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19681     return list;
19682
19683   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19684
19685   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19686     {
19687       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19688       const char *p = IDENTIFIER_POINTER (id);
19689
19690       switch (p[0])
19691         {
19692         case 'd':
19693           if (strcmp ("dynamic", p) != 0)
19694             goto invalid_kind;
19695           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19696           break;
19697
19698         case 'g':
19699           if (strcmp ("guided", p) != 0)
19700             goto invalid_kind;
19701           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19702           break;
19703
19704         case 'r':
19705           if (strcmp ("runtime", p) != 0)
19706             goto invalid_kind;
19707           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19708           break;
19709
19710         default:
19711           goto invalid_kind;
19712         }
19713     }
19714   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19715     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19716   else
19717     goto invalid_kind;
19718   cp_lexer_consume_token (parser->lexer);
19719
19720   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19721     {
19722       cp_lexer_consume_token (parser->lexer);
19723
19724       t = cp_parser_assignment_expression (parser, false);
19725
19726       if (t == error_mark_node)
19727         goto resync_fail;
19728       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19729         error ("schedule %<runtime%> does not take "
19730                "a %<chunk_size%> parameter");
19731       else
19732         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19733
19734       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19735         goto resync_fail;
19736     }
19737   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19738     goto resync_fail;
19739
19740   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19741   OMP_CLAUSE_CHAIN (c) = list;
19742   return c;
19743
19744  invalid_kind:
19745   cp_parser_error (parser, "invalid schedule kind");
19746  resync_fail:
19747   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19748                                          /*or_comma=*/false,
19749                                          /*consume_paren=*/true);
19750   return list;
19751 }
19752
19753 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19754    is a bitmask in MASK.  Return the list of clauses found; the result
19755    of clause default goes in *pdefault.  */
19756
19757 static tree
19758 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19759                            const char *where, cp_token *pragma_tok)
19760 {
19761   tree clauses = NULL;
19762   bool first = true;
19763
19764   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19765     {
19766       pragma_omp_clause c_kind;
19767       const char *c_name;
19768       tree prev = clauses;
19769
19770       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19771         cp_lexer_consume_token (parser->lexer);
19772
19773       c_kind = cp_parser_omp_clause_name (parser);
19774       first = false;
19775
19776       switch (c_kind)
19777         {
19778         case PRAGMA_OMP_CLAUSE_COPYIN:
19779           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19780           c_name = "copyin";
19781           break;
19782         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19783           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19784                                             clauses);
19785           c_name = "copyprivate";
19786           break;
19787         case PRAGMA_OMP_CLAUSE_DEFAULT:
19788           clauses = cp_parser_omp_clause_default (parser, clauses);
19789           c_name = "default";
19790           break;
19791         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19792           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19793                                             clauses);
19794           c_name = "firstprivate";
19795           break;
19796         case PRAGMA_OMP_CLAUSE_IF:
19797           clauses = cp_parser_omp_clause_if (parser, clauses);
19798           c_name = "if";
19799           break;
19800         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19801           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19802                                             clauses);
19803           c_name = "lastprivate";
19804           break;
19805         case PRAGMA_OMP_CLAUSE_NOWAIT:
19806           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19807           c_name = "nowait";
19808           break;
19809         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19810           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19811           c_name = "num_threads";
19812           break;
19813         case PRAGMA_OMP_CLAUSE_ORDERED:
19814           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19815           c_name = "ordered";
19816           break;
19817         case PRAGMA_OMP_CLAUSE_PRIVATE:
19818           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19819                                             clauses);
19820           c_name = "private";
19821           break;
19822         case PRAGMA_OMP_CLAUSE_REDUCTION:
19823           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19824           c_name = "reduction";
19825           break;
19826         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19827           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19828           c_name = "schedule";
19829           break;
19830         case PRAGMA_OMP_CLAUSE_SHARED:
19831           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19832                                             clauses);
19833           c_name = "shared";
19834           break;
19835         default:
19836           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19837           goto saw_error;
19838         }
19839
19840       if (((mask >> c_kind) & 1) == 0)
19841         {
19842           /* Remove the invalid clause(s) from the list to avoid
19843              confusing the rest of the compiler.  */
19844           clauses = prev;
19845           error ("%qs is not valid for %qs", c_name, where);
19846         }
19847     }
19848  saw_error:
19849   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19850   return finish_omp_clauses (clauses);
19851 }
19852
19853 /* OpenMP 2.5:
19854    structured-block:
19855      statement
19856
19857    In practice, we're also interested in adding the statement to an
19858    outer node.  So it is convenient if we work around the fact that
19859    cp_parser_statement calls add_stmt.  */
19860
19861 static unsigned
19862 cp_parser_begin_omp_structured_block (cp_parser *parser)
19863 {
19864   unsigned save = parser->in_statement;
19865
19866   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19867      This preserves the "not within loop or switch" style error messages
19868      for nonsense cases like
19869         void foo() {
19870         #pragma omp single
19871           break;
19872         }
19873   */
19874   if (parser->in_statement)
19875     parser->in_statement = IN_OMP_BLOCK;
19876
19877   return save;
19878 }
19879
19880 static void
19881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19882 {
19883   parser->in_statement = save;
19884 }
19885
19886 static tree
19887 cp_parser_omp_structured_block (cp_parser *parser)
19888 {
19889   tree stmt = begin_omp_structured_block ();
19890   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19891
19892   cp_parser_statement (parser, NULL_TREE, false, NULL);
19893
19894   cp_parser_end_omp_structured_block (parser, save);
19895   return finish_omp_structured_block (stmt);
19896 }
19897
19898 /* OpenMP 2.5:
19899    # pragma omp atomic new-line
19900      expression-stmt
19901
19902    expression-stmt:
19903      x binop= expr | x++ | ++x | x-- | --x
19904    binop:
19905      +, *, -, /, &, ^, |, <<, >>
19906
19907   where x is an lvalue expression with scalar type.  */
19908
19909 static void
19910 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19911 {
19912   tree lhs, rhs;
19913   enum tree_code code;
19914
19915   cp_parser_require_pragma_eol (parser, pragma_tok);
19916
19917   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19918                                     /*cast_p=*/false);
19919   switch (TREE_CODE (lhs))
19920     {
19921     case ERROR_MARK:
19922       goto saw_error;
19923
19924     case PREINCREMENT_EXPR:
19925     case POSTINCREMENT_EXPR:
19926       lhs = TREE_OPERAND (lhs, 0);
19927       code = PLUS_EXPR;
19928       rhs = integer_one_node;
19929       break;
19930
19931     case PREDECREMENT_EXPR:
19932     case POSTDECREMENT_EXPR:
19933       lhs = TREE_OPERAND (lhs, 0);
19934       code = MINUS_EXPR;
19935       rhs = integer_one_node;
19936       break;
19937
19938     default:
19939       switch (cp_lexer_peek_token (parser->lexer)->type)
19940         {
19941         case CPP_MULT_EQ:
19942           code = MULT_EXPR;
19943           break;
19944         case CPP_DIV_EQ:
19945           code = TRUNC_DIV_EXPR;
19946           break;
19947         case CPP_PLUS_EQ:
19948           code = PLUS_EXPR;
19949           break;
19950         case CPP_MINUS_EQ:
19951           code = MINUS_EXPR;
19952           break;
19953         case CPP_LSHIFT_EQ:
19954           code = LSHIFT_EXPR;
19955           break;
19956         case CPP_RSHIFT_EQ:
19957           code = RSHIFT_EXPR;
19958           break;
19959         case CPP_AND_EQ:
19960           code = BIT_AND_EXPR;
19961           break;
19962         case CPP_OR_EQ:
19963           code = BIT_IOR_EXPR;
19964           break;
19965         case CPP_XOR_EQ:
19966           code = BIT_XOR_EXPR;
19967           break;
19968         default:
19969           cp_parser_error (parser,
19970                            "invalid operator for %<#pragma omp atomic%>");
19971           goto saw_error;
19972         }
19973       cp_lexer_consume_token (parser->lexer);
19974
19975       rhs = cp_parser_expression (parser, false);
19976       if (rhs == error_mark_node)
19977         goto saw_error;
19978       break;
19979     }
19980   finish_omp_atomic (code, lhs, rhs);
19981   cp_parser_consume_semicolon_at_end_of_statement (parser);
19982   return;
19983
19984  saw_error:
19985   cp_parser_skip_to_end_of_block_or_statement (parser);
19986 }
19987
19988
19989 /* OpenMP 2.5:
19990    # pragma omp barrier new-line  */
19991
19992 static void
19993 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19994 {
19995   cp_parser_require_pragma_eol (parser, pragma_tok);
19996   finish_omp_barrier ();
19997 }
19998
19999 /* OpenMP 2.5:
20000    # pragma omp critical [(name)] new-line
20001      structured-block  */
20002
20003 static tree
20004 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20005 {
20006   tree stmt, name = NULL;
20007
20008   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20009     {
20010       cp_lexer_consume_token (parser->lexer);
20011
20012       name = cp_parser_identifier (parser);
20013
20014       if (name == error_mark_node
20015           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20016         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20017                                                /*or_comma=*/false,
20018                                                /*consume_paren=*/true);
20019       if (name == error_mark_node)
20020         name = NULL;
20021     }
20022   cp_parser_require_pragma_eol (parser, pragma_tok);
20023
20024   stmt = cp_parser_omp_structured_block (parser);
20025   return c_finish_omp_critical (stmt, name);
20026 }
20027
20028 /* OpenMP 2.5:
20029    # pragma omp flush flush-vars[opt] new-line
20030
20031    flush-vars:
20032      ( variable-list ) */
20033
20034 static void
20035 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20036 {
20037   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20038     (void) cp_parser_omp_var_list (parser, 0, NULL);
20039   cp_parser_require_pragma_eol (parser, pragma_tok);
20040
20041   finish_omp_flush ();
20042 }
20043
20044 /* Parse the restricted form of the for statment allowed by OpenMP.  */
20045
20046 static tree
20047 cp_parser_omp_for_loop (cp_parser *parser)
20048 {
20049   tree init, cond, incr, body, decl, pre_body;
20050   location_t loc;
20051
20052   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20053     {
20054       cp_parser_error (parser, "for statement expected");
20055       return NULL;
20056     }
20057   loc = cp_lexer_consume_token (parser->lexer)->location;
20058   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20059     return NULL;
20060
20061   init = decl = NULL;
20062   pre_body = push_stmt_list ();
20063   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20064     {
20065       cp_decl_specifier_seq type_specifiers;
20066
20067       /* First, try to parse as an initialized declaration.  See
20068          cp_parser_condition, from whence the bulk of this is copied.  */
20069
20070       cp_parser_parse_tentatively (parser);
20071       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20072                                     &type_specifiers);
20073       if (!cp_parser_error_occurred (parser))
20074         {
20075           tree asm_specification, attributes;
20076           cp_declarator *declarator;
20077
20078           declarator = cp_parser_declarator (parser,
20079                                              CP_PARSER_DECLARATOR_NAMED,
20080                                              /*ctor_dtor_or_conv_p=*/NULL,
20081                                              /*parenthesized_p=*/NULL,
20082                                              /*member_p=*/false);
20083           attributes = cp_parser_attributes_opt (parser);
20084           asm_specification = cp_parser_asm_specification_opt (parser);
20085
20086           cp_parser_require (parser, CPP_EQ, "`='");
20087           if (cp_parser_parse_definitely (parser))
20088             {
20089               tree pushed_scope;
20090
20091               decl = start_decl (declarator, &type_specifiers,
20092                                  /*initialized_p=*/false, attributes,
20093                                  /*prefix_attributes=*/NULL_TREE,
20094                                  &pushed_scope);
20095
20096               init = cp_parser_assignment_expression (parser, false);
20097
20098               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20099                 init = error_mark_node;
20100               else
20101                 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20102                                 asm_specification, LOOKUP_ONLYCONVERTING);
20103
20104               if (pushed_scope)
20105                 pop_scope (pushed_scope);
20106             }
20107         }
20108       else
20109         cp_parser_abort_tentative_parse (parser);
20110
20111       /* If parsing as an initialized declaration failed, try again as
20112          a simple expression.  */
20113       if (decl == NULL)
20114         init = cp_parser_expression (parser, false);
20115     }
20116   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20117   pre_body = pop_stmt_list (pre_body);
20118
20119   cond = NULL;
20120   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20121     cond = cp_parser_condition (parser);
20122   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20123
20124   incr = NULL;
20125   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20126     incr = cp_parser_expression (parser, false);
20127
20128   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20129     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20130                                            /*or_comma=*/false,
20131                                            /*consume_paren=*/true);
20132
20133   /* Note that we saved the original contents of this flag when we entered
20134      the structured block, and so we don't need to re-save it here.  */
20135   parser->in_statement = IN_OMP_FOR;
20136
20137   /* Note that the grammar doesn't call for a structured block here,
20138      though the loop as a whole is a structured block.  */
20139   body = push_stmt_list ();
20140   cp_parser_statement (parser, NULL_TREE, false, NULL);
20141   body = pop_stmt_list (body);
20142
20143   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20144 }
20145
20146 /* OpenMP 2.5:
20147    #pragma omp for for-clause[optseq] new-line
20148      for-loop  */
20149
20150 #define OMP_FOR_CLAUSE_MASK                             \
20151         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20152         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20153         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20154         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20155         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
20156         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
20157         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20158
20159 static tree
20160 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20161 {
20162   tree clauses, sb, ret;
20163   unsigned int save;
20164
20165   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20166                                        "#pragma omp for", pragma_tok);
20167
20168   sb = begin_omp_structured_block ();
20169   save = cp_parser_begin_omp_structured_block (parser);
20170
20171   ret = cp_parser_omp_for_loop (parser);
20172   if (ret)
20173     OMP_FOR_CLAUSES (ret) = clauses;
20174
20175   cp_parser_end_omp_structured_block (parser, save);
20176   add_stmt (finish_omp_structured_block (sb));
20177
20178   return ret;
20179 }
20180
20181 /* OpenMP 2.5:
20182    # pragma omp master new-line
20183      structured-block  */
20184
20185 static tree
20186 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20187 {
20188   cp_parser_require_pragma_eol (parser, pragma_tok);
20189   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20190 }
20191
20192 /* OpenMP 2.5:
20193    # pragma omp ordered new-line
20194      structured-block  */
20195
20196 static tree
20197 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20198 {
20199   cp_parser_require_pragma_eol (parser, pragma_tok);
20200   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20201 }
20202
20203 /* OpenMP 2.5:
20204
20205    section-scope:
20206      { section-sequence }
20207
20208    section-sequence:
20209      section-directive[opt] structured-block
20210      section-sequence section-directive structured-block  */
20211
20212 static tree
20213 cp_parser_omp_sections_scope (cp_parser *parser)
20214 {
20215   tree stmt, substmt;
20216   bool error_suppress = false;
20217   cp_token *tok;
20218
20219   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20220     return NULL_TREE;
20221
20222   stmt = push_stmt_list ();
20223
20224   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20225     {
20226       unsigned save;
20227
20228       substmt = begin_omp_structured_block ();
20229       save = cp_parser_begin_omp_structured_block (parser);
20230
20231       while (1)
20232         {
20233           cp_parser_statement (parser, NULL_TREE, false, NULL);
20234
20235           tok = cp_lexer_peek_token (parser->lexer);
20236           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20237             break;
20238           if (tok->type == CPP_CLOSE_BRACE)
20239             break;
20240           if (tok->type == CPP_EOF)
20241             break;
20242         }
20243
20244       cp_parser_end_omp_structured_block (parser, save);
20245       substmt = finish_omp_structured_block (substmt);
20246       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20247       add_stmt (substmt);
20248     }
20249
20250   while (1)
20251     {
20252       tok = cp_lexer_peek_token (parser->lexer);
20253       if (tok->type == CPP_CLOSE_BRACE)
20254         break;
20255       if (tok->type == CPP_EOF)
20256         break;
20257
20258       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20259         {
20260           cp_lexer_consume_token (parser->lexer);
20261           cp_parser_require_pragma_eol (parser, tok);
20262           error_suppress = false;
20263         }
20264       else if (!error_suppress)
20265         {
20266           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20267           error_suppress = true;
20268         }
20269
20270       substmt = cp_parser_omp_structured_block (parser);
20271       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20272       add_stmt (substmt);
20273     }
20274   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20275
20276   substmt = pop_stmt_list (stmt);
20277
20278   stmt = make_node (OMP_SECTIONS);
20279   TREE_TYPE (stmt) = void_type_node;
20280   OMP_SECTIONS_BODY (stmt) = substmt;
20281
20282   add_stmt (stmt);
20283   return stmt;
20284 }
20285
20286 /* OpenMP 2.5:
20287    # pragma omp sections sections-clause[optseq] newline
20288      sections-scope  */
20289
20290 #define OMP_SECTIONS_CLAUSE_MASK                        \
20291         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20292         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20293         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20294         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20295         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20296
20297 static tree
20298 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20299 {
20300   tree clauses, ret;
20301
20302   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20303                                        "#pragma omp sections", pragma_tok);
20304
20305   ret = cp_parser_omp_sections_scope (parser);
20306   if (ret)
20307     OMP_SECTIONS_CLAUSES (ret) = clauses;
20308
20309   return ret;
20310 }
20311
20312 /* OpenMP 2.5:
20313    # pragma parallel parallel-clause new-line
20314    # pragma parallel for parallel-for-clause new-line
20315    # pragma parallel sections parallel-sections-clause new-line  */
20316
20317 #define OMP_PARALLEL_CLAUSE_MASK                        \
20318         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20319         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20320         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20321         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20322         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20323         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20324         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20325         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20326
20327 static tree
20328 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20329 {
20330   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20331   const char *p_name = "#pragma omp parallel";
20332   tree stmt, clauses, par_clause, ws_clause, block;
20333   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20334   unsigned int save;
20335
20336   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20337     {
20338       cp_lexer_consume_token (parser->lexer);
20339       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20340       p_name = "#pragma omp parallel for";
20341       mask |= OMP_FOR_CLAUSE_MASK;
20342       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20343     }
20344   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20345     {
20346       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20347       const char *p = IDENTIFIER_POINTER (id);
20348       if (strcmp (p, "sections") == 0)
20349         {
20350           cp_lexer_consume_token (parser->lexer);
20351           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20352           p_name = "#pragma omp parallel sections";
20353           mask |= OMP_SECTIONS_CLAUSE_MASK;
20354           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20355         }
20356     }
20357
20358   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20359   block = begin_omp_parallel ();
20360   save = cp_parser_begin_omp_structured_block (parser);
20361
20362   switch (p_kind)
20363     {
20364     case PRAGMA_OMP_PARALLEL:
20365       cp_parser_statement (parser, NULL_TREE, false, NULL);
20366       par_clause = clauses;
20367       break;
20368
20369     case PRAGMA_OMP_PARALLEL_FOR:
20370       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20371       stmt = cp_parser_omp_for_loop (parser);
20372       if (stmt)
20373         OMP_FOR_CLAUSES (stmt) = ws_clause;
20374       break;
20375
20376     case PRAGMA_OMP_PARALLEL_SECTIONS:
20377       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20378       stmt = cp_parser_omp_sections_scope (parser);
20379       if (stmt)
20380         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20381       break;
20382
20383     default:
20384       gcc_unreachable ();
20385     }
20386
20387   cp_parser_end_omp_structured_block (parser, save);
20388   stmt = finish_omp_parallel (par_clause, block);
20389   if (p_kind != PRAGMA_OMP_PARALLEL)
20390     OMP_PARALLEL_COMBINED (stmt) = 1;
20391   return stmt;
20392 }
20393
20394 /* OpenMP 2.5:
20395    # pragma omp single single-clause[optseq] new-line
20396      structured-block  */
20397
20398 #define OMP_SINGLE_CLAUSE_MASK                          \
20399         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20400         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20401         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20402         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20403
20404 static tree
20405 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20406 {
20407   tree stmt = make_node (OMP_SINGLE);
20408   TREE_TYPE (stmt) = void_type_node;
20409
20410   OMP_SINGLE_CLAUSES (stmt)
20411     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20412                                  "#pragma omp single", pragma_tok);
20413   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20414
20415   return add_stmt (stmt);
20416 }
20417
20418 /* OpenMP 2.5:
20419    # pragma omp threadprivate (variable-list) */
20420
20421 static void
20422 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20423 {
20424   tree vars;
20425
20426   vars = cp_parser_omp_var_list (parser, 0, NULL);
20427   cp_parser_require_pragma_eol (parser, pragma_tok);
20428
20429   finish_omp_threadprivate (vars);
20430 }
20431
20432 /* Main entry point to OpenMP statement pragmas.  */
20433
20434 static void
20435 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20436 {
20437   tree stmt;
20438
20439   switch (pragma_tok->pragma_kind)
20440     {
20441     case PRAGMA_OMP_ATOMIC:
20442       cp_parser_omp_atomic (parser, pragma_tok);
20443       return;
20444     case PRAGMA_OMP_CRITICAL:
20445       stmt = cp_parser_omp_critical (parser, pragma_tok);
20446       break;
20447     case PRAGMA_OMP_FOR:
20448       stmt = cp_parser_omp_for (parser, pragma_tok);
20449       break;
20450     case PRAGMA_OMP_MASTER:
20451       stmt = cp_parser_omp_master (parser, pragma_tok);
20452       break;
20453     case PRAGMA_OMP_ORDERED:
20454       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20455       break;
20456     case PRAGMA_OMP_PARALLEL:
20457       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20458       break;
20459     case PRAGMA_OMP_SECTIONS:
20460       stmt = cp_parser_omp_sections (parser, pragma_tok);
20461       break;
20462     case PRAGMA_OMP_SINGLE:
20463       stmt = cp_parser_omp_single (parser, pragma_tok);
20464       break;
20465     default:
20466       gcc_unreachable ();
20467     }
20468
20469   if (stmt)
20470     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20471 }
20472 \f
20473 /* The parser.  */
20474
20475 static GTY (()) cp_parser *the_parser;
20476
20477 \f
20478 /* Special handling for the first token or line in the file.  The first
20479    thing in the file might be #pragma GCC pch_preprocess, which loads a
20480    PCH file, which is a GC collection point.  So we need to handle this
20481    first pragma without benefit of an existing lexer structure.
20482
20483    Always returns one token to the caller in *FIRST_TOKEN.  This is
20484    either the true first token of the file, or the first token after
20485    the initial pragma.  */
20486
20487 static void
20488 cp_parser_initial_pragma (cp_token *first_token)
20489 {
20490   tree name = NULL;
20491
20492   cp_lexer_get_preprocessor_token (NULL, first_token);
20493   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20494     return;
20495
20496   cp_lexer_get_preprocessor_token (NULL, first_token);
20497   if (first_token->type == CPP_STRING)
20498     {
20499       name = first_token->u.value;
20500
20501       cp_lexer_get_preprocessor_token (NULL, first_token);
20502       if (first_token->type != CPP_PRAGMA_EOL)
20503         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20504     }
20505   else
20506     error ("expected string literal");
20507
20508   /* Skip to the end of the pragma.  */
20509   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20510     cp_lexer_get_preprocessor_token (NULL, first_token);
20511
20512   /* Now actually load the PCH file.  */
20513   if (name)
20514     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20515
20516   /* Read one more token to return to our caller.  We have to do this
20517      after reading the PCH file in, since its pointers have to be
20518      live.  */
20519   cp_lexer_get_preprocessor_token (NULL, first_token);
20520 }
20521
20522 /* Normal parsing of a pragma token.  Here we can (and must) use the
20523    regular lexer.  */
20524
20525 static bool
20526 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20527 {
20528   cp_token *pragma_tok;
20529   unsigned int id;
20530
20531   pragma_tok = cp_lexer_consume_token (parser->lexer);
20532   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20533   parser->lexer->in_pragma = true;
20534
20535   id = pragma_tok->pragma_kind;
20536   switch (id)
20537     {
20538     case PRAGMA_GCC_PCH_PREPROCESS:
20539       error ("%<#pragma GCC pch_preprocess%> must be first");
20540       break;
20541
20542     case PRAGMA_OMP_BARRIER:
20543       switch (context)
20544         {
20545         case pragma_compound:
20546           cp_parser_omp_barrier (parser, pragma_tok);
20547           return false;
20548         case pragma_stmt:
20549           error ("%<#pragma omp barrier%> may only be "
20550                  "used in compound statements");
20551           break;
20552         default:
20553           goto bad_stmt;
20554         }
20555       break;
20556
20557     case PRAGMA_OMP_FLUSH:
20558       switch (context)
20559         {
20560         case pragma_compound:
20561           cp_parser_omp_flush (parser, pragma_tok);
20562           return false;
20563         case pragma_stmt:
20564           error ("%<#pragma omp flush%> may only be "
20565                  "used in compound statements");
20566           break;
20567         default:
20568           goto bad_stmt;
20569         }
20570       break;
20571
20572     case PRAGMA_OMP_THREADPRIVATE:
20573       cp_parser_omp_threadprivate (parser, pragma_tok);
20574       return false;
20575
20576     case PRAGMA_OMP_ATOMIC:
20577     case PRAGMA_OMP_CRITICAL:
20578     case PRAGMA_OMP_FOR:
20579     case PRAGMA_OMP_MASTER:
20580     case PRAGMA_OMP_ORDERED:
20581     case PRAGMA_OMP_PARALLEL:
20582     case PRAGMA_OMP_SECTIONS:
20583     case PRAGMA_OMP_SINGLE:
20584       if (context == pragma_external)
20585         goto bad_stmt;
20586       cp_parser_omp_construct (parser, pragma_tok);
20587       return true;
20588
20589     case PRAGMA_OMP_SECTION:
20590       error ("%<#pragma omp section%> may only be used in "
20591              "%<#pragma omp sections%> construct");
20592       break;
20593
20594     default:
20595       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20596       c_invoke_pragma_handler (id);
20597       break;
20598
20599     bad_stmt:
20600       cp_parser_error (parser, "expected declaration specifiers");
20601       break;
20602     }
20603
20604   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20605   return false;
20606 }
20607
20608 /* The interface the pragma parsers have to the lexer.  */
20609
20610 enum cpp_ttype
20611 pragma_lex (tree *value)
20612 {
20613   cp_token *tok;
20614   enum cpp_ttype ret;
20615
20616   tok = cp_lexer_peek_token (the_parser->lexer);
20617
20618   ret = tok->type;
20619   *value = tok->u.value;
20620
20621   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20622     ret = CPP_EOF;
20623   else if (ret == CPP_STRING)
20624     *value = cp_parser_string_literal (the_parser, false, false);
20625   else
20626     {
20627       cp_lexer_consume_token (the_parser->lexer);
20628       if (ret == CPP_KEYWORD)
20629         ret = CPP_NAME;
20630     }
20631
20632   return ret;
20633 }
20634
20635 \f
20636 /* External interface.  */
20637
20638 /* Parse one entire translation unit.  */
20639
20640 void
20641 c_parse_file (void)
20642 {
20643   bool error_occurred;
20644   static bool already_called = false;
20645
20646   if (already_called)
20647     {
20648       sorry ("inter-module optimizations not implemented for C++");
20649       return;
20650     }
20651   already_called = true;
20652
20653   the_parser = cp_parser_new ();
20654   push_deferring_access_checks (flag_access_control
20655                                 ? dk_no_deferred : dk_no_check);
20656   error_occurred = cp_parser_translation_unit (the_parser);
20657   the_parser = NULL;
20658 }
20659
20660 #include "gt-cp-parser.h"