remove unused files
[platform/upstream/gcc48.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000-2013 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 static cp_token eof_token =
49 {
50   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
51 };
52
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant {
55   NIC_NONE,
56   /* floating-point literal */
57   NIC_FLOAT,
58   /* %<this%> */
59   NIC_THIS,
60   /* %<__FUNCTION__%> */
61   NIC_FUNC_NAME,
62   /* %<__PRETTY_FUNCTION__%> */
63   NIC_PRETTY_FUNC,
64   /* %<__func__%> */
65   NIC_C99_FUNC,
66   /* "%<va_arg%> */
67   NIC_VA_ARG,
68   /* a cast */
69   NIC_CAST,
70   /* %<typeid%> operator */
71   NIC_TYPEID,
72   /* non-constant compound literals */
73   NIC_NCC,
74   /* a function call */
75   NIC_FUNC_CALL,
76   /* an increment */
77   NIC_INC,
78   /* an decrement */
79   NIC_DEC,
80   /* an array reference */
81   NIC_ARRAY_REF,
82   /* %<->%> */
83   NIC_ARROW,
84   /* %<.%> */
85   NIC_POINT,
86   /* the address of a label */
87   NIC_ADDR_LABEL,
88   /* %<*%> */
89   NIC_STAR,
90   /* %<&%> */
91   NIC_ADDR,
92   /* %<++%> */
93   NIC_PREINCREMENT,
94   /* %<--%> */
95   NIC_PREDECREMENT,
96   /* %<new%> */
97   NIC_NEW,
98   /* %<delete%> */
99   NIC_DEL,
100   /* calls to overloaded operators */
101   NIC_OVERLOADED,
102   /* an assignment */
103   NIC_ASSIGNMENT,
104   /* a comma operator */
105   NIC_COMMA,
106   /* a call to a constructor */
107   NIC_CONSTRUCTOR,
108   /* a transaction expression */
109   NIC_TRANSACTION
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
175   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
176   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
177   RT_TRANSACTION_CANCEL /* __transaction_cancel */
178 } required_token;
179
180 /* Prototypes.  */
181
182 static cp_lexer *cp_lexer_new_main
183   (void);
184 static cp_lexer *cp_lexer_new_from_tokens
185   (cp_token_cache *tokens);
186 static void cp_lexer_destroy
187   (cp_lexer *);
188 static int cp_lexer_saving_tokens
189   (const cp_lexer *);
190 static cp_token *cp_lexer_token_at
191   (cp_lexer *, cp_token_position);
192 static void cp_lexer_get_preprocessor_token
193   (cp_lexer *, cp_token *);
194 static inline cp_token *cp_lexer_peek_token
195   (cp_lexer *);
196 static cp_token *cp_lexer_peek_nth_token
197   (cp_lexer *, size_t);
198 static inline bool cp_lexer_next_token_is
199   (cp_lexer *, enum cpp_ttype);
200 static bool cp_lexer_next_token_is_not
201   (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_keyword
203   (cp_lexer *, enum rid);
204 static cp_token *cp_lexer_consume_token
205   (cp_lexer *);
206 static void cp_lexer_purge_token
207   (cp_lexer *);
208 static void cp_lexer_purge_tokens_after
209   (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
211   (cp_lexer *);
212 static void cp_lexer_commit_tokens
213   (cp_lexer *);
214 static void cp_lexer_rollback_tokens
215   (cp_lexer *);
216 static void cp_lexer_print_token
217   (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
219   (cp_lexer *);
220 static void cp_lexer_start_debugging
221   (cp_lexer *) ATTRIBUTE_UNUSED;
222 static void cp_lexer_stop_debugging
223   (cp_lexer *) ATTRIBUTE_UNUSED;
224
225 static cp_token_cache *cp_token_cache_new
226   (cp_token *, cp_token *);
227
228 static void cp_parser_initial_pragma
229   (cp_token *);
230
231 static tree cp_literal_operator_id
232   (const char *);
233
234 /* Manifest constants.  */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
237
238 /* Variables.  */
239
240 /* The stream to which debugging output should be written.  */
241 static FILE *cp_lexer_debug_stream;
242
243 /* Nonzero if we are parsing an unevaluated operand: an operand to
244    sizeof, typeof, or alignof.  */
245 int cp_unevaluated_operand;
246
247 /* Dump up to NUM tokens in BUFFER to FILE starting with token
248    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
249    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
250    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
251    highlighted by surrounding it in [[ ]].  */
252
253 static void
254 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
255                       cp_token *start_token, unsigned num,
256                       cp_token *curr_token)
257 {
258   unsigned i, nprinted;
259   cp_token *token;
260   bool do_print;
261
262   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
263
264   if (buffer == NULL)
265     return;
266
267   if (num == 0)
268     num = buffer->length ();
269
270   if (start_token == NULL)
271     start_token = buffer->address ();
272
273   if (start_token > buffer->address ())
274     {
275       cp_lexer_print_token (file, &(*buffer)[0]);
276       fprintf (file, " ... ");
277     }
278
279   do_print = false;
280   nprinted = 0;
281   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
282     {
283       if (token == start_token)
284         do_print = true;
285
286       if (!do_print)
287         continue;
288
289       nprinted++;
290       if (token == curr_token)
291         fprintf (file, "[[");
292
293       cp_lexer_print_token (file, token);
294
295       if (token == curr_token)
296         fprintf (file, "]]");
297
298       switch (token->type)
299         {
300           case CPP_SEMICOLON:
301           case CPP_OPEN_BRACE:
302           case CPP_CLOSE_BRACE:
303           case CPP_EOF:
304             fputc ('\n', file);
305             break;
306
307           default:
308             fputc (' ', file);
309         }
310     }
311
312   if (i == num && i < buffer->length ())
313     {
314       fprintf (file, " ... ");
315       cp_lexer_print_token (file, &buffer->last ());
316     }
317
318   fprintf (file, "\n");
319 }
320
321
322 /* Dump all tokens in BUFFER to stderr.  */
323
324 void
325 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
326 {
327   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
328 }
329
330
331 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
332    description for T.  */
333
334 static void
335 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
336 {
337   if (t)
338     {
339       fprintf (file, "%s: ", desc);
340       print_node_brief (file, "", t, 0);
341     }
342 }
343
344
345 /* Dump parser context C to FILE.  */
346
347 static void
348 cp_debug_print_context (FILE *file, cp_parser_context *c)
349 {
350   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
351   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
352   print_node_brief (file, "", c->object_type, 0);
353   fprintf (file, "}\n");
354 }
355
356
357 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
358
359 static void
360 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
361 {
362   unsigned i;
363   cp_parser_context *c;
364
365   fprintf (file, "Parsing context stack:\n");
366   for (i = 0, c = first; c; c = c->next, i++)
367     {
368       fprintf (file, "\t#%u: ", i);
369       cp_debug_print_context (file, c);
370     }
371 }
372
373
374 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
375
376 static void
377 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
378 {
379   if (flag)
380     fprintf (file, "%s: true\n", desc);
381 }
382
383
384 /* Print an unparsed function entry UF to FILE.  */
385
386 static void
387 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
388 {
389   unsigned i;
390   cp_default_arg_entry *default_arg_fn;
391   tree fn;
392
393   fprintf (file, "\tFunctions with default args:\n");
394   for (i = 0;
395        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
396        i++)
397     {
398       fprintf (file, "\t\tClass type: ");
399       print_node_brief (file, "", default_arg_fn->class_type, 0);
400       fprintf (file, "\t\tDeclaration: ");
401       print_node_brief (file, "", default_arg_fn->decl, 0);
402       fprintf (file, "\n");
403     }
404
405   fprintf (file, "\n\tFunctions with definitions that require "
406            "post-processing\n\t\t");
407   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
408     {
409       print_node_brief (file, "", fn, 0);
410       fprintf (file, " ");
411     }
412   fprintf (file, "\n");
413
414   fprintf (file, "\n\tNon-static data members with initializers that require "
415            "post-processing\n\t\t");
416   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
417     {
418       print_node_brief (file, "", fn, 0);
419       fprintf (file, " ");
420     }
421   fprintf (file, "\n");
422 }
423
424
425 /* Print the stack of unparsed member functions S to FILE.  */
426
427 static void
428 cp_debug_print_unparsed_queues (FILE *file,
429                                 vec<cp_unparsed_functions_entry, va_gc> *s)
430 {
431   unsigned i;
432   cp_unparsed_functions_entry *uf;
433
434   fprintf (file, "Unparsed functions\n");
435   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
436     {
437       fprintf (file, "#%u:\n", i);
438       cp_debug_print_unparsed_function (file, uf);
439     }
440 }
441
442
443 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
444    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
445
446 static void
447 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
448 {
449   cp_token *next_token, *first_token, *start_token;
450
451   if (file == NULL)
452     file = stderr;
453
454   next_token = parser->lexer->next_token;
455   first_token = parser->lexer->buffer->address ();
456   start_token = (next_token > first_token + window_size / 2)
457                 ? next_token - window_size / 2
458                 : first_token;
459   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
460                         next_token);
461 }
462
463
464 /* Dump debugging information for the given PARSER.  If FILE is NULL,
465    the output is printed on stderr.  */
466
467 void
468 cp_debug_parser (FILE *file, cp_parser *parser)
469 {
470   const size_t window_size = 20;
471   cp_token *token;
472   expanded_location eloc;
473
474   if (file == NULL)
475     file = stderr;
476
477   fprintf (file, "Parser state\n\n");
478   fprintf (file, "Number of tokens: %u\n",
479            vec_safe_length (parser->lexer->buffer));
480   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
481   cp_debug_print_tree_if_set (file, "Object scope",
482                                      parser->object_scope);
483   cp_debug_print_tree_if_set (file, "Qualifying scope",
484                                      parser->qualifying_scope);
485   cp_debug_print_context_stack (file, parser->context);
486   cp_debug_print_flag (file, "Allow GNU extensions",
487                               parser->allow_gnu_extensions_p);
488   cp_debug_print_flag (file, "'>' token is greater-than",
489                               parser->greater_than_is_operator_p);
490   cp_debug_print_flag (file, "Default args allowed in current "
491                               "parameter list", parser->default_arg_ok_p);
492   cp_debug_print_flag (file, "Parsing integral constant-expression",
493                               parser->integral_constant_expression_p);
494   cp_debug_print_flag (file, "Allow non-constant expression in current "
495                               "constant-expression",
496                               parser->allow_non_integral_constant_expression_p);
497   cp_debug_print_flag (file, "Seen non-constant expression",
498                               parser->non_integral_constant_expression_p);
499   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
500                               "current context",
501                               parser->local_variables_forbidden_p);
502   cp_debug_print_flag (file, "In unbraced linkage specification",
503                               parser->in_unbraced_linkage_specification_p);
504   cp_debug_print_flag (file, "Parsing a declarator",
505                               parser->in_declarator_p);
506   cp_debug_print_flag (file, "In template argument list",
507                               parser->in_template_argument_list_p);
508   cp_debug_print_flag (file, "Parsing an iteration statement",
509                               parser->in_statement & IN_ITERATION_STMT);
510   cp_debug_print_flag (file, "Parsing a switch statement",
511                               parser->in_statement & IN_SWITCH_STMT);
512   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
513                               parser->in_statement & IN_OMP_BLOCK);
514   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
515                               parser->in_statement & IN_OMP_FOR);
516   cp_debug_print_flag (file, "Parsing an if statement",
517                               parser->in_statement & IN_IF_STMT);
518   cp_debug_print_flag (file, "Parsing a type-id in an expression "
519                               "context", parser->in_type_id_in_expr_p);
520   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
521                               parser->implicit_extern_c);
522   cp_debug_print_flag (file, "String expressions should be translated "
523                               "to execution character set",
524                               parser->translate_strings_p);
525   cp_debug_print_flag (file, "Parsing function body outside of a "
526                               "local class", parser->in_function_body);
527   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
528                               parser->colon_corrects_to_scope_p);
529   if (parser->type_definition_forbidden_message)
530     fprintf (file, "Error message for forbidden type definitions: %s\n",
531              parser->type_definition_forbidden_message);
532   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
533   fprintf (file, "Number of class definitions in progress: %u\n",
534            parser->num_classes_being_defined);
535   fprintf (file, "Number of template parameter lists for the current "
536            "declaration: %u\n", parser->num_template_parameter_lists);
537   cp_debug_parser_tokens (file, parser, window_size);
538   token = parser->lexer->next_token;
539   fprintf (file, "Next token to parse:\n");
540   fprintf (file, "\tToken:  ");
541   cp_lexer_print_token (file, token);
542   eloc = expand_location (token->location);
543   fprintf (file, "\n\tFile:   %s\n", eloc.file);
544   fprintf (file, "\tLine:   %d\n", eloc.line);
545   fprintf (file, "\tColumn: %d\n", eloc.column);
546 }
547
548
549 /* Allocate memory for a new lexer object and return it.  */
550
551 static cp_lexer *
552 cp_lexer_alloc (void)
553 {
554   cp_lexer *lexer;
555
556   c_common_no_more_pch ();
557
558   /* Allocate the memory.  */
559   lexer = ggc_alloc_cleared_cp_lexer ();
560
561   /* Initially we are not debugging.  */
562   lexer->debugging_p = false;
563
564   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
565
566   /* Create the buffer.  */
567   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
568
569   return lexer;
570 }
571
572
573 /* Create a new main C++ lexer, the lexer that gets tokens from the
574    preprocessor.  */
575
576 static cp_lexer *
577 cp_lexer_new_main (void)
578 {
579   cp_lexer *lexer;
580   cp_token token;
581
582   /* It's possible that parsing the first pragma will load a PCH file,
583      which is a GC collection point.  So we have to do that before
584      allocating any memory.  */
585   cp_parser_initial_pragma (&token);
586
587   lexer = cp_lexer_alloc ();
588
589   /* Put the first token in the buffer.  */
590   lexer->buffer->quick_push (token);
591
592   /* Get the remaining tokens from the preprocessor.  */
593   while (token.type != CPP_EOF)
594     {
595       cp_lexer_get_preprocessor_token (lexer, &token);
596       vec_safe_push (lexer->buffer, token);
597     }
598
599   lexer->last_token = lexer->buffer->address ()
600                       + lexer->buffer->length ()
601                       - 1;
602   lexer->next_token = lexer->buffer->length ()
603                       ? lexer->buffer->address ()
604                       : &eof_token;
605
606   /* Subsequent preprocessor diagnostics should use compiler
607      diagnostic functions to get the compiler source location.  */
608   done_lexing = true;
609
610   gcc_assert (!lexer->next_token->purged_p);
611   return lexer;
612 }
613
614 /* Create a new lexer whose token stream is primed with the tokens in
615    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
616
617 static cp_lexer *
618 cp_lexer_new_from_tokens (cp_token_cache *cache)
619 {
620   cp_token *first = cache->first;
621   cp_token *last = cache->last;
622   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
623
624   /* We do not own the buffer.  */
625   lexer->buffer = NULL;
626   lexer->next_token = first == last ? &eof_token : first;
627   lexer->last_token = last;
628
629   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630
631   /* Initially we are not debugging.  */
632   lexer->debugging_p = false;
633
634   gcc_assert (!lexer->next_token->purged_p);
635   return lexer;
636 }
637
638 /* Frees all resources associated with LEXER.  */
639
640 static void
641 cp_lexer_destroy (cp_lexer *lexer)
642 {
643   vec_free (lexer->buffer);
644   lexer->saved_tokens.release ();
645   ggc_free (lexer);
646 }
647
648 /* Returns nonzero if debugging information should be output.  */
649
650 static inline bool
651 cp_lexer_debugging_p (cp_lexer *lexer)
652 {
653   return lexer->debugging_p;
654 }
655
656
657 static inline cp_token_position
658 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
659 {
660   gcc_assert (!previous_p || lexer->next_token != &eof_token);
661
662   return lexer->next_token - previous_p;
663 }
664
665 static inline cp_token *
666 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
667 {
668   return pos;
669 }
670
671 static inline void
672 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
673 {
674   lexer->next_token = cp_lexer_token_at (lexer, pos);
675 }
676
677 static inline cp_token_position
678 cp_lexer_previous_token_position (cp_lexer *lexer)
679 {
680   if (lexer->next_token == &eof_token)
681     return lexer->last_token - 1;
682   else
683     return cp_lexer_token_position (lexer, true);
684 }
685
686 static inline cp_token *
687 cp_lexer_previous_token (cp_lexer *lexer)
688 {
689   cp_token_position tp = cp_lexer_previous_token_position (lexer);
690
691   return cp_lexer_token_at (lexer, tp);
692 }
693
694 /* nonzero if we are presently saving tokens.  */
695
696 static inline int
697 cp_lexer_saving_tokens (const cp_lexer* lexer)
698 {
699   return lexer->saved_tokens.length () != 0;
700 }
701
702 /* Store the next token from the preprocessor in *TOKEN.  Return true
703    if we reach EOF.  If LEXER is NULL, assume we are handling an
704    initial #pragma pch_preprocess, and thus want the lexer to return
705    processed strings.  */
706
707 static void
708 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
709 {
710   static int is_extern_c = 0;
711
712    /* Get a new token from the preprocessor.  */
713   token->type
714     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
715                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
716   token->keyword = RID_MAX;
717   token->pragma_kind = PRAGMA_NONE;
718   token->purged_p = false;
719
720   /* On some systems, some header files are surrounded by an
721      implicit extern "C" block.  Set a flag in the token if it
722      comes from such a header.  */
723   is_extern_c += pending_lang_change;
724   pending_lang_change = 0;
725   token->implicit_extern_c = is_extern_c > 0;
726
727   /* Check to see if this token is a keyword.  */
728   if (token->type == CPP_NAME)
729     {
730       if (C_IS_RESERVED_WORD (token->u.value))
731         {
732           /* Mark this token as a keyword.  */
733           token->type = CPP_KEYWORD;
734           /* Record which keyword.  */
735           token->keyword = C_RID_CODE (token->u.value);
736         }
737       else
738         {
739           if (warn_cxx0x_compat
740               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
741               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
742             {
743               /* Warn about the C++0x keyword (but still treat it as
744                  an identifier).  */
745               warning (OPT_Wc__0x_compat, 
746                        "identifier %qE is a keyword in C++11",
747                        token->u.value);
748
749               /* Clear out the C_RID_CODE so we don't warn about this
750                  particular identifier-turned-keyword again.  */
751               C_SET_RID_CODE (token->u.value, RID_MAX);
752             }
753
754           token->ambiguous_p = false;
755           token->keyword = RID_MAX;
756         }
757     }
758   else if (token->type == CPP_AT_NAME)
759     {
760       /* This only happens in Objective-C++; it must be a keyword.  */
761       token->type = CPP_KEYWORD;
762       switch (C_RID_CODE (token->u.value))
763         {
764           /* Replace 'class' with '@class', 'private' with '@private',
765              etc.  This prevents confusion with the C++ keyword
766              'class', and makes the tokens consistent with other
767              Objective-C 'AT' keywords.  For example '@class' is
768              reported as RID_AT_CLASS which is consistent with
769              '@synchronized', which is reported as
770              RID_AT_SYNCHRONIZED.
771           */
772         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
773         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
774         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
775         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
776         case RID_THROW:     token->keyword = RID_AT_THROW; break;
777         case RID_TRY:       token->keyword = RID_AT_TRY; break;
778         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
779         default:            token->keyword = C_RID_CODE (token->u.value);
780         }
781     }
782   else if (token->type == CPP_PRAGMA)
783     {
784       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
785       token->pragma_kind = ((enum pragma_kind)
786                             TREE_INT_CST_LOW (token->u.value));
787       token->u.value = NULL_TREE;
788     }
789 }
790
791 /* Update the globals input_location and the input file stack from TOKEN.  */
792 static inline void
793 cp_lexer_set_source_position_from_token (cp_token *token)
794 {
795   if (token->type != CPP_EOF)
796     {
797       input_location = token->location;
798     }
799 }
800
801 /* Return a pointer to the next token in the token stream, but do not
802    consume it.  */
803
804 static inline cp_token *
805 cp_lexer_peek_token (cp_lexer *lexer)
806 {
807   if (cp_lexer_debugging_p (lexer))
808     {
809       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
810       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
811       putc ('\n', cp_lexer_debug_stream);
812     }
813   return lexer->next_token;
814 }
815
816 /* Return true if the next token has the indicated TYPE.  */
817
818 static inline bool
819 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
820 {
821   return cp_lexer_peek_token (lexer)->type == type;
822 }
823
824 /* Return true if the next token does not have the indicated TYPE.  */
825
826 static inline bool
827 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
828 {
829   return !cp_lexer_next_token_is (lexer, type);
830 }
831
832 /* Return true if the next token is the indicated KEYWORD.  */
833
834 static inline bool
835 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
836 {
837   return cp_lexer_peek_token (lexer)->keyword == keyword;
838 }
839
840 /* Return true if the next token is not the indicated KEYWORD.  */
841
842 static inline bool
843 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
844 {
845   return cp_lexer_peek_token (lexer)->keyword != keyword;
846 }
847
848 /* Return true if the next token is a keyword for a decl-specifier.  */
849
850 static bool
851 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
852 {
853   cp_token *token;
854
855   token = cp_lexer_peek_token (lexer);
856   switch (token->keyword) 
857     {
858       /* auto specifier: storage-class-specifier in C++,
859          simple-type-specifier in C++0x.  */
860     case RID_AUTO:
861       /* Storage classes.  */
862     case RID_REGISTER:
863     case RID_STATIC:
864     case RID_EXTERN:
865     case RID_MUTABLE:
866     case RID_THREAD:
867       /* Elaborated type specifiers.  */
868     case RID_ENUM:
869     case RID_CLASS:
870     case RID_STRUCT:
871     case RID_UNION:
872     case RID_TYPENAME:
873       /* Simple type specifiers.  */
874     case RID_CHAR:
875     case RID_CHAR16:
876     case RID_CHAR32:
877     case RID_WCHAR:
878     case RID_BOOL:
879     case RID_SHORT:
880     case RID_INT:
881     case RID_LONG:
882     case RID_INT128:
883     case RID_SIGNED:
884     case RID_UNSIGNED:
885     case RID_FLOAT:
886     case RID_DOUBLE:
887     case RID_VOID:
888       /* GNU extensions.  */ 
889     case RID_ATTRIBUTE:
890     case RID_TYPEOF:
891       /* C++0x extensions.  */
892     case RID_DECLTYPE:
893     case RID_UNDERLYING_TYPE:
894       return true;
895
896     default:
897       return false;
898     }
899 }
900
901 /* Returns TRUE iff the token T begins a decltype type.  */
902
903 static bool
904 token_is_decltype (cp_token *t)
905 {
906   return (t->keyword == RID_DECLTYPE
907           || t->type == CPP_DECLTYPE);
908 }
909
910 /* Returns TRUE iff the next token begins a decltype type.  */
911
912 static bool
913 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
914 {
915   cp_token *t = cp_lexer_peek_token (lexer);
916   return token_is_decltype (t);
917 }
918
919 /* Return a pointer to the Nth token in the token stream.  If N is 1,
920    then this is precisely equivalent to cp_lexer_peek_token (except
921    that it is not inline).  One would like to disallow that case, but
922    there is one case (cp_parser_nth_token_starts_template_id) where
923    the caller passes a variable for N and it might be 1.  */
924
925 static cp_token *
926 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
927 {
928   cp_token *token;
929
930   /* N is 1-based, not zero-based.  */
931   gcc_assert (n > 0);
932
933   if (cp_lexer_debugging_p (lexer))
934     fprintf (cp_lexer_debug_stream,
935              "cp_lexer: peeking ahead %ld at token: ", (long)n);
936
937   --n;
938   token = lexer->next_token;
939   gcc_assert (!n || token != &eof_token);
940   while (n != 0)
941     {
942       ++token;
943       if (token == lexer->last_token)
944         {
945           token = &eof_token;
946           break;
947         }
948
949       if (!token->purged_p)
950         --n;
951     }
952
953   if (cp_lexer_debugging_p (lexer))
954     {
955       cp_lexer_print_token (cp_lexer_debug_stream, token);
956       putc ('\n', cp_lexer_debug_stream);
957     }
958
959   return token;
960 }
961
962 /* Return the next token, and advance the lexer's next_token pointer
963    to point to the next non-purged token.  */
964
965 static cp_token *
966 cp_lexer_consume_token (cp_lexer* lexer)
967 {
968   cp_token *token = lexer->next_token;
969
970   gcc_assert (token != &eof_token);
971   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
972
973   do
974     {
975       lexer->next_token++;
976       if (lexer->next_token == lexer->last_token)
977         {
978           lexer->next_token = &eof_token;
979           break;
980         }
981
982     }
983   while (lexer->next_token->purged_p);
984
985   cp_lexer_set_source_position_from_token (token);
986
987   /* Provide debugging output.  */
988   if (cp_lexer_debugging_p (lexer))
989     {
990       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
991       cp_lexer_print_token (cp_lexer_debug_stream, token);
992       putc ('\n', cp_lexer_debug_stream);
993     }
994
995   return token;
996 }
997
998 /* Permanently remove the next token from the token stream, and
999    advance the next_token pointer to refer to the next non-purged
1000    token.  */
1001
1002 static void
1003 cp_lexer_purge_token (cp_lexer *lexer)
1004 {
1005   cp_token *tok = lexer->next_token;
1006
1007   gcc_assert (tok != &eof_token);
1008   tok->purged_p = true;
1009   tok->location = UNKNOWN_LOCATION;
1010   tok->u.value = NULL_TREE;
1011   tok->keyword = RID_MAX;
1012
1013   do
1014     {
1015       tok++;
1016       if (tok == lexer->last_token)
1017         {
1018           tok = &eof_token;
1019           break;
1020         }
1021     }
1022   while (tok->purged_p);
1023   lexer->next_token = tok;
1024 }
1025
1026 /* Permanently remove all tokens after TOK, up to, but not
1027    including, the token that will be returned next by
1028    cp_lexer_peek_token.  */
1029
1030 static void
1031 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1032 {
1033   cp_token *peek = lexer->next_token;
1034
1035   if (peek == &eof_token)
1036     peek = lexer->last_token;
1037
1038   gcc_assert (tok < peek);
1039
1040   for ( tok += 1; tok != peek; tok += 1)
1041     {
1042       tok->purged_p = true;
1043       tok->location = UNKNOWN_LOCATION;
1044       tok->u.value = NULL_TREE;
1045       tok->keyword = RID_MAX;
1046     }
1047 }
1048
1049 /* Begin saving tokens.  All tokens consumed after this point will be
1050    preserved.  */
1051
1052 static void
1053 cp_lexer_save_tokens (cp_lexer* lexer)
1054 {
1055   /* Provide debugging output.  */
1056   if (cp_lexer_debugging_p (lexer))
1057     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1058
1059   lexer->saved_tokens.safe_push (lexer->next_token);
1060 }
1061
1062 /* Commit to the portion of the token stream most recently saved.  */
1063
1064 static void
1065 cp_lexer_commit_tokens (cp_lexer* lexer)
1066 {
1067   /* Provide debugging output.  */
1068   if (cp_lexer_debugging_p (lexer))
1069     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1070
1071   lexer->saved_tokens.pop ();
1072 }
1073
1074 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1075    to the token stream.  Stop saving tokens.  */
1076
1077 static void
1078 cp_lexer_rollback_tokens (cp_lexer* lexer)
1079 {
1080   /* Provide debugging output.  */
1081   if (cp_lexer_debugging_p (lexer))
1082     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1083
1084   lexer->next_token = lexer->saved_tokens.pop ();
1085 }
1086
1087 /* Print a representation of the TOKEN on the STREAM.  */
1088
1089 static void
1090 cp_lexer_print_token (FILE * stream, cp_token *token)
1091 {
1092   /* We don't use cpp_type2name here because the parser defines
1093      a few tokens of its own.  */
1094   static const char *const token_names[] = {
1095     /* cpplib-defined token types */
1096 #define OP(e, s) #e,
1097 #define TK(e, s) #e,
1098     TTYPE_TABLE
1099 #undef OP
1100 #undef TK
1101     /* C++ parser token types - see "Manifest constants", above.  */
1102     "KEYWORD",
1103     "TEMPLATE_ID",
1104     "NESTED_NAME_SPECIFIER",
1105   };
1106
1107   /* For some tokens, print the associated data.  */
1108   switch (token->type)
1109     {
1110     case CPP_KEYWORD:
1111       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1112          For example, `struct' is mapped to an INTEGER_CST.  */
1113       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1114         break;
1115       /* else fall through */
1116     case CPP_NAME:
1117       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1118       break;
1119
1120     case CPP_STRING:
1121     case CPP_STRING16:
1122     case CPP_STRING32:
1123     case CPP_WSTRING:
1124     case CPP_UTF8STRING:
1125       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1126       break;
1127
1128     case CPP_NUMBER:
1129       print_generic_expr (stream, token->u.value, 0);
1130       break;
1131
1132     default:
1133       /* If we have a name for the token, print it out.  Otherwise, we
1134          simply give the numeric code.  */
1135       if (token->type < ARRAY_SIZE(token_names))
1136         fputs (token_names[token->type], stream);
1137       else
1138         fprintf (stream, "[%d]", token->type);
1139       break;
1140     }
1141 }
1142
1143 /* Start emitting debugging information.  */
1144
1145 static void
1146 cp_lexer_start_debugging (cp_lexer* lexer)
1147 {
1148   lexer->debugging_p = true;
1149   cp_lexer_debug_stream = stderr;
1150 }
1151
1152 /* Stop emitting debugging information.  */
1153
1154 static void
1155 cp_lexer_stop_debugging (cp_lexer* lexer)
1156 {
1157   lexer->debugging_p = false;
1158   cp_lexer_debug_stream = NULL;
1159 }
1160
1161 /* Create a new cp_token_cache, representing a range of tokens.  */
1162
1163 static cp_token_cache *
1164 cp_token_cache_new (cp_token *first, cp_token *last)
1165 {
1166   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1167   cache->first = first;
1168   cache->last = last;
1169   return cache;
1170 }
1171
1172 \f
1173 /* Decl-specifiers.  */
1174
1175 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1176
1177 static void
1178 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1179 {
1180   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1181 }
1182
1183 /* Declarators.  */
1184
1185 /* Nothing other than the parser should be creating declarators;
1186    declarators are a semi-syntactic representation of C++ entities.
1187    Other parts of the front end that need to create entities (like
1188    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1189
1190 static cp_declarator *make_call_declarator
1191   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1192 static cp_declarator *make_array_declarator
1193   (cp_declarator *, tree);
1194 static cp_declarator *make_pointer_declarator
1195   (cp_cv_quals, cp_declarator *, tree);
1196 static cp_declarator *make_reference_declarator
1197   (cp_cv_quals, cp_declarator *, bool, tree);
1198 static cp_parameter_declarator *make_parameter_declarator
1199   (cp_decl_specifier_seq *, cp_declarator *, tree);
1200 static cp_declarator *make_ptrmem_declarator
1201   (cp_cv_quals, tree, cp_declarator *, tree);
1202
1203 /* An erroneous declarator.  */
1204 static cp_declarator *cp_error_declarator;
1205
1206 /* The obstack on which declarators and related data structures are
1207    allocated.  */
1208 static struct obstack declarator_obstack;
1209
1210 /* Alloc BYTES from the declarator memory pool.  */
1211
1212 static inline void *
1213 alloc_declarator (size_t bytes)
1214 {
1215   return obstack_alloc (&declarator_obstack, bytes);
1216 }
1217
1218 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1219    common to all declarators.  */
1220
1221 static cp_declarator *
1222 make_declarator (cp_declarator_kind kind)
1223 {
1224   cp_declarator *declarator;
1225
1226   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1227   declarator->kind = kind;
1228   declarator->attributes = NULL_TREE;
1229   declarator->std_attributes = NULL_TREE;
1230   declarator->declarator = NULL;
1231   declarator->parameter_pack_p = false;
1232   declarator->id_loc = UNKNOWN_LOCATION;
1233
1234   return declarator;
1235 }
1236
1237 /* Make a declarator for a generalized identifier.  If
1238    QUALIFYING_SCOPE is non-NULL, the identifier is
1239    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1240    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1241    is, if any.   */
1242
1243 static cp_declarator *
1244 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1245                     special_function_kind sfk)
1246 {
1247   cp_declarator *declarator;
1248
1249   /* It is valid to write:
1250
1251        class C { void f(); };
1252        typedef C D;
1253        void D::f();
1254
1255      The standard is not clear about whether `typedef const C D' is
1256      legal; as of 2002-09-15 the committee is considering that
1257      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1258      well.  */
1259   if (qualifying_scope && TYPE_P (qualifying_scope))
1260     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1261
1262   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1263               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1264               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1265
1266   declarator = make_declarator (cdk_id);
1267   declarator->u.id.qualifying_scope = qualifying_scope;
1268   declarator->u.id.unqualified_name = unqualified_name;
1269   declarator->u.id.sfk = sfk;
1270   
1271   return declarator;
1272 }
1273
1274 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1275    of modifiers such as const or volatile to apply to the pointer
1276    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1277    appertain to the pointer or reference.  */
1278
1279 cp_declarator *
1280 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1281                          tree attributes)
1282 {
1283   cp_declarator *declarator;
1284
1285   declarator = make_declarator (cdk_pointer);
1286   declarator->declarator = target;
1287   declarator->u.pointer.qualifiers = cv_qualifiers;
1288   declarator->u.pointer.class_type = NULL_TREE;
1289   if (target)
1290     {
1291       declarator->id_loc = target->id_loc;
1292       declarator->parameter_pack_p = target->parameter_pack_p;
1293       target->parameter_pack_p = false;
1294     }
1295   else
1296     declarator->parameter_pack_p = false;
1297
1298   declarator->std_attributes = attributes;
1299
1300   return declarator;
1301 }
1302
1303 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1304    represent the attributes that appertain to the pointer or
1305    reference.  */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309                            bool rvalue_ref, tree attributes)
1310 {
1311   cp_declarator *declarator;
1312
1313   declarator = make_declarator (cdk_reference);
1314   declarator->declarator = target;
1315   declarator->u.reference.qualifiers = cv_qualifiers;
1316   declarator->u.reference.rvalue_ref = rvalue_ref;
1317   if (target)
1318     {
1319       declarator->id_loc = target->id_loc;
1320       declarator->parameter_pack_p = target->parameter_pack_p;
1321       target->parameter_pack_p = false;
1322     }
1323   else
1324     declarator->parameter_pack_p = false;
1325
1326   declarator->std_attributes = attributes;
1327
1328   return declarator;
1329 }
1330
1331 /* Like make_pointer_declarator -- but for a pointer to a non-static
1332    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1333    appertain to the pointer or reference.  */
1334
1335 cp_declarator *
1336 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1337                         cp_declarator *pointee,
1338                         tree attributes)
1339 {
1340   cp_declarator *declarator;
1341
1342   declarator = make_declarator (cdk_ptrmem);
1343   declarator->declarator = pointee;
1344   declarator->u.pointer.qualifiers = cv_qualifiers;
1345   declarator->u.pointer.class_type = class_type;
1346
1347   if (pointee)
1348     {
1349       declarator->parameter_pack_p = pointee->parameter_pack_p;
1350       pointee->parameter_pack_p = false;
1351     }
1352   else
1353     declarator->parameter_pack_p = false;
1354
1355   declarator->std_attributes = attributes;
1356
1357   return declarator;
1358 }
1359
1360 /* Make a declarator for the function given by TARGET, with the
1361    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1362    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1363    indicates what exceptions can be thrown.  */
1364
1365 cp_declarator *
1366 make_call_declarator (cp_declarator *target,
1367                       tree parms,
1368                       cp_cv_quals cv_qualifiers,
1369                       cp_virt_specifiers virt_specifiers,
1370                       cp_ref_qualifier ref_qualifier,
1371                       tree exception_specification,
1372                       tree late_return_type)
1373 {
1374   cp_declarator *declarator;
1375
1376   declarator = make_declarator (cdk_function);
1377   declarator->declarator = target;
1378   declarator->u.function.parameters = parms;
1379   declarator->u.function.qualifiers = cv_qualifiers;
1380   declarator->u.function.virt_specifiers = virt_specifiers;
1381   declarator->u.function.ref_qualifier = ref_qualifier;
1382   declarator->u.function.exception_specification = exception_specification;
1383   declarator->u.function.late_return_type = late_return_type;
1384   if (target)
1385     {
1386       declarator->id_loc = target->id_loc;
1387       declarator->parameter_pack_p = target->parameter_pack_p;
1388       target->parameter_pack_p = false;
1389     }
1390   else
1391     declarator->parameter_pack_p = false;
1392
1393   return declarator;
1394 }
1395
1396 /* Make a declarator for an array of BOUNDS elements, each of which is
1397    defined by ELEMENT.  */
1398
1399 cp_declarator *
1400 make_array_declarator (cp_declarator *element, tree bounds)
1401 {
1402   cp_declarator *declarator;
1403
1404   declarator = make_declarator (cdk_array);
1405   declarator->declarator = element;
1406   declarator->u.array.bounds = bounds;
1407   if (element)
1408     {
1409       declarator->id_loc = element->id_loc;
1410       declarator->parameter_pack_p = element->parameter_pack_p;
1411       element->parameter_pack_p = false;
1412     }
1413   else
1414     declarator->parameter_pack_p = false;
1415
1416   return declarator;
1417 }
1418
1419 /* Determine whether the declarator we've seen so far can be a
1420    parameter pack, when followed by an ellipsis.  */
1421 static bool 
1422 declarator_can_be_parameter_pack (cp_declarator *declarator)
1423 {
1424   /* Search for a declarator name, or any other declarator that goes
1425      after the point where the ellipsis could appear in a parameter
1426      pack. If we find any of these, then this declarator can not be
1427      made into a parameter pack.  */
1428   bool found = false;
1429   while (declarator && !found)
1430     {
1431       switch ((int)declarator->kind)
1432         {
1433         case cdk_id:
1434         case cdk_array:
1435           found = true;
1436           break;
1437
1438         case cdk_error:
1439           return true;
1440
1441         default:
1442           declarator = declarator->declarator;
1443           break;
1444         }
1445     }
1446
1447   return !found;
1448 }
1449
1450 cp_parameter_declarator *no_parameters;
1451
1452 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1453    DECLARATOR and DEFAULT_ARGUMENT.  */
1454
1455 cp_parameter_declarator *
1456 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1457                            cp_declarator *declarator,
1458                            tree default_argument)
1459 {
1460   cp_parameter_declarator *parameter;
1461
1462   parameter = ((cp_parameter_declarator *)
1463                alloc_declarator (sizeof (cp_parameter_declarator)));
1464   parameter->next = NULL;
1465   if (decl_specifiers)
1466     parameter->decl_specifiers = *decl_specifiers;
1467   else
1468     clear_decl_specs (&parameter->decl_specifiers);
1469   parameter->declarator = declarator;
1470   parameter->default_argument = default_argument;
1471   parameter->ellipsis_p = false;
1472
1473   return parameter;
1474 }
1475
1476 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1477
1478 static bool
1479 function_declarator_p (const cp_declarator *declarator)
1480 {
1481   while (declarator)
1482     {
1483       if (declarator->kind == cdk_function
1484           && declarator->declarator->kind == cdk_id)
1485         return true;
1486       if (declarator->kind == cdk_id
1487           || declarator->kind == cdk_error)
1488         return false;
1489       declarator = declarator->declarator;
1490     }
1491   return false;
1492 }
1493  
1494 /* The parser.  */
1495
1496 /* Overview
1497    --------
1498
1499    A cp_parser parses the token stream as specified by the C++
1500    grammar.  Its job is purely parsing, not semantic analysis.  For
1501    example, the parser breaks the token stream into declarators,
1502    expressions, statements, and other similar syntactic constructs.
1503    It does not check that the types of the expressions on either side
1504    of an assignment-statement are compatible, or that a function is
1505    not declared with a parameter of type `void'.
1506
1507    The parser invokes routines elsewhere in the compiler to perform
1508    semantic analysis and to build up the abstract syntax tree for the
1509    code processed.
1510
1511    The parser (and the template instantiation code, which is, in a
1512    way, a close relative of parsing) are the only parts of the
1513    compiler that should be calling push_scope and pop_scope, or
1514    related functions.  The parser (and template instantiation code)
1515    keeps track of what scope is presently active; everything else
1516    should simply honor that.  (The code that generates static
1517    initializers may also need to set the scope, in order to check
1518    access control correctly when emitting the initializers.)
1519
1520    Methodology
1521    -----------
1522
1523    The parser is of the standard recursive-descent variety.  Upcoming
1524    tokens in the token stream are examined in order to determine which
1525    production to use when parsing a non-terminal.  Some C++ constructs
1526    require arbitrary look ahead to disambiguate.  For example, it is
1527    impossible, in the general case, to tell whether a statement is an
1528    expression or declaration without scanning the entire statement.
1529    Therefore, the parser is capable of "parsing tentatively."  When the
1530    parser is not sure what construct comes next, it enters this mode.
1531    Then, while we attempt to parse the construct, the parser queues up
1532    error messages, rather than issuing them immediately, and saves the
1533    tokens it consumes.  If the construct is parsed successfully, the
1534    parser "commits", i.e., it issues any queued error messages and
1535    the tokens that were being preserved are permanently discarded.
1536    If, however, the construct is not parsed successfully, the parser
1537    rolls back its state completely so that it can resume parsing using
1538    a different alternative.
1539
1540    Future Improvements
1541    -------------------
1542
1543    The performance of the parser could probably be improved substantially.
1544    We could often eliminate the need to parse tentatively by looking ahead
1545    a little bit.  In some places, this approach might not entirely eliminate
1546    the need to parse tentatively, but it might still speed up the average
1547    case.  */
1548
1549 /* Flags that are passed to some parsing functions.  These values can
1550    be bitwise-ored together.  */
1551
1552 enum
1553 {
1554   /* No flags.  */
1555   CP_PARSER_FLAGS_NONE = 0x0,
1556   /* The construct is optional.  If it is not present, then no error
1557      should be issued.  */
1558   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1559   /* When parsing a type-specifier, treat user-defined type-names
1560      as non-type identifiers.  */
1561   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1562   /* When parsing a type-specifier, do not try to parse a class-specifier
1563      or enum-specifier.  */
1564   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1565   /* When parsing a decl-specifier-seq, only allow type-specifier or
1566      constexpr.  */
1567   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1568 };
1569
1570 /* This type is used for parameters and variables which hold
1571    combinations of the above flags.  */
1572 typedef int cp_parser_flags;
1573
1574 /* The different kinds of declarators we want to parse.  */
1575
1576 typedef enum cp_parser_declarator_kind
1577 {
1578   /* We want an abstract declarator.  */
1579   CP_PARSER_DECLARATOR_ABSTRACT,
1580   /* We want a named declarator.  */
1581   CP_PARSER_DECLARATOR_NAMED,
1582   /* We don't mind, but the name must be an unqualified-id.  */
1583   CP_PARSER_DECLARATOR_EITHER
1584 } cp_parser_declarator_kind;
1585
1586 /* The precedence values used to parse binary expressions.  The minimum value
1587    of PREC must be 1, because zero is reserved to quickly discriminate
1588    binary operators from other tokens.  */
1589
1590 enum cp_parser_prec
1591 {
1592   PREC_NOT_OPERATOR,
1593   PREC_LOGICAL_OR_EXPRESSION,
1594   PREC_LOGICAL_AND_EXPRESSION,
1595   PREC_INCLUSIVE_OR_EXPRESSION,
1596   PREC_EXCLUSIVE_OR_EXPRESSION,
1597   PREC_AND_EXPRESSION,
1598   PREC_EQUALITY_EXPRESSION,
1599   PREC_RELATIONAL_EXPRESSION,
1600   PREC_SHIFT_EXPRESSION,
1601   PREC_ADDITIVE_EXPRESSION,
1602   PREC_MULTIPLICATIVE_EXPRESSION,
1603   PREC_PM_EXPRESSION,
1604   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1605 };
1606
1607 /* A mapping from a token type to a corresponding tree node type, with a
1608    precedence value.  */
1609
1610 typedef struct cp_parser_binary_operations_map_node
1611 {
1612   /* The token type.  */
1613   enum cpp_ttype token_type;
1614   /* The corresponding tree code.  */
1615   enum tree_code tree_type;
1616   /* The precedence of this operator.  */
1617   enum cp_parser_prec prec;
1618 } cp_parser_binary_operations_map_node;
1619
1620 typedef struct cp_parser_expression_stack_entry
1621 {
1622   /* Left hand side of the binary operation we are currently
1623      parsing.  */
1624   tree lhs;
1625   /* Original tree code for left hand side, if it was a binary
1626      expression itself (used for -Wparentheses).  */
1627   enum tree_code lhs_type;
1628   /* Tree code for the binary operation we are parsing.  */
1629   enum tree_code tree_type;
1630   /* Precedence of the binary operation we are parsing.  */
1631   enum cp_parser_prec prec;
1632   /* Location of the binary operation we are parsing.  */
1633   location_t loc;
1634 } cp_parser_expression_stack_entry;
1635
1636 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1637    entries because precedence levels on the stack are monotonically
1638    increasing.  */
1639 typedef struct cp_parser_expression_stack_entry
1640   cp_parser_expression_stack[NUM_PREC_VALUES];
1641
1642 /* Prototypes.  */
1643
1644 /* Constructors and destructors.  */
1645
1646 static cp_parser_context *cp_parser_context_new
1647   (cp_parser_context *);
1648
1649 /* Class variables.  */
1650
1651 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1652
1653 /* The operator-precedence table used by cp_parser_binary_expression.
1654    Transformed into an associative array (binops_by_token) by
1655    cp_parser_new.  */
1656
1657 static const cp_parser_binary_operations_map_node binops[] = {
1658   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1659   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1660
1661   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1662   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1663   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1664
1665   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1666   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1667
1668   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1669   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1670
1671   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1672   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1673   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1674   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1675
1676   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1677   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1678
1679   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1680
1681   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1682
1683   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1684
1685   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1686
1687   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1688 };
1689
1690 /* The same as binops, but initialized by cp_parser_new so that
1691    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1692    for speed.  */
1693 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1694
1695 /* Constructors and destructors.  */
1696
1697 /* Construct a new context.  The context below this one on the stack
1698    is given by NEXT.  */
1699
1700 static cp_parser_context *
1701 cp_parser_context_new (cp_parser_context* next)
1702 {
1703   cp_parser_context *context;
1704
1705   /* Allocate the storage.  */
1706   if (cp_parser_context_free_list != NULL)
1707     {
1708       /* Pull the first entry from the free list.  */
1709       context = cp_parser_context_free_list;
1710       cp_parser_context_free_list = context->next;
1711       memset (context, 0, sizeof (*context));
1712     }
1713   else
1714     context = ggc_alloc_cleared_cp_parser_context ();
1715
1716   /* No errors have occurred yet in this context.  */
1717   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1718   /* If this is not the bottommost context, copy information that we
1719      need from the previous context.  */
1720   if (next)
1721     {
1722       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1723          expression, then we are parsing one in this context, too.  */
1724       context->object_type = next->object_type;
1725       /* Thread the stack.  */
1726       context->next = next;
1727     }
1728
1729   return context;
1730 }
1731
1732 /* Managing the unparsed function queues.  */
1733
1734 #define unparsed_funs_with_default_args \
1735   parser->unparsed_queues->last ().funs_with_default_args
1736 #define unparsed_funs_with_definitions \
1737   parser->unparsed_queues->last ().funs_with_definitions
1738 #define unparsed_nsdmis \
1739   parser->unparsed_queues->last ().nsdmis
1740
1741 static void
1742 push_unparsed_function_queues (cp_parser *parser)
1743 {
1744   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1745   vec_safe_push (parser->unparsed_queues, e);
1746 }
1747
1748 static void
1749 pop_unparsed_function_queues (cp_parser *parser)
1750 {
1751   release_tree_vector (unparsed_funs_with_definitions);
1752   parser->unparsed_queues->pop ();
1753 }
1754
1755 /* Prototypes.  */
1756
1757 /* Constructors and destructors.  */
1758
1759 static cp_parser *cp_parser_new
1760   (void);
1761
1762 /* Routines to parse various constructs.
1763
1764    Those that return `tree' will return the error_mark_node (rather
1765    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1766    Sometimes, they will return an ordinary node if error-recovery was
1767    attempted, even though a parse error occurred.  So, to check
1768    whether or not a parse error occurred, you should always use
1769    cp_parser_error_occurred.  If the construct is optional (indicated
1770    either by an `_opt' in the name of the function that does the
1771    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1772    the construct is not present.  */
1773
1774 /* Lexical conventions [gram.lex]  */
1775
1776 static tree cp_parser_identifier
1777   (cp_parser *);
1778 static tree cp_parser_string_literal
1779   (cp_parser *, bool, bool);
1780 static tree cp_parser_userdef_char_literal
1781   (cp_parser *);
1782 static tree cp_parser_userdef_string_literal
1783   (cp_token *);
1784 static tree cp_parser_userdef_numeric_literal
1785   (cp_parser *);
1786
1787 /* Basic concepts [gram.basic]  */
1788
1789 static bool cp_parser_translation_unit
1790   (cp_parser *);
1791
1792 /* Expressions [gram.expr]  */
1793
1794 static tree cp_parser_primary_expression
1795   (cp_parser *, bool, bool, bool, cp_id_kind *);
1796 static tree cp_parser_id_expression
1797   (cp_parser *, bool, bool, bool *, bool, bool);
1798 static tree cp_parser_unqualified_id
1799   (cp_parser *, bool, bool, bool, bool);
1800 static tree cp_parser_nested_name_specifier_opt
1801   (cp_parser *, bool, bool, bool, bool);
1802 static tree cp_parser_nested_name_specifier
1803   (cp_parser *, bool, bool, bool, bool);
1804 static tree cp_parser_qualifying_entity
1805   (cp_parser *, bool, bool, bool, bool, bool);
1806 static tree cp_parser_postfix_expression
1807   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1808 static tree cp_parser_postfix_open_square_expression
1809   (cp_parser *, tree, bool, bool);
1810 static tree cp_parser_postfix_dot_deref_expression
1811   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1812 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1813   (cp_parser *, int, bool, bool, bool *);
1814 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1815 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1816 static void cp_parser_pseudo_destructor_name
1817   (cp_parser *, tree *, tree *);
1818 static tree cp_parser_unary_expression
1819   (cp_parser *, bool, bool, cp_id_kind *);
1820 static enum tree_code cp_parser_unary_operator
1821   (cp_token *);
1822 static tree cp_parser_new_expression
1823   (cp_parser *);
1824 static vec<tree, va_gc> *cp_parser_new_placement
1825   (cp_parser *);
1826 static tree cp_parser_new_type_id
1827   (cp_parser *, tree *);
1828 static cp_declarator *cp_parser_new_declarator_opt
1829   (cp_parser *);
1830 static cp_declarator *cp_parser_direct_new_declarator
1831   (cp_parser *);
1832 static vec<tree, va_gc> *cp_parser_new_initializer
1833   (cp_parser *);
1834 static tree cp_parser_delete_expression
1835   (cp_parser *);
1836 static tree cp_parser_cast_expression
1837   (cp_parser *, bool, bool, bool, cp_id_kind *);
1838 static tree cp_parser_binary_expression
1839   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1840 static tree cp_parser_question_colon_clause
1841   (cp_parser *, tree);
1842 static tree cp_parser_assignment_expression
1843   (cp_parser *, bool, cp_id_kind *);
1844 static enum tree_code cp_parser_assignment_operator_opt
1845   (cp_parser *);
1846 static tree cp_parser_expression
1847   (cp_parser *, bool, cp_id_kind *);
1848 static tree cp_parser_expression
1849   (cp_parser *, bool, bool, cp_id_kind *);
1850 static tree cp_parser_constant_expression
1851   (cp_parser *, bool, bool *);
1852 static tree cp_parser_builtin_offsetof
1853   (cp_parser *);
1854 static tree cp_parser_lambda_expression
1855   (cp_parser *);
1856 static void cp_parser_lambda_introducer
1857   (cp_parser *, tree);
1858 static bool cp_parser_lambda_declarator_opt
1859   (cp_parser *, tree);
1860 static void cp_parser_lambda_body
1861   (cp_parser *, tree);
1862
1863 /* Statements [gram.stmt.stmt]  */
1864
1865 static void cp_parser_statement
1866   (cp_parser *, tree, bool, bool *);
1867 static void cp_parser_label_for_labeled_statement
1868 (cp_parser *, tree);
1869 static tree cp_parser_expression_statement
1870   (cp_parser *, tree);
1871 static tree cp_parser_compound_statement
1872   (cp_parser *, tree, bool, bool);
1873 static void cp_parser_statement_seq_opt
1874   (cp_parser *, tree);
1875 static tree cp_parser_selection_statement
1876   (cp_parser *, bool *);
1877 static tree cp_parser_condition
1878   (cp_parser *);
1879 static tree cp_parser_iteration_statement
1880   (cp_parser *);
1881 static bool cp_parser_for_init_statement
1882   (cp_parser *, tree *decl);
1883 static tree cp_parser_for
1884   (cp_parser *);
1885 static tree cp_parser_c_for
1886   (cp_parser *, tree, tree);
1887 static tree cp_parser_range_for
1888   (cp_parser *, tree, tree, tree);
1889 static void do_range_for_auto_deduction
1890   (tree, tree);
1891 static tree cp_parser_perform_range_for_lookup
1892   (tree, tree *, tree *);
1893 static tree cp_parser_range_for_member_function
1894   (tree, tree);
1895 static tree cp_parser_jump_statement
1896   (cp_parser *);
1897 static void cp_parser_declaration_statement
1898   (cp_parser *);
1899
1900 static tree cp_parser_implicitly_scoped_statement
1901   (cp_parser *, bool *);
1902 static void cp_parser_already_scoped_statement
1903   (cp_parser *);
1904
1905 /* Declarations [gram.dcl.dcl] */
1906
1907 static void cp_parser_declaration_seq_opt
1908   (cp_parser *);
1909 static void cp_parser_declaration
1910   (cp_parser *);
1911 static void cp_parser_block_declaration
1912   (cp_parser *, bool);
1913 static void cp_parser_simple_declaration
1914   (cp_parser *, bool, tree *);
1915 static void cp_parser_decl_specifier_seq
1916   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1917 static tree cp_parser_storage_class_specifier_opt
1918   (cp_parser *);
1919 static tree cp_parser_function_specifier_opt
1920   (cp_parser *, cp_decl_specifier_seq *);
1921 static tree cp_parser_type_specifier
1922   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1923    int *, bool *);
1924 static tree cp_parser_simple_type_specifier
1925   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1926 static tree cp_parser_type_name
1927   (cp_parser *);
1928 static tree cp_parser_nonclass_name 
1929   (cp_parser* parser);
1930 static tree cp_parser_elaborated_type_specifier
1931   (cp_parser *, bool, bool);
1932 static tree cp_parser_enum_specifier
1933   (cp_parser *);
1934 static void cp_parser_enumerator_list
1935   (cp_parser *, tree);
1936 static void cp_parser_enumerator_definition
1937   (cp_parser *, tree);
1938 static tree cp_parser_namespace_name
1939   (cp_parser *);
1940 static void cp_parser_namespace_definition
1941   (cp_parser *);
1942 static void cp_parser_namespace_body
1943   (cp_parser *);
1944 static tree cp_parser_qualified_namespace_specifier
1945   (cp_parser *);
1946 static void cp_parser_namespace_alias_definition
1947   (cp_parser *);
1948 static bool cp_parser_using_declaration
1949   (cp_parser *, bool);
1950 static void cp_parser_using_directive
1951   (cp_parser *);
1952 static tree cp_parser_alias_declaration
1953   (cp_parser *);
1954 static void cp_parser_asm_definition
1955   (cp_parser *);
1956 static void cp_parser_linkage_specification
1957   (cp_parser *);
1958 static void cp_parser_static_assert
1959   (cp_parser *, bool);
1960 static tree cp_parser_decltype
1961   (cp_parser *);
1962
1963 /* Declarators [gram.dcl.decl] */
1964
1965 static tree cp_parser_init_declarator
1966   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
1967 static cp_declarator *cp_parser_declarator
1968   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1969 static cp_declarator *cp_parser_direct_declarator
1970   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1971 static enum tree_code cp_parser_ptr_operator
1972   (cp_parser *, tree *, cp_cv_quals *, tree *);
1973 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1974   (cp_parser *);
1975 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1976   (cp_parser *);
1977 static cp_ref_qualifier cp_parser_ref_qualifier_seq_opt
1978   (cp_parser *);
1979 static tree cp_parser_late_return_type_opt
1980   (cp_parser *, cp_cv_quals);
1981 static tree cp_parser_declarator_id
1982   (cp_parser *, bool);
1983 static tree cp_parser_type_id
1984   (cp_parser *);
1985 static tree cp_parser_template_type_arg
1986   (cp_parser *);
1987 static tree cp_parser_trailing_type_id (cp_parser *);
1988 static tree cp_parser_type_id_1
1989   (cp_parser *, bool, bool);
1990 static void cp_parser_type_specifier_seq
1991   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1992 static tree cp_parser_parameter_declaration_clause
1993   (cp_parser *);
1994 static tree cp_parser_parameter_declaration_list
1995   (cp_parser *, bool *);
1996 static cp_parameter_declarator *cp_parser_parameter_declaration
1997   (cp_parser *, bool, bool *);
1998 static tree cp_parser_default_argument 
1999   (cp_parser *, bool);
2000 static void cp_parser_function_body
2001   (cp_parser *, bool);
2002 static tree cp_parser_initializer
2003   (cp_parser *, bool *, bool *);
2004 static tree cp_parser_initializer_clause
2005   (cp_parser *, bool *);
2006 static tree cp_parser_braced_list
2007   (cp_parser*, bool*);
2008 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2009   (cp_parser *, bool *);
2010
2011 static bool cp_parser_ctor_initializer_opt_and_function_body
2012   (cp_parser *, bool);
2013
2014 /* Classes [gram.class] */
2015
2016 static tree cp_parser_class_name
2017   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2018 static tree cp_parser_class_specifier
2019   (cp_parser *);
2020 static tree cp_parser_class_head
2021   (cp_parser *, bool *);
2022 static enum tag_types cp_parser_class_key
2023   (cp_parser *);
2024 static void cp_parser_member_specification_opt
2025   (cp_parser *);
2026 static void cp_parser_member_declaration
2027   (cp_parser *);
2028 static tree cp_parser_pure_specifier
2029   (cp_parser *);
2030 static tree cp_parser_constant_initializer
2031   (cp_parser *);
2032
2033 /* Derived classes [gram.class.derived] */
2034
2035 static tree cp_parser_base_clause
2036   (cp_parser *);
2037 static tree cp_parser_base_specifier
2038   (cp_parser *);
2039
2040 /* Special member functions [gram.special] */
2041
2042 static tree cp_parser_conversion_function_id
2043   (cp_parser *);
2044 static tree cp_parser_conversion_type_id
2045   (cp_parser *);
2046 static cp_declarator *cp_parser_conversion_declarator_opt
2047   (cp_parser *);
2048 static bool cp_parser_ctor_initializer_opt
2049   (cp_parser *);
2050 static void cp_parser_mem_initializer_list
2051   (cp_parser *);
2052 static tree cp_parser_mem_initializer
2053   (cp_parser *);
2054 static tree cp_parser_mem_initializer_id
2055   (cp_parser *);
2056
2057 /* Overloading [gram.over] */
2058
2059 static tree cp_parser_operator_function_id
2060   (cp_parser *);
2061 static tree cp_parser_operator
2062   (cp_parser *);
2063
2064 /* Templates [gram.temp] */
2065
2066 static void cp_parser_template_declaration
2067   (cp_parser *, bool);
2068 static tree cp_parser_template_parameter_list
2069   (cp_parser *);
2070 static tree cp_parser_template_parameter
2071   (cp_parser *, bool *, bool *);
2072 static tree cp_parser_type_parameter
2073   (cp_parser *, bool *);
2074 static tree cp_parser_template_id
2075   (cp_parser *, bool, bool, enum tag_types, bool);
2076 static tree cp_parser_template_name
2077   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2078 static tree cp_parser_template_argument_list
2079   (cp_parser *);
2080 static tree cp_parser_template_argument
2081   (cp_parser *);
2082 static void cp_parser_explicit_instantiation
2083   (cp_parser *);
2084 static void cp_parser_explicit_specialization
2085   (cp_parser *);
2086
2087 /* Exception handling [gram.exception] */
2088
2089 static tree cp_parser_try_block
2090   (cp_parser *);
2091 static bool cp_parser_function_try_block
2092   (cp_parser *);
2093 static void cp_parser_handler_seq
2094   (cp_parser *);
2095 static void cp_parser_handler
2096   (cp_parser *);
2097 static tree cp_parser_exception_declaration
2098   (cp_parser *);
2099 static tree cp_parser_throw_expression
2100   (cp_parser *);
2101 static tree cp_parser_exception_specification_opt
2102   (cp_parser *);
2103 static tree cp_parser_type_id_list
2104   (cp_parser *);
2105
2106 /* GNU Extensions */
2107
2108 static tree cp_parser_asm_specification_opt
2109   (cp_parser *);
2110 static tree cp_parser_asm_operand_list
2111   (cp_parser *);
2112 static tree cp_parser_asm_clobber_list
2113   (cp_parser *);
2114 static tree cp_parser_asm_label_list
2115   (cp_parser *);
2116 static bool cp_next_tokens_can_be_attribute_p
2117   (cp_parser *);
2118 static bool cp_next_tokens_can_be_gnu_attribute_p
2119   (cp_parser *);
2120 static bool cp_next_tokens_can_be_std_attribute_p
2121   (cp_parser *);
2122 static bool cp_nth_tokens_can_be_std_attribute_p
2123   (cp_parser *, size_t);
2124 static bool cp_nth_tokens_can_be_gnu_attribute_p
2125   (cp_parser *, size_t);
2126 static bool cp_nth_tokens_can_be_attribute_p
2127   (cp_parser *, size_t);
2128 static tree cp_parser_attributes_opt
2129   (cp_parser *);
2130 static tree cp_parser_gnu_attributes_opt
2131   (cp_parser *);
2132 static tree cp_parser_gnu_attribute_list
2133   (cp_parser *);
2134 static tree cp_parser_std_attribute
2135   (cp_parser *);
2136 static tree cp_parser_std_attribute_spec
2137   (cp_parser *);
2138 static tree cp_parser_std_attribute_spec_seq
2139   (cp_parser *);
2140 static bool cp_parser_extension_opt
2141   (cp_parser *, int *);
2142 static void cp_parser_label_declaration
2143   (cp_parser *);
2144
2145 /* Transactional Memory Extensions */
2146
2147 static tree cp_parser_transaction
2148   (cp_parser *, enum rid);
2149 static tree cp_parser_transaction_expression
2150   (cp_parser *, enum rid);
2151 static bool cp_parser_function_transaction
2152   (cp_parser *, enum rid);
2153 static tree cp_parser_transaction_cancel
2154   (cp_parser *);
2155
2156 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2157 static bool cp_parser_pragma
2158   (cp_parser *, enum pragma_context);
2159
2160 /* Objective-C++ Productions */
2161
2162 static tree cp_parser_objc_message_receiver
2163   (cp_parser *);
2164 static tree cp_parser_objc_message_args
2165   (cp_parser *);
2166 static tree cp_parser_objc_message_expression
2167   (cp_parser *);
2168 static tree cp_parser_objc_encode_expression
2169   (cp_parser *);
2170 static tree cp_parser_objc_defs_expression
2171   (cp_parser *);
2172 static tree cp_parser_objc_protocol_expression
2173   (cp_parser *);
2174 static tree cp_parser_objc_selector_expression
2175   (cp_parser *);
2176 static tree cp_parser_objc_expression
2177   (cp_parser *);
2178 static bool cp_parser_objc_selector_p
2179   (enum cpp_ttype);
2180 static tree cp_parser_objc_selector
2181   (cp_parser *);
2182 static tree cp_parser_objc_protocol_refs_opt
2183   (cp_parser *);
2184 static void cp_parser_objc_declaration
2185   (cp_parser *, tree);
2186 static tree cp_parser_objc_statement
2187   (cp_parser *);
2188 static bool cp_parser_objc_valid_prefix_attributes
2189   (cp_parser *, tree *);
2190 static void cp_parser_objc_at_property_declaration 
2191   (cp_parser *) ;
2192 static void cp_parser_objc_at_synthesize_declaration 
2193   (cp_parser *) ;
2194 static void cp_parser_objc_at_dynamic_declaration
2195   (cp_parser *) ;
2196 static tree cp_parser_objc_struct_declaration
2197   (cp_parser *) ;
2198
2199 /* Utility Routines */
2200
2201 static tree cp_parser_lookup_name
2202   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2203 static tree cp_parser_lookup_name_simple
2204   (cp_parser *, tree, location_t);
2205 static tree cp_parser_maybe_treat_template_as_class
2206   (tree, bool);
2207 static bool cp_parser_check_declarator_template_parameters
2208   (cp_parser *, cp_declarator *, location_t);
2209 static bool cp_parser_check_template_parameters
2210   (cp_parser *, unsigned, location_t, cp_declarator *);
2211 static tree cp_parser_simple_cast_expression
2212   (cp_parser *);
2213 static tree cp_parser_global_scope_opt
2214   (cp_parser *, bool);
2215 static bool cp_parser_constructor_declarator_p
2216   (cp_parser *, bool);
2217 static tree cp_parser_function_definition_from_specifiers_and_declarator
2218   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2219 static tree cp_parser_function_definition_after_declarator
2220   (cp_parser *, bool);
2221 static void cp_parser_template_declaration_after_export
2222   (cp_parser *, bool);
2223 static void cp_parser_perform_template_parameter_access_checks
2224   (vec<deferred_access_check, va_gc> *);
2225 static tree cp_parser_single_declaration
2226   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2227 static tree cp_parser_functional_cast
2228   (cp_parser *, tree);
2229 static tree cp_parser_save_member_function_body
2230   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2231 static tree cp_parser_save_nsdmi
2232   (cp_parser *);
2233 static tree cp_parser_enclosed_template_argument_list
2234   (cp_parser *);
2235 static void cp_parser_save_default_args
2236   (cp_parser *, tree);
2237 static void cp_parser_late_parsing_for_member
2238   (cp_parser *, tree);
2239 static tree cp_parser_late_parse_one_default_arg
2240   (cp_parser *, tree, tree, tree);
2241 static void cp_parser_late_parsing_nsdmi
2242   (cp_parser *, tree);
2243 static void cp_parser_late_parsing_default_args
2244   (cp_parser *, tree);
2245 static tree cp_parser_sizeof_operand
2246   (cp_parser *, enum rid);
2247 static tree cp_parser_trait_expr
2248   (cp_parser *, enum rid);
2249 static bool cp_parser_declares_only_class_p
2250   (cp_parser *);
2251 static void cp_parser_set_storage_class
2252   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2253 static void cp_parser_set_decl_spec_type
2254   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2255 static void set_and_check_decl_spec_loc
2256   (cp_decl_specifier_seq *decl_specs,
2257    cp_decl_spec ds, cp_token *);
2258 static bool cp_parser_friend_p
2259   (const cp_decl_specifier_seq *);
2260 static void cp_parser_required_error
2261   (cp_parser *, required_token, bool);
2262 static cp_token *cp_parser_require
2263   (cp_parser *, enum cpp_ttype, required_token);
2264 static cp_token *cp_parser_require_keyword
2265   (cp_parser *, enum rid, required_token);
2266 static bool cp_parser_token_starts_function_definition_p
2267   (cp_token *);
2268 static bool cp_parser_next_token_starts_class_definition_p
2269   (cp_parser *);
2270 static bool cp_parser_next_token_ends_template_argument_p
2271   (cp_parser *);
2272 static bool cp_parser_nth_token_starts_template_argument_list_p
2273   (cp_parser *, size_t);
2274 static enum tag_types cp_parser_token_is_class_key
2275   (cp_token *);
2276 static void cp_parser_check_class_key
2277   (enum tag_types, tree type);
2278 static void cp_parser_check_access_in_redeclaration
2279   (tree type, location_t location);
2280 static bool cp_parser_optional_template_keyword
2281   (cp_parser *);
2282 static void cp_parser_pre_parsed_nested_name_specifier
2283   (cp_parser *);
2284 static bool cp_parser_cache_group
2285   (cp_parser *, enum cpp_ttype, unsigned);
2286 static tree cp_parser_cache_defarg
2287   (cp_parser *parser, bool nsdmi);
2288 static void cp_parser_parse_tentatively
2289   (cp_parser *);
2290 static void cp_parser_commit_to_tentative_parse
2291   (cp_parser *);
2292 static void cp_parser_abort_tentative_parse
2293   (cp_parser *);
2294 static bool cp_parser_parse_definitely
2295   (cp_parser *);
2296 static inline bool cp_parser_parsing_tentatively
2297   (cp_parser *);
2298 static bool cp_parser_uncommitted_to_tentative_parse_p
2299   (cp_parser *);
2300 static void cp_parser_error
2301   (cp_parser *, const char *);
2302 static void cp_parser_name_lookup_error
2303   (cp_parser *, tree, tree, name_lookup_error, location_t);
2304 static bool cp_parser_simulate_error
2305   (cp_parser *);
2306 static bool cp_parser_check_type_definition
2307   (cp_parser *);
2308 static void cp_parser_check_for_definition_in_return_type
2309   (cp_declarator *, tree, location_t type_location);
2310 static void cp_parser_check_for_invalid_template_id
2311   (cp_parser *, tree, enum tag_types, location_t location);
2312 static bool cp_parser_non_integral_constant_expression
2313   (cp_parser *, non_integral_constant);
2314 static void cp_parser_diagnose_invalid_type_name
2315   (cp_parser *, tree, tree, location_t);
2316 static bool cp_parser_parse_and_diagnose_invalid_type_name
2317   (cp_parser *);
2318 static int cp_parser_skip_to_closing_parenthesis
2319   (cp_parser *, bool, bool, bool);
2320 static void cp_parser_skip_to_end_of_statement
2321   (cp_parser *);
2322 static void cp_parser_consume_semicolon_at_end_of_statement
2323   (cp_parser *);
2324 static void cp_parser_skip_to_end_of_block_or_statement
2325   (cp_parser *);
2326 static bool cp_parser_skip_to_closing_brace
2327   (cp_parser *);
2328 static void cp_parser_skip_to_end_of_template_parameter_list
2329   (cp_parser *);
2330 static void cp_parser_skip_to_pragma_eol
2331   (cp_parser*, cp_token *);
2332 static bool cp_parser_error_occurred
2333   (cp_parser *);
2334 static bool cp_parser_allow_gnu_extensions_p
2335   (cp_parser *);
2336 static bool cp_parser_is_pure_string_literal
2337   (cp_token *);
2338 static bool cp_parser_is_string_literal
2339   (cp_token *);
2340 static bool cp_parser_is_keyword
2341   (cp_token *, enum rid);
2342 static tree cp_parser_make_typename_type
2343   (cp_parser *, tree, tree, location_t location);
2344 static cp_declarator * cp_parser_make_indirect_declarator
2345  (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2346
2347 /* Returns nonzero if we are parsing tentatively.  */
2348
2349 static inline bool
2350 cp_parser_parsing_tentatively (cp_parser* parser)
2351 {
2352   return parser->context->next != NULL;
2353 }
2354
2355 /* Returns nonzero if TOKEN is a string literal.  */
2356
2357 static bool
2358 cp_parser_is_pure_string_literal (cp_token* token)
2359 {
2360   return (token->type == CPP_STRING ||
2361           token->type == CPP_STRING16 ||
2362           token->type == CPP_STRING32 ||
2363           token->type == CPP_WSTRING ||
2364           token->type == CPP_UTF8STRING);
2365 }
2366
2367 /* Returns nonzero if TOKEN is a string literal
2368    of a user-defined string literal.  */
2369
2370 static bool
2371 cp_parser_is_string_literal (cp_token* token)
2372 {
2373   return (cp_parser_is_pure_string_literal (token) ||
2374           token->type == CPP_STRING_USERDEF ||
2375           token->type == CPP_STRING16_USERDEF ||
2376           token->type == CPP_STRING32_USERDEF ||
2377           token->type == CPP_WSTRING_USERDEF ||
2378           token->type == CPP_UTF8STRING_USERDEF);
2379 }
2380
2381 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2382
2383 static bool
2384 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2385 {
2386   return token->keyword == keyword;
2387 }
2388
2389 /* If not parsing tentatively, issue a diagnostic of the form
2390       FILE:LINE: MESSAGE before TOKEN
2391    where TOKEN is the next token in the input stream.  MESSAGE
2392    (specified by the caller) is usually of the form "expected
2393    OTHER-TOKEN".  */
2394
2395 static void
2396 cp_parser_error (cp_parser* parser, const char* gmsgid)
2397 {
2398   if (!cp_parser_simulate_error (parser))
2399     {
2400       cp_token *token = cp_lexer_peek_token (parser->lexer);
2401       /* This diagnostic makes more sense if it is tagged to the line
2402          of the token we just peeked at.  */
2403       cp_lexer_set_source_position_from_token (token);
2404
2405       if (token->type == CPP_PRAGMA)
2406         {
2407           error_at (token->location,
2408                     "%<#pragma%> is not allowed here");
2409           cp_parser_skip_to_pragma_eol (parser, token);
2410           return;
2411         }
2412
2413       c_parse_error (gmsgid,
2414                      /* Because c_parser_error does not understand
2415                         CPP_KEYWORD, keywords are treated like
2416                         identifiers.  */
2417                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2418                      token->u.value, token->flags);
2419     }
2420 }
2421
2422 /* Issue an error about name-lookup failing.  NAME is the
2423    IDENTIFIER_NODE DECL is the result of
2424    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2425    the thing that we hoped to find.  */
2426
2427 static void
2428 cp_parser_name_lookup_error (cp_parser* parser,
2429                              tree name,
2430                              tree decl,
2431                              name_lookup_error desired,
2432                              location_t location)
2433 {
2434   /* If name lookup completely failed, tell the user that NAME was not
2435      declared.  */
2436   if (decl == error_mark_node)
2437     {
2438       if (parser->scope && parser->scope != global_namespace)
2439         error_at (location, "%<%E::%E%> has not been declared",
2440                   parser->scope, name);
2441       else if (parser->scope == global_namespace)
2442         error_at (location, "%<::%E%> has not been declared", name);
2443       else if (parser->object_scope
2444                && !CLASS_TYPE_P (parser->object_scope))
2445         error_at (location, "request for member %qE in non-class type %qT",
2446                   name, parser->object_scope);
2447       else if (parser->object_scope)
2448         error_at (location, "%<%T::%E%> has not been declared",
2449                   parser->object_scope, name);
2450       else
2451         error_at (location, "%qE has not been declared", name);
2452     }
2453   else if (parser->scope && parser->scope != global_namespace)
2454     {
2455       switch (desired)
2456         {
2457           case NLE_TYPE:
2458             error_at (location, "%<%E::%E%> is not a type",
2459                                 parser->scope, name);
2460             break;
2461           case NLE_CXX98:
2462             error_at (location, "%<%E::%E%> is not a class or namespace",
2463                                 parser->scope, name);
2464             break;
2465           case NLE_NOT_CXX98:
2466             error_at (location,
2467                       "%<%E::%E%> is not a class, namespace, or enumeration",
2468                       parser->scope, name);
2469             break;
2470           default:
2471             gcc_unreachable ();
2472             
2473         }
2474     }
2475   else if (parser->scope == global_namespace)
2476     {
2477       switch (desired)
2478         {
2479           case NLE_TYPE:
2480             error_at (location, "%<::%E%> is not a type", name);
2481             break;
2482           case NLE_CXX98:
2483             error_at (location, "%<::%E%> is not a class or namespace", name);
2484             break;
2485           case NLE_NOT_CXX98:
2486             error_at (location,
2487                       "%<::%E%> is not a class, namespace, or enumeration",
2488                       name);
2489             break;
2490           default:
2491             gcc_unreachable ();
2492         }
2493     }
2494   else
2495     {
2496       switch (desired)
2497         {
2498           case NLE_TYPE:
2499             error_at (location, "%qE is not a type", name);
2500             break;
2501           case NLE_CXX98:
2502             error_at (location, "%qE is not a class or namespace", name);
2503             break;
2504           case NLE_NOT_CXX98:
2505             error_at (location,
2506                       "%qE is not a class, namespace, or enumeration", name);
2507             break;
2508           default:
2509             gcc_unreachable ();
2510         }
2511     }
2512 }
2513
2514 /* If we are parsing tentatively, remember that an error has occurred
2515    during this tentative parse.  Returns true if the error was
2516    simulated; false if a message should be issued by the caller.  */
2517
2518 static bool
2519 cp_parser_simulate_error (cp_parser* parser)
2520 {
2521   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2522     {
2523       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2524       return true;
2525     }
2526   return false;
2527 }
2528
2529 /* This function is called when a type is defined.  If type
2530    definitions are forbidden at this point, an error message is
2531    issued.  */
2532
2533 static bool
2534 cp_parser_check_type_definition (cp_parser* parser)
2535 {
2536   /* If types are forbidden here, issue a message.  */
2537   if (parser->type_definition_forbidden_message)
2538     {
2539       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2540          in the message need to be interpreted.  */
2541       error (parser->type_definition_forbidden_message);
2542       return false;
2543     }
2544   return true;
2545 }
2546
2547 /* This function is called when the DECLARATOR is processed.  The TYPE
2548    was a type defined in the decl-specifiers.  If it is invalid to
2549    define a type in the decl-specifiers for DECLARATOR, an error is
2550    issued. TYPE_LOCATION is the location of TYPE and is used
2551    for error reporting.  */
2552
2553 static void
2554 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2555                                                tree type, location_t type_location)
2556 {
2557   /* [dcl.fct] forbids type definitions in return types.
2558      Unfortunately, it's not easy to know whether or not we are
2559      processing a return type until after the fact.  */
2560   while (declarator
2561          && (declarator->kind == cdk_pointer
2562              || declarator->kind == cdk_reference
2563              || declarator->kind == cdk_ptrmem))
2564     declarator = declarator->declarator;
2565   if (declarator
2566       && declarator->kind == cdk_function)
2567     {
2568       error_at (type_location,
2569                 "new types may not be defined in a return type");
2570       inform (type_location, 
2571               "(perhaps a semicolon is missing after the definition of %qT)",
2572               type);
2573     }
2574 }
2575
2576 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2577    "<" in any valid C++ program.  If the next token is indeed "<",
2578    issue a message warning the user about what appears to be an
2579    invalid attempt to form a template-id. LOCATION is the location
2580    of the type-specifier (TYPE) */
2581
2582 static void
2583 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2584                                          tree type,
2585                                          enum tag_types tag_type,
2586                                          location_t location)
2587 {
2588   cp_token_position start = 0;
2589
2590   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2591     {
2592       if (TYPE_P (type))
2593         error_at (location, "%qT is not a template", type);
2594       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2595         {
2596           if (tag_type != none_type)
2597             error_at (location, "%qE is not a class template", type);
2598           else
2599             error_at (location, "%qE is not a template", type);
2600         }
2601       else
2602         error_at (location, "invalid template-id");
2603       /* Remember the location of the invalid "<".  */
2604       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2605         start = cp_lexer_token_position (parser->lexer, true);
2606       /* Consume the "<".  */
2607       cp_lexer_consume_token (parser->lexer);
2608       /* Parse the template arguments.  */
2609       cp_parser_enclosed_template_argument_list (parser);
2610       /* Permanently remove the invalid template arguments so that
2611          this error message is not issued again.  */
2612       if (start)
2613         cp_lexer_purge_tokens_after (parser->lexer, start);
2614     }
2615 }
2616
2617 /* If parsing an integral constant-expression, issue an error message
2618    about the fact that THING appeared and return true.  Otherwise,
2619    return false.  In either case, set
2620    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2621
2622 static bool
2623 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2624                                             non_integral_constant thing)
2625 {
2626   parser->non_integral_constant_expression_p = true;
2627   if (parser->integral_constant_expression_p)
2628     {
2629       if (!parser->allow_non_integral_constant_expression_p)
2630         {
2631           const char *msg = NULL;
2632           switch (thing)
2633             {
2634               case NIC_FLOAT:
2635                 error ("floating-point literal "
2636                        "cannot appear in a constant-expression");
2637                 return true;
2638               case NIC_CAST:
2639                 error ("a cast to a type other than an integral or "
2640                        "enumeration type cannot appear in a "
2641                        "constant-expression");
2642                 return true;
2643               case NIC_TYPEID:
2644                 error ("%<typeid%> operator "
2645                        "cannot appear in a constant-expression");
2646                 return true;
2647               case NIC_NCC:
2648                 error ("non-constant compound literals "
2649                        "cannot appear in a constant-expression");
2650                 return true;
2651               case NIC_FUNC_CALL:
2652                 error ("a function call "
2653                        "cannot appear in a constant-expression");
2654                 return true;
2655               case NIC_INC:
2656                 error ("an increment "
2657                        "cannot appear in a constant-expression");
2658                 return true;
2659               case NIC_DEC:
2660                 error ("an decrement "
2661                        "cannot appear in a constant-expression");
2662                 return true;
2663               case NIC_ARRAY_REF:
2664                 error ("an array reference "
2665                        "cannot appear in a constant-expression");
2666                 return true;
2667               case NIC_ADDR_LABEL:
2668                 error ("the address of a label "
2669                        "cannot appear in a constant-expression");
2670                 return true;
2671               case NIC_OVERLOADED:
2672                 error ("calls to overloaded operators "
2673                        "cannot appear in a constant-expression");
2674                 return true;
2675               case NIC_ASSIGNMENT:
2676                 error ("an assignment cannot appear in a constant-expression");
2677                 return true;
2678               case NIC_COMMA:
2679                 error ("a comma operator "
2680                        "cannot appear in a constant-expression");
2681                 return true;
2682               case NIC_CONSTRUCTOR:
2683                 error ("a call to a constructor "
2684                        "cannot appear in a constant-expression");
2685                 return true;
2686               case NIC_TRANSACTION:
2687                 error ("a transaction expression "
2688                        "cannot appear in a constant-expression");
2689                 return true;
2690               case NIC_THIS:
2691                 msg = "this";
2692                 break;
2693               case NIC_FUNC_NAME:
2694                 msg = "__FUNCTION__";
2695                 break;
2696               case NIC_PRETTY_FUNC:
2697                 msg = "__PRETTY_FUNCTION__";
2698                 break;
2699               case NIC_C99_FUNC:
2700                 msg = "__func__";
2701                 break;
2702               case NIC_VA_ARG:
2703                 msg = "va_arg";
2704                 break;
2705               case NIC_ARROW:
2706                 msg = "->";
2707                 break;
2708               case NIC_POINT:
2709                 msg = ".";
2710                 break;
2711               case NIC_STAR:
2712                 msg = "*";
2713                 break;
2714               case NIC_ADDR:
2715                 msg = "&";
2716                 break;
2717               case NIC_PREINCREMENT:
2718                 msg = "++";
2719                 break;
2720               case NIC_PREDECREMENT:
2721                 msg = "--";
2722                 break;
2723               case NIC_NEW:
2724                 msg = "new";
2725                 break;
2726               case NIC_DEL:
2727                 msg = "delete";
2728                 break;
2729               default:
2730                 gcc_unreachable ();
2731             }
2732           if (msg)
2733             error ("%qs cannot appear in a constant-expression", msg);
2734           return true;
2735         }
2736     }
2737   return false;
2738 }
2739
2740 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2741    qualifying scope (or NULL, if none) for ID.  This function commits
2742    to the current active tentative parse, if any.  (Otherwise, the
2743    problematic construct might be encountered again later, resulting
2744    in duplicate error messages.) LOCATION is the location of ID.  */
2745
2746 static void
2747 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2748                                       tree scope, tree id,
2749                                       location_t location)
2750 {
2751   tree decl, old_scope;
2752   cp_parser_commit_to_tentative_parse (parser);
2753   /* Try to lookup the identifier.  */
2754   old_scope = parser->scope;
2755   parser->scope = scope;
2756   decl = cp_parser_lookup_name_simple (parser, id, location);
2757   parser->scope = old_scope;
2758   /* If the lookup found a template-name, it means that the user forgot
2759   to specify an argument list. Emit a useful error message.  */
2760   if (TREE_CODE (decl) == TEMPLATE_DECL)
2761     error_at (location,
2762               "invalid use of template-name %qE without an argument list",
2763               decl);
2764   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2765     error_at (location, "invalid use of destructor %qD as a type", id);
2766   else if (TREE_CODE (decl) == TYPE_DECL)
2767     /* Something like 'unsigned A a;'  */
2768     error_at (location, "invalid combination of multiple type-specifiers");
2769   else if (!parser->scope)
2770     {
2771       /* Issue an error message.  */
2772       error_at (location, "%qE does not name a type", id);
2773       /* If we're in a template class, it's possible that the user was
2774          referring to a type from a base class.  For example:
2775
2776            template <typename T> struct A { typedef T X; };
2777            template <typename T> struct B : public A<T> { X x; };
2778
2779          The user should have said "typename A<T>::X".  */
2780       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2781         inform (location, "C++11 %<constexpr%> only available with "
2782                 "-std=c++11 or -std=gnu++11");
2783       else if (processing_template_decl && current_class_type
2784                && TYPE_BINFO (current_class_type))
2785         {
2786           tree b;
2787
2788           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2789                b;
2790                b = TREE_CHAIN (b))
2791             {
2792               tree base_type = BINFO_TYPE (b);
2793               if (CLASS_TYPE_P (base_type)
2794                   && dependent_type_p (base_type))
2795                 {
2796                   tree field;
2797                   /* Go from a particular instantiation of the
2798                      template (which will have an empty TYPE_FIELDs),
2799                      to the main version.  */
2800                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2801                   for (field = TYPE_FIELDS (base_type);
2802                        field;
2803                        field = DECL_CHAIN (field))
2804                     if (TREE_CODE (field) == TYPE_DECL
2805                         && DECL_NAME (field) == id)
2806                       {
2807                         inform (location, 
2808                                 "(perhaps %<typename %T::%E%> was intended)",
2809                                 BINFO_TYPE (b), id);
2810                         break;
2811                       }
2812                   if (field)
2813                     break;
2814                 }
2815             }
2816         }
2817     }
2818   /* Here we diagnose qualified-ids where the scope is actually correct,
2819      but the identifier does not resolve to a valid type name.  */
2820   else if (parser->scope != error_mark_node)
2821     {
2822       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2823         error_at (location, "%qE in namespace %qE does not name a type",
2824                   id, parser->scope);
2825       else if (CLASS_TYPE_P (parser->scope)
2826                && constructor_name_p (id, parser->scope))
2827         {
2828           /* A<T>::A<T>() */
2829           error_at (location, "%<%T::%E%> names the constructor, not"
2830                     " the type", parser->scope, id);
2831           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2832             error_at (location, "and %qT has no template constructors",
2833                       parser->scope);
2834         }
2835       else if (TYPE_P (parser->scope)
2836                && dependent_scope_p (parser->scope))
2837         error_at (location, "need %<typename%> before %<%T::%E%> because "
2838                   "%qT is a dependent scope",
2839                   parser->scope, id, parser->scope);
2840       else if (TYPE_P (parser->scope))
2841         error_at (location, "%qE in %q#T does not name a type",
2842                   id, parser->scope);
2843       else
2844         gcc_unreachable ();
2845     }
2846 }
2847
2848 /* Check for a common situation where a type-name should be present,
2849    but is not, and issue a sensible error message.  Returns true if an
2850    invalid type-name was detected.
2851
2852    The situation handled by this function are variable declarations of the
2853    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2854    Usually, `ID' should name a type, but if we got here it means that it
2855    does not. We try to emit the best possible error message depending on
2856    how exactly the id-expression looks like.  */
2857
2858 static bool
2859 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2860 {
2861   tree id;
2862   cp_token *token = cp_lexer_peek_token (parser->lexer);
2863
2864   /* Avoid duplicate error about ambiguous lookup.  */
2865   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2866     {
2867       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2868       if (next->type == CPP_NAME && next->ambiguous_p)
2869         goto out;
2870     }
2871
2872   cp_parser_parse_tentatively (parser);
2873   id = cp_parser_id_expression (parser,
2874                                 /*template_keyword_p=*/false,
2875                                 /*check_dependency_p=*/true,
2876                                 /*template_p=*/NULL,
2877                                 /*declarator_p=*/true,
2878                                 /*optional_p=*/false);
2879   /* If the next token is a (, this is a function with no explicit return
2880      type, i.e. constructor, destructor or conversion op.  */
2881   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2882       || TREE_CODE (id) == TYPE_DECL)
2883     {
2884       cp_parser_abort_tentative_parse (parser);
2885       return false;
2886     }
2887   if (!cp_parser_parse_definitely (parser))
2888     return false;
2889
2890   /* Emit a diagnostic for the invalid type.  */
2891   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2892                                         id, token->location);
2893  out:
2894   /* If we aren't in the middle of a declarator (i.e. in a
2895      parameter-declaration-clause), skip to the end of the declaration;
2896      there's no point in trying to process it.  */
2897   if (!parser->in_declarator_p)
2898     cp_parser_skip_to_end_of_block_or_statement (parser);
2899   return true;
2900 }
2901
2902 /* Consume tokens up to, and including, the next non-nested closing `)'.
2903    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2904    are doing error recovery. Returns -1 if OR_COMMA is true and we
2905    found an unnested comma.  */
2906
2907 static int
2908 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2909                                        bool recovering,
2910                                        bool or_comma,
2911                                        bool consume_paren)
2912 {
2913   unsigned paren_depth = 0;
2914   unsigned brace_depth = 0;
2915   unsigned square_depth = 0;
2916
2917   if (recovering && !or_comma
2918       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2919     return 0;
2920
2921   while (true)
2922     {
2923       cp_token * token = cp_lexer_peek_token (parser->lexer);
2924
2925       switch (token->type)
2926         {
2927         case CPP_EOF:
2928         case CPP_PRAGMA_EOL:
2929           /* If we've run out of tokens, then there is no closing `)'.  */
2930           return 0;
2931
2932         /* This is good for lambda expression capture-lists.  */
2933         case CPP_OPEN_SQUARE:
2934           ++square_depth;
2935           break;
2936         case CPP_CLOSE_SQUARE:
2937           if (!square_depth--)
2938             return 0;
2939           break;
2940
2941         case CPP_SEMICOLON:
2942           /* This matches the processing in skip_to_end_of_statement.  */
2943           if (!brace_depth)
2944             return 0;
2945           break;
2946
2947         case CPP_OPEN_BRACE:
2948           ++brace_depth;
2949           break;
2950         case CPP_CLOSE_BRACE:
2951           if (!brace_depth--)
2952             return 0;
2953           break;
2954
2955         case CPP_COMMA:
2956           if (recovering && or_comma && !brace_depth && !paren_depth
2957               && !square_depth)
2958             return -1;
2959           break;
2960
2961         case CPP_OPEN_PAREN:
2962           if (!brace_depth)
2963             ++paren_depth;
2964           break;
2965
2966         case CPP_CLOSE_PAREN:
2967           if (!brace_depth && !paren_depth--)
2968             {
2969               if (consume_paren)
2970                 cp_lexer_consume_token (parser->lexer);
2971               return 1;
2972             }
2973           break;
2974
2975         default:
2976           break;
2977         }
2978
2979       /* Consume the token.  */
2980       cp_lexer_consume_token (parser->lexer);
2981     }
2982 }
2983
2984 /* Consume tokens until we reach the end of the current statement.
2985    Normally, that will be just before consuming a `;'.  However, if a
2986    non-nested `}' comes first, then we stop before consuming that.  */
2987
2988 static void
2989 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2990 {
2991   unsigned nesting_depth = 0;
2992
2993   while (true)
2994     {
2995       cp_token *token = cp_lexer_peek_token (parser->lexer);
2996
2997       switch (token->type)
2998         {
2999         case CPP_EOF:
3000         case CPP_PRAGMA_EOL:
3001           /* If we've run out of tokens, stop.  */
3002           return;
3003
3004         case CPP_SEMICOLON:
3005           /* If the next token is a `;', we have reached the end of the
3006              statement.  */
3007           if (!nesting_depth)
3008             return;
3009           break;
3010
3011         case CPP_CLOSE_BRACE:
3012           /* If this is a non-nested '}', stop before consuming it.
3013              That way, when confronted with something like:
3014
3015                { 3 + }
3016
3017              we stop before consuming the closing '}', even though we
3018              have not yet reached a `;'.  */
3019           if (nesting_depth == 0)
3020             return;
3021
3022           /* If it is the closing '}' for a block that we have
3023              scanned, stop -- but only after consuming the token.
3024              That way given:
3025
3026                 void f g () { ... }
3027                 typedef int I;
3028
3029              we will stop after the body of the erroneously declared
3030              function, but before consuming the following `typedef'
3031              declaration.  */
3032           if (--nesting_depth == 0)
3033             {
3034               cp_lexer_consume_token (parser->lexer);
3035               return;
3036             }
3037
3038         case CPP_OPEN_BRACE:
3039           ++nesting_depth;
3040           break;
3041
3042         default:
3043           break;
3044         }
3045
3046       /* Consume the token.  */
3047       cp_lexer_consume_token (parser->lexer);
3048     }
3049 }
3050
3051 /* This function is called at the end of a statement or declaration.
3052    If the next token is a semicolon, it is consumed; otherwise, error
3053    recovery is attempted.  */
3054
3055 static void
3056 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3057 {
3058   /* Look for the trailing `;'.  */
3059   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3060     {
3061       /* If there is additional (erroneous) input, skip to the end of
3062          the statement.  */
3063       cp_parser_skip_to_end_of_statement (parser);
3064       /* If the next token is now a `;', consume it.  */
3065       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3066         cp_lexer_consume_token (parser->lexer);
3067     }
3068 }
3069
3070 /* Skip tokens until we have consumed an entire block, or until we
3071    have consumed a non-nested `;'.  */
3072
3073 static void
3074 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3075 {
3076   int nesting_depth = 0;
3077
3078   while (nesting_depth >= 0)
3079     {
3080       cp_token *token = cp_lexer_peek_token (parser->lexer);
3081
3082       switch (token->type)
3083         {
3084         case CPP_EOF:
3085         case CPP_PRAGMA_EOL:
3086           /* If we've run out of tokens, stop.  */
3087           return;
3088
3089         case CPP_SEMICOLON:
3090           /* Stop if this is an unnested ';'. */
3091           if (!nesting_depth)
3092             nesting_depth = -1;
3093           break;
3094
3095         case CPP_CLOSE_BRACE:
3096           /* Stop if this is an unnested '}', or closes the outermost
3097              nesting level.  */
3098           nesting_depth--;
3099           if (nesting_depth < 0)
3100             return;
3101           if (!nesting_depth)
3102             nesting_depth = -1;
3103           break;
3104
3105         case CPP_OPEN_BRACE:
3106           /* Nest. */
3107           nesting_depth++;
3108           break;
3109
3110         default:
3111           break;
3112         }
3113
3114       /* Consume the token.  */
3115       cp_lexer_consume_token (parser->lexer);
3116     }
3117 }
3118
3119 /* Skip tokens until a non-nested closing curly brace is the next
3120    token, or there are no more tokens. Return true in the first case,
3121    false otherwise.  */
3122
3123 static bool
3124 cp_parser_skip_to_closing_brace (cp_parser *parser)
3125 {
3126   unsigned nesting_depth = 0;
3127
3128   while (true)
3129     {
3130       cp_token *token = cp_lexer_peek_token (parser->lexer);
3131
3132       switch (token->type)
3133         {
3134         case CPP_EOF:
3135         case CPP_PRAGMA_EOL:
3136           /* If we've run out of tokens, stop.  */
3137           return false;
3138
3139         case CPP_CLOSE_BRACE:
3140           /* If the next token is a non-nested `}', then we have reached
3141              the end of the current block.  */
3142           if (nesting_depth-- == 0)
3143             return true;
3144           break;
3145
3146         case CPP_OPEN_BRACE:
3147           /* If it the next token is a `{', then we are entering a new
3148              block.  Consume the entire block.  */
3149           ++nesting_depth;
3150           break;
3151
3152         default:
3153           break;
3154         }
3155
3156       /* Consume the token.  */
3157       cp_lexer_consume_token (parser->lexer);
3158     }
3159 }
3160
3161 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3162    parameter is the PRAGMA token, allowing us to purge the entire pragma
3163    sequence.  */
3164
3165 static void
3166 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3167 {
3168   cp_token *token;
3169
3170   parser->lexer->in_pragma = false;
3171
3172   do
3173     token = cp_lexer_consume_token (parser->lexer);
3174   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3175
3176   /* Ensure that the pragma is not parsed again.  */
3177   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3178 }
3179
3180 /* Require pragma end of line, resyncing with it as necessary.  The
3181    arguments are as for cp_parser_skip_to_pragma_eol.  */
3182
3183 static void
3184 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3185 {
3186   parser->lexer->in_pragma = false;
3187   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3188     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3189 }
3190
3191 /* This is a simple wrapper around make_typename_type. When the id is
3192    an unresolved identifier node, we can provide a superior diagnostic
3193    using cp_parser_diagnose_invalid_type_name.  */
3194
3195 static tree
3196 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3197                               tree id, location_t id_location)
3198 {
3199   tree result;
3200   if (TREE_CODE (id) == IDENTIFIER_NODE)
3201     {
3202       result = make_typename_type (scope, id, typename_type,
3203                                    /*complain=*/tf_none);
3204       if (result == error_mark_node)
3205         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3206       return result;
3207     }
3208   return make_typename_type (scope, id, typename_type, tf_error);
3209 }
3210
3211 /* This is a wrapper around the
3212    make_{pointer,ptrmem,reference}_declarator functions that decides
3213    which one to call based on the CODE and CLASS_TYPE arguments. The
3214    CODE argument should be one of the values returned by
3215    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3216    appertain to the pointer or reference.  */
3217
3218 static cp_declarator *
3219 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3220                                     cp_cv_quals cv_qualifiers,
3221                                     cp_declarator *target,
3222                                     tree attributes)
3223 {
3224   if (code == ERROR_MARK)
3225     return cp_error_declarator;
3226
3227   if (code == INDIRECT_REF)
3228     if (class_type == NULL_TREE)
3229       return make_pointer_declarator (cv_qualifiers, target, attributes);
3230     else
3231       return make_ptrmem_declarator (cv_qualifiers, class_type,
3232                                      target, attributes);
3233   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234     return make_reference_declarator (cv_qualifiers, target,
3235                                       false, attributes);
3236   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3237     return make_reference_declarator (cv_qualifiers, target,
3238                                       true, attributes);
3239   gcc_unreachable ();
3240 }
3241
3242 /* Create a new C++ parser.  */
3243
3244 static cp_parser *
3245 cp_parser_new (void)
3246 {
3247   cp_parser *parser;
3248   cp_lexer *lexer;
3249   unsigned i;
3250
3251   /* cp_lexer_new_main is called before doing GC allocation because
3252      cp_lexer_new_main might load a PCH file.  */
3253   lexer = cp_lexer_new_main ();
3254
3255   /* Initialize the binops_by_token so that we can get the tree
3256      directly from the token.  */
3257   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258     binops_by_token[binops[i].token_type] = binops[i];
3259
3260   parser = ggc_alloc_cleared_cp_parser ();
3261   parser->lexer = lexer;
3262   parser->context = cp_parser_context_new (NULL);
3263
3264   /* For now, we always accept GNU extensions.  */
3265   parser->allow_gnu_extensions_p = 1;
3266
3267   /* The `>' token is a greater-than operator, not the end of a
3268      template-id.  */
3269   parser->greater_than_is_operator_p = true;
3270
3271   parser->default_arg_ok_p = true;
3272
3273   /* We are not parsing a constant-expression.  */
3274   parser->integral_constant_expression_p = false;
3275   parser->allow_non_integral_constant_expression_p = false;
3276   parser->non_integral_constant_expression_p = false;
3277
3278   /* Local variable names are not forbidden.  */
3279   parser->local_variables_forbidden_p = false;
3280
3281   /* We are not processing an `extern "C"' declaration.  */
3282   parser->in_unbraced_linkage_specification_p = false;
3283
3284   /* We are not processing a declarator.  */
3285   parser->in_declarator_p = false;
3286
3287   /* We are not processing a template-argument-list.  */
3288   parser->in_template_argument_list_p = false;
3289
3290   /* We are not in an iteration statement.  */
3291   parser->in_statement = 0;
3292
3293   /* We are not in a switch statement.  */
3294   parser->in_switch_statement_p = false;
3295
3296   /* We are not parsing a type-id inside an expression.  */
3297   parser->in_type_id_in_expr_p = false;
3298
3299   /* Declarations aren't implicitly extern "C".  */
3300   parser->implicit_extern_c = false;
3301
3302   /* String literals should be translated to the execution character set.  */
3303   parser->translate_strings_p = true;
3304
3305   /* We are not parsing a function body.  */
3306   parser->in_function_body = false;
3307
3308   /* We can correct until told otherwise.  */
3309   parser->colon_corrects_to_scope_p = true;
3310
3311   /* The unparsed function queue is empty.  */
3312   push_unparsed_function_queues (parser);
3313
3314   /* There are no classes being defined.  */
3315   parser->num_classes_being_defined = 0;
3316
3317   /* No template parameters apply.  */
3318   parser->num_template_parameter_lists = 0;
3319
3320   return parser;
3321 }
3322
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324    and push it onto the parser's lexer stack.  This is used for delayed
3325    parsing of in-class method bodies and default arguments, and should
3326    not be confused with tentative parsing.  */
3327 static void
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3329 {
3330   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331   lexer->next = parser->lexer;
3332   parser->lexer = lexer;
3333
3334   /* Move the current source position to that of the first token in the
3335      new lexer.  */
3336   cp_lexer_set_source_position_from_token (lexer->next_token);
3337 }
3338
3339 /* Pop the top lexer off the parser stack.  This is never used for the
3340    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3341 static void
3342 cp_parser_pop_lexer (cp_parser *parser)
3343 {
3344   cp_lexer *lexer = parser->lexer;
3345   parser->lexer = lexer->next;
3346   cp_lexer_destroy (lexer);
3347
3348   /* Put the current source position back where it was before this
3349      lexer was pushed.  */
3350   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3351 }
3352
3353 /* Lexical conventions [gram.lex]  */
3354
3355 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3356    identifier.  */
3357
3358 static tree
3359 cp_parser_identifier (cp_parser* parser)
3360 {
3361   cp_token *token;
3362
3363   /* Look for the identifier.  */
3364   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365   /* Return the value.  */
3366   return token ? token->u.value : error_mark_node;
3367 }
3368
3369 /* Parse a sequence of adjacent string constants.  Returns a
3370    TREE_STRING representing the combined, nul-terminated string
3371    constant.  If TRANSLATE is true, translate the string to the
3372    execution character set.  If WIDE_OK is true, a wide string is
3373    invalid here.
3374
3375    C++98 [lex.string] says that if a narrow string literal token is
3376    adjacent to a wide string literal token, the behavior is undefined.
3377    However, C99 6.4.5p4 says that this results in a wide string literal.
3378    We follow C99 here, for consistency with the C front end.
3379
3380    This code is largely lifted from lex_string() in c-lex.c.
3381
3382    FUTURE: ObjC++ will need to handle @-strings here.  */
3383 static tree
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3385 {
3386   tree value;
3387   size_t count;
3388   struct obstack str_ob;
3389   cpp_string str, istr, *strs;
3390   cp_token *tok;
3391   enum cpp_ttype type, curr_type;
3392   int have_suffix_p = 0;
3393   tree string_tree;
3394   tree suffix_id = NULL_TREE;
3395   bool curr_tok_is_userdef_p = false;
3396
3397   tok = cp_lexer_peek_token (parser->lexer);
3398   if (!cp_parser_is_string_literal (tok))
3399     {
3400       cp_parser_error (parser, "expected string-literal");
3401       return error_mark_node;
3402     }
3403
3404   if (cpp_userdef_string_p (tok->type))
3405     {
3406       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407       curr_type = cpp_userdef_string_remove_type (tok->type);
3408       curr_tok_is_userdef_p = true;
3409     }
3410   else
3411     {
3412       string_tree = tok->u.value;
3413       curr_type = tok->type;
3414     }
3415   type = curr_type;
3416
3417   /* Try to avoid the overhead of creating and destroying an obstack
3418      for the common case of just one string.  */
3419   if (!cp_parser_is_string_literal
3420       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3421     {
3422       cp_lexer_consume_token (parser->lexer);
3423
3424       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425       str.len = TREE_STRING_LENGTH (string_tree);
3426       count = 1;
3427
3428       if (curr_tok_is_userdef_p)
3429         {
3430           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3431           have_suffix_p = 1;
3432           curr_type = cpp_userdef_string_remove_type (tok->type);
3433         }
3434       else
3435         curr_type = tok->type;
3436
3437       strs = &str;
3438     }
3439   else
3440     {
3441       gcc_obstack_init (&str_ob);
3442       count = 0;
3443
3444       do
3445         {
3446           cp_lexer_consume_token (parser->lexer);
3447           count++;
3448           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449           str.len = TREE_STRING_LENGTH (string_tree);
3450
3451           if (curr_tok_is_userdef_p)
3452             {
3453               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454               if (have_suffix_p == 0)
3455                 {
3456                   suffix_id = curr_suffix_id;
3457                   have_suffix_p = 1;
3458                 }
3459               else if (have_suffix_p == 1
3460                        && curr_suffix_id != suffix_id)
3461                 {
3462                   error ("inconsistent user-defined literal suffixes"
3463                          " %qD and %qD in string literal",
3464                          suffix_id, curr_suffix_id);
3465                   have_suffix_p = -1;
3466                 }
3467               curr_type = cpp_userdef_string_remove_type (tok->type);
3468             }
3469           else
3470             curr_type = tok->type;
3471
3472           if (type != curr_type)
3473             {
3474               if (type == CPP_STRING)
3475                 type = curr_type;
3476               else if (curr_type != CPP_STRING)
3477                 error_at (tok->location,
3478                           "unsupported non-standard concatenation "
3479                           "of string literals");
3480             }
3481
3482           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3483
3484           tok = cp_lexer_peek_token (parser->lexer);
3485           if (cpp_userdef_string_p (tok->type))
3486             {
3487               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488               curr_type = cpp_userdef_string_remove_type (tok->type);
3489               curr_tok_is_userdef_p = true;
3490             }
3491           else
3492             {
3493               string_tree = tok->u.value;
3494               curr_type = tok->type;
3495               curr_tok_is_userdef_p = false;
3496             }
3497         }
3498       while (cp_parser_is_string_literal (tok));
3499
3500       strs = (cpp_string *) obstack_finish (&str_ob);
3501     }
3502
3503   if (type != CPP_STRING && !wide_ok)
3504     {
3505       cp_parser_error (parser, "a wide string is invalid in this context");
3506       type = CPP_STRING;
3507     }
3508
3509   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510       (parse_in, strs, count, &istr, type))
3511     {
3512       value = build_string (istr.len, (const char *)istr.text);
3513       free (CONST_CAST (unsigned char *, istr.text));
3514
3515       switch (type)
3516         {
3517         default:
3518         case CPP_STRING:
3519         case CPP_UTF8STRING:
3520           TREE_TYPE (value) = char_array_type_node;
3521           break;
3522         case CPP_STRING16:
3523           TREE_TYPE (value) = char16_array_type_node;
3524           break;
3525         case CPP_STRING32:
3526           TREE_TYPE (value) = char32_array_type_node;
3527           break;
3528         case CPP_WSTRING:
3529           TREE_TYPE (value) = wchar_array_type_node;
3530           break;
3531         }
3532
3533       value = fix_string_type (value);
3534
3535       if (have_suffix_p)
3536         {
3537           tree literal = build_userdef_literal (suffix_id, value,
3538                                                 OT_NONE, NULL_TREE);
3539           tok->u.value = literal;
3540           return cp_parser_userdef_string_literal (tok);
3541         }
3542     }
3543   else
3544     /* cpp_interpret_string has issued an error.  */
3545     value = error_mark_node;
3546
3547   if (count > 1)
3548     obstack_free (&str_ob, 0);
3549
3550   return value;
3551 }
3552
3553 /* Look up a literal operator with the name and the exact arguments.  */
3554
3555 static tree
3556 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3557 {
3558   tree decl, fns;
3559   decl = lookup_name (name);
3560   if (!decl || !is_overloaded_fn (decl))
3561     return error_mark_node;
3562
3563   for (fns = decl; fns; fns = OVL_NEXT (fns))
3564     {
3565       unsigned int ix;
3566       bool found = true;
3567       tree fn = OVL_CURRENT (fns);
3568       tree argtypes = NULL_TREE;
3569       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3570       if (argtypes != NULL_TREE)
3571         {
3572           for (ix = 0; ix < vec_safe_length (args) && argtypes != NULL_TREE;
3573                ++ix, argtypes = TREE_CHAIN (argtypes))
3574             {
3575               tree targ = TREE_VALUE (argtypes);
3576               tree tparm = TREE_TYPE ((*args)[ix]);
3577               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3578               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3579               if ((ptr || arr || !same_type_p (targ, tparm))
3580                   && (!ptr || !arr
3581                       || !same_type_p (TREE_TYPE (targ),
3582                                        TREE_TYPE (tparm))))
3583                 found = false;
3584             }
3585           if (found
3586               && ix == vec_safe_length (args)
3587               /* May be this should be sufficient_parms_p instead,
3588                  depending on how exactly should user-defined literals
3589                  work in presence of default arguments on the literal
3590                  operator parameters.  */
3591               && argtypes == void_list_node)
3592             return fn;
3593         }
3594     }
3595
3596   return error_mark_node;
3597 }
3598
3599 /* Parse a user-defined char constant.  Returns a call to a user-defined
3600    literal operator taking the character as an argument.  */
3601
3602 static tree
3603 cp_parser_userdef_char_literal (cp_parser *parser)
3604 {
3605   cp_token *token = cp_lexer_consume_token (parser->lexer);
3606   tree literal = token->u.value;
3607   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3608   tree value = USERDEF_LITERAL_VALUE (literal);
3609   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3610   tree decl, result;
3611
3612   /* Build up a call to the user-defined operator  */
3613   /* Lookup the name we got back from the id-expression.  */
3614   vec<tree, va_gc> *args = make_tree_vector ();
3615   vec_safe_push (args, value);
3616   decl = lookup_literal_operator (name, args);
3617   if (!decl || decl == error_mark_node)
3618     {
3619       error ("unable to find character literal operator %qD with %qT argument",
3620              name, TREE_TYPE (value));
3621       release_tree_vector (args);
3622       return error_mark_node;
3623     }
3624   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3625   release_tree_vector (args);
3626   if (result != error_mark_node)
3627     return result;
3628
3629   error ("unable to find character literal operator %qD with %qT argument",
3630          name, TREE_TYPE (value));
3631   return error_mark_node;
3632 }
3633
3634 /* A subroutine of cp_parser_userdef_numeric_literal to
3635    create a char... template parameter pack from a string node.  */
3636
3637 static tree
3638 make_char_string_pack (tree value)
3639 {
3640   tree charvec;
3641   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3642   const char *str = TREE_STRING_POINTER (value);
3643   int i, len = TREE_STRING_LENGTH (value) - 1;
3644   tree argvec = make_tree_vec (1);
3645
3646   /* Fill in CHARVEC with all of the parameters.  */
3647   charvec = make_tree_vec (len);
3648   for (i = 0; i < len; ++i)
3649     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3650
3651   /* Build the argument packs.  */
3652   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3653   TREE_TYPE (argpack) = char_type_node;
3654
3655   TREE_VEC_ELT (argvec, 0) = argpack;
3656
3657   return argvec;
3658 }
3659
3660 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3661    literal operator.  */
3662
3663 static tree
3664 cp_parser_userdef_numeric_literal (cp_parser *parser)
3665 {
3666   cp_token *token = cp_lexer_consume_token (parser->lexer);
3667   tree literal = token->u.value;
3668   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3669   tree value = USERDEF_LITERAL_VALUE (literal);
3670   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3671   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3672   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3673   tree decl, result;
3674   vec<tree, va_gc> *args;
3675
3676   /* Look for a literal operator taking the exact type of numeric argument
3677      as the literal value.  */
3678   args = make_tree_vector ();
3679   vec_safe_push (args, value);
3680   decl = lookup_literal_operator (name, args);
3681   if (decl && decl != error_mark_node)
3682     {
3683       result = finish_call_expr (decl, &args, false, true, tf_none);
3684       if (result != error_mark_node)
3685         {
3686           if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3687             warning_at (token->location, OPT_Woverflow,
3688                         "integer literal exceeds range of %qT type",
3689                         long_long_unsigned_type_node);
3690           else
3691             {
3692               if (overflow > 0)
3693                 warning_at (token->location, OPT_Woverflow,
3694                             "floating literal exceeds range of %qT type",
3695                             long_double_type_node);
3696               else if (overflow < 0)
3697                 warning_at (token->location, OPT_Woverflow,
3698                             "floating literal truncated to zero");
3699             }
3700           release_tree_vector (args);
3701           return result;
3702         }
3703     }
3704   release_tree_vector (args);
3705
3706   /* If the numeric argument didn't work, look for a raw literal
3707      operator taking a const char* argument consisting of the number
3708      in string format.  */
3709   args = make_tree_vector ();
3710   vec_safe_push (args, num_string);
3711   decl = lookup_literal_operator (name, args);
3712   if (decl && decl != error_mark_node)
3713     {
3714       result = finish_call_expr (decl, &args, false, true, tf_none);
3715       if (result != error_mark_node)
3716         {
3717           release_tree_vector (args);
3718           return result;
3719         }
3720     }
3721   release_tree_vector (args);
3722
3723   /* If the raw literal didn't work, look for a non-type template
3724      function with parameter pack char....  Call the function with
3725      template parameter characters representing the number.  */
3726   args = make_tree_vector ();
3727   decl = lookup_literal_operator (name, args);
3728   if (decl && decl != error_mark_node)
3729     {
3730       tree tmpl_args = make_char_string_pack (num_string);
3731       decl = lookup_template_function (decl, tmpl_args);
3732       result = finish_call_expr (decl, &args, false, true, tf_none);
3733       if (result != error_mark_node)
3734         {
3735           release_tree_vector (args);
3736           return result;
3737         }
3738     }
3739   release_tree_vector (args);
3740
3741   error ("unable to find numeric literal operator %qD", name);
3742   return error_mark_node;
3743 }
3744
3745 /* Parse a user-defined string constant.  Returns a call to a user-defined
3746    literal operator taking a character pointer and the length of the string
3747    as arguments.  */
3748
3749 static tree
3750 cp_parser_userdef_string_literal (cp_token *token)
3751 {
3752   tree literal = token->u.value;
3753   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3754   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3755   tree value = USERDEF_LITERAL_VALUE (literal);
3756   int len = TREE_STRING_LENGTH (value)
3757         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3758   tree decl, result;
3759
3760   /* Build up a call to the user-defined operator  */
3761   /* Lookup the name we got back from the id-expression.  */
3762   vec<tree, va_gc> *args = make_tree_vector ();
3763   vec_safe_push (args, value);
3764   vec_safe_push (args, build_int_cst (size_type_node, len));
3765   decl = lookup_name (name);
3766   if (!decl || decl == error_mark_node)
3767     {
3768       error ("unable to find string literal operator %qD", name);
3769       release_tree_vector (args);
3770       return error_mark_node;
3771     }
3772   result = finish_call_expr (decl, &args, false, true, tf_none);
3773   release_tree_vector (args);
3774   if (result != error_mark_node)
3775     return result;
3776
3777   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3778          name, TREE_TYPE (value), size_type_node);
3779   return error_mark_node;
3780 }
3781
3782
3783 /* Basic concepts [gram.basic]  */
3784
3785 /* Parse a translation-unit.
3786
3787    translation-unit:
3788      declaration-seq [opt]
3789
3790    Returns TRUE if all went well.  */
3791
3792 static bool
3793 cp_parser_translation_unit (cp_parser* parser)
3794 {
3795   /* The address of the first non-permanent object on the declarator
3796      obstack.  */
3797   static void *declarator_obstack_base;
3798
3799   bool success;
3800
3801   /* Create the declarator obstack, if necessary.  */
3802   if (!cp_error_declarator)
3803     {
3804       gcc_obstack_init (&declarator_obstack);
3805       /* Create the error declarator.  */
3806       cp_error_declarator = make_declarator (cdk_error);
3807       /* Create the empty parameter list.  */
3808       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3809       /* Remember where the base of the declarator obstack lies.  */
3810       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3811     }
3812
3813   cp_parser_declaration_seq_opt (parser);
3814
3815   /* If there are no tokens left then all went well.  */
3816   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3817     {
3818       /* Get rid of the token array; we don't need it any more.  */
3819       cp_lexer_destroy (parser->lexer);
3820       parser->lexer = NULL;
3821
3822       /* This file might have been a context that's implicitly extern
3823          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3824       if (parser->implicit_extern_c)
3825         {
3826           pop_lang_context ();
3827           parser->implicit_extern_c = false;
3828         }
3829
3830       /* Finish up.  */
3831       finish_translation_unit ();
3832
3833       success = true;
3834     }
3835   else
3836     {
3837       cp_parser_error (parser, "expected declaration");
3838       success = false;
3839     }
3840
3841   /* Make sure the declarator obstack was fully cleaned up.  */
3842   gcc_assert (obstack_next_free (&declarator_obstack)
3843               == declarator_obstack_base);
3844
3845   /* All went well.  */
3846   return success;
3847 }
3848
3849 /* Return the appropriate tsubst flags for parsing, possibly in N3276
3850    decltype context.  */
3851
3852 static inline tsubst_flags_t
3853 complain_flags (bool decltype_p)
3854 {
3855   tsubst_flags_t complain = tf_warning_or_error;
3856   if (decltype_p)
3857     complain |= tf_decltype;
3858   return complain;
3859 }
3860
3861 /* Expressions [gram.expr] */
3862
3863 /* Parse a primary-expression.
3864
3865    primary-expression:
3866      literal
3867      this
3868      ( expression )
3869      id-expression
3870
3871    GNU Extensions:
3872
3873    primary-expression:
3874      ( compound-statement )
3875      __builtin_va_arg ( assignment-expression , type-id )
3876      __builtin_offsetof ( type-id , offsetof-expression )
3877
3878    C++ Extensions:
3879      __has_nothrow_assign ( type-id )   
3880      __has_nothrow_constructor ( type-id )
3881      __has_nothrow_copy ( type-id )
3882      __has_trivial_assign ( type-id )   
3883      __has_trivial_constructor ( type-id )
3884      __has_trivial_copy ( type-id )
3885      __has_trivial_destructor ( type-id )
3886      __has_virtual_destructor ( type-id )     
3887      __is_abstract ( type-id )
3888      __is_base_of ( type-id , type-id )
3889      __is_class ( type-id )
3890      __is_convertible_to ( type-id , type-id )     
3891      __is_empty ( type-id )
3892      __is_enum ( type-id )
3893      __is_final ( type-id )
3894      __is_literal_type ( type-id )
3895      __is_pod ( type-id )
3896      __is_polymorphic ( type-id )
3897      __is_std_layout ( type-id )
3898      __is_trivial ( type-id )
3899      __is_union ( type-id )
3900
3901    Objective-C++ Extension:
3902
3903    primary-expression:
3904      objc-expression
3905
3906    literal:
3907      __null
3908
3909    ADDRESS_P is true iff this expression was immediately preceded by
3910    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3911    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3912    true iff this expression is a template argument.
3913
3914    Returns a representation of the expression.  Upon return, *IDK
3915    indicates what kind of id-expression (if any) was present.  */
3916
3917 static tree
3918 cp_parser_primary_expression (cp_parser *parser,
3919                               bool address_p,
3920                               bool cast_p,
3921                               bool template_arg_p,
3922                               bool decltype_p,
3923                               cp_id_kind *idk)
3924 {
3925   cp_token *token = NULL;
3926
3927   /* Assume the primary expression is not an id-expression.  */
3928   *idk = CP_ID_KIND_NONE;
3929
3930   /* Peek at the next token.  */
3931   token = cp_lexer_peek_token (parser->lexer);
3932   switch (token->type)
3933     {
3934       /* literal:
3935            integer-literal
3936            character-literal
3937            floating-literal
3938            string-literal
3939            boolean-literal
3940            pointer-literal
3941            user-defined-literal  */
3942     case CPP_CHAR:
3943     case CPP_CHAR16:
3944     case CPP_CHAR32:
3945     case CPP_WCHAR:
3946     case CPP_NUMBER:
3947       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3948         return cp_parser_userdef_numeric_literal (parser);
3949       token = cp_lexer_consume_token (parser->lexer);
3950       if (TREE_CODE (token->u.value) == FIXED_CST)
3951         {
3952           error_at (token->location,
3953                     "fixed-point types not supported in C++");
3954           return error_mark_node;
3955         }
3956       /* Floating-point literals are only allowed in an integral
3957          constant expression if they are cast to an integral or
3958          enumeration type.  */
3959       if (TREE_CODE (token->u.value) == REAL_CST
3960           && parser->integral_constant_expression_p
3961           && pedantic)
3962         {
3963           /* CAST_P will be set even in invalid code like "int(2.7 +
3964              ...)".   Therefore, we have to check that the next token
3965              is sure to end the cast.  */
3966           if (cast_p)
3967             {
3968               cp_token *next_token;
3969
3970               next_token = cp_lexer_peek_token (parser->lexer);
3971               if (/* The comma at the end of an
3972                      enumerator-definition.  */
3973                   next_token->type != CPP_COMMA
3974                   /* The curly brace at the end of an enum-specifier.  */
3975                   && next_token->type != CPP_CLOSE_BRACE
3976                   /* The end of a statement.  */
3977                   && next_token->type != CPP_SEMICOLON
3978                   /* The end of the cast-expression.  */
3979                   && next_token->type != CPP_CLOSE_PAREN
3980                   /* The end of an array bound.  */
3981                   && next_token->type != CPP_CLOSE_SQUARE
3982                   /* The closing ">" in a template-argument-list.  */
3983                   && (next_token->type != CPP_GREATER
3984                       || parser->greater_than_is_operator_p)
3985                   /* C++0x only: A ">>" treated like two ">" tokens,
3986                      in a template-argument-list.  */
3987                   && (next_token->type != CPP_RSHIFT
3988                       || (cxx_dialect == cxx98)
3989                       || parser->greater_than_is_operator_p))
3990                 cast_p = false;
3991             }
3992
3993           /* If we are within a cast, then the constraint that the
3994              cast is to an integral or enumeration type will be
3995              checked at that point.  If we are not within a cast, then
3996              this code is invalid.  */
3997           if (!cast_p)
3998             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3999         }
4000       return token->u.value;
4001
4002     case CPP_CHAR_USERDEF:
4003     case CPP_CHAR16_USERDEF:
4004     case CPP_CHAR32_USERDEF:
4005     case CPP_WCHAR_USERDEF:
4006       return cp_parser_userdef_char_literal (parser);
4007
4008     case CPP_STRING:
4009     case CPP_STRING16:
4010     case CPP_STRING32:
4011     case CPP_WSTRING:
4012     case CPP_UTF8STRING:
4013     case CPP_STRING_USERDEF:
4014     case CPP_STRING16_USERDEF:
4015     case CPP_STRING32_USERDEF:
4016     case CPP_WSTRING_USERDEF:
4017     case CPP_UTF8STRING_USERDEF:
4018       /* ??? Should wide strings be allowed when parser->translate_strings_p
4019          is false (i.e. in attributes)?  If not, we can kill the third
4020          argument to cp_parser_string_literal.  */
4021       return cp_parser_string_literal (parser,
4022                                        parser->translate_strings_p,
4023                                        true);
4024
4025     case CPP_OPEN_PAREN:
4026       {
4027         tree expr;
4028         bool saved_greater_than_is_operator_p;
4029
4030         /* Consume the `('.  */
4031         cp_lexer_consume_token (parser->lexer);
4032         /* Within a parenthesized expression, a `>' token is always
4033            the greater-than operator.  */
4034         saved_greater_than_is_operator_p
4035           = parser->greater_than_is_operator_p;
4036         parser->greater_than_is_operator_p = true;
4037         /* If we see `( { ' then we are looking at the beginning of
4038            a GNU statement-expression.  */
4039         if (cp_parser_allow_gnu_extensions_p (parser)
4040             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4041           {
4042             /* Statement-expressions are not allowed by the standard.  */
4043             pedwarn (token->location, OPT_Wpedantic, 
4044                      "ISO C++ forbids braced-groups within expressions");
4045
4046             /* And they're not allowed outside of a function-body; you
4047                cannot, for example, write:
4048
4049                  int i = ({ int j = 3; j + 1; });
4050
4051                at class or namespace scope.  */
4052             if (!parser->in_function_body
4053                 || parser->in_template_argument_list_p)
4054               {
4055                 error_at (token->location,
4056                           "statement-expressions are not allowed outside "
4057                           "functions nor in template-argument lists");
4058                 cp_parser_skip_to_end_of_block_or_statement (parser);
4059                 expr = error_mark_node;
4060               }
4061             else
4062               {
4063                 /* Start the statement-expression.  */
4064                 expr = begin_stmt_expr ();
4065                 /* Parse the compound-statement.  */
4066                 cp_parser_compound_statement (parser, expr, false, false);
4067                 /* Finish up.  */
4068                 expr = finish_stmt_expr (expr, false);
4069               }
4070           }
4071         else
4072           {
4073             /* Parse the parenthesized expression.  */
4074             expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4075             /* Let the front end know that this expression was
4076                enclosed in parentheses. This matters in case, for
4077                example, the expression is of the form `A::B', since
4078                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4079                not.  */
4080             finish_parenthesized_expr (expr);
4081             /* DR 705: Wrapping an unqualified name in parentheses
4082                suppresses arg-dependent lookup.  We want to pass back
4083                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4084                (c++/37862), but none of the others.  */
4085             if (*idk != CP_ID_KIND_QUALIFIED)
4086               *idk = CP_ID_KIND_NONE;
4087           }
4088         /* The `>' token might be the end of a template-id or
4089            template-parameter-list now.  */
4090         parser->greater_than_is_operator_p
4091           = saved_greater_than_is_operator_p;
4092         /* Consume the `)'.  */
4093         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4094           cp_parser_skip_to_end_of_statement (parser);
4095
4096         return expr;
4097       }
4098
4099     case CPP_OPEN_SQUARE:
4100       if (c_dialect_objc ())
4101         /* We have an Objective-C++ message. */
4102         return cp_parser_objc_expression (parser);
4103       {
4104         tree lam = cp_parser_lambda_expression (parser);
4105         /* Don't warn about a failed tentative parse.  */
4106         if (cp_parser_error_occurred (parser))
4107           return error_mark_node;
4108         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4109         return lam;
4110       }
4111
4112     case CPP_OBJC_STRING:
4113       if (c_dialect_objc ())
4114         /* We have an Objective-C++ string literal. */
4115         return cp_parser_objc_expression (parser);
4116       cp_parser_error (parser, "expected primary-expression");
4117       return error_mark_node;
4118
4119     case CPP_KEYWORD:
4120       switch (token->keyword)
4121         {
4122           /* These two are the boolean literals.  */
4123         case RID_TRUE:
4124           cp_lexer_consume_token (parser->lexer);
4125           return boolean_true_node;
4126         case RID_FALSE:
4127           cp_lexer_consume_token (parser->lexer);
4128           return boolean_false_node;
4129
4130           /* The `__null' literal.  */
4131         case RID_NULL:
4132           cp_lexer_consume_token (parser->lexer);
4133           return null_node;
4134
4135           /* The `nullptr' literal.  */
4136         case RID_NULLPTR:
4137           cp_lexer_consume_token (parser->lexer);
4138           return nullptr_node;
4139
4140           /* Recognize the `this' keyword.  */
4141         case RID_THIS:
4142           cp_lexer_consume_token (parser->lexer);
4143           if (parser->local_variables_forbidden_p)
4144             {
4145               error_at (token->location,
4146                         "%<this%> may not be used in this context");
4147               return error_mark_node;
4148             }
4149           /* Pointers cannot appear in constant-expressions.  */
4150           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4151             return error_mark_node;
4152           return finish_this_expr ();
4153
4154           /* The `operator' keyword can be the beginning of an
4155              id-expression.  */
4156         case RID_OPERATOR:
4157           goto id_expression;
4158
4159         case RID_FUNCTION_NAME:
4160         case RID_PRETTY_FUNCTION_NAME:
4161         case RID_C99_FUNCTION_NAME:
4162           {
4163             non_integral_constant name;
4164
4165             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4166                __func__ are the names of variables -- but they are
4167                treated specially.  Therefore, they are handled here,
4168                rather than relying on the generic id-expression logic
4169                below.  Grammatically, these names are id-expressions.
4170
4171                Consume the token.  */
4172             token = cp_lexer_consume_token (parser->lexer);
4173
4174             switch (token->keyword)
4175               {
4176               case RID_FUNCTION_NAME:
4177                 name = NIC_FUNC_NAME;
4178                 break;
4179               case RID_PRETTY_FUNCTION_NAME:
4180                 name = NIC_PRETTY_FUNC;
4181                 break;
4182               case RID_C99_FUNCTION_NAME:
4183                 name = NIC_C99_FUNC;
4184                 break;
4185               default:
4186                 gcc_unreachable ();
4187               }
4188
4189             if (cp_parser_non_integral_constant_expression (parser, name))
4190               return error_mark_node;
4191
4192             /* Look up the name.  */
4193             return finish_fname (token->u.value);
4194           }
4195
4196         case RID_VA_ARG:
4197           {
4198             tree expression;
4199             tree type;
4200             source_location type_location;
4201
4202             /* The `__builtin_va_arg' construct is used to handle
4203                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4204             cp_lexer_consume_token (parser->lexer);
4205             /* Look for the opening `('.  */
4206             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4207             /* Now, parse the assignment-expression.  */
4208             expression = cp_parser_assignment_expression (parser,
4209                                                           /*cast_p=*/false, NULL);
4210             /* Look for the `,'.  */
4211             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4212             type_location = cp_lexer_peek_token (parser->lexer)->location;
4213             /* Parse the type-id.  */
4214             type = cp_parser_type_id (parser);
4215             /* Look for the closing `)'.  */
4216             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4217             /* Using `va_arg' in a constant-expression is not
4218                allowed.  */
4219             if (cp_parser_non_integral_constant_expression (parser,
4220                                                             NIC_VA_ARG))
4221               return error_mark_node;
4222             return build_x_va_arg (type_location, expression, type);
4223           }
4224
4225         case RID_OFFSETOF:
4226           return cp_parser_builtin_offsetof (parser);
4227
4228         case RID_HAS_NOTHROW_ASSIGN:
4229         case RID_HAS_NOTHROW_CONSTRUCTOR:
4230         case RID_HAS_NOTHROW_COPY:        
4231         case RID_HAS_TRIVIAL_ASSIGN:
4232         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4233         case RID_HAS_TRIVIAL_COPY:        
4234         case RID_HAS_TRIVIAL_DESTRUCTOR:
4235         case RID_HAS_VIRTUAL_DESTRUCTOR:
4236         case RID_IS_ABSTRACT:
4237         case RID_IS_BASE_OF:
4238         case RID_IS_CLASS:
4239         case RID_IS_CONVERTIBLE_TO:
4240         case RID_IS_EMPTY:
4241         case RID_IS_ENUM:
4242         case RID_IS_FINAL:
4243         case RID_IS_LITERAL_TYPE:
4244         case RID_IS_POD:
4245         case RID_IS_POLYMORPHIC:
4246         case RID_IS_STD_LAYOUT:
4247         case RID_IS_TRIVIAL:
4248         case RID_IS_UNION:
4249           return cp_parser_trait_expr (parser, token->keyword);
4250
4251         /* Objective-C++ expressions.  */
4252         case RID_AT_ENCODE:
4253         case RID_AT_PROTOCOL:
4254         case RID_AT_SELECTOR:
4255           return cp_parser_objc_expression (parser);
4256
4257         case RID_TEMPLATE:
4258           if (parser->in_function_body
4259               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4260                   == CPP_LESS))
4261             {
4262               error_at (token->location,
4263                         "a template declaration cannot appear at block scope");
4264               cp_parser_skip_to_end_of_block_or_statement (parser);
4265               return error_mark_node;
4266             }
4267         default:
4268           cp_parser_error (parser, "expected primary-expression");
4269           return error_mark_node;
4270         }
4271
4272       /* An id-expression can start with either an identifier, a
4273          `::' as the beginning of a qualified-id, or the "operator"
4274          keyword.  */
4275     case CPP_NAME:
4276     case CPP_SCOPE:
4277     case CPP_TEMPLATE_ID:
4278     case CPP_NESTED_NAME_SPECIFIER:
4279       {
4280         tree id_expression;
4281         tree decl;
4282         const char *error_msg;
4283         bool template_p;
4284         bool done;
4285         cp_token *id_expr_token;
4286
4287       id_expression:
4288         /* Parse the id-expression.  */
4289         id_expression
4290           = cp_parser_id_expression (parser,
4291                                      /*template_keyword_p=*/false,
4292                                      /*check_dependency_p=*/true,
4293                                      &template_p,
4294                                      /*declarator_p=*/false,
4295                                      /*optional_p=*/false);
4296         if (id_expression == error_mark_node)
4297           return error_mark_node;
4298         id_expr_token = token;
4299         token = cp_lexer_peek_token (parser->lexer);
4300         done = (token->type != CPP_OPEN_SQUARE
4301                 && token->type != CPP_OPEN_PAREN
4302                 && token->type != CPP_DOT
4303                 && token->type != CPP_DEREF
4304                 && token->type != CPP_PLUS_PLUS
4305                 && token->type != CPP_MINUS_MINUS);
4306         /* If we have a template-id, then no further lookup is
4307            required.  If the template-id was for a template-class, we
4308            will sometimes have a TYPE_DECL at this point.  */
4309         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4310                  || TREE_CODE (id_expression) == TYPE_DECL)
4311           decl = id_expression;
4312         /* Look up the name.  */
4313         else
4314           {
4315             tree ambiguous_decls;
4316
4317             /* If we already know that this lookup is ambiguous, then
4318                we've already issued an error message; there's no reason
4319                to check again.  */
4320             if (id_expr_token->type == CPP_NAME
4321                 && id_expr_token->ambiguous_p)
4322               {
4323                 cp_parser_simulate_error (parser);
4324                 return error_mark_node;
4325               }
4326
4327             decl = cp_parser_lookup_name (parser, id_expression,
4328                                           none_type,
4329                                           template_p,
4330                                           /*is_namespace=*/false,
4331                                           /*check_dependency=*/true,
4332                                           &ambiguous_decls,
4333                                           id_expr_token->location);
4334             /* If the lookup was ambiguous, an error will already have
4335                been issued.  */
4336             if (ambiguous_decls)
4337               return error_mark_node;
4338
4339             /* In Objective-C++, we may have an Objective-C 2.0
4340                dot-syntax for classes here.  */
4341             if (c_dialect_objc ()
4342                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4343                 && TREE_CODE (decl) == TYPE_DECL
4344                 && objc_is_class_name (decl))
4345               {
4346                 tree component;
4347                 cp_lexer_consume_token (parser->lexer);
4348                 component = cp_parser_identifier (parser);
4349                 if (component == error_mark_node)
4350                   return error_mark_node;
4351
4352                 return objc_build_class_component_ref (id_expression, component);
4353               }
4354
4355             /* In Objective-C++, an instance variable (ivar) may be preferred
4356                to whatever cp_parser_lookup_name() found.  */
4357             decl = objc_lookup_ivar (decl, id_expression);
4358
4359             /* If name lookup gives us a SCOPE_REF, then the
4360                qualifying scope was dependent.  */
4361             if (TREE_CODE (decl) == SCOPE_REF)
4362               {
4363                 /* At this point, we do not know if DECL is a valid
4364                    integral constant expression.  We assume that it is
4365                    in fact such an expression, so that code like:
4366
4367                       template <int N> struct A {
4368                         int a[B<N>::i];
4369                       };
4370                      
4371                    is accepted.  At template-instantiation time, we
4372                    will check that B<N>::i is actually a constant.  */
4373                 return decl;
4374               }
4375             /* Check to see if DECL is a local variable in a context
4376                where that is forbidden.  */
4377             if (parser->local_variables_forbidden_p
4378                 && local_variable_p (decl))
4379               {
4380                 /* It might be that we only found DECL because we are
4381                    trying to be generous with pre-ISO scoping rules.
4382                    For example, consider:
4383
4384                      int i;
4385                      void g() {
4386                        for (int i = 0; i < 10; ++i) {}
4387                        extern void f(int j = i);
4388                      }
4389
4390                    Here, name look up will originally find the out
4391                    of scope `i'.  We need to issue a warning message,
4392                    but then use the global `i'.  */
4393                 decl = check_for_out_of_scope_variable (decl);
4394                 if (local_variable_p (decl))
4395                   {
4396                     error_at (id_expr_token->location,
4397                               "local variable %qD may not appear in this context",
4398                               decl);
4399                     return error_mark_node;
4400                   }
4401               }
4402           }
4403
4404         decl = (finish_id_expression
4405                 (id_expression, decl, parser->scope,
4406                  idk,
4407                  parser->integral_constant_expression_p,
4408                  parser->allow_non_integral_constant_expression_p,
4409                  &parser->non_integral_constant_expression_p,
4410                  template_p, done, address_p,
4411                  template_arg_p,
4412                  &error_msg,
4413                  id_expr_token->location));
4414         if (error_msg)
4415           cp_parser_error (parser, error_msg);
4416         return decl;
4417       }
4418
4419       /* Anything else is an error.  */
4420     default:
4421       cp_parser_error (parser, "expected primary-expression");
4422       return error_mark_node;
4423     }
4424 }
4425
4426 static inline tree
4427 cp_parser_primary_expression (cp_parser *parser,
4428                               bool address_p,
4429                               bool cast_p,
4430                               bool template_arg_p,
4431                               cp_id_kind *idk)
4432 {
4433   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4434                                        /*decltype*/false, idk);
4435 }
4436
4437 /* Parse an id-expression.
4438
4439    id-expression:
4440      unqualified-id
4441      qualified-id
4442
4443    qualified-id:
4444      :: [opt] nested-name-specifier template [opt] unqualified-id
4445      :: identifier
4446      :: operator-function-id
4447      :: template-id
4448
4449    Return a representation of the unqualified portion of the
4450    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4451    a `::' or nested-name-specifier.
4452
4453    Often, if the id-expression was a qualified-id, the caller will
4454    want to make a SCOPE_REF to represent the qualified-id.  This
4455    function does not do this in order to avoid wastefully creating
4456    SCOPE_REFs when they are not required.
4457
4458    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4459    `template' keyword.
4460
4461    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4462    uninstantiated templates.
4463
4464    If *TEMPLATE_P is non-NULL, it is set to true iff the
4465    `template' keyword is used to explicitly indicate that the entity
4466    named is a template.
4467
4468    If DECLARATOR_P is true, the id-expression is appearing as part of
4469    a declarator, rather than as part of an expression.  */
4470
4471 static tree
4472 cp_parser_id_expression (cp_parser *parser,
4473                          bool template_keyword_p,
4474                          bool check_dependency_p,
4475                          bool *template_p,
4476                          bool declarator_p,
4477                          bool optional_p)
4478 {
4479   bool global_scope_p;
4480   bool nested_name_specifier_p;
4481
4482   /* Assume the `template' keyword was not used.  */
4483   if (template_p)
4484     *template_p = template_keyword_p;
4485
4486   /* Look for the optional `::' operator.  */
4487   global_scope_p
4488     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4489        != NULL_TREE);
4490   /* Look for the optional nested-name-specifier.  */
4491   nested_name_specifier_p
4492     = (cp_parser_nested_name_specifier_opt (parser,
4493                                             /*typename_keyword_p=*/false,
4494                                             check_dependency_p,
4495                                             /*type_p=*/false,
4496                                             declarator_p)
4497        != NULL_TREE);
4498   /* If there is a nested-name-specifier, then we are looking at
4499      the first qualified-id production.  */
4500   if (nested_name_specifier_p)
4501     {
4502       tree saved_scope;
4503       tree saved_object_scope;
4504       tree saved_qualifying_scope;
4505       tree unqualified_id;
4506       bool is_template;
4507
4508       /* See if the next token is the `template' keyword.  */
4509       if (!template_p)
4510         template_p = &is_template;
4511       *template_p = cp_parser_optional_template_keyword (parser);
4512       /* Name lookup we do during the processing of the
4513          unqualified-id might obliterate SCOPE.  */
4514       saved_scope = parser->scope;
4515       saved_object_scope = parser->object_scope;
4516       saved_qualifying_scope = parser->qualifying_scope;
4517       /* Process the final unqualified-id.  */
4518       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4519                                                  check_dependency_p,
4520                                                  declarator_p,
4521                                                  /*optional_p=*/false);
4522       /* Restore the SAVED_SCOPE for our caller.  */
4523       parser->scope = saved_scope;
4524       parser->object_scope = saved_object_scope;
4525       parser->qualifying_scope = saved_qualifying_scope;
4526
4527       return unqualified_id;
4528     }
4529   /* Otherwise, if we are in global scope, then we are looking at one
4530      of the other qualified-id productions.  */
4531   else if (global_scope_p)
4532     {
4533       cp_token *token;
4534       tree id;
4535
4536       /* Peek at the next token.  */
4537       token = cp_lexer_peek_token (parser->lexer);
4538
4539       /* If it's an identifier, and the next token is not a "<", then
4540          we can avoid the template-id case.  This is an optimization
4541          for this common case.  */
4542       if (token->type == CPP_NAME
4543           && !cp_parser_nth_token_starts_template_argument_list_p
4544                (parser, 2))
4545         return cp_parser_identifier (parser);
4546
4547       cp_parser_parse_tentatively (parser);
4548       /* Try a template-id.  */
4549       id = cp_parser_template_id (parser,
4550                                   /*template_keyword_p=*/false,
4551                                   /*check_dependency_p=*/true,
4552                                   none_type,
4553                                   declarator_p);
4554       /* If that worked, we're done.  */
4555       if (cp_parser_parse_definitely (parser))
4556         return id;
4557
4558       /* Peek at the next token.  (Changes in the token buffer may
4559          have invalidated the pointer obtained above.)  */
4560       token = cp_lexer_peek_token (parser->lexer);
4561
4562       switch (token->type)
4563         {
4564         case CPP_NAME:
4565           return cp_parser_identifier (parser);
4566
4567         case CPP_KEYWORD:
4568           if (token->keyword == RID_OPERATOR)
4569             return cp_parser_operator_function_id (parser);
4570           /* Fall through.  */
4571
4572         default:
4573           cp_parser_error (parser, "expected id-expression");
4574           return error_mark_node;
4575         }
4576     }
4577   else
4578     return cp_parser_unqualified_id (parser, template_keyword_p,
4579                                      /*check_dependency_p=*/true,
4580                                      declarator_p,
4581                                      optional_p);
4582 }
4583
4584 /* Parse an unqualified-id.
4585
4586    unqualified-id:
4587      identifier
4588      operator-function-id
4589      conversion-function-id
4590      ~ class-name
4591      template-id
4592
4593    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4594    keyword, in a construct like `A::template ...'.
4595
4596    Returns a representation of unqualified-id.  For the `identifier'
4597    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4598    production a BIT_NOT_EXPR is returned; the operand of the
4599    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4600    other productions, see the documentation accompanying the
4601    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4602    names are looked up in uninstantiated templates.  If DECLARATOR_P
4603    is true, the unqualified-id is appearing as part of a declarator,
4604    rather than as part of an expression.  */
4605
4606 static tree
4607 cp_parser_unqualified_id (cp_parser* parser,
4608                           bool template_keyword_p,
4609                           bool check_dependency_p,
4610                           bool declarator_p,
4611                           bool optional_p)
4612 {
4613   cp_token *token;
4614
4615   /* Peek at the next token.  */
4616   token = cp_lexer_peek_token (parser->lexer);
4617
4618   switch (token->type)
4619     {
4620     case CPP_NAME:
4621       {
4622         tree id;
4623
4624         /* We don't know yet whether or not this will be a
4625            template-id.  */
4626         cp_parser_parse_tentatively (parser);
4627         /* Try a template-id.  */
4628         id = cp_parser_template_id (parser, template_keyword_p,
4629                                     check_dependency_p,
4630                                     none_type,
4631                                     declarator_p);
4632         /* If it worked, we're done.  */
4633         if (cp_parser_parse_definitely (parser))
4634           return id;
4635         /* Otherwise, it's an ordinary identifier.  */
4636         return cp_parser_identifier (parser);
4637       }
4638
4639     case CPP_TEMPLATE_ID:
4640       return cp_parser_template_id (parser, template_keyword_p,
4641                                     check_dependency_p,
4642                                     none_type,
4643                                     declarator_p);
4644
4645     case CPP_COMPL:
4646       {
4647         tree type_decl;
4648         tree qualifying_scope;
4649         tree object_scope;
4650         tree scope;
4651         bool done;
4652
4653         /* Consume the `~' token.  */
4654         cp_lexer_consume_token (parser->lexer);
4655         /* Parse the class-name.  The standard, as written, seems to
4656            say that:
4657
4658              template <typename T> struct S { ~S (); };
4659              template <typename T> S<T>::~S() {}
4660
4661            is invalid, since `~' must be followed by a class-name, but
4662            `S<T>' is dependent, and so not known to be a class.
4663            That's not right; we need to look in uninstantiated
4664            templates.  A further complication arises from:
4665
4666              template <typename T> void f(T t) {
4667                t.T::~T();
4668              }
4669
4670            Here, it is not possible to look up `T' in the scope of `T'
4671            itself.  We must look in both the current scope, and the
4672            scope of the containing complete expression.
4673
4674            Yet another issue is:
4675
4676              struct S {
4677                int S;
4678                ~S();
4679              };
4680
4681              S::~S() {}
4682
4683            The standard does not seem to say that the `S' in `~S'
4684            should refer to the type `S' and not the data member
4685            `S::S'.  */
4686
4687         /* DR 244 says that we look up the name after the "~" in the
4688            same scope as we looked up the qualifying name.  That idea
4689            isn't fully worked out; it's more complicated than that.  */
4690         scope = parser->scope;
4691         object_scope = parser->object_scope;
4692         qualifying_scope = parser->qualifying_scope;
4693
4694         /* Check for invalid scopes.  */
4695         if (scope == error_mark_node)
4696           {
4697             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4698               cp_lexer_consume_token (parser->lexer);
4699             return error_mark_node;
4700           }
4701         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4702           {
4703             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4704               error_at (token->location,
4705                         "scope %qT before %<~%> is not a class-name",
4706                         scope);
4707             cp_parser_simulate_error (parser);
4708             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4709               cp_lexer_consume_token (parser->lexer);
4710             return error_mark_node;
4711           }
4712         gcc_assert (!scope || TYPE_P (scope));
4713
4714         /* If the name is of the form "X::~X" it's OK even if X is a
4715            typedef.  */
4716         token = cp_lexer_peek_token (parser->lexer);
4717         if (scope
4718             && token->type == CPP_NAME
4719             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4720                 != CPP_LESS)
4721             && (token->u.value == TYPE_IDENTIFIER (scope)
4722                 || (CLASS_TYPE_P (scope)
4723                     && constructor_name_p (token->u.value, scope))))
4724           {
4725             cp_lexer_consume_token (parser->lexer);
4726             return build_nt (BIT_NOT_EXPR, scope);
4727           }
4728
4729         /* If there was an explicit qualification (S::~T), first look
4730            in the scope given by the qualification (i.e., S).
4731
4732            Note: in the calls to cp_parser_class_name below we pass
4733            typename_type so that lookup finds the injected-class-name
4734            rather than the constructor.  */
4735         done = false;
4736         type_decl = NULL_TREE;
4737         if (scope)
4738           {
4739             cp_parser_parse_tentatively (parser);
4740             type_decl = cp_parser_class_name (parser,
4741                                               /*typename_keyword_p=*/false,
4742                                               /*template_keyword_p=*/false,
4743                                               typename_type,
4744                                               /*check_dependency=*/false,
4745                                               /*class_head_p=*/false,
4746                                               declarator_p);
4747             if (cp_parser_parse_definitely (parser))
4748               done = true;
4749           }
4750         /* In "N::S::~S", look in "N" as well.  */
4751         if (!done && scope && qualifying_scope)
4752           {
4753             cp_parser_parse_tentatively (parser);
4754             parser->scope = qualifying_scope;
4755             parser->object_scope = NULL_TREE;
4756             parser->qualifying_scope = NULL_TREE;
4757             type_decl
4758               = cp_parser_class_name (parser,
4759                                       /*typename_keyword_p=*/false,
4760                                       /*template_keyword_p=*/false,
4761                                       typename_type,
4762                                       /*check_dependency=*/false,
4763                                       /*class_head_p=*/false,
4764                                       declarator_p);
4765             if (cp_parser_parse_definitely (parser))
4766               done = true;
4767           }
4768         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4769         else if (!done && object_scope)
4770           {
4771             cp_parser_parse_tentatively (parser);
4772             parser->scope = object_scope;
4773             parser->object_scope = NULL_TREE;
4774             parser->qualifying_scope = NULL_TREE;
4775             type_decl
4776               = cp_parser_class_name (parser,
4777                                       /*typename_keyword_p=*/false,
4778                                       /*template_keyword_p=*/false,
4779                                       typename_type,
4780                                       /*check_dependency=*/false,
4781                                       /*class_head_p=*/false,
4782                                       declarator_p);
4783             if (cp_parser_parse_definitely (parser))
4784               done = true;
4785           }
4786         /* Look in the surrounding context.  */
4787         if (!done)
4788           {
4789             parser->scope = NULL_TREE;
4790             parser->object_scope = NULL_TREE;
4791             parser->qualifying_scope = NULL_TREE;
4792             if (processing_template_decl)
4793               cp_parser_parse_tentatively (parser);
4794             type_decl
4795               = cp_parser_class_name (parser,
4796                                       /*typename_keyword_p=*/false,
4797                                       /*template_keyword_p=*/false,
4798                                       typename_type,
4799                                       /*check_dependency=*/false,
4800                                       /*class_head_p=*/false,
4801                                       declarator_p);
4802             if (processing_template_decl
4803                 && ! cp_parser_parse_definitely (parser))
4804               {
4805                 /* We couldn't find a type with this name, so just accept
4806                    it and check for a match at instantiation time.  */
4807                 type_decl = cp_parser_identifier (parser);
4808                 if (type_decl != error_mark_node)
4809                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4810                 return type_decl;
4811               }
4812           }
4813         /* If an error occurred, assume that the name of the
4814            destructor is the same as the name of the qualifying
4815            class.  That allows us to keep parsing after running
4816            into ill-formed destructor names.  */
4817         if (type_decl == error_mark_node && scope)
4818           return build_nt (BIT_NOT_EXPR, scope);
4819         else if (type_decl == error_mark_node)
4820           return error_mark_node;
4821
4822         /* Check that destructor name and scope match.  */
4823         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4824           {
4825             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4826               error_at (token->location,
4827                         "declaration of %<~%T%> as member of %qT",
4828                         type_decl, scope);
4829             cp_parser_simulate_error (parser);
4830             return error_mark_node;
4831           }
4832
4833         /* [class.dtor]
4834
4835            A typedef-name that names a class shall not be used as the
4836            identifier in the declarator for a destructor declaration.  */
4837         if (declarator_p
4838             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4839             && !DECL_SELF_REFERENCE_P (type_decl)
4840             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4841           error_at (token->location,
4842                     "typedef-name %qD used as destructor declarator",
4843                     type_decl);
4844
4845         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4846       }
4847
4848     case CPP_KEYWORD:
4849       if (token->keyword == RID_OPERATOR)
4850         {
4851           tree id;
4852
4853           /* This could be a template-id, so we try that first.  */
4854           cp_parser_parse_tentatively (parser);
4855           /* Try a template-id.  */
4856           id = cp_parser_template_id (parser, template_keyword_p,
4857                                       /*check_dependency_p=*/true,
4858                                       none_type,
4859                                       declarator_p);
4860           /* If that worked, we're done.  */
4861           if (cp_parser_parse_definitely (parser))
4862             return id;
4863           /* We still don't know whether we're looking at an
4864              operator-function-id or a conversion-function-id.  */
4865           cp_parser_parse_tentatively (parser);
4866           /* Try an operator-function-id.  */
4867           id = cp_parser_operator_function_id (parser);
4868           /* If that didn't work, try a conversion-function-id.  */
4869           if (!cp_parser_parse_definitely (parser))
4870             id = cp_parser_conversion_function_id (parser);
4871           else if (UDLIT_OPER_P (id))
4872             {
4873               /* 17.6.3.3.5  */
4874               const char *name = UDLIT_OP_SUFFIX (id);
4875               if (name[0] != '_' && !in_system_header)
4876                 warning (0, "literal operator suffixes not preceded by %<_%>"
4877                             " are reserved for future standardization");
4878             }
4879
4880           return id;
4881         }
4882       /* Fall through.  */
4883
4884     default:
4885       if (optional_p)
4886         return NULL_TREE;
4887       cp_parser_error (parser, "expected unqualified-id");
4888       return error_mark_node;
4889     }
4890 }
4891
4892 /* Parse an (optional) nested-name-specifier.
4893
4894    nested-name-specifier: [C++98]
4895      class-or-namespace-name :: nested-name-specifier [opt]
4896      class-or-namespace-name :: template nested-name-specifier [opt]
4897
4898    nested-name-specifier: [C++0x]
4899      type-name ::
4900      namespace-name ::
4901      nested-name-specifier identifier ::
4902      nested-name-specifier template [opt] simple-template-id ::
4903
4904    PARSER->SCOPE should be set appropriately before this function is
4905    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4906    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4907    in name lookups.
4908
4909    Sets PARSER->SCOPE to the class (TYPE) or namespace
4910    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4911    it unchanged if there is no nested-name-specifier.  Returns the new
4912    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4913
4914    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4915    part of a declaration and/or decl-specifier.  */
4916
4917 static tree
4918 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4919                                      bool typename_keyword_p,
4920                                      bool check_dependency_p,
4921                                      bool type_p,
4922                                      bool is_declaration)
4923 {
4924   bool success = false;
4925   cp_token_position start = 0;
4926   cp_token *token;
4927
4928   /* Remember where the nested-name-specifier starts.  */
4929   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4930     {
4931       start = cp_lexer_token_position (parser->lexer, false);
4932       push_deferring_access_checks (dk_deferred);
4933     }
4934
4935   while (true)
4936     {
4937       tree new_scope;
4938       tree old_scope;
4939       tree saved_qualifying_scope;
4940       bool template_keyword_p;
4941
4942       /* Spot cases that cannot be the beginning of a
4943          nested-name-specifier.  */
4944       token = cp_lexer_peek_token (parser->lexer);
4945
4946       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4947          the already parsed nested-name-specifier.  */
4948       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4949         {
4950           /* Grab the nested-name-specifier and continue the loop.  */
4951           cp_parser_pre_parsed_nested_name_specifier (parser);
4952           /* If we originally encountered this nested-name-specifier
4953              with IS_DECLARATION set to false, we will not have
4954              resolved TYPENAME_TYPEs, so we must do so here.  */
4955           if (is_declaration
4956               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4957             {
4958               new_scope = resolve_typename_type (parser->scope,
4959                                                  /*only_current_p=*/false);
4960               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4961                 parser->scope = new_scope;
4962             }
4963           success = true;
4964           continue;
4965         }
4966
4967       /* Spot cases that cannot be the beginning of a
4968          nested-name-specifier.  On the second and subsequent times
4969          through the loop, we look for the `template' keyword.  */
4970       if (success && token->keyword == RID_TEMPLATE)
4971         ;
4972       /* A template-id can start a nested-name-specifier.  */
4973       else if (token->type == CPP_TEMPLATE_ID)
4974         ;
4975       /* DR 743: decltype can be used in a nested-name-specifier.  */
4976       else if (token_is_decltype (token))
4977         ;
4978       else
4979         {
4980           /* If the next token is not an identifier, then it is
4981              definitely not a type-name or namespace-name.  */
4982           if (token->type != CPP_NAME)
4983             break;
4984           /* If the following token is neither a `<' (to begin a
4985              template-id), nor a `::', then we are not looking at a
4986              nested-name-specifier.  */
4987           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4988
4989           if (token->type == CPP_COLON
4990               && parser->colon_corrects_to_scope_p
4991               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4992             {
4993               error_at (token->location,
4994                         "found %<:%> in nested-name-specifier, expected %<::%>");
4995               token->type = CPP_SCOPE;
4996             }
4997
4998           if (token->type != CPP_SCOPE
4999               && !cp_parser_nth_token_starts_template_argument_list_p
5000                   (parser, 2))
5001             break;
5002         }
5003
5004       /* The nested-name-specifier is optional, so we parse
5005          tentatively.  */
5006       cp_parser_parse_tentatively (parser);
5007
5008       /* Look for the optional `template' keyword, if this isn't the
5009          first time through the loop.  */
5010       if (success)
5011         template_keyword_p = cp_parser_optional_template_keyword (parser);
5012       else
5013         template_keyword_p = false;
5014
5015       /* Save the old scope since the name lookup we are about to do
5016          might destroy it.  */
5017       old_scope = parser->scope;
5018       saved_qualifying_scope = parser->qualifying_scope;
5019       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5020          look up names in "X<T>::I" in order to determine that "Y" is
5021          a template.  So, if we have a typename at this point, we make
5022          an effort to look through it.  */
5023       if (is_declaration
5024           && !typename_keyword_p
5025           && parser->scope
5026           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5027         parser->scope = resolve_typename_type (parser->scope,
5028                                                /*only_current_p=*/false);
5029       /* Parse the qualifying entity.  */
5030       new_scope
5031         = cp_parser_qualifying_entity (parser,
5032                                        typename_keyword_p,
5033                                        template_keyword_p,
5034                                        check_dependency_p,
5035                                        type_p,
5036                                        is_declaration);
5037       /* Look for the `::' token.  */
5038       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5039
5040       /* If we found what we wanted, we keep going; otherwise, we're
5041          done.  */
5042       if (!cp_parser_parse_definitely (parser))
5043         {
5044           bool error_p = false;
5045
5046           /* Restore the OLD_SCOPE since it was valid before the
5047              failed attempt at finding the last
5048              class-or-namespace-name.  */
5049           parser->scope = old_scope;
5050           parser->qualifying_scope = saved_qualifying_scope;
5051
5052           /* If the next token is a decltype, and the one after that is a
5053              `::', then the decltype has failed to resolve to a class or
5054              enumeration type.  Give this error even when parsing
5055              tentatively since it can't possibly be valid--and we're going
5056              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5057              won't get another chance.*/
5058           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5059               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5060                   == CPP_SCOPE))
5061             {
5062               token = cp_lexer_consume_token (parser->lexer);
5063               error_at (token->location, "decltype evaluates to %qT, "
5064                         "which is not a class or enumeration type",
5065                         token->u.value);
5066               parser->scope = error_mark_node;
5067               error_p = true;
5068               /* As below.  */
5069               success = true;
5070               cp_lexer_consume_token (parser->lexer);
5071             }
5072
5073           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5074             break;
5075           /* If the next token is an identifier, and the one after
5076              that is a `::', then any valid interpretation would have
5077              found a class-or-namespace-name.  */
5078           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5079                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5080                      == CPP_SCOPE)
5081                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5082                      != CPP_COMPL))
5083             {
5084               token = cp_lexer_consume_token (parser->lexer);
5085               if (!error_p)
5086                 {
5087                   if (!token->ambiguous_p)
5088                     {
5089                       tree decl;
5090                       tree ambiguous_decls;
5091
5092                       decl = cp_parser_lookup_name (parser, token->u.value,
5093                                                     none_type,
5094                                                     /*is_template=*/false,
5095                                                     /*is_namespace=*/false,
5096                                                     /*check_dependency=*/true,
5097                                                     &ambiguous_decls,
5098                                                     token->location);
5099                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5100                         error_at (token->location,
5101                                   "%qD used without template parameters",
5102                                   decl);
5103                       else if (ambiguous_decls)
5104                         {
5105                           error_at (token->location,
5106                                     "reference to %qD is ambiguous",
5107                                     token->u.value);
5108                           print_candidates (ambiguous_decls);
5109                           decl = error_mark_node;
5110                         }
5111                       else
5112                         {
5113                           if (cxx_dialect != cxx98)
5114                             cp_parser_name_lookup_error
5115                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5116                              token->location);
5117                           else
5118                             cp_parser_name_lookup_error
5119                             (parser, token->u.value, decl, NLE_CXX98,
5120                              token->location);
5121                         }
5122                     }
5123                   parser->scope = error_mark_node;
5124                   error_p = true;
5125                   /* Treat this as a successful nested-name-specifier
5126                      due to:
5127
5128                      [basic.lookup.qual]
5129
5130                      If the name found is not a class-name (clause
5131                      _class_) or namespace-name (_namespace.def_), the
5132                      program is ill-formed.  */
5133                   success = true;
5134                 }
5135               cp_lexer_consume_token (parser->lexer);
5136             }
5137           break;
5138         }
5139       /* We've found one valid nested-name-specifier.  */
5140       success = true;
5141       /* Name lookup always gives us a DECL.  */
5142       if (TREE_CODE (new_scope) == TYPE_DECL)
5143         new_scope = TREE_TYPE (new_scope);
5144       /* Uses of "template" must be followed by actual templates.  */
5145       if (template_keyword_p
5146           && !(CLASS_TYPE_P (new_scope)
5147                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5148                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5149                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5150           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5151                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5152                    == TEMPLATE_ID_EXPR)))
5153         permerror (input_location, TYPE_P (new_scope)
5154                    ? G_("%qT is not a template")
5155                    : G_("%qD is not a template"),
5156                    new_scope);
5157       /* If it is a class scope, try to complete it; we are about to
5158          be looking up names inside the class.  */
5159       if (TYPE_P (new_scope)
5160           /* Since checking types for dependency can be expensive,
5161              avoid doing it if the type is already complete.  */
5162           && !COMPLETE_TYPE_P (new_scope)
5163           /* Do not try to complete dependent types.  */
5164           && !dependent_type_p (new_scope))
5165         {
5166           new_scope = complete_type (new_scope);
5167           /* If it is a typedef to current class, use the current
5168              class instead, as the typedef won't have any names inside
5169              it yet.  */
5170           if (!COMPLETE_TYPE_P (new_scope)
5171               && currently_open_class (new_scope))
5172             new_scope = TYPE_MAIN_VARIANT (new_scope);
5173         }
5174       /* Make sure we look in the right scope the next time through
5175          the loop.  */
5176       parser->scope = new_scope;
5177     }
5178
5179   /* If parsing tentatively, replace the sequence of tokens that makes
5180      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5181      token.  That way, should we re-parse the token stream, we will
5182      not have to repeat the effort required to do the parse, nor will
5183      we issue duplicate error messages.  */
5184   if (success && start)
5185     {
5186       cp_token *token;
5187
5188       token = cp_lexer_token_at (parser->lexer, start);
5189       /* Reset the contents of the START token.  */
5190       token->type = CPP_NESTED_NAME_SPECIFIER;
5191       /* Retrieve any deferred checks.  Do not pop this access checks yet
5192          so the memory will not be reclaimed during token replacing below.  */
5193       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5194       token->u.tree_check_value->value = parser->scope;
5195       token->u.tree_check_value->checks = get_deferred_access_checks ();
5196       token->u.tree_check_value->qualifying_scope =
5197         parser->qualifying_scope;
5198       token->keyword = RID_MAX;
5199
5200       /* Purge all subsequent tokens.  */
5201       cp_lexer_purge_tokens_after (parser->lexer, start);
5202     }
5203
5204   if (start)
5205     pop_to_parent_deferring_access_checks ();
5206
5207   return success ? parser->scope : NULL_TREE;
5208 }
5209
5210 /* Parse a nested-name-specifier.  See
5211    cp_parser_nested_name_specifier_opt for details.  This function
5212    behaves identically, except that it will an issue an error if no
5213    nested-name-specifier is present.  */
5214
5215 static tree
5216 cp_parser_nested_name_specifier (cp_parser *parser,
5217                                  bool typename_keyword_p,
5218                                  bool check_dependency_p,
5219                                  bool type_p,
5220                                  bool is_declaration)
5221 {
5222   tree scope;
5223
5224   /* Look for the nested-name-specifier.  */
5225   scope = cp_parser_nested_name_specifier_opt (parser,
5226                                                typename_keyword_p,
5227                                                check_dependency_p,
5228                                                type_p,
5229                                                is_declaration);
5230   /* If it was not present, issue an error message.  */
5231   if (!scope)
5232     {
5233       cp_parser_error (parser, "expected nested-name-specifier");
5234       parser->scope = NULL_TREE;
5235     }
5236
5237   return scope;
5238 }
5239
5240 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5241    this is either a class-name or a namespace-name (which corresponds
5242    to the class-or-namespace-name production in the grammar). For
5243    C++0x, it can also be a type-name that refers to an enumeration
5244    type or a simple-template-id.
5245
5246    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5247    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5248    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5249    TYPE_P is TRUE iff the next name should be taken as a class-name,
5250    even the same name is declared to be another entity in the same
5251    scope.
5252
5253    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5254    specified by the class-or-namespace-name.  If neither is found the
5255    ERROR_MARK_NODE is returned.  */
5256
5257 static tree
5258 cp_parser_qualifying_entity (cp_parser *parser,
5259                              bool typename_keyword_p,
5260                              bool template_keyword_p,
5261                              bool check_dependency_p,
5262                              bool type_p,
5263                              bool is_declaration)
5264 {
5265   tree saved_scope;
5266   tree saved_qualifying_scope;
5267   tree saved_object_scope;
5268   tree scope;
5269   bool only_class_p;
5270   bool successful_parse_p;
5271
5272   /* DR 743: decltype can appear in a nested-name-specifier.  */
5273   if (cp_lexer_next_token_is_decltype (parser->lexer))
5274     {
5275       scope = cp_parser_decltype (parser);
5276       if (TREE_CODE (scope) != ENUMERAL_TYPE
5277           && !MAYBE_CLASS_TYPE_P (scope))
5278         {
5279           cp_parser_simulate_error (parser);
5280           return error_mark_node;
5281         }
5282       if (TYPE_NAME (scope))
5283         scope = TYPE_NAME (scope);
5284       return scope;
5285     }
5286
5287   /* Before we try to parse the class-name, we must save away the
5288      current PARSER->SCOPE since cp_parser_class_name will destroy
5289      it.  */
5290   saved_scope = parser->scope;
5291   saved_qualifying_scope = parser->qualifying_scope;
5292   saved_object_scope = parser->object_scope;
5293   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5294      there is no need to look for a namespace-name.  */
5295   only_class_p = template_keyword_p 
5296     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5297   if (!only_class_p)
5298     cp_parser_parse_tentatively (parser);
5299   scope = cp_parser_class_name (parser,
5300                                 typename_keyword_p,
5301                                 template_keyword_p,
5302                                 type_p ? class_type : none_type,
5303                                 check_dependency_p,
5304                                 /*class_head_p=*/false,
5305                                 is_declaration);
5306   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5307   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5308   if (!only_class_p 
5309       && cxx_dialect != cxx98
5310       && !successful_parse_p)
5311     {
5312       /* Restore the saved scope.  */
5313       parser->scope = saved_scope;
5314       parser->qualifying_scope = saved_qualifying_scope;
5315       parser->object_scope = saved_object_scope;
5316
5317       /* Parse tentatively.  */
5318       cp_parser_parse_tentatively (parser);
5319      
5320       /* Parse a type-name  */
5321       scope = cp_parser_type_name (parser);
5322
5323       /* "If the name found does not designate a namespace or a class,
5324          enumeration, or dependent type, the program is ill-formed."
5325
5326          We cover classes and dependent types above and namespaces below,
5327          so this code is only looking for enums.  */
5328       if (!scope || TREE_CODE (scope) != TYPE_DECL
5329           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5330         cp_parser_simulate_error (parser);
5331
5332       successful_parse_p = cp_parser_parse_definitely (parser);
5333     }
5334   /* If that didn't work, try for a namespace-name.  */
5335   if (!only_class_p && !successful_parse_p)
5336     {
5337       /* Restore the saved scope.  */
5338       parser->scope = saved_scope;
5339       parser->qualifying_scope = saved_qualifying_scope;
5340       parser->object_scope = saved_object_scope;
5341       /* If we are not looking at an identifier followed by the scope
5342          resolution operator, then this is not part of a
5343          nested-name-specifier.  (Note that this function is only used
5344          to parse the components of a nested-name-specifier.)  */
5345       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5346           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5347         return error_mark_node;
5348       scope = cp_parser_namespace_name (parser);
5349     }
5350
5351   return scope;
5352 }
5353
5354 /* Parse a postfix-expression.
5355
5356    postfix-expression:
5357      primary-expression
5358      postfix-expression [ expression ]
5359      postfix-expression ( expression-list [opt] )
5360      simple-type-specifier ( expression-list [opt] )
5361      typename :: [opt] nested-name-specifier identifier
5362        ( expression-list [opt] )
5363      typename :: [opt] nested-name-specifier template [opt] template-id
5364        ( expression-list [opt] )
5365      postfix-expression . template [opt] id-expression
5366      postfix-expression -> template [opt] id-expression
5367      postfix-expression . pseudo-destructor-name
5368      postfix-expression -> pseudo-destructor-name
5369      postfix-expression ++
5370      postfix-expression --
5371      dynamic_cast < type-id > ( expression )
5372      static_cast < type-id > ( expression )
5373      reinterpret_cast < type-id > ( expression )
5374      const_cast < type-id > ( expression )
5375      typeid ( expression )
5376      typeid ( type-id )
5377
5378    GNU Extension:
5379
5380    postfix-expression:
5381      ( type-id ) { initializer-list , [opt] }
5382
5383    This extension is a GNU version of the C99 compound-literal
5384    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5385    but they are essentially the same concept.)
5386
5387    If ADDRESS_P is true, the postfix expression is the operand of the
5388    `&' operator.  CAST_P is true if this expression is the target of a
5389    cast.
5390
5391    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5392    class member access expressions [expr.ref].
5393
5394    Returns a representation of the expression.  */
5395
5396 static tree
5397 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5398                               bool member_access_only_p, bool decltype_p,
5399                               cp_id_kind * pidk_return)
5400 {
5401   cp_token *token;
5402   enum rid keyword;
5403   cp_id_kind idk = CP_ID_KIND_NONE;
5404   tree postfix_expression = NULL_TREE;
5405   bool is_member_access = false;
5406
5407   /* Peek at the next token.  */
5408   token = cp_lexer_peek_token (parser->lexer);
5409   /* Some of the productions are determined by keywords.  */
5410   keyword = token->keyword;
5411   switch (keyword)
5412     {
5413     case RID_DYNCAST:
5414     case RID_STATCAST:
5415     case RID_REINTCAST:
5416     case RID_CONSTCAST:
5417       {
5418         tree type;
5419         tree expression;
5420         const char *saved_message;
5421
5422         /* All of these can be handled in the same way from the point
5423            of view of parsing.  Begin by consuming the token
5424            identifying the cast.  */
5425         cp_lexer_consume_token (parser->lexer);
5426
5427         /* New types cannot be defined in the cast.  */
5428         saved_message = parser->type_definition_forbidden_message;
5429         parser->type_definition_forbidden_message
5430           = G_("types may not be defined in casts");
5431
5432         /* Look for the opening `<'.  */
5433         cp_parser_require (parser, CPP_LESS, RT_LESS);
5434         /* Parse the type to which we are casting.  */
5435         type = cp_parser_type_id (parser);
5436         /* Look for the closing `>'.  */
5437         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5438         /* Restore the old message.  */
5439         parser->type_definition_forbidden_message = saved_message;
5440
5441         bool saved_greater_than_is_operator_p
5442           = parser->greater_than_is_operator_p;
5443         parser->greater_than_is_operator_p = true;
5444
5445         /* And the expression which is being cast.  */
5446         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5447         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5448         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5449
5450         parser->greater_than_is_operator_p
5451           = saved_greater_than_is_operator_p;
5452
5453         /* Only type conversions to integral or enumeration types
5454            can be used in constant-expressions.  */
5455         if (!cast_valid_in_integral_constant_expression_p (type)
5456             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5457           return error_mark_node;
5458
5459         switch (keyword)
5460           {
5461           case RID_DYNCAST:
5462             postfix_expression
5463               = build_dynamic_cast (type, expression, tf_warning_or_error);
5464             break;
5465           case RID_STATCAST:
5466             postfix_expression
5467               = build_static_cast (type, expression, tf_warning_or_error);
5468             break;
5469           case RID_REINTCAST:
5470             postfix_expression
5471               = build_reinterpret_cast (type, expression, 
5472                                         tf_warning_or_error);
5473             break;
5474           case RID_CONSTCAST:
5475             postfix_expression
5476               = build_const_cast (type, expression, tf_warning_or_error);
5477             break;
5478           default:
5479             gcc_unreachable ();
5480           }
5481       }
5482       break;
5483
5484     case RID_TYPEID:
5485       {
5486         tree type;
5487         const char *saved_message;
5488         bool saved_in_type_id_in_expr_p;
5489
5490         /* Consume the `typeid' token.  */
5491         cp_lexer_consume_token (parser->lexer);
5492         /* Look for the `(' token.  */
5493         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5494         /* Types cannot be defined in a `typeid' expression.  */
5495         saved_message = parser->type_definition_forbidden_message;
5496         parser->type_definition_forbidden_message
5497           = G_("types may not be defined in a %<typeid%> expression");
5498         /* We can't be sure yet whether we're looking at a type-id or an
5499            expression.  */
5500         cp_parser_parse_tentatively (parser);
5501         /* Try a type-id first.  */
5502         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5503         parser->in_type_id_in_expr_p = true;
5504         type = cp_parser_type_id (parser);
5505         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5506         /* Look for the `)' token.  Otherwise, we can't be sure that
5507            we're not looking at an expression: consider `typeid (int
5508            (3))', for example.  */
5509         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5510         /* If all went well, simply lookup the type-id.  */
5511         if (cp_parser_parse_definitely (parser))
5512           postfix_expression = get_typeid (type, tf_warning_or_error);
5513         /* Otherwise, fall back to the expression variant.  */
5514         else
5515           {
5516             tree expression;
5517
5518             /* Look for an expression.  */
5519             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5520             /* Compute its typeid.  */
5521             postfix_expression = build_typeid (expression, tf_warning_or_error);
5522             /* Look for the `)' token.  */
5523             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5524           }
5525         /* Restore the saved message.  */
5526         parser->type_definition_forbidden_message = saved_message;
5527         /* `typeid' may not appear in an integral constant expression.  */
5528         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5529           return error_mark_node;
5530       }
5531       break;
5532
5533     case RID_TYPENAME:
5534       {
5535         tree type;
5536         /* The syntax permitted here is the same permitted for an
5537            elaborated-type-specifier.  */
5538         type = cp_parser_elaborated_type_specifier (parser,
5539                                                     /*is_friend=*/false,
5540                                                     /*is_declaration=*/false);
5541         postfix_expression = cp_parser_functional_cast (parser, type);
5542       }
5543       break;
5544
5545     case RID_BUILTIN_SHUFFLE:
5546       {
5547         vec<tree, va_gc> *vec;
5548         unsigned int i;
5549         tree p;
5550         location_t loc = token->location;
5551
5552         cp_lexer_consume_token (parser->lexer);
5553         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5554                     /*cast_p=*/false, /*allow_expansion_p=*/true,
5555                     /*non_constant_p=*/NULL);
5556         if (vec == NULL)
5557           return error_mark_node;
5558
5559         FOR_EACH_VEC_ELT (*vec, i, p)
5560           mark_exp_read (p);
5561
5562         if (vec->length () == 2)
5563           return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]); 
5564         else if (vec->length () == 3)
5565           return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5566         else
5567         {
5568           error_at (loc, "wrong number of arguments to "
5569               "%<__builtin_shuffle%>");
5570           return error_mark_node;
5571         }
5572         break;
5573       }
5574
5575     default:
5576       {
5577         tree type;
5578
5579         /* If the next thing is a simple-type-specifier, we may be
5580            looking at a functional cast.  We could also be looking at
5581            an id-expression.  So, we try the functional cast, and if
5582            that doesn't work we fall back to the primary-expression.  */
5583         cp_parser_parse_tentatively (parser);
5584         /* Look for the simple-type-specifier.  */
5585         type = cp_parser_simple_type_specifier (parser,
5586                                                 /*decl_specs=*/NULL,
5587                                                 CP_PARSER_FLAGS_NONE);
5588         /* Parse the cast itself.  */
5589         if (!cp_parser_error_occurred (parser))
5590           postfix_expression
5591             = cp_parser_functional_cast (parser, type);
5592         /* If that worked, we're done.  */
5593         if (cp_parser_parse_definitely (parser))
5594           break;
5595
5596         /* If the functional-cast didn't work out, try a
5597            compound-literal.  */
5598         if (cp_parser_allow_gnu_extensions_p (parser)
5599             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5600           {
5601             vec<constructor_elt, va_gc> *initializer_list = NULL;
5602             bool saved_in_type_id_in_expr_p;
5603
5604             cp_parser_parse_tentatively (parser);
5605             /* Consume the `('.  */
5606             cp_lexer_consume_token (parser->lexer);
5607             /* Parse the type.  */
5608             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5609             parser->in_type_id_in_expr_p = true;
5610             type = cp_parser_type_id (parser);
5611             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5612             /* Look for the `)'.  */
5613             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5614             /* Look for the `{'.  */
5615             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5616             /* If things aren't going well, there's no need to
5617                keep going.  */
5618             if (!cp_parser_error_occurred (parser))
5619               {
5620                 bool non_constant_p;
5621                 /* Parse the initializer-list.  */
5622                 initializer_list
5623                   = cp_parser_initializer_list (parser, &non_constant_p);
5624                 /* Allow a trailing `,'.  */
5625                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5626                   cp_lexer_consume_token (parser->lexer);
5627                 /* Look for the final `}'.  */
5628                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5629               }
5630             /* If that worked, we're definitely looking at a
5631                compound-literal expression.  */
5632             if (cp_parser_parse_definitely (parser))
5633               {
5634                 /* Warn the user that a compound literal is not
5635                    allowed in standard C++.  */
5636                 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5637                 /* For simplicity, we disallow compound literals in
5638                    constant-expressions.  We could
5639                    allow compound literals of integer type, whose
5640                    initializer was a constant, in constant
5641                    expressions.  Permitting that usage, as a further
5642                    extension, would not change the meaning of any
5643                    currently accepted programs.  (Of course, as
5644                    compound literals are not part of ISO C++, the
5645                    standard has nothing to say.)  */
5646                 if (cp_parser_non_integral_constant_expression (parser,
5647                                                                 NIC_NCC))
5648                   {
5649                     postfix_expression = error_mark_node;
5650                     break;
5651                   }
5652                 /* Form the representation of the compound-literal.  */
5653                 postfix_expression
5654                   = (finish_compound_literal
5655                      (type, build_constructor (init_list_type_node,
5656                                                initializer_list),
5657                       tf_warning_or_error));
5658                 break;
5659               }
5660           }
5661
5662         /* It must be a primary-expression.  */
5663         postfix_expression
5664           = cp_parser_primary_expression (parser, address_p, cast_p,
5665                                           /*template_arg_p=*/false,
5666                                           decltype_p,
5667                                           &idk);
5668       }
5669       break;
5670     }
5671
5672   /* Note that we don't need to worry about calling build_cplus_new on a
5673      class-valued CALL_EXPR in decltype when it isn't the end of the
5674      postfix-expression; unary_complex_lvalue will take care of that for
5675      all these cases.  */
5676
5677   /* Keep looping until the postfix-expression is complete.  */
5678   while (true)
5679     {
5680       if (idk == CP_ID_KIND_UNQUALIFIED
5681           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5682           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5683         /* It is not a Koenig lookup function call.  */
5684         postfix_expression
5685           = unqualified_name_lookup_error (postfix_expression);
5686
5687       /* Peek at the next token.  */
5688       token = cp_lexer_peek_token (parser->lexer);
5689
5690       switch (token->type)
5691         {
5692         case CPP_OPEN_SQUARE:
5693           if (cp_next_tokens_can_be_std_attribute_p (parser))
5694             {
5695               cp_parser_error (parser,
5696                                "two consecutive %<[%> shall "
5697                                "only introduce an attribute");
5698               return error_mark_node;
5699             }
5700           postfix_expression
5701             = cp_parser_postfix_open_square_expression (parser,
5702                                                         postfix_expression,
5703                                                         false,
5704                                                         decltype_p);
5705           idk = CP_ID_KIND_NONE;
5706           is_member_access = false;
5707           break;
5708
5709         case CPP_OPEN_PAREN:
5710           /* postfix-expression ( expression-list [opt] ) */
5711           {
5712             bool koenig_p;
5713             bool is_builtin_constant_p;
5714             bool saved_integral_constant_expression_p = false;
5715             bool saved_non_integral_constant_expression_p = false;
5716             tsubst_flags_t complain = complain_flags (decltype_p);
5717             vec<tree, va_gc> *args;
5718
5719             is_member_access = false;
5720
5721             is_builtin_constant_p
5722               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5723             if (is_builtin_constant_p)
5724               {
5725                 /* The whole point of __builtin_constant_p is to allow
5726                    non-constant expressions to appear as arguments.  */
5727                 saved_integral_constant_expression_p
5728                   = parser->integral_constant_expression_p;
5729                 saved_non_integral_constant_expression_p
5730                   = parser->non_integral_constant_expression_p;
5731                 parser->integral_constant_expression_p = false;
5732               }
5733             args = (cp_parser_parenthesized_expression_list
5734                     (parser, non_attr,
5735                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5736                      /*non_constant_p=*/NULL));
5737             if (is_builtin_constant_p)
5738               {
5739                 parser->integral_constant_expression_p
5740                   = saved_integral_constant_expression_p;
5741                 parser->non_integral_constant_expression_p
5742                   = saved_non_integral_constant_expression_p;
5743               }
5744
5745             if (args == NULL)
5746               {
5747                 postfix_expression = error_mark_node;
5748                 break;
5749               }
5750
5751             /* Function calls are not permitted in
5752                constant-expressions.  */
5753             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5754                 && cp_parser_non_integral_constant_expression (parser,
5755                                                                NIC_FUNC_CALL))
5756               {
5757                 postfix_expression = error_mark_node;
5758                 release_tree_vector (args);
5759                 break;
5760               }
5761
5762             koenig_p = false;
5763             if (idk == CP_ID_KIND_UNQUALIFIED
5764                 || idk == CP_ID_KIND_TEMPLATE_ID)
5765               {
5766                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5767                   {
5768                     if (!args->is_empty ())
5769                       {
5770                         koenig_p = true;
5771                         if (!any_type_dependent_arguments_p (args))
5772                           postfix_expression
5773                             = perform_koenig_lookup (postfix_expression, args,
5774                                                      /*include_std=*/false,
5775                                                      complain);
5776                       }
5777                     else
5778                       postfix_expression
5779                         = unqualified_fn_lookup_error (postfix_expression);
5780                   }
5781                 /* We do not perform argument-dependent lookup if
5782                    normal lookup finds a non-function, in accordance
5783                    with the expected resolution of DR 218.  */
5784                 else if (!args->is_empty ()
5785                          && is_overloaded_fn (postfix_expression))
5786                   {
5787                     tree fn = get_first_fn (postfix_expression);
5788                     fn = STRIP_TEMPLATE (fn);
5789
5790                     /* Do not do argument dependent lookup if regular
5791                        lookup finds a member function or a block-scope
5792                        function declaration.  [basic.lookup.argdep]/3  */
5793                     if (!DECL_FUNCTION_MEMBER_P (fn)
5794                         && !DECL_LOCAL_FUNCTION_P (fn))
5795                       {
5796                         koenig_p = true;
5797                         if (!any_type_dependent_arguments_p (args))
5798                           postfix_expression
5799                             = perform_koenig_lookup (postfix_expression, args,
5800                                                      /*include_std=*/false,
5801                                                      complain);
5802                       }
5803                   }
5804               }
5805
5806             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5807               {
5808                 tree instance = TREE_OPERAND (postfix_expression, 0);
5809                 tree fn = TREE_OPERAND (postfix_expression, 1);
5810
5811                 if (processing_template_decl
5812                     && (type_dependent_expression_p (instance)
5813                         || (!BASELINK_P (fn)
5814                             && TREE_CODE (fn) != FIELD_DECL)
5815                         || type_dependent_expression_p (fn)
5816                         || any_type_dependent_arguments_p (args)))
5817                   {
5818                     postfix_expression
5819                       = build_nt_call_vec (postfix_expression, args);
5820                     release_tree_vector (args);
5821                     break;
5822                   }
5823
5824                 if (BASELINK_P (fn))
5825                   {
5826                   postfix_expression
5827                     = (build_new_method_call
5828                        (instance, fn, &args, NULL_TREE,
5829                         (idk == CP_ID_KIND_QUALIFIED
5830                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5831                          : LOOKUP_NORMAL),
5832                         /*fn_p=*/NULL,
5833                         complain));
5834                   }
5835                 else
5836                   postfix_expression
5837                     = finish_call_expr (postfix_expression, &args,
5838                                         /*disallow_virtual=*/false,
5839                                         /*koenig_p=*/false,
5840                                         complain);
5841               }
5842             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5843                      || TREE_CODE (postfix_expression) == MEMBER_REF
5844                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5845               postfix_expression = (build_offset_ref_call_from_tree
5846                                     (postfix_expression, &args,
5847                                      complain));
5848             else if (idk == CP_ID_KIND_QUALIFIED)
5849               /* A call to a static class member, or a namespace-scope
5850                  function.  */
5851               postfix_expression
5852                 = finish_call_expr (postfix_expression, &args,
5853                                     /*disallow_virtual=*/true,
5854                                     koenig_p,
5855                                     complain);
5856             else
5857               /* All other function calls.  */
5858               postfix_expression
5859                 = finish_call_expr (postfix_expression, &args,
5860                                     /*disallow_virtual=*/false,
5861                                     koenig_p,
5862                                     complain);
5863
5864             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5865             idk = CP_ID_KIND_NONE;
5866
5867             release_tree_vector (args);
5868           }
5869           break;
5870
5871         case CPP_DOT:
5872         case CPP_DEREF:
5873           /* postfix-expression . template [opt] id-expression
5874              postfix-expression . pseudo-destructor-name
5875              postfix-expression -> template [opt] id-expression
5876              postfix-expression -> pseudo-destructor-name */
5877
5878           /* Consume the `.' or `->' operator.  */
5879           cp_lexer_consume_token (parser->lexer);
5880
5881           postfix_expression
5882             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5883                                                       postfix_expression,
5884                                                       false, &idk,
5885                                                       token->location);
5886
5887           is_member_access = true;
5888           break;
5889
5890         case CPP_PLUS_PLUS:
5891           /* postfix-expression ++  */
5892           /* Consume the `++' token.  */
5893           cp_lexer_consume_token (parser->lexer);
5894           /* Generate a representation for the complete expression.  */
5895           postfix_expression
5896             = finish_increment_expr (postfix_expression,
5897                                      POSTINCREMENT_EXPR);
5898           /* Increments may not appear in constant-expressions.  */
5899           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5900             postfix_expression = error_mark_node;
5901           idk = CP_ID_KIND_NONE;
5902           is_member_access = false;
5903           break;
5904
5905         case CPP_MINUS_MINUS:
5906           /* postfix-expression -- */
5907           /* Consume the `--' token.  */
5908           cp_lexer_consume_token (parser->lexer);
5909           /* Generate a representation for the complete expression.  */
5910           postfix_expression
5911             = finish_increment_expr (postfix_expression,
5912                                      POSTDECREMENT_EXPR);
5913           /* Decrements may not appear in constant-expressions.  */
5914           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5915             postfix_expression = error_mark_node;
5916           idk = CP_ID_KIND_NONE;
5917           is_member_access = false;
5918           break;
5919
5920         default:
5921           if (pidk_return != NULL)
5922             * pidk_return = idk;
5923           if (member_access_only_p)
5924             return is_member_access? postfix_expression : error_mark_node;
5925           else
5926             return postfix_expression;
5927         }
5928     }
5929
5930   /* We should never get here.  */
5931   gcc_unreachable ();
5932   return error_mark_node;
5933 }
5934
5935 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5936    by cp_parser_builtin_offsetof.  We're looking for
5937
5938      postfix-expression [ expression ]
5939      postfix-expression [ braced-init-list ] (C++11)
5940
5941    FOR_OFFSETOF is set if we're being called in that context, which
5942    changes how we deal with integer constant expressions.  */
5943
5944 static tree
5945 cp_parser_postfix_open_square_expression (cp_parser *parser,
5946                                           tree postfix_expression,
5947                                           bool for_offsetof,
5948                                           bool decltype_p)
5949 {
5950   tree index;
5951   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5952
5953   /* Consume the `[' token.  */
5954   cp_lexer_consume_token (parser->lexer);
5955
5956   /* Parse the index expression.  */
5957   /* ??? For offsetof, there is a question of what to allow here.  If
5958      offsetof is not being used in an integral constant expression context,
5959      then we *could* get the right answer by computing the value at runtime.
5960      If we are in an integral constant expression context, then we might
5961      could accept any constant expression; hard to say without analysis.
5962      Rather than open the barn door too wide right away, allow only integer
5963      constant expressions here.  */
5964   if (for_offsetof)
5965     index = cp_parser_constant_expression (parser, false, NULL);
5966   else
5967     {
5968       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5969         {
5970           bool expr_nonconst_p;
5971           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5972           index = cp_parser_braced_list (parser, &expr_nonconst_p);
5973         }
5974       else
5975         index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5976     }
5977
5978   /* Look for the closing `]'.  */
5979   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5980
5981   /* Build the ARRAY_REF.  */
5982   postfix_expression = grok_array_decl (loc, postfix_expression,
5983                                         index, decltype_p);
5984
5985   /* When not doing offsetof, array references are not permitted in
5986      constant-expressions.  */
5987   if (!for_offsetof
5988       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5989     postfix_expression = error_mark_node;
5990
5991   return postfix_expression;
5992 }
5993
5994 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5995    by cp_parser_builtin_offsetof.  We're looking for
5996
5997      postfix-expression . template [opt] id-expression
5998      postfix-expression . pseudo-destructor-name
5999      postfix-expression -> template [opt] id-expression
6000      postfix-expression -> pseudo-destructor-name
6001
6002    FOR_OFFSETOF is set if we're being called in that context.  That sorta
6003    limits what of the above we'll actually accept, but nevermind.
6004    TOKEN_TYPE is the "." or "->" token, which will already have been
6005    removed from the stream.  */
6006
6007 static tree
6008 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6009                                         enum cpp_ttype token_type,
6010                                         tree postfix_expression,
6011                                         bool for_offsetof, cp_id_kind *idk,
6012                                         location_t location)
6013 {
6014   tree name;
6015   bool dependent_p;
6016   bool pseudo_destructor_p;
6017   tree scope = NULL_TREE;
6018
6019   /* If this is a `->' operator, dereference the pointer.  */
6020   if (token_type == CPP_DEREF)
6021     postfix_expression = build_x_arrow (location, postfix_expression,
6022                                         tf_warning_or_error);
6023   /* Check to see whether or not the expression is type-dependent.  */
6024   dependent_p = type_dependent_expression_p (postfix_expression);
6025   /* The identifier following the `->' or `.' is not qualified.  */
6026   parser->scope = NULL_TREE;
6027   parser->qualifying_scope = NULL_TREE;
6028   parser->object_scope = NULL_TREE;
6029   *idk = CP_ID_KIND_NONE;
6030
6031   /* Enter the scope corresponding to the type of the object
6032      given by the POSTFIX_EXPRESSION.  */
6033   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6034     {
6035       scope = TREE_TYPE (postfix_expression);
6036       /* According to the standard, no expression should ever have
6037          reference type.  Unfortunately, we do not currently match
6038          the standard in this respect in that our internal representation
6039          of an expression may have reference type even when the standard
6040          says it does not.  Therefore, we have to manually obtain the
6041          underlying type here.  */
6042       scope = non_reference (scope);
6043       /* The type of the POSTFIX_EXPRESSION must be complete.  */
6044       if (scope == unknown_type_node)
6045         {
6046           error_at (location, "%qE does not have class type",
6047                     postfix_expression);
6048           scope = NULL_TREE;
6049         }
6050       /* Unlike the object expression in other contexts, *this is not
6051          required to be of complete type for purposes of class member
6052          access (5.2.5) outside the member function body.  */
6053       else if (scope != current_class_ref
6054                && !(processing_template_decl && scope == current_class_type))
6055         scope = complete_type_or_else (scope, NULL_TREE);
6056       /* Let the name lookup machinery know that we are processing a
6057          class member access expression.  */
6058       parser->context->object_type = scope;
6059       /* If something went wrong, we want to be able to discern that case,
6060          as opposed to the case where there was no SCOPE due to the type
6061          of expression being dependent.  */
6062       if (!scope)
6063         scope = error_mark_node;
6064       /* If the SCOPE was erroneous, make the various semantic analysis
6065          functions exit quickly -- and without issuing additional error
6066          messages.  */
6067       if (scope == error_mark_node)
6068         postfix_expression = error_mark_node;
6069     }
6070
6071   /* Assume this expression is not a pseudo-destructor access.  */
6072   pseudo_destructor_p = false;
6073
6074   /* If the SCOPE is a scalar type, then, if this is a valid program,
6075      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
6076      is type dependent, it can be pseudo-destructor-name or something else.
6077      Try to parse it as pseudo-destructor-name first.  */
6078   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6079     {
6080       tree s;
6081       tree type;
6082
6083       cp_parser_parse_tentatively (parser);
6084       /* Parse the pseudo-destructor-name.  */
6085       s = NULL_TREE;
6086       cp_parser_pseudo_destructor_name (parser, &s, &type);
6087       if (dependent_p
6088           && (cp_parser_error_occurred (parser)
6089               || TREE_CODE (type) != TYPE_DECL
6090               || !SCALAR_TYPE_P (TREE_TYPE (type))))
6091         cp_parser_abort_tentative_parse (parser);
6092       else if (cp_parser_parse_definitely (parser))
6093         {
6094           pseudo_destructor_p = true;
6095           postfix_expression
6096             = finish_pseudo_destructor_expr (postfix_expression,
6097                                              s, TREE_TYPE (type));
6098         }
6099     }
6100
6101   if (!pseudo_destructor_p)
6102     {
6103       /* If the SCOPE is not a scalar type, we are looking at an
6104          ordinary class member access expression, rather than a
6105          pseudo-destructor-name.  */
6106       bool template_p;
6107       cp_token *token = cp_lexer_peek_token (parser->lexer);
6108       /* Parse the id-expression.  */
6109       name = (cp_parser_id_expression
6110               (parser,
6111                cp_parser_optional_template_keyword (parser),
6112                /*check_dependency_p=*/true,
6113                &template_p,
6114                /*declarator_p=*/false,
6115                /*optional_p=*/false));
6116       /* In general, build a SCOPE_REF if the member name is qualified.
6117          However, if the name was not dependent and has already been
6118          resolved; there is no need to build the SCOPE_REF.  For example;
6119
6120              struct X { void f(); };
6121              template <typename T> void f(T* t) { t->X::f(); }
6122
6123          Even though "t" is dependent, "X::f" is not and has been resolved
6124          to a BASELINK; there is no need to include scope information.  */
6125
6126       /* But we do need to remember that there was an explicit scope for
6127          virtual function calls.  */
6128       if (parser->scope)
6129         *idk = CP_ID_KIND_QUALIFIED;
6130
6131       /* If the name is a template-id that names a type, we will get a
6132          TYPE_DECL here.  That is invalid code.  */
6133       if (TREE_CODE (name) == TYPE_DECL)
6134         {
6135           error_at (token->location, "invalid use of %qD", name);
6136           postfix_expression = error_mark_node;
6137         }
6138       else
6139         {
6140           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6141             {
6142               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6143                 {
6144                   error_at (token->location, "%<%D::%D%> is not a class member",
6145                             parser->scope, name);
6146                   postfix_expression = error_mark_node;
6147                 }
6148               else
6149                 name = build_qualified_name (/*type=*/NULL_TREE,
6150                                              parser->scope,
6151                                              name,
6152                                              template_p);
6153               parser->scope = NULL_TREE;
6154               parser->qualifying_scope = NULL_TREE;
6155               parser->object_scope = NULL_TREE;
6156             }
6157           if (parser->scope && name && BASELINK_P (name))
6158             adjust_result_of_qualified_name_lookup
6159               (name, parser->scope, scope);
6160           postfix_expression
6161             = finish_class_member_access_expr (postfix_expression, name,
6162                                                template_p, 
6163                                                tf_warning_or_error);
6164         }
6165     }
6166
6167   /* We no longer need to look up names in the scope of the object on
6168      the left-hand side of the `.' or `->' operator.  */
6169   parser->context->object_type = NULL_TREE;
6170
6171   /* Outside of offsetof, these operators may not appear in
6172      constant-expressions.  */
6173   if (!for_offsetof
6174       && (cp_parser_non_integral_constant_expression
6175           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6176     postfix_expression = error_mark_node;
6177
6178   return postfix_expression;
6179 }
6180
6181 /* Parse a parenthesized expression-list.
6182
6183    expression-list:
6184      assignment-expression
6185      expression-list, assignment-expression
6186
6187    attribute-list:
6188      expression-list
6189      identifier
6190      identifier, expression-list
6191
6192    CAST_P is true if this expression is the target of a cast.
6193
6194    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6195    argument pack.
6196
6197    Returns a vector of trees.  Each element is a representation of an
6198    assignment-expression.  NULL is returned if the ( and or ) are
6199    missing.  An empty, but allocated, vector is returned on no
6200    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6201    if we are parsing an attribute list for an attribute that wants a
6202    plain identifier argument, normal_attr for an attribute that wants
6203    an expression, or non_attr if we aren't parsing an attribute list.  If
6204    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6205    not all of the expressions in the list were constant.  */
6206
6207 static vec<tree, va_gc> *
6208 cp_parser_parenthesized_expression_list (cp_parser* parser,
6209                                          int is_attribute_list,
6210                                          bool cast_p,
6211                                          bool allow_expansion_p,
6212                                          bool *non_constant_p)
6213 {
6214   vec<tree, va_gc> *expression_list;
6215   bool fold_expr_p = is_attribute_list != non_attr;
6216   tree identifier = NULL_TREE;
6217   bool saved_greater_than_is_operator_p;
6218
6219   /* Assume all the expressions will be constant.  */
6220   if (non_constant_p)
6221     *non_constant_p = false;
6222
6223   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6224     return NULL;
6225
6226   expression_list = make_tree_vector ();
6227
6228   /* Within a parenthesized expression, a `>' token is always
6229      the greater-than operator.  */
6230   saved_greater_than_is_operator_p
6231     = parser->greater_than_is_operator_p;
6232   parser->greater_than_is_operator_p = true;
6233
6234   /* Consume expressions until there are no more.  */
6235   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6236     while (true)
6237       {
6238         tree expr;
6239
6240         /* At the beginning of attribute lists, check to see if the
6241            next token is an identifier.  */
6242         if (is_attribute_list == id_attr
6243             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6244           {
6245             cp_token *token;
6246
6247             /* Consume the identifier.  */
6248             token = cp_lexer_consume_token (parser->lexer);
6249             /* Save the identifier.  */
6250             identifier = token->u.value;
6251           }
6252         else
6253           {
6254             bool expr_non_constant_p;
6255
6256             /* Parse the next assignment-expression.  */
6257             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6258               {
6259                 /* A braced-init-list.  */
6260                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6261                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6262                 if (non_constant_p && expr_non_constant_p)
6263                   *non_constant_p = true;
6264               }
6265             else if (non_constant_p)
6266               {
6267                 expr = (cp_parser_constant_expression
6268                         (parser, /*allow_non_constant_p=*/true,
6269                          &expr_non_constant_p));
6270                 if (expr_non_constant_p)
6271                   *non_constant_p = true;
6272               }
6273             else
6274               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6275
6276             if (fold_expr_p)
6277               expr = fold_non_dependent_expr (expr);
6278
6279             /* If we have an ellipsis, then this is an expression
6280                expansion.  */
6281             if (allow_expansion_p
6282                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6283               {
6284                 /* Consume the `...'.  */
6285                 cp_lexer_consume_token (parser->lexer);
6286
6287                 /* Build the argument pack.  */
6288                 expr = make_pack_expansion (expr);
6289               }
6290
6291              /* Add it to the list.  We add error_mark_node
6292                 expressions to the list, so that we can still tell if
6293                 the correct form for a parenthesized expression-list
6294                 is found. That gives better errors.  */
6295             vec_safe_push (expression_list, expr);
6296
6297             if (expr == error_mark_node)
6298               goto skip_comma;
6299           }
6300
6301         /* After the first item, attribute lists look the same as
6302            expression lists.  */
6303         is_attribute_list = non_attr;
6304
6305       get_comma:;
6306         /* If the next token isn't a `,', then we are done.  */
6307         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6308           break;
6309
6310         /* Otherwise, consume the `,' and keep going.  */
6311         cp_lexer_consume_token (parser->lexer);
6312       }
6313
6314   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6315     {
6316       int ending;
6317
6318     skip_comma:;
6319       /* We try and resync to an unnested comma, as that will give the
6320          user better diagnostics.  */
6321       ending = cp_parser_skip_to_closing_parenthesis (parser,
6322                                                       /*recovering=*/true,
6323                                                       /*or_comma=*/true,
6324                                                       /*consume_paren=*/true);
6325       if (ending < 0)
6326         goto get_comma;
6327       if (!ending)
6328         {
6329           parser->greater_than_is_operator_p
6330             = saved_greater_than_is_operator_p;
6331           return NULL;
6332         }
6333     }
6334
6335   parser->greater_than_is_operator_p
6336     = saved_greater_than_is_operator_p;
6337
6338   if (identifier)
6339     vec_safe_insert (expression_list, 0, identifier);
6340
6341   return expression_list;
6342 }
6343
6344 /* Parse a pseudo-destructor-name.
6345
6346    pseudo-destructor-name:
6347      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6348      :: [opt] nested-name-specifier template template-id :: ~ type-name
6349      :: [opt] nested-name-specifier [opt] ~ type-name
6350
6351    If either of the first two productions is used, sets *SCOPE to the
6352    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6353    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6354    or ERROR_MARK_NODE if the parse fails.  */
6355
6356 static void
6357 cp_parser_pseudo_destructor_name (cp_parser* parser,
6358                                   tree* scope,
6359                                   tree* type)
6360 {
6361   bool nested_name_specifier_p;
6362
6363   /* Assume that things will not work out.  */
6364   *type = error_mark_node;
6365
6366   /* Look for the optional `::' operator.  */
6367   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6368   /* Look for the optional nested-name-specifier.  */
6369   nested_name_specifier_p
6370     = (cp_parser_nested_name_specifier_opt (parser,
6371                                             /*typename_keyword_p=*/false,
6372                                             /*check_dependency_p=*/true,
6373                                             /*type_p=*/false,
6374                                             /*is_declaration=*/false)
6375        != NULL_TREE);
6376   /* Now, if we saw a nested-name-specifier, we might be doing the
6377      second production.  */
6378   if (nested_name_specifier_p
6379       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6380     {
6381       /* Consume the `template' keyword.  */
6382       cp_lexer_consume_token (parser->lexer);
6383       /* Parse the template-id.  */
6384       cp_parser_template_id (parser,
6385                              /*template_keyword_p=*/true,
6386                              /*check_dependency_p=*/false,
6387                              class_type,
6388                              /*is_declaration=*/true);
6389       /* Look for the `::' token.  */
6390       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6391     }
6392   /* If the next token is not a `~', then there might be some
6393      additional qualification.  */
6394   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6395     {
6396       /* At this point, we're looking for "type-name :: ~".  The type-name
6397          must not be a class-name, since this is a pseudo-destructor.  So,
6398          it must be either an enum-name, or a typedef-name -- both of which
6399          are just identifiers.  So, we peek ahead to check that the "::"
6400          and "~" tokens are present; if they are not, then we can avoid
6401          calling type_name.  */
6402       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6403           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6404           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6405         {
6406           cp_parser_error (parser, "non-scalar type");
6407           return;
6408         }
6409
6410       /* Look for the type-name.  */
6411       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6412       if (*scope == error_mark_node)
6413         return;
6414
6415       /* Look for the `::' token.  */
6416       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6417     }
6418   else
6419     *scope = NULL_TREE;
6420
6421   /* Look for the `~'.  */
6422   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6423
6424   /* Once we see the ~, this has to be a pseudo-destructor.  */
6425   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6426     cp_parser_commit_to_tentative_parse (parser);
6427
6428   /* Look for the type-name again.  We are not responsible for
6429      checking that it matches the first type-name.  */
6430   *type = cp_parser_nonclass_name (parser);
6431 }
6432
6433 /* Parse a unary-expression.
6434
6435    unary-expression:
6436      postfix-expression
6437      ++ cast-expression
6438      -- cast-expression
6439      unary-operator cast-expression
6440      sizeof unary-expression
6441      sizeof ( type-id )
6442      alignof ( type-id )  [C++0x]
6443      new-expression
6444      delete-expression
6445
6446    GNU Extensions:
6447
6448    unary-expression:
6449      __extension__ cast-expression
6450      __alignof__ unary-expression
6451      __alignof__ ( type-id )
6452      alignof unary-expression  [C++0x]
6453      __real__ cast-expression
6454      __imag__ cast-expression
6455      && identifier
6456
6457    ADDRESS_P is true iff the unary-expression is appearing as the
6458    operand of the `&' operator.   CAST_P is true if this expression is
6459    the target of a cast.
6460
6461    Returns a representation of the expression.  */
6462
6463 static tree
6464 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6465                             bool decltype_p, cp_id_kind * pidk)
6466 {
6467   cp_token *token;
6468   enum tree_code unary_operator;
6469
6470   /* Peek at the next token.  */
6471   token = cp_lexer_peek_token (parser->lexer);
6472   /* Some keywords give away the kind of expression.  */
6473   if (token->type == CPP_KEYWORD)
6474     {
6475       enum rid keyword = token->keyword;
6476
6477       switch (keyword)
6478         {
6479         case RID_ALIGNOF:
6480         case RID_SIZEOF:
6481           {
6482             tree operand, ret;
6483             enum tree_code op;
6484             location_t first_loc;
6485
6486             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6487             /* Consume the token.  */
6488             cp_lexer_consume_token (parser->lexer);
6489             first_loc = cp_lexer_peek_token (parser->lexer)->location;
6490             /* Parse the operand.  */
6491             operand = cp_parser_sizeof_operand (parser, keyword);
6492
6493             if (TYPE_P (operand))
6494               ret = cxx_sizeof_or_alignof_type (operand, op, true);
6495             else
6496               {
6497                 /* ISO C++ defines alignof only with types, not with
6498                    expressions. So pedwarn if alignof is used with a non-
6499                    type expression. However, __alignof__ is ok.  */
6500                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6501                   pedwarn (token->location, OPT_Wpedantic,
6502                            "ISO C++ does not allow %<alignof%> "
6503                            "with a non-type");
6504
6505                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6506               }
6507             /* For SIZEOF_EXPR, just issue diagnostics, but keep
6508                SIZEOF_EXPR with the original operand.  */
6509             if (op == SIZEOF_EXPR && ret != error_mark_node)
6510               {
6511                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6512                   {
6513                     if (!processing_template_decl && TYPE_P (operand))
6514                       {
6515                         ret = build_min (SIZEOF_EXPR, size_type_node,
6516                                          build1 (NOP_EXPR, operand,
6517                                                  error_mark_node));
6518                         SIZEOF_EXPR_TYPE_P (ret) = 1;
6519                       }
6520                     else
6521                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6522                     TREE_SIDE_EFFECTS (ret) = 0;
6523                     TREE_READONLY (ret) = 1;
6524                   }
6525                 SET_EXPR_LOCATION (ret, first_loc);
6526               }
6527             return ret;
6528           }
6529
6530         case RID_NEW:
6531           return cp_parser_new_expression (parser);
6532
6533         case RID_DELETE:
6534           return cp_parser_delete_expression (parser);
6535
6536         case RID_EXTENSION:
6537           {
6538             /* The saved value of the PEDANTIC flag.  */
6539             int saved_pedantic;
6540             tree expr;
6541
6542             /* Save away the PEDANTIC flag.  */
6543             cp_parser_extension_opt (parser, &saved_pedantic);
6544             /* Parse the cast-expression.  */
6545             expr = cp_parser_simple_cast_expression (parser);
6546             /* Restore the PEDANTIC flag.  */
6547             pedantic = saved_pedantic;
6548
6549             return expr;
6550           }
6551
6552         case RID_REALPART:
6553         case RID_IMAGPART:
6554           {
6555             tree expression;
6556
6557             /* Consume the `__real__' or `__imag__' token.  */
6558             cp_lexer_consume_token (parser->lexer);
6559             /* Parse the cast-expression.  */
6560             expression = cp_parser_simple_cast_expression (parser);
6561             /* Create the complete representation.  */
6562             return build_x_unary_op (token->location,
6563                                      (keyword == RID_REALPART
6564                                       ? REALPART_EXPR : IMAGPART_EXPR),
6565                                      expression,
6566                                      tf_warning_or_error);
6567           }
6568           break;
6569
6570         case RID_TRANSACTION_ATOMIC:
6571         case RID_TRANSACTION_RELAXED:
6572           return cp_parser_transaction_expression (parser, keyword);
6573
6574         case RID_NOEXCEPT:
6575           {
6576             tree expr;
6577             const char *saved_message;
6578             bool saved_integral_constant_expression_p;
6579             bool saved_non_integral_constant_expression_p;
6580             bool saved_greater_than_is_operator_p;
6581
6582             cp_lexer_consume_token (parser->lexer);
6583             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6584
6585             saved_message = parser->type_definition_forbidden_message;
6586             parser->type_definition_forbidden_message
6587               = G_("types may not be defined in %<noexcept%> expressions");
6588
6589             saved_integral_constant_expression_p
6590               = parser->integral_constant_expression_p;
6591             saved_non_integral_constant_expression_p
6592               = parser->non_integral_constant_expression_p;
6593             parser->integral_constant_expression_p = false;
6594
6595             saved_greater_than_is_operator_p
6596               = parser->greater_than_is_operator_p;
6597             parser->greater_than_is_operator_p = true;
6598
6599             ++cp_unevaluated_operand;
6600             ++c_inhibit_evaluation_warnings;
6601             expr = cp_parser_expression (parser, false, NULL);
6602             --c_inhibit_evaluation_warnings;
6603             --cp_unevaluated_operand;
6604
6605             parser->greater_than_is_operator_p
6606               = saved_greater_than_is_operator_p;
6607
6608             parser->integral_constant_expression_p
6609               = saved_integral_constant_expression_p;
6610             parser->non_integral_constant_expression_p
6611               = saved_non_integral_constant_expression_p;
6612
6613             parser->type_definition_forbidden_message = saved_message;
6614
6615             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6616             return finish_noexcept_expr (expr, tf_warning_or_error);
6617           }
6618
6619         default:
6620           break;
6621         }
6622     }
6623
6624   /* Look for the `:: new' and `:: delete', which also signal the
6625      beginning of a new-expression, or delete-expression,
6626      respectively.  If the next token is `::', then it might be one of
6627      these.  */
6628   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6629     {
6630       enum rid keyword;
6631
6632       /* See if the token after the `::' is one of the keywords in
6633          which we're interested.  */
6634       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6635       /* If it's `new', we have a new-expression.  */
6636       if (keyword == RID_NEW)
6637         return cp_parser_new_expression (parser);
6638       /* Similarly, for `delete'.  */
6639       else if (keyword == RID_DELETE)
6640         return cp_parser_delete_expression (parser);
6641     }
6642
6643   /* Look for a unary operator.  */
6644   unary_operator = cp_parser_unary_operator (token);
6645   /* The `++' and `--' operators can be handled similarly, even though
6646      they are not technically unary-operators in the grammar.  */
6647   if (unary_operator == ERROR_MARK)
6648     {
6649       if (token->type == CPP_PLUS_PLUS)
6650         unary_operator = PREINCREMENT_EXPR;
6651       else if (token->type == CPP_MINUS_MINUS)
6652         unary_operator = PREDECREMENT_EXPR;
6653       /* Handle the GNU address-of-label extension.  */
6654       else if (cp_parser_allow_gnu_extensions_p (parser)
6655                && token->type == CPP_AND_AND)
6656         {
6657           tree identifier;
6658           tree expression;
6659           location_t loc = token->location;
6660
6661           /* Consume the '&&' token.  */
6662           cp_lexer_consume_token (parser->lexer);
6663           /* Look for the identifier.  */
6664           identifier = cp_parser_identifier (parser);
6665           /* Create an expression representing the address.  */
6666           expression = finish_label_address_expr (identifier, loc);
6667           if (cp_parser_non_integral_constant_expression (parser,
6668                                                           NIC_ADDR_LABEL))
6669             expression = error_mark_node;
6670           return expression;
6671         }
6672     }
6673   if (unary_operator != ERROR_MARK)
6674     {
6675       tree cast_expression;
6676       tree expression = error_mark_node;
6677       non_integral_constant non_constant_p = NIC_NONE;
6678       location_t loc = token->location;
6679       tsubst_flags_t complain = complain_flags (decltype_p);
6680
6681       /* Consume the operator token.  */
6682       token = cp_lexer_consume_token (parser->lexer);
6683       /* Parse the cast-expression.  */
6684       cast_expression
6685         = cp_parser_cast_expression (parser,
6686                                      unary_operator == ADDR_EXPR,
6687                                      /*cast_p=*/false,
6688                                      /*decltype*/false,
6689                                      pidk);
6690       /* Now, build an appropriate representation.  */
6691       switch (unary_operator)
6692         {
6693         case INDIRECT_REF:
6694           non_constant_p = NIC_STAR;
6695           expression = build_x_indirect_ref (loc, cast_expression,
6696                                              RO_UNARY_STAR,
6697                                              complain);
6698           break;
6699
6700         case ADDR_EXPR:
6701            non_constant_p = NIC_ADDR;
6702           /* Fall through.  */
6703         case BIT_NOT_EXPR:
6704           expression = build_x_unary_op (loc, unary_operator,
6705                                          cast_expression,
6706                                          complain);
6707           break;
6708
6709         case PREINCREMENT_EXPR:
6710         case PREDECREMENT_EXPR:
6711           non_constant_p = unary_operator == PREINCREMENT_EXPR
6712                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6713           /* Fall through.  */
6714         case UNARY_PLUS_EXPR:
6715         case NEGATE_EXPR:
6716         case TRUTH_NOT_EXPR:
6717           expression = finish_unary_op_expr (loc, unary_operator,
6718                                              cast_expression, complain);
6719           break;
6720
6721         default:
6722           gcc_unreachable ();
6723         }
6724
6725       if (non_constant_p != NIC_NONE
6726           && cp_parser_non_integral_constant_expression (parser,
6727                                                          non_constant_p))
6728         expression = error_mark_node;
6729
6730       return expression;
6731     }
6732
6733   return cp_parser_postfix_expression (parser, address_p, cast_p,
6734                                        /*member_access_only_p=*/false,
6735                                        decltype_p,
6736                                        pidk);
6737 }
6738
6739 static inline tree
6740 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6741                             cp_id_kind * pidk)
6742 {
6743   return cp_parser_unary_expression (parser, address_p, cast_p,
6744                                      /*decltype*/false, pidk);
6745 }
6746
6747 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6748    unary-operator, the corresponding tree code is returned.  */
6749
6750 static enum tree_code
6751 cp_parser_unary_operator (cp_token* token)
6752 {
6753   switch (token->type)
6754     {
6755     case CPP_MULT:
6756       return INDIRECT_REF;
6757
6758     case CPP_AND:
6759       return ADDR_EXPR;
6760
6761     case CPP_PLUS:
6762       return UNARY_PLUS_EXPR;
6763
6764     case CPP_MINUS:
6765       return NEGATE_EXPR;
6766
6767     case CPP_NOT:
6768       return TRUTH_NOT_EXPR;
6769
6770     case CPP_COMPL:
6771       return BIT_NOT_EXPR;
6772
6773     default:
6774       return ERROR_MARK;
6775     }
6776 }
6777
6778 /* Parse a new-expression.
6779
6780    new-expression:
6781      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6782      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6783
6784    Returns a representation of the expression.  */
6785
6786 static tree
6787 cp_parser_new_expression (cp_parser* parser)
6788 {
6789   bool global_scope_p;
6790   vec<tree, va_gc> *placement;
6791   tree type;
6792   vec<tree, va_gc> *initializer;
6793   tree nelts = NULL_TREE;
6794   tree ret;
6795
6796   /* Look for the optional `::' operator.  */
6797   global_scope_p
6798     = (cp_parser_global_scope_opt (parser,
6799                                    /*current_scope_valid_p=*/false)
6800        != NULL_TREE);
6801   /* Look for the `new' operator.  */
6802   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6803   /* There's no easy way to tell a new-placement from the
6804      `( type-id )' construct.  */
6805   cp_parser_parse_tentatively (parser);
6806   /* Look for a new-placement.  */
6807   placement = cp_parser_new_placement (parser);
6808   /* If that didn't work out, there's no new-placement.  */
6809   if (!cp_parser_parse_definitely (parser))
6810     {
6811       if (placement != NULL)
6812         release_tree_vector (placement);
6813       placement = NULL;
6814     }
6815
6816   /* If the next token is a `(', then we have a parenthesized
6817      type-id.  */
6818   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6819     {
6820       cp_token *token;
6821       const char *saved_message = parser->type_definition_forbidden_message;
6822
6823       /* Consume the `('.  */
6824       cp_lexer_consume_token (parser->lexer);
6825
6826       /* Parse the type-id.  */
6827       parser->type_definition_forbidden_message
6828         = G_("types may not be defined in a new-expression");
6829       type = cp_parser_type_id (parser);
6830       parser->type_definition_forbidden_message = saved_message;
6831
6832       /* Look for the closing `)'.  */
6833       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6834       token = cp_lexer_peek_token (parser->lexer);
6835       /* There should not be a direct-new-declarator in this production,
6836          but GCC used to allowed this, so we check and emit a sensible error
6837          message for this case.  */
6838       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6839         {
6840           error_at (token->location,
6841                     "array bound forbidden after parenthesized type-id");
6842           inform (token->location, 
6843                   "try removing the parentheses around the type-id");
6844           cp_parser_direct_new_declarator (parser);
6845         }
6846     }
6847   /* Otherwise, there must be a new-type-id.  */
6848   else
6849     type = cp_parser_new_type_id (parser, &nelts);
6850
6851   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6852   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6853       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6854     initializer = cp_parser_new_initializer (parser);
6855   else
6856     initializer = NULL;
6857
6858   /* A new-expression may not appear in an integral constant
6859      expression.  */
6860   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6861     ret = error_mark_node;
6862   else
6863     {
6864       /* Create a representation of the new-expression.  */
6865       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6866                        tf_warning_or_error);
6867     }
6868
6869   if (placement != NULL)
6870     release_tree_vector (placement);
6871   if (initializer != NULL)
6872     release_tree_vector (initializer);
6873
6874   return ret;
6875 }
6876
6877 /* Parse a new-placement.
6878
6879    new-placement:
6880      ( expression-list )
6881
6882    Returns the same representation as for an expression-list.  */
6883
6884 static vec<tree, va_gc> *
6885 cp_parser_new_placement (cp_parser* parser)
6886 {
6887   vec<tree, va_gc> *expression_list;
6888
6889   /* Parse the expression-list.  */
6890   expression_list = (cp_parser_parenthesized_expression_list
6891                      (parser, non_attr, /*cast_p=*/false,
6892                       /*allow_expansion_p=*/true,
6893                       /*non_constant_p=*/NULL));
6894
6895   return expression_list;
6896 }
6897
6898 /* Parse a new-type-id.
6899
6900    new-type-id:
6901      type-specifier-seq new-declarator [opt]
6902
6903    Returns the TYPE allocated.  If the new-type-id indicates an array
6904    type, *NELTS is set to the number of elements in the last array
6905    bound; the TYPE will not include the last array bound.  */
6906
6907 static tree
6908 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6909 {
6910   cp_decl_specifier_seq type_specifier_seq;
6911   cp_declarator *new_declarator;
6912   cp_declarator *declarator;
6913   cp_declarator *outer_declarator;
6914   const char *saved_message;
6915
6916   /* The type-specifier sequence must not contain type definitions.
6917      (It cannot contain declarations of new types either, but if they
6918      are not definitions we will catch that because they are not
6919      complete.)  */
6920   saved_message = parser->type_definition_forbidden_message;
6921   parser->type_definition_forbidden_message
6922     = G_("types may not be defined in a new-type-id");
6923   /* Parse the type-specifier-seq.  */
6924   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6925                                 /*is_trailing_return=*/false,
6926                                 &type_specifier_seq);
6927   /* Restore the old message.  */
6928   parser->type_definition_forbidden_message = saved_message;
6929
6930   if (type_specifier_seq.type == error_mark_node)
6931     return error_mark_node;
6932
6933   /* Parse the new-declarator.  */
6934   new_declarator = cp_parser_new_declarator_opt (parser);
6935
6936   /* Determine the number of elements in the last array dimension, if
6937      any.  */
6938   *nelts = NULL_TREE;
6939   /* Skip down to the last array dimension.  */
6940   declarator = new_declarator;
6941   outer_declarator = NULL;
6942   while (declarator && (declarator->kind == cdk_pointer
6943                         || declarator->kind == cdk_ptrmem))
6944     {
6945       outer_declarator = declarator;
6946       declarator = declarator->declarator;
6947     }
6948   while (declarator
6949          && declarator->kind == cdk_array
6950          && declarator->declarator
6951          && declarator->declarator->kind == cdk_array)
6952     {
6953       outer_declarator = declarator;
6954       declarator = declarator->declarator;
6955     }
6956
6957   if (declarator && declarator->kind == cdk_array)
6958     {
6959       *nelts = declarator->u.array.bounds;
6960       if (*nelts == error_mark_node)
6961         *nelts = integer_one_node;
6962
6963       if (outer_declarator)
6964         outer_declarator->declarator = declarator->declarator;
6965       else
6966         new_declarator = NULL;
6967     }
6968
6969   return groktypename (&type_specifier_seq, new_declarator, false);
6970 }
6971
6972 /* Parse an (optional) new-declarator.
6973
6974    new-declarator:
6975      ptr-operator new-declarator [opt]
6976      direct-new-declarator
6977
6978    Returns the declarator.  */
6979
6980 static cp_declarator *
6981 cp_parser_new_declarator_opt (cp_parser* parser)
6982 {
6983   enum tree_code code;
6984   tree type, std_attributes = NULL_TREE;
6985   cp_cv_quals cv_quals;  
6986
6987   /* We don't know if there's a ptr-operator next, or not.  */
6988   cp_parser_parse_tentatively (parser);
6989   /* Look for a ptr-operator.  */
6990   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6991   /* If that worked, look for more new-declarators.  */
6992   if (cp_parser_parse_definitely (parser))
6993     {
6994       cp_declarator *declarator;
6995
6996       /* Parse another optional declarator.  */
6997       declarator = cp_parser_new_declarator_opt (parser);
6998
6999       declarator = cp_parser_make_indirect_declarator
7000         (code, type, cv_quals, declarator, std_attributes);
7001
7002       return declarator;
7003     }
7004
7005   /* If the next token is a `[', there is a direct-new-declarator.  */
7006   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7007     return cp_parser_direct_new_declarator (parser);
7008
7009   return NULL;
7010 }
7011
7012 /* Parse a direct-new-declarator.
7013
7014    direct-new-declarator:
7015      [ expression ]
7016      direct-new-declarator [constant-expression]
7017
7018    */
7019
7020 static cp_declarator *
7021 cp_parser_direct_new_declarator (cp_parser* parser)
7022 {
7023   cp_declarator *declarator = NULL;
7024
7025   while (true)
7026     {
7027       tree expression;
7028       cp_token *token;
7029
7030       /* Look for the opening `['.  */
7031       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7032
7033       token = cp_lexer_peek_token (parser->lexer);
7034       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7035       /* The standard requires that the expression have integral
7036          type.  DR 74 adds enumeration types.  We believe that the
7037          real intent is that these expressions be handled like the
7038          expression in a `switch' condition, which also allows
7039          classes with a single conversion to integral or
7040          enumeration type.  */
7041       if (!processing_template_decl)
7042         {
7043           expression
7044             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7045                                           expression,
7046                                           /*complain=*/true);
7047           if (!expression)
7048             {
7049               error_at (token->location,
7050                         "expression in new-declarator must have integral "
7051                         "or enumeration type");
7052               expression = error_mark_node;
7053             }
7054         }
7055
7056       /* Look for the closing `]'.  */
7057       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7058
7059       /* Add this bound to the declarator.  */
7060       declarator = make_array_declarator (declarator, expression);
7061
7062       /* If the next token is not a `[', then there are no more
7063          bounds.  */
7064       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7065         break;
7066     }
7067
7068   return declarator;
7069 }
7070
7071 /* Parse a new-initializer.
7072
7073    new-initializer:
7074      ( expression-list [opt] )
7075      braced-init-list
7076
7077    Returns a representation of the expression-list.  */
7078
7079 static vec<tree, va_gc> *
7080 cp_parser_new_initializer (cp_parser* parser)
7081 {
7082   vec<tree, va_gc> *expression_list;
7083
7084   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7085     {
7086       tree t;
7087       bool expr_non_constant_p;
7088       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7089       t = cp_parser_braced_list (parser, &expr_non_constant_p);
7090       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7091       expression_list = make_tree_vector_single (t);
7092     }
7093   else
7094     expression_list = (cp_parser_parenthesized_expression_list
7095                        (parser, non_attr, /*cast_p=*/false,
7096                         /*allow_expansion_p=*/true,
7097                         /*non_constant_p=*/NULL));
7098
7099   return expression_list;
7100 }
7101
7102 /* Parse a delete-expression.
7103
7104    delete-expression:
7105      :: [opt] delete cast-expression
7106      :: [opt] delete [ ] cast-expression
7107
7108    Returns a representation of the expression.  */
7109
7110 static tree
7111 cp_parser_delete_expression (cp_parser* parser)
7112 {
7113   bool global_scope_p;
7114   bool array_p;
7115   tree expression;
7116
7117   /* Look for the optional `::' operator.  */
7118   global_scope_p
7119     = (cp_parser_global_scope_opt (parser,
7120                                    /*current_scope_valid_p=*/false)
7121        != NULL_TREE);
7122   /* Look for the `delete' keyword.  */
7123   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7124   /* See if the array syntax is in use.  */
7125   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7126     {
7127       /* Consume the `[' token.  */
7128       cp_lexer_consume_token (parser->lexer);
7129       /* Look for the `]' token.  */
7130       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7131       /* Remember that this is the `[]' construct.  */
7132       array_p = true;
7133     }
7134   else
7135     array_p = false;
7136
7137   /* Parse the cast-expression.  */
7138   expression = cp_parser_simple_cast_expression (parser);
7139
7140   /* A delete-expression may not appear in an integral constant
7141      expression.  */
7142   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7143     return error_mark_node;
7144
7145   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7146                         tf_warning_or_error);
7147 }
7148
7149 /* Returns true if TOKEN may start a cast-expression and false
7150    otherwise.  */
7151
7152 static bool
7153 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7154 {
7155   cp_token *token = cp_lexer_peek_token (parser->lexer);
7156   switch (token->type)
7157     {
7158     case CPP_COMMA:
7159     case CPP_SEMICOLON:
7160     case CPP_QUERY:
7161     case CPP_COLON:
7162     case CPP_CLOSE_SQUARE:
7163     case CPP_CLOSE_PAREN:
7164     case CPP_CLOSE_BRACE:
7165     case CPP_DOT:
7166     case CPP_DOT_STAR:
7167     case CPP_DEREF:
7168     case CPP_DEREF_STAR:
7169     case CPP_DIV:
7170     case CPP_MOD:
7171     case CPP_LSHIFT:
7172     case CPP_RSHIFT:
7173     case CPP_LESS:
7174     case CPP_GREATER:
7175     case CPP_LESS_EQ:
7176     case CPP_GREATER_EQ:
7177     case CPP_EQ_EQ:
7178     case CPP_NOT_EQ:
7179     case CPP_EQ:
7180     case CPP_MULT_EQ:
7181     case CPP_DIV_EQ:
7182     case CPP_MOD_EQ:
7183     case CPP_PLUS_EQ:
7184     case CPP_MINUS_EQ:
7185     case CPP_RSHIFT_EQ:
7186     case CPP_LSHIFT_EQ:
7187     case CPP_AND_EQ:
7188     case CPP_XOR_EQ:
7189     case CPP_OR_EQ:
7190     case CPP_XOR:
7191     case CPP_OR:
7192     case CPP_OR_OR:
7193     case CPP_EOF:
7194       return false;
7195
7196     case CPP_OPEN_PAREN:
7197       /* In ((type ()) () the last () isn't a valid cast-expression,
7198          so the whole must be parsed as postfix-expression.  */
7199       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7200              != CPP_CLOSE_PAREN;
7201
7202       /* '[' may start a primary-expression in obj-c++.  */
7203     case CPP_OPEN_SQUARE:
7204       return c_dialect_objc ();
7205
7206     default:
7207       return true;
7208     }
7209 }
7210
7211 /* Parse a cast-expression.
7212
7213    cast-expression:
7214      unary-expression
7215      ( type-id ) cast-expression
7216
7217    ADDRESS_P is true iff the unary-expression is appearing as the
7218    operand of the `&' operator.   CAST_P is true if this expression is
7219    the target of a cast.
7220
7221    Returns a representation of the expression.  */
7222
7223 static tree
7224 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7225                            bool decltype_p, cp_id_kind * pidk)
7226 {
7227   /* If it's a `(', then we might be looking at a cast.  */
7228   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7229     {
7230       tree type = NULL_TREE;
7231       tree expr = NULL_TREE;
7232       bool compound_literal_p;
7233       const char *saved_message;
7234
7235       /* There's no way to know yet whether or not this is a cast.
7236          For example, `(int (3))' is a unary-expression, while `(int)
7237          3' is a cast.  So, we resort to parsing tentatively.  */
7238       cp_parser_parse_tentatively (parser);
7239       /* Types may not be defined in a cast.  */
7240       saved_message = parser->type_definition_forbidden_message;
7241       parser->type_definition_forbidden_message
7242         = G_("types may not be defined in casts");
7243       /* Consume the `('.  */
7244       cp_lexer_consume_token (parser->lexer);
7245       /* A very tricky bit is that `(struct S) { 3 }' is a
7246          compound-literal (which we permit in C++ as an extension).
7247          But, that construct is not a cast-expression -- it is a
7248          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7249          is legal; if the compound-literal were a cast-expression,
7250          you'd need an extra set of parentheses.)  But, if we parse
7251          the type-id, and it happens to be a class-specifier, then we
7252          will commit to the parse at that point, because we cannot
7253          undo the action that is done when creating a new class.  So,
7254          then we cannot back up and do a postfix-expression.
7255
7256          Therefore, we scan ahead to the closing `)', and check to see
7257          if the token after the `)' is a `{'.  If so, we are not
7258          looking at a cast-expression.
7259
7260          Save tokens so that we can put them back.  */
7261       cp_lexer_save_tokens (parser->lexer);
7262       /* Skip tokens until the next token is a closing parenthesis.
7263          If we find the closing `)', and the next token is a `{', then
7264          we are looking at a compound-literal.  */
7265       compound_literal_p
7266         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7267                                                   /*consume_paren=*/true)
7268            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7269       /* Roll back the tokens we skipped.  */
7270       cp_lexer_rollback_tokens (parser->lexer);
7271       /* If we were looking at a compound-literal, simulate an error
7272          so that the call to cp_parser_parse_definitely below will
7273          fail.  */
7274       if (compound_literal_p)
7275         cp_parser_simulate_error (parser);
7276       else
7277         {
7278           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7279           parser->in_type_id_in_expr_p = true;
7280           /* Look for the type-id.  */
7281           type = cp_parser_type_id (parser);
7282           /* Look for the closing `)'.  */
7283           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7284           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7285         }
7286
7287       /* Restore the saved message.  */
7288       parser->type_definition_forbidden_message = saved_message;
7289
7290       /* At this point this can only be either a cast or a
7291          parenthesized ctor such as `(T ())' that looks like a cast to
7292          function returning T.  */
7293       if (!cp_parser_error_occurred (parser)
7294           && cp_parser_tokens_start_cast_expression (parser))
7295         {
7296           cp_parser_parse_definitely (parser);
7297           expr = cp_parser_cast_expression (parser,
7298                                             /*address_p=*/false,
7299                                             /*cast_p=*/true,
7300                                             /*decltype_p=*/false,
7301                                             pidk);
7302
7303           /* Warn about old-style casts, if so requested.  */
7304           if (warn_old_style_cast
7305               && !in_system_header
7306               && !VOID_TYPE_P (type)
7307               && current_lang_name != lang_name_c)
7308             warning (OPT_Wold_style_cast, "use of old-style cast");
7309
7310           /* Only type conversions to integral or enumeration types
7311              can be used in constant-expressions.  */
7312           if (!cast_valid_in_integral_constant_expression_p (type)
7313               && cp_parser_non_integral_constant_expression (parser,
7314                                                              NIC_CAST))
7315             return error_mark_node;
7316
7317           /* Perform the cast.  */
7318           expr = build_c_cast (input_location, type, expr);
7319           return expr;
7320         }
7321       else 
7322         cp_parser_abort_tentative_parse (parser);
7323     }
7324
7325   /* If we get here, then it's not a cast, so it must be a
7326      unary-expression.  */
7327   return cp_parser_unary_expression (parser, address_p, cast_p,
7328                                      decltype_p, pidk);
7329 }
7330
7331 /* Parse a binary expression of the general form:
7332
7333    pm-expression:
7334      cast-expression
7335      pm-expression .* cast-expression
7336      pm-expression ->* cast-expression
7337
7338    multiplicative-expression:
7339      pm-expression
7340      multiplicative-expression * pm-expression
7341      multiplicative-expression / pm-expression
7342      multiplicative-expression % pm-expression
7343
7344    additive-expression:
7345      multiplicative-expression
7346      additive-expression + multiplicative-expression
7347      additive-expression - multiplicative-expression
7348
7349    shift-expression:
7350      additive-expression
7351      shift-expression << additive-expression
7352      shift-expression >> additive-expression
7353
7354    relational-expression:
7355      shift-expression
7356      relational-expression < shift-expression
7357      relational-expression > shift-expression
7358      relational-expression <= shift-expression
7359      relational-expression >= shift-expression
7360
7361   GNU Extension:
7362
7363    relational-expression:
7364      relational-expression <? shift-expression
7365      relational-expression >? shift-expression
7366
7367    equality-expression:
7368      relational-expression
7369      equality-expression == relational-expression
7370      equality-expression != relational-expression
7371
7372    and-expression:
7373      equality-expression
7374      and-expression & equality-expression
7375
7376    exclusive-or-expression:
7377      and-expression
7378      exclusive-or-expression ^ and-expression
7379
7380    inclusive-or-expression:
7381      exclusive-or-expression
7382      inclusive-or-expression | exclusive-or-expression
7383
7384    logical-and-expression:
7385      inclusive-or-expression
7386      logical-and-expression && inclusive-or-expression
7387
7388    logical-or-expression:
7389      logical-and-expression
7390      logical-or-expression || logical-and-expression
7391
7392    All these are implemented with a single function like:
7393
7394    binary-expression:
7395      simple-cast-expression
7396      binary-expression <token> binary-expression
7397
7398    CAST_P is true if this expression is the target of a cast.
7399
7400    The binops_by_token map is used to get the tree codes for each <token> type.
7401    binary-expressions are associated according to a precedence table.  */
7402
7403 #define TOKEN_PRECEDENCE(token)                              \
7404 (((token->type == CPP_GREATER                                \
7405    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7406   && !parser->greater_than_is_operator_p)                    \
7407  ? PREC_NOT_OPERATOR                                         \
7408  : binops_by_token[token->type].prec)
7409
7410 static tree
7411 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7412                              bool no_toplevel_fold_p,
7413                              bool decltype_p,
7414                              enum cp_parser_prec prec,
7415                              cp_id_kind * pidk)
7416 {
7417   cp_parser_expression_stack stack;
7418   cp_parser_expression_stack_entry *sp = &stack[0];
7419   cp_parser_expression_stack_entry current;
7420   tree rhs;
7421   cp_token *token;
7422   enum tree_code rhs_type;
7423   enum cp_parser_prec new_prec, lookahead_prec;
7424   tree overload;
7425
7426   /* Parse the first expression.  */
7427   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7428                                            cast_p, decltype_p, pidk);
7429   current.lhs_type = ERROR_MARK;
7430   current.prec = prec;
7431
7432   if (cp_parser_error_occurred (parser))
7433     return error_mark_node;
7434
7435   for (;;)
7436     {
7437       /* Get an operator token.  */
7438       token = cp_lexer_peek_token (parser->lexer);
7439
7440       if (warn_cxx0x_compat
7441           && token->type == CPP_RSHIFT
7442           && !parser->greater_than_is_operator_p)
7443         {
7444           if (warning_at (token->location, OPT_Wc__0x_compat,
7445                           "%<>>%> operator is treated"
7446                           " as two right angle brackets in C++11"))
7447             inform (token->location,
7448                     "suggest parentheses around %<>>%> expression");
7449         }
7450
7451       new_prec = TOKEN_PRECEDENCE (token);
7452
7453       /* Popping an entry off the stack means we completed a subexpression:
7454          - either we found a token which is not an operator (`>' where it is not
7455            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7456            will happen repeatedly;
7457          - or, we found an operator which has lower priority.  This is the case
7458            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7459            parsing `3 * 4'.  */
7460       if (new_prec <= current.prec)
7461         {
7462           if (sp == stack)
7463             break;
7464           else
7465             goto pop;
7466         }
7467
7468      get_rhs:
7469       current.tree_type = binops_by_token[token->type].tree_type;
7470       current.loc = token->location;
7471
7472       /* We used the operator token.  */
7473       cp_lexer_consume_token (parser->lexer);
7474
7475       /* For "false && x" or "true || x", x will never be executed;
7476          disable warnings while evaluating it.  */
7477       if (current.tree_type == TRUTH_ANDIF_EXPR)
7478         c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7479       else if (current.tree_type == TRUTH_ORIF_EXPR)
7480         c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7481
7482       /* Extract another operand.  It may be the RHS of this expression
7483          or the LHS of a new, higher priority expression.  */
7484       rhs = cp_parser_simple_cast_expression (parser);
7485       rhs_type = ERROR_MARK;
7486
7487       /* Get another operator token.  Look up its precedence to avoid
7488          building a useless (immediately popped) stack entry for common
7489          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7490       token = cp_lexer_peek_token (parser->lexer);
7491       lookahead_prec = TOKEN_PRECEDENCE (token);
7492       if (lookahead_prec > new_prec)
7493         {
7494           /* ... and prepare to parse the RHS of the new, higher priority
7495              expression.  Since precedence levels on the stack are
7496              monotonically increasing, we do not have to care about
7497              stack overflows.  */
7498           *sp = current;
7499           ++sp;
7500           current.lhs = rhs;
7501           current.lhs_type = rhs_type;
7502           current.prec = new_prec;
7503           new_prec = lookahead_prec;
7504           goto get_rhs;
7505
7506          pop:
7507           lookahead_prec = new_prec;
7508           /* If the stack is not empty, we have parsed into LHS the right side
7509              (`4' in the example above) of an expression we had suspended.
7510              We can use the information on the stack to recover the LHS (`3')
7511              from the stack together with the tree code (`MULT_EXPR'), and
7512              the precedence of the higher level subexpression
7513              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7514              which will be used to actually build the additive expression.  */
7515           rhs = current.lhs;
7516           rhs_type = current.lhs_type;
7517           --sp;
7518           current = *sp;
7519         }
7520
7521       /* Undo the disabling of warnings done above.  */
7522       if (current.tree_type == TRUTH_ANDIF_EXPR)
7523         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7524       else if (current.tree_type == TRUTH_ORIF_EXPR)
7525         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7526
7527       overload = NULL;
7528       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7529          ERROR_MARK for everything that is not a binary expression.
7530          This makes warn_about_parentheses miss some warnings that
7531          involve unary operators.  For unary expressions we should
7532          pass the correct tree_code unless the unary expression was
7533          surrounded by parentheses.
7534       */
7535       if (no_toplevel_fold_p
7536           && lookahead_prec <= current.prec
7537           && sp == stack
7538           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7539         current.lhs = build2 (current.tree_type, boolean_type_node,
7540                               current.lhs, rhs);
7541       else
7542         current.lhs = build_x_binary_op (current.loc, current.tree_type,
7543                                          current.lhs, current.lhs_type,
7544                                          rhs, rhs_type, &overload,
7545                                          complain_flags (decltype_p));
7546       current.lhs_type = current.tree_type;
7547       if (EXPR_P (current.lhs))
7548         SET_EXPR_LOCATION (current.lhs, current.loc);
7549
7550       /* If the binary operator required the use of an overloaded operator,
7551          then this expression cannot be an integral constant-expression.
7552          An overloaded operator can be used even if both operands are
7553          otherwise permissible in an integral constant-expression if at
7554          least one of the operands is of enumeration type.  */
7555
7556       if (overload
7557           && cp_parser_non_integral_constant_expression (parser,
7558                                                          NIC_OVERLOADED))
7559         return error_mark_node;
7560     }
7561
7562   return current.lhs;
7563 }
7564
7565 static tree
7566 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7567                              bool no_toplevel_fold_p,
7568                              enum cp_parser_prec prec,
7569                              cp_id_kind * pidk)
7570 {
7571   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7572                                       /*decltype*/false, prec, pidk);
7573 }
7574
7575 /* Parse the `? expression : assignment-expression' part of a
7576    conditional-expression.  The LOGICAL_OR_EXPR is the
7577    logical-or-expression that started the conditional-expression.
7578    Returns a representation of the entire conditional-expression.
7579
7580    This routine is used by cp_parser_assignment_expression.
7581
7582      ? expression : assignment-expression
7583
7584    GNU Extensions:
7585
7586      ? : assignment-expression */
7587
7588 static tree
7589 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7590 {
7591   tree expr;
7592   tree assignment_expr;
7593   struct cp_token *token;
7594   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7595
7596   /* Consume the `?' token.  */
7597   cp_lexer_consume_token (parser->lexer);
7598   token = cp_lexer_peek_token (parser->lexer);
7599   if (cp_parser_allow_gnu_extensions_p (parser)
7600       && token->type == CPP_COLON)
7601     {
7602       pedwarn (token->location, OPT_Wpedantic, 
7603                "ISO C++ does not allow ?: with omitted middle operand");
7604       /* Implicit true clause.  */
7605       expr = NULL_TREE;
7606       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7607       warn_for_omitted_condop (token->location, logical_or_expr);
7608     }
7609   else
7610     {
7611       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7612       parser->colon_corrects_to_scope_p = false;
7613       /* Parse the expression.  */
7614       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7615       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7616       c_inhibit_evaluation_warnings +=
7617         ((logical_or_expr == truthvalue_true_node)
7618          - (logical_or_expr == truthvalue_false_node));
7619       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7620     }
7621
7622   /* The next token should be a `:'.  */
7623   cp_parser_require (parser, CPP_COLON, RT_COLON);
7624   /* Parse the assignment-expression.  */
7625   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7626   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7627
7628   /* Build the conditional-expression.  */
7629   return build_x_conditional_expr (loc, logical_or_expr,
7630                                    expr,
7631                                    assignment_expr,
7632                                    tf_warning_or_error);
7633 }
7634
7635 /* Parse an assignment-expression.
7636
7637    assignment-expression:
7638      conditional-expression
7639      logical-or-expression assignment-operator assignment_expression
7640      throw-expression
7641
7642    CAST_P is true if this expression is the target of a cast.
7643    DECLTYPE_P is true if this expression is the operand of decltype.
7644
7645    Returns a representation for the expression.  */
7646
7647 static tree
7648 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7649                                  bool decltype_p, cp_id_kind * pidk)
7650 {
7651   tree expr;
7652
7653   /* If the next token is the `throw' keyword, then we're looking at
7654      a throw-expression.  */
7655   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7656     expr = cp_parser_throw_expression (parser);
7657   /* Otherwise, it must be that we are looking at a
7658      logical-or-expression.  */
7659   else
7660     {
7661       /* Parse the binary expressions (logical-or-expression).  */
7662       expr = cp_parser_binary_expression (parser, cast_p, false,
7663                                           decltype_p,
7664                                           PREC_NOT_OPERATOR, pidk);
7665       /* If the next token is a `?' then we're actually looking at a
7666          conditional-expression.  */
7667       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7668         return cp_parser_question_colon_clause (parser, expr);
7669       else
7670         {
7671           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7672
7673           /* If it's an assignment-operator, we're using the second
7674              production.  */
7675           enum tree_code assignment_operator
7676             = cp_parser_assignment_operator_opt (parser);
7677           if (assignment_operator != ERROR_MARK)
7678             {
7679               bool non_constant_p;
7680               location_t saved_input_location;
7681
7682               /* Parse the right-hand side of the assignment.  */
7683               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7684
7685               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7686                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7687
7688               /* An assignment may not appear in a
7689                  constant-expression.  */
7690               if (cp_parser_non_integral_constant_expression (parser,
7691                                                               NIC_ASSIGNMENT))
7692                 return error_mark_node;
7693               /* Build the assignment expression.  Its default
7694                  location is the location of the '=' token.  */
7695               saved_input_location = input_location;
7696               input_location = loc;
7697               expr = build_x_modify_expr (loc, expr,
7698                                           assignment_operator,
7699                                           rhs,
7700                                           complain_flags (decltype_p));
7701               input_location = saved_input_location;
7702             }
7703         }
7704     }
7705
7706   return expr;
7707 }
7708
7709 static tree
7710 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7711                                  cp_id_kind * pidk)
7712 {
7713   return cp_parser_assignment_expression (parser, cast_p,
7714                                           /*decltype*/false, pidk);
7715 }
7716
7717 /* Parse an (optional) assignment-operator.
7718
7719    assignment-operator: one of
7720      = *= /= %= += -= >>= <<= &= ^= |=
7721
7722    GNU Extension:
7723
7724    assignment-operator: one of
7725      <?= >?=
7726
7727    If the next token is an assignment operator, the corresponding tree
7728    code is returned, and the token is consumed.  For example, for
7729    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7730    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7731    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7732    operator, ERROR_MARK is returned.  */
7733
7734 static enum tree_code
7735 cp_parser_assignment_operator_opt (cp_parser* parser)
7736 {
7737   enum tree_code op;
7738   cp_token *token;
7739
7740   /* Peek at the next token.  */
7741   token = cp_lexer_peek_token (parser->lexer);
7742
7743   switch (token->type)
7744     {
7745     case CPP_EQ:
7746       op = NOP_EXPR;
7747       break;
7748
7749     case CPP_MULT_EQ:
7750       op = MULT_EXPR;
7751       break;
7752
7753     case CPP_DIV_EQ:
7754       op = TRUNC_DIV_EXPR;
7755       break;
7756
7757     case CPP_MOD_EQ:
7758       op = TRUNC_MOD_EXPR;
7759       break;
7760
7761     case CPP_PLUS_EQ:
7762       op = PLUS_EXPR;
7763       break;
7764
7765     case CPP_MINUS_EQ:
7766       op = MINUS_EXPR;
7767       break;
7768
7769     case CPP_RSHIFT_EQ:
7770       op = RSHIFT_EXPR;
7771       break;
7772
7773     case CPP_LSHIFT_EQ:
7774       op = LSHIFT_EXPR;
7775       break;
7776
7777     case CPP_AND_EQ:
7778       op = BIT_AND_EXPR;
7779       break;
7780
7781     case CPP_XOR_EQ:
7782       op = BIT_XOR_EXPR;
7783       break;
7784
7785     case CPP_OR_EQ:
7786       op = BIT_IOR_EXPR;
7787       break;
7788
7789     default:
7790       /* Nothing else is an assignment operator.  */
7791       op = ERROR_MARK;
7792     }
7793
7794   /* If it was an assignment operator, consume it.  */
7795   if (op != ERROR_MARK)
7796     cp_lexer_consume_token (parser->lexer);
7797
7798   return op;
7799 }
7800
7801 /* Parse an expression.
7802
7803    expression:
7804      assignment-expression
7805      expression , assignment-expression
7806
7807    CAST_P is true if this expression is the target of a cast.
7808    DECLTYPE_P is true if this expression is the immediate operand of decltype,
7809      except possibly parenthesized or on the RHS of a comma (N3276).
7810
7811    Returns a representation of the expression.  */
7812
7813 static tree
7814 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
7815                       cp_id_kind * pidk)
7816 {
7817   tree expression = NULL_TREE;
7818   location_t loc = UNKNOWN_LOCATION;
7819
7820   while (true)
7821     {
7822       tree assignment_expression;
7823
7824       /* Parse the next assignment-expression.  */
7825       assignment_expression
7826         = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
7827
7828       /* We don't create a temporary for a call that is the immediate operand
7829          of decltype or on the RHS of a comma.  But when we see a comma, we
7830          need to create a temporary for a call on the LHS.  */
7831       if (decltype_p && !processing_template_decl
7832           && TREE_CODE (assignment_expression) == CALL_EXPR
7833           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
7834           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7835         assignment_expression
7836           = build_cplus_new (TREE_TYPE (assignment_expression),
7837                              assignment_expression, tf_warning_or_error);
7838
7839       /* If this is the first assignment-expression, we can just
7840          save it away.  */
7841       if (!expression)
7842         expression = assignment_expression;
7843       else
7844         expression = build_x_compound_expr (loc, expression,
7845                                             assignment_expression,
7846                                             complain_flags (decltype_p));
7847       /* If the next token is not a comma, then we are done with the
7848          expression.  */
7849       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7850         break;
7851       /* Consume the `,'.  */
7852       loc = cp_lexer_peek_token (parser->lexer)->location;
7853       cp_lexer_consume_token (parser->lexer);
7854       /* A comma operator cannot appear in a constant-expression.  */
7855       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7856         expression = error_mark_node;
7857     }
7858
7859   return expression;
7860 }
7861
7862 static inline tree
7863 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7864 {
7865   return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
7866 }
7867
7868 /* Parse a constant-expression.
7869
7870    constant-expression:
7871      conditional-expression
7872
7873   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7874   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7875   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7876   is false, NON_CONSTANT_P should be NULL.  */
7877
7878 static tree
7879 cp_parser_constant_expression (cp_parser* parser,
7880                                bool allow_non_constant_p,
7881                                bool *non_constant_p)
7882 {
7883   bool saved_integral_constant_expression_p;
7884   bool saved_allow_non_integral_constant_expression_p;
7885   bool saved_non_integral_constant_expression_p;
7886   tree expression;
7887
7888   /* It might seem that we could simply parse the
7889      conditional-expression, and then check to see if it were
7890      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7891      one that the compiler can figure out is constant, possibly after
7892      doing some simplifications or optimizations.  The standard has a
7893      precise definition of constant-expression, and we must honor
7894      that, even though it is somewhat more restrictive.
7895
7896      For example:
7897
7898        int i[(2, 3)];
7899
7900      is not a legal declaration, because `(2, 3)' is not a
7901      constant-expression.  The `,' operator is forbidden in a
7902      constant-expression.  However, GCC's constant-folding machinery
7903      will fold this operation to an INTEGER_CST for `3'.  */
7904
7905   /* Save the old settings.  */
7906   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7907   saved_allow_non_integral_constant_expression_p
7908     = parser->allow_non_integral_constant_expression_p;
7909   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7910   /* We are now parsing a constant-expression.  */
7911   parser->integral_constant_expression_p = true;
7912   parser->allow_non_integral_constant_expression_p
7913     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7914   parser->non_integral_constant_expression_p = false;
7915   /* Although the grammar says "conditional-expression", we parse an
7916      "assignment-expression", which also permits "throw-expression"
7917      and the use of assignment operators.  In the case that
7918      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7919      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7920      actually essential that we look for an assignment-expression.
7921      For example, cp_parser_initializer_clauses uses this function to
7922      determine whether a particular assignment-expression is in fact
7923      constant.  */
7924   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7925   /* Restore the old settings.  */
7926   parser->integral_constant_expression_p
7927     = saved_integral_constant_expression_p;
7928   parser->allow_non_integral_constant_expression_p
7929     = saved_allow_non_integral_constant_expression_p;
7930   if (cxx_dialect >= cxx0x)
7931     {
7932       /* Require an rvalue constant expression here; that's what our
7933          callers expect.  Reference constant expressions are handled
7934          separately in e.g. cp_parser_template_argument.  */
7935       bool is_const = potential_rvalue_constant_expression (expression);
7936       parser->non_integral_constant_expression_p = !is_const;
7937       if (!is_const && !allow_non_constant_p)
7938         require_potential_rvalue_constant_expression (expression);
7939     }
7940   if (allow_non_constant_p)
7941     *non_constant_p = parser->non_integral_constant_expression_p;
7942   parser->non_integral_constant_expression_p
7943     = saved_non_integral_constant_expression_p;
7944
7945   return expression;
7946 }
7947
7948 /* Parse __builtin_offsetof.
7949
7950    offsetof-expression:
7951      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7952
7953    offsetof-member-designator:
7954      id-expression
7955      | offsetof-member-designator "." id-expression
7956      | offsetof-member-designator "[" expression "]"
7957      | offsetof-member-designator "->" id-expression  */
7958
7959 static tree
7960 cp_parser_builtin_offsetof (cp_parser *parser)
7961 {
7962   int save_ice_p, save_non_ice_p;
7963   tree type, expr;
7964   cp_id_kind dummy;
7965   cp_token *token;
7966
7967   /* We're about to accept non-integral-constant things, but will
7968      definitely yield an integral constant expression.  Save and
7969      restore these values around our local parsing.  */
7970   save_ice_p = parser->integral_constant_expression_p;
7971   save_non_ice_p = parser->non_integral_constant_expression_p;
7972
7973   /* Consume the "__builtin_offsetof" token.  */
7974   cp_lexer_consume_token (parser->lexer);
7975   /* Consume the opening `('.  */
7976   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7977   /* Parse the type-id.  */
7978   type = cp_parser_type_id (parser);
7979   /* Look for the `,'.  */
7980   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7981   token = cp_lexer_peek_token (parser->lexer);
7982
7983   /* Build the (type *)null that begins the traditional offsetof macro.  */
7984   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7985                             tf_warning_or_error);
7986
7987   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7988   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7989                                                  true, &dummy, token->location);
7990   while (true)
7991     {
7992       token = cp_lexer_peek_token (parser->lexer);
7993       switch (token->type)
7994         {
7995         case CPP_OPEN_SQUARE:
7996           /* offsetof-member-designator "[" expression "]" */
7997           expr = cp_parser_postfix_open_square_expression (parser, expr,
7998                                                            true, false);
7999           break;
8000
8001         case CPP_DEREF:
8002           /* offsetof-member-designator "->" identifier */
8003           expr = grok_array_decl (token->location, expr,
8004                                   integer_zero_node, false);
8005           /* FALLTHRU */
8006
8007         case CPP_DOT:
8008           /* offsetof-member-designator "." identifier */
8009           cp_lexer_consume_token (parser->lexer);
8010           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8011                                                          expr, true, &dummy,
8012                                                          token->location);
8013           break;
8014
8015         case CPP_CLOSE_PAREN:
8016           /* Consume the ")" token.  */
8017           cp_lexer_consume_token (parser->lexer);
8018           goto success;
8019
8020         default:
8021           /* Error.  We know the following require will fail, but
8022              that gives the proper error message.  */
8023           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8024           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8025           expr = error_mark_node;
8026           goto failure;
8027         }
8028     }
8029
8030  success:
8031   /* If we're processing a template, we can't finish the semantics yet.
8032      Otherwise we can fold the entire expression now.  */
8033   if (processing_template_decl)
8034     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8035   else
8036     expr = finish_offsetof (expr);
8037
8038  failure:
8039   parser->integral_constant_expression_p = save_ice_p;
8040   parser->non_integral_constant_expression_p = save_non_ice_p;
8041
8042   return expr;
8043 }
8044
8045 /* Parse a trait expression.
8046
8047    Returns a representation of the expression, the underlying type
8048    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
8049
8050 static tree
8051 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8052 {
8053   cp_trait_kind kind;
8054   tree type1, type2 = NULL_TREE;
8055   bool binary = false;
8056   cp_decl_specifier_seq decl_specs;
8057
8058   switch (keyword)
8059     {
8060     case RID_HAS_NOTHROW_ASSIGN:
8061       kind = CPTK_HAS_NOTHROW_ASSIGN;
8062       break;
8063     case RID_HAS_NOTHROW_CONSTRUCTOR:
8064       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8065       break;
8066     case RID_HAS_NOTHROW_COPY:
8067       kind = CPTK_HAS_NOTHROW_COPY;
8068       break;
8069     case RID_HAS_TRIVIAL_ASSIGN:
8070       kind = CPTK_HAS_TRIVIAL_ASSIGN;
8071       break;
8072     case RID_HAS_TRIVIAL_CONSTRUCTOR:
8073       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8074       break;
8075     case RID_HAS_TRIVIAL_COPY:
8076       kind = CPTK_HAS_TRIVIAL_COPY;
8077       break;
8078     case RID_HAS_TRIVIAL_DESTRUCTOR:
8079       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8080       break;
8081     case RID_HAS_VIRTUAL_DESTRUCTOR:
8082       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8083       break;
8084     case RID_IS_ABSTRACT:
8085       kind = CPTK_IS_ABSTRACT;
8086       break;
8087     case RID_IS_BASE_OF:
8088       kind = CPTK_IS_BASE_OF;
8089       binary = true;
8090       break;
8091     case RID_IS_CLASS:
8092       kind = CPTK_IS_CLASS;
8093       break;
8094     case RID_IS_CONVERTIBLE_TO:
8095       kind = CPTK_IS_CONVERTIBLE_TO;
8096       binary = true;
8097       break;
8098     case RID_IS_EMPTY:
8099       kind = CPTK_IS_EMPTY;
8100       break;
8101     case RID_IS_ENUM:
8102       kind = CPTK_IS_ENUM;
8103       break;
8104     case RID_IS_FINAL:
8105       kind = CPTK_IS_FINAL;
8106       break;
8107     case RID_IS_LITERAL_TYPE:
8108       kind = CPTK_IS_LITERAL_TYPE;
8109       break;
8110     case RID_IS_POD:
8111       kind = CPTK_IS_POD;
8112       break;
8113     case RID_IS_POLYMORPHIC:
8114       kind = CPTK_IS_POLYMORPHIC;
8115       break;
8116     case RID_IS_STD_LAYOUT:
8117       kind = CPTK_IS_STD_LAYOUT;
8118       break;
8119     case RID_IS_TRIVIAL:
8120       kind = CPTK_IS_TRIVIAL;
8121       break;
8122     case RID_IS_UNION:
8123       kind = CPTK_IS_UNION;
8124       break;
8125     case RID_UNDERLYING_TYPE:
8126       kind = CPTK_UNDERLYING_TYPE;
8127       break;
8128     case RID_BASES:
8129       kind = CPTK_BASES;
8130       break;
8131     case RID_DIRECT_BASES:
8132       kind = CPTK_DIRECT_BASES;
8133       break;
8134     default:
8135       gcc_unreachable ();
8136     }
8137
8138   /* Consume the token.  */
8139   cp_lexer_consume_token (parser->lexer);
8140
8141   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8142
8143   type1 = cp_parser_type_id (parser);
8144
8145   if (type1 == error_mark_node)
8146     return error_mark_node;
8147
8148   /* Build a trivial decl-specifier-seq.  */
8149   clear_decl_specs (&decl_specs);
8150   decl_specs.type = type1;
8151
8152   /* Call grokdeclarator to figure out what type this is.  */
8153   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8154                           /*initialized=*/0, /*attrlist=*/NULL);
8155
8156   if (binary)
8157     {
8158       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8159  
8160       type2 = cp_parser_type_id (parser);
8161
8162       if (type2 == error_mark_node)
8163         return error_mark_node;
8164
8165       /* Build a trivial decl-specifier-seq.  */
8166       clear_decl_specs (&decl_specs);
8167       decl_specs.type = type2;
8168
8169       /* Call grokdeclarator to figure out what type this is.  */
8170       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8171                               /*initialized=*/0, /*attrlist=*/NULL);
8172     }
8173
8174   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8175
8176   /* Complete the trait expression, which may mean either processing
8177      the trait expr now or saving it for template instantiation.  */
8178   switch(kind)
8179     {
8180     case CPTK_UNDERLYING_TYPE:
8181       return finish_underlying_type (type1);
8182     case CPTK_BASES:
8183       return finish_bases (type1, false);
8184     case CPTK_DIRECT_BASES:
8185       return finish_bases (type1, true);
8186     default:
8187       return finish_trait_expr (kind, type1, type2);
8188     }
8189 }
8190
8191 /* Lambdas that appear in variable initializer or default argument scope
8192    get that in their mangling, so we need to record it.  We might as well
8193    use the count for function and namespace scopes as well.  */
8194 static GTY(()) tree lambda_scope;
8195 static GTY(()) int lambda_count;
8196 typedef struct GTY(()) tree_int
8197 {
8198   tree t;
8199   int i;
8200 } tree_int;
8201 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8202
8203 static void
8204 start_lambda_scope (tree decl)
8205 {
8206   tree_int ti;
8207   gcc_assert (decl);
8208   /* Once we're inside a function, we ignore other scopes and just push
8209      the function again so that popping works properly.  */
8210   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8211     decl = current_function_decl;
8212   ti.t = lambda_scope;
8213   ti.i = lambda_count;
8214   vec_safe_push (lambda_scope_stack, ti);
8215   if (lambda_scope != decl)
8216     {
8217       /* Don't reset the count if we're still in the same function.  */
8218       lambda_scope = decl;
8219       lambda_count = 0;
8220     }
8221 }
8222
8223 static void
8224 record_lambda_scope (tree lambda)
8225 {
8226   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8227   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8228 }
8229
8230 static void
8231 finish_lambda_scope (void)
8232 {
8233   tree_int *p = &lambda_scope_stack->last ();
8234   if (lambda_scope != p->t)
8235     {
8236       lambda_scope = p->t;
8237       lambda_count = p->i;
8238     }
8239   lambda_scope_stack->pop ();
8240 }
8241
8242 /* Parse a lambda expression.
8243
8244    lambda-expression:
8245      lambda-introducer lambda-declarator [opt] compound-statement
8246
8247    Returns a representation of the expression.  */
8248
8249 static tree
8250 cp_parser_lambda_expression (cp_parser* parser)
8251 {
8252   tree lambda_expr = build_lambda_expr ();
8253   tree type;
8254   bool ok;
8255
8256   LAMBDA_EXPR_LOCATION (lambda_expr)
8257     = cp_lexer_peek_token (parser->lexer)->location;
8258
8259   if (cp_unevaluated_operand)
8260     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8261               "lambda-expression in unevaluated context");
8262
8263   /* We may be in the middle of deferred access check.  Disable
8264      it now.  */
8265   push_deferring_access_checks (dk_no_deferred);
8266
8267   cp_parser_lambda_introducer (parser, lambda_expr);
8268
8269   type = begin_lambda_type (lambda_expr);
8270   if (type == error_mark_node)
8271     return error_mark_node;
8272
8273   record_lambda_scope (lambda_expr);
8274
8275   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8276   determine_visibility (TYPE_NAME (type));
8277
8278   /* Now that we've started the type, add the capture fields for any
8279      explicit captures.  */
8280   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8281
8282   {
8283     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8284     unsigned int saved_num_template_parameter_lists
8285         = parser->num_template_parameter_lists;
8286     unsigned char in_statement = parser->in_statement;
8287     bool in_switch_statement_p = parser->in_switch_statement_p;
8288
8289     parser->num_template_parameter_lists = 0;
8290     parser->in_statement = 0;
8291     parser->in_switch_statement_p = false;
8292
8293     /* By virtue of defining a local class, a lambda expression has access to
8294        the private variables of enclosing classes.  */
8295
8296     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8297
8298     if (ok)
8299       cp_parser_lambda_body (parser, lambda_expr);
8300     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8301       cp_parser_skip_to_end_of_block_or_statement (parser);
8302
8303     /* The capture list was built up in reverse order; fix that now.  */
8304     {
8305       tree newlist = NULL_TREE;
8306       tree elt, next;
8307
8308       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8309            elt; elt = next)
8310         {
8311           next = TREE_CHAIN (elt);
8312           TREE_CHAIN (elt) = newlist;
8313           newlist = elt;
8314         }
8315       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8316     }
8317
8318     if (ok)
8319       maybe_add_lambda_conv_op (type);
8320
8321     type = finish_struct (type, /*attributes=*/NULL_TREE);
8322
8323     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8324     parser->in_statement = in_statement;
8325     parser->in_switch_statement_p = in_switch_statement_p;
8326   }
8327
8328   pop_deferring_access_checks ();
8329
8330   /* This field is only used during parsing of the lambda.  */
8331   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8332
8333   /* This lambda shouldn't have any proxies left at this point.  */
8334   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8335   /* And now that we're done, push proxies for an enclosing lambda.  */
8336   insert_pending_capture_proxies ();
8337
8338   if (ok)
8339     return build_lambda_object (lambda_expr);
8340   else
8341     return error_mark_node;
8342 }
8343
8344 /* Parse the beginning of a lambda expression.
8345
8346    lambda-introducer:
8347      [ lambda-capture [opt] ]
8348
8349    LAMBDA_EXPR is the current representation of the lambda expression.  */
8350
8351 static void
8352 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8353 {
8354   /* Need commas after the first capture.  */
8355   bool first = true;
8356
8357   /* Eat the leading `['.  */
8358   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8359
8360   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8361   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8362       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8363     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8364   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8365     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8366
8367   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8368     {
8369       cp_lexer_consume_token (parser->lexer);
8370       first = false;
8371     }
8372
8373   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8374     {
8375       cp_token* capture_token;
8376       tree capture_id;
8377       tree capture_init_expr;
8378       cp_id_kind idk = CP_ID_KIND_NONE;
8379       bool explicit_init_p = false;
8380
8381       enum capture_kind_type
8382       {
8383         BY_COPY,
8384         BY_REFERENCE
8385       };
8386       enum capture_kind_type capture_kind = BY_COPY;
8387
8388       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8389         {
8390           error ("expected end of capture-list");
8391           return;
8392         }
8393
8394       if (first)
8395         first = false;
8396       else
8397         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8398
8399       /* Possibly capture `this'.  */
8400       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8401         {
8402           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8403           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8404             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8405                      "with by-copy capture default");
8406           cp_lexer_consume_token (parser->lexer);
8407           add_capture (lambda_expr,
8408                        /*id=*/this_identifier,
8409                        /*initializer=*/finish_this_expr(),
8410                        /*by_reference_p=*/false,
8411                        explicit_init_p);
8412           continue;
8413         }
8414
8415       /* Remember whether we want to capture as a reference or not.  */
8416       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8417         {
8418           capture_kind = BY_REFERENCE;
8419           cp_lexer_consume_token (parser->lexer);
8420         }
8421
8422       /* Get the identifier.  */
8423       capture_token = cp_lexer_peek_token (parser->lexer);
8424       capture_id = cp_parser_identifier (parser);
8425
8426       if (capture_id == error_mark_node)
8427         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8428            delimiters, but I modified this to stop on unnested ']' as well.  It
8429            was already changed to stop on unnested '}', so the
8430            "closing_parenthesis" name is no more misleading with my change.  */
8431         {
8432           cp_parser_skip_to_closing_parenthesis (parser,
8433                                                  /*recovering=*/true,
8434                                                  /*or_comma=*/true,
8435                                                  /*consume_paren=*/true);
8436           break;
8437         }
8438
8439       /* Find the initializer for this capture.  */
8440       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8441         {
8442           /* An explicit expression exists.  */
8443           cp_lexer_consume_token (parser->lexer);
8444           pedwarn (input_location, OPT_Wpedantic,
8445                    "ISO C++ does not allow initializers "
8446                    "in lambda expression capture lists");
8447           capture_init_expr = cp_parser_assignment_expression (parser,
8448                                                                /*cast_p=*/true,
8449                                                                &idk);
8450           explicit_init_p = true;
8451         }
8452       else
8453         {
8454           const char* error_msg;
8455
8456           /* Turn the identifier into an id-expression.  */
8457           capture_init_expr
8458             = cp_parser_lookup_name
8459                 (parser,
8460                  capture_id,
8461                  none_type,
8462                  /*is_template=*/false,
8463                  /*is_namespace=*/false,
8464                  /*check_dependency=*/true,
8465                  /*ambiguous_decls=*/NULL,
8466                  capture_token->location);
8467
8468           if (capture_init_expr == error_mark_node)
8469             {
8470               unqualified_name_lookup_error (capture_id);
8471               continue;
8472             }
8473           else if (DECL_P (capture_init_expr)
8474                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8475                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8476             {
8477               error_at (capture_token->location,
8478                         "capture of non-variable %qD ",
8479                         capture_init_expr);
8480               inform (0, "%q+#D declared here", capture_init_expr);
8481               continue;
8482             }
8483           if (TREE_CODE (capture_init_expr) == VAR_DECL
8484               && decl_storage_duration (capture_init_expr) != dk_auto)
8485             {
8486               pedwarn (capture_token->location, 0, "capture of variable "
8487                        "%qD with non-automatic storage duration",
8488                        capture_init_expr);
8489               inform (0, "%q+#D declared here", capture_init_expr);
8490               continue;
8491             }
8492
8493           capture_init_expr
8494             = finish_id_expression
8495                 (capture_id,
8496                  capture_init_expr,
8497                  parser->scope,
8498                  &idk,
8499                  /*integral_constant_expression_p=*/false,
8500                  /*allow_non_integral_constant_expression_p=*/false,
8501                  /*non_integral_constant_expression_p=*/NULL,
8502                  /*template_p=*/false,
8503                  /*done=*/true,
8504                  /*address_p=*/false,
8505                  /*template_arg_p=*/false,
8506                  &error_msg,
8507                  capture_token->location);
8508         }
8509
8510       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8511           && !explicit_init_p)
8512         {
8513           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8514               && capture_kind == BY_COPY)
8515             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8516                      "of %qD redundant with by-copy capture default",
8517                      capture_id);
8518           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8519               && capture_kind == BY_REFERENCE)
8520             pedwarn (capture_token->location, 0, "explicit by-reference "
8521                      "capture of %qD redundant with by-reference capture "
8522                      "default", capture_id);
8523         }
8524
8525       add_capture (lambda_expr,
8526                    capture_id,
8527                    capture_init_expr,
8528                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8529                    explicit_init_p);
8530     }
8531
8532   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8533 }
8534
8535 /* Parse the (optional) middle of a lambda expression.
8536
8537    lambda-declarator:
8538      ( parameter-declaration-clause [opt] )
8539        attribute-specifier [opt]
8540        mutable [opt]
8541        exception-specification [opt]
8542        lambda-return-type-clause [opt]
8543
8544    LAMBDA_EXPR is the current representation of the lambda expression.  */
8545
8546 static bool
8547 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8548 {
8549   /* 5.1.1.4 of the standard says:
8550        If a lambda-expression does not include a lambda-declarator, it is as if
8551        the lambda-declarator were ().
8552      This means an empty parameter list, no attributes, and no exception
8553      specification.  */
8554   tree param_list = void_list_node;
8555   tree attributes = NULL_TREE;
8556   tree exception_spec = NULL_TREE;
8557   tree t;
8558
8559   /* The lambda-declarator is optional, but must begin with an opening
8560      parenthesis if present.  */
8561   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8562     {
8563       cp_lexer_consume_token (parser->lexer);
8564
8565       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8566
8567       /* Parse parameters.  */
8568       param_list = cp_parser_parameter_declaration_clause (parser);
8569
8570       /* Default arguments shall not be specified in the
8571          parameter-declaration-clause of a lambda-declarator.  */
8572       for (t = param_list; t; t = TREE_CHAIN (t))
8573         if (TREE_PURPOSE (t))
8574           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8575                    "default argument specified for lambda parameter");
8576
8577       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8578
8579       attributes = cp_parser_attributes_opt (parser);
8580
8581       /* Parse optional `mutable' keyword.  */
8582       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8583         {
8584           cp_lexer_consume_token (parser->lexer);
8585           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8586         }
8587
8588       /* Parse optional exception specification.  */
8589       exception_spec = cp_parser_exception_specification_opt (parser);
8590
8591       /* Parse optional trailing return type.  */
8592       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8593         {
8594           cp_lexer_consume_token (parser->lexer);
8595           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8596         }
8597
8598       /* The function parameters must be in scope all the way until after the
8599          trailing-return-type in case of decltype.  */
8600       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8601         pop_binding (DECL_NAME (t), t);
8602
8603       leave_scope ();
8604     }
8605
8606   /* Create the function call operator.
8607
8608      Messing with declarators like this is no uglier than building up the
8609      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8610      other code.  */
8611   {
8612     cp_decl_specifier_seq return_type_specs;
8613     cp_declarator* declarator;
8614     tree fco;
8615     int quals;
8616     void *p;
8617
8618     clear_decl_specs (&return_type_specs);
8619     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8620       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8621     else
8622       /* Maybe we will deduce the return type later.  */
8623       return_type_specs.type = make_auto ();
8624
8625     p = obstack_alloc (&declarator_obstack, 0);
8626
8627     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8628                                      sfk_none);
8629
8630     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8631              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8632     declarator = make_call_declarator (declarator, param_list, quals,
8633                                        VIRT_SPEC_UNSPECIFIED,
8634                                        REF_QUAL_NONE,
8635                                        exception_spec,
8636                                        /*late_return_type=*/NULL_TREE);
8637     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8638
8639     fco = grokmethod (&return_type_specs,
8640                       declarator,
8641                       attributes);
8642     if (fco != error_mark_node)
8643       {
8644         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8645         DECL_ARTIFICIAL (fco) = 1;
8646         /* Give the object parameter a different name.  */
8647         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8648       }
8649
8650     finish_member_declaration (fco);
8651
8652     obstack_free (&declarator_obstack, p);
8653
8654     return (fco != error_mark_node);
8655   }
8656 }
8657
8658 /* Parse the body of a lambda expression, which is simply
8659
8660    compound-statement
8661
8662    but which requires special handling.
8663    LAMBDA_EXPR is the current representation of the lambda expression.  */
8664
8665 static void
8666 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8667 {
8668   bool nested = (current_function_decl != NULL_TREE);
8669   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8670   if (nested)
8671     push_function_context ();
8672   else
8673     /* Still increment function_depth so that we don't GC in the
8674        middle of an expression.  */
8675     ++function_depth;
8676   /* Clear this in case we're in the middle of a default argument.  */
8677   parser->local_variables_forbidden_p = false;
8678
8679   /* Finish the function call operator
8680      - class_specifier
8681      + late_parsing_for_member
8682      + function_definition_after_declarator
8683      + ctor_initializer_opt_and_function_body  */
8684   {
8685     tree fco = lambda_function (lambda_expr);
8686     tree body;
8687     bool done = false;
8688     tree compound_stmt;
8689     tree cap;
8690
8691     /* Let the front end know that we are going to be defining this
8692        function.  */
8693     start_preparsed_function (fco,
8694                               NULL_TREE,
8695                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8696
8697     start_lambda_scope (fco);
8698     body = begin_function_body ();
8699
8700     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8701       goto out;
8702
8703     /* Push the proxies for any explicit captures.  */
8704     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8705          cap = TREE_CHAIN (cap))
8706       build_capture_proxy (TREE_PURPOSE (cap));
8707
8708     compound_stmt = begin_compound_stmt (0);
8709
8710     /* 5.1.1.4 of the standard says:
8711          If a lambda-expression does not include a trailing-return-type, it
8712          is as if the trailing-return-type denotes the following type:
8713           * if the compound-statement is of the form
8714                { return attribute-specifier [opt] expression ; }
8715              the type of the returned expression after lvalue-to-rvalue
8716              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8717              (_conv.array_ 4.2), and function-to-pointer conversion
8718              (_conv.func_ 4.3);
8719           * otherwise, void.  */
8720
8721     /* In a lambda that has neither a lambda-return-type-clause
8722        nor a deducible form, errors should be reported for return statements
8723        in the body.  Since we used void as the placeholder return type, parsing
8724        the body as usual will give such desired behavior.  */
8725     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8726         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8727         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8728       {
8729         tree expr = NULL_TREE;
8730         cp_id_kind idk = CP_ID_KIND_NONE;
8731
8732         /* Parse tentatively in case there's more after the initial return
8733            statement.  */
8734         cp_parser_parse_tentatively (parser);
8735
8736         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8737
8738         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8739
8740         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8741         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8742
8743         if (cp_parser_parse_definitely (parser))
8744           {
8745             if (!processing_template_decl)
8746               apply_deduced_return_type (fco, lambda_return_type (expr));
8747
8748             /* Will get error here if type not deduced yet.  */
8749             finish_return_stmt (expr);
8750
8751             done = true;
8752           }
8753       }
8754
8755     if (!done)
8756       {
8757         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8758           cp_parser_label_declaration (parser);
8759         cp_parser_statement_seq_opt (parser, NULL_TREE);
8760         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8761       }
8762
8763     finish_compound_stmt (compound_stmt);
8764
8765   out:
8766     finish_function_body (body);
8767     finish_lambda_scope ();
8768
8769     /* Finish the function and generate code for it if necessary.  */
8770     expand_or_defer_fn (finish_function (/*inline*/2));
8771   }
8772
8773   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8774   if (nested)
8775     pop_function_context();
8776   else
8777     --function_depth;
8778 }
8779
8780 /* Statements [gram.stmt.stmt]  */
8781
8782 /* Parse a statement.
8783
8784    statement:
8785      labeled-statement
8786      expression-statement
8787      compound-statement
8788      selection-statement
8789      iteration-statement
8790      jump-statement
8791      declaration-statement
8792      try-block
8793
8794   C++11:
8795
8796   statement:
8797     labeled-statement
8798     attribute-specifier-seq (opt) expression-statement
8799     attribute-specifier-seq (opt) compound-statement
8800     attribute-specifier-seq (opt) selection-statement
8801     attribute-specifier-seq (opt) iteration-statement
8802     attribute-specifier-seq (opt) jump-statement
8803     declaration-statement
8804     attribute-specifier-seq (opt) try-block
8805
8806   TM Extension:
8807
8808    statement:
8809      atomic-statement
8810
8811   IN_COMPOUND is true when the statement is nested inside a
8812   cp_parser_compound_statement; this matters for certain pragmas.
8813
8814   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8815   is a (possibly labeled) if statement which is not enclosed in braces
8816   and has an else clause.  This is used to implement -Wparentheses.  */
8817
8818 static void
8819 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8820                      bool in_compound, bool *if_p)
8821 {
8822   tree statement, std_attrs = NULL_TREE;
8823   cp_token *token;
8824   location_t statement_location, attrs_location;
8825
8826  restart:
8827   if (if_p != NULL)
8828     *if_p = false;
8829   /* There is no statement yet.  */
8830   statement = NULL_TREE;
8831
8832   cp_lexer_save_tokens (parser->lexer);
8833   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8834   if (c_dialect_objc ())
8835     /* In obj-c++, seing '[[' might be the either the beginning of
8836        c++11 attributes, or a nested objc-message-expression.  So
8837        let's parse the c++11 attributes tentatively.  */
8838     cp_parser_parse_tentatively (parser);
8839   std_attrs = cp_parser_std_attribute_spec_seq (parser);
8840   if (c_dialect_objc ())
8841     {
8842       if (!cp_parser_parse_definitely (parser))
8843         std_attrs = NULL_TREE;
8844     }
8845
8846   /* Peek at the next token.  */
8847   token = cp_lexer_peek_token (parser->lexer);
8848   /* Remember the location of the first token in the statement.  */
8849   statement_location = token->location;
8850   /* If this is a keyword, then that will often determine what kind of
8851      statement we have.  */
8852   if (token->type == CPP_KEYWORD)
8853     {
8854       enum rid keyword = token->keyword;
8855
8856       switch (keyword)
8857         {
8858         case RID_CASE:
8859         case RID_DEFAULT:
8860           /* Looks like a labeled-statement with a case label.
8861              Parse the label, and then use tail recursion to parse
8862              the statement.  */
8863           cp_parser_label_for_labeled_statement (parser, std_attrs);
8864           goto restart;
8865
8866         case RID_IF:
8867         case RID_SWITCH:
8868           statement = cp_parser_selection_statement (parser, if_p);
8869           break;
8870
8871         case RID_WHILE:
8872         case RID_DO:
8873         case RID_FOR:
8874           statement = cp_parser_iteration_statement (parser);
8875           break;
8876
8877         case RID_BREAK:
8878         case RID_CONTINUE:
8879         case RID_RETURN:
8880         case RID_GOTO:
8881           statement = cp_parser_jump_statement (parser);
8882           break;
8883
8884           /* Objective-C++ exception-handling constructs.  */
8885         case RID_AT_TRY:
8886         case RID_AT_CATCH:
8887         case RID_AT_FINALLY:
8888         case RID_AT_SYNCHRONIZED:
8889         case RID_AT_THROW:
8890           statement = cp_parser_objc_statement (parser);
8891           break;
8892
8893         case RID_TRY:
8894           statement = cp_parser_try_block (parser);
8895           break;
8896
8897         case RID_NAMESPACE:
8898           /* This must be a namespace alias definition.  */
8899           cp_parser_declaration_statement (parser);
8900           return;
8901           
8902         case RID_TRANSACTION_ATOMIC:
8903         case RID_TRANSACTION_RELAXED:
8904           statement = cp_parser_transaction (parser, keyword);
8905           break;
8906         case RID_TRANSACTION_CANCEL:
8907           statement = cp_parser_transaction_cancel (parser);
8908           break;
8909
8910         default:
8911           /* It might be a keyword like `int' that can start a
8912              declaration-statement.  */
8913           break;
8914         }
8915     }
8916   else if (token->type == CPP_NAME)
8917     {
8918       /* If the next token is a `:', then we are looking at a
8919          labeled-statement.  */
8920       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8921       if (token->type == CPP_COLON)
8922         {
8923           /* Looks like a labeled-statement with an ordinary label.
8924              Parse the label, and then use tail recursion to parse
8925              the statement.  */
8926
8927           cp_parser_label_for_labeled_statement (parser, std_attrs);
8928           goto restart;
8929         }
8930     }
8931   /* Anything that starts with a `{' must be a compound-statement.  */
8932   else if (token->type == CPP_OPEN_BRACE)
8933     statement = cp_parser_compound_statement (parser, NULL, false, false);
8934   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8935      a statement all its own.  */
8936   else if (token->type == CPP_PRAGMA)
8937     {
8938       /* Only certain OpenMP pragmas are attached to statements, and thus
8939          are considered statements themselves.  All others are not.  In
8940          the context of a compound, accept the pragma as a "statement" and
8941          return so that we can check for a close brace.  Otherwise we
8942          require a real statement and must go back and read one.  */
8943       if (in_compound)
8944         cp_parser_pragma (parser, pragma_compound);
8945       else if (!cp_parser_pragma (parser, pragma_stmt))
8946         goto restart;
8947       return;
8948     }
8949   else if (token->type == CPP_EOF)
8950     {
8951       cp_parser_error (parser, "expected statement");
8952       return;
8953     }
8954
8955   /* Everything else must be a declaration-statement or an
8956      expression-statement.  Try for the declaration-statement
8957      first, unless we are looking at a `;', in which case we know that
8958      we have an expression-statement.  */
8959   if (!statement)
8960     {
8961       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8962         {
8963           if (std_attrs != NULL_TREE)
8964             {
8965               /*  Attributes should be parsed as part of the the
8966                   declaration, so let's un-parse them.  */
8967               cp_lexer_rollback_tokens (parser->lexer);
8968               std_attrs = NULL_TREE;
8969             }
8970
8971           cp_parser_parse_tentatively (parser);
8972           /* Try to parse the declaration-statement.  */
8973           cp_parser_declaration_statement (parser);
8974           /* If that worked, we're done.  */
8975           if (cp_parser_parse_definitely (parser))
8976             return;
8977         }
8978       /* Look for an expression-statement instead.  */
8979       statement = cp_parser_expression_statement (parser, in_statement_expr);
8980     }
8981
8982   /* Set the line number for the statement.  */
8983   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8984     SET_EXPR_LOCATION (statement, statement_location);
8985
8986   /* Note that for now, we don't do anything with c++11 statements
8987      parsed at this level.  */
8988   if (std_attrs != NULL_TREE)
8989     warning_at (attrs_location,
8990                 OPT_Wattributes,
8991                 "attributes at the beginning of statement are ignored");
8992 }
8993
8994 /* Parse the label for a labeled-statement, i.e.
8995
8996    identifier :
8997    case constant-expression :
8998    default :
8999
9000    GNU Extension:
9001    case constant-expression ... constant-expression : statement
9002
9003    When a label is parsed without errors, the label is added to the
9004    parse tree by the finish_* functions, so this function doesn't
9005    have to return the label.  */
9006
9007 static void
9008 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9009 {
9010   cp_token *token;
9011   tree label = NULL_TREE;
9012   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9013
9014   /* The next token should be an identifier.  */
9015   token = cp_lexer_peek_token (parser->lexer);
9016   if (token->type != CPP_NAME
9017       && token->type != CPP_KEYWORD)
9018     {
9019       cp_parser_error (parser, "expected labeled-statement");
9020       return;
9021     }
9022
9023   parser->colon_corrects_to_scope_p = false;
9024   switch (token->keyword)
9025     {
9026     case RID_CASE:
9027       {
9028         tree expr, expr_hi;
9029         cp_token *ellipsis;
9030
9031         /* Consume the `case' token.  */
9032         cp_lexer_consume_token (parser->lexer);
9033         /* Parse the constant-expression.  */
9034         expr = cp_parser_constant_expression (parser,
9035                                               /*allow_non_constant_p=*/false,
9036                                               NULL);
9037
9038         ellipsis = cp_lexer_peek_token (parser->lexer);
9039         if (ellipsis->type == CPP_ELLIPSIS)
9040           {
9041             /* Consume the `...' token.  */
9042             cp_lexer_consume_token (parser->lexer);
9043             expr_hi =
9044               cp_parser_constant_expression (parser,
9045                                              /*allow_non_constant_p=*/false,
9046                                              NULL);
9047             /* We don't need to emit warnings here, as the common code
9048                will do this for us.  */
9049           }
9050         else
9051           expr_hi = NULL_TREE;
9052
9053         if (parser->in_switch_statement_p)
9054           finish_case_label (token->location, expr, expr_hi);
9055         else
9056           error_at (token->location,
9057                     "case label %qE not within a switch statement",
9058                     expr);
9059       }
9060       break;
9061
9062     case RID_DEFAULT:
9063       /* Consume the `default' token.  */
9064       cp_lexer_consume_token (parser->lexer);
9065
9066       if (parser->in_switch_statement_p)
9067         finish_case_label (token->location, NULL_TREE, NULL_TREE);
9068       else
9069         error_at (token->location, "case label not within a switch statement");
9070       break;
9071
9072     default:
9073       /* Anything else must be an ordinary label.  */
9074       label = finish_label_stmt (cp_parser_identifier (parser));
9075       break;
9076     }
9077
9078   /* Require the `:' token.  */
9079   cp_parser_require (parser, CPP_COLON, RT_COLON);
9080
9081   /* An ordinary label may optionally be followed by attributes.
9082      However, this is only permitted if the attributes are then
9083      followed by a semicolon.  This is because, for backward
9084      compatibility, when parsing
9085        lab: __attribute__ ((unused)) int i;
9086      we want the attribute to attach to "i", not "lab".  */
9087   if (label != NULL_TREE
9088       && cp_next_tokens_can_be_gnu_attribute_p (parser))
9089     {
9090       tree attrs;
9091       cp_parser_parse_tentatively (parser);
9092       attrs = cp_parser_gnu_attributes_opt (parser);
9093       if (attrs == NULL_TREE
9094           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9095         cp_parser_abort_tentative_parse (parser);
9096       else if (!cp_parser_parse_definitely (parser))
9097         ;
9098       else
9099         attributes = chainon (attributes, attrs);
9100     }
9101
9102   if (attributes != NULL_TREE)
9103     cplus_decl_attributes (&label, attributes, 0);
9104
9105   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9106 }
9107
9108 /* Parse an expression-statement.
9109
9110    expression-statement:
9111      expression [opt] ;
9112
9113    Returns the new EXPR_STMT -- or NULL_TREE if the expression
9114    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9115    indicates whether this expression-statement is part of an
9116    expression statement.  */
9117
9118 static tree
9119 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9120 {
9121   tree statement = NULL_TREE;
9122   cp_token *token = cp_lexer_peek_token (parser->lexer);
9123
9124   /* If the next token is a ';', then there is no expression
9125      statement.  */
9126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9127     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9128
9129   /* Give a helpful message for "A<T>::type t;" and the like.  */
9130   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9131       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9132     {
9133       if (TREE_CODE (statement) == SCOPE_REF)
9134         error_at (token->location, "need %<typename%> before %qE because "
9135                   "%qT is a dependent scope",
9136                   statement, TREE_OPERAND (statement, 0));
9137       else if (is_overloaded_fn (statement)
9138                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9139         {
9140           /* A::A a; */
9141           tree fn = get_first_fn (statement);
9142           error_at (token->location,
9143                     "%<%T::%D%> names the constructor, not the type",
9144                     DECL_CONTEXT (fn), DECL_NAME (fn));
9145         }
9146     }
9147
9148   /* Consume the final `;'.  */
9149   cp_parser_consume_semicolon_at_end_of_statement (parser);
9150
9151   if (in_statement_expr
9152       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9153     /* This is the final expression statement of a statement
9154        expression.  */
9155     statement = finish_stmt_expr_expr (statement, in_statement_expr);
9156   else if (statement)
9157     statement = finish_expr_stmt (statement);
9158   else
9159     finish_stmt ();
9160
9161   return statement;
9162 }
9163
9164 /* Parse a compound-statement.
9165
9166    compound-statement:
9167      { statement-seq [opt] }
9168
9169    GNU extension:
9170
9171    compound-statement:
9172      { label-declaration-seq [opt] statement-seq [opt] }
9173
9174    label-declaration-seq:
9175      label-declaration
9176      label-declaration-seq label-declaration
9177
9178    Returns a tree representing the statement.  */
9179
9180 static tree
9181 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9182                               bool in_try, bool function_body)
9183 {
9184   tree compound_stmt;
9185
9186   /* Consume the `{'.  */
9187   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9188     return error_mark_node;
9189   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9190       && !function_body)
9191     pedwarn (input_location, OPT_Wpedantic,
9192              "compound-statement in constexpr function");
9193   /* Begin the compound-statement.  */
9194   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9195   /* If the next keyword is `__label__' we have a label declaration.  */
9196   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9197     cp_parser_label_declaration (parser);
9198   /* Parse an (optional) statement-seq.  */
9199   cp_parser_statement_seq_opt (parser, in_statement_expr);
9200   /* Finish the compound-statement.  */
9201   finish_compound_stmt (compound_stmt);
9202   /* Consume the `}'.  */
9203   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9204
9205   return compound_stmt;
9206 }
9207
9208 /* Parse an (optional) statement-seq.
9209
9210    statement-seq:
9211      statement
9212      statement-seq [opt] statement  */
9213
9214 static void
9215 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9216 {
9217   /* Scan statements until there aren't any more.  */
9218   while (true)
9219     {
9220       cp_token *token = cp_lexer_peek_token (parser->lexer);
9221
9222       /* If we are looking at a `}', then we have run out of
9223          statements; the same is true if we have reached the end
9224          of file, or have stumbled upon a stray '@end'.  */
9225       if (token->type == CPP_CLOSE_BRACE
9226           || token->type == CPP_EOF
9227           || token->type == CPP_PRAGMA_EOL
9228           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9229         break;
9230       
9231       /* If we are in a compound statement and find 'else' then
9232          something went wrong.  */
9233       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9234         {
9235           if (parser->in_statement & IN_IF_STMT) 
9236             break;
9237           else
9238             {
9239               token = cp_lexer_consume_token (parser->lexer);
9240               error_at (token->location, "%<else%> without a previous %<if%>");
9241             }
9242         }
9243
9244       /* Parse the statement.  */
9245       cp_parser_statement (parser, in_statement_expr, true, NULL);
9246     }
9247 }
9248
9249 /* Parse a selection-statement.
9250
9251    selection-statement:
9252      if ( condition ) statement
9253      if ( condition ) statement else statement
9254      switch ( condition ) statement
9255
9256    Returns the new IF_STMT or SWITCH_STMT.
9257
9258    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9259    is a (possibly labeled) if statement which is not enclosed in
9260    braces and has an else clause.  This is used to implement
9261    -Wparentheses.  */
9262
9263 static tree
9264 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9265 {
9266   cp_token *token;
9267   enum rid keyword;
9268
9269   if (if_p != NULL)
9270     *if_p = false;
9271
9272   /* Peek at the next token.  */
9273   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9274
9275   /* See what kind of keyword it is.  */
9276   keyword = token->keyword;
9277   switch (keyword)
9278     {
9279     case RID_IF:
9280     case RID_SWITCH:
9281       {
9282         tree statement;
9283         tree condition;
9284
9285         /* Look for the `('.  */
9286         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9287           {
9288             cp_parser_skip_to_end_of_statement (parser);
9289             return error_mark_node;
9290           }
9291
9292         /* Begin the selection-statement.  */
9293         if (keyword == RID_IF)
9294           statement = begin_if_stmt ();
9295         else
9296           statement = begin_switch_stmt ();
9297
9298         /* Parse the condition.  */
9299         condition = cp_parser_condition (parser);
9300         /* Look for the `)'.  */
9301         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9302           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9303                                                  /*consume_paren=*/true);
9304
9305         if (keyword == RID_IF)
9306           {
9307             bool nested_if;
9308             unsigned char in_statement;
9309
9310             /* Add the condition.  */
9311             finish_if_stmt_cond (condition, statement);
9312
9313             /* Parse the then-clause.  */
9314             in_statement = parser->in_statement;
9315             parser->in_statement |= IN_IF_STMT;
9316             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9317               {
9318                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9319                 add_stmt (build_empty_stmt (loc));
9320                 cp_lexer_consume_token (parser->lexer);
9321                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9322                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9323                               "empty body in an %<if%> statement");
9324                 nested_if = false;
9325               }
9326             else
9327               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9328             parser->in_statement = in_statement;
9329
9330             finish_then_clause (statement);
9331
9332             /* If the next token is `else', parse the else-clause.  */
9333             if (cp_lexer_next_token_is_keyword (parser->lexer,
9334                                                 RID_ELSE))
9335               {
9336                 /* Consume the `else' keyword.  */
9337                 cp_lexer_consume_token (parser->lexer);
9338                 begin_else_clause (statement);
9339                 /* Parse the else-clause.  */
9340                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9341                   {
9342                     location_t loc;
9343                     loc = cp_lexer_peek_token (parser->lexer)->location;
9344                     warning_at (loc,
9345                                 OPT_Wempty_body, "suggest braces around "
9346                                 "empty body in an %<else%> statement");
9347                     add_stmt (build_empty_stmt (loc));
9348                     cp_lexer_consume_token (parser->lexer);
9349                   }
9350                 else
9351                   cp_parser_implicitly_scoped_statement (parser, NULL);
9352
9353                 finish_else_clause (statement);
9354
9355                 /* If we are currently parsing a then-clause, then
9356                    IF_P will not be NULL.  We set it to true to
9357                    indicate that this if statement has an else clause.
9358                    This may trigger the Wparentheses warning below
9359                    when we get back up to the parent if statement.  */
9360                 if (if_p != NULL)
9361                   *if_p = true;
9362               }
9363             else
9364               {
9365                 /* This if statement does not have an else clause.  If
9366                    NESTED_IF is true, then the then-clause is an if
9367                    statement which does have an else clause.  We warn
9368                    about the potential ambiguity.  */
9369                 if (nested_if)
9370                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9371                               "suggest explicit braces to avoid ambiguous"
9372                               " %<else%>");
9373               }
9374
9375             /* Now we're all done with the if-statement.  */
9376             finish_if_stmt (statement);
9377           }
9378         else
9379           {
9380             bool in_switch_statement_p;
9381             unsigned char in_statement;
9382
9383             /* Add the condition.  */
9384             finish_switch_cond (condition, statement);
9385
9386             /* Parse the body of the switch-statement.  */
9387             in_switch_statement_p = parser->in_switch_statement_p;
9388             in_statement = parser->in_statement;
9389             parser->in_switch_statement_p = true;
9390             parser->in_statement |= IN_SWITCH_STMT;
9391             cp_parser_implicitly_scoped_statement (parser, NULL);
9392             parser->in_switch_statement_p = in_switch_statement_p;
9393             parser->in_statement = in_statement;
9394
9395             /* Now we're all done with the switch-statement.  */
9396             finish_switch_stmt (statement);
9397           }
9398
9399         return statement;
9400       }
9401       break;
9402
9403     default:
9404       cp_parser_error (parser, "expected selection-statement");
9405       return error_mark_node;
9406     }
9407 }
9408
9409 /* Parse a condition.
9410
9411    condition:
9412      expression
9413      type-specifier-seq declarator = initializer-clause
9414      type-specifier-seq declarator braced-init-list
9415
9416    GNU Extension:
9417
9418    condition:
9419      type-specifier-seq declarator asm-specification [opt]
9420        attributes [opt] = assignment-expression
9421
9422    Returns the expression that should be tested.  */
9423
9424 static tree
9425 cp_parser_condition (cp_parser* parser)
9426 {
9427   cp_decl_specifier_seq type_specifiers;
9428   const char *saved_message;
9429   int declares_class_or_enum;
9430
9431   /* Try the declaration first.  */
9432   cp_parser_parse_tentatively (parser);
9433   /* New types are not allowed in the type-specifier-seq for a
9434      condition.  */
9435   saved_message = parser->type_definition_forbidden_message;
9436   parser->type_definition_forbidden_message
9437     = G_("types may not be defined in conditions");
9438   /* Parse the type-specifier-seq.  */
9439   cp_parser_decl_specifier_seq (parser,
9440                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9441                                 &type_specifiers,
9442                                 &declares_class_or_enum);
9443   /* Restore the saved message.  */
9444   parser->type_definition_forbidden_message = saved_message;
9445   /* If all is well, we might be looking at a declaration.  */
9446   if (!cp_parser_error_occurred (parser))
9447     {
9448       tree decl;
9449       tree asm_specification;
9450       tree attributes;
9451       cp_declarator *declarator;
9452       tree initializer = NULL_TREE;
9453
9454       /* Parse the declarator.  */
9455       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9456                                          /*ctor_dtor_or_conv_p=*/NULL,
9457                                          /*parenthesized_p=*/NULL,
9458                                          /*member_p=*/false);
9459       /* Parse the attributes.  */
9460       attributes = cp_parser_attributes_opt (parser);
9461       /* Parse the asm-specification.  */
9462       asm_specification = cp_parser_asm_specification_opt (parser);
9463       /* If the next token is not an `=' or '{', then we might still be
9464          looking at an expression.  For example:
9465
9466            if (A(a).x)
9467
9468          looks like a decl-specifier-seq and a declarator -- but then
9469          there is no `=', so this is an expression.  */
9470       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9471           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9472         cp_parser_simulate_error (parser);
9473         
9474       /* If we did see an `=' or '{', then we are looking at a declaration
9475          for sure.  */
9476       if (cp_parser_parse_definitely (parser))
9477         {
9478           tree pushed_scope;
9479           bool non_constant_p;
9480           bool flags = LOOKUP_ONLYCONVERTING;
9481
9482           /* Create the declaration.  */
9483           decl = start_decl (declarator, &type_specifiers,
9484                              /*initialized_p=*/true,
9485                              attributes, /*prefix_attributes=*/NULL_TREE,
9486                              &pushed_scope);
9487
9488           /* Parse the initializer.  */
9489           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9490             {
9491               initializer = cp_parser_braced_list (parser, &non_constant_p);
9492               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9493               flags = 0;
9494             }
9495           else
9496             {
9497               /* Consume the `='.  */
9498               cp_parser_require (parser, CPP_EQ, RT_EQ);
9499               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9500             }
9501           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9502             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9503
9504           /* Process the initializer.  */
9505           cp_finish_decl (decl,
9506                           initializer, !non_constant_p,
9507                           asm_specification,
9508                           flags);
9509
9510           if (pushed_scope)
9511             pop_scope (pushed_scope);
9512
9513           return convert_from_reference (decl);
9514         }
9515     }
9516   /* If we didn't even get past the declarator successfully, we are
9517      definitely not looking at a declaration.  */
9518   else
9519     cp_parser_abort_tentative_parse (parser);
9520
9521   /* Otherwise, we are looking at an expression.  */
9522   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9523 }
9524
9525 /* Parses a for-statement or range-for-statement until the closing ')',
9526    not included. */
9527
9528 static tree
9529 cp_parser_for (cp_parser *parser)
9530 {
9531   tree init, scope, decl;
9532   bool is_range_for;
9533
9534   /* Begin the for-statement.  */
9535   scope = begin_for_scope (&init);
9536
9537   /* Parse the initialization.  */
9538   is_range_for = cp_parser_for_init_statement (parser, &decl);
9539
9540   if (is_range_for)
9541     return cp_parser_range_for (parser, scope, init, decl);
9542   else
9543     return cp_parser_c_for (parser, scope, init);
9544 }
9545
9546 static tree
9547 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9548 {
9549   /* Normal for loop */
9550   tree condition = NULL_TREE;
9551   tree expression = NULL_TREE;
9552   tree stmt;
9553
9554   stmt = begin_for_stmt (scope, init);
9555   /* The for-init-statement has already been parsed in
9556      cp_parser_for_init_statement, so no work is needed here.  */
9557   finish_for_init_stmt (stmt);
9558
9559   /* If there's a condition, process it.  */
9560   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9561     condition = cp_parser_condition (parser);
9562   finish_for_cond (condition, stmt);
9563   /* Look for the `;'.  */
9564   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9565
9566   /* If there's an expression, process it.  */
9567   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9568     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9569   finish_for_expr (expression, stmt);
9570
9571   return stmt;
9572 }
9573
9574 /* Tries to parse a range-based for-statement:
9575
9576   range-based-for:
9577     decl-specifier-seq declarator : expression
9578
9579   The decl-specifier-seq declarator and the `:' are already parsed by
9580   cp_parser_for_init_statement. If processing_template_decl it returns a
9581   newly created RANGE_FOR_STMT; if not, it is converted to a
9582   regular FOR_STMT.  */
9583
9584 static tree
9585 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9586 {
9587   tree stmt, range_expr;
9588
9589   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9590     {
9591       bool expr_non_constant_p;
9592       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9593     }
9594   else
9595     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9596
9597   /* If in template, STMT is converted to a normal for-statement
9598      at instantiation. If not, it is done just ahead. */
9599   if (processing_template_decl)
9600     {
9601       if (check_for_bare_parameter_packs (range_expr))
9602         range_expr = error_mark_node;
9603       stmt = begin_range_for_stmt (scope, init);
9604       finish_range_for_decl (stmt, range_decl, range_expr);
9605       if (!type_dependent_expression_p (range_expr)
9606           /* do_auto_deduction doesn't mess with template init-lists.  */
9607           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9608         do_range_for_auto_deduction (range_decl, range_expr);
9609     }
9610   else
9611     {
9612       stmt = begin_for_stmt (scope, init);
9613       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9614     }
9615   return stmt;
9616 }
9617
9618 /* Subroutine of cp_convert_range_for: given the initializer expression,
9619    builds up the range temporary.  */
9620
9621 static tree
9622 build_range_temp (tree range_expr)
9623 {
9624   tree range_type, range_temp;
9625
9626   /* Find out the type deduced by the declaration
9627      `auto &&__range = range_expr'.  */
9628   range_type = cp_build_reference_type (make_auto (), true);
9629   range_type = do_auto_deduction (range_type, range_expr,
9630                                   type_uses_auto (range_type));
9631
9632   /* Create the __range variable.  */
9633   range_temp = build_decl (input_location, VAR_DECL,
9634                            get_identifier ("__for_range"), range_type);
9635   TREE_USED (range_temp) = 1;
9636   DECL_ARTIFICIAL (range_temp) = 1;
9637
9638   return range_temp;
9639 }
9640
9641 /* Used by cp_parser_range_for in template context: we aren't going to
9642    do a full conversion yet, but we still need to resolve auto in the
9643    type of the for-range-declaration if present.  This is basically
9644    a shortcut version of cp_convert_range_for.  */
9645
9646 static void
9647 do_range_for_auto_deduction (tree decl, tree range_expr)
9648 {
9649   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9650   if (auto_node)
9651     {
9652       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9653       range_temp = convert_from_reference (build_range_temp (range_expr));
9654       iter_type = (cp_parser_perform_range_for_lookup
9655                    (range_temp, &begin_dummy, &end_dummy));
9656       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9657       iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9658                                         tf_warning_or_error);
9659       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9660                                             iter_decl, auto_node);
9661     }
9662 }
9663
9664 /* Converts a range-based for-statement into a normal
9665    for-statement, as per the definition.
9666
9667       for (RANGE_DECL : RANGE_EXPR)
9668         BLOCK
9669
9670    should be equivalent to:
9671
9672       {
9673         auto &&__range = RANGE_EXPR;
9674         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9675               __begin != __end;
9676               ++__begin)
9677           {
9678               RANGE_DECL = *__begin;
9679               BLOCK
9680           }
9681       }
9682
9683    If RANGE_EXPR is an array:
9684         BEGIN_EXPR = __range
9685         END_EXPR = __range + ARRAY_SIZE(__range)
9686    Else if RANGE_EXPR has a member 'begin' or 'end':
9687         BEGIN_EXPR = __range.begin()
9688         END_EXPR = __range.end()
9689    Else:
9690         BEGIN_EXPR = begin(__range)
9691         END_EXPR = end(__range);
9692
9693    If __range has a member 'begin' but not 'end', or vice versa, we must
9694    still use the second alternative (it will surely fail, however).
9695    When calling begin()/end() in the third alternative we must use
9696    argument dependent lookup, but always considering 'std' as an associated
9697    namespace.  */
9698
9699 tree
9700 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9701 {
9702   tree begin, end;
9703   tree iter_type, begin_expr, end_expr;
9704   tree condition, expression;
9705
9706   if (range_decl == error_mark_node || range_expr == error_mark_node)
9707     /* If an error happened previously do nothing or else a lot of
9708        unhelpful errors would be issued.  */
9709     begin_expr = end_expr = iter_type = error_mark_node;
9710   else
9711     {
9712       tree range_temp = build_range_temp (range_expr);
9713       pushdecl (range_temp);
9714       cp_finish_decl (range_temp, range_expr,
9715                       /*is_constant_init*/false, NULL_TREE,
9716                       LOOKUP_ONLYCONVERTING);
9717
9718       range_temp = convert_from_reference (range_temp);
9719       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9720                                                       &begin_expr, &end_expr);
9721     }
9722
9723   /* The new for initialization statement.  */
9724   begin = build_decl (input_location, VAR_DECL,
9725                       get_identifier ("__for_begin"), iter_type);
9726   TREE_USED (begin) = 1;
9727   DECL_ARTIFICIAL (begin) = 1;
9728   pushdecl (begin);
9729   cp_finish_decl (begin, begin_expr,
9730                   /*is_constant_init*/false, NULL_TREE,
9731                   LOOKUP_ONLYCONVERTING);
9732
9733   end = build_decl (input_location, VAR_DECL,
9734                     get_identifier ("__for_end"), iter_type);
9735   TREE_USED (end) = 1;
9736   DECL_ARTIFICIAL (end) = 1;
9737   pushdecl (end);
9738   cp_finish_decl (end, end_expr,
9739                   /*is_constant_init*/false, NULL_TREE,
9740                   LOOKUP_ONLYCONVERTING);
9741
9742   finish_for_init_stmt (statement);
9743
9744   /* The new for condition.  */
9745   condition = build_x_binary_op (input_location, NE_EXPR,
9746                                  begin, ERROR_MARK,
9747                                  end, ERROR_MARK,
9748                                  NULL, tf_warning_or_error);
9749   finish_for_cond (condition, statement);
9750
9751   /* The new increment expression.  */
9752   expression = finish_unary_op_expr (input_location,
9753                                      PREINCREMENT_EXPR, begin,
9754                                      tf_warning_or_error);
9755   finish_for_expr (expression, statement);
9756
9757   /* The declaration is initialized with *__begin inside the loop body.  */
9758   cp_finish_decl (range_decl,
9759                   build_x_indirect_ref (input_location, begin, RO_NULL,
9760                                         tf_warning_or_error),
9761                   /*is_constant_init*/false, NULL_TREE,
9762                   LOOKUP_ONLYCONVERTING);
9763
9764   return statement;
9765 }
9766
9767 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9768    We need to solve both at the same time because the method used
9769    depends on the existence of members begin or end.
9770    Returns the type deduced for the iterator expression.  */
9771
9772 static tree
9773 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9774 {
9775   if (error_operand_p (range))
9776     {
9777       *begin = *end = error_mark_node;
9778       return error_mark_node;
9779     }
9780
9781   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9782     {
9783       error ("range-based %<for%> expression of type %qT "
9784              "has incomplete type", TREE_TYPE (range));
9785       *begin = *end = error_mark_node;
9786       return error_mark_node;
9787     }
9788   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9789     {
9790       /* If RANGE is an array, we will use pointer arithmetic.  */
9791       *begin = range;
9792       *end = build_binary_op (input_location, PLUS_EXPR,
9793                               range,
9794                               array_type_nelts_top (TREE_TYPE (range)),
9795                               0);
9796       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9797     }
9798   else
9799     {
9800       /* If it is not an array, we must do a bit of magic.  */
9801       tree id_begin, id_end;
9802       tree member_begin, member_end;
9803
9804       *begin = *end = error_mark_node;
9805
9806       id_begin = get_identifier ("begin");
9807       id_end = get_identifier ("end");
9808       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9809                                     /*protect=*/2, /*want_type=*/false,
9810                                     tf_warning_or_error);
9811       member_end = lookup_member (TREE_TYPE (range), id_end,
9812                                   /*protect=*/2, /*want_type=*/false,
9813                                   tf_warning_or_error);
9814
9815       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9816         {
9817           /* Use the member functions.  */
9818           if (member_begin != NULL_TREE)
9819             *begin = cp_parser_range_for_member_function (range, id_begin);
9820           else
9821             error ("range-based %<for%> expression of type %qT has an "
9822                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9823
9824           if (member_end != NULL_TREE)
9825             *end = cp_parser_range_for_member_function (range, id_end);
9826           else
9827             error ("range-based %<for%> expression of type %qT has a "
9828                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9829         }
9830       else
9831         {
9832           /* Use global functions with ADL.  */
9833           vec<tree, va_gc> *vec;
9834           vec = make_tree_vector ();
9835
9836           vec_safe_push (vec, range);
9837
9838           member_begin = perform_koenig_lookup (id_begin, vec,
9839                                                 /*include_std=*/true,
9840                                                 tf_warning_or_error);
9841           *begin = finish_call_expr (member_begin, &vec, false, true,
9842                                      tf_warning_or_error);
9843           member_end = perform_koenig_lookup (id_end, vec,
9844                                               /*include_std=*/true,
9845                                               tf_warning_or_error);
9846           *end = finish_call_expr (member_end, &vec, false, true,
9847                                    tf_warning_or_error);
9848
9849           release_tree_vector (vec);
9850         }
9851
9852       /* Last common checks.  */
9853       if (*begin == error_mark_node || *end == error_mark_node)
9854         {
9855           /* If one of the expressions is an error do no more checks.  */
9856           *begin = *end = error_mark_node;
9857           return error_mark_node;
9858         }
9859       else
9860         {
9861           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9862           /* The unqualified type of the __begin and __end temporaries should
9863              be the same, as required by the multiple auto declaration.  */
9864           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9865             error ("inconsistent begin/end types in range-based %<for%> "
9866                    "statement: %qT and %qT",
9867                    TREE_TYPE (*begin), TREE_TYPE (*end));
9868           return iter_type;
9869         }
9870     }
9871 }
9872
9873 /* Helper function for cp_parser_perform_range_for_lookup.
9874    Builds a tree for RANGE.IDENTIFIER().  */
9875
9876 static tree
9877 cp_parser_range_for_member_function (tree range, tree identifier)
9878 {
9879   tree member, res;
9880   vec<tree, va_gc> *vec;
9881
9882   member = finish_class_member_access_expr (range, identifier,
9883                                             false, tf_warning_or_error);
9884   if (member == error_mark_node)
9885     return error_mark_node;
9886
9887   vec = make_tree_vector ();
9888   res = finish_call_expr (member, &vec,
9889                           /*disallow_virtual=*/false,
9890                           /*koenig_p=*/false,
9891                           tf_warning_or_error);
9892   release_tree_vector (vec);
9893   return res;
9894 }
9895
9896 /* Parse an iteration-statement.
9897
9898    iteration-statement:
9899      while ( condition ) statement
9900      do statement while ( expression ) ;
9901      for ( for-init-statement condition [opt] ; expression [opt] )
9902        statement
9903
9904    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9905
9906 static tree
9907 cp_parser_iteration_statement (cp_parser* parser)
9908 {
9909   cp_token *token;
9910   enum rid keyword;
9911   tree statement;
9912   unsigned char in_statement;
9913
9914   /* Peek at the next token.  */
9915   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9916   if (!token)
9917     return error_mark_node;
9918
9919   /* Remember whether or not we are already within an iteration
9920      statement.  */
9921   in_statement = parser->in_statement;
9922
9923   /* See what kind of keyword it is.  */
9924   keyword = token->keyword;
9925   switch (keyword)
9926     {
9927     case RID_WHILE:
9928       {
9929         tree condition;
9930
9931         /* Begin the while-statement.  */
9932         statement = begin_while_stmt ();
9933         /* Look for the `('.  */
9934         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9935         /* Parse the condition.  */
9936         condition = cp_parser_condition (parser);
9937         finish_while_stmt_cond (condition, statement);
9938         /* Look for the `)'.  */
9939         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9940         /* Parse the dependent statement.  */
9941         parser->in_statement = IN_ITERATION_STMT;
9942         cp_parser_already_scoped_statement (parser);
9943         parser->in_statement = in_statement;
9944         /* We're done with the while-statement.  */
9945         finish_while_stmt (statement);
9946       }
9947       break;
9948
9949     case RID_DO:
9950       {
9951         tree expression;
9952
9953         /* Begin the do-statement.  */
9954         statement = begin_do_stmt ();
9955         /* Parse the body of the do-statement.  */
9956         parser->in_statement = IN_ITERATION_STMT;
9957         cp_parser_implicitly_scoped_statement (parser, NULL);
9958         parser->in_statement = in_statement;
9959         finish_do_body (statement);
9960         /* Look for the `while' keyword.  */
9961         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9962         /* Look for the `('.  */
9963         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9964         /* Parse the expression.  */
9965         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9966         /* We're done with the do-statement.  */
9967         finish_do_stmt (expression, statement);
9968         /* Look for the `)'.  */
9969         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9970         /* Look for the `;'.  */
9971         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9972       }
9973       break;
9974
9975     case RID_FOR:
9976       {
9977         /* Look for the `('.  */
9978         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9979
9980         statement = cp_parser_for (parser);
9981
9982         /* Look for the `)'.  */
9983         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9984
9985         /* Parse the body of the for-statement.  */
9986         parser->in_statement = IN_ITERATION_STMT;
9987         cp_parser_already_scoped_statement (parser);
9988         parser->in_statement = in_statement;
9989
9990         /* We're done with the for-statement.  */
9991         finish_for_stmt (statement);
9992       }
9993       break;
9994
9995     default:
9996       cp_parser_error (parser, "expected iteration-statement");
9997       statement = error_mark_node;
9998       break;
9999     }
10000
10001   return statement;
10002 }
10003
10004 /* Parse a for-init-statement or the declarator of a range-based-for.
10005    Returns true if a range-based-for declaration is seen.
10006
10007    for-init-statement:
10008      expression-statement
10009      simple-declaration  */
10010
10011 static bool
10012 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10013 {
10014   /* If the next token is a `;', then we have an empty
10015      expression-statement.  Grammatically, this is also a
10016      simple-declaration, but an invalid one, because it does not
10017      declare anything.  Therefore, if we did not handle this case
10018      specially, we would issue an error message about an invalid
10019      declaration.  */
10020   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10021     {
10022       bool is_range_for = false;
10023       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10024
10025       parser->colon_corrects_to_scope_p = false;
10026
10027       /* We're going to speculatively look for a declaration, falling back
10028          to an expression, if necessary.  */
10029       cp_parser_parse_tentatively (parser);
10030       /* Parse the declaration.  */
10031       cp_parser_simple_declaration (parser,
10032                                     /*function_definition_allowed_p=*/false,
10033                                     decl);
10034       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10035       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10036         {
10037           /* It is a range-for, consume the ':' */
10038           cp_lexer_consume_token (parser->lexer);
10039           is_range_for = true;
10040           if (cxx_dialect < cxx0x)
10041             {
10042               error_at (cp_lexer_peek_token (parser->lexer)->location,
10043                         "range-based %<for%> loops are not allowed "
10044                         "in C++98 mode");
10045               *decl = error_mark_node;
10046             }
10047         }
10048       else
10049           /* The ';' is not consumed yet because we told
10050              cp_parser_simple_declaration not to.  */
10051           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10052
10053       if (cp_parser_parse_definitely (parser))
10054         return is_range_for;
10055       /* If the tentative parse failed, then we shall need to look for an
10056          expression-statement.  */
10057     }
10058   /* If we are here, it is an expression-statement.  */
10059   cp_parser_expression_statement (parser, NULL_TREE);
10060   return false;
10061 }
10062
10063 /* Parse a jump-statement.
10064
10065    jump-statement:
10066      break ;
10067      continue ;
10068      return expression [opt] ;
10069      return braced-init-list ;
10070      goto identifier ;
10071
10072    GNU extension:
10073
10074    jump-statement:
10075      goto * expression ;
10076
10077    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
10078
10079 static tree
10080 cp_parser_jump_statement (cp_parser* parser)
10081 {
10082   tree statement = error_mark_node;
10083   cp_token *token;
10084   enum rid keyword;
10085   unsigned char in_statement;
10086
10087   /* Peek at the next token.  */
10088   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10089   if (!token)
10090     return error_mark_node;
10091
10092   /* See what kind of keyword it is.  */
10093   keyword = token->keyword;
10094   switch (keyword)
10095     {
10096     case RID_BREAK:
10097       in_statement = parser->in_statement & ~IN_IF_STMT;      
10098       switch (in_statement)
10099         {
10100         case 0:
10101           error_at (token->location, "break statement not within loop or switch");
10102           break;
10103         default:
10104           gcc_assert ((in_statement & IN_SWITCH_STMT)
10105                       || in_statement == IN_ITERATION_STMT);
10106           statement = finish_break_stmt ();
10107           break;
10108         case IN_OMP_BLOCK:
10109           error_at (token->location, "invalid exit from OpenMP structured block");
10110           break;
10111         case IN_OMP_FOR:
10112           error_at (token->location, "break statement used with OpenMP for loop");
10113           break;
10114         }
10115       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10116       break;
10117
10118     case RID_CONTINUE:
10119       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10120         {
10121         case 0:
10122           error_at (token->location, "continue statement not within a loop");
10123           break;
10124         case IN_ITERATION_STMT:
10125         case IN_OMP_FOR:
10126           statement = finish_continue_stmt ();
10127           break;
10128         case IN_OMP_BLOCK:
10129           error_at (token->location, "invalid exit from OpenMP structured block");
10130           break;
10131         default:
10132           gcc_unreachable ();
10133         }
10134       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10135       break;
10136
10137     case RID_RETURN:
10138       {
10139         tree expr;
10140         bool expr_non_constant_p;
10141
10142         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10143           {
10144             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10145             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10146           }
10147         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10148           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10149         else
10150           /* If the next token is a `;', then there is no
10151              expression.  */
10152           expr = NULL_TREE;
10153         /* Build the return-statement.  */
10154         statement = finish_return_stmt (expr);
10155         /* Look for the final `;'.  */
10156         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10157       }
10158       break;
10159
10160     case RID_GOTO:
10161       /* Create the goto-statement.  */
10162       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10163         {
10164           /* Issue a warning about this use of a GNU extension.  */
10165           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10166           /* Consume the '*' token.  */
10167           cp_lexer_consume_token (parser->lexer);
10168           /* Parse the dependent expression.  */
10169           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10170         }
10171       else
10172         finish_goto_stmt (cp_parser_identifier (parser));
10173       /* Look for the final `;'.  */
10174       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10175       break;
10176
10177     default:
10178       cp_parser_error (parser, "expected jump-statement");
10179       break;
10180     }
10181
10182   return statement;
10183 }
10184
10185 /* Parse a declaration-statement.
10186
10187    declaration-statement:
10188      block-declaration  */
10189
10190 static void
10191 cp_parser_declaration_statement (cp_parser* parser)
10192 {
10193   void *p;
10194
10195   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10196   p = obstack_alloc (&declarator_obstack, 0);
10197
10198  /* Parse the block-declaration.  */
10199   cp_parser_block_declaration (parser, /*statement_p=*/true);
10200
10201   /* Free any declarators allocated.  */
10202   obstack_free (&declarator_obstack, p);
10203
10204   /* Finish off the statement.  */
10205   finish_stmt ();
10206 }
10207
10208 /* Some dependent statements (like `if (cond) statement'), are
10209    implicitly in their own scope.  In other words, if the statement is
10210    a single statement (as opposed to a compound-statement), it is
10211    none-the-less treated as if it were enclosed in braces.  Any
10212    declarations appearing in the dependent statement are out of scope
10213    after control passes that point.  This function parses a statement,
10214    but ensures that is in its own scope, even if it is not a
10215    compound-statement.
10216
10217    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10218    is a (possibly labeled) if statement which is not enclosed in
10219    braces and has an else clause.  This is used to implement
10220    -Wparentheses.
10221
10222    Returns the new statement.  */
10223
10224 static tree
10225 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10226 {
10227   tree statement;
10228
10229   if (if_p != NULL)
10230     *if_p = false;
10231
10232   /* Mark if () ; with a special NOP_EXPR.  */
10233   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10234     {
10235       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10236       cp_lexer_consume_token (parser->lexer);
10237       statement = add_stmt (build_empty_stmt (loc));
10238     }
10239   /* if a compound is opened, we simply parse the statement directly.  */
10240   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10241     statement = cp_parser_compound_statement (parser, NULL, false, false);
10242   /* If the token is not a `{', then we must take special action.  */
10243   else
10244     {
10245       /* Create a compound-statement.  */
10246       statement = begin_compound_stmt (0);
10247       /* Parse the dependent-statement.  */
10248       cp_parser_statement (parser, NULL_TREE, false, if_p);
10249       /* Finish the dummy compound-statement.  */
10250       finish_compound_stmt (statement);
10251     }
10252
10253   /* Return the statement.  */
10254   return statement;
10255 }
10256
10257 /* For some dependent statements (like `while (cond) statement'), we
10258    have already created a scope.  Therefore, even if the dependent
10259    statement is a compound-statement, we do not want to create another
10260    scope.  */
10261
10262 static void
10263 cp_parser_already_scoped_statement (cp_parser* parser)
10264 {
10265   /* If the token is a `{', then we must take special action.  */
10266   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10267     cp_parser_statement (parser, NULL_TREE, false, NULL);
10268   else
10269     {
10270       /* Avoid calling cp_parser_compound_statement, so that we
10271          don't create a new scope.  Do everything else by hand.  */
10272       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10273       /* If the next keyword is `__label__' we have a label declaration.  */
10274       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10275         cp_parser_label_declaration (parser);
10276       /* Parse an (optional) statement-seq.  */
10277       cp_parser_statement_seq_opt (parser, NULL_TREE);
10278       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10279     }
10280 }
10281
10282 /* Declarations [gram.dcl.dcl] */
10283
10284 /* Parse an optional declaration-sequence.
10285
10286    declaration-seq:
10287      declaration
10288      declaration-seq declaration  */
10289
10290 static void
10291 cp_parser_declaration_seq_opt (cp_parser* parser)
10292 {
10293   while (true)
10294     {
10295       cp_token *token;
10296
10297       token = cp_lexer_peek_token (parser->lexer);
10298
10299       if (token->type == CPP_CLOSE_BRACE
10300           || token->type == CPP_EOF
10301           || token->type == CPP_PRAGMA_EOL)
10302         break;
10303
10304       if (token->type == CPP_SEMICOLON)
10305         {
10306           /* A declaration consisting of a single semicolon is
10307              invalid.  Allow it unless we're being pedantic.  */
10308           cp_lexer_consume_token (parser->lexer);
10309           if (!in_system_header)
10310             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10311           continue;
10312         }
10313
10314       /* If we're entering or exiting a region that's implicitly
10315          extern "C", modify the lang context appropriately.  */
10316       if (!parser->implicit_extern_c && token->implicit_extern_c)
10317         {
10318           push_lang_context (lang_name_c);
10319           parser->implicit_extern_c = true;
10320         }
10321       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10322         {
10323           pop_lang_context ();
10324           parser->implicit_extern_c = false;
10325         }
10326
10327       if (token->type == CPP_PRAGMA)
10328         {
10329           /* A top-level declaration can consist solely of a #pragma.
10330              A nested declaration cannot, so this is done here and not
10331              in cp_parser_declaration.  (A #pragma at block scope is
10332              handled in cp_parser_statement.)  */
10333           cp_parser_pragma (parser, pragma_external);
10334           continue;
10335         }
10336
10337       /* Parse the declaration itself.  */
10338       cp_parser_declaration (parser);
10339     }
10340 }
10341
10342 /* Parse a declaration.
10343
10344    declaration:
10345      block-declaration
10346      function-definition
10347      template-declaration
10348      explicit-instantiation
10349      explicit-specialization
10350      linkage-specification
10351      namespace-definition
10352
10353    GNU extension:
10354
10355    declaration:
10356       __extension__ declaration */
10357
10358 static void
10359 cp_parser_declaration (cp_parser* parser)
10360 {
10361   cp_token token1;
10362   cp_token token2;
10363   int saved_pedantic;
10364   void *p;
10365   tree attributes = NULL_TREE;
10366
10367   /* Check for the `__extension__' keyword.  */
10368   if (cp_parser_extension_opt (parser, &saved_pedantic))
10369     {
10370       /* Parse the qualified declaration.  */
10371       cp_parser_declaration (parser);
10372       /* Restore the PEDANTIC flag.  */
10373       pedantic = saved_pedantic;
10374
10375       return;
10376     }
10377
10378   /* Try to figure out what kind of declaration is present.  */
10379   token1 = *cp_lexer_peek_token (parser->lexer);
10380
10381   if (token1.type != CPP_EOF)
10382     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10383   else
10384     {
10385       token2.type = CPP_EOF;
10386       token2.keyword = RID_MAX;
10387     }
10388
10389   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10390   p = obstack_alloc (&declarator_obstack, 0);
10391
10392   /* If the next token is `extern' and the following token is a string
10393      literal, then we have a linkage specification.  */
10394   if (token1.keyword == RID_EXTERN
10395       && cp_parser_is_pure_string_literal (&token2))
10396     cp_parser_linkage_specification (parser);
10397   /* If the next token is `template', then we have either a template
10398      declaration, an explicit instantiation, or an explicit
10399      specialization.  */
10400   else if (token1.keyword == RID_TEMPLATE)
10401     {
10402       /* `template <>' indicates a template specialization.  */
10403       if (token2.type == CPP_LESS
10404           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10405         cp_parser_explicit_specialization (parser);
10406       /* `template <' indicates a template declaration.  */
10407       else if (token2.type == CPP_LESS)
10408         cp_parser_template_declaration (parser, /*member_p=*/false);
10409       /* Anything else must be an explicit instantiation.  */
10410       else
10411         cp_parser_explicit_instantiation (parser);
10412     }
10413   /* If the next token is `export', then we have a template
10414      declaration.  */
10415   else if (token1.keyword == RID_EXPORT)
10416     cp_parser_template_declaration (parser, /*member_p=*/false);
10417   /* If the next token is `extern', 'static' or 'inline' and the one
10418      after that is `template', we have a GNU extended explicit
10419      instantiation directive.  */
10420   else if (cp_parser_allow_gnu_extensions_p (parser)
10421            && (token1.keyword == RID_EXTERN
10422                || token1.keyword == RID_STATIC
10423                || token1.keyword == RID_INLINE)
10424            && token2.keyword == RID_TEMPLATE)
10425     cp_parser_explicit_instantiation (parser);
10426   /* If the next token is `namespace', check for a named or unnamed
10427      namespace definition.  */
10428   else if (token1.keyword == RID_NAMESPACE
10429            && (/* A named namespace definition.  */
10430                (token2.type == CPP_NAME
10431                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10432                     != CPP_EQ))
10433                /* An unnamed namespace definition.  */
10434                || token2.type == CPP_OPEN_BRACE
10435                || token2.keyword == RID_ATTRIBUTE))
10436     cp_parser_namespace_definition (parser);
10437   /* An inline (associated) namespace definition.  */
10438   else if (token1.keyword == RID_INLINE
10439            && token2.keyword == RID_NAMESPACE)
10440     cp_parser_namespace_definition (parser);
10441   /* Objective-C++ declaration/definition.  */
10442   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10443     cp_parser_objc_declaration (parser, NULL_TREE);
10444   else if (c_dialect_objc ()
10445            && token1.keyword == RID_ATTRIBUTE
10446            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10447     cp_parser_objc_declaration (parser, attributes);
10448   /* We must have either a block declaration or a function
10449      definition.  */
10450   else
10451     /* Try to parse a block-declaration, or a function-definition.  */
10452     cp_parser_block_declaration (parser, /*statement_p=*/false);
10453
10454   /* Free any declarators allocated.  */
10455   obstack_free (&declarator_obstack, p);
10456 }
10457
10458 /* Parse a block-declaration.
10459
10460    block-declaration:
10461      simple-declaration
10462      asm-definition
10463      namespace-alias-definition
10464      using-declaration
10465      using-directive
10466
10467    GNU Extension:
10468
10469    block-declaration:
10470      __extension__ block-declaration
10471
10472    C++0x Extension:
10473
10474    block-declaration:
10475      static_assert-declaration
10476
10477    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10478    part of a declaration-statement.  */
10479
10480 static void
10481 cp_parser_block_declaration (cp_parser *parser,
10482                              bool      statement_p)
10483 {
10484   cp_token *token1;
10485   int saved_pedantic;
10486
10487   /* Check for the `__extension__' keyword.  */
10488   if (cp_parser_extension_opt (parser, &saved_pedantic))
10489     {
10490       /* Parse the qualified declaration.  */
10491       cp_parser_block_declaration (parser, statement_p);
10492       /* Restore the PEDANTIC flag.  */
10493       pedantic = saved_pedantic;
10494
10495       return;
10496     }
10497
10498   /* Peek at the next token to figure out which kind of declaration is
10499      present.  */
10500   token1 = cp_lexer_peek_token (parser->lexer);
10501
10502   /* If the next keyword is `asm', we have an asm-definition.  */
10503   if (token1->keyword == RID_ASM)
10504     {
10505       if (statement_p)
10506         cp_parser_commit_to_tentative_parse (parser);
10507       cp_parser_asm_definition (parser);
10508     }
10509   /* If the next keyword is `namespace', we have a
10510      namespace-alias-definition.  */
10511   else if (token1->keyword == RID_NAMESPACE)
10512     cp_parser_namespace_alias_definition (parser);
10513   /* If the next keyword is `using', we have a
10514      using-declaration, a using-directive, or an alias-declaration.  */
10515   else if (token1->keyword == RID_USING)
10516     {
10517       cp_token *token2;
10518
10519       if (statement_p)
10520         cp_parser_commit_to_tentative_parse (parser);
10521       /* If the token after `using' is `namespace', then we have a
10522          using-directive.  */
10523       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10524       if (token2->keyword == RID_NAMESPACE)
10525         cp_parser_using_directive (parser);
10526       /* If the second token after 'using' is '=', then we have an
10527          alias-declaration.  */
10528       else if (cxx_dialect >= cxx0x
10529                && token2->type == CPP_NAME
10530                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10531                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10532         cp_parser_alias_declaration (parser);
10533       /* Otherwise, it's a using-declaration.  */
10534       else
10535         cp_parser_using_declaration (parser,
10536                                      /*access_declaration_p=*/false);
10537     }
10538   /* If the next keyword is `__label__' we have a misplaced label
10539      declaration.  */
10540   else if (token1->keyword == RID_LABEL)
10541     {
10542       cp_lexer_consume_token (parser->lexer);
10543       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10544       cp_parser_skip_to_end_of_statement (parser);
10545       /* If the next token is now a `;', consume it.  */
10546       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10547         cp_lexer_consume_token (parser->lexer);
10548     }
10549   /* If the next token is `static_assert' we have a static assertion.  */
10550   else if (token1->keyword == RID_STATIC_ASSERT)
10551     cp_parser_static_assert (parser, /*member_p=*/false);
10552   /* Anything else must be a simple-declaration.  */
10553   else
10554     cp_parser_simple_declaration (parser, !statement_p,
10555                                   /*maybe_range_for_decl*/NULL);
10556 }
10557
10558 /* Parse a simple-declaration.
10559
10560    simple-declaration:
10561      decl-specifier-seq [opt] init-declarator-list [opt] ;
10562
10563    init-declarator-list:
10564      init-declarator
10565      init-declarator-list , init-declarator
10566
10567    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10568    function-definition as a simple-declaration.
10569
10570    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10571    parsed declaration if it is an uninitialized single declarator not followed
10572    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10573    if present, will not be consumed.  */
10574
10575 static void
10576 cp_parser_simple_declaration (cp_parser* parser,
10577                               bool function_definition_allowed_p,
10578                               tree *maybe_range_for_decl)
10579 {
10580   cp_decl_specifier_seq decl_specifiers;
10581   int declares_class_or_enum;
10582   bool saw_declarator;
10583
10584   if (maybe_range_for_decl)
10585     *maybe_range_for_decl = NULL_TREE;
10586
10587   /* Defer access checks until we know what is being declared; the
10588      checks for names appearing in the decl-specifier-seq should be
10589      done as if we were in the scope of the thing being declared.  */
10590   push_deferring_access_checks (dk_deferred);
10591
10592   /* Parse the decl-specifier-seq.  We have to keep track of whether
10593      or not the decl-specifier-seq declares a named class or
10594      enumeration type, since that is the only case in which the
10595      init-declarator-list is allowed to be empty.
10596
10597      [dcl.dcl]
10598
10599      In a simple-declaration, the optional init-declarator-list can be
10600      omitted only when declaring a class or enumeration, that is when
10601      the decl-specifier-seq contains either a class-specifier, an
10602      elaborated-type-specifier, or an enum-specifier.  */
10603   cp_parser_decl_specifier_seq (parser,
10604                                 CP_PARSER_FLAGS_OPTIONAL,
10605                                 &decl_specifiers,
10606                                 &declares_class_or_enum);
10607   /* We no longer need to defer access checks.  */
10608   stop_deferring_access_checks ();
10609
10610   /* In a block scope, a valid declaration must always have a
10611      decl-specifier-seq.  By not trying to parse declarators, we can
10612      resolve the declaration/expression ambiguity more quickly.  */
10613   if (!function_definition_allowed_p
10614       && !decl_specifiers.any_specifiers_p)
10615     {
10616       cp_parser_error (parser, "expected declaration");
10617       goto done;
10618     }
10619
10620   /* If the next two tokens are both identifiers, the code is
10621      erroneous. The usual cause of this situation is code like:
10622
10623        T t;
10624
10625      where "T" should name a type -- but does not.  */
10626   if (!decl_specifiers.any_type_specifiers_p
10627       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10628     {
10629       /* If parsing tentatively, we should commit; we really are
10630          looking at a declaration.  */
10631       cp_parser_commit_to_tentative_parse (parser);
10632       /* Give up.  */
10633       goto done;
10634     }
10635
10636   /* If we have seen at least one decl-specifier, and the next token
10637      is not a parenthesis, then we must be looking at a declaration.
10638      (After "int (" we might be looking at a functional cast.)  */
10639   if (decl_specifiers.any_specifiers_p
10640       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10641       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10642       && !cp_parser_error_occurred (parser))
10643     cp_parser_commit_to_tentative_parse (parser);
10644
10645   /* Keep going until we hit the `;' at the end of the simple
10646      declaration.  */
10647   saw_declarator = false;
10648   while (cp_lexer_next_token_is_not (parser->lexer,
10649                                      CPP_SEMICOLON))
10650     {
10651       cp_token *token;
10652       bool function_definition_p;
10653       tree decl;
10654
10655       if (saw_declarator)
10656         {
10657           /* If we are processing next declarator, coma is expected */
10658           token = cp_lexer_peek_token (parser->lexer);
10659           gcc_assert (token->type == CPP_COMMA);
10660           cp_lexer_consume_token (parser->lexer);
10661           if (maybe_range_for_decl)
10662             *maybe_range_for_decl = error_mark_node;
10663         }
10664       else
10665         saw_declarator = true;
10666
10667       /* Parse the init-declarator.  */
10668       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10669                                         /*checks=*/NULL,
10670                                         function_definition_allowed_p,
10671                                         /*member_p=*/false,
10672                                         declares_class_or_enum,
10673                                         &function_definition_p,
10674                                         maybe_range_for_decl);
10675       /* If an error occurred while parsing tentatively, exit quickly.
10676          (That usually happens when in the body of a function; each
10677          statement is treated as a declaration-statement until proven
10678          otherwise.)  */
10679       if (cp_parser_error_occurred (parser))
10680         goto done;
10681       /* Handle function definitions specially.  */
10682       if (function_definition_p)
10683         {
10684           /* If the next token is a `,', then we are probably
10685              processing something like:
10686
10687                void f() {}, *p;
10688
10689              which is erroneous.  */
10690           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10691             {
10692               cp_token *token = cp_lexer_peek_token (parser->lexer);
10693               error_at (token->location,
10694                         "mixing"
10695                         " declarations and function-definitions is forbidden");
10696             }
10697           /* Otherwise, we're done with the list of declarators.  */
10698           else
10699             {
10700               pop_deferring_access_checks ();
10701               return;
10702             }
10703         }
10704       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10705         *maybe_range_for_decl = decl;
10706       /* The next token should be either a `,' or a `;'.  */
10707       token = cp_lexer_peek_token (parser->lexer);
10708       /* If it's a `,', there are more declarators to come.  */
10709       if (token->type == CPP_COMMA)
10710         /* will be consumed next time around */;
10711       /* If it's a `;', we are done.  */
10712       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10713         break;
10714       /* Anything else is an error.  */
10715       else
10716         {
10717           /* If we have already issued an error message we don't need
10718              to issue another one.  */
10719           if (decl != error_mark_node
10720               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10721             cp_parser_error (parser, "expected %<,%> or %<;%>");
10722           /* Skip tokens until we reach the end of the statement.  */
10723           cp_parser_skip_to_end_of_statement (parser);
10724           /* If the next token is now a `;', consume it.  */
10725           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10726             cp_lexer_consume_token (parser->lexer);
10727           goto done;
10728         }
10729       /* After the first time around, a function-definition is not
10730          allowed -- even if it was OK at first.  For example:
10731
10732            int i, f() {}
10733
10734          is not valid.  */
10735       function_definition_allowed_p = false;
10736     }
10737
10738   /* Issue an error message if no declarators are present, and the
10739      decl-specifier-seq does not itself declare a class or
10740      enumeration.  */
10741   if (!saw_declarator)
10742     {
10743       if (cp_parser_declares_only_class_p (parser))
10744         shadow_tag (&decl_specifiers);
10745       /* Perform any deferred access checks.  */
10746       perform_deferred_access_checks (tf_warning_or_error);
10747     }
10748
10749   /* Consume the `;'.  */
10750   if (!maybe_range_for_decl)
10751       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10752
10753  done:
10754   pop_deferring_access_checks ();
10755 }
10756
10757 /* Parse a decl-specifier-seq.
10758
10759    decl-specifier-seq:
10760      decl-specifier-seq [opt] decl-specifier
10761      decl-specifier attribute-specifier-seq [opt] (C++11)
10762
10763    decl-specifier:
10764      storage-class-specifier
10765      type-specifier
10766      function-specifier
10767      friend
10768      typedef
10769
10770    GNU Extension:
10771
10772    decl-specifier:
10773      attributes
10774
10775    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10776
10777    The parser flags FLAGS is used to control type-specifier parsing.
10778
10779    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10780    flags:
10781
10782      1: one of the decl-specifiers is an elaborated-type-specifier
10783         (i.e., a type declaration)
10784      2: one of the decl-specifiers is an enum-specifier or a
10785         class-specifier (i.e., a type definition)
10786
10787    */
10788
10789 static void
10790 cp_parser_decl_specifier_seq (cp_parser* parser,
10791                               cp_parser_flags flags,
10792                               cp_decl_specifier_seq *decl_specs,
10793                               int* declares_class_or_enum)
10794 {
10795   bool constructor_possible_p = !parser->in_declarator_p;
10796   bool found_decl_spec = false;
10797   cp_token *start_token = NULL;
10798   cp_decl_spec ds;
10799
10800   /* Clear DECL_SPECS.  */
10801   clear_decl_specs (decl_specs);
10802
10803   /* Assume no class or enumeration type is declared.  */
10804   *declares_class_or_enum = 0;
10805
10806   /* Keep reading specifiers until there are no more to read.  */
10807   while (true)
10808     {
10809       bool constructor_p;
10810       cp_token *token;
10811       ds = ds_last;
10812
10813       /* Peek at the next token.  */
10814       token = cp_lexer_peek_token (parser->lexer);
10815
10816       /* Save the first token of the decl spec list for error
10817          reporting.  */
10818       if (!start_token)
10819         start_token = token;
10820       /* Handle attributes.  */
10821       if (cp_next_tokens_can_be_attribute_p (parser))
10822         {
10823           /* Parse the attributes.  */
10824           tree attrs = cp_parser_attributes_opt (parser);
10825
10826           /* In a sequence of declaration specifiers, c++11 attributes
10827              appertain to the type that precede them. In that case
10828              [dcl.spec]/1 says:
10829
10830                  The attribute-specifier-seq affects the type only for
10831                  the declaration it appears in, not other declarations
10832                  involving the same type.
10833
10834              But for now let's force the user to position the
10835              attribute either at the beginning of the declaration or
10836              after the declarator-id, which would clearly mean that it
10837              applies to the declarator.  */
10838           if (cxx11_attribute_p (attrs))
10839             {
10840               if (!found_decl_spec)
10841                 /* The c++11 attribute is at the beginning of the
10842                    declaration.  It appertains to the entity being
10843                    declared.  */;
10844               else
10845                 {
10846                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10847                     {
10848                       /*  This is an attribute following a
10849                           class-specifier.  */
10850                       if (decl_specs->type_definition_p)
10851                         warn_misplaced_attr_for_class_type (token->location,
10852                                                             decl_specs->type);
10853                       attrs = NULL_TREE;
10854                     }
10855                   else
10856                     {
10857                       decl_specs->std_attributes
10858                         = chainon (decl_specs->std_attributes,
10859                                    attrs);
10860                       if (decl_specs->locations[ds_std_attribute] == 0)
10861                         decl_specs->locations[ds_std_attribute] = token->location;
10862                     }
10863                   continue;
10864                 }
10865             }
10866
10867             decl_specs->attributes
10868               = chainon (decl_specs->attributes,
10869                          attrs);
10870           if (decl_specs->locations[ds_attribute] == 0)
10871             decl_specs->locations[ds_attribute] = token->location;
10872           continue;
10873         }
10874       /* Assume we will find a decl-specifier keyword.  */
10875       found_decl_spec = true;
10876       /* If the next token is an appropriate keyword, we can simply
10877          add it to the list.  */
10878       switch (token->keyword)
10879         {
10880           /* decl-specifier:
10881                friend
10882                constexpr */
10883         case RID_FRIEND:
10884           if (!at_class_scope_p ())
10885             {
10886               error_at (token->location, "%<friend%> used outside of class");
10887               cp_lexer_purge_token (parser->lexer);
10888             }
10889           else
10890             {
10891               ds = ds_friend;
10892               /* Consume the token.  */
10893               cp_lexer_consume_token (parser->lexer);
10894             }
10895           break;
10896
10897         case RID_CONSTEXPR:
10898           ds = ds_constexpr;
10899           cp_lexer_consume_token (parser->lexer);
10900           break;
10901
10902           /* function-specifier:
10903                inline
10904                virtual
10905                explicit  */
10906         case RID_INLINE:
10907         case RID_VIRTUAL:
10908         case RID_EXPLICIT:
10909           cp_parser_function_specifier_opt (parser, decl_specs);
10910           break;
10911
10912           /* decl-specifier:
10913                typedef  */
10914         case RID_TYPEDEF:
10915           ds = ds_typedef;
10916           /* Consume the token.  */
10917           cp_lexer_consume_token (parser->lexer);
10918           /* A constructor declarator cannot appear in a typedef.  */
10919           constructor_possible_p = false;
10920           /* The "typedef" keyword can only occur in a declaration; we
10921              may as well commit at this point.  */
10922           cp_parser_commit_to_tentative_parse (parser);
10923
10924           if (decl_specs->storage_class != sc_none)
10925             decl_specs->conflicting_specifiers_p = true;
10926           break;
10927
10928           /* storage-class-specifier:
10929                auto
10930                register
10931                static
10932                extern
10933                mutable
10934
10935              GNU Extension:
10936                thread  */
10937         case RID_AUTO:
10938           if (cxx_dialect == cxx98) 
10939             {
10940               /* Consume the token.  */
10941               cp_lexer_consume_token (parser->lexer);
10942
10943               /* Complain about `auto' as a storage specifier, if
10944                  we're complaining about C++0x compatibility.  */
10945               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10946                           " changes meaning in C++11; please remove it");
10947
10948               /* Set the storage class anyway.  */
10949               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10950                                            token);
10951             }
10952           else
10953             /* C++0x auto type-specifier.  */
10954             found_decl_spec = false;
10955           break;
10956
10957         case RID_REGISTER:
10958         case RID_STATIC:
10959         case RID_EXTERN:
10960         case RID_MUTABLE:
10961           /* Consume the token.  */
10962           cp_lexer_consume_token (parser->lexer);
10963           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10964                                        token);
10965           break;
10966         case RID_THREAD:
10967           /* Consume the token.  */
10968           ds = ds_thread;
10969           cp_lexer_consume_token (parser->lexer);
10970           break;
10971
10972         default:
10973           /* We did not yet find a decl-specifier yet.  */
10974           found_decl_spec = false;
10975           break;
10976         }
10977
10978       if (found_decl_spec
10979           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10980           && token->keyword != RID_CONSTEXPR)
10981         error ("decl-specifier invalid in condition");
10982
10983       if (ds != ds_last)
10984         set_and_check_decl_spec_loc (decl_specs, ds, token);
10985
10986       /* Constructors are a special case.  The `S' in `S()' is not a
10987          decl-specifier; it is the beginning of the declarator.  */
10988       constructor_p
10989         = (!found_decl_spec
10990            && constructor_possible_p
10991            && (cp_parser_constructor_declarator_p
10992                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10993
10994       /* If we don't have a DECL_SPEC yet, then we must be looking at
10995          a type-specifier.  */
10996       if (!found_decl_spec && !constructor_p)
10997         {
10998           int decl_spec_declares_class_or_enum;
10999           bool is_cv_qualifier;
11000           tree type_spec;
11001
11002           type_spec
11003             = cp_parser_type_specifier (parser, flags,
11004                                         decl_specs,
11005                                         /*is_declaration=*/true,
11006                                         &decl_spec_declares_class_or_enum,
11007                                         &is_cv_qualifier);
11008           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11009
11010           /* If this type-specifier referenced a user-defined type
11011              (a typedef, class-name, etc.), then we can't allow any
11012              more such type-specifiers henceforth.
11013
11014              [dcl.spec]
11015
11016              The longest sequence of decl-specifiers that could
11017              possibly be a type name is taken as the
11018              decl-specifier-seq of a declaration.  The sequence shall
11019              be self-consistent as described below.
11020
11021              [dcl.type]
11022
11023              As a general rule, at most one type-specifier is allowed
11024              in the complete decl-specifier-seq of a declaration.  The
11025              only exceptions are the following:
11026
11027              -- const or volatile can be combined with any other
11028                 type-specifier.
11029
11030              -- signed or unsigned can be combined with char, long,
11031                 short, or int.
11032
11033              -- ..
11034
11035              Example:
11036
11037                typedef char* Pc;
11038                void g (const int Pc);
11039
11040              Here, Pc is *not* part of the decl-specifier seq; it's
11041              the declarator.  Therefore, once we see a type-specifier
11042              (other than a cv-qualifier), we forbid any additional
11043              user-defined types.  We *do* still allow things like `int
11044              int' to be considered a decl-specifier-seq, and issue the
11045              error message later.  */
11046           if (type_spec && !is_cv_qualifier)
11047             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11048           /* A constructor declarator cannot follow a type-specifier.  */
11049           if (type_spec)
11050             {
11051               constructor_possible_p = false;
11052               found_decl_spec = true;
11053               if (!is_cv_qualifier)
11054                 decl_specs->any_type_specifiers_p = true;
11055             }
11056         }
11057
11058       /* If we still do not have a DECL_SPEC, then there are no more
11059          decl-specifiers.  */
11060       if (!found_decl_spec)
11061         break;
11062
11063       decl_specs->any_specifiers_p = true;
11064       /* After we see one decl-specifier, further decl-specifiers are
11065          always optional.  */
11066       flags |= CP_PARSER_FLAGS_OPTIONAL;
11067     }
11068
11069   /* Don't allow a friend specifier with a class definition.  */
11070   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11071       && (*declares_class_or_enum & 2))
11072     error_at (decl_specs->locations[ds_friend],
11073               "class definition may not be declared a friend");
11074 }
11075
11076 /* Parse an (optional) storage-class-specifier.
11077
11078    storage-class-specifier:
11079      auto
11080      register
11081      static
11082      extern
11083      mutable
11084
11085    GNU Extension:
11086
11087    storage-class-specifier:
11088      thread
11089
11090    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
11091
11092 static tree
11093 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11094 {
11095   switch (cp_lexer_peek_token (parser->lexer)->keyword)
11096     {
11097     case RID_AUTO:
11098       if (cxx_dialect != cxx98)
11099         return NULL_TREE;
11100       /* Fall through for C++98.  */
11101
11102     case RID_REGISTER:
11103     case RID_STATIC:
11104     case RID_EXTERN:
11105     case RID_MUTABLE:
11106     case RID_THREAD:
11107       /* Consume the token.  */
11108       return cp_lexer_consume_token (parser->lexer)->u.value;
11109
11110     default:
11111       return NULL_TREE;
11112     }
11113 }
11114
11115 /* Parse an (optional) function-specifier.
11116
11117    function-specifier:
11118      inline
11119      virtual
11120      explicit
11121
11122    Returns an IDENTIFIER_NODE corresponding to the keyword used.
11123    Updates DECL_SPECS, if it is non-NULL.  */
11124
11125 static tree
11126 cp_parser_function_specifier_opt (cp_parser* parser,
11127                                   cp_decl_specifier_seq *decl_specs)
11128 {
11129   cp_token *token = cp_lexer_peek_token (parser->lexer);
11130   switch (token->keyword)
11131     {
11132     case RID_INLINE:
11133       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11134       break;
11135
11136     case RID_VIRTUAL:
11137       /* 14.5.2.3 [temp.mem]
11138
11139          A member function template shall not be virtual.  */
11140       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11141         error_at (token->location, "templates may not be %<virtual%>");
11142       else
11143         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11144       break;
11145
11146     case RID_EXPLICIT:
11147       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11148       break;
11149
11150     default:
11151       return NULL_TREE;
11152     }
11153
11154   /* Consume the token.  */
11155   return cp_lexer_consume_token (parser->lexer)->u.value;
11156 }
11157
11158 /* Parse a linkage-specification.
11159
11160    linkage-specification:
11161      extern string-literal { declaration-seq [opt] }
11162      extern string-literal declaration  */
11163
11164 static void
11165 cp_parser_linkage_specification (cp_parser* parser)
11166 {
11167   tree linkage;
11168
11169   /* Look for the `extern' keyword.  */
11170   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11171
11172   /* Look for the string-literal.  */
11173   linkage = cp_parser_string_literal (parser, false, false);
11174
11175   /* Transform the literal into an identifier.  If the literal is a
11176      wide-character string, or contains embedded NULs, then we can't
11177      handle it as the user wants.  */
11178   if (strlen (TREE_STRING_POINTER (linkage))
11179       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11180     {
11181       cp_parser_error (parser, "invalid linkage-specification");
11182       /* Assume C++ linkage.  */
11183       linkage = lang_name_cplusplus;
11184     }
11185   else
11186     linkage = get_identifier (TREE_STRING_POINTER (linkage));
11187
11188   /* We're now using the new linkage.  */
11189   push_lang_context (linkage);
11190
11191   /* If the next token is a `{', then we're using the first
11192      production.  */
11193   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11194     {
11195       /* Consume the `{' token.  */
11196       cp_lexer_consume_token (parser->lexer);
11197       /* Parse the declarations.  */
11198       cp_parser_declaration_seq_opt (parser);
11199       /* Look for the closing `}'.  */
11200       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11201     }
11202   /* Otherwise, there's just one declaration.  */
11203   else
11204     {
11205       bool saved_in_unbraced_linkage_specification_p;
11206
11207       saved_in_unbraced_linkage_specification_p
11208         = parser->in_unbraced_linkage_specification_p;
11209       parser->in_unbraced_linkage_specification_p = true;
11210       cp_parser_declaration (parser);
11211       parser->in_unbraced_linkage_specification_p
11212         = saved_in_unbraced_linkage_specification_p;
11213     }
11214
11215   /* We're done with the linkage-specification.  */
11216   pop_lang_context ();
11217 }
11218
11219 /* Parse a static_assert-declaration.
11220
11221    static_assert-declaration:
11222      static_assert ( constant-expression , string-literal ) ; 
11223
11224    If MEMBER_P, this static_assert is a class member.  */
11225
11226 static void 
11227 cp_parser_static_assert(cp_parser *parser, bool member_p)
11228 {
11229   tree condition;
11230   tree message;
11231   cp_token *token;
11232   location_t saved_loc;
11233   bool dummy;
11234
11235   /* Peek at the `static_assert' token so we can keep track of exactly
11236      where the static assertion started.  */
11237   token = cp_lexer_peek_token (parser->lexer);
11238   saved_loc = token->location;
11239
11240   /* Look for the `static_assert' keyword.  */
11241   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
11242                                   RT_STATIC_ASSERT))
11243     return;
11244
11245   /*  We know we are in a static assertion; commit to any tentative
11246       parse.  */
11247   if (cp_parser_parsing_tentatively (parser))
11248     cp_parser_commit_to_tentative_parse (parser);
11249
11250   /* Parse the `(' starting the static assertion condition.  */
11251   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11252
11253   /* Parse the constant-expression.  Allow a non-constant expression
11254      here in order to give better diagnostics in finish_static_assert.  */
11255   condition = 
11256     cp_parser_constant_expression (parser,
11257                                    /*allow_non_constant_p=*/true,
11258                                    /*non_constant_p=*/&dummy);
11259
11260   /* Parse the separating `,'.  */
11261   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11262
11263   /* Parse the string-literal message.  */
11264   message = cp_parser_string_literal (parser, 
11265                                       /*translate=*/false,
11266                                       /*wide_ok=*/true);
11267
11268   /* A `)' completes the static assertion.  */
11269   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11270     cp_parser_skip_to_closing_parenthesis (parser, 
11271                                            /*recovering=*/true, 
11272                                            /*or_comma=*/false,
11273                                            /*consume_paren=*/true);
11274
11275   /* A semicolon terminates the declaration.  */
11276   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11277
11278   /* Complete the static assertion, which may mean either processing 
11279      the static assert now or saving it for template instantiation.  */
11280   finish_static_assert (condition, message, saved_loc, member_p);
11281 }
11282
11283 /* Parse a `decltype' type. Returns the type. 
11284
11285    simple-type-specifier:
11286      decltype ( expression )  */
11287
11288 static tree
11289 cp_parser_decltype (cp_parser *parser)
11290 {
11291   tree expr;
11292   bool id_expression_or_member_access_p = false;
11293   const char *saved_message;
11294   bool saved_integral_constant_expression_p;
11295   bool saved_non_integral_constant_expression_p;
11296   cp_token *id_expr_start_token;
11297   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11298
11299   if (start_token->type == CPP_DECLTYPE)
11300     {
11301       /* Already parsed.  */
11302       cp_lexer_consume_token (parser->lexer);
11303       return start_token->u.value;
11304     }
11305
11306   /* Look for the `decltype' token.  */
11307   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11308     return error_mark_node;
11309
11310   /* Types cannot be defined in a `decltype' expression.  Save away the
11311      old message.  */
11312   saved_message = parser->type_definition_forbidden_message;
11313
11314   /* And create the new one.  */
11315   parser->type_definition_forbidden_message
11316     = G_("types may not be defined in %<decltype%> expressions");
11317
11318   /* The restrictions on constant-expressions do not apply inside
11319      decltype expressions.  */
11320   saved_integral_constant_expression_p
11321     = parser->integral_constant_expression_p;
11322   saved_non_integral_constant_expression_p
11323     = parser->non_integral_constant_expression_p;
11324   parser->integral_constant_expression_p = false;
11325
11326   /* Do not actually evaluate the expression.  */
11327   ++cp_unevaluated_operand;
11328
11329   /* Do not warn about problems with the expression.  */
11330   ++c_inhibit_evaluation_warnings;
11331
11332   /* Parse the opening `('.  */
11333   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11334     return error_mark_node;
11335   
11336   /* First, try parsing an id-expression.  */
11337   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11338   cp_parser_parse_tentatively (parser);
11339   expr = cp_parser_id_expression (parser,
11340                                   /*template_keyword_p=*/false,
11341                                   /*check_dependency_p=*/true,
11342                                   /*template_p=*/NULL,
11343                                   /*declarator_p=*/false,
11344                                   /*optional_p=*/false);
11345
11346   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11347     {
11348       bool non_integral_constant_expression_p = false;
11349       tree id_expression = expr;
11350       cp_id_kind idk;
11351       const char *error_msg;
11352
11353       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11354         /* Lookup the name we got back from the id-expression.  */
11355         expr = cp_parser_lookup_name (parser, expr,
11356                                       none_type,
11357                                       /*is_template=*/false,
11358                                       /*is_namespace=*/false,
11359                                       /*check_dependency=*/true,
11360                                       /*ambiguous_decls=*/NULL,
11361                                       id_expr_start_token->location);
11362
11363       if (expr
11364           && expr != error_mark_node
11365           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11366           && TREE_CODE (expr) != TYPE_DECL
11367           && (TREE_CODE (expr) != BIT_NOT_EXPR
11368               || !TYPE_P (TREE_OPERAND (expr, 0)))
11369           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11370         {
11371           /* Complete lookup of the id-expression.  */
11372           expr = (finish_id_expression
11373                   (id_expression, expr, parser->scope, &idk,
11374                    /*integral_constant_expression_p=*/false,
11375                    /*allow_non_integral_constant_expression_p=*/true,
11376                    &non_integral_constant_expression_p,
11377                    /*template_p=*/false,
11378                    /*done=*/true,
11379                    /*address_p=*/false,
11380                    /*template_arg_p=*/false,
11381                    &error_msg,
11382                    id_expr_start_token->location));
11383
11384           if (expr == error_mark_node)
11385             /* We found an id-expression, but it was something that we
11386                should not have found. This is an error, not something
11387                we can recover from, so note that we found an
11388                id-expression and we'll recover as gracefully as
11389                possible.  */
11390             id_expression_or_member_access_p = true;
11391         }
11392
11393       if (expr 
11394           && expr != error_mark_node
11395           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11396         /* We have an id-expression.  */
11397         id_expression_or_member_access_p = true;
11398     }
11399
11400   if (!id_expression_or_member_access_p)
11401     {
11402       /* Abort the id-expression parse.  */
11403       cp_parser_abort_tentative_parse (parser);
11404
11405       /* Parsing tentatively, again.  */
11406       cp_parser_parse_tentatively (parser);
11407
11408       /* Parse a class member access.  */
11409       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11410                                            /*cast_p=*/false, /*decltype*/true,
11411                                            /*member_access_only_p=*/true, NULL);
11412
11413       if (expr 
11414           && expr != error_mark_node
11415           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11416         /* We have an id-expression.  */
11417         id_expression_or_member_access_p = true;
11418     }
11419
11420   if (id_expression_or_member_access_p)
11421     /* We have parsed the complete id-expression or member access.  */
11422     cp_parser_parse_definitely (parser);
11423   else
11424     {
11425       bool saved_greater_than_is_operator_p;
11426
11427       /* Abort our attempt to parse an id-expression or member access
11428          expression.  */
11429       cp_parser_abort_tentative_parse (parser);
11430
11431       /* Within a parenthesized expression, a `>' token is always
11432          the greater-than operator.  */
11433       saved_greater_than_is_operator_p
11434         = parser->greater_than_is_operator_p;
11435       parser->greater_than_is_operator_p = true;
11436
11437       /* Parse a full expression.  */
11438       expr = cp_parser_expression (parser, /*cast_p=*/false,
11439                                    /*decltype*/true, NULL);
11440
11441       /* The `>' token might be the end of a template-id or
11442          template-parameter-list now.  */
11443       parser->greater_than_is_operator_p
11444         = saved_greater_than_is_operator_p;
11445     }
11446
11447   /* Go back to evaluating expressions.  */
11448   --cp_unevaluated_operand;
11449   --c_inhibit_evaluation_warnings;
11450
11451   /* Restore the old message and the integral constant expression
11452      flags.  */
11453   parser->type_definition_forbidden_message = saved_message;
11454   parser->integral_constant_expression_p
11455     = saved_integral_constant_expression_p;
11456   parser->non_integral_constant_expression_p
11457     = saved_non_integral_constant_expression_p;
11458
11459   /* Parse to the closing `)'.  */
11460   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11461     {
11462       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11463                                              /*consume_paren=*/true);
11464       return error_mark_node;
11465     }
11466
11467   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11468                                tf_warning_or_error);
11469
11470   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11471      it again.  */
11472   start_token->type = CPP_DECLTYPE;
11473   start_token->u.value = expr;
11474   start_token->keyword = RID_MAX;
11475   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11476
11477   return expr;
11478 }
11479
11480 /* Special member functions [gram.special] */
11481
11482 /* Parse a conversion-function-id.
11483
11484    conversion-function-id:
11485      operator conversion-type-id
11486
11487    Returns an IDENTIFIER_NODE representing the operator.  */
11488
11489 static tree
11490 cp_parser_conversion_function_id (cp_parser* parser)
11491 {
11492   tree type;
11493   tree saved_scope;
11494   tree saved_qualifying_scope;
11495   tree saved_object_scope;
11496   tree pushed_scope = NULL_TREE;
11497
11498   /* Look for the `operator' token.  */
11499   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11500     return error_mark_node;
11501   /* When we parse the conversion-type-id, the current scope will be
11502      reset.  However, we need that information in able to look up the
11503      conversion function later, so we save it here.  */
11504   saved_scope = parser->scope;
11505   saved_qualifying_scope = parser->qualifying_scope;
11506   saved_object_scope = parser->object_scope;
11507   /* We must enter the scope of the class so that the names of
11508      entities declared within the class are available in the
11509      conversion-type-id.  For example, consider:
11510
11511        struct S {
11512          typedef int I;
11513          operator I();
11514        };
11515
11516        S::operator I() { ... }
11517
11518      In order to see that `I' is a type-name in the definition, we
11519      must be in the scope of `S'.  */
11520   if (saved_scope)
11521     pushed_scope = push_scope (saved_scope);
11522   /* Parse the conversion-type-id.  */
11523   type = cp_parser_conversion_type_id (parser);
11524   /* Leave the scope of the class, if any.  */
11525   if (pushed_scope)
11526     pop_scope (pushed_scope);
11527   /* Restore the saved scope.  */
11528   parser->scope = saved_scope;
11529   parser->qualifying_scope = saved_qualifying_scope;
11530   parser->object_scope = saved_object_scope;
11531   /* If the TYPE is invalid, indicate failure.  */
11532   if (type == error_mark_node)
11533     return error_mark_node;
11534   return mangle_conv_op_name_for_type (type);
11535 }
11536
11537 /* Parse a conversion-type-id:
11538
11539    conversion-type-id:
11540      type-specifier-seq conversion-declarator [opt]
11541
11542    Returns the TYPE specified.  */
11543
11544 static tree
11545 cp_parser_conversion_type_id (cp_parser* parser)
11546 {
11547   tree attributes;
11548   cp_decl_specifier_seq type_specifiers;
11549   cp_declarator *declarator;
11550   tree type_specified;
11551
11552   /* Parse the attributes.  */
11553   attributes = cp_parser_attributes_opt (parser);
11554   /* Parse the type-specifiers.  */
11555   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11556                                 /*is_trailing_return=*/false,
11557                                 &type_specifiers);
11558   /* If that didn't work, stop.  */
11559   if (type_specifiers.type == error_mark_node)
11560     return error_mark_node;
11561   /* Parse the conversion-declarator.  */
11562   declarator = cp_parser_conversion_declarator_opt (parser);
11563
11564   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11565                                     /*initialized=*/0, &attributes);
11566   if (attributes)
11567     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11568
11569   /* Don't give this error when parsing tentatively.  This happens to
11570      work because we always parse this definitively once.  */
11571   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11572       && type_uses_auto (type_specified))
11573     {
11574       if (cxx_dialect < cxx1y)
11575         {
11576           error ("invalid use of %<auto%> in conversion operator");
11577           return error_mark_node;
11578         }
11579       else if (template_parm_scope_p ())
11580         warning (0, "use of %<auto%> in member template "
11581                  "conversion operator can never be deduced");
11582     }
11583
11584   return type_specified;
11585 }
11586
11587 /* Parse an (optional) conversion-declarator.
11588
11589    conversion-declarator:
11590      ptr-operator conversion-declarator [opt]
11591
11592    */
11593
11594 static cp_declarator *
11595 cp_parser_conversion_declarator_opt (cp_parser* parser)
11596 {
11597   enum tree_code code;
11598   tree class_type, std_attributes = NULL_TREE;
11599   cp_cv_quals cv_quals;
11600
11601   /* We don't know if there's a ptr-operator next, or not.  */
11602   cp_parser_parse_tentatively (parser);
11603   /* Try the ptr-operator.  */
11604   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11605                                  &std_attributes);
11606   /* If it worked, look for more conversion-declarators.  */
11607   if (cp_parser_parse_definitely (parser))
11608     {
11609       cp_declarator *declarator;
11610
11611       /* Parse another optional declarator.  */
11612       declarator = cp_parser_conversion_declarator_opt (parser);
11613
11614       declarator = cp_parser_make_indirect_declarator
11615         (code, class_type, cv_quals, declarator, std_attributes);
11616
11617       return declarator;
11618    }
11619
11620   return NULL;
11621 }
11622
11623 /* Parse an (optional) ctor-initializer.
11624
11625    ctor-initializer:
11626      : mem-initializer-list
11627
11628    Returns TRUE iff the ctor-initializer was actually present.  */
11629
11630 static bool
11631 cp_parser_ctor_initializer_opt (cp_parser* parser)
11632 {
11633   /* If the next token is not a `:', then there is no
11634      ctor-initializer.  */
11635   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11636     {
11637       /* Do default initialization of any bases and members.  */
11638       if (DECL_CONSTRUCTOR_P (current_function_decl))
11639         finish_mem_initializers (NULL_TREE);
11640
11641       return false;
11642     }
11643
11644   /* Consume the `:' token.  */
11645   cp_lexer_consume_token (parser->lexer);
11646   /* And the mem-initializer-list.  */
11647   cp_parser_mem_initializer_list (parser);
11648
11649   return true;
11650 }
11651
11652 /* Parse a mem-initializer-list.
11653
11654    mem-initializer-list:
11655      mem-initializer ... [opt]
11656      mem-initializer ... [opt] , mem-initializer-list  */
11657
11658 static void
11659 cp_parser_mem_initializer_list (cp_parser* parser)
11660 {
11661   tree mem_initializer_list = NULL_TREE;
11662   tree target_ctor = error_mark_node;
11663   cp_token *token = cp_lexer_peek_token (parser->lexer);
11664
11665   /* Let the semantic analysis code know that we are starting the
11666      mem-initializer-list.  */
11667   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11668     error_at (token->location,
11669               "only constructors take member initializers");
11670
11671   /* Loop through the list.  */
11672   while (true)
11673     {
11674       tree mem_initializer;
11675
11676       token = cp_lexer_peek_token (parser->lexer);
11677       /* Parse the mem-initializer.  */
11678       mem_initializer = cp_parser_mem_initializer (parser);
11679       /* If the next token is a `...', we're expanding member initializers. */
11680       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11681         {
11682           /* Consume the `...'. */
11683           cp_lexer_consume_token (parser->lexer);
11684
11685           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11686              can be expanded but members cannot. */
11687           if (mem_initializer != error_mark_node
11688               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11689             {
11690               error_at (token->location,
11691                         "cannot expand initializer for member %<%D%>",
11692                         TREE_PURPOSE (mem_initializer));
11693               mem_initializer = error_mark_node;
11694             }
11695
11696           /* Construct the pack expansion type. */
11697           if (mem_initializer != error_mark_node)
11698             mem_initializer = make_pack_expansion (mem_initializer);
11699         }
11700       if (target_ctor != error_mark_node
11701           && mem_initializer != error_mark_node)
11702         {
11703           error ("mem-initializer for %qD follows constructor delegation",
11704                  TREE_PURPOSE (mem_initializer));
11705           mem_initializer = error_mark_node;
11706         }
11707       /* Look for a target constructor. */
11708       if (mem_initializer != error_mark_node
11709           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
11710           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11711         {
11712           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11713           if (mem_initializer_list)
11714             {
11715               error ("constructor delegation follows mem-initializer for %qD",
11716                      TREE_PURPOSE (mem_initializer_list));
11717               mem_initializer = error_mark_node;
11718             }
11719           target_ctor = mem_initializer;
11720         }
11721       /* Add it to the list, unless it was erroneous.  */
11722       if (mem_initializer != error_mark_node)
11723         {
11724           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11725           mem_initializer_list = mem_initializer;
11726         }
11727       /* If the next token is not a `,', we're done.  */
11728       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11729         break;
11730       /* Consume the `,' token.  */
11731       cp_lexer_consume_token (parser->lexer);
11732     }
11733
11734   /* Perform semantic analysis.  */
11735   if (DECL_CONSTRUCTOR_P (current_function_decl))
11736     finish_mem_initializers (mem_initializer_list);
11737 }
11738
11739 /* Parse a mem-initializer.
11740
11741    mem-initializer:
11742      mem-initializer-id ( expression-list [opt] )
11743      mem-initializer-id braced-init-list
11744
11745    GNU extension:
11746
11747    mem-initializer:
11748      ( expression-list [opt] )
11749
11750    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11751    class) or FIELD_DECL (for a non-static data member) to initialize;
11752    the TREE_VALUE is the expression-list.  An empty initialization
11753    list is represented by void_list_node.  */
11754
11755 static tree
11756 cp_parser_mem_initializer (cp_parser* parser)
11757 {
11758   tree mem_initializer_id;
11759   tree expression_list;
11760   tree member;
11761   cp_token *token = cp_lexer_peek_token (parser->lexer);
11762
11763   /* Find out what is being initialized.  */
11764   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11765     {
11766       permerror (token->location,
11767                  "anachronistic old-style base class initializer");
11768       mem_initializer_id = NULL_TREE;
11769     }
11770   else
11771     {
11772       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11773       if (mem_initializer_id == error_mark_node)
11774         return mem_initializer_id;
11775     }
11776   member = expand_member_init (mem_initializer_id);
11777   if (member && !DECL_P (member))
11778     in_base_initializer = 1;
11779
11780   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11781     {
11782       bool expr_non_constant_p;
11783       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11784       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11785       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11786       expression_list = build_tree_list (NULL_TREE, expression_list);
11787     }
11788   else
11789     {
11790       vec<tree, va_gc> *vec;
11791       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11792                                                      /*cast_p=*/false,
11793                                                      /*allow_expansion_p=*/true,
11794                                                      /*non_constant_p=*/NULL);
11795       if (vec == NULL)
11796         return error_mark_node;
11797       expression_list = build_tree_list_vec (vec);
11798       release_tree_vector (vec);
11799     }
11800
11801   if (expression_list == error_mark_node)
11802     return error_mark_node;
11803   if (!expression_list)
11804     expression_list = void_type_node;
11805
11806   in_base_initializer = 0;
11807
11808   return member ? build_tree_list (member, expression_list) : error_mark_node;
11809 }
11810
11811 /* Parse a mem-initializer-id.
11812
11813    mem-initializer-id:
11814      :: [opt] nested-name-specifier [opt] class-name
11815      identifier
11816
11817    Returns a TYPE indicating the class to be initializer for the first
11818    production.  Returns an IDENTIFIER_NODE indicating the data member
11819    to be initialized for the second production.  */
11820
11821 static tree
11822 cp_parser_mem_initializer_id (cp_parser* parser)
11823 {
11824   bool global_scope_p;
11825   bool nested_name_specifier_p;
11826   bool template_p = false;
11827   tree id;
11828
11829   cp_token *token = cp_lexer_peek_token (parser->lexer);
11830
11831   /* `typename' is not allowed in this context ([temp.res]).  */
11832   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11833     {
11834       error_at (token->location, 
11835                 "keyword %<typename%> not allowed in this context (a qualified "
11836                 "member initializer is implicitly a type)");
11837       cp_lexer_consume_token (parser->lexer);
11838     }
11839   /* Look for the optional `::' operator.  */
11840   global_scope_p
11841     = (cp_parser_global_scope_opt (parser,
11842                                    /*current_scope_valid_p=*/false)
11843        != NULL_TREE);
11844   /* Look for the optional nested-name-specifier.  The simplest way to
11845      implement:
11846
11847        [temp.res]
11848
11849        The keyword `typename' is not permitted in a base-specifier or
11850        mem-initializer; in these contexts a qualified name that
11851        depends on a template-parameter is implicitly assumed to be a
11852        type name.
11853
11854      is to assume that we have seen the `typename' keyword at this
11855      point.  */
11856   nested_name_specifier_p
11857     = (cp_parser_nested_name_specifier_opt (parser,
11858                                             /*typename_keyword_p=*/true,
11859                                             /*check_dependency_p=*/true,
11860                                             /*type_p=*/true,
11861                                             /*is_declaration=*/true)
11862        != NULL_TREE);
11863   if (nested_name_specifier_p)
11864     template_p = cp_parser_optional_template_keyword (parser);
11865   /* If there is a `::' operator or a nested-name-specifier, then we
11866      are definitely looking for a class-name.  */
11867   if (global_scope_p || nested_name_specifier_p)
11868     return cp_parser_class_name (parser,
11869                                  /*typename_keyword_p=*/true,
11870                                  /*template_keyword_p=*/template_p,
11871                                  typename_type,
11872                                  /*check_dependency_p=*/true,
11873                                  /*class_head_p=*/false,
11874                                  /*is_declaration=*/true);
11875   /* Otherwise, we could also be looking for an ordinary identifier.  */
11876   cp_parser_parse_tentatively (parser);
11877   /* Try a class-name.  */
11878   id = cp_parser_class_name (parser,
11879                              /*typename_keyword_p=*/true,
11880                              /*template_keyword_p=*/false,
11881                              none_type,
11882                              /*check_dependency_p=*/true,
11883                              /*class_head_p=*/false,
11884                              /*is_declaration=*/true);
11885   /* If we found one, we're done.  */
11886   if (cp_parser_parse_definitely (parser))
11887     return id;
11888   /* Otherwise, look for an ordinary identifier.  */
11889   return cp_parser_identifier (parser);
11890 }
11891
11892 /* Overloading [gram.over] */
11893
11894 /* Parse an operator-function-id.
11895
11896    operator-function-id:
11897      operator operator
11898
11899    Returns an IDENTIFIER_NODE for the operator which is a
11900    human-readable spelling of the identifier, e.g., `operator +'.  */
11901
11902 static tree
11903 cp_parser_operator_function_id (cp_parser* parser)
11904 {
11905   /* Look for the `operator' keyword.  */
11906   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11907     return error_mark_node;
11908   /* And then the name of the operator itself.  */
11909   return cp_parser_operator (parser);
11910 }
11911
11912 /* Return an identifier node for a user-defined literal operator.
11913    The suffix identifier is chained to the operator name identifier.  */
11914
11915 static tree
11916 cp_literal_operator_id (const char* name)
11917 {
11918   tree identifier;
11919   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11920                               + strlen (name) + 10);
11921   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11922   identifier = get_identifier (buffer);
11923   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11924
11925   return identifier;
11926 }
11927
11928 /* Parse an operator.
11929
11930    operator:
11931      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11932      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11933      || ++ -- , ->* -> () []
11934
11935    GNU Extensions:
11936
11937    operator:
11938      <? >? <?= >?=
11939
11940    Returns an IDENTIFIER_NODE for the operator which is a
11941    human-readable spelling of the identifier, e.g., `operator +'.  */
11942
11943 static tree
11944 cp_parser_operator (cp_parser* parser)
11945 {
11946   tree id = NULL_TREE;
11947   cp_token *token;
11948
11949   /* Peek at the next token.  */
11950   token = cp_lexer_peek_token (parser->lexer);
11951   /* Figure out which operator we have.  */
11952   switch (token->type)
11953     {
11954     case CPP_KEYWORD:
11955       {
11956         enum tree_code op;
11957
11958         /* The keyword should be either `new' or `delete'.  */
11959         if (token->keyword == RID_NEW)
11960           op = NEW_EXPR;
11961         else if (token->keyword == RID_DELETE)
11962           op = DELETE_EXPR;
11963         else
11964           break;
11965
11966         /* Consume the `new' or `delete' token.  */
11967         cp_lexer_consume_token (parser->lexer);
11968
11969         /* Peek at the next token.  */
11970         token = cp_lexer_peek_token (parser->lexer);
11971         /* If it's a `[' token then this is the array variant of the
11972            operator.  */
11973         if (token->type == CPP_OPEN_SQUARE)
11974           {
11975             /* Consume the `[' token.  */
11976             cp_lexer_consume_token (parser->lexer);
11977             /* Look for the `]' token.  */
11978             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11979             id = ansi_opname (op == NEW_EXPR
11980                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11981           }
11982         /* Otherwise, we have the non-array variant.  */
11983         else
11984           id = ansi_opname (op);
11985
11986         return id;
11987       }
11988
11989     case CPP_PLUS:
11990       id = ansi_opname (PLUS_EXPR);
11991       break;
11992
11993     case CPP_MINUS:
11994       id = ansi_opname (MINUS_EXPR);
11995       break;
11996
11997     case CPP_MULT:
11998       id = ansi_opname (MULT_EXPR);
11999       break;
12000
12001     case CPP_DIV:
12002       id = ansi_opname (TRUNC_DIV_EXPR);
12003       break;
12004
12005     case CPP_MOD:
12006       id = ansi_opname (TRUNC_MOD_EXPR);
12007       break;
12008
12009     case CPP_XOR:
12010       id = ansi_opname (BIT_XOR_EXPR);
12011       break;
12012
12013     case CPP_AND:
12014       id = ansi_opname (BIT_AND_EXPR);
12015       break;
12016
12017     case CPP_OR:
12018       id = ansi_opname (BIT_IOR_EXPR);
12019       break;
12020
12021     case CPP_COMPL:
12022       id = ansi_opname (BIT_NOT_EXPR);
12023       break;
12024
12025     case CPP_NOT:
12026       id = ansi_opname (TRUTH_NOT_EXPR);
12027       break;
12028
12029     case CPP_EQ:
12030       id = ansi_assopname (NOP_EXPR);
12031       break;
12032
12033     case CPP_LESS:
12034       id = ansi_opname (LT_EXPR);
12035       break;
12036
12037     case CPP_GREATER:
12038       id = ansi_opname (GT_EXPR);
12039       break;
12040
12041     case CPP_PLUS_EQ:
12042       id = ansi_assopname (PLUS_EXPR);
12043       break;
12044
12045     case CPP_MINUS_EQ:
12046       id = ansi_assopname (MINUS_EXPR);
12047       break;
12048
12049     case CPP_MULT_EQ:
12050       id = ansi_assopname (MULT_EXPR);
12051       break;
12052
12053     case CPP_DIV_EQ:
12054       id = ansi_assopname (TRUNC_DIV_EXPR);
12055       break;
12056
12057     case CPP_MOD_EQ:
12058       id = ansi_assopname (TRUNC_MOD_EXPR);
12059       break;
12060
12061     case CPP_XOR_EQ:
12062       id = ansi_assopname (BIT_XOR_EXPR);
12063       break;
12064
12065     case CPP_AND_EQ:
12066       id = ansi_assopname (BIT_AND_EXPR);
12067       break;
12068
12069     case CPP_OR_EQ:
12070       id = ansi_assopname (BIT_IOR_EXPR);
12071       break;
12072
12073     case CPP_LSHIFT:
12074       id = ansi_opname (LSHIFT_EXPR);
12075       break;
12076
12077     case CPP_RSHIFT:
12078       id = ansi_opname (RSHIFT_EXPR);
12079       break;
12080
12081     case CPP_LSHIFT_EQ:
12082       id = ansi_assopname (LSHIFT_EXPR);
12083       break;
12084
12085     case CPP_RSHIFT_EQ:
12086       id = ansi_assopname (RSHIFT_EXPR);
12087       break;
12088
12089     case CPP_EQ_EQ:
12090       id = ansi_opname (EQ_EXPR);
12091       break;
12092
12093     case CPP_NOT_EQ:
12094       id = ansi_opname (NE_EXPR);
12095       break;
12096
12097     case CPP_LESS_EQ:
12098       id = ansi_opname (LE_EXPR);
12099       break;
12100
12101     case CPP_GREATER_EQ:
12102       id = ansi_opname (GE_EXPR);
12103       break;
12104
12105     case CPP_AND_AND:
12106       id = ansi_opname (TRUTH_ANDIF_EXPR);
12107       break;
12108
12109     case CPP_OR_OR:
12110       id = ansi_opname (TRUTH_ORIF_EXPR);
12111       break;
12112
12113     case CPP_PLUS_PLUS:
12114       id = ansi_opname (POSTINCREMENT_EXPR);
12115       break;
12116
12117     case CPP_MINUS_MINUS:
12118       id = ansi_opname (PREDECREMENT_EXPR);
12119       break;
12120
12121     case CPP_COMMA:
12122       id = ansi_opname (COMPOUND_EXPR);
12123       break;
12124
12125     case CPP_DEREF_STAR:
12126       id = ansi_opname (MEMBER_REF);
12127       break;
12128
12129     case CPP_DEREF:
12130       id = ansi_opname (COMPONENT_REF);
12131       break;
12132
12133     case CPP_OPEN_PAREN:
12134       /* Consume the `('.  */
12135       cp_lexer_consume_token (parser->lexer);
12136       /* Look for the matching `)'.  */
12137       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12138       return ansi_opname (CALL_EXPR);
12139
12140     case CPP_OPEN_SQUARE:
12141       /* Consume the `['.  */
12142       cp_lexer_consume_token (parser->lexer);
12143       /* Look for the matching `]'.  */
12144       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12145       return ansi_opname (ARRAY_REF);
12146
12147     case CPP_STRING:
12148       if (cxx_dialect == cxx98)
12149         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12150       if (TREE_STRING_LENGTH (token->u.value) > 2)
12151         {
12152           error ("expected empty string after %<operator%> keyword");
12153           return error_mark_node;
12154         }
12155       /* Consume the string.  */
12156       cp_lexer_consume_token (parser->lexer);
12157       /* Look for the suffix identifier.  */
12158       token = cp_lexer_peek_token (parser->lexer);
12159       if (token->type == CPP_NAME)
12160         {
12161           id = cp_parser_identifier (parser);
12162           if (id != error_mark_node)
12163             {
12164               const char *name = IDENTIFIER_POINTER (id);
12165               return cp_literal_operator_id (name);
12166             }
12167         }
12168       else
12169         {
12170           error ("expected suffix identifier");
12171           return error_mark_node;
12172         }
12173
12174     case CPP_STRING_USERDEF:
12175       error ("missing space between %<\"\"%> and suffix identifier");
12176       return error_mark_node;
12177
12178     default:
12179       /* Anything else is an error.  */
12180       break;
12181     }
12182
12183   /* If we have selected an identifier, we need to consume the
12184      operator token.  */
12185   if (id)
12186     cp_lexer_consume_token (parser->lexer);
12187   /* Otherwise, no valid operator name was present.  */
12188   else
12189     {
12190       cp_parser_error (parser, "expected operator");
12191       id = error_mark_node;
12192     }
12193
12194   return id;
12195 }
12196
12197 /* Parse a template-declaration.
12198
12199    template-declaration:
12200      export [opt] template < template-parameter-list > declaration
12201
12202    If MEMBER_P is TRUE, this template-declaration occurs within a
12203    class-specifier.
12204
12205    The grammar rule given by the standard isn't correct.  What
12206    is really meant is:
12207
12208    template-declaration:
12209      export [opt] template-parameter-list-seq
12210        decl-specifier-seq [opt] init-declarator [opt] ;
12211      export [opt] template-parameter-list-seq
12212        function-definition
12213
12214    template-parameter-list-seq:
12215      template-parameter-list-seq [opt]
12216      template < template-parameter-list >  */
12217
12218 static void
12219 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12220 {
12221   /* Check for `export'.  */
12222   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12223     {
12224       /* Consume the `export' token.  */
12225       cp_lexer_consume_token (parser->lexer);
12226       /* Warn that we do not support `export'.  */
12227       warning (0, "keyword %<export%> not implemented, and will be ignored");
12228     }
12229
12230   cp_parser_template_declaration_after_export (parser, member_p);
12231 }
12232
12233 /* Parse a template-parameter-list.
12234
12235    template-parameter-list:
12236      template-parameter
12237      template-parameter-list , template-parameter
12238
12239    Returns a TREE_LIST.  Each node represents a template parameter.
12240    The nodes are connected via their TREE_CHAINs.  */
12241
12242 static tree
12243 cp_parser_template_parameter_list (cp_parser* parser)
12244 {
12245   tree parameter_list = NULL_TREE;
12246
12247   begin_template_parm_list ();
12248
12249   /* The loop below parses the template parms.  We first need to know
12250      the total number of template parms to be able to compute proper
12251      canonical types of each dependent type. So after the loop, when
12252      we know the total number of template parms,
12253      end_template_parm_list computes the proper canonical types and
12254      fixes up the dependent types accordingly.  */
12255   while (true)
12256     {
12257       tree parameter;
12258       bool is_non_type;
12259       bool is_parameter_pack;
12260       location_t parm_loc;
12261
12262       /* Parse the template-parameter.  */
12263       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12264       parameter = cp_parser_template_parameter (parser, 
12265                                                 &is_non_type,
12266                                                 &is_parameter_pack);
12267       /* Add it to the list.  */
12268       if (parameter != error_mark_node)
12269         parameter_list = process_template_parm (parameter_list,
12270                                                 parm_loc,
12271                                                 parameter,
12272                                                 is_non_type,
12273                                                 is_parameter_pack);
12274       else
12275        {
12276          tree err_parm = build_tree_list (parameter, parameter);
12277          parameter_list = chainon (parameter_list, err_parm);
12278        }
12279
12280       /* If the next token is not a `,', we're done.  */
12281       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12282         break;
12283       /* Otherwise, consume the `,' token.  */
12284       cp_lexer_consume_token (parser->lexer);
12285     }
12286
12287   return end_template_parm_list (parameter_list);
12288 }
12289
12290 /* Parse a template-parameter.
12291
12292    template-parameter:
12293      type-parameter
12294      parameter-declaration
12295
12296    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
12297    the parameter.  The TREE_PURPOSE is the default value, if any.
12298    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
12299    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
12300    set to true iff this parameter is a parameter pack. */
12301
12302 static tree
12303 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12304                               bool *is_parameter_pack)
12305 {
12306   cp_token *token;
12307   cp_parameter_declarator *parameter_declarator;
12308   cp_declarator *id_declarator;
12309   tree parm;
12310
12311   /* Assume it is a type parameter or a template parameter.  */
12312   *is_non_type = false;
12313   /* Assume it not a parameter pack. */
12314   *is_parameter_pack = false;
12315   /* Peek at the next token.  */
12316   token = cp_lexer_peek_token (parser->lexer);
12317   /* If it is `class' or `template', we have a type-parameter.  */
12318   if (token->keyword == RID_TEMPLATE)
12319     return cp_parser_type_parameter (parser, is_parameter_pack);
12320   /* If it is `class' or `typename' we do not know yet whether it is a
12321      type parameter or a non-type parameter.  Consider:
12322
12323        template <typename T, typename T::X X> ...
12324
12325      or:
12326
12327        template <class C, class D*> ...
12328
12329      Here, the first parameter is a type parameter, and the second is
12330      a non-type parameter.  We can tell by looking at the token after
12331      the identifier -- if it is a `,', `=', or `>' then we have a type
12332      parameter.  */
12333   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12334     {
12335       /* Peek at the token after `class' or `typename'.  */
12336       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12337       /* If it's an ellipsis, we have a template type parameter
12338          pack. */
12339       if (token->type == CPP_ELLIPSIS)
12340         return cp_parser_type_parameter (parser, is_parameter_pack);
12341       /* If it's an identifier, skip it.  */
12342       if (token->type == CPP_NAME)
12343         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12344       /* Now, see if the token looks like the end of a template
12345          parameter.  */
12346       if (token->type == CPP_COMMA
12347           || token->type == CPP_EQ
12348           || token->type == CPP_GREATER)
12349         return cp_parser_type_parameter (parser, is_parameter_pack);
12350     }
12351
12352   /* Otherwise, it is a non-type parameter.
12353
12354      [temp.param]
12355
12356      When parsing a default template-argument for a non-type
12357      template-parameter, the first non-nested `>' is taken as the end
12358      of the template parameter-list rather than a greater-than
12359      operator.  */
12360   *is_non_type = true;
12361   parameter_declarator
12362      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12363                                         /*parenthesized_p=*/NULL);
12364
12365   /* If the parameter declaration is marked as a parameter pack, set
12366      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12367      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12368      grokdeclarator. */
12369   if (parameter_declarator
12370       && parameter_declarator->declarator
12371       && parameter_declarator->declarator->parameter_pack_p)
12372     {
12373       *is_parameter_pack = true;
12374       parameter_declarator->declarator->parameter_pack_p = false;
12375     }
12376
12377   if (parameter_declarator
12378       && parameter_declarator->default_argument)
12379     {
12380       /* Can happen in some cases of erroneous input (c++/34892).  */
12381       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12382         /* Consume the `...' for better error recovery.  */
12383         cp_lexer_consume_token (parser->lexer);
12384     }
12385   /* If the next token is an ellipsis, and we don't already have it
12386      marked as a parameter pack, then we have a parameter pack (that
12387      has no declarator).  */
12388   else if (!*is_parameter_pack
12389            && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12390            && (declarator_can_be_parameter_pack
12391                (parameter_declarator->declarator)))
12392     {
12393       /* Consume the `...'.  */
12394       cp_lexer_consume_token (parser->lexer);
12395       maybe_warn_variadic_templates ();
12396       
12397       *is_parameter_pack = true;
12398     }
12399   /* We might end up with a pack expansion as the type of the non-type
12400      template parameter, in which case this is a non-type template
12401      parameter pack.  */
12402   else if (parameter_declarator
12403            && parameter_declarator->decl_specifiers.type
12404            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12405     {
12406       *is_parameter_pack = true;
12407       parameter_declarator->decl_specifiers.type = 
12408         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12409     }
12410
12411   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12412     {
12413       /* Parameter packs cannot have default arguments.  However, a
12414          user may try to do so, so we'll parse them and give an
12415          appropriate diagnostic here.  */
12416
12417       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12418       
12419       /* Find the name of the parameter pack.  */     
12420       id_declarator = parameter_declarator->declarator;
12421       while (id_declarator && id_declarator->kind != cdk_id)
12422         id_declarator = id_declarator->declarator;
12423       
12424       if (id_declarator && id_declarator->kind == cdk_id)
12425         error_at (start_token->location,
12426                   "template parameter pack %qD cannot have a default argument",
12427                   id_declarator->u.id.unqualified_name);
12428       else
12429         error_at (start_token->location,
12430                   "template parameter pack cannot have a default argument");
12431       
12432       /* Parse the default argument, but throw away the result.  */
12433       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12434     }
12435
12436   parm = grokdeclarator (parameter_declarator->declarator,
12437                          &parameter_declarator->decl_specifiers,
12438                          TPARM, /*initialized=*/0,
12439                          /*attrlist=*/NULL);
12440   if (parm == error_mark_node)
12441     return error_mark_node;
12442
12443   return build_tree_list (parameter_declarator->default_argument, parm);
12444 }
12445
12446 /* Parse a type-parameter.
12447
12448    type-parameter:
12449      class identifier [opt]
12450      class identifier [opt] = type-id
12451      typename identifier [opt]
12452      typename identifier [opt] = type-id
12453      template < template-parameter-list > class identifier [opt]
12454      template < template-parameter-list > class identifier [opt]
12455        = id-expression
12456
12457    GNU Extension (variadic templates):
12458
12459    type-parameter:
12460      class ... identifier [opt]
12461      typename ... identifier [opt]
12462
12463    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12464    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12465    the declaration of the parameter.
12466
12467    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12468
12469 static tree
12470 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12471 {
12472   cp_token *token;
12473   tree parameter;
12474
12475   /* Look for a keyword to tell us what kind of parameter this is.  */
12476   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12477   if (!token)
12478     return error_mark_node;
12479
12480   switch (token->keyword)
12481     {
12482     case RID_CLASS:
12483     case RID_TYPENAME:
12484       {
12485         tree identifier;
12486         tree default_argument;
12487
12488         /* If the next token is an ellipsis, we have a template
12489            argument pack. */
12490         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12491           {
12492             /* Consume the `...' token. */
12493             cp_lexer_consume_token (parser->lexer);
12494             maybe_warn_variadic_templates ();
12495
12496             *is_parameter_pack = true;
12497           }
12498
12499         /* If the next token is an identifier, then it names the
12500            parameter.  */
12501         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12502           identifier = cp_parser_identifier (parser);
12503         else
12504           identifier = NULL_TREE;
12505
12506         /* Create the parameter.  */
12507         parameter = finish_template_type_parm (class_type_node, identifier);
12508
12509         /* If the next token is an `=', we have a default argument.  */
12510         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12511           {
12512             /* Consume the `=' token.  */
12513             cp_lexer_consume_token (parser->lexer);
12514             /* Parse the default-argument.  */
12515             push_deferring_access_checks (dk_no_deferred);
12516             default_argument = cp_parser_type_id (parser);
12517
12518             /* Template parameter packs cannot have default
12519                arguments. */
12520             if (*is_parameter_pack)
12521               {
12522                 if (identifier)
12523                   error_at (token->location,
12524                             "template parameter pack %qD cannot have a "
12525                             "default argument", identifier);
12526                 else
12527                   error_at (token->location,
12528                             "template parameter packs cannot have "
12529                             "default arguments");
12530                 default_argument = NULL_TREE;
12531               }
12532             pop_deferring_access_checks ();
12533           }
12534         else
12535           default_argument = NULL_TREE;
12536
12537         /* Create the combined representation of the parameter and the
12538            default argument.  */
12539         parameter = build_tree_list (default_argument, parameter);
12540       }
12541       break;
12542
12543     case RID_TEMPLATE:
12544       {
12545         tree identifier;
12546         tree default_argument;
12547
12548         /* Look for the `<'.  */
12549         cp_parser_require (parser, CPP_LESS, RT_LESS);
12550         /* Parse the template-parameter-list.  */
12551         cp_parser_template_parameter_list (parser);
12552         /* Look for the `>'.  */
12553         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12554         /* Look for the `class' keyword.  */
12555         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12556         /* If the next token is an ellipsis, we have a template
12557            argument pack. */
12558         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12559           {
12560             /* Consume the `...' token. */
12561             cp_lexer_consume_token (parser->lexer);
12562             maybe_warn_variadic_templates ();
12563
12564             *is_parameter_pack = true;
12565           }
12566         /* If the next token is an `=', then there is a
12567            default-argument.  If the next token is a `>', we are at
12568            the end of the parameter-list.  If the next token is a `,',
12569            then we are at the end of this parameter.  */
12570         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12571             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12572             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12573           {
12574             identifier = cp_parser_identifier (parser);
12575             /* Treat invalid names as if the parameter were nameless.  */
12576             if (identifier == error_mark_node)
12577               identifier = NULL_TREE;
12578           }
12579         else
12580           identifier = NULL_TREE;
12581
12582         /* Create the template parameter.  */
12583         parameter = finish_template_template_parm (class_type_node,
12584                                                    identifier);
12585
12586         /* If the next token is an `=', then there is a
12587            default-argument.  */
12588         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12589           {
12590             bool is_template;
12591
12592             /* Consume the `='.  */
12593             cp_lexer_consume_token (parser->lexer);
12594             /* Parse the id-expression.  */
12595             push_deferring_access_checks (dk_no_deferred);
12596             /* save token before parsing the id-expression, for error
12597                reporting */
12598             token = cp_lexer_peek_token (parser->lexer);
12599             default_argument
12600               = cp_parser_id_expression (parser,
12601                                          /*template_keyword_p=*/false,
12602                                          /*check_dependency_p=*/true,
12603                                          /*template_p=*/&is_template,
12604                                          /*declarator_p=*/false,
12605                                          /*optional_p=*/false);
12606             if (TREE_CODE (default_argument) == TYPE_DECL)
12607               /* If the id-expression was a template-id that refers to
12608                  a template-class, we already have the declaration here,
12609                  so no further lookup is needed.  */
12610                  ;
12611             else
12612               /* Look up the name.  */
12613               default_argument
12614                 = cp_parser_lookup_name (parser, default_argument,
12615                                          none_type,
12616                                          /*is_template=*/is_template,
12617                                          /*is_namespace=*/false,
12618                                          /*check_dependency=*/true,
12619                                          /*ambiguous_decls=*/NULL,
12620                                          token->location);
12621             /* See if the default argument is valid.  */
12622             default_argument
12623               = check_template_template_default_arg (default_argument);
12624
12625             /* Template parameter packs cannot have default
12626                arguments. */
12627             if (*is_parameter_pack)
12628               {
12629                 if (identifier)
12630                   error_at (token->location,
12631                             "template parameter pack %qD cannot "
12632                             "have a default argument",
12633                             identifier);
12634                 else
12635                   error_at (token->location, "template parameter packs cannot "
12636                             "have default arguments");
12637                 default_argument = NULL_TREE;
12638               }
12639             pop_deferring_access_checks ();
12640           }
12641         else
12642           default_argument = NULL_TREE;
12643
12644         /* Create the combined representation of the parameter and the
12645            default argument.  */
12646         parameter = build_tree_list (default_argument, parameter);
12647       }
12648       break;
12649
12650     default:
12651       gcc_unreachable ();
12652       break;
12653     }
12654
12655   return parameter;
12656 }
12657
12658 /* Parse a template-id.
12659
12660    template-id:
12661      template-name < template-argument-list [opt] >
12662
12663    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12664    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12665    returned.  Otherwise, if the template-name names a function, or set
12666    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12667    names a class, returns a TYPE_DECL for the specialization.
12668
12669    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12670    uninstantiated templates.  */
12671
12672 static tree
12673 cp_parser_template_id (cp_parser *parser,
12674                        bool template_keyword_p,
12675                        bool check_dependency_p,
12676                        enum tag_types tag_type,
12677                        bool is_declaration)
12678 {
12679   int i;
12680   tree templ;
12681   tree arguments;
12682   tree template_id;
12683   cp_token_position start_of_id = 0;
12684   deferred_access_check *chk;
12685   vec<deferred_access_check, va_gc> *access_check;
12686   cp_token *next_token = NULL, *next_token_2 = NULL;
12687   bool is_identifier;
12688
12689   /* If the next token corresponds to a template-id, there is no need
12690      to reparse it.  */
12691   next_token = cp_lexer_peek_token (parser->lexer);
12692   if (next_token->type == CPP_TEMPLATE_ID)
12693     {
12694       struct tree_check *check_value;
12695
12696       /* Get the stored value.  */
12697       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12698       /* Perform any access checks that were deferred.  */
12699       access_check = check_value->checks;
12700       if (access_check)
12701         {
12702           FOR_EACH_VEC_ELT (*access_check, i, chk)
12703             perform_or_defer_access_check (chk->binfo,
12704                                            chk->decl,
12705                                            chk->diag_decl,
12706                                            tf_warning_or_error);
12707         }
12708       /* Return the stored value.  */
12709       return check_value->value;
12710     }
12711
12712   /* Avoid performing name lookup if there is no possibility of
12713      finding a template-id.  */
12714   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12715       || (next_token->type == CPP_NAME
12716           && !cp_parser_nth_token_starts_template_argument_list_p
12717                (parser, 2)))
12718     {
12719       cp_parser_error (parser, "expected template-id");
12720       return error_mark_node;
12721     }
12722
12723   /* Remember where the template-id starts.  */
12724   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12725     start_of_id = cp_lexer_token_position (parser->lexer, false);
12726
12727   push_deferring_access_checks (dk_deferred);
12728
12729   /* Parse the template-name.  */
12730   is_identifier = false;
12731   templ = cp_parser_template_name (parser, template_keyword_p,
12732                                    check_dependency_p,
12733                                    is_declaration,
12734                                    tag_type,
12735                                    &is_identifier);
12736   if (templ == error_mark_node || is_identifier)
12737     {
12738       pop_deferring_access_checks ();
12739       return templ;
12740     }
12741
12742   /* If we find the sequence `[:' after a template-name, it's probably
12743      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12744      parse correctly the argument list.  */
12745   next_token = cp_lexer_peek_token (parser->lexer);
12746   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12747   if (next_token->type == CPP_OPEN_SQUARE
12748       && next_token->flags & DIGRAPH
12749       && next_token_2->type == CPP_COLON
12750       && !(next_token_2->flags & PREV_WHITE))
12751     {
12752       cp_parser_parse_tentatively (parser);
12753       /* Change `:' into `::'.  */
12754       next_token_2->type = CPP_SCOPE;
12755       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12756          CPP_LESS.  */
12757       cp_lexer_consume_token (parser->lexer);
12758
12759       /* Parse the arguments.  */
12760       arguments = cp_parser_enclosed_template_argument_list (parser);
12761       if (!cp_parser_parse_definitely (parser))
12762         {
12763           /* If we couldn't parse an argument list, then we revert our changes
12764              and return simply an error. Maybe this is not a template-id
12765              after all.  */
12766           next_token_2->type = CPP_COLON;
12767           cp_parser_error (parser, "expected %<<%>");
12768           pop_deferring_access_checks ();
12769           return error_mark_node;
12770         }
12771       /* Otherwise, emit an error about the invalid digraph, but continue
12772          parsing because we got our argument list.  */
12773       if (permerror (next_token->location,
12774                      "%<<::%> cannot begin a template-argument list"))
12775         {
12776           static bool hint = false;
12777           inform (next_token->location,
12778                   "%<<:%> is an alternate spelling for %<[%>."
12779                   " Insert whitespace between %<<%> and %<::%>");
12780           if (!hint && !flag_permissive)
12781             {
12782               inform (next_token->location, "(if you use %<-fpermissive%> "
12783                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12784                       "accept your code)");
12785               hint = true;
12786             }
12787         }
12788     }
12789   else
12790     {
12791       /* Look for the `<' that starts the template-argument-list.  */
12792       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12793         {
12794           pop_deferring_access_checks ();
12795           return error_mark_node;
12796         }
12797       /* Parse the arguments.  */
12798       arguments = cp_parser_enclosed_template_argument_list (parser);
12799     }
12800
12801   /* Build a representation of the specialization.  */
12802   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12803     template_id = build_min_nt_loc (next_token->location,
12804                                     TEMPLATE_ID_EXPR,
12805                                     templ, arguments);
12806   else if (DECL_TYPE_TEMPLATE_P (templ)
12807            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12808     {
12809       bool entering_scope;
12810       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12811          template (rather than some instantiation thereof) only if
12812          is not nested within some other construct.  For example, in
12813          "template <typename T> void f(T) { A<T>::", A<T> is just an
12814          instantiation of A.  */
12815       entering_scope = (template_parm_scope_p ()
12816                         && cp_lexer_next_token_is (parser->lexer,
12817                                                    CPP_SCOPE));
12818       template_id
12819         = finish_template_type (templ, arguments, entering_scope);
12820     }
12821   else
12822     {
12823       /* If it's not a class-template or a template-template, it should be
12824          a function-template.  */
12825       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12826                    || TREE_CODE (templ) == OVERLOAD
12827                    || BASELINK_P (templ)));
12828
12829       template_id = lookup_template_function (templ, arguments);
12830     }
12831
12832   /* If parsing tentatively, replace the sequence of tokens that makes
12833      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12834      should we re-parse the token stream, we will not have to repeat
12835      the effort required to do the parse, nor will we issue duplicate
12836      error messages about problems during instantiation of the
12837      template.  */
12838   if (start_of_id)
12839     {
12840       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12841
12842       /* Reset the contents of the START_OF_ID token.  */
12843       token->type = CPP_TEMPLATE_ID;
12844       /* Retrieve any deferred checks.  Do not pop this access checks yet
12845          so the memory will not be reclaimed during token replacing below.  */
12846       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12847       token->u.tree_check_value->value = template_id;
12848       token->u.tree_check_value->checks = get_deferred_access_checks ();
12849       token->keyword = RID_MAX;
12850
12851       /* Purge all subsequent tokens.  */
12852       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12853
12854       /* ??? Can we actually assume that, if template_id ==
12855          error_mark_node, we will have issued a diagnostic to the
12856          user, as opposed to simply marking the tentative parse as
12857          failed?  */
12858       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12859         error_at (token->location, "parse error in template argument list");
12860     }
12861
12862   pop_deferring_access_checks ();
12863   return template_id;
12864 }
12865
12866 /* Parse a template-name.
12867
12868    template-name:
12869      identifier
12870
12871    The standard should actually say:
12872
12873    template-name:
12874      identifier
12875      operator-function-id
12876
12877    A defect report has been filed about this issue.
12878
12879    A conversion-function-id cannot be a template name because they cannot
12880    be part of a template-id. In fact, looking at this code:
12881
12882    a.operator K<int>()
12883
12884    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12885    It is impossible to call a templated conversion-function-id with an
12886    explicit argument list, since the only allowed template parameter is
12887    the type to which it is converting.
12888
12889    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12890    `template' keyword, in a construction like:
12891
12892      T::template f<3>()
12893
12894    In that case `f' is taken to be a template-name, even though there
12895    is no way of knowing for sure.
12896
12897    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12898    name refers to a set of overloaded functions, at least one of which
12899    is a template, or an IDENTIFIER_NODE with the name of the template,
12900    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12901    names are looked up inside uninstantiated templates.  */
12902
12903 static tree
12904 cp_parser_template_name (cp_parser* parser,
12905                          bool template_keyword_p,
12906                          bool check_dependency_p,
12907                          bool is_declaration,
12908                          enum tag_types tag_type,
12909                          bool *is_identifier)
12910 {
12911   tree identifier;
12912   tree decl;
12913   tree fns;
12914   cp_token *token = cp_lexer_peek_token (parser->lexer);
12915
12916   /* If the next token is `operator', then we have either an
12917      operator-function-id or a conversion-function-id.  */
12918   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12919     {
12920       /* We don't know whether we're looking at an
12921          operator-function-id or a conversion-function-id.  */
12922       cp_parser_parse_tentatively (parser);
12923       /* Try an operator-function-id.  */
12924       identifier = cp_parser_operator_function_id (parser);
12925       /* If that didn't work, try a conversion-function-id.  */
12926       if (!cp_parser_parse_definitely (parser))
12927         {
12928           cp_parser_error (parser, "expected template-name");
12929           return error_mark_node;
12930         }
12931     }
12932   /* Look for the identifier.  */
12933   else
12934     identifier = cp_parser_identifier (parser);
12935
12936   /* If we didn't find an identifier, we don't have a template-id.  */
12937   if (identifier == error_mark_node)
12938     return error_mark_node;
12939
12940   /* If the name immediately followed the `template' keyword, then it
12941      is a template-name.  However, if the next token is not `<', then
12942      we do not treat it as a template-name, since it is not being used
12943      as part of a template-id.  This enables us to handle constructs
12944      like:
12945
12946        template <typename T> struct S { S(); };
12947        template <typename T> S<T>::S();
12948
12949      correctly.  We would treat `S' as a template -- if it were `S<T>'
12950      -- but we do not if there is no `<'.  */
12951
12952   if (processing_template_decl
12953       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12954     {
12955       /* In a declaration, in a dependent context, we pretend that the
12956          "template" keyword was present in order to improve error
12957          recovery.  For example, given:
12958
12959            template <typename T> void f(T::X<int>);
12960
12961          we want to treat "X<int>" as a template-id.  */
12962       if (is_declaration
12963           && !template_keyword_p
12964           && parser->scope && TYPE_P (parser->scope)
12965           && check_dependency_p
12966           && dependent_scope_p (parser->scope)
12967           /* Do not do this for dtors (or ctors), since they never
12968              need the template keyword before their name.  */
12969           && !constructor_name_p (identifier, parser->scope))
12970         {
12971           cp_token_position start = 0;
12972
12973           /* Explain what went wrong.  */
12974           error_at (token->location, "non-template %qD used as template",
12975                     identifier);
12976           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12977                   parser->scope, identifier);
12978           /* If parsing tentatively, find the location of the "<" token.  */
12979           if (cp_parser_simulate_error (parser))
12980             start = cp_lexer_token_position (parser->lexer, true);
12981           /* Parse the template arguments so that we can issue error
12982              messages about them.  */
12983           cp_lexer_consume_token (parser->lexer);
12984           cp_parser_enclosed_template_argument_list (parser);
12985           /* Skip tokens until we find a good place from which to
12986              continue parsing.  */
12987           cp_parser_skip_to_closing_parenthesis (parser,
12988                                                  /*recovering=*/true,
12989                                                  /*or_comma=*/true,
12990                                                  /*consume_paren=*/false);
12991           /* If parsing tentatively, permanently remove the
12992              template argument list.  That will prevent duplicate
12993              error messages from being issued about the missing
12994              "template" keyword.  */
12995           if (start)
12996             cp_lexer_purge_tokens_after (parser->lexer, start);
12997           if (is_identifier)
12998             *is_identifier = true;
12999           return identifier;
13000         }
13001
13002       /* If the "template" keyword is present, then there is generally
13003          no point in doing name-lookup, so we just return IDENTIFIER.
13004          But, if the qualifying scope is non-dependent then we can
13005          (and must) do name-lookup normally.  */
13006       if (template_keyword_p
13007           && (!parser->scope
13008               || (TYPE_P (parser->scope)
13009                   && dependent_type_p (parser->scope))))
13010         return identifier;
13011     }
13012
13013   /* Look up the name.  */
13014   decl = cp_parser_lookup_name (parser, identifier,
13015                                 tag_type,
13016                                 /*is_template=*/true,
13017                                 /*is_namespace=*/false,
13018                                 check_dependency_p,
13019                                 /*ambiguous_decls=*/NULL,
13020                                 token->location);
13021
13022   /* If DECL is a template, then the name was a template-name.  */
13023   if (TREE_CODE (decl) == TEMPLATE_DECL)
13024     ;
13025   else
13026     {
13027       tree fn = NULL_TREE;
13028
13029       /* The standard does not explicitly indicate whether a name that
13030          names a set of overloaded declarations, some of which are
13031          templates, is a template-name.  However, such a name should
13032          be a template-name; otherwise, there is no way to form a
13033          template-id for the overloaded templates.  */
13034       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13035       if (TREE_CODE (fns) == OVERLOAD)
13036         for (fn = fns; fn; fn = OVL_NEXT (fn))
13037           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13038             break;
13039
13040       if (!fn)
13041         {
13042           /* The name does not name a template.  */
13043           cp_parser_error (parser, "expected template-name");
13044           return error_mark_node;
13045         }
13046     }
13047
13048   /* If DECL is dependent, and refers to a function, then just return
13049      its name; we will look it up again during template instantiation.  */
13050   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13051     {
13052       tree scope = ovl_scope (decl);
13053       if (TYPE_P (scope) && dependent_type_p (scope))
13054         return identifier;
13055     }
13056
13057   return decl;
13058 }
13059
13060 /* Parse a template-argument-list.
13061
13062    template-argument-list:
13063      template-argument ... [opt]
13064      template-argument-list , template-argument ... [opt]
13065
13066    Returns a TREE_VEC containing the arguments.  */
13067
13068 static tree
13069 cp_parser_template_argument_list (cp_parser* parser)
13070 {
13071   tree fixed_args[10];
13072   unsigned n_args = 0;
13073   unsigned alloced = 10;
13074   tree *arg_ary = fixed_args;
13075   tree vec;
13076   bool saved_in_template_argument_list_p;
13077   bool saved_ice_p;
13078   bool saved_non_ice_p;
13079
13080   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13081   parser->in_template_argument_list_p = true;
13082   /* Even if the template-id appears in an integral
13083      constant-expression, the contents of the argument list do
13084      not.  */
13085   saved_ice_p = parser->integral_constant_expression_p;
13086   parser->integral_constant_expression_p = false;
13087   saved_non_ice_p = parser->non_integral_constant_expression_p;
13088   parser->non_integral_constant_expression_p = false;
13089
13090   /* Parse the arguments.  */
13091   do
13092     {
13093       tree argument;
13094
13095       if (n_args)
13096         /* Consume the comma.  */
13097         cp_lexer_consume_token (parser->lexer);
13098
13099       /* Parse the template-argument.  */
13100       argument = cp_parser_template_argument (parser);
13101
13102       /* If the next token is an ellipsis, we're expanding a template
13103          argument pack. */
13104       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13105         {
13106           if (argument == error_mark_node)
13107             {
13108               cp_token *token = cp_lexer_peek_token (parser->lexer);
13109               error_at (token->location,
13110                         "expected parameter pack before %<...%>");
13111             }
13112           /* Consume the `...' token. */
13113           cp_lexer_consume_token (parser->lexer);
13114
13115           /* Make the argument into a TYPE_PACK_EXPANSION or
13116              EXPR_PACK_EXPANSION. */
13117           argument = make_pack_expansion (argument);
13118         }
13119
13120       if (n_args == alloced)
13121         {
13122           alloced *= 2;
13123
13124           if (arg_ary == fixed_args)
13125             {
13126               arg_ary = XNEWVEC (tree, alloced);
13127               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13128             }
13129           else
13130             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13131         }
13132       arg_ary[n_args++] = argument;
13133     }
13134   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13135
13136   vec = make_tree_vec (n_args);
13137
13138   while (n_args--)
13139     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13140
13141   if (arg_ary != fixed_args)
13142     free (arg_ary);
13143   parser->non_integral_constant_expression_p = saved_non_ice_p;
13144   parser->integral_constant_expression_p = saved_ice_p;
13145   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13146 #ifdef ENABLE_CHECKING
13147   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13148 #endif
13149   return vec;
13150 }
13151
13152 /* Parse a template-argument.
13153
13154    template-argument:
13155      assignment-expression
13156      type-id
13157      id-expression
13158
13159    The representation is that of an assignment-expression, type-id, or
13160    id-expression -- except that the qualified id-expression is
13161    evaluated, so that the value returned is either a DECL or an
13162    OVERLOAD.
13163
13164    Although the standard says "assignment-expression", it forbids
13165    throw-expressions or assignments in the template argument.
13166    Therefore, we use "conditional-expression" instead.  */
13167
13168 static tree
13169 cp_parser_template_argument (cp_parser* parser)
13170 {
13171   tree argument;
13172   bool template_p;
13173   bool address_p;
13174   bool maybe_type_id = false;
13175   cp_token *token = NULL, *argument_start_token = NULL;
13176   location_t loc = 0;
13177   cp_id_kind idk;
13178
13179   /* There's really no way to know what we're looking at, so we just
13180      try each alternative in order.
13181
13182        [temp.arg]
13183
13184        In a template-argument, an ambiguity between a type-id and an
13185        expression is resolved to a type-id, regardless of the form of
13186        the corresponding template-parameter.
13187
13188      Therefore, we try a type-id first.  */
13189   cp_parser_parse_tentatively (parser);
13190   argument = cp_parser_template_type_arg (parser);
13191   /* If there was no error parsing the type-id but the next token is a
13192      '>>', our behavior depends on which dialect of C++ we're
13193      parsing. In C++98, we probably found a typo for '> >'. But there
13194      are type-id which are also valid expressions. For instance:
13195
13196      struct X { int operator >> (int); };
13197      template <int V> struct Foo {};
13198      Foo<X () >> 5> r;
13199
13200      Here 'X()' is a valid type-id of a function type, but the user just
13201      wanted to write the expression "X() >> 5". Thus, we remember that we
13202      found a valid type-id, but we still try to parse the argument as an
13203      expression to see what happens. 
13204
13205      In C++0x, the '>>' will be considered two separate '>'
13206      tokens.  */
13207   if (!cp_parser_error_occurred (parser)
13208       && cxx_dialect == cxx98
13209       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13210     {
13211       maybe_type_id = true;
13212       cp_parser_abort_tentative_parse (parser);
13213     }
13214   else
13215     {
13216       /* If the next token isn't a `,' or a `>', then this argument wasn't
13217       really finished. This means that the argument is not a valid
13218       type-id.  */
13219       if (!cp_parser_next_token_ends_template_argument_p (parser))
13220         cp_parser_error (parser, "expected template-argument");
13221       /* If that worked, we're done.  */
13222       if (cp_parser_parse_definitely (parser))
13223         return argument;
13224     }
13225   /* We're still not sure what the argument will be.  */
13226   cp_parser_parse_tentatively (parser);
13227   /* Try a template.  */
13228   argument_start_token = cp_lexer_peek_token (parser->lexer);
13229   argument = cp_parser_id_expression (parser,
13230                                       /*template_keyword_p=*/false,
13231                                       /*check_dependency_p=*/true,
13232                                       &template_p,
13233                                       /*declarator_p=*/false,
13234                                       /*optional_p=*/false);
13235   /* If the next token isn't a `,' or a `>', then this argument wasn't
13236      really finished.  */
13237   if (!cp_parser_next_token_ends_template_argument_p (parser))
13238     cp_parser_error (parser, "expected template-argument");
13239   if (!cp_parser_error_occurred (parser))
13240     {
13241       /* Figure out what is being referred to.  If the id-expression
13242          was for a class template specialization, then we will have a
13243          TYPE_DECL at this point.  There is no need to do name lookup
13244          at this point in that case.  */
13245       if (TREE_CODE (argument) != TYPE_DECL)
13246         argument = cp_parser_lookup_name (parser, argument,
13247                                           none_type,
13248                                           /*is_template=*/template_p,
13249                                           /*is_namespace=*/false,
13250                                           /*check_dependency=*/true,
13251                                           /*ambiguous_decls=*/NULL,
13252                                           argument_start_token->location);
13253       if (TREE_CODE (argument) != TEMPLATE_DECL
13254           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13255         cp_parser_error (parser, "expected template-name");
13256     }
13257   if (cp_parser_parse_definitely (parser))
13258     return argument;
13259   /* It must be a non-type argument.  There permitted cases are given
13260      in [temp.arg.nontype]:
13261
13262      -- an integral constant-expression of integral or enumeration
13263         type; or
13264
13265      -- the name of a non-type template-parameter; or
13266
13267      -- the name of an object or function with external linkage...
13268
13269      -- the address of an object or function with external linkage...
13270
13271      -- a pointer to member...  */
13272   /* Look for a non-type template parameter.  */
13273   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13274     {
13275       cp_parser_parse_tentatively (parser);
13276       argument = cp_parser_primary_expression (parser,
13277                                                /*address_p=*/false,
13278                                                /*cast_p=*/false,
13279                                                /*template_arg_p=*/true,
13280                                                &idk);
13281       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13282           || !cp_parser_next_token_ends_template_argument_p (parser))
13283         cp_parser_simulate_error (parser);
13284       if (cp_parser_parse_definitely (parser))
13285         return argument;
13286     }
13287
13288   /* If the next token is "&", the argument must be the address of an
13289      object or function with external linkage.  */
13290   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13291   if (address_p)
13292     {
13293       loc = cp_lexer_peek_token (parser->lexer)->location;
13294       cp_lexer_consume_token (parser->lexer);
13295     }
13296   /* See if we might have an id-expression.  */
13297   token = cp_lexer_peek_token (parser->lexer);
13298   if (token->type == CPP_NAME
13299       || token->keyword == RID_OPERATOR
13300       || token->type == CPP_SCOPE
13301       || token->type == CPP_TEMPLATE_ID
13302       || token->type == CPP_NESTED_NAME_SPECIFIER)
13303     {
13304       cp_parser_parse_tentatively (parser);
13305       argument = cp_parser_primary_expression (parser,
13306                                                address_p,
13307                                                /*cast_p=*/false,
13308                                                /*template_arg_p=*/true,
13309                                                &idk);
13310       if (cp_parser_error_occurred (parser)
13311           || !cp_parser_next_token_ends_template_argument_p (parser))
13312         cp_parser_abort_tentative_parse (parser);
13313       else
13314         {
13315           tree probe;
13316
13317           if (TREE_CODE (argument) == INDIRECT_REF)
13318             {
13319               gcc_assert (REFERENCE_REF_P (argument));
13320               argument = TREE_OPERAND (argument, 0);
13321             }
13322
13323           /* If we're in a template, we represent a qualified-id referring
13324              to a static data member as a SCOPE_REF even if the scope isn't
13325              dependent so that we can check access control later.  */
13326           probe = argument;
13327           if (TREE_CODE (probe) == SCOPE_REF)
13328             probe = TREE_OPERAND (probe, 1);
13329           if (TREE_CODE (probe) == VAR_DECL)
13330             {
13331               /* A variable without external linkage might still be a
13332                  valid constant-expression, so no error is issued here
13333                  if the external-linkage check fails.  */
13334               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13335                 cp_parser_simulate_error (parser);
13336             }
13337           else if (is_overloaded_fn (argument))
13338             /* All overloaded functions are allowed; if the external
13339                linkage test does not pass, an error will be issued
13340                later.  */
13341             ;
13342           else if (address_p
13343                    && (TREE_CODE (argument) == OFFSET_REF
13344                        || TREE_CODE (argument) == SCOPE_REF))
13345             /* A pointer-to-member.  */
13346             ;
13347           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13348             ;
13349           else
13350             cp_parser_simulate_error (parser);
13351
13352           if (cp_parser_parse_definitely (parser))
13353             {
13354               if (address_p)
13355                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13356                                              tf_warning_or_error);
13357               return argument;
13358             }
13359         }
13360     }
13361   /* If the argument started with "&", there are no other valid
13362      alternatives at this point.  */
13363   if (address_p)
13364     {
13365       cp_parser_error (parser, "invalid non-type template argument");
13366       return error_mark_node;
13367     }
13368
13369   /* If the argument wasn't successfully parsed as a type-id followed
13370      by '>>', the argument can only be a constant expression now.
13371      Otherwise, we try parsing the constant-expression tentatively,
13372      because the argument could really be a type-id.  */
13373   if (maybe_type_id)
13374     cp_parser_parse_tentatively (parser);
13375   argument = cp_parser_constant_expression (parser,
13376                                             /*allow_non_constant_p=*/false,
13377                                             /*non_constant_p=*/NULL);
13378   argument = fold_non_dependent_expr (argument);
13379   if (!maybe_type_id)
13380     return argument;
13381   if (!cp_parser_next_token_ends_template_argument_p (parser))
13382     cp_parser_error (parser, "expected template-argument");
13383   if (cp_parser_parse_definitely (parser))
13384     return argument;
13385   /* We did our best to parse the argument as a non type-id, but that
13386      was the only alternative that matched (albeit with a '>' after
13387      it). We can assume it's just a typo from the user, and a
13388      diagnostic will then be issued.  */
13389   return cp_parser_template_type_arg (parser);
13390 }
13391
13392 /* Parse an explicit-instantiation.
13393
13394    explicit-instantiation:
13395      template declaration
13396
13397    Although the standard says `declaration', what it really means is:
13398
13399    explicit-instantiation:
13400      template decl-specifier-seq [opt] declarator [opt] ;
13401
13402    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13403    supposed to be allowed.  A defect report has been filed about this
13404    issue.
13405
13406    GNU Extension:
13407
13408    explicit-instantiation:
13409      storage-class-specifier template
13410        decl-specifier-seq [opt] declarator [opt] ;
13411      function-specifier template
13412        decl-specifier-seq [opt] declarator [opt] ;  */
13413
13414 static void
13415 cp_parser_explicit_instantiation (cp_parser* parser)
13416 {
13417   int declares_class_or_enum;
13418   cp_decl_specifier_seq decl_specifiers;
13419   tree extension_specifier = NULL_TREE;
13420
13421   timevar_push (TV_TEMPLATE_INST);
13422
13423   /* Look for an (optional) storage-class-specifier or
13424      function-specifier.  */
13425   if (cp_parser_allow_gnu_extensions_p (parser))
13426     {
13427       extension_specifier
13428         = cp_parser_storage_class_specifier_opt (parser);
13429       if (!extension_specifier)
13430         extension_specifier
13431           = cp_parser_function_specifier_opt (parser,
13432                                               /*decl_specs=*/NULL);
13433     }
13434
13435   /* Look for the `template' keyword.  */
13436   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13437   /* Let the front end know that we are processing an explicit
13438      instantiation.  */
13439   begin_explicit_instantiation ();
13440   /* [temp.explicit] says that we are supposed to ignore access
13441      control while processing explicit instantiation directives.  */
13442   push_deferring_access_checks (dk_no_check);
13443   /* Parse a decl-specifier-seq.  */
13444   cp_parser_decl_specifier_seq (parser,
13445                                 CP_PARSER_FLAGS_OPTIONAL,
13446                                 &decl_specifiers,
13447                                 &declares_class_or_enum);
13448   /* If there was exactly one decl-specifier, and it declared a class,
13449      and there's no declarator, then we have an explicit type
13450      instantiation.  */
13451   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13452     {
13453       tree type;
13454
13455       type = check_tag_decl (&decl_specifiers,
13456                              /*explicit_type_instantiation_p=*/true);
13457       /* Turn access control back on for names used during
13458          template instantiation.  */
13459       pop_deferring_access_checks ();
13460       if (type)
13461         do_type_instantiation (type, extension_specifier,
13462                                /*complain=*/tf_error);
13463     }
13464   else
13465     {
13466       cp_declarator *declarator;
13467       tree decl;
13468
13469       /* Parse the declarator.  */
13470       declarator
13471         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13472                                 /*ctor_dtor_or_conv_p=*/NULL,
13473                                 /*parenthesized_p=*/NULL,
13474                                 /*member_p=*/false);
13475       if (declares_class_or_enum & 2)
13476         cp_parser_check_for_definition_in_return_type (declarator,
13477                                                        decl_specifiers.type,
13478                                                        decl_specifiers.locations[ds_type_spec]);
13479       if (declarator != cp_error_declarator)
13480         {
13481           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13482             permerror (decl_specifiers.locations[ds_inline],
13483                        "explicit instantiation shall not use"
13484                        " %<inline%> specifier");
13485           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13486             permerror (decl_specifiers.locations[ds_constexpr],
13487                        "explicit instantiation shall not use"
13488                        " %<constexpr%> specifier");
13489
13490           decl = grokdeclarator (declarator, &decl_specifiers,
13491                                  NORMAL, 0, &decl_specifiers.attributes);
13492           /* Turn access control back on for names used during
13493              template instantiation.  */
13494           pop_deferring_access_checks ();
13495           /* Do the explicit instantiation.  */
13496           do_decl_instantiation (decl, extension_specifier);
13497         }
13498       else
13499         {
13500           pop_deferring_access_checks ();
13501           /* Skip the body of the explicit instantiation.  */
13502           cp_parser_skip_to_end_of_statement (parser);
13503         }
13504     }
13505   /* We're done with the instantiation.  */
13506   end_explicit_instantiation ();
13507
13508   cp_parser_consume_semicolon_at_end_of_statement (parser);
13509
13510   timevar_pop (TV_TEMPLATE_INST);
13511 }
13512
13513 /* Parse an explicit-specialization.
13514
13515    explicit-specialization:
13516      template < > declaration
13517
13518    Although the standard says `declaration', what it really means is:
13519
13520    explicit-specialization:
13521      template <> decl-specifier [opt] init-declarator [opt] ;
13522      template <> function-definition
13523      template <> explicit-specialization
13524      template <> template-declaration  */
13525
13526 static void
13527 cp_parser_explicit_specialization (cp_parser* parser)
13528 {
13529   bool need_lang_pop;
13530   cp_token *token = cp_lexer_peek_token (parser->lexer);
13531
13532   /* Look for the `template' keyword.  */
13533   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13534   /* Look for the `<'.  */
13535   cp_parser_require (parser, CPP_LESS, RT_LESS);
13536   /* Look for the `>'.  */
13537   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13538   /* We have processed another parameter list.  */
13539   ++parser->num_template_parameter_lists;
13540   /* [temp]
13541
13542      A template ... explicit specialization ... shall not have C
13543      linkage.  */
13544   if (current_lang_name == lang_name_c)
13545     {
13546       error_at (token->location, "template specialization with C linkage");
13547       /* Give it C++ linkage to avoid confusing other parts of the
13548          front end.  */
13549       push_lang_context (lang_name_cplusplus);
13550       need_lang_pop = true;
13551     }
13552   else
13553     need_lang_pop = false;
13554   /* Let the front end know that we are beginning a specialization.  */
13555   if (!begin_specialization ())
13556     {
13557       end_specialization ();
13558       return;
13559     }
13560
13561   /* If the next keyword is `template', we need to figure out whether
13562      or not we're looking a template-declaration.  */
13563   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13564     {
13565       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13566           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13567         cp_parser_template_declaration_after_export (parser,
13568                                                      /*member_p=*/false);
13569       else
13570         cp_parser_explicit_specialization (parser);
13571     }
13572   else
13573     /* Parse the dependent declaration.  */
13574     cp_parser_single_declaration (parser,
13575                                   /*checks=*/NULL,
13576                                   /*member_p=*/false,
13577                                   /*explicit_specialization_p=*/true,
13578                                   /*friend_p=*/NULL);
13579   /* We're done with the specialization.  */
13580   end_specialization ();
13581   /* For the erroneous case of a template with C linkage, we pushed an
13582      implicit C++ linkage scope; exit that scope now.  */
13583   if (need_lang_pop)
13584     pop_lang_context ();
13585   /* We're done with this parameter list.  */
13586   --parser->num_template_parameter_lists;
13587 }
13588
13589 /* Parse a type-specifier.
13590
13591    type-specifier:
13592      simple-type-specifier
13593      class-specifier
13594      enum-specifier
13595      elaborated-type-specifier
13596      cv-qualifier
13597
13598    GNU Extension:
13599
13600    type-specifier:
13601      __complex__
13602
13603    Returns a representation of the type-specifier.  For a
13604    class-specifier, enum-specifier, or elaborated-type-specifier, a
13605    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13606
13607    The parser flags FLAGS is used to control type-specifier parsing.
13608
13609    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13610    in a decl-specifier-seq.
13611
13612    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13613    class-specifier, enum-specifier, or elaborated-type-specifier, then
13614    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13615    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13616    zero.
13617
13618    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13619    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13620    is set to FALSE.  */
13621
13622 static tree
13623 cp_parser_type_specifier (cp_parser* parser,
13624                           cp_parser_flags flags,
13625                           cp_decl_specifier_seq *decl_specs,
13626                           bool is_declaration,
13627                           int* declares_class_or_enum,
13628                           bool* is_cv_qualifier)
13629 {
13630   tree type_spec = NULL_TREE;
13631   cp_token *token;
13632   enum rid keyword;
13633   cp_decl_spec ds = ds_last;
13634
13635   /* Assume this type-specifier does not declare a new type.  */
13636   if (declares_class_or_enum)
13637     *declares_class_or_enum = 0;
13638   /* And that it does not specify a cv-qualifier.  */
13639   if (is_cv_qualifier)
13640     *is_cv_qualifier = false;
13641   /* Peek at the next token.  */
13642   token = cp_lexer_peek_token (parser->lexer);
13643
13644   /* If we're looking at a keyword, we can use that to guide the
13645      production we choose.  */
13646   keyword = token->keyword;
13647   switch (keyword)
13648     {
13649     case RID_ENUM:
13650       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13651         goto elaborated_type_specifier;
13652
13653       /* Look for the enum-specifier.  */
13654       type_spec = cp_parser_enum_specifier (parser);
13655       /* If that worked, we're done.  */
13656       if (type_spec)
13657         {
13658           if (declares_class_or_enum)
13659             *declares_class_or_enum = 2;
13660           if (decl_specs)
13661             cp_parser_set_decl_spec_type (decl_specs,
13662                                           type_spec,
13663                                           token,
13664                                           /*type_definition_p=*/true);
13665           return type_spec;
13666         }
13667       else
13668         goto elaborated_type_specifier;
13669
13670       /* Any of these indicate either a class-specifier, or an
13671          elaborated-type-specifier.  */
13672     case RID_CLASS:
13673     case RID_STRUCT:
13674     case RID_UNION:
13675       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13676         goto elaborated_type_specifier;
13677
13678       /* Parse tentatively so that we can back up if we don't find a
13679          class-specifier.  */
13680       cp_parser_parse_tentatively (parser);
13681       /* Look for the class-specifier.  */
13682       type_spec = cp_parser_class_specifier (parser);
13683       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13684       /* If that worked, we're done.  */
13685       if (cp_parser_parse_definitely (parser))
13686         {
13687           if (declares_class_or_enum)
13688             *declares_class_or_enum = 2;
13689           if (decl_specs)
13690             cp_parser_set_decl_spec_type (decl_specs,
13691                                           type_spec,
13692                                           token,
13693                                           /*type_definition_p=*/true);
13694           return type_spec;
13695         }
13696
13697       /* Fall through.  */
13698     elaborated_type_specifier:
13699       /* We're declaring (not defining) a class or enum.  */
13700       if (declares_class_or_enum)
13701         *declares_class_or_enum = 1;
13702
13703       /* Fall through.  */
13704     case RID_TYPENAME:
13705       /* Look for an elaborated-type-specifier.  */
13706       type_spec
13707         = (cp_parser_elaborated_type_specifier
13708            (parser,
13709             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13710             is_declaration));
13711       if (decl_specs)
13712         cp_parser_set_decl_spec_type (decl_specs,
13713                                       type_spec,
13714                                       token,
13715                                       /*type_definition_p=*/false);
13716       return type_spec;
13717
13718     case RID_CONST:
13719       ds = ds_const;
13720       if (is_cv_qualifier)
13721         *is_cv_qualifier = true;
13722       break;
13723
13724     case RID_VOLATILE:
13725       ds = ds_volatile;
13726       if (is_cv_qualifier)
13727         *is_cv_qualifier = true;
13728       break;
13729
13730     case RID_RESTRICT:
13731       ds = ds_restrict;
13732       if (is_cv_qualifier)
13733         *is_cv_qualifier = true;
13734       break;
13735
13736     case RID_COMPLEX:
13737       /* The `__complex__' keyword is a GNU extension.  */
13738       ds = ds_complex;
13739       break;
13740
13741     default:
13742       break;
13743     }
13744
13745   /* Handle simple keywords.  */
13746   if (ds != ds_last)
13747     {
13748       if (decl_specs)
13749         {
13750           set_and_check_decl_spec_loc (decl_specs, ds, token);
13751           decl_specs->any_specifiers_p = true;
13752         }
13753       return cp_lexer_consume_token (parser->lexer)->u.value;
13754     }
13755
13756   /* If we do not already have a type-specifier, assume we are looking
13757      at a simple-type-specifier.  */
13758   type_spec = cp_parser_simple_type_specifier (parser,
13759                                                decl_specs,
13760                                                flags);
13761
13762   /* If we didn't find a type-specifier, and a type-specifier was not
13763      optional in this context, issue an error message.  */
13764   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13765     {
13766       cp_parser_error (parser, "expected type specifier");
13767       return error_mark_node;
13768     }
13769
13770   return type_spec;
13771 }
13772
13773 /* Parse a simple-type-specifier.
13774
13775    simple-type-specifier:
13776      :: [opt] nested-name-specifier [opt] type-name
13777      :: [opt] nested-name-specifier template template-id
13778      char
13779      wchar_t
13780      bool
13781      short
13782      int
13783      long
13784      signed
13785      unsigned
13786      float
13787      double
13788      void
13789
13790    C++0x Extension:
13791
13792    simple-type-specifier:
13793      auto
13794      decltype ( expression )   
13795      char16_t
13796      char32_t
13797      __underlying_type ( type-id )
13798
13799    GNU Extension:
13800
13801    simple-type-specifier:
13802      __int128
13803      __typeof__ unary-expression
13804      __typeof__ ( type-id )
13805
13806    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13807    appropriately updated.  */
13808
13809 static tree
13810 cp_parser_simple_type_specifier (cp_parser* parser,
13811                                  cp_decl_specifier_seq *decl_specs,
13812                                  cp_parser_flags flags)
13813 {
13814   tree type = NULL_TREE;
13815   cp_token *token;
13816
13817   /* Peek at the next token.  */
13818   token = cp_lexer_peek_token (parser->lexer);
13819
13820   /* If we're looking at a keyword, things are easy.  */
13821   switch (token->keyword)
13822     {
13823     case RID_CHAR:
13824       if (decl_specs)
13825         decl_specs->explicit_char_p = true;
13826       type = char_type_node;
13827       break;
13828     case RID_CHAR16:
13829       type = char16_type_node;
13830       break;
13831     case RID_CHAR32:
13832       type = char32_type_node;
13833       break;
13834     case RID_WCHAR:
13835       type = wchar_type_node;
13836       break;
13837     case RID_BOOL:
13838       type = boolean_type_node;
13839       break;
13840     case RID_SHORT:
13841       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13842       type = short_integer_type_node;
13843       break;
13844     case RID_INT:
13845       if (decl_specs)
13846         decl_specs->explicit_int_p = true;
13847       type = integer_type_node;
13848       break;
13849     case RID_INT128:
13850       if (!int128_integer_type_node)
13851         break;
13852       if (decl_specs)
13853         decl_specs->explicit_int128_p = true;
13854       type = int128_integer_type_node;
13855       break;
13856     case RID_LONG:
13857       if (decl_specs)
13858         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13859       type = long_integer_type_node;
13860       break;
13861     case RID_SIGNED:
13862       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13863       type = integer_type_node;
13864       break;
13865     case RID_UNSIGNED:
13866       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13867       type = unsigned_type_node;
13868       break;
13869     case RID_FLOAT:
13870       type = float_type_node;
13871       break;
13872     case RID_DOUBLE:
13873       type = double_type_node;
13874       break;
13875     case RID_VOID:
13876       type = void_type_node;
13877       break;
13878       
13879     case RID_AUTO:
13880       maybe_warn_cpp0x (CPP0X_AUTO);
13881       type = make_auto ();
13882       break;
13883
13884     case RID_DECLTYPE:
13885       /* Since DR 743, decltype can either be a simple-type-specifier by
13886          itself or begin a nested-name-specifier.  Parsing it will replace
13887          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13888          handling below decide what to do.  */
13889       cp_parser_decltype (parser);
13890       cp_lexer_set_token_position (parser->lexer, token);
13891       break;
13892
13893     case RID_TYPEOF:
13894       /* Consume the `typeof' token.  */
13895       cp_lexer_consume_token (parser->lexer);
13896       /* Parse the operand to `typeof'.  */
13897       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13898       /* If it is not already a TYPE, take its type.  */
13899       if (!TYPE_P (type))
13900         type = finish_typeof (type);
13901
13902       if (decl_specs)
13903         cp_parser_set_decl_spec_type (decl_specs, type,
13904                                       token,
13905                                       /*type_definition_p=*/false);
13906
13907       return type;
13908
13909     case RID_UNDERLYING_TYPE:
13910       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13911       if (decl_specs)
13912         cp_parser_set_decl_spec_type (decl_specs, type,
13913                                       token,
13914                                       /*type_definition_p=*/false);
13915
13916       return type;
13917
13918     case RID_BASES:
13919     case RID_DIRECT_BASES:
13920       type = cp_parser_trait_expr (parser, token->keyword);
13921       if (decl_specs)
13922        cp_parser_set_decl_spec_type (decl_specs, type,
13923                                      token,
13924                                      /*type_definition_p=*/false);
13925       return type;
13926     default:
13927       break;
13928     }
13929
13930   /* If token is an already-parsed decltype not followed by ::,
13931      it's a simple-type-specifier.  */
13932   if (token->type == CPP_DECLTYPE
13933       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13934     {
13935       type = token->u.value;
13936       if (decl_specs)
13937         cp_parser_set_decl_spec_type (decl_specs, type,
13938                                       token,
13939                                       /*type_definition_p=*/false);
13940       cp_lexer_consume_token (parser->lexer);
13941       return type;
13942     }
13943
13944   /* If the type-specifier was for a built-in type, we're done.  */
13945   if (type)
13946     {
13947       /* Record the type.  */
13948       if (decl_specs
13949           && (token->keyword != RID_SIGNED
13950               && token->keyword != RID_UNSIGNED
13951               && token->keyword != RID_SHORT
13952               && token->keyword != RID_LONG))
13953         cp_parser_set_decl_spec_type (decl_specs,
13954                                       type,
13955                                       token,
13956                                       /*type_definition_p=*/false);
13957       if (decl_specs)
13958         decl_specs->any_specifiers_p = true;
13959
13960       /* Consume the token.  */
13961       cp_lexer_consume_token (parser->lexer);
13962
13963       /* There is no valid C++ program where a non-template type is
13964          followed by a "<".  That usually indicates that the user thought
13965          that the type was a template.  */
13966       cp_parser_check_for_invalid_template_id (parser, type, none_type,
13967                                                token->location);
13968
13969       return TYPE_NAME (type);
13970     }
13971
13972   /* The type-specifier must be a user-defined type.  */
13973   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13974     {
13975       bool qualified_p;
13976       bool global_p;
13977
13978       /* Don't gobble tokens or issue error messages if this is an
13979          optional type-specifier.  */
13980       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13981         cp_parser_parse_tentatively (parser);
13982
13983       /* Look for the optional `::' operator.  */
13984       global_p
13985         = (cp_parser_global_scope_opt (parser,
13986                                        /*current_scope_valid_p=*/false)
13987            != NULL_TREE);
13988       /* Look for the nested-name specifier.  */
13989       qualified_p
13990         = (cp_parser_nested_name_specifier_opt (parser,
13991                                                 /*typename_keyword_p=*/false,
13992                                                 /*check_dependency_p=*/true,
13993                                                 /*type_p=*/false,
13994                                                 /*is_declaration=*/false)
13995            != NULL_TREE);
13996       token = cp_lexer_peek_token (parser->lexer);
13997       /* If we have seen a nested-name-specifier, and the next token
13998          is `template', then we are using the template-id production.  */
13999       if (parser->scope
14000           && cp_parser_optional_template_keyword (parser))
14001         {
14002           /* Look for the template-id.  */
14003           type = cp_parser_template_id (parser,
14004                                         /*template_keyword_p=*/true,
14005                                         /*check_dependency_p=*/true,
14006                                         none_type,
14007                                         /*is_declaration=*/false);
14008           /* If the template-id did not name a type, we are out of
14009              luck.  */
14010           if (TREE_CODE (type) != TYPE_DECL)
14011             {
14012               cp_parser_error (parser, "expected template-id for type");
14013               type = NULL_TREE;
14014             }
14015         }
14016       /* Otherwise, look for a type-name.  */
14017       else
14018         type = cp_parser_type_name (parser);
14019       /* Keep track of all name-lookups performed in class scopes.  */
14020       if (type
14021           && !global_p
14022           && !qualified_p
14023           && TREE_CODE (type) == TYPE_DECL
14024           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
14025         maybe_note_name_used_in_class (DECL_NAME (type), type);
14026       /* If it didn't work out, we don't have a TYPE.  */
14027       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14028           && !cp_parser_parse_definitely (parser))
14029         type = NULL_TREE;
14030       if (type && decl_specs)
14031         cp_parser_set_decl_spec_type (decl_specs, type,
14032                                       token,
14033                                       /*type_definition_p=*/false);
14034     }
14035
14036   /* If we didn't get a type-name, issue an error message.  */
14037   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14038     {
14039       cp_parser_error (parser, "expected type-name");
14040       return error_mark_node;
14041     }
14042
14043   if (type && type != error_mark_node)
14044     {
14045       /* See if TYPE is an Objective-C type, and if so, parse and
14046          accept any protocol references following it.  Do this before
14047          the cp_parser_check_for_invalid_template_id() call, because
14048          Objective-C types can be followed by '<...>' which would
14049          enclose protocol names rather than template arguments, and so
14050          everything is fine.  */
14051       if (c_dialect_objc () && !parser->scope
14052           && (objc_is_id (type) || objc_is_class_name (type)))
14053         {
14054           tree protos = cp_parser_objc_protocol_refs_opt (parser);
14055           tree qual_type = objc_get_protocol_qualified_type (type, protos);
14056
14057           /* Clobber the "unqualified" type previously entered into
14058              DECL_SPECS with the new, improved protocol-qualified version.  */
14059           if (decl_specs)
14060             decl_specs->type = qual_type;
14061
14062           return qual_type;
14063         }
14064
14065       /* There is no valid C++ program where a non-template type is
14066          followed by a "<".  That usually indicates that the user
14067          thought that the type was a template.  */
14068       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14069                                                none_type,
14070                                                token->location);
14071     }
14072
14073   return type;
14074 }
14075
14076 /* Parse a type-name.
14077
14078    type-name:
14079      class-name
14080      enum-name
14081      typedef-name
14082      simple-template-id [in c++0x]
14083
14084    enum-name:
14085      identifier
14086
14087    typedef-name:
14088      identifier
14089
14090    Returns a TYPE_DECL for the type.  */
14091
14092 static tree
14093 cp_parser_type_name (cp_parser* parser)
14094 {
14095   tree type_decl;
14096
14097   /* We can't know yet whether it is a class-name or not.  */
14098   cp_parser_parse_tentatively (parser);
14099   /* Try a class-name.  */
14100   type_decl = cp_parser_class_name (parser,
14101                                     /*typename_keyword_p=*/false,
14102                                     /*template_keyword_p=*/false,
14103                                     none_type,
14104                                     /*check_dependency_p=*/true,
14105                                     /*class_head_p=*/false,
14106                                     /*is_declaration=*/false);
14107   /* If it's not a class-name, keep looking.  */
14108   if (!cp_parser_parse_definitely (parser))
14109     {
14110       if (cxx_dialect < cxx0x)
14111         /* It must be a typedef-name or an enum-name.  */
14112         return cp_parser_nonclass_name (parser);
14113
14114       cp_parser_parse_tentatively (parser);
14115       /* It is either a simple-template-id representing an
14116          instantiation of an alias template...  */
14117       type_decl = cp_parser_template_id (parser,
14118                                          /*template_keyword_p=*/false,
14119                                          /*check_dependency_p=*/false,
14120                                          none_type,
14121                                          /*is_declaration=*/false);
14122       /* Note that this must be an instantiation of an alias template
14123          because [temp.names]/6 says:
14124          
14125              A template-id that names an alias template specialization
14126              is a type-name.
14127
14128          Whereas [temp.names]/7 says:
14129          
14130              A simple-template-id that names a class template
14131              specialization is a class-name.  */
14132       if (type_decl != NULL_TREE
14133           && TREE_CODE (type_decl) == TYPE_DECL
14134           && TYPE_DECL_ALIAS_P (type_decl))
14135         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14136       else
14137         cp_parser_simulate_error (parser);
14138
14139       if (!cp_parser_parse_definitely (parser))
14140         /* ... Or a typedef-name or an enum-name.  */
14141         return cp_parser_nonclass_name (parser);
14142     }
14143
14144   return type_decl;
14145 }
14146
14147 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14148
14149    enum-name:
14150      identifier
14151
14152    typedef-name:
14153      identifier
14154
14155    Returns a TYPE_DECL for the type.  */
14156
14157 static tree
14158 cp_parser_nonclass_name (cp_parser* parser)
14159 {
14160   tree type_decl;
14161   tree identifier;
14162
14163   cp_token *token = cp_lexer_peek_token (parser->lexer);
14164   identifier = cp_parser_identifier (parser);
14165   if (identifier == error_mark_node)
14166     return error_mark_node;
14167
14168   /* Look up the type-name.  */
14169   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14170
14171   if (TREE_CODE (type_decl) == USING_DECL)
14172     {
14173       if (!DECL_DEPENDENT_P (type_decl))
14174         type_decl = strip_using_decl (type_decl);
14175       else if (USING_DECL_TYPENAME_P (type_decl))
14176         {
14177           /* We have found a type introduced by a using
14178              declaration at class scope that refers to a dependent
14179              type.
14180              
14181              using typename :: [opt] nested-name-specifier unqualified-id ;
14182           */
14183           type_decl = make_typename_type (TREE_TYPE (type_decl),
14184                                           DECL_NAME (type_decl),
14185                                           typename_type, tf_error);
14186           if (type_decl != error_mark_node)
14187             type_decl = TYPE_NAME (type_decl);
14188         }
14189     }
14190   
14191   if (TREE_CODE (type_decl) != TYPE_DECL
14192       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14193     {
14194       /* See if this is an Objective-C type.  */
14195       tree protos = cp_parser_objc_protocol_refs_opt (parser);
14196       tree type = objc_get_protocol_qualified_type (identifier, protos);
14197       if (type)
14198         type_decl = TYPE_NAME (type);
14199     }
14200
14201   /* Issue an error if we did not find a type-name.  */
14202   if (TREE_CODE (type_decl) != TYPE_DECL
14203       /* In Objective-C, we have the complication that class names are
14204          normally type names and start declarations (eg, the
14205          "NSObject" in "NSObject *object;"), but can be used in an
14206          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14207          is an expression.  So, a classname followed by a dot is not a
14208          valid type-name.  */
14209       || (objc_is_class_name (TREE_TYPE (type_decl))
14210           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14211     {
14212       if (!cp_parser_simulate_error (parser))
14213         cp_parser_name_lookup_error (parser, identifier, type_decl,
14214                                      NLE_TYPE, token->location);
14215       return error_mark_node;
14216     }
14217   /* Remember that the name was used in the definition of the
14218      current class so that we can check later to see if the
14219      meaning would have been different after the class was
14220      entirely defined.  */
14221   else if (type_decl != error_mark_node
14222            && !parser->scope)
14223     maybe_note_name_used_in_class (identifier, type_decl);
14224   
14225   return type_decl;
14226 }
14227
14228 /* Parse an elaborated-type-specifier.  Note that the grammar given
14229    here incorporates the resolution to DR68.
14230
14231    elaborated-type-specifier:
14232      class-key :: [opt] nested-name-specifier [opt] identifier
14233      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14234      enum-key :: [opt] nested-name-specifier [opt] identifier
14235      typename :: [opt] nested-name-specifier identifier
14236      typename :: [opt] nested-name-specifier template [opt]
14237        template-id
14238
14239    GNU extension:
14240
14241    elaborated-type-specifier:
14242      class-key attributes :: [opt] nested-name-specifier [opt] identifier
14243      class-key attributes :: [opt] nested-name-specifier [opt]
14244                template [opt] template-id
14245      enum attributes :: [opt] nested-name-specifier [opt] identifier
14246
14247    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14248    declared `friend'.  If IS_DECLARATION is TRUE, then this
14249    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14250    something is being declared.
14251
14252    Returns the TYPE specified.  */
14253
14254 static tree
14255 cp_parser_elaborated_type_specifier (cp_parser* parser,
14256                                      bool is_friend,
14257                                      bool is_declaration)
14258 {
14259   enum tag_types tag_type;
14260   tree identifier;
14261   tree type = NULL_TREE;
14262   tree attributes = NULL_TREE;
14263   tree globalscope;
14264   cp_token *token = NULL;
14265
14266   /* See if we're looking at the `enum' keyword.  */
14267   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14268     {
14269       /* Consume the `enum' token.  */
14270       cp_lexer_consume_token (parser->lexer);
14271       /* Remember that it's an enumeration type.  */
14272       tag_type = enum_type;
14273       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14274          enums) is used here.  */
14275       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14276           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14277         {
14278             pedwarn (input_location, 0, "elaborated-type-specifier "
14279                       "for a scoped enum must not use the %<%D%> keyword",
14280                       cp_lexer_peek_token (parser->lexer)->u.value);
14281           /* Consume the `struct' or `class' and parse it anyway.  */
14282           cp_lexer_consume_token (parser->lexer);
14283         }
14284       /* Parse the attributes.  */
14285       attributes = cp_parser_attributes_opt (parser);
14286     }
14287   /* Or, it might be `typename'.  */
14288   else if (cp_lexer_next_token_is_keyword (parser->lexer,
14289                                            RID_TYPENAME))
14290     {
14291       /* Consume the `typename' token.  */
14292       cp_lexer_consume_token (parser->lexer);
14293       /* Remember that it's a `typename' type.  */
14294       tag_type = typename_type;
14295     }
14296   /* Otherwise it must be a class-key.  */
14297   else
14298     {
14299       tag_type = cp_parser_class_key (parser);
14300       if (tag_type == none_type)
14301         return error_mark_node;
14302       /* Parse the attributes.  */
14303       attributes = cp_parser_attributes_opt (parser);
14304     }
14305
14306   /* Look for the `::' operator.  */
14307   globalscope =  cp_parser_global_scope_opt (parser,
14308                                              /*current_scope_valid_p=*/false);
14309   /* Look for the nested-name-specifier.  */
14310   if (tag_type == typename_type && !globalscope)
14311     {
14312       if (!cp_parser_nested_name_specifier (parser,
14313                                            /*typename_keyword_p=*/true,
14314                                            /*check_dependency_p=*/true,
14315                                            /*type_p=*/true,
14316                                             is_declaration))
14317         return error_mark_node;
14318     }
14319   else
14320     /* Even though `typename' is not present, the proposed resolution
14321        to Core Issue 180 says that in `class A<T>::B', `B' should be
14322        considered a type-name, even if `A<T>' is dependent.  */
14323     cp_parser_nested_name_specifier_opt (parser,
14324                                          /*typename_keyword_p=*/true,
14325                                          /*check_dependency_p=*/true,
14326                                          /*type_p=*/true,
14327                                          is_declaration);
14328  /* For everything but enumeration types, consider a template-id.
14329     For an enumeration type, consider only a plain identifier.  */
14330   if (tag_type != enum_type)
14331     {
14332       bool template_p = false;
14333       tree decl;
14334
14335       /* Allow the `template' keyword.  */
14336       template_p = cp_parser_optional_template_keyword (parser);
14337       /* If we didn't see `template', we don't know if there's a
14338          template-id or not.  */
14339       if (!template_p)
14340         cp_parser_parse_tentatively (parser);
14341       /* Parse the template-id.  */
14342       token = cp_lexer_peek_token (parser->lexer);
14343       decl = cp_parser_template_id (parser, template_p,
14344                                     /*check_dependency_p=*/true,
14345                                     tag_type,
14346                                     is_declaration);
14347       /* If we didn't find a template-id, look for an ordinary
14348          identifier.  */
14349       if (!template_p && !cp_parser_parse_definitely (parser))
14350         ;
14351       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14352          in effect, then we must assume that, upon instantiation, the
14353          template will correspond to a class.  */
14354       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14355                && tag_type == typename_type)
14356         type = make_typename_type (parser->scope, decl,
14357                                    typename_type,
14358                                    /*complain=*/tf_error);
14359       /* If the `typename' keyword is in effect and DECL is not a type
14360          decl, then type is non existent.   */
14361       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14362         ; 
14363       else if (TREE_CODE (decl) == TYPE_DECL)
14364         type = check_elaborated_type_specifier (tag_type, decl,
14365                                                 /*allow_template_p=*/true);
14366       else if (decl == error_mark_node)
14367         type = error_mark_node; 
14368     }
14369
14370   if (!type)
14371     {
14372       token = cp_lexer_peek_token (parser->lexer);
14373       identifier = cp_parser_identifier (parser);
14374
14375       if (identifier == error_mark_node)
14376         {
14377           parser->scope = NULL_TREE;
14378           return error_mark_node;
14379         }
14380
14381       /* For a `typename', we needn't call xref_tag.  */
14382       if (tag_type == typename_type
14383           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14384         return cp_parser_make_typename_type (parser, parser->scope,
14385                                              identifier,
14386                                              token->location);
14387       /* Look up a qualified name in the usual way.  */
14388       if (parser->scope)
14389         {
14390           tree decl;
14391           tree ambiguous_decls;
14392
14393           decl = cp_parser_lookup_name (parser, identifier,
14394                                         tag_type,
14395                                         /*is_template=*/false,
14396                                         /*is_namespace=*/false,
14397                                         /*check_dependency=*/true,
14398                                         &ambiguous_decls,
14399                                         token->location);
14400
14401           /* If the lookup was ambiguous, an error will already have been
14402              issued.  */
14403           if (ambiguous_decls)
14404             return error_mark_node;
14405
14406           /* If we are parsing friend declaration, DECL may be a
14407              TEMPLATE_DECL tree node here.  However, we need to check
14408              whether this TEMPLATE_DECL results in valid code.  Consider
14409              the following example:
14410
14411                namespace N {
14412                  template <class T> class C {};
14413                }
14414                class X {
14415                  template <class T> friend class N::C; // #1, valid code
14416                };
14417                template <class T> class Y {
14418                  friend class N::C;                    // #2, invalid code
14419                };
14420
14421              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14422              name lookup of `N::C'.  We see that friend declaration must
14423              be template for the code to be valid.  Note that
14424              processing_template_decl does not work here since it is
14425              always 1 for the above two cases.  */
14426
14427           decl = (cp_parser_maybe_treat_template_as_class
14428                   (decl, /*tag_name_p=*/is_friend
14429                          && parser->num_template_parameter_lists));
14430
14431           if (TREE_CODE (decl) != TYPE_DECL)
14432             {
14433               cp_parser_diagnose_invalid_type_name (parser,
14434                                                     parser->scope,
14435                                                     identifier,
14436                                                     token->location);
14437               return error_mark_node;
14438             }
14439
14440           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14441             {
14442               bool allow_template = (parser->num_template_parameter_lists
14443                                       || DECL_SELF_REFERENCE_P (decl));
14444               type = check_elaborated_type_specifier (tag_type, decl, 
14445                                                       allow_template);
14446
14447               if (type == error_mark_node)
14448                 return error_mark_node;
14449             }
14450
14451           /* Forward declarations of nested types, such as
14452
14453                class C1::C2;
14454                class C1::C2::C3;
14455
14456              are invalid unless all components preceding the final '::'
14457              are complete.  If all enclosing types are complete, these
14458              declarations become merely pointless.
14459
14460              Invalid forward declarations of nested types are errors
14461              caught elsewhere in parsing.  Those that are pointless arrive
14462              here.  */
14463
14464           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14465               && !is_friend && !processing_explicit_instantiation)
14466             warning (0, "declaration %qD does not declare anything", decl);
14467
14468           type = TREE_TYPE (decl);
14469         }
14470       else
14471         {
14472           /* An elaborated-type-specifier sometimes introduces a new type and
14473              sometimes names an existing type.  Normally, the rule is that it
14474              introduces a new type only if there is not an existing type of
14475              the same name already in scope.  For example, given:
14476
14477                struct S {};
14478                void f() { struct S s; }
14479
14480              the `struct S' in the body of `f' is the same `struct S' as in
14481              the global scope; the existing definition is used.  However, if
14482              there were no global declaration, this would introduce a new
14483              local class named `S'.
14484
14485              An exception to this rule applies to the following code:
14486
14487                namespace N { struct S; }
14488
14489              Here, the elaborated-type-specifier names a new type
14490              unconditionally; even if there is already an `S' in the
14491              containing scope this declaration names a new type.
14492              This exception only applies if the elaborated-type-specifier
14493              forms the complete declaration:
14494
14495                [class.name]
14496
14497                A declaration consisting solely of `class-key identifier ;' is
14498                either a redeclaration of the name in the current scope or a
14499                forward declaration of the identifier as a class name.  It
14500                introduces the name into the current scope.
14501
14502              We are in this situation precisely when the next token is a `;'.
14503
14504              An exception to the exception is that a `friend' declaration does
14505              *not* name a new type; i.e., given:
14506
14507                struct S { friend struct T; };
14508
14509              `T' is not a new type in the scope of `S'.
14510
14511              Also, `new struct S' or `sizeof (struct S)' never results in the
14512              definition of a new type; a new type can only be declared in a
14513              declaration context.  */
14514
14515           tag_scope ts;
14516           bool template_p;
14517
14518           if (is_friend)
14519             /* Friends have special name lookup rules.  */
14520             ts = ts_within_enclosing_non_class;
14521           else if (is_declaration
14522                    && cp_lexer_next_token_is (parser->lexer,
14523                                               CPP_SEMICOLON))
14524             /* This is a `class-key identifier ;' */
14525             ts = ts_current;
14526           else
14527             ts = ts_global;
14528
14529           template_p =
14530             (parser->num_template_parameter_lists
14531              && (cp_parser_next_token_starts_class_definition_p (parser)
14532                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14533           /* An unqualified name was used to reference this type, so
14534              there were no qualifying templates.  */
14535           if (!cp_parser_check_template_parameters (parser,
14536                                                     /*num_templates=*/0,
14537                                                     token->location,
14538                                                     /*declarator=*/NULL))
14539             return error_mark_node;
14540           type = xref_tag (tag_type, identifier, ts, template_p);
14541         }
14542     }
14543
14544   if (type == error_mark_node)
14545     return error_mark_node;
14546
14547   /* Allow attributes on forward declarations of classes.  */
14548   if (attributes)
14549     {
14550       if (TREE_CODE (type) == TYPENAME_TYPE)
14551         warning (OPT_Wattributes,
14552                  "attributes ignored on uninstantiated type");
14553       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14554                && ! processing_explicit_instantiation)
14555         warning (OPT_Wattributes,
14556                  "attributes ignored on template instantiation");
14557       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14558         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14559       else
14560         warning (OPT_Wattributes,
14561                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14562     }
14563
14564   if (tag_type != enum_type)
14565     {
14566       /* Indicate whether this class was declared as a `class' or as a
14567          `struct'.  */
14568       if (TREE_CODE (type) == RECORD_TYPE)
14569         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14570       cp_parser_check_class_key (tag_type, type);
14571     }
14572
14573   /* A "<" cannot follow an elaborated type specifier.  If that
14574      happens, the user was probably trying to form a template-id.  */
14575   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14576                                            token->location);
14577
14578   return type;
14579 }
14580
14581 /* Parse an enum-specifier.
14582
14583    enum-specifier:
14584      enum-head { enumerator-list [opt] }
14585      enum-head { enumerator-list , } [C++0x]
14586
14587    enum-head:
14588      enum-key identifier [opt] enum-base [opt]
14589      enum-key nested-name-specifier identifier enum-base [opt]
14590
14591    enum-key:
14592      enum
14593      enum class   [C++0x]
14594      enum struct  [C++0x]
14595
14596    enum-base:   [C++0x]
14597      : type-specifier-seq
14598
14599    opaque-enum-specifier:
14600      enum-key identifier enum-base [opt] ;
14601
14602    GNU Extensions:
14603      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14604        { enumerator-list [opt] }attributes[opt]
14605      enum-key attributes[opt] identifier [opt] enum-base [opt]
14606        { enumerator-list, }attributes[opt] [C++0x]
14607
14608    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14609    if the token stream isn't an enum-specifier after all.  */
14610
14611 static tree
14612 cp_parser_enum_specifier (cp_parser* parser)
14613 {
14614   tree identifier;
14615   tree type = NULL_TREE;
14616   tree prev_scope;
14617   tree nested_name_specifier = NULL_TREE;
14618   tree attributes;
14619   bool scoped_enum_p = false;
14620   bool has_underlying_type = false;
14621   bool nested_being_defined = false;
14622   bool new_value_list = false;
14623   bool is_new_type = false;
14624   bool is_anonymous = false;
14625   tree underlying_type = NULL_TREE;
14626   cp_token *type_start_token = NULL;
14627   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14628
14629   parser->colon_corrects_to_scope_p = false;
14630
14631   /* Parse tentatively so that we can back up if we don't find a
14632      enum-specifier.  */
14633   cp_parser_parse_tentatively (parser);
14634
14635   /* Caller guarantees that the current token is 'enum', an identifier
14636      possibly follows, and the token after that is an opening brace.
14637      If we don't have an identifier, fabricate an anonymous name for
14638      the enumeration being defined.  */
14639   cp_lexer_consume_token (parser->lexer);
14640
14641   /* Parse the "class" or "struct", which indicates a scoped
14642      enumeration type in C++0x.  */
14643   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14644       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14645     {
14646       if (cxx_dialect < cxx0x)
14647         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14648
14649       /* Consume the `struct' or `class' token.  */
14650       cp_lexer_consume_token (parser->lexer);
14651
14652       scoped_enum_p = true;
14653     }
14654
14655   attributes = cp_parser_attributes_opt (parser);
14656
14657   /* Clear the qualification.  */
14658   parser->scope = NULL_TREE;
14659   parser->qualifying_scope = NULL_TREE;
14660   parser->object_scope = NULL_TREE;
14661
14662   /* Figure out in what scope the declaration is being placed.  */
14663   prev_scope = current_scope ();
14664
14665   type_start_token = cp_lexer_peek_token (parser->lexer);
14666
14667   push_deferring_access_checks (dk_no_check);
14668   nested_name_specifier
14669       = cp_parser_nested_name_specifier_opt (parser,
14670                                              /*typename_keyword_p=*/true,
14671                                              /*check_dependency_p=*/false,
14672                                              /*type_p=*/false,
14673                                              /*is_declaration=*/false);
14674
14675   if (nested_name_specifier)
14676     {
14677       tree name;
14678
14679       identifier = cp_parser_identifier (parser);
14680       name =  cp_parser_lookup_name (parser, identifier,
14681                                      enum_type,
14682                                      /*is_template=*/false,
14683                                      /*is_namespace=*/false,
14684                                      /*check_dependency=*/true,
14685                                      /*ambiguous_decls=*/NULL,
14686                                      input_location);
14687       if (name && name != error_mark_node)
14688         {
14689           type = TREE_TYPE (name);
14690           if (TREE_CODE (type) == TYPENAME_TYPE)
14691             {
14692               /* Are template enums allowed in ISO? */
14693               if (template_parm_scope_p ())
14694                 pedwarn (type_start_token->location, OPT_Wpedantic,
14695                          "%qD is an enumeration template", name);
14696               /* ignore a typename reference, for it will be solved by name
14697                  in start_enum.  */
14698               type = NULL_TREE;
14699             }
14700         }
14701       else
14702         error_at (type_start_token->location,
14703                   "%qD is not an enumerator-name", identifier);
14704     }
14705   else
14706     {
14707       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14708         identifier = cp_parser_identifier (parser);
14709       else
14710         {
14711           identifier = make_anon_name ();
14712           is_anonymous = true;
14713         }
14714     }
14715   pop_deferring_access_checks ();
14716
14717   /* Check for the `:' that denotes a specified underlying type in C++0x.
14718      Note that a ':' could also indicate a bitfield width, however.  */
14719   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14720     {
14721       cp_decl_specifier_seq type_specifiers;
14722
14723       /* Consume the `:'.  */
14724       cp_lexer_consume_token (parser->lexer);
14725
14726       /* Parse the type-specifier-seq.  */
14727       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14728                                     /*is_trailing_return=*/false,
14729                                     &type_specifiers);
14730
14731       /* At this point this is surely not elaborated type specifier.  */
14732       if (!cp_parser_parse_definitely (parser))
14733         return NULL_TREE;
14734
14735       if (cxx_dialect < cxx0x)
14736         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14737
14738       has_underlying_type = true;
14739
14740       /* If that didn't work, stop.  */
14741       if (type_specifiers.type != error_mark_node)
14742         {
14743           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14744                                             /*initialized=*/0, NULL);
14745           if (underlying_type == error_mark_node)
14746             underlying_type = NULL_TREE;
14747         }
14748     }
14749
14750   /* Look for the `{' but don't consume it yet.  */
14751   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14752     {
14753       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14754         {
14755           cp_parser_error (parser, "expected %<{%>");
14756           if (has_underlying_type)
14757             {
14758               type = NULL_TREE;
14759               goto out;
14760             }
14761         }
14762       /* An opaque-enum-specifier must have a ';' here.  */
14763       if ((scoped_enum_p || underlying_type)
14764           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14765         {
14766           cp_parser_error (parser, "expected %<;%> or %<{%>");
14767           if (has_underlying_type)
14768             {
14769               type = NULL_TREE;
14770               goto out;
14771             }
14772         }
14773     }
14774
14775   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14776     return NULL_TREE;
14777
14778   if (nested_name_specifier)
14779     {
14780       if (CLASS_TYPE_P (nested_name_specifier))
14781         {
14782           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14783           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14784           push_scope (nested_name_specifier);
14785         }
14786       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14787         {
14788           push_nested_namespace (nested_name_specifier);
14789         }
14790     }
14791
14792   /* Issue an error message if type-definitions are forbidden here.  */
14793   if (!cp_parser_check_type_definition (parser))
14794     type = error_mark_node;
14795   else
14796     /* Create the new type.  We do this before consuming the opening
14797        brace so the enum will be recorded as being on the line of its
14798        tag (or the 'enum' keyword, if there is no tag).  */
14799     type = start_enum (identifier, type, underlying_type,
14800                        scoped_enum_p, &is_new_type);
14801
14802   /* If the next token is not '{' it is an opaque-enum-specifier or an
14803      elaborated-type-specifier.  */
14804   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14805     {
14806       timevar_push (TV_PARSE_ENUM);
14807       if (nested_name_specifier)
14808         {
14809           /* The following catches invalid code such as:
14810              enum class S<int>::E { A, B, C }; */
14811           if (!processing_specialization
14812               && CLASS_TYPE_P (nested_name_specifier)
14813               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14814             error_at (type_start_token->location, "cannot add an enumerator "
14815                       "list to a template instantiation");
14816
14817           /* If that scope does not contain the scope in which the
14818              class was originally declared, the program is invalid.  */
14819           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14820             {
14821               if (at_namespace_scope_p ())
14822                 error_at (type_start_token->location,
14823                           "declaration of %qD in namespace %qD which does not "
14824                           "enclose %qD",
14825                           type, prev_scope, nested_name_specifier);
14826               else
14827                 error_at (type_start_token->location,
14828                           "declaration of %qD in %qD which does not enclose %qD",
14829                           type, prev_scope, nested_name_specifier);
14830               type = error_mark_node;
14831             }
14832         }
14833
14834       if (scoped_enum_p)
14835         begin_scope (sk_scoped_enum, type);
14836
14837       /* Consume the opening brace.  */
14838       cp_lexer_consume_token (parser->lexer);
14839
14840       if (type == error_mark_node)
14841         ; /* Nothing to add */
14842       else if (OPAQUE_ENUM_P (type)
14843                || (cxx_dialect > cxx98 && processing_specialization))
14844         {
14845           new_value_list = true;
14846           SET_OPAQUE_ENUM_P (type, false);
14847           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14848         }
14849       else
14850         {
14851           error_at (type_start_token->location, "multiple definition of %q#T", type);
14852           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14853                     "previous definition here");
14854           type = error_mark_node;
14855         }
14856
14857       if (type == error_mark_node)
14858         cp_parser_skip_to_end_of_block_or_statement (parser);
14859       /* If the next token is not '}', then there are some enumerators.  */
14860       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14861         cp_parser_enumerator_list (parser, type);
14862
14863       /* Consume the final '}'.  */
14864       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14865
14866       if (scoped_enum_p)
14867         finish_scope ();
14868       timevar_pop (TV_PARSE_ENUM);
14869     }
14870   else
14871     {
14872       /* If a ';' follows, then it is an opaque-enum-specifier
14873         and additional restrictions apply.  */
14874       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14875         {
14876           if (is_anonymous)
14877             error_at (type_start_token->location,
14878                       "opaque-enum-specifier without name");
14879           else if (nested_name_specifier)
14880             error_at (type_start_token->location,
14881                       "opaque-enum-specifier must use a simple identifier");
14882         }
14883     }
14884
14885   /* Look for trailing attributes to apply to this enumeration, and
14886      apply them if appropriate.  */
14887   if (cp_parser_allow_gnu_extensions_p (parser))
14888     {
14889       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14890       trailing_attr = chainon (trailing_attr, attributes);
14891       cplus_decl_attributes (&type,
14892                              trailing_attr,
14893                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14894     }
14895
14896   /* Finish up the enumeration.  */
14897   if (type != error_mark_node)
14898     {
14899       if (new_value_list)
14900         finish_enum_value_list (type);
14901       if (is_new_type)
14902         finish_enum (type);
14903     }
14904
14905   if (nested_name_specifier)
14906     {
14907       if (CLASS_TYPE_P (nested_name_specifier))
14908         {
14909           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14910           pop_scope (nested_name_specifier);
14911         }
14912       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14913         {
14914           pop_nested_namespace (nested_name_specifier);
14915         }
14916     }
14917  out:
14918   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14919   return type;
14920 }
14921
14922 /* Parse an enumerator-list.  The enumerators all have the indicated
14923    TYPE.
14924
14925    enumerator-list:
14926      enumerator-definition
14927      enumerator-list , enumerator-definition  */
14928
14929 static void
14930 cp_parser_enumerator_list (cp_parser* parser, tree type)
14931 {
14932   while (true)
14933     {
14934       /* Parse an enumerator-definition.  */
14935       cp_parser_enumerator_definition (parser, type);
14936
14937       /* If the next token is not a ',', we've reached the end of
14938          the list.  */
14939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14940         break;
14941       /* Otherwise, consume the `,' and keep going.  */
14942       cp_lexer_consume_token (parser->lexer);
14943       /* If the next token is a `}', there is a trailing comma.  */
14944       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14945         {
14946           if (cxx_dialect < cxx0x && !in_system_header)
14947             pedwarn (input_location, OPT_Wpedantic,
14948                      "comma at end of enumerator list");
14949           break;
14950         }
14951     }
14952 }
14953
14954 /* Parse an enumerator-definition.  The enumerator has the indicated
14955    TYPE.
14956
14957    enumerator-definition:
14958      enumerator
14959      enumerator = constant-expression
14960
14961    enumerator:
14962      identifier  */
14963
14964 static void
14965 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14966 {
14967   tree identifier;
14968   tree value;
14969   location_t loc;
14970
14971   /* Save the input location because we are interested in the location
14972      of the identifier and not the location of the explicit value.  */
14973   loc = cp_lexer_peek_token (parser->lexer)->location;
14974
14975   /* Look for the identifier.  */
14976   identifier = cp_parser_identifier (parser);
14977   if (identifier == error_mark_node)
14978     return;
14979
14980   /* If the next token is an '=', then there is an explicit value.  */
14981   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14982     {
14983       /* Consume the `=' token.  */
14984       cp_lexer_consume_token (parser->lexer);
14985       /* Parse the value.  */
14986       value = cp_parser_constant_expression (parser,
14987                                              /*allow_non_constant_p=*/false,
14988                                              NULL);
14989     }
14990   else
14991     value = NULL_TREE;
14992
14993   /* If we are processing a template, make sure the initializer of the
14994      enumerator doesn't contain any bare template parameter pack.  */
14995   if (check_for_bare_parameter_packs (value))
14996     value = error_mark_node;
14997
14998   /* integral_constant_value will pull out this expression, so make sure
14999      it's folded as appropriate.  */
15000   value = fold_non_dependent_expr (value);
15001
15002   /* Create the enumerator.  */
15003   build_enumerator (identifier, value, type, loc);
15004 }
15005
15006 /* Parse a namespace-name.
15007
15008    namespace-name:
15009      original-namespace-name
15010      namespace-alias
15011
15012    Returns the NAMESPACE_DECL for the namespace.  */
15013
15014 static tree
15015 cp_parser_namespace_name (cp_parser* parser)
15016 {
15017   tree identifier;
15018   tree namespace_decl;
15019
15020   cp_token *token = cp_lexer_peek_token (parser->lexer);
15021
15022   /* Get the name of the namespace.  */
15023   identifier = cp_parser_identifier (parser);
15024   if (identifier == error_mark_node)
15025     return error_mark_node;
15026
15027   /* Look up the identifier in the currently active scope.  Look only
15028      for namespaces, due to:
15029
15030        [basic.lookup.udir]
15031
15032        When looking up a namespace-name in a using-directive or alias
15033        definition, only namespace names are considered.
15034
15035      And:
15036
15037        [basic.lookup.qual]
15038
15039        During the lookup of a name preceding the :: scope resolution
15040        operator, object, function, and enumerator names are ignored.
15041
15042      (Note that cp_parser_qualifying_entity only calls this
15043      function if the token after the name is the scope resolution
15044      operator.)  */
15045   namespace_decl = cp_parser_lookup_name (parser, identifier,
15046                                           none_type,
15047                                           /*is_template=*/false,
15048                                           /*is_namespace=*/true,
15049                                           /*check_dependency=*/true,
15050                                           /*ambiguous_decls=*/NULL,
15051                                           token->location);
15052   /* If it's not a namespace, issue an error.  */
15053   if (namespace_decl == error_mark_node
15054       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15055     {
15056       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15057         error_at (token->location, "%qD is not a namespace-name", identifier);
15058       cp_parser_error (parser, "expected namespace-name");
15059       namespace_decl = error_mark_node;
15060     }
15061
15062   return namespace_decl;
15063 }
15064
15065 /* Parse a namespace-definition.
15066
15067    namespace-definition:
15068      named-namespace-definition
15069      unnamed-namespace-definition
15070
15071    named-namespace-definition:
15072      original-namespace-definition
15073      extension-namespace-definition
15074
15075    original-namespace-definition:
15076      namespace identifier { namespace-body }
15077
15078    extension-namespace-definition:
15079      namespace original-namespace-name { namespace-body }
15080
15081    unnamed-namespace-definition:
15082      namespace { namespace-body } */
15083
15084 static void
15085 cp_parser_namespace_definition (cp_parser* parser)
15086 {
15087   tree identifier, attribs;
15088   bool has_visibility;
15089   bool is_inline;
15090
15091   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15092     {
15093       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15094       is_inline = true;
15095       cp_lexer_consume_token (parser->lexer);
15096     }
15097   else
15098     is_inline = false;
15099
15100   /* Look for the `namespace' keyword.  */
15101   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15102
15103   /* Get the name of the namespace.  We do not attempt to distinguish
15104      between an original-namespace-definition and an
15105      extension-namespace-definition at this point.  The semantic
15106      analysis routines are responsible for that.  */
15107   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15108     identifier = cp_parser_identifier (parser);
15109   else
15110     identifier = NULL_TREE;
15111
15112   /* Parse any specified attributes.  */
15113   attribs = cp_parser_attributes_opt (parser);
15114
15115   /* Look for the `{' to start the namespace.  */
15116   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15117   /* Start the namespace.  */
15118   push_namespace (identifier);
15119
15120   /* "inline namespace" is equivalent to a stub namespace definition
15121      followed by a strong using directive.  */
15122   if (is_inline)
15123     {
15124       tree name_space = current_namespace;
15125       /* Set up namespace association.  */
15126       DECL_NAMESPACE_ASSOCIATIONS (name_space)
15127         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15128                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
15129       /* Import the contents of the inline namespace.  */
15130       pop_namespace ();
15131       do_using_directive (name_space);
15132       push_namespace (identifier);
15133     }
15134
15135   has_visibility = handle_namespace_attrs (current_namespace, attribs);
15136
15137   /* Parse the body of the namespace.  */
15138   cp_parser_namespace_body (parser);
15139
15140   if (has_visibility)
15141     pop_visibility (1);
15142
15143   /* Finish the namespace.  */
15144   pop_namespace ();
15145   /* Look for the final `}'.  */
15146   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15147 }
15148
15149 /* Parse a namespace-body.
15150
15151    namespace-body:
15152      declaration-seq [opt]  */
15153
15154 static void
15155 cp_parser_namespace_body (cp_parser* parser)
15156 {
15157   cp_parser_declaration_seq_opt (parser);
15158 }
15159
15160 /* Parse a namespace-alias-definition.
15161
15162    namespace-alias-definition:
15163      namespace identifier = qualified-namespace-specifier ;  */
15164
15165 static void
15166 cp_parser_namespace_alias_definition (cp_parser* parser)
15167 {
15168   tree identifier;
15169   tree namespace_specifier;
15170
15171   cp_token *token = cp_lexer_peek_token (parser->lexer);
15172
15173   /* Look for the `namespace' keyword.  */
15174   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15175   /* Look for the identifier.  */
15176   identifier = cp_parser_identifier (parser);
15177   if (identifier == error_mark_node)
15178     return;
15179   /* Look for the `=' token.  */
15180   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15181       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
15182     {
15183       error_at (token->location, "%<namespace%> definition is not allowed here");
15184       /* Skip the definition.  */
15185       cp_lexer_consume_token (parser->lexer);
15186       if (cp_parser_skip_to_closing_brace (parser))
15187         cp_lexer_consume_token (parser->lexer);
15188       return;
15189     }
15190   cp_parser_require (parser, CPP_EQ, RT_EQ);
15191   /* Look for the qualified-namespace-specifier.  */
15192   namespace_specifier
15193     = cp_parser_qualified_namespace_specifier (parser);
15194   /* Look for the `;' token.  */
15195   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15196
15197   /* Register the alias in the symbol table.  */
15198   do_namespace_alias (identifier, namespace_specifier);
15199 }
15200
15201 /* Parse a qualified-namespace-specifier.
15202
15203    qualified-namespace-specifier:
15204      :: [opt] nested-name-specifier [opt] namespace-name
15205
15206    Returns a NAMESPACE_DECL corresponding to the specified
15207    namespace.  */
15208
15209 static tree
15210 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15211 {
15212   /* Look for the optional `::'.  */
15213   cp_parser_global_scope_opt (parser,
15214                               /*current_scope_valid_p=*/false);
15215
15216   /* Look for the optional nested-name-specifier.  */
15217   cp_parser_nested_name_specifier_opt (parser,
15218                                        /*typename_keyword_p=*/false,
15219                                        /*check_dependency_p=*/true,
15220                                        /*type_p=*/false,
15221                                        /*is_declaration=*/true);
15222
15223   return cp_parser_namespace_name (parser);
15224 }
15225
15226 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15227    access declaration.
15228
15229    using-declaration:
15230      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15231      using :: unqualified-id ;  
15232
15233    access-declaration:
15234      qualified-id ;  
15235
15236    */
15237
15238 static bool
15239 cp_parser_using_declaration (cp_parser* parser, 
15240                              bool access_declaration_p)
15241 {
15242   cp_token *token;
15243   bool typename_p = false;
15244   bool global_scope_p;
15245   tree decl;
15246   tree identifier;
15247   tree qscope;
15248   int oldcount = errorcount;
15249   cp_token *diag_token = NULL;
15250
15251   if (access_declaration_p)
15252     {
15253       diag_token = cp_lexer_peek_token (parser->lexer);
15254       cp_parser_parse_tentatively (parser);
15255     }
15256   else
15257     {
15258       /* Look for the `using' keyword.  */
15259       cp_parser_require_keyword (parser, RID_USING, RT_USING);
15260       
15261       /* Peek at the next token.  */
15262       token = cp_lexer_peek_token (parser->lexer);
15263       /* See if it's `typename'.  */
15264       if (token->keyword == RID_TYPENAME)
15265         {
15266           /* Remember that we've seen it.  */
15267           typename_p = true;
15268           /* Consume the `typename' token.  */
15269           cp_lexer_consume_token (parser->lexer);
15270         }
15271     }
15272
15273   /* Look for the optional global scope qualification.  */
15274   global_scope_p
15275     = (cp_parser_global_scope_opt (parser,
15276                                    /*current_scope_valid_p=*/false)
15277        != NULL_TREE);
15278
15279   /* If we saw `typename', or didn't see `::', then there must be a
15280      nested-name-specifier present.  */
15281   if (typename_p || !global_scope_p)
15282     qscope = cp_parser_nested_name_specifier (parser, typename_p,
15283                                               /*check_dependency_p=*/true,
15284                                               /*type_p=*/false,
15285                                               /*is_declaration=*/true);
15286   /* Otherwise, we could be in either of the two productions.  In that
15287      case, treat the nested-name-specifier as optional.  */
15288   else
15289     qscope = cp_parser_nested_name_specifier_opt (parser,
15290                                                   /*typename_keyword_p=*/false,
15291                                                   /*check_dependency_p=*/true,
15292                                                   /*type_p=*/false,
15293                                                   /*is_declaration=*/true);
15294   if (!qscope)
15295     qscope = global_namespace;
15296
15297   if (access_declaration_p && cp_parser_error_occurred (parser))
15298     /* Something has already gone wrong; there's no need to parse
15299        further.  Since an error has occurred, the return value of
15300        cp_parser_parse_definitely will be false, as required.  */
15301     return cp_parser_parse_definitely (parser);
15302
15303   token = cp_lexer_peek_token (parser->lexer);
15304   /* Parse the unqualified-id.  */
15305   identifier = cp_parser_unqualified_id (parser,
15306                                          /*template_keyword_p=*/false,
15307                                          /*check_dependency_p=*/true,
15308                                          /*declarator_p=*/true,
15309                                          /*optional_p=*/false);
15310
15311   if (access_declaration_p)
15312     {
15313       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15314         cp_parser_simulate_error (parser);
15315       if (!cp_parser_parse_definitely (parser))
15316         return false;
15317     }
15318
15319   /* The function we call to handle a using-declaration is different
15320      depending on what scope we are in.  */
15321   if (qscope == error_mark_node || identifier == error_mark_node)
15322     ;
15323   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15324            && TREE_CODE (identifier) != BIT_NOT_EXPR)
15325     /* [namespace.udecl]
15326
15327        A using declaration shall not name a template-id.  */
15328     error_at (token->location,
15329               "a template-id may not appear in a using-declaration");
15330   else
15331     {
15332       if (at_class_scope_p ())
15333         {
15334           /* Create the USING_DECL.  */
15335           decl = do_class_using_decl (parser->scope, identifier);
15336
15337           if (decl && typename_p)
15338             USING_DECL_TYPENAME_P (decl) = 1;
15339
15340           if (check_for_bare_parameter_packs (decl))
15341             return false;
15342           else
15343             /* Add it to the list of members in this class.  */
15344             finish_member_declaration (decl);
15345         }
15346       else
15347         {
15348           decl = cp_parser_lookup_name_simple (parser,
15349                                                identifier,
15350                                                token->location);
15351           if (decl == error_mark_node)
15352             cp_parser_name_lookup_error (parser, identifier,
15353                                          decl, NLE_NULL,
15354                                          token->location);
15355           else if (check_for_bare_parameter_packs (decl))
15356             return false;
15357           else if (!at_namespace_scope_p ())
15358             do_local_using_decl (decl, qscope, identifier);
15359           else
15360             do_toplevel_using_decl (decl, qscope, identifier);
15361         }
15362     }
15363
15364   /* Look for the final `;'.  */
15365   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15366
15367   if (access_declaration_p && errorcount == oldcount)
15368     warning_at (diag_token->location, OPT_Wdeprecated,
15369                 "access declarations are deprecated "
15370                 "in favour of using-declarations; "
15371                 "suggestion: add the %<using%> keyword");
15372
15373   return true;
15374 }
15375
15376 /* Parse an alias-declaration.
15377
15378    alias-declaration:
15379      using identifier attribute-specifier-seq [opt] = type-id  */
15380
15381 static tree
15382 cp_parser_alias_declaration (cp_parser* parser)
15383 {
15384   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15385   location_t id_location;
15386   cp_declarator *declarator;
15387   cp_decl_specifier_seq decl_specs;
15388   bool member_p;
15389   const char *saved_message = NULL;
15390
15391   /* Look for the `using' keyword.  */
15392   cp_token *using_token
15393     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15394   if (using_token == NULL)
15395     return error_mark_node;
15396
15397   id_location = cp_lexer_peek_token (parser->lexer)->location;
15398   id = cp_parser_identifier (parser);
15399   if (id == error_mark_node)
15400     return error_mark_node;
15401
15402   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15403   attributes = cp_parser_attributes_opt (parser);
15404   if (attributes == error_mark_node)
15405     return error_mark_node;
15406
15407   cp_parser_require (parser, CPP_EQ, RT_EQ);
15408
15409   if (cp_parser_error_occurred (parser))
15410     return error_mark_node;
15411
15412   cp_parser_commit_to_tentative_parse (parser);
15413
15414   /* Now we are going to parse the type-id of the declaration.  */
15415
15416   /*
15417     [dcl.type]/3 says:
15418
15419         "A type-specifier-seq shall not define a class or enumeration
15420          unless it appears in the type-id of an alias-declaration (7.1.3) that
15421          is not the declaration of a template-declaration."
15422
15423     In other words, if we currently are in an alias template, the
15424     type-id should not define a type.
15425
15426     So let's set parser->type_definition_forbidden_message in that
15427     case; cp_parser_check_type_definition (called by
15428     cp_parser_class_specifier) will then emit an error if a type is
15429     defined in the type-id.  */
15430   if (parser->num_template_parameter_lists)
15431     {
15432       saved_message = parser->type_definition_forbidden_message;
15433       parser->type_definition_forbidden_message =
15434         G_("types may not be defined in alias template declarations");
15435     }
15436
15437   type = cp_parser_type_id (parser);
15438
15439   /* Restore the error message if need be.  */
15440   if (parser->num_template_parameter_lists)
15441     parser->type_definition_forbidden_message = saved_message;
15442
15443   if (type == error_mark_node)
15444     {
15445       cp_parser_skip_to_end_of_block_or_statement (parser);
15446       return error_mark_node;
15447     }
15448
15449   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15450
15451   if (cp_parser_error_occurred (parser))
15452     {
15453       cp_parser_skip_to_end_of_block_or_statement (parser);
15454       return error_mark_node;
15455     }
15456
15457   /* A typedef-name can also be introduced by an alias-declaration. The
15458      identifier following the using keyword becomes a typedef-name. It has
15459      the same semantics as if it were introduced by the typedef
15460      specifier. In particular, it does not define a new type and it shall
15461      not appear in the type-id.  */
15462
15463   clear_decl_specs (&decl_specs);
15464   decl_specs.type = type;
15465   if (attributes != NULL_TREE)
15466     {
15467       decl_specs.attributes = attributes;
15468       set_and_check_decl_spec_loc (&decl_specs,
15469                                    ds_attribute,
15470                                    attrs_token);
15471     }
15472   set_and_check_decl_spec_loc (&decl_specs,
15473                                ds_typedef,
15474                                using_token);
15475   set_and_check_decl_spec_loc (&decl_specs,
15476                                ds_alias,
15477                                using_token);
15478
15479   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15480   declarator->id_loc = id_location;
15481
15482   member_p = at_class_scope_p ();
15483   if (member_p)
15484     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15485                       NULL_TREE, attributes);
15486   else
15487     decl = start_decl (declarator, &decl_specs, 0,
15488                        attributes, NULL_TREE, &pushed_scope);
15489   if (decl == error_mark_node)
15490     return decl;
15491
15492   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15493
15494   if (pushed_scope)
15495     pop_scope (pushed_scope);
15496
15497   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15498      added into the symbol table; otherwise, return the TYPE_DECL.  */
15499   if (DECL_LANG_SPECIFIC (decl)
15500       && DECL_TEMPLATE_INFO (decl)
15501       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15502     {
15503       decl = DECL_TI_TEMPLATE (decl);
15504       if (member_p)
15505         check_member_template (decl);
15506     }
15507
15508   return decl;
15509 }
15510
15511 /* Parse a using-directive.
15512
15513    using-directive:
15514      using namespace :: [opt] nested-name-specifier [opt]
15515        namespace-name ;  */
15516
15517 static void
15518 cp_parser_using_directive (cp_parser* parser)
15519 {
15520   tree namespace_decl;
15521   tree attribs;
15522
15523   /* Look for the `using' keyword.  */
15524   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15525   /* And the `namespace' keyword.  */
15526   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15527   /* Look for the optional `::' operator.  */
15528   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15529   /* And the optional nested-name-specifier.  */
15530   cp_parser_nested_name_specifier_opt (parser,
15531                                        /*typename_keyword_p=*/false,
15532                                        /*check_dependency_p=*/true,
15533                                        /*type_p=*/false,
15534                                        /*is_declaration=*/true);
15535   /* Get the namespace being used.  */
15536   namespace_decl = cp_parser_namespace_name (parser);
15537   /* And any specified attributes.  */
15538   attribs = cp_parser_attributes_opt (parser);
15539   /* Update the symbol table.  */
15540   parse_using_directive (namespace_decl, attribs);
15541   /* Look for the final `;'.  */
15542   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15543 }
15544
15545 /* Parse an asm-definition.
15546
15547    asm-definition:
15548      asm ( string-literal ) ;
15549
15550    GNU Extension:
15551
15552    asm-definition:
15553      asm volatile [opt] ( string-literal ) ;
15554      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15555      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15556                           : asm-operand-list [opt] ) ;
15557      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15558                           : asm-operand-list [opt]
15559                           : asm-clobber-list [opt] ) ;
15560      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15561                                : asm-clobber-list [opt]
15562                                : asm-goto-list ) ;  */
15563
15564 static void
15565 cp_parser_asm_definition (cp_parser* parser)
15566 {
15567   tree string;
15568   tree outputs = NULL_TREE;
15569   tree inputs = NULL_TREE;
15570   tree clobbers = NULL_TREE;
15571   tree labels = NULL_TREE;
15572   tree asm_stmt;
15573   bool volatile_p = false;
15574   bool extended_p = false;
15575   bool invalid_inputs_p = false;
15576   bool invalid_outputs_p = false;
15577   bool goto_p = false;
15578   required_token missing = RT_NONE;
15579
15580   /* Look for the `asm' keyword.  */
15581   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15582   /* See if the next token is `volatile'.  */
15583   if (cp_parser_allow_gnu_extensions_p (parser)
15584       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15585     {
15586       /* Remember that we saw the `volatile' keyword.  */
15587       volatile_p = true;
15588       /* Consume the token.  */
15589       cp_lexer_consume_token (parser->lexer);
15590     }
15591   if (cp_parser_allow_gnu_extensions_p (parser)
15592       && parser->in_function_body
15593       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15594     {
15595       /* Remember that we saw the `goto' keyword.  */
15596       goto_p = true;
15597       /* Consume the token.  */
15598       cp_lexer_consume_token (parser->lexer);
15599     }
15600   /* Look for the opening `('.  */
15601   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15602     return;
15603   /* Look for the string.  */
15604   string = cp_parser_string_literal (parser, false, false);
15605   if (string == error_mark_node)
15606     {
15607       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15608                                              /*consume_paren=*/true);
15609       return;
15610     }
15611
15612   /* If we're allowing GNU extensions, check for the extended assembly
15613      syntax.  Unfortunately, the `:' tokens need not be separated by
15614      a space in C, and so, for compatibility, we tolerate that here
15615      too.  Doing that means that we have to treat the `::' operator as
15616      two `:' tokens.  */
15617   if (cp_parser_allow_gnu_extensions_p (parser)
15618       && parser->in_function_body
15619       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15620           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15621     {
15622       bool inputs_p = false;
15623       bool clobbers_p = false;
15624       bool labels_p = false;
15625
15626       /* The extended syntax was used.  */
15627       extended_p = true;
15628
15629       /* Look for outputs.  */
15630       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15631         {
15632           /* Consume the `:'.  */
15633           cp_lexer_consume_token (parser->lexer);
15634           /* Parse the output-operands.  */
15635           if (cp_lexer_next_token_is_not (parser->lexer,
15636                                           CPP_COLON)
15637               && cp_lexer_next_token_is_not (parser->lexer,
15638                                              CPP_SCOPE)
15639               && cp_lexer_next_token_is_not (parser->lexer,
15640                                              CPP_CLOSE_PAREN)
15641               && !goto_p)
15642             outputs = cp_parser_asm_operand_list (parser);
15643
15644             if (outputs == error_mark_node)
15645               invalid_outputs_p = true;
15646         }
15647       /* If the next token is `::', there are no outputs, and the
15648          next token is the beginning of the inputs.  */
15649       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15650         /* The inputs are coming next.  */
15651         inputs_p = true;
15652
15653       /* Look for inputs.  */
15654       if (inputs_p
15655           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15656         {
15657           /* Consume the `:' or `::'.  */
15658           cp_lexer_consume_token (parser->lexer);
15659           /* Parse the output-operands.  */
15660           if (cp_lexer_next_token_is_not (parser->lexer,
15661                                           CPP_COLON)
15662               && cp_lexer_next_token_is_not (parser->lexer,
15663                                              CPP_SCOPE)
15664               && cp_lexer_next_token_is_not (parser->lexer,
15665                                              CPP_CLOSE_PAREN))
15666             inputs = cp_parser_asm_operand_list (parser);
15667
15668             if (inputs == error_mark_node)
15669               invalid_inputs_p = true;
15670         }
15671       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15672         /* The clobbers are coming next.  */
15673         clobbers_p = true;
15674
15675       /* Look for clobbers.  */
15676       if (clobbers_p
15677           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15678         {
15679           clobbers_p = true;
15680           /* Consume the `:' or `::'.  */
15681           cp_lexer_consume_token (parser->lexer);
15682           /* Parse the clobbers.  */
15683           if (cp_lexer_next_token_is_not (parser->lexer,
15684                                           CPP_COLON)
15685               && cp_lexer_next_token_is_not (parser->lexer,
15686                                              CPP_CLOSE_PAREN))
15687             clobbers = cp_parser_asm_clobber_list (parser);
15688         }
15689       else if (goto_p
15690                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15691         /* The labels are coming next.  */
15692         labels_p = true;
15693
15694       /* Look for labels.  */
15695       if (labels_p
15696           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15697         {
15698           labels_p = true;
15699           /* Consume the `:' or `::'.  */
15700           cp_lexer_consume_token (parser->lexer);
15701           /* Parse the labels.  */
15702           labels = cp_parser_asm_label_list (parser);
15703         }
15704
15705       if (goto_p && !labels_p)
15706         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15707     }
15708   else if (goto_p)
15709     missing = RT_COLON_SCOPE;
15710
15711   /* Look for the closing `)'.  */
15712   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15713                           missing ? missing : RT_CLOSE_PAREN))
15714     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15715                                            /*consume_paren=*/true);
15716   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15717
15718   if (!invalid_inputs_p && !invalid_outputs_p)
15719     {
15720       /* Create the ASM_EXPR.  */
15721       if (parser->in_function_body)
15722         {
15723           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15724                                       inputs, clobbers, labels);
15725           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15726           if (!extended_p)
15727             {
15728               tree temp = asm_stmt;
15729               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15730                 temp = TREE_OPERAND (temp, 0);
15731
15732               ASM_INPUT_P (temp) = 1;
15733             }
15734         }
15735       else
15736         add_asm_node (string);
15737     }
15738 }
15739
15740 /* Declarators [gram.dcl.decl] */
15741
15742 /* Parse an init-declarator.
15743
15744    init-declarator:
15745      declarator initializer [opt]
15746
15747    GNU Extension:
15748
15749    init-declarator:
15750      declarator asm-specification [opt] attributes [opt] initializer [opt]
15751
15752    function-definition:
15753      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15754        function-body
15755      decl-specifier-seq [opt] declarator function-try-block
15756
15757    GNU Extension:
15758
15759    function-definition:
15760      __extension__ function-definition
15761
15762    TM Extension:
15763
15764    function-definition:
15765      decl-specifier-seq [opt] declarator function-transaction-block
15766
15767    The DECL_SPECIFIERS apply to this declarator.  Returns a
15768    representation of the entity declared.  If MEMBER_P is TRUE, then
15769    this declarator appears in a class scope.  The new DECL created by
15770    this declarator is returned.
15771
15772    The CHECKS are access checks that should be performed once we know
15773    what entity is being declared (and, therefore, what classes have
15774    befriended it).
15775
15776    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15777    for a function-definition here as well.  If the declarator is a
15778    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15779    be TRUE upon return.  By that point, the function-definition will
15780    have been completely parsed.
15781
15782    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15783    is FALSE.
15784
15785    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15786    parsed declaration if it is an uninitialized single declarator not followed
15787    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15788    if present, will not be consumed.  If returned, this declarator will be
15789    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15790
15791 static tree
15792 cp_parser_init_declarator (cp_parser* parser,
15793                            cp_decl_specifier_seq *decl_specifiers,
15794                            vec<deferred_access_check, va_gc> *checks,
15795                            bool function_definition_allowed_p,
15796                            bool member_p,
15797                            int declares_class_or_enum,
15798                            bool* function_definition_p,
15799                            tree* maybe_range_for_decl)
15800 {
15801   cp_token *token = NULL, *asm_spec_start_token = NULL,
15802            *attributes_start_token = NULL;
15803   cp_declarator *declarator;
15804   tree prefix_attributes;
15805   tree attributes = NULL;
15806   tree asm_specification;
15807   tree initializer;
15808   tree decl = NULL_TREE;
15809   tree scope;
15810   int is_initialized;
15811   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15812      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15813      "(...)".  */
15814   enum cpp_ttype initialization_kind;
15815   bool is_direct_init = false;
15816   bool is_non_constant_init;
15817   int ctor_dtor_or_conv_p;
15818   bool friend_p;
15819   tree pushed_scope = NULL_TREE;
15820   bool range_for_decl_p = false;
15821
15822   /* Gather the attributes that were provided with the
15823      decl-specifiers.  */
15824   prefix_attributes = decl_specifiers->attributes;
15825
15826   /* Assume that this is not the declarator for a function
15827      definition.  */
15828   if (function_definition_p)
15829     *function_definition_p = false;
15830
15831   /* Defer access checks while parsing the declarator; we cannot know
15832      what names are accessible until we know what is being
15833      declared.  */
15834   resume_deferring_access_checks ();
15835
15836   /* Parse the declarator.  */
15837   token = cp_lexer_peek_token (parser->lexer);
15838   declarator
15839     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15840                             &ctor_dtor_or_conv_p,
15841                             /*parenthesized_p=*/NULL,
15842                             member_p);
15843   /* Gather up the deferred checks.  */
15844   stop_deferring_access_checks ();
15845
15846   /* If the DECLARATOR was erroneous, there's no need to go
15847      further.  */
15848   if (declarator == cp_error_declarator)
15849     return error_mark_node;
15850
15851   /* Check that the number of template-parameter-lists is OK.  */
15852   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15853                                                        token->location))
15854     return error_mark_node;
15855
15856   if (declares_class_or_enum & 2)
15857     cp_parser_check_for_definition_in_return_type (declarator,
15858                                                    decl_specifiers->type,
15859                                                    decl_specifiers->locations[ds_type_spec]);
15860
15861   /* Figure out what scope the entity declared by the DECLARATOR is
15862      located in.  `grokdeclarator' sometimes changes the scope, so
15863      we compute it now.  */
15864   scope = get_scope_of_declarator (declarator);
15865
15866   /* Perform any lookups in the declared type which were thought to be
15867      dependent, but are not in the scope of the declarator.  */
15868   decl_specifiers->type
15869     = maybe_update_decl_type (decl_specifiers->type, scope);
15870
15871   /* If we're allowing GNU extensions, look for an
15872      asm-specification.  */
15873   if (cp_parser_allow_gnu_extensions_p (parser))
15874     {
15875       /* Look for an asm-specification.  */
15876       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15877       asm_specification = cp_parser_asm_specification_opt (parser);
15878     }
15879   else
15880     asm_specification = NULL_TREE;
15881
15882   /* Look for attributes.  */
15883   attributes_start_token = cp_lexer_peek_token (parser->lexer);
15884   attributes = cp_parser_attributes_opt (parser);
15885
15886   /* Peek at the next token.  */
15887   token = cp_lexer_peek_token (parser->lexer);
15888   /* Check to see if the token indicates the start of a
15889      function-definition.  */
15890   if (function_declarator_p (declarator)
15891       && cp_parser_token_starts_function_definition_p (token))
15892     {
15893       if (!function_definition_allowed_p)
15894         {
15895           /* If a function-definition should not appear here, issue an
15896              error message.  */
15897           cp_parser_error (parser,
15898                            "a function-definition is not allowed here");
15899           return error_mark_node;
15900         }
15901       else
15902         {
15903           location_t func_brace_location
15904             = cp_lexer_peek_token (parser->lexer)->location;
15905
15906           /* Neither attributes nor an asm-specification are allowed
15907              on a function-definition.  */
15908           if (asm_specification)
15909             error_at (asm_spec_start_token->location,
15910                       "an asm-specification is not allowed "
15911                       "on a function-definition");
15912           if (attributes)
15913             error_at (attributes_start_token->location,
15914                       "attributes are not allowed on a function-definition");
15915           /* This is a function-definition.  */
15916           *function_definition_p = true;
15917
15918           /* Parse the function definition.  */
15919           if (member_p)
15920             decl = cp_parser_save_member_function_body (parser,
15921                                                         decl_specifiers,
15922                                                         declarator,
15923                                                         prefix_attributes);
15924           else
15925             decl
15926               = (cp_parser_function_definition_from_specifiers_and_declarator
15927                  (parser, decl_specifiers, prefix_attributes, declarator));
15928
15929           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15930             {
15931               /* This is where the prologue starts...  */
15932               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15933                 = func_brace_location;
15934             }
15935
15936           return decl;
15937         }
15938     }
15939
15940   /* [dcl.dcl]
15941
15942      Only in function declarations for constructors, destructors, and
15943      type conversions can the decl-specifier-seq be omitted.
15944
15945      We explicitly postpone this check past the point where we handle
15946      function-definitions because we tolerate function-definitions
15947      that are missing their return types in some modes.  */
15948   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15949     {
15950       cp_parser_error (parser,
15951                        "expected constructor, destructor, or type conversion");
15952       return error_mark_node;
15953     }
15954
15955   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15956   if (token->type == CPP_EQ
15957       || token->type == CPP_OPEN_PAREN
15958       || token->type == CPP_OPEN_BRACE)
15959     {
15960       is_initialized = SD_INITIALIZED;
15961       initialization_kind = token->type;
15962       if (maybe_range_for_decl)
15963         *maybe_range_for_decl = error_mark_node;
15964
15965       if (token->type == CPP_EQ
15966           && function_declarator_p (declarator))
15967         {
15968           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15969           if (t2->keyword == RID_DEFAULT)
15970             is_initialized = SD_DEFAULTED;
15971           else if (t2->keyword == RID_DELETE)
15972             is_initialized = SD_DELETED;
15973         }
15974     }
15975   else
15976     {
15977       /* If the init-declarator isn't initialized and isn't followed by a
15978          `,' or `;', it's not a valid init-declarator.  */
15979       if (token->type != CPP_COMMA
15980           && token->type != CPP_SEMICOLON)
15981         {
15982           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15983             range_for_decl_p = true;
15984           else
15985             {
15986               cp_parser_error (parser, "expected initializer");
15987               return error_mark_node;
15988             }
15989         }
15990       is_initialized = SD_UNINITIALIZED;
15991       initialization_kind = CPP_EOF;
15992     }
15993
15994   /* Because start_decl has side-effects, we should only call it if we
15995      know we're going ahead.  By this point, we know that we cannot
15996      possibly be looking at any other construct.  */
15997   cp_parser_commit_to_tentative_parse (parser);
15998
15999   /* If the decl specifiers were bad, issue an error now that we're
16000      sure this was intended to be a declarator.  Then continue
16001      declaring the variable(s), as int, to try to cut down on further
16002      errors.  */
16003   if (decl_specifiers->any_specifiers_p
16004       && decl_specifiers->type == error_mark_node)
16005     {
16006       cp_parser_error (parser, "invalid type in declaration");
16007       decl_specifiers->type = integer_type_node;
16008     }
16009
16010   /* Check to see whether or not this declaration is a friend.  */
16011   friend_p = cp_parser_friend_p (decl_specifiers);
16012
16013   /* Enter the newly declared entry in the symbol table.  If we're
16014      processing a declaration in a class-specifier, we wait until
16015      after processing the initializer.  */
16016   if (!member_p)
16017     {
16018       if (parser->in_unbraced_linkage_specification_p)
16019         decl_specifiers->storage_class = sc_extern;
16020       decl = start_decl (declarator, decl_specifiers,
16021                          range_for_decl_p? SD_INITIALIZED : is_initialized,
16022                          attributes, prefix_attributes,
16023                          &pushed_scope);
16024       /* Adjust location of decl if declarator->id_loc is more appropriate:
16025          set, and decl wasn't merged with another decl, in which case its
16026          location would be different from input_location, and more accurate.  */
16027       if (DECL_P (decl)
16028           && declarator->id_loc != UNKNOWN_LOCATION
16029           && DECL_SOURCE_LOCATION (decl) == input_location)
16030         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16031     }
16032   else if (scope)
16033     /* Enter the SCOPE.  That way unqualified names appearing in the
16034        initializer will be looked up in SCOPE.  */
16035     pushed_scope = push_scope (scope);
16036
16037   /* Perform deferred access control checks, now that we know in which
16038      SCOPE the declared entity resides.  */
16039   if (!member_p && decl)
16040     {
16041       tree saved_current_function_decl = NULL_TREE;
16042
16043       /* If the entity being declared is a function, pretend that we
16044          are in its scope.  If it is a `friend', it may have access to
16045          things that would not otherwise be accessible.  */
16046       if (TREE_CODE (decl) == FUNCTION_DECL)
16047         {
16048           saved_current_function_decl = current_function_decl;
16049           current_function_decl = decl;
16050         }
16051
16052       /* Perform access checks for template parameters.  */
16053       cp_parser_perform_template_parameter_access_checks (checks);
16054
16055       /* Perform the access control checks for the declarator and the
16056          decl-specifiers.  */
16057       perform_deferred_access_checks (tf_warning_or_error);
16058
16059       /* Restore the saved value.  */
16060       if (TREE_CODE (decl) == FUNCTION_DECL)
16061         current_function_decl = saved_current_function_decl;
16062     }
16063
16064   /* Parse the initializer.  */
16065   initializer = NULL_TREE;
16066   is_direct_init = false;
16067   is_non_constant_init = true;
16068   if (is_initialized)
16069     {
16070       if (function_declarator_p (declarator))
16071         {
16072           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16073            if (initialization_kind == CPP_EQ)
16074              initializer = cp_parser_pure_specifier (parser);
16075            else
16076              {
16077                /* If the declaration was erroneous, we don't really
16078                   know what the user intended, so just silently
16079                   consume the initializer.  */
16080                if (decl != error_mark_node)
16081                  error_at (initializer_start_token->location,
16082                            "initializer provided for function");
16083                cp_parser_skip_to_closing_parenthesis (parser,
16084                                                       /*recovering=*/true,
16085                                                       /*or_comma=*/false,
16086                                                       /*consume_paren=*/true);
16087              }
16088         }
16089       else
16090         {
16091           /* We want to record the extra mangling scope for in-class
16092              initializers of class members and initializers of static data
16093              member templates.  The former involves deferring
16094              parsing of the initializer until end of class as with default
16095              arguments.  So right here we only handle the latter.  */
16096           if (!member_p && processing_template_decl)
16097             start_lambda_scope (decl);
16098           initializer = cp_parser_initializer (parser,
16099                                                &is_direct_init,
16100                                                &is_non_constant_init);
16101           if (!member_p && processing_template_decl)
16102             finish_lambda_scope ();
16103           if (initializer == error_mark_node)
16104             cp_parser_skip_to_end_of_statement (parser);
16105         }
16106     }
16107
16108   /* The old parser allows attributes to appear after a parenthesized
16109      initializer.  Mark Mitchell proposed removing this functionality
16110      on the GCC mailing lists on 2002-08-13.  This parser accepts the
16111      attributes -- but ignores them.  */
16112   if (cp_parser_allow_gnu_extensions_p (parser)
16113       && initialization_kind == CPP_OPEN_PAREN)
16114     if (cp_parser_attributes_opt (parser))
16115       warning (OPT_Wattributes,
16116                "attributes after parenthesized initializer ignored");
16117
16118   /* For an in-class declaration, use `grokfield' to create the
16119      declaration.  */
16120   if (member_p)
16121     {
16122       if (pushed_scope)
16123         {
16124           pop_scope (pushed_scope);
16125           pushed_scope = NULL_TREE;
16126         }
16127       decl = grokfield (declarator, decl_specifiers,
16128                         initializer, !is_non_constant_init,
16129                         /*asmspec=*/NULL_TREE,
16130                         prefix_attributes);
16131       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16132         cp_parser_save_default_args (parser, decl);
16133     }
16134
16135   /* Finish processing the declaration.  But, skip member
16136      declarations.  */
16137   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16138     {
16139       cp_finish_decl (decl,
16140                       initializer, !is_non_constant_init,
16141                       asm_specification,
16142                       /* If the initializer is in parentheses, then this is
16143                          a direct-initialization, which means that an
16144                          `explicit' constructor is OK.  Otherwise, an
16145                          `explicit' constructor cannot be used.  */
16146                       ((is_direct_init || !is_initialized)
16147                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16148     }
16149   else if ((cxx_dialect != cxx98) && friend_p
16150            && decl && TREE_CODE (decl) == FUNCTION_DECL)
16151     /* Core issue #226 (C++0x only): A default template-argument
16152        shall not be specified in a friend class template
16153        declaration. */
16154     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
16155                              /*is_partial=*/false, /*is_friend_decl=*/1);
16156
16157   if (!friend_p && pushed_scope)
16158     pop_scope (pushed_scope);
16159
16160   return decl;
16161 }
16162
16163 /* Parse a declarator.
16164
16165    declarator:
16166      direct-declarator
16167      ptr-operator declarator
16168
16169    abstract-declarator:
16170      ptr-operator abstract-declarator [opt]
16171      direct-abstract-declarator
16172
16173    GNU Extensions:
16174
16175    declarator:
16176      attributes [opt] direct-declarator
16177      attributes [opt] ptr-operator declarator
16178
16179    abstract-declarator:
16180      attributes [opt] ptr-operator abstract-declarator [opt]
16181      attributes [opt] direct-abstract-declarator
16182
16183    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16184    detect constructor, destructor or conversion operators. It is set
16185    to -1 if the declarator is a name, and +1 if it is a
16186    function. Otherwise it is set to zero. Usually you just want to
16187    test for >0, but internally the negative value is used.
16188
16189    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16190    a decl-specifier-seq unless it declares a constructor, destructor,
16191    or conversion.  It might seem that we could check this condition in
16192    semantic analysis, rather than parsing, but that makes it difficult
16193    to handle something like `f()'.  We want to notice that there are
16194    no decl-specifiers, and therefore realize that this is an
16195    expression, not a declaration.)
16196
16197    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16198    the declarator is a direct-declarator of the form "(...)".
16199
16200    MEMBER_P is true iff this declarator is a member-declarator.  */
16201
16202 static cp_declarator *
16203 cp_parser_declarator (cp_parser* parser,
16204                       cp_parser_declarator_kind dcl_kind,
16205                       int* ctor_dtor_or_conv_p,
16206                       bool* parenthesized_p,
16207                       bool member_p)
16208 {
16209   cp_declarator *declarator;
16210   enum tree_code code;
16211   cp_cv_quals cv_quals;
16212   tree class_type;
16213   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16214
16215   /* Assume this is not a constructor, destructor, or type-conversion
16216      operator.  */
16217   if (ctor_dtor_or_conv_p)
16218     *ctor_dtor_or_conv_p = 0;
16219
16220   if (cp_parser_allow_gnu_extensions_p (parser))
16221     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16222
16223   /* Check for the ptr-operator production.  */
16224   cp_parser_parse_tentatively (parser);
16225   /* Parse the ptr-operator.  */
16226   code = cp_parser_ptr_operator (parser,
16227                                  &class_type,
16228                                  &cv_quals,
16229                                  &std_attributes);
16230
16231   /* If that worked, then we have a ptr-operator.  */
16232   if (cp_parser_parse_definitely (parser))
16233     {
16234       /* If a ptr-operator was found, then this declarator was not
16235          parenthesized.  */
16236       if (parenthesized_p)
16237         *parenthesized_p = true;
16238       /* The dependent declarator is optional if we are parsing an
16239          abstract-declarator.  */
16240       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16241         cp_parser_parse_tentatively (parser);
16242
16243       /* Parse the dependent declarator.  */
16244       declarator = cp_parser_declarator (parser, dcl_kind,
16245                                          /*ctor_dtor_or_conv_p=*/NULL,
16246                                          /*parenthesized_p=*/NULL,
16247                                          /*member_p=*/false);
16248
16249       /* If we are parsing an abstract-declarator, we must handle the
16250          case where the dependent declarator is absent.  */
16251       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16252           && !cp_parser_parse_definitely (parser))
16253         declarator = NULL;
16254
16255       declarator = cp_parser_make_indirect_declarator
16256         (code, class_type, cv_quals, declarator, std_attributes);
16257     }
16258   /* Everything else is a direct-declarator.  */
16259   else
16260     {
16261       if (parenthesized_p)
16262         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16263                                                    CPP_OPEN_PAREN);
16264       declarator = cp_parser_direct_declarator (parser, dcl_kind,
16265                                                 ctor_dtor_or_conv_p,
16266                                                 member_p);
16267     }
16268
16269   if (gnu_attributes && declarator && declarator != cp_error_declarator)
16270     declarator->attributes = gnu_attributes;
16271   return declarator;
16272 }
16273
16274 /* Parse a direct-declarator or direct-abstract-declarator.
16275
16276    direct-declarator:
16277      declarator-id
16278      direct-declarator ( parameter-declaration-clause )
16279        cv-qualifier-seq [opt]
16280        ref-qualifier [opt]
16281        exception-specification [opt]
16282      direct-declarator [ constant-expression [opt] ]
16283      ( declarator )
16284
16285    direct-abstract-declarator:
16286      direct-abstract-declarator [opt]
16287        ( parameter-declaration-clause )
16288        cv-qualifier-seq [opt]
16289        ref-qualifier [opt]
16290        exception-specification [opt]
16291      direct-abstract-declarator [opt] [ constant-expression [opt] ]
16292      ( abstract-declarator )
16293
16294    Returns a representation of the declarator.  DCL_KIND is
16295    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16296    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
16297    we are parsing a direct-declarator.  It is
16298    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16299    of ambiguity we prefer an abstract declarator, as per
16300    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16301    cp_parser_declarator.  */
16302
16303 static cp_declarator *
16304 cp_parser_direct_declarator (cp_parser* parser,
16305                              cp_parser_declarator_kind dcl_kind,
16306                              int* ctor_dtor_or_conv_p,
16307                              bool member_p)
16308 {
16309   cp_token *token;
16310   cp_declarator *declarator = NULL;
16311   tree scope = NULL_TREE;
16312   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16313   bool saved_in_declarator_p = parser->in_declarator_p;
16314   bool first = true;
16315   tree pushed_scope = NULL_TREE;
16316
16317   while (true)
16318     {
16319       /* Peek at the next token.  */
16320       token = cp_lexer_peek_token (parser->lexer);
16321       if (token->type == CPP_OPEN_PAREN)
16322         {
16323           /* This is either a parameter-declaration-clause, or a
16324              parenthesized declarator. When we know we are parsing a
16325              named declarator, it must be a parenthesized declarator
16326              if FIRST is true. For instance, `(int)' is a
16327              parameter-declaration-clause, with an omitted
16328              direct-abstract-declarator. But `((*))', is a
16329              parenthesized abstract declarator. Finally, when T is a
16330              template parameter `(T)' is a
16331              parameter-declaration-clause, and not a parenthesized
16332              named declarator.
16333
16334              We first try and parse a parameter-declaration-clause,
16335              and then try a nested declarator (if FIRST is true).
16336
16337              It is not an error for it not to be a
16338              parameter-declaration-clause, even when FIRST is
16339              false. Consider,
16340
16341                int i (int);
16342                int i (3);
16343
16344              The first is the declaration of a function while the
16345              second is the definition of a variable, including its
16346              initializer.
16347
16348              Having seen only the parenthesis, we cannot know which of
16349              these two alternatives should be selected.  Even more
16350              complex are examples like:
16351
16352                int i (int (a));
16353                int i (int (3));
16354
16355              The former is a function-declaration; the latter is a
16356              variable initialization.
16357
16358              Thus again, we try a parameter-declaration-clause, and if
16359              that fails, we back out and return.  */
16360
16361           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16362             {
16363               tree params;
16364               unsigned saved_num_template_parameter_lists;
16365               bool is_declarator = false;
16366               tree t;
16367
16368               /* In a member-declarator, the only valid interpretation
16369                  of a parenthesis is the start of a
16370                  parameter-declaration-clause.  (It is invalid to
16371                  initialize a static data member with a parenthesized
16372                  initializer; only the "=" form of initialization is
16373                  permitted.)  */
16374               if (!member_p)
16375                 cp_parser_parse_tentatively (parser);
16376
16377               /* Consume the `('.  */
16378               cp_lexer_consume_token (parser->lexer);
16379               if (first)
16380                 {
16381                   /* If this is going to be an abstract declarator, we're
16382                      in a declarator and we can't have default args.  */
16383                   parser->default_arg_ok_p = false;
16384                   parser->in_declarator_p = true;
16385                 }
16386
16387               /* Inside the function parameter list, surrounding
16388                  template-parameter-lists do not apply.  */
16389               saved_num_template_parameter_lists
16390                 = parser->num_template_parameter_lists;
16391               parser->num_template_parameter_lists = 0;
16392
16393               begin_scope (sk_function_parms, NULL_TREE);
16394
16395               /* Parse the parameter-declaration-clause.  */
16396               params = cp_parser_parameter_declaration_clause (parser);
16397
16398               parser->num_template_parameter_lists
16399                 = saved_num_template_parameter_lists;
16400
16401               /* Consume the `)'.  */
16402               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16403
16404               /* If all went well, parse the cv-qualifier-seq,
16405                  ref-qualifier and the exception-specification.  */
16406               if (member_p || cp_parser_parse_definitely (parser))
16407                 {
16408                   cp_cv_quals cv_quals;
16409                   cp_virt_specifiers virt_specifiers;
16410                   cp_ref_qualifier ref_qual;
16411                   tree exception_specification;
16412                   tree late_return;
16413                   tree attrs;
16414                   bool memfn = (member_p || (pushed_scope
16415                                              && CLASS_TYPE_P (pushed_scope)));
16416
16417                   is_declarator = true;
16418
16419                   if (ctor_dtor_or_conv_p)
16420                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16421                   first = false;
16422
16423                   /* Parse the cv-qualifier-seq.  */
16424                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16425                   /* Parse the ref-qualifier. */
16426                   ref_qual = cp_parser_ref_qualifier_seq_opt (parser);
16427                   /* And the exception-specification.  */
16428                   exception_specification
16429                     = cp_parser_exception_specification_opt (parser);
16430
16431                   attrs = cp_parser_std_attribute_spec_seq (parser);
16432
16433                   late_return = (cp_parser_late_return_type_opt
16434                                  (parser, memfn ? cv_quals : -1));
16435
16436                   /* Parse the virt-specifier-seq.  */
16437                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16438
16439                   /* Create the function-declarator.  */
16440                   declarator = make_call_declarator (declarator,
16441                                                      params,
16442                                                      cv_quals,
16443                                                      virt_specifiers,
16444                                                      ref_qual,
16445                                                      exception_specification,
16446                                                      late_return);
16447                   declarator->std_attributes = attrs;
16448                   /* Any subsequent parameter lists are to do with
16449                      return type, so are not those of the declared
16450                      function.  */
16451                   parser->default_arg_ok_p = false;
16452                 }
16453
16454               /* Remove the function parms from scope.  */
16455               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16456                 pop_binding (DECL_NAME (t), t);
16457               leave_scope();
16458
16459               if (is_declarator)
16460                 /* Repeat the main loop.  */
16461                 continue;
16462             }
16463
16464           /* If this is the first, we can try a parenthesized
16465              declarator.  */
16466           if (first)
16467             {
16468               bool saved_in_type_id_in_expr_p;
16469
16470               parser->default_arg_ok_p = saved_default_arg_ok_p;
16471               parser->in_declarator_p = saved_in_declarator_p;
16472
16473               /* Consume the `('.  */
16474               cp_lexer_consume_token (parser->lexer);
16475               /* Parse the nested declarator.  */
16476               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16477               parser->in_type_id_in_expr_p = true;
16478               declarator
16479                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16480                                         /*parenthesized_p=*/NULL,
16481                                         member_p);
16482               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16483               first = false;
16484               /* Expect a `)'.  */
16485               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16486                 declarator = cp_error_declarator;
16487               if (declarator == cp_error_declarator)
16488                 break;
16489
16490               goto handle_declarator;
16491             }
16492           /* Otherwise, we must be done.  */
16493           else
16494             break;
16495         }
16496       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16497                && token->type == CPP_OPEN_SQUARE
16498                && !cp_next_tokens_can_be_attribute_p (parser))
16499         {
16500           /* Parse an array-declarator.  */
16501           tree bounds, attrs;
16502
16503           if (ctor_dtor_or_conv_p)
16504             *ctor_dtor_or_conv_p = 0;
16505
16506           first = false;
16507           parser->default_arg_ok_p = false;
16508           parser->in_declarator_p = true;
16509           /* Consume the `['.  */
16510           cp_lexer_consume_token (parser->lexer);
16511           /* Peek at the next token.  */
16512           token = cp_lexer_peek_token (parser->lexer);
16513           /* If the next token is `]', then there is no
16514              constant-expression.  */
16515           if (token->type != CPP_CLOSE_SQUARE)
16516             {
16517               bool non_constant_p;
16518
16519               bounds
16520                 = cp_parser_constant_expression (parser,
16521                                                  /*allow_non_constant=*/true,
16522                                                  &non_constant_p);
16523               if (!non_constant_p)
16524                 /* OK */;
16525               else if (error_operand_p (bounds))
16526                 /* Already gave an error.  */;
16527               else if (!parser->in_function_body
16528                        || current_binding_level->kind == sk_function_parms)
16529                 {
16530                   /* Normally, the array bound must be an integral constant
16531                      expression.  However, as an extension, we allow VLAs
16532                      in function scopes as long as they aren't part of a
16533                      parameter declaration.  */
16534                   cp_parser_error (parser,
16535                                    "array bound is not an integer constant");
16536                   bounds = error_mark_node;
16537                 }
16538               else if (processing_template_decl)
16539                 {
16540                   /* Remember this wasn't a constant-expression.  */
16541                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16542                   TREE_SIDE_EFFECTS (bounds) = 1;
16543                 }
16544             }
16545           else
16546             bounds = NULL_TREE;
16547           /* Look for the closing `]'.  */
16548           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16549             {
16550               declarator = cp_error_declarator;
16551               break;
16552             }
16553
16554           attrs = cp_parser_std_attribute_spec_seq (parser);
16555           declarator = make_array_declarator (declarator, bounds);
16556           declarator->std_attributes = attrs;
16557         }
16558       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16559         {
16560           {
16561             tree qualifying_scope;
16562             tree unqualified_name;
16563             tree attrs;
16564             special_function_kind sfk;
16565             bool abstract_ok;
16566             bool pack_expansion_p = false;
16567             cp_token *declarator_id_start_token;
16568
16569             /* Parse a declarator-id */
16570             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16571             if (abstract_ok)
16572               {
16573                 cp_parser_parse_tentatively (parser);
16574
16575                 /* If we see an ellipsis, we should be looking at a
16576                    parameter pack. */
16577                 if (token->type == CPP_ELLIPSIS)
16578                   {
16579                     /* Consume the `...' */
16580                     cp_lexer_consume_token (parser->lexer);
16581
16582                     pack_expansion_p = true;
16583                   }
16584               }
16585
16586             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16587             unqualified_name
16588               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16589             qualifying_scope = parser->scope;
16590             if (abstract_ok)
16591               {
16592                 bool okay = false;
16593
16594                 if (!unqualified_name && pack_expansion_p)
16595                   {
16596                     /* Check whether an error occurred. */
16597                     okay = !cp_parser_error_occurred (parser);
16598
16599                     /* We already consumed the ellipsis to mark a
16600                        parameter pack, but we have no way to report it,
16601                        so abort the tentative parse. We will be exiting
16602                        immediately anyway. */
16603                     cp_parser_abort_tentative_parse (parser);
16604                   }
16605                 else
16606                   okay = cp_parser_parse_definitely (parser);
16607
16608                 if (!okay)
16609                   unqualified_name = error_mark_node;
16610                 else if (unqualified_name
16611                          && (qualifying_scope
16612                              || (TREE_CODE (unqualified_name)
16613                                  != IDENTIFIER_NODE)))
16614                   {
16615                     cp_parser_error (parser, "expected unqualified-id");
16616                     unqualified_name = error_mark_node;
16617                   }
16618               }
16619
16620             if (!unqualified_name)
16621               return NULL;
16622             if (unqualified_name == error_mark_node)
16623               {
16624                 declarator = cp_error_declarator;
16625                 pack_expansion_p = false;
16626                 declarator->parameter_pack_p = false;
16627                 break;
16628               }
16629
16630             attrs = cp_parser_std_attribute_spec_seq (parser);
16631
16632             if (qualifying_scope && at_namespace_scope_p ()
16633                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16634               {
16635                 /* In the declaration of a member of a template class
16636                    outside of the class itself, the SCOPE will sometimes
16637                    be a TYPENAME_TYPE.  For example, given:
16638
16639                    template <typename T>
16640                    int S<T>::R::i = 3;
16641
16642                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16643                    this context, we must resolve S<T>::R to an ordinary
16644                    type, rather than a typename type.
16645
16646                    The reason we normally avoid resolving TYPENAME_TYPEs
16647                    is that a specialization of `S' might render
16648                    `S<T>::R' not a type.  However, if `S' is
16649                    specialized, then this `i' will not be used, so there
16650                    is no harm in resolving the types here.  */
16651                 tree type;
16652
16653                 /* Resolve the TYPENAME_TYPE.  */
16654                 type = resolve_typename_type (qualifying_scope,
16655                                               /*only_current_p=*/false);
16656                 /* If that failed, the declarator is invalid.  */
16657                 if (TREE_CODE (type) == TYPENAME_TYPE)
16658                   {
16659                     if (typedef_variant_p (type))
16660                       error_at (declarator_id_start_token->location,
16661                                 "cannot define member of dependent typedef "
16662                                 "%qT", type);
16663                     else
16664                       error_at (declarator_id_start_token->location,
16665                                 "%<%T::%E%> is not a type",
16666                                 TYPE_CONTEXT (qualifying_scope),
16667                                 TYPE_IDENTIFIER (qualifying_scope));
16668                   }
16669                 qualifying_scope = type;
16670               }
16671
16672             sfk = sfk_none;
16673
16674             if (unqualified_name)
16675               {
16676                 tree class_type;
16677
16678                 if (qualifying_scope
16679                     && CLASS_TYPE_P (qualifying_scope))
16680                   class_type = qualifying_scope;
16681                 else
16682                   class_type = current_class_type;
16683
16684                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16685                   {
16686                     tree name_type = TREE_TYPE (unqualified_name);
16687                     if (class_type && same_type_p (name_type, class_type))
16688                       {
16689                         if (qualifying_scope
16690                             && CLASSTYPE_USE_TEMPLATE (name_type))
16691                           {
16692                             error_at (declarator_id_start_token->location,
16693                                       "invalid use of constructor as a template");
16694                             inform (declarator_id_start_token->location,
16695                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16696                                     "name the constructor in a qualified name",
16697                                     class_type,
16698                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16699                                     class_type, name_type);
16700                             declarator = cp_error_declarator;
16701                             break;
16702                           }
16703                         else
16704                           unqualified_name = constructor_name (class_type);
16705                       }
16706                     else
16707                       {
16708                         /* We do not attempt to print the declarator
16709                            here because we do not have enough
16710                            information about its original syntactic
16711                            form.  */
16712                         cp_parser_error (parser, "invalid declarator");
16713                         declarator = cp_error_declarator;
16714                         break;
16715                       }
16716                   }
16717
16718                 if (class_type)
16719                   {
16720                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16721                       sfk = sfk_destructor;
16722                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16723                       sfk = sfk_conversion;
16724                     else if (/* There's no way to declare a constructor
16725                                 for an anonymous type, even if the type
16726                                 got a name for linkage purposes.  */
16727                              !TYPE_WAS_ANONYMOUS (class_type)
16728                              && constructor_name_p (unqualified_name,
16729                                                     class_type))
16730                       {
16731                         unqualified_name = constructor_name (class_type);
16732                         sfk = sfk_constructor;
16733                       }
16734                     else if (is_overloaded_fn (unqualified_name)
16735                              && DECL_CONSTRUCTOR_P (get_first_fn
16736                                                     (unqualified_name)))
16737                       sfk = sfk_constructor;
16738
16739                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16740                       *ctor_dtor_or_conv_p = -1;
16741                   }
16742               }
16743             declarator = make_id_declarator (qualifying_scope,
16744                                              unqualified_name,
16745                                              sfk);
16746             declarator->std_attributes = attrs;
16747             declarator->id_loc = token->location;
16748             declarator->parameter_pack_p = pack_expansion_p;
16749
16750             if (pack_expansion_p)
16751               maybe_warn_variadic_templates ();
16752           }
16753
16754         handle_declarator:;
16755           scope = get_scope_of_declarator (declarator);
16756           if (scope)
16757             /* Any names that appear after the declarator-id for a
16758                member are looked up in the containing scope.  */
16759             pushed_scope = push_scope (scope);
16760           parser->in_declarator_p = true;
16761           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16762               || (declarator && declarator->kind == cdk_id))
16763             /* Default args are only allowed on function
16764                declarations.  */
16765             parser->default_arg_ok_p = saved_default_arg_ok_p;
16766           else
16767             parser->default_arg_ok_p = false;
16768
16769           first = false;
16770         }
16771       /* We're done.  */
16772       else
16773         break;
16774     }
16775
16776   /* For an abstract declarator, we might wind up with nothing at this
16777      point.  That's an error; the declarator is not optional.  */
16778   if (!declarator)
16779     cp_parser_error (parser, "expected declarator");
16780
16781   /* If we entered a scope, we must exit it now.  */
16782   if (pushed_scope)
16783     pop_scope (pushed_scope);
16784
16785   parser->default_arg_ok_p = saved_default_arg_ok_p;
16786   parser->in_declarator_p = saved_in_declarator_p;
16787
16788   return declarator;
16789 }
16790
16791 /* Parse a ptr-operator.
16792
16793    ptr-operator:
16794      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16795      * cv-qualifier-seq [opt]
16796      &
16797      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16798      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16799
16800    GNU Extension:
16801
16802    ptr-operator:
16803      & cv-qualifier-seq [opt]
16804
16805    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16806    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16807    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16808    filled in with the TYPE containing the member.  *CV_QUALS is
16809    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16810    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16811    Note that the tree codes returned by this function have nothing
16812    to do with the types of trees that will be eventually be created
16813    to represent the pointer or reference type being parsed. They are
16814    just constants with suggestive names. */
16815 static enum tree_code
16816 cp_parser_ptr_operator (cp_parser* parser,
16817                         tree* type,
16818                         cp_cv_quals *cv_quals,
16819                         tree *attributes)
16820 {
16821   enum tree_code code = ERROR_MARK;
16822   cp_token *token;
16823   tree attrs = NULL_TREE;
16824
16825   /* Assume that it's not a pointer-to-member.  */
16826   *type = NULL_TREE;
16827   /* And that there are no cv-qualifiers.  */
16828   *cv_quals = TYPE_UNQUALIFIED;
16829
16830   /* Peek at the next token.  */
16831   token = cp_lexer_peek_token (parser->lexer);
16832
16833   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16834   if (token->type == CPP_MULT)
16835     code = INDIRECT_REF;
16836   else if (token->type == CPP_AND)
16837     code = ADDR_EXPR;
16838   else if ((cxx_dialect != cxx98) &&
16839            token->type == CPP_AND_AND) /* C++0x only */
16840     code = NON_LVALUE_EXPR;
16841
16842   if (code != ERROR_MARK)
16843     {
16844       /* Consume the `*', `&' or `&&'.  */
16845       cp_lexer_consume_token (parser->lexer);
16846
16847       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16848          `&', if we are allowing GNU extensions.  (The only qualifier
16849          that can legally appear after `&' is `restrict', but that is
16850          enforced during semantic analysis.  */
16851       if (code == INDIRECT_REF
16852           || cp_parser_allow_gnu_extensions_p (parser))
16853         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16854
16855       attrs = cp_parser_std_attribute_spec_seq (parser);
16856       if (attributes != NULL)
16857         *attributes = attrs;
16858     }
16859   else
16860     {
16861       /* Try the pointer-to-member case.  */
16862       cp_parser_parse_tentatively (parser);
16863       /* Look for the optional `::' operator.  */
16864       cp_parser_global_scope_opt (parser,
16865                                   /*current_scope_valid_p=*/false);
16866       /* Look for the nested-name specifier.  */
16867       token = cp_lexer_peek_token (parser->lexer);
16868       cp_parser_nested_name_specifier (parser,
16869                                        /*typename_keyword_p=*/false,
16870                                        /*check_dependency_p=*/true,
16871                                        /*type_p=*/false,
16872                                        /*is_declaration=*/false);
16873       /* If we found it, and the next token is a `*', then we are
16874          indeed looking at a pointer-to-member operator.  */
16875       if (!cp_parser_error_occurred (parser)
16876           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16877         {
16878           /* Indicate that the `*' operator was used.  */
16879           code = INDIRECT_REF;
16880
16881           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16882             error_at (token->location, "%qD is a namespace", parser->scope);
16883           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16884             error_at (token->location, "cannot form pointer to member of "
16885                       "non-class %q#T", parser->scope);
16886           else
16887             {
16888               /* The type of which the member is a member is given by the
16889                  current SCOPE.  */
16890               *type = parser->scope;
16891               /* The next name will not be qualified.  */
16892               parser->scope = NULL_TREE;
16893               parser->qualifying_scope = NULL_TREE;
16894               parser->object_scope = NULL_TREE;
16895               /* Look for optional c++11 attributes.  */
16896               attrs = cp_parser_std_attribute_spec_seq (parser);
16897               if (attributes != NULL)
16898                 *attributes = attrs;
16899               /* Look for the optional cv-qualifier-seq.  */
16900               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16901             }
16902         }
16903       /* If that didn't work we don't have a ptr-operator.  */
16904       if (!cp_parser_parse_definitely (parser))
16905         cp_parser_error (parser, "expected ptr-operator");
16906     }
16907
16908   return code;
16909 }
16910
16911 /* Parse an (optional) cv-qualifier-seq.
16912
16913    cv-qualifier-seq:
16914      cv-qualifier cv-qualifier-seq [opt]
16915
16916    cv-qualifier:
16917      const
16918      volatile
16919
16920    GNU Extension:
16921
16922    cv-qualifier:
16923      __restrict__
16924
16925    Returns a bitmask representing the cv-qualifiers.  */
16926
16927 static cp_cv_quals
16928 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16929 {
16930   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16931
16932   while (true)
16933     {
16934       cp_token *token;
16935       cp_cv_quals cv_qualifier;
16936
16937       /* Peek at the next token.  */
16938       token = cp_lexer_peek_token (parser->lexer);
16939       /* See if it's a cv-qualifier.  */
16940       switch (token->keyword)
16941         {
16942         case RID_CONST:
16943           cv_qualifier = TYPE_QUAL_CONST;
16944           break;
16945
16946         case RID_VOLATILE:
16947           cv_qualifier = TYPE_QUAL_VOLATILE;
16948           break;
16949
16950         case RID_RESTRICT:
16951           cv_qualifier = TYPE_QUAL_RESTRICT;
16952           break;
16953
16954         default:
16955           cv_qualifier = TYPE_UNQUALIFIED;
16956           break;
16957         }
16958
16959       if (!cv_qualifier)
16960         break;
16961
16962       if (cv_quals & cv_qualifier)
16963         {
16964           error_at (token->location, "duplicate cv-qualifier");
16965           cp_lexer_purge_token (parser->lexer);
16966         }
16967       else
16968         {
16969           cp_lexer_consume_token (parser->lexer);
16970           cv_quals |= cv_qualifier;
16971         }
16972     }
16973
16974   return cv_quals;
16975 }
16976
16977 /* Parse an (optional) ref-qualifier
16978
16979    ref-qualifier:
16980      &
16981      &&
16982
16983    Returns cp_ref_qualifier representing ref-qualifier. */
16984
16985 static cp_ref_qualifier
16986 cp_parser_ref_qualifier_seq_opt (cp_parser* parser)
16987 {
16988   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
16989   cp_token *token = cp_lexer_peek_token (parser->lexer);
16990
16991   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
16992   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
16993     return ref_qual;
16994
16995   switch (token->type)
16996     {
16997     case CPP_AND:
16998       ref_qual = REF_QUAL_LVALUE;
16999       break;
17000     case CPP_AND_AND:
17001       ref_qual = REF_QUAL_RVALUE;
17002       break;
17003     }
17004
17005   if (ref_qual)
17006     {
17007       cp_lexer_consume_token (parser->lexer);
17008     }
17009
17010   return ref_qual;
17011 }
17012
17013 /* Parse an (optional) virt-specifier-seq.
17014
17015    virt-specifier-seq:
17016      virt-specifier virt-specifier-seq [opt]
17017
17018    virt-specifier:
17019      override
17020      final
17021
17022    Returns a bitmask representing the virt-specifiers.  */
17023
17024 static cp_virt_specifiers
17025 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17026 {
17027   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17028
17029   while (true)
17030     {
17031       cp_token *token;
17032       cp_virt_specifiers virt_specifier;
17033
17034       /* Peek at the next token.  */
17035       token = cp_lexer_peek_token (parser->lexer);
17036       /* See if it's a virt-specifier-qualifier.  */
17037       if (token->type != CPP_NAME)
17038         break;
17039       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17040         {
17041           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17042           virt_specifier = VIRT_SPEC_OVERRIDE;
17043         }
17044       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17045         {
17046           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17047           virt_specifier = VIRT_SPEC_FINAL;
17048         }
17049       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17050         {
17051           virt_specifier = VIRT_SPEC_FINAL;
17052         }
17053       else
17054         break;
17055
17056       if (virt_specifiers & virt_specifier)
17057         {
17058           error_at (token->location, "duplicate virt-specifier");
17059           cp_lexer_purge_token (parser->lexer);
17060         }
17061       else
17062         {
17063           cp_lexer_consume_token (parser->lexer);
17064           virt_specifiers |= virt_specifier;
17065         }
17066     }
17067   return virt_specifiers;
17068 }
17069
17070 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17071    is in scope even though it isn't real.  */
17072
17073 static void
17074 inject_this_parameter (tree ctype, cp_cv_quals quals)
17075 {
17076   tree this_parm;
17077
17078   if (current_class_ptr)
17079     {
17080       /* We don't clear this between NSDMIs.  Is it already what we want?  */
17081       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17082       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17083           && cp_type_quals (type) == quals)
17084         return;
17085     }
17086
17087   this_parm = build_this_parm (ctype, quals);
17088   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
17089   current_class_ptr = NULL_TREE;
17090   current_class_ref
17091     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17092   current_class_ptr = this_parm;
17093 }
17094
17095 /* Parse a late-specified return type, if any.  This is not a separate
17096    non-terminal, but part of a function declarator, which looks like
17097
17098    -> trailing-type-specifier-seq abstract-declarator(opt)
17099
17100    Returns the type indicated by the type-id.
17101
17102    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17103    function.  */
17104
17105 static tree
17106 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
17107 {
17108   cp_token *token;
17109   tree type;
17110
17111   /* Peek at the next token.  */
17112   token = cp_lexer_peek_token (parser->lexer);
17113   /* A late-specified return type is indicated by an initial '->'. */
17114   if (token->type != CPP_DEREF)
17115     return NULL_TREE;
17116
17117   /* Consume the ->.  */
17118   cp_lexer_consume_token (parser->lexer);
17119
17120   tree save_ccp = current_class_ptr;
17121   tree save_ccr = current_class_ref;
17122   if (quals >= 0)
17123     {
17124       /* DR 1207: 'this' is in scope in the trailing return type.  */
17125       inject_this_parameter (current_class_type, quals);
17126     }
17127
17128   type = cp_parser_trailing_type_id (parser);
17129
17130   if (quals >= 0)
17131     {
17132       current_class_ptr = save_ccp;
17133       current_class_ref = save_ccr;
17134     }
17135
17136   return type;
17137 }
17138
17139 /* Parse a declarator-id.
17140
17141    declarator-id:
17142      id-expression
17143      :: [opt] nested-name-specifier [opt] type-name
17144
17145    In the `id-expression' case, the value returned is as for
17146    cp_parser_id_expression if the id-expression was an unqualified-id.
17147    If the id-expression was a qualified-id, then a SCOPE_REF is
17148    returned.  The first operand is the scope (either a NAMESPACE_DECL
17149    or TREE_TYPE), but the second is still just a representation of an
17150    unqualified-id.  */
17151
17152 static tree
17153 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17154 {
17155   tree id;
17156   /* The expression must be an id-expression.  Assume that qualified
17157      names are the names of types so that:
17158
17159        template <class T>
17160        int S<T>::R::i = 3;
17161
17162      will work; we must treat `S<T>::R' as the name of a type.
17163      Similarly, assume that qualified names are templates, where
17164      required, so that:
17165
17166        template <class T>
17167        int S<T>::R<T>::i = 3;
17168
17169      will work, too.  */
17170   id = cp_parser_id_expression (parser,
17171                                 /*template_keyword_p=*/false,
17172                                 /*check_dependency_p=*/false,
17173                                 /*template_p=*/NULL,
17174                                 /*declarator_p=*/true,
17175                                 optional_p);
17176   if (id && BASELINK_P (id))
17177     id = BASELINK_FUNCTIONS (id);
17178   return id;
17179 }
17180
17181 /* Parse a type-id.
17182
17183    type-id:
17184      type-specifier-seq abstract-declarator [opt]
17185
17186    Returns the TYPE specified.  */
17187
17188 static tree
17189 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17190                      bool is_trailing_return)
17191 {
17192   cp_decl_specifier_seq type_specifier_seq;
17193   cp_declarator *abstract_declarator;
17194
17195   /* Parse the type-specifier-seq.  */
17196   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17197                                 is_trailing_return,
17198                                 &type_specifier_seq);
17199   if (type_specifier_seq.type == error_mark_node)
17200     return error_mark_node;
17201
17202   /* There might or might not be an abstract declarator.  */
17203   cp_parser_parse_tentatively (parser);
17204   /* Look for the declarator.  */
17205   abstract_declarator
17206     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17207                             /*parenthesized_p=*/NULL,
17208                             /*member_p=*/false);
17209   /* Check to see if there really was a declarator.  */
17210   if (!cp_parser_parse_definitely (parser))
17211     abstract_declarator = NULL;
17212
17213   if (type_specifier_seq.type
17214       && type_uses_auto (type_specifier_seq.type))
17215     {
17216       /* A type-id with type 'auto' is only ok if the abstract declarator
17217          is a function declarator with a late-specified return type.  */
17218       if (abstract_declarator
17219           && abstract_declarator->kind == cdk_function
17220           && abstract_declarator->u.function.late_return_type)
17221         /* OK */;
17222       else
17223         {
17224           error ("invalid use of %<auto%>");
17225           return error_mark_node;
17226         }
17227     }
17228   
17229   return groktypename (&type_specifier_seq, abstract_declarator,
17230                        is_template_arg);
17231 }
17232
17233 static tree cp_parser_type_id (cp_parser *parser)
17234 {
17235   return cp_parser_type_id_1 (parser, false, false);
17236 }
17237
17238 static tree cp_parser_template_type_arg (cp_parser *parser)
17239 {
17240   tree r;
17241   const char *saved_message = parser->type_definition_forbidden_message;
17242   parser->type_definition_forbidden_message
17243     = G_("types may not be defined in template arguments");
17244   r = cp_parser_type_id_1 (parser, true, false);
17245   parser->type_definition_forbidden_message = saved_message;
17246   return r;
17247 }
17248
17249 static tree cp_parser_trailing_type_id (cp_parser *parser)
17250 {
17251   return cp_parser_type_id_1 (parser, false, true);
17252 }
17253
17254 /* Parse a type-specifier-seq.
17255
17256    type-specifier-seq:
17257      type-specifier type-specifier-seq [opt]
17258
17259    GNU extension:
17260
17261    type-specifier-seq:
17262      attributes type-specifier-seq [opt]
17263
17264    If IS_DECLARATION is true, we are at the start of a "condition" or
17265    exception-declaration, so we might be followed by a declarator-id.
17266
17267    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17268    i.e. we've just seen "->".
17269
17270    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
17271
17272 static void
17273 cp_parser_type_specifier_seq (cp_parser* parser,
17274                               bool is_declaration,
17275                               bool is_trailing_return,
17276                               cp_decl_specifier_seq *type_specifier_seq)
17277 {
17278   bool seen_type_specifier = false;
17279   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17280   cp_token *start_token = NULL;
17281
17282   /* Clear the TYPE_SPECIFIER_SEQ.  */
17283   clear_decl_specs (type_specifier_seq);
17284
17285   /* In the context of a trailing return type, enum E { } is an
17286      elaborated-type-specifier followed by a function-body, not an
17287      enum-specifier.  */
17288   if (is_trailing_return)
17289     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17290
17291   /* Parse the type-specifiers and attributes.  */
17292   while (true)
17293     {
17294       tree type_specifier;
17295       bool is_cv_qualifier;
17296
17297       /* Check for attributes first.  */
17298       if (cp_next_tokens_can_be_attribute_p (parser))
17299         {
17300           type_specifier_seq->attributes =
17301             chainon (type_specifier_seq->attributes,
17302                      cp_parser_attributes_opt (parser));
17303           continue;
17304         }
17305
17306       /* record the token of the beginning of the type specifier seq,
17307          for error reporting purposes*/
17308      if (!start_token)
17309        start_token = cp_lexer_peek_token (parser->lexer);
17310
17311       /* Look for the type-specifier.  */
17312       type_specifier = cp_parser_type_specifier (parser,
17313                                                  flags,
17314                                                  type_specifier_seq,
17315                                                  /*is_declaration=*/false,
17316                                                  NULL,
17317                                                  &is_cv_qualifier);
17318       if (!type_specifier)
17319         {
17320           /* If the first type-specifier could not be found, this is not a
17321              type-specifier-seq at all.  */
17322           if (!seen_type_specifier)
17323             {
17324               cp_parser_error (parser, "expected type-specifier");
17325               type_specifier_seq->type = error_mark_node;
17326               return;
17327             }
17328           /* If subsequent type-specifiers could not be found, the
17329              type-specifier-seq is complete.  */
17330           break;
17331         }
17332
17333       seen_type_specifier = true;
17334       /* The standard says that a condition can be:
17335
17336             type-specifier-seq declarator = assignment-expression
17337
17338          However, given:
17339
17340            struct S {};
17341            if (int S = ...)
17342
17343          we should treat the "S" as a declarator, not as a
17344          type-specifier.  The standard doesn't say that explicitly for
17345          type-specifier-seq, but it does say that for
17346          decl-specifier-seq in an ordinary declaration.  Perhaps it
17347          would be clearer just to allow a decl-specifier-seq here, and
17348          then add a semantic restriction that if any decl-specifiers
17349          that are not type-specifiers appear, the program is invalid.  */
17350       if (is_declaration && !is_cv_qualifier)
17351         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17352     }
17353 }
17354
17355 /* Parse a parameter-declaration-clause.
17356
17357    parameter-declaration-clause:
17358      parameter-declaration-list [opt] ... [opt]
17359      parameter-declaration-list , ...
17360
17361    Returns a representation for the parameter declarations.  A return
17362    value of NULL indicates a parameter-declaration-clause consisting
17363    only of an ellipsis.  */
17364
17365 static tree
17366 cp_parser_parameter_declaration_clause (cp_parser* parser)
17367 {
17368   tree parameters;
17369   cp_token *token;
17370   bool ellipsis_p;
17371   bool is_error;
17372
17373   /* Peek at the next token.  */
17374   token = cp_lexer_peek_token (parser->lexer);
17375   /* Check for trivial parameter-declaration-clauses.  */
17376   if (token->type == CPP_ELLIPSIS)
17377     {
17378       /* Consume the `...' token.  */
17379       cp_lexer_consume_token (parser->lexer);
17380       return NULL_TREE;
17381     }
17382   else if (token->type == CPP_CLOSE_PAREN)
17383     /* There are no parameters.  */
17384     {
17385 #ifndef NO_IMPLICIT_EXTERN_C
17386       if (in_system_header && current_class_type == NULL
17387           && current_lang_name == lang_name_c)
17388         return NULL_TREE;
17389       else
17390 #endif
17391         return void_list_node;
17392     }
17393   /* Check for `(void)', too, which is a special case.  */
17394   else if (token->keyword == RID_VOID
17395            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17396                == CPP_CLOSE_PAREN))
17397     {
17398       /* Consume the `void' token.  */
17399       cp_lexer_consume_token (parser->lexer);
17400       /* There are no parameters.  */
17401       return void_list_node;
17402     }
17403
17404   /* Parse the parameter-declaration-list.  */
17405   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17406   /* If a parse error occurred while parsing the
17407      parameter-declaration-list, then the entire
17408      parameter-declaration-clause is erroneous.  */
17409   if (is_error)
17410     return NULL;
17411
17412   /* Peek at the next token.  */
17413   token = cp_lexer_peek_token (parser->lexer);
17414   /* If it's a `,', the clause should terminate with an ellipsis.  */
17415   if (token->type == CPP_COMMA)
17416     {
17417       /* Consume the `,'.  */
17418       cp_lexer_consume_token (parser->lexer);
17419       /* Expect an ellipsis.  */
17420       ellipsis_p
17421         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17422     }
17423   /* It might also be `...' if the optional trailing `,' was
17424      omitted.  */
17425   else if (token->type == CPP_ELLIPSIS)
17426     {
17427       /* Consume the `...' token.  */
17428       cp_lexer_consume_token (parser->lexer);
17429       /* And remember that we saw it.  */
17430       ellipsis_p = true;
17431     }
17432   else
17433     ellipsis_p = false;
17434
17435   /* Finish the parameter list.  */
17436   if (!ellipsis_p)
17437     parameters = chainon (parameters, void_list_node);
17438
17439   return parameters;
17440 }
17441
17442 /* Parse a parameter-declaration-list.
17443
17444    parameter-declaration-list:
17445      parameter-declaration
17446      parameter-declaration-list , parameter-declaration
17447
17448    Returns a representation of the parameter-declaration-list, as for
17449    cp_parser_parameter_declaration_clause.  However, the
17450    `void_list_node' is never appended to the list.  Upon return,
17451    *IS_ERROR will be true iff an error occurred.  */
17452
17453 static tree
17454 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17455 {
17456   tree parameters = NULL_TREE;
17457   tree *tail = &parameters; 
17458   bool saved_in_unbraced_linkage_specification_p;
17459   int index = 0;
17460
17461   /* Assume all will go well.  */
17462   *is_error = false;
17463   /* The special considerations that apply to a function within an
17464      unbraced linkage specifications do not apply to the parameters
17465      to the function.  */
17466   saved_in_unbraced_linkage_specification_p 
17467     = parser->in_unbraced_linkage_specification_p;
17468   parser->in_unbraced_linkage_specification_p = false;
17469
17470   /* Look for more parameters.  */
17471   while (true)
17472     {
17473       cp_parameter_declarator *parameter;
17474       tree decl = error_mark_node;
17475       bool parenthesized_p = false;
17476       /* Parse the parameter.  */
17477       parameter
17478         = cp_parser_parameter_declaration (parser,
17479                                            /*template_parm_p=*/false,
17480                                            &parenthesized_p);
17481
17482       /* We don't know yet if the enclosing context is deprecated, so wait
17483          and warn in grokparms if appropriate.  */
17484       deprecated_state = DEPRECATED_SUPPRESS;
17485
17486       if (parameter)
17487         decl = grokdeclarator (parameter->declarator,
17488                                &parameter->decl_specifiers,
17489                                PARM,
17490                                parameter->default_argument != NULL_TREE,
17491                                &parameter->decl_specifiers.attributes);
17492
17493       deprecated_state = DEPRECATED_NORMAL;
17494
17495       /* If a parse error occurred parsing the parameter declaration,
17496          then the entire parameter-declaration-list is erroneous.  */
17497       if (decl == error_mark_node)
17498         {
17499           *is_error = true;
17500           parameters = error_mark_node;
17501           break;
17502         }
17503
17504       if (parameter->decl_specifiers.attributes)
17505         cplus_decl_attributes (&decl,
17506                                parameter->decl_specifiers.attributes,
17507                                0);
17508       if (DECL_NAME (decl))
17509         decl = pushdecl (decl);
17510
17511       if (decl != error_mark_node)
17512         {
17513           retrofit_lang_decl (decl);
17514           DECL_PARM_INDEX (decl) = ++index;
17515           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17516         }
17517
17518       /* Add the new parameter to the list.  */
17519       *tail = build_tree_list (parameter->default_argument, decl);
17520       tail = &TREE_CHAIN (*tail);
17521
17522       /* Peek at the next token.  */
17523       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17524           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17525           /* These are for Objective-C++ */
17526           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17527           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17528         /* The parameter-declaration-list is complete.  */
17529         break;
17530       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17531         {
17532           cp_token *token;
17533
17534           /* Peek at the next token.  */
17535           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17536           /* If it's an ellipsis, then the list is complete.  */
17537           if (token->type == CPP_ELLIPSIS)
17538             break;
17539           /* Otherwise, there must be more parameters.  Consume the
17540              `,'.  */
17541           cp_lexer_consume_token (parser->lexer);
17542           /* When parsing something like:
17543
17544                 int i(float f, double d)
17545
17546              we can tell after seeing the declaration for "f" that we
17547              are not looking at an initialization of a variable "i",
17548              but rather at the declaration of a function "i".
17549
17550              Due to the fact that the parsing of template arguments
17551              (as specified to a template-id) requires backtracking we
17552              cannot use this technique when inside a template argument
17553              list.  */
17554           if (!parser->in_template_argument_list_p
17555               && !parser->in_type_id_in_expr_p
17556               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17557               /* However, a parameter-declaration of the form
17558                  "foat(f)" (which is a valid declaration of a
17559                  parameter "f") can also be interpreted as an
17560                  expression (the conversion of "f" to "float").  */
17561               && !parenthesized_p)
17562             cp_parser_commit_to_tentative_parse (parser);
17563         }
17564       else
17565         {
17566           cp_parser_error (parser, "expected %<,%> or %<...%>");
17567           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17568             cp_parser_skip_to_closing_parenthesis (parser,
17569                                                    /*recovering=*/true,
17570                                                    /*or_comma=*/false,
17571                                                    /*consume_paren=*/false);
17572           break;
17573         }
17574     }
17575
17576   parser->in_unbraced_linkage_specification_p
17577     = saved_in_unbraced_linkage_specification_p;
17578
17579   return parameters;
17580 }
17581
17582 /* Parse a parameter declaration.
17583
17584    parameter-declaration:
17585      decl-specifier-seq ... [opt] declarator
17586      decl-specifier-seq declarator = assignment-expression
17587      decl-specifier-seq ... [opt] abstract-declarator [opt]
17588      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17589
17590    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17591    declares a template parameter.  (In that case, a non-nested `>'
17592    token encountered during the parsing of the assignment-expression
17593    is not interpreted as a greater-than operator.)
17594
17595    Returns a representation of the parameter, or NULL if an error
17596    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17597    true iff the declarator is of the form "(p)".  */
17598
17599 static cp_parameter_declarator *
17600 cp_parser_parameter_declaration (cp_parser *parser,
17601                                  bool template_parm_p,
17602                                  bool *parenthesized_p)
17603 {
17604   int declares_class_or_enum;
17605   cp_decl_specifier_seq decl_specifiers;
17606   cp_declarator *declarator;
17607   tree default_argument;
17608   cp_token *token = NULL, *declarator_token_start = NULL;
17609   const char *saved_message;
17610
17611   /* In a template parameter, `>' is not an operator.
17612
17613      [temp.param]
17614
17615      When parsing a default template-argument for a non-type
17616      template-parameter, the first non-nested `>' is taken as the end
17617      of the template parameter-list rather than a greater-than
17618      operator.  */
17619
17620   /* Type definitions may not appear in parameter types.  */
17621   saved_message = parser->type_definition_forbidden_message;
17622   parser->type_definition_forbidden_message
17623     = G_("types may not be defined in parameter types");
17624
17625   /* Parse the declaration-specifiers.  */
17626   cp_parser_decl_specifier_seq (parser,
17627                                 CP_PARSER_FLAGS_NONE,
17628                                 &decl_specifiers,
17629                                 &declares_class_or_enum);
17630
17631   /* Complain about missing 'typename' or other invalid type names.  */
17632   if (!decl_specifiers.any_type_specifiers_p)
17633     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17634
17635   /* If an error occurred, there's no reason to attempt to parse the
17636      rest of the declaration.  */
17637   if (cp_parser_error_occurred (parser))
17638     {
17639       parser->type_definition_forbidden_message = saved_message;
17640       return NULL;
17641     }
17642
17643   /* Peek at the next token.  */
17644   token = cp_lexer_peek_token (parser->lexer);
17645
17646   /* If the next token is a `)', `,', `=', `>', or `...', then there
17647      is no declarator. However, when variadic templates are enabled,
17648      there may be a declarator following `...'.  */
17649   if (token->type == CPP_CLOSE_PAREN
17650       || token->type == CPP_COMMA
17651       || token->type == CPP_EQ
17652       || token->type == CPP_GREATER)
17653     {
17654       declarator = NULL;
17655       if (parenthesized_p)
17656         *parenthesized_p = false;
17657     }
17658   /* Otherwise, there should be a declarator.  */
17659   else
17660     {
17661       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17662       parser->default_arg_ok_p = false;
17663
17664       /* After seeing a decl-specifier-seq, if the next token is not a
17665          "(", there is no possibility that the code is a valid
17666          expression.  Therefore, if parsing tentatively, we commit at
17667          this point.  */
17668       if (!parser->in_template_argument_list_p
17669           /* In an expression context, having seen:
17670
17671                (int((char ...
17672
17673              we cannot be sure whether we are looking at a
17674              function-type (taking a "char" as a parameter) or a cast
17675              of some object of type "char" to "int".  */
17676           && !parser->in_type_id_in_expr_p
17677           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17678           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17679           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17680         cp_parser_commit_to_tentative_parse (parser);
17681       /* Parse the declarator.  */
17682       declarator_token_start = token;
17683       declarator = cp_parser_declarator (parser,
17684                                          CP_PARSER_DECLARATOR_EITHER,
17685                                          /*ctor_dtor_or_conv_p=*/NULL,
17686                                          parenthesized_p,
17687                                          /*member_p=*/false);
17688       parser->default_arg_ok_p = saved_default_arg_ok_p;
17689       /* After the declarator, allow more attributes.  */
17690       decl_specifiers.attributes
17691         = chainon (decl_specifiers.attributes,
17692                    cp_parser_attributes_opt (parser));
17693     }
17694
17695   /* If the next token is an ellipsis, and we have not seen a
17696      declarator name, and the type of the declarator contains parameter
17697      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17698      a parameter pack expansion expression. Otherwise, leave the
17699      ellipsis for a C-style variadic function. */
17700   token = cp_lexer_peek_token (parser->lexer);
17701   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17702     {
17703       tree type = decl_specifiers.type;
17704
17705       if (type && DECL_P (type))
17706         type = TREE_TYPE (type);
17707
17708       if (type
17709           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17710           && declarator_can_be_parameter_pack (declarator)
17711           && (!declarator || !declarator->parameter_pack_p)
17712           && uses_parameter_packs (type))
17713         {
17714           /* Consume the `...'. */
17715           cp_lexer_consume_token (parser->lexer);
17716           maybe_warn_variadic_templates ();
17717           
17718           /* Build a pack expansion type */
17719           if (declarator)
17720             declarator->parameter_pack_p = true;
17721           else
17722             decl_specifiers.type = make_pack_expansion (type);
17723         }
17724     }
17725
17726   /* The restriction on defining new types applies only to the type
17727      of the parameter, not to the default argument.  */
17728   parser->type_definition_forbidden_message = saved_message;
17729
17730   /* If the next token is `=', then process a default argument.  */
17731   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17732     {
17733       token = cp_lexer_peek_token (parser->lexer);
17734       /* If we are defining a class, then the tokens that make up the
17735          default argument must be saved and processed later.  */
17736       if (!template_parm_p && at_class_scope_p ()
17737           && TYPE_BEING_DEFINED (current_class_type)
17738           && !LAMBDA_TYPE_P (current_class_type))
17739         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17740       /* Outside of a class definition, we can just parse the
17741          assignment-expression.  */
17742       else
17743         default_argument
17744           = cp_parser_default_argument (parser, template_parm_p);
17745
17746       if (!parser->default_arg_ok_p)
17747         {
17748           if (flag_permissive)
17749             warning (0, "deprecated use of default argument for parameter of non-function");
17750           else
17751             {
17752               error_at (token->location,
17753                         "default arguments are only "
17754                         "permitted for function parameters");
17755               default_argument = NULL_TREE;
17756             }
17757         }
17758       else if ((declarator && declarator->parameter_pack_p)
17759                || (decl_specifiers.type
17760                    && PACK_EXPANSION_P (decl_specifiers.type)))
17761         {
17762           /* Find the name of the parameter pack.  */     
17763           cp_declarator *id_declarator = declarator;
17764           while (id_declarator && id_declarator->kind != cdk_id)
17765             id_declarator = id_declarator->declarator;
17766           
17767           if (id_declarator && id_declarator->kind == cdk_id)
17768             error_at (declarator_token_start->location,
17769                       template_parm_p
17770                       ? G_("template parameter pack %qD "
17771                            "cannot have a default argument")
17772                       : G_("parameter pack %qD cannot have "
17773                            "a default argument"),
17774                       id_declarator->u.id.unqualified_name);
17775           else
17776             error_at (declarator_token_start->location,
17777                       template_parm_p
17778                       ? G_("template parameter pack cannot have "
17779                            "a default argument")
17780                       : G_("parameter pack cannot have a "
17781                            "default argument"));
17782
17783           default_argument = NULL_TREE;
17784         }
17785     }
17786   else
17787     default_argument = NULL_TREE;
17788
17789   return make_parameter_declarator (&decl_specifiers,
17790                                     declarator,
17791                                     default_argument);
17792 }
17793
17794 /* Parse a default argument and return it.
17795
17796    TEMPLATE_PARM_P is true if this is a default argument for a
17797    non-type template parameter.  */
17798 static tree
17799 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17800 {
17801   tree default_argument = NULL_TREE;
17802   bool saved_greater_than_is_operator_p;
17803   bool saved_local_variables_forbidden_p;
17804   bool non_constant_p, is_direct_init;
17805
17806   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17807      set correctly.  */
17808   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17809   parser->greater_than_is_operator_p = !template_parm_p;
17810   /* Local variable names (and the `this' keyword) may not
17811      appear in a default argument.  */
17812   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17813   parser->local_variables_forbidden_p = true;
17814   /* Parse the assignment-expression.  */
17815   if (template_parm_p)
17816     push_deferring_access_checks (dk_no_deferred);
17817   default_argument
17818     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17819   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17820     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17821   if (template_parm_p)
17822     pop_deferring_access_checks ();
17823   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17824   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17825
17826   return default_argument;
17827 }
17828
17829 /* Parse a function-body.
17830
17831    function-body:
17832      compound_statement  */
17833
17834 static void
17835 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17836 {
17837   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17838 }
17839
17840 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17841    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
17842    is true we are parsing a function-try-block.  */
17843
17844 static bool
17845 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17846                                                   bool in_function_try_block)
17847 {
17848   tree body, list;
17849   bool ctor_initializer_p;
17850   const bool check_body_p =
17851      DECL_CONSTRUCTOR_P (current_function_decl)
17852      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17853   tree last = NULL;
17854
17855   /* Begin the function body.  */
17856   body = begin_function_body ();
17857   /* Parse the optional ctor-initializer.  */
17858   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17859
17860   /* If we're parsing a constexpr constructor definition, we need
17861      to check that the constructor body is indeed empty.  However,
17862      before we get to cp_parser_function_body lot of junk has been
17863      generated, so we can't just check that we have an empty block.
17864      Rather we take a snapshot of the outermost block, and check whether
17865      cp_parser_function_body changed its state.  */
17866   if (check_body_p)
17867     {
17868       list = cur_stmt_list;
17869       if (STATEMENT_LIST_TAIL (list))
17870         last = STATEMENT_LIST_TAIL (list)->stmt;
17871     }
17872   /* Parse the function-body.  */
17873   cp_parser_function_body (parser, in_function_try_block);
17874   if (check_body_p)
17875     check_constexpr_ctor_body (last, list);
17876   /* Finish the function body.  */
17877   finish_function_body (body);
17878
17879   return ctor_initializer_p;
17880 }
17881
17882 /* Parse an initializer.
17883
17884    initializer:
17885      = initializer-clause
17886      ( expression-list )
17887
17888    Returns an expression representing the initializer.  If no
17889    initializer is present, NULL_TREE is returned.
17890
17891    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17892    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17893    set to TRUE if there is no initializer present.  If there is an
17894    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17895    is set to true; otherwise it is set to false.  */
17896
17897 static tree
17898 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17899                        bool* non_constant_p)
17900 {
17901   cp_token *token;
17902   tree init;
17903
17904   /* Peek at the next token.  */
17905   token = cp_lexer_peek_token (parser->lexer);
17906
17907   /* Let our caller know whether or not this initializer was
17908      parenthesized.  */
17909   *is_direct_init = (token->type != CPP_EQ);
17910   /* Assume that the initializer is constant.  */
17911   *non_constant_p = false;
17912
17913   if (token->type == CPP_EQ)
17914     {
17915       /* Consume the `='.  */
17916       cp_lexer_consume_token (parser->lexer);
17917       /* Parse the initializer-clause.  */
17918       init = cp_parser_initializer_clause (parser, non_constant_p);
17919     }
17920   else if (token->type == CPP_OPEN_PAREN)
17921     {
17922       vec<tree, va_gc> *vec;
17923       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17924                                                      /*cast_p=*/false,
17925                                                      /*allow_expansion_p=*/true,
17926                                                      non_constant_p);
17927       if (vec == NULL)
17928         return error_mark_node;
17929       init = build_tree_list_vec (vec);
17930       release_tree_vector (vec);
17931     }
17932   else if (token->type == CPP_OPEN_BRACE)
17933     {
17934       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17935       init = cp_parser_braced_list (parser, non_constant_p);
17936       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17937     }
17938   else
17939     {
17940       /* Anything else is an error.  */
17941       cp_parser_error (parser, "expected initializer");
17942       init = error_mark_node;
17943     }
17944
17945   return init;
17946 }
17947
17948 /* Parse an initializer-clause.
17949
17950    initializer-clause:
17951      assignment-expression
17952      braced-init-list
17953
17954    Returns an expression representing the initializer.
17955
17956    If the `assignment-expression' production is used the value
17957    returned is simply a representation for the expression.
17958
17959    Otherwise, calls cp_parser_braced_list.  */
17960
17961 static tree
17962 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17963 {
17964   tree initializer;
17965
17966   /* Assume the expression is constant.  */
17967   *non_constant_p = false;
17968
17969   /* If it is not a `{', then we are looking at an
17970      assignment-expression.  */
17971   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17972     {
17973       initializer
17974         = cp_parser_constant_expression (parser,
17975                                         /*allow_non_constant_p=*/true,
17976                                         non_constant_p);
17977     }
17978   else
17979     initializer = cp_parser_braced_list (parser, non_constant_p);
17980
17981   return initializer;
17982 }
17983
17984 /* Parse a brace-enclosed initializer list.
17985
17986    braced-init-list:
17987      { initializer-list , [opt] }
17988      { }
17989
17990    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17991    the elements of the initializer-list (or NULL, if the last
17992    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17993    NULL_TREE.  There is no way to detect whether or not the optional
17994    trailing `,' was provided.  NON_CONSTANT_P is as for
17995    cp_parser_initializer.  */     
17996
17997 static tree
17998 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17999 {
18000   tree initializer;
18001
18002   /* Consume the `{' token.  */
18003   cp_lexer_consume_token (parser->lexer);
18004   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
18005   initializer = make_node (CONSTRUCTOR);
18006   /* If it's not a `}', then there is a non-trivial initializer.  */
18007   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18008     {
18009       /* Parse the initializer list.  */
18010       CONSTRUCTOR_ELTS (initializer)
18011         = cp_parser_initializer_list (parser, non_constant_p);
18012       /* A trailing `,' token is allowed.  */
18013       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18014         cp_lexer_consume_token (parser->lexer);
18015     }
18016   else
18017     *non_constant_p = false;
18018   /* Now, there should be a trailing `}'.  */
18019   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18020   TREE_TYPE (initializer) = init_list_type_node;
18021   return initializer;
18022 }
18023
18024 /* Parse an initializer-list.
18025
18026    initializer-list:
18027      initializer-clause ... [opt]
18028      initializer-list , initializer-clause ... [opt]
18029
18030    GNU Extension:
18031
18032    initializer-list:
18033      designation initializer-clause ...[opt]
18034      initializer-list , designation initializer-clause ...[opt]
18035
18036    designation:
18037      . identifier =
18038      identifier :
18039      [ constant-expression ] =
18040
18041    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
18042    for the initializer.  If the INDEX of the elt is non-NULL, it is the
18043    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
18044    as for cp_parser_initializer.  */
18045
18046 static vec<constructor_elt, va_gc> *
18047 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18048 {
18049   vec<constructor_elt, va_gc> *v = NULL;
18050
18051   /* Assume all of the expressions are constant.  */
18052   *non_constant_p = false;
18053
18054   /* Parse the rest of the list.  */
18055   while (true)
18056     {
18057       cp_token *token;
18058       tree designator;
18059       tree initializer;
18060       bool clause_non_constant_p;
18061
18062       /* If the next token is an identifier and the following one is a
18063          colon, we are looking at the GNU designated-initializer
18064          syntax.  */
18065       if (cp_parser_allow_gnu_extensions_p (parser)
18066           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18067           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18068         {
18069           /* Warn the user that they are using an extension.  */
18070           pedwarn (input_location, OPT_Wpedantic, 
18071                    "ISO C++ does not allow designated initializers");
18072           /* Consume the identifier.  */
18073           designator = cp_lexer_consume_token (parser->lexer)->u.value;
18074           /* Consume the `:'.  */
18075           cp_lexer_consume_token (parser->lexer);
18076         }
18077       /* Also handle the C99 syntax, '. id ='.  */
18078       else if (cp_parser_allow_gnu_extensions_p (parser)
18079                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18080                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18081                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18082         {
18083           /* Warn the user that they are using an extension.  */
18084           pedwarn (input_location, OPT_Wpedantic,
18085                    "ISO C++ does not allow C99 designated initializers");
18086           /* Consume the `.'.  */
18087           cp_lexer_consume_token (parser->lexer);
18088           /* Consume the identifier.  */
18089           designator = cp_lexer_consume_token (parser->lexer)->u.value;
18090           /* Consume the `='.  */
18091           cp_lexer_consume_token (parser->lexer);
18092         }
18093       /* Also handle C99 array designators, '[ const ] ='.  */
18094       else if (cp_parser_allow_gnu_extensions_p (parser)
18095                && !c_dialect_objc ()
18096                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18097         {
18098           /* In C++11, [ could start a lambda-introducer.  */
18099           bool non_const = false;
18100
18101           cp_parser_parse_tentatively (parser);
18102           cp_lexer_consume_token (parser->lexer);
18103           designator = cp_parser_constant_expression (parser, true, &non_const);
18104           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18105           cp_parser_require (parser, CPP_EQ, RT_EQ);
18106           if (!cp_parser_parse_definitely (parser))
18107             designator = NULL_TREE;
18108           else if (non_const)
18109             require_potential_rvalue_constant_expression (designator);
18110         }
18111       else
18112         designator = NULL_TREE;
18113
18114       /* Parse the initializer.  */
18115       initializer = cp_parser_initializer_clause (parser,
18116                                                   &clause_non_constant_p);
18117       /* If any clause is non-constant, so is the entire initializer.  */
18118       if (clause_non_constant_p)
18119         *non_constant_p = true;
18120
18121       /* If we have an ellipsis, this is an initializer pack
18122          expansion.  */
18123       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18124         {
18125           /* Consume the `...'.  */
18126           cp_lexer_consume_token (parser->lexer);
18127
18128           /* Turn the initializer into an initializer expansion.  */
18129           initializer = make_pack_expansion (initializer);
18130         }
18131
18132       /* Add it to the vector.  */
18133       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18134
18135       /* If the next token is not a comma, we have reached the end of
18136          the list.  */
18137       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18138         break;
18139
18140       /* Peek at the next token.  */
18141       token = cp_lexer_peek_nth_token (parser->lexer, 2);
18142       /* If the next token is a `}', then we're still done.  An
18143          initializer-clause can have a trailing `,' after the
18144          initializer-list and before the closing `}'.  */
18145       if (token->type == CPP_CLOSE_BRACE)
18146         break;
18147
18148       /* Consume the `,' token.  */
18149       cp_lexer_consume_token (parser->lexer);
18150     }
18151
18152   return v;
18153 }
18154
18155 /* Classes [gram.class] */
18156
18157 /* Parse a class-name.
18158
18159    class-name:
18160      identifier
18161      template-id
18162
18163    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18164    to indicate that names looked up in dependent types should be
18165    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
18166    keyword has been used to indicate that the name that appears next
18167    is a template.  TAG_TYPE indicates the explicit tag given before
18168    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
18169    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
18170    is the class being defined in a class-head.
18171
18172    Returns the TYPE_DECL representing the class.  */
18173
18174 static tree
18175 cp_parser_class_name (cp_parser *parser,
18176                       bool typename_keyword_p,
18177                       bool template_keyword_p,
18178                       enum tag_types tag_type,
18179                       bool check_dependency_p,
18180                       bool class_head_p,
18181                       bool is_declaration)
18182 {
18183   tree decl;
18184   tree scope;
18185   bool typename_p;
18186   cp_token *token;
18187   tree identifier = NULL_TREE;
18188
18189   /* All class-names start with an identifier.  */
18190   token = cp_lexer_peek_token (parser->lexer);
18191   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18192     {
18193       cp_parser_error (parser, "expected class-name");
18194       return error_mark_node;
18195     }
18196
18197   /* PARSER->SCOPE can be cleared when parsing the template-arguments
18198      to a template-id, so we save it here.  */
18199   scope = parser->scope;
18200   if (scope == error_mark_node)
18201     return error_mark_node;
18202
18203   /* Any name names a type if we're following the `typename' keyword
18204      in a qualified name where the enclosing scope is type-dependent.  */
18205   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18206                 && dependent_type_p (scope));
18207   /* Handle the common case (an identifier, but not a template-id)
18208      efficiently.  */
18209   if (token->type == CPP_NAME
18210       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18211     {
18212       cp_token *identifier_token;
18213       bool ambiguous_p;
18214
18215       /* Look for the identifier.  */
18216       identifier_token = cp_lexer_peek_token (parser->lexer);
18217       ambiguous_p = identifier_token->ambiguous_p;
18218       identifier = cp_parser_identifier (parser);
18219       /* If the next token isn't an identifier, we are certainly not
18220          looking at a class-name.  */
18221       if (identifier == error_mark_node)
18222         decl = error_mark_node;
18223       /* If we know this is a type-name, there's no need to look it
18224          up.  */
18225       else if (typename_p)
18226         decl = identifier;
18227       else
18228         {
18229           tree ambiguous_decls;
18230           /* If we already know that this lookup is ambiguous, then
18231              we've already issued an error message; there's no reason
18232              to check again.  */
18233           if (ambiguous_p)
18234             {
18235               cp_parser_simulate_error (parser);
18236               return error_mark_node;
18237             }
18238           /* If the next token is a `::', then the name must be a type
18239              name.
18240
18241              [basic.lookup.qual]
18242
18243              During the lookup for a name preceding the :: scope
18244              resolution operator, object, function, and enumerator
18245              names are ignored.  */
18246           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18247             tag_type = typename_type;
18248           /* Look up the name.  */
18249           decl = cp_parser_lookup_name (parser, identifier,
18250                                         tag_type,
18251                                         /*is_template=*/false,
18252                                         /*is_namespace=*/false,
18253                                         check_dependency_p,
18254                                         &ambiguous_decls,
18255                                         identifier_token->location);
18256           if (ambiguous_decls)
18257             {
18258               if (cp_parser_parsing_tentatively (parser))
18259                 cp_parser_simulate_error (parser);
18260               return error_mark_node;
18261             }
18262         }
18263     }
18264   else
18265     {
18266       /* Try a template-id.  */
18267       decl = cp_parser_template_id (parser, template_keyword_p,
18268                                     check_dependency_p,
18269                                     tag_type,
18270                                     is_declaration);
18271       if (decl == error_mark_node)
18272         return error_mark_node;
18273     }
18274
18275   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18276
18277   /* If this is a typename, create a TYPENAME_TYPE.  */
18278   if (typename_p && decl != error_mark_node)
18279     {
18280       decl = make_typename_type (scope, decl, typename_type,
18281                                  /*complain=*/tf_error);
18282       if (decl != error_mark_node)
18283         decl = TYPE_NAME (decl);
18284     }
18285
18286   decl = strip_using_decl (decl);
18287
18288   /* Check to see that it is really the name of a class.  */
18289   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18290       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18291       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18292     /* Situations like this:
18293
18294          template <typename T> struct A {
18295            typename T::template X<int>::I i;
18296          };
18297
18298        are problematic.  Is `T::template X<int>' a class-name?  The
18299        standard does not seem to be definitive, but there is no other
18300        valid interpretation of the following `::'.  Therefore, those
18301        names are considered class-names.  */
18302     {
18303       decl = make_typename_type (scope, decl, tag_type, tf_error);
18304       if (decl != error_mark_node)
18305         decl = TYPE_NAME (decl);
18306     }
18307   else if (TREE_CODE (decl) != TYPE_DECL
18308            || TREE_TYPE (decl) == error_mark_node
18309            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18310            /* In Objective-C 2.0, a classname followed by '.' starts a
18311               dot-syntax expression, and it's not a type-name.  */
18312            || (c_dialect_objc ()
18313                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
18314                && objc_is_class_name (decl)))
18315     decl = error_mark_node;
18316
18317   if (decl == error_mark_node)
18318     cp_parser_error (parser, "expected class-name");
18319   else if (identifier && !parser->scope)
18320     maybe_note_name_used_in_class (identifier, decl);
18321
18322   return decl;
18323 }
18324
18325 /* Parse a class-specifier.
18326
18327    class-specifier:
18328      class-head { member-specification [opt] }
18329
18330    Returns the TREE_TYPE representing the class.  */
18331
18332 static tree
18333 cp_parser_class_specifier_1 (cp_parser* parser)
18334 {
18335   tree type;
18336   tree attributes = NULL_TREE;
18337   bool nested_name_specifier_p;
18338   unsigned saved_num_template_parameter_lists;
18339   bool saved_in_function_body;
18340   unsigned char in_statement;
18341   bool in_switch_statement_p;
18342   bool saved_in_unbraced_linkage_specification_p;
18343   tree old_scope = NULL_TREE;
18344   tree scope = NULL_TREE;
18345   cp_token *closing_brace;
18346
18347   push_deferring_access_checks (dk_no_deferred);
18348
18349   /* Parse the class-head.  */
18350   type = cp_parser_class_head (parser,
18351                                &nested_name_specifier_p);
18352   /* If the class-head was a semantic disaster, skip the entire body
18353      of the class.  */
18354   if (!type)
18355     {
18356       cp_parser_skip_to_end_of_block_or_statement (parser);
18357       pop_deferring_access_checks ();
18358       return error_mark_node;
18359     }
18360
18361   /* Look for the `{'.  */
18362   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18363     {
18364       pop_deferring_access_checks ();
18365       return error_mark_node;
18366     }
18367
18368   /* Issue an error message if type-definitions are forbidden here.  */
18369   cp_parser_check_type_definition (parser);
18370   /* Remember that we are defining one more class.  */
18371   ++parser->num_classes_being_defined;
18372   /* Inside the class, surrounding template-parameter-lists do not
18373      apply.  */
18374   saved_num_template_parameter_lists
18375     = parser->num_template_parameter_lists;
18376   parser->num_template_parameter_lists = 0;
18377   /* We are not in a function body.  */
18378   saved_in_function_body = parser->in_function_body;
18379   parser->in_function_body = false;
18380   /* Or in a loop.  */
18381   in_statement = parser->in_statement;
18382   parser->in_statement = 0;
18383   /* Or in a switch.  */
18384   in_switch_statement_p = parser->in_switch_statement_p;
18385   parser->in_switch_statement_p = false;
18386   /* We are not immediately inside an extern "lang" block.  */
18387   saved_in_unbraced_linkage_specification_p
18388     = parser->in_unbraced_linkage_specification_p;
18389   parser->in_unbraced_linkage_specification_p = false;
18390
18391   /* Start the class.  */
18392   if (nested_name_specifier_p)
18393     {
18394       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18395       old_scope = push_inner_scope (scope);
18396     }
18397   type = begin_class_definition (type);
18398
18399   if (type == error_mark_node)
18400     /* If the type is erroneous, skip the entire body of the class.  */
18401     cp_parser_skip_to_closing_brace (parser);
18402   else
18403     /* Parse the member-specification.  */
18404     cp_parser_member_specification_opt (parser);
18405
18406   /* Look for the trailing `}'.  */
18407   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18408   /* Look for trailing attributes to apply to this class.  */
18409   if (cp_parser_allow_gnu_extensions_p (parser))
18410     attributes = cp_parser_gnu_attributes_opt (parser);
18411   if (type != error_mark_node)
18412     type = finish_struct (type, attributes);
18413   if (nested_name_specifier_p)
18414     pop_inner_scope (old_scope, scope);
18415
18416   /* We've finished a type definition.  Check for the common syntax
18417      error of forgetting a semicolon after the definition.  We need to
18418      be careful, as we can't just check for not-a-semicolon and be done
18419      with it; the user might have typed:
18420
18421      class X { } c = ...;
18422      class X { } *p = ...;
18423
18424      and so forth.  Instead, enumerate all the possible tokens that
18425      might follow this production; if we don't see one of them, then
18426      complain and silently insert the semicolon.  */
18427   {
18428     cp_token *token = cp_lexer_peek_token (parser->lexer);
18429     bool want_semicolon = true;
18430
18431     if (cp_next_tokens_can_be_std_attribute_p (parser))
18432       /* Don't try to parse c++11 attributes here.  As per the
18433          grammar, that should be a task for
18434          cp_parser_decl_specifier_seq.  */
18435       want_semicolon = false;
18436
18437     switch (token->type)
18438       {
18439       case CPP_NAME:
18440       case CPP_SEMICOLON:
18441       case CPP_MULT:
18442       case CPP_AND:
18443       case CPP_OPEN_PAREN:
18444       case CPP_CLOSE_PAREN:
18445       case CPP_COMMA:
18446         want_semicolon = false;
18447         break;
18448
18449         /* While it's legal for type qualifiers and storage class
18450            specifiers to follow type definitions in the grammar, only
18451            compiler testsuites contain code like that.  Assume that if
18452            we see such code, then what we're really seeing is a case
18453            like:
18454
18455            class X { }
18456            const <type> var = ...;
18457
18458            or
18459
18460            class Y { }
18461            static <type> func (...) ...
18462
18463            i.e. the qualifier or specifier applies to the next
18464            declaration.  To do so, however, we need to look ahead one
18465            more token to see if *that* token is a type specifier.
18466
18467            This code could be improved to handle:
18468
18469            class Z { }
18470            static const <type> var = ...;  */
18471       case CPP_KEYWORD:
18472         if (keyword_is_decl_specifier (token->keyword))
18473           {
18474             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18475
18476             /* Handling user-defined types here would be nice, but very
18477                tricky.  */
18478             want_semicolon
18479               = (lookahead->type == CPP_KEYWORD
18480                  && keyword_begins_type_specifier (lookahead->keyword));
18481           }
18482         break;
18483       default:
18484         break;
18485       }
18486
18487     /* If we don't have a type, then something is very wrong and we
18488        shouldn't try to do anything clever.  Likewise for not seeing the
18489        closing brace.  */
18490     if (closing_brace && TYPE_P (type) && want_semicolon)
18491       {
18492         cp_token_position prev
18493           = cp_lexer_previous_token_position (parser->lexer);
18494         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18495         location_t loc = prev_token->location;
18496
18497         if (CLASSTYPE_DECLARED_CLASS (type))
18498           error_at (loc, "expected %<;%> after class definition");
18499         else if (TREE_CODE (type) == RECORD_TYPE)
18500           error_at (loc, "expected %<;%> after struct definition");
18501         else if (TREE_CODE (type) == UNION_TYPE)
18502           error_at (loc, "expected %<;%> after union definition");
18503         else
18504           gcc_unreachable ();
18505
18506         /* Unget one token and smash it to look as though we encountered
18507            a semicolon in the input stream.  */
18508         cp_lexer_set_token_position (parser->lexer, prev);
18509         token = cp_lexer_peek_token (parser->lexer);
18510         token->type = CPP_SEMICOLON;
18511         token->keyword = RID_MAX;
18512       }
18513   }
18514
18515   /* If this class is not itself within the scope of another class,
18516      then we need to parse the bodies of all of the queued function
18517      definitions.  Note that the queued functions defined in a class
18518      are not always processed immediately following the
18519      class-specifier for that class.  Consider:
18520
18521        struct A {
18522          struct B { void f() { sizeof (A); } };
18523        };
18524
18525      If `f' were processed before the processing of `A' were
18526      completed, there would be no way to compute the size of `A'.
18527      Note that the nesting we are interested in here is lexical --
18528      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18529      for:
18530
18531        struct A { struct B; };
18532        struct A::B { void f() { } };
18533
18534      there is no need to delay the parsing of `A::B::f'.  */
18535   if (--parser->num_classes_being_defined == 0)
18536     {
18537       tree decl;
18538       tree class_type = NULL_TREE;
18539       tree pushed_scope = NULL_TREE;
18540       unsigned ix;
18541       cp_default_arg_entry *e;
18542       tree save_ccp, save_ccr;
18543
18544       /* In a first pass, parse default arguments to the functions.
18545          Then, in a second pass, parse the bodies of the functions.
18546          This two-phased approach handles cases like:
18547
18548             struct S {
18549               void f() { g(); }
18550               void g(int i = 3);
18551             };
18552
18553          */
18554       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18555         {
18556           decl = e->decl;
18557           /* If there are default arguments that have not yet been processed,
18558              take care of them now.  */
18559           if (class_type != e->class_type)
18560             {
18561               if (pushed_scope)
18562                 pop_scope (pushed_scope);
18563               class_type = e->class_type;
18564               pushed_scope = push_scope (class_type);
18565             }
18566           /* Make sure that any template parameters are in scope.  */
18567           maybe_begin_member_template_processing (decl);
18568           /* Parse the default argument expressions.  */
18569           cp_parser_late_parsing_default_args (parser, decl);
18570           /* Remove any template parameters from the symbol table.  */
18571           maybe_end_member_template_processing ();
18572         }
18573       vec_safe_truncate (unparsed_funs_with_default_args, 0);
18574       /* Now parse any NSDMIs.  */
18575       save_ccp = current_class_ptr;
18576       save_ccr = current_class_ref;
18577       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18578         {
18579           if (class_type != DECL_CONTEXT (decl))
18580             {
18581               if (pushed_scope)
18582                 pop_scope (pushed_scope);
18583               class_type = DECL_CONTEXT (decl);
18584               pushed_scope = push_scope (class_type);
18585             }
18586           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18587           cp_parser_late_parsing_nsdmi (parser, decl);
18588         }
18589       vec_safe_truncate (unparsed_nsdmis, 0);
18590       current_class_ptr = save_ccp;
18591       current_class_ref = save_ccr;
18592       if (pushed_scope)
18593         pop_scope (pushed_scope);
18594       /* Now parse the body of the functions.  */
18595       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18596         cp_parser_late_parsing_for_member (parser, decl);
18597       vec_safe_truncate (unparsed_funs_with_definitions, 0);
18598     }
18599
18600   /* Put back any saved access checks.  */
18601   pop_deferring_access_checks ();
18602
18603   /* Restore saved state.  */
18604   parser->in_switch_statement_p = in_switch_statement_p;
18605   parser->in_statement = in_statement;
18606   parser->in_function_body = saved_in_function_body;
18607   parser->num_template_parameter_lists
18608     = saved_num_template_parameter_lists;
18609   parser->in_unbraced_linkage_specification_p
18610     = saved_in_unbraced_linkage_specification_p;
18611
18612   return type;
18613 }
18614
18615 static tree
18616 cp_parser_class_specifier (cp_parser* parser)
18617 {
18618   tree ret;
18619   timevar_push (TV_PARSE_STRUCT);
18620   ret = cp_parser_class_specifier_1 (parser);
18621   timevar_pop (TV_PARSE_STRUCT);
18622   return ret;
18623 }
18624
18625 /* Parse a class-head.
18626
18627    class-head:
18628      class-key identifier [opt] base-clause [opt]
18629      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18630      class-key nested-name-specifier [opt] template-id
18631        base-clause [opt]
18632
18633    class-virt-specifier:
18634      final
18635
18636    GNU Extensions:
18637      class-key attributes identifier [opt] base-clause [opt]
18638      class-key attributes nested-name-specifier identifier base-clause [opt]
18639      class-key attributes nested-name-specifier [opt] template-id
18640        base-clause [opt]
18641
18642    Upon return BASES is initialized to the list of base classes (or
18643    NULL, if there are none) in the same form returned by
18644    cp_parser_base_clause.
18645
18646    Returns the TYPE of the indicated class.  Sets
18647    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18648    involving a nested-name-specifier was used, and FALSE otherwise.
18649
18650    Returns error_mark_node if this is not a class-head.
18651
18652    Returns NULL_TREE if the class-head is syntactically valid, but
18653    semantically invalid in a way that means we should skip the entire
18654    body of the class.  */
18655
18656 static tree
18657 cp_parser_class_head (cp_parser* parser,
18658                       bool* nested_name_specifier_p)
18659 {
18660   tree nested_name_specifier;
18661   enum tag_types class_key;
18662   tree id = NULL_TREE;
18663   tree type = NULL_TREE;
18664   tree attributes;
18665   tree bases;
18666   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18667   bool template_id_p = false;
18668   bool qualified_p = false;
18669   bool invalid_nested_name_p = false;
18670   bool invalid_explicit_specialization_p = false;
18671   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18672   tree pushed_scope = NULL_TREE;
18673   unsigned num_templates;
18674   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18675   /* Assume no nested-name-specifier will be present.  */
18676   *nested_name_specifier_p = false;
18677   /* Assume no template parameter lists will be used in defining the
18678      type.  */
18679   num_templates = 0;
18680   parser->colon_corrects_to_scope_p = false;
18681
18682   /* Look for the class-key.  */
18683   class_key = cp_parser_class_key (parser);
18684   if (class_key == none_type)
18685     return error_mark_node;
18686
18687   /* Parse the attributes.  */
18688   attributes = cp_parser_attributes_opt (parser);
18689
18690   /* If the next token is `::', that is invalid -- but sometimes
18691      people do try to write:
18692
18693        struct ::S {};
18694
18695      Handle this gracefully by accepting the extra qualifier, and then
18696      issuing an error about it later if this really is a
18697      class-head.  If it turns out just to be an elaborated type
18698      specifier, remain silent.  */
18699   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18700     qualified_p = true;
18701
18702   push_deferring_access_checks (dk_no_check);
18703
18704   /* Determine the name of the class.  Begin by looking for an
18705      optional nested-name-specifier.  */
18706   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18707   nested_name_specifier
18708     = cp_parser_nested_name_specifier_opt (parser,
18709                                            /*typename_keyword_p=*/false,
18710                                            /*check_dependency_p=*/false,
18711                                            /*type_p=*/true,
18712                                            /*is_declaration=*/false);
18713   /* If there was a nested-name-specifier, then there *must* be an
18714      identifier.  */
18715   if (nested_name_specifier)
18716     {
18717       type_start_token = cp_lexer_peek_token (parser->lexer);
18718       /* Although the grammar says `identifier', it really means
18719          `class-name' or `template-name'.  You are only allowed to
18720          define a class that has already been declared with this
18721          syntax.
18722
18723          The proposed resolution for Core Issue 180 says that wherever
18724          you see `class T::X' you should treat `X' as a type-name.
18725
18726          It is OK to define an inaccessible class; for example:
18727
18728            class A { class B; };
18729            class A::B {};
18730
18731          We do not know if we will see a class-name, or a
18732          template-name.  We look for a class-name first, in case the
18733          class-name is a template-id; if we looked for the
18734          template-name first we would stop after the template-name.  */
18735       cp_parser_parse_tentatively (parser);
18736       type = cp_parser_class_name (parser,
18737                                    /*typename_keyword_p=*/false,
18738                                    /*template_keyword_p=*/false,
18739                                    class_type,
18740                                    /*check_dependency_p=*/false,
18741                                    /*class_head_p=*/true,
18742                                    /*is_declaration=*/false);
18743       /* If that didn't work, ignore the nested-name-specifier.  */
18744       if (!cp_parser_parse_definitely (parser))
18745         {
18746           invalid_nested_name_p = true;
18747           type_start_token = cp_lexer_peek_token (parser->lexer);
18748           id = cp_parser_identifier (parser);
18749           if (id == error_mark_node)
18750             id = NULL_TREE;
18751         }
18752       /* If we could not find a corresponding TYPE, treat this
18753          declaration like an unqualified declaration.  */
18754       if (type == error_mark_node)
18755         nested_name_specifier = NULL_TREE;
18756       /* Otherwise, count the number of templates used in TYPE and its
18757          containing scopes.  */
18758       else
18759         {
18760           tree scope;
18761
18762           for (scope = TREE_TYPE (type);
18763                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18764                scope = (TYPE_P (scope)
18765                         ? TYPE_CONTEXT (scope)
18766                         : DECL_CONTEXT (scope)))
18767             if (TYPE_P (scope)
18768                 && CLASS_TYPE_P (scope)
18769                 && CLASSTYPE_TEMPLATE_INFO (scope)
18770                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18771                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18772                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18773               ++num_templates;
18774         }
18775     }
18776   /* Otherwise, the identifier is optional.  */
18777   else
18778     {
18779       /* We don't know whether what comes next is a template-id,
18780          an identifier, or nothing at all.  */
18781       cp_parser_parse_tentatively (parser);
18782       /* Check for a template-id.  */
18783       type_start_token = cp_lexer_peek_token (parser->lexer);
18784       id = cp_parser_template_id (parser,
18785                                   /*template_keyword_p=*/false,
18786                                   /*check_dependency_p=*/true,
18787                                   class_key,
18788                                   /*is_declaration=*/true);
18789       /* If that didn't work, it could still be an identifier.  */
18790       if (!cp_parser_parse_definitely (parser))
18791         {
18792           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18793             {
18794               type_start_token = cp_lexer_peek_token (parser->lexer);
18795               id = cp_parser_identifier (parser);
18796             }
18797           else
18798             id = NULL_TREE;
18799         }
18800       else
18801         {
18802           template_id_p = true;
18803           ++num_templates;
18804         }
18805     }
18806
18807   pop_deferring_access_checks ();
18808
18809   if (id)
18810     {
18811       cp_parser_check_for_invalid_template_id (parser, id,
18812                                                class_key,
18813                                                type_start_token->location);
18814     }
18815   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18816
18817   /* If it's not a `:' or a `{' then we can't really be looking at a
18818      class-head, since a class-head only appears as part of a
18819      class-specifier.  We have to detect this situation before calling
18820      xref_tag, since that has irreversible side-effects.  */
18821   if (!cp_parser_next_token_starts_class_definition_p (parser))
18822     {
18823       cp_parser_error (parser, "expected %<{%> or %<:%>");
18824       type = error_mark_node;
18825       goto out;
18826     }
18827
18828   /* At this point, we're going ahead with the class-specifier, even
18829      if some other problem occurs.  */
18830   cp_parser_commit_to_tentative_parse (parser);
18831   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18832     {
18833       cp_parser_error (parser,
18834                        "cannot specify %<override%> for a class");
18835       type = error_mark_node;
18836       goto out;
18837     }
18838   /* Issue the error about the overly-qualified name now.  */
18839   if (qualified_p)
18840     {
18841       cp_parser_error (parser,
18842                        "global qualification of class name is invalid");
18843       type = error_mark_node;
18844       goto out;
18845     }
18846   else if (invalid_nested_name_p)
18847     {
18848       cp_parser_error (parser,
18849                        "qualified name does not name a class");
18850       type = error_mark_node;
18851       goto out;
18852     }
18853   else if (nested_name_specifier)
18854     {
18855       tree scope;
18856
18857       /* Reject typedef-names in class heads.  */
18858       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18859         {
18860           error_at (type_start_token->location,
18861                     "invalid class name in declaration of %qD",
18862                     type);
18863           type = NULL_TREE;
18864           goto done;
18865         }
18866
18867       /* Figure out in what scope the declaration is being placed.  */
18868       scope = current_scope ();
18869       /* If that scope does not contain the scope in which the
18870          class was originally declared, the program is invalid.  */
18871       if (scope && !is_ancestor (scope, nested_name_specifier))
18872         {
18873           if (at_namespace_scope_p ())
18874             error_at (type_start_token->location,
18875                       "declaration of %qD in namespace %qD which does not "
18876                       "enclose %qD",
18877                       type, scope, nested_name_specifier);
18878           else
18879             error_at (type_start_token->location,
18880                       "declaration of %qD in %qD which does not enclose %qD",
18881                       type, scope, nested_name_specifier);
18882           type = NULL_TREE;
18883           goto done;
18884         }
18885       /* [dcl.meaning]
18886
18887          A declarator-id shall not be qualified except for the
18888          definition of a ... nested class outside of its class
18889          ... [or] the definition or explicit instantiation of a
18890          class member of a namespace outside of its namespace.  */
18891       if (scope == nested_name_specifier)
18892         {
18893           permerror (nested_name_specifier_token_start->location,
18894                      "extra qualification not allowed");
18895           nested_name_specifier = NULL_TREE;
18896           num_templates = 0;
18897         }
18898     }
18899   /* An explicit-specialization must be preceded by "template <>".  If
18900      it is not, try to recover gracefully.  */
18901   if (at_namespace_scope_p ()
18902       && parser->num_template_parameter_lists == 0
18903       && template_id_p)
18904     {
18905       error_at (type_start_token->location,
18906                 "an explicit specialization must be preceded by %<template <>%>");
18907       invalid_explicit_specialization_p = true;
18908       /* Take the same action that would have been taken by
18909          cp_parser_explicit_specialization.  */
18910       ++parser->num_template_parameter_lists;
18911       begin_specialization ();
18912     }
18913   /* There must be no "return" statements between this point and the
18914      end of this function; set "type "to the correct return value and
18915      use "goto done;" to return.  */
18916   /* Make sure that the right number of template parameters were
18917      present.  */
18918   if (!cp_parser_check_template_parameters (parser, num_templates,
18919                                             type_start_token->location,
18920                                             /*declarator=*/NULL))
18921     {
18922       /* If something went wrong, there is no point in even trying to
18923          process the class-definition.  */
18924       type = NULL_TREE;
18925       goto done;
18926     }
18927
18928   /* Look up the type.  */
18929   if (template_id_p)
18930     {
18931       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18932           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18933               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18934         {
18935           error_at (type_start_token->location,
18936                     "function template %qD redeclared as a class template", id);
18937           type = error_mark_node;
18938         }
18939       else
18940         {
18941           type = TREE_TYPE (id);
18942           type = maybe_process_partial_specialization (type);
18943         }
18944       if (nested_name_specifier)
18945         pushed_scope = push_scope (nested_name_specifier);
18946     }
18947   else if (nested_name_specifier)
18948     {
18949       tree class_type;
18950
18951       /* Given:
18952
18953             template <typename T> struct S { struct T };
18954             template <typename T> struct S<T>::T { };
18955
18956          we will get a TYPENAME_TYPE when processing the definition of
18957          `S::T'.  We need to resolve it to the actual type before we
18958          try to define it.  */
18959       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18960         {
18961           class_type = resolve_typename_type (TREE_TYPE (type),
18962                                               /*only_current_p=*/false);
18963           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18964             type = TYPE_NAME (class_type);
18965           else
18966             {
18967               cp_parser_error (parser, "could not resolve typename type");
18968               type = error_mark_node;
18969             }
18970         }
18971
18972       if (maybe_process_partial_specialization (TREE_TYPE (type))
18973           == error_mark_node)
18974         {
18975           type = NULL_TREE;
18976           goto done;
18977         }
18978
18979       class_type = current_class_type;
18980       /* Enter the scope indicated by the nested-name-specifier.  */
18981       pushed_scope = push_scope (nested_name_specifier);
18982       /* Get the canonical version of this type.  */
18983       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18984       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18985           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18986         {
18987           type = push_template_decl (type);
18988           if (type == error_mark_node)
18989             {
18990               type = NULL_TREE;
18991               goto done;
18992             }
18993         }
18994
18995       type = TREE_TYPE (type);
18996       *nested_name_specifier_p = true;
18997     }
18998   else      /* The name is not a nested name.  */
18999     {
19000       /* If the class was unnamed, create a dummy name.  */
19001       if (!id)
19002         id = make_anon_name ();
19003       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19004                        parser->num_template_parameter_lists);
19005     }
19006
19007   /* Indicate whether this class was declared as a `class' or as a
19008      `struct'.  */
19009   if (TREE_CODE (type) == RECORD_TYPE)
19010     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19011   cp_parser_check_class_key (class_key, type);
19012
19013   /* If this type was already complete, and we see another definition,
19014      that's an error.  */
19015   if (type != error_mark_node && COMPLETE_TYPE_P (type))
19016     {
19017       error_at (type_start_token->location, "redefinition of %q#T",
19018                 type);
19019       error_at (type_start_token->location, "previous definition of %q+#T",
19020                 type);
19021       type = NULL_TREE;
19022       goto done;
19023     }
19024   else if (type == error_mark_node)
19025     type = NULL_TREE;
19026
19027   if (type)
19028     {
19029       /* Apply attributes now, before any use of the class as a template
19030          argument in its base list.  */
19031       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19032       fixup_attribute_variants (type);
19033     }
19034
19035   /* We will have entered the scope containing the class; the names of
19036      base classes should be looked up in that context.  For example:
19037
19038        struct A { struct B {}; struct C; };
19039        struct A::C : B {};
19040
19041      is valid.  */
19042
19043   /* Get the list of base-classes, if there is one.  */
19044   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19045     bases = cp_parser_base_clause (parser);
19046   else
19047     bases = NULL_TREE;
19048
19049   /* If we're really defining a class, process the base classes.
19050      If they're invalid, fail.  */
19051   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19052       && !xref_basetypes (type, bases))
19053     type = NULL_TREE;
19054
19055  done:
19056   /* Leave the scope given by the nested-name-specifier.  We will
19057      enter the class scope itself while processing the members.  */
19058   if (pushed_scope)
19059     pop_scope (pushed_scope);
19060
19061   if (invalid_explicit_specialization_p)
19062     {
19063       end_specialization ();
19064       --parser->num_template_parameter_lists;
19065     }
19066
19067   if (type)
19068     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19069   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19070     CLASSTYPE_FINAL (type) = 1;
19071  out:
19072   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19073   return type;
19074 }
19075
19076 /* Parse a class-key.
19077
19078    class-key:
19079      class
19080      struct
19081      union
19082
19083    Returns the kind of class-key specified, or none_type to indicate
19084    error.  */
19085
19086 static enum tag_types
19087 cp_parser_class_key (cp_parser* parser)
19088 {
19089   cp_token *token;
19090   enum tag_types tag_type;
19091
19092   /* Look for the class-key.  */
19093   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19094   if (!token)
19095     return none_type;
19096
19097   /* Check to see if the TOKEN is a class-key.  */
19098   tag_type = cp_parser_token_is_class_key (token);
19099   if (!tag_type)
19100     cp_parser_error (parser, "expected class-key");
19101   return tag_type;
19102 }
19103
19104 /* Parse an (optional) member-specification.
19105
19106    member-specification:
19107      member-declaration member-specification [opt]
19108      access-specifier : member-specification [opt]  */
19109
19110 static void
19111 cp_parser_member_specification_opt (cp_parser* parser)
19112 {
19113   while (true)
19114     {
19115       cp_token *token;
19116       enum rid keyword;
19117
19118       /* Peek at the next token.  */
19119       token = cp_lexer_peek_token (parser->lexer);
19120       /* If it's a `}', or EOF then we've seen all the members.  */
19121       if (token->type == CPP_CLOSE_BRACE
19122           || token->type == CPP_EOF
19123           || token->type == CPP_PRAGMA_EOL)
19124         break;
19125
19126       /* See if this token is a keyword.  */
19127       keyword = token->keyword;
19128       switch (keyword)
19129         {
19130         case RID_PUBLIC:
19131         case RID_PROTECTED:
19132         case RID_PRIVATE:
19133           /* Consume the access-specifier.  */
19134           cp_lexer_consume_token (parser->lexer);
19135           /* Remember which access-specifier is active.  */
19136           current_access_specifier = token->u.value;
19137           /* Look for the `:'.  */
19138           cp_parser_require (parser, CPP_COLON, RT_COLON);
19139           break;
19140
19141         default:
19142           /* Accept #pragmas at class scope.  */
19143           if (token->type == CPP_PRAGMA)
19144             {
19145               cp_parser_pragma (parser, pragma_external);
19146               break;
19147             }
19148
19149           /* Otherwise, the next construction must be a
19150              member-declaration.  */
19151           cp_parser_member_declaration (parser);
19152         }
19153     }
19154 }
19155
19156 /* Parse a member-declaration.
19157
19158    member-declaration:
19159      decl-specifier-seq [opt] member-declarator-list [opt] ;
19160      function-definition ; [opt]
19161      :: [opt] nested-name-specifier template [opt] unqualified-id ;
19162      using-declaration
19163      template-declaration
19164      alias-declaration
19165
19166    member-declarator-list:
19167      member-declarator
19168      member-declarator-list , member-declarator
19169
19170    member-declarator:
19171      declarator pure-specifier [opt]
19172      declarator constant-initializer [opt]
19173      identifier [opt] : constant-expression
19174
19175    GNU Extensions:
19176
19177    member-declaration:
19178      __extension__ member-declaration
19179
19180    member-declarator:
19181      declarator attributes [opt] pure-specifier [opt]
19182      declarator attributes [opt] constant-initializer [opt]
19183      identifier [opt] attributes [opt] : constant-expression  
19184
19185    C++0x Extensions:
19186
19187    member-declaration:
19188      static_assert-declaration  */
19189
19190 static void
19191 cp_parser_member_declaration (cp_parser* parser)
19192 {
19193   cp_decl_specifier_seq decl_specifiers;
19194   tree prefix_attributes;
19195   tree decl;
19196   int declares_class_or_enum;
19197   bool friend_p;
19198   cp_token *token = NULL;
19199   cp_token *decl_spec_token_start = NULL;
19200   cp_token *initializer_token_start = NULL;
19201   int saved_pedantic;
19202   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19203
19204   /* Check for the `__extension__' keyword.  */
19205   if (cp_parser_extension_opt (parser, &saved_pedantic))
19206     {
19207       /* Recurse.  */
19208       cp_parser_member_declaration (parser);
19209       /* Restore the old value of the PEDANTIC flag.  */
19210       pedantic = saved_pedantic;
19211
19212       return;
19213     }
19214
19215   /* Check for a template-declaration.  */
19216   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19217     {
19218       /* An explicit specialization here is an error condition, and we
19219          expect the specialization handler to detect and report this.  */
19220       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19221           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19222         cp_parser_explicit_specialization (parser);
19223       else
19224         cp_parser_template_declaration (parser, /*member_p=*/true);
19225
19226       return;
19227     }
19228
19229   /* Check for a using-declaration.  */
19230   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19231     {
19232       if (cxx_dialect < cxx0x)
19233         {
19234           /* Parse the using-declaration.  */
19235           cp_parser_using_declaration (parser,
19236                                        /*access_declaration_p=*/false);
19237           return;
19238         }
19239       else
19240         {
19241           tree decl;
19242           bool alias_decl_expected;
19243           cp_parser_parse_tentatively (parser);
19244           decl = cp_parser_alias_declaration (parser);
19245           /* Note that if we actually see the '=' token after the
19246              identifier, cp_parser_alias_declaration commits the
19247              tentative parse.  In that case, we really expects an
19248              alias-declaration.  Otherwise, we expect a using
19249              declaration.  */
19250           alias_decl_expected =
19251             !cp_parser_uncommitted_to_tentative_parse_p (parser);
19252           cp_parser_parse_definitely (parser);
19253
19254           if (alias_decl_expected)
19255             finish_member_declaration (decl);
19256           else
19257             cp_parser_using_declaration (parser,
19258                                          /*access_declaration_p=*/false);
19259           return;
19260         }
19261     }
19262
19263   /* Check for @defs.  */
19264   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19265     {
19266       tree ivar, member;
19267       tree ivar_chains = cp_parser_objc_defs_expression (parser);
19268       ivar = ivar_chains;
19269       while (ivar)
19270         {
19271           member = ivar;
19272           ivar = TREE_CHAIN (member);
19273           TREE_CHAIN (member) = NULL_TREE;
19274           finish_member_declaration (member);
19275         }
19276       return;
19277     }
19278
19279   /* If the next token is `static_assert' we have a static assertion.  */
19280   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19281     {
19282       cp_parser_static_assert (parser, /*member_p=*/true);
19283       return;
19284     }
19285
19286   parser->colon_corrects_to_scope_p = false;
19287
19288   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19289       goto out;
19290
19291   /* Parse the decl-specifier-seq.  */
19292   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19293   cp_parser_decl_specifier_seq (parser,
19294                                 CP_PARSER_FLAGS_OPTIONAL,
19295                                 &decl_specifiers,
19296                                 &declares_class_or_enum);
19297   /* Check for an invalid type-name.  */
19298   if (!decl_specifiers.any_type_specifiers_p
19299       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19300     goto out;
19301   /* If there is no declarator, then the decl-specifier-seq should
19302      specify a type.  */
19303   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19304     {
19305       /* If there was no decl-specifier-seq, and the next token is a
19306          `;', then we have something like:
19307
19308            struct S { ; };
19309
19310          [class.mem]
19311
19312          Each member-declaration shall declare at least one member
19313          name of the class.  */
19314       if (!decl_specifiers.any_specifiers_p)
19315         {
19316           cp_token *token = cp_lexer_peek_token (parser->lexer);
19317           if (!in_system_header_at (token->location))
19318             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19319         }
19320       else
19321         {
19322           tree type;
19323
19324           /* See if this declaration is a friend.  */
19325           friend_p = cp_parser_friend_p (&decl_specifiers);
19326           /* If there were decl-specifiers, check to see if there was
19327              a class-declaration.  */
19328           type = check_tag_decl (&decl_specifiers,
19329                                  /*explicit_type_instantiation_p=*/false);
19330           /* Nested classes have already been added to the class, but
19331              a `friend' needs to be explicitly registered.  */
19332           if (friend_p)
19333             {
19334               /* If the `friend' keyword was present, the friend must
19335                  be introduced with a class-key.  */
19336                if (!declares_class_or_enum && cxx_dialect < cxx0x)
19337                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19338                           "in C++03 a class-key must be used "
19339                           "when declaring a friend");
19340                /* In this case:
19341
19342                     template <typename T> struct A {
19343                       friend struct A<T>::B;
19344                     };
19345
19346                   A<T>::B will be represented by a TYPENAME_TYPE, and
19347                   therefore not recognized by check_tag_decl.  */
19348                if (!type)
19349                  {
19350                    type = decl_specifiers.type;
19351                    if (type && TREE_CODE (type) == TYPE_DECL)
19352                      type = TREE_TYPE (type);
19353                  }
19354                if (!type || !TYPE_P (type))
19355                  error_at (decl_spec_token_start->location,
19356                            "friend declaration does not name a class or "
19357                            "function");
19358                else
19359                  make_friend_class (current_class_type, type,
19360                                     /*complain=*/true);
19361             }
19362           /* If there is no TYPE, an error message will already have
19363              been issued.  */
19364           else if (!type || type == error_mark_node)
19365             ;
19366           /* An anonymous aggregate has to be handled specially; such
19367              a declaration really declares a data member (with a
19368              particular type), as opposed to a nested class.  */
19369           else if (ANON_AGGR_TYPE_P (type))
19370             {
19371               /* C++11 9.5/6.  */
19372               if (decl_specifiers.storage_class != sc_none)
19373                 error_at (decl_spec_token_start->location,
19374                           "a storage class on an anonymous aggregate "
19375                           "in class scope is not allowed");
19376
19377               /* Remove constructors and such from TYPE, now that we
19378                  know it is an anonymous aggregate.  */
19379               fixup_anonymous_aggr (type);
19380               /* And make the corresponding data member.  */
19381               decl = build_decl (decl_spec_token_start->location,
19382                                  FIELD_DECL, NULL_TREE, type);
19383               /* Add it to the class.  */
19384               finish_member_declaration (decl);
19385             }
19386           else
19387             cp_parser_check_access_in_redeclaration
19388                                               (TYPE_NAME (type),
19389                                                decl_spec_token_start->location);
19390         }
19391     }
19392   else
19393     {
19394       bool assume_semicolon = false;
19395
19396       /* Clear attributes from the decl_specifiers but keep them
19397          around as prefix attributes that apply them to the entity
19398          being declared.  */
19399       prefix_attributes = decl_specifiers.attributes;
19400       decl_specifiers.attributes = NULL_TREE;
19401
19402       /* See if these declarations will be friends.  */
19403       friend_p = cp_parser_friend_p (&decl_specifiers);
19404
19405       /* Keep going until we hit the `;' at the end of the
19406          declaration.  */
19407       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19408         {
19409           tree attributes = NULL_TREE;
19410           tree first_attribute;
19411
19412           /* Peek at the next token.  */
19413           token = cp_lexer_peek_token (parser->lexer);
19414
19415           /* Check for a bitfield declaration.  */
19416           if (token->type == CPP_COLON
19417               || (token->type == CPP_NAME
19418                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19419                   == CPP_COLON))
19420             {
19421               tree identifier;
19422               tree width;
19423
19424               /* Get the name of the bitfield.  Note that we cannot just
19425                  check TOKEN here because it may have been invalidated by
19426                  the call to cp_lexer_peek_nth_token above.  */
19427               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19428                 identifier = cp_parser_identifier (parser);
19429               else
19430                 identifier = NULL_TREE;
19431
19432               /* Consume the `:' token.  */
19433               cp_lexer_consume_token (parser->lexer);
19434               /* Get the width of the bitfield.  */
19435               width
19436                 = cp_parser_constant_expression (parser,
19437                                                  /*allow_non_constant=*/false,
19438                                                  NULL);
19439
19440               /* Look for attributes that apply to the bitfield.  */
19441               attributes = cp_parser_attributes_opt (parser);
19442               /* Remember which attributes are prefix attributes and
19443                  which are not.  */
19444               first_attribute = attributes;
19445               /* Combine the attributes.  */
19446               attributes = chainon (prefix_attributes, attributes);
19447
19448               /* Create the bitfield declaration.  */
19449               decl = grokbitfield (identifier
19450                                    ? make_id_declarator (NULL_TREE,
19451                                                          identifier,
19452                                                          sfk_none)
19453                                    : NULL,
19454                                    &decl_specifiers,
19455                                    width,
19456                                    attributes);
19457             }
19458           else
19459             {
19460               cp_declarator *declarator;
19461               tree initializer;
19462               tree asm_specification;
19463               int ctor_dtor_or_conv_p;
19464
19465               /* Parse the declarator.  */
19466               declarator
19467                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19468                                         &ctor_dtor_or_conv_p,
19469                                         /*parenthesized_p=*/NULL,
19470                                         /*member_p=*/true);
19471
19472               /* If something went wrong parsing the declarator, make sure
19473                  that we at least consume some tokens.  */
19474               if (declarator == cp_error_declarator)
19475                 {
19476                   /* Skip to the end of the statement.  */
19477                   cp_parser_skip_to_end_of_statement (parser);
19478                   /* If the next token is not a semicolon, that is
19479                      probably because we just skipped over the body of
19480                      a function.  So, we consume a semicolon if
19481                      present, but do not issue an error message if it
19482                      is not present.  */
19483                   if (cp_lexer_next_token_is (parser->lexer,
19484                                               CPP_SEMICOLON))
19485                     cp_lexer_consume_token (parser->lexer);
19486                   goto out;
19487                 }
19488
19489               if (declares_class_or_enum & 2)
19490                 cp_parser_check_for_definition_in_return_type
19491                                             (declarator, decl_specifiers.type,
19492                                              decl_specifiers.locations[ds_type_spec]);
19493
19494               /* Look for an asm-specification.  */
19495               asm_specification = cp_parser_asm_specification_opt (parser);
19496               /* Look for attributes that apply to the declaration.  */
19497               attributes = cp_parser_attributes_opt (parser);
19498               /* Remember which attributes are prefix attributes and
19499                  which are not.  */
19500               first_attribute = attributes;
19501               /* Combine the attributes.  */
19502               attributes = chainon (prefix_attributes, attributes);
19503
19504               /* If it's an `=', then we have a constant-initializer or a
19505                  pure-specifier.  It is not correct to parse the
19506                  initializer before registering the member declaration
19507                  since the member declaration should be in scope while
19508                  its initializer is processed.  However, the rest of the
19509                  front end does not yet provide an interface that allows
19510                  us to handle this correctly.  */
19511               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19512                 {
19513                   /* In [class.mem]:
19514
19515                      A pure-specifier shall be used only in the declaration of
19516                      a virtual function.
19517
19518                      A member-declarator can contain a constant-initializer
19519                      only if it declares a static member of integral or
19520                      enumeration type.
19521
19522                      Therefore, if the DECLARATOR is for a function, we look
19523                      for a pure-specifier; otherwise, we look for a
19524                      constant-initializer.  When we call `grokfield', it will
19525                      perform more stringent semantics checks.  */
19526                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19527                   if (function_declarator_p (declarator)
19528                       || (decl_specifiers.type
19529                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19530                           && declarator->kind == cdk_id
19531                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19532                               == FUNCTION_TYPE)))
19533                     initializer = cp_parser_pure_specifier (parser);
19534                   else if (decl_specifiers.storage_class != sc_static)
19535                     initializer = cp_parser_save_nsdmi (parser);
19536                   else if (cxx_dialect >= cxx0x)
19537                     {
19538                       bool nonconst;
19539                       /* Don't require a constant rvalue in C++11, since we
19540                          might want a reference constant.  We'll enforce
19541                          constancy later.  */
19542                       cp_lexer_consume_token (parser->lexer);
19543                       /* Parse the initializer.  */
19544                       initializer = cp_parser_initializer_clause (parser,
19545                                                                   &nonconst);
19546                     }
19547                   else
19548                     /* Parse the initializer.  */
19549                     initializer = cp_parser_constant_initializer (parser);
19550                 }
19551               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19552                        && !function_declarator_p (declarator))
19553                 {
19554                   bool x;
19555                   if (decl_specifiers.storage_class != sc_static)
19556                     initializer = cp_parser_save_nsdmi (parser);
19557                   else
19558                     initializer = cp_parser_initializer (parser, &x, &x);
19559                 }
19560               /* Otherwise, there is no initializer.  */
19561               else
19562                 initializer = NULL_TREE;
19563
19564               /* See if we are probably looking at a function
19565                  definition.  We are certainly not looking at a
19566                  member-declarator.  Calling `grokfield' has
19567                  side-effects, so we must not do it unless we are sure
19568                  that we are looking at a member-declarator.  */
19569               if (cp_parser_token_starts_function_definition_p
19570                   (cp_lexer_peek_token (parser->lexer)))
19571                 {
19572                   /* The grammar does not allow a pure-specifier to be
19573                      used when a member function is defined.  (It is
19574                      possible that this fact is an oversight in the
19575                      standard, since a pure function may be defined
19576                      outside of the class-specifier.  */
19577                   if (initializer && initializer_token_start)
19578                     error_at (initializer_token_start->location,
19579                               "pure-specifier on function-definition");
19580                   decl = cp_parser_save_member_function_body (parser,
19581                                                               &decl_specifiers,
19582                                                               declarator,
19583                                                               attributes);
19584                   /* If the member was not a friend, declare it here.  */
19585                   if (!friend_p)
19586                     finish_member_declaration (decl);
19587                   /* Peek at the next token.  */
19588                   token = cp_lexer_peek_token (parser->lexer);
19589                   /* If the next token is a semicolon, consume it.  */
19590                   if (token->type == CPP_SEMICOLON)
19591                     cp_lexer_consume_token (parser->lexer);
19592                   goto out;
19593                 }
19594               else
19595                 if (declarator->kind == cdk_function)
19596                   declarator->id_loc = token->location;
19597                 /* Create the declaration.  */
19598                 decl = grokfield (declarator, &decl_specifiers,
19599                                   initializer, /*init_const_expr_p=*/true,
19600                                   asm_specification,
19601                                   attributes);
19602             }
19603
19604           /* Reset PREFIX_ATTRIBUTES.  */
19605           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19606             attributes = TREE_CHAIN (attributes);
19607           if (attributes)
19608             TREE_CHAIN (attributes) = NULL_TREE;
19609
19610           /* If there is any qualification still in effect, clear it
19611              now; we will be starting fresh with the next declarator.  */
19612           parser->scope = NULL_TREE;
19613           parser->qualifying_scope = NULL_TREE;
19614           parser->object_scope = NULL_TREE;
19615           /* If it's a `,', then there are more declarators.  */
19616           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19617             {
19618               cp_lexer_consume_token (parser->lexer);
19619               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19620                 {
19621                   cp_token *token = cp_lexer_previous_token (parser->lexer);
19622                   error_at (token->location,
19623                             "stray %<,%> at end of member declaration");
19624                 }
19625             }
19626           /* If the next token isn't a `;', then we have a parse error.  */
19627           else if (cp_lexer_next_token_is_not (parser->lexer,
19628                                                CPP_SEMICOLON))
19629             {
19630               /* The next token might be a ways away from where the
19631                  actual semicolon is missing.  Find the previous token
19632                  and use that for our error position.  */
19633               cp_token *token = cp_lexer_previous_token (parser->lexer);
19634               error_at (token->location,
19635                         "expected %<;%> at end of member declaration");
19636
19637               /* Assume that the user meant to provide a semicolon.  If
19638                  we were to cp_parser_skip_to_end_of_statement, we might
19639                  skip to a semicolon inside a member function definition
19640                  and issue nonsensical error messages.  */
19641               assume_semicolon = true;
19642             }
19643
19644           if (decl)
19645             {
19646               /* Add DECL to the list of members.  */
19647               if (!friend_p)
19648                 finish_member_declaration (decl);
19649
19650               if (TREE_CODE (decl) == FUNCTION_DECL)
19651                 cp_parser_save_default_args (parser, decl);
19652               else if (TREE_CODE (decl) == FIELD_DECL
19653                        && !DECL_C_BIT_FIELD (decl)
19654                        && DECL_INITIAL (decl))
19655                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19656                 vec_safe_push (unparsed_nsdmis, decl);
19657             }
19658
19659           if (assume_semicolon)
19660             goto out;
19661         }
19662     }
19663
19664   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19665  out:
19666   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19667 }
19668
19669 /* Parse a pure-specifier.
19670
19671    pure-specifier:
19672      = 0
19673
19674    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19675    Otherwise, ERROR_MARK_NODE is returned.  */
19676
19677 static tree
19678 cp_parser_pure_specifier (cp_parser* parser)
19679 {
19680   cp_token *token;
19681
19682   /* Look for the `=' token.  */
19683   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19684     return error_mark_node;
19685   /* Look for the `0' token.  */
19686   token = cp_lexer_peek_token (parser->lexer);
19687
19688   if (token->type == CPP_EOF
19689       || token->type == CPP_PRAGMA_EOL)
19690     return error_mark_node;
19691
19692   cp_lexer_consume_token (parser->lexer);
19693
19694   /* Accept = default or = delete in c++0x mode.  */
19695   if (token->keyword == RID_DEFAULT
19696       || token->keyword == RID_DELETE)
19697     {
19698       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19699       return token->u.value;
19700     }
19701
19702   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19703   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19704     {
19705       cp_parser_error (parser,
19706                        "invalid pure specifier (only %<= 0%> is allowed)");
19707       cp_parser_skip_to_end_of_statement (parser);
19708       return error_mark_node;
19709     }
19710   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19711     {
19712       error_at (token->location, "templates may not be %<virtual%>");
19713       return error_mark_node;
19714     }
19715
19716   return integer_zero_node;
19717 }
19718
19719 /* Parse a constant-initializer.
19720
19721    constant-initializer:
19722      = constant-expression
19723
19724    Returns a representation of the constant-expression.  */
19725
19726 static tree
19727 cp_parser_constant_initializer (cp_parser* parser)
19728 {
19729   /* Look for the `=' token.  */
19730   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19731     return error_mark_node;
19732
19733   /* It is invalid to write:
19734
19735        struct S { static const int i = { 7 }; };
19736
19737      */
19738   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19739     {
19740       cp_parser_error (parser,
19741                        "a brace-enclosed initializer is not allowed here");
19742       /* Consume the opening brace.  */
19743       cp_lexer_consume_token (parser->lexer);
19744       /* Skip the initializer.  */
19745       cp_parser_skip_to_closing_brace (parser);
19746       /* Look for the trailing `}'.  */
19747       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19748
19749       return error_mark_node;
19750     }
19751
19752   return cp_parser_constant_expression (parser,
19753                                         /*allow_non_constant=*/false,
19754                                         NULL);
19755 }
19756
19757 /* Derived classes [gram.class.derived] */
19758
19759 /* Parse a base-clause.
19760
19761    base-clause:
19762      : base-specifier-list
19763
19764    base-specifier-list:
19765      base-specifier ... [opt]
19766      base-specifier-list , base-specifier ... [opt]
19767
19768    Returns a TREE_LIST representing the base-classes, in the order in
19769    which they were declared.  The representation of each node is as
19770    described by cp_parser_base_specifier.
19771
19772    In the case that no bases are specified, this function will return
19773    NULL_TREE, not ERROR_MARK_NODE.  */
19774
19775 static tree
19776 cp_parser_base_clause (cp_parser* parser)
19777 {
19778   tree bases = NULL_TREE;
19779
19780   /* Look for the `:' that begins the list.  */
19781   cp_parser_require (parser, CPP_COLON, RT_COLON);
19782
19783   /* Scan the base-specifier-list.  */
19784   while (true)
19785     {
19786       cp_token *token;
19787       tree base;
19788       bool pack_expansion_p = false;
19789
19790       /* Look for the base-specifier.  */
19791       base = cp_parser_base_specifier (parser);
19792       /* Look for the (optional) ellipsis. */
19793       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19794         {
19795           /* Consume the `...'. */
19796           cp_lexer_consume_token (parser->lexer);
19797
19798           pack_expansion_p = true;
19799         }
19800
19801       /* Add BASE to the front of the list.  */
19802       if (base && base != error_mark_node)
19803         {
19804           if (pack_expansion_p)
19805             /* Make this a pack expansion type. */
19806             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19807
19808           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19809             {
19810               TREE_CHAIN (base) = bases;
19811               bases = base;
19812             }
19813         }
19814       /* Peek at the next token.  */
19815       token = cp_lexer_peek_token (parser->lexer);
19816       /* If it's not a comma, then the list is complete.  */
19817       if (token->type != CPP_COMMA)
19818         break;
19819       /* Consume the `,'.  */
19820       cp_lexer_consume_token (parser->lexer);
19821     }
19822
19823   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19824      base class had a qualified name.  However, the next name that
19825      appears is certainly not qualified.  */
19826   parser->scope = NULL_TREE;
19827   parser->qualifying_scope = NULL_TREE;
19828   parser->object_scope = NULL_TREE;
19829
19830   return nreverse (bases);
19831 }
19832
19833 /* Parse a base-specifier.
19834
19835    base-specifier:
19836      :: [opt] nested-name-specifier [opt] class-name
19837      virtual access-specifier [opt] :: [opt] nested-name-specifier
19838        [opt] class-name
19839      access-specifier virtual [opt] :: [opt] nested-name-specifier
19840        [opt] class-name
19841
19842    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19843    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19844    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19845    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19846
19847 static tree
19848 cp_parser_base_specifier (cp_parser* parser)
19849 {
19850   cp_token *token;
19851   bool done = false;
19852   bool virtual_p = false;
19853   bool duplicate_virtual_error_issued_p = false;
19854   bool duplicate_access_error_issued_p = false;
19855   bool class_scope_p, template_p;
19856   tree access = access_default_node;
19857   tree type;
19858
19859   /* Process the optional `virtual' and `access-specifier'.  */
19860   while (!done)
19861     {
19862       /* Peek at the next token.  */
19863       token = cp_lexer_peek_token (parser->lexer);
19864       /* Process `virtual'.  */
19865       switch (token->keyword)
19866         {
19867         case RID_VIRTUAL:
19868           /* If `virtual' appears more than once, issue an error.  */
19869           if (virtual_p && !duplicate_virtual_error_issued_p)
19870             {
19871               cp_parser_error (parser,
19872                                "%<virtual%> specified more than once in base-specified");
19873               duplicate_virtual_error_issued_p = true;
19874             }
19875
19876           virtual_p = true;
19877
19878           /* Consume the `virtual' token.  */
19879           cp_lexer_consume_token (parser->lexer);
19880
19881           break;
19882
19883         case RID_PUBLIC:
19884         case RID_PROTECTED:
19885         case RID_PRIVATE:
19886           /* If more than one access specifier appears, issue an
19887              error.  */
19888           if (access != access_default_node
19889               && !duplicate_access_error_issued_p)
19890             {
19891               cp_parser_error (parser,
19892                                "more than one access specifier in base-specified");
19893               duplicate_access_error_issued_p = true;
19894             }
19895
19896           access = ridpointers[(int) token->keyword];
19897
19898           /* Consume the access-specifier.  */
19899           cp_lexer_consume_token (parser->lexer);
19900
19901           break;
19902
19903         default:
19904           done = true;
19905           break;
19906         }
19907     }
19908   /* It is not uncommon to see programs mechanically, erroneously, use
19909      the 'typename' keyword to denote (dependent) qualified types
19910      as base classes.  */
19911   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19912     {
19913       token = cp_lexer_peek_token (parser->lexer);
19914       if (!processing_template_decl)
19915         error_at (token->location,
19916                   "keyword %<typename%> not allowed outside of templates");
19917       else
19918         error_at (token->location,
19919                   "keyword %<typename%> not allowed in this context "
19920                   "(the base class is implicitly a type)");
19921       cp_lexer_consume_token (parser->lexer);
19922     }
19923
19924   /* Look for the optional `::' operator.  */
19925   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19926   /* Look for the nested-name-specifier.  The simplest way to
19927      implement:
19928
19929        [temp.res]
19930
19931        The keyword `typename' is not permitted in a base-specifier or
19932        mem-initializer; in these contexts a qualified name that
19933        depends on a template-parameter is implicitly assumed to be a
19934        type name.
19935
19936      is to pretend that we have seen the `typename' keyword at this
19937      point.  */
19938   cp_parser_nested_name_specifier_opt (parser,
19939                                        /*typename_keyword_p=*/true,
19940                                        /*check_dependency_p=*/true,
19941                                        typename_type,
19942                                        /*is_declaration=*/true);
19943   /* If the base class is given by a qualified name, assume that names
19944      we see are type names or templates, as appropriate.  */
19945   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19946   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19947
19948   if (!parser->scope
19949       && cp_lexer_next_token_is_decltype (parser->lexer))
19950     /* DR 950 allows decltype as a base-specifier.  */
19951     type = cp_parser_decltype (parser);
19952   else
19953     {
19954       /* Otherwise, look for the class-name.  */
19955       type = cp_parser_class_name (parser,
19956                                    class_scope_p,
19957                                    template_p,
19958                                    typename_type,
19959                                    /*check_dependency_p=*/true,
19960                                    /*class_head_p=*/false,
19961                                    /*is_declaration=*/true);
19962       type = TREE_TYPE (type);
19963     }
19964
19965   if (type == error_mark_node)
19966     return error_mark_node;
19967
19968   return finish_base_specifier (type, access, virtual_p);
19969 }
19970
19971 /* Exception handling [gram.exception] */
19972
19973 /* Parse an (optional) noexcept-specification.
19974
19975    noexcept-specification:
19976      noexcept ( constant-expression ) [opt]
19977
19978    If no noexcept-specification is present, returns NULL_TREE.
19979    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19980    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19981    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19982    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19983    in which case a boolean condition is returned instead.  */
19984
19985 static tree
19986 cp_parser_noexcept_specification_opt (cp_parser* parser,
19987                                       bool require_constexpr,
19988                                       bool* consumed_expr,
19989                                       bool return_cond)
19990 {
19991   cp_token *token;
19992   const char *saved_message;
19993
19994   /* Peek at the next token.  */
19995   token = cp_lexer_peek_token (parser->lexer);
19996
19997   /* Is it a noexcept-specification?  */
19998   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19999     {
20000       tree expr;
20001       cp_lexer_consume_token (parser->lexer);
20002
20003       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20004         {
20005           cp_lexer_consume_token (parser->lexer);
20006
20007           if (require_constexpr)
20008             {
20009               /* Types may not be defined in an exception-specification.  */
20010               saved_message = parser->type_definition_forbidden_message;
20011               parser->type_definition_forbidden_message
20012               = G_("types may not be defined in an exception-specification");
20013
20014               expr = cp_parser_constant_expression (parser, false, NULL);
20015
20016               /* Restore the saved message.  */
20017               parser->type_definition_forbidden_message = saved_message;
20018             }
20019           else
20020             {
20021               expr = cp_parser_expression (parser, false, NULL);
20022               *consumed_expr = true;
20023             }
20024
20025           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20026         }
20027       else
20028         {
20029           expr = boolean_true_node;
20030           if (!require_constexpr)
20031             *consumed_expr = false;
20032         }
20033
20034       /* We cannot build a noexcept-spec right away because this will check
20035          that expr is a constexpr.  */
20036       if (!return_cond)
20037         return build_noexcept_spec (expr, tf_warning_or_error);
20038       else
20039         return expr;
20040     }
20041   else
20042     return NULL_TREE;
20043 }
20044
20045 /* Parse an (optional) exception-specification.
20046
20047    exception-specification:
20048      throw ( type-id-list [opt] )
20049
20050    Returns a TREE_LIST representing the exception-specification.  The
20051    TREE_VALUE of each node is a type.  */
20052
20053 static tree
20054 cp_parser_exception_specification_opt (cp_parser* parser)
20055 {
20056   cp_token *token;
20057   tree type_id_list;
20058   const char *saved_message;
20059
20060   /* Peek at the next token.  */
20061   token = cp_lexer_peek_token (parser->lexer);
20062
20063   /* Is it a noexcept-specification?  */
20064   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20065                                                       false);
20066   if (type_id_list != NULL_TREE)
20067     return type_id_list;
20068
20069   /* If it's not `throw', then there's no exception-specification.  */
20070   if (!cp_parser_is_keyword (token, RID_THROW))
20071     return NULL_TREE;
20072
20073 #if 0
20074   /* Enable this once a lot of code has transitioned to noexcept?  */
20075   if (cxx_dialect >= cxx0x && !in_system_header)
20076     warning (OPT_Wdeprecated, "dynamic exception specifications are "
20077              "deprecated in C++0x; use %<noexcept%> instead");
20078 #endif
20079
20080   /* Consume the `throw'.  */
20081   cp_lexer_consume_token (parser->lexer);
20082
20083   /* Look for the `('.  */
20084   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20085
20086   /* Peek at the next token.  */
20087   token = cp_lexer_peek_token (parser->lexer);
20088   /* If it's not a `)', then there is a type-id-list.  */
20089   if (token->type != CPP_CLOSE_PAREN)
20090     {
20091       /* Types may not be defined in an exception-specification.  */
20092       saved_message = parser->type_definition_forbidden_message;
20093       parser->type_definition_forbidden_message
20094         = G_("types may not be defined in an exception-specification");
20095       /* Parse the type-id-list.  */
20096       type_id_list = cp_parser_type_id_list (parser);
20097       /* Restore the saved message.  */
20098       parser->type_definition_forbidden_message = saved_message;
20099     }
20100   else
20101     type_id_list = empty_except_spec;
20102
20103   /* Look for the `)'.  */
20104   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20105
20106   return type_id_list;
20107 }
20108
20109 /* Parse an (optional) type-id-list.
20110
20111    type-id-list:
20112      type-id ... [opt]
20113      type-id-list , type-id ... [opt]
20114
20115    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
20116    in the order that the types were presented.  */
20117
20118 static tree
20119 cp_parser_type_id_list (cp_parser* parser)
20120 {
20121   tree types = NULL_TREE;
20122
20123   while (true)
20124     {
20125       cp_token *token;
20126       tree type;
20127
20128       /* Get the next type-id.  */
20129       type = cp_parser_type_id (parser);
20130       /* Parse the optional ellipsis. */
20131       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20132         {
20133           /* Consume the `...'. */
20134           cp_lexer_consume_token (parser->lexer);
20135
20136           /* Turn the type into a pack expansion expression. */
20137           type = make_pack_expansion (type);
20138         }
20139       /* Add it to the list.  */
20140       types = add_exception_specifier (types, type, /*complain=*/1);
20141       /* Peek at the next token.  */
20142       token = cp_lexer_peek_token (parser->lexer);
20143       /* If it is not a `,', we are done.  */
20144       if (token->type != CPP_COMMA)
20145         break;
20146       /* Consume the `,'.  */
20147       cp_lexer_consume_token (parser->lexer);
20148     }
20149
20150   return nreverse (types);
20151 }
20152
20153 /* Parse a try-block.
20154
20155    try-block:
20156      try compound-statement handler-seq  */
20157
20158 static tree
20159 cp_parser_try_block (cp_parser* parser)
20160 {
20161   tree try_block;
20162
20163   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20164   try_block = begin_try_block ();
20165   cp_parser_compound_statement (parser, NULL, true, false);
20166   finish_try_block (try_block);
20167   cp_parser_handler_seq (parser);
20168   finish_handler_sequence (try_block);
20169
20170   return try_block;
20171 }
20172
20173 /* Parse a function-try-block.
20174
20175    function-try-block:
20176      try ctor-initializer [opt] function-body handler-seq  */
20177
20178 static bool
20179 cp_parser_function_try_block (cp_parser* parser)
20180 {
20181   tree compound_stmt;
20182   tree try_block;
20183   bool ctor_initializer_p;
20184
20185   /* Look for the `try' keyword.  */
20186   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20187     return false;
20188   /* Let the rest of the front end know where we are.  */
20189   try_block = begin_function_try_block (&compound_stmt);
20190   /* Parse the function-body.  */
20191   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20192     (parser, /*in_function_try_block=*/true);
20193   /* We're done with the `try' part.  */
20194   finish_function_try_block (try_block);
20195   /* Parse the handlers.  */
20196   cp_parser_handler_seq (parser);
20197   /* We're done with the handlers.  */
20198   finish_function_handler_sequence (try_block, compound_stmt);
20199
20200   return ctor_initializer_p;
20201 }
20202
20203 /* Parse a handler-seq.
20204
20205    handler-seq:
20206      handler handler-seq [opt]  */
20207
20208 static void
20209 cp_parser_handler_seq (cp_parser* parser)
20210 {
20211   while (true)
20212     {
20213       cp_token *token;
20214
20215       /* Parse the handler.  */
20216       cp_parser_handler (parser);
20217       /* Peek at the next token.  */
20218       token = cp_lexer_peek_token (parser->lexer);
20219       /* If it's not `catch' then there are no more handlers.  */
20220       if (!cp_parser_is_keyword (token, RID_CATCH))
20221         break;
20222     }
20223 }
20224
20225 /* Parse a handler.
20226
20227    handler:
20228      catch ( exception-declaration ) compound-statement  */
20229
20230 static void
20231 cp_parser_handler (cp_parser* parser)
20232 {
20233   tree handler;
20234   tree declaration;
20235
20236   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20237   handler = begin_handler ();
20238   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20239   declaration = cp_parser_exception_declaration (parser);
20240   finish_handler_parms (declaration, handler);
20241   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20242   cp_parser_compound_statement (parser, NULL, false, false);
20243   finish_handler (handler);
20244 }
20245
20246 /* Parse an exception-declaration.
20247
20248    exception-declaration:
20249      type-specifier-seq declarator
20250      type-specifier-seq abstract-declarator
20251      type-specifier-seq
20252      ...
20253
20254    Returns a VAR_DECL for the declaration, or NULL_TREE if the
20255    ellipsis variant is used.  */
20256
20257 static tree
20258 cp_parser_exception_declaration (cp_parser* parser)
20259 {
20260   cp_decl_specifier_seq type_specifiers;
20261   cp_declarator *declarator;
20262   const char *saved_message;
20263
20264   /* If it's an ellipsis, it's easy to handle.  */
20265   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20266     {
20267       /* Consume the `...' token.  */
20268       cp_lexer_consume_token (parser->lexer);
20269       return NULL_TREE;
20270     }
20271
20272   /* Types may not be defined in exception-declarations.  */
20273   saved_message = parser->type_definition_forbidden_message;
20274   parser->type_definition_forbidden_message
20275     = G_("types may not be defined in exception-declarations");
20276
20277   /* Parse the type-specifier-seq.  */
20278   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20279                                 /*is_trailing_return=*/false,
20280                                 &type_specifiers);
20281   /* If it's a `)', then there is no declarator.  */
20282   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20283     declarator = NULL;
20284   else
20285     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20286                                        /*ctor_dtor_or_conv_p=*/NULL,
20287                                        /*parenthesized_p=*/NULL,
20288                                        /*member_p=*/false);
20289
20290   /* Restore the saved message.  */
20291   parser->type_definition_forbidden_message = saved_message;
20292
20293   if (!type_specifiers.any_specifiers_p)
20294     return error_mark_node;
20295
20296   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20297 }
20298
20299 /* Parse a throw-expression.
20300
20301    throw-expression:
20302      throw assignment-expression [opt]
20303
20304    Returns a THROW_EXPR representing the throw-expression.  */
20305
20306 static tree
20307 cp_parser_throw_expression (cp_parser* parser)
20308 {
20309   tree expression;
20310   cp_token* token;
20311
20312   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20313   token = cp_lexer_peek_token (parser->lexer);
20314   /* Figure out whether or not there is an assignment-expression
20315      following the "throw" keyword.  */
20316   if (token->type == CPP_COMMA
20317       || token->type == CPP_SEMICOLON
20318       || token->type == CPP_CLOSE_PAREN
20319       || token->type == CPP_CLOSE_SQUARE
20320       || token->type == CPP_CLOSE_BRACE
20321       || token->type == CPP_COLON)
20322     expression = NULL_TREE;
20323   else
20324     expression = cp_parser_assignment_expression (parser,
20325                                                   /*cast_p=*/false, NULL);
20326
20327   return build_throw (expression);
20328 }
20329
20330 /* GNU Extensions */
20331
20332 /* Parse an (optional) asm-specification.
20333
20334    asm-specification:
20335      asm ( string-literal )
20336
20337    If the asm-specification is present, returns a STRING_CST
20338    corresponding to the string-literal.  Otherwise, returns
20339    NULL_TREE.  */
20340
20341 static tree
20342 cp_parser_asm_specification_opt (cp_parser* parser)
20343 {
20344   cp_token *token;
20345   tree asm_specification;
20346
20347   /* Peek at the next token.  */
20348   token = cp_lexer_peek_token (parser->lexer);
20349   /* If the next token isn't the `asm' keyword, then there's no
20350      asm-specification.  */
20351   if (!cp_parser_is_keyword (token, RID_ASM))
20352     return NULL_TREE;
20353
20354   /* Consume the `asm' token.  */
20355   cp_lexer_consume_token (parser->lexer);
20356   /* Look for the `('.  */
20357   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20358
20359   /* Look for the string-literal.  */
20360   asm_specification = cp_parser_string_literal (parser, false, false);
20361
20362   /* Look for the `)'.  */
20363   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20364
20365   return asm_specification;
20366 }
20367
20368 /* Parse an asm-operand-list.
20369
20370    asm-operand-list:
20371      asm-operand
20372      asm-operand-list , asm-operand
20373
20374    asm-operand:
20375      string-literal ( expression )
20376      [ string-literal ] string-literal ( expression )
20377
20378    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
20379    each node is the expression.  The TREE_PURPOSE is itself a
20380    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20381    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20382    is a STRING_CST for the string literal before the parenthesis. Returns
20383    ERROR_MARK_NODE if any of the operands are invalid.  */
20384
20385 static tree
20386 cp_parser_asm_operand_list (cp_parser* parser)
20387 {
20388   tree asm_operands = NULL_TREE;
20389   bool invalid_operands = false;
20390
20391   while (true)
20392     {
20393       tree string_literal;
20394       tree expression;
20395       tree name;
20396
20397       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20398         {
20399           /* Consume the `[' token.  */
20400           cp_lexer_consume_token (parser->lexer);
20401           /* Read the operand name.  */
20402           name = cp_parser_identifier (parser);
20403           if (name != error_mark_node)
20404             name = build_string (IDENTIFIER_LENGTH (name),
20405                                  IDENTIFIER_POINTER (name));
20406           /* Look for the closing `]'.  */
20407           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20408         }
20409       else
20410         name = NULL_TREE;
20411       /* Look for the string-literal.  */
20412       string_literal = cp_parser_string_literal (parser, false, false);
20413
20414       /* Look for the `('.  */
20415       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20416       /* Parse the expression.  */
20417       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20418       /* Look for the `)'.  */
20419       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20420
20421       if (name == error_mark_node 
20422           || string_literal == error_mark_node 
20423           || expression == error_mark_node)
20424         invalid_operands = true;
20425
20426       /* Add this operand to the list.  */
20427       asm_operands = tree_cons (build_tree_list (name, string_literal),
20428                                 expression,
20429                                 asm_operands);
20430       /* If the next token is not a `,', there are no more
20431          operands.  */
20432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20433         break;
20434       /* Consume the `,'.  */
20435       cp_lexer_consume_token (parser->lexer);
20436     }
20437
20438   return invalid_operands ? error_mark_node : nreverse (asm_operands);
20439 }
20440
20441 /* Parse an asm-clobber-list.
20442
20443    asm-clobber-list:
20444      string-literal
20445      asm-clobber-list , string-literal
20446
20447    Returns a TREE_LIST, indicating the clobbers in the order that they
20448    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
20449
20450 static tree
20451 cp_parser_asm_clobber_list (cp_parser* parser)
20452 {
20453   tree clobbers = NULL_TREE;
20454
20455   while (true)
20456     {
20457       tree string_literal;
20458
20459       /* Look for the string literal.  */
20460       string_literal = cp_parser_string_literal (parser, false, false);
20461       /* Add it to the list.  */
20462       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20463       /* If the next token is not a `,', then the list is
20464          complete.  */
20465       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20466         break;
20467       /* Consume the `,' token.  */
20468       cp_lexer_consume_token (parser->lexer);
20469     }
20470
20471   return clobbers;
20472 }
20473
20474 /* Parse an asm-label-list.
20475
20476    asm-label-list:
20477      identifier
20478      asm-label-list , identifier
20479
20480    Returns a TREE_LIST, indicating the labels in the order that they
20481    appeared.  The TREE_VALUE of each node is a label.  */
20482
20483 static tree
20484 cp_parser_asm_label_list (cp_parser* parser)
20485 {
20486   tree labels = NULL_TREE;
20487
20488   while (true)
20489     {
20490       tree identifier, label, name;
20491
20492       /* Look for the identifier.  */
20493       identifier = cp_parser_identifier (parser);
20494       if (!error_operand_p (identifier))
20495         {
20496           label = lookup_label (identifier);
20497           if (TREE_CODE (label) == LABEL_DECL)
20498             {
20499               TREE_USED (label) = 1;
20500               check_goto (label);
20501               name = build_string (IDENTIFIER_LENGTH (identifier),
20502                                    IDENTIFIER_POINTER (identifier));
20503               labels = tree_cons (name, label, labels);
20504             }
20505         }
20506       /* If the next token is not a `,', then the list is
20507          complete.  */
20508       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20509         break;
20510       /* Consume the `,' token.  */
20511       cp_lexer_consume_token (parser->lexer);
20512     }
20513
20514   return nreverse (labels);
20515 }
20516
20517 /* Return TRUE iff the next tokens in the stream are possibly the
20518    beginning of a GNU extension attribute. */
20519
20520 static bool
20521 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20522 {
20523   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20524 }
20525
20526 /* Return TRUE iff the next tokens in the stream are possibly the
20527    beginning of a standard C++-11 attribute specifier.  */
20528
20529 static bool
20530 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20531 {
20532   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20533 }
20534
20535 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20536    beginning of a standard C++-11 attribute specifier.  */
20537
20538 static bool
20539 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20540 {
20541   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20542
20543   return (cxx_dialect >= cxx0x
20544           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20545               || (token->type == CPP_OPEN_SQUARE
20546                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20547                   && token->type == CPP_OPEN_SQUARE)));
20548 }
20549
20550 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20551    beginning of a GNU extension attribute.  */
20552
20553 static bool
20554 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20555 {
20556   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20557
20558   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20559 }
20560
20561 /* Return true iff the next tokens can be the beginning of either a
20562    GNU attribute list, or a standard C++11 attribute sequence.  */
20563
20564 static bool
20565 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20566 {
20567   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20568           || cp_next_tokens_can_be_std_attribute_p (parser));
20569 }
20570
20571 /* Return true iff the next Nth tokens can be the beginning of either
20572    a GNU attribute list, or a standard C++11 attribute sequence.  */
20573
20574 static bool
20575 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20576 {
20577   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20578           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20579 }
20580
20581 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20582    of GNU attributes, or return NULL.  */
20583
20584 static tree
20585 cp_parser_attributes_opt (cp_parser *parser)
20586 {
20587   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20588       return cp_parser_gnu_attributes_opt (parser);
20589   return cp_parser_std_attribute_spec_seq (parser);
20590 }
20591
20592 /* Parse an (optional) series of attributes.
20593
20594    attributes:
20595      attributes attribute
20596
20597    attribute:
20598      __attribute__ (( attribute-list [opt] ))
20599
20600    The return value is as for cp_parser_gnu_attribute_list.  */
20601
20602 static tree
20603 cp_parser_gnu_attributes_opt (cp_parser* parser)
20604 {
20605   tree attributes = NULL_TREE;
20606
20607   while (true)
20608     {
20609       cp_token *token;
20610       tree attribute_list;
20611       bool ok = true;
20612
20613       /* Peek at the next token.  */
20614       token = cp_lexer_peek_token (parser->lexer);
20615       /* If it's not `__attribute__', then we're done.  */
20616       if (token->keyword != RID_ATTRIBUTE)
20617         break;
20618
20619       /* Consume the `__attribute__' keyword.  */
20620       cp_lexer_consume_token (parser->lexer);
20621       /* Look for the two `(' tokens.  */
20622       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20623       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20624
20625       /* Peek at the next token.  */
20626       token = cp_lexer_peek_token (parser->lexer);
20627       if (token->type != CPP_CLOSE_PAREN)
20628         /* Parse the attribute-list.  */
20629         attribute_list = cp_parser_gnu_attribute_list (parser);
20630       else
20631         /* If the next token is a `)', then there is no attribute
20632            list.  */
20633         attribute_list = NULL;
20634
20635       /* Look for the two `)' tokens.  */
20636       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20637         ok = false;
20638       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20639         ok = false;
20640       if (!ok)
20641         cp_parser_skip_to_end_of_statement (parser);
20642
20643       /* Add these new attributes to the list.  */
20644       attributes = chainon (attributes, attribute_list);
20645     }
20646
20647   return attributes;
20648 }
20649
20650 /* Parse a GNU attribute-list.
20651
20652    attribute-list:
20653      attribute
20654      attribute-list , attribute
20655
20656    attribute:
20657      identifier
20658      identifier ( identifier )
20659      identifier ( identifier , expression-list )
20660      identifier ( expression-list )
20661
20662    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20663    to an attribute.  The TREE_PURPOSE of each node is the identifier
20664    indicating which attribute is in use.  The TREE_VALUE represents
20665    the arguments, if any.  */
20666
20667 static tree
20668 cp_parser_gnu_attribute_list (cp_parser* parser)
20669 {
20670   tree attribute_list = NULL_TREE;
20671   bool save_translate_strings_p = parser->translate_strings_p;
20672
20673   parser->translate_strings_p = false;
20674   while (true)
20675     {
20676       cp_token *token;
20677       tree identifier;
20678       tree attribute;
20679
20680       /* Look for the identifier.  We also allow keywords here; for
20681          example `__attribute__ ((const))' is legal.  */
20682       token = cp_lexer_peek_token (parser->lexer);
20683       if (token->type == CPP_NAME
20684           || token->type == CPP_KEYWORD)
20685         {
20686           tree arguments = NULL_TREE;
20687
20688           /* Consume the token.  */
20689           token = cp_lexer_consume_token (parser->lexer);
20690
20691           /* Save away the identifier that indicates which attribute
20692              this is.  */
20693           identifier = (token->type == CPP_KEYWORD) 
20694             /* For keywords, use the canonical spelling, not the
20695                parsed identifier.  */
20696             ? ridpointers[(int) token->keyword]
20697             : token->u.value;
20698           
20699           attribute = build_tree_list (identifier, NULL_TREE);
20700
20701           /* Peek at the next token.  */
20702           token = cp_lexer_peek_token (parser->lexer);
20703           /* If it's an `(', then parse the attribute arguments.  */
20704           if (token->type == CPP_OPEN_PAREN)
20705             {
20706               vec<tree, va_gc> *vec;
20707               int attr_flag = (attribute_takes_identifier_p (identifier)
20708                                ? id_attr : normal_attr);
20709               vec = cp_parser_parenthesized_expression_list
20710                     (parser, attr_flag, /*cast_p=*/false,
20711                      /*allow_expansion_p=*/false,
20712                      /*non_constant_p=*/NULL);
20713               if (vec == NULL)
20714                 arguments = error_mark_node;
20715               else
20716                 {
20717                   arguments = build_tree_list_vec (vec);
20718                   release_tree_vector (vec);
20719                 }
20720               /* Save the arguments away.  */
20721               TREE_VALUE (attribute) = arguments;
20722             }
20723
20724           if (arguments != error_mark_node)
20725             {
20726               /* Add this attribute to the list.  */
20727               TREE_CHAIN (attribute) = attribute_list;
20728               attribute_list = attribute;
20729             }
20730
20731           token = cp_lexer_peek_token (parser->lexer);
20732         }
20733       /* Now, look for more attributes.  If the next token isn't a
20734          `,', we're done.  */
20735       if (token->type != CPP_COMMA)
20736         break;
20737
20738       /* Consume the comma and keep going.  */
20739       cp_lexer_consume_token (parser->lexer);
20740     }
20741   parser->translate_strings_p = save_translate_strings_p;
20742
20743   /* We built up the list in reverse order.  */
20744   return nreverse (attribute_list);
20745 }
20746
20747 /*  Parse a standard C++11 attribute.
20748
20749     The returned representation is a TREE_LIST which TREE_PURPOSE is
20750     the scoped name of the attribute, and the TREE_VALUE is its
20751     arguments list.
20752
20753     Note that the scoped name of the attribute is itself a TREE_LIST
20754     which TREE_PURPOSE is the namespace of the attribute, and
20755     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
20756     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20757     and which TREE_PURPOSE is directly the attribute name.
20758
20759     Clients of the attribute code should use get_attribute_namespace
20760     and get_attribute_name to get the actual namespace and name of
20761     attributes, regardless of their being GNU or C++11 attributes.
20762
20763     attribute:
20764       attribute-token attribute-argument-clause [opt]
20765
20766     attribute-token:
20767       identifier
20768       attribute-scoped-token
20769
20770     attribute-scoped-token:
20771       attribute-namespace :: identifier
20772
20773     attribute-namespace:
20774       identifier
20775
20776     attribute-argument-clause:
20777       ( balanced-token-seq )
20778
20779     balanced-token-seq:
20780       balanced-token [opt]
20781       balanced-token-seq balanced-token
20782
20783     balanced-token:
20784       ( balanced-token-seq )
20785       [ balanced-token-seq ]
20786       { balanced-token-seq }.  */
20787
20788 static tree
20789 cp_parser_std_attribute (cp_parser *parser)
20790 {
20791   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20792   cp_token *token;
20793
20794   /* First, parse name of the the attribute, a.k.a
20795      attribute-token.  */
20796
20797   token = cp_lexer_peek_token (parser->lexer);
20798   if (token->type == CPP_NAME)
20799     attr_id = token->u.value;
20800   else if (token->type == CPP_KEYWORD)
20801     attr_id = ridpointers[(int) token->keyword];
20802   else if (token->flags & NAMED_OP)
20803     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20804
20805   if (attr_id == NULL_TREE)
20806     return NULL_TREE;
20807
20808   cp_lexer_consume_token (parser->lexer);
20809
20810   token = cp_lexer_peek_token (parser->lexer);
20811   if (token->type == CPP_SCOPE)
20812     {
20813       /* We are seeing a scoped attribute token.  */
20814
20815       cp_lexer_consume_token (parser->lexer);
20816       attr_ns = attr_id;
20817
20818       token = cp_lexer_consume_token (parser->lexer);
20819       if (token->type == CPP_NAME)
20820         attr_id = token->u.value;
20821       else if (token->type == CPP_KEYWORD)
20822         attr_id = ridpointers[(int) token->keyword];
20823       else
20824         {
20825           error_at (token->location,
20826                     "expected an identifier for the attribute name");
20827           return error_mark_node;
20828         }
20829       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20830                                    NULL_TREE);
20831       token = cp_lexer_peek_token (parser->lexer);
20832     }
20833   else
20834     {
20835       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20836                                    NULL_TREE);
20837       /* C++11 noreturn attribute is equivalent to GNU's.  */
20838       if (is_attribute_p ("noreturn", attr_id))
20839         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
20840     }
20841
20842   /* Now parse the optional argument clause of the attribute.  */
20843
20844   if (token->type != CPP_OPEN_PAREN)
20845     return attribute;
20846
20847   {
20848     vec<tree, va_gc> *vec;
20849     int attr_flag = normal_attr;
20850
20851     if (attr_ns == get_identifier ("gnu")
20852         && attribute_takes_identifier_p (attr_id))
20853       /* A GNU attribute that takes an identifier in parameter.  */
20854       attr_flag = id_attr;
20855
20856     vec = cp_parser_parenthesized_expression_list
20857       (parser, attr_flag, /*cast_p=*/false,
20858        /*allow_expansion_p=*/true,
20859        /*non_constant_p=*/NULL);
20860     if (vec == NULL)
20861       arguments = error_mark_node;
20862     else
20863       {
20864         arguments = build_tree_list_vec (vec);
20865         release_tree_vector (vec);
20866       }
20867
20868     if (arguments == error_mark_node)
20869       attribute = error_mark_node;
20870     else
20871       TREE_VALUE (attribute) = arguments;
20872   }
20873
20874   return attribute;
20875 }
20876
20877 /* Parse a list of standard C++-11 attributes.
20878
20879    attribute-list:
20880      attribute [opt]
20881      attribute-list , attribute[opt]
20882      attribute ...
20883      attribute-list , attribute ...
20884 */
20885
20886 static tree
20887 cp_parser_std_attribute_list (cp_parser *parser)
20888 {
20889   tree attributes = NULL_TREE, attribute = NULL_TREE;
20890   cp_token *token = NULL;
20891
20892   while (true)
20893     {
20894       attribute = cp_parser_std_attribute (parser);
20895       if (attribute == error_mark_node)
20896         break;
20897       if (attribute != NULL_TREE)
20898         {
20899           TREE_CHAIN (attribute) = attributes;
20900           attributes = attribute;
20901         }
20902       token = cp_lexer_peek_token (parser->lexer);
20903       if (token->type != CPP_COMMA)
20904         break;
20905       cp_lexer_consume_token (parser->lexer);
20906     }
20907   attributes = nreverse (attributes);
20908   return attributes;
20909 }
20910
20911 /* Parse a standard C++-11 attribute specifier.
20912
20913    attribute-specifier:
20914      [ [ attribute-list ] ]
20915      alignment-specifier
20916
20917    alignment-specifier:
20918      alignas ( type-id ... [opt] )
20919      alignas ( alignment-expression ... [opt] ).  */
20920
20921 static tree
20922 cp_parser_std_attribute_spec (cp_parser *parser)
20923 {
20924   tree attributes = NULL_TREE;
20925   cp_token *token = cp_lexer_peek_token (parser->lexer);
20926
20927   if (token->type == CPP_OPEN_SQUARE
20928       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20929     {
20930       cp_lexer_consume_token (parser->lexer);
20931       cp_lexer_consume_token (parser->lexer);
20932
20933       attributes = cp_parser_std_attribute_list (parser);
20934
20935       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20936           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20937         cp_parser_skip_to_end_of_statement (parser);
20938       else
20939         /* Warn about parsing c++11 attribute in non-c++1 mode, only
20940            when we are sure that we have actually parsed them.  */
20941         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20942     }
20943   else
20944     {
20945       tree alignas_expr;
20946
20947       /* Look for an alignment-specifier.  */
20948
20949       token = cp_lexer_peek_token (parser->lexer);
20950
20951       if (token->type != CPP_KEYWORD
20952           || token->keyword != RID_ALIGNAS)
20953         return NULL_TREE;
20954
20955       cp_lexer_consume_token (parser->lexer);
20956       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20957
20958       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20959         {
20960           cp_parser_error (parser, "expected %<(%>");
20961           return error_mark_node;
20962         }
20963
20964       cp_parser_parse_tentatively (parser);
20965       alignas_expr = cp_parser_type_id (parser);
20966
20967       if (!cp_parser_parse_definitely (parser))
20968         {
20969           gcc_assert (alignas_expr == error_mark_node
20970                       || alignas_expr == NULL_TREE);
20971
20972           alignas_expr =
20973             cp_parser_assignment_expression (parser, /*cast_p=*/false,
20974                                              /**cp_id_kind=*/NULL);
20975           if (alignas_expr == NULL_TREE
20976               || alignas_expr == error_mark_node)
20977             return alignas_expr;
20978         }
20979
20980       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20981         {
20982           cp_parser_error (parser, "expected %<)%>");
20983           return error_mark_node;
20984         }
20985
20986       alignas_expr = cxx_alignas_expr (alignas_expr);
20987
20988       /* Build the C++-11 representation of an 'aligned'
20989          attribute.  */
20990       attributes =
20991         build_tree_list (build_tree_list (get_identifier ("gnu"),
20992                                           get_identifier ("aligned")),
20993                          build_tree_list (NULL_TREE, alignas_expr));
20994     }
20995
20996   return attributes;
20997 }
20998
20999 /* Parse a standard C++-11 attribute-specifier-seq.
21000
21001    attribute-specifier-seq:
21002      attribute-specifier-seq [opt] attribute-specifier
21003  */
21004
21005 static tree
21006 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21007 {
21008   tree attr_specs = NULL;
21009
21010   while (true)
21011     {
21012       tree attr_spec = cp_parser_std_attribute_spec (parser);
21013       if (attr_spec == NULL_TREE)
21014         break;
21015       if (attr_spec == error_mark_node)
21016         return error_mark_node;
21017
21018       TREE_CHAIN (attr_spec) = attr_specs;
21019       attr_specs = attr_spec;
21020     }
21021
21022   attr_specs = nreverse (attr_specs);
21023   return attr_specs;
21024 }
21025
21026 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
21027    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
21028    current value of the PEDANTIC flag, regardless of whether or not
21029    the `__extension__' keyword is present.  The caller is responsible
21030    for restoring the value of the PEDANTIC flag.  */
21031
21032 static bool
21033 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21034 {
21035   /* Save the old value of the PEDANTIC flag.  */
21036   *saved_pedantic = pedantic;
21037
21038   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21039     {
21040       /* Consume the `__extension__' token.  */
21041       cp_lexer_consume_token (parser->lexer);
21042       /* We're not being pedantic while the `__extension__' keyword is
21043          in effect.  */
21044       pedantic = 0;
21045
21046       return true;
21047     }
21048
21049   return false;
21050 }
21051
21052 /* Parse a label declaration.
21053
21054    label-declaration:
21055      __label__ label-declarator-seq ;
21056
21057    label-declarator-seq:
21058      identifier , label-declarator-seq
21059      identifier  */
21060
21061 static void
21062 cp_parser_label_declaration (cp_parser* parser)
21063 {
21064   /* Look for the `__label__' keyword.  */
21065   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21066
21067   while (true)
21068     {
21069       tree identifier;
21070
21071       /* Look for an identifier.  */
21072       identifier = cp_parser_identifier (parser);
21073       /* If we failed, stop.  */
21074       if (identifier == error_mark_node)
21075         break;
21076       /* Declare it as a label.  */
21077       finish_label_decl (identifier);
21078       /* If the next token is a `;', stop.  */
21079       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21080         break;
21081       /* Look for the `,' separating the label declarations.  */
21082       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21083     }
21084
21085   /* Look for the final `;'.  */
21086   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21087 }
21088
21089 /* Support Functions */
21090
21091 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21092    NAME should have one of the representations used for an
21093    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21094    is returned.  If PARSER->SCOPE is a dependent type, then a
21095    SCOPE_REF is returned.
21096
21097    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21098    returned; the name was already resolved when the TEMPLATE_ID_EXPR
21099    was formed.  Abstractly, such entities should not be passed to this
21100    function, because they do not need to be looked up, but it is
21101    simpler to check for this special case here, rather than at the
21102    call-sites.
21103
21104    In cases not explicitly covered above, this function returns a
21105    DECL, OVERLOAD, or baselink representing the result of the lookup.
21106    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21107    is returned.
21108
21109    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21110    (e.g., "struct") that was used.  In that case bindings that do not
21111    refer to types are ignored.
21112
21113    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21114    ignored.
21115
21116    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21117    are ignored.
21118
21119    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21120    types.
21121
21122    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21123    TREE_LIST of candidates if name-lookup results in an ambiguity, and
21124    NULL_TREE otherwise.  */
21125
21126 static tree
21127 cp_parser_lookup_name (cp_parser *parser, tree name,
21128                        enum tag_types tag_type,
21129                        bool is_template,
21130                        bool is_namespace,
21131                        bool check_dependency,
21132                        tree *ambiguous_decls,
21133                        location_t name_location)
21134 {
21135   tree decl;
21136   tree object_type = parser->context->object_type;
21137
21138   /* Assume that the lookup will be unambiguous.  */
21139   if (ambiguous_decls)
21140     *ambiguous_decls = NULL_TREE;
21141
21142   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21143      no longer valid.  Note that if we are parsing tentatively, and
21144      the parse fails, OBJECT_TYPE will be automatically restored.  */
21145   parser->context->object_type = NULL_TREE;
21146
21147   if (name == error_mark_node)
21148     return error_mark_node;
21149
21150   /* A template-id has already been resolved; there is no lookup to
21151      do.  */
21152   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21153     return name;
21154   if (BASELINK_P (name))
21155     {
21156       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21157                   == TEMPLATE_ID_EXPR);
21158       return name;
21159     }
21160
21161   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
21162      it should already have been checked to make sure that the name
21163      used matches the type being destroyed.  */
21164   if (TREE_CODE (name) == BIT_NOT_EXPR)
21165     {
21166       tree type;
21167
21168       /* Figure out to which type this destructor applies.  */
21169       if (parser->scope)
21170         type = parser->scope;
21171       else if (object_type)
21172         type = object_type;
21173       else
21174         type = current_class_type;
21175       /* If that's not a class type, there is no destructor.  */
21176       if (!type || !CLASS_TYPE_P (type))
21177         return error_mark_node;
21178       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21179         lazily_declare_fn (sfk_destructor, type);
21180       if (!CLASSTYPE_DESTRUCTORS (type))
21181           return error_mark_node;
21182       /* If it was a class type, return the destructor.  */
21183       return CLASSTYPE_DESTRUCTORS (type);
21184     }
21185
21186   /* By this point, the NAME should be an ordinary identifier.  If
21187      the id-expression was a qualified name, the qualifying scope is
21188      stored in PARSER->SCOPE at this point.  */
21189   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21190
21191   /* Perform the lookup.  */
21192   if (parser->scope)
21193     {
21194       bool dependent_p;
21195
21196       if (parser->scope == error_mark_node)
21197         return error_mark_node;
21198
21199       /* If the SCOPE is dependent, the lookup must be deferred until
21200          the template is instantiated -- unless we are explicitly
21201          looking up names in uninstantiated templates.  Even then, we
21202          cannot look up the name if the scope is not a class type; it
21203          might, for example, be a template type parameter.  */
21204       dependent_p = (TYPE_P (parser->scope)
21205                      && dependent_scope_p (parser->scope));
21206       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21207           && dependent_p)
21208         /* Defer lookup.  */
21209         decl = error_mark_node;
21210       else
21211         {
21212           tree pushed_scope = NULL_TREE;
21213
21214           /* If PARSER->SCOPE is a dependent type, then it must be a
21215              class type, and we must not be checking dependencies;
21216              otherwise, we would have processed this lookup above.  So
21217              that PARSER->SCOPE is not considered a dependent base by
21218              lookup_member, we must enter the scope here.  */
21219           if (dependent_p)
21220             pushed_scope = push_scope (parser->scope);
21221
21222           /* If the PARSER->SCOPE is a template specialization, it
21223              may be instantiated during name lookup.  In that case,
21224              errors may be issued.  Even if we rollback the current
21225              tentative parse, those errors are valid.  */
21226           decl = lookup_qualified_name (parser->scope, name,
21227                                         tag_type != none_type,
21228                                         /*complain=*/true);
21229
21230           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21231              lookup result and the nested-name-specifier nominates a class C:
21232                * if the name specified after the nested-name-specifier, when
21233                looked up in C, is the injected-class-name of C (Clause 9), or
21234                * if the name specified after the nested-name-specifier is the
21235                same as the identifier or the simple-template-id's template-
21236                name in the last component of the nested-name-specifier,
21237              the name is instead considered to name the constructor of
21238              class C. [ Note: for example, the constructor is not an
21239              acceptable lookup result in an elaborated-type-specifier so
21240              the constructor would not be used in place of the
21241              injected-class-name. --end note ] Such a constructor name
21242              shall be used only in the declarator-id of a declaration that
21243              names a constructor or in a using-declaration.  */
21244           if (tag_type == none_type
21245               && DECL_SELF_REFERENCE_P (decl)
21246               && same_type_p (DECL_CONTEXT (decl), parser->scope))
21247             decl = lookup_qualified_name (parser->scope, ctor_identifier,
21248                                           tag_type != none_type,
21249                                           /*complain=*/true);
21250
21251           /* If we have a single function from a using decl, pull it out.  */
21252           if (TREE_CODE (decl) == OVERLOAD
21253               && !really_overloaded_fn (decl))
21254             decl = OVL_FUNCTION (decl);
21255
21256           if (pushed_scope)
21257             pop_scope (pushed_scope);
21258         }
21259
21260       /* If the scope is a dependent type and either we deferred lookup or
21261          we did lookup but didn't find the name, rememeber the name.  */
21262       if (decl == error_mark_node && TYPE_P (parser->scope)
21263           && dependent_type_p (parser->scope))
21264         {
21265           if (tag_type)
21266             {
21267               tree type;
21268
21269               /* The resolution to Core Issue 180 says that `struct
21270                  A::B' should be considered a type-name, even if `A'
21271                  is dependent.  */
21272               type = make_typename_type (parser->scope, name, tag_type,
21273                                          /*complain=*/tf_error);
21274               decl = TYPE_NAME (type);
21275             }
21276           else if (is_template
21277                    && (cp_parser_next_token_ends_template_argument_p (parser)
21278                        || cp_lexer_next_token_is (parser->lexer,
21279                                                   CPP_CLOSE_PAREN)))
21280             decl = make_unbound_class_template (parser->scope,
21281                                                 name, NULL_TREE,
21282                                                 /*complain=*/tf_error);
21283           else
21284             decl = build_qualified_name (/*type=*/NULL_TREE,
21285                                          parser->scope, name,
21286                                          is_template);
21287         }
21288       parser->qualifying_scope = parser->scope;
21289       parser->object_scope = NULL_TREE;
21290     }
21291   else if (object_type)
21292     {
21293       tree object_decl = NULL_TREE;
21294       /* Look up the name in the scope of the OBJECT_TYPE, unless the
21295          OBJECT_TYPE is not a class.  */
21296       if (CLASS_TYPE_P (object_type))
21297         /* If the OBJECT_TYPE is a template specialization, it may
21298            be instantiated during name lookup.  In that case, errors
21299            may be issued.  Even if we rollback the current tentative
21300            parse, those errors are valid.  */
21301         object_decl = lookup_member (object_type,
21302                                      name,
21303                                      /*protect=*/0,
21304                                      tag_type != none_type,
21305                                      tf_warning_or_error);
21306       /* Look it up in the enclosing context, too.  */
21307       decl = lookup_name_real (name, tag_type != none_type,
21308                                /*nonclass=*/0,
21309                                /*block_p=*/true, is_namespace, 0);
21310       parser->object_scope = object_type;
21311       parser->qualifying_scope = NULL_TREE;
21312       if (object_decl)
21313         decl = object_decl;
21314     }
21315   else
21316     {
21317       decl = lookup_name_real (name, tag_type != none_type,
21318                                /*nonclass=*/0,
21319                                /*block_p=*/true, is_namespace, 0);
21320       parser->qualifying_scope = NULL_TREE;
21321       parser->object_scope = NULL_TREE;
21322     }
21323
21324   /* If the lookup failed, let our caller know.  */
21325   if (!decl || decl == error_mark_node)
21326     return error_mark_node;
21327
21328   /* Pull out the template from an injected-class-name (or multiple).  */
21329   if (is_template)
21330     decl = maybe_get_template_decl_from_type_decl (decl);
21331
21332   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
21333   if (TREE_CODE (decl) == TREE_LIST)
21334     {
21335       if (ambiguous_decls)
21336         *ambiguous_decls = decl;
21337       /* The error message we have to print is too complicated for
21338          cp_parser_error, so we incorporate its actions directly.  */
21339       if (!cp_parser_simulate_error (parser))
21340         {
21341           error_at (name_location, "reference to %qD is ambiguous",
21342                     name);
21343           print_candidates (decl);
21344         }
21345       return error_mark_node;
21346     }
21347
21348   gcc_assert (DECL_P (decl)
21349               || TREE_CODE (decl) == OVERLOAD
21350               || TREE_CODE (decl) == SCOPE_REF
21351               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21352               || BASELINK_P (decl));
21353
21354   /* If we have resolved the name of a member declaration, check to
21355      see if the declaration is accessible.  When the name resolves to
21356      set of overloaded functions, accessibility is checked when
21357      overload resolution is done.
21358
21359      During an explicit instantiation, access is not checked at all,
21360      as per [temp.explicit].  */
21361   if (DECL_P (decl))
21362     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21363
21364   maybe_record_typedef_use (decl);
21365
21366   return decl;
21367 }
21368
21369 /* Like cp_parser_lookup_name, but for use in the typical case where
21370    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21371    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
21372
21373 static tree
21374 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21375 {
21376   return cp_parser_lookup_name (parser, name,
21377                                 none_type,
21378                                 /*is_template=*/false,
21379                                 /*is_namespace=*/false,
21380                                 /*check_dependency=*/true,
21381                                 /*ambiguous_decls=*/NULL,
21382                                 location);
21383 }
21384
21385 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21386    the current context, return the TYPE_DECL.  If TAG_NAME_P is
21387    true, the DECL indicates the class being defined in a class-head,
21388    or declared in an elaborated-type-specifier.
21389
21390    Otherwise, return DECL.  */
21391
21392 static tree
21393 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21394 {
21395   /* If the TEMPLATE_DECL is being declared as part of a class-head,
21396      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21397
21398        struct A {
21399          template <typename T> struct B;
21400        };
21401
21402        template <typename T> struct A::B {};
21403
21404      Similarly, in an elaborated-type-specifier:
21405
21406        namespace N { struct X{}; }
21407
21408        struct A {
21409          template <typename T> friend struct N::X;
21410        };
21411
21412      However, if the DECL refers to a class type, and we are in
21413      the scope of the class, then the name lookup automatically
21414      finds the TYPE_DECL created by build_self_reference rather
21415      than a TEMPLATE_DECL.  For example, in:
21416
21417        template <class T> struct S {
21418          S s;
21419        };
21420
21421      there is no need to handle such case.  */
21422
21423   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21424     return DECL_TEMPLATE_RESULT (decl);
21425
21426   return decl;
21427 }
21428
21429 /* If too many, or too few, template-parameter lists apply to the
21430    declarator, issue an error message.  Returns TRUE if all went well,
21431    and FALSE otherwise.  */
21432
21433 static bool
21434 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21435                                                 cp_declarator *declarator,
21436                                                 location_t declarator_location)
21437 {
21438   switch (declarator->kind)
21439     {
21440     case cdk_id:
21441       {
21442         unsigned num_templates = 0;
21443         tree scope = declarator->u.id.qualifying_scope;
21444
21445         if (scope)
21446           num_templates = num_template_headers_for_class (scope);
21447         else if (TREE_CODE (declarator->u.id.unqualified_name)
21448                  == TEMPLATE_ID_EXPR)
21449           /* If the DECLARATOR has the form `X<y>' then it uses one
21450              additional level of template parameters.  */
21451           ++num_templates;
21452
21453         return cp_parser_check_template_parameters 
21454           (parser, num_templates, declarator_location, declarator);
21455       }
21456
21457     case cdk_function:
21458     case cdk_array:
21459     case cdk_pointer:
21460     case cdk_reference:
21461     case cdk_ptrmem:
21462       return (cp_parser_check_declarator_template_parameters
21463               (parser, declarator->declarator, declarator_location));
21464
21465     case cdk_error:
21466       return true;
21467
21468     default:
21469       gcc_unreachable ();
21470     }
21471   return false;
21472 }
21473
21474 /* NUM_TEMPLATES were used in the current declaration.  If that is
21475    invalid, return FALSE and issue an error messages.  Otherwise,
21476    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
21477    declarator and we can print more accurate diagnostics.  */
21478
21479 static bool
21480 cp_parser_check_template_parameters (cp_parser* parser,
21481                                      unsigned num_templates,
21482                                      location_t location,
21483                                      cp_declarator *declarator)
21484 {
21485   /* If there are the same number of template classes and parameter
21486      lists, that's OK.  */
21487   if (parser->num_template_parameter_lists == num_templates)
21488     return true;
21489   /* If there are more, but only one more, then we are referring to a
21490      member template.  That's OK too.  */
21491   if (parser->num_template_parameter_lists == num_templates + 1)
21492     return true;
21493   /* If there are more template classes than parameter lists, we have
21494      something like:
21495
21496        template <class T> void S<T>::R<T>::f ();  */
21497   if (parser->num_template_parameter_lists < num_templates)
21498     {
21499       if (declarator && !current_function_decl)
21500         error_at (location, "specializing member %<%T::%E%> "
21501                   "requires %<template<>%> syntax", 
21502                   declarator->u.id.qualifying_scope,
21503                   declarator->u.id.unqualified_name);
21504       else if (declarator)
21505         error_at (location, "invalid declaration of %<%T::%E%>",
21506                   declarator->u.id.qualifying_scope,
21507                   declarator->u.id.unqualified_name);
21508       else 
21509         error_at (location, "too few template-parameter-lists");
21510       return false;
21511     }
21512   /* Otherwise, there are too many template parameter lists.  We have
21513      something like:
21514
21515      template <class T> template <class U> void S::f();  */
21516   error_at (location, "too many template-parameter-lists");
21517   return false;
21518 }
21519
21520 /* Parse an optional `::' token indicating that the following name is
21521    from the global namespace.  If so, PARSER->SCOPE is set to the
21522    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21523    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21524    Returns the new value of PARSER->SCOPE, if the `::' token is
21525    present, and NULL_TREE otherwise.  */
21526
21527 static tree
21528 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21529 {
21530   cp_token *token;
21531
21532   /* Peek at the next token.  */
21533   token = cp_lexer_peek_token (parser->lexer);
21534   /* If we're looking at a `::' token then we're starting from the
21535      global namespace, not our current location.  */
21536   if (token->type == CPP_SCOPE)
21537     {
21538       /* Consume the `::' token.  */
21539       cp_lexer_consume_token (parser->lexer);
21540       /* Set the SCOPE so that we know where to start the lookup.  */
21541       parser->scope = global_namespace;
21542       parser->qualifying_scope = global_namespace;
21543       parser->object_scope = NULL_TREE;
21544
21545       return parser->scope;
21546     }
21547   else if (!current_scope_valid_p)
21548     {
21549       parser->scope = NULL_TREE;
21550       parser->qualifying_scope = NULL_TREE;
21551       parser->object_scope = NULL_TREE;
21552     }
21553
21554   return NULL_TREE;
21555 }
21556
21557 /* Returns TRUE if the upcoming token sequence is the start of a
21558    constructor declarator.  If FRIEND_P is true, the declarator is
21559    preceded by the `friend' specifier.  */
21560
21561 static bool
21562 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21563 {
21564   bool constructor_p;
21565   tree nested_name_specifier;
21566   cp_token *next_token;
21567
21568   /* The common case is that this is not a constructor declarator, so
21569      try to avoid doing lots of work if at all possible.  It's not
21570      valid declare a constructor at function scope.  */
21571   if (parser->in_function_body)
21572     return false;
21573   /* And only certain tokens can begin a constructor declarator.  */
21574   next_token = cp_lexer_peek_token (parser->lexer);
21575   if (next_token->type != CPP_NAME
21576       && next_token->type != CPP_SCOPE
21577       && next_token->type != CPP_NESTED_NAME_SPECIFIER
21578       && next_token->type != CPP_TEMPLATE_ID)
21579     return false;
21580
21581   /* Parse tentatively; we are going to roll back all of the tokens
21582      consumed here.  */
21583   cp_parser_parse_tentatively (parser);
21584   /* Assume that we are looking at a constructor declarator.  */
21585   constructor_p = true;
21586
21587   /* Look for the optional `::' operator.  */
21588   cp_parser_global_scope_opt (parser,
21589                               /*current_scope_valid_p=*/false);
21590   /* Look for the nested-name-specifier.  */
21591   nested_name_specifier
21592     = (cp_parser_nested_name_specifier_opt (parser,
21593                                             /*typename_keyword_p=*/false,
21594                                             /*check_dependency_p=*/false,
21595                                             /*type_p=*/false,
21596                                             /*is_declaration=*/false));
21597   /* Outside of a class-specifier, there must be a
21598      nested-name-specifier.  */
21599   if (!nested_name_specifier &&
21600       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21601        || friend_p))
21602     constructor_p = false;
21603   else if (nested_name_specifier == error_mark_node)
21604     constructor_p = false;
21605
21606   /* If we have a class scope, this is easy; DR 147 says that S::S always
21607      names the constructor, and no other qualified name could.  */
21608   if (constructor_p && nested_name_specifier
21609       && CLASS_TYPE_P (nested_name_specifier))
21610     {
21611       tree id = cp_parser_unqualified_id (parser,
21612                                           /*template_keyword_p=*/false,
21613                                           /*check_dependency_p=*/false,
21614                                           /*declarator_p=*/true,
21615                                           /*optional_p=*/false);
21616       if (is_overloaded_fn (id))
21617         id = DECL_NAME (get_first_fn (id));
21618       if (!constructor_name_p (id, nested_name_specifier))
21619         constructor_p = false;
21620     }
21621   /* If we still think that this might be a constructor-declarator,
21622      look for a class-name.  */
21623   else if (constructor_p)
21624     {
21625       /* If we have:
21626
21627            template <typename T> struct S {
21628              S();
21629            };
21630
21631          we must recognize that the nested `S' names a class.  */
21632       tree type_decl;
21633       type_decl = cp_parser_class_name (parser,
21634                                         /*typename_keyword_p=*/false,
21635                                         /*template_keyword_p=*/false,
21636                                         none_type,
21637                                         /*check_dependency_p=*/false,
21638                                         /*class_head_p=*/false,
21639                                         /*is_declaration=*/false);
21640       /* If there was no class-name, then this is not a constructor.  */
21641       constructor_p = !cp_parser_error_occurred (parser);
21642
21643       /* If we're still considering a constructor, we have to see a `(',
21644          to begin the parameter-declaration-clause, followed by either a
21645          `)', an `...', or a decl-specifier.  We need to check for a
21646          type-specifier to avoid being fooled into thinking that:
21647
21648            S (f) (int);
21649
21650          is a constructor.  (It is actually a function named `f' that
21651          takes one parameter (of type `int') and returns a value of type
21652          `S'.  */
21653       if (constructor_p
21654           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21655         constructor_p = false;
21656
21657       if (constructor_p
21658           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21659           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21660           /* A parameter declaration begins with a decl-specifier,
21661              which is either the "attribute" keyword, a storage class
21662              specifier, or (usually) a type-specifier.  */
21663           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21664         {
21665           tree type;
21666           tree pushed_scope = NULL_TREE;
21667           unsigned saved_num_template_parameter_lists;
21668
21669           /* Names appearing in the type-specifier should be looked up
21670              in the scope of the class.  */
21671           if (current_class_type)
21672             type = NULL_TREE;
21673           else
21674             {
21675               type = TREE_TYPE (type_decl);
21676               if (TREE_CODE (type) == TYPENAME_TYPE)
21677                 {
21678                   type = resolve_typename_type (type,
21679                                                 /*only_current_p=*/false);
21680                   if (TREE_CODE (type) == TYPENAME_TYPE)
21681                     {
21682                       cp_parser_abort_tentative_parse (parser);
21683                       return false;
21684                     }
21685                 }
21686               pushed_scope = push_scope (type);
21687             }
21688
21689           /* Inside the constructor parameter list, surrounding
21690              template-parameter-lists do not apply.  */
21691           saved_num_template_parameter_lists
21692             = parser->num_template_parameter_lists;
21693           parser->num_template_parameter_lists = 0;
21694
21695           /* Look for the type-specifier.  */
21696           cp_parser_type_specifier (parser,
21697                                     CP_PARSER_FLAGS_NONE,
21698                                     /*decl_specs=*/NULL,
21699                                     /*is_declarator=*/true,
21700                                     /*declares_class_or_enum=*/NULL,
21701                                     /*is_cv_qualifier=*/NULL);
21702
21703           parser->num_template_parameter_lists
21704             = saved_num_template_parameter_lists;
21705
21706           /* Leave the scope of the class.  */
21707           if (pushed_scope)
21708             pop_scope (pushed_scope);
21709
21710           constructor_p = !cp_parser_error_occurred (parser);
21711         }
21712     }
21713
21714   /* We did not really want to consume any tokens.  */
21715   cp_parser_abort_tentative_parse (parser);
21716
21717   return constructor_p;
21718 }
21719
21720 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21721    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
21722    they must be performed once we are in the scope of the function.
21723
21724    Returns the function defined.  */
21725
21726 static tree
21727 cp_parser_function_definition_from_specifiers_and_declarator
21728   (cp_parser* parser,
21729    cp_decl_specifier_seq *decl_specifiers,
21730    tree attributes,
21731    const cp_declarator *declarator)
21732 {
21733   tree fn;
21734   bool success_p;
21735
21736   /* Begin the function-definition.  */
21737   success_p = start_function (decl_specifiers, declarator, attributes);
21738
21739   /* The things we're about to see are not directly qualified by any
21740      template headers we've seen thus far.  */
21741   reset_specialization ();
21742
21743   /* If there were names looked up in the decl-specifier-seq that we
21744      did not check, check them now.  We must wait until we are in the
21745      scope of the function to perform the checks, since the function
21746      might be a friend.  */
21747   perform_deferred_access_checks (tf_warning_or_error);
21748
21749   if (!success_p)
21750     {
21751       /* Skip the entire function.  */
21752       cp_parser_skip_to_end_of_block_or_statement (parser);
21753       fn = error_mark_node;
21754     }
21755   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21756     {
21757       /* Seen already, skip it.  An error message has already been output.  */
21758       cp_parser_skip_to_end_of_block_or_statement (parser);
21759       fn = current_function_decl;
21760       current_function_decl = NULL_TREE;
21761       /* If this is a function from a class, pop the nested class.  */
21762       if (current_class_name)
21763         pop_nested_class ();
21764     }
21765   else
21766     {
21767       timevar_id_t tv;
21768       if (DECL_DECLARED_INLINE_P (current_function_decl))
21769         tv = TV_PARSE_INLINE;
21770       else
21771         tv = TV_PARSE_FUNC;
21772       timevar_push (tv);
21773       fn = cp_parser_function_definition_after_declarator (parser,
21774                                                          /*inline_p=*/false);
21775       timevar_pop (tv);
21776     }
21777
21778   return fn;
21779 }
21780
21781 /* Parse the part of a function-definition that follows the
21782    declarator.  INLINE_P is TRUE iff this function is an inline
21783    function defined within a class-specifier.
21784
21785    Returns the function defined.  */
21786
21787 static tree
21788 cp_parser_function_definition_after_declarator (cp_parser* parser,
21789                                                 bool inline_p)
21790 {
21791   tree fn;
21792   bool ctor_initializer_p = false;
21793   bool saved_in_unbraced_linkage_specification_p;
21794   bool saved_in_function_body;
21795   unsigned saved_num_template_parameter_lists;
21796   cp_token *token;
21797
21798   saved_in_function_body = parser->in_function_body;
21799   parser->in_function_body = true;
21800   /* If the next token is `return', then the code may be trying to
21801      make use of the "named return value" extension that G++ used to
21802      support.  */
21803   token = cp_lexer_peek_token (parser->lexer);
21804   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21805     {
21806       /* Consume the `return' keyword.  */
21807       cp_lexer_consume_token (parser->lexer);
21808       /* Look for the identifier that indicates what value is to be
21809          returned.  */
21810       cp_parser_identifier (parser);
21811       /* Issue an error message.  */
21812       error_at (token->location,
21813                 "named return values are no longer supported");
21814       /* Skip tokens until we reach the start of the function body.  */
21815       while (true)
21816         {
21817           cp_token *token = cp_lexer_peek_token (parser->lexer);
21818           if (token->type == CPP_OPEN_BRACE
21819               || token->type == CPP_EOF
21820               || token->type == CPP_PRAGMA_EOL)
21821             break;
21822           cp_lexer_consume_token (parser->lexer);
21823         }
21824     }
21825   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21826      anything declared inside `f'.  */
21827   saved_in_unbraced_linkage_specification_p
21828     = parser->in_unbraced_linkage_specification_p;
21829   parser->in_unbraced_linkage_specification_p = false;
21830   /* Inside the function, surrounding template-parameter-lists do not
21831      apply.  */
21832   saved_num_template_parameter_lists
21833     = parser->num_template_parameter_lists;
21834   parser->num_template_parameter_lists = 0;
21835
21836   start_lambda_scope (current_function_decl);
21837
21838   /* If the next token is `try', `__transaction_atomic', or
21839      `__transaction_relaxed`, then we are looking at either function-try-block
21840      or function-transaction-block.  Note that all of these include the
21841      function-body.  */
21842   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21843     ctor_initializer_p = cp_parser_function_transaction (parser,
21844         RID_TRANSACTION_ATOMIC);
21845   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21846       RID_TRANSACTION_RELAXED))
21847     ctor_initializer_p = cp_parser_function_transaction (parser,
21848         RID_TRANSACTION_RELAXED);
21849   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21850     ctor_initializer_p = cp_parser_function_try_block (parser);
21851   else
21852     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21853       (parser, /*in_function_try_block=*/false);
21854
21855   finish_lambda_scope ();
21856
21857   /* Finish the function.  */
21858   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21859                         (inline_p ? 2 : 0));
21860   /* Generate code for it, if necessary.  */
21861   expand_or_defer_fn (fn);
21862   /* Restore the saved values.  */
21863   parser->in_unbraced_linkage_specification_p
21864     = saved_in_unbraced_linkage_specification_p;
21865   parser->num_template_parameter_lists
21866     = saved_num_template_parameter_lists;
21867   parser->in_function_body = saved_in_function_body;
21868
21869   return fn;
21870 }
21871
21872 /* Parse a template-declaration, assuming that the `export' (and
21873    `extern') keywords, if present, has already been scanned.  MEMBER_P
21874    is as for cp_parser_template_declaration.  */
21875
21876 static void
21877 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21878 {
21879   tree decl = NULL_TREE;
21880   vec<deferred_access_check, va_gc> *checks;
21881   tree parameter_list;
21882   bool friend_p = false;
21883   bool need_lang_pop;
21884   cp_token *token;
21885
21886   /* Look for the `template' keyword.  */
21887   token = cp_lexer_peek_token (parser->lexer);
21888   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21889     return;
21890
21891   /* And the `<'.  */
21892   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21893     return;
21894   if (at_class_scope_p () && current_function_decl)
21895     {
21896       /* 14.5.2.2 [temp.mem]
21897
21898          A local class shall not have member templates.  */
21899       error_at (token->location,
21900                 "invalid declaration of member template in local class");
21901       cp_parser_skip_to_end_of_block_or_statement (parser);
21902       return;
21903     }
21904   /* [temp]
21905
21906      A template ... shall not have C linkage.  */
21907   if (current_lang_name == lang_name_c)
21908     {
21909       error_at (token->location, "template with C linkage");
21910       /* Give it C++ linkage to avoid confusing other parts of the
21911          front end.  */
21912       push_lang_context (lang_name_cplusplus);
21913       need_lang_pop = true;
21914     }
21915   else
21916     need_lang_pop = false;
21917
21918   /* We cannot perform access checks on the template parameter
21919      declarations until we know what is being declared, just as we
21920      cannot check the decl-specifier list.  */
21921   push_deferring_access_checks (dk_deferred);
21922
21923   /* If the next token is `>', then we have an invalid
21924      specialization.  Rather than complain about an invalid template
21925      parameter, issue an error message here.  */
21926   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21927     {
21928       cp_parser_error (parser, "invalid explicit specialization");
21929       begin_specialization ();
21930       parameter_list = NULL_TREE;
21931     }
21932   else
21933     {
21934       /* Parse the template parameters.  */
21935       parameter_list = cp_parser_template_parameter_list (parser);
21936     }
21937
21938   /* Get the deferred access checks from the parameter list.  These
21939      will be checked once we know what is being declared, as for a
21940      member template the checks must be performed in the scope of the
21941      class containing the member.  */
21942   checks = get_deferred_access_checks ();
21943
21944   /* Look for the `>'.  */
21945   cp_parser_skip_to_end_of_template_parameter_list (parser);
21946   /* We just processed one more parameter list.  */
21947   ++parser->num_template_parameter_lists;
21948   /* If the next token is `template', there are more template
21949      parameters.  */
21950   if (cp_lexer_next_token_is_keyword (parser->lexer,
21951                                       RID_TEMPLATE))
21952     cp_parser_template_declaration_after_export (parser, member_p);
21953   else if (cxx_dialect >= cxx0x
21954            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21955     decl = cp_parser_alias_declaration (parser);
21956   else
21957     {
21958       /* There are no access checks when parsing a template, as we do not
21959          know if a specialization will be a friend.  */
21960       push_deferring_access_checks (dk_no_check);
21961       token = cp_lexer_peek_token (parser->lexer);
21962       decl = cp_parser_single_declaration (parser,
21963                                            checks,
21964                                            member_p,
21965                                            /*explicit_specialization_p=*/false,
21966                                            &friend_p);
21967       pop_deferring_access_checks ();
21968
21969       /* If this is a member template declaration, let the front
21970          end know.  */
21971       if (member_p && !friend_p && decl)
21972         {
21973           if (TREE_CODE (decl) == TYPE_DECL)
21974             cp_parser_check_access_in_redeclaration (decl, token->location);
21975
21976           decl = finish_member_template_decl (decl);
21977         }
21978       else if (friend_p && decl
21979                && (TREE_CODE (decl) == TYPE_DECL
21980                    || DECL_TYPE_TEMPLATE_P (decl)))
21981         make_friend_class (current_class_type, TREE_TYPE (decl),
21982                            /*complain=*/true);
21983     }
21984   /* We are done with the current parameter list.  */
21985   --parser->num_template_parameter_lists;
21986
21987   pop_deferring_access_checks ();
21988
21989   /* Finish up.  */
21990   finish_template_decl (parameter_list);
21991
21992   /* Check the template arguments for a literal operator template.  */
21993   if (decl
21994       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21995       && UDLIT_OPER_P (DECL_NAME (decl)))
21996     {
21997       bool ok = true;
21998       if (parameter_list == NULL_TREE)
21999         ok = false;
22000       else
22001         {
22002           int num_parms = TREE_VEC_LENGTH (parameter_list);
22003           if (num_parms != 1)
22004             ok = false;
22005           else
22006             {
22007               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22008               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22009               if (TREE_TYPE (parm) != char_type_node
22010                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22011                 ok = false;
22012             }
22013         }
22014       if (!ok)
22015         error ("literal operator template %qD has invalid parameter list."
22016                "  Expected non-type template argument pack <char...>",
22017                decl);
22018     }
22019   /* Register member declarations.  */
22020   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22021     finish_member_declaration (decl);
22022   /* For the erroneous case of a template with C linkage, we pushed an
22023      implicit C++ linkage scope; exit that scope now.  */
22024   if (need_lang_pop)
22025     pop_lang_context ();
22026   /* If DECL is a function template, we must return to parse it later.
22027      (Even though there is no definition, there might be default
22028      arguments that need handling.)  */
22029   if (member_p && decl
22030       && (TREE_CODE (decl) == FUNCTION_DECL
22031           || DECL_FUNCTION_TEMPLATE_P (decl)))
22032     vec_safe_push (unparsed_funs_with_definitions, decl);
22033 }
22034
22035 /* Perform the deferred access checks from a template-parameter-list.
22036    CHECKS is a TREE_LIST of access checks, as returned by
22037    get_deferred_access_checks.  */
22038
22039 static void
22040 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22041 {
22042   ++processing_template_parmlist;
22043   perform_access_checks (checks, tf_warning_or_error);
22044   --processing_template_parmlist;
22045 }
22046
22047 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22048    `function-definition' sequence that follows a template header.
22049    If MEMBER_P is true, this declaration appears in a class scope.
22050
22051    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
22052    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
22053
22054 static tree
22055 cp_parser_single_declaration (cp_parser* parser,
22056                               vec<deferred_access_check, va_gc> *checks,
22057                               bool member_p,
22058                               bool explicit_specialization_p,
22059                               bool* friend_p)
22060 {
22061   int declares_class_or_enum;
22062   tree decl = NULL_TREE;
22063   cp_decl_specifier_seq decl_specifiers;
22064   bool function_definition_p = false;
22065   cp_token *decl_spec_token_start;
22066
22067   /* This function is only used when processing a template
22068      declaration.  */
22069   gcc_assert (innermost_scope_kind () == sk_template_parms
22070               || innermost_scope_kind () == sk_template_spec);
22071
22072   /* Defer access checks until we know what is being declared.  */
22073   push_deferring_access_checks (dk_deferred);
22074
22075   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22076      alternative.  */
22077   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22078   cp_parser_decl_specifier_seq (parser,
22079                                 CP_PARSER_FLAGS_OPTIONAL,
22080                                 &decl_specifiers,
22081                                 &declares_class_or_enum);
22082   if (friend_p)
22083     *friend_p = cp_parser_friend_p (&decl_specifiers);
22084
22085   /* There are no template typedefs.  */
22086   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22087     {
22088       error_at (decl_spec_token_start->location,
22089                 "template declaration of %<typedef%>");
22090       decl = error_mark_node;
22091     }
22092
22093   /* Gather up the access checks that occurred the
22094      decl-specifier-seq.  */
22095   stop_deferring_access_checks ();
22096
22097   /* Check for the declaration of a template class.  */
22098   if (declares_class_or_enum)
22099     {
22100       if (cp_parser_declares_only_class_p (parser))
22101         {
22102           decl = shadow_tag (&decl_specifiers);
22103
22104           /* In this case:
22105
22106                struct C {
22107                  friend template <typename T> struct A<T>::B;
22108                };
22109
22110              A<T>::B will be represented by a TYPENAME_TYPE, and
22111              therefore not recognized by shadow_tag.  */
22112           if (friend_p && *friend_p
22113               && !decl
22114               && decl_specifiers.type
22115               && TYPE_P (decl_specifiers.type))
22116             decl = decl_specifiers.type;
22117
22118           if (decl && decl != error_mark_node)
22119             decl = TYPE_NAME (decl);
22120           else
22121             decl = error_mark_node;
22122
22123           /* Perform access checks for template parameters.  */
22124           cp_parser_perform_template_parameter_access_checks (checks);
22125         }
22126     }
22127
22128   /* Complain about missing 'typename' or other invalid type names.  */
22129   if (!decl_specifiers.any_type_specifiers_p
22130       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22131     {
22132       /* cp_parser_parse_and_diagnose_invalid_type_name calls
22133          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22134          the rest of this declaration.  */
22135       decl = error_mark_node;
22136       goto out;
22137     }
22138
22139   /* If it's not a template class, try for a template function.  If
22140      the next token is a `;', then this declaration does not declare
22141      anything.  But, if there were errors in the decl-specifiers, then
22142      the error might well have come from an attempted class-specifier.
22143      In that case, there's no need to warn about a missing declarator.  */
22144   if (!decl
22145       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22146           || decl_specifiers.type != error_mark_node))
22147     {
22148       decl = cp_parser_init_declarator (parser,
22149                                         &decl_specifiers,
22150                                         checks,
22151                                         /*function_definition_allowed_p=*/true,
22152                                         member_p,
22153                                         declares_class_or_enum,
22154                                         &function_definition_p,
22155                                         NULL);
22156
22157     /* 7.1.1-1 [dcl.stc]
22158
22159        A storage-class-specifier shall not be specified in an explicit
22160        specialization...  */
22161     if (decl
22162         && explicit_specialization_p
22163         && decl_specifiers.storage_class != sc_none)
22164       {
22165         error_at (decl_spec_token_start->location,
22166                   "explicit template specialization cannot have a storage class");
22167         decl = error_mark_node;
22168       }
22169
22170     if (decl && TREE_CODE (decl) == VAR_DECL)
22171       check_template_variable (decl);
22172     }
22173
22174   /* Look for a trailing `;' after the declaration.  */
22175   if (!function_definition_p
22176       && (decl == error_mark_node
22177           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22178     cp_parser_skip_to_end_of_block_or_statement (parser);
22179
22180  out:
22181   pop_deferring_access_checks ();
22182
22183   /* Clear any current qualification; whatever comes next is the start
22184      of something new.  */
22185   parser->scope = NULL_TREE;
22186   parser->qualifying_scope = NULL_TREE;
22187   parser->object_scope = NULL_TREE;
22188
22189   return decl;
22190 }
22191
22192 /* Parse a cast-expression that is not the operand of a unary "&".  */
22193
22194 static tree
22195 cp_parser_simple_cast_expression (cp_parser *parser)
22196 {
22197   return cp_parser_cast_expression (parser, /*address_p=*/false,
22198                                     /*cast_p=*/false, /*decltype*/false, NULL);
22199 }
22200
22201 /* Parse a functional cast to TYPE.  Returns an expression
22202    representing the cast.  */
22203
22204 static tree
22205 cp_parser_functional_cast (cp_parser* parser, tree type)
22206 {
22207   vec<tree, va_gc> *vec;
22208   tree expression_list;
22209   tree cast;
22210   bool nonconst_p;
22211
22212   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22213     {
22214       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22215       expression_list = cp_parser_braced_list (parser, &nonconst_p);
22216       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22217       if (TREE_CODE (type) == TYPE_DECL)
22218         type = TREE_TYPE (type);
22219       return finish_compound_literal (type, expression_list,
22220                                       tf_warning_or_error);
22221     }
22222
22223
22224   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22225                                                  /*cast_p=*/true,
22226                                                  /*allow_expansion_p=*/true,
22227                                                  /*non_constant_p=*/NULL);
22228   if (vec == NULL)
22229     expression_list = error_mark_node;
22230   else
22231     {
22232       expression_list = build_tree_list_vec (vec);
22233       release_tree_vector (vec);
22234     }
22235
22236   cast = build_functional_cast (type, expression_list,
22237                                 tf_warning_or_error);
22238   /* [expr.const]/1: In an integral constant expression "only type
22239      conversions to integral or enumeration type can be used".  */
22240   if (TREE_CODE (type) == TYPE_DECL)
22241     type = TREE_TYPE (type);
22242   if (cast != error_mark_node
22243       && !cast_valid_in_integral_constant_expression_p (type)
22244       && cp_parser_non_integral_constant_expression (parser,
22245                                                      NIC_CONSTRUCTOR))
22246     return error_mark_node;
22247   return cast;
22248 }
22249
22250 /* Save the tokens that make up the body of a member function defined
22251    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
22252    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
22253    specifiers applied to the declaration.  Returns the FUNCTION_DECL
22254    for the member function.  */
22255
22256 static tree
22257 cp_parser_save_member_function_body (cp_parser* parser,
22258                                      cp_decl_specifier_seq *decl_specifiers,
22259                                      cp_declarator *declarator,
22260                                      tree attributes)
22261 {
22262   cp_token *first;
22263   cp_token *last;
22264   tree fn;
22265
22266   /* Create the FUNCTION_DECL.  */
22267   fn = grokmethod (decl_specifiers, declarator, attributes);
22268   /* If something went badly wrong, bail out now.  */
22269   if (fn == error_mark_node)
22270     {
22271       /* If there's a function-body, skip it.  */
22272       if (cp_parser_token_starts_function_definition_p
22273           (cp_lexer_peek_token (parser->lexer)))
22274         cp_parser_skip_to_end_of_block_or_statement (parser);
22275       return error_mark_node;
22276     }
22277
22278   /* Remember it, if there default args to post process.  */
22279   cp_parser_save_default_args (parser, fn);
22280
22281   /* Save away the tokens that make up the body of the
22282      function.  */
22283   first = parser->lexer->next_token;
22284   /* We can have braced-init-list mem-initializers before the fn body.  */
22285   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22286     {
22287       cp_lexer_consume_token (parser->lexer);
22288       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22289              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22290         {
22291           /* cache_group will stop after an un-nested { } pair, too.  */
22292           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22293             break;
22294
22295           /* variadic mem-inits have ... after the ')'.  */
22296           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22297             cp_lexer_consume_token (parser->lexer);
22298         }
22299     }
22300   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22301   /* Handle function try blocks.  */
22302   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22303     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22304   last = parser->lexer->next_token;
22305
22306   /* Save away the inline definition; we will process it when the
22307      class is complete.  */
22308   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22309   DECL_PENDING_INLINE_P (fn) = 1;
22310
22311   /* We need to know that this was defined in the class, so that
22312      friend templates are handled correctly.  */
22313   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22314
22315   /* Add FN to the queue of functions to be parsed later.  */
22316   vec_safe_push (unparsed_funs_with_definitions, fn);
22317
22318   return fn;
22319 }
22320
22321 /* Save the tokens that make up the in-class initializer for a non-static
22322    data member.  Returns a DEFAULT_ARG.  */
22323
22324 static tree
22325 cp_parser_save_nsdmi (cp_parser* parser)
22326 {
22327   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22328 }
22329
22330 /* Parse a template-argument-list, as well as the trailing ">" (but
22331    not the opening "<").  See cp_parser_template_argument_list for the
22332    return value.  */
22333
22334 static tree
22335 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22336 {
22337   tree arguments;
22338   tree saved_scope;
22339   tree saved_qualifying_scope;
22340   tree saved_object_scope;
22341   bool saved_greater_than_is_operator_p;
22342   int saved_unevaluated_operand;
22343   int saved_inhibit_evaluation_warnings;
22344
22345   /* [temp.names]
22346
22347      When parsing a template-id, the first non-nested `>' is taken as
22348      the end of the template-argument-list rather than a greater-than
22349      operator.  */
22350   saved_greater_than_is_operator_p
22351     = parser->greater_than_is_operator_p;
22352   parser->greater_than_is_operator_p = false;
22353   /* Parsing the argument list may modify SCOPE, so we save it
22354      here.  */
22355   saved_scope = parser->scope;
22356   saved_qualifying_scope = parser->qualifying_scope;
22357   saved_object_scope = parser->object_scope;
22358   /* We need to evaluate the template arguments, even though this
22359      template-id may be nested within a "sizeof".  */
22360   saved_unevaluated_operand = cp_unevaluated_operand;
22361   cp_unevaluated_operand = 0;
22362   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22363   c_inhibit_evaluation_warnings = 0;
22364   /* Parse the template-argument-list itself.  */
22365   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22366       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22367     arguments = NULL_TREE;
22368   else
22369     arguments = cp_parser_template_argument_list (parser);
22370   /* Look for the `>' that ends the template-argument-list. If we find
22371      a '>>' instead, it's probably just a typo.  */
22372   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22373     {
22374       if (cxx_dialect != cxx98)
22375         {
22376           /* In C++0x, a `>>' in a template argument list or cast
22377              expression is considered to be two separate `>'
22378              tokens. So, change the current token to a `>', but don't
22379              consume it: it will be consumed later when the outer
22380              template argument list (or cast expression) is parsed.
22381              Note that this replacement of `>' for `>>' is necessary
22382              even if we are parsing tentatively: in the tentative
22383              case, after calling
22384              cp_parser_enclosed_template_argument_list we will always
22385              throw away all of the template arguments and the first
22386              closing `>', either because the template argument list
22387              was erroneous or because we are replacing those tokens
22388              with a CPP_TEMPLATE_ID token.  The second `>' (which will
22389              not have been thrown away) is needed either to close an
22390              outer template argument list or to complete a new-style
22391              cast.  */
22392           cp_token *token = cp_lexer_peek_token (parser->lexer);
22393           token->type = CPP_GREATER;
22394         }
22395       else if (!saved_greater_than_is_operator_p)
22396         {
22397           /* If we're in a nested template argument list, the '>>' has
22398             to be a typo for '> >'. We emit the error message, but we
22399             continue parsing and we push a '>' as next token, so that
22400             the argument list will be parsed correctly.  Note that the
22401             global source location is still on the token before the
22402             '>>', so we need to say explicitly where we want it.  */
22403           cp_token *token = cp_lexer_peek_token (parser->lexer);
22404           error_at (token->location, "%<>>%> should be %<> >%> "
22405                     "within a nested template argument list");
22406
22407           token->type = CPP_GREATER;
22408         }
22409       else
22410         {
22411           /* If this is not a nested template argument list, the '>>'
22412             is a typo for '>'. Emit an error message and continue.
22413             Same deal about the token location, but here we can get it
22414             right by consuming the '>>' before issuing the diagnostic.  */
22415           cp_token *token = cp_lexer_consume_token (parser->lexer);
22416           error_at (token->location,
22417                     "spurious %<>>%>, use %<>%> to terminate "
22418                     "a template argument list");
22419         }
22420     }
22421   else
22422     cp_parser_skip_to_end_of_template_parameter_list (parser);
22423   /* The `>' token might be a greater-than operator again now.  */
22424   parser->greater_than_is_operator_p
22425     = saved_greater_than_is_operator_p;
22426   /* Restore the SAVED_SCOPE.  */
22427   parser->scope = saved_scope;
22428   parser->qualifying_scope = saved_qualifying_scope;
22429   parser->object_scope = saved_object_scope;
22430   cp_unevaluated_operand = saved_unevaluated_operand;
22431   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22432
22433   return arguments;
22434 }
22435
22436 /* MEMBER_FUNCTION is a member function, or a friend.  If default
22437    arguments, or the body of the function have not yet been parsed,
22438    parse them now.  */
22439
22440 static void
22441 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22442 {
22443   timevar_push (TV_PARSE_INMETH);
22444   /* If this member is a template, get the underlying
22445      FUNCTION_DECL.  */
22446   if (DECL_FUNCTION_TEMPLATE_P (member_function))
22447     member_function = DECL_TEMPLATE_RESULT (member_function);
22448
22449   /* There should not be any class definitions in progress at this
22450      point; the bodies of members are only parsed outside of all class
22451      definitions.  */
22452   gcc_assert (parser->num_classes_being_defined == 0);
22453   /* While we're parsing the member functions we might encounter more
22454      classes.  We want to handle them right away, but we don't want
22455      them getting mixed up with functions that are currently in the
22456      queue.  */
22457   push_unparsed_function_queues (parser);
22458
22459   /* Make sure that any template parameters are in scope.  */
22460   maybe_begin_member_template_processing (member_function);
22461
22462   /* If the body of the function has not yet been parsed, parse it
22463      now.  */
22464   if (DECL_PENDING_INLINE_P (member_function))
22465     {
22466       tree function_scope;
22467       cp_token_cache *tokens;
22468
22469       /* The function is no longer pending; we are processing it.  */
22470       tokens = DECL_PENDING_INLINE_INFO (member_function);
22471       DECL_PENDING_INLINE_INFO (member_function) = NULL;
22472       DECL_PENDING_INLINE_P (member_function) = 0;
22473
22474       /* If this is a local class, enter the scope of the containing
22475          function.  */
22476       function_scope = current_function_decl;
22477       if (function_scope)
22478         push_function_context ();
22479
22480       /* Push the body of the function onto the lexer stack.  */
22481       cp_parser_push_lexer_for_tokens (parser, tokens);
22482
22483       /* Let the front end know that we going to be defining this
22484          function.  */
22485       start_preparsed_function (member_function, NULL_TREE,
22486                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
22487
22488       /* Don't do access checking if it is a templated function.  */
22489       if (processing_template_decl)
22490         push_deferring_access_checks (dk_no_check);
22491
22492       /* Now, parse the body of the function.  */
22493       cp_parser_function_definition_after_declarator (parser,
22494                                                       /*inline_p=*/true);
22495
22496       if (processing_template_decl)
22497         pop_deferring_access_checks ();
22498
22499       /* Leave the scope of the containing function.  */
22500       if (function_scope)
22501         pop_function_context ();
22502       cp_parser_pop_lexer (parser);
22503     }
22504
22505   /* Remove any template parameters from the symbol table.  */
22506   maybe_end_member_template_processing ();
22507
22508   /* Restore the queue.  */
22509   pop_unparsed_function_queues (parser);
22510   timevar_pop (TV_PARSE_INMETH);
22511 }
22512
22513 /* If DECL contains any default args, remember it on the unparsed
22514    functions queue.  */
22515
22516 static void
22517 cp_parser_save_default_args (cp_parser* parser, tree decl)
22518 {
22519   tree probe;
22520
22521   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22522        probe;
22523        probe = TREE_CHAIN (probe))
22524     if (TREE_PURPOSE (probe))
22525       {
22526         cp_default_arg_entry entry = {current_class_type, decl};
22527         vec_safe_push (unparsed_funs_with_default_args, entry);
22528         break;
22529       }
22530 }
22531
22532 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22533    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
22534    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
22535    from the parameter-type-list.  */
22536
22537 static tree
22538 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22539                                       tree default_arg, tree parmtype)
22540 {
22541   cp_token_cache *tokens;
22542   tree parsed_arg;
22543   bool dummy;
22544
22545   if (default_arg == error_mark_node)
22546     return error_mark_node;
22547
22548   /* Push the saved tokens for the default argument onto the parser's
22549      lexer stack.  */
22550   tokens = DEFARG_TOKENS (default_arg);
22551   cp_parser_push_lexer_for_tokens (parser, tokens);
22552
22553   start_lambda_scope (decl);
22554
22555   /* Parse the default argument.  */
22556   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22557   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22558     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22559
22560   finish_lambda_scope ();
22561
22562   if (parsed_arg == error_mark_node)
22563     cp_parser_skip_to_end_of_statement (parser);
22564
22565   if (!processing_template_decl)
22566     {
22567       /* In a non-template class, check conversions now.  In a template,
22568          we'll wait and instantiate these as needed.  */
22569       if (TREE_CODE (decl) == PARM_DECL)
22570         parsed_arg = check_default_argument (parmtype, parsed_arg,
22571                                              tf_warning_or_error);
22572       else
22573         {
22574           int flags = LOOKUP_IMPLICIT;
22575           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22576               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22577             flags = LOOKUP_NORMAL;
22578           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22579         }
22580     }
22581
22582   /* If the token stream has not been completely used up, then
22583      there was extra junk after the end of the default
22584      argument.  */
22585   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22586     {
22587       if (TREE_CODE (decl) == PARM_DECL)
22588         cp_parser_error (parser, "expected %<,%>");
22589       else
22590         cp_parser_error (parser, "expected %<;%>");
22591     }
22592
22593   /* Revert to the main lexer.  */
22594   cp_parser_pop_lexer (parser);
22595
22596   return parsed_arg;
22597 }
22598
22599 /* FIELD is a non-static data member with an initializer which we saved for
22600    later; parse it now.  */
22601
22602 static void
22603 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22604 {
22605   tree def;
22606
22607   push_unparsed_function_queues (parser);
22608   def = cp_parser_late_parse_one_default_arg (parser, field,
22609                                               DECL_INITIAL (field),
22610                                               NULL_TREE);
22611   pop_unparsed_function_queues (parser);
22612
22613   DECL_INITIAL (field) = def;
22614 }
22615
22616 /* FN is a FUNCTION_DECL which may contains a parameter with an
22617    unparsed DEFAULT_ARG.  Parse the default args now.  This function
22618    assumes that the current scope is the scope in which the default
22619    argument should be processed.  */
22620
22621 static void
22622 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22623 {
22624   bool saved_local_variables_forbidden_p;
22625   tree parm, parmdecl;
22626
22627   /* While we're parsing the default args, we might (due to the
22628      statement expression extension) encounter more classes.  We want
22629      to handle them right away, but we don't want them getting mixed
22630      up with default args that are currently in the queue.  */
22631   push_unparsed_function_queues (parser);
22632
22633   /* Local variable names (and the `this' keyword) may not appear
22634      in a default argument.  */
22635   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22636   parser->local_variables_forbidden_p = true;
22637
22638   push_defarg_context (fn);
22639
22640   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22641          parmdecl = DECL_ARGUMENTS (fn);
22642        parm && parm != void_list_node;
22643        parm = TREE_CHAIN (parm),
22644          parmdecl = DECL_CHAIN (parmdecl))
22645     {
22646       tree default_arg = TREE_PURPOSE (parm);
22647       tree parsed_arg;
22648       vec<tree, va_gc> *insts;
22649       tree copy;
22650       unsigned ix;
22651
22652       if (!default_arg)
22653         continue;
22654
22655       if (TREE_CODE (default_arg) != DEFAULT_ARG)
22656         /* This can happen for a friend declaration for a function
22657            already declared with default arguments.  */
22658         continue;
22659
22660       parsed_arg
22661         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22662                                                 default_arg,
22663                                                 TREE_VALUE (parm));
22664       if (parsed_arg == error_mark_node)
22665         {
22666           continue;
22667         }
22668
22669       TREE_PURPOSE (parm) = parsed_arg;
22670
22671       /* Update any instantiations we've already created.  */
22672       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22673            vec_safe_iterate (insts, ix, &copy); ix++)
22674         TREE_PURPOSE (copy) = parsed_arg;
22675     }
22676
22677   pop_defarg_context ();
22678
22679   /* Make sure no default arg is missing.  */
22680   check_default_args (fn);
22681
22682   /* Restore the state of local_variables_forbidden_p.  */
22683   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22684
22685   /* Restore the queue.  */
22686   pop_unparsed_function_queues (parser);
22687 }
22688
22689 /* Parse the operand of `sizeof' (or a similar operator).  Returns
22690    either a TYPE or an expression, depending on the form of the
22691    input.  The KEYWORD indicates which kind of expression we have
22692    encountered.  */
22693
22694 static tree
22695 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22696 {
22697   tree expr = NULL_TREE;
22698   const char *saved_message;
22699   char *tmp;
22700   bool saved_integral_constant_expression_p;
22701   bool saved_non_integral_constant_expression_p;
22702   bool pack_expansion_p = false;
22703
22704   /* Types cannot be defined in a `sizeof' expression.  Save away the
22705      old message.  */
22706   saved_message = parser->type_definition_forbidden_message;
22707   /* And create the new one.  */
22708   tmp = concat ("types may not be defined in %<",
22709                 IDENTIFIER_POINTER (ridpointers[keyword]),
22710                 "%> expressions", NULL);
22711   parser->type_definition_forbidden_message = tmp;
22712
22713   /* The restrictions on constant-expressions do not apply inside
22714      sizeof expressions.  */
22715   saved_integral_constant_expression_p
22716     = parser->integral_constant_expression_p;
22717   saved_non_integral_constant_expression_p
22718     = parser->non_integral_constant_expression_p;
22719   parser->integral_constant_expression_p = false;
22720
22721   /* If it's a `...', then we are computing the length of a parameter
22722      pack.  */
22723   if (keyword == RID_SIZEOF
22724       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22725     {
22726       /* Consume the `...'.  */
22727       cp_lexer_consume_token (parser->lexer);
22728       maybe_warn_variadic_templates ();
22729
22730       /* Note that this is an expansion.  */
22731       pack_expansion_p = true;
22732     }
22733
22734   /* Do not actually evaluate the expression.  */
22735   ++cp_unevaluated_operand;
22736   ++c_inhibit_evaluation_warnings;
22737   /* If it's a `(', then we might be looking at the type-id
22738      construction.  */
22739   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22740     {
22741       tree type;
22742       bool saved_in_type_id_in_expr_p;
22743
22744       /* We can't be sure yet whether we're looking at a type-id or an
22745          expression.  */
22746       cp_parser_parse_tentatively (parser);
22747       /* Consume the `('.  */
22748       cp_lexer_consume_token (parser->lexer);
22749       /* Parse the type-id.  */
22750       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22751       parser->in_type_id_in_expr_p = true;
22752       type = cp_parser_type_id (parser);
22753       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22754       /* Now, look for the trailing `)'.  */
22755       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22756       /* If all went well, then we're done.  */
22757       if (cp_parser_parse_definitely (parser))
22758         {
22759           cp_decl_specifier_seq decl_specs;
22760
22761           /* Build a trivial decl-specifier-seq.  */
22762           clear_decl_specs (&decl_specs);
22763           decl_specs.type = type;
22764
22765           /* Call grokdeclarator to figure out what type this is.  */
22766           expr = grokdeclarator (NULL,
22767                                  &decl_specs,
22768                                  TYPENAME,
22769                                  /*initialized=*/0,
22770                                  /*attrlist=*/NULL);
22771         }
22772     }
22773   else if (pack_expansion_p)
22774     permerror (cp_lexer_peek_token (parser->lexer)->location,
22775                "%<sizeof...%> argument must be surrounded by parentheses");
22776
22777   /* If the type-id production did not work out, then we must be
22778      looking at the unary-expression production.  */
22779   if (!expr)
22780     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22781                                        /*cast_p=*/false, NULL);
22782
22783   if (pack_expansion_p)
22784     /* Build a pack expansion. */
22785     expr = make_pack_expansion (expr);
22786
22787   /* Go back to evaluating expressions.  */
22788   --cp_unevaluated_operand;
22789   --c_inhibit_evaluation_warnings;
22790
22791   /* Free the message we created.  */
22792   free (tmp);
22793   /* And restore the old one.  */
22794   parser->type_definition_forbidden_message = saved_message;
22795   parser->integral_constant_expression_p
22796     = saved_integral_constant_expression_p;
22797   parser->non_integral_constant_expression_p
22798     = saved_non_integral_constant_expression_p;
22799
22800   return expr;
22801 }
22802
22803 /* If the current declaration has no declarator, return true.  */
22804
22805 static bool
22806 cp_parser_declares_only_class_p (cp_parser *parser)
22807 {
22808   /* If the next token is a `;' or a `,' then there is no
22809      declarator.  */
22810   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22811           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22812 }
22813
22814 /* Update the DECL_SPECS to reflect the storage class indicated by
22815    KEYWORD.  */
22816
22817 static void
22818 cp_parser_set_storage_class (cp_parser *parser,
22819                              cp_decl_specifier_seq *decl_specs,
22820                              enum rid keyword,
22821                              cp_token *token)
22822 {
22823   cp_storage_class storage_class;
22824
22825   if (parser->in_unbraced_linkage_specification_p)
22826     {
22827       error_at (token->location, "invalid use of %qD in linkage specification",
22828                 ridpointers[keyword]);
22829       return;
22830     }
22831   else if (decl_specs->storage_class != sc_none)
22832     {
22833       decl_specs->conflicting_specifiers_p = true;
22834       return;
22835     }
22836
22837   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22838       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22839       && decl_specs->gnu_thread_keyword_p)
22840     {
22841       pedwarn (decl_specs->locations[ds_thread], 0,
22842                 "%<__thread%> before %qD", ridpointers[keyword]);
22843     }
22844
22845   switch (keyword)
22846     {
22847     case RID_AUTO:
22848       storage_class = sc_auto;
22849       break;
22850     case RID_REGISTER:
22851       storage_class = sc_register;
22852       break;
22853     case RID_STATIC:
22854       storage_class = sc_static;
22855       break;
22856     case RID_EXTERN:
22857       storage_class = sc_extern;
22858       break;
22859     case RID_MUTABLE:
22860       storage_class = sc_mutable;
22861       break;
22862     default:
22863       gcc_unreachable ();
22864     }
22865   decl_specs->storage_class = storage_class;
22866   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22867
22868   /* A storage class specifier cannot be applied alongside a typedef 
22869      specifier. If there is a typedef specifier present then set 
22870      conflicting_specifiers_p which will trigger an error later
22871      on in grokdeclarator. */
22872   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22873     decl_specs->conflicting_specifiers_p = true;
22874 }
22875
22876 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22877    is true, the type is a class or enum definition.  */
22878
22879 static void
22880 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22881                               tree type_spec,
22882                               cp_token *token,
22883                               bool type_definition_p)
22884 {
22885   decl_specs->any_specifiers_p = true;
22886
22887   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22888      (with, for example, in "typedef int wchar_t;") we remember that
22889      this is what happened.  In system headers, we ignore these
22890      declarations so that G++ can work with system headers that are not
22891      C++-safe.  */
22892   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22893       && !type_definition_p
22894       && (type_spec == boolean_type_node
22895           || type_spec == char16_type_node
22896           || type_spec == char32_type_node
22897           || type_spec == wchar_type_node)
22898       && (decl_specs->type
22899           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22900           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22901           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22902           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22903     {
22904       decl_specs->redefined_builtin_type = type_spec;
22905       set_and_check_decl_spec_loc (decl_specs,
22906                                    ds_redefined_builtin_type_spec,
22907                                    token);
22908       if (!decl_specs->type)
22909         {
22910           decl_specs->type = type_spec;
22911           decl_specs->type_definition_p = false;
22912           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22913         }
22914     }
22915   else if (decl_specs->type)
22916     decl_specs->multiple_types_p = true;
22917   else
22918     {
22919       decl_specs->type = type_spec;
22920       decl_specs->type_definition_p = type_definition_p;
22921       decl_specs->redefined_builtin_type = NULL_TREE;
22922       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22923     }
22924 }
22925
22926 /* True iff TOKEN is the GNU keyword __thread.  */
22927
22928 static bool
22929 token_is__thread (cp_token *token)
22930 {
22931   gcc_assert (token->keyword == RID_THREAD);
22932   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22933 }
22934
22935 /* Set the location for a declarator specifier and check if it is
22936    duplicated.
22937
22938    DECL_SPECS is the sequence of declarator specifiers onto which to
22939    set the location.
22940
22941    DS is the single declarator specifier to set which location  is to
22942    be set onto the existing sequence of declarators.
22943
22944    LOCATION is the location for the declarator specifier to
22945    consider.  */
22946
22947 static void
22948 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22949                              cp_decl_spec ds, cp_token *token)
22950 {
22951   gcc_assert (ds < ds_last);
22952
22953   if (decl_specs == NULL)
22954     return;
22955
22956   source_location location = token->location;
22957
22958   if (decl_specs->locations[ds] == 0)
22959     {
22960       decl_specs->locations[ds] = location;
22961       if (ds == ds_thread)
22962         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22963     }
22964   else
22965     {
22966       if (ds == ds_long)
22967         {
22968           if (decl_specs->locations[ds_long_long] != 0)
22969             error_at (location,
22970                       "%<long long long%> is too long for GCC");
22971           else
22972             {
22973               decl_specs->locations[ds_long_long] = location;
22974               pedwarn_cxx98 (location,
22975                              OPT_Wlong_long, 
22976                              "ISO C++ 1998 does not support %<long long%>");
22977             }
22978         }
22979       else if (ds == ds_thread)
22980         {
22981           bool gnu = token_is__thread (token);
22982           if (gnu != decl_specs->gnu_thread_keyword_p)
22983             error_at (location,
22984                       "both %<__thread%> and %<thread_local%> specified");
22985           else
22986             error_at (location, "duplicate %qD", token->u.value);
22987         }
22988       else
22989         {
22990           static const char *const decl_spec_names[] = {
22991             "signed",
22992             "unsigned",
22993             "short",
22994             "long",
22995             "const",
22996             "volatile",
22997             "restrict",
22998             "inline",
22999             "virtual",
23000             "explicit",
23001             "friend",
23002             "typedef",
23003             "using",
23004             "constexpr",
23005             "__complex"
23006           };
23007           error_at (location,
23008                     "duplicate %qs", decl_spec_names[ds]);
23009         }
23010     }
23011 }
23012
23013 /* Return true iff the declarator specifier DS is present in the
23014    sequence of declarator specifiers DECL_SPECS.  */
23015
23016 bool
23017 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23018                           cp_decl_spec ds)
23019 {
23020   gcc_assert (ds < ds_last);
23021
23022   if (decl_specs == NULL)
23023     return false;
23024
23025   return decl_specs->locations[ds] != 0;
23026 }
23027
23028 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23029    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
23030
23031 static bool
23032 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23033 {
23034   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23035 }
23036
23037 /* Issue an error message indicating that TOKEN_DESC was expected.
23038    If KEYWORD is true, it indicated this function is called by
23039    cp_parser_require_keword and the required token can only be
23040    a indicated keyword. */
23041
23042 static void
23043 cp_parser_required_error (cp_parser *parser,
23044                           required_token token_desc,
23045                           bool keyword)
23046 {
23047   switch (token_desc)
23048     {
23049       case RT_NEW:
23050         cp_parser_error (parser, "expected %<new%>");
23051         return;
23052       case RT_DELETE:
23053         cp_parser_error (parser, "expected %<delete%>");
23054         return;
23055       case RT_RETURN:
23056         cp_parser_error (parser, "expected %<return%>");
23057         return;
23058       case RT_WHILE:
23059         cp_parser_error (parser, "expected %<while%>");
23060         return;
23061       case RT_EXTERN:
23062         cp_parser_error (parser, "expected %<extern%>");
23063         return;
23064       case RT_STATIC_ASSERT:
23065         cp_parser_error (parser, "expected %<static_assert%>");
23066         return;
23067       case RT_DECLTYPE:
23068         cp_parser_error (parser, "expected %<decltype%>");
23069         return;
23070       case RT_OPERATOR:
23071         cp_parser_error (parser, "expected %<operator%>");
23072         return;
23073       case RT_CLASS:
23074         cp_parser_error (parser, "expected %<class%>");
23075         return;
23076       case RT_TEMPLATE:
23077         cp_parser_error (parser, "expected %<template%>");
23078         return;
23079       case RT_NAMESPACE:
23080         cp_parser_error (parser, "expected %<namespace%>");
23081         return;
23082       case RT_USING:
23083         cp_parser_error (parser, "expected %<using%>");
23084         return;
23085       case RT_ASM:
23086         cp_parser_error (parser, "expected %<asm%>");
23087         return;
23088       case RT_TRY:
23089         cp_parser_error (parser, "expected %<try%>");
23090         return;
23091       case RT_CATCH:
23092         cp_parser_error (parser, "expected %<catch%>");
23093         return;
23094       case RT_THROW:
23095         cp_parser_error (parser, "expected %<throw%>");
23096         return;
23097       case RT_LABEL:
23098         cp_parser_error (parser, "expected %<__label__%>");
23099         return;
23100       case RT_AT_TRY:
23101         cp_parser_error (parser, "expected %<@try%>");
23102         return;
23103       case RT_AT_SYNCHRONIZED:
23104         cp_parser_error (parser, "expected %<@synchronized%>");
23105         return;
23106       case RT_AT_THROW:
23107         cp_parser_error (parser, "expected %<@throw%>");
23108         return;
23109       case RT_TRANSACTION_ATOMIC:
23110         cp_parser_error (parser, "expected %<__transaction_atomic%>");
23111         return;
23112       case RT_TRANSACTION_RELAXED:
23113         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23114         return;
23115       default:
23116         break;
23117     }
23118   if (!keyword)
23119     {
23120       switch (token_desc)
23121         {
23122           case RT_SEMICOLON:
23123             cp_parser_error (parser, "expected %<;%>");
23124             return;
23125           case RT_OPEN_PAREN:
23126             cp_parser_error (parser, "expected %<(%>");
23127             return;
23128           case RT_CLOSE_BRACE:
23129             cp_parser_error (parser, "expected %<}%>");
23130             return;
23131           case RT_OPEN_BRACE:
23132             cp_parser_error (parser, "expected %<{%>");
23133             return;
23134           case RT_CLOSE_SQUARE:
23135             cp_parser_error (parser, "expected %<]%>");
23136             return;
23137           case RT_OPEN_SQUARE:
23138             cp_parser_error (parser, "expected %<[%>");
23139             return;
23140           case RT_COMMA:
23141             cp_parser_error (parser, "expected %<,%>");
23142             return;
23143           case RT_SCOPE:
23144             cp_parser_error (parser, "expected %<::%>");
23145             return;
23146           case RT_LESS:
23147             cp_parser_error (parser, "expected %<<%>");
23148             return;
23149           case RT_GREATER:
23150             cp_parser_error (parser, "expected %<>%>");
23151             return;
23152           case RT_EQ:
23153             cp_parser_error (parser, "expected %<=%>");
23154             return;
23155           case RT_ELLIPSIS:
23156             cp_parser_error (parser, "expected %<...%>");
23157             return;
23158           case RT_MULT:
23159             cp_parser_error (parser, "expected %<*%>");
23160             return;
23161           case RT_COMPL:
23162             cp_parser_error (parser, "expected %<~%>");
23163             return;
23164           case RT_COLON:
23165             cp_parser_error (parser, "expected %<:%>");
23166             return;
23167           case RT_COLON_SCOPE:
23168             cp_parser_error (parser, "expected %<:%> or %<::%>");
23169             return;
23170           case RT_CLOSE_PAREN:
23171             cp_parser_error (parser, "expected %<)%>");
23172             return;
23173           case RT_COMMA_CLOSE_PAREN:
23174             cp_parser_error (parser, "expected %<,%> or %<)%>");
23175             return;
23176           case RT_PRAGMA_EOL:
23177             cp_parser_error (parser, "expected end of line");
23178             return;
23179           case RT_NAME:
23180             cp_parser_error (parser, "expected identifier");
23181             return;
23182           case RT_SELECT:
23183             cp_parser_error (parser, "expected selection-statement");
23184             return;
23185           case RT_INTERATION:
23186             cp_parser_error (parser, "expected iteration-statement");
23187             return;
23188           case RT_JUMP:
23189             cp_parser_error (parser, "expected jump-statement");
23190             return;
23191           case RT_CLASS_KEY:
23192             cp_parser_error (parser, "expected class-key");
23193             return;
23194           case RT_CLASS_TYPENAME_TEMPLATE:
23195             cp_parser_error (parser,
23196                  "expected %<class%>, %<typename%>, or %<template%>");
23197             return;
23198           default:
23199             gcc_unreachable ();
23200         }
23201     }
23202   else
23203     gcc_unreachable ();
23204 }
23205
23206
23207
23208 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
23209    issue an error message indicating that TOKEN_DESC was expected.
23210
23211    Returns the token consumed, if the token had the appropriate type.
23212    Otherwise, returns NULL.  */
23213
23214 static cp_token *
23215 cp_parser_require (cp_parser* parser,
23216                    enum cpp_ttype type,
23217                    required_token token_desc)
23218 {
23219   if (cp_lexer_next_token_is (parser->lexer, type))
23220     return cp_lexer_consume_token (parser->lexer);
23221   else
23222     {
23223       /* Output the MESSAGE -- unless we're parsing tentatively.  */
23224       if (!cp_parser_simulate_error (parser))
23225         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23226       return NULL;
23227     }
23228 }
23229
23230 /* An error message is produced if the next token is not '>'.
23231    All further tokens are skipped until the desired token is
23232    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
23233
23234 static void
23235 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23236 {
23237   /* Current level of '< ... >'.  */
23238   unsigned level = 0;
23239   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
23240   unsigned nesting_depth = 0;
23241
23242   /* Are we ready, yet?  If not, issue error message.  */
23243   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23244     return;
23245
23246   /* Skip tokens until the desired token is found.  */
23247   while (true)
23248     {
23249       /* Peek at the next token.  */
23250       switch (cp_lexer_peek_token (parser->lexer)->type)
23251         {
23252         case CPP_LESS:
23253           if (!nesting_depth)
23254             ++level;
23255           break;
23256
23257         case CPP_RSHIFT:
23258           if (cxx_dialect == cxx98)
23259             /* C++0x views the `>>' operator as two `>' tokens, but
23260                C++98 does not. */
23261             break;
23262           else if (!nesting_depth && level-- == 0)
23263             {
23264               /* We've hit a `>>' where the first `>' closes the
23265                  template argument list, and the second `>' is
23266                  spurious.  Just consume the `>>' and stop; we've
23267                  already produced at least one error.  */
23268               cp_lexer_consume_token (parser->lexer);
23269               return;
23270             }
23271           /* Fall through for C++0x, so we handle the second `>' in
23272              the `>>'.  */
23273
23274         case CPP_GREATER:
23275           if (!nesting_depth && level-- == 0)
23276             {
23277               /* We've reached the token we want, consume it and stop.  */
23278               cp_lexer_consume_token (parser->lexer);
23279               return;
23280             }
23281           break;
23282
23283         case CPP_OPEN_PAREN:
23284         case CPP_OPEN_SQUARE:
23285           ++nesting_depth;
23286           break;
23287
23288         case CPP_CLOSE_PAREN:
23289         case CPP_CLOSE_SQUARE:
23290           if (nesting_depth-- == 0)
23291             return;
23292           break;
23293
23294         case CPP_EOF:
23295         case CPP_PRAGMA_EOL:
23296         case CPP_SEMICOLON:
23297         case CPP_OPEN_BRACE:
23298         case CPP_CLOSE_BRACE:
23299           /* The '>' was probably forgotten, don't look further.  */
23300           return;
23301
23302         default:
23303           break;
23304         }
23305
23306       /* Consume this token.  */
23307       cp_lexer_consume_token (parser->lexer);
23308     }
23309 }
23310
23311 /* If the next token is the indicated keyword, consume it.  Otherwise,
23312    issue an error message indicating that TOKEN_DESC was expected.
23313
23314    Returns the token consumed, if the token had the appropriate type.
23315    Otherwise, returns NULL.  */
23316
23317 static cp_token *
23318 cp_parser_require_keyword (cp_parser* parser,
23319                            enum rid keyword,
23320                            required_token token_desc)
23321 {
23322   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23323
23324   if (token && token->keyword != keyword)
23325     {
23326       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
23327       return NULL;
23328     }
23329
23330   return token;
23331 }
23332
23333 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23334    function-definition.  */
23335
23336 static bool
23337 cp_parser_token_starts_function_definition_p (cp_token* token)
23338 {
23339   return (/* An ordinary function-body begins with an `{'.  */
23340           token->type == CPP_OPEN_BRACE
23341           /* A ctor-initializer begins with a `:'.  */
23342           || token->type == CPP_COLON
23343           /* A function-try-block begins with `try'.  */
23344           || token->keyword == RID_TRY
23345           /* A function-transaction-block begins with `__transaction_atomic'
23346              or `__transaction_relaxed'.  */
23347           || token->keyword == RID_TRANSACTION_ATOMIC
23348           || token->keyword == RID_TRANSACTION_RELAXED
23349           /* The named return value extension begins with `return'.  */
23350           || token->keyword == RID_RETURN);
23351 }
23352
23353 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23354    definition.  */
23355
23356 static bool
23357 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23358 {
23359   cp_token *token;
23360
23361   token = cp_lexer_peek_token (parser->lexer);
23362   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23363 }
23364
23365 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23366    C++0x) ending a template-argument.  */
23367
23368 static bool
23369 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23370 {
23371   cp_token *token;
23372
23373   token = cp_lexer_peek_token (parser->lexer);
23374   return (token->type == CPP_COMMA 
23375           || token->type == CPP_GREATER
23376           || token->type == CPP_ELLIPSIS
23377           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23378 }
23379
23380 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23381    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
23382
23383 static bool
23384 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23385                                                      size_t n)
23386 {
23387   cp_token *token;
23388
23389   token = cp_lexer_peek_nth_token (parser->lexer, n);
23390   if (token->type == CPP_LESS)
23391     return true;
23392   /* Check for the sequence `<::' in the original code. It would be lexed as
23393      `[:', where `[' is a digraph, and there is no whitespace before
23394      `:'.  */
23395   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23396     {
23397       cp_token *token2;
23398       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23399       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23400         return true;
23401     }
23402   return false;
23403 }
23404
23405 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23406    or none_type otherwise.  */
23407
23408 static enum tag_types
23409 cp_parser_token_is_class_key (cp_token* token)
23410 {
23411   switch (token->keyword)
23412     {
23413     case RID_CLASS:
23414       return class_type;
23415     case RID_STRUCT:
23416       return record_type;
23417     case RID_UNION:
23418       return union_type;
23419
23420     default:
23421       return none_type;
23422     }
23423 }
23424
23425 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
23426
23427 static void
23428 cp_parser_check_class_key (enum tag_types class_key, tree type)
23429 {
23430   if (type == error_mark_node)
23431     return;
23432   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23433     {
23434       permerror (input_location, "%qs tag used in naming %q#T",
23435                  class_key == union_type ? "union"
23436                  : class_key == record_type ? "struct" : "class",
23437                  type);
23438       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23439               "%q#T was previously declared here", type);
23440     }
23441 }
23442
23443 /* Issue an error message if DECL is redeclared with different
23444    access than its original declaration [class.access.spec/3].
23445    This applies to nested classes and nested class templates.
23446    [class.mem/1].  */
23447
23448 static void
23449 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23450 {
23451   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23452     return;
23453
23454   if ((TREE_PRIVATE (decl)
23455        != (current_access_specifier == access_private_node))
23456       || (TREE_PROTECTED (decl)
23457           != (current_access_specifier == access_protected_node)))
23458     error_at (location, "%qD redeclared with different access", decl);
23459 }
23460
23461 /* Look for the `template' keyword, as a syntactic disambiguator.
23462    Return TRUE iff it is present, in which case it will be
23463    consumed.  */
23464
23465 static bool
23466 cp_parser_optional_template_keyword (cp_parser *parser)
23467 {
23468   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23469     {
23470       /* In C++98 the `template' keyword can only be used within templates;
23471          outside templates the parser can always figure out what is a
23472          template and what is not.  In C++11,  per the resolution of DR 468,
23473          `template' is allowed in cases where it is not strictly necessary.  */
23474       if (!processing_template_decl
23475           && pedantic && cxx_dialect == cxx98)
23476         {
23477           cp_token *token = cp_lexer_peek_token (parser->lexer);
23478           pedwarn (token->location, OPT_Wpedantic,
23479                    "in C++98 %<template%> (as a disambiguator) is only "
23480                    "allowed within templates");
23481           /* If this part of the token stream is rescanned, the same
23482              error message would be generated.  So, we purge the token
23483              from the stream.  */
23484           cp_lexer_purge_token (parser->lexer);
23485           return false;
23486         }
23487       else
23488         {
23489           /* Consume the `template' keyword.  */
23490           cp_lexer_consume_token (parser->lexer);
23491           return true;
23492         }
23493     }
23494   return false;
23495 }
23496
23497 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
23498    set PARSER->SCOPE, and perform other related actions.  */
23499
23500 static void
23501 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23502 {
23503   int i;
23504   struct tree_check *check_value;
23505   deferred_access_check *chk;
23506   vec<deferred_access_check, va_gc> *checks;
23507
23508   /* Get the stored value.  */
23509   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23510   /* Perform any access checks that were deferred.  */
23511   checks = check_value->checks;
23512   if (checks)
23513     {
23514       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23515         perform_or_defer_access_check (chk->binfo,
23516                                        chk->decl,
23517                                        chk->diag_decl, tf_warning_or_error);
23518     }
23519   /* Set the scope from the stored value.  */
23520   parser->scope = check_value->value;
23521   parser->qualifying_scope = check_value->qualifying_scope;
23522   parser->object_scope = NULL_TREE;
23523 }
23524
23525 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
23526    encounter the end of a block before what we were looking for.  */
23527
23528 static bool
23529 cp_parser_cache_group (cp_parser *parser,
23530                        enum cpp_ttype end,
23531                        unsigned depth)
23532 {
23533   while (true)
23534     {
23535       cp_token *token = cp_lexer_peek_token (parser->lexer);
23536
23537       /* Abort a parenthesized expression if we encounter a semicolon.  */
23538       if ((end == CPP_CLOSE_PAREN || depth == 0)
23539           && token->type == CPP_SEMICOLON)
23540         return true;
23541       /* If we've reached the end of the file, stop.  */
23542       if (token->type == CPP_EOF
23543           || (end != CPP_PRAGMA_EOL
23544               && token->type == CPP_PRAGMA_EOL))
23545         return true;
23546       if (token->type == CPP_CLOSE_BRACE && depth == 0)
23547         /* We've hit the end of an enclosing block, so there's been some
23548            kind of syntax error.  */
23549         return true;
23550
23551       /* Consume the token.  */
23552       cp_lexer_consume_token (parser->lexer);
23553       /* See if it starts a new group.  */
23554       if (token->type == CPP_OPEN_BRACE)
23555         {
23556           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23557           /* In theory this should probably check end == '}', but
23558              cp_parser_save_member_function_body needs it to exit
23559              after either '}' or ')' when called with ')'.  */
23560           if (depth == 0)
23561             return false;
23562         }
23563       else if (token->type == CPP_OPEN_PAREN)
23564         {
23565           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23566           if (depth == 0 && end == CPP_CLOSE_PAREN)
23567             return false;
23568         }
23569       else if (token->type == CPP_PRAGMA)
23570         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23571       else if (token->type == end)
23572         return false;
23573     }
23574 }
23575
23576 /* Like above, for caching a default argument or NSDMI.  Both of these are
23577    terminated by a non-nested comma, but it can be unclear whether or not a
23578    comma is nested in a template argument list unless we do more parsing.
23579    In order to handle this ambiguity, when we encounter a ',' after a '<'
23580    we try to parse what follows as a parameter-declaration-list (in the
23581    case of a default argument) or a member-declarator (in the case of an
23582    NSDMI).  If that succeeds, then we stop caching.  */
23583
23584 static tree
23585 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23586 {
23587   unsigned depth = 0;
23588   int maybe_template_id = 0;
23589   cp_token *first_token;
23590   cp_token *token;
23591   tree default_argument;
23592
23593   /* Add tokens until we have processed the entire default
23594      argument.  We add the range [first_token, token).  */
23595   first_token = cp_lexer_peek_token (parser->lexer);
23596   if (first_token->type == CPP_OPEN_BRACE)
23597     {
23598       /* For list-initialization, this is straightforward.  */
23599       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23600       token = cp_lexer_peek_token (parser->lexer);
23601     }
23602   else while (true)
23603     {
23604       bool done = false;
23605
23606       /* Peek at the next token.  */
23607       token = cp_lexer_peek_token (parser->lexer);
23608       /* What we do depends on what token we have.  */
23609       switch (token->type)
23610         {
23611           /* In valid code, a default argument must be
23612              immediately followed by a `,' `)', or `...'.  */
23613         case CPP_COMMA:
23614           if (depth == 0 && maybe_template_id)
23615             {
23616               /* If we've seen a '<', we might be in a
23617                  template-argument-list.  Until Core issue 325 is
23618                  resolved, we don't know how this situation ought
23619                  to be handled, so try to DTRT.  We check whether
23620                  what comes after the comma is a valid parameter
23621                  declaration list.  If it is, then the comma ends
23622                  the default argument; otherwise the default
23623                  argument continues.  */
23624               bool error = false;
23625               tree t;
23626
23627               /* Set ITALP so cp_parser_parameter_declaration_list
23628                  doesn't decide to commit to this parse.  */
23629               bool saved_italp = parser->in_template_argument_list_p;
23630               parser->in_template_argument_list_p = true;
23631
23632               cp_parser_parse_tentatively (parser);
23633               cp_lexer_consume_token (parser->lexer);
23634
23635               if (nsdmi)
23636                 {
23637                   int ctor_dtor_or_conv_p;
23638                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23639                                         &ctor_dtor_or_conv_p,
23640                                         /*parenthesized_p=*/NULL,
23641                                         /*member_p=*/true);
23642                 }
23643               else
23644                 {
23645                   begin_scope (sk_function_parms, NULL_TREE);
23646                   cp_parser_parameter_declaration_list (parser, &error);
23647                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23648                     pop_binding (DECL_NAME (t), t);
23649                   leave_scope ();
23650                 }
23651               if (!cp_parser_error_occurred (parser) && !error)
23652                 done = true;
23653               cp_parser_abort_tentative_parse (parser);
23654
23655               parser->in_template_argument_list_p = saved_italp;
23656               break;
23657             }
23658         case CPP_CLOSE_PAREN:
23659         case CPP_ELLIPSIS:
23660           /* If we run into a non-nested `;', `}', or `]',
23661              then the code is invalid -- but the default
23662              argument is certainly over.  */
23663         case CPP_SEMICOLON:
23664         case CPP_CLOSE_BRACE:
23665         case CPP_CLOSE_SQUARE:
23666           if (depth == 0)
23667             done = true;
23668           /* Update DEPTH, if necessary.  */
23669           else if (token->type == CPP_CLOSE_PAREN
23670                    || token->type == CPP_CLOSE_BRACE
23671                    || token->type == CPP_CLOSE_SQUARE)
23672             --depth;
23673           break;
23674
23675         case CPP_OPEN_PAREN:
23676         case CPP_OPEN_SQUARE:
23677         case CPP_OPEN_BRACE:
23678           ++depth;
23679           break;
23680
23681         case CPP_LESS:
23682           if (depth == 0)
23683             /* This might be the comparison operator, or it might
23684                start a template argument list.  */
23685             ++maybe_template_id;
23686           break;
23687
23688         case CPP_RSHIFT:
23689           if (cxx_dialect == cxx98)
23690             break;
23691           /* Fall through for C++0x, which treats the `>>'
23692              operator like two `>' tokens in certain
23693              cases.  */
23694
23695         case CPP_GREATER:
23696           if (depth == 0)
23697             {
23698               /* This might be an operator, or it might close a
23699                  template argument list.  But if a previous '<'
23700                  started a template argument list, this will have
23701                  closed it, so we can't be in one anymore.  */
23702               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23703               if (maybe_template_id < 0)
23704                 maybe_template_id = 0;
23705             }
23706           break;
23707
23708           /* If we run out of tokens, issue an error message.  */
23709         case CPP_EOF:
23710         case CPP_PRAGMA_EOL:
23711           error_at (token->location, "file ends in default argument");
23712           done = true;
23713           break;
23714
23715         case CPP_NAME:
23716         case CPP_SCOPE:
23717           /* In these cases, we should look for template-ids.
23718              For example, if the default argument is
23719              `X<int, double>()', we need to do name lookup to
23720              figure out whether or not `X' is a template; if
23721              so, the `,' does not end the default argument.
23722
23723              That is not yet done.  */
23724           break;
23725
23726         default:
23727           break;
23728         }
23729
23730       /* If we've reached the end, stop.  */
23731       if (done)
23732         break;
23733
23734       /* Add the token to the token block.  */
23735       token = cp_lexer_consume_token (parser->lexer);
23736     }
23737
23738   /* Create a DEFAULT_ARG to represent the unparsed default
23739      argument.  */
23740   default_argument = make_node (DEFAULT_ARG);
23741   DEFARG_TOKENS (default_argument)
23742     = cp_token_cache_new (first_token, token);
23743   DEFARG_INSTANTIATIONS (default_argument) = NULL;
23744
23745   return default_argument;
23746 }
23747
23748 /* Begin parsing tentatively.  We always save tokens while parsing
23749    tentatively so that if the tentative parsing fails we can restore the
23750    tokens.  */
23751
23752 static void
23753 cp_parser_parse_tentatively (cp_parser* parser)
23754 {
23755   /* Enter a new parsing context.  */
23756   parser->context = cp_parser_context_new (parser->context);
23757   /* Begin saving tokens.  */
23758   cp_lexer_save_tokens (parser->lexer);
23759   /* In order to avoid repetitive access control error messages,
23760      access checks are queued up until we are no longer parsing
23761      tentatively.  */
23762   push_deferring_access_checks (dk_deferred);
23763 }
23764
23765 /* Commit to the currently active tentative parse.  */
23766
23767 static void
23768 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23769 {
23770   cp_parser_context *context;
23771   cp_lexer *lexer;
23772
23773   /* Mark all of the levels as committed.  */
23774   lexer = parser->lexer;
23775   for (context = parser->context; context->next; context = context->next)
23776     {
23777       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23778         break;
23779       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23780       while (!cp_lexer_saving_tokens (lexer))
23781         lexer = lexer->next;
23782       cp_lexer_commit_tokens (lexer);
23783     }
23784 }
23785
23786 /* Abort the currently active tentative parse.  All consumed tokens
23787    will be rolled back, and no diagnostics will be issued.  */
23788
23789 static void
23790 cp_parser_abort_tentative_parse (cp_parser* parser)
23791 {
23792   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23793               || errorcount > 0);
23794   cp_parser_simulate_error (parser);
23795   /* Now, pretend that we want to see if the construct was
23796      successfully parsed.  */
23797   cp_parser_parse_definitely (parser);
23798 }
23799
23800 /* Stop parsing tentatively.  If a parse error has occurred, restore the
23801    token stream.  Otherwise, commit to the tokens we have consumed.
23802    Returns true if no error occurred; false otherwise.  */
23803
23804 static bool
23805 cp_parser_parse_definitely (cp_parser* parser)
23806 {
23807   bool error_occurred;
23808   cp_parser_context *context;
23809
23810   /* Remember whether or not an error occurred, since we are about to
23811      destroy that information.  */
23812   error_occurred = cp_parser_error_occurred (parser);
23813   /* Remove the topmost context from the stack.  */
23814   context = parser->context;
23815   parser->context = context->next;
23816   /* If no parse errors occurred, commit to the tentative parse.  */
23817   if (!error_occurred)
23818     {
23819       /* Commit to the tokens read tentatively, unless that was
23820          already done.  */
23821       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23822         cp_lexer_commit_tokens (parser->lexer);
23823
23824       pop_to_parent_deferring_access_checks ();
23825     }
23826   /* Otherwise, if errors occurred, roll back our state so that things
23827      are just as they were before we began the tentative parse.  */
23828   else
23829     {
23830       cp_lexer_rollback_tokens (parser->lexer);
23831       pop_deferring_access_checks ();
23832     }
23833   /* Add the context to the front of the free list.  */
23834   context->next = cp_parser_context_free_list;
23835   cp_parser_context_free_list = context;
23836
23837   return !error_occurred;
23838 }
23839
23840 /* Returns true if we are parsing tentatively and are not committed to
23841    this tentative parse.  */
23842
23843 static bool
23844 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23845 {
23846   return (cp_parser_parsing_tentatively (parser)
23847           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23848 }
23849
23850 /* Returns nonzero iff an error has occurred during the most recent
23851    tentative parse.  */
23852
23853 static bool
23854 cp_parser_error_occurred (cp_parser* parser)
23855 {
23856   return (cp_parser_parsing_tentatively (parser)
23857           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23858 }
23859
23860 /* Returns nonzero if GNU extensions are allowed.  */
23861
23862 static bool
23863 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23864 {
23865   return parser->allow_gnu_extensions_p;
23866 }
23867 \f
23868 /* Objective-C++ Productions */
23869
23870
23871 /* Parse an Objective-C expression, which feeds into a primary-expression
23872    above.
23873
23874    objc-expression:
23875      objc-message-expression
23876      objc-string-literal
23877      objc-encode-expression
23878      objc-protocol-expression
23879      objc-selector-expression
23880
23881   Returns a tree representation of the expression.  */
23882
23883 static tree
23884 cp_parser_objc_expression (cp_parser* parser)
23885 {
23886   /* Try to figure out what kind of declaration is present.  */
23887   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23888
23889   switch (kwd->type)
23890     {
23891     case CPP_OPEN_SQUARE:
23892       return cp_parser_objc_message_expression (parser);
23893
23894     case CPP_OBJC_STRING:
23895       kwd = cp_lexer_consume_token (parser->lexer);
23896       return objc_build_string_object (kwd->u.value);
23897
23898     case CPP_KEYWORD:
23899       switch (kwd->keyword)
23900         {
23901         case RID_AT_ENCODE:
23902           return cp_parser_objc_encode_expression (parser);
23903
23904         case RID_AT_PROTOCOL:
23905           return cp_parser_objc_protocol_expression (parser);
23906
23907         case RID_AT_SELECTOR:
23908           return cp_parser_objc_selector_expression (parser);
23909
23910         default:
23911           break;
23912         }
23913     default:
23914       error_at (kwd->location,
23915                 "misplaced %<@%D%> Objective-C++ construct",
23916                 kwd->u.value);
23917       cp_parser_skip_to_end_of_block_or_statement (parser);
23918     }
23919
23920   return error_mark_node;
23921 }
23922
23923 /* Parse an Objective-C message expression.
23924
23925    objc-message-expression:
23926      [ objc-message-receiver objc-message-args ]
23927
23928    Returns a representation of an Objective-C message.  */
23929
23930 static tree
23931 cp_parser_objc_message_expression (cp_parser* parser)
23932 {
23933   tree receiver, messageargs;
23934
23935   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
23936   receiver = cp_parser_objc_message_receiver (parser);
23937   messageargs = cp_parser_objc_message_args (parser);
23938   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23939
23940   return objc_build_message_expr (receiver, messageargs);
23941 }
23942
23943 /* Parse an objc-message-receiver.
23944
23945    objc-message-receiver:
23946      expression
23947      simple-type-specifier
23948
23949   Returns a representation of the type or expression.  */
23950
23951 static tree
23952 cp_parser_objc_message_receiver (cp_parser* parser)
23953 {
23954   tree rcv;
23955
23956   /* An Objective-C message receiver may be either (1) a type
23957      or (2) an expression.  */
23958   cp_parser_parse_tentatively (parser);
23959   rcv = cp_parser_expression (parser, false, NULL);
23960
23961   if (cp_parser_parse_definitely (parser))
23962     return rcv;
23963
23964   rcv = cp_parser_simple_type_specifier (parser,
23965                                          /*decl_specs=*/NULL,
23966                                          CP_PARSER_FLAGS_NONE);
23967
23968   return objc_get_class_reference (rcv);
23969 }
23970
23971 /* Parse the arguments and selectors comprising an Objective-C message.
23972
23973    objc-message-args:
23974      objc-selector
23975      objc-selector-args
23976      objc-selector-args , objc-comma-args
23977
23978    objc-selector-args:
23979      objc-selector [opt] : assignment-expression
23980      objc-selector-args objc-selector [opt] : assignment-expression
23981
23982    objc-comma-args:
23983      assignment-expression
23984      objc-comma-args , assignment-expression
23985
23986    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23987    selector arguments and TREE_VALUE containing a list of comma
23988    arguments.  */
23989
23990 static tree
23991 cp_parser_objc_message_args (cp_parser* parser)
23992 {
23993   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23994   bool maybe_unary_selector_p = true;
23995   cp_token *token = cp_lexer_peek_token (parser->lexer);
23996
23997   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23998     {
23999       tree selector = NULL_TREE, arg;
24000
24001       if (token->type != CPP_COLON)
24002         selector = cp_parser_objc_selector (parser);
24003
24004       /* Detect if we have a unary selector.  */
24005       if (maybe_unary_selector_p
24006           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24007         return build_tree_list (selector, NULL_TREE);
24008
24009       maybe_unary_selector_p = false;
24010       cp_parser_require (parser, CPP_COLON, RT_COLON);
24011       arg = cp_parser_assignment_expression (parser, false, NULL);
24012
24013       sel_args
24014         = chainon (sel_args,
24015                    build_tree_list (selector, arg));
24016
24017       token = cp_lexer_peek_token (parser->lexer);
24018     }
24019
24020   /* Handle non-selector arguments, if any. */
24021   while (token->type == CPP_COMMA)
24022     {
24023       tree arg;
24024
24025       cp_lexer_consume_token (parser->lexer);
24026       arg = cp_parser_assignment_expression (parser, false, NULL);
24027
24028       addl_args
24029         = chainon (addl_args,
24030                    build_tree_list (NULL_TREE, arg));
24031
24032       token = cp_lexer_peek_token (parser->lexer);
24033     }
24034
24035   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24036     {
24037       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24038       return build_tree_list (error_mark_node, error_mark_node);
24039     }
24040
24041   return build_tree_list (sel_args, addl_args);
24042 }
24043
24044 /* Parse an Objective-C encode expression.
24045
24046    objc-encode-expression:
24047      @encode objc-typename
24048
24049    Returns an encoded representation of the type argument.  */
24050
24051 static tree
24052 cp_parser_objc_encode_expression (cp_parser* parser)
24053 {
24054   tree type;
24055   cp_token *token;
24056
24057   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
24058   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24059   token = cp_lexer_peek_token (parser->lexer);
24060   type = complete_type (cp_parser_type_id (parser));
24061   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24062
24063   if (!type)
24064     {
24065       error_at (token->location, 
24066                 "%<@encode%> must specify a type as an argument");
24067       return error_mark_node;
24068     }
24069
24070   /* This happens if we find @encode(T) (where T is a template
24071      typename or something dependent on a template typename) when
24072      parsing a template.  In that case, we can't compile it
24073      immediately, but we rather create an AT_ENCODE_EXPR which will
24074      need to be instantiated when the template is used.
24075   */
24076   if (dependent_type_p (type))
24077     {
24078       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24079       TREE_READONLY (value) = 1;
24080       return value;
24081     }
24082
24083   return objc_build_encode_expr (type);
24084 }
24085
24086 /* Parse an Objective-C @defs expression.  */
24087
24088 static tree
24089 cp_parser_objc_defs_expression (cp_parser *parser)
24090 {
24091   tree name;
24092
24093   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
24094   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24095   name = cp_parser_identifier (parser);
24096   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24097
24098   return objc_get_class_ivars (name);
24099 }
24100
24101 /* Parse an Objective-C protocol expression.
24102
24103   objc-protocol-expression:
24104     @protocol ( identifier )
24105
24106   Returns a representation of the protocol expression.  */
24107
24108 static tree
24109 cp_parser_objc_protocol_expression (cp_parser* parser)
24110 {
24111   tree proto;
24112
24113   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24114   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24115   proto = cp_parser_identifier (parser);
24116   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24117
24118   return objc_build_protocol_expr (proto);
24119 }
24120
24121 /* Parse an Objective-C selector expression.
24122
24123    objc-selector-expression:
24124      @selector ( objc-method-signature )
24125
24126    objc-method-signature:
24127      objc-selector
24128      objc-selector-seq
24129
24130    objc-selector-seq:
24131      objc-selector :
24132      objc-selector-seq objc-selector :
24133
24134   Returns a representation of the method selector.  */
24135
24136 static tree
24137 cp_parser_objc_selector_expression (cp_parser* parser)
24138 {
24139   tree sel_seq = NULL_TREE;
24140   bool maybe_unary_selector_p = true;
24141   cp_token *token;
24142   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24143
24144   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
24145   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24146   token = cp_lexer_peek_token (parser->lexer);
24147
24148   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24149          || token->type == CPP_SCOPE)
24150     {
24151       tree selector = NULL_TREE;
24152
24153       if (token->type != CPP_COLON
24154           || token->type == CPP_SCOPE)
24155         selector = cp_parser_objc_selector (parser);
24156
24157       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24158           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24159         {
24160           /* Detect if we have a unary selector.  */
24161           if (maybe_unary_selector_p)
24162             {
24163               sel_seq = selector;
24164               goto finish_selector;
24165             }
24166           else
24167             {
24168               cp_parser_error (parser, "expected %<:%>");
24169             }
24170         }
24171       maybe_unary_selector_p = false;
24172       token = cp_lexer_consume_token (parser->lexer);
24173
24174       if (token->type == CPP_SCOPE)
24175         {
24176           sel_seq
24177             = chainon (sel_seq,
24178                        build_tree_list (selector, NULL_TREE));
24179           sel_seq
24180             = chainon (sel_seq,
24181                        build_tree_list (NULL_TREE, NULL_TREE));
24182         }
24183       else
24184         sel_seq
24185           = chainon (sel_seq,
24186                      build_tree_list (selector, NULL_TREE));
24187
24188       token = cp_lexer_peek_token (parser->lexer);
24189     }
24190
24191  finish_selector:
24192   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24193
24194   return objc_build_selector_expr (loc, sel_seq);
24195 }
24196
24197 /* Parse a list of identifiers.
24198
24199    objc-identifier-list:
24200      identifier
24201      objc-identifier-list , identifier
24202
24203    Returns a TREE_LIST of identifier nodes.  */
24204
24205 static tree
24206 cp_parser_objc_identifier_list (cp_parser* parser)
24207 {
24208   tree identifier;
24209   tree list;
24210   cp_token *sep;
24211
24212   identifier = cp_parser_identifier (parser);
24213   if (identifier == error_mark_node)
24214     return error_mark_node;      
24215
24216   list = build_tree_list (NULL_TREE, identifier);
24217   sep = cp_lexer_peek_token (parser->lexer);
24218
24219   while (sep->type == CPP_COMMA)
24220     {
24221       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24222       identifier = cp_parser_identifier (parser);
24223       if (identifier == error_mark_node)
24224         return list;
24225
24226       list = chainon (list, build_tree_list (NULL_TREE,
24227                                              identifier));
24228       sep = cp_lexer_peek_token (parser->lexer);
24229     }
24230   
24231   return list;
24232 }
24233
24234 /* Parse an Objective-C alias declaration.
24235
24236    objc-alias-declaration:
24237      @compatibility_alias identifier identifier ;
24238
24239    This function registers the alias mapping with the Objective-C front end.
24240    It returns nothing.  */
24241
24242 static void
24243 cp_parser_objc_alias_declaration (cp_parser* parser)
24244 {
24245   tree alias, orig;
24246
24247   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
24248   alias = cp_parser_identifier (parser);
24249   orig = cp_parser_identifier (parser);
24250   objc_declare_alias (alias, orig);
24251   cp_parser_consume_semicolon_at_end_of_statement (parser);
24252 }
24253
24254 /* Parse an Objective-C class forward-declaration.
24255
24256    objc-class-declaration:
24257      @class objc-identifier-list ;
24258
24259    The function registers the forward declarations with the Objective-C
24260    front end.  It returns nothing.  */
24261
24262 static void
24263 cp_parser_objc_class_declaration (cp_parser* parser)
24264 {
24265   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
24266   while (true)
24267     {
24268       tree id;
24269       
24270       id = cp_parser_identifier (parser);
24271       if (id == error_mark_node)
24272         break;
24273       
24274       objc_declare_class (id);
24275
24276       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24277         cp_lexer_consume_token (parser->lexer);
24278       else
24279         break;
24280     }
24281   cp_parser_consume_semicolon_at_end_of_statement (parser);
24282 }
24283
24284 /* Parse a list of Objective-C protocol references.
24285
24286    objc-protocol-refs-opt:
24287      objc-protocol-refs [opt]
24288
24289    objc-protocol-refs:
24290      < objc-identifier-list >
24291
24292    Returns a TREE_LIST of identifiers, if any.  */
24293
24294 static tree
24295 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24296 {
24297   tree protorefs = NULL_TREE;
24298
24299   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24300     {
24301       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
24302       protorefs = cp_parser_objc_identifier_list (parser);
24303       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24304     }
24305
24306   return protorefs;
24307 }
24308
24309 /* Parse a Objective-C visibility specification.  */
24310
24311 static void
24312 cp_parser_objc_visibility_spec (cp_parser* parser)
24313 {
24314   cp_token *vis = cp_lexer_peek_token (parser->lexer);
24315
24316   switch (vis->keyword)
24317     {
24318     case RID_AT_PRIVATE:
24319       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24320       break;
24321     case RID_AT_PROTECTED:
24322       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24323       break;
24324     case RID_AT_PUBLIC:
24325       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24326       break;
24327     case RID_AT_PACKAGE:
24328       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24329       break;
24330     default:
24331       return;
24332     }
24333
24334   /* Eat '@private'/'@protected'/'@public'.  */
24335   cp_lexer_consume_token (parser->lexer);
24336 }
24337
24338 /* Parse an Objective-C method type.  Return 'true' if it is a class
24339    (+) method, and 'false' if it is an instance (-) method.  */
24340
24341 static inline bool
24342 cp_parser_objc_method_type (cp_parser* parser)
24343 {
24344   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24345     return true;
24346   else
24347     return false;
24348 }
24349
24350 /* Parse an Objective-C protocol qualifier.  */
24351
24352 static tree
24353 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24354 {
24355   tree quals = NULL_TREE, node;
24356   cp_token *token = cp_lexer_peek_token (parser->lexer);
24357
24358   node = token->u.value;
24359
24360   while (node && TREE_CODE (node) == IDENTIFIER_NODE
24361          && (node == ridpointers [(int) RID_IN]
24362              || node == ridpointers [(int) RID_OUT]
24363              || node == ridpointers [(int) RID_INOUT]
24364              || node == ridpointers [(int) RID_BYCOPY]
24365              || node == ridpointers [(int) RID_BYREF]
24366              || node == ridpointers [(int) RID_ONEWAY]))
24367     {
24368       quals = tree_cons (NULL_TREE, node, quals);
24369       cp_lexer_consume_token (parser->lexer);
24370       token = cp_lexer_peek_token (parser->lexer);
24371       node = token->u.value;
24372     }
24373
24374   return quals;
24375 }
24376
24377 /* Parse an Objective-C typename.  */
24378
24379 static tree
24380 cp_parser_objc_typename (cp_parser* parser)
24381 {
24382   tree type_name = NULL_TREE;
24383
24384   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24385     {
24386       tree proto_quals, cp_type = NULL_TREE;
24387
24388       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24389       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24390
24391       /* An ObjC type name may consist of just protocol qualifiers, in which
24392          case the type shall default to 'id'.  */
24393       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24394         {
24395           cp_type = cp_parser_type_id (parser);
24396           
24397           /* If the type could not be parsed, an error has already
24398              been produced.  For error recovery, behave as if it had
24399              not been specified, which will use the default type
24400              'id'.  */
24401           if (cp_type == error_mark_node)
24402             {
24403               cp_type = NULL_TREE;
24404               /* We need to skip to the closing parenthesis as
24405                  cp_parser_type_id() does not seem to do it for
24406                  us.  */
24407               cp_parser_skip_to_closing_parenthesis (parser,
24408                                                      /*recovering=*/true,
24409                                                      /*or_comma=*/false,
24410                                                      /*consume_paren=*/false);
24411             }
24412         }
24413
24414       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24415       type_name = build_tree_list (proto_quals, cp_type);
24416     }
24417
24418   return type_name;
24419 }
24420
24421 /* Check to see if TYPE refers to an Objective-C selector name.  */
24422
24423 static bool
24424 cp_parser_objc_selector_p (enum cpp_ttype type)
24425 {
24426   return (type == CPP_NAME || type == CPP_KEYWORD
24427           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24428           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24429           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24430           || type == CPP_XOR || type == CPP_XOR_EQ);
24431 }
24432
24433 /* Parse an Objective-C selector.  */
24434
24435 static tree
24436 cp_parser_objc_selector (cp_parser* parser)
24437 {
24438   cp_token *token = cp_lexer_consume_token (parser->lexer);
24439
24440   if (!cp_parser_objc_selector_p (token->type))
24441     {
24442       error_at (token->location, "invalid Objective-C++ selector name");
24443       return error_mark_node;
24444     }
24445
24446   /* C++ operator names are allowed to appear in ObjC selectors.  */
24447   switch (token->type)
24448     {
24449     case CPP_AND_AND: return get_identifier ("and");
24450     case CPP_AND_EQ: return get_identifier ("and_eq");
24451     case CPP_AND: return get_identifier ("bitand");
24452     case CPP_OR: return get_identifier ("bitor");
24453     case CPP_COMPL: return get_identifier ("compl");
24454     case CPP_NOT: return get_identifier ("not");
24455     case CPP_NOT_EQ: return get_identifier ("not_eq");
24456     case CPP_OR_OR: return get_identifier ("or");
24457     case CPP_OR_EQ: return get_identifier ("or_eq");
24458     case CPP_XOR: return get_identifier ("xor");
24459     case CPP_XOR_EQ: return get_identifier ("xor_eq");
24460     default: return token->u.value;
24461     }
24462 }
24463
24464 /* Parse an Objective-C params list.  */
24465
24466 static tree
24467 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24468 {
24469   tree params = NULL_TREE;
24470   bool maybe_unary_selector_p = true;
24471   cp_token *token = cp_lexer_peek_token (parser->lexer);
24472
24473   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24474     {
24475       tree selector = NULL_TREE, type_name, identifier;
24476       tree parm_attr = NULL_TREE;
24477
24478       if (token->keyword == RID_ATTRIBUTE)
24479         break;
24480
24481       if (token->type != CPP_COLON)
24482         selector = cp_parser_objc_selector (parser);
24483
24484       /* Detect if we have a unary selector.  */
24485       if (maybe_unary_selector_p
24486           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24487         {
24488           params = selector; /* Might be followed by attributes.  */
24489           break;
24490         }
24491
24492       maybe_unary_selector_p = false;
24493       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24494         {
24495           /* Something went quite wrong.  There should be a colon
24496              here, but there is not.  Stop parsing parameters.  */
24497           break;
24498         }
24499       type_name = cp_parser_objc_typename (parser);
24500       /* New ObjC allows attributes on parameters too.  */
24501       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24502         parm_attr = cp_parser_attributes_opt (parser);
24503       identifier = cp_parser_identifier (parser);
24504
24505       params
24506         = chainon (params,
24507                    objc_build_keyword_decl (selector,
24508                                             type_name,
24509                                             identifier,
24510                                             parm_attr));
24511
24512       token = cp_lexer_peek_token (parser->lexer);
24513     }
24514
24515   if (params == NULL_TREE)
24516     {
24517       cp_parser_error (parser, "objective-c++ method declaration is expected");
24518       return error_mark_node;
24519     }
24520
24521   /* We allow tail attributes for the method.  */
24522   if (token->keyword == RID_ATTRIBUTE)
24523     {
24524       *attributes = cp_parser_attributes_opt (parser);
24525       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24526           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24527         return params;
24528       cp_parser_error (parser, 
24529                        "method attributes must be specified at the end");
24530       return error_mark_node;
24531     }
24532
24533   if (params == NULL_TREE)
24534     {
24535       cp_parser_error (parser, "objective-c++ method declaration is expected");
24536       return error_mark_node;
24537     }
24538   return params;
24539 }
24540
24541 /* Parse the non-keyword Objective-C params.  */
24542
24543 static tree
24544 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
24545                                        tree* attributes)
24546 {
24547   tree params = make_node (TREE_LIST);
24548   cp_token *token = cp_lexer_peek_token (parser->lexer);
24549   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
24550
24551   while (token->type == CPP_COMMA)
24552     {
24553       cp_parameter_declarator *parmdecl;
24554       tree parm;
24555
24556       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24557       token = cp_lexer_peek_token (parser->lexer);
24558
24559       if (token->type == CPP_ELLIPSIS)
24560         {
24561           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
24562           *ellipsisp = true;
24563           token = cp_lexer_peek_token (parser->lexer);
24564           break;
24565         }
24566
24567       /* TODO: parse attributes for tail parameters.  */
24568       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24569       parm = grokdeclarator (parmdecl->declarator,
24570                              &parmdecl->decl_specifiers,
24571                              PARM, /*initialized=*/0,
24572                              /*attrlist=*/NULL);
24573
24574       chainon (params, build_tree_list (NULL_TREE, parm));
24575       token = cp_lexer_peek_token (parser->lexer);
24576     }
24577
24578   /* We allow tail attributes for the method.  */
24579   if (token->keyword == RID_ATTRIBUTE)
24580     {
24581       if (*attributes == NULL_TREE)
24582         {
24583           *attributes = cp_parser_attributes_opt (parser);
24584           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24585               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24586             return params;
24587         }
24588       else        
24589         /* We have an error, but parse the attributes, so that we can 
24590            carry on.  */
24591         *attributes = cp_parser_attributes_opt (parser);
24592
24593       cp_parser_error (parser, 
24594                        "method attributes must be specified at the end");
24595       return error_mark_node;
24596     }
24597
24598   return params;
24599 }
24600
24601 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
24602
24603 static void
24604 cp_parser_objc_interstitial_code (cp_parser* parser)
24605 {
24606   cp_token *token = cp_lexer_peek_token (parser->lexer);
24607
24608   /* If the next token is `extern' and the following token is a string
24609      literal, then we have a linkage specification.  */
24610   if (token->keyword == RID_EXTERN
24611       && cp_parser_is_pure_string_literal
24612          (cp_lexer_peek_nth_token (parser->lexer, 2)))
24613     cp_parser_linkage_specification (parser);
24614   /* Handle #pragma, if any.  */
24615   else if (token->type == CPP_PRAGMA)
24616     cp_parser_pragma (parser, pragma_external);
24617   /* Allow stray semicolons.  */
24618   else if (token->type == CPP_SEMICOLON)
24619     cp_lexer_consume_token (parser->lexer);
24620   /* Mark methods as optional or required, when building protocols.  */
24621   else if (token->keyword == RID_AT_OPTIONAL)
24622     {
24623       cp_lexer_consume_token (parser->lexer);
24624       objc_set_method_opt (true);
24625     }
24626   else if (token->keyword == RID_AT_REQUIRED)
24627     {
24628       cp_lexer_consume_token (parser->lexer);
24629       objc_set_method_opt (false);
24630     }
24631   else if (token->keyword == RID_NAMESPACE)
24632     cp_parser_namespace_definition (parser);
24633   /* Other stray characters must generate errors.  */
24634   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24635     {
24636       cp_lexer_consume_token (parser->lexer);
24637       error ("stray %qs between Objective-C++ methods",
24638              token->type == CPP_OPEN_BRACE ? "{" : "}");
24639     }
24640   /* Finally, try to parse a block-declaration, or a function-definition.  */
24641   else
24642     cp_parser_block_declaration (parser, /*statement_p=*/false);
24643 }
24644
24645 /* Parse a method signature.  */
24646
24647 static tree
24648 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24649 {
24650   tree rettype, kwdparms, optparms;
24651   bool ellipsis = false;
24652   bool is_class_method;
24653
24654   is_class_method = cp_parser_objc_method_type (parser);
24655   rettype = cp_parser_objc_typename (parser);
24656   *attributes = NULL_TREE;
24657   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24658   if (kwdparms == error_mark_node)
24659     return error_mark_node;
24660   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24661   if (optparms == error_mark_node)
24662     return error_mark_node;
24663
24664   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24665 }
24666
24667 static bool
24668 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24669 {
24670   tree tattr;  
24671   cp_lexer_save_tokens (parser->lexer);
24672   tattr = cp_parser_attributes_opt (parser);
24673   gcc_assert (tattr) ;
24674   
24675   /* If the attributes are followed by a method introducer, this is not allowed.
24676      Dump the attributes and flag the situation.  */
24677   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24678       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24679     return true;
24680
24681   /* Otherwise, the attributes introduce some interstitial code, possibly so
24682      rewind to allow that check.  */
24683   cp_lexer_rollback_tokens (parser->lexer);
24684   return false;  
24685 }
24686
24687 /* Parse an Objective-C method prototype list.  */
24688
24689 static void
24690 cp_parser_objc_method_prototype_list (cp_parser* parser)
24691 {
24692   cp_token *token = cp_lexer_peek_token (parser->lexer);
24693
24694   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24695     {
24696       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24697         {
24698           tree attributes, sig;
24699           bool is_class_method;
24700           if (token->type == CPP_PLUS)
24701             is_class_method = true;
24702           else
24703             is_class_method = false;
24704           sig = cp_parser_objc_method_signature (parser, &attributes);
24705           if (sig == error_mark_node)
24706             {
24707               cp_parser_skip_to_end_of_block_or_statement (parser);
24708               token = cp_lexer_peek_token (parser->lexer);
24709               continue;
24710             }
24711           objc_add_method_declaration (is_class_method, sig, attributes);
24712           cp_parser_consume_semicolon_at_end_of_statement (parser);
24713         }
24714       else if (token->keyword == RID_AT_PROPERTY)
24715         cp_parser_objc_at_property_declaration (parser);
24716       else if (token->keyword == RID_ATTRIBUTE 
24717                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24718         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
24719                     OPT_Wattributes, 
24720                     "prefix attributes are ignored for methods");
24721       else
24722         /* Allow for interspersed non-ObjC++ code.  */
24723         cp_parser_objc_interstitial_code (parser);
24724
24725       token = cp_lexer_peek_token (parser->lexer);
24726     }
24727
24728   if (token->type != CPP_EOF)
24729     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24730   else
24731     cp_parser_error (parser, "expected %<@end%>");
24732
24733   objc_finish_interface ();
24734 }
24735
24736 /* Parse an Objective-C method definition list.  */
24737
24738 static void
24739 cp_parser_objc_method_definition_list (cp_parser* parser)
24740 {
24741   cp_token *token = cp_lexer_peek_token (parser->lexer);
24742
24743   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24744     {
24745       tree meth;
24746
24747       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24748         {
24749           cp_token *ptk;
24750           tree sig, attribute;
24751           bool is_class_method;
24752           if (token->type == CPP_PLUS)
24753             is_class_method = true;
24754           else
24755             is_class_method = false;
24756           push_deferring_access_checks (dk_deferred);
24757           sig = cp_parser_objc_method_signature (parser, &attribute);
24758           if (sig == error_mark_node)
24759             {
24760               cp_parser_skip_to_end_of_block_or_statement (parser);
24761               token = cp_lexer_peek_token (parser->lexer);
24762               continue;
24763             }
24764           objc_start_method_definition (is_class_method, sig, attribute,
24765                                         NULL_TREE);
24766
24767           /* For historical reasons, we accept an optional semicolon.  */
24768           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24769             cp_lexer_consume_token (parser->lexer);
24770
24771           ptk = cp_lexer_peek_token (parser->lexer);
24772           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
24773                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24774             {
24775               perform_deferred_access_checks (tf_warning_or_error);
24776               stop_deferring_access_checks ();
24777               meth = cp_parser_function_definition_after_declarator (parser,
24778                                                                      false);
24779               pop_deferring_access_checks ();
24780               objc_finish_method_definition (meth);
24781             }
24782         }
24783       /* The following case will be removed once @synthesize is
24784          completely implemented.  */
24785       else if (token->keyword == RID_AT_PROPERTY)
24786         cp_parser_objc_at_property_declaration (parser);
24787       else if (token->keyword == RID_AT_SYNTHESIZE)
24788         cp_parser_objc_at_synthesize_declaration (parser);
24789       else if (token->keyword == RID_AT_DYNAMIC)
24790         cp_parser_objc_at_dynamic_declaration (parser);
24791       else if (token->keyword == RID_ATTRIBUTE 
24792                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24793         warning_at (token->location, OPT_Wattributes,
24794                     "prefix attributes are ignored for methods");
24795       else
24796         /* Allow for interspersed non-ObjC++ code.  */
24797         cp_parser_objc_interstitial_code (parser);
24798
24799       token = cp_lexer_peek_token (parser->lexer);
24800     }
24801
24802   if (token->type != CPP_EOF)
24803     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24804   else
24805     cp_parser_error (parser, "expected %<@end%>");
24806
24807   objc_finish_implementation ();
24808 }
24809
24810 /* Parse Objective-C ivars.  */
24811
24812 static void
24813 cp_parser_objc_class_ivars (cp_parser* parser)
24814 {
24815   cp_token *token = cp_lexer_peek_token (parser->lexer);
24816
24817   if (token->type != CPP_OPEN_BRACE)
24818     return;     /* No ivars specified.  */
24819
24820   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
24821   token = cp_lexer_peek_token (parser->lexer);
24822
24823   while (token->type != CPP_CLOSE_BRACE 
24824         && token->keyword != RID_AT_END && token->type != CPP_EOF)
24825     {
24826       cp_decl_specifier_seq declspecs;
24827       int decl_class_or_enum_p;
24828       tree prefix_attributes;
24829
24830       cp_parser_objc_visibility_spec (parser);
24831
24832       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24833         break;
24834
24835       cp_parser_decl_specifier_seq (parser,
24836                                     CP_PARSER_FLAGS_OPTIONAL,
24837                                     &declspecs,
24838                                     &decl_class_or_enum_p);
24839
24840       /* auto, register, static, extern, mutable.  */
24841       if (declspecs.storage_class != sc_none)
24842         {
24843           cp_parser_error (parser, "invalid type for instance variable");         
24844           declspecs.storage_class = sc_none;
24845         }
24846
24847       /* thread_local.  */
24848       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24849         {
24850           cp_parser_error (parser, "invalid type for instance variable");
24851           declspecs.locations[ds_thread] = 0;
24852         }
24853       
24854       /* typedef.  */
24855       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24856         {
24857           cp_parser_error (parser, "invalid type for instance variable");
24858           declspecs.locations[ds_typedef] = 0;
24859         }
24860
24861       prefix_attributes = declspecs.attributes;
24862       declspecs.attributes = NULL_TREE;
24863
24864       /* Keep going until we hit the `;' at the end of the
24865          declaration.  */
24866       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24867         {
24868           tree width = NULL_TREE, attributes, first_attribute, decl;
24869           cp_declarator *declarator = NULL;
24870           int ctor_dtor_or_conv_p;
24871
24872           /* Check for a (possibly unnamed) bitfield declaration.  */
24873           token = cp_lexer_peek_token (parser->lexer);
24874           if (token->type == CPP_COLON)
24875             goto eat_colon;
24876
24877           if (token->type == CPP_NAME
24878               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24879                   == CPP_COLON))
24880             {
24881               /* Get the name of the bitfield.  */
24882               declarator = make_id_declarator (NULL_TREE,
24883                                                cp_parser_identifier (parser),
24884                                                sfk_none);
24885
24886              eat_colon:
24887               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24888               /* Get the width of the bitfield.  */
24889               width
24890                 = cp_parser_constant_expression (parser,
24891                                                  /*allow_non_constant=*/false,
24892                                                  NULL);
24893             }
24894           else
24895             {
24896               /* Parse the declarator.  */
24897               declarator
24898                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24899                                         &ctor_dtor_or_conv_p,
24900                                         /*parenthesized_p=*/NULL,
24901                                         /*member_p=*/false);
24902             }
24903
24904           /* Look for attributes that apply to the ivar.  */
24905           attributes = cp_parser_attributes_opt (parser);
24906           /* Remember which attributes are prefix attributes and
24907              which are not.  */
24908           first_attribute = attributes;
24909           /* Combine the attributes.  */
24910           attributes = chainon (prefix_attributes, attributes);
24911
24912           if (width)
24913               /* Create the bitfield declaration.  */
24914               decl = grokbitfield (declarator, &declspecs,
24915                                    width,
24916                                    attributes);
24917           else
24918             decl = grokfield (declarator, &declspecs,
24919                               NULL_TREE, /*init_const_expr_p=*/false,
24920                               NULL_TREE, attributes);
24921
24922           /* Add the instance variable.  */
24923           if (decl != error_mark_node && decl != NULL_TREE)
24924             objc_add_instance_variable (decl);
24925
24926           /* Reset PREFIX_ATTRIBUTES.  */
24927           while (attributes && TREE_CHAIN (attributes) != first_attribute)
24928             attributes = TREE_CHAIN (attributes);
24929           if (attributes)
24930             TREE_CHAIN (attributes) = NULL_TREE;
24931
24932           token = cp_lexer_peek_token (parser->lexer);
24933
24934           if (token->type == CPP_COMMA)
24935             {
24936               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24937               continue;
24938             }
24939           break;
24940         }
24941
24942       cp_parser_consume_semicolon_at_end_of_statement (parser);
24943       token = cp_lexer_peek_token (parser->lexer);
24944     }
24945
24946   if (token->keyword == RID_AT_END)
24947     cp_parser_error (parser, "expected %<}%>");
24948
24949   /* Do not consume the RID_AT_END, so it will be read again as terminating
24950      the @interface of @implementation.  */ 
24951   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24952     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24953     
24954   /* For historical reasons, we accept an optional semicolon.  */
24955   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24956     cp_lexer_consume_token (parser->lexer);
24957 }
24958
24959 /* Parse an Objective-C protocol declaration.  */
24960
24961 static void
24962 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24963 {
24964   tree proto, protorefs;
24965   cp_token *tok;
24966
24967   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24968   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24969     {
24970       tok = cp_lexer_peek_token (parser->lexer);
24971       error_at (tok->location, "identifier expected after %<@protocol%>");
24972       cp_parser_consume_semicolon_at_end_of_statement (parser);
24973       return;
24974     }
24975
24976   /* See if we have a forward declaration or a definition.  */
24977   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24978
24979   /* Try a forward declaration first.  */
24980   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24981     {
24982       while (true)
24983         {
24984           tree id;
24985           
24986           id = cp_parser_identifier (parser);
24987           if (id == error_mark_node)
24988             break;
24989           
24990           objc_declare_protocol (id, attributes);
24991           
24992           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24993             cp_lexer_consume_token (parser->lexer);
24994           else
24995             break;
24996         }
24997       cp_parser_consume_semicolon_at_end_of_statement (parser);
24998     }
24999
25000   /* Ok, we got a full-fledged definition (or at least should).  */
25001   else
25002     {
25003       proto = cp_parser_identifier (parser);
25004       protorefs = cp_parser_objc_protocol_refs_opt (parser);
25005       objc_start_protocol (proto, protorefs, attributes);
25006       cp_parser_objc_method_prototype_list (parser);
25007     }
25008 }
25009
25010 /* Parse an Objective-C superclass or category.  */
25011
25012 static void
25013 cp_parser_objc_superclass_or_category (cp_parser *parser, 
25014                                        bool iface_p,
25015                                        tree *super,
25016                                        tree *categ, bool *is_class_extension)
25017 {
25018   cp_token *next = cp_lexer_peek_token (parser->lexer);
25019
25020   *super = *categ = NULL_TREE;
25021   *is_class_extension = false;
25022   if (next->type == CPP_COLON)
25023     {
25024       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
25025       *super = cp_parser_identifier (parser);
25026     }
25027   else if (next->type == CPP_OPEN_PAREN)
25028     {
25029       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
25030
25031       /* If there is no category name, and this is an @interface, we
25032          have a class extension.  */
25033       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25034         {
25035           *categ = NULL_TREE;
25036           *is_class_extension = true;
25037         }
25038       else
25039         *categ = cp_parser_identifier (parser);
25040
25041       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25042     }
25043 }
25044
25045 /* Parse an Objective-C class interface.  */
25046
25047 static void
25048 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25049 {
25050   tree name, super, categ, protos;
25051   bool is_class_extension;
25052
25053   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
25054   name = cp_parser_identifier (parser);
25055   if (name == error_mark_node)
25056     {
25057       /* It's hard to recover because even if valid @interface stuff
25058          is to follow, we can't compile it (or validate it) if we
25059          don't even know which class it refers to.  Let's assume this
25060          was a stray '@interface' token in the stream and skip it.
25061       */
25062       return;
25063     }
25064   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25065                                          &is_class_extension);
25066   protos = cp_parser_objc_protocol_refs_opt (parser);
25067
25068   /* We have either a class or a category on our hands.  */
25069   if (categ || is_class_extension)
25070     objc_start_category_interface (name, categ, protos, attributes);
25071   else
25072     {
25073       objc_start_class_interface (name, super, protos, attributes);
25074       /* Handle instance variable declarations, if any.  */
25075       cp_parser_objc_class_ivars (parser);
25076       objc_continue_interface ();
25077     }
25078
25079   cp_parser_objc_method_prototype_list (parser);
25080 }
25081
25082 /* Parse an Objective-C class implementation.  */
25083
25084 static void
25085 cp_parser_objc_class_implementation (cp_parser* parser)
25086 {
25087   tree name, super, categ;
25088   bool is_class_extension;
25089
25090   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
25091   name = cp_parser_identifier (parser);
25092   if (name == error_mark_node)
25093     {
25094       /* It's hard to recover because even if valid @implementation
25095          stuff is to follow, we can't compile it (or validate it) if
25096          we don't even know which class it refers to.  Let's assume
25097          this was a stray '@implementation' token in the stream and
25098          skip it.
25099       */
25100       return;
25101     }
25102   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25103                                          &is_class_extension);
25104
25105   /* We have either a class or a category on our hands.  */
25106   if (categ)
25107     objc_start_category_implementation (name, categ);
25108   else
25109     {
25110       objc_start_class_implementation (name, super);
25111       /* Handle instance variable declarations, if any.  */
25112       cp_parser_objc_class_ivars (parser);
25113       objc_continue_implementation ();
25114     }
25115
25116   cp_parser_objc_method_definition_list (parser);
25117 }
25118
25119 /* Consume the @end token and finish off the implementation.  */
25120
25121 static void
25122 cp_parser_objc_end_implementation (cp_parser* parser)
25123 {
25124   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
25125   objc_finish_implementation ();
25126 }
25127
25128 /* Parse an Objective-C declaration.  */
25129
25130 static void
25131 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25132 {
25133   /* Try to figure out what kind of declaration is present.  */
25134   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25135
25136   if (attributes)
25137     switch (kwd->keyword)
25138       {
25139         case RID_AT_ALIAS:
25140         case RID_AT_CLASS:
25141         case RID_AT_END:
25142           error_at (kwd->location, "attributes may not be specified before"
25143                     " the %<@%D%> Objective-C++ keyword",
25144                     kwd->u.value);
25145           attributes = NULL;
25146           break;
25147         case RID_AT_IMPLEMENTATION:
25148           warning_at (kwd->location, OPT_Wattributes,
25149                       "prefix attributes are ignored before %<@%D%>",
25150                       kwd->u.value);
25151           attributes = NULL;
25152         default:
25153           break;
25154       }
25155
25156   switch (kwd->keyword)
25157     {
25158     case RID_AT_ALIAS:
25159       cp_parser_objc_alias_declaration (parser);
25160       break;
25161     case RID_AT_CLASS:
25162       cp_parser_objc_class_declaration (parser);
25163       break;
25164     case RID_AT_PROTOCOL:
25165       cp_parser_objc_protocol_declaration (parser, attributes);
25166       break;
25167     case RID_AT_INTERFACE:
25168       cp_parser_objc_class_interface (parser, attributes);
25169       break;
25170     case RID_AT_IMPLEMENTATION:
25171       cp_parser_objc_class_implementation (parser);
25172       break;
25173     case RID_AT_END:
25174       cp_parser_objc_end_implementation (parser);
25175       break;
25176     default:
25177       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25178                 kwd->u.value);
25179       cp_parser_skip_to_end_of_block_or_statement (parser);
25180     }
25181 }
25182
25183 /* Parse an Objective-C try-catch-finally statement.
25184
25185    objc-try-catch-finally-stmt:
25186      @try compound-statement objc-catch-clause-seq [opt]
25187        objc-finally-clause [opt]
25188
25189    objc-catch-clause-seq:
25190      objc-catch-clause objc-catch-clause-seq [opt]
25191
25192    objc-catch-clause:
25193      @catch ( objc-exception-declaration ) compound-statement
25194
25195    objc-finally-clause:
25196      @finally compound-statement
25197
25198    objc-exception-declaration:
25199      parameter-declaration
25200      '...'
25201
25202    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25203
25204    Returns NULL_TREE.
25205
25206    PS: This function is identical to c_parser_objc_try_catch_finally_statement
25207    for C.  Keep them in sync.  */   
25208
25209 static tree
25210 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25211 {
25212   location_t location;
25213   tree stmt;
25214
25215   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25216   location = cp_lexer_peek_token (parser->lexer)->location;
25217   objc_maybe_warn_exceptions (location);
25218   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25219      node, lest it get absorbed into the surrounding block.  */
25220   stmt = push_stmt_list ();
25221   cp_parser_compound_statement (parser, NULL, false, false);
25222   objc_begin_try_stmt (location, pop_stmt_list (stmt));
25223
25224   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25225     {
25226       cp_parameter_declarator *parm;
25227       tree parameter_declaration = error_mark_node;
25228       bool seen_open_paren = false;
25229
25230       cp_lexer_consume_token (parser->lexer);
25231       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25232         seen_open_paren = true;
25233       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25234         {
25235           /* We have "@catch (...)" (where the '...' are literally
25236              what is in the code).  Skip the '...'.
25237              parameter_declaration is set to NULL_TREE, and
25238              objc_being_catch_clauses() knows that that means
25239              '...'.  */
25240           cp_lexer_consume_token (parser->lexer);
25241           parameter_declaration = NULL_TREE;
25242         }
25243       else
25244         {
25245           /* We have "@catch (NSException *exception)" or something
25246              like that.  Parse the parameter declaration.  */
25247           parm = cp_parser_parameter_declaration (parser, false, NULL);
25248           if (parm == NULL)
25249             parameter_declaration = error_mark_node;
25250           else
25251             parameter_declaration = grokdeclarator (parm->declarator,
25252                                                     &parm->decl_specifiers,
25253                                                     PARM, /*initialized=*/0,
25254                                                     /*attrlist=*/NULL);
25255         }
25256       if (seen_open_paren)
25257         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25258       else
25259         {
25260           /* If there was no open parenthesis, we are recovering from
25261              an error, and we are trying to figure out what mistake
25262              the user has made.  */
25263
25264           /* If there is an immediate closing parenthesis, the user
25265              probably forgot the opening one (ie, they typed "@catch
25266              NSException *e)".  Parse the closing parenthesis and keep
25267              going.  */
25268           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25269             cp_lexer_consume_token (parser->lexer);
25270           
25271           /* If these is no immediate closing parenthesis, the user
25272              probably doesn't know that parenthesis are required at
25273              all (ie, they typed "@catch NSException *e").  So, just
25274              forget about the closing parenthesis and keep going.  */
25275         }
25276       objc_begin_catch_clause (parameter_declaration);
25277       cp_parser_compound_statement (parser, NULL, false, false);
25278       objc_finish_catch_clause ();
25279     }
25280   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25281     {
25282       cp_lexer_consume_token (parser->lexer);
25283       location = cp_lexer_peek_token (parser->lexer)->location;
25284       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25285          node, lest it get absorbed into the surrounding block.  */
25286       stmt = push_stmt_list ();
25287       cp_parser_compound_statement (parser, NULL, false, false);
25288       objc_build_finally_clause (location, pop_stmt_list (stmt));
25289     }
25290
25291   return objc_finish_try_stmt ();
25292 }
25293
25294 /* Parse an Objective-C synchronized statement.
25295
25296    objc-synchronized-stmt:
25297      @synchronized ( expression ) compound-statement
25298
25299    Returns NULL_TREE.  */
25300
25301 static tree
25302 cp_parser_objc_synchronized_statement (cp_parser *parser)
25303 {
25304   location_t location;
25305   tree lock, stmt;
25306
25307   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25308
25309   location = cp_lexer_peek_token (parser->lexer)->location;
25310   objc_maybe_warn_exceptions (location);
25311   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25312   lock = cp_parser_expression (parser, false, NULL);
25313   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25314
25315   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25316      node, lest it get absorbed into the surrounding block.  */
25317   stmt = push_stmt_list ();
25318   cp_parser_compound_statement (parser, NULL, false, false);
25319
25320   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25321 }
25322
25323 /* Parse an Objective-C throw statement.
25324
25325    objc-throw-stmt:
25326      @throw assignment-expression [opt] ;
25327
25328    Returns a constructed '@throw' statement.  */
25329
25330 static tree
25331 cp_parser_objc_throw_statement (cp_parser *parser)
25332 {
25333   tree expr = NULL_TREE;
25334   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25335
25336   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25337
25338   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25339     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25340
25341   cp_parser_consume_semicolon_at_end_of_statement (parser);
25342
25343   return objc_build_throw_stmt (loc, expr);
25344 }
25345
25346 /* Parse an Objective-C statement.  */
25347
25348 static tree
25349 cp_parser_objc_statement (cp_parser * parser)
25350 {
25351   /* Try to figure out what kind of declaration is present.  */
25352   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25353
25354   switch (kwd->keyword)
25355     {
25356     case RID_AT_TRY:
25357       return cp_parser_objc_try_catch_finally_statement (parser);
25358     case RID_AT_SYNCHRONIZED:
25359       return cp_parser_objc_synchronized_statement (parser);
25360     case RID_AT_THROW:
25361       return cp_parser_objc_throw_statement (parser);
25362     default:
25363       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25364                kwd->u.value);
25365       cp_parser_skip_to_end_of_block_or_statement (parser);
25366     }
25367
25368   return error_mark_node;
25369 }
25370
25371 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
25372    look ahead to see if an objc keyword follows the attributes.  This
25373    is to detect the use of prefix attributes on ObjC @interface and 
25374    @protocol.  */
25375
25376 static bool
25377 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25378 {
25379   cp_lexer_save_tokens (parser->lexer);
25380   *attrib = cp_parser_attributes_opt (parser);
25381   gcc_assert (*attrib);
25382   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25383     {
25384       cp_lexer_commit_tokens (parser->lexer);
25385       return true;
25386     }
25387   cp_lexer_rollback_tokens (parser->lexer);
25388   return false;  
25389 }
25390
25391 /* This routine is a minimal replacement for
25392    c_parser_struct_declaration () used when parsing the list of
25393    types/names or ObjC++ properties.  For example, when parsing the
25394    code
25395
25396    @property (readonly) int a, b, c;
25397
25398    this function is responsible for parsing "int a, int b, int c" and
25399    returning the declarations as CHAIN of DECLs.
25400
25401    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
25402    similar parsing.  */
25403 static tree
25404 cp_parser_objc_struct_declaration (cp_parser *parser)
25405 {
25406   tree decls = NULL_TREE;
25407   cp_decl_specifier_seq declspecs;
25408   int decl_class_or_enum_p;
25409   tree prefix_attributes;
25410
25411   cp_parser_decl_specifier_seq (parser,
25412                                 CP_PARSER_FLAGS_NONE,
25413                                 &declspecs,
25414                                 &decl_class_or_enum_p);
25415
25416   if (declspecs.type == error_mark_node)
25417     return error_mark_node;
25418
25419   /* auto, register, static, extern, mutable.  */
25420   if (declspecs.storage_class != sc_none)
25421     {
25422       cp_parser_error (parser, "invalid type for property");
25423       declspecs.storage_class = sc_none;
25424     }
25425   
25426   /* thread_local.  */
25427   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25428     {
25429       cp_parser_error (parser, "invalid type for property");
25430       declspecs.locations[ds_thread] = 0;
25431     }
25432   
25433   /* typedef.  */
25434   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25435     {
25436       cp_parser_error (parser, "invalid type for property");
25437       declspecs.locations[ds_typedef] = 0;
25438     }
25439
25440   prefix_attributes = declspecs.attributes;
25441   declspecs.attributes = NULL_TREE;
25442
25443   /* Keep going until we hit the `;' at the end of the declaration. */
25444   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25445     {
25446       tree attributes, first_attribute, decl;
25447       cp_declarator *declarator;
25448       cp_token *token;
25449
25450       /* Parse the declarator.  */
25451       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25452                                          NULL, NULL, false);
25453
25454       /* Look for attributes that apply to the ivar.  */
25455       attributes = cp_parser_attributes_opt (parser);
25456       /* Remember which attributes are prefix attributes and
25457          which are not.  */
25458       first_attribute = attributes;
25459       /* Combine the attributes.  */
25460       attributes = chainon (prefix_attributes, attributes);
25461       
25462       decl = grokfield (declarator, &declspecs,
25463                         NULL_TREE, /*init_const_expr_p=*/false,
25464                         NULL_TREE, attributes);
25465
25466       if (decl == error_mark_node || decl == NULL_TREE)
25467         return error_mark_node;
25468       
25469       /* Reset PREFIX_ATTRIBUTES.  */
25470       while (attributes && TREE_CHAIN (attributes) != first_attribute)
25471         attributes = TREE_CHAIN (attributes);
25472       if (attributes)
25473         TREE_CHAIN (attributes) = NULL_TREE;
25474
25475       DECL_CHAIN (decl) = decls;
25476       decls = decl;
25477
25478       token = cp_lexer_peek_token (parser->lexer);
25479       if (token->type == CPP_COMMA)
25480         {
25481           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
25482           continue;
25483         }
25484       else
25485         break;
25486     }
25487   return decls;
25488 }
25489
25490 /* Parse an Objective-C @property declaration.  The syntax is:
25491
25492    objc-property-declaration:
25493      '@property' objc-property-attributes[opt] struct-declaration ;
25494
25495    objc-property-attributes:
25496     '(' objc-property-attribute-list ')'
25497
25498    objc-property-attribute-list:
25499      objc-property-attribute
25500      objc-property-attribute-list, objc-property-attribute
25501
25502    objc-property-attribute
25503      'getter' = identifier
25504      'setter' = identifier
25505      'readonly'
25506      'readwrite'
25507      'assign'
25508      'retain'
25509      'copy'
25510      'nonatomic'
25511
25512   For example:
25513     @property NSString *name;
25514     @property (readonly) id object;
25515     @property (retain, nonatomic, getter=getTheName) id name;
25516     @property int a, b, c;
25517
25518    PS: This function is identical to
25519    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
25520 static void 
25521 cp_parser_objc_at_property_declaration (cp_parser *parser)
25522 {
25523   /* The following variables hold the attributes of the properties as
25524      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
25525      seen.  When we see an attribute, we set them to 'true' (if they
25526      are boolean properties) or to the identifier (if they have an
25527      argument, ie, for getter and setter).  Note that here we only
25528      parse the list of attributes, check the syntax and accumulate the
25529      attributes that we find.  objc_add_property_declaration() will
25530      then process the information.  */
25531   bool property_assign = false;
25532   bool property_copy = false;
25533   tree property_getter_ident = NULL_TREE;
25534   bool property_nonatomic = false;
25535   bool property_readonly = false;
25536   bool property_readwrite = false;
25537   bool property_retain = false;
25538   tree property_setter_ident = NULL_TREE;
25539
25540   /* 'properties' is the list of properties that we read.  Usually a
25541      single one, but maybe more (eg, in "@property int a, b, c;" there
25542      are three).  */
25543   tree properties;
25544   location_t loc;
25545
25546   loc = cp_lexer_peek_token (parser->lexer)->location;
25547
25548   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
25549
25550   /* Parse the optional attribute list...  */
25551   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25552     {
25553       /* Eat the '('.  */
25554       cp_lexer_consume_token (parser->lexer);
25555
25556       while (true)
25557         {
25558           bool syntax_error = false;
25559           cp_token *token = cp_lexer_peek_token (parser->lexer);
25560           enum rid keyword;
25561
25562           if (token->type != CPP_NAME)
25563             {
25564               cp_parser_error (parser, "expected identifier");
25565               break;
25566             }
25567           keyword = C_RID_CODE (token->u.value);
25568           cp_lexer_consume_token (parser->lexer);
25569           switch (keyword)
25570             {
25571             case RID_ASSIGN:    property_assign = true;    break;
25572             case RID_COPY:      property_copy = true;      break;
25573             case RID_NONATOMIC: property_nonatomic = true; break;
25574             case RID_READONLY:  property_readonly = true;  break;
25575             case RID_READWRITE: property_readwrite = true; break;
25576             case RID_RETAIN:    property_retain = true;    break;
25577
25578             case RID_GETTER:
25579             case RID_SETTER:
25580               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25581                 {
25582                   if (keyword == RID_GETTER)
25583                     cp_parser_error (parser,
25584                                      "missing %<=%> (after %<getter%> attribute)");
25585                   else
25586                     cp_parser_error (parser,
25587                                      "missing %<=%> (after %<setter%> attribute)");
25588                   syntax_error = true;
25589                   break;
25590                 }
25591               cp_lexer_consume_token (parser->lexer); /* eat the = */
25592               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25593                 {
25594                   cp_parser_error (parser, "expected identifier");
25595                   syntax_error = true;
25596                   break;
25597                 }
25598               if (keyword == RID_SETTER)
25599                 {
25600                   if (property_setter_ident != NULL_TREE)
25601                     {
25602                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25603                       cp_lexer_consume_token (parser->lexer);
25604                     }
25605                   else
25606                     property_setter_ident = cp_parser_objc_selector (parser);
25607                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25608                     cp_parser_error (parser, "setter name must terminate with %<:%>");
25609                   else
25610                     cp_lexer_consume_token (parser->lexer);
25611                 }
25612               else
25613                 {
25614                   if (property_getter_ident != NULL_TREE)
25615                     {
25616                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25617                       cp_lexer_consume_token (parser->lexer);
25618                     }
25619                   else
25620                     property_getter_ident = cp_parser_objc_selector (parser);
25621                 }
25622               break;
25623             default:
25624               cp_parser_error (parser, "unknown property attribute");
25625               syntax_error = true;
25626               break;
25627             }
25628
25629           if (syntax_error)
25630             break;
25631
25632           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25633             cp_lexer_consume_token (parser->lexer);
25634           else
25635             break;
25636         }
25637
25638       /* FIXME: "@property (setter, assign);" will generate a spurious
25639          "error: expected â€˜)’ before â€˜,’ token".  This is because
25640          cp_parser_require, unlike the C counterpart, will produce an
25641          error even if we are in error recovery.  */
25642       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25643         {
25644           cp_parser_skip_to_closing_parenthesis (parser,
25645                                                  /*recovering=*/true,
25646                                                  /*or_comma=*/false,
25647                                                  /*consume_paren=*/true);
25648         }
25649     }
25650
25651   /* ... and the property declaration(s).  */
25652   properties = cp_parser_objc_struct_declaration (parser);
25653
25654   if (properties == error_mark_node)
25655     {
25656       cp_parser_skip_to_end_of_statement (parser);
25657       /* If the next token is now a `;', consume it.  */
25658       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25659         cp_lexer_consume_token (parser->lexer);
25660       return;
25661     }
25662
25663   if (properties == NULL_TREE)
25664     cp_parser_error (parser, "expected identifier");
25665   else
25666     {
25667       /* Comma-separated properties are chained together in
25668          reverse order; add them one by one.  */
25669       properties = nreverse (properties);
25670       
25671       for (; properties; properties = TREE_CHAIN (properties))
25672         objc_add_property_declaration (loc, copy_node (properties),
25673                                        property_readonly, property_readwrite,
25674                                        property_assign, property_retain,
25675                                        property_copy, property_nonatomic,
25676                                        property_getter_ident, property_setter_ident);
25677     }
25678   
25679   cp_parser_consume_semicolon_at_end_of_statement (parser);
25680 }
25681
25682 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
25683
25684    objc-synthesize-declaration:
25685      @synthesize objc-synthesize-identifier-list ;
25686
25687    objc-synthesize-identifier-list:
25688      objc-synthesize-identifier
25689      objc-synthesize-identifier-list, objc-synthesize-identifier
25690
25691    objc-synthesize-identifier
25692      identifier
25693      identifier = identifier
25694
25695   For example:
25696     @synthesize MyProperty;
25697     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25698
25699   PS: This function is identical to c_parser_objc_at_synthesize_declaration
25700   for C.  Keep them in sync.
25701 */
25702 static void 
25703 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25704 {
25705   tree list = NULL_TREE;
25706   location_t loc;
25707   loc = cp_lexer_peek_token (parser->lexer)->location;
25708
25709   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
25710   while (true)
25711     {
25712       tree property, ivar;
25713       property = cp_parser_identifier (parser);
25714       if (property == error_mark_node)
25715         {
25716           cp_parser_consume_semicolon_at_end_of_statement (parser);
25717           return;
25718         }
25719       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25720         {
25721           cp_lexer_consume_token (parser->lexer);
25722           ivar = cp_parser_identifier (parser);
25723           if (ivar == error_mark_node)
25724             {
25725               cp_parser_consume_semicolon_at_end_of_statement (parser);
25726               return;
25727             }
25728         }
25729       else
25730         ivar = NULL_TREE;
25731       list = chainon (list, build_tree_list (ivar, property));
25732       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25733         cp_lexer_consume_token (parser->lexer);
25734       else
25735         break;
25736     }
25737   cp_parser_consume_semicolon_at_end_of_statement (parser);
25738   objc_add_synthesize_declaration (loc, list);
25739 }
25740
25741 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
25742
25743    objc-dynamic-declaration:
25744      @dynamic identifier-list ;
25745
25746    For example:
25747      @dynamic MyProperty;
25748      @dynamic MyProperty, AnotherProperty;
25749
25750   PS: This function is identical to c_parser_objc_at_dynamic_declaration
25751   for C.  Keep them in sync.
25752 */
25753 static void 
25754 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25755 {
25756   tree list = NULL_TREE;
25757   location_t loc;
25758   loc = cp_lexer_peek_token (parser->lexer)->location;
25759
25760   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
25761   while (true)
25762     {
25763       tree property;
25764       property = cp_parser_identifier (parser);
25765       if (property == error_mark_node)
25766         {
25767           cp_parser_consume_semicolon_at_end_of_statement (parser);
25768           return;
25769         }
25770       list = chainon (list, build_tree_list (NULL, property));
25771       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25772         cp_lexer_consume_token (parser->lexer);
25773       else
25774         break;
25775     }
25776   cp_parser_consume_semicolon_at_end_of_statement (parser);
25777   objc_add_dynamic_declaration (loc, list);
25778 }
25779
25780 \f
25781 /* OpenMP 2.5 parsing routines.  */
25782
25783 /* Returns name of the next clause.
25784    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25785    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
25786    returned and the token is consumed.  */
25787
25788 static pragma_omp_clause
25789 cp_parser_omp_clause_name (cp_parser *parser)
25790 {
25791   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25792
25793   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25794     result = PRAGMA_OMP_CLAUSE_IF;
25795   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25796     result = PRAGMA_OMP_CLAUSE_DEFAULT;
25797   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25798     result = PRAGMA_OMP_CLAUSE_PRIVATE;
25799   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25800     {
25801       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25802       const char *p = IDENTIFIER_POINTER (id);
25803
25804       switch (p[0])
25805         {
25806         case 'c':
25807           if (!strcmp ("collapse", p))
25808             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25809           else if (!strcmp ("copyin", p))
25810             result = PRAGMA_OMP_CLAUSE_COPYIN;
25811           else if (!strcmp ("copyprivate", p))
25812             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25813           break;
25814         case 'f':
25815           if (!strcmp ("final", p))
25816             result = PRAGMA_OMP_CLAUSE_FINAL;
25817           else if (!strcmp ("firstprivate", p))
25818             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25819           break;
25820         case 'l':
25821           if (!strcmp ("lastprivate", p))
25822             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25823           break;
25824         case 'm':
25825           if (!strcmp ("mergeable", p))
25826             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25827           break;
25828         case 'n':
25829           if (!strcmp ("nowait", p))
25830             result = PRAGMA_OMP_CLAUSE_NOWAIT;
25831           else if (!strcmp ("num_threads", p))
25832             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25833           break;
25834         case 'o':
25835           if (!strcmp ("ordered", p))
25836             result = PRAGMA_OMP_CLAUSE_ORDERED;
25837           break;
25838         case 'r':
25839           if (!strcmp ("reduction", p))
25840             result = PRAGMA_OMP_CLAUSE_REDUCTION;
25841           break;
25842         case 's':
25843           if (!strcmp ("schedule", p))
25844             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25845           else if (!strcmp ("shared", p))
25846             result = PRAGMA_OMP_CLAUSE_SHARED;
25847           break;
25848         case 'u':
25849           if (!strcmp ("untied", p))
25850             result = PRAGMA_OMP_CLAUSE_UNTIED;
25851           break;
25852         }
25853     }
25854
25855   if (result != PRAGMA_OMP_CLAUSE_NONE)
25856     cp_lexer_consume_token (parser->lexer);
25857
25858   return result;
25859 }
25860
25861 /* Validate that a clause of the given type does not already exist.  */
25862
25863 static void
25864 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25865                            const char *name, location_t location)
25866 {
25867   tree c;
25868
25869   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25870     if (OMP_CLAUSE_CODE (c) == code)
25871       {
25872         error_at (location, "too many %qs clauses", name);
25873         break;
25874       }
25875 }
25876
25877 /* OpenMP 2.5:
25878    variable-list:
25879      identifier
25880      variable-list , identifier
25881
25882    In addition, we match a closing parenthesis.  An opening parenthesis
25883    will have been consumed by the caller.
25884
25885    If KIND is nonzero, create the appropriate node and install the decl
25886    in OMP_CLAUSE_DECL and add the node to the head of the list.
25887
25888    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25889    return the list created.  */
25890
25891 static tree
25892 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25893                                 tree list)
25894 {
25895   cp_token *token;
25896   while (1)
25897     {
25898       tree name, decl;
25899
25900       token = cp_lexer_peek_token (parser->lexer);
25901       name = cp_parser_id_expression (parser, /*template_p=*/false,
25902                                       /*check_dependency_p=*/true,
25903                                       /*template_p=*/NULL,
25904                                       /*declarator_p=*/false,
25905                                       /*optional_p=*/false);
25906       if (name == error_mark_node)
25907         goto skip_comma;
25908
25909       decl = cp_parser_lookup_name_simple (parser, name, token->location);
25910       if (decl == error_mark_node)
25911         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25912                                      token->location);
25913       else if (kind != 0)
25914         {
25915           tree u = build_omp_clause (token->location, kind);
25916           OMP_CLAUSE_DECL (u) = decl;
25917           OMP_CLAUSE_CHAIN (u) = list;
25918           list = u;
25919         }
25920       else
25921         list = tree_cons (decl, NULL_TREE, list);
25922
25923     get_comma:
25924       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25925         break;
25926       cp_lexer_consume_token (parser->lexer);
25927     }
25928
25929   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25930     {
25931       int ending;
25932
25933       /* Try to resync to an unnested comma.  Copied from
25934          cp_parser_parenthesized_expression_list.  */
25935     skip_comma:
25936       ending = cp_parser_skip_to_closing_parenthesis (parser,
25937                                                       /*recovering=*/true,
25938                                                       /*or_comma=*/true,
25939                                                       /*consume_paren=*/true);
25940       if (ending < 0)
25941         goto get_comma;
25942     }
25943
25944   return list;
25945 }
25946
25947 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25948    common case for omp clauses.  */
25949
25950 static tree
25951 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25952 {
25953   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25954     return cp_parser_omp_var_list_no_open (parser, kind, list);
25955   return list;
25956 }
25957
25958 /* OpenMP 3.0:
25959    collapse ( constant-expression ) */
25960
25961 static tree
25962 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25963 {
25964   tree c, num;
25965   location_t loc;
25966   HOST_WIDE_INT n;
25967
25968   loc = cp_lexer_peek_token (parser->lexer)->location;
25969   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25970     return list;
25971
25972   num = cp_parser_constant_expression (parser, false, NULL);
25973
25974   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25975     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25976                                            /*or_comma=*/false,
25977                                            /*consume_paren=*/true);
25978
25979   if (num == error_mark_node)
25980     return list;
25981   num = fold_non_dependent_expr (num);
25982   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25983       || !host_integerp (num, 0)
25984       || (n = tree_low_cst (num, 0)) <= 0
25985       || (int) n != n)
25986     {
25987       error_at (loc, "collapse argument needs positive constant integer expression");
25988       return list;
25989     }
25990
25991   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25992   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25993   OMP_CLAUSE_CHAIN (c) = list;
25994   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25995
25996   return c;
25997 }
25998
25999 /* OpenMP 2.5:
26000    default ( shared | none ) */
26001
26002 static tree
26003 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26004 {
26005   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26006   tree c;
26007
26008   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26009     return list;
26010   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26011     {
26012       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26013       const char *p = IDENTIFIER_POINTER (id);
26014
26015       switch (p[0])
26016         {
26017         case 'n':
26018           if (strcmp ("none", p) != 0)
26019             goto invalid_kind;
26020           kind = OMP_CLAUSE_DEFAULT_NONE;
26021           break;
26022
26023         case 's':
26024           if (strcmp ("shared", p) != 0)
26025             goto invalid_kind;
26026           kind = OMP_CLAUSE_DEFAULT_SHARED;
26027           break;
26028
26029         default:
26030           goto invalid_kind;
26031         }
26032
26033       cp_lexer_consume_token (parser->lexer);
26034     }
26035   else
26036     {
26037     invalid_kind:
26038       cp_parser_error (parser, "expected %<none%> or %<shared%>");
26039     }
26040
26041   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26042     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26043                                            /*or_comma=*/false,
26044                                            /*consume_paren=*/true);
26045
26046   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26047     return list;
26048
26049   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26050   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26051   OMP_CLAUSE_CHAIN (c) = list;
26052   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26053
26054   return c;
26055 }
26056
26057 /* OpenMP 3.1:
26058    final ( expression ) */
26059
26060 static tree
26061 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26062 {
26063   tree t, c;
26064
26065   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26066     return list;
26067
26068   t = cp_parser_condition (parser);
26069
26070   if (t == error_mark_node
26071       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26072     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26073                                            /*or_comma=*/false,
26074                                            /*consume_paren=*/true);
26075
26076   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26077
26078   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26079   OMP_CLAUSE_FINAL_EXPR (c) = t;
26080   OMP_CLAUSE_CHAIN (c) = list;
26081
26082   return c;
26083 }
26084
26085 /* OpenMP 2.5:
26086    if ( expression ) */
26087
26088 static tree
26089 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26090 {
26091   tree t, c;
26092
26093   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26094     return list;
26095
26096   t = cp_parser_condition (parser);
26097
26098   if (t == error_mark_node
26099       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26100     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26101                                            /*or_comma=*/false,
26102                                            /*consume_paren=*/true);
26103
26104   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26105
26106   c = build_omp_clause (location, OMP_CLAUSE_IF);
26107   OMP_CLAUSE_IF_EXPR (c) = t;
26108   OMP_CLAUSE_CHAIN (c) = list;
26109
26110   return c;
26111 }
26112
26113 /* OpenMP 3.1:
26114    mergeable */
26115
26116 static tree
26117 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26118                                 tree list, location_t location)
26119 {
26120   tree c;
26121
26122   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26123                              location);
26124
26125   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26126   OMP_CLAUSE_CHAIN (c) = list;
26127   return c;
26128 }
26129
26130 /* OpenMP 2.5:
26131    nowait */
26132
26133 static tree
26134 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26135                              tree list, location_t location)
26136 {
26137   tree c;
26138
26139   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26140
26141   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26142   OMP_CLAUSE_CHAIN (c) = list;
26143   return c;
26144 }
26145
26146 /* OpenMP 2.5:
26147    num_threads ( expression ) */
26148
26149 static tree
26150 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26151                                   location_t location)
26152 {
26153   tree t, c;
26154
26155   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26156     return list;
26157
26158   t = cp_parser_expression (parser, false, NULL);
26159
26160   if (t == error_mark_node
26161       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26162     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26163                                            /*or_comma=*/false,
26164                                            /*consume_paren=*/true);
26165
26166   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26167                              "num_threads", location);
26168
26169   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
26170   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
26171   OMP_CLAUSE_CHAIN (c) = list;
26172
26173   return c;
26174 }
26175
26176 /* OpenMP 2.5:
26177    ordered */
26178
26179 static tree
26180 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26181                               tree list, location_t location)
26182 {
26183   tree c;
26184
26185   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26186                              "ordered", location);
26187
26188   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26189   OMP_CLAUSE_CHAIN (c) = list;
26190   return c;
26191 }
26192
26193 /* OpenMP 2.5:
26194    reduction ( reduction-operator : variable-list )
26195
26196    reduction-operator:
26197      One of: + * - & ^ | && ||
26198
26199    OpenMP 3.1:
26200
26201    reduction-operator:
26202      One of: + * - & ^ | && || min max  */
26203
26204 static tree
26205 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26206 {
26207   enum tree_code code;
26208   tree nlist, c;
26209
26210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26211     return list;
26212
26213   switch (cp_lexer_peek_token (parser->lexer)->type)
26214     {
26215     case CPP_PLUS:
26216       code = PLUS_EXPR;
26217       break;
26218     case CPP_MULT:
26219       code = MULT_EXPR;
26220       break;
26221     case CPP_MINUS:
26222       code = MINUS_EXPR;
26223       break;
26224     case CPP_AND:
26225       code = BIT_AND_EXPR;
26226       break;
26227     case CPP_XOR:
26228       code = BIT_XOR_EXPR;
26229       break;
26230     case CPP_OR:
26231       code = BIT_IOR_EXPR;
26232       break;
26233     case CPP_AND_AND:
26234       code = TRUTH_ANDIF_EXPR;
26235       break;
26236     case CPP_OR_OR:
26237       code = TRUTH_ORIF_EXPR;
26238       break;
26239     case CPP_NAME:
26240       {
26241         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26242         const char *p = IDENTIFIER_POINTER (id);
26243
26244         if (strcmp (p, "min") == 0)
26245           {
26246             code = MIN_EXPR;
26247             break;
26248           }
26249         if (strcmp (p, "max") == 0)
26250           {
26251             code = MAX_EXPR;
26252             break;
26253           }
26254       }
26255       /* FALLTHROUGH */
26256     default:
26257       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26258                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26259     resync_fail:
26260       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26261                                              /*or_comma=*/false,
26262                                              /*consume_paren=*/true);
26263       return list;
26264     }
26265   cp_lexer_consume_token (parser->lexer);
26266
26267   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26268     goto resync_fail;
26269
26270   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26271   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26272     OMP_CLAUSE_REDUCTION_CODE (c) = code;
26273
26274   return nlist;
26275 }
26276
26277 /* OpenMP 2.5:
26278    schedule ( schedule-kind )
26279    schedule ( schedule-kind , expression )
26280
26281    schedule-kind:
26282      static | dynamic | guided | runtime | auto  */
26283
26284 static tree
26285 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26286 {
26287   tree c, t;
26288
26289   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26290     return list;
26291
26292   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26293
26294   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26295     {
26296       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26297       const char *p = IDENTIFIER_POINTER (id);
26298
26299       switch (p[0])
26300         {
26301         case 'd':
26302           if (strcmp ("dynamic", p) != 0)
26303             goto invalid_kind;
26304           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26305           break;
26306
26307         case 'g':
26308           if (strcmp ("guided", p) != 0)
26309             goto invalid_kind;
26310           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26311           break;
26312
26313         case 'r':
26314           if (strcmp ("runtime", p) != 0)
26315             goto invalid_kind;
26316           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26317           break;
26318
26319         default:
26320           goto invalid_kind;
26321         }
26322     }
26323   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26324     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26325   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26326     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26327   else
26328     goto invalid_kind;
26329   cp_lexer_consume_token (parser->lexer);
26330
26331   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26332     {
26333       cp_token *token;
26334       cp_lexer_consume_token (parser->lexer);
26335
26336       token = cp_lexer_peek_token (parser->lexer);
26337       t = cp_parser_assignment_expression (parser, false, NULL);
26338
26339       if (t == error_mark_node)
26340         goto resync_fail;
26341       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26342         error_at (token->location, "schedule %<runtime%> does not take "
26343                   "a %<chunk_size%> parameter");
26344       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26345         error_at (token->location, "schedule %<auto%> does not take "
26346                   "a %<chunk_size%> parameter");
26347       else
26348         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26349
26350       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26351         goto resync_fail;
26352     }
26353   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26354     goto resync_fail;
26355
26356   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26357   OMP_CLAUSE_CHAIN (c) = list;
26358   return c;
26359
26360  invalid_kind:
26361   cp_parser_error (parser, "invalid schedule kind");
26362  resync_fail:
26363   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26364                                          /*or_comma=*/false,
26365                                          /*consume_paren=*/true);
26366   return list;
26367 }
26368
26369 /* OpenMP 3.0:
26370    untied */
26371
26372 static tree
26373 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26374                              tree list, location_t location)
26375 {
26376   tree c;
26377
26378   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26379
26380   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26381   OMP_CLAUSE_CHAIN (c) = list;
26382   return c;
26383 }
26384
26385 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
26386    is a bitmask in MASK.  Return the list of clauses found; the result
26387    of clause default goes in *pdefault.  */
26388
26389 static tree
26390 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26391                            const char *where, cp_token *pragma_tok)
26392 {
26393   tree clauses = NULL;
26394   bool first = true;
26395   cp_token *token = NULL;
26396
26397   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26398     {
26399       pragma_omp_clause c_kind;
26400       const char *c_name;
26401       tree prev = clauses;
26402
26403       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26404         cp_lexer_consume_token (parser->lexer);
26405
26406       token = cp_lexer_peek_token (parser->lexer);
26407       c_kind = cp_parser_omp_clause_name (parser);
26408       first = false;
26409
26410       switch (c_kind)
26411         {
26412         case PRAGMA_OMP_CLAUSE_COLLAPSE:
26413           clauses = cp_parser_omp_clause_collapse (parser, clauses,
26414                                                    token->location);
26415           c_name = "collapse";
26416           break;
26417         case PRAGMA_OMP_CLAUSE_COPYIN:
26418           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26419           c_name = "copyin";
26420           break;
26421         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26422           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26423                                             clauses);
26424           c_name = "copyprivate";
26425           break;
26426         case PRAGMA_OMP_CLAUSE_DEFAULT:
26427           clauses = cp_parser_omp_clause_default (parser, clauses,
26428                                                   token->location);
26429           c_name = "default";
26430           break;
26431         case PRAGMA_OMP_CLAUSE_FINAL:
26432           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26433           c_name = "final";
26434           break;
26435         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26436           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26437                                             clauses);
26438           c_name = "firstprivate";
26439           break;
26440         case PRAGMA_OMP_CLAUSE_IF:
26441           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26442           c_name = "if";
26443           break;
26444         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26445           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26446                                             clauses);
26447           c_name = "lastprivate";
26448           break;
26449         case PRAGMA_OMP_CLAUSE_MERGEABLE:
26450           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26451                                                     token->location);
26452           c_name = "mergeable";
26453           break;
26454         case PRAGMA_OMP_CLAUSE_NOWAIT:
26455           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26456           c_name = "nowait";
26457           break;
26458         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26459           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26460                                                       token->location);
26461           c_name = "num_threads";
26462           break;
26463         case PRAGMA_OMP_CLAUSE_ORDERED:
26464           clauses = cp_parser_omp_clause_ordered (parser, clauses,
26465                                                   token->location);
26466           c_name = "ordered";
26467           break;
26468         case PRAGMA_OMP_CLAUSE_PRIVATE:
26469           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26470                                             clauses);
26471           c_name = "private";
26472           break;
26473         case PRAGMA_OMP_CLAUSE_REDUCTION:
26474           clauses = cp_parser_omp_clause_reduction (parser, clauses);
26475           c_name = "reduction";
26476           break;
26477         case PRAGMA_OMP_CLAUSE_SCHEDULE:
26478           clauses = cp_parser_omp_clause_schedule (parser, clauses,
26479                                                    token->location);
26480           c_name = "schedule";
26481           break;
26482         case PRAGMA_OMP_CLAUSE_SHARED:
26483           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26484                                             clauses);
26485           c_name = "shared";
26486           break;
26487         case PRAGMA_OMP_CLAUSE_UNTIED:
26488           clauses = cp_parser_omp_clause_untied (parser, clauses,
26489                                                  token->location);
26490           c_name = "nowait";
26491           break;
26492         default:
26493           cp_parser_error (parser, "expected %<#pragma omp%> clause");
26494           goto saw_error;
26495         }
26496
26497       if (((mask >> c_kind) & 1) == 0)
26498         {
26499           /* Remove the invalid clause(s) from the list to avoid
26500              confusing the rest of the compiler.  */
26501           clauses = prev;
26502           error_at (token->location, "%qs is not valid for %qs", c_name, where);
26503         }
26504     }
26505  saw_error:
26506   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26507   return finish_omp_clauses (clauses);
26508 }
26509
26510 /* OpenMP 2.5:
26511    structured-block:
26512      statement
26513
26514    In practice, we're also interested in adding the statement to an
26515    outer node.  So it is convenient if we work around the fact that
26516    cp_parser_statement calls add_stmt.  */
26517
26518 static unsigned
26519 cp_parser_begin_omp_structured_block (cp_parser *parser)
26520 {
26521   unsigned save = parser->in_statement;
26522
26523   /* Only move the values to IN_OMP_BLOCK if they weren't false.
26524      This preserves the "not within loop or switch" style error messages
26525      for nonsense cases like
26526         void foo() {
26527         #pragma omp single
26528           break;
26529         }
26530   */
26531   if (parser->in_statement)
26532     parser->in_statement = IN_OMP_BLOCK;
26533
26534   return save;
26535 }
26536
26537 static void
26538 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26539 {
26540   parser->in_statement = save;
26541 }
26542
26543 static tree
26544 cp_parser_omp_structured_block (cp_parser *parser)
26545 {
26546   tree stmt = begin_omp_structured_block ();
26547   unsigned int save = cp_parser_begin_omp_structured_block (parser);
26548
26549   cp_parser_statement (parser, NULL_TREE, false, NULL);
26550
26551   cp_parser_end_omp_structured_block (parser, save);
26552   return finish_omp_structured_block (stmt);
26553 }
26554
26555 /* OpenMP 2.5:
26556    # pragma omp atomic new-line
26557      expression-stmt
26558
26559    expression-stmt:
26560      x binop= expr | x++ | ++x | x-- | --x
26561    binop:
26562      +, *, -, /, &, ^, |, <<, >>
26563
26564   where x is an lvalue expression with scalar type.
26565
26566    OpenMP 3.1:
26567    # pragma omp atomic new-line
26568      update-stmt
26569
26570    # pragma omp atomic read new-line
26571      read-stmt
26572
26573    # pragma omp atomic write new-line
26574      write-stmt
26575
26576    # pragma omp atomic update new-line
26577      update-stmt
26578
26579    # pragma omp atomic capture new-line
26580      capture-stmt
26581
26582    # pragma omp atomic capture new-line
26583      capture-block
26584
26585    read-stmt:
26586      v = x
26587    write-stmt:
26588      x = expr
26589    update-stmt:
26590      expression-stmt | x = x binop expr
26591    capture-stmt:
26592      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26593    capture-block:
26594      { v = x; update-stmt; } | { update-stmt; v = x; }
26595
26596   where x and v are lvalue expressions with scalar type.  */
26597
26598 static void
26599 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26600 {
26601   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26602   tree rhs1 = NULL_TREE, orig_lhs;
26603   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26604   bool structured_block = false;
26605
26606   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26607     {
26608       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26609       const char *p = IDENTIFIER_POINTER (id);
26610
26611       if (!strcmp (p, "read"))
26612         code = OMP_ATOMIC_READ;
26613       else if (!strcmp (p, "write"))
26614         code = NOP_EXPR;
26615       else if (!strcmp (p, "update"))
26616         code = OMP_ATOMIC;
26617       else if (!strcmp (p, "capture"))
26618         code = OMP_ATOMIC_CAPTURE_NEW;
26619       else
26620         p = NULL;
26621       if (p)
26622         cp_lexer_consume_token (parser->lexer);
26623     }
26624   cp_parser_require_pragma_eol (parser, pragma_tok);
26625
26626   switch (code)
26627     {
26628     case OMP_ATOMIC_READ:
26629     case NOP_EXPR: /* atomic write */
26630       v = cp_parser_unary_expression (parser, /*address_p=*/false,
26631                                       /*cast_p=*/false, NULL);
26632       if (v == error_mark_node)
26633         goto saw_error;
26634       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26635         goto saw_error;
26636       if (code == NOP_EXPR)
26637         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26638       else
26639         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26640                                           /*cast_p=*/false, NULL);
26641       if (lhs == error_mark_node)
26642         goto saw_error;
26643       if (code == NOP_EXPR)
26644         {
26645           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26646              opcode.  */
26647           code = OMP_ATOMIC;
26648           rhs = lhs;
26649           lhs = v;
26650           v = NULL_TREE;
26651         }
26652       goto done;
26653     case OMP_ATOMIC_CAPTURE_NEW:
26654       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26655         {
26656           cp_lexer_consume_token (parser->lexer);
26657           structured_block = true;
26658         }
26659       else
26660         {
26661           v = cp_parser_unary_expression (parser, /*address_p=*/false,
26662                                           /*cast_p=*/false, NULL);
26663           if (v == error_mark_node)
26664             goto saw_error;
26665           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26666             goto saw_error;
26667         }
26668     default:
26669       break;
26670     }
26671
26672 restart:
26673   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26674                                     /*cast_p=*/false, NULL);
26675   orig_lhs = lhs;
26676   switch (TREE_CODE (lhs))
26677     {
26678     case ERROR_MARK:
26679       goto saw_error;
26680
26681     case POSTINCREMENT_EXPR:
26682       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26683         code = OMP_ATOMIC_CAPTURE_OLD;
26684       /* FALLTHROUGH */
26685     case PREINCREMENT_EXPR:
26686       lhs = TREE_OPERAND (lhs, 0);
26687       opcode = PLUS_EXPR;
26688       rhs = integer_one_node;
26689       break;
26690
26691     case POSTDECREMENT_EXPR:
26692       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26693         code = OMP_ATOMIC_CAPTURE_OLD;
26694       /* FALLTHROUGH */
26695     case PREDECREMENT_EXPR:
26696       lhs = TREE_OPERAND (lhs, 0);
26697       opcode = MINUS_EXPR;
26698       rhs = integer_one_node;
26699       break;
26700
26701     case COMPOUND_EXPR:
26702       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26703          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26704          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26705          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26706          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26707                                              (TREE_OPERAND (lhs, 1), 0), 0)))
26708             == BOOLEAN_TYPE)
26709        /* Undo effects of boolean_increment for post {in,de}crement.  */
26710        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26711       /* FALLTHRU */
26712     case MODIFY_EXPR:
26713       if (TREE_CODE (lhs) == MODIFY_EXPR
26714          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26715         {
26716           /* Undo effects of boolean_increment.  */
26717           if (integer_onep (TREE_OPERAND (lhs, 1)))
26718             {
26719               /* This is pre or post increment.  */
26720               rhs = TREE_OPERAND (lhs, 1);
26721               lhs = TREE_OPERAND (lhs, 0);
26722               opcode = NOP_EXPR;
26723               if (code == OMP_ATOMIC_CAPTURE_NEW
26724                   && !structured_block
26725                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26726                 code = OMP_ATOMIC_CAPTURE_OLD;
26727               break;
26728             }
26729         }
26730       /* FALLTHRU */
26731     default:
26732       switch (cp_lexer_peek_token (parser->lexer)->type)
26733         {
26734         case CPP_MULT_EQ:
26735           opcode = MULT_EXPR;
26736           break;
26737         case CPP_DIV_EQ:
26738           opcode = TRUNC_DIV_EXPR;
26739           break;
26740         case CPP_PLUS_EQ:
26741           opcode = PLUS_EXPR;
26742           break;
26743         case CPP_MINUS_EQ:
26744           opcode = MINUS_EXPR;
26745           break;
26746         case CPP_LSHIFT_EQ:
26747           opcode = LSHIFT_EXPR;
26748           break;
26749         case CPP_RSHIFT_EQ:
26750           opcode = RSHIFT_EXPR;
26751           break;
26752         case CPP_AND_EQ:
26753           opcode = BIT_AND_EXPR;
26754           break;
26755         case CPP_OR_EQ:
26756           opcode = BIT_IOR_EXPR;
26757           break;
26758         case CPP_XOR_EQ:
26759           opcode = BIT_XOR_EXPR;
26760           break;
26761         case CPP_EQ:
26762           if (structured_block || code == OMP_ATOMIC)
26763             {
26764               enum cp_parser_prec oprec;
26765               cp_token *token;
26766               cp_lexer_consume_token (parser->lexer);
26767               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26768                                                  /*cast_p=*/false, NULL);
26769               if (rhs1 == error_mark_node)
26770                 goto saw_error;
26771               token = cp_lexer_peek_token (parser->lexer);
26772               switch (token->type)
26773                 {
26774                 case CPP_SEMICOLON:
26775                   if (code == OMP_ATOMIC_CAPTURE_NEW)
26776                     {
26777                       code = OMP_ATOMIC_CAPTURE_OLD;
26778                       v = lhs;
26779                       lhs = NULL_TREE;
26780                       lhs1 = rhs1;
26781                       rhs1 = NULL_TREE;
26782                       cp_lexer_consume_token (parser->lexer);
26783                       goto restart;
26784                     }
26785                   cp_parser_error (parser,
26786                                    "invalid form of %<#pragma omp atomic%>");
26787                   goto saw_error;
26788                 case CPP_MULT:
26789                   opcode = MULT_EXPR;
26790                   break;
26791                 case CPP_DIV:
26792                   opcode = TRUNC_DIV_EXPR;
26793                   break;
26794                 case CPP_PLUS:
26795                   opcode = PLUS_EXPR;
26796                   break;
26797                 case CPP_MINUS:
26798                   opcode = MINUS_EXPR;
26799                   break;
26800                 case CPP_LSHIFT:
26801                   opcode = LSHIFT_EXPR;
26802                   break;
26803                 case CPP_RSHIFT:
26804                   opcode = RSHIFT_EXPR;
26805                   break;
26806                 case CPP_AND:
26807                   opcode = BIT_AND_EXPR;
26808                   break;
26809                 case CPP_OR:
26810                   opcode = BIT_IOR_EXPR;
26811                   break;
26812                 case CPP_XOR:
26813                   opcode = BIT_XOR_EXPR;
26814                   break;
26815                 default:
26816                   cp_parser_error (parser,
26817                                    "invalid operator for %<#pragma omp atomic%>");
26818                   goto saw_error;
26819                 }
26820               oprec = TOKEN_PRECEDENCE (token);
26821               gcc_assert (oprec != PREC_NOT_OPERATOR);
26822               if (commutative_tree_code (opcode))
26823                 oprec = (enum cp_parser_prec) (oprec - 1);
26824               cp_lexer_consume_token (parser->lexer);
26825               rhs = cp_parser_binary_expression (parser, false, false,
26826                                                  oprec, NULL);
26827               if (rhs == error_mark_node)
26828                 goto saw_error;
26829               goto stmt_done;
26830             }
26831           /* FALLTHROUGH */
26832         default:
26833           cp_parser_error (parser,
26834                            "invalid operator for %<#pragma omp atomic%>");
26835           goto saw_error;
26836         }
26837       cp_lexer_consume_token (parser->lexer);
26838
26839       rhs = cp_parser_expression (parser, false, NULL);
26840       if (rhs == error_mark_node)
26841         goto saw_error;
26842       break;
26843     }
26844 stmt_done:
26845   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26846     {
26847       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26848         goto saw_error;
26849       v = cp_parser_unary_expression (parser, /*address_p=*/false,
26850                                       /*cast_p=*/false, NULL);
26851       if (v == error_mark_node)
26852         goto saw_error;
26853       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26854         goto saw_error;
26855       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26856                                          /*cast_p=*/false, NULL);
26857       if (lhs1 == error_mark_node)
26858         goto saw_error;
26859     }
26860   if (structured_block)
26861     {
26862       cp_parser_consume_semicolon_at_end_of_statement (parser);
26863       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26864     }
26865 done:
26866   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26867   if (!structured_block)
26868     cp_parser_consume_semicolon_at_end_of_statement (parser);
26869   return;
26870
26871  saw_error:
26872   cp_parser_skip_to_end_of_block_or_statement (parser);
26873   if (structured_block)
26874     {
26875       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26876         cp_lexer_consume_token (parser->lexer);
26877       else if (code == OMP_ATOMIC_CAPTURE_NEW)
26878         {
26879           cp_parser_skip_to_end_of_block_or_statement (parser);
26880           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26881             cp_lexer_consume_token (parser->lexer);
26882         }
26883     }
26884 }
26885
26886
26887 /* OpenMP 2.5:
26888    # pragma omp barrier new-line  */
26889
26890 static void
26891 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26892 {
26893   cp_parser_require_pragma_eol (parser, pragma_tok);
26894   finish_omp_barrier ();
26895 }
26896
26897 /* OpenMP 2.5:
26898    # pragma omp critical [(name)] new-line
26899      structured-block  */
26900
26901 static tree
26902 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26903 {
26904   tree stmt, name = NULL;
26905
26906   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26907     {
26908       cp_lexer_consume_token (parser->lexer);
26909
26910       name = cp_parser_identifier (parser);
26911
26912       if (name == error_mark_node
26913           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26914         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26915                                                /*or_comma=*/false,
26916                                                /*consume_paren=*/true);
26917       if (name == error_mark_node)
26918         name = NULL;
26919     }
26920   cp_parser_require_pragma_eol (parser, pragma_tok);
26921
26922   stmt = cp_parser_omp_structured_block (parser);
26923   return c_finish_omp_critical (input_location, stmt, name);
26924 }
26925
26926 /* OpenMP 2.5:
26927    # pragma omp flush flush-vars[opt] new-line
26928
26929    flush-vars:
26930      ( variable-list ) */
26931
26932 static void
26933 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26934 {
26935   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26936     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26937   cp_parser_require_pragma_eol (parser, pragma_tok);
26938
26939   finish_omp_flush ();
26940 }
26941
26942 /* Helper function, to parse omp for increment expression.  */
26943
26944 static tree
26945 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26946 {
26947   tree cond = cp_parser_binary_expression (parser, false, true,
26948                                            PREC_NOT_OPERATOR, NULL);
26949   if (cond == error_mark_node
26950       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26951     {
26952       cp_parser_skip_to_end_of_statement (parser);
26953       return error_mark_node;
26954     }
26955
26956   switch (TREE_CODE (cond))
26957     {
26958     case GT_EXPR:
26959     case GE_EXPR:
26960     case LT_EXPR:
26961     case LE_EXPR:
26962       break;
26963     default:
26964       return error_mark_node;
26965     }
26966
26967   /* If decl is an iterator, preserve LHS and RHS of the relational
26968      expr until finish_omp_for.  */
26969   if (decl
26970       && (type_dependent_expression_p (decl)
26971           || CLASS_TYPE_P (TREE_TYPE (decl))))
26972     return cond;
26973
26974   return build_x_binary_op (input_location, TREE_CODE (cond),
26975                             TREE_OPERAND (cond, 0), ERROR_MARK,
26976                             TREE_OPERAND (cond, 1), ERROR_MARK,
26977                             /*overload=*/NULL, tf_warning_or_error);
26978 }
26979
26980 /* Helper function, to parse omp for increment expression.  */
26981
26982 static tree
26983 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26984 {
26985   cp_token *token = cp_lexer_peek_token (parser->lexer);
26986   enum tree_code op;
26987   tree lhs, rhs;
26988   cp_id_kind idk;
26989   bool decl_first;
26990
26991   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26992     {
26993       op = (token->type == CPP_PLUS_PLUS
26994             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26995       cp_lexer_consume_token (parser->lexer);
26996       lhs = cp_parser_simple_cast_expression (parser);
26997       if (lhs != decl)
26998         return error_mark_node;
26999       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27000     }
27001
27002   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
27003   if (lhs != decl)
27004     return error_mark_node;
27005
27006   token = cp_lexer_peek_token (parser->lexer);
27007   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27008     {
27009       op = (token->type == CPP_PLUS_PLUS
27010             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
27011       cp_lexer_consume_token (parser->lexer);
27012       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27013     }
27014
27015   op = cp_parser_assignment_operator_opt (parser);
27016   if (op == ERROR_MARK)
27017     return error_mark_node;
27018
27019   if (op != NOP_EXPR)
27020     {
27021       rhs = cp_parser_assignment_expression (parser, false, NULL);
27022       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
27023       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27024     }
27025
27026   lhs = cp_parser_binary_expression (parser, false, false,
27027                                      PREC_ADDITIVE_EXPRESSION, NULL);
27028   token = cp_lexer_peek_token (parser->lexer);
27029   decl_first = lhs == decl;
27030   if (decl_first)
27031     lhs = NULL_TREE;
27032   if (token->type != CPP_PLUS
27033       && token->type != CPP_MINUS)
27034     return error_mark_node;
27035
27036   do
27037     {
27038       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
27039       cp_lexer_consume_token (parser->lexer);
27040       rhs = cp_parser_binary_expression (parser, false, false,
27041                                          PREC_ADDITIVE_EXPRESSION, NULL);
27042       token = cp_lexer_peek_token (parser->lexer);
27043       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
27044         {
27045           if (lhs == NULL_TREE)
27046             {
27047               if (op == PLUS_EXPR)
27048                 lhs = rhs;
27049               else
27050                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
27051                                         tf_warning_or_error);
27052             }
27053           else
27054             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
27055                                      ERROR_MARK, NULL, tf_warning_or_error);
27056         }
27057     }
27058   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
27059
27060   if (!decl_first)
27061     {
27062       if (rhs != decl || op == MINUS_EXPR)
27063         return error_mark_node;
27064       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
27065     }
27066   else
27067     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
27068
27069   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27070 }
27071
27072 /* Parse the restricted form of the for statement allowed by OpenMP.  */
27073
27074 static tree
27075 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
27076 {
27077   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
27078   tree real_decl, initv, condv, incrv, declv;
27079   tree this_pre_body, cl;
27080   location_t loc_first;
27081   bool collapse_err = false;
27082   int i, collapse = 1, nbraces = 0;
27083   vec<tree, va_gc> *for_block = make_tree_vector ();
27084
27085   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
27086     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
27087       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
27088
27089   gcc_assert (collapse >= 1);
27090
27091   declv = make_tree_vec (collapse);
27092   initv = make_tree_vec (collapse);
27093   condv = make_tree_vec (collapse);
27094   incrv = make_tree_vec (collapse);
27095
27096   loc_first = cp_lexer_peek_token (parser->lexer)->location;
27097
27098   for (i = 0; i < collapse; i++)
27099     {
27100       int bracecount = 0;
27101       bool add_private_clause = false;
27102       location_t loc;
27103
27104       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27105         {
27106           cp_parser_error (parser, "for statement expected");
27107           return NULL;
27108         }
27109       loc = cp_lexer_consume_token (parser->lexer)->location;
27110
27111       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27112         return NULL;
27113
27114       init = decl = real_decl = NULL;
27115       this_pre_body = push_stmt_list ();
27116       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27117         {
27118           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27119
27120              init-expr:
27121                        var = lb
27122                        integer-type var = lb
27123                        random-access-iterator-type var = lb
27124                        pointer-type var = lb
27125           */
27126           cp_decl_specifier_seq type_specifiers;
27127
27128           /* First, try to parse as an initialized declaration.  See
27129              cp_parser_condition, from whence the bulk of this is copied.  */
27130
27131           cp_parser_parse_tentatively (parser);
27132           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
27133                                         /*is_trailing_return=*/false,
27134                                         &type_specifiers);
27135           if (cp_parser_parse_definitely (parser))
27136             {
27137               /* If parsing a type specifier seq succeeded, then this
27138                  MUST be a initialized declaration.  */
27139               tree asm_specification, attributes;
27140               cp_declarator *declarator;
27141
27142               declarator = cp_parser_declarator (parser,
27143                                                  CP_PARSER_DECLARATOR_NAMED,
27144                                                  /*ctor_dtor_or_conv_p=*/NULL,
27145                                                  /*parenthesized_p=*/NULL,
27146                                                  /*member_p=*/false);
27147               attributes = cp_parser_attributes_opt (parser);
27148               asm_specification = cp_parser_asm_specification_opt (parser);
27149
27150               if (declarator == cp_error_declarator) 
27151                 cp_parser_skip_to_end_of_statement (parser);
27152
27153               else 
27154                 {
27155                   tree pushed_scope, auto_node;
27156
27157                   decl = start_decl (declarator, &type_specifiers,
27158                                      SD_INITIALIZED, attributes,
27159                                      /*prefix_attributes=*/NULL_TREE,
27160                                      &pushed_scope);
27161
27162                   auto_node = type_uses_auto (TREE_TYPE (decl));
27163                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27164                     {
27165                       if (cp_lexer_next_token_is (parser->lexer, 
27166                                                   CPP_OPEN_PAREN))
27167                         error ("parenthesized initialization is not allowed in "
27168                                "OpenMP %<for%> loop");
27169                       else
27170                         /* Trigger an error.  */
27171                         cp_parser_require (parser, CPP_EQ, RT_EQ);
27172
27173                       init = error_mark_node;
27174                       cp_parser_skip_to_end_of_statement (parser);
27175                     }
27176                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
27177                            || type_dependent_expression_p (decl)
27178                            || auto_node)
27179                     {
27180                       bool is_direct_init, is_non_constant_init;
27181
27182                       init = cp_parser_initializer (parser,
27183                                                     &is_direct_init,
27184                                                     &is_non_constant_init);
27185
27186                       if (auto_node)
27187                         {
27188                           TREE_TYPE (decl)
27189                             = do_auto_deduction (TREE_TYPE (decl), init,
27190                                                  auto_node);
27191
27192                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
27193                               && !type_dependent_expression_p (decl))
27194                             goto non_class;
27195                         }
27196                       
27197                       cp_finish_decl (decl, init, !is_non_constant_init,
27198                                       asm_specification,
27199                                       LOOKUP_ONLYCONVERTING);
27200                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
27201                         {
27202                           vec_safe_push (for_block, this_pre_body);
27203                           init = NULL_TREE;
27204                         }
27205                       else
27206                         init = pop_stmt_list (this_pre_body);
27207                       this_pre_body = NULL_TREE;
27208                     }
27209                   else
27210                     {
27211                       /* Consume '='.  */
27212                       cp_lexer_consume_token (parser->lexer);
27213                       init = cp_parser_assignment_expression (parser, false, NULL);
27214
27215                     non_class:
27216                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27217                         init = error_mark_node;
27218                       else
27219                         cp_finish_decl (decl, NULL_TREE,
27220                                         /*init_const_expr_p=*/false,
27221                                         asm_specification,
27222                                         LOOKUP_ONLYCONVERTING);
27223                     }
27224
27225                   if (pushed_scope)
27226                     pop_scope (pushed_scope);
27227                 }
27228             }
27229           else 
27230             {
27231               cp_id_kind idk;
27232               /* If parsing a type specifier sequence failed, then
27233                  this MUST be a simple expression.  */
27234               cp_parser_parse_tentatively (parser);
27235               decl = cp_parser_primary_expression (parser, false, false,
27236                                                    false, &idk);
27237               if (!cp_parser_error_occurred (parser)
27238                   && decl
27239                   && DECL_P (decl)
27240                   && CLASS_TYPE_P (TREE_TYPE (decl)))
27241                 {
27242                   tree rhs;
27243
27244                   cp_parser_parse_definitely (parser);
27245                   cp_parser_require (parser, CPP_EQ, RT_EQ);
27246                   rhs = cp_parser_assignment_expression (parser, false, NULL);
27247                   finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27248                                                          decl, NOP_EXPR,
27249                                                          rhs,
27250                                                          tf_warning_or_error));
27251                   add_private_clause = true;
27252                 }
27253               else
27254                 {
27255                   decl = NULL;
27256                   cp_parser_abort_tentative_parse (parser);
27257                   init = cp_parser_expression (parser, false, NULL);
27258                   if (init)
27259                     {
27260                       if (TREE_CODE (init) == MODIFY_EXPR
27261                           || TREE_CODE (init) == MODOP_EXPR)
27262                         real_decl = TREE_OPERAND (init, 0);
27263                     }
27264                 }
27265             }
27266         }
27267       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27268       if (this_pre_body)
27269         {
27270           this_pre_body = pop_stmt_list (this_pre_body);
27271           if (pre_body)
27272             {
27273               tree t = pre_body;
27274               pre_body = push_stmt_list ();
27275               add_stmt (t);
27276               add_stmt (this_pre_body);
27277               pre_body = pop_stmt_list (pre_body);
27278             }
27279           else
27280             pre_body = this_pre_body;
27281         }
27282
27283       if (decl)
27284         real_decl = decl;
27285       if (par_clauses != NULL && real_decl != NULL_TREE)
27286         {
27287           tree *c;
27288           for (c = par_clauses; *c ; )
27289             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27290                 && OMP_CLAUSE_DECL (*c) == real_decl)
27291               {
27292                 error_at (loc, "iteration variable %qD"
27293                           " should not be firstprivate", real_decl);
27294                 *c = OMP_CLAUSE_CHAIN (*c);
27295               }
27296             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27297                      && OMP_CLAUSE_DECL (*c) == real_decl)
27298               {
27299                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27300                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
27301                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27302                 OMP_CLAUSE_DECL (l) = real_decl;
27303                 OMP_CLAUSE_CHAIN (l) = clauses;
27304                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27305                 clauses = l;
27306                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27307                 CP_OMP_CLAUSE_INFO (*c) = NULL;
27308                 add_private_clause = false;
27309               }
27310             else
27311               {
27312                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27313                     && OMP_CLAUSE_DECL (*c) == real_decl)
27314                   add_private_clause = false;
27315                 c = &OMP_CLAUSE_CHAIN (*c);
27316               }
27317         }
27318
27319       if (add_private_clause)
27320         {
27321           tree c;
27322           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27323             {
27324               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27325                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27326                   && OMP_CLAUSE_DECL (c) == decl)
27327                 break;
27328               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27329                        && OMP_CLAUSE_DECL (c) == decl)
27330                 error_at (loc, "iteration variable %qD "
27331                           "should not be firstprivate",
27332                           decl);
27333               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27334                        && OMP_CLAUSE_DECL (c) == decl)
27335                 error_at (loc, "iteration variable %qD should not be reduction",
27336                           decl);
27337             }
27338           if (c == NULL)
27339             {
27340               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27341               OMP_CLAUSE_DECL (c) = decl;
27342               c = finish_omp_clauses (c);
27343               if (c)
27344                 {
27345                   OMP_CLAUSE_CHAIN (c) = clauses;
27346                   clauses = c;
27347                 }
27348             }
27349         }
27350
27351       cond = NULL;
27352       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27353         cond = cp_parser_omp_for_cond (parser, decl);
27354       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27355
27356       incr = NULL;
27357       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27358         {
27359           /* If decl is an iterator, preserve the operator on decl
27360              until finish_omp_for.  */
27361           if (real_decl
27362               && ((processing_template_decl
27363                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27364                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27365             incr = cp_parser_omp_for_incr (parser, real_decl);
27366           else
27367             incr = cp_parser_expression (parser, false, NULL);
27368           if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27369             SET_EXPR_LOCATION (incr, input_location);
27370         }
27371
27372       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27373         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27374                                                /*or_comma=*/false,
27375                                                /*consume_paren=*/true);
27376
27377       TREE_VEC_ELT (declv, i) = decl;
27378       TREE_VEC_ELT (initv, i) = init;
27379       TREE_VEC_ELT (condv, i) = cond;
27380       TREE_VEC_ELT (incrv, i) = incr;
27381
27382       if (i == collapse - 1)
27383         break;
27384
27385       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27386          in between the collapsed for loops to be still considered perfectly
27387          nested.  Hopefully the final version clarifies this.
27388          For now handle (multiple) {'s and empty statements.  */
27389       cp_parser_parse_tentatively (parser);
27390       do
27391         {
27392           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27393             break;
27394           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27395             {
27396               cp_lexer_consume_token (parser->lexer);
27397               bracecount++;
27398             }
27399           else if (bracecount
27400                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27401             cp_lexer_consume_token (parser->lexer);
27402           else
27403             {
27404               loc = cp_lexer_peek_token (parser->lexer)->location;
27405               error_at (loc, "not enough collapsed for loops");
27406               collapse_err = true;
27407               cp_parser_abort_tentative_parse (parser);
27408               declv = NULL_TREE;
27409               break;
27410             }
27411         }
27412       while (1);
27413
27414       if (declv)
27415         {
27416           cp_parser_parse_definitely (parser);
27417           nbraces += bracecount;
27418         }
27419     }
27420
27421   /* Note that we saved the original contents of this flag when we entered
27422      the structured block, and so we don't need to re-save it here.  */
27423   parser->in_statement = IN_OMP_FOR;
27424
27425   /* Note that the grammar doesn't call for a structured block here,
27426      though the loop as a whole is a structured block.  */
27427   body = push_stmt_list ();
27428   cp_parser_statement (parser, NULL_TREE, false, NULL);
27429   body = pop_stmt_list (body);
27430
27431   if (declv == NULL_TREE)
27432     ret = NULL_TREE;
27433   else
27434     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27435                           pre_body, clauses);
27436
27437   while (nbraces)
27438     {
27439       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27440         {
27441           cp_lexer_consume_token (parser->lexer);
27442           nbraces--;
27443         }
27444       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27445         cp_lexer_consume_token (parser->lexer);
27446       else
27447         {
27448           if (!collapse_err)
27449             {
27450               error_at (cp_lexer_peek_token (parser->lexer)->location,
27451                         "collapsed loops not perfectly nested");
27452             }
27453           collapse_err = true;
27454           cp_parser_statement_seq_opt (parser, NULL);
27455           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27456             break;
27457         }
27458     }
27459
27460   while (!for_block->is_empty ())
27461     add_stmt (pop_stmt_list (for_block->pop ()));
27462   release_tree_vector (for_block);
27463
27464   return ret;
27465 }
27466
27467 /* OpenMP 2.5:
27468    #pragma omp for for-clause[optseq] new-line
27469      for-loop  */
27470
27471 #define OMP_FOR_CLAUSE_MASK                             \
27472         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
27473         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
27474         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
27475         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
27476         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
27477         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
27478         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
27479         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27480
27481 static tree
27482 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27483 {
27484   tree clauses, sb, ret;
27485   unsigned int save;
27486
27487   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27488                                        "#pragma omp for", pragma_tok);
27489
27490   sb = begin_omp_structured_block ();
27491   save = cp_parser_begin_omp_structured_block (parser);
27492
27493   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27494
27495   cp_parser_end_omp_structured_block (parser, save);
27496   add_stmt (finish_omp_structured_block (sb));
27497
27498   return ret;
27499 }
27500
27501 /* OpenMP 2.5:
27502    # pragma omp master new-line
27503      structured-block  */
27504
27505 static tree
27506 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27507 {
27508   cp_parser_require_pragma_eol (parser, pragma_tok);
27509   return c_finish_omp_master (input_location,
27510                               cp_parser_omp_structured_block (parser));
27511 }
27512
27513 /* OpenMP 2.5:
27514    # pragma omp ordered new-line
27515      structured-block  */
27516
27517 static tree
27518 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27519 {
27520   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27521   cp_parser_require_pragma_eol (parser, pragma_tok);
27522   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27523 }
27524
27525 /* OpenMP 2.5:
27526
27527    section-scope:
27528      { section-sequence }
27529
27530    section-sequence:
27531      section-directive[opt] structured-block
27532      section-sequence section-directive structured-block  */
27533
27534 static tree
27535 cp_parser_omp_sections_scope (cp_parser *parser)
27536 {
27537   tree stmt, substmt;
27538   bool error_suppress = false;
27539   cp_token *tok;
27540
27541   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27542     return NULL_TREE;
27543
27544   stmt = push_stmt_list ();
27545
27546   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27547     {
27548       unsigned save;
27549
27550       substmt = begin_omp_structured_block ();
27551       save = cp_parser_begin_omp_structured_block (parser);
27552
27553       while (1)
27554         {
27555           cp_parser_statement (parser, NULL_TREE, false, NULL);
27556
27557           tok = cp_lexer_peek_token (parser->lexer);
27558           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27559             break;
27560           if (tok->type == CPP_CLOSE_BRACE)
27561             break;
27562           if (tok->type == CPP_EOF)
27563             break;
27564         }
27565
27566       cp_parser_end_omp_structured_block (parser, save);
27567       substmt = finish_omp_structured_block (substmt);
27568       substmt = build1 (OMP_SECTION, void_type_node, substmt);
27569       add_stmt (substmt);
27570     }
27571
27572   while (1)
27573     {
27574       tok = cp_lexer_peek_token (parser->lexer);
27575       if (tok->type == CPP_CLOSE_BRACE)
27576         break;
27577       if (tok->type == CPP_EOF)
27578         break;
27579
27580       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27581         {
27582           cp_lexer_consume_token (parser->lexer);
27583           cp_parser_require_pragma_eol (parser, tok);
27584           error_suppress = false;
27585         }
27586       else if (!error_suppress)
27587         {
27588           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27589           error_suppress = true;
27590         }
27591
27592       substmt = cp_parser_omp_structured_block (parser);
27593       substmt = build1 (OMP_SECTION, void_type_node, substmt);
27594       add_stmt (substmt);
27595     }
27596   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27597
27598   substmt = pop_stmt_list (stmt);
27599
27600   stmt = make_node (OMP_SECTIONS);
27601   TREE_TYPE (stmt) = void_type_node;
27602   OMP_SECTIONS_BODY (stmt) = substmt;
27603
27604   add_stmt (stmt);
27605   return stmt;
27606 }
27607
27608 /* OpenMP 2.5:
27609    # pragma omp sections sections-clause[optseq] newline
27610      sections-scope  */
27611
27612 #define OMP_SECTIONS_CLAUSE_MASK                        \
27613         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
27614         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
27615         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
27616         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
27617         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27618
27619 static tree
27620 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27621 {
27622   tree clauses, ret;
27623
27624   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27625                                        "#pragma omp sections", pragma_tok);
27626
27627   ret = cp_parser_omp_sections_scope (parser);
27628   if (ret)
27629     OMP_SECTIONS_CLAUSES (ret) = clauses;
27630
27631   return ret;
27632 }
27633
27634 /* OpenMP 2.5:
27635    # pragma parallel parallel-clause new-line
27636    # pragma parallel for parallel-for-clause new-line
27637    # pragma parallel sections parallel-sections-clause new-line  */
27638
27639 #define OMP_PARALLEL_CLAUSE_MASK                        \
27640         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
27641         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
27642         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
27643         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
27644         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
27645         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
27646         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
27647         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27648
27649 static tree
27650 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27651 {
27652   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27653   const char *p_name = "#pragma omp parallel";
27654   tree stmt, clauses, par_clause, ws_clause, block;
27655   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27656   unsigned int save;
27657   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27658
27659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27660     {
27661       cp_lexer_consume_token (parser->lexer);
27662       p_kind = PRAGMA_OMP_PARALLEL_FOR;
27663       p_name = "#pragma omp parallel for";
27664       mask |= OMP_FOR_CLAUSE_MASK;
27665       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27666     }
27667   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27668     {
27669       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27670       const char *p = IDENTIFIER_POINTER (id);
27671       if (strcmp (p, "sections") == 0)
27672         {
27673           cp_lexer_consume_token (parser->lexer);
27674           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27675           p_name = "#pragma omp parallel sections";
27676           mask |= OMP_SECTIONS_CLAUSE_MASK;
27677           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27678         }
27679     }
27680
27681   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27682   block = begin_omp_parallel ();
27683   save = cp_parser_begin_omp_structured_block (parser);
27684
27685   switch (p_kind)
27686     {
27687     case PRAGMA_OMP_PARALLEL:
27688       cp_parser_statement (parser, NULL_TREE, false, NULL);
27689       par_clause = clauses;
27690       break;
27691
27692     case PRAGMA_OMP_PARALLEL_FOR:
27693       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27694       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27695       break;
27696
27697     case PRAGMA_OMP_PARALLEL_SECTIONS:
27698       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27699       stmt = cp_parser_omp_sections_scope (parser);
27700       if (stmt)
27701         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27702       break;
27703
27704     default:
27705       gcc_unreachable ();
27706     }
27707
27708   cp_parser_end_omp_structured_block (parser, save);
27709   stmt = finish_omp_parallel (par_clause, block);
27710   if (p_kind != PRAGMA_OMP_PARALLEL)
27711     OMP_PARALLEL_COMBINED (stmt) = 1;
27712   return stmt;
27713 }
27714
27715 /* OpenMP 2.5:
27716    # pragma omp single single-clause[optseq] new-line
27717      structured-block  */
27718
27719 #define OMP_SINGLE_CLAUSE_MASK                          \
27720         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
27721         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
27722         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
27723         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27724
27725 static tree
27726 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27727 {
27728   tree stmt = make_node (OMP_SINGLE);
27729   TREE_TYPE (stmt) = void_type_node;
27730
27731   OMP_SINGLE_CLAUSES (stmt)
27732     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27733                                  "#pragma omp single", pragma_tok);
27734   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27735
27736   return add_stmt (stmt);
27737 }
27738
27739 /* OpenMP 3.0:
27740    # pragma omp task task-clause[optseq] new-line
27741      structured-block  */
27742
27743 #define OMP_TASK_CLAUSE_MASK                            \
27744         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
27745         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
27746         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
27747         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
27748         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
27749         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
27750         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
27751         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27752
27753 static tree
27754 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27755 {
27756   tree clauses, block;
27757   unsigned int save;
27758
27759   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27760                                        "#pragma omp task", pragma_tok);
27761   block = begin_omp_task ();
27762   save = cp_parser_begin_omp_structured_block (parser);
27763   cp_parser_statement (parser, NULL_TREE, false, NULL);
27764   cp_parser_end_omp_structured_block (parser, save);
27765   return finish_omp_task (clauses, block);
27766 }
27767
27768 /* OpenMP 3.0:
27769    # pragma omp taskwait new-line  */
27770
27771 static void
27772 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27773 {
27774   cp_parser_require_pragma_eol (parser, pragma_tok);
27775   finish_omp_taskwait ();
27776 }
27777
27778 /* OpenMP 3.1:
27779    # pragma omp taskyield new-line  */
27780
27781 static void
27782 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27783 {
27784   cp_parser_require_pragma_eol (parser, pragma_tok);
27785   finish_omp_taskyield ();
27786 }
27787
27788 /* OpenMP 2.5:
27789    # pragma omp threadprivate (variable-list) */
27790
27791 static void
27792 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27793 {
27794   tree vars;
27795
27796   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27797   cp_parser_require_pragma_eol (parser, pragma_tok);
27798
27799   finish_omp_threadprivate (vars);
27800 }
27801
27802 /* Main entry point to OpenMP statement pragmas.  */
27803
27804 static void
27805 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27806 {
27807   tree stmt;
27808
27809   switch (pragma_tok->pragma_kind)
27810     {
27811     case PRAGMA_OMP_ATOMIC:
27812       cp_parser_omp_atomic (parser, pragma_tok);
27813       return;
27814     case PRAGMA_OMP_CRITICAL:
27815       stmt = cp_parser_omp_critical (parser, pragma_tok);
27816       break;
27817     case PRAGMA_OMP_FOR:
27818       stmt = cp_parser_omp_for (parser, pragma_tok);
27819       break;
27820     case PRAGMA_OMP_MASTER:
27821       stmt = cp_parser_omp_master (parser, pragma_tok);
27822       break;
27823     case PRAGMA_OMP_ORDERED:
27824       stmt = cp_parser_omp_ordered (parser, pragma_tok);
27825       break;
27826     case PRAGMA_OMP_PARALLEL:
27827       stmt = cp_parser_omp_parallel (parser, pragma_tok);
27828       break;
27829     case PRAGMA_OMP_SECTIONS:
27830       stmt = cp_parser_omp_sections (parser, pragma_tok);
27831       break;
27832     case PRAGMA_OMP_SINGLE:
27833       stmt = cp_parser_omp_single (parser, pragma_tok);
27834       break;
27835     case PRAGMA_OMP_TASK:
27836       stmt = cp_parser_omp_task (parser, pragma_tok);
27837       break;
27838     default:
27839       gcc_unreachable ();
27840     }
27841
27842   if (stmt)
27843     SET_EXPR_LOCATION (stmt, pragma_tok->location);
27844 }
27845 \f
27846 /* Transactional Memory parsing routines.  */
27847
27848 /* Parse a transaction attribute.
27849
27850    txn-attribute:
27851         attribute
27852         [ [ identifier ] ]
27853
27854    ??? Simplify this when C++0x bracket attributes are
27855    implemented properly.  */
27856
27857 static tree
27858 cp_parser_txn_attribute_opt (cp_parser *parser)
27859 {
27860   cp_token *token;
27861   tree attr_name, attr = NULL;
27862
27863   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27864     return cp_parser_attributes_opt (parser);
27865
27866   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27867     return NULL_TREE;
27868   cp_lexer_consume_token (parser->lexer);
27869   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27870     goto error1;
27871
27872   token = cp_lexer_peek_token (parser->lexer);
27873   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27874     {
27875       token = cp_lexer_consume_token (parser->lexer);
27876
27877       attr_name = (token->type == CPP_KEYWORD
27878                    /* For keywords, use the canonical spelling,
27879                       not the parsed identifier.  */
27880                    ? ridpointers[(int) token->keyword]
27881                    : token->u.value);
27882       attr = build_tree_list (attr_name, NULL_TREE);
27883     }
27884   else
27885     cp_parser_error (parser, "expected identifier");
27886
27887   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27888  error1:
27889   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27890   return attr;
27891 }
27892
27893 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27894
27895    transaction-statement:
27896      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27897        compound-statement
27898      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27899 */
27900
27901 static tree
27902 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27903 {
27904   unsigned char old_in = parser->in_transaction;
27905   unsigned char this_in = 1, new_in;
27906   cp_token *token;
27907   tree stmt, attrs, noex;
27908
27909   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27910       || keyword == RID_TRANSACTION_RELAXED);
27911   token = cp_parser_require_keyword (parser, keyword,
27912       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27913           : RT_TRANSACTION_RELAXED));
27914   gcc_assert (token != NULL);
27915
27916   if (keyword == RID_TRANSACTION_RELAXED)
27917     this_in |= TM_STMT_ATTR_RELAXED;
27918   else
27919     {
27920       attrs = cp_parser_txn_attribute_opt (parser);
27921       if (attrs)
27922         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27923     }
27924
27925   /* Parse a noexcept specification.  */
27926   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27927
27928   /* Keep track if we're in the lexical scope of an outer transaction.  */
27929   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27930
27931   stmt = begin_transaction_stmt (token->location, NULL, this_in);
27932
27933   parser->in_transaction = new_in;
27934   cp_parser_compound_statement (parser, NULL, false, false);
27935   parser->in_transaction = old_in;
27936
27937   finish_transaction_stmt (stmt, NULL, this_in, noex);
27938
27939   return stmt;
27940 }
27941
27942 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27943
27944    transaction-expression:
27945      __transaction_atomic txn-noexcept-spec[opt] ( expression )
27946      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27947 */
27948
27949 static tree
27950 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27951 {
27952   unsigned char old_in = parser->in_transaction;
27953   unsigned char this_in = 1;
27954   cp_token *token;
27955   tree expr, noex;
27956   bool noex_expr;
27957
27958   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27959       || keyword == RID_TRANSACTION_RELAXED);
27960
27961   if (!flag_tm)
27962     error (keyword == RID_TRANSACTION_RELAXED
27963            ? G_("%<__transaction_relaxed%> without transactional memory "
27964                 "support enabled")
27965            : G_("%<__transaction_atomic%> without transactional memory "
27966                 "support enabled"));
27967
27968   token = cp_parser_require_keyword (parser, keyword,
27969       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27970           : RT_TRANSACTION_RELAXED));
27971   gcc_assert (token != NULL);
27972
27973   if (keyword == RID_TRANSACTION_RELAXED)
27974     this_in |= TM_STMT_ATTR_RELAXED;
27975
27976   /* Set this early.  This might mean that we allow transaction_cancel in
27977      an expression that we find out later actually has to be a constexpr.
27978      However, we expect that cxx_constant_value will be able to deal with
27979      this; also, if the noexcept has no constexpr, then what we parse next
27980      really is a transaction's body.  */
27981   parser->in_transaction = this_in;
27982
27983   /* Parse a noexcept specification.  */
27984   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27985                                                true);
27986
27987   if (!noex || !noex_expr
27988       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27989     {
27990       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27991
27992       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27993       finish_parenthesized_expr (expr);
27994
27995       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27996     }
27997   else
27998     {
27999       /* The only expression that is available got parsed for the noexcept
28000          already.  noexcept is true then.  */
28001       expr = noex;
28002       noex = boolean_true_node;
28003     }
28004
28005   expr = build_transaction_expr (token->location, expr, this_in, noex);
28006   parser->in_transaction = old_in;
28007
28008   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
28009     return error_mark_node;
28010
28011   return (flag_tm ? expr : error_mark_node);
28012 }
28013
28014 /* Parse a function-transaction-block.
28015
28016    function-transaction-block:
28017      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28018          function-body
28019      __transaction_atomic txn-attribute[opt] function-try-block
28020      __transaction_relaxed ctor-initializer[opt] function-body
28021      __transaction_relaxed function-try-block
28022 */
28023
28024 static bool
28025 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
28026 {
28027   unsigned char old_in = parser->in_transaction;
28028   unsigned char new_in = 1;
28029   tree compound_stmt, stmt, attrs;
28030   bool ctor_initializer_p;
28031   cp_token *token;
28032
28033   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28034       || keyword == RID_TRANSACTION_RELAXED);
28035   token = cp_parser_require_keyword (parser, keyword,
28036       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28037           : RT_TRANSACTION_RELAXED));
28038   gcc_assert (token != NULL);
28039
28040   if (keyword == RID_TRANSACTION_RELAXED)
28041     new_in |= TM_STMT_ATTR_RELAXED;
28042   else
28043     {
28044       attrs = cp_parser_txn_attribute_opt (parser);
28045       if (attrs)
28046         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28047     }
28048
28049   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
28050
28051   parser->in_transaction = new_in;
28052
28053   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28054     ctor_initializer_p = cp_parser_function_try_block (parser);
28055   else
28056     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
28057       (parser, /*in_function_try_block=*/false);
28058
28059   parser->in_transaction = old_in;
28060
28061   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
28062
28063   return ctor_initializer_p;
28064 }
28065
28066 /* Parse a __transaction_cancel statement.
28067
28068    cancel-statement:
28069      __transaction_cancel txn-attribute[opt] ;
28070      __transaction_cancel txn-attribute[opt] throw-expression ;
28071
28072    ??? Cancel and throw is not yet implemented.  */
28073
28074 static tree
28075 cp_parser_transaction_cancel (cp_parser *parser)
28076 {
28077   cp_token *token;
28078   bool is_outer = false;
28079   tree stmt, attrs;
28080
28081   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
28082                                      RT_TRANSACTION_CANCEL);
28083   gcc_assert (token != NULL);
28084
28085   attrs = cp_parser_txn_attribute_opt (parser);
28086   if (attrs)
28087     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
28088
28089   /* ??? Parse cancel-and-throw here.  */
28090
28091   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28092
28093   if (!flag_tm)
28094     {
28095       error_at (token->location, "%<__transaction_cancel%> without "
28096                 "transactional memory support enabled");
28097       return error_mark_node;
28098     }
28099   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
28100     {
28101       error_at (token->location, "%<__transaction_cancel%> within a "
28102                 "%<__transaction_relaxed%>");
28103       return error_mark_node;
28104     }
28105   else if (is_outer)
28106     {
28107       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
28108           && !is_tm_may_cancel_outer (current_function_decl))
28109         {
28110           error_at (token->location, "outer %<__transaction_cancel%> not "
28111                     "within outer %<__transaction_atomic%>");
28112           error_at (token->location,
28113                     "  or a %<transaction_may_cancel_outer%> function");
28114           return error_mark_node;
28115         }
28116     }
28117   else if (parser->in_transaction == 0)
28118     {
28119       error_at (token->location, "%<__transaction_cancel%> not within "
28120                 "%<__transaction_atomic%>");
28121       return error_mark_node;
28122     }
28123
28124   stmt = build_tm_abort_call (token->location, is_outer);
28125   add_stmt (stmt);
28126   finish_stmt ();
28127
28128   return stmt;
28129 }
28130 \f
28131 /* The parser.  */
28132
28133 static GTY (()) cp_parser *the_parser;
28134
28135 \f
28136 /* Special handling for the first token or line in the file.  The first
28137    thing in the file might be #pragma GCC pch_preprocess, which loads a
28138    PCH file, which is a GC collection point.  So we need to handle this
28139    first pragma without benefit of an existing lexer structure.
28140
28141    Always returns one token to the caller in *FIRST_TOKEN.  This is
28142    either the true first token of the file, or the first token after
28143    the initial pragma.  */
28144
28145 static void
28146 cp_parser_initial_pragma (cp_token *first_token)
28147 {
28148   tree name = NULL;
28149
28150   cp_lexer_get_preprocessor_token (NULL, first_token);
28151   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
28152     return;
28153
28154   cp_lexer_get_preprocessor_token (NULL, first_token);
28155   if (first_token->type == CPP_STRING)
28156     {
28157       name = first_token->u.value;
28158
28159       cp_lexer_get_preprocessor_token (NULL, first_token);
28160       if (first_token->type != CPP_PRAGMA_EOL)
28161         error_at (first_token->location,
28162                   "junk at end of %<#pragma GCC pch_preprocess%>");
28163     }
28164   else
28165     error_at (first_token->location, "expected string literal");
28166
28167   /* Skip to the end of the pragma.  */
28168   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
28169     cp_lexer_get_preprocessor_token (NULL, first_token);
28170
28171   /* Now actually load the PCH file.  */
28172   if (name)
28173     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28174
28175   /* Read one more token to return to our caller.  We have to do this
28176      after reading the PCH file in, since its pointers have to be
28177      live.  */
28178   cp_lexer_get_preprocessor_token (NULL, first_token);
28179 }
28180
28181 /* Normal parsing of a pragma token.  Here we can (and must) use the
28182    regular lexer.  */
28183
28184 static bool
28185 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28186 {
28187   cp_token *pragma_tok;
28188   unsigned int id;
28189
28190   pragma_tok = cp_lexer_consume_token (parser->lexer);
28191   gcc_assert (pragma_tok->type == CPP_PRAGMA);
28192   parser->lexer->in_pragma = true;
28193
28194   id = pragma_tok->pragma_kind;
28195   switch (id)
28196     {
28197     case PRAGMA_GCC_PCH_PREPROCESS:
28198       error_at (pragma_tok->location,
28199                 "%<#pragma GCC pch_preprocess%> must be first");
28200       break;
28201
28202     case PRAGMA_OMP_BARRIER:
28203       switch (context)
28204         {
28205         case pragma_compound:
28206           cp_parser_omp_barrier (parser, pragma_tok);
28207           return false;
28208         case pragma_stmt:
28209           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28210                     "used in compound statements");
28211           break;
28212         default:
28213           goto bad_stmt;
28214         }
28215       break;
28216
28217     case PRAGMA_OMP_FLUSH:
28218       switch (context)
28219         {
28220         case pragma_compound:
28221           cp_parser_omp_flush (parser, pragma_tok);
28222           return false;
28223         case pragma_stmt:
28224           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28225                     "used in compound statements");
28226           break;
28227         default:
28228           goto bad_stmt;
28229         }
28230       break;
28231
28232     case PRAGMA_OMP_TASKWAIT:
28233       switch (context)
28234         {
28235         case pragma_compound:
28236           cp_parser_omp_taskwait (parser, pragma_tok);
28237           return false;
28238         case pragma_stmt:
28239           error_at (pragma_tok->location,
28240                     "%<#pragma omp taskwait%> may only be "
28241                     "used in compound statements");
28242           break;
28243         default:
28244           goto bad_stmt;
28245         }
28246       break;
28247
28248     case PRAGMA_OMP_TASKYIELD:
28249       switch (context)
28250         {
28251         case pragma_compound:
28252           cp_parser_omp_taskyield (parser, pragma_tok);
28253           return false;
28254         case pragma_stmt:
28255           error_at (pragma_tok->location,
28256                     "%<#pragma omp taskyield%> may only be "
28257                     "used in compound statements");
28258           break;
28259         default:
28260           goto bad_stmt;
28261         }
28262       break;
28263
28264     case PRAGMA_OMP_THREADPRIVATE:
28265       cp_parser_omp_threadprivate (parser, pragma_tok);
28266       return false;
28267
28268     case PRAGMA_OMP_ATOMIC:
28269     case PRAGMA_OMP_CRITICAL:
28270     case PRAGMA_OMP_FOR:
28271     case PRAGMA_OMP_MASTER:
28272     case PRAGMA_OMP_ORDERED:
28273     case PRAGMA_OMP_PARALLEL:
28274     case PRAGMA_OMP_SECTIONS:
28275     case PRAGMA_OMP_SINGLE:
28276     case PRAGMA_OMP_TASK:
28277       if (context == pragma_external)
28278         goto bad_stmt;
28279       cp_parser_omp_construct (parser, pragma_tok);
28280       return true;
28281
28282     case PRAGMA_OMP_SECTION:
28283       error_at (pragma_tok->location, 
28284                 "%<#pragma omp section%> may only be used in "
28285                 "%<#pragma omp sections%> construct");
28286       break;
28287
28288     default:
28289       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28290       c_invoke_pragma_handler (id);
28291       break;
28292
28293     bad_stmt:
28294       cp_parser_error (parser, "expected declaration specifiers");
28295       break;
28296     }
28297
28298   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28299   return false;
28300 }
28301
28302 /* The interface the pragma parsers have to the lexer.  */
28303
28304 enum cpp_ttype
28305 pragma_lex (tree *value)
28306 {
28307   cp_token *tok;
28308   enum cpp_ttype ret;
28309
28310   tok = cp_lexer_peek_token (the_parser->lexer);
28311
28312   ret = tok->type;
28313   *value = tok->u.value;
28314
28315   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28316     ret = CPP_EOF;
28317   else if (ret == CPP_STRING)
28318     *value = cp_parser_string_literal (the_parser, false, false);
28319   else
28320     {
28321       cp_lexer_consume_token (the_parser->lexer);
28322       if (ret == CPP_KEYWORD)
28323         ret = CPP_NAME;
28324     }
28325
28326   return ret;
28327 }
28328
28329 \f
28330 /* External interface.  */
28331
28332 /* Parse one entire translation unit.  */
28333
28334 void
28335 c_parse_file (void)
28336 {
28337   static bool already_called = false;
28338
28339   if (already_called)
28340     {
28341       sorry ("inter-module optimizations not implemented for C++");
28342       return;
28343     }
28344   already_called = true;
28345
28346   the_parser = cp_parser_new ();
28347   push_deferring_access_checks (flag_access_control
28348                                 ? dk_no_deferred : dk_no_check);
28349   cp_parser_translation_unit (the_parser);
28350   the_parser = NULL;
28351 }
28352
28353 #include "gt-cp-parser.h"