re PR c++/53549 (g++ and armadillo 3.2.0, operator() is inaccessible)
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011, 2012  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40 #include "tree-pretty-print.h"
41 #include "parser.h"
42
43 \f
44 /* The lexer.  */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47    and c-lex.c) and the C++ parser.  */
48
49 static cp_token eof_token =
50 {
51   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
52 };
53
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56   NIC_NONE,
57   /* floating-point literal */
58   NIC_FLOAT,
59   /* %<this%> */
60   NIC_THIS,
61   /* %<__FUNCTION__%> */
62   NIC_FUNC_NAME,
63   /* %<__PRETTY_FUNCTION__%> */
64   NIC_PRETTY_FUNC,
65   /* %<__func__%> */
66   NIC_C99_FUNC,
67   /* "%<va_arg%> */
68   NIC_VA_ARG,
69   /* a cast */
70   NIC_CAST,
71   /* %<typeid%> operator */
72   NIC_TYPEID,
73   /* non-constant compound literals */
74   NIC_NCC,
75   /* a function call */
76   NIC_FUNC_CALL,
77   /* an increment */
78   NIC_INC,
79   /* an decrement */
80   NIC_DEC,
81   /* an array reference */
82   NIC_ARRAY_REF,
83   /* %<->%> */
84   NIC_ARROW,
85   /* %<.%> */
86   NIC_POINT,
87   /* the address of a label */
88   NIC_ADDR_LABEL,
89   /* %<*%> */
90   NIC_STAR,
91   /* %<&%> */
92   NIC_ADDR,
93   /* %<++%> */
94   NIC_PREINCREMENT,
95   /* %<--%> */
96   NIC_PREDECREMENT,
97   /* %<new%> */
98   NIC_NEW,
99   /* %<delete%> */
100   NIC_DEL,
101   /* calls to overloaded operators */
102   NIC_OVERLOADED,
103   /* an assignment */
104   NIC_ASSIGNMENT,
105   /* a comma operator */
106   NIC_COMMA,
107   /* a call to a constructor */
108   NIC_CONSTRUCTOR,
109   /* a transaction expression */
110   NIC_TRANSACTION
111 } non_integral_constant;
112
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error {
115   /* NULL */
116   NLE_NULL,
117   /* is not a type */
118   NLE_TYPE,
119   /* is not a class or namespace */
120   NLE_CXX98,
121   /* is not a class, namespace, or enumeration */
122   NLE_NOT_CXX98
123 } name_lookup_error;
124
125 /* The various kinds of required token */
126 typedef enum required_token {
127   RT_NONE,
128   RT_SEMICOLON,  /* ';' */
129   RT_OPEN_PAREN, /* '(' */
130   RT_CLOSE_BRACE, /* '}' */
131   RT_OPEN_BRACE,  /* '{' */
132   RT_CLOSE_SQUARE, /* ']' */
133   RT_OPEN_SQUARE,  /* '[' */
134   RT_COMMA, /* ',' */
135   RT_SCOPE, /* '::' */
136   RT_LESS, /* '<' */
137   RT_GREATER, /* '>' */
138   RT_EQ, /* '=' */
139   RT_ELLIPSIS, /* '...' */
140   RT_MULT, /* '*' */
141   RT_COMPL, /* '~' */
142   RT_COLON, /* ':' */
143   RT_COLON_SCOPE, /* ':' or '::' */
144   RT_CLOSE_PAREN, /* ')' */
145   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146   RT_PRAGMA_EOL, /* end of line */
147   RT_NAME, /* identifier */
148
149   /* The type is CPP_KEYWORD */
150   RT_NEW, /* new */
151   RT_DELETE, /* delete */
152   RT_RETURN, /* return */
153   RT_WHILE, /* while */
154   RT_EXTERN, /* extern */
155   RT_STATIC_ASSERT, /* static_assert */
156   RT_DECLTYPE, /* decltype */
157   RT_OPERATOR, /* operator */
158   RT_CLASS, /* class */
159   RT_TEMPLATE, /* template */
160   RT_NAMESPACE, /* namespace */
161   RT_USING, /* using */
162   RT_ASM, /* asm */
163   RT_TRY, /* try */
164   RT_CATCH, /* catch */
165   RT_THROW, /* throw */
166   RT_LABEL, /* __label__ */
167   RT_AT_TRY, /* @try */
168   RT_AT_SYNCHRONIZED, /* @synchronized */
169   RT_AT_THROW, /* @throw */
170
171   RT_SELECT,  /* selection-statement */
172   RT_INTERATION, /* iteration-statement */
173   RT_JUMP, /* jump-statement */
174   RT_CLASS_KEY, /* class-key */
175   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178   RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 } required_token;
180
181 /* Prototypes.  */
182
183 static cp_lexer *cp_lexer_new_main
184   (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186   (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188   (cp_lexer *);
189 static int cp_lexer_saving_tokens
190   (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192   (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194   (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196   (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198   (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200   (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202   (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204   (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206   (cp_lexer *);
207 static void cp_lexer_purge_token
208   (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210   (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212   (cp_lexer *);
213 static void cp_lexer_commit_tokens
214   (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216   (cp_lexer *);
217 static void cp_lexer_print_token
218   (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220   (cp_lexer *);
221 static void cp_lexer_start_debugging
222   (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224   (cp_lexer *) ATTRIBUTE_UNUSED;
225
226 static cp_token_cache *cp_token_cache_new
227   (cp_token *, cp_token *);
228
229 static void cp_parser_initial_pragma
230   (cp_token *);
231
232 static tree cp_literal_operator_id
233   (const char *);
234
235 /* Manifest constants.  */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
238
239 /* Variables.  */
240
241 /* The stream to which debugging output should be written.  */
242 static FILE *cp_lexer_debug_stream;
243
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245    sizeof, typeof, or alignof.  */
246 int cp_unevaluated_operand;
247
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
250    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
251    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252    highlighted by surrounding it in [[ ]].  */
253
254 static void
255 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
256                       cp_token *start_token, unsigned num,
257                       cp_token *curr_token)
258 {
259   unsigned i, nprinted;
260   cp_token *token;
261   bool do_print;
262
263   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265   if (buffer == NULL)
266     return;
267
268   if (num == 0)
269     num = VEC_length (cp_token, buffer);
270
271   if (start_token == NULL)
272     start_token = VEC_address (cp_token, buffer);
273
274   if (start_token > VEC_address (cp_token, buffer))
275     {
276       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
277       fprintf (file, " ... ");
278     }
279
280   do_print = false;
281   nprinted = 0;
282   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
283     {
284       if (token == start_token)
285         do_print = true;
286
287       if (!do_print)
288         continue;
289
290       nprinted++;
291       if (token == curr_token)
292         fprintf (file, "[[");
293
294       cp_lexer_print_token (file, token);
295
296       if (token == curr_token)
297         fprintf (file, "]]");
298
299       switch (token->type)
300         {
301           case CPP_SEMICOLON:
302           case CPP_OPEN_BRACE:
303           case CPP_CLOSE_BRACE:
304           case CPP_EOF:
305             fputc ('\n', file);
306             break;
307
308           default:
309             fputc (' ', file);
310         }
311     }
312
313   if (i == num && i < VEC_length (cp_token, buffer))
314     {
315       fprintf (file, " ... ");
316       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
317                             VEC_length (cp_token, buffer) - 1));
318     }
319
320   fprintf (file, "\n");
321 }
322
323
324 /* Dump all tokens in BUFFER to stderr.  */
325
326 void
327 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
328 {
329   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
330 }
331
332
333 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
334    description for T.  */
335
336 static void
337 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
338 {
339   if (t)
340     {
341       fprintf (file, "%s: ", desc);
342       print_node_brief (file, "", t, 0);
343     }
344 }
345
346
347 /* Dump parser context C to FILE.  */
348
349 static void
350 cp_debug_print_context (FILE *file, cp_parser_context *c)
351 {
352   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
353   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
354   print_node_brief (file, "", c->object_type, 0);
355   fprintf (file, "}\n");
356 }
357
358
359 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
360
361 static void
362 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
363 {
364   unsigned i;
365   cp_parser_context *c;
366
367   fprintf (file, "Parsing context stack:\n");
368   for (i = 0, c = first; c; c = c->next, i++)
369     {
370       fprintf (file, "\t#%u: ", i);
371       cp_debug_print_context (file, c);
372     }
373 }
374
375
376 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
377
378 static void
379 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
380 {
381   if (flag)
382     fprintf (file, "%s: true\n", desc);
383 }
384
385
386 /* Print an unparsed function entry UF to FILE.  */
387
388 static void
389 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
390 {
391   unsigned i;
392   cp_default_arg_entry *default_arg_fn;
393   tree fn;
394
395   fprintf (file, "\tFunctions with default args:\n");
396   for (i = 0;
397        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
398                     default_arg_fn);
399        i++)
400     {
401       fprintf (file, "\t\tClass type: ");
402       print_node_brief (file, "", default_arg_fn->class_type, 0);
403       fprintf (file, "\t\tDeclaration: ");
404       print_node_brief (file, "", default_arg_fn->decl, 0);
405       fprintf (file, "\n");
406     }
407
408   fprintf (file, "\n\tFunctions with definitions that require "
409            "post-processing\n\t\t");
410   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
411     {
412       print_node_brief (file, "", fn, 0);
413       fprintf (file, " ");
414     }
415   fprintf (file, "\n");
416
417   fprintf (file, "\n\tNon-static data members with initializers that require "
418            "post-processing\n\t\t");
419   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
420     {
421       print_node_brief (file, "", fn, 0);
422       fprintf (file, " ");
423     }
424   fprintf (file, "\n");
425 }
426
427
428 /* Print the stack of unparsed member functions S to FILE.  */
429
430 static void
431 cp_debug_print_unparsed_queues (FILE *file,
432                                 VEC(cp_unparsed_functions_entry, gc) *s)
433 {
434   unsigned i;
435   cp_unparsed_functions_entry *uf;
436
437   fprintf (file, "Unparsed functions\n");
438   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
439     {
440       fprintf (file, "#%u:\n", i);
441       cp_debug_print_unparsed_function (file, uf);
442     }
443 }
444
445
446 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
447    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
448
449 static void
450 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
451 {
452   cp_token *next_token, *first_token, *start_token;
453
454   if (file == NULL)
455     file = stderr;
456
457   next_token = parser->lexer->next_token;
458   first_token = VEC_address (cp_token, parser->lexer->buffer);
459   start_token = (next_token > first_token + window_size / 2)
460                 ? next_token - window_size / 2
461                 : first_token;
462   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
463                         next_token);
464 }
465
466
467 /* Dump debugging information for the given PARSER.  If FILE is NULL,
468    the output is printed on stderr.  */
469
470 void
471 cp_debug_parser (FILE *file, cp_parser *parser)
472 {
473   const size_t window_size = 20;
474   cp_token *token;
475   expanded_location eloc;
476
477   if (file == NULL)
478     file = stderr;
479
480   fprintf (file, "Parser state\n\n");
481   fprintf (file, "Number of tokens: %u\n",
482            VEC_length (cp_token, parser->lexer->buffer));
483   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
484   cp_debug_print_tree_if_set (file, "Object scope",
485                                      parser->object_scope);
486   cp_debug_print_tree_if_set (file, "Qualifying scope",
487                                      parser->qualifying_scope);
488   cp_debug_print_context_stack (file, parser->context);
489   cp_debug_print_flag (file, "Allow GNU extensions",
490                               parser->allow_gnu_extensions_p);
491   cp_debug_print_flag (file, "'>' token is greater-than",
492                               parser->greater_than_is_operator_p);
493   cp_debug_print_flag (file, "Default args allowed in current "
494                               "parameter list", parser->default_arg_ok_p);
495   cp_debug_print_flag (file, "Parsing integral constant-expression",
496                               parser->integral_constant_expression_p);
497   cp_debug_print_flag (file, "Allow non-constant expression in current "
498                               "constant-expression",
499                               parser->allow_non_integral_constant_expression_p);
500   cp_debug_print_flag (file, "Seen non-constant expression",
501                               parser->non_integral_constant_expression_p);
502   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
503                               "current context",
504                               parser->local_variables_forbidden_p);
505   cp_debug_print_flag (file, "In unbraced linkage specification",
506                               parser->in_unbraced_linkage_specification_p);
507   cp_debug_print_flag (file, "Parsing a declarator",
508                               parser->in_declarator_p);
509   cp_debug_print_flag (file, "In template argument list",
510                               parser->in_template_argument_list_p);
511   cp_debug_print_flag (file, "Parsing an iteration statement",
512                               parser->in_statement & IN_ITERATION_STMT);
513   cp_debug_print_flag (file, "Parsing a switch statement",
514                               parser->in_statement & IN_SWITCH_STMT);
515   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
516                               parser->in_statement & IN_OMP_BLOCK);
517   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
518                               parser->in_statement & IN_OMP_FOR);
519   cp_debug_print_flag (file, "Parsing an if statement",
520                               parser->in_statement & IN_IF_STMT);
521   cp_debug_print_flag (file, "Parsing a type-id in an expression "
522                               "context", parser->in_type_id_in_expr_p);
523   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
524                               parser->implicit_extern_c);
525   cp_debug_print_flag (file, "String expressions should be translated "
526                               "to execution character set",
527                               parser->translate_strings_p);
528   cp_debug_print_flag (file, "Parsing function body outside of a "
529                               "local class", parser->in_function_body);
530   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
531                               parser->colon_corrects_to_scope_p);
532   if (parser->type_definition_forbidden_message)
533     fprintf (file, "Error message for forbidden type definitions: %s\n",
534              parser->type_definition_forbidden_message);
535   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
536   fprintf (file, "Number of class definitions in progress: %u\n",
537            parser->num_classes_being_defined);
538   fprintf (file, "Number of template parameter lists for the current "
539            "declaration: %u\n", parser->num_template_parameter_lists);
540   cp_debug_parser_tokens (file, parser, window_size);
541   token = parser->lexer->next_token;
542   fprintf (file, "Next token to parse:\n");
543   fprintf (file, "\tToken:  ");
544   cp_lexer_print_token (file, token);
545   eloc = expand_location (token->location);
546   fprintf (file, "\n\tFile:   %s\n", eloc.file);
547   fprintf (file, "\tLine:   %d\n", eloc.line);
548   fprintf (file, "\tColumn: %d\n", eloc.column);
549 }
550
551
552 /* Allocate memory for a new lexer object and return it.  */
553
554 static cp_lexer *
555 cp_lexer_alloc (void)
556 {
557   cp_lexer *lexer;
558
559   c_common_no_more_pch ();
560
561   /* Allocate the memory.  */
562   lexer = ggc_alloc_cleared_cp_lexer ();
563
564   /* Initially we are not debugging.  */
565   lexer->debugging_p = false;
566
567   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
568                                    CP_SAVED_TOKEN_STACK);
569
570   /* Create the buffer.  */
571   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
572
573   return lexer;
574 }
575
576
577 /* Create a new main C++ lexer, the lexer that gets tokens from the
578    preprocessor.  */
579
580 static cp_lexer *
581 cp_lexer_new_main (void)
582 {
583   cp_lexer *lexer;
584   cp_token token;
585
586   /* It's possible that parsing the first pragma will load a PCH file,
587      which is a GC collection point.  So we have to do that before
588      allocating any memory.  */
589   cp_parser_initial_pragma (&token);
590
591   lexer = cp_lexer_alloc ();
592
593   /* Put the first token in the buffer.  */
594   VEC_quick_push (cp_token, lexer->buffer, &token);
595
596   /* Get the remaining tokens from the preprocessor.  */
597   while (token.type != CPP_EOF)
598     {
599       cp_lexer_get_preprocessor_token (lexer, &token);
600       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
601     }
602
603   lexer->last_token = VEC_address (cp_token, lexer->buffer)
604                       + VEC_length (cp_token, lexer->buffer)
605                       - 1;
606   lexer->next_token = VEC_length (cp_token, lexer->buffer)
607                       ? VEC_address (cp_token, lexer->buffer)
608                       : &eof_token;
609
610   /* Subsequent preprocessor diagnostics should use compiler
611      diagnostic functions to get the compiler source location.  */
612   done_lexing = true;
613
614   gcc_assert (!lexer->next_token->purged_p);
615   return lexer;
616 }
617
618 /* Create a new lexer whose token stream is primed with the tokens in
619    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
620
621 static cp_lexer *
622 cp_lexer_new_from_tokens (cp_token_cache *cache)
623 {
624   cp_token *first = cache->first;
625   cp_token *last = cache->last;
626   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
627
628   /* We do not own the buffer.  */
629   lexer->buffer = NULL;
630   lexer->next_token = first == last ? &eof_token : first;
631   lexer->last_token = last;
632
633   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
634                                    CP_SAVED_TOKEN_STACK);
635
636   /* Initially we are not debugging.  */
637   lexer->debugging_p = false;
638
639   gcc_assert (!lexer->next_token->purged_p);
640   return lexer;
641 }
642
643 /* Frees all resources associated with LEXER.  */
644
645 static void
646 cp_lexer_destroy (cp_lexer *lexer)
647 {
648   VEC_free (cp_token, gc, lexer->buffer);
649   VEC_free (cp_token_position, heap, lexer->saved_tokens);
650   ggc_free (lexer);
651 }
652
653 /* Returns nonzero if debugging information should be output.  */
654
655 static inline bool
656 cp_lexer_debugging_p (cp_lexer *lexer)
657 {
658   return lexer->debugging_p;
659 }
660
661
662 static inline cp_token_position
663 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
664 {
665   gcc_assert (!previous_p || lexer->next_token != &eof_token);
666
667   return lexer->next_token - previous_p;
668 }
669
670 static inline cp_token *
671 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
672 {
673   return pos;
674 }
675
676 static inline void
677 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
678 {
679   lexer->next_token = cp_lexer_token_at (lexer, pos);
680 }
681
682 static inline cp_token_position
683 cp_lexer_previous_token_position (cp_lexer *lexer)
684 {
685   if (lexer->next_token == &eof_token)
686     return lexer->last_token - 1;
687   else
688     return cp_lexer_token_position (lexer, true);
689 }
690
691 static inline cp_token *
692 cp_lexer_previous_token (cp_lexer *lexer)
693 {
694   cp_token_position tp = cp_lexer_previous_token_position (lexer);
695
696   return cp_lexer_token_at (lexer, tp);
697 }
698
699 /* nonzero if we are presently saving tokens.  */
700
701 static inline int
702 cp_lexer_saving_tokens (const cp_lexer* lexer)
703 {
704   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
705 }
706
707 /* Store the next token from the preprocessor in *TOKEN.  Return true
708    if we reach EOF.  If LEXER is NULL, assume we are handling an
709    initial #pragma pch_preprocess, and thus want the lexer to return
710    processed strings.  */
711
712 static void
713 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
714 {
715   static int is_extern_c = 0;
716
717    /* Get a new token from the preprocessor.  */
718   token->type
719     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
720                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
721   token->keyword = RID_MAX;
722   token->pragma_kind = PRAGMA_NONE;
723   token->purged_p = false;
724
725   /* On some systems, some header files are surrounded by an
726      implicit extern "C" block.  Set a flag in the token if it
727      comes from such a header.  */
728   is_extern_c += pending_lang_change;
729   pending_lang_change = 0;
730   token->implicit_extern_c = is_extern_c > 0;
731
732   /* Check to see if this token is a keyword.  */
733   if (token->type == CPP_NAME)
734     {
735       if (C_IS_RESERVED_WORD (token->u.value))
736         {
737           /* Mark this token as a keyword.  */
738           token->type = CPP_KEYWORD;
739           /* Record which keyword.  */
740           token->keyword = C_RID_CODE (token->u.value);
741         }
742       else
743         {
744           if (warn_cxx0x_compat
745               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
746               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
747             {
748               /* Warn about the C++0x keyword (but still treat it as
749                  an identifier).  */
750               warning (OPT_Wc__0x_compat, 
751                        "identifier %qE is a keyword in C++11",
752                        token->u.value);
753
754               /* Clear out the C_RID_CODE so we don't warn about this
755                  particular identifier-turned-keyword again.  */
756               C_SET_RID_CODE (token->u.value, RID_MAX);
757             }
758
759           token->ambiguous_p = false;
760           token->keyword = RID_MAX;
761         }
762     }
763   else if (token->type == CPP_AT_NAME)
764     {
765       /* This only happens in Objective-C++; it must be a keyword.  */
766       token->type = CPP_KEYWORD;
767       switch (C_RID_CODE (token->u.value))
768         {
769           /* Replace 'class' with '@class', 'private' with '@private',
770              etc.  This prevents confusion with the C++ keyword
771              'class', and makes the tokens consistent with other
772              Objective-C 'AT' keywords.  For example '@class' is
773              reported as RID_AT_CLASS which is consistent with
774              '@synchronized', which is reported as
775              RID_AT_SYNCHRONIZED.
776           */
777         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
778         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
779         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
780         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
781         case RID_THROW:     token->keyword = RID_AT_THROW; break;
782         case RID_TRY:       token->keyword = RID_AT_TRY; break;
783         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
784         default:            token->keyword = C_RID_CODE (token->u.value);
785         }
786     }
787   else if (token->type == CPP_PRAGMA)
788     {
789       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
790       token->pragma_kind = ((enum pragma_kind)
791                             TREE_INT_CST_LOW (token->u.value));
792       token->u.value = NULL_TREE;
793     }
794 }
795
796 /* Update the globals input_location and the input file stack from TOKEN.  */
797 static inline void
798 cp_lexer_set_source_position_from_token (cp_token *token)
799 {
800   if (token->type != CPP_EOF)
801     {
802       input_location = token->location;
803     }
804 }
805
806 /* Return a pointer to the next token in the token stream, but do not
807    consume it.  */
808
809 static inline cp_token *
810 cp_lexer_peek_token (cp_lexer *lexer)
811 {
812   if (cp_lexer_debugging_p (lexer))
813     {
814       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
815       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
816       putc ('\n', cp_lexer_debug_stream);
817     }
818   return lexer->next_token;
819 }
820
821 /* Return true if the next token has the indicated TYPE.  */
822
823 static inline bool
824 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
825 {
826   return cp_lexer_peek_token (lexer)->type == type;
827 }
828
829 /* Return true if the next token does not have the indicated TYPE.  */
830
831 static inline bool
832 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
833 {
834   return !cp_lexer_next_token_is (lexer, type);
835 }
836
837 /* Return true if the next token is the indicated KEYWORD.  */
838
839 static inline bool
840 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
841 {
842   return cp_lexer_peek_token (lexer)->keyword == keyword;
843 }
844
845 /* Return true if the next token is not the indicated KEYWORD.  */
846
847 static inline bool
848 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
849 {
850   return cp_lexer_peek_token (lexer)->keyword != keyword;
851 }
852
853 /* Return true if the next token is a keyword for a decl-specifier.  */
854
855 static bool
856 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
857 {
858   cp_token *token;
859
860   token = cp_lexer_peek_token (lexer);
861   switch (token->keyword) 
862     {
863       /* auto specifier: storage-class-specifier in C++,
864          simple-type-specifier in C++0x.  */
865     case RID_AUTO:
866       /* Storage classes.  */
867     case RID_REGISTER:
868     case RID_STATIC:
869     case RID_EXTERN:
870     case RID_MUTABLE:
871     case RID_THREAD:
872       /* Elaborated type specifiers.  */
873     case RID_ENUM:
874     case RID_CLASS:
875     case RID_STRUCT:
876     case RID_UNION:
877     case RID_TYPENAME:
878       /* Simple type specifiers.  */
879     case RID_CHAR:
880     case RID_CHAR16:
881     case RID_CHAR32:
882     case RID_WCHAR:
883     case RID_BOOL:
884     case RID_SHORT:
885     case RID_INT:
886     case RID_LONG:
887     case RID_INT128:
888     case RID_SIGNED:
889     case RID_UNSIGNED:
890     case RID_FLOAT:
891     case RID_DOUBLE:
892     case RID_VOID:
893       /* GNU extensions.  */ 
894     case RID_ATTRIBUTE:
895     case RID_TYPEOF:
896       /* C++0x extensions.  */
897     case RID_DECLTYPE:
898     case RID_UNDERLYING_TYPE:
899       return true;
900
901     default:
902       return false;
903     }
904 }
905
906 /* Returns TRUE iff the token T begins a decltype type.  */
907
908 static bool
909 token_is_decltype (cp_token *t)
910 {
911   return (t->keyword == RID_DECLTYPE
912           || t->type == CPP_DECLTYPE);
913 }
914
915 /* Returns TRUE iff the next token begins a decltype type.  */
916
917 static bool
918 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
919 {
920   cp_token *t = cp_lexer_peek_token (lexer);
921   return token_is_decltype (t);
922 }
923
924 /* Return a pointer to the Nth token in the token stream.  If N is 1,
925    then this is precisely equivalent to cp_lexer_peek_token (except
926    that it is not inline).  One would like to disallow that case, but
927    there is one case (cp_parser_nth_token_starts_template_id) where
928    the caller passes a variable for N and it might be 1.  */
929
930 static cp_token *
931 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
932 {
933   cp_token *token;
934
935   /* N is 1-based, not zero-based.  */
936   gcc_assert (n > 0);
937
938   if (cp_lexer_debugging_p (lexer))
939     fprintf (cp_lexer_debug_stream,
940              "cp_lexer: peeking ahead %ld at token: ", (long)n);
941
942   --n;
943   token = lexer->next_token;
944   gcc_assert (!n || token != &eof_token);
945   while (n != 0)
946     {
947       ++token;
948       if (token == lexer->last_token)
949         {
950           token = &eof_token;
951           break;
952         }
953
954       if (!token->purged_p)
955         --n;
956     }
957
958   if (cp_lexer_debugging_p (lexer))
959     {
960       cp_lexer_print_token (cp_lexer_debug_stream, token);
961       putc ('\n', cp_lexer_debug_stream);
962     }
963
964   return token;
965 }
966
967 /* Return the next token, and advance the lexer's next_token pointer
968    to point to the next non-purged token.  */
969
970 static cp_token *
971 cp_lexer_consume_token (cp_lexer* lexer)
972 {
973   cp_token *token = lexer->next_token;
974
975   gcc_assert (token != &eof_token);
976   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
977
978   do
979     {
980       lexer->next_token++;
981       if (lexer->next_token == lexer->last_token)
982         {
983           lexer->next_token = &eof_token;
984           break;
985         }
986
987     }
988   while (lexer->next_token->purged_p);
989
990   cp_lexer_set_source_position_from_token (token);
991
992   /* Provide debugging output.  */
993   if (cp_lexer_debugging_p (lexer))
994     {
995       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
996       cp_lexer_print_token (cp_lexer_debug_stream, token);
997       putc ('\n', cp_lexer_debug_stream);
998     }
999
1000   return token;
1001 }
1002
1003 /* Permanently remove the next token from the token stream, and
1004    advance the next_token pointer to refer to the next non-purged
1005    token.  */
1006
1007 static void
1008 cp_lexer_purge_token (cp_lexer *lexer)
1009 {
1010   cp_token *tok = lexer->next_token;
1011
1012   gcc_assert (tok != &eof_token);
1013   tok->purged_p = true;
1014   tok->location = UNKNOWN_LOCATION;
1015   tok->u.value = NULL_TREE;
1016   tok->keyword = RID_MAX;
1017
1018   do
1019     {
1020       tok++;
1021       if (tok == lexer->last_token)
1022         {
1023           tok = &eof_token;
1024           break;
1025         }
1026     }
1027   while (tok->purged_p);
1028   lexer->next_token = tok;
1029 }
1030
1031 /* Permanently remove all tokens after TOK, up to, but not
1032    including, the token that will be returned next by
1033    cp_lexer_peek_token.  */
1034
1035 static void
1036 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1037 {
1038   cp_token *peek = lexer->next_token;
1039
1040   if (peek == &eof_token)
1041     peek = lexer->last_token;
1042
1043   gcc_assert (tok < peek);
1044
1045   for ( tok += 1; tok != peek; tok += 1)
1046     {
1047       tok->purged_p = true;
1048       tok->location = UNKNOWN_LOCATION;
1049       tok->u.value = NULL_TREE;
1050       tok->keyword = RID_MAX;
1051     }
1052 }
1053
1054 /* Begin saving tokens.  All tokens consumed after this point will be
1055    preserved.  */
1056
1057 static void
1058 cp_lexer_save_tokens (cp_lexer* lexer)
1059 {
1060   /* Provide debugging output.  */
1061   if (cp_lexer_debugging_p (lexer))
1062     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1063
1064   VEC_safe_push (cp_token_position, heap,
1065                  lexer->saved_tokens, lexer->next_token);
1066 }
1067
1068 /* Commit to the portion of the token stream most recently saved.  */
1069
1070 static void
1071 cp_lexer_commit_tokens (cp_lexer* lexer)
1072 {
1073   /* Provide debugging output.  */
1074   if (cp_lexer_debugging_p (lexer))
1075     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1076
1077   VEC_pop (cp_token_position, lexer->saved_tokens);
1078 }
1079
1080 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1081    to the token stream.  Stop saving tokens.  */
1082
1083 static void
1084 cp_lexer_rollback_tokens (cp_lexer* lexer)
1085 {
1086   /* Provide debugging output.  */
1087   if (cp_lexer_debugging_p (lexer))
1088     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1089
1090   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1091 }
1092
1093 /* Print a representation of the TOKEN on the STREAM.  */
1094
1095 static void
1096 cp_lexer_print_token (FILE * stream, cp_token *token)
1097 {
1098   /* We don't use cpp_type2name here because the parser defines
1099      a few tokens of its own.  */
1100   static const char *const token_names[] = {
1101     /* cpplib-defined token types */
1102 #define OP(e, s) #e,
1103 #define TK(e, s) #e,
1104     TTYPE_TABLE
1105 #undef OP
1106 #undef TK
1107     /* C++ parser token types - see "Manifest constants", above.  */
1108     "KEYWORD",
1109     "TEMPLATE_ID",
1110     "NESTED_NAME_SPECIFIER",
1111   };
1112
1113   /* For some tokens, print the associated data.  */
1114   switch (token->type)
1115     {
1116     case CPP_KEYWORD:
1117       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1118          For example, `struct' is mapped to an INTEGER_CST.  */
1119       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1120         break;
1121       /* else fall through */
1122     case CPP_NAME:
1123       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1124       break;
1125
1126     case CPP_STRING:
1127     case CPP_STRING16:
1128     case CPP_STRING32:
1129     case CPP_WSTRING:
1130     case CPP_UTF8STRING:
1131       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1132       break;
1133
1134     case CPP_NUMBER:
1135       print_generic_expr (stream, token->u.value, 0);
1136       break;
1137
1138     default:
1139       /* If we have a name for the token, print it out.  Otherwise, we
1140          simply give the numeric code.  */
1141       if (token->type < ARRAY_SIZE(token_names))
1142         fputs (token_names[token->type], stream);
1143       else
1144         fprintf (stream, "[%d]", token->type);
1145       break;
1146     }
1147 }
1148
1149 /* Start emitting debugging information.  */
1150
1151 static void
1152 cp_lexer_start_debugging (cp_lexer* lexer)
1153 {
1154   lexer->debugging_p = true;
1155   cp_lexer_debug_stream = stderr;
1156 }
1157
1158 /* Stop emitting debugging information.  */
1159
1160 static void
1161 cp_lexer_stop_debugging (cp_lexer* lexer)
1162 {
1163   lexer->debugging_p = false;
1164   cp_lexer_debug_stream = NULL;
1165 }
1166
1167 /* Create a new cp_token_cache, representing a range of tokens.  */
1168
1169 static cp_token_cache *
1170 cp_token_cache_new (cp_token *first, cp_token *last)
1171 {
1172   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1173   cache->first = first;
1174   cache->last = last;
1175   return cache;
1176 }
1177
1178 \f
1179 /* Decl-specifiers.  */
1180
1181 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1182
1183 static void
1184 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1185 {
1186   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1187 }
1188
1189 /* Declarators.  */
1190
1191 /* Nothing other than the parser should be creating declarators;
1192    declarators are a semi-syntactic representation of C++ entities.
1193    Other parts of the front end that need to create entities (like
1194    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1195
1196 static cp_declarator *make_call_declarator
1197   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1198 static cp_declarator *make_array_declarator
1199   (cp_declarator *, tree);
1200 static cp_declarator *make_pointer_declarator
1201   (cp_cv_quals, cp_declarator *);
1202 static cp_declarator *make_reference_declarator
1203   (cp_cv_quals, cp_declarator *, bool);
1204 static cp_parameter_declarator *make_parameter_declarator
1205   (cp_decl_specifier_seq *, cp_declarator *, tree);
1206 static cp_declarator *make_ptrmem_declarator
1207   (cp_cv_quals, tree, cp_declarator *);
1208
1209 /* An erroneous declarator.  */
1210 static cp_declarator *cp_error_declarator;
1211
1212 /* The obstack on which declarators and related data structures are
1213    allocated.  */
1214 static struct obstack declarator_obstack;
1215
1216 /* Alloc BYTES from the declarator memory pool.  */
1217
1218 static inline void *
1219 alloc_declarator (size_t bytes)
1220 {
1221   return obstack_alloc (&declarator_obstack, bytes);
1222 }
1223
1224 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1225    common to all declarators.  */
1226
1227 static cp_declarator *
1228 make_declarator (cp_declarator_kind kind)
1229 {
1230   cp_declarator *declarator;
1231
1232   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1233   declarator->kind = kind;
1234   declarator->attributes = NULL_TREE;
1235   declarator->declarator = NULL;
1236   declarator->parameter_pack_p = false;
1237   declarator->id_loc = UNKNOWN_LOCATION;
1238
1239   return declarator;
1240 }
1241
1242 /* Make a declarator for a generalized identifier.  If
1243    QUALIFYING_SCOPE is non-NULL, the identifier is
1244    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1246    is, if any.   */
1247
1248 static cp_declarator *
1249 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1250                     special_function_kind sfk)
1251 {
1252   cp_declarator *declarator;
1253
1254   /* It is valid to write:
1255
1256        class C { void f(); };
1257        typedef C D;
1258        void D::f();
1259
1260      The standard is not clear about whether `typedef const C D' is
1261      legal; as of 2002-09-15 the committee is considering that
1262      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1263      well.  */
1264   if (qualifying_scope && TYPE_P (qualifying_scope))
1265     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1266
1267   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1268               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1269               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1270
1271   declarator = make_declarator (cdk_id);
1272   declarator->u.id.qualifying_scope = qualifying_scope;
1273   declarator->u.id.unqualified_name = unqualified_name;
1274   declarator->u.id.sfk = sfk;
1275   
1276   return declarator;
1277 }
1278
1279 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1280    of modifiers such as const or volatile to apply to the pointer
1281    type, represented as identifiers.  */
1282
1283 cp_declarator *
1284 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1285 {
1286   cp_declarator *declarator;
1287
1288   declarator = make_declarator (cdk_pointer);
1289   declarator->declarator = target;
1290   declarator->u.pointer.qualifiers = cv_qualifiers;
1291   declarator->u.pointer.class_type = NULL_TREE;
1292   if (target)
1293     {
1294       declarator->id_loc = target->id_loc;
1295       declarator->parameter_pack_p = target->parameter_pack_p;
1296       target->parameter_pack_p = false;
1297     }
1298   else
1299     declarator->parameter_pack_p = false;
1300
1301   return declarator;
1302 }
1303
1304 /* Like make_pointer_declarator -- but for references.  */
1305
1306 cp_declarator *
1307 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1308                            bool rvalue_ref)
1309 {
1310   cp_declarator *declarator;
1311
1312   declarator = make_declarator (cdk_reference);
1313   declarator->declarator = target;
1314   declarator->u.reference.qualifiers = cv_qualifiers;
1315   declarator->u.reference.rvalue_ref = rvalue_ref;
1316   if (target)
1317     {
1318       declarator->id_loc = target->id_loc;
1319       declarator->parameter_pack_p = target->parameter_pack_p;
1320       target->parameter_pack_p = false;
1321     }
1322   else
1323     declarator->parameter_pack_p = false;
1324
1325   return declarator;
1326 }
1327
1328 /* Like make_pointer_declarator -- but for a pointer to a non-static
1329    member of CLASS_TYPE.  */
1330
1331 cp_declarator *
1332 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1333                         cp_declarator *pointee)
1334 {
1335   cp_declarator *declarator;
1336
1337   declarator = make_declarator (cdk_ptrmem);
1338   declarator->declarator = pointee;
1339   declarator->u.pointer.qualifiers = cv_qualifiers;
1340   declarator->u.pointer.class_type = class_type;
1341
1342   if (pointee)
1343     {
1344       declarator->parameter_pack_p = pointee->parameter_pack_p;
1345       pointee->parameter_pack_p = false;
1346     }
1347   else
1348     declarator->parameter_pack_p = false;
1349
1350   return declarator;
1351 }
1352
1353 /* Make a declarator for the function given by TARGET, with the
1354    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1355    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1356    indicates what exceptions can be thrown.  */
1357
1358 cp_declarator *
1359 make_call_declarator (cp_declarator *target,
1360                       tree parms,
1361                       cp_cv_quals cv_qualifiers,
1362                       cp_virt_specifiers virt_specifiers,
1363                       tree exception_specification,
1364                       tree late_return_type)
1365 {
1366   cp_declarator *declarator;
1367
1368   declarator = make_declarator (cdk_function);
1369   declarator->declarator = target;
1370   declarator->u.function.parameters = parms;
1371   declarator->u.function.qualifiers = cv_qualifiers;
1372   declarator->u.function.virt_specifiers = virt_specifiers;
1373   declarator->u.function.exception_specification = exception_specification;
1374   declarator->u.function.late_return_type = late_return_type;
1375   if (target)
1376     {
1377       declarator->id_loc = target->id_loc;
1378       declarator->parameter_pack_p = target->parameter_pack_p;
1379       target->parameter_pack_p = false;
1380     }
1381   else
1382     declarator->parameter_pack_p = false;
1383
1384   return declarator;
1385 }
1386
1387 /* Make a declarator for an array of BOUNDS elements, each of which is
1388    defined by ELEMENT.  */
1389
1390 cp_declarator *
1391 make_array_declarator (cp_declarator *element, tree bounds)
1392 {
1393   cp_declarator *declarator;
1394
1395   declarator = make_declarator (cdk_array);
1396   declarator->declarator = element;
1397   declarator->u.array.bounds = bounds;
1398   if (element)
1399     {
1400       declarator->id_loc = element->id_loc;
1401       declarator->parameter_pack_p = element->parameter_pack_p;
1402       element->parameter_pack_p = false;
1403     }
1404   else
1405     declarator->parameter_pack_p = false;
1406
1407   return declarator;
1408 }
1409
1410 /* Determine whether the declarator we've seen so far can be a
1411    parameter pack, when followed by an ellipsis.  */
1412 static bool 
1413 declarator_can_be_parameter_pack (cp_declarator *declarator)
1414 {
1415   /* Search for a declarator name, or any other declarator that goes
1416      after the point where the ellipsis could appear in a parameter
1417      pack. If we find any of these, then this declarator can not be
1418      made into a parameter pack.  */
1419   bool found = false;
1420   while (declarator && !found)
1421     {
1422       switch ((int)declarator->kind)
1423         {
1424         case cdk_id:
1425         case cdk_array:
1426           found = true;
1427           break;
1428
1429         case cdk_error:
1430           return true;
1431
1432         default:
1433           declarator = declarator->declarator;
1434           break;
1435         }
1436     }
1437
1438   return !found;
1439 }
1440
1441 cp_parameter_declarator *no_parameters;
1442
1443 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1444    DECLARATOR and DEFAULT_ARGUMENT.  */
1445
1446 cp_parameter_declarator *
1447 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1448                            cp_declarator *declarator,
1449                            tree default_argument)
1450 {
1451   cp_parameter_declarator *parameter;
1452
1453   parameter = ((cp_parameter_declarator *)
1454                alloc_declarator (sizeof (cp_parameter_declarator)));
1455   parameter->next = NULL;
1456   if (decl_specifiers)
1457     parameter->decl_specifiers = *decl_specifiers;
1458   else
1459     clear_decl_specs (&parameter->decl_specifiers);
1460   parameter->declarator = declarator;
1461   parameter->default_argument = default_argument;
1462   parameter->ellipsis_p = false;
1463
1464   return parameter;
1465 }
1466
1467 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1468
1469 static bool
1470 function_declarator_p (const cp_declarator *declarator)
1471 {
1472   while (declarator)
1473     {
1474       if (declarator->kind == cdk_function
1475           && declarator->declarator->kind == cdk_id)
1476         return true;
1477       if (declarator->kind == cdk_id
1478           || declarator->kind == cdk_error)
1479         return false;
1480       declarator = declarator->declarator;
1481     }
1482   return false;
1483 }
1484  
1485 /* The parser.  */
1486
1487 /* Overview
1488    --------
1489
1490    A cp_parser parses the token stream as specified by the C++
1491    grammar.  Its job is purely parsing, not semantic analysis.  For
1492    example, the parser breaks the token stream into declarators,
1493    expressions, statements, and other similar syntactic constructs.
1494    It does not check that the types of the expressions on either side
1495    of an assignment-statement are compatible, or that a function is
1496    not declared with a parameter of type `void'.
1497
1498    The parser invokes routines elsewhere in the compiler to perform
1499    semantic analysis and to build up the abstract syntax tree for the
1500    code processed.
1501
1502    The parser (and the template instantiation code, which is, in a
1503    way, a close relative of parsing) are the only parts of the
1504    compiler that should be calling push_scope and pop_scope, or
1505    related functions.  The parser (and template instantiation code)
1506    keeps track of what scope is presently active; everything else
1507    should simply honor that.  (The code that generates static
1508    initializers may also need to set the scope, in order to check
1509    access control correctly when emitting the initializers.)
1510
1511    Methodology
1512    -----------
1513
1514    The parser is of the standard recursive-descent variety.  Upcoming
1515    tokens in the token stream are examined in order to determine which
1516    production to use when parsing a non-terminal.  Some C++ constructs
1517    require arbitrary look ahead to disambiguate.  For example, it is
1518    impossible, in the general case, to tell whether a statement is an
1519    expression or declaration without scanning the entire statement.
1520    Therefore, the parser is capable of "parsing tentatively."  When the
1521    parser is not sure what construct comes next, it enters this mode.
1522    Then, while we attempt to parse the construct, the parser queues up
1523    error messages, rather than issuing them immediately, and saves the
1524    tokens it consumes.  If the construct is parsed successfully, the
1525    parser "commits", i.e., it issues any queued error messages and
1526    the tokens that were being preserved are permanently discarded.
1527    If, however, the construct is not parsed successfully, the parser
1528    rolls back its state completely so that it can resume parsing using
1529    a different alternative.
1530
1531    Future Improvements
1532    -------------------
1533
1534    The performance of the parser could probably be improved substantially.
1535    We could often eliminate the need to parse tentatively by looking ahead
1536    a little bit.  In some places, this approach might not entirely eliminate
1537    the need to parse tentatively, but it might still speed up the average
1538    case.  */
1539
1540 /* Flags that are passed to some parsing functions.  These values can
1541    be bitwise-ored together.  */
1542
1543 enum
1544 {
1545   /* No flags.  */
1546   CP_PARSER_FLAGS_NONE = 0x0,
1547   /* The construct is optional.  If it is not present, then no error
1548      should be issued.  */
1549   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1550   /* When parsing a type-specifier, treat user-defined type-names
1551      as non-type identifiers.  */
1552   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1553   /* When parsing a type-specifier, do not try to parse a class-specifier
1554      or enum-specifier.  */
1555   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1556   /* When parsing a decl-specifier-seq, only allow type-specifier or
1557      constexpr.  */
1558   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1559 };
1560
1561 /* This type is used for parameters and variables which hold
1562    combinations of the above flags.  */
1563 typedef int cp_parser_flags;
1564
1565 /* The different kinds of declarators we want to parse.  */
1566
1567 typedef enum cp_parser_declarator_kind
1568 {
1569   /* We want an abstract declarator.  */
1570   CP_PARSER_DECLARATOR_ABSTRACT,
1571   /* We want a named declarator.  */
1572   CP_PARSER_DECLARATOR_NAMED,
1573   /* We don't mind, but the name must be an unqualified-id.  */
1574   CP_PARSER_DECLARATOR_EITHER
1575 } cp_parser_declarator_kind;
1576
1577 /* The precedence values used to parse binary expressions.  The minimum value
1578    of PREC must be 1, because zero is reserved to quickly discriminate
1579    binary operators from other tokens.  */
1580
1581 enum cp_parser_prec
1582 {
1583   PREC_NOT_OPERATOR,
1584   PREC_LOGICAL_OR_EXPRESSION,
1585   PREC_LOGICAL_AND_EXPRESSION,
1586   PREC_INCLUSIVE_OR_EXPRESSION,
1587   PREC_EXCLUSIVE_OR_EXPRESSION,
1588   PREC_AND_EXPRESSION,
1589   PREC_EQUALITY_EXPRESSION,
1590   PREC_RELATIONAL_EXPRESSION,
1591   PREC_SHIFT_EXPRESSION,
1592   PREC_ADDITIVE_EXPRESSION,
1593   PREC_MULTIPLICATIVE_EXPRESSION,
1594   PREC_PM_EXPRESSION,
1595   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1596 };
1597
1598 /* A mapping from a token type to a corresponding tree node type, with a
1599    precedence value.  */
1600
1601 typedef struct cp_parser_binary_operations_map_node
1602 {
1603   /* The token type.  */
1604   enum cpp_ttype token_type;
1605   /* The corresponding tree code.  */
1606   enum tree_code tree_type;
1607   /* The precedence of this operator.  */
1608   enum cp_parser_prec prec;
1609 } cp_parser_binary_operations_map_node;
1610
1611 typedef struct cp_parser_expression_stack_entry
1612 {
1613   /* Left hand side of the binary operation we are currently
1614      parsing.  */
1615   tree lhs;
1616   /* Original tree code for left hand side, if it was a binary
1617      expression itself (used for -Wparentheses).  */
1618   enum tree_code lhs_type;
1619   /* Tree code for the binary operation we are parsing.  */
1620   enum tree_code tree_type;
1621   /* Precedence of the binary operation we are parsing.  */
1622   enum cp_parser_prec prec;
1623   /* Location of the binary operation we are parsing.  */
1624   location_t loc;
1625 } cp_parser_expression_stack_entry;
1626
1627 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1628    entries because precedence levels on the stack are monotonically
1629    increasing.  */
1630 typedef struct cp_parser_expression_stack_entry
1631   cp_parser_expression_stack[NUM_PREC_VALUES];
1632
1633 /* Prototypes.  */
1634
1635 /* Constructors and destructors.  */
1636
1637 static cp_parser_context *cp_parser_context_new
1638   (cp_parser_context *);
1639
1640 /* Class variables.  */
1641
1642 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1643
1644 /* The operator-precedence table used by cp_parser_binary_expression.
1645    Transformed into an associative array (binops_by_token) by
1646    cp_parser_new.  */
1647
1648 static const cp_parser_binary_operations_map_node binops[] = {
1649   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1650   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1651
1652   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1655
1656   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1658
1659   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1661
1662   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1664   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1666
1667   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1668   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1669
1670   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1671
1672   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1673
1674   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1675
1676   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1677
1678   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1679 };
1680
1681 /* The same as binops, but initialized by cp_parser_new so that
1682    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1683    for speed.  */
1684 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1685
1686 /* Constructors and destructors.  */
1687
1688 /* Construct a new context.  The context below this one on the stack
1689    is given by NEXT.  */
1690
1691 static cp_parser_context *
1692 cp_parser_context_new (cp_parser_context* next)
1693 {
1694   cp_parser_context *context;
1695
1696   /* Allocate the storage.  */
1697   if (cp_parser_context_free_list != NULL)
1698     {
1699       /* Pull the first entry from the free list.  */
1700       context = cp_parser_context_free_list;
1701       cp_parser_context_free_list = context->next;
1702       memset (context, 0, sizeof (*context));
1703     }
1704   else
1705     context = ggc_alloc_cleared_cp_parser_context ();
1706
1707   /* No errors have occurred yet in this context.  */
1708   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1709   /* If this is not the bottommost context, copy information that we
1710      need from the previous context.  */
1711   if (next)
1712     {
1713       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1714          expression, then we are parsing one in this context, too.  */
1715       context->object_type = next->object_type;
1716       /* Thread the stack.  */
1717       context->next = next;
1718     }
1719
1720   return context;
1721 }
1722
1723 /* Managing the unparsed function queues.  */
1724
1725 #define unparsed_funs_with_default_args \
1726   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1727 #define unparsed_funs_with_definitions \
1728   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1729 #define unparsed_nsdmis \
1730   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1731
1732 static void
1733 push_unparsed_function_queues (cp_parser *parser)
1734 {
1735   VEC_safe_push (cp_unparsed_functions_entry, gc,
1736                  parser->unparsed_queues, NULL);
1737   unparsed_funs_with_default_args = NULL;
1738   unparsed_funs_with_definitions = make_tree_vector ();
1739   unparsed_nsdmis = NULL;
1740 }
1741
1742 static void
1743 pop_unparsed_function_queues (cp_parser *parser)
1744 {
1745   release_tree_vector (unparsed_funs_with_definitions);
1746   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1747 }
1748
1749 /* Prototypes.  */
1750
1751 /* Constructors and destructors.  */
1752
1753 static cp_parser *cp_parser_new
1754   (void);
1755
1756 /* Routines to parse various constructs.
1757
1758    Those that return `tree' will return the error_mark_node (rather
1759    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1760    Sometimes, they will return an ordinary node if error-recovery was
1761    attempted, even though a parse error occurred.  So, to check
1762    whether or not a parse error occurred, you should always use
1763    cp_parser_error_occurred.  If the construct is optional (indicated
1764    either by an `_opt' in the name of the function that does the
1765    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1766    the construct is not present.  */
1767
1768 /* Lexical conventions [gram.lex]  */
1769
1770 static tree cp_parser_identifier
1771   (cp_parser *);
1772 static tree cp_parser_string_literal
1773   (cp_parser *, bool, bool);
1774 static tree cp_parser_userdef_char_literal
1775   (cp_parser *);
1776 static tree cp_parser_userdef_string_literal
1777   (cp_token *);
1778 static tree cp_parser_userdef_numeric_literal
1779   (cp_parser *);
1780
1781 /* Basic concepts [gram.basic]  */
1782
1783 static bool cp_parser_translation_unit
1784   (cp_parser *);
1785
1786 /* Expressions [gram.expr]  */
1787
1788 static tree cp_parser_primary_expression
1789   (cp_parser *, bool, bool, bool, cp_id_kind *);
1790 static tree cp_parser_id_expression
1791   (cp_parser *, bool, bool, bool *, bool, bool);
1792 static tree cp_parser_unqualified_id
1793   (cp_parser *, bool, bool, bool, bool);
1794 static tree cp_parser_nested_name_specifier_opt
1795   (cp_parser *, bool, bool, bool, bool);
1796 static tree cp_parser_nested_name_specifier
1797   (cp_parser *, bool, bool, bool, bool);
1798 static tree cp_parser_qualifying_entity
1799   (cp_parser *, bool, bool, bool, bool, bool);
1800 static tree cp_parser_postfix_expression
1801   (cp_parser *, bool, bool, bool, cp_id_kind *);
1802 static tree cp_parser_postfix_open_square_expression
1803   (cp_parser *, tree, bool);
1804 static tree cp_parser_postfix_dot_deref_expression
1805   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1806 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1807   (cp_parser *, int, bool, bool, bool *);
1808 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1809 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1810 static void cp_parser_pseudo_destructor_name
1811   (cp_parser *, tree *, tree *);
1812 static tree cp_parser_unary_expression
1813   (cp_parser *, bool, bool, cp_id_kind *);
1814 static enum tree_code cp_parser_unary_operator
1815   (cp_token *);
1816 static tree cp_parser_new_expression
1817   (cp_parser *);
1818 static VEC(tree,gc) *cp_parser_new_placement
1819   (cp_parser *);
1820 static tree cp_parser_new_type_id
1821   (cp_parser *, tree *);
1822 static cp_declarator *cp_parser_new_declarator_opt
1823   (cp_parser *);
1824 static cp_declarator *cp_parser_direct_new_declarator
1825   (cp_parser *);
1826 static VEC(tree,gc) *cp_parser_new_initializer
1827   (cp_parser *);
1828 static tree cp_parser_delete_expression
1829   (cp_parser *);
1830 static tree cp_parser_cast_expression
1831   (cp_parser *, bool, bool, cp_id_kind *);
1832 static tree cp_parser_binary_expression
1833   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1834 static tree cp_parser_question_colon_clause
1835   (cp_parser *, tree);
1836 static tree cp_parser_assignment_expression
1837   (cp_parser *, bool, cp_id_kind *);
1838 static enum tree_code cp_parser_assignment_operator_opt
1839   (cp_parser *);
1840 static tree cp_parser_expression
1841   (cp_parser *, bool, cp_id_kind *);
1842 static tree cp_parser_constant_expression
1843   (cp_parser *, bool, bool *);
1844 static tree cp_parser_builtin_offsetof
1845   (cp_parser *);
1846 static tree cp_parser_lambda_expression
1847   (cp_parser *);
1848 static void cp_parser_lambda_introducer
1849   (cp_parser *, tree);
1850 static bool cp_parser_lambda_declarator_opt
1851   (cp_parser *, tree);
1852 static void cp_parser_lambda_body
1853   (cp_parser *, tree);
1854
1855 /* Statements [gram.stmt.stmt]  */
1856
1857 static void cp_parser_statement
1858   (cp_parser *, tree, bool, bool *);
1859 static void cp_parser_label_for_labeled_statement
1860   (cp_parser *);
1861 static tree cp_parser_expression_statement
1862   (cp_parser *, tree);
1863 static tree cp_parser_compound_statement
1864   (cp_parser *, tree, bool, bool);
1865 static void cp_parser_statement_seq_opt
1866   (cp_parser *, tree);
1867 static tree cp_parser_selection_statement
1868   (cp_parser *, bool *);
1869 static tree cp_parser_condition
1870   (cp_parser *);
1871 static tree cp_parser_iteration_statement
1872   (cp_parser *);
1873 static bool cp_parser_for_init_statement
1874   (cp_parser *, tree *decl);
1875 static tree cp_parser_for
1876   (cp_parser *);
1877 static tree cp_parser_c_for
1878   (cp_parser *, tree, tree);
1879 static tree cp_parser_range_for
1880   (cp_parser *, tree, tree, tree);
1881 static void do_range_for_auto_deduction
1882   (tree, tree);
1883 static tree cp_parser_perform_range_for_lookup
1884   (tree, tree *, tree *);
1885 static tree cp_parser_range_for_member_function
1886   (tree, tree);
1887 static tree cp_parser_jump_statement
1888   (cp_parser *);
1889 static void cp_parser_declaration_statement
1890   (cp_parser *);
1891
1892 static tree cp_parser_implicitly_scoped_statement
1893   (cp_parser *, bool *);
1894 static void cp_parser_already_scoped_statement
1895   (cp_parser *);
1896
1897 /* Declarations [gram.dcl.dcl] */
1898
1899 static void cp_parser_declaration_seq_opt
1900   (cp_parser *);
1901 static void cp_parser_declaration
1902   (cp_parser *);
1903 static void cp_parser_block_declaration
1904   (cp_parser *, bool);
1905 static void cp_parser_simple_declaration
1906   (cp_parser *, bool, tree *);
1907 static void cp_parser_decl_specifier_seq
1908   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1909 static tree cp_parser_storage_class_specifier_opt
1910   (cp_parser *);
1911 static tree cp_parser_function_specifier_opt
1912   (cp_parser *, cp_decl_specifier_seq *);
1913 static tree cp_parser_type_specifier
1914   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1915    int *, bool *);
1916 static tree cp_parser_simple_type_specifier
1917   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1918 static tree cp_parser_type_name
1919   (cp_parser *);
1920 static tree cp_parser_nonclass_name 
1921   (cp_parser* parser);
1922 static tree cp_parser_elaborated_type_specifier
1923   (cp_parser *, bool, bool);
1924 static tree cp_parser_enum_specifier
1925   (cp_parser *);
1926 static void cp_parser_enumerator_list
1927   (cp_parser *, tree);
1928 static void cp_parser_enumerator_definition
1929   (cp_parser *, tree);
1930 static tree cp_parser_namespace_name
1931   (cp_parser *);
1932 static void cp_parser_namespace_definition
1933   (cp_parser *);
1934 static void cp_parser_namespace_body
1935   (cp_parser *);
1936 static tree cp_parser_qualified_namespace_specifier
1937   (cp_parser *);
1938 static void cp_parser_namespace_alias_definition
1939   (cp_parser *);
1940 static bool cp_parser_using_declaration
1941   (cp_parser *, bool);
1942 static void cp_parser_using_directive
1943   (cp_parser *);
1944 static tree cp_parser_alias_declaration
1945   (cp_parser *);
1946 static void cp_parser_asm_definition
1947   (cp_parser *);
1948 static void cp_parser_linkage_specification
1949   (cp_parser *);
1950 static void cp_parser_static_assert
1951   (cp_parser *, bool);
1952 static tree cp_parser_decltype
1953   (cp_parser *);
1954
1955 /* Declarators [gram.dcl.decl] */
1956
1957 static tree cp_parser_init_declarator
1958   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1959 static cp_declarator *cp_parser_declarator
1960   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1961 static cp_declarator *cp_parser_direct_declarator
1962   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1963 static enum tree_code cp_parser_ptr_operator
1964   (cp_parser *, tree *, cp_cv_quals *);
1965 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1966   (cp_parser *);
1967 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1968   (cp_parser *);
1969 static tree cp_parser_late_return_type_opt
1970   (cp_parser *, cp_cv_quals);
1971 static tree cp_parser_declarator_id
1972   (cp_parser *, bool);
1973 static tree cp_parser_type_id
1974   (cp_parser *);
1975 static tree cp_parser_template_type_arg
1976   (cp_parser *);
1977 static tree cp_parser_trailing_type_id (cp_parser *);
1978 static tree cp_parser_type_id_1
1979   (cp_parser *, bool, bool);
1980 static void cp_parser_type_specifier_seq
1981   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1982 static tree cp_parser_parameter_declaration_clause
1983   (cp_parser *);
1984 static tree cp_parser_parameter_declaration_list
1985   (cp_parser *, bool *);
1986 static cp_parameter_declarator *cp_parser_parameter_declaration
1987   (cp_parser *, bool, bool *);
1988 static tree cp_parser_default_argument 
1989   (cp_parser *, bool);
1990 static void cp_parser_function_body
1991   (cp_parser *, bool);
1992 static tree cp_parser_initializer
1993   (cp_parser *, bool *, bool *);
1994 static tree cp_parser_initializer_clause
1995   (cp_parser *, bool *);
1996 static tree cp_parser_braced_list
1997   (cp_parser*, bool*);
1998 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1999   (cp_parser *, bool *);
2000
2001 static bool cp_parser_ctor_initializer_opt_and_function_body
2002   (cp_parser *, bool);
2003
2004 /* Classes [gram.class] */
2005
2006 static tree cp_parser_class_name
2007   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2008 static tree cp_parser_class_specifier
2009   (cp_parser *);
2010 static tree cp_parser_class_head
2011   (cp_parser *, bool *);
2012 static enum tag_types cp_parser_class_key
2013   (cp_parser *);
2014 static void cp_parser_member_specification_opt
2015   (cp_parser *);
2016 static void cp_parser_member_declaration
2017   (cp_parser *);
2018 static tree cp_parser_pure_specifier
2019   (cp_parser *);
2020 static tree cp_parser_constant_initializer
2021   (cp_parser *);
2022
2023 /* Derived classes [gram.class.derived] */
2024
2025 static tree cp_parser_base_clause
2026   (cp_parser *);
2027 static tree cp_parser_base_specifier
2028   (cp_parser *);
2029
2030 /* Special member functions [gram.special] */
2031
2032 static tree cp_parser_conversion_function_id
2033   (cp_parser *);
2034 static tree cp_parser_conversion_type_id
2035   (cp_parser *);
2036 static cp_declarator *cp_parser_conversion_declarator_opt
2037   (cp_parser *);
2038 static bool cp_parser_ctor_initializer_opt
2039   (cp_parser *);
2040 static void cp_parser_mem_initializer_list
2041   (cp_parser *);
2042 static tree cp_parser_mem_initializer
2043   (cp_parser *);
2044 static tree cp_parser_mem_initializer_id
2045   (cp_parser *);
2046
2047 /* Overloading [gram.over] */
2048
2049 static tree cp_parser_operator_function_id
2050   (cp_parser *);
2051 static tree cp_parser_operator
2052   (cp_parser *);
2053
2054 /* Templates [gram.temp] */
2055
2056 static void cp_parser_template_declaration
2057   (cp_parser *, bool);
2058 static tree cp_parser_template_parameter_list
2059   (cp_parser *);
2060 static tree cp_parser_template_parameter
2061   (cp_parser *, bool *, bool *);
2062 static tree cp_parser_type_parameter
2063   (cp_parser *, bool *);
2064 static tree cp_parser_template_id
2065   (cp_parser *, bool, bool, enum tag_types, bool);
2066 static tree cp_parser_template_name
2067   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2068 static tree cp_parser_template_argument_list
2069   (cp_parser *);
2070 static tree cp_parser_template_argument
2071   (cp_parser *);
2072 static void cp_parser_explicit_instantiation
2073   (cp_parser *);
2074 static void cp_parser_explicit_specialization
2075   (cp_parser *);
2076
2077 /* Exception handling [gram.exception] */
2078
2079 static tree cp_parser_try_block
2080   (cp_parser *);
2081 static bool cp_parser_function_try_block
2082   (cp_parser *);
2083 static void cp_parser_handler_seq
2084   (cp_parser *);
2085 static void cp_parser_handler
2086   (cp_parser *);
2087 static tree cp_parser_exception_declaration
2088   (cp_parser *);
2089 static tree cp_parser_throw_expression
2090   (cp_parser *);
2091 static tree cp_parser_exception_specification_opt
2092   (cp_parser *);
2093 static tree cp_parser_type_id_list
2094   (cp_parser *);
2095
2096 /* GNU Extensions */
2097
2098 static tree cp_parser_asm_specification_opt
2099   (cp_parser *);
2100 static tree cp_parser_asm_operand_list
2101   (cp_parser *);
2102 static tree cp_parser_asm_clobber_list
2103   (cp_parser *);
2104 static tree cp_parser_asm_label_list
2105   (cp_parser *);
2106 static tree cp_parser_attributes_opt
2107   (cp_parser *);
2108 static tree cp_parser_attribute_list
2109   (cp_parser *);
2110 static bool cp_parser_extension_opt
2111   (cp_parser *, int *);
2112 static void cp_parser_label_declaration
2113   (cp_parser *);
2114
2115 /* Transactional Memory Extensions */
2116
2117 static tree cp_parser_transaction
2118   (cp_parser *, enum rid);
2119 static tree cp_parser_transaction_expression
2120   (cp_parser *, enum rid);
2121 static bool cp_parser_function_transaction
2122   (cp_parser *, enum rid);
2123 static tree cp_parser_transaction_cancel
2124   (cp_parser *);
2125
2126 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2127 static bool cp_parser_pragma
2128   (cp_parser *, enum pragma_context);
2129
2130 /* Objective-C++ Productions */
2131
2132 static tree cp_parser_objc_message_receiver
2133   (cp_parser *);
2134 static tree cp_parser_objc_message_args
2135   (cp_parser *);
2136 static tree cp_parser_objc_message_expression
2137   (cp_parser *);
2138 static tree cp_parser_objc_encode_expression
2139   (cp_parser *);
2140 static tree cp_parser_objc_defs_expression
2141   (cp_parser *);
2142 static tree cp_parser_objc_protocol_expression
2143   (cp_parser *);
2144 static tree cp_parser_objc_selector_expression
2145   (cp_parser *);
2146 static tree cp_parser_objc_expression
2147   (cp_parser *);
2148 static bool cp_parser_objc_selector_p
2149   (enum cpp_ttype);
2150 static tree cp_parser_objc_selector
2151   (cp_parser *);
2152 static tree cp_parser_objc_protocol_refs_opt
2153   (cp_parser *);
2154 static void cp_parser_objc_declaration
2155   (cp_parser *, tree);
2156 static tree cp_parser_objc_statement
2157   (cp_parser *);
2158 static bool cp_parser_objc_valid_prefix_attributes
2159   (cp_parser *, tree *);
2160 static void cp_parser_objc_at_property_declaration 
2161   (cp_parser *) ;
2162 static void cp_parser_objc_at_synthesize_declaration 
2163   (cp_parser *) ;
2164 static void cp_parser_objc_at_dynamic_declaration
2165   (cp_parser *) ;
2166 static tree cp_parser_objc_struct_declaration
2167   (cp_parser *) ;
2168
2169 /* Utility Routines */
2170
2171 static tree cp_parser_lookup_name
2172   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2173 static tree cp_parser_lookup_name_simple
2174   (cp_parser *, tree, location_t);
2175 static tree cp_parser_maybe_treat_template_as_class
2176   (tree, bool);
2177 static bool cp_parser_check_declarator_template_parameters
2178   (cp_parser *, cp_declarator *, location_t);
2179 static bool cp_parser_check_template_parameters
2180   (cp_parser *, unsigned, location_t, cp_declarator *);
2181 static tree cp_parser_simple_cast_expression
2182   (cp_parser *);
2183 static tree cp_parser_global_scope_opt
2184   (cp_parser *, bool);
2185 static bool cp_parser_constructor_declarator_p
2186   (cp_parser *, bool);
2187 static tree cp_parser_function_definition_from_specifiers_and_declarator
2188   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2189 static tree cp_parser_function_definition_after_declarator
2190   (cp_parser *, bool);
2191 static void cp_parser_template_declaration_after_export
2192   (cp_parser *, bool);
2193 static void cp_parser_perform_template_parameter_access_checks
2194   (VEC (deferred_access_check,gc)*);
2195 static tree cp_parser_single_declaration
2196   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2197 static tree cp_parser_functional_cast
2198   (cp_parser *, tree);
2199 static tree cp_parser_save_member_function_body
2200   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2201 static tree cp_parser_save_nsdmi
2202   (cp_parser *);
2203 static tree cp_parser_enclosed_template_argument_list
2204   (cp_parser *);
2205 static void cp_parser_save_default_args
2206   (cp_parser *, tree);
2207 static void cp_parser_late_parsing_for_member
2208   (cp_parser *, tree);
2209 static tree cp_parser_late_parse_one_default_arg
2210   (cp_parser *, tree, tree, tree);
2211 static void cp_parser_late_parsing_nsdmi
2212   (cp_parser *, tree);
2213 static void cp_parser_late_parsing_default_args
2214   (cp_parser *, tree);
2215 static tree cp_parser_sizeof_operand
2216   (cp_parser *, enum rid);
2217 static tree cp_parser_trait_expr
2218   (cp_parser *, enum rid);
2219 static bool cp_parser_declares_only_class_p
2220   (cp_parser *);
2221 static void cp_parser_set_storage_class
2222   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2223 static void cp_parser_set_decl_spec_type
2224   (cp_decl_specifier_seq *, tree, location_t, bool);
2225 static void set_and_check_decl_spec_loc
2226   (cp_decl_specifier_seq *decl_specs,
2227    cp_decl_spec ds, source_location location);
2228 static bool cp_parser_friend_p
2229   (const cp_decl_specifier_seq *);
2230 static void cp_parser_required_error
2231   (cp_parser *, required_token, bool);
2232 static cp_token *cp_parser_require
2233   (cp_parser *, enum cpp_ttype, required_token);
2234 static cp_token *cp_parser_require_keyword
2235   (cp_parser *, enum rid, required_token);
2236 static bool cp_parser_token_starts_function_definition_p
2237   (cp_token *);
2238 static bool cp_parser_next_token_starts_class_definition_p
2239   (cp_parser *);
2240 static bool cp_parser_next_token_ends_template_argument_p
2241   (cp_parser *);
2242 static bool cp_parser_nth_token_starts_template_argument_list_p
2243   (cp_parser *, size_t);
2244 static enum tag_types cp_parser_token_is_class_key
2245   (cp_token *);
2246 static void cp_parser_check_class_key
2247   (enum tag_types, tree type);
2248 static void cp_parser_check_access_in_redeclaration
2249   (tree type, location_t location);
2250 static bool cp_parser_optional_template_keyword
2251   (cp_parser *);
2252 static void cp_parser_pre_parsed_nested_name_specifier
2253   (cp_parser *);
2254 static bool cp_parser_cache_group
2255   (cp_parser *, enum cpp_ttype, unsigned);
2256 static tree cp_parser_cache_defarg
2257   (cp_parser *parser, bool nsdmi);
2258 static void cp_parser_parse_tentatively
2259   (cp_parser *);
2260 static void cp_parser_commit_to_tentative_parse
2261   (cp_parser *);
2262 static void cp_parser_abort_tentative_parse
2263   (cp_parser *);
2264 static bool cp_parser_parse_definitely
2265   (cp_parser *);
2266 static inline bool cp_parser_parsing_tentatively
2267   (cp_parser *);
2268 static bool cp_parser_uncommitted_to_tentative_parse_p
2269   (cp_parser *);
2270 static void cp_parser_error
2271   (cp_parser *, const char *);
2272 static void cp_parser_name_lookup_error
2273   (cp_parser *, tree, tree, name_lookup_error, location_t);
2274 static bool cp_parser_simulate_error
2275   (cp_parser *);
2276 static bool cp_parser_check_type_definition
2277   (cp_parser *);
2278 static void cp_parser_check_for_definition_in_return_type
2279   (cp_declarator *, tree, location_t type_location);
2280 static void cp_parser_check_for_invalid_template_id
2281   (cp_parser *, tree, enum tag_types, location_t location);
2282 static bool cp_parser_non_integral_constant_expression
2283   (cp_parser *, non_integral_constant);
2284 static void cp_parser_diagnose_invalid_type_name
2285   (cp_parser *, tree, tree, location_t);
2286 static bool cp_parser_parse_and_diagnose_invalid_type_name
2287   (cp_parser *);
2288 static int cp_parser_skip_to_closing_parenthesis
2289   (cp_parser *, bool, bool, bool);
2290 static void cp_parser_skip_to_end_of_statement
2291   (cp_parser *);
2292 static void cp_parser_consume_semicolon_at_end_of_statement
2293   (cp_parser *);
2294 static void cp_parser_skip_to_end_of_block_or_statement
2295   (cp_parser *);
2296 static bool cp_parser_skip_to_closing_brace
2297   (cp_parser *);
2298 static void cp_parser_skip_to_end_of_template_parameter_list
2299   (cp_parser *);
2300 static void cp_parser_skip_to_pragma_eol
2301   (cp_parser*, cp_token *);
2302 static bool cp_parser_error_occurred
2303   (cp_parser *);
2304 static bool cp_parser_allow_gnu_extensions_p
2305   (cp_parser *);
2306 static bool cp_parser_is_pure_string_literal
2307   (cp_token *);
2308 static bool cp_parser_is_string_literal
2309   (cp_token *);
2310 static bool cp_parser_is_keyword
2311   (cp_token *, enum rid);
2312 static tree cp_parser_make_typename_type
2313   (cp_parser *, tree, tree, location_t location);
2314 static cp_declarator * cp_parser_make_indirect_declarator
2315   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2316
2317 /* Returns nonzero if we are parsing tentatively.  */
2318
2319 static inline bool
2320 cp_parser_parsing_tentatively (cp_parser* parser)
2321 {
2322   return parser->context->next != NULL;
2323 }
2324
2325 /* Returns nonzero if TOKEN is a string literal.  */
2326
2327 static bool
2328 cp_parser_is_pure_string_literal (cp_token* token)
2329 {
2330   return (token->type == CPP_STRING ||
2331           token->type == CPP_STRING16 ||
2332           token->type == CPP_STRING32 ||
2333           token->type == CPP_WSTRING ||
2334           token->type == CPP_UTF8STRING);
2335 }
2336
2337 /* Returns nonzero if TOKEN is a string literal
2338    of a user-defined string literal.  */
2339
2340 static bool
2341 cp_parser_is_string_literal (cp_token* token)
2342 {
2343   return (cp_parser_is_pure_string_literal (token) ||
2344           token->type == CPP_STRING_USERDEF ||
2345           token->type == CPP_STRING16_USERDEF ||
2346           token->type == CPP_STRING32_USERDEF ||
2347           token->type == CPP_WSTRING_USERDEF ||
2348           token->type == CPP_UTF8STRING_USERDEF);
2349 }
2350
2351 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2352
2353 static bool
2354 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2355 {
2356   return token->keyword == keyword;
2357 }
2358
2359 /* If not parsing tentatively, issue a diagnostic of the form
2360       FILE:LINE: MESSAGE before TOKEN
2361    where TOKEN is the next token in the input stream.  MESSAGE
2362    (specified by the caller) is usually of the form "expected
2363    OTHER-TOKEN".  */
2364
2365 static void
2366 cp_parser_error (cp_parser* parser, const char* gmsgid)
2367 {
2368   if (!cp_parser_simulate_error (parser))
2369     {
2370       cp_token *token = cp_lexer_peek_token (parser->lexer);
2371       /* This diagnostic makes more sense if it is tagged to the line
2372          of the token we just peeked at.  */
2373       cp_lexer_set_source_position_from_token (token);
2374
2375       if (token->type == CPP_PRAGMA)
2376         {
2377           error_at (token->location,
2378                     "%<#pragma%> is not allowed here");
2379           cp_parser_skip_to_pragma_eol (parser, token);
2380           return;
2381         }
2382
2383       c_parse_error (gmsgid,
2384                      /* Because c_parser_error does not understand
2385                         CPP_KEYWORD, keywords are treated like
2386                         identifiers.  */
2387                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2388                      token->u.value, token->flags);
2389     }
2390 }
2391
2392 /* Issue an error about name-lookup failing.  NAME is the
2393    IDENTIFIER_NODE DECL is the result of
2394    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2395    the thing that we hoped to find.  */
2396
2397 static void
2398 cp_parser_name_lookup_error (cp_parser* parser,
2399                              tree name,
2400                              tree decl,
2401                              name_lookup_error desired,
2402                              location_t location)
2403 {
2404   /* If name lookup completely failed, tell the user that NAME was not
2405      declared.  */
2406   if (decl == error_mark_node)
2407     {
2408       if (parser->scope && parser->scope != global_namespace)
2409         error_at (location, "%<%E::%E%> has not been declared",
2410                   parser->scope, name);
2411       else if (parser->scope == global_namespace)
2412         error_at (location, "%<::%E%> has not been declared", name);
2413       else if (parser->object_scope
2414                && !CLASS_TYPE_P (parser->object_scope))
2415         error_at (location, "request for member %qE in non-class type %qT",
2416                   name, parser->object_scope);
2417       else if (parser->object_scope)
2418         error_at (location, "%<%T::%E%> has not been declared",
2419                   parser->object_scope, name);
2420       else
2421         error_at (location, "%qE has not been declared", name);
2422     }
2423   else if (parser->scope && parser->scope != global_namespace)
2424     {
2425       switch (desired)
2426         {
2427           case NLE_TYPE:
2428             error_at (location, "%<%E::%E%> is not a type",
2429                                 parser->scope, name);
2430             break;
2431           case NLE_CXX98:
2432             error_at (location, "%<%E::%E%> is not a class or namespace",
2433                                 parser->scope, name);
2434             break;
2435           case NLE_NOT_CXX98:
2436             error_at (location,
2437                       "%<%E::%E%> is not a class, namespace, or enumeration",
2438                       parser->scope, name);
2439             break;
2440           default:
2441             gcc_unreachable ();
2442             
2443         }
2444     }
2445   else if (parser->scope == global_namespace)
2446     {
2447       switch (desired)
2448         {
2449           case NLE_TYPE:
2450             error_at (location, "%<::%E%> is not a type", name);
2451             break;
2452           case NLE_CXX98:
2453             error_at (location, "%<::%E%> is not a class or namespace", name);
2454             break;
2455           case NLE_NOT_CXX98:
2456             error_at (location,
2457                       "%<::%E%> is not a class, namespace, or enumeration",
2458                       name);
2459             break;
2460           default:
2461             gcc_unreachable ();
2462         }
2463     }
2464   else
2465     {
2466       switch (desired)
2467         {
2468           case NLE_TYPE:
2469             error_at (location, "%qE is not a type", name);
2470             break;
2471           case NLE_CXX98:
2472             error_at (location, "%qE is not a class or namespace", name);
2473             break;
2474           case NLE_NOT_CXX98:
2475             error_at (location,
2476                       "%qE is not a class, namespace, or enumeration", name);
2477             break;
2478           default:
2479             gcc_unreachable ();
2480         }
2481     }
2482 }
2483
2484 /* If we are parsing tentatively, remember that an error has occurred
2485    during this tentative parse.  Returns true if the error was
2486    simulated; false if a message should be issued by the caller.  */
2487
2488 static bool
2489 cp_parser_simulate_error (cp_parser* parser)
2490 {
2491   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2492     {
2493       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2494       return true;
2495     }
2496   return false;
2497 }
2498
2499 /* This function is called when a type is defined.  If type
2500    definitions are forbidden at this point, an error message is
2501    issued.  */
2502
2503 static bool
2504 cp_parser_check_type_definition (cp_parser* parser)
2505 {
2506   /* If types are forbidden here, issue a message.  */
2507   if (parser->type_definition_forbidden_message)
2508     {
2509       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2510          in the message need to be interpreted.  */
2511       error (parser->type_definition_forbidden_message);
2512       return false;
2513     }
2514   return true;
2515 }
2516
2517 /* This function is called when the DECLARATOR is processed.  The TYPE
2518    was a type defined in the decl-specifiers.  If it is invalid to
2519    define a type in the decl-specifiers for DECLARATOR, an error is
2520    issued. TYPE_LOCATION is the location of TYPE and is used
2521    for error reporting.  */
2522
2523 static void
2524 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2525                                                tree type, location_t type_location)
2526 {
2527   /* [dcl.fct] forbids type definitions in return types.
2528      Unfortunately, it's not easy to know whether or not we are
2529      processing a return type until after the fact.  */
2530   while (declarator
2531          && (declarator->kind == cdk_pointer
2532              || declarator->kind == cdk_reference
2533              || declarator->kind == cdk_ptrmem))
2534     declarator = declarator->declarator;
2535   if (declarator
2536       && declarator->kind == cdk_function)
2537     {
2538       error_at (type_location,
2539                 "new types may not be defined in a return type");
2540       inform (type_location, 
2541               "(perhaps a semicolon is missing after the definition of %qT)",
2542               type);
2543     }
2544 }
2545
2546 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2547    "<" in any valid C++ program.  If the next token is indeed "<",
2548    issue a message warning the user about what appears to be an
2549    invalid attempt to form a template-id. LOCATION is the location
2550    of the type-specifier (TYPE) */
2551
2552 static void
2553 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2554                                          tree type,
2555                                          enum tag_types tag_type,
2556                                          location_t location)
2557 {
2558   cp_token_position start = 0;
2559
2560   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2561     {
2562       if (TYPE_P (type))
2563         error_at (location, "%qT is not a template", type);
2564       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2565         {
2566           if (tag_type != none_type)
2567             error_at (location, "%qE is not a class template", type);
2568           else
2569             error_at (location, "%qE is not a template", type);
2570         }
2571       else
2572         error_at (location, "invalid template-id");
2573       /* Remember the location of the invalid "<".  */
2574       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2575         start = cp_lexer_token_position (parser->lexer, true);
2576       /* Consume the "<".  */
2577       cp_lexer_consume_token (parser->lexer);
2578       /* Parse the template arguments.  */
2579       cp_parser_enclosed_template_argument_list (parser);
2580       /* Permanently remove the invalid template arguments so that
2581          this error message is not issued again.  */
2582       if (start)
2583         cp_lexer_purge_tokens_after (parser->lexer, start);
2584     }
2585 }
2586
2587 /* If parsing an integral constant-expression, issue an error message
2588    about the fact that THING appeared and return true.  Otherwise,
2589    return false.  In either case, set
2590    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2591
2592 static bool
2593 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2594                                             non_integral_constant thing)
2595 {
2596   parser->non_integral_constant_expression_p = true;
2597   if (parser->integral_constant_expression_p)
2598     {
2599       if (!parser->allow_non_integral_constant_expression_p)
2600         {
2601           const char *msg = NULL;
2602           switch (thing)
2603             {
2604               case NIC_FLOAT:
2605                 error ("floating-point literal "
2606                        "cannot appear in a constant-expression");
2607                 return true;
2608               case NIC_CAST:
2609                 error ("a cast to a type other than an integral or "
2610                        "enumeration type cannot appear in a "
2611                        "constant-expression");
2612                 return true;
2613               case NIC_TYPEID:
2614                 error ("%<typeid%> operator "
2615                        "cannot appear in a constant-expression");
2616                 return true;
2617               case NIC_NCC:
2618                 error ("non-constant compound literals "
2619                        "cannot appear in a constant-expression");
2620                 return true;
2621               case NIC_FUNC_CALL:
2622                 error ("a function call "
2623                        "cannot appear in a constant-expression");
2624                 return true;
2625               case NIC_INC:
2626                 error ("an increment "
2627                        "cannot appear in a constant-expression");
2628                 return true;
2629               case NIC_DEC:
2630                 error ("an decrement "
2631                        "cannot appear in a constant-expression");
2632                 return true;
2633               case NIC_ARRAY_REF:
2634                 error ("an array reference "
2635                        "cannot appear in a constant-expression");
2636                 return true;
2637               case NIC_ADDR_LABEL:
2638                 error ("the address of a label "
2639                        "cannot appear in a constant-expression");
2640                 return true;
2641               case NIC_OVERLOADED:
2642                 error ("calls to overloaded operators "
2643                        "cannot appear in a constant-expression");
2644                 return true;
2645               case NIC_ASSIGNMENT:
2646                 error ("an assignment cannot appear in a constant-expression");
2647                 return true;
2648               case NIC_COMMA:
2649                 error ("a comma operator "
2650                        "cannot appear in a constant-expression");
2651                 return true;
2652               case NIC_CONSTRUCTOR:
2653                 error ("a call to a constructor "
2654                        "cannot appear in a constant-expression");
2655                 return true;
2656               case NIC_TRANSACTION:
2657                 error ("a transaction expression "
2658                        "cannot appear in a constant-expression");
2659                 return true;
2660               case NIC_THIS:
2661                 msg = "this";
2662                 break;
2663               case NIC_FUNC_NAME:
2664                 msg = "__FUNCTION__";
2665                 break;
2666               case NIC_PRETTY_FUNC:
2667                 msg = "__PRETTY_FUNCTION__";
2668                 break;
2669               case NIC_C99_FUNC:
2670                 msg = "__func__";
2671                 break;
2672               case NIC_VA_ARG:
2673                 msg = "va_arg";
2674                 break;
2675               case NIC_ARROW:
2676                 msg = "->";
2677                 break;
2678               case NIC_POINT:
2679                 msg = ".";
2680                 break;
2681               case NIC_STAR:
2682                 msg = "*";
2683                 break;
2684               case NIC_ADDR:
2685                 msg = "&";
2686                 break;
2687               case NIC_PREINCREMENT:
2688                 msg = "++";
2689                 break;
2690               case NIC_PREDECREMENT:
2691                 msg = "--";
2692                 break;
2693               case NIC_NEW:
2694                 msg = "new";
2695                 break;
2696               case NIC_DEL:
2697                 msg = "delete";
2698                 break;
2699               default:
2700                 gcc_unreachable ();
2701             }
2702           if (msg)
2703             error ("%qs cannot appear in a constant-expression", msg);
2704           return true;
2705         }
2706     }
2707   return false;
2708 }
2709
2710 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2711    qualifying scope (or NULL, if none) for ID.  This function commits
2712    to the current active tentative parse, if any.  (Otherwise, the
2713    problematic construct might be encountered again later, resulting
2714    in duplicate error messages.) LOCATION is the location of ID.  */
2715
2716 static void
2717 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2718                                       tree scope, tree id,
2719                                       location_t location)
2720 {
2721   tree decl, old_scope;
2722   cp_parser_commit_to_tentative_parse (parser);
2723   /* Try to lookup the identifier.  */
2724   old_scope = parser->scope;
2725   parser->scope = scope;
2726   decl = cp_parser_lookup_name_simple (parser, id, location);
2727   parser->scope = old_scope;
2728   /* If the lookup found a template-name, it means that the user forgot
2729   to specify an argument list. Emit a useful error message.  */
2730   if (TREE_CODE (decl) == TEMPLATE_DECL)
2731     error_at (location,
2732               "invalid use of template-name %qE without an argument list",
2733               decl);
2734   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2735     error_at (location, "invalid use of destructor %qD as a type", id);
2736   else if (TREE_CODE (decl) == TYPE_DECL)
2737     /* Something like 'unsigned A a;'  */
2738     error_at (location, "invalid combination of multiple type-specifiers");
2739   else if (!parser->scope)
2740     {
2741       /* Issue an error message.  */
2742       error_at (location, "%qE does not name a type", id);
2743       /* If we're in a template class, it's possible that the user was
2744          referring to a type from a base class.  For example:
2745
2746            template <typename T> struct A { typedef T X; };
2747            template <typename T> struct B : public A<T> { X x; };
2748
2749          The user should have said "typename A<T>::X".  */
2750       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2751         inform (location, "C++11 %<constexpr%> only available with "
2752                 "-std=c++11 or -std=gnu++11");
2753       else if (processing_template_decl && current_class_type
2754                && TYPE_BINFO (current_class_type))
2755         {
2756           tree b;
2757
2758           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2759                b;
2760                b = TREE_CHAIN (b))
2761             {
2762               tree base_type = BINFO_TYPE (b);
2763               if (CLASS_TYPE_P (base_type)
2764                   && dependent_type_p (base_type))
2765                 {
2766                   tree field;
2767                   /* Go from a particular instantiation of the
2768                      template (which will have an empty TYPE_FIELDs),
2769                      to the main version.  */
2770                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2771                   for (field = TYPE_FIELDS (base_type);
2772                        field;
2773                        field = DECL_CHAIN (field))
2774                     if (TREE_CODE (field) == TYPE_DECL
2775                         && DECL_NAME (field) == id)
2776                       {
2777                         inform (location, 
2778                                 "(perhaps %<typename %T::%E%> was intended)",
2779                                 BINFO_TYPE (b), id);
2780                         break;
2781                       }
2782                   if (field)
2783                     break;
2784                 }
2785             }
2786         }
2787     }
2788   /* Here we diagnose qualified-ids where the scope is actually correct,
2789      but the identifier does not resolve to a valid type name.  */
2790   else if (parser->scope != error_mark_node)
2791     {
2792       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2793         error_at (location, "%qE in namespace %qE does not name a type",
2794                   id, parser->scope);
2795       else if (CLASS_TYPE_P (parser->scope)
2796                && constructor_name_p (id, parser->scope))
2797         {
2798           /* A<T>::A<T>() */
2799           error_at (location, "%<%T::%E%> names the constructor, not"
2800                     " the type", parser->scope, id);
2801           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2802             error_at (location, "and %qT has no template constructors",
2803                       parser->scope);
2804         }
2805       else if (TYPE_P (parser->scope)
2806                && dependent_scope_p (parser->scope))
2807         error_at (location, "need %<typename%> before %<%T::%E%> because "
2808                   "%qT is a dependent scope",
2809                   parser->scope, id, parser->scope);
2810       else if (TYPE_P (parser->scope))
2811         error_at (location, "%qE in %q#T does not name a type",
2812                   id, parser->scope);
2813       else
2814         gcc_unreachable ();
2815     }
2816 }
2817
2818 /* Check for a common situation where a type-name should be present,
2819    but is not, and issue a sensible error message.  Returns true if an
2820    invalid type-name was detected.
2821
2822    The situation handled by this function are variable declarations of the
2823    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2824    Usually, `ID' should name a type, but if we got here it means that it
2825    does not. We try to emit the best possible error message depending on
2826    how exactly the id-expression looks like.  */
2827
2828 static bool
2829 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2830 {
2831   tree id;
2832   cp_token *token = cp_lexer_peek_token (parser->lexer);
2833
2834   /* Avoid duplicate error about ambiguous lookup.  */
2835   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2836     {
2837       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2838       if (next->type == CPP_NAME && next->ambiguous_p)
2839         goto out;
2840     }
2841
2842   cp_parser_parse_tentatively (parser);
2843   id = cp_parser_id_expression (parser,
2844                                 /*template_keyword_p=*/false,
2845                                 /*check_dependency_p=*/true,
2846                                 /*template_p=*/NULL,
2847                                 /*declarator_p=*/true,
2848                                 /*optional_p=*/false);
2849   /* If the next token is a (, this is a function with no explicit return
2850      type, i.e. constructor, destructor or conversion op.  */
2851   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2852       || TREE_CODE (id) == TYPE_DECL)
2853     {
2854       cp_parser_abort_tentative_parse (parser);
2855       return false;
2856     }
2857   if (!cp_parser_parse_definitely (parser))
2858     return false;
2859
2860   /* Emit a diagnostic for the invalid type.  */
2861   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2862                                         id, token->location);
2863  out:
2864   /* If we aren't in the middle of a declarator (i.e. in a
2865      parameter-declaration-clause), skip to the end of the declaration;
2866      there's no point in trying to process it.  */
2867   if (!parser->in_declarator_p)
2868     cp_parser_skip_to_end_of_block_or_statement (parser);
2869   return true;
2870 }
2871
2872 /* Consume tokens up to, and including, the next non-nested closing `)'.
2873    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2874    are doing error recovery. Returns -1 if OR_COMMA is true and we
2875    found an unnested comma.  */
2876
2877 static int
2878 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2879                                        bool recovering,
2880                                        bool or_comma,
2881                                        bool consume_paren)
2882 {
2883   unsigned paren_depth = 0;
2884   unsigned brace_depth = 0;
2885   unsigned square_depth = 0;
2886
2887   if (recovering && !or_comma
2888       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2889     return 0;
2890
2891   while (true)
2892     {
2893       cp_token * token = cp_lexer_peek_token (parser->lexer);
2894
2895       switch (token->type)
2896         {
2897         case CPP_EOF:
2898         case CPP_PRAGMA_EOL:
2899           /* If we've run out of tokens, then there is no closing `)'.  */
2900           return 0;
2901
2902         /* This is good for lambda expression capture-lists.  */
2903         case CPP_OPEN_SQUARE:
2904           ++square_depth;
2905           break;
2906         case CPP_CLOSE_SQUARE:
2907           if (!square_depth--)
2908             return 0;
2909           break;
2910
2911         case CPP_SEMICOLON:
2912           /* This matches the processing in skip_to_end_of_statement.  */
2913           if (!brace_depth)
2914             return 0;
2915           break;
2916
2917         case CPP_OPEN_BRACE:
2918           ++brace_depth;
2919           break;
2920         case CPP_CLOSE_BRACE:
2921           if (!brace_depth--)
2922             return 0;
2923           break;
2924
2925         case CPP_COMMA:
2926           if (recovering && or_comma && !brace_depth && !paren_depth
2927               && !square_depth)
2928             return -1;
2929           break;
2930
2931         case CPP_OPEN_PAREN:
2932           if (!brace_depth)
2933             ++paren_depth;
2934           break;
2935
2936         case CPP_CLOSE_PAREN:
2937           if (!brace_depth && !paren_depth--)
2938             {
2939               if (consume_paren)
2940                 cp_lexer_consume_token (parser->lexer);
2941               return 1;
2942             }
2943           break;
2944
2945         default:
2946           break;
2947         }
2948
2949       /* Consume the token.  */
2950       cp_lexer_consume_token (parser->lexer);
2951     }
2952 }
2953
2954 /* Consume tokens until we reach the end of the current statement.
2955    Normally, that will be just before consuming a `;'.  However, if a
2956    non-nested `}' comes first, then we stop before consuming that.  */
2957
2958 static void
2959 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2960 {
2961   unsigned nesting_depth = 0;
2962
2963   while (true)
2964     {
2965       cp_token *token = cp_lexer_peek_token (parser->lexer);
2966
2967       switch (token->type)
2968         {
2969         case CPP_EOF:
2970         case CPP_PRAGMA_EOL:
2971           /* If we've run out of tokens, stop.  */
2972           return;
2973
2974         case CPP_SEMICOLON:
2975           /* If the next token is a `;', we have reached the end of the
2976              statement.  */
2977           if (!nesting_depth)
2978             return;
2979           break;
2980
2981         case CPP_CLOSE_BRACE:
2982           /* If this is a non-nested '}', stop before consuming it.
2983              That way, when confronted with something like:
2984
2985                { 3 + }
2986
2987              we stop before consuming the closing '}', even though we
2988              have not yet reached a `;'.  */
2989           if (nesting_depth == 0)
2990             return;
2991
2992           /* If it is the closing '}' for a block that we have
2993              scanned, stop -- but only after consuming the token.
2994              That way given:
2995
2996                 void f g () { ... }
2997                 typedef int I;
2998
2999              we will stop after the body of the erroneously declared
3000              function, but before consuming the following `typedef'
3001              declaration.  */
3002           if (--nesting_depth == 0)
3003             {
3004               cp_lexer_consume_token (parser->lexer);
3005               return;
3006             }
3007
3008         case CPP_OPEN_BRACE:
3009           ++nesting_depth;
3010           break;
3011
3012         default:
3013           break;
3014         }
3015
3016       /* Consume the token.  */
3017       cp_lexer_consume_token (parser->lexer);
3018     }
3019 }
3020
3021 /* This function is called at the end of a statement or declaration.
3022    If the next token is a semicolon, it is consumed; otherwise, error
3023    recovery is attempted.  */
3024
3025 static void
3026 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3027 {
3028   /* Look for the trailing `;'.  */
3029   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3030     {
3031       /* If there is additional (erroneous) input, skip to the end of
3032          the statement.  */
3033       cp_parser_skip_to_end_of_statement (parser);
3034       /* If the next token is now a `;', consume it.  */
3035       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3036         cp_lexer_consume_token (parser->lexer);
3037     }
3038 }
3039
3040 /* Skip tokens until we have consumed an entire block, or until we
3041    have consumed a non-nested `;'.  */
3042
3043 static void
3044 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3045 {
3046   int nesting_depth = 0;
3047
3048   while (nesting_depth >= 0)
3049     {
3050       cp_token *token = cp_lexer_peek_token (parser->lexer);
3051
3052       switch (token->type)
3053         {
3054         case CPP_EOF:
3055         case CPP_PRAGMA_EOL:
3056           /* If we've run out of tokens, stop.  */
3057           return;
3058
3059         case CPP_SEMICOLON:
3060           /* Stop if this is an unnested ';'. */
3061           if (!nesting_depth)
3062             nesting_depth = -1;
3063           break;
3064
3065         case CPP_CLOSE_BRACE:
3066           /* Stop if this is an unnested '}', or closes the outermost
3067              nesting level.  */
3068           nesting_depth--;
3069           if (nesting_depth < 0)
3070             return;
3071           if (!nesting_depth)
3072             nesting_depth = -1;
3073           break;
3074
3075         case CPP_OPEN_BRACE:
3076           /* Nest. */
3077           nesting_depth++;
3078           break;
3079
3080         default:
3081           break;
3082         }
3083
3084       /* Consume the token.  */
3085       cp_lexer_consume_token (parser->lexer);
3086     }
3087 }
3088
3089 /* Skip tokens until a non-nested closing curly brace is the next
3090    token, or there are no more tokens. Return true in the first case,
3091    false otherwise.  */
3092
3093 static bool
3094 cp_parser_skip_to_closing_brace (cp_parser *parser)
3095 {
3096   unsigned nesting_depth = 0;
3097
3098   while (true)
3099     {
3100       cp_token *token = cp_lexer_peek_token (parser->lexer);
3101
3102       switch (token->type)
3103         {
3104         case CPP_EOF:
3105         case CPP_PRAGMA_EOL:
3106           /* If we've run out of tokens, stop.  */
3107           return false;
3108
3109         case CPP_CLOSE_BRACE:
3110           /* If the next token is a non-nested `}', then we have reached
3111              the end of the current block.  */
3112           if (nesting_depth-- == 0)
3113             return true;
3114           break;
3115
3116         case CPP_OPEN_BRACE:
3117           /* If it the next token is a `{', then we are entering a new
3118              block.  Consume the entire block.  */
3119           ++nesting_depth;
3120           break;
3121
3122         default:
3123           break;
3124         }
3125
3126       /* Consume the token.  */
3127       cp_lexer_consume_token (parser->lexer);
3128     }
3129 }
3130
3131 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3132    parameter is the PRAGMA token, allowing us to purge the entire pragma
3133    sequence.  */
3134
3135 static void
3136 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3137 {
3138   cp_token *token;
3139
3140   parser->lexer->in_pragma = false;
3141
3142   do
3143     token = cp_lexer_consume_token (parser->lexer);
3144   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3145
3146   /* Ensure that the pragma is not parsed again.  */
3147   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3148 }
3149
3150 /* Require pragma end of line, resyncing with it as necessary.  The
3151    arguments are as for cp_parser_skip_to_pragma_eol.  */
3152
3153 static void
3154 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3155 {
3156   parser->lexer->in_pragma = false;
3157   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3158     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3159 }
3160
3161 /* This is a simple wrapper around make_typename_type. When the id is
3162    an unresolved identifier node, we can provide a superior diagnostic
3163    using cp_parser_diagnose_invalid_type_name.  */
3164
3165 static tree
3166 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3167                               tree id, location_t id_location)
3168 {
3169   tree result;
3170   if (TREE_CODE (id) == IDENTIFIER_NODE)
3171     {
3172       result = make_typename_type (scope, id, typename_type,
3173                                    /*complain=*/tf_none);
3174       if (result == error_mark_node)
3175         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3176       return result;
3177     }
3178   return make_typename_type (scope, id, typename_type, tf_error);
3179 }
3180
3181 /* This is a wrapper around the
3182    make_{pointer,ptrmem,reference}_declarator functions that decides
3183    which one to call based on the CODE and CLASS_TYPE arguments. The
3184    CODE argument should be one of the values returned by
3185    cp_parser_ptr_operator. */
3186 static cp_declarator *
3187 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3188                                     cp_cv_quals cv_qualifiers,
3189                                     cp_declarator *target)
3190 {
3191   if (code == ERROR_MARK)
3192     return cp_error_declarator;
3193
3194   if (code == INDIRECT_REF)
3195     if (class_type == NULL_TREE)
3196       return make_pointer_declarator (cv_qualifiers, target);
3197     else
3198       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3199   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3200     return make_reference_declarator (cv_qualifiers, target, false);
3201   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3202     return make_reference_declarator (cv_qualifiers, target, true);
3203   gcc_unreachable ();
3204 }
3205
3206 /* Create a new C++ parser.  */
3207
3208 static cp_parser *
3209 cp_parser_new (void)
3210 {
3211   cp_parser *parser;
3212   cp_lexer *lexer;
3213   unsigned i;
3214
3215   /* cp_lexer_new_main is called before doing GC allocation because
3216      cp_lexer_new_main might load a PCH file.  */
3217   lexer = cp_lexer_new_main ();
3218
3219   /* Initialize the binops_by_token so that we can get the tree
3220      directly from the token.  */
3221   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3222     binops_by_token[binops[i].token_type] = binops[i];
3223
3224   parser = ggc_alloc_cleared_cp_parser ();
3225   parser->lexer = lexer;
3226   parser->context = cp_parser_context_new (NULL);
3227
3228   /* For now, we always accept GNU extensions.  */
3229   parser->allow_gnu_extensions_p = 1;
3230
3231   /* The `>' token is a greater-than operator, not the end of a
3232      template-id.  */
3233   parser->greater_than_is_operator_p = true;
3234
3235   parser->default_arg_ok_p = true;
3236
3237   /* We are not parsing a constant-expression.  */
3238   parser->integral_constant_expression_p = false;
3239   parser->allow_non_integral_constant_expression_p = false;
3240   parser->non_integral_constant_expression_p = false;
3241
3242   /* Local variable names are not forbidden.  */
3243   parser->local_variables_forbidden_p = false;
3244
3245   /* We are not processing an `extern "C"' declaration.  */
3246   parser->in_unbraced_linkage_specification_p = false;
3247
3248   /* We are not processing a declarator.  */
3249   parser->in_declarator_p = false;
3250
3251   /* We are not processing a template-argument-list.  */
3252   parser->in_template_argument_list_p = false;
3253
3254   /* We are not in an iteration statement.  */
3255   parser->in_statement = 0;
3256
3257   /* We are not in a switch statement.  */
3258   parser->in_switch_statement_p = false;
3259
3260   /* We are not parsing a type-id inside an expression.  */
3261   parser->in_type_id_in_expr_p = false;
3262
3263   /* Declarations aren't implicitly extern "C".  */
3264   parser->implicit_extern_c = false;
3265
3266   /* String literals should be translated to the execution character set.  */
3267   parser->translate_strings_p = true;
3268
3269   /* We are not parsing a function body.  */
3270   parser->in_function_body = false;
3271
3272   /* We can correct until told otherwise.  */
3273   parser->colon_corrects_to_scope_p = true;
3274
3275   /* The unparsed function queue is empty.  */
3276   push_unparsed_function_queues (parser);
3277
3278   /* There are no classes being defined.  */
3279   parser->num_classes_being_defined = 0;
3280
3281   /* No template parameters apply.  */
3282   parser->num_template_parameter_lists = 0;
3283
3284   return parser;
3285 }
3286
3287 /* Create a cp_lexer structure which will emit the tokens in CACHE
3288    and push it onto the parser's lexer stack.  This is used for delayed
3289    parsing of in-class method bodies and default arguments, and should
3290    not be confused with tentative parsing.  */
3291 static void
3292 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3293 {
3294   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3295   lexer->next = parser->lexer;
3296   parser->lexer = lexer;
3297
3298   /* Move the current source position to that of the first token in the
3299      new lexer.  */
3300   cp_lexer_set_source_position_from_token (lexer->next_token);
3301 }
3302
3303 /* Pop the top lexer off the parser stack.  This is never used for the
3304    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3305 static void
3306 cp_parser_pop_lexer (cp_parser *parser)
3307 {
3308   cp_lexer *lexer = parser->lexer;
3309   parser->lexer = lexer->next;
3310   cp_lexer_destroy (lexer);
3311
3312   /* Put the current source position back where it was before this
3313      lexer was pushed.  */
3314   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3315 }
3316
3317 /* Lexical conventions [gram.lex]  */
3318
3319 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3320    identifier.  */
3321
3322 static tree
3323 cp_parser_identifier (cp_parser* parser)
3324 {
3325   cp_token *token;
3326
3327   /* Look for the identifier.  */
3328   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3329   /* Return the value.  */
3330   return token ? token->u.value : error_mark_node;
3331 }
3332
3333 /* Parse a sequence of adjacent string constants.  Returns a
3334    TREE_STRING representing the combined, nul-terminated string
3335    constant.  If TRANSLATE is true, translate the string to the
3336    execution character set.  If WIDE_OK is true, a wide string is
3337    invalid here.
3338
3339    C++98 [lex.string] says that if a narrow string literal token is
3340    adjacent to a wide string literal token, the behavior is undefined.
3341    However, C99 6.4.5p4 says that this results in a wide string literal.
3342    We follow C99 here, for consistency with the C front end.
3343
3344    This code is largely lifted from lex_string() in c-lex.c.
3345
3346    FUTURE: ObjC++ will need to handle @-strings here.  */
3347 static tree
3348 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3349 {
3350   tree value;
3351   size_t count;
3352   struct obstack str_ob;
3353   cpp_string str, istr, *strs;
3354   cp_token *tok;
3355   enum cpp_ttype type, curr_type;
3356   int have_suffix_p = 0;
3357   tree string_tree;
3358   tree suffix_id = NULL_TREE;
3359   bool curr_tok_is_userdef_p = false;
3360
3361   tok = cp_lexer_peek_token (parser->lexer);
3362   if (!cp_parser_is_string_literal (tok))
3363     {
3364       cp_parser_error (parser, "expected string-literal");
3365       return error_mark_node;
3366     }
3367
3368   if (cpp_userdef_string_p (tok->type))
3369     {
3370       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3371       curr_type = cpp_userdef_string_remove_type (tok->type);
3372       curr_tok_is_userdef_p = true;
3373     }
3374   else
3375     {
3376       string_tree = tok->u.value;
3377       curr_type = tok->type;
3378     }
3379   type = curr_type;
3380
3381   /* Try to avoid the overhead of creating and destroying an obstack
3382      for the common case of just one string.  */
3383   if (!cp_parser_is_string_literal
3384       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3385     {
3386       cp_lexer_consume_token (parser->lexer);
3387
3388       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3389       str.len = TREE_STRING_LENGTH (string_tree);
3390       count = 1;
3391
3392       if (curr_tok_is_userdef_p)
3393         {
3394           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3395           have_suffix_p = 1;
3396           curr_type = cpp_userdef_string_remove_type (tok->type);
3397         }
3398       else
3399         curr_type = tok->type;
3400
3401       strs = &str;
3402     }
3403   else
3404     {
3405       gcc_obstack_init (&str_ob);
3406       count = 0;
3407
3408       do
3409         {
3410           cp_lexer_consume_token (parser->lexer);
3411           count++;
3412           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3413           str.len = TREE_STRING_LENGTH (string_tree);
3414
3415           if (curr_tok_is_userdef_p)
3416             {
3417               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3418               if (have_suffix_p == 0)
3419                 {
3420                   suffix_id = curr_suffix_id;
3421                   have_suffix_p = 1;
3422                 }
3423               else if (have_suffix_p == 1
3424                        && curr_suffix_id != suffix_id)
3425                 {
3426                   error ("inconsistent user-defined literal suffixes"
3427                          " %qD and %qD in string literal",
3428                          suffix_id, curr_suffix_id);
3429                   have_suffix_p = -1;
3430                 }
3431               curr_type = cpp_userdef_string_remove_type (tok->type);
3432             }
3433           else
3434             curr_type = tok->type;
3435
3436           if (type != curr_type)
3437             {
3438               if (type == CPP_STRING)
3439                 type = curr_type;
3440               else if (curr_type != CPP_STRING)
3441                 error_at (tok->location,
3442                           "unsupported non-standard concatenation "
3443                           "of string literals");
3444             }
3445
3446           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3447
3448           tok = cp_lexer_peek_token (parser->lexer);
3449           if (cpp_userdef_string_p (tok->type))
3450             {
3451               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3452               curr_type = cpp_userdef_string_remove_type (tok->type);
3453               curr_tok_is_userdef_p = true;
3454             }
3455           else
3456             {
3457               string_tree = tok->u.value;
3458               curr_type = tok->type;
3459               curr_tok_is_userdef_p = false;
3460             }
3461         }
3462       while (cp_parser_is_string_literal (tok));
3463
3464       strs = (cpp_string *) obstack_finish (&str_ob);
3465     }
3466
3467   if (type != CPP_STRING && !wide_ok)
3468     {
3469       cp_parser_error (parser, "a wide string is invalid in this context");
3470       type = CPP_STRING;
3471     }
3472
3473   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3474       (parse_in, strs, count, &istr, type))
3475     {
3476       value = build_string (istr.len, (const char *)istr.text);
3477       free (CONST_CAST (unsigned char *, istr.text));
3478
3479       switch (type)
3480         {
3481         default:
3482         case CPP_STRING:
3483         case CPP_UTF8STRING:
3484           TREE_TYPE (value) = char_array_type_node;
3485           break;
3486         case CPP_STRING16:
3487           TREE_TYPE (value) = char16_array_type_node;
3488           break;
3489         case CPP_STRING32:
3490           TREE_TYPE (value) = char32_array_type_node;
3491           break;
3492         case CPP_WSTRING:
3493           TREE_TYPE (value) = wchar_array_type_node;
3494           break;
3495         }
3496
3497       value = fix_string_type (value);
3498
3499       if (have_suffix_p)
3500         {
3501           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3502           tok->u.value = literal;
3503           return cp_parser_userdef_string_literal (tok);
3504         }
3505     }
3506   else
3507     /* cpp_interpret_string has issued an error.  */
3508     value = error_mark_node;
3509
3510   if (count > 1)
3511     obstack_free (&str_ob, 0);
3512
3513   return value;
3514 }
3515
3516 /* Look up a literal operator with the name and the exact arguments.  */
3517
3518 static tree
3519 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3520 {
3521   tree decl, fns;
3522   decl = lookup_name (name);
3523   if (!decl || !is_overloaded_fn (decl))
3524     return error_mark_node;
3525
3526   for (fns = decl; fns; fns = OVL_NEXT (fns))
3527     {
3528       unsigned int ix;
3529       bool found = true;
3530       tree fn = OVL_CURRENT (fns);
3531       tree argtypes = NULL_TREE;
3532       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3533       if (argtypes != NULL_TREE)
3534         {
3535           for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3536                ++ix, argtypes = TREE_CHAIN (argtypes))
3537             {
3538               tree targ = TREE_VALUE (argtypes);
3539               tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3540               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3541               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3542               if ((ptr || arr || !same_type_p (targ, tparm))
3543                   && (!ptr || !arr
3544                       || !same_type_p (TREE_TYPE (targ),
3545                                        TREE_TYPE (tparm))))
3546                 found = false;
3547             }
3548           if (found
3549               && ix == VEC_length (tree, args)
3550               /* May be this should be sufficient_parms_p instead,
3551                  depending on how exactly should user-defined literals
3552                  work in presence of default arguments on the literal
3553                  operator parameters.  */
3554               && argtypes == void_list_node)
3555             return fn;
3556         }
3557     }
3558
3559   return error_mark_node;
3560 }
3561
3562 /* Parse a user-defined char constant.  Returns a call to a user-defined
3563    literal operator taking the character as an argument.  */
3564
3565 static tree
3566 cp_parser_userdef_char_literal (cp_parser *parser)
3567 {
3568   cp_token *token = cp_lexer_consume_token (parser->lexer);
3569   tree literal = token->u.value;
3570   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3571   tree value = USERDEF_LITERAL_VALUE (literal);
3572   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3573   tree decl, result;
3574
3575   /* Build up a call to the user-defined operator  */
3576   /* Lookup the name we got back from the id-expression.  */
3577   VEC(tree,gc) *args = make_tree_vector ();
3578   VEC_safe_push (tree, gc, args, value);
3579   decl = lookup_literal_operator (name, args);
3580   if (!decl || decl == error_mark_node)
3581     {
3582       error ("unable to find character literal operator %qD with %qT argument",
3583              name, TREE_TYPE (value));
3584       release_tree_vector (args);
3585       return error_mark_node;
3586     }
3587   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3588   release_tree_vector (args);
3589   if (result != error_mark_node)
3590     return result;
3591
3592   error ("unable to find character literal operator %qD with %qT argument",
3593          name, TREE_TYPE (value));
3594   return error_mark_node;
3595 }
3596
3597 /* A subroutine of cp_parser_userdef_numeric_literal to
3598    create a char... template parameter pack from a string node.  */
3599
3600 static tree
3601 make_char_string_pack (tree value)
3602 {
3603   tree charvec;
3604   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3605   const char *str = TREE_STRING_POINTER (value);
3606   int i, len = TREE_STRING_LENGTH (value) - 1;
3607   tree argvec = make_tree_vec (1);
3608
3609   /* Fill in CHARVEC with all of the parameters.  */
3610   charvec = make_tree_vec (len);
3611   for (i = 0; i < len; ++i)
3612     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3613
3614   /* Build the argument packs.  */
3615   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3616   TREE_TYPE (argpack) = char_type_node;
3617
3618   TREE_VEC_ELT (argvec, 0) = argpack;
3619
3620   return argvec;
3621 }
3622
3623 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3624    literal operator.  */
3625
3626 static tree
3627 cp_parser_userdef_numeric_literal (cp_parser *parser)
3628 {
3629   cp_token *token = cp_lexer_consume_token (parser->lexer);
3630   tree literal = token->u.value;
3631   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3632   tree value = USERDEF_LITERAL_VALUE (literal);
3633   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3634   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3635   tree decl, result;
3636   VEC(tree,gc) *args;
3637
3638   /* Look for a literal operator taking the exact type of numeric argument
3639      as the literal value.  */
3640   args = make_tree_vector ();
3641   VEC_safe_push (tree, gc, args, value);
3642   decl = lookup_literal_operator (name, args);
3643   if (decl && decl != error_mark_node)
3644     {
3645       result = finish_call_expr (decl, &args, false, true, tf_none);
3646       if (result != error_mark_node)
3647         {
3648           release_tree_vector (args);
3649           return result;
3650         }
3651     }
3652   release_tree_vector (args);
3653
3654   /* If the numeric argument didn't work, look for a raw literal
3655      operator taking a const char* argument consisting of the number
3656      in string format.  */
3657   args = make_tree_vector ();
3658   VEC_safe_push (tree, gc, args, num_string);
3659   decl = lookup_literal_operator (name, args);
3660   if (decl && decl != error_mark_node)
3661     {
3662       result = finish_call_expr (decl, &args, false, true, tf_none);
3663       if (result != error_mark_node)
3664         {
3665           release_tree_vector (args);
3666           return result;
3667         }
3668     }
3669   release_tree_vector (args);
3670
3671   /* If the raw literal didn't work, look for a non-type template
3672      function with parameter pack char....  Call the function with
3673      template parameter characters representing the number.  */
3674   args = make_tree_vector ();
3675   decl = lookup_literal_operator (name, args);
3676   if (decl && decl != error_mark_node)
3677     {
3678       tree tmpl_args = make_char_string_pack (num_string);
3679       decl = lookup_template_function (decl, tmpl_args);
3680       result = finish_call_expr (decl, &args, false, true, tf_none);
3681       if (result != error_mark_node)
3682         {
3683           release_tree_vector (args);
3684           return result;
3685         }
3686     }
3687   release_tree_vector (args);
3688
3689   error ("unable to find numeric literal operator %qD", name);
3690   return error_mark_node;
3691 }
3692
3693 /* Parse a user-defined string constant.  Returns a call to a user-defined
3694    literal operator taking a character pointer and the length of the string
3695    as arguments.  */
3696
3697 static tree
3698 cp_parser_userdef_string_literal (cp_token *token)
3699 {
3700   tree literal = token->u.value;
3701   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3702   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3703   tree value = USERDEF_LITERAL_VALUE (literal);
3704   int len = TREE_STRING_LENGTH (value)
3705         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3706   tree decl, result;
3707
3708   /* Build up a call to the user-defined operator  */
3709   /* Lookup the name we got back from the id-expression.  */
3710   VEC(tree,gc) *args = make_tree_vector ();
3711   VEC_safe_push (tree, gc, args, value);
3712   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3713   decl = lookup_name (name);
3714   if (!decl || decl == error_mark_node)
3715     {
3716       error ("unable to find string literal operator %qD", name);
3717       release_tree_vector (args);
3718       return error_mark_node;
3719     }
3720   result = finish_call_expr (decl, &args, false, true, tf_none);
3721   release_tree_vector (args);
3722   if (result != error_mark_node)
3723     return result;
3724
3725   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3726          name, TREE_TYPE (value), size_type_node);
3727   return error_mark_node;
3728 }
3729
3730
3731 /* Basic concepts [gram.basic]  */
3732
3733 /* Parse a translation-unit.
3734
3735    translation-unit:
3736      declaration-seq [opt]
3737
3738    Returns TRUE if all went well.  */
3739
3740 static bool
3741 cp_parser_translation_unit (cp_parser* parser)
3742 {
3743   /* The address of the first non-permanent object on the declarator
3744      obstack.  */
3745   static void *declarator_obstack_base;
3746
3747   bool success;
3748
3749   /* Create the declarator obstack, if necessary.  */
3750   if (!cp_error_declarator)
3751     {
3752       gcc_obstack_init (&declarator_obstack);
3753       /* Create the error declarator.  */
3754       cp_error_declarator = make_declarator (cdk_error);
3755       /* Create the empty parameter list.  */
3756       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3757       /* Remember where the base of the declarator obstack lies.  */
3758       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3759     }
3760
3761   cp_parser_declaration_seq_opt (parser);
3762
3763   /* If there are no tokens left then all went well.  */
3764   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3765     {
3766       /* Get rid of the token array; we don't need it any more.  */
3767       cp_lexer_destroy (parser->lexer);
3768       parser->lexer = NULL;
3769
3770       /* This file might have been a context that's implicitly extern
3771          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3772       if (parser->implicit_extern_c)
3773         {
3774           pop_lang_context ();
3775           parser->implicit_extern_c = false;
3776         }
3777
3778       /* Finish up.  */
3779       finish_translation_unit ();
3780
3781       success = true;
3782     }
3783   else
3784     {
3785       cp_parser_error (parser, "expected declaration");
3786       success = false;
3787     }
3788
3789   /* Make sure the declarator obstack was fully cleaned up.  */
3790   gcc_assert (obstack_next_free (&declarator_obstack)
3791               == declarator_obstack_base);
3792
3793   /* All went well.  */
3794   return success;
3795 }
3796
3797 /* Expressions [gram.expr] */
3798
3799 /* Parse a primary-expression.
3800
3801    primary-expression:
3802      literal
3803      this
3804      ( expression )
3805      id-expression
3806
3807    GNU Extensions:
3808
3809    primary-expression:
3810      ( compound-statement )
3811      __builtin_va_arg ( assignment-expression , type-id )
3812      __builtin_offsetof ( type-id , offsetof-expression )
3813
3814    C++ Extensions:
3815      __has_nothrow_assign ( type-id )   
3816      __has_nothrow_constructor ( type-id )
3817      __has_nothrow_copy ( type-id )
3818      __has_trivial_assign ( type-id )   
3819      __has_trivial_constructor ( type-id )
3820      __has_trivial_copy ( type-id )
3821      __has_trivial_destructor ( type-id )
3822      __has_virtual_destructor ( type-id )     
3823      __is_abstract ( type-id )
3824      __is_base_of ( type-id , type-id )
3825      __is_class ( type-id )
3826      __is_convertible_to ( type-id , type-id )     
3827      __is_empty ( type-id )
3828      __is_enum ( type-id )
3829      __is_final ( type-id )
3830      __is_literal_type ( type-id )
3831      __is_pod ( type-id )
3832      __is_polymorphic ( type-id )
3833      __is_std_layout ( type-id )
3834      __is_trivial ( type-id )
3835      __is_union ( type-id )
3836
3837    Objective-C++ Extension:
3838
3839    primary-expression:
3840      objc-expression
3841
3842    literal:
3843      __null
3844
3845    ADDRESS_P is true iff this expression was immediately preceded by
3846    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3847    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3848    true iff this expression is a template argument.
3849
3850    Returns a representation of the expression.  Upon return, *IDK
3851    indicates what kind of id-expression (if any) was present.  */
3852
3853 static tree
3854 cp_parser_primary_expression (cp_parser *parser,
3855                               bool address_p,
3856                               bool cast_p,
3857                               bool template_arg_p,
3858                               cp_id_kind *idk)
3859 {
3860   cp_token *token = NULL;
3861
3862   /* Assume the primary expression is not an id-expression.  */
3863   *idk = CP_ID_KIND_NONE;
3864
3865   /* Peek at the next token.  */
3866   token = cp_lexer_peek_token (parser->lexer);
3867   switch (token->type)
3868     {
3869       /* literal:
3870            integer-literal
3871            character-literal
3872            floating-literal
3873            string-literal
3874            boolean-literal
3875            pointer-literal
3876            user-defined-literal  */
3877     case CPP_CHAR:
3878     case CPP_CHAR16:
3879     case CPP_CHAR32:
3880     case CPP_WCHAR:
3881     case CPP_NUMBER:
3882       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3883         return cp_parser_userdef_numeric_literal (parser);
3884       token = cp_lexer_consume_token (parser->lexer);
3885       if (TREE_CODE (token->u.value) == FIXED_CST)
3886         {
3887           error_at (token->location,
3888                     "fixed-point types not supported in C++");
3889           return error_mark_node;
3890         }
3891       /* Floating-point literals are only allowed in an integral
3892          constant expression if they are cast to an integral or
3893          enumeration type.  */
3894       if (TREE_CODE (token->u.value) == REAL_CST
3895           && parser->integral_constant_expression_p
3896           && pedantic)
3897         {
3898           /* CAST_P will be set even in invalid code like "int(2.7 +
3899              ...)".   Therefore, we have to check that the next token
3900              is sure to end the cast.  */
3901           if (cast_p)
3902             {
3903               cp_token *next_token;
3904
3905               next_token = cp_lexer_peek_token (parser->lexer);
3906               if (/* The comma at the end of an
3907                      enumerator-definition.  */
3908                   next_token->type != CPP_COMMA
3909                   /* The curly brace at the end of an enum-specifier.  */
3910                   && next_token->type != CPP_CLOSE_BRACE
3911                   /* The end of a statement.  */
3912                   && next_token->type != CPP_SEMICOLON
3913                   /* The end of the cast-expression.  */
3914                   && next_token->type != CPP_CLOSE_PAREN
3915                   /* The end of an array bound.  */
3916                   && next_token->type != CPP_CLOSE_SQUARE
3917                   /* The closing ">" in a template-argument-list.  */
3918                   && (next_token->type != CPP_GREATER
3919                       || parser->greater_than_is_operator_p)
3920                   /* C++0x only: A ">>" treated like two ">" tokens,
3921                      in a template-argument-list.  */
3922                   && (next_token->type != CPP_RSHIFT
3923                       || (cxx_dialect == cxx98)
3924                       || parser->greater_than_is_operator_p))
3925                 cast_p = false;
3926             }
3927
3928           /* If we are within a cast, then the constraint that the
3929              cast is to an integral or enumeration type will be
3930              checked at that point.  If we are not within a cast, then
3931              this code is invalid.  */
3932           if (!cast_p)
3933             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3934         }
3935       return token->u.value;
3936
3937     case CPP_CHAR_USERDEF:
3938     case CPP_CHAR16_USERDEF:
3939     case CPP_CHAR32_USERDEF:
3940     case CPP_WCHAR_USERDEF:
3941       return cp_parser_userdef_char_literal (parser);
3942
3943     case CPP_STRING:
3944     case CPP_STRING16:
3945     case CPP_STRING32:
3946     case CPP_WSTRING:
3947     case CPP_UTF8STRING:
3948     case CPP_STRING_USERDEF:
3949     case CPP_STRING16_USERDEF:
3950     case CPP_STRING32_USERDEF:
3951     case CPP_WSTRING_USERDEF:
3952     case CPP_UTF8STRING_USERDEF:
3953       /* ??? Should wide strings be allowed when parser->translate_strings_p
3954          is false (i.e. in attributes)?  If not, we can kill the third
3955          argument to cp_parser_string_literal.  */
3956       return cp_parser_string_literal (parser,
3957                                        parser->translate_strings_p,
3958                                        true);
3959
3960     case CPP_OPEN_PAREN:
3961       {
3962         tree expr;
3963         bool saved_greater_than_is_operator_p;
3964
3965         /* Consume the `('.  */
3966         cp_lexer_consume_token (parser->lexer);
3967         /* Within a parenthesized expression, a `>' token is always
3968            the greater-than operator.  */
3969         saved_greater_than_is_operator_p
3970           = parser->greater_than_is_operator_p;
3971         parser->greater_than_is_operator_p = true;
3972         /* If we see `( { ' then we are looking at the beginning of
3973            a GNU statement-expression.  */
3974         if (cp_parser_allow_gnu_extensions_p (parser)
3975             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3976           {
3977             /* Statement-expressions are not allowed by the standard.  */
3978             pedwarn (token->location, OPT_Wpedantic, 
3979                      "ISO C++ forbids braced-groups within expressions");
3980
3981             /* And they're not allowed outside of a function-body; you
3982                cannot, for example, write:
3983
3984                  int i = ({ int j = 3; j + 1; });
3985
3986                at class or namespace scope.  */
3987             if (!parser->in_function_body
3988                 || parser->in_template_argument_list_p)
3989               {
3990                 error_at (token->location,
3991                           "statement-expressions are not allowed outside "
3992                           "functions nor in template-argument lists");
3993                 cp_parser_skip_to_end_of_block_or_statement (parser);
3994                 expr = error_mark_node;
3995               }
3996             else
3997               {
3998                 /* Start the statement-expression.  */
3999                 expr = begin_stmt_expr ();
4000                 /* Parse the compound-statement.  */
4001                 cp_parser_compound_statement (parser, expr, false, false);
4002                 /* Finish up.  */
4003                 expr = finish_stmt_expr (expr, false);
4004               }
4005           }
4006         else
4007           {
4008             /* Parse the parenthesized expression.  */
4009             expr = cp_parser_expression (parser, cast_p, idk);
4010             /* Let the front end know that this expression was
4011                enclosed in parentheses. This matters in case, for
4012                example, the expression is of the form `A::B', since
4013                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4014                not.  */
4015             finish_parenthesized_expr (expr);
4016             /* DR 705: Wrapping an unqualified name in parentheses
4017                suppresses arg-dependent lookup.  We want to pass back
4018                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4019                (c++/37862), but none of the others.  */
4020             if (*idk != CP_ID_KIND_QUALIFIED)
4021               *idk = CP_ID_KIND_NONE;
4022           }
4023         /* The `>' token might be the end of a template-id or
4024            template-parameter-list now.  */
4025         parser->greater_than_is_operator_p
4026           = saved_greater_than_is_operator_p;
4027         /* Consume the `)'.  */
4028         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4029           cp_parser_skip_to_end_of_statement (parser);
4030
4031         return expr;
4032       }
4033
4034     case CPP_OPEN_SQUARE:
4035       if (c_dialect_objc ())
4036         /* We have an Objective-C++ message. */
4037         return cp_parser_objc_expression (parser);
4038       {
4039         tree lam = cp_parser_lambda_expression (parser);
4040         /* Don't warn about a failed tentative parse.  */
4041         if (cp_parser_error_occurred (parser))
4042           return error_mark_node;
4043         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4044         return lam;
4045       }
4046
4047     case CPP_OBJC_STRING:
4048       if (c_dialect_objc ())
4049         /* We have an Objective-C++ string literal. */
4050         return cp_parser_objc_expression (parser);
4051       cp_parser_error (parser, "expected primary-expression");
4052       return error_mark_node;
4053
4054     case CPP_KEYWORD:
4055       switch (token->keyword)
4056         {
4057           /* These two are the boolean literals.  */
4058         case RID_TRUE:
4059           cp_lexer_consume_token (parser->lexer);
4060           return boolean_true_node;
4061         case RID_FALSE:
4062           cp_lexer_consume_token (parser->lexer);
4063           return boolean_false_node;
4064
4065           /* The `__null' literal.  */
4066         case RID_NULL:
4067           cp_lexer_consume_token (parser->lexer);
4068           return null_node;
4069
4070           /* The `nullptr' literal.  */
4071         case RID_NULLPTR:
4072           cp_lexer_consume_token (parser->lexer);
4073           return nullptr_node;
4074
4075           /* Recognize the `this' keyword.  */
4076         case RID_THIS:
4077           cp_lexer_consume_token (parser->lexer);
4078           if (parser->local_variables_forbidden_p)
4079             {
4080               error_at (token->location,
4081                         "%<this%> may not be used in this context");
4082               return error_mark_node;
4083             }
4084           /* Pointers cannot appear in constant-expressions.  */
4085           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4086             return error_mark_node;
4087           return finish_this_expr ();
4088
4089           /* The `operator' keyword can be the beginning of an
4090              id-expression.  */
4091         case RID_OPERATOR:
4092           goto id_expression;
4093
4094         case RID_FUNCTION_NAME:
4095         case RID_PRETTY_FUNCTION_NAME:
4096         case RID_C99_FUNCTION_NAME:
4097           {
4098             non_integral_constant name;
4099
4100             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4101                __func__ are the names of variables -- but they are
4102                treated specially.  Therefore, they are handled here,
4103                rather than relying on the generic id-expression logic
4104                below.  Grammatically, these names are id-expressions.
4105
4106                Consume the token.  */
4107             token = cp_lexer_consume_token (parser->lexer);
4108
4109             switch (token->keyword)
4110               {
4111               case RID_FUNCTION_NAME:
4112                 name = NIC_FUNC_NAME;
4113                 break;
4114               case RID_PRETTY_FUNCTION_NAME:
4115                 name = NIC_PRETTY_FUNC;
4116                 break;
4117               case RID_C99_FUNCTION_NAME:
4118                 name = NIC_C99_FUNC;
4119                 break;
4120               default:
4121                 gcc_unreachable ();
4122               }
4123
4124             if (cp_parser_non_integral_constant_expression (parser, name))
4125               return error_mark_node;
4126
4127             /* Look up the name.  */
4128             return finish_fname (token->u.value);
4129           }
4130
4131         case RID_VA_ARG:
4132           {
4133             tree expression;
4134             tree type;
4135             source_location type_location;
4136
4137             /* The `__builtin_va_arg' construct is used to handle
4138                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4139             cp_lexer_consume_token (parser->lexer);
4140             /* Look for the opening `('.  */
4141             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4142             /* Now, parse the assignment-expression.  */
4143             expression = cp_parser_assignment_expression (parser,
4144                                                           /*cast_p=*/false, NULL);
4145             /* Look for the `,'.  */
4146             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4147             type_location = cp_lexer_peek_token (parser->lexer)->location;
4148             /* Parse the type-id.  */
4149             type = cp_parser_type_id (parser);
4150             /* Look for the closing `)'.  */
4151             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4152             /* Using `va_arg' in a constant-expression is not
4153                allowed.  */
4154             if (cp_parser_non_integral_constant_expression (parser,
4155                                                             NIC_VA_ARG))
4156               return error_mark_node;
4157             return build_x_va_arg (type_location, expression, type);
4158           }
4159
4160         case RID_OFFSETOF:
4161           return cp_parser_builtin_offsetof (parser);
4162
4163         case RID_HAS_NOTHROW_ASSIGN:
4164         case RID_HAS_NOTHROW_CONSTRUCTOR:
4165         case RID_HAS_NOTHROW_COPY:        
4166         case RID_HAS_TRIVIAL_ASSIGN:
4167         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4168         case RID_HAS_TRIVIAL_COPY:        
4169         case RID_HAS_TRIVIAL_DESTRUCTOR:
4170         case RID_HAS_VIRTUAL_DESTRUCTOR:
4171         case RID_IS_ABSTRACT:
4172         case RID_IS_BASE_OF:
4173         case RID_IS_CLASS:
4174         case RID_IS_CONVERTIBLE_TO:
4175         case RID_IS_EMPTY:
4176         case RID_IS_ENUM:
4177         case RID_IS_FINAL:
4178         case RID_IS_LITERAL_TYPE:
4179         case RID_IS_POD:
4180         case RID_IS_POLYMORPHIC:
4181         case RID_IS_STD_LAYOUT:
4182         case RID_IS_TRIVIAL:
4183         case RID_IS_UNION:
4184           return cp_parser_trait_expr (parser, token->keyword);
4185
4186         /* Objective-C++ expressions.  */
4187         case RID_AT_ENCODE:
4188         case RID_AT_PROTOCOL:
4189         case RID_AT_SELECTOR:
4190           return cp_parser_objc_expression (parser);
4191
4192         case RID_TEMPLATE:
4193           if (parser->in_function_body
4194               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4195                   == CPP_LESS))
4196             {
4197               error_at (token->location,
4198                         "a template declaration cannot appear at block scope");
4199               cp_parser_skip_to_end_of_block_or_statement (parser);
4200               return error_mark_node;
4201             }
4202         default:
4203           cp_parser_error (parser, "expected primary-expression");
4204           return error_mark_node;
4205         }
4206
4207       /* An id-expression can start with either an identifier, a
4208          `::' as the beginning of a qualified-id, or the "operator"
4209          keyword.  */
4210     case CPP_NAME:
4211     case CPP_SCOPE:
4212     case CPP_TEMPLATE_ID:
4213     case CPP_NESTED_NAME_SPECIFIER:
4214       {
4215         tree id_expression;
4216         tree decl;
4217         const char *error_msg;
4218         bool template_p;
4219         bool done;
4220         cp_token *id_expr_token;
4221
4222       id_expression:
4223         /* Parse the id-expression.  */
4224         id_expression
4225           = cp_parser_id_expression (parser,
4226                                      /*template_keyword_p=*/false,
4227                                      /*check_dependency_p=*/true,
4228                                      &template_p,
4229                                      /*declarator_p=*/false,
4230                                      /*optional_p=*/false);
4231         if (id_expression == error_mark_node)
4232           return error_mark_node;
4233         id_expr_token = token;
4234         token = cp_lexer_peek_token (parser->lexer);
4235         done = (token->type != CPP_OPEN_SQUARE
4236                 && token->type != CPP_OPEN_PAREN
4237                 && token->type != CPP_DOT
4238                 && token->type != CPP_DEREF
4239                 && token->type != CPP_PLUS_PLUS
4240                 && token->type != CPP_MINUS_MINUS);
4241         /* If we have a template-id, then no further lookup is
4242            required.  If the template-id was for a template-class, we
4243            will sometimes have a TYPE_DECL at this point.  */
4244         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4245                  || TREE_CODE (id_expression) == TYPE_DECL)
4246           decl = id_expression;
4247         /* Look up the name.  */
4248         else
4249           {
4250             tree ambiguous_decls;
4251
4252             /* If we already know that this lookup is ambiguous, then
4253                we've already issued an error message; there's no reason
4254                to check again.  */
4255             if (id_expr_token->type == CPP_NAME
4256                 && id_expr_token->ambiguous_p)
4257               {
4258                 cp_parser_simulate_error (parser);
4259                 return error_mark_node;
4260               }
4261
4262             decl = cp_parser_lookup_name (parser, id_expression,
4263                                           none_type,
4264                                           template_p,
4265                                           /*is_namespace=*/false,
4266                                           /*check_dependency=*/true,
4267                                           &ambiguous_decls,
4268                                           id_expr_token->location);
4269             /* If the lookup was ambiguous, an error will already have
4270                been issued.  */
4271             if (ambiguous_decls)
4272               return error_mark_node;
4273
4274             /* In Objective-C++, we may have an Objective-C 2.0
4275                dot-syntax for classes here.  */
4276             if (c_dialect_objc ()
4277                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4278                 && TREE_CODE (decl) == TYPE_DECL
4279                 && objc_is_class_name (decl))
4280               {
4281                 tree component;
4282                 cp_lexer_consume_token (parser->lexer);
4283                 component = cp_parser_identifier (parser);
4284                 if (component == error_mark_node)
4285                   return error_mark_node;
4286
4287                 return objc_build_class_component_ref (id_expression, component);
4288               }
4289
4290             /* In Objective-C++, an instance variable (ivar) may be preferred
4291                to whatever cp_parser_lookup_name() found.  */
4292             decl = objc_lookup_ivar (decl, id_expression);
4293
4294             /* If name lookup gives us a SCOPE_REF, then the
4295                qualifying scope was dependent.  */
4296             if (TREE_CODE (decl) == SCOPE_REF)
4297               {
4298                 /* At this point, we do not know if DECL is a valid
4299                    integral constant expression.  We assume that it is
4300                    in fact such an expression, so that code like:
4301
4302                       template <int N> struct A {
4303                         int a[B<N>::i];
4304                       };
4305                      
4306                    is accepted.  At template-instantiation time, we
4307                    will check that B<N>::i is actually a constant.  */
4308                 return decl;
4309               }
4310             /* Check to see if DECL is a local variable in a context
4311                where that is forbidden.  */
4312             if (parser->local_variables_forbidden_p
4313                 && local_variable_p (decl))
4314               {
4315                 /* It might be that we only found DECL because we are
4316                    trying to be generous with pre-ISO scoping rules.
4317                    For example, consider:
4318
4319                      int i;
4320                      void g() {
4321                        for (int i = 0; i < 10; ++i) {}
4322                        extern void f(int j = i);
4323                      }
4324
4325                    Here, name look up will originally find the out
4326                    of scope `i'.  We need to issue a warning message,
4327                    but then use the global `i'.  */
4328                 decl = check_for_out_of_scope_variable (decl);
4329                 if (local_variable_p (decl))
4330                   {
4331                     error_at (id_expr_token->location,
4332                               "local variable %qD may not appear in this context",
4333                               decl);
4334                     return error_mark_node;
4335                   }
4336               }
4337           }
4338
4339         decl = (finish_id_expression
4340                 (id_expression, decl, parser->scope,
4341                  idk,
4342                  parser->integral_constant_expression_p,
4343                  parser->allow_non_integral_constant_expression_p,
4344                  &parser->non_integral_constant_expression_p,
4345                  template_p, done, address_p,
4346                  template_arg_p,
4347                  &error_msg,
4348                  id_expr_token->location));
4349         if (error_msg)
4350           cp_parser_error (parser, error_msg);
4351         return decl;
4352       }
4353
4354       /* Anything else is an error.  */
4355     default:
4356       cp_parser_error (parser, "expected primary-expression");
4357       return error_mark_node;
4358     }
4359 }
4360
4361 /* Parse an id-expression.
4362
4363    id-expression:
4364      unqualified-id
4365      qualified-id
4366
4367    qualified-id:
4368      :: [opt] nested-name-specifier template [opt] unqualified-id
4369      :: identifier
4370      :: operator-function-id
4371      :: template-id
4372
4373    Return a representation of the unqualified portion of the
4374    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4375    a `::' or nested-name-specifier.
4376
4377    Often, if the id-expression was a qualified-id, the caller will
4378    want to make a SCOPE_REF to represent the qualified-id.  This
4379    function does not do this in order to avoid wastefully creating
4380    SCOPE_REFs when they are not required.
4381
4382    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4383    `template' keyword.
4384
4385    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4386    uninstantiated templates.
4387
4388    If *TEMPLATE_P is non-NULL, it is set to true iff the
4389    `template' keyword is used to explicitly indicate that the entity
4390    named is a template.
4391
4392    If DECLARATOR_P is true, the id-expression is appearing as part of
4393    a declarator, rather than as part of an expression.  */
4394
4395 static tree
4396 cp_parser_id_expression (cp_parser *parser,
4397                          bool template_keyword_p,
4398                          bool check_dependency_p,
4399                          bool *template_p,
4400                          bool declarator_p,
4401                          bool optional_p)
4402 {
4403   bool global_scope_p;
4404   bool nested_name_specifier_p;
4405
4406   /* Assume the `template' keyword was not used.  */
4407   if (template_p)
4408     *template_p = template_keyword_p;
4409
4410   /* Look for the optional `::' operator.  */
4411   global_scope_p
4412     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4413        != NULL_TREE);
4414   /* Look for the optional nested-name-specifier.  */
4415   nested_name_specifier_p
4416     = (cp_parser_nested_name_specifier_opt (parser,
4417                                             /*typename_keyword_p=*/false,
4418                                             check_dependency_p,
4419                                             /*type_p=*/false,
4420                                             declarator_p)
4421        != NULL_TREE);
4422   /* If there is a nested-name-specifier, then we are looking at
4423      the first qualified-id production.  */
4424   if (nested_name_specifier_p)
4425     {
4426       tree saved_scope;
4427       tree saved_object_scope;
4428       tree saved_qualifying_scope;
4429       tree unqualified_id;
4430       bool is_template;
4431
4432       /* See if the next token is the `template' keyword.  */
4433       if (!template_p)
4434         template_p = &is_template;
4435       *template_p = cp_parser_optional_template_keyword (parser);
4436       /* Name lookup we do during the processing of the
4437          unqualified-id might obliterate SCOPE.  */
4438       saved_scope = parser->scope;
4439       saved_object_scope = parser->object_scope;
4440       saved_qualifying_scope = parser->qualifying_scope;
4441       /* Process the final unqualified-id.  */
4442       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4443                                                  check_dependency_p,
4444                                                  declarator_p,
4445                                                  /*optional_p=*/false);
4446       /* Restore the SAVED_SCOPE for our caller.  */
4447       parser->scope = saved_scope;
4448       parser->object_scope = saved_object_scope;
4449       parser->qualifying_scope = saved_qualifying_scope;
4450
4451       return unqualified_id;
4452     }
4453   /* Otherwise, if we are in global scope, then we are looking at one
4454      of the other qualified-id productions.  */
4455   else if (global_scope_p)
4456     {
4457       cp_token *token;
4458       tree id;
4459
4460       /* Peek at the next token.  */
4461       token = cp_lexer_peek_token (parser->lexer);
4462
4463       /* If it's an identifier, and the next token is not a "<", then
4464          we can avoid the template-id case.  This is an optimization
4465          for this common case.  */
4466       if (token->type == CPP_NAME
4467           && !cp_parser_nth_token_starts_template_argument_list_p
4468                (parser, 2))
4469         return cp_parser_identifier (parser);
4470
4471       cp_parser_parse_tentatively (parser);
4472       /* Try a template-id.  */
4473       id = cp_parser_template_id (parser,
4474                                   /*template_keyword_p=*/false,
4475                                   /*check_dependency_p=*/true,
4476                                   none_type,
4477                                   declarator_p);
4478       /* If that worked, we're done.  */
4479       if (cp_parser_parse_definitely (parser))
4480         return id;
4481
4482       /* Peek at the next token.  (Changes in the token buffer may
4483          have invalidated the pointer obtained above.)  */
4484       token = cp_lexer_peek_token (parser->lexer);
4485
4486       switch (token->type)
4487         {
4488         case CPP_NAME:
4489           return cp_parser_identifier (parser);
4490
4491         case CPP_KEYWORD:
4492           if (token->keyword == RID_OPERATOR)
4493             return cp_parser_operator_function_id (parser);
4494           /* Fall through.  */
4495
4496         default:
4497           cp_parser_error (parser, "expected id-expression");
4498           return error_mark_node;
4499         }
4500     }
4501   else
4502     return cp_parser_unqualified_id (parser, template_keyword_p,
4503                                      /*check_dependency_p=*/true,
4504                                      declarator_p,
4505                                      optional_p);
4506 }
4507
4508 /* Parse an unqualified-id.
4509
4510    unqualified-id:
4511      identifier
4512      operator-function-id
4513      conversion-function-id
4514      ~ class-name
4515      template-id
4516
4517    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4518    keyword, in a construct like `A::template ...'.
4519
4520    Returns a representation of unqualified-id.  For the `identifier'
4521    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4522    production a BIT_NOT_EXPR is returned; the operand of the
4523    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4524    other productions, see the documentation accompanying the
4525    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4526    names are looked up in uninstantiated templates.  If DECLARATOR_P
4527    is true, the unqualified-id is appearing as part of a declarator,
4528    rather than as part of an expression.  */
4529
4530 static tree
4531 cp_parser_unqualified_id (cp_parser* parser,
4532                           bool template_keyword_p,
4533                           bool check_dependency_p,
4534                           bool declarator_p,
4535                           bool optional_p)
4536 {
4537   cp_token *token;
4538
4539   /* Peek at the next token.  */
4540   token = cp_lexer_peek_token (parser->lexer);
4541
4542   switch (token->type)
4543     {
4544     case CPP_NAME:
4545       {
4546         tree id;
4547
4548         /* We don't know yet whether or not this will be a
4549            template-id.  */
4550         cp_parser_parse_tentatively (parser);
4551         /* Try a template-id.  */
4552         id = cp_parser_template_id (parser, template_keyword_p,
4553                                     check_dependency_p,
4554                                     none_type,
4555                                     declarator_p);
4556         /* If it worked, we're done.  */
4557         if (cp_parser_parse_definitely (parser))
4558           return id;
4559         /* Otherwise, it's an ordinary identifier.  */
4560         return cp_parser_identifier (parser);
4561       }
4562
4563     case CPP_TEMPLATE_ID:
4564       return cp_parser_template_id (parser, template_keyword_p,
4565                                     check_dependency_p,
4566                                     none_type,
4567                                     declarator_p);
4568
4569     case CPP_COMPL:
4570       {
4571         tree type_decl;
4572         tree qualifying_scope;
4573         tree object_scope;
4574         tree scope;
4575         bool done;
4576
4577         /* Consume the `~' token.  */
4578         cp_lexer_consume_token (parser->lexer);
4579         /* Parse the class-name.  The standard, as written, seems to
4580            say that:
4581
4582              template <typename T> struct S { ~S (); };
4583              template <typename T> S<T>::~S() {}
4584
4585            is invalid, since `~' must be followed by a class-name, but
4586            `S<T>' is dependent, and so not known to be a class.
4587            That's not right; we need to look in uninstantiated
4588            templates.  A further complication arises from:
4589
4590              template <typename T> void f(T t) {
4591                t.T::~T();
4592              }
4593
4594            Here, it is not possible to look up `T' in the scope of `T'
4595            itself.  We must look in both the current scope, and the
4596            scope of the containing complete expression.
4597
4598            Yet another issue is:
4599
4600              struct S {
4601                int S;
4602                ~S();
4603              };
4604
4605              S::~S() {}
4606
4607            The standard does not seem to say that the `S' in `~S'
4608            should refer to the type `S' and not the data member
4609            `S::S'.  */
4610
4611         /* DR 244 says that we look up the name after the "~" in the
4612            same scope as we looked up the qualifying name.  That idea
4613            isn't fully worked out; it's more complicated than that.  */
4614         scope = parser->scope;
4615         object_scope = parser->object_scope;
4616         qualifying_scope = parser->qualifying_scope;
4617
4618         /* Check for invalid scopes.  */
4619         if (scope == error_mark_node)
4620           {
4621             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4622               cp_lexer_consume_token (parser->lexer);
4623             return error_mark_node;
4624           }
4625         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4626           {
4627             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4628               error_at (token->location,
4629                         "scope %qT before %<~%> is not a class-name",
4630                         scope);
4631             cp_parser_simulate_error (parser);
4632             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4633               cp_lexer_consume_token (parser->lexer);
4634             return error_mark_node;
4635           }
4636         gcc_assert (!scope || TYPE_P (scope));
4637
4638         /* If the name is of the form "X::~X" it's OK even if X is a
4639            typedef.  */
4640         token = cp_lexer_peek_token (parser->lexer);
4641         if (scope
4642             && token->type == CPP_NAME
4643             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4644                 != CPP_LESS)
4645             && (token->u.value == TYPE_IDENTIFIER (scope)
4646                 || (CLASS_TYPE_P (scope)
4647                     && constructor_name_p (token->u.value, scope))))
4648           {
4649             cp_lexer_consume_token (parser->lexer);
4650             return build_nt (BIT_NOT_EXPR, scope);
4651           }
4652
4653         /* If there was an explicit qualification (S::~T), first look
4654            in the scope given by the qualification (i.e., S).
4655
4656            Note: in the calls to cp_parser_class_name below we pass
4657            typename_type so that lookup finds the injected-class-name
4658            rather than the constructor.  */
4659         done = false;
4660         type_decl = NULL_TREE;
4661         if (scope)
4662           {
4663             cp_parser_parse_tentatively (parser);
4664             type_decl = cp_parser_class_name (parser,
4665                                               /*typename_keyword_p=*/false,
4666                                               /*template_keyword_p=*/false,
4667                                               typename_type,
4668                                               /*check_dependency=*/false,
4669                                               /*class_head_p=*/false,
4670                                               declarator_p);
4671             if (cp_parser_parse_definitely (parser))
4672               done = true;
4673           }
4674         /* In "N::S::~S", look in "N" as well.  */
4675         if (!done && scope && qualifying_scope)
4676           {
4677             cp_parser_parse_tentatively (parser);
4678             parser->scope = qualifying_scope;
4679             parser->object_scope = NULL_TREE;
4680             parser->qualifying_scope = NULL_TREE;
4681             type_decl
4682               = cp_parser_class_name (parser,
4683                                       /*typename_keyword_p=*/false,
4684                                       /*template_keyword_p=*/false,
4685                                       typename_type,
4686                                       /*check_dependency=*/false,
4687                                       /*class_head_p=*/false,
4688                                       declarator_p);
4689             if (cp_parser_parse_definitely (parser))
4690               done = true;
4691           }
4692         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4693         else if (!done && object_scope)
4694           {
4695             cp_parser_parse_tentatively (parser);
4696             parser->scope = object_scope;
4697             parser->object_scope = NULL_TREE;
4698             parser->qualifying_scope = NULL_TREE;
4699             type_decl
4700               = cp_parser_class_name (parser,
4701                                       /*typename_keyword_p=*/false,
4702                                       /*template_keyword_p=*/false,
4703                                       typename_type,
4704                                       /*check_dependency=*/false,
4705                                       /*class_head_p=*/false,
4706                                       declarator_p);
4707             if (cp_parser_parse_definitely (parser))
4708               done = true;
4709           }
4710         /* Look in the surrounding context.  */
4711         if (!done)
4712           {
4713             parser->scope = NULL_TREE;
4714             parser->object_scope = NULL_TREE;
4715             parser->qualifying_scope = NULL_TREE;
4716             if (processing_template_decl)
4717               cp_parser_parse_tentatively (parser);
4718             type_decl
4719               = cp_parser_class_name (parser,
4720                                       /*typename_keyword_p=*/false,
4721                                       /*template_keyword_p=*/false,
4722                                       typename_type,
4723                                       /*check_dependency=*/false,
4724                                       /*class_head_p=*/false,
4725                                       declarator_p);
4726             if (processing_template_decl
4727                 && ! cp_parser_parse_definitely (parser))
4728               {
4729                 /* We couldn't find a type with this name, so just accept
4730                    it and check for a match at instantiation time.  */
4731                 type_decl = cp_parser_identifier (parser);
4732                 if (type_decl != error_mark_node)
4733                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4734                 return type_decl;
4735               }
4736           }
4737         /* If an error occurred, assume that the name of the
4738            destructor is the same as the name of the qualifying
4739            class.  That allows us to keep parsing after running
4740            into ill-formed destructor names.  */
4741         if (type_decl == error_mark_node && scope)
4742           return build_nt (BIT_NOT_EXPR, scope);
4743         else if (type_decl == error_mark_node)
4744           return error_mark_node;
4745
4746         /* Check that destructor name and scope match.  */
4747         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4748           {
4749             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4750               error_at (token->location,
4751                         "declaration of %<~%T%> as member of %qT",
4752                         type_decl, scope);
4753             cp_parser_simulate_error (parser);
4754             return error_mark_node;
4755           }
4756
4757         /* [class.dtor]
4758
4759            A typedef-name that names a class shall not be used as the
4760            identifier in the declarator for a destructor declaration.  */
4761         if (declarator_p
4762             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4763             && !DECL_SELF_REFERENCE_P (type_decl)
4764             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4765           error_at (token->location,
4766                     "typedef-name %qD used as destructor declarator",
4767                     type_decl);
4768
4769         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4770       }
4771
4772     case CPP_KEYWORD:
4773       if (token->keyword == RID_OPERATOR)
4774         {
4775           tree id;
4776
4777           /* This could be a template-id, so we try that first.  */
4778           cp_parser_parse_tentatively (parser);
4779           /* Try a template-id.  */
4780           id = cp_parser_template_id (parser, template_keyword_p,
4781                                       /*check_dependency_p=*/true,
4782                                       none_type,
4783                                       declarator_p);
4784           /* If that worked, we're done.  */
4785           if (cp_parser_parse_definitely (parser))
4786             return id;
4787           /* We still don't know whether we're looking at an
4788              operator-function-id or a conversion-function-id.  */
4789           cp_parser_parse_tentatively (parser);
4790           /* Try an operator-function-id.  */
4791           id = cp_parser_operator_function_id (parser);
4792           /* If that didn't work, try a conversion-function-id.  */
4793           if (!cp_parser_parse_definitely (parser))
4794             id = cp_parser_conversion_function_id (parser);
4795           else if (UDLIT_OPER_P (id))
4796             {
4797               /* 17.6.3.3.5  */
4798               const char *name = UDLIT_OP_SUFFIX (id);
4799               if (name[0] != '_' && !in_system_header)
4800                 warning (0, "literal operator suffixes not preceded by %<_%>"
4801                             " are reserved for future standardization");
4802             }
4803
4804           return id;
4805         }
4806       /* Fall through.  */
4807
4808     default:
4809       if (optional_p)
4810         return NULL_TREE;
4811       cp_parser_error (parser, "expected unqualified-id");
4812       return error_mark_node;
4813     }
4814 }
4815
4816 /* Parse an (optional) nested-name-specifier.
4817
4818    nested-name-specifier: [C++98]
4819      class-or-namespace-name :: nested-name-specifier [opt]
4820      class-or-namespace-name :: template nested-name-specifier [opt]
4821
4822    nested-name-specifier: [C++0x]
4823      type-name ::
4824      namespace-name ::
4825      nested-name-specifier identifier ::
4826      nested-name-specifier template [opt] simple-template-id ::
4827
4828    PARSER->SCOPE should be set appropriately before this function is
4829    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4830    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4831    in name lookups.
4832
4833    Sets PARSER->SCOPE to the class (TYPE) or namespace
4834    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4835    it unchanged if there is no nested-name-specifier.  Returns the new
4836    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4837
4838    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4839    part of a declaration and/or decl-specifier.  */
4840
4841 static tree
4842 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4843                                      bool typename_keyword_p,
4844                                      bool check_dependency_p,
4845                                      bool type_p,
4846                                      bool is_declaration)
4847 {
4848   bool success = false;
4849   cp_token_position start = 0;
4850   cp_token *token;
4851
4852   /* Remember where the nested-name-specifier starts.  */
4853   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4854     {
4855       start = cp_lexer_token_position (parser->lexer, false);
4856       push_deferring_access_checks (dk_deferred);
4857     }
4858
4859   while (true)
4860     {
4861       tree new_scope;
4862       tree old_scope;
4863       tree saved_qualifying_scope;
4864       bool template_keyword_p;
4865
4866       /* Spot cases that cannot be the beginning of a
4867          nested-name-specifier.  */
4868       token = cp_lexer_peek_token (parser->lexer);
4869
4870       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4871          the already parsed nested-name-specifier.  */
4872       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4873         {
4874           /* Grab the nested-name-specifier and continue the loop.  */
4875           cp_parser_pre_parsed_nested_name_specifier (parser);
4876           /* If we originally encountered this nested-name-specifier
4877              with IS_DECLARATION set to false, we will not have
4878              resolved TYPENAME_TYPEs, so we must do so here.  */
4879           if (is_declaration
4880               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4881             {
4882               new_scope = resolve_typename_type (parser->scope,
4883                                                  /*only_current_p=*/false);
4884               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4885                 parser->scope = new_scope;
4886             }
4887           success = true;
4888           continue;
4889         }
4890
4891       /* Spot cases that cannot be the beginning of a
4892          nested-name-specifier.  On the second and subsequent times
4893          through the loop, we look for the `template' keyword.  */
4894       if (success && token->keyword == RID_TEMPLATE)
4895         ;
4896       /* A template-id can start a nested-name-specifier.  */
4897       else if (token->type == CPP_TEMPLATE_ID)
4898         ;
4899       /* DR 743: decltype can be used in a nested-name-specifier.  */
4900       else if (token_is_decltype (token))
4901         ;
4902       else
4903         {
4904           /* If the next token is not an identifier, then it is
4905              definitely not a type-name or namespace-name.  */
4906           if (token->type != CPP_NAME)
4907             break;
4908           /* If the following token is neither a `<' (to begin a
4909              template-id), nor a `::', then we are not looking at a
4910              nested-name-specifier.  */
4911           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4912
4913           if (token->type == CPP_COLON
4914               && parser->colon_corrects_to_scope_p
4915               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4916             {
4917               error_at (token->location,
4918                         "found %<:%> in nested-name-specifier, expected %<::%>");
4919               token->type = CPP_SCOPE;
4920             }
4921
4922           if (token->type != CPP_SCOPE
4923               && !cp_parser_nth_token_starts_template_argument_list_p
4924                   (parser, 2))
4925             break;
4926         }
4927
4928       /* The nested-name-specifier is optional, so we parse
4929          tentatively.  */
4930       cp_parser_parse_tentatively (parser);
4931
4932       /* Look for the optional `template' keyword, if this isn't the
4933          first time through the loop.  */
4934       if (success)
4935         template_keyword_p = cp_parser_optional_template_keyword (parser);
4936       else
4937         template_keyword_p = false;
4938
4939       /* Save the old scope since the name lookup we are about to do
4940          might destroy it.  */
4941       old_scope = parser->scope;
4942       saved_qualifying_scope = parser->qualifying_scope;
4943       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4944          look up names in "X<T>::I" in order to determine that "Y" is
4945          a template.  So, if we have a typename at this point, we make
4946          an effort to look through it.  */
4947       if (is_declaration
4948           && !typename_keyword_p
4949           && parser->scope
4950           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4951         parser->scope = resolve_typename_type (parser->scope,
4952                                                /*only_current_p=*/false);
4953       /* Parse the qualifying entity.  */
4954       new_scope
4955         = cp_parser_qualifying_entity (parser,
4956                                        typename_keyword_p,
4957                                        template_keyword_p,
4958                                        check_dependency_p,
4959                                        type_p,
4960                                        is_declaration);
4961       /* Look for the `::' token.  */
4962       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4963
4964       /* If we found what we wanted, we keep going; otherwise, we're
4965          done.  */
4966       if (!cp_parser_parse_definitely (parser))
4967         {
4968           bool error_p = false;
4969
4970           /* Restore the OLD_SCOPE since it was valid before the
4971              failed attempt at finding the last
4972              class-or-namespace-name.  */
4973           parser->scope = old_scope;
4974           parser->qualifying_scope = saved_qualifying_scope;
4975
4976           /* If the next token is a decltype, and the one after that is a
4977              `::', then the decltype has failed to resolve to a class or
4978              enumeration type.  Give this error even when parsing
4979              tentatively since it can't possibly be valid--and we're going
4980              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4981              won't get another chance.*/
4982           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4983               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4984                   == CPP_SCOPE))
4985             {
4986               token = cp_lexer_consume_token (parser->lexer);
4987               error_at (token->location, "decltype evaluates to %qT, "
4988                         "which is not a class or enumeration type",
4989                         token->u.value);
4990               parser->scope = error_mark_node;
4991               error_p = true;
4992               /* As below.  */
4993               success = true;
4994               cp_lexer_consume_token (parser->lexer);
4995             }
4996
4997           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4998             break;
4999           /* If the next token is an identifier, and the one after
5000              that is a `::', then any valid interpretation would have
5001              found a class-or-namespace-name.  */
5002           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5003                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5004                      == CPP_SCOPE)
5005                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5006                      != CPP_COMPL))
5007             {
5008               token = cp_lexer_consume_token (parser->lexer);
5009               if (!error_p)
5010                 {
5011                   if (!token->ambiguous_p)
5012                     {
5013                       tree decl;
5014                       tree ambiguous_decls;
5015
5016                       decl = cp_parser_lookup_name (parser, token->u.value,
5017                                                     none_type,
5018                                                     /*is_template=*/false,
5019                                                     /*is_namespace=*/false,
5020                                                     /*check_dependency=*/true,
5021                                                     &ambiguous_decls,
5022                                                     token->location);
5023                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5024                         error_at (token->location,
5025                                   "%qD used without template parameters",
5026                                   decl);
5027                       else if (ambiguous_decls)
5028                         {
5029                           error_at (token->location,
5030                                     "reference to %qD is ambiguous",
5031                                     token->u.value);
5032                           print_candidates (ambiguous_decls);
5033                           decl = error_mark_node;
5034                         }
5035                       else
5036                         {
5037                           if (cxx_dialect != cxx98)
5038                             cp_parser_name_lookup_error
5039                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5040                              token->location);
5041                           else
5042                             cp_parser_name_lookup_error
5043                             (parser, token->u.value, decl, NLE_CXX98,
5044                              token->location);
5045                         }
5046                     }
5047                   parser->scope = error_mark_node;
5048                   error_p = true;
5049                   /* Treat this as a successful nested-name-specifier
5050                      due to:
5051
5052                      [basic.lookup.qual]
5053
5054                      If the name found is not a class-name (clause
5055                      _class_) or namespace-name (_namespace.def_), the
5056                      program is ill-formed.  */
5057                   success = true;
5058                 }
5059               cp_lexer_consume_token (parser->lexer);
5060             }
5061           break;
5062         }
5063       /* We've found one valid nested-name-specifier.  */
5064       success = true;
5065       /* Name lookup always gives us a DECL.  */
5066       if (TREE_CODE (new_scope) == TYPE_DECL)
5067         new_scope = TREE_TYPE (new_scope);
5068       /* Uses of "template" must be followed by actual templates.  */
5069       if (template_keyword_p
5070           && !(CLASS_TYPE_P (new_scope)
5071                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5072                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5073                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5074           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5075                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5076                    == TEMPLATE_ID_EXPR)))
5077         permerror (input_location, TYPE_P (new_scope)
5078                    ? G_("%qT is not a template")
5079                    : G_("%qD is not a template"),
5080                    new_scope);
5081       /* If it is a class scope, try to complete it; we are about to
5082          be looking up names inside the class.  */
5083       if (TYPE_P (new_scope)
5084           /* Since checking types for dependency can be expensive,
5085              avoid doing it if the type is already complete.  */
5086           && !COMPLETE_TYPE_P (new_scope)
5087           /* Do not try to complete dependent types.  */
5088           && !dependent_type_p (new_scope))
5089         {
5090           new_scope = complete_type (new_scope);
5091           /* If it is a typedef to current class, use the current
5092              class instead, as the typedef won't have any names inside
5093              it yet.  */
5094           if (!COMPLETE_TYPE_P (new_scope)
5095               && currently_open_class (new_scope))
5096             new_scope = TYPE_MAIN_VARIANT (new_scope);
5097         }
5098       /* Make sure we look in the right scope the next time through
5099          the loop.  */
5100       parser->scope = new_scope;
5101     }
5102
5103   /* If parsing tentatively, replace the sequence of tokens that makes
5104      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5105      token.  That way, should we re-parse the token stream, we will
5106      not have to repeat the effort required to do the parse, nor will
5107      we issue duplicate error messages.  */
5108   if (success && start)
5109     {
5110       cp_token *token;
5111
5112       token = cp_lexer_token_at (parser->lexer, start);
5113       /* Reset the contents of the START token.  */
5114       token->type = CPP_NESTED_NAME_SPECIFIER;
5115       /* Retrieve any deferred checks.  Do not pop this access checks yet
5116          so the memory will not be reclaimed during token replacing below.  */
5117       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5118       token->u.tree_check_value->value = parser->scope;
5119       token->u.tree_check_value->checks = get_deferred_access_checks ();
5120       token->u.tree_check_value->qualifying_scope =
5121         parser->qualifying_scope;
5122       token->keyword = RID_MAX;
5123
5124       /* Purge all subsequent tokens.  */
5125       cp_lexer_purge_tokens_after (parser->lexer, start);
5126     }
5127
5128   if (start)
5129     pop_to_parent_deferring_access_checks ();
5130
5131   return success ? parser->scope : NULL_TREE;
5132 }
5133
5134 /* Parse a nested-name-specifier.  See
5135    cp_parser_nested_name_specifier_opt for details.  This function
5136    behaves identically, except that it will an issue an error if no
5137    nested-name-specifier is present.  */
5138
5139 static tree
5140 cp_parser_nested_name_specifier (cp_parser *parser,
5141                                  bool typename_keyword_p,
5142                                  bool check_dependency_p,
5143                                  bool type_p,
5144                                  bool is_declaration)
5145 {
5146   tree scope;
5147
5148   /* Look for the nested-name-specifier.  */
5149   scope = cp_parser_nested_name_specifier_opt (parser,
5150                                                typename_keyword_p,
5151                                                check_dependency_p,
5152                                                type_p,
5153                                                is_declaration);
5154   /* If it was not present, issue an error message.  */
5155   if (!scope)
5156     {
5157       cp_parser_error (parser, "expected nested-name-specifier");
5158       parser->scope = NULL_TREE;
5159     }
5160
5161   return scope;
5162 }
5163
5164 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5165    this is either a class-name or a namespace-name (which corresponds
5166    to the class-or-namespace-name production in the grammar). For
5167    C++0x, it can also be a type-name that refers to an enumeration
5168    type or a simple-template-id.
5169
5170    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5171    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5172    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5173    TYPE_P is TRUE iff the next name should be taken as a class-name,
5174    even the same name is declared to be another entity in the same
5175    scope.
5176
5177    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5178    specified by the class-or-namespace-name.  If neither is found the
5179    ERROR_MARK_NODE is returned.  */
5180
5181 static tree
5182 cp_parser_qualifying_entity (cp_parser *parser,
5183                              bool typename_keyword_p,
5184                              bool template_keyword_p,
5185                              bool check_dependency_p,
5186                              bool type_p,
5187                              bool is_declaration)
5188 {
5189   tree saved_scope;
5190   tree saved_qualifying_scope;
5191   tree saved_object_scope;
5192   tree scope;
5193   bool only_class_p;
5194   bool successful_parse_p;
5195
5196   /* DR 743: decltype can appear in a nested-name-specifier.  */
5197   if (cp_lexer_next_token_is_decltype (parser->lexer))
5198     {
5199       scope = cp_parser_decltype (parser);
5200       if (TREE_CODE (scope) != ENUMERAL_TYPE
5201           && !MAYBE_CLASS_TYPE_P (scope))
5202         {
5203           cp_parser_simulate_error (parser);
5204           return error_mark_node;
5205         }
5206       if (TYPE_NAME (scope))
5207         scope = TYPE_NAME (scope);
5208       return scope;
5209     }
5210
5211   /* Before we try to parse the class-name, we must save away the
5212      current PARSER->SCOPE since cp_parser_class_name will destroy
5213      it.  */
5214   saved_scope = parser->scope;
5215   saved_qualifying_scope = parser->qualifying_scope;
5216   saved_object_scope = parser->object_scope;
5217   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5218      there is no need to look for a namespace-name.  */
5219   only_class_p = template_keyword_p 
5220     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5221   if (!only_class_p)
5222     cp_parser_parse_tentatively (parser);
5223   scope = cp_parser_class_name (parser,
5224                                 typename_keyword_p,
5225                                 template_keyword_p,
5226                                 type_p ? class_type : none_type,
5227                                 check_dependency_p,
5228                                 /*class_head_p=*/false,
5229                                 is_declaration);
5230   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5231   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5232   if (!only_class_p 
5233       && cxx_dialect != cxx98
5234       && !successful_parse_p)
5235     {
5236       /* Restore the saved scope.  */
5237       parser->scope = saved_scope;
5238       parser->qualifying_scope = saved_qualifying_scope;
5239       parser->object_scope = saved_object_scope;
5240
5241       /* Parse tentatively.  */
5242       cp_parser_parse_tentatively (parser);
5243      
5244       /* Parse a type-name  */
5245       scope = cp_parser_type_name (parser);
5246
5247       /* "If the name found does not designate a namespace or a class,
5248          enumeration, or dependent type, the program is ill-formed."
5249
5250          We cover classes and dependent types above and namespaces below,
5251          so this code is only looking for enums.  */
5252       if (!scope || TREE_CODE (scope) != TYPE_DECL
5253           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5254         cp_parser_simulate_error (parser);
5255
5256       successful_parse_p = cp_parser_parse_definitely (parser);
5257     }
5258   /* If that didn't work, try for a namespace-name.  */
5259   if (!only_class_p && !successful_parse_p)
5260     {
5261       /* Restore the saved scope.  */
5262       parser->scope = saved_scope;
5263       parser->qualifying_scope = saved_qualifying_scope;
5264       parser->object_scope = saved_object_scope;
5265       /* If we are not looking at an identifier followed by the scope
5266          resolution operator, then this is not part of a
5267          nested-name-specifier.  (Note that this function is only used
5268          to parse the components of a nested-name-specifier.)  */
5269       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5270           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5271         return error_mark_node;
5272       scope = cp_parser_namespace_name (parser);
5273     }
5274
5275   return scope;
5276 }
5277
5278 /* Parse a postfix-expression.
5279
5280    postfix-expression:
5281      primary-expression
5282      postfix-expression [ expression ]
5283      postfix-expression ( expression-list [opt] )
5284      simple-type-specifier ( expression-list [opt] )
5285      typename :: [opt] nested-name-specifier identifier
5286        ( expression-list [opt] )
5287      typename :: [opt] nested-name-specifier template [opt] template-id
5288        ( expression-list [opt] )
5289      postfix-expression . template [opt] id-expression
5290      postfix-expression -> template [opt] id-expression
5291      postfix-expression . pseudo-destructor-name
5292      postfix-expression -> pseudo-destructor-name
5293      postfix-expression ++
5294      postfix-expression --
5295      dynamic_cast < type-id > ( expression )
5296      static_cast < type-id > ( expression )
5297      reinterpret_cast < type-id > ( expression )
5298      const_cast < type-id > ( expression )
5299      typeid ( expression )
5300      typeid ( type-id )
5301
5302    GNU Extension:
5303
5304    postfix-expression:
5305      ( type-id ) { initializer-list , [opt] }
5306
5307    This extension is a GNU version of the C99 compound-literal
5308    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5309    but they are essentially the same concept.)
5310
5311    If ADDRESS_P is true, the postfix expression is the operand of the
5312    `&' operator.  CAST_P is true if this expression is the target of a
5313    cast.
5314
5315    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5316    class member access expressions [expr.ref].
5317
5318    Returns a representation of the expression.  */
5319
5320 static tree
5321 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5322                               bool member_access_only_p,
5323                               cp_id_kind * pidk_return)
5324 {
5325   cp_token *token;
5326   enum rid keyword;
5327   cp_id_kind idk = CP_ID_KIND_NONE;
5328   tree postfix_expression = NULL_TREE;
5329   bool is_member_access = false;
5330
5331   /* Peek at the next token.  */
5332   token = cp_lexer_peek_token (parser->lexer);
5333   /* Some of the productions are determined by keywords.  */
5334   keyword = token->keyword;
5335   switch (keyword)
5336     {
5337     case RID_DYNCAST:
5338     case RID_STATCAST:
5339     case RID_REINTCAST:
5340     case RID_CONSTCAST:
5341       {
5342         tree type;
5343         tree expression;
5344         const char *saved_message;
5345
5346         /* All of these can be handled in the same way from the point
5347            of view of parsing.  Begin by consuming the token
5348            identifying the cast.  */
5349         cp_lexer_consume_token (parser->lexer);
5350
5351         /* New types cannot be defined in the cast.  */
5352         saved_message = parser->type_definition_forbidden_message;
5353         parser->type_definition_forbidden_message
5354           = G_("types may not be defined in casts");
5355
5356         /* Look for the opening `<'.  */
5357         cp_parser_require (parser, CPP_LESS, RT_LESS);
5358         /* Parse the type to which we are casting.  */
5359         type = cp_parser_type_id (parser);
5360         /* Look for the closing `>'.  */
5361         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5362         /* Restore the old message.  */
5363         parser->type_definition_forbidden_message = saved_message;
5364
5365         /* And the expression which is being cast.  */
5366         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5367         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5368         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5369
5370         /* Only type conversions to integral or enumeration types
5371            can be used in constant-expressions.  */
5372         if (!cast_valid_in_integral_constant_expression_p (type)
5373             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5374           return error_mark_node;
5375
5376         switch (keyword)
5377           {
5378           case RID_DYNCAST:
5379             postfix_expression
5380               = build_dynamic_cast (type, expression, tf_warning_or_error);
5381             break;
5382           case RID_STATCAST:
5383             postfix_expression
5384               = build_static_cast (type, expression, tf_warning_or_error);
5385             break;
5386           case RID_REINTCAST:
5387             postfix_expression
5388               = build_reinterpret_cast (type, expression, 
5389                                         tf_warning_or_error);
5390             break;
5391           case RID_CONSTCAST:
5392             postfix_expression
5393               = build_const_cast (type, expression, tf_warning_or_error);
5394             break;
5395           default:
5396             gcc_unreachable ();
5397           }
5398       }
5399       break;
5400
5401     case RID_TYPEID:
5402       {
5403         tree type;
5404         const char *saved_message;
5405         bool saved_in_type_id_in_expr_p;
5406
5407         /* Consume the `typeid' token.  */
5408         cp_lexer_consume_token (parser->lexer);
5409         /* Look for the `(' token.  */
5410         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5411         /* Types cannot be defined in a `typeid' expression.  */
5412         saved_message = parser->type_definition_forbidden_message;
5413         parser->type_definition_forbidden_message
5414           = G_("types may not be defined in a %<typeid%> expression");
5415         /* We can't be sure yet whether we're looking at a type-id or an
5416            expression.  */
5417         cp_parser_parse_tentatively (parser);
5418         /* Try a type-id first.  */
5419         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5420         parser->in_type_id_in_expr_p = true;
5421         type = cp_parser_type_id (parser);
5422         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5423         /* Look for the `)' token.  Otherwise, we can't be sure that
5424            we're not looking at an expression: consider `typeid (int
5425            (3))', for example.  */
5426         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5427         /* If all went well, simply lookup the type-id.  */
5428         if (cp_parser_parse_definitely (parser))
5429           postfix_expression = get_typeid (type);
5430         /* Otherwise, fall back to the expression variant.  */
5431         else
5432           {
5433             tree expression;
5434
5435             /* Look for an expression.  */
5436             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5437             /* Compute its typeid.  */
5438             postfix_expression = build_typeid (expression);
5439             /* Look for the `)' token.  */
5440             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5441           }
5442         /* Restore the saved message.  */
5443         parser->type_definition_forbidden_message = saved_message;
5444         /* `typeid' may not appear in an integral constant expression.  */
5445         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5446           return error_mark_node;
5447       }
5448       break;
5449
5450     case RID_TYPENAME:
5451       {
5452         tree type;
5453         /* The syntax permitted here is the same permitted for an
5454            elaborated-type-specifier.  */
5455         type = cp_parser_elaborated_type_specifier (parser,
5456                                                     /*is_friend=*/false,
5457                                                     /*is_declaration=*/false);
5458         postfix_expression = cp_parser_functional_cast (parser, type);
5459       }
5460       break;
5461
5462     case RID_BUILTIN_SHUFFLE:
5463       {
5464         VEC(tree,gc)* vec;
5465         unsigned int i;
5466         tree p;
5467         location_t loc = token->location;
5468
5469         cp_lexer_consume_token (parser->lexer);
5470         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5471                     /*cast_p=*/false, /*allow_expansion_p=*/true,
5472                     /*non_constant_p=*/NULL);
5473         if (vec == NULL)
5474           return error_mark_node;
5475
5476         FOR_EACH_VEC_ELT (tree, vec, i, p)
5477           mark_exp_read (p);
5478
5479         if (VEC_length (tree, vec) == 2)
5480           return
5481             c_build_vec_perm_expr
5482             (loc, VEC_index (tree, vec, 0),
5483              NULL_TREE, VEC_index (tree, vec, 1));
5484
5485         else if (VEC_length (tree, vec) == 3)
5486           return
5487             c_build_vec_perm_expr
5488             (loc, VEC_index (tree, vec, 0),
5489              VEC_index (tree, vec, 1),
5490              VEC_index (tree, vec, 2));
5491         else
5492         {
5493           error_at (loc, "wrong number of arguments to "
5494               "%<__builtin_shuffle%>");
5495           return error_mark_node;
5496         }
5497         break;
5498       }
5499
5500     default:
5501       {
5502         tree type;
5503
5504         /* If the next thing is a simple-type-specifier, we may be
5505            looking at a functional cast.  We could also be looking at
5506            an id-expression.  So, we try the functional cast, and if
5507            that doesn't work we fall back to the primary-expression.  */
5508         cp_parser_parse_tentatively (parser);
5509         /* Look for the simple-type-specifier.  */
5510         type = cp_parser_simple_type_specifier (parser,
5511                                                 /*decl_specs=*/NULL,
5512                                                 CP_PARSER_FLAGS_NONE);
5513         /* Parse the cast itself.  */
5514         if (!cp_parser_error_occurred (parser))
5515           postfix_expression
5516             = cp_parser_functional_cast (parser, type);
5517         /* If that worked, we're done.  */
5518         if (cp_parser_parse_definitely (parser))
5519           break;
5520
5521         /* If the functional-cast didn't work out, try a
5522            compound-literal.  */
5523         if (cp_parser_allow_gnu_extensions_p (parser)
5524             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5525           {
5526             VEC(constructor_elt,gc) *initializer_list = NULL;
5527             bool saved_in_type_id_in_expr_p;
5528
5529             cp_parser_parse_tentatively (parser);
5530             /* Consume the `('.  */
5531             cp_lexer_consume_token (parser->lexer);
5532             /* Parse the type.  */
5533             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5534             parser->in_type_id_in_expr_p = true;
5535             type = cp_parser_type_id (parser);
5536             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5537             /* Look for the `)'.  */
5538             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5539             /* Look for the `{'.  */
5540             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5541             /* If things aren't going well, there's no need to
5542                keep going.  */
5543             if (!cp_parser_error_occurred (parser))
5544               {
5545                 bool non_constant_p;
5546                 /* Parse the initializer-list.  */
5547                 initializer_list
5548                   = cp_parser_initializer_list (parser, &non_constant_p);
5549                 /* Allow a trailing `,'.  */
5550                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5551                   cp_lexer_consume_token (parser->lexer);
5552                 /* Look for the final `}'.  */
5553                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5554               }
5555             /* If that worked, we're definitely looking at a
5556                compound-literal expression.  */
5557             if (cp_parser_parse_definitely (parser))
5558               {
5559                 /* Warn the user that a compound literal is not
5560                    allowed in standard C++.  */
5561                 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5562                 /* For simplicity, we disallow compound literals in
5563                    constant-expressions.  We could
5564                    allow compound literals of integer type, whose
5565                    initializer was a constant, in constant
5566                    expressions.  Permitting that usage, as a further
5567                    extension, would not change the meaning of any
5568                    currently accepted programs.  (Of course, as
5569                    compound literals are not part of ISO C++, the
5570                    standard has nothing to say.)  */
5571                 if (cp_parser_non_integral_constant_expression (parser,
5572                                                                 NIC_NCC))
5573                   {
5574                     postfix_expression = error_mark_node;
5575                     break;
5576                   }
5577                 /* Form the representation of the compound-literal.  */
5578                 postfix_expression
5579                   = (finish_compound_literal
5580                      (type, build_constructor (init_list_type_node,
5581                                                initializer_list),
5582                       tf_warning_or_error));
5583                 break;
5584               }
5585           }
5586
5587         /* It must be a primary-expression.  */
5588         postfix_expression
5589           = cp_parser_primary_expression (parser, address_p, cast_p,
5590                                           /*template_arg_p=*/false,
5591                                           &idk);
5592       }
5593       break;
5594     }
5595
5596   /* Keep looping until the postfix-expression is complete.  */
5597   while (true)
5598     {
5599       if (idk == CP_ID_KIND_UNQUALIFIED
5600           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5601           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5602         /* It is not a Koenig lookup function call.  */
5603         postfix_expression
5604           = unqualified_name_lookup_error (postfix_expression);
5605
5606       /* Peek at the next token.  */
5607       token = cp_lexer_peek_token (parser->lexer);
5608
5609       switch (token->type)
5610         {
5611         case CPP_OPEN_SQUARE:
5612           postfix_expression
5613             = cp_parser_postfix_open_square_expression (parser,
5614                                                         postfix_expression,
5615                                                         false);
5616           idk = CP_ID_KIND_NONE;
5617           is_member_access = false;
5618           break;
5619
5620         case CPP_OPEN_PAREN:
5621           /* postfix-expression ( expression-list [opt] ) */
5622           {
5623             bool koenig_p;
5624             bool is_builtin_constant_p;
5625             bool saved_integral_constant_expression_p = false;
5626             bool saved_non_integral_constant_expression_p = false;
5627             VEC(tree,gc) *args;
5628
5629             is_member_access = false;
5630
5631             is_builtin_constant_p
5632               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5633             if (is_builtin_constant_p)
5634               {
5635                 /* The whole point of __builtin_constant_p is to allow
5636                    non-constant expressions to appear as arguments.  */
5637                 saved_integral_constant_expression_p
5638                   = parser->integral_constant_expression_p;
5639                 saved_non_integral_constant_expression_p
5640                   = parser->non_integral_constant_expression_p;
5641                 parser->integral_constant_expression_p = false;
5642               }
5643             args = (cp_parser_parenthesized_expression_list
5644                     (parser, non_attr,
5645                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5646                      /*non_constant_p=*/NULL));
5647             if (is_builtin_constant_p)
5648               {
5649                 parser->integral_constant_expression_p
5650                   = saved_integral_constant_expression_p;
5651                 parser->non_integral_constant_expression_p
5652                   = saved_non_integral_constant_expression_p;
5653               }
5654
5655             if (args == NULL)
5656               {
5657                 postfix_expression = error_mark_node;
5658                 break;
5659               }
5660
5661             /* Function calls are not permitted in
5662                constant-expressions.  */
5663             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5664                 && cp_parser_non_integral_constant_expression (parser,
5665                                                                NIC_FUNC_CALL))
5666               {
5667                 postfix_expression = error_mark_node;
5668                 release_tree_vector (args);
5669                 break;
5670               }
5671
5672             koenig_p = false;
5673             if (idk == CP_ID_KIND_UNQUALIFIED
5674                 || idk == CP_ID_KIND_TEMPLATE_ID)
5675               {
5676                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5677                   {
5678                     if (!VEC_empty (tree, args))
5679                       {
5680                         koenig_p = true;
5681                         if (!any_type_dependent_arguments_p (args))
5682                           postfix_expression
5683                             = perform_koenig_lookup (postfix_expression, args,
5684                                                      /*include_std=*/false,
5685                                                      tf_warning_or_error);
5686                       }
5687                     else
5688                       postfix_expression
5689                         = unqualified_fn_lookup_error (postfix_expression);
5690                   }
5691                 /* We do not perform argument-dependent lookup if
5692                    normal lookup finds a non-function, in accordance
5693                    with the expected resolution of DR 218.  */
5694                 else if (!VEC_empty (tree, args)
5695                          && is_overloaded_fn (postfix_expression))
5696                   {
5697                     tree fn = get_first_fn (postfix_expression);
5698                     fn = STRIP_TEMPLATE (fn);
5699
5700                     /* Do not do argument dependent lookup if regular
5701                        lookup finds a member function or a block-scope
5702                        function declaration.  [basic.lookup.argdep]/3  */
5703                     if (!DECL_FUNCTION_MEMBER_P (fn)
5704                         && !DECL_LOCAL_FUNCTION_P (fn))
5705                       {
5706                         koenig_p = true;
5707                         if (!any_type_dependent_arguments_p (args))
5708                           postfix_expression
5709                             = perform_koenig_lookup (postfix_expression, args,
5710                                                      /*include_std=*/false,
5711                                                      tf_warning_or_error);
5712                       }
5713                   }
5714               }
5715
5716             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5717               {
5718                 tree instance = TREE_OPERAND (postfix_expression, 0);
5719                 tree fn = TREE_OPERAND (postfix_expression, 1);
5720
5721                 if (processing_template_decl
5722                     && (type_dependent_expression_p (instance)
5723                         || (!BASELINK_P (fn)
5724                             && TREE_CODE (fn) != FIELD_DECL)
5725                         || type_dependent_expression_p (fn)
5726                         || any_type_dependent_arguments_p (args)))
5727                   {
5728                     postfix_expression
5729                       = build_nt_call_vec (postfix_expression, args);
5730                     release_tree_vector (args);
5731                     break;
5732                   }
5733
5734                 if (BASELINK_P (fn))
5735                   {
5736                   postfix_expression
5737                     = (build_new_method_call
5738                        (instance, fn, &args, NULL_TREE,
5739                         (idk == CP_ID_KIND_QUALIFIED
5740                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5741                          : LOOKUP_NORMAL),
5742                         /*fn_p=*/NULL,
5743                         tf_warning_or_error));
5744                   }
5745                 else
5746                   postfix_expression
5747                     = finish_call_expr (postfix_expression, &args,
5748                                         /*disallow_virtual=*/false,
5749                                         /*koenig_p=*/false,
5750                                         tf_warning_or_error);
5751               }
5752             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5753                      || TREE_CODE (postfix_expression) == MEMBER_REF
5754                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5755               postfix_expression = (build_offset_ref_call_from_tree
5756                                     (postfix_expression, &args));
5757             else if (idk == CP_ID_KIND_QUALIFIED)
5758               /* A call to a static class member, or a namespace-scope
5759                  function.  */
5760               postfix_expression
5761                 = finish_call_expr (postfix_expression, &args,
5762                                     /*disallow_virtual=*/true,
5763                                     koenig_p,
5764                                     tf_warning_or_error);
5765             else
5766               /* All other function calls.  */
5767               postfix_expression
5768                 = finish_call_expr (postfix_expression, &args,
5769                                     /*disallow_virtual=*/false,
5770                                     koenig_p,
5771                                     tf_warning_or_error);
5772
5773             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5774             idk = CP_ID_KIND_NONE;
5775
5776             release_tree_vector (args);
5777           }
5778           break;
5779
5780         case CPP_DOT:
5781         case CPP_DEREF:
5782           /* postfix-expression . template [opt] id-expression
5783              postfix-expression . pseudo-destructor-name
5784              postfix-expression -> template [opt] id-expression
5785              postfix-expression -> pseudo-destructor-name */
5786
5787           /* Consume the `.' or `->' operator.  */
5788           cp_lexer_consume_token (parser->lexer);
5789
5790           postfix_expression
5791             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5792                                                       postfix_expression,
5793                                                       false, &idk,
5794                                                       token->location);
5795
5796           is_member_access = true;
5797           break;
5798
5799         case CPP_PLUS_PLUS:
5800           /* postfix-expression ++  */
5801           /* Consume the `++' token.  */
5802           cp_lexer_consume_token (parser->lexer);
5803           /* Generate a representation for the complete expression.  */
5804           postfix_expression
5805             = finish_increment_expr (postfix_expression,
5806                                      POSTINCREMENT_EXPR);
5807           /* Increments may not appear in constant-expressions.  */
5808           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5809             postfix_expression = error_mark_node;
5810           idk = CP_ID_KIND_NONE;
5811           is_member_access = false;
5812           break;
5813
5814         case CPP_MINUS_MINUS:
5815           /* postfix-expression -- */
5816           /* Consume the `--' token.  */
5817           cp_lexer_consume_token (parser->lexer);
5818           /* Generate a representation for the complete expression.  */
5819           postfix_expression
5820             = finish_increment_expr (postfix_expression,
5821                                      POSTDECREMENT_EXPR);
5822           /* Decrements may not appear in constant-expressions.  */
5823           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5824             postfix_expression = error_mark_node;
5825           idk = CP_ID_KIND_NONE;
5826           is_member_access = false;
5827           break;
5828
5829         default:
5830           if (pidk_return != NULL)
5831             * pidk_return = idk;
5832           if (member_access_only_p)
5833             return is_member_access? postfix_expression : error_mark_node;
5834           else
5835             return postfix_expression;
5836         }
5837     }
5838
5839   /* We should never get here.  */
5840   gcc_unreachable ();
5841   return error_mark_node;
5842 }
5843
5844 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5845    by cp_parser_builtin_offsetof.  We're looking for
5846
5847      postfix-expression [ expression ]
5848      postfix-expression [ braced-init-list ] (C++11)
5849
5850    FOR_OFFSETOF is set if we're being called in that context, which
5851    changes how we deal with integer constant expressions.  */
5852
5853 static tree
5854 cp_parser_postfix_open_square_expression (cp_parser *parser,
5855                                           tree postfix_expression,
5856                                           bool for_offsetof)
5857 {
5858   tree index;
5859   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5860
5861   /* Consume the `[' token.  */
5862   cp_lexer_consume_token (parser->lexer);
5863
5864   /* Parse the index expression.  */
5865   /* ??? For offsetof, there is a question of what to allow here.  If
5866      offsetof is not being used in an integral constant expression context,
5867      then we *could* get the right answer by computing the value at runtime.
5868      If we are in an integral constant expression context, then we might
5869      could accept any constant expression; hard to say without analysis.
5870      Rather than open the barn door too wide right away, allow only integer
5871      constant expressions here.  */
5872   if (for_offsetof)
5873     index = cp_parser_constant_expression (parser, false, NULL);
5874   else
5875     {
5876       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5877         {
5878           bool expr_nonconst_p;
5879           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5880           index = cp_parser_braced_list (parser, &expr_nonconst_p);
5881         }
5882       else
5883         index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5884     }
5885
5886   /* Look for the closing `]'.  */
5887   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5888
5889   /* Build the ARRAY_REF.  */
5890   postfix_expression = grok_array_decl (loc, postfix_expression, index);
5891
5892   /* When not doing offsetof, array references are not permitted in
5893      constant-expressions.  */
5894   if (!for_offsetof
5895       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5896     postfix_expression = error_mark_node;
5897
5898   return postfix_expression;
5899 }
5900
5901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5902    by cp_parser_builtin_offsetof.  We're looking for
5903
5904      postfix-expression . template [opt] id-expression
5905      postfix-expression . pseudo-destructor-name
5906      postfix-expression -> template [opt] id-expression
5907      postfix-expression -> pseudo-destructor-name
5908
5909    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5910    limits what of the above we'll actually accept, but nevermind.
5911    TOKEN_TYPE is the "." or "->" token, which will already have been
5912    removed from the stream.  */
5913
5914 static tree
5915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5916                                         enum cpp_ttype token_type,
5917                                         tree postfix_expression,
5918                                         bool for_offsetof, cp_id_kind *idk,
5919                                         location_t location)
5920 {
5921   tree name;
5922   bool dependent_p;
5923   bool pseudo_destructor_p;
5924   tree scope = NULL_TREE;
5925
5926   /* If this is a `->' operator, dereference the pointer.  */
5927   if (token_type == CPP_DEREF)
5928     postfix_expression = build_x_arrow (location, postfix_expression,
5929                                         tf_warning_or_error);
5930   /* Check to see whether or not the expression is type-dependent.  */
5931   dependent_p = type_dependent_expression_p (postfix_expression);
5932   /* The identifier following the `->' or `.' is not qualified.  */
5933   parser->scope = NULL_TREE;
5934   parser->qualifying_scope = NULL_TREE;
5935   parser->object_scope = NULL_TREE;
5936   *idk = CP_ID_KIND_NONE;
5937
5938   /* Enter the scope corresponding to the type of the object
5939      given by the POSTFIX_EXPRESSION.  */
5940   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5941     {
5942       scope = TREE_TYPE (postfix_expression);
5943       /* According to the standard, no expression should ever have
5944          reference type.  Unfortunately, we do not currently match
5945          the standard in this respect in that our internal representation
5946          of an expression may have reference type even when the standard
5947          says it does not.  Therefore, we have to manually obtain the
5948          underlying type here.  */
5949       scope = non_reference (scope);
5950       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5951       if (scope == unknown_type_node)
5952         {
5953           error_at (location, "%qE does not have class type",
5954                     postfix_expression);
5955           scope = NULL_TREE;
5956         }
5957       /* Unlike the object expression in other contexts, *this is not
5958          required to be of complete type for purposes of class member
5959          access (5.2.5) outside the member function body.  */
5960       else if (scope != current_class_ref
5961                && !(processing_template_decl && scope == current_class_type))
5962         scope = complete_type_or_else (scope, NULL_TREE);
5963       /* Let the name lookup machinery know that we are processing a
5964          class member access expression.  */
5965       parser->context->object_type = scope;
5966       /* If something went wrong, we want to be able to discern that case,
5967          as opposed to the case where there was no SCOPE due to the type
5968          of expression being dependent.  */
5969       if (!scope)
5970         scope = error_mark_node;
5971       /* If the SCOPE was erroneous, make the various semantic analysis
5972          functions exit quickly -- and without issuing additional error
5973          messages.  */
5974       if (scope == error_mark_node)
5975         postfix_expression = error_mark_node;
5976     }
5977
5978   /* Assume this expression is not a pseudo-destructor access.  */
5979   pseudo_destructor_p = false;
5980
5981   /* If the SCOPE is a scalar type, then, if this is a valid program,
5982      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5983      is type dependent, it can be pseudo-destructor-name or something else.
5984      Try to parse it as pseudo-destructor-name first.  */
5985   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5986     {
5987       tree s;
5988       tree type;
5989
5990       cp_parser_parse_tentatively (parser);
5991       /* Parse the pseudo-destructor-name.  */
5992       s = NULL_TREE;
5993       cp_parser_pseudo_destructor_name (parser, &s, &type);
5994       if (dependent_p
5995           && (cp_parser_error_occurred (parser)
5996               || TREE_CODE (type) != TYPE_DECL
5997               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5998         cp_parser_abort_tentative_parse (parser);
5999       else if (cp_parser_parse_definitely (parser))
6000         {
6001           pseudo_destructor_p = true;
6002           postfix_expression
6003             = finish_pseudo_destructor_expr (postfix_expression,
6004                                              s, TREE_TYPE (type));
6005         }
6006     }
6007
6008   if (!pseudo_destructor_p)
6009     {
6010       /* If the SCOPE is not a scalar type, we are looking at an
6011          ordinary class member access expression, rather than a
6012          pseudo-destructor-name.  */
6013       bool template_p;
6014       cp_token *token = cp_lexer_peek_token (parser->lexer);
6015       /* Parse the id-expression.  */
6016       name = (cp_parser_id_expression
6017               (parser,
6018                cp_parser_optional_template_keyword (parser),
6019                /*check_dependency_p=*/true,
6020                &template_p,
6021                /*declarator_p=*/false,
6022                /*optional_p=*/false));
6023       /* In general, build a SCOPE_REF if the member name is qualified.
6024          However, if the name was not dependent and has already been
6025          resolved; there is no need to build the SCOPE_REF.  For example;
6026
6027              struct X { void f(); };
6028              template <typename T> void f(T* t) { t->X::f(); }
6029
6030          Even though "t" is dependent, "X::f" is not and has been resolved
6031          to a BASELINK; there is no need to include scope information.  */
6032
6033       /* But we do need to remember that there was an explicit scope for
6034          virtual function calls.  */
6035       if (parser->scope)
6036         *idk = CP_ID_KIND_QUALIFIED;
6037
6038       /* If the name is a template-id that names a type, we will get a
6039          TYPE_DECL here.  That is invalid code.  */
6040       if (TREE_CODE (name) == TYPE_DECL)
6041         {
6042           error_at (token->location, "invalid use of %qD", name);
6043           postfix_expression = error_mark_node;
6044         }
6045       else
6046         {
6047           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6048             {
6049               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6050                 {
6051                   error_at (token->location, "%<%D::%D%> is not a class member",
6052                             parser->scope, name);
6053                   postfix_expression = error_mark_node;
6054                 }
6055               else
6056                 name = build_qualified_name (/*type=*/NULL_TREE,
6057                                              parser->scope,
6058                                              name,
6059                                              template_p);
6060               parser->scope = NULL_TREE;
6061               parser->qualifying_scope = NULL_TREE;
6062               parser->object_scope = NULL_TREE;
6063             }
6064           if (parser->scope && name && BASELINK_P (name))
6065             adjust_result_of_qualified_name_lookup
6066               (name, parser->scope, scope);
6067           postfix_expression
6068             = finish_class_member_access_expr (postfix_expression, name,
6069                                                template_p, 
6070                                                tf_warning_or_error);
6071         }
6072     }
6073
6074   /* We no longer need to look up names in the scope of the object on
6075      the left-hand side of the `.' or `->' operator.  */
6076   parser->context->object_type = NULL_TREE;
6077
6078   /* Outside of offsetof, these operators may not appear in
6079      constant-expressions.  */
6080   if (!for_offsetof
6081       && (cp_parser_non_integral_constant_expression
6082           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6083     postfix_expression = error_mark_node;
6084
6085   return postfix_expression;
6086 }
6087
6088 /* Parse a parenthesized expression-list.
6089
6090    expression-list:
6091      assignment-expression
6092      expression-list, assignment-expression
6093
6094    attribute-list:
6095      expression-list
6096      identifier
6097      identifier, expression-list
6098
6099    CAST_P is true if this expression is the target of a cast.
6100
6101    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6102    argument pack.
6103
6104    Returns a vector of trees.  Each element is a representation of an
6105    assignment-expression.  NULL is returned if the ( and or ) are
6106    missing.  An empty, but allocated, vector is returned on no
6107    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6108    if we are parsing an attribute list for an attribute that wants a
6109    plain identifier argument, normal_attr for an attribute that wants
6110    an expression, or non_attr if we aren't parsing an attribute list.  If
6111    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6112    not all of the expressions in the list were constant.  */
6113
6114 static VEC(tree,gc) *
6115 cp_parser_parenthesized_expression_list (cp_parser* parser,
6116                                          int is_attribute_list,
6117                                          bool cast_p,
6118                                          bool allow_expansion_p,
6119                                          bool *non_constant_p)
6120 {
6121   VEC(tree,gc) *expression_list;
6122   bool fold_expr_p = is_attribute_list != non_attr;
6123   tree identifier = NULL_TREE;
6124   bool saved_greater_than_is_operator_p;
6125
6126   /* Assume all the expressions will be constant.  */
6127   if (non_constant_p)
6128     *non_constant_p = false;
6129
6130   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6131     return NULL;
6132
6133   expression_list = make_tree_vector ();
6134
6135   /* Within a parenthesized expression, a `>' token is always
6136      the greater-than operator.  */
6137   saved_greater_than_is_operator_p
6138     = parser->greater_than_is_operator_p;
6139   parser->greater_than_is_operator_p = true;
6140
6141   /* Consume expressions until there are no more.  */
6142   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6143     while (true)
6144       {
6145         tree expr;
6146
6147         /* At the beginning of attribute lists, check to see if the
6148            next token is an identifier.  */
6149         if (is_attribute_list == id_attr
6150             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6151           {
6152             cp_token *token;
6153
6154             /* Consume the identifier.  */
6155             token = cp_lexer_consume_token (parser->lexer);
6156             /* Save the identifier.  */
6157             identifier = token->u.value;
6158           }
6159         else
6160           {
6161             bool expr_non_constant_p;
6162
6163             /* Parse the next assignment-expression.  */
6164             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6165               {
6166                 /* A braced-init-list.  */
6167                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6168                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6169                 if (non_constant_p && expr_non_constant_p)
6170                   *non_constant_p = true;
6171               }
6172             else if (non_constant_p)
6173               {
6174                 expr = (cp_parser_constant_expression
6175                         (parser, /*allow_non_constant_p=*/true,
6176                          &expr_non_constant_p));
6177                 if (expr_non_constant_p)
6178                   *non_constant_p = true;
6179               }
6180             else
6181               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6182
6183             if (fold_expr_p)
6184               expr = fold_non_dependent_expr (expr);
6185
6186             /* If we have an ellipsis, then this is an expression
6187                expansion.  */
6188             if (allow_expansion_p
6189                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6190               {
6191                 /* Consume the `...'.  */
6192                 cp_lexer_consume_token (parser->lexer);
6193
6194                 /* Build the argument pack.  */
6195                 expr = make_pack_expansion (expr);
6196               }
6197
6198              /* Add it to the list.  We add error_mark_node
6199                 expressions to the list, so that we can still tell if
6200                 the correct form for a parenthesized expression-list
6201                 is found. That gives better errors.  */
6202             VEC_safe_push (tree, gc, expression_list, expr);
6203
6204             if (expr == error_mark_node)
6205               goto skip_comma;
6206           }
6207
6208         /* After the first item, attribute lists look the same as
6209            expression lists.  */
6210         is_attribute_list = non_attr;
6211
6212       get_comma:;
6213         /* If the next token isn't a `,', then we are done.  */
6214         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6215           break;
6216
6217         /* Otherwise, consume the `,' and keep going.  */
6218         cp_lexer_consume_token (parser->lexer);
6219       }
6220
6221   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6222     {
6223       int ending;
6224
6225     skip_comma:;
6226       /* We try and resync to an unnested comma, as that will give the
6227          user better diagnostics.  */
6228       ending = cp_parser_skip_to_closing_parenthesis (parser,
6229                                                       /*recovering=*/true,
6230                                                       /*or_comma=*/true,
6231                                                       /*consume_paren=*/true);
6232       if (ending < 0)
6233         goto get_comma;
6234       if (!ending)
6235         {
6236           parser->greater_than_is_operator_p
6237             = saved_greater_than_is_operator_p;
6238           return NULL;
6239         }
6240     }
6241
6242   parser->greater_than_is_operator_p
6243     = saved_greater_than_is_operator_p;
6244
6245   if (identifier)
6246     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6247
6248   return expression_list;
6249 }
6250
6251 /* Parse a pseudo-destructor-name.
6252
6253    pseudo-destructor-name:
6254      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6255      :: [opt] nested-name-specifier template template-id :: ~ type-name
6256      :: [opt] nested-name-specifier [opt] ~ type-name
6257
6258    If either of the first two productions is used, sets *SCOPE to the
6259    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6260    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6261    or ERROR_MARK_NODE if the parse fails.  */
6262
6263 static void
6264 cp_parser_pseudo_destructor_name (cp_parser* parser,
6265                                   tree* scope,
6266                                   tree* type)
6267 {
6268   bool nested_name_specifier_p;
6269
6270   /* Assume that things will not work out.  */
6271   *type = error_mark_node;
6272
6273   /* Look for the optional `::' operator.  */
6274   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6275   /* Look for the optional nested-name-specifier.  */
6276   nested_name_specifier_p
6277     = (cp_parser_nested_name_specifier_opt (parser,
6278                                             /*typename_keyword_p=*/false,
6279                                             /*check_dependency_p=*/true,
6280                                             /*type_p=*/false,
6281                                             /*is_declaration=*/false)
6282        != NULL_TREE);
6283   /* Now, if we saw a nested-name-specifier, we might be doing the
6284      second production.  */
6285   if (nested_name_specifier_p
6286       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6287     {
6288       /* Consume the `template' keyword.  */
6289       cp_lexer_consume_token (parser->lexer);
6290       /* Parse the template-id.  */
6291       cp_parser_template_id (parser,
6292                              /*template_keyword_p=*/true,
6293                              /*check_dependency_p=*/false,
6294                              class_type,
6295                              /*is_declaration=*/true);
6296       /* Look for the `::' token.  */
6297       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6298     }
6299   /* If the next token is not a `~', then there might be some
6300      additional qualification.  */
6301   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6302     {
6303       /* At this point, we're looking for "type-name :: ~".  The type-name
6304          must not be a class-name, since this is a pseudo-destructor.  So,
6305          it must be either an enum-name, or a typedef-name -- both of which
6306          are just identifiers.  So, we peek ahead to check that the "::"
6307          and "~" tokens are present; if they are not, then we can avoid
6308          calling type_name.  */
6309       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6310           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6311           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6312         {
6313           cp_parser_error (parser, "non-scalar type");
6314           return;
6315         }
6316
6317       /* Look for the type-name.  */
6318       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6319       if (*scope == error_mark_node)
6320         return;
6321
6322       /* Look for the `::' token.  */
6323       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6324     }
6325   else
6326     *scope = NULL_TREE;
6327
6328   /* Look for the `~'.  */
6329   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6330
6331   /* Once we see the ~, this has to be a pseudo-destructor.  */
6332   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6333     cp_parser_commit_to_tentative_parse (parser);
6334
6335   /* Look for the type-name again.  We are not responsible for
6336      checking that it matches the first type-name.  */
6337   *type = cp_parser_nonclass_name (parser);
6338 }
6339
6340 /* Parse a unary-expression.
6341
6342    unary-expression:
6343      postfix-expression
6344      ++ cast-expression
6345      -- cast-expression
6346      unary-operator cast-expression
6347      sizeof unary-expression
6348      sizeof ( type-id )
6349      alignof ( type-id )  [C++0x]
6350      new-expression
6351      delete-expression
6352
6353    GNU Extensions:
6354
6355    unary-expression:
6356      __extension__ cast-expression
6357      __alignof__ unary-expression
6358      __alignof__ ( type-id )
6359      alignof unary-expression  [C++0x]
6360      __real__ cast-expression
6361      __imag__ cast-expression
6362      && identifier
6363
6364    ADDRESS_P is true iff the unary-expression is appearing as the
6365    operand of the `&' operator.   CAST_P is true if this expression is
6366    the target of a cast.
6367
6368    Returns a representation of the expression.  */
6369
6370 static tree
6371 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6372                             cp_id_kind * pidk)
6373 {
6374   cp_token *token;
6375   enum tree_code unary_operator;
6376
6377   /* Peek at the next token.  */
6378   token = cp_lexer_peek_token (parser->lexer);
6379   /* Some keywords give away the kind of expression.  */
6380   if (token->type == CPP_KEYWORD)
6381     {
6382       enum rid keyword = token->keyword;
6383
6384       switch (keyword)
6385         {
6386         case RID_ALIGNOF:
6387         case RID_SIZEOF:
6388           {
6389             tree operand;
6390             enum tree_code op;
6391
6392             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6393             /* Consume the token.  */
6394             cp_lexer_consume_token (parser->lexer);
6395             /* Parse the operand.  */
6396             operand = cp_parser_sizeof_operand (parser, keyword);
6397
6398             if (TYPE_P (operand))
6399               return cxx_sizeof_or_alignof_type (operand, op, true);
6400             else
6401               {
6402                 /* ISO C++ defines alignof only with types, not with
6403                    expressions. So pedwarn if alignof is used with a non-
6404                    type expression. However, __alignof__ is ok.  */
6405                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6406                   pedwarn (token->location, OPT_Wpedantic,
6407                            "ISO C++ does not allow %<alignof%> "
6408                            "with a non-type");
6409
6410                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6411               }
6412           }
6413
6414         case RID_NEW:
6415           return cp_parser_new_expression (parser);
6416
6417         case RID_DELETE:
6418           return cp_parser_delete_expression (parser);
6419
6420         case RID_EXTENSION:
6421           {
6422             /* The saved value of the PEDANTIC flag.  */
6423             int saved_pedantic;
6424             tree expr;
6425
6426             /* Save away the PEDANTIC flag.  */
6427             cp_parser_extension_opt (parser, &saved_pedantic);
6428             /* Parse the cast-expression.  */
6429             expr = cp_parser_simple_cast_expression (parser);
6430             /* Restore the PEDANTIC flag.  */
6431             pedantic = saved_pedantic;
6432
6433             return expr;
6434           }
6435
6436         case RID_REALPART:
6437         case RID_IMAGPART:
6438           {
6439             tree expression;
6440
6441             /* Consume the `__real__' or `__imag__' token.  */
6442             cp_lexer_consume_token (parser->lexer);
6443             /* Parse the cast-expression.  */
6444             expression = cp_parser_simple_cast_expression (parser);
6445             /* Create the complete representation.  */
6446             return build_x_unary_op (token->location,
6447                                      (keyword == RID_REALPART
6448                                       ? REALPART_EXPR : IMAGPART_EXPR),
6449                                      expression,
6450                                      tf_warning_or_error);
6451           }
6452           break;
6453
6454         case RID_TRANSACTION_ATOMIC:
6455         case RID_TRANSACTION_RELAXED:
6456           return cp_parser_transaction_expression (parser, keyword);
6457
6458         case RID_NOEXCEPT:
6459           {
6460             tree expr;
6461             const char *saved_message;
6462             bool saved_integral_constant_expression_p;
6463             bool saved_non_integral_constant_expression_p;
6464             bool saved_greater_than_is_operator_p;
6465
6466             cp_lexer_consume_token (parser->lexer);
6467             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6468
6469             saved_message = parser->type_definition_forbidden_message;
6470             parser->type_definition_forbidden_message
6471               = G_("types may not be defined in %<noexcept%> expressions");
6472
6473             saved_integral_constant_expression_p
6474               = parser->integral_constant_expression_p;
6475             saved_non_integral_constant_expression_p
6476               = parser->non_integral_constant_expression_p;
6477             parser->integral_constant_expression_p = false;
6478
6479             saved_greater_than_is_operator_p
6480               = parser->greater_than_is_operator_p;
6481             parser->greater_than_is_operator_p = true;
6482
6483             ++cp_unevaluated_operand;
6484             ++c_inhibit_evaluation_warnings;
6485             expr = cp_parser_expression (parser, false, NULL);
6486             --c_inhibit_evaluation_warnings;
6487             --cp_unevaluated_operand;
6488
6489             parser->greater_than_is_operator_p
6490               = saved_greater_than_is_operator_p;
6491
6492             parser->integral_constant_expression_p
6493               = saved_integral_constant_expression_p;
6494             parser->non_integral_constant_expression_p
6495               = saved_non_integral_constant_expression_p;
6496
6497             parser->type_definition_forbidden_message = saved_message;
6498
6499             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6500             return finish_noexcept_expr (expr, tf_warning_or_error);
6501           }
6502
6503         default:
6504           break;
6505         }
6506     }
6507
6508   /* Look for the `:: new' and `:: delete', which also signal the
6509      beginning of a new-expression, or delete-expression,
6510      respectively.  If the next token is `::', then it might be one of
6511      these.  */
6512   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6513     {
6514       enum rid keyword;
6515
6516       /* See if the token after the `::' is one of the keywords in
6517          which we're interested.  */
6518       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6519       /* If it's `new', we have a new-expression.  */
6520       if (keyword == RID_NEW)
6521         return cp_parser_new_expression (parser);
6522       /* Similarly, for `delete'.  */
6523       else if (keyword == RID_DELETE)
6524         return cp_parser_delete_expression (parser);
6525     }
6526
6527   /* Look for a unary operator.  */
6528   unary_operator = cp_parser_unary_operator (token);
6529   /* The `++' and `--' operators can be handled similarly, even though
6530      they are not technically unary-operators in the grammar.  */
6531   if (unary_operator == ERROR_MARK)
6532     {
6533       if (token->type == CPP_PLUS_PLUS)
6534         unary_operator = PREINCREMENT_EXPR;
6535       else if (token->type == CPP_MINUS_MINUS)
6536         unary_operator = PREDECREMENT_EXPR;
6537       /* Handle the GNU address-of-label extension.  */
6538       else if (cp_parser_allow_gnu_extensions_p (parser)
6539                && token->type == CPP_AND_AND)
6540         {
6541           tree identifier;
6542           tree expression;
6543           location_t loc = token->location;
6544
6545           /* Consume the '&&' token.  */
6546           cp_lexer_consume_token (parser->lexer);
6547           /* Look for the identifier.  */
6548           identifier = cp_parser_identifier (parser);
6549           /* Create an expression representing the address.  */
6550           expression = finish_label_address_expr (identifier, loc);
6551           if (cp_parser_non_integral_constant_expression (parser,
6552                                                           NIC_ADDR_LABEL))
6553             expression = error_mark_node;
6554           return expression;
6555         }
6556     }
6557   if (unary_operator != ERROR_MARK)
6558     {
6559       tree cast_expression;
6560       tree expression = error_mark_node;
6561       non_integral_constant non_constant_p = NIC_NONE;
6562       location_t loc = token->location;
6563
6564       /* Consume the operator token.  */
6565       token = cp_lexer_consume_token (parser->lexer);
6566       /* Parse the cast-expression.  */
6567       cast_expression
6568         = cp_parser_cast_expression (parser,
6569                                      unary_operator == ADDR_EXPR,
6570                                      /*cast_p=*/false, pidk);
6571       /* Now, build an appropriate representation.  */
6572       switch (unary_operator)
6573         {
6574         case INDIRECT_REF:
6575           non_constant_p = NIC_STAR;
6576           expression = build_x_indirect_ref (loc, cast_expression,
6577                                              RO_UNARY_STAR,
6578                                              tf_warning_or_error);
6579           break;
6580
6581         case ADDR_EXPR:
6582            non_constant_p = NIC_ADDR;
6583           /* Fall through.  */
6584         case BIT_NOT_EXPR:
6585           expression = build_x_unary_op (loc, unary_operator,
6586                                          cast_expression,
6587                                          tf_warning_or_error);
6588           break;
6589
6590         case PREINCREMENT_EXPR:
6591         case PREDECREMENT_EXPR:
6592           non_constant_p = unary_operator == PREINCREMENT_EXPR
6593                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6594           /* Fall through.  */
6595         case UNARY_PLUS_EXPR:
6596         case NEGATE_EXPR:
6597         case TRUTH_NOT_EXPR:
6598           expression = finish_unary_op_expr (loc, unary_operator,
6599                                              cast_expression);
6600           break;
6601
6602         default:
6603           gcc_unreachable ();
6604         }
6605
6606       if (non_constant_p != NIC_NONE
6607           && cp_parser_non_integral_constant_expression (parser,
6608                                                          non_constant_p))
6609         expression = error_mark_node;
6610
6611       return expression;
6612     }
6613
6614   return cp_parser_postfix_expression (parser, address_p, cast_p,
6615                                        /*member_access_only_p=*/false,
6616                                        pidk);
6617 }
6618
6619 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6620    unary-operator, the corresponding tree code is returned.  */
6621
6622 static enum tree_code
6623 cp_parser_unary_operator (cp_token* token)
6624 {
6625   switch (token->type)
6626     {
6627     case CPP_MULT:
6628       return INDIRECT_REF;
6629
6630     case CPP_AND:
6631       return ADDR_EXPR;
6632
6633     case CPP_PLUS:
6634       return UNARY_PLUS_EXPR;
6635
6636     case CPP_MINUS:
6637       return NEGATE_EXPR;
6638
6639     case CPP_NOT:
6640       return TRUTH_NOT_EXPR;
6641
6642     case CPP_COMPL:
6643       return BIT_NOT_EXPR;
6644
6645     default:
6646       return ERROR_MARK;
6647     }
6648 }
6649
6650 /* Parse a new-expression.
6651
6652    new-expression:
6653      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6654      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6655
6656    Returns a representation of the expression.  */
6657
6658 static tree
6659 cp_parser_new_expression (cp_parser* parser)
6660 {
6661   bool global_scope_p;
6662   VEC(tree,gc) *placement;
6663   tree type;
6664   VEC(tree,gc) *initializer;
6665   tree nelts = NULL_TREE;
6666   tree ret;
6667
6668   /* Look for the optional `::' operator.  */
6669   global_scope_p
6670     = (cp_parser_global_scope_opt (parser,
6671                                    /*current_scope_valid_p=*/false)
6672        != NULL_TREE);
6673   /* Look for the `new' operator.  */
6674   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6675   /* There's no easy way to tell a new-placement from the
6676      `( type-id )' construct.  */
6677   cp_parser_parse_tentatively (parser);
6678   /* Look for a new-placement.  */
6679   placement = cp_parser_new_placement (parser);
6680   /* If that didn't work out, there's no new-placement.  */
6681   if (!cp_parser_parse_definitely (parser))
6682     {
6683       if (placement != NULL)
6684         release_tree_vector (placement);
6685       placement = NULL;
6686     }
6687
6688   /* If the next token is a `(', then we have a parenthesized
6689      type-id.  */
6690   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6691     {
6692       cp_token *token;
6693       const char *saved_message = parser->type_definition_forbidden_message;
6694
6695       /* Consume the `('.  */
6696       cp_lexer_consume_token (parser->lexer);
6697
6698       /* Parse the type-id.  */
6699       parser->type_definition_forbidden_message
6700         = G_("types may not be defined in a new-expression");
6701       type = cp_parser_type_id (parser);
6702       parser->type_definition_forbidden_message = saved_message;
6703
6704       /* Look for the closing `)'.  */
6705       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6706       token = cp_lexer_peek_token (parser->lexer);
6707       /* There should not be a direct-new-declarator in this production,
6708          but GCC used to allowed this, so we check and emit a sensible error
6709          message for this case.  */
6710       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6711         {
6712           error_at (token->location,
6713                     "array bound forbidden after parenthesized type-id");
6714           inform (token->location, 
6715                   "try removing the parentheses around the type-id");
6716           cp_parser_direct_new_declarator (parser);
6717         }
6718     }
6719   /* Otherwise, there must be a new-type-id.  */
6720   else
6721     type = cp_parser_new_type_id (parser, &nelts);
6722
6723   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6724   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6725       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6726     initializer = cp_parser_new_initializer (parser);
6727   else
6728     initializer = NULL;
6729
6730   /* A new-expression may not appear in an integral constant
6731      expression.  */
6732   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6733     ret = error_mark_node;
6734   else
6735     {
6736       /* Create a representation of the new-expression.  */
6737       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6738                        tf_warning_or_error);
6739     }
6740
6741   if (placement != NULL)
6742     release_tree_vector (placement);
6743   if (initializer != NULL)
6744     release_tree_vector (initializer);
6745
6746   return ret;
6747 }
6748
6749 /* Parse a new-placement.
6750
6751    new-placement:
6752      ( expression-list )
6753
6754    Returns the same representation as for an expression-list.  */
6755
6756 static VEC(tree,gc) *
6757 cp_parser_new_placement (cp_parser* parser)
6758 {
6759   VEC(tree,gc) *expression_list;
6760
6761   /* Parse the expression-list.  */
6762   expression_list = (cp_parser_parenthesized_expression_list
6763                      (parser, non_attr, /*cast_p=*/false,
6764                       /*allow_expansion_p=*/true,
6765                       /*non_constant_p=*/NULL));
6766
6767   return expression_list;
6768 }
6769
6770 /* Parse a new-type-id.
6771
6772    new-type-id:
6773      type-specifier-seq new-declarator [opt]
6774
6775    Returns the TYPE allocated.  If the new-type-id indicates an array
6776    type, *NELTS is set to the number of elements in the last array
6777    bound; the TYPE will not include the last array bound.  */
6778
6779 static tree
6780 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6781 {
6782   cp_decl_specifier_seq type_specifier_seq;
6783   cp_declarator *new_declarator;
6784   cp_declarator *declarator;
6785   cp_declarator *outer_declarator;
6786   const char *saved_message;
6787
6788   /* The type-specifier sequence must not contain type definitions.
6789      (It cannot contain declarations of new types either, but if they
6790      are not definitions we will catch that because they are not
6791      complete.)  */
6792   saved_message = parser->type_definition_forbidden_message;
6793   parser->type_definition_forbidden_message
6794     = G_("types may not be defined in a new-type-id");
6795   /* Parse the type-specifier-seq.  */
6796   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6797                                 /*is_trailing_return=*/false,
6798                                 &type_specifier_seq);
6799   /* Restore the old message.  */
6800   parser->type_definition_forbidden_message = saved_message;
6801
6802   if (type_specifier_seq.type == error_mark_node)
6803     return error_mark_node;
6804
6805   /* Parse the new-declarator.  */
6806   new_declarator = cp_parser_new_declarator_opt (parser);
6807
6808   /* Determine the number of elements in the last array dimension, if
6809      any.  */
6810   *nelts = NULL_TREE;
6811   /* Skip down to the last array dimension.  */
6812   declarator = new_declarator;
6813   outer_declarator = NULL;
6814   while (declarator && (declarator->kind == cdk_pointer
6815                         || declarator->kind == cdk_ptrmem))
6816     {
6817       outer_declarator = declarator;
6818       declarator = declarator->declarator;
6819     }
6820   while (declarator
6821          && declarator->kind == cdk_array
6822          && declarator->declarator
6823          && declarator->declarator->kind == cdk_array)
6824     {
6825       outer_declarator = declarator;
6826       declarator = declarator->declarator;
6827     }
6828
6829   if (declarator && declarator->kind == cdk_array)
6830     {
6831       *nelts = declarator->u.array.bounds;
6832       if (*nelts == error_mark_node)
6833         *nelts = integer_one_node;
6834
6835       if (outer_declarator)
6836         outer_declarator->declarator = declarator->declarator;
6837       else
6838         new_declarator = NULL;
6839     }
6840
6841   return groktypename (&type_specifier_seq, new_declarator, false);
6842 }
6843
6844 /* Parse an (optional) new-declarator.
6845
6846    new-declarator:
6847      ptr-operator new-declarator [opt]
6848      direct-new-declarator
6849
6850    Returns the declarator.  */
6851
6852 static cp_declarator *
6853 cp_parser_new_declarator_opt (cp_parser* parser)
6854 {
6855   enum tree_code code;
6856   tree type;
6857   cp_cv_quals cv_quals;
6858
6859   /* We don't know if there's a ptr-operator next, or not.  */
6860   cp_parser_parse_tentatively (parser);
6861   /* Look for a ptr-operator.  */
6862   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6863   /* If that worked, look for more new-declarators.  */
6864   if (cp_parser_parse_definitely (parser))
6865     {
6866       cp_declarator *declarator;
6867
6868       /* Parse another optional declarator.  */
6869       declarator = cp_parser_new_declarator_opt (parser);
6870
6871       return cp_parser_make_indirect_declarator
6872         (code, type, cv_quals, declarator);
6873     }
6874
6875   /* If the next token is a `[', there is a direct-new-declarator.  */
6876   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6877     return cp_parser_direct_new_declarator (parser);
6878
6879   return NULL;
6880 }
6881
6882 /* Parse a direct-new-declarator.
6883
6884    direct-new-declarator:
6885      [ expression ]
6886      direct-new-declarator [constant-expression]
6887
6888    */
6889
6890 static cp_declarator *
6891 cp_parser_direct_new_declarator (cp_parser* parser)
6892 {
6893   cp_declarator *declarator = NULL;
6894
6895   while (true)
6896     {
6897       tree expression;
6898       cp_token *token;
6899
6900       /* Look for the opening `['.  */
6901       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6902
6903       token = cp_lexer_peek_token (parser->lexer);
6904       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6905       /* The standard requires that the expression have integral
6906          type.  DR 74 adds enumeration types.  We believe that the
6907          real intent is that these expressions be handled like the
6908          expression in a `switch' condition, which also allows
6909          classes with a single conversion to integral or
6910          enumeration type.  */
6911       if (!processing_template_decl)
6912         {
6913           expression
6914             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6915                                           expression,
6916                                           /*complain=*/true);
6917           if (!expression)
6918             {
6919               error_at (token->location,
6920                         "expression in new-declarator must have integral "
6921                         "or enumeration type");
6922               expression = error_mark_node;
6923             }
6924         }
6925
6926       /* Look for the closing `]'.  */
6927       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6928
6929       /* Add this bound to the declarator.  */
6930       declarator = make_array_declarator (declarator, expression);
6931
6932       /* If the next token is not a `[', then there are no more
6933          bounds.  */
6934       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6935         break;
6936     }
6937
6938   return declarator;
6939 }
6940
6941 /* Parse a new-initializer.
6942
6943    new-initializer:
6944      ( expression-list [opt] )
6945      braced-init-list
6946
6947    Returns a representation of the expression-list.  */
6948
6949 static VEC(tree,gc) *
6950 cp_parser_new_initializer (cp_parser* parser)
6951 {
6952   VEC(tree,gc) *expression_list;
6953
6954   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6955     {
6956       tree t;
6957       bool expr_non_constant_p;
6958       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6959       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6960       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6961       expression_list = make_tree_vector_single (t);
6962     }
6963   else
6964     expression_list = (cp_parser_parenthesized_expression_list
6965                        (parser, non_attr, /*cast_p=*/false,
6966                         /*allow_expansion_p=*/true,
6967                         /*non_constant_p=*/NULL));
6968
6969   return expression_list;
6970 }
6971
6972 /* Parse a delete-expression.
6973
6974    delete-expression:
6975      :: [opt] delete cast-expression
6976      :: [opt] delete [ ] cast-expression
6977
6978    Returns a representation of the expression.  */
6979
6980 static tree
6981 cp_parser_delete_expression (cp_parser* parser)
6982 {
6983   bool global_scope_p;
6984   bool array_p;
6985   tree expression;
6986
6987   /* Look for the optional `::' operator.  */
6988   global_scope_p
6989     = (cp_parser_global_scope_opt (parser,
6990                                    /*current_scope_valid_p=*/false)
6991        != NULL_TREE);
6992   /* Look for the `delete' keyword.  */
6993   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6994   /* See if the array syntax is in use.  */
6995   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6996     {
6997       /* Consume the `[' token.  */
6998       cp_lexer_consume_token (parser->lexer);
6999       /* Look for the `]' token.  */
7000       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7001       /* Remember that this is the `[]' construct.  */
7002       array_p = true;
7003     }
7004   else
7005     array_p = false;
7006
7007   /* Parse the cast-expression.  */
7008   expression = cp_parser_simple_cast_expression (parser);
7009
7010   /* A delete-expression may not appear in an integral constant
7011      expression.  */
7012   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7013     return error_mark_node;
7014
7015   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7016                         tf_warning_or_error);
7017 }
7018
7019 /* Returns true if TOKEN may start a cast-expression and false
7020    otherwise.  */
7021
7022 static bool
7023 cp_parser_token_starts_cast_expression (cp_token *token)
7024 {
7025   switch (token->type)
7026     {
7027     case CPP_COMMA:
7028     case CPP_SEMICOLON:
7029     case CPP_QUERY:
7030     case CPP_COLON:
7031     case CPP_CLOSE_SQUARE:
7032     case CPP_CLOSE_PAREN:
7033     case CPP_CLOSE_BRACE:
7034     case CPP_DOT:
7035     case CPP_DOT_STAR:
7036     case CPP_DEREF:
7037     case CPP_DEREF_STAR:
7038     case CPP_DIV:
7039     case CPP_MOD:
7040     case CPP_LSHIFT:
7041     case CPP_RSHIFT:
7042     case CPP_LESS:
7043     case CPP_GREATER:
7044     case CPP_LESS_EQ:
7045     case CPP_GREATER_EQ:
7046     case CPP_EQ_EQ:
7047     case CPP_NOT_EQ:
7048     case CPP_EQ:
7049     case CPP_MULT_EQ:
7050     case CPP_DIV_EQ:
7051     case CPP_MOD_EQ:
7052     case CPP_PLUS_EQ:
7053     case CPP_MINUS_EQ:
7054     case CPP_RSHIFT_EQ:
7055     case CPP_LSHIFT_EQ:
7056     case CPP_AND_EQ:
7057     case CPP_XOR_EQ:
7058     case CPP_OR_EQ:
7059     case CPP_XOR:
7060     case CPP_OR:
7061     case CPP_OR_OR:
7062     case CPP_EOF:
7063       return false;
7064
7065       /* '[' may start a primary-expression in obj-c++.  */
7066     case CPP_OPEN_SQUARE:
7067       return c_dialect_objc ();
7068
7069     default:
7070       return true;
7071     }
7072 }
7073
7074 /* Parse a cast-expression.
7075
7076    cast-expression:
7077      unary-expression
7078      ( type-id ) cast-expression
7079
7080    ADDRESS_P is true iff the unary-expression is appearing as the
7081    operand of the `&' operator.   CAST_P is true if this expression is
7082    the target of a cast.
7083
7084    Returns a representation of the expression.  */
7085
7086 static tree
7087 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7088                            cp_id_kind * pidk)
7089 {
7090   /* If it's a `(', then we might be looking at a cast.  */
7091   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7092     {
7093       tree type = NULL_TREE;
7094       tree expr = NULL_TREE;
7095       bool compound_literal_p;
7096       const char *saved_message;
7097
7098       /* There's no way to know yet whether or not this is a cast.
7099          For example, `(int (3))' is a unary-expression, while `(int)
7100          3' is a cast.  So, we resort to parsing tentatively.  */
7101       cp_parser_parse_tentatively (parser);
7102       /* Types may not be defined in a cast.  */
7103       saved_message = parser->type_definition_forbidden_message;
7104       parser->type_definition_forbidden_message
7105         = G_("types may not be defined in casts");
7106       /* Consume the `('.  */
7107       cp_lexer_consume_token (parser->lexer);
7108       /* A very tricky bit is that `(struct S) { 3 }' is a
7109          compound-literal (which we permit in C++ as an extension).
7110          But, that construct is not a cast-expression -- it is a
7111          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7112          is legal; if the compound-literal were a cast-expression,
7113          you'd need an extra set of parentheses.)  But, if we parse
7114          the type-id, and it happens to be a class-specifier, then we
7115          will commit to the parse at that point, because we cannot
7116          undo the action that is done when creating a new class.  So,
7117          then we cannot back up and do a postfix-expression.
7118
7119          Therefore, we scan ahead to the closing `)', and check to see
7120          if the token after the `)' is a `{'.  If so, we are not
7121          looking at a cast-expression.
7122
7123          Save tokens so that we can put them back.  */
7124       cp_lexer_save_tokens (parser->lexer);
7125       /* Skip tokens until the next token is a closing parenthesis.
7126          If we find the closing `)', and the next token is a `{', then
7127          we are looking at a compound-literal.  */
7128       compound_literal_p
7129         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7130                                                   /*consume_paren=*/true)
7131            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7132       /* Roll back the tokens we skipped.  */
7133       cp_lexer_rollback_tokens (parser->lexer);
7134       /* If we were looking at a compound-literal, simulate an error
7135          so that the call to cp_parser_parse_definitely below will
7136          fail.  */
7137       if (compound_literal_p)
7138         cp_parser_simulate_error (parser);
7139       else
7140         {
7141           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7142           parser->in_type_id_in_expr_p = true;
7143           /* Look for the type-id.  */
7144           type = cp_parser_type_id (parser);
7145           /* Look for the closing `)'.  */
7146           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7147           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7148         }
7149
7150       /* Restore the saved message.  */
7151       parser->type_definition_forbidden_message = saved_message;
7152
7153       /* At this point this can only be either a cast or a
7154          parenthesized ctor such as `(T ())' that looks like a cast to
7155          function returning T.  */
7156       if (!cp_parser_error_occurred (parser)
7157           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7158                                                      (parser->lexer)))
7159         {
7160           cp_parser_parse_definitely (parser);
7161           expr = cp_parser_cast_expression (parser,
7162                                             /*address_p=*/false,
7163                                             /*cast_p=*/true, pidk);
7164
7165           /* Warn about old-style casts, if so requested.  */
7166           if (warn_old_style_cast
7167               && !in_system_header
7168               && !VOID_TYPE_P (type)
7169               && current_lang_name != lang_name_c)
7170             warning (OPT_Wold_style_cast, "use of old-style cast");
7171
7172           /* Only type conversions to integral or enumeration types
7173              can be used in constant-expressions.  */
7174           if (!cast_valid_in_integral_constant_expression_p (type)
7175               && cp_parser_non_integral_constant_expression (parser,
7176                                                              NIC_CAST))
7177             return error_mark_node;
7178
7179           /* Perform the cast.  */
7180           expr = build_c_cast (input_location, type, expr);
7181           return expr;
7182         }
7183       else 
7184         cp_parser_abort_tentative_parse (parser);
7185     }
7186
7187   /* If we get here, then it's not a cast, so it must be a
7188      unary-expression.  */
7189   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7190 }
7191
7192 /* Parse a binary expression of the general form:
7193
7194    pm-expression:
7195      cast-expression
7196      pm-expression .* cast-expression
7197      pm-expression ->* cast-expression
7198
7199    multiplicative-expression:
7200      pm-expression
7201      multiplicative-expression * pm-expression
7202      multiplicative-expression / pm-expression
7203      multiplicative-expression % pm-expression
7204
7205    additive-expression:
7206      multiplicative-expression
7207      additive-expression + multiplicative-expression
7208      additive-expression - multiplicative-expression
7209
7210    shift-expression:
7211      additive-expression
7212      shift-expression << additive-expression
7213      shift-expression >> additive-expression
7214
7215    relational-expression:
7216      shift-expression
7217      relational-expression < shift-expression
7218      relational-expression > shift-expression
7219      relational-expression <= shift-expression
7220      relational-expression >= shift-expression
7221
7222   GNU Extension:
7223
7224    relational-expression:
7225      relational-expression <? shift-expression
7226      relational-expression >? shift-expression
7227
7228    equality-expression:
7229      relational-expression
7230      equality-expression == relational-expression
7231      equality-expression != relational-expression
7232
7233    and-expression:
7234      equality-expression
7235      and-expression & equality-expression
7236
7237    exclusive-or-expression:
7238      and-expression
7239      exclusive-or-expression ^ and-expression
7240
7241    inclusive-or-expression:
7242      exclusive-or-expression
7243      inclusive-or-expression | exclusive-or-expression
7244
7245    logical-and-expression:
7246      inclusive-or-expression
7247      logical-and-expression && inclusive-or-expression
7248
7249    logical-or-expression:
7250      logical-and-expression
7251      logical-or-expression || logical-and-expression
7252
7253    All these are implemented with a single function like:
7254
7255    binary-expression:
7256      simple-cast-expression
7257      binary-expression <token> binary-expression
7258
7259    CAST_P is true if this expression is the target of a cast.
7260
7261    The binops_by_token map is used to get the tree codes for each <token> type.
7262    binary-expressions are associated according to a precedence table.  */
7263
7264 #define TOKEN_PRECEDENCE(token)                              \
7265 (((token->type == CPP_GREATER                                \
7266    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7267   && !parser->greater_than_is_operator_p)                    \
7268  ? PREC_NOT_OPERATOR                                         \
7269  : binops_by_token[token->type].prec)
7270
7271 static tree
7272 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7273                              bool no_toplevel_fold_p,
7274                              enum cp_parser_prec prec,
7275                              cp_id_kind * pidk)
7276 {
7277   cp_parser_expression_stack stack;
7278   cp_parser_expression_stack_entry *sp = &stack[0];
7279   cp_parser_expression_stack_entry current;
7280   tree rhs;
7281   cp_token *token;
7282   enum tree_code rhs_type;
7283   enum cp_parser_prec new_prec, lookahead_prec;
7284   tree overload;
7285
7286   /* Parse the first expression.  */
7287   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7288                                            cast_p, pidk);
7289   current.lhs_type = ERROR_MARK;
7290   current.prec = prec;
7291
7292   if (cp_parser_error_occurred (parser))
7293     return error_mark_node;
7294
7295   for (;;)
7296     {
7297       /* Get an operator token.  */
7298       token = cp_lexer_peek_token (parser->lexer);
7299
7300       if (warn_cxx0x_compat
7301           && token->type == CPP_RSHIFT
7302           && !parser->greater_than_is_operator_p)
7303         {
7304           if (warning_at (token->location, OPT_Wc__0x_compat,
7305                           "%<>>%> operator is treated"
7306                           " as two right angle brackets in C++11"))
7307             inform (token->location,
7308                     "suggest parentheses around %<>>%> expression");
7309         }
7310
7311       new_prec = TOKEN_PRECEDENCE (token);
7312
7313       /* Popping an entry off the stack means we completed a subexpression:
7314          - either we found a token which is not an operator (`>' where it is not
7315            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7316            will happen repeatedly;
7317          - or, we found an operator which has lower priority.  This is the case
7318            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7319            parsing `3 * 4'.  */
7320       if (new_prec <= current.prec)
7321         {
7322           if (sp == stack)
7323             break;
7324           else
7325             goto pop;
7326         }
7327
7328      get_rhs:
7329       current.tree_type = binops_by_token[token->type].tree_type;
7330       current.loc = token->location;
7331
7332       /* We used the operator token.  */
7333       cp_lexer_consume_token (parser->lexer);
7334
7335       /* For "false && x" or "true || x", x will never be executed;
7336          disable warnings while evaluating it.  */
7337       if (current.tree_type == TRUTH_ANDIF_EXPR)
7338         c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7339       else if (current.tree_type == TRUTH_ORIF_EXPR)
7340         c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7341
7342       /* Extract another operand.  It may be the RHS of this expression
7343          or the LHS of a new, higher priority expression.  */
7344       rhs = cp_parser_simple_cast_expression (parser);
7345       rhs_type = ERROR_MARK;
7346
7347       /* Get another operator token.  Look up its precedence to avoid
7348          building a useless (immediately popped) stack entry for common
7349          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7350       token = cp_lexer_peek_token (parser->lexer);
7351       lookahead_prec = TOKEN_PRECEDENCE (token);
7352       if (lookahead_prec > new_prec)
7353         {
7354           /* ... and prepare to parse the RHS of the new, higher priority
7355              expression.  Since precedence levels on the stack are
7356              monotonically increasing, we do not have to care about
7357              stack overflows.  */
7358           *sp = current;
7359           ++sp;
7360           current.lhs = rhs;
7361           current.lhs_type = rhs_type;
7362           current.prec = new_prec;
7363           new_prec = lookahead_prec;
7364           goto get_rhs;
7365
7366          pop:
7367           lookahead_prec = new_prec;
7368           /* If the stack is not empty, we have parsed into LHS the right side
7369              (`4' in the example above) of an expression we had suspended.
7370              We can use the information on the stack to recover the LHS (`3')
7371              from the stack together with the tree code (`MULT_EXPR'), and
7372              the precedence of the higher level subexpression
7373              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7374              which will be used to actually build the additive expression.  */
7375           rhs = current.lhs;
7376           rhs_type = current.lhs_type;
7377           --sp;
7378           current = *sp;
7379         }
7380
7381       /* Undo the disabling of warnings done above.  */
7382       if (current.tree_type == TRUTH_ANDIF_EXPR)
7383         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7384       else if (current.tree_type == TRUTH_ORIF_EXPR)
7385         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7386
7387       overload = NULL;
7388       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7389          ERROR_MARK for everything that is not a binary expression.
7390          This makes warn_about_parentheses miss some warnings that
7391          involve unary operators.  For unary expressions we should
7392          pass the correct tree_code unless the unary expression was
7393          surrounded by parentheses.
7394       */
7395       if (no_toplevel_fold_p
7396           && lookahead_prec <= current.prec
7397           && sp == stack
7398           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7399         current.lhs = build2 (current.tree_type, boolean_type_node,
7400                               current.lhs, rhs);
7401       else
7402         current.lhs = build_x_binary_op (current.loc, current.tree_type,
7403                                          current.lhs, current.lhs_type,
7404                                          rhs, rhs_type, &overload,
7405                                          tf_warning_or_error);
7406       current.lhs_type = current.tree_type;
7407
7408       /* If the binary operator required the use of an overloaded operator,
7409          then this expression cannot be an integral constant-expression.
7410          An overloaded operator can be used even if both operands are
7411          otherwise permissible in an integral constant-expression if at
7412          least one of the operands is of enumeration type.  */
7413
7414       if (overload
7415           && cp_parser_non_integral_constant_expression (parser,
7416                                                          NIC_OVERLOADED))
7417         return error_mark_node;
7418     }
7419
7420   return current.lhs;
7421 }
7422
7423
7424 /* Parse the `? expression : assignment-expression' part of a
7425    conditional-expression.  The LOGICAL_OR_EXPR is the
7426    logical-or-expression that started the conditional-expression.
7427    Returns a representation of the entire conditional-expression.
7428
7429    This routine is used by cp_parser_assignment_expression.
7430
7431      ? expression : assignment-expression
7432
7433    GNU Extensions:
7434
7435      ? : assignment-expression */
7436
7437 static tree
7438 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7439 {
7440   tree expr;
7441   tree assignment_expr;
7442   struct cp_token *token;
7443   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7444
7445   /* Consume the `?' token.  */
7446   cp_lexer_consume_token (parser->lexer);
7447   token = cp_lexer_peek_token (parser->lexer);
7448   if (cp_parser_allow_gnu_extensions_p (parser)
7449       && token->type == CPP_COLON)
7450     {
7451       pedwarn (token->location, OPT_Wpedantic, 
7452                "ISO C++ does not allow ?: with omitted middle operand");
7453       /* Implicit true clause.  */
7454       expr = NULL_TREE;
7455       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7456       warn_for_omitted_condop (token->location, logical_or_expr);
7457     }
7458   else
7459     {
7460       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7461       parser->colon_corrects_to_scope_p = false;
7462       /* Parse the expression.  */
7463       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7464       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7465       c_inhibit_evaluation_warnings +=
7466         ((logical_or_expr == truthvalue_true_node)
7467          - (logical_or_expr == truthvalue_false_node));
7468       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7469     }
7470
7471   /* The next token should be a `:'.  */
7472   cp_parser_require (parser, CPP_COLON, RT_COLON);
7473   /* Parse the assignment-expression.  */
7474   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7475   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7476
7477   /* Build the conditional-expression.  */
7478   return build_x_conditional_expr (loc, logical_or_expr,
7479                                    expr,
7480                                    assignment_expr,
7481                                    tf_warning_or_error);
7482 }
7483
7484 /* Parse an assignment-expression.
7485
7486    assignment-expression:
7487      conditional-expression
7488      logical-or-expression assignment-operator assignment_expression
7489      throw-expression
7490
7491    CAST_P is true if this expression is the target of a cast.
7492
7493    Returns a representation for the expression.  */
7494
7495 static tree
7496 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7497                                  cp_id_kind * pidk)
7498 {
7499   tree expr;
7500
7501   /* If the next token is the `throw' keyword, then we're looking at
7502      a throw-expression.  */
7503   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7504     expr = cp_parser_throw_expression (parser);
7505   /* Otherwise, it must be that we are looking at a
7506      logical-or-expression.  */
7507   else
7508     {
7509       /* Parse the binary expressions (logical-or-expression).  */
7510       expr = cp_parser_binary_expression (parser, cast_p, false,
7511                                           PREC_NOT_OPERATOR, pidk);
7512       /* If the next token is a `?' then we're actually looking at a
7513          conditional-expression.  */
7514       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7515         return cp_parser_question_colon_clause (parser, expr);
7516       else
7517         {
7518           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7519
7520           /* If it's an assignment-operator, we're using the second
7521              production.  */
7522           enum tree_code assignment_operator
7523             = cp_parser_assignment_operator_opt (parser);
7524           if (assignment_operator != ERROR_MARK)
7525             {
7526               bool non_constant_p;
7527               location_t saved_input_location;
7528
7529               /* Parse the right-hand side of the assignment.  */
7530               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7531
7532               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7533                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7534
7535               /* An assignment may not appear in a
7536                  constant-expression.  */
7537               if (cp_parser_non_integral_constant_expression (parser,
7538                                                               NIC_ASSIGNMENT))
7539                 return error_mark_node;
7540               /* Build the assignment expression.  Its default
7541                  location is the location of the '=' token.  */
7542               saved_input_location = input_location;
7543               input_location = loc;
7544               expr = build_x_modify_expr (loc, expr,
7545                                           assignment_operator,
7546                                           rhs,
7547                                           tf_warning_or_error);
7548               input_location = saved_input_location;
7549             }
7550         }
7551     }
7552
7553   return expr;
7554 }
7555
7556 /* Parse an (optional) assignment-operator.
7557
7558    assignment-operator: one of
7559      = *= /= %= += -= >>= <<= &= ^= |=
7560
7561    GNU Extension:
7562
7563    assignment-operator: one of
7564      <?= >?=
7565
7566    If the next token is an assignment operator, the corresponding tree
7567    code is returned, and the token is consumed.  For example, for
7568    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7569    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7570    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7571    operator, ERROR_MARK is returned.  */
7572
7573 static enum tree_code
7574 cp_parser_assignment_operator_opt (cp_parser* parser)
7575 {
7576   enum tree_code op;
7577   cp_token *token;
7578
7579   /* Peek at the next token.  */
7580   token = cp_lexer_peek_token (parser->lexer);
7581
7582   switch (token->type)
7583     {
7584     case CPP_EQ:
7585       op = NOP_EXPR;
7586       break;
7587
7588     case CPP_MULT_EQ:
7589       op = MULT_EXPR;
7590       break;
7591
7592     case CPP_DIV_EQ:
7593       op = TRUNC_DIV_EXPR;
7594       break;
7595
7596     case CPP_MOD_EQ:
7597       op = TRUNC_MOD_EXPR;
7598       break;
7599
7600     case CPP_PLUS_EQ:
7601       op = PLUS_EXPR;
7602       break;
7603
7604     case CPP_MINUS_EQ:
7605       op = MINUS_EXPR;
7606       break;
7607
7608     case CPP_RSHIFT_EQ:
7609       op = RSHIFT_EXPR;
7610       break;
7611
7612     case CPP_LSHIFT_EQ:
7613       op = LSHIFT_EXPR;
7614       break;
7615
7616     case CPP_AND_EQ:
7617       op = BIT_AND_EXPR;
7618       break;
7619
7620     case CPP_XOR_EQ:
7621       op = BIT_XOR_EXPR;
7622       break;
7623
7624     case CPP_OR_EQ:
7625       op = BIT_IOR_EXPR;
7626       break;
7627
7628     default:
7629       /* Nothing else is an assignment operator.  */
7630       op = ERROR_MARK;
7631     }
7632
7633   /* If it was an assignment operator, consume it.  */
7634   if (op != ERROR_MARK)
7635     cp_lexer_consume_token (parser->lexer);
7636
7637   return op;
7638 }
7639
7640 /* Parse an expression.
7641
7642    expression:
7643      assignment-expression
7644      expression , assignment-expression
7645
7646    CAST_P is true if this expression is the target of a cast.
7647
7648    Returns a representation of the expression.  */
7649
7650 static tree
7651 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7652 {
7653   tree expression = NULL_TREE;
7654   location_t loc = UNKNOWN_LOCATION;
7655
7656   while (true)
7657     {
7658       tree assignment_expression;
7659
7660       /* Parse the next assignment-expression.  */
7661       assignment_expression
7662         = cp_parser_assignment_expression (parser, cast_p, pidk);
7663       /* If this is the first assignment-expression, we can just
7664          save it away.  */
7665       if (!expression)
7666         expression = assignment_expression;
7667       else
7668         expression = build_x_compound_expr (loc, expression,
7669                                             assignment_expression,
7670                                             tf_warning_or_error);
7671       /* If the next token is not a comma, then we are done with the
7672          expression.  */
7673       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7674         break;
7675       /* Consume the `,'.  */
7676       loc = cp_lexer_peek_token (parser->lexer)->location;
7677       cp_lexer_consume_token (parser->lexer);
7678       /* A comma operator cannot appear in a constant-expression.  */
7679       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7680         expression = error_mark_node;
7681     }
7682
7683   return expression;
7684 }
7685
7686 /* Parse a constant-expression.
7687
7688    constant-expression:
7689      conditional-expression
7690
7691   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7692   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7693   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7694   is false, NON_CONSTANT_P should be NULL.  */
7695
7696 static tree
7697 cp_parser_constant_expression (cp_parser* parser,
7698                                bool allow_non_constant_p,
7699                                bool *non_constant_p)
7700 {
7701   bool saved_integral_constant_expression_p;
7702   bool saved_allow_non_integral_constant_expression_p;
7703   bool saved_non_integral_constant_expression_p;
7704   tree expression;
7705
7706   /* It might seem that we could simply parse the
7707      conditional-expression, and then check to see if it were
7708      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7709      one that the compiler can figure out is constant, possibly after
7710      doing some simplifications or optimizations.  The standard has a
7711      precise definition of constant-expression, and we must honor
7712      that, even though it is somewhat more restrictive.
7713
7714      For example:
7715
7716        int i[(2, 3)];
7717
7718      is not a legal declaration, because `(2, 3)' is not a
7719      constant-expression.  The `,' operator is forbidden in a
7720      constant-expression.  However, GCC's constant-folding machinery
7721      will fold this operation to an INTEGER_CST for `3'.  */
7722
7723   /* Save the old settings.  */
7724   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7725   saved_allow_non_integral_constant_expression_p
7726     = parser->allow_non_integral_constant_expression_p;
7727   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7728   /* We are now parsing a constant-expression.  */
7729   parser->integral_constant_expression_p = true;
7730   parser->allow_non_integral_constant_expression_p
7731     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7732   parser->non_integral_constant_expression_p = false;
7733   /* Although the grammar says "conditional-expression", we parse an
7734      "assignment-expression", which also permits "throw-expression"
7735      and the use of assignment operators.  In the case that
7736      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7737      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7738      actually essential that we look for an assignment-expression.
7739      For example, cp_parser_initializer_clauses uses this function to
7740      determine whether a particular assignment-expression is in fact
7741      constant.  */
7742   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7743   /* Restore the old settings.  */
7744   parser->integral_constant_expression_p
7745     = saved_integral_constant_expression_p;
7746   parser->allow_non_integral_constant_expression_p
7747     = saved_allow_non_integral_constant_expression_p;
7748   if (cxx_dialect >= cxx0x)
7749     {
7750       /* Require an rvalue constant expression here; that's what our
7751          callers expect.  Reference constant expressions are handled
7752          separately in e.g. cp_parser_template_argument.  */
7753       bool is_const = potential_rvalue_constant_expression (expression);
7754       parser->non_integral_constant_expression_p = !is_const;
7755       if (!is_const && !allow_non_constant_p)
7756         require_potential_rvalue_constant_expression (expression);
7757     }
7758   if (allow_non_constant_p)
7759     *non_constant_p = parser->non_integral_constant_expression_p;
7760   parser->non_integral_constant_expression_p
7761     = saved_non_integral_constant_expression_p;
7762
7763   return expression;
7764 }
7765
7766 /* Parse __builtin_offsetof.
7767
7768    offsetof-expression:
7769      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7770
7771    offsetof-member-designator:
7772      id-expression
7773      | offsetof-member-designator "." id-expression
7774      | offsetof-member-designator "[" expression "]"
7775      | offsetof-member-designator "->" id-expression  */
7776
7777 static tree
7778 cp_parser_builtin_offsetof (cp_parser *parser)
7779 {
7780   int save_ice_p, save_non_ice_p;
7781   tree type, expr;
7782   cp_id_kind dummy;
7783   cp_token *token;
7784
7785   /* We're about to accept non-integral-constant things, but will
7786      definitely yield an integral constant expression.  Save and
7787      restore these values around our local parsing.  */
7788   save_ice_p = parser->integral_constant_expression_p;
7789   save_non_ice_p = parser->non_integral_constant_expression_p;
7790
7791   /* Consume the "__builtin_offsetof" token.  */
7792   cp_lexer_consume_token (parser->lexer);
7793   /* Consume the opening `('.  */
7794   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7795   /* Parse the type-id.  */
7796   type = cp_parser_type_id (parser);
7797   /* Look for the `,'.  */
7798   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7799   token = cp_lexer_peek_token (parser->lexer);
7800
7801   /* Build the (type *)null that begins the traditional offsetof macro.  */
7802   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7803                             tf_warning_or_error);
7804
7805   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7806   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7807                                                  true, &dummy, token->location);
7808   while (true)
7809     {
7810       token = cp_lexer_peek_token (parser->lexer);
7811       switch (token->type)
7812         {
7813         case CPP_OPEN_SQUARE:
7814           /* offsetof-member-designator "[" expression "]" */
7815           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7816           break;
7817
7818         case CPP_DEREF:
7819           /* offsetof-member-designator "->" identifier */
7820           expr = grok_array_decl (token->location, expr, integer_zero_node);
7821           /* FALLTHRU */
7822
7823         case CPP_DOT:
7824           /* offsetof-member-designator "." identifier */
7825           cp_lexer_consume_token (parser->lexer);
7826           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7827                                                          expr, true, &dummy,
7828                                                          token->location);
7829           break;
7830
7831         case CPP_CLOSE_PAREN:
7832           /* Consume the ")" token.  */
7833           cp_lexer_consume_token (parser->lexer);
7834           goto success;
7835
7836         default:
7837           /* Error.  We know the following require will fail, but
7838              that gives the proper error message.  */
7839           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7840           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7841           expr = error_mark_node;
7842           goto failure;
7843         }
7844     }
7845
7846  success:
7847   /* If we're processing a template, we can't finish the semantics yet.
7848      Otherwise we can fold the entire expression now.  */
7849   if (processing_template_decl)
7850     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7851   else
7852     expr = finish_offsetof (expr);
7853
7854  failure:
7855   parser->integral_constant_expression_p = save_ice_p;
7856   parser->non_integral_constant_expression_p = save_non_ice_p;
7857
7858   return expr;
7859 }
7860
7861 /* Parse a trait expression.
7862
7863    Returns a representation of the expression, the underlying type
7864    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7865
7866 static tree
7867 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7868 {
7869   cp_trait_kind kind;
7870   tree type1, type2 = NULL_TREE;
7871   bool binary = false;
7872   cp_decl_specifier_seq decl_specs;
7873
7874   switch (keyword)
7875     {
7876     case RID_HAS_NOTHROW_ASSIGN:
7877       kind = CPTK_HAS_NOTHROW_ASSIGN;
7878       break;
7879     case RID_HAS_NOTHROW_CONSTRUCTOR:
7880       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7881       break;
7882     case RID_HAS_NOTHROW_COPY:
7883       kind = CPTK_HAS_NOTHROW_COPY;
7884       break;
7885     case RID_HAS_TRIVIAL_ASSIGN:
7886       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7887       break;
7888     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7889       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7890       break;
7891     case RID_HAS_TRIVIAL_COPY:
7892       kind = CPTK_HAS_TRIVIAL_COPY;
7893       break;
7894     case RID_HAS_TRIVIAL_DESTRUCTOR:
7895       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7896       break;
7897     case RID_HAS_VIRTUAL_DESTRUCTOR:
7898       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7899       break;
7900     case RID_IS_ABSTRACT:
7901       kind = CPTK_IS_ABSTRACT;
7902       break;
7903     case RID_IS_BASE_OF:
7904       kind = CPTK_IS_BASE_OF;
7905       binary = true;
7906       break;
7907     case RID_IS_CLASS:
7908       kind = CPTK_IS_CLASS;
7909       break;
7910     case RID_IS_CONVERTIBLE_TO:
7911       kind = CPTK_IS_CONVERTIBLE_TO;
7912       binary = true;
7913       break;
7914     case RID_IS_EMPTY:
7915       kind = CPTK_IS_EMPTY;
7916       break;
7917     case RID_IS_ENUM:
7918       kind = CPTK_IS_ENUM;
7919       break;
7920     case RID_IS_FINAL:
7921       kind = CPTK_IS_FINAL;
7922       break;
7923     case RID_IS_LITERAL_TYPE:
7924       kind = CPTK_IS_LITERAL_TYPE;
7925       break;
7926     case RID_IS_POD:
7927       kind = CPTK_IS_POD;
7928       break;
7929     case RID_IS_POLYMORPHIC:
7930       kind = CPTK_IS_POLYMORPHIC;
7931       break;
7932     case RID_IS_STD_LAYOUT:
7933       kind = CPTK_IS_STD_LAYOUT;
7934       break;
7935     case RID_IS_TRIVIAL:
7936       kind = CPTK_IS_TRIVIAL;
7937       break;
7938     case RID_IS_UNION:
7939       kind = CPTK_IS_UNION;
7940       break;
7941     case RID_UNDERLYING_TYPE:
7942       kind = CPTK_UNDERLYING_TYPE;
7943       break;
7944     case RID_BASES:
7945       kind = CPTK_BASES;
7946       break;
7947     case RID_DIRECT_BASES:
7948       kind = CPTK_DIRECT_BASES;
7949       break;
7950     default:
7951       gcc_unreachable ();
7952     }
7953
7954   /* Consume the token.  */
7955   cp_lexer_consume_token (parser->lexer);
7956
7957   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7958
7959   type1 = cp_parser_type_id (parser);
7960
7961   if (type1 == error_mark_node)
7962     return error_mark_node;
7963
7964   /* Build a trivial decl-specifier-seq.  */
7965   clear_decl_specs (&decl_specs);
7966   decl_specs.type = type1;
7967
7968   /* Call grokdeclarator to figure out what type this is.  */
7969   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7970                           /*initialized=*/0, /*attrlist=*/NULL);
7971
7972   if (binary)
7973     {
7974       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7975  
7976       type2 = cp_parser_type_id (parser);
7977
7978       if (type2 == error_mark_node)
7979         return error_mark_node;
7980
7981       /* Build a trivial decl-specifier-seq.  */
7982       clear_decl_specs (&decl_specs);
7983       decl_specs.type = type2;
7984
7985       /* Call grokdeclarator to figure out what type this is.  */
7986       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7987                               /*initialized=*/0, /*attrlist=*/NULL);
7988     }
7989
7990   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7991
7992   /* Complete the trait expression, which may mean either processing
7993      the trait expr now or saving it for template instantiation.  */
7994   switch(kind)
7995     {
7996     case CPTK_UNDERLYING_TYPE:
7997       return finish_underlying_type (type1);
7998     case CPTK_BASES:
7999       return finish_bases (type1, false);
8000     case CPTK_DIRECT_BASES:
8001       return finish_bases (type1, true);
8002     default:
8003       return finish_trait_expr (kind, type1, type2);
8004     }
8005 }
8006
8007 /* Lambdas that appear in variable initializer or default argument scope
8008    get that in their mangling, so we need to record it.  We might as well
8009    use the count for function and namespace scopes as well.  */
8010 static GTY(()) tree lambda_scope;
8011 static GTY(()) int lambda_count;
8012 typedef struct GTY(()) tree_int
8013 {
8014   tree t;
8015   int i;
8016 } tree_int;
8017 DEF_VEC_O(tree_int);
8018 DEF_VEC_ALLOC_O(tree_int,gc);
8019 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8020
8021 static void
8022 start_lambda_scope (tree decl)
8023 {
8024   tree_int ti;
8025   gcc_assert (decl);
8026   /* Once we're inside a function, we ignore other scopes and just push
8027      the function again so that popping works properly.  */
8028   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8029     decl = current_function_decl;
8030   ti.t = lambda_scope;
8031   ti.i = lambda_count;
8032   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8033   if (lambda_scope != decl)
8034     {
8035       /* Don't reset the count if we're still in the same function.  */
8036       lambda_scope = decl;
8037       lambda_count = 0;
8038     }
8039 }
8040
8041 static void
8042 record_lambda_scope (tree lambda)
8043 {
8044   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8045   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8046 }
8047
8048 static void
8049 finish_lambda_scope (void)
8050 {
8051   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8052   if (lambda_scope != p->t)
8053     {
8054       lambda_scope = p->t;
8055       lambda_count = p->i;
8056     }
8057   VEC_pop (tree_int, lambda_scope_stack);
8058 }
8059
8060 /* Parse a lambda expression.
8061
8062    lambda-expression:
8063      lambda-introducer lambda-declarator [opt] compound-statement
8064
8065    Returns a representation of the expression.  */
8066
8067 static tree
8068 cp_parser_lambda_expression (cp_parser* parser)
8069 {
8070   tree lambda_expr = build_lambda_expr ();
8071   tree type;
8072   bool ok;
8073
8074   LAMBDA_EXPR_LOCATION (lambda_expr)
8075     = cp_lexer_peek_token (parser->lexer)->location;
8076
8077   if (cp_unevaluated_operand)
8078     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8079               "lambda-expression in unevaluated context");
8080
8081   /* We may be in the middle of deferred access check.  Disable
8082      it now.  */
8083   push_deferring_access_checks (dk_no_deferred);
8084
8085   cp_parser_lambda_introducer (parser, lambda_expr);
8086
8087   type = begin_lambda_type (lambda_expr);
8088   if (type == error_mark_node)
8089     return error_mark_node;
8090
8091   record_lambda_scope (lambda_expr);
8092
8093   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8094   determine_visibility (TYPE_NAME (type));
8095
8096   /* Now that we've started the type, add the capture fields for any
8097      explicit captures.  */
8098   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8099
8100   {
8101     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8102     unsigned int saved_num_template_parameter_lists
8103         = parser->num_template_parameter_lists;
8104     unsigned char in_statement = parser->in_statement;
8105     bool in_switch_statement_p = parser->in_switch_statement_p;
8106
8107     parser->num_template_parameter_lists = 0;
8108     parser->in_statement = 0;
8109     parser->in_switch_statement_p = false;
8110
8111     /* By virtue of defining a local class, a lambda expression has access to
8112        the private variables of enclosing classes.  */
8113
8114     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8115
8116     if (ok)
8117       cp_parser_lambda_body (parser, lambda_expr);
8118     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8119       cp_parser_skip_to_end_of_block_or_statement (parser);
8120
8121     /* The capture list was built up in reverse order; fix that now.  */
8122     {
8123       tree newlist = NULL_TREE;
8124       tree elt, next;
8125
8126       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8127            elt; elt = next)
8128         {
8129           next = TREE_CHAIN (elt);
8130           TREE_CHAIN (elt) = newlist;
8131           newlist = elt;
8132         }
8133       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8134     }
8135
8136     if (ok)
8137       maybe_add_lambda_conv_op (type);
8138
8139     type = finish_struct (type, /*attributes=*/NULL_TREE);
8140
8141     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8142     parser->in_statement = in_statement;
8143     parser->in_switch_statement_p = in_switch_statement_p;
8144   }
8145
8146   pop_deferring_access_checks ();
8147
8148   /* This field is only used during parsing of the lambda.  */
8149   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8150
8151   /* This lambda shouldn't have any proxies left at this point.  */
8152   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8153   /* And now that we're done, push proxies for an enclosing lambda.  */
8154   insert_pending_capture_proxies ();
8155
8156   if (ok)
8157     return build_lambda_object (lambda_expr);
8158   else
8159     return error_mark_node;
8160 }
8161
8162 /* Parse the beginning of a lambda expression.
8163
8164    lambda-introducer:
8165      [ lambda-capture [opt] ]
8166
8167    LAMBDA_EXPR is the current representation of the lambda expression.  */
8168
8169 static void
8170 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8171 {
8172   /* Need commas after the first capture.  */
8173   bool first = true;
8174
8175   /* Eat the leading `['.  */
8176   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8177
8178   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8179   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8180       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8181     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8182   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8183     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8184
8185   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8186     {
8187       cp_lexer_consume_token (parser->lexer);
8188       first = false;
8189     }
8190
8191   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8192     {
8193       cp_token* capture_token;
8194       tree capture_id;
8195       tree capture_init_expr;
8196       cp_id_kind idk = CP_ID_KIND_NONE;
8197       bool explicit_init_p = false;
8198
8199       enum capture_kind_type
8200       {
8201         BY_COPY,
8202         BY_REFERENCE
8203       };
8204       enum capture_kind_type capture_kind = BY_COPY;
8205
8206       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8207         {
8208           error ("expected end of capture-list");
8209           return;
8210         }
8211
8212       if (first)
8213         first = false;
8214       else
8215         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8216
8217       /* Possibly capture `this'.  */
8218       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8219         {
8220           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8221           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8222             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8223                      "with by-copy capture default");
8224           cp_lexer_consume_token (parser->lexer);
8225           add_capture (lambda_expr,
8226                        /*id=*/this_identifier,
8227                        /*initializer=*/finish_this_expr(),
8228                        /*by_reference_p=*/false,
8229                        explicit_init_p);
8230           continue;
8231         }
8232
8233       /* Remember whether we want to capture as a reference or not.  */
8234       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8235         {
8236           capture_kind = BY_REFERENCE;
8237           cp_lexer_consume_token (parser->lexer);
8238         }
8239
8240       /* Get the identifier.  */
8241       capture_token = cp_lexer_peek_token (parser->lexer);
8242       capture_id = cp_parser_identifier (parser);
8243
8244       if (capture_id == error_mark_node)
8245         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8246            delimiters, but I modified this to stop on unnested ']' as well.  It
8247            was already changed to stop on unnested '}', so the
8248            "closing_parenthesis" name is no more misleading with my change.  */
8249         {
8250           cp_parser_skip_to_closing_parenthesis (parser,
8251                                                  /*recovering=*/true,
8252                                                  /*or_comma=*/true,
8253                                                  /*consume_paren=*/true);
8254           break;
8255         }
8256
8257       /* Find the initializer for this capture.  */
8258       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8259         {
8260           /* An explicit expression exists.  */
8261           cp_lexer_consume_token (parser->lexer);
8262           pedwarn (input_location, OPT_Wpedantic,
8263                    "ISO C++ does not allow initializers "
8264                    "in lambda expression capture lists");
8265           capture_init_expr = cp_parser_assignment_expression (parser,
8266                                                                /*cast_p=*/true,
8267                                                                &idk);
8268           explicit_init_p = true;
8269         }
8270       else
8271         {
8272           const char* error_msg;
8273
8274           /* Turn the identifier into an id-expression.  */
8275           capture_init_expr
8276             = cp_parser_lookup_name
8277                 (parser,
8278                  capture_id,
8279                  none_type,
8280                  /*is_template=*/false,
8281                  /*is_namespace=*/false,
8282                  /*check_dependency=*/true,
8283                  /*ambiguous_decls=*/NULL,
8284                  capture_token->location);
8285
8286           if (capture_init_expr == error_mark_node)
8287             {
8288               unqualified_name_lookup_error (capture_id);
8289               continue;
8290             }
8291           else if (DECL_P (capture_init_expr)
8292                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8293                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8294             {
8295               error_at (capture_token->location,
8296                         "capture of non-variable %qD ",
8297                         capture_init_expr);
8298               inform (0, "%q+#D declared here", capture_init_expr);
8299               continue;
8300             }
8301           if (TREE_CODE (capture_init_expr) == VAR_DECL
8302               && decl_storage_duration (capture_init_expr) != dk_auto)
8303             {
8304               pedwarn (capture_token->location, 0, "capture of variable "
8305                        "%qD with non-automatic storage duration",
8306                        capture_init_expr);
8307               inform (0, "%q+#D declared here", capture_init_expr);
8308               continue;
8309             }
8310
8311           capture_init_expr
8312             = finish_id_expression
8313                 (capture_id,
8314                  capture_init_expr,
8315                  parser->scope,
8316                  &idk,
8317                  /*integral_constant_expression_p=*/false,
8318                  /*allow_non_integral_constant_expression_p=*/false,
8319                  /*non_integral_constant_expression_p=*/NULL,
8320                  /*template_p=*/false,
8321                  /*done=*/true,
8322                  /*address_p=*/false,
8323                  /*template_arg_p=*/false,
8324                  &error_msg,
8325                  capture_token->location);
8326         }
8327
8328       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8329           && !explicit_init_p)
8330         {
8331           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8332               && capture_kind == BY_COPY)
8333             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8334                      "of %qD redundant with by-copy capture default",
8335                      capture_id);
8336           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8337               && capture_kind == BY_REFERENCE)
8338             pedwarn (capture_token->location, 0, "explicit by-reference "
8339                      "capture of %qD redundant with by-reference capture "
8340                      "default", capture_id);
8341         }
8342
8343       add_capture (lambda_expr,
8344                    capture_id,
8345                    capture_init_expr,
8346                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8347                    explicit_init_p);
8348     }
8349
8350   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8351 }
8352
8353 /* Parse the (optional) middle of a lambda expression.
8354
8355    lambda-declarator:
8356      ( parameter-declaration-clause [opt] )
8357        attribute-specifier [opt]
8358        mutable [opt]
8359        exception-specification [opt]
8360        lambda-return-type-clause [opt]
8361
8362    LAMBDA_EXPR is the current representation of the lambda expression.  */
8363
8364 static bool
8365 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8366 {
8367   /* 5.1.1.4 of the standard says:
8368        If a lambda-expression does not include a lambda-declarator, it is as if
8369        the lambda-declarator were ().
8370      This means an empty parameter list, no attributes, and no exception
8371      specification.  */
8372   tree param_list = void_list_node;
8373   tree attributes = NULL_TREE;
8374   tree exception_spec = NULL_TREE;
8375   tree t;
8376
8377   /* The lambda-declarator is optional, but must begin with an opening
8378      parenthesis if present.  */
8379   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8380     {
8381       cp_lexer_consume_token (parser->lexer);
8382
8383       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8384
8385       /* Parse parameters.  */
8386       param_list = cp_parser_parameter_declaration_clause (parser);
8387
8388       /* Default arguments shall not be specified in the
8389          parameter-declaration-clause of a lambda-declarator.  */
8390       for (t = param_list; t; t = TREE_CHAIN (t))
8391         if (TREE_PURPOSE (t))
8392           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8393                    "default argument specified for lambda parameter");
8394
8395       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8396
8397       attributes = cp_parser_attributes_opt (parser);
8398
8399       /* Parse optional `mutable' keyword.  */
8400       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8401         {
8402           cp_lexer_consume_token (parser->lexer);
8403           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8404         }
8405
8406       /* Parse optional exception specification.  */
8407       exception_spec = cp_parser_exception_specification_opt (parser);
8408
8409       /* Parse optional trailing return type.  */
8410       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8411         {
8412           cp_lexer_consume_token (parser->lexer);
8413           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8414         }
8415
8416       /* The function parameters must be in scope all the way until after the
8417          trailing-return-type in case of decltype.  */
8418       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8419         pop_binding (DECL_NAME (t), t);
8420
8421       leave_scope ();
8422     }
8423
8424   /* Create the function call operator.
8425
8426      Messing with declarators like this is no uglier than building up the
8427      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8428      other code.  */
8429   {
8430     cp_decl_specifier_seq return_type_specs;
8431     cp_declarator* declarator;
8432     tree fco;
8433     int quals;
8434     void *p;
8435
8436     clear_decl_specs (&return_type_specs);
8437     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8438       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8439     else
8440       /* Maybe we will deduce the return type later.  */
8441       return_type_specs.type = make_auto ();
8442
8443     p = obstack_alloc (&declarator_obstack, 0);
8444
8445     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8446                                      sfk_none);
8447
8448     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8449              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8450     declarator = make_call_declarator (declarator, param_list, quals,
8451                                        VIRT_SPEC_UNSPECIFIED,
8452                                        exception_spec,
8453                                        /*late_return_type=*/NULL_TREE);
8454     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8455
8456     fco = grokmethod (&return_type_specs,
8457                       declarator,
8458                       attributes);
8459     if (fco != error_mark_node)
8460       {
8461         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8462         DECL_ARTIFICIAL (fco) = 1;
8463         /* Give the object parameter a different name.  */
8464         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8465       }
8466
8467     finish_member_declaration (fco);
8468
8469     obstack_free (&declarator_obstack, p);
8470
8471     return (fco != error_mark_node);
8472   }
8473 }
8474
8475 /* Parse the body of a lambda expression, which is simply
8476
8477    compound-statement
8478
8479    but which requires special handling.
8480    LAMBDA_EXPR is the current representation of the lambda expression.  */
8481
8482 static void
8483 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8484 {
8485   bool nested = (current_function_decl != NULL_TREE);
8486   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8487   if (nested)
8488     push_function_context ();
8489   else
8490     /* Still increment function_depth so that we don't GC in the
8491        middle of an expression.  */
8492     ++function_depth;
8493   /* Clear this in case we're in the middle of a default argument.  */
8494   parser->local_variables_forbidden_p = false;
8495
8496   /* Finish the function call operator
8497      - class_specifier
8498      + late_parsing_for_member
8499      + function_definition_after_declarator
8500      + ctor_initializer_opt_and_function_body  */
8501   {
8502     tree fco = lambda_function (lambda_expr);
8503     tree body;
8504     bool done = false;
8505     tree compound_stmt;
8506     tree cap;
8507
8508     /* Let the front end know that we are going to be defining this
8509        function.  */
8510     start_preparsed_function (fco,
8511                               NULL_TREE,
8512                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8513
8514     start_lambda_scope (fco);
8515     body = begin_function_body ();
8516
8517     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8518       goto out;
8519
8520     /* Push the proxies for any explicit captures.  */
8521     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8522          cap = TREE_CHAIN (cap))
8523       build_capture_proxy (TREE_PURPOSE (cap));
8524
8525     compound_stmt = begin_compound_stmt (0);
8526
8527     /* 5.1.1.4 of the standard says:
8528          If a lambda-expression does not include a trailing-return-type, it
8529          is as if the trailing-return-type denotes the following type:
8530           * if the compound-statement is of the form
8531                { return attribute-specifier [opt] expression ; }
8532              the type of the returned expression after lvalue-to-rvalue
8533              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8534              (_conv.array_ 4.2), and function-to-pointer conversion
8535              (_conv.func_ 4.3);
8536           * otherwise, void.  */
8537
8538     /* In a lambda that has neither a lambda-return-type-clause
8539        nor a deducible form, errors should be reported for return statements
8540        in the body.  Since we used void as the placeholder return type, parsing
8541        the body as usual will give such desired behavior.  */
8542     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8543         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8544         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8545       {
8546         tree expr = NULL_TREE;
8547         cp_id_kind idk = CP_ID_KIND_NONE;
8548
8549         /* Parse tentatively in case there's more after the initial return
8550            statement.  */
8551         cp_parser_parse_tentatively (parser);
8552
8553         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8554
8555         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8556
8557         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8558         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8559
8560         if (cp_parser_parse_definitely (parser))
8561           {
8562             if (!processing_template_decl)
8563               apply_deduced_return_type (fco, lambda_return_type (expr));
8564
8565             /* Will get error here if type not deduced yet.  */
8566             finish_return_stmt (expr);
8567
8568             done = true;
8569           }
8570       }
8571
8572     if (!done)
8573       {
8574         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8575           cp_parser_label_declaration (parser);
8576         cp_parser_statement_seq_opt (parser, NULL_TREE);
8577         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8578       }
8579
8580     finish_compound_stmt (compound_stmt);
8581
8582   out:
8583     finish_function_body (body);
8584     finish_lambda_scope ();
8585
8586     /* Finish the function and generate code for it if necessary.  */
8587     expand_or_defer_fn (finish_function (/*inline*/2));
8588   }
8589
8590   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8591   if (nested)
8592     pop_function_context();
8593   else
8594     --function_depth;
8595 }
8596
8597 /* Statements [gram.stmt.stmt]  */
8598
8599 /* Parse a statement.
8600
8601    statement:
8602      labeled-statement
8603      expression-statement
8604      compound-statement
8605      selection-statement
8606      iteration-statement
8607      jump-statement
8608      declaration-statement
8609      try-block
8610
8611   TM Extension:
8612
8613    statement:
8614      atomic-statement
8615
8616   IN_COMPOUND is true when the statement is nested inside a
8617   cp_parser_compound_statement; this matters for certain pragmas.
8618
8619   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8620   is a (possibly labeled) if statement which is not enclosed in braces
8621   and has an else clause.  This is used to implement -Wparentheses.  */
8622
8623 static void
8624 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8625                      bool in_compound, bool *if_p)
8626 {
8627   tree statement;
8628   cp_token *token;
8629   location_t statement_location;
8630
8631  restart:
8632   if (if_p != NULL)
8633     *if_p = false;
8634   /* There is no statement yet.  */
8635   statement = NULL_TREE;
8636   /* Peek at the next token.  */
8637   token = cp_lexer_peek_token (parser->lexer);
8638   /* Remember the location of the first token in the statement.  */
8639   statement_location = token->location;
8640   /* If this is a keyword, then that will often determine what kind of
8641      statement we have.  */
8642   if (token->type == CPP_KEYWORD)
8643     {
8644       enum rid keyword = token->keyword;
8645
8646       switch (keyword)
8647         {
8648         case RID_CASE:
8649         case RID_DEFAULT:
8650           /* Looks like a labeled-statement with a case label.
8651              Parse the label, and then use tail recursion to parse
8652              the statement.  */
8653           cp_parser_label_for_labeled_statement (parser);
8654           goto restart;
8655
8656         case RID_IF:
8657         case RID_SWITCH:
8658           statement = cp_parser_selection_statement (parser, if_p);
8659           break;
8660
8661         case RID_WHILE:
8662         case RID_DO:
8663         case RID_FOR:
8664           statement = cp_parser_iteration_statement (parser);
8665           break;
8666
8667         case RID_BREAK:
8668         case RID_CONTINUE:
8669         case RID_RETURN:
8670         case RID_GOTO:
8671           statement = cp_parser_jump_statement (parser);
8672           break;
8673
8674           /* Objective-C++ exception-handling constructs.  */
8675         case RID_AT_TRY:
8676         case RID_AT_CATCH:
8677         case RID_AT_FINALLY:
8678         case RID_AT_SYNCHRONIZED:
8679         case RID_AT_THROW:
8680           statement = cp_parser_objc_statement (parser);
8681           break;
8682
8683         case RID_TRY:
8684           statement = cp_parser_try_block (parser);
8685           break;
8686
8687         case RID_NAMESPACE:
8688           /* This must be a namespace alias definition.  */
8689           cp_parser_declaration_statement (parser);
8690           return;
8691           
8692         case RID_TRANSACTION_ATOMIC:
8693         case RID_TRANSACTION_RELAXED:
8694           statement = cp_parser_transaction (parser, keyword);
8695           break;
8696         case RID_TRANSACTION_CANCEL:
8697           statement = cp_parser_transaction_cancel (parser);
8698           break;
8699
8700         default:
8701           /* It might be a keyword like `int' that can start a
8702              declaration-statement.  */
8703           break;
8704         }
8705     }
8706   else if (token->type == CPP_NAME)
8707     {
8708       /* If the next token is a `:', then we are looking at a
8709          labeled-statement.  */
8710       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8711       if (token->type == CPP_COLON)
8712         {
8713           /* Looks like a labeled-statement with an ordinary label.
8714              Parse the label, and then use tail recursion to parse
8715              the statement.  */
8716           cp_parser_label_for_labeled_statement (parser);
8717           goto restart;
8718         }
8719     }
8720   /* Anything that starts with a `{' must be a compound-statement.  */
8721   else if (token->type == CPP_OPEN_BRACE)
8722     statement = cp_parser_compound_statement (parser, NULL, false, false);
8723   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8724      a statement all its own.  */
8725   else if (token->type == CPP_PRAGMA)
8726     {
8727       /* Only certain OpenMP pragmas are attached to statements, and thus
8728          are considered statements themselves.  All others are not.  In
8729          the context of a compound, accept the pragma as a "statement" and
8730          return so that we can check for a close brace.  Otherwise we
8731          require a real statement and must go back and read one.  */
8732       if (in_compound)
8733         cp_parser_pragma (parser, pragma_compound);
8734       else if (!cp_parser_pragma (parser, pragma_stmt))
8735         goto restart;
8736       return;
8737     }
8738   else if (token->type == CPP_EOF)
8739     {
8740       cp_parser_error (parser, "expected statement");
8741       return;
8742     }
8743
8744   /* Everything else must be a declaration-statement or an
8745      expression-statement.  Try for the declaration-statement
8746      first, unless we are looking at a `;', in which case we know that
8747      we have an expression-statement.  */
8748   if (!statement)
8749     {
8750       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8751         {
8752           cp_parser_parse_tentatively (parser);
8753           /* Try to parse the declaration-statement.  */
8754           cp_parser_declaration_statement (parser);
8755           /* If that worked, we're done.  */
8756           if (cp_parser_parse_definitely (parser))
8757             return;
8758         }
8759       /* Look for an expression-statement instead.  */
8760       statement = cp_parser_expression_statement (parser, in_statement_expr);
8761     }
8762
8763   /* Set the line number for the statement.  */
8764   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8765     SET_EXPR_LOCATION (statement, statement_location);
8766 }
8767
8768 /* Parse the label for a labeled-statement, i.e.
8769
8770    identifier :
8771    case constant-expression :
8772    default :
8773
8774    GNU Extension:
8775    case constant-expression ... constant-expression : statement
8776
8777    When a label is parsed without errors, the label is added to the
8778    parse tree by the finish_* functions, so this function doesn't
8779    have to return the label.  */
8780
8781 static void
8782 cp_parser_label_for_labeled_statement (cp_parser* parser)
8783 {
8784   cp_token *token;
8785   tree label = NULL_TREE;
8786   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8787
8788   /* The next token should be an identifier.  */
8789   token = cp_lexer_peek_token (parser->lexer);
8790   if (token->type != CPP_NAME
8791       && token->type != CPP_KEYWORD)
8792     {
8793       cp_parser_error (parser, "expected labeled-statement");
8794       return;
8795     }
8796
8797   parser->colon_corrects_to_scope_p = false;
8798   switch (token->keyword)
8799     {
8800     case RID_CASE:
8801       {
8802         tree expr, expr_hi;
8803         cp_token *ellipsis;
8804
8805         /* Consume the `case' token.  */
8806         cp_lexer_consume_token (parser->lexer);
8807         /* Parse the constant-expression.  */
8808         expr = cp_parser_constant_expression (parser,
8809                                               /*allow_non_constant_p=*/false,
8810                                               NULL);
8811
8812         ellipsis = cp_lexer_peek_token (parser->lexer);
8813         if (ellipsis->type == CPP_ELLIPSIS)
8814           {
8815             /* Consume the `...' token.  */
8816             cp_lexer_consume_token (parser->lexer);
8817             expr_hi =
8818               cp_parser_constant_expression (parser,
8819                                              /*allow_non_constant_p=*/false,
8820                                              NULL);
8821             /* We don't need to emit warnings here, as the common code
8822                will do this for us.  */
8823           }
8824         else
8825           expr_hi = NULL_TREE;
8826
8827         if (parser->in_switch_statement_p)
8828           finish_case_label (token->location, expr, expr_hi);
8829         else
8830           error_at (token->location,
8831                     "case label %qE not within a switch statement",
8832                     expr);
8833       }
8834       break;
8835
8836     case RID_DEFAULT:
8837       /* Consume the `default' token.  */
8838       cp_lexer_consume_token (parser->lexer);
8839
8840       if (parser->in_switch_statement_p)
8841         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8842       else
8843         error_at (token->location, "case label not within a switch statement");
8844       break;
8845
8846     default:
8847       /* Anything else must be an ordinary label.  */
8848       label = finish_label_stmt (cp_parser_identifier (parser));
8849       break;
8850     }
8851
8852   /* Require the `:' token.  */
8853   cp_parser_require (parser, CPP_COLON, RT_COLON);
8854
8855   /* An ordinary label may optionally be followed by attributes.
8856      However, this is only permitted if the attributes are then
8857      followed by a semicolon.  This is because, for backward
8858      compatibility, when parsing
8859        lab: __attribute__ ((unused)) int i;
8860      we want the attribute to attach to "i", not "lab".  */
8861   if (label != NULL_TREE
8862       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8863     {
8864       tree attrs;
8865
8866       cp_parser_parse_tentatively (parser);
8867       attrs = cp_parser_attributes_opt (parser);
8868       if (attrs == NULL_TREE
8869           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8870         cp_parser_abort_tentative_parse (parser);
8871       else if (!cp_parser_parse_definitely (parser))
8872         ;
8873       else
8874         cplus_decl_attributes (&label, attrs, 0);
8875     }
8876
8877   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8878 }
8879
8880 /* Parse an expression-statement.
8881
8882    expression-statement:
8883      expression [opt] ;
8884
8885    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8886    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8887    indicates whether this expression-statement is part of an
8888    expression statement.  */
8889
8890 static tree
8891 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8892 {
8893   tree statement = NULL_TREE;
8894   cp_token *token = cp_lexer_peek_token (parser->lexer);
8895
8896   /* If the next token is a ';', then there is no expression
8897      statement.  */
8898   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8899     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8900
8901   /* Give a helpful message for "A<T>::type t;" and the like.  */
8902   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8903       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8904     {
8905       if (TREE_CODE (statement) == SCOPE_REF)
8906         error_at (token->location, "need %<typename%> before %qE because "
8907                   "%qT is a dependent scope",
8908                   statement, TREE_OPERAND (statement, 0));
8909       else if (is_overloaded_fn (statement)
8910                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8911         {
8912           /* A::A a; */
8913           tree fn = get_first_fn (statement);
8914           error_at (token->location,
8915                     "%<%T::%D%> names the constructor, not the type",
8916                     DECL_CONTEXT (fn), DECL_NAME (fn));
8917         }
8918     }
8919
8920   /* Consume the final `;'.  */
8921   cp_parser_consume_semicolon_at_end_of_statement (parser);
8922
8923   if (in_statement_expr
8924       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8925     /* This is the final expression statement of a statement
8926        expression.  */
8927     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8928   else if (statement)
8929     statement = finish_expr_stmt (statement);
8930   else
8931     finish_stmt ();
8932
8933   return statement;
8934 }
8935
8936 /* Parse a compound-statement.
8937
8938    compound-statement:
8939      { statement-seq [opt] }
8940
8941    GNU extension:
8942
8943    compound-statement:
8944      { label-declaration-seq [opt] statement-seq [opt] }
8945
8946    label-declaration-seq:
8947      label-declaration
8948      label-declaration-seq label-declaration
8949
8950    Returns a tree representing the statement.  */
8951
8952 static tree
8953 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8954                               bool in_try, bool function_body)
8955 {
8956   tree compound_stmt;
8957
8958   /* Consume the `{'.  */
8959   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8960     return error_mark_node;
8961   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8962       && !function_body)
8963     pedwarn (input_location, OPT_Wpedantic,
8964              "compound-statement in constexpr function");
8965   /* Begin the compound-statement.  */
8966   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8967   /* If the next keyword is `__label__' we have a label declaration.  */
8968   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8969     cp_parser_label_declaration (parser);
8970   /* Parse an (optional) statement-seq.  */
8971   cp_parser_statement_seq_opt (parser, in_statement_expr);
8972   /* Finish the compound-statement.  */
8973   finish_compound_stmt (compound_stmt);
8974   /* Consume the `}'.  */
8975   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8976
8977   return compound_stmt;
8978 }
8979
8980 /* Parse an (optional) statement-seq.
8981
8982    statement-seq:
8983      statement
8984      statement-seq [opt] statement  */
8985
8986 static void
8987 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8988 {
8989   /* Scan statements until there aren't any more.  */
8990   while (true)
8991     {
8992       cp_token *token = cp_lexer_peek_token (parser->lexer);
8993
8994       /* If we are looking at a `}', then we have run out of
8995          statements; the same is true if we have reached the end
8996          of file, or have stumbled upon a stray '@end'.  */
8997       if (token->type == CPP_CLOSE_BRACE
8998           || token->type == CPP_EOF
8999           || token->type == CPP_PRAGMA_EOL
9000           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9001         break;
9002       
9003       /* If we are in a compound statement and find 'else' then
9004          something went wrong.  */
9005       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9006         {
9007           if (parser->in_statement & IN_IF_STMT) 
9008             break;
9009           else
9010             {
9011               token = cp_lexer_consume_token (parser->lexer);
9012               error_at (token->location, "%<else%> without a previous %<if%>");
9013             }
9014         }
9015
9016       /* Parse the statement.  */
9017       cp_parser_statement (parser, in_statement_expr, true, NULL);
9018     }
9019 }
9020
9021 /* Parse a selection-statement.
9022
9023    selection-statement:
9024      if ( condition ) statement
9025      if ( condition ) statement else statement
9026      switch ( condition ) statement
9027
9028    Returns the new IF_STMT or SWITCH_STMT.
9029
9030    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9031    is a (possibly labeled) if statement which is not enclosed in
9032    braces and has an else clause.  This is used to implement
9033    -Wparentheses.  */
9034
9035 static tree
9036 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9037 {
9038   cp_token *token;
9039   enum rid keyword;
9040
9041   if (if_p != NULL)
9042     *if_p = false;
9043
9044   /* Peek at the next token.  */
9045   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9046
9047   /* See what kind of keyword it is.  */
9048   keyword = token->keyword;
9049   switch (keyword)
9050     {
9051     case RID_IF:
9052     case RID_SWITCH:
9053       {
9054         tree statement;
9055         tree condition;
9056
9057         /* Look for the `('.  */
9058         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9059           {
9060             cp_parser_skip_to_end_of_statement (parser);
9061             return error_mark_node;
9062           }
9063
9064         /* Begin the selection-statement.  */
9065         if (keyword == RID_IF)
9066           statement = begin_if_stmt ();
9067         else
9068           statement = begin_switch_stmt ();
9069
9070         /* Parse the condition.  */
9071         condition = cp_parser_condition (parser);
9072         /* Look for the `)'.  */
9073         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9074           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9075                                                  /*consume_paren=*/true);
9076
9077         if (keyword == RID_IF)
9078           {
9079             bool nested_if;
9080             unsigned char in_statement;
9081
9082             /* Add the condition.  */
9083             finish_if_stmt_cond (condition, statement);
9084
9085             /* Parse the then-clause.  */
9086             in_statement = parser->in_statement;
9087             parser->in_statement |= IN_IF_STMT;
9088             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9089               {
9090                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9091                 add_stmt (build_empty_stmt (loc));
9092                 cp_lexer_consume_token (parser->lexer);
9093                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9094                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9095                               "empty body in an %<if%> statement");
9096                 nested_if = false;
9097               }
9098             else
9099               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9100             parser->in_statement = in_statement;
9101
9102             finish_then_clause (statement);
9103
9104             /* If the next token is `else', parse the else-clause.  */
9105             if (cp_lexer_next_token_is_keyword (parser->lexer,
9106                                                 RID_ELSE))
9107               {
9108                 /* Consume the `else' keyword.  */
9109                 cp_lexer_consume_token (parser->lexer);
9110                 begin_else_clause (statement);
9111                 /* Parse the else-clause.  */
9112                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9113                   {
9114                     location_t loc;
9115                     loc = cp_lexer_peek_token (parser->lexer)->location;
9116                     warning_at (loc,
9117                                 OPT_Wempty_body, "suggest braces around "
9118                                 "empty body in an %<else%> statement");
9119                     add_stmt (build_empty_stmt (loc));
9120                     cp_lexer_consume_token (parser->lexer);
9121                   }
9122                 else
9123                   cp_parser_implicitly_scoped_statement (parser, NULL);
9124
9125                 finish_else_clause (statement);
9126
9127                 /* If we are currently parsing a then-clause, then
9128                    IF_P will not be NULL.  We set it to true to
9129                    indicate that this if statement has an else clause.
9130                    This may trigger the Wparentheses warning below
9131                    when we get back up to the parent if statement.  */
9132                 if (if_p != NULL)
9133                   *if_p = true;
9134               }
9135             else
9136               {
9137                 /* This if statement does not have an else clause.  If
9138                    NESTED_IF is true, then the then-clause is an if
9139                    statement which does have an else clause.  We warn
9140                    about the potential ambiguity.  */
9141                 if (nested_if)
9142                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9143                               "suggest explicit braces to avoid ambiguous"
9144                               " %<else%>");
9145               }
9146
9147             /* Now we're all done with the if-statement.  */
9148             finish_if_stmt (statement);
9149           }
9150         else
9151           {
9152             bool in_switch_statement_p;
9153             unsigned char in_statement;
9154
9155             /* Add the condition.  */
9156             finish_switch_cond (condition, statement);
9157
9158             /* Parse the body of the switch-statement.  */
9159             in_switch_statement_p = parser->in_switch_statement_p;
9160             in_statement = parser->in_statement;
9161             parser->in_switch_statement_p = true;
9162             parser->in_statement |= IN_SWITCH_STMT;
9163             cp_parser_implicitly_scoped_statement (parser, NULL);
9164             parser->in_switch_statement_p = in_switch_statement_p;
9165             parser->in_statement = in_statement;
9166
9167             /* Now we're all done with the switch-statement.  */
9168             finish_switch_stmt (statement);
9169           }
9170
9171         return statement;
9172       }
9173       break;
9174
9175     default:
9176       cp_parser_error (parser, "expected selection-statement");
9177       return error_mark_node;
9178     }
9179 }
9180
9181 /* Parse a condition.
9182
9183    condition:
9184      expression
9185      type-specifier-seq declarator = initializer-clause
9186      type-specifier-seq declarator braced-init-list
9187
9188    GNU Extension:
9189
9190    condition:
9191      type-specifier-seq declarator asm-specification [opt]
9192        attributes [opt] = assignment-expression
9193
9194    Returns the expression that should be tested.  */
9195
9196 static tree
9197 cp_parser_condition (cp_parser* parser)
9198 {
9199   cp_decl_specifier_seq type_specifiers;
9200   const char *saved_message;
9201   int declares_class_or_enum;
9202
9203   /* Try the declaration first.  */
9204   cp_parser_parse_tentatively (parser);
9205   /* New types are not allowed in the type-specifier-seq for a
9206      condition.  */
9207   saved_message = parser->type_definition_forbidden_message;
9208   parser->type_definition_forbidden_message
9209     = G_("types may not be defined in conditions");
9210   /* Parse the type-specifier-seq.  */
9211   cp_parser_decl_specifier_seq (parser,
9212                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9213                                 &type_specifiers,
9214                                 &declares_class_or_enum);
9215   /* Restore the saved message.  */
9216   parser->type_definition_forbidden_message = saved_message;
9217   /* If all is well, we might be looking at a declaration.  */
9218   if (!cp_parser_error_occurred (parser))
9219     {
9220       tree decl;
9221       tree asm_specification;
9222       tree attributes;
9223       cp_declarator *declarator;
9224       tree initializer = NULL_TREE;
9225
9226       /* Parse the declarator.  */
9227       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9228                                          /*ctor_dtor_or_conv_p=*/NULL,
9229                                          /*parenthesized_p=*/NULL,
9230                                          /*member_p=*/false);
9231       /* Parse the attributes.  */
9232       attributes = cp_parser_attributes_opt (parser);
9233       /* Parse the asm-specification.  */
9234       asm_specification = cp_parser_asm_specification_opt (parser);
9235       /* If the next token is not an `=' or '{', then we might still be
9236          looking at an expression.  For example:
9237
9238            if (A(a).x)
9239
9240          looks like a decl-specifier-seq and a declarator -- but then
9241          there is no `=', so this is an expression.  */
9242       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9243           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9244         cp_parser_simulate_error (parser);
9245         
9246       /* If we did see an `=' or '{', then we are looking at a declaration
9247          for sure.  */
9248       if (cp_parser_parse_definitely (parser))
9249         {
9250           tree pushed_scope;
9251           bool non_constant_p;
9252           bool flags = LOOKUP_ONLYCONVERTING;
9253
9254           /* Create the declaration.  */
9255           decl = start_decl (declarator, &type_specifiers,
9256                              /*initialized_p=*/true,
9257                              attributes, /*prefix_attributes=*/NULL_TREE,
9258                              &pushed_scope);
9259
9260           /* Parse the initializer.  */
9261           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9262             {
9263               initializer = cp_parser_braced_list (parser, &non_constant_p);
9264               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9265               flags = 0;
9266             }
9267           else
9268             {
9269               /* Consume the `='.  */
9270               cp_parser_require (parser, CPP_EQ, RT_EQ);
9271               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9272             }
9273           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9274             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9275
9276           /* Process the initializer.  */
9277           cp_finish_decl (decl,
9278                           initializer, !non_constant_p,
9279                           asm_specification,
9280                           flags);
9281
9282           if (pushed_scope)
9283             pop_scope (pushed_scope);
9284
9285           return convert_from_reference (decl);
9286         }
9287     }
9288   /* If we didn't even get past the declarator successfully, we are
9289      definitely not looking at a declaration.  */
9290   else
9291     cp_parser_abort_tentative_parse (parser);
9292
9293   /* Otherwise, we are looking at an expression.  */
9294   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9295 }
9296
9297 /* Parses a for-statement or range-for-statement until the closing ')',
9298    not included. */
9299
9300 static tree
9301 cp_parser_for (cp_parser *parser)
9302 {
9303   tree init, scope, decl;
9304   bool is_range_for;
9305
9306   /* Begin the for-statement.  */
9307   scope = begin_for_scope (&init);
9308
9309   /* Parse the initialization.  */
9310   is_range_for = cp_parser_for_init_statement (parser, &decl);
9311
9312   if (is_range_for)
9313     return cp_parser_range_for (parser, scope, init, decl);
9314   else
9315     return cp_parser_c_for (parser, scope, init);
9316 }
9317
9318 static tree
9319 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9320 {
9321   /* Normal for loop */
9322   tree condition = NULL_TREE;
9323   tree expression = NULL_TREE;
9324   tree stmt;
9325
9326   stmt = begin_for_stmt (scope, init);
9327   /* The for-init-statement has already been parsed in
9328      cp_parser_for_init_statement, so no work is needed here.  */
9329   finish_for_init_stmt (stmt);
9330
9331   /* If there's a condition, process it.  */
9332   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9333     condition = cp_parser_condition (parser);
9334   finish_for_cond (condition, stmt);
9335   /* Look for the `;'.  */
9336   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9337
9338   /* If there's an expression, process it.  */
9339   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9340     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9341   finish_for_expr (expression, stmt);
9342
9343   return stmt;
9344 }
9345
9346 /* Tries to parse a range-based for-statement:
9347
9348   range-based-for:
9349     decl-specifier-seq declarator : expression
9350
9351   The decl-specifier-seq declarator and the `:' are already parsed by
9352   cp_parser_for_init_statement. If processing_template_decl it returns a
9353   newly created RANGE_FOR_STMT; if not, it is converted to a
9354   regular FOR_STMT.  */
9355
9356 static tree
9357 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9358 {
9359   tree stmt, range_expr;
9360
9361   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9362     {
9363       bool expr_non_constant_p;
9364       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9365     }
9366   else
9367     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9368
9369   /* If in template, STMT is converted to a normal for-statement
9370      at instantiation. If not, it is done just ahead. */
9371   if (processing_template_decl)
9372     {
9373       if (check_for_bare_parameter_packs (range_expr))
9374         range_expr = error_mark_node;
9375       stmt = begin_range_for_stmt (scope, init);
9376       finish_range_for_decl (stmt, range_decl, range_expr);
9377       if (!type_dependent_expression_p (range_expr)
9378           /* do_auto_deduction doesn't mess with template init-lists.  */
9379           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9380         do_range_for_auto_deduction (range_decl, range_expr);
9381     }
9382   else
9383     {
9384       stmt = begin_for_stmt (scope, init);
9385       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9386     }
9387   return stmt;
9388 }
9389
9390 /* Subroutine of cp_convert_range_for: given the initializer expression,
9391    builds up the range temporary.  */
9392
9393 static tree
9394 build_range_temp (tree range_expr)
9395 {
9396   tree range_type, range_temp;
9397
9398   /* Find out the type deduced by the declaration
9399      `auto &&__range = range_expr'.  */
9400   range_type = cp_build_reference_type (make_auto (), true);
9401   range_type = do_auto_deduction (range_type, range_expr,
9402                                   type_uses_auto (range_type));
9403
9404   /* Create the __range variable.  */
9405   range_temp = build_decl (input_location, VAR_DECL,
9406                            get_identifier ("__for_range"), range_type);
9407   TREE_USED (range_temp) = 1;
9408   DECL_ARTIFICIAL (range_temp) = 1;
9409
9410   return range_temp;
9411 }
9412
9413 /* Used by cp_parser_range_for in template context: we aren't going to
9414    do a full conversion yet, but we still need to resolve auto in the
9415    type of the for-range-declaration if present.  This is basically
9416    a shortcut version of cp_convert_range_for.  */
9417
9418 static void
9419 do_range_for_auto_deduction (tree decl, tree range_expr)
9420 {
9421   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9422   if (auto_node)
9423     {
9424       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9425       range_temp = convert_from_reference (build_range_temp (range_expr));
9426       iter_type = (cp_parser_perform_range_for_lookup
9427                    (range_temp, &begin_dummy, &end_dummy));
9428       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9429       iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9430                                         tf_warning_or_error);
9431       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9432                                             iter_decl, auto_node);
9433     }
9434 }
9435
9436 /* Converts a range-based for-statement into a normal
9437    for-statement, as per the definition.
9438
9439       for (RANGE_DECL : RANGE_EXPR)
9440         BLOCK
9441
9442    should be equivalent to:
9443
9444       {
9445         auto &&__range = RANGE_EXPR;
9446         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9447               __begin != __end;
9448               ++__begin)
9449           {
9450               RANGE_DECL = *__begin;
9451               BLOCK
9452           }
9453       }
9454
9455    If RANGE_EXPR is an array:
9456         BEGIN_EXPR = __range
9457         END_EXPR = __range + ARRAY_SIZE(__range)
9458    Else if RANGE_EXPR has a member 'begin' or 'end':
9459         BEGIN_EXPR = __range.begin()
9460         END_EXPR = __range.end()
9461    Else:
9462         BEGIN_EXPR = begin(__range)
9463         END_EXPR = end(__range);
9464
9465    If __range has a member 'begin' but not 'end', or vice versa, we must
9466    still use the second alternative (it will surely fail, however).
9467    When calling begin()/end() in the third alternative we must use
9468    argument dependent lookup, but always considering 'std' as an associated
9469    namespace.  */
9470
9471 tree
9472 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9473 {
9474   tree begin, end;
9475   tree iter_type, begin_expr, end_expr;
9476   tree condition, expression;
9477
9478   if (range_decl == error_mark_node || range_expr == error_mark_node)
9479     /* If an error happened previously do nothing or else a lot of
9480        unhelpful errors would be issued.  */
9481     begin_expr = end_expr = iter_type = error_mark_node;
9482   else
9483     {
9484       tree range_temp = build_range_temp (range_expr);
9485       pushdecl (range_temp);
9486       cp_finish_decl (range_temp, range_expr,
9487                       /*is_constant_init*/false, NULL_TREE,
9488                       LOOKUP_ONLYCONVERTING);
9489
9490       range_temp = convert_from_reference (range_temp);
9491       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9492                                                       &begin_expr, &end_expr);
9493     }
9494
9495   /* The new for initialization statement.  */
9496   begin = build_decl (input_location, VAR_DECL,
9497                       get_identifier ("__for_begin"), iter_type);
9498   TREE_USED (begin) = 1;
9499   DECL_ARTIFICIAL (begin) = 1;
9500   pushdecl (begin);
9501   cp_finish_decl (begin, begin_expr,
9502                   /*is_constant_init*/false, NULL_TREE,
9503                   LOOKUP_ONLYCONVERTING);
9504
9505   end = build_decl (input_location, VAR_DECL,
9506                     get_identifier ("__for_end"), iter_type);
9507   TREE_USED (end) = 1;
9508   DECL_ARTIFICIAL (end) = 1;
9509   pushdecl (end);
9510   cp_finish_decl (end, end_expr,
9511                   /*is_constant_init*/false, NULL_TREE,
9512                   LOOKUP_ONLYCONVERTING);
9513
9514   finish_for_init_stmt (statement);
9515
9516   /* The new for condition.  */
9517   condition = build_x_binary_op (input_location, NE_EXPR,
9518                                  begin, ERROR_MARK,
9519                                  end, ERROR_MARK,
9520                                  NULL, tf_warning_or_error);
9521   finish_for_cond (condition, statement);
9522
9523   /* The new increment expression.  */
9524   expression = finish_unary_op_expr (input_location,
9525                                      PREINCREMENT_EXPR, begin);
9526   finish_for_expr (expression, statement);
9527
9528   /* The declaration is initialized with *__begin inside the loop body.  */
9529   cp_finish_decl (range_decl,
9530                   build_x_indirect_ref (input_location, begin, RO_NULL,
9531                                         tf_warning_or_error),
9532                   /*is_constant_init*/false, NULL_TREE,
9533                   LOOKUP_ONLYCONVERTING);
9534
9535   return statement;
9536 }
9537
9538 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9539    We need to solve both at the same time because the method used
9540    depends on the existence of members begin or end.
9541    Returns the type deduced for the iterator expression.  */
9542
9543 static tree
9544 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9545 {
9546   if (error_operand_p (range))
9547     {
9548       *begin = *end = error_mark_node;
9549       return error_mark_node;
9550     }
9551
9552   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9553     {
9554       error ("range-based %<for%> expression of type %qT "
9555              "has incomplete type", TREE_TYPE (range));
9556       *begin = *end = error_mark_node;
9557       return error_mark_node;
9558     }
9559   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9560     {
9561       /* If RANGE is an array, we will use pointer arithmetic.  */
9562       *begin = range;
9563       *end = build_binary_op (input_location, PLUS_EXPR,
9564                               range,
9565                               array_type_nelts_top (TREE_TYPE (range)),
9566                               0);
9567       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9568     }
9569   else
9570     {
9571       /* If it is not an array, we must do a bit of magic.  */
9572       tree id_begin, id_end;
9573       tree member_begin, member_end;
9574
9575       *begin = *end = error_mark_node;
9576
9577       id_begin = get_identifier ("begin");
9578       id_end = get_identifier ("end");
9579       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9580                                     /*protect=*/2, /*want_type=*/false,
9581                                     tf_warning_or_error);
9582       member_end = lookup_member (TREE_TYPE (range), id_end,
9583                                   /*protect=*/2, /*want_type=*/false,
9584                                   tf_warning_or_error);
9585
9586       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9587         {
9588           /* Use the member functions.  */
9589           if (member_begin != NULL_TREE)
9590             *begin = cp_parser_range_for_member_function (range, id_begin);
9591           else
9592             error ("range-based %<for%> expression of type %qT has an "
9593                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9594
9595           if (member_end != NULL_TREE)
9596             *end = cp_parser_range_for_member_function (range, id_end);
9597           else
9598             error ("range-based %<for%> expression of type %qT has a "
9599                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9600         }
9601       else
9602         {
9603           /* Use global functions with ADL.  */
9604           VEC(tree,gc) *vec;
9605           vec = make_tree_vector ();
9606
9607           VEC_safe_push (tree, gc, vec, range);
9608
9609           member_begin = perform_koenig_lookup (id_begin, vec,
9610                                                 /*include_std=*/true,
9611                                                 tf_warning_or_error);
9612           *begin = finish_call_expr (member_begin, &vec, false, true,
9613                                      tf_warning_or_error);
9614           member_end = perform_koenig_lookup (id_end, vec,
9615                                               /*include_std=*/true,
9616                                               tf_warning_or_error);
9617           *end = finish_call_expr (member_end, &vec, false, true,
9618                                    tf_warning_or_error);
9619
9620           release_tree_vector (vec);
9621         }
9622
9623       /* Last common checks.  */
9624       if (*begin == error_mark_node || *end == error_mark_node)
9625         {
9626           /* If one of the expressions is an error do no more checks.  */
9627           *begin = *end = error_mark_node;
9628           return error_mark_node;
9629         }
9630       else
9631         {
9632           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9633           /* The unqualified type of the __begin and __end temporaries should
9634              be the same, as required by the multiple auto declaration.  */
9635           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9636             error ("inconsistent begin/end types in range-based %<for%> "
9637                    "statement: %qT and %qT",
9638                    TREE_TYPE (*begin), TREE_TYPE (*end));
9639           return iter_type;
9640         }
9641     }
9642 }
9643
9644 /* Helper function for cp_parser_perform_range_for_lookup.
9645    Builds a tree for RANGE.IDENTIFIER().  */
9646
9647 static tree
9648 cp_parser_range_for_member_function (tree range, tree identifier)
9649 {
9650   tree member, res;
9651   VEC(tree,gc) *vec;
9652
9653   member = finish_class_member_access_expr (range, identifier,
9654                                             false, tf_warning_or_error);
9655   if (member == error_mark_node)
9656     return error_mark_node;
9657
9658   vec = make_tree_vector ();
9659   res = finish_call_expr (member, &vec,
9660                           /*disallow_virtual=*/false,
9661                           /*koenig_p=*/false,
9662                           tf_warning_or_error);
9663   release_tree_vector (vec);
9664   return res;
9665 }
9666
9667 /* Parse an iteration-statement.
9668
9669    iteration-statement:
9670      while ( condition ) statement
9671      do statement while ( expression ) ;
9672      for ( for-init-statement condition [opt] ; expression [opt] )
9673        statement
9674
9675    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9676
9677 static tree
9678 cp_parser_iteration_statement (cp_parser* parser)
9679 {
9680   cp_token *token;
9681   enum rid keyword;
9682   tree statement;
9683   unsigned char in_statement;
9684
9685   /* Peek at the next token.  */
9686   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9687   if (!token)
9688     return error_mark_node;
9689
9690   /* Remember whether or not we are already within an iteration
9691      statement.  */
9692   in_statement = parser->in_statement;
9693
9694   /* See what kind of keyword it is.  */
9695   keyword = token->keyword;
9696   switch (keyword)
9697     {
9698     case RID_WHILE:
9699       {
9700         tree condition;
9701
9702         /* Begin the while-statement.  */
9703         statement = begin_while_stmt ();
9704         /* Look for the `('.  */
9705         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9706         /* Parse the condition.  */
9707         condition = cp_parser_condition (parser);
9708         finish_while_stmt_cond (condition, statement);
9709         /* Look for the `)'.  */
9710         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9711         /* Parse the dependent statement.  */
9712         parser->in_statement = IN_ITERATION_STMT;
9713         cp_parser_already_scoped_statement (parser);
9714         parser->in_statement = in_statement;
9715         /* We're done with the while-statement.  */
9716         finish_while_stmt (statement);
9717       }
9718       break;
9719
9720     case RID_DO:
9721       {
9722         tree expression;
9723
9724         /* Begin the do-statement.  */
9725         statement = begin_do_stmt ();
9726         /* Parse the body of the do-statement.  */
9727         parser->in_statement = IN_ITERATION_STMT;
9728         cp_parser_implicitly_scoped_statement (parser, NULL);
9729         parser->in_statement = in_statement;
9730         finish_do_body (statement);
9731         /* Look for the `while' keyword.  */
9732         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9733         /* Look for the `('.  */
9734         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9735         /* Parse the expression.  */
9736         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9737         /* We're done with the do-statement.  */
9738         finish_do_stmt (expression, statement);
9739         /* Look for the `)'.  */
9740         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9741         /* Look for the `;'.  */
9742         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9743       }
9744       break;
9745
9746     case RID_FOR:
9747       {
9748         /* Look for the `('.  */
9749         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9750
9751         statement = cp_parser_for (parser);
9752
9753         /* Look for the `)'.  */
9754         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9755
9756         /* Parse the body of the for-statement.  */
9757         parser->in_statement = IN_ITERATION_STMT;
9758         cp_parser_already_scoped_statement (parser);
9759         parser->in_statement = in_statement;
9760
9761         /* We're done with the for-statement.  */
9762         finish_for_stmt (statement);
9763       }
9764       break;
9765
9766     default:
9767       cp_parser_error (parser, "expected iteration-statement");
9768       statement = error_mark_node;
9769       break;
9770     }
9771
9772   return statement;
9773 }
9774
9775 /* Parse a for-init-statement or the declarator of a range-based-for.
9776    Returns true if a range-based-for declaration is seen.
9777
9778    for-init-statement:
9779      expression-statement
9780      simple-declaration  */
9781
9782 static bool
9783 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9784 {
9785   /* If the next token is a `;', then we have an empty
9786      expression-statement.  Grammatically, this is also a
9787      simple-declaration, but an invalid one, because it does not
9788      declare anything.  Therefore, if we did not handle this case
9789      specially, we would issue an error message about an invalid
9790      declaration.  */
9791   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9792     {
9793       bool is_range_for = false;
9794       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9795
9796       parser->colon_corrects_to_scope_p = false;
9797
9798       /* We're going to speculatively look for a declaration, falling back
9799          to an expression, if necessary.  */
9800       cp_parser_parse_tentatively (parser);
9801       /* Parse the declaration.  */
9802       cp_parser_simple_declaration (parser,
9803                                     /*function_definition_allowed_p=*/false,
9804                                     decl);
9805       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9806       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9807         {
9808           /* It is a range-for, consume the ':' */
9809           cp_lexer_consume_token (parser->lexer);
9810           is_range_for = true;
9811           if (cxx_dialect < cxx0x)
9812             {
9813               error_at (cp_lexer_peek_token (parser->lexer)->location,
9814                         "range-based %<for%> loops are not allowed "
9815                         "in C++98 mode");
9816               *decl = error_mark_node;
9817             }
9818         }
9819       else
9820           /* The ';' is not consumed yet because we told
9821              cp_parser_simple_declaration not to.  */
9822           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9823
9824       if (cp_parser_parse_definitely (parser))
9825         return is_range_for;
9826       /* If the tentative parse failed, then we shall need to look for an
9827          expression-statement.  */
9828     }
9829   /* If we are here, it is an expression-statement.  */
9830   cp_parser_expression_statement (parser, NULL_TREE);
9831   return false;
9832 }
9833
9834 /* Parse a jump-statement.
9835
9836    jump-statement:
9837      break ;
9838      continue ;
9839      return expression [opt] ;
9840      return braced-init-list ;
9841      goto identifier ;
9842
9843    GNU extension:
9844
9845    jump-statement:
9846      goto * expression ;
9847
9848    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9849
9850 static tree
9851 cp_parser_jump_statement (cp_parser* parser)
9852 {
9853   tree statement = error_mark_node;
9854   cp_token *token;
9855   enum rid keyword;
9856   unsigned char in_statement;
9857
9858   /* Peek at the next token.  */
9859   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9860   if (!token)
9861     return error_mark_node;
9862
9863   /* See what kind of keyword it is.  */
9864   keyword = token->keyword;
9865   switch (keyword)
9866     {
9867     case RID_BREAK:
9868       in_statement = parser->in_statement & ~IN_IF_STMT;      
9869       switch (in_statement)
9870         {
9871         case 0:
9872           error_at (token->location, "break statement not within loop or switch");
9873           break;
9874         default:
9875           gcc_assert ((in_statement & IN_SWITCH_STMT)
9876                       || in_statement == IN_ITERATION_STMT);
9877           statement = finish_break_stmt ();
9878           break;
9879         case IN_OMP_BLOCK:
9880           error_at (token->location, "invalid exit from OpenMP structured block");
9881           break;
9882         case IN_OMP_FOR:
9883           error_at (token->location, "break statement used with OpenMP for loop");
9884           break;
9885         }
9886       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9887       break;
9888
9889     case RID_CONTINUE:
9890       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9891         {
9892         case 0:
9893           error_at (token->location, "continue statement not within a loop");
9894           break;
9895         case IN_ITERATION_STMT:
9896         case IN_OMP_FOR:
9897           statement = finish_continue_stmt ();
9898           break;
9899         case IN_OMP_BLOCK:
9900           error_at (token->location, "invalid exit from OpenMP structured block");
9901           break;
9902         default:
9903           gcc_unreachable ();
9904         }
9905       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9906       break;
9907
9908     case RID_RETURN:
9909       {
9910         tree expr;
9911         bool expr_non_constant_p;
9912
9913         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9914           {
9915             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9916             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9917           }
9918         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9919           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9920         else
9921           /* If the next token is a `;', then there is no
9922              expression.  */
9923           expr = NULL_TREE;
9924         /* Build the return-statement.  */
9925         statement = finish_return_stmt (expr);
9926         /* Look for the final `;'.  */
9927         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9928       }
9929       break;
9930
9931     case RID_GOTO:
9932       /* Create the goto-statement.  */
9933       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9934         {
9935           /* Issue a warning about this use of a GNU extension.  */
9936           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9937           /* Consume the '*' token.  */
9938           cp_lexer_consume_token (parser->lexer);
9939           /* Parse the dependent expression.  */
9940           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9941         }
9942       else
9943         finish_goto_stmt (cp_parser_identifier (parser));
9944       /* Look for the final `;'.  */
9945       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9946       break;
9947
9948     default:
9949       cp_parser_error (parser, "expected jump-statement");
9950       break;
9951     }
9952
9953   return statement;
9954 }
9955
9956 /* Parse a declaration-statement.
9957
9958    declaration-statement:
9959      block-declaration  */
9960
9961 static void
9962 cp_parser_declaration_statement (cp_parser* parser)
9963 {
9964   void *p;
9965
9966   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9967   p = obstack_alloc (&declarator_obstack, 0);
9968
9969  /* Parse the block-declaration.  */
9970   cp_parser_block_declaration (parser, /*statement_p=*/true);
9971
9972   /* Free any declarators allocated.  */
9973   obstack_free (&declarator_obstack, p);
9974
9975   /* Finish off the statement.  */
9976   finish_stmt ();
9977 }
9978
9979 /* Some dependent statements (like `if (cond) statement'), are
9980    implicitly in their own scope.  In other words, if the statement is
9981    a single statement (as opposed to a compound-statement), it is
9982    none-the-less treated as if it were enclosed in braces.  Any
9983    declarations appearing in the dependent statement are out of scope
9984    after control passes that point.  This function parses a statement,
9985    but ensures that is in its own scope, even if it is not a
9986    compound-statement.
9987
9988    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9989    is a (possibly labeled) if statement which is not enclosed in
9990    braces and has an else clause.  This is used to implement
9991    -Wparentheses.
9992
9993    Returns the new statement.  */
9994
9995 static tree
9996 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9997 {
9998   tree statement;
9999
10000   if (if_p != NULL)
10001     *if_p = false;
10002
10003   /* Mark if () ; with a special NOP_EXPR.  */
10004   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10005     {
10006       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10007       cp_lexer_consume_token (parser->lexer);
10008       statement = add_stmt (build_empty_stmt (loc));
10009     }
10010   /* if a compound is opened, we simply parse the statement directly.  */
10011   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10012     statement = cp_parser_compound_statement (parser, NULL, false, false);
10013   /* If the token is not a `{', then we must take special action.  */
10014   else
10015     {
10016       /* Create a compound-statement.  */
10017       statement = begin_compound_stmt (0);
10018       /* Parse the dependent-statement.  */
10019       cp_parser_statement (parser, NULL_TREE, false, if_p);
10020       /* Finish the dummy compound-statement.  */
10021       finish_compound_stmt (statement);
10022     }
10023
10024   /* Return the statement.  */
10025   return statement;
10026 }
10027
10028 /* For some dependent statements (like `while (cond) statement'), we
10029    have already created a scope.  Therefore, even if the dependent
10030    statement is a compound-statement, we do not want to create another
10031    scope.  */
10032
10033 static void
10034 cp_parser_already_scoped_statement (cp_parser* parser)
10035 {
10036   /* If the token is a `{', then we must take special action.  */
10037   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10038     cp_parser_statement (parser, NULL_TREE, false, NULL);
10039   else
10040     {
10041       /* Avoid calling cp_parser_compound_statement, so that we
10042          don't create a new scope.  Do everything else by hand.  */
10043       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10044       /* If the next keyword is `__label__' we have a label declaration.  */
10045       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10046         cp_parser_label_declaration (parser);
10047       /* Parse an (optional) statement-seq.  */
10048       cp_parser_statement_seq_opt (parser, NULL_TREE);
10049       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10050     }
10051 }
10052
10053 /* Declarations [gram.dcl.dcl] */
10054
10055 /* Parse an optional declaration-sequence.
10056
10057    declaration-seq:
10058      declaration
10059      declaration-seq declaration  */
10060
10061 static void
10062 cp_parser_declaration_seq_opt (cp_parser* parser)
10063 {
10064   while (true)
10065     {
10066       cp_token *token;
10067
10068       token = cp_lexer_peek_token (parser->lexer);
10069
10070       if (token->type == CPP_CLOSE_BRACE
10071           || token->type == CPP_EOF
10072           || token->type == CPP_PRAGMA_EOL)
10073         break;
10074
10075       if (token->type == CPP_SEMICOLON)
10076         {
10077           /* A declaration consisting of a single semicolon is
10078              invalid.  Allow it unless we're being pedantic.  */
10079           cp_lexer_consume_token (parser->lexer);
10080           if (!in_system_header)
10081             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10082           continue;
10083         }
10084
10085       /* If we're entering or exiting a region that's implicitly
10086          extern "C", modify the lang context appropriately.  */
10087       if (!parser->implicit_extern_c && token->implicit_extern_c)
10088         {
10089           push_lang_context (lang_name_c);
10090           parser->implicit_extern_c = true;
10091         }
10092       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10093         {
10094           pop_lang_context ();
10095           parser->implicit_extern_c = false;
10096         }
10097
10098       if (token->type == CPP_PRAGMA)
10099         {
10100           /* A top-level declaration can consist solely of a #pragma.
10101              A nested declaration cannot, so this is done here and not
10102              in cp_parser_declaration.  (A #pragma at block scope is
10103              handled in cp_parser_statement.)  */
10104           cp_parser_pragma (parser, pragma_external);
10105           continue;
10106         }
10107
10108       /* Parse the declaration itself.  */
10109       cp_parser_declaration (parser);
10110     }
10111 }
10112
10113 /* Parse a declaration.
10114
10115    declaration:
10116      block-declaration
10117      function-definition
10118      template-declaration
10119      explicit-instantiation
10120      explicit-specialization
10121      linkage-specification
10122      namespace-definition
10123
10124    GNU extension:
10125
10126    declaration:
10127       __extension__ declaration */
10128
10129 static void
10130 cp_parser_declaration (cp_parser* parser)
10131 {
10132   cp_token token1;
10133   cp_token token2;
10134   int saved_pedantic;
10135   void *p;
10136   tree attributes = NULL_TREE;
10137
10138   /* Check for the `__extension__' keyword.  */
10139   if (cp_parser_extension_opt (parser, &saved_pedantic))
10140     {
10141       /* Parse the qualified declaration.  */
10142       cp_parser_declaration (parser);
10143       /* Restore the PEDANTIC flag.  */
10144       pedantic = saved_pedantic;
10145
10146       return;
10147     }
10148
10149   /* Try to figure out what kind of declaration is present.  */
10150   token1 = *cp_lexer_peek_token (parser->lexer);
10151
10152   if (token1.type != CPP_EOF)
10153     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10154   else
10155     {
10156       token2.type = CPP_EOF;
10157       token2.keyword = RID_MAX;
10158     }
10159
10160   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10161   p = obstack_alloc (&declarator_obstack, 0);
10162
10163   /* If the next token is `extern' and the following token is a string
10164      literal, then we have a linkage specification.  */
10165   if (token1.keyword == RID_EXTERN
10166       && cp_parser_is_pure_string_literal (&token2))
10167     cp_parser_linkage_specification (parser);
10168   /* If the next token is `template', then we have either a template
10169      declaration, an explicit instantiation, or an explicit
10170      specialization.  */
10171   else if (token1.keyword == RID_TEMPLATE)
10172     {
10173       /* `template <>' indicates a template specialization.  */
10174       if (token2.type == CPP_LESS
10175           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10176         cp_parser_explicit_specialization (parser);
10177       /* `template <' indicates a template declaration.  */
10178       else if (token2.type == CPP_LESS)
10179         cp_parser_template_declaration (parser, /*member_p=*/false);
10180       /* Anything else must be an explicit instantiation.  */
10181       else
10182         cp_parser_explicit_instantiation (parser);
10183     }
10184   /* If the next token is `export', then we have a template
10185      declaration.  */
10186   else if (token1.keyword == RID_EXPORT)
10187     cp_parser_template_declaration (parser, /*member_p=*/false);
10188   /* If the next token is `extern', 'static' or 'inline' and the one
10189      after that is `template', we have a GNU extended explicit
10190      instantiation directive.  */
10191   else if (cp_parser_allow_gnu_extensions_p (parser)
10192            && (token1.keyword == RID_EXTERN
10193                || token1.keyword == RID_STATIC
10194                || token1.keyword == RID_INLINE)
10195            && token2.keyword == RID_TEMPLATE)
10196     cp_parser_explicit_instantiation (parser);
10197   /* If the next token is `namespace', check for a named or unnamed
10198      namespace definition.  */
10199   else if (token1.keyword == RID_NAMESPACE
10200            && (/* A named namespace definition.  */
10201                (token2.type == CPP_NAME
10202                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10203                     != CPP_EQ))
10204                /* An unnamed namespace definition.  */
10205                || token2.type == CPP_OPEN_BRACE
10206                || token2.keyword == RID_ATTRIBUTE))
10207     cp_parser_namespace_definition (parser);
10208   /* An inline (associated) namespace definition.  */
10209   else if (token1.keyword == RID_INLINE
10210            && token2.keyword == RID_NAMESPACE)
10211     cp_parser_namespace_definition (parser);
10212   /* Objective-C++ declaration/definition.  */
10213   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10214     cp_parser_objc_declaration (parser, NULL_TREE);
10215   else if (c_dialect_objc ()
10216            && token1.keyword == RID_ATTRIBUTE
10217            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10218     cp_parser_objc_declaration (parser, attributes);
10219   /* We must have either a block declaration or a function
10220      definition.  */
10221   else
10222     /* Try to parse a block-declaration, or a function-definition.  */
10223     cp_parser_block_declaration (parser, /*statement_p=*/false);
10224
10225   /* Free any declarators allocated.  */
10226   obstack_free (&declarator_obstack, p);
10227 }
10228
10229 /* Parse a block-declaration.
10230
10231    block-declaration:
10232      simple-declaration
10233      asm-definition
10234      namespace-alias-definition
10235      using-declaration
10236      using-directive
10237
10238    GNU Extension:
10239
10240    block-declaration:
10241      __extension__ block-declaration
10242
10243    C++0x Extension:
10244
10245    block-declaration:
10246      static_assert-declaration
10247
10248    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10249    part of a declaration-statement.  */
10250
10251 static void
10252 cp_parser_block_declaration (cp_parser *parser,
10253                              bool      statement_p)
10254 {
10255   cp_token *token1;
10256   int saved_pedantic;
10257
10258   /* Check for the `__extension__' keyword.  */
10259   if (cp_parser_extension_opt (parser, &saved_pedantic))
10260     {
10261       /* Parse the qualified declaration.  */
10262       cp_parser_block_declaration (parser, statement_p);
10263       /* Restore the PEDANTIC flag.  */
10264       pedantic = saved_pedantic;
10265
10266       return;
10267     }
10268
10269   /* Peek at the next token to figure out which kind of declaration is
10270      present.  */
10271   token1 = cp_lexer_peek_token (parser->lexer);
10272
10273   /* If the next keyword is `asm', we have an asm-definition.  */
10274   if (token1->keyword == RID_ASM)
10275     {
10276       if (statement_p)
10277         cp_parser_commit_to_tentative_parse (parser);
10278       cp_parser_asm_definition (parser);
10279     }
10280   /* If the next keyword is `namespace', we have a
10281      namespace-alias-definition.  */
10282   else if (token1->keyword == RID_NAMESPACE)
10283     cp_parser_namespace_alias_definition (parser);
10284   /* If the next keyword is `using', we have a
10285      using-declaration, a using-directive, or an alias-declaration.  */
10286   else if (token1->keyword == RID_USING)
10287     {
10288       cp_token *token2;
10289
10290       if (statement_p)
10291         cp_parser_commit_to_tentative_parse (parser);
10292       /* If the token after `using' is `namespace', then we have a
10293          using-directive.  */
10294       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10295       if (token2->keyword == RID_NAMESPACE)
10296         cp_parser_using_directive (parser);
10297       /* If the second token after 'using' is '=', then we have an
10298          alias-declaration.  */
10299       else if (cxx_dialect >= cxx0x
10300                && token2->type == CPP_NAME
10301                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10302                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10303                        == RID_ATTRIBUTE)))
10304         cp_parser_alias_declaration (parser);
10305       /* Otherwise, it's a using-declaration.  */
10306       else
10307         cp_parser_using_declaration (parser,
10308                                      /*access_declaration_p=*/false);
10309     }
10310   /* If the next keyword is `__label__' we have a misplaced label
10311      declaration.  */
10312   else if (token1->keyword == RID_LABEL)
10313     {
10314       cp_lexer_consume_token (parser->lexer);
10315       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10316       cp_parser_skip_to_end_of_statement (parser);
10317       /* If the next token is now a `;', consume it.  */
10318       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10319         cp_lexer_consume_token (parser->lexer);
10320     }
10321   /* If the next token is `static_assert' we have a static assertion.  */
10322   else if (token1->keyword == RID_STATIC_ASSERT)
10323     cp_parser_static_assert (parser, /*member_p=*/false);
10324   /* Anything else must be a simple-declaration.  */
10325   else
10326     cp_parser_simple_declaration (parser, !statement_p,
10327                                   /*maybe_range_for_decl*/NULL);
10328 }
10329
10330 /* Parse a simple-declaration.
10331
10332    simple-declaration:
10333      decl-specifier-seq [opt] init-declarator-list [opt] ;
10334
10335    init-declarator-list:
10336      init-declarator
10337      init-declarator-list , init-declarator
10338
10339    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10340    function-definition as a simple-declaration.
10341
10342    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10343    parsed declaration if it is an uninitialized single declarator not followed
10344    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10345    if present, will not be consumed.  */
10346
10347 static void
10348 cp_parser_simple_declaration (cp_parser* parser,
10349                               bool function_definition_allowed_p,
10350                               tree *maybe_range_for_decl)
10351 {
10352   cp_decl_specifier_seq decl_specifiers;
10353   int declares_class_or_enum;
10354   bool saw_declarator;
10355
10356   if (maybe_range_for_decl)
10357     *maybe_range_for_decl = NULL_TREE;
10358
10359   /* Defer access checks until we know what is being declared; the
10360      checks for names appearing in the decl-specifier-seq should be
10361      done as if we were in the scope of the thing being declared.  */
10362   push_deferring_access_checks (dk_deferred);
10363
10364   /* Parse the decl-specifier-seq.  We have to keep track of whether
10365      or not the decl-specifier-seq declares a named class or
10366      enumeration type, since that is the only case in which the
10367      init-declarator-list is allowed to be empty.
10368
10369      [dcl.dcl]
10370
10371      In a simple-declaration, the optional init-declarator-list can be
10372      omitted only when declaring a class or enumeration, that is when
10373      the decl-specifier-seq contains either a class-specifier, an
10374      elaborated-type-specifier, or an enum-specifier.  */
10375   cp_parser_decl_specifier_seq (parser,
10376                                 CP_PARSER_FLAGS_OPTIONAL,
10377                                 &decl_specifiers,
10378                                 &declares_class_or_enum);
10379   /* We no longer need to defer access checks.  */
10380   stop_deferring_access_checks ();
10381
10382   /* In a block scope, a valid declaration must always have a
10383      decl-specifier-seq.  By not trying to parse declarators, we can
10384      resolve the declaration/expression ambiguity more quickly.  */
10385   if (!function_definition_allowed_p
10386       && !decl_specifiers.any_specifiers_p)
10387     {
10388       cp_parser_error (parser, "expected declaration");
10389       goto done;
10390     }
10391
10392   /* If the next two tokens are both identifiers, the code is
10393      erroneous. The usual cause of this situation is code like:
10394
10395        T t;
10396
10397      where "T" should name a type -- but does not.  */
10398   if (!decl_specifiers.any_type_specifiers_p
10399       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10400     {
10401       /* If parsing tentatively, we should commit; we really are
10402          looking at a declaration.  */
10403       cp_parser_commit_to_tentative_parse (parser);
10404       /* Give up.  */
10405       goto done;
10406     }
10407
10408   /* If we have seen at least one decl-specifier, and the next token
10409      is not a parenthesis, then we must be looking at a declaration.
10410      (After "int (" we might be looking at a functional cast.)  */
10411   if (decl_specifiers.any_specifiers_p
10412       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10413       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10414       && !cp_parser_error_occurred (parser))
10415     cp_parser_commit_to_tentative_parse (parser);
10416
10417   /* Keep going until we hit the `;' at the end of the simple
10418      declaration.  */
10419   saw_declarator = false;
10420   while (cp_lexer_next_token_is_not (parser->lexer,
10421                                      CPP_SEMICOLON))
10422     {
10423       cp_token *token;
10424       bool function_definition_p;
10425       tree decl;
10426
10427       if (saw_declarator)
10428         {
10429           /* If we are processing next declarator, coma is expected */
10430           token = cp_lexer_peek_token (parser->lexer);
10431           gcc_assert (token->type == CPP_COMMA);
10432           cp_lexer_consume_token (parser->lexer);
10433           if (maybe_range_for_decl)
10434             *maybe_range_for_decl = error_mark_node;
10435         }
10436       else
10437         saw_declarator = true;
10438
10439       /* Parse the init-declarator.  */
10440       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10441                                         /*checks=*/NULL,
10442                                         function_definition_allowed_p,
10443                                         /*member_p=*/false,
10444                                         declares_class_or_enum,
10445                                         &function_definition_p,
10446                                         maybe_range_for_decl);
10447       /* If an error occurred while parsing tentatively, exit quickly.
10448          (That usually happens when in the body of a function; each
10449          statement is treated as a declaration-statement until proven
10450          otherwise.)  */
10451       if (cp_parser_error_occurred (parser))
10452         goto done;
10453       /* Handle function definitions specially.  */
10454       if (function_definition_p)
10455         {
10456           /* If the next token is a `,', then we are probably
10457              processing something like:
10458
10459                void f() {}, *p;
10460
10461              which is erroneous.  */
10462           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10463             {
10464               cp_token *token = cp_lexer_peek_token (parser->lexer);
10465               error_at (token->location,
10466                         "mixing"
10467                         " declarations and function-definitions is forbidden");
10468             }
10469           /* Otherwise, we're done with the list of declarators.  */
10470           else
10471             {
10472               pop_deferring_access_checks ();
10473               return;
10474             }
10475         }
10476       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10477         *maybe_range_for_decl = decl;
10478       /* The next token should be either a `,' or a `;'.  */
10479       token = cp_lexer_peek_token (parser->lexer);
10480       /* If it's a `,', there are more declarators to come.  */
10481       if (token->type == CPP_COMMA)
10482         /* will be consumed next time around */;
10483       /* If it's a `;', we are done.  */
10484       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10485         break;
10486       /* Anything else is an error.  */
10487       else
10488         {
10489           /* If we have already issued an error message we don't need
10490              to issue another one.  */
10491           if (decl != error_mark_node
10492               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10493             cp_parser_error (parser, "expected %<,%> or %<;%>");
10494           /* Skip tokens until we reach the end of the statement.  */
10495           cp_parser_skip_to_end_of_statement (parser);
10496           /* If the next token is now a `;', consume it.  */
10497           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10498             cp_lexer_consume_token (parser->lexer);
10499           goto done;
10500         }
10501       /* After the first time around, a function-definition is not
10502          allowed -- even if it was OK at first.  For example:
10503
10504            int i, f() {}
10505
10506          is not valid.  */
10507       function_definition_allowed_p = false;
10508     }
10509
10510   /* Issue an error message if no declarators are present, and the
10511      decl-specifier-seq does not itself declare a class or
10512      enumeration.  */
10513   if (!saw_declarator)
10514     {
10515       if (cp_parser_declares_only_class_p (parser))
10516         shadow_tag (&decl_specifiers);
10517       /* Perform any deferred access checks.  */
10518       perform_deferred_access_checks ();
10519     }
10520
10521   /* Consume the `;'.  */
10522   if (!maybe_range_for_decl)
10523       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10524
10525  done:
10526   pop_deferring_access_checks ();
10527 }
10528
10529 /* Parse a decl-specifier-seq.
10530
10531    decl-specifier-seq:
10532      decl-specifier-seq [opt] decl-specifier
10533
10534    decl-specifier:
10535      storage-class-specifier
10536      type-specifier
10537      function-specifier
10538      friend
10539      typedef
10540
10541    GNU Extension:
10542
10543    decl-specifier:
10544      attributes
10545
10546    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10547
10548    The parser flags FLAGS is used to control type-specifier parsing.
10549
10550    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10551    flags:
10552
10553      1: one of the decl-specifiers is an elaborated-type-specifier
10554         (i.e., a type declaration)
10555      2: one of the decl-specifiers is an enum-specifier or a
10556         class-specifier (i.e., a type definition)
10557
10558    */
10559
10560 static void
10561 cp_parser_decl_specifier_seq (cp_parser* parser,
10562                               cp_parser_flags flags,
10563                               cp_decl_specifier_seq *decl_specs,
10564                               int* declares_class_or_enum)
10565 {
10566   bool constructor_possible_p = !parser->in_declarator_p;
10567   cp_token *start_token = NULL;
10568   cp_decl_spec ds;
10569
10570   /* Clear DECL_SPECS.  */
10571   clear_decl_specs (decl_specs);
10572
10573   /* Assume no class or enumeration type is declared.  */
10574   *declares_class_or_enum = 0;
10575
10576   /* Keep reading specifiers until there are no more to read.  */
10577   while (true)
10578     {
10579       bool constructor_p;
10580       bool found_decl_spec;
10581       cp_token *token;
10582       ds = ds_last;
10583
10584       /* Peek at the next token.  */
10585       token = cp_lexer_peek_token (parser->lexer);
10586
10587       /* Save the first token of the decl spec list for error
10588          reporting.  */
10589       if (!start_token)
10590         start_token = token;
10591       /* Handle attributes.  */
10592       if (token->keyword == RID_ATTRIBUTE)
10593         {
10594           /* Parse the attributes.  */
10595           decl_specs->attributes
10596             = chainon (decl_specs->attributes,
10597                        cp_parser_attributes_opt (parser));
10598           if (decl_specs->locations[ds_attribute] == 0)
10599             decl_specs->locations[ds_attribute] = token->location;
10600           continue;
10601         }
10602       /* Assume we will find a decl-specifier keyword.  */
10603       found_decl_spec = true;
10604       /* If the next token is an appropriate keyword, we can simply
10605          add it to the list.  */
10606       switch (token->keyword)
10607         {
10608           /* decl-specifier:
10609                friend
10610                constexpr */
10611         case RID_FRIEND:
10612           if (!at_class_scope_p ())
10613             {
10614               error_at (token->location, "%<friend%> used outside of class");
10615               cp_lexer_purge_token (parser->lexer);
10616             }
10617           else
10618             {
10619               ds = ds_friend;
10620               /* Consume the token.  */
10621               cp_lexer_consume_token (parser->lexer);
10622             }
10623           break;
10624
10625         case RID_CONSTEXPR:
10626           ds = ds_constexpr;
10627           cp_lexer_consume_token (parser->lexer);
10628           break;
10629
10630           /* function-specifier:
10631                inline
10632                virtual
10633                explicit  */
10634         case RID_INLINE:
10635         case RID_VIRTUAL:
10636         case RID_EXPLICIT:
10637           cp_parser_function_specifier_opt (parser, decl_specs);
10638           break;
10639
10640           /* decl-specifier:
10641                typedef  */
10642         case RID_TYPEDEF:
10643           ds = ds_typedef;
10644           /* Consume the token.  */
10645           cp_lexer_consume_token (parser->lexer);
10646           /* A constructor declarator cannot appear in a typedef.  */
10647           constructor_possible_p = false;
10648           /* The "typedef" keyword can only occur in a declaration; we
10649              may as well commit at this point.  */
10650           cp_parser_commit_to_tentative_parse (parser);
10651
10652           if (decl_specs->storage_class != sc_none)
10653             decl_specs->conflicting_specifiers_p = true;
10654           break;
10655
10656           /* storage-class-specifier:
10657                auto
10658                register
10659                static
10660                extern
10661                mutable
10662
10663              GNU Extension:
10664                thread  */
10665         case RID_AUTO:
10666           if (cxx_dialect == cxx98) 
10667             {
10668               /* Consume the token.  */
10669               cp_lexer_consume_token (parser->lexer);
10670
10671               /* Complain about `auto' as a storage specifier, if
10672                  we're complaining about C++0x compatibility.  */
10673               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10674                           " changes meaning in C++11; please remove it");
10675
10676               /* Set the storage class anyway.  */
10677               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10678                                            token->location);
10679             }
10680           else
10681             /* C++0x auto type-specifier.  */
10682             found_decl_spec = false;
10683           break;
10684
10685         case RID_REGISTER:
10686         case RID_STATIC:
10687         case RID_EXTERN:
10688         case RID_MUTABLE:
10689           /* Consume the token.  */
10690           cp_lexer_consume_token (parser->lexer);
10691           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10692                                        token->location);
10693           break;
10694         case RID_THREAD:
10695           /* Consume the token.  */
10696           ds = ds_thread;
10697           cp_lexer_consume_token (parser->lexer);
10698           break;
10699
10700         default:
10701           /* We did not yet find a decl-specifier yet.  */
10702           found_decl_spec = false;
10703           break;
10704         }
10705
10706       if (found_decl_spec
10707           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10708           && token->keyword != RID_CONSTEXPR)
10709         error ("decl-specifier invalid in condition");
10710
10711       if (ds != ds_last)
10712         set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10713
10714       /* Constructors are a special case.  The `S' in `S()' is not a
10715          decl-specifier; it is the beginning of the declarator.  */
10716       constructor_p
10717         = (!found_decl_spec
10718            && constructor_possible_p
10719            && (cp_parser_constructor_declarator_p
10720                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10721
10722       /* If we don't have a DECL_SPEC yet, then we must be looking at
10723          a type-specifier.  */
10724       if (!found_decl_spec && !constructor_p)
10725         {
10726           int decl_spec_declares_class_or_enum;
10727           bool is_cv_qualifier;
10728           tree type_spec;
10729
10730           type_spec
10731             = cp_parser_type_specifier (parser, flags,
10732                                         decl_specs,
10733                                         /*is_declaration=*/true,
10734                                         &decl_spec_declares_class_or_enum,
10735                                         &is_cv_qualifier);
10736           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10737
10738           /* If this type-specifier referenced a user-defined type
10739              (a typedef, class-name, etc.), then we can't allow any
10740              more such type-specifiers henceforth.
10741
10742              [dcl.spec]
10743
10744              The longest sequence of decl-specifiers that could
10745              possibly be a type name is taken as the
10746              decl-specifier-seq of a declaration.  The sequence shall
10747              be self-consistent as described below.
10748
10749              [dcl.type]
10750
10751              As a general rule, at most one type-specifier is allowed
10752              in the complete decl-specifier-seq of a declaration.  The
10753              only exceptions are the following:
10754
10755              -- const or volatile can be combined with any other
10756                 type-specifier.
10757
10758              -- signed or unsigned can be combined with char, long,
10759                 short, or int.
10760
10761              -- ..
10762
10763              Example:
10764
10765                typedef char* Pc;
10766                void g (const int Pc);
10767
10768              Here, Pc is *not* part of the decl-specifier seq; it's
10769              the declarator.  Therefore, once we see a type-specifier
10770              (other than a cv-qualifier), we forbid any additional
10771              user-defined types.  We *do* still allow things like `int
10772              int' to be considered a decl-specifier-seq, and issue the
10773              error message later.  */
10774           if (type_spec && !is_cv_qualifier)
10775             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10776           /* A constructor declarator cannot follow a type-specifier.  */
10777           if (type_spec)
10778             {
10779               constructor_possible_p = false;
10780               found_decl_spec = true;
10781               if (!is_cv_qualifier)
10782                 decl_specs->any_type_specifiers_p = true;
10783             }
10784         }
10785
10786       /* If we still do not have a DECL_SPEC, then there are no more
10787          decl-specifiers.  */
10788       if (!found_decl_spec)
10789         break;
10790
10791       decl_specs->any_specifiers_p = true;
10792       /* After we see one decl-specifier, further decl-specifiers are
10793          always optional.  */
10794       flags |= CP_PARSER_FLAGS_OPTIONAL;
10795     }
10796
10797   /* Don't allow a friend specifier with a class definition.  */
10798   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10799       && (*declares_class_or_enum & 2))
10800     error_at (decl_specs->locations[ds_friend],
10801               "class definition may not be declared a friend");
10802 }
10803
10804 /* Parse an (optional) storage-class-specifier.
10805
10806    storage-class-specifier:
10807      auto
10808      register
10809      static
10810      extern
10811      mutable
10812
10813    GNU Extension:
10814
10815    storage-class-specifier:
10816      thread
10817
10818    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10819
10820 static tree
10821 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10822 {
10823   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10824     {
10825     case RID_AUTO:
10826       if (cxx_dialect != cxx98)
10827         return NULL_TREE;
10828       /* Fall through for C++98.  */
10829
10830     case RID_REGISTER:
10831     case RID_STATIC:
10832     case RID_EXTERN:
10833     case RID_MUTABLE:
10834     case RID_THREAD:
10835       /* Consume the token.  */
10836       return cp_lexer_consume_token (parser->lexer)->u.value;
10837
10838     default:
10839       return NULL_TREE;
10840     }
10841 }
10842
10843 /* Parse an (optional) function-specifier.
10844
10845    function-specifier:
10846      inline
10847      virtual
10848      explicit
10849
10850    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10851    Updates DECL_SPECS, if it is non-NULL.  */
10852
10853 static tree
10854 cp_parser_function_specifier_opt (cp_parser* parser,
10855                                   cp_decl_specifier_seq *decl_specs)
10856 {
10857   cp_token *token = cp_lexer_peek_token (parser->lexer);
10858   switch (token->keyword)
10859     {
10860     case RID_INLINE:
10861       set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10862       break;
10863
10864     case RID_VIRTUAL:
10865       /* 14.5.2.3 [temp.mem]
10866
10867          A member function template shall not be virtual.  */
10868       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10869         error_at (token->location, "templates may not be %<virtual%>");
10870       set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10871       break;
10872
10873     case RID_EXPLICIT:
10874       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10875       break;
10876
10877     default:
10878       return NULL_TREE;
10879     }
10880
10881   /* Consume the token.  */
10882   return cp_lexer_consume_token (parser->lexer)->u.value;
10883 }
10884
10885 /* Parse a linkage-specification.
10886
10887    linkage-specification:
10888      extern string-literal { declaration-seq [opt] }
10889      extern string-literal declaration  */
10890
10891 static void
10892 cp_parser_linkage_specification (cp_parser* parser)
10893 {
10894   tree linkage;
10895
10896   /* Look for the `extern' keyword.  */
10897   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10898
10899   /* Look for the string-literal.  */
10900   linkage = cp_parser_string_literal (parser, false, false);
10901
10902   /* Transform the literal into an identifier.  If the literal is a
10903      wide-character string, or contains embedded NULs, then we can't
10904      handle it as the user wants.  */
10905   if (strlen (TREE_STRING_POINTER (linkage))
10906       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10907     {
10908       cp_parser_error (parser, "invalid linkage-specification");
10909       /* Assume C++ linkage.  */
10910       linkage = lang_name_cplusplus;
10911     }
10912   else
10913     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10914
10915   /* We're now using the new linkage.  */
10916   push_lang_context (linkage);
10917
10918   /* If the next token is a `{', then we're using the first
10919      production.  */
10920   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10921     {
10922       /* Consume the `{' token.  */
10923       cp_lexer_consume_token (parser->lexer);
10924       /* Parse the declarations.  */
10925       cp_parser_declaration_seq_opt (parser);
10926       /* Look for the closing `}'.  */
10927       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10928     }
10929   /* Otherwise, there's just one declaration.  */
10930   else
10931     {
10932       bool saved_in_unbraced_linkage_specification_p;
10933
10934       saved_in_unbraced_linkage_specification_p
10935         = parser->in_unbraced_linkage_specification_p;
10936       parser->in_unbraced_linkage_specification_p = true;
10937       cp_parser_declaration (parser);
10938       parser->in_unbraced_linkage_specification_p
10939         = saved_in_unbraced_linkage_specification_p;
10940     }
10941
10942   /* We're done with the linkage-specification.  */
10943   pop_lang_context ();
10944 }
10945
10946 /* Parse a static_assert-declaration.
10947
10948    static_assert-declaration:
10949      static_assert ( constant-expression , string-literal ) ; 
10950
10951    If MEMBER_P, this static_assert is a class member.  */
10952
10953 static void 
10954 cp_parser_static_assert(cp_parser *parser, bool member_p)
10955 {
10956   tree condition;
10957   tree message;
10958   cp_token *token;
10959   location_t saved_loc;
10960   bool dummy;
10961
10962   /* Peek at the `static_assert' token so we can keep track of exactly
10963      where the static assertion started.  */
10964   token = cp_lexer_peek_token (parser->lexer);
10965   saved_loc = token->location;
10966
10967   /* Look for the `static_assert' keyword.  */
10968   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10969                                   RT_STATIC_ASSERT))
10970     return;
10971
10972   /*  We know we are in a static assertion; commit to any tentative
10973       parse.  */
10974   if (cp_parser_parsing_tentatively (parser))
10975     cp_parser_commit_to_tentative_parse (parser);
10976
10977   /* Parse the `(' starting the static assertion condition.  */
10978   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10979
10980   /* Parse the constant-expression.  Allow a non-constant expression
10981      here in order to give better diagnostics in finish_static_assert.  */
10982   condition = 
10983     cp_parser_constant_expression (parser,
10984                                    /*allow_non_constant_p=*/true,
10985                                    /*non_constant_p=*/&dummy);
10986
10987   /* Parse the separating `,'.  */
10988   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10989
10990   /* Parse the string-literal message.  */
10991   message = cp_parser_string_literal (parser, 
10992                                       /*translate=*/false,
10993                                       /*wide_ok=*/true);
10994
10995   /* A `)' completes the static assertion.  */
10996   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10997     cp_parser_skip_to_closing_parenthesis (parser, 
10998                                            /*recovering=*/true, 
10999                                            /*or_comma=*/false,
11000                                            /*consume_paren=*/true);
11001
11002   /* A semicolon terminates the declaration.  */
11003   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11004
11005   /* Complete the static assertion, which may mean either processing 
11006      the static assert now or saving it for template instantiation.  */
11007   finish_static_assert (condition, message, saved_loc, member_p);
11008 }
11009
11010 /* Parse a `decltype' type. Returns the type. 
11011
11012    simple-type-specifier:
11013      decltype ( expression )  */
11014
11015 static tree
11016 cp_parser_decltype (cp_parser *parser)
11017 {
11018   tree expr;
11019   bool id_expression_or_member_access_p = false;
11020   const char *saved_message;
11021   bool saved_integral_constant_expression_p;
11022   bool saved_non_integral_constant_expression_p;
11023   cp_token *id_expr_start_token;
11024   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11025
11026   if (start_token->type == CPP_DECLTYPE)
11027     {
11028       /* Already parsed.  */
11029       cp_lexer_consume_token (parser->lexer);
11030       return start_token->u.value;
11031     }
11032
11033   /* Look for the `decltype' token.  */
11034   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11035     return error_mark_node;
11036
11037   /* Types cannot be defined in a `decltype' expression.  Save away the
11038      old message.  */
11039   saved_message = parser->type_definition_forbidden_message;
11040
11041   /* And create the new one.  */
11042   parser->type_definition_forbidden_message
11043     = G_("types may not be defined in %<decltype%> expressions");
11044
11045   /* The restrictions on constant-expressions do not apply inside
11046      decltype expressions.  */
11047   saved_integral_constant_expression_p
11048     = parser->integral_constant_expression_p;
11049   saved_non_integral_constant_expression_p
11050     = parser->non_integral_constant_expression_p;
11051   parser->integral_constant_expression_p = false;
11052
11053   /* Do not actually evaluate the expression.  */
11054   ++cp_unevaluated_operand;
11055
11056   /* Do not warn about problems with the expression.  */
11057   ++c_inhibit_evaluation_warnings;
11058
11059   /* Parse the opening `('.  */
11060   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11061     return error_mark_node;
11062   
11063   /* First, try parsing an id-expression.  */
11064   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11065   cp_parser_parse_tentatively (parser);
11066   expr = cp_parser_id_expression (parser,
11067                                   /*template_keyword_p=*/false,
11068                                   /*check_dependency_p=*/true,
11069                                   /*template_p=*/NULL,
11070                                   /*declarator_p=*/false,
11071                                   /*optional_p=*/false);
11072
11073   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11074     {
11075       bool non_integral_constant_expression_p = false;
11076       tree id_expression = expr;
11077       cp_id_kind idk;
11078       const char *error_msg;
11079
11080       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11081         /* Lookup the name we got back from the id-expression.  */
11082         expr = cp_parser_lookup_name (parser, expr,
11083                                       none_type,
11084                                       /*is_template=*/false,
11085                                       /*is_namespace=*/false,
11086                                       /*check_dependency=*/true,
11087                                       /*ambiguous_decls=*/NULL,
11088                                       id_expr_start_token->location);
11089
11090       if (expr
11091           && expr != error_mark_node
11092           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11093           && TREE_CODE (expr) != TYPE_DECL
11094           && (TREE_CODE (expr) != BIT_NOT_EXPR
11095               || !TYPE_P (TREE_OPERAND (expr, 0)))
11096           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11097         {
11098           /* Complete lookup of the id-expression.  */
11099           expr = (finish_id_expression
11100                   (id_expression, expr, parser->scope, &idk,
11101                    /*integral_constant_expression_p=*/false,
11102                    /*allow_non_integral_constant_expression_p=*/true,
11103                    &non_integral_constant_expression_p,
11104                    /*template_p=*/false,
11105                    /*done=*/true,
11106                    /*address_p=*/false,
11107                    /*template_arg_p=*/false,
11108                    &error_msg,
11109                    id_expr_start_token->location));
11110
11111           if (expr == error_mark_node)
11112             /* We found an id-expression, but it was something that we
11113                should not have found. This is an error, not something
11114                we can recover from, so note that we found an
11115                id-expression and we'll recover as gracefully as
11116                possible.  */
11117             id_expression_or_member_access_p = true;
11118         }
11119
11120       if (expr 
11121           && expr != error_mark_node
11122           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11123         /* We have an id-expression.  */
11124         id_expression_or_member_access_p = true;
11125     }
11126
11127   if (!id_expression_or_member_access_p)
11128     {
11129       /* Abort the id-expression parse.  */
11130       cp_parser_abort_tentative_parse (parser);
11131
11132       /* Parsing tentatively, again.  */
11133       cp_parser_parse_tentatively (parser);
11134
11135       /* Parse a class member access.  */
11136       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11137                                            /*cast_p=*/false,
11138                                            /*member_access_only_p=*/true, NULL);
11139
11140       if (expr 
11141           && expr != error_mark_node
11142           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11143         /* We have an id-expression.  */
11144         id_expression_or_member_access_p = true;
11145     }
11146
11147   if (id_expression_or_member_access_p)
11148     /* We have parsed the complete id-expression or member access.  */
11149     cp_parser_parse_definitely (parser);
11150   else
11151     {
11152       bool saved_greater_than_is_operator_p;
11153
11154       /* Abort our attempt to parse an id-expression or member access
11155          expression.  */
11156       cp_parser_abort_tentative_parse (parser);
11157
11158       /* Within a parenthesized expression, a `>' token is always
11159          the greater-than operator.  */
11160       saved_greater_than_is_operator_p
11161         = parser->greater_than_is_operator_p;
11162       parser->greater_than_is_operator_p = true;
11163
11164       /* Parse a full expression.  */
11165       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11166
11167       /* The `>' token might be the end of a template-id or
11168          template-parameter-list now.  */
11169       parser->greater_than_is_operator_p
11170         = saved_greater_than_is_operator_p;
11171     }
11172
11173   /* Go back to evaluating expressions.  */
11174   --cp_unevaluated_operand;
11175   --c_inhibit_evaluation_warnings;
11176
11177   /* Restore the old message and the integral constant expression
11178      flags.  */
11179   parser->type_definition_forbidden_message = saved_message;
11180   parser->integral_constant_expression_p
11181     = saved_integral_constant_expression_p;
11182   parser->non_integral_constant_expression_p
11183     = saved_non_integral_constant_expression_p;
11184
11185   /* Parse to the closing `)'.  */
11186   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11187     {
11188       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11189                                              /*consume_paren=*/true);
11190       return error_mark_node;
11191     }
11192
11193   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11194                                tf_warning_or_error);
11195
11196   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11197      it again.  */
11198   start_token->type = CPP_DECLTYPE;
11199   start_token->u.value = expr;
11200   start_token->keyword = RID_MAX;
11201   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11202
11203   return expr;
11204 }
11205
11206 /* Special member functions [gram.special] */
11207
11208 /* Parse a conversion-function-id.
11209
11210    conversion-function-id:
11211      operator conversion-type-id
11212
11213    Returns an IDENTIFIER_NODE representing the operator.  */
11214
11215 static tree
11216 cp_parser_conversion_function_id (cp_parser* parser)
11217 {
11218   tree type;
11219   tree saved_scope;
11220   tree saved_qualifying_scope;
11221   tree saved_object_scope;
11222   tree pushed_scope = NULL_TREE;
11223
11224   /* Look for the `operator' token.  */
11225   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11226     return error_mark_node;
11227   /* When we parse the conversion-type-id, the current scope will be
11228      reset.  However, we need that information in able to look up the
11229      conversion function later, so we save it here.  */
11230   saved_scope = parser->scope;
11231   saved_qualifying_scope = parser->qualifying_scope;
11232   saved_object_scope = parser->object_scope;
11233   /* We must enter the scope of the class so that the names of
11234      entities declared within the class are available in the
11235      conversion-type-id.  For example, consider:
11236
11237        struct S {
11238          typedef int I;
11239          operator I();
11240        };
11241
11242        S::operator I() { ... }
11243
11244      In order to see that `I' is a type-name in the definition, we
11245      must be in the scope of `S'.  */
11246   if (saved_scope)
11247     pushed_scope = push_scope (saved_scope);
11248   /* Parse the conversion-type-id.  */
11249   type = cp_parser_conversion_type_id (parser);
11250   /* Leave the scope of the class, if any.  */
11251   if (pushed_scope)
11252     pop_scope (pushed_scope);
11253   /* Restore the saved scope.  */
11254   parser->scope = saved_scope;
11255   parser->qualifying_scope = saved_qualifying_scope;
11256   parser->object_scope = saved_object_scope;
11257   /* If the TYPE is invalid, indicate failure.  */
11258   if (type == error_mark_node)
11259     return error_mark_node;
11260   return mangle_conv_op_name_for_type (type);
11261 }
11262
11263 /* Parse a conversion-type-id:
11264
11265    conversion-type-id:
11266      type-specifier-seq conversion-declarator [opt]
11267
11268    Returns the TYPE specified.  */
11269
11270 static tree
11271 cp_parser_conversion_type_id (cp_parser* parser)
11272 {
11273   tree attributes;
11274   cp_decl_specifier_seq type_specifiers;
11275   cp_declarator *declarator;
11276   tree type_specified;
11277
11278   /* Parse the attributes.  */
11279   attributes = cp_parser_attributes_opt (parser);
11280   /* Parse the type-specifiers.  */
11281   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11282                                 /*is_trailing_return=*/false,
11283                                 &type_specifiers);
11284   /* If that didn't work, stop.  */
11285   if (type_specifiers.type == error_mark_node)
11286     return error_mark_node;
11287   /* Parse the conversion-declarator.  */
11288   declarator = cp_parser_conversion_declarator_opt (parser);
11289
11290   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11291                                     /*initialized=*/0, &attributes);
11292   if (attributes)
11293     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11294
11295   /* Don't give this error when parsing tentatively.  This happens to
11296      work because we always parse this definitively once.  */
11297   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11298       && type_uses_auto (type_specified))
11299     {
11300       if (cxx_dialect < cxx1y)
11301         {
11302           error ("invalid use of %<auto%> in conversion operator");
11303           return error_mark_node;
11304         }
11305       else if (template_parm_scope_p ())
11306         warning (0, "use of %<auto%> in member template "
11307                  "conversion operator can never be deduced");
11308     }
11309
11310   return type_specified;
11311 }
11312
11313 /* Parse an (optional) conversion-declarator.
11314
11315    conversion-declarator:
11316      ptr-operator conversion-declarator [opt]
11317
11318    */
11319
11320 static cp_declarator *
11321 cp_parser_conversion_declarator_opt (cp_parser* parser)
11322 {
11323   enum tree_code code;
11324   tree class_type;
11325   cp_cv_quals cv_quals;
11326
11327   /* We don't know if there's a ptr-operator next, or not.  */
11328   cp_parser_parse_tentatively (parser);
11329   /* Try the ptr-operator.  */
11330   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11331   /* If it worked, look for more conversion-declarators.  */
11332   if (cp_parser_parse_definitely (parser))
11333     {
11334       cp_declarator *declarator;
11335
11336       /* Parse another optional declarator.  */
11337       declarator = cp_parser_conversion_declarator_opt (parser);
11338
11339       return cp_parser_make_indirect_declarator
11340         (code, class_type, cv_quals, declarator);
11341    }
11342
11343   return NULL;
11344 }
11345
11346 /* Parse an (optional) ctor-initializer.
11347
11348    ctor-initializer:
11349      : mem-initializer-list
11350
11351    Returns TRUE iff the ctor-initializer was actually present.  */
11352
11353 static bool
11354 cp_parser_ctor_initializer_opt (cp_parser* parser)
11355 {
11356   /* If the next token is not a `:', then there is no
11357      ctor-initializer.  */
11358   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11359     {
11360       /* Do default initialization of any bases and members.  */
11361       if (DECL_CONSTRUCTOR_P (current_function_decl))
11362         finish_mem_initializers (NULL_TREE);
11363
11364       return false;
11365     }
11366
11367   /* Consume the `:' token.  */
11368   cp_lexer_consume_token (parser->lexer);
11369   /* And the mem-initializer-list.  */
11370   cp_parser_mem_initializer_list (parser);
11371
11372   return true;
11373 }
11374
11375 /* Parse a mem-initializer-list.
11376
11377    mem-initializer-list:
11378      mem-initializer ... [opt]
11379      mem-initializer ... [opt] , mem-initializer-list  */
11380
11381 static void
11382 cp_parser_mem_initializer_list (cp_parser* parser)
11383 {
11384   tree mem_initializer_list = NULL_TREE;
11385   tree target_ctor = error_mark_node;
11386   cp_token *token = cp_lexer_peek_token (parser->lexer);
11387
11388   /* Let the semantic analysis code know that we are starting the
11389      mem-initializer-list.  */
11390   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11391     error_at (token->location,
11392               "only constructors take member initializers");
11393
11394   /* Loop through the list.  */
11395   while (true)
11396     {
11397       tree mem_initializer;
11398
11399       token = cp_lexer_peek_token (parser->lexer);
11400       /* Parse the mem-initializer.  */
11401       mem_initializer = cp_parser_mem_initializer (parser);
11402       /* If the next token is a `...', we're expanding member initializers. */
11403       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11404         {
11405           /* Consume the `...'. */
11406           cp_lexer_consume_token (parser->lexer);
11407
11408           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11409              can be expanded but members cannot. */
11410           if (mem_initializer != error_mark_node
11411               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11412             {
11413               error_at (token->location,
11414                         "cannot expand initializer for member %<%D%>",
11415                         TREE_PURPOSE (mem_initializer));
11416               mem_initializer = error_mark_node;
11417             }
11418
11419           /* Construct the pack expansion type. */
11420           if (mem_initializer != error_mark_node)
11421             mem_initializer = make_pack_expansion (mem_initializer);
11422         }
11423       if (target_ctor != error_mark_node
11424           && mem_initializer != error_mark_node)
11425         {
11426           error ("mem-initializer for %qD follows constructor delegation",
11427                  TREE_PURPOSE (mem_initializer));
11428           mem_initializer = error_mark_node;
11429         }
11430       /* Look for a target constructor. */
11431       if (mem_initializer != error_mark_node
11432           && TYPE_P (TREE_PURPOSE (mem_initializer))
11433           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11434         {
11435           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11436           if (mem_initializer_list)
11437             {
11438               error ("constructor delegation follows mem-initializer for %qD",
11439                      TREE_PURPOSE (mem_initializer_list));
11440               mem_initializer = error_mark_node;
11441             }
11442           target_ctor = mem_initializer;
11443         }
11444       /* Add it to the list, unless it was erroneous.  */
11445       if (mem_initializer != error_mark_node)
11446         {
11447           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11448           mem_initializer_list = mem_initializer;
11449         }
11450       /* If the next token is not a `,', we're done.  */
11451       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11452         break;
11453       /* Consume the `,' token.  */
11454       cp_lexer_consume_token (parser->lexer);
11455     }
11456
11457   /* Perform semantic analysis.  */
11458   if (DECL_CONSTRUCTOR_P (current_function_decl))
11459     finish_mem_initializers (mem_initializer_list);
11460 }
11461
11462 /* Parse a mem-initializer.
11463
11464    mem-initializer:
11465      mem-initializer-id ( expression-list [opt] )
11466      mem-initializer-id braced-init-list
11467
11468    GNU extension:
11469
11470    mem-initializer:
11471      ( expression-list [opt] )
11472
11473    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11474    class) or FIELD_DECL (for a non-static data member) to initialize;
11475    the TREE_VALUE is the expression-list.  An empty initialization
11476    list is represented by void_list_node.  */
11477
11478 static tree
11479 cp_parser_mem_initializer (cp_parser* parser)
11480 {
11481   tree mem_initializer_id;
11482   tree expression_list;
11483   tree member;
11484   cp_token *token = cp_lexer_peek_token (parser->lexer);
11485
11486   /* Find out what is being initialized.  */
11487   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11488     {
11489       permerror (token->location,
11490                  "anachronistic old-style base class initializer");
11491       mem_initializer_id = NULL_TREE;
11492     }
11493   else
11494     {
11495       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11496       if (mem_initializer_id == error_mark_node)
11497         return mem_initializer_id;
11498     }
11499   member = expand_member_init (mem_initializer_id);
11500   if (member && !DECL_P (member))
11501     in_base_initializer = 1;
11502
11503   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11504     {
11505       bool expr_non_constant_p;
11506       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11507       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11508       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11509       expression_list = build_tree_list (NULL_TREE, expression_list);
11510     }
11511   else
11512     {
11513       VEC(tree,gc)* vec;
11514       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11515                                                      /*cast_p=*/false,
11516                                                      /*allow_expansion_p=*/true,
11517                                                      /*non_constant_p=*/NULL);
11518       if (vec == NULL)
11519         return error_mark_node;
11520       expression_list = build_tree_list_vec (vec);
11521       release_tree_vector (vec);
11522     }
11523
11524   if (expression_list == error_mark_node)
11525     return error_mark_node;
11526   if (!expression_list)
11527     expression_list = void_type_node;
11528
11529   in_base_initializer = 0;
11530
11531   return member ? build_tree_list (member, expression_list) : error_mark_node;
11532 }
11533
11534 /* Parse a mem-initializer-id.
11535
11536    mem-initializer-id:
11537      :: [opt] nested-name-specifier [opt] class-name
11538      identifier
11539
11540    Returns a TYPE indicating the class to be initializer for the first
11541    production.  Returns an IDENTIFIER_NODE indicating the data member
11542    to be initialized for the second production.  */
11543
11544 static tree
11545 cp_parser_mem_initializer_id (cp_parser* parser)
11546 {
11547   bool global_scope_p;
11548   bool nested_name_specifier_p;
11549   bool template_p = false;
11550   tree id;
11551
11552   cp_token *token = cp_lexer_peek_token (parser->lexer);
11553
11554   /* `typename' is not allowed in this context ([temp.res]).  */
11555   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11556     {
11557       error_at (token->location, 
11558                 "keyword %<typename%> not allowed in this context (a qualified "
11559                 "member initializer is implicitly a type)");
11560       cp_lexer_consume_token (parser->lexer);
11561     }
11562   /* Look for the optional `::' operator.  */
11563   global_scope_p
11564     = (cp_parser_global_scope_opt (parser,
11565                                    /*current_scope_valid_p=*/false)
11566        != NULL_TREE);
11567   /* Look for the optional nested-name-specifier.  The simplest way to
11568      implement:
11569
11570        [temp.res]
11571
11572        The keyword `typename' is not permitted in a base-specifier or
11573        mem-initializer; in these contexts a qualified name that
11574        depends on a template-parameter is implicitly assumed to be a
11575        type name.
11576
11577      is to assume that we have seen the `typename' keyword at this
11578      point.  */
11579   nested_name_specifier_p
11580     = (cp_parser_nested_name_specifier_opt (parser,
11581                                             /*typename_keyword_p=*/true,
11582                                             /*check_dependency_p=*/true,
11583                                             /*type_p=*/true,
11584                                             /*is_declaration=*/true)
11585        != NULL_TREE);
11586   if (nested_name_specifier_p)
11587     template_p = cp_parser_optional_template_keyword (parser);
11588   /* If there is a `::' operator or a nested-name-specifier, then we
11589      are definitely looking for a class-name.  */
11590   if (global_scope_p || nested_name_specifier_p)
11591     return cp_parser_class_name (parser,
11592                                  /*typename_keyword_p=*/true,
11593                                  /*template_keyword_p=*/template_p,
11594                                  typename_type,
11595                                  /*check_dependency_p=*/true,
11596                                  /*class_head_p=*/false,
11597                                  /*is_declaration=*/true);
11598   /* Otherwise, we could also be looking for an ordinary identifier.  */
11599   cp_parser_parse_tentatively (parser);
11600   /* Try a class-name.  */
11601   id = cp_parser_class_name (parser,
11602                              /*typename_keyword_p=*/true,
11603                              /*template_keyword_p=*/false,
11604                              none_type,
11605                              /*check_dependency_p=*/true,
11606                              /*class_head_p=*/false,
11607                              /*is_declaration=*/true);
11608   /* If we found one, we're done.  */
11609   if (cp_parser_parse_definitely (parser))
11610     return id;
11611   /* Otherwise, look for an ordinary identifier.  */
11612   return cp_parser_identifier (parser);
11613 }
11614
11615 /* Overloading [gram.over] */
11616
11617 /* Parse an operator-function-id.
11618
11619    operator-function-id:
11620      operator operator
11621
11622    Returns an IDENTIFIER_NODE for the operator which is a
11623    human-readable spelling of the identifier, e.g., `operator +'.  */
11624
11625 static tree
11626 cp_parser_operator_function_id (cp_parser* parser)
11627 {
11628   /* Look for the `operator' keyword.  */
11629   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11630     return error_mark_node;
11631   /* And then the name of the operator itself.  */
11632   return cp_parser_operator (parser);
11633 }
11634
11635 /* Return an identifier node for a user-defined literal operator.
11636    The suffix identifier is chained to the operator name identifier.  */
11637
11638 static tree
11639 cp_literal_operator_id (const char* name)
11640 {
11641   tree identifier;
11642   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11643                               + strlen (name) + 10);
11644   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11645   identifier = get_identifier (buffer);
11646   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11647
11648   return identifier;
11649 }
11650
11651 /* Parse an operator.
11652
11653    operator:
11654      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11655      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11656      || ++ -- , ->* -> () []
11657
11658    GNU Extensions:
11659
11660    operator:
11661      <? >? <?= >?=
11662
11663    Returns an IDENTIFIER_NODE for the operator which is a
11664    human-readable spelling of the identifier, e.g., `operator +'.  */
11665
11666 static tree
11667 cp_parser_operator (cp_parser* parser)
11668 {
11669   tree id = NULL_TREE;
11670   cp_token *token;
11671
11672   /* Peek at the next token.  */
11673   token = cp_lexer_peek_token (parser->lexer);
11674   /* Figure out which operator we have.  */
11675   switch (token->type)
11676     {
11677     case CPP_KEYWORD:
11678       {
11679         enum tree_code op;
11680
11681         /* The keyword should be either `new' or `delete'.  */
11682         if (token->keyword == RID_NEW)
11683           op = NEW_EXPR;
11684         else if (token->keyword == RID_DELETE)
11685           op = DELETE_EXPR;
11686         else
11687           break;
11688
11689         /* Consume the `new' or `delete' token.  */
11690         cp_lexer_consume_token (parser->lexer);
11691
11692         /* Peek at the next token.  */
11693         token = cp_lexer_peek_token (parser->lexer);
11694         /* If it's a `[' token then this is the array variant of the
11695            operator.  */
11696         if (token->type == CPP_OPEN_SQUARE)
11697           {
11698             /* Consume the `[' token.  */
11699             cp_lexer_consume_token (parser->lexer);
11700             /* Look for the `]' token.  */
11701             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11702             id = ansi_opname (op == NEW_EXPR
11703                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11704           }
11705         /* Otherwise, we have the non-array variant.  */
11706         else
11707           id = ansi_opname (op);
11708
11709         return id;
11710       }
11711
11712     case CPP_PLUS:
11713       id = ansi_opname (PLUS_EXPR);
11714       break;
11715
11716     case CPP_MINUS:
11717       id = ansi_opname (MINUS_EXPR);
11718       break;
11719
11720     case CPP_MULT:
11721       id = ansi_opname (MULT_EXPR);
11722       break;
11723
11724     case CPP_DIV:
11725       id = ansi_opname (TRUNC_DIV_EXPR);
11726       break;
11727
11728     case CPP_MOD:
11729       id = ansi_opname (TRUNC_MOD_EXPR);
11730       break;
11731
11732     case CPP_XOR:
11733       id = ansi_opname (BIT_XOR_EXPR);
11734       break;
11735
11736     case CPP_AND:
11737       id = ansi_opname (BIT_AND_EXPR);
11738       break;
11739
11740     case CPP_OR:
11741       id = ansi_opname (BIT_IOR_EXPR);
11742       break;
11743
11744     case CPP_COMPL:
11745       id = ansi_opname (BIT_NOT_EXPR);
11746       break;
11747
11748     case CPP_NOT:
11749       id = ansi_opname (TRUTH_NOT_EXPR);
11750       break;
11751
11752     case CPP_EQ:
11753       id = ansi_assopname (NOP_EXPR);
11754       break;
11755
11756     case CPP_LESS:
11757       id = ansi_opname (LT_EXPR);
11758       break;
11759
11760     case CPP_GREATER:
11761       id = ansi_opname (GT_EXPR);
11762       break;
11763
11764     case CPP_PLUS_EQ:
11765       id = ansi_assopname (PLUS_EXPR);
11766       break;
11767
11768     case CPP_MINUS_EQ:
11769       id = ansi_assopname (MINUS_EXPR);
11770       break;
11771
11772     case CPP_MULT_EQ:
11773       id = ansi_assopname (MULT_EXPR);
11774       break;
11775
11776     case CPP_DIV_EQ:
11777       id = ansi_assopname (TRUNC_DIV_EXPR);
11778       break;
11779
11780     case CPP_MOD_EQ:
11781       id = ansi_assopname (TRUNC_MOD_EXPR);
11782       break;
11783
11784     case CPP_XOR_EQ:
11785       id = ansi_assopname (BIT_XOR_EXPR);
11786       break;
11787
11788     case CPP_AND_EQ:
11789       id = ansi_assopname (BIT_AND_EXPR);
11790       break;
11791
11792     case CPP_OR_EQ:
11793       id = ansi_assopname (BIT_IOR_EXPR);
11794       break;
11795
11796     case CPP_LSHIFT:
11797       id = ansi_opname (LSHIFT_EXPR);
11798       break;
11799
11800     case CPP_RSHIFT:
11801       id = ansi_opname (RSHIFT_EXPR);
11802       break;
11803
11804     case CPP_LSHIFT_EQ:
11805       id = ansi_assopname (LSHIFT_EXPR);
11806       break;
11807
11808     case CPP_RSHIFT_EQ:
11809       id = ansi_assopname (RSHIFT_EXPR);
11810       break;
11811
11812     case CPP_EQ_EQ:
11813       id = ansi_opname (EQ_EXPR);
11814       break;
11815
11816     case CPP_NOT_EQ:
11817       id = ansi_opname (NE_EXPR);
11818       break;
11819
11820     case CPP_LESS_EQ:
11821       id = ansi_opname (LE_EXPR);
11822       break;
11823
11824     case CPP_GREATER_EQ:
11825       id = ansi_opname (GE_EXPR);
11826       break;
11827
11828     case CPP_AND_AND:
11829       id = ansi_opname (TRUTH_ANDIF_EXPR);
11830       break;
11831
11832     case CPP_OR_OR:
11833       id = ansi_opname (TRUTH_ORIF_EXPR);
11834       break;
11835
11836     case CPP_PLUS_PLUS:
11837       id = ansi_opname (POSTINCREMENT_EXPR);
11838       break;
11839
11840     case CPP_MINUS_MINUS:
11841       id = ansi_opname (PREDECREMENT_EXPR);
11842       break;
11843
11844     case CPP_COMMA:
11845       id = ansi_opname (COMPOUND_EXPR);
11846       break;
11847
11848     case CPP_DEREF_STAR:
11849       id = ansi_opname (MEMBER_REF);
11850       break;
11851
11852     case CPP_DEREF:
11853       id = ansi_opname (COMPONENT_REF);
11854       break;
11855
11856     case CPP_OPEN_PAREN:
11857       /* Consume the `('.  */
11858       cp_lexer_consume_token (parser->lexer);
11859       /* Look for the matching `)'.  */
11860       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11861       return ansi_opname (CALL_EXPR);
11862
11863     case CPP_OPEN_SQUARE:
11864       /* Consume the `['.  */
11865       cp_lexer_consume_token (parser->lexer);
11866       /* Look for the matching `]'.  */
11867       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11868       return ansi_opname (ARRAY_REF);
11869
11870     case CPP_STRING:
11871       if (cxx_dialect == cxx98)
11872         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11873       if (TREE_STRING_LENGTH (token->u.value) > 2)
11874         {
11875           error ("expected empty string after %<operator%> keyword");
11876           return error_mark_node;
11877         }
11878       /* Consume the string.  */
11879       cp_lexer_consume_token (parser->lexer);
11880       /* Look for the suffix identifier.  */
11881       token = cp_lexer_peek_token (parser->lexer);
11882       if (token->type == CPP_NAME)
11883         {
11884           id = cp_parser_identifier (parser);
11885           if (id != error_mark_node)
11886             {
11887               const char *name = IDENTIFIER_POINTER (id);
11888               return cp_literal_operator_id (name);
11889             }
11890         }
11891       else
11892         {
11893           error ("expected suffix identifier");
11894           return error_mark_node;
11895         }
11896
11897     case CPP_STRING_USERDEF:
11898       error ("missing space between %<\"\"%> and suffix identifier");
11899       return error_mark_node;
11900
11901     default:
11902       /* Anything else is an error.  */
11903       break;
11904     }
11905
11906   /* If we have selected an identifier, we need to consume the
11907      operator token.  */
11908   if (id)
11909     cp_lexer_consume_token (parser->lexer);
11910   /* Otherwise, no valid operator name was present.  */
11911   else
11912     {
11913       cp_parser_error (parser, "expected operator");
11914       id = error_mark_node;
11915     }
11916
11917   return id;
11918 }
11919
11920 /* Parse a template-declaration.
11921
11922    template-declaration:
11923      export [opt] template < template-parameter-list > declaration
11924
11925    If MEMBER_P is TRUE, this template-declaration occurs within a
11926    class-specifier.
11927
11928    The grammar rule given by the standard isn't correct.  What
11929    is really meant is:
11930
11931    template-declaration:
11932      export [opt] template-parameter-list-seq
11933        decl-specifier-seq [opt] init-declarator [opt] ;
11934      export [opt] template-parameter-list-seq
11935        function-definition
11936
11937    template-parameter-list-seq:
11938      template-parameter-list-seq [opt]
11939      template < template-parameter-list >  */
11940
11941 static void
11942 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11943 {
11944   /* Check for `export'.  */
11945   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11946     {
11947       /* Consume the `export' token.  */
11948       cp_lexer_consume_token (parser->lexer);
11949       /* Warn that we do not support `export'.  */
11950       warning (0, "keyword %<export%> not implemented, and will be ignored");
11951     }
11952
11953   cp_parser_template_declaration_after_export (parser, member_p);
11954 }
11955
11956 /* Parse a template-parameter-list.
11957
11958    template-parameter-list:
11959      template-parameter
11960      template-parameter-list , template-parameter
11961
11962    Returns a TREE_LIST.  Each node represents a template parameter.
11963    The nodes are connected via their TREE_CHAINs.  */
11964
11965 static tree
11966 cp_parser_template_parameter_list (cp_parser* parser)
11967 {
11968   tree parameter_list = NULL_TREE;
11969
11970   begin_template_parm_list ();
11971
11972   /* The loop below parses the template parms.  We first need to know
11973      the total number of template parms to be able to compute proper
11974      canonical types of each dependent type. So after the loop, when
11975      we know the total number of template parms,
11976      end_template_parm_list computes the proper canonical types and
11977      fixes up the dependent types accordingly.  */
11978   while (true)
11979     {
11980       tree parameter;
11981       bool is_non_type;
11982       bool is_parameter_pack;
11983       location_t parm_loc;
11984
11985       /* Parse the template-parameter.  */
11986       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11987       parameter = cp_parser_template_parameter (parser, 
11988                                                 &is_non_type,
11989                                                 &is_parameter_pack);
11990       /* Add it to the list.  */
11991       if (parameter != error_mark_node)
11992         parameter_list = process_template_parm (parameter_list,
11993                                                 parm_loc,
11994                                                 parameter,
11995                                                 is_non_type,
11996                                                 is_parameter_pack);
11997       else
11998        {
11999          tree err_parm = build_tree_list (parameter, parameter);
12000          parameter_list = chainon (parameter_list, err_parm);
12001        }
12002
12003       /* If the next token is not a `,', we're done.  */
12004       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12005         break;
12006       /* Otherwise, consume the `,' token.  */
12007       cp_lexer_consume_token (parser->lexer);
12008     }
12009
12010   return end_template_parm_list (parameter_list);
12011 }
12012
12013 /* Parse a template-parameter.
12014
12015    template-parameter:
12016      type-parameter
12017      parameter-declaration
12018
12019    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
12020    the parameter.  The TREE_PURPOSE is the default value, if any.
12021    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
12022    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
12023    set to true iff this parameter is a parameter pack. */
12024
12025 static tree
12026 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12027                               bool *is_parameter_pack)
12028 {
12029   cp_token *token;
12030   cp_parameter_declarator *parameter_declarator;
12031   cp_declarator *id_declarator;
12032   tree parm;
12033
12034   /* Assume it is a type parameter or a template parameter.  */
12035   *is_non_type = false;
12036   /* Assume it not a parameter pack. */
12037   *is_parameter_pack = false;
12038   /* Peek at the next token.  */
12039   token = cp_lexer_peek_token (parser->lexer);
12040   /* If it is `class' or `template', we have a type-parameter.  */
12041   if (token->keyword == RID_TEMPLATE)
12042     return cp_parser_type_parameter (parser, is_parameter_pack);
12043   /* If it is `class' or `typename' we do not know yet whether it is a
12044      type parameter or a non-type parameter.  Consider:
12045
12046        template <typename T, typename T::X X> ...
12047
12048      or:
12049
12050        template <class C, class D*> ...
12051
12052      Here, the first parameter is a type parameter, and the second is
12053      a non-type parameter.  We can tell by looking at the token after
12054      the identifier -- if it is a `,', `=', or `>' then we have a type
12055      parameter.  */
12056   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12057     {
12058       /* Peek at the token after `class' or `typename'.  */
12059       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12060       /* If it's an ellipsis, we have a template type parameter
12061          pack. */
12062       if (token->type == CPP_ELLIPSIS)
12063         return cp_parser_type_parameter (parser, is_parameter_pack);
12064       /* If it's an identifier, skip it.  */
12065       if (token->type == CPP_NAME)
12066         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12067       /* Now, see if the token looks like the end of a template
12068          parameter.  */
12069       if (token->type == CPP_COMMA
12070           || token->type == CPP_EQ
12071           || token->type == CPP_GREATER)
12072         return cp_parser_type_parameter (parser, is_parameter_pack);
12073     }
12074
12075   /* Otherwise, it is a non-type parameter.
12076
12077      [temp.param]
12078
12079      When parsing a default template-argument for a non-type
12080      template-parameter, the first non-nested `>' is taken as the end
12081      of the template parameter-list rather than a greater-than
12082      operator.  */
12083   *is_non_type = true;
12084   parameter_declarator
12085      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12086                                         /*parenthesized_p=*/NULL);
12087
12088   /* If the parameter declaration is marked as a parameter pack, set
12089      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12090      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12091      grokdeclarator. */
12092   if (parameter_declarator
12093       && parameter_declarator->declarator
12094       && parameter_declarator->declarator->parameter_pack_p)
12095     {
12096       *is_parameter_pack = true;
12097       parameter_declarator->declarator->parameter_pack_p = false;
12098     }
12099
12100   /* If the next token is an ellipsis, and we don't already have it
12101      marked as a parameter pack, then we have a parameter pack (that
12102      has no declarator).  */
12103   if (!*is_parameter_pack
12104       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12105       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12106     {
12107       /* Consume the `...'.  */
12108       cp_lexer_consume_token (parser->lexer);
12109       maybe_warn_variadic_templates ();
12110       
12111       *is_parameter_pack = true;
12112     }
12113   /* We might end up with a pack expansion as the type of the non-type
12114      template parameter, in which case this is a non-type template
12115      parameter pack.  */
12116   else if (parameter_declarator
12117            && parameter_declarator->decl_specifiers.type
12118            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12119     {
12120       *is_parameter_pack = true;
12121       parameter_declarator->decl_specifiers.type = 
12122         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12123     }
12124
12125   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12126     {
12127       /* Parameter packs cannot have default arguments.  However, a
12128          user may try to do so, so we'll parse them and give an
12129          appropriate diagnostic here.  */
12130
12131       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12132       
12133       /* Find the name of the parameter pack.  */     
12134       id_declarator = parameter_declarator->declarator;
12135       while (id_declarator && id_declarator->kind != cdk_id)
12136         id_declarator = id_declarator->declarator;
12137       
12138       if (id_declarator && id_declarator->kind == cdk_id)
12139         error_at (start_token->location,
12140                   "template parameter pack %qD cannot have a default argument",
12141                   id_declarator->u.id.unqualified_name);
12142       else
12143         error_at (start_token->location,
12144                   "template parameter pack cannot have a default argument");
12145       
12146       /* Parse the default argument, but throw away the result.  */
12147       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12148     }
12149
12150   parm = grokdeclarator (parameter_declarator->declarator,
12151                          &parameter_declarator->decl_specifiers,
12152                          TPARM, /*initialized=*/0,
12153                          /*attrlist=*/NULL);
12154   if (parm == error_mark_node)
12155     return error_mark_node;
12156
12157   return build_tree_list (parameter_declarator->default_argument, parm);
12158 }
12159
12160 /* Parse a type-parameter.
12161
12162    type-parameter:
12163      class identifier [opt]
12164      class identifier [opt] = type-id
12165      typename identifier [opt]
12166      typename identifier [opt] = type-id
12167      template < template-parameter-list > class identifier [opt]
12168      template < template-parameter-list > class identifier [opt]
12169        = id-expression
12170
12171    GNU Extension (variadic templates):
12172
12173    type-parameter:
12174      class ... identifier [opt]
12175      typename ... identifier [opt]
12176
12177    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12178    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12179    the declaration of the parameter.
12180
12181    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12182
12183 static tree
12184 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12185 {
12186   cp_token *token;
12187   tree parameter;
12188
12189   /* Look for a keyword to tell us what kind of parameter this is.  */
12190   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12191   if (!token)
12192     return error_mark_node;
12193
12194   switch (token->keyword)
12195     {
12196     case RID_CLASS:
12197     case RID_TYPENAME:
12198       {
12199         tree identifier;
12200         tree default_argument;
12201
12202         /* If the next token is an ellipsis, we have a template
12203            argument pack. */
12204         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12205           {
12206             /* Consume the `...' token. */
12207             cp_lexer_consume_token (parser->lexer);
12208             maybe_warn_variadic_templates ();
12209
12210             *is_parameter_pack = true;
12211           }
12212
12213         /* If the next token is an identifier, then it names the
12214            parameter.  */
12215         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12216           identifier = cp_parser_identifier (parser);
12217         else
12218           identifier = NULL_TREE;
12219
12220         /* Create the parameter.  */
12221         parameter = finish_template_type_parm (class_type_node, identifier);
12222
12223         /* If the next token is an `=', we have a default argument.  */
12224         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12225           {
12226             /* Consume the `=' token.  */
12227             cp_lexer_consume_token (parser->lexer);
12228             /* Parse the default-argument.  */
12229             push_deferring_access_checks (dk_no_deferred);
12230             default_argument = cp_parser_type_id (parser);
12231
12232             /* Template parameter packs cannot have default
12233                arguments. */
12234             if (*is_parameter_pack)
12235               {
12236                 if (identifier)
12237                   error_at (token->location,
12238                             "template parameter pack %qD cannot have a "
12239                             "default argument", identifier);
12240                 else
12241                   error_at (token->location,
12242                             "template parameter packs cannot have "
12243                             "default arguments");
12244                 default_argument = NULL_TREE;
12245               }
12246             pop_deferring_access_checks ();
12247           }
12248         else
12249           default_argument = NULL_TREE;
12250
12251         /* Create the combined representation of the parameter and the
12252            default argument.  */
12253         parameter = build_tree_list (default_argument, parameter);
12254       }
12255       break;
12256
12257     case RID_TEMPLATE:
12258       {
12259         tree identifier;
12260         tree default_argument;
12261
12262         /* Look for the `<'.  */
12263         cp_parser_require (parser, CPP_LESS, RT_LESS);
12264         /* Parse the template-parameter-list.  */
12265         cp_parser_template_parameter_list (parser);
12266         /* Look for the `>'.  */
12267         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12268         /* Look for the `class' keyword.  */
12269         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12270         /* If the next token is an ellipsis, we have a template
12271            argument pack. */
12272         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12273           {
12274             /* Consume the `...' token. */
12275             cp_lexer_consume_token (parser->lexer);
12276             maybe_warn_variadic_templates ();
12277
12278             *is_parameter_pack = true;
12279           }
12280         /* If the next token is an `=', then there is a
12281            default-argument.  If the next token is a `>', we are at
12282            the end of the parameter-list.  If the next token is a `,',
12283            then we are at the end of this parameter.  */
12284         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12285             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12286             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12287           {
12288             identifier = cp_parser_identifier (parser);
12289             /* Treat invalid names as if the parameter were nameless.  */
12290             if (identifier == error_mark_node)
12291               identifier = NULL_TREE;
12292           }
12293         else
12294           identifier = NULL_TREE;
12295
12296         /* Create the template parameter.  */
12297         parameter = finish_template_template_parm (class_type_node,
12298                                                    identifier);
12299
12300         /* If the next token is an `=', then there is a
12301            default-argument.  */
12302         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12303           {
12304             bool is_template;
12305
12306             /* Consume the `='.  */
12307             cp_lexer_consume_token (parser->lexer);
12308             /* Parse the id-expression.  */
12309             push_deferring_access_checks (dk_no_deferred);
12310             /* save token before parsing the id-expression, for error
12311                reporting */
12312             token = cp_lexer_peek_token (parser->lexer);
12313             default_argument
12314               = cp_parser_id_expression (parser,
12315                                          /*template_keyword_p=*/false,
12316                                          /*check_dependency_p=*/true,
12317                                          /*template_p=*/&is_template,
12318                                          /*declarator_p=*/false,
12319                                          /*optional_p=*/false);
12320             if (TREE_CODE (default_argument) == TYPE_DECL)
12321               /* If the id-expression was a template-id that refers to
12322                  a template-class, we already have the declaration here,
12323                  so no further lookup is needed.  */
12324                  ;
12325             else
12326               /* Look up the name.  */
12327               default_argument
12328                 = cp_parser_lookup_name (parser, default_argument,
12329                                          none_type,
12330                                          /*is_template=*/is_template,
12331                                          /*is_namespace=*/false,
12332                                          /*check_dependency=*/true,
12333                                          /*ambiguous_decls=*/NULL,
12334                                          token->location);
12335             /* See if the default argument is valid.  */
12336             default_argument
12337               = check_template_template_default_arg (default_argument);
12338
12339             /* Template parameter packs cannot have default
12340                arguments. */
12341             if (*is_parameter_pack)
12342               {
12343                 if (identifier)
12344                   error_at (token->location,
12345                             "template parameter pack %qD cannot "
12346                             "have a default argument",
12347                             identifier);
12348                 else
12349                   error_at (token->location, "template parameter packs cannot "
12350                             "have default arguments");
12351                 default_argument = NULL_TREE;
12352               }
12353             pop_deferring_access_checks ();
12354           }
12355         else
12356           default_argument = NULL_TREE;
12357
12358         /* Create the combined representation of the parameter and the
12359            default argument.  */
12360         parameter = build_tree_list (default_argument, parameter);
12361       }
12362       break;
12363
12364     default:
12365       gcc_unreachable ();
12366       break;
12367     }
12368
12369   return parameter;
12370 }
12371
12372 /* Parse a template-id.
12373
12374    template-id:
12375      template-name < template-argument-list [opt] >
12376
12377    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12378    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12379    returned.  Otherwise, if the template-name names a function, or set
12380    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12381    names a class, returns a TYPE_DECL for the specialization.
12382
12383    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12384    uninstantiated templates.  */
12385
12386 static tree
12387 cp_parser_template_id (cp_parser *parser,
12388                        bool template_keyword_p,
12389                        bool check_dependency_p,
12390                        enum tag_types tag_type,
12391                        bool is_declaration)
12392 {
12393   int i;
12394   tree templ;
12395   tree arguments;
12396   tree template_id;
12397   cp_token_position start_of_id = 0;
12398   deferred_access_check *chk;
12399   VEC (deferred_access_check,gc) *access_check;
12400   cp_token *next_token = NULL, *next_token_2 = NULL;
12401   bool is_identifier;
12402
12403   /* If the next token corresponds to a template-id, there is no need
12404      to reparse it.  */
12405   next_token = cp_lexer_peek_token (parser->lexer);
12406   if (next_token->type == CPP_TEMPLATE_ID)
12407     {
12408       struct tree_check *check_value;
12409
12410       /* Get the stored value.  */
12411       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12412       /* Perform any access checks that were deferred.  */
12413       access_check = check_value->checks;
12414       if (access_check)
12415         {
12416           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12417             perform_or_defer_access_check (chk->binfo,
12418                                            chk->decl,
12419                                            chk->diag_decl);
12420         }
12421       /* Return the stored value.  */
12422       return check_value->value;
12423     }
12424
12425   /* Avoid performing name lookup if there is no possibility of
12426      finding a template-id.  */
12427   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12428       || (next_token->type == CPP_NAME
12429           && !cp_parser_nth_token_starts_template_argument_list_p
12430                (parser, 2)))
12431     {
12432       cp_parser_error (parser, "expected template-id");
12433       return error_mark_node;
12434     }
12435
12436   /* Remember where the template-id starts.  */
12437   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12438     start_of_id = cp_lexer_token_position (parser->lexer, false);
12439
12440   push_deferring_access_checks (dk_deferred);
12441
12442   /* Parse the template-name.  */
12443   is_identifier = false;
12444   templ = cp_parser_template_name (parser, template_keyword_p,
12445                                    check_dependency_p,
12446                                    is_declaration,
12447                                    tag_type,
12448                                    &is_identifier);
12449   if (templ == error_mark_node || is_identifier)
12450     {
12451       pop_deferring_access_checks ();
12452       return templ;
12453     }
12454
12455   /* If we find the sequence `[:' after a template-name, it's probably
12456      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12457      parse correctly the argument list.  */
12458   next_token = cp_lexer_peek_token (parser->lexer);
12459   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12460   if (next_token->type == CPP_OPEN_SQUARE
12461       && next_token->flags & DIGRAPH
12462       && next_token_2->type == CPP_COLON
12463       && !(next_token_2->flags & PREV_WHITE))
12464     {
12465       cp_parser_parse_tentatively (parser);
12466       /* Change `:' into `::'.  */
12467       next_token_2->type = CPP_SCOPE;
12468       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12469          CPP_LESS.  */
12470       cp_lexer_consume_token (parser->lexer);
12471
12472       /* Parse the arguments.  */
12473       arguments = cp_parser_enclosed_template_argument_list (parser);
12474       if (!cp_parser_parse_definitely (parser))
12475         {
12476           /* If we couldn't parse an argument list, then we revert our changes
12477              and return simply an error. Maybe this is not a template-id
12478              after all.  */
12479           next_token_2->type = CPP_COLON;
12480           cp_parser_error (parser, "expected %<<%>");
12481           pop_deferring_access_checks ();
12482           return error_mark_node;
12483         }
12484       /* Otherwise, emit an error about the invalid digraph, but continue
12485          parsing because we got our argument list.  */
12486       if (permerror (next_token->location,
12487                      "%<<::%> cannot begin a template-argument list"))
12488         {
12489           static bool hint = false;
12490           inform (next_token->location,
12491                   "%<<:%> is an alternate spelling for %<[%>."
12492                   " Insert whitespace between %<<%> and %<::%>");
12493           if (!hint && !flag_permissive)
12494             {
12495               inform (next_token->location, "(if you use %<-fpermissive%>"
12496                       " G++ will accept your code)");
12497               hint = true;
12498             }
12499         }
12500     }
12501   else
12502     {
12503       /* Look for the `<' that starts the template-argument-list.  */
12504       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12505         {
12506           pop_deferring_access_checks ();
12507           return error_mark_node;
12508         }
12509       /* Parse the arguments.  */
12510       arguments = cp_parser_enclosed_template_argument_list (parser);
12511     }
12512
12513   /* Build a representation of the specialization.  */
12514   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12515     template_id = build_min_nt_loc (next_token->location,
12516                                     TEMPLATE_ID_EXPR,
12517                                     templ, arguments);
12518   else if (DECL_TYPE_TEMPLATE_P (templ)
12519            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12520     {
12521       bool entering_scope;
12522       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12523          template (rather than some instantiation thereof) only if
12524          is not nested within some other construct.  For example, in
12525          "template <typename T> void f(T) { A<T>::", A<T> is just an
12526          instantiation of A.  */
12527       entering_scope = (template_parm_scope_p ()
12528                         && cp_lexer_next_token_is (parser->lexer,
12529                                                    CPP_SCOPE));
12530       template_id
12531         = finish_template_type (templ, arguments, entering_scope);
12532     }
12533   else
12534     {
12535       /* If it's not a class-template or a template-template, it should be
12536          a function-template.  */
12537       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12538                    || TREE_CODE (templ) == OVERLOAD
12539                    || BASELINK_P (templ)));
12540
12541       template_id = lookup_template_function (templ, arguments);
12542     }
12543
12544   /* If parsing tentatively, replace the sequence of tokens that makes
12545      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12546      should we re-parse the token stream, we will not have to repeat
12547      the effort required to do the parse, nor will we issue duplicate
12548      error messages about problems during instantiation of the
12549      template.  */
12550   if (start_of_id)
12551     {
12552       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12553
12554       /* Reset the contents of the START_OF_ID token.  */
12555       token->type = CPP_TEMPLATE_ID;
12556       /* Retrieve any deferred checks.  Do not pop this access checks yet
12557          so the memory will not be reclaimed during token replacing below.  */
12558       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12559       token->u.tree_check_value->value = template_id;
12560       token->u.tree_check_value->checks = get_deferred_access_checks ();
12561       token->keyword = RID_MAX;
12562
12563       /* Purge all subsequent tokens.  */
12564       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12565
12566       /* ??? Can we actually assume that, if template_id ==
12567          error_mark_node, we will have issued a diagnostic to the
12568          user, as opposed to simply marking the tentative parse as
12569          failed?  */
12570       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12571         error_at (token->location, "parse error in template argument list");
12572     }
12573
12574   pop_deferring_access_checks ();
12575   return template_id;
12576 }
12577
12578 /* Parse a template-name.
12579
12580    template-name:
12581      identifier
12582
12583    The standard should actually say:
12584
12585    template-name:
12586      identifier
12587      operator-function-id
12588
12589    A defect report has been filed about this issue.
12590
12591    A conversion-function-id cannot be a template name because they cannot
12592    be part of a template-id. In fact, looking at this code:
12593
12594    a.operator K<int>()
12595
12596    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12597    It is impossible to call a templated conversion-function-id with an
12598    explicit argument list, since the only allowed template parameter is
12599    the type to which it is converting.
12600
12601    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12602    `template' keyword, in a construction like:
12603
12604      T::template f<3>()
12605
12606    In that case `f' is taken to be a template-name, even though there
12607    is no way of knowing for sure.
12608
12609    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12610    name refers to a set of overloaded functions, at least one of which
12611    is a template, or an IDENTIFIER_NODE with the name of the template,
12612    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12613    names are looked up inside uninstantiated templates.  */
12614
12615 static tree
12616 cp_parser_template_name (cp_parser* parser,
12617                          bool template_keyword_p,
12618                          bool check_dependency_p,
12619                          bool is_declaration,
12620                          enum tag_types tag_type,
12621                          bool *is_identifier)
12622 {
12623   tree identifier;
12624   tree decl;
12625   tree fns;
12626   cp_token *token = cp_lexer_peek_token (parser->lexer);
12627
12628   /* If the next token is `operator', then we have either an
12629      operator-function-id or a conversion-function-id.  */
12630   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12631     {
12632       /* We don't know whether we're looking at an
12633          operator-function-id or a conversion-function-id.  */
12634       cp_parser_parse_tentatively (parser);
12635       /* Try an operator-function-id.  */
12636       identifier = cp_parser_operator_function_id (parser);
12637       /* If that didn't work, try a conversion-function-id.  */
12638       if (!cp_parser_parse_definitely (parser))
12639         {
12640           cp_parser_error (parser, "expected template-name");
12641           return error_mark_node;
12642         }
12643     }
12644   /* Look for the identifier.  */
12645   else
12646     identifier = cp_parser_identifier (parser);
12647
12648   /* If we didn't find an identifier, we don't have a template-id.  */
12649   if (identifier == error_mark_node)
12650     return error_mark_node;
12651
12652   /* If the name immediately followed the `template' keyword, then it
12653      is a template-name.  However, if the next token is not `<', then
12654      we do not treat it as a template-name, since it is not being used
12655      as part of a template-id.  This enables us to handle constructs
12656      like:
12657
12658        template <typename T> struct S { S(); };
12659        template <typename T> S<T>::S();
12660
12661      correctly.  We would treat `S' as a template -- if it were `S<T>'
12662      -- but we do not if there is no `<'.  */
12663
12664   if (processing_template_decl
12665       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12666     {
12667       /* In a declaration, in a dependent context, we pretend that the
12668          "template" keyword was present in order to improve error
12669          recovery.  For example, given:
12670
12671            template <typename T> void f(T::X<int>);
12672
12673          we want to treat "X<int>" as a template-id.  */
12674       if (is_declaration
12675           && !template_keyword_p
12676           && parser->scope && TYPE_P (parser->scope)
12677           && check_dependency_p
12678           && dependent_scope_p (parser->scope)
12679           /* Do not do this for dtors (or ctors), since they never
12680              need the template keyword before their name.  */
12681           && !constructor_name_p (identifier, parser->scope))
12682         {
12683           cp_token_position start = 0;
12684
12685           /* Explain what went wrong.  */
12686           error_at (token->location, "non-template %qD used as template",
12687                     identifier);
12688           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12689                   parser->scope, identifier);
12690           /* If parsing tentatively, find the location of the "<" token.  */
12691           if (cp_parser_simulate_error (parser))
12692             start = cp_lexer_token_position (parser->lexer, true);
12693           /* Parse the template arguments so that we can issue error
12694              messages about them.  */
12695           cp_lexer_consume_token (parser->lexer);
12696           cp_parser_enclosed_template_argument_list (parser);
12697           /* Skip tokens until we find a good place from which to
12698              continue parsing.  */
12699           cp_parser_skip_to_closing_parenthesis (parser,
12700                                                  /*recovering=*/true,
12701                                                  /*or_comma=*/true,
12702                                                  /*consume_paren=*/false);
12703           /* If parsing tentatively, permanently remove the
12704              template argument list.  That will prevent duplicate
12705              error messages from being issued about the missing
12706              "template" keyword.  */
12707           if (start)
12708             cp_lexer_purge_tokens_after (parser->lexer, start);
12709           if (is_identifier)
12710             *is_identifier = true;
12711           return identifier;
12712         }
12713
12714       /* If the "template" keyword is present, then there is generally
12715          no point in doing name-lookup, so we just return IDENTIFIER.
12716          But, if the qualifying scope is non-dependent then we can
12717          (and must) do name-lookup normally.  */
12718       if (template_keyword_p
12719           && (!parser->scope
12720               || (TYPE_P (parser->scope)
12721                   && dependent_type_p (parser->scope))))
12722         return identifier;
12723     }
12724
12725   /* Look up the name.  */
12726   decl = cp_parser_lookup_name (parser, identifier,
12727                                 tag_type,
12728                                 /*is_template=*/true,
12729                                 /*is_namespace=*/false,
12730                                 check_dependency_p,
12731                                 /*ambiguous_decls=*/NULL,
12732                                 token->location);
12733
12734   /* If DECL is a template, then the name was a template-name.  */
12735   if (TREE_CODE (decl) == TEMPLATE_DECL)
12736     ;
12737   else
12738     {
12739       tree fn = NULL_TREE;
12740
12741       /* The standard does not explicitly indicate whether a name that
12742          names a set of overloaded declarations, some of which are
12743          templates, is a template-name.  However, such a name should
12744          be a template-name; otherwise, there is no way to form a
12745          template-id for the overloaded templates.  */
12746       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12747       if (TREE_CODE (fns) == OVERLOAD)
12748         for (fn = fns; fn; fn = OVL_NEXT (fn))
12749           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12750             break;
12751
12752       if (!fn)
12753         {
12754           /* The name does not name a template.  */
12755           cp_parser_error (parser, "expected template-name");
12756           return error_mark_node;
12757         }
12758     }
12759
12760   /* If DECL is dependent, and refers to a function, then just return
12761      its name; we will look it up again during template instantiation.  */
12762   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12763     {
12764       tree scope = ovl_scope (decl);
12765       if (TYPE_P (scope) && dependent_type_p (scope))
12766         return identifier;
12767     }
12768
12769   return decl;
12770 }
12771
12772 /* Parse a template-argument-list.
12773
12774    template-argument-list:
12775      template-argument ... [opt]
12776      template-argument-list , template-argument ... [opt]
12777
12778    Returns a TREE_VEC containing the arguments.  */
12779
12780 static tree
12781 cp_parser_template_argument_list (cp_parser* parser)
12782 {
12783   tree fixed_args[10];
12784   unsigned n_args = 0;
12785   unsigned alloced = 10;
12786   tree *arg_ary = fixed_args;
12787   tree vec;
12788   bool saved_in_template_argument_list_p;
12789   bool saved_ice_p;
12790   bool saved_non_ice_p;
12791
12792   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12793   parser->in_template_argument_list_p = true;
12794   /* Even if the template-id appears in an integral
12795      constant-expression, the contents of the argument list do
12796      not.  */
12797   saved_ice_p = parser->integral_constant_expression_p;
12798   parser->integral_constant_expression_p = false;
12799   saved_non_ice_p = parser->non_integral_constant_expression_p;
12800   parser->non_integral_constant_expression_p = false;
12801
12802   /* Parse the arguments.  */
12803   do
12804     {
12805       tree argument;
12806
12807       if (n_args)
12808         /* Consume the comma.  */
12809         cp_lexer_consume_token (parser->lexer);
12810
12811       /* Parse the template-argument.  */
12812       argument = cp_parser_template_argument (parser);
12813
12814       /* If the next token is an ellipsis, we're expanding a template
12815          argument pack. */
12816       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12817         {
12818           if (argument == error_mark_node)
12819             {
12820               cp_token *token = cp_lexer_peek_token (parser->lexer);
12821               error_at (token->location,
12822                         "expected parameter pack before %<...%>");
12823             }
12824           /* Consume the `...' token. */
12825           cp_lexer_consume_token (parser->lexer);
12826
12827           /* Make the argument into a TYPE_PACK_EXPANSION or
12828              EXPR_PACK_EXPANSION. */
12829           argument = make_pack_expansion (argument);
12830         }
12831
12832       if (n_args == alloced)
12833         {
12834           alloced *= 2;
12835
12836           if (arg_ary == fixed_args)
12837             {
12838               arg_ary = XNEWVEC (tree, alloced);
12839               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12840             }
12841           else
12842             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12843         }
12844       arg_ary[n_args++] = argument;
12845     }
12846   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12847
12848   vec = make_tree_vec (n_args);
12849
12850   while (n_args--)
12851     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12852
12853   if (arg_ary != fixed_args)
12854     free (arg_ary);
12855   parser->non_integral_constant_expression_p = saved_non_ice_p;
12856   parser->integral_constant_expression_p = saved_ice_p;
12857   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12858 #ifdef ENABLE_CHECKING
12859   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12860 #endif
12861   return vec;
12862 }
12863
12864 /* Parse a template-argument.
12865
12866    template-argument:
12867      assignment-expression
12868      type-id
12869      id-expression
12870
12871    The representation is that of an assignment-expression, type-id, or
12872    id-expression -- except that the qualified id-expression is
12873    evaluated, so that the value returned is either a DECL or an
12874    OVERLOAD.
12875
12876    Although the standard says "assignment-expression", it forbids
12877    throw-expressions or assignments in the template argument.
12878    Therefore, we use "conditional-expression" instead.  */
12879
12880 static tree
12881 cp_parser_template_argument (cp_parser* parser)
12882 {
12883   tree argument;
12884   bool template_p;
12885   bool address_p;
12886   bool maybe_type_id = false;
12887   cp_token *token = NULL, *argument_start_token = NULL;
12888   location_t loc = 0;
12889   cp_id_kind idk;
12890
12891   /* There's really no way to know what we're looking at, so we just
12892      try each alternative in order.
12893
12894        [temp.arg]
12895
12896        In a template-argument, an ambiguity between a type-id and an
12897        expression is resolved to a type-id, regardless of the form of
12898        the corresponding template-parameter.
12899
12900      Therefore, we try a type-id first.  */
12901   cp_parser_parse_tentatively (parser);
12902   argument = cp_parser_template_type_arg (parser);
12903   /* If there was no error parsing the type-id but the next token is a
12904      '>>', our behavior depends on which dialect of C++ we're
12905      parsing. In C++98, we probably found a typo for '> >'. But there
12906      are type-id which are also valid expressions. For instance:
12907
12908      struct X { int operator >> (int); };
12909      template <int V> struct Foo {};
12910      Foo<X () >> 5> r;
12911
12912      Here 'X()' is a valid type-id of a function type, but the user just
12913      wanted to write the expression "X() >> 5". Thus, we remember that we
12914      found a valid type-id, but we still try to parse the argument as an
12915      expression to see what happens. 
12916
12917      In C++0x, the '>>' will be considered two separate '>'
12918      tokens.  */
12919   if (!cp_parser_error_occurred (parser)
12920       && cxx_dialect == cxx98
12921       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12922     {
12923       maybe_type_id = true;
12924       cp_parser_abort_tentative_parse (parser);
12925     }
12926   else
12927     {
12928       /* If the next token isn't a `,' or a `>', then this argument wasn't
12929       really finished. This means that the argument is not a valid
12930       type-id.  */
12931       if (!cp_parser_next_token_ends_template_argument_p (parser))
12932         cp_parser_error (parser, "expected template-argument");
12933       /* If that worked, we're done.  */
12934       if (cp_parser_parse_definitely (parser))
12935         return argument;
12936     }
12937   /* We're still not sure what the argument will be.  */
12938   cp_parser_parse_tentatively (parser);
12939   /* Try a template.  */
12940   argument_start_token = cp_lexer_peek_token (parser->lexer);
12941   argument = cp_parser_id_expression (parser,
12942                                       /*template_keyword_p=*/false,
12943                                       /*check_dependency_p=*/true,
12944                                       &template_p,
12945                                       /*declarator_p=*/false,
12946                                       /*optional_p=*/false);
12947   /* If the next token isn't a `,' or a `>', then this argument wasn't
12948      really finished.  */
12949   if (!cp_parser_next_token_ends_template_argument_p (parser))
12950     cp_parser_error (parser, "expected template-argument");
12951   if (!cp_parser_error_occurred (parser))
12952     {
12953       /* Figure out what is being referred to.  If the id-expression
12954          was for a class template specialization, then we will have a
12955          TYPE_DECL at this point.  There is no need to do name lookup
12956          at this point in that case.  */
12957       if (TREE_CODE (argument) != TYPE_DECL)
12958         argument = cp_parser_lookup_name (parser, argument,
12959                                           none_type,
12960                                           /*is_template=*/template_p,
12961                                           /*is_namespace=*/false,
12962                                           /*check_dependency=*/true,
12963                                           /*ambiguous_decls=*/NULL,
12964                                           argument_start_token->location);
12965       if (TREE_CODE (argument) != TEMPLATE_DECL
12966           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12967         cp_parser_error (parser, "expected template-name");
12968     }
12969   if (cp_parser_parse_definitely (parser))
12970     return argument;
12971   /* It must be a non-type argument.  There permitted cases are given
12972      in [temp.arg.nontype]:
12973
12974      -- an integral constant-expression of integral or enumeration
12975         type; or
12976
12977      -- the name of a non-type template-parameter; or
12978
12979      -- the name of an object or function with external linkage...
12980
12981      -- the address of an object or function with external linkage...
12982
12983      -- a pointer to member...  */
12984   /* Look for a non-type template parameter.  */
12985   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12986     {
12987       cp_parser_parse_tentatively (parser);
12988       argument = cp_parser_primary_expression (parser,
12989                                                /*address_p=*/false,
12990                                                /*cast_p=*/false,
12991                                                /*template_arg_p=*/true,
12992                                                &idk);
12993       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12994           || !cp_parser_next_token_ends_template_argument_p (parser))
12995         cp_parser_simulate_error (parser);
12996       if (cp_parser_parse_definitely (parser))
12997         return argument;
12998     }
12999
13000   /* If the next token is "&", the argument must be the address of an
13001      object or function with external linkage.  */
13002   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13003   if (address_p)
13004     {
13005       loc = cp_lexer_peek_token (parser->lexer)->location;
13006       cp_lexer_consume_token (parser->lexer);
13007     }
13008   /* See if we might have an id-expression.  */
13009   token = cp_lexer_peek_token (parser->lexer);
13010   if (token->type == CPP_NAME
13011       || token->keyword == RID_OPERATOR
13012       || token->type == CPP_SCOPE
13013       || token->type == CPP_TEMPLATE_ID
13014       || token->type == CPP_NESTED_NAME_SPECIFIER)
13015     {
13016       cp_parser_parse_tentatively (parser);
13017       argument = cp_parser_primary_expression (parser,
13018                                                address_p,
13019                                                /*cast_p=*/false,
13020                                                /*template_arg_p=*/true,
13021                                                &idk);
13022       if (cp_parser_error_occurred (parser)
13023           || !cp_parser_next_token_ends_template_argument_p (parser))
13024         cp_parser_abort_tentative_parse (parser);
13025       else
13026         {
13027           tree probe;
13028
13029           if (TREE_CODE (argument) == INDIRECT_REF)
13030             {
13031               gcc_assert (REFERENCE_REF_P (argument));
13032               argument = TREE_OPERAND (argument, 0);
13033             }
13034
13035           /* If we're in a template, we represent a qualified-id referring
13036              to a static data member as a SCOPE_REF even if the scope isn't
13037              dependent so that we can check access control later.  */
13038           probe = argument;
13039           if (TREE_CODE (probe) == SCOPE_REF)
13040             probe = TREE_OPERAND (probe, 1);
13041           if (TREE_CODE (probe) == VAR_DECL)
13042             {
13043               /* A variable without external linkage might still be a
13044                  valid constant-expression, so no error is issued here
13045                  if the external-linkage check fails.  */
13046               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13047                 cp_parser_simulate_error (parser);
13048             }
13049           else if (is_overloaded_fn (argument))
13050             /* All overloaded functions are allowed; if the external
13051                linkage test does not pass, an error will be issued
13052                later.  */
13053             ;
13054           else if (address_p
13055                    && (TREE_CODE (argument) == OFFSET_REF
13056                        || TREE_CODE (argument) == SCOPE_REF))
13057             /* A pointer-to-member.  */
13058             ;
13059           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13060             ;
13061           else
13062             cp_parser_simulate_error (parser);
13063
13064           if (cp_parser_parse_definitely (parser))
13065             {
13066               if (address_p)
13067                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13068                                              tf_warning_or_error);
13069               return argument;
13070             }
13071         }
13072     }
13073   /* If the argument started with "&", there are no other valid
13074      alternatives at this point.  */
13075   if (address_p)
13076     {
13077       cp_parser_error (parser, "invalid non-type template argument");
13078       return error_mark_node;
13079     }
13080
13081   /* If the argument wasn't successfully parsed as a type-id followed
13082      by '>>', the argument can only be a constant expression now.
13083      Otherwise, we try parsing the constant-expression tentatively,
13084      because the argument could really be a type-id.  */
13085   if (maybe_type_id)
13086     cp_parser_parse_tentatively (parser);
13087   argument = cp_parser_constant_expression (parser,
13088                                             /*allow_non_constant_p=*/false,
13089                                             /*non_constant_p=*/NULL);
13090   argument = fold_non_dependent_expr (argument);
13091   if (!maybe_type_id)
13092     return argument;
13093   if (!cp_parser_next_token_ends_template_argument_p (parser))
13094     cp_parser_error (parser, "expected template-argument");
13095   if (cp_parser_parse_definitely (parser))
13096     return argument;
13097   /* We did our best to parse the argument as a non type-id, but that
13098      was the only alternative that matched (albeit with a '>' after
13099      it). We can assume it's just a typo from the user, and a
13100      diagnostic will then be issued.  */
13101   return cp_parser_template_type_arg (parser);
13102 }
13103
13104 /* Parse an explicit-instantiation.
13105
13106    explicit-instantiation:
13107      template declaration
13108
13109    Although the standard says `declaration', what it really means is:
13110
13111    explicit-instantiation:
13112      template decl-specifier-seq [opt] declarator [opt] ;
13113
13114    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13115    supposed to be allowed.  A defect report has been filed about this
13116    issue.
13117
13118    GNU Extension:
13119
13120    explicit-instantiation:
13121      storage-class-specifier template
13122        decl-specifier-seq [opt] declarator [opt] ;
13123      function-specifier template
13124        decl-specifier-seq [opt] declarator [opt] ;  */
13125
13126 static void
13127 cp_parser_explicit_instantiation (cp_parser* parser)
13128 {
13129   int declares_class_or_enum;
13130   cp_decl_specifier_seq decl_specifiers;
13131   tree extension_specifier = NULL_TREE;
13132
13133   timevar_push (TV_TEMPLATE_INST);
13134
13135   /* Look for an (optional) storage-class-specifier or
13136      function-specifier.  */
13137   if (cp_parser_allow_gnu_extensions_p (parser))
13138     {
13139       extension_specifier
13140         = cp_parser_storage_class_specifier_opt (parser);
13141       if (!extension_specifier)
13142         extension_specifier
13143           = cp_parser_function_specifier_opt (parser,
13144                                               /*decl_specs=*/NULL);
13145     }
13146
13147   /* Look for the `template' keyword.  */
13148   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13149   /* Let the front end know that we are processing an explicit
13150      instantiation.  */
13151   begin_explicit_instantiation ();
13152   /* [temp.explicit] says that we are supposed to ignore access
13153      control while processing explicit instantiation directives.  */
13154   push_deferring_access_checks (dk_no_check);
13155   /* Parse a decl-specifier-seq.  */
13156   cp_parser_decl_specifier_seq (parser,
13157                                 CP_PARSER_FLAGS_OPTIONAL,
13158                                 &decl_specifiers,
13159                                 &declares_class_or_enum);
13160   /* If there was exactly one decl-specifier, and it declared a class,
13161      and there's no declarator, then we have an explicit type
13162      instantiation.  */
13163   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13164     {
13165       tree type;
13166
13167       type = check_tag_decl (&decl_specifiers);
13168       /* Turn access control back on for names used during
13169          template instantiation.  */
13170       pop_deferring_access_checks ();
13171       if (type)
13172         do_type_instantiation (type, extension_specifier,
13173                                /*complain=*/tf_error);
13174     }
13175   else
13176     {
13177       cp_declarator *declarator;
13178       tree decl;
13179
13180       /* Parse the declarator.  */
13181       declarator
13182         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13183                                 /*ctor_dtor_or_conv_p=*/NULL,
13184                                 /*parenthesized_p=*/NULL,
13185                                 /*member_p=*/false);
13186       if (declares_class_or_enum & 2)
13187         cp_parser_check_for_definition_in_return_type (declarator,
13188                                                        decl_specifiers.type,
13189                                                        decl_specifiers.locations[ds_type_spec]);
13190       if (declarator != cp_error_declarator)
13191         {
13192           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13193             permerror (decl_specifiers.locations[ds_inline],
13194                        "explicit instantiation shall not use"
13195                        " %<inline%> specifier");
13196           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13197             permerror (decl_specifiers.locations[ds_constexpr],
13198                        "explicit instantiation shall not use"
13199                        " %<constexpr%> specifier");
13200
13201           decl = grokdeclarator (declarator, &decl_specifiers,
13202                                  NORMAL, 0, &decl_specifiers.attributes);
13203           /* Turn access control back on for names used during
13204              template instantiation.  */
13205           pop_deferring_access_checks ();
13206           /* Do the explicit instantiation.  */
13207           do_decl_instantiation (decl, extension_specifier);
13208         }
13209       else
13210         {
13211           pop_deferring_access_checks ();
13212           /* Skip the body of the explicit instantiation.  */
13213           cp_parser_skip_to_end_of_statement (parser);
13214         }
13215     }
13216   /* We're done with the instantiation.  */
13217   end_explicit_instantiation ();
13218
13219   cp_parser_consume_semicolon_at_end_of_statement (parser);
13220
13221   timevar_pop (TV_TEMPLATE_INST);
13222 }
13223
13224 /* Parse an explicit-specialization.
13225
13226    explicit-specialization:
13227      template < > declaration
13228
13229    Although the standard says `declaration', what it really means is:
13230
13231    explicit-specialization:
13232      template <> decl-specifier [opt] init-declarator [opt] ;
13233      template <> function-definition
13234      template <> explicit-specialization
13235      template <> template-declaration  */
13236
13237 static void
13238 cp_parser_explicit_specialization (cp_parser* parser)
13239 {
13240   bool need_lang_pop;
13241   cp_token *token = cp_lexer_peek_token (parser->lexer);
13242
13243   /* Look for the `template' keyword.  */
13244   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13245   /* Look for the `<'.  */
13246   cp_parser_require (parser, CPP_LESS, RT_LESS);
13247   /* Look for the `>'.  */
13248   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13249   /* We have processed another parameter list.  */
13250   ++parser->num_template_parameter_lists;
13251   /* [temp]
13252
13253      A template ... explicit specialization ... shall not have C
13254      linkage.  */
13255   if (current_lang_name == lang_name_c)
13256     {
13257       error_at (token->location, "template specialization with C linkage");
13258       /* Give it C++ linkage to avoid confusing other parts of the
13259          front end.  */
13260       push_lang_context (lang_name_cplusplus);
13261       need_lang_pop = true;
13262     }
13263   else
13264     need_lang_pop = false;
13265   /* Let the front end know that we are beginning a specialization.  */
13266   if (!begin_specialization ())
13267     {
13268       end_specialization ();
13269       return;
13270     }
13271
13272   /* If the next keyword is `template', we need to figure out whether
13273      or not we're looking a template-declaration.  */
13274   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13275     {
13276       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13277           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13278         cp_parser_template_declaration_after_export (parser,
13279                                                      /*member_p=*/false);
13280       else
13281         cp_parser_explicit_specialization (parser);
13282     }
13283   else
13284     /* Parse the dependent declaration.  */
13285     cp_parser_single_declaration (parser,
13286                                   /*checks=*/NULL,
13287                                   /*member_p=*/false,
13288                                   /*explicit_specialization_p=*/true,
13289                                   /*friend_p=*/NULL);
13290   /* We're done with the specialization.  */
13291   end_specialization ();
13292   /* For the erroneous case of a template with C linkage, we pushed an
13293      implicit C++ linkage scope; exit that scope now.  */
13294   if (need_lang_pop)
13295     pop_lang_context ();
13296   /* We're done with this parameter list.  */
13297   --parser->num_template_parameter_lists;
13298 }
13299
13300 /* Parse a type-specifier.
13301
13302    type-specifier:
13303      simple-type-specifier
13304      class-specifier
13305      enum-specifier
13306      elaborated-type-specifier
13307      cv-qualifier
13308
13309    GNU Extension:
13310
13311    type-specifier:
13312      __complex__
13313
13314    Returns a representation of the type-specifier.  For a
13315    class-specifier, enum-specifier, or elaborated-type-specifier, a
13316    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13317
13318    The parser flags FLAGS is used to control type-specifier parsing.
13319
13320    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13321    in a decl-specifier-seq.
13322
13323    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13324    class-specifier, enum-specifier, or elaborated-type-specifier, then
13325    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13326    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13327    zero.
13328
13329    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13330    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13331    is set to FALSE.  */
13332
13333 static tree
13334 cp_parser_type_specifier (cp_parser* parser,
13335                           cp_parser_flags flags,
13336                           cp_decl_specifier_seq *decl_specs,
13337                           bool is_declaration,
13338                           int* declares_class_or_enum,
13339                           bool* is_cv_qualifier)
13340 {
13341   tree type_spec = NULL_TREE;
13342   cp_token *token;
13343   enum rid keyword;
13344   cp_decl_spec ds = ds_last;
13345
13346   /* Assume this type-specifier does not declare a new type.  */
13347   if (declares_class_or_enum)
13348     *declares_class_or_enum = 0;
13349   /* And that it does not specify a cv-qualifier.  */
13350   if (is_cv_qualifier)
13351     *is_cv_qualifier = false;
13352   /* Peek at the next token.  */
13353   token = cp_lexer_peek_token (parser->lexer);
13354
13355   /* If we're looking at a keyword, we can use that to guide the
13356      production we choose.  */
13357   keyword = token->keyword;
13358   switch (keyword)
13359     {
13360     case RID_ENUM:
13361       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13362         goto elaborated_type_specifier;
13363
13364       /* Look for the enum-specifier.  */
13365       type_spec = cp_parser_enum_specifier (parser);
13366       /* If that worked, we're done.  */
13367       if (type_spec)
13368         {
13369           if (declares_class_or_enum)
13370             *declares_class_or_enum = 2;
13371           if (decl_specs)
13372             cp_parser_set_decl_spec_type (decl_specs,
13373                                           type_spec,
13374                                           token->location,
13375                                           /*type_definition_p=*/true);
13376           return type_spec;
13377         }
13378       else
13379         goto elaborated_type_specifier;
13380
13381       /* Any of these indicate either a class-specifier, or an
13382          elaborated-type-specifier.  */
13383     case RID_CLASS:
13384     case RID_STRUCT:
13385     case RID_UNION:
13386       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13387         goto elaborated_type_specifier;
13388
13389       /* Parse tentatively so that we can back up if we don't find a
13390          class-specifier.  */
13391       cp_parser_parse_tentatively (parser);
13392       /* Look for the class-specifier.  */
13393       type_spec = cp_parser_class_specifier (parser);
13394       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13395       /* If that worked, we're done.  */
13396       if (cp_parser_parse_definitely (parser))
13397         {
13398           if (declares_class_or_enum)
13399             *declares_class_or_enum = 2;
13400           if (decl_specs)
13401             cp_parser_set_decl_spec_type (decl_specs,
13402                                           type_spec,
13403                                           token->location,
13404                                           /*type_definition_p=*/true);
13405           return type_spec;
13406         }
13407
13408       /* Fall through.  */
13409     elaborated_type_specifier:
13410       /* We're declaring (not defining) a class or enum.  */
13411       if (declares_class_or_enum)
13412         *declares_class_or_enum = 1;
13413
13414       /* Fall through.  */
13415     case RID_TYPENAME:
13416       /* Look for an elaborated-type-specifier.  */
13417       type_spec
13418         = (cp_parser_elaborated_type_specifier
13419            (parser,
13420             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13421             is_declaration));
13422       if (decl_specs)
13423         cp_parser_set_decl_spec_type (decl_specs,
13424                                       type_spec,
13425                                       token->location,
13426                                       /*type_definition_p=*/false);
13427       return type_spec;
13428
13429     case RID_CONST:
13430       ds = ds_const;
13431       if (is_cv_qualifier)
13432         *is_cv_qualifier = true;
13433       break;
13434
13435     case RID_VOLATILE:
13436       ds = ds_volatile;
13437       if (is_cv_qualifier)
13438         *is_cv_qualifier = true;
13439       break;
13440
13441     case RID_RESTRICT:
13442       ds = ds_restrict;
13443       if (is_cv_qualifier)
13444         *is_cv_qualifier = true;
13445       break;
13446
13447     case RID_COMPLEX:
13448       /* The `__complex__' keyword is a GNU extension.  */
13449       ds = ds_complex;
13450       break;
13451
13452     default:
13453       break;
13454     }
13455
13456   /* Handle simple keywords.  */
13457   if (ds != ds_last)
13458     {
13459       if (decl_specs)
13460         {
13461           set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13462           decl_specs->any_specifiers_p = true;
13463         }
13464       return cp_lexer_consume_token (parser->lexer)->u.value;
13465     }
13466
13467   /* If we do not already have a type-specifier, assume we are looking
13468      at a simple-type-specifier.  */
13469   type_spec = cp_parser_simple_type_specifier (parser,
13470                                                decl_specs,
13471                                                flags);
13472
13473   /* If we didn't find a type-specifier, and a type-specifier was not
13474      optional in this context, issue an error message.  */
13475   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13476     {
13477       cp_parser_error (parser, "expected type specifier");
13478       return error_mark_node;
13479     }
13480
13481   return type_spec;
13482 }
13483
13484 /* Parse a simple-type-specifier.
13485
13486    simple-type-specifier:
13487      :: [opt] nested-name-specifier [opt] type-name
13488      :: [opt] nested-name-specifier template template-id
13489      char
13490      wchar_t
13491      bool
13492      short
13493      int
13494      long
13495      signed
13496      unsigned
13497      float
13498      double
13499      void
13500
13501    C++0x Extension:
13502
13503    simple-type-specifier:
13504      auto
13505      decltype ( expression )   
13506      char16_t
13507      char32_t
13508      __underlying_type ( type-id )
13509
13510    GNU Extension:
13511
13512    simple-type-specifier:
13513      __int128
13514      __typeof__ unary-expression
13515      __typeof__ ( type-id )
13516
13517    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13518    appropriately updated.  */
13519
13520 static tree
13521 cp_parser_simple_type_specifier (cp_parser* parser,
13522                                  cp_decl_specifier_seq *decl_specs,
13523                                  cp_parser_flags flags)
13524 {
13525   tree type = NULL_TREE;
13526   cp_token *token;
13527
13528   /* Peek at the next token.  */
13529   token = cp_lexer_peek_token (parser->lexer);
13530
13531   /* If we're looking at a keyword, things are easy.  */
13532   switch (token->keyword)
13533     {
13534     case RID_CHAR:
13535       if (decl_specs)
13536         decl_specs->explicit_char_p = true;
13537       type = char_type_node;
13538       break;
13539     case RID_CHAR16:
13540       type = char16_type_node;
13541       break;
13542     case RID_CHAR32:
13543       type = char32_type_node;
13544       break;
13545     case RID_WCHAR:
13546       type = wchar_type_node;
13547       break;
13548     case RID_BOOL:
13549       type = boolean_type_node;
13550       break;
13551     case RID_SHORT:
13552       set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13553       type = short_integer_type_node;
13554       break;
13555     case RID_INT:
13556       if (decl_specs)
13557         decl_specs->explicit_int_p = true;
13558       type = integer_type_node;
13559       break;
13560     case RID_INT128:
13561       if (!int128_integer_type_node)
13562         break;
13563       if (decl_specs)
13564         decl_specs->explicit_int128_p = true;
13565       type = int128_integer_type_node;
13566       break;
13567     case RID_LONG:
13568       if (decl_specs)
13569         set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13570       type = long_integer_type_node;
13571       break;
13572     case RID_SIGNED:
13573       set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13574       type = integer_type_node;
13575       break;
13576     case RID_UNSIGNED:
13577       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13578       type = unsigned_type_node;
13579       break;
13580     case RID_FLOAT:
13581       type = float_type_node;
13582       break;
13583     case RID_DOUBLE:
13584       type = double_type_node;
13585       break;
13586     case RID_VOID:
13587       type = void_type_node;
13588       break;
13589       
13590     case RID_AUTO:
13591       maybe_warn_cpp0x (CPP0X_AUTO);
13592       type = make_auto ();
13593       break;
13594
13595     case RID_DECLTYPE:
13596       /* Since DR 743, decltype can either be a simple-type-specifier by
13597          itself or begin a nested-name-specifier.  Parsing it will replace
13598          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13599          handling below decide what to do.  */
13600       cp_parser_decltype (parser);
13601       cp_lexer_set_token_position (parser->lexer, token);
13602       break;
13603
13604     case RID_TYPEOF:
13605       /* Consume the `typeof' token.  */
13606       cp_lexer_consume_token (parser->lexer);
13607       /* Parse the operand to `typeof'.  */
13608       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13609       /* If it is not already a TYPE, take its type.  */
13610       if (!TYPE_P (type))
13611         type = finish_typeof (type);
13612
13613       if (decl_specs)
13614         cp_parser_set_decl_spec_type (decl_specs, type,
13615                                       token->location,
13616                                       /*type_definition_p=*/false);
13617
13618       return type;
13619
13620     case RID_UNDERLYING_TYPE:
13621       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13622       if (decl_specs)
13623         cp_parser_set_decl_spec_type (decl_specs, type,
13624                                       token->location,
13625                                       /*type_definition_p=*/false);
13626
13627       return type;
13628
13629     case RID_BASES:
13630     case RID_DIRECT_BASES:
13631       type = cp_parser_trait_expr (parser, token->keyword);
13632       if (decl_specs)
13633        cp_parser_set_decl_spec_type (decl_specs, type,
13634                                      token->location,
13635                                      /*type_definition_p=*/false);
13636       return type;
13637     default:
13638       break;
13639     }
13640
13641   /* If token is an already-parsed decltype not followed by ::,
13642      it's a simple-type-specifier.  */
13643   if (token->type == CPP_DECLTYPE
13644       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13645     {
13646       type = token->u.value;
13647       if (decl_specs)
13648         cp_parser_set_decl_spec_type (decl_specs, type,
13649                                       token->location,
13650                                       /*type_definition_p=*/false);
13651       cp_lexer_consume_token (parser->lexer);
13652       return type;
13653     }
13654
13655   /* If the type-specifier was for a built-in type, we're done.  */
13656   if (type)
13657     {
13658       /* Record the type.  */
13659       if (decl_specs
13660           && (token->keyword != RID_SIGNED
13661               && token->keyword != RID_UNSIGNED
13662               && token->keyword != RID_SHORT
13663               && token->keyword != RID_LONG))
13664         cp_parser_set_decl_spec_type (decl_specs,
13665                                       type,
13666                                       token->location,
13667                                       /*type_definition_p=*/false);
13668       if (decl_specs)
13669         decl_specs->any_specifiers_p = true;
13670
13671       /* Consume the token.  */
13672       cp_lexer_consume_token (parser->lexer);
13673
13674       /* There is no valid C++ program where a non-template type is
13675          followed by a "<".  That usually indicates that the user thought
13676          that the type was a template.  */
13677       cp_parser_check_for_invalid_template_id (parser, type, none_type,
13678                                                token->location);
13679
13680       return TYPE_NAME (type);
13681     }
13682
13683   /* The type-specifier must be a user-defined type.  */
13684   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13685     {
13686       bool qualified_p;
13687       bool global_p;
13688
13689       /* Don't gobble tokens or issue error messages if this is an
13690          optional type-specifier.  */
13691       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13692         cp_parser_parse_tentatively (parser);
13693
13694       /* Look for the optional `::' operator.  */
13695       global_p
13696         = (cp_parser_global_scope_opt (parser,
13697                                        /*current_scope_valid_p=*/false)
13698            != NULL_TREE);
13699       /* Look for the nested-name specifier.  */
13700       qualified_p
13701         = (cp_parser_nested_name_specifier_opt (parser,
13702                                                 /*typename_keyword_p=*/false,
13703                                                 /*check_dependency_p=*/true,
13704                                                 /*type_p=*/false,
13705                                                 /*is_declaration=*/false)
13706            != NULL_TREE);
13707       token = cp_lexer_peek_token (parser->lexer);
13708       /* If we have seen a nested-name-specifier, and the next token
13709          is `template', then we are using the template-id production.  */
13710       if (parser->scope
13711           && cp_parser_optional_template_keyword (parser))
13712         {
13713           /* Look for the template-id.  */
13714           type = cp_parser_template_id (parser,
13715                                         /*template_keyword_p=*/true,
13716                                         /*check_dependency_p=*/true,
13717                                         none_type,
13718                                         /*is_declaration=*/false);
13719           /* If the template-id did not name a type, we are out of
13720              luck.  */
13721           if (TREE_CODE (type) != TYPE_DECL)
13722             {
13723               cp_parser_error (parser, "expected template-id for type");
13724               type = NULL_TREE;
13725             }
13726         }
13727       /* Otherwise, look for a type-name.  */
13728       else
13729         type = cp_parser_type_name (parser);
13730       /* Keep track of all name-lookups performed in class scopes.  */
13731       if (type
13732           && !global_p
13733           && !qualified_p
13734           && TREE_CODE (type) == TYPE_DECL
13735           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13736         maybe_note_name_used_in_class (DECL_NAME (type), type);
13737       /* If it didn't work out, we don't have a TYPE.  */
13738       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13739           && !cp_parser_parse_definitely (parser))
13740         type = NULL_TREE;
13741       if (type && decl_specs)
13742         cp_parser_set_decl_spec_type (decl_specs, type,
13743                                       token->location,
13744                                       /*type_definition_p=*/false);
13745     }
13746
13747   /* If we didn't get a type-name, issue an error message.  */
13748   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13749     {
13750       cp_parser_error (parser, "expected type-name");
13751       return error_mark_node;
13752     }
13753
13754   if (type && type != error_mark_node)
13755     {
13756       /* See if TYPE is an Objective-C type, and if so, parse and
13757          accept any protocol references following it.  Do this before
13758          the cp_parser_check_for_invalid_template_id() call, because
13759          Objective-C types can be followed by '<...>' which would
13760          enclose protocol names rather than template arguments, and so
13761          everything is fine.  */
13762       if (c_dialect_objc () && !parser->scope
13763           && (objc_is_id (type) || objc_is_class_name (type)))
13764         {
13765           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13766           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13767
13768           /* Clobber the "unqualified" type previously entered into
13769              DECL_SPECS with the new, improved protocol-qualified version.  */
13770           if (decl_specs)
13771             decl_specs->type = qual_type;
13772
13773           return qual_type;
13774         }
13775
13776       /* There is no valid C++ program where a non-template type is
13777          followed by a "<".  That usually indicates that the user
13778          thought that the type was a template.  */
13779       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13780                                                none_type,
13781                                                token->location);
13782     }
13783
13784   return type;
13785 }
13786
13787 /* Parse a type-name.
13788
13789    type-name:
13790      class-name
13791      enum-name
13792      typedef-name
13793      simple-template-id [in c++0x]
13794
13795    enum-name:
13796      identifier
13797
13798    typedef-name:
13799      identifier
13800
13801    Returns a TYPE_DECL for the type.  */
13802
13803 static tree
13804 cp_parser_type_name (cp_parser* parser)
13805 {
13806   tree type_decl;
13807
13808   /* We can't know yet whether it is a class-name or not.  */
13809   cp_parser_parse_tentatively (parser);
13810   /* Try a class-name.  */
13811   type_decl = cp_parser_class_name (parser,
13812                                     /*typename_keyword_p=*/false,
13813                                     /*template_keyword_p=*/false,
13814                                     none_type,
13815                                     /*check_dependency_p=*/true,
13816                                     /*class_head_p=*/false,
13817                                     /*is_declaration=*/false);
13818   /* If it's not a class-name, keep looking.  */
13819   if (!cp_parser_parse_definitely (parser))
13820     {
13821       if (cxx_dialect < cxx0x)
13822         /* It must be a typedef-name or an enum-name.  */
13823         return cp_parser_nonclass_name (parser);
13824
13825       cp_parser_parse_tentatively (parser);
13826       /* It is either a simple-template-id representing an
13827          instantiation of an alias template...  */
13828       type_decl = cp_parser_template_id (parser,
13829                                          /*template_keyword_p=*/false,
13830                                          /*check_dependency_p=*/false,
13831                                          none_type,
13832                                          /*is_declaration=*/false);
13833       /* Note that this must be an instantiation of an alias template
13834          because [temp.names]/6 says:
13835          
13836              A template-id that names an alias template specialization
13837              is a type-name.
13838
13839          Whereas [temp.names]/7 says:
13840          
13841              A simple-template-id that names a class template
13842              specialization is a class-name.  */
13843       if (type_decl != NULL_TREE
13844           && TREE_CODE (type_decl) == TYPE_DECL
13845           && TYPE_DECL_ALIAS_P (type_decl))
13846         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13847       else
13848         cp_parser_simulate_error (parser);
13849
13850       if (!cp_parser_parse_definitely (parser))
13851         /* ... Or a typedef-name or an enum-name.  */
13852         return cp_parser_nonclass_name (parser);
13853     }
13854
13855   return type_decl;
13856 }
13857
13858 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13859
13860    enum-name:
13861      identifier
13862
13863    typedef-name:
13864      identifier
13865
13866    Returns a TYPE_DECL for the type.  */
13867
13868 static tree
13869 cp_parser_nonclass_name (cp_parser* parser)
13870 {
13871   tree type_decl;
13872   tree identifier;
13873
13874   cp_token *token = cp_lexer_peek_token (parser->lexer);
13875   identifier = cp_parser_identifier (parser);
13876   if (identifier == error_mark_node)
13877     return error_mark_node;
13878
13879   /* Look up the type-name.  */
13880   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13881
13882   if (TREE_CODE (type_decl) == USING_DECL)
13883     {
13884       if (!DECL_DEPENDENT_P (type_decl))
13885         type_decl = strip_using_decl (type_decl);
13886       else if (USING_DECL_TYPENAME_P (type_decl))
13887         {
13888           /* We have found a type introduced by a using
13889              declaration at class scope that refers to a dependent
13890              type.
13891              
13892              using typename :: [opt] nested-name-specifier unqualified-id ;
13893           */
13894           type_decl = make_typename_type (TREE_TYPE (type_decl),
13895                                           DECL_NAME (type_decl),
13896                                           typename_type, tf_error);
13897           if (type_decl != error_mark_node)
13898             type_decl = TYPE_NAME (type_decl);
13899         }
13900     }
13901   
13902   if (TREE_CODE (type_decl) != TYPE_DECL
13903       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13904     {
13905       /* See if this is an Objective-C type.  */
13906       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13907       tree type = objc_get_protocol_qualified_type (identifier, protos);
13908       if (type)
13909         type_decl = TYPE_NAME (type);
13910     }
13911
13912   /* Issue an error if we did not find a type-name.  */
13913   if (TREE_CODE (type_decl) != TYPE_DECL
13914       /* In Objective-C, we have the complication that class names are
13915          normally type names and start declarations (eg, the
13916          "NSObject" in "NSObject *object;"), but can be used in an
13917          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13918          is an expression.  So, a classname followed by a dot is not a
13919          valid type-name.  */
13920       || (objc_is_class_name (TREE_TYPE (type_decl))
13921           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13922     {
13923       if (!cp_parser_simulate_error (parser))
13924         cp_parser_name_lookup_error (parser, identifier, type_decl,
13925                                      NLE_TYPE, token->location);
13926       return error_mark_node;
13927     }
13928   /* Remember that the name was used in the definition of the
13929      current class so that we can check later to see if the
13930      meaning would have been different after the class was
13931      entirely defined.  */
13932   else if (type_decl != error_mark_node
13933            && !parser->scope)
13934     maybe_note_name_used_in_class (identifier, type_decl);
13935   
13936   return type_decl;
13937 }
13938
13939 /* Parse an elaborated-type-specifier.  Note that the grammar given
13940    here incorporates the resolution to DR68.
13941
13942    elaborated-type-specifier:
13943      class-key :: [opt] nested-name-specifier [opt] identifier
13944      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13945      enum-key :: [opt] nested-name-specifier [opt] identifier
13946      typename :: [opt] nested-name-specifier identifier
13947      typename :: [opt] nested-name-specifier template [opt]
13948        template-id
13949
13950    GNU extension:
13951
13952    elaborated-type-specifier:
13953      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13954      class-key attributes :: [opt] nested-name-specifier [opt]
13955                template [opt] template-id
13956      enum attributes :: [opt] nested-name-specifier [opt] identifier
13957
13958    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13959    declared `friend'.  If IS_DECLARATION is TRUE, then this
13960    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13961    something is being declared.
13962
13963    Returns the TYPE specified.  */
13964
13965 static tree
13966 cp_parser_elaborated_type_specifier (cp_parser* parser,
13967                                      bool is_friend,
13968                                      bool is_declaration)
13969 {
13970   enum tag_types tag_type;
13971   tree identifier;
13972   tree type = NULL_TREE;
13973   tree attributes = NULL_TREE;
13974   tree globalscope;
13975   cp_token *token = NULL;
13976
13977   /* See if we're looking at the `enum' keyword.  */
13978   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13979     {
13980       /* Consume the `enum' token.  */
13981       cp_lexer_consume_token (parser->lexer);
13982       /* Remember that it's an enumeration type.  */
13983       tag_type = enum_type;
13984       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13985          enums) is used here.  */
13986       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13987           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13988         {
13989             pedwarn (input_location, 0, "elaborated-type-specifier "
13990                       "for a scoped enum must not use the %<%D%> keyword",
13991                       cp_lexer_peek_token (parser->lexer)->u.value);
13992           /* Consume the `struct' or `class' and parse it anyway.  */
13993           cp_lexer_consume_token (parser->lexer);
13994         }
13995       /* Parse the attributes.  */
13996       attributes = cp_parser_attributes_opt (parser);
13997     }
13998   /* Or, it might be `typename'.  */
13999   else if (cp_lexer_next_token_is_keyword (parser->lexer,
14000                                            RID_TYPENAME))
14001     {
14002       /* Consume the `typename' token.  */
14003       cp_lexer_consume_token (parser->lexer);
14004       /* Remember that it's a `typename' type.  */
14005       tag_type = typename_type;
14006     }
14007   /* Otherwise it must be a class-key.  */
14008   else
14009     {
14010       tag_type = cp_parser_class_key (parser);
14011       if (tag_type == none_type)
14012         return error_mark_node;
14013       /* Parse the attributes.  */
14014       attributes = cp_parser_attributes_opt (parser);
14015     }
14016
14017   /* Look for the `::' operator.  */
14018   globalscope =  cp_parser_global_scope_opt (parser,
14019                                              /*current_scope_valid_p=*/false);
14020   /* Look for the nested-name-specifier.  */
14021   if (tag_type == typename_type && !globalscope)
14022     {
14023       if (!cp_parser_nested_name_specifier (parser,
14024                                            /*typename_keyword_p=*/true,
14025                                            /*check_dependency_p=*/true,
14026                                            /*type_p=*/true,
14027                                             is_declaration))
14028         return error_mark_node;
14029     }
14030   else
14031     /* Even though `typename' is not present, the proposed resolution
14032        to Core Issue 180 says that in `class A<T>::B', `B' should be
14033        considered a type-name, even if `A<T>' is dependent.  */
14034     cp_parser_nested_name_specifier_opt (parser,
14035                                          /*typename_keyword_p=*/true,
14036                                          /*check_dependency_p=*/true,
14037                                          /*type_p=*/true,
14038                                          is_declaration);
14039  /* For everything but enumeration types, consider a template-id.
14040     For an enumeration type, consider only a plain identifier.  */
14041   if (tag_type != enum_type)
14042     {
14043       bool template_p = false;
14044       tree decl;
14045
14046       /* Allow the `template' keyword.  */
14047       template_p = cp_parser_optional_template_keyword (parser);
14048       /* If we didn't see `template', we don't know if there's a
14049          template-id or not.  */
14050       if (!template_p)
14051         cp_parser_parse_tentatively (parser);
14052       /* Parse the template-id.  */
14053       token = cp_lexer_peek_token (parser->lexer);
14054       decl = cp_parser_template_id (parser, template_p,
14055                                     /*check_dependency_p=*/true,
14056                                     tag_type,
14057                                     is_declaration);
14058       /* If we didn't find a template-id, look for an ordinary
14059          identifier.  */
14060       if (!template_p && !cp_parser_parse_definitely (parser))
14061         ;
14062       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14063          in effect, then we must assume that, upon instantiation, the
14064          template will correspond to a class.  */
14065       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14066                && tag_type == typename_type)
14067         type = make_typename_type (parser->scope, decl,
14068                                    typename_type,
14069                                    /*complain=*/tf_error);
14070       /* If the `typename' keyword is in effect and DECL is not a type
14071          decl. Then type is non existant.   */
14072       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14073         type = NULL_TREE; 
14074       else 
14075         type = check_elaborated_type_specifier (tag_type, decl,
14076                                                 /*allow_template_p=*/true);
14077     }
14078
14079   if (!type)
14080     {
14081       token = cp_lexer_peek_token (parser->lexer);
14082       identifier = cp_parser_identifier (parser);
14083
14084       if (identifier == error_mark_node)
14085         {
14086           parser->scope = NULL_TREE;
14087           return error_mark_node;
14088         }
14089
14090       /* For a `typename', we needn't call xref_tag.  */
14091       if (tag_type == typename_type
14092           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14093         return cp_parser_make_typename_type (parser, parser->scope,
14094                                              identifier,
14095                                              token->location);
14096       /* Look up a qualified name in the usual way.  */
14097       if (parser->scope)
14098         {
14099           tree decl;
14100           tree ambiguous_decls;
14101
14102           decl = cp_parser_lookup_name (parser, identifier,
14103                                         tag_type,
14104                                         /*is_template=*/false,
14105                                         /*is_namespace=*/false,
14106                                         /*check_dependency=*/true,
14107                                         &ambiguous_decls,
14108                                         token->location);
14109
14110           /* If the lookup was ambiguous, an error will already have been
14111              issued.  */
14112           if (ambiguous_decls)
14113             return error_mark_node;
14114
14115           /* If we are parsing friend declaration, DECL may be a
14116              TEMPLATE_DECL tree node here.  However, we need to check
14117              whether this TEMPLATE_DECL results in valid code.  Consider
14118              the following example:
14119
14120                namespace N {
14121                  template <class T> class C {};
14122                }
14123                class X {
14124                  template <class T> friend class N::C; // #1, valid code
14125                };
14126                template <class T> class Y {
14127                  friend class N::C;                    // #2, invalid code
14128                };
14129
14130              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14131              name lookup of `N::C'.  We see that friend declaration must
14132              be template for the code to be valid.  Note that
14133              processing_template_decl does not work here since it is
14134              always 1 for the above two cases.  */
14135
14136           decl = (cp_parser_maybe_treat_template_as_class
14137                   (decl, /*tag_name_p=*/is_friend
14138                          && parser->num_template_parameter_lists));
14139
14140           if (TREE_CODE (decl) != TYPE_DECL)
14141             {
14142               cp_parser_diagnose_invalid_type_name (parser,
14143                                                     parser->scope,
14144                                                     identifier,
14145                                                     token->location);
14146               return error_mark_node;
14147             }
14148
14149           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14150             {
14151               bool allow_template = (parser->num_template_parameter_lists
14152                                       || DECL_SELF_REFERENCE_P (decl));
14153               type = check_elaborated_type_specifier (tag_type, decl, 
14154                                                       allow_template);
14155
14156               if (type == error_mark_node)
14157                 return error_mark_node;
14158             }
14159
14160           /* Forward declarations of nested types, such as
14161
14162                class C1::C2;
14163                class C1::C2::C3;
14164
14165              are invalid unless all components preceding the final '::'
14166              are complete.  If all enclosing types are complete, these
14167              declarations become merely pointless.
14168
14169              Invalid forward declarations of nested types are errors
14170              caught elsewhere in parsing.  Those that are pointless arrive
14171              here.  */
14172
14173           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14174               && !is_friend && !processing_explicit_instantiation)
14175             warning (0, "declaration %qD does not declare anything", decl);
14176
14177           type = TREE_TYPE (decl);
14178         }
14179       else
14180         {
14181           /* An elaborated-type-specifier sometimes introduces a new type and
14182              sometimes names an existing type.  Normally, the rule is that it
14183              introduces a new type only if there is not an existing type of
14184              the same name already in scope.  For example, given:
14185
14186                struct S {};
14187                void f() { struct S s; }
14188
14189              the `struct S' in the body of `f' is the same `struct S' as in
14190              the global scope; the existing definition is used.  However, if
14191              there were no global declaration, this would introduce a new
14192              local class named `S'.
14193
14194              An exception to this rule applies to the following code:
14195
14196                namespace N { struct S; }
14197
14198              Here, the elaborated-type-specifier names a new type
14199              unconditionally; even if there is already an `S' in the
14200              containing scope this declaration names a new type.
14201              This exception only applies if the elaborated-type-specifier
14202              forms the complete declaration:
14203
14204                [class.name]
14205
14206                A declaration consisting solely of `class-key identifier ;' is
14207                either a redeclaration of the name in the current scope or a
14208                forward declaration of the identifier as a class name.  It
14209                introduces the name into the current scope.
14210
14211              We are in this situation precisely when the next token is a `;'.
14212
14213              An exception to the exception is that a `friend' declaration does
14214              *not* name a new type; i.e., given:
14215
14216                struct S { friend struct T; };
14217
14218              `T' is not a new type in the scope of `S'.
14219
14220              Also, `new struct S' or `sizeof (struct S)' never results in the
14221              definition of a new type; a new type can only be declared in a
14222              declaration context.  */
14223
14224           tag_scope ts;
14225           bool template_p;
14226
14227           if (is_friend)
14228             /* Friends have special name lookup rules.  */
14229             ts = ts_within_enclosing_non_class;
14230           else if (is_declaration
14231                    && cp_lexer_next_token_is (parser->lexer,
14232                                               CPP_SEMICOLON))
14233             /* This is a `class-key identifier ;' */
14234             ts = ts_current;
14235           else
14236             ts = ts_global;
14237
14238           template_p =
14239             (parser->num_template_parameter_lists
14240              && (cp_parser_next_token_starts_class_definition_p (parser)
14241                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14242           /* An unqualified name was used to reference this type, so
14243              there were no qualifying templates.  */
14244           if (!cp_parser_check_template_parameters (parser,
14245                                                     /*num_templates=*/0,
14246                                                     token->location,
14247                                                     /*declarator=*/NULL))
14248             return error_mark_node;
14249           type = xref_tag (tag_type, identifier, ts, template_p);
14250         }
14251     }
14252
14253   if (type == error_mark_node)
14254     return error_mark_node;
14255
14256   /* Allow attributes on forward declarations of classes.  */
14257   if (attributes)
14258     {
14259       if (TREE_CODE (type) == TYPENAME_TYPE)
14260         warning (OPT_Wattributes,
14261                  "attributes ignored on uninstantiated type");
14262       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14263                && ! processing_explicit_instantiation)
14264         warning (OPT_Wattributes,
14265                  "attributes ignored on template instantiation");
14266       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14267         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14268       else
14269         warning (OPT_Wattributes,
14270                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14271     }
14272
14273   if (tag_type != enum_type)
14274     {
14275       /* Indicate whether this class was declared as a `class' or as a
14276          `struct'.  */
14277       if (TREE_CODE (type) == RECORD_TYPE)
14278         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14279       cp_parser_check_class_key (tag_type, type);
14280     }
14281
14282   /* A "<" cannot follow an elaborated type specifier.  If that
14283      happens, the user was probably trying to form a template-id.  */
14284   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14285                                            token->location);
14286
14287   return type;
14288 }
14289
14290 /* Parse an enum-specifier.
14291
14292    enum-specifier:
14293      enum-head { enumerator-list [opt] }
14294      enum-head { enumerator-list , } [C++0x]
14295
14296    enum-head:
14297      enum-key identifier [opt] enum-base [opt]
14298      enum-key nested-name-specifier identifier enum-base [opt]
14299
14300    enum-key:
14301      enum
14302      enum class   [C++0x]
14303      enum struct  [C++0x]
14304
14305    enum-base:   [C++0x]
14306      : type-specifier-seq
14307
14308    opaque-enum-specifier:
14309      enum-key identifier enum-base [opt] ;
14310
14311    GNU Extensions:
14312      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14313        { enumerator-list [opt] }attributes[opt]
14314      enum-key attributes[opt] identifier [opt] enum-base [opt]
14315        { enumerator-list, }attributes[opt] [C++0x]
14316
14317    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14318    if the token stream isn't an enum-specifier after all.  */
14319
14320 static tree
14321 cp_parser_enum_specifier (cp_parser* parser)
14322 {
14323   tree identifier;
14324   tree type = NULL_TREE;
14325   tree prev_scope;
14326   tree nested_name_specifier = NULL_TREE;
14327   tree attributes;
14328   bool scoped_enum_p = false;
14329   bool has_underlying_type = false;
14330   bool nested_being_defined = false;
14331   bool new_value_list = false;
14332   bool is_new_type = false;
14333   bool is_anonymous = false;
14334   tree underlying_type = NULL_TREE;
14335   cp_token *type_start_token = NULL;
14336   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14337
14338   parser->colon_corrects_to_scope_p = false;
14339
14340   /* Parse tentatively so that we can back up if we don't find a
14341      enum-specifier.  */
14342   cp_parser_parse_tentatively (parser);
14343
14344   /* Caller guarantees that the current token is 'enum', an identifier
14345      possibly follows, and the token after that is an opening brace.
14346      If we don't have an identifier, fabricate an anonymous name for
14347      the enumeration being defined.  */
14348   cp_lexer_consume_token (parser->lexer);
14349
14350   /* Parse the "class" or "struct", which indicates a scoped
14351      enumeration type in C++0x.  */
14352   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14353       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14354     {
14355       if (cxx_dialect < cxx0x)
14356         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14357
14358       /* Consume the `struct' or `class' token.  */
14359       cp_lexer_consume_token (parser->lexer);
14360
14361       scoped_enum_p = true;
14362     }
14363
14364   attributes = cp_parser_attributes_opt (parser);
14365
14366   /* Clear the qualification.  */
14367   parser->scope = NULL_TREE;
14368   parser->qualifying_scope = NULL_TREE;
14369   parser->object_scope = NULL_TREE;
14370
14371   /* Figure out in what scope the declaration is being placed.  */
14372   prev_scope = current_scope ();
14373
14374   type_start_token = cp_lexer_peek_token (parser->lexer);
14375
14376   push_deferring_access_checks (dk_no_check);
14377   nested_name_specifier
14378       = cp_parser_nested_name_specifier_opt (parser,
14379                                              /*typename_keyword_p=*/true,
14380                                              /*check_dependency_p=*/false,
14381                                              /*type_p=*/false,
14382                                              /*is_declaration=*/false);
14383
14384   if (nested_name_specifier)
14385     {
14386       tree name;
14387
14388       identifier = cp_parser_identifier (parser);
14389       name =  cp_parser_lookup_name (parser, identifier,
14390                                      enum_type,
14391                                      /*is_template=*/false,
14392                                      /*is_namespace=*/false,
14393                                      /*check_dependency=*/true,
14394                                      /*ambiguous_decls=*/NULL,
14395                                      input_location);
14396       if (name)
14397         {
14398           type = TREE_TYPE (name);
14399           if (TREE_CODE (type) == TYPENAME_TYPE)
14400             {
14401               /* Are template enums allowed in ISO? */
14402               if (template_parm_scope_p ())
14403                 pedwarn (type_start_token->location, OPT_Wpedantic,
14404                          "%qD is an enumeration template", name);
14405               /* ignore a typename reference, for it will be solved by name
14406                  in start_enum.  */
14407               type = NULL_TREE;
14408             }
14409         }
14410       else
14411         error_at (type_start_token->location,
14412                   "%qD is not an enumerator-name", identifier);
14413     }
14414   else
14415     {
14416       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14417         identifier = cp_parser_identifier (parser);
14418       else
14419         {
14420           identifier = make_anon_name ();
14421           is_anonymous = true;
14422         }
14423     }
14424   pop_deferring_access_checks ();
14425
14426   /* Check for the `:' that denotes a specified underlying type in C++0x.
14427      Note that a ':' could also indicate a bitfield width, however.  */
14428   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14429     {
14430       cp_decl_specifier_seq type_specifiers;
14431
14432       /* Consume the `:'.  */
14433       cp_lexer_consume_token (parser->lexer);
14434
14435       /* Parse the type-specifier-seq.  */
14436       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14437                                     /*is_trailing_return=*/false,
14438                                     &type_specifiers);
14439
14440       /* At this point this is surely not elaborated type specifier.  */
14441       if (!cp_parser_parse_definitely (parser))
14442         return NULL_TREE;
14443
14444       if (cxx_dialect < cxx0x)
14445         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14446
14447       has_underlying_type = true;
14448
14449       /* If that didn't work, stop.  */
14450       if (type_specifiers.type != error_mark_node)
14451         {
14452           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14453                                             /*initialized=*/0, NULL);
14454           if (underlying_type == error_mark_node)
14455             underlying_type = NULL_TREE;
14456         }
14457     }
14458
14459   /* Look for the `{' but don't consume it yet.  */
14460   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14461     {
14462       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14463         {
14464           cp_parser_error (parser, "expected %<{%>");
14465           if (has_underlying_type)
14466             {
14467               type = NULL_TREE;
14468               goto out;
14469             }
14470         }
14471       /* An opaque-enum-specifier must have a ';' here.  */
14472       if ((scoped_enum_p || underlying_type)
14473           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14474         {
14475           cp_parser_error (parser, "expected %<;%> or %<{%>");
14476           if (has_underlying_type)
14477             {
14478               type = NULL_TREE;
14479               goto out;
14480             }
14481         }
14482     }
14483
14484   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14485     return NULL_TREE;
14486
14487   if (nested_name_specifier)
14488     {
14489       if (CLASS_TYPE_P (nested_name_specifier))
14490         {
14491           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14492           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14493           push_scope (nested_name_specifier);
14494         }
14495       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14496         {
14497           push_nested_namespace (nested_name_specifier);
14498         }
14499     }
14500
14501   /* Issue an error message if type-definitions are forbidden here.  */
14502   if (!cp_parser_check_type_definition (parser))
14503     type = error_mark_node;
14504   else
14505     /* Create the new type.  We do this before consuming the opening
14506        brace so the enum will be recorded as being on the line of its
14507        tag (or the 'enum' keyword, if there is no tag).  */
14508     type = start_enum (identifier, type, underlying_type,
14509                        scoped_enum_p, &is_new_type);
14510
14511   /* If the next token is not '{' it is an opaque-enum-specifier or an
14512      elaborated-type-specifier.  */
14513   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14514     {
14515       timevar_push (TV_PARSE_ENUM);
14516       if (nested_name_specifier)
14517         {
14518           /* The following catches invalid code such as:
14519              enum class S<int>::E { A, B, C }; */
14520           if (!processing_specialization
14521               && CLASS_TYPE_P (nested_name_specifier)
14522               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14523             error_at (type_start_token->location, "cannot add an enumerator "
14524                       "list to a template instantiation");
14525
14526           /* If that scope does not contain the scope in which the
14527              class was originally declared, the program is invalid.  */
14528           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14529             {
14530               if (at_namespace_scope_p ())
14531                 error_at (type_start_token->location,
14532                           "declaration of %qD in namespace %qD which does not "
14533                           "enclose %qD",
14534                           type, prev_scope, nested_name_specifier);
14535               else
14536                 error_at (type_start_token->location,
14537                           "declaration of %qD in %qD which does not enclose %qD",
14538                           type, prev_scope, nested_name_specifier);
14539               type = error_mark_node;
14540             }
14541         }
14542
14543       if (scoped_enum_p)
14544         begin_scope (sk_scoped_enum, type);
14545
14546       /* Consume the opening brace.  */
14547       cp_lexer_consume_token (parser->lexer);
14548
14549       if (type == error_mark_node)
14550         ; /* Nothing to add */
14551       else if (OPAQUE_ENUM_P (type)
14552                || (cxx_dialect > cxx98 && processing_specialization))
14553         {
14554           new_value_list = true;
14555           SET_OPAQUE_ENUM_P (type, false);
14556           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14557         }
14558       else
14559         {
14560           error_at (type_start_token->location, "multiple definition of %q#T", type);
14561           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14562                     "previous definition here");
14563           type = error_mark_node;
14564         }
14565
14566       if (type == error_mark_node)
14567         cp_parser_skip_to_end_of_block_or_statement (parser);
14568       /* If the next token is not '}', then there are some enumerators.  */
14569       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14570         cp_parser_enumerator_list (parser, type);
14571
14572       /* Consume the final '}'.  */
14573       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14574
14575       if (scoped_enum_p)
14576         finish_scope ();
14577       timevar_pop (TV_PARSE_ENUM);
14578     }
14579   else
14580     {
14581       /* If a ';' follows, then it is an opaque-enum-specifier
14582         and additional restrictions apply.  */
14583       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14584         {
14585           if (is_anonymous)
14586             error_at (type_start_token->location,
14587                       "opaque-enum-specifier without name");
14588           else if (nested_name_specifier)
14589             error_at (type_start_token->location,
14590                       "opaque-enum-specifier must use a simple identifier");
14591         }
14592     }
14593
14594   /* Look for trailing attributes to apply to this enumeration, and
14595      apply them if appropriate.  */
14596   if (cp_parser_allow_gnu_extensions_p (parser))
14597     {
14598       tree trailing_attr = cp_parser_attributes_opt (parser);
14599       trailing_attr = chainon (trailing_attr, attributes);
14600       cplus_decl_attributes (&type,
14601                              trailing_attr,
14602                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14603     }
14604
14605   /* Finish up the enumeration.  */
14606   if (type != error_mark_node)
14607     {
14608       if (new_value_list)
14609         finish_enum_value_list (type);
14610       if (is_new_type)
14611         finish_enum (type);
14612     }
14613
14614   if (nested_name_specifier)
14615     {
14616       if (CLASS_TYPE_P (nested_name_specifier))
14617         {
14618           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14619           pop_scope (nested_name_specifier);
14620         }
14621       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14622         {
14623           pop_nested_namespace (nested_name_specifier);
14624         }
14625     }
14626  out:
14627   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14628   return type;
14629 }
14630
14631 /* Parse an enumerator-list.  The enumerators all have the indicated
14632    TYPE.
14633
14634    enumerator-list:
14635      enumerator-definition
14636      enumerator-list , enumerator-definition  */
14637
14638 static void
14639 cp_parser_enumerator_list (cp_parser* parser, tree type)
14640 {
14641   while (true)
14642     {
14643       /* Parse an enumerator-definition.  */
14644       cp_parser_enumerator_definition (parser, type);
14645
14646       /* If the next token is not a ',', we've reached the end of
14647          the list.  */
14648       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14649         break;
14650       /* Otherwise, consume the `,' and keep going.  */
14651       cp_lexer_consume_token (parser->lexer);
14652       /* If the next token is a `}', there is a trailing comma.  */
14653       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14654         {
14655           if (cxx_dialect < cxx0x && !in_system_header)
14656             pedwarn (input_location, OPT_Wpedantic,
14657                      "comma at end of enumerator list");
14658           break;
14659         }
14660     }
14661 }
14662
14663 /* Parse an enumerator-definition.  The enumerator has the indicated
14664    TYPE.
14665
14666    enumerator-definition:
14667      enumerator
14668      enumerator = constant-expression
14669
14670    enumerator:
14671      identifier  */
14672
14673 static void
14674 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14675 {
14676   tree identifier;
14677   tree value;
14678   location_t loc;
14679
14680   /* Save the input location because we are interested in the location
14681      of the identifier and not the location of the explicit value.  */
14682   loc = cp_lexer_peek_token (parser->lexer)->location;
14683
14684   /* Look for the identifier.  */
14685   identifier = cp_parser_identifier (parser);
14686   if (identifier == error_mark_node)
14687     return;
14688
14689   /* If the next token is an '=', then there is an explicit value.  */
14690   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14691     {
14692       /* Consume the `=' token.  */
14693       cp_lexer_consume_token (parser->lexer);
14694       /* Parse the value.  */
14695       value = cp_parser_constant_expression (parser,
14696                                              /*allow_non_constant_p=*/false,
14697                                              NULL);
14698     }
14699   else
14700     value = NULL_TREE;
14701
14702   /* If we are processing a template, make sure the initializer of the
14703      enumerator doesn't contain any bare template parameter pack.  */
14704   if (check_for_bare_parameter_packs (value))
14705     value = error_mark_node;
14706
14707   /* integral_constant_value will pull out this expression, so make sure
14708      it's folded as appropriate.  */
14709   value = fold_non_dependent_expr (value);
14710
14711   /* Create the enumerator.  */
14712   build_enumerator (identifier, value, type, loc);
14713 }
14714
14715 /* Parse a namespace-name.
14716
14717    namespace-name:
14718      original-namespace-name
14719      namespace-alias
14720
14721    Returns the NAMESPACE_DECL for the namespace.  */
14722
14723 static tree
14724 cp_parser_namespace_name (cp_parser* parser)
14725 {
14726   tree identifier;
14727   tree namespace_decl;
14728
14729   cp_token *token = cp_lexer_peek_token (parser->lexer);
14730
14731   /* Get the name of the namespace.  */
14732   identifier = cp_parser_identifier (parser);
14733   if (identifier == error_mark_node)
14734     return error_mark_node;
14735
14736   /* Look up the identifier in the currently active scope.  Look only
14737      for namespaces, due to:
14738
14739        [basic.lookup.udir]
14740
14741        When looking up a namespace-name in a using-directive or alias
14742        definition, only namespace names are considered.
14743
14744      And:
14745
14746        [basic.lookup.qual]
14747
14748        During the lookup of a name preceding the :: scope resolution
14749        operator, object, function, and enumerator names are ignored.
14750
14751      (Note that cp_parser_qualifying_entity only calls this
14752      function if the token after the name is the scope resolution
14753      operator.)  */
14754   namespace_decl = cp_parser_lookup_name (parser, identifier,
14755                                           none_type,
14756                                           /*is_template=*/false,
14757                                           /*is_namespace=*/true,
14758                                           /*check_dependency=*/true,
14759                                           /*ambiguous_decls=*/NULL,
14760                                           token->location);
14761   /* If it's not a namespace, issue an error.  */
14762   if (namespace_decl == error_mark_node
14763       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14764     {
14765       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14766         error_at (token->location, "%qD is not a namespace-name", identifier);
14767       cp_parser_error (parser, "expected namespace-name");
14768       namespace_decl = error_mark_node;
14769     }
14770
14771   return namespace_decl;
14772 }
14773
14774 /* Parse a namespace-definition.
14775
14776    namespace-definition:
14777      named-namespace-definition
14778      unnamed-namespace-definition
14779
14780    named-namespace-definition:
14781      original-namespace-definition
14782      extension-namespace-definition
14783
14784    original-namespace-definition:
14785      namespace identifier { namespace-body }
14786
14787    extension-namespace-definition:
14788      namespace original-namespace-name { namespace-body }
14789
14790    unnamed-namespace-definition:
14791      namespace { namespace-body } */
14792
14793 static void
14794 cp_parser_namespace_definition (cp_parser* parser)
14795 {
14796   tree identifier, attribs;
14797   bool has_visibility;
14798   bool is_inline;
14799
14800   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14801     {
14802       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14803       is_inline = true;
14804       cp_lexer_consume_token (parser->lexer);
14805     }
14806   else
14807     is_inline = false;
14808
14809   /* Look for the `namespace' keyword.  */
14810   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14811
14812   /* Get the name of the namespace.  We do not attempt to distinguish
14813      between an original-namespace-definition and an
14814      extension-namespace-definition at this point.  The semantic
14815      analysis routines are responsible for that.  */
14816   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14817     identifier = cp_parser_identifier (parser);
14818   else
14819     identifier = NULL_TREE;
14820
14821   /* Parse any specified attributes.  */
14822   attribs = cp_parser_attributes_opt (parser);
14823
14824   /* Look for the `{' to start the namespace.  */
14825   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14826   /* Start the namespace.  */
14827   push_namespace (identifier);
14828
14829   /* "inline namespace" is equivalent to a stub namespace definition
14830      followed by a strong using directive.  */
14831   if (is_inline)
14832     {
14833       tree name_space = current_namespace;
14834       /* Set up namespace association.  */
14835       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14836         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14837                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14838       /* Import the contents of the inline namespace.  */
14839       pop_namespace ();
14840       do_using_directive (name_space);
14841       push_namespace (identifier);
14842     }
14843
14844   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14845
14846   /* Parse the body of the namespace.  */
14847   cp_parser_namespace_body (parser);
14848
14849   if (has_visibility)
14850     pop_visibility (1);
14851
14852   /* Finish the namespace.  */
14853   pop_namespace ();
14854   /* Look for the final `}'.  */
14855   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14856 }
14857
14858 /* Parse a namespace-body.
14859
14860    namespace-body:
14861      declaration-seq [opt]  */
14862
14863 static void
14864 cp_parser_namespace_body (cp_parser* parser)
14865 {
14866   cp_parser_declaration_seq_opt (parser);
14867 }
14868
14869 /* Parse a namespace-alias-definition.
14870
14871    namespace-alias-definition:
14872      namespace identifier = qualified-namespace-specifier ;  */
14873
14874 static void
14875 cp_parser_namespace_alias_definition (cp_parser* parser)
14876 {
14877   tree identifier;
14878   tree namespace_specifier;
14879
14880   cp_token *token = cp_lexer_peek_token (parser->lexer);
14881
14882   /* Look for the `namespace' keyword.  */
14883   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14884   /* Look for the identifier.  */
14885   identifier = cp_parser_identifier (parser);
14886   if (identifier == error_mark_node)
14887     return;
14888   /* Look for the `=' token.  */
14889   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14890       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14891     {
14892       error_at (token->location, "%<namespace%> definition is not allowed here");
14893       /* Skip the definition.  */
14894       cp_lexer_consume_token (parser->lexer);
14895       if (cp_parser_skip_to_closing_brace (parser))
14896         cp_lexer_consume_token (parser->lexer);
14897       return;
14898     }
14899   cp_parser_require (parser, CPP_EQ, RT_EQ);
14900   /* Look for the qualified-namespace-specifier.  */
14901   namespace_specifier
14902     = cp_parser_qualified_namespace_specifier (parser);
14903   /* Look for the `;' token.  */
14904   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14905
14906   /* Register the alias in the symbol table.  */
14907   do_namespace_alias (identifier, namespace_specifier);
14908 }
14909
14910 /* Parse a qualified-namespace-specifier.
14911
14912    qualified-namespace-specifier:
14913      :: [opt] nested-name-specifier [opt] namespace-name
14914
14915    Returns a NAMESPACE_DECL corresponding to the specified
14916    namespace.  */
14917
14918 static tree
14919 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14920 {
14921   /* Look for the optional `::'.  */
14922   cp_parser_global_scope_opt (parser,
14923                               /*current_scope_valid_p=*/false);
14924
14925   /* Look for the optional nested-name-specifier.  */
14926   cp_parser_nested_name_specifier_opt (parser,
14927                                        /*typename_keyword_p=*/false,
14928                                        /*check_dependency_p=*/true,
14929                                        /*type_p=*/false,
14930                                        /*is_declaration=*/true);
14931
14932   return cp_parser_namespace_name (parser);
14933 }
14934
14935 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14936    access declaration.
14937
14938    using-declaration:
14939      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14940      using :: unqualified-id ;  
14941
14942    access-declaration:
14943      qualified-id ;  
14944
14945    */
14946
14947 static bool
14948 cp_parser_using_declaration (cp_parser* parser, 
14949                              bool access_declaration_p)
14950 {
14951   cp_token *token;
14952   bool typename_p = false;
14953   bool global_scope_p;
14954   tree decl;
14955   tree identifier;
14956   tree qscope;
14957   int oldcount = errorcount;
14958   cp_token *diag_token = NULL;
14959
14960   if (access_declaration_p)
14961     {
14962       diag_token = cp_lexer_peek_token (parser->lexer);
14963       cp_parser_parse_tentatively (parser);
14964     }
14965   else
14966     {
14967       /* Look for the `using' keyword.  */
14968       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14969       
14970       /* Peek at the next token.  */
14971       token = cp_lexer_peek_token (parser->lexer);
14972       /* See if it's `typename'.  */
14973       if (token->keyword == RID_TYPENAME)
14974         {
14975           /* Remember that we've seen it.  */
14976           typename_p = true;
14977           /* Consume the `typename' token.  */
14978           cp_lexer_consume_token (parser->lexer);
14979         }
14980     }
14981
14982   /* Look for the optional global scope qualification.  */
14983   global_scope_p
14984     = (cp_parser_global_scope_opt (parser,
14985                                    /*current_scope_valid_p=*/false)
14986        != NULL_TREE);
14987
14988   /* If we saw `typename', or didn't see `::', then there must be a
14989      nested-name-specifier present.  */
14990   if (typename_p || !global_scope_p)
14991     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14992                                               /*check_dependency_p=*/true,
14993                                               /*type_p=*/false,
14994                                               /*is_declaration=*/true);
14995   /* Otherwise, we could be in either of the two productions.  In that
14996      case, treat the nested-name-specifier as optional.  */
14997   else
14998     qscope = cp_parser_nested_name_specifier_opt (parser,
14999                                                   /*typename_keyword_p=*/false,
15000                                                   /*check_dependency_p=*/true,
15001                                                   /*type_p=*/false,
15002                                                   /*is_declaration=*/true);
15003   if (!qscope)
15004     qscope = global_namespace;
15005
15006   if (access_declaration_p && cp_parser_error_occurred (parser))
15007     /* Something has already gone wrong; there's no need to parse
15008        further.  Since an error has occurred, the return value of
15009        cp_parser_parse_definitely will be false, as required.  */
15010     return cp_parser_parse_definitely (parser);
15011
15012   token = cp_lexer_peek_token (parser->lexer);
15013   /* Parse the unqualified-id.  */
15014   identifier = cp_parser_unqualified_id (parser,
15015                                          /*template_keyword_p=*/false,
15016                                          /*check_dependency_p=*/true,
15017                                          /*declarator_p=*/true,
15018                                          /*optional_p=*/false);
15019
15020   if (access_declaration_p)
15021     {
15022       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15023         cp_parser_simulate_error (parser);
15024       if (!cp_parser_parse_definitely (parser))
15025         return false;
15026     }
15027
15028   /* The function we call to handle a using-declaration is different
15029      depending on what scope we are in.  */
15030   if (qscope == error_mark_node || identifier == error_mark_node)
15031     ;
15032   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15033            && TREE_CODE (identifier) != BIT_NOT_EXPR)
15034     /* [namespace.udecl]
15035
15036        A using declaration shall not name a template-id.  */
15037     error_at (token->location,
15038               "a template-id may not appear in a using-declaration");
15039   else
15040     {
15041       if (at_class_scope_p ())
15042         {
15043           /* Create the USING_DECL.  */
15044           decl = do_class_using_decl (parser->scope, identifier);
15045
15046           if (decl && typename_p)
15047             USING_DECL_TYPENAME_P (decl) = 1;
15048
15049           if (check_for_bare_parameter_packs (decl))
15050             return false;
15051           else
15052             /* Add it to the list of members in this class.  */
15053             finish_member_declaration (decl);
15054         }
15055       else
15056         {
15057           decl = cp_parser_lookup_name_simple (parser,
15058                                                identifier,
15059                                                token->location);
15060           if (decl == error_mark_node)
15061             cp_parser_name_lookup_error (parser, identifier,
15062                                          decl, NLE_NULL,
15063                                          token->location);
15064           else if (check_for_bare_parameter_packs (decl))
15065             return false;
15066           else if (!at_namespace_scope_p ())
15067             do_local_using_decl (decl, qscope, identifier);
15068           else
15069             do_toplevel_using_decl (decl, qscope, identifier);
15070         }
15071     }
15072
15073   /* Look for the final `;'.  */
15074   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15075
15076   if (access_declaration_p && errorcount == oldcount)
15077     warning_at (diag_token->location, OPT_Wdeprecated,
15078                 "access declarations are deprecated "
15079                 "in favour of using-declarations; "
15080                 "suggestion: add the %<using%> keyword");
15081
15082   return true;
15083 }
15084
15085 /* Parse an alias-declaration.
15086
15087    alias-declaration:
15088      using identifier attribute-specifier-seq [opt] = type-id  */
15089
15090 static tree
15091 cp_parser_alias_declaration (cp_parser* parser)
15092 {
15093   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15094   location_t id_location, using_location, attrs_location = 0;
15095   cp_declarator *declarator;
15096   cp_decl_specifier_seq decl_specs;
15097   bool member_p;
15098   const char *saved_message = NULL;
15099
15100   /* Look for the `using' keyword.  */
15101   using_location = cp_lexer_peek_token (parser->lexer)->location;
15102   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15103   id_location = cp_lexer_peek_token (parser->lexer)->location;
15104   id = cp_parser_identifier (parser);
15105   if (id == error_mark_node)
15106     return error_mark_node;
15107
15108   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15109   attributes = cp_parser_attributes_opt (parser);
15110   if (attributes == error_mark_node)
15111     return error_mark_node;
15112
15113   cp_parser_require (parser, CPP_EQ, RT_EQ);
15114
15115   if (cp_parser_error_occurred (parser))
15116     return error_mark_node;
15117
15118   /* Now we are going to parse the type-id of the declaration.  */
15119
15120   /*
15121     [dcl.type]/3 says:
15122
15123         "A type-specifier-seq shall not define a class or enumeration
15124          unless it appears in the type-id of an alias-declaration (7.1.3) that
15125          is not the declaration of a template-declaration."
15126
15127     In other words, if we currently are in an alias template, the
15128     type-id should not define a type.
15129
15130     So let's set parser->type_definition_forbidden_message in that
15131     case; cp_parser_check_type_definition (called by
15132     cp_parser_class_specifier) will then emit an error if a type is
15133     defined in the type-id.  */
15134   if (parser->num_template_parameter_lists)
15135     {
15136       saved_message = parser->type_definition_forbidden_message;
15137       parser->type_definition_forbidden_message =
15138         G_("types may not be defined in alias template declarations");
15139     }
15140
15141   type = cp_parser_type_id (parser);
15142
15143   /* Restore the error message if need be.  */
15144   if (parser->num_template_parameter_lists)
15145     parser->type_definition_forbidden_message = saved_message;
15146
15147   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15148
15149   if (cp_parser_error_occurred (parser))
15150     return error_mark_node;
15151
15152   /* A typedef-name can also be introduced by an alias-declaration. The
15153      identifier following the using keyword becomes a typedef-name. It has
15154      the same semantics as if it were introduced by the typedef
15155      specifier. In particular, it does not define a new type and it shall
15156      not appear in the type-id.  */
15157
15158   clear_decl_specs (&decl_specs);
15159   decl_specs.type = type;
15160   if (attributes != NULL_TREE)
15161     {
15162       decl_specs.attributes = attributes;
15163       set_and_check_decl_spec_loc (&decl_specs,
15164                                    ds_attribute,
15165                                    attrs_location);
15166     }
15167   set_and_check_decl_spec_loc (&decl_specs,
15168                                ds_typedef,
15169                                using_location);
15170   set_and_check_decl_spec_loc (&decl_specs,
15171                                ds_alias,
15172                                using_location);
15173
15174   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15175   declarator->id_loc = id_location;
15176
15177   member_p = at_class_scope_p ();
15178   if (member_p)
15179     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15180                       NULL_TREE, attributes);
15181   else
15182     decl = start_decl (declarator, &decl_specs, 0,
15183                        attributes, NULL_TREE, &pushed_scope);
15184   if (decl == error_mark_node)
15185     return decl;
15186
15187   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15188
15189   if (pushed_scope)
15190     pop_scope (pushed_scope);
15191
15192   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15193      added into the symbol table; otherwise, return the TYPE_DECL.  */
15194   if (DECL_LANG_SPECIFIC (decl)
15195       && DECL_TEMPLATE_INFO (decl)
15196       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15197     {
15198       decl = DECL_TI_TEMPLATE (decl);
15199       if (member_p)
15200         check_member_template (decl);
15201     }
15202
15203   return decl;
15204 }
15205
15206 /* Parse a using-directive.
15207
15208    using-directive:
15209      using namespace :: [opt] nested-name-specifier [opt]
15210        namespace-name ;  */
15211
15212 static void
15213 cp_parser_using_directive (cp_parser* parser)
15214 {
15215   tree namespace_decl;
15216   tree attribs;
15217
15218   /* Look for the `using' keyword.  */
15219   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15220   /* And the `namespace' keyword.  */
15221   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15222   /* Look for the optional `::' operator.  */
15223   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15224   /* And the optional nested-name-specifier.  */
15225   cp_parser_nested_name_specifier_opt (parser,
15226                                        /*typename_keyword_p=*/false,
15227                                        /*check_dependency_p=*/true,
15228                                        /*type_p=*/false,
15229                                        /*is_declaration=*/true);
15230   /* Get the namespace being used.  */
15231   namespace_decl = cp_parser_namespace_name (parser);
15232   /* And any specified attributes.  */
15233   attribs = cp_parser_attributes_opt (parser);
15234   /* Update the symbol table.  */
15235   parse_using_directive (namespace_decl, attribs);
15236   /* Look for the final `;'.  */
15237   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15238 }
15239
15240 /* Parse an asm-definition.
15241
15242    asm-definition:
15243      asm ( string-literal ) ;
15244
15245    GNU Extension:
15246
15247    asm-definition:
15248      asm volatile [opt] ( string-literal ) ;
15249      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15250      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15251                           : asm-operand-list [opt] ) ;
15252      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15253                           : asm-operand-list [opt]
15254                           : asm-clobber-list [opt] ) ;
15255      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15256                                : asm-clobber-list [opt]
15257                                : asm-goto-list ) ;  */
15258
15259 static void
15260 cp_parser_asm_definition (cp_parser* parser)
15261 {
15262   tree string;
15263   tree outputs = NULL_TREE;
15264   tree inputs = NULL_TREE;
15265   tree clobbers = NULL_TREE;
15266   tree labels = NULL_TREE;
15267   tree asm_stmt;
15268   bool volatile_p = false;
15269   bool extended_p = false;
15270   bool invalid_inputs_p = false;
15271   bool invalid_outputs_p = false;
15272   bool goto_p = false;
15273   required_token missing = RT_NONE;
15274
15275   /* Look for the `asm' keyword.  */
15276   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15277   /* See if the next token is `volatile'.  */
15278   if (cp_parser_allow_gnu_extensions_p (parser)
15279       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15280     {
15281       /* Remember that we saw the `volatile' keyword.  */
15282       volatile_p = true;
15283       /* Consume the token.  */
15284       cp_lexer_consume_token (parser->lexer);
15285     }
15286   if (cp_parser_allow_gnu_extensions_p (parser)
15287       && parser->in_function_body
15288       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15289     {
15290       /* Remember that we saw the `goto' keyword.  */
15291       goto_p = true;
15292       /* Consume the token.  */
15293       cp_lexer_consume_token (parser->lexer);
15294     }
15295   /* Look for the opening `('.  */
15296   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15297     return;
15298   /* Look for the string.  */
15299   string = cp_parser_string_literal (parser, false, false);
15300   if (string == error_mark_node)
15301     {
15302       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15303                                              /*consume_paren=*/true);
15304       return;
15305     }
15306
15307   /* If we're allowing GNU extensions, check for the extended assembly
15308      syntax.  Unfortunately, the `:' tokens need not be separated by
15309      a space in C, and so, for compatibility, we tolerate that here
15310      too.  Doing that means that we have to treat the `::' operator as
15311      two `:' tokens.  */
15312   if (cp_parser_allow_gnu_extensions_p (parser)
15313       && parser->in_function_body
15314       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15315           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15316     {
15317       bool inputs_p = false;
15318       bool clobbers_p = false;
15319       bool labels_p = false;
15320
15321       /* The extended syntax was used.  */
15322       extended_p = true;
15323
15324       /* Look for outputs.  */
15325       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15326         {
15327           /* Consume the `:'.  */
15328           cp_lexer_consume_token (parser->lexer);
15329           /* Parse the output-operands.  */
15330           if (cp_lexer_next_token_is_not (parser->lexer,
15331                                           CPP_COLON)
15332               && cp_lexer_next_token_is_not (parser->lexer,
15333                                              CPP_SCOPE)
15334               && cp_lexer_next_token_is_not (parser->lexer,
15335                                              CPP_CLOSE_PAREN)
15336               && !goto_p)
15337             outputs = cp_parser_asm_operand_list (parser);
15338
15339             if (outputs == error_mark_node)
15340               invalid_outputs_p = true;
15341         }
15342       /* If the next token is `::', there are no outputs, and the
15343          next token is the beginning of the inputs.  */
15344       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15345         /* The inputs are coming next.  */
15346         inputs_p = true;
15347
15348       /* Look for inputs.  */
15349       if (inputs_p
15350           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15351         {
15352           /* Consume the `:' or `::'.  */
15353           cp_lexer_consume_token (parser->lexer);
15354           /* Parse the output-operands.  */
15355           if (cp_lexer_next_token_is_not (parser->lexer,
15356                                           CPP_COLON)
15357               && cp_lexer_next_token_is_not (parser->lexer,
15358                                              CPP_SCOPE)
15359               && cp_lexer_next_token_is_not (parser->lexer,
15360                                              CPP_CLOSE_PAREN))
15361             inputs = cp_parser_asm_operand_list (parser);
15362
15363             if (inputs == error_mark_node)
15364               invalid_inputs_p = true;
15365         }
15366       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15367         /* The clobbers are coming next.  */
15368         clobbers_p = true;
15369
15370       /* Look for clobbers.  */
15371       if (clobbers_p
15372           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15373         {
15374           clobbers_p = true;
15375           /* Consume the `:' or `::'.  */
15376           cp_lexer_consume_token (parser->lexer);
15377           /* Parse the clobbers.  */
15378           if (cp_lexer_next_token_is_not (parser->lexer,
15379                                           CPP_COLON)
15380               && cp_lexer_next_token_is_not (parser->lexer,
15381                                              CPP_CLOSE_PAREN))
15382             clobbers = cp_parser_asm_clobber_list (parser);
15383         }
15384       else if (goto_p
15385                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15386         /* The labels are coming next.  */
15387         labels_p = true;
15388
15389       /* Look for labels.  */
15390       if (labels_p
15391           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15392         {
15393           labels_p = true;
15394           /* Consume the `:' or `::'.  */
15395           cp_lexer_consume_token (parser->lexer);
15396           /* Parse the labels.  */
15397           labels = cp_parser_asm_label_list (parser);
15398         }
15399
15400       if (goto_p && !labels_p)
15401         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15402     }
15403   else if (goto_p)
15404     missing = RT_COLON_SCOPE;
15405
15406   /* Look for the closing `)'.  */
15407   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15408                           missing ? missing : RT_CLOSE_PAREN))
15409     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15410                                            /*consume_paren=*/true);
15411   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15412
15413   if (!invalid_inputs_p && !invalid_outputs_p)
15414     {
15415       /* Create the ASM_EXPR.  */
15416       if (parser->in_function_body)
15417         {
15418           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15419                                       inputs, clobbers, labels);
15420           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15421           if (!extended_p)
15422             {
15423               tree temp = asm_stmt;
15424               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15425                 temp = TREE_OPERAND (temp, 0);
15426
15427               ASM_INPUT_P (temp) = 1;
15428             }
15429         }
15430       else
15431         add_asm_node (string);
15432     }
15433 }
15434
15435 /* Declarators [gram.dcl.decl] */
15436
15437 /* Parse an init-declarator.
15438
15439    init-declarator:
15440      declarator initializer [opt]
15441
15442    GNU Extension:
15443
15444    init-declarator:
15445      declarator asm-specification [opt] attributes [opt] initializer [opt]
15446
15447    function-definition:
15448      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15449        function-body
15450      decl-specifier-seq [opt] declarator function-try-block
15451
15452    GNU Extension:
15453
15454    function-definition:
15455      __extension__ function-definition
15456
15457    TM Extension:
15458
15459    function-definition:
15460      decl-specifier-seq [opt] declarator function-transaction-block
15461
15462    The DECL_SPECIFIERS apply to this declarator.  Returns a
15463    representation of the entity declared.  If MEMBER_P is TRUE, then
15464    this declarator appears in a class scope.  The new DECL created by
15465    this declarator is returned.
15466
15467    The CHECKS are access checks that should be performed once we know
15468    what entity is being declared (and, therefore, what classes have
15469    befriended it).
15470
15471    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15472    for a function-definition here as well.  If the declarator is a
15473    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15474    be TRUE upon return.  By that point, the function-definition will
15475    have been completely parsed.
15476
15477    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15478    is FALSE.
15479
15480    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15481    parsed declaration if it is an uninitialized single declarator not followed
15482    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15483    if present, will not be consumed.  If returned, this declarator will be
15484    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15485
15486 static tree
15487 cp_parser_init_declarator (cp_parser* parser,
15488                            cp_decl_specifier_seq *decl_specifiers,
15489                            VEC (deferred_access_check,gc)* checks,
15490                            bool function_definition_allowed_p,
15491                            bool member_p,
15492                            int declares_class_or_enum,
15493                            bool* function_definition_p,
15494                            tree* maybe_range_for_decl)
15495 {
15496   cp_token *token = NULL, *asm_spec_start_token = NULL,
15497            *attributes_start_token = NULL;
15498   cp_declarator *declarator;
15499   tree prefix_attributes;
15500   tree attributes;
15501   tree asm_specification;
15502   tree initializer;
15503   tree decl = NULL_TREE;
15504   tree scope;
15505   int is_initialized;
15506   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15507      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15508      "(...)".  */
15509   enum cpp_ttype initialization_kind;
15510   bool is_direct_init = false;
15511   bool is_non_constant_init;
15512   int ctor_dtor_or_conv_p;
15513   bool friend_p;
15514   tree pushed_scope = NULL_TREE;
15515   bool range_for_decl_p = false;
15516
15517   /* Gather the attributes that were provided with the
15518      decl-specifiers.  */
15519   prefix_attributes = decl_specifiers->attributes;
15520
15521   /* Assume that this is not the declarator for a function
15522      definition.  */
15523   if (function_definition_p)
15524     *function_definition_p = false;
15525
15526   /* Defer access checks while parsing the declarator; we cannot know
15527      what names are accessible until we know what is being
15528      declared.  */
15529   resume_deferring_access_checks ();
15530
15531   /* Parse the declarator.  */
15532   token = cp_lexer_peek_token (parser->lexer);
15533   declarator
15534     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15535                             &ctor_dtor_or_conv_p,
15536                             /*parenthesized_p=*/NULL,
15537                             member_p);
15538   /* Gather up the deferred checks.  */
15539   stop_deferring_access_checks ();
15540
15541   /* If the DECLARATOR was erroneous, there's no need to go
15542      further.  */
15543   if (declarator == cp_error_declarator)
15544     return error_mark_node;
15545
15546   /* Check that the number of template-parameter-lists is OK.  */
15547   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15548                                                        token->location))
15549     return error_mark_node;
15550
15551   if (declares_class_or_enum & 2)
15552     cp_parser_check_for_definition_in_return_type (declarator,
15553                                                    decl_specifiers->type,
15554                                                    decl_specifiers->locations[ds_type_spec]);
15555
15556   /* Figure out what scope the entity declared by the DECLARATOR is
15557      located in.  `grokdeclarator' sometimes changes the scope, so
15558      we compute it now.  */
15559   scope = get_scope_of_declarator (declarator);
15560
15561   /* Perform any lookups in the declared type which were thought to be
15562      dependent, but are not in the scope of the declarator.  */
15563   decl_specifiers->type
15564     = maybe_update_decl_type (decl_specifiers->type, scope);
15565
15566   /* If we're allowing GNU extensions, look for an asm-specification
15567      and attributes.  */
15568   if (cp_parser_allow_gnu_extensions_p (parser))
15569     {
15570       /* Look for an asm-specification.  */
15571       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15572       asm_specification = cp_parser_asm_specification_opt (parser);
15573       /* And attributes.  */
15574       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15575       attributes = cp_parser_attributes_opt (parser);
15576     }
15577   else
15578     {
15579       asm_specification = NULL_TREE;
15580       attributes = NULL_TREE;
15581     }
15582
15583   /* Peek at the next token.  */
15584   token = cp_lexer_peek_token (parser->lexer);
15585   /* Check to see if the token indicates the start of a
15586      function-definition.  */
15587   if (function_declarator_p (declarator)
15588       && cp_parser_token_starts_function_definition_p (token))
15589     {
15590       if (!function_definition_allowed_p)
15591         {
15592           /* If a function-definition should not appear here, issue an
15593              error message.  */
15594           cp_parser_error (parser,
15595                            "a function-definition is not allowed here");
15596           return error_mark_node;
15597         }
15598       else
15599         {
15600           location_t func_brace_location
15601             = cp_lexer_peek_token (parser->lexer)->location;
15602
15603           /* Neither attributes nor an asm-specification are allowed
15604              on a function-definition.  */
15605           if (asm_specification)
15606             error_at (asm_spec_start_token->location,
15607                       "an asm-specification is not allowed "
15608                       "on a function-definition");
15609           if (attributes)
15610             error_at (attributes_start_token->location,
15611                       "attributes are not allowed on a function-definition");
15612           /* This is a function-definition.  */
15613           *function_definition_p = true;
15614
15615           /* Parse the function definition.  */
15616           if (member_p)
15617             decl = cp_parser_save_member_function_body (parser,
15618                                                         decl_specifiers,
15619                                                         declarator,
15620                                                         prefix_attributes);
15621           else
15622             decl
15623               = (cp_parser_function_definition_from_specifiers_and_declarator
15624                  (parser, decl_specifiers, prefix_attributes, declarator));
15625
15626           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15627             {
15628               /* This is where the prologue starts...  */
15629               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15630                 = func_brace_location;
15631             }
15632
15633           return decl;
15634         }
15635     }
15636
15637   /* [dcl.dcl]
15638
15639      Only in function declarations for constructors, destructors, and
15640      type conversions can the decl-specifier-seq be omitted.
15641
15642      We explicitly postpone this check past the point where we handle
15643      function-definitions because we tolerate function-definitions
15644      that are missing their return types in some modes.  */
15645   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15646     {
15647       cp_parser_error (parser,
15648                        "expected constructor, destructor, or type conversion");
15649       return error_mark_node;
15650     }
15651
15652   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15653   if (token->type == CPP_EQ
15654       || token->type == CPP_OPEN_PAREN
15655       || token->type == CPP_OPEN_BRACE)
15656     {
15657       is_initialized = SD_INITIALIZED;
15658       initialization_kind = token->type;
15659       if (maybe_range_for_decl)
15660         *maybe_range_for_decl = error_mark_node;
15661
15662       if (token->type == CPP_EQ
15663           && function_declarator_p (declarator))
15664         {
15665           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15666           if (t2->keyword == RID_DEFAULT)
15667             is_initialized = SD_DEFAULTED;
15668           else if (t2->keyword == RID_DELETE)
15669             is_initialized = SD_DELETED;
15670         }
15671     }
15672   else
15673     {
15674       /* If the init-declarator isn't initialized and isn't followed by a
15675          `,' or `;', it's not a valid init-declarator.  */
15676       if (token->type != CPP_COMMA
15677           && token->type != CPP_SEMICOLON)
15678         {
15679           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15680             range_for_decl_p = true;
15681           else
15682             {
15683               cp_parser_error (parser, "expected initializer");
15684               return error_mark_node;
15685             }
15686         }
15687       is_initialized = SD_UNINITIALIZED;
15688       initialization_kind = CPP_EOF;
15689     }
15690
15691   /* Because start_decl has side-effects, we should only call it if we
15692      know we're going ahead.  By this point, we know that we cannot
15693      possibly be looking at any other construct.  */
15694   cp_parser_commit_to_tentative_parse (parser);
15695
15696   /* If the decl specifiers were bad, issue an error now that we're
15697      sure this was intended to be a declarator.  Then continue
15698      declaring the variable(s), as int, to try to cut down on further
15699      errors.  */
15700   if (decl_specifiers->any_specifiers_p
15701       && decl_specifiers->type == error_mark_node)
15702     {
15703       cp_parser_error (parser, "invalid type in declaration");
15704       decl_specifiers->type = integer_type_node;
15705     }
15706
15707   /* Check to see whether or not this declaration is a friend.  */
15708   friend_p = cp_parser_friend_p (decl_specifiers);
15709
15710   /* Enter the newly declared entry in the symbol table.  If we're
15711      processing a declaration in a class-specifier, we wait until
15712      after processing the initializer.  */
15713   if (!member_p)
15714     {
15715       if (parser->in_unbraced_linkage_specification_p)
15716         decl_specifiers->storage_class = sc_extern;
15717       decl = start_decl (declarator, decl_specifiers,
15718                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15719                          attributes, prefix_attributes,
15720                          &pushed_scope);
15721       /* Adjust location of decl if declarator->id_loc is more appropriate:
15722          set, and decl wasn't merged with another decl, in which case its
15723          location would be different from input_location, and more accurate.  */
15724       if (DECL_P (decl)
15725           && declarator->id_loc != UNKNOWN_LOCATION
15726           && DECL_SOURCE_LOCATION (decl) == input_location)
15727         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15728     }
15729   else if (scope)
15730     /* Enter the SCOPE.  That way unqualified names appearing in the
15731        initializer will be looked up in SCOPE.  */
15732     pushed_scope = push_scope (scope);
15733
15734   /* Perform deferred access control checks, now that we know in which
15735      SCOPE the declared entity resides.  */
15736   if (!member_p && decl)
15737     {
15738       tree saved_current_function_decl = NULL_TREE;
15739
15740       /* If the entity being declared is a function, pretend that we
15741          are in its scope.  If it is a `friend', it may have access to
15742          things that would not otherwise be accessible.  */
15743       if (TREE_CODE (decl) == FUNCTION_DECL)
15744         {
15745           saved_current_function_decl = current_function_decl;
15746           current_function_decl = decl;
15747         }
15748
15749       /* Perform access checks for template parameters.  */
15750       cp_parser_perform_template_parameter_access_checks (checks);
15751
15752       /* Perform the access control checks for the declarator and the
15753          decl-specifiers.  */
15754       perform_deferred_access_checks ();
15755
15756       /* Restore the saved value.  */
15757       if (TREE_CODE (decl) == FUNCTION_DECL)
15758         current_function_decl = saved_current_function_decl;
15759     }
15760
15761   /* Parse the initializer.  */
15762   initializer = NULL_TREE;
15763   is_direct_init = false;
15764   is_non_constant_init = true;
15765   if (is_initialized)
15766     {
15767       if (function_declarator_p (declarator))
15768         {
15769           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15770            if (initialization_kind == CPP_EQ)
15771              initializer = cp_parser_pure_specifier (parser);
15772            else
15773              {
15774                /* If the declaration was erroneous, we don't really
15775                   know what the user intended, so just silently
15776                   consume the initializer.  */
15777                if (decl != error_mark_node)
15778                  error_at (initializer_start_token->location,
15779                            "initializer provided for function");
15780                cp_parser_skip_to_closing_parenthesis (parser,
15781                                                       /*recovering=*/true,
15782                                                       /*or_comma=*/false,
15783                                                       /*consume_paren=*/true);
15784              }
15785         }
15786       else
15787         {
15788           /* We want to record the extra mangling scope for in-class
15789              initializers of class members and initializers of static data
15790              member templates.  The former involves deferring
15791              parsing of the initializer until end of class as with default
15792              arguments.  So right here we only handle the latter.  */
15793           if (!member_p && processing_template_decl)
15794             start_lambda_scope (decl);
15795           initializer = cp_parser_initializer (parser,
15796                                                &is_direct_init,
15797                                                &is_non_constant_init);
15798           if (!member_p && processing_template_decl)
15799             finish_lambda_scope ();
15800           if (initializer == error_mark_node)
15801             cp_parser_skip_to_end_of_statement (parser);
15802         }
15803     }
15804
15805   /* The old parser allows attributes to appear after a parenthesized
15806      initializer.  Mark Mitchell proposed removing this functionality
15807      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15808      attributes -- but ignores them.  */
15809   if (cp_parser_allow_gnu_extensions_p (parser)
15810       && initialization_kind == CPP_OPEN_PAREN)
15811     if (cp_parser_attributes_opt (parser))
15812       warning (OPT_Wattributes,
15813                "attributes after parenthesized initializer ignored");
15814
15815   /* For an in-class declaration, use `grokfield' to create the
15816      declaration.  */
15817   if (member_p)
15818     {
15819       if (pushed_scope)
15820         {
15821           pop_scope (pushed_scope);
15822           pushed_scope = NULL_TREE;
15823         }
15824       decl = grokfield (declarator, decl_specifiers,
15825                         initializer, !is_non_constant_init,
15826                         /*asmspec=*/NULL_TREE,
15827                         prefix_attributes);
15828       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15829         cp_parser_save_default_args (parser, decl);
15830     }
15831
15832   /* Finish processing the declaration.  But, skip member
15833      declarations.  */
15834   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15835     {
15836       cp_finish_decl (decl,
15837                       initializer, !is_non_constant_init,
15838                       asm_specification,
15839                       /* If the initializer is in parentheses, then this is
15840                          a direct-initialization, which means that an
15841                          `explicit' constructor is OK.  Otherwise, an
15842                          `explicit' constructor cannot be used.  */
15843                       ((is_direct_init || !is_initialized)
15844                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15845     }
15846   else if ((cxx_dialect != cxx98) && friend_p
15847            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15848     /* Core issue #226 (C++0x only): A default template-argument
15849        shall not be specified in a friend class template
15850        declaration. */
15851     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15852                              /*is_partial=*/0, /*is_friend_decl=*/1);
15853
15854   if (!friend_p && pushed_scope)
15855     pop_scope (pushed_scope);
15856
15857   return decl;
15858 }
15859
15860 /* Parse a declarator.
15861
15862    declarator:
15863      direct-declarator
15864      ptr-operator declarator
15865
15866    abstract-declarator:
15867      ptr-operator abstract-declarator [opt]
15868      direct-abstract-declarator
15869
15870    GNU Extensions:
15871
15872    declarator:
15873      attributes [opt] direct-declarator
15874      attributes [opt] ptr-operator declarator
15875
15876    abstract-declarator:
15877      attributes [opt] ptr-operator abstract-declarator [opt]
15878      attributes [opt] direct-abstract-declarator
15879
15880    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15881    detect constructor, destructor or conversion operators. It is set
15882    to -1 if the declarator is a name, and +1 if it is a
15883    function. Otherwise it is set to zero. Usually you just want to
15884    test for >0, but internally the negative value is used.
15885
15886    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15887    a decl-specifier-seq unless it declares a constructor, destructor,
15888    or conversion.  It might seem that we could check this condition in
15889    semantic analysis, rather than parsing, but that makes it difficult
15890    to handle something like `f()'.  We want to notice that there are
15891    no decl-specifiers, and therefore realize that this is an
15892    expression, not a declaration.)
15893
15894    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15895    the declarator is a direct-declarator of the form "(...)".
15896
15897    MEMBER_P is true iff this declarator is a member-declarator.  */
15898
15899 static cp_declarator *
15900 cp_parser_declarator (cp_parser* parser,
15901                       cp_parser_declarator_kind dcl_kind,
15902                       int* ctor_dtor_or_conv_p,
15903                       bool* parenthesized_p,
15904                       bool member_p)
15905 {
15906   cp_declarator *declarator;
15907   enum tree_code code;
15908   cp_cv_quals cv_quals;
15909   tree class_type;
15910   tree attributes = NULL_TREE;
15911
15912   /* Assume this is not a constructor, destructor, or type-conversion
15913      operator.  */
15914   if (ctor_dtor_or_conv_p)
15915     *ctor_dtor_or_conv_p = 0;
15916
15917   if (cp_parser_allow_gnu_extensions_p (parser))
15918     attributes = cp_parser_attributes_opt (parser);
15919
15920   /* Check for the ptr-operator production.  */
15921   cp_parser_parse_tentatively (parser);
15922   /* Parse the ptr-operator.  */
15923   code = cp_parser_ptr_operator (parser,
15924                                  &class_type,
15925                                  &cv_quals);
15926   /* If that worked, then we have a ptr-operator.  */
15927   if (cp_parser_parse_definitely (parser))
15928     {
15929       /* If a ptr-operator was found, then this declarator was not
15930          parenthesized.  */
15931       if (parenthesized_p)
15932         *parenthesized_p = true;
15933       /* The dependent declarator is optional if we are parsing an
15934          abstract-declarator.  */
15935       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15936         cp_parser_parse_tentatively (parser);
15937
15938       /* Parse the dependent declarator.  */
15939       declarator = cp_parser_declarator (parser, dcl_kind,
15940                                          /*ctor_dtor_or_conv_p=*/NULL,
15941                                          /*parenthesized_p=*/NULL,
15942                                          /*member_p=*/false);
15943
15944       /* If we are parsing an abstract-declarator, we must handle the
15945          case where the dependent declarator is absent.  */
15946       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15947           && !cp_parser_parse_definitely (parser))
15948         declarator = NULL;
15949
15950       declarator = cp_parser_make_indirect_declarator
15951         (code, class_type, cv_quals, declarator);
15952     }
15953   /* Everything else is a direct-declarator.  */
15954   else
15955     {
15956       if (parenthesized_p)
15957         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15958                                                    CPP_OPEN_PAREN);
15959       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15960                                                 ctor_dtor_or_conv_p,
15961                                                 member_p);
15962     }
15963
15964   if (attributes && declarator && declarator != cp_error_declarator)
15965     declarator->attributes = attributes;
15966
15967   return declarator;
15968 }
15969
15970 /* Parse a direct-declarator or direct-abstract-declarator.
15971
15972    direct-declarator:
15973      declarator-id
15974      direct-declarator ( parameter-declaration-clause )
15975        cv-qualifier-seq [opt]
15976        exception-specification [opt]
15977      direct-declarator [ constant-expression [opt] ]
15978      ( declarator )
15979
15980    direct-abstract-declarator:
15981      direct-abstract-declarator [opt]
15982        ( parameter-declaration-clause )
15983        cv-qualifier-seq [opt]
15984        exception-specification [opt]
15985      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15986      ( abstract-declarator )
15987
15988    Returns a representation of the declarator.  DCL_KIND is
15989    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15990    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15991    we are parsing a direct-declarator.  It is
15992    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15993    of ambiguity we prefer an abstract declarator, as per
15994    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15995    cp_parser_declarator.  */
15996
15997 static cp_declarator *
15998 cp_parser_direct_declarator (cp_parser* parser,
15999                              cp_parser_declarator_kind dcl_kind,
16000                              int* ctor_dtor_or_conv_p,
16001                              bool member_p)
16002 {
16003   cp_token *token;
16004   cp_declarator *declarator = NULL;
16005   tree scope = NULL_TREE;
16006   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16007   bool saved_in_declarator_p = parser->in_declarator_p;
16008   bool first = true;
16009   tree pushed_scope = NULL_TREE;
16010
16011   while (true)
16012     {
16013       /* Peek at the next token.  */
16014       token = cp_lexer_peek_token (parser->lexer);
16015       if (token->type == CPP_OPEN_PAREN)
16016         {
16017           /* This is either a parameter-declaration-clause, or a
16018              parenthesized declarator. When we know we are parsing a
16019              named declarator, it must be a parenthesized declarator
16020              if FIRST is true. For instance, `(int)' is a
16021              parameter-declaration-clause, with an omitted
16022              direct-abstract-declarator. But `((*))', is a
16023              parenthesized abstract declarator. Finally, when T is a
16024              template parameter `(T)' is a
16025              parameter-declaration-clause, and not a parenthesized
16026              named declarator.
16027
16028              We first try and parse a parameter-declaration-clause,
16029              and then try a nested declarator (if FIRST is true).
16030
16031              It is not an error for it not to be a
16032              parameter-declaration-clause, even when FIRST is
16033              false. Consider,
16034
16035                int i (int);
16036                int i (3);
16037
16038              The first is the declaration of a function while the
16039              second is the definition of a variable, including its
16040              initializer.
16041
16042              Having seen only the parenthesis, we cannot know which of
16043              these two alternatives should be selected.  Even more
16044              complex are examples like:
16045
16046                int i (int (a));
16047                int i (int (3));
16048
16049              The former is a function-declaration; the latter is a
16050              variable initialization.
16051
16052              Thus again, we try a parameter-declaration-clause, and if
16053              that fails, we back out and return.  */
16054
16055           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16056             {
16057               tree params;
16058               unsigned saved_num_template_parameter_lists;
16059               bool is_declarator = false;
16060               tree t;
16061
16062               /* In a member-declarator, the only valid interpretation
16063                  of a parenthesis is the start of a
16064                  parameter-declaration-clause.  (It is invalid to
16065                  initialize a static data member with a parenthesized
16066                  initializer; only the "=" form of initialization is
16067                  permitted.)  */
16068               if (!member_p)
16069                 cp_parser_parse_tentatively (parser);
16070
16071               /* Consume the `('.  */
16072               cp_lexer_consume_token (parser->lexer);
16073               if (first)
16074                 {
16075                   /* If this is going to be an abstract declarator, we're
16076                      in a declarator and we can't have default args.  */
16077                   parser->default_arg_ok_p = false;
16078                   parser->in_declarator_p = true;
16079                 }
16080
16081               /* Inside the function parameter list, surrounding
16082                  template-parameter-lists do not apply.  */
16083               saved_num_template_parameter_lists
16084                 = parser->num_template_parameter_lists;
16085               parser->num_template_parameter_lists = 0;
16086
16087               begin_scope (sk_function_parms, NULL_TREE);
16088
16089               /* Parse the parameter-declaration-clause.  */
16090               params = cp_parser_parameter_declaration_clause (parser);
16091
16092               parser->num_template_parameter_lists
16093                 = saved_num_template_parameter_lists;
16094
16095               /* Consume the `)'.  */
16096               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16097
16098               /* If all went well, parse the cv-qualifier-seq and the
16099                  exception-specification.  */
16100               if (member_p || cp_parser_parse_definitely (parser))
16101                 {
16102                   cp_cv_quals cv_quals;
16103                   cp_virt_specifiers virt_specifiers;
16104                   tree exception_specification;
16105                   tree late_return;
16106
16107                   is_declarator = true;
16108
16109                   if (ctor_dtor_or_conv_p)
16110                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16111                   first = false;
16112
16113                   /* Parse the cv-qualifier-seq.  */
16114                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16115                   /* And the exception-specification.  */
16116                   exception_specification
16117                     = cp_parser_exception_specification_opt (parser);
16118
16119                   late_return = (cp_parser_late_return_type_opt
16120                                  (parser, member_p ? cv_quals : -1));
16121
16122                   /* Parse the virt-specifier-seq.  */
16123                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16124
16125                   /* Create the function-declarator.  */
16126                   declarator = make_call_declarator (declarator,
16127                                                      params,
16128                                                      cv_quals,
16129                                                      virt_specifiers,
16130                                                      exception_specification,
16131                                                      late_return);
16132                   /* Any subsequent parameter lists are to do with
16133                      return type, so are not those of the declared
16134                      function.  */
16135                   parser->default_arg_ok_p = false;
16136                 }
16137
16138               /* Remove the function parms from scope.  */
16139               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16140                 pop_binding (DECL_NAME (t), t);
16141               leave_scope();
16142
16143               if (is_declarator)
16144                 /* Repeat the main loop.  */
16145                 continue;
16146             }
16147
16148           /* If this is the first, we can try a parenthesized
16149              declarator.  */
16150           if (first)
16151             {
16152               bool saved_in_type_id_in_expr_p;
16153
16154               parser->default_arg_ok_p = saved_default_arg_ok_p;
16155               parser->in_declarator_p = saved_in_declarator_p;
16156
16157               /* Consume the `('.  */
16158               cp_lexer_consume_token (parser->lexer);
16159               /* Parse the nested declarator.  */
16160               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16161               parser->in_type_id_in_expr_p = true;
16162               declarator
16163                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16164                                         /*parenthesized_p=*/NULL,
16165                                         member_p);
16166               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16167               first = false;
16168               /* Expect a `)'.  */
16169               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16170                 declarator = cp_error_declarator;
16171               if (declarator == cp_error_declarator)
16172                 break;
16173
16174               goto handle_declarator;
16175             }
16176           /* Otherwise, we must be done.  */
16177           else
16178             break;
16179         }
16180       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16181                && token->type == CPP_OPEN_SQUARE)
16182         {
16183           /* Parse an array-declarator.  */
16184           tree bounds;
16185
16186           if (ctor_dtor_or_conv_p)
16187             *ctor_dtor_or_conv_p = 0;
16188
16189           first = false;
16190           parser->default_arg_ok_p = false;
16191           parser->in_declarator_p = true;
16192           /* Consume the `['.  */
16193           cp_lexer_consume_token (parser->lexer);
16194           /* Peek at the next token.  */
16195           token = cp_lexer_peek_token (parser->lexer);
16196           /* If the next token is `]', then there is no
16197              constant-expression.  */
16198           if (token->type != CPP_CLOSE_SQUARE)
16199             {
16200               bool non_constant_p;
16201
16202               bounds
16203                 = cp_parser_constant_expression (parser,
16204                                                  /*allow_non_constant=*/true,
16205                                                  &non_constant_p);
16206               if (!non_constant_p)
16207                 /* OK */;
16208               else if (error_operand_p (bounds))
16209                 /* Already gave an error.  */;
16210               else if (!parser->in_function_body
16211                        || current_binding_level->kind == sk_function_parms)
16212                 {
16213                   /* Normally, the array bound must be an integral constant
16214                      expression.  However, as an extension, we allow VLAs
16215                      in function scopes as long as they aren't part of a
16216                      parameter declaration.  */
16217                   cp_parser_error (parser,
16218                                    "array bound is not an integer constant");
16219                   bounds = error_mark_node;
16220                 }
16221               else if (processing_template_decl)
16222                 {
16223                   /* Remember this wasn't a constant-expression.  */
16224                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16225                   TREE_SIDE_EFFECTS (bounds) = 1;
16226                 }
16227             }
16228           else
16229             bounds = NULL_TREE;
16230           /* Look for the closing `]'.  */
16231           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16232             {
16233               declarator = cp_error_declarator;
16234               break;
16235             }
16236
16237           declarator = make_array_declarator (declarator, bounds);
16238         }
16239       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16240         {
16241           {
16242             tree qualifying_scope;
16243             tree unqualified_name;
16244             special_function_kind sfk;
16245             bool abstract_ok;
16246             bool pack_expansion_p = false;
16247             cp_token *declarator_id_start_token;
16248
16249             /* Parse a declarator-id */
16250             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16251             if (abstract_ok)
16252               {
16253                 cp_parser_parse_tentatively (parser);
16254
16255                 /* If we see an ellipsis, we should be looking at a
16256                    parameter pack. */
16257                 if (token->type == CPP_ELLIPSIS)
16258                   {
16259                     /* Consume the `...' */
16260                     cp_lexer_consume_token (parser->lexer);
16261
16262                     pack_expansion_p = true;
16263                   }
16264               }
16265
16266             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16267             unqualified_name
16268               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16269             qualifying_scope = parser->scope;
16270             if (abstract_ok)
16271               {
16272                 bool okay = false;
16273
16274                 if (!unqualified_name && pack_expansion_p)
16275                   {
16276                     /* Check whether an error occurred. */
16277                     okay = !cp_parser_error_occurred (parser);
16278
16279                     /* We already consumed the ellipsis to mark a
16280                        parameter pack, but we have no way to report it,
16281                        so abort the tentative parse. We will be exiting
16282                        immediately anyway. */
16283                     cp_parser_abort_tentative_parse (parser);
16284                   }
16285                 else
16286                   okay = cp_parser_parse_definitely (parser);
16287
16288                 if (!okay)
16289                   unqualified_name = error_mark_node;
16290                 else if (unqualified_name
16291                          && (qualifying_scope
16292                              || (TREE_CODE (unqualified_name)
16293                                  != IDENTIFIER_NODE)))
16294                   {
16295                     cp_parser_error (parser, "expected unqualified-id");
16296                     unqualified_name = error_mark_node;
16297                   }
16298               }
16299
16300             if (!unqualified_name)
16301               return NULL;
16302             if (unqualified_name == error_mark_node)
16303               {
16304                 declarator = cp_error_declarator;
16305                 pack_expansion_p = false;
16306                 declarator->parameter_pack_p = false;
16307                 break;
16308               }
16309
16310             if (qualifying_scope && at_namespace_scope_p ()
16311                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16312               {
16313                 /* In the declaration of a member of a template class
16314                    outside of the class itself, the SCOPE will sometimes
16315                    be a TYPENAME_TYPE.  For example, given:
16316
16317                    template <typename T>
16318                    int S<T>::R::i = 3;
16319
16320                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16321                    this context, we must resolve S<T>::R to an ordinary
16322                    type, rather than a typename type.
16323
16324                    The reason we normally avoid resolving TYPENAME_TYPEs
16325                    is that a specialization of `S' might render
16326                    `S<T>::R' not a type.  However, if `S' is
16327                    specialized, then this `i' will not be used, so there
16328                    is no harm in resolving the types here.  */
16329                 tree type;
16330
16331                 /* Resolve the TYPENAME_TYPE.  */
16332                 type = resolve_typename_type (qualifying_scope,
16333                                               /*only_current_p=*/false);
16334                 /* If that failed, the declarator is invalid.  */
16335                 if (TREE_CODE (type) == TYPENAME_TYPE)
16336                   {
16337                     if (typedef_variant_p (type))
16338                       error_at (declarator_id_start_token->location,
16339                                 "cannot define member of dependent typedef "
16340                                 "%qT", type);
16341                     else
16342                       error_at (declarator_id_start_token->location,
16343                                 "%<%T::%E%> is not a type",
16344                                 TYPE_CONTEXT (qualifying_scope),
16345                                 TYPE_IDENTIFIER (qualifying_scope));
16346                   }
16347                 qualifying_scope = type;
16348               }
16349
16350             sfk = sfk_none;
16351
16352             if (unqualified_name)
16353               {
16354                 tree class_type;
16355
16356                 if (qualifying_scope
16357                     && CLASS_TYPE_P (qualifying_scope))
16358                   class_type = qualifying_scope;
16359                 else
16360                   class_type = current_class_type;
16361
16362                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16363                   {
16364                     tree name_type = TREE_TYPE (unqualified_name);
16365                     if (class_type && same_type_p (name_type, class_type))
16366                       {
16367                         if (qualifying_scope
16368                             && CLASSTYPE_USE_TEMPLATE (name_type))
16369                           {
16370                             error_at (declarator_id_start_token->location,
16371                                       "invalid use of constructor as a template");
16372                             inform (declarator_id_start_token->location,
16373                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16374                                     "name the constructor in a qualified name",
16375                                     class_type,
16376                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16377                                     class_type, name_type);
16378                             declarator = cp_error_declarator;
16379                             break;
16380                           }
16381                         else
16382                           unqualified_name = constructor_name (class_type);
16383                       }
16384                     else
16385                       {
16386                         /* We do not attempt to print the declarator
16387                            here because we do not have enough
16388                            information about its original syntactic
16389                            form.  */
16390                         cp_parser_error (parser, "invalid declarator");
16391                         declarator = cp_error_declarator;
16392                         break;
16393                       }
16394                   }
16395
16396                 if (class_type)
16397                   {
16398                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16399                       sfk = sfk_destructor;
16400                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16401                       sfk = sfk_conversion;
16402                     else if (/* There's no way to declare a constructor
16403                                 for an anonymous type, even if the type
16404                                 got a name for linkage purposes.  */
16405                              !TYPE_WAS_ANONYMOUS (class_type)
16406                              && constructor_name_p (unqualified_name,
16407                                                     class_type))
16408                       {
16409                         unqualified_name = constructor_name (class_type);
16410                         sfk = sfk_constructor;
16411                       }
16412                     else if (is_overloaded_fn (unqualified_name)
16413                              && DECL_CONSTRUCTOR_P (get_first_fn
16414                                                     (unqualified_name)))
16415                       sfk = sfk_constructor;
16416
16417                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16418                       *ctor_dtor_or_conv_p = -1;
16419                   }
16420               }
16421             declarator = make_id_declarator (qualifying_scope,
16422                                              unqualified_name,
16423                                              sfk);
16424             declarator->id_loc = token->location;
16425             declarator->parameter_pack_p = pack_expansion_p;
16426
16427             if (pack_expansion_p)
16428               maybe_warn_variadic_templates ();
16429           }
16430
16431         handle_declarator:;
16432           scope = get_scope_of_declarator (declarator);
16433           if (scope)
16434             /* Any names that appear after the declarator-id for a
16435                member are looked up in the containing scope.  */
16436             pushed_scope = push_scope (scope);
16437           parser->in_declarator_p = true;
16438           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16439               || (declarator && declarator->kind == cdk_id))
16440             /* Default args are only allowed on function
16441                declarations.  */
16442             parser->default_arg_ok_p = saved_default_arg_ok_p;
16443           else
16444             parser->default_arg_ok_p = false;
16445
16446           first = false;
16447         }
16448       /* We're done.  */
16449       else
16450         break;
16451     }
16452
16453   /* For an abstract declarator, we might wind up with nothing at this
16454      point.  That's an error; the declarator is not optional.  */
16455   if (!declarator)
16456     cp_parser_error (parser, "expected declarator");
16457
16458   /* If we entered a scope, we must exit it now.  */
16459   if (pushed_scope)
16460     pop_scope (pushed_scope);
16461
16462   parser->default_arg_ok_p = saved_default_arg_ok_p;
16463   parser->in_declarator_p = saved_in_declarator_p;
16464
16465   return declarator;
16466 }
16467
16468 /* Parse a ptr-operator.
16469
16470    ptr-operator:
16471      * cv-qualifier-seq [opt]
16472      &
16473      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16474
16475    GNU Extension:
16476
16477    ptr-operator:
16478      & cv-qualifier-seq [opt]
16479
16480    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16481    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16482    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16483    filled in with the TYPE containing the member.  *CV_QUALS is
16484    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16485    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16486    Note that the tree codes returned by this function have nothing
16487    to do with the types of trees that will be eventually be created
16488    to represent the pointer or reference type being parsed. They are
16489    just constants with suggestive names. */
16490 static enum tree_code
16491 cp_parser_ptr_operator (cp_parser* parser,
16492                         tree* type,
16493                         cp_cv_quals *cv_quals)
16494 {
16495   enum tree_code code = ERROR_MARK;
16496   cp_token *token;
16497
16498   /* Assume that it's not a pointer-to-member.  */
16499   *type = NULL_TREE;
16500   /* And that there are no cv-qualifiers.  */
16501   *cv_quals = TYPE_UNQUALIFIED;
16502
16503   /* Peek at the next token.  */
16504   token = cp_lexer_peek_token (parser->lexer);
16505
16506   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16507   if (token->type == CPP_MULT)
16508     code = INDIRECT_REF;
16509   else if (token->type == CPP_AND)
16510     code = ADDR_EXPR;
16511   else if ((cxx_dialect != cxx98) &&
16512            token->type == CPP_AND_AND) /* C++0x only */
16513     code = NON_LVALUE_EXPR;
16514
16515   if (code != ERROR_MARK)
16516     {
16517       /* Consume the `*', `&' or `&&'.  */
16518       cp_lexer_consume_token (parser->lexer);
16519
16520       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16521          `&', if we are allowing GNU extensions.  (The only qualifier
16522          that can legally appear after `&' is `restrict', but that is
16523          enforced during semantic analysis.  */
16524       if (code == INDIRECT_REF
16525           || cp_parser_allow_gnu_extensions_p (parser))
16526         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16527     }
16528   else
16529     {
16530       /* Try the pointer-to-member case.  */
16531       cp_parser_parse_tentatively (parser);
16532       /* Look for the optional `::' operator.  */
16533       cp_parser_global_scope_opt (parser,
16534                                   /*current_scope_valid_p=*/false);
16535       /* Look for the nested-name specifier.  */
16536       token = cp_lexer_peek_token (parser->lexer);
16537       cp_parser_nested_name_specifier (parser,
16538                                        /*typename_keyword_p=*/false,
16539                                        /*check_dependency_p=*/true,
16540                                        /*type_p=*/false,
16541                                        /*is_declaration=*/false);
16542       /* If we found it, and the next token is a `*', then we are
16543          indeed looking at a pointer-to-member operator.  */
16544       if (!cp_parser_error_occurred (parser)
16545           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16546         {
16547           /* Indicate that the `*' operator was used.  */
16548           code = INDIRECT_REF;
16549
16550           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16551             error_at (token->location, "%qD is a namespace", parser->scope);
16552           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16553             error_at (token->location, "cannot form pointer to member of "
16554                       "non-class %q#T", parser->scope);
16555           else
16556             {
16557               /* The type of which the member is a member is given by the
16558                  current SCOPE.  */
16559               *type = parser->scope;
16560               /* The next name will not be qualified.  */
16561               parser->scope = NULL_TREE;
16562               parser->qualifying_scope = NULL_TREE;
16563               parser->object_scope = NULL_TREE;
16564               /* Look for the optional cv-qualifier-seq.  */
16565               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16566             }
16567         }
16568       /* If that didn't work we don't have a ptr-operator.  */
16569       if (!cp_parser_parse_definitely (parser))
16570         cp_parser_error (parser, "expected ptr-operator");
16571     }
16572
16573   return code;
16574 }
16575
16576 /* Parse an (optional) cv-qualifier-seq.
16577
16578    cv-qualifier-seq:
16579      cv-qualifier cv-qualifier-seq [opt]
16580
16581    cv-qualifier:
16582      const
16583      volatile
16584
16585    GNU Extension:
16586
16587    cv-qualifier:
16588      __restrict__
16589
16590    Returns a bitmask representing the cv-qualifiers.  */
16591
16592 static cp_cv_quals
16593 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16594 {
16595   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16596
16597   while (true)
16598     {
16599       cp_token *token;
16600       cp_cv_quals cv_qualifier;
16601
16602       /* Peek at the next token.  */
16603       token = cp_lexer_peek_token (parser->lexer);
16604       /* See if it's a cv-qualifier.  */
16605       switch (token->keyword)
16606         {
16607         case RID_CONST:
16608           cv_qualifier = TYPE_QUAL_CONST;
16609           break;
16610
16611         case RID_VOLATILE:
16612           cv_qualifier = TYPE_QUAL_VOLATILE;
16613           break;
16614
16615         case RID_RESTRICT:
16616           cv_qualifier = TYPE_QUAL_RESTRICT;
16617           break;
16618
16619         default:
16620           cv_qualifier = TYPE_UNQUALIFIED;
16621           break;
16622         }
16623
16624       if (!cv_qualifier)
16625         break;
16626
16627       if (cv_quals & cv_qualifier)
16628         {
16629           error_at (token->location, "duplicate cv-qualifier");
16630           cp_lexer_purge_token (parser->lexer);
16631         }
16632       else
16633         {
16634           cp_lexer_consume_token (parser->lexer);
16635           cv_quals |= cv_qualifier;
16636         }
16637     }
16638
16639   return cv_quals;
16640 }
16641
16642 /* Parse an (optional) virt-specifier-seq.
16643
16644    virt-specifier-seq:
16645      virt-specifier virt-specifier-seq [opt]
16646
16647    virt-specifier:
16648      override
16649      final
16650
16651    Returns a bitmask representing the virt-specifiers.  */
16652
16653 static cp_virt_specifiers
16654 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16655 {
16656   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16657
16658   while (true)
16659     {
16660       cp_token *token;
16661       cp_virt_specifiers virt_specifier;
16662
16663       /* Peek at the next token.  */
16664       token = cp_lexer_peek_token (parser->lexer);
16665       /* See if it's a virt-specifier-qualifier.  */
16666       if (token->type != CPP_NAME)
16667         break;
16668       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16669         {
16670           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16671           virt_specifier = VIRT_SPEC_OVERRIDE;
16672         }
16673       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16674         {
16675           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16676           virt_specifier = VIRT_SPEC_FINAL;
16677         }
16678       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16679         {
16680           virt_specifier = VIRT_SPEC_FINAL;
16681         }
16682       else
16683         break;
16684
16685       if (virt_specifiers & virt_specifier)
16686         {
16687           error_at (token->location, "duplicate virt-specifier");
16688           cp_lexer_purge_token (parser->lexer);
16689         }
16690       else
16691         {
16692           cp_lexer_consume_token (parser->lexer);
16693           virt_specifiers |= virt_specifier;
16694         }
16695     }
16696   return virt_specifiers;
16697 }
16698
16699 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16700    is in scope even though it isn't real.  */
16701
16702 static void
16703 inject_this_parameter (tree ctype, cp_cv_quals quals)
16704 {
16705   tree this_parm;
16706
16707   if (current_class_ptr)
16708     {
16709       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16710       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16711       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16712           && cp_type_quals (type) == quals)
16713         return;
16714     }
16715
16716   this_parm = build_this_parm (ctype, quals);
16717   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16718   current_class_ptr = NULL_TREE;
16719   current_class_ref
16720     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16721   current_class_ptr = this_parm;
16722 }
16723
16724 /* Parse a late-specified return type, if any.  This is not a separate
16725    non-terminal, but part of a function declarator, which looks like
16726
16727    -> trailing-type-specifier-seq abstract-declarator(opt)
16728
16729    Returns the type indicated by the type-id.
16730
16731    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16732    function.  */
16733
16734 static tree
16735 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16736 {
16737   cp_token *token;
16738   tree type;
16739
16740   /* Peek at the next token.  */
16741   token = cp_lexer_peek_token (parser->lexer);
16742   /* A late-specified return type is indicated by an initial '->'. */
16743   if (token->type != CPP_DEREF)
16744     return NULL_TREE;
16745
16746   /* Consume the ->.  */
16747   cp_lexer_consume_token (parser->lexer);
16748
16749   if (quals >= 0)
16750     {
16751       /* DR 1207: 'this' is in scope in the trailing return type.  */
16752       gcc_assert (current_class_ptr == NULL_TREE);
16753       inject_this_parameter (current_class_type, quals);
16754     }
16755
16756   type = cp_parser_trailing_type_id (parser);
16757
16758   if (quals >= 0)
16759     current_class_ptr = current_class_ref = NULL_TREE;
16760
16761   return type;
16762 }
16763
16764 /* Parse a declarator-id.
16765
16766    declarator-id:
16767      id-expression
16768      :: [opt] nested-name-specifier [opt] type-name
16769
16770    In the `id-expression' case, the value returned is as for
16771    cp_parser_id_expression if the id-expression was an unqualified-id.
16772    If the id-expression was a qualified-id, then a SCOPE_REF is
16773    returned.  The first operand is the scope (either a NAMESPACE_DECL
16774    or TREE_TYPE), but the second is still just a representation of an
16775    unqualified-id.  */
16776
16777 static tree
16778 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16779 {
16780   tree id;
16781   /* The expression must be an id-expression.  Assume that qualified
16782      names are the names of types so that:
16783
16784        template <class T>
16785        int S<T>::R::i = 3;
16786
16787      will work; we must treat `S<T>::R' as the name of a type.
16788      Similarly, assume that qualified names are templates, where
16789      required, so that:
16790
16791        template <class T>
16792        int S<T>::R<T>::i = 3;
16793
16794      will work, too.  */
16795   id = cp_parser_id_expression (parser,
16796                                 /*template_keyword_p=*/false,
16797                                 /*check_dependency_p=*/false,
16798                                 /*template_p=*/NULL,
16799                                 /*declarator_p=*/true,
16800                                 optional_p);
16801   if (id && BASELINK_P (id))
16802     id = BASELINK_FUNCTIONS (id);
16803   return id;
16804 }
16805
16806 /* Parse a type-id.
16807
16808    type-id:
16809      type-specifier-seq abstract-declarator [opt]
16810
16811    Returns the TYPE specified.  */
16812
16813 static tree
16814 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16815                      bool is_trailing_return)
16816 {
16817   cp_decl_specifier_seq type_specifier_seq;
16818   cp_declarator *abstract_declarator;
16819
16820   /* Parse the type-specifier-seq.  */
16821   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16822                                 is_trailing_return,
16823                                 &type_specifier_seq);
16824   if (type_specifier_seq.type == error_mark_node)
16825     return error_mark_node;
16826
16827   /* There might or might not be an abstract declarator.  */
16828   cp_parser_parse_tentatively (parser);
16829   /* Look for the declarator.  */
16830   abstract_declarator
16831     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16832                             /*parenthesized_p=*/NULL,
16833                             /*member_p=*/false);
16834   /* Check to see if there really was a declarator.  */
16835   if (!cp_parser_parse_definitely (parser))
16836     abstract_declarator = NULL;
16837
16838   if (type_specifier_seq.type
16839       && type_uses_auto (type_specifier_seq.type))
16840     {
16841       /* A type-id with type 'auto' is only ok if the abstract declarator
16842          is a function declarator with a late-specified return type.  */
16843       if (abstract_declarator
16844           && abstract_declarator->kind == cdk_function
16845           && abstract_declarator->u.function.late_return_type)
16846         /* OK */;
16847       else
16848         {
16849           error ("invalid use of %<auto%>");
16850           return error_mark_node;
16851         }
16852     }
16853   
16854   return groktypename (&type_specifier_seq, abstract_declarator,
16855                        is_template_arg);
16856 }
16857
16858 static tree cp_parser_type_id (cp_parser *parser)
16859 {
16860   return cp_parser_type_id_1 (parser, false, false);
16861 }
16862
16863 static tree cp_parser_template_type_arg (cp_parser *parser)
16864 {
16865   tree r;
16866   const char *saved_message = parser->type_definition_forbidden_message;
16867   parser->type_definition_forbidden_message
16868     = G_("types may not be defined in template arguments");
16869   r = cp_parser_type_id_1 (parser, true, false);
16870   parser->type_definition_forbidden_message = saved_message;
16871   return r;
16872 }
16873
16874 static tree cp_parser_trailing_type_id (cp_parser *parser)
16875 {
16876   return cp_parser_type_id_1 (parser, false, true);
16877 }
16878
16879 /* Parse a type-specifier-seq.
16880
16881    type-specifier-seq:
16882      type-specifier type-specifier-seq [opt]
16883
16884    GNU extension:
16885
16886    type-specifier-seq:
16887      attributes type-specifier-seq [opt]
16888
16889    If IS_DECLARATION is true, we are at the start of a "condition" or
16890    exception-declaration, so we might be followed by a declarator-id.
16891
16892    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16893    i.e. we've just seen "->".
16894
16895    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16896
16897 static void
16898 cp_parser_type_specifier_seq (cp_parser* parser,
16899                               bool is_declaration,
16900                               bool is_trailing_return,
16901                               cp_decl_specifier_seq *type_specifier_seq)
16902 {
16903   bool seen_type_specifier = false;
16904   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16905   cp_token *start_token = NULL;
16906
16907   /* Clear the TYPE_SPECIFIER_SEQ.  */
16908   clear_decl_specs (type_specifier_seq);
16909
16910   /* In the context of a trailing return type, enum E { } is an
16911      elaborated-type-specifier followed by a function-body, not an
16912      enum-specifier.  */
16913   if (is_trailing_return)
16914     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16915
16916   /* Parse the type-specifiers and attributes.  */
16917   while (true)
16918     {
16919       tree type_specifier;
16920       bool is_cv_qualifier;
16921
16922       /* Check for attributes first.  */
16923       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16924         {
16925           type_specifier_seq->attributes =
16926             chainon (type_specifier_seq->attributes,
16927                      cp_parser_attributes_opt (parser));
16928           continue;
16929         }
16930
16931       /* record the token of the beginning of the type specifier seq,
16932          for error reporting purposes*/
16933      if (!start_token)
16934        start_token = cp_lexer_peek_token (parser->lexer);
16935
16936       /* Look for the type-specifier.  */
16937       type_specifier = cp_parser_type_specifier (parser,
16938                                                  flags,
16939                                                  type_specifier_seq,
16940                                                  /*is_declaration=*/false,
16941                                                  NULL,
16942                                                  &is_cv_qualifier);
16943       if (!type_specifier)
16944         {
16945           /* If the first type-specifier could not be found, this is not a
16946              type-specifier-seq at all.  */
16947           if (!seen_type_specifier)
16948             {
16949               cp_parser_error (parser, "expected type-specifier");
16950               type_specifier_seq->type = error_mark_node;
16951               return;
16952             }
16953           /* If subsequent type-specifiers could not be found, the
16954              type-specifier-seq is complete.  */
16955           break;
16956         }
16957
16958       seen_type_specifier = true;
16959       /* The standard says that a condition can be:
16960
16961             type-specifier-seq declarator = assignment-expression
16962
16963          However, given:
16964
16965            struct S {};
16966            if (int S = ...)
16967
16968          we should treat the "S" as a declarator, not as a
16969          type-specifier.  The standard doesn't say that explicitly for
16970          type-specifier-seq, but it does say that for
16971          decl-specifier-seq in an ordinary declaration.  Perhaps it
16972          would be clearer just to allow a decl-specifier-seq here, and
16973          then add a semantic restriction that if any decl-specifiers
16974          that are not type-specifiers appear, the program is invalid.  */
16975       if (is_declaration && !is_cv_qualifier)
16976         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16977     }
16978 }
16979
16980 /* Parse a parameter-declaration-clause.
16981
16982    parameter-declaration-clause:
16983      parameter-declaration-list [opt] ... [opt]
16984      parameter-declaration-list , ...
16985
16986    Returns a representation for the parameter declarations.  A return
16987    value of NULL indicates a parameter-declaration-clause consisting
16988    only of an ellipsis.  */
16989
16990 static tree
16991 cp_parser_parameter_declaration_clause (cp_parser* parser)
16992 {
16993   tree parameters;
16994   cp_token *token;
16995   bool ellipsis_p;
16996   bool is_error;
16997
16998   /* Peek at the next token.  */
16999   token = cp_lexer_peek_token (parser->lexer);
17000   /* Check for trivial parameter-declaration-clauses.  */
17001   if (token->type == CPP_ELLIPSIS)
17002     {
17003       /* Consume the `...' token.  */
17004       cp_lexer_consume_token (parser->lexer);
17005       return NULL_TREE;
17006     }
17007   else if (token->type == CPP_CLOSE_PAREN)
17008     /* There are no parameters.  */
17009     {
17010 #ifndef NO_IMPLICIT_EXTERN_C
17011       if (in_system_header && current_class_type == NULL
17012           && current_lang_name == lang_name_c)
17013         return NULL_TREE;
17014       else
17015 #endif
17016         return void_list_node;
17017     }
17018   /* Check for `(void)', too, which is a special case.  */
17019   else if (token->keyword == RID_VOID
17020            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17021                == CPP_CLOSE_PAREN))
17022     {
17023       /* Consume the `void' token.  */
17024       cp_lexer_consume_token (parser->lexer);
17025       /* There are no parameters.  */
17026       return void_list_node;
17027     }
17028
17029   /* Parse the parameter-declaration-list.  */
17030   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17031   /* If a parse error occurred while parsing the
17032      parameter-declaration-list, then the entire
17033      parameter-declaration-clause is erroneous.  */
17034   if (is_error)
17035     return NULL;
17036
17037   /* Peek at the next token.  */
17038   token = cp_lexer_peek_token (parser->lexer);
17039   /* If it's a `,', the clause should terminate with an ellipsis.  */
17040   if (token->type == CPP_COMMA)
17041     {
17042       /* Consume the `,'.  */
17043       cp_lexer_consume_token (parser->lexer);
17044       /* Expect an ellipsis.  */
17045       ellipsis_p
17046         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17047     }
17048   /* It might also be `...' if the optional trailing `,' was
17049      omitted.  */
17050   else if (token->type == CPP_ELLIPSIS)
17051     {
17052       /* Consume the `...' token.  */
17053       cp_lexer_consume_token (parser->lexer);
17054       /* And remember that we saw it.  */
17055       ellipsis_p = true;
17056     }
17057   else
17058     ellipsis_p = false;
17059
17060   /* Finish the parameter list.  */
17061   if (!ellipsis_p)
17062     parameters = chainon (parameters, void_list_node);
17063
17064   return parameters;
17065 }
17066
17067 /* Parse a parameter-declaration-list.
17068
17069    parameter-declaration-list:
17070      parameter-declaration
17071      parameter-declaration-list , parameter-declaration
17072
17073    Returns a representation of the parameter-declaration-list, as for
17074    cp_parser_parameter_declaration_clause.  However, the
17075    `void_list_node' is never appended to the list.  Upon return,
17076    *IS_ERROR will be true iff an error occurred.  */
17077
17078 static tree
17079 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17080 {
17081   tree parameters = NULL_TREE;
17082   tree *tail = &parameters; 
17083   bool saved_in_unbraced_linkage_specification_p;
17084   int index = 0;
17085
17086   /* Assume all will go well.  */
17087   *is_error = false;
17088   /* The special considerations that apply to a function within an
17089      unbraced linkage specifications do not apply to the parameters
17090      to the function.  */
17091   saved_in_unbraced_linkage_specification_p 
17092     = parser->in_unbraced_linkage_specification_p;
17093   parser->in_unbraced_linkage_specification_p = false;
17094
17095   /* Look for more parameters.  */
17096   while (true)
17097     {
17098       cp_parameter_declarator *parameter;
17099       tree decl = error_mark_node;
17100       bool parenthesized_p = false;
17101       /* Parse the parameter.  */
17102       parameter
17103         = cp_parser_parameter_declaration (parser,
17104                                            /*template_parm_p=*/false,
17105                                            &parenthesized_p);
17106
17107       /* We don't know yet if the enclosing context is deprecated, so wait
17108          and warn in grokparms if appropriate.  */
17109       deprecated_state = DEPRECATED_SUPPRESS;
17110
17111       if (parameter)
17112         decl = grokdeclarator (parameter->declarator,
17113                                &parameter->decl_specifiers,
17114                                PARM,
17115                                parameter->default_argument != NULL_TREE,
17116                                &parameter->decl_specifiers.attributes);
17117
17118       deprecated_state = DEPRECATED_NORMAL;
17119
17120       /* If a parse error occurred parsing the parameter declaration,
17121          then the entire parameter-declaration-list is erroneous.  */
17122       if (decl == error_mark_node)
17123         {
17124           *is_error = true;
17125           parameters = error_mark_node;
17126           break;
17127         }
17128
17129       if (parameter->decl_specifiers.attributes)
17130         cplus_decl_attributes (&decl,
17131                                parameter->decl_specifiers.attributes,
17132                                0);
17133       if (DECL_NAME (decl))
17134         decl = pushdecl (decl);
17135
17136       if (decl != error_mark_node)
17137         {
17138           retrofit_lang_decl (decl);
17139           DECL_PARM_INDEX (decl) = ++index;
17140           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17141         }
17142
17143       /* Add the new parameter to the list.  */
17144       *tail = build_tree_list (parameter->default_argument, decl);
17145       tail = &TREE_CHAIN (*tail);
17146
17147       /* Peek at the next token.  */
17148       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17149           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17150           /* These are for Objective-C++ */
17151           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17152           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17153         /* The parameter-declaration-list is complete.  */
17154         break;
17155       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17156         {
17157           cp_token *token;
17158
17159           /* Peek at the next token.  */
17160           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17161           /* If it's an ellipsis, then the list is complete.  */
17162           if (token->type == CPP_ELLIPSIS)
17163             break;
17164           /* Otherwise, there must be more parameters.  Consume the
17165              `,'.  */
17166           cp_lexer_consume_token (parser->lexer);
17167           /* When parsing something like:
17168
17169                 int i(float f, double d)
17170
17171              we can tell after seeing the declaration for "f" that we
17172              are not looking at an initialization of a variable "i",
17173              but rather at the declaration of a function "i".
17174
17175              Due to the fact that the parsing of template arguments
17176              (as specified to a template-id) requires backtracking we
17177              cannot use this technique when inside a template argument
17178              list.  */
17179           if (!parser->in_template_argument_list_p
17180               && !parser->in_type_id_in_expr_p
17181               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17182               /* However, a parameter-declaration of the form
17183                  "foat(f)" (which is a valid declaration of a
17184                  parameter "f") can also be interpreted as an
17185                  expression (the conversion of "f" to "float").  */
17186               && !parenthesized_p)
17187             cp_parser_commit_to_tentative_parse (parser);
17188         }
17189       else
17190         {
17191           cp_parser_error (parser, "expected %<,%> or %<...%>");
17192           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17193             cp_parser_skip_to_closing_parenthesis (parser,
17194                                                    /*recovering=*/true,
17195                                                    /*or_comma=*/false,
17196                                                    /*consume_paren=*/false);
17197           break;
17198         }
17199     }
17200
17201   parser->in_unbraced_linkage_specification_p
17202     = saved_in_unbraced_linkage_specification_p;
17203
17204   return parameters;
17205 }
17206
17207 /* Parse a parameter declaration.
17208
17209    parameter-declaration:
17210      decl-specifier-seq ... [opt] declarator
17211      decl-specifier-seq declarator = assignment-expression
17212      decl-specifier-seq ... [opt] abstract-declarator [opt]
17213      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17214
17215    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17216    declares a template parameter.  (In that case, a non-nested `>'
17217    token encountered during the parsing of the assignment-expression
17218    is not interpreted as a greater-than operator.)
17219
17220    Returns a representation of the parameter, or NULL if an error
17221    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17222    true iff the declarator is of the form "(p)".  */
17223
17224 static cp_parameter_declarator *
17225 cp_parser_parameter_declaration (cp_parser *parser,
17226                                  bool template_parm_p,
17227                                  bool *parenthesized_p)
17228 {
17229   int declares_class_or_enum;
17230   cp_decl_specifier_seq decl_specifiers;
17231   cp_declarator *declarator;
17232   tree default_argument;
17233   cp_token *token = NULL, *declarator_token_start = NULL;
17234   const char *saved_message;
17235
17236   /* In a template parameter, `>' is not an operator.
17237
17238      [temp.param]
17239
17240      When parsing a default template-argument for a non-type
17241      template-parameter, the first non-nested `>' is taken as the end
17242      of the template parameter-list rather than a greater-than
17243      operator.  */
17244
17245   /* Type definitions may not appear in parameter types.  */
17246   saved_message = parser->type_definition_forbidden_message;
17247   parser->type_definition_forbidden_message
17248     = G_("types may not be defined in parameter types");
17249
17250   /* Parse the declaration-specifiers.  */
17251   cp_parser_decl_specifier_seq (parser,
17252                                 CP_PARSER_FLAGS_NONE,
17253                                 &decl_specifiers,
17254                                 &declares_class_or_enum);
17255
17256   /* Complain about missing 'typename' or other invalid type names.  */
17257   if (!decl_specifiers.any_type_specifiers_p)
17258     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17259
17260   /* If an error occurred, there's no reason to attempt to parse the
17261      rest of the declaration.  */
17262   if (cp_parser_error_occurred (parser))
17263     {
17264       parser->type_definition_forbidden_message = saved_message;
17265       return NULL;
17266     }
17267
17268   /* Peek at the next token.  */
17269   token = cp_lexer_peek_token (parser->lexer);
17270
17271   /* If the next token is a `)', `,', `=', `>', or `...', then there
17272      is no declarator. However, when variadic templates are enabled,
17273      there may be a declarator following `...'.  */
17274   if (token->type == CPP_CLOSE_PAREN
17275       || token->type == CPP_COMMA
17276       || token->type == CPP_EQ
17277       || token->type == CPP_GREATER)
17278     {
17279       declarator = NULL;
17280       if (parenthesized_p)
17281         *parenthesized_p = false;
17282     }
17283   /* Otherwise, there should be a declarator.  */
17284   else
17285     {
17286       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17287       parser->default_arg_ok_p = false;
17288
17289       /* After seeing a decl-specifier-seq, if the next token is not a
17290          "(", there is no possibility that the code is a valid
17291          expression.  Therefore, if parsing tentatively, we commit at
17292          this point.  */
17293       if (!parser->in_template_argument_list_p
17294           /* In an expression context, having seen:
17295
17296                (int((char ...
17297
17298              we cannot be sure whether we are looking at a
17299              function-type (taking a "char" as a parameter) or a cast
17300              of some object of type "char" to "int".  */
17301           && !parser->in_type_id_in_expr_p
17302           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17303           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17304           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17305         cp_parser_commit_to_tentative_parse (parser);
17306       /* Parse the declarator.  */
17307       declarator_token_start = token;
17308       declarator = cp_parser_declarator (parser,
17309                                          CP_PARSER_DECLARATOR_EITHER,
17310                                          /*ctor_dtor_or_conv_p=*/NULL,
17311                                          parenthesized_p,
17312                                          /*member_p=*/false);
17313       parser->default_arg_ok_p = saved_default_arg_ok_p;
17314       /* After the declarator, allow more attributes.  */
17315       decl_specifiers.attributes
17316         = chainon (decl_specifiers.attributes,
17317                    cp_parser_attributes_opt (parser));
17318     }
17319
17320   /* If the next token is an ellipsis, and we have not seen a
17321      declarator name, and the type of the declarator contains parameter
17322      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17323      a parameter pack expansion expression. Otherwise, leave the
17324      ellipsis for a C-style variadic function. */
17325   token = cp_lexer_peek_token (parser->lexer);
17326   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17327     {
17328       tree type = decl_specifiers.type;
17329
17330       if (type && DECL_P (type))
17331         type = TREE_TYPE (type);
17332
17333       if (type
17334           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17335           && declarator_can_be_parameter_pack (declarator)
17336           && (!declarator || !declarator->parameter_pack_p)
17337           && uses_parameter_packs (type))
17338         {
17339           /* Consume the `...'. */
17340           cp_lexer_consume_token (parser->lexer);
17341           maybe_warn_variadic_templates ();
17342           
17343           /* Build a pack expansion type */
17344           if (declarator)
17345             declarator->parameter_pack_p = true;
17346           else
17347             decl_specifiers.type = make_pack_expansion (type);
17348         }
17349     }
17350
17351   /* The restriction on defining new types applies only to the type
17352      of the parameter, not to the default argument.  */
17353   parser->type_definition_forbidden_message = saved_message;
17354
17355   /* If the next token is `=', then process a default argument.  */
17356   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17357     {
17358       token = cp_lexer_peek_token (parser->lexer);
17359       /* If we are defining a class, then the tokens that make up the
17360          default argument must be saved and processed later.  */
17361       if (!template_parm_p && at_class_scope_p ()
17362           && TYPE_BEING_DEFINED (current_class_type)
17363           && !LAMBDA_TYPE_P (current_class_type))
17364         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17365       /* Outside of a class definition, we can just parse the
17366          assignment-expression.  */
17367       else
17368         default_argument
17369           = cp_parser_default_argument (parser, template_parm_p);
17370
17371       if (!parser->default_arg_ok_p)
17372         {
17373           if (flag_permissive)
17374             warning (0, "deprecated use of default argument for parameter of non-function");
17375           else
17376             {
17377               error_at (token->location,
17378                         "default arguments are only "
17379                         "permitted for function parameters");
17380               default_argument = NULL_TREE;
17381             }
17382         }
17383       else if ((declarator && declarator->parameter_pack_p)
17384                || (decl_specifiers.type
17385                    && PACK_EXPANSION_P (decl_specifiers.type)))
17386         {
17387           /* Find the name of the parameter pack.  */     
17388           cp_declarator *id_declarator = declarator;
17389           while (id_declarator && id_declarator->kind != cdk_id)
17390             id_declarator = id_declarator->declarator;
17391           
17392           if (id_declarator && id_declarator->kind == cdk_id)
17393             error_at (declarator_token_start->location,
17394                       template_parm_p
17395                       ? G_("template parameter pack %qD "
17396                            "cannot have a default argument")
17397                       : G_("parameter pack %qD cannot have "
17398                            "a default argument"),
17399                       id_declarator->u.id.unqualified_name);
17400           else
17401             error_at (declarator_token_start->location,
17402                       template_parm_p
17403                       ? G_("template parameter pack cannot have "
17404                            "a default argument")
17405                       : G_("parameter pack cannot have a "
17406                            "default argument"));
17407
17408           default_argument = NULL_TREE;
17409         }
17410     }
17411   else
17412     default_argument = NULL_TREE;
17413
17414   return make_parameter_declarator (&decl_specifiers,
17415                                     declarator,
17416                                     default_argument);
17417 }
17418
17419 /* Parse a default argument and return it.
17420
17421    TEMPLATE_PARM_P is true if this is a default argument for a
17422    non-type template parameter.  */
17423 static tree
17424 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17425 {
17426   tree default_argument = NULL_TREE;
17427   bool saved_greater_than_is_operator_p;
17428   bool saved_local_variables_forbidden_p;
17429   bool non_constant_p, is_direct_init;
17430
17431   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17432      set correctly.  */
17433   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17434   parser->greater_than_is_operator_p = !template_parm_p;
17435   /* Local variable names (and the `this' keyword) may not
17436      appear in a default argument.  */
17437   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17438   parser->local_variables_forbidden_p = true;
17439   /* Parse the assignment-expression.  */
17440   if (template_parm_p)
17441     push_deferring_access_checks (dk_no_deferred);
17442   default_argument
17443     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17444   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17445     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17446   if (template_parm_p)
17447     pop_deferring_access_checks ();
17448   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17449   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17450
17451   return default_argument;
17452 }
17453
17454 /* Parse a function-body.
17455
17456    function-body:
17457      compound_statement  */
17458
17459 static void
17460 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17461 {
17462   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17463 }
17464
17465 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17466    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
17467    is true we are parsing a function-try-block.  */
17468
17469 static bool
17470 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17471                                                   bool in_function_try_block)
17472 {
17473   tree body, list;
17474   bool ctor_initializer_p;
17475   const bool check_body_p =
17476      DECL_CONSTRUCTOR_P (current_function_decl)
17477      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17478   tree last = NULL;
17479
17480   /* Begin the function body.  */
17481   body = begin_function_body ();
17482   /* Parse the optional ctor-initializer.  */
17483   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17484
17485   /* If we're parsing a constexpr constructor definition, we need
17486      to check that the constructor body is indeed empty.  However,
17487      before we get to cp_parser_function_body lot of junk has been
17488      generated, so we can't just check that we have an empty block.
17489      Rather we take a snapshot of the outermost block, and check whether
17490      cp_parser_function_body changed its state.  */
17491   if (check_body_p)
17492     {
17493       list = cur_stmt_list;
17494       if (STATEMENT_LIST_TAIL (list))
17495         last = STATEMENT_LIST_TAIL (list)->stmt;
17496     }
17497   /* Parse the function-body.  */
17498   cp_parser_function_body (parser, in_function_try_block);
17499   if (check_body_p)
17500     check_constexpr_ctor_body (last, list);
17501   /* Finish the function body.  */
17502   finish_function_body (body);
17503
17504   return ctor_initializer_p;
17505 }
17506
17507 /* Parse an initializer.
17508
17509    initializer:
17510      = initializer-clause
17511      ( expression-list )
17512
17513    Returns an expression representing the initializer.  If no
17514    initializer is present, NULL_TREE is returned.
17515
17516    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17517    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17518    set to TRUE if there is no initializer present.  If there is an
17519    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17520    is set to true; otherwise it is set to false.  */
17521
17522 static tree
17523 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17524                        bool* non_constant_p)
17525 {
17526   cp_token *token;
17527   tree init;
17528
17529   /* Peek at the next token.  */
17530   token = cp_lexer_peek_token (parser->lexer);
17531
17532   /* Let our caller know whether or not this initializer was
17533      parenthesized.  */
17534   *is_direct_init = (token->type != CPP_EQ);
17535   /* Assume that the initializer is constant.  */
17536   *non_constant_p = false;
17537
17538   if (token->type == CPP_EQ)
17539     {
17540       /* Consume the `='.  */
17541       cp_lexer_consume_token (parser->lexer);
17542       /* Parse the initializer-clause.  */
17543       init = cp_parser_initializer_clause (parser, non_constant_p);
17544     }
17545   else if (token->type == CPP_OPEN_PAREN)
17546     {
17547       VEC(tree,gc) *vec;
17548       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17549                                                      /*cast_p=*/false,
17550                                                      /*allow_expansion_p=*/true,
17551                                                      non_constant_p);
17552       if (vec == NULL)
17553         return error_mark_node;
17554       init = build_tree_list_vec (vec);
17555       release_tree_vector (vec);
17556     }
17557   else if (token->type == CPP_OPEN_BRACE)
17558     {
17559       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17560       init = cp_parser_braced_list (parser, non_constant_p);
17561       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17562     }
17563   else
17564     {
17565       /* Anything else is an error.  */
17566       cp_parser_error (parser, "expected initializer");
17567       init = error_mark_node;
17568     }
17569
17570   return init;
17571 }
17572
17573 /* Parse an initializer-clause.
17574
17575    initializer-clause:
17576      assignment-expression
17577      braced-init-list
17578
17579    Returns an expression representing the initializer.
17580
17581    If the `assignment-expression' production is used the value
17582    returned is simply a representation for the expression.
17583
17584    Otherwise, calls cp_parser_braced_list.  */
17585
17586 static tree
17587 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17588 {
17589   tree initializer;
17590
17591   /* Assume the expression is constant.  */
17592   *non_constant_p = false;
17593
17594   /* If it is not a `{', then we are looking at an
17595      assignment-expression.  */
17596   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17597     {
17598       initializer
17599         = cp_parser_constant_expression (parser,
17600                                         /*allow_non_constant_p=*/true,
17601                                         non_constant_p);
17602     }
17603   else
17604     initializer = cp_parser_braced_list (parser, non_constant_p);
17605
17606   return initializer;
17607 }
17608
17609 /* Parse a brace-enclosed initializer list.
17610
17611    braced-init-list:
17612      { initializer-list , [opt] }
17613      { }
17614
17615    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17616    the elements of the initializer-list (or NULL, if the last
17617    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17618    NULL_TREE.  There is no way to detect whether or not the optional
17619    trailing `,' was provided.  NON_CONSTANT_P is as for
17620    cp_parser_initializer.  */     
17621
17622 static tree
17623 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17624 {
17625   tree initializer;
17626
17627   /* Consume the `{' token.  */
17628   cp_lexer_consume_token (parser->lexer);
17629   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17630   initializer = make_node (CONSTRUCTOR);
17631   /* If it's not a `}', then there is a non-trivial initializer.  */
17632   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17633     {
17634       /* Parse the initializer list.  */
17635       CONSTRUCTOR_ELTS (initializer)
17636         = cp_parser_initializer_list (parser, non_constant_p);
17637       /* A trailing `,' token is allowed.  */
17638       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17639         cp_lexer_consume_token (parser->lexer);
17640     }
17641   /* Now, there should be a trailing `}'.  */
17642   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17643   TREE_TYPE (initializer) = init_list_type_node;
17644   return initializer;
17645 }
17646
17647 /* Parse an initializer-list.
17648
17649    initializer-list:
17650      initializer-clause ... [opt]
17651      initializer-list , initializer-clause ... [opt]
17652
17653    GNU Extension:
17654
17655    initializer-list:
17656      designation initializer-clause ...[opt]
17657      initializer-list , designation initializer-clause ...[opt]
17658
17659    designation:
17660      . identifier =
17661      identifier :
17662      [ constant-expression ] =
17663
17664    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17665    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17666    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17667    as for cp_parser_initializer.  */
17668
17669 static VEC(constructor_elt,gc) *
17670 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17671 {
17672   VEC(constructor_elt,gc) *v = NULL;
17673
17674   /* Assume all of the expressions are constant.  */
17675   *non_constant_p = false;
17676
17677   /* Parse the rest of the list.  */
17678   while (true)
17679     {
17680       cp_token *token;
17681       tree designator;
17682       tree initializer;
17683       bool clause_non_constant_p;
17684
17685       /* If the next token is an identifier and the following one is a
17686          colon, we are looking at the GNU designated-initializer
17687          syntax.  */
17688       if (cp_parser_allow_gnu_extensions_p (parser)
17689           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17690           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17691         {
17692           /* Warn the user that they are using an extension.  */
17693           pedwarn (input_location, OPT_Wpedantic, 
17694                    "ISO C++ does not allow designated initializers");
17695           /* Consume the identifier.  */
17696           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17697           /* Consume the `:'.  */
17698           cp_lexer_consume_token (parser->lexer);
17699         }
17700       /* Also handle the C99 syntax, '. id ='.  */
17701       else if (cp_parser_allow_gnu_extensions_p (parser)
17702                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17703                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17704                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17705         {
17706           /* Warn the user that they are using an extension.  */
17707           pedwarn (input_location, OPT_Wpedantic,
17708                    "ISO C++ does not allow C99 designated initializers");
17709           /* Consume the `.'.  */
17710           cp_lexer_consume_token (parser->lexer);
17711           /* Consume the identifier.  */
17712           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17713           /* Consume the `='.  */
17714           cp_lexer_consume_token (parser->lexer);
17715         }
17716       /* Also handle C99 array designators, '[ const ] ='.  */
17717       else if (cp_parser_allow_gnu_extensions_p (parser)
17718                && !c_dialect_objc ()
17719                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17720         {
17721           /* In C++11, [ could start a lambda-introducer.  */
17722           cp_parser_parse_tentatively (parser);
17723           cp_lexer_consume_token (parser->lexer);
17724           designator = cp_parser_constant_expression (parser, false, NULL);
17725           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17726           cp_parser_require (parser, CPP_EQ, RT_EQ);
17727           if (!cp_parser_parse_definitely (parser))
17728             designator = NULL_TREE;
17729         }
17730       else
17731         designator = NULL_TREE;
17732
17733       /* Parse the initializer.  */
17734       initializer = cp_parser_initializer_clause (parser,
17735                                                   &clause_non_constant_p);
17736       /* If any clause is non-constant, so is the entire initializer.  */
17737       if (clause_non_constant_p)
17738         *non_constant_p = true;
17739
17740       /* If we have an ellipsis, this is an initializer pack
17741          expansion.  */
17742       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17743         {
17744           /* Consume the `...'.  */
17745           cp_lexer_consume_token (parser->lexer);
17746
17747           /* Turn the initializer into an initializer expansion.  */
17748           initializer = make_pack_expansion (initializer);
17749         }
17750
17751       /* Add it to the vector.  */
17752       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17753
17754       /* If the next token is not a comma, we have reached the end of
17755          the list.  */
17756       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17757         break;
17758
17759       /* Peek at the next token.  */
17760       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17761       /* If the next token is a `}', then we're still done.  An
17762          initializer-clause can have a trailing `,' after the
17763          initializer-list and before the closing `}'.  */
17764       if (token->type == CPP_CLOSE_BRACE)
17765         break;
17766
17767       /* Consume the `,' token.  */
17768       cp_lexer_consume_token (parser->lexer);
17769     }
17770
17771   return v;
17772 }
17773
17774 /* Classes [gram.class] */
17775
17776 /* Parse a class-name.
17777
17778    class-name:
17779      identifier
17780      template-id
17781
17782    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17783    to indicate that names looked up in dependent types should be
17784    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17785    keyword has been used to indicate that the name that appears next
17786    is a template.  TAG_TYPE indicates the explicit tag given before
17787    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17788    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17789    is the class being defined in a class-head.
17790
17791    Returns the TYPE_DECL representing the class.  */
17792
17793 static tree
17794 cp_parser_class_name (cp_parser *parser,
17795                       bool typename_keyword_p,
17796                       bool template_keyword_p,
17797                       enum tag_types tag_type,
17798                       bool check_dependency_p,
17799                       bool class_head_p,
17800                       bool is_declaration)
17801 {
17802   tree decl;
17803   tree scope;
17804   bool typename_p;
17805   cp_token *token;
17806   tree identifier = NULL_TREE;
17807
17808   /* All class-names start with an identifier.  */
17809   token = cp_lexer_peek_token (parser->lexer);
17810   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17811     {
17812       cp_parser_error (parser, "expected class-name");
17813       return error_mark_node;
17814     }
17815
17816   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17817      to a template-id, so we save it here.  */
17818   scope = parser->scope;
17819   if (scope == error_mark_node)
17820     return error_mark_node;
17821
17822   /* Any name names a type if we're following the `typename' keyword
17823      in a qualified name where the enclosing scope is type-dependent.  */
17824   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17825                 && dependent_type_p (scope));
17826   /* Handle the common case (an identifier, but not a template-id)
17827      efficiently.  */
17828   if (token->type == CPP_NAME
17829       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17830     {
17831       cp_token *identifier_token;
17832       bool ambiguous_p;
17833
17834       /* Look for the identifier.  */
17835       identifier_token = cp_lexer_peek_token (parser->lexer);
17836       ambiguous_p = identifier_token->ambiguous_p;
17837       identifier = cp_parser_identifier (parser);
17838       /* If the next token isn't an identifier, we are certainly not
17839          looking at a class-name.  */
17840       if (identifier == error_mark_node)
17841         decl = error_mark_node;
17842       /* If we know this is a type-name, there's no need to look it
17843          up.  */
17844       else if (typename_p)
17845         decl = identifier;
17846       else
17847         {
17848           tree ambiguous_decls;
17849           /* If we already know that this lookup is ambiguous, then
17850              we've already issued an error message; there's no reason
17851              to check again.  */
17852           if (ambiguous_p)
17853             {
17854               cp_parser_simulate_error (parser);
17855               return error_mark_node;
17856             }
17857           /* If the next token is a `::', then the name must be a type
17858              name.
17859
17860              [basic.lookup.qual]
17861
17862              During the lookup for a name preceding the :: scope
17863              resolution operator, object, function, and enumerator
17864              names are ignored.  */
17865           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17866             tag_type = typename_type;
17867           /* Look up the name.  */
17868           decl = cp_parser_lookup_name (parser, identifier,
17869                                         tag_type,
17870                                         /*is_template=*/false,
17871                                         /*is_namespace=*/false,
17872                                         check_dependency_p,
17873                                         &ambiguous_decls,
17874                                         identifier_token->location);
17875           if (ambiguous_decls)
17876             {
17877               if (cp_parser_parsing_tentatively (parser))
17878                 cp_parser_simulate_error (parser);
17879               return error_mark_node;
17880             }
17881         }
17882     }
17883   else
17884     {
17885       /* Try a template-id.  */
17886       decl = cp_parser_template_id (parser, template_keyword_p,
17887                                     check_dependency_p,
17888                                     tag_type,
17889                                     is_declaration);
17890       if (decl == error_mark_node)
17891         return error_mark_node;
17892     }
17893
17894   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17895
17896   /* If this is a typename, create a TYPENAME_TYPE.  */
17897   if (typename_p && decl != error_mark_node)
17898     {
17899       decl = make_typename_type (scope, decl, typename_type,
17900                                  /*complain=*/tf_error);
17901       if (decl != error_mark_node)
17902         decl = TYPE_NAME (decl);
17903     }
17904
17905   decl = strip_using_decl (decl);
17906
17907   /* Check to see that it is really the name of a class.  */
17908   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17909       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17910       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17911     /* Situations like this:
17912
17913          template <typename T> struct A {
17914            typename T::template X<int>::I i;
17915          };
17916
17917        are problematic.  Is `T::template X<int>' a class-name?  The
17918        standard does not seem to be definitive, but there is no other
17919        valid interpretation of the following `::'.  Therefore, those
17920        names are considered class-names.  */
17921     {
17922       decl = make_typename_type (scope, decl, tag_type, tf_error);
17923       if (decl != error_mark_node)
17924         decl = TYPE_NAME (decl);
17925     }
17926   else if (TREE_CODE (decl) != TYPE_DECL
17927            || TREE_TYPE (decl) == error_mark_node
17928            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17929            /* In Objective-C 2.0, a classname followed by '.' starts a
17930               dot-syntax expression, and it's not a type-name.  */
17931            || (c_dialect_objc ()
17932                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17933                && objc_is_class_name (decl)))
17934     decl = error_mark_node;
17935
17936   if (decl == error_mark_node)
17937     cp_parser_error (parser, "expected class-name");
17938   else if (identifier && !parser->scope)
17939     maybe_note_name_used_in_class (identifier, decl);
17940
17941   return decl;
17942 }
17943
17944 /* Parse a class-specifier.
17945
17946    class-specifier:
17947      class-head { member-specification [opt] }
17948
17949    Returns the TREE_TYPE representing the class.  */
17950
17951 static tree
17952 cp_parser_class_specifier_1 (cp_parser* parser)
17953 {
17954   tree type;
17955   tree attributes = NULL_TREE;
17956   bool nested_name_specifier_p;
17957   unsigned saved_num_template_parameter_lists;
17958   bool saved_in_function_body;
17959   unsigned char in_statement;
17960   bool in_switch_statement_p;
17961   bool saved_in_unbraced_linkage_specification_p;
17962   tree old_scope = NULL_TREE;
17963   tree scope = NULL_TREE;
17964   cp_token *closing_brace;
17965
17966   push_deferring_access_checks (dk_no_deferred);
17967
17968   /* Parse the class-head.  */
17969   type = cp_parser_class_head (parser,
17970                                &nested_name_specifier_p);
17971   /* If the class-head was a semantic disaster, skip the entire body
17972      of the class.  */
17973   if (!type)
17974     {
17975       cp_parser_skip_to_end_of_block_or_statement (parser);
17976       pop_deferring_access_checks ();
17977       return error_mark_node;
17978     }
17979
17980   /* Look for the `{'.  */
17981   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17982     {
17983       pop_deferring_access_checks ();
17984       return error_mark_node;
17985     }
17986
17987   /* Issue an error message if type-definitions are forbidden here.  */
17988   cp_parser_check_type_definition (parser);
17989   /* Remember that we are defining one more class.  */
17990   ++parser->num_classes_being_defined;
17991   /* Inside the class, surrounding template-parameter-lists do not
17992      apply.  */
17993   saved_num_template_parameter_lists
17994     = parser->num_template_parameter_lists;
17995   parser->num_template_parameter_lists = 0;
17996   /* We are not in a function body.  */
17997   saved_in_function_body = parser->in_function_body;
17998   parser->in_function_body = false;
17999   /* Or in a loop.  */
18000   in_statement = parser->in_statement;
18001   parser->in_statement = 0;
18002   /* Or in a switch.  */
18003   in_switch_statement_p = parser->in_switch_statement_p;
18004   parser->in_switch_statement_p = false;
18005   /* We are not immediately inside an extern "lang" block.  */
18006   saved_in_unbraced_linkage_specification_p
18007     = parser->in_unbraced_linkage_specification_p;
18008   parser->in_unbraced_linkage_specification_p = false;
18009
18010   /* Start the class.  */
18011   if (nested_name_specifier_p)
18012     {
18013       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18014       old_scope = push_inner_scope (scope);
18015     }
18016   type = begin_class_definition (type);
18017
18018   if (type == error_mark_node)
18019     /* If the type is erroneous, skip the entire body of the class.  */
18020     cp_parser_skip_to_closing_brace (parser);
18021   else
18022     /* Parse the member-specification.  */
18023     cp_parser_member_specification_opt (parser);
18024
18025   /* Look for the trailing `}'.  */
18026   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18027   /* Look for trailing attributes to apply to this class.  */
18028   if (cp_parser_allow_gnu_extensions_p (parser))
18029     attributes = cp_parser_attributes_opt (parser);
18030   if (type != error_mark_node)
18031     type = finish_struct (type, attributes);
18032   if (nested_name_specifier_p)
18033     pop_inner_scope (old_scope, scope);
18034
18035   /* We've finished a type definition.  Check for the common syntax
18036      error of forgetting a semicolon after the definition.  We need to
18037      be careful, as we can't just check for not-a-semicolon and be done
18038      with it; the user might have typed:
18039
18040      class X { } c = ...;
18041      class X { } *p = ...;
18042
18043      and so forth.  Instead, enumerate all the possible tokens that
18044      might follow this production; if we don't see one of them, then
18045      complain and silently insert the semicolon.  */
18046   {
18047     cp_token *token = cp_lexer_peek_token (parser->lexer);
18048     bool want_semicolon = true;
18049
18050     switch (token->type)
18051       {
18052       case CPP_NAME:
18053       case CPP_SEMICOLON:
18054       case CPP_MULT:
18055       case CPP_AND:
18056       case CPP_OPEN_PAREN:
18057       case CPP_CLOSE_PAREN:
18058       case CPP_COMMA:
18059         want_semicolon = false;
18060         break;
18061
18062         /* While it's legal for type qualifiers and storage class
18063            specifiers to follow type definitions in the grammar, only
18064            compiler testsuites contain code like that.  Assume that if
18065            we see such code, then what we're really seeing is a case
18066            like:
18067
18068            class X { }
18069            const <type> var = ...;
18070
18071            or
18072
18073            class Y { }
18074            static <type> func (...) ...
18075
18076            i.e. the qualifier or specifier applies to the next
18077            declaration.  To do so, however, we need to look ahead one
18078            more token to see if *that* token is a type specifier.
18079
18080            This code could be improved to handle:
18081
18082            class Z { }
18083            static const <type> var = ...;  */
18084       case CPP_KEYWORD:
18085         if (keyword_is_decl_specifier (token->keyword))
18086           {
18087             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18088
18089             /* Handling user-defined types here would be nice, but very
18090                tricky.  */
18091             want_semicolon
18092               = (lookahead->type == CPP_KEYWORD
18093                  && keyword_begins_type_specifier (lookahead->keyword));
18094           }
18095         break;
18096       default:
18097         break;
18098       }
18099
18100     /* If we don't have a type, then something is very wrong and we
18101        shouldn't try to do anything clever.  Likewise for not seeing the
18102        closing brace.  */
18103     if (closing_brace && TYPE_P (type) && want_semicolon)
18104       {
18105         cp_token_position prev
18106           = cp_lexer_previous_token_position (parser->lexer);
18107         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18108         location_t loc = prev_token->location;
18109
18110         if (CLASSTYPE_DECLARED_CLASS (type))
18111           error_at (loc, "expected %<;%> after class definition");
18112         else if (TREE_CODE (type) == RECORD_TYPE)
18113           error_at (loc, "expected %<;%> after struct definition");
18114         else if (TREE_CODE (type) == UNION_TYPE)
18115           error_at (loc, "expected %<;%> after union definition");
18116         else
18117           gcc_unreachable ();
18118
18119         /* Unget one token and smash it to look as though we encountered
18120            a semicolon in the input stream.  */
18121         cp_lexer_set_token_position (parser->lexer, prev);
18122         token = cp_lexer_peek_token (parser->lexer);
18123         token->type = CPP_SEMICOLON;
18124         token->keyword = RID_MAX;
18125       }
18126   }
18127
18128   /* If this class is not itself within the scope of another class,
18129      then we need to parse the bodies of all of the queued function
18130      definitions.  Note that the queued functions defined in a class
18131      are not always processed immediately following the
18132      class-specifier for that class.  Consider:
18133
18134        struct A {
18135          struct B { void f() { sizeof (A); } };
18136        };
18137
18138      If `f' were processed before the processing of `A' were
18139      completed, there would be no way to compute the size of `A'.
18140      Note that the nesting we are interested in here is lexical --
18141      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18142      for:
18143
18144        struct A { struct B; };
18145        struct A::B { void f() { } };
18146
18147      there is no need to delay the parsing of `A::B::f'.  */
18148   if (--parser->num_classes_being_defined == 0)
18149     {
18150       tree decl;
18151       tree class_type = NULL_TREE;
18152       tree pushed_scope = NULL_TREE;
18153       unsigned ix;
18154       cp_default_arg_entry *e;
18155       tree save_ccp, save_ccr;
18156
18157       /* In a first pass, parse default arguments to the functions.
18158          Then, in a second pass, parse the bodies of the functions.
18159          This two-phased approach handles cases like:
18160
18161             struct S {
18162               void f() { g(); }
18163               void g(int i = 3);
18164             };
18165
18166          */
18167       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18168                         ix, e)
18169         {
18170           decl = e->decl;
18171           /* If there are default arguments that have not yet been processed,
18172              take care of them now.  */
18173           if (class_type != e->class_type)
18174             {
18175               if (pushed_scope)
18176                 pop_scope (pushed_scope);
18177               class_type = e->class_type;
18178               pushed_scope = push_scope (class_type);
18179             }
18180           /* Make sure that any template parameters are in scope.  */
18181           maybe_begin_member_template_processing (decl);
18182           /* Parse the default argument expressions.  */
18183           cp_parser_late_parsing_default_args (parser, decl);
18184           /* Remove any template parameters from the symbol table.  */
18185           maybe_end_member_template_processing ();
18186         }
18187       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18188       /* Now parse any NSDMIs.  */
18189       save_ccp = current_class_ptr;
18190       save_ccr = current_class_ref;
18191       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18192         {
18193           if (class_type != DECL_CONTEXT (decl))
18194             {
18195               if (pushed_scope)
18196                 pop_scope (pushed_scope);
18197               class_type = DECL_CONTEXT (decl);
18198               pushed_scope = push_scope (class_type);
18199             }
18200           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18201           cp_parser_late_parsing_nsdmi (parser, decl);
18202         }
18203       VEC_truncate (tree, unparsed_nsdmis, 0);
18204       current_class_ptr = save_ccp;
18205       current_class_ref = save_ccr;
18206       if (pushed_scope)
18207         pop_scope (pushed_scope);
18208       /* Now parse the body of the functions.  */
18209       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18210         cp_parser_late_parsing_for_member (parser, decl);
18211       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18212     }
18213
18214   /* Put back any saved access checks.  */
18215   pop_deferring_access_checks ();
18216
18217   /* Restore saved state.  */
18218   parser->in_switch_statement_p = in_switch_statement_p;
18219   parser->in_statement = in_statement;
18220   parser->in_function_body = saved_in_function_body;
18221   parser->num_template_parameter_lists
18222     = saved_num_template_parameter_lists;
18223   parser->in_unbraced_linkage_specification_p
18224     = saved_in_unbraced_linkage_specification_p;
18225
18226   return type;
18227 }
18228
18229 static tree
18230 cp_parser_class_specifier (cp_parser* parser)
18231 {
18232   tree ret;
18233   timevar_push (TV_PARSE_STRUCT);
18234   ret = cp_parser_class_specifier_1 (parser);
18235   timevar_pop (TV_PARSE_STRUCT);
18236   return ret;
18237 }
18238
18239 /* Parse a class-head.
18240
18241    class-head:
18242      class-key identifier [opt] base-clause [opt]
18243      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18244      class-key nested-name-specifier [opt] template-id
18245        base-clause [opt]
18246
18247    class-virt-specifier:
18248      final
18249
18250    GNU Extensions:
18251      class-key attributes identifier [opt] base-clause [opt]
18252      class-key attributes nested-name-specifier identifier base-clause [opt]
18253      class-key attributes nested-name-specifier [opt] template-id
18254        base-clause [opt]
18255
18256    Upon return BASES is initialized to the list of base classes (or
18257    NULL, if there are none) in the same form returned by
18258    cp_parser_base_clause.
18259
18260    Returns the TYPE of the indicated class.  Sets
18261    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18262    involving a nested-name-specifier was used, and FALSE otherwise.
18263
18264    Returns error_mark_node if this is not a class-head.
18265
18266    Returns NULL_TREE if the class-head is syntactically valid, but
18267    semantically invalid in a way that means we should skip the entire
18268    body of the class.  */
18269
18270 static tree
18271 cp_parser_class_head (cp_parser* parser,
18272                       bool* nested_name_specifier_p)
18273 {
18274   tree nested_name_specifier;
18275   enum tag_types class_key;
18276   tree id = NULL_TREE;
18277   tree type = NULL_TREE;
18278   tree attributes;
18279   tree bases;
18280   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18281   bool template_id_p = false;
18282   bool qualified_p = false;
18283   bool invalid_nested_name_p = false;
18284   bool invalid_explicit_specialization_p = false;
18285   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18286   tree pushed_scope = NULL_TREE;
18287   unsigned num_templates;
18288   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18289   /* Assume no nested-name-specifier will be present.  */
18290   *nested_name_specifier_p = false;
18291   /* Assume no template parameter lists will be used in defining the
18292      type.  */
18293   num_templates = 0;
18294   parser->colon_corrects_to_scope_p = false;
18295
18296   /* Look for the class-key.  */
18297   class_key = cp_parser_class_key (parser);
18298   if (class_key == none_type)
18299     return error_mark_node;
18300
18301   /* Parse the attributes.  */
18302   attributes = cp_parser_attributes_opt (parser);
18303
18304   /* If the next token is `::', that is invalid -- but sometimes
18305      people do try to write:
18306
18307        struct ::S {};
18308
18309      Handle this gracefully by accepting the extra qualifier, and then
18310      issuing an error about it later if this really is a
18311      class-head.  If it turns out just to be an elaborated type
18312      specifier, remain silent.  */
18313   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18314     qualified_p = true;
18315
18316   push_deferring_access_checks (dk_no_check);
18317
18318   /* Determine the name of the class.  Begin by looking for an
18319      optional nested-name-specifier.  */
18320   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18321   nested_name_specifier
18322     = cp_parser_nested_name_specifier_opt (parser,
18323                                            /*typename_keyword_p=*/false,
18324                                            /*check_dependency_p=*/false,
18325                                            /*type_p=*/true,
18326                                            /*is_declaration=*/false);
18327   /* If there was a nested-name-specifier, then there *must* be an
18328      identifier.  */
18329   if (nested_name_specifier)
18330     {
18331       type_start_token = cp_lexer_peek_token (parser->lexer);
18332       /* Although the grammar says `identifier', it really means
18333          `class-name' or `template-name'.  You are only allowed to
18334          define a class that has already been declared with this
18335          syntax.
18336
18337          The proposed resolution for Core Issue 180 says that wherever
18338          you see `class T::X' you should treat `X' as a type-name.
18339
18340          It is OK to define an inaccessible class; for example:
18341
18342            class A { class B; };
18343            class A::B {};
18344
18345          We do not know if we will see a class-name, or a
18346          template-name.  We look for a class-name first, in case the
18347          class-name is a template-id; if we looked for the
18348          template-name first we would stop after the template-name.  */
18349       cp_parser_parse_tentatively (parser);
18350       type = cp_parser_class_name (parser,
18351                                    /*typename_keyword_p=*/false,
18352                                    /*template_keyword_p=*/false,
18353                                    class_type,
18354                                    /*check_dependency_p=*/false,
18355                                    /*class_head_p=*/true,
18356                                    /*is_declaration=*/false);
18357       /* If that didn't work, ignore the nested-name-specifier.  */
18358       if (!cp_parser_parse_definitely (parser))
18359         {
18360           invalid_nested_name_p = true;
18361           type_start_token = cp_lexer_peek_token (parser->lexer);
18362           id = cp_parser_identifier (parser);
18363           if (id == error_mark_node)
18364             id = NULL_TREE;
18365         }
18366       /* If we could not find a corresponding TYPE, treat this
18367          declaration like an unqualified declaration.  */
18368       if (type == error_mark_node)
18369         nested_name_specifier = NULL_TREE;
18370       /* Otherwise, count the number of templates used in TYPE and its
18371          containing scopes.  */
18372       else
18373         {
18374           tree scope;
18375
18376           for (scope = TREE_TYPE (type);
18377                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18378                scope = (TYPE_P (scope)
18379                         ? TYPE_CONTEXT (scope)
18380                         : DECL_CONTEXT (scope)))
18381             if (TYPE_P (scope)
18382                 && CLASS_TYPE_P (scope)
18383                 && CLASSTYPE_TEMPLATE_INFO (scope)
18384                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18385                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18386               ++num_templates;
18387         }
18388     }
18389   /* Otherwise, the identifier is optional.  */
18390   else
18391     {
18392       /* We don't know whether what comes next is a template-id,
18393          an identifier, or nothing at all.  */
18394       cp_parser_parse_tentatively (parser);
18395       /* Check for a template-id.  */
18396       type_start_token = cp_lexer_peek_token (parser->lexer);
18397       id = cp_parser_template_id (parser,
18398                                   /*template_keyword_p=*/false,
18399                                   /*check_dependency_p=*/true,
18400                                   class_key,
18401                                   /*is_declaration=*/true);
18402       /* If that didn't work, it could still be an identifier.  */
18403       if (!cp_parser_parse_definitely (parser))
18404         {
18405           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18406             {
18407               type_start_token = cp_lexer_peek_token (parser->lexer);
18408               id = cp_parser_identifier (parser);
18409             }
18410           else
18411             id = NULL_TREE;
18412         }
18413       else
18414         {
18415           template_id_p = true;
18416           ++num_templates;
18417         }
18418     }
18419
18420   pop_deferring_access_checks ();
18421
18422   if (id)
18423     {
18424       cp_parser_check_for_invalid_template_id (parser, id,
18425                                                class_key,
18426                                                type_start_token->location);
18427     }
18428   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18429
18430   /* If it's not a `:' or a `{' then we can't really be looking at a
18431      class-head, since a class-head only appears as part of a
18432      class-specifier.  We have to detect this situation before calling
18433      xref_tag, since that has irreversible side-effects.  */
18434   if (!cp_parser_next_token_starts_class_definition_p (parser))
18435     {
18436       cp_parser_error (parser, "expected %<{%> or %<:%>");
18437       type = error_mark_node;
18438       goto out;
18439     }
18440
18441   /* At this point, we're going ahead with the class-specifier, even
18442      if some other problem occurs.  */
18443   cp_parser_commit_to_tentative_parse (parser);
18444   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18445     {
18446       cp_parser_error (parser,
18447                        "cannot specify %<override%> for a class");
18448       type = error_mark_node;
18449       goto out;
18450     }
18451   /* Issue the error about the overly-qualified name now.  */
18452   if (qualified_p)
18453     {
18454       cp_parser_error (parser,
18455                        "global qualification of class name is invalid");
18456       type = error_mark_node;
18457       goto out;
18458     }
18459   else if (invalid_nested_name_p)
18460     {
18461       cp_parser_error (parser,
18462                        "qualified name does not name a class");
18463       type = error_mark_node;
18464       goto out;
18465     }
18466   else if (nested_name_specifier)
18467     {
18468       tree scope;
18469
18470       /* Reject typedef-names in class heads.  */
18471       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18472         {
18473           error_at (type_start_token->location,
18474                     "invalid class name in declaration of %qD",
18475                     type);
18476           type = NULL_TREE;
18477           goto done;
18478         }
18479
18480       /* Figure out in what scope the declaration is being placed.  */
18481       scope = current_scope ();
18482       /* If that scope does not contain the scope in which the
18483          class was originally declared, the program is invalid.  */
18484       if (scope && !is_ancestor (scope, nested_name_specifier))
18485         {
18486           if (at_namespace_scope_p ())
18487             error_at (type_start_token->location,
18488                       "declaration of %qD in namespace %qD which does not "
18489                       "enclose %qD",
18490                       type, scope, nested_name_specifier);
18491           else
18492             error_at (type_start_token->location,
18493                       "declaration of %qD in %qD which does not enclose %qD",
18494                       type, scope, nested_name_specifier);
18495           type = NULL_TREE;
18496           goto done;
18497         }
18498       /* [dcl.meaning]
18499
18500          A declarator-id shall not be qualified except for the
18501          definition of a ... nested class outside of its class
18502          ... [or] the definition or explicit instantiation of a
18503          class member of a namespace outside of its namespace.  */
18504       if (scope == nested_name_specifier)
18505         {
18506           permerror (nested_name_specifier_token_start->location,
18507                      "extra qualification not allowed");
18508           nested_name_specifier = NULL_TREE;
18509           num_templates = 0;
18510         }
18511     }
18512   /* An explicit-specialization must be preceded by "template <>".  If
18513      it is not, try to recover gracefully.  */
18514   if (at_namespace_scope_p ()
18515       && parser->num_template_parameter_lists == 0
18516       && template_id_p)
18517     {
18518       error_at (type_start_token->location,
18519                 "an explicit specialization must be preceded by %<template <>%>");
18520       invalid_explicit_specialization_p = true;
18521       /* Take the same action that would have been taken by
18522          cp_parser_explicit_specialization.  */
18523       ++parser->num_template_parameter_lists;
18524       begin_specialization ();
18525     }
18526   /* There must be no "return" statements between this point and the
18527      end of this function; set "type "to the correct return value and
18528      use "goto done;" to return.  */
18529   /* Make sure that the right number of template parameters were
18530      present.  */
18531   if (!cp_parser_check_template_parameters (parser, num_templates,
18532                                             type_start_token->location,
18533                                             /*declarator=*/NULL))
18534     {
18535       /* If something went wrong, there is no point in even trying to
18536          process the class-definition.  */
18537       type = NULL_TREE;
18538       goto done;
18539     }
18540
18541   /* Look up the type.  */
18542   if (template_id_p)
18543     {
18544       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18545           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18546               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18547         {
18548           error_at (type_start_token->location,
18549                     "function template %qD redeclared as a class template", id);
18550           type = error_mark_node;
18551         }
18552       else
18553         {
18554           type = TREE_TYPE (id);
18555           type = maybe_process_partial_specialization (type);
18556         }
18557       if (nested_name_specifier)
18558         pushed_scope = push_scope (nested_name_specifier);
18559     }
18560   else if (nested_name_specifier)
18561     {
18562       tree class_type;
18563
18564       /* Given:
18565
18566             template <typename T> struct S { struct T };
18567             template <typename T> struct S<T>::T { };
18568
18569          we will get a TYPENAME_TYPE when processing the definition of
18570          `S::T'.  We need to resolve it to the actual type before we
18571          try to define it.  */
18572       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18573         {
18574           class_type = resolve_typename_type (TREE_TYPE (type),
18575                                               /*only_current_p=*/false);
18576           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18577             type = TYPE_NAME (class_type);
18578           else
18579             {
18580               cp_parser_error (parser, "could not resolve typename type");
18581               type = error_mark_node;
18582             }
18583         }
18584
18585       if (maybe_process_partial_specialization (TREE_TYPE (type))
18586           == error_mark_node)
18587         {
18588           type = NULL_TREE;
18589           goto done;
18590         }
18591
18592       class_type = current_class_type;
18593       /* Enter the scope indicated by the nested-name-specifier.  */
18594       pushed_scope = push_scope (nested_name_specifier);
18595       /* Get the canonical version of this type.  */
18596       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18597       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18598           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18599         {
18600           type = push_template_decl (type);
18601           if (type == error_mark_node)
18602             {
18603               type = NULL_TREE;
18604               goto done;
18605             }
18606         }
18607
18608       type = TREE_TYPE (type);
18609       *nested_name_specifier_p = true;
18610     }
18611   else      /* The name is not a nested name.  */
18612     {
18613       /* If the class was unnamed, create a dummy name.  */
18614       if (!id)
18615         id = make_anon_name ();
18616       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18617                        parser->num_template_parameter_lists);
18618     }
18619
18620   /* Indicate whether this class was declared as a `class' or as a
18621      `struct'.  */
18622   if (TREE_CODE (type) == RECORD_TYPE)
18623     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18624   cp_parser_check_class_key (class_key, type);
18625
18626   /* If this type was already complete, and we see another definition,
18627      that's an error.  */
18628   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18629     {
18630       error_at (type_start_token->location, "redefinition of %q#T",
18631                 type);
18632       error_at (type_start_token->location, "previous definition of %q+#T",
18633                 type);
18634       type = NULL_TREE;
18635       goto done;
18636     }
18637   else if (type == error_mark_node)
18638     type = NULL_TREE;
18639
18640   if (type)
18641     {
18642       /* Apply attributes now, before any use of the class as a template
18643          argument in its base list.  */
18644       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18645       fixup_attribute_variants (type);
18646     }
18647
18648   /* We will have entered the scope containing the class; the names of
18649      base classes should be looked up in that context.  For example:
18650
18651        struct A { struct B {}; struct C; };
18652        struct A::C : B {};
18653
18654      is valid.  */
18655
18656   /* Get the list of base-classes, if there is one.  */
18657   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18658     bases = cp_parser_base_clause (parser);
18659   else
18660     bases = NULL_TREE;
18661
18662   /* If we're really defining a class, process the base classes.
18663      If they're invalid, fail.  */
18664   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18665       && !xref_basetypes (type, bases))
18666     type = NULL_TREE;
18667
18668  done:
18669   /* Leave the scope given by the nested-name-specifier.  We will
18670      enter the class scope itself while processing the members.  */
18671   if (pushed_scope)
18672     pop_scope (pushed_scope);
18673
18674   if (invalid_explicit_specialization_p)
18675     {
18676       end_specialization ();
18677       --parser->num_template_parameter_lists;
18678     }
18679
18680   if (type)
18681     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18682   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18683     CLASSTYPE_FINAL (type) = 1;
18684  out:
18685   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18686   return type;
18687 }
18688
18689 /* Parse a class-key.
18690
18691    class-key:
18692      class
18693      struct
18694      union
18695
18696    Returns the kind of class-key specified, or none_type to indicate
18697    error.  */
18698
18699 static enum tag_types
18700 cp_parser_class_key (cp_parser* parser)
18701 {
18702   cp_token *token;
18703   enum tag_types tag_type;
18704
18705   /* Look for the class-key.  */
18706   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18707   if (!token)
18708     return none_type;
18709
18710   /* Check to see if the TOKEN is a class-key.  */
18711   tag_type = cp_parser_token_is_class_key (token);
18712   if (!tag_type)
18713     cp_parser_error (parser, "expected class-key");
18714   return tag_type;
18715 }
18716
18717 /* Parse an (optional) member-specification.
18718
18719    member-specification:
18720      member-declaration member-specification [opt]
18721      access-specifier : member-specification [opt]  */
18722
18723 static void
18724 cp_parser_member_specification_opt (cp_parser* parser)
18725 {
18726   while (true)
18727     {
18728       cp_token *token;
18729       enum rid keyword;
18730
18731       /* Peek at the next token.  */
18732       token = cp_lexer_peek_token (parser->lexer);
18733       /* If it's a `}', or EOF then we've seen all the members.  */
18734       if (token->type == CPP_CLOSE_BRACE
18735           || token->type == CPP_EOF
18736           || token->type == CPP_PRAGMA_EOL)
18737         break;
18738
18739       /* See if this token is a keyword.  */
18740       keyword = token->keyword;
18741       switch (keyword)
18742         {
18743         case RID_PUBLIC:
18744         case RID_PROTECTED:
18745         case RID_PRIVATE:
18746           /* Consume the access-specifier.  */
18747           cp_lexer_consume_token (parser->lexer);
18748           /* Remember which access-specifier is active.  */
18749           current_access_specifier = token->u.value;
18750           /* Look for the `:'.  */
18751           cp_parser_require (parser, CPP_COLON, RT_COLON);
18752           break;
18753
18754         default:
18755           /* Accept #pragmas at class scope.  */
18756           if (token->type == CPP_PRAGMA)
18757             {
18758               cp_parser_pragma (parser, pragma_external);
18759               break;
18760             }
18761
18762           /* Otherwise, the next construction must be a
18763              member-declaration.  */
18764           cp_parser_member_declaration (parser);
18765         }
18766     }
18767 }
18768
18769 /* Parse a member-declaration.
18770
18771    member-declaration:
18772      decl-specifier-seq [opt] member-declarator-list [opt] ;
18773      function-definition ; [opt]
18774      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18775      using-declaration
18776      template-declaration
18777      alias-declaration
18778
18779    member-declarator-list:
18780      member-declarator
18781      member-declarator-list , member-declarator
18782
18783    member-declarator:
18784      declarator pure-specifier [opt]
18785      declarator constant-initializer [opt]
18786      identifier [opt] : constant-expression
18787
18788    GNU Extensions:
18789
18790    member-declaration:
18791      __extension__ member-declaration
18792
18793    member-declarator:
18794      declarator attributes [opt] pure-specifier [opt]
18795      declarator attributes [opt] constant-initializer [opt]
18796      identifier [opt] attributes [opt] : constant-expression  
18797
18798    C++0x Extensions:
18799
18800    member-declaration:
18801      static_assert-declaration  */
18802
18803 static void
18804 cp_parser_member_declaration (cp_parser* parser)
18805 {
18806   cp_decl_specifier_seq decl_specifiers;
18807   tree prefix_attributes;
18808   tree decl;
18809   int declares_class_or_enum;
18810   bool friend_p;
18811   cp_token *token = NULL;
18812   cp_token *decl_spec_token_start = NULL;
18813   cp_token *initializer_token_start = NULL;
18814   int saved_pedantic;
18815   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18816
18817   /* Check for the `__extension__' keyword.  */
18818   if (cp_parser_extension_opt (parser, &saved_pedantic))
18819     {
18820       /* Recurse.  */
18821       cp_parser_member_declaration (parser);
18822       /* Restore the old value of the PEDANTIC flag.  */
18823       pedantic = saved_pedantic;
18824
18825       return;
18826     }
18827
18828   /* Check for a template-declaration.  */
18829   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18830     {
18831       /* An explicit specialization here is an error condition, and we
18832          expect the specialization handler to detect and report this.  */
18833       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18834           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18835         cp_parser_explicit_specialization (parser);
18836       else
18837         cp_parser_template_declaration (parser, /*member_p=*/true);
18838
18839       return;
18840     }
18841
18842   /* Check for a using-declaration.  */
18843   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18844     {
18845       if (cxx_dialect < cxx0x)
18846         {
18847           /* Parse the using-declaration.  */
18848           cp_parser_using_declaration (parser,
18849                                        /*access_declaration_p=*/false);
18850           return;
18851         }
18852       else
18853         {
18854           tree decl;
18855           cp_parser_parse_tentatively (parser);
18856           decl = cp_parser_alias_declaration (parser);
18857           if (cp_parser_parse_definitely (parser))
18858             finish_member_declaration (decl);
18859           else
18860             cp_parser_using_declaration (parser,
18861                                          /*access_declaration_p=*/false);
18862           return;
18863         }
18864     }
18865
18866   /* Check for @defs.  */
18867   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18868     {
18869       tree ivar, member;
18870       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18871       ivar = ivar_chains;
18872       while (ivar)
18873         {
18874           member = ivar;
18875           ivar = TREE_CHAIN (member);
18876           TREE_CHAIN (member) = NULL_TREE;
18877           finish_member_declaration (member);
18878         }
18879       return;
18880     }
18881
18882   /* If the next token is `static_assert' we have a static assertion.  */
18883   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18884     {
18885       cp_parser_static_assert (parser, /*member_p=*/true);
18886       return;
18887     }
18888
18889   parser->colon_corrects_to_scope_p = false;
18890
18891   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18892       goto out;
18893
18894   /* Parse the decl-specifier-seq.  */
18895   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18896   cp_parser_decl_specifier_seq (parser,
18897                                 CP_PARSER_FLAGS_OPTIONAL,
18898                                 &decl_specifiers,
18899                                 &declares_class_or_enum);
18900   prefix_attributes = decl_specifiers.attributes;
18901   decl_specifiers.attributes = NULL_TREE;
18902   /* Check for an invalid type-name.  */
18903   if (!decl_specifiers.any_type_specifiers_p
18904       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18905     goto out;
18906   /* If there is no declarator, then the decl-specifier-seq should
18907      specify a type.  */
18908   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18909     {
18910       /* If there was no decl-specifier-seq, and the next token is a
18911          `;', then we have something like:
18912
18913            struct S { ; };
18914
18915          [class.mem]
18916
18917          Each member-declaration shall declare at least one member
18918          name of the class.  */
18919       if (!decl_specifiers.any_specifiers_p)
18920         {
18921           cp_token *token = cp_lexer_peek_token (parser->lexer);
18922           if (!in_system_header_at (token->location))
18923             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18924         }
18925       else
18926         {
18927           tree type;
18928
18929           /* See if this declaration is a friend.  */
18930           friend_p = cp_parser_friend_p (&decl_specifiers);
18931           /* If there were decl-specifiers, check to see if there was
18932              a class-declaration.  */
18933           type = check_tag_decl (&decl_specifiers);
18934           /* Nested classes have already been added to the class, but
18935              a `friend' needs to be explicitly registered.  */
18936           if (friend_p)
18937             {
18938               /* If the `friend' keyword was present, the friend must
18939                  be introduced with a class-key.  */
18940                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18941                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18942                           "in C++03 a class-key must be used "
18943                           "when declaring a friend");
18944                /* In this case:
18945
18946                     template <typename T> struct A {
18947                       friend struct A<T>::B;
18948                     };
18949
18950                   A<T>::B will be represented by a TYPENAME_TYPE, and
18951                   therefore not recognized by check_tag_decl.  */
18952                if (!type)
18953                  {
18954                    type = decl_specifiers.type;
18955                    if (type && TREE_CODE (type) == TYPE_DECL)
18956                      type = TREE_TYPE (type);
18957                  }
18958                if (!type || !TYPE_P (type))
18959                  error_at (decl_spec_token_start->location,
18960                            "friend declaration does not name a class or "
18961                            "function");
18962                else
18963                  make_friend_class (current_class_type, type,
18964                                     /*complain=*/true);
18965             }
18966           /* If there is no TYPE, an error message will already have
18967              been issued.  */
18968           else if (!type || type == error_mark_node)
18969             ;
18970           /* An anonymous aggregate has to be handled specially; such
18971              a declaration really declares a data member (with a
18972              particular type), as opposed to a nested class.  */
18973           else if (ANON_AGGR_TYPE_P (type))
18974             {
18975               /* C++11 9.5/6.  */
18976               if (decl_specifiers.storage_class != sc_none)
18977                 error_at (decl_spec_token_start->location,
18978                           "a storage class on an anonymous aggregate "
18979                           "in class scope is not allowed");
18980
18981               /* Remove constructors and such from TYPE, now that we
18982                  know it is an anonymous aggregate.  */
18983               fixup_anonymous_aggr (type);
18984               /* And make the corresponding data member.  */
18985               decl = build_decl (decl_spec_token_start->location,
18986                                  FIELD_DECL, NULL_TREE, type);
18987               /* Add it to the class.  */
18988               finish_member_declaration (decl);
18989             }
18990           else
18991             cp_parser_check_access_in_redeclaration
18992                                               (TYPE_NAME (type),
18993                                                decl_spec_token_start->location);
18994         }
18995     }
18996   else
18997     {
18998       bool assume_semicolon = false;
18999
19000       /* See if these declarations will be friends.  */
19001       friend_p = cp_parser_friend_p (&decl_specifiers);
19002
19003       /* Keep going until we hit the `;' at the end of the
19004          declaration.  */
19005       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19006         {
19007           tree attributes = NULL_TREE;
19008           tree first_attribute;
19009
19010           /* Peek at the next token.  */
19011           token = cp_lexer_peek_token (parser->lexer);
19012
19013           /* Check for a bitfield declaration.  */
19014           if (token->type == CPP_COLON
19015               || (token->type == CPP_NAME
19016                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19017                   == CPP_COLON))
19018             {
19019               tree identifier;
19020               tree width;
19021
19022               /* Get the name of the bitfield.  Note that we cannot just
19023                  check TOKEN here because it may have been invalidated by
19024                  the call to cp_lexer_peek_nth_token above.  */
19025               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19026                 identifier = cp_parser_identifier (parser);
19027               else
19028                 identifier = NULL_TREE;
19029
19030               /* Consume the `:' token.  */
19031               cp_lexer_consume_token (parser->lexer);
19032               /* Get the width of the bitfield.  */
19033               width
19034                 = cp_parser_constant_expression (parser,
19035                                                  /*allow_non_constant=*/false,
19036                                                  NULL);
19037
19038               /* Look for attributes that apply to the bitfield.  */
19039               attributes = cp_parser_attributes_opt (parser);
19040               /* Remember which attributes are prefix attributes and
19041                  which are not.  */
19042               first_attribute = attributes;
19043               /* Combine the attributes.  */
19044               attributes = chainon (prefix_attributes, attributes);
19045
19046               /* Create the bitfield declaration.  */
19047               decl = grokbitfield (identifier
19048                                    ? make_id_declarator (NULL_TREE,
19049                                                          identifier,
19050                                                          sfk_none)
19051                                    : NULL,
19052                                    &decl_specifiers,
19053                                    width,
19054                                    attributes);
19055             }
19056           else
19057             {
19058               cp_declarator *declarator;
19059               tree initializer;
19060               tree asm_specification;
19061               int ctor_dtor_or_conv_p;
19062
19063               /* Parse the declarator.  */
19064               declarator
19065                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19066                                         &ctor_dtor_or_conv_p,
19067                                         /*parenthesized_p=*/NULL,
19068                                         /*member_p=*/true);
19069
19070               /* If something went wrong parsing the declarator, make sure
19071                  that we at least consume some tokens.  */
19072               if (declarator == cp_error_declarator)
19073                 {
19074                   /* Skip to the end of the statement.  */
19075                   cp_parser_skip_to_end_of_statement (parser);
19076                   /* If the next token is not a semicolon, that is
19077                      probably because we just skipped over the body of
19078                      a function.  So, we consume a semicolon if
19079                      present, but do not issue an error message if it
19080                      is not present.  */
19081                   if (cp_lexer_next_token_is (parser->lexer,
19082                                               CPP_SEMICOLON))
19083                     cp_lexer_consume_token (parser->lexer);
19084                   goto out;
19085                 }
19086
19087               if (declares_class_or_enum & 2)
19088                 cp_parser_check_for_definition_in_return_type
19089                                             (declarator, decl_specifiers.type,
19090                                              decl_specifiers.locations[ds_type_spec]);
19091
19092               /* Look for an asm-specification.  */
19093               asm_specification = cp_parser_asm_specification_opt (parser);
19094               /* Look for attributes that apply to the declaration.  */
19095               attributes = cp_parser_attributes_opt (parser);
19096               /* Remember which attributes are prefix attributes and
19097                  which are not.  */
19098               first_attribute = attributes;
19099               /* Combine the attributes.  */
19100               attributes = chainon (prefix_attributes, attributes);
19101
19102               /* If it's an `=', then we have a constant-initializer or a
19103                  pure-specifier.  It is not correct to parse the
19104                  initializer before registering the member declaration
19105                  since the member declaration should be in scope while
19106                  its initializer is processed.  However, the rest of the
19107                  front end does not yet provide an interface that allows
19108                  us to handle this correctly.  */
19109               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19110                 {
19111                   /* In [class.mem]:
19112
19113                      A pure-specifier shall be used only in the declaration of
19114                      a virtual function.
19115
19116                      A member-declarator can contain a constant-initializer
19117                      only if it declares a static member of integral or
19118                      enumeration type.
19119
19120                      Therefore, if the DECLARATOR is for a function, we look
19121                      for a pure-specifier; otherwise, we look for a
19122                      constant-initializer.  When we call `grokfield', it will
19123                      perform more stringent semantics checks.  */
19124                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19125                   if (function_declarator_p (declarator)
19126                       || (decl_specifiers.type
19127                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19128                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19129                               == FUNCTION_TYPE)))
19130                     initializer = cp_parser_pure_specifier (parser);
19131                   else if (decl_specifiers.storage_class != sc_static)
19132                     initializer = cp_parser_save_nsdmi (parser);
19133                   else if (cxx_dialect >= cxx0x)
19134                     {
19135                       bool nonconst;
19136                       /* Don't require a constant rvalue in C++11, since we
19137                          might want a reference constant.  We'll enforce
19138                          constancy later.  */
19139                       cp_lexer_consume_token (parser->lexer);
19140                       /* Parse the initializer.  */
19141                       initializer = cp_parser_initializer_clause (parser,
19142                                                                   &nonconst);
19143                     }
19144                   else
19145                     /* Parse the initializer.  */
19146                     initializer = cp_parser_constant_initializer (parser);
19147                 }
19148               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19149                        && !function_declarator_p (declarator))
19150                 {
19151                   bool x;
19152                   if (decl_specifiers.storage_class != sc_static)
19153                     initializer = cp_parser_save_nsdmi (parser);
19154                   else
19155                     initializer = cp_parser_initializer (parser, &x, &x);
19156                 }
19157               /* Otherwise, there is no initializer.  */
19158               else
19159                 initializer = NULL_TREE;
19160
19161               /* See if we are probably looking at a function
19162                  definition.  We are certainly not looking at a
19163                  member-declarator.  Calling `grokfield' has
19164                  side-effects, so we must not do it unless we are sure
19165                  that we are looking at a member-declarator.  */
19166               if (cp_parser_token_starts_function_definition_p
19167                   (cp_lexer_peek_token (parser->lexer)))
19168                 {
19169                   /* The grammar does not allow a pure-specifier to be
19170                      used when a member function is defined.  (It is
19171                      possible that this fact is an oversight in the
19172                      standard, since a pure function may be defined
19173                      outside of the class-specifier.  */
19174                   if (initializer && initializer_token_start)
19175                     error_at (initializer_token_start->location,
19176                               "pure-specifier on function-definition");
19177                   decl = cp_parser_save_member_function_body (parser,
19178                                                               &decl_specifiers,
19179                                                               declarator,
19180                                                               attributes);
19181                   /* If the member was not a friend, declare it here.  */
19182                   if (!friend_p)
19183                     finish_member_declaration (decl);
19184                   /* Peek at the next token.  */
19185                   token = cp_lexer_peek_token (parser->lexer);
19186                   /* If the next token is a semicolon, consume it.  */
19187                   if (token->type == CPP_SEMICOLON)
19188                     cp_lexer_consume_token (parser->lexer);
19189                   goto out;
19190                 }
19191               else
19192                 if (declarator->kind == cdk_function)
19193                   declarator->id_loc = token->location;
19194                 /* Create the declaration.  */
19195                 decl = grokfield (declarator, &decl_specifiers,
19196                                   initializer, /*init_const_expr_p=*/true,
19197                                   asm_specification,
19198                                   attributes);
19199             }
19200
19201           /* Reset PREFIX_ATTRIBUTES.  */
19202           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19203             attributes = TREE_CHAIN (attributes);
19204           if (attributes)
19205             TREE_CHAIN (attributes) = NULL_TREE;
19206
19207           /* If there is any qualification still in effect, clear it
19208              now; we will be starting fresh with the next declarator.  */
19209           parser->scope = NULL_TREE;
19210           parser->qualifying_scope = NULL_TREE;
19211           parser->object_scope = NULL_TREE;
19212           /* If it's a `,', then there are more declarators.  */
19213           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19214             cp_lexer_consume_token (parser->lexer);
19215           /* If the next token isn't a `;', then we have a parse error.  */
19216           else if (cp_lexer_next_token_is_not (parser->lexer,
19217                                                CPP_SEMICOLON))
19218             {
19219               /* The next token might be a ways away from where the
19220                  actual semicolon is missing.  Find the previous token
19221                  and use that for our error position.  */
19222               cp_token *token = cp_lexer_previous_token (parser->lexer);
19223               error_at (token->location,
19224                         "expected %<;%> at end of member declaration");
19225
19226               /* Assume that the user meant to provide a semicolon.  If
19227                  we were to cp_parser_skip_to_end_of_statement, we might
19228                  skip to a semicolon inside a member function definition
19229                  and issue nonsensical error messages.  */
19230               assume_semicolon = true;
19231             }
19232
19233           if (decl)
19234             {
19235               /* Add DECL to the list of members.  */
19236               if (!friend_p)
19237                 finish_member_declaration (decl);
19238
19239               if (TREE_CODE (decl) == FUNCTION_DECL)
19240                 cp_parser_save_default_args (parser, decl);
19241               else if (TREE_CODE (decl) == FIELD_DECL
19242                        && !DECL_C_BIT_FIELD (decl)
19243                        && DECL_INITIAL (decl))
19244                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19245                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19246             }
19247
19248           if (assume_semicolon)
19249             goto out;
19250         }
19251     }
19252
19253   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19254  out:
19255   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19256 }
19257
19258 /* Parse a pure-specifier.
19259
19260    pure-specifier:
19261      = 0
19262
19263    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19264    Otherwise, ERROR_MARK_NODE is returned.  */
19265
19266 static tree
19267 cp_parser_pure_specifier (cp_parser* parser)
19268 {
19269   cp_token *token;
19270
19271   /* Look for the `=' token.  */
19272   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19273     return error_mark_node;
19274   /* Look for the `0' token.  */
19275   token = cp_lexer_peek_token (parser->lexer);
19276
19277   if (token->type == CPP_EOF
19278       || token->type == CPP_PRAGMA_EOL)
19279     return error_mark_node;
19280
19281   cp_lexer_consume_token (parser->lexer);
19282
19283   /* Accept = default or = delete in c++0x mode.  */
19284   if (token->keyword == RID_DEFAULT
19285       || token->keyword == RID_DELETE)
19286     {
19287       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19288       return token->u.value;
19289     }
19290
19291   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19292   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19293     {
19294       cp_parser_error (parser,
19295                        "invalid pure specifier (only %<= 0%> is allowed)");
19296       cp_parser_skip_to_end_of_statement (parser);
19297       return error_mark_node;
19298     }
19299   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19300     {
19301       error_at (token->location, "templates may not be %<virtual%>");
19302       return error_mark_node;
19303     }
19304
19305   return integer_zero_node;
19306 }
19307
19308 /* Parse a constant-initializer.
19309
19310    constant-initializer:
19311      = constant-expression
19312
19313    Returns a representation of the constant-expression.  */
19314
19315 static tree
19316 cp_parser_constant_initializer (cp_parser* parser)
19317 {
19318   /* Look for the `=' token.  */
19319   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19320     return error_mark_node;
19321
19322   /* It is invalid to write:
19323
19324        struct S { static const int i = { 7 }; };
19325
19326      */
19327   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19328     {
19329       cp_parser_error (parser,
19330                        "a brace-enclosed initializer is not allowed here");
19331       /* Consume the opening brace.  */
19332       cp_lexer_consume_token (parser->lexer);
19333       /* Skip the initializer.  */
19334       cp_parser_skip_to_closing_brace (parser);
19335       /* Look for the trailing `}'.  */
19336       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19337
19338       return error_mark_node;
19339     }
19340
19341   return cp_parser_constant_expression (parser,
19342                                         /*allow_non_constant=*/false,
19343                                         NULL);
19344 }
19345
19346 /* Derived classes [gram.class.derived] */
19347
19348 /* Parse a base-clause.
19349
19350    base-clause:
19351      : base-specifier-list
19352
19353    base-specifier-list:
19354      base-specifier ... [opt]
19355      base-specifier-list , base-specifier ... [opt]
19356
19357    Returns a TREE_LIST representing the base-classes, in the order in
19358    which they were declared.  The representation of each node is as
19359    described by cp_parser_base_specifier.
19360
19361    In the case that no bases are specified, this function will return
19362    NULL_TREE, not ERROR_MARK_NODE.  */
19363
19364 static tree
19365 cp_parser_base_clause (cp_parser* parser)
19366 {
19367   tree bases = NULL_TREE;
19368
19369   /* Look for the `:' that begins the list.  */
19370   cp_parser_require (parser, CPP_COLON, RT_COLON);
19371
19372   /* Scan the base-specifier-list.  */
19373   while (true)
19374     {
19375       cp_token *token;
19376       tree base;
19377       bool pack_expansion_p = false;
19378
19379       /* Look for the base-specifier.  */
19380       base = cp_parser_base_specifier (parser);
19381       /* Look for the (optional) ellipsis. */
19382       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19383         {
19384           /* Consume the `...'. */
19385           cp_lexer_consume_token (parser->lexer);
19386
19387           pack_expansion_p = true;
19388         }
19389
19390       /* Add BASE to the front of the list.  */
19391       if (base && base != error_mark_node)
19392         {
19393           if (pack_expansion_p)
19394             /* Make this a pack expansion type. */
19395             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19396
19397           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19398             {
19399               TREE_CHAIN (base) = bases;
19400               bases = base;
19401             }
19402         }
19403       /* Peek at the next token.  */
19404       token = cp_lexer_peek_token (parser->lexer);
19405       /* If it's not a comma, then the list is complete.  */
19406       if (token->type != CPP_COMMA)
19407         break;
19408       /* Consume the `,'.  */
19409       cp_lexer_consume_token (parser->lexer);
19410     }
19411
19412   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19413      base class had a qualified name.  However, the next name that
19414      appears is certainly not qualified.  */
19415   parser->scope = NULL_TREE;
19416   parser->qualifying_scope = NULL_TREE;
19417   parser->object_scope = NULL_TREE;
19418
19419   return nreverse (bases);
19420 }
19421
19422 /* Parse a base-specifier.
19423
19424    base-specifier:
19425      :: [opt] nested-name-specifier [opt] class-name
19426      virtual access-specifier [opt] :: [opt] nested-name-specifier
19427        [opt] class-name
19428      access-specifier virtual [opt] :: [opt] nested-name-specifier
19429        [opt] class-name
19430
19431    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19432    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19433    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19434    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19435
19436 static tree
19437 cp_parser_base_specifier (cp_parser* parser)
19438 {
19439   cp_token *token;
19440   bool done = false;
19441   bool virtual_p = false;
19442   bool duplicate_virtual_error_issued_p = false;
19443   bool duplicate_access_error_issued_p = false;
19444   bool class_scope_p, template_p;
19445   tree access = access_default_node;
19446   tree type;
19447
19448   /* Process the optional `virtual' and `access-specifier'.  */
19449   while (!done)
19450     {
19451       /* Peek at the next token.  */
19452       token = cp_lexer_peek_token (parser->lexer);
19453       /* Process `virtual'.  */
19454       switch (token->keyword)
19455         {
19456         case RID_VIRTUAL:
19457           /* If `virtual' appears more than once, issue an error.  */
19458           if (virtual_p && !duplicate_virtual_error_issued_p)
19459             {
19460               cp_parser_error (parser,
19461                                "%<virtual%> specified more than once in base-specified");
19462               duplicate_virtual_error_issued_p = true;
19463             }
19464
19465           virtual_p = true;
19466
19467           /* Consume the `virtual' token.  */
19468           cp_lexer_consume_token (parser->lexer);
19469
19470           break;
19471
19472         case RID_PUBLIC:
19473         case RID_PROTECTED:
19474         case RID_PRIVATE:
19475           /* If more than one access specifier appears, issue an
19476              error.  */
19477           if (access != access_default_node
19478               && !duplicate_access_error_issued_p)
19479             {
19480               cp_parser_error (parser,
19481                                "more than one access specifier in base-specified");
19482               duplicate_access_error_issued_p = true;
19483             }
19484
19485           access = ridpointers[(int) token->keyword];
19486
19487           /* Consume the access-specifier.  */
19488           cp_lexer_consume_token (parser->lexer);
19489
19490           break;
19491
19492         default:
19493           done = true;
19494           break;
19495         }
19496     }
19497   /* It is not uncommon to see programs mechanically, erroneously, use
19498      the 'typename' keyword to denote (dependent) qualified types
19499      as base classes.  */
19500   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19501     {
19502       token = cp_lexer_peek_token (parser->lexer);
19503       if (!processing_template_decl)
19504         error_at (token->location,
19505                   "keyword %<typename%> not allowed outside of templates");
19506       else
19507         error_at (token->location,
19508                   "keyword %<typename%> not allowed in this context "
19509                   "(the base class is implicitly a type)");
19510       cp_lexer_consume_token (parser->lexer);
19511     }
19512
19513   /* Look for the optional `::' operator.  */
19514   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19515   /* Look for the nested-name-specifier.  The simplest way to
19516      implement:
19517
19518        [temp.res]
19519
19520        The keyword `typename' is not permitted in a base-specifier or
19521        mem-initializer; in these contexts a qualified name that
19522        depends on a template-parameter is implicitly assumed to be a
19523        type name.
19524
19525      is to pretend that we have seen the `typename' keyword at this
19526      point.  */
19527   cp_parser_nested_name_specifier_opt (parser,
19528                                        /*typename_keyword_p=*/true,
19529                                        /*check_dependency_p=*/true,
19530                                        typename_type,
19531                                        /*is_declaration=*/true);
19532   /* If the base class is given by a qualified name, assume that names
19533      we see are type names or templates, as appropriate.  */
19534   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19535   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19536
19537   if (!parser->scope
19538       && cp_lexer_next_token_is_decltype (parser->lexer))
19539     /* DR 950 allows decltype as a base-specifier.  */
19540     type = cp_parser_decltype (parser);
19541   else
19542     {
19543       /* Otherwise, look for the class-name.  */
19544       type = cp_parser_class_name (parser,
19545                                    class_scope_p,
19546                                    template_p,
19547                                    typename_type,
19548                                    /*check_dependency_p=*/true,
19549                                    /*class_head_p=*/false,
19550                                    /*is_declaration=*/true);
19551       type = TREE_TYPE (type);
19552     }
19553
19554   if (type == error_mark_node)
19555     return error_mark_node;
19556
19557   return finish_base_specifier (type, access, virtual_p);
19558 }
19559
19560 /* Exception handling [gram.exception] */
19561
19562 /* Parse an (optional) noexcept-specification.
19563
19564    noexcept-specification:
19565      noexcept ( constant-expression ) [opt]
19566
19567    If no noexcept-specification is present, returns NULL_TREE.
19568    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19569    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19570    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19571    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19572    in which case a boolean condition is returned instead.  */
19573
19574 static tree
19575 cp_parser_noexcept_specification_opt (cp_parser* parser,
19576                                       bool require_constexpr,
19577                                       bool* consumed_expr,
19578                                       bool return_cond)
19579 {
19580   cp_token *token;
19581   const char *saved_message;
19582
19583   /* Peek at the next token.  */
19584   token = cp_lexer_peek_token (parser->lexer);
19585
19586   /* Is it a noexcept-specification?  */
19587   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19588     {
19589       tree expr;
19590       cp_lexer_consume_token (parser->lexer);
19591
19592       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19593         {
19594           cp_lexer_consume_token (parser->lexer);
19595
19596           if (require_constexpr)
19597             {
19598               /* Types may not be defined in an exception-specification.  */
19599               saved_message = parser->type_definition_forbidden_message;
19600               parser->type_definition_forbidden_message
19601               = G_("types may not be defined in an exception-specification");
19602
19603               expr = cp_parser_constant_expression (parser, false, NULL);
19604
19605               /* Restore the saved message.  */
19606               parser->type_definition_forbidden_message = saved_message;
19607             }
19608           else
19609             {
19610               expr = cp_parser_expression (parser, false, NULL);
19611               *consumed_expr = true;
19612             }
19613
19614           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19615         }
19616       else
19617         {
19618           expr = boolean_true_node;
19619           if (!require_constexpr)
19620             *consumed_expr = false;
19621         }
19622
19623       /* We cannot build a noexcept-spec right away because this will check
19624          that expr is a constexpr.  */
19625       if (!return_cond)
19626         return build_noexcept_spec (expr, tf_warning_or_error);
19627       else
19628         return expr;
19629     }
19630   else
19631     return NULL_TREE;
19632 }
19633
19634 /* Parse an (optional) exception-specification.
19635
19636    exception-specification:
19637      throw ( type-id-list [opt] )
19638
19639    Returns a TREE_LIST representing the exception-specification.  The
19640    TREE_VALUE of each node is a type.  */
19641
19642 static tree
19643 cp_parser_exception_specification_opt (cp_parser* parser)
19644 {
19645   cp_token *token;
19646   tree type_id_list;
19647   const char *saved_message;
19648
19649   /* Peek at the next token.  */
19650   token = cp_lexer_peek_token (parser->lexer);
19651
19652   /* Is it a noexcept-specification?  */
19653   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19654                                                       false);
19655   if (type_id_list != NULL_TREE)
19656     return type_id_list;
19657
19658   /* If it's not `throw', then there's no exception-specification.  */
19659   if (!cp_parser_is_keyword (token, RID_THROW))
19660     return NULL_TREE;
19661
19662 #if 0
19663   /* Enable this once a lot of code has transitioned to noexcept?  */
19664   if (cxx_dialect >= cxx0x && !in_system_header)
19665     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19666              "deprecated in C++0x; use %<noexcept%> instead");
19667 #endif
19668
19669   /* Consume the `throw'.  */
19670   cp_lexer_consume_token (parser->lexer);
19671
19672   /* Look for the `('.  */
19673   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19674
19675   /* Peek at the next token.  */
19676   token = cp_lexer_peek_token (parser->lexer);
19677   /* If it's not a `)', then there is a type-id-list.  */
19678   if (token->type != CPP_CLOSE_PAREN)
19679     {
19680       /* Types may not be defined in an exception-specification.  */
19681       saved_message = parser->type_definition_forbidden_message;
19682       parser->type_definition_forbidden_message
19683         = G_("types may not be defined in an exception-specification");
19684       /* Parse the type-id-list.  */
19685       type_id_list = cp_parser_type_id_list (parser);
19686       /* Restore the saved message.  */
19687       parser->type_definition_forbidden_message = saved_message;
19688     }
19689   else
19690     type_id_list = empty_except_spec;
19691
19692   /* Look for the `)'.  */
19693   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19694
19695   return type_id_list;
19696 }
19697
19698 /* Parse an (optional) type-id-list.
19699
19700    type-id-list:
19701      type-id ... [opt]
19702      type-id-list , type-id ... [opt]
19703
19704    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19705    in the order that the types were presented.  */
19706
19707 static tree
19708 cp_parser_type_id_list (cp_parser* parser)
19709 {
19710   tree types = NULL_TREE;
19711
19712   while (true)
19713     {
19714       cp_token *token;
19715       tree type;
19716
19717       /* Get the next type-id.  */
19718       type = cp_parser_type_id (parser);
19719       /* Parse the optional ellipsis. */
19720       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19721         {
19722           /* Consume the `...'. */
19723           cp_lexer_consume_token (parser->lexer);
19724
19725           /* Turn the type into a pack expansion expression. */
19726           type = make_pack_expansion (type);
19727         }
19728       /* Add it to the list.  */
19729       types = add_exception_specifier (types, type, /*complain=*/1);
19730       /* Peek at the next token.  */
19731       token = cp_lexer_peek_token (parser->lexer);
19732       /* If it is not a `,', we are done.  */
19733       if (token->type != CPP_COMMA)
19734         break;
19735       /* Consume the `,'.  */
19736       cp_lexer_consume_token (parser->lexer);
19737     }
19738
19739   return nreverse (types);
19740 }
19741
19742 /* Parse a try-block.
19743
19744    try-block:
19745      try compound-statement handler-seq  */
19746
19747 static tree
19748 cp_parser_try_block (cp_parser* parser)
19749 {
19750   tree try_block;
19751
19752   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19753   try_block = begin_try_block ();
19754   cp_parser_compound_statement (parser, NULL, true, false);
19755   finish_try_block (try_block);
19756   cp_parser_handler_seq (parser);
19757   finish_handler_sequence (try_block);
19758
19759   return try_block;
19760 }
19761
19762 /* Parse a function-try-block.
19763
19764    function-try-block:
19765      try ctor-initializer [opt] function-body handler-seq  */
19766
19767 static bool
19768 cp_parser_function_try_block (cp_parser* parser)
19769 {
19770   tree compound_stmt;
19771   tree try_block;
19772   bool ctor_initializer_p;
19773
19774   /* Look for the `try' keyword.  */
19775   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19776     return false;
19777   /* Let the rest of the front end know where we are.  */
19778   try_block = begin_function_try_block (&compound_stmt);
19779   /* Parse the function-body.  */
19780   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19781     (parser, /*in_function_try_block=*/true);
19782   /* We're done with the `try' part.  */
19783   finish_function_try_block (try_block);
19784   /* Parse the handlers.  */
19785   cp_parser_handler_seq (parser);
19786   /* We're done with the handlers.  */
19787   finish_function_handler_sequence (try_block, compound_stmt);
19788
19789   return ctor_initializer_p;
19790 }
19791
19792 /* Parse a handler-seq.
19793
19794    handler-seq:
19795      handler handler-seq [opt]  */
19796
19797 static void
19798 cp_parser_handler_seq (cp_parser* parser)
19799 {
19800   while (true)
19801     {
19802       cp_token *token;
19803
19804       /* Parse the handler.  */
19805       cp_parser_handler (parser);
19806       /* Peek at the next token.  */
19807       token = cp_lexer_peek_token (parser->lexer);
19808       /* If it's not `catch' then there are no more handlers.  */
19809       if (!cp_parser_is_keyword (token, RID_CATCH))
19810         break;
19811     }
19812 }
19813
19814 /* Parse a handler.
19815
19816    handler:
19817      catch ( exception-declaration ) compound-statement  */
19818
19819 static void
19820 cp_parser_handler (cp_parser* parser)
19821 {
19822   tree handler;
19823   tree declaration;
19824
19825   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19826   handler = begin_handler ();
19827   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19828   declaration = cp_parser_exception_declaration (parser);
19829   finish_handler_parms (declaration, handler);
19830   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19831   cp_parser_compound_statement (parser, NULL, false, false);
19832   finish_handler (handler);
19833 }
19834
19835 /* Parse an exception-declaration.
19836
19837    exception-declaration:
19838      type-specifier-seq declarator
19839      type-specifier-seq abstract-declarator
19840      type-specifier-seq
19841      ...
19842
19843    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19844    ellipsis variant is used.  */
19845
19846 static tree
19847 cp_parser_exception_declaration (cp_parser* parser)
19848 {
19849   cp_decl_specifier_seq type_specifiers;
19850   cp_declarator *declarator;
19851   const char *saved_message;
19852
19853   /* If it's an ellipsis, it's easy to handle.  */
19854   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19855     {
19856       /* Consume the `...' token.  */
19857       cp_lexer_consume_token (parser->lexer);
19858       return NULL_TREE;
19859     }
19860
19861   /* Types may not be defined in exception-declarations.  */
19862   saved_message = parser->type_definition_forbidden_message;
19863   parser->type_definition_forbidden_message
19864     = G_("types may not be defined in exception-declarations");
19865
19866   /* Parse the type-specifier-seq.  */
19867   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19868                                 /*is_trailing_return=*/false,
19869                                 &type_specifiers);
19870   /* If it's a `)', then there is no declarator.  */
19871   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19872     declarator = NULL;
19873   else
19874     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19875                                        /*ctor_dtor_or_conv_p=*/NULL,
19876                                        /*parenthesized_p=*/NULL,
19877                                        /*member_p=*/false);
19878
19879   /* Restore the saved message.  */
19880   parser->type_definition_forbidden_message = saved_message;
19881
19882   if (!type_specifiers.any_specifiers_p)
19883     return error_mark_node;
19884
19885   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19886 }
19887
19888 /* Parse a throw-expression.
19889
19890    throw-expression:
19891      throw assignment-expression [opt]
19892
19893    Returns a THROW_EXPR representing the throw-expression.  */
19894
19895 static tree
19896 cp_parser_throw_expression (cp_parser* parser)
19897 {
19898   tree expression;
19899   cp_token* token;
19900
19901   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19902   token = cp_lexer_peek_token (parser->lexer);
19903   /* Figure out whether or not there is an assignment-expression
19904      following the "throw" keyword.  */
19905   if (token->type == CPP_COMMA
19906       || token->type == CPP_SEMICOLON
19907       || token->type == CPP_CLOSE_PAREN
19908       || token->type == CPP_CLOSE_SQUARE
19909       || token->type == CPP_CLOSE_BRACE
19910       || token->type == CPP_COLON)
19911     expression = NULL_TREE;
19912   else
19913     expression = cp_parser_assignment_expression (parser,
19914                                                   /*cast_p=*/false, NULL);
19915
19916   return build_throw (expression);
19917 }
19918
19919 /* GNU Extensions */
19920
19921 /* Parse an (optional) asm-specification.
19922
19923    asm-specification:
19924      asm ( string-literal )
19925
19926    If the asm-specification is present, returns a STRING_CST
19927    corresponding to the string-literal.  Otherwise, returns
19928    NULL_TREE.  */
19929
19930 static tree
19931 cp_parser_asm_specification_opt (cp_parser* parser)
19932 {
19933   cp_token *token;
19934   tree asm_specification;
19935
19936   /* Peek at the next token.  */
19937   token = cp_lexer_peek_token (parser->lexer);
19938   /* If the next token isn't the `asm' keyword, then there's no
19939      asm-specification.  */
19940   if (!cp_parser_is_keyword (token, RID_ASM))
19941     return NULL_TREE;
19942
19943   /* Consume the `asm' token.  */
19944   cp_lexer_consume_token (parser->lexer);
19945   /* Look for the `('.  */
19946   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19947
19948   /* Look for the string-literal.  */
19949   asm_specification = cp_parser_string_literal (parser, false, false);
19950
19951   /* Look for the `)'.  */
19952   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19953
19954   return asm_specification;
19955 }
19956
19957 /* Parse an asm-operand-list.
19958
19959    asm-operand-list:
19960      asm-operand
19961      asm-operand-list , asm-operand
19962
19963    asm-operand:
19964      string-literal ( expression )
19965      [ string-literal ] string-literal ( expression )
19966
19967    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19968    each node is the expression.  The TREE_PURPOSE is itself a
19969    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19970    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19971    is a STRING_CST for the string literal before the parenthesis. Returns
19972    ERROR_MARK_NODE if any of the operands are invalid.  */
19973
19974 static tree
19975 cp_parser_asm_operand_list (cp_parser* parser)
19976 {
19977   tree asm_operands = NULL_TREE;
19978   bool invalid_operands = false;
19979
19980   while (true)
19981     {
19982       tree string_literal;
19983       tree expression;
19984       tree name;
19985
19986       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19987         {
19988           /* Consume the `[' token.  */
19989           cp_lexer_consume_token (parser->lexer);
19990           /* Read the operand name.  */
19991           name = cp_parser_identifier (parser);
19992           if (name != error_mark_node)
19993             name = build_string (IDENTIFIER_LENGTH (name),
19994                                  IDENTIFIER_POINTER (name));
19995           /* Look for the closing `]'.  */
19996           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19997         }
19998       else
19999         name = NULL_TREE;
20000       /* Look for the string-literal.  */
20001       string_literal = cp_parser_string_literal (parser, false, false);
20002
20003       /* Look for the `('.  */
20004       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20005       /* Parse the expression.  */
20006       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20007       /* Look for the `)'.  */
20008       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20009
20010       if (name == error_mark_node 
20011           || string_literal == error_mark_node 
20012           || expression == error_mark_node)
20013         invalid_operands = true;
20014
20015       /* Add this operand to the list.  */
20016       asm_operands = tree_cons (build_tree_list (name, string_literal),
20017                                 expression,
20018                                 asm_operands);
20019       /* If the next token is not a `,', there are no more
20020          operands.  */
20021       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20022         break;
20023       /* Consume the `,'.  */
20024       cp_lexer_consume_token (parser->lexer);
20025     }
20026
20027   return invalid_operands ? error_mark_node : nreverse (asm_operands);
20028 }
20029
20030 /* Parse an asm-clobber-list.
20031
20032    asm-clobber-list:
20033      string-literal
20034      asm-clobber-list , string-literal
20035
20036    Returns a TREE_LIST, indicating the clobbers in the order that they
20037    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
20038
20039 static tree
20040 cp_parser_asm_clobber_list (cp_parser* parser)
20041 {
20042   tree clobbers = NULL_TREE;
20043
20044   while (true)
20045     {
20046       tree string_literal;
20047
20048       /* Look for the string literal.  */
20049       string_literal = cp_parser_string_literal (parser, false, false);
20050       /* Add it to the list.  */
20051       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20052       /* If the next token is not a `,', then the list is
20053          complete.  */
20054       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20055         break;
20056       /* Consume the `,' token.  */
20057       cp_lexer_consume_token (parser->lexer);
20058     }
20059
20060   return clobbers;
20061 }
20062
20063 /* Parse an asm-label-list.
20064
20065    asm-label-list:
20066      identifier
20067      asm-label-list , identifier
20068
20069    Returns a TREE_LIST, indicating the labels in the order that they
20070    appeared.  The TREE_VALUE of each node is a label.  */
20071
20072 static tree
20073 cp_parser_asm_label_list (cp_parser* parser)
20074 {
20075   tree labels = NULL_TREE;
20076
20077   while (true)
20078     {
20079       tree identifier, label, name;
20080
20081       /* Look for the identifier.  */
20082       identifier = cp_parser_identifier (parser);
20083       if (!error_operand_p (identifier))
20084         {
20085           label = lookup_label (identifier);
20086           if (TREE_CODE (label) == LABEL_DECL)
20087             {
20088               TREE_USED (label) = 1;
20089               check_goto (label);
20090               name = build_string (IDENTIFIER_LENGTH (identifier),
20091                                    IDENTIFIER_POINTER (identifier));
20092               labels = tree_cons (name, label, labels);
20093             }
20094         }
20095       /* If the next token is not a `,', then the list is
20096          complete.  */
20097       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20098         break;
20099       /* Consume the `,' token.  */
20100       cp_lexer_consume_token (parser->lexer);
20101     }
20102
20103   return nreverse (labels);
20104 }
20105
20106 /* Parse an (optional) series of attributes.
20107
20108    attributes:
20109      attributes attribute
20110
20111    attribute:
20112      __attribute__ (( attribute-list [opt] ))
20113
20114    The return value is as for cp_parser_attribute_list.  */
20115
20116 static tree
20117 cp_parser_attributes_opt (cp_parser* parser)
20118 {
20119   tree attributes = NULL_TREE;
20120
20121   while (true)
20122     {
20123       cp_token *token;
20124       tree attribute_list;
20125       bool ok = true;
20126
20127       /* Peek at the next token.  */
20128       token = cp_lexer_peek_token (parser->lexer);
20129       /* If it's not `__attribute__', then we're done.  */
20130       if (token->keyword != RID_ATTRIBUTE)
20131         break;
20132
20133       /* Consume the `__attribute__' keyword.  */
20134       cp_lexer_consume_token (parser->lexer);
20135       /* Look for the two `(' tokens.  */
20136       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20137       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20138
20139       /* Peek at the next token.  */
20140       token = cp_lexer_peek_token (parser->lexer);
20141       if (token->type != CPP_CLOSE_PAREN)
20142         /* Parse the attribute-list.  */
20143         attribute_list = cp_parser_attribute_list (parser);
20144       else
20145         /* If the next token is a `)', then there is no attribute
20146            list.  */
20147         attribute_list = NULL;
20148
20149       /* Look for the two `)' tokens.  */
20150       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20151         ok = false;
20152       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20153         ok = false;
20154       if (!ok)
20155         cp_parser_skip_to_end_of_statement (parser);
20156
20157       /* Add these new attributes to the list.  */
20158       attributes = chainon (attributes, attribute_list);
20159     }
20160
20161   return attributes;
20162 }
20163
20164 /* Parse an attribute-list.
20165
20166    attribute-list:
20167      attribute
20168      attribute-list , attribute
20169
20170    attribute:
20171      identifier
20172      identifier ( identifier )
20173      identifier ( identifier , expression-list )
20174      identifier ( expression-list )
20175
20176    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20177    to an attribute.  The TREE_PURPOSE of each node is the identifier
20178    indicating which attribute is in use.  The TREE_VALUE represents
20179    the arguments, if any.  */
20180
20181 static tree
20182 cp_parser_attribute_list (cp_parser* parser)
20183 {
20184   tree attribute_list = NULL_TREE;
20185   bool save_translate_strings_p = parser->translate_strings_p;
20186
20187   parser->translate_strings_p = false;
20188   while (true)
20189     {
20190       cp_token *token;
20191       tree identifier;
20192       tree attribute;
20193
20194       /* Look for the identifier.  We also allow keywords here; for
20195          example `__attribute__ ((const))' is legal.  */
20196       token = cp_lexer_peek_token (parser->lexer);
20197       if (token->type == CPP_NAME
20198           || token->type == CPP_KEYWORD)
20199         {
20200           tree arguments = NULL_TREE;
20201
20202           /* Consume the token.  */
20203           token = cp_lexer_consume_token (parser->lexer);
20204
20205           /* Save away the identifier that indicates which attribute
20206              this is.  */
20207           identifier = (token->type == CPP_KEYWORD) 
20208             /* For keywords, use the canonical spelling, not the
20209                parsed identifier.  */
20210             ? ridpointers[(int) token->keyword]
20211             : token->u.value;
20212           
20213           attribute = build_tree_list (identifier, NULL_TREE);
20214
20215           /* Peek at the next token.  */
20216           token = cp_lexer_peek_token (parser->lexer);
20217           /* If it's an `(', then parse the attribute arguments.  */
20218           if (token->type == CPP_OPEN_PAREN)
20219             {
20220               VEC(tree,gc) *vec;
20221               int attr_flag = (attribute_takes_identifier_p (identifier)
20222                                ? id_attr : normal_attr);
20223               vec = cp_parser_parenthesized_expression_list
20224                     (parser, attr_flag, /*cast_p=*/false,
20225                      /*allow_expansion_p=*/false,
20226                      /*non_constant_p=*/NULL);
20227               if (vec == NULL)
20228                 arguments = error_mark_node;
20229               else
20230                 {
20231                   arguments = build_tree_list_vec (vec);
20232                   release_tree_vector (vec);
20233                 }
20234               /* Save the arguments away.  */
20235               TREE_VALUE (attribute) = arguments;
20236             }
20237
20238           if (arguments != error_mark_node)
20239             {
20240               /* Add this attribute to the list.  */
20241               TREE_CHAIN (attribute) = attribute_list;
20242               attribute_list = attribute;
20243             }
20244
20245           token = cp_lexer_peek_token (parser->lexer);
20246         }
20247       /* Now, look for more attributes.  If the next token isn't a
20248          `,', we're done.  */
20249       if (token->type != CPP_COMMA)
20250         break;
20251
20252       /* Consume the comma and keep going.  */
20253       cp_lexer_consume_token (parser->lexer);
20254     }
20255   parser->translate_strings_p = save_translate_strings_p;
20256
20257   /* We built up the list in reverse order.  */
20258   return nreverse (attribute_list);
20259 }
20260
20261 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20262    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20263    current value of the PEDANTIC flag, regardless of whether or not
20264    the `__extension__' keyword is present.  The caller is responsible
20265    for restoring the value of the PEDANTIC flag.  */
20266
20267 static bool
20268 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20269 {
20270   /* Save the old value of the PEDANTIC flag.  */
20271   *saved_pedantic = pedantic;
20272
20273   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20274     {
20275       /* Consume the `__extension__' token.  */
20276       cp_lexer_consume_token (parser->lexer);
20277       /* We're not being pedantic while the `__extension__' keyword is
20278          in effect.  */
20279       pedantic = 0;
20280
20281       return true;
20282     }
20283
20284   return false;
20285 }
20286
20287 /* Parse a label declaration.
20288
20289    label-declaration:
20290      __label__ label-declarator-seq ;
20291
20292    label-declarator-seq:
20293      identifier , label-declarator-seq
20294      identifier  */
20295
20296 static void
20297 cp_parser_label_declaration (cp_parser* parser)
20298 {
20299   /* Look for the `__label__' keyword.  */
20300   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20301
20302   while (true)
20303     {
20304       tree identifier;
20305
20306       /* Look for an identifier.  */
20307       identifier = cp_parser_identifier (parser);
20308       /* If we failed, stop.  */
20309       if (identifier == error_mark_node)
20310         break;
20311       /* Declare it as a label.  */
20312       finish_label_decl (identifier);
20313       /* If the next token is a `;', stop.  */
20314       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20315         break;
20316       /* Look for the `,' separating the label declarations.  */
20317       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20318     }
20319
20320   /* Look for the final `;'.  */
20321   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20322 }
20323
20324 /* Support Functions */
20325
20326 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20327    NAME should have one of the representations used for an
20328    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20329    is returned.  If PARSER->SCOPE is a dependent type, then a
20330    SCOPE_REF is returned.
20331
20332    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20333    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20334    was formed.  Abstractly, such entities should not be passed to this
20335    function, because they do not need to be looked up, but it is
20336    simpler to check for this special case here, rather than at the
20337    call-sites.
20338
20339    In cases not explicitly covered above, this function returns a
20340    DECL, OVERLOAD, or baselink representing the result of the lookup.
20341    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20342    is returned.
20343
20344    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20345    (e.g., "struct") that was used.  In that case bindings that do not
20346    refer to types are ignored.
20347
20348    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20349    ignored.
20350
20351    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20352    are ignored.
20353
20354    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20355    types.
20356
20357    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20358    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20359    NULL_TREE otherwise.  */
20360
20361 static tree
20362 cp_parser_lookup_name (cp_parser *parser, tree name,
20363                        enum tag_types tag_type,
20364                        bool is_template,
20365                        bool is_namespace,
20366                        bool check_dependency,
20367                        tree *ambiguous_decls,
20368                        location_t name_location)
20369 {
20370   tree decl;
20371   tree object_type = parser->context->object_type;
20372
20373   /* Assume that the lookup will be unambiguous.  */
20374   if (ambiguous_decls)
20375     *ambiguous_decls = NULL_TREE;
20376
20377   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20378      no longer valid.  Note that if we are parsing tentatively, and
20379      the parse fails, OBJECT_TYPE will be automatically restored.  */
20380   parser->context->object_type = NULL_TREE;
20381
20382   if (name == error_mark_node)
20383     return error_mark_node;
20384
20385   /* A template-id has already been resolved; there is no lookup to
20386      do.  */
20387   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20388     return name;
20389   if (BASELINK_P (name))
20390     {
20391       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20392                   == TEMPLATE_ID_EXPR);
20393       return name;
20394     }
20395
20396   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20397      it should already have been checked to make sure that the name
20398      used matches the type being destroyed.  */
20399   if (TREE_CODE (name) == BIT_NOT_EXPR)
20400     {
20401       tree type;
20402
20403       /* Figure out to which type this destructor applies.  */
20404       if (parser->scope)
20405         type = parser->scope;
20406       else if (object_type)
20407         type = object_type;
20408       else
20409         type = current_class_type;
20410       /* If that's not a class type, there is no destructor.  */
20411       if (!type || !CLASS_TYPE_P (type))
20412         return error_mark_node;
20413       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20414         lazily_declare_fn (sfk_destructor, type);
20415       if (!CLASSTYPE_DESTRUCTORS (type))
20416           return error_mark_node;
20417       /* If it was a class type, return the destructor.  */
20418       return CLASSTYPE_DESTRUCTORS (type);
20419     }
20420
20421   /* By this point, the NAME should be an ordinary identifier.  If
20422      the id-expression was a qualified name, the qualifying scope is
20423      stored in PARSER->SCOPE at this point.  */
20424   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20425
20426   /* Perform the lookup.  */
20427   if (parser->scope)
20428     {
20429       bool dependent_p;
20430
20431       if (parser->scope == error_mark_node)
20432         return error_mark_node;
20433
20434       /* If the SCOPE is dependent, the lookup must be deferred until
20435          the template is instantiated -- unless we are explicitly
20436          looking up names in uninstantiated templates.  Even then, we
20437          cannot look up the name if the scope is not a class type; it
20438          might, for example, be a template type parameter.  */
20439       dependent_p = (TYPE_P (parser->scope)
20440                      && dependent_scope_p (parser->scope));
20441       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20442           && dependent_p)
20443         /* Defer lookup.  */
20444         decl = error_mark_node;
20445       else
20446         {
20447           tree pushed_scope = NULL_TREE;
20448
20449           /* If PARSER->SCOPE is a dependent type, then it must be a
20450              class type, and we must not be checking dependencies;
20451              otherwise, we would have processed this lookup above.  So
20452              that PARSER->SCOPE is not considered a dependent base by
20453              lookup_member, we must enter the scope here.  */
20454           if (dependent_p)
20455             pushed_scope = push_scope (parser->scope);
20456
20457           /* If the PARSER->SCOPE is a template specialization, it
20458              may be instantiated during name lookup.  In that case,
20459              errors may be issued.  Even if we rollback the current
20460              tentative parse, those errors are valid.  */
20461           decl = lookup_qualified_name (parser->scope, name,
20462                                         tag_type != none_type,
20463                                         /*complain=*/true);
20464
20465           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20466              lookup result and the nested-name-specifier nominates a class C:
20467                * if the name specified after the nested-name-specifier, when
20468                looked up in C, is the injected-class-name of C (Clause 9), or
20469                * if the name specified after the nested-name-specifier is the
20470                same as the identifier or the simple-template-id's template-
20471                name in the last component of the nested-name-specifier,
20472              the name is instead considered to name the constructor of
20473              class C. [ Note: for example, the constructor is not an
20474              acceptable lookup result in an elaborated-type-specifier so
20475              the constructor would not be used in place of the
20476              injected-class-name. --end note ] Such a constructor name
20477              shall be used only in the declarator-id of a declaration that
20478              names a constructor or in a using-declaration.  */
20479           if (tag_type == none_type
20480               && DECL_SELF_REFERENCE_P (decl)
20481               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20482             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20483                                           tag_type != none_type,
20484                                           /*complain=*/true);
20485
20486           /* If we have a single function from a using decl, pull it out.  */
20487           if (TREE_CODE (decl) == OVERLOAD
20488               && !really_overloaded_fn (decl))
20489             decl = OVL_FUNCTION (decl);
20490
20491           if (pushed_scope)
20492             pop_scope (pushed_scope);
20493         }
20494
20495       /* If the scope is a dependent type and either we deferred lookup or
20496          we did lookup but didn't find the name, rememeber the name.  */
20497       if (decl == error_mark_node && TYPE_P (parser->scope)
20498           && dependent_type_p (parser->scope))
20499         {
20500           if (tag_type)
20501             {
20502               tree type;
20503
20504               /* The resolution to Core Issue 180 says that `struct
20505                  A::B' should be considered a type-name, even if `A'
20506                  is dependent.  */
20507               type = make_typename_type (parser->scope, name, tag_type,
20508                                          /*complain=*/tf_error);
20509               decl = TYPE_NAME (type);
20510             }
20511           else if (is_template
20512                    && (cp_parser_next_token_ends_template_argument_p (parser)
20513                        || cp_lexer_next_token_is (parser->lexer,
20514                                                   CPP_CLOSE_PAREN)))
20515             decl = make_unbound_class_template (parser->scope,
20516                                                 name, NULL_TREE,
20517                                                 /*complain=*/tf_error);
20518           else
20519             decl = build_qualified_name (/*type=*/NULL_TREE,
20520                                          parser->scope, name,
20521                                          is_template);
20522         }
20523       parser->qualifying_scope = parser->scope;
20524       parser->object_scope = NULL_TREE;
20525     }
20526   else if (object_type)
20527     {
20528       tree object_decl = NULL_TREE;
20529       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20530          OBJECT_TYPE is not a class.  */
20531       if (CLASS_TYPE_P (object_type))
20532         /* If the OBJECT_TYPE is a template specialization, it may
20533            be instantiated during name lookup.  In that case, errors
20534            may be issued.  Even if we rollback the current tentative
20535            parse, those errors are valid.  */
20536         object_decl = lookup_member (object_type,
20537                                      name,
20538                                      /*protect=*/0,
20539                                      tag_type != none_type,
20540                                      tf_warning_or_error);
20541       /* Look it up in the enclosing context, too.  */
20542       decl = lookup_name_real (name, tag_type != none_type,
20543                                /*nonclass=*/0,
20544                                /*block_p=*/true, is_namespace, 0);
20545       parser->object_scope = object_type;
20546       parser->qualifying_scope = NULL_TREE;
20547       if (object_decl)
20548         decl = object_decl;
20549     }
20550   else
20551     {
20552       decl = lookup_name_real (name, tag_type != none_type,
20553                                /*nonclass=*/0,
20554                                /*block_p=*/true, is_namespace, 0);
20555       parser->qualifying_scope = NULL_TREE;
20556       parser->object_scope = NULL_TREE;
20557     }
20558
20559   /* If the lookup failed, let our caller know.  */
20560   if (!decl || decl == error_mark_node)
20561     return error_mark_node;
20562
20563   /* Pull out the template from an injected-class-name (or multiple).  */
20564   if (is_template)
20565     decl = maybe_get_template_decl_from_type_decl (decl);
20566
20567   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20568   if (TREE_CODE (decl) == TREE_LIST)
20569     {
20570       if (ambiguous_decls)
20571         *ambiguous_decls = decl;
20572       /* The error message we have to print is too complicated for
20573          cp_parser_error, so we incorporate its actions directly.  */
20574       if (!cp_parser_simulate_error (parser))
20575         {
20576           error_at (name_location, "reference to %qD is ambiguous",
20577                     name);
20578           print_candidates (decl);
20579         }
20580       return error_mark_node;
20581     }
20582
20583   gcc_assert (DECL_P (decl)
20584               || TREE_CODE (decl) == OVERLOAD
20585               || TREE_CODE (decl) == SCOPE_REF
20586               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20587               || BASELINK_P (decl));
20588
20589   /* If we have resolved the name of a member declaration, check to
20590      see if the declaration is accessible.  When the name resolves to
20591      set of overloaded functions, accessibility is checked when
20592      overload resolution is done.
20593
20594      During an explicit instantiation, access is not checked at all,
20595      as per [temp.explicit].  */
20596   if (DECL_P (decl))
20597     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20598
20599   maybe_record_typedef_use (decl);
20600
20601   return decl;
20602 }
20603
20604 /* Like cp_parser_lookup_name, but for use in the typical case where
20605    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20606    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20607
20608 static tree
20609 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20610 {
20611   return cp_parser_lookup_name (parser, name,
20612                                 none_type,
20613                                 /*is_template=*/false,
20614                                 /*is_namespace=*/false,
20615                                 /*check_dependency=*/true,
20616                                 /*ambiguous_decls=*/NULL,
20617                                 location);
20618 }
20619
20620 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20621    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20622    true, the DECL indicates the class being defined in a class-head,
20623    or declared in an elaborated-type-specifier.
20624
20625    Otherwise, return DECL.  */
20626
20627 static tree
20628 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20629 {
20630   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20631      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20632
20633        struct A {
20634          template <typename T> struct B;
20635        };
20636
20637        template <typename T> struct A::B {};
20638
20639      Similarly, in an elaborated-type-specifier:
20640
20641        namespace N { struct X{}; }
20642
20643        struct A {
20644          template <typename T> friend struct N::X;
20645        };
20646
20647      However, if the DECL refers to a class type, and we are in
20648      the scope of the class, then the name lookup automatically
20649      finds the TYPE_DECL created by build_self_reference rather
20650      than a TEMPLATE_DECL.  For example, in:
20651
20652        template <class T> struct S {
20653          S s;
20654        };
20655
20656      there is no need to handle such case.  */
20657
20658   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20659     return DECL_TEMPLATE_RESULT (decl);
20660
20661   return decl;
20662 }
20663
20664 /* If too many, or too few, template-parameter lists apply to the
20665    declarator, issue an error message.  Returns TRUE if all went well,
20666    and FALSE otherwise.  */
20667
20668 static bool
20669 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20670                                                 cp_declarator *declarator,
20671                                                 location_t declarator_location)
20672 {
20673   unsigned num_templates;
20674
20675   /* We haven't seen any classes that involve template parameters yet.  */
20676   num_templates = 0;
20677
20678   switch (declarator->kind)
20679     {
20680     case cdk_id:
20681       if (declarator->u.id.qualifying_scope)
20682         {
20683           tree scope;
20684
20685           scope = declarator->u.id.qualifying_scope;
20686
20687           while (scope && CLASS_TYPE_P (scope))
20688             {
20689               /* You're supposed to have one `template <...>'
20690                  for every template class, but you don't need one
20691                  for a full specialization.  For example:
20692
20693                  template <class T> struct S{};
20694                  template <> struct S<int> { void f(); };
20695                  void S<int>::f () {}
20696
20697                  is correct; there shouldn't be a `template <>' for
20698                  the definition of `S<int>::f'.  */
20699               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20700                 /* If SCOPE does not have template information of any
20701                    kind, then it is not a template, nor is it nested
20702                    within a template.  */
20703                 break;
20704               if (explicit_class_specialization_p (scope))
20705                 break;
20706               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20707                 ++num_templates;
20708
20709               scope = TYPE_CONTEXT (scope);
20710             }
20711         }
20712       else if (TREE_CODE (declarator->u.id.unqualified_name)
20713                == TEMPLATE_ID_EXPR)
20714         /* If the DECLARATOR has the form `X<y>' then it uses one
20715            additional level of template parameters.  */
20716         ++num_templates;
20717
20718       return cp_parser_check_template_parameters 
20719         (parser, num_templates, declarator_location, declarator);
20720
20721
20722     case cdk_function:
20723     case cdk_array:
20724     case cdk_pointer:
20725     case cdk_reference:
20726     case cdk_ptrmem:
20727       return (cp_parser_check_declarator_template_parameters
20728               (parser, declarator->declarator, declarator_location));
20729
20730     case cdk_error:
20731       return true;
20732
20733     default:
20734       gcc_unreachable ();
20735     }
20736   return false;
20737 }
20738
20739 /* NUM_TEMPLATES were used in the current declaration.  If that is
20740    invalid, return FALSE and issue an error messages.  Otherwise,
20741    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20742    declarator and we can print more accurate diagnostics.  */
20743
20744 static bool
20745 cp_parser_check_template_parameters (cp_parser* parser,
20746                                      unsigned num_templates,
20747                                      location_t location,
20748                                      cp_declarator *declarator)
20749 {
20750   /* If there are the same number of template classes and parameter
20751      lists, that's OK.  */
20752   if (parser->num_template_parameter_lists == num_templates)
20753     return true;
20754   /* If there are more, but only one more, then we are referring to a
20755      member template.  That's OK too.  */
20756   if (parser->num_template_parameter_lists == num_templates + 1)
20757     return true;
20758   /* If there are more template classes than parameter lists, we have
20759      something like:
20760
20761        template <class T> void S<T>::R<T>::f ();  */
20762   if (parser->num_template_parameter_lists < num_templates)
20763     {
20764       if (declarator && !current_function_decl)
20765         error_at (location, "specializing member %<%T::%E%> "
20766                   "requires %<template<>%> syntax", 
20767                   declarator->u.id.qualifying_scope,
20768                   declarator->u.id.unqualified_name);
20769       else if (declarator)
20770         error_at (location, "invalid declaration of %<%T::%E%>",
20771                   declarator->u.id.qualifying_scope,
20772                   declarator->u.id.unqualified_name);
20773       else 
20774         error_at (location, "too few template-parameter-lists");
20775       return false;
20776     }
20777   /* Otherwise, there are too many template parameter lists.  We have
20778      something like:
20779
20780      template <class T> template <class U> void S::f();  */
20781   error_at (location, "too many template-parameter-lists");
20782   return false;
20783 }
20784
20785 /* Parse an optional `::' token indicating that the following name is
20786    from the global namespace.  If so, PARSER->SCOPE is set to the
20787    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20788    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20789    Returns the new value of PARSER->SCOPE, if the `::' token is
20790    present, and NULL_TREE otherwise.  */
20791
20792 static tree
20793 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20794 {
20795   cp_token *token;
20796
20797   /* Peek at the next token.  */
20798   token = cp_lexer_peek_token (parser->lexer);
20799   /* If we're looking at a `::' token then we're starting from the
20800      global namespace, not our current location.  */
20801   if (token->type == CPP_SCOPE)
20802     {
20803       /* Consume the `::' token.  */
20804       cp_lexer_consume_token (parser->lexer);
20805       /* Set the SCOPE so that we know where to start the lookup.  */
20806       parser->scope = global_namespace;
20807       parser->qualifying_scope = global_namespace;
20808       parser->object_scope = NULL_TREE;
20809
20810       return parser->scope;
20811     }
20812   else if (!current_scope_valid_p)
20813     {
20814       parser->scope = NULL_TREE;
20815       parser->qualifying_scope = NULL_TREE;
20816       parser->object_scope = NULL_TREE;
20817     }
20818
20819   return NULL_TREE;
20820 }
20821
20822 /* Returns TRUE if the upcoming token sequence is the start of a
20823    constructor declarator.  If FRIEND_P is true, the declarator is
20824    preceded by the `friend' specifier.  */
20825
20826 static bool
20827 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20828 {
20829   bool constructor_p;
20830   tree nested_name_specifier;
20831   cp_token *next_token;
20832
20833   /* The common case is that this is not a constructor declarator, so
20834      try to avoid doing lots of work if at all possible.  It's not
20835      valid declare a constructor at function scope.  */
20836   if (parser->in_function_body)
20837     return false;
20838   /* And only certain tokens can begin a constructor declarator.  */
20839   next_token = cp_lexer_peek_token (parser->lexer);
20840   if (next_token->type != CPP_NAME
20841       && next_token->type != CPP_SCOPE
20842       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20843       && next_token->type != CPP_TEMPLATE_ID)
20844     return false;
20845
20846   /* Parse tentatively; we are going to roll back all of the tokens
20847      consumed here.  */
20848   cp_parser_parse_tentatively (parser);
20849   /* Assume that we are looking at a constructor declarator.  */
20850   constructor_p = true;
20851
20852   /* Look for the optional `::' operator.  */
20853   cp_parser_global_scope_opt (parser,
20854                               /*current_scope_valid_p=*/false);
20855   /* Look for the nested-name-specifier.  */
20856   nested_name_specifier
20857     = (cp_parser_nested_name_specifier_opt (parser,
20858                                             /*typename_keyword_p=*/false,
20859                                             /*check_dependency_p=*/false,
20860                                             /*type_p=*/false,
20861                                             /*is_declaration=*/false));
20862   /* Outside of a class-specifier, there must be a
20863      nested-name-specifier.  */
20864   if (!nested_name_specifier &&
20865       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20866        || friend_p))
20867     constructor_p = false;
20868   else if (nested_name_specifier == error_mark_node)
20869     constructor_p = false;
20870
20871   /* If we have a class scope, this is easy; DR 147 says that S::S always
20872      names the constructor, and no other qualified name could.  */
20873   if (constructor_p && nested_name_specifier
20874       && CLASS_TYPE_P (nested_name_specifier))
20875     {
20876       tree id = cp_parser_unqualified_id (parser,
20877                                           /*template_keyword_p=*/false,
20878                                           /*check_dependency_p=*/false,
20879                                           /*declarator_p=*/true,
20880                                           /*optional_p=*/false);
20881       if (is_overloaded_fn (id))
20882         id = DECL_NAME (get_first_fn (id));
20883       if (!constructor_name_p (id, nested_name_specifier))
20884         constructor_p = false;
20885     }
20886   /* If we still think that this might be a constructor-declarator,
20887      look for a class-name.  */
20888   else if (constructor_p)
20889     {
20890       /* If we have:
20891
20892            template <typename T> struct S {
20893              S();
20894            };
20895
20896          we must recognize that the nested `S' names a class.  */
20897       tree type_decl;
20898       type_decl = cp_parser_class_name (parser,
20899                                         /*typename_keyword_p=*/false,
20900                                         /*template_keyword_p=*/false,
20901                                         none_type,
20902                                         /*check_dependency_p=*/false,
20903                                         /*class_head_p=*/false,
20904                                         /*is_declaration=*/false);
20905       /* If there was no class-name, then this is not a constructor.  */
20906       constructor_p = !cp_parser_error_occurred (parser);
20907
20908       /* If we're still considering a constructor, we have to see a `(',
20909          to begin the parameter-declaration-clause, followed by either a
20910          `)', an `...', or a decl-specifier.  We need to check for a
20911          type-specifier to avoid being fooled into thinking that:
20912
20913            S (f) (int);
20914
20915          is a constructor.  (It is actually a function named `f' that
20916          takes one parameter (of type `int') and returns a value of type
20917          `S'.  */
20918       if (constructor_p
20919           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20920         constructor_p = false;
20921
20922       if (constructor_p
20923           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20924           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20925           /* A parameter declaration begins with a decl-specifier,
20926              which is either the "attribute" keyword, a storage class
20927              specifier, or (usually) a type-specifier.  */
20928           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20929         {
20930           tree type;
20931           tree pushed_scope = NULL_TREE;
20932           unsigned saved_num_template_parameter_lists;
20933
20934           /* Names appearing in the type-specifier should be looked up
20935              in the scope of the class.  */
20936           if (current_class_type)
20937             type = NULL_TREE;
20938           else
20939             {
20940               type = TREE_TYPE (type_decl);
20941               if (TREE_CODE (type) == TYPENAME_TYPE)
20942                 {
20943                   type = resolve_typename_type (type,
20944                                                 /*only_current_p=*/false);
20945                   if (TREE_CODE (type) == TYPENAME_TYPE)
20946                     {
20947                       cp_parser_abort_tentative_parse (parser);
20948                       return false;
20949                     }
20950                 }
20951               pushed_scope = push_scope (type);
20952             }
20953
20954           /* Inside the constructor parameter list, surrounding
20955              template-parameter-lists do not apply.  */
20956           saved_num_template_parameter_lists
20957             = parser->num_template_parameter_lists;
20958           parser->num_template_parameter_lists = 0;
20959
20960           /* Look for the type-specifier.  */
20961           cp_parser_type_specifier (parser,
20962                                     CP_PARSER_FLAGS_NONE,
20963                                     /*decl_specs=*/NULL,
20964                                     /*is_declarator=*/true,
20965                                     /*declares_class_or_enum=*/NULL,
20966                                     /*is_cv_qualifier=*/NULL);
20967
20968           parser->num_template_parameter_lists
20969             = saved_num_template_parameter_lists;
20970
20971           /* Leave the scope of the class.  */
20972           if (pushed_scope)
20973             pop_scope (pushed_scope);
20974
20975           constructor_p = !cp_parser_error_occurred (parser);
20976         }
20977     }
20978
20979   /* We did not really want to consume any tokens.  */
20980   cp_parser_abort_tentative_parse (parser);
20981
20982   return constructor_p;
20983 }
20984
20985 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20986    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20987    they must be performed once we are in the scope of the function.
20988
20989    Returns the function defined.  */
20990
20991 static tree
20992 cp_parser_function_definition_from_specifiers_and_declarator
20993   (cp_parser* parser,
20994    cp_decl_specifier_seq *decl_specifiers,
20995    tree attributes,
20996    const cp_declarator *declarator)
20997 {
20998   tree fn;
20999   bool success_p;
21000
21001   /* Begin the function-definition.  */
21002   success_p = start_function (decl_specifiers, declarator, attributes);
21003
21004   /* The things we're about to see are not directly qualified by any
21005      template headers we've seen thus far.  */
21006   reset_specialization ();
21007
21008   /* If there were names looked up in the decl-specifier-seq that we
21009      did not check, check them now.  We must wait until we are in the
21010      scope of the function to perform the checks, since the function
21011      might be a friend.  */
21012   perform_deferred_access_checks ();
21013
21014   if (!success_p)
21015     {
21016       /* Skip the entire function.  */
21017       cp_parser_skip_to_end_of_block_or_statement (parser);
21018       fn = error_mark_node;
21019     }
21020   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21021     {
21022       /* Seen already, skip it.  An error message has already been output.  */
21023       cp_parser_skip_to_end_of_block_or_statement (parser);
21024       fn = current_function_decl;
21025       current_function_decl = NULL_TREE;
21026       /* If this is a function from a class, pop the nested class.  */
21027       if (current_class_name)
21028         pop_nested_class ();
21029     }
21030   else
21031     {
21032       timevar_id_t tv;
21033       if (DECL_DECLARED_INLINE_P (current_function_decl))
21034         tv = TV_PARSE_INLINE;
21035       else
21036         tv = TV_PARSE_FUNC;
21037       timevar_push (tv);
21038       fn = cp_parser_function_definition_after_declarator (parser,
21039                                                          /*inline_p=*/false);
21040       timevar_pop (tv);
21041     }
21042
21043   return fn;
21044 }
21045
21046 /* Parse the part of a function-definition that follows the
21047    declarator.  INLINE_P is TRUE iff this function is an inline
21048    function defined within a class-specifier.
21049
21050    Returns the function defined.  */
21051
21052 static tree
21053 cp_parser_function_definition_after_declarator (cp_parser* parser,
21054                                                 bool inline_p)
21055 {
21056   tree fn;
21057   bool ctor_initializer_p = false;
21058   bool saved_in_unbraced_linkage_specification_p;
21059   bool saved_in_function_body;
21060   unsigned saved_num_template_parameter_lists;
21061   cp_token *token;
21062
21063   saved_in_function_body = parser->in_function_body;
21064   parser->in_function_body = true;
21065   /* If the next token is `return', then the code may be trying to
21066      make use of the "named return value" extension that G++ used to
21067      support.  */
21068   token = cp_lexer_peek_token (parser->lexer);
21069   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21070     {
21071       /* Consume the `return' keyword.  */
21072       cp_lexer_consume_token (parser->lexer);
21073       /* Look for the identifier that indicates what value is to be
21074          returned.  */
21075       cp_parser_identifier (parser);
21076       /* Issue an error message.  */
21077       error_at (token->location,
21078                 "named return values are no longer supported");
21079       /* Skip tokens until we reach the start of the function body.  */
21080       while (true)
21081         {
21082           cp_token *token = cp_lexer_peek_token (parser->lexer);
21083           if (token->type == CPP_OPEN_BRACE
21084               || token->type == CPP_EOF
21085               || token->type == CPP_PRAGMA_EOL)
21086             break;
21087           cp_lexer_consume_token (parser->lexer);
21088         }
21089     }
21090   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21091      anything declared inside `f'.  */
21092   saved_in_unbraced_linkage_specification_p
21093     = parser->in_unbraced_linkage_specification_p;
21094   parser->in_unbraced_linkage_specification_p = false;
21095   /* Inside the function, surrounding template-parameter-lists do not
21096      apply.  */
21097   saved_num_template_parameter_lists
21098     = parser->num_template_parameter_lists;
21099   parser->num_template_parameter_lists = 0;
21100
21101   start_lambda_scope (current_function_decl);
21102
21103   /* If the next token is `try', `__transaction_atomic', or
21104      `__transaction_relaxed`, then we are looking at either function-try-block
21105      or function-transaction-block.  Note that all of these include the
21106      function-body.  */
21107   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21108     ctor_initializer_p = cp_parser_function_transaction (parser,
21109         RID_TRANSACTION_ATOMIC);
21110   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21111       RID_TRANSACTION_RELAXED))
21112     ctor_initializer_p = cp_parser_function_transaction (parser,
21113         RID_TRANSACTION_RELAXED);
21114   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21115     ctor_initializer_p = cp_parser_function_try_block (parser);
21116   else
21117     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21118       (parser, /*in_function_try_block=*/false);
21119
21120   finish_lambda_scope ();
21121
21122   /* Finish the function.  */
21123   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21124                         (inline_p ? 2 : 0));
21125   /* Generate code for it, if necessary.  */
21126   expand_or_defer_fn (fn);
21127   /* Restore the saved values.  */
21128   parser->in_unbraced_linkage_specification_p
21129     = saved_in_unbraced_linkage_specification_p;
21130   parser->num_template_parameter_lists
21131     = saved_num_template_parameter_lists;
21132   parser->in_function_body = saved_in_function_body;
21133
21134   return fn;
21135 }
21136
21137 /* Parse a template-declaration, assuming that the `export' (and
21138    `extern') keywords, if present, has already been scanned.  MEMBER_P
21139    is as for cp_parser_template_declaration.  */
21140
21141 static void
21142 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21143 {
21144   tree decl = NULL_TREE;
21145   VEC (deferred_access_check,gc) *checks;
21146   tree parameter_list;
21147   bool friend_p = false;
21148   bool need_lang_pop;
21149   cp_token *token;
21150
21151   /* Look for the `template' keyword.  */
21152   token = cp_lexer_peek_token (parser->lexer);
21153   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21154     return;
21155
21156   /* And the `<'.  */
21157   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21158     return;
21159   if (at_class_scope_p () && current_function_decl)
21160     {
21161       /* 14.5.2.2 [temp.mem]
21162
21163          A local class shall not have member templates.  */
21164       error_at (token->location,
21165                 "invalid declaration of member template in local class");
21166       cp_parser_skip_to_end_of_block_or_statement (parser);
21167       return;
21168     }
21169   /* [temp]
21170
21171      A template ... shall not have C linkage.  */
21172   if (current_lang_name == lang_name_c)
21173     {
21174       error_at (token->location, "template with C linkage");
21175       /* Give it C++ linkage to avoid confusing other parts of the
21176          front end.  */
21177       push_lang_context (lang_name_cplusplus);
21178       need_lang_pop = true;
21179     }
21180   else
21181     need_lang_pop = false;
21182
21183   /* We cannot perform access checks on the template parameter
21184      declarations until we know what is being declared, just as we
21185      cannot check the decl-specifier list.  */
21186   push_deferring_access_checks (dk_deferred);
21187
21188   /* If the next token is `>', then we have an invalid
21189      specialization.  Rather than complain about an invalid template
21190      parameter, issue an error message here.  */
21191   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21192     {
21193       cp_parser_error (parser, "invalid explicit specialization");
21194       begin_specialization ();
21195       parameter_list = NULL_TREE;
21196     }
21197   else
21198     {
21199       /* Parse the template parameters.  */
21200       parameter_list = cp_parser_template_parameter_list (parser);
21201     }
21202
21203   /* Get the deferred access checks from the parameter list.  These
21204      will be checked once we know what is being declared, as for a
21205      member template the checks must be performed in the scope of the
21206      class containing the member.  */
21207   checks = get_deferred_access_checks ();
21208
21209   /* Look for the `>'.  */
21210   cp_parser_skip_to_end_of_template_parameter_list (parser);
21211   /* We just processed one more parameter list.  */
21212   ++parser->num_template_parameter_lists;
21213   /* If the next token is `template', there are more template
21214      parameters.  */
21215   if (cp_lexer_next_token_is_keyword (parser->lexer,
21216                                       RID_TEMPLATE))
21217     cp_parser_template_declaration_after_export (parser, member_p);
21218   else if (cxx_dialect >= cxx0x
21219            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21220     decl = cp_parser_alias_declaration (parser);
21221   else
21222     {
21223       /* There are no access checks when parsing a template, as we do not
21224          know if a specialization will be a friend.  */
21225       push_deferring_access_checks (dk_no_check);
21226       token = cp_lexer_peek_token (parser->lexer);
21227       decl = cp_parser_single_declaration (parser,
21228                                            checks,
21229                                            member_p,
21230                                            /*explicit_specialization_p=*/false,
21231                                            &friend_p);
21232       pop_deferring_access_checks ();
21233
21234       /* If this is a member template declaration, let the front
21235          end know.  */
21236       if (member_p && !friend_p && decl)
21237         {
21238           if (TREE_CODE (decl) == TYPE_DECL)
21239             cp_parser_check_access_in_redeclaration (decl, token->location);
21240
21241           decl = finish_member_template_decl (decl);
21242         }
21243       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21244         make_friend_class (current_class_type, TREE_TYPE (decl),
21245                            /*complain=*/true);
21246     }
21247   /* We are done with the current parameter list.  */
21248   --parser->num_template_parameter_lists;
21249
21250   pop_deferring_access_checks ();
21251
21252   /* Finish up.  */
21253   finish_template_decl (parameter_list);
21254
21255   /* Check the template arguments for a literal operator template.  */
21256   if (decl
21257       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21258       && UDLIT_OPER_P (DECL_NAME (decl)))
21259     {
21260       bool ok = true;
21261       if (parameter_list == NULL_TREE)
21262         ok = false;
21263       else
21264         {
21265           int num_parms = TREE_VEC_LENGTH (parameter_list);
21266           if (num_parms != 1)
21267             ok = false;
21268           else
21269             {
21270               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21271               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21272               if (TREE_TYPE (parm) != char_type_node
21273                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21274                 ok = false;
21275             }
21276         }
21277       if (!ok)
21278         error ("literal operator template %qD has invalid parameter list."
21279                "  Expected non-type template argument pack <char...>",
21280                decl);
21281     }
21282   /* Register member declarations.  */
21283   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21284     finish_member_declaration (decl);
21285   /* For the erroneous case of a template with C linkage, we pushed an
21286      implicit C++ linkage scope; exit that scope now.  */
21287   if (need_lang_pop)
21288     pop_lang_context ();
21289   /* If DECL is a function template, we must return to parse it later.
21290      (Even though there is no definition, there might be default
21291      arguments that need handling.)  */
21292   if (member_p && decl
21293       && (TREE_CODE (decl) == FUNCTION_DECL
21294           || DECL_FUNCTION_TEMPLATE_P (decl)))
21295     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21296 }
21297
21298 /* Perform the deferred access checks from a template-parameter-list.
21299    CHECKS is a TREE_LIST of access checks, as returned by
21300    get_deferred_access_checks.  */
21301
21302 static void
21303 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21304 {
21305   ++processing_template_parmlist;
21306   perform_access_checks (checks);
21307   --processing_template_parmlist;
21308 }
21309
21310 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21311    `function-definition' sequence.  MEMBER_P is true, this declaration
21312    appears in a class scope.
21313
21314    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21315    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21316
21317 static tree
21318 cp_parser_single_declaration (cp_parser* parser,
21319                               VEC (deferred_access_check,gc)* checks,
21320                               bool member_p,
21321                               bool explicit_specialization_p,
21322                               bool* friend_p)
21323 {
21324   int declares_class_or_enum;
21325   tree decl = NULL_TREE;
21326   cp_decl_specifier_seq decl_specifiers;
21327   bool function_definition_p = false;
21328   cp_token *decl_spec_token_start;
21329
21330   /* This function is only used when processing a template
21331      declaration.  */
21332   gcc_assert (innermost_scope_kind () == sk_template_parms
21333               || innermost_scope_kind () == sk_template_spec);
21334
21335   /* Defer access checks until we know what is being declared.  */
21336   push_deferring_access_checks (dk_deferred);
21337
21338   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21339      alternative.  */
21340   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21341   cp_parser_decl_specifier_seq (parser,
21342                                 CP_PARSER_FLAGS_OPTIONAL,
21343                                 &decl_specifiers,
21344                                 &declares_class_or_enum);
21345   if (friend_p)
21346     *friend_p = cp_parser_friend_p (&decl_specifiers);
21347
21348   /* There are no template typedefs.  */
21349   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21350     {
21351       error_at (decl_spec_token_start->location,
21352                 "template declaration of %<typedef%>");
21353       decl = error_mark_node;
21354     }
21355
21356   /* Gather up the access checks that occurred the
21357      decl-specifier-seq.  */
21358   stop_deferring_access_checks ();
21359
21360   /* Check for the declaration of a template class.  */
21361   if (declares_class_or_enum)
21362     {
21363       if (cp_parser_declares_only_class_p (parser))
21364         {
21365           decl = shadow_tag (&decl_specifiers);
21366
21367           /* In this case:
21368
21369                struct C {
21370                  friend template <typename T> struct A<T>::B;
21371                };
21372
21373              A<T>::B will be represented by a TYPENAME_TYPE, and
21374              therefore not recognized by shadow_tag.  */
21375           if (friend_p && *friend_p
21376               && !decl
21377               && decl_specifiers.type
21378               && TYPE_P (decl_specifiers.type))
21379             decl = decl_specifiers.type;
21380
21381           if (decl && decl != error_mark_node)
21382             decl = TYPE_NAME (decl);
21383           else
21384             decl = error_mark_node;
21385
21386           /* Perform access checks for template parameters.  */
21387           cp_parser_perform_template_parameter_access_checks (checks);
21388         }
21389     }
21390
21391   /* Complain about missing 'typename' or other invalid type names.  */
21392   if (!decl_specifiers.any_type_specifiers_p
21393       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21394     {
21395       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21396          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21397          the rest of this declaration.  */
21398       decl = error_mark_node;
21399       goto out;
21400     }
21401
21402   /* If it's not a template class, try for a template function.  If
21403      the next token is a `;', then this declaration does not declare
21404      anything.  But, if there were errors in the decl-specifiers, then
21405      the error might well have come from an attempted class-specifier.
21406      In that case, there's no need to warn about a missing declarator.  */
21407   if (!decl
21408       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21409           || decl_specifiers.type != error_mark_node))
21410     {
21411       decl = cp_parser_init_declarator (parser,
21412                                         &decl_specifiers,
21413                                         checks,
21414                                         /*function_definition_allowed_p=*/true,
21415                                         member_p,
21416                                         declares_class_or_enum,
21417                                         &function_definition_p,
21418                                         NULL);
21419
21420     /* 7.1.1-1 [dcl.stc]
21421
21422        A storage-class-specifier shall not be specified in an explicit
21423        specialization...  */
21424     if (decl
21425         && explicit_specialization_p
21426         && decl_specifiers.storage_class != sc_none)
21427       {
21428         error_at (decl_spec_token_start->location,
21429                   "explicit template specialization cannot have a storage class");
21430         decl = error_mark_node;
21431       }
21432     }
21433
21434   /* Look for a trailing `;' after the declaration.  */
21435   if (!function_definition_p
21436       && (decl == error_mark_node
21437           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21438     cp_parser_skip_to_end_of_block_or_statement (parser);
21439
21440  out:
21441   pop_deferring_access_checks ();
21442
21443   /* Clear any current qualification; whatever comes next is the start
21444      of something new.  */
21445   parser->scope = NULL_TREE;
21446   parser->qualifying_scope = NULL_TREE;
21447   parser->object_scope = NULL_TREE;
21448
21449   return decl;
21450 }
21451
21452 /* Parse a cast-expression that is not the operand of a unary "&".  */
21453
21454 static tree
21455 cp_parser_simple_cast_expression (cp_parser *parser)
21456 {
21457   return cp_parser_cast_expression (parser, /*address_p=*/false,
21458                                     /*cast_p=*/false, NULL);
21459 }
21460
21461 /* Parse a functional cast to TYPE.  Returns an expression
21462    representing the cast.  */
21463
21464 static tree
21465 cp_parser_functional_cast (cp_parser* parser, tree type)
21466 {
21467   VEC(tree,gc) *vec;
21468   tree expression_list;
21469   tree cast;
21470   bool nonconst_p;
21471
21472   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21473     {
21474       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21475       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21476       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21477       if (TREE_CODE (type) == TYPE_DECL)
21478         type = TREE_TYPE (type);
21479       return finish_compound_literal (type, expression_list,
21480                                       tf_warning_or_error);
21481     }
21482
21483
21484   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21485                                                  /*cast_p=*/true,
21486                                                  /*allow_expansion_p=*/true,
21487                                                  /*non_constant_p=*/NULL);
21488   if (vec == NULL)
21489     expression_list = error_mark_node;
21490   else
21491     {
21492       expression_list = build_tree_list_vec (vec);
21493       release_tree_vector (vec);
21494     }
21495
21496   cast = build_functional_cast (type, expression_list,
21497                                 tf_warning_or_error);
21498   /* [expr.const]/1: In an integral constant expression "only type
21499      conversions to integral or enumeration type can be used".  */
21500   if (TREE_CODE (type) == TYPE_DECL)
21501     type = TREE_TYPE (type);
21502   if (cast != error_mark_node
21503       && !cast_valid_in_integral_constant_expression_p (type)
21504       && cp_parser_non_integral_constant_expression (parser,
21505                                                      NIC_CONSTRUCTOR))
21506     return error_mark_node;
21507   return cast;
21508 }
21509
21510 /* Save the tokens that make up the body of a member function defined
21511    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21512    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21513    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21514    for the member function.  */
21515
21516 static tree
21517 cp_parser_save_member_function_body (cp_parser* parser,
21518                                      cp_decl_specifier_seq *decl_specifiers,
21519                                      cp_declarator *declarator,
21520                                      tree attributes)
21521 {
21522   cp_token *first;
21523   cp_token *last;
21524   tree fn;
21525
21526   /* Create the FUNCTION_DECL.  */
21527   fn = grokmethod (decl_specifiers, declarator, attributes);
21528   /* If something went badly wrong, bail out now.  */
21529   if (fn == error_mark_node)
21530     {
21531       /* If there's a function-body, skip it.  */
21532       if (cp_parser_token_starts_function_definition_p
21533           (cp_lexer_peek_token (parser->lexer)))
21534         cp_parser_skip_to_end_of_block_or_statement (parser);
21535       return error_mark_node;
21536     }
21537
21538   /* Remember it, if there default args to post process.  */
21539   cp_parser_save_default_args (parser, fn);
21540
21541   /* Save away the tokens that make up the body of the
21542      function.  */
21543   first = parser->lexer->next_token;
21544   /* We can have braced-init-list mem-initializers before the fn body.  */
21545   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21546     {
21547       cp_lexer_consume_token (parser->lexer);
21548       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21549              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21550         {
21551           /* cache_group will stop after an un-nested { } pair, too.  */
21552           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21553             break;
21554
21555           /* variadic mem-inits have ... after the ')'.  */
21556           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21557             cp_lexer_consume_token (parser->lexer);
21558         }
21559     }
21560   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21561   /* Handle function try blocks.  */
21562   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21563     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21564   last = parser->lexer->next_token;
21565
21566   /* Save away the inline definition; we will process it when the
21567      class is complete.  */
21568   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21569   DECL_PENDING_INLINE_P (fn) = 1;
21570
21571   /* We need to know that this was defined in the class, so that
21572      friend templates are handled correctly.  */
21573   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21574
21575   /* Add FN to the queue of functions to be parsed later.  */
21576   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21577
21578   return fn;
21579 }
21580
21581 /* Save the tokens that make up the in-class initializer for a non-static
21582    data member.  Returns a DEFAULT_ARG.  */
21583
21584 static tree
21585 cp_parser_save_nsdmi (cp_parser* parser)
21586 {
21587   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21588 }
21589
21590 /* Parse a template-argument-list, as well as the trailing ">" (but
21591    not the opening "<").  See cp_parser_template_argument_list for the
21592    return value.  */
21593
21594 static tree
21595 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21596 {
21597   tree arguments;
21598   tree saved_scope;
21599   tree saved_qualifying_scope;
21600   tree saved_object_scope;
21601   bool saved_greater_than_is_operator_p;
21602   int saved_unevaluated_operand;
21603   int saved_inhibit_evaluation_warnings;
21604
21605   /* [temp.names]
21606
21607      When parsing a template-id, the first non-nested `>' is taken as
21608      the end of the template-argument-list rather than a greater-than
21609      operator.  */
21610   saved_greater_than_is_operator_p
21611     = parser->greater_than_is_operator_p;
21612   parser->greater_than_is_operator_p = false;
21613   /* Parsing the argument list may modify SCOPE, so we save it
21614      here.  */
21615   saved_scope = parser->scope;
21616   saved_qualifying_scope = parser->qualifying_scope;
21617   saved_object_scope = parser->object_scope;
21618   /* We need to evaluate the template arguments, even though this
21619      template-id may be nested within a "sizeof".  */
21620   saved_unevaluated_operand = cp_unevaluated_operand;
21621   cp_unevaluated_operand = 0;
21622   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21623   c_inhibit_evaluation_warnings = 0;
21624   /* Parse the template-argument-list itself.  */
21625   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21626       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21627     arguments = NULL_TREE;
21628   else
21629     arguments = cp_parser_template_argument_list (parser);
21630   /* Look for the `>' that ends the template-argument-list. If we find
21631      a '>>' instead, it's probably just a typo.  */
21632   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21633     {
21634       if (cxx_dialect != cxx98)
21635         {
21636           /* In C++0x, a `>>' in a template argument list or cast
21637              expression is considered to be two separate `>'
21638              tokens. So, change the current token to a `>', but don't
21639              consume it: it will be consumed later when the outer
21640              template argument list (or cast expression) is parsed.
21641              Note that this replacement of `>' for `>>' is necessary
21642              even if we are parsing tentatively: in the tentative
21643              case, after calling
21644              cp_parser_enclosed_template_argument_list we will always
21645              throw away all of the template arguments and the first
21646              closing `>', either because the template argument list
21647              was erroneous or because we are replacing those tokens
21648              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21649              not have been thrown away) is needed either to close an
21650              outer template argument list or to complete a new-style
21651              cast.  */
21652           cp_token *token = cp_lexer_peek_token (parser->lexer);
21653           token->type = CPP_GREATER;
21654         }
21655       else if (!saved_greater_than_is_operator_p)
21656         {
21657           /* If we're in a nested template argument list, the '>>' has
21658             to be a typo for '> >'. We emit the error message, but we
21659             continue parsing and we push a '>' as next token, so that
21660             the argument list will be parsed correctly.  Note that the
21661             global source location is still on the token before the
21662             '>>', so we need to say explicitly where we want it.  */
21663           cp_token *token = cp_lexer_peek_token (parser->lexer);
21664           error_at (token->location, "%<>>%> should be %<> >%> "
21665                     "within a nested template argument list");
21666
21667           token->type = CPP_GREATER;
21668         }
21669       else
21670         {
21671           /* If this is not a nested template argument list, the '>>'
21672             is a typo for '>'. Emit an error message and continue.
21673             Same deal about the token location, but here we can get it
21674             right by consuming the '>>' before issuing the diagnostic.  */
21675           cp_token *token = cp_lexer_consume_token (parser->lexer);
21676           error_at (token->location,
21677                     "spurious %<>>%>, use %<>%> to terminate "
21678                     "a template argument list");
21679         }
21680     }
21681   else
21682     cp_parser_skip_to_end_of_template_parameter_list (parser);
21683   /* The `>' token might be a greater-than operator again now.  */
21684   parser->greater_than_is_operator_p
21685     = saved_greater_than_is_operator_p;
21686   /* Restore the SAVED_SCOPE.  */
21687   parser->scope = saved_scope;
21688   parser->qualifying_scope = saved_qualifying_scope;
21689   parser->object_scope = saved_object_scope;
21690   cp_unevaluated_operand = saved_unevaluated_operand;
21691   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21692
21693   return arguments;
21694 }
21695
21696 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21697    arguments, or the body of the function have not yet been parsed,
21698    parse them now.  */
21699
21700 static void
21701 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21702 {
21703   timevar_push (TV_PARSE_INMETH);
21704   /* If this member is a template, get the underlying
21705      FUNCTION_DECL.  */
21706   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21707     member_function = DECL_TEMPLATE_RESULT (member_function);
21708
21709   /* There should not be any class definitions in progress at this
21710      point; the bodies of members are only parsed outside of all class
21711      definitions.  */
21712   gcc_assert (parser->num_classes_being_defined == 0);
21713   /* While we're parsing the member functions we might encounter more
21714      classes.  We want to handle them right away, but we don't want
21715      them getting mixed up with functions that are currently in the
21716      queue.  */
21717   push_unparsed_function_queues (parser);
21718
21719   /* Make sure that any template parameters are in scope.  */
21720   maybe_begin_member_template_processing (member_function);
21721
21722   /* If the body of the function has not yet been parsed, parse it
21723      now.  */
21724   if (DECL_PENDING_INLINE_P (member_function))
21725     {
21726       tree function_scope;
21727       cp_token_cache *tokens;
21728
21729       /* The function is no longer pending; we are processing it.  */
21730       tokens = DECL_PENDING_INLINE_INFO (member_function);
21731       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21732       DECL_PENDING_INLINE_P (member_function) = 0;
21733
21734       /* If this is a local class, enter the scope of the containing
21735          function.  */
21736       function_scope = current_function_decl;
21737       if (function_scope)
21738         push_function_context ();
21739
21740       /* Push the body of the function onto the lexer stack.  */
21741       cp_parser_push_lexer_for_tokens (parser, tokens);
21742
21743       /* Let the front end know that we going to be defining this
21744          function.  */
21745       start_preparsed_function (member_function, NULL_TREE,
21746                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21747
21748       /* Don't do access checking if it is a templated function.  */
21749       if (processing_template_decl)
21750         push_deferring_access_checks (dk_no_check);
21751
21752       /* Now, parse the body of the function.  */
21753       cp_parser_function_definition_after_declarator (parser,
21754                                                       /*inline_p=*/true);
21755
21756       if (processing_template_decl)
21757         pop_deferring_access_checks ();
21758
21759       /* Leave the scope of the containing function.  */
21760       if (function_scope)
21761         pop_function_context ();
21762       cp_parser_pop_lexer (parser);
21763     }
21764
21765   /* Remove any template parameters from the symbol table.  */
21766   maybe_end_member_template_processing ();
21767
21768   /* Restore the queue.  */
21769   pop_unparsed_function_queues (parser);
21770   timevar_pop (TV_PARSE_INMETH);
21771 }
21772
21773 /* If DECL contains any default args, remember it on the unparsed
21774    functions queue.  */
21775
21776 static void
21777 cp_parser_save_default_args (cp_parser* parser, tree decl)
21778 {
21779   tree probe;
21780
21781   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21782        probe;
21783        probe = TREE_CHAIN (probe))
21784     if (TREE_PURPOSE (probe))
21785       {
21786         cp_default_arg_entry *entry
21787           = VEC_safe_push (cp_default_arg_entry, gc,
21788                            unparsed_funs_with_default_args, NULL);
21789         entry->class_type = current_class_type;
21790         entry->decl = decl;
21791         break;
21792       }
21793 }
21794
21795 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21796    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21797    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21798    from the parameter-type-list.  */
21799
21800 static tree
21801 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21802                                       tree default_arg, tree parmtype)
21803 {
21804   cp_token_cache *tokens;
21805   tree parsed_arg;
21806   bool dummy;
21807
21808   if (default_arg == error_mark_node)
21809     return error_mark_node;
21810
21811   /* Push the saved tokens for the default argument onto the parser's
21812      lexer stack.  */
21813   tokens = DEFARG_TOKENS (default_arg);
21814   cp_parser_push_lexer_for_tokens (parser, tokens);
21815
21816   start_lambda_scope (decl);
21817
21818   /* Parse the default argument.  */
21819   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21820   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21821     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21822
21823   finish_lambda_scope ();
21824
21825   if (parsed_arg == error_mark_node)
21826     cp_parser_skip_to_end_of_statement (parser);
21827
21828   if (!processing_template_decl)
21829     {
21830       /* In a non-template class, check conversions now.  In a template,
21831          we'll wait and instantiate these as needed.  */
21832       if (TREE_CODE (decl) == PARM_DECL)
21833         parsed_arg = check_default_argument (parmtype, parsed_arg);
21834       else
21835         {
21836           int flags = LOOKUP_IMPLICIT;
21837           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21838               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21839             flags = LOOKUP_NORMAL;
21840           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21841         }
21842     }
21843
21844   /* If the token stream has not been completely used up, then
21845      there was extra junk after the end of the default
21846      argument.  */
21847   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21848     {
21849       if (TREE_CODE (decl) == PARM_DECL)
21850         cp_parser_error (parser, "expected %<,%>");
21851       else
21852         cp_parser_error (parser, "expected %<;%>");
21853     }
21854
21855   /* Revert to the main lexer.  */
21856   cp_parser_pop_lexer (parser);
21857
21858   return parsed_arg;
21859 }
21860
21861 /* FIELD is a non-static data member with an initializer which we saved for
21862    later; parse it now.  */
21863
21864 static void
21865 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21866 {
21867   tree def;
21868
21869   push_unparsed_function_queues (parser);
21870   def = cp_parser_late_parse_one_default_arg (parser, field,
21871                                               DECL_INITIAL (field),
21872                                               NULL_TREE);
21873   pop_unparsed_function_queues (parser);
21874
21875   DECL_INITIAL (field) = def;
21876 }
21877
21878 /* FN is a FUNCTION_DECL which may contains a parameter with an
21879    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21880    assumes that the current scope is the scope in which the default
21881    argument should be processed.  */
21882
21883 static void
21884 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21885 {
21886   bool saved_local_variables_forbidden_p;
21887   tree parm, parmdecl;
21888
21889   /* While we're parsing the default args, we might (due to the
21890      statement expression extension) encounter more classes.  We want
21891      to handle them right away, but we don't want them getting mixed
21892      up with default args that are currently in the queue.  */
21893   push_unparsed_function_queues (parser);
21894
21895   /* Local variable names (and the `this' keyword) may not appear
21896      in a default argument.  */
21897   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21898   parser->local_variables_forbidden_p = true;
21899
21900   push_defarg_context (fn);
21901
21902   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21903          parmdecl = DECL_ARGUMENTS (fn);
21904        parm && parm != void_list_node;
21905        parm = TREE_CHAIN (parm),
21906          parmdecl = DECL_CHAIN (parmdecl))
21907     {
21908       tree default_arg = TREE_PURPOSE (parm);
21909       tree parsed_arg;
21910       VEC(tree,gc) *insts;
21911       tree copy;
21912       unsigned ix;
21913
21914       if (!default_arg)
21915         continue;
21916
21917       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21918         /* This can happen for a friend declaration for a function
21919            already declared with default arguments.  */
21920         continue;
21921
21922       parsed_arg
21923         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21924                                                 default_arg,
21925                                                 TREE_VALUE (parm));
21926       if (parsed_arg == error_mark_node)
21927         {
21928           continue;
21929         }
21930
21931       TREE_PURPOSE (parm) = parsed_arg;
21932
21933       /* Update any instantiations we've already created.  */
21934       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21935            VEC_iterate (tree, insts, ix, copy); ix++)
21936         TREE_PURPOSE (copy) = parsed_arg;
21937     }
21938
21939   pop_defarg_context ();
21940
21941   /* Make sure no default arg is missing.  */
21942   check_default_args (fn);
21943
21944   /* Restore the state of local_variables_forbidden_p.  */
21945   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21946
21947   /* Restore the queue.  */
21948   pop_unparsed_function_queues (parser);
21949 }
21950
21951 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21952    either a TYPE or an expression, depending on the form of the
21953    input.  The KEYWORD indicates which kind of expression we have
21954    encountered.  */
21955
21956 static tree
21957 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21958 {
21959   tree expr = NULL_TREE;
21960   const char *saved_message;
21961   char *tmp;
21962   bool saved_integral_constant_expression_p;
21963   bool saved_non_integral_constant_expression_p;
21964   bool pack_expansion_p = false;
21965
21966   /* Types cannot be defined in a `sizeof' expression.  Save away the
21967      old message.  */
21968   saved_message = parser->type_definition_forbidden_message;
21969   /* And create the new one.  */
21970   tmp = concat ("types may not be defined in %<",
21971                 IDENTIFIER_POINTER (ridpointers[keyword]),
21972                 "%> expressions", NULL);
21973   parser->type_definition_forbidden_message = tmp;
21974
21975   /* The restrictions on constant-expressions do not apply inside
21976      sizeof expressions.  */
21977   saved_integral_constant_expression_p
21978     = parser->integral_constant_expression_p;
21979   saved_non_integral_constant_expression_p
21980     = parser->non_integral_constant_expression_p;
21981   parser->integral_constant_expression_p = false;
21982
21983   /* If it's a `...', then we are computing the length of a parameter
21984      pack.  */
21985   if (keyword == RID_SIZEOF
21986       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21987     {
21988       /* Consume the `...'.  */
21989       cp_lexer_consume_token (parser->lexer);
21990       maybe_warn_variadic_templates ();
21991
21992       /* Note that this is an expansion.  */
21993       pack_expansion_p = true;
21994     }
21995
21996   /* Do not actually evaluate the expression.  */
21997   ++cp_unevaluated_operand;
21998   ++c_inhibit_evaluation_warnings;
21999   /* If it's a `(', then we might be looking at the type-id
22000      construction.  */
22001   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22002     {
22003       tree type;
22004       bool saved_in_type_id_in_expr_p;
22005
22006       /* We can't be sure yet whether we're looking at a type-id or an
22007          expression.  */
22008       cp_parser_parse_tentatively (parser);
22009       /* Consume the `('.  */
22010       cp_lexer_consume_token (parser->lexer);
22011       /* Parse the type-id.  */
22012       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22013       parser->in_type_id_in_expr_p = true;
22014       type = cp_parser_type_id (parser);
22015       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22016       /* Now, look for the trailing `)'.  */
22017       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22018       /* If all went well, then we're done.  */
22019       if (cp_parser_parse_definitely (parser))
22020         {
22021           cp_decl_specifier_seq decl_specs;
22022
22023           /* Build a trivial decl-specifier-seq.  */
22024           clear_decl_specs (&decl_specs);
22025           decl_specs.type = type;
22026
22027           /* Call grokdeclarator to figure out what type this is.  */
22028           expr = grokdeclarator (NULL,
22029                                  &decl_specs,
22030                                  TYPENAME,
22031                                  /*initialized=*/0,
22032                                  /*attrlist=*/NULL);
22033         }
22034     }
22035   else if (pack_expansion_p)
22036     permerror (cp_lexer_peek_token (parser->lexer)->location,
22037                "%<sizeof...%> argument must be surrounded by parentheses");
22038
22039   /* If the type-id production did not work out, then we must be
22040      looking at the unary-expression production.  */
22041   if (!expr)
22042     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22043                                        /*cast_p=*/false, NULL);
22044
22045   if (pack_expansion_p)
22046     /* Build a pack expansion. */
22047     expr = make_pack_expansion (expr);
22048
22049   /* Go back to evaluating expressions.  */
22050   --cp_unevaluated_operand;
22051   --c_inhibit_evaluation_warnings;
22052
22053   /* Free the message we created.  */
22054   free (tmp);
22055   /* And restore the old one.  */
22056   parser->type_definition_forbidden_message = saved_message;
22057   parser->integral_constant_expression_p
22058     = saved_integral_constant_expression_p;
22059   parser->non_integral_constant_expression_p
22060     = saved_non_integral_constant_expression_p;
22061
22062   return expr;
22063 }
22064
22065 /* If the current declaration has no declarator, return true.  */
22066
22067 static bool
22068 cp_parser_declares_only_class_p (cp_parser *parser)
22069 {
22070   /* If the next token is a `;' or a `,' then there is no
22071      declarator.  */
22072   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22073           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22074 }
22075
22076 /* Update the DECL_SPECS to reflect the storage class indicated by
22077    KEYWORD.  */
22078
22079 static void
22080 cp_parser_set_storage_class (cp_parser *parser,
22081                              cp_decl_specifier_seq *decl_specs,
22082                              enum rid keyword,
22083                              location_t location)
22084 {
22085   cp_storage_class storage_class;
22086
22087   if (parser->in_unbraced_linkage_specification_p)
22088     {
22089       error_at (location, "invalid use of %qD in linkage specification",
22090                 ridpointers[keyword]);
22091       return;
22092     }
22093   else if (decl_specs->storage_class != sc_none)
22094     {
22095       decl_specs->conflicting_specifiers_p = true;
22096       return;
22097     }
22098
22099   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22100       && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22101     {
22102       error_at (decl_specs->locations[ds_thread],
22103                 "%<__thread%> before %qD", ridpointers[keyword]);
22104       decl_specs->locations[ds_thread] = 0;
22105     }
22106
22107   switch (keyword)
22108     {
22109     case RID_AUTO:
22110       storage_class = sc_auto;
22111       break;
22112     case RID_REGISTER:
22113       storage_class = sc_register;
22114       break;
22115     case RID_STATIC:
22116       storage_class = sc_static;
22117       break;
22118     case RID_EXTERN:
22119       storage_class = sc_extern;
22120       break;
22121     case RID_MUTABLE:
22122       storage_class = sc_mutable;
22123       break;
22124     default:
22125       gcc_unreachable ();
22126     }
22127   decl_specs->storage_class = storage_class;
22128   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22129
22130   /* A storage class specifier cannot be applied alongside a typedef 
22131      specifier. If there is a typedef specifier present then set 
22132      conflicting_specifiers_p which will trigger an error later
22133      on in grokdeclarator. */
22134   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22135     decl_specs->conflicting_specifiers_p = true;
22136 }
22137
22138 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22139    is true, the type is a class or enum definition.  */
22140
22141 static void
22142 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22143                               tree type_spec,
22144                               location_t location,
22145                               bool type_definition_p)
22146 {
22147   decl_specs->any_specifiers_p = true;
22148
22149   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22150      (with, for example, in "typedef int wchar_t;") we remember that
22151      this is what happened.  In system headers, we ignore these
22152      declarations so that G++ can work with system headers that are not
22153      C++-safe.  */
22154   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22155       && !type_definition_p
22156       && (type_spec == boolean_type_node
22157           || type_spec == char16_type_node
22158           || type_spec == char32_type_node
22159           || type_spec == wchar_type_node)
22160       && (decl_specs->type
22161           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22162           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22163           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22164           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22165     {
22166       decl_specs->redefined_builtin_type = type_spec;
22167       set_and_check_decl_spec_loc (decl_specs,
22168                                    ds_redefined_builtin_type_spec,
22169                                    location);
22170       if (!decl_specs->type)
22171         {
22172           decl_specs->type = type_spec;
22173           decl_specs->type_definition_p = false;
22174           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22175         }
22176     }
22177   else if (decl_specs->type)
22178     decl_specs->multiple_types_p = true;
22179   else
22180     {
22181       decl_specs->type = type_spec;
22182       decl_specs->type_definition_p = type_definition_p;
22183       decl_specs->redefined_builtin_type = NULL_TREE;
22184       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22185     }
22186 }
22187
22188 /* Set the location for a declarator specifier and check if it is
22189    duplicated.
22190
22191    DECL_SPECS is the sequence of declarator specifiers onto which to
22192    set the location.
22193
22194    DS is the single declarator specifier to set which location  is to
22195    be set onto the existing sequence of declarators.
22196
22197    LOCATION is the location for the declarator specifier to
22198    consider.  */
22199
22200 static void
22201 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22202                              cp_decl_spec ds, source_location location)
22203 {
22204   gcc_assert (ds < ds_last);
22205
22206   if (decl_specs == NULL)
22207     return;
22208
22209   if (decl_specs->locations[ds] == 0)
22210     decl_specs->locations[ds] = location;
22211   else
22212     {
22213       if (ds == ds_long)
22214         {
22215           if (decl_specs->locations[ds_long_long] != 0)
22216             error_at (location,
22217                       "%<long long long%> is too long for GCC");
22218           else
22219             {
22220               decl_specs->locations[ds_long_long] = location;
22221               pedwarn_cxx98 (location,
22222                              OPT_Wlong_long, 
22223                              "ISO C++ 1998 does not support %<long long%>");
22224             }
22225         }
22226       else
22227         {
22228           static const char *const decl_spec_names[] = {
22229             "signed",
22230             "unsigned",
22231             "short",
22232             "long",
22233             "const",
22234             "volatile",
22235             "restrict",
22236             "inline",
22237             "virtual",
22238             "explicit",
22239             "friend",
22240             "typedef",
22241             "using",
22242             "constexpr",
22243             "__complex",
22244             "__thread"
22245           };
22246           error_at (location,
22247                     "duplicate %qs", decl_spec_names[ds]);
22248         }
22249     }
22250 }
22251
22252 /* Return true iff the declarator specifier DS is present in the
22253    sequence of declarator specifiers DECL_SPECS.  */
22254
22255 bool
22256 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22257                           cp_decl_spec ds)
22258 {
22259   gcc_assert (ds < ds_last);
22260
22261   if (decl_specs == NULL)
22262     return false;
22263
22264   return decl_specs->locations[ds] != 0;
22265 }
22266
22267 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22268    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22269
22270 static bool
22271 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22272 {
22273   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22274 }
22275
22276 /* Issue an error message indicating that TOKEN_DESC was expected.
22277    If KEYWORD is true, it indicated this function is called by
22278    cp_parser_require_keword and the required token can only be
22279    a indicated keyword. */
22280
22281 static void
22282 cp_parser_required_error (cp_parser *parser,
22283                           required_token token_desc,
22284                           bool keyword)
22285 {
22286   switch (token_desc)
22287     {
22288       case RT_NEW:
22289         cp_parser_error (parser, "expected %<new%>");
22290         return;
22291       case RT_DELETE:
22292         cp_parser_error (parser, "expected %<delete%>");
22293         return;
22294       case RT_RETURN:
22295         cp_parser_error (parser, "expected %<return%>");
22296         return;
22297       case RT_WHILE:
22298         cp_parser_error (parser, "expected %<while%>");
22299         return;
22300       case RT_EXTERN:
22301         cp_parser_error (parser, "expected %<extern%>");
22302         return;
22303       case RT_STATIC_ASSERT:
22304         cp_parser_error (parser, "expected %<static_assert%>");
22305         return;
22306       case RT_DECLTYPE:
22307         cp_parser_error (parser, "expected %<decltype%>");
22308         return;
22309       case RT_OPERATOR:
22310         cp_parser_error (parser, "expected %<operator%>");
22311         return;
22312       case RT_CLASS:
22313         cp_parser_error (parser, "expected %<class%>");
22314         return;
22315       case RT_TEMPLATE:
22316         cp_parser_error (parser, "expected %<template%>");
22317         return;
22318       case RT_NAMESPACE:
22319         cp_parser_error (parser, "expected %<namespace%>");
22320         return;
22321       case RT_USING:
22322         cp_parser_error (parser, "expected %<using%>");
22323         return;
22324       case RT_ASM:
22325         cp_parser_error (parser, "expected %<asm%>");
22326         return;
22327       case RT_TRY:
22328         cp_parser_error (parser, "expected %<try%>");
22329         return;
22330       case RT_CATCH:
22331         cp_parser_error (parser, "expected %<catch%>");
22332         return;
22333       case RT_THROW:
22334         cp_parser_error (parser, "expected %<throw%>");
22335         return;
22336       case RT_LABEL:
22337         cp_parser_error (parser, "expected %<__label__%>");
22338         return;
22339       case RT_AT_TRY:
22340         cp_parser_error (parser, "expected %<@try%>");
22341         return;
22342       case RT_AT_SYNCHRONIZED:
22343         cp_parser_error (parser, "expected %<@synchronized%>");
22344         return;
22345       case RT_AT_THROW:
22346         cp_parser_error (parser, "expected %<@throw%>");
22347         return;
22348       case RT_TRANSACTION_ATOMIC:
22349         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22350         return;
22351       case RT_TRANSACTION_RELAXED:
22352         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22353         return;
22354       default:
22355         break;
22356     }
22357   if (!keyword)
22358     {
22359       switch (token_desc)
22360         {
22361           case RT_SEMICOLON:
22362             cp_parser_error (parser, "expected %<;%>");
22363             return;
22364           case RT_OPEN_PAREN:
22365             cp_parser_error (parser, "expected %<(%>");
22366             return;
22367           case RT_CLOSE_BRACE:
22368             cp_parser_error (parser, "expected %<}%>");
22369             return;
22370           case RT_OPEN_BRACE:
22371             cp_parser_error (parser, "expected %<{%>");
22372             return;
22373           case RT_CLOSE_SQUARE:
22374             cp_parser_error (parser, "expected %<]%>");
22375             return;
22376           case RT_OPEN_SQUARE:
22377             cp_parser_error (parser, "expected %<[%>");
22378             return;
22379           case RT_COMMA:
22380             cp_parser_error (parser, "expected %<,%>");
22381             return;
22382           case RT_SCOPE:
22383             cp_parser_error (parser, "expected %<::%>");
22384             return;
22385           case RT_LESS:
22386             cp_parser_error (parser, "expected %<<%>");
22387             return;
22388           case RT_GREATER:
22389             cp_parser_error (parser, "expected %<>%>");
22390             return;
22391           case RT_EQ:
22392             cp_parser_error (parser, "expected %<=%>");
22393             return;
22394           case RT_ELLIPSIS:
22395             cp_parser_error (parser, "expected %<...%>");
22396             return;
22397           case RT_MULT:
22398             cp_parser_error (parser, "expected %<*%>");
22399             return;
22400           case RT_COMPL:
22401             cp_parser_error (parser, "expected %<~%>");
22402             return;
22403           case RT_COLON:
22404             cp_parser_error (parser, "expected %<:%>");
22405             return;
22406           case RT_COLON_SCOPE:
22407             cp_parser_error (parser, "expected %<:%> or %<::%>");
22408             return;
22409           case RT_CLOSE_PAREN:
22410             cp_parser_error (parser, "expected %<)%>");
22411             return;
22412           case RT_COMMA_CLOSE_PAREN:
22413             cp_parser_error (parser, "expected %<,%> or %<)%>");
22414             return;
22415           case RT_PRAGMA_EOL:
22416             cp_parser_error (parser, "expected end of line");
22417             return;
22418           case RT_NAME:
22419             cp_parser_error (parser, "expected identifier");
22420             return;
22421           case RT_SELECT:
22422             cp_parser_error (parser, "expected selection-statement");
22423             return;
22424           case RT_INTERATION:
22425             cp_parser_error (parser, "expected iteration-statement");
22426             return;
22427           case RT_JUMP:
22428             cp_parser_error (parser, "expected jump-statement");
22429             return;
22430           case RT_CLASS_KEY:
22431             cp_parser_error (parser, "expected class-key");
22432             return;
22433           case RT_CLASS_TYPENAME_TEMPLATE:
22434             cp_parser_error (parser,
22435                  "expected %<class%>, %<typename%>, or %<template%>");
22436             return;
22437           default:
22438             gcc_unreachable ();
22439         }
22440     }
22441   else
22442     gcc_unreachable ();
22443 }
22444
22445
22446
22447 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22448    issue an error message indicating that TOKEN_DESC was expected.
22449
22450    Returns the token consumed, if the token had the appropriate type.
22451    Otherwise, returns NULL.  */
22452
22453 static cp_token *
22454 cp_parser_require (cp_parser* parser,
22455                    enum cpp_ttype type,
22456                    required_token token_desc)
22457 {
22458   if (cp_lexer_next_token_is (parser->lexer, type))
22459     return cp_lexer_consume_token (parser->lexer);
22460   else
22461     {
22462       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22463       if (!cp_parser_simulate_error (parser))
22464         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22465       return NULL;
22466     }
22467 }
22468
22469 /* An error message is produced if the next token is not '>'.
22470    All further tokens are skipped until the desired token is
22471    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22472
22473 static void
22474 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22475 {
22476   /* Current level of '< ... >'.  */
22477   unsigned level = 0;
22478   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22479   unsigned nesting_depth = 0;
22480
22481   /* Are we ready, yet?  If not, issue error message.  */
22482   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22483     return;
22484
22485   /* Skip tokens until the desired token is found.  */
22486   while (true)
22487     {
22488       /* Peek at the next token.  */
22489       switch (cp_lexer_peek_token (parser->lexer)->type)
22490         {
22491         case CPP_LESS:
22492           if (!nesting_depth)
22493             ++level;
22494           break;
22495
22496         case CPP_RSHIFT:
22497           if (cxx_dialect == cxx98)
22498             /* C++0x views the `>>' operator as two `>' tokens, but
22499                C++98 does not. */
22500             break;
22501           else if (!nesting_depth && level-- == 0)
22502             {
22503               /* We've hit a `>>' where the first `>' closes the
22504                  template argument list, and the second `>' is
22505                  spurious.  Just consume the `>>' and stop; we've
22506                  already produced at least one error.  */
22507               cp_lexer_consume_token (parser->lexer);
22508               return;
22509             }
22510           /* Fall through for C++0x, so we handle the second `>' in
22511              the `>>'.  */
22512
22513         case CPP_GREATER:
22514           if (!nesting_depth && level-- == 0)
22515             {
22516               /* We've reached the token we want, consume it and stop.  */
22517               cp_lexer_consume_token (parser->lexer);
22518               return;
22519             }
22520           break;
22521
22522         case CPP_OPEN_PAREN:
22523         case CPP_OPEN_SQUARE:
22524           ++nesting_depth;
22525           break;
22526
22527         case CPP_CLOSE_PAREN:
22528         case CPP_CLOSE_SQUARE:
22529           if (nesting_depth-- == 0)
22530             return;
22531           break;
22532
22533         case CPP_EOF:
22534         case CPP_PRAGMA_EOL:
22535         case CPP_SEMICOLON:
22536         case CPP_OPEN_BRACE:
22537         case CPP_CLOSE_BRACE:
22538           /* The '>' was probably forgotten, don't look further.  */
22539           return;
22540
22541         default:
22542           break;
22543         }
22544
22545       /* Consume this token.  */
22546       cp_lexer_consume_token (parser->lexer);
22547     }
22548 }
22549
22550 /* If the next token is the indicated keyword, consume it.  Otherwise,
22551    issue an error message indicating that TOKEN_DESC was expected.
22552
22553    Returns the token consumed, if the token had the appropriate type.
22554    Otherwise, returns NULL.  */
22555
22556 static cp_token *
22557 cp_parser_require_keyword (cp_parser* parser,
22558                            enum rid keyword,
22559                            required_token token_desc)
22560 {
22561   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22562
22563   if (token && token->keyword != keyword)
22564     {
22565       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22566       return NULL;
22567     }
22568
22569   return token;
22570 }
22571
22572 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22573    function-definition.  */
22574
22575 static bool
22576 cp_parser_token_starts_function_definition_p (cp_token* token)
22577 {
22578   return (/* An ordinary function-body begins with an `{'.  */
22579           token->type == CPP_OPEN_BRACE
22580           /* A ctor-initializer begins with a `:'.  */
22581           || token->type == CPP_COLON
22582           /* A function-try-block begins with `try'.  */
22583           || token->keyword == RID_TRY
22584           /* A function-transaction-block begins with `__transaction_atomic'
22585              or `__transaction_relaxed'.  */
22586           || token->keyword == RID_TRANSACTION_ATOMIC
22587           || token->keyword == RID_TRANSACTION_RELAXED
22588           /* The named return value extension begins with `return'.  */
22589           || token->keyword == RID_RETURN);
22590 }
22591
22592 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22593    definition.  */
22594
22595 static bool
22596 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22597 {
22598   cp_token *token;
22599
22600   token = cp_lexer_peek_token (parser->lexer);
22601   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22602 }
22603
22604 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22605    C++0x) ending a template-argument.  */
22606
22607 static bool
22608 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22609 {
22610   cp_token *token;
22611
22612   token = cp_lexer_peek_token (parser->lexer);
22613   return (token->type == CPP_COMMA 
22614           || token->type == CPP_GREATER
22615           || token->type == CPP_ELLIPSIS
22616           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22617 }
22618
22619 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22620    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22621
22622 static bool
22623 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22624                                                      size_t n)
22625 {
22626   cp_token *token;
22627
22628   token = cp_lexer_peek_nth_token (parser->lexer, n);
22629   if (token->type == CPP_LESS)
22630     return true;
22631   /* Check for the sequence `<::' in the original code. It would be lexed as
22632      `[:', where `[' is a digraph, and there is no whitespace before
22633      `:'.  */
22634   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22635     {
22636       cp_token *token2;
22637       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22638       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22639         return true;
22640     }
22641   return false;
22642 }
22643
22644 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22645    or none_type otherwise.  */
22646
22647 static enum tag_types
22648 cp_parser_token_is_class_key (cp_token* token)
22649 {
22650   switch (token->keyword)
22651     {
22652     case RID_CLASS:
22653       return class_type;
22654     case RID_STRUCT:
22655       return record_type;
22656     case RID_UNION:
22657       return union_type;
22658
22659     default:
22660       return none_type;
22661     }
22662 }
22663
22664 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22665
22666 static void
22667 cp_parser_check_class_key (enum tag_types class_key, tree type)
22668 {
22669   if (type == error_mark_node)
22670     return;
22671   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22672     {
22673       permerror (input_location, "%qs tag used in naming %q#T",
22674                  class_key == union_type ? "union"
22675                  : class_key == record_type ? "struct" : "class",
22676                  type);
22677       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22678               "%q#T was previously declared here", type);
22679     }
22680 }
22681
22682 /* Issue an error message if DECL is redeclared with different
22683    access than its original declaration [class.access.spec/3].
22684    This applies to nested classes and nested class templates.
22685    [class.mem/1].  */
22686
22687 static void
22688 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22689 {
22690   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22691     return;
22692
22693   if ((TREE_PRIVATE (decl)
22694        != (current_access_specifier == access_private_node))
22695       || (TREE_PROTECTED (decl)
22696           != (current_access_specifier == access_protected_node)))
22697     error_at (location, "%qD redeclared with different access", decl);
22698 }
22699
22700 /* Look for the `template' keyword, as a syntactic disambiguator.
22701    Return TRUE iff it is present, in which case it will be
22702    consumed.  */
22703
22704 static bool
22705 cp_parser_optional_template_keyword (cp_parser *parser)
22706 {
22707   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22708     {
22709       /* The `template' keyword can only be used within templates;
22710          outside templates the parser can always figure out what is a
22711          template and what is not.  */
22712       if (!processing_template_decl)
22713         {
22714           cp_token *token = cp_lexer_peek_token (parser->lexer);
22715           error_at (token->location,
22716                     "%<template%> (as a disambiguator) is only allowed "
22717                     "within templates");
22718           /* If this part of the token stream is rescanned, the same
22719              error message would be generated.  So, we purge the token
22720              from the stream.  */
22721           cp_lexer_purge_token (parser->lexer);
22722           return false;
22723         }
22724       else
22725         {
22726           /* Consume the `template' keyword.  */
22727           cp_lexer_consume_token (parser->lexer);
22728           return true;
22729         }
22730     }
22731
22732   return false;
22733 }
22734
22735 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22736    set PARSER->SCOPE, and perform other related actions.  */
22737
22738 static void
22739 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22740 {
22741   int i;
22742   struct tree_check *check_value;
22743   deferred_access_check *chk;
22744   VEC (deferred_access_check,gc) *checks;
22745
22746   /* Get the stored value.  */
22747   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22748   /* Perform any access checks that were deferred.  */
22749   checks = check_value->checks;
22750   if (checks)
22751     {
22752       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22753         perform_or_defer_access_check (chk->binfo,
22754                                        chk->decl,
22755                                        chk->diag_decl);
22756     }
22757   /* Set the scope from the stored value.  */
22758   parser->scope = check_value->value;
22759   parser->qualifying_scope = check_value->qualifying_scope;
22760   parser->object_scope = NULL_TREE;
22761 }
22762
22763 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22764    encounter the end of a block before what we were looking for.  */
22765
22766 static bool
22767 cp_parser_cache_group (cp_parser *parser,
22768                        enum cpp_ttype end,
22769                        unsigned depth)
22770 {
22771   while (true)
22772     {
22773       cp_token *token = cp_lexer_peek_token (parser->lexer);
22774
22775       /* Abort a parenthesized expression if we encounter a semicolon.  */
22776       if ((end == CPP_CLOSE_PAREN || depth == 0)
22777           && token->type == CPP_SEMICOLON)
22778         return true;
22779       /* If we've reached the end of the file, stop.  */
22780       if (token->type == CPP_EOF
22781           || (end != CPP_PRAGMA_EOL
22782               && token->type == CPP_PRAGMA_EOL))
22783         return true;
22784       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22785         /* We've hit the end of an enclosing block, so there's been some
22786            kind of syntax error.  */
22787         return true;
22788
22789       /* Consume the token.  */
22790       cp_lexer_consume_token (parser->lexer);
22791       /* See if it starts a new group.  */
22792       if (token->type == CPP_OPEN_BRACE)
22793         {
22794           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22795           /* In theory this should probably check end == '}', but
22796              cp_parser_save_member_function_body needs it to exit
22797              after either '}' or ')' when called with ')'.  */
22798           if (depth == 0)
22799             return false;
22800         }
22801       else if (token->type == CPP_OPEN_PAREN)
22802         {
22803           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22804           if (depth == 0 && end == CPP_CLOSE_PAREN)
22805             return false;
22806         }
22807       else if (token->type == CPP_PRAGMA)
22808         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22809       else if (token->type == end)
22810         return false;
22811     }
22812 }
22813
22814 /* Like above, for caching a default argument or NSDMI.  Both of these are
22815    terminated by a non-nested comma, but it can be unclear whether or not a
22816    comma is nested in a template argument list unless we do more parsing.
22817    In order to handle this ambiguity, when we encounter a ',' after a '<'
22818    we try to parse what follows as a parameter-declaration-list (in the
22819    case of a default argument) or a member-declarator (in the case of an
22820    NSDMI).  If that succeeds, then we stop caching.  */
22821
22822 static tree
22823 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22824 {
22825   unsigned depth = 0;
22826   int maybe_template_id = 0;
22827   cp_token *first_token;
22828   cp_token *token;
22829   tree default_argument;
22830
22831   /* Add tokens until we have processed the entire default
22832      argument.  We add the range [first_token, token).  */
22833   first_token = cp_lexer_peek_token (parser->lexer);
22834   if (first_token->type == CPP_OPEN_BRACE)
22835     {
22836       /* For list-initialization, this is straightforward.  */
22837       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22838       token = cp_lexer_peek_token (parser->lexer);
22839     }
22840   else while (true)
22841     {
22842       bool done = false;
22843
22844       /* Peek at the next token.  */
22845       token = cp_lexer_peek_token (parser->lexer);
22846       /* What we do depends on what token we have.  */
22847       switch (token->type)
22848         {
22849           /* In valid code, a default argument must be
22850              immediately followed by a `,' `)', or `...'.  */
22851         case CPP_COMMA:
22852           if (depth == 0 && maybe_template_id)
22853             {
22854               /* If we've seen a '<', we might be in a
22855                  template-argument-list.  Until Core issue 325 is
22856                  resolved, we don't know how this situation ought
22857                  to be handled, so try to DTRT.  We check whether
22858                  what comes after the comma is a valid parameter
22859                  declaration list.  If it is, then the comma ends
22860                  the default argument; otherwise the default
22861                  argument continues.  */
22862               bool error = false;
22863               tree t;
22864
22865               /* Set ITALP so cp_parser_parameter_declaration_list
22866                  doesn't decide to commit to this parse.  */
22867               bool saved_italp = parser->in_template_argument_list_p;
22868               parser->in_template_argument_list_p = true;
22869
22870               cp_parser_parse_tentatively (parser);
22871               cp_lexer_consume_token (parser->lexer);
22872
22873               if (nsdmi)
22874                 {
22875                   int ctor_dtor_or_conv_p;
22876                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22877                                         &ctor_dtor_or_conv_p,
22878                                         /*parenthesized_p=*/NULL,
22879                                         /*member_p=*/true);
22880                 }
22881               else
22882                 {
22883                   begin_scope (sk_function_parms, NULL_TREE);
22884                   cp_parser_parameter_declaration_list (parser, &error);
22885                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22886                     pop_binding (DECL_NAME (t), t);
22887                   leave_scope ();
22888                 }
22889               if (!cp_parser_error_occurred (parser) && !error)
22890                 done = true;
22891               cp_parser_abort_tentative_parse (parser);
22892
22893               parser->in_template_argument_list_p = saved_italp;
22894               break;
22895             }
22896         case CPP_CLOSE_PAREN:
22897         case CPP_ELLIPSIS:
22898           /* If we run into a non-nested `;', `}', or `]',
22899              then the code is invalid -- but the default
22900              argument is certainly over.  */
22901         case CPP_SEMICOLON:
22902         case CPP_CLOSE_BRACE:
22903         case CPP_CLOSE_SQUARE:
22904           if (depth == 0)
22905             done = true;
22906           /* Update DEPTH, if necessary.  */
22907           else if (token->type == CPP_CLOSE_PAREN
22908                    || token->type == CPP_CLOSE_BRACE
22909                    || token->type == CPP_CLOSE_SQUARE)
22910             --depth;
22911           break;
22912
22913         case CPP_OPEN_PAREN:
22914         case CPP_OPEN_SQUARE:
22915         case CPP_OPEN_BRACE:
22916           ++depth;
22917           break;
22918
22919         case CPP_LESS:
22920           if (depth == 0)
22921             /* This might be the comparison operator, or it might
22922                start a template argument list.  */
22923             ++maybe_template_id;
22924           break;
22925
22926         case CPP_RSHIFT:
22927           if (cxx_dialect == cxx98)
22928             break;
22929           /* Fall through for C++0x, which treats the `>>'
22930              operator like two `>' tokens in certain
22931              cases.  */
22932
22933         case CPP_GREATER:
22934           if (depth == 0)
22935             {
22936               /* This might be an operator, or it might close a
22937                  template argument list.  But if a previous '<'
22938                  started a template argument list, this will have
22939                  closed it, so we can't be in one anymore.  */
22940               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22941               if (maybe_template_id < 0)
22942                 maybe_template_id = 0;
22943             }
22944           break;
22945
22946           /* If we run out of tokens, issue an error message.  */
22947         case CPP_EOF:
22948         case CPP_PRAGMA_EOL:
22949           error_at (token->location, "file ends in default argument");
22950           done = true;
22951           break;
22952
22953         case CPP_NAME:
22954         case CPP_SCOPE:
22955           /* In these cases, we should look for template-ids.
22956              For example, if the default argument is
22957              `X<int, double>()', we need to do name lookup to
22958              figure out whether or not `X' is a template; if
22959              so, the `,' does not end the default argument.
22960
22961              That is not yet done.  */
22962           break;
22963
22964         default:
22965           break;
22966         }
22967
22968       /* If we've reached the end, stop.  */
22969       if (done)
22970         break;
22971
22972       /* Add the token to the token block.  */
22973       token = cp_lexer_consume_token (parser->lexer);
22974     }
22975
22976   /* Create a DEFAULT_ARG to represent the unparsed default
22977      argument.  */
22978   default_argument = make_node (DEFAULT_ARG);
22979   DEFARG_TOKENS (default_argument)
22980     = cp_token_cache_new (first_token, token);
22981   DEFARG_INSTANTIATIONS (default_argument) = NULL;
22982
22983   return default_argument;
22984 }
22985
22986 /* Begin parsing tentatively.  We always save tokens while parsing
22987    tentatively so that if the tentative parsing fails we can restore the
22988    tokens.  */
22989
22990 static void
22991 cp_parser_parse_tentatively (cp_parser* parser)
22992 {
22993   /* Enter a new parsing context.  */
22994   parser->context = cp_parser_context_new (parser->context);
22995   /* Begin saving tokens.  */
22996   cp_lexer_save_tokens (parser->lexer);
22997   /* In order to avoid repetitive access control error messages,
22998      access checks are queued up until we are no longer parsing
22999      tentatively.  */
23000   push_deferring_access_checks (dk_deferred);
23001 }
23002
23003 /* Commit to the currently active tentative parse.  */
23004
23005 static void
23006 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23007 {
23008   cp_parser_context *context;
23009   cp_lexer *lexer;
23010
23011   /* Mark all of the levels as committed.  */
23012   lexer = parser->lexer;
23013   for (context = parser->context; context->next; context = context->next)
23014     {
23015       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23016         break;
23017       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23018       while (!cp_lexer_saving_tokens (lexer))
23019         lexer = lexer->next;
23020       cp_lexer_commit_tokens (lexer);
23021     }
23022 }
23023
23024 /* Abort the currently active tentative parse.  All consumed tokens
23025    will be rolled back, and no diagnostics will be issued.  */
23026
23027 static void
23028 cp_parser_abort_tentative_parse (cp_parser* parser)
23029 {
23030   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23031               || errorcount > 0);
23032   cp_parser_simulate_error (parser);
23033   /* Now, pretend that we want to see if the construct was
23034      successfully parsed.  */
23035   cp_parser_parse_definitely (parser);
23036 }
23037
23038 /* Stop parsing tentatively.  If a parse error has occurred, restore the
23039    token stream.  Otherwise, commit to the tokens we have consumed.
23040    Returns true if no error occurred; false otherwise.  */
23041
23042 static bool
23043 cp_parser_parse_definitely (cp_parser* parser)
23044 {
23045   bool error_occurred;
23046   cp_parser_context *context;
23047
23048   /* Remember whether or not an error occurred, since we are about to
23049      destroy that information.  */
23050   error_occurred = cp_parser_error_occurred (parser);
23051   /* Remove the topmost context from the stack.  */
23052   context = parser->context;
23053   parser->context = context->next;
23054   /* If no parse errors occurred, commit to the tentative parse.  */
23055   if (!error_occurred)
23056     {
23057       /* Commit to the tokens read tentatively, unless that was
23058          already done.  */
23059       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23060         cp_lexer_commit_tokens (parser->lexer);
23061
23062       pop_to_parent_deferring_access_checks ();
23063     }
23064   /* Otherwise, if errors occurred, roll back our state so that things
23065      are just as they were before we began the tentative parse.  */
23066   else
23067     {
23068       cp_lexer_rollback_tokens (parser->lexer);
23069       pop_deferring_access_checks ();
23070     }
23071   /* Add the context to the front of the free list.  */
23072   context->next = cp_parser_context_free_list;
23073   cp_parser_context_free_list = context;
23074
23075   return !error_occurred;
23076 }
23077
23078 /* Returns true if we are parsing tentatively and are not committed to
23079    this tentative parse.  */
23080
23081 static bool
23082 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23083 {
23084   return (cp_parser_parsing_tentatively (parser)
23085           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23086 }
23087
23088 /* Returns nonzero iff an error has occurred during the most recent
23089    tentative parse.  */
23090
23091 static bool
23092 cp_parser_error_occurred (cp_parser* parser)
23093 {
23094   return (cp_parser_parsing_tentatively (parser)
23095           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23096 }
23097
23098 /* Returns nonzero if GNU extensions are allowed.  */
23099
23100 static bool
23101 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23102 {
23103   return parser->allow_gnu_extensions_p;
23104 }
23105 \f
23106 /* Objective-C++ Productions */
23107
23108
23109 /* Parse an Objective-C expression, which feeds into a primary-expression
23110    above.
23111
23112    objc-expression:
23113      objc-message-expression
23114      objc-string-literal
23115      objc-encode-expression
23116      objc-protocol-expression
23117      objc-selector-expression
23118
23119   Returns a tree representation of the expression.  */
23120
23121 static tree
23122 cp_parser_objc_expression (cp_parser* parser)
23123 {
23124   /* Try to figure out what kind of declaration is present.  */
23125   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23126
23127   switch (kwd->type)
23128     {
23129     case CPP_OPEN_SQUARE:
23130       return cp_parser_objc_message_expression (parser);
23131
23132     case CPP_OBJC_STRING:
23133       kwd = cp_lexer_consume_token (parser->lexer);
23134       return objc_build_string_object (kwd->u.value);
23135
23136     case CPP_KEYWORD:
23137       switch (kwd->keyword)
23138         {
23139         case RID_AT_ENCODE:
23140           return cp_parser_objc_encode_expression (parser);
23141
23142         case RID_AT_PROTOCOL:
23143           return cp_parser_objc_protocol_expression (parser);
23144
23145         case RID_AT_SELECTOR:
23146           return cp_parser_objc_selector_expression (parser);
23147
23148         default:
23149           break;
23150         }
23151     default:
23152       error_at (kwd->location,
23153                 "misplaced %<@%D%> Objective-C++ construct",
23154                 kwd->u.value);
23155       cp_parser_skip_to_end_of_block_or_statement (parser);
23156     }
23157
23158   return error_mark_node;
23159 }
23160
23161 /* Parse an Objective-C message expression.
23162
23163    objc-message-expression:
23164      [ objc-message-receiver objc-message-args ]
23165
23166    Returns a representation of an Objective-C message.  */
23167
23168 static tree
23169 cp_parser_objc_message_expression (cp_parser* parser)
23170 {
23171   tree receiver, messageargs;
23172
23173   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
23174   receiver = cp_parser_objc_message_receiver (parser);
23175   messageargs = cp_parser_objc_message_args (parser);
23176   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23177
23178   return objc_build_message_expr (receiver, messageargs);
23179 }
23180
23181 /* Parse an objc-message-receiver.
23182
23183    objc-message-receiver:
23184      expression
23185      simple-type-specifier
23186
23187   Returns a representation of the type or expression.  */
23188
23189 static tree
23190 cp_parser_objc_message_receiver (cp_parser* parser)
23191 {
23192   tree rcv;
23193
23194   /* An Objective-C message receiver may be either (1) a type
23195      or (2) an expression.  */
23196   cp_parser_parse_tentatively (parser);
23197   rcv = cp_parser_expression (parser, false, NULL);
23198
23199   if (cp_parser_parse_definitely (parser))
23200     return rcv;
23201
23202   rcv = cp_parser_simple_type_specifier (parser,
23203                                          /*decl_specs=*/NULL,
23204                                          CP_PARSER_FLAGS_NONE);
23205
23206   return objc_get_class_reference (rcv);
23207 }
23208
23209 /* Parse the arguments and selectors comprising an Objective-C message.
23210
23211    objc-message-args:
23212      objc-selector
23213      objc-selector-args
23214      objc-selector-args , objc-comma-args
23215
23216    objc-selector-args:
23217      objc-selector [opt] : assignment-expression
23218      objc-selector-args objc-selector [opt] : assignment-expression
23219
23220    objc-comma-args:
23221      assignment-expression
23222      objc-comma-args , assignment-expression
23223
23224    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23225    selector arguments and TREE_VALUE containing a list of comma
23226    arguments.  */
23227
23228 static tree
23229 cp_parser_objc_message_args (cp_parser* parser)
23230 {
23231   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23232   bool maybe_unary_selector_p = true;
23233   cp_token *token = cp_lexer_peek_token (parser->lexer);
23234
23235   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23236     {
23237       tree selector = NULL_TREE, arg;
23238
23239       if (token->type != CPP_COLON)
23240         selector = cp_parser_objc_selector (parser);
23241
23242       /* Detect if we have a unary selector.  */
23243       if (maybe_unary_selector_p
23244           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23245         return build_tree_list (selector, NULL_TREE);
23246
23247       maybe_unary_selector_p = false;
23248       cp_parser_require (parser, CPP_COLON, RT_COLON);
23249       arg = cp_parser_assignment_expression (parser, false, NULL);
23250
23251       sel_args
23252         = chainon (sel_args,
23253                    build_tree_list (selector, arg));
23254
23255       token = cp_lexer_peek_token (parser->lexer);
23256     }
23257
23258   /* Handle non-selector arguments, if any. */
23259   while (token->type == CPP_COMMA)
23260     {
23261       tree arg;
23262
23263       cp_lexer_consume_token (parser->lexer);
23264       arg = cp_parser_assignment_expression (parser, false, NULL);
23265
23266       addl_args
23267         = chainon (addl_args,
23268                    build_tree_list (NULL_TREE, arg));
23269
23270       token = cp_lexer_peek_token (parser->lexer);
23271     }
23272
23273   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23274     {
23275       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23276       return build_tree_list (error_mark_node, error_mark_node);
23277     }
23278
23279   return build_tree_list (sel_args, addl_args);
23280 }
23281
23282 /* Parse an Objective-C encode expression.
23283
23284    objc-encode-expression:
23285      @encode objc-typename
23286
23287    Returns an encoded representation of the type argument.  */
23288
23289 static tree
23290 cp_parser_objc_encode_expression (cp_parser* parser)
23291 {
23292   tree type;
23293   cp_token *token;
23294
23295   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23296   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23297   token = cp_lexer_peek_token (parser->lexer);
23298   type = complete_type (cp_parser_type_id (parser));
23299   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23300
23301   if (!type)
23302     {
23303       error_at (token->location, 
23304                 "%<@encode%> must specify a type as an argument");
23305       return error_mark_node;
23306     }
23307
23308   /* This happens if we find @encode(T) (where T is a template
23309      typename or something dependent on a template typename) when
23310      parsing a template.  In that case, we can't compile it
23311      immediately, but we rather create an AT_ENCODE_EXPR which will
23312      need to be instantiated when the template is used.
23313   */
23314   if (dependent_type_p (type))
23315     {
23316       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23317       TREE_READONLY (value) = 1;
23318       return value;
23319     }
23320
23321   return objc_build_encode_expr (type);
23322 }
23323
23324 /* Parse an Objective-C @defs expression.  */
23325
23326 static tree
23327 cp_parser_objc_defs_expression (cp_parser *parser)
23328 {
23329   tree name;
23330
23331   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23332   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23333   name = cp_parser_identifier (parser);
23334   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23335
23336   return objc_get_class_ivars (name);
23337 }
23338
23339 /* Parse an Objective-C protocol expression.
23340
23341   objc-protocol-expression:
23342     @protocol ( identifier )
23343
23344   Returns a representation of the protocol expression.  */
23345
23346 static tree
23347 cp_parser_objc_protocol_expression (cp_parser* parser)
23348 {
23349   tree proto;
23350
23351   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23352   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23353   proto = cp_parser_identifier (parser);
23354   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23355
23356   return objc_build_protocol_expr (proto);
23357 }
23358
23359 /* Parse an Objective-C selector expression.
23360
23361    objc-selector-expression:
23362      @selector ( objc-method-signature )
23363
23364    objc-method-signature:
23365      objc-selector
23366      objc-selector-seq
23367
23368    objc-selector-seq:
23369      objc-selector :
23370      objc-selector-seq objc-selector :
23371
23372   Returns a representation of the method selector.  */
23373
23374 static tree
23375 cp_parser_objc_selector_expression (cp_parser* parser)
23376 {
23377   tree sel_seq = NULL_TREE;
23378   bool maybe_unary_selector_p = true;
23379   cp_token *token;
23380   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23381
23382   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23383   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23384   token = cp_lexer_peek_token (parser->lexer);
23385
23386   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23387          || token->type == CPP_SCOPE)
23388     {
23389       tree selector = NULL_TREE;
23390
23391       if (token->type != CPP_COLON
23392           || token->type == CPP_SCOPE)
23393         selector = cp_parser_objc_selector (parser);
23394
23395       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23396           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23397         {
23398           /* Detect if we have a unary selector.  */
23399           if (maybe_unary_selector_p)
23400             {
23401               sel_seq = selector;
23402               goto finish_selector;
23403             }
23404           else
23405             {
23406               cp_parser_error (parser, "expected %<:%>");
23407             }
23408         }
23409       maybe_unary_selector_p = false;
23410       token = cp_lexer_consume_token (parser->lexer);
23411
23412       if (token->type == CPP_SCOPE)
23413         {
23414           sel_seq
23415             = chainon (sel_seq,
23416                        build_tree_list (selector, NULL_TREE));
23417           sel_seq
23418             = chainon (sel_seq,
23419                        build_tree_list (NULL_TREE, NULL_TREE));
23420         }
23421       else
23422         sel_seq
23423           = chainon (sel_seq,
23424                      build_tree_list (selector, NULL_TREE));
23425
23426       token = cp_lexer_peek_token (parser->lexer);
23427     }
23428
23429  finish_selector:
23430   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23431
23432   return objc_build_selector_expr (loc, sel_seq);
23433 }
23434
23435 /* Parse a list of identifiers.
23436
23437    objc-identifier-list:
23438      identifier
23439      objc-identifier-list , identifier
23440
23441    Returns a TREE_LIST of identifier nodes.  */
23442
23443 static tree
23444 cp_parser_objc_identifier_list (cp_parser* parser)
23445 {
23446   tree identifier;
23447   tree list;
23448   cp_token *sep;
23449
23450   identifier = cp_parser_identifier (parser);
23451   if (identifier == error_mark_node)
23452     return error_mark_node;      
23453
23454   list = build_tree_list (NULL_TREE, identifier);
23455   sep = cp_lexer_peek_token (parser->lexer);
23456
23457   while (sep->type == CPP_COMMA)
23458     {
23459       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23460       identifier = cp_parser_identifier (parser);
23461       if (identifier == error_mark_node)
23462         return list;
23463
23464       list = chainon (list, build_tree_list (NULL_TREE,
23465                                              identifier));
23466       sep = cp_lexer_peek_token (parser->lexer);
23467     }
23468   
23469   return list;
23470 }
23471
23472 /* Parse an Objective-C alias declaration.
23473
23474    objc-alias-declaration:
23475      @compatibility_alias identifier identifier ;
23476
23477    This function registers the alias mapping with the Objective-C front end.
23478    It returns nothing.  */
23479
23480 static void
23481 cp_parser_objc_alias_declaration (cp_parser* parser)
23482 {
23483   tree alias, orig;
23484
23485   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23486   alias = cp_parser_identifier (parser);
23487   orig = cp_parser_identifier (parser);
23488   objc_declare_alias (alias, orig);
23489   cp_parser_consume_semicolon_at_end_of_statement (parser);
23490 }
23491
23492 /* Parse an Objective-C class forward-declaration.
23493
23494    objc-class-declaration:
23495      @class objc-identifier-list ;
23496
23497    The function registers the forward declarations with the Objective-C
23498    front end.  It returns nothing.  */
23499
23500 static void
23501 cp_parser_objc_class_declaration (cp_parser* parser)
23502 {
23503   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23504   while (true)
23505     {
23506       tree id;
23507       
23508       id = cp_parser_identifier (parser);
23509       if (id == error_mark_node)
23510         break;
23511       
23512       objc_declare_class (id);
23513
23514       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23515         cp_lexer_consume_token (parser->lexer);
23516       else
23517         break;
23518     }
23519   cp_parser_consume_semicolon_at_end_of_statement (parser);
23520 }
23521
23522 /* Parse a list of Objective-C protocol references.
23523
23524    objc-protocol-refs-opt:
23525      objc-protocol-refs [opt]
23526
23527    objc-protocol-refs:
23528      < objc-identifier-list >
23529
23530    Returns a TREE_LIST of identifiers, if any.  */
23531
23532 static tree
23533 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23534 {
23535   tree protorefs = NULL_TREE;
23536
23537   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23538     {
23539       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23540       protorefs = cp_parser_objc_identifier_list (parser);
23541       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23542     }
23543
23544   return protorefs;
23545 }
23546
23547 /* Parse a Objective-C visibility specification.  */
23548
23549 static void
23550 cp_parser_objc_visibility_spec (cp_parser* parser)
23551 {
23552   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23553
23554   switch (vis->keyword)
23555     {
23556     case RID_AT_PRIVATE:
23557       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23558       break;
23559     case RID_AT_PROTECTED:
23560       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23561       break;
23562     case RID_AT_PUBLIC:
23563       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23564       break;
23565     case RID_AT_PACKAGE:
23566       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23567       break;
23568     default:
23569       return;
23570     }
23571
23572   /* Eat '@private'/'@protected'/'@public'.  */
23573   cp_lexer_consume_token (parser->lexer);
23574 }
23575
23576 /* Parse an Objective-C method type.  Return 'true' if it is a class
23577    (+) method, and 'false' if it is an instance (-) method.  */
23578
23579 static inline bool
23580 cp_parser_objc_method_type (cp_parser* parser)
23581 {
23582   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23583     return true;
23584   else
23585     return false;
23586 }
23587
23588 /* Parse an Objective-C protocol qualifier.  */
23589
23590 static tree
23591 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23592 {
23593   tree quals = NULL_TREE, node;
23594   cp_token *token = cp_lexer_peek_token (parser->lexer);
23595
23596   node = token->u.value;
23597
23598   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23599          && (node == ridpointers [(int) RID_IN]
23600              || node == ridpointers [(int) RID_OUT]
23601              || node == ridpointers [(int) RID_INOUT]
23602              || node == ridpointers [(int) RID_BYCOPY]
23603              || node == ridpointers [(int) RID_BYREF]
23604              || node == ridpointers [(int) RID_ONEWAY]))
23605     {
23606       quals = tree_cons (NULL_TREE, node, quals);
23607       cp_lexer_consume_token (parser->lexer);
23608       token = cp_lexer_peek_token (parser->lexer);
23609       node = token->u.value;
23610     }
23611
23612   return quals;
23613 }
23614
23615 /* Parse an Objective-C typename.  */
23616
23617 static tree
23618 cp_parser_objc_typename (cp_parser* parser)
23619 {
23620   tree type_name = NULL_TREE;
23621
23622   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23623     {
23624       tree proto_quals, cp_type = NULL_TREE;
23625
23626       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23627       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23628
23629       /* An ObjC type name may consist of just protocol qualifiers, in which
23630          case the type shall default to 'id'.  */
23631       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23632         {
23633           cp_type = cp_parser_type_id (parser);
23634           
23635           /* If the type could not be parsed, an error has already
23636              been produced.  For error recovery, behave as if it had
23637              not been specified, which will use the default type
23638              'id'.  */
23639           if (cp_type == error_mark_node)
23640             {
23641               cp_type = NULL_TREE;
23642               /* We need to skip to the closing parenthesis as
23643                  cp_parser_type_id() does not seem to do it for
23644                  us.  */
23645               cp_parser_skip_to_closing_parenthesis (parser,
23646                                                      /*recovering=*/true,
23647                                                      /*or_comma=*/false,
23648                                                      /*consume_paren=*/false);
23649             }
23650         }
23651
23652       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23653       type_name = build_tree_list (proto_quals, cp_type);
23654     }
23655
23656   return type_name;
23657 }
23658
23659 /* Check to see if TYPE refers to an Objective-C selector name.  */
23660
23661 static bool
23662 cp_parser_objc_selector_p (enum cpp_ttype type)
23663 {
23664   return (type == CPP_NAME || type == CPP_KEYWORD
23665           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23666           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23667           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23668           || type == CPP_XOR || type == CPP_XOR_EQ);
23669 }
23670
23671 /* Parse an Objective-C selector.  */
23672
23673 static tree
23674 cp_parser_objc_selector (cp_parser* parser)
23675 {
23676   cp_token *token = cp_lexer_consume_token (parser->lexer);
23677
23678   if (!cp_parser_objc_selector_p (token->type))
23679     {
23680       error_at (token->location, "invalid Objective-C++ selector name");
23681       return error_mark_node;
23682     }
23683
23684   /* C++ operator names are allowed to appear in ObjC selectors.  */
23685   switch (token->type)
23686     {
23687     case CPP_AND_AND: return get_identifier ("and");
23688     case CPP_AND_EQ: return get_identifier ("and_eq");
23689     case CPP_AND: return get_identifier ("bitand");
23690     case CPP_OR: return get_identifier ("bitor");
23691     case CPP_COMPL: return get_identifier ("compl");
23692     case CPP_NOT: return get_identifier ("not");
23693     case CPP_NOT_EQ: return get_identifier ("not_eq");
23694     case CPP_OR_OR: return get_identifier ("or");
23695     case CPP_OR_EQ: return get_identifier ("or_eq");
23696     case CPP_XOR: return get_identifier ("xor");
23697     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23698     default: return token->u.value;
23699     }
23700 }
23701
23702 /* Parse an Objective-C params list.  */
23703
23704 static tree
23705 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23706 {
23707   tree params = NULL_TREE;
23708   bool maybe_unary_selector_p = true;
23709   cp_token *token = cp_lexer_peek_token (parser->lexer);
23710
23711   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23712     {
23713       tree selector = NULL_TREE, type_name, identifier;
23714       tree parm_attr = NULL_TREE;
23715
23716       if (token->keyword == RID_ATTRIBUTE)
23717         break;
23718
23719       if (token->type != CPP_COLON)
23720         selector = cp_parser_objc_selector (parser);
23721
23722       /* Detect if we have a unary selector.  */
23723       if (maybe_unary_selector_p
23724           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23725         {
23726           params = selector; /* Might be followed by attributes.  */
23727           break;
23728         }
23729
23730       maybe_unary_selector_p = false;
23731       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23732         {
23733           /* Something went quite wrong.  There should be a colon
23734              here, but there is not.  Stop parsing parameters.  */
23735           break;
23736         }
23737       type_name = cp_parser_objc_typename (parser);
23738       /* New ObjC allows attributes on parameters too.  */
23739       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23740         parm_attr = cp_parser_attributes_opt (parser);
23741       identifier = cp_parser_identifier (parser);
23742
23743       params
23744         = chainon (params,
23745                    objc_build_keyword_decl (selector,
23746                                             type_name,
23747                                             identifier,
23748                                             parm_attr));
23749
23750       token = cp_lexer_peek_token (parser->lexer);
23751     }
23752
23753   if (params == NULL_TREE)
23754     {
23755       cp_parser_error (parser, "objective-c++ method declaration is expected");
23756       return error_mark_node;
23757     }
23758
23759   /* We allow tail attributes for the method.  */
23760   if (token->keyword == RID_ATTRIBUTE)
23761     {
23762       *attributes = cp_parser_attributes_opt (parser);
23763       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23764           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23765         return params;
23766       cp_parser_error (parser, 
23767                        "method attributes must be specified at the end");
23768       return error_mark_node;
23769     }
23770
23771   if (params == NULL_TREE)
23772     {
23773       cp_parser_error (parser, "objective-c++ method declaration is expected");
23774       return error_mark_node;
23775     }
23776   return params;
23777 }
23778
23779 /* Parse the non-keyword Objective-C params.  */
23780
23781 static tree
23782 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23783                                        tree* attributes)
23784 {
23785   tree params = make_node (TREE_LIST);
23786   cp_token *token = cp_lexer_peek_token (parser->lexer);
23787   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23788
23789   while (token->type == CPP_COMMA)
23790     {
23791       cp_parameter_declarator *parmdecl;
23792       tree parm;
23793
23794       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23795       token = cp_lexer_peek_token (parser->lexer);
23796
23797       if (token->type == CPP_ELLIPSIS)
23798         {
23799           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23800           *ellipsisp = true;
23801           token = cp_lexer_peek_token (parser->lexer);
23802           break;
23803         }
23804
23805       /* TODO: parse attributes for tail parameters.  */
23806       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23807       parm = grokdeclarator (parmdecl->declarator,
23808                              &parmdecl->decl_specifiers,
23809                              PARM, /*initialized=*/0,
23810                              /*attrlist=*/NULL);
23811
23812       chainon (params, build_tree_list (NULL_TREE, parm));
23813       token = cp_lexer_peek_token (parser->lexer);
23814     }
23815
23816   /* We allow tail attributes for the method.  */
23817   if (token->keyword == RID_ATTRIBUTE)
23818     {
23819       if (*attributes == NULL_TREE)
23820         {
23821           *attributes = cp_parser_attributes_opt (parser);
23822           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23823               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23824             return params;
23825         }
23826       else        
23827         /* We have an error, but parse the attributes, so that we can 
23828            carry on.  */
23829         *attributes = cp_parser_attributes_opt (parser);
23830
23831       cp_parser_error (parser, 
23832                        "method attributes must be specified at the end");
23833       return error_mark_node;
23834     }
23835
23836   return params;
23837 }
23838
23839 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23840
23841 static void
23842 cp_parser_objc_interstitial_code (cp_parser* parser)
23843 {
23844   cp_token *token = cp_lexer_peek_token (parser->lexer);
23845
23846   /* If the next token is `extern' and the following token is a string
23847      literal, then we have a linkage specification.  */
23848   if (token->keyword == RID_EXTERN
23849       && cp_parser_is_pure_string_literal
23850          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23851     cp_parser_linkage_specification (parser);
23852   /* Handle #pragma, if any.  */
23853   else if (token->type == CPP_PRAGMA)
23854     cp_parser_pragma (parser, pragma_external);
23855   /* Allow stray semicolons.  */
23856   else if (token->type == CPP_SEMICOLON)
23857     cp_lexer_consume_token (parser->lexer);
23858   /* Mark methods as optional or required, when building protocols.  */
23859   else if (token->keyword == RID_AT_OPTIONAL)
23860     {
23861       cp_lexer_consume_token (parser->lexer);
23862       objc_set_method_opt (true);
23863     }
23864   else if (token->keyword == RID_AT_REQUIRED)
23865     {
23866       cp_lexer_consume_token (parser->lexer);
23867       objc_set_method_opt (false);
23868     }
23869   else if (token->keyword == RID_NAMESPACE)
23870     cp_parser_namespace_definition (parser);
23871   /* Other stray characters must generate errors.  */
23872   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23873     {
23874       cp_lexer_consume_token (parser->lexer);
23875       error ("stray %qs between Objective-C++ methods",
23876              token->type == CPP_OPEN_BRACE ? "{" : "}");
23877     }
23878   /* Finally, try to parse a block-declaration, or a function-definition.  */
23879   else
23880     cp_parser_block_declaration (parser, /*statement_p=*/false);
23881 }
23882
23883 /* Parse a method signature.  */
23884
23885 static tree
23886 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23887 {
23888   tree rettype, kwdparms, optparms;
23889   bool ellipsis = false;
23890   bool is_class_method;
23891
23892   is_class_method = cp_parser_objc_method_type (parser);
23893   rettype = cp_parser_objc_typename (parser);
23894   *attributes = NULL_TREE;
23895   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23896   if (kwdparms == error_mark_node)
23897     return error_mark_node;
23898   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23899   if (optparms == error_mark_node)
23900     return error_mark_node;
23901
23902   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23903 }
23904
23905 static bool
23906 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23907 {
23908   tree tattr;  
23909   cp_lexer_save_tokens (parser->lexer);
23910   tattr = cp_parser_attributes_opt (parser);
23911   gcc_assert (tattr) ;
23912   
23913   /* If the attributes are followed by a method introducer, this is not allowed.
23914      Dump the attributes and flag the situation.  */
23915   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23916       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23917     return true;
23918
23919   /* Otherwise, the attributes introduce some interstitial code, possibly so
23920      rewind to allow that check.  */
23921   cp_lexer_rollback_tokens (parser->lexer);
23922   return false;  
23923 }
23924
23925 /* Parse an Objective-C method prototype list.  */
23926
23927 static void
23928 cp_parser_objc_method_prototype_list (cp_parser* parser)
23929 {
23930   cp_token *token = cp_lexer_peek_token (parser->lexer);
23931
23932   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23933     {
23934       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23935         {
23936           tree attributes, sig;
23937           bool is_class_method;
23938           if (token->type == CPP_PLUS)
23939             is_class_method = true;
23940           else
23941             is_class_method = false;
23942           sig = cp_parser_objc_method_signature (parser, &attributes);
23943           if (sig == error_mark_node)
23944             {
23945               cp_parser_skip_to_end_of_block_or_statement (parser);
23946               token = cp_lexer_peek_token (parser->lexer);
23947               continue;
23948             }
23949           objc_add_method_declaration (is_class_method, sig, attributes);
23950           cp_parser_consume_semicolon_at_end_of_statement (parser);
23951         }
23952       else if (token->keyword == RID_AT_PROPERTY)
23953         cp_parser_objc_at_property_declaration (parser);
23954       else if (token->keyword == RID_ATTRIBUTE 
23955                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23956         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23957                     OPT_Wattributes, 
23958                     "prefix attributes are ignored for methods");
23959       else
23960         /* Allow for interspersed non-ObjC++ code.  */
23961         cp_parser_objc_interstitial_code (parser);
23962
23963       token = cp_lexer_peek_token (parser->lexer);
23964     }
23965
23966   if (token->type != CPP_EOF)
23967     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23968   else
23969     cp_parser_error (parser, "expected %<@end%>");
23970
23971   objc_finish_interface ();
23972 }
23973
23974 /* Parse an Objective-C method definition list.  */
23975
23976 static void
23977 cp_parser_objc_method_definition_list (cp_parser* parser)
23978 {
23979   cp_token *token = cp_lexer_peek_token (parser->lexer);
23980
23981   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23982     {
23983       tree meth;
23984
23985       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23986         {
23987           cp_token *ptk;
23988           tree sig, attribute;
23989           bool is_class_method;
23990           if (token->type == CPP_PLUS)
23991             is_class_method = true;
23992           else
23993             is_class_method = false;
23994           push_deferring_access_checks (dk_deferred);
23995           sig = cp_parser_objc_method_signature (parser, &attribute);
23996           if (sig == error_mark_node)
23997             {
23998               cp_parser_skip_to_end_of_block_or_statement (parser);
23999               token = cp_lexer_peek_token (parser->lexer);
24000               continue;
24001             }
24002           objc_start_method_definition (is_class_method, sig, attribute,
24003                                         NULL_TREE);
24004
24005           /* For historical reasons, we accept an optional semicolon.  */
24006           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24007             cp_lexer_consume_token (parser->lexer);
24008
24009           ptk = cp_lexer_peek_token (parser->lexer);
24010           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
24011                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24012             {
24013               perform_deferred_access_checks ();
24014               stop_deferring_access_checks ();
24015               meth = cp_parser_function_definition_after_declarator (parser,
24016                                                                      false);
24017               pop_deferring_access_checks ();
24018               objc_finish_method_definition (meth);
24019             }
24020         }
24021       /* The following case will be removed once @synthesize is
24022          completely implemented.  */
24023       else if (token->keyword == RID_AT_PROPERTY)
24024         cp_parser_objc_at_property_declaration (parser);
24025       else if (token->keyword == RID_AT_SYNTHESIZE)
24026         cp_parser_objc_at_synthesize_declaration (parser);
24027       else if (token->keyword == RID_AT_DYNAMIC)
24028         cp_parser_objc_at_dynamic_declaration (parser);
24029       else if (token->keyword == RID_ATTRIBUTE 
24030                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24031         warning_at (token->location, OPT_Wattributes,
24032                     "prefix attributes are ignored for methods");
24033       else
24034         /* Allow for interspersed non-ObjC++ code.  */
24035         cp_parser_objc_interstitial_code (parser);
24036
24037       token = cp_lexer_peek_token (parser->lexer);
24038     }
24039
24040   if (token->type != CPP_EOF)
24041     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24042   else
24043     cp_parser_error (parser, "expected %<@end%>");
24044
24045   objc_finish_implementation ();
24046 }
24047
24048 /* Parse Objective-C ivars.  */
24049
24050 static void
24051 cp_parser_objc_class_ivars (cp_parser* parser)
24052 {
24053   cp_token *token = cp_lexer_peek_token (parser->lexer);
24054
24055   if (token->type != CPP_OPEN_BRACE)
24056     return;     /* No ivars specified.  */
24057
24058   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
24059   token = cp_lexer_peek_token (parser->lexer);
24060
24061   while (token->type != CPP_CLOSE_BRACE 
24062         && token->keyword != RID_AT_END && token->type != CPP_EOF)
24063     {
24064       cp_decl_specifier_seq declspecs;
24065       int decl_class_or_enum_p;
24066       tree prefix_attributes;
24067
24068       cp_parser_objc_visibility_spec (parser);
24069
24070       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24071         break;
24072
24073       cp_parser_decl_specifier_seq (parser,
24074                                     CP_PARSER_FLAGS_OPTIONAL,
24075                                     &declspecs,
24076                                     &decl_class_or_enum_p);
24077
24078       /* auto, register, static, extern, mutable.  */
24079       if (declspecs.storage_class != sc_none)
24080         {
24081           cp_parser_error (parser, "invalid type for instance variable");         
24082           declspecs.storage_class = sc_none;
24083         }
24084
24085       /* __thread.  */
24086       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24087         {
24088           cp_parser_error (parser, "invalid type for instance variable");
24089           declspecs.locations[ds_thread] = 0;
24090         }
24091       
24092       /* typedef.  */
24093       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24094         {
24095           cp_parser_error (parser, "invalid type for instance variable");
24096           declspecs.locations[ds_thread] = 0;
24097         }
24098
24099       prefix_attributes = declspecs.attributes;
24100       declspecs.attributes = NULL_TREE;
24101
24102       /* Keep going until we hit the `;' at the end of the
24103          declaration.  */
24104       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24105         {
24106           tree width = NULL_TREE, attributes, first_attribute, decl;
24107           cp_declarator *declarator = NULL;
24108           int ctor_dtor_or_conv_p;
24109
24110           /* Check for a (possibly unnamed) bitfield declaration.  */
24111           token = cp_lexer_peek_token (parser->lexer);
24112           if (token->type == CPP_COLON)
24113             goto eat_colon;
24114
24115           if (token->type == CPP_NAME
24116               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24117                   == CPP_COLON))
24118             {
24119               /* Get the name of the bitfield.  */
24120               declarator = make_id_declarator (NULL_TREE,
24121                                                cp_parser_identifier (parser),
24122                                                sfk_none);
24123
24124              eat_colon:
24125               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24126               /* Get the width of the bitfield.  */
24127               width
24128                 = cp_parser_constant_expression (parser,
24129                                                  /*allow_non_constant=*/false,
24130                                                  NULL);
24131             }
24132           else
24133             {
24134               /* Parse the declarator.  */
24135               declarator
24136                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24137                                         &ctor_dtor_or_conv_p,
24138                                         /*parenthesized_p=*/NULL,
24139                                         /*member_p=*/false);
24140             }
24141
24142           /* Look for attributes that apply to the ivar.  */
24143           attributes = cp_parser_attributes_opt (parser);
24144           /* Remember which attributes are prefix attributes and
24145              which are not.  */
24146           first_attribute = attributes;
24147           /* Combine the attributes.  */
24148           attributes = chainon (prefix_attributes, attributes);
24149
24150           if (width)
24151               /* Create the bitfield declaration.  */
24152               decl = grokbitfield (declarator, &declspecs,
24153                                    width,
24154                                    attributes);
24155           else
24156             decl = grokfield (declarator, &declspecs,
24157                               NULL_TREE, /*init_const_expr_p=*/false,
24158                               NULL_TREE, attributes);
24159
24160           /* Add the instance variable.  */
24161           if (decl != error_mark_node && decl != NULL_TREE)
24162             objc_add_instance_variable (decl);
24163
24164           /* Reset PREFIX_ATTRIBUTES.  */
24165           while (attributes && TREE_CHAIN (attributes) != first_attribute)
24166             attributes = TREE_CHAIN (attributes);
24167           if (attributes)
24168             TREE_CHAIN (attributes) = NULL_TREE;
24169
24170           token = cp_lexer_peek_token (parser->lexer);
24171
24172           if (token->type == CPP_COMMA)
24173             {
24174               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24175               continue;
24176             }
24177           break;
24178         }
24179
24180       cp_parser_consume_semicolon_at_end_of_statement (parser);
24181       token = cp_lexer_peek_token (parser->lexer);
24182     }
24183
24184   if (token->keyword == RID_AT_END)
24185     cp_parser_error (parser, "expected %<}%>");
24186
24187   /* Do not consume the RID_AT_END, so it will be read again as terminating
24188      the @interface of @implementation.  */ 
24189   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24190     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24191     
24192   /* For historical reasons, we accept an optional semicolon.  */
24193   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24194     cp_lexer_consume_token (parser->lexer);
24195 }
24196
24197 /* Parse an Objective-C protocol declaration.  */
24198
24199 static void
24200 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24201 {
24202   tree proto, protorefs;
24203   cp_token *tok;
24204
24205   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24206   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24207     {
24208       tok = cp_lexer_peek_token (parser->lexer);
24209       error_at (tok->location, "identifier expected after %<@protocol%>");
24210       cp_parser_consume_semicolon_at_end_of_statement (parser);
24211       return;
24212     }
24213
24214   /* See if we have a forward declaration or a definition.  */
24215   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24216
24217   /* Try a forward declaration first.  */
24218   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24219     {
24220       while (true)
24221         {
24222           tree id;
24223           
24224           id = cp_parser_identifier (parser);
24225           if (id == error_mark_node)
24226             break;
24227           
24228           objc_declare_protocol (id, attributes);
24229           
24230           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24231             cp_lexer_consume_token (parser->lexer);
24232           else
24233             break;
24234         }
24235       cp_parser_consume_semicolon_at_end_of_statement (parser);
24236     }
24237
24238   /* Ok, we got a full-fledged definition (or at least should).  */
24239   else
24240     {
24241       proto = cp_parser_identifier (parser);
24242       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24243       objc_start_protocol (proto, protorefs, attributes);
24244       cp_parser_objc_method_prototype_list (parser);
24245     }
24246 }
24247
24248 /* Parse an Objective-C superclass or category.  */
24249
24250 static void
24251 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24252                                        bool iface_p,
24253                                        tree *super,
24254                                        tree *categ, bool *is_class_extension)
24255 {
24256   cp_token *next = cp_lexer_peek_token (parser->lexer);
24257
24258   *super = *categ = NULL_TREE;
24259   *is_class_extension = false;
24260   if (next->type == CPP_COLON)
24261     {
24262       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24263       *super = cp_parser_identifier (parser);
24264     }
24265   else if (next->type == CPP_OPEN_PAREN)
24266     {
24267       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24268
24269       /* If there is no category name, and this is an @interface, we
24270          have a class extension.  */
24271       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24272         {
24273           *categ = NULL_TREE;
24274           *is_class_extension = true;
24275         }
24276       else
24277         *categ = cp_parser_identifier (parser);
24278
24279       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24280     }
24281 }
24282
24283 /* Parse an Objective-C class interface.  */
24284
24285 static void
24286 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24287 {
24288   tree name, super, categ, protos;
24289   bool is_class_extension;
24290
24291   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24292   name = cp_parser_identifier (parser);
24293   if (name == error_mark_node)
24294     {
24295       /* It's hard to recover because even if valid @interface stuff
24296          is to follow, we can't compile it (or validate it) if we
24297          don't even know which class it refers to.  Let's assume this
24298          was a stray '@interface' token in the stream and skip it.
24299       */
24300       return;
24301     }
24302   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24303                                          &is_class_extension);
24304   protos = cp_parser_objc_protocol_refs_opt (parser);
24305
24306   /* We have either a class or a category on our hands.  */
24307   if (categ || is_class_extension)
24308     objc_start_category_interface (name, categ, protos, attributes);
24309   else
24310     {
24311       objc_start_class_interface (name, super, protos, attributes);
24312       /* Handle instance variable declarations, if any.  */
24313       cp_parser_objc_class_ivars (parser);
24314       objc_continue_interface ();
24315     }
24316
24317   cp_parser_objc_method_prototype_list (parser);
24318 }
24319
24320 /* Parse an Objective-C class implementation.  */
24321
24322 static void
24323 cp_parser_objc_class_implementation (cp_parser* parser)
24324 {
24325   tree name, super, categ;
24326   bool is_class_extension;
24327
24328   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24329   name = cp_parser_identifier (parser);
24330   if (name == error_mark_node)
24331     {
24332       /* It's hard to recover because even if valid @implementation
24333          stuff is to follow, we can't compile it (or validate it) if
24334          we don't even know which class it refers to.  Let's assume
24335          this was a stray '@implementation' token in the stream and
24336          skip it.
24337       */
24338       return;
24339     }
24340   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24341                                          &is_class_extension);
24342
24343   /* We have either a class or a category on our hands.  */
24344   if (categ)
24345     objc_start_category_implementation (name, categ);
24346   else
24347     {
24348       objc_start_class_implementation (name, super);
24349       /* Handle instance variable declarations, if any.  */
24350       cp_parser_objc_class_ivars (parser);
24351       objc_continue_implementation ();
24352     }
24353
24354   cp_parser_objc_method_definition_list (parser);
24355 }
24356
24357 /* Consume the @end token and finish off the implementation.  */
24358
24359 static void
24360 cp_parser_objc_end_implementation (cp_parser* parser)
24361 {
24362   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24363   objc_finish_implementation ();
24364 }
24365
24366 /* Parse an Objective-C declaration.  */
24367
24368 static void
24369 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24370 {
24371   /* Try to figure out what kind of declaration is present.  */
24372   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24373
24374   if (attributes)
24375     switch (kwd->keyword)
24376       {
24377         case RID_AT_ALIAS:
24378         case RID_AT_CLASS:
24379         case RID_AT_END:
24380           error_at (kwd->location, "attributes may not be specified before"
24381                     " the %<@%D%> Objective-C++ keyword",
24382                     kwd->u.value);
24383           attributes = NULL;
24384           break;
24385         case RID_AT_IMPLEMENTATION:
24386           warning_at (kwd->location, OPT_Wattributes,
24387                       "prefix attributes are ignored before %<@%D%>",
24388                       kwd->u.value);
24389           attributes = NULL;
24390         default:
24391           break;
24392       }
24393
24394   switch (kwd->keyword)
24395     {
24396     case RID_AT_ALIAS:
24397       cp_parser_objc_alias_declaration (parser);
24398       break;
24399     case RID_AT_CLASS:
24400       cp_parser_objc_class_declaration (parser);
24401       break;
24402     case RID_AT_PROTOCOL:
24403       cp_parser_objc_protocol_declaration (parser, attributes);
24404       break;
24405     case RID_AT_INTERFACE:
24406       cp_parser_objc_class_interface (parser, attributes);
24407       break;
24408     case RID_AT_IMPLEMENTATION:
24409       cp_parser_objc_class_implementation (parser);
24410       break;
24411     case RID_AT_END:
24412       cp_parser_objc_end_implementation (parser);
24413       break;
24414     default:
24415       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24416                 kwd->u.value);
24417       cp_parser_skip_to_end_of_block_or_statement (parser);
24418     }
24419 }
24420
24421 /* Parse an Objective-C try-catch-finally statement.
24422
24423    objc-try-catch-finally-stmt:
24424      @try compound-statement objc-catch-clause-seq [opt]
24425        objc-finally-clause [opt]
24426
24427    objc-catch-clause-seq:
24428      objc-catch-clause objc-catch-clause-seq [opt]
24429
24430    objc-catch-clause:
24431      @catch ( objc-exception-declaration ) compound-statement
24432
24433    objc-finally-clause:
24434      @finally compound-statement
24435
24436    objc-exception-declaration:
24437      parameter-declaration
24438      '...'
24439
24440    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24441
24442    Returns NULL_TREE.
24443
24444    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24445    for C.  Keep them in sync.  */   
24446
24447 static tree
24448 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24449 {
24450   location_t location;
24451   tree stmt;
24452
24453   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24454   location = cp_lexer_peek_token (parser->lexer)->location;
24455   objc_maybe_warn_exceptions (location);
24456   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24457      node, lest it get absorbed into the surrounding block.  */
24458   stmt = push_stmt_list ();
24459   cp_parser_compound_statement (parser, NULL, false, false);
24460   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24461
24462   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24463     {
24464       cp_parameter_declarator *parm;
24465       tree parameter_declaration = error_mark_node;
24466       bool seen_open_paren = false;
24467
24468       cp_lexer_consume_token (parser->lexer);
24469       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24470         seen_open_paren = true;
24471       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24472         {
24473           /* We have "@catch (...)" (where the '...' are literally
24474              what is in the code).  Skip the '...'.
24475              parameter_declaration is set to NULL_TREE, and
24476              objc_being_catch_clauses() knows that that means
24477              '...'.  */
24478           cp_lexer_consume_token (parser->lexer);
24479           parameter_declaration = NULL_TREE;
24480         }
24481       else
24482         {
24483           /* We have "@catch (NSException *exception)" or something
24484              like that.  Parse the parameter declaration.  */
24485           parm = cp_parser_parameter_declaration (parser, false, NULL);
24486           if (parm == NULL)
24487             parameter_declaration = error_mark_node;
24488           else
24489             parameter_declaration = grokdeclarator (parm->declarator,
24490                                                     &parm->decl_specifiers,
24491                                                     PARM, /*initialized=*/0,
24492                                                     /*attrlist=*/NULL);
24493         }
24494       if (seen_open_paren)
24495         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24496       else
24497         {
24498           /* If there was no open parenthesis, we are recovering from
24499              an error, and we are trying to figure out what mistake
24500              the user has made.  */
24501
24502           /* If there is an immediate closing parenthesis, the user
24503              probably forgot the opening one (ie, they typed "@catch
24504              NSException *e)".  Parse the closing parenthesis and keep
24505              going.  */
24506           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24507             cp_lexer_consume_token (parser->lexer);
24508           
24509           /* If these is no immediate closing parenthesis, the user
24510              probably doesn't know that parenthesis are required at
24511              all (ie, they typed "@catch NSException *e").  So, just
24512              forget about the closing parenthesis and keep going.  */
24513         }
24514       objc_begin_catch_clause (parameter_declaration);
24515       cp_parser_compound_statement (parser, NULL, false, false);
24516       objc_finish_catch_clause ();
24517     }
24518   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24519     {
24520       cp_lexer_consume_token (parser->lexer);
24521       location = cp_lexer_peek_token (parser->lexer)->location;
24522       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24523          node, lest it get absorbed into the surrounding block.  */
24524       stmt = push_stmt_list ();
24525       cp_parser_compound_statement (parser, NULL, false, false);
24526       objc_build_finally_clause (location, pop_stmt_list (stmt));
24527     }
24528
24529   return objc_finish_try_stmt ();
24530 }
24531
24532 /* Parse an Objective-C synchronized statement.
24533
24534    objc-synchronized-stmt:
24535      @synchronized ( expression ) compound-statement
24536
24537    Returns NULL_TREE.  */
24538
24539 static tree
24540 cp_parser_objc_synchronized_statement (cp_parser *parser)
24541 {
24542   location_t location;
24543   tree lock, stmt;
24544
24545   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24546
24547   location = cp_lexer_peek_token (parser->lexer)->location;
24548   objc_maybe_warn_exceptions (location);
24549   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24550   lock = cp_parser_expression (parser, false, NULL);
24551   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24552
24553   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24554      node, lest it get absorbed into the surrounding block.  */
24555   stmt = push_stmt_list ();
24556   cp_parser_compound_statement (parser, NULL, false, false);
24557
24558   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24559 }
24560
24561 /* Parse an Objective-C throw statement.
24562
24563    objc-throw-stmt:
24564      @throw assignment-expression [opt] ;
24565
24566    Returns a constructed '@throw' statement.  */
24567
24568 static tree
24569 cp_parser_objc_throw_statement (cp_parser *parser)
24570 {
24571   tree expr = NULL_TREE;
24572   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24573
24574   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24575
24576   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24577     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24578
24579   cp_parser_consume_semicolon_at_end_of_statement (parser);
24580
24581   return objc_build_throw_stmt (loc, expr);
24582 }
24583
24584 /* Parse an Objective-C statement.  */
24585
24586 static tree
24587 cp_parser_objc_statement (cp_parser * parser)
24588 {
24589   /* Try to figure out what kind of declaration is present.  */
24590   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24591
24592   switch (kwd->keyword)
24593     {
24594     case RID_AT_TRY:
24595       return cp_parser_objc_try_catch_finally_statement (parser);
24596     case RID_AT_SYNCHRONIZED:
24597       return cp_parser_objc_synchronized_statement (parser);
24598     case RID_AT_THROW:
24599       return cp_parser_objc_throw_statement (parser);
24600     default:
24601       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24602                kwd->u.value);
24603       cp_parser_skip_to_end_of_block_or_statement (parser);
24604     }
24605
24606   return error_mark_node;
24607 }
24608
24609 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24610    look ahead to see if an objc keyword follows the attributes.  This
24611    is to detect the use of prefix attributes on ObjC @interface and 
24612    @protocol.  */
24613
24614 static bool
24615 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24616 {
24617   cp_lexer_save_tokens (parser->lexer);
24618   *attrib = cp_parser_attributes_opt (parser);
24619   gcc_assert (*attrib);
24620   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24621     {
24622       cp_lexer_commit_tokens (parser->lexer);
24623       return true;
24624     }
24625   cp_lexer_rollback_tokens (parser->lexer);
24626   return false;  
24627 }
24628
24629 /* This routine is a minimal replacement for
24630    c_parser_struct_declaration () used when parsing the list of
24631    types/names or ObjC++ properties.  For example, when parsing the
24632    code
24633
24634    @property (readonly) int a, b, c;
24635
24636    this function is responsible for parsing "int a, int b, int c" and
24637    returning the declarations as CHAIN of DECLs.
24638
24639    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24640    similar parsing.  */
24641 static tree
24642 cp_parser_objc_struct_declaration (cp_parser *parser)
24643 {
24644   tree decls = NULL_TREE;
24645   cp_decl_specifier_seq declspecs;
24646   int decl_class_or_enum_p;
24647   tree prefix_attributes;
24648
24649   cp_parser_decl_specifier_seq (parser,
24650                                 CP_PARSER_FLAGS_NONE,
24651                                 &declspecs,
24652                                 &decl_class_or_enum_p);
24653
24654   if (declspecs.type == error_mark_node)
24655     return error_mark_node;
24656
24657   /* auto, register, static, extern, mutable.  */
24658   if (declspecs.storage_class != sc_none)
24659     {
24660       cp_parser_error (parser, "invalid type for property");
24661       declspecs.storage_class = sc_none;
24662     }
24663   
24664   /* __thread.  */
24665   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24666     {
24667       cp_parser_error (parser, "invalid type for property");
24668       declspecs.locations[ds_thread] = 0;
24669     }
24670   
24671   /* typedef.  */
24672   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24673     {
24674       cp_parser_error (parser, "invalid type for property");
24675       declspecs.locations[ds_typedef] = 0;
24676     }
24677
24678   prefix_attributes = declspecs.attributes;
24679   declspecs.attributes = NULL_TREE;
24680
24681   /* Keep going until we hit the `;' at the end of the declaration. */
24682   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24683     {
24684       tree attributes, first_attribute, decl;
24685       cp_declarator *declarator;
24686       cp_token *token;
24687
24688       /* Parse the declarator.  */
24689       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24690                                          NULL, NULL, false);
24691
24692       /* Look for attributes that apply to the ivar.  */
24693       attributes = cp_parser_attributes_opt (parser);
24694       /* Remember which attributes are prefix attributes and
24695          which are not.  */
24696       first_attribute = attributes;
24697       /* Combine the attributes.  */
24698       attributes = chainon (prefix_attributes, attributes);
24699       
24700       decl = grokfield (declarator, &declspecs,
24701                         NULL_TREE, /*init_const_expr_p=*/false,
24702                         NULL_TREE, attributes);
24703
24704       if (decl == error_mark_node || decl == NULL_TREE)
24705         return error_mark_node;
24706       
24707       /* Reset PREFIX_ATTRIBUTES.  */
24708       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24709         attributes = TREE_CHAIN (attributes);
24710       if (attributes)
24711         TREE_CHAIN (attributes) = NULL_TREE;
24712
24713       DECL_CHAIN (decl) = decls;
24714       decls = decl;
24715
24716       token = cp_lexer_peek_token (parser->lexer);
24717       if (token->type == CPP_COMMA)
24718         {
24719           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24720           continue;
24721         }
24722       else
24723         break;
24724     }
24725   return decls;
24726 }
24727
24728 /* Parse an Objective-C @property declaration.  The syntax is:
24729
24730    objc-property-declaration:
24731      '@property' objc-property-attributes[opt] struct-declaration ;
24732
24733    objc-property-attributes:
24734     '(' objc-property-attribute-list ')'
24735
24736    objc-property-attribute-list:
24737      objc-property-attribute
24738      objc-property-attribute-list, objc-property-attribute
24739
24740    objc-property-attribute
24741      'getter' = identifier
24742      'setter' = identifier
24743      'readonly'
24744      'readwrite'
24745      'assign'
24746      'retain'
24747      'copy'
24748      'nonatomic'
24749
24750   For example:
24751     @property NSString *name;
24752     @property (readonly) id object;
24753     @property (retain, nonatomic, getter=getTheName) id name;
24754     @property int a, b, c;
24755
24756    PS: This function is identical to
24757    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24758 static void 
24759 cp_parser_objc_at_property_declaration (cp_parser *parser)
24760 {
24761   /* The following variables hold the attributes of the properties as
24762      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24763      seen.  When we see an attribute, we set them to 'true' (if they
24764      are boolean properties) or to the identifier (if they have an
24765      argument, ie, for getter and setter).  Note that here we only
24766      parse the list of attributes, check the syntax and accumulate the
24767      attributes that we find.  objc_add_property_declaration() will
24768      then process the information.  */
24769   bool property_assign = false;
24770   bool property_copy = false;
24771   tree property_getter_ident = NULL_TREE;
24772   bool property_nonatomic = false;
24773   bool property_readonly = false;
24774   bool property_readwrite = false;
24775   bool property_retain = false;
24776   tree property_setter_ident = NULL_TREE;
24777
24778   /* 'properties' is the list of properties that we read.  Usually a
24779      single one, but maybe more (eg, in "@property int a, b, c;" there
24780      are three).  */
24781   tree properties;
24782   location_t loc;
24783
24784   loc = cp_lexer_peek_token (parser->lexer)->location;
24785
24786   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24787
24788   /* Parse the optional attribute list...  */
24789   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24790     {
24791       /* Eat the '('.  */
24792       cp_lexer_consume_token (parser->lexer);
24793
24794       while (true)
24795         {
24796           bool syntax_error = false;
24797           cp_token *token = cp_lexer_peek_token (parser->lexer);
24798           enum rid keyword;
24799
24800           if (token->type != CPP_NAME)
24801             {
24802               cp_parser_error (parser, "expected identifier");
24803               break;
24804             }
24805           keyword = C_RID_CODE (token->u.value);
24806           cp_lexer_consume_token (parser->lexer);
24807           switch (keyword)
24808             {
24809             case RID_ASSIGN:    property_assign = true;    break;
24810             case RID_COPY:      property_copy = true;      break;
24811             case RID_NONATOMIC: property_nonatomic = true; break;
24812             case RID_READONLY:  property_readonly = true;  break;
24813             case RID_READWRITE: property_readwrite = true; break;
24814             case RID_RETAIN:    property_retain = true;    break;
24815
24816             case RID_GETTER:
24817             case RID_SETTER:
24818               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24819                 {
24820                   if (keyword == RID_GETTER)
24821                     cp_parser_error (parser,
24822                                      "missing %<=%> (after %<getter%> attribute)");
24823                   else
24824                     cp_parser_error (parser,
24825                                      "missing %<=%> (after %<setter%> attribute)");
24826                   syntax_error = true;
24827                   break;
24828                 }
24829               cp_lexer_consume_token (parser->lexer); /* eat the = */
24830               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24831                 {
24832                   cp_parser_error (parser, "expected identifier");
24833                   syntax_error = true;
24834                   break;
24835                 }
24836               if (keyword == RID_SETTER)
24837                 {
24838                   if (property_setter_ident != NULL_TREE)
24839                     {
24840                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24841                       cp_lexer_consume_token (parser->lexer);
24842                     }
24843                   else
24844                     property_setter_ident = cp_parser_objc_selector (parser);
24845                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24846                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24847                   else
24848                     cp_lexer_consume_token (parser->lexer);
24849                 }
24850               else
24851                 {
24852                   if (property_getter_ident != NULL_TREE)
24853                     {
24854                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24855                       cp_lexer_consume_token (parser->lexer);
24856                     }
24857                   else
24858                     property_getter_ident = cp_parser_objc_selector (parser);
24859                 }
24860               break;
24861             default:
24862               cp_parser_error (parser, "unknown property attribute");
24863               syntax_error = true;
24864               break;
24865             }
24866
24867           if (syntax_error)
24868             break;
24869
24870           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24871             cp_lexer_consume_token (parser->lexer);
24872           else
24873             break;
24874         }
24875
24876       /* FIXME: "@property (setter, assign);" will generate a spurious
24877          "error: expected â€˜)’ before â€˜,’ token".  This is because
24878          cp_parser_require, unlike the C counterpart, will produce an
24879          error even if we are in error recovery.  */
24880       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24881         {
24882           cp_parser_skip_to_closing_parenthesis (parser,
24883                                                  /*recovering=*/true,
24884                                                  /*or_comma=*/false,
24885                                                  /*consume_paren=*/true);
24886         }
24887     }
24888
24889   /* ... and the property declaration(s).  */
24890   properties = cp_parser_objc_struct_declaration (parser);
24891
24892   if (properties == error_mark_node)
24893     {
24894       cp_parser_skip_to_end_of_statement (parser);
24895       /* If the next token is now a `;', consume it.  */
24896       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24897         cp_lexer_consume_token (parser->lexer);
24898       return;
24899     }
24900
24901   if (properties == NULL_TREE)
24902     cp_parser_error (parser, "expected identifier");
24903   else
24904     {
24905       /* Comma-separated properties are chained together in
24906          reverse order; add them one by one.  */
24907       properties = nreverse (properties);
24908       
24909       for (; properties; properties = TREE_CHAIN (properties))
24910         objc_add_property_declaration (loc, copy_node (properties),
24911                                        property_readonly, property_readwrite,
24912                                        property_assign, property_retain,
24913                                        property_copy, property_nonatomic,
24914                                        property_getter_ident, property_setter_ident);
24915     }
24916   
24917   cp_parser_consume_semicolon_at_end_of_statement (parser);
24918 }
24919
24920 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24921
24922    objc-synthesize-declaration:
24923      @synthesize objc-synthesize-identifier-list ;
24924
24925    objc-synthesize-identifier-list:
24926      objc-synthesize-identifier
24927      objc-synthesize-identifier-list, objc-synthesize-identifier
24928
24929    objc-synthesize-identifier
24930      identifier
24931      identifier = identifier
24932
24933   For example:
24934     @synthesize MyProperty;
24935     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24936
24937   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24938   for C.  Keep them in sync.
24939 */
24940 static void 
24941 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24942 {
24943   tree list = NULL_TREE;
24944   location_t loc;
24945   loc = cp_lexer_peek_token (parser->lexer)->location;
24946
24947   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24948   while (true)
24949     {
24950       tree property, ivar;
24951       property = cp_parser_identifier (parser);
24952       if (property == error_mark_node)
24953         {
24954           cp_parser_consume_semicolon_at_end_of_statement (parser);
24955           return;
24956         }
24957       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24958         {
24959           cp_lexer_consume_token (parser->lexer);
24960           ivar = cp_parser_identifier (parser);
24961           if (ivar == error_mark_node)
24962             {
24963               cp_parser_consume_semicolon_at_end_of_statement (parser);
24964               return;
24965             }
24966         }
24967       else
24968         ivar = NULL_TREE;
24969       list = chainon (list, build_tree_list (ivar, property));
24970       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24971         cp_lexer_consume_token (parser->lexer);
24972       else
24973         break;
24974     }
24975   cp_parser_consume_semicolon_at_end_of_statement (parser);
24976   objc_add_synthesize_declaration (loc, list);
24977 }
24978
24979 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24980
24981    objc-dynamic-declaration:
24982      @dynamic identifier-list ;
24983
24984    For example:
24985      @dynamic MyProperty;
24986      @dynamic MyProperty, AnotherProperty;
24987
24988   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24989   for C.  Keep them in sync.
24990 */
24991 static void 
24992 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24993 {
24994   tree list = NULL_TREE;
24995   location_t loc;
24996   loc = cp_lexer_peek_token (parser->lexer)->location;
24997
24998   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24999   while (true)
25000     {
25001       tree property;
25002       property = cp_parser_identifier (parser);
25003       if (property == error_mark_node)
25004         {
25005           cp_parser_consume_semicolon_at_end_of_statement (parser);
25006           return;
25007         }
25008       list = chainon (list, build_tree_list (NULL, property));
25009       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25010         cp_lexer_consume_token (parser->lexer);
25011       else
25012         break;
25013     }
25014   cp_parser_consume_semicolon_at_end_of_statement (parser);
25015   objc_add_dynamic_declaration (loc, list);
25016 }
25017
25018 \f
25019 /* OpenMP 2.5 parsing routines.  */
25020
25021 /* Returns name of the next clause.
25022    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25023    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
25024    returned and the token is consumed.  */
25025
25026 static pragma_omp_clause
25027 cp_parser_omp_clause_name (cp_parser *parser)
25028 {
25029   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25030
25031   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25032     result = PRAGMA_OMP_CLAUSE_IF;
25033   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25034     result = PRAGMA_OMP_CLAUSE_DEFAULT;
25035   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25036     result = PRAGMA_OMP_CLAUSE_PRIVATE;
25037   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25038     {
25039       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25040       const char *p = IDENTIFIER_POINTER (id);
25041
25042       switch (p[0])
25043         {
25044         case 'c':
25045           if (!strcmp ("collapse", p))
25046             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25047           else if (!strcmp ("copyin", p))
25048             result = PRAGMA_OMP_CLAUSE_COPYIN;
25049           else if (!strcmp ("copyprivate", p))
25050             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25051           break;
25052         case 'f':
25053           if (!strcmp ("final", p))
25054             result = PRAGMA_OMP_CLAUSE_FINAL;
25055           else if (!strcmp ("firstprivate", p))
25056             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25057           break;
25058         case 'l':
25059           if (!strcmp ("lastprivate", p))
25060             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25061           break;
25062         case 'm':
25063           if (!strcmp ("mergeable", p))
25064             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25065           break;
25066         case 'n':
25067           if (!strcmp ("nowait", p))
25068             result = PRAGMA_OMP_CLAUSE_NOWAIT;
25069           else if (!strcmp ("num_threads", p))
25070             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25071           break;
25072         case 'o':
25073           if (!strcmp ("ordered", p))
25074             result = PRAGMA_OMP_CLAUSE_ORDERED;
25075           break;
25076         case 'r':
25077           if (!strcmp ("reduction", p))
25078             result = PRAGMA_OMP_CLAUSE_REDUCTION;
25079           break;
25080         case 's':
25081           if (!strcmp ("schedule", p))
25082             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25083           else if (!strcmp ("shared", p))
25084             result = PRAGMA_OMP_CLAUSE_SHARED;
25085           break;
25086         case 'u':
25087           if (!strcmp ("untied", p))
25088             result = PRAGMA_OMP_CLAUSE_UNTIED;
25089           break;
25090         }
25091     }
25092
25093   if (result != PRAGMA_OMP_CLAUSE_NONE)
25094     cp_lexer_consume_token (parser->lexer);
25095
25096   return result;
25097 }
25098
25099 /* Validate that a clause of the given type does not already exist.  */
25100
25101 static void
25102 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25103                            const char *name, location_t location)
25104 {
25105   tree c;
25106
25107   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25108     if (OMP_CLAUSE_CODE (c) == code)
25109       {
25110         error_at (location, "too many %qs clauses", name);
25111         break;
25112       }
25113 }
25114
25115 /* OpenMP 2.5:
25116    variable-list:
25117      identifier
25118      variable-list , identifier
25119
25120    In addition, we match a closing parenthesis.  An opening parenthesis
25121    will have been consumed by the caller.
25122
25123    If KIND is nonzero, create the appropriate node and install the decl
25124    in OMP_CLAUSE_DECL and add the node to the head of the list.
25125
25126    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25127    return the list created.  */
25128
25129 static tree
25130 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25131                                 tree list)
25132 {
25133   cp_token *token;
25134   while (1)
25135     {
25136       tree name, decl;
25137
25138       token = cp_lexer_peek_token (parser->lexer);
25139       name = cp_parser_id_expression (parser, /*template_p=*/false,
25140                                       /*check_dependency_p=*/true,
25141                                       /*template_p=*/NULL,
25142                                       /*declarator_p=*/false,
25143                                       /*optional_p=*/false);
25144       if (name == error_mark_node)
25145         goto skip_comma;
25146
25147       decl = cp_parser_lookup_name_simple (parser, name, token->location);
25148       if (decl == error_mark_node)
25149         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25150                                      token->location);
25151       else if (kind != 0)
25152         {
25153           tree u = build_omp_clause (token->location, kind);
25154           OMP_CLAUSE_DECL (u) = decl;
25155           OMP_CLAUSE_CHAIN (u) = list;
25156           list = u;
25157         }
25158       else
25159         list = tree_cons (decl, NULL_TREE, list);
25160
25161     get_comma:
25162       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25163         break;
25164       cp_lexer_consume_token (parser->lexer);
25165     }
25166
25167   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25168     {
25169       int ending;
25170
25171       /* Try to resync to an unnested comma.  Copied from
25172          cp_parser_parenthesized_expression_list.  */
25173     skip_comma:
25174       ending = cp_parser_skip_to_closing_parenthesis (parser,
25175                                                       /*recovering=*/true,
25176                                                       /*or_comma=*/true,
25177                                                       /*consume_paren=*/true);
25178       if (ending < 0)
25179         goto get_comma;
25180     }
25181
25182   return list;
25183 }
25184
25185 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25186    common case for omp clauses.  */
25187
25188 static tree
25189 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25190 {
25191   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25192     return cp_parser_omp_var_list_no_open (parser, kind, list);
25193   return list;
25194 }
25195
25196 /* OpenMP 3.0:
25197    collapse ( constant-expression ) */
25198
25199 static tree
25200 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25201 {
25202   tree c, num;
25203   location_t loc;
25204   HOST_WIDE_INT n;
25205
25206   loc = cp_lexer_peek_token (parser->lexer)->location;
25207   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25208     return list;
25209
25210   num = cp_parser_constant_expression (parser, false, NULL);
25211
25212   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25213     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25214                                            /*or_comma=*/false,
25215                                            /*consume_paren=*/true);
25216
25217   if (num == error_mark_node)
25218     return list;
25219   num = fold_non_dependent_expr (num);
25220   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25221       || !host_integerp (num, 0)
25222       || (n = tree_low_cst (num, 0)) <= 0
25223       || (int) n != n)
25224     {
25225       error_at (loc, "collapse argument needs positive constant integer expression");
25226       return list;
25227     }
25228
25229   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25230   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25231   OMP_CLAUSE_CHAIN (c) = list;
25232   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25233
25234   return c;
25235 }
25236
25237 /* OpenMP 2.5:
25238    default ( shared | none ) */
25239
25240 static tree
25241 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25242 {
25243   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25244   tree c;
25245
25246   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25247     return list;
25248   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25249     {
25250       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25251       const char *p = IDENTIFIER_POINTER (id);
25252
25253       switch (p[0])
25254         {
25255         case 'n':
25256           if (strcmp ("none", p) != 0)
25257             goto invalid_kind;
25258           kind = OMP_CLAUSE_DEFAULT_NONE;
25259           break;
25260
25261         case 's':
25262           if (strcmp ("shared", p) != 0)
25263             goto invalid_kind;
25264           kind = OMP_CLAUSE_DEFAULT_SHARED;
25265           break;
25266
25267         default:
25268           goto invalid_kind;
25269         }
25270
25271       cp_lexer_consume_token (parser->lexer);
25272     }
25273   else
25274     {
25275     invalid_kind:
25276       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25277     }
25278
25279   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25280     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25281                                            /*or_comma=*/false,
25282                                            /*consume_paren=*/true);
25283
25284   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25285     return list;
25286
25287   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25288   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25289   OMP_CLAUSE_CHAIN (c) = list;
25290   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25291
25292   return c;
25293 }
25294
25295 /* OpenMP 3.1:
25296    final ( expression ) */
25297
25298 static tree
25299 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25300 {
25301   tree t, c;
25302
25303   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25304     return list;
25305
25306   t = cp_parser_condition (parser);
25307
25308   if (t == error_mark_node
25309       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25310     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25311                                            /*or_comma=*/false,
25312                                            /*consume_paren=*/true);
25313
25314   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25315
25316   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25317   OMP_CLAUSE_FINAL_EXPR (c) = t;
25318   OMP_CLAUSE_CHAIN (c) = list;
25319
25320   return c;
25321 }
25322
25323 /* OpenMP 2.5:
25324    if ( expression ) */
25325
25326 static tree
25327 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25328 {
25329   tree t, c;
25330
25331   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25332     return list;
25333
25334   t = cp_parser_condition (parser);
25335
25336   if (t == error_mark_node
25337       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25338     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25339                                            /*or_comma=*/false,
25340                                            /*consume_paren=*/true);
25341
25342   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25343
25344   c = build_omp_clause (location, OMP_CLAUSE_IF);
25345   OMP_CLAUSE_IF_EXPR (c) = t;
25346   OMP_CLAUSE_CHAIN (c) = list;
25347
25348   return c;
25349 }
25350
25351 /* OpenMP 3.1:
25352    mergeable */
25353
25354 static tree
25355 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25356                                 tree list, location_t location)
25357 {
25358   tree c;
25359
25360   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25361                              location);
25362
25363   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25364   OMP_CLAUSE_CHAIN (c) = list;
25365   return c;
25366 }
25367
25368 /* OpenMP 2.5:
25369    nowait */
25370
25371 static tree
25372 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25373                              tree list, location_t location)
25374 {
25375   tree c;
25376
25377   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25378
25379   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25380   OMP_CLAUSE_CHAIN (c) = list;
25381   return c;
25382 }
25383
25384 /* OpenMP 2.5:
25385    num_threads ( expression ) */
25386
25387 static tree
25388 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25389                                   location_t location)
25390 {
25391   tree t, c;
25392
25393   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25394     return list;
25395
25396   t = cp_parser_expression (parser, false, NULL);
25397
25398   if (t == error_mark_node
25399       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25400     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25401                                            /*or_comma=*/false,
25402                                            /*consume_paren=*/true);
25403
25404   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25405                              "num_threads", location);
25406
25407   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25408   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25409   OMP_CLAUSE_CHAIN (c) = list;
25410
25411   return c;
25412 }
25413
25414 /* OpenMP 2.5:
25415    ordered */
25416
25417 static tree
25418 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25419                               tree list, location_t location)
25420 {
25421   tree c;
25422
25423   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25424                              "ordered", location);
25425
25426   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25427   OMP_CLAUSE_CHAIN (c) = list;
25428   return c;
25429 }
25430
25431 /* OpenMP 2.5:
25432    reduction ( reduction-operator : variable-list )
25433
25434    reduction-operator:
25435      One of: + * - & ^ | && ||
25436
25437    OpenMP 3.1:
25438
25439    reduction-operator:
25440      One of: + * - & ^ | && || min max  */
25441
25442 static tree
25443 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25444 {
25445   enum tree_code code;
25446   tree nlist, c;
25447
25448   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25449     return list;
25450
25451   switch (cp_lexer_peek_token (parser->lexer)->type)
25452     {
25453     case CPP_PLUS:
25454       code = PLUS_EXPR;
25455       break;
25456     case CPP_MULT:
25457       code = MULT_EXPR;
25458       break;
25459     case CPP_MINUS:
25460       code = MINUS_EXPR;
25461       break;
25462     case CPP_AND:
25463       code = BIT_AND_EXPR;
25464       break;
25465     case CPP_XOR:
25466       code = BIT_XOR_EXPR;
25467       break;
25468     case CPP_OR:
25469       code = BIT_IOR_EXPR;
25470       break;
25471     case CPP_AND_AND:
25472       code = TRUTH_ANDIF_EXPR;
25473       break;
25474     case CPP_OR_OR:
25475       code = TRUTH_ORIF_EXPR;
25476       break;
25477     case CPP_NAME:
25478       {
25479         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25480         const char *p = IDENTIFIER_POINTER (id);
25481
25482         if (strcmp (p, "min") == 0)
25483           {
25484             code = MIN_EXPR;
25485             break;
25486           }
25487         if (strcmp (p, "max") == 0)
25488           {
25489             code = MAX_EXPR;
25490             break;
25491           }
25492       }
25493       /* FALLTHROUGH */
25494     default:
25495       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25496                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25497     resync_fail:
25498       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25499                                              /*or_comma=*/false,
25500                                              /*consume_paren=*/true);
25501       return list;
25502     }
25503   cp_lexer_consume_token (parser->lexer);
25504
25505   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25506     goto resync_fail;
25507
25508   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25509   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25510     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25511
25512   return nlist;
25513 }
25514
25515 /* OpenMP 2.5:
25516    schedule ( schedule-kind )
25517    schedule ( schedule-kind , expression )
25518
25519    schedule-kind:
25520      static | dynamic | guided | runtime | auto  */
25521
25522 static tree
25523 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25524 {
25525   tree c, t;
25526
25527   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25528     return list;
25529
25530   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25531
25532   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25533     {
25534       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25535       const char *p = IDENTIFIER_POINTER (id);
25536
25537       switch (p[0])
25538         {
25539         case 'd':
25540           if (strcmp ("dynamic", p) != 0)
25541             goto invalid_kind;
25542           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25543           break;
25544
25545         case 'g':
25546           if (strcmp ("guided", p) != 0)
25547             goto invalid_kind;
25548           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25549           break;
25550
25551         case 'r':
25552           if (strcmp ("runtime", p) != 0)
25553             goto invalid_kind;
25554           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25555           break;
25556
25557         default:
25558           goto invalid_kind;
25559         }
25560     }
25561   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25562     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25563   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25564     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25565   else
25566     goto invalid_kind;
25567   cp_lexer_consume_token (parser->lexer);
25568
25569   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25570     {
25571       cp_token *token;
25572       cp_lexer_consume_token (parser->lexer);
25573
25574       token = cp_lexer_peek_token (parser->lexer);
25575       t = cp_parser_assignment_expression (parser, false, NULL);
25576
25577       if (t == error_mark_node)
25578         goto resync_fail;
25579       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25580         error_at (token->location, "schedule %<runtime%> does not take "
25581                   "a %<chunk_size%> parameter");
25582       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25583         error_at (token->location, "schedule %<auto%> does not take "
25584                   "a %<chunk_size%> parameter");
25585       else
25586         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25587
25588       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25589         goto resync_fail;
25590     }
25591   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25592     goto resync_fail;
25593
25594   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25595   OMP_CLAUSE_CHAIN (c) = list;
25596   return c;
25597
25598  invalid_kind:
25599   cp_parser_error (parser, "invalid schedule kind");
25600  resync_fail:
25601   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25602                                          /*or_comma=*/false,
25603                                          /*consume_paren=*/true);
25604   return list;
25605 }
25606
25607 /* OpenMP 3.0:
25608    untied */
25609
25610 static tree
25611 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25612                              tree list, location_t location)
25613 {
25614   tree c;
25615
25616   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25617
25618   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25619   OMP_CLAUSE_CHAIN (c) = list;
25620   return c;
25621 }
25622
25623 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25624    is a bitmask in MASK.  Return the list of clauses found; the result
25625    of clause default goes in *pdefault.  */
25626
25627 static tree
25628 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25629                            const char *where, cp_token *pragma_tok)
25630 {
25631   tree clauses = NULL;
25632   bool first = true;
25633   cp_token *token = NULL;
25634
25635   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25636     {
25637       pragma_omp_clause c_kind;
25638       const char *c_name;
25639       tree prev = clauses;
25640
25641       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25642         cp_lexer_consume_token (parser->lexer);
25643
25644       token = cp_lexer_peek_token (parser->lexer);
25645       c_kind = cp_parser_omp_clause_name (parser);
25646       first = false;
25647
25648       switch (c_kind)
25649         {
25650         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25651           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25652                                                    token->location);
25653           c_name = "collapse";
25654           break;
25655         case PRAGMA_OMP_CLAUSE_COPYIN:
25656           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25657           c_name = "copyin";
25658           break;
25659         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25660           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25661                                             clauses);
25662           c_name = "copyprivate";
25663           break;
25664         case PRAGMA_OMP_CLAUSE_DEFAULT:
25665           clauses = cp_parser_omp_clause_default (parser, clauses,
25666                                                   token->location);
25667           c_name = "default";
25668           break;
25669         case PRAGMA_OMP_CLAUSE_FINAL:
25670           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25671           c_name = "final";
25672           break;
25673         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25674           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25675                                             clauses);
25676           c_name = "firstprivate";
25677           break;
25678         case PRAGMA_OMP_CLAUSE_IF:
25679           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25680           c_name = "if";
25681           break;
25682         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25683           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25684                                             clauses);
25685           c_name = "lastprivate";
25686           break;
25687         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25688           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25689                                                     token->location);
25690           c_name = "mergeable";
25691           break;
25692         case PRAGMA_OMP_CLAUSE_NOWAIT:
25693           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25694           c_name = "nowait";
25695           break;
25696         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25697           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25698                                                       token->location);
25699           c_name = "num_threads";
25700           break;
25701         case PRAGMA_OMP_CLAUSE_ORDERED:
25702           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25703                                                   token->location);
25704           c_name = "ordered";
25705           break;
25706         case PRAGMA_OMP_CLAUSE_PRIVATE:
25707           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25708                                             clauses);
25709           c_name = "private";
25710           break;
25711         case PRAGMA_OMP_CLAUSE_REDUCTION:
25712           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25713           c_name = "reduction";
25714           break;
25715         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25716           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25717                                                    token->location);
25718           c_name = "schedule";
25719           break;
25720         case PRAGMA_OMP_CLAUSE_SHARED:
25721           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25722                                             clauses);
25723           c_name = "shared";
25724           break;
25725         case PRAGMA_OMP_CLAUSE_UNTIED:
25726           clauses = cp_parser_omp_clause_untied (parser, clauses,
25727                                                  token->location);
25728           c_name = "nowait";
25729           break;
25730         default:
25731           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25732           goto saw_error;
25733         }
25734
25735       if (((mask >> c_kind) & 1) == 0)
25736         {
25737           /* Remove the invalid clause(s) from the list to avoid
25738              confusing the rest of the compiler.  */
25739           clauses = prev;
25740           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25741         }
25742     }
25743  saw_error:
25744   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25745   return finish_omp_clauses (clauses);
25746 }
25747
25748 /* OpenMP 2.5:
25749    structured-block:
25750      statement
25751
25752    In practice, we're also interested in adding the statement to an
25753    outer node.  So it is convenient if we work around the fact that
25754    cp_parser_statement calls add_stmt.  */
25755
25756 static unsigned
25757 cp_parser_begin_omp_structured_block (cp_parser *parser)
25758 {
25759   unsigned save = parser->in_statement;
25760
25761   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25762      This preserves the "not within loop or switch" style error messages
25763      for nonsense cases like
25764         void foo() {
25765         #pragma omp single
25766           break;
25767         }
25768   */
25769   if (parser->in_statement)
25770     parser->in_statement = IN_OMP_BLOCK;
25771
25772   return save;
25773 }
25774
25775 static void
25776 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25777 {
25778   parser->in_statement = save;
25779 }
25780
25781 static tree
25782 cp_parser_omp_structured_block (cp_parser *parser)
25783 {
25784   tree stmt = begin_omp_structured_block ();
25785   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25786
25787   cp_parser_statement (parser, NULL_TREE, false, NULL);
25788
25789   cp_parser_end_omp_structured_block (parser, save);
25790   return finish_omp_structured_block (stmt);
25791 }
25792
25793 /* OpenMP 2.5:
25794    # pragma omp atomic new-line
25795      expression-stmt
25796
25797    expression-stmt:
25798      x binop= expr | x++ | ++x | x-- | --x
25799    binop:
25800      +, *, -, /, &, ^, |, <<, >>
25801
25802   where x is an lvalue expression with scalar type.
25803
25804    OpenMP 3.1:
25805    # pragma omp atomic new-line
25806      update-stmt
25807
25808    # pragma omp atomic read new-line
25809      read-stmt
25810
25811    # pragma omp atomic write new-line
25812      write-stmt
25813
25814    # pragma omp atomic update new-line
25815      update-stmt
25816
25817    # pragma omp atomic capture new-line
25818      capture-stmt
25819
25820    # pragma omp atomic capture new-line
25821      capture-block
25822
25823    read-stmt:
25824      v = x
25825    write-stmt:
25826      x = expr
25827    update-stmt:
25828      expression-stmt | x = x binop expr
25829    capture-stmt:
25830      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25831    capture-block:
25832      { v = x; update-stmt; } | { update-stmt; v = x; }
25833
25834   where x and v are lvalue expressions with scalar type.  */
25835
25836 static void
25837 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25838 {
25839   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25840   tree rhs1 = NULL_TREE, orig_lhs;
25841   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25842   bool structured_block = false;
25843
25844   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25845     {
25846       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25847       const char *p = IDENTIFIER_POINTER (id);
25848
25849       if (!strcmp (p, "read"))
25850         code = OMP_ATOMIC_READ;
25851       else if (!strcmp (p, "write"))
25852         code = NOP_EXPR;
25853       else if (!strcmp (p, "update"))
25854         code = OMP_ATOMIC;
25855       else if (!strcmp (p, "capture"))
25856         code = OMP_ATOMIC_CAPTURE_NEW;
25857       else
25858         p = NULL;
25859       if (p)
25860         cp_lexer_consume_token (parser->lexer);
25861     }
25862   cp_parser_require_pragma_eol (parser, pragma_tok);
25863
25864   switch (code)
25865     {
25866     case OMP_ATOMIC_READ:
25867     case NOP_EXPR: /* atomic write */
25868       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25869                                       /*cast_p=*/false, NULL);
25870       if (v == error_mark_node)
25871         goto saw_error;
25872       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25873         goto saw_error;
25874       if (code == NOP_EXPR)
25875         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25876       else
25877         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25878                                           /*cast_p=*/false, NULL);
25879       if (lhs == error_mark_node)
25880         goto saw_error;
25881       if (code == NOP_EXPR)
25882         {
25883           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25884              opcode.  */
25885           code = OMP_ATOMIC;
25886           rhs = lhs;
25887           lhs = v;
25888           v = NULL_TREE;
25889         }
25890       goto done;
25891     case OMP_ATOMIC_CAPTURE_NEW:
25892       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25893         {
25894           cp_lexer_consume_token (parser->lexer);
25895           structured_block = true;
25896         }
25897       else
25898         {
25899           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25900                                           /*cast_p=*/false, NULL);
25901           if (v == error_mark_node)
25902             goto saw_error;
25903           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25904             goto saw_error;
25905         }
25906     default:
25907       break;
25908     }
25909
25910 restart:
25911   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25912                                     /*cast_p=*/false, NULL);
25913   orig_lhs = lhs;
25914   switch (TREE_CODE (lhs))
25915     {
25916     case ERROR_MARK:
25917       goto saw_error;
25918
25919     case POSTINCREMENT_EXPR:
25920       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25921         code = OMP_ATOMIC_CAPTURE_OLD;
25922       /* FALLTHROUGH */
25923     case PREINCREMENT_EXPR:
25924       lhs = TREE_OPERAND (lhs, 0);
25925       opcode = PLUS_EXPR;
25926       rhs = integer_one_node;
25927       break;
25928
25929     case POSTDECREMENT_EXPR:
25930       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25931         code = OMP_ATOMIC_CAPTURE_OLD;
25932       /* FALLTHROUGH */
25933     case PREDECREMENT_EXPR:
25934       lhs = TREE_OPERAND (lhs, 0);
25935       opcode = MINUS_EXPR;
25936       rhs = integer_one_node;
25937       break;
25938
25939     case COMPOUND_EXPR:
25940       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25941          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25942          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25943          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25944          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25945                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25946             == BOOLEAN_TYPE)
25947        /* Undo effects of boolean_increment for post {in,de}crement.  */
25948        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25949       /* FALLTHRU */
25950     case MODIFY_EXPR:
25951       if (TREE_CODE (lhs) == MODIFY_EXPR
25952          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25953         {
25954           /* Undo effects of boolean_increment.  */
25955           if (integer_onep (TREE_OPERAND (lhs, 1)))
25956             {
25957               /* This is pre or post increment.  */
25958               rhs = TREE_OPERAND (lhs, 1);
25959               lhs = TREE_OPERAND (lhs, 0);
25960               opcode = NOP_EXPR;
25961               if (code == OMP_ATOMIC_CAPTURE_NEW
25962                   && !structured_block
25963                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25964                 code = OMP_ATOMIC_CAPTURE_OLD;
25965               break;
25966             }
25967         }
25968       /* FALLTHRU */
25969     default:
25970       switch (cp_lexer_peek_token (parser->lexer)->type)
25971         {
25972         case CPP_MULT_EQ:
25973           opcode = MULT_EXPR;
25974           break;
25975         case CPP_DIV_EQ:
25976           opcode = TRUNC_DIV_EXPR;
25977           break;
25978         case CPP_PLUS_EQ:
25979           opcode = PLUS_EXPR;
25980           break;
25981         case CPP_MINUS_EQ:
25982           opcode = MINUS_EXPR;
25983           break;
25984         case CPP_LSHIFT_EQ:
25985           opcode = LSHIFT_EXPR;
25986           break;
25987         case CPP_RSHIFT_EQ:
25988           opcode = RSHIFT_EXPR;
25989           break;
25990         case CPP_AND_EQ:
25991           opcode = BIT_AND_EXPR;
25992           break;
25993         case CPP_OR_EQ:
25994           opcode = BIT_IOR_EXPR;
25995           break;
25996         case CPP_XOR_EQ:
25997           opcode = BIT_XOR_EXPR;
25998           break;
25999         case CPP_EQ:
26000           if (structured_block || code == OMP_ATOMIC)
26001             {
26002               enum cp_parser_prec oprec;
26003               cp_token *token;
26004               cp_lexer_consume_token (parser->lexer);
26005               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26006                                                  /*cast_p=*/false, NULL);
26007               if (rhs1 == error_mark_node)
26008                 goto saw_error;
26009               token = cp_lexer_peek_token (parser->lexer);
26010               switch (token->type)
26011                 {
26012                 case CPP_SEMICOLON:
26013                   if (code == OMP_ATOMIC_CAPTURE_NEW)
26014                     {
26015                       code = OMP_ATOMIC_CAPTURE_OLD;
26016                       v = lhs;
26017                       lhs = NULL_TREE;
26018                       lhs1 = rhs1;
26019                       rhs1 = NULL_TREE;
26020                       cp_lexer_consume_token (parser->lexer);
26021                       goto restart;
26022                     }
26023                   cp_parser_error (parser,
26024                                    "invalid form of %<#pragma omp atomic%>");
26025                   goto saw_error;
26026                 case CPP_MULT:
26027                   opcode = MULT_EXPR;
26028                   break;
26029                 case CPP_DIV:
26030                   opcode = TRUNC_DIV_EXPR;
26031                   break;
26032                 case CPP_PLUS:
26033                   opcode = PLUS_EXPR;
26034                   break;
26035                 case CPP_MINUS:
26036                   opcode = MINUS_EXPR;
26037                   break;
26038                 case CPP_LSHIFT:
26039                   opcode = LSHIFT_EXPR;
26040                   break;
26041                 case CPP_RSHIFT:
26042                   opcode = RSHIFT_EXPR;
26043                   break;
26044                 case CPP_AND:
26045                   opcode = BIT_AND_EXPR;
26046                   break;
26047                 case CPP_OR:
26048                   opcode = BIT_IOR_EXPR;
26049                   break;
26050                 case CPP_XOR:
26051                   opcode = BIT_XOR_EXPR;
26052                   break;
26053                 default:
26054                   cp_parser_error (parser,
26055                                    "invalid operator for %<#pragma omp atomic%>");
26056                   goto saw_error;
26057                 }
26058               oprec = TOKEN_PRECEDENCE (token);
26059               gcc_assert (oprec != PREC_NOT_OPERATOR);
26060               if (commutative_tree_code (opcode))
26061                 oprec = (enum cp_parser_prec) (oprec - 1);
26062               cp_lexer_consume_token (parser->lexer);
26063               rhs = cp_parser_binary_expression (parser, false, false,
26064                                                  oprec, NULL);
26065               if (rhs == error_mark_node)
26066                 goto saw_error;
26067               goto stmt_done;
26068             }
26069           /* FALLTHROUGH */
26070         default:
26071           cp_parser_error (parser,
26072                            "invalid operator for %<#pragma omp atomic%>");
26073           goto saw_error;
26074         }
26075       cp_lexer_consume_token (parser->lexer);
26076
26077       rhs = cp_parser_expression (parser, false, NULL);
26078       if (rhs == error_mark_node)
26079         goto saw_error;
26080       break;
26081     }
26082 stmt_done:
26083   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26084     {
26085       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26086         goto saw_error;
26087       v = cp_parser_unary_expression (parser, /*address_p=*/false,
26088                                       /*cast_p=*/false, NULL);
26089       if (v == error_mark_node)
26090         goto saw_error;
26091       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26092         goto saw_error;
26093       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26094                                          /*cast_p=*/false, NULL);
26095       if (lhs1 == error_mark_node)
26096         goto saw_error;
26097     }
26098   if (structured_block)
26099     {
26100       cp_parser_consume_semicolon_at_end_of_statement (parser);
26101       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26102     }
26103 done:
26104   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26105   if (!structured_block)
26106     cp_parser_consume_semicolon_at_end_of_statement (parser);
26107   return;
26108
26109  saw_error:
26110   cp_parser_skip_to_end_of_block_or_statement (parser);
26111   if (structured_block)
26112     {
26113       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26114         cp_lexer_consume_token (parser->lexer);
26115       else if (code == OMP_ATOMIC_CAPTURE_NEW)
26116         {
26117           cp_parser_skip_to_end_of_block_or_statement (parser);
26118           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26119             cp_lexer_consume_token (parser->lexer);
26120         }
26121     }
26122 }
26123
26124
26125 /* OpenMP 2.5:
26126    # pragma omp barrier new-line  */
26127
26128 static void
26129 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26130 {
26131   cp_parser_require_pragma_eol (parser, pragma_tok);
26132   finish_omp_barrier ();
26133 }
26134
26135 /* OpenMP 2.5:
26136    # pragma omp critical [(name)] new-line
26137      structured-block  */
26138
26139 static tree
26140 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26141 {
26142   tree stmt, name = NULL;
26143
26144   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26145     {
26146       cp_lexer_consume_token (parser->lexer);
26147
26148       name = cp_parser_identifier (parser);
26149
26150       if (name == error_mark_node
26151           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26152         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26153                                                /*or_comma=*/false,
26154                                                /*consume_paren=*/true);
26155       if (name == error_mark_node)
26156         name = NULL;
26157     }
26158   cp_parser_require_pragma_eol (parser, pragma_tok);
26159
26160   stmt = cp_parser_omp_structured_block (parser);
26161   return c_finish_omp_critical (input_location, stmt, name);
26162 }
26163
26164 /* OpenMP 2.5:
26165    # pragma omp flush flush-vars[opt] new-line
26166
26167    flush-vars:
26168      ( variable-list ) */
26169
26170 static void
26171 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26172 {
26173   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26174     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26175   cp_parser_require_pragma_eol (parser, pragma_tok);
26176
26177   finish_omp_flush ();
26178 }
26179
26180 /* Helper function, to parse omp for increment expression.  */
26181
26182 static tree
26183 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26184 {
26185   tree cond = cp_parser_binary_expression (parser, false, true,
26186                                            PREC_NOT_OPERATOR, NULL);
26187   if (cond == error_mark_node
26188       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26189     {
26190       cp_parser_skip_to_end_of_statement (parser);
26191       return error_mark_node;
26192     }
26193
26194   switch (TREE_CODE (cond))
26195     {
26196     case GT_EXPR:
26197     case GE_EXPR:
26198     case LT_EXPR:
26199     case LE_EXPR:
26200       break;
26201     default:
26202       return error_mark_node;
26203     }
26204
26205   /* If decl is an iterator, preserve LHS and RHS of the relational
26206      expr until finish_omp_for.  */
26207   if (decl
26208       && (type_dependent_expression_p (decl)
26209           || CLASS_TYPE_P (TREE_TYPE (decl))))
26210     return cond;
26211
26212   return build_x_binary_op (input_location, TREE_CODE (cond),
26213                             TREE_OPERAND (cond, 0), ERROR_MARK,
26214                             TREE_OPERAND (cond, 1), ERROR_MARK,
26215                             /*overload=*/NULL, tf_warning_or_error);
26216 }
26217
26218 /* Helper function, to parse omp for increment expression.  */
26219
26220 static tree
26221 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26222 {
26223   cp_token *token = cp_lexer_peek_token (parser->lexer);
26224   enum tree_code op;
26225   tree lhs, rhs;
26226   cp_id_kind idk;
26227   bool decl_first;
26228
26229   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26230     {
26231       op = (token->type == CPP_PLUS_PLUS
26232             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26233       cp_lexer_consume_token (parser->lexer);
26234       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26235       if (lhs != decl)
26236         return error_mark_node;
26237       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26238     }
26239
26240   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26241   if (lhs != decl)
26242     return error_mark_node;
26243
26244   token = cp_lexer_peek_token (parser->lexer);
26245   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26246     {
26247       op = (token->type == CPP_PLUS_PLUS
26248             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26249       cp_lexer_consume_token (parser->lexer);
26250       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26251     }
26252
26253   op = cp_parser_assignment_operator_opt (parser);
26254   if (op == ERROR_MARK)
26255     return error_mark_node;
26256
26257   if (op != NOP_EXPR)
26258     {
26259       rhs = cp_parser_assignment_expression (parser, false, NULL);
26260       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26261       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26262     }
26263
26264   lhs = cp_parser_binary_expression (parser, false, false,
26265                                      PREC_ADDITIVE_EXPRESSION, NULL);
26266   token = cp_lexer_peek_token (parser->lexer);
26267   decl_first = lhs == decl;
26268   if (decl_first)
26269     lhs = NULL_TREE;
26270   if (token->type != CPP_PLUS
26271       && token->type != CPP_MINUS)
26272     return error_mark_node;
26273
26274   do
26275     {
26276       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26277       cp_lexer_consume_token (parser->lexer);
26278       rhs = cp_parser_binary_expression (parser, false, false,
26279                                          PREC_ADDITIVE_EXPRESSION, NULL);
26280       token = cp_lexer_peek_token (parser->lexer);
26281       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26282         {
26283           if (lhs == NULL_TREE)
26284             {
26285               if (op == PLUS_EXPR)
26286                 lhs = rhs;
26287               else
26288                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26289                                         tf_warning_or_error);
26290             }
26291           else
26292             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26293                                      ERROR_MARK, NULL, tf_warning_or_error);
26294         }
26295     }
26296   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26297
26298   if (!decl_first)
26299     {
26300       if (rhs != decl || op == MINUS_EXPR)
26301         return error_mark_node;
26302       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26303     }
26304   else
26305     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26306
26307   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26308 }
26309
26310 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26311
26312 static tree
26313 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26314 {
26315   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26316   tree real_decl, initv, condv, incrv, declv;
26317   tree this_pre_body, cl;
26318   location_t loc_first;
26319   bool collapse_err = false;
26320   int i, collapse = 1, nbraces = 0;
26321   VEC(tree,gc) *for_block = make_tree_vector ();
26322
26323   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26324     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26325       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26326
26327   gcc_assert (collapse >= 1);
26328
26329   declv = make_tree_vec (collapse);
26330   initv = make_tree_vec (collapse);
26331   condv = make_tree_vec (collapse);
26332   incrv = make_tree_vec (collapse);
26333
26334   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26335
26336   for (i = 0; i < collapse; i++)
26337     {
26338       int bracecount = 0;
26339       bool add_private_clause = false;
26340       location_t loc;
26341
26342       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26343         {
26344           cp_parser_error (parser, "for statement expected");
26345           return NULL;
26346         }
26347       loc = cp_lexer_consume_token (parser->lexer)->location;
26348
26349       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26350         return NULL;
26351
26352       init = decl = real_decl = NULL;
26353       this_pre_body = push_stmt_list ();
26354       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26355         {
26356           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26357
26358              init-expr:
26359                        var = lb
26360                        integer-type var = lb
26361                        random-access-iterator-type var = lb
26362                        pointer-type var = lb
26363           */
26364           cp_decl_specifier_seq type_specifiers;
26365
26366           /* First, try to parse as an initialized declaration.  See
26367              cp_parser_condition, from whence the bulk of this is copied.  */
26368
26369           cp_parser_parse_tentatively (parser);
26370           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26371                                         /*is_trailing_return=*/false,
26372                                         &type_specifiers);
26373           if (cp_parser_parse_definitely (parser))
26374             {
26375               /* If parsing a type specifier seq succeeded, then this
26376                  MUST be a initialized declaration.  */
26377               tree asm_specification, attributes;
26378               cp_declarator *declarator;
26379
26380               declarator = cp_parser_declarator (parser,
26381                                                  CP_PARSER_DECLARATOR_NAMED,
26382                                                  /*ctor_dtor_or_conv_p=*/NULL,
26383                                                  /*parenthesized_p=*/NULL,
26384                                                  /*member_p=*/false);
26385               attributes = cp_parser_attributes_opt (parser);
26386               asm_specification = cp_parser_asm_specification_opt (parser);
26387
26388               if (declarator == cp_error_declarator) 
26389                 cp_parser_skip_to_end_of_statement (parser);
26390
26391               else 
26392                 {
26393                   tree pushed_scope, auto_node;
26394
26395                   decl = start_decl (declarator, &type_specifiers,
26396                                      SD_INITIALIZED, attributes,
26397                                      /*prefix_attributes=*/NULL_TREE,
26398                                      &pushed_scope);
26399
26400                   auto_node = type_uses_auto (TREE_TYPE (decl));
26401                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26402                     {
26403                       if (cp_lexer_next_token_is (parser->lexer, 
26404                                                   CPP_OPEN_PAREN))
26405                         error ("parenthesized initialization is not allowed in "
26406                                "OpenMP %<for%> loop");
26407                       else
26408                         /* Trigger an error.  */
26409                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26410
26411                       init = error_mark_node;
26412                       cp_parser_skip_to_end_of_statement (parser);
26413                     }
26414                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26415                            || type_dependent_expression_p (decl)
26416                            || auto_node)
26417                     {
26418                       bool is_direct_init, is_non_constant_init;
26419
26420                       init = cp_parser_initializer (parser,
26421                                                     &is_direct_init,
26422                                                     &is_non_constant_init);
26423
26424                       if (auto_node)
26425                         {
26426                           TREE_TYPE (decl)
26427                             = do_auto_deduction (TREE_TYPE (decl), init,
26428                                                  auto_node);
26429
26430                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26431                               && !type_dependent_expression_p (decl))
26432                             goto non_class;
26433                         }
26434                       
26435                       cp_finish_decl (decl, init, !is_non_constant_init,
26436                                       asm_specification,
26437                                       LOOKUP_ONLYCONVERTING);
26438                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26439                         {
26440                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26441                           init = NULL_TREE;
26442                         }
26443                       else
26444                         init = pop_stmt_list (this_pre_body);
26445                       this_pre_body = NULL_TREE;
26446                     }
26447                   else
26448                     {
26449                       /* Consume '='.  */
26450                       cp_lexer_consume_token (parser->lexer);
26451                       init = cp_parser_assignment_expression (parser, false, NULL);
26452
26453                     non_class:
26454                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26455                         init = error_mark_node;
26456                       else
26457                         cp_finish_decl (decl, NULL_TREE,
26458                                         /*init_const_expr_p=*/false,
26459                                         asm_specification,
26460                                         LOOKUP_ONLYCONVERTING);
26461                     }
26462
26463                   if (pushed_scope)
26464                     pop_scope (pushed_scope);
26465                 }
26466             }
26467           else 
26468             {
26469               cp_id_kind idk;
26470               /* If parsing a type specifier sequence failed, then
26471                  this MUST be a simple expression.  */
26472               cp_parser_parse_tentatively (parser);
26473               decl = cp_parser_primary_expression (parser, false, false,
26474                                                    false, &idk);
26475               if (!cp_parser_error_occurred (parser)
26476                   && decl
26477                   && DECL_P (decl)
26478                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26479                 {
26480                   tree rhs;
26481
26482                   cp_parser_parse_definitely (parser);
26483                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26484                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26485                   finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26486                                                          decl, NOP_EXPR,
26487                                                          rhs,
26488                                                          tf_warning_or_error));
26489                   add_private_clause = true;
26490                 }
26491               else
26492                 {
26493                   decl = NULL;
26494                   cp_parser_abort_tentative_parse (parser);
26495                   init = cp_parser_expression (parser, false, NULL);
26496                   if (init)
26497                     {
26498                       if (TREE_CODE (init) == MODIFY_EXPR
26499                           || TREE_CODE (init) == MODOP_EXPR)
26500                         real_decl = TREE_OPERAND (init, 0);
26501                     }
26502                 }
26503             }
26504         }
26505       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26506       if (this_pre_body)
26507         {
26508           this_pre_body = pop_stmt_list (this_pre_body);
26509           if (pre_body)
26510             {
26511               tree t = pre_body;
26512               pre_body = push_stmt_list ();
26513               add_stmt (t);
26514               add_stmt (this_pre_body);
26515               pre_body = pop_stmt_list (pre_body);
26516             }
26517           else
26518             pre_body = this_pre_body;
26519         }
26520
26521       if (decl)
26522         real_decl = decl;
26523       if (par_clauses != NULL && real_decl != NULL_TREE)
26524         {
26525           tree *c;
26526           for (c = par_clauses; *c ; )
26527             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26528                 && OMP_CLAUSE_DECL (*c) == real_decl)
26529               {
26530                 error_at (loc, "iteration variable %qD"
26531                           " should not be firstprivate", real_decl);
26532                 *c = OMP_CLAUSE_CHAIN (*c);
26533               }
26534             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26535                      && OMP_CLAUSE_DECL (*c) == real_decl)
26536               {
26537                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26538                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26539                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26540                 OMP_CLAUSE_DECL (l) = real_decl;
26541                 OMP_CLAUSE_CHAIN (l) = clauses;
26542                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26543                 clauses = l;
26544                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26545                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26546                 add_private_clause = false;
26547               }
26548             else
26549               {
26550                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26551                     && OMP_CLAUSE_DECL (*c) == real_decl)
26552                   add_private_clause = false;
26553                 c = &OMP_CLAUSE_CHAIN (*c);
26554               }
26555         }
26556
26557       if (add_private_clause)
26558         {
26559           tree c;
26560           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26561             {
26562               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26563                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26564                   && OMP_CLAUSE_DECL (c) == decl)
26565                 break;
26566               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26567                        && OMP_CLAUSE_DECL (c) == decl)
26568                 error_at (loc, "iteration variable %qD "
26569                           "should not be firstprivate",
26570                           decl);
26571               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26572                        && OMP_CLAUSE_DECL (c) == decl)
26573                 error_at (loc, "iteration variable %qD should not be reduction",
26574                           decl);
26575             }
26576           if (c == NULL)
26577             {
26578               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26579               OMP_CLAUSE_DECL (c) = decl;
26580               c = finish_omp_clauses (c);
26581               if (c)
26582                 {
26583                   OMP_CLAUSE_CHAIN (c) = clauses;
26584                   clauses = c;
26585                 }
26586             }
26587         }
26588
26589       cond = NULL;
26590       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26591         cond = cp_parser_omp_for_cond (parser, decl);
26592       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26593
26594       incr = NULL;
26595       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26596         {
26597           /* If decl is an iterator, preserve the operator on decl
26598              until finish_omp_for.  */
26599           if (real_decl
26600               && ((processing_template_decl
26601                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26602                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26603             incr = cp_parser_omp_for_incr (parser, real_decl);
26604           else
26605             incr = cp_parser_expression (parser, false, NULL);
26606         }
26607
26608       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26609         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26610                                                /*or_comma=*/false,
26611                                                /*consume_paren=*/true);
26612
26613       TREE_VEC_ELT (declv, i) = decl;
26614       TREE_VEC_ELT (initv, i) = init;
26615       TREE_VEC_ELT (condv, i) = cond;
26616       TREE_VEC_ELT (incrv, i) = incr;
26617
26618       if (i == collapse - 1)
26619         break;
26620
26621       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26622          in between the collapsed for loops to be still considered perfectly
26623          nested.  Hopefully the final version clarifies this.
26624          For now handle (multiple) {'s and empty statements.  */
26625       cp_parser_parse_tentatively (parser);
26626       do
26627         {
26628           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26629             break;
26630           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26631             {
26632               cp_lexer_consume_token (parser->lexer);
26633               bracecount++;
26634             }
26635           else if (bracecount
26636                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26637             cp_lexer_consume_token (parser->lexer);
26638           else
26639             {
26640               loc = cp_lexer_peek_token (parser->lexer)->location;
26641               error_at (loc, "not enough collapsed for loops");
26642               collapse_err = true;
26643               cp_parser_abort_tentative_parse (parser);
26644               declv = NULL_TREE;
26645               break;
26646             }
26647         }
26648       while (1);
26649
26650       if (declv)
26651         {
26652           cp_parser_parse_definitely (parser);
26653           nbraces += bracecount;
26654         }
26655     }
26656
26657   /* Note that we saved the original contents of this flag when we entered
26658      the structured block, and so we don't need to re-save it here.  */
26659   parser->in_statement = IN_OMP_FOR;
26660
26661   /* Note that the grammar doesn't call for a structured block here,
26662      though the loop as a whole is a structured block.  */
26663   body = push_stmt_list ();
26664   cp_parser_statement (parser, NULL_TREE, false, NULL);
26665   body = pop_stmt_list (body);
26666
26667   if (declv == NULL_TREE)
26668     ret = NULL_TREE;
26669   else
26670     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26671                           pre_body, clauses);
26672
26673   while (nbraces)
26674     {
26675       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26676         {
26677           cp_lexer_consume_token (parser->lexer);
26678           nbraces--;
26679         }
26680       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26681         cp_lexer_consume_token (parser->lexer);
26682       else
26683         {
26684           if (!collapse_err)
26685             {
26686               error_at (cp_lexer_peek_token (parser->lexer)->location,
26687                         "collapsed loops not perfectly nested");
26688             }
26689           collapse_err = true;
26690           cp_parser_statement_seq_opt (parser, NULL);
26691           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26692             break;
26693         }
26694     }
26695
26696   while (!VEC_empty (tree, for_block))
26697     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26698   release_tree_vector (for_block);
26699
26700   return ret;
26701 }
26702
26703 /* OpenMP 2.5:
26704    #pragma omp for for-clause[optseq] new-line
26705      for-loop  */
26706
26707 #define OMP_FOR_CLAUSE_MASK                             \
26708         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26709         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26710         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26711         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26712         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26713         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26714         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26715         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26716
26717 static tree
26718 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26719 {
26720   tree clauses, sb, ret;
26721   unsigned int save;
26722
26723   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26724                                        "#pragma omp for", pragma_tok);
26725
26726   sb = begin_omp_structured_block ();
26727   save = cp_parser_begin_omp_structured_block (parser);
26728
26729   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26730
26731   cp_parser_end_omp_structured_block (parser, save);
26732   add_stmt (finish_omp_structured_block (sb));
26733
26734   return ret;
26735 }
26736
26737 /* OpenMP 2.5:
26738    # pragma omp master new-line
26739      structured-block  */
26740
26741 static tree
26742 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26743 {
26744   cp_parser_require_pragma_eol (parser, pragma_tok);
26745   return c_finish_omp_master (input_location,
26746                               cp_parser_omp_structured_block (parser));
26747 }
26748
26749 /* OpenMP 2.5:
26750    # pragma omp ordered new-line
26751      structured-block  */
26752
26753 static tree
26754 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26755 {
26756   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26757   cp_parser_require_pragma_eol (parser, pragma_tok);
26758   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26759 }
26760
26761 /* OpenMP 2.5:
26762
26763    section-scope:
26764      { section-sequence }
26765
26766    section-sequence:
26767      section-directive[opt] structured-block
26768      section-sequence section-directive structured-block  */
26769
26770 static tree
26771 cp_parser_omp_sections_scope (cp_parser *parser)
26772 {
26773   tree stmt, substmt;
26774   bool error_suppress = false;
26775   cp_token *tok;
26776
26777   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26778     return NULL_TREE;
26779
26780   stmt = push_stmt_list ();
26781
26782   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26783     {
26784       unsigned save;
26785
26786       substmt = begin_omp_structured_block ();
26787       save = cp_parser_begin_omp_structured_block (parser);
26788
26789       while (1)
26790         {
26791           cp_parser_statement (parser, NULL_TREE, false, NULL);
26792
26793           tok = cp_lexer_peek_token (parser->lexer);
26794           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26795             break;
26796           if (tok->type == CPP_CLOSE_BRACE)
26797             break;
26798           if (tok->type == CPP_EOF)
26799             break;
26800         }
26801
26802       cp_parser_end_omp_structured_block (parser, save);
26803       substmt = finish_omp_structured_block (substmt);
26804       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26805       add_stmt (substmt);
26806     }
26807
26808   while (1)
26809     {
26810       tok = cp_lexer_peek_token (parser->lexer);
26811       if (tok->type == CPP_CLOSE_BRACE)
26812         break;
26813       if (tok->type == CPP_EOF)
26814         break;
26815
26816       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26817         {
26818           cp_lexer_consume_token (parser->lexer);
26819           cp_parser_require_pragma_eol (parser, tok);
26820           error_suppress = false;
26821         }
26822       else if (!error_suppress)
26823         {
26824           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26825           error_suppress = true;
26826         }
26827
26828       substmt = cp_parser_omp_structured_block (parser);
26829       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26830       add_stmt (substmt);
26831     }
26832   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26833
26834   substmt = pop_stmt_list (stmt);
26835
26836   stmt = make_node (OMP_SECTIONS);
26837   TREE_TYPE (stmt) = void_type_node;
26838   OMP_SECTIONS_BODY (stmt) = substmt;
26839
26840   add_stmt (stmt);
26841   return stmt;
26842 }
26843
26844 /* OpenMP 2.5:
26845    # pragma omp sections sections-clause[optseq] newline
26846      sections-scope  */
26847
26848 #define OMP_SECTIONS_CLAUSE_MASK                        \
26849         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26850         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26851         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26852         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26853         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26854
26855 static tree
26856 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26857 {
26858   tree clauses, ret;
26859
26860   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26861                                        "#pragma omp sections", pragma_tok);
26862
26863   ret = cp_parser_omp_sections_scope (parser);
26864   if (ret)
26865     OMP_SECTIONS_CLAUSES (ret) = clauses;
26866
26867   return ret;
26868 }
26869
26870 /* OpenMP 2.5:
26871    # pragma parallel parallel-clause new-line
26872    # pragma parallel for parallel-for-clause new-line
26873    # pragma parallel sections parallel-sections-clause new-line  */
26874
26875 #define OMP_PARALLEL_CLAUSE_MASK                        \
26876         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26877         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26878         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26879         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26880         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26881         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26882         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26883         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26884
26885 static tree
26886 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26887 {
26888   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26889   const char *p_name = "#pragma omp parallel";
26890   tree stmt, clauses, par_clause, ws_clause, block;
26891   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26892   unsigned int save;
26893   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26894
26895   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26896     {
26897       cp_lexer_consume_token (parser->lexer);
26898       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26899       p_name = "#pragma omp parallel for";
26900       mask |= OMP_FOR_CLAUSE_MASK;
26901       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26902     }
26903   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26904     {
26905       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26906       const char *p = IDENTIFIER_POINTER (id);
26907       if (strcmp (p, "sections") == 0)
26908         {
26909           cp_lexer_consume_token (parser->lexer);
26910           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26911           p_name = "#pragma omp parallel sections";
26912           mask |= OMP_SECTIONS_CLAUSE_MASK;
26913           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26914         }
26915     }
26916
26917   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26918   block = begin_omp_parallel ();
26919   save = cp_parser_begin_omp_structured_block (parser);
26920
26921   switch (p_kind)
26922     {
26923     case PRAGMA_OMP_PARALLEL:
26924       cp_parser_statement (parser, NULL_TREE, false, NULL);
26925       par_clause = clauses;
26926       break;
26927
26928     case PRAGMA_OMP_PARALLEL_FOR:
26929       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26930       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26931       break;
26932
26933     case PRAGMA_OMP_PARALLEL_SECTIONS:
26934       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26935       stmt = cp_parser_omp_sections_scope (parser);
26936       if (stmt)
26937         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26938       break;
26939
26940     default:
26941       gcc_unreachable ();
26942     }
26943
26944   cp_parser_end_omp_structured_block (parser, save);
26945   stmt = finish_omp_parallel (par_clause, block);
26946   if (p_kind != PRAGMA_OMP_PARALLEL)
26947     OMP_PARALLEL_COMBINED (stmt) = 1;
26948   return stmt;
26949 }
26950
26951 /* OpenMP 2.5:
26952    # pragma omp single single-clause[optseq] new-line
26953      structured-block  */
26954
26955 #define OMP_SINGLE_CLAUSE_MASK                          \
26956         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26957         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26958         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26959         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26960
26961 static tree
26962 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26963 {
26964   tree stmt = make_node (OMP_SINGLE);
26965   TREE_TYPE (stmt) = void_type_node;
26966
26967   OMP_SINGLE_CLAUSES (stmt)
26968     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26969                                  "#pragma omp single", pragma_tok);
26970   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26971
26972   return add_stmt (stmt);
26973 }
26974
26975 /* OpenMP 3.0:
26976    # pragma omp task task-clause[optseq] new-line
26977      structured-block  */
26978
26979 #define OMP_TASK_CLAUSE_MASK                            \
26980         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26981         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26982         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26983         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26984         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26985         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26986         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26987         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26988
26989 static tree
26990 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26991 {
26992   tree clauses, block;
26993   unsigned int save;
26994
26995   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26996                                        "#pragma omp task", pragma_tok);
26997   block = begin_omp_task ();
26998   save = cp_parser_begin_omp_structured_block (parser);
26999   cp_parser_statement (parser, NULL_TREE, false, NULL);
27000   cp_parser_end_omp_structured_block (parser, save);
27001   return finish_omp_task (clauses, block);
27002 }
27003
27004 /* OpenMP 3.0:
27005    # pragma omp taskwait new-line  */
27006
27007 static void
27008 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27009 {
27010   cp_parser_require_pragma_eol (parser, pragma_tok);
27011   finish_omp_taskwait ();
27012 }
27013
27014 /* OpenMP 3.1:
27015    # pragma omp taskyield new-line  */
27016
27017 static void
27018 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27019 {
27020   cp_parser_require_pragma_eol (parser, pragma_tok);
27021   finish_omp_taskyield ();
27022 }
27023
27024 /* OpenMP 2.5:
27025    # pragma omp threadprivate (variable-list) */
27026
27027 static void
27028 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27029 {
27030   tree vars;
27031
27032   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27033   cp_parser_require_pragma_eol (parser, pragma_tok);
27034
27035   finish_omp_threadprivate (vars);
27036 }
27037
27038 /* Main entry point to OpenMP statement pragmas.  */
27039
27040 static void
27041 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27042 {
27043   tree stmt;
27044
27045   switch (pragma_tok->pragma_kind)
27046     {
27047     case PRAGMA_OMP_ATOMIC:
27048       cp_parser_omp_atomic (parser, pragma_tok);
27049       return;
27050     case PRAGMA_OMP_CRITICAL:
27051       stmt = cp_parser_omp_critical (parser, pragma_tok);
27052       break;
27053     case PRAGMA_OMP_FOR:
27054       stmt = cp_parser_omp_for (parser, pragma_tok);
27055       break;
27056     case PRAGMA_OMP_MASTER:
27057       stmt = cp_parser_omp_master (parser, pragma_tok);
27058       break;
27059     case PRAGMA_OMP_ORDERED:
27060       stmt = cp_parser_omp_ordered (parser, pragma_tok);
27061       break;
27062     case PRAGMA_OMP_PARALLEL:
27063       stmt = cp_parser_omp_parallel (parser, pragma_tok);
27064       break;
27065     case PRAGMA_OMP_SECTIONS:
27066       stmt = cp_parser_omp_sections (parser, pragma_tok);
27067       break;
27068     case PRAGMA_OMP_SINGLE:
27069       stmt = cp_parser_omp_single (parser, pragma_tok);
27070       break;
27071     case PRAGMA_OMP_TASK:
27072       stmt = cp_parser_omp_task (parser, pragma_tok);
27073       break;
27074     default:
27075       gcc_unreachable ();
27076     }
27077
27078   if (stmt)
27079     SET_EXPR_LOCATION (stmt, pragma_tok->location);
27080 }
27081 \f
27082 /* Transactional Memory parsing routines.  */
27083
27084 /* Parse a transaction attribute.
27085
27086    txn-attribute:
27087         attribute
27088         [ [ identifier ] ]
27089
27090    ??? Simplify this when C++0x bracket attributes are
27091    implemented properly.  */
27092
27093 static tree
27094 cp_parser_txn_attribute_opt (cp_parser *parser)
27095 {
27096   cp_token *token;
27097   tree attr_name, attr = NULL;
27098
27099   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27100     return cp_parser_attributes_opt (parser);
27101
27102   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27103     return NULL_TREE;
27104   cp_lexer_consume_token (parser->lexer);
27105   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27106     goto error1;
27107
27108   token = cp_lexer_peek_token (parser->lexer);
27109   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27110     {
27111       token = cp_lexer_consume_token (parser->lexer);
27112
27113       attr_name = (token->type == CPP_KEYWORD
27114                    /* For keywords, use the canonical spelling,
27115                       not the parsed identifier.  */
27116                    ? ridpointers[(int) token->keyword]
27117                    : token->u.value);
27118       attr = build_tree_list (attr_name, NULL_TREE);
27119     }
27120   else
27121     cp_parser_error (parser, "expected identifier");
27122
27123   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27124  error1:
27125   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27126   return attr;
27127 }
27128
27129 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27130
27131    transaction-statement:
27132      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27133        compound-statement
27134      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27135 */
27136
27137 static tree
27138 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27139 {
27140   unsigned char old_in = parser->in_transaction;
27141   unsigned char this_in = 1, new_in;
27142   cp_token *token;
27143   tree stmt, attrs, noex;
27144
27145   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27146       || keyword == RID_TRANSACTION_RELAXED);
27147   token = cp_parser_require_keyword (parser, keyword,
27148       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27149           : RT_TRANSACTION_RELAXED));
27150   gcc_assert (token != NULL);
27151
27152   if (keyword == RID_TRANSACTION_RELAXED)
27153     this_in |= TM_STMT_ATTR_RELAXED;
27154   else
27155     {
27156       attrs = cp_parser_txn_attribute_opt (parser);
27157       if (attrs)
27158         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27159     }
27160
27161   /* Parse a noexcept specification.  */
27162   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27163
27164   /* Keep track if we're in the lexical scope of an outer transaction.  */
27165   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27166
27167   stmt = begin_transaction_stmt (token->location, NULL, this_in);
27168
27169   parser->in_transaction = new_in;
27170   cp_parser_compound_statement (parser, NULL, false, false);
27171   parser->in_transaction = old_in;
27172
27173   finish_transaction_stmt (stmt, NULL, this_in, noex);
27174
27175   return stmt;
27176 }
27177
27178 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27179
27180    transaction-expression:
27181      __transaction_atomic txn-noexcept-spec[opt] ( expression )
27182      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27183 */
27184
27185 static tree
27186 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27187 {
27188   unsigned char old_in = parser->in_transaction;
27189   unsigned char this_in = 1;
27190   cp_token *token;
27191   tree expr, noex;
27192   bool noex_expr;
27193
27194   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27195       || keyword == RID_TRANSACTION_RELAXED);
27196
27197   if (!flag_tm)
27198     error (keyword == RID_TRANSACTION_RELAXED
27199            ? G_("%<__transaction_relaxed%> without transactional memory "
27200                 "support enabled")
27201            : G_("%<__transaction_atomic%> without transactional memory "
27202                 "support enabled"));
27203
27204   token = cp_parser_require_keyword (parser, keyword,
27205       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27206           : RT_TRANSACTION_RELAXED));
27207   gcc_assert (token != NULL);
27208
27209   if (keyword == RID_TRANSACTION_RELAXED)
27210     this_in |= TM_STMT_ATTR_RELAXED;
27211
27212   /* Set this early.  This might mean that we allow transaction_cancel in
27213      an expression that we find out later actually has to be a constexpr.
27214      However, we expect that cxx_constant_value will be able to deal with
27215      this; also, if the noexcept has no constexpr, then what we parse next
27216      really is a transaction's body.  */
27217   parser->in_transaction = this_in;
27218
27219   /* Parse a noexcept specification.  */
27220   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27221                                                true);
27222
27223   if (!noex || !noex_expr
27224       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27225     {
27226       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27227
27228       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27229       finish_parenthesized_expr (expr);
27230
27231       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27232     }
27233   else
27234     {
27235       /* The only expression that is available got parsed for the noexcept
27236          already.  noexcept is true then.  */
27237       expr = noex;
27238       noex = boolean_true_node;
27239     }
27240
27241   expr = build_transaction_expr (token->location, expr, this_in, noex);
27242   parser->in_transaction = old_in;
27243
27244   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27245     return error_mark_node;
27246
27247   return (flag_tm ? expr : error_mark_node);
27248 }
27249
27250 /* Parse a function-transaction-block.
27251
27252    function-transaction-block:
27253      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27254          function-body
27255      __transaction_atomic txn-attribute[opt] function-try-block
27256      __transaction_relaxed ctor-initializer[opt] function-body
27257      __transaction_relaxed function-try-block
27258 */
27259
27260 static bool
27261 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27262 {
27263   unsigned char old_in = parser->in_transaction;
27264   unsigned char new_in = 1;
27265   tree compound_stmt, stmt, attrs;
27266   bool ctor_initializer_p;
27267   cp_token *token;
27268
27269   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27270       || keyword == RID_TRANSACTION_RELAXED);
27271   token = cp_parser_require_keyword (parser, keyword,
27272       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27273           : RT_TRANSACTION_RELAXED));
27274   gcc_assert (token != NULL);
27275
27276   if (keyword == RID_TRANSACTION_RELAXED)
27277     new_in |= TM_STMT_ATTR_RELAXED;
27278   else
27279     {
27280       attrs = cp_parser_txn_attribute_opt (parser);
27281       if (attrs)
27282         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27283     }
27284
27285   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27286
27287   parser->in_transaction = new_in;
27288
27289   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27290     ctor_initializer_p = cp_parser_function_try_block (parser);
27291   else
27292     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27293       (parser, /*in_function_try_block=*/false);
27294
27295   parser->in_transaction = old_in;
27296
27297   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27298
27299   return ctor_initializer_p;
27300 }
27301
27302 /* Parse a __transaction_cancel statement.
27303
27304    cancel-statement:
27305      __transaction_cancel txn-attribute[opt] ;
27306      __transaction_cancel txn-attribute[opt] throw-expression ;
27307
27308    ??? Cancel and throw is not yet implemented.  */
27309
27310 static tree
27311 cp_parser_transaction_cancel (cp_parser *parser)
27312 {
27313   cp_token *token;
27314   bool is_outer = false;
27315   tree stmt, attrs;
27316
27317   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27318                                      RT_TRANSACTION_CANCEL);
27319   gcc_assert (token != NULL);
27320
27321   attrs = cp_parser_txn_attribute_opt (parser);
27322   if (attrs)
27323     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27324
27325   /* ??? Parse cancel-and-throw here.  */
27326
27327   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27328
27329   if (!flag_tm)
27330     {
27331       error_at (token->location, "%<__transaction_cancel%> without "
27332                 "transactional memory support enabled");
27333       return error_mark_node;
27334     }
27335   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27336     {
27337       error_at (token->location, "%<__transaction_cancel%> within a "
27338                 "%<__transaction_relaxed%>");
27339       return error_mark_node;
27340     }
27341   else if (is_outer)
27342     {
27343       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27344           && !is_tm_may_cancel_outer (current_function_decl))
27345         {
27346           error_at (token->location, "outer %<__transaction_cancel%> not "
27347                     "within outer %<__transaction_atomic%>");
27348           error_at (token->location,
27349                     "  or a %<transaction_may_cancel_outer%> function");
27350           return error_mark_node;
27351         }
27352     }
27353   else if (parser->in_transaction == 0)
27354     {
27355       error_at (token->location, "%<__transaction_cancel%> not within "
27356                 "%<__transaction_atomic%>");
27357       return error_mark_node;
27358     }
27359
27360   stmt = build_tm_abort_call (token->location, is_outer);
27361   add_stmt (stmt);
27362   finish_stmt ();
27363
27364   return stmt;
27365 }
27366 \f
27367 /* The parser.  */
27368
27369 static GTY (()) cp_parser *the_parser;
27370
27371 \f
27372 /* Special handling for the first token or line in the file.  The first
27373    thing in the file might be #pragma GCC pch_preprocess, which loads a
27374    PCH file, which is a GC collection point.  So we need to handle this
27375    first pragma without benefit of an existing lexer structure.
27376
27377    Always returns one token to the caller in *FIRST_TOKEN.  This is
27378    either the true first token of the file, or the first token after
27379    the initial pragma.  */
27380
27381 static void
27382 cp_parser_initial_pragma (cp_token *first_token)
27383 {
27384   tree name = NULL;
27385
27386   cp_lexer_get_preprocessor_token (NULL, first_token);
27387   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27388     return;
27389
27390   cp_lexer_get_preprocessor_token (NULL, first_token);
27391   if (first_token->type == CPP_STRING)
27392     {
27393       name = first_token->u.value;
27394
27395       cp_lexer_get_preprocessor_token (NULL, first_token);
27396       if (first_token->type != CPP_PRAGMA_EOL)
27397         error_at (first_token->location,
27398                   "junk at end of %<#pragma GCC pch_preprocess%>");
27399     }
27400   else
27401     error_at (first_token->location, "expected string literal");
27402
27403   /* Skip to the end of the pragma.  */
27404   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27405     cp_lexer_get_preprocessor_token (NULL, first_token);
27406
27407   /* Now actually load the PCH file.  */
27408   if (name)
27409     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27410
27411   /* Read one more token to return to our caller.  We have to do this
27412      after reading the PCH file in, since its pointers have to be
27413      live.  */
27414   cp_lexer_get_preprocessor_token (NULL, first_token);
27415 }
27416
27417 /* Normal parsing of a pragma token.  Here we can (and must) use the
27418    regular lexer.  */
27419
27420 static bool
27421 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27422 {
27423   cp_token *pragma_tok;
27424   unsigned int id;
27425
27426   pragma_tok = cp_lexer_consume_token (parser->lexer);
27427   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27428   parser->lexer->in_pragma = true;
27429
27430   id = pragma_tok->pragma_kind;
27431   switch (id)
27432     {
27433     case PRAGMA_GCC_PCH_PREPROCESS:
27434       error_at (pragma_tok->location,
27435                 "%<#pragma GCC pch_preprocess%> must be first");
27436       break;
27437
27438     case PRAGMA_OMP_BARRIER:
27439       switch (context)
27440         {
27441         case pragma_compound:
27442           cp_parser_omp_barrier (parser, pragma_tok);
27443           return false;
27444         case pragma_stmt:
27445           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27446                     "used in compound statements");
27447           break;
27448         default:
27449           goto bad_stmt;
27450         }
27451       break;
27452
27453     case PRAGMA_OMP_FLUSH:
27454       switch (context)
27455         {
27456         case pragma_compound:
27457           cp_parser_omp_flush (parser, pragma_tok);
27458           return false;
27459         case pragma_stmt:
27460           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27461                     "used in compound statements");
27462           break;
27463         default:
27464           goto bad_stmt;
27465         }
27466       break;
27467
27468     case PRAGMA_OMP_TASKWAIT:
27469       switch (context)
27470         {
27471         case pragma_compound:
27472           cp_parser_omp_taskwait (parser, pragma_tok);
27473           return false;
27474         case pragma_stmt:
27475           error_at (pragma_tok->location,
27476                     "%<#pragma omp taskwait%> may only be "
27477                     "used in compound statements");
27478           break;
27479         default:
27480           goto bad_stmt;
27481         }
27482       break;
27483
27484     case PRAGMA_OMP_TASKYIELD:
27485       switch (context)
27486         {
27487         case pragma_compound:
27488           cp_parser_omp_taskyield (parser, pragma_tok);
27489           return false;
27490         case pragma_stmt:
27491           error_at (pragma_tok->location,
27492                     "%<#pragma omp taskyield%> may only be "
27493                     "used in compound statements");
27494           break;
27495         default:
27496           goto bad_stmt;
27497         }
27498       break;
27499
27500     case PRAGMA_OMP_THREADPRIVATE:
27501       cp_parser_omp_threadprivate (parser, pragma_tok);
27502       return false;
27503
27504     case PRAGMA_OMP_ATOMIC:
27505     case PRAGMA_OMP_CRITICAL:
27506     case PRAGMA_OMP_FOR:
27507     case PRAGMA_OMP_MASTER:
27508     case PRAGMA_OMP_ORDERED:
27509     case PRAGMA_OMP_PARALLEL:
27510     case PRAGMA_OMP_SECTIONS:
27511     case PRAGMA_OMP_SINGLE:
27512     case PRAGMA_OMP_TASK:
27513       if (context == pragma_external)
27514         goto bad_stmt;
27515       cp_parser_omp_construct (parser, pragma_tok);
27516       return true;
27517
27518     case PRAGMA_OMP_SECTION:
27519       error_at (pragma_tok->location, 
27520                 "%<#pragma omp section%> may only be used in "
27521                 "%<#pragma omp sections%> construct");
27522       break;
27523
27524     default:
27525       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27526       c_invoke_pragma_handler (id);
27527       break;
27528
27529     bad_stmt:
27530       cp_parser_error (parser, "expected declaration specifiers");
27531       break;
27532     }
27533
27534   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27535   return false;
27536 }
27537
27538 /* The interface the pragma parsers have to the lexer.  */
27539
27540 enum cpp_ttype
27541 pragma_lex (tree *value)
27542 {
27543   cp_token *tok;
27544   enum cpp_ttype ret;
27545
27546   tok = cp_lexer_peek_token (the_parser->lexer);
27547
27548   ret = tok->type;
27549   *value = tok->u.value;
27550
27551   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27552     ret = CPP_EOF;
27553   else if (ret == CPP_STRING)
27554     *value = cp_parser_string_literal (the_parser, false, false);
27555   else
27556     {
27557       cp_lexer_consume_token (the_parser->lexer);
27558       if (ret == CPP_KEYWORD)
27559         ret = CPP_NAME;
27560     }
27561
27562   return ret;
27563 }
27564
27565 \f
27566 /* External interface.  */
27567
27568 /* Parse one entire translation unit.  */
27569
27570 void
27571 c_parse_file (void)
27572 {
27573   static bool already_called = false;
27574
27575   if (already_called)
27576     {
27577       sorry ("inter-module optimizations not implemented for C++");
27578       return;
27579     }
27580   already_called = true;
27581
27582   the_parser = cp_parser_new ();
27583   push_deferring_access_checks (flag_access_control
27584                                 ? dk_no_deferred : dk_no_check);
27585   cp_parser_translation_unit (the_parser);
27586   the_parser = NULL;
27587 }
27588
27589 #include "gt-cp-parser.h"